hash_utils_64.c 21 KB

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