hash_utils_64.c 28 KB

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