mmu.c 34 KB

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