early_ioremap.c 5.4 KB

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