mm-armv.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * linux/arch/arm/mm/mm-armv.c
  3. *
  4. * Copyright (C) 1998-2002 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. * Page table sludge for ARM v3 and v4 processor architectures.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/init.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/highmem.h>
  18. #include <linux/nodemask.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/page.h>
  21. #include <asm/io.h>
  22. #include <asm/setup.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/mach/map.h>
  25. #define CPOLICY_UNCACHED 0
  26. #define CPOLICY_BUFFERED 1
  27. #define CPOLICY_WRITETHROUGH 2
  28. #define CPOLICY_WRITEBACK 3
  29. #define CPOLICY_WRITEALLOC 4
  30. static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
  31. static unsigned int ecc_mask __initdata = 0;
  32. pgprot_t pgprot_kernel;
  33. EXPORT_SYMBOL(pgprot_kernel);
  34. struct cachepolicy {
  35. const char policy[16];
  36. unsigned int cr_mask;
  37. unsigned int pmd;
  38. unsigned int pte;
  39. };
  40. static struct cachepolicy cache_policies[] __initdata = {
  41. {
  42. .policy = "uncached",
  43. .cr_mask = CR_W|CR_C,
  44. .pmd = PMD_SECT_UNCACHED,
  45. .pte = 0,
  46. }, {
  47. .policy = "buffered",
  48. .cr_mask = CR_C,
  49. .pmd = PMD_SECT_BUFFERED,
  50. .pte = PTE_BUFFERABLE,
  51. }, {
  52. .policy = "writethrough",
  53. .cr_mask = 0,
  54. .pmd = PMD_SECT_WT,
  55. .pte = PTE_CACHEABLE,
  56. }, {
  57. .policy = "writeback",
  58. .cr_mask = 0,
  59. .pmd = PMD_SECT_WB,
  60. .pte = PTE_BUFFERABLE|PTE_CACHEABLE,
  61. }, {
  62. .policy = "writealloc",
  63. .cr_mask = 0,
  64. .pmd = PMD_SECT_WBWA,
  65. .pte = PTE_BUFFERABLE|PTE_CACHEABLE,
  66. }
  67. };
  68. /*
  69. * These are useful for identifing cache coherency
  70. * problems by allowing the cache or the cache and
  71. * writebuffer to be turned off. (Note: the write
  72. * buffer should not be on and the cache off).
  73. */
  74. static void __init early_cachepolicy(char **p)
  75. {
  76. int i;
  77. for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
  78. int len = strlen(cache_policies[i].policy);
  79. if (memcmp(*p, cache_policies[i].policy, len) == 0) {
  80. cachepolicy = i;
  81. cr_alignment &= ~cache_policies[i].cr_mask;
  82. cr_no_alignment &= ~cache_policies[i].cr_mask;
  83. *p += len;
  84. break;
  85. }
  86. }
  87. if (i == ARRAY_SIZE(cache_policies))
  88. printk(KERN_ERR "ERROR: unknown or unsupported cache policy\n");
  89. flush_cache_all();
  90. set_cr(cr_alignment);
  91. }
  92. static void __init early_nocache(char **__unused)
  93. {
  94. char *p = "buffered";
  95. printk(KERN_WARNING "nocache is deprecated; use cachepolicy=%s\n", p);
  96. early_cachepolicy(&p);
  97. }
  98. static void __init early_nowrite(char **__unused)
  99. {
  100. char *p = "uncached";
  101. printk(KERN_WARNING "nowb is deprecated; use cachepolicy=%s\n", p);
  102. early_cachepolicy(&p);
  103. }
  104. static void __init early_ecc(char **p)
  105. {
  106. if (memcmp(*p, "on", 2) == 0) {
  107. ecc_mask = PMD_PROTECTION;
  108. *p += 2;
  109. } else if (memcmp(*p, "off", 3) == 0) {
  110. ecc_mask = 0;
  111. *p += 3;
  112. }
  113. }
  114. __early_param("nocache", early_nocache);
  115. __early_param("nowb", early_nowrite);
  116. __early_param("cachepolicy=", early_cachepolicy);
  117. __early_param("ecc=", early_ecc);
  118. static int __init noalign_setup(char *__unused)
  119. {
  120. cr_alignment &= ~CR_A;
  121. cr_no_alignment &= ~CR_A;
  122. set_cr(cr_alignment);
  123. return 1;
  124. }
  125. __setup("noalign", noalign_setup);
  126. #define FIRST_KERNEL_PGD_NR (FIRST_USER_PGD_NR + USER_PTRS_PER_PGD)
  127. /*
  128. * need to get a 16k page for level 1
  129. */
  130. pgd_t *get_pgd_slow(struct mm_struct *mm)
  131. {
  132. pgd_t *new_pgd, *init_pgd;
  133. pmd_t *new_pmd, *init_pmd;
  134. pte_t *new_pte, *init_pte;
  135. new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
  136. if (!new_pgd)
  137. goto no_pgd;
  138. memzero(new_pgd, FIRST_KERNEL_PGD_NR * sizeof(pgd_t));
  139. init_pgd = pgd_offset_k(0);
  140. if (!vectors_high()) {
  141. /*
  142. * This lock is here just to satisfy pmd_alloc and pte_lock
  143. */
  144. spin_lock(&mm->page_table_lock);
  145. /*
  146. * On ARM, first page must always be allocated since it
  147. * contains the machine vectors.
  148. */
  149. new_pmd = pmd_alloc(mm, new_pgd, 0);
  150. if (!new_pmd)
  151. goto no_pmd;
  152. new_pte = pte_alloc_map(mm, new_pmd, 0);
  153. if (!new_pte)
  154. goto no_pte;
  155. init_pmd = pmd_offset(init_pgd, 0);
  156. init_pte = pte_offset_map_nested(init_pmd, 0);
  157. set_pte(new_pte, *init_pte);
  158. pte_unmap_nested(init_pte);
  159. pte_unmap(new_pte);
  160. spin_unlock(&mm->page_table_lock);
  161. }
  162. /*
  163. * Copy over the kernel and IO PGD entries
  164. */
  165. memcpy(new_pgd + FIRST_KERNEL_PGD_NR, init_pgd + FIRST_KERNEL_PGD_NR,
  166. (PTRS_PER_PGD - FIRST_KERNEL_PGD_NR) * sizeof(pgd_t));
  167. clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
  168. return new_pgd;
  169. no_pte:
  170. spin_unlock(&mm->page_table_lock);
  171. pmd_free(new_pmd);
  172. free_pages((unsigned long)new_pgd, 2);
  173. return NULL;
  174. no_pmd:
  175. spin_unlock(&mm->page_table_lock);
  176. free_pages((unsigned long)new_pgd, 2);
  177. return NULL;
  178. no_pgd:
  179. return NULL;
  180. }
  181. void free_pgd_slow(pgd_t *pgd)
  182. {
  183. pmd_t *pmd;
  184. struct page *pte;
  185. if (!pgd)
  186. return;
  187. /* pgd is always present and good */
  188. pmd = (pmd_t *)pgd;
  189. if (pmd_none(*pmd))
  190. goto free;
  191. if (pmd_bad(*pmd)) {
  192. pmd_ERROR(*pmd);
  193. pmd_clear(pmd);
  194. goto free;
  195. }
  196. pte = pmd_page(*pmd);
  197. pmd_clear(pmd);
  198. dec_page_state(nr_page_table_pages);
  199. pte_free(pte);
  200. pmd_free(pmd);
  201. free:
  202. free_pages((unsigned long) pgd, 2);
  203. }
  204. /*
  205. * Create a SECTION PGD between VIRT and PHYS in domain
  206. * DOMAIN with protection PROT. This operates on half-
  207. * pgdir entry increments.
  208. */
  209. static inline void
  210. alloc_init_section(unsigned long virt, unsigned long phys, int prot)
  211. {
  212. pmd_t *pmdp;
  213. pmdp = pmd_offset(pgd_offset_k(virt), virt);
  214. if (virt & (1 << 20))
  215. pmdp++;
  216. *pmdp = __pmd(phys | prot);
  217. flush_pmd_entry(pmdp);
  218. }
  219. /*
  220. * Create a SUPER SECTION PGD between VIRT and PHYS with protection PROT
  221. */
  222. static inline void
  223. alloc_init_supersection(unsigned long virt, unsigned long phys, int prot)
  224. {
  225. int i;
  226. for (i = 0; i < 16; i += 1) {
  227. alloc_init_section(virt, phys & SUPERSECTION_MASK,
  228. prot | PMD_SECT_SUPER);
  229. virt += (PGDIR_SIZE / 2);
  230. phys += (PGDIR_SIZE / 2);
  231. }
  232. }
  233. /*
  234. * Add a PAGE mapping between VIRT and PHYS in domain
  235. * DOMAIN with protection PROT. Note that due to the
  236. * way we map the PTEs, we must allocate two PTE_SIZE'd
  237. * blocks - one for the Linux pte table, and one for
  238. * the hardware pte table.
  239. */
  240. static inline void
  241. alloc_init_page(unsigned long virt, unsigned long phys, unsigned int prot_l1, pgprot_t prot)
  242. {
  243. pmd_t *pmdp;
  244. pte_t *ptep;
  245. pmdp = pmd_offset(pgd_offset_k(virt), virt);
  246. if (pmd_none(*pmdp)) {
  247. unsigned long pmdval;
  248. ptep = alloc_bootmem_low_pages(2 * PTRS_PER_PTE *
  249. sizeof(pte_t));
  250. pmdval = __pa(ptep) | prot_l1;
  251. pmdp[0] = __pmd(pmdval);
  252. pmdp[1] = __pmd(pmdval + 256 * sizeof(pte_t));
  253. flush_pmd_entry(pmdp);
  254. }
  255. ptep = pte_offset_kernel(pmdp, virt);
  256. set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
  257. }
  258. /*
  259. * Clear any PGD mapping. On a two-level page table system,
  260. * the clearance is done by the middle-level functions (pmd)
  261. * rather than the top-level (pgd) functions.
  262. */
  263. static inline void clear_mapping(unsigned long virt)
  264. {
  265. pmd_clear(pmd_offset(pgd_offset_k(virt), virt));
  266. }
  267. struct mem_types {
  268. unsigned int prot_pte;
  269. unsigned int prot_l1;
  270. unsigned int prot_sect;
  271. unsigned int domain;
  272. };
  273. static struct mem_types mem_types[] __initdata = {
  274. [MT_DEVICE] = {
  275. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  276. L_PTE_WRITE,
  277. .prot_l1 = PMD_TYPE_TABLE,
  278. .prot_sect = PMD_TYPE_SECT | PMD_SECT_UNCACHED |
  279. PMD_SECT_AP_WRITE,
  280. .domain = DOMAIN_IO,
  281. },
  282. [MT_CACHECLEAN] = {
  283. .prot_sect = PMD_TYPE_SECT,
  284. .domain = DOMAIN_KERNEL,
  285. },
  286. [MT_MINICLEAN] = {
  287. .prot_sect = PMD_TYPE_SECT | PMD_SECT_MINICACHE,
  288. .domain = DOMAIN_KERNEL,
  289. },
  290. [MT_LOW_VECTORS] = {
  291. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  292. L_PTE_EXEC,
  293. .prot_l1 = PMD_TYPE_TABLE,
  294. .domain = DOMAIN_USER,
  295. },
  296. [MT_HIGH_VECTORS] = {
  297. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  298. L_PTE_USER | L_PTE_EXEC,
  299. .prot_l1 = PMD_TYPE_TABLE,
  300. .domain = DOMAIN_USER,
  301. },
  302. [MT_MEMORY] = {
  303. .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
  304. .domain = DOMAIN_KERNEL,
  305. },
  306. [MT_ROM] = {
  307. .prot_sect = PMD_TYPE_SECT,
  308. .domain = DOMAIN_KERNEL,
  309. },
  310. [MT_IXP2000_DEVICE] = { /* IXP2400 requires XCB=101 for on-chip I/O */
  311. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  312. L_PTE_WRITE,
  313. .prot_l1 = PMD_TYPE_TABLE,
  314. .prot_sect = PMD_TYPE_SECT | PMD_SECT_UNCACHED |
  315. PMD_SECT_AP_WRITE | PMD_SECT_BUFFERABLE |
  316. PMD_SECT_TEX(1),
  317. .domain = DOMAIN_IO,
  318. }
  319. };
  320. /*
  321. * Adjust the PMD section entries according to the CPU in use.
  322. */
  323. static void __init build_mem_type_table(void)
  324. {
  325. struct cachepolicy *cp;
  326. unsigned int cr = get_cr();
  327. int cpu_arch = cpu_architecture();
  328. int i;
  329. #if defined(CONFIG_CPU_DCACHE_DISABLE)
  330. if (cachepolicy > CPOLICY_BUFFERED)
  331. cachepolicy = CPOLICY_BUFFERED;
  332. #elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
  333. if (cachepolicy > CPOLICY_WRITETHROUGH)
  334. cachepolicy = CPOLICY_WRITETHROUGH;
  335. #endif
  336. if (cpu_arch < CPU_ARCH_ARMv5) {
  337. if (cachepolicy >= CPOLICY_WRITEALLOC)
  338. cachepolicy = CPOLICY_WRITEBACK;
  339. ecc_mask = 0;
  340. }
  341. if (cpu_arch <= CPU_ARCH_ARMv5) {
  342. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  343. if (mem_types[i].prot_l1)
  344. mem_types[i].prot_l1 |= PMD_BIT4;
  345. if (mem_types[i].prot_sect)
  346. mem_types[i].prot_sect |= PMD_BIT4;
  347. }
  348. }
  349. /*
  350. * ARMv6 and above have extended page tables.
  351. */
  352. if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
  353. /*
  354. * bit 4 becomes XN which we must clear for the
  355. * kernel memory mapping.
  356. */
  357. mem_types[MT_MEMORY].prot_sect &= ~PMD_BIT4;
  358. mem_types[MT_ROM].prot_sect &= ~PMD_BIT4;
  359. /*
  360. * Mark cache clean areas and XIP ROM read only
  361. * from SVC mode and no access from userspace.
  362. */
  363. mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  364. mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  365. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  366. }
  367. cp = &cache_policies[cachepolicy];
  368. if (cpu_arch >= CPU_ARCH_ARMv5) {
  369. mem_types[MT_LOW_VECTORS].prot_pte |= cp->pte & PTE_CACHEABLE;
  370. mem_types[MT_HIGH_VECTORS].prot_pte |= cp->pte & PTE_CACHEABLE;
  371. } else {
  372. mem_types[MT_LOW_VECTORS].prot_pte |= cp->pte;
  373. mem_types[MT_HIGH_VECTORS].prot_pte |= cp->pte;
  374. mem_types[MT_MINICLEAN].prot_sect &= ~PMD_SECT_TEX(1);
  375. }
  376. mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
  377. mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
  378. mem_types[MT_MEMORY].prot_sect |= ecc_mask | cp->pmd;
  379. mem_types[MT_ROM].prot_sect |= cp->pmd;
  380. for (i = 0; i < 16; i++) {
  381. unsigned long v = pgprot_val(protection_map[i]);
  382. v &= (~(PTE_BUFFERABLE|PTE_CACHEABLE)) | cp->pte;
  383. protection_map[i] = __pgprot(v);
  384. }
  385. pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
  386. L_PTE_DIRTY | L_PTE_WRITE |
  387. L_PTE_EXEC | cp->pte);
  388. switch (cp->pmd) {
  389. case PMD_SECT_WT:
  390. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
  391. break;
  392. case PMD_SECT_WB:
  393. case PMD_SECT_WBWA:
  394. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
  395. break;
  396. }
  397. printk("Memory policy: ECC %sabled, Data cache %s\n",
  398. ecc_mask ? "en" : "dis", cp->policy);
  399. }
  400. #define vectors_base() (vectors_high() ? 0xffff0000 : 0)
  401. /*
  402. * Create the page directory entries and any necessary
  403. * page tables for the mapping specified by `md'. We
  404. * are able to cope here with varying sizes and address
  405. * offsets, and we take full advantage of sections and
  406. * supersections.
  407. */
  408. static void __init create_mapping(struct map_desc *md)
  409. {
  410. unsigned long virt, length;
  411. int prot_sect, prot_l1, domain;
  412. pgprot_t prot_pte;
  413. long off;
  414. if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
  415. printk(KERN_WARNING "BUG: not creating mapping for "
  416. "0x%08lx at 0x%08lx in user region\n",
  417. md->physical, md->virtual);
  418. return;
  419. }
  420. if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
  421. md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
  422. printk(KERN_WARNING "BUG: mapping for 0x%08lx at 0x%08lx "
  423. "overlaps vmalloc space\n",
  424. md->physical, md->virtual);
  425. }
  426. domain = mem_types[md->type].domain;
  427. prot_pte = __pgprot(mem_types[md->type].prot_pte);
  428. prot_l1 = mem_types[md->type].prot_l1 | PMD_DOMAIN(domain);
  429. prot_sect = mem_types[md->type].prot_sect | PMD_DOMAIN(domain);
  430. virt = md->virtual;
  431. off = md->physical - virt;
  432. length = md->length;
  433. if (mem_types[md->type].prot_l1 == 0 &&
  434. (virt & 0xfffff || (virt + off) & 0xfffff || (virt + length) & 0xfffff)) {
  435. printk(KERN_WARNING "BUG: map for 0x%08lx at 0x%08lx can not "
  436. "be mapped using pages, ignoring.\n",
  437. md->physical, md->virtual);
  438. return;
  439. }
  440. while ((virt & 0xfffff || (virt + off) & 0xfffff) && length >= PAGE_SIZE) {
  441. alloc_init_page(virt, virt + off, prot_l1, prot_pte);
  442. virt += PAGE_SIZE;
  443. length -= PAGE_SIZE;
  444. }
  445. /* N.B. ARMv6 supersections are only defined to work with domain 0.
  446. * Since domain assignments can in fact be arbitrary, the
  447. * 'domain == 0' check below is required to insure that ARMv6
  448. * supersections are only allocated for domain 0 regardless
  449. * of the actual domain assignments in use.
  450. */
  451. if (cpu_architecture() >= CPU_ARCH_ARMv6 && domain == 0) {
  452. /* Align to supersection boundary */
  453. while ((virt & ~SUPERSECTION_MASK || (virt + off) &
  454. ~SUPERSECTION_MASK) && length >= (PGDIR_SIZE / 2)) {
  455. alloc_init_section(virt, virt + off, prot_sect);
  456. virt += (PGDIR_SIZE / 2);
  457. length -= (PGDIR_SIZE / 2);
  458. }
  459. while (length >= SUPERSECTION_SIZE) {
  460. alloc_init_supersection(virt, virt + off, prot_sect);
  461. virt += SUPERSECTION_SIZE;
  462. length -= SUPERSECTION_SIZE;
  463. }
  464. }
  465. /*
  466. * A section mapping covers half a "pgdir" entry.
  467. */
  468. while (length >= (PGDIR_SIZE / 2)) {
  469. alloc_init_section(virt, virt + off, prot_sect);
  470. virt += (PGDIR_SIZE / 2);
  471. length -= (PGDIR_SIZE / 2);
  472. }
  473. while (length >= PAGE_SIZE) {
  474. alloc_init_page(virt, virt + off, prot_l1, prot_pte);
  475. virt += PAGE_SIZE;
  476. length -= PAGE_SIZE;
  477. }
  478. }
  479. /*
  480. * In order to soft-boot, we need to insert a 1:1 mapping in place of
  481. * the user-mode pages. This will then ensure that we have predictable
  482. * results when turning the mmu off
  483. */
  484. void setup_mm_for_reboot(char mode)
  485. {
  486. unsigned long pmdval;
  487. pgd_t *pgd;
  488. pmd_t *pmd;
  489. int i;
  490. int cpu_arch = cpu_architecture();
  491. if (current->mm && current->mm->pgd)
  492. pgd = current->mm->pgd;
  493. else
  494. pgd = init_mm.pgd;
  495. for (i = 0; i < FIRST_USER_PGD_NR + USER_PTRS_PER_PGD; i++) {
  496. pmdval = (i << PGDIR_SHIFT) |
  497. PMD_SECT_AP_WRITE | PMD_SECT_AP_READ |
  498. PMD_TYPE_SECT;
  499. if (cpu_arch <= CPU_ARCH_ARMv5)
  500. pmdval |= PMD_BIT4;
  501. pmd = pmd_offset(pgd + i, i << PGDIR_SHIFT);
  502. pmd[0] = __pmd(pmdval);
  503. pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
  504. flush_pmd_entry(pmd);
  505. }
  506. }
  507. extern void _stext, _etext;
  508. /*
  509. * Setup initial mappings. We use the page we allocated for zero page to hold
  510. * the mappings, which will get overwritten by the vectors in traps_init().
  511. * The mappings must be in virtual address order.
  512. */
  513. void __init memtable_init(struct meminfo *mi)
  514. {
  515. struct map_desc *init_maps, *p, *q;
  516. unsigned long address = 0;
  517. int i;
  518. build_mem_type_table();
  519. init_maps = p = alloc_bootmem_low_pages(PAGE_SIZE);
  520. #ifdef CONFIG_XIP_KERNEL
  521. p->physical = CONFIG_XIP_PHYS_ADDR & PMD_MASK;
  522. p->virtual = (unsigned long)&_stext & PMD_MASK;
  523. p->length = ((unsigned long)&_etext - p->virtual + ~PMD_MASK) & PMD_MASK;
  524. p->type = MT_ROM;
  525. p ++;
  526. #endif
  527. for (i = 0; i < mi->nr_banks; i++) {
  528. if (mi->bank[i].size == 0)
  529. continue;
  530. p->physical = mi->bank[i].start;
  531. p->virtual = __phys_to_virt(p->physical);
  532. p->length = mi->bank[i].size;
  533. p->type = MT_MEMORY;
  534. p ++;
  535. }
  536. #ifdef FLUSH_BASE
  537. p->physical = FLUSH_BASE_PHYS;
  538. p->virtual = FLUSH_BASE;
  539. p->length = PGDIR_SIZE;
  540. p->type = MT_CACHECLEAN;
  541. p ++;
  542. #endif
  543. #ifdef FLUSH_BASE_MINICACHE
  544. p->physical = FLUSH_BASE_PHYS + PGDIR_SIZE;
  545. p->virtual = FLUSH_BASE_MINICACHE;
  546. p->length = PGDIR_SIZE;
  547. p->type = MT_MINICLEAN;
  548. p ++;
  549. #endif
  550. /*
  551. * Go through the initial mappings, but clear out any
  552. * pgdir entries that are not in the description.
  553. */
  554. q = init_maps;
  555. do {
  556. if (address < q->virtual || q == p) {
  557. clear_mapping(address);
  558. address += PGDIR_SIZE;
  559. } else {
  560. create_mapping(q);
  561. address = q->virtual + q->length;
  562. address = (address + PGDIR_SIZE - 1) & PGDIR_MASK;
  563. q ++;
  564. }
  565. } while (address != 0);
  566. /*
  567. * Create a mapping for the machine vectors at the high-vectors
  568. * location (0xffff0000). If we aren't using high-vectors, also
  569. * create a mapping at the low-vectors virtual address.
  570. */
  571. init_maps->physical = virt_to_phys(init_maps);
  572. init_maps->virtual = 0xffff0000;
  573. init_maps->length = PAGE_SIZE;
  574. init_maps->type = MT_HIGH_VECTORS;
  575. create_mapping(init_maps);
  576. if (!vectors_high()) {
  577. init_maps->virtual = 0;
  578. init_maps->type = MT_LOW_VECTORS;
  579. create_mapping(init_maps);
  580. }
  581. flush_cache_all();
  582. flush_tlb_all();
  583. }
  584. /*
  585. * Create the architecture specific mappings
  586. */
  587. void __init iotable_init(struct map_desc *io_desc, int nr)
  588. {
  589. int i;
  590. for (i = 0; i < nr; i++)
  591. create_mapping(io_desc + i);
  592. }
  593. static inline void
  594. free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
  595. {
  596. struct page *start_pg, *end_pg;
  597. unsigned long pg, pgend;
  598. /*
  599. * Convert start_pfn/end_pfn to a struct page pointer.
  600. */
  601. start_pg = pfn_to_page(start_pfn);
  602. end_pg = pfn_to_page(end_pfn);
  603. /*
  604. * Convert to physical addresses, and
  605. * round start upwards and end downwards.
  606. */
  607. pg = PAGE_ALIGN(__pa(start_pg));
  608. pgend = __pa(end_pg) & PAGE_MASK;
  609. /*
  610. * If there are free pages between these,
  611. * free the section of the memmap array.
  612. */
  613. if (pg < pgend)
  614. free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
  615. }
  616. static inline void free_unused_memmap_node(int node, struct meminfo *mi)
  617. {
  618. unsigned long bank_start, prev_bank_end = 0;
  619. unsigned int i;
  620. /*
  621. * [FIXME] This relies on each bank being in address order. This
  622. * may not be the case, especially if the user has provided the
  623. * information on the command line.
  624. */
  625. for (i = 0; i < mi->nr_banks; i++) {
  626. if (mi->bank[i].size == 0 || mi->bank[i].node != node)
  627. continue;
  628. bank_start = mi->bank[i].start >> PAGE_SHIFT;
  629. if (bank_start < prev_bank_end) {
  630. printk(KERN_ERR "MEM: unordered memory banks. "
  631. "Not freeing memmap.\n");
  632. break;
  633. }
  634. /*
  635. * If we had a previous bank, and there is a space
  636. * between the current bank and the previous, free it.
  637. */
  638. if (prev_bank_end && prev_bank_end != bank_start)
  639. free_memmap(node, prev_bank_end, bank_start);
  640. prev_bank_end = PAGE_ALIGN(mi->bank[i].start +
  641. mi->bank[i].size) >> PAGE_SHIFT;
  642. }
  643. }
  644. /*
  645. * The mem_map array can get very big. Free
  646. * the unused area of the memory map.
  647. */
  648. void __init create_memmap_holes(struct meminfo *mi)
  649. {
  650. int node;
  651. for_each_online_node(node)
  652. free_unused_memmap_node(node, mi);
  653. }