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