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