mmu.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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. unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
  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. return memblock_alloc_or_panic(sz, sz);
  658. }
  659. static void *__init late_alloc(unsigned long sz)
  660. {
  661. void *ptdesc = pagetable_alloc(GFP_PGTABLE_KERNEL & ~__GFP_HIGHMEM,
  662. get_order(sz));
  663. if (!ptdesc || !pagetable_pte_ctor(NULL, ptdesc))
  664. BUG();
  665. return ptdesc_address(ptdesc);
  666. }
  667. static pte_t * __init arm_pte_alloc(pmd_t *pmd, unsigned long addr,
  668. unsigned long prot,
  669. void *(*alloc)(unsigned long sz))
  670. {
  671. if (pmd_none(*pmd)) {
  672. pte_t *pte = alloc(PTE_HWTABLE_OFF + PTE_HWTABLE_SIZE);
  673. __pmd_populate(pmd, __pa(pte), prot);
  674. }
  675. BUG_ON(pmd_bad(*pmd));
  676. return pte_offset_kernel(pmd, addr);
  677. }
  678. static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr,
  679. unsigned long prot)
  680. {
  681. return arm_pte_alloc(pmd, addr, prot, early_alloc);
  682. }
  683. static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
  684. unsigned long end, unsigned long pfn,
  685. const struct mem_type *type,
  686. void *(*alloc)(unsigned long sz),
  687. bool ng)
  688. {
  689. pte_t *pte = arm_pte_alloc(pmd, addr, type->prot_l1, alloc);
  690. do {
  691. set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)),
  692. ng ? PTE_EXT_NG : 0);
  693. pfn++;
  694. } while (pte++, addr += PAGE_SIZE, addr != end);
  695. }
  696. static void __init __map_init_section(pmd_t *pmd, unsigned long addr,
  697. unsigned long end, phys_addr_t phys,
  698. const struct mem_type *type, bool ng)
  699. {
  700. pmd_t *p = pmd;
  701. #ifndef CONFIG_ARM_LPAE
  702. /*
  703. * In classic MMU format, puds and pmds are folded in to
  704. * the pgds. pmd_offset gives the PGD entry. PGDs refer to a
  705. * group of L1 entries making up one logical pointer to
  706. * an L2 table (2MB), where as PMDs refer to the individual
  707. * L1 entries (1MB). Hence increment to get the correct
  708. * offset for odd 1MB sections.
  709. * (See arch/arm/include/asm/pgtable-2level.h)
  710. */
  711. if (addr & SECTION_SIZE)
  712. pmd++;
  713. #endif
  714. do {
  715. *pmd = __pmd(phys | type->prot_sect | (ng ? PMD_SECT_nG : 0));
  716. phys += SECTION_SIZE;
  717. } while (pmd++, addr += SECTION_SIZE, addr != end);
  718. flush_pmd_entry(p);
  719. }
  720. static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
  721. unsigned long end, phys_addr_t phys,
  722. const struct mem_type *type,
  723. void *(*alloc)(unsigned long sz), bool ng)
  724. {
  725. pmd_t *pmd = pmd_offset(pud, addr);
  726. unsigned long next;
  727. do {
  728. /*
  729. * With LPAE, we must loop over to map
  730. * all the pmds for the given range.
  731. */
  732. next = pmd_addr_end(addr, end);
  733. /*
  734. * Try a section mapping - addr, next and phys must all be
  735. * aligned to a section boundary.
  736. */
  737. if (type->prot_sect &&
  738. ((addr | next | phys) & ~SECTION_MASK) == 0) {
  739. __map_init_section(pmd, addr, next, phys, type, ng);
  740. } else {
  741. alloc_init_pte(pmd, addr, next,
  742. __phys_to_pfn(phys), type, alloc, ng);
  743. }
  744. phys += next - addr;
  745. } while (pmd++, addr = next, addr != end);
  746. }
  747. static void __init alloc_init_pud(p4d_t *p4d, unsigned long addr,
  748. unsigned long end, phys_addr_t phys,
  749. const struct mem_type *type,
  750. void *(*alloc)(unsigned long sz), bool ng)
  751. {
  752. pud_t *pud = pud_offset(p4d, addr);
  753. unsigned long next;
  754. do {
  755. next = pud_addr_end(addr, end);
  756. alloc_init_pmd(pud, addr, next, phys, type, alloc, ng);
  757. phys += next - addr;
  758. } while (pud++, addr = next, addr != end);
  759. }
  760. static void __init alloc_init_p4d(pgd_t *pgd, unsigned long addr,
  761. unsigned long end, phys_addr_t phys,
  762. const struct mem_type *type,
  763. void *(*alloc)(unsigned long sz), bool ng)
  764. {
  765. p4d_t *p4d = p4d_offset(pgd, addr);
  766. unsigned long next;
  767. do {
  768. next = p4d_addr_end(addr, end);
  769. alloc_init_pud(p4d, addr, next, phys, type, alloc, ng);
  770. phys += next - addr;
  771. } while (p4d++, addr = next, addr != end);
  772. }
  773. #ifndef CONFIG_ARM_LPAE
  774. static void __init create_36bit_mapping(struct mm_struct *mm,
  775. struct map_desc *md,
  776. const struct mem_type *type,
  777. bool ng)
  778. {
  779. unsigned long addr, length, end;
  780. phys_addr_t phys;
  781. pgd_t *pgd;
  782. addr = md->virtual;
  783. phys = __pfn_to_phys(md->pfn);
  784. length = PAGE_ALIGN(md->length);
  785. if (!(cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())) {
  786. pr_err("MM: CPU does not support supersection mapping for 0x%08llx at 0x%08lx\n",
  787. (long long)__pfn_to_phys((u64)md->pfn), addr);
  788. return;
  789. }
  790. /* N.B. ARMv6 supersections are only defined to work with domain 0.
  791. * Since domain assignments can in fact be arbitrary, the
  792. * 'domain == 0' check below is required to insure that ARMv6
  793. * supersections are only allocated for domain 0 regardless
  794. * of the actual domain assignments in use.
  795. */
  796. if (type->domain) {
  797. pr_err("MM: invalid domain in supersection mapping for 0x%08llx at 0x%08lx\n",
  798. (long long)__pfn_to_phys((u64)md->pfn), addr);
  799. return;
  800. }
  801. if ((addr | length | __pfn_to_phys(md->pfn)) & ~SUPERSECTION_MASK) {
  802. pr_err("MM: cannot create mapping for 0x%08llx at 0x%08lx invalid alignment\n",
  803. (long long)__pfn_to_phys((u64)md->pfn), addr);
  804. return;
  805. }
  806. /*
  807. * Shift bits [35:32] of address into bits [23:20] of PMD
  808. * (See ARMv6 spec).
  809. */
  810. phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
  811. pgd = pgd_offset(mm, addr);
  812. end = addr + length;
  813. do {
  814. p4d_t *p4d = p4d_offset(pgd, addr);
  815. pud_t *pud = pud_offset(p4d, addr);
  816. pmd_t *pmd = pmd_offset(pud, addr);
  817. int i;
  818. for (i = 0; i < 16; i++)
  819. *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER |
  820. (ng ? PMD_SECT_nG : 0));
  821. addr += SUPERSECTION_SIZE;
  822. phys += SUPERSECTION_SIZE;
  823. pgd += SUPERSECTION_SIZE >> PGDIR_SHIFT;
  824. } while (addr != end);
  825. }
  826. #endif /* !CONFIG_ARM_LPAE */
  827. static void __init __create_mapping(struct mm_struct *mm, struct map_desc *md,
  828. void *(*alloc)(unsigned long sz),
  829. bool ng)
  830. {
  831. unsigned long addr, length, end;
  832. phys_addr_t phys;
  833. const struct mem_type *type;
  834. pgd_t *pgd;
  835. type = &mem_types[md->type];
  836. #ifndef CONFIG_ARM_LPAE
  837. /*
  838. * Catch 36-bit addresses
  839. */
  840. if (md->pfn >= 0x100000) {
  841. create_36bit_mapping(mm, md, type, ng);
  842. return;
  843. }
  844. #endif
  845. addr = md->virtual & PAGE_MASK;
  846. phys = __pfn_to_phys(md->pfn);
  847. length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
  848. if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
  849. pr_warn("BUG: map for 0x%08llx at 0x%08lx can not be mapped using pages, ignoring.\n",
  850. (long long)__pfn_to_phys(md->pfn), addr);
  851. return;
  852. }
  853. pgd = pgd_offset(mm, addr);
  854. end = addr + length;
  855. do {
  856. unsigned long next = pgd_addr_end(addr, end);
  857. alloc_init_p4d(pgd, addr, next, phys, type, alloc, ng);
  858. phys += next - addr;
  859. addr = next;
  860. } while (pgd++, addr != end);
  861. }
  862. /*
  863. * Create the page directory entries and any necessary
  864. * page tables for the mapping specified by `md'. We
  865. * are able to cope here with varying sizes and address
  866. * offsets, and we take full advantage of sections and
  867. * supersections.
  868. */
  869. static void __init create_mapping(struct map_desc *md)
  870. {
  871. if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
  872. pr_warn("BUG: not creating mapping for 0x%08llx at 0x%08lx in user region\n",
  873. (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
  874. return;
  875. }
  876. if (md->type == MT_DEVICE &&
  877. md->virtual >= PAGE_OFFSET && md->virtual < FIXADDR_START &&
  878. (md->virtual < VMALLOC_START || md->virtual >= VMALLOC_END)) {
  879. pr_warn("BUG: mapping for 0x%08llx at 0x%08lx out of vmalloc space\n",
  880. (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
  881. }
  882. __create_mapping(&init_mm, md, early_alloc, false);
  883. }
  884. void __init create_mapping_late(struct mm_struct *mm, struct map_desc *md,
  885. bool ng)
  886. {
  887. #ifdef CONFIG_ARM_LPAE
  888. p4d_t *p4d;
  889. pud_t *pud;
  890. p4d = p4d_alloc(mm, pgd_offset(mm, md->virtual), md->virtual);
  891. if (WARN_ON(!p4d))
  892. return;
  893. pud = pud_alloc(mm, p4d, md->virtual);
  894. if (WARN_ON(!pud))
  895. return;
  896. pmd_alloc(mm, pud, 0);
  897. #endif
  898. __create_mapping(mm, md, late_alloc, ng);
  899. }
  900. /*
  901. * Create the architecture specific mappings
  902. */
  903. void __init iotable_init(struct map_desc *io_desc, int nr)
  904. {
  905. struct map_desc *md;
  906. struct vm_struct *vm;
  907. struct static_vm *svm;
  908. if (!nr)
  909. return;
  910. svm = memblock_alloc_or_panic(sizeof(*svm) * nr, __alignof__(*svm));
  911. for (md = io_desc; nr; md++, nr--) {
  912. create_mapping(md);
  913. vm = &svm->vm;
  914. vm->addr = (void *)(md->virtual & PAGE_MASK);
  915. vm->size = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
  916. vm->phys_addr = __pfn_to_phys(md->pfn);
  917. vm->flags = VM_IOREMAP | VM_ARM_STATIC_MAPPING;
  918. vm->flags |= VM_ARM_MTYPE(md->type);
  919. vm->caller = iotable_init;
  920. add_static_vm_early(svm++);
  921. }
  922. }
  923. void __init vm_reserve_area_early(unsigned long addr, unsigned long size,
  924. void *caller)
  925. {
  926. struct vm_struct *vm;
  927. struct static_vm *svm;
  928. svm = memblock_alloc_or_panic(sizeof(*svm), __alignof__(*svm));
  929. vm = &svm->vm;
  930. vm->addr = (void *)addr;
  931. vm->size = size;
  932. vm->flags = VM_IOREMAP | VM_ARM_EMPTY_MAPPING;
  933. vm->caller = caller;
  934. add_static_vm_early(svm);
  935. }
  936. #ifndef CONFIG_ARM_LPAE
  937. /*
  938. * The Linux PMD is made of two consecutive section entries covering 2MB
  939. * (see definition in include/asm/pgtable-2level.h). However a call to
  940. * create_mapping() may optimize static mappings by using individual
  941. * 1MB section mappings. This leaves the actual PMD potentially half
  942. * initialized if the top or bottom section entry isn't used, leaving it
  943. * open to problems if a subsequent ioremap() or vmalloc() tries to use
  944. * the virtual space left free by that unused section entry.
  945. *
  946. * Let's avoid the issue by inserting dummy vm entries covering the unused
  947. * PMD halves once the static mappings are in place.
  948. */
  949. static void __init pmd_empty_section_gap(unsigned long addr)
  950. {
  951. vm_reserve_area_early(addr, SECTION_SIZE, pmd_empty_section_gap);
  952. }
  953. static void __init fill_pmd_gaps(void)
  954. {
  955. struct static_vm *svm;
  956. struct vm_struct *vm;
  957. unsigned long addr, next = 0;
  958. pmd_t *pmd;
  959. list_for_each_entry(svm, &static_vmlist, list) {
  960. vm = &svm->vm;
  961. addr = (unsigned long)vm->addr;
  962. if (addr < next)
  963. continue;
  964. /*
  965. * Check if this vm starts on an odd section boundary.
  966. * If so and the first section entry for this PMD is free
  967. * then we block the corresponding virtual address.
  968. */
  969. if ((addr & ~PMD_MASK) == SECTION_SIZE) {
  970. pmd = pmd_off_k(addr);
  971. if (pmd_none(*pmd))
  972. pmd_empty_section_gap(addr & PMD_MASK);
  973. }
  974. /*
  975. * Then check if this vm ends on an odd section boundary.
  976. * If so and the second section entry for this PMD is empty
  977. * then we block the corresponding virtual address.
  978. */
  979. addr += vm->size;
  980. if ((addr & ~PMD_MASK) == SECTION_SIZE) {
  981. pmd = pmd_off_k(addr) + 1;
  982. if (pmd_none(*pmd))
  983. pmd_empty_section_gap(addr);
  984. }
  985. /* no need to look at any vm entry until we hit the next PMD */
  986. next = (addr + PMD_SIZE - 1) & PMD_MASK;
  987. }
  988. }
  989. #else
  990. #define fill_pmd_gaps() do { } while (0)
  991. #endif
  992. #if defined(CONFIG_PCI) && !defined(CONFIG_NEED_MACH_IO_H)
  993. static void __init pci_reserve_io(void)
  994. {
  995. struct static_vm *svm;
  996. svm = find_static_vm_vaddr((void *)PCI_IO_VIRT_BASE);
  997. if (svm)
  998. return;
  999. vm_reserve_area_early(PCI_IO_VIRT_BASE, SZ_2M, pci_reserve_io);
  1000. }
  1001. #else
  1002. #define pci_reserve_io() do { } while (0)
  1003. #endif
  1004. #ifdef CONFIG_DEBUG_LL
  1005. void __init debug_ll_io_init(void)
  1006. {
  1007. struct map_desc map;
  1008. debug_ll_addr(&map.pfn, &map.virtual);
  1009. if (!map.pfn || !map.virtual)
  1010. return;
  1011. map.pfn = __phys_to_pfn(map.pfn);
  1012. map.virtual &= PAGE_MASK;
  1013. map.length = PAGE_SIZE;
  1014. map.type = MT_DEVICE;
  1015. iotable_init(&map, 1);
  1016. }
  1017. #endif
  1018. static unsigned long __initdata vmalloc_size = 240 * SZ_1M;
  1019. /*
  1020. * vmalloc=size forces the vmalloc area to be exactly 'size'
  1021. * bytes. This can be used to increase (or decrease) the vmalloc
  1022. * area - the default is 240MiB.
  1023. */
  1024. static int __init early_vmalloc(char *arg)
  1025. {
  1026. unsigned long vmalloc_reserve = memparse(arg, NULL);
  1027. unsigned long vmalloc_max;
  1028. if (vmalloc_reserve < SZ_16M) {
  1029. vmalloc_reserve = SZ_16M;
  1030. pr_warn("vmalloc area is too small, limiting to %luMiB\n",
  1031. vmalloc_reserve >> 20);
  1032. }
  1033. vmalloc_max = VMALLOC_END - (PAGE_OFFSET + SZ_32M + VMALLOC_OFFSET);
  1034. if (vmalloc_reserve > vmalloc_max) {
  1035. vmalloc_reserve = vmalloc_max;
  1036. pr_warn("vmalloc area is too big, limiting to %luMiB\n",
  1037. vmalloc_reserve >> 20);
  1038. }
  1039. vmalloc_size = vmalloc_reserve;
  1040. return 0;
  1041. }
  1042. early_param("vmalloc", early_vmalloc);
  1043. phys_addr_t arm_lowmem_limit __initdata = 0;
  1044. void __init adjust_lowmem_bounds(void)
  1045. {
  1046. phys_addr_t block_start, block_end, memblock_limit = 0;
  1047. u64 vmalloc_limit, i;
  1048. phys_addr_t lowmem_limit = 0;
  1049. /*
  1050. * Let's use our own (unoptimized) equivalent of __pa() that is
  1051. * not affected by wrap-arounds when sizeof(phys_addr_t) == 4.
  1052. * The result is used as the upper bound on physical memory address
  1053. * and may itself be outside the valid range for which phys_addr_t
  1054. * and therefore __pa() is defined.
  1055. */
  1056. vmalloc_limit = (u64)VMALLOC_END - vmalloc_size - VMALLOC_OFFSET -
  1057. PAGE_OFFSET + PHYS_OFFSET;
  1058. /*
  1059. * The first usable region must be PMD aligned. Mark its start
  1060. * as MEMBLOCK_NOMAP if it isn't
  1061. */
  1062. for_each_mem_range(i, &block_start, &block_end) {
  1063. if (!IS_ALIGNED(block_start, PMD_SIZE)) {
  1064. phys_addr_t len;
  1065. len = round_up(block_start, PMD_SIZE) - block_start;
  1066. memblock_mark_nomap(block_start, len);
  1067. }
  1068. break;
  1069. }
  1070. for_each_mem_range(i, &block_start, &block_end) {
  1071. if (block_start < vmalloc_limit) {
  1072. if (block_end > lowmem_limit)
  1073. /*
  1074. * Compare as u64 to ensure vmalloc_limit does
  1075. * not get truncated. block_end should always
  1076. * fit in phys_addr_t so there should be no
  1077. * issue with assignment.
  1078. */
  1079. lowmem_limit = min_t(u64,
  1080. vmalloc_limit,
  1081. block_end);
  1082. /*
  1083. * Find the first non-pmd-aligned page, and point
  1084. * memblock_limit at it. This relies on rounding the
  1085. * limit down to be pmd-aligned, which happens at the
  1086. * end of this function.
  1087. *
  1088. * With this algorithm, the start or end of almost any
  1089. * bank can be non-pmd-aligned. The only exception is
  1090. * that the start of the bank 0 must be section-
  1091. * aligned, since otherwise memory would need to be
  1092. * allocated when mapping the start of bank 0, which
  1093. * occurs before any free memory is mapped.
  1094. */
  1095. if (!memblock_limit) {
  1096. if (!IS_ALIGNED(block_start, PMD_SIZE))
  1097. memblock_limit = block_start;
  1098. else if (!IS_ALIGNED(block_end, PMD_SIZE))
  1099. memblock_limit = lowmem_limit;
  1100. }
  1101. }
  1102. }
  1103. arm_lowmem_limit = lowmem_limit;
  1104. high_memory = __va(arm_lowmem_limit - 1) + 1;
  1105. if (!memblock_limit)
  1106. memblock_limit = arm_lowmem_limit;
  1107. /*
  1108. * Round the memblock limit down to a pmd size. This
  1109. * helps to ensure that we will allocate memory from the
  1110. * last full pmd, which should be mapped.
  1111. */
  1112. memblock_limit = round_down(memblock_limit, PMD_SIZE);
  1113. if (!IS_ENABLED(CONFIG_HIGHMEM) || cache_is_vipt_aliasing()) {
  1114. if (memblock_end_of_DRAM() > arm_lowmem_limit) {
  1115. phys_addr_t end = memblock_end_of_DRAM();
  1116. pr_notice("Ignoring RAM at %pa-%pa\n",
  1117. &memblock_limit, &end);
  1118. pr_notice("Consider using a HIGHMEM enabled kernel.\n");
  1119. memblock_remove(memblock_limit, end - memblock_limit);
  1120. }
  1121. }
  1122. memblock_set_current_limit(memblock_limit);
  1123. }
  1124. static __init void prepare_page_table(void)
  1125. {
  1126. unsigned long addr;
  1127. phys_addr_t end;
  1128. /*
  1129. * Clear out all the mappings below the kernel image.
  1130. */
  1131. #ifdef CONFIG_KASAN
  1132. /*
  1133. * KASan's shadow memory inserts itself between the TASK_SIZE
  1134. * and MODULES_VADDR. Do not clear the KASan shadow memory mappings.
  1135. */
  1136. for (addr = 0; addr < KASAN_SHADOW_START; addr += PMD_SIZE)
  1137. pmd_clear(pmd_off_k(addr));
  1138. /*
  1139. * Skip over the KASan shadow area. KASAN_SHADOW_END is sometimes
  1140. * equal to MODULES_VADDR and then we exit the pmd clearing. If we
  1141. * are using a thumb-compiled kernel, there there will be 8MB more
  1142. * to clear as KASan always offset to 16 MB below MODULES_VADDR.
  1143. */
  1144. for (addr = KASAN_SHADOW_END; addr < MODULES_VADDR; addr += PMD_SIZE)
  1145. pmd_clear(pmd_off_k(addr));
  1146. #else
  1147. for (addr = 0; addr < MODULES_VADDR; addr += PMD_SIZE)
  1148. pmd_clear(pmd_off_k(addr));
  1149. #endif
  1150. #ifdef CONFIG_XIP_KERNEL
  1151. /* The XIP kernel is mapped in the module area -- skip over it */
  1152. addr = ((unsigned long)_exiprom + PMD_SIZE - 1) & PMD_MASK;
  1153. #endif
  1154. for ( ; addr < PAGE_OFFSET; addr += PMD_SIZE)
  1155. pmd_clear(pmd_off_k(addr));
  1156. /*
  1157. * Find the end of the first block of lowmem.
  1158. */
  1159. end = memblock.memory.regions[0].base + memblock.memory.regions[0].size;
  1160. if (end >= arm_lowmem_limit)
  1161. end = arm_lowmem_limit;
  1162. /*
  1163. * Clear out all the kernel space mappings, except for the first
  1164. * memory bank, up to the vmalloc region.
  1165. */
  1166. for (addr = __phys_to_virt(end);
  1167. addr < VMALLOC_START; addr += PMD_SIZE)
  1168. pmd_clear(pmd_off_k(addr));
  1169. }
  1170. #ifdef CONFIG_ARM_LPAE
  1171. /* the first page is reserved for pgd */
  1172. #define SWAPPER_PG_DIR_SIZE (PAGE_SIZE + \
  1173. PTRS_PER_PGD * PTRS_PER_PMD * sizeof(pmd_t))
  1174. #else
  1175. #define SWAPPER_PG_DIR_SIZE (PTRS_PER_PGD * sizeof(pgd_t))
  1176. #endif
  1177. /*
  1178. * Reserve the special regions of memory
  1179. */
  1180. void __init arm_mm_memblock_reserve(void)
  1181. {
  1182. /*
  1183. * Reserve the page tables. These are already in use,
  1184. * and can only be in node 0.
  1185. */
  1186. memblock_reserve(__pa(swapper_pg_dir), SWAPPER_PG_DIR_SIZE);
  1187. #ifdef CONFIG_SA1111
  1188. /*
  1189. * Because of the SA1111 DMA bug, we want to preserve our
  1190. * precious DMA-able memory...
  1191. */
  1192. memblock_reserve(PHYS_OFFSET, __pa(swapper_pg_dir) - PHYS_OFFSET);
  1193. #endif
  1194. }
  1195. /*
  1196. * Set up the device mappings. Since we clear out the page tables for all
  1197. * mappings above VMALLOC_START, except early fixmap, we might remove debug
  1198. * device mappings. This means earlycon can be used to debug this function
  1199. * Any other function or debugging method which may touch any device _will_
  1200. * crash the kernel.
  1201. */
  1202. static void __init devicemaps_init(const struct machine_desc *mdesc)
  1203. {
  1204. struct map_desc map;
  1205. unsigned long addr;
  1206. void *vectors;
  1207. /*
  1208. * Allocate the vector page early.
  1209. */
  1210. vectors = early_alloc(PAGE_SIZE * 2);
  1211. early_trap_init(vectors);
  1212. /*
  1213. * Clear page table except top pmd used by early fixmaps
  1214. */
  1215. for (addr = VMALLOC_START; addr < (FIXADDR_TOP & PMD_MASK); addr += PMD_SIZE)
  1216. pmd_clear(pmd_off_k(addr));
  1217. if (__atags_pointer) {
  1218. /* create a read-only mapping of the device tree */
  1219. map.pfn = __phys_to_pfn(__atags_pointer & SECTION_MASK);
  1220. map.virtual = FDT_FIXED_BASE;
  1221. map.length = FDT_FIXED_SIZE;
  1222. map.type = MT_MEMORY_RO;
  1223. create_mapping(&map);
  1224. }
  1225. /*
  1226. * Map the cache flushing regions.
  1227. */
  1228. #ifdef FLUSH_BASE
  1229. map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
  1230. map.virtual = FLUSH_BASE;
  1231. map.length = SZ_1M;
  1232. map.type = MT_CACHECLEAN;
  1233. create_mapping(&map);
  1234. #endif
  1235. #ifdef FLUSH_BASE_MINICACHE
  1236. map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
  1237. map.virtual = FLUSH_BASE_MINICACHE;
  1238. map.length = SZ_1M;
  1239. map.type = MT_MINICLEAN;
  1240. create_mapping(&map);
  1241. #endif
  1242. /*
  1243. * Create a mapping for the machine vectors at the high-vectors
  1244. * location (0xffff0000). If we aren't using high-vectors, also
  1245. * create a mapping at the low-vectors virtual address.
  1246. */
  1247. map.pfn = __phys_to_pfn(virt_to_phys(vectors));
  1248. map.virtual = 0xffff0000;
  1249. map.length = PAGE_SIZE;
  1250. #ifdef CONFIG_KUSER_HELPERS
  1251. map.type = MT_HIGH_VECTORS;
  1252. #else
  1253. map.type = MT_LOW_VECTORS;
  1254. #endif
  1255. create_mapping(&map);
  1256. if (!vectors_high()) {
  1257. map.virtual = 0;
  1258. map.length = PAGE_SIZE * 2;
  1259. map.type = MT_LOW_VECTORS;
  1260. create_mapping(&map);
  1261. }
  1262. /* Now create a kernel read-only mapping */
  1263. map.pfn += 1;
  1264. map.virtual = 0xffff0000 + PAGE_SIZE;
  1265. map.length = PAGE_SIZE;
  1266. map.type = MT_LOW_VECTORS;
  1267. create_mapping(&map);
  1268. /*
  1269. * Ask the machine support to map in the statically mapped devices.
  1270. */
  1271. if (mdesc->map_io)
  1272. mdesc->map_io();
  1273. else
  1274. debug_ll_io_init();
  1275. fill_pmd_gaps();
  1276. /* Reserve fixed i/o space in VMALLOC region */
  1277. pci_reserve_io();
  1278. /*
  1279. * Finally flush the caches and tlb to ensure that we're in a
  1280. * consistent state wrt the writebuffer. This also ensures that
  1281. * any write-allocated cache lines in the vector page are written
  1282. * back. After this point, we can start to touch devices again.
  1283. */
  1284. local_flush_tlb_all();
  1285. flush_cache_all();
  1286. /* Enable asynchronous aborts */
  1287. early_abt_enable();
  1288. }
  1289. static void __init kmap_init(void)
  1290. {
  1291. #ifdef CONFIG_HIGHMEM
  1292. pkmap_page_table = early_pte_alloc(pmd_off_k(PKMAP_BASE),
  1293. PKMAP_BASE, _PAGE_KERNEL_TABLE);
  1294. #endif
  1295. early_pte_alloc(pmd_off_k(FIXADDR_START), FIXADDR_START,
  1296. _PAGE_KERNEL_TABLE);
  1297. }
  1298. static void __init map_lowmem(void)
  1299. {
  1300. phys_addr_t start, end;
  1301. u64 i;
  1302. /* Map all the lowmem memory banks. */
  1303. for_each_mem_range(i, &start, &end) {
  1304. struct map_desc map;
  1305. pr_debug("map lowmem start: 0x%08llx, end: 0x%08llx\n",
  1306. (long long)start, (long long)end);
  1307. if (end > arm_lowmem_limit)
  1308. end = arm_lowmem_limit;
  1309. if (start >= end)
  1310. break;
  1311. /*
  1312. * If our kernel image is in the VMALLOC area we need to remove
  1313. * the kernel physical memory from lowmem since the kernel will
  1314. * be mapped separately.
  1315. *
  1316. * The kernel will typically be at the very start of lowmem,
  1317. * but any placement relative to memory ranges is possible.
  1318. *
  1319. * If the memblock contains the kernel, we have to chisel out
  1320. * the kernel memory from it and map each part separately. We
  1321. * get 6 different theoretical cases:
  1322. *
  1323. * +--------+ +--------+
  1324. * +-- start --+ +--------+ | Kernel | | Kernel |
  1325. * | | | Kernel | | case 2 | | case 5 |
  1326. * | | | case 1 | +--------+ | | +--------+
  1327. * | Memory | +--------+ | | | Kernel |
  1328. * | range | +--------+ | | | case 6 |
  1329. * | | | Kernel | +--------+ | | +--------+
  1330. * | | | case 3 | | Kernel | | |
  1331. * +-- end ----+ +--------+ | case 4 | | |
  1332. * +--------+ +--------+
  1333. */
  1334. /* Case 5: kernel covers range, don't map anything, should be rare */
  1335. if ((start > kernel_sec_start) && (end < kernel_sec_end))
  1336. break;
  1337. /* Cases where the kernel is starting inside the range */
  1338. if ((kernel_sec_start >= start) && (kernel_sec_start <= end)) {
  1339. /* Case 6: kernel is embedded in the range, we need two mappings */
  1340. if ((start < kernel_sec_start) && (end > kernel_sec_end)) {
  1341. /* Map memory below the kernel */
  1342. map.pfn = __phys_to_pfn(start);
  1343. map.virtual = __phys_to_virt(start);
  1344. map.length = kernel_sec_start - start;
  1345. map.type = MT_MEMORY_RW;
  1346. create_mapping(&map);
  1347. /* Map memory above the kernel */
  1348. map.pfn = __phys_to_pfn(kernel_sec_end);
  1349. map.virtual = __phys_to_virt(kernel_sec_end);
  1350. map.length = end - kernel_sec_end;
  1351. map.type = MT_MEMORY_RW;
  1352. create_mapping(&map);
  1353. break;
  1354. }
  1355. /* Case 1: kernel and range start at the same address, should be common */
  1356. if (kernel_sec_start == start)
  1357. start = kernel_sec_end;
  1358. /* Case 3: kernel and range end at the same address, should be rare */
  1359. if (kernel_sec_end == end)
  1360. end = kernel_sec_start;
  1361. } else if ((kernel_sec_start < start) && (kernel_sec_end > start) && (kernel_sec_end < end)) {
  1362. /* Case 2: kernel ends inside range, starts below it */
  1363. start = kernel_sec_end;
  1364. } else if ((kernel_sec_start > start) && (kernel_sec_start < end) && (kernel_sec_end > end)) {
  1365. /* Case 4: kernel starts inside range, ends above it */
  1366. end = kernel_sec_start;
  1367. }
  1368. map.pfn = __phys_to_pfn(start);
  1369. map.virtual = __phys_to_virt(start);
  1370. map.length = end - start;
  1371. map.type = MT_MEMORY_RW;
  1372. create_mapping(&map);
  1373. }
  1374. }
  1375. static void __init map_kernel(void)
  1376. {
  1377. /*
  1378. * We use the well known kernel section start and end and split the area in the
  1379. * middle like this:
  1380. * . .
  1381. * | RW memory |
  1382. * +----------------+ kernel_x_start
  1383. * | Executable |
  1384. * | kernel memory |
  1385. * +----------------+ kernel_x_end / kernel_nx_start
  1386. * | Non-executable |
  1387. * | kernel memory |
  1388. * +----------------+ kernel_nx_end
  1389. * | RW memory |
  1390. * . .
  1391. *
  1392. * Notice that we are dealing with section sized mappings here so all of this
  1393. * will be bumped to the closest section boundary. This means that some of the
  1394. * non-executable part of the kernel memory is actually mapped as executable.
  1395. * This will only persist until we turn on proper memory management later on
  1396. * and we remap the whole kernel with page granularity.
  1397. */
  1398. #ifdef CONFIG_XIP_KERNEL
  1399. phys_addr_t kernel_nx_start = kernel_sec_start;
  1400. #else
  1401. phys_addr_t kernel_x_start = kernel_sec_start;
  1402. phys_addr_t kernel_x_end = round_up(__pa(__init_end), SECTION_SIZE);
  1403. phys_addr_t kernel_nx_start = kernel_x_end;
  1404. #endif
  1405. phys_addr_t kernel_nx_end = kernel_sec_end;
  1406. struct map_desc map;
  1407. /*
  1408. * Map the kernel if it is XIP.
  1409. * It is always first in the modulearea.
  1410. */
  1411. #ifdef CONFIG_XIP_KERNEL
  1412. map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
  1413. map.virtual = MODULES_VADDR;
  1414. map.length = ((unsigned long)_exiprom - map.virtual + ~SECTION_MASK) & SECTION_MASK;
  1415. map.type = MT_ROM;
  1416. create_mapping(&map);
  1417. #else
  1418. map.pfn = __phys_to_pfn(kernel_x_start);
  1419. map.virtual = __phys_to_virt(kernel_x_start);
  1420. map.length = kernel_x_end - kernel_x_start;
  1421. map.type = MT_MEMORY_RWX;
  1422. create_mapping(&map);
  1423. /* If the nx part is small it may end up covered by the tail of the RWX section */
  1424. if (kernel_x_end == kernel_nx_end)
  1425. return;
  1426. #endif
  1427. map.pfn = __phys_to_pfn(kernel_nx_start);
  1428. map.virtual = __phys_to_virt(kernel_nx_start);
  1429. map.length = kernel_nx_end - kernel_nx_start;
  1430. map.type = MT_MEMORY_RW;
  1431. create_mapping(&map);
  1432. }
  1433. #ifdef CONFIG_ARM_PV_FIXUP
  1434. typedef void pgtables_remap(long long offset, unsigned long pgd);
  1435. pgtables_remap lpae_pgtables_remap_asm;
  1436. /*
  1437. * early_paging_init() recreates boot time page table setup, allowing machines
  1438. * to switch over to a high (>4G) address space on LPAE systems
  1439. */
  1440. static void __init early_paging_init(const struct machine_desc *mdesc)
  1441. {
  1442. pgtables_remap *lpae_pgtables_remap;
  1443. unsigned long pa_pgd;
  1444. u32 cr, ttbcr, tmp;
  1445. long long offset;
  1446. if (!mdesc->pv_fixup)
  1447. return;
  1448. offset = mdesc->pv_fixup();
  1449. if (offset == 0)
  1450. return;
  1451. /*
  1452. * Offset the kernel section physical offsets so that the kernel
  1453. * mapping will work out later on.
  1454. */
  1455. kernel_sec_start += offset;
  1456. kernel_sec_end += offset;
  1457. /*
  1458. * Get the address of the remap function in the 1:1 identity
  1459. * mapping setup by the early page table assembly code. We
  1460. * must get this prior to the pv update. The following barrier
  1461. * ensures that this is complete before we fixup any P:V offsets.
  1462. */
  1463. lpae_pgtables_remap = (pgtables_remap *)(unsigned long)__pa(lpae_pgtables_remap_asm);
  1464. pa_pgd = __pa(swapper_pg_dir);
  1465. barrier();
  1466. pr_info("Switching physical address space to 0x%08llx\n",
  1467. (u64)PHYS_OFFSET + offset);
  1468. /* Re-set the phys pfn offset, and the pv offset */
  1469. __pv_offset += offset;
  1470. __pv_phys_pfn_offset += PFN_DOWN(offset);
  1471. /* Run the patch stub to update the constants */
  1472. fixup_pv_table(&__pv_table_begin,
  1473. (&__pv_table_end - &__pv_table_begin) << 2);
  1474. /*
  1475. * We changing not only the virtual to physical mapping, but also
  1476. * the physical addresses used to access memory. We need to flush
  1477. * all levels of cache in the system with caching disabled to
  1478. * ensure that all data is written back, and nothing is prefetched
  1479. * into the caches. We also need to prevent the TLB walkers
  1480. * allocating into the caches too. Note that this is ARMv7 LPAE
  1481. * specific.
  1482. */
  1483. cr = get_cr();
  1484. set_cr(cr & ~(CR_I | CR_C));
  1485. ttbcr = cpu_get_ttbcr();
  1486. /* Disable all kind of caching of the translation table */
  1487. tmp = ttbcr & ~(TTBCR_ORGN0_MASK | TTBCR_IRGN0_MASK);
  1488. cpu_set_ttbcr(tmp);
  1489. flush_cache_all();
  1490. /*
  1491. * Fixup the page tables - this must be in the idmap region as
  1492. * we need to disable the MMU to do this safely, and hence it
  1493. * needs to be assembly. It's fairly simple, as we're using the
  1494. * temporary tables setup by the initial assembly code.
  1495. */
  1496. lpae_pgtables_remap(offset, pa_pgd);
  1497. /* Re-enable the caches and cacheable TLB walks */
  1498. cpu_set_ttbcr(ttbcr);
  1499. set_cr(cr);
  1500. }
  1501. #else
  1502. static void __init early_paging_init(const struct machine_desc *mdesc)
  1503. {
  1504. long long offset;
  1505. if (!mdesc->pv_fixup)
  1506. return;
  1507. offset = mdesc->pv_fixup();
  1508. if (offset == 0)
  1509. return;
  1510. pr_crit("Physical address space modification is only to support Keystone2.\n");
  1511. pr_crit("Please enable ARM_LPAE and ARM_PATCH_PHYS_VIRT support to use this\n");
  1512. pr_crit("feature. Your kernel may crash now, have a good day.\n");
  1513. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  1514. }
  1515. #endif
  1516. static void __init early_fixmap_shutdown(void)
  1517. {
  1518. int i;
  1519. unsigned long va = fix_to_virt(__end_of_permanent_fixed_addresses - 1);
  1520. pte_offset_fixmap = pte_offset_late_fixmap;
  1521. pmd_clear(fixmap_pmd(va));
  1522. local_flush_tlb_kernel_page(va);
  1523. for (i = 0; i < __end_of_permanent_fixed_addresses; i++) {
  1524. pte_t *pte;
  1525. struct map_desc map;
  1526. map.virtual = fix_to_virt(i);
  1527. pte = pte_offset_early_fixmap(pmd_off_k(map.virtual), map.virtual);
  1528. /* Only i/o device mappings are supported ATM */
  1529. if (pte_none(*pte) ||
  1530. (pte_val(*pte) & L_PTE_MT_MASK) != L_PTE_MT_DEV_SHARED)
  1531. continue;
  1532. map.pfn = pte_pfn(*pte);
  1533. map.type = MT_DEVICE;
  1534. map.length = PAGE_SIZE;
  1535. create_mapping(&map);
  1536. }
  1537. }
  1538. /*
  1539. * paging_init() sets up the page tables, initialises the zone memory
  1540. * maps, and sets up the zero page, bad page and bad page tables.
  1541. */
  1542. void __init paging_init(const struct machine_desc *mdesc)
  1543. {
  1544. #ifdef CONFIG_XIP_KERNEL
  1545. /* Store the kernel RW RAM region start/end in these variables */
  1546. kernel_sec_start = CONFIG_PHYS_OFFSET & SECTION_MASK;
  1547. kernel_sec_end = round_up(__pa(_end), SECTION_SIZE);
  1548. #endif
  1549. pr_debug("physical kernel sections: 0x%08llx-0x%08llx\n",
  1550. kernel_sec_start, kernel_sec_end);
  1551. prepare_page_table();
  1552. map_lowmem();
  1553. memblock_set_current_limit(arm_lowmem_limit);
  1554. pr_debug("lowmem limit is %08llx\n", (long long)arm_lowmem_limit);
  1555. /*
  1556. * After this point early_alloc(), i.e. the memblock allocator, can
  1557. * be used
  1558. */
  1559. map_kernel();
  1560. dma_contiguous_remap();
  1561. early_fixmap_shutdown();
  1562. devicemaps_init(mdesc);
  1563. kmap_init();
  1564. tcm_init();
  1565. top_pmd = pmd_off_k(0xffff0000);
  1566. bootmem_init();
  1567. }
  1568. void __init early_mm_init(const struct machine_desc *mdesc)
  1569. {
  1570. build_mem_type_table();
  1571. early_paging_init(mdesc);
  1572. }
  1573. void set_ptes(struct mm_struct *mm, unsigned long addr,
  1574. pte_t *ptep, pte_t pteval, unsigned int nr)
  1575. {
  1576. unsigned long ext = 0;
  1577. if (addr < TASK_SIZE && pte_valid_user(pteval)) {
  1578. if (!pte_special(pteval))
  1579. __sync_icache_dcache(pteval);
  1580. ext |= PTE_EXT_NG;
  1581. }
  1582. for (;;) {
  1583. set_pte_ext(ptep, pteval, ext);
  1584. if (--nr == 0)
  1585. break;
  1586. ptep++;
  1587. pteval = pte_next_pfn(pteval);
  1588. }
  1589. }