blob: ded34851729571814768a63cb31ae10ad78a8db1 [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>
Stefan Reinauer0593f212009-01-26 01:10:48 +000026#include <stdlib.h>
Stefan Reinauer8fa64812009-08-12 09:27:45 +000027#include <string.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000028#include "flash.h"
29
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000030/* Do we need any file access or ioctl for physmap or MSR? */
31#if !defined(__DJGPP__)
32#include <sys/stat.h>
33#include <fcntl.h>
34#include <errno.h>
35#endif
36
Rudolf Marek03ae5c12010-03-16 23:59:19 +000037#ifdef __DJGPP__
38#include <dpmi.h>
Rudolf Marek837d8102010-04-25 22:47:50 +000039#include <sys/nearptr.h>
Rudolf Marek03ae5c12010-03-16 23:59:19 +000040
41#define MEM_DEV "dpmi"
42
Rudolf Marek837d8102010-04-25 22:47:50 +000043static void *realmem_map;
44
45static void *map_first_meg(unsigned long phys_addr, size_t len)
46{
47
48 if (realmem_map) {
49 return realmem_map + phys_addr;
50 }
51
52 realmem_map = valloc(1024 * 1024);
53
54 if (!realmem_map) {
55 return NULL;
56 }
57
58 if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) {
59 return NULL;
60 }
61
62 return realmem_map + phys_addr;
63}
Rudolf Marek03ae5c12010-03-16 23:59:19 +000064
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000065static void *sys_physmap(unsigned long phys_addr, size_t len)
Rudolf Marek03ae5c12010-03-16 23:59:19 +000066{
67 int ret;
68 __dpmi_meminfo mi;
69
Rudolf Marek837d8102010-04-25 22:47:50 +000070 /* enable 4GB limit on DS descriptor */
71 if (!__djgpp_nearptr_enable()) {
72 return NULL;
73 }
74
75 if ((phys_addr + len - 1) < (1024 * 1024)) {
76 /* we need to use another method to map first 1MB */
77 return map_first_meg(phys_addr, len);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000078 }
79
80 mi.address = phys_addr;
81 mi.size = len;
82 ret = __dpmi_physical_address_mapping (&mi);
83
84 if (ret != 0) {
85 return NULL;
86 }
87
Rudolf Marek837d8102010-04-25 22:47:50 +000088 return (void *) mi.address + __djgpp_conventional_base;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000089}
90
91#define sys_physmap_rw_uncached sys_physmap
Rudolf Marek837d8102010-04-25 22:47:50 +000092#define sys_physmap_ro_cached sys_physmap
Rudolf Marek03ae5c12010-03-16 23:59:19 +000093
94void physunmap(void *virt_addr, size_t len)
95{
96 __dpmi_meminfo mi;
97
Rudolf Marek837d8102010-04-25 22:47:50 +000098 /* we ignore unmaps for our first 1MB */
99 if ((virt_addr >= realmem_map) && ((virt_addr + len) <= (realmem_map + (1024 * 1024)))) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000100 return;
101 }
102
103 mi.address = (unsigned long) virt_addr;
104 __dpmi_free_physical_address_mapping(&mi);
105}
106
107#elif defined(__DARWIN__)
108
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000109#include <DirectIO/darwinio.h>
110
111#define MEM_DEV "DirectIO"
112
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000113static void *sys_physmap(unsigned long phys_addr, size_t len)
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000114{
115 return map_physical(phys_addr, len);
116}
117
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000118/* The OS X driver does not differentiate between mapping types. */
119#define sys_physmap_rw_uncached sys_physmap
120#define sys_physmap_ro_cached sys_physmap
121
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000122void physunmap(void *virt_addr, size_t len)
123{
124 unmap_physical(virt_addr, len);
125}
126
127#else
128#include <sys/mman.h>
129
Stefan Reinauer0593f212009-01-26 01:10:48 +0000130#if defined (__sun) && (defined(__i386) || defined(__amd64))
131# define MEM_DEV "/dev/xsvc"
132#else
133# define MEM_DEV "/dev/mem"
134#endif
135
136static int fd_mem = -1;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000137static int fd_mem_cached = -1;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000138
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000139/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000140static void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000141{
142 void *virt_addr;
143
144 if (-1 == fd_mem) {
145 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000146 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Reinauer0593f212009-01-26 01:10:48 +0000147 perror("Critical error: open(" MEM_DEV ")");
Peter Stuge43438ba2009-01-26 02:04:19 +0000148 exit(2);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000149 }
150 }
151
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000152 virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
153 fd_mem, (off_t)phys_addr);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000154 return MAP_FAILED == virt_addr ? NULL : virt_addr;
155}
156
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000157/* For reading DMI/coreboot/whatever tables. We should never write, and we
158 * do not care about caching.
159 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000160static void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000161{
162 void *virt_addr;
163
164 if (-1 == fd_mem_cached) {
165 /* Open the memory device CACHED. */
166 if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000167 msg_perr("Critical error: open(" MEM_DEV "): %s", strerror(errno));
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000168 exit(2);
169 }
170 }
171
172 virt_addr = mmap(0, len, PROT_READ, MAP_SHARED,
173 fd_mem_cached, (off_t)phys_addr);
174 return MAP_FAILED == virt_addr ? NULL : virt_addr;
175}
176
Stefan Reinauer0593f212009-01-26 01:10:48 +0000177void physunmap(void *virt_addr, size_t len)
178{
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000179 if (len == 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000180 msg_pspew("Not unmapping zero size at %p\n", virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000181 return;
182 }
183
Stefan Reinauer0593f212009-01-26 01:10:48 +0000184 munmap(virt_addr, len);
185}
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000186#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +0000187
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000188#define PHYSMAP_NOFAIL 0
189#define PHYSMAP_MAYFAIL 1
190#define PHYSMAP_RW 0
191#define PHYSMAP_RO 1
192
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000193static void *physmap_common(const char *descr, unsigned long phys_addr, size_t len, int mayfail, int readonly)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000194{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000195 void *virt_addr;
196
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000197 if (len == 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000198 msg_pspew("Not mapping %s, zero size at 0x%08lx.\n",
Uwe Hermanneaefb482009-05-17 22:57:34 +0000199 descr, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000200 return NULL;
201 }
202
203 if ((getpagesize() - 1) & len) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000204 msg_perr("Mapping %s at 0x%08lx, unaligned size 0x%lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000205 descr, phys_addr, (unsigned long)len);
206 }
207
208 if ((getpagesize() - 1) & phys_addr) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000209 msg_perr("Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000210 descr, (unsigned long)len, phys_addr);
211 }
212
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000213 if (readonly) {
214 virt_addr = sys_physmap_ro_cached(phys_addr, len);
215 } else {
216 virt_addr = sys_physmap_rw_uncached(phys_addr, len);
217 }
Stefan Reinauer0593f212009-01-26 01:10:48 +0000218
219 if (NULL == virt_addr) {
220 if (NULL == descr)
221 descr = "memory";
Sean Nelson316a29f2010-05-07 20:09:04 +0000222 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 +0000223 perror(MEM_DEV " mmap failed");
Sean Nelson316a29f2010-05-07 20:09:04 +0000224#ifdef __linux__
Stefan Reinauer0593f212009-01-26 01:10:48 +0000225 if (EINVAL == errno) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000226 msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
227 msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
228 msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n");
229 msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
230 msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
Stefan Reinauer0593f212009-01-26 01:10:48 +0000231 }
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000232#elif defined (__OpenBSD__)
233 msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
234 "and reboot, or reboot into \n");
235 msg_perr("single user mode.\n");
Sean Nelson316a29f2010-05-07 20:09:04 +0000236#endif
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000237 if (!mayfail)
238 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000239 }
240
241 return virt_addr;
242}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000243
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000244void *physmap(const char *descr, unsigned long phys_addr, size_t len)
245{
246 return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL, PHYSMAP_RW);
247}
248
249void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len)
250{
251 return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL, PHYSMAP_RO);
252}
253
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000254#if defined(__i386__) || defined(__x86_64__)
255
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000256#ifdef __linux__
257/*
258 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
259 * which are ring0 privileged instructions so only the kernel can do the
260 * read/write. This function, therefore, requires that the msr kernel module
261 * be loaded to access these instructions from user space using device
262 * /dev/cpu/0/msr.
263 */
264
265static int fd_msr = -1;
266
267msr_t rdmsr(int addr)
268{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000269 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000270 msr_t msr = { 0xffffffff, 0xffffffff };
271
272 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
273 perror("Could not lseek() to MSR");
274 close(fd_msr);
275 exit(1);
276 }
277
278 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000279 msr.lo = buf[0];
280 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000281
282 return msr;
283 }
284
285 if (errno != EIO) {
286 // A severe error.
287 perror("Could not read() MSR");
288 close(fd_msr);
289 exit(1);
290 }
291
292 return msr;
293}
294
295int wrmsr(int addr, msr_t msr)
296{
Anti Sullinbe24d812010-05-17 23:19:22 +0000297 uint32_t buf[2];
298 buf[0] = msr.lo;
299 buf[1] = msr.hi;
300
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000301 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
302 perror("Could not lseek() to MSR");
303 close(fd_msr);
304 exit(1);
305 }
306
Anti Sullinbe24d812010-05-17 23:19:22 +0000307 if (write(fd_msr, buf, 8) != 8 && errno != EIO) {
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000308 perror("Could not write() MSR");
309 close(fd_msr);
310 exit(1);
311 }
312
313 /* some MSRs must not be written */
314 if (errno == EIO)
315 return -1;
316
317 return 0;
318}
319
320int setup_cpu_msr(int cpu)
321{
322 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000323 memset(msrfilename, 0, sizeof(msrfilename));
324 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu);
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000325
326 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000327 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000328 return -1;
329 }
330
331 fd_msr = open(msrfilename, O_RDWR);
332
333 if (fd_msr < 0) {
334 perror("Error while opening /dev/cpu/0/msr");
Sean Nelson316a29f2010-05-07 20:09:04 +0000335 msg_pinfo("Did you run 'modprobe msr'?\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000336 return -1;
337 }
338
339 return 0;
340}
341
342void cleanup_cpu_msr(void)
343{
344 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000345 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000346 return;
347 }
348
349 close(fd_msr);
350
351 /* Clear MSR file descriptor */
352 fd_msr = -1;
353}
354#else
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000355#if defined(__FreeBSD__) || defined(__DragonFly__)
356#include <sys/ioctl.h>
357
358typedef struct {
359 int msr;
360 uint64_t data;
361} cpu_msr_args_t;
362#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
363#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
364
365static int fd_msr = -1;
366
367msr_t rdmsr(int addr)
368{
369 cpu_msr_args_t args;
370
371 msr_t msr = { 0xffffffff, 0xffffffff };
372
373 args.msr = addr;
374
375 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
376 perror("CPU_RDMSR");
377 close(fd_msr);
378 exit(1);
379 }
380
381 msr.lo = args.data & 0xffffffff;
382 msr.hi = args.data >> 32;
383
384 return msr;
385}
386
387int wrmsr(int addr, msr_t msr)
388{
389 cpu_msr_args_t args;
390
391 args.msr = addr;
392 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
393
394 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
395 perror("CPU_WRMSR");
396 close(fd_msr);
397 exit(1);
398 }
399
400 return 0;
401}
402
403int setup_cpu_msr(int cpu)
404{
405 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000406 memset(msrfilename, 0, sizeof(msrfilename));
407 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu);
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000408
409 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000410 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000411 return -1;
412 }
413
414 fd_msr = open(msrfilename, O_RDWR);
415
416 if (fd_msr < 0) {
417 perror("Error while opening /dev/cpu0");
Sean Nelson316a29f2010-05-07 20:09:04 +0000418 msg_pinfo("Did you install ports/sysutils/devcpu?\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000419 return -1;
420 }
421
422 return 0;
423}
424
425void cleanup_cpu_msr(void)
426{
427 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000428 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000429 return;
430 }
431
432 close(fd_msr);
433
434 /* Clear MSR file descriptor */
435 fd_msr = -1;
436}
437
438#else
439
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000440#ifdef __DARWIN__
441int setup_cpu_msr(int cpu)
442{
443 // Always succeed for now
444 return 0;
445}
446
447void cleanup_cpu_msr(void)
448{
449 // Nothing, yet.
450}
451#else
452msr_t rdmsr(int addr)
453{
454 msr_t ret = { 0xffffffff, 0xffffffff };
455
456 return ret;
457}
458
459int wrmsr(int addr, msr_t msr)
460{
461 return -1;
462}
463
464int setup_cpu_msr(int cpu)
465{
Sean Nelson316a29f2010-05-07 20:09:04 +0000466 msg_pinfo("No MSR support for your OS yet.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000467 return -1;
468}
469
470void cleanup_cpu_msr(void)
471{
472 // Nothing, yet.
473}
474#endif
475#endif
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000476#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000477#else
478/* Does MSR exist on non-x86 architectures? */
479#endif