mmu.c 42 KB

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