early_ioremap.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Provide common bits of early_ioremap() support for architectures needing
  4. * temporary mappings during boot before ioremap() is available.
  5. *
  6. * This is mostly a direct copy of the x86 early_ioremap implementation.
  7. *
  8. * (C) Copyright 1995 1996, 2014 Linus Torvalds
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/mm.h>
  17. #include <linux/vmalloc.h>
  18. #include <asm/fixmap.h>
  19. #include <asm/early_ioremap.h>
  20. #include "internal.h"
  21. #ifdef CONFIG_MMU
  22. static int early_ioremap_debug __initdata;
  23. static int __init early_ioremap_debug_setup(char *str)
  24. {
  25. early_ioremap_debug = 1;
  26. return 0;
  27. }
  28. early_param("early_ioremap_debug", early_ioremap_debug_setup);
  29. #define early_ioremap_dbg(fmt, args...) \
  30. do { \
  31. if (unlikely(early_ioremap_debug)) { \
  32. pr_warn(fmt, ##args); \
  33. dump_stack(); \
  34. } \
  35. } while (0)
  36. static int after_paging_init __initdata;
  37. pgprot_t __init __weak early_memremap_pgprot_adjust(resource_size_t phys_addr,
  38. unsigned long size,
  39. pgprot_t prot)
  40. {
  41. return prot;
  42. }
  43. void __init early_ioremap_reset(void)
  44. {
  45. after_paging_init = 1;
  46. }
  47. /*
  48. * Generally, ioremap() is available after paging_init() has been called.
  49. * Architectures wanting to allow early_ioremap after paging_init() can
  50. * define __late_set_fixmap and __late_clear_fixmap to do the right thing.
  51. */
  52. #ifndef __late_set_fixmap
  53. static inline void __init __late_set_fixmap(enum fixed_addresses idx,
  54. phys_addr_t phys, pgprot_t prot)
  55. {
  56. BUG();
  57. }
  58. #endif
  59. #ifndef __late_clear_fixmap
  60. static inline void __init __late_clear_fixmap(enum fixed_addresses idx)
  61. {
  62. BUG();
  63. }
  64. #endif
  65. static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
  66. static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
  67. static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
  68. void __init early_ioremap_setup(void)
  69. {
  70. int i;
  71. for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
  72. WARN_ON_ONCE(prev_map[i]);
  73. slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
  74. }
  75. }
  76. static int __init check_early_ioremap_leak(void)
  77. {
  78. int count = 0;
  79. int i;
  80. for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
  81. if (prev_map[i])
  82. count++;
  83. if (WARN(count, KERN_WARNING
  84. "Debug warning: early ioremap leak of %d areas detected.\n"
  85. "please boot with early_ioremap_debug and report the dmesg.\n",
  86. count))
  87. return 1;
  88. return 0;
  89. }
  90. late_initcall(check_early_ioremap_leak);
  91. static void __init __iomem *
  92. __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
  93. {
  94. unsigned long offset;
  95. resource_size_t last_addr;
  96. unsigned int nrpages;
  97. enum fixed_addresses idx;
  98. int i, slot;
  99. WARN_ON(system_state >= SYSTEM_RUNNING);
  100. slot = -1;
  101. for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
  102. if (!prev_map[i]) {
  103. slot = i;
  104. break;
  105. }
  106. }
  107. if (WARN(slot < 0, "%s(%pa, %08lx) not found slot\n",
  108. __func__, &phys_addr, size))
  109. return NULL;
  110. /* Don't allow wraparound or zero size */
  111. last_addr = phys_addr + size - 1;
  112. if (WARN_ON(!size || last_addr < phys_addr))
  113. return NULL;
  114. prev_size[slot] = size;
  115. /*
  116. * Mappings have to be page-aligned
  117. */
  118. offset = offset_in_page(phys_addr);
  119. phys_addr &= PAGE_MASK;
  120. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  121. /*
  122. * Mappings have to fit in the FIX_BTMAP area.
  123. */
  124. nrpages = size >> PAGE_SHIFT;
  125. if (WARN_ON(nrpages > NR_FIX_BTMAPS))
  126. return NULL;
  127. early_ioremap_dbg("%s(%pa, %08lx) [%d] => %08lx + %08lx\n",
  128. __func__, &phys_addr, size, slot, slot_virt[slot], offset);
  129. /*
  130. * Ok, go for it..
  131. */
  132. idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
  133. while (nrpages > 0) {
  134. if (after_paging_init)
  135. __late_set_fixmap(idx, phys_addr, prot);
  136. else
  137. __early_set_fixmap(idx, phys_addr, prot);
  138. phys_addr += PAGE_SIZE;
  139. --idx;
  140. --nrpages;
  141. }
  142. prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
  143. return prev_map[slot];
  144. }
  145. void __init early_iounmap(void __iomem *addr, unsigned long size)
  146. {
  147. unsigned long virt_addr;
  148. unsigned long offset;
  149. unsigned int nrpages;
  150. enum fixed_addresses idx;
  151. int i, slot;
  152. slot = -1;
  153. for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
  154. if (prev_map[i] == addr) {
  155. slot = i;
  156. break;
  157. }
  158. }
  159. if (WARN(slot < 0, "%s(%p, %08lx) not found slot\n",
  160. __func__, addr, size))
  161. return;
  162. if (WARN(prev_size[slot] != size,
  163. "%s(%p, %08lx) [%d] size not consistent %08lx\n",
  164. __func__, addr, size, slot, prev_size[slot]))
  165. return;
  166. early_ioremap_dbg("%s(%p, %08lx) [%d]\n", __func__, addr, size, slot);
  167. virt_addr = (unsigned long)addr;
  168. if (WARN_ON(virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)))
  169. return;
  170. offset = offset_in_page(virt_addr);
  171. nrpages = PAGE_ALIGN(offset + size) >> PAGE_SHIFT;
  172. idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
  173. while (nrpages > 0) {
  174. if (after_paging_init)
  175. __late_clear_fixmap(idx);
  176. else
  177. __early_set_fixmap(idx, 0, FIXMAP_PAGE_CLEAR);
  178. --idx;
  179. --nrpages;
  180. }
  181. prev_map[slot] = NULL;
  182. }
  183. /* Remap an IO device */
  184. void __init __iomem *
  185. early_ioremap(resource_size_t phys_addr, unsigned long size)
  186. {
  187. return __early_ioremap(phys_addr, size, FIXMAP_PAGE_IO);
  188. }
  189. /* Remap memory */
  190. void __init *
  191. early_memremap(resource_size_t phys_addr, unsigned long size)
  192. {
  193. pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size,
  194. FIXMAP_PAGE_NORMAL);
  195. return (__force void *)__early_ioremap(phys_addr, size, prot);
  196. }
  197. #ifdef FIXMAP_PAGE_RO
  198. void __init *
  199. early_memremap_ro(resource_size_t phys_addr, unsigned long size)
  200. {
  201. pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size,
  202. FIXMAP_PAGE_RO);
  203. return (__force void *)__early_ioremap(phys_addr, size, prot);
  204. }
  205. #endif
  206. #ifdef CONFIG_ARCH_USE_MEMREMAP_PROT
  207. void __init *
  208. early_memremap_prot(resource_size_t phys_addr, unsigned long size,
  209. unsigned long prot_val)
  210. {
  211. return (__force void *)__early_ioremap(phys_addr, size,
  212. __pgprot(prot_val));
  213. }
  214. #endif
  215. #define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT)
  216. /*
  217. * If no empty slot, handle that and return -ENOMEM.
  218. */
  219. int __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size)
  220. {
  221. unsigned long slop, clen;
  222. char *p;
  223. while (size) {
  224. slop = offset_in_page(src);
  225. clen = size;
  226. if (clen > MAX_MAP_CHUNK - slop)
  227. clen = MAX_MAP_CHUNK - slop;
  228. p = early_memremap(src & PAGE_MASK, clen + slop);
  229. if (!p)
  230. return -ENOMEM;
  231. memcpy(dest, p + slop, clen);
  232. early_memunmap(p, clen + slop);
  233. dest += clen;
  234. src += clen;
  235. size -= clen;
  236. }
  237. return 0;
  238. }
  239. #else /* CONFIG_MMU */
  240. void __init __iomem *
  241. early_ioremap(resource_size_t phys_addr, unsigned long size)
  242. {
  243. return (__force void __iomem *)phys_addr;
  244. }
  245. /* Remap memory */
  246. void __init *
  247. early_memremap(resource_size_t phys_addr, unsigned long size)
  248. {
  249. return (void *)phys_addr;
  250. }
  251. void __init *
  252. early_memremap_ro(resource_size_t phys_addr, unsigned long size)
  253. {
  254. return (void *)phys_addr;
  255. }
  256. void __init early_iounmap(void __iomem *addr, unsigned long size)
  257. {
  258. }
  259. #endif /* CONFIG_MMU */
  260. void __init early_memunmap(void *addr, unsigned long size)
  261. {
  262. early_iounmap((__force void __iomem *)addr, size);
  263. }