mmu.c 20 KB

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