cpu.c 7.6 KB

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