hash_utils_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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/config.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/errno.h>
  25. #include <linux/sched.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/stat.h>
  28. #include <linux/sysctl.h>
  29. #include <linux/ctype.h>
  30. #include <linux/cache.h>
  31. #include <linux/init.h>
  32. #include <linux/signal.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/lmb.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/abs_addr.h>
  51. #include <asm/sections.h>
  52. #ifdef DEBUG
  53. #define DBG(fmt...) udbg_printf(fmt)
  54. #else
  55. #define DBG(fmt...)
  56. #endif
  57. #ifdef DEBUG_LOW
  58. #define DBG_LOW(fmt...) udbg_printf(fmt)
  59. #else
  60. #define DBG_LOW(fmt...)
  61. #endif
  62. #define KB (1024)
  63. #define MB (1024*KB)
  64. /*
  65. * Note: pte --> Linux PTE
  66. * HPTE --> PowerPC Hashed Page Table Entry
  67. *
  68. * Execution context:
  69. * htab_initialize is called with the MMU off (of course), but
  70. * the kernel has been copied down to zero so it can directly
  71. * reference global data. At this point it is very difficult
  72. * to print debug info.
  73. *
  74. */
  75. #ifdef CONFIG_U3_DART
  76. extern unsigned long dart_tablebase;
  77. #endif /* CONFIG_U3_DART */
  78. hpte_t *htab_address;
  79. unsigned long htab_hash_mask;
  80. unsigned long _SDR1;
  81. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
  82. int mmu_linear_psize = MMU_PAGE_4K;
  83. int mmu_virtual_psize = MMU_PAGE_4K;
  84. #ifdef CONFIG_HUGETLB_PAGE
  85. int mmu_huge_psize = MMU_PAGE_16M;
  86. unsigned int HPAGE_SHIFT;
  87. #endif
  88. /* There are definitions of page sizes arrays to be used when none
  89. * is provided by the firmware.
  90. */
  91. /* Pre-POWER4 CPUs (4k pages only)
  92. */
  93. struct mmu_psize_def mmu_psize_defaults_old[] = {
  94. [MMU_PAGE_4K] = {
  95. .shift = 12,
  96. .sllp = 0,
  97. .penc = 0,
  98. .avpnm = 0,
  99. .tlbiel = 0,
  100. },
  101. };
  102. /* POWER4, GPUL, POWER5
  103. *
  104. * Support for 16Mb large pages
  105. */
  106. struct mmu_psize_def mmu_psize_defaults_gp[] = {
  107. [MMU_PAGE_4K] = {
  108. .shift = 12,
  109. .sllp = 0,
  110. .penc = 0,
  111. .avpnm = 0,
  112. .tlbiel = 1,
  113. },
  114. [MMU_PAGE_16M] = {
  115. .shift = 24,
  116. .sllp = SLB_VSID_L,
  117. .penc = 0,
  118. .avpnm = 0x1UL,
  119. .tlbiel = 0,
  120. },
  121. };
  122. int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
  123. unsigned long pstart, unsigned long mode, int psize)
  124. {
  125. unsigned long vaddr, paddr;
  126. unsigned int step, shift;
  127. unsigned long tmp_mode;
  128. int ret = 0;
  129. shift = mmu_psize_defs[psize].shift;
  130. step = 1 << shift;
  131. for (vaddr = vstart, paddr = pstart; vaddr < vend;
  132. vaddr += step, paddr += step) {
  133. unsigned long vpn, hash, hpteg;
  134. unsigned long vsid = get_kernel_vsid(vaddr);
  135. unsigned long va = (vsid << 28) | (vaddr & 0x0fffffff);
  136. vpn = va >> shift;
  137. tmp_mode = mode;
  138. /* Make non-kernel text non-executable */
  139. if (!in_kernel_text(vaddr))
  140. tmp_mode = mode | HPTE_R_N;
  141. hash = hpt_hash(va, shift);
  142. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  143. /* The crap below can be cleaned once ppd_md.probe() can
  144. * set up the hash callbacks, thus we can just used the
  145. * normal insert callback here.
  146. */
  147. #ifdef CONFIG_PPC_ISERIES
  148. if (systemcfg->platform == PLATFORM_ISERIES_LPAR)
  149. ret = iSeries_hpte_insert(hpteg, va,
  150. virt_to_abs(paddr),
  151. tmp_mode,
  152. HPTE_V_BOLTED,
  153. psize);
  154. else
  155. #endif
  156. #ifdef CONFIG_PPC_PSERIES
  157. if (systemcfg->platform & PLATFORM_LPAR)
  158. ret = pSeries_lpar_hpte_insert(hpteg, va,
  159. virt_to_abs(paddr),
  160. tmp_mode,
  161. HPTE_V_BOLTED,
  162. psize);
  163. else
  164. #endif
  165. #ifdef CONFIG_PPC_MULTIPLATFORM
  166. ret = native_hpte_insert(hpteg, va,
  167. virt_to_abs(paddr),
  168. tmp_mode, HPTE_V_BOLTED,
  169. psize);
  170. #endif
  171. if (ret < 0)
  172. break;
  173. }
  174. return ret < 0 ? ret : 0;
  175. }
  176. static int __init htab_dt_scan_page_sizes(unsigned long node,
  177. const char *uname, int depth,
  178. void *data)
  179. {
  180. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  181. u32 *prop;
  182. unsigned long size = 0;
  183. /* We are scanning "cpu" nodes only */
  184. if (type == NULL || strcmp(type, "cpu") != 0)
  185. return 0;
  186. prop = (u32 *)of_get_flat_dt_prop(node,
  187. "ibm,segment-page-sizes", &size);
  188. if (prop != NULL) {
  189. DBG("Page sizes from device-tree:\n");
  190. size /= 4;
  191. cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
  192. while(size > 0) {
  193. unsigned int shift = prop[0];
  194. unsigned int slbenc = prop[1];
  195. unsigned int lpnum = prop[2];
  196. unsigned int lpenc = 0;
  197. struct mmu_psize_def *def;
  198. int idx = -1;
  199. size -= 3; prop += 3;
  200. while(size > 0 && lpnum) {
  201. if (prop[0] == shift)
  202. lpenc = prop[1];
  203. prop += 2; size -= 2;
  204. lpnum--;
  205. }
  206. switch(shift) {
  207. case 0xc:
  208. idx = MMU_PAGE_4K;
  209. break;
  210. case 0x10:
  211. idx = MMU_PAGE_64K;
  212. break;
  213. case 0x14:
  214. idx = MMU_PAGE_1M;
  215. break;
  216. case 0x18:
  217. idx = MMU_PAGE_16M;
  218. cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
  219. break;
  220. case 0x22:
  221. idx = MMU_PAGE_16G;
  222. break;
  223. }
  224. if (idx < 0)
  225. continue;
  226. def = &mmu_psize_defs[idx];
  227. def->shift = shift;
  228. if (shift <= 23)
  229. def->avpnm = 0;
  230. else
  231. def->avpnm = (1 << (shift - 23)) - 1;
  232. def->sllp = slbenc;
  233. def->penc = lpenc;
  234. /* We don't know for sure what's up with tlbiel, so
  235. * for now we only set it for 4K and 64K pages
  236. */
  237. if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
  238. def->tlbiel = 1;
  239. else
  240. def->tlbiel = 0;
  241. DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
  242. "tlbiel=%d, penc=%d\n",
  243. idx, shift, def->sllp, def->avpnm, def->tlbiel,
  244. def->penc);
  245. }
  246. return 1;
  247. }
  248. return 0;
  249. }
  250. static void __init htab_init_page_sizes(void)
  251. {
  252. int rc;
  253. /* Default to 4K pages only */
  254. memcpy(mmu_psize_defs, mmu_psize_defaults_old,
  255. sizeof(mmu_psize_defaults_old));
  256. /*
  257. * Try to find the available page sizes in the device-tree
  258. */
  259. rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
  260. if (rc != 0) /* Found */
  261. goto found;
  262. /*
  263. * Not in the device-tree, let's fallback on known size
  264. * list for 16M capable GP & GR
  265. */
  266. if ((systemcfg->platform != PLATFORM_ISERIES_LPAR) &&
  267. cpu_has_feature(CPU_FTR_16M_PAGE))
  268. memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
  269. sizeof(mmu_psize_defaults_gp));
  270. found:
  271. /*
  272. * Pick a size for the linear mapping. Currently, we only support
  273. * 16M, 1M and 4K which is the default
  274. */
  275. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  276. mmu_linear_psize = MMU_PAGE_16M;
  277. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  278. mmu_linear_psize = MMU_PAGE_1M;
  279. /*
  280. * Pick a size for the ordinary pages. Default is 4K, we support
  281. * 64K if cache inhibited large pages are supported by the
  282. * processor
  283. */
  284. #ifdef CONFIG_PPC_64K_PAGES
  285. if (mmu_psize_defs[MMU_PAGE_64K].shift &&
  286. cpu_has_feature(CPU_FTR_CI_LARGE_PAGE))
  287. mmu_virtual_psize = MMU_PAGE_64K;
  288. #endif
  289. printk(KERN_INFO "Page orders: linear mapping = %d, others = %d\n",
  290. mmu_psize_defs[mmu_linear_psize].shift,
  291. mmu_psize_defs[mmu_virtual_psize].shift);
  292. #ifdef CONFIG_HUGETLB_PAGE
  293. /* Init large page size. Currently, we pick 16M or 1M depending
  294. * on what is available
  295. */
  296. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  297. mmu_huge_psize = MMU_PAGE_16M;
  298. /* With 4k/4level pagetables, we can't (for now) cope with a
  299. * huge page size < PMD_SIZE */
  300. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  301. mmu_huge_psize = MMU_PAGE_1M;
  302. /* Calculate HPAGE_SHIFT and sanity check it */
  303. if (mmu_psize_defs[mmu_huge_psize].shift > MIN_HUGEPTE_SHIFT &&
  304. mmu_psize_defs[mmu_huge_psize].shift < SID_SHIFT)
  305. HPAGE_SHIFT = mmu_psize_defs[mmu_huge_psize].shift;
  306. else
  307. HPAGE_SHIFT = 0; /* No huge pages dude ! */
  308. #endif /* CONFIG_HUGETLB_PAGE */
  309. }
  310. static int __init htab_dt_scan_pftsize(unsigned long node,
  311. const char *uname, int depth,
  312. void *data)
  313. {
  314. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  315. u32 *prop;
  316. /* We are scanning "cpu" nodes only */
  317. if (type == NULL || strcmp(type, "cpu") != 0)
  318. return 0;
  319. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
  320. if (prop != NULL) {
  321. /* pft_size[0] is the NUMA CEC cookie */
  322. ppc64_pft_size = prop[1];
  323. return 1;
  324. }
  325. return 0;
  326. }
  327. static unsigned long __init htab_get_table_size(void)
  328. {
  329. unsigned long rnd_mem_size, pteg_count;
  330. /* If hash size isn't already provided by the platform, we try to
  331. * retreive it from the device-tree. If it's not there neither, we
  332. * calculate it now based on the total RAM size
  333. */
  334. if (ppc64_pft_size == 0)
  335. of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
  336. if (ppc64_pft_size)
  337. return 1UL << ppc64_pft_size;
  338. /* round mem_size up to next power of 2 */
  339. rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
  340. if (rnd_mem_size < systemcfg->physicalMemorySize)
  341. rnd_mem_size <<= 1;
  342. /* # pages / 2 */
  343. pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
  344. return pteg_count << 7;
  345. }
  346. #ifdef CONFIG_MEMORY_HOTPLUG
  347. void create_section_mapping(unsigned long start, unsigned long end)
  348. {
  349. BUG_ON(htab_bolt_mapping(start, end, start,
  350. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
  351. mmu_linear_psize));
  352. }
  353. #endif /* CONFIG_MEMORY_HOTPLUG */
  354. void __init htab_initialize(void)
  355. {
  356. unsigned long table, htab_size_bytes;
  357. unsigned long pteg_count;
  358. unsigned long mode_rw;
  359. unsigned long base = 0, size = 0;
  360. int i;
  361. extern unsigned long tce_alloc_start, tce_alloc_end;
  362. DBG(" -> htab_initialize()\n");
  363. /* Initialize page sizes */
  364. htab_init_page_sizes();
  365. /*
  366. * Calculate the required size of the htab. We want the number of
  367. * PTEGs to equal one half the number of real pages.
  368. */
  369. htab_size_bytes = htab_get_table_size();
  370. pteg_count = htab_size_bytes >> 7;
  371. htab_hash_mask = pteg_count - 1;
  372. if (systemcfg->platform & PLATFORM_LPAR) {
  373. /* Using a hypervisor which owns the htab */
  374. htab_address = NULL;
  375. _SDR1 = 0;
  376. } else {
  377. /* Find storage for the HPT. Must be contiguous in
  378. * the absolute address space.
  379. */
  380. table = lmb_alloc(htab_size_bytes, htab_size_bytes);
  381. BUG_ON(table == 0);
  382. DBG("Hash table allocated at %lx, size: %lx\n", table,
  383. htab_size_bytes);
  384. htab_address = abs_to_virt(table);
  385. /* htab absolute addr + encoded htabsize */
  386. _SDR1 = table + __ilog2(pteg_count) - 11;
  387. /* Initialize the HPT with no entries */
  388. memset((void *)table, 0, htab_size_bytes);
  389. }
  390. mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
  391. /* On U3 based machines, we need to reserve the DART area and
  392. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  393. * cacheable later on
  394. */
  395. /* create bolted the linear mapping in the hash table */
  396. for (i=0; i < lmb.memory.cnt; i++) {
  397. base = lmb.memory.region[i].base + KERNELBASE;
  398. size = lmb.memory.region[i].size;
  399. DBG("creating mapping for region: %lx : %lx\n", base, size);
  400. #ifdef CONFIG_U3_DART
  401. /* Do not map the DART space. Fortunately, it will be aligned
  402. * in such a way that it will not cross two lmb regions and
  403. * will fit within a single 16Mb page.
  404. * The DART space is assumed to be a full 16Mb region even if
  405. * we only use 2Mb of that space. We will use more of it later
  406. * for AGP GART. We have to use a full 16Mb large page.
  407. */
  408. DBG("DART base: %lx\n", dart_tablebase);
  409. if (dart_tablebase != 0 && dart_tablebase >= base
  410. && dart_tablebase < (base + size)) {
  411. if (base != dart_tablebase)
  412. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  413. base, mode_rw,
  414. mmu_linear_psize));
  415. if ((base + size) > (dart_tablebase + 16*MB))
  416. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  417. base + size,
  418. dart_tablebase+16*MB,
  419. mode_rw,
  420. mmu_linear_psize));
  421. continue;
  422. }
  423. #endif /* CONFIG_U3_DART */
  424. BUG_ON(htab_bolt_mapping(base, base + size, base,
  425. mode_rw, mmu_linear_psize));
  426. }
  427. /*
  428. * If we have a memory_limit and we've allocated TCEs then we need to
  429. * explicitly map the TCE area at the top of RAM. We also cope with the
  430. * case that the TCEs start below memory_limit.
  431. * tce_alloc_start/end are 16MB aligned so the mapping should work
  432. * for either 4K or 16MB pages.
  433. */
  434. if (tce_alloc_start) {
  435. tce_alloc_start += KERNELBASE;
  436. tce_alloc_end += KERNELBASE;
  437. if (base + size >= tce_alloc_start)
  438. tce_alloc_start = base + size + 1;
  439. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  440. tce_alloc_start, mode_rw,
  441. mmu_linear_psize));
  442. }
  443. DBG(" <- htab_initialize()\n");
  444. }
  445. #undef KB
  446. #undef MB
  447. /*
  448. * Called by asm hashtable.S for doing lazy icache flush
  449. */
  450. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  451. {
  452. struct page *page;
  453. if (!pfn_valid(pte_pfn(pte)))
  454. return pp;
  455. page = pte_page(pte);
  456. /* page is dirty */
  457. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  458. if (trap == 0x400) {
  459. __flush_dcache_icache(page_address(page));
  460. set_bit(PG_arch_1, &page->flags);
  461. } else
  462. pp |= HPTE_R_N;
  463. }
  464. return pp;
  465. }
  466. /* Result code is:
  467. * 0 - handled
  468. * 1 - normal page fault
  469. * -1 - critical hash insertion error
  470. */
  471. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  472. {
  473. void *pgdir;
  474. unsigned long vsid;
  475. struct mm_struct *mm;
  476. pte_t *ptep;
  477. cpumask_t tmp;
  478. int rc, user_region = 0, local = 0;
  479. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  480. ea, access, trap);
  481. if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
  482. DBG_LOW(" out of pgtable range !\n");
  483. return 1;
  484. }
  485. /* Get region & vsid */
  486. switch (REGION_ID(ea)) {
  487. case USER_REGION_ID:
  488. user_region = 1;
  489. mm = current->mm;
  490. if (! mm) {
  491. DBG_LOW(" user region with no mm !\n");
  492. return 1;
  493. }
  494. vsid = get_vsid(mm->context.id, ea);
  495. break;
  496. case VMALLOC_REGION_ID:
  497. mm = &init_mm;
  498. vsid = get_kernel_vsid(ea);
  499. break;
  500. default:
  501. /* Not a valid range
  502. * Send the problem up to do_page_fault
  503. */
  504. return 1;
  505. }
  506. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  507. /* Get pgdir */
  508. pgdir = mm->pgd;
  509. if (pgdir == NULL)
  510. return 1;
  511. /* Check CPU locality */
  512. tmp = cpumask_of_cpu(smp_processor_id());
  513. if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
  514. local = 1;
  515. /* Handle hugepage regions */
  516. if (unlikely(in_hugepage_area(mm->context, ea))) {
  517. DBG_LOW(" -> huge page !\n");
  518. return hash_huge_page(mm, access, ea, vsid, local);
  519. }
  520. /* Get PTE and page size from page tables */
  521. ptep = find_linux_pte(pgdir, ea);
  522. if (ptep == NULL || !pte_present(*ptep)) {
  523. DBG_LOW(" no PTE !\n");
  524. return 1;
  525. }
  526. #ifndef CONFIG_PPC_64K_PAGES
  527. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  528. #else
  529. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  530. pte_val(*(ptep + PTRS_PER_PTE)));
  531. #endif
  532. /* Pre-check access permissions (will be re-checked atomically
  533. * in __hash_page_XX but this pre-check is a fast path
  534. */
  535. if (access & ~pte_val(*ptep)) {
  536. DBG_LOW(" no access !\n");
  537. return 1;
  538. }
  539. /* Do actual hashing */
  540. #ifndef CONFIG_PPC_64K_PAGES
  541. rc = __hash_page_4K(ea, access, vsid, ptep, trap, local);
  542. #else
  543. if (mmu_virtual_psize == MMU_PAGE_64K)
  544. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local);
  545. else
  546. rc = __hash_page_4K(ea, access, vsid, ptep, trap, local);
  547. #endif /* CONFIG_PPC_64K_PAGES */
  548. #ifndef CONFIG_PPC_64K_PAGES
  549. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  550. #else
  551. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  552. pte_val(*(ptep + PTRS_PER_PTE)));
  553. #endif
  554. DBG_LOW(" -> rc=%d\n", rc);
  555. return rc;
  556. }
  557. void hash_preload(struct mm_struct *mm, unsigned long ea,
  558. unsigned long access, unsigned long trap)
  559. {
  560. unsigned long vsid;
  561. void *pgdir;
  562. pte_t *ptep;
  563. cpumask_t mask;
  564. unsigned long flags;
  565. int local = 0;
  566. /* We don't want huge pages prefaulted for now
  567. */
  568. if (unlikely(in_hugepage_area(mm->context, ea)))
  569. return;
  570. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  571. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  572. /* Get PTE, VSID, access mask */
  573. pgdir = mm->pgd;
  574. if (pgdir == NULL)
  575. return;
  576. ptep = find_linux_pte(pgdir, ea);
  577. if (!ptep)
  578. return;
  579. vsid = get_vsid(mm->context.id, ea);
  580. /* Hash it in */
  581. local_irq_save(flags);
  582. mask = cpumask_of_cpu(smp_processor_id());
  583. if (cpus_equal(mm->cpu_vm_mask, mask))
  584. local = 1;
  585. #ifndef CONFIG_PPC_64K_PAGES
  586. __hash_page_4K(ea, access, vsid, ptep, trap, local);
  587. #else
  588. if (mmu_virtual_psize == MMU_PAGE_64K)
  589. __hash_page_64K(ea, access, vsid, ptep, trap, local);
  590. else
  591. __hash_page_4K(ea, access, vsid, ptep, trap, local);
  592. #endif /* CONFIG_PPC_64K_PAGES */
  593. local_irq_restore(flags);
  594. }
  595. void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int local)
  596. {
  597. unsigned long hash, index, shift, hidx, slot;
  598. DBG_LOW("flush_hash_page(va=%016x)\n", va);
  599. pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
  600. hash = hpt_hash(va, shift);
  601. hidx = __rpte_to_hidx(pte, index);
  602. if (hidx & _PTEIDX_SECONDARY)
  603. hash = ~hash;
  604. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  605. slot += hidx & _PTEIDX_GROUP_IX;
  606. DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
  607. ppc_md.hpte_invalidate(slot, va, psize, local);
  608. } pte_iterate_hashed_end();
  609. }
  610. void flush_hash_range(unsigned long number, int local)
  611. {
  612. if (ppc_md.flush_hash_range)
  613. ppc_md.flush_hash_range(number, local);
  614. else {
  615. int i;
  616. struct ppc64_tlb_batch *batch =
  617. &__get_cpu_var(ppc64_tlb_batch);
  618. for (i = 0; i < number; i++)
  619. flush_hash_page(batch->vaddr[i], batch->pte[i],
  620. batch->psize, local);
  621. }
  622. }
  623. static inline void make_bl(unsigned int *insn_addr, void *func)
  624. {
  625. unsigned long funcp = *((unsigned long *)func);
  626. int offset = funcp - (unsigned long)insn_addr;
  627. *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
  628. flush_icache_range((unsigned long)insn_addr, 4+
  629. (unsigned long)insn_addr);
  630. }
  631. /*
  632. * low_hash_fault is called when we the low level hash code failed
  633. * to instert a PTE due to an hypervisor error
  634. */
  635. void low_hash_fault(struct pt_regs *regs, unsigned long address)
  636. {
  637. if (user_mode(regs)) {
  638. siginfo_t info;
  639. info.si_signo = SIGBUS;
  640. info.si_errno = 0;
  641. info.si_code = BUS_ADRERR;
  642. info.si_addr = (void __user *)address;
  643. force_sig_info(SIGBUS, &info, current);
  644. return;
  645. }
  646. bad_page_fault(regs, address, SIGBUS);
  647. }
  648. void __init htab_finish_init(void)
  649. {
  650. extern unsigned int *htab_call_hpte_insert1;
  651. extern unsigned int *htab_call_hpte_insert2;
  652. extern unsigned int *htab_call_hpte_remove;
  653. extern unsigned int *htab_call_hpte_updatepp;
  654. #ifdef CONFIG_PPC_64K_PAGES
  655. extern unsigned int *ht64_call_hpte_insert1;
  656. extern unsigned int *ht64_call_hpte_insert2;
  657. extern unsigned int *ht64_call_hpte_remove;
  658. extern unsigned int *ht64_call_hpte_updatepp;
  659. make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
  660. make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
  661. make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
  662. make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
  663. #endif /* CONFIG_PPC_64K_PAGES */
  664. make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
  665. make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
  666. make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
  667. make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
  668. }