hash_utils_64.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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 <linux/lmb.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/prom.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/sections.h>
  51. #include <asm/spu.h>
  52. #include <asm/udbg.h>
  53. #ifdef DEBUG
  54. #define DBG(fmt...) udbg_printf(fmt)
  55. #else
  56. #define DBG(fmt...)
  57. #endif
  58. #ifdef DEBUG_LOW
  59. #define DBG_LOW(fmt...) udbg_printf(fmt)
  60. #else
  61. #define DBG_LOW(fmt...)
  62. #endif
  63. #define KB (1024)
  64. #define MB (1024*KB)
  65. /*
  66. * Note: pte --> Linux PTE
  67. * HPTE --> PowerPC Hashed Page Table Entry
  68. *
  69. * Execution context:
  70. * htab_initialize is called with the MMU off (of course), but
  71. * the kernel has been copied down to zero so it can directly
  72. * reference global data. At this point it is very difficult
  73. * to print debug info.
  74. *
  75. */
  76. #ifdef CONFIG_U3_DART
  77. extern unsigned long dart_tablebase;
  78. #endif /* CONFIG_U3_DART */
  79. static unsigned long _SDR1;
  80. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
  81. struct hash_pte *htab_address;
  82. unsigned long htab_size_bytes;
  83. unsigned long htab_hash_mask;
  84. int mmu_linear_psize = MMU_PAGE_4K;
  85. int mmu_virtual_psize = MMU_PAGE_4K;
  86. int mmu_vmalloc_psize = MMU_PAGE_4K;
  87. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  88. int mmu_vmemmap_psize = MMU_PAGE_4K;
  89. #endif
  90. int mmu_io_psize = MMU_PAGE_4K;
  91. int mmu_kernel_ssize = MMU_SEGSIZE_256M;
  92. int mmu_highuser_ssize = MMU_SEGSIZE_256M;
  93. u16 mmu_slb_size = 64;
  94. #ifdef CONFIG_HUGETLB_PAGE
  95. int mmu_huge_psize = MMU_PAGE_16M;
  96. unsigned int HPAGE_SHIFT;
  97. #endif
  98. #ifdef CONFIG_PPC_64K_PAGES
  99. int mmu_ci_restrictions;
  100. #endif
  101. #ifdef CONFIG_DEBUG_PAGEALLOC
  102. static u8 *linear_map_hash_slots;
  103. static unsigned long linear_map_hash_count;
  104. static DEFINE_SPINLOCK(linear_map_hash_lock);
  105. #endif /* CONFIG_DEBUG_PAGEALLOC */
  106. /* There are definitions of page sizes arrays to be used when none
  107. * is provided by the firmware.
  108. */
  109. /* Pre-POWER4 CPUs (4k pages only)
  110. */
  111. struct mmu_psize_def mmu_psize_defaults_old[] = {
  112. [MMU_PAGE_4K] = {
  113. .shift = 12,
  114. .sllp = 0,
  115. .penc = 0,
  116. .avpnm = 0,
  117. .tlbiel = 0,
  118. },
  119. };
  120. /* POWER4, GPUL, POWER5
  121. *
  122. * Support for 16Mb large pages
  123. */
  124. struct mmu_psize_def mmu_psize_defaults_gp[] = {
  125. [MMU_PAGE_4K] = {
  126. .shift = 12,
  127. .sllp = 0,
  128. .penc = 0,
  129. .avpnm = 0,
  130. .tlbiel = 1,
  131. },
  132. [MMU_PAGE_16M] = {
  133. .shift = 24,
  134. .sllp = SLB_VSID_L,
  135. .penc = 0,
  136. .avpnm = 0x1UL,
  137. .tlbiel = 0,
  138. },
  139. };
  140. int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
  141. unsigned long pstart, unsigned long mode,
  142. int psize, int ssize)
  143. {
  144. unsigned long vaddr, paddr;
  145. unsigned int step, shift;
  146. unsigned long tmp_mode;
  147. int ret = 0;
  148. shift = mmu_psize_defs[psize].shift;
  149. step = 1 << shift;
  150. for (vaddr = vstart, paddr = pstart; vaddr < vend;
  151. vaddr += step, paddr += step) {
  152. unsigned long hash, hpteg;
  153. unsigned long vsid = get_kernel_vsid(vaddr, ssize);
  154. unsigned long va = hpt_va(vaddr, vsid, ssize);
  155. tmp_mode = mode;
  156. /* Make non-kernel text non-executable */
  157. if (!in_kernel_text(vaddr))
  158. tmp_mode = mode | HPTE_R_N;
  159. hash = hpt_hash(va, shift, ssize);
  160. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  161. DBG("htab_bolt_mapping: calling %p\n", ppc_md.hpte_insert);
  162. BUG_ON(!ppc_md.hpte_insert);
  163. ret = ppc_md.hpte_insert(hpteg, va, paddr,
  164. tmp_mode, HPTE_V_BOLTED, psize, ssize);
  165. if (ret < 0)
  166. break;
  167. #ifdef CONFIG_DEBUG_PAGEALLOC
  168. if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
  169. linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
  170. #endif /* CONFIG_DEBUG_PAGEALLOC */
  171. }
  172. return ret < 0 ? ret : 0;
  173. }
  174. #ifdef CONFIG_MEMORY_HOTPLUG
  175. static int htab_remove_mapping(unsigned long vstart, unsigned long vend,
  176. int psize, int ssize)
  177. {
  178. unsigned long vaddr;
  179. unsigned int step, shift;
  180. shift = mmu_psize_defs[psize].shift;
  181. step = 1 << shift;
  182. if (!ppc_md.hpte_removebolted) {
  183. printk(KERN_WARNING "Platform doesn't implement "
  184. "hpte_removebolted\n");
  185. return -EINVAL;
  186. }
  187. for (vaddr = vstart; vaddr < vend; vaddr += step)
  188. ppc_md.hpte_removebolted(vaddr, psize, ssize);
  189. return 0;
  190. }
  191. #endif /* CONFIG_MEMORY_HOTPLUG */
  192. static int __init htab_dt_scan_seg_sizes(unsigned long node,
  193. const char *uname, int depth,
  194. void *data)
  195. {
  196. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  197. u32 *prop;
  198. unsigned long size = 0;
  199. /* We are scanning "cpu" nodes only */
  200. if (type == NULL || strcmp(type, "cpu") != 0)
  201. return 0;
  202. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
  203. &size);
  204. if (prop == NULL)
  205. return 0;
  206. for (; size >= 4; size -= 4, ++prop) {
  207. if (prop[0] == 40) {
  208. DBG("1T segment support detected\n");
  209. cur_cpu_spec->cpu_features |= CPU_FTR_1T_SEGMENT;
  210. return 1;
  211. }
  212. }
  213. cur_cpu_spec->cpu_features &= ~CPU_FTR_NO_SLBIE_B;
  214. return 0;
  215. }
  216. static void __init htab_init_seg_sizes(void)
  217. {
  218. of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
  219. }
  220. static int __init htab_dt_scan_page_sizes(unsigned long node,
  221. const char *uname, int depth,
  222. void *data)
  223. {
  224. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  225. u32 *prop;
  226. unsigned long size = 0;
  227. /* We are scanning "cpu" nodes only */
  228. if (type == NULL || strcmp(type, "cpu") != 0)
  229. return 0;
  230. prop = (u32 *)of_get_flat_dt_prop(node,
  231. "ibm,segment-page-sizes", &size);
  232. if (prop != NULL) {
  233. DBG("Page sizes from device-tree:\n");
  234. size /= 4;
  235. cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
  236. while(size > 0) {
  237. unsigned int shift = prop[0];
  238. unsigned int slbenc = prop[1];
  239. unsigned int lpnum = prop[2];
  240. unsigned int lpenc = 0;
  241. struct mmu_psize_def *def;
  242. int idx = -1;
  243. size -= 3; prop += 3;
  244. while(size > 0 && lpnum) {
  245. if (prop[0] == shift)
  246. lpenc = prop[1];
  247. prop += 2; size -= 2;
  248. lpnum--;
  249. }
  250. switch(shift) {
  251. case 0xc:
  252. idx = MMU_PAGE_4K;
  253. break;
  254. case 0x10:
  255. idx = MMU_PAGE_64K;
  256. break;
  257. case 0x14:
  258. idx = MMU_PAGE_1M;
  259. break;
  260. case 0x18:
  261. idx = MMU_PAGE_16M;
  262. cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
  263. break;
  264. case 0x22:
  265. idx = MMU_PAGE_16G;
  266. break;
  267. }
  268. if (idx < 0)
  269. continue;
  270. def = &mmu_psize_defs[idx];
  271. def->shift = shift;
  272. if (shift <= 23)
  273. def->avpnm = 0;
  274. else
  275. def->avpnm = (1 << (shift - 23)) - 1;
  276. def->sllp = slbenc;
  277. def->penc = lpenc;
  278. /* We don't know for sure what's up with tlbiel, so
  279. * for now we only set it for 4K and 64K pages
  280. */
  281. if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
  282. def->tlbiel = 1;
  283. else
  284. def->tlbiel = 0;
  285. DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
  286. "tlbiel=%d, penc=%d\n",
  287. idx, shift, def->sllp, def->avpnm, def->tlbiel,
  288. def->penc);
  289. }
  290. return 1;
  291. }
  292. return 0;
  293. }
  294. static void __init htab_init_page_sizes(void)
  295. {
  296. int rc;
  297. /* Default to 4K pages only */
  298. memcpy(mmu_psize_defs, mmu_psize_defaults_old,
  299. sizeof(mmu_psize_defaults_old));
  300. /*
  301. * Try to find the available page sizes in the device-tree
  302. */
  303. rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
  304. if (rc != 0) /* Found */
  305. goto found;
  306. /*
  307. * Not in the device-tree, let's fallback on known size
  308. * list for 16M capable GP & GR
  309. */
  310. if (cpu_has_feature(CPU_FTR_16M_PAGE))
  311. memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
  312. sizeof(mmu_psize_defaults_gp));
  313. found:
  314. #ifndef CONFIG_DEBUG_PAGEALLOC
  315. /*
  316. * Pick a size for the linear mapping. Currently, we only support
  317. * 16M, 1M and 4K which is the default
  318. */
  319. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  320. mmu_linear_psize = MMU_PAGE_16M;
  321. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  322. mmu_linear_psize = MMU_PAGE_1M;
  323. #endif /* CONFIG_DEBUG_PAGEALLOC */
  324. #ifdef CONFIG_PPC_64K_PAGES
  325. /*
  326. * Pick a size for the ordinary pages. Default is 4K, we support
  327. * 64K for user mappings and vmalloc if supported by the processor.
  328. * We only use 64k for ioremap if the processor
  329. * (and firmware) support cache-inhibited large pages.
  330. * If not, we use 4k and set mmu_ci_restrictions so that
  331. * hash_page knows to switch processes that use cache-inhibited
  332. * mappings to 4k pages.
  333. */
  334. if (mmu_psize_defs[MMU_PAGE_64K].shift) {
  335. mmu_virtual_psize = MMU_PAGE_64K;
  336. mmu_vmalloc_psize = MMU_PAGE_64K;
  337. if (mmu_linear_psize == MMU_PAGE_4K)
  338. mmu_linear_psize = MMU_PAGE_64K;
  339. if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE)) {
  340. /*
  341. * Don't use 64k pages for ioremap on pSeries, since
  342. * that would stop us accessing the HEA ethernet.
  343. */
  344. if (!machine_is(pseries))
  345. mmu_io_psize = MMU_PAGE_64K;
  346. } else
  347. mmu_ci_restrictions = 1;
  348. }
  349. #endif /* CONFIG_PPC_64K_PAGES */
  350. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  351. /* We try to use 16M pages for vmemmap if that is supported
  352. * and we have at least 1G of RAM at boot
  353. */
  354. if (mmu_psize_defs[MMU_PAGE_16M].shift &&
  355. lmb_phys_mem_size() >= 0x40000000)
  356. mmu_vmemmap_psize = MMU_PAGE_16M;
  357. else if (mmu_psize_defs[MMU_PAGE_64K].shift)
  358. mmu_vmemmap_psize = MMU_PAGE_64K;
  359. else
  360. mmu_vmemmap_psize = MMU_PAGE_4K;
  361. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  362. printk(KERN_DEBUG "Page orders: linear mapping = %d, "
  363. "virtual = %d, io = %d"
  364. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  365. ", vmemmap = %d"
  366. #endif
  367. "\n",
  368. mmu_psize_defs[mmu_linear_psize].shift,
  369. mmu_psize_defs[mmu_virtual_psize].shift,
  370. mmu_psize_defs[mmu_io_psize].shift
  371. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  372. ,mmu_psize_defs[mmu_vmemmap_psize].shift
  373. #endif
  374. );
  375. #ifdef CONFIG_HUGETLB_PAGE
  376. /* Init large page size. Currently, we pick 16M or 1M depending
  377. * on what is available
  378. */
  379. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  380. set_huge_psize(MMU_PAGE_16M);
  381. /* With 4k/4level pagetables, we can't (for now) cope with a
  382. * huge page size < PMD_SIZE */
  383. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  384. set_huge_psize(MMU_PAGE_1M);
  385. #endif /* CONFIG_HUGETLB_PAGE */
  386. }
  387. static int __init htab_dt_scan_pftsize(unsigned long node,
  388. const char *uname, int depth,
  389. void *data)
  390. {
  391. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  392. u32 *prop;
  393. /* We are scanning "cpu" nodes only */
  394. if (type == NULL || strcmp(type, "cpu") != 0)
  395. return 0;
  396. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
  397. if (prop != NULL) {
  398. /* pft_size[0] is the NUMA CEC cookie */
  399. ppc64_pft_size = prop[1];
  400. return 1;
  401. }
  402. return 0;
  403. }
  404. static unsigned long __init htab_get_table_size(void)
  405. {
  406. unsigned long mem_size, rnd_mem_size, pteg_count;
  407. /* If hash size isn't already provided by the platform, we try to
  408. * retrieve it from the device-tree. If it's not there neither, we
  409. * calculate it now based on the total RAM size
  410. */
  411. if (ppc64_pft_size == 0)
  412. of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
  413. if (ppc64_pft_size)
  414. return 1UL << ppc64_pft_size;
  415. /* round mem_size up to next power of 2 */
  416. mem_size = lmb_phys_mem_size();
  417. rnd_mem_size = 1UL << __ilog2(mem_size);
  418. if (rnd_mem_size < mem_size)
  419. rnd_mem_size <<= 1;
  420. /* # pages / 2 */
  421. pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
  422. return pteg_count << 7;
  423. }
  424. #ifdef CONFIG_MEMORY_HOTPLUG
  425. void create_section_mapping(unsigned long start, unsigned long end)
  426. {
  427. BUG_ON(htab_bolt_mapping(start, end, __pa(start),
  428. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
  429. mmu_linear_psize, mmu_kernel_ssize));
  430. }
  431. int remove_section_mapping(unsigned long start, unsigned long end)
  432. {
  433. return htab_remove_mapping(start, end, mmu_linear_psize,
  434. mmu_kernel_ssize);
  435. }
  436. #endif /* CONFIG_MEMORY_HOTPLUG */
  437. static inline void make_bl(unsigned int *insn_addr, void *func)
  438. {
  439. unsigned long funcp = *((unsigned long *)func);
  440. int offset = funcp - (unsigned long)insn_addr;
  441. *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
  442. flush_icache_range((unsigned long)insn_addr, 4+
  443. (unsigned long)insn_addr);
  444. }
  445. static void __init htab_finish_init(void)
  446. {
  447. extern unsigned int *htab_call_hpte_insert1;
  448. extern unsigned int *htab_call_hpte_insert2;
  449. extern unsigned int *htab_call_hpte_remove;
  450. extern unsigned int *htab_call_hpte_updatepp;
  451. #ifdef CONFIG_PPC_HAS_HASH_64K
  452. extern unsigned int *ht64_call_hpte_insert1;
  453. extern unsigned int *ht64_call_hpte_insert2;
  454. extern unsigned int *ht64_call_hpte_remove;
  455. extern unsigned int *ht64_call_hpte_updatepp;
  456. make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
  457. make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
  458. make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
  459. make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
  460. #endif /* CONFIG_PPC_HAS_HASH_64K */
  461. make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
  462. make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
  463. make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
  464. make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
  465. }
  466. void __init htab_initialize(void)
  467. {
  468. unsigned long table;
  469. unsigned long pteg_count;
  470. unsigned long mode_rw;
  471. unsigned long base = 0, size = 0, limit;
  472. int i;
  473. extern unsigned long tce_alloc_start, tce_alloc_end;
  474. DBG(" -> htab_initialize()\n");
  475. /* Initialize segment sizes */
  476. htab_init_seg_sizes();
  477. /* Initialize page sizes */
  478. htab_init_page_sizes();
  479. if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
  480. mmu_kernel_ssize = MMU_SEGSIZE_1T;
  481. mmu_highuser_ssize = MMU_SEGSIZE_1T;
  482. printk(KERN_INFO "Using 1TB segments\n");
  483. }
  484. /*
  485. * Calculate the required size of the htab. We want the number of
  486. * PTEGs to equal one half the number of real pages.
  487. */
  488. htab_size_bytes = htab_get_table_size();
  489. pteg_count = htab_size_bytes >> 7;
  490. htab_hash_mask = pteg_count - 1;
  491. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  492. /* Using a hypervisor which owns the htab */
  493. htab_address = NULL;
  494. _SDR1 = 0;
  495. } else {
  496. /* Find storage for the HPT. Must be contiguous in
  497. * the absolute address space. On cell we want it to be
  498. * in the first 2 Gig so we can use it for IOMMU hacks.
  499. */
  500. if (machine_is(cell))
  501. limit = 0x80000000;
  502. else
  503. limit = 0;
  504. table = lmb_alloc_base(htab_size_bytes, htab_size_bytes, limit);
  505. DBG("Hash table allocated at %lx, size: %lx\n", table,
  506. htab_size_bytes);
  507. htab_address = abs_to_virt(table);
  508. /* htab absolute addr + encoded htabsize */
  509. _SDR1 = table + __ilog2(pteg_count) - 11;
  510. /* Initialize the HPT with no entries */
  511. memset((void *)table, 0, htab_size_bytes);
  512. /* Set SDR1 */
  513. mtspr(SPRN_SDR1, _SDR1);
  514. }
  515. mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
  516. #ifdef CONFIG_DEBUG_PAGEALLOC
  517. linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
  518. linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
  519. 1, lmb.rmo_size));
  520. memset(linear_map_hash_slots, 0, linear_map_hash_count);
  521. #endif /* CONFIG_DEBUG_PAGEALLOC */
  522. /* On U3 based machines, we need to reserve the DART area and
  523. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  524. * cacheable later on
  525. */
  526. /* create bolted the linear mapping in the hash table */
  527. for (i=0; i < lmb.memory.cnt; i++) {
  528. base = (unsigned long)__va(lmb.memory.region[i].base);
  529. size = lmb.memory.region[i].size;
  530. DBG("creating mapping for region: %lx : %lx\n", base, size);
  531. #ifdef CONFIG_U3_DART
  532. /* Do not map the DART space. Fortunately, it will be aligned
  533. * in such a way that it will not cross two lmb regions and
  534. * will fit within a single 16Mb page.
  535. * The DART space is assumed to be a full 16Mb region even if
  536. * we only use 2Mb of that space. We will use more of it later
  537. * for AGP GART. We have to use a full 16Mb large page.
  538. */
  539. DBG("DART base: %lx\n", dart_tablebase);
  540. if (dart_tablebase != 0 && dart_tablebase >= base
  541. && dart_tablebase < (base + size)) {
  542. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  543. if (base != dart_tablebase)
  544. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  545. __pa(base), mode_rw,
  546. mmu_linear_psize,
  547. mmu_kernel_ssize));
  548. if ((base + size) > dart_table_end)
  549. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  550. base + size,
  551. __pa(dart_table_end),
  552. mode_rw,
  553. mmu_linear_psize,
  554. mmu_kernel_ssize));
  555. continue;
  556. }
  557. #endif /* CONFIG_U3_DART */
  558. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  559. mode_rw, mmu_linear_psize, mmu_kernel_ssize));
  560. }
  561. /*
  562. * If we have a memory_limit and we've allocated TCEs then we need to
  563. * explicitly map the TCE area at the top of RAM. We also cope with the
  564. * case that the TCEs start below memory_limit.
  565. * tce_alloc_start/end are 16MB aligned so the mapping should work
  566. * for either 4K or 16MB pages.
  567. */
  568. if (tce_alloc_start) {
  569. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  570. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  571. if (base + size >= tce_alloc_start)
  572. tce_alloc_start = base + size + 1;
  573. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  574. __pa(tce_alloc_start), mode_rw,
  575. mmu_linear_psize, mmu_kernel_ssize));
  576. }
  577. htab_finish_init();
  578. DBG(" <- htab_initialize()\n");
  579. }
  580. #undef KB
  581. #undef MB
  582. void htab_initialize_secondary(void)
  583. {
  584. if (!firmware_has_feature(FW_FEATURE_LPAR))
  585. mtspr(SPRN_SDR1, _SDR1);
  586. }
  587. /*
  588. * Called by asm hashtable.S for doing lazy icache flush
  589. */
  590. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  591. {
  592. struct page *page;
  593. if (!pfn_valid(pte_pfn(pte)))
  594. return pp;
  595. page = pte_page(pte);
  596. /* page is dirty */
  597. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  598. if (trap == 0x400) {
  599. __flush_dcache_icache(page_address(page));
  600. set_bit(PG_arch_1, &page->flags);
  601. } else
  602. pp |= HPTE_R_N;
  603. }
  604. return pp;
  605. }
  606. /*
  607. * Demote a segment to using 4k pages.
  608. * For now this makes the whole process use 4k pages.
  609. */
  610. #ifdef CONFIG_PPC_64K_PAGES
  611. void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  612. {
  613. if (mm->context.user_psize == MMU_PAGE_4K)
  614. return;
  615. slice_set_user_psize(mm, MMU_PAGE_4K);
  616. #ifdef CONFIG_SPU_BASE
  617. spu_flush_all_slbs(mm);
  618. #endif
  619. if (get_paca()->context.user_psize != MMU_PAGE_4K) {
  620. get_paca()->context = mm->context;
  621. slb_flush_and_rebolt();
  622. }
  623. }
  624. #endif /* CONFIG_PPC_64K_PAGES */
  625. #ifdef CONFIG_PPC_SUBPAGE_PROT
  626. /*
  627. * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
  628. * Userspace sets the subpage permissions using the subpage_prot system call.
  629. *
  630. * Result is 0: full permissions, _PAGE_RW: read-only,
  631. * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
  632. */
  633. static int subpage_protection(pgd_t *pgdir, unsigned long ea)
  634. {
  635. struct subpage_prot_table *spt = pgd_subpage_prot(pgdir);
  636. u32 spp = 0;
  637. u32 **sbpm, *sbpp;
  638. if (ea >= spt->maxaddr)
  639. return 0;
  640. if (ea < 0x100000000) {
  641. /* addresses below 4GB use spt->low_prot */
  642. sbpm = spt->low_prot;
  643. } else {
  644. sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
  645. if (!sbpm)
  646. return 0;
  647. }
  648. sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  649. if (!sbpp)
  650. return 0;
  651. spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
  652. /* extract 2-bit bitfield for this 4k subpage */
  653. spp >>= 30 - 2 * ((ea >> 12) & 0xf);
  654. /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
  655. spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
  656. return spp;
  657. }
  658. #else /* CONFIG_PPC_SUBPAGE_PROT */
  659. static inline int subpage_protection(pgd_t *pgdir, unsigned long ea)
  660. {
  661. return 0;
  662. }
  663. #endif
  664. /* Result code is:
  665. * 0 - handled
  666. * 1 - normal page fault
  667. * -1 - critical hash insertion error
  668. * -2 - access not permitted by subpage protection mechanism
  669. */
  670. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  671. {
  672. void *pgdir;
  673. unsigned long vsid;
  674. struct mm_struct *mm;
  675. pte_t *ptep;
  676. cpumask_t tmp;
  677. int rc, user_region = 0, local = 0;
  678. int psize, ssize;
  679. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  680. ea, access, trap);
  681. if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
  682. DBG_LOW(" out of pgtable range !\n");
  683. return 1;
  684. }
  685. /* Get region & vsid */
  686. switch (REGION_ID(ea)) {
  687. case USER_REGION_ID:
  688. user_region = 1;
  689. mm = current->mm;
  690. if (! mm) {
  691. DBG_LOW(" user region with no mm !\n");
  692. return 1;
  693. }
  694. #ifdef CONFIG_PPC_MM_SLICES
  695. psize = get_slice_psize(mm, ea);
  696. #else
  697. psize = mm->context.user_psize;
  698. #endif
  699. ssize = user_segment_size(ea);
  700. vsid = get_vsid(mm->context.id, ea, ssize);
  701. break;
  702. case VMALLOC_REGION_ID:
  703. mm = &init_mm;
  704. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  705. if (ea < VMALLOC_END)
  706. psize = mmu_vmalloc_psize;
  707. else
  708. psize = mmu_io_psize;
  709. ssize = mmu_kernel_ssize;
  710. break;
  711. default:
  712. /* Not a valid range
  713. * Send the problem up to do_page_fault
  714. */
  715. return 1;
  716. }
  717. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  718. /* Get pgdir */
  719. pgdir = mm->pgd;
  720. if (pgdir == NULL)
  721. return 1;
  722. /* Check CPU locality */
  723. tmp = cpumask_of_cpu(smp_processor_id());
  724. if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
  725. local = 1;
  726. #ifdef CONFIG_HUGETLB_PAGE
  727. /* Handle hugepage regions */
  728. if (HPAGE_SHIFT && psize == mmu_huge_psize) {
  729. DBG_LOW(" -> huge page !\n");
  730. return hash_huge_page(mm, access, ea, vsid, local, trap);
  731. }
  732. #endif /* CONFIG_HUGETLB_PAGE */
  733. #ifndef CONFIG_PPC_64K_PAGES
  734. /* If we use 4K pages and our psize is not 4K, then we are hitting
  735. * a special driver mapping, we need to align the address before
  736. * we fetch the PTE
  737. */
  738. if (psize != MMU_PAGE_4K)
  739. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  740. #endif /* CONFIG_PPC_64K_PAGES */
  741. /* Get PTE and page size from page tables */
  742. ptep = find_linux_pte(pgdir, ea);
  743. if (ptep == NULL || !pte_present(*ptep)) {
  744. DBG_LOW(" no PTE !\n");
  745. return 1;
  746. }
  747. #ifndef CONFIG_PPC_64K_PAGES
  748. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  749. #else
  750. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  751. pte_val(*(ptep + PTRS_PER_PTE)));
  752. #endif
  753. /* Pre-check access permissions (will be re-checked atomically
  754. * in __hash_page_XX but this pre-check is a fast path
  755. */
  756. if (access & ~pte_val(*ptep)) {
  757. DBG_LOW(" no access !\n");
  758. return 1;
  759. }
  760. /* Do actual hashing */
  761. #ifdef CONFIG_PPC_64K_PAGES
  762. /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
  763. if (pte_val(*ptep) & _PAGE_4K_PFN) {
  764. demote_segment_4k(mm, ea);
  765. psize = MMU_PAGE_4K;
  766. }
  767. /* If this PTE is non-cacheable and we have restrictions on
  768. * using non cacheable large pages, then we switch to 4k
  769. */
  770. if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
  771. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  772. if (user_region) {
  773. demote_segment_4k(mm, ea);
  774. psize = MMU_PAGE_4K;
  775. } else if (ea < VMALLOC_END) {
  776. /*
  777. * some driver did a non-cacheable mapping
  778. * in vmalloc space, so switch vmalloc
  779. * to 4k pages
  780. */
  781. printk(KERN_ALERT "Reducing vmalloc segment "
  782. "to 4kB pages because of "
  783. "non-cacheable mapping\n");
  784. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  785. #ifdef CONFIG_SPU_BASE
  786. spu_flush_all_slbs(mm);
  787. #endif
  788. }
  789. }
  790. if (user_region) {
  791. if (psize != get_paca()->context.user_psize) {
  792. get_paca()->context = mm->context;
  793. slb_flush_and_rebolt();
  794. }
  795. } else if (get_paca()->vmalloc_sllp !=
  796. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  797. get_paca()->vmalloc_sllp =
  798. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  799. slb_vmalloc_update();
  800. }
  801. #endif /* CONFIG_PPC_64K_PAGES */
  802. #ifdef CONFIG_PPC_HAS_HASH_64K
  803. if (psize == MMU_PAGE_64K)
  804. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  805. else
  806. #endif /* CONFIG_PPC_HAS_HASH_64K */
  807. {
  808. int spp = subpage_protection(pgdir, ea);
  809. if (access & spp)
  810. rc = -2;
  811. else
  812. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  813. local, ssize, spp);
  814. }
  815. #ifndef CONFIG_PPC_64K_PAGES
  816. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  817. #else
  818. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  819. pte_val(*(ptep + PTRS_PER_PTE)));
  820. #endif
  821. DBG_LOW(" -> rc=%d\n", rc);
  822. return rc;
  823. }
  824. EXPORT_SYMBOL_GPL(hash_page);
  825. void hash_preload(struct mm_struct *mm, unsigned long ea,
  826. unsigned long access, unsigned long trap)
  827. {
  828. unsigned long vsid;
  829. void *pgdir;
  830. pte_t *ptep;
  831. cpumask_t mask;
  832. unsigned long flags;
  833. int local = 0;
  834. int ssize;
  835. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  836. #ifdef CONFIG_PPC_MM_SLICES
  837. /* We only prefault standard pages for now */
  838. if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
  839. return;
  840. #endif
  841. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  842. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  843. /* Get Linux PTE if available */
  844. pgdir = mm->pgd;
  845. if (pgdir == NULL)
  846. return;
  847. ptep = find_linux_pte(pgdir, ea);
  848. if (!ptep)
  849. return;
  850. #ifdef CONFIG_PPC_64K_PAGES
  851. /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
  852. * a 64K kernel), then we don't preload, hash_page() will take
  853. * care of it once we actually try to access the page.
  854. * That way we don't have to duplicate all of the logic for segment
  855. * page size demotion here
  856. */
  857. if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
  858. return;
  859. #endif /* CONFIG_PPC_64K_PAGES */
  860. /* Get VSID */
  861. ssize = user_segment_size(ea);
  862. vsid = get_vsid(mm->context.id, ea, ssize);
  863. /* Hash doesn't like irqs */
  864. local_irq_save(flags);
  865. /* Is that local to this CPU ? */
  866. mask = cpumask_of_cpu(smp_processor_id());
  867. if (cpus_equal(mm->cpu_vm_mask, mask))
  868. local = 1;
  869. /* Hash it in */
  870. #ifdef CONFIG_PPC_HAS_HASH_64K
  871. if (mm->context.user_psize == MMU_PAGE_64K)
  872. __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  873. else
  874. #endif /* CONFIG_PPC_HAS_HASH_64K */
  875. __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
  876. subpage_protection(pgdir, ea));
  877. local_irq_restore(flags);
  878. }
  879. /* WARNING: This is called from hash_low_64.S, if you change this prototype,
  880. * do not forget to update the assembly call site !
  881. */
  882. void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
  883. int local)
  884. {
  885. unsigned long hash, index, shift, hidx, slot;
  886. DBG_LOW("flush_hash_page(va=%016x)\n", va);
  887. pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
  888. hash = hpt_hash(va, shift, ssize);
  889. hidx = __rpte_to_hidx(pte, index);
  890. if (hidx & _PTEIDX_SECONDARY)
  891. hash = ~hash;
  892. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  893. slot += hidx & _PTEIDX_GROUP_IX;
  894. DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
  895. ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
  896. } pte_iterate_hashed_end();
  897. }
  898. void flush_hash_range(unsigned long number, int local)
  899. {
  900. if (ppc_md.flush_hash_range)
  901. ppc_md.flush_hash_range(number, local);
  902. else {
  903. int i;
  904. struct ppc64_tlb_batch *batch =
  905. &__get_cpu_var(ppc64_tlb_batch);
  906. for (i = 0; i < number; i++)
  907. flush_hash_page(batch->vaddr[i], batch->pte[i],
  908. batch->psize, batch->ssize, local);
  909. }
  910. }
  911. /*
  912. * low_hash_fault is called when we the low level hash code failed
  913. * to instert a PTE due to an hypervisor error
  914. */
  915. void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
  916. {
  917. if (user_mode(regs)) {
  918. #ifdef CONFIG_PPC_SUBPAGE_PROT
  919. if (rc == -2)
  920. _exception(SIGSEGV, regs, SEGV_ACCERR, address);
  921. else
  922. #endif
  923. _exception(SIGBUS, regs, BUS_ADRERR, address);
  924. } else
  925. bad_page_fault(regs, address, SIGBUS);
  926. }
  927. #ifdef CONFIG_DEBUG_PAGEALLOC
  928. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  929. {
  930. unsigned long hash, hpteg;
  931. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  932. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  933. unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
  934. _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
  935. int ret;
  936. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  937. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  938. ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
  939. mode, HPTE_V_BOLTED,
  940. mmu_linear_psize, mmu_kernel_ssize);
  941. BUG_ON (ret < 0);
  942. spin_lock(&linear_map_hash_lock);
  943. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  944. linear_map_hash_slots[lmi] = ret | 0x80;
  945. spin_unlock(&linear_map_hash_lock);
  946. }
  947. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  948. {
  949. unsigned long hash, hidx, slot;
  950. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  951. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  952. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  953. spin_lock(&linear_map_hash_lock);
  954. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  955. hidx = linear_map_hash_slots[lmi] & 0x7f;
  956. linear_map_hash_slots[lmi] = 0;
  957. spin_unlock(&linear_map_hash_lock);
  958. if (hidx & _PTEIDX_SECONDARY)
  959. hash = ~hash;
  960. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  961. slot += hidx & _PTEIDX_GROUP_IX;
  962. ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
  963. }
  964. void kernel_map_pages(struct page *page, int numpages, int enable)
  965. {
  966. unsigned long flags, vaddr, lmi;
  967. int i;
  968. local_irq_save(flags);
  969. for (i = 0; i < numpages; i++, page++) {
  970. vaddr = (unsigned long)page_address(page);
  971. lmi = __pa(vaddr) >> PAGE_SHIFT;
  972. if (lmi >= linear_map_hash_count)
  973. continue;
  974. if (enable)
  975. kernel_map_linear_page(vaddr, lmi);
  976. else
  977. kernel_unmap_linear_page(vaddr, lmi);
  978. }
  979. local_irq_restore(flags);
  980. }
  981. #endif /* CONFIG_DEBUG_PAGEALLOC */