hash_utils_64.c 28 KB

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