1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
12 #include <linux/libfdt.h>
15 #include <asm/malloc.h>
16 #include <asm/setjmp.h>
17 #include <asm/state.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 /* Enable access to PCI memory with map_sysmem() */
23 static bool enable_pci_map;
26 /* Last device that was mapped into memory, and length of mapping */
27 static struct udevice *map_dev;
28 unsigned long map_len;
31 void sandbox_exit(void)
33 /* Do this here while it still has an effect */
41 /* This is considered normal termination for now */
45 /* delay x useconds */
46 void __udelay(unsigned long usec)
48 struct sandbox_state *state = state_get_current();
50 if (!state->skip_delays)
54 int cleanup_before_linux(void)
59 int cleanup_before_linux_select(int flags)
65 * is_in_sandbox_mem() - Checks if a pointer is within sandbox's emulated DRAM
67 * This provides a way to check if a pointer is owned by sandbox (and is within
68 * its RAM) or not. Sometimes pointers come from a test which conceptually runs
69 * output sandbox, potentially with direct access to the C-library malloc()
70 * function, or the sandbox stack (which is not actually within the emulated
73 * Such pointers obviously cannot be mapped into sandbox's DRAM, so we must
74 * detect them an process them separately, by recording a mapping to a tag,
75 * which we can use to map back to the pointer later.
77 * @ptr: Pointer to check
78 * @return true if this is within sandbox emulated DRAM, false if not
80 static bool is_in_sandbox_mem(const void *ptr)
82 return (const uint8_t *)ptr >= gd->arch.ram_buf &&
83 (const uint8_t *)ptr < gd->arch.ram_buf + gd->ram_size;
87 * phys_to_virt() - Converts a sandbox RAM address to a pointer
89 * Sandbox uses U-Boot addresses from 0 to the size of DRAM. These index into
90 * the emulated DRAM buffer used by sandbox. This function converts such an
91 * address to a pointer into this buffer, which can be used to access the
94 * If the address is outside this range, it is assumed to be a tag
96 void *phys_to_virt(phys_addr_t paddr)
98 struct sandbox_mapmem_entry *mentry;
99 struct sandbox_state *state;
101 /* If the address is within emulated DRAM, calculate the value */
102 if (paddr < gd->ram_size)
103 return (void *)(gd->arch.ram_buf + paddr);
106 * Otherwise search out list of tags for the correct pointer previously
107 * created by map_to_sysmem()
109 state = state_get_current();
110 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
111 if (mentry->tag == paddr) {
112 debug("%s: Used map from %lx to %p\n", __func__,
113 (ulong)paddr, mentry->ptr);
118 printf("%s: Cannot map sandbox address %lx (SDRAM from 0 to %lx)\n",
119 __func__, (ulong)paddr, (ulong)gd->ram_size);
126 struct sandbox_mapmem_entry *find_tag(const void *ptr)
128 struct sandbox_mapmem_entry *mentry;
129 struct sandbox_state *state = state_get_current();
131 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
132 if (mentry->ptr == ptr) {
133 debug("%s: Used map from %p to %lx\n", __func__, ptr,
141 phys_addr_t virt_to_phys(void *ptr)
143 struct sandbox_mapmem_entry *mentry;
146 * If it is in emulated RAM, don't bother looking for a tag. Just
147 * calculate the pointer using the provides offset into the RAM buffer.
149 if (is_in_sandbox_mem(ptr))
150 return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf);
152 mentry = find_tag(ptr);
154 /* Abort so that gdb can be used here */
155 printf("%s: Cannot map sandbox address %p (SDRAM from 0 to %lx)\n",
156 __func__, ptr, (ulong)gd->ram_size);
159 debug("%s: Used map from %p to %lx\n", __func__, ptr, mentry->tag);
164 void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
166 #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
167 unsigned long plen = len;
171 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
173 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
174 __func__, (uint)paddr, len, plen);
181 return phys_to_virt(paddr);
184 void unmap_physmem(const void *ptr, unsigned long flags)
188 pci_unmap_physmem(ptr, map_len, map_dev);
194 phys_addr_t map_to_sysmem(const void *ptr)
196 struct sandbox_mapmem_entry *mentry;
199 * If it is in emulated RAM, don't bother creating a tag. Just return
200 * the offset into the RAM buffer.
202 if (is_in_sandbox_mem(ptr))
203 return (u8 *)ptr - gd->arch.ram_buf;
206 * See if there is an existing tag with this pointer. If not, set up a
209 mentry = find_tag(ptr);
211 struct sandbox_state *state = state_get_current();
213 mentry = malloc(sizeof(*mentry));
215 printf("%s: Error: Out of memory\n", __func__);
218 mentry->tag = state->next_tag++;
219 mentry->ptr = (void *)ptr;
220 list_add_tail(&mentry->sibling_node, &state->mapmem_head);
221 debug("%s: Added map from %p to %lx\n", __func__, ptr,
226 * Return the tag as the address to use. A later call to map_sysmem()
232 unsigned int sandbox_read(const void *addr, enum sandboxio_size_t size)
234 struct sandbox_state *state = state_get_current();
236 if (!state->allow_memio)
253 void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size)
255 struct sandbox_state *state = state_get_current();
257 if (!state->allow_memio)
276 void sandbox_set_enable_memio(bool enable)
278 struct sandbox_state *state = state_get_current();
280 state->allow_memio = enable;
283 void sandbox_set_enable_pci_map(int enable)
285 enable_pci_map = enable;
288 void flush_dcache_range(unsigned long start, unsigned long stop)
292 void invalidate_dcache_range(unsigned long start, unsigned long stop)
296 int sandbox_read_fdt_from_file(void)
298 struct sandbox_state *state = state_get_current();
299 const char *fname = state->fdt_fname;
305 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
306 if (!state->fdt_fname) {
307 err = fdt_create_empty_tree(blob, 256);
310 printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
314 err = os_get_filesize(fname, &size);
316 printf("Failed to file FDT file '%s'\n", fname);
319 fd = os_open(fname, OS_O_RDONLY);
321 printf("Failed to open FDT file '%s'\n", fname);
324 if (os_read(fd, blob, size) != size) {
336 ulong timer_get_boot_us(void)
338 static uint64_t base_count;
339 uint64_t count = os_get_nsec();
344 return (count - base_count) / 1000;