mmu.c 40 KB

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