nommu.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/mm/nommu.c
  4. *
  5. * ARM uCLinux supporting functions.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mm.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/io.h>
  11. #include <linux/memblock.h>
  12. #include <linux/kernel.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/cp15.h>
  15. #include <asm/sections.h>
  16. #include <asm/page.h>
  17. #include <asm/setup.h>
  18. #include <asm/traps.h>
  19. #include <asm/mach/arch.h>
  20. #include <asm/cputype.h>
  21. #include <asm/mpu.h>
  22. #include <asm/procinfo.h>
  23. #include "mm.h"
  24. unsigned long vectors_base;
  25. /*
  26. * empty_zero_page is a special page that is used for
  27. * zero-initialized data and COW.
  28. */
  29. struct page *empty_zero_page;
  30. EXPORT_SYMBOL(empty_zero_page);
  31. #ifdef CONFIG_ARM_MPU
  32. struct mpu_rgn_info mpu_rgn_info;
  33. #endif
  34. #ifdef CONFIG_CPU_CP15
  35. #ifdef CONFIG_CPU_HIGH_VECTOR
  36. unsigned long setup_vectors_base(void)
  37. {
  38. unsigned long reg = get_cr();
  39. set_cr(reg | CR_V);
  40. return 0xffff0000;
  41. }
  42. #else /* CONFIG_CPU_HIGH_VECTOR */
  43. /* Write exception base address to VBAR */
  44. static inline void set_vbar(unsigned long val)
  45. {
  46. asm("mcr p15, 0, %0, c12, c0, 0" : : "r" (val) : "cc");
  47. }
  48. /*
  49. * Security extensions, bits[7:4], permitted values,
  50. * 0b0000 - not implemented, 0b0001/0b0010 - implemented
  51. */
  52. static inline bool security_extensions_enabled(void)
  53. {
  54. /* Check CPUID Identification Scheme before ID_PFR1 read */
  55. if ((read_cpuid_id() & 0x000f0000) == 0x000f0000)
  56. return cpuid_feature_extract(CPUID_EXT_PFR1, 4) ||
  57. cpuid_feature_extract(CPUID_EXT_PFR1, 20);
  58. return 0;
  59. }
  60. unsigned long setup_vectors_base(void)
  61. {
  62. unsigned long base = 0, reg = get_cr();
  63. set_cr(reg & ~CR_V);
  64. if (security_extensions_enabled()) {
  65. if (IS_ENABLED(CONFIG_REMAP_VECTORS_TO_RAM))
  66. base = CONFIG_DRAM_BASE;
  67. set_vbar(base);
  68. } else if (IS_ENABLED(CONFIG_REMAP_VECTORS_TO_RAM)) {
  69. if (CONFIG_DRAM_BASE != 0)
  70. pr_err("Security extensions not enabled, vectors cannot be remapped to RAM, vectors base will be 0x00000000\n");
  71. }
  72. return base;
  73. }
  74. #endif /* CONFIG_CPU_HIGH_VECTOR */
  75. #endif /* CONFIG_CPU_CP15 */
  76. void __init arm_mm_memblock_reserve(void)
  77. {
  78. #ifndef CONFIG_CPU_V7M
  79. vectors_base = IS_ENABLED(CONFIG_CPU_CP15) ? setup_vectors_base() : 0;
  80. /*
  81. * Register the exception vector page.
  82. * some architectures which the DRAM is the exception vector to trap,
  83. * alloc_page breaks with error, although it is not NULL, but "0."
  84. */
  85. memblock_reserve(vectors_base, 2 * PAGE_SIZE);
  86. #else /* ifndef CONFIG_CPU_V7M */
  87. /*
  88. * There is no dedicated vector page on V7-M. So nothing needs to be
  89. * reserved here.
  90. */
  91. #endif
  92. /*
  93. * In any case, always ensure address 0 is never used as many things
  94. * get very confused if 0 is returned as a legitimate address.
  95. */
  96. memblock_reserve(0, 1);
  97. }
  98. static void __init adjust_lowmem_bounds_mpu(void)
  99. {
  100. unsigned long pmsa = read_cpuid_ext(CPUID_EXT_MMFR0) & MMFR0_PMSA;
  101. switch (pmsa) {
  102. case MMFR0_PMSAv7:
  103. pmsav7_adjust_lowmem_bounds();
  104. break;
  105. case MMFR0_PMSAv8:
  106. pmsav8_adjust_lowmem_bounds();
  107. break;
  108. default:
  109. break;
  110. }
  111. }
  112. static void __init mpu_setup(void)
  113. {
  114. unsigned long pmsa = read_cpuid_ext(CPUID_EXT_MMFR0) & MMFR0_PMSA;
  115. switch (pmsa) {
  116. case MMFR0_PMSAv7:
  117. pmsav7_setup();
  118. break;
  119. case MMFR0_PMSAv8:
  120. pmsav8_setup();
  121. break;
  122. default:
  123. break;
  124. }
  125. }
  126. void __init adjust_lowmem_bounds(void)
  127. {
  128. phys_addr_t end;
  129. adjust_lowmem_bounds_mpu();
  130. end = memblock_end_of_DRAM();
  131. high_memory = __va(end - 1) + 1;
  132. memblock_set_current_limit(end);
  133. }
  134. /*
  135. * paging_init() sets up the page tables, initialises the zone memory
  136. * maps, and sets up the zero page, bad page and bad page tables.
  137. */
  138. void __init paging_init(const struct machine_desc *mdesc)
  139. {
  140. void *zero_page;
  141. early_trap_init((void *)vectors_base);
  142. mpu_setup();
  143. /* allocate the zero page. */
  144. zero_page = (void *)memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  145. if (!zero_page)
  146. panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
  147. __func__, PAGE_SIZE, PAGE_SIZE);
  148. bootmem_init();
  149. empty_zero_page = virt_to_page(zero_page);
  150. flush_dcache_page(empty_zero_page);
  151. }
  152. /*
  153. * We don't need to do anything here for nommu machines.
  154. */
  155. void setup_mm_for_reboot(void)
  156. {
  157. }
  158. void flush_dcache_page(struct page *page)
  159. {
  160. __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
  161. }
  162. EXPORT_SYMBOL(flush_dcache_page);
  163. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  164. unsigned long uaddr, void *dst, const void *src,
  165. unsigned long len)
  166. {
  167. memcpy(dst, src, len);
  168. if (vma->vm_flags & VM_EXEC)
  169. __cpuc_coherent_user_range(uaddr, uaddr + len);
  170. }
  171. void __iomem *__arm_ioremap_pfn(unsigned long pfn, unsigned long offset,
  172. size_t size, unsigned int mtype)
  173. {
  174. if (pfn >= (0x100000000ULL >> PAGE_SHIFT))
  175. return NULL;
  176. return (void __iomem *) (offset + (pfn << PAGE_SHIFT));
  177. }
  178. EXPORT_SYMBOL(__arm_ioremap_pfn);
  179. void __iomem *__arm_ioremap_caller(phys_addr_t phys_addr, size_t size,
  180. unsigned int mtype, void *caller)
  181. {
  182. return (void __iomem *)phys_addr;
  183. }
  184. void __iomem * (*arch_ioremap_caller)(phys_addr_t, size_t, unsigned int, void *);
  185. void __iomem *ioremap(resource_size_t res_cookie, size_t size)
  186. {
  187. return __arm_ioremap_caller(res_cookie, size, MT_DEVICE,
  188. __builtin_return_address(0));
  189. }
  190. EXPORT_SYMBOL(ioremap);
  191. void __iomem *ioremap_cache(resource_size_t res_cookie, size_t size)
  192. {
  193. return __arm_ioremap_caller(res_cookie, size, MT_DEVICE_CACHED,
  194. __builtin_return_address(0));
  195. }
  196. EXPORT_SYMBOL(ioremap_cache);
  197. void __iomem *ioremap_wc(resource_size_t res_cookie, size_t size)
  198. {
  199. return __arm_ioremap_caller(res_cookie, size, MT_DEVICE_WC,
  200. __builtin_return_address(0));
  201. }
  202. EXPORT_SYMBOL(ioremap_wc);
  203. #ifdef CONFIG_PCI
  204. #include <asm/mach/map.h>
  205. void __iomem *pci_remap_cfgspace(resource_size_t res_cookie, size_t size)
  206. {
  207. return arch_ioremap_caller(res_cookie, size, MT_UNCACHED,
  208. __builtin_return_address(0));
  209. }
  210. EXPORT_SYMBOL_GPL(pci_remap_cfgspace);
  211. #endif
  212. void *arch_memremap_wb(phys_addr_t phys_addr, size_t size)
  213. {
  214. return (void *)phys_addr;
  215. }
  216. void iounmap(volatile void __iomem *io_addr)
  217. {
  218. }
  219. EXPORT_SYMBOL(iounmap);