hash_utils_64.c 37 KB

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