mmu.c 40 KB

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