blob: fba7a53b78146b4a97c630de9f053573778ac304 [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
Peter Stuge33fefca2009-01-26 01:33:02 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Uwe Hermann7b2969b2009-04-15 10:52:49 +000021
Stefan Reinauer0593f212009-01-26 01:10:48 +000022#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <stdlib.h>
Stefan Reinauer8fa64812009-08-12 09:27:45 +000026#include <string.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000027#include <errno.h>
28#include "flash.h"
29
Stefan Reinauerf79edb92009-01-26 01:23:31 +000030#ifdef __DARWIN__
31#include <DirectIO/darwinio.h>
32
33#define MEM_DEV "DirectIO"
34
35void *sys_physmap(unsigned long phys_addr, size_t len)
36{
37 return map_physical(phys_addr, len);
38}
39
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +000040/* The OS X driver does not differentiate between mapping types. */
41#define sys_physmap_rw_uncached sys_physmap
42#define sys_physmap_ro_cached sys_physmap
43
Stefan Reinauerf79edb92009-01-26 01:23:31 +000044void physunmap(void *virt_addr, size_t len)
45{
46 unmap_physical(virt_addr, len);
47}
48
49#else
50#include <sys/mman.h>
51
Stefan Reinauer0593f212009-01-26 01:10:48 +000052#if defined (__sun) && (defined(__i386) || defined(__amd64))
53# define MEM_DEV "/dev/xsvc"
54#else
55# define MEM_DEV "/dev/mem"
56#endif
57
58static int fd_mem = -1;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +000059static int fd_mem_cached = -1;
Stefan Reinauer0593f212009-01-26 01:10:48 +000060
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +000061/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
62void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +000063{
64 void *virt_addr;
65
66 if (-1 == fd_mem) {
67 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +000068 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Reinauer0593f212009-01-26 01:10:48 +000069 perror("Critical error: open(" MEM_DEV ")");
Peter Stuge43438ba2009-01-26 02:04:19 +000070 exit(2);
Stefan Reinauer0593f212009-01-26 01:10:48 +000071 }
72 }
73
Uwe Hermann7b2969b2009-04-15 10:52:49 +000074 virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
75 fd_mem, (off_t)phys_addr);
Stefan Reinauer0593f212009-01-26 01:10:48 +000076 return MAP_FAILED == virt_addr ? NULL : virt_addr;
77}
78
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +000079/* For reading DMI/coreboot/whatever tables. We should never write, and we
80 * do not care about caching.
81 */
82void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len)
83{
84 void *virt_addr;
85
86 if (-1 == fd_mem_cached) {
87 /* Open the memory device CACHED. */
88 if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) {
89 perror("Critical error: open(" MEM_DEV ")");
90 exit(2);
91 }
92 }
93
94 virt_addr = mmap(0, len, PROT_READ, MAP_SHARED,
95 fd_mem_cached, (off_t)phys_addr);
96 return MAP_FAILED == virt_addr ? NULL : virt_addr;
97}
98
Stefan Reinauer0593f212009-01-26 01:10:48 +000099void physunmap(void *virt_addr, size_t len)
100{
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000101 if (len == 0) {
102 printf_debug("Not unmapping zero size at %p\n", virt_addr);
103 return;
104 }
105
Stefan Reinauer0593f212009-01-26 01:10:48 +0000106 munmap(virt_addr, len);
107}
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000108#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +0000109
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000110#define PHYSMAP_NOFAIL 0
111#define PHYSMAP_MAYFAIL 1
112#define PHYSMAP_RW 0
113#define PHYSMAP_RO 1
114
115void *physmap_common(const char *descr, unsigned long phys_addr, size_t len, int mayfail, int readonly)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000116{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000117 void *virt_addr;
118
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000119 if (len == 0) {
Uwe Hermanneaefb482009-05-17 22:57:34 +0000120 printf_debug("Not mapping %s, zero size at 0x%08lx.\n",
121 descr, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000122 return NULL;
123 }
124
125 if ((getpagesize() - 1) & len) {
Uwe Hermanneaefb482009-05-17 22:57:34 +0000126 fprintf(stderr, "Mapping %s at 0x%08lx, unaligned size 0x%lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000127 descr, phys_addr, (unsigned long)len);
128 }
129
130 if ((getpagesize() - 1) & phys_addr) {
Uwe Hermanneaefb482009-05-17 22:57:34 +0000131 fprintf(stderr, "Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000132 descr, (unsigned long)len, phys_addr);
133 }
134
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000135 if (readonly) {
136 virt_addr = sys_physmap_ro_cached(phys_addr, len);
137 } else {
138 virt_addr = sys_physmap_rw_uncached(phys_addr, len);
139 }
Stefan Reinauer0593f212009-01-26 01:10:48 +0000140
141 if (NULL == virt_addr) {
142 if (NULL == descr)
143 descr = "memory";
144 fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
145 perror(MEM_DEV " mmap failed");
146 if (EINVAL == errno) {
147 fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
148 fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
149 fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
150 fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
151 fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
152 }
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000153 if (!mayfail)
154 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000155 }
156
157 return virt_addr;
158}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000159
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000160void *physmap(const char *descr, unsigned long phys_addr, size_t len)
161{
162 return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL, PHYSMAP_RW);
163}
164
165void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len)
166{
167 return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL, PHYSMAP_RO);
168}
169
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000170#ifdef __linux__
171/*
172 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
173 * which are ring0 privileged instructions so only the kernel can do the
174 * read/write. This function, therefore, requires that the msr kernel module
175 * be loaded to access these instructions from user space using device
176 * /dev/cpu/0/msr.
177 */
178
179static int fd_msr = -1;
180
181msr_t rdmsr(int addr)
182{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000183 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000184 msr_t msr = { 0xffffffff, 0xffffffff };
185
186 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
187 perror("Could not lseek() to MSR");
188 close(fd_msr);
189 exit(1);
190 }
191
192 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000193 msr.lo = buf[0];
194 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000195
196 return msr;
197 }
198
199 if (errno != EIO) {
200 // A severe error.
201 perror("Could not read() MSR");
202 close(fd_msr);
203 exit(1);
204 }
205
206 return msr;
207}
208
209int wrmsr(int addr, msr_t msr)
210{
211 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
212 perror("Could not lseek() to MSR");
213 close(fd_msr);
214 exit(1);
215 }
216
217 if (write(fd_msr, &msr, 8) != 8 && errno != EIO) {
218 perror("Could not write() MSR");
219 close(fd_msr);
220 exit(1);
221 }
222
223 /* some MSRs must not be written */
224 if (errno == EIO)
225 return -1;
226
227 return 0;
228}
229
230int setup_cpu_msr(int cpu)
231{
232 char msrfilename[64];
233 memset(msrfilename, 0, 64);
234 sprintf(msrfilename, "/dev/cpu/%d/msr", cpu);
235
236 if (fd_msr != -1) {
237 printf("MSR was already initialized\n");
238 return -1;
239 }
240
241 fd_msr = open(msrfilename, O_RDWR);
242
243 if (fd_msr < 0) {
244 perror("Error while opening /dev/cpu/0/msr");
245 printf("Did you run 'modprobe msr'?\n");
246 return -1;
247 }
248
249 return 0;
250}
251
252void cleanup_cpu_msr(void)
253{
254 if (fd_msr == -1) {
255 printf("No MSR initialized.\n");
256 return;
257 }
258
259 close(fd_msr);
260
261 /* Clear MSR file descriptor */
262 fd_msr = -1;
263}
264#else
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000265#if defined(__FreeBSD__) || defined(__DragonFly__)
266#include <sys/ioctl.h>
267
268typedef struct {
269 int msr;
270 uint64_t data;
271} cpu_msr_args_t;
272#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
273#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
274
275static int fd_msr = -1;
276
277msr_t rdmsr(int addr)
278{
279 cpu_msr_args_t args;
280
281 msr_t msr = { 0xffffffff, 0xffffffff };
282
283 args.msr = addr;
284
285 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
286 perror("CPU_RDMSR");
287 close(fd_msr);
288 exit(1);
289 }
290
291 msr.lo = args.data & 0xffffffff;
292 msr.hi = args.data >> 32;
293
294 return msr;
295}
296
297int wrmsr(int addr, msr_t msr)
298{
299 cpu_msr_args_t args;
300
301 args.msr = addr;
302 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
303
304 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
305 perror("CPU_WRMSR");
306 close(fd_msr);
307 exit(1);
308 }
309
310 return 0;
311}
312
313int setup_cpu_msr(int cpu)
314{
315 char msrfilename[64];
316 memset(msrfilename, 0, 64);
317 sprintf(msrfilename, "/dev/cpu%d", cpu);
318
319 if (fd_msr != -1) {
320 printf("MSR was already initialized\n");
321 return -1;
322 }
323
324 fd_msr = open(msrfilename, O_RDWR);
325
326 if (fd_msr < 0) {
327 perror("Error while opening /dev/cpu0");
328 printf("Did you install ports/sysutils/devcpu?\n");
329 return -1;
330 }
331
332 return 0;
333}
334
335void cleanup_cpu_msr(void)
336{
337 if (fd_msr == -1) {
338 printf("No MSR initialized.\n");
339 return;
340 }
341
342 close(fd_msr);
343
344 /* Clear MSR file descriptor */
345 fd_msr = -1;
346}
347
348#else
349
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000350#ifdef __DARWIN__
351int setup_cpu_msr(int cpu)
352{
353 // Always succeed for now
354 return 0;
355}
356
357void cleanup_cpu_msr(void)
358{
359 // Nothing, yet.
360}
361#else
362msr_t rdmsr(int addr)
363{
364 msr_t ret = { 0xffffffff, 0xffffffff };
365
366 return ret;
367}
368
369int wrmsr(int addr, msr_t msr)
370{
371 return -1;
372}
373
374int setup_cpu_msr(int cpu)
375{
376 printf("No MSR support for your OS yet.\n");
377 return -1;
378}
379
380void cleanup_cpu_msr(void)
381{
382 // Nothing, yet.
383}
384#endif
385#endif
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000386#endif
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000387