nommu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 adjust_lowmem_bounds_mpu(void)
  75. {
  76. phys_addr_t phys_offset = PHYS_OFFSET;
  77. phys_addr_t aligned_region_size, specified_mem_size, rounded_mem_size;
  78. struct memblock_region *reg;
  79. bool first = true;
  80. phys_addr_t mem_start;
  81. phys_addr_t mem_end;
  82. for_each_memblock(memory, reg) {
  83. if (first) {
  84. /*
  85. * Initially only use memory continuous from
  86. * PHYS_OFFSET */
  87. if (reg->base != phys_offset)
  88. panic("First memory bank must be contiguous from PHYS_OFFSET");
  89. mem_start = reg->base;
  90. mem_end = reg->base + reg->size;
  91. specified_mem_size = reg->size;
  92. first = false;
  93. } else {
  94. /*
  95. * memblock auto merges contiguous blocks, remove
  96. * all blocks afterwards in one go (we can't remove
  97. * blocks separately while iterating)
  98. */
  99. pr_notice("Ignoring RAM after %pa, memory at %pa ignored\n",
  100. &mem_end, &reg->base);
  101. memblock_remove(reg->base, 0 - reg->base);
  102. break;
  103. }
  104. }
  105. /*
  106. * MPU has curious alignment requirements: Size must be power of 2, and
  107. * region start must be aligned to the region size
  108. */
  109. if (phys_offset != 0)
  110. pr_info("PHYS_OFFSET != 0 => MPU Region size constrained by alignment requirements\n");
  111. /*
  112. * Maximum aligned region might overflow phys_addr_t if phys_offset is
  113. * 0. Hence we keep everything below 4G until we take the smaller of
  114. * the aligned_region_size and rounded_mem_size, one of which is
  115. * guaranteed to be smaller than the maximum physical address.
  116. */
  117. aligned_region_size = (phys_offset - 1) ^ (phys_offset);
  118. /* Find the max power-of-two sized region that fits inside our bank */
  119. rounded_mem_size = (1 << __fls(specified_mem_size)) - 1;
  120. /* The actual region size is the smaller of the two */
  121. aligned_region_size = aligned_region_size < rounded_mem_size
  122. ? aligned_region_size + 1
  123. : rounded_mem_size + 1;
  124. if (aligned_region_size != specified_mem_size) {
  125. pr_warn("Truncating memory from %pa to %pa (MPU region constraints)",
  126. &specified_mem_size, &aligned_region_size);
  127. memblock_remove(mem_start + aligned_region_size,
  128. specified_mem_size - aligned_region_size);
  129. mem_end = mem_start + aligned_region_size;
  130. }
  131. pr_debug("MPU Region from %pa size %pa (end %pa))\n",
  132. &phys_offset, &aligned_region_size, &mem_end);
  133. }
  134. static int mpu_present(void)
  135. {
  136. return ((read_cpuid_ext(CPUID_EXT_MMFR0) & MMFR0_PMSA) == MMFR0_PMSAv7);
  137. }
  138. static int mpu_max_regions(void)
  139. {
  140. /*
  141. * We don't support a different number of I/D side regions so if we
  142. * have separate instruction and data memory maps then return
  143. * whichever side has a smaller number of supported regions.
  144. */
  145. u32 dregions, iregions, mpuir;
  146. mpuir = read_cpuid(CPUID_MPUIR);
  147. dregions = iregions = (mpuir & MPUIR_DREGION_SZMASK) >> MPUIR_DREGION;
  148. /* Check for separate d-side and i-side memory maps */
  149. if (mpuir & MPUIR_nU)
  150. iregions = (mpuir & MPUIR_IREGION_SZMASK) >> MPUIR_IREGION;
  151. /* Use the smallest of the two maxima */
  152. return min(dregions, iregions);
  153. }
  154. static int mpu_iside_independent(void)
  155. {
  156. /* MPUIR.nU specifies whether there is *not* a unified memory map */
  157. return read_cpuid(CPUID_MPUIR) & MPUIR_nU;
  158. }
  159. static int mpu_min_region_order(void)
  160. {
  161. u32 drbar_result, irbar_result;
  162. /* We've kept a region free for this probing */
  163. rgnr_write(MPU_PROBE_REGION);
  164. isb();
  165. /*
  166. * As per ARM ARM, write 0xFFFFFFFC to DRBAR to find the minimum
  167. * region order
  168. */
  169. drbar_write(0xFFFFFFFC);
  170. drbar_result = irbar_result = drbar_read();
  171. drbar_write(0x0);
  172. /* If the MPU is non-unified, we use the larger of the two minima*/
  173. if (mpu_iside_independent()) {
  174. irbar_write(0xFFFFFFFC);
  175. irbar_result = irbar_read();
  176. irbar_write(0x0);
  177. }
  178. isb(); /* Ensure that MPU region operations have completed */
  179. /* Return whichever result is larger */
  180. return __ffs(max(drbar_result, irbar_result));
  181. }
  182. static int mpu_setup_region(unsigned int number, phys_addr_t start,
  183. unsigned int size_order, unsigned int properties)
  184. {
  185. u32 size_data;
  186. /* We kept a region free for probing resolution of MPU regions*/
  187. if (number > mpu_max_regions() || number == MPU_PROBE_REGION)
  188. return -ENOENT;
  189. if (size_order > 32)
  190. return -ENOMEM;
  191. if (size_order < mpu_min_region_order())
  192. return -ENOMEM;
  193. /* Writing N to bits 5:1 (RSR_SZ) specifies region size 2^N+1 */
  194. size_data = ((size_order - 1) << MPU_RSR_SZ) | 1 << MPU_RSR_EN;
  195. dsb(); /* Ensure all previous data accesses occur with old mappings */
  196. rgnr_write(number);
  197. isb();
  198. drbar_write(start);
  199. dracr_write(properties);
  200. isb(); /* Propagate properties before enabling region */
  201. drsr_write(size_data);
  202. /* Check for independent I-side registers */
  203. if (mpu_iside_independent()) {
  204. irbar_write(start);
  205. iracr_write(properties);
  206. isb();
  207. irsr_write(size_data);
  208. }
  209. isb();
  210. /* Store region info (we treat i/d side the same, so only store d) */
  211. mpu_rgn_info.rgns[number].dracr = properties;
  212. mpu_rgn_info.rgns[number].drbar = start;
  213. mpu_rgn_info.rgns[number].drsr = size_data;
  214. return 0;
  215. }
  216. /*
  217. * Set up default MPU regions, doing nothing if there is no MPU
  218. */
  219. void __init mpu_setup(void)
  220. {
  221. int region_err;
  222. if (!mpu_present())
  223. return;
  224. region_err = mpu_setup_region(MPU_RAM_REGION, PHYS_OFFSET,
  225. ilog2(memblock.memory.regions[0].size),
  226. MPU_AP_PL1RW_PL0RW | MPU_RGN_NORMAL);
  227. if (region_err) {
  228. panic("MPU region initialization failure! %d", region_err);
  229. } else {
  230. pr_info("Using ARMv7 PMSA Compliant MPU. "
  231. "Region independence: %s, Max regions: %d\n",
  232. mpu_iside_independent() ? "Yes" : "No",
  233. mpu_max_regions());
  234. }
  235. }
  236. #else
  237. static void adjust_lowmem_bounds_mpu(void) {}
  238. static void __init mpu_setup(void) {}
  239. #endif /* CONFIG_ARM_MPU */
  240. void __init arm_mm_memblock_reserve(void)
  241. {
  242. #ifndef CONFIG_CPU_V7M
  243. /*
  244. * Register the exception vector page.
  245. * some architectures which the DRAM is the exception vector to trap,
  246. * alloc_page breaks with error, although it is not NULL, but "0."
  247. */
  248. memblock_reserve(CONFIG_VECTORS_BASE, 2 * PAGE_SIZE);
  249. #else /* ifndef CONFIG_CPU_V7M */
  250. /*
  251. * There is no dedicated vector page on V7-M. So nothing needs to be
  252. * reserved here.
  253. */
  254. #endif
  255. }
  256. void __init adjust_lowmem_bounds(void)
  257. {
  258. phys_addr_t end;
  259. adjust_lowmem_bounds_mpu();
  260. end = memblock_end_of_DRAM();
  261. high_memory = __va(end - 1) + 1;
  262. memblock_set_current_limit(end);
  263. }
  264. /*
  265. * paging_init() sets up the page tables, initialises the zone memory
  266. * maps, and sets up the zero page, bad page and bad page tables.
  267. */
  268. void __init paging_init(const struct machine_desc *mdesc)
  269. {
  270. early_trap_init((void *)CONFIG_VECTORS_BASE);
  271. mpu_setup();
  272. bootmem_init();
  273. }
  274. /*
  275. * We don't need to do anything here for nommu machines.
  276. */
  277. void setup_mm_for_reboot(void)
  278. {
  279. }
  280. void flush_dcache_page(struct page *page)
  281. {
  282. __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
  283. }
  284. EXPORT_SYMBOL(flush_dcache_page);
  285. void flush_kernel_dcache_page(struct page *page)
  286. {
  287. __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
  288. }
  289. EXPORT_SYMBOL(flush_kernel_dcache_page);
  290. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  291. unsigned long uaddr, void *dst, const void *src,
  292. unsigned long len)
  293. {
  294. memcpy(dst, src, len);
  295. if (vma->vm_flags & VM_EXEC)
  296. __cpuc_coherent_user_range(uaddr, uaddr + len);
  297. }
  298. void __iomem *__arm_ioremap_pfn(unsigned long pfn, unsigned long offset,
  299. size_t size, unsigned int mtype)
  300. {
  301. if (pfn >= (0x100000000ULL >> PAGE_SHIFT))
  302. return NULL;
  303. return (void __iomem *) (offset + (pfn << PAGE_SHIFT));
  304. }
  305. EXPORT_SYMBOL(__arm_ioremap_pfn);
  306. void __iomem *__arm_ioremap_caller(phys_addr_t phys_addr, size_t size,
  307. unsigned int mtype, void *caller)
  308. {
  309. return (void __iomem *)phys_addr;
  310. }
  311. void __iomem * (*arch_ioremap_caller)(phys_addr_t, size_t, unsigned int, void *);
  312. void __iomem *ioremap(resource_size_t res_cookie, size_t size)
  313. {
  314. return __arm_ioremap_caller(res_cookie, size, MT_DEVICE,
  315. __builtin_return_address(0));
  316. }
  317. EXPORT_SYMBOL(ioremap);
  318. void __iomem *ioremap_cache(resource_size_t res_cookie, size_t size)
  319. __alias(ioremap_cached);
  320. void __iomem *ioremap_cached(resource_size_t res_cookie, size_t size)
  321. {
  322. return __arm_ioremap_caller(res_cookie, size, MT_DEVICE_CACHED,
  323. __builtin_return_address(0));
  324. }
  325. EXPORT_SYMBOL(ioremap_cache);
  326. EXPORT_SYMBOL(ioremap_cached);
  327. void __iomem *ioremap_wc(resource_size_t res_cookie, size_t size)
  328. {
  329. return __arm_ioremap_caller(res_cookie, size, MT_DEVICE_WC,
  330. __builtin_return_address(0));
  331. }
  332. EXPORT_SYMBOL(ioremap_wc);
  333. void *arch_memremap_wb(phys_addr_t phys_addr, size_t size)
  334. {
  335. return (void *)phys_addr;
  336. }
  337. void __iounmap(volatile void __iomem *addr)
  338. {
  339. }
  340. EXPORT_SYMBOL(__iounmap);
  341. void (*arch_iounmap)(volatile void __iomem *);
  342. void iounmap(volatile void __iomem *addr)
  343. {
  344. }
  345. EXPORT_SYMBOL(iounmap);