hash_utils_64.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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, limit;
  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. On cell we want it to be
  446. * in the first 1 Gig.
  447. */
  448. if (machine_is(cell))
  449. limit = 0x40000000;
  450. else
  451. limit = 0;
  452. table = lmb_alloc_base(htab_size_bytes, htab_size_bytes, limit);
  453. DBG("Hash table allocated at %lx, size: %lx\n", table,
  454. htab_size_bytes);
  455. htab_address = abs_to_virt(table);
  456. /* htab absolute addr + encoded htabsize */
  457. _SDR1 = table + __ilog2(pteg_count) - 11;
  458. /* Initialize the HPT with no entries */
  459. memset((void *)table, 0, htab_size_bytes);
  460. /* Set SDR1 */
  461. mtspr(SPRN_SDR1, _SDR1);
  462. }
  463. mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
  464. #ifdef CONFIG_DEBUG_PAGEALLOC
  465. linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
  466. linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
  467. 1, lmb.rmo_size));
  468. memset(linear_map_hash_slots, 0, linear_map_hash_count);
  469. #endif /* CONFIG_DEBUG_PAGEALLOC */
  470. /* On U3 based machines, we need to reserve the DART area and
  471. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  472. * cacheable later on
  473. */
  474. /* create bolted the linear mapping in the hash table */
  475. for (i=0; i < lmb.memory.cnt; i++) {
  476. base = (unsigned long)__va(lmb.memory.region[i].base);
  477. size = lmb.memory.region[i].size;
  478. DBG("creating mapping for region: %lx : %lx\n", base, size);
  479. #ifdef CONFIG_U3_DART
  480. /* Do not map the DART space. Fortunately, it will be aligned
  481. * in such a way that it will not cross two lmb regions and
  482. * will fit within a single 16Mb page.
  483. * The DART space is assumed to be a full 16Mb region even if
  484. * we only use 2Mb of that space. We will use more of it later
  485. * for AGP GART. We have to use a full 16Mb large page.
  486. */
  487. DBG("DART base: %lx\n", dart_tablebase);
  488. if (dart_tablebase != 0 && dart_tablebase >= base
  489. && dart_tablebase < (base + size)) {
  490. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  491. if (base != dart_tablebase)
  492. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  493. __pa(base), mode_rw,
  494. mmu_linear_psize,
  495. mmu_kernel_ssize));
  496. if ((base + size) > dart_table_end)
  497. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  498. base + size,
  499. __pa(dart_table_end),
  500. mode_rw,
  501. mmu_linear_psize,
  502. mmu_kernel_ssize));
  503. continue;
  504. }
  505. #endif /* CONFIG_U3_DART */
  506. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  507. mode_rw, mmu_linear_psize, mmu_kernel_ssize));
  508. }
  509. /*
  510. * If we have a memory_limit and we've allocated TCEs then we need to
  511. * explicitly map the TCE area at the top of RAM. We also cope with the
  512. * case that the TCEs start below memory_limit.
  513. * tce_alloc_start/end are 16MB aligned so the mapping should work
  514. * for either 4K or 16MB pages.
  515. */
  516. if (tce_alloc_start) {
  517. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  518. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  519. if (base + size >= tce_alloc_start)
  520. tce_alloc_start = base + size + 1;
  521. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  522. __pa(tce_alloc_start), mode_rw,
  523. mmu_linear_psize, mmu_kernel_ssize));
  524. }
  525. htab_finish_init();
  526. DBG(" <- htab_initialize()\n");
  527. }
  528. #undef KB
  529. #undef MB
  530. void htab_initialize_secondary(void)
  531. {
  532. if (!firmware_has_feature(FW_FEATURE_LPAR))
  533. mtspr(SPRN_SDR1, _SDR1);
  534. }
  535. /*
  536. * Called by asm hashtable.S for doing lazy icache flush
  537. */
  538. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  539. {
  540. struct page *page;
  541. if (!pfn_valid(pte_pfn(pte)))
  542. return pp;
  543. page = pte_page(pte);
  544. /* page is dirty */
  545. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  546. if (trap == 0x400) {
  547. __flush_dcache_icache(page_address(page));
  548. set_bit(PG_arch_1, &page->flags);
  549. } else
  550. pp |= HPTE_R_N;
  551. }
  552. return pp;
  553. }
  554. /*
  555. * Demote a segment to using 4k pages.
  556. * For now this makes the whole process use 4k pages.
  557. */
  558. #ifdef CONFIG_PPC_64K_PAGES
  559. void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  560. {
  561. if (mm->context.user_psize == MMU_PAGE_4K)
  562. return;
  563. slice_set_user_psize(mm, MMU_PAGE_4K);
  564. #ifdef CONFIG_SPU_BASE
  565. spu_flush_all_slbs(mm);
  566. #endif
  567. if (get_paca()->context.user_psize != MMU_PAGE_4K) {
  568. get_paca()->context = mm->context;
  569. slb_flush_and_rebolt();
  570. }
  571. }
  572. #endif /* CONFIG_PPC_64K_PAGES */
  573. #ifdef CONFIG_PPC_SUBPAGE_PROT
  574. /*
  575. * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
  576. * Userspace sets the subpage permissions using the subpage_prot system call.
  577. *
  578. * Result is 0: full permissions, _PAGE_RW: read-only,
  579. * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
  580. */
  581. static int subpage_protection(pgd_t *pgdir, unsigned long ea)
  582. {
  583. struct subpage_prot_table *spt = pgd_subpage_prot(pgdir);
  584. u32 spp = 0;
  585. u32 **sbpm, *sbpp;
  586. if (ea >= spt->maxaddr)
  587. return 0;
  588. if (ea < 0x100000000) {
  589. /* addresses below 4GB use spt->low_prot */
  590. sbpm = spt->low_prot;
  591. } else {
  592. sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
  593. if (!sbpm)
  594. return 0;
  595. }
  596. sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  597. if (!sbpp)
  598. return 0;
  599. spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
  600. /* extract 2-bit bitfield for this 4k subpage */
  601. spp >>= 30 - 2 * ((ea >> 12) & 0xf);
  602. /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
  603. spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
  604. return spp;
  605. }
  606. #else /* CONFIG_PPC_SUBPAGE_PROT */
  607. static inline int subpage_protection(pgd_t *pgdir, unsigned long ea)
  608. {
  609. return 0;
  610. }
  611. #endif
  612. /* Result code is:
  613. * 0 - handled
  614. * 1 - normal page fault
  615. * -1 - critical hash insertion error
  616. * -2 - access not permitted by subpage protection mechanism
  617. */
  618. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  619. {
  620. void *pgdir;
  621. unsigned long vsid;
  622. struct mm_struct *mm;
  623. pte_t *ptep;
  624. cpumask_t tmp;
  625. int rc, user_region = 0, local = 0;
  626. int psize, ssize;
  627. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  628. ea, access, trap);
  629. if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
  630. DBG_LOW(" out of pgtable range !\n");
  631. return 1;
  632. }
  633. /* Get region & vsid */
  634. switch (REGION_ID(ea)) {
  635. case USER_REGION_ID:
  636. user_region = 1;
  637. mm = current->mm;
  638. if (! mm) {
  639. DBG_LOW(" user region with no mm !\n");
  640. return 1;
  641. }
  642. #ifdef CONFIG_PPC_MM_SLICES
  643. psize = get_slice_psize(mm, ea);
  644. #else
  645. psize = mm->context.user_psize;
  646. #endif
  647. ssize = user_segment_size(ea);
  648. vsid = get_vsid(mm->context.id, ea, ssize);
  649. break;
  650. case VMALLOC_REGION_ID:
  651. mm = &init_mm;
  652. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  653. if (ea < VMALLOC_END)
  654. psize = mmu_vmalloc_psize;
  655. else
  656. psize = mmu_io_psize;
  657. ssize = mmu_kernel_ssize;
  658. break;
  659. default:
  660. /* Not a valid range
  661. * Send the problem up to do_page_fault
  662. */
  663. return 1;
  664. }
  665. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  666. /* Get pgdir */
  667. pgdir = mm->pgd;
  668. if (pgdir == NULL)
  669. return 1;
  670. /* Check CPU locality */
  671. tmp = cpumask_of_cpu(smp_processor_id());
  672. if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
  673. local = 1;
  674. #ifdef CONFIG_HUGETLB_PAGE
  675. /* Handle hugepage regions */
  676. if (HPAGE_SHIFT && psize == mmu_huge_psize) {
  677. DBG_LOW(" -> huge page !\n");
  678. return hash_huge_page(mm, access, ea, vsid, local, trap);
  679. }
  680. #endif /* CONFIG_HUGETLB_PAGE */
  681. #ifndef CONFIG_PPC_64K_PAGES
  682. /* If we use 4K pages and our psize is not 4K, then we are hitting
  683. * a special driver mapping, we need to align the address before
  684. * we fetch the PTE
  685. */
  686. if (psize != MMU_PAGE_4K)
  687. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  688. #endif /* CONFIG_PPC_64K_PAGES */
  689. /* Get PTE and page size from page tables */
  690. ptep = find_linux_pte(pgdir, ea);
  691. if (ptep == NULL || !pte_present(*ptep)) {
  692. DBG_LOW(" no PTE !\n");
  693. return 1;
  694. }
  695. #ifndef CONFIG_PPC_64K_PAGES
  696. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  697. #else
  698. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  699. pte_val(*(ptep + PTRS_PER_PTE)));
  700. #endif
  701. /* Pre-check access permissions (will be re-checked atomically
  702. * in __hash_page_XX but this pre-check is a fast path
  703. */
  704. if (access & ~pte_val(*ptep)) {
  705. DBG_LOW(" no access !\n");
  706. return 1;
  707. }
  708. /* Do actual hashing */
  709. #ifdef CONFIG_PPC_64K_PAGES
  710. /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
  711. if (pte_val(*ptep) & _PAGE_4K_PFN) {
  712. demote_segment_4k(mm, ea);
  713. psize = MMU_PAGE_4K;
  714. }
  715. /* If this PTE is non-cacheable and we have restrictions on
  716. * using non cacheable large pages, then we switch to 4k
  717. */
  718. if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
  719. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  720. if (user_region) {
  721. demote_segment_4k(mm, ea);
  722. psize = MMU_PAGE_4K;
  723. } else if (ea < VMALLOC_END) {
  724. /*
  725. * some driver did a non-cacheable mapping
  726. * in vmalloc space, so switch vmalloc
  727. * to 4k pages
  728. */
  729. printk(KERN_ALERT "Reducing vmalloc segment "
  730. "to 4kB pages because of "
  731. "non-cacheable mapping\n");
  732. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  733. #ifdef CONFIG_SPU_BASE
  734. spu_flush_all_slbs(mm);
  735. #endif
  736. }
  737. }
  738. if (user_region) {
  739. if (psize != get_paca()->context.user_psize) {
  740. get_paca()->context = mm->context;
  741. slb_flush_and_rebolt();
  742. }
  743. } else if (get_paca()->vmalloc_sllp !=
  744. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  745. get_paca()->vmalloc_sllp =
  746. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  747. slb_vmalloc_update();
  748. }
  749. #endif /* CONFIG_PPC_64K_PAGES */
  750. #ifdef CONFIG_PPC_HAS_HASH_64K
  751. if (psize == MMU_PAGE_64K)
  752. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  753. else
  754. #endif /* CONFIG_PPC_HAS_HASH_64K */
  755. {
  756. int spp = subpage_protection(pgdir, ea);
  757. if (access & spp)
  758. rc = -2;
  759. else
  760. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  761. local, ssize, spp);
  762. }
  763. #ifndef CONFIG_PPC_64K_PAGES
  764. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  765. #else
  766. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  767. pte_val(*(ptep + PTRS_PER_PTE)));
  768. #endif
  769. DBG_LOW(" -> rc=%d\n", rc);
  770. return rc;
  771. }
  772. EXPORT_SYMBOL_GPL(hash_page);
  773. void hash_preload(struct mm_struct *mm, unsigned long ea,
  774. unsigned long access, unsigned long trap)
  775. {
  776. unsigned long vsid;
  777. void *pgdir;
  778. pte_t *ptep;
  779. cpumask_t mask;
  780. unsigned long flags;
  781. int local = 0;
  782. int ssize;
  783. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  784. #ifdef CONFIG_PPC_MM_SLICES
  785. /* We only prefault standard pages for now */
  786. if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
  787. return;
  788. #endif
  789. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  790. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  791. /* Get Linux PTE if available */
  792. pgdir = mm->pgd;
  793. if (pgdir == NULL)
  794. return;
  795. ptep = find_linux_pte(pgdir, ea);
  796. if (!ptep)
  797. return;
  798. #ifdef CONFIG_PPC_64K_PAGES
  799. /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
  800. * a 64K kernel), then we don't preload, hash_page() will take
  801. * care of it once we actually try to access the page.
  802. * That way we don't have to duplicate all of the logic for segment
  803. * page size demotion here
  804. */
  805. if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
  806. return;
  807. #endif /* CONFIG_PPC_64K_PAGES */
  808. /* Get VSID */
  809. ssize = user_segment_size(ea);
  810. vsid = get_vsid(mm->context.id, ea, ssize);
  811. /* Hash doesn't like irqs */
  812. local_irq_save(flags);
  813. /* Is that local to this CPU ? */
  814. mask = cpumask_of_cpu(smp_processor_id());
  815. if (cpus_equal(mm->cpu_vm_mask, mask))
  816. local = 1;
  817. /* Hash it in */
  818. #ifdef CONFIG_PPC_HAS_HASH_64K
  819. if (mm->context.user_psize == MMU_PAGE_64K)
  820. __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  821. else
  822. #endif /* CONFIG_PPC_HAS_HASH_64K */
  823. __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
  824. subpage_protection(pgdir, ea));
  825. local_irq_restore(flags);
  826. }
  827. /* WARNING: This is called from hash_low_64.S, if you change this prototype,
  828. * do not forget to update the assembly call site !
  829. */
  830. void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
  831. int local)
  832. {
  833. unsigned long hash, index, shift, hidx, slot;
  834. DBG_LOW("flush_hash_page(va=%016x)\n", va);
  835. pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
  836. hash = hpt_hash(va, shift, ssize);
  837. hidx = __rpte_to_hidx(pte, index);
  838. if (hidx & _PTEIDX_SECONDARY)
  839. hash = ~hash;
  840. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  841. slot += hidx & _PTEIDX_GROUP_IX;
  842. DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
  843. ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
  844. } pte_iterate_hashed_end();
  845. }
  846. void flush_hash_range(unsigned long number, int local)
  847. {
  848. if (ppc_md.flush_hash_range)
  849. ppc_md.flush_hash_range(number, local);
  850. else {
  851. int i;
  852. struct ppc64_tlb_batch *batch =
  853. &__get_cpu_var(ppc64_tlb_batch);
  854. for (i = 0; i < number; i++)
  855. flush_hash_page(batch->vaddr[i], batch->pte[i],
  856. batch->psize, batch->ssize, local);
  857. }
  858. }
  859. /*
  860. * low_hash_fault is called when we the low level hash code failed
  861. * to instert a PTE due to an hypervisor error
  862. */
  863. void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
  864. {
  865. if (user_mode(regs)) {
  866. #ifdef CONFIG_PPC_SUBPAGE_PROT
  867. if (rc == -2)
  868. _exception(SIGSEGV, regs, SEGV_ACCERR, address);
  869. else
  870. #endif
  871. _exception(SIGBUS, regs, BUS_ADRERR, address);
  872. } else
  873. bad_page_fault(regs, address, SIGBUS);
  874. }
  875. #ifdef CONFIG_DEBUG_PAGEALLOC
  876. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  877. {
  878. unsigned long hash, hpteg;
  879. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  880. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  881. unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
  882. _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
  883. int ret;
  884. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  885. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  886. ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
  887. mode, HPTE_V_BOLTED,
  888. mmu_linear_psize, mmu_kernel_ssize);
  889. BUG_ON (ret < 0);
  890. spin_lock(&linear_map_hash_lock);
  891. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  892. linear_map_hash_slots[lmi] = ret | 0x80;
  893. spin_unlock(&linear_map_hash_lock);
  894. }
  895. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  896. {
  897. unsigned long hash, hidx, slot;
  898. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  899. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  900. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  901. spin_lock(&linear_map_hash_lock);
  902. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  903. hidx = linear_map_hash_slots[lmi] & 0x7f;
  904. linear_map_hash_slots[lmi] = 0;
  905. spin_unlock(&linear_map_hash_lock);
  906. if (hidx & _PTEIDX_SECONDARY)
  907. hash = ~hash;
  908. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  909. slot += hidx & _PTEIDX_GROUP_IX;
  910. ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
  911. }
  912. void kernel_map_pages(struct page *page, int numpages, int enable)
  913. {
  914. unsigned long flags, vaddr, lmi;
  915. int i;
  916. local_irq_save(flags);
  917. for (i = 0; i < numpages; i++, page++) {
  918. vaddr = (unsigned long)page_address(page);
  919. lmi = __pa(vaddr) >> PAGE_SHIFT;
  920. if (lmi >= linear_map_hash_count)
  921. continue;
  922. if (enable)
  923. kernel_map_linear_page(vaddr, lmi);
  924. else
  925. kernel_unmap_linear_page(vaddr, lmi);
  926. }
  927. local_irq_restore(flags);
  928. }
  929. #endif /* CONFIG_DEBUG_PAGEALLOC */