mmu.c 41 KB

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