mmu.c 50 KB

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