hash_utils_64.c 21 KB

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