mmu.c 20 KB

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