nommu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * linux/arch/arm/mm/nommu.c
  3. *
  4. * ARM uCLinux supporting functions.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/mm.h>
  8. #include <linux/pagemap.h>
  9. #include <linux/io.h>
  10. #include <linux/memblock.h>
  11. #include <linux/kernel.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/sections.h>
  14. #include <asm/page.h>
  15. #include <asm/setup.h>
  16. #include <asm/traps.h>
  17. #include <asm/mach/arch.h>
  18. #include <asm/cputype.h>
  19. #include <asm/mpu.h>
  20. #include <asm/procinfo.h>
  21. #include "mm.h"
  22. #ifdef CONFIG_ARM_MPU
  23. struct mpu_rgn_info mpu_rgn_info;
  24. /* Region number */
  25. static void rgnr_write(u32 v)
  26. {
  27. asm("mcr p15, 0, %0, c6, c2, 0" : : "r" (v));
  28. }
  29. /* Data-side / unified region attributes */
  30. /* Region access control register */
  31. static void dracr_write(u32 v)
  32. {
  33. asm("mcr p15, 0, %0, c6, c1, 4" : : "r" (v));
  34. }
  35. /* Region size register */
  36. static void drsr_write(u32 v)
  37. {
  38. asm("mcr p15, 0, %0, c6, c1, 2" : : "r" (v));
  39. }
  40. /* Region base address register */
  41. static void drbar_write(u32 v)
  42. {
  43. asm("mcr p15, 0, %0, c6, c1, 0" : : "r" (v));
  44. }
  45. static u32 drbar_read(void)
  46. {
  47. u32 v;
  48. asm("mrc p15, 0, %0, c6, c1, 0" : "=r" (v));
  49. return v;
  50. }
  51. /* Optional instruction-side region attributes */
  52. /* I-side Region access control register */
  53. static void iracr_write(u32 v)
  54. {
  55. asm("mcr p15, 0, %0, c6, c1, 5" : : "r" (v));
  56. }
  57. /* I-side Region size register */
  58. static void irsr_write(u32 v)
  59. {
  60. asm("mcr p15, 0, %0, c6, c1, 3" : : "r" (v));
  61. }
  62. /* I-side Region base address register */
  63. static void irbar_write(u32 v)
  64. {
  65. asm("mcr p15, 0, %0, c6, c1, 1" : : "r" (v));
  66. }
  67. static unsigned long irbar_read(void)
  68. {
  69. unsigned long v;
  70. asm("mrc p15, 0, %0, c6, c1, 1" : "=r" (v));
  71. return v;
  72. }
  73. /* MPU initialisation functions */
  74. void __init sanity_check_meminfo_mpu(void)
  75. {
  76. int i;
  77. struct membank *bank = meminfo.bank;
  78. phys_addr_t phys_offset = PHYS_OFFSET;
  79. phys_addr_t aligned_region_size, specified_mem_size, rounded_mem_size;
  80. /* Initially only use memory continuous from PHYS_OFFSET */
  81. if (bank_phys_start(&bank[0]) != phys_offset)
  82. panic("First memory bank must be contiguous from PHYS_OFFSET");
  83. /* Banks have already been sorted by start address */
  84. for (i = 1; i < meminfo.nr_banks; i++) {
  85. if (bank[i].start <= bank_phys_end(&bank[0]) &&
  86. bank_phys_end(&bank[i]) > bank_phys_end(&bank[0])) {
  87. bank[0].size = bank_phys_end(&bank[i]) - bank[0].start;
  88. } else {
  89. pr_notice("Ignoring RAM after 0x%.8lx. "
  90. "First non-contiguous (ignored) bank start: 0x%.8lx\n",
  91. (unsigned long)bank_phys_end(&bank[0]),
  92. (unsigned long)bank_phys_start(&bank[i]));
  93. break;
  94. }
  95. }
  96. /* All contiguous banks are now merged in to the first bank */
  97. meminfo.nr_banks = 1;
  98. specified_mem_size = bank[0].size;
  99. /*
  100. * MPU has curious alignment requirements: Size must be power of 2, and
  101. * region start must be aligned to the region size
  102. */
  103. if (phys_offset != 0)
  104. pr_info("PHYS_OFFSET != 0 => MPU Region size constrained by alignment requirements\n");
  105. /*
  106. * Maximum aligned region might overflow phys_addr_t if phys_offset is
  107. * 0. Hence we keep everything below 4G until we take the smaller of
  108. * the aligned_region_size and rounded_mem_size, one of which is
  109. * guaranteed to be smaller than the maximum physical address.
  110. */
  111. aligned_region_size = (phys_offset - 1) ^ (phys_offset);
  112. /* Find the max power-of-two sized region that fits inside our bank */
  113. rounded_mem_size = (1 << __fls(bank[0].size)) - 1;
  114. /* The actual region size is the smaller of the two */
  115. aligned_region_size = aligned_region_size < rounded_mem_size
  116. ? aligned_region_size + 1
  117. : rounded_mem_size + 1;
  118. if (aligned_region_size != specified_mem_size)
  119. pr_warn("Truncating memory from 0x%.8lx to 0x%.8lx (MPU region constraints)",
  120. (unsigned long)specified_mem_size,
  121. (unsigned long)aligned_region_size);
  122. meminfo.bank[0].size = aligned_region_size;
  123. pr_debug("MPU Region from 0x%.8lx size 0x%.8lx (end 0x%.8lx))\n",
  124. (unsigned long)phys_offset,
  125. (unsigned long)aligned_region_size,
  126. (unsigned long)bank_phys_end(&bank[0]));
  127. }
  128. static int mpu_present(void)
  129. {
  130. return ((read_cpuid_ext(CPUID_EXT_MMFR0) & MMFR0_PMSA) == MMFR0_PMSAv7);
  131. }
  132. static int mpu_max_regions(void)
  133. {
  134. /*
  135. * We don't support a different number of I/D side regions so if we
  136. * have separate instruction and data memory maps then return
  137. * whichever side has a smaller number of supported regions.
  138. */
  139. u32 dregions, iregions, mpuir;
  140. mpuir = read_cpuid(CPUID_MPUIR);
  141. dregions = iregions = (mpuir & MPUIR_DREGION_SZMASK) >> MPUIR_DREGION;
  142. /* Check for separate d-side and i-side memory maps */
  143. if (mpuir & MPUIR_nU)
  144. iregions = (mpuir & MPUIR_IREGION_SZMASK) >> MPUIR_IREGION;
  145. /* Use the smallest of the two maxima */
  146. return min(dregions, iregions);
  147. }
  148. static int mpu_iside_independent(void)
  149. {
  150. /* MPUIR.nU specifies whether there is *not* a unified memory map */
  151. return read_cpuid(CPUID_MPUIR) & MPUIR_nU;
  152. }
  153. static int mpu_min_region_order(void)
  154. {
  155. u32 drbar_result, irbar_result;
  156. /* We've kept a region free for this probing */
  157. rgnr_write(MPU_PROBE_REGION);
  158. isb();
  159. /*
  160. * As per ARM ARM, write 0xFFFFFFFC to DRBAR to find the minimum
  161. * region order
  162. */
  163. drbar_write(0xFFFFFFFC);
  164. drbar_result = irbar_result = drbar_read();
  165. drbar_write(0x0);
  166. /* If the MPU is non-unified, we use the larger of the two minima*/
  167. if (mpu_iside_independent()) {
  168. irbar_write(0xFFFFFFFC);
  169. irbar_result = irbar_read();
  170. irbar_write(0x0);
  171. }
  172. isb(); /* Ensure that MPU region operations have completed */
  173. /* Return whichever result is larger */
  174. return __ffs(max(drbar_result, irbar_result));
  175. }
  176. static int mpu_setup_region(unsigned int number, phys_addr_t start,
  177. unsigned int size_order, unsigned int properties)
  178. {
  179. u32 size_data;
  180. /* We kept a region free for probing resolution of MPU regions*/
  181. if (number > mpu_max_regions() || number == MPU_PROBE_REGION)
  182. return -ENOENT;
  183. if (size_order > 32)
  184. return -ENOMEM;
  185. if (size_order < mpu_min_region_order())
  186. return -ENOMEM;
  187. /* Writing N to bits 5:1 (RSR_SZ) specifies region size 2^N+1 */
  188. size_data = ((size_order - 1) << MPU_RSR_SZ) | 1 << MPU_RSR_EN;
  189. dsb(); /* Ensure all previous data accesses occur with old mappings */
  190. rgnr_write(number);
  191. isb();
  192. drbar_write(start);
  193. dracr_write(properties);
  194. isb(); /* Propagate properties before enabling region */
  195. drsr_write(size_data);
  196. /* Check for independent I-side registers */
  197. if (mpu_iside_independent()) {
  198. irbar_write(start);
  199. iracr_write(properties);
  200. isb();
  201. irsr_write(size_data);
  202. }
  203. isb();
  204. /* Store region info (we treat i/d side the same, so only store d) */
  205. mpu_rgn_info.rgns[number].dracr = properties;
  206. mpu_rgn_info.rgns[number].drbar = start;
  207. mpu_rgn_info.rgns[number].drsr = size_data;
  208. return 0;
  209. }
  210. /*
  211. * Set up default MPU regions, doing nothing if there is no MPU
  212. */
  213. void __init mpu_setup(void)
  214. {
  215. int region_err;
  216. if (!mpu_present())
  217. return;
  218. region_err = mpu_setup_region(MPU_RAM_REGION, PHYS_OFFSET,
  219. ilog2(meminfo.bank[0].size),
  220. MPU_AP_PL1RW_PL0RW | MPU_RGN_NORMAL);
  221. if (region_err) {
  222. panic("MPU region initialization failure! %d", region_err);
  223. } else {
  224. pr_info("Using ARMv7 PMSA Compliant MPU. "
  225. "Region independence: %s, Max regions: %d\n",
  226. mpu_iside_independent() ? "Yes" : "No",
  227. mpu_max_regions());
  228. }
  229. }
  230. #else
  231. static void sanity_check_meminfo_mpu(void) {}
  232. static void __init mpu_setup(void) {}
  233. #endif /* CONFIG_ARM_MPU */
  234. void __init arm_mm_memblock_reserve(void)
  235. {
  236. #ifndef CONFIG_CPU_V7M
  237. /*
  238. * Register the exception vector page.
  239. * some architectures which the DRAM is the exception vector to trap,
  240. * alloc_page breaks with error, although it is not NULL, but "0."
  241. */
  242. memblock_reserve(CONFIG_VECTORS_BASE, PAGE_SIZE);
  243. #else /* ifndef CONFIG_CPU_V7M */
  244. /*
  245. * There is no dedicated vector page on V7-M. So nothing needs to be
  246. * reserved here.
  247. */
  248. #endif
  249. }
  250. void __init sanity_check_meminfo(void)
  251. {
  252. phys_addr_t end;
  253. sanity_check_meminfo_mpu();
  254. end = bank_phys_end(&meminfo.bank[meminfo.nr_banks - 1]);
  255. high_memory = __va(end - 1) + 1;
  256. }
  257. /*
  258. * early_paging_init() recreates boot time page table setup, allowing machines
  259. * to switch over to a high (>4G) address space on LPAE systems
  260. */
  261. void __init early_paging_init(const struct machine_desc *mdesc,
  262. struct proc_info_list *procinfo)
  263. {
  264. }
  265. /*
  266. * paging_init() sets up the page tables, initialises the zone memory
  267. * maps, and sets up the zero page, bad page and bad page tables.
  268. */
  269. void __init paging_init(const struct machine_desc *mdesc)
  270. {
  271. early_trap_init((void *)CONFIG_VECTORS_BASE);
  272. mpu_setup();
  273. bootmem_init();
  274. }
  275. /*
  276. * We don't need to do anything here for nommu machines.
  277. */
  278. void setup_mm_for_reboot(void)
  279. {
  280. }
  281. void flush_dcache_page(struct page *page)
  282. {
  283. __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
  284. }
  285. EXPORT_SYMBOL(flush_dcache_page);
  286. void flush_kernel_dcache_page(struct page *page)
  287. {
  288. __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
  289. }
  290. EXPORT_SYMBOL(flush_kernel_dcache_page);
  291. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  292. unsigned long uaddr, void *dst, const void *src,
  293. unsigned long len)
  294. {
  295. memcpy(dst, src, len);
  296. if (vma->vm_flags & VM_EXEC)
  297. __cpuc_coherent_user_range(uaddr, uaddr + len);
  298. }
  299. void __iomem *__arm_ioremap_pfn(unsigned long pfn, unsigned long offset,
  300. size_t size, unsigned int mtype)
  301. {
  302. if (pfn >= (0x100000000ULL >> PAGE_SHIFT))
  303. return NULL;
  304. return (void __iomem *) (offset + (pfn << PAGE_SHIFT));
  305. }
  306. EXPORT_SYMBOL(__arm_ioremap_pfn);
  307. void __iomem *__arm_ioremap_pfn_caller(unsigned long pfn, unsigned long offset,
  308. size_t size, unsigned int mtype, void *caller)
  309. {
  310. return __arm_ioremap_pfn(pfn, offset, size, mtype);
  311. }
  312. void __iomem *__arm_ioremap(phys_addr_t phys_addr, size_t size,
  313. unsigned int mtype)
  314. {
  315. return (void __iomem *)phys_addr;
  316. }
  317. EXPORT_SYMBOL(__arm_ioremap);
  318. void __iomem * (*arch_ioremap_caller)(phys_addr_t, size_t, unsigned int, void *);
  319. void __iomem *__arm_ioremap_caller(phys_addr_t phys_addr, size_t size,
  320. unsigned int mtype, void *caller)
  321. {
  322. return __arm_ioremap(phys_addr, size, mtype);
  323. }
  324. void (*arch_iounmap)(volatile void __iomem *);
  325. void __arm_iounmap(volatile void __iomem *addr)
  326. {
  327. }
  328. EXPORT_SYMBOL(__arm_iounmap);