PageRenderTime 2003ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/arm/mm/mmu.c

https://github.com/ab3416/linux-2.6
C | 1052 lines | 718 code | 120 blank | 214 comment | 120 complexity | df173adcdacb2f2204507bba5e110a08 MD5 | raw file
  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/mman.h>
  15. #include <linux/nodemask.h>
  16. #include <linux/memblock.h>
  17. #include <linux/fs.h>
  18. #include <asm/cputype.h>
  19. #include <asm/sections.h>
  20. #include <asm/cachetype.h>
  21. #include <asm/setup.h>
  22. #include <asm/sizes.h>
  23. #include <asm/smp_plat.h>
  24. #include <asm/tlb.h>
  25. #include <asm/highmem.h>
  26. #include <asm/traps.h>
  27. #include <asm/mach/arch.h>
  28. #include <asm/mach/map.h>
  29. #include "mm.h"
  30. /*
  31. * empty_zero_page is a special page that is used for
  32. * zero-initialized data and COW.
  33. */
  34. struct page *empty_zero_page;
  35. EXPORT_SYMBOL(empty_zero_page);
  36. /*
  37. * The pmd table for the upper-most set of pages.
  38. */
  39. pmd_t *top_pmd;
  40. #define CPOLICY_UNCACHED 0
  41. #define CPOLICY_BUFFERED 1
  42. #define CPOLICY_WRITETHROUGH 2
  43. #define CPOLICY_WRITEBACK 3
  44. #define CPOLICY_WRITEALLOC 4
  45. static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
  46. static unsigned int ecc_mask __initdata = 0;
  47. pgprot_t pgprot_user;
  48. pgprot_t pgprot_kernel;
  49. EXPORT_SYMBOL(pgprot_user);
  50. EXPORT_SYMBOL(pgprot_kernel);
  51. struct cachepolicy {
  52. const char policy[16];
  53. unsigned int cr_mask;
  54. unsigned int pmd;
  55. pteval_t pte;
  56. };
  57. static struct cachepolicy cache_policies[] __initdata = {
  58. {
  59. .policy = "uncached",
  60. .cr_mask = CR_W|CR_C,
  61. .pmd = PMD_SECT_UNCACHED,
  62. .pte = L_PTE_MT_UNCACHED,
  63. }, {
  64. .policy = "buffered",
  65. .cr_mask = CR_C,
  66. .pmd = PMD_SECT_BUFFERED,
  67. .pte = L_PTE_MT_BUFFERABLE,
  68. }, {
  69. .policy = "writethrough",
  70. .cr_mask = 0,
  71. .pmd = PMD_SECT_WT,
  72. .pte = L_PTE_MT_WRITETHROUGH,
  73. }, {
  74. .policy = "writeback",
  75. .cr_mask = 0,
  76. .pmd = PMD_SECT_WB,
  77. .pte = L_PTE_MT_WRITEBACK,
  78. }, {
  79. .policy = "writealloc",
  80. .cr_mask = 0,
  81. .pmd = PMD_SECT_WBWA,
  82. .pte = L_PTE_MT_WRITEALLOC,
  83. }
  84. };
  85. /*
  86. * These are useful for identifying cache coherency
  87. * problems by allowing the cache or the cache and
  88. * writebuffer to be turned off. (Note: the write
  89. * buffer should not be on and the cache off).
  90. */
  91. static int __init early_cachepolicy(char *p)
  92. {
  93. int i;
  94. for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
  95. int len = strlen(cache_policies[i].policy);
  96. if (memcmp(p, cache_policies[i].policy, len) == 0) {
  97. cachepolicy = i;
  98. cr_alignment &= ~cache_policies[i].cr_mask;
  99. cr_no_alignment &= ~cache_policies[i].cr_mask;
  100. break;
  101. }
  102. }
  103. if (i == ARRAY_SIZE(cache_policies))
  104. printk(KERN_ERR "ERROR: unknown or unsupported cache policy\n");
  105. /*
  106. * This restriction is partly to do with the way we boot; it is
  107. * unpredictable to have memory mapped using two different sets of
  108. * memory attributes (shared, type, and cache attribs). We can not
  109. * change these attributes once the initial assembly has setup the
  110. * page tables.
  111. */
  112. if (cpu_architecture() >= CPU_ARCH_ARMv6) {
  113. printk(KERN_WARNING "Only cachepolicy=writeback supported on ARMv6 and later\n");
  114. cachepolicy = CPOLICY_WRITEBACK;
  115. }
  116. flush_cache_all();
  117. set_cr(cr_alignment);
  118. return 0;
  119. }
  120. early_param("cachepolicy", early_cachepolicy);
  121. static int __init early_nocache(char *__unused)
  122. {
  123. char *p = "buffered";
  124. printk(KERN_WARNING "nocache is deprecated; use cachepolicy=%s\n", p);
  125. early_cachepolicy(p);
  126. return 0;
  127. }
  128. early_param("nocache", early_nocache);
  129. static int __init early_nowrite(char *__unused)
  130. {
  131. char *p = "uncached";
  132. printk(KERN_WARNING "nowb is deprecated; use cachepolicy=%s\n", p);
  133. early_cachepolicy(p);
  134. return 0;
  135. }
  136. early_param("nowb", early_nowrite);
  137. static int __init early_ecc(char *p)
  138. {
  139. if (memcmp(p, "on", 2) == 0)
  140. ecc_mask = PMD_PROTECTION;
  141. else if (memcmp(p, "off", 3) == 0)
  142. ecc_mask = 0;
  143. return 0;
  144. }
  145. early_param("ecc", early_ecc);
  146. static int __init noalign_setup(char *__unused)
  147. {
  148. cr_alignment &= ~CR_A;
  149. cr_no_alignment &= ~CR_A;
  150. set_cr(cr_alignment);
  151. return 1;
  152. }
  153. __setup("noalign", noalign_setup);
  154. #ifndef CONFIG_SMP
  155. void adjust_cr(unsigned long mask, unsigned long set)
  156. {
  157. unsigned long flags;
  158. mask &= ~CR_A;
  159. set &= mask;
  160. local_irq_save(flags);
  161. cr_no_alignment = (cr_no_alignment & ~mask) | set;
  162. cr_alignment = (cr_alignment & ~mask) | set;
  163. set_cr((get_cr() & ~mask) | set);
  164. local_irq_restore(flags);
  165. }
  166. #endif
  167. #define PROT_PTE_DEVICE L_PTE_PRESENT|L_PTE_YOUNG|L_PTE_DIRTY|L_PTE_XN
  168. #define PROT_SECT_DEVICE PMD_TYPE_SECT|PMD_SECT_AP_WRITE
  169. static struct mem_type mem_types[] = {
  170. [MT_DEVICE] = { /* Strongly ordered / ARMv6 shared device */
  171. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_SHARED |
  172. L_PTE_SHARED,
  173. .prot_l1 = PMD_TYPE_TABLE,
  174. .prot_sect = PROT_SECT_DEVICE | PMD_SECT_S,
  175. .domain = DOMAIN_IO,
  176. },
  177. [MT_DEVICE_NONSHARED] = { /* ARMv6 non-shared device */
  178. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_NONSHARED,
  179. .prot_l1 = PMD_TYPE_TABLE,
  180. .prot_sect = PROT_SECT_DEVICE,
  181. .domain = DOMAIN_IO,
  182. },
  183. [MT_DEVICE_CACHED] = { /* ioremap_cached */
  184. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_CACHED,
  185. .prot_l1 = PMD_TYPE_TABLE,
  186. .prot_sect = PROT_SECT_DEVICE | PMD_SECT_WB,
  187. .domain = DOMAIN_IO,
  188. },
  189. [MT_DEVICE_WC] = { /* ioremap_wc */
  190. .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_WC,
  191. .prot_l1 = PMD_TYPE_TABLE,
  192. .prot_sect = PROT_SECT_DEVICE,
  193. .domain = DOMAIN_IO,
  194. },
  195. [MT_UNCACHED] = {
  196. .prot_pte = PROT_PTE_DEVICE,
  197. .prot_l1 = PMD_TYPE_TABLE,
  198. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
  199. .domain = DOMAIN_IO,
  200. },
  201. [MT_CACHECLEAN] = {
  202. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
  203. .domain = DOMAIN_KERNEL,
  204. },
  205. [MT_MINICLEAN] = {
  206. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN | PMD_SECT_MINICACHE,
  207. .domain = DOMAIN_KERNEL,
  208. },
  209. [MT_LOW_VECTORS] = {
  210. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  211. L_PTE_RDONLY,
  212. .prot_l1 = PMD_TYPE_TABLE,
  213. .domain = DOMAIN_USER,
  214. },
  215. [MT_HIGH_VECTORS] = {
  216. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  217. L_PTE_USER | L_PTE_RDONLY,
  218. .prot_l1 = PMD_TYPE_TABLE,
  219. .domain = DOMAIN_USER,
  220. },
  221. [MT_MEMORY] = {
  222. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY,
  223. .prot_l1 = PMD_TYPE_TABLE,
  224. .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
  225. .domain = DOMAIN_KERNEL,
  226. },
  227. [MT_ROM] = {
  228. .prot_sect = PMD_TYPE_SECT,
  229. .domain = DOMAIN_KERNEL,
  230. },
  231. [MT_MEMORY_NONCACHED] = {
  232. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  233. L_PTE_MT_BUFFERABLE,
  234. .prot_l1 = PMD_TYPE_TABLE,
  235. .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
  236. .domain = DOMAIN_KERNEL,
  237. },
  238. [MT_MEMORY_DTCM] = {
  239. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  240. L_PTE_XN,
  241. .prot_l1 = PMD_TYPE_TABLE,
  242. .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
  243. .domain = DOMAIN_KERNEL,
  244. },
  245. [MT_MEMORY_ITCM] = {
  246. .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY,
  247. .prot_l1 = PMD_TYPE_TABLE,
  248. .domain = DOMAIN_KERNEL,
  249. },
  250. };
  251. const struct mem_type *get_mem_type(unsigned int type)
  252. {
  253. return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
  254. }
  255. EXPORT_SYMBOL(get_mem_type);
  256. /*
  257. * Adjust the PMD section entries according to the CPU in use.
  258. */
  259. static void __init build_mem_type_table(void)
  260. {
  261. struct cachepolicy *cp;
  262. unsigned int cr = get_cr();
  263. unsigned int user_pgprot, kern_pgprot, vecs_pgprot;
  264. int cpu_arch = cpu_architecture();
  265. int i;
  266. if (cpu_arch < CPU_ARCH_ARMv6) {
  267. #if defined(CONFIG_CPU_DCACHE_DISABLE)
  268. if (cachepolicy > CPOLICY_BUFFERED)
  269. cachepolicy = CPOLICY_BUFFERED;
  270. #elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
  271. if (cachepolicy > CPOLICY_WRITETHROUGH)
  272. cachepolicy = CPOLICY_WRITETHROUGH;
  273. #endif
  274. }
  275. if (cpu_arch < CPU_ARCH_ARMv5) {
  276. if (cachepolicy >= CPOLICY_WRITEALLOC)
  277. cachepolicy = CPOLICY_WRITEBACK;
  278. ecc_mask = 0;
  279. }
  280. if (is_smp())
  281. cachepolicy = CPOLICY_WRITEALLOC;
  282. /*
  283. * Strip out features not present on earlier architectures.
  284. * Pre-ARMv5 CPUs don't have TEX bits. Pre-ARMv6 CPUs or those
  285. * without extended page tables don't have the 'Shared' bit.
  286. */
  287. if (cpu_arch < CPU_ARCH_ARMv5)
  288. for (i = 0; i < ARRAY_SIZE(mem_types); i++)
  289. mem_types[i].prot_sect &= ~PMD_SECT_TEX(7);
  290. if ((cpu_arch < CPU_ARCH_ARMv6 || !(cr & CR_XP)) && !cpu_is_xsc3())
  291. for (i = 0; i < ARRAY_SIZE(mem_types); i++)
  292. mem_types[i].prot_sect &= ~PMD_SECT_S;
  293. /*
  294. * ARMv5 and lower, bit 4 must be set for page tables (was: cache
  295. * "update-able on write" bit on ARM610). However, Xscale and
  296. * Xscale3 require this bit to be cleared.
  297. */
  298. if (cpu_is_xscale() || cpu_is_xsc3()) {
  299. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  300. mem_types[i].prot_sect &= ~PMD_BIT4;
  301. mem_types[i].prot_l1 &= ~PMD_BIT4;
  302. }
  303. } else if (cpu_arch < CPU_ARCH_ARMv6) {
  304. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  305. if (mem_types[i].prot_l1)
  306. mem_types[i].prot_l1 |= PMD_BIT4;
  307. if (mem_types[i].prot_sect)
  308. mem_types[i].prot_sect |= PMD_BIT4;
  309. }
  310. }
  311. /*
  312. * Mark the device areas according to the CPU/architecture.
  313. */
  314. if (cpu_is_xsc3() || (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP))) {
  315. if (!cpu_is_xsc3()) {
  316. /*
  317. * Mark device regions on ARMv6+ as execute-never
  318. * to prevent speculative instruction fetches.
  319. */
  320. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_XN;
  321. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_XN;
  322. mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_XN;
  323. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_XN;
  324. }
  325. if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
  326. /*
  327. * For ARMv7 with TEX remapping,
  328. * - shared device is SXCB=1100
  329. * - nonshared device is SXCB=0100
  330. * - write combine device mem is SXCB=0001
  331. * (Uncached Normal memory)
  332. */
  333. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1);
  334. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(1);
  335. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
  336. } else if (cpu_is_xsc3()) {
  337. /*
  338. * For Xscale3,
  339. * - shared device is TEXCB=00101
  340. * - nonshared device is TEXCB=01000
  341. * - write combine device mem is TEXCB=00100
  342. * (Inner/Outer Uncacheable in xsc3 parlance)
  343. */
  344. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1) | PMD_SECT_BUFFERED;
  345. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
  346. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
  347. } else {
  348. /*
  349. * For ARMv6 and ARMv7 without TEX remapping,
  350. * - shared device is TEXCB=00001
  351. * - nonshared device is TEXCB=01000
  352. * - write combine device mem is TEXCB=00100
  353. * (Uncached Normal in ARMv6 parlance).
  354. */
  355. mem_types[MT_DEVICE].prot_sect |= PMD_SECT_BUFFERED;
  356. mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
  357. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
  358. }
  359. } else {
  360. /*
  361. * On others, write combining is "Uncached/Buffered"
  362. */
  363. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
  364. }
  365. /*
  366. * Now deal with the memory-type mappings
  367. */
  368. cp = &cache_policies[cachepolicy];
  369. vecs_pgprot = kern_pgprot = user_pgprot = cp->pte;
  370. /*
  371. * Only use write-through for non-SMP systems
  372. */
  373. if (!is_smp() && cpu_arch >= CPU_ARCH_ARMv5 && cachepolicy > CPOLICY_WRITETHROUGH)
  374. vecs_pgprot = cache_policies[CPOLICY_WRITETHROUGH].pte;
  375. /*
  376. * Enable CPU-specific coherency if supported.
  377. * (Only available on XSC3 at the moment.)
  378. */
  379. if (arch_is_coherent() && cpu_is_xsc3()) {
  380. mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
  381. mem_types[MT_MEMORY].prot_pte |= L_PTE_SHARED;
  382. mem_types[MT_MEMORY_NONCACHED].prot_sect |= PMD_SECT_S;
  383. mem_types[MT_MEMORY_NONCACHED].prot_pte |= L_PTE_SHARED;
  384. }
  385. /*
  386. * ARMv6 and above have extended page tables.
  387. */
  388. if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
  389. /*
  390. * Mark cache clean areas and XIP ROM read only
  391. * from SVC mode and no access from userspace.
  392. */
  393. mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  394. mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  395. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
  396. if (is_smp()) {
  397. /*
  398. * Mark memory with the "shared" attribute
  399. * for SMP systems
  400. */
  401. user_pgprot |= L_PTE_SHARED;
  402. kern_pgprot |= L_PTE_SHARED;
  403. vecs_pgprot |= L_PTE_SHARED;
  404. mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_S;
  405. mem_types[MT_DEVICE_WC].prot_pte |= L_PTE_SHARED;
  406. mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_S;
  407. mem_types[MT_DEVICE_CACHED].prot_pte |= L_PTE_SHARED;
  408. mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
  409. mem_types[MT_MEMORY].prot_pte |= L_PTE_SHARED;
  410. mem_types[MT_MEMORY_NONCACHED].prot_sect |= PMD_SECT_S;
  411. mem_types[MT_MEMORY_NONCACHED].prot_pte |= L_PTE_SHARED;
  412. }
  413. }
  414. /*
  415. * Non-cacheable Normal - intended for memory areas that must
  416. * not cause dirty cache line writebacks when used
  417. */
  418. if (cpu_arch >= CPU_ARCH_ARMv6) {
  419. if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
  420. /* Non-cacheable Normal is XCB = 001 */
  421. mem_types[MT_MEMORY_NONCACHED].prot_sect |=
  422. PMD_SECT_BUFFERED;
  423. } else {
  424. /* For both ARMv6 and non-TEX-remapping ARMv7 */
  425. mem_types[MT_MEMORY_NONCACHED].prot_sect |=
  426. PMD_SECT_TEX(1);
  427. }
  428. } else {
  429. mem_types[MT_MEMORY_NONCACHED].prot_sect |= PMD_SECT_BUFFERABLE;
  430. }
  431. for (i = 0; i < 16; i++) {
  432. unsigned long v = pgprot_val(protection_map[i]);
  433. protection_map[i] = __pgprot(v | user_pgprot);
  434. }
  435. mem_types[MT_LOW_VECTORS].prot_pte |= vecs_pgprot;
  436. mem_types[MT_HIGH_VECTORS].prot_pte |= vecs_pgprot;
  437. pgprot_user = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | user_pgprot);
  438. pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
  439. L_PTE_DIRTY | kern_pgprot);
  440. mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
  441. mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
  442. mem_types[MT_MEMORY].prot_sect |= ecc_mask | cp->pmd;
  443. mem_types[MT_MEMORY].prot_pte |= kern_pgprot;
  444. mem_types[MT_MEMORY_NONCACHED].prot_sect |= ecc_mask;
  445. mem_types[MT_ROM].prot_sect |= cp->pmd;
  446. switch (cp->pmd) {
  447. case PMD_SECT_WT:
  448. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
  449. break;
  450. case PMD_SECT_WB:
  451. case PMD_SECT_WBWA:
  452. mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
  453. break;
  454. }
  455. printk("Memory policy: ECC %sabled, Data cache %s\n",
  456. ecc_mask ? "en" : "dis", cp->policy);
  457. for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
  458. struct mem_type *t = &mem_types[i];
  459. if (t->prot_l1)
  460. t->prot_l1 |= PMD_DOMAIN(t->domain);
  461. if (t->prot_sect)
  462. t->prot_sect |= PMD_DOMAIN(t->domain);
  463. }
  464. }
  465. #ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
  466. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  467. unsigned long size, pgprot_t vma_prot)
  468. {
  469. if (!pfn_valid(pfn))
  470. return pgprot_noncached(vma_prot);
  471. else if (file->f_flags & O_SYNC)
  472. return pgprot_writecombine(vma_prot);
  473. return vma_prot;
  474. }
  475. EXPORT_SYMBOL(phys_mem_access_prot);
  476. #endif
  477. #define vectors_base() (vectors_high() ? 0xffff0000 : 0)
  478. static void __init *early_alloc(unsigned long sz)
  479. {
  480. void *ptr = __va(memblock_alloc(sz, sz));
  481. memset(ptr, 0, sz);
  482. return ptr;
  483. }
  484. static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr, unsigned long prot)
  485. {
  486. if (pmd_none(*pmd)) {
  487. pte_t *pte = early_alloc(PTE_HWTABLE_OFF + PTE_HWTABLE_SIZE);
  488. __pmd_populate(pmd, __pa(pte), prot);
  489. }
  490. BUG_ON(pmd_bad(*pmd));
  491. return pte_offset_kernel(pmd, addr);
  492. }
  493. static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
  494. unsigned long end, unsigned long pfn,
  495. const struct mem_type *type)
  496. {
  497. pte_t *pte = early_pte_alloc(pmd, addr, type->prot_l1);
  498. do {
  499. set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)), 0);
  500. pfn++;
  501. } while (pte++, addr += PAGE_SIZE, addr != end);
  502. }
  503. static void __init alloc_init_section(pud_t *pud, unsigned long addr,
  504. unsigned long end, phys_addr_t phys,
  505. const struct mem_type *type)
  506. {
  507. pmd_t *pmd = pmd_offset(pud, addr);
  508. /*
  509. * Try a section mapping - end, addr and phys must all be aligned
  510. * to a section boundary. Note that PMDs refer to the individual
  511. * L1 entries, whereas PGDs refer to a group of L1 entries making
  512. * up one logical pointer to an L2 table.
  513. */
  514. if (((addr | end | phys) & ~SECTION_MASK) == 0) {
  515. pmd_t *p = pmd;
  516. if (addr & SECTION_SIZE)
  517. pmd++;
  518. do {
  519. *pmd = __pmd(phys | type->prot_sect);
  520. phys += SECTION_SIZE;
  521. } while (pmd++, addr += SECTION_SIZE, addr != end);
  522. flush_pmd_entry(p);
  523. } else {
  524. /*
  525. * No need to loop; pte's aren't interested in the
  526. * individual L1 entries.
  527. */
  528. alloc_init_pte(pmd, addr, end, __phys_to_pfn(phys), type);
  529. }
  530. }
  531. static void alloc_init_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
  532. unsigned long phys, const struct mem_type *type)
  533. {
  534. pud_t *pud = pud_offset(pgd, addr);
  535. unsigned long next;
  536. do {
  537. next = pud_addr_end(addr, end);
  538. alloc_init_section(pud, addr, next, phys, type);
  539. phys += next - addr;
  540. } while (pud++, addr = next, addr != end);
  541. }
  542. static void __init create_36bit_mapping(struct map_desc *md,
  543. const struct mem_type *type)
  544. {
  545. unsigned long addr, length, end;
  546. phys_addr_t phys;
  547. pgd_t *pgd;
  548. addr = md->virtual;
  549. phys = __pfn_to_phys(md->pfn);
  550. length = PAGE_ALIGN(md->length);
  551. if (!(cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())) {
  552. printk(KERN_ERR "MM: CPU does not support supersection "
  553. "mapping for 0x%08llx at 0x%08lx\n",
  554. (long long)__pfn_to_phys((u64)md->pfn), addr);
  555. return;
  556. }
  557. /* N.B. ARMv6 supersections are only defined to work with domain 0.
  558. * Since domain assignments can in fact be arbitrary, the
  559. * 'domain == 0' check below is required to insure that ARMv6
  560. * supersections are only allocated for domain 0 regardless
  561. * of the actual domain assignments in use.
  562. */
  563. if (type->domain) {
  564. printk(KERN_ERR "MM: invalid domain in supersection "
  565. "mapping for 0x%08llx at 0x%08lx\n",
  566. (long long)__pfn_to_phys((u64)md->pfn), addr);
  567. return;
  568. }
  569. if ((addr | length | __pfn_to_phys(md->pfn)) & ~SUPERSECTION_MASK) {
  570. printk(KERN_ERR "MM: cannot create mapping for 0x%08llx"
  571. " at 0x%08lx invalid alignment\n",
  572. (long long)__pfn_to_phys((u64)md->pfn), addr);
  573. return;
  574. }
  575. /*
  576. * Shift bits [35:32] of address into bits [23:20] of PMD
  577. * (See ARMv6 spec).
  578. */
  579. phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
  580. pgd = pgd_offset_k(addr);
  581. end = addr + length;
  582. do {
  583. pud_t *pud = pud_offset(pgd, addr);
  584. pmd_t *pmd = pmd_offset(pud, addr);
  585. int i;
  586. for (i = 0; i < 16; i++)
  587. *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER);
  588. addr += SUPERSECTION_SIZE;
  589. phys += SUPERSECTION_SIZE;
  590. pgd += SUPERSECTION_SIZE >> PGDIR_SHIFT;
  591. } while (addr != end);
  592. }
  593. /*
  594. * Create the page directory entries and any necessary
  595. * page tables for the mapping specified by `md'. We
  596. * are able to cope here with varying sizes and address
  597. * offsets, and we take full advantage of sections and
  598. * supersections.
  599. */
  600. static void __init create_mapping(struct map_desc *md)
  601. {
  602. unsigned long addr, length, end;
  603. phys_addr_t phys;
  604. const struct mem_type *type;
  605. pgd_t *pgd;
  606. if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
  607. printk(KERN_WARNING "BUG: not creating mapping for 0x%08llx"
  608. " at 0x%08lx in user region\n",
  609. (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
  610. return;
  611. }
  612. if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
  613. md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
  614. printk(KERN_WARNING "BUG: mapping for 0x%08llx"
  615. " at 0x%08lx overlaps vmalloc space\n",
  616. (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
  617. }
  618. type = &mem_types[md->type];
  619. /*
  620. * Catch 36-bit addresses
  621. */
  622. if (md->pfn >= 0x100000) {
  623. create_36bit_mapping(md, type);
  624. return;
  625. }
  626. addr = md->virtual & PAGE_MASK;
  627. phys = __pfn_to_phys(md->pfn);
  628. length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
  629. if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
  630. printk(KERN_WARNING "BUG: map for 0x%08llx at 0x%08lx can not "
  631. "be mapped using pages, ignoring.\n",
  632. (long long)__pfn_to_phys(md->pfn), addr);
  633. return;
  634. }
  635. pgd = pgd_offset_k(addr);
  636. end = addr + length;
  637. do {
  638. unsigned long next = pgd_addr_end(addr, end);
  639. alloc_init_pud(pgd, addr, next, phys, type);
  640. phys += next - addr;
  641. addr = next;
  642. } while (pgd++, addr != end);
  643. }
  644. /*
  645. * Create the architecture specific mappings
  646. */
  647. void __init iotable_init(struct map_desc *io_desc, int nr)
  648. {
  649. int i;
  650. for (i = 0; i < nr; i++)
  651. create_mapping(io_desc + i);
  652. }
  653. static void * __initdata vmalloc_min = (void *)(VMALLOC_END - SZ_128M);
  654. /*
  655. * vmalloc=size forces the vmalloc area to be exactly 'size'
  656. * bytes. This can be used to increase (or decrease) the vmalloc
  657. * area - the default is 128m.
  658. */
  659. static int __init early_vmalloc(char *arg)
  660. {
  661. unsigned long vmalloc_reserve = memparse(arg, NULL);
  662. if (vmalloc_reserve < SZ_16M) {
  663. vmalloc_reserve = SZ_16M;
  664. printk(KERN_WARNING
  665. "vmalloc area too small, limiting to %luMB\n",
  666. vmalloc_reserve >> 20);
  667. }
  668. if (vmalloc_reserve > VMALLOC_END - (PAGE_OFFSET + SZ_32M)) {
  669. vmalloc_reserve = VMALLOC_END - (PAGE_OFFSET + SZ_32M);
  670. printk(KERN_WARNING
  671. "vmalloc area is too big, limiting to %luMB\n",
  672. vmalloc_reserve >> 20);
  673. }
  674. vmalloc_min = (void *)(VMALLOC_END - vmalloc_reserve);
  675. return 0;
  676. }
  677. early_param("vmalloc", early_vmalloc);
  678. static phys_addr_t lowmem_limit __initdata = 0;
  679. void __init sanity_check_meminfo(void)
  680. {
  681. int i, j, highmem = 0;
  682. for (i = 0, j = 0; i < meminfo.nr_banks; i++) {
  683. struct membank *bank = &meminfo.bank[j];
  684. *bank = meminfo.bank[i];
  685. #ifdef CONFIG_HIGHMEM
  686. if (__va(bank->start) >= vmalloc_min ||
  687. __va(bank->start) < (void *)PAGE_OFFSET)
  688. highmem = 1;
  689. bank->highmem = highmem;
  690. /*
  691. * Split those memory banks which are partially overlapping
  692. * the vmalloc area greatly simplifying things later.
  693. */
  694. if (__va(bank->start) < vmalloc_min &&
  695. bank->size > vmalloc_min - __va(bank->start)) {
  696. if (meminfo.nr_banks >= NR_BANKS) {
  697. printk(KERN_CRIT "NR_BANKS too low, "
  698. "ignoring high memory\n");
  699. } else {
  700. memmove(bank + 1, bank,
  701. (meminfo.nr_banks - i) * sizeof(*bank));
  702. meminfo.nr_banks++;
  703. i++;
  704. bank[1].size -= vmalloc_min - __va(bank->start);
  705. bank[1].start = __pa(vmalloc_min - 1) + 1;
  706. bank[1].highmem = highmem = 1;
  707. j++;
  708. }
  709. bank->size = vmalloc_min - __va(bank->start);
  710. }
  711. #else
  712. bank->highmem = highmem;
  713. /*
  714. * Check whether this memory bank would entirely overlap
  715. * the vmalloc area.
  716. */
  717. if (__va(bank->start) >= vmalloc_min ||
  718. __va(bank->start) < (void *)PAGE_OFFSET) {
  719. printk(KERN_NOTICE "Ignoring RAM at %.8llx-%.8llx "
  720. "(vmalloc region overlap).\n",
  721. (unsigned long long)bank->start,
  722. (unsigned long long)bank->start + bank->size - 1);
  723. continue;
  724. }
  725. /*
  726. * Check whether this memory bank would partially overlap
  727. * the vmalloc area.
  728. */
  729. if (__va(bank->start + bank->size) > vmalloc_min ||
  730. __va(bank->start + bank->size) < __va(bank->start)) {
  731. unsigned long newsize = vmalloc_min - __va(bank->start);
  732. printk(KERN_NOTICE "Truncating RAM at %.8llx-%.8llx "
  733. "to -%.8llx (vmalloc region overlap).\n",
  734. (unsigned long long)bank->start,
  735. (unsigned long long)bank->start + bank->size - 1,
  736. (unsigned long long)bank->start + newsize - 1);
  737. bank->size = newsize;
  738. }
  739. #endif
  740. if (!bank->highmem && bank->start + bank->size > lowmem_limit)
  741. lowmem_limit = bank->start + bank->size;
  742. j++;
  743. }
  744. #ifdef CONFIG_HIGHMEM
  745. if (highmem) {
  746. const char *reason = NULL;
  747. if (cache_is_vipt_aliasing()) {
  748. /*
  749. * Interactions between kmap and other mappings
  750. * make highmem support with aliasing VIPT caches
  751. * rather difficult.
  752. */
  753. reason = "with VIPT aliasing cache";
  754. }
  755. if (reason) {
  756. printk(KERN_CRIT "HIGHMEM is not supported %s, ignoring high memory\n",
  757. reason);
  758. while (j > 0 && meminfo.bank[j - 1].highmem)
  759. j--;
  760. }
  761. }
  762. #endif
  763. meminfo.nr_banks = j;
  764. memblock_set_current_limit(lowmem_limit);
  765. }
  766. static inline void prepare_page_table(void)
  767. {
  768. unsigned long addr;
  769. phys_addr_t end;
  770. /*
  771. * Clear out all the mappings below the kernel image.
  772. */
  773. for (addr = 0; addr < MODULES_VADDR; addr += PGDIR_SIZE)
  774. pmd_clear(pmd_off_k(addr));
  775. #ifdef CONFIG_XIP_KERNEL
  776. /* The XIP kernel is mapped in the module area -- skip over it */
  777. addr = ((unsigned long)_etext + PGDIR_SIZE - 1) & PGDIR_MASK;
  778. #endif
  779. for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
  780. pmd_clear(pmd_off_k(addr));
  781. /*
  782. * Find the end of the first block of lowmem.
  783. */
  784. end = memblock.memory.regions[0].base + memblock.memory.regions[0].size;
  785. if (end >= lowmem_limit)
  786. end = lowmem_limit;
  787. /*
  788. * Clear out all the kernel space mappings, except for the first
  789. * memory bank, up to the end of the vmalloc region.
  790. */
  791. for (addr = __phys_to_virt(end);
  792. addr < VMALLOC_END; addr += PGDIR_SIZE)
  793. pmd_clear(pmd_off_k(addr));
  794. }
  795. /*
  796. * Reserve the special regions of memory
  797. */
  798. void __init arm_mm_memblock_reserve(void)
  799. {
  800. /*
  801. * Reserve the page tables. These are already in use,
  802. * and can only be in node 0.
  803. */
  804. memblock_reserve(__pa(swapper_pg_dir), PTRS_PER_PGD * sizeof(pgd_t));
  805. #ifdef CONFIG_SA1111
  806. /*
  807. * Because of the SA1111 DMA bug, we want to preserve our
  808. * precious DMA-able memory...
  809. */
  810. memblock_reserve(PHYS_OFFSET, __pa(swapper_pg_dir) - PHYS_OFFSET);
  811. #endif
  812. }
  813. /*
  814. * Set up device the mappings. Since we clear out the page tables for all
  815. * mappings above VMALLOC_END, we will remove any debug device mappings.
  816. * This means you have to be careful how you debug this function, or any
  817. * called function. This means you can't use any function or debugging
  818. * method which may touch any device, otherwise the kernel _will_ crash.
  819. */
  820. static void __init devicemaps_init(struct machine_desc *mdesc)
  821. {
  822. struct map_desc map;
  823. unsigned long addr;
  824. /*
  825. * Allocate the vector page early.
  826. */
  827. vectors_page = early_alloc(PAGE_SIZE);
  828. for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
  829. pmd_clear(pmd_off_k(addr));
  830. /*
  831. * Map the kernel if it is XIP.
  832. * It is always first in the modulearea.
  833. */
  834. #ifdef CONFIG_XIP_KERNEL
  835. map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
  836. map.virtual = MODULES_VADDR;
  837. map.length = ((unsigned long)_etext - map.virtual + ~SECTION_MASK) & SECTION_MASK;
  838. map.type = MT_ROM;
  839. create_mapping(&map);
  840. #endif
  841. /*
  842. * Map the cache flushing regions.
  843. */
  844. #ifdef FLUSH_BASE
  845. map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
  846. map.virtual = FLUSH_BASE;
  847. map.length = SZ_1M;
  848. map.type = MT_CACHECLEAN;
  849. create_mapping(&map);
  850. #endif
  851. #ifdef FLUSH_BASE_MINICACHE
  852. map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
  853. map.virtual = FLUSH_BASE_MINICACHE;
  854. map.length = SZ_1M;
  855. map.type = MT_MINICLEAN;
  856. create_mapping(&map);
  857. #endif
  858. /*
  859. * Create a mapping for the machine vectors at the high-vectors
  860. * location (0xffff0000). If we aren't using high-vectors, also
  861. * create a mapping at the low-vectors virtual address.
  862. */
  863. map.pfn = __phys_to_pfn(virt_to_phys(vectors_page));
  864. map.virtual = 0xffff0000;
  865. map.length = PAGE_SIZE;
  866. map.type = MT_HIGH_VECTORS;
  867. create_mapping(&map);
  868. if (!vectors_high()) {
  869. map.virtual = 0;
  870. map.type = MT_LOW_VECTORS;
  871. create_mapping(&map);
  872. }
  873. /*
  874. * Ask the machine support to map in the statically mapped devices.
  875. */
  876. if (mdesc->map_io)
  877. mdesc->map_io();
  878. /*
  879. * Finally flush the caches and tlb to ensure that we're in a
  880. * consistent state wrt the writebuffer. This also ensures that
  881. * any write-allocated cache lines in the vector page are written
  882. * back. After this point, we can start to touch devices again.
  883. */
  884. local_flush_tlb_all();
  885. flush_cache_all();
  886. }
  887. static void __init kmap_init(void)
  888. {
  889. #ifdef CONFIG_HIGHMEM
  890. pkmap_page_table = early_pte_alloc(pmd_off_k(PKMAP_BASE),
  891. PKMAP_BASE, _PAGE_KERNEL_TABLE);
  892. #endif
  893. }
  894. static void __init map_lowmem(void)
  895. {
  896. struct memblock_region *reg;
  897. /* Map all the lowmem memory banks. */
  898. for_each_memblock(memory, reg) {
  899. phys_addr_t start = reg->base;
  900. phys_addr_t end = start + reg->size;
  901. struct map_desc map;
  902. if (end > lowmem_limit)
  903. end = lowmem_limit;
  904. if (start >= end)
  905. break;
  906. map.pfn = __phys_to_pfn(start);
  907. map.virtual = __phys_to_virt(start);
  908. map.length = end - start;
  909. map.type = MT_MEMORY;
  910. create_mapping(&map);
  911. }
  912. }
  913. /*
  914. * paging_init() sets up the page tables, initialises the zone memory
  915. * maps, and sets up the zero page, bad page and bad page tables.
  916. */
  917. void __init paging_init(struct machine_desc *mdesc)
  918. {
  919. void *zero_page;
  920. memblock_set_current_limit(lowmem_limit);
  921. build_mem_type_table();
  922. prepare_page_table();
  923. map_lowmem();
  924. devicemaps_init(mdesc);
  925. kmap_init();
  926. top_pmd = pmd_off_k(0xffff0000);
  927. /* allocate the zero page. */
  928. zero_page = early_alloc(PAGE_SIZE);
  929. bootmem_init();
  930. empty_zero_page = virt_to_page(zero_page);
  931. __flush_dcache_page(NULL, empty_zero_page);
  932. }