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