blob: 76429388489e422ccfaddaeddac25ebd6ba42723 [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
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000023#include <unistd.h>
24#include <stdio.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000025#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28#include <stdlib.h>
Stefan Reinauer8fa64812009-08-12 09:27:45 +000029#include <string.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000030#include <errno.h>
31#include "flash.h"
32
Rudolf Marek03ae5c12010-03-16 23:59:19 +000033#ifdef __DJGPP__
34#include <dpmi.h>
Rudolf Marek837d8102010-04-25 22:47:50 +000035#include <sys/nearptr.h>
Rudolf Marek03ae5c12010-03-16 23:59:19 +000036
37#define MEM_DEV "dpmi"
38
Rudolf Marek837d8102010-04-25 22:47:50 +000039static void *realmem_map;
40
41static void *map_first_meg(unsigned long phys_addr, size_t len)
42{
43
44 if (realmem_map) {
45 return realmem_map + phys_addr;
46 }
47
48 realmem_map = valloc(1024 * 1024);
49
50 if (!realmem_map) {
51 return NULL;
52 }
53
54 if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) {
55 return NULL;
56 }
57
58 return realmem_map + phys_addr;
59}
Rudolf Marek03ae5c12010-03-16 23:59:19 +000060
61void *sys_physmap(unsigned long phys_addr, size_t len)
62{
63 int ret;
64 __dpmi_meminfo mi;
65
Rudolf Marek837d8102010-04-25 22:47:50 +000066 /* enable 4GB limit on DS descriptor */
67 if (!__djgpp_nearptr_enable()) {
68 return NULL;
69 }
70
71 if ((phys_addr + len - 1) < (1024 * 1024)) {
72 /* we need to use another method to map first 1MB */
73 return map_first_meg(phys_addr, len);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000074 }
75
76 mi.address = phys_addr;
77 mi.size = len;
78 ret = __dpmi_physical_address_mapping (&mi);
79
80 if (ret != 0) {
81 return NULL;
82 }
83
Rudolf Marek837d8102010-04-25 22:47:50 +000084 return (void *) mi.address + __djgpp_conventional_base;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000085}
86
87#define sys_physmap_rw_uncached sys_physmap
Rudolf Marek837d8102010-04-25 22:47:50 +000088#define sys_physmap_ro_cached sys_physmap
Rudolf Marek03ae5c12010-03-16 23:59:19 +000089
90void physunmap(void *virt_addr, size_t len)
91{
92 __dpmi_meminfo mi;
93
Rudolf Marek837d8102010-04-25 22:47:50 +000094 /* we ignore unmaps for our first 1MB */
95 if ((virt_addr >= realmem_map) && ((virt_addr + len) <= (realmem_map + (1024 * 1024)))) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +000096 return;
97 }
98
99 mi.address = (unsigned long) virt_addr;
100 __dpmi_free_physical_address_mapping(&mi);
101}
102
103#elif defined(__DARWIN__)
104
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000105#include <DirectIO/darwinio.h>
106
107#define MEM_DEV "DirectIO"
108
109void *sys_physmap(unsigned long phys_addr, size_t len)
110{
111 return map_physical(phys_addr, len);
112}
113
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000114/* The OS X driver does not differentiate between mapping types. */
115#define sys_physmap_rw_uncached sys_physmap
116#define sys_physmap_ro_cached sys_physmap
117
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000118void physunmap(void *virt_addr, size_t len)
119{
120 unmap_physical(virt_addr, len);
121}
122
123#else
124#include <sys/mman.h>
125
Stefan Reinauer0593f212009-01-26 01:10:48 +0000126#if defined (__sun) && (defined(__i386) || defined(__amd64))
127# define MEM_DEV "/dev/xsvc"
128#else
129# define MEM_DEV "/dev/mem"
130#endif
131
132static int fd_mem = -1;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000133static int fd_mem_cached = -1;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000134
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000135/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
136void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000137{
138 void *virt_addr;
139
140 if (-1 == fd_mem) {
141 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000142 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Reinauer0593f212009-01-26 01:10:48 +0000143 perror("Critical error: open(" MEM_DEV ")");
Peter Stuge43438ba2009-01-26 02:04:19 +0000144 exit(2);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000145 }
146 }
147
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000148 virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
149 fd_mem, (off_t)phys_addr);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000150 return MAP_FAILED == virt_addr ? NULL : virt_addr;
151}
152
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000153/* For reading DMI/coreboot/whatever tables. We should never write, and we
154 * do not care about caching.
155 */
156void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len)
157{
158 void *virt_addr;
159
160 if (-1 == fd_mem_cached) {
161 /* Open the memory device CACHED. */
162 if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000163 msg_perr("Critical error: open(" MEM_DEV "): %s", strerror(errno));
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000164 exit(2);
165 }
166 }
167
168 virt_addr = mmap(0, len, PROT_READ, MAP_SHARED,
169 fd_mem_cached, (off_t)phys_addr);
170 return MAP_FAILED == virt_addr ? NULL : virt_addr;
171}
172
Stefan Reinauer0593f212009-01-26 01:10:48 +0000173void physunmap(void *virt_addr, size_t len)
174{
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000175 if (len == 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000176 msg_pspew("Not unmapping zero size at %p\n", virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000177 return;
178 }
179
Stefan Reinauer0593f212009-01-26 01:10:48 +0000180 munmap(virt_addr, len);
181}
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000182#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +0000183
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000184#define PHYSMAP_NOFAIL 0
185#define PHYSMAP_MAYFAIL 1
186#define PHYSMAP_RW 0
187#define PHYSMAP_RO 1
188
189void *physmap_common(const char *descr, unsigned long phys_addr, size_t len, int mayfail, int readonly)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000190{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000191 void *virt_addr;
192
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000193 if (len == 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000194 msg_pspew("Not mapping %s, zero size at 0x%08lx.\n",
Uwe Hermanneaefb482009-05-17 22:57:34 +0000195 descr, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000196 return NULL;
197 }
198
199 if ((getpagesize() - 1) & len) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000200 msg_perr("Mapping %s at 0x%08lx, unaligned size 0x%lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000201 descr, phys_addr, (unsigned long)len);
202 }
203
204 if ((getpagesize() - 1) & phys_addr) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000205 msg_perr("Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000206 descr, (unsigned long)len, phys_addr);
207 }
208
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000209 if (readonly) {
210 virt_addr = sys_physmap_ro_cached(phys_addr, len);
211 } else {
212 virt_addr = sys_physmap_rw_uncached(phys_addr, len);
213 }
Stefan Reinauer0593f212009-01-26 01:10:48 +0000214
215 if (NULL == virt_addr) {
216 if (NULL == descr)
217 descr = "memory";
Sean Nelson316a29f2010-05-07 20:09:04 +0000218 msg_perr("Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000219 perror(MEM_DEV " mmap failed");
Sean Nelson316a29f2010-05-07 20:09:04 +0000220#ifdef __linux__
Stefan Reinauer0593f212009-01-26 01:10:48 +0000221 if (EINVAL == errno) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000222 msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
223 msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
224 msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n");
225 msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
226 msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
Stefan Reinauer0593f212009-01-26 01:10:48 +0000227 }
Sean Nelson316a29f2010-05-07 20:09:04 +0000228#endif
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000229 if (!mayfail)
230 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000231 }
232
233 return virt_addr;
234}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000235
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000236void *physmap(const char *descr, unsigned long phys_addr, size_t len)
237{
238 return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL, PHYSMAP_RW);
239}
240
241void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len)
242{
243 return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL, PHYSMAP_RO);
244}
245
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000246#if defined(__i386__) || defined(__x86_64__)
247
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000248#ifdef __linux__
249/*
250 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
251 * which are ring0 privileged instructions so only the kernel can do the
252 * read/write. This function, therefore, requires that the msr kernel module
253 * be loaded to access these instructions from user space using device
254 * /dev/cpu/0/msr.
255 */
256
257static int fd_msr = -1;
258
259msr_t rdmsr(int addr)
260{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000261 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000262 msr_t msr = { 0xffffffff, 0xffffffff };
263
264 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
265 perror("Could not lseek() to MSR");
266 close(fd_msr);
267 exit(1);
268 }
269
270 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000271 msr.lo = buf[0];
272 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000273
274 return msr;
275 }
276
277 if (errno != EIO) {
278 // A severe error.
279 perror("Could not read() MSR");
280 close(fd_msr);
281 exit(1);
282 }
283
284 return msr;
285}
286
287int wrmsr(int addr, msr_t msr)
288{
Anti Sullinbe24d812010-05-17 23:19:22 +0000289 uint32_t buf[2];
290 buf[0] = msr.lo;
291 buf[1] = msr.hi;
292
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000293 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
294 perror("Could not lseek() to MSR");
295 close(fd_msr);
296 exit(1);
297 }
298
Anti Sullinbe24d812010-05-17 23:19:22 +0000299 if (write(fd_msr, buf, 8) != 8 && errno != EIO) {
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000300 perror("Could not write() MSR");
301 close(fd_msr);
302 exit(1);
303 }
304
305 /* some MSRs must not be written */
306 if (errno == EIO)
307 return -1;
308
309 return 0;
310}
311
312int setup_cpu_msr(int cpu)
313{
314 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000315 memset(msrfilename, 0, sizeof(msrfilename));
316 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu);
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000317
318 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000319 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000320 return -1;
321 }
322
323 fd_msr = open(msrfilename, O_RDWR);
324
325 if (fd_msr < 0) {
326 perror("Error while opening /dev/cpu/0/msr");
Sean Nelson316a29f2010-05-07 20:09:04 +0000327 msg_pinfo("Did you run 'modprobe msr'?\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000328 return -1;
329 }
330
331 return 0;
332}
333
334void cleanup_cpu_msr(void)
335{
336 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000337 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000338 return;
339 }
340
341 close(fd_msr);
342
343 /* Clear MSR file descriptor */
344 fd_msr = -1;
345}
346#else
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000347#if defined(__FreeBSD__) || defined(__DragonFly__)
348#include <sys/ioctl.h>
349
350typedef struct {
351 int msr;
352 uint64_t data;
353} cpu_msr_args_t;
354#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
355#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
356
357static int fd_msr = -1;
358
359msr_t rdmsr(int addr)
360{
361 cpu_msr_args_t args;
362
363 msr_t msr = { 0xffffffff, 0xffffffff };
364
365 args.msr = addr;
366
367 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
368 perror("CPU_RDMSR");
369 close(fd_msr);
370 exit(1);
371 }
372
373 msr.lo = args.data & 0xffffffff;
374 msr.hi = args.data >> 32;
375
376 return msr;
377}
378
379int wrmsr(int addr, msr_t msr)
380{
381 cpu_msr_args_t args;
382
383 args.msr = addr;
384 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
385
386 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
387 perror("CPU_WRMSR");
388 close(fd_msr);
389 exit(1);
390 }
391
392 return 0;
393}
394
395int setup_cpu_msr(int cpu)
396{
397 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000398 memset(msrfilename, 0, sizeof(msrfilename));
399 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu);
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000400
401 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000402 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000403 return -1;
404 }
405
406 fd_msr = open(msrfilename, O_RDWR);
407
408 if (fd_msr < 0) {
409 perror("Error while opening /dev/cpu0");
Sean Nelson316a29f2010-05-07 20:09:04 +0000410 msg_pinfo("Did you install ports/sysutils/devcpu?\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000411 return -1;
412 }
413
414 return 0;
415}
416
417void cleanup_cpu_msr(void)
418{
419 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000420 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000421 return;
422 }
423
424 close(fd_msr);
425
426 /* Clear MSR file descriptor */
427 fd_msr = -1;
428}
429
430#else
431
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000432#ifdef __DARWIN__
433int setup_cpu_msr(int cpu)
434{
435 // Always succeed for now
436 return 0;
437}
438
439void cleanup_cpu_msr(void)
440{
441 // Nothing, yet.
442}
443#else
444msr_t rdmsr(int addr)
445{
446 msr_t ret = { 0xffffffff, 0xffffffff };
447
448 return ret;
449}
450
451int wrmsr(int addr, msr_t msr)
452{
453 return -1;
454}
455
456int setup_cpu_msr(int cpu)
457{
Sean Nelson316a29f2010-05-07 20:09:04 +0000458 msg_pinfo("No MSR support for your OS yet.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000459 return -1;
460}
461
462void cleanup_cpu_msr(void)
463{
464 // Nothing, yet.
465}
466#endif
467#endif
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000468#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000469#else
470/* Does MSR exist on non-x86 architectures? */
471#endif