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_HAS_HASH_64K
  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_HAS_HASH_64K */
  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. enum ctx_state prev_state = exception_enter();
  873. pgd_t *pgdir;
  874. unsigned long vsid;
  875. pte_t *ptep;
  876. unsigned hugeshift;
  877. const struct cpumask *tmp;
  878. int rc, user_region = 0;
  879. int psize, ssize;
  880. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  881. ea, access, trap);
  882. trace_hash_fault(ea, access, trap);
  883. /* Get region & vsid */
  884. switch (REGION_ID(ea)) {
  885. case USER_REGION_ID:
  886. user_region = 1;
  887. if (! mm) {
  888. DBG_LOW(" user region with no mm !\n");
  889. rc = 1;
  890. goto bail;
  891. }
  892. psize = get_slice_psize(mm, ea);
  893. ssize = user_segment_size(ea);
  894. vsid = get_vsid(mm->context.id, ea, ssize);
  895. break;
  896. case VMALLOC_REGION_ID:
  897. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  898. if (ea < VMALLOC_END)
  899. psize = mmu_vmalloc_psize;
  900. else
  901. psize = mmu_io_psize;
  902. ssize = mmu_kernel_ssize;
  903. break;
  904. default:
  905. /* Not a valid range
  906. * Send the problem up to do_page_fault
  907. */
  908. rc = 1;
  909. goto bail;
  910. }
  911. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  912. /* Bad address. */
  913. if (!vsid) {
  914. DBG_LOW("Bad address!\n");
  915. rc = 1;
  916. goto bail;
  917. }
  918. /* Get pgdir */
  919. pgdir = mm->pgd;
  920. if (pgdir == NULL) {
  921. rc = 1;
  922. goto bail;
  923. }
  924. /* Check CPU locality */
  925. tmp = cpumask_of(smp_processor_id());
  926. if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
  927. flags |= HPTE_LOCAL_UPDATE;
  928. #ifndef CONFIG_PPC_64K_PAGES
  929. /* If we use 4K pages and our psize is not 4K, then we might
  930. * be hitting a special driver mapping, and need to align the
  931. * address before we fetch the PTE.
  932. *
  933. * It could also be a hugepage mapping, in which case this is
  934. * not necessary, but it's not harmful, either.
  935. */
  936. if (psize != MMU_PAGE_4K)
  937. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  938. #endif /* CONFIG_PPC_64K_PAGES */
  939. /* Get PTE and page size from page tables */
  940. ptep = __find_linux_pte_or_hugepte(pgdir, ea, &hugeshift);
  941. if (ptep == NULL || !pte_present(*ptep)) {
  942. DBG_LOW(" no PTE !\n");
  943. rc = 1;
  944. goto bail;
  945. }
  946. /* Add _PAGE_PRESENT to the required access perm */
  947. access |= _PAGE_PRESENT;
  948. /* Pre-check access permissions (will be re-checked atomically
  949. * in __hash_page_XX but this pre-check is a fast path
  950. */
  951. if (access & ~pte_val(*ptep)) {
  952. DBG_LOW(" no access !\n");
  953. rc = 1;
  954. goto bail;
  955. }
  956. if (hugeshift) {
  957. if (pmd_trans_huge(*(pmd_t *)ptep))
  958. rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
  959. trap, flags, ssize, psize);
  960. #ifdef CONFIG_HUGETLB_PAGE
  961. else
  962. rc = __hash_page_huge(ea, access, vsid, ptep, trap,
  963. flags, ssize, hugeshift, psize);
  964. #else
  965. else {
  966. /*
  967. * if we have hugeshift, and is not transhuge with
  968. * hugetlb disabled, something is really wrong.
  969. */
  970. rc = 1;
  971. WARN_ON(1);
  972. }
  973. #endif
  974. if (current->mm == mm)
  975. check_paca_psize(ea, mm, psize, user_region);
  976. goto bail;
  977. }
  978. #ifndef CONFIG_PPC_64K_PAGES
  979. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  980. #else
  981. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  982. pte_val(*(ptep + PTRS_PER_PTE)));
  983. #endif
  984. /* Do actual hashing */
  985. #ifdef CONFIG_PPC_64K_PAGES
  986. /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
  987. if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
  988. demote_segment_4k(mm, ea);
  989. psize = MMU_PAGE_4K;
  990. }
  991. /* If this PTE is non-cacheable and we have restrictions on
  992. * using non cacheable large pages, then we switch to 4k
  993. */
  994. if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
  995. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  996. if (user_region) {
  997. demote_segment_4k(mm, ea);
  998. psize = MMU_PAGE_4K;
  999. } else if (ea < VMALLOC_END) {
  1000. /*
  1001. * some driver did a non-cacheable mapping
  1002. * in vmalloc space, so switch vmalloc
  1003. * to 4k pages
  1004. */
  1005. printk(KERN_ALERT "Reducing vmalloc segment "
  1006. "to 4kB pages because of "
  1007. "non-cacheable mapping\n");
  1008. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  1009. copro_flush_all_slbs(mm);
  1010. }
  1011. }
  1012. if (current->mm == mm)
  1013. check_paca_psize(ea, mm, psize, user_region);
  1014. #endif /* CONFIG_PPC_64K_PAGES */
  1015. #ifdef CONFIG_PPC_HAS_HASH_64K
  1016. if (psize == MMU_PAGE_64K)
  1017. rc = __hash_page_64K(ea, access, vsid, ptep, trap,
  1018. flags, ssize);
  1019. else
  1020. #endif /* CONFIG_PPC_HAS_HASH_64K */
  1021. {
  1022. int spp = subpage_protection(mm, ea);
  1023. if (access & spp)
  1024. rc = -2;
  1025. else
  1026. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  1027. flags, ssize, spp);
  1028. }
  1029. /* Dump some info in case of hash insertion failure, they should
  1030. * never happen so it is really useful to know if/when they do
  1031. */
  1032. if (rc == -1)
  1033. hash_failure_debug(ea, access, vsid, trap, ssize, psize,
  1034. psize, pte_val(*ptep));
  1035. #ifndef CONFIG_PPC_64K_PAGES
  1036. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  1037. #else
  1038. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  1039. pte_val(*(ptep + PTRS_PER_PTE)));
  1040. #endif
  1041. DBG_LOW(" -> rc=%d\n", rc);
  1042. bail:
  1043. exception_exit(prev_state);
  1044. return rc;
  1045. }
  1046. EXPORT_SYMBOL_GPL(hash_page_mm);
  1047. int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
  1048. unsigned long dsisr)
  1049. {
  1050. unsigned long flags = 0;
  1051. struct mm_struct *mm = current->mm;
  1052. if (REGION_ID(ea) == VMALLOC_REGION_ID)
  1053. mm = &init_mm;
  1054. if (dsisr & DSISR_NOHPTE)
  1055. flags |= HPTE_NOHPTE_UPDATE;
  1056. return hash_page_mm(mm, ea, access, trap, flags);
  1057. }
  1058. EXPORT_SYMBOL_GPL(hash_page);
  1059. void hash_preload(struct mm_struct *mm, unsigned long ea,
  1060. unsigned long access, unsigned long trap)
  1061. {
  1062. int hugepage_shift;
  1063. unsigned long vsid;
  1064. pgd_t *pgdir;
  1065. pte_t *ptep;
  1066. unsigned long flags;
  1067. int rc, ssize, update_flags = 0;
  1068. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  1069. #ifdef CONFIG_PPC_MM_SLICES
  1070. /* We only prefault standard pages for now */
  1071. if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
  1072. return;
  1073. #endif
  1074. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  1075. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  1076. /* Get Linux PTE if available */
  1077. pgdir = mm->pgd;
  1078. if (pgdir == NULL)
  1079. return;
  1080. /* Get VSID */
  1081. ssize = user_segment_size(ea);
  1082. vsid = get_vsid(mm->context.id, ea, ssize);
  1083. if (!vsid)
  1084. return;
  1085. /*
  1086. * Hash doesn't like irqs. Walking linux page table with irq disabled
  1087. * saves us from holding multiple locks.
  1088. */
  1089. local_irq_save(flags);
  1090. /*
  1091. * THP pages use update_mmu_cache_pmd. We don't do
  1092. * hash preload there. Hence can ignore THP here
  1093. */
  1094. ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugepage_shift);
  1095. if (!ptep)
  1096. goto out_exit;
  1097. WARN_ON(hugepage_shift);
  1098. #ifdef CONFIG_PPC_64K_PAGES
  1099. /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
  1100. * a 64K kernel), then we don't preload, hash_page() will take
  1101. * care of it once we actually try to access the page.
  1102. * That way we don't have to duplicate all of the logic for segment
  1103. * page size demotion here
  1104. */
  1105. if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
  1106. goto out_exit;
  1107. #endif /* CONFIG_PPC_64K_PAGES */
  1108. /* Is that local to this CPU ? */
  1109. if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  1110. update_flags |= HPTE_LOCAL_UPDATE;
  1111. /* Hash it in */
  1112. #ifdef CONFIG_PPC_HAS_HASH_64K
  1113. if (mm->context.user_psize == MMU_PAGE_64K)
  1114. rc = __hash_page_64K(ea, access, vsid, ptep, trap,
  1115. update_flags, ssize);
  1116. else
  1117. #endif /* CONFIG_PPC_HAS_HASH_64K */
  1118. rc = __hash_page_4K(ea, access, vsid, ptep, trap, update_flags,
  1119. ssize, subpage_protection(mm, ea));
  1120. /* Dump some info in case of hash insertion failure, they should
  1121. * never happen so it is really useful to know if/when they do
  1122. */
  1123. if (rc == -1)
  1124. hash_failure_debug(ea, access, vsid, trap, ssize,
  1125. mm->context.user_psize,
  1126. mm->context.user_psize,
  1127. pte_val(*ptep));
  1128. out_exit:
  1129. local_irq_restore(flags);
  1130. }
  1131. /* WARNING: This is called from hash_low_64.S, if you change this prototype,
  1132. * do not forget to update the assembly call site !
  1133. */
  1134. void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
  1135. unsigned long flags)
  1136. {
  1137. unsigned long hash, index, shift, hidx, slot;
  1138. int local = flags & HPTE_LOCAL_UPDATE;
  1139. DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
  1140. pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
  1141. hash = hpt_hash(vpn, shift, ssize);
  1142. hidx = __rpte_to_hidx(pte, index);
  1143. if (hidx & _PTEIDX_SECONDARY)
  1144. hash = ~hash;
  1145. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1146. slot += hidx & _PTEIDX_GROUP_IX;
  1147. DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
  1148. /*
  1149. * We use same base page size and actual psize, because we don't
  1150. * use these functions for hugepage
  1151. */
  1152. ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
  1153. } pte_iterate_hashed_end();
  1154. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1155. /* Transactions are not aborted by tlbiel, only tlbie.
  1156. * Without, syncing a page back to a block device w/ PIO could pick up
  1157. * transactional data (bad!) so we force an abort here. Before the
  1158. * sync the page will be made read-only, which will flush_hash_page.
  1159. * BIG ISSUE here: if the kernel uses a page from userspace without
  1160. * unmapping it first, it may see the speculated version.
  1161. */
  1162. if (local && cpu_has_feature(CPU_FTR_TM) &&
  1163. current->thread.regs &&
  1164. MSR_TM_ACTIVE(current->thread.regs->msr)) {
  1165. tm_enable();
  1166. tm_abort(TM_CAUSE_TLBI);
  1167. }
  1168. #endif
  1169. }
  1170. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1171. void flush_hash_hugepage(unsigned long vsid, unsigned long addr,
  1172. pmd_t *pmdp, unsigned int psize, int ssize,
  1173. unsigned long flags)
  1174. {
  1175. int i, max_hpte_count, valid;
  1176. unsigned long s_addr;
  1177. unsigned char *hpte_slot_array;
  1178. unsigned long hidx, shift, vpn, hash, slot;
  1179. int local = flags & HPTE_LOCAL_UPDATE;
  1180. s_addr = addr & HPAGE_PMD_MASK;
  1181. hpte_slot_array = get_hpte_slot_array(pmdp);
  1182. /*
  1183. * IF we try to do a HUGE PTE update after a withdraw is done.
  1184. * we will find the below NULL. This happens when we do
  1185. * split_huge_page_pmd
  1186. */
  1187. if (!hpte_slot_array)
  1188. return;
  1189. if (ppc_md.hugepage_invalidate) {
  1190. ppc_md.hugepage_invalidate(vsid, s_addr, hpte_slot_array,
  1191. psize, ssize, local);
  1192. goto tm_abort;
  1193. }
  1194. /*
  1195. * No bluk hpte removal support, invalidate each entry
  1196. */
  1197. shift = mmu_psize_defs[psize].shift;
  1198. max_hpte_count = HPAGE_PMD_SIZE >> shift;
  1199. for (i = 0; i < max_hpte_count; i++) {
  1200. /*
  1201. * 8 bits per each hpte entries
  1202. * 000| [ secondary group (one bit) | hidx (3 bits) | valid bit]
  1203. */
  1204. valid = hpte_valid(hpte_slot_array, i);
  1205. if (!valid)
  1206. continue;
  1207. hidx = hpte_hash_index(hpte_slot_array, i);
  1208. /* get the vpn */
  1209. addr = s_addr + (i * (1ul << shift));
  1210. vpn = hpt_vpn(addr, vsid, ssize);
  1211. hash = hpt_hash(vpn, shift, ssize);
  1212. if (hidx & _PTEIDX_SECONDARY)
  1213. hash = ~hash;
  1214. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1215. slot += hidx & _PTEIDX_GROUP_IX;
  1216. ppc_md.hpte_invalidate(slot, vpn, psize,
  1217. MMU_PAGE_16M, ssize, local);
  1218. }
  1219. tm_abort:
  1220. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1221. /* Transactions are not aborted by tlbiel, only tlbie.
  1222. * Without, syncing a page back to a block device w/ PIO could pick up
  1223. * transactional data (bad!) so we force an abort here. Before the
  1224. * sync the page will be made read-only, which will flush_hash_page.
  1225. * BIG ISSUE here: if the kernel uses a page from userspace without
  1226. * unmapping it first, it may see the speculated version.
  1227. */
  1228. if (local && cpu_has_feature(CPU_FTR_TM) &&
  1229. current->thread.regs &&
  1230. MSR_TM_ACTIVE(current->thread.regs->msr)) {
  1231. tm_enable();
  1232. tm_abort(TM_CAUSE_TLBI);
  1233. }
  1234. #endif
  1235. return;
  1236. }
  1237. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1238. void flush_hash_range(unsigned long number, int local)
  1239. {
  1240. if (ppc_md.flush_hash_range)
  1241. ppc_md.flush_hash_range(number, local);
  1242. else {
  1243. int i;
  1244. struct ppc64_tlb_batch *batch =
  1245. this_cpu_ptr(&ppc64_tlb_batch);
  1246. for (i = 0; i < number; i++)
  1247. flush_hash_page(batch->vpn[i], batch->pte[i],
  1248. batch->psize, batch->ssize, local);
  1249. }
  1250. }
  1251. /*
  1252. * low_hash_fault is called when we the low level hash code failed
  1253. * to instert a PTE due to an hypervisor error
  1254. */
  1255. void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
  1256. {
  1257. enum ctx_state prev_state = exception_enter();
  1258. if (user_mode(regs)) {
  1259. #ifdef CONFIG_PPC_SUBPAGE_PROT
  1260. if (rc == -2)
  1261. _exception(SIGSEGV, regs, SEGV_ACCERR, address);
  1262. else
  1263. #endif
  1264. _exception(SIGBUS, regs, BUS_ADRERR, address);
  1265. } else
  1266. bad_page_fault(regs, address, SIGBUS);
  1267. exception_exit(prev_state);
  1268. }
  1269. long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
  1270. unsigned long pa, unsigned long rflags,
  1271. unsigned long vflags, int psize, int ssize)
  1272. {
  1273. unsigned long hpte_group;
  1274. long slot;
  1275. repeat:
  1276. hpte_group = ((hash & htab_hash_mask) *
  1277. HPTES_PER_GROUP) & ~0x7UL;
  1278. /* Insert into the hash table, primary slot */
  1279. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
  1280. psize, psize, ssize);
  1281. /* Primary is full, try the secondary */
  1282. if (unlikely(slot == -1)) {
  1283. hpte_group = ((~hash & htab_hash_mask) *
  1284. HPTES_PER_GROUP) & ~0x7UL;
  1285. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
  1286. vflags | HPTE_V_SECONDARY,
  1287. psize, psize, ssize);
  1288. if (slot == -1) {
  1289. if (mftb() & 0x1)
  1290. hpte_group = ((hash & htab_hash_mask) *
  1291. HPTES_PER_GROUP)&~0x7UL;
  1292. ppc_md.hpte_remove(hpte_group);
  1293. goto repeat;
  1294. }
  1295. }
  1296. return slot;
  1297. }
  1298. #ifdef CONFIG_DEBUG_PAGEALLOC
  1299. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  1300. {
  1301. unsigned long hash;
  1302. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1303. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1304. unsigned long mode = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL));
  1305. long ret;
  1306. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1307. /* Don't create HPTE entries for bad address */
  1308. if (!vsid)
  1309. return;
  1310. ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
  1311. HPTE_V_BOLTED,
  1312. mmu_linear_psize, mmu_kernel_ssize);
  1313. BUG_ON (ret < 0);
  1314. spin_lock(&linear_map_hash_lock);
  1315. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  1316. linear_map_hash_slots[lmi] = ret | 0x80;
  1317. spin_unlock(&linear_map_hash_lock);
  1318. }
  1319. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  1320. {
  1321. unsigned long hash, hidx, slot;
  1322. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1323. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1324. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1325. spin_lock(&linear_map_hash_lock);
  1326. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  1327. hidx = linear_map_hash_slots[lmi] & 0x7f;
  1328. linear_map_hash_slots[lmi] = 0;
  1329. spin_unlock(&linear_map_hash_lock);
  1330. if (hidx & _PTEIDX_SECONDARY)
  1331. hash = ~hash;
  1332. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1333. slot += hidx & _PTEIDX_GROUP_IX;
  1334. ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
  1335. mmu_kernel_ssize, 0);
  1336. }
  1337. void __kernel_map_pages(struct page *page, int numpages, int enable)
  1338. {
  1339. unsigned long flags, vaddr, lmi;
  1340. int i;
  1341. local_irq_save(flags);
  1342. for (i = 0; i < numpages; i++, page++) {
  1343. vaddr = (unsigned long)page_address(page);
  1344. lmi = __pa(vaddr) >> PAGE_SHIFT;
  1345. if (lmi >= linear_map_hash_count)
  1346. continue;
  1347. if (enable)
  1348. kernel_map_linear_page(vaddr, lmi);
  1349. else
  1350. kernel_unmap_linear_page(vaddr, lmi);
  1351. }
  1352. local_irq_restore(flags);
  1353. }
  1354. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1355. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  1356. phys_addr_t first_memblock_size)
  1357. {
  1358. /* We don't currently support the first MEMBLOCK not mapping 0
  1359. * physical on those processors
  1360. */
  1361. BUG_ON(first_memblock_base != 0);
  1362. /* On LPAR systems, the first entry is our RMA region,
  1363. * non-LPAR 64-bit hash MMU systems don't have a limitation
  1364. * on real mode access, but using the first entry works well
  1365. * enough. We also clamp it to 1G to avoid some funky things
  1366. * such as RTAS bugs etc...
  1367. */
  1368. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  1369. /* Finally limit subsequent allocations */
  1370. memblock_set_current_limit(ppc64_rma_size);
  1371. }