hash_utils_64.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. static inline void make_bl(unsigned int *insn_addr, void *func)
  372. {
  373. unsigned long funcp = *((unsigned long *)func);
  374. int offset = funcp - (unsigned long)insn_addr;
  375. *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
  376. flush_icache_range((unsigned long)insn_addr, 4+
  377. (unsigned long)insn_addr);
  378. }
  379. static void __init htab_finish_init(void)
  380. {
  381. extern unsigned int *htab_call_hpte_insert1;
  382. extern unsigned int *htab_call_hpte_insert2;
  383. extern unsigned int *htab_call_hpte_remove;
  384. extern unsigned int *htab_call_hpte_updatepp;
  385. #ifdef CONFIG_PPC_64K_PAGES
  386. extern unsigned int *ht64_call_hpte_insert1;
  387. extern unsigned int *ht64_call_hpte_insert2;
  388. extern unsigned int *ht64_call_hpte_remove;
  389. extern unsigned int *ht64_call_hpte_updatepp;
  390. make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
  391. make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
  392. make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
  393. make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
  394. #endif /* CONFIG_PPC_64K_PAGES */
  395. make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
  396. make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
  397. make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
  398. make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
  399. }
  400. void __init htab_initialize(void)
  401. {
  402. unsigned long table;
  403. unsigned long pteg_count;
  404. unsigned long mode_rw;
  405. unsigned long base = 0, size = 0;
  406. int i;
  407. extern unsigned long tce_alloc_start, tce_alloc_end;
  408. DBG(" -> htab_initialize()\n");
  409. /* Initialize page sizes */
  410. htab_init_page_sizes();
  411. /*
  412. * Calculate the required size of the htab. We want the number of
  413. * PTEGs to equal one half the number of real pages.
  414. */
  415. htab_size_bytes = htab_get_table_size();
  416. pteg_count = htab_size_bytes >> 7;
  417. htab_hash_mask = pteg_count - 1;
  418. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  419. /* Using a hypervisor which owns the htab */
  420. htab_address = NULL;
  421. _SDR1 = 0;
  422. } else {
  423. /* Find storage for the HPT. Must be contiguous in
  424. * the absolute address space.
  425. */
  426. table = lmb_alloc(htab_size_bytes, htab_size_bytes);
  427. DBG("Hash table allocated at %lx, size: %lx\n", table,
  428. htab_size_bytes);
  429. htab_address = abs_to_virt(table);
  430. /* htab absolute addr + encoded htabsize */
  431. _SDR1 = table + __ilog2(pteg_count) - 11;
  432. /* Initialize the HPT with no entries */
  433. memset((void *)table, 0, htab_size_bytes);
  434. /* Set SDR1 */
  435. mtspr(SPRN_SDR1, _SDR1);
  436. }
  437. mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
  438. /* On U3 based machines, we need to reserve the DART area and
  439. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  440. * cacheable later on
  441. */
  442. /* create bolted the linear mapping in the hash table */
  443. for (i=0; i < lmb.memory.cnt; i++) {
  444. base = (unsigned long)__va(lmb.memory.region[i].base);
  445. size = lmb.memory.region[i].size;
  446. DBG("creating mapping for region: %lx : %lx\n", base, size);
  447. #ifdef CONFIG_U3_DART
  448. /* Do not map the DART space. Fortunately, it will be aligned
  449. * in such a way that it will not cross two lmb regions and
  450. * will fit within a single 16Mb page.
  451. * The DART space is assumed to be a full 16Mb region even if
  452. * we only use 2Mb of that space. We will use more of it later
  453. * for AGP GART. We have to use a full 16Mb large page.
  454. */
  455. DBG("DART base: %lx\n", dart_tablebase);
  456. if (dart_tablebase != 0 && dart_tablebase >= base
  457. && dart_tablebase < (base + size)) {
  458. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  459. if (base != dart_tablebase)
  460. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  461. __pa(base), mode_rw,
  462. mmu_linear_psize));
  463. if ((base + size) > dart_table_end)
  464. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  465. base + size,
  466. __pa(dart_table_end),
  467. mode_rw,
  468. mmu_linear_psize));
  469. continue;
  470. }
  471. #endif /* CONFIG_U3_DART */
  472. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  473. mode_rw, mmu_linear_psize));
  474. }
  475. /*
  476. * If we have a memory_limit and we've allocated TCEs then we need to
  477. * explicitly map the TCE area at the top of RAM. We also cope with the
  478. * case that the TCEs start below memory_limit.
  479. * tce_alloc_start/end are 16MB aligned so the mapping should work
  480. * for either 4K or 16MB pages.
  481. */
  482. if (tce_alloc_start) {
  483. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  484. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  485. if (base + size >= tce_alloc_start)
  486. tce_alloc_start = base + size + 1;
  487. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  488. __pa(tce_alloc_start), mode_rw,
  489. mmu_linear_psize));
  490. }
  491. htab_finish_init();
  492. DBG(" <- htab_initialize()\n");
  493. }
  494. #undef KB
  495. #undef MB
  496. void htab_initialize_secondary(void)
  497. {
  498. if (!firmware_has_feature(FW_FEATURE_LPAR))
  499. mtspr(SPRN_SDR1, _SDR1);
  500. }
  501. /*
  502. * Called by asm hashtable.S for doing lazy icache flush
  503. */
  504. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  505. {
  506. struct page *page;
  507. if (!pfn_valid(pte_pfn(pte)))
  508. return pp;
  509. page = pte_page(pte);
  510. /* page is dirty */
  511. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  512. if (trap == 0x400) {
  513. __flush_dcache_icache(page_address(page));
  514. set_bit(PG_arch_1, &page->flags);
  515. } else
  516. pp |= HPTE_R_N;
  517. }
  518. return pp;
  519. }
  520. /* Result code is:
  521. * 0 - handled
  522. * 1 - normal page fault
  523. * -1 - critical hash insertion error
  524. */
  525. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  526. {
  527. void *pgdir;
  528. unsigned long vsid;
  529. struct mm_struct *mm;
  530. pte_t *ptep;
  531. cpumask_t tmp;
  532. int rc, user_region = 0, local = 0;
  533. int psize;
  534. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  535. ea, access, trap);
  536. if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
  537. DBG_LOW(" out of pgtable range !\n");
  538. return 1;
  539. }
  540. /* Get region & vsid */
  541. switch (REGION_ID(ea)) {
  542. case USER_REGION_ID:
  543. user_region = 1;
  544. mm = current->mm;
  545. if (! mm) {
  546. DBG_LOW(" user region with no mm !\n");
  547. return 1;
  548. }
  549. vsid = get_vsid(mm->context.id, ea);
  550. psize = mm->context.user_psize;
  551. break;
  552. case VMALLOC_REGION_ID:
  553. mm = &init_mm;
  554. vsid = get_kernel_vsid(ea);
  555. if (ea < VMALLOC_END)
  556. psize = mmu_vmalloc_psize;
  557. else
  558. psize = mmu_io_psize;
  559. break;
  560. default:
  561. /* Not a valid range
  562. * Send the problem up to do_page_fault
  563. */
  564. return 1;
  565. }
  566. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  567. /* Get pgdir */
  568. pgdir = mm->pgd;
  569. if (pgdir == NULL)
  570. return 1;
  571. /* Check CPU locality */
  572. tmp = cpumask_of_cpu(smp_processor_id());
  573. if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
  574. local = 1;
  575. /* Handle hugepage regions */
  576. if (unlikely(in_hugepage_area(mm->context, ea))) {
  577. DBG_LOW(" -> huge page !\n");
  578. return hash_huge_page(mm, access, ea, vsid, local, trap);
  579. }
  580. /* Get PTE and page size from page tables */
  581. ptep = find_linux_pte(pgdir, ea);
  582. if (ptep == NULL || !pte_present(*ptep)) {
  583. DBG_LOW(" no PTE !\n");
  584. return 1;
  585. }
  586. #ifndef CONFIG_PPC_64K_PAGES
  587. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  588. #else
  589. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  590. pte_val(*(ptep + PTRS_PER_PTE)));
  591. #endif
  592. /* Pre-check access permissions (will be re-checked atomically
  593. * in __hash_page_XX but this pre-check is a fast path
  594. */
  595. if (access & ~pte_val(*ptep)) {
  596. DBG_LOW(" no access !\n");
  597. return 1;
  598. }
  599. /* Do actual hashing */
  600. #ifndef CONFIG_PPC_64K_PAGES
  601. rc = __hash_page_4K(ea, access, vsid, ptep, trap, local);
  602. #else
  603. if (mmu_ci_restrictions) {
  604. /* If this PTE is non-cacheable, switch to 4k */
  605. if (psize == MMU_PAGE_64K &&
  606. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  607. if (user_region) {
  608. psize = MMU_PAGE_4K;
  609. mm->context.user_psize = MMU_PAGE_4K;
  610. mm->context.sllp = SLB_VSID_USER |
  611. mmu_psize_defs[MMU_PAGE_4K].sllp;
  612. } else if (ea < VMALLOC_END) {
  613. /*
  614. * some driver did a non-cacheable mapping
  615. * in vmalloc space, so switch vmalloc
  616. * to 4k pages
  617. */
  618. printk(KERN_ALERT "Reducing vmalloc segment "
  619. "to 4kB pages because of "
  620. "non-cacheable mapping\n");
  621. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  622. }
  623. }
  624. if (user_region) {
  625. if (psize != get_paca()->context.user_psize) {
  626. get_paca()->context = mm->context;
  627. slb_flush_and_rebolt();
  628. }
  629. } else if (get_paca()->vmalloc_sllp !=
  630. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  631. get_paca()->vmalloc_sllp =
  632. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  633. slb_flush_and_rebolt();
  634. }
  635. }
  636. if (psize == MMU_PAGE_64K)
  637. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local);
  638. else
  639. rc = __hash_page_4K(ea, access, vsid, ptep, trap, local);
  640. #endif /* CONFIG_PPC_64K_PAGES */
  641. #ifndef CONFIG_PPC_64K_PAGES
  642. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  643. #else
  644. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  645. pte_val(*(ptep + PTRS_PER_PTE)));
  646. #endif
  647. DBG_LOW(" -> rc=%d\n", rc);
  648. return rc;
  649. }
  650. EXPORT_SYMBOL_GPL(hash_page);
  651. void hash_preload(struct mm_struct *mm, unsigned long ea,
  652. unsigned long access, unsigned long trap)
  653. {
  654. unsigned long vsid;
  655. void *pgdir;
  656. pte_t *ptep;
  657. cpumask_t mask;
  658. unsigned long flags;
  659. int local = 0;
  660. /* We don't want huge pages prefaulted for now
  661. */
  662. if (unlikely(in_hugepage_area(mm->context, ea)))
  663. return;
  664. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  665. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  666. /* Get PTE, VSID, access mask */
  667. pgdir = mm->pgd;
  668. if (pgdir == NULL)
  669. return;
  670. ptep = find_linux_pte(pgdir, ea);
  671. if (!ptep)
  672. return;
  673. vsid = get_vsid(mm->context.id, ea);
  674. /* Hash it in */
  675. local_irq_save(flags);
  676. mask = cpumask_of_cpu(smp_processor_id());
  677. if (cpus_equal(mm->cpu_vm_mask, mask))
  678. local = 1;
  679. #ifndef CONFIG_PPC_64K_PAGES
  680. __hash_page_4K(ea, access, vsid, ptep, trap, local);
  681. #else
  682. if (mmu_ci_restrictions) {
  683. /* If this PTE is non-cacheable, switch to 4k */
  684. if (mm->context.user_psize == MMU_PAGE_64K &&
  685. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  686. mm->context.user_psize = MMU_PAGE_4K;
  687. mm->context.sllp = SLB_VSID_USER |
  688. mmu_psize_defs[MMU_PAGE_4K].sllp;
  689. get_paca()->context = mm->context;
  690. slb_flush_and_rebolt();
  691. }
  692. }
  693. if (mm->context.user_psize == MMU_PAGE_64K)
  694. __hash_page_64K(ea, access, vsid, ptep, trap, local);
  695. else
  696. __hash_page_4K(ea, access, vsid, ptep, trap, local);
  697. #endif /* CONFIG_PPC_64K_PAGES */
  698. local_irq_restore(flags);
  699. }
  700. void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int local)
  701. {
  702. unsigned long hash, index, shift, hidx, slot;
  703. DBG_LOW("flush_hash_page(va=%016x)\n", va);
  704. pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
  705. hash = hpt_hash(va, shift);
  706. hidx = __rpte_to_hidx(pte, index);
  707. if (hidx & _PTEIDX_SECONDARY)
  708. hash = ~hash;
  709. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  710. slot += hidx & _PTEIDX_GROUP_IX;
  711. DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
  712. ppc_md.hpte_invalidate(slot, va, psize, local);
  713. } pte_iterate_hashed_end();
  714. }
  715. void flush_hash_range(unsigned long number, int local)
  716. {
  717. if (ppc_md.flush_hash_range)
  718. ppc_md.flush_hash_range(number, local);
  719. else {
  720. int i;
  721. struct ppc64_tlb_batch *batch =
  722. &__get_cpu_var(ppc64_tlb_batch);
  723. for (i = 0; i < number; i++)
  724. flush_hash_page(batch->vaddr[i], batch->pte[i],
  725. batch->psize, local);
  726. }
  727. }
  728. /*
  729. * low_hash_fault is called when we the low level hash code failed
  730. * to instert a PTE due to an hypervisor error
  731. */
  732. void low_hash_fault(struct pt_regs *regs, unsigned long address)
  733. {
  734. if (user_mode(regs)) {
  735. siginfo_t info;
  736. info.si_signo = SIGBUS;
  737. info.si_errno = 0;
  738. info.si_code = BUS_ADRERR;
  739. info.si_addr = (void __user *)address;
  740. force_sig_info(SIGBUS, &info, current);
  741. return;
  742. }
  743. bad_page_fault(regs, address, SIGBUS);
  744. }