mmu.c 34 KB

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