cpu.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #define DEBUG
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <libfdt.h>
  10. #include <os.h>
  11. #include <asm/io.h>
  12. #include <asm/state.h>
  13. #include <dm/root.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /* Enable access to PCI memory with map_sysmem() */
  16. static bool enable_pci_map;
  17. #ifdef CONFIG_PCI
  18. /* Last device that was mapped into memory, and length of mapping */
  19. static struct udevice *map_dev;
  20. unsigned long map_len;
  21. #endif
  22. void sandbox_exit(void)
  23. {
  24. /* Do this here while it still has an effect */
  25. os_fd_restore();
  26. if (state_uninit())
  27. os_exit(2);
  28. if (dm_uninit())
  29. os_exit(2);
  30. /* This is considered normal termination for now */
  31. os_exit(0);
  32. }
  33. /* delay x useconds */
  34. void __udelay(unsigned long usec)
  35. {
  36. struct sandbox_state *state = state_get_current();
  37. if (!state->skip_delays)
  38. os_usleep(usec);
  39. }
  40. int cleanup_before_linux(void)
  41. {
  42. return 0;
  43. }
  44. int cleanup_before_linux_select(int flags)
  45. {
  46. return 0;
  47. }
  48. void *phys_to_virt(phys_addr_t paddr)
  49. {
  50. return (void *)(gd->arch.ram_buf + paddr);
  51. }
  52. phys_addr_t virt_to_phys(void *vaddr)
  53. {
  54. return (phys_addr_t)((uint8_t *)vaddr - gd->arch.ram_buf);
  55. }
  56. void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
  57. {
  58. #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
  59. unsigned long plen = len;
  60. void *ptr;
  61. map_dev = NULL;
  62. if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
  63. if (plen != len) {
  64. printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
  65. __func__, (uint)paddr, len, plen);
  66. }
  67. map_len = len;
  68. return ptr;
  69. }
  70. #endif
  71. return phys_to_virt(paddr);
  72. }
  73. void unmap_physmem(const void *vaddr, unsigned long flags)
  74. {
  75. #ifdef CONFIG_PCI
  76. if (map_dev) {
  77. pci_unmap_physmem(vaddr, map_len, map_dev);
  78. map_dev = NULL;
  79. }
  80. #endif
  81. }
  82. void sandbox_set_enable_pci_map(int enable)
  83. {
  84. enable_pci_map = enable;
  85. }
  86. phys_addr_t map_to_sysmem(const void *ptr)
  87. {
  88. return (u8 *)ptr - gd->arch.ram_buf;
  89. }
  90. void flush_dcache_range(unsigned long start, unsigned long stop)
  91. {
  92. }
  93. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  94. {
  95. }
  96. int sandbox_read_fdt_from_file(void)
  97. {
  98. struct sandbox_state *state = state_get_current();
  99. const char *fname = state->fdt_fname;
  100. void *blob;
  101. loff_t size;
  102. int err;
  103. int fd;
  104. blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
  105. if (!state->fdt_fname) {
  106. err = fdt_create_empty_tree(blob, 256);
  107. if (!err)
  108. goto done;
  109. printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
  110. return -EINVAL;
  111. }
  112. err = os_get_filesize(fname, &size);
  113. if (err < 0) {
  114. printf("Failed to file FDT file '%s'\n", fname);
  115. return err;
  116. }
  117. fd = os_open(fname, OS_O_RDONLY);
  118. if (fd < 0) {
  119. printf("Failed to open FDT file '%s'\n", fname);
  120. return -EACCES;
  121. }
  122. if (os_read(fd, blob, size) != size) {
  123. os_close(fd);
  124. return -EIO;
  125. }
  126. os_close(fd);
  127. done:
  128. gd->fdt_blob = blob;
  129. return 0;
  130. }
  131. ulong timer_get_boot_us(void)
  132. {
  133. static uint64_t base_count;
  134. uint64_t count = os_get_nsec();
  135. if (!base_count)
  136. base_count = count;
  137. return (count - base_count) / 1000;
  138. }