cpu.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. */
  5. #include <common.h>
  6. #include <cpu_func.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <linux/libfdt.h>
  10. #include <os.h>
  11. #include <asm/io.h>
  12. #include <asm/setjmp.h>
  13. #include <asm/state.h>
  14. #include <dm/root.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /* Enable access to PCI memory with map_sysmem() */
  17. static bool enable_pci_map;
  18. #ifdef CONFIG_PCI
  19. /* Last device that was mapped into memory, and length of mapping */
  20. static struct udevice *map_dev;
  21. unsigned long map_len;
  22. #endif
  23. void sandbox_exit(void)
  24. {
  25. /* Do this here while it still has an effect */
  26. os_fd_restore();
  27. if (state_uninit())
  28. os_exit(2);
  29. if (dm_uninit())
  30. os_exit(2);
  31. /* This is considered normal termination for now */
  32. os_exit(0);
  33. }
  34. /* delay x useconds */
  35. void __udelay(unsigned long usec)
  36. {
  37. struct sandbox_state *state = state_get_current();
  38. if (!state->skip_delays)
  39. os_usleep(usec);
  40. }
  41. int cleanup_before_linux(void)
  42. {
  43. return 0;
  44. }
  45. int cleanup_before_linux_select(int flags)
  46. {
  47. return 0;
  48. }
  49. /**
  50. * is_in_sandbox_mem() - Checks if a pointer is within sandbox's emulated DRAM
  51. *
  52. * This provides a way to check if a pointer is owned by sandbox (and is within
  53. * its RAM) or not. Sometimes pointers come from a test which conceptually runs
  54. * output sandbox, potentially with direct access to the C-library malloc()
  55. * function, or the sandbox stack (which is not actually within the emulated
  56. * DRAM.
  57. *
  58. * Such pointers obviously cannot be mapped into sandbox's DRAM, so we must
  59. * detect them an process them separately, by recording a mapping to a tag,
  60. * which we can use to map back to the pointer later.
  61. *
  62. * @ptr: Pointer to check
  63. * @return true if this is within sandbox emulated DRAM, false if not
  64. */
  65. static bool is_in_sandbox_mem(const void *ptr)
  66. {
  67. return (const uint8_t *)ptr >= gd->arch.ram_buf &&
  68. (const uint8_t *)ptr < gd->arch.ram_buf + gd->ram_size;
  69. }
  70. /**
  71. * phys_to_virt() - Converts a sandbox RAM address to a pointer
  72. *
  73. * Sandbox uses U-Boot addresses from 0 to the size of DRAM. These index into
  74. * the emulated DRAM buffer used by sandbox. This function converts such an
  75. * address to a pointer into this buffer, which can be used to access the
  76. * memory.
  77. *
  78. * If the address is outside this range, it is assumed to be a tag
  79. */
  80. void *phys_to_virt(phys_addr_t paddr)
  81. {
  82. struct sandbox_mapmem_entry *mentry;
  83. struct sandbox_state *state;
  84. /* If the address is within emulated DRAM, calculate the value */
  85. if (paddr < gd->ram_size)
  86. return (void *)(gd->arch.ram_buf + paddr);
  87. /*
  88. * Otherwise search out list of tags for the correct pointer previously
  89. * created by map_to_sysmem()
  90. */
  91. state = state_get_current();
  92. list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
  93. if (mentry->tag == paddr) {
  94. debug("%s: Used map from %lx to %p\n", __func__,
  95. (ulong)paddr, mentry->ptr);
  96. return mentry->ptr;
  97. }
  98. }
  99. printf("%s: Cannot map sandbox address %lx (SDRAM from 0 to %lx)\n",
  100. __func__, (ulong)paddr, (ulong)gd->ram_size);
  101. os_abort();
  102. /* Not reached */
  103. return NULL;
  104. }
  105. struct sandbox_mapmem_entry *find_tag(const void *ptr)
  106. {
  107. struct sandbox_mapmem_entry *mentry;
  108. struct sandbox_state *state = state_get_current();
  109. list_for_each_entry(mentry, &state->mapmem_head, sibling_node) {
  110. if (mentry->ptr == ptr) {
  111. debug("%s: Used map from %p to %lx\n", __func__, ptr,
  112. mentry->tag);
  113. return mentry;
  114. }
  115. }
  116. return NULL;
  117. }
  118. phys_addr_t virt_to_phys(void *ptr)
  119. {
  120. struct sandbox_mapmem_entry *mentry;
  121. /*
  122. * If it is in emulated RAM, don't bother looking for a tag. Just
  123. * calculate the pointer using the provides offset into the RAM buffer.
  124. */
  125. if (is_in_sandbox_mem(ptr))
  126. return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf);
  127. mentry = find_tag(ptr);
  128. if (!mentry) {
  129. /* Abort so that gdb can be used here */
  130. printf("%s: Cannot map sandbox address %p (SDRAM from 0 to %lx)\n",
  131. __func__, ptr, (ulong)gd->ram_size);
  132. os_abort();
  133. }
  134. debug("%s: Used map from %p to %lx\n", __func__, ptr, mentry->tag);
  135. return mentry->tag;
  136. }
  137. void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
  138. {
  139. #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
  140. unsigned long plen = len;
  141. void *ptr;
  142. map_dev = NULL;
  143. if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
  144. if (plen != len) {
  145. printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
  146. __func__, (uint)paddr, len, plen);
  147. }
  148. map_len = len;
  149. return ptr;
  150. }
  151. #endif
  152. return phys_to_virt(paddr);
  153. }
  154. void unmap_physmem(const void *ptr, unsigned long flags)
  155. {
  156. #ifdef CONFIG_PCI
  157. if (map_dev) {
  158. pci_unmap_physmem(ptr, map_len, map_dev);
  159. map_dev = NULL;
  160. }
  161. #endif
  162. }
  163. phys_addr_t map_to_sysmem(const void *ptr)
  164. {
  165. struct sandbox_mapmem_entry *mentry;
  166. /*
  167. * If it is in emulated RAM, don't bother creating a tag. Just return
  168. * the offset into the RAM buffer.
  169. */
  170. if (is_in_sandbox_mem(ptr))
  171. return (u8 *)ptr - gd->arch.ram_buf;
  172. /*
  173. * See if there is an existing tag with this pointer. If not, set up a
  174. * new one.
  175. */
  176. mentry = find_tag(ptr);
  177. if (!mentry) {
  178. struct sandbox_state *state = state_get_current();
  179. mentry = malloc(sizeof(*mentry));
  180. if (!mentry) {
  181. printf("%s: Error: Out of memory\n", __func__);
  182. os_exit(ENOMEM);
  183. }
  184. mentry->tag = state->next_tag++;
  185. mentry->ptr = (void *)ptr;
  186. list_add_tail(&mentry->sibling_node, &state->mapmem_head);
  187. debug("%s: Added map from %p to %lx\n", __func__, ptr,
  188. (ulong)mentry->tag);
  189. }
  190. /*
  191. * Return the tag as the address to use. A later call to map_sysmem()
  192. * will return ptr
  193. */
  194. return mentry->tag;
  195. }
  196. unsigned int sandbox_read(const void *addr, enum sandboxio_size_t size)
  197. {
  198. struct sandbox_state *state = state_get_current();
  199. if (!state->allow_memio)
  200. return 0;
  201. switch (size) {
  202. case SB_SIZE_8:
  203. return *(u8 *)addr;
  204. case SB_SIZE_16:
  205. return *(u16 *)addr;
  206. case SB_SIZE_32:
  207. return *(u32 *)addr;
  208. case SB_SIZE_64:
  209. return *(u64 *)addr;
  210. }
  211. return 0;
  212. }
  213. void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size)
  214. {
  215. struct sandbox_state *state = state_get_current();
  216. if (!state->allow_memio)
  217. return;
  218. switch (size) {
  219. case SB_SIZE_8:
  220. *(u8 *)addr = val;
  221. break;
  222. case SB_SIZE_16:
  223. *(u16 *)addr = val;
  224. break;
  225. case SB_SIZE_32:
  226. *(u32 *)addr = val;
  227. break;
  228. case SB_SIZE_64:
  229. *(u64 *)addr = val;
  230. break;
  231. }
  232. }
  233. void sandbox_set_enable_memio(bool enable)
  234. {
  235. struct sandbox_state *state = state_get_current();
  236. state->allow_memio = enable;
  237. }
  238. void sandbox_set_enable_pci_map(int enable)
  239. {
  240. enable_pci_map = enable;
  241. }
  242. void flush_dcache_range(unsigned long start, unsigned long stop)
  243. {
  244. }
  245. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  246. {
  247. }
  248. int sandbox_read_fdt_from_file(void)
  249. {
  250. struct sandbox_state *state = state_get_current();
  251. const char *fname = state->fdt_fname;
  252. void *blob;
  253. loff_t size;
  254. int err;
  255. int fd;
  256. blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
  257. if (!state->fdt_fname) {
  258. err = fdt_create_empty_tree(blob, 256);
  259. if (!err)
  260. goto done;
  261. printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
  262. return -EINVAL;
  263. }
  264. err = os_get_filesize(fname, &size);
  265. if (err < 0) {
  266. printf("Failed to file FDT file '%s'\n", fname);
  267. return err;
  268. }
  269. fd = os_open(fname, OS_O_RDONLY);
  270. if (fd < 0) {
  271. printf("Failed to open FDT file '%s'\n", fname);
  272. return -EACCES;
  273. }
  274. if (os_read(fd, blob, size) != size) {
  275. os_close(fd);
  276. return -EIO;
  277. }
  278. os_close(fd);
  279. done:
  280. gd->fdt_blob = blob;
  281. return 0;
  282. }
  283. ulong timer_get_boot_us(void)
  284. {
  285. static uint64_t base_count;
  286. uint64_t count = os_get_nsec();
  287. if (!base_count)
  288. base_count = count;
  289. return (count - base_count) / 1000;
  290. }