mmu.c 43 KB

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