2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2011-09-26 14:10:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 The Chromium OS Authors.
|
|
|
|
*/
|
2015-03-05 19:25:26 +00:00
|
|
|
#define DEBUG
|
2011-09-26 14:10:39 +00:00
|
|
|
#include <common.h>
|
2017-05-17 23:18:03 +00:00
|
|
|
#include <dm.h>
|
2016-07-04 17:57:48 +00:00
|
|
|
#include <errno.h>
|
2018-03-04 16:20:11 +00:00
|
|
|
#include <linux/libfdt.h>
|
2011-10-03 19:26:44 +00:00
|
|
|
#include <os.h>
|
2015-02-28 05:06:34 +00:00
|
|
|
#include <asm/io.h>
|
2018-05-16 15:42:22 +00:00
|
|
|
#include <asm/setjmp.h>
|
2013-11-10 17:27:03 +00:00
|
|
|
#include <asm/state.h>
|
2016-07-04 17:57:47 +00:00
|
|
|
#include <dm/root.h>
|
2011-09-26 14:10:39 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2015-03-05 19:25:26 +00:00
|
|
|
/* Enable access to PCI memory with map_sysmem() */
|
|
|
|
static bool enable_pci_map;
|
|
|
|
|
|
|
|
#ifdef CONFIG_PCI
|
|
|
|
/* Last device that was mapped into memory, and length of mapping */
|
|
|
|
static struct udevice *map_dev;
|
|
|
|
unsigned long map_len;
|
|
|
|
#endif
|
|
|
|
|
2015-07-06 18:54:29 +00:00
|
|
|
void sandbox_exit(void)
|
2011-09-26 14:10:39 +00:00
|
|
|
{
|
2015-05-11 03:07:27 +00:00
|
|
|
/* Do this here while it still has an effect */
|
|
|
|
os_fd_restore();
|
2013-11-10 17:27:03 +00:00
|
|
|
if (state_uninit())
|
|
|
|
os_exit(2);
|
|
|
|
|
2014-07-23 12:55:02 +00:00
|
|
|
if (dm_uninit())
|
|
|
|
os_exit(2);
|
|
|
|
|
2011-10-03 19:26:44 +00:00
|
|
|
/* This is considered normal termination for now */
|
|
|
|
os_exit(0);
|
2013-11-10 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 14:10:39 +00:00
|
|
|
/* delay x useconds */
|
|
|
|
void __udelay(unsigned long usec)
|
|
|
|
{
|
2015-11-09 06:47:43 +00:00
|
|
|
struct sandbox_state *state = state_get_current();
|
|
|
|
|
|
|
|
if (!state->skip_delays)
|
|
|
|
os_usleep(usec);
|
2011-09-26 14:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cleanup_before_linux(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:02:26 +00:00
|
|
|
int cleanup_before_linux_select(int flags)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:05:13 +00:00
|
|
|
void *phys_to_virt(phys_addr_t paddr)
|
|
|
|
{
|
|
|
|
return (void *)(gd->arch.ram_buf + paddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
phys_addr_t virt_to_phys(void *vaddr)
|
|
|
|
{
|
|
|
|
return (phys_addr_t)((uint8_t *)vaddr - gd->arch.ram_buf);
|
|
|
|
}
|
|
|
|
|
2011-09-26 14:10:39 +00:00
|
|
|
void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
|
|
|
|
{
|
2016-07-04 17:57:49 +00:00
|
|
|
#if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
|
2015-03-05 19:25:26 +00:00
|
|
|
unsigned long plen = len;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
map_dev = NULL;
|
|
|
|
if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
|
|
|
|
if (plen != len) {
|
|
|
|
printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
|
2018-02-12 07:05:57 +00:00
|
|
|
__func__, (uint)paddr, len, plen);
|
2015-03-05 19:25:26 +00:00
|
|
|
}
|
|
|
|
map_len = len;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-14 22:05:13 +00:00
|
|
|
return phys_to_virt(paddr);
|
2011-09-26 14:10:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 19:25:26 +00:00
|
|
|
void unmap_physmem(const void *vaddr, unsigned long flags)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_PCI
|
|
|
|
if (map_dev) {
|
|
|
|
pci_unmap_physmem(vaddr, map_len, map_dev);
|
|
|
|
map_dev = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void sandbox_set_enable_pci_map(int enable)
|
|
|
|
{
|
|
|
|
enable_pci_map = enable;
|
|
|
|
}
|
|
|
|
|
2014-02-27 20:25:55 +00:00
|
|
|
phys_addr_t map_to_sysmem(const void *ptr)
|
2013-04-20 08:42:37 +00:00
|
|
|
{
|
|
|
|
return (u8 *)ptr - gd->arch.ram_buf;
|
|
|
|
}
|
|
|
|
|
2011-09-26 14:10:39 +00:00
|
|
|
void flush_dcache_range(unsigned long start, unsigned long stop)
|
|
|
|
{
|
|
|
|
}
|
2015-02-28 05:06:34 +00:00
|
|
|
|
2017-08-22 15:15:18 +00:00
|
|
|
void invalidate_dcache_range(unsigned long start, unsigned long stop)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-28 05:06:34 +00:00
|
|
|
int sandbox_read_fdt_from_file(void)
|
|
|
|
{
|
|
|
|
struct sandbox_state *state = state_get_current();
|
|
|
|
const char *fname = state->fdt_fname;
|
|
|
|
void *blob;
|
|
|
|
loff_t size;
|
|
|
|
int err;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
|
|
|
|
if (!state->fdt_fname) {
|
|
|
|
err = fdt_create_empty_tree(blob, 256);
|
|
|
|
if (!err)
|
|
|
|
goto done;
|
|
|
|
printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os_get_filesize(fname, &size);
|
|
|
|
if (err < 0) {
|
|
|
|
printf("Failed to file FDT file '%s'\n", fname);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
fd = os_open(fname, OS_O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
printf("Failed to open FDT file '%s'\n", fname);
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
if (os_read(fd, blob, size) != size) {
|
|
|
|
os_close(fd);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
os_close(fd);
|
|
|
|
|
|
|
|
done:
|
|
|
|
gd->fdt_blob = blob;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-05-22 11:05:23 +00:00
|
|
|
|
|
|
|
ulong timer_get_boot_us(void)
|
|
|
|
{
|
|
|
|
static uint64_t base_count;
|
|
|
|
uint64_t count = os_get_nsec();
|
|
|
|
|
|
|
|
if (!base_count)
|
|
|
|
base_count = count;
|
|
|
|
|
|
|
|
return (count - base_count) / 1000;
|
|
|
|
}
|
2018-05-16 15:42:22 +00:00
|
|
|
|
|
|
|
int setjmp(jmp_buf jmp)
|
|
|
|
{
|
|
|
|
return os_setjmp((ulong *)jmp, sizeof(*jmp));
|
|
|
|
}
|
|
|
|
|
|
|
|
void longjmp(jmp_buf jmp, int ret)
|
|
|
|
{
|
|
|
|
os_longjmp((ulong *)jmp, ret);
|
|
|
|
while (1)
|
|
|
|
;
|
|
|
|
}
|