blob: 0e3ce604c1a26dd5eafd76b49ad7e80bcbf8d532 [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>
Rudolf Marek837d8102010-04-25 22:47:50 +000033#include <sys/nearptr.h>
Rudolf Marek03ae5c12010-03-16 23:59:19 +000034
35#define MEM_DEV "dpmi"
36
Rudolf Marek837d8102010-04-25 22:47:50 +000037static void *realmem_map;
38
39static void *map_first_meg(unsigned long phys_addr, size_t len)
40{
41
42 if (realmem_map) {
43 return realmem_map + phys_addr;
44 }
45
46 realmem_map = valloc(1024 * 1024);
47
48 if (!realmem_map) {
49 return NULL;
50 }
51
52 if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) {
53 return NULL;
54 }
55
56 return realmem_map + phys_addr;
57}
Rudolf Marek03ae5c12010-03-16 23:59:19 +000058
59void *sys_physmap(unsigned long phys_addr, size_t len)
60{
61 int ret;
62 __dpmi_meminfo mi;
63
Rudolf Marek837d8102010-04-25 22:47:50 +000064 /* enable 4GB limit on DS descriptor */
65 if (!__djgpp_nearptr_enable()) {
66 return NULL;
67 }
68
69 if ((phys_addr + len - 1) < (1024 * 1024)) {
70 /* we need to use another method to map first 1MB */
71 return map_first_meg(phys_addr, len);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000072 }
73
74 mi.address = phys_addr;
75 mi.size = len;
76 ret = __dpmi_physical_address_mapping (&mi);
77
78 if (ret != 0) {
79 return NULL;
80 }
81
Rudolf Marek837d8102010-04-25 22:47:50 +000082 return (void *) mi.address + __djgpp_conventional_base;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000083}
84
85#define sys_physmap_rw_uncached sys_physmap
Rudolf Marek837d8102010-04-25 22:47:50 +000086#define sys_physmap_ro_cached sys_physmap
Rudolf Marek03ae5c12010-03-16 23:59:19 +000087
88void physunmap(void *virt_addr, size_t len)
89{
90 __dpmi_meminfo mi;
91
Rudolf Marek837d8102010-04-25 22:47:50 +000092 /* we ignore unmaps for our first 1MB */
93 if ((virt_addr >= realmem_map) && ((virt_addr + len) <= (realmem_map + (1024 * 1024)))) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +000094 return;
95 }
96
97 mi.address = (unsigned long) virt_addr;
98 __dpmi_free_physical_address_mapping(&mi);
99}
100
101#elif defined(__DARWIN__)
102
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000103#include <DirectIO/darwinio.h>
104
105#define MEM_DEV "DirectIO"
106
107void *sys_physmap(unsigned long phys_addr, size_t len)
108{
109 return map_physical(phys_addr, len);
110}
111
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000112/* The OS X driver does not differentiate between mapping types. */
113#define sys_physmap_rw_uncached sys_physmap
114#define sys_physmap_ro_cached sys_physmap
115
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000116void physunmap(void *virt_addr, size_t len)
117{
118 unmap_physical(virt_addr, len);
119}
120
121#else
122#include <sys/mman.h>
123
Stefan Reinauer0593f212009-01-26 01:10:48 +0000124#if defined (__sun) && (defined(__i386) || defined(__amd64))
125# define MEM_DEV "/dev/xsvc"
126#else
127# define MEM_DEV "/dev/mem"
128#endif
129
130static int fd_mem = -1;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000131static int fd_mem_cached = -1;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000132
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000133/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
134void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000135{
136 void *virt_addr;
137
138 if (-1 == fd_mem) {
139 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000140 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Reinauer0593f212009-01-26 01:10:48 +0000141 perror("Critical error: open(" MEM_DEV ")");
Peter Stuge43438ba2009-01-26 02:04:19 +0000142 exit(2);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000143 }
144 }
145
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000146 virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
147 fd_mem, (off_t)phys_addr);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000148 return MAP_FAILED == virt_addr ? NULL : virt_addr;
149}
150
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000151/* For reading DMI/coreboot/whatever tables. We should never write, and we
152 * do not care about caching.
153 */
154void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len)
155{
156 void *virt_addr;
157
158 if (-1 == fd_mem_cached) {
159 /* Open the memory device CACHED. */
160 if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) {
161 perror("Critical error: open(" MEM_DEV ")");
162 exit(2);
163 }
164 }
165
166 virt_addr = mmap(0, len, PROT_READ, MAP_SHARED,
167 fd_mem_cached, (off_t)phys_addr);
168 return MAP_FAILED == virt_addr ? NULL : virt_addr;
169}
170
Stefan Reinauer0593f212009-01-26 01:10:48 +0000171void physunmap(void *virt_addr, size_t len)
172{
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000173 if (len == 0) {
174 printf_debug("Not unmapping zero size at %p\n", virt_addr);
175 return;
176 }
177
Stefan Reinauer0593f212009-01-26 01:10:48 +0000178 munmap(virt_addr, len);
179}
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000180#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +0000181
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000182#define PHYSMAP_NOFAIL 0
183#define PHYSMAP_MAYFAIL 1
184#define PHYSMAP_RW 0
185#define PHYSMAP_RO 1
186
187void *physmap_common(const char *descr, unsigned long phys_addr, size_t len, int mayfail, int readonly)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000188{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000189 void *virt_addr;
190
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000191 if (len == 0) {
Uwe Hermanneaefb482009-05-17 22:57:34 +0000192 printf_debug("Not mapping %s, zero size at 0x%08lx.\n",
193 descr, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000194 return NULL;
195 }
196
197 if ((getpagesize() - 1) & len) {
Uwe Hermanneaefb482009-05-17 22:57:34 +0000198 fprintf(stderr, "Mapping %s at 0x%08lx, unaligned size 0x%lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000199 descr, phys_addr, (unsigned long)len);
200 }
201
202 if ((getpagesize() - 1) & phys_addr) {
Uwe Hermanneaefb482009-05-17 22:57:34 +0000203 fprintf(stderr, "Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000204 descr, (unsigned long)len, phys_addr);
205 }
206
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000207 if (readonly) {
208 virt_addr = sys_physmap_ro_cached(phys_addr, len);
209 } else {
210 virt_addr = sys_physmap_rw_uncached(phys_addr, len);
211 }
Stefan Reinauer0593f212009-01-26 01:10:48 +0000212
213 if (NULL == virt_addr) {
214 if (NULL == descr)
215 descr = "memory";
216 fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
217 perror(MEM_DEV " mmap failed");
218 if (EINVAL == errno) {
219 fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
220 fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
221 fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
222 fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
223 fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
224 }
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000225 if (!mayfail)
226 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000227 }
228
229 return virt_addr;
230}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000231
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000232void *physmap(const char *descr, unsigned long phys_addr, size_t len)
233{
234 return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL, PHYSMAP_RW);
235}
236
237void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len)
238{
239 return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL, PHYSMAP_RO);
240}
241
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000242#ifdef __linux__
243/*
244 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
245 * which are ring0 privileged instructions so only the kernel can do the
246 * read/write. This function, therefore, requires that the msr kernel module
247 * be loaded to access these instructions from user space using device
248 * /dev/cpu/0/msr.
249 */
250
251static int fd_msr = -1;
252
253msr_t rdmsr(int addr)
254{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000255 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000256 msr_t msr = { 0xffffffff, 0xffffffff };
257
258 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
259 perror("Could not lseek() to MSR");
260 close(fd_msr);
261 exit(1);
262 }
263
264 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000265 msr.lo = buf[0];
266 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000267
268 return msr;
269 }
270
271 if (errno != EIO) {
272 // A severe error.
273 perror("Could not read() MSR");
274 close(fd_msr);
275 exit(1);
276 }
277
278 return msr;
279}
280
281int wrmsr(int addr, msr_t msr)
282{
283 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
284 perror("Could not lseek() to MSR");
285 close(fd_msr);
286 exit(1);
287 }
288
289 if (write(fd_msr, &msr, 8) != 8 && errno != EIO) {
290 perror("Could not write() MSR");
291 close(fd_msr);
292 exit(1);
293 }
294
295 /* some MSRs must not be written */
296 if (errno == EIO)
297 return -1;
298
299 return 0;
300}
301
302int setup_cpu_msr(int cpu)
303{
304 char msrfilename[64];
305 memset(msrfilename, 0, 64);
306 sprintf(msrfilename, "/dev/cpu/%d/msr", cpu);
307
308 if (fd_msr != -1) {
309 printf("MSR was already initialized\n");
310 return -1;
311 }
312
313 fd_msr = open(msrfilename, O_RDWR);
314
315 if (fd_msr < 0) {
316 perror("Error while opening /dev/cpu/0/msr");
317 printf("Did you run 'modprobe msr'?\n");
318 return -1;
319 }
320
321 return 0;
322}
323
324void cleanup_cpu_msr(void)
325{
326 if (fd_msr == -1) {
327 printf("No MSR initialized.\n");
328 return;
329 }
330
331 close(fd_msr);
332
333 /* Clear MSR file descriptor */
334 fd_msr = -1;
335}
336#else
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000337#if defined(__FreeBSD__) || defined(__DragonFly__)
338#include <sys/ioctl.h>
339
340typedef struct {
341 int msr;
342 uint64_t data;
343} cpu_msr_args_t;
344#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
345#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
346
347static int fd_msr = -1;
348
349msr_t rdmsr(int addr)
350{
351 cpu_msr_args_t args;
352
353 msr_t msr = { 0xffffffff, 0xffffffff };
354
355 args.msr = addr;
356
357 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
358 perror("CPU_RDMSR");
359 close(fd_msr);
360 exit(1);
361 }
362
363 msr.lo = args.data & 0xffffffff;
364 msr.hi = args.data >> 32;
365
366 return msr;
367}
368
369int wrmsr(int addr, msr_t msr)
370{
371 cpu_msr_args_t args;
372
373 args.msr = addr;
374 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
375
376 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
377 perror("CPU_WRMSR");
378 close(fd_msr);
379 exit(1);
380 }
381
382 return 0;
383}
384
385int setup_cpu_msr(int cpu)
386{
387 char msrfilename[64];
388 memset(msrfilename, 0, 64);
389 sprintf(msrfilename, "/dev/cpu%d", cpu);
390
391 if (fd_msr != -1) {
392 printf("MSR was already initialized\n");
393 return -1;
394 }
395
396 fd_msr = open(msrfilename, O_RDWR);
397
398 if (fd_msr < 0) {
399 perror("Error while opening /dev/cpu0");
400 printf("Did you install ports/sysutils/devcpu?\n");
401 return -1;
402 }
403
404 return 0;
405}
406
407void cleanup_cpu_msr(void)
408{
409 if (fd_msr == -1) {
410 printf("No MSR initialized.\n");
411 return;
412 }
413
414 close(fd_msr);
415
416 /* Clear MSR file descriptor */
417 fd_msr = -1;
418}
419
420#else
421
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000422#ifdef __DARWIN__
423int setup_cpu_msr(int cpu)
424{
425 // Always succeed for now
426 return 0;
427}
428
429void cleanup_cpu_msr(void)
430{
431 // Nothing, yet.
432}
433#else
434msr_t rdmsr(int addr)
435{
436 msr_t ret = { 0xffffffff, 0xffffffff };
437
438 return ret;
439}
440
441int wrmsr(int addr, msr_t msr)
442{
443 return -1;
444}
445
446int setup_cpu_msr(int cpu)
447{
448 printf("No MSR support for your OS yet.\n");
449 return -1;
450}
451
452void cleanup_cpu_msr(void)
453{
454 // Nothing, yet.
455}
456#endif
457#endif
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000458#endif
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000459