cpu.c 7.6 KB

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