hash_utils_64.c 25 KB

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