mmu.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /*
  2. * linux/arch/arm/mm/mmu.c
  3. *
  4. * Copyright (C) 1995-2005 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/mman.h>
  15. #include <linux/nodemask.h>
  16. #include <linux/memblock.h>
  17. #include <linux/fs.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/sizes.h>
  20. #include <asm/cp15.h>
  21. #include <asm/cputype.h>
  22. #include <asm/sections.h>
  23. #include <asm/cachetype.h>
  24. #include <asm/fixmap.h>
  25. #include <asm/sections.h>
  26. #include <asm/setup.h>
  27. #include <asm/smp_plat.h>
  28. #include <asm/tlb.h>
  29. #include <asm/highmem.h>
  30. #include <asm/system_info.h>
  31. #include <asm/traps.h>
  32. #include <asm/procinfo.h>
  33. #include <asm/memory.h>
  34. #include <asm/mach/arch.h>
  35. #include <asm/mach/map.h>
  36. #include <asm/mach/pci.h>
  37. #include <asm/fixmap.h>
  38. #include "fault.h"
  39. #include "mm.h"
  40. #include "tcm.h"
  41. /*
  42. * empty_zero_page is a special page that is used for
  43. * zero-initialized data and COW.
  44. */
  45. struct page *empty_zero_page;
  46. EXPORT_SYMBOL(empty_zero_page);
  47. /*
  48. * The pmd table for the upper-most set of pages.
  49. */
  50. pmd_t *top_pmd;
  51. pmdval_t user_pmd_table = _PAGE_USER_TABLE;
  52. #define CPOLICY_UNCACHED 0
  53. #define CPOLICY_BUFFERED 1
  54. #define CPOLICY_WRITETHROUGH 2
  55. #define CPOLICY_WRITEBACK 3
  56. #define CPOLICY_WRITEALLOC 4
  57. static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
  58. static unsigned int ecc_mask __initdata = 0;
  59. pgprot_t pgprot_user;
  60. pgprot_t pgprot_kernel;
  61. pgprot_t pgprot_hyp_device;
  62. pgprot_t pgprot_s2;
  63. pgprot_t pgprot_s2_device;
  64. EXPORT_SYMBOL(pgprot_user);
  65. EXPORT_SYMBOL(pgprot_kernel);
  66. struct cachepolicy {
  67. const char policy[16];
  68. unsigned int cr_mask;
  69. pmdval_t pmd;
  70. pteval_t pte;
  71. pteval_t pte_s2;
  72. };
  73. #ifdef CONFIG_ARM_LPAE
  74. #define s2_policy(policy) policy
  75. #else
  76. #define s2_policy(policy) 0
  77. #endif
  78. static struct cachepolicy cache_policies[] __initdata = {
  79. {
  80. .policy = "uncached",
  81. .cr_mask = CR_W|CR_C,
  82. .pmd = PMD_SECT_UNCACHED,
  83. .pte = L_PTE_MT_UNCACHED,
  84. .pte_s2 = s2_policy(L_PTE_S2_MT_UNCACHED),
  85. }, {
  86. .policy = "buffered",
  87. .cr_mask = CR_C,
  88. .pmd = PMD_SECT_BUFFERED,
  89. .pte = L_PTE_MT_BUFFERABLE,
  90. .pte_s2 = s2_policy(L_PTE_S2_MT_UNCACHED),
  91. }, {
  92. .policy = "writethrough",
  93. .cr_mask = 0,
  94. .pmd = PMD_SECT_WT,
  95. .pte = L_PTE_MT_WRITETHROUGH,
  96. .pte_s2 = s2_policy(L_PTE_S2_MT_WRITETHROUGH),
  97. }, {
  98. .policy = "writeback",
  99. .cr_mask = 0,
  100. .pmd = PMD_SECT_WB,
  101. .pte = L_PTE_MT_WRITEBACK,
  102. .pte_s2 = s2_policy(L_PTE_S2_MT_WRITEBACK),
  103. }, {
  104. .policy = "writealloc",
  105. .cr_mask = 0,
  106. .pmd = PMD_SECT_WBWA,
  107. .pte = L_PTE_MT_WRITEALLOC,
  108. .pte_s2 = s2_policy(L_PTE_S2_MT_WRITEBACK),
  109. }
  110. };
  111. #ifdef CONFIG_CPU_CP15
  112. static unsigned long initial_pmd_value __initdata = 0;
  113. /*
  114. * Initialise the cache_policy variable with the initial state specified
  115. * via the "pmd" value. This is used to ensure that on ARMv6 and later,
  116. * the C code sets the page tables up with the same policy as the head
  117. * assembly code, which avoids an illegal state where the TLBs can get
  118. * confused. See comments in early_cachepolicy() for more information.
  119. */
  120. void __init init_default_cache_policy(unsigned long pmd)
  121. {
  122. int i;
  123. initial_pmd_value = pmd;
  124. pmd &= PMD_SECT_TEX(1) | PMD_SECT_BUFFERABLE | PMD_SECT_CACHEABLE;
  125. for (i = 0; i < ARRAY_SIZE(cache_policies); i++)
  126. if (cache_policies[i].pmd == pmd) {
  127. cachepolicy = i;
  128. break;
  129. }
  130. if (i == ARRAY_SIZE(cache_policies))
  131. pr_err("ERROR: could not find cache policy\n");
  132. }
  133. /*
  134. * These are useful for identifying cache coherency problems by allowing
  135. * the cache or the cache and writebuffer to be turned off. (Note: the
  136. * write buffer should not be on and the cache off).
  137. */
  138. static int __init early_cachepolicy(char *p)
  139. {
  140. int i, selected = -1;
  141. for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
  142. int len = strlen(cache_policies[i].policy);
  143. if (memcmp(p, cache_policies[i].policy, len) == 0) {
  144. selected = i;
  145. break;
  146. }
  147. }
  148. if (selected == -1)
  149. pr_err("ERROR: unknown or unsupported cache policy\n");
  150. /*
  151. * This restriction is partly to do with the way we boot; it is
  152. * unpredictable to have memory mapped using two different sets of
  153. * memory attributes (shared, type, and cache attribs). We can not
  154. * change these attributes once the initial assembly has setup the
  155. * page tables.
  156. */
  157. if (cpu_architecture() >= CPU_ARCH_ARMv6 && selected != cachepolicy) {
  158. pr_warn("Only cachepolicy=%s supported on ARMv6 and later\n",
  159. cache_policies[cachepolicy].policy);
  160. return 0;
  161. }
  162. if (selected != cachepolicy) {
  163. unsigned long cr = __clear_cr(cache_policies[selected].cr_mask);
  164. cachepolicy = selected;
  165. flush_cache_all();
  166. set_cr(cr);
  167. }
  168. return 0;
  169. }
  170. early_param("cachepolicy", early_cachepolicy);
  171. static int __init early_nocache(char *__unused)
  172. {
  173. char *p = "buffered";
  174. pr_warn("nocache is deprecated; use cachepolicy=%s\n", p);
  175. early_cachepolicy(p);
  176. return 0;
  177. }
  178. early_param("nocache", early_nocache);
  179. static int __init early_nowrite(char *__unused)
  180. {
  181. char *p = "uncached";
  182. pr_warn("nowb is deprecated; use cachepolicy=%s\n", p);
  183. early_cachepolicy(p);
  184. return 0;
  185. }
  186. early_param("nowb", early_nowrite);
  187. #ifndef CONFIG_ARM_LPAE
  188. static int __init early_ecc(char *p)
  189. {
  190. if (memcmp(p, "on", 2) == 0)
  191. ecc_mask = PMD_PROTECTION;
  192. else if (memcmp(p, "off", 3) == 0)
  193. ecc_mask = 0;
  194. return 0;
  195. }
  196. early_param("ecc", early_ecc);
  197. #endif
  198. #else /* ifdef CONFIG_CPU_CP15 */
  199. static int __init early_cachepolicy(char *p)
  200. {
  201. pr_warn("cachepolicy kernel parameter not supported without cp15\n");
  202. }
  203. early_param("cachepolicy", early_cachepolicy);
  204. static int __init noalign_setup(char *__unused)
  205. {
  206. pr_warn("noalign kernel parameter not supported without cp15\n");
  207. }
  208. __setup("noalign", noalign_setup);
  209. #endif /* ifdef CONFIG_CPU_CP15 / else */
  210. #define PROT_PTE_DEVICE L_PTE_PRESENT|L_PTE_YOUNG|L_PTE_DIRTY|L_PTE_XN
  211. #define PROT_PTE_S2_DEVICE PROT_PTE_DEVICE
  212. #define PROT_SECT_DEVICE PMD_TYPE_SECT|PMD_SECT_AP_WRITE
  213. static struct mem_type mem_types[] = {
  214. [MT_DEVICE] = { /* Strongly ordered / ARMv6 shared device */
  215. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_SHARED |
  216. L_PTE_SHARED,
  217. .prot_pte_s2 = s2_policy(PROT_PTE_S2_DEVICE) |
  218. s2_policy(L_PTE_S2_MT_DEV_SHARED) |
  219. L_PTE_SHARED,
  220. .prot_l1 = PMD_TYPE_TABLE,
  221. .prot_sect = PROT_SECT_DEVICE | PMD_SECT_S,
  222. .domain = DOMAIN_IO,
  223. },
  224. [MT_DEVICE_NONSHARED] = { /* ARMv6 non-shared device */
  225. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_NONSHARED,
  226. .prot_l1 = PMD_TYPE_TABLE,
  227. .prot_sect = PROT_SECT_DEVICE,
  228. .domain = DOMAIN_IO,
  229. },
  230. [MT_DEVICE_CACHED] = { /* ioremap_cached */
  231. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_CACHED,
  232. .prot_l1 = PMD_TYPE_TABLE,
  233. .prot_sect = PROT_SECT_DEVICE | PMD_SECT_WB,
  234. .domain = DOMAIN_IO,
  235. },
  236. [MT_DEVICE_WC] = { /* ioremap_wc */
  237. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_WC,
  238. .prot_l1 = PMD_TYPE_TABLE,
  239. .prot_sect = PROT_SECT_DEVICE,
  240. .domain = DOMAIN_IO,
  241. },
  242. [MT_UNCACHED] = {
  243. .prot_pte = PROT_PTE_DEVICE,
  244. .prot_l1 = PMD_TYPE_TABLE,
  245. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
  246. .domain = DOMAIN_IO,
  247. },
  248. [MT_CACHECLEAN] = {
  249. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
  250. .domain = DOMAIN_KERNEL,
  251. },
  252. #ifndef CONFIG_ARM_LPAE
  253. [MT_MINICLEAN] = {
  254. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN | PMD_SECT_MINICACHE,
  255. .domain = DOMAIN_KERNEL,
  256. },
  257. #endif
  258. [MT_LOW_VECTORS] = {
  259. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  260. L_PTE_RDONLY,
  261. .prot_l1 = PMD_TYPE_TABLE,
  262. .domain = DOMAIN_VECTORS,
  263. },
  264. [MT_HIGH_VECTORS] = {
  265. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  266. L_PTE_USER | L_PTE_RDONLY,
  267. .prot_l1 = PMD_TYPE_TABLE,
  268. .domain = DOMAIN_VECTORS,
  269. },
  270. [MT_MEMORY_RWX] = {
  271. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY,
  272. .prot_l1 = PMD_TYPE_TABLE,
  273. .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
  274. .domain = DOMAIN_KERNEL,
  275. },
  276. [MT_MEMORY_RW] = {
  277. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  278. L_PTE_XN,
  279. .prot_l1 = PMD_TYPE_TABLE,
  280. .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
  281. .domain = DOMAIN_KERNEL,
  282. },
  283. [MT_ROM] = {
  284. .prot_sect = PMD_TYPE_SECT,
  285. .domain = DOMAIN_KERNEL,
  286. },
  287. [MT_MEMORY_RWX_NONCACHED] = {
  288. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  289. L_PTE_MT_BUFFERABLE,
  290. .prot_l1 = PMD_TYPE_TABLE,
  291. .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
  292. .domain = DOMAIN_KERNEL,
  293. },
  294. [MT_MEMORY_RW_DTCM] = {
  295. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  296. L_PTE_XN,
  297. .prot_l1 = PMD_TYPE_TABLE,
  298. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
  299. .domain = DOMAIN_KERNEL,
  300. },
  301. [MT_MEMORY_RWX_ITCM] = {
  302. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY,
  303. .prot_l1 = PMD_TYPE_TABLE,
  304. .domain = DOMAIN_KERNEL,
  305. },
  306. [MT_MEMORY_RW_SO] = {
  307. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  308. L_PTE_MT_UNCACHED | L_PTE_XN,
  309. .prot_l1 = PMD_TYPE_TABLE,
  310. .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_S |
  311. PMD_SECT_UNCACHED | PMD_SECT_XN,
  312. .domain = DOMAIN_KERNEL,
  313. },
  314. [MT_MEMORY_DMA_READY] = {
  315. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  316. L_PTE_XN,
  317. .prot_l1 = PMD_TYPE_TABLE,
  318. .domain = DOMAIN_KERNEL,
  319. },
  320. };
  321. const struct mem_type *get_mem_type(unsigned int type)
  322. {
  323. return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
  324. }
  325. EXPORT_SYMBOL(get_mem_type);
  326. static pte_t *(*pte_offset_fixmap)(pmd_t *dir, unsigned long addr);
  327. static pte_t bm_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS]
  328. __aligned(PTE_HWTABLE_OFF + PTE_HWTABLE_SIZE) __initdata;
  329. static pte_t * __init pte_offset_early_fixmap(pmd_t *dir, unsigned long addr)
  330. {
  331. return &bm_pte[pte_index(addr)];
  332. }
  333. static pte_t *pte_offset_late_fixmap(pmd_t *dir, unsigned long addr)
  334. {
  335. return pte_offset_kernel(dir, addr);
  336. }
  337. static inline pmd_t * __init fixmap_pmd(unsigned long addr)
  338. {
  339. pgd_t *pgd = pgd_offset_k(addr);
  340. pud_t *pud = pud_offset(pgd, addr);
  341. pmd_t *pmd = pmd_offset(pud, addr);
  342. return pmd;
  343. }
  344. void __init early_fixmap_init(void)
  345. {
  346. pmd_t *pmd;
  347. /*
  348. * The early fixmap range spans multiple pmds, for which
  349. * we are not prepared:
  350. */
  351. BUILD_BUG_ON((__fix_to_virt(__end_of_early_ioremap_region) >> PMD_SHIFT)
  352. != FIXADDR_TOP >> PMD_SHIFT);
  353. pmd = fixmap_pmd(FIXADDR_TOP);
  354. pmd_populate_kernel(&init_mm, pmd, bm_pte);
  355. pte_offset_fixmap = pte_offset_early_fixmap;
  356. }
  357. /*
  358. * To avoid TLB flush broadcasts, this uses local_flush_tlb_kernel_range().
  359. * As a result, this can only be called with preemption disabled, as under
  360. * stop_machine().
  361. */
  362. void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
  363. {
  364. unsigned long vaddr = __fix_to_virt(idx);
  365. pte_t *pte = pte_offset_fixmap(pmd_off_k(vaddr), vaddr);
  366. /* Make sure fixmap region does not exceed available allocation. */
  367. BUILD_BUG_ON(FIXADDR_START + (__end_of_fixed_addresses * PAGE_SIZE) >
  368. FIXADDR_END);
  369. BUG_ON(idx >= __end_of_fixed_addresses);
  370. if (pgprot_val(prot))
  371. set_pte_at(NULL, vaddr, pte,
  372. pfn_pte(phys >> PAGE_SHIFT, prot));
  373. else
  374. pte_clear(NULL, vaddr, pte);
  375. local_flush_tlb_kernel_range(vaddr, vaddr + PAGE_SIZE);
  376. }
  377. /*
  378. * Adjust the PMD section entries according to the CPU in use.
  379. */
  380. static void __init build_mem_type_table(void)
  381. {
  382. struct cachepolicy *cp;
  383. unsigned int cr = get_cr();
  384. pteval_t user_pgprot, kern_pgprot, vecs_pgprot;
  385. pteval_t hyp_device_pgprot, s2_pgprot, s2_device_pgprot;
  386. int cpu_arch = cpu_architecture();
  387. int i;
  388. if (cpu_arch < CPU_ARCH_ARMv6) {
  389. #if defined(CONFIG_CPU_DCACHE_DISABLE)
  390. if (cachepolicy > CPOLICY_BUFFERED)
  391. cachepolicy = CPOLICY_BUFFERED;
  392. #elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
  393. if (cachepolicy > CPOLICY_WRITETHROUGH)
  394. cachepolicy = CPOLICY_WRITETHROUGH;
  395. #endif
  396. }
  397. if (cpu_arch < CPU_ARCH_ARMv5) {
  398. if (cachepolicy >= CPOLICY_WRITEALLOC)
  399. cachepolicy = CPOLICY_WRITEBACK;
  400. ecc_mask = 0;
  401. }
  402. if (is_smp()) {
  403. if (cachepolicy != CPOLICY_WRITEALLOC) {
  404. pr_warn("Forcing write-allocate cache policy for SMP\n");
  405. cachepolicy = CPOLICY_WRITEALLOC;
  406. }
  407. if (!(initial_pmd_value & PMD_SECT_S)) {
  408. pr_warn("Forcing shared mappings for SMP\n");
  409. initial_pmd_value |= PMD_SECT_S;
  410. }
  411. }
  412. /*
  413. * Strip out features not present on earlier architectures.
  414. * Pre-ARMv5 CPUs don't have TEX bits. Pre-ARMv6 CPUs or those
  415. * without extended page tables don't have the 'Shared' bit.
  416. */
  417. if (cpu_arch < CPU_ARCH_ARMv5)
  418. for (i = 0; i < ARRAY_SIZE(mem_types); i++)
  419. mem_types[i].prot_sect &= ~PMD_SECT_TEX(7);
  420. if ((cpu_arch < CPU_ARCH_ARMv6 || !(cr & CR_XP)) && !cpu_is_xsc3())
  421. for (i = 0; i < ARRAY_SIZE(mem_types); i++)
  422. mem_types[i].prot_sect &= ~PMD_SECT_S;
  423. /*
  424. * ARMv5 and lower, bit 4 must be set for page tables (was: cache
  425. * "update-able on write" bit on ARM610). However, Xscale and
  426. * Xscale3 require this bit to be cleared.
  427. */
  428. if (cpu_is_xscale_family()) {
  429. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  430. mem_types[i].prot_sect &= ~PMD_BIT4;
  431. mem_types[i].prot_l1 &= ~PMD_BIT4;
  432. }
  433. } else if (cpu_arch < CPU_ARCH_ARMv6) {
  434. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  435. if (mem_types[i].prot_l1)
  436. mem_types[i].prot_l1 |= PMD_BIT4;
  437. if (mem_types[i].prot_sect)
  438. mem_types[i].prot_sect |= PMD_BIT4;
  439. }
  440. }
  441. /*
  442. * Mark the device areas according to the CPU/architecture.
  443. */
  444. if (cpu_is_xsc3() || (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP))) {
  445. if (!cpu_is_xsc3()) {
  446. /*
  447. * Mark device regions on ARMv6+ as execute-never
  448. * to prevent speculative instruction fetches.
  449. */
  450. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_XN;
  451. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_XN;
  452. mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_XN;
  453. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_XN;
  454. /* Also setup NX memory mapping */
  455. mem_types[MT_MEMORY_RW].prot_sect |= PMD_SECT_XN;
  456. }
  457. if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
  458. /*
  459. * For ARMv7 with TEX remapping,
  460. * - shared device is SXCB=1100
  461. * - nonshared device is SXCB=0100
  462. * - write combine device mem is SXCB=0001
  463. * (Uncached Normal memory)
  464. */
  465. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1);
  466. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(1);
  467. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
  468. } else if (cpu_is_xsc3()) {
  469. /*
  470. * For Xscale3,
  471. * - shared device is TEXCB=00101
  472. * - nonshared device is TEXCB=01000
  473. * - write combine device mem is TEXCB=00100
  474. * (Inner/Outer Uncacheable in xsc3 parlance)
  475. */
  476. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1) | PMD_SECT_BUFFERED;
  477. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
  478. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
  479. } else {
  480. /*
  481. * For ARMv6 and ARMv7 without TEX remapping,
  482. * - shared device is TEXCB=00001
  483. * - nonshared device is TEXCB=01000
  484. * - write combine device mem is TEXCB=00100
  485. * (Uncached Normal in ARMv6 parlance).
  486. */
  487. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_BUFFERED;
  488. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
  489. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
  490. }
  491. } else {
  492. /*
  493. * On others, write combining is "Uncached/Buffered"
  494. */
  495. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
  496. }
  497. /*
  498. * Now deal with the memory-type mappings
  499. */
  500. cp = &cache_policies[cachepolicy];
  501. vecs_pgprot = kern_pgprot = user_pgprot = cp->pte;
  502. s2_pgprot = cp->pte_s2;
  503. hyp_device_pgprot = mem_types[MT_DEVICE].prot_pte;
  504. s2_device_pgprot = mem_types[MT_DEVICE].prot_pte_s2;
  505. #ifndef CONFIG_ARM_LPAE
  506. /*
  507. * We don't use domains on ARMv6 (since this causes problems with
  508. * v6/v7 kernels), so we must use a separate memory type for user
  509. * r/o, kernel r/w to map the vectors page.
  510. */
  511. if (cpu_arch == CPU_ARCH_ARMv6)
  512. vecs_pgprot |= L_PTE_MT_VECTORS;
  513. /*
  514. * Check is it with support for the PXN bit
  515. * in the Short-descriptor translation table format descriptors.
  516. */
  517. if (cpu_arch == CPU_ARCH_ARMv7 &&
  518. (read_cpuid_ext(CPUID_EXT_MMFR0) & 0xF) >= 4) {
  519. user_pmd_table |= PMD_PXNTABLE;
  520. }
  521. #endif
  522. /*
  523. * ARMv6 and above have extended page tables.
  524. */
  525. if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
  526. #ifndef CONFIG_ARM_LPAE
  527. /*
  528. * Mark cache clean areas and XIP ROM read only
  529. * from SVC mode and no access from userspace.
  530. */
  531. mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  532. mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  533. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  534. #endif
  535. /*
  536. * If the initial page tables were created with the S bit
  537. * set, then we need to do the same here for the same
  538. * reasons given in early_cachepolicy().
  539. */
  540. if (initial_pmd_value & PMD_SECT_S) {
  541. user_pgprot |= L_PTE_SHARED;
  542. kern_pgprot |= L_PTE_SHARED;
  543. vecs_pgprot |= L_PTE_SHARED;
  544. s2_pgprot |= L_PTE_SHARED;
  545. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_S;
  546. mem_types[MT_DEVICE_WC].prot_pte |= L_PTE_SHARED;
  547. mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_S;
  548. mem_types[MT_DEVICE_CACHED].prot_pte |= L_PTE_SHARED;
  549. mem_types[MT_MEMORY_RWX].prot_sect |= PMD_SECT_S;
  550. mem_types[MT_MEMORY_RWX].prot_pte |= L_PTE_SHARED;
  551. mem_types[MT_MEMORY_RW].prot_sect |= PMD_SECT_S;
  552. mem_types[MT_MEMORY_RW].prot_pte |= L_PTE_SHARED;
  553. mem_types[MT_MEMORY_DMA_READY].prot_pte |= L_PTE_SHARED;
  554. mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= PMD_SECT_S;
  555. mem_types[MT_MEMORY_RWX_NONCACHED].prot_pte |= L_PTE_SHARED;
  556. }
  557. }
  558. /*
  559. * Non-cacheable Normal - intended for memory areas that must
  560. * not cause dirty cache line writebacks when used
  561. */
  562. if (cpu_arch >= CPU_ARCH_ARMv6) {
  563. if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
  564. /* Non-cacheable Normal is XCB = 001 */
  565. mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |=
  566. PMD_SECT_BUFFERED;
  567. } else {
  568. /* For both ARMv6 and non-TEX-remapping ARMv7 */
  569. mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |=
  570. PMD_SECT_TEX(1);
  571. }
  572. } else {
  573. mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= PMD_SECT_BUFFERABLE;
  574. }
  575. #ifdef CONFIG_ARM_LPAE
  576. /*
  577. * Do not generate access flag faults for the kernel mappings.
  578. */
  579. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  580. mem_types[i].prot_pte |= PTE_EXT_AF;
  581. if (mem_types[i].prot_sect)
  582. mem_types[i].prot_sect |= PMD_SECT_AF;
  583. }
  584. kern_pgprot |= PTE_EXT_AF;
  585. vecs_pgprot |= PTE_EXT_AF;
  586. /*
  587. * Set PXN for user mappings
  588. */
  589. user_pgprot |= PTE_EXT_PXN;
  590. #endif
  591. for (i = 0; i < 16; i++) {
  592. pteval_t v = pgprot_val(protection_map[i]);
  593. protection_map[i] = __pgprot(v | user_pgprot);
  594. }
  595. mem_types[MT_LOW_VECTORS].prot_pte |= vecs_pgprot;
  596. mem_types[MT_HIGH_VECTORS].prot_pte |= vecs_pgprot;
  597. pgprot_user = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | user_pgprot);
  598. pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
  599. L_PTE_DIRTY | kern_pgprot);
  600. pgprot_s2 = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | s2_pgprot);
  601. pgprot_s2_device = __pgprot(s2_device_pgprot);
  602. pgprot_hyp_device = __pgprot(hyp_device_pgprot);
  603. mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
  604. mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
  605. mem_types[MT_MEMORY_RWX].prot_sect |= ecc_mask | cp->pmd;
  606. mem_types[MT_MEMORY_RWX].prot_pte |= kern_pgprot;
  607. mem_types[MT_MEMORY_RW].prot_sect |= ecc_mask | cp->pmd;
  608. mem_types[MT_MEMORY_RW].prot_pte |= kern_pgprot;
  609. mem_types[MT_MEMORY_DMA_READY].prot_pte |= kern_pgprot;
  610. mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= ecc_mask;
  611. mem_types[MT_ROM].prot_sect |= cp->pmd;
  612. switch (cp->pmd) {
  613. case PMD_SECT_WT:
  614. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
  615. break;
  616. case PMD_SECT_WB:
  617. case PMD_SECT_WBWA:
  618. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
  619. break;
  620. }
  621. pr_info("Memory policy: %sData cache %s\n",
  622. ecc_mask ? "ECC enabled, " : "", cp->policy);
  623. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  624. struct mem_type *t = &mem_types[i];
  625. if (t->prot_l1)
  626. t->prot_l1 |= PMD_DOMAIN(t->domain);
  627. if (t->prot_sect)
  628. t->prot_sect |= PMD_DOMAIN(t->domain);
  629. }
  630. }
  631. #ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
  632. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  633. unsigned long size, pgprot_t vma_prot)
  634. {
  635. if (!pfn_valid(pfn))
  636. return pgprot_noncached(vma_prot);
  637. else if (file->f_flags & O_SYNC)
  638. return pgprot_writecombine(vma_prot);
  639. return vma_prot;
  640. }
  641. EXPORT_SYMBOL(phys_mem_access_prot);
  642. #endif
  643. #define vectors_base() (vectors_high() ? 0xffff0000 : 0)
  644. static void __init *early_alloc_aligned(unsigned long sz, unsigned long align)
  645. {
  646. void *ptr = __va(memblock_alloc(sz, align));
  647. memset(ptr, 0, sz);
  648. return ptr;
  649. }
  650. static void __init *early_alloc(unsigned long sz)
  651. {
  652. return early_alloc_aligned(sz, sz);
  653. }
  654. static void *__init late_alloc(unsigned long sz)
  655. {
  656. void *ptr = (void *)__get_free_pages(PGALLOC_GFP, get_order(sz));
  657. BUG_ON(!ptr);
  658. return ptr;
  659. }
  660. static pte_t * __init pte_alloc(pmd_t *pmd, unsigned long addr,
  661. unsigned long prot,
  662. void *(*alloc)(unsigned long sz))
  663. {
  664. if (pmd_none(*pmd)) {
  665. pte_t *pte = alloc(PTE_HWTABLE_OFF + PTE_HWTABLE_SIZE);
  666. __pmd_populate(pmd, __pa(pte), prot);
  667. }
  668. BUG_ON(pmd_bad(*pmd));
  669. return pte_offset_kernel(pmd, addr);
  670. }
  671. static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr,
  672. unsigned long prot)
  673. {
  674. return pte_alloc(pmd, addr, prot, early_alloc);
  675. }
  676. static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
  677. unsigned long end, unsigned long pfn,
  678. const struct mem_type *type,
  679. void *(*alloc)(unsigned long sz),
  680. bool ng)
  681. {
  682. pte_t *pte = pte_alloc(pmd, addr, type->prot_l1, alloc);
  683. do {
  684. set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)),
  685. ng ? PTE_EXT_NG : 0);
  686. pfn++;
  687. } while (pte++, addr += PAGE_SIZE, addr != end);
  688. }
  689. static void __init __map_init_section(pmd_t *pmd, unsigned long addr,
  690. unsigned long end, phys_addr_t phys,
  691. const struct mem_type *type, bool ng)
  692. {
  693. pmd_t *p = pmd;
  694. #ifndef CONFIG_ARM_LPAE
  695. /*
  696. * In classic MMU format, puds and pmds are folded in to
  697. * the pgds. pmd_offset gives the PGD entry. PGDs refer to a
  698. * group of L1 entries making up one logical pointer to
  699. * an L2 table (2MB), where as PMDs refer to the individual
  700. * L1 entries (1MB). Hence increment to get the correct
  701. * offset for odd 1MB sections.
  702. * (See arch/arm/include/asm/pgtable-2level.h)
  703. */
  704. if (addr & SECTION_SIZE)
  705. pmd++;
  706. #endif
  707. do {
  708. *pmd = __pmd(phys | type->prot_sect | (ng ? PMD_SECT_nG : 0));
  709. phys += SECTION_SIZE;
  710. } while (pmd++, addr += SECTION_SIZE, addr != end);
  711. flush_pmd_entry(p);
  712. }
  713. static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
  714. unsigned long end, phys_addr_t phys,
  715. const struct mem_type *type,
  716. void *(*alloc)(unsigned long sz), bool ng)
  717. {
  718. pmd_t *pmd = pmd_offset(pud, addr);
  719. unsigned long next;
  720. do {
  721. /*
  722. * With LPAE, we must loop over to map
  723. * all the pmds for the given range.
  724. */
  725. next = pmd_addr_end(addr, end);
  726. /*
  727. * Try a section mapping - addr, next and phys must all be
  728. * aligned to a section boundary.
  729. */
  730. if (type->prot_sect &&
  731. ((addr | next | phys) & ~SECTION_MASK) == 0) {
  732. __map_init_section(pmd, addr, next, phys, type, ng);
  733. } else {
  734. alloc_init_pte(pmd, addr, next,
  735. __phys_to_pfn(phys), type, alloc, ng);
  736. }
  737. phys += next - addr;
  738. } while (pmd++, addr = next, addr != end);
  739. }
  740. static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
  741. unsigned long end, phys_addr_t phys,
  742. const struct mem_type *type,
  743. void *(*alloc)(unsigned long sz), bool ng)
  744. {
  745. pud_t *pud = pud_offset(pgd, addr);
  746. unsigned long next;
  747. do {
  748. next = pud_addr_end(addr, end);
  749. alloc_init_pmd(pud, addr, next, phys, type, alloc, ng);
  750. phys += next - addr;
  751. } while (pud++, addr = next, addr != end);
  752. }
  753. #ifndef CONFIG_ARM_LPAE
  754. static void __init create_36bit_mapping(struct mm_struct *mm,
  755. struct map_desc *md,
  756. const struct mem_type *type,
  757. bool ng)
  758. {
  759. unsigned long addr, length, end;
  760. phys_addr_t phys;
  761. pgd_t *pgd;
  762. addr = md->virtual;
  763. phys = __pfn_to_phys(md->pfn);
  764. length = PAGE_ALIGN(md->length);
  765. if (!(cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())) {
  766. pr_err("MM: CPU does not support supersection mapping for 0x%08llx at 0x%08lx\n",
  767. (long long)__pfn_to_phys((u64)md->pfn), addr);
  768. return;
  769. }
  770. /* N.B. ARMv6 supersections are only defined to work with domain 0.
  771. * Since domain assignments can in fact be arbitrary, the
  772. * 'domain == 0' check below is required to insure that ARMv6
  773. * supersections are only allocated for domain 0 regardless
  774. * of the actual domain assignments in use.
  775. */
  776. if (type->domain) {
  777. pr_err("MM: invalid domain in supersection mapping for 0x%08llx at 0x%08lx\n",
  778. (long long)__pfn_to_phys((u64)md->pfn), addr);
  779. return;
  780. }
  781. if ((addr | length | __pfn_to_phys(md->pfn)) & ~SUPERSECTION_MASK) {
  782. pr_err("MM: cannot create mapping for 0x%08llx at 0x%08lx invalid alignment\n",
  783. (long long)__pfn_to_phys((u64)md->pfn), addr);
  784. return;
  785. }
  786. /*
  787. * Shift bits [35:32] of address into bits [23:20] of PMD
  788. * (See ARMv6 spec).
  789. */
  790. phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
  791. pgd = pgd_offset(mm, addr);
  792. end = addr + length;
  793. do {
  794. pud_t *pud = pud_offset(pgd, addr);
  795. pmd_t *pmd = pmd_offset(pud, addr);
  796. int i;
  797. for (i = 0; i < 16; i++)
  798. *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER |
  799. (ng ? PMD_SECT_nG : 0));
  800. addr += SUPERSECTION_SIZE;
  801. phys += SUPERSECTION_SIZE;
  802. pgd += SUPERSECTION_SIZE >> PGDIR_SHIFT;
  803. } while (addr != end);
  804. }
  805. #endif /* !CONFIG_ARM_LPAE */
  806. static void __init __create_mapping(struct mm_struct *mm, struct map_desc *md,
  807. void *(*alloc)(unsigned long sz),
  808. bool ng)
  809. {
  810. unsigned long addr, length, end;
  811. phys_addr_t phys;
  812. const struct mem_type *type;
  813. pgd_t *pgd;
  814. type = &mem_types[md->type];
  815. #ifndef CONFIG_ARM_LPAE
  816. /*
  817. * Catch 36-bit addresses
  818. */
  819. if (md->pfn >= 0x100000) {
  820. create_36bit_mapping(mm, md, type, ng);
  821. return;
  822. }
  823. #endif
  824. addr = md->virtual & PAGE_MASK;
  825. phys = __pfn_to_phys(md->pfn);
  826. length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
  827. if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
  828. pr_warn("BUG: map for 0x%08llx at 0x%08lx can not be mapped using pages, ignoring.\n",
  829. (long long)__pfn_to_phys(md->pfn), addr);
  830. return;
  831. }
  832. pgd = pgd_offset(mm, addr);
  833. end = addr + length;
  834. do {
  835. unsigned long next = pgd_addr_end(addr, end);
  836. alloc_init_pud(pgd, addr, next, phys, type, alloc, ng);
  837. phys += next - addr;
  838. addr = next;
  839. } while (pgd++, addr != end);
  840. }
  841. /*
  842. * Create the page directory entries and any necessary
  843. * page tables for the mapping specified by `md'. We
  844. * are able to cope here with varying sizes and address
  845. * offsets, and we take full advantage of sections and
  846. * supersections.
  847. */
  848. static void __init create_mapping(struct map_desc *md)
  849. {
  850. if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
  851. pr_warn("BUG: not creating mapping for 0x%08llx at 0x%08lx in user region\n",
  852. (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
  853. return;
  854. }
  855. if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
  856. md->virtual >= PAGE_OFFSET && md->virtual < FIXADDR_START &&
  857. (md->virtual < VMALLOC_START || md->virtual >= VMALLOC_END)) {
  858. pr_warn("BUG: mapping for 0x%08llx at 0x%08lx out of vmalloc space\n",
  859. (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
  860. }
  861. __create_mapping(&init_mm, md, early_alloc, false);
  862. }
  863. void __init create_mapping_late(struct mm_struct *mm, struct map_desc *md,
  864. bool ng)
  865. {
  866. #ifdef CONFIG_ARM_LPAE
  867. pud_t *pud = pud_alloc(mm, pgd_offset(mm, md->virtual), md->virtual);
  868. if (WARN_ON(!pud))
  869. return;
  870. pmd_alloc(mm, pud, 0);
  871. #endif
  872. __create_mapping(mm, md, late_alloc, ng);
  873. }
  874. /*
  875. * Create the architecture specific mappings
  876. */
  877. void __init iotable_init(struct map_desc *io_desc, int nr)
  878. {
  879. struct map_desc *md;
  880. struct vm_struct *vm;
  881. struct static_vm *svm;
  882. if (!nr)
  883. return;
  884. svm = early_alloc_aligned(sizeof(*svm) * nr, __alignof__(*svm));
  885. for (md = io_desc; nr; md++, nr--) {
  886. create_mapping(md);
  887. vm = &svm->vm;
  888. vm->addr = (void *)(md->virtual & PAGE_MASK);
  889. vm->size = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
  890. vm->phys_addr = __pfn_to_phys(md->pfn);
  891. vm->flags = VM_IOREMAP | VM_ARM_STATIC_MAPPING;
  892. vm->flags |= VM_ARM_MTYPE(md->type);
  893. vm->caller = iotable_init;
  894. add_static_vm_early(svm++);
  895. }
  896. }
  897. void __init vm_reserve_area_early(unsigned long addr, unsigned long size,
  898. void *caller)
  899. {
  900. struct vm_struct *vm;
  901. struct static_vm *svm;
  902. svm = early_alloc_aligned(sizeof(*svm), __alignof__(*svm));
  903. vm = &svm->vm;
  904. vm->addr = (void *)addr;
  905. vm->size = size;
  906. vm->flags = VM_IOREMAP | VM_ARM_EMPTY_MAPPING;
  907. vm->caller = caller;
  908. add_static_vm_early(svm);
  909. }
  910. #ifndef CONFIG_ARM_LPAE
  911. /*
  912. * The Linux PMD is made of two consecutive section entries covering 2MB
  913. * (see definition in include/asm/pgtable-2level.h). However a call to
  914. * create_mapping() may optimize static mappings by using individual
  915. * 1MB section mappings. This leaves the actual PMD potentially half
  916. * initialized if the top or bottom section entry isn't used, leaving it
  917. * open to problems if a subsequent ioremap() or vmalloc() tries to use
  918. * the virtual space left free by that unused section entry.
  919. *
  920. * Let's avoid the issue by inserting dummy vm entries covering the unused
  921. * PMD halves once the static mappings are in place.
  922. */
  923. static void __init pmd_empty_section_gap(unsigned long addr)
  924. {
  925. vm_reserve_area_early(addr, SECTION_SIZE, pmd_empty_section_gap);
  926. }
  927. static void __init fill_pmd_gaps(void)
  928. {
  929. struct static_vm *svm;
  930. struct vm_struct *vm;
  931. unsigned long addr, next = 0;
  932. pmd_t *pmd;
  933. list_for_each_entry(svm, &static_vmlist, list) {
  934. vm = &svm->vm;
  935. addr = (unsigned long)vm->addr;
  936. if (addr < next)
  937. continue;
  938. /*
  939. * Check if this vm starts on an odd section boundary.
  940. * If so and the first section entry for this PMD is free
  941. * then we block the corresponding virtual address.
  942. */
  943. if ((addr & ~PMD_MASK) == SECTION_SIZE) {
  944. pmd = pmd_off_k(addr);
  945. if (pmd_none(*pmd))
  946. pmd_empty_section_gap(addr & PMD_MASK);
  947. }
  948. /*
  949. * Then check if this vm ends on an odd section boundary.
  950. * If so and the second section entry for this PMD is empty
  951. * then we block the corresponding virtual address.
  952. */
  953. addr += vm->size;
  954. if ((addr & ~PMD_MASK) == SECTION_SIZE) {
  955. pmd = pmd_off_k(addr) + 1;
  956. if (pmd_none(*pmd))
  957. pmd_empty_section_gap(addr);
  958. }
  959. /* no need to look at any vm entry until we hit the next PMD */
  960. next = (addr + PMD_SIZE - 1) & PMD_MASK;
  961. }
  962. }
  963. #else
  964. #define fill_pmd_gaps() do { } while (0)
  965. #endif
  966. #if defined(CONFIG_PCI) && !defined(CONFIG_NEED_MACH_IO_H)
  967. static void __init pci_reserve_io(void)
  968. {
  969. struct static_vm *svm;
  970. svm = find_static_vm_vaddr((void *)PCI_IO_VIRT_BASE);
  971. if (svm)
  972. return;
  973. vm_reserve_area_early(PCI_IO_VIRT_BASE, SZ_2M, pci_reserve_io);
  974. }
  975. #else
  976. #define pci_reserve_io() do { } while (0)
  977. #endif
  978. #ifdef CONFIG_DEBUG_LL
  979. void __init debug_ll_io_init(void)
  980. {
  981. struct map_desc map;
  982. debug_ll_addr(&map.pfn, &map.virtual);
  983. if (!map.pfn || !map.virtual)
  984. return;
  985. map.pfn = __phys_to_pfn(map.pfn);
  986. map.virtual &= PAGE_MASK;
  987. map.length = PAGE_SIZE;
  988. map.type = MT_DEVICE;
  989. iotable_init(&map, 1);
  990. }
  991. #endif
  992. static void * __initdata vmalloc_min =
  993. (void *)(VMALLOC_END - (240 << 20) - VMALLOC_OFFSET);
  994. /*
  995. * vmalloc=size forces the vmalloc area to be exactly 'size'
  996. * bytes. This can be used to increase (or decrease) the vmalloc
  997. * area - the default is 240m.
  998. */
  999. static int __init early_vmalloc(char *arg)
  1000. {
  1001. unsigned long vmalloc_reserve = memparse(arg, NULL);
  1002. if (vmalloc_reserve < SZ_16M) {
  1003. vmalloc_reserve = SZ_16M;
  1004. pr_warn("vmalloc area too small, limiting to %luMB\n",
  1005. vmalloc_reserve >> 20);
  1006. }
  1007. if (vmalloc_reserve > VMALLOC_END - (PAGE_OFFSET + SZ_32M)) {
  1008. vmalloc_reserve = VMALLOC_END - (PAGE_OFFSET + SZ_32M);
  1009. pr_warn("vmalloc area is too big, limiting to %luMB\n",
  1010. vmalloc_reserve >> 20);
  1011. }
  1012. vmalloc_min = (void *)(VMALLOC_END - vmalloc_reserve);
  1013. return 0;
  1014. }
  1015. early_param("vmalloc", early_vmalloc);
  1016. phys_addr_t arm_lowmem_limit __initdata = 0;
  1017. void __init sanity_check_meminfo(void)
  1018. {
  1019. phys_addr_t memblock_limit = 0;
  1020. int highmem = 0;
  1021. phys_addr_t vmalloc_limit = __pa(vmalloc_min - 1) + 1;
  1022. struct memblock_region *reg;
  1023. bool should_use_highmem = false;
  1024. for_each_memblock(memory, reg) {
  1025. phys_addr_t block_start = reg->base;
  1026. phys_addr_t block_end = reg->base + reg->size;
  1027. phys_addr_t size_limit = reg->size;
  1028. if (reg->base >= vmalloc_limit)
  1029. highmem = 1;
  1030. else
  1031. size_limit = vmalloc_limit - reg->base;
  1032. if (!IS_ENABLED(CONFIG_HIGHMEM) || cache_is_vipt_aliasing()) {
  1033. if (highmem) {
  1034. pr_notice("Ignoring RAM at %pa-%pa (!CONFIG_HIGHMEM)\n",
  1035. &block_start, &block_end);
  1036. memblock_remove(reg->base, reg->size);
  1037. should_use_highmem = true;
  1038. continue;
  1039. }
  1040. if (reg->size > size_limit) {
  1041. phys_addr_t overlap_size = reg->size - size_limit;
  1042. pr_notice("Truncating RAM at %pa-%pa to -%pa",
  1043. &block_start, &block_end, &vmalloc_limit);
  1044. memblock_remove(vmalloc_limit, overlap_size);
  1045. block_end = vmalloc_limit;
  1046. should_use_highmem = true;
  1047. }
  1048. }
  1049. if (!highmem) {
  1050. if (block_end > arm_lowmem_limit) {
  1051. if (reg->size > size_limit)
  1052. arm_lowmem_limit = vmalloc_limit;
  1053. else
  1054. arm_lowmem_limit = block_end;
  1055. }
  1056. /*
  1057. * Find the first non-pmd-aligned page, and point
  1058. * memblock_limit at it. This relies on rounding the
  1059. * limit down to be pmd-aligned, which happens at the
  1060. * end of this function.
  1061. *
  1062. * With this algorithm, the start or end of almost any
  1063. * bank can be non-pmd-aligned. The only exception is
  1064. * that the start of the bank 0 must be section-
  1065. * aligned, since otherwise memory would need to be
  1066. * allocated when mapping the start of bank 0, which
  1067. * occurs before any free memory is mapped.
  1068. */
  1069. if (!memblock_limit) {
  1070. if (!IS_ALIGNED(block_start, PMD_SIZE))
  1071. memblock_limit = block_start;
  1072. else if (!IS_ALIGNED(block_end, PMD_SIZE))
  1073. memblock_limit = arm_lowmem_limit;
  1074. }
  1075. }
  1076. }
  1077. if (should_use_highmem)
  1078. pr_notice("Consider using a HIGHMEM enabled kernel.\n");
  1079. high_memory = __va(arm_lowmem_limit - 1) + 1;
  1080. /*
  1081. * Round the memblock limit down to a pmd size. This
  1082. * helps to ensure that we will allocate memory from the
  1083. * last full pmd, which should be mapped.
  1084. */
  1085. if (memblock_limit)
  1086. memblock_limit = round_down(memblock_limit, PMD_SIZE);
  1087. if (!memblock_limit)
  1088. memblock_limit = arm_lowmem_limit;
  1089. memblock_set_current_limit(memblock_limit);
  1090. }
  1091. static inline void prepare_page_table(void)
  1092. {
  1093. unsigned long addr;
  1094. phys_addr_t end;
  1095. /*
  1096. * Clear out all the mappings below the kernel image.
  1097. */
  1098. for (addr = 0; addr < MODULES_VADDR; addr += PMD_SIZE)
  1099. pmd_clear(pmd_off_k(addr));
  1100. #ifdef CONFIG_XIP_KERNEL
  1101. /* The XIP kernel is mapped in the module area -- skip over it */
  1102. addr = ((unsigned long)_etext + PMD_SIZE - 1) & PMD_MASK;
  1103. #endif
  1104. for ( ; addr < PAGE_OFFSET; addr += PMD_SIZE)
  1105. pmd_clear(pmd_off_k(addr));
  1106. /*
  1107. * Find the end of the first block of lowmem.
  1108. */
  1109. end = memblock.memory.regions[0].base + memblock.memory.regions[0].size;
  1110. if (end >= arm_lowmem_limit)
  1111. end = arm_lowmem_limit;
  1112. /*
  1113. * Clear out all the kernel space mappings, except for the first
  1114. * memory bank, up to the vmalloc region.
  1115. */
  1116. for (addr = __phys_to_virt(end);
  1117. addr < VMALLOC_START; addr += PMD_SIZE)
  1118. pmd_clear(pmd_off_k(addr));
  1119. }
  1120. #ifdef CONFIG_ARM_LPAE
  1121. /* the first page is reserved for pgd */
  1122. #define SWAPPER_PG_DIR_SIZE (PAGE_SIZE + \
  1123. PTRS_PER_PGD * PTRS_PER_PMD * sizeof(pmd_t))
  1124. #else
  1125. #define SWAPPER_PG_DIR_SIZE (PTRS_PER_PGD * sizeof(pgd_t))
  1126. #endif
  1127. /*
  1128. * Reserve the special regions of memory
  1129. */
  1130. void __init arm_mm_memblock_reserve(void)
  1131. {
  1132. /*
  1133. * Reserve the page tables. These are already in use,
  1134. * and can only be in node 0.
  1135. */
  1136. memblock_reserve(__pa(swapper_pg_dir), SWAPPER_PG_DIR_SIZE);
  1137. #ifdef CONFIG_SA1111
  1138. /*
  1139. * Because of the SA1111 DMA bug, we want to preserve our
  1140. * precious DMA-able memory...
  1141. */
  1142. memblock_reserve(PHYS_OFFSET, __pa(swapper_pg_dir) - PHYS_OFFSET);
  1143. #endif
  1144. }
  1145. /*
  1146. * Set up the device mappings. Since we clear out the page tables for all
  1147. * mappings above VMALLOC_START, except early fixmap, we might remove debug
  1148. * device mappings. This means earlycon can be used to debug this function
  1149. * Any other function or debugging method which may touch any device _will_
  1150. * crash the kernel.
  1151. */
  1152. static void __init devicemaps_init(const struct machine_desc *mdesc)
  1153. {
  1154. struct map_desc map;
  1155. unsigned long addr;
  1156. void *vectors;
  1157. /*
  1158. * Allocate the vector page early.
  1159. */
  1160. vectors = early_alloc(PAGE_SIZE * 2);
  1161. early_trap_init(vectors);
  1162. /*
  1163. * Clear page table except top pmd used by early fixmaps
  1164. */
  1165. for (addr = VMALLOC_START; addr < (FIXADDR_TOP & PMD_MASK); addr += PMD_SIZE)
  1166. pmd_clear(pmd_off_k(addr));
  1167. /*
  1168. * Map the kernel if it is XIP.
  1169. * It is always first in the modulearea.
  1170. */
  1171. #ifdef CONFIG_XIP_KERNEL
  1172. map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
  1173. map.virtual = MODULES_VADDR;
  1174. map.length = ((unsigned long)_etext - map.virtual + ~SECTION_MASK) & SECTION_MASK;
  1175. map.type = MT_ROM;
  1176. create_mapping(&map);
  1177. #endif
  1178. /*
  1179. * Map the cache flushing regions.
  1180. */
  1181. #ifdef FLUSH_BASE
  1182. map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
  1183. map.virtual = FLUSH_BASE;
  1184. map.length = SZ_1M;
  1185. map.type = MT_CACHECLEAN;
  1186. create_mapping(&map);
  1187. #endif
  1188. #ifdef FLUSH_BASE_MINICACHE
  1189. map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
  1190. map.virtual = FLUSH_BASE_MINICACHE;
  1191. map.length = SZ_1M;
  1192. map.type = MT_MINICLEAN;
  1193. create_mapping(&map);
  1194. #endif
  1195. /*
  1196. * Create a mapping for the machine vectors at the high-vectors
  1197. * location (0xffff0000). If we aren't using high-vectors, also
  1198. * create a mapping at the low-vectors virtual address.
  1199. */
  1200. map.pfn = __phys_to_pfn(virt_to_phys(vectors));
  1201. map.virtual = 0xffff0000;
  1202. map.length = PAGE_SIZE;
  1203. #ifdef CONFIG_KUSER_HELPERS
  1204. map.type = MT_HIGH_VECTORS;
  1205. #else
  1206. map.type = MT_LOW_VECTORS;
  1207. #endif
  1208. create_mapping(&map);
  1209. if (!vectors_high()) {
  1210. map.virtual = 0;
  1211. map.length = PAGE_SIZE * 2;
  1212. map.type = MT_LOW_VECTORS;
  1213. create_mapping(&map);
  1214. }
  1215. /* Now create a kernel read-only mapping */
  1216. map.pfn += 1;
  1217. map.virtual = 0xffff0000 + PAGE_SIZE;
  1218. map.length = PAGE_SIZE;
  1219. map.type = MT_LOW_VECTORS;
  1220. create_mapping(&map);
  1221. /*
  1222. * Ask the machine support to map in the statically mapped devices.
  1223. */
  1224. if (mdesc->map_io)
  1225. mdesc->map_io();
  1226. else
  1227. debug_ll_io_init();
  1228. fill_pmd_gaps();
  1229. /* Reserve fixed i/o space in VMALLOC region */
  1230. pci_reserve_io();
  1231. /*
  1232. * Finally flush the caches and tlb to ensure that we're in a
  1233. * consistent state wrt the writebuffer. This also ensures that
  1234. * any write-allocated cache lines in the vector page are written
  1235. * back. After this point, we can start to touch devices again.
  1236. */
  1237. local_flush_tlb_all();
  1238. flush_cache_all();
  1239. /* Enable asynchronous aborts */
  1240. early_abt_enable();
  1241. }
  1242. static void __init kmap_init(void)
  1243. {
  1244. #ifdef CONFIG_HIGHMEM
  1245. pkmap_page_table = early_pte_alloc(pmd_off_k(PKMAP_BASE),
  1246. PKMAP_BASE, _PAGE_KERNEL_TABLE);
  1247. #endif
  1248. early_pte_alloc(pmd_off_k(FIXADDR_START), FIXADDR_START,
  1249. _PAGE_KERNEL_TABLE);
  1250. }
  1251. static void __init map_lowmem(void)
  1252. {
  1253. struct memblock_region *reg;
  1254. phys_addr_t kernel_x_start = round_down(__pa(_stext), SECTION_SIZE);
  1255. phys_addr_t kernel_x_end = round_up(__pa(__init_end), SECTION_SIZE);
  1256. /* Map all the lowmem memory banks. */
  1257. for_each_memblock(memory, reg) {
  1258. phys_addr_t start = reg->base;
  1259. phys_addr_t end = start + reg->size;
  1260. struct map_desc map;
  1261. if (memblock_is_nomap(reg))
  1262. continue;
  1263. if (end > arm_lowmem_limit)
  1264. end = arm_lowmem_limit;
  1265. if (start >= end)
  1266. break;
  1267. if (end < kernel_x_start) {
  1268. map.pfn = __phys_to_pfn(start);
  1269. map.virtual = __phys_to_virt(start);
  1270. map.length = end - start;
  1271. map.type = MT_MEMORY_RWX;
  1272. create_mapping(&map);
  1273. } else if (start >= kernel_x_end) {
  1274. map.pfn = __phys_to_pfn(start);
  1275. map.virtual = __phys_to_virt(start);
  1276. map.length = end - start;
  1277. map.type = MT_MEMORY_RW;
  1278. create_mapping(&map);
  1279. } else {
  1280. /* This better cover the entire kernel */
  1281. if (start < kernel_x_start) {
  1282. map.pfn = __phys_to_pfn(start);
  1283. map.virtual = __phys_to_virt(start);
  1284. map.length = kernel_x_start - start;
  1285. map.type = MT_MEMORY_RW;
  1286. create_mapping(&map);
  1287. }
  1288. map.pfn = __phys_to_pfn(kernel_x_start);
  1289. map.virtual = __phys_to_virt(kernel_x_start);
  1290. map.length = kernel_x_end - kernel_x_start;
  1291. map.type = MT_MEMORY_RWX;
  1292. create_mapping(&map);
  1293. if (kernel_x_end < end) {
  1294. map.pfn = __phys_to_pfn(kernel_x_end);
  1295. map.virtual = __phys_to_virt(kernel_x_end);
  1296. map.length = end - kernel_x_end;
  1297. map.type = MT_MEMORY_RW;
  1298. create_mapping(&map);
  1299. }
  1300. }
  1301. }
  1302. }
  1303. #ifdef CONFIG_ARM_PV_FIXUP
  1304. extern unsigned long __atags_pointer;
  1305. typedef void pgtables_remap(long long offset, unsigned long pgd, void *bdata);
  1306. pgtables_remap lpae_pgtables_remap_asm;
  1307. /*
  1308. * early_paging_init() recreates boot time page table setup, allowing machines
  1309. * to switch over to a high (>4G) address space on LPAE systems
  1310. */
  1311. void __init early_paging_init(const struct machine_desc *mdesc)
  1312. {
  1313. pgtables_remap *lpae_pgtables_remap;
  1314. unsigned long pa_pgd;
  1315. unsigned int cr, ttbcr;
  1316. long long offset;
  1317. void *boot_data;
  1318. if (!mdesc->pv_fixup)
  1319. return;
  1320. offset = mdesc->pv_fixup();
  1321. if (offset == 0)
  1322. return;
  1323. /*
  1324. * Get the address of the remap function in the 1:1 identity
  1325. * mapping setup by the early page table assembly code. We
  1326. * must get this prior to the pv update. The following barrier
  1327. * ensures that this is complete before we fixup any P:V offsets.
  1328. */
  1329. lpae_pgtables_remap = (pgtables_remap *)(unsigned long)__pa(lpae_pgtables_remap_asm);
  1330. pa_pgd = __pa(swapper_pg_dir);
  1331. boot_data = __va(__atags_pointer);
  1332. barrier();
  1333. pr_info("Switching physical address space to 0x%08llx\n",
  1334. (u64)PHYS_OFFSET + offset);
  1335. /* Re-set the phys pfn offset, and the pv offset */
  1336. __pv_offset += offset;
  1337. __pv_phys_pfn_offset += PFN_DOWN(offset);
  1338. /* Run the patch stub to update the constants */
  1339. fixup_pv_table(&__pv_table_begin,
  1340. (&__pv_table_end - &__pv_table_begin) << 2);
  1341. /*
  1342. * We changing not only the virtual to physical mapping, but also
  1343. * the physical addresses used to access memory. We need to flush
  1344. * all levels of cache in the system with caching disabled to
  1345. * ensure that all data is written back, and nothing is prefetched
  1346. * into the caches. We also need to prevent the TLB walkers
  1347. * allocating into the caches too. Note that this is ARMv7 LPAE
  1348. * specific.
  1349. */
  1350. cr = get_cr();
  1351. set_cr(cr & ~(CR_I | CR_C));
  1352. asm("mrc p15, 0, %0, c2, c0, 2" : "=r" (ttbcr));
  1353. asm volatile("mcr p15, 0, %0, c2, c0, 2"
  1354. : : "r" (ttbcr & ~(3 << 8 | 3 << 10)));
  1355. flush_cache_all();
  1356. /*
  1357. * Fixup the page tables - this must be in the idmap region as
  1358. * we need to disable the MMU to do this safely, and hence it
  1359. * needs to be assembly. It's fairly simple, as we're using the
  1360. * temporary tables setup by the initial assembly code.
  1361. */
  1362. lpae_pgtables_remap(offset, pa_pgd, boot_data);
  1363. /* Re-enable the caches and cacheable TLB walks */
  1364. asm volatile("mcr p15, 0, %0, c2, c0, 2" : : "r" (ttbcr));
  1365. set_cr(cr);
  1366. }
  1367. #else
  1368. void __init early_paging_init(const struct machine_desc *mdesc)
  1369. {
  1370. long long offset;
  1371. if (!mdesc->pv_fixup)
  1372. return;
  1373. offset = mdesc->pv_fixup();
  1374. if (offset == 0)
  1375. return;
  1376. pr_crit("Physical address space modification is only to support Keystone2.\n");
  1377. pr_crit("Please enable ARM_LPAE and ARM_PATCH_PHYS_VIRT support to use this\n");
  1378. pr_crit("feature. Your kernel may crash now, have a good day.\n");
  1379. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  1380. }
  1381. #endif
  1382. static void __init early_fixmap_shutdown(void)
  1383. {
  1384. int i;
  1385. unsigned long va = fix_to_virt(__end_of_permanent_fixed_addresses - 1);
  1386. pte_offset_fixmap = pte_offset_late_fixmap;
  1387. pmd_clear(fixmap_pmd(va));
  1388. local_flush_tlb_kernel_page(va);
  1389. for (i = 0; i < __end_of_permanent_fixed_addresses; i++) {
  1390. pte_t *pte;
  1391. struct map_desc map;
  1392. map.virtual = fix_to_virt(i);
  1393. pte = pte_offset_early_fixmap(pmd_off_k(map.virtual), map.virtual);
  1394. /* Only i/o device mappings are supported ATM */
  1395. if (pte_none(*pte) ||
  1396. (pte_val(*pte) & L_PTE_MT_MASK) != L_PTE_MT_DEV_SHARED)
  1397. continue;
  1398. map.pfn = pte_pfn(*pte);
  1399. map.type = MT_DEVICE;
  1400. map.length = PAGE_SIZE;
  1401. create_mapping(&map);
  1402. }
  1403. }
  1404. /*
  1405. * paging_init() sets up the page tables, initialises the zone memory
  1406. * maps, and sets up the zero page, bad page and bad page tables.
  1407. */
  1408. void __init paging_init(const struct machine_desc *mdesc)
  1409. {
  1410. void *zero_page;
  1411. build_mem_type_table();
  1412. prepare_page_table();
  1413. map_lowmem();
  1414. memblock_set_current_limit(arm_lowmem_limit);
  1415. dma_contiguous_remap();
  1416. early_fixmap_shutdown();
  1417. devicemaps_init(mdesc);
  1418. kmap_init();
  1419. tcm_init();
  1420. top_pmd = pmd_off_k(0xffff0000);
  1421. /* allocate the zero page. */
  1422. zero_page = early_alloc(PAGE_SIZE);
  1423. bootmem_init();
  1424. empty_zero_page = virt_to_page(zero_page);
  1425. __flush_dcache_page(NULL, empty_zero_page);
  1426. }