mmu.c 33 KB

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