hash_utils_64.c 28 KB

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