blob: 3a003feeaf4a78c11c9c9a080dc761e7ced1455c [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
Peter Stuge33fefca2009-01-26 01:33:02 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
Uwe Hermann7b2969b2009-04-15 10:52:49 +000020
Stefan Reinauer0593f212009-01-26 01:10:48 +000021#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <stdlib.h>
Stefan Reinauer8fa64812009-08-12 09:27:45 +000025#include <string.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000026#include <errno.h>
27#include "flash.h"
28
Stefan Reinauerf79edb92009-01-26 01:23:31 +000029#ifdef __DARWIN__
30#include <DirectIO/darwinio.h>
31
32#define MEM_DEV "DirectIO"
33
34void *sys_physmap(unsigned long phys_addr, size_t len)
35{
36 return map_physical(phys_addr, len);
37}
38
39void physunmap(void *virt_addr, size_t len)
40{
41 unmap_physical(virt_addr, len);
42}
43
44#else
45#include <sys/mman.h>
46
Stefan Reinauer0593f212009-01-26 01:10:48 +000047#if defined (__sun) && (defined(__i386) || defined(__amd64))
48# define MEM_DEV "/dev/xsvc"
49#else
50# define MEM_DEV "/dev/mem"
51#endif
52
53static int fd_mem = -1;
54
55void *sys_physmap(unsigned long phys_addr, size_t len)
56{
57 void *virt_addr;
58
59 if (-1 == fd_mem) {
60 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +000061 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Reinauer0593f212009-01-26 01:10:48 +000062 perror("Critical error: open(" MEM_DEV ")");
Peter Stuge43438ba2009-01-26 02:04:19 +000063 exit(2);
Stefan Reinauer0593f212009-01-26 01:10:48 +000064 }
65 }
66
Uwe Hermann7b2969b2009-04-15 10:52:49 +000067 virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
68 fd_mem, (off_t)phys_addr);
Stefan Reinauer0593f212009-01-26 01:10:48 +000069 return MAP_FAILED == virt_addr ? NULL : virt_addr;
70}
71
72void physunmap(void *virt_addr, size_t len)
73{
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000074 if (len == 0) {
75 printf_debug("Not unmapping zero size at %p\n", virt_addr);
76 return;
77 }
78
Stefan Reinauer0593f212009-01-26 01:10:48 +000079 munmap(virt_addr, len);
80}
Stefan Reinauerf79edb92009-01-26 01:23:31 +000081#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +000082
83void *physmap(const char *descr, unsigned long phys_addr, size_t len)
84{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +000085 void *virt_addr;
86
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000087 if (len == 0) {
Uwe Hermanneaefb482009-05-17 22:57:34 +000088 printf_debug("Not mapping %s, zero size at 0x%08lx.\n",
89 descr, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000090 return NULL;
91 }
92
93 if ((getpagesize() - 1) & len) {
Uwe Hermanneaefb482009-05-17 22:57:34 +000094 fprintf(stderr, "Mapping %s at 0x%08lx, unaligned size 0x%lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000095 descr, phys_addr, (unsigned long)len);
96 }
97
98 if ((getpagesize() - 1) & phys_addr) {
Uwe Hermanneaefb482009-05-17 22:57:34 +000099 fprintf(stderr, "Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n",
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000100 descr, (unsigned long)len, phys_addr);
101 }
102
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000103 virt_addr = sys_physmap(phys_addr, len);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000104
105 if (NULL == virt_addr) {
106 if (NULL == descr)
107 descr = "memory";
108 fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
109 perror(MEM_DEV " mmap failed");
110 if (EINVAL == errno) {
111 fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
112 fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
113 fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
114 fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
115 fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
116 }
Peter Stuge43438ba2009-01-26 02:04:19 +0000117 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000118 }
119
120 return virt_addr;
121}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000122
123#ifdef __linux__
124/*
125 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
126 * which are ring0 privileged instructions so only the kernel can do the
127 * read/write. This function, therefore, requires that the msr kernel module
128 * be loaded to access these instructions from user space using device
129 * /dev/cpu/0/msr.
130 */
131
132static int fd_msr = -1;
133
134msr_t rdmsr(int addr)
135{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000136 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000137 msr_t msr = { 0xffffffff, 0xffffffff };
138
139 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
140 perror("Could not lseek() to MSR");
141 close(fd_msr);
142 exit(1);
143 }
144
145 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000146 msr.lo = buf[0];
147 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000148
149 return msr;
150 }
151
152 if (errno != EIO) {
153 // A severe error.
154 perror("Could not read() MSR");
155 close(fd_msr);
156 exit(1);
157 }
158
159 return msr;
160}
161
162int wrmsr(int addr, msr_t msr)
163{
164 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
165 perror("Could not lseek() to MSR");
166 close(fd_msr);
167 exit(1);
168 }
169
170 if (write(fd_msr, &msr, 8) != 8 && errno != EIO) {
171 perror("Could not write() MSR");
172 close(fd_msr);
173 exit(1);
174 }
175
176 /* some MSRs must not be written */
177 if (errno == EIO)
178 return -1;
179
180 return 0;
181}
182
183int setup_cpu_msr(int cpu)
184{
185 char msrfilename[64];
186 memset(msrfilename, 0, 64);
187 sprintf(msrfilename, "/dev/cpu/%d/msr", cpu);
188
189 if (fd_msr != -1) {
190 printf("MSR was already initialized\n");
191 return -1;
192 }
193
194 fd_msr = open(msrfilename, O_RDWR);
195
196 if (fd_msr < 0) {
197 perror("Error while opening /dev/cpu/0/msr");
198 printf("Did you run 'modprobe msr'?\n");
199 return -1;
200 }
201
202 return 0;
203}
204
205void cleanup_cpu_msr(void)
206{
207 if (fd_msr == -1) {
208 printf("No MSR initialized.\n");
209 return;
210 }
211
212 close(fd_msr);
213
214 /* Clear MSR file descriptor */
215 fd_msr = -1;
216}
217#else
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000218#if defined(__FreeBSD__) || defined(__DragonFly__)
219#include <sys/ioctl.h>
220
221typedef struct {
222 int msr;
223 uint64_t data;
224} cpu_msr_args_t;
225#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
226#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
227
228static int fd_msr = -1;
229
230msr_t rdmsr(int addr)
231{
232 cpu_msr_args_t args;
233
234 msr_t msr = { 0xffffffff, 0xffffffff };
235
236 args.msr = addr;
237
238 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
239 perror("CPU_RDMSR");
240 close(fd_msr);
241 exit(1);
242 }
243
244 msr.lo = args.data & 0xffffffff;
245 msr.hi = args.data >> 32;
246
247 return msr;
248}
249
250int wrmsr(int addr, msr_t msr)
251{
252 cpu_msr_args_t args;
253
254 args.msr = addr;
255 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
256
257 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
258 perror("CPU_WRMSR");
259 close(fd_msr);
260 exit(1);
261 }
262
263 return 0;
264}
265
266int setup_cpu_msr(int cpu)
267{
268 char msrfilename[64];
269 memset(msrfilename, 0, 64);
270 sprintf(msrfilename, "/dev/cpu%d", cpu);
271
272 if (fd_msr != -1) {
273 printf("MSR was already initialized\n");
274 return -1;
275 }
276
277 fd_msr = open(msrfilename, O_RDWR);
278
279 if (fd_msr < 0) {
280 perror("Error while opening /dev/cpu0");
281 printf("Did you install ports/sysutils/devcpu?\n");
282 return -1;
283 }
284
285 return 0;
286}
287
288void cleanup_cpu_msr(void)
289{
290 if (fd_msr == -1) {
291 printf("No MSR initialized.\n");
292 return;
293 }
294
295 close(fd_msr);
296
297 /* Clear MSR file descriptor */
298 fd_msr = -1;
299}
300
301#else
302
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000303#ifdef __DARWIN__
304int setup_cpu_msr(int cpu)
305{
306 // Always succeed for now
307 return 0;
308}
309
310void cleanup_cpu_msr(void)
311{
312 // Nothing, yet.
313}
314#else
315msr_t rdmsr(int addr)
316{
317 msr_t ret = { 0xffffffff, 0xffffffff };
318
319 return ret;
320}
321
322int wrmsr(int addr, msr_t msr)
323{
324 return -1;
325}
326
327int setup_cpu_msr(int cpu)
328{
329 printf("No MSR support for your OS yet.\n");
330 return -1;
331}
332
333void cleanup_cpu_msr(void)
334{
335 // Nothing, yet.
336}
337#endif
338#endif
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000339#endif
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000340