mmu.c 44 KB

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