hash_utils_64.c 34 KB

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