blob: 38a395c3df212e3e508784e9119801dc9b3d35a2 [file] [log] [blame]
Peter Stuge33fefca2009-01-26 01:33:02 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Peter Stuge <peter@stuge.se>
Stefan Reinauer8fa64812009-08-12 09:27:45 +00005 * Copyright (C) 2009 coresystems GmbH
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +00006 * Copyright (C) 2010 Carl-Daniel Hailfinger
Rudolf Marek03ae5c12010-03-16 23:59:19 +00007 * Copyright (C) 2010 Rudolf Marek <r.marek@assembler.cz>
Peter Stuge33fefca2009-01-26 01:33:02 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
Uwe Hermann7b2969b2009-04-15 10:52:49 +000022
Stefan Reinauer0593f212009-01-26 01:10:48 +000023#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <stdlib.h>
Stefan Reinauer8fa64812009-08-12 09:27:45 +000027#include <string.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000028#include <errno.h>
29#include "flash.h"
30
Rudolf Marek03ae5c12010-03-16 23:59:19 +000031#ifdef __DJGPP__
32#include <dpmi.h>
33
34#define MEM_DEV "dpmi"
35
36unsigned short segFS = 0;
37
38void *sys_physmap(unsigned long phys_addr, size_t len)
39{
40 int ret;
41 __dpmi_meminfo mi;
42
43 if (segFS == 0) {
44 segFS = __dpmi_allocate_ldt_descriptors (1);
45 __dpmi_set_segment_base_address (segFS, 0x0);
46 __dpmi_set_segment_limit (segFS, 0xffffffff);
47 }
48
49 mi.address = phys_addr;
50 mi.size = len;
51 ret = __dpmi_physical_address_mapping (&mi);
52
53 if (ret != 0) {
54 return NULL;
55 }
56
57 return (void *) mi.address;
58}
59
60#define sys_physmap_rw_uncached sys_physmap
61
62#include <sys/movedata.h>
63#include <sys/segments.h>
64#include <go32.h>
65
66static void *realmem_cpy;
67
68void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len)
69{
70 /* no support for not a 1MB of mem */
71 if ((phys_addr + len) > 1024*1024)
72 return NULL;
73
74 if (realmem_cpy)
75 return realmem_cpy + phys_addr;
76
77 realmem_cpy = valloc(1024*1024);
78
79 if (!realmem_cpy)
80 return NULL;
81
82 movedata(_dos_ds, 0, _my_ds(), (unsigned long) realmem_cpy, 1024*1024);
83 return realmem_cpy + phys_addr;
84}
85
86
87void physunmap(void *virt_addr, size_t len)
88{
89 __dpmi_meminfo mi;
90
91 /* we ignore unmaps for our cheat 1MB copy */
92 if ((virt_addr >= realmem_cpy) && ((virt_addr + len) <= (realmem_cpy + 1024*1024))) {
93 return;
94 }
95
96 mi.address = (unsigned long) virt_addr;
97 __dpmi_free_physical_address_mapping(&mi);
98}
99
100#elif defined(__DARWIN__)
101
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000102#include <DirectIO/darwinio.h>
103
104#define MEM_DEV "DirectIO"
105
106void *sys_physmap(unsigned long phys_addr, size_t len)
107{
108 return map_physical(phys_addr, len);
109}
110
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000111/* The OS X driver does not differentiate between mapping types. */
112#define sys_physmap_rw_uncached sys_physmap
113#define sys_physmap_ro_cached sys_physmap
114
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000115void physunmap(void *virt_addr, size_t len)
116{
117 unmap_physical(virt_addr, len);
118}
119
120#else
121#include <sys/mman.h>
122
Stefan Reinauer0593f212009-01-26 01:10:48 +0000123#if defined (__sun) && (defined(__i386) || defined(__amd64))
124# define MEM_DEV "/dev/xsvc"
125#else
126# define MEM_DEV "/dev/mem"
127#endif
128
129static int fd_mem = -1;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000130static int fd_mem_cached = -1;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000131
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000132/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
133void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000134{
135 void *virt_addr;
136
137 if (-1 == fd_mem) {
138 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000139 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Reinauer0593f212009-01-26 01:10:48 +0000140 perror("Critical error: open(" MEM_DEV ")");
Peter Stuge43438ba2009-01-26 02:04:19 +0000141 exit(2);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000142 }
143 }
144
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000145 virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
146 fd_mem, (off_t)phys_addr);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000147 return MAP_FAILED == virt_addr ? NULL : virt_addr;
148}
149
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000150/* For reading DMI/coreboot/whatever tables. We should never write, and we
151 * do not care about caching.
152 */
153void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len)
154{
155 void *virt_addr;
156
157 if (-1 == fd_mem_cached) {
158 /* Open the memory device CACHED. */
159 if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) {
160 perror("Critical error: open(" MEM_DEV ")");
161 exit(2);
162 }
163 }
164
165 virt_addr = mmap(0, len, PROT_READ, MAP_SHARED,
166 fd_mem_cached, (off_t)phys_addr);
167 return MAP_FAILED == virt_addr ? NULL : virt_addr;
168}
169
Stefan Reinauer0593f212009-01-26 01:10:48 +0000170void physunmap(void *virt_addr, size_t len)
171{
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000172 if (len == 0) {
173 printf_debug("Not unmapping zero size at %p\n", virt_addr);
174 return;
175 }
176
Stefan Reinauer0593f212009-01-26 01:10:48 +0000177 munmap(virt_addr, len);
178}
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000179#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +0000180
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000181#define PHYSMAP_NOFAIL 0
182#define PHYSMAP_MAYFAIL 1
183#define PHYSMAP_RW 0
184#define PHYSMAP_RO 1
185
186void *physmap_common(const char *descr, unsigned long phys_addr, size_t len, int mayfail, int readonly)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000187{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000188 void *virt_addr;
189
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000190 if (len == 0) {
Uwe Hermanneaefb482009-05-17 22:57:34 +0000191 printf_debug("Not mapping %s, zero size at 0x%08lx.\n",
192 descr, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000193 return NULL;
194 }
195
196 if ((getpagesize() - 1) & len) {
Uwe Hermanneaefb482009-05-17 22:57:34 +0000197 fprintf(stderr, "Mapping %s at 0x%08lx, unaligned size 0x%lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000198 descr, phys_addr, (unsigned long)len);
199 }
200
201 if ((getpagesize() - 1) & phys_addr) {
Uwe Hermanneaefb482009-05-17 22:57:34 +0000202 fprintf(stderr, "Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000203 descr, (unsigned long)len, phys_addr);
204 }
205
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000206 if (readonly) {
207 virt_addr = sys_physmap_ro_cached(phys_addr, len);
208 } else {
209 virt_addr = sys_physmap_rw_uncached(phys_addr, len);
210 }
Stefan Reinauer0593f212009-01-26 01:10:48 +0000211
212 if (NULL == virt_addr) {
213 if (NULL == descr)
214 descr = "memory";
215 fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
216 perror(MEM_DEV " mmap failed");
217 if (EINVAL == errno) {
218 fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
219 fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
220 fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
221 fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
222 fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
223 }
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000224 if (!mayfail)
225 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000226 }
227
228 return virt_addr;
229}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000230
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000231void *physmap(const char *descr, unsigned long phys_addr, size_t len)
232{
233 return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL, PHYSMAP_RW);
234}
235
236void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len)
237{
238 return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL, PHYSMAP_RO);
239}
240
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000241#ifdef __linux__
242/*
243 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
244 * which are ring0 privileged instructions so only the kernel can do the
245 * read/write. This function, therefore, requires that the msr kernel module
246 * be loaded to access these instructions from user space using device
247 * /dev/cpu/0/msr.
248 */
249
250static int fd_msr = -1;
251
252msr_t rdmsr(int addr)
253{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000254 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000255 msr_t msr = { 0xffffffff, 0xffffffff };
256
257 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
258 perror("Could not lseek() to MSR");
259 close(fd_msr);
260 exit(1);
261 }
262
263 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000264 msr.lo = buf[0];
265 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000266
267 return msr;
268 }
269
270 if (errno != EIO) {
271 // A severe error.
272 perror("Could not read() MSR");
273 close(fd_msr);
274 exit(1);
275 }
276
277 return msr;
278}
279
280int wrmsr(int addr, msr_t msr)
281{
282 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
283 perror("Could not lseek() to MSR");
284 close(fd_msr);
285 exit(1);
286 }
287
288 if (write(fd_msr, &msr, 8) != 8 && errno != EIO) {
289 perror("Could not write() MSR");
290 close(fd_msr);
291 exit(1);
292 }
293
294 /* some MSRs must not be written */
295 if (errno == EIO)
296 return -1;
297
298 return 0;
299}
300
301int setup_cpu_msr(int cpu)
302{
303 char msrfilename[64];
304 memset(msrfilename, 0, 64);
305 sprintf(msrfilename, "/dev/cpu/%d/msr", cpu);
306
307 if (fd_msr != -1) {
308 printf("MSR was already initialized\n");
309 return -1;
310 }
311
312 fd_msr = open(msrfilename, O_RDWR);
313
314 if (fd_msr < 0) {
315 perror("Error while opening /dev/cpu/0/msr");
316 printf("Did you run 'modprobe msr'?\n");
317 return -1;
318 }
319
320 return 0;
321}
322
323void cleanup_cpu_msr(void)
324{
325 if (fd_msr == -1) {
326 printf("No MSR initialized.\n");
327 return;
328 }
329
330 close(fd_msr);
331
332 /* Clear MSR file descriptor */
333 fd_msr = -1;
334}
335#else
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000336#if defined(__FreeBSD__) || defined(__DragonFly__)
337#include <sys/ioctl.h>
338
339typedef struct {
340 int msr;
341 uint64_t data;
342} cpu_msr_args_t;
343#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
344#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
345
346static int fd_msr = -1;
347
348msr_t rdmsr(int addr)
349{
350 cpu_msr_args_t args;
351
352 msr_t msr = { 0xffffffff, 0xffffffff };
353
354 args.msr = addr;
355
356 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
357 perror("CPU_RDMSR");
358 close(fd_msr);
359 exit(1);
360 }
361
362 msr.lo = args.data & 0xffffffff;
363 msr.hi = args.data >> 32;
364
365 return msr;
366}
367
368int wrmsr(int addr, msr_t msr)
369{
370 cpu_msr_args_t args;
371
372 args.msr = addr;
373 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
374
375 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
376 perror("CPU_WRMSR");
377 close(fd_msr);
378 exit(1);
379 }
380
381 return 0;
382}
383
384int setup_cpu_msr(int cpu)
385{
386 char msrfilename[64];
387 memset(msrfilename, 0, 64);
388 sprintf(msrfilename, "/dev/cpu%d", cpu);
389
390 if (fd_msr != -1) {
391 printf("MSR was already initialized\n");
392 return -1;
393 }
394
395 fd_msr = open(msrfilename, O_RDWR);
396
397 if (fd_msr < 0) {
398 perror("Error while opening /dev/cpu0");
399 printf("Did you install ports/sysutils/devcpu?\n");
400 return -1;
401 }
402
403 return 0;
404}
405
406void cleanup_cpu_msr(void)
407{
408 if (fd_msr == -1) {
409 printf("No MSR initialized.\n");
410 return;
411 }
412
413 close(fd_msr);
414
415 /* Clear MSR file descriptor */
416 fd_msr = -1;
417}
418
419#else
420
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000421#ifdef __DARWIN__
422int setup_cpu_msr(int cpu)
423{
424 // Always succeed for now
425 return 0;
426}
427
428void cleanup_cpu_msr(void)
429{
430 // Nothing, yet.
431}
432#else
433msr_t rdmsr(int addr)
434{
435 msr_t ret = { 0xffffffff, 0xffffffff };
436
437 return ret;
438}
439
440int wrmsr(int addr, msr_t msr)
441{
442 return -1;
443}
444
445int setup_cpu_msr(int cpu)
446{
447 printf("No MSR support for your OS yet.\n");
448 return -1;
449}
450
451void cleanup_cpu_msr(void)
452{
453 // Nothing, yet.
454}
455#endif
456#endif
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000457#endif
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000458