blob: 3e1c0dd583e978fa50e16623c99e29102024ef01 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass3d750d72011-09-26 14:10:39 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass3d750d72011-09-26 14:10:39 +00004 */
Heinrich Schuchardtfa4bf412018-10-14 20:45:32 +02005
Simon Glass57b00a92022-09-06 20:27:10 -06006#define LOG_CATEGORY LOGC_SANDBOX
7
Simon Glass1ea97892020-05-10 11:40:00 -06008#include <bootstage.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glass141e5a02016-07-04 11:57:48 -060010#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000012#include <os.h>
Simon Glass57b00a92022-09-06 20:27:10 -060013#include <asm/global_data.h>
Simon Glass26b78b22015-02-27 22:06:34 -070014#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <asm/malloc.h>
Simon Glass5dccd152018-05-16 09:42:22 -060016#include <asm/setjmp.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070017#include <asm/state.h>
Simon Glass57b00a92022-09-06 20:27:10 -060018#include <dm/ofnode.h>
19#include <linux/delay.h>
20#include <linux/libfdt.h>
Simon Glass3d750d72011-09-26 14:10:39 +000021
22DECLARE_GLOBAL_DATA_PTR;
23
Simon Glass70778bc2015-03-05 12:25:26 -070024/* Enable access to PCI memory with map_sysmem() */
25static bool enable_pci_map;
26
27#ifdef CONFIG_PCI
28/* Last device that was mapped into memory, and length of mapping */
29static struct udevice *map_dev;
30unsigned long map_len;
31#endif
32
Heinrich Schuchardt2722a9b2023-04-01 09:54:25 +020033void __noreturn sandbox_exit(void)
Simon Glass3d750d72011-09-26 14:10:39 +000034{
Simon Glass9c3b7d62015-05-10 21:07:27 -060035 /* Do this here while it still has an effect */
36 os_fd_restore();
Simon Glass9dd10bf2013-11-10 10:27:03 -070037
Simon Glass1b0851f2021-03-15 18:11:24 +130038 if (state_uninit())
Simon Glass19200512014-07-23 06:55:02 -060039 os_exit(2);
40
Simon Glasscd0684f2011-10-03 19:26:44 +000041 /* This is considered normal termination for now */
42 os_exit(0);
Simon Glassdc9f8cd2013-11-10 10:27:00 -070043}
44
Simon Glass3d750d72011-09-26 14:10:39 +000045/* delay x useconds */
46void __udelay(unsigned long usec)
47{
Simon Glassb25fa312015-11-08 23:47:43 -070048 struct sandbox_state *state = state_get_current();
49
50 if (!state->skip_delays)
51 os_usleep(usec);
Simon Glass3d750d72011-09-26 14:10:39 +000052}
53
Simon Glass3d750d72011-09-26 14:10:39 +000054int cleanup_before_linux(void)
55{
56 return 0;
57}
58
Simon Glass770d93f2015-05-13 07:02:26 -060059int cleanup_before_linux_select(int flags)
60{
61 return 0;
62}
63
Simon Glassfc1ebd32018-09-15 00:50:56 -060064/**
65 * is_in_sandbox_mem() - Checks if a pointer is within sandbox's emulated DRAM
66 *
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
71 * DRAM.
72 *
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.
76 *
77 * @ptr: Pointer to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010078 * Return: true if this is within sandbox emulated DRAM, false if not
Simon Glassfc1ebd32018-09-15 00:50:56 -060079 */
80static bool is_in_sandbox_mem(const void *ptr)
81{
82 return (const uint8_t *)ptr >= gd->arch.ram_buf &&
83 (const uint8_t *)ptr < gd->arch.ram_buf + gd->ram_size;
84}
85
86/**
87 * phys_to_virt() - Converts a sandbox RAM address to a pointer
88 *
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
92 * memory.
93 *
94 * If the address is outside this range, it is assumed to be a tag
95 */
Paul Burton51369252017-09-14 15:05:13 -070096void *phys_to_virt(phys_addr_t paddr)
97{
Simon Glassfc1ebd32018-09-15 00:50:56 -060098 struct sandbox_mapmem_entry *mentry;
99 struct sandbox_state *state;
100
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);
104
105 /*
106 * Otherwise search out list of tags for the correct pointer previously
107 * created by map_to_sysmem()
108 */
109 state = state_get_current();
110 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
111 if (mentry->tag == paddr) {
Simon Glass3c2f51e2024-09-01 16:26:24 -0600112 log_debug("Used map from %lx to %p\n", (ulong)paddr,
113 mentry->ptr);
Simon Glassfc1ebd32018-09-15 00:50:56 -0600114 return mentry->ptr;
115 }
116 }
117
118 printf("%s: Cannot map sandbox address %lx (SDRAM from 0 to %lx)\n",
119 __func__, (ulong)paddr, (ulong)gd->ram_size);
120 os_abort();
121
122 /* Not reached */
123 return NULL;
Paul Burton51369252017-09-14 15:05:13 -0700124}
125
Simon Glassfc1ebd32018-09-15 00:50:56 -0600126struct sandbox_mapmem_entry *find_tag(const void *ptr)
Paul Burton51369252017-09-14 15:05:13 -0700127{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600128 struct sandbox_mapmem_entry *mentry;
129 struct sandbox_state *state = state_get_current();
130
131 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
132 if (mentry->ptr == ptr) {
Simon Glass3c2f51e2024-09-01 16:26:24 -0600133 log_debug("Used map from %p to %lx\n", ptr,
134 mentry->tag);
Simon Glassfc1ebd32018-09-15 00:50:56 -0600135 return mentry;
136 }
137 }
Simon Glass3c2f51e2024-09-01 16:26:24 -0600138
Simon Glassfc1ebd32018-09-15 00:50:56 -0600139 return NULL;
Paul Burton51369252017-09-14 15:05:13 -0700140}
141
Simon Glassfc1ebd32018-09-15 00:50:56 -0600142phys_addr_t virt_to_phys(void *ptr)
143{
144 struct sandbox_mapmem_entry *mentry;
145
146 /*
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.
149 */
150 if (is_in_sandbox_mem(ptr))
151 return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf);
152
153 mentry = find_tag(ptr);
154 if (!mentry) {
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);
158 os_abort();
159 }
Simon Glass3c2f51e2024-09-01 16:26:24 -0600160 log_debug("Used map from %p to %lx\n", ptr, mentry->tag);
Simon Glassfc1ebd32018-09-15 00:50:56 -0600161
162 return mentry->tag;
163}
164
Simon Glass3d750d72011-09-26 14:10:39 +0000165void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
166{
Simon Glassf44a3a52016-07-04 11:57:49 -0600167#if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
Simon Glass70778bc2015-03-05 12:25:26 -0700168 unsigned long plen = len;
169 void *ptr;
170
171 map_dev = NULL;
172 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
173 if (plen != len) {
174 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
Mario Six61efece2018-02-12 08:05:57 +0100175 __func__, (uint)paddr, len, plen);
Simon Glass70778bc2015-03-05 12:25:26 -0700176 }
177 map_len = len;
Simon Glass3c2f51e2024-09-01 16:26:24 -0600178 log_debug("pci map %lx -> %p\n", (ulong)paddr, ptr);
Simon Glass70778bc2015-03-05 12:25:26 -0700179 return ptr;
180 }
181#endif
182
Paul Burton51369252017-09-14 15:05:13 -0700183 return phys_to_virt(paddr);
Simon Glass3d750d72011-09-26 14:10:39 +0000184}
185
Simon Glassfc1ebd32018-09-15 00:50:56 -0600186void unmap_physmem(const void *ptr, unsigned long flags)
Simon Glass70778bc2015-03-05 12:25:26 -0700187{
Simon Glass58b7aee2024-09-01 16:26:25 -0600188 struct sandbox_mapmem_entry *mentry;
189
Simon Glass70778bc2015-03-05 12:25:26 -0700190#ifdef CONFIG_PCI
191 if (map_dev) {
Simon Glassfc1ebd32018-09-15 00:50:56 -0600192 pci_unmap_physmem(ptr, map_len, map_dev);
Simon Glass70778bc2015-03-05 12:25:26 -0700193 map_dev = NULL;
194 }
195#endif
Simon Glass58b7aee2024-09-01 16:26:25 -0600196
197 /* If it is in emulated RAM, we didn't create a tag, so nothing to do */
198 if (is_in_sandbox_mem(ptr))
199 return;
200
201 mentry = find_tag(ptr);
202 if (mentry) {
203 list_del(&mentry->sibling_node);
204 log_debug("Removed map from %p to %lx\n", ptr,
205 (ulong)mentry->tag);
206 free(mentry);
207 } else {
208 log_warning("Address not mapped: %p\n", ptr);
209 }
Simon Glass70778bc2015-03-05 12:25:26 -0700210}
211
Simon Glassfc1ebd32018-09-15 00:50:56 -0600212phys_addr_t map_to_sysmem(const void *ptr)
Simon Glass70778bc2015-03-05 12:25:26 -0700213{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600214 struct sandbox_mapmem_entry *mentry;
215
216 /*
217 * If it is in emulated RAM, don't bother creating a tag. Just return
218 * the offset into the RAM buffer.
219 */
220 if (is_in_sandbox_mem(ptr))
221 return (u8 *)ptr - gd->arch.ram_buf;
222
223 /*
224 * See if there is an existing tag with this pointer. If not, set up a
225 * new one.
226 */
227 mentry = find_tag(ptr);
228 if (!mentry) {
229 struct sandbox_state *state = state_get_current();
230
231 mentry = malloc(sizeof(*mentry));
232 if (!mentry) {
233 printf("%s: Error: Out of memory\n", __func__);
234 os_exit(ENOMEM);
235 }
236 mentry->tag = state->next_tag++;
237 mentry->ptr = (void *)ptr;
238 list_add_tail(&mentry->sibling_node, &state->mapmem_head);
Simon Glass3c2f51e2024-09-01 16:26:24 -0600239 log_debug("Added map from %p to %lx\n", ptr,
240 (ulong)mentry->tag);
Simon Glassfc1ebd32018-09-15 00:50:56 -0600241 }
242
243 /*
244 * Return the tag as the address to use. A later call to map_sysmem()
245 * will return ptr
246 */
247 return mentry->tag;
Simon Glass70778bc2015-03-05 12:25:26 -0700248}
249
Abdellatif El Khlifi32f72952023-04-17 10:11:54 +0100250unsigned long sandbox_read(const void *addr, enum sandboxio_size_t size)
Simon Glass0a341a72019-09-25 08:56:09 -0600251{
252 struct sandbox_state *state = state_get_current();
253
254 if (!state->allow_memio)
255 return 0;
256
257 switch (size) {
258 case SB_SIZE_8:
259 return *(u8 *)addr;
260 case SB_SIZE_16:
261 return *(u16 *)addr;
262 case SB_SIZE_32:
263 return *(u32 *)addr;
264 case SB_SIZE_64:
265 return *(u64 *)addr;
266 }
267
268 return 0;
269}
270
Simon Glassd80cda22019-10-11 16:16:47 -0600271void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size)
Simon Glass0a341a72019-09-25 08:56:09 -0600272{
273 struct sandbox_state *state = state_get_current();
274
275 if (!state->allow_memio)
276 return;
277
278 switch (size) {
279 case SB_SIZE_8:
280 *(u8 *)addr = val;
281 break;
282 case SB_SIZE_16:
283 *(u16 *)addr = val;
284 break;
285 case SB_SIZE_32:
286 *(u32 *)addr = val;
287 break;
288 case SB_SIZE_64:
289 *(u64 *)addr = val;
290 break;
291 }
292}
293
294void sandbox_set_enable_memio(bool enable)
295{
296 struct sandbox_state *state = state_get_current();
297
298 state->allow_memio = enable;
299}
300
Simon Glassfc1ebd32018-09-15 00:50:56 -0600301void sandbox_set_enable_pci_map(int enable)
Simon Glasse23d2932013-04-20 08:42:37 +0000302{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600303 enable_pci_map = enable;
Simon Glasse23d2932013-04-20 08:42:37 +0000304}
305
Simon Glass08412872023-12-15 20:14:11 -0700306void dcache_enable(void)
307{
308}
309
310void dcache_disable(void)
311{
312}
313
Emanuele Ghidoli2d2ae362023-05-30 15:33:26 +0200314int dcache_status(void)
315{
316 return 1;
317}
318
Simon Glass3d750d72011-09-26 14:10:39 +0000319void flush_dcache_range(unsigned long start, unsigned long stop)
320{
321}
Simon Glass26b78b22015-02-27 22:06:34 -0700322
Bin Meng843c2992017-08-22 08:15:18 -0700323void invalidate_dcache_range(unsigned long start, unsigned long stop)
324{
325}
326
Simon Glass9a3adfa2022-04-27 13:47:57 -0600327/**
328 * setup_auto_tree() - Set up a basic device tree to allow sandbox to work
329 *
330 * This is used when no device tree is provided. It creates a simple tree with
331 * just a /binman node.
332 *
333 * @blob: Place to put the created device tree
334 * Returns: 0 on success, -ve FDT error code on failure
335 */
336static int setup_auto_tree(void *blob)
337{
338 int err;
339
340 err = fdt_create_empty_tree(blob, 256);
341 if (err)
342 return err;
343
344 /* Create a /binman node in case CONFIG_BINMAN is enabled */
345 err = fdt_add_subnode(blob, 0, "binman");
346 if (err < 0)
347 return err;
348
349 return 0;
350}
351
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300352void *board_fdt_blob_setup(int *ret)
Simon Glass26b78b22015-02-27 22:06:34 -0700353{
354 struct sandbox_state *state = state_get_current();
355 const char *fname = state->fdt_fname;
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300356 void *blob = NULL;
Simon Glass26b78b22015-02-27 22:06:34 -0700357 loff_t size;
358 int err;
359 int fd;
360
Simon Glass0900c792024-08-07 16:47:23 -0600361 if (gd->fdt_blob)
362 return (void *)gd->fdt_blob;
Simon Glass26b78b22015-02-27 22:06:34 -0700363 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300364 *ret = 0;
Simon Glass26b78b22015-02-27 22:06:34 -0700365 if (!state->fdt_fname) {
Simon Glass9a3adfa2022-04-27 13:47:57 -0600366 err = setup_auto_tree(blob);
Simon Glass26b78b22015-02-27 22:06:34 -0700367 if (!err)
368 goto done;
Heinrich Schuchardt6fd60e92022-04-04 22:45:04 +0200369 os_printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300370 *ret = -EINVAL;
371 goto fail;
Simon Glass26b78b22015-02-27 22:06:34 -0700372 }
373
374 err = os_get_filesize(fname, &size);
375 if (err < 0) {
Heinrich Schuchardt6fd60e92022-04-04 22:45:04 +0200376 os_printf("Failed to find FDT file '%s'\n", fname);
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300377 *ret = err;
378 goto fail;
Simon Glass26b78b22015-02-27 22:06:34 -0700379 }
380 fd = os_open(fname, OS_O_RDONLY);
381 if (fd < 0) {
Heinrich Schuchardt6fd60e92022-04-04 22:45:04 +0200382 os_printf("Failed to open FDT file '%s'\n", fname);
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300383 *ret = -EACCES;
384 goto fail;
Simon Glass26b78b22015-02-27 22:06:34 -0700385 }
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300386
Simon Glass26b78b22015-02-27 22:06:34 -0700387 if (os_read(fd, blob, size) != size) {
388 os_close(fd);
Heinrich Schuchardt6fd60e92022-04-04 22:45:04 +0200389 os_printf("Failed to read FDT file '%s'\n", fname);
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300390 *ret = -EIO;
391 goto fail;
Simon Glass26b78b22015-02-27 22:06:34 -0700392 }
393 os_close(fd);
394
395done:
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300396 return blob;
397fail:
398 return NULL;
Simon Glass26b78b22015-02-27 22:06:34 -0700399}
Simon Glass38e79742017-05-22 05:05:23 -0600400
401ulong timer_get_boot_us(void)
402{
403 static uint64_t base_count;
404 uint64_t count = os_get_nsec();
405
406 if (!base_count)
407 base_count = count;
408
409 return (count - base_count) / 1000;
410}
Simon Glass57b00a92022-09-06 20:27:10 -0600411
412int sandbox_load_other_fdt(void **fdtp, int *sizep)
413{
414 const char *orig;
415 int ret, size;
416 void *fdt = *fdtp;
417
418 ret = state_load_other_fdt(&orig, &size);
419 if (ret) {
420 log_err("Cannot read other FDT\n");
421 return log_msg_ret("ld", ret);
422 }
423
424 if (!*fdtp) {
425 fdt = os_malloc(size);
426 if (!fdt)
427 return log_msg_ret("mem", -ENOMEM);
428 *sizep = size;
429 }
430
431 memcpy(fdt, orig, *sizep);
432 *fdtp = fdt;
433
434 return 0;
435}