mmu.c 26 KB

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