blob: 2046cb53c4ae6cc4008d29e5e23371996d2a0e40 [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 Glass11c89f32017-05-17 17:18:03 -06007#include <dm.h>
Simon Glass141e5a02016-07-04 11:57:48 -06008#include <errno.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +09009#include <linux/libfdt.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000010#include <os.h>
Simon Glass26b78b22015-02-27 22:06:34 -070011#include <asm/io.h>
Simon Glass5dccd152018-05-16 09:42:22 -060012#include <asm/setjmp.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070013#include <asm/state.h>
Simon Glassbe310372016-07-04 11:57:47 -060014#include <dm/root.h>
Simon Glass3d750d72011-09-26 14:10:39 +000015
16DECLARE_GLOBAL_DATA_PTR;
17
Simon Glass70778bc2015-03-05 12:25:26 -070018/* Enable access to PCI memory with map_sysmem() */
19static bool enable_pci_map;
20
21#ifdef CONFIG_PCI
22/* Last device that was mapped into memory, and length of mapping */
23static struct udevice *map_dev;
24unsigned long map_len;
25#endif
26
Simon Glassd860f222015-07-06 12:54:29 -060027void sandbox_exit(void)
Simon Glass3d750d72011-09-26 14:10:39 +000028{
Simon Glass9c3b7d62015-05-10 21:07:27 -060029 /* Do this here while it still has an effect */
30 os_fd_restore();
Simon Glass9dd10bf2013-11-10 10:27:03 -070031 if (state_uninit())
32 os_exit(2);
33
Simon Glass19200512014-07-23 06:55:02 -060034 if (dm_uninit())
35 os_exit(2);
36
Simon Glasscd0684f2011-10-03 19:26:44 +000037 /* This is considered normal termination for now */
38 os_exit(0);
Simon Glassdc9f8cd2013-11-10 10:27:00 -070039}
40
Simon Glass3d750d72011-09-26 14:10:39 +000041/* delay x useconds */
42void __udelay(unsigned long usec)
43{
Simon Glassb25fa312015-11-08 23:47:43 -070044 struct sandbox_state *state = state_get_current();
45
46 if (!state->skip_delays)
47 os_usleep(usec);
Simon Glass3d750d72011-09-26 14:10:39 +000048}
49
Simon Glass3d750d72011-09-26 14:10:39 +000050int cleanup_before_linux(void)
51{
52 return 0;
53}
54
Simon Glass770d93f2015-05-13 07:02:26 -060055int cleanup_before_linux_select(int flags)
56{
57 return 0;
58}
59
Simon Glassfc1ebd32018-09-15 00:50:56 -060060/**
61 * is_in_sandbox_mem() - Checks if a pointer is within sandbox's emulated DRAM
62 *
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
67 * DRAM.
68 *
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.
72 *
73 * @ptr: Pointer to check
74 * @return true if this is within sandbox emulated DRAM, false if not
75 */
76static bool is_in_sandbox_mem(const void *ptr)
77{
78 return (const uint8_t *)ptr >= gd->arch.ram_buf &&
79 (const uint8_t *)ptr < gd->arch.ram_buf + gd->ram_size;
80}
81
82/**
83 * phys_to_virt() - Converts a sandbox RAM address to a pointer
84 *
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
88 * memory.
89 *
90 * If the address is outside this range, it is assumed to be a tag
91 */
Paul Burton51369252017-09-14 15:05:13 -070092void *phys_to_virt(phys_addr_t paddr)
93{
Simon Glassfc1ebd32018-09-15 00:50:56 -060094 struct sandbox_mapmem_entry *mentry;
95 struct sandbox_state *state;
96
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);
100
101 /*
102 * Otherwise search out list of tags for the correct pointer previously
103 * created by map_to_sysmem()
104 */
105 state = state_get_current();
106 list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
107 if (mentry->tag == paddr) {
Heinrich Schuchardtfa4bf412018-10-14 20:45:32 +0200108 debug("%s: Used map from %lx to %p\n", __func__,
109 (ulong)paddr, mentry->ptr);
Simon Glassfc1ebd32018-09-15 00:50:56 -0600110 return mentry->ptr;
111 }
112 }
113
114 printf("%s: Cannot map sandbox address %lx (SDRAM from 0 to %lx)\n",
115 __func__, (ulong)paddr, (ulong)gd->ram_size);
116 os_abort();
117
118 /* Not reached */
119 return NULL;
Paul Burton51369252017-09-14 15:05:13 -0700120}
121
Simon Glassfc1ebd32018-09-15 00:50:56 -0600122struct sandbox_mapmem_entry *find_tag(const void *ptr)
Paul Burton51369252017-09-14 15:05:13 -0700123{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600124 struct sandbox_mapmem_entry *mentry;
125 struct sandbox_state *state = state_get_current();
126
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,
130 mentry->tag);
131 return mentry;
132 }
133 }
134 return NULL;
Paul Burton51369252017-09-14 15:05:13 -0700135}
136
Simon Glassfc1ebd32018-09-15 00:50:56 -0600137phys_addr_t virt_to_phys(void *ptr)
138{
139 struct sandbox_mapmem_entry *mentry;
140
141 /*
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.
144 */
145 if (is_in_sandbox_mem(ptr))
146 return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf);
147
148 mentry = find_tag(ptr);
149 if (!mentry) {
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);
153 os_abort();
154 }
Heinrich Schuchardtfa4bf412018-10-14 20:45:32 +0200155 debug("%s: Used map from %p to %lx\n", __func__, ptr, mentry->tag);
Simon Glassfc1ebd32018-09-15 00:50:56 -0600156
157 return mentry->tag;
158}
159
Simon Glass3d750d72011-09-26 14:10:39 +0000160void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
161{
Simon Glassf44a3a52016-07-04 11:57:49 -0600162#if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
Simon Glass70778bc2015-03-05 12:25:26 -0700163 unsigned long plen = len;
164 void *ptr;
165
166 map_dev = NULL;
167 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
168 if (plen != len) {
169 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
Mario Six61efece2018-02-12 08:05:57 +0100170 __func__, (uint)paddr, len, plen);
Simon Glass70778bc2015-03-05 12:25:26 -0700171 }
172 map_len = len;
173 return ptr;
174 }
175#endif
176
Paul Burton51369252017-09-14 15:05:13 -0700177 return phys_to_virt(paddr);
Simon Glass3d750d72011-09-26 14:10:39 +0000178}
179
Simon Glassfc1ebd32018-09-15 00:50:56 -0600180void unmap_physmem(const void *ptr, unsigned long flags)
Simon Glass70778bc2015-03-05 12:25:26 -0700181{
182#ifdef CONFIG_PCI
183 if (map_dev) {
Simon Glassfc1ebd32018-09-15 00:50:56 -0600184 pci_unmap_physmem(ptr, map_len, map_dev);
Simon Glass70778bc2015-03-05 12:25:26 -0700185 map_dev = NULL;
186 }
187#endif
188}
189
Simon Glassfc1ebd32018-09-15 00:50:56 -0600190phys_addr_t map_to_sysmem(const void *ptr)
Simon Glass70778bc2015-03-05 12:25:26 -0700191{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600192 struct sandbox_mapmem_entry *mentry;
193
194 /*
195 * If it is in emulated RAM, don't bother creating a tag. Just return
196 * the offset into the RAM buffer.
197 */
198 if (is_in_sandbox_mem(ptr))
199 return (u8 *)ptr - gd->arch.ram_buf;
200
201 /*
202 * See if there is an existing tag with this pointer. If not, set up a
203 * new one.
204 */
205 mentry = find_tag(ptr);
206 if (!mentry) {
207 struct sandbox_state *state = state_get_current();
208
209 mentry = malloc(sizeof(*mentry));
210 if (!mentry) {
211 printf("%s: Error: Out of memory\n", __func__);
212 os_exit(ENOMEM);
213 }
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,
218 (ulong)mentry->tag);
219 }
220
221 /*
222 * Return the tag as the address to use. A later call to map_sysmem()
223 * will return ptr
224 */
225 return mentry->tag;
Simon Glass70778bc2015-03-05 12:25:26 -0700226}
227
Simon Glass0a341a72019-09-25 08:56:09 -0600228unsigned int sandbox_read(const void *addr, enum sandboxio_size_t size)
229{
230 struct sandbox_state *state = state_get_current();
231
232 if (!state->allow_memio)
233 return 0;
234
235 switch (size) {
236 case SB_SIZE_8:
237 return *(u8 *)addr;
238 case SB_SIZE_16:
239 return *(u16 *)addr;
240 case SB_SIZE_32:
241 return *(u32 *)addr;
242 case SB_SIZE_64:
243 return *(u64 *)addr;
244 }
245
246 return 0;
247}
248
249void sandbox_write(const void *addr, unsigned int val,
250 enum sandboxio_size_t size)
251{
252 struct sandbox_state *state = state_get_current();
253
254 if (!state->allow_memio)
255 return;
256
257 switch (size) {
258 case SB_SIZE_8:
259 *(u8 *)addr = val;
260 break;
261 case SB_SIZE_16:
262 *(u16 *)addr = val;
263 break;
264 case SB_SIZE_32:
265 *(u32 *)addr = val;
266 break;
267 case SB_SIZE_64:
268 *(u64 *)addr = val;
269 break;
270 }
271}
272
273void sandbox_set_enable_memio(bool enable)
274{
275 struct sandbox_state *state = state_get_current();
276
277 state->allow_memio = enable;
278}
279
Simon Glassfc1ebd32018-09-15 00:50:56 -0600280void sandbox_set_enable_pci_map(int enable)
Simon Glasse23d2932013-04-20 08:42:37 +0000281{
Simon Glassfc1ebd32018-09-15 00:50:56 -0600282 enable_pci_map = enable;
Simon Glasse23d2932013-04-20 08:42:37 +0000283}
284
Simon Glass3d750d72011-09-26 14:10:39 +0000285void flush_dcache_range(unsigned long start, unsigned long stop)
286{
287}
Simon Glass26b78b22015-02-27 22:06:34 -0700288
Bin Meng843c2992017-08-22 08:15:18 -0700289void invalidate_dcache_range(unsigned long start, unsigned long stop)
290{
291}
292
Simon Glass26b78b22015-02-27 22:06:34 -0700293int sandbox_read_fdt_from_file(void)
294{
295 struct sandbox_state *state = state_get_current();
296 const char *fname = state->fdt_fname;
297 void *blob;
298 loff_t size;
299 int err;
300 int fd;
301
302 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
303 if (!state->fdt_fname) {
304 err = fdt_create_empty_tree(blob, 256);
305 if (!err)
306 goto done;
307 printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
308 return -EINVAL;
309 }
310
311 err = os_get_filesize(fname, &size);
312 if (err < 0) {
313 printf("Failed to file FDT file '%s'\n", fname);
314 return err;
315 }
316 fd = os_open(fname, OS_O_RDONLY);
317 if (fd < 0) {
318 printf("Failed to open FDT file '%s'\n", fname);
319 return -EACCES;
320 }
321 if (os_read(fd, blob, size) != size) {
322 os_close(fd);
323 return -EIO;
324 }
325 os_close(fd);
326
327done:
328 gd->fdt_blob = blob;
329
330 return 0;
331}
Simon Glass38e79742017-05-22 05:05:23 -0600332
333ulong timer_get_boot_us(void)
334{
335 static uint64_t base_count;
336 uint64_t count = os_get_nsec();
337
338 if (!base_count)
339 base_count = count;
340
341 return (count - base_count) / 1000;
342}