Peter Stuge | 33fefca | 2009-01-26 01:33:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Peter Stuge <peter@stuge.se> |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009 coresystems GmbH |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 6 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 7 | * Copyright (C) 2010 Rudolf Marek <r.marek@assembler.cz> |
Peter Stuge | 33fefca | 2009-01-26 01:33:02 +0000 | [diff] [blame] | 8 | * |
| 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. |
Peter Stuge | 33fefca | 2009-01-26 01:33:02 +0000 | [diff] [blame] | 17 | */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 18 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 19 | #include <unistd.h> |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 20 | #include <stdbool.h> |
Thomas Heijligen | 3f4d35d | 2022-01-17 15:11:43 +0100 | [diff] [blame] | 21 | #include <stdint.h> |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 24 | #include <string.h> |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 26 | #include "flash.h" |
Thomas Heijligen | 3f4d35d | 2022-01-17 15:11:43 +0100 | [diff] [blame] | 27 | #include "hwaccess.h" |
Thomas Heijligen | 74b4aa0 | 2021-12-14 17:52:30 +0100 | [diff] [blame] | 28 | #include "hwaccess_physmap.h" |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 29 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 30 | #if !defined(__DJGPP__) && !defined(__LIBPAYLOAD__) |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 31 | /* No file access needed/possible to get mmap access permissions or access MSR. */ |
Thomas Heijligen | 3f4d35d | 2022-01-17 15:11:43 +0100 | [diff] [blame] | 32 | #include <unistd.h> |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 33 | #include <sys/stat.h> |
Thomas Heijligen | 3f4d35d | 2022-01-17 15:11:43 +0100 | [diff] [blame] | 34 | #include <sys/types.h> |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 35 | #include <fcntl.h> |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 38 | #ifdef __DJGPP__ |
| 39 | #include <dpmi.h> |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 40 | #include <malloc.h> |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 41 | #include <sys/nearptr.h> |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 42 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 43 | #define ONE_MEGABYTE (1024 * 1024) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 44 | #define MEM_DEV "dpmi" |
| 45 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 46 | static void *realmem_map_aligned; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 47 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 48 | static void *map_first_meg(uintptr_t phys_addr, size_t len) |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 49 | { |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 50 | void *realmem_map; |
| 51 | size_t pagesize; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 52 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 53 | if (realmem_map_aligned) |
| 54 | return realmem_map_aligned + phys_addr; |
| 55 | |
| 56 | /* valloc() from DJGPP 2.05 does not work properly */ |
| 57 | pagesize = getpagesize(); |
| 58 | |
| 59 | realmem_map = malloc(ONE_MEGABYTE + pagesize); |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 60 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 61 | if (!realmem_map) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 62 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 63 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 64 | realmem_map_aligned = (void *)(((size_t) realmem_map + |
| 65 | (pagesize - 1)) & ~(pagesize - 1)); |
| 66 | |
| 67 | if (__djgpp_map_physical_memory(realmem_map_aligned, ONE_MEGABYTE, 0)) { |
Carl-Daniel Hailfinger | 602de98 | 2010-10-05 23:21:51 +0000 | [diff] [blame] | 68 | free(realmem_map); |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 69 | realmem_map_aligned = NULL; |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 70 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 73 | return realmem_map_aligned + phys_addr; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 74 | } |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 75 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 76 | static void *sys_physmap(uintptr_t phys_addr, size_t len) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 77 | { |
| 78 | int ret; |
| 79 | __dpmi_meminfo mi; |
| 80 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 81 | /* Enable 4GB limit on DS descriptor. */ |
| 82 | if (!__djgpp_nearptr_enable()) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 83 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 84 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 85 | if ((phys_addr + len - 1) < ONE_MEGABYTE) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 86 | /* We need to use another method to map first 1MB. */ |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 87 | return map_first_meg(phys_addr, len); |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | mi.address = phys_addr; |
| 91 | mi.size = len; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 92 | ret = __dpmi_physical_address_mapping(&mi); |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 93 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 94 | if (ret != 0) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 95 | return ERROR_PTR; |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 96 | |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 97 | return (void *) mi.address + __djgpp_conventional_base; |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | #define sys_physmap_rw_uncached sys_physmap |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 101 | #define sys_physmap_ro_cached sys_physmap |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 102 | |
Jacob Garber | beeb8bc | 2019-06-21 15:24:17 -0600 | [diff] [blame] | 103 | static void sys_physunmap_unaligned(void *virt_addr, size_t len) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 104 | { |
| 105 | __dpmi_meminfo mi; |
| 106 | |
Carl-Daniel Hailfinger | 602de98 | 2010-10-05 23:21:51 +0000 | [diff] [blame] | 107 | /* There is no known way to unmap the first 1 MB. The DPMI server will |
| 108 | * do this for us on exit. |
| 109 | */ |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 110 | if ((virt_addr >= realmem_map_aligned) && |
| 111 | ((virt_addr + len) <= (realmem_map_aligned + ONE_MEGABYTE))) { |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
| 115 | mi.address = (unsigned long) virt_addr; |
| 116 | __dpmi_free_physical_address_mapping(&mi); |
| 117 | } |
| 118 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 119 | #elif defined(__LIBPAYLOAD__) |
| 120 | #include <arch/virtual.h> |
| 121 | |
| 122 | #define MEM_DEV "" |
| 123 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 124 | void *sys_physmap(uintptr_t phys_addr, size_t len) |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 125 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 126 | return (void *)phys_to_virt(phys_addr); |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | #define sys_physmap_rw_uncached sys_physmap |
| 130 | #define sys_physmap_ro_cached sys_physmap |
| 131 | |
Jacob Garber | beeb8bc | 2019-06-21 15:24:17 -0600 | [diff] [blame] | 132 | static void sys_physunmap_unaligned(void *virt_addr, size_t len) |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 133 | { |
| 134 | } |
Carl-Daniel Hailfinger | 60d9bd2 | 2012-08-09 23:34:41 +0000 | [diff] [blame] | 135 | #elif defined(__MACH__) && defined(__APPLE__) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 136 | |
Stefan Reinauer | 83704c5 | 2011-03-18 22:00:15 +0000 | [diff] [blame] | 137 | #define MEM_DEV "DirectHW" |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 138 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 139 | static void *sys_physmap(uintptr_t phys_addr, size_t len) |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 140 | { |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 141 | /* The short form of ?: is a GNU extension. |
| 142 | * FIXME: map_physical returns NULL both for errors and for success |
| 143 | * if the region is mapped at virtual address zero. If in doubt, report |
| 144 | * an error until a better interface exists. |
| 145 | */ |
| 146 | return map_physical(phys_addr, len) ? : ERROR_PTR; |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 149 | /* The OS X driver does not differentiate between mapping types. */ |
| 150 | #define sys_physmap_rw_uncached sys_physmap |
| 151 | #define sys_physmap_ro_cached sys_physmap |
| 152 | |
Jacob Garber | beeb8bc | 2019-06-21 15:24:17 -0600 | [diff] [blame] | 153 | static void sys_physunmap_unaligned(void *virt_addr, size_t len) |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 154 | { |
| 155 | unmap_physical(virt_addr, len); |
| 156 | } |
| 157 | |
| 158 | #else |
| 159 | #include <sys/mman.h> |
| 160 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 161 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 162 | # define MEM_DEV "/dev/xsvc" |
| 163 | #else |
| 164 | # define MEM_DEV "/dev/mem" |
| 165 | #endif |
| 166 | |
| 167 | static int fd_mem = -1; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 168 | static int fd_mem_cached = -1; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 169 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 170 | /* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */ |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 171 | static void *sys_physmap_rw_uncached(uintptr_t phys_addr, size_t len) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 172 | { |
| 173 | void *virt_addr; |
| 174 | |
| 175 | if (-1 == fd_mem) { |
| 176 | /* Open the memory device UNCACHED. Important for MMIO. */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 177 | if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 178 | msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno)); |
Niklas Söderlund | 2d8b7ef | 2013-09-13 19:19:25 +0000 | [diff] [blame] | 179 | return ERROR_PTR; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Niklas Söderlund | 2d8b7ef | 2013-09-13 19:19:25 +0000 | [diff] [blame] | 183 | virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fd_mem, (off_t)phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 184 | return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 187 | /* For reading DMI/coreboot/whatever tables. We should never write, and we |
| 188 | * do not care about caching. |
| 189 | */ |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 190 | static void *sys_physmap_ro_cached(uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 191 | { |
| 192 | void *virt_addr; |
| 193 | |
| 194 | if (-1 == fd_mem_cached) { |
| 195 | /* Open the memory device CACHED. */ |
| 196 | if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 197 | msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno)); |
Niklas Söderlund | 2d8b7ef | 2013-09-13 19:19:25 +0000 | [diff] [blame] | 198 | return ERROR_PTR; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Niklas Söderlund | 2d8b7ef | 2013-09-13 19:19:25 +0000 | [diff] [blame] | 202 | virt_addr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd_mem_cached, (off_t)phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 203 | return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Jacob Garber | beeb8bc | 2019-06-21 15:24:17 -0600 | [diff] [blame] | 206 | static void sys_physunmap_unaligned(void *virt_addr, size_t len) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 207 | { |
| 208 | munmap(virt_addr, len); |
| 209 | } |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 210 | #endif |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 211 | |
Stefan Tauner | 3285d7f | 2013-08-14 16:28:19 +0000 | [diff] [blame] | 212 | #define PHYSM_RW 0 |
| 213 | #define PHYSM_RO 1 |
| 214 | #define PHYSM_NOCLEANUP 0 |
| 215 | #define PHYSM_CLEANUP 1 |
| 216 | #define PHYSM_EXACT 0 |
| 217 | #define PHYSM_ROUND 1 |
| 218 | |
| 219 | /* Round start to nearest page boundary below and set len so that the resulting address range ends at the lowest |
| 220 | * possible page boundary where the original address range is still entirely contained. It returns the |
| 221 | * difference between the rounded start address and the original start address. */ |
| 222 | static uintptr_t round_to_page_boundaries(uintptr_t *start, size_t *len) |
| 223 | { |
| 224 | uintptr_t page_size = getpagesize(); |
| 225 | uintptr_t page_mask = ~(page_size-1); |
| 226 | uintptr_t end = *start + *len; |
| 227 | uintptr_t old_start = *start; |
| 228 | msg_gspew("page_size=%" PRIxPTR "\n", page_size); |
| 229 | msg_gspew("pre-rounding: start=0x%0*" PRIxPTR ", len=0x%zx, end=0x%0*" PRIxPTR "\n", |
| 230 | PRIxPTR_WIDTH, *start, *len, PRIxPTR_WIDTH, end); |
| 231 | *start = *start & page_mask; |
| 232 | end = (end + page_size - 1) & page_mask; |
| 233 | *len = end - *start; |
| 234 | msg_gspew("post-rounding: start=0x%0*" PRIxPTR ", len=0x%zx, end=0x%0*" PRIxPTR "\n", |
| 235 | PRIxPTR_WIDTH, *start, *len, PRIxPTR_WIDTH, *start + *len); |
| 236 | return old_start - *start; |
| 237 | } |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 238 | |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 239 | struct undo_physmap_data { |
| 240 | void *virt_addr; |
| 241 | size_t len; |
| 242 | }; |
| 243 | |
| 244 | static int undo_physmap(void *data) |
| 245 | { |
| 246 | if (data == NULL) { |
| 247 | msg_perr("%s: tried to physunmap without valid data!\n", __func__); |
| 248 | return 1; |
| 249 | } |
| 250 | struct undo_physmap_data *d = data; |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 251 | physunmap_unaligned(d->virt_addr, d->len); |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 252 | free(data); |
| 253 | return 0; |
| 254 | } |
| 255 | |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 256 | static void *physmap_common(const char *descr, uintptr_t phys_addr, size_t len, bool readonly, bool autocleanup, |
| 257 | bool round) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 258 | { |
Stephan Guilloux | 6d08a3e | 2009-06-23 10:44:36 +0000 | [diff] [blame] | 259 | void *virt_addr; |
Stefan Tauner | 3285d7f | 2013-08-14 16:28:19 +0000 | [diff] [blame] | 260 | uintptr_t offset = 0; |
Stephan Guilloux | 6d08a3e | 2009-06-23 10:44:36 +0000 | [diff] [blame] | 261 | |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 262 | if (len == 0) { |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 263 | msg_pspew("Not mapping %s, zero size at 0x%0*" PRIxPTR ".\n", descr, PRIxPTR_WIDTH, phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 264 | return ERROR_PTR; |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 265 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 266 | |
Stefan Tauner | 3285d7f | 2013-08-14 16:28:19 +0000 | [diff] [blame] | 267 | if (round) |
| 268 | offset = round_to_page_boundaries(&phys_addr, &len); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 269 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 270 | if (readonly) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 271 | virt_addr = sys_physmap_ro_cached(phys_addr, len); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 272 | else |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 273 | virt_addr = sys_physmap_rw_uncached(phys_addr, len); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 274 | |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 275 | if (ERROR_PTR == virt_addr) { |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 276 | if (NULL == descr) |
| 277 | descr = "memory"; |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 278 | msg_perr("Error accessing %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n", |
| 279 | descr, len, PRIxPTR_WIDTH, phys_addr); |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 280 | msg_perr(MEM_DEV " mmap failed: %s\n", strerror(errno)); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 281 | #ifdef __linux__ |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 282 | if (EINVAL == errno) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 283 | msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); |
| 284 | msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); |
| 285 | msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n"); |
| 286 | msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); |
| 287 | msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n"); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 288 | } |
Carl-Daniel Hailfinger | b63b067 | 2010-07-02 17:12:50 +0000 | [diff] [blame] | 289 | #elif defined (__OpenBSD__) |
| 290 | msg_perr("Please set securelevel=-1 in /etc/rc.securelevel " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 291 | "and reboot, or reboot into\n" |
| 292 | "single user mode.\n"); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 293 | #endif |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 294 | return ERROR_PTR; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 297 | if (autocleanup) { |
Angel Pons | 690a944 | 2021-06-07 12:33:53 +0200 | [diff] [blame] | 298 | struct undo_physmap_data *d = malloc(sizeof(*d)); |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 299 | if (d == NULL) { |
| 300 | msg_perr("%s: Out of memory!\n", __func__); |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 301 | physunmap_unaligned(virt_addr, len); |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 302 | return ERROR_PTR; |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | d->virt_addr = virt_addr; |
| 306 | d->len = len; |
| 307 | if (register_shutdown(undo_physmap, d) != 0) { |
| 308 | msg_perr("%s: Could not register shutdown function!\n", __func__); |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 309 | physunmap_unaligned(virt_addr, len); |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 310 | return ERROR_PTR; |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Stefan Tauner | 3285d7f | 2013-08-14 16:28:19 +0000 | [diff] [blame] | 314 | return virt_addr + offset; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 315 | } |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 316 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 317 | void physunmap_unaligned(void *virt_addr, size_t len) |
| 318 | { |
| 319 | /* No need to check for zero size, such mappings would have yielded ERROR_PTR. */ |
| 320 | if (virt_addr == ERROR_PTR) { |
| 321 | msg_perr("Trying to unmap a nonexisting mapping!\n" |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 322 | "Please report a bug at flashrom-stable@flashrom.org\n"); |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 323 | return; |
| 324 | } |
| 325 | |
| 326 | sys_physunmap_unaligned(virt_addr, len); |
| 327 | } |
| 328 | |
| 329 | void physunmap(void *virt_addr, size_t len) |
| 330 | { |
| 331 | uintptr_t tmp; |
| 332 | |
| 333 | /* No need to check for zero size, such mappings would have yielded ERROR_PTR. */ |
| 334 | if (virt_addr == ERROR_PTR) { |
| 335 | msg_perr("Trying to unmap a nonexisting mapping!\n" |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 336 | "Please report a bug at flashrom-stable@flashrom.org\n"); |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 337 | return; |
| 338 | } |
| 339 | tmp = (uintptr_t)virt_addr; |
| 340 | /* We assume that the virtual address of a page-aligned physical address is page-aligned as well. By |
| 341 | * extension, rounding a virtual unaligned address as returned by physmap should yield the same offset |
| 342 | * between rounded and original virtual address as between rounded and original physical address. |
| 343 | */ |
| 344 | round_to_page_boundaries(&tmp, &len); |
| 345 | virt_addr = (void *)tmp; |
| 346 | physunmap_unaligned(virt_addr, len); |
| 347 | } |
| 348 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 349 | void *physmap(const char *descr, uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 350 | { |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 351 | return physmap_common(descr, phys_addr, len, PHYSM_RW, PHYSM_NOCLEANUP, PHYSM_ROUND); |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void *rphysmap(const char *descr, uintptr_t phys_addr, size_t len) |
| 355 | { |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 356 | return physmap_common(descr, phys_addr, len, PHYSM_RW, PHYSM_CLEANUP, PHYSM_ROUND); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 359 | void *physmap_ro(const char *descr, uintptr_t phys_addr, size_t len) |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 360 | { |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 361 | return physmap_common(descr, phys_addr, len, PHYSM_RO, PHYSM_NOCLEANUP, PHYSM_ROUND); |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 364 | void *physmap_ro_unaligned(const char *descr, uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 365 | { |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 366 | return physmap_common(descr, phys_addr, len, PHYSM_RO, PHYSM_NOCLEANUP, PHYSM_EXACT); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 367 | } |
Thomas Heijligen | 3f4d35d | 2022-01-17 15:11:43 +0100 | [diff] [blame] | 368 | |
| 369 | /* Prevent reordering and/or merging of reads/writes to hardware. |
| 370 | * Such reordering and/or merging would break device accesses which depend on the exact access order. |
| 371 | */ |
| 372 | static inline void sync_primitive(void) |
| 373 | { |
| 374 | /* This is not needed for... |
| 375 | * - x86: uses uncached accesses which have a strongly ordered memory model. |
| 376 | * - MIPS: uses uncached accesses in mode 2 on /dev/mem which has also a strongly ordered memory model. |
| 377 | * - ARM: uses a strongly ordered memory model for device memories. |
| 378 | * |
| 379 | * See also https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/memory-barriers.txt |
| 380 | */ |
| 381 | // cf. http://lxr.free-electrons.com/source/arch/powerpc/include/asm/barrier.h |
| 382 | #if defined(__powerpc) || defined(__powerpc__) || defined(__powerpc64__) || defined(__POWERPC__) || \ |
| 383 | defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || \ |
| 384 | defined(_ARCH_PPC64) || defined(__ppc) |
| 385 | asm("eieio" : : : "memory"); |
| 386 | #elif (__sparc__) || defined (__sparc) |
| 387 | #if defined(__sparc_v9__) || defined(__sparcv9) |
| 388 | /* Sparc V9 CPUs support three different memory orderings that range from x86-like TSO to PowerPC-like |
| 389 | * RMO. The modes can be switched at runtime thus to make sure we maintain the right order of access we |
| 390 | * use the strongest hardware memory barriers that exist on Sparc V9. */ |
| 391 | asm volatile ("membar #Sync" ::: "memory"); |
| 392 | #elif defined(__sparc_v8__) || defined(__sparcv8) |
| 393 | /* On SPARC V8 there is no RMO just PSO and that does not apply to I/O accesses... but if V8 code is run |
| 394 | * on V9 CPUs it might apply... or not... we issue a write barrier anyway. That's the most suitable |
| 395 | * operation in the V8 instruction set anyway. If you know better then please tell us. */ |
| 396 | asm volatile ("stbar"); |
| 397 | #else |
| 398 | #error Unknown and/or unsupported SPARC instruction set version detected. |
| 399 | #endif |
| 400 | #endif |
| 401 | } |
| 402 | |
| 403 | void mmio_writeb(uint8_t val, void *addr) |
| 404 | { |
| 405 | *(volatile uint8_t *) addr = val; |
| 406 | sync_primitive(); |
| 407 | } |
| 408 | |
| 409 | void mmio_writew(uint16_t val, void *addr) |
| 410 | { |
| 411 | *(volatile uint16_t *) addr = val; |
| 412 | sync_primitive(); |
| 413 | } |
| 414 | |
| 415 | void mmio_writel(uint32_t val, void *addr) |
| 416 | { |
| 417 | *(volatile uint32_t *) addr = val; |
| 418 | sync_primitive(); |
| 419 | } |
| 420 | |
| 421 | uint8_t mmio_readb(const void *addr) |
| 422 | { |
| 423 | return *(volatile const uint8_t *) addr; |
| 424 | } |
| 425 | |
| 426 | uint16_t mmio_readw(const void *addr) |
| 427 | { |
| 428 | return *(volatile const uint16_t *) addr; |
| 429 | } |
| 430 | |
| 431 | uint32_t mmio_readl(const void *addr) |
| 432 | { |
| 433 | return *(volatile const uint32_t *) addr; |
| 434 | } |
| 435 | |
| 436 | void mmio_readn(const void *addr, uint8_t *buf, size_t len) |
| 437 | { |
| 438 | memcpy(buf, addr, len); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | void mmio_le_writeb(uint8_t val, void *addr) |
| 443 | { |
| 444 | mmio_writeb(cpu_to_le8(val), addr); |
| 445 | } |
| 446 | |
| 447 | void mmio_le_writew(uint16_t val, void *addr) |
| 448 | { |
| 449 | mmio_writew(cpu_to_le16(val), addr); |
| 450 | } |
| 451 | |
| 452 | void mmio_le_writel(uint32_t val, void *addr) |
| 453 | { |
| 454 | mmio_writel(cpu_to_le32(val), addr); |
| 455 | } |
| 456 | |
| 457 | uint8_t mmio_le_readb(const void *addr) |
| 458 | { |
| 459 | return le_to_cpu8(mmio_readb(addr)); |
| 460 | } |
| 461 | |
| 462 | uint16_t mmio_le_readw(const void *addr) |
| 463 | { |
| 464 | return le_to_cpu16(mmio_readw(addr)); |
| 465 | } |
| 466 | |
| 467 | uint32_t mmio_le_readl(const void *addr) |
| 468 | { |
| 469 | return le_to_cpu32(mmio_readl(addr)); |
| 470 | } |
| 471 | |
| 472 | enum mmio_write_type { |
| 473 | mmio_write_type_b, |
| 474 | mmio_write_type_w, |
| 475 | mmio_write_type_l, |
| 476 | }; |
| 477 | |
| 478 | struct undo_mmio_write_data { |
| 479 | void *addr; |
| 480 | int reg; |
| 481 | enum mmio_write_type type; |
| 482 | union { |
| 483 | uint8_t bdata; |
| 484 | uint16_t wdata; |
| 485 | uint32_t ldata; |
| 486 | }; |
| 487 | }; |
| 488 | |
| 489 | static int undo_mmio_write(void *p) |
| 490 | { |
| 491 | struct undo_mmio_write_data *data = p; |
| 492 | msg_pdbg("Restoring MMIO space at %p\n", data->addr); |
| 493 | switch (data->type) { |
| 494 | case mmio_write_type_b: |
| 495 | mmio_writeb(data->bdata, data->addr); |
| 496 | break; |
| 497 | case mmio_write_type_w: |
| 498 | mmio_writew(data->wdata, data->addr); |
| 499 | break; |
| 500 | case mmio_write_type_l: |
| 501 | mmio_writel(data->ldata, data->addr); |
| 502 | break; |
| 503 | } |
| 504 | /* p was allocated in register_undo_mmio_write. */ |
| 505 | free(p); |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | #define register_undo_mmio_write(a, c) \ |
| 510 | { \ |
| 511 | struct undo_mmio_write_data *undo_mmio_write_data; \ |
| 512 | undo_mmio_write_data = malloc(sizeof(*undo_mmio_write_data)); \ |
| 513 | if (!undo_mmio_write_data) { \ |
| 514 | msg_gerr("Out of memory!\n"); \ |
| 515 | exit(1); \ |
| 516 | } \ |
| 517 | undo_mmio_write_data->addr = a; \ |
| 518 | undo_mmio_write_data->type = mmio_write_type_##c; \ |
| 519 | undo_mmio_write_data->c##data = mmio_read##c(a); \ |
| 520 | register_shutdown(undo_mmio_write, undo_mmio_write_data); \ |
| 521 | } |
| 522 | |
| 523 | #define register_undo_mmio_writeb(a) register_undo_mmio_write(a, b) |
| 524 | #define register_undo_mmio_writew(a) register_undo_mmio_write(a, w) |
| 525 | #define register_undo_mmio_writel(a) register_undo_mmio_write(a, l) |
| 526 | |
| 527 | void rmmio_writeb(uint8_t val, void *addr) |
| 528 | { |
| 529 | register_undo_mmio_writeb(addr); |
| 530 | mmio_writeb(val, addr); |
| 531 | } |
| 532 | |
| 533 | void rmmio_writew(uint16_t val, void *addr) |
| 534 | { |
| 535 | register_undo_mmio_writew(addr); |
| 536 | mmio_writew(val, addr); |
| 537 | } |
| 538 | |
| 539 | void rmmio_writel(uint32_t val, void *addr) |
| 540 | { |
| 541 | register_undo_mmio_writel(addr); |
| 542 | mmio_writel(val, addr); |
| 543 | } |
| 544 | |
| 545 | void rmmio_le_writeb(uint8_t val, void *addr) |
| 546 | { |
| 547 | register_undo_mmio_writeb(addr); |
| 548 | mmio_le_writeb(val, addr); |
| 549 | } |
| 550 | |
| 551 | void rmmio_le_writew(uint16_t val, void *addr) |
| 552 | { |
| 553 | register_undo_mmio_writew(addr); |
| 554 | mmio_le_writew(val, addr); |
| 555 | } |
| 556 | |
| 557 | void rmmio_le_writel(uint32_t val, void *addr) |
| 558 | { |
| 559 | register_undo_mmio_writel(addr); |
| 560 | mmio_le_writel(val, addr); |
| 561 | } |
| 562 | |
| 563 | void rmmio_valb(void *addr) |
| 564 | { |
| 565 | register_undo_mmio_writeb(addr); |
| 566 | } |
| 567 | |
| 568 | void rmmio_valw(void *addr) |
| 569 | { |
| 570 | register_undo_mmio_writew(addr); |
| 571 | } |
| 572 | |
| 573 | void rmmio_vall(void *addr) |
| 574 | { |
| 575 | register_undo_mmio_writel(addr); |
| 576 | } |