blob: 842640c50edfe5ccb6941047f3296ce6ffa3c37c [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 Glass3d750d72011-09-26 14:10:39 +00006#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -06007#include <bootstage.h>
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glass11c89f32017-05-17 17:18:03 -06009#include <dm.h>
Simon Glass141e5a02016-07-04 11:57:48 -060010#include <errno.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000012#include <os.h>
Simon Glass26b78b22015-02-27 22:06:34 -070013#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <asm/malloc.h>
Simon Glass5dccd152018-05-16 09:42:22 -060015#include <asm/setjmp.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070016#include <asm/state.h>
Simon Glassbe310372016-07-04 11:57:47 -060017#include <dm/root.h>
Simon Glass3d750d72011-09-26 14:10:39 +000018
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glass70778bc2015-03-05 12:25:26 -070021/* Enable access to PCI memory with map_sysmem() */
22static bool enable_pci_map;
23
24#ifdef CONFIG_PCI
25/* Last device that was mapped into memory, and length of mapping */
26static struct udevice *map_dev;
27unsigned long map_len;
28#endif
29
Simon Glassd860f222015-07-06 12:54:29 -060030void sandbox_exit(void)
Simon Glass3d750d72011-09-26 14:10:39 +000031{
Simon Glass9c3b7d62015-05-10 21:07:27 -060032 /* Do this here while it still has an effect */
33 os_fd_restore();
Simon Glass9dd10bf2013-11-10 10:27:03 -070034 if (state_uninit())
35 os_exit(2);
36
Simon Glass19200512014-07-23 06:55:02 -060037 if (dm_uninit())
38 os_exit(2);
39
Simon Glasscd0684f2011-10-03 19:26:44 +000040 /* This is considered normal termination for now */
41 os_exit(0);
Simon Glassdc9f8cd2013-11-10 10:27:00 -070042}
43
Simon Glass3d750d72011-09-26 14:10:39 +000044/* delay x useconds */
45void __udelay(unsigned long usec)
46{
Simon Glassb25fa312015-11-08 23:47:43 -070047 struct sandbox_state *state = state_get_current();
48
49 if (!state->skip_delays)
50 os_usleep(usec);
Simon Glass3d750d72011-09-26 14:10:39 +000051}
52
Simon Glass3d750d72011-09-26 14:10:39 +000053int cleanup_before_linux(void)
54{
55 return 0;
56}
57
Simon Glass770d93f2015-05-13 07:02:26 -060058int cleanup_before_linux_select(int flags)
59{
60 return 0;
61}
62
Simon Glassfc1ebd32018-09-15 00:50:56 -060063/**
64 * is_in_sandbox_mem() - Checks if a pointer is within sandbox's emulated DRAM
65 *
66 * This provides a way to check if a pointer is owned by sandbox (and is within
67 * its RAM) or not. Sometimes pointers come from a test which conceptually runs
68 * output sandbox, potentially with direct access to the C-library malloc()
69 * function, or the sandbox stack (which is not actually within the emulated
70 * DRAM.
71 *
72 * Such pointers obviously cannot be mapped into sandbox's DRAM, so we must
73 * detect them an process them separately, by recording a mapping to a tag,
74 * which we can use to map back to the pointer later.
75 *
76 * @ptr: Pointer to check
77 * @return true if this is within sandbox emulated DRAM, false if not
78 */
79static bool is_in_sandbox_mem(const void *ptr)
80{
81 return (const uint8_t *)ptr >= gd->arch.ram_buf &&
82 (const uint8_t *)ptr < gd->arch.ram_buf + gd->ram_size;
83}
84
85/**
86 * phys_to_virt() - Converts a sandbox RAM address to a pointer
87 *
88 * Sandbox uses U-Boot addresses from 0 to the size of DRAM. These index into
89 * the emulated DRAM buffer used by sandbox. This function converts such an
90 * address to a pointer into this buffer, which can be used to access the
91 * memory.
92 *
93 * If the address is outside this range, it is assumed to be a tag
94 */
Paul Burton51369252017-09-14 15:05:13 -070095void *phys_to_virt(phys_addr_t paddr)
96{
Simon Glassfc1ebd32018-09-15 00:50:56 -060097 struct sandbox_mapmem_entry *mentry;
98 struct sandbox_state *state;
99
100 /* If the address is within emulated DRAM, calculate the value */
101 if (paddr < gd->ram_size)
102 return (void *)(gd->arch.ram_buf + paddr);
103
104 /*
105 * Otherwise search out list of tags for the correct pointer previously
106 * created by map_to_sysmem()
107 */
108 state = state_get_current();
109 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
110 if (mentry->tag == paddr) {
Heinrich Schuchardtfa4bf412018-10-14 20:45:32 +0200111 debug("%s: Used map from %lx to %p\n", __func__,
112 (ulong)paddr, mentry->ptr);
Simon Glassfc1ebd32018-09-15 00:50:56 -0600113 return mentry->ptr;
114 }
115 }
116
117 printf("%s: Cannot map sandbox address %lx (SDRAM from 0 to %lx)\n",
118 __func__, (ulong)paddr, (ulong)gd->ram_size);
119 os_abort();
120
121 /* Not reached */
122 return NULL;
Paul Burton51369252017-09-14 15:05:13 -0700123}
124
Simon Glassfc1ebd32018-09-15 00:50:56 -0600125struct sandbox_mapmem_entry *find_tag(const void *ptr)
Paul Burton51369252017-09-14 15:05:13 -0700126{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600127 struct sandbox_mapmem_entry *mentry;
128 struct sandbox_state *state = state_get_current();
129
130 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
131 if (mentry->ptr == ptr) {
132 debug("%s: Used map from %p to %lx\n", __func__, ptr,
133 mentry->tag);
134 return mentry;
135 }
136 }
137 return NULL;
Paul Burton51369252017-09-14 15:05:13 -0700138}
139
Simon Glassfc1ebd32018-09-15 00:50:56 -0600140phys_addr_t virt_to_phys(void *ptr)
141{
142 struct sandbox_mapmem_entry *mentry;
143
144 /*
145 * If it is in emulated RAM, don't bother looking for a tag. Just
146 * calculate the pointer using the provides offset into the RAM buffer.
147 */
148 if (is_in_sandbox_mem(ptr))
149 return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf);
150
151 mentry = find_tag(ptr);
152 if (!mentry) {
153 /* Abort so that gdb can be used here */
154 printf("%s: Cannot map sandbox address %p (SDRAM from 0 to %lx)\n",
155 __func__, ptr, (ulong)gd->ram_size);
156 os_abort();
157 }
Heinrich Schuchardtfa4bf412018-10-14 20:45:32 +0200158 debug("%s: Used map from %p to %lx\n", __func__, ptr, mentry->tag);
Simon Glassfc1ebd32018-09-15 00:50:56 -0600159
160 return mentry->tag;
161}
162
Simon Glass3d750d72011-09-26 14:10:39 +0000163void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
164{
Simon Glassf44a3a52016-07-04 11:57:49 -0600165#if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
Simon Glass70778bc2015-03-05 12:25:26 -0700166 unsigned long plen = len;
167 void *ptr;
168
169 map_dev = NULL;
170 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
171 if (plen != len) {
172 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
Mario Six61efece2018-02-12 08:05:57 +0100173 __func__, (uint)paddr, len, plen);
Simon Glass70778bc2015-03-05 12:25:26 -0700174 }
175 map_len = len;
176 return ptr;
177 }
178#endif
179
Paul Burton51369252017-09-14 15:05:13 -0700180 return phys_to_virt(paddr);
Simon Glass3d750d72011-09-26 14:10:39 +0000181}
182
Simon Glassfc1ebd32018-09-15 00:50:56 -0600183void unmap_physmem(const void *ptr, unsigned long flags)
Simon Glass70778bc2015-03-05 12:25:26 -0700184{
185#ifdef CONFIG_PCI
186 if (map_dev) {
Simon Glassfc1ebd32018-09-15 00:50:56 -0600187 pci_unmap_physmem(ptr, map_len, map_dev);
Simon Glass70778bc2015-03-05 12:25:26 -0700188 map_dev = NULL;
189 }
190#endif
191}
192
Simon Glassfc1ebd32018-09-15 00:50:56 -0600193phys_addr_t map_to_sysmem(const void *ptr)
Simon Glass70778bc2015-03-05 12:25:26 -0700194{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600195 struct sandbox_mapmem_entry *mentry;
196
197 /*
198 * If it is in emulated RAM, don't bother creating a tag. Just return
199 * the offset into the RAM buffer.
200 */
201 if (is_in_sandbox_mem(ptr))
202 return (u8 *)ptr - gd->arch.ram_buf;
203
204 /*
205 * See if there is an existing tag with this pointer. If not, set up a
206 * new one.
207 */
208 mentry = find_tag(ptr);
209 if (!mentry) {
210 struct sandbox_state *state = state_get_current();
211
212 mentry = malloc(sizeof(*mentry));
213 if (!mentry) {
214 printf("%s: Error: Out of memory\n", __func__);
215 os_exit(ENOMEM);
216 }
217 mentry->tag = state->next_tag++;
218 mentry->ptr = (void *)ptr;
219 list_add_tail(&mentry->sibling_node, &state->mapmem_head);
220 debug("%s: Added map from %p to %lx\n", __func__, ptr,
221 (ulong)mentry->tag);
222 }
223
224 /*
225 * Return the tag as the address to use. A later call to map_sysmem()
226 * will return ptr
227 */
228 return mentry->tag;
Simon Glass70778bc2015-03-05 12:25:26 -0700229}
230
Simon Glass0a341a72019-09-25 08:56:09 -0600231unsigned int sandbox_read(const void *addr, enum sandboxio_size_t size)
232{
233 struct sandbox_state *state = state_get_current();
234
235 if (!state->allow_memio)
236 return 0;
237
238 switch (size) {
239 case SB_SIZE_8:
240 return *(u8 *)addr;
241 case SB_SIZE_16:
242 return *(u16 *)addr;
243 case SB_SIZE_32:
244 return *(u32 *)addr;
245 case SB_SIZE_64:
246 return *(u64 *)addr;
247 }
248
249 return 0;
250}
251
Simon Glassd80cda22019-10-11 16:16:47 -0600252void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size)
Simon Glass0a341a72019-09-25 08:56:09 -0600253{
254 struct sandbox_state *state = state_get_current();
255
256 if (!state->allow_memio)
257 return;
258
259 switch (size) {
260 case SB_SIZE_8:
261 *(u8 *)addr = val;
262 break;
263 case SB_SIZE_16:
264 *(u16 *)addr = val;
265 break;
266 case SB_SIZE_32:
267 *(u32 *)addr = val;
268 break;
269 case SB_SIZE_64:
270 *(u64 *)addr = val;
271 break;
272 }
273}
274
275void sandbox_set_enable_memio(bool enable)
276{
277 struct sandbox_state *state = state_get_current();
278
279 state->allow_memio = enable;
280}
281
Simon Glassfc1ebd32018-09-15 00:50:56 -0600282void sandbox_set_enable_pci_map(int enable)
Simon Glasse23d2932013-04-20 08:42:37 +0000283{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600284 enable_pci_map = enable;
Simon Glasse23d2932013-04-20 08:42:37 +0000285}
286
Simon Glass3d750d72011-09-26 14:10:39 +0000287void flush_dcache_range(unsigned long start, unsigned long stop)
288{
289}
Simon Glass26b78b22015-02-27 22:06:34 -0700290
Bin Meng843c2992017-08-22 08:15:18 -0700291void invalidate_dcache_range(unsigned long start, unsigned long stop)
292{
293}
294
Simon Glass26b78b22015-02-27 22:06:34 -0700295int sandbox_read_fdt_from_file(void)
296{
297 struct sandbox_state *state = state_get_current();
298 const char *fname = state->fdt_fname;
299 void *blob;
300 loff_t size;
301 int err;
302 int fd;
303
304 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
305 if (!state->fdt_fname) {
306 err = fdt_create_empty_tree(blob, 256);
307 if (!err)
308 goto done;
309 printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
310 return -EINVAL;
311 }
312
313 err = os_get_filesize(fname, &size);
314 if (err < 0) {
315 printf("Failed to file FDT file '%s'\n", fname);
316 return err;
317 }
318 fd = os_open(fname, OS_O_RDONLY);
319 if (fd < 0) {
320 printf("Failed to open FDT file '%s'\n", fname);
321 return -EACCES;
322 }
323 if (os_read(fd, blob, size) != size) {
324 os_close(fd);
325 return -EIO;
326 }
327 os_close(fd);
328
329done:
330 gd->fdt_blob = blob;
331
332 return 0;
333}
Simon Glass38e79742017-05-22 05:05:23 -0600334
335ulong timer_get_boot_us(void)
336{
337 static uint64_t base_count;
338 uint64_t count = os_get_nsec();
339
340 if (!base_count)
341 base_count = count;
342
343 return (count - base_count) / 1000;
344}