mmu.c 29 KB

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