hash_utils_64.c 38 KB

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