hash_utils_64.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. /*
  2. * PowerPC64 port by Mike Corrigan and Dave Engebretsen
  3. * {mikejc|engebret}@us.ibm.com
  4. *
  5. * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com>
  6. *
  7. * SMP scalability work:
  8. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  9. *
  10. * Module name: htab.c
  11. *
  12. * Description:
  13. * PowerPC Hashed Page Table functions
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #undef DEBUG
  21. #undef DEBUG_LOW
  22. #include <linux/spinlock.h>
  23. #include <linux/errno.h>
  24. #include <linux/sched.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/stat.h>
  27. #include <linux/sysctl.h>
  28. #include <linux/export.h>
  29. #include <linux/ctype.h>
  30. #include <linux/cache.h>
  31. #include <linux/init.h>
  32. #include <linux/signal.h>
  33. #include <linux/memblock.h>
  34. #include <linux/context_tracking.h>
  35. #include <asm/processor.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/mmu.h>
  38. #include <asm/mmu_context.h>
  39. #include <asm/page.h>
  40. #include <asm/types.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/machdep.h>
  43. #include <asm/prom.h>
  44. #include <asm/tlbflush.h>
  45. #include <asm/io.h>
  46. #include <asm/eeh.h>
  47. #include <asm/tlb.h>
  48. #include <asm/cacheflush.h>
  49. #include <asm/cputable.h>
  50. #include <asm/sections.h>
  51. #include <asm/copro.h>
  52. #include <asm/udbg.h>
  53. #include <asm/code-patching.h>
  54. #include <asm/fadump.h>
  55. #include <asm/firmware.h>
  56. #include <asm/tm.h>
  57. #include <asm/trace.h>
  58. #ifdef DEBUG
  59. #define DBG(fmt...) udbg_printf(fmt)
  60. #else
  61. #define DBG(fmt...)
  62. #endif
  63. #ifdef DEBUG_LOW
  64. #define DBG_LOW(fmt...) udbg_printf(fmt)
  65. #else
  66. #define DBG_LOW(fmt...)
  67. #endif
  68. #define KB (1024)
  69. #define MB (1024*KB)
  70. #define GB (1024L*MB)
  71. /*
  72. * Note: pte --> Linux PTE
  73. * HPTE --> PowerPC Hashed Page Table Entry
  74. *
  75. * Execution context:
  76. * htab_initialize is called with the MMU off (of course), but
  77. * the kernel has been copied down to zero so it can directly
  78. * reference global data. At this point it is very difficult
  79. * to print debug info.
  80. *
  81. */
  82. #ifdef CONFIG_U3_DART
  83. extern unsigned long dart_tablebase;
  84. #endif /* CONFIG_U3_DART */
  85. static unsigned long _SDR1;
  86. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
  87. EXPORT_SYMBOL_GPL(mmu_psize_defs);
  88. struct hash_pte *htab_address;
  89. unsigned long htab_size_bytes;
  90. unsigned long htab_hash_mask;
  91. EXPORT_SYMBOL_GPL(htab_hash_mask);
  92. int mmu_linear_psize = MMU_PAGE_4K;
  93. EXPORT_SYMBOL_GPL(mmu_linear_psize);
  94. int mmu_virtual_psize = MMU_PAGE_4K;
  95. int mmu_vmalloc_psize = MMU_PAGE_4K;
  96. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  97. int mmu_vmemmap_psize = MMU_PAGE_4K;
  98. #endif
  99. int mmu_io_psize = MMU_PAGE_4K;
  100. int mmu_kernel_ssize = MMU_SEGSIZE_256M;
  101. EXPORT_SYMBOL_GPL(mmu_kernel_ssize);
  102. int mmu_highuser_ssize = MMU_SEGSIZE_256M;
  103. u16 mmu_slb_size = 64;
  104. EXPORT_SYMBOL_GPL(mmu_slb_size);
  105. #ifdef CONFIG_PPC_64K_PAGES
  106. int mmu_ci_restrictions;
  107. #endif
  108. #ifdef CONFIG_DEBUG_PAGEALLOC
  109. static u8 *linear_map_hash_slots;
  110. static unsigned long linear_map_hash_count;
  111. static DEFINE_SPINLOCK(linear_map_hash_lock);
  112. #endif /* CONFIG_DEBUG_PAGEALLOC */
  113. /* There are definitions of page sizes arrays to be used when none
  114. * is provided by the firmware.
  115. */
  116. /* Pre-POWER4 CPUs (4k pages only)
  117. */
  118. static struct mmu_psize_def mmu_psize_defaults_old[] = {
  119. [MMU_PAGE_4K] = {
  120. .shift = 12,
  121. .sllp = 0,
  122. .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
  123. .avpnm = 0,
  124. .tlbiel = 0,
  125. },
  126. };
  127. /* POWER4, GPUL, POWER5
  128. *
  129. * Support for 16Mb large pages
  130. */
  131. static struct mmu_psize_def mmu_psize_defaults_gp[] = {
  132. [MMU_PAGE_4K] = {
  133. .shift = 12,
  134. .sllp = 0,
  135. .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
  136. .avpnm = 0,
  137. .tlbiel = 1,
  138. },
  139. [MMU_PAGE_16M] = {
  140. .shift = 24,
  141. .sllp = SLB_VSID_L,
  142. .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
  143. [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
  144. .avpnm = 0x1UL,
  145. .tlbiel = 0,
  146. },
  147. };
  148. static unsigned long htab_convert_pte_flags(unsigned long pteflags)
  149. {
  150. unsigned long rflags = pteflags & 0x1fa;
  151. /* _PAGE_EXEC -> NOEXEC */
  152. if ((pteflags & _PAGE_EXEC) == 0)
  153. rflags |= HPTE_R_N;
  154. /* PP bits. PAGE_USER is already PP bit 0x2, so we only
  155. * need to add in 0x1 if it's a read-only user page
  156. */
  157. if ((pteflags & _PAGE_USER) && !((pteflags & _PAGE_RW) &&
  158. (pteflags & _PAGE_DIRTY)))
  159. rflags |= 1;
  160. /*
  161. * Always add "C" bit for perf. Memory coherence is always enabled
  162. */
  163. return rflags | HPTE_R_C | HPTE_R_M;
  164. }
  165. int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
  166. unsigned long pstart, unsigned long prot,
  167. int psize, int ssize)
  168. {
  169. unsigned long vaddr, paddr;
  170. unsigned int step, shift;
  171. int ret = 0;
  172. shift = mmu_psize_defs[psize].shift;
  173. step = 1 << shift;
  174. prot = htab_convert_pte_flags(prot);
  175. DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
  176. vstart, vend, pstart, prot, psize, ssize);
  177. for (vaddr = vstart, paddr = pstart; vaddr < vend;
  178. vaddr += step, paddr += step) {
  179. unsigned long hash, hpteg;
  180. unsigned long vsid = get_kernel_vsid(vaddr, ssize);
  181. unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
  182. unsigned long tprot = prot;
  183. /*
  184. * If we hit a bad address return error.
  185. */
  186. if (!vsid)
  187. return -1;
  188. /* Make kernel text executable */
  189. if (overlaps_kernel_text(vaddr, vaddr + step))
  190. tprot &= ~HPTE_R_N;
  191. /* Make kvm guest trampolines executable */
  192. if (overlaps_kvm_tmp(vaddr, vaddr + step))
  193. tprot &= ~HPTE_R_N;
  194. /*
  195. * If relocatable, check if it overlaps interrupt vectors that
  196. * are copied down to real 0. For relocatable kernel
  197. * (e.g. kdump case) we copy interrupt vectors down to real
  198. * address 0. Mark that region as executable. This is
  199. * because on p8 system with relocation on exception feature
  200. * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
  201. * in order to execute the interrupt handlers in virtual
  202. * mode the vector region need to be marked as executable.
  203. */
  204. if ((PHYSICAL_START > MEMORY_START) &&
  205. overlaps_interrupt_vector_text(vaddr, vaddr + step))
  206. tprot &= ~HPTE_R_N;
  207. hash = hpt_hash(vpn, shift, ssize);
  208. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  209. BUG_ON(!ppc_md.hpte_insert);
  210. ret = ppc_md.hpte_insert(hpteg, vpn, paddr, tprot,
  211. HPTE_V_BOLTED, psize, psize, ssize);
  212. if (ret < 0)
  213. break;
  214. #ifdef CONFIG_DEBUG_PAGEALLOC
  215. if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
  216. linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
  217. #endif /* CONFIG_DEBUG_PAGEALLOC */
  218. }
  219. return ret < 0 ? ret : 0;
  220. }
  221. #ifdef CONFIG_MEMORY_HOTPLUG
  222. int htab_remove_mapping(unsigned long vstart, unsigned long vend,
  223. int psize, int ssize)
  224. {
  225. unsigned long vaddr;
  226. unsigned int step, shift;
  227. shift = mmu_psize_defs[psize].shift;
  228. step = 1 << shift;
  229. if (!ppc_md.hpte_removebolted) {
  230. printk(KERN_WARNING "Platform doesn't implement "
  231. "hpte_removebolted\n");
  232. return -EINVAL;
  233. }
  234. for (vaddr = vstart; vaddr < vend; vaddr += step)
  235. ppc_md.hpte_removebolted(vaddr, psize, ssize);
  236. return 0;
  237. }
  238. #endif /* CONFIG_MEMORY_HOTPLUG */
  239. static int __init htab_dt_scan_seg_sizes(unsigned long node,
  240. const char *uname, int depth,
  241. void *data)
  242. {
  243. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  244. const __be32 *prop;
  245. int size = 0;
  246. /* We are scanning "cpu" nodes only */
  247. if (type == NULL || strcmp(type, "cpu") != 0)
  248. return 0;
  249. prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
  250. if (prop == NULL)
  251. return 0;
  252. for (; size >= 4; size -= 4, ++prop) {
  253. if (be32_to_cpu(prop[0]) == 40) {
  254. DBG("1T segment support detected\n");
  255. cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
  256. return 1;
  257. }
  258. }
  259. cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
  260. return 0;
  261. }
  262. static void __init htab_init_seg_sizes(void)
  263. {
  264. of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
  265. }
  266. static int __init get_idx_from_shift(unsigned int shift)
  267. {
  268. int idx = -1;
  269. switch (shift) {
  270. case 0xc:
  271. idx = MMU_PAGE_4K;
  272. break;
  273. case 0x10:
  274. idx = MMU_PAGE_64K;
  275. break;
  276. case 0x14:
  277. idx = MMU_PAGE_1M;
  278. break;
  279. case 0x18:
  280. idx = MMU_PAGE_16M;
  281. break;
  282. case 0x22:
  283. idx = MMU_PAGE_16G;
  284. break;
  285. }
  286. return idx;
  287. }
  288. static int __init htab_dt_scan_page_sizes(unsigned long node,
  289. const char *uname, int depth,
  290. void *data)
  291. {
  292. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  293. const __be32 *prop;
  294. int size = 0;
  295. /* We are scanning "cpu" nodes only */
  296. if (type == NULL || strcmp(type, "cpu") != 0)
  297. return 0;
  298. prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
  299. if (!prop)
  300. return 0;
  301. pr_info("Page sizes from device-tree:\n");
  302. size /= 4;
  303. cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
  304. while(size > 0) {
  305. unsigned int base_shift = be32_to_cpu(prop[0]);
  306. unsigned int slbenc = be32_to_cpu(prop[1]);
  307. unsigned int lpnum = be32_to_cpu(prop[2]);
  308. struct mmu_psize_def *def;
  309. int idx, base_idx;
  310. size -= 3; prop += 3;
  311. base_idx = get_idx_from_shift(base_shift);
  312. if (base_idx < 0) {
  313. /* skip the pte encoding also */
  314. prop += lpnum * 2; size -= lpnum * 2;
  315. continue;
  316. }
  317. def = &mmu_psize_defs[base_idx];
  318. if (base_idx == MMU_PAGE_16M)
  319. cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
  320. def->shift = base_shift;
  321. if (base_shift <= 23)
  322. def->avpnm = 0;
  323. else
  324. def->avpnm = (1 << (base_shift - 23)) - 1;
  325. def->sllp = slbenc;
  326. /*
  327. * We don't know for sure what's up with tlbiel, so
  328. * for now we only set it for 4K and 64K pages
  329. */
  330. if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
  331. def->tlbiel = 1;
  332. else
  333. def->tlbiel = 0;
  334. while (size > 0 && lpnum) {
  335. unsigned int shift = be32_to_cpu(prop[0]);
  336. int penc = be32_to_cpu(prop[1]);
  337. prop += 2; size -= 2;
  338. lpnum--;
  339. idx = get_idx_from_shift(shift);
  340. if (idx < 0)
  341. continue;
  342. if (penc == -1)
  343. pr_err("Invalid penc for base_shift=%d "
  344. "shift=%d\n", base_shift, shift);
  345. def->penc[idx] = penc;
  346. pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
  347. " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
  348. base_shift, shift, def->sllp,
  349. def->avpnm, def->tlbiel, def->penc[idx]);
  350. }
  351. }
  352. return 1;
  353. }
  354. #ifdef CONFIG_HUGETLB_PAGE
  355. /* Scan for 16G memory blocks that have been set aside for huge pages
  356. * and reserve those blocks for 16G huge pages.
  357. */
  358. static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
  359. const char *uname, int depth,
  360. void *data) {
  361. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  362. const __be64 *addr_prop;
  363. const __be32 *page_count_prop;
  364. unsigned int expected_pages;
  365. long unsigned int phys_addr;
  366. long unsigned int block_size;
  367. /* We are scanning "memory" nodes only */
  368. if (type == NULL || strcmp(type, "memory") != 0)
  369. return 0;
  370. /* This property is the log base 2 of the number of virtual pages that
  371. * will represent this memory block. */
  372. page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
  373. if (page_count_prop == NULL)
  374. return 0;
  375. expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
  376. addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
  377. if (addr_prop == NULL)
  378. return 0;
  379. phys_addr = be64_to_cpu(addr_prop[0]);
  380. block_size = be64_to_cpu(addr_prop[1]);
  381. if (block_size != (16 * GB))
  382. return 0;
  383. printk(KERN_INFO "Huge page(16GB) memory: "
  384. "addr = 0x%lX size = 0x%lX pages = %d\n",
  385. phys_addr, block_size, expected_pages);
  386. if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
  387. memblock_reserve(phys_addr, block_size * expected_pages);
  388. add_gpage(phys_addr, block_size, expected_pages);
  389. }
  390. return 0;
  391. }
  392. #endif /* CONFIG_HUGETLB_PAGE */
  393. static void mmu_psize_set_default_penc(void)
  394. {
  395. int bpsize, apsize;
  396. for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
  397. for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
  398. mmu_psize_defs[bpsize].penc[apsize] = -1;
  399. }
  400. #ifdef CONFIG_PPC_64K_PAGES
  401. static bool might_have_hea(void)
  402. {
  403. /*
  404. * The HEA ethernet adapter requires awareness of the
  405. * GX bus. Without that awareness we can easily assume
  406. * we will never see an HEA ethernet device.
  407. */
  408. #ifdef CONFIG_IBMEBUS
  409. return !cpu_has_feature(CPU_FTR_ARCH_207S);
  410. #else
  411. return false;
  412. #endif
  413. }
  414. #endif /* #ifdef CONFIG_PPC_64K_PAGES */
  415. static void __init htab_init_page_sizes(void)
  416. {
  417. int rc;
  418. /* se the invalid penc to -1 */
  419. mmu_psize_set_default_penc();
  420. /* Default to 4K pages only */
  421. memcpy(mmu_psize_defs, mmu_psize_defaults_old,
  422. sizeof(mmu_psize_defaults_old));
  423. /*
  424. * Try to find the available page sizes in the device-tree
  425. */
  426. rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
  427. if (rc != 0) /* Found */
  428. goto found;
  429. /*
  430. * Not in the device-tree, let's fallback on known size
  431. * list for 16M capable GP & GR
  432. */
  433. if (mmu_has_feature(MMU_FTR_16M_PAGE))
  434. memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
  435. sizeof(mmu_psize_defaults_gp));
  436. found:
  437. #ifndef CONFIG_DEBUG_PAGEALLOC
  438. /*
  439. * Pick a size for the linear mapping. Currently, we only support
  440. * 16M, 1M and 4K which is the default
  441. */
  442. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  443. mmu_linear_psize = MMU_PAGE_16M;
  444. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  445. mmu_linear_psize = MMU_PAGE_1M;
  446. #endif /* CONFIG_DEBUG_PAGEALLOC */
  447. #ifdef CONFIG_PPC_64K_PAGES
  448. /*
  449. * Pick a size for the ordinary pages. Default is 4K, we support
  450. * 64K for user mappings and vmalloc if supported by the processor.
  451. * We only use 64k for ioremap if the processor
  452. * (and firmware) support cache-inhibited large pages.
  453. * If not, we use 4k and set mmu_ci_restrictions so that
  454. * hash_page knows to switch processes that use cache-inhibited
  455. * mappings to 4k pages.
  456. */
  457. if (mmu_psize_defs[MMU_PAGE_64K].shift) {
  458. mmu_virtual_psize = MMU_PAGE_64K;
  459. mmu_vmalloc_psize = MMU_PAGE_64K;
  460. if (mmu_linear_psize == MMU_PAGE_4K)
  461. mmu_linear_psize = MMU_PAGE_64K;
  462. if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
  463. /*
  464. * When running on pSeries using 64k pages for ioremap
  465. * would stop us accessing the HEA ethernet. So if we
  466. * have the chance of ever seeing one, stay at 4k.
  467. */
  468. if (!might_have_hea() || !machine_is(pseries))
  469. mmu_io_psize = MMU_PAGE_64K;
  470. } else
  471. mmu_ci_restrictions = 1;
  472. }
  473. #endif /* CONFIG_PPC_64K_PAGES */
  474. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  475. /* We try to use 16M pages for vmemmap if that is supported
  476. * and we have at least 1G of RAM at boot
  477. */
  478. if (mmu_psize_defs[MMU_PAGE_16M].shift &&
  479. memblock_phys_mem_size() >= 0x40000000)
  480. mmu_vmemmap_psize = MMU_PAGE_16M;
  481. else if (mmu_psize_defs[MMU_PAGE_64K].shift)
  482. mmu_vmemmap_psize = MMU_PAGE_64K;
  483. else
  484. mmu_vmemmap_psize = MMU_PAGE_4K;
  485. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  486. printk(KERN_DEBUG "Page orders: linear mapping = %d, "
  487. "virtual = %d, io = %d"
  488. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  489. ", vmemmap = %d"
  490. #endif
  491. "\n",
  492. mmu_psize_defs[mmu_linear_psize].shift,
  493. mmu_psize_defs[mmu_virtual_psize].shift,
  494. mmu_psize_defs[mmu_io_psize].shift
  495. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  496. ,mmu_psize_defs[mmu_vmemmap_psize].shift
  497. #endif
  498. );
  499. #ifdef CONFIG_HUGETLB_PAGE
  500. /* Reserve 16G huge page memory sections for huge pages */
  501. of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
  502. #endif /* CONFIG_HUGETLB_PAGE */
  503. }
  504. static int __init htab_dt_scan_pftsize(unsigned long node,
  505. const char *uname, int depth,
  506. void *data)
  507. {
  508. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  509. const __be32 *prop;
  510. /* We are scanning "cpu" nodes only */
  511. if (type == NULL || strcmp(type, "cpu") != 0)
  512. return 0;
  513. prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
  514. if (prop != NULL) {
  515. /* pft_size[0] is the NUMA CEC cookie */
  516. ppc64_pft_size = be32_to_cpu(prop[1]);
  517. return 1;
  518. }
  519. return 0;
  520. }
  521. static unsigned long __init htab_get_table_size(void)
  522. {
  523. unsigned long mem_size, rnd_mem_size, pteg_count, psize;
  524. /* If hash size isn't already provided by the platform, we try to
  525. * retrieve it from the device-tree. If it's not there neither, we
  526. * calculate it now based on the total RAM size
  527. */
  528. if (ppc64_pft_size == 0)
  529. of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
  530. if (ppc64_pft_size)
  531. return 1UL << ppc64_pft_size;
  532. /* round mem_size up to next power of 2 */
  533. mem_size = memblock_phys_mem_size();
  534. rnd_mem_size = 1UL << __ilog2(mem_size);
  535. if (rnd_mem_size < mem_size)
  536. rnd_mem_size <<= 1;
  537. /* # pages / 2 */
  538. psize = mmu_psize_defs[mmu_virtual_psize].shift;
  539. pteg_count = max(rnd_mem_size >> (psize + 1), 1UL << 11);
  540. return pteg_count << 7;
  541. }
  542. #ifdef CONFIG_MEMORY_HOTPLUG
  543. int create_section_mapping(unsigned long start, unsigned long end)
  544. {
  545. return htab_bolt_mapping(start, end, __pa(start),
  546. pgprot_val(PAGE_KERNEL), mmu_linear_psize,
  547. mmu_kernel_ssize);
  548. }
  549. int remove_section_mapping(unsigned long start, unsigned long end)
  550. {
  551. return htab_remove_mapping(start, end, mmu_linear_psize,
  552. mmu_kernel_ssize);
  553. }
  554. #endif /* CONFIG_MEMORY_HOTPLUG */
  555. extern u32 htab_call_hpte_insert1[];
  556. extern u32 htab_call_hpte_insert2[];
  557. extern u32 htab_call_hpte_remove[];
  558. extern u32 htab_call_hpte_updatepp[];
  559. extern u32 ht64_call_hpte_insert1[];
  560. extern u32 ht64_call_hpte_insert2[];
  561. extern u32 ht64_call_hpte_remove[];
  562. extern u32 ht64_call_hpte_updatepp[];
  563. static void __init htab_finish_init(void)
  564. {
  565. #ifdef CONFIG_PPC_64K_PAGES
  566. patch_branch(ht64_call_hpte_insert1,
  567. ppc_function_entry(ppc_md.hpte_insert),
  568. BRANCH_SET_LINK);
  569. patch_branch(ht64_call_hpte_insert2,
  570. ppc_function_entry(ppc_md.hpte_insert),
  571. BRANCH_SET_LINK);
  572. patch_branch(ht64_call_hpte_remove,
  573. ppc_function_entry(ppc_md.hpte_remove),
  574. BRANCH_SET_LINK);
  575. patch_branch(ht64_call_hpte_updatepp,
  576. ppc_function_entry(ppc_md.hpte_updatepp),
  577. BRANCH_SET_LINK);
  578. #endif /* CONFIG_PPC_64K_PAGES */
  579. patch_branch(htab_call_hpte_insert1,
  580. ppc_function_entry(ppc_md.hpte_insert),
  581. BRANCH_SET_LINK);
  582. patch_branch(htab_call_hpte_insert2,
  583. ppc_function_entry(ppc_md.hpte_insert),
  584. BRANCH_SET_LINK);
  585. patch_branch(htab_call_hpte_remove,
  586. ppc_function_entry(ppc_md.hpte_remove),
  587. BRANCH_SET_LINK);
  588. patch_branch(htab_call_hpte_updatepp,
  589. ppc_function_entry(ppc_md.hpte_updatepp),
  590. BRANCH_SET_LINK);
  591. }
  592. static void __init htab_initialize(void)
  593. {
  594. unsigned long table;
  595. unsigned long pteg_count;
  596. unsigned long prot;
  597. unsigned long base = 0, size = 0, limit;
  598. struct memblock_region *reg;
  599. DBG(" -> htab_initialize()\n");
  600. /* Initialize segment sizes */
  601. htab_init_seg_sizes();
  602. /* Initialize page sizes */
  603. htab_init_page_sizes();
  604. if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
  605. mmu_kernel_ssize = MMU_SEGSIZE_1T;
  606. mmu_highuser_ssize = MMU_SEGSIZE_1T;
  607. printk(KERN_INFO "Using 1TB segments\n");
  608. }
  609. /*
  610. * Calculate the required size of the htab. We want the number of
  611. * PTEGs to equal one half the number of real pages.
  612. */
  613. htab_size_bytes = htab_get_table_size();
  614. pteg_count = htab_size_bytes >> 7;
  615. htab_hash_mask = pteg_count - 1;
  616. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  617. /* Using a hypervisor which owns the htab */
  618. htab_address = NULL;
  619. _SDR1 = 0;
  620. #ifdef CONFIG_FA_DUMP
  621. /*
  622. * If firmware assisted dump is active firmware preserves
  623. * the contents of htab along with entire partition memory.
  624. * Clear the htab if firmware assisted dump is active so
  625. * that we dont end up using old mappings.
  626. */
  627. if (is_fadump_active() && ppc_md.hpte_clear_all)
  628. ppc_md.hpte_clear_all();
  629. #endif
  630. } else {
  631. /* Find storage for the HPT. Must be contiguous in
  632. * the absolute address space. On cell we want it to be
  633. * in the first 2 Gig so we can use it for IOMMU hacks.
  634. */
  635. if (machine_is(cell))
  636. limit = 0x80000000;
  637. else
  638. limit = MEMBLOCK_ALLOC_ANYWHERE;
  639. table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
  640. DBG("Hash table allocated at %lx, size: %lx\n", table,
  641. htab_size_bytes);
  642. htab_address = __va(table);
  643. /* htab absolute addr + encoded htabsize */
  644. _SDR1 = table + __ilog2(pteg_count) - 11;
  645. /* Initialize the HPT with no entries */
  646. memset((void *)table, 0, htab_size_bytes);
  647. /* Set SDR1 */
  648. mtspr(SPRN_SDR1, _SDR1);
  649. }
  650. prot = pgprot_val(PAGE_KERNEL);
  651. #ifdef CONFIG_DEBUG_PAGEALLOC
  652. linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
  653. linear_map_hash_slots = __va(memblock_alloc_base(linear_map_hash_count,
  654. 1, ppc64_rma_size));
  655. memset(linear_map_hash_slots, 0, linear_map_hash_count);
  656. #endif /* CONFIG_DEBUG_PAGEALLOC */
  657. /* On U3 based machines, we need to reserve the DART area and
  658. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  659. * cacheable later on
  660. */
  661. /* create bolted the linear mapping in the hash table */
  662. for_each_memblock(memory, reg) {
  663. base = (unsigned long)__va(reg->base);
  664. size = reg->size;
  665. DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
  666. base, size, prot);
  667. #ifdef CONFIG_U3_DART
  668. /* Do not map the DART space. Fortunately, it will be aligned
  669. * in such a way that it will not cross two memblock regions and
  670. * will fit within a single 16Mb page.
  671. * The DART space is assumed to be a full 16Mb region even if
  672. * we only use 2Mb of that space. We will use more of it later
  673. * for AGP GART. We have to use a full 16Mb large page.
  674. */
  675. DBG("DART base: %lx\n", dart_tablebase);
  676. if (dart_tablebase != 0 && dart_tablebase >= base
  677. && dart_tablebase < (base + size)) {
  678. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  679. if (base != dart_tablebase)
  680. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  681. __pa(base), prot,
  682. mmu_linear_psize,
  683. mmu_kernel_ssize));
  684. if ((base + size) > dart_table_end)
  685. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  686. base + size,
  687. __pa(dart_table_end),
  688. prot,
  689. mmu_linear_psize,
  690. mmu_kernel_ssize));
  691. continue;
  692. }
  693. #endif /* CONFIG_U3_DART */
  694. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  695. prot, mmu_linear_psize, mmu_kernel_ssize));
  696. }
  697. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  698. /*
  699. * If we have a memory_limit and we've allocated TCEs then we need to
  700. * explicitly map the TCE area at the top of RAM. We also cope with the
  701. * case that the TCEs start below memory_limit.
  702. * tce_alloc_start/end are 16MB aligned so the mapping should work
  703. * for either 4K or 16MB pages.
  704. */
  705. if (tce_alloc_start) {
  706. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  707. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  708. if (base + size >= tce_alloc_start)
  709. tce_alloc_start = base + size + 1;
  710. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  711. __pa(tce_alloc_start), prot,
  712. mmu_linear_psize, mmu_kernel_ssize));
  713. }
  714. htab_finish_init();
  715. DBG(" <- htab_initialize()\n");
  716. }
  717. #undef KB
  718. #undef MB
  719. void __init early_init_mmu(void)
  720. {
  721. /* Initialize the MMU Hash table and create the linear mapping
  722. * of memory. Has to be done before SLB initialization as this is
  723. * currently where the page size encoding is obtained.
  724. */
  725. htab_initialize();
  726. /* Initialize SLB management */
  727. slb_initialize();
  728. }
  729. #ifdef CONFIG_SMP
  730. void early_init_mmu_secondary(void)
  731. {
  732. /* Initialize hash table for that CPU */
  733. if (!firmware_has_feature(FW_FEATURE_LPAR))
  734. mtspr(SPRN_SDR1, _SDR1);
  735. /* Initialize SLB */
  736. slb_initialize();
  737. }
  738. #endif /* CONFIG_SMP */
  739. /*
  740. * Called by asm hashtable.S for doing lazy icache flush
  741. */
  742. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  743. {
  744. struct page *page;
  745. if (!pfn_valid(pte_pfn(pte)))
  746. return pp;
  747. page = pte_page(pte);
  748. /* page is dirty */
  749. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  750. if (trap == 0x400) {
  751. flush_dcache_icache_page(page);
  752. set_bit(PG_arch_1, &page->flags);
  753. } else
  754. pp |= HPTE_R_N;
  755. }
  756. return pp;
  757. }
  758. #ifdef CONFIG_PPC_MM_SLICES
  759. static unsigned int get_paca_psize(unsigned long addr)
  760. {
  761. u64 lpsizes;
  762. unsigned char *hpsizes;
  763. unsigned long index, mask_index;
  764. if (addr < SLICE_LOW_TOP) {
  765. lpsizes = get_paca()->context.low_slices_psize;
  766. index = GET_LOW_SLICE_INDEX(addr);
  767. return (lpsizes >> (index * 4)) & 0xF;
  768. }
  769. hpsizes = get_paca()->context.high_slices_psize;
  770. index = GET_HIGH_SLICE_INDEX(addr);
  771. mask_index = index & 0x1;
  772. return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xF;
  773. }
  774. #else
  775. unsigned int get_paca_psize(unsigned long addr)
  776. {
  777. return get_paca()->context.user_psize;
  778. }
  779. #endif
  780. /*
  781. * Demote a segment to using 4k pages.
  782. * For now this makes the whole process use 4k pages.
  783. */
  784. #ifdef CONFIG_PPC_64K_PAGES
  785. void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  786. {
  787. if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
  788. return;
  789. slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
  790. copro_flush_all_slbs(mm);
  791. if ((get_paca_psize(addr) != MMU_PAGE_4K) && (current->mm == mm)) {
  792. get_paca()->context = mm->context;
  793. slb_flush_and_rebolt();
  794. }
  795. }
  796. #endif /* CONFIG_PPC_64K_PAGES */
  797. #ifdef CONFIG_PPC_SUBPAGE_PROT
  798. /*
  799. * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
  800. * Userspace sets the subpage permissions using the subpage_prot system call.
  801. *
  802. * Result is 0: full permissions, _PAGE_RW: read-only,
  803. * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
  804. */
  805. static int subpage_protection(struct mm_struct *mm, unsigned long ea)
  806. {
  807. struct subpage_prot_table *spt = &mm->context.spt;
  808. u32 spp = 0;
  809. u32 **sbpm, *sbpp;
  810. if (ea >= spt->maxaddr)
  811. return 0;
  812. if (ea < 0x100000000UL) {
  813. /* addresses below 4GB use spt->low_prot */
  814. sbpm = spt->low_prot;
  815. } else {
  816. sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
  817. if (!sbpm)
  818. return 0;
  819. }
  820. sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  821. if (!sbpp)
  822. return 0;
  823. spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
  824. /* extract 2-bit bitfield for this 4k subpage */
  825. spp >>= 30 - 2 * ((ea >> 12) & 0xf);
  826. /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
  827. spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
  828. return spp;
  829. }
  830. #else /* CONFIG_PPC_SUBPAGE_PROT */
  831. static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
  832. {
  833. return 0;
  834. }
  835. #endif
  836. void hash_failure_debug(unsigned long ea, unsigned long access,
  837. unsigned long vsid, unsigned long trap,
  838. int ssize, int psize, int lpsize, unsigned long pte)
  839. {
  840. if (!printk_ratelimit())
  841. return;
  842. pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
  843. ea, access, current->comm);
  844. pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
  845. trap, vsid, ssize, psize, lpsize, pte);
  846. }
  847. static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
  848. int psize, bool user_region)
  849. {
  850. if (user_region) {
  851. if (psize != get_paca_psize(ea)) {
  852. get_paca()->context = mm->context;
  853. slb_flush_and_rebolt();
  854. }
  855. } else if (get_paca()->vmalloc_sllp !=
  856. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  857. get_paca()->vmalloc_sllp =
  858. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  859. slb_vmalloc_update();
  860. }
  861. }
  862. /* Result code is:
  863. * 0 - handled
  864. * 1 - normal page fault
  865. * -1 - critical hash insertion error
  866. * -2 - access not permitted by subpage protection mechanism
  867. */
  868. int hash_page_mm(struct mm_struct *mm, unsigned long ea,
  869. unsigned long access, unsigned long trap,
  870. unsigned long flags)
  871. {
  872. bool is_thp;
  873. enum ctx_state prev_state = exception_enter();
  874. pgd_t *pgdir;
  875. unsigned long vsid;
  876. pte_t *ptep;
  877. unsigned hugeshift;
  878. const struct cpumask *tmp;
  879. int rc, user_region = 0;
  880. int psize, ssize;
  881. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  882. ea, access, trap);
  883. trace_hash_fault(ea, access, trap);
  884. /* Get region & vsid */
  885. switch (REGION_ID(ea)) {
  886. case USER_REGION_ID:
  887. user_region = 1;
  888. if (! mm) {
  889. DBG_LOW(" user region with no mm !\n");
  890. rc = 1;
  891. goto bail;
  892. }
  893. psize = get_slice_psize(mm, ea);
  894. ssize = user_segment_size(ea);
  895. vsid = get_vsid(mm->context.id, ea, ssize);
  896. break;
  897. case VMALLOC_REGION_ID:
  898. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  899. if (ea < VMALLOC_END)
  900. psize = mmu_vmalloc_psize;
  901. else
  902. psize = mmu_io_psize;
  903. ssize = mmu_kernel_ssize;
  904. break;
  905. default:
  906. /* Not a valid range
  907. * Send the problem up to do_page_fault
  908. */
  909. rc = 1;
  910. goto bail;
  911. }
  912. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  913. /* Bad address. */
  914. if (!vsid) {
  915. DBG_LOW("Bad address!\n");
  916. rc = 1;
  917. goto bail;
  918. }
  919. /* Get pgdir */
  920. pgdir = mm->pgd;
  921. if (pgdir == NULL) {
  922. rc = 1;
  923. goto bail;
  924. }
  925. /* Check CPU locality */
  926. tmp = cpumask_of(smp_processor_id());
  927. if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
  928. flags |= HPTE_LOCAL_UPDATE;
  929. #ifndef CONFIG_PPC_64K_PAGES
  930. /* If we use 4K pages and our psize is not 4K, then we might
  931. * be hitting a special driver mapping, and need to align the
  932. * address before we fetch the PTE.
  933. *
  934. * It could also be a hugepage mapping, in which case this is
  935. * not necessary, but it's not harmful, either.
  936. */
  937. if (psize != MMU_PAGE_4K)
  938. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  939. #endif /* CONFIG_PPC_64K_PAGES */
  940. /* Get PTE and page size from page tables */
  941. ptep = __find_linux_pte_or_hugepte(pgdir, ea, &is_thp, &hugeshift);
  942. if (ptep == NULL || !pte_present(*ptep)) {
  943. DBG_LOW(" no PTE !\n");
  944. rc = 1;
  945. goto bail;
  946. }
  947. /* Add _PAGE_PRESENT to the required access perm */
  948. access |= _PAGE_PRESENT;
  949. /* Pre-check access permissions (will be re-checked atomically
  950. * in __hash_page_XX but this pre-check is a fast path
  951. */
  952. if (access & ~pte_val(*ptep)) {
  953. DBG_LOW(" no access !\n");
  954. rc = 1;
  955. goto bail;
  956. }
  957. if (hugeshift) {
  958. if (is_thp)
  959. rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
  960. trap, flags, ssize, psize);
  961. #ifdef CONFIG_HUGETLB_PAGE
  962. else
  963. rc = __hash_page_huge(ea, access, vsid, ptep, trap,
  964. flags, ssize, hugeshift, psize);
  965. #else
  966. else {
  967. /*
  968. * if we have hugeshift, and is not transhuge with
  969. * hugetlb disabled, something is really wrong.
  970. */
  971. rc = 1;
  972. WARN_ON(1);
  973. }
  974. #endif
  975. if (current->mm == mm)
  976. check_paca_psize(ea, mm, psize, user_region);
  977. goto bail;
  978. }
  979. #ifndef CONFIG_PPC_64K_PAGES
  980. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  981. #else
  982. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  983. pte_val(*(ptep + PTRS_PER_PTE)));
  984. #endif
  985. /* Do actual hashing */
  986. #ifdef CONFIG_PPC_64K_PAGES
  987. /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
  988. if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
  989. demote_segment_4k(mm, ea);
  990. psize = MMU_PAGE_4K;
  991. }
  992. /* If this PTE is non-cacheable and we have restrictions on
  993. * using non cacheable large pages, then we switch to 4k
  994. */
  995. if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
  996. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  997. if (user_region) {
  998. demote_segment_4k(mm, ea);
  999. psize = MMU_PAGE_4K;
  1000. } else if (ea < VMALLOC_END) {
  1001. /*
  1002. * some driver did a non-cacheable mapping
  1003. * in vmalloc space, so switch vmalloc
  1004. * to 4k pages
  1005. */
  1006. printk(KERN_ALERT "Reducing vmalloc segment "
  1007. "to 4kB pages because of "
  1008. "non-cacheable mapping\n");
  1009. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  1010. copro_flush_all_slbs(mm);
  1011. }
  1012. }
  1013. if (current->mm == mm)
  1014. check_paca_psize(ea, mm, psize, user_region);
  1015. #endif /* CONFIG_PPC_64K_PAGES */
  1016. #ifdef CONFIG_PPC_64K_PAGES
  1017. if (psize == MMU_PAGE_64K)
  1018. rc = __hash_page_64K(ea, access, vsid, ptep, trap,
  1019. flags, ssize);
  1020. else
  1021. #endif /* CONFIG_PPC_64K_PAGES */
  1022. {
  1023. int spp = subpage_protection(mm, ea);
  1024. if (access & spp)
  1025. rc = -2;
  1026. else
  1027. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  1028. flags, ssize, spp);
  1029. }
  1030. /* Dump some info in case of hash insertion failure, they should
  1031. * never happen so it is really useful to know if/when they do
  1032. */
  1033. if (rc == -1)
  1034. hash_failure_debug(ea, access, vsid, trap, ssize, psize,
  1035. psize, pte_val(*ptep));
  1036. #ifndef CONFIG_PPC_64K_PAGES
  1037. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  1038. #else
  1039. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  1040. pte_val(*(ptep + PTRS_PER_PTE)));
  1041. #endif
  1042. DBG_LOW(" -> rc=%d\n", rc);
  1043. bail:
  1044. exception_exit(prev_state);
  1045. return rc;
  1046. }
  1047. EXPORT_SYMBOL_GPL(hash_page_mm);
  1048. int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
  1049. unsigned long dsisr)
  1050. {
  1051. unsigned long flags = 0;
  1052. struct mm_struct *mm = current->mm;
  1053. if (REGION_ID(ea) == VMALLOC_REGION_ID)
  1054. mm = &init_mm;
  1055. if (dsisr & DSISR_NOHPTE)
  1056. flags |= HPTE_NOHPTE_UPDATE;
  1057. return hash_page_mm(mm, ea, access, trap, flags);
  1058. }
  1059. EXPORT_SYMBOL_GPL(hash_page);
  1060. void hash_preload(struct mm_struct *mm, unsigned long ea,
  1061. unsigned long access, unsigned long trap)
  1062. {
  1063. int hugepage_shift;
  1064. unsigned long vsid;
  1065. pgd_t *pgdir;
  1066. pte_t *ptep;
  1067. unsigned long flags;
  1068. int rc, ssize, update_flags = 0;
  1069. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  1070. #ifdef CONFIG_PPC_MM_SLICES
  1071. /* We only prefault standard pages for now */
  1072. if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
  1073. return;
  1074. #endif
  1075. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  1076. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  1077. /* Get Linux PTE if available */
  1078. pgdir = mm->pgd;
  1079. if (pgdir == NULL)
  1080. return;
  1081. /* Get VSID */
  1082. ssize = user_segment_size(ea);
  1083. vsid = get_vsid(mm->context.id, ea, ssize);
  1084. if (!vsid)
  1085. return;
  1086. /*
  1087. * Hash doesn't like irqs. Walking linux page table with irq disabled
  1088. * saves us from holding multiple locks.
  1089. */
  1090. local_irq_save(flags);
  1091. /*
  1092. * THP pages use update_mmu_cache_pmd. We don't do
  1093. * hash preload there. Hence can ignore THP here
  1094. */
  1095. ptep = find_linux_pte_or_hugepte(pgdir, ea, NULL, &hugepage_shift);
  1096. if (!ptep)
  1097. goto out_exit;
  1098. WARN_ON(hugepage_shift);
  1099. #ifdef CONFIG_PPC_64K_PAGES
  1100. /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
  1101. * a 64K kernel), then we don't preload, hash_page() will take
  1102. * care of it once we actually try to access the page.
  1103. * That way we don't have to duplicate all of the logic for segment
  1104. * page size demotion here
  1105. */
  1106. if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
  1107. goto out_exit;
  1108. #endif /* CONFIG_PPC_64K_PAGES */
  1109. /* Is that local to this CPU ? */
  1110. if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  1111. update_flags |= HPTE_LOCAL_UPDATE;
  1112. /* Hash it in */
  1113. #ifdef CONFIG_PPC_64K_PAGES
  1114. if (mm->context.user_psize == MMU_PAGE_64K)
  1115. rc = __hash_page_64K(ea, access, vsid, ptep, trap,
  1116. update_flags, ssize);
  1117. else
  1118. #endif /* CONFIG_PPC_64K_PAGES */
  1119. rc = __hash_page_4K(ea, access, vsid, ptep, trap, update_flags,
  1120. ssize, subpage_protection(mm, ea));
  1121. /* Dump some info in case of hash insertion failure, they should
  1122. * never happen so it is really useful to know if/when they do
  1123. */
  1124. if (rc == -1)
  1125. hash_failure_debug(ea, access, vsid, trap, ssize,
  1126. mm->context.user_psize,
  1127. mm->context.user_psize,
  1128. pte_val(*ptep));
  1129. out_exit:
  1130. local_irq_restore(flags);
  1131. }
  1132. /* WARNING: This is called from hash_low_64.S, if you change this prototype,
  1133. * do not forget to update the assembly call site !
  1134. */
  1135. void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
  1136. unsigned long flags)
  1137. {
  1138. unsigned long hash, index, shift, hidx, slot;
  1139. int local = flags & HPTE_LOCAL_UPDATE;
  1140. DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
  1141. pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
  1142. hash = hpt_hash(vpn, shift, ssize);
  1143. hidx = __rpte_to_hidx(pte, index);
  1144. if (hidx & _PTEIDX_SECONDARY)
  1145. hash = ~hash;
  1146. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1147. slot += hidx & _PTEIDX_GROUP_IX;
  1148. DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
  1149. /*
  1150. * We use same base page size and actual psize, because we don't
  1151. * use these functions for hugepage
  1152. */
  1153. ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
  1154. } pte_iterate_hashed_end();
  1155. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1156. /* Transactions are not aborted by tlbiel, only tlbie.
  1157. * Without, syncing a page back to a block device w/ PIO could pick up
  1158. * transactional data (bad!) so we force an abort here. Before the
  1159. * sync the page will be made read-only, which will flush_hash_page.
  1160. * BIG ISSUE here: if the kernel uses a page from userspace without
  1161. * unmapping it first, it may see the speculated version.
  1162. */
  1163. if (local && cpu_has_feature(CPU_FTR_TM) &&
  1164. current->thread.regs &&
  1165. MSR_TM_ACTIVE(current->thread.regs->msr)) {
  1166. tm_enable();
  1167. tm_abort(TM_CAUSE_TLBI);
  1168. }
  1169. #endif
  1170. }
  1171. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1172. void flush_hash_hugepage(unsigned long vsid, unsigned long addr,
  1173. pmd_t *pmdp, unsigned int psize, int ssize,
  1174. unsigned long flags)
  1175. {
  1176. int i, max_hpte_count, valid;
  1177. unsigned long s_addr;
  1178. unsigned char *hpte_slot_array;
  1179. unsigned long hidx, shift, vpn, hash, slot;
  1180. int local = flags & HPTE_LOCAL_UPDATE;
  1181. s_addr = addr & HPAGE_PMD_MASK;
  1182. hpte_slot_array = get_hpte_slot_array(pmdp);
  1183. /*
  1184. * IF we try to do a HUGE PTE update after a withdraw is done.
  1185. * we will find the below NULL. This happens when we do
  1186. * split_huge_page_pmd
  1187. */
  1188. if (!hpte_slot_array)
  1189. return;
  1190. if (ppc_md.hugepage_invalidate) {
  1191. ppc_md.hugepage_invalidate(vsid, s_addr, hpte_slot_array,
  1192. psize, ssize, local);
  1193. goto tm_abort;
  1194. }
  1195. /*
  1196. * No bluk hpte removal support, invalidate each entry
  1197. */
  1198. shift = mmu_psize_defs[psize].shift;
  1199. max_hpte_count = HPAGE_PMD_SIZE >> shift;
  1200. for (i = 0; i < max_hpte_count; i++) {
  1201. /*
  1202. * 8 bits per each hpte entries
  1203. * 000| [ secondary group (one bit) | hidx (3 bits) | valid bit]
  1204. */
  1205. valid = hpte_valid(hpte_slot_array, i);
  1206. if (!valid)
  1207. continue;
  1208. hidx = hpte_hash_index(hpte_slot_array, i);
  1209. /* get the vpn */
  1210. addr = s_addr + (i * (1ul << shift));
  1211. vpn = hpt_vpn(addr, vsid, ssize);
  1212. hash = hpt_hash(vpn, shift, ssize);
  1213. if (hidx & _PTEIDX_SECONDARY)
  1214. hash = ~hash;
  1215. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1216. slot += hidx & _PTEIDX_GROUP_IX;
  1217. ppc_md.hpte_invalidate(slot, vpn, psize,
  1218. MMU_PAGE_16M, ssize, local);
  1219. }
  1220. tm_abort:
  1221. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1222. /* Transactions are not aborted by tlbiel, only tlbie.
  1223. * Without, syncing a page back to a block device w/ PIO could pick up
  1224. * transactional data (bad!) so we force an abort here. Before the
  1225. * sync the page will be made read-only, which will flush_hash_page.
  1226. * BIG ISSUE here: if the kernel uses a page from userspace without
  1227. * unmapping it first, it may see the speculated version.
  1228. */
  1229. if (local && cpu_has_feature(CPU_FTR_TM) &&
  1230. current->thread.regs &&
  1231. MSR_TM_ACTIVE(current->thread.regs->msr)) {
  1232. tm_enable();
  1233. tm_abort(TM_CAUSE_TLBI);
  1234. }
  1235. #endif
  1236. return;
  1237. }
  1238. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1239. void flush_hash_range(unsigned long number, int local)
  1240. {
  1241. if (ppc_md.flush_hash_range)
  1242. ppc_md.flush_hash_range(number, local);
  1243. else {
  1244. int i;
  1245. struct ppc64_tlb_batch *batch =
  1246. this_cpu_ptr(&ppc64_tlb_batch);
  1247. for (i = 0; i < number; i++)
  1248. flush_hash_page(batch->vpn[i], batch->pte[i],
  1249. batch->psize, batch->ssize, local);
  1250. }
  1251. }
  1252. /*
  1253. * low_hash_fault is called when we the low level hash code failed
  1254. * to instert a PTE due to an hypervisor error
  1255. */
  1256. void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
  1257. {
  1258. enum ctx_state prev_state = exception_enter();
  1259. if (user_mode(regs)) {
  1260. #ifdef CONFIG_PPC_SUBPAGE_PROT
  1261. if (rc == -2)
  1262. _exception(SIGSEGV, regs, SEGV_ACCERR, address);
  1263. else
  1264. #endif
  1265. _exception(SIGBUS, regs, BUS_ADRERR, address);
  1266. } else
  1267. bad_page_fault(regs, address, SIGBUS);
  1268. exception_exit(prev_state);
  1269. }
  1270. long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
  1271. unsigned long pa, unsigned long rflags,
  1272. unsigned long vflags, int psize, int ssize)
  1273. {
  1274. unsigned long hpte_group;
  1275. long slot;
  1276. repeat:
  1277. hpte_group = ((hash & htab_hash_mask) *
  1278. HPTES_PER_GROUP) & ~0x7UL;
  1279. /* Insert into the hash table, primary slot */
  1280. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
  1281. psize, psize, ssize);
  1282. /* Primary is full, try the secondary */
  1283. if (unlikely(slot == -1)) {
  1284. hpte_group = ((~hash & htab_hash_mask) *
  1285. HPTES_PER_GROUP) & ~0x7UL;
  1286. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
  1287. vflags | HPTE_V_SECONDARY,
  1288. psize, psize, ssize);
  1289. if (slot == -1) {
  1290. if (mftb() & 0x1)
  1291. hpte_group = ((hash & htab_hash_mask) *
  1292. HPTES_PER_GROUP)&~0x7UL;
  1293. ppc_md.hpte_remove(hpte_group);
  1294. goto repeat;
  1295. }
  1296. }
  1297. return slot;
  1298. }
  1299. #ifdef CONFIG_DEBUG_PAGEALLOC
  1300. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  1301. {
  1302. unsigned long hash;
  1303. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1304. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1305. unsigned long mode = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL));
  1306. long ret;
  1307. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1308. /* Don't create HPTE entries for bad address */
  1309. if (!vsid)
  1310. return;
  1311. ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
  1312. HPTE_V_BOLTED,
  1313. mmu_linear_psize, mmu_kernel_ssize);
  1314. BUG_ON (ret < 0);
  1315. spin_lock(&linear_map_hash_lock);
  1316. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  1317. linear_map_hash_slots[lmi] = ret | 0x80;
  1318. spin_unlock(&linear_map_hash_lock);
  1319. }
  1320. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  1321. {
  1322. unsigned long hash, hidx, slot;
  1323. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1324. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1325. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1326. spin_lock(&linear_map_hash_lock);
  1327. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  1328. hidx = linear_map_hash_slots[lmi] & 0x7f;
  1329. linear_map_hash_slots[lmi] = 0;
  1330. spin_unlock(&linear_map_hash_lock);
  1331. if (hidx & _PTEIDX_SECONDARY)
  1332. hash = ~hash;
  1333. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1334. slot += hidx & _PTEIDX_GROUP_IX;
  1335. ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
  1336. mmu_kernel_ssize, 0);
  1337. }
  1338. void __kernel_map_pages(struct page *page, int numpages, int enable)
  1339. {
  1340. unsigned long flags, vaddr, lmi;
  1341. int i;
  1342. local_irq_save(flags);
  1343. for (i = 0; i < numpages; i++, page++) {
  1344. vaddr = (unsigned long)page_address(page);
  1345. lmi = __pa(vaddr) >> PAGE_SHIFT;
  1346. if (lmi >= linear_map_hash_count)
  1347. continue;
  1348. if (enable)
  1349. kernel_map_linear_page(vaddr, lmi);
  1350. else
  1351. kernel_unmap_linear_page(vaddr, lmi);
  1352. }
  1353. local_irq_restore(flags);
  1354. }
  1355. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1356. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  1357. phys_addr_t first_memblock_size)
  1358. {
  1359. /* We don't currently support the first MEMBLOCK not mapping 0
  1360. * physical on those processors
  1361. */
  1362. BUG_ON(first_memblock_base != 0);
  1363. /* On LPAR systems, the first entry is our RMA region,
  1364. * non-LPAR 64-bit hash MMU systems don't have a limitation
  1365. * on real mode access, but using the first entry works well
  1366. * enough. We also clamp it to 1G to avoid some funky things
  1367. * such as RTAS bugs etc...
  1368. */
  1369. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  1370. /* Finally limit subsequent allocations */
  1371. memblock_set_current_limit(ppc64_rma_size);
  1372. }