blob: e03a23d8890c73d55ee1bf5c79821540a983d019 [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{
136 uint8_t buf[8];
137 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) {
146 msr.lo = *(uint32_t *)buf;
147 msr.hi = *(uint32_t *)(buf + 4);
148
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
218#ifdef __DARWIN__
219int setup_cpu_msr(int cpu)
220{
221 // Always succeed for now
222 return 0;
223}
224
225void cleanup_cpu_msr(void)
226{
227 // Nothing, yet.
228}
229#else
230msr_t rdmsr(int addr)
231{
232 msr_t ret = { 0xffffffff, 0xffffffff };
233
234 return ret;
235}
236
237int wrmsr(int addr, msr_t msr)
238{
239 return -1;
240}
241
242int setup_cpu_msr(int cpu)
243{
244 printf("No MSR support for your OS yet.\n");
245 return -1;
246}
247
248void cleanup_cpu_msr(void)
249{
250 // Nothing, yet.
251}
252#endif
253#endif
254