mmu.c 21 KB

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