blob: 6e3d64612f787364be0ceb9aedb7c3edb485ca02 [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>
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +000027#include <errno.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000028#include "flash.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000029#include "hwaccess.h"
Stefan Reinauer0593f212009-01-26 01:10:48 +000030
Patrick Georgia9095a92010-09-30 17:03:32 +000031#if !defined(__DJGPP__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +000032/* No file access needed/possible to get mmap access permissions or access MSR. */
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000033#include <sys/stat.h>
34#include <fcntl.h>
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000035#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}
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000123#elif defined(__MACH__) && defined(__APPLE__)
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000124
Stefan Reinauer83704c52011-03-18 22:00:15 +0000125#define MEM_DEV "DirectHW"
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000126
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000127static void *sys_physmap(unsigned long phys_addr, size_t len)
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000128{
Patrick Georgied7a9642010-09-25 22:53:44 +0000129 /* The short form of ?: is a GNU extension.
130 * FIXME: map_physical returns NULL both for errors and for success
131 * if the region is mapped at virtual address zero. If in doubt, report
132 * an error until a better interface exists.
133 */
134 return map_physical(phys_addr, len) ? : ERROR_PTR;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000135}
136
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000137/* The OS X driver does not differentiate between mapping types. */
138#define sys_physmap_rw_uncached sys_physmap
139#define sys_physmap_ro_cached sys_physmap
140
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000141void physunmap(void *virt_addr, size_t len)
142{
143 unmap_physical(virt_addr, len);
144}
145
146#else
147#include <sys/mman.h>
148
Stefan Reinauer0593f212009-01-26 01:10:48 +0000149#if defined (__sun) && (defined(__i386) || defined(__amd64))
150# define MEM_DEV "/dev/xsvc"
151#else
152# define MEM_DEV "/dev/mem"
153#endif
154
155static int fd_mem = -1;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000156static int fd_mem_cached = -1;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000157
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000158/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000159static void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000160{
161 void *virt_addr;
162
163 if (-1 == fd_mem) {
164 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000165 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000166 msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno));
Peter Stuge43438ba2009-01-26 02:04:19 +0000167 exit(2);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000168 }
169 }
170
Peter Huewe73f8ec82011-01-24 19:15:51 +0000171 virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED,
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000172 fd_mem, (off_t)phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000173 return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000174}
175
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000176/* For reading DMI/coreboot/whatever tables. We should never write, and we
177 * do not care about caching.
178 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000179static void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000180{
181 void *virt_addr;
182
183 if (-1 == fd_mem_cached) {
184 /* Open the memory device CACHED. */
185 if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000186 msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno));
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000187 exit(2);
188 }
189 }
190
Peter Huewe73f8ec82011-01-24 19:15:51 +0000191 virt_addr = mmap(NULL, len, PROT_READ, MAP_SHARED,
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000192 fd_mem_cached, (off_t)phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000193 return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000194}
195
Stefan Reinauer0593f212009-01-26 01:10:48 +0000196void physunmap(void *virt_addr, size_t len)
197{
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000198 if (len == 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000199 msg_pspew("Not unmapping zero size at %p\n", virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000200 return;
201 }
202
Stefan Reinauer0593f212009-01-26 01:10:48 +0000203 munmap(virt_addr, len);
204}
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000205#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +0000206
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000207#define PHYSMAP_NOFAIL 0
208#define PHYSMAP_MAYFAIL 1
209#define PHYSMAP_RW 0
210#define PHYSMAP_RO 1
211
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000212static void *physmap_common(const char *descr, unsigned long phys_addr,
213 size_t len, int mayfail, int readonly)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000214{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000215 void *virt_addr;
216
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000217 if (len == 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000218 msg_pspew("Not mapping %s, zero size at 0x%08lx.\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000219 descr, phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000220 return ERROR_PTR;
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000221 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000222
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000223 if ((getpagesize() - 1) & len) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000224 msg_perr("Mapping %s at 0x%08lx, unaligned size 0x%lx.\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000225 descr, phys_addr, (unsigned long)len);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000226 }
227
228 if ((getpagesize() - 1) & phys_addr) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000229 msg_perr("Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000230 descr, (unsigned long)len, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000231 }
232
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000233 if (readonly)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000234 virt_addr = sys_physmap_ro_cached(phys_addr, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000235 else
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000236 virt_addr = sys_physmap_rw_uncached(phys_addr, len);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000237
Patrick Georgied7a9642010-09-25 22:53:44 +0000238 if (ERROR_PTR == virt_addr) {
Stefan Reinauer0593f212009-01-26 01:10:48 +0000239 if (NULL == descr)
240 descr = "memory";
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000241 msg_perr("Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr,
242 (unsigned long)len, phys_addr);
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000243 msg_perr(MEM_DEV " mmap failed: %s\n", strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000244#ifdef __linux__
Stefan Reinauer0593f212009-01-26 01:10:48 +0000245 if (EINVAL == errno) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000246 msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
247 msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
248 msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n");
249 msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
250 msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
Stefan Reinauer0593f212009-01-26 01:10:48 +0000251 }
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000252#elif defined (__OpenBSD__)
253 msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000254 "and reboot, or reboot into\n"
255 "single user mode.\n");
Sean Nelson316a29f2010-05-07 20:09:04 +0000256#endif
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000257 if (!mayfail)
258 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000259 }
260
261 return virt_addr;
262}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000263
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000264void *physmap(const char *descr, unsigned long phys_addr, size_t len)
265{
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000266 return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL,
267 PHYSMAP_RW);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000268}
269
270void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len)
271{
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000272 return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL,
273 PHYSMAP_RO);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000274}
275
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000276/* MSR abstraction implementations for Linux, OpenBSD, FreeBSD/Dragonfly, OSX, libpayload
277 * and a non-working default implemenation on the bottom. See also hwaccess.h for some (re)declarations. */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000278#if defined(__i386__) || defined(__x86_64__)
279
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000280#ifdef __linux__
281/*
282 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
283 * which are ring0 privileged instructions so only the kernel can do the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000284 * read/write. This function, therefore, requires that the msr kernel module
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000285 * be loaded to access these instructions from user space using device
286 * /dev/cpu/0/msr.
287 */
288
289static int fd_msr = -1;
290
291msr_t rdmsr(int addr)
292{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000293 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000294 msr_t msr = { 0xffffffff, 0xffffffff };
295
296 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000297 msg_perr("Could not lseek() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000298 close(fd_msr);
299 exit(1);
300 }
301
302 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000303 msr.lo = buf[0];
304 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000305 return msr;
306 }
307
308 if (errno != EIO) {
309 // A severe error.
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000310 msg_perr("Could not read() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000311 close(fd_msr);
312 exit(1);
313 }
314
315 return msr;
316}
317
318int wrmsr(int addr, msr_t msr)
319{
Anti Sullinbe24d812010-05-17 23:19:22 +0000320 uint32_t buf[2];
321 buf[0] = msr.lo;
322 buf[1] = msr.hi;
323
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000324 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000325 msg_perr("Could not lseek() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000326 close(fd_msr);
327 exit(1);
328 }
329
Anti Sullinbe24d812010-05-17 23:19:22 +0000330 if (write(fd_msr, buf, 8) != 8 && errno != EIO) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000331 msg_perr("Could not write() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000332 close(fd_msr);
333 exit(1);
334 }
335
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000336 /* Some MSRs must not be written. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000337 if (errno == EIO)
338 return -1;
339
340 return 0;
341}
342
343int setup_cpu_msr(int cpu)
344{
345 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000346 memset(msrfilename, 0, sizeof(msrfilename));
347 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu);
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000348
349 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000350 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000351 return -1;
352 }
353
354 fd_msr = open(msrfilename, O_RDWR);
355
356 if (fd_msr < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000357 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000358 msg_pinfo("Did you run 'modprobe msr'?\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000359 return -1;
360 }
361
362 return 0;
363}
364
365void cleanup_cpu_msr(void)
366{
367 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000368 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000369 return;
370 }
371
372 close(fd_msr);
373
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000374 /* Clear MSR file descriptor. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000375 fd_msr = -1;
376}
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000377#elif defined(__OpenBSD__) /* This does only work for certain AMD Geode LX systems see amdmsr(4). */
378#include <sys/ioctl.h>
379#include <machine/amdmsr.h>
380
381static int fd_msr = -1;
382
383msr_t rdmsr(int addr)
384{
385 struct amdmsr_req args;
386
387 msr_t msr = { 0xffffffff, 0xffffffff };
388
389 args.addr = (uint32_t)addr;
390
391 if (ioctl(fd_msr, RDMSR, &args) < 0) {
392 msg_perr("Error while executing RDMSR ioctl: %s\n", strerror(errno));
393 close(fd_msr);
394 exit(1);
395 }
396
397 msr.lo = args.val & 0xffffffff;
398 msr.hi = args.val >> 32;
399
400 return msr;
401}
402
403int wrmsr(int addr, msr_t msr)
404{
405 struct amdmsr_req args;
406
407 args.addr = addr;
408 args.val = (((uint64_t)msr.hi) << 32) | msr.lo;
409
410 if (ioctl(fd_msr, WRMSR, &args) < 0) {
411 msg_perr("Error while executing WRMSR ioctl: %s\n", strerror(errno));
412 close(fd_msr);
413 exit(1);
414 }
415
416 return 0;
417}
418
419int setup_cpu_msr(int cpu)
420{
421 char msrfilename[64];
422 memset(msrfilename, 0, sizeof(msrfilename));
423 snprintf(msrfilename, sizeof(msrfilename), "/dev/amdmsr");
424
425 if (fd_msr != -1) {
426 msg_pinfo("MSR was already initialized\n");
427 return -1;
428 }
429
430 fd_msr = open(msrfilename, O_RDWR);
431
432 if (fd_msr < 0) {
433 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
434 return -1;
435 }
436
437 return 0;
438}
439
440void cleanup_cpu_msr(void)
441{
442 if (fd_msr == -1) {
443 msg_pinfo("No MSR initialized.\n");
444 return;
445 }
446
447 close(fd_msr);
448
449 /* Clear MSR file descriptor. */
450 fd_msr = -1;
451}
452
453#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000454#include <sys/ioctl.h>
455
456typedef struct {
457 int msr;
458 uint64_t data;
459} cpu_msr_args_t;
460#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
461#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
462
463static int fd_msr = -1;
464
465msr_t rdmsr(int addr)
466{
467 cpu_msr_args_t args;
468
469 msr_t msr = { 0xffffffff, 0xffffffff };
470
471 args.msr = addr;
472
473 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000474 msg_perr("Error while executing CPU_RDMSR ioctl: %s\n", strerror(errno));
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000475 close(fd_msr);
476 exit(1);
477 }
478
479 msr.lo = args.data & 0xffffffff;
480 msr.hi = args.data >> 32;
481
482 return msr;
483}
484
485int wrmsr(int addr, msr_t msr)
486{
487 cpu_msr_args_t args;
488
489 args.msr = addr;
490 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
491
492 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000493 msg_perr("Error while executing CPU_WRMSR ioctl: %s\n", strerror(errno));
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000494 close(fd_msr);
495 exit(1);
496 }
497
498 return 0;
499}
500
501int setup_cpu_msr(int cpu)
502{
503 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000504 memset(msrfilename, 0, sizeof(msrfilename));
505 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu);
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000506
507 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000508 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000509 return -1;
510 }
511
512 fd_msr = open(msrfilename, O_RDWR);
513
514 if (fd_msr < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000515 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000516 msg_pinfo("Did you install ports/sysutils/devcpu?\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000517 return -1;
518 }
519
520 return 0;
521}
522
523void cleanup_cpu_msr(void)
524{
525 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000526 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000527 return;
528 }
529
530 close(fd_msr);
531
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000532 /* Clear MSR file descriptor. */
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000533 fd_msr = -1;
534}
535
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000536#elif defined(__MACH__) && defined(__APPLE__)
537/* rdmsr() and wrmsr() are provided by DirectHW which needs neither setup nor cleanup. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000538int setup_cpu_msr(int cpu)
539{
540 // Always succeed for now
541 return 0;
542}
543
544void cleanup_cpu_msr(void)
545{
546 // Nothing, yet.
547}
Patrick Georgia9095a92010-09-30 17:03:32 +0000548#elif defined(__LIBPAYLOAD__)
549msr_t libpayload_rdmsr(int addr)
550{
551 msr_t msr;
552 unsigned long long val = _rdmsr(addr);
553 msr.lo = val & 0xffffffff;
554 msr.hi = val >> 32;
555 return msr;
556}
557
558int libpayload_wrmsr(int addr, msr_t msr)
559{
560 _wrmsr(addr, msr.lo | ((unsigned long long)msr.hi << 32));
Patrick Georgi97bc95c2011-03-08 07:17:44 +0000561 return 0;
Patrick Georgia9095a92010-09-30 17:03:32 +0000562}
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +0000563
564int setup_cpu_msr(int cpu)
565{
566 return 0;
567}
568
569void cleanup_cpu_msr(void)
570{
571}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000572#else
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000573/* default MSR implementation */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000574msr_t rdmsr(int addr)
575{
576 msr_t ret = { 0xffffffff, 0xffffffff };
577
578 return ret;
579}
580
581int wrmsr(int addr, msr_t msr)
582{
583 return -1;
584}
585
586int setup_cpu_msr(int cpu)
587{
Sean Nelson316a29f2010-05-07 20:09:04 +0000588 msg_pinfo("No MSR support for your OS yet.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000589 return -1;
590}
591
592void cleanup_cpu_msr(void)
593{
594 // Nothing, yet.
595}
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000596#endif // OS switches for MSR code
597#else // x86
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000598/* Does MSR exist on non-x86 architectures? */
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000599#endif // arch switches for MSR code