hash_utils_64.c 21 KB

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