mmu.c 35 KB

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