1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
6 #define LOG_CATEGORY LOGC_SANDBOX
14 #include <asm/global_data.h>
16 #include <asm/malloc.h>
17 #include <asm/setjmp.h>
18 #include <asm/state.h>
19 #include <dm/ofnode.h>
20 #include <linux/delay.h>
21 #include <linux/libfdt.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 /* Enable access to PCI memory with map_sysmem() */
26 static bool enable_pci_map;
29 /* Last device that was mapped into memory, and length of mapping */
30 static struct udevice *map_dev;
31 unsigned long map_len;
34 void __noreturn sandbox_exit(void)
36 /* Do this here while it still has an effect */
42 /* This is considered normal termination for now */
46 /* delay x useconds */
47 void __udelay(unsigned long usec)
49 struct sandbox_state *state = state_get_current();
51 if (!state->skip_delays)
55 int cleanup_before_linux(void)
60 int cleanup_before_linux_select(int flags)
66 * is_in_sandbox_mem() - Checks if a pointer is within sandbox's emulated DRAM
68 * This provides a way to check if a pointer is owned by sandbox (and is within
69 * its RAM) or not. Sometimes pointers come from a test which conceptually runs
70 * output sandbox, potentially with direct access to the C-library malloc()
71 * function, or the sandbox stack (which is not actually within the emulated
74 * Such pointers obviously cannot be mapped into sandbox's DRAM, so we must
75 * detect them an process them separately, by recording a mapping to a tag,
76 * which we can use to map back to the pointer later.
78 * @ptr: Pointer to check
79 * Return: true if this is within sandbox emulated DRAM, false if not
81 static bool is_in_sandbox_mem(const void *ptr)
83 return (const uint8_t *)ptr >= gd->arch.ram_buf &&
84 (const uint8_t *)ptr < gd->arch.ram_buf + gd->ram_size;
88 * phys_to_virt() - Converts a sandbox RAM address to a pointer
90 * Sandbox uses U-Boot addresses from 0 to the size of DRAM. These index into
91 * the emulated DRAM buffer used by sandbox. This function converts such an
92 * address to a pointer into this buffer, which can be used to access the
95 * If the address is outside this range, it is assumed to be a tag
97 void *phys_to_virt(phys_addr_t paddr)
99 struct sandbox_mapmem_entry *mentry;
100 struct sandbox_state *state;
102 /* If the address is within emulated DRAM, calculate the value */
103 if (paddr < gd->ram_size)
104 return (void *)(gd->arch.ram_buf + paddr);
107 * Otherwise search out list of tags for the correct pointer previously
108 * created by map_to_sysmem()
110 state = state_get_current();
111 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
112 if (mentry->tag == paddr) {
113 debug("%s: Used map from %lx to %p\n", __func__,
114 (ulong)paddr, mentry->ptr);
119 printf("%s: Cannot map sandbox address %lx (SDRAM from 0 to %lx)\n",
120 __func__, (ulong)paddr, (ulong)gd->ram_size);
127 struct sandbox_mapmem_entry *find_tag(const void *ptr)
129 struct sandbox_mapmem_entry *mentry;
130 struct sandbox_state *state = state_get_current();
132 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
133 if (mentry->ptr == ptr) {
134 debug("%s: Used map from %p to %lx\n", __func__, ptr,
142 phys_addr_t virt_to_phys(void *ptr)
144 struct sandbox_mapmem_entry *mentry;
147 * If it is in emulated RAM, don't bother looking for a tag. Just
148 * calculate the pointer using the provides offset into the RAM buffer.
150 if (is_in_sandbox_mem(ptr))
151 return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf);
153 mentry = find_tag(ptr);
155 /* Abort so that gdb can be used here */
156 printf("%s: Cannot map sandbox address %p (SDRAM from 0 to %lx)\n",
157 __func__, ptr, (ulong)gd->ram_size);
160 debug("%s: Used map from %p to %lx\n", __func__, ptr, mentry->tag);
165 void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
167 #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
168 unsigned long plen = len;
172 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
174 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
175 __func__, (uint)paddr, len, plen);
182 return phys_to_virt(paddr);
185 void unmap_physmem(const void *ptr, unsigned long flags)
189 pci_unmap_physmem(ptr, map_len, map_dev);
195 phys_addr_t map_to_sysmem(const void *ptr)
197 struct sandbox_mapmem_entry *mentry;
200 * If it is in emulated RAM, don't bother creating a tag. Just return
201 * the offset into the RAM buffer.
203 if (is_in_sandbox_mem(ptr))
204 return (u8 *)ptr - gd->arch.ram_buf;
207 * See if there is an existing tag with this pointer. If not, set up a
210 mentry = find_tag(ptr);
212 struct sandbox_state *state = state_get_current();
214 mentry = malloc(sizeof(*mentry));
216 printf("%s: Error: Out of memory\n", __func__);
219 mentry->tag = state->next_tag++;
220 mentry->ptr = (void *)ptr;
221 list_add_tail(&mentry->sibling_node, &state->mapmem_head);
222 debug("%s: Added map from %p to %lx\n", __func__, ptr,
227 * Return the tag as the address to use. A later call to map_sysmem()
233 unsigned long sandbox_read(const void *addr, enum sandboxio_size_t size)
235 struct sandbox_state *state = state_get_current();
237 if (!state->allow_memio)
254 void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size)
256 struct sandbox_state *state = state_get_current();
258 if (!state->allow_memio)
277 void sandbox_set_enable_memio(bool enable)
279 struct sandbox_state *state = state_get_current();
281 state->allow_memio = enable;
284 void sandbox_set_enable_pci_map(int enable)
286 enable_pci_map = enable;
289 int dcache_status(void)
294 void flush_dcache_range(unsigned long start, unsigned long stop)
298 void invalidate_dcache_range(unsigned long start, unsigned long stop)
303 * setup_auto_tree() - Set up a basic device tree to allow sandbox to work
305 * This is used when no device tree is provided. It creates a simple tree with
306 * just a /binman node.
308 * @blob: Place to put the created device tree
309 * Returns: 0 on success, -ve FDT error code on failure
311 static int setup_auto_tree(void *blob)
315 err = fdt_create_empty_tree(blob, 256);
319 /* Create a /binman node in case CONFIG_BINMAN is enabled */
320 err = fdt_add_subnode(blob, 0, "binman");
327 void *board_fdt_blob_setup(int *ret)
329 struct sandbox_state *state = state_get_current();
330 const char *fname = state->fdt_fname;
336 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
338 if (!state->fdt_fname) {
339 err = setup_auto_tree(blob);
342 os_printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
347 err = os_get_filesize(fname, &size);
349 os_printf("Failed to find FDT file '%s'\n", fname);
353 fd = os_open(fname, OS_O_RDONLY);
355 os_printf("Failed to open FDT file '%s'\n", fname);
360 if (os_read(fd, blob, size) != size) {
362 os_printf("Failed to read FDT file '%s'\n", fname);
374 ulong timer_get_boot_us(void)
376 static uint64_t base_count;
377 uint64_t count = os_get_nsec();
382 return (count - base_count) / 1000;
385 int sandbox_load_other_fdt(void **fdtp, int *sizep)
391 ret = state_load_other_fdt(&orig, &size);
393 log_err("Cannot read other FDT\n");
394 return log_msg_ret("ld", ret);
398 fdt = os_malloc(size);
400 return log_msg_ret("mem", -ENOMEM);
404 memcpy(fdt, orig, *sizep);