blob: caad1ca5a4bb5d67640bc1d74acc31fb0400ba99 [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>
Stefan Tauner7fb5aa02013-08-14 15:48:44 +000024#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000025#include <stdio.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000026#include <stdlib.h>
Stefan Reinauer8fa64812009-08-12 09:27:45 +000027#include <string.h>
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +000028#include <errno.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000029#include "flash.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000030#include "hwaccess.h"
Stefan Reinauer0593f212009-01-26 01:10:48 +000031
Patrick Georgia9095a92010-09-30 17:03:32 +000032#if !defined(__DJGPP__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +000033/* No file access needed/possible to get mmap access permissions or access MSR. */
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000034#include <sys/stat.h>
35#include <fcntl.h>
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000036#endif
37
Rudolf Marek03ae5c12010-03-16 23:59:19 +000038#ifdef __DJGPP__
39#include <dpmi.h>
Rudolf Marek837d8102010-04-25 22:47:50 +000040#include <sys/nearptr.h>
Rudolf Marek03ae5c12010-03-16 23:59:19 +000041
42#define MEM_DEV "dpmi"
43
Rudolf Marek837d8102010-04-25 22:47:50 +000044static void *realmem_map;
45
Stefan Tauner305e0b92013-07-17 23:46:44 +000046static void *map_first_meg(uintptr_t phys_addr, size_t len)
Rudolf Marek837d8102010-04-25 22:47:50 +000047{
Uwe Hermann91f4afa2011-07-28 08:13:25 +000048 if (realmem_map)
Rudolf Marek837d8102010-04-25 22:47:50 +000049 return realmem_map + phys_addr;
Rudolf Marek837d8102010-04-25 22:47:50 +000050
51 realmem_map = valloc(1024 * 1024);
52
Uwe Hermann91f4afa2011-07-28 08:13:25 +000053 if (!realmem_map)
Patrick Georgied7a9642010-09-25 22:53:44 +000054 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000055
56 if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) {
Carl-Daniel Hailfinger602de982010-10-05 23:21:51 +000057 free(realmem_map);
58 realmem_map = NULL;
Patrick Georgied7a9642010-09-25 22:53:44 +000059 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000060 }
61
62 return realmem_map + phys_addr;
63}
Rudolf Marek03ae5c12010-03-16 23:59:19 +000064
Stefan Tauner305e0b92013-07-17 23:46:44 +000065static void *sys_physmap(uintptr_t phys_addr, size_t len)
Rudolf Marek03ae5c12010-03-16 23:59:19 +000066{
67 int ret;
68 __dpmi_meminfo mi;
69
Uwe Hermann91f4afa2011-07-28 08:13:25 +000070 /* Enable 4GB limit on DS descriptor. */
71 if (!__djgpp_nearptr_enable())
Patrick Georgied7a9642010-09-25 22:53:44 +000072 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000073
74 if ((phys_addr + len - 1) < (1024 * 1024)) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +000075 /* We need to use another method to map first 1MB. */
Rudolf Marek837d8102010-04-25 22:47:50 +000076 return map_first_meg(phys_addr, len);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000077 }
78
79 mi.address = phys_addr;
80 mi.size = len;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +000081 ret = __dpmi_physical_address_mapping(&mi);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000082
Uwe Hermann91f4afa2011-07-28 08:13:25 +000083 if (ret != 0)
Patrick Georgied7a9642010-09-25 22:53:44 +000084 return ERROR_PTR;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000085
Rudolf Marek837d8102010-04-25 22:47:50 +000086 return (void *) mi.address + __djgpp_conventional_base;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000087}
88
89#define sys_physmap_rw_uncached sys_physmap
Rudolf Marek837d8102010-04-25 22:47:50 +000090#define sys_physmap_ro_cached sys_physmap
Rudolf Marek03ae5c12010-03-16 23:59:19 +000091
92void physunmap(void *virt_addr, size_t len)
93{
94 __dpmi_meminfo mi;
95
Carl-Daniel Hailfinger602de982010-10-05 23:21:51 +000096 /* There is no known way to unmap the first 1 MB. The DPMI server will
97 * do this for us on exit.
98 */
Uwe Hermann91f4afa2011-07-28 08:13:25 +000099 if ((virt_addr >= realmem_map) &&
100 ((virt_addr + len) <= (realmem_map + (1024 * 1024)))) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000101 return;
102 }
103
104 mi.address = (unsigned long) virt_addr;
105 __dpmi_free_physical_address_mapping(&mi);
106}
107
Patrick Georgia9095a92010-09-30 17:03:32 +0000108#elif defined(__LIBPAYLOAD__)
109#include <arch/virtual.h>
110
111#define MEM_DEV ""
112
Stefan Tauner305e0b92013-07-17 23:46:44 +0000113void *sys_physmap(uintptr_t phys_addr, size_t len)
Patrick Georgia9095a92010-09-30 17:03:32 +0000114{
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000115 return (void *)phys_to_virt(phys_addr);
Patrick Georgia9095a92010-09-30 17:03:32 +0000116}
117
118#define sys_physmap_rw_uncached sys_physmap
119#define sys_physmap_ro_cached sys_physmap
120
121void physunmap(void *virt_addr, size_t len)
122{
123}
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000124#elif defined(__MACH__) && defined(__APPLE__)
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000125
Stefan Reinauer83704c52011-03-18 22:00:15 +0000126#define MEM_DEV "DirectHW"
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000127
Stefan Tauner305e0b92013-07-17 23:46:44 +0000128static void *sys_physmap(uintptr_t phys_addr, size_t len)
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000129{
Patrick Georgied7a9642010-09-25 22:53:44 +0000130 /* The short form of ?: is a GNU extension.
131 * FIXME: map_physical returns NULL both for errors and for success
132 * if the region is mapped at virtual address zero. If in doubt, report
133 * an error until a better interface exists.
134 */
135 return map_physical(phys_addr, len) ? : ERROR_PTR;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000136}
137
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000138/* The OS X driver does not differentiate between mapping types. */
139#define sys_physmap_rw_uncached sys_physmap
140#define sys_physmap_ro_cached sys_physmap
141
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000142void physunmap(void *virt_addr, size_t len)
143{
144 unmap_physical(virt_addr, len);
145}
146
147#else
148#include <sys/mman.h>
149
Stefan Reinauer0593f212009-01-26 01:10:48 +0000150#if defined (__sun) && (defined(__i386) || defined(__amd64))
151# define MEM_DEV "/dev/xsvc"
152#else
153# define MEM_DEV "/dev/mem"
154#endif
155
156static int fd_mem = -1;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000157static int fd_mem_cached = -1;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000158
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000159/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
Stefan Tauner305e0b92013-07-17 23:46:44 +0000160static void *sys_physmap_rw_uncached(uintptr_t phys_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000161{
162 void *virt_addr;
163
164 if (-1 == fd_mem) {
165 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000166 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000167 msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno));
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000168 return ERROR_PTR;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000169 }
170 }
171
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000172 virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, 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 */
Stefan Tauner305e0b92013-07-17 23:46:44 +0000179static void *sys_physmap_ro_cached(uintptr_t 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));
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000187 return ERROR_PTR;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000188 }
189 }
190
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000191 virt_addr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd_mem_cached, (off_t)phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000192 return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000193}
194
Stefan Reinauer0593f212009-01-26 01:10:48 +0000195void physunmap(void *virt_addr, size_t len)
196{
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000197 if (len == 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000198 msg_pspew("Not unmapping zero size at %p\n", virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000199 return;
200 }
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000201
Stefan Reinauer0593f212009-01-26 01:10:48 +0000202 munmap(virt_addr, len);
203}
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000204#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +0000205
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000206#define PHYSM_NOFAIL 0
207#define PHYSM_MAYFAIL 1
208#define PHYSM_RW 0
209#define PHYSM_RO 1
210#define PHYSM_NOCLEANUP 0
211#define PHYSM_CLEANUP 1
212#define PHYSM_EXACT 0
213#define PHYSM_ROUND 1
214
215/* Round start to nearest page boundary below and set len so that the resulting address range ends at the lowest
216 * possible page boundary where the original address range is still entirely contained. It returns the
217 * difference between the rounded start address and the original start address. */
218static uintptr_t round_to_page_boundaries(uintptr_t *start, size_t *len)
219{
220 uintptr_t page_size = getpagesize();
221 uintptr_t page_mask = ~(page_size-1);
222 uintptr_t end = *start + *len;
223 uintptr_t old_start = *start;
224 msg_gspew("page_size=%" PRIxPTR "\n", page_size);
225 msg_gspew("pre-rounding: start=0x%0*" PRIxPTR ", len=0x%zx, end=0x%0*" PRIxPTR "\n",
226 PRIxPTR_WIDTH, *start, *len, PRIxPTR_WIDTH, end);
227 *start = *start & page_mask;
228 end = (end + page_size - 1) & page_mask;
229 *len = end - *start;
230 msg_gspew("post-rounding: start=0x%0*" PRIxPTR ", len=0x%zx, end=0x%0*" PRIxPTR "\n",
231 PRIxPTR_WIDTH, *start, *len, PRIxPTR_WIDTH, *start + *len);
232 return old_start - *start;
233}
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000234
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000235struct undo_physmap_data {
236 void *virt_addr;
237 size_t len;
238};
239
240static int undo_physmap(void *data)
241{
242 if (data == NULL) {
243 msg_perr("%s: tried to physunmap without valid data!\n", __func__);
244 return 1;
245 }
246 struct undo_physmap_data *d = data;
247 physunmap(d->virt_addr, d->len);
248 free(data);
249 return 0;
250}
251
252static void *physmap_common(const char *descr, uintptr_t phys_addr, size_t len, bool mayfail,
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000253 bool readonly, bool autocleanup, bool round)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000254{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000255 void *virt_addr;
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000256 uintptr_t offset = 0;
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000257
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000258 if (len == 0) {
Stefan Tauner305e0b92013-07-17 23:46:44 +0000259 msg_pspew("Not mapping %s, zero size at 0x%0*" PRIxPTR ".\n", descr, PRIxPTR_WIDTH, phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000260 return ERROR_PTR;
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000261 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000262
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000263 if (round)
264 offset = round_to_page_boundaries(&phys_addr, &len);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000265
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000266 if (readonly)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000267 virt_addr = sys_physmap_ro_cached(phys_addr, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000268 else
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000269 virt_addr = sys_physmap_rw_uncached(phys_addr, len);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000270
Patrick Georgied7a9642010-09-25 22:53:44 +0000271 if (ERROR_PTR == virt_addr) {
Stefan Reinauer0593f212009-01-26 01:10:48 +0000272 if (NULL == descr)
273 descr = "memory";
Stefan Tauner0554ca52013-07-25 22:54:25 +0000274 msg_perr("Error accessing %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
275 descr, len, PRIxPTR_WIDTH, phys_addr);
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000276 msg_perr(MEM_DEV " mmap failed: %s\n", strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000277#ifdef __linux__
Stefan Reinauer0593f212009-01-26 01:10:48 +0000278 if (EINVAL == errno) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000279 msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
280 msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
281 msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n");
282 msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
283 msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
Stefan Reinauer0593f212009-01-26 01:10:48 +0000284 }
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000285#elif defined (__OpenBSD__)
286 msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000287 "and reboot, or reboot into\n"
288 "single user mode.\n");
Sean Nelson316a29f2010-05-07 20:09:04 +0000289#endif
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000290 if (mayfail)
291 return ERROR_PTR;
292 else
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000293 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000294 }
295
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000296 if (autocleanup) {
297 struct undo_physmap_data *d = malloc(sizeof(struct undo_physmap_data));
298 if (d == NULL) {
299 msg_perr("%s: Out of memory!\n", __func__);
300 goto unmap_out;
301 }
302
303 d->virt_addr = virt_addr;
304 d->len = len;
305 if (register_shutdown(undo_physmap, d) != 0) {
306 msg_perr("%s: Could not register shutdown function!\n", __func__);
307 goto unmap_out;
308 }
309 }
310
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000311 return virt_addr + offset;
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000312unmap_out:
313 physunmap(virt_addr, len);
314 if (!mayfail)
315 exit(3);
316 return ERROR_PTR;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000317}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000318
Stefan Tauner305e0b92013-07-17 23:46:44 +0000319void *physmap(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000320{
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000321 return physmap_common(descr, phys_addr, len, PHYSM_NOFAIL, PHYSM_RW, PHYSM_NOCLEANUP, PHYSM_EXACT);
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000322}
323
324void *rphysmap(const char *descr, uintptr_t phys_addr, size_t len)
325{
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000326 return physmap_common(descr, phys_addr, len, PHYSM_NOFAIL, PHYSM_RW, PHYSM_CLEANUP, PHYSM_ROUND);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000327}
328
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000329void *physmap_round(const char *descr, uintptr_t phys_addr, size_t len)
330{
331 return physmap_common(descr, phys_addr, len, PHYSM_NOFAIL, PHYSM_RW, PHYSM_NOCLEANUP, PHYSM_ROUND);
332}
333
Stefan Tauner305e0b92013-07-17 23:46:44 +0000334void *physmap_try_ro(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000335{
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000336 return physmap_common(descr, phys_addr, len, PHYSM_MAYFAIL, PHYSM_RO, PHYSM_NOCLEANUP, PHYSM_EXACT);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000337}
338
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000339/* MSR abstraction implementations for Linux, OpenBSD, FreeBSD/Dragonfly, OSX, libpayload
340 * 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 +0000341#if defined(__i386__) || defined(__x86_64__)
342
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000343#ifdef __linux__
344/*
345 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
346 * which are ring0 privileged instructions so only the kernel can do the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000347 * read/write. This function, therefore, requires that the msr kernel module
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000348 * be loaded to access these instructions from user space using device
349 * /dev/cpu/0/msr.
350 */
351
352static int fd_msr = -1;
353
354msr_t rdmsr(int addr)
355{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000356 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000357 msr_t msr = { 0xffffffff, 0xffffffff };
358
359 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000360 msg_perr("Could not lseek() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000361 close(fd_msr);
362 exit(1);
363 }
364
365 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000366 msr.lo = buf[0];
367 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000368 return msr;
369 }
370
371 if (errno != EIO) {
372 // A severe error.
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000373 msg_perr("Could not read() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000374 close(fd_msr);
375 exit(1);
376 }
377
378 return msr;
379}
380
381int wrmsr(int addr, msr_t msr)
382{
Anti Sullinbe24d812010-05-17 23:19:22 +0000383 uint32_t buf[2];
384 buf[0] = msr.lo;
385 buf[1] = msr.hi;
386
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000387 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000388 msg_perr("Could not lseek() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000389 close(fd_msr);
390 exit(1);
391 }
392
Anti Sullinbe24d812010-05-17 23:19:22 +0000393 if (write(fd_msr, buf, 8) != 8 && errno != EIO) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000394 msg_perr("Could not write() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000395 close(fd_msr);
396 exit(1);
397 }
398
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000399 /* Some MSRs must not be written. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000400 if (errno == EIO)
401 return -1;
402
403 return 0;
404}
405
406int setup_cpu_msr(int cpu)
407{
408 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000409 memset(msrfilename, 0, sizeof(msrfilename));
410 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu);
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000411
412 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000413 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000414 return -1;
415 }
416
417 fd_msr = open(msrfilename, O_RDWR);
418
419 if (fd_msr < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000420 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000421 msg_pinfo("Did you run 'modprobe msr'?\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000422 return -1;
423 }
424
425 return 0;
426}
427
428void cleanup_cpu_msr(void)
429{
430 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000431 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000432 return;
433 }
434
435 close(fd_msr);
436
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000437 /* Clear MSR file descriptor. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000438 fd_msr = -1;
439}
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000440#elif defined(__OpenBSD__) /* This does only work for certain AMD Geode LX systems see amdmsr(4). */
441#include <sys/ioctl.h>
442#include <machine/amdmsr.h>
443
444static int fd_msr = -1;
445
446msr_t rdmsr(int addr)
447{
448 struct amdmsr_req args;
449
450 msr_t msr = { 0xffffffff, 0xffffffff };
451
452 args.addr = (uint32_t)addr;
453
454 if (ioctl(fd_msr, RDMSR, &args) < 0) {
455 msg_perr("Error while executing RDMSR ioctl: %s\n", strerror(errno));
456 close(fd_msr);
457 exit(1);
458 }
459
460 msr.lo = args.val & 0xffffffff;
461 msr.hi = args.val >> 32;
462
463 return msr;
464}
465
466int wrmsr(int addr, msr_t msr)
467{
468 struct amdmsr_req args;
469
470 args.addr = addr;
471 args.val = (((uint64_t)msr.hi) << 32) | msr.lo;
472
473 if (ioctl(fd_msr, WRMSR, &args) < 0) {
474 msg_perr("Error while executing WRMSR ioctl: %s\n", strerror(errno));
475 close(fd_msr);
476 exit(1);
477 }
478
479 return 0;
480}
481
482int setup_cpu_msr(int cpu)
483{
484 char msrfilename[64];
485 memset(msrfilename, 0, sizeof(msrfilename));
486 snprintf(msrfilename, sizeof(msrfilename), "/dev/amdmsr");
487
488 if (fd_msr != -1) {
489 msg_pinfo("MSR was already initialized\n");
490 return -1;
491 }
492
493 fd_msr = open(msrfilename, O_RDWR);
494
495 if (fd_msr < 0) {
496 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
497 return -1;
498 }
499
500 return 0;
501}
502
503void cleanup_cpu_msr(void)
504{
505 if (fd_msr == -1) {
506 msg_pinfo("No MSR initialized.\n");
507 return;
508 }
509
510 close(fd_msr);
511
512 /* Clear MSR file descriptor. */
513 fd_msr = -1;
514}
515
516#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000517#include <sys/ioctl.h>
518
519typedef struct {
520 int msr;
521 uint64_t data;
522} cpu_msr_args_t;
523#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
524#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
525
526static int fd_msr = -1;
527
528msr_t rdmsr(int addr)
529{
530 cpu_msr_args_t args;
531
532 msr_t msr = { 0xffffffff, 0xffffffff };
533
534 args.msr = addr;
535
536 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000537 msg_perr("Error while executing CPU_RDMSR ioctl: %s\n", strerror(errno));
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000538 close(fd_msr);
539 exit(1);
540 }
541
542 msr.lo = args.data & 0xffffffff;
543 msr.hi = args.data >> 32;
544
545 return msr;
546}
547
548int wrmsr(int addr, msr_t msr)
549{
550 cpu_msr_args_t args;
551
552 args.msr = addr;
553 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
554
555 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000556 msg_perr("Error while executing CPU_WRMSR ioctl: %s\n", strerror(errno));
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000557 close(fd_msr);
558 exit(1);
559 }
560
561 return 0;
562}
563
564int setup_cpu_msr(int cpu)
565{
566 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000567 memset(msrfilename, 0, sizeof(msrfilename));
568 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu);
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000569
570 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000571 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000572 return -1;
573 }
574
575 fd_msr = open(msrfilename, O_RDWR);
576
577 if (fd_msr < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000578 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000579 msg_pinfo("Did you install ports/sysutils/devcpu?\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000580 return -1;
581 }
582
583 return 0;
584}
585
586void cleanup_cpu_msr(void)
587{
588 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000589 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000590 return;
591 }
592
593 close(fd_msr);
594
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000595 /* Clear MSR file descriptor. */
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000596 fd_msr = -1;
597}
598
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000599#elif defined(__MACH__) && defined(__APPLE__)
600/* rdmsr() and wrmsr() are provided by DirectHW which needs neither setup nor cleanup. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000601int setup_cpu_msr(int cpu)
602{
603 // Always succeed for now
604 return 0;
605}
606
607void cleanup_cpu_msr(void)
608{
609 // Nothing, yet.
610}
Patrick Georgia9095a92010-09-30 17:03:32 +0000611#elif defined(__LIBPAYLOAD__)
612msr_t libpayload_rdmsr(int addr)
613{
614 msr_t msr;
615 unsigned long long val = _rdmsr(addr);
616 msr.lo = val & 0xffffffff;
617 msr.hi = val >> 32;
618 return msr;
619}
620
621int libpayload_wrmsr(int addr, msr_t msr)
622{
623 _wrmsr(addr, msr.lo | ((unsigned long long)msr.hi << 32));
Patrick Georgi97bc95c2011-03-08 07:17:44 +0000624 return 0;
Patrick Georgia9095a92010-09-30 17:03:32 +0000625}
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +0000626
627int setup_cpu_msr(int cpu)
628{
629 return 0;
630}
631
632void cleanup_cpu_msr(void)
633{
634}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000635#else
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000636/* default MSR implementation */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000637msr_t rdmsr(int addr)
638{
639 msr_t ret = { 0xffffffff, 0xffffffff };
640
641 return ret;
642}
643
644int wrmsr(int addr, msr_t msr)
645{
646 return -1;
647}
648
649int setup_cpu_msr(int cpu)
650{
Sean Nelson316a29f2010-05-07 20:09:04 +0000651 msg_pinfo("No MSR support for your OS yet.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000652 return -1;
653}
654
655void cleanup_cpu_msr(void)
656{
657 // Nothing, yet.
658}
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000659#endif // OS switches for MSR code
660#else // x86
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000661/* Does MSR exist on non-x86 architectures? */
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000662#endif // arch switches for MSR code