hash_utils_64.c 27 KB

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