1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
9 #include <linux/libfdt.h>
12 #include <asm/setjmp.h>
13 #include <asm/state.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 /* Enable access to PCI memory with map_sysmem() */
19 static bool enable_pci_map;
22 /* Last device that was mapped into memory, and length of mapping */
23 static struct udevice *map_dev;
24 unsigned long map_len;
27 void sandbox_exit(void)
29 /* Do this here while it still has an effect */
37 /* This is considered normal termination for now */
41 /* delay x useconds */
42 void __udelay(unsigned long usec)
44 struct sandbox_state *state = state_get_current();
46 if (!state->skip_delays)
50 int cleanup_before_linux(void)
55 int cleanup_before_linux_select(int flags)
61 * is_in_sandbox_mem() - Checks if a pointer is within sandbox's emulated DRAM
63 * This provides a way to check if a pointer is owned by sandbox (and is within
64 * its RAM) or not. Sometimes pointers come from a test which conceptually runs
65 * output sandbox, potentially with direct access to the C-library malloc()
66 * function, or the sandbox stack (which is not actually within the emulated
69 * Such pointers obviously cannot be mapped into sandbox's DRAM, so we must
70 * detect them an process them separately, by recording a mapping to a tag,
71 * which we can use to map back to the pointer later.
73 * @ptr: Pointer to check
74 * @return true if this is within sandbox emulated DRAM, false if not
76 static bool is_in_sandbox_mem(const void *ptr)
78 return (const uint8_t *)ptr >= gd->arch.ram_buf &&
79 (const uint8_t *)ptr < gd->arch.ram_buf + gd->ram_size;
83 * phys_to_virt() - Converts a sandbox RAM address to a pointer
85 * Sandbox uses U-Boot addresses from 0 to the size of DRAM. These index into
86 * the emulated DRAM buffer used by sandbox. This function converts such an
87 * address to a pointer into this buffer, which can be used to access the
90 * If the address is outside this range, it is assumed to be a tag
92 void *phys_to_virt(phys_addr_t paddr)
94 struct sandbox_mapmem_entry *mentry;
95 struct sandbox_state *state;
97 /* If the address is within emulated DRAM, calculate the value */
98 if (paddr < gd->ram_size)
99 return (void *)(gd->arch.ram_buf + paddr);
102 * Otherwise search out list of tags for the correct pointer previously
103 * created by map_to_sysmem()
105 state = state_get_current();
106 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
107 if (mentry->tag == paddr) {
108 debug("%s: Used map from %lx to %p\n", __func__,
109 (ulong)paddr, mentry->ptr);
114 printf("%s: Cannot map sandbox address %lx (SDRAM from 0 to %lx)\n",
115 __func__, (ulong)paddr, (ulong)gd->ram_size);
122 struct sandbox_mapmem_entry *find_tag(const void *ptr)
124 struct sandbox_mapmem_entry *mentry;
125 struct sandbox_state *state = state_get_current();
127 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
128 if (mentry->ptr == ptr) {
129 debug("%s: Used map from %p to %lx\n", __func__, ptr,
137 phys_addr_t virt_to_phys(void *ptr)
139 struct sandbox_mapmem_entry *mentry;
142 * If it is in emulated RAM, don't bother looking for a tag. Just
143 * calculate the pointer using the provides offset into the RAM buffer.
145 if (is_in_sandbox_mem(ptr))
146 return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf);
148 mentry = find_tag(ptr);
150 /* Abort so that gdb can be used here */
151 printf("%s: Cannot map sandbox address %p (SDRAM from 0 to %lx)\n",
152 __func__, ptr, (ulong)gd->ram_size);
155 debug("%s: Used map from %p to %lx\n", __func__, ptr, mentry->tag);
160 void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
162 #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
163 unsigned long plen = len;
167 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
169 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
170 __func__, (uint)paddr, len, plen);
177 return phys_to_virt(paddr);
180 void unmap_physmem(const void *ptr, unsigned long flags)
184 pci_unmap_physmem(ptr, map_len, map_dev);
190 phys_addr_t map_to_sysmem(const void *ptr)
192 struct sandbox_mapmem_entry *mentry;
195 * If it is in emulated RAM, don't bother creating a tag. Just return
196 * the offset into the RAM buffer.
198 if (is_in_sandbox_mem(ptr))
199 return (u8 *)ptr - gd->arch.ram_buf;
202 * See if there is an existing tag with this pointer. If not, set up a
205 mentry = find_tag(ptr);
207 struct sandbox_state *state = state_get_current();
209 mentry = malloc(sizeof(*mentry));
211 printf("%s: Error: Out of memory\n", __func__);
214 mentry->tag = state->next_tag++;
215 mentry->ptr = (void *)ptr;
216 list_add_tail(&mentry->sibling_node, &state->mapmem_head);
217 debug("%s: Added map from %p to %lx\n", __func__, ptr,
222 * Return the tag as the address to use. A later call to map_sysmem()
228 unsigned int sandbox_read(const void *addr, enum sandboxio_size_t size)
230 struct sandbox_state *state = state_get_current();
232 if (!state->allow_memio)
249 void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size)
251 struct sandbox_state *state = state_get_current();
253 if (!state->allow_memio)
272 void sandbox_set_enable_memio(bool enable)
274 struct sandbox_state *state = state_get_current();
276 state->allow_memio = enable;
279 void sandbox_set_enable_pci_map(int enable)
281 enable_pci_map = enable;
284 void flush_dcache_range(unsigned long start, unsigned long stop)
288 void invalidate_dcache_range(unsigned long start, unsigned long stop)
292 int sandbox_read_fdt_from_file(void)
294 struct sandbox_state *state = state_get_current();
295 const char *fname = state->fdt_fname;
301 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
302 if (!state->fdt_fname) {
303 err = fdt_create_empty_tree(blob, 256);
306 printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
310 err = os_get_filesize(fname, &size);
312 printf("Failed to file FDT file '%s'\n", fname);
315 fd = os_open(fname, OS_O_RDONLY);
317 printf("Failed to open FDT file '%s'\n", fname);
320 if (os_read(fd, blob, size) != size) {
332 ulong timer_get_boot_us(void)
334 static uint64_t base_count;
335 uint64_t count = os_get_nsec();
340 return (count - base_count) / 1000;