mmu.c 22 KB

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