1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
10 #include <linux/libfdt.h>
13 #include <asm/setjmp.h>
14 #include <asm/state.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 /* Enable access to PCI memory with map_sysmem() */
20 static bool enable_pci_map;
23 /* Last device that was mapped into memory, and length of mapping */
24 static struct udevice *map_dev;
25 unsigned long map_len;
28 void sandbox_exit(void)
30 /* Do this here while it still has an effect */
38 /* This is considered normal termination for now */
42 /* delay x useconds */
43 void __udelay(unsigned long usec)
45 struct sandbox_state *state = state_get_current();
47 if (!state->skip_delays)
51 int cleanup_before_linux(void)
56 int cleanup_before_linux_select(int flags)
62 * is_in_sandbox_mem() - Checks if a pointer is within sandbox's emulated DRAM
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
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.
74 * @ptr: Pointer to check
75 * @return true if this is within sandbox emulated DRAM, false if not
77 static bool is_in_sandbox_mem(const void *ptr)
79 return (const uint8_t *)ptr >= gd->arch.ram_buf &&
80 (const uint8_t *)ptr < gd->arch.ram_buf + gd->ram_size;
84 * phys_to_virt() - Converts a sandbox RAM address to a pointer
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
91 * If the address is outside this range, it is assumed to be a tag
93 void *phys_to_virt(phys_addr_t paddr)
95 struct sandbox_mapmem_entry *mentry;
96 struct sandbox_state *state;
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);
103 * Otherwise search out list of tags for the correct pointer previously
104 * created by map_to_sysmem()
106 state = state_get_current();
107 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
108 if (mentry->tag == paddr) {
109 debug("%s: Used map from %lx to %p\n", __func__,
110 (ulong)paddr, mentry->ptr);
115 printf("%s: Cannot map sandbox address %lx (SDRAM from 0 to %lx)\n",
116 __func__, (ulong)paddr, (ulong)gd->ram_size);
123 struct sandbox_mapmem_entry *find_tag(const void *ptr)
125 struct sandbox_mapmem_entry *mentry;
126 struct sandbox_state *state = state_get_current();
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,
138 phys_addr_t virt_to_phys(void *ptr)
140 struct sandbox_mapmem_entry *mentry;
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.
146 if (is_in_sandbox_mem(ptr))
147 return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf);
149 mentry = find_tag(ptr);
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);
156 debug("%s: Used map from %p to %lx\n", __func__, ptr, mentry->tag);
161 void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
163 #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
164 unsigned long plen = len;
168 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
170 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
171 __func__, (uint)paddr, len, plen);
178 return phys_to_virt(paddr);
181 void unmap_physmem(const void *ptr, unsigned long flags)
185 pci_unmap_physmem(ptr, map_len, map_dev);
191 phys_addr_t map_to_sysmem(const void *ptr)
193 struct sandbox_mapmem_entry *mentry;
196 * If it is in emulated RAM, don't bother creating a tag. Just return
197 * the offset into the RAM buffer.
199 if (is_in_sandbox_mem(ptr))
200 return (u8 *)ptr - gd->arch.ram_buf;
203 * See if there is an existing tag with this pointer. If not, set up a
206 mentry = find_tag(ptr);
208 struct sandbox_state *state = state_get_current();
210 mentry = malloc(sizeof(*mentry));
212 printf("%s: Error: Out of memory\n", __func__);
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,
223 * Return the tag as the address to use. A later call to map_sysmem()
229 unsigned int sandbox_read(const void *addr, enum sandboxio_size_t size)
231 struct sandbox_state *state = state_get_current();
233 if (!state->allow_memio)
250 void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size)
252 struct sandbox_state *state = state_get_current();
254 if (!state->allow_memio)
273 void sandbox_set_enable_memio(bool enable)
275 struct sandbox_state *state = state_get_current();
277 state->allow_memio = enable;
280 void sandbox_set_enable_pci_map(int enable)
282 enable_pci_map = enable;
285 void flush_dcache_range(unsigned long start, unsigned long stop)
289 void invalidate_dcache_range(unsigned long start, unsigned long stop)
293 int sandbox_read_fdt_from_file(void)
295 struct sandbox_state *state = state_get_current();
296 const char *fname = state->fdt_fname;
302 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
303 if (!state->fdt_fname) {
304 err = fdt_create_empty_tree(blob, 256);
307 printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
311 err = os_get_filesize(fname, &size);
313 printf("Failed to file FDT file '%s'\n", fname);
316 fd = os_open(fname, OS_O_RDONLY);
318 printf("Failed to open FDT file '%s'\n", fname);
321 if (os_read(fd, blob, size) != size) {
333 ulong timer_get_boot_us(void)
335 static uint64_t base_count;
336 uint64_t count = os_get_nsec();
341 return (count - base_count) / 1000;