blob: 0d9300736d46d230da74fd2068479b157de050b4 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000013#include <os.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 Glassbe310372016-07-04 11:57:47 -060018#include <dm/root.h>
Simon Glass3d750d72011-09-26 14:10:39 +000019
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass70778bc2015-03-05 12:25:26 -070022/* Enable access to PCI memory with map_sysmem() */
23static bool enable_pci_map;
24
25#ifdef CONFIG_PCI
26/* Last device that was mapped into memory, and length of mapping */
27static struct udevice *map_dev;
28unsigned long map_len;
29#endif
30
Simon Glassd860f222015-07-06 12:54:29 -060031void sandbox_exit(void)
Simon Glass3d750d72011-09-26 14:10:39 +000032{
Simon Glass9c3b7d62015-05-10 21:07:27 -060033 /* Do this here while it still has an effect */
34 os_fd_restore();
Simon Glass9dd10bf2013-11-10 10:27:03 -070035 if (state_uninit())
36 os_exit(2);
37
Simon Glass19200512014-07-23 06:55:02 -060038 if (dm_uninit())
39 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
78 * @return true if this is within sandbox emulated DRAM, false if not
79 */
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) {
Heinrich Schuchardtfa4bf412018-10-14 20:45:32 +0200112 debug("%s: Used map from %lx to %p\n", __func__,
113 (ulong)paddr, 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) {
133 debug("%s: Used map from %p to %lx\n", __func__, ptr,
134 mentry->tag);
135 return mentry;
136 }
137 }
138 return NULL;
Paul Burton51369252017-09-14 15:05:13 -0700139}
140
Simon Glassfc1ebd32018-09-15 00:50:56 -0600141phys_addr_t virt_to_phys(void *ptr)
142{
143 struct sandbox_mapmem_entry *mentry;
144
145 /*
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.
148 */
149 if (is_in_sandbox_mem(ptr))
150 return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf);
151
152 mentry = find_tag(ptr);
153 if (!mentry) {
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);
157 os_abort();
158 }
Heinrich Schuchardtfa4bf412018-10-14 20:45:32 +0200159 debug("%s: Used map from %p to %lx\n", __func__, ptr, mentry->tag);
Simon Glassfc1ebd32018-09-15 00:50:56 -0600160
161 return mentry->tag;
162}
163
Simon Glass3d750d72011-09-26 14:10:39 +0000164void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
165{
Simon Glassf44a3a52016-07-04 11:57:49 -0600166#if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
Simon Glass70778bc2015-03-05 12:25:26 -0700167 unsigned long plen = len;
168 void *ptr;
169
170 map_dev = NULL;
171 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
172 if (plen != len) {
173 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
Mario Six61efece2018-02-12 08:05:57 +0100174 __func__, (uint)paddr, len, plen);
Simon Glass70778bc2015-03-05 12:25:26 -0700175 }
176 map_len = len;
177 return ptr;
178 }
179#endif
180
Paul Burton51369252017-09-14 15:05:13 -0700181 return phys_to_virt(paddr);
Simon Glass3d750d72011-09-26 14:10:39 +0000182}
183
Simon Glassfc1ebd32018-09-15 00:50:56 -0600184void unmap_physmem(const void *ptr, unsigned long flags)
Simon Glass70778bc2015-03-05 12:25:26 -0700185{
186#ifdef CONFIG_PCI
187 if (map_dev) {
Simon Glassfc1ebd32018-09-15 00:50:56 -0600188 pci_unmap_physmem(ptr, map_len, map_dev);
Simon Glass70778bc2015-03-05 12:25:26 -0700189 map_dev = NULL;
190 }
191#endif
192}
193
Simon Glassfc1ebd32018-09-15 00:50:56 -0600194phys_addr_t map_to_sysmem(const void *ptr)
Simon Glass70778bc2015-03-05 12:25:26 -0700195{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600196 struct sandbox_mapmem_entry *mentry;
197
198 /*
199 * If it is in emulated RAM, don't bother creating a tag. Just return
200 * the offset into the RAM buffer.
201 */
202 if (is_in_sandbox_mem(ptr))
203 return (u8 *)ptr - gd->arch.ram_buf;
204
205 /*
206 * See if there is an existing tag with this pointer. If not, set up a
207 * new one.
208 */
209 mentry = find_tag(ptr);
210 if (!mentry) {
211 struct sandbox_state *state = state_get_current();
212
213 mentry = malloc(sizeof(*mentry));
214 if (!mentry) {
215 printf("%s: Error: Out of memory\n", __func__);
216 os_exit(ENOMEM);
217 }
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,
222 (ulong)mentry->tag);
223 }
224
225 /*
226 * Return the tag as the address to use. A later call to map_sysmem()
227 * will return ptr
228 */
229 return mentry->tag;
Simon Glass70778bc2015-03-05 12:25:26 -0700230}
231
Simon Glass0a341a72019-09-25 08:56:09 -0600232unsigned int sandbox_read(const void *addr, enum sandboxio_size_t size)
233{
234 struct sandbox_state *state = state_get_current();
235
236 if (!state->allow_memio)
237 return 0;
238
239 switch (size) {
240 case SB_SIZE_8:
241 return *(u8 *)addr;
242 case SB_SIZE_16:
243 return *(u16 *)addr;
244 case SB_SIZE_32:
245 return *(u32 *)addr;
246 case SB_SIZE_64:
247 return *(u64 *)addr;
248 }
249
250 return 0;
251}
252
Simon Glassd80cda22019-10-11 16:16:47 -0600253void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size)
Simon Glass0a341a72019-09-25 08:56:09 -0600254{
255 struct sandbox_state *state = state_get_current();
256
257 if (!state->allow_memio)
258 return;
259
260 switch (size) {
261 case SB_SIZE_8:
262 *(u8 *)addr = val;
263 break;
264 case SB_SIZE_16:
265 *(u16 *)addr = val;
266 break;
267 case SB_SIZE_32:
268 *(u32 *)addr = val;
269 break;
270 case SB_SIZE_64:
271 *(u64 *)addr = val;
272 break;
273 }
274}
275
276void sandbox_set_enable_memio(bool enable)
277{
278 struct sandbox_state *state = state_get_current();
279
280 state->allow_memio = enable;
281}
282
Simon Glassfc1ebd32018-09-15 00:50:56 -0600283void sandbox_set_enable_pci_map(int enable)
Simon Glasse23d2932013-04-20 08:42:37 +0000284{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600285 enable_pci_map = enable;
Simon Glasse23d2932013-04-20 08:42:37 +0000286}
287
Simon Glass3d750d72011-09-26 14:10:39 +0000288void flush_dcache_range(unsigned long start, unsigned long stop)
289{
290}
Simon Glass26b78b22015-02-27 22:06:34 -0700291
Bin Meng843c2992017-08-22 08:15:18 -0700292void invalidate_dcache_range(unsigned long start, unsigned long stop)
293{
294}
295
Simon Glass26b78b22015-02-27 22:06:34 -0700296int sandbox_read_fdt_from_file(void)
297{
298 struct sandbox_state *state = state_get_current();
299 const char *fname = state->fdt_fname;
300 void *blob;
301 loff_t size;
302 int err;
303 int fd;
304
305 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
306 if (!state->fdt_fname) {
307 err = fdt_create_empty_tree(blob, 256);
308 if (!err)
309 goto done;
310 printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
311 return -EINVAL;
312 }
313
314 err = os_get_filesize(fname, &size);
315 if (err < 0) {
316 printf("Failed to file FDT file '%s'\n", fname);
317 return err;
318 }
319 fd = os_open(fname, OS_O_RDONLY);
320 if (fd < 0) {
321 printf("Failed to open FDT file '%s'\n", fname);
322 return -EACCES;
323 }
324 if (os_read(fd, blob, size) != size) {
325 os_close(fd);
326 return -EIO;
327 }
328 os_close(fd);
329
330done:
331 gd->fdt_blob = blob;
332
333 return 0;
334}
Simon Glass38e79742017-05-22 05:05:23 -0600335
336ulong timer_get_boot_us(void)
337{
338 static uint64_t base_count;
339 uint64_t count = os_get_nsec();
340
341 if (!base_count)
342 base_count = count;
343
344 return (count - base_count) / 1000;
345}