mmu.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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/bootmem.h>
  15. #include <linux/mman.h>
  16. #include <linux/nodemask.h>
  17. #include <asm/cputype.h>
  18. #include <asm/mach-types.h>
  19. #include <asm/setup.h>
  20. #include <asm/sizes.h>
  21. #include <asm/tlb.h>
  22. #include <asm/mach/arch.h>
  23. #include <asm/mach/map.h>
  24. #include "mm.h"
  25. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  26. /*
  27. * empty_zero_page is a special page that is used for
  28. * zero-initialized data and COW.
  29. */
  30. struct page *empty_zero_page;
  31. EXPORT_SYMBOL(empty_zero_page);
  32. /*
  33. * The pmd table for the upper-most set of pages.
  34. */
  35. pmd_t *top_pmd;
  36. #define CPOLICY_UNCACHED 0
  37. #define CPOLICY_BUFFERED 1
  38. #define CPOLICY_WRITETHROUGH 2
  39. #define CPOLICY_WRITEBACK 3
  40. #define CPOLICY_WRITEALLOC 4
  41. static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
  42. static unsigned int ecc_mask __initdata = 0;
  43. pgprot_t pgprot_user;
  44. pgprot_t pgprot_kernel;
  45. EXPORT_SYMBOL(pgprot_user);
  46. EXPORT_SYMBOL(pgprot_kernel);
  47. struct cachepolicy {
  48. const char policy[16];
  49. unsigned int cr_mask;
  50. unsigned int pmd;
  51. unsigned int pte;
  52. };
  53. static struct cachepolicy cache_policies[] __initdata = {
  54. {
  55. .policy = "uncached",
  56. .cr_mask = CR_W|CR_C,
  57. .pmd = PMD_SECT_UNCACHED,
  58. .pte = L_PTE_MT_UNCACHED,
  59. }, {
  60. .policy = "buffered",
  61. .cr_mask = CR_C,
  62. .pmd = PMD_SECT_BUFFERED,
  63. .pte = L_PTE_MT_BUFFERABLE,
  64. }, {
  65. .policy = "writethrough",
  66. .cr_mask = 0,
  67. .pmd = PMD_SECT_WT,
  68. .pte = L_PTE_MT_WRITETHROUGH,
  69. }, {
  70. .policy = "writeback",
  71. .cr_mask = 0,
  72. .pmd = PMD_SECT_WB,
  73. .pte = L_PTE_MT_WRITEBACK,
  74. }, {
  75. .policy = "writealloc",
  76. .cr_mask = 0,
  77. .pmd = PMD_SECT_WBWA,
  78. .pte = L_PTE_MT_WRITEALLOC,
  79. }
  80. };
  81. /*
  82. * These are useful for identifying cache coherency
  83. * problems by allowing the cache or the cache and
  84. * writebuffer to be turned off. (Note: the write
  85. * buffer should not be on and the cache off).
  86. */
  87. static void __init early_cachepolicy(char **p)
  88. {
  89. int i;
  90. for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
  91. int len = strlen(cache_policies[i].policy);
  92. if (memcmp(*p, cache_policies[i].policy, len) == 0) {
  93. cachepolicy = i;
  94. cr_alignment &= ~cache_policies[i].cr_mask;
  95. cr_no_alignment &= ~cache_policies[i].cr_mask;
  96. *p += len;
  97. break;
  98. }
  99. }
  100. if (i == ARRAY_SIZE(cache_policies))
  101. printk(KERN_ERR "ERROR: unknown or unsupported cache policy\n");
  102. if (cpu_architecture() >= CPU_ARCH_ARMv6) {
  103. printk(KERN_WARNING "Only cachepolicy=writeback supported on ARMv6 and later\n");
  104. cachepolicy = CPOLICY_WRITEBACK;
  105. }
  106. flush_cache_all();
  107. set_cr(cr_alignment);
  108. }
  109. __early_param("cachepolicy=", early_cachepolicy);
  110. static void __init early_nocache(char **__unused)
  111. {
  112. char *p = "buffered";
  113. printk(KERN_WARNING "nocache is deprecated; use cachepolicy=%s\n", p);
  114. early_cachepolicy(&p);
  115. }
  116. __early_param("nocache", early_nocache);
  117. static void __init early_nowrite(char **__unused)
  118. {
  119. char *p = "uncached";
  120. printk(KERN_WARNING "nowb is deprecated; use cachepolicy=%s\n", p);
  121. early_cachepolicy(&p);
  122. }
  123. __early_param("nowb", early_nowrite);
  124. static void __init early_ecc(char **p)
  125. {
  126. if (memcmp(*p, "on", 2) == 0) {
  127. ecc_mask = PMD_PROTECTION;
  128. *p += 2;
  129. } else if (memcmp(*p, "off", 3) == 0) {
  130. ecc_mask = 0;
  131. *p += 3;
  132. }
  133. }
  134. __early_param("ecc=", early_ecc);
  135. static int __init noalign_setup(char *__unused)
  136. {
  137. cr_alignment &= ~CR_A;
  138. cr_no_alignment &= ~CR_A;
  139. set_cr(cr_alignment);
  140. return 1;
  141. }
  142. __setup("noalign", noalign_setup);
  143. #ifndef CONFIG_SMP
  144. void adjust_cr(unsigned long mask, unsigned long set)
  145. {
  146. unsigned long flags;
  147. mask &= ~CR_A;
  148. set &= mask;
  149. local_irq_save(flags);
  150. cr_no_alignment = (cr_no_alignment & ~mask) | set;
  151. cr_alignment = (cr_alignment & ~mask) | set;
  152. set_cr((get_cr() & ~mask) | set);
  153. local_irq_restore(flags);
  154. }
  155. #endif
  156. #define PROT_PTE_DEVICE L_PTE_PRESENT|L_PTE_YOUNG|L_PTE_DIRTY|L_PTE_WRITE
  157. #define PROT_SECT_DEVICE PMD_TYPE_SECT|PMD_SECT_AP_WRITE
  158. static struct mem_type mem_types[] = {
  159. [MT_DEVICE] = { /* Strongly ordered / ARMv6 shared device */
  160. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_SHARED |
  161. L_PTE_SHARED,
  162. .prot_l1 = PMD_TYPE_TABLE,
  163. .prot_sect = PROT_SECT_DEVICE | PMD_SECT_S,
  164. .domain = DOMAIN_IO,
  165. },
  166. [MT_DEVICE_NONSHARED] = { /* ARMv6 non-shared device */
  167. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_NONSHARED,
  168. .prot_l1 = PMD_TYPE_TABLE,
  169. .prot_sect = PROT_SECT_DEVICE,
  170. .domain = DOMAIN_IO,
  171. },
  172. [MT_DEVICE_CACHED] = { /* ioremap_cached */
  173. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_CACHED,
  174. .prot_l1 = PMD_TYPE_TABLE,
  175. .prot_sect = PROT_SECT_DEVICE | PMD_SECT_WB,
  176. .domain = DOMAIN_IO,
  177. },
  178. [MT_DEVICE_WC] = { /* ioremap_wc */
  179. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_WC,
  180. .prot_l1 = PMD_TYPE_TABLE,
  181. .prot_sect = PROT_SECT_DEVICE,
  182. .domain = DOMAIN_IO,
  183. },
  184. [MT_UNCACHED] = {
  185. .prot_pte = PROT_PTE_DEVICE,
  186. .prot_l1 = PMD_TYPE_TABLE,
  187. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
  188. .domain = DOMAIN_IO,
  189. },
  190. [MT_CACHECLEAN] = {
  191. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
  192. .domain = DOMAIN_KERNEL,
  193. },
  194. [MT_MINICLEAN] = {
  195. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN | PMD_SECT_MINICACHE,
  196. .domain = DOMAIN_KERNEL,
  197. },
  198. [MT_LOW_VECTORS] = {
  199. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  200. L_PTE_EXEC,
  201. .prot_l1 = PMD_TYPE_TABLE,
  202. .domain = DOMAIN_USER,
  203. },
  204. [MT_HIGH_VECTORS] = {
  205. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  206. L_PTE_USER | L_PTE_EXEC,
  207. .prot_l1 = PMD_TYPE_TABLE,
  208. .domain = DOMAIN_USER,
  209. },
  210. [MT_MEMORY] = {
  211. .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
  212. .domain = DOMAIN_KERNEL,
  213. },
  214. [MT_ROM] = {
  215. .prot_sect = PMD_TYPE_SECT,
  216. .domain = DOMAIN_KERNEL,
  217. },
  218. };
  219. const struct mem_type *get_mem_type(unsigned int type)
  220. {
  221. return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
  222. }
  223. /*
  224. * Adjust the PMD section entries according to the CPU in use.
  225. */
  226. static void __init build_mem_type_table(void)
  227. {
  228. struct cachepolicy *cp;
  229. unsigned int cr = get_cr();
  230. unsigned int user_pgprot, kern_pgprot, vecs_pgprot;
  231. int cpu_arch = cpu_architecture();
  232. int i;
  233. if (cpu_arch < CPU_ARCH_ARMv6) {
  234. #if defined(CONFIG_CPU_DCACHE_DISABLE)
  235. if (cachepolicy > CPOLICY_BUFFERED)
  236. cachepolicy = CPOLICY_BUFFERED;
  237. #elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
  238. if (cachepolicy > CPOLICY_WRITETHROUGH)
  239. cachepolicy = CPOLICY_WRITETHROUGH;
  240. #endif
  241. }
  242. if (cpu_arch < CPU_ARCH_ARMv5) {
  243. if (cachepolicy >= CPOLICY_WRITEALLOC)
  244. cachepolicy = CPOLICY_WRITEBACK;
  245. ecc_mask = 0;
  246. }
  247. #ifdef CONFIG_SMP
  248. cachepolicy = CPOLICY_WRITEALLOC;
  249. #endif
  250. /*
  251. * Strip out features not present on earlier architectures.
  252. * Pre-ARMv5 CPUs don't have TEX bits. Pre-ARMv6 CPUs or those
  253. * without extended page tables don't have the 'Shared' bit.
  254. */
  255. if (cpu_arch < CPU_ARCH_ARMv5)
  256. for (i = 0; i < ARRAY_SIZE(mem_types); i++)
  257. mem_types[i].prot_sect &= ~PMD_SECT_TEX(7);
  258. if ((cpu_arch < CPU_ARCH_ARMv6 || !(cr & CR_XP)) && !cpu_is_xsc3())
  259. for (i = 0; i < ARRAY_SIZE(mem_types); i++)
  260. mem_types[i].prot_sect &= ~PMD_SECT_S;
  261. /*
  262. * ARMv5 and lower, bit 4 must be set for page tables (was: cache
  263. * "update-able on write" bit on ARM610). However, Xscale and
  264. * Xscale3 require this bit to be cleared.
  265. */
  266. if (cpu_is_xscale() || cpu_is_xsc3()) {
  267. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  268. mem_types[i].prot_sect &= ~PMD_BIT4;
  269. mem_types[i].prot_l1 &= ~PMD_BIT4;
  270. }
  271. } else if (cpu_arch < CPU_ARCH_ARMv6) {
  272. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  273. if (mem_types[i].prot_l1)
  274. mem_types[i].prot_l1 |= PMD_BIT4;
  275. if (mem_types[i].prot_sect)
  276. mem_types[i].prot_sect |= PMD_BIT4;
  277. }
  278. }
  279. /*
  280. * Mark the device areas according to the CPU/architecture.
  281. */
  282. if (cpu_is_xsc3() || (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP))) {
  283. if (!cpu_is_xsc3()) {
  284. /*
  285. * Mark device regions on ARMv6+ as execute-never
  286. * to prevent speculative instruction fetches.
  287. */
  288. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_XN;
  289. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_XN;
  290. mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_XN;
  291. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_XN;
  292. }
  293. if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
  294. /*
  295. * For ARMv7 with TEX remapping,
  296. * - shared device is SXCB=1100
  297. * - nonshared device is SXCB=0100
  298. * - write combine device mem is SXCB=0001
  299. * (Uncached Normal memory)
  300. */
  301. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1);
  302. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(1);
  303. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
  304. } else if (cpu_is_xsc3()) {
  305. /*
  306. * For Xscale3,
  307. * - shared device is TEXCB=00101
  308. * - nonshared device is TEXCB=01000
  309. * - write combine device mem is TEXCB=00100
  310. * (Inner/Outer Uncacheable in xsc3 parlance)
  311. */
  312. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1) | PMD_SECT_BUFFERED;
  313. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
  314. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
  315. } else {
  316. /*
  317. * For ARMv6 and ARMv7 without TEX remapping,
  318. * - shared device is TEXCB=00001
  319. * - nonshared device is TEXCB=01000
  320. * - write combine device mem is TEXCB=00100
  321. * (Uncached Normal in ARMv6 parlance).
  322. */
  323. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_BUFFERED;
  324. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
  325. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
  326. }
  327. } else {
  328. /*
  329. * On others, write combining is "Uncached/Buffered"
  330. */
  331. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
  332. }
  333. /*
  334. * Now deal with the memory-type mappings
  335. */
  336. cp = &cache_policies[cachepolicy];
  337. vecs_pgprot = kern_pgprot = user_pgprot = cp->pte;
  338. #ifndef CONFIG_SMP
  339. /*
  340. * Only use write-through for non-SMP systems
  341. */
  342. if (cpu_arch >= CPU_ARCH_ARMv5 && cachepolicy > CPOLICY_WRITETHROUGH)
  343. vecs_pgprot = cache_policies[CPOLICY_WRITETHROUGH].pte;
  344. #endif
  345. /*
  346. * Enable CPU-specific coherency if supported.
  347. * (Only available on XSC3 at the moment.)
  348. */
  349. if (arch_is_coherent() && cpu_is_xsc3())
  350. mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
  351. /*
  352. * ARMv6 and above have extended page tables.
  353. */
  354. if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
  355. /*
  356. * Mark cache clean areas and XIP ROM read only
  357. * from SVC mode and no access from userspace.
  358. */
  359. mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  360. mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  361. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  362. #ifdef CONFIG_SMP
  363. /*
  364. * Mark memory with the "shared" attribute for SMP systems
  365. */
  366. user_pgprot |= L_PTE_SHARED;
  367. kern_pgprot |= L_PTE_SHARED;
  368. vecs_pgprot |= L_PTE_SHARED;
  369. mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
  370. #endif
  371. }
  372. for (i = 0; i < 16; i++) {
  373. unsigned long v = pgprot_val(protection_map[i]);
  374. protection_map[i] = __pgprot(v | user_pgprot);
  375. }
  376. mem_types[MT_LOW_VECTORS].prot_pte |= vecs_pgprot;
  377. mem_types[MT_HIGH_VECTORS].prot_pte |= vecs_pgprot;
  378. pgprot_user = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | user_pgprot);
  379. pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
  380. L_PTE_DIRTY | L_PTE_WRITE |
  381. L_PTE_EXEC | kern_pgprot);
  382. mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
  383. mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
  384. mem_types[MT_MEMORY].prot_sect |= ecc_mask | cp->pmd;
  385. mem_types[MT_ROM].prot_sect |= cp->pmd;
  386. switch (cp->pmd) {
  387. case PMD_SECT_WT:
  388. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
  389. break;
  390. case PMD_SECT_WB:
  391. case PMD_SECT_WBWA:
  392. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
  393. break;
  394. }
  395. printk("Memory policy: ECC %sabled, Data cache %s\n",
  396. ecc_mask ? "en" : "dis", cp->policy);
  397. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  398. struct mem_type *t = &mem_types[i];
  399. if (t->prot_l1)
  400. t->prot_l1 |= PMD_DOMAIN(t->domain);
  401. if (t->prot_sect)
  402. t->prot_sect |= PMD_DOMAIN(t->domain);
  403. }
  404. }
  405. #define vectors_base() (vectors_high() ? 0xffff0000 : 0)
  406. static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
  407. unsigned long end, unsigned long pfn,
  408. const struct mem_type *type)
  409. {
  410. pte_t *pte;
  411. if (pmd_none(*pmd)) {
  412. pte = alloc_bootmem_low_pages(2 * PTRS_PER_PTE * sizeof(pte_t));
  413. __pmd_populate(pmd, __pa(pte) | type->prot_l1);
  414. }
  415. pte = pte_offset_kernel(pmd, addr);
  416. do {
  417. set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)), 0);
  418. pfn++;
  419. } while (pte++, addr += PAGE_SIZE, addr != end);
  420. }
  421. static void __init alloc_init_section(pgd_t *pgd, unsigned long addr,
  422. unsigned long end, unsigned long phys,
  423. const struct mem_type *type)
  424. {
  425. pmd_t *pmd = pmd_offset(pgd, addr);
  426. /*
  427. * Try a section mapping - end, addr and phys must all be aligned
  428. * to a section boundary. Note that PMDs refer to the individual
  429. * L1 entries, whereas PGDs refer to a group of L1 entries making
  430. * up one logical pointer to an L2 table.
  431. */
  432. if (((addr | end | phys) & ~SECTION_MASK) == 0) {
  433. pmd_t *p = pmd;
  434. if (addr & SECTION_SIZE)
  435. pmd++;
  436. do {
  437. *pmd = __pmd(phys | type->prot_sect);
  438. phys += SECTION_SIZE;
  439. } while (pmd++, addr += SECTION_SIZE, addr != end);
  440. flush_pmd_entry(p);
  441. } else {
  442. /*
  443. * No need to loop; pte's aren't interested in the
  444. * individual L1 entries.
  445. */
  446. alloc_init_pte(pmd, addr, end, __phys_to_pfn(phys), type);
  447. }
  448. }
  449. static void __init create_36bit_mapping(struct map_desc *md,
  450. const struct mem_type *type)
  451. {
  452. unsigned long phys, addr, length, end;
  453. pgd_t *pgd;
  454. addr = md->virtual;
  455. phys = (unsigned long)__pfn_to_phys(md->pfn);
  456. length = PAGE_ALIGN(md->length);
  457. if (!(cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())) {
  458. printk(KERN_ERR "MM: CPU does not support supersection "
  459. "mapping for 0x%08llx at 0x%08lx\n",
  460. __pfn_to_phys((u64)md->pfn), addr);
  461. return;
  462. }
  463. /* N.B. ARMv6 supersections are only defined to work with domain 0.
  464. * Since domain assignments can in fact be arbitrary, the
  465. * 'domain == 0' check below is required to insure that ARMv6
  466. * supersections are only allocated for domain 0 regardless
  467. * of the actual domain assignments in use.
  468. */
  469. if (type->domain) {
  470. printk(KERN_ERR "MM: invalid domain in supersection "
  471. "mapping for 0x%08llx at 0x%08lx\n",
  472. __pfn_to_phys((u64)md->pfn), addr);
  473. return;
  474. }
  475. if ((addr | length | __pfn_to_phys(md->pfn)) & ~SUPERSECTION_MASK) {
  476. printk(KERN_ERR "MM: cannot create mapping for "
  477. "0x%08llx at 0x%08lx invalid alignment\n",
  478. __pfn_to_phys((u64)md->pfn), addr);
  479. return;
  480. }
  481. /*
  482. * Shift bits [35:32] of address into bits [23:20] of PMD
  483. * (See ARMv6 spec).
  484. */
  485. phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
  486. pgd = pgd_offset_k(addr);
  487. end = addr + length;
  488. do {
  489. pmd_t *pmd = pmd_offset(pgd, addr);
  490. int i;
  491. for (i = 0; i < 16; i++)
  492. *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER);
  493. addr += SUPERSECTION_SIZE;
  494. phys += SUPERSECTION_SIZE;
  495. pgd += SUPERSECTION_SIZE >> PGDIR_SHIFT;
  496. } while (addr != end);
  497. }
  498. /*
  499. * Create the page directory entries and any necessary
  500. * page tables for the mapping specified by `md'. We
  501. * are able to cope here with varying sizes and address
  502. * offsets, and we take full advantage of sections and
  503. * supersections.
  504. */
  505. void __init create_mapping(struct map_desc *md)
  506. {
  507. unsigned long phys, addr, length, end;
  508. const struct mem_type *type;
  509. pgd_t *pgd;
  510. if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
  511. printk(KERN_WARNING "BUG: not creating mapping for "
  512. "0x%08llx at 0x%08lx in user region\n",
  513. __pfn_to_phys((u64)md->pfn), md->virtual);
  514. return;
  515. }
  516. if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
  517. md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
  518. printk(KERN_WARNING "BUG: mapping for 0x%08llx at 0x%08lx "
  519. "overlaps vmalloc space\n",
  520. __pfn_to_phys((u64)md->pfn), md->virtual);
  521. }
  522. type = &mem_types[md->type];
  523. /*
  524. * Catch 36-bit addresses
  525. */
  526. if (md->pfn >= 0x100000) {
  527. create_36bit_mapping(md, type);
  528. return;
  529. }
  530. addr = md->virtual & PAGE_MASK;
  531. phys = (unsigned long)__pfn_to_phys(md->pfn);
  532. length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
  533. if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
  534. printk(KERN_WARNING "BUG: map for 0x%08lx at 0x%08lx can not "
  535. "be mapped using pages, ignoring.\n",
  536. __pfn_to_phys(md->pfn), addr);
  537. return;
  538. }
  539. pgd = pgd_offset_k(addr);
  540. end = addr + length;
  541. do {
  542. unsigned long next = pgd_addr_end(addr, end);
  543. alloc_init_section(pgd, addr, next, phys, type);
  544. phys += next - addr;
  545. addr = next;
  546. } while (pgd++, addr != end);
  547. }
  548. /*
  549. * Create the architecture specific mappings
  550. */
  551. void __init iotable_init(struct map_desc *io_desc, int nr)
  552. {
  553. int i;
  554. for (i = 0; i < nr; i++)
  555. create_mapping(io_desc + i);
  556. }
  557. static unsigned long __initdata vmalloc_reserve = SZ_128M;
  558. /*
  559. * vmalloc=size forces the vmalloc area to be exactly 'size'
  560. * bytes. This can be used to increase (or decrease) the vmalloc
  561. * area - the default is 128m.
  562. */
  563. static void __init early_vmalloc(char **arg)
  564. {
  565. vmalloc_reserve = memparse(*arg, arg);
  566. if (vmalloc_reserve < SZ_16M) {
  567. vmalloc_reserve = SZ_16M;
  568. printk(KERN_WARNING
  569. "vmalloc area too small, limiting to %luMB\n",
  570. vmalloc_reserve >> 20);
  571. }
  572. }
  573. __early_param("vmalloc=", early_vmalloc);
  574. #define VMALLOC_MIN (void *)(VMALLOC_END - vmalloc_reserve)
  575. static int __init check_membank_valid(struct membank *mb)
  576. {
  577. /*
  578. * Check whether this memory region has non-zero size or
  579. * invalid node number.
  580. */
  581. if (mb->size == 0 || mb->node >= MAX_NUMNODES)
  582. return 0;
  583. /*
  584. * Check whether this memory region would entirely overlap
  585. * the vmalloc area.
  586. */
  587. if (phys_to_virt(mb->start) >= VMALLOC_MIN) {
  588. printk(KERN_NOTICE "Ignoring RAM at %.8lx-%.8lx "
  589. "(vmalloc region overlap).\n",
  590. mb->start, mb->start + mb->size - 1);
  591. return 0;
  592. }
  593. /*
  594. * Check whether this memory region would partially overlap
  595. * the vmalloc area.
  596. */
  597. if (phys_to_virt(mb->start + mb->size) < phys_to_virt(mb->start) ||
  598. phys_to_virt(mb->start + mb->size) > VMALLOC_MIN) {
  599. unsigned long newsize = VMALLOC_MIN - phys_to_virt(mb->start);
  600. printk(KERN_NOTICE "Truncating RAM at %.8lx-%.8lx "
  601. "to -%.8lx (vmalloc region overlap).\n",
  602. mb->start, mb->start + mb->size - 1,
  603. mb->start + newsize - 1);
  604. mb->size = newsize;
  605. }
  606. return 1;
  607. }
  608. static void __init sanity_check_meminfo(struct meminfo *mi)
  609. {
  610. int i, j;
  611. for (i = 0, j = 0; i < mi->nr_banks; i++) {
  612. if (check_membank_valid(&mi->bank[i]))
  613. mi->bank[j++] = mi->bank[i];
  614. }
  615. mi->nr_banks = j;
  616. }
  617. static inline void prepare_page_table(struct meminfo *mi)
  618. {
  619. unsigned long addr;
  620. /*
  621. * Clear out all the mappings below the kernel image.
  622. */
  623. for (addr = 0; addr < MODULES_VADDR; addr += PGDIR_SIZE)
  624. pmd_clear(pmd_off_k(addr));
  625. #ifdef CONFIG_XIP_KERNEL
  626. /* The XIP kernel is mapped in the module area -- skip over it */
  627. addr = ((unsigned long)&_etext + PGDIR_SIZE - 1) & PGDIR_MASK;
  628. #endif
  629. for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
  630. pmd_clear(pmd_off_k(addr));
  631. /*
  632. * Clear out all the kernel space mappings, except for the first
  633. * memory bank, up to the end of the vmalloc region.
  634. */
  635. for (addr = __phys_to_virt(mi->bank[0].start + mi->bank[0].size);
  636. addr < VMALLOC_END; addr += PGDIR_SIZE)
  637. pmd_clear(pmd_off_k(addr));
  638. }
  639. /*
  640. * Reserve the various regions of node 0
  641. */
  642. void __init reserve_node_zero(pg_data_t *pgdat)
  643. {
  644. unsigned long res_size = 0;
  645. /*
  646. * Register the kernel text and data with bootmem.
  647. * Note that this can only be in node 0.
  648. */
  649. #ifdef CONFIG_XIP_KERNEL
  650. reserve_bootmem_node(pgdat, __pa(&__data_start), &_end - &__data_start,
  651. BOOTMEM_DEFAULT);
  652. #else
  653. reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext,
  654. BOOTMEM_DEFAULT);
  655. #endif
  656. /*
  657. * Reserve the page tables. These are already in use,
  658. * and can only be in node 0.
  659. */
  660. reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
  661. PTRS_PER_PGD * sizeof(pgd_t), BOOTMEM_DEFAULT);
  662. /*
  663. * Hmm... This should go elsewhere, but we really really need to
  664. * stop things allocating the low memory; ideally we need a better
  665. * implementation of GFP_DMA which does not assume that DMA-able
  666. * memory starts at zero.
  667. */
  668. if (machine_is_integrator() || machine_is_cintegrator())
  669. res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
  670. /*
  671. * These should likewise go elsewhere. They pre-reserve the
  672. * screen memory region at the start of main system memory.
  673. */
  674. if (machine_is_edb7211())
  675. res_size = 0x00020000;
  676. if (machine_is_p720t())
  677. res_size = 0x00014000;
  678. /* H1940 and RX3715 need to reserve this for suspend */
  679. if (machine_is_h1940() || machine_is_rx3715()) {
  680. reserve_bootmem_node(pgdat, 0x30003000, 0x1000,
  681. BOOTMEM_DEFAULT);
  682. reserve_bootmem_node(pgdat, 0x30081000, 0x1000,
  683. BOOTMEM_DEFAULT);
  684. }
  685. #ifdef CONFIG_SA1111
  686. /*
  687. * Because of the SA1111 DMA bug, we want to preserve our
  688. * precious DMA-able memory...
  689. */
  690. res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
  691. #endif
  692. if (res_size)
  693. reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size,
  694. BOOTMEM_DEFAULT);
  695. }
  696. /*
  697. * Set up device the mappings. Since we clear out the page tables for all
  698. * mappings above VMALLOC_END, we will remove any debug device mappings.
  699. * This means you have to be careful how you debug this function, or any
  700. * called function. This means you can't use any function or debugging
  701. * method which may touch any device, otherwise the kernel _will_ crash.
  702. */
  703. static void __init devicemaps_init(struct machine_desc *mdesc)
  704. {
  705. struct map_desc map;
  706. unsigned long addr;
  707. void *vectors;
  708. /*
  709. * Allocate the vector page early.
  710. */
  711. vectors = alloc_bootmem_low_pages(PAGE_SIZE);
  712. BUG_ON(!vectors);
  713. for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
  714. pmd_clear(pmd_off_k(addr));
  715. /*
  716. * Map the kernel if it is XIP.
  717. * It is always first in the modulearea.
  718. */
  719. #ifdef CONFIG_XIP_KERNEL
  720. map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
  721. map.virtual = MODULES_VADDR;
  722. map.length = ((unsigned long)&_etext - map.virtual + ~SECTION_MASK) & SECTION_MASK;
  723. map.type = MT_ROM;
  724. create_mapping(&map);
  725. #endif
  726. /*
  727. * Map the cache flushing regions.
  728. */
  729. #ifdef FLUSH_BASE
  730. map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
  731. map.virtual = FLUSH_BASE;
  732. map.length = SZ_1M;
  733. map.type = MT_CACHECLEAN;
  734. create_mapping(&map);
  735. #endif
  736. #ifdef FLUSH_BASE_MINICACHE
  737. map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
  738. map.virtual = FLUSH_BASE_MINICACHE;
  739. map.length = SZ_1M;
  740. map.type = MT_MINICLEAN;
  741. create_mapping(&map);
  742. #endif
  743. /*
  744. * Create a mapping for the machine vectors at the high-vectors
  745. * location (0xffff0000). If we aren't using high-vectors, also
  746. * create a mapping at the low-vectors virtual address.
  747. */
  748. map.pfn = __phys_to_pfn(virt_to_phys(vectors));
  749. map.virtual = 0xffff0000;
  750. map.length = PAGE_SIZE;
  751. map.type = MT_HIGH_VECTORS;
  752. create_mapping(&map);
  753. if (!vectors_high()) {
  754. map.virtual = 0;
  755. map.type = MT_LOW_VECTORS;
  756. create_mapping(&map);
  757. }
  758. /*
  759. * Ask the machine support to map in the statically mapped devices.
  760. */
  761. if (mdesc->map_io)
  762. mdesc->map_io();
  763. /*
  764. * Finally flush the caches and tlb to ensure that we're in a
  765. * consistent state wrt the writebuffer. This also ensures that
  766. * any write-allocated cache lines in the vector page are written
  767. * back. After this point, we can start to touch devices again.
  768. */
  769. local_flush_tlb_all();
  770. flush_cache_all();
  771. }
  772. /*
  773. * paging_init() sets up the page tables, initialises the zone memory
  774. * maps, and sets up the zero page, bad page and bad page tables.
  775. */
  776. void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
  777. {
  778. void *zero_page;
  779. build_mem_type_table();
  780. sanity_check_meminfo(mi);
  781. prepare_page_table(mi);
  782. bootmem_init(mi);
  783. devicemaps_init(mdesc);
  784. top_pmd = pmd_off_k(0xffff0000);
  785. /*
  786. * allocate the zero page. Note that we count on this going ok.
  787. */
  788. zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
  789. memzero(zero_page, PAGE_SIZE);
  790. empty_zero_page = virt_to_page(zero_page);
  791. flush_dcache_page(empty_zero_page);
  792. }
  793. /*
  794. * In order to soft-boot, we need to insert a 1:1 mapping in place of
  795. * the user-mode pages. This will then ensure that we have predictable
  796. * results when turning the mmu off
  797. */
  798. void setup_mm_for_reboot(char mode)
  799. {
  800. unsigned long base_pmdval;
  801. pgd_t *pgd;
  802. int i;
  803. if (current->mm && current->mm->pgd)
  804. pgd = current->mm->pgd;
  805. else
  806. pgd = init_mm.pgd;
  807. base_pmdval = PMD_SECT_AP_WRITE | PMD_SECT_AP_READ | PMD_TYPE_SECT;
  808. if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
  809. base_pmdval |= PMD_BIT4;
  810. for (i = 0; i < FIRST_USER_PGD_NR + USER_PTRS_PER_PGD; i++, pgd++) {
  811. unsigned long pmdval = (i << PGDIR_SHIFT) | base_pmdval;
  812. pmd_t *pmd;
  813. pmd = pmd_off(pgd, i << PGDIR_SHIFT);
  814. pmd[0] = __pmd(pmdval);
  815. pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
  816. flush_pmd_entry(pmd);
  817. }
  818. }