hash_utils_64.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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/lmb.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. #ifdef DEBUG
  54. #define DBG(fmt...) udbg_printf(fmt)
  55. #else
  56. #define DBG(fmt...)
  57. #endif
  58. #ifdef DEBUG_LOW
  59. #define DBG_LOW(fmt...) udbg_printf(fmt)
  60. #else
  61. #define DBG_LOW(fmt...)
  62. #endif
  63. #define KB (1024)
  64. #define MB (1024*KB)
  65. /*
  66. * Note: pte --> Linux PTE
  67. * HPTE --> PowerPC Hashed Page Table Entry
  68. *
  69. * Execution context:
  70. * htab_initialize is called with the MMU off (of course), but
  71. * the kernel has been copied down to zero so it can directly
  72. * reference global data. At this point it is very difficult
  73. * to print debug info.
  74. *
  75. */
  76. #ifdef CONFIG_U3_DART
  77. extern unsigned long dart_tablebase;
  78. #endif /* CONFIG_U3_DART */
  79. static unsigned long _SDR1;
  80. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
  81. struct hash_pte *htab_address;
  82. unsigned long htab_size_bytes;
  83. unsigned long htab_hash_mask;
  84. int mmu_linear_psize = MMU_PAGE_4K;
  85. int mmu_virtual_psize = MMU_PAGE_4K;
  86. int mmu_vmalloc_psize = MMU_PAGE_4K;
  87. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  88. int mmu_vmemmap_psize = MMU_PAGE_4K;
  89. #endif
  90. int mmu_io_psize = MMU_PAGE_4K;
  91. int mmu_kernel_ssize = MMU_SEGSIZE_256M;
  92. int mmu_highuser_ssize = MMU_SEGSIZE_256M;
  93. u16 mmu_slb_size = 64;
  94. #ifdef CONFIG_HUGETLB_PAGE
  95. int mmu_huge_psize = MMU_PAGE_16M;
  96. unsigned int HPAGE_SHIFT;
  97. #endif
  98. #ifdef CONFIG_PPC_64K_PAGES
  99. int mmu_ci_restrictions;
  100. #endif
  101. #ifdef CONFIG_DEBUG_PAGEALLOC
  102. static u8 *linear_map_hash_slots;
  103. static unsigned long linear_map_hash_count;
  104. static DEFINE_SPINLOCK(linear_map_hash_lock);
  105. #endif /* CONFIG_DEBUG_PAGEALLOC */
  106. /* There are definitions of page sizes arrays to be used when none
  107. * is provided by the firmware.
  108. */
  109. /* Pre-POWER4 CPUs (4k pages only)
  110. */
  111. static struct mmu_psize_def mmu_psize_defaults_old[] = {
  112. [MMU_PAGE_4K] = {
  113. .shift = 12,
  114. .sllp = 0,
  115. .penc = 0,
  116. .avpnm = 0,
  117. .tlbiel = 0,
  118. },
  119. };
  120. /* POWER4, GPUL, POWER5
  121. *
  122. * Support for 16Mb large pages
  123. */
  124. static struct mmu_psize_def mmu_psize_defaults_gp[] = {
  125. [MMU_PAGE_4K] = {
  126. .shift = 12,
  127. .sllp = 0,
  128. .penc = 0,
  129. .avpnm = 0,
  130. .tlbiel = 1,
  131. },
  132. [MMU_PAGE_16M] = {
  133. .shift = 24,
  134. .sllp = SLB_VSID_L,
  135. .penc = 0,
  136. .avpnm = 0x1UL,
  137. .tlbiel = 0,
  138. },
  139. };
  140. int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
  141. unsigned long pstart, unsigned long mode,
  142. int psize, int ssize)
  143. {
  144. unsigned long vaddr, paddr;
  145. unsigned int step, shift;
  146. unsigned long tmp_mode;
  147. int ret = 0;
  148. shift = mmu_psize_defs[psize].shift;
  149. step = 1 << shift;
  150. for (vaddr = vstart, paddr = pstart; vaddr < vend;
  151. vaddr += step, paddr += step) {
  152. unsigned long hash, hpteg;
  153. unsigned long vsid = get_kernel_vsid(vaddr, ssize);
  154. unsigned long va = hpt_va(vaddr, vsid, ssize);
  155. tmp_mode = mode;
  156. /* Make non-kernel text non-executable */
  157. if (!in_kernel_text(vaddr))
  158. tmp_mode = mode | HPTE_R_N;
  159. hash = hpt_hash(va, shift, ssize);
  160. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  161. DBG("htab_bolt_mapping: calling %p\n", ppc_md.hpte_insert);
  162. BUG_ON(!ppc_md.hpte_insert);
  163. ret = ppc_md.hpte_insert(hpteg, va, paddr,
  164. tmp_mode, HPTE_V_BOLTED, psize, ssize);
  165. if (ret < 0)
  166. break;
  167. #ifdef CONFIG_DEBUG_PAGEALLOC
  168. if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
  169. linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
  170. #endif /* CONFIG_DEBUG_PAGEALLOC */
  171. }
  172. return ret < 0 ? ret : 0;
  173. }
  174. #ifdef CONFIG_MEMORY_HOTPLUG
  175. static int htab_remove_mapping(unsigned long vstart, unsigned long vend,
  176. int psize, int ssize)
  177. {
  178. unsigned long vaddr;
  179. unsigned int step, shift;
  180. shift = mmu_psize_defs[psize].shift;
  181. step = 1 << shift;
  182. if (!ppc_md.hpte_removebolted) {
  183. printk(KERN_WARNING "Platform doesn't implement "
  184. "hpte_removebolted\n");
  185. return -EINVAL;
  186. }
  187. for (vaddr = vstart; vaddr < vend; vaddr += step)
  188. ppc_md.hpte_removebolted(vaddr, psize, ssize);
  189. return 0;
  190. }
  191. #endif /* CONFIG_MEMORY_HOTPLUG */
  192. static int __init htab_dt_scan_seg_sizes(unsigned long node,
  193. const char *uname, int depth,
  194. void *data)
  195. {
  196. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  197. u32 *prop;
  198. unsigned long size = 0;
  199. /* We are scanning "cpu" nodes only */
  200. if (type == NULL || strcmp(type, "cpu") != 0)
  201. return 0;
  202. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
  203. &size);
  204. if (prop == NULL)
  205. return 0;
  206. for (; size >= 4; size -= 4, ++prop) {
  207. if (prop[0] == 40) {
  208. DBG("1T segment support detected\n");
  209. cur_cpu_spec->cpu_features |= CPU_FTR_1T_SEGMENT;
  210. return 1;
  211. }
  212. }
  213. cur_cpu_spec->cpu_features &= ~CPU_FTR_NO_SLBIE_B;
  214. return 0;
  215. }
  216. static void __init htab_init_seg_sizes(void)
  217. {
  218. of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
  219. }
  220. static int __init htab_dt_scan_page_sizes(unsigned long node,
  221. const char *uname, int depth,
  222. void *data)
  223. {
  224. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  225. u32 *prop;
  226. unsigned long size = 0;
  227. /* We are scanning "cpu" nodes only */
  228. if (type == NULL || strcmp(type, "cpu") != 0)
  229. return 0;
  230. prop = (u32 *)of_get_flat_dt_prop(node,
  231. "ibm,segment-page-sizes", &size);
  232. if (prop != NULL) {
  233. DBG("Page sizes from device-tree:\n");
  234. size /= 4;
  235. cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
  236. while(size > 0) {
  237. unsigned int shift = prop[0];
  238. unsigned int slbenc = prop[1];
  239. unsigned int lpnum = prop[2];
  240. unsigned int lpenc = 0;
  241. struct mmu_psize_def *def;
  242. int idx = -1;
  243. size -= 3; prop += 3;
  244. while(size > 0 && lpnum) {
  245. if (prop[0] == shift)
  246. lpenc = prop[1];
  247. prop += 2; size -= 2;
  248. lpnum--;
  249. }
  250. switch(shift) {
  251. case 0xc:
  252. idx = MMU_PAGE_4K;
  253. break;
  254. case 0x10:
  255. idx = MMU_PAGE_64K;
  256. break;
  257. case 0x14:
  258. idx = MMU_PAGE_1M;
  259. break;
  260. case 0x18:
  261. idx = MMU_PAGE_16M;
  262. cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
  263. break;
  264. case 0x22:
  265. idx = MMU_PAGE_16G;
  266. break;
  267. }
  268. if (idx < 0)
  269. continue;
  270. def = &mmu_psize_defs[idx];
  271. def->shift = shift;
  272. if (shift <= 23)
  273. def->avpnm = 0;
  274. else
  275. def->avpnm = (1 << (shift - 23)) - 1;
  276. def->sllp = slbenc;
  277. def->penc = lpenc;
  278. /* We don't know for sure what's up with tlbiel, so
  279. * for now we only set it for 4K and 64K pages
  280. */
  281. if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
  282. def->tlbiel = 1;
  283. else
  284. def->tlbiel = 0;
  285. DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
  286. "tlbiel=%d, penc=%d\n",
  287. idx, shift, def->sllp, def->avpnm, def->tlbiel,
  288. def->penc);
  289. }
  290. return 1;
  291. }
  292. return 0;
  293. }
  294. static void __init htab_init_page_sizes(void)
  295. {
  296. int rc;
  297. /* Default to 4K pages only */
  298. memcpy(mmu_psize_defs, mmu_psize_defaults_old,
  299. sizeof(mmu_psize_defaults_old));
  300. /*
  301. * Try to find the available page sizes in the device-tree
  302. */
  303. rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
  304. if (rc != 0) /* Found */
  305. goto found;
  306. /*
  307. * Not in the device-tree, let's fallback on known size
  308. * list for 16M capable GP & GR
  309. */
  310. if (cpu_has_feature(CPU_FTR_16M_PAGE))
  311. memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
  312. sizeof(mmu_psize_defaults_gp));
  313. found:
  314. #ifndef CONFIG_DEBUG_PAGEALLOC
  315. /*
  316. * Pick a size for the linear mapping. Currently, we only support
  317. * 16M, 1M and 4K which is the default
  318. */
  319. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  320. mmu_linear_psize = MMU_PAGE_16M;
  321. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  322. mmu_linear_psize = MMU_PAGE_1M;
  323. #endif /* CONFIG_DEBUG_PAGEALLOC */
  324. #ifdef CONFIG_PPC_64K_PAGES
  325. /*
  326. * Pick a size for the ordinary pages. Default is 4K, we support
  327. * 64K for user mappings and vmalloc if supported by the processor.
  328. * We only use 64k for ioremap if the processor
  329. * (and firmware) support cache-inhibited large pages.
  330. * If not, we use 4k and set mmu_ci_restrictions so that
  331. * hash_page knows to switch processes that use cache-inhibited
  332. * mappings to 4k pages.
  333. */
  334. if (mmu_psize_defs[MMU_PAGE_64K].shift) {
  335. mmu_virtual_psize = MMU_PAGE_64K;
  336. mmu_vmalloc_psize = MMU_PAGE_64K;
  337. if (mmu_linear_psize == MMU_PAGE_4K)
  338. mmu_linear_psize = MMU_PAGE_64K;
  339. if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE)) {
  340. /*
  341. * Don't use 64k pages for ioremap on pSeries, since
  342. * that would stop us accessing the HEA ethernet.
  343. */
  344. if (!machine_is(pseries))
  345. mmu_io_psize = MMU_PAGE_64K;
  346. } else
  347. mmu_ci_restrictions = 1;
  348. }
  349. #endif /* CONFIG_PPC_64K_PAGES */
  350. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  351. /* We try to use 16M pages for vmemmap if that is supported
  352. * and we have at least 1G of RAM at boot
  353. */
  354. if (mmu_psize_defs[MMU_PAGE_16M].shift &&
  355. lmb_phys_mem_size() >= 0x40000000)
  356. mmu_vmemmap_psize = MMU_PAGE_16M;
  357. else if (mmu_psize_defs[MMU_PAGE_64K].shift)
  358. mmu_vmemmap_psize = MMU_PAGE_64K;
  359. else
  360. mmu_vmemmap_psize = MMU_PAGE_4K;
  361. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  362. printk(KERN_DEBUG "Page orders: linear mapping = %d, "
  363. "virtual = %d, io = %d"
  364. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  365. ", vmemmap = %d"
  366. #endif
  367. "\n",
  368. mmu_psize_defs[mmu_linear_psize].shift,
  369. mmu_psize_defs[mmu_virtual_psize].shift,
  370. mmu_psize_defs[mmu_io_psize].shift
  371. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  372. ,mmu_psize_defs[mmu_vmemmap_psize].shift
  373. #endif
  374. );
  375. #ifdef CONFIG_HUGETLB_PAGE
  376. /* Init large page size. Currently, we pick 16M or 1M depending
  377. * on what is available
  378. */
  379. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  380. set_huge_psize(MMU_PAGE_16M);
  381. /* With 4k/4level pagetables, we can't (for now) cope with a
  382. * huge page size < PMD_SIZE */
  383. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  384. set_huge_psize(MMU_PAGE_1M);
  385. #endif /* CONFIG_HUGETLB_PAGE */
  386. }
  387. static int __init htab_dt_scan_pftsize(unsigned long node,
  388. const char *uname, int depth,
  389. void *data)
  390. {
  391. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  392. u32 *prop;
  393. /* We are scanning "cpu" nodes only */
  394. if (type == NULL || strcmp(type, "cpu") != 0)
  395. return 0;
  396. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
  397. if (prop != NULL) {
  398. /* pft_size[0] is the NUMA CEC cookie */
  399. ppc64_pft_size = prop[1];
  400. return 1;
  401. }
  402. return 0;
  403. }
  404. static unsigned long __init htab_get_table_size(void)
  405. {
  406. unsigned long mem_size, rnd_mem_size, pteg_count;
  407. /* If hash size isn't already provided by the platform, we try to
  408. * retrieve it from the device-tree. If it's not there neither, we
  409. * calculate it now based on the total RAM size
  410. */
  411. if (ppc64_pft_size == 0)
  412. of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
  413. if (ppc64_pft_size)
  414. return 1UL << ppc64_pft_size;
  415. /* round mem_size up to next power of 2 */
  416. mem_size = lmb_phys_mem_size();
  417. rnd_mem_size = 1UL << __ilog2(mem_size);
  418. if (rnd_mem_size < mem_size)
  419. rnd_mem_size <<= 1;
  420. /* # pages / 2 */
  421. pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
  422. return pteg_count << 7;
  423. }
  424. #ifdef CONFIG_MEMORY_HOTPLUG
  425. void create_section_mapping(unsigned long start, unsigned long end)
  426. {
  427. BUG_ON(htab_bolt_mapping(start, end, __pa(start),
  428. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
  429. mmu_linear_psize, mmu_kernel_ssize));
  430. }
  431. int remove_section_mapping(unsigned long start, unsigned long end)
  432. {
  433. return htab_remove_mapping(start, end, mmu_linear_psize,
  434. mmu_kernel_ssize);
  435. }
  436. #endif /* CONFIG_MEMORY_HOTPLUG */
  437. static inline void make_bl(unsigned int *insn_addr, void *func)
  438. {
  439. unsigned long funcp = *((unsigned long *)func);
  440. int offset = funcp - (unsigned long)insn_addr;
  441. *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
  442. flush_icache_range((unsigned long)insn_addr, 4+
  443. (unsigned long)insn_addr);
  444. }
  445. static void __init htab_finish_init(void)
  446. {
  447. extern unsigned int *htab_call_hpte_insert1;
  448. extern unsigned int *htab_call_hpte_insert2;
  449. extern unsigned int *htab_call_hpte_remove;
  450. extern unsigned int *htab_call_hpte_updatepp;
  451. #ifdef CONFIG_PPC_HAS_HASH_64K
  452. extern unsigned int *ht64_call_hpte_insert1;
  453. extern unsigned int *ht64_call_hpte_insert2;
  454. extern unsigned int *ht64_call_hpte_remove;
  455. extern unsigned int *ht64_call_hpte_updatepp;
  456. make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
  457. make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
  458. make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
  459. make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
  460. #endif /* CONFIG_PPC_HAS_HASH_64K */
  461. make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
  462. make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
  463. make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
  464. make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
  465. }
  466. void __init htab_initialize(void)
  467. {
  468. unsigned long table;
  469. unsigned long pteg_count;
  470. unsigned long mode_rw;
  471. unsigned long base = 0, size = 0, limit;
  472. int i;
  473. DBG(" -> htab_initialize()\n");
  474. /* Initialize segment sizes */
  475. htab_init_seg_sizes();
  476. /* Initialize page sizes */
  477. htab_init_page_sizes();
  478. if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
  479. mmu_kernel_ssize = MMU_SEGSIZE_1T;
  480. mmu_highuser_ssize = MMU_SEGSIZE_1T;
  481. printk(KERN_INFO "Using 1TB segments\n");
  482. }
  483. /*
  484. * Calculate the required size of the htab. We want the number of
  485. * PTEGs to equal one half the number of real pages.
  486. */
  487. htab_size_bytes = htab_get_table_size();
  488. pteg_count = htab_size_bytes >> 7;
  489. htab_hash_mask = pteg_count - 1;
  490. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  491. /* Using a hypervisor which owns the htab */
  492. htab_address = NULL;
  493. _SDR1 = 0;
  494. } else {
  495. /* Find storage for the HPT. Must be contiguous in
  496. * the absolute address space. On cell we want it to be
  497. * in the first 2 Gig so we can use it for IOMMU hacks.
  498. */
  499. if (machine_is(cell))
  500. limit = 0x80000000;
  501. else
  502. limit = 0;
  503. table = lmb_alloc_base(htab_size_bytes, htab_size_bytes, limit);
  504. DBG("Hash table allocated at %lx, size: %lx\n", table,
  505. htab_size_bytes);
  506. htab_address = abs_to_virt(table);
  507. /* htab absolute addr + encoded htabsize */
  508. _SDR1 = table + __ilog2(pteg_count) - 11;
  509. /* Initialize the HPT with no entries */
  510. memset((void *)table, 0, htab_size_bytes);
  511. /* Set SDR1 */
  512. mtspr(SPRN_SDR1, _SDR1);
  513. }
  514. mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
  515. #ifdef CONFIG_DEBUG_PAGEALLOC
  516. linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
  517. linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
  518. 1, lmb.rmo_size));
  519. memset(linear_map_hash_slots, 0, linear_map_hash_count);
  520. #endif /* CONFIG_DEBUG_PAGEALLOC */
  521. /* On U3 based machines, we need to reserve the DART area and
  522. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  523. * cacheable later on
  524. */
  525. /* create bolted the linear mapping in the hash table */
  526. for (i=0; i < lmb.memory.cnt; i++) {
  527. base = (unsigned long)__va(lmb.memory.region[i].base);
  528. size = lmb.memory.region[i].size;
  529. DBG("creating mapping for region: %lx : %lx\n", base, size);
  530. #ifdef CONFIG_U3_DART
  531. /* Do not map the DART space. Fortunately, it will be aligned
  532. * in such a way that it will not cross two lmb regions and
  533. * will fit within a single 16Mb page.
  534. * The DART space is assumed to be a full 16Mb region even if
  535. * we only use 2Mb of that space. We will use more of it later
  536. * for AGP GART. We have to use a full 16Mb large page.
  537. */
  538. DBG("DART base: %lx\n", dart_tablebase);
  539. if (dart_tablebase != 0 && dart_tablebase >= base
  540. && dart_tablebase < (base + size)) {
  541. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  542. if (base != dart_tablebase)
  543. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  544. __pa(base), mode_rw,
  545. mmu_linear_psize,
  546. mmu_kernel_ssize));
  547. if ((base + size) > dart_table_end)
  548. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  549. base + size,
  550. __pa(dart_table_end),
  551. mode_rw,
  552. mmu_linear_psize,
  553. mmu_kernel_ssize));
  554. continue;
  555. }
  556. #endif /* CONFIG_U3_DART */
  557. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  558. mode_rw, mmu_linear_psize, mmu_kernel_ssize));
  559. }
  560. /*
  561. * If we have a memory_limit and we've allocated TCEs then we need to
  562. * explicitly map the TCE area at the top of RAM. We also cope with the
  563. * case that the TCEs start below memory_limit.
  564. * tce_alloc_start/end are 16MB aligned so the mapping should work
  565. * for either 4K or 16MB pages.
  566. */
  567. if (tce_alloc_start) {
  568. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  569. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  570. if (base + size >= tce_alloc_start)
  571. tce_alloc_start = base + size + 1;
  572. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  573. __pa(tce_alloc_start), mode_rw,
  574. mmu_linear_psize, mmu_kernel_ssize));
  575. }
  576. htab_finish_init();
  577. DBG(" <- htab_initialize()\n");
  578. }
  579. #undef KB
  580. #undef MB
  581. void htab_initialize_secondary(void)
  582. {
  583. if (!firmware_has_feature(FW_FEATURE_LPAR))
  584. mtspr(SPRN_SDR1, _SDR1);
  585. }
  586. /*
  587. * Called by asm hashtable.S for doing lazy icache flush
  588. */
  589. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  590. {
  591. struct page *page;
  592. if (!pfn_valid(pte_pfn(pte)))
  593. return pp;
  594. page = pte_page(pte);
  595. /* page is dirty */
  596. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  597. if (trap == 0x400) {
  598. __flush_dcache_icache(page_address(page));
  599. set_bit(PG_arch_1, &page->flags);
  600. } else
  601. pp |= HPTE_R_N;
  602. }
  603. return pp;
  604. }
  605. #ifdef CONFIG_PPC_MM_SLICES
  606. unsigned int get_paca_psize(unsigned long addr)
  607. {
  608. unsigned long index, slices;
  609. if (addr < SLICE_LOW_TOP) {
  610. slices = get_paca()->context.low_slices_psize;
  611. index = GET_LOW_SLICE_INDEX(addr);
  612. } else {
  613. slices = get_paca()->context.high_slices_psize;
  614. index = GET_HIGH_SLICE_INDEX(addr);
  615. }
  616. return (slices >> (index * 4)) & 0xF;
  617. }
  618. #else
  619. unsigned int get_paca_psize(unsigned long addr)
  620. {
  621. return get_paca()->context.user_psize;
  622. }
  623. #endif
  624. /*
  625. * Demote a segment to using 4k pages.
  626. * For now this makes the whole process use 4k pages.
  627. */
  628. #ifdef CONFIG_PPC_64K_PAGES
  629. void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  630. {
  631. if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
  632. return;
  633. slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
  634. #ifdef CONFIG_SPU_BASE
  635. spu_flush_all_slbs(mm);
  636. #endif
  637. if (get_paca_psize(addr) != MMU_PAGE_4K) {
  638. get_paca()->context = mm->context;
  639. slb_flush_and_rebolt();
  640. }
  641. }
  642. #endif /* CONFIG_PPC_64K_PAGES */
  643. #ifdef CONFIG_PPC_SUBPAGE_PROT
  644. /*
  645. * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
  646. * Userspace sets the subpage permissions using the subpage_prot system call.
  647. *
  648. * Result is 0: full permissions, _PAGE_RW: read-only,
  649. * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
  650. */
  651. static int subpage_protection(pgd_t *pgdir, unsigned long ea)
  652. {
  653. struct subpage_prot_table *spt = pgd_subpage_prot(pgdir);
  654. u32 spp = 0;
  655. u32 **sbpm, *sbpp;
  656. if (ea >= spt->maxaddr)
  657. return 0;
  658. if (ea < 0x100000000) {
  659. /* addresses below 4GB use spt->low_prot */
  660. sbpm = spt->low_prot;
  661. } else {
  662. sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
  663. if (!sbpm)
  664. return 0;
  665. }
  666. sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  667. if (!sbpp)
  668. return 0;
  669. spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
  670. /* extract 2-bit bitfield for this 4k subpage */
  671. spp >>= 30 - 2 * ((ea >> 12) & 0xf);
  672. /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
  673. spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
  674. return spp;
  675. }
  676. #else /* CONFIG_PPC_SUBPAGE_PROT */
  677. static inline int subpage_protection(pgd_t *pgdir, unsigned long ea)
  678. {
  679. return 0;
  680. }
  681. #endif
  682. /* Result code is:
  683. * 0 - handled
  684. * 1 - normal page fault
  685. * -1 - critical hash insertion error
  686. * -2 - access not permitted by subpage protection mechanism
  687. */
  688. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  689. {
  690. void *pgdir;
  691. unsigned long vsid;
  692. struct mm_struct *mm;
  693. pte_t *ptep;
  694. cpumask_t tmp;
  695. int rc, user_region = 0, local = 0;
  696. int psize, ssize;
  697. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  698. ea, access, trap);
  699. if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
  700. DBG_LOW(" out of pgtable range !\n");
  701. return 1;
  702. }
  703. /* Get region & vsid */
  704. switch (REGION_ID(ea)) {
  705. case USER_REGION_ID:
  706. user_region = 1;
  707. mm = current->mm;
  708. if (! mm) {
  709. DBG_LOW(" user region with no mm !\n");
  710. return 1;
  711. }
  712. psize = get_slice_psize(mm, ea);
  713. ssize = user_segment_size(ea);
  714. vsid = get_vsid(mm->context.id, ea, ssize);
  715. break;
  716. case VMALLOC_REGION_ID:
  717. mm = &init_mm;
  718. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  719. if (ea < VMALLOC_END)
  720. psize = mmu_vmalloc_psize;
  721. else
  722. psize = mmu_io_psize;
  723. ssize = mmu_kernel_ssize;
  724. break;
  725. default:
  726. /* Not a valid range
  727. * Send the problem up to do_page_fault
  728. */
  729. return 1;
  730. }
  731. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  732. /* Get pgdir */
  733. pgdir = mm->pgd;
  734. if (pgdir == NULL)
  735. return 1;
  736. /* Check CPU locality */
  737. tmp = cpumask_of_cpu(smp_processor_id());
  738. if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
  739. local = 1;
  740. #ifdef CONFIG_HUGETLB_PAGE
  741. /* Handle hugepage regions */
  742. if (HPAGE_SHIFT && psize == mmu_huge_psize) {
  743. DBG_LOW(" -> huge page !\n");
  744. return hash_huge_page(mm, access, ea, vsid, local, trap);
  745. }
  746. #endif /* CONFIG_HUGETLB_PAGE */
  747. #ifndef CONFIG_PPC_64K_PAGES
  748. /* If we use 4K pages and our psize is not 4K, then we are hitting
  749. * a special driver mapping, we need to align the address before
  750. * we fetch the PTE
  751. */
  752. if (psize != MMU_PAGE_4K)
  753. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  754. #endif /* CONFIG_PPC_64K_PAGES */
  755. /* Get PTE and page size from page tables */
  756. ptep = find_linux_pte(pgdir, ea);
  757. if (ptep == NULL || !pte_present(*ptep)) {
  758. DBG_LOW(" no PTE !\n");
  759. return 1;
  760. }
  761. #ifndef CONFIG_PPC_64K_PAGES
  762. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  763. #else
  764. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  765. pte_val(*(ptep + PTRS_PER_PTE)));
  766. #endif
  767. /* Pre-check access permissions (will be re-checked atomically
  768. * in __hash_page_XX but this pre-check is a fast path
  769. */
  770. if (access & ~pte_val(*ptep)) {
  771. DBG_LOW(" no access !\n");
  772. return 1;
  773. }
  774. /* Do actual hashing */
  775. #ifdef CONFIG_PPC_64K_PAGES
  776. /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
  777. if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
  778. demote_segment_4k(mm, ea);
  779. psize = MMU_PAGE_4K;
  780. }
  781. /* If this PTE is non-cacheable and we have restrictions on
  782. * using non cacheable large pages, then we switch to 4k
  783. */
  784. if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
  785. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  786. if (user_region) {
  787. demote_segment_4k(mm, ea);
  788. psize = MMU_PAGE_4K;
  789. } else if (ea < VMALLOC_END) {
  790. /*
  791. * some driver did a non-cacheable mapping
  792. * in vmalloc space, so switch vmalloc
  793. * to 4k pages
  794. */
  795. printk(KERN_ALERT "Reducing vmalloc segment "
  796. "to 4kB pages because of "
  797. "non-cacheable mapping\n");
  798. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  799. #ifdef CONFIG_SPU_BASE
  800. spu_flush_all_slbs(mm);
  801. #endif
  802. }
  803. }
  804. if (user_region) {
  805. if (psize != get_paca_psize(ea)) {
  806. get_paca()->context = mm->context;
  807. slb_flush_and_rebolt();
  808. }
  809. } else if (get_paca()->vmalloc_sllp !=
  810. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  811. get_paca()->vmalloc_sllp =
  812. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  813. slb_vmalloc_update();
  814. }
  815. #endif /* CONFIG_PPC_64K_PAGES */
  816. #ifdef CONFIG_PPC_HAS_HASH_64K
  817. if (psize == MMU_PAGE_64K)
  818. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  819. else
  820. #endif /* CONFIG_PPC_HAS_HASH_64K */
  821. {
  822. int spp = subpage_protection(pgdir, ea);
  823. if (access & spp)
  824. rc = -2;
  825. else
  826. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  827. local, ssize, spp);
  828. }
  829. #ifndef CONFIG_PPC_64K_PAGES
  830. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  831. #else
  832. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  833. pte_val(*(ptep + PTRS_PER_PTE)));
  834. #endif
  835. DBG_LOW(" -> rc=%d\n", rc);
  836. return rc;
  837. }
  838. EXPORT_SYMBOL_GPL(hash_page);
  839. void hash_preload(struct mm_struct *mm, unsigned long ea,
  840. unsigned long access, unsigned long trap)
  841. {
  842. unsigned long vsid;
  843. void *pgdir;
  844. pte_t *ptep;
  845. cpumask_t mask;
  846. unsigned long flags;
  847. int local = 0;
  848. int ssize;
  849. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  850. #ifdef CONFIG_PPC_MM_SLICES
  851. /* We only prefault standard pages for now */
  852. if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
  853. return;
  854. #endif
  855. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  856. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  857. /* Get Linux PTE if available */
  858. pgdir = mm->pgd;
  859. if (pgdir == NULL)
  860. return;
  861. ptep = find_linux_pte(pgdir, ea);
  862. if (!ptep)
  863. return;
  864. #ifdef CONFIG_PPC_64K_PAGES
  865. /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
  866. * a 64K kernel), then we don't preload, hash_page() will take
  867. * care of it once we actually try to access the page.
  868. * That way we don't have to duplicate all of the logic for segment
  869. * page size demotion here
  870. */
  871. if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
  872. return;
  873. #endif /* CONFIG_PPC_64K_PAGES */
  874. /* Get VSID */
  875. ssize = user_segment_size(ea);
  876. vsid = get_vsid(mm->context.id, ea, ssize);
  877. /* Hash doesn't like irqs */
  878. local_irq_save(flags);
  879. /* Is that local to this CPU ? */
  880. mask = cpumask_of_cpu(smp_processor_id());
  881. if (cpus_equal(mm->cpu_vm_mask, mask))
  882. local = 1;
  883. /* Hash it in */
  884. #ifdef CONFIG_PPC_HAS_HASH_64K
  885. if (mm->context.user_psize == MMU_PAGE_64K)
  886. __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  887. else
  888. #endif /* CONFIG_PPC_HAS_HASH_64K */
  889. __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
  890. subpage_protection(pgdir, ea));
  891. local_irq_restore(flags);
  892. }
  893. /* WARNING: This is called from hash_low_64.S, if you change this prototype,
  894. * do not forget to update the assembly call site !
  895. */
  896. void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
  897. int local)
  898. {
  899. unsigned long hash, index, shift, hidx, slot;
  900. DBG_LOW("flush_hash_page(va=%016x)\n", va);
  901. pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
  902. hash = hpt_hash(va, shift, ssize);
  903. hidx = __rpte_to_hidx(pte, index);
  904. if (hidx & _PTEIDX_SECONDARY)
  905. hash = ~hash;
  906. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  907. slot += hidx & _PTEIDX_GROUP_IX;
  908. DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
  909. ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
  910. } pte_iterate_hashed_end();
  911. }
  912. void flush_hash_range(unsigned long number, int local)
  913. {
  914. if (ppc_md.flush_hash_range)
  915. ppc_md.flush_hash_range(number, local);
  916. else {
  917. int i;
  918. struct ppc64_tlb_batch *batch =
  919. &__get_cpu_var(ppc64_tlb_batch);
  920. for (i = 0; i < number; i++)
  921. flush_hash_page(batch->vaddr[i], batch->pte[i],
  922. batch->psize, batch->ssize, local);
  923. }
  924. }
  925. /*
  926. * low_hash_fault is called when we the low level hash code failed
  927. * to instert a PTE due to an hypervisor error
  928. */
  929. void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
  930. {
  931. if (user_mode(regs)) {
  932. #ifdef CONFIG_PPC_SUBPAGE_PROT
  933. if (rc == -2)
  934. _exception(SIGSEGV, regs, SEGV_ACCERR, address);
  935. else
  936. #endif
  937. _exception(SIGBUS, regs, BUS_ADRERR, address);
  938. } else
  939. bad_page_fault(regs, address, SIGBUS);
  940. }
  941. #ifdef CONFIG_DEBUG_PAGEALLOC
  942. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  943. {
  944. unsigned long hash, hpteg;
  945. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  946. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  947. unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
  948. _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
  949. int ret;
  950. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  951. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  952. ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
  953. mode, HPTE_V_BOLTED,
  954. mmu_linear_psize, mmu_kernel_ssize);
  955. BUG_ON (ret < 0);
  956. spin_lock(&linear_map_hash_lock);
  957. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  958. linear_map_hash_slots[lmi] = ret | 0x80;
  959. spin_unlock(&linear_map_hash_lock);
  960. }
  961. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  962. {
  963. unsigned long hash, hidx, slot;
  964. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  965. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  966. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  967. spin_lock(&linear_map_hash_lock);
  968. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  969. hidx = linear_map_hash_slots[lmi] & 0x7f;
  970. linear_map_hash_slots[lmi] = 0;
  971. spin_unlock(&linear_map_hash_lock);
  972. if (hidx & _PTEIDX_SECONDARY)
  973. hash = ~hash;
  974. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  975. slot += hidx & _PTEIDX_GROUP_IX;
  976. ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
  977. }
  978. void kernel_map_pages(struct page *page, int numpages, int enable)
  979. {
  980. unsigned long flags, vaddr, lmi;
  981. int i;
  982. local_irq_save(flags);
  983. for (i = 0; i < numpages; i++, page++) {
  984. vaddr = (unsigned long)page_address(page);
  985. lmi = __pa(vaddr) >> PAGE_SHIFT;
  986. if (lmi >= linear_map_hash_count)
  987. continue;
  988. if (enable)
  989. kernel_map_linear_page(vaddr, lmi);
  990. else
  991. kernel_unmap_linear_page(vaddr, lmi);
  992. }
  993. local_irq_restore(flags);
  994. }
  995. #endif /* CONFIG_DEBUG_PAGEALLOC */