mmu.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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/mach-types.h>
  18. #include <asm/setup.h>
  19. #include <asm/sizes.h>
  20. #include <asm/tlb.h>
  21. #include <asm/mach/arch.h>
  22. #include <asm/mach/map.h>
  23. #include "mm.h"
  24. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  25. extern void _stext, _etext, __data_start, _end;
  26. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  27. /*
  28. * empty_zero_page is a special page that is used for
  29. * zero-initialized data and COW.
  30. */
  31. struct page *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_kernel;
  44. EXPORT_SYMBOL(pgprot_kernel);
  45. struct cachepolicy {
  46. const char policy[16];
  47. unsigned int cr_mask;
  48. unsigned int pmd;
  49. unsigned int pte;
  50. };
  51. static struct cachepolicy cache_policies[] __initdata = {
  52. {
  53. .policy = "uncached",
  54. .cr_mask = CR_W|CR_C,
  55. .pmd = PMD_SECT_UNCACHED,
  56. .pte = 0,
  57. }, {
  58. .policy = "buffered",
  59. .cr_mask = CR_C,
  60. .pmd = PMD_SECT_BUFFERED,
  61. .pte = PTE_BUFFERABLE,
  62. }, {
  63. .policy = "writethrough",
  64. .cr_mask = 0,
  65. .pmd = PMD_SECT_WT,
  66. .pte = PTE_CACHEABLE,
  67. }, {
  68. .policy = "writeback",
  69. .cr_mask = 0,
  70. .pmd = PMD_SECT_WB,
  71. .pte = PTE_BUFFERABLE|PTE_CACHEABLE,
  72. }, {
  73. .policy = "writealloc",
  74. .cr_mask = 0,
  75. .pmd = PMD_SECT_WBWA,
  76. .pte = PTE_BUFFERABLE|PTE_CACHEABLE,
  77. }
  78. };
  79. /*
  80. * These are useful for identifing cache coherency
  81. * problems by allowing the cache or the cache and
  82. * writebuffer to be turned off. (Note: the write
  83. * buffer should not be on and the cache off).
  84. */
  85. static void __init early_cachepolicy(char **p)
  86. {
  87. int i;
  88. for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
  89. int len = strlen(cache_policies[i].policy);
  90. if (memcmp(*p, cache_policies[i].policy, len) == 0) {
  91. cachepolicy = i;
  92. cr_alignment &= ~cache_policies[i].cr_mask;
  93. cr_no_alignment &= ~cache_policies[i].cr_mask;
  94. *p += len;
  95. break;
  96. }
  97. }
  98. if (i == ARRAY_SIZE(cache_policies))
  99. printk(KERN_ERR "ERROR: unknown or unsupported cache policy\n");
  100. flush_cache_all();
  101. set_cr(cr_alignment);
  102. }
  103. __early_param("cachepolicy=", early_cachepolicy);
  104. static void __init early_nocache(char **__unused)
  105. {
  106. char *p = "buffered";
  107. printk(KERN_WARNING "nocache is deprecated; use cachepolicy=%s\n", p);
  108. early_cachepolicy(&p);
  109. }
  110. __early_param("nocache", early_nocache);
  111. static void __init early_nowrite(char **__unused)
  112. {
  113. char *p = "uncached";
  114. printk(KERN_WARNING "nowb is deprecated; use cachepolicy=%s\n", p);
  115. early_cachepolicy(&p);
  116. }
  117. __early_param("nowb", early_nowrite);
  118. static void __init early_ecc(char **p)
  119. {
  120. if (memcmp(*p, "on", 2) == 0) {
  121. ecc_mask = PMD_PROTECTION;
  122. *p += 2;
  123. } else if (memcmp(*p, "off", 3) == 0) {
  124. ecc_mask = 0;
  125. *p += 3;
  126. }
  127. }
  128. __early_param("ecc=", early_ecc);
  129. static int __init noalign_setup(char *__unused)
  130. {
  131. cr_alignment &= ~CR_A;
  132. cr_no_alignment &= ~CR_A;
  133. set_cr(cr_alignment);
  134. return 1;
  135. }
  136. __setup("noalign", noalign_setup);
  137. struct mem_types {
  138. unsigned int prot_pte;
  139. unsigned int prot_l1;
  140. unsigned int prot_sect;
  141. unsigned int domain;
  142. };
  143. static struct mem_types mem_types[] __initdata = {
  144. [MT_DEVICE] = {
  145. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  146. L_PTE_WRITE,
  147. .prot_l1 = PMD_TYPE_TABLE,
  148. .prot_sect = PMD_TYPE_SECT | PMD_BIT4 | PMD_SECT_UNCACHED |
  149. PMD_SECT_AP_WRITE,
  150. .domain = DOMAIN_IO,
  151. },
  152. [MT_CACHECLEAN] = {
  153. .prot_sect = PMD_TYPE_SECT | PMD_BIT4,
  154. .domain = DOMAIN_KERNEL,
  155. },
  156. [MT_MINICLEAN] = {
  157. .prot_sect = PMD_TYPE_SECT | PMD_BIT4 | PMD_SECT_MINICACHE,
  158. .domain = DOMAIN_KERNEL,
  159. },
  160. [MT_LOW_VECTORS] = {
  161. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  162. L_PTE_EXEC,
  163. .prot_l1 = PMD_TYPE_TABLE,
  164. .domain = DOMAIN_USER,
  165. },
  166. [MT_HIGH_VECTORS] = {
  167. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  168. L_PTE_USER | L_PTE_EXEC,
  169. .prot_l1 = PMD_TYPE_TABLE,
  170. .domain = DOMAIN_USER,
  171. },
  172. [MT_MEMORY] = {
  173. .prot_sect = PMD_TYPE_SECT | PMD_BIT4 | PMD_SECT_AP_WRITE,
  174. .domain = DOMAIN_KERNEL,
  175. },
  176. [MT_ROM] = {
  177. .prot_sect = PMD_TYPE_SECT | PMD_BIT4,
  178. .domain = DOMAIN_KERNEL,
  179. },
  180. [MT_IXP2000_DEVICE] = { /* IXP2400 requires XCB=101 for on-chip I/O */
  181. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  182. L_PTE_WRITE,
  183. .prot_l1 = PMD_TYPE_TABLE,
  184. .prot_sect = PMD_TYPE_SECT | PMD_BIT4 | PMD_SECT_UNCACHED |
  185. PMD_SECT_AP_WRITE | PMD_SECT_BUFFERABLE |
  186. PMD_SECT_TEX(1),
  187. .domain = DOMAIN_IO,
  188. },
  189. [MT_NONSHARED_DEVICE] = {
  190. .prot_l1 = PMD_TYPE_TABLE,
  191. .prot_sect = PMD_TYPE_SECT | PMD_BIT4 | PMD_SECT_NONSHARED_DEV |
  192. PMD_SECT_AP_WRITE,
  193. .domain = DOMAIN_IO,
  194. }
  195. };
  196. /*
  197. * Adjust the PMD section entries according to the CPU in use.
  198. */
  199. static void __init build_mem_type_table(void)
  200. {
  201. struct cachepolicy *cp;
  202. unsigned int cr = get_cr();
  203. unsigned int user_pgprot, kern_pgprot;
  204. int cpu_arch = cpu_architecture();
  205. int i;
  206. #if defined(CONFIG_CPU_DCACHE_DISABLE)
  207. if (cachepolicy > CPOLICY_BUFFERED)
  208. cachepolicy = CPOLICY_BUFFERED;
  209. #elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
  210. if (cachepolicy > CPOLICY_WRITETHROUGH)
  211. cachepolicy = CPOLICY_WRITETHROUGH;
  212. #endif
  213. if (cpu_arch < CPU_ARCH_ARMv5) {
  214. if (cachepolicy >= CPOLICY_WRITEALLOC)
  215. cachepolicy = CPOLICY_WRITEBACK;
  216. ecc_mask = 0;
  217. }
  218. /*
  219. * Xscale must not have PMD bit 4 set for section mappings.
  220. */
  221. if (cpu_is_xscale())
  222. for (i = 0; i < ARRAY_SIZE(mem_types); i++)
  223. mem_types[i].prot_sect &= ~PMD_BIT4;
  224. /*
  225. * ARMv5 and lower, excluding Xscale, bit 4 must be set for
  226. * page tables.
  227. */
  228. if (cpu_arch < CPU_ARCH_ARMv6 && !cpu_is_xscale())
  229. for (i = 0; i < ARRAY_SIZE(mem_types); i++)
  230. if (mem_types[i].prot_l1)
  231. mem_types[i].prot_l1 |= PMD_BIT4;
  232. cp = &cache_policies[cachepolicy];
  233. kern_pgprot = user_pgprot = cp->pte;
  234. /*
  235. * Enable CPU-specific coherency if supported.
  236. * (Only available on XSC3 at the moment.)
  237. */
  238. if (arch_is_coherent()) {
  239. if (cpu_is_xsc3()) {
  240. mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
  241. mem_types[MT_MEMORY].prot_pte |= L_PTE_COHERENT;
  242. }
  243. }
  244. /*
  245. * ARMv6 and above have extended page tables.
  246. */
  247. if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
  248. /*
  249. * bit 4 becomes XN which we must clear for the
  250. * kernel memory mapping.
  251. */
  252. mem_types[MT_MEMORY].prot_sect &= ~PMD_SECT_XN;
  253. mem_types[MT_ROM].prot_sect &= ~PMD_SECT_XN;
  254. /*
  255. * Mark cache clean areas and XIP ROM read only
  256. * from SVC mode and no access from userspace.
  257. */
  258. mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  259. mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  260. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  261. /*
  262. * Mark the device area as "shared device"
  263. */
  264. mem_types[MT_DEVICE].prot_pte |= L_PTE_BUFFERABLE;
  265. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_BUFFERED;
  266. /*
  267. * User pages need to be mapped with the ASID
  268. * (iow, non-global)
  269. */
  270. user_pgprot |= L_PTE_ASID;
  271. #ifdef CONFIG_SMP
  272. /*
  273. * Mark memory with the "shared" attribute for SMP systems
  274. */
  275. user_pgprot |= L_PTE_SHARED;
  276. kern_pgprot |= L_PTE_SHARED;
  277. mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
  278. #endif
  279. }
  280. for (i = 0; i < 16; i++) {
  281. unsigned long v = pgprot_val(protection_map[i]);
  282. v = (v & ~(L_PTE_BUFFERABLE|L_PTE_CACHEABLE)) | user_pgprot;
  283. protection_map[i] = __pgprot(v);
  284. }
  285. mem_types[MT_LOW_VECTORS].prot_pte |= kern_pgprot;
  286. mem_types[MT_HIGH_VECTORS].prot_pte |= kern_pgprot;
  287. if (cpu_arch >= CPU_ARCH_ARMv5) {
  288. #ifndef CONFIG_SMP
  289. /*
  290. * Only use write-through for non-SMP systems
  291. */
  292. mem_types[MT_LOW_VECTORS].prot_pte &= ~L_PTE_BUFFERABLE;
  293. mem_types[MT_HIGH_VECTORS].prot_pte &= ~L_PTE_BUFFERABLE;
  294. #endif
  295. } else {
  296. mem_types[MT_MINICLEAN].prot_sect &= ~PMD_SECT_TEX(1);
  297. }
  298. pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
  299. L_PTE_DIRTY | L_PTE_WRITE |
  300. L_PTE_EXEC | kern_pgprot);
  301. mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
  302. mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
  303. mem_types[MT_MEMORY].prot_sect |= ecc_mask | cp->pmd;
  304. mem_types[MT_ROM].prot_sect |= cp->pmd;
  305. switch (cp->pmd) {
  306. case PMD_SECT_WT:
  307. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
  308. break;
  309. case PMD_SECT_WB:
  310. case PMD_SECT_WBWA:
  311. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
  312. break;
  313. }
  314. printk("Memory policy: ECC %sabled, Data cache %s\n",
  315. ecc_mask ? "en" : "dis", cp->policy);
  316. }
  317. #define vectors_base() (vectors_high() ? 0xffff0000 : 0)
  318. /*
  319. * Create a SECTION PGD between VIRT and PHYS in domain
  320. * DOMAIN with protection PROT. This operates on half-
  321. * pgdir entry increments.
  322. */
  323. static inline void
  324. alloc_init_section(unsigned long virt, unsigned long phys, int prot)
  325. {
  326. pmd_t *pmdp = pmd_off_k(virt);
  327. if (virt & (1 << 20))
  328. pmdp++;
  329. *pmdp = __pmd(phys | prot);
  330. flush_pmd_entry(pmdp);
  331. }
  332. /*
  333. * Create a SUPER SECTION PGD between VIRT and PHYS with protection PROT
  334. */
  335. static inline void
  336. alloc_init_supersection(unsigned long virt, unsigned long phys, int prot)
  337. {
  338. int i;
  339. for (i = 0; i < 16; i += 1) {
  340. alloc_init_section(virt, phys, prot | PMD_SECT_SUPER);
  341. virt += (PGDIR_SIZE / 2);
  342. }
  343. }
  344. /*
  345. * Add a PAGE mapping between VIRT and PHYS in domain
  346. * DOMAIN with protection PROT. Note that due to the
  347. * way we map the PTEs, we must allocate two PTE_SIZE'd
  348. * blocks - one for the Linux pte table, and one for
  349. * the hardware pte table.
  350. */
  351. static inline void
  352. alloc_init_page(unsigned long virt, unsigned long phys, unsigned int prot_l1, pgprot_t prot)
  353. {
  354. pmd_t *pmdp = pmd_off_k(virt);
  355. pte_t *ptep;
  356. if (pmd_none(*pmdp)) {
  357. ptep = alloc_bootmem_low_pages(2 * PTRS_PER_PTE *
  358. sizeof(pte_t));
  359. __pmd_populate(pmdp, __pa(ptep) | prot_l1);
  360. }
  361. ptep = pte_offset_kernel(pmdp, virt);
  362. set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
  363. }
  364. /*
  365. * Create the page directory entries and any necessary
  366. * page tables for the mapping specified by `md'. We
  367. * are able to cope here with varying sizes and address
  368. * offsets, and we take full advantage of sections and
  369. * supersections.
  370. */
  371. void __init create_mapping(struct map_desc *md)
  372. {
  373. unsigned long virt, length;
  374. int prot_sect, prot_l1, domain;
  375. pgprot_t prot_pte;
  376. unsigned long off = (u32)__pfn_to_phys(md->pfn);
  377. if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
  378. printk(KERN_WARNING "BUG: not creating mapping for "
  379. "0x%08llx at 0x%08lx in user region\n",
  380. __pfn_to_phys((u64)md->pfn), md->virtual);
  381. return;
  382. }
  383. if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
  384. md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
  385. printk(KERN_WARNING "BUG: mapping for 0x%08llx at 0x%08lx "
  386. "overlaps vmalloc space\n",
  387. __pfn_to_phys((u64)md->pfn), md->virtual);
  388. }
  389. domain = mem_types[md->type].domain;
  390. prot_pte = __pgprot(mem_types[md->type].prot_pte);
  391. prot_l1 = mem_types[md->type].prot_l1 | PMD_DOMAIN(domain);
  392. prot_sect = mem_types[md->type].prot_sect | PMD_DOMAIN(domain);
  393. /*
  394. * Catch 36-bit addresses
  395. */
  396. if(md->pfn >= 0x100000) {
  397. if(domain) {
  398. printk(KERN_ERR "MM: invalid domain in supersection "
  399. "mapping for 0x%08llx at 0x%08lx\n",
  400. __pfn_to_phys((u64)md->pfn), md->virtual);
  401. return;
  402. }
  403. if((md->virtual | md->length | __pfn_to_phys(md->pfn))
  404. & ~SUPERSECTION_MASK) {
  405. printk(KERN_ERR "MM: cannot create mapping for "
  406. "0x%08llx at 0x%08lx invalid alignment\n",
  407. __pfn_to_phys((u64)md->pfn), md->virtual);
  408. return;
  409. }
  410. /*
  411. * Shift bits [35:32] of address into bits [23:20] of PMD
  412. * (See ARMv6 spec).
  413. */
  414. off |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
  415. }
  416. virt = md->virtual;
  417. off -= virt;
  418. length = md->length;
  419. if (mem_types[md->type].prot_l1 == 0 &&
  420. (virt & 0xfffff || (virt + off) & 0xfffff || (virt + length) & 0xfffff)) {
  421. printk(KERN_WARNING "BUG: map for 0x%08lx at 0x%08lx can not "
  422. "be mapped using pages, ignoring.\n",
  423. __pfn_to_phys(md->pfn), md->virtual);
  424. return;
  425. }
  426. while ((virt & 0xfffff || (virt + off) & 0xfffff) && length >= PAGE_SIZE) {
  427. alloc_init_page(virt, virt + off, prot_l1, prot_pte);
  428. virt += PAGE_SIZE;
  429. length -= PAGE_SIZE;
  430. }
  431. /* N.B. ARMv6 supersections are only defined to work with domain 0.
  432. * Since domain assignments can in fact be arbitrary, the
  433. * 'domain == 0' check below is required to insure that ARMv6
  434. * supersections are only allocated for domain 0 regardless
  435. * of the actual domain assignments in use.
  436. */
  437. if ((cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())
  438. && domain == 0) {
  439. /*
  440. * Align to supersection boundary if !high pages.
  441. * High pages have already been checked for proper
  442. * alignment above and they will fail the SUPSERSECTION_MASK
  443. * check because of the way the address is encoded into
  444. * offset.
  445. */
  446. if (md->pfn <= 0x100000) {
  447. while ((virt & ~SUPERSECTION_MASK ||
  448. (virt + off) & ~SUPERSECTION_MASK) &&
  449. length >= (PGDIR_SIZE / 2)) {
  450. alloc_init_section(virt, virt + off, prot_sect);
  451. virt += (PGDIR_SIZE / 2);
  452. length -= (PGDIR_SIZE / 2);
  453. }
  454. }
  455. while (length >= SUPERSECTION_SIZE) {
  456. alloc_init_supersection(virt, virt + off, prot_sect);
  457. virt += SUPERSECTION_SIZE;
  458. length -= SUPERSECTION_SIZE;
  459. }
  460. }
  461. /*
  462. * A section mapping covers half a "pgdir" entry.
  463. */
  464. while (length >= (PGDIR_SIZE / 2)) {
  465. alloc_init_section(virt, virt + off, prot_sect);
  466. virt += (PGDIR_SIZE / 2);
  467. length -= (PGDIR_SIZE / 2);
  468. }
  469. while (length >= PAGE_SIZE) {
  470. alloc_init_page(virt, virt + off, prot_l1, prot_pte);
  471. virt += PAGE_SIZE;
  472. length -= PAGE_SIZE;
  473. }
  474. }
  475. /*
  476. * Create the architecture specific mappings
  477. */
  478. void __init iotable_init(struct map_desc *io_desc, int nr)
  479. {
  480. int i;
  481. for (i = 0; i < nr; i++)
  482. create_mapping(io_desc + i);
  483. }
  484. static inline void prepare_page_table(struct meminfo *mi)
  485. {
  486. unsigned long addr;
  487. /*
  488. * Clear out all the mappings below the kernel image.
  489. */
  490. for (addr = 0; addr < MODULE_START; addr += PGDIR_SIZE)
  491. pmd_clear(pmd_off_k(addr));
  492. #ifdef CONFIG_XIP_KERNEL
  493. /* The XIP kernel is mapped in the module area -- skip over it */
  494. addr = ((unsigned long)&_etext + PGDIR_SIZE - 1) & PGDIR_MASK;
  495. #endif
  496. for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
  497. pmd_clear(pmd_off_k(addr));
  498. /*
  499. * Clear out all the kernel space mappings, except for the first
  500. * memory bank, up to the end of the vmalloc region.
  501. */
  502. for (addr = __phys_to_virt(mi->bank[0].start + mi->bank[0].size);
  503. addr < VMALLOC_END; addr += PGDIR_SIZE)
  504. pmd_clear(pmd_off_k(addr));
  505. }
  506. /*
  507. * Reserve the various regions of node 0
  508. */
  509. void __init reserve_node_zero(pg_data_t *pgdat)
  510. {
  511. unsigned long res_size = 0;
  512. /*
  513. * Register the kernel text and data with bootmem.
  514. * Note that this can only be in node 0.
  515. */
  516. #ifdef CONFIG_XIP_KERNEL
  517. reserve_bootmem_node(pgdat, __pa(&__data_start), &_end - &__data_start);
  518. #else
  519. reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
  520. #endif
  521. /*
  522. * Reserve the page tables. These are already in use,
  523. * and can only be in node 0.
  524. */
  525. reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
  526. PTRS_PER_PGD * sizeof(pgd_t));
  527. /*
  528. * Hmm... This should go elsewhere, but we really really need to
  529. * stop things allocating the low memory; ideally we need a better
  530. * implementation of GFP_DMA which does not assume that DMA-able
  531. * memory starts at zero.
  532. */
  533. if (machine_is_integrator() || machine_is_cintegrator())
  534. res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
  535. /*
  536. * These should likewise go elsewhere. They pre-reserve the
  537. * screen memory region at the start of main system memory.
  538. */
  539. if (machine_is_edb7211())
  540. res_size = 0x00020000;
  541. if (machine_is_p720t())
  542. res_size = 0x00014000;
  543. #ifdef CONFIG_SA1111
  544. /*
  545. * Because of the SA1111 DMA bug, we want to preserve our
  546. * precious DMA-able memory...
  547. */
  548. res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
  549. #endif
  550. if (res_size)
  551. reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size);
  552. }
  553. /*
  554. * Set up device the mappings. Since we clear out the page tables for all
  555. * mappings above VMALLOC_END, we will remove any debug device mappings.
  556. * This means you have to be careful how you debug this function, or any
  557. * called function. This means you can't use any function or debugging
  558. * method which may touch any device, otherwise the kernel _will_ crash.
  559. */
  560. static void __init devicemaps_init(struct machine_desc *mdesc)
  561. {
  562. struct map_desc map;
  563. unsigned long addr;
  564. void *vectors;
  565. /*
  566. * Allocate the vector page early.
  567. */
  568. vectors = alloc_bootmem_low_pages(PAGE_SIZE);
  569. BUG_ON(!vectors);
  570. for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
  571. pmd_clear(pmd_off_k(addr));
  572. /*
  573. * Map the kernel if it is XIP.
  574. * It is always first in the modulearea.
  575. */
  576. #ifdef CONFIG_XIP_KERNEL
  577. map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
  578. map.virtual = MODULE_START;
  579. map.length = ((unsigned long)&_etext - map.virtual + ~SECTION_MASK) & SECTION_MASK;
  580. map.type = MT_ROM;
  581. create_mapping(&map);
  582. #endif
  583. /*
  584. * Map the cache flushing regions.
  585. */
  586. #ifdef FLUSH_BASE
  587. map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
  588. map.virtual = FLUSH_BASE;
  589. map.length = SZ_1M;
  590. map.type = MT_CACHECLEAN;
  591. create_mapping(&map);
  592. #endif
  593. #ifdef FLUSH_BASE_MINICACHE
  594. map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
  595. map.virtual = FLUSH_BASE_MINICACHE;
  596. map.length = SZ_1M;
  597. map.type = MT_MINICLEAN;
  598. create_mapping(&map);
  599. #endif
  600. /*
  601. * Create a mapping for the machine vectors at the high-vectors
  602. * location (0xffff0000). If we aren't using high-vectors, also
  603. * create a mapping at the low-vectors virtual address.
  604. */
  605. map.pfn = __phys_to_pfn(virt_to_phys(vectors));
  606. map.virtual = 0xffff0000;
  607. map.length = PAGE_SIZE;
  608. map.type = MT_HIGH_VECTORS;
  609. create_mapping(&map);
  610. if (!vectors_high()) {
  611. map.virtual = 0;
  612. map.type = MT_LOW_VECTORS;
  613. create_mapping(&map);
  614. }
  615. /*
  616. * Ask the machine support to map in the statically mapped devices.
  617. */
  618. if (mdesc->map_io)
  619. mdesc->map_io();
  620. /*
  621. * Finally flush the caches and tlb to ensure that we're in a
  622. * consistent state wrt the writebuffer. This also ensures that
  623. * any write-allocated cache lines in the vector page are written
  624. * back. After this point, we can start to touch devices again.
  625. */
  626. local_flush_tlb_all();
  627. flush_cache_all();
  628. }
  629. /*
  630. * paging_init() sets up the page tables, initialises the zone memory
  631. * maps, and sets up the zero page, bad page and bad page tables.
  632. */
  633. void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
  634. {
  635. void *zero_page;
  636. build_mem_type_table();
  637. prepare_page_table(mi);
  638. bootmem_init(mi);
  639. devicemaps_init(mdesc);
  640. top_pmd = pmd_off_k(0xffff0000);
  641. /*
  642. * allocate the zero page. Note that we count on this going ok.
  643. */
  644. zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
  645. memzero(zero_page, PAGE_SIZE);
  646. empty_zero_page = virt_to_page(zero_page);
  647. flush_dcache_page(empty_zero_page);
  648. }
  649. /*
  650. * In order to soft-boot, we need to insert a 1:1 mapping in place of
  651. * the user-mode pages. This will then ensure that we have predictable
  652. * results when turning the mmu off
  653. */
  654. void setup_mm_for_reboot(char mode)
  655. {
  656. unsigned long base_pmdval;
  657. pgd_t *pgd;
  658. int i;
  659. if (current->mm && current->mm->pgd)
  660. pgd = current->mm->pgd;
  661. else
  662. pgd = init_mm.pgd;
  663. base_pmdval = PMD_SECT_AP_WRITE | PMD_SECT_AP_READ | PMD_TYPE_SECT;
  664. if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
  665. base_pmdval |= PMD_BIT4;
  666. for (i = 0; i < FIRST_USER_PGD_NR + USER_PTRS_PER_PGD; i++, pgd++) {
  667. unsigned long pmdval = (i << PGDIR_SHIFT) | base_pmdval;
  668. pmd_t *pmd;
  669. pmd = pmd_off(pgd, i << PGDIR_SHIFT);
  670. pmd[0] = __pmd(pmdval);
  671. pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
  672. flush_pmd_entry(pmd);
  673. }
  674. }