hash_utils_64.c 26 KB

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