mmu.c 40 KB

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