mmu.c 51 KB

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