blob: 5aa98748acdf4eea46fcdcbb196650f1018416b2 [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 <stdlib.h>
Stefan Reinauer8fa64812009-08-12 09:27:45 +000026#include <string.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000027#include "flash.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000028#include "hwaccess.h"
Stefan Reinauer0593f212009-01-26 01:10:48 +000029
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000030/* Do we need any file access or ioctl for physmap or MSR? */
Patrick Georgia9095a92010-09-30 17:03:32 +000031#if !defined(__DJGPP__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000032#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{
Uwe Hermann91f4afa2011-07-28 08:13:25 +000047 if (realmem_map)
Rudolf Marek837d8102010-04-25 22:47:50 +000048 return realmem_map + phys_addr;
Rudolf Marek837d8102010-04-25 22:47:50 +000049
50 realmem_map = valloc(1024 * 1024);
51
Uwe Hermann91f4afa2011-07-28 08:13:25 +000052 if (!realmem_map)
Patrick Georgied7a9642010-09-25 22:53:44 +000053 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000054
55 if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) {
Carl-Daniel Hailfinger602de982010-10-05 23:21:51 +000056 free(realmem_map);
57 realmem_map = NULL;
Patrick Georgied7a9642010-09-25 22:53:44 +000058 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000059 }
60
61 return realmem_map + phys_addr;
62}
Rudolf Marek03ae5c12010-03-16 23:59:19 +000063
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000064static void *sys_physmap(unsigned long phys_addr, size_t len)
Rudolf Marek03ae5c12010-03-16 23:59:19 +000065{
66 int ret;
67 __dpmi_meminfo mi;
68
Uwe Hermann91f4afa2011-07-28 08:13:25 +000069 /* Enable 4GB limit on DS descriptor. */
70 if (!__djgpp_nearptr_enable())
Patrick Georgied7a9642010-09-25 22:53:44 +000071 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000072
73 if ((phys_addr + len - 1) < (1024 * 1024)) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +000074 /* We need to use another method to map first 1MB. */
Rudolf Marek837d8102010-04-25 22:47:50 +000075 return map_first_meg(phys_addr, len);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000076 }
77
78 mi.address = phys_addr;
79 mi.size = len;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +000080 ret = __dpmi_physical_address_mapping(&mi);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000081
Uwe Hermann91f4afa2011-07-28 08:13:25 +000082 if (ret != 0)
Patrick Georgied7a9642010-09-25 22:53:44 +000083 return ERROR_PTR;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000084
Rudolf Marek837d8102010-04-25 22:47:50 +000085 return (void *) mi.address + __djgpp_conventional_base;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000086}
87
88#define sys_physmap_rw_uncached sys_physmap
Rudolf Marek837d8102010-04-25 22:47:50 +000089#define sys_physmap_ro_cached sys_physmap
Rudolf Marek03ae5c12010-03-16 23:59:19 +000090
91void physunmap(void *virt_addr, size_t len)
92{
93 __dpmi_meminfo mi;
94
Carl-Daniel Hailfinger602de982010-10-05 23:21:51 +000095 /* There is no known way to unmap the first 1 MB. The DPMI server will
96 * do this for us on exit.
97 */
Uwe Hermann91f4afa2011-07-28 08:13:25 +000098 if ((virt_addr >= realmem_map) &&
99 ((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
Patrick Georgia9095a92010-09-30 17:03:32 +0000107#elif defined(__LIBPAYLOAD__)
108#include <arch/virtual.h>
109
110#define MEM_DEV ""
111
112void *sys_physmap(unsigned long phys_addr, size_t len)
113{
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000114 return (void *)phys_to_virt(phys_addr);
Patrick Georgia9095a92010-09-30 17:03:32 +0000115}
116
117#define sys_physmap_rw_uncached sys_physmap
118#define sys_physmap_ro_cached sys_physmap
119
120void physunmap(void *virt_addr, size_t len)
121{
122}
123
124int setup_cpu_msr(int cpu)
125{
126 return 0;
127}
128
129void cleanup_cpu_msr(void)
130{
131}
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000132#elif defined(__MACH__) && defined(__APPLE__)
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000133
Stefan Reinauer83704c52011-03-18 22:00:15 +0000134#define MEM_DEV "DirectHW"
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000135
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000136static void *sys_physmap(unsigned long phys_addr, size_t len)
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000137{
Patrick Georgied7a9642010-09-25 22:53:44 +0000138 /* The short form of ?: is a GNU extension.
139 * FIXME: map_physical returns NULL both for errors and for success
140 * if the region is mapped at virtual address zero. If in doubt, report
141 * an error until a better interface exists.
142 */
143 return map_physical(phys_addr, len) ? : ERROR_PTR;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000144}
145
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000146/* The OS X driver does not differentiate between mapping types. */
147#define sys_physmap_rw_uncached sys_physmap
148#define sys_physmap_ro_cached sys_physmap
149
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000150void physunmap(void *virt_addr, size_t len)
151{
152 unmap_physical(virt_addr, len);
153}
154
155#else
156#include <sys/mman.h>
157
Stefan Reinauer0593f212009-01-26 01:10:48 +0000158#if defined (__sun) && (defined(__i386) || defined(__amd64))
159# define MEM_DEV "/dev/xsvc"
160#else
161# define MEM_DEV "/dev/mem"
162#endif
163
164static int fd_mem = -1;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000165static int fd_mem_cached = -1;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000166
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000167/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000168static void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000169{
170 void *virt_addr;
171
172 if (-1 == fd_mem) {
173 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000174 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Reinauer0593f212009-01-26 01:10:48 +0000175 perror("Critical error: open(" MEM_DEV ")");
Peter Stuge43438ba2009-01-26 02:04:19 +0000176 exit(2);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000177 }
178 }
179
Peter Huewe73f8ec82011-01-24 19:15:51 +0000180 virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED,
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000181 fd_mem, (off_t)phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000182 return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000183}
184
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000185/* For reading DMI/coreboot/whatever tables. We should never write, and we
186 * do not care about caching.
187 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000188static void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000189{
190 void *virt_addr;
191
192 if (-1 == fd_mem_cached) {
193 /* Open the memory device CACHED. */
194 if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000195 msg_perr("Critical error: open(" MEM_DEV "): %s",
196 strerror(errno));
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000197 exit(2);
198 }
199 }
200
Peter Huewe73f8ec82011-01-24 19:15:51 +0000201 virt_addr = mmap(NULL, len, PROT_READ, MAP_SHARED,
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000202 fd_mem_cached, (off_t)phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000203 return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000204}
205
Stefan Reinauer0593f212009-01-26 01:10:48 +0000206void physunmap(void *virt_addr, size_t len)
207{
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000208 if (len == 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000209 msg_pspew("Not unmapping zero size at %p\n", virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000210 return;
211 }
212
Stefan Reinauer0593f212009-01-26 01:10:48 +0000213 munmap(virt_addr, len);
214}
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000215#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +0000216
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000217#define PHYSMAP_NOFAIL 0
218#define PHYSMAP_MAYFAIL 1
219#define PHYSMAP_RW 0
220#define PHYSMAP_RO 1
221
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000222static void *physmap_common(const char *descr, unsigned long phys_addr,
223 size_t len, int mayfail, int readonly)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000224{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000225 void *virt_addr;
226
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000227 if (len == 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000228 msg_pspew("Not mapping %s, zero size at 0x%08lx.\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000229 descr, phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000230 return ERROR_PTR;
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000231 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000232
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000233 if ((getpagesize() - 1) & len) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000234 msg_perr("Mapping %s at 0x%08lx, unaligned size 0x%lx.\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000235 descr, phys_addr, (unsigned long)len);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000236 }
237
238 if ((getpagesize() - 1) & phys_addr) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000239 msg_perr("Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000240 descr, (unsigned long)len, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000241 }
242
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000243 if (readonly)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000244 virt_addr = sys_physmap_ro_cached(phys_addr, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000245 else
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000246 virt_addr = sys_physmap_rw_uncached(phys_addr, len);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000247
Patrick Georgied7a9642010-09-25 22:53:44 +0000248 if (ERROR_PTR == virt_addr) {
Stefan Reinauer0593f212009-01-26 01:10:48 +0000249 if (NULL == descr)
250 descr = "memory";
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000251 msg_perr("Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr,
252 (unsigned long)len, phys_addr);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000253 perror(MEM_DEV " mmap failed");
Sean Nelson316a29f2010-05-07 20:09:04 +0000254#ifdef __linux__
Stefan Reinauer0593f212009-01-26 01:10:48 +0000255 if (EINVAL == errno) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000256 msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
257 msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
258 msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n");
259 msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
260 msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
Stefan Reinauer0593f212009-01-26 01:10:48 +0000261 }
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000262#elif defined (__OpenBSD__)
263 msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000264 "and reboot, or reboot into\n"
265 "single user mode.\n");
Sean Nelson316a29f2010-05-07 20:09:04 +0000266#endif
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000267 if (!mayfail)
268 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000269 }
270
271 return virt_addr;
272}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000273
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000274void *physmap(const char *descr, unsigned long phys_addr, size_t len)
275{
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000276 return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL,
277 PHYSMAP_RW);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000278}
279
280void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len)
281{
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000282 return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL,
283 PHYSMAP_RO);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000284}
285
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000286#if defined(__i386__) || defined(__x86_64__)
287
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000288#ifdef __linux__
289/*
290 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
291 * which are ring0 privileged instructions so only the kernel can do the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000292 * read/write. This function, therefore, requires that the msr kernel module
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000293 * be loaded to access these instructions from user space using device
294 * /dev/cpu/0/msr.
295 */
296
297static int fd_msr = -1;
298
299msr_t rdmsr(int addr)
300{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000301 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000302 msr_t msr = { 0xffffffff, 0xffffffff };
303
304 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
305 perror("Could not lseek() to MSR");
306 close(fd_msr);
307 exit(1);
308 }
309
310 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000311 msr.lo = buf[0];
312 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000313 return msr;
314 }
315
316 if (errno != EIO) {
317 // A severe error.
318 perror("Could not read() MSR");
319 close(fd_msr);
320 exit(1);
321 }
322
323 return msr;
324}
325
326int wrmsr(int addr, msr_t msr)
327{
Anti Sullinbe24d812010-05-17 23:19:22 +0000328 uint32_t buf[2];
329 buf[0] = msr.lo;
330 buf[1] = msr.hi;
331
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000332 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
333 perror("Could not lseek() to MSR");
334 close(fd_msr);
335 exit(1);
336 }
337
Anti Sullinbe24d812010-05-17 23:19:22 +0000338 if (write(fd_msr, buf, 8) != 8 && errno != EIO) {
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000339 perror("Could not write() MSR");
340 close(fd_msr);
341 exit(1);
342 }
343
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000344 /* Some MSRs must not be written. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000345 if (errno == EIO)
346 return -1;
347
348 return 0;
349}
350
351int setup_cpu_msr(int cpu)
352{
353 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000354 memset(msrfilename, 0, sizeof(msrfilename));
355 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu);
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000356
357 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000358 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000359 return -1;
360 }
361
362 fd_msr = open(msrfilename, O_RDWR);
363
364 if (fd_msr < 0) {
365 perror("Error while opening /dev/cpu/0/msr");
Sean Nelson316a29f2010-05-07 20:09:04 +0000366 msg_pinfo("Did you run 'modprobe msr'?\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000367 return -1;
368 }
369
370 return 0;
371}
372
373void cleanup_cpu_msr(void)
374{
375 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000376 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000377 return;
378 }
379
380 close(fd_msr);
381
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000382 /* Clear MSR file descriptor. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000383 fd_msr = -1;
384}
385#else
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +0000386#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000387#include <sys/ioctl.h>
388
389typedef struct {
390 int msr;
391 uint64_t data;
392} cpu_msr_args_t;
393#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
394#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
395
396static int fd_msr = -1;
397
398msr_t rdmsr(int addr)
399{
400 cpu_msr_args_t args;
401
402 msr_t msr = { 0xffffffff, 0xffffffff };
403
404 args.msr = addr;
405
406 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
407 perror("CPU_RDMSR");
408 close(fd_msr);
409 exit(1);
410 }
411
412 msr.lo = args.data & 0xffffffff;
413 msr.hi = args.data >> 32;
414
415 return msr;
416}
417
418int wrmsr(int addr, msr_t msr)
419{
420 cpu_msr_args_t args;
421
422 args.msr = addr;
423 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
424
425 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
426 perror("CPU_WRMSR");
427 close(fd_msr);
428 exit(1);
429 }
430
431 return 0;
432}
433
434int setup_cpu_msr(int cpu)
435{
436 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000437 memset(msrfilename, 0, sizeof(msrfilename));
438 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu);
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000439
440 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000441 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000442 return -1;
443 }
444
445 fd_msr = open(msrfilename, O_RDWR);
446
447 if (fd_msr < 0) {
448 perror("Error while opening /dev/cpu0");
Sean Nelson316a29f2010-05-07 20:09:04 +0000449 msg_pinfo("Did you install ports/sysutils/devcpu?\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000450 return -1;
451 }
452
453 return 0;
454}
455
456void cleanup_cpu_msr(void)
457{
458 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000459 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000460 return;
461 }
462
463 close(fd_msr);
464
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000465 /* Clear MSR file descriptor. */
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000466 fd_msr = -1;
467}
468
469#else
470
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000471#if defined(__MACH__) && defined(__APPLE__)
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000472int setup_cpu_msr(int cpu)
473{
474 // Always succeed for now
475 return 0;
476}
477
478void cleanup_cpu_msr(void)
479{
480 // Nothing, yet.
481}
Patrick Georgia9095a92010-09-30 17:03:32 +0000482#elif defined(__LIBPAYLOAD__)
483msr_t libpayload_rdmsr(int addr)
484{
485 msr_t msr;
486 unsigned long long val = _rdmsr(addr);
487 msr.lo = val & 0xffffffff;
488 msr.hi = val >> 32;
489 return msr;
490}
491
492int libpayload_wrmsr(int addr, msr_t msr)
493{
494 _wrmsr(addr, msr.lo | ((unsigned long long)msr.hi << 32));
Patrick Georgi97bc95c2011-03-08 07:17:44 +0000495 return 0;
Patrick Georgia9095a92010-09-30 17:03:32 +0000496}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000497#else
498msr_t rdmsr(int addr)
499{
500 msr_t ret = { 0xffffffff, 0xffffffff };
501
502 return ret;
503}
504
505int wrmsr(int addr, msr_t msr)
506{
507 return -1;
508}
509
510int setup_cpu_msr(int cpu)
511{
Sean Nelson316a29f2010-05-07 20:09:04 +0000512 msg_pinfo("No MSR support for your OS yet.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000513 return -1;
514}
515
516void cleanup_cpu_msr(void)
517{
518 // Nothing, yet.
519}
520#endif
521#endif
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000522#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000523#else
524/* Does MSR exist on non-x86 architectures? */
525#endif