hash_utils_64.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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/system.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/machdep.h>
  43. #include <asm/prom.h>
  44. #include <asm/abs_addr.h>
  45. #include <asm/tlbflush.h>
  46. #include <asm/io.h>
  47. #include <asm/eeh.h>
  48. #include <asm/tlb.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/cputable.h>
  51. #include <asm/sections.h>
  52. #include <asm/spu.h>
  53. #include <asm/udbg.h>
  54. #include <asm/code-patching.h>
  55. #include <asm/fadump.h>
  56. #include <asm/firmware.h>
  57. #ifdef DEBUG
  58. #define DBG(fmt...) udbg_printf(fmt)
  59. #else
  60. #define DBG(fmt...)
  61. #endif
  62. #ifdef DEBUG_LOW
  63. #define DBG_LOW(fmt...) udbg_printf(fmt)
  64. #else
  65. #define DBG_LOW(fmt...)
  66. #endif
  67. #define KB (1024)
  68. #define MB (1024*KB)
  69. #define GB (1024L*MB)
  70. /*
  71. * Note: pte --> Linux PTE
  72. * HPTE --> PowerPC Hashed Page Table Entry
  73. *
  74. * Execution context:
  75. * htab_initialize is called with the MMU off (of course), but
  76. * the kernel has been copied down to zero so it can directly
  77. * reference global data. At this point it is very difficult
  78. * to print debug info.
  79. *
  80. */
  81. #ifdef CONFIG_U3_DART
  82. extern unsigned long dart_tablebase;
  83. #endif /* CONFIG_U3_DART */
  84. static unsigned long _SDR1;
  85. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
  86. struct hash_pte *htab_address;
  87. unsigned long htab_size_bytes;
  88. unsigned long htab_hash_mask;
  89. EXPORT_SYMBOL_GPL(htab_hash_mask);
  90. int mmu_linear_psize = MMU_PAGE_4K;
  91. int mmu_virtual_psize = MMU_PAGE_4K;
  92. int mmu_vmalloc_psize = MMU_PAGE_4K;
  93. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  94. int mmu_vmemmap_psize = MMU_PAGE_4K;
  95. #endif
  96. int mmu_io_psize = MMU_PAGE_4K;
  97. int mmu_kernel_ssize = MMU_SEGSIZE_256M;
  98. int mmu_highuser_ssize = MMU_SEGSIZE_256M;
  99. u16 mmu_slb_size = 64;
  100. EXPORT_SYMBOL_GPL(mmu_slb_size);
  101. #ifdef CONFIG_PPC_64K_PAGES
  102. int mmu_ci_restrictions;
  103. #endif
  104. #ifdef CONFIG_DEBUG_PAGEALLOC
  105. static u8 *linear_map_hash_slots;
  106. static unsigned long linear_map_hash_count;
  107. static DEFINE_SPINLOCK(linear_map_hash_lock);
  108. #endif /* CONFIG_DEBUG_PAGEALLOC */
  109. /* There are definitions of page sizes arrays to be used when none
  110. * is provided by the firmware.
  111. */
  112. /* Pre-POWER4 CPUs (4k pages only)
  113. */
  114. static struct mmu_psize_def mmu_psize_defaults_old[] = {
  115. [MMU_PAGE_4K] = {
  116. .shift = 12,
  117. .sllp = 0,
  118. .penc = 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. int create_section_mapping(unsigned long start, unsigned long end)
  478. {
  479. return 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. #ifdef CONFIG_FA_DUMP
  556. /*
  557. * If firmware assisted dump is active firmware preserves
  558. * the contents of htab along with entire partition memory.
  559. * Clear the htab if firmware assisted dump is active so
  560. * that we dont end up using old mappings.
  561. */
  562. if (is_fadump_active() && ppc_md.hpte_clear_all)
  563. ppc_md.hpte_clear_all();
  564. #endif
  565. } else {
  566. /* Find storage for the HPT. Must be contiguous in
  567. * the absolute address space. On cell we want it to be
  568. * in the first 2 Gig so we can use it for IOMMU hacks.
  569. */
  570. if (machine_is(cell))
  571. limit = 0x80000000;
  572. else
  573. limit = MEMBLOCK_ALLOC_ANYWHERE;
  574. table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
  575. DBG("Hash table allocated at %lx, size: %lx\n", table,
  576. htab_size_bytes);
  577. htab_address = abs_to_virt(table);
  578. /* htab absolute addr + encoded htabsize */
  579. _SDR1 = table + __ilog2(pteg_count) - 11;
  580. /* Initialize the HPT with no entries */
  581. memset((void *)table, 0, htab_size_bytes);
  582. /* Set SDR1 */
  583. mtspr(SPRN_SDR1, _SDR1);
  584. }
  585. prot = pgprot_val(PAGE_KERNEL);
  586. #ifdef CONFIG_DEBUG_PAGEALLOC
  587. linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
  588. linear_map_hash_slots = __va(memblock_alloc_base(linear_map_hash_count,
  589. 1, ppc64_rma_size));
  590. memset(linear_map_hash_slots, 0, linear_map_hash_count);
  591. #endif /* CONFIG_DEBUG_PAGEALLOC */
  592. /* On U3 based machines, we need to reserve the DART area and
  593. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  594. * cacheable later on
  595. */
  596. /* create bolted the linear mapping in the hash table */
  597. for_each_memblock(memory, reg) {
  598. base = (unsigned long)__va(reg->base);
  599. size = reg->size;
  600. DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
  601. base, size, prot);
  602. #ifdef CONFIG_U3_DART
  603. /* Do not map the DART space. Fortunately, it will be aligned
  604. * in such a way that it will not cross two memblock regions and
  605. * will fit within a single 16Mb page.
  606. * The DART space is assumed to be a full 16Mb region even if
  607. * we only use 2Mb of that space. We will use more of it later
  608. * for AGP GART. We have to use a full 16Mb large page.
  609. */
  610. DBG("DART base: %lx\n", dart_tablebase);
  611. if (dart_tablebase != 0 && dart_tablebase >= base
  612. && dart_tablebase < (base + size)) {
  613. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  614. if (base != dart_tablebase)
  615. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  616. __pa(base), prot,
  617. mmu_linear_psize,
  618. mmu_kernel_ssize));
  619. if ((base + size) > dart_table_end)
  620. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  621. base + size,
  622. __pa(dart_table_end),
  623. prot,
  624. mmu_linear_psize,
  625. mmu_kernel_ssize));
  626. continue;
  627. }
  628. #endif /* CONFIG_U3_DART */
  629. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  630. prot, mmu_linear_psize, mmu_kernel_ssize));
  631. }
  632. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  633. /*
  634. * If we have a memory_limit and we've allocated TCEs then we need to
  635. * explicitly map the TCE area at the top of RAM. We also cope with the
  636. * case that the TCEs start below memory_limit.
  637. * tce_alloc_start/end are 16MB aligned so the mapping should work
  638. * for either 4K or 16MB pages.
  639. */
  640. if (tce_alloc_start) {
  641. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  642. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  643. if (base + size >= tce_alloc_start)
  644. tce_alloc_start = base + size + 1;
  645. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  646. __pa(tce_alloc_start), prot,
  647. mmu_linear_psize, mmu_kernel_ssize));
  648. }
  649. htab_finish_init();
  650. DBG(" <- htab_initialize()\n");
  651. }
  652. #undef KB
  653. #undef MB
  654. void __init early_init_mmu(void)
  655. {
  656. /* Setup initial STAB address in the PACA */
  657. get_paca()->stab_real = __pa((u64)&initial_stab);
  658. get_paca()->stab_addr = (u64)&initial_stab;
  659. /* Initialize the MMU Hash table and create the linear mapping
  660. * of memory. Has to be done before stab/slb initialization as
  661. * this is currently where the page size encoding is obtained
  662. */
  663. htab_initialize();
  664. /* Initialize stab / SLB management */
  665. if (mmu_has_feature(MMU_FTR_SLB))
  666. slb_initialize();
  667. }
  668. #ifdef CONFIG_SMP
  669. void __cpuinit early_init_mmu_secondary(void)
  670. {
  671. /* Initialize hash table for that CPU */
  672. if (!firmware_has_feature(FW_FEATURE_LPAR))
  673. mtspr(SPRN_SDR1, _SDR1);
  674. /* Initialize STAB/SLB. We use a virtual address as it works
  675. * in real mode on pSeries.
  676. */
  677. if (mmu_has_feature(MMU_FTR_SLB))
  678. slb_initialize();
  679. else
  680. stab_initialize(get_paca()->stab_addr);
  681. }
  682. #endif /* CONFIG_SMP */
  683. /*
  684. * Called by asm hashtable.S for doing lazy icache flush
  685. */
  686. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  687. {
  688. struct page *page;
  689. if (!pfn_valid(pte_pfn(pte)))
  690. return pp;
  691. page = pte_page(pte);
  692. /* page is dirty */
  693. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  694. if (trap == 0x400) {
  695. flush_dcache_icache_page(page);
  696. set_bit(PG_arch_1, &page->flags);
  697. } else
  698. pp |= HPTE_R_N;
  699. }
  700. return pp;
  701. }
  702. #ifdef CONFIG_PPC_MM_SLICES
  703. unsigned int get_paca_psize(unsigned long addr)
  704. {
  705. unsigned long index, slices;
  706. if (addr < SLICE_LOW_TOP) {
  707. slices = get_paca()->context.low_slices_psize;
  708. index = GET_LOW_SLICE_INDEX(addr);
  709. } else {
  710. slices = get_paca()->context.high_slices_psize;
  711. index = GET_HIGH_SLICE_INDEX(addr);
  712. }
  713. return (slices >> (index * 4)) & 0xF;
  714. }
  715. #else
  716. unsigned int get_paca_psize(unsigned long addr)
  717. {
  718. return get_paca()->context.user_psize;
  719. }
  720. #endif
  721. /*
  722. * Demote a segment to using 4k pages.
  723. * For now this makes the whole process use 4k pages.
  724. */
  725. #ifdef CONFIG_PPC_64K_PAGES
  726. void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  727. {
  728. if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
  729. return;
  730. slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
  731. #ifdef CONFIG_SPU_BASE
  732. spu_flush_all_slbs(mm);
  733. #endif
  734. if (get_paca_psize(addr) != MMU_PAGE_4K) {
  735. get_paca()->context = mm->context;
  736. slb_flush_and_rebolt();
  737. }
  738. }
  739. #endif /* CONFIG_PPC_64K_PAGES */
  740. #ifdef CONFIG_PPC_SUBPAGE_PROT
  741. /*
  742. * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
  743. * Userspace sets the subpage permissions using the subpage_prot system call.
  744. *
  745. * Result is 0: full permissions, _PAGE_RW: read-only,
  746. * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
  747. */
  748. static int subpage_protection(struct mm_struct *mm, unsigned long ea)
  749. {
  750. struct subpage_prot_table *spt = &mm->context.spt;
  751. u32 spp = 0;
  752. u32 **sbpm, *sbpp;
  753. if (ea >= spt->maxaddr)
  754. return 0;
  755. if (ea < 0x100000000) {
  756. /* addresses below 4GB use spt->low_prot */
  757. sbpm = spt->low_prot;
  758. } else {
  759. sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
  760. if (!sbpm)
  761. return 0;
  762. }
  763. sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  764. if (!sbpp)
  765. return 0;
  766. spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
  767. /* extract 2-bit bitfield for this 4k subpage */
  768. spp >>= 30 - 2 * ((ea >> 12) & 0xf);
  769. /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
  770. spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
  771. return spp;
  772. }
  773. #else /* CONFIG_PPC_SUBPAGE_PROT */
  774. static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
  775. {
  776. return 0;
  777. }
  778. #endif
  779. void hash_failure_debug(unsigned long ea, unsigned long access,
  780. unsigned long vsid, unsigned long trap,
  781. int ssize, int psize, unsigned long pte)
  782. {
  783. if (!printk_ratelimit())
  784. return;
  785. pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
  786. ea, access, current->comm);
  787. pr_info(" trap=0x%lx vsid=0x%lx ssize=%d psize=%d pte=0x%lx\n",
  788. trap, vsid, ssize, psize, pte);
  789. }
  790. /* Result code is:
  791. * 0 - handled
  792. * 1 - normal page fault
  793. * -1 - critical hash insertion error
  794. * -2 - access not permitted by subpage protection mechanism
  795. */
  796. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  797. {
  798. pgd_t *pgdir;
  799. unsigned long vsid;
  800. struct mm_struct *mm;
  801. pte_t *ptep;
  802. unsigned hugeshift;
  803. const struct cpumask *tmp;
  804. int rc, user_region = 0, local = 0;
  805. int psize, ssize;
  806. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  807. ea, access, trap);
  808. if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
  809. DBG_LOW(" out of pgtable range !\n");
  810. return 1;
  811. }
  812. /* Get region & vsid */
  813. switch (REGION_ID(ea)) {
  814. case USER_REGION_ID:
  815. user_region = 1;
  816. mm = current->mm;
  817. if (! mm) {
  818. DBG_LOW(" user region with no mm !\n");
  819. return 1;
  820. }
  821. psize = get_slice_psize(mm, ea);
  822. ssize = user_segment_size(ea);
  823. vsid = get_vsid(mm->context.id, ea, ssize);
  824. break;
  825. case VMALLOC_REGION_ID:
  826. mm = &init_mm;
  827. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  828. if (ea < VMALLOC_END)
  829. psize = mmu_vmalloc_psize;
  830. else
  831. psize = mmu_io_psize;
  832. ssize = mmu_kernel_ssize;
  833. break;
  834. default:
  835. /* Not a valid range
  836. * Send the problem up to do_page_fault
  837. */
  838. return 1;
  839. }
  840. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  841. /* Get pgdir */
  842. pgdir = mm->pgd;
  843. if (pgdir == NULL)
  844. return 1;
  845. /* Check CPU locality */
  846. tmp = cpumask_of(smp_processor_id());
  847. if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
  848. local = 1;
  849. #ifndef CONFIG_PPC_64K_PAGES
  850. /* If we use 4K pages and our psize is not 4K, then we might
  851. * be hitting a special driver mapping, and need to align the
  852. * address before we fetch the PTE.
  853. *
  854. * It could also be a hugepage mapping, in which case this is
  855. * not necessary, but it's not harmful, either.
  856. */
  857. if (psize != MMU_PAGE_4K)
  858. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  859. #endif /* CONFIG_PPC_64K_PAGES */
  860. /* Get PTE and page size from page tables */
  861. ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugeshift);
  862. if (ptep == NULL || !pte_present(*ptep)) {
  863. DBG_LOW(" no PTE !\n");
  864. return 1;
  865. }
  866. /* Add _PAGE_PRESENT to the required access perm */
  867. access |= _PAGE_PRESENT;
  868. /* Pre-check access permissions (will be re-checked atomically
  869. * in __hash_page_XX but this pre-check is a fast path
  870. */
  871. if (access & ~pte_val(*ptep)) {
  872. DBG_LOW(" no access !\n");
  873. return 1;
  874. }
  875. #ifdef CONFIG_HUGETLB_PAGE
  876. if (hugeshift)
  877. return __hash_page_huge(ea, access, vsid, ptep, trap, local,
  878. ssize, hugeshift, psize);
  879. #endif /* CONFIG_HUGETLB_PAGE */
  880. #ifndef CONFIG_PPC_64K_PAGES
  881. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  882. #else
  883. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  884. pte_val(*(ptep + PTRS_PER_PTE)));
  885. #endif
  886. /* Do actual hashing */
  887. #ifdef CONFIG_PPC_64K_PAGES
  888. /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
  889. if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
  890. demote_segment_4k(mm, ea);
  891. psize = MMU_PAGE_4K;
  892. }
  893. /* If this PTE is non-cacheable and we have restrictions on
  894. * using non cacheable large pages, then we switch to 4k
  895. */
  896. if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
  897. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  898. if (user_region) {
  899. demote_segment_4k(mm, ea);
  900. psize = MMU_PAGE_4K;
  901. } else if (ea < VMALLOC_END) {
  902. /*
  903. * some driver did a non-cacheable mapping
  904. * in vmalloc space, so switch vmalloc
  905. * to 4k pages
  906. */
  907. printk(KERN_ALERT "Reducing vmalloc segment "
  908. "to 4kB pages because of "
  909. "non-cacheable mapping\n");
  910. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  911. #ifdef CONFIG_SPU_BASE
  912. spu_flush_all_slbs(mm);
  913. #endif
  914. }
  915. }
  916. if (user_region) {
  917. if (psize != get_paca_psize(ea)) {
  918. get_paca()->context = mm->context;
  919. slb_flush_and_rebolt();
  920. }
  921. } else if (get_paca()->vmalloc_sllp !=
  922. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  923. get_paca()->vmalloc_sllp =
  924. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  925. slb_vmalloc_update();
  926. }
  927. #endif /* CONFIG_PPC_64K_PAGES */
  928. #ifdef CONFIG_PPC_HAS_HASH_64K
  929. if (psize == MMU_PAGE_64K)
  930. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  931. else
  932. #endif /* CONFIG_PPC_HAS_HASH_64K */
  933. {
  934. int spp = subpage_protection(mm, ea);
  935. if (access & spp)
  936. rc = -2;
  937. else
  938. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  939. local, ssize, spp);
  940. }
  941. /* Dump some info in case of hash insertion failure, they should
  942. * never happen so it is really useful to know if/when they do
  943. */
  944. if (rc == -1)
  945. hash_failure_debug(ea, access, vsid, trap, ssize, psize,
  946. pte_val(*ptep));
  947. #ifndef CONFIG_PPC_64K_PAGES
  948. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  949. #else
  950. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  951. pte_val(*(ptep + PTRS_PER_PTE)));
  952. #endif
  953. DBG_LOW(" -> rc=%d\n", rc);
  954. return rc;
  955. }
  956. EXPORT_SYMBOL_GPL(hash_page);
  957. void hash_preload(struct mm_struct *mm, unsigned long ea,
  958. unsigned long access, unsigned long trap)
  959. {
  960. unsigned long vsid;
  961. pgd_t *pgdir;
  962. pte_t *ptep;
  963. unsigned long flags;
  964. int rc, ssize, local = 0;
  965. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  966. #ifdef CONFIG_PPC_MM_SLICES
  967. /* We only prefault standard pages for now */
  968. if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
  969. return;
  970. #endif
  971. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  972. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  973. /* Get Linux PTE if available */
  974. pgdir = mm->pgd;
  975. if (pgdir == NULL)
  976. return;
  977. ptep = find_linux_pte(pgdir, ea);
  978. if (!ptep)
  979. return;
  980. #ifdef CONFIG_PPC_64K_PAGES
  981. /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
  982. * a 64K kernel), then we don't preload, hash_page() will take
  983. * care of it once we actually try to access the page.
  984. * That way we don't have to duplicate all of the logic for segment
  985. * page size demotion here
  986. */
  987. if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
  988. return;
  989. #endif /* CONFIG_PPC_64K_PAGES */
  990. /* Get VSID */
  991. ssize = user_segment_size(ea);
  992. vsid = get_vsid(mm->context.id, ea, ssize);
  993. /* Hash doesn't like irqs */
  994. local_irq_save(flags);
  995. /* Is that local to this CPU ? */
  996. if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  997. local = 1;
  998. /* Hash it in */
  999. #ifdef CONFIG_PPC_HAS_HASH_64K
  1000. if (mm->context.user_psize == MMU_PAGE_64K)
  1001. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  1002. else
  1003. #endif /* CONFIG_PPC_HAS_HASH_64K */
  1004. rc = __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
  1005. subpage_protection(mm, ea));
  1006. /* Dump some info in case of hash insertion failure, they should
  1007. * never happen so it is really useful to know if/when they do
  1008. */
  1009. if (rc == -1)
  1010. hash_failure_debug(ea, access, vsid, trap, ssize,
  1011. mm->context.user_psize, pte_val(*ptep));
  1012. local_irq_restore(flags);
  1013. }
  1014. /* WARNING: This is called from hash_low_64.S, if you change this prototype,
  1015. * do not forget to update the assembly call site !
  1016. */
  1017. void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
  1018. int local)
  1019. {
  1020. unsigned long hash, index, shift, hidx, slot;
  1021. DBG_LOW("flush_hash_page(va=%016lx)\n", va);
  1022. pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
  1023. hash = hpt_hash(va, shift, ssize);
  1024. hidx = __rpte_to_hidx(pte, index);
  1025. if (hidx & _PTEIDX_SECONDARY)
  1026. hash = ~hash;
  1027. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1028. slot += hidx & _PTEIDX_GROUP_IX;
  1029. DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
  1030. ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
  1031. } pte_iterate_hashed_end();
  1032. }
  1033. void flush_hash_range(unsigned long number, int local)
  1034. {
  1035. if (ppc_md.flush_hash_range)
  1036. ppc_md.flush_hash_range(number, local);
  1037. else {
  1038. int i;
  1039. struct ppc64_tlb_batch *batch =
  1040. &__get_cpu_var(ppc64_tlb_batch);
  1041. for (i = 0; i < number; i++)
  1042. flush_hash_page(batch->vaddr[i], batch->pte[i],
  1043. batch->psize, batch->ssize, local);
  1044. }
  1045. }
  1046. /*
  1047. * low_hash_fault is called when we the low level hash code failed
  1048. * to instert a PTE due to an hypervisor error
  1049. */
  1050. void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
  1051. {
  1052. if (user_mode(regs)) {
  1053. #ifdef CONFIG_PPC_SUBPAGE_PROT
  1054. if (rc == -2)
  1055. _exception(SIGSEGV, regs, SEGV_ACCERR, address);
  1056. else
  1057. #endif
  1058. _exception(SIGBUS, regs, BUS_ADRERR, address);
  1059. } else
  1060. bad_page_fault(regs, address, SIGBUS);
  1061. }
  1062. #ifdef CONFIG_DEBUG_PAGEALLOC
  1063. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  1064. {
  1065. unsigned long hash, hpteg;
  1066. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1067. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  1068. unsigned long mode = htab_convert_pte_flags(PAGE_KERNEL);
  1069. int ret;
  1070. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  1071. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  1072. ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
  1073. mode, HPTE_V_BOLTED,
  1074. mmu_linear_psize, mmu_kernel_ssize);
  1075. BUG_ON (ret < 0);
  1076. spin_lock(&linear_map_hash_lock);
  1077. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  1078. linear_map_hash_slots[lmi] = ret | 0x80;
  1079. spin_unlock(&linear_map_hash_lock);
  1080. }
  1081. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  1082. {
  1083. unsigned long hash, hidx, slot;
  1084. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1085. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  1086. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  1087. spin_lock(&linear_map_hash_lock);
  1088. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  1089. hidx = linear_map_hash_slots[lmi] & 0x7f;
  1090. linear_map_hash_slots[lmi] = 0;
  1091. spin_unlock(&linear_map_hash_lock);
  1092. if (hidx & _PTEIDX_SECONDARY)
  1093. hash = ~hash;
  1094. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1095. slot += hidx & _PTEIDX_GROUP_IX;
  1096. ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
  1097. }
  1098. void kernel_map_pages(struct page *page, int numpages, int enable)
  1099. {
  1100. unsigned long flags, vaddr, lmi;
  1101. int i;
  1102. local_irq_save(flags);
  1103. for (i = 0; i < numpages; i++, page++) {
  1104. vaddr = (unsigned long)page_address(page);
  1105. lmi = __pa(vaddr) >> PAGE_SHIFT;
  1106. if (lmi >= linear_map_hash_count)
  1107. continue;
  1108. if (enable)
  1109. kernel_map_linear_page(vaddr, lmi);
  1110. else
  1111. kernel_unmap_linear_page(vaddr, lmi);
  1112. }
  1113. local_irq_restore(flags);
  1114. }
  1115. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1116. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  1117. phys_addr_t first_memblock_size)
  1118. {
  1119. /* We don't currently support the first MEMBLOCK not mapping 0
  1120. * physical on those processors
  1121. */
  1122. BUG_ON(first_memblock_base != 0);
  1123. /* On LPAR systems, the first entry is our RMA region,
  1124. * non-LPAR 64-bit hash MMU systems don't have a limitation
  1125. * on real mode access, but using the first entry works well
  1126. * enough. We also clamp it to 1G to avoid some funky things
  1127. * such as RTAS bugs etc...
  1128. */
  1129. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  1130. /* Finally limit subsequent allocations */
  1131. memblock_set_current_limit(ppc64_rma_size);
  1132. }