cpu.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. */
  5. #define DEBUG
  6. #include <common.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. void *phys_to_virt(phys_addr_t paddr)
  50. {
  51. return (void *)(gd->arch.ram_buf + paddr);
  52. }
  53. phys_addr_t virt_to_phys(void *vaddr)
  54. {
  55. return (phys_addr_t)((uint8_t *)vaddr - gd->arch.ram_buf);
  56. }
  57. void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
  58. {
  59. #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
  60. unsigned long plen = len;
  61. void *ptr;
  62. map_dev = NULL;
  63. if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
  64. if (plen != len) {
  65. printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
  66. __func__, (uint)paddr, len, plen);
  67. }
  68. map_len = len;
  69. return ptr;
  70. }
  71. #endif
  72. return phys_to_virt(paddr);
  73. }
  74. void unmap_physmem(const void *vaddr, unsigned long flags)
  75. {
  76. #ifdef CONFIG_PCI
  77. if (map_dev) {
  78. pci_unmap_physmem(vaddr, map_len, map_dev);
  79. map_dev = NULL;
  80. }
  81. #endif
  82. }
  83. void sandbox_set_enable_pci_map(int enable)
  84. {
  85. enable_pci_map = enable;
  86. }
  87. phys_addr_t map_to_sysmem(const void *ptr)
  88. {
  89. return (u8 *)ptr - gd->arch.ram_buf;
  90. }
  91. void flush_dcache_range(unsigned long start, unsigned long stop)
  92. {
  93. }
  94. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  95. {
  96. }
  97. int sandbox_read_fdt_from_file(void)
  98. {
  99. struct sandbox_state *state = state_get_current();
  100. const char *fname = state->fdt_fname;
  101. void *blob;
  102. loff_t size;
  103. int err;
  104. int fd;
  105. blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
  106. if (!state->fdt_fname) {
  107. err = fdt_create_empty_tree(blob, 256);
  108. if (!err)
  109. goto done;
  110. printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
  111. return -EINVAL;
  112. }
  113. err = os_get_filesize(fname, &size);
  114. if (err < 0) {
  115. printf("Failed to file FDT file '%s'\n", fname);
  116. return err;
  117. }
  118. fd = os_open(fname, OS_O_RDONLY);
  119. if (fd < 0) {
  120. printf("Failed to open FDT file '%s'\n", fname);
  121. return -EACCES;
  122. }
  123. if (os_read(fd, blob, size) != size) {
  124. os_close(fd);
  125. return -EIO;
  126. }
  127. os_close(fd);
  128. done:
  129. gd->fdt_blob = blob;
  130. return 0;
  131. }
  132. ulong timer_get_boot_us(void)
  133. {
  134. static uint64_t base_count;
  135. uint64_t count = os_get_nsec();
  136. if (!base_count)
  137. base_count = count;
  138. return (count - base_count) / 1000;
  139. }
  140. int setjmp(jmp_buf jmp)
  141. {
  142. return os_setjmp((ulong *)jmp, sizeof(*jmp));
  143. }
  144. void longjmp(jmp_buf jmp, int ret)
  145. {
  146. os_longjmp((ulong *)jmp, ret);
  147. while (1)
  148. ;
  149. }