mmu.c 26 KB

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