Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 4 | */ |
Heinrich Schuchardt | fa4bf41 | 2018-10-14 20:45:32 +0200 | [diff] [blame] | 5 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | 141e5a0 | 2016-07-04 11:57:48 -0600 | [diff] [blame] | 8 | #include <errno.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 9 | #include <linux/libfdt.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 10 | #include <os.h> |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 11 | #include <asm/io.h> |
Simon Glass | 5dccd15 | 2018-05-16 09:42:22 -0600 | [diff] [blame] | 12 | #include <asm/setjmp.h> |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 13 | #include <asm/state.h> |
Simon Glass | be31037 | 2016-07-04 11:57:47 -0600 | [diff] [blame] | 14 | #include <dm/root.h> |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 18 | /* Enable access to PCI memory with map_sysmem() */ |
| 19 | static bool enable_pci_map; |
| 20 | |
| 21 | #ifdef CONFIG_PCI |
| 22 | /* Last device that was mapped into memory, and length of mapping */ |
| 23 | static struct udevice *map_dev; |
| 24 | unsigned long map_len; |
| 25 | #endif |
| 26 | |
Simon Glass | d860f22 | 2015-07-06 12:54:29 -0600 | [diff] [blame] | 27 | void sandbox_exit(void) |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 28 | { |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 29 | /* Do this here while it still has an effect */ |
| 30 | os_fd_restore(); |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 31 | if (state_uninit()) |
| 32 | os_exit(2); |
| 33 | |
Simon Glass | 1920051 | 2014-07-23 06:55:02 -0600 | [diff] [blame] | 34 | if (dm_uninit()) |
| 35 | os_exit(2); |
| 36 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 37 | /* This is considered normal termination for now */ |
| 38 | os_exit(0); |
Simon Glass | dc9f8cd | 2013-11-10 10:27:00 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 41 | /* delay x useconds */ |
| 42 | void __udelay(unsigned long usec) |
| 43 | { |
Simon Glass | b25fa31 | 2015-11-08 23:47:43 -0700 | [diff] [blame] | 44 | struct sandbox_state *state = state_get_current(); |
| 45 | |
| 46 | if (!state->skip_delays) |
| 47 | os_usleep(usec); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 50 | int cleanup_before_linux(void) |
| 51 | { |
| 52 | return 0; |
| 53 | } |
| 54 | |
Simon Glass | 770d93f | 2015-05-13 07:02:26 -0600 | [diff] [blame] | 55 | int cleanup_before_linux_select(int flags) |
| 56 | { |
| 57 | return 0; |
| 58 | } |
| 59 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 60 | /** |
| 61 | * is_in_sandbox_mem() - Checks if a pointer is within sandbox's emulated DRAM |
| 62 | * |
| 63 | * This provides a way to check if a pointer is owned by sandbox (and is within |
| 64 | * its RAM) or not. Sometimes pointers come from a test which conceptually runs |
| 65 | * output sandbox, potentially with direct access to the C-library malloc() |
| 66 | * function, or the sandbox stack (which is not actually within the emulated |
| 67 | * DRAM. |
| 68 | * |
| 69 | * Such pointers obviously cannot be mapped into sandbox's DRAM, so we must |
| 70 | * detect them an process them separately, by recording a mapping to a tag, |
| 71 | * which we can use to map back to the pointer later. |
| 72 | * |
| 73 | * @ptr: Pointer to check |
| 74 | * @return true if this is within sandbox emulated DRAM, false if not |
| 75 | */ |
| 76 | static bool is_in_sandbox_mem(const void *ptr) |
| 77 | { |
| 78 | return (const uint8_t *)ptr >= gd->arch.ram_buf && |
| 79 | (const uint8_t *)ptr < gd->arch.ram_buf + gd->ram_size; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * phys_to_virt() - Converts a sandbox RAM address to a pointer |
| 84 | * |
| 85 | * Sandbox uses U-Boot addresses from 0 to the size of DRAM. These index into |
| 86 | * the emulated DRAM buffer used by sandbox. This function converts such an |
| 87 | * address to a pointer into this buffer, which can be used to access the |
| 88 | * memory. |
| 89 | * |
| 90 | * If the address is outside this range, it is assumed to be a tag |
| 91 | */ |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 92 | void *phys_to_virt(phys_addr_t paddr) |
| 93 | { |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 94 | struct sandbox_mapmem_entry *mentry; |
| 95 | struct sandbox_state *state; |
| 96 | |
| 97 | /* If the address is within emulated DRAM, calculate the value */ |
| 98 | if (paddr < gd->ram_size) |
| 99 | return (void *)(gd->arch.ram_buf + paddr); |
| 100 | |
| 101 | /* |
| 102 | * Otherwise search out list of tags for the correct pointer previously |
| 103 | * created by map_to_sysmem() |
| 104 | */ |
| 105 | state = state_get_current(); |
| 106 | list_for_each_entry(mentry, &state->mapmem_head, sibling_node) { |
| 107 | if (mentry->tag == paddr) { |
Heinrich Schuchardt | fa4bf41 | 2018-10-14 20:45:32 +0200 | [diff] [blame] | 108 | debug("%s: Used map from %lx to %p\n", __func__, |
| 109 | (ulong)paddr, mentry->ptr); |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 110 | return mentry->ptr; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | printf("%s: Cannot map sandbox address %lx (SDRAM from 0 to %lx)\n", |
| 115 | __func__, (ulong)paddr, (ulong)gd->ram_size); |
| 116 | os_abort(); |
| 117 | |
| 118 | /* Not reached */ |
| 119 | return NULL; |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 122 | struct sandbox_mapmem_entry *find_tag(const void *ptr) |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 123 | { |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 124 | struct sandbox_mapmem_entry *mentry; |
| 125 | struct sandbox_state *state = state_get_current(); |
| 126 | |
| 127 | list_for_each_entry(mentry, &state->mapmem_head, sibling_node) { |
| 128 | if (mentry->ptr == ptr) { |
| 129 | debug("%s: Used map from %p to %lx\n", __func__, ptr, |
| 130 | mentry->tag); |
| 131 | return mentry; |
| 132 | } |
| 133 | } |
| 134 | return NULL; |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 137 | phys_addr_t virt_to_phys(void *ptr) |
| 138 | { |
| 139 | struct sandbox_mapmem_entry *mentry; |
| 140 | |
| 141 | /* |
| 142 | * If it is in emulated RAM, don't bother looking for a tag. Just |
| 143 | * calculate the pointer using the provides offset into the RAM buffer. |
| 144 | */ |
| 145 | if (is_in_sandbox_mem(ptr)) |
| 146 | return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf); |
| 147 | |
| 148 | mentry = find_tag(ptr); |
| 149 | if (!mentry) { |
| 150 | /* Abort so that gdb can be used here */ |
| 151 | printf("%s: Cannot map sandbox address %p (SDRAM from 0 to %lx)\n", |
| 152 | __func__, ptr, (ulong)gd->ram_size); |
| 153 | os_abort(); |
| 154 | } |
Heinrich Schuchardt | fa4bf41 | 2018-10-14 20:45:32 +0200 | [diff] [blame] | 155 | debug("%s: Used map from %p to %lx\n", __func__, ptr, mentry->tag); |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 156 | |
| 157 | return mentry->tag; |
| 158 | } |
| 159 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 160 | void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags) |
| 161 | { |
Simon Glass | f44a3a5 | 2016-07-04 11:57:49 -0600 | [diff] [blame] | 162 | #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD) |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 163 | unsigned long plen = len; |
| 164 | void *ptr; |
| 165 | |
| 166 | map_dev = NULL; |
| 167 | if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) { |
| 168 | if (plen != len) { |
| 169 | printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n", |
Mario Six | 61efece | 2018-02-12 08:05:57 +0100 | [diff] [blame] | 170 | __func__, (uint)paddr, len, plen); |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 171 | } |
| 172 | map_len = len; |
| 173 | return ptr; |
| 174 | } |
| 175 | #endif |
| 176 | |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 177 | return phys_to_virt(paddr); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 180 | void unmap_physmem(const void *ptr, unsigned long flags) |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 181 | { |
| 182 | #ifdef CONFIG_PCI |
| 183 | if (map_dev) { |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 184 | pci_unmap_physmem(ptr, map_len, map_dev); |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 185 | map_dev = NULL; |
| 186 | } |
| 187 | #endif |
| 188 | } |
| 189 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 190 | phys_addr_t map_to_sysmem(const void *ptr) |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 191 | { |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 192 | struct sandbox_mapmem_entry *mentry; |
| 193 | |
| 194 | /* |
| 195 | * If it is in emulated RAM, don't bother creating a tag. Just return |
| 196 | * the offset into the RAM buffer. |
| 197 | */ |
| 198 | if (is_in_sandbox_mem(ptr)) |
| 199 | return (u8 *)ptr - gd->arch.ram_buf; |
| 200 | |
| 201 | /* |
| 202 | * See if there is an existing tag with this pointer. If not, set up a |
| 203 | * new one. |
| 204 | */ |
| 205 | mentry = find_tag(ptr); |
| 206 | if (!mentry) { |
| 207 | struct sandbox_state *state = state_get_current(); |
| 208 | |
| 209 | mentry = malloc(sizeof(*mentry)); |
| 210 | if (!mentry) { |
| 211 | printf("%s: Error: Out of memory\n", __func__); |
| 212 | os_exit(ENOMEM); |
| 213 | } |
| 214 | mentry->tag = state->next_tag++; |
| 215 | mentry->ptr = (void *)ptr; |
| 216 | list_add_tail(&mentry->sibling_node, &state->mapmem_head); |
| 217 | debug("%s: Added map from %p to %lx\n", __func__, ptr, |
| 218 | (ulong)mentry->tag); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Return the tag as the address to use. A later call to map_sysmem() |
| 223 | * will return ptr |
| 224 | */ |
| 225 | return mentry->tag; |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Simon Glass | 0a341a7 | 2019-09-25 08:56:09 -0600 | [diff] [blame] | 228 | unsigned int sandbox_read(const void *addr, enum sandboxio_size_t size) |
| 229 | { |
| 230 | struct sandbox_state *state = state_get_current(); |
| 231 | |
| 232 | if (!state->allow_memio) |
| 233 | return 0; |
| 234 | |
| 235 | switch (size) { |
| 236 | case SB_SIZE_8: |
| 237 | return *(u8 *)addr; |
| 238 | case SB_SIZE_16: |
| 239 | return *(u16 *)addr; |
| 240 | case SB_SIZE_32: |
| 241 | return *(u32 *)addr; |
| 242 | case SB_SIZE_64: |
| 243 | return *(u64 *)addr; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Simon Glass | d80cda2 | 2019-10-11 16:16:47 -0600 | [diff] [blame] | 249 | void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size) |
Simon Glass | 0a341a7 | 2019-09-25 08:56:09 -0600 | [diff] [blame] | 250 | { |
| 251 | struct sandbox_state *state = state_get_current(); |
| 252 | |
| 253 | if (!state->allow_memio) |
| 254 | return; |
| 255 | |
| 256 | switch (size) { |
| 257 | case SB_SIZE_8: |
| 258 | *(u8 *)addr = val; |
| 259 | break; |
| 260 | case SB_SIZE_16: |
| 261 | *(u16 *)addr = val; |
| 262 | break; |
| 263 | case SB_SIZE_32: |
| 264 | *(u32 *)addr = val; |
| 265 | break; |
| 266 | case SB_SIZE_64: |
| 267 | *(u64 *)addr = val; |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void sandbox_set_enable_memio(bool enable) |
| 273 | { |
| 274 | struct sandbox_state *state = state_get_current(); |
| 275 | |
| 276 | state->allow_memio = enable; |
| 277 | } |
| 278 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 279 | void sandbox_set_enable_pci_map(int enable) |
Simon Glass | e23d293 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 280 | { |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 281 | enable_pci_map = enable; |
Simon Glass | e23d293 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 284 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 285 | { |
| 286 | } |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 287 | |
Bin Meng | 843c299 | 2017-08-22 08:15:18 -0700 | [diff] [blame] | 288 | void invalidate_dcache_range(unsigned long start, unsigned long stop) |
| 289 | { |
| 290 | } |
| 291 | |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 292 | int sandbox_read_fdt_from_file(void) |
| 293 | { |
| 294 | struct sandbox_state *state = state_get_current(); |
| 295 | const char *fname = state->fdt_fname; |
| 296 | void *blob; |
| 297 | loff_t size; |
| 298 | int err; |
| 299 | int fd; |
| 300 | |
| 301 | blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0); |
| 302 | if (!state->fdt_fname) { |
| 303 | err = fdt_create_empty_tree(blob, 256); |
| 304 | if (!err) |
| 305 | goto done; |
| 306 | printf("Unable to create empty FDT: %s\n", fdt_strerror(err)); |
| 307 | return -EINVAL; |
| 308 | } |
| 309 | |
| 310 | err = os_get_filesize(fname, &size); |
| 311 | if (err < 0) { |
| 312 | printf("Failed to file FDT file '%s'\n", fname); |
| 313 | return err; |
| 314 | } |
| 315 | fd = os_open(fname, OS_O_RDONLY); |
| 316 | if (fd < 0) { |
| 317 | printf("Failed to open FDT file '%s'\n", fname); |
| 318 | return -EACCES; |
| 319 | } |
| 320 | if (os_read(fd, blob, size) != size) { |
| 321 | os_close(fd); |
| 322 | return -EIO; |
| 323 | } |
| 324 | os_close(fd); |
| 325 | |
| 326 | done: |
| 327 | gd->fdt_blob = blob; |
| 328 | |
| 329 | return 0; |
| 330 | } |
Simon Glass | 38e7974 | 2017-05-22 05:05:23 -0600 | [diff] [blame] | 331 | |
| 332 | ulong timer_get_boot_us(void) |
| 333 | { |
| 334 | static uint64_t base_count; |
| 335 | uint64_t count = os_get_nsec(); |
| 336 | |
| 337 | if (!base_count) |
| 338 | base_count = count; |
| 339 | |
| 340 | return (count - base_count) / 1000; |
| 341 | } |