hash_utils_64.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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/export.h>
  29. #include <linux/ctype.h>
  30. #include <linux/cache.h>
  31. #include <linux/init.h>
  32. #include <linux/signal.h>
  33. #include <linux/memblock.h>
  34. #include <linux/context_tracking.h>
  35. #include <asm/processor.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/mmu.h>
  38. #include <asm/mmu_context.h>
  39. #include <asm/page.h>
  40. #include <asm/types.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/machdep.h>
  43. #include <asm/prom.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/copro.h>
  52. #include <asm/udbg.h>
  53. #include <asm/code-patching.h>
  54. #include <asm/fadump.h>
  55. #include <asm/firmware.h>
  56. #include <asm/tm.h>
  57. #include <asm/trace.h>
  58. #ifdef DEBUG
  59. #define DBG(fmt...) udbg_printf(fmt)
  60. #else
  61. #define DBG(fmt...)
  62. #endif
  63. #ifdef DEBUG_LOW
  64. #define DBG_LOW(fmt...) udbg_printf(fmt)
  65. #else
  66. #define DBG_LOW(fmt...)
  67. #endif
  68. #define KB (1024)
  69. #define MB (1024*KB)
  70. #define GB (1024L*MB)
  71. /*
  72. * Note: pte --> Linux PTE
  73. * HPTE --> PowerPC Hashed Page Table Entry
  74. *
  75. * Execution context:
  76. * htab_initialize is called with the MMU off (of course), but
  77. * the kernel has been copied down to zero so it can directly
  78. * reference global data. At this point it is very difficult
  79. * to print debug info.
  80. *
  81. */
  82. #ifdef CONFIG_U3_DART
  83. extern unsigned long dart_tablebase;
  84. #endif /* CONFIG_U3_DART */
  85. static unsigned long _SDR1;
  86. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
  87. EXPORT_SYMBOL_GPL(mmu_psize_defs);
  88. struct hash_pte *htab_address;
  89. unsigned long htab_size_bytes;
  90. unsigned long htab_hash_mask;
  91. EXPORT_SYMBOL_GPL(htab_hash_mask);
  92. int mmu_linear_psize = MMU_PAGE_4K;
  93. EXPORT_SYMBOL_GPL(mmu_linear_psize);
  94. int mmu_virtual_psize = MMU_PAGE_4K;
  95. int mmu_vmalloc_psize = MMU_PAGE_4K;
  96. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  97. int mmu_vmemmap_psize = MMU_PAGE_4K;
  98. #endif
  99. int mmu_io_psize = MMU_PAGE_4K;
  100. int mmu_kernel_ssize = MMU_SEGSIZE_256M;
  101. EXPORT_SYMBOL_GPL(mmu_kernel_ssize);
  102. int mmu_highuser_ssize = MMU_SEGSIZE_256M;
  103. u16 mmu_slb_size = 64;
  104. EXPORT_SYMBOL_GPL(mmu_slb_size);
  105. #ifdef CONFIG_PPC_64K_PAGES
  106. int mmu_ci_restrictions;
  107. #endif
  108. #ifdef CONFIG_DEBUG_PAGEALLOC
  109. static u8 *linear_map_hash_slots;
  110. static unsigned long linear_map_hash_count;
  111. static DEFINE_SPINLOCK(linear_map_hash_lock);
  112. #endif /* CONFIG_DEBUG_PAGEALLOC */
  113. /* There are definitions of page sizes arrays to be used when none
  114. * is provided by the firmware.
  115. */
  116. /* Pre-POWER4 CPUs (4k pages only)
  117. */
  118. static struct mmu_psize_def mmu_psize_defaults_old[] = {
  119. [MMU_PAGE_4K] = {
  120. .shift = 12,
  121. .sllp = 0,
  122. .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
  123. .avpnm = 0,
  124. .tlbiel = 0,
  125. },
  126. };
  127. /* POWER4, GPUL, POWER5
  128. *
  129. * Support for 16Mb large pages
  130. */
  131. static struct mmu_psize_def mmu_psize_defaults_gp[] = {
  132. [MMU_PAGE_4K] = {
  133. .shift = 12,
  134. .sllp = 0,
  135. .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
  136. .avpnm = 0,
  137. .tlbiel = 1,
  138. },
  139. [MMU_PAGE_16M] = {
  140. .shift = 24,
  141. .sllp = SLB_VSID_L,
  142. .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
  143. [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
  144. .avpnm = 0x1UL,
  145. .tlbiel = 0,
  146. },
  147. };
  148. /*
  149. * 'R' and 'C' update notes:
  150. * - Under pHyp or KVM, the updatepp path will not set C, thus it *will*
  151. * create writeable HPTEs without C set, because the hcall H_PROTECT
  152. * that we use in that case will not update C
  153. * - The above is however not a problem, because we also don't do that
  154. * fancy "no flush" variant of eviction and we use H_REMOVE which will
  155. * do the right thing and thus we don't have the race I described earlier
  156. *
  157. * - Under bare metal, we do have the race, so we need R and C set
  158. * - We make sure R is always set and never lost
  159. * - C is _PAGE_DIRTY, and *should* always be set for a writeable mapping
  160. */
  161. unsigned long htab_convert_pte_flags(unsigned long pteflags)
  162. {
  163. unsigned long rflags = 0;
  164. /* _PAGE_EXEC -> NOEXEC */
  165. if ((pteflags & _PAGE_EXEC) == 0)
  166. rflags |= HPTE_R_N;
  167. /*
  168. * PPP bits:
  169. * Linux uses slb key 0 for kernel and 1 for user.
  170. * kernel RW areas are mapped with PPP=0b000
  171. * User area is mapped with PPP=0b010 for read/write
  172. * or PPP=0b011 for read-only (including writeable but clean pages).
  173. */
  174. if (pteflags & _PAGE_PRIVILEGED) {
  175. /*
  176. * Kernel read only mapped with ppp bits 0b110
  177. */
  178. if (!(pteflags & _PAGE_WRITE))
  179. rflags |= (HPTE_R_PP0 | 0x2);
  180. } else {
  181. if (pteflags & _PAGE_RWX)
  182. rflags |= 0x2;
  183. if (!((pteflags & _PAGE_WRITE) && (pteflags & _PAGE_DIRTY)))
  184. rflags |= 0x1;
  185. }
  186. /*
  187. * We can't allow hardware to update hpte bits. Hence always
  188. * set 'R' bit and set 'C' if it is a write fault
  189. * Memory coherence is always enabled
  190. */
  191. rflags |= HPTE_R_R | HPTE_R_M;
  192. if (pteflags & _PAGE_DIRTY)
  193. rflags |= HPTE_R_C;
  194. /*
  195. * Add in WIG bits
  196. */
  197. if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_TOLERANT)
  198. rflags |= HPTE_R_I;
  199. if ((pteflags & _PAGE_CACHE_CTL ) == _PAGE_NON_IDEMPOTENT)
  200. rflags |= (HPTE_R_I | HPTE_R_G);
  201. if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_SAO)
  202. rflags |= (HPTE_R_I | HPTE_R_W);
  203. return rflags;
  204. }
  205. int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
  206. unsigned long pstart, unsigned long prot,
  207. int psize, int ssize)
  208. {
  209. unsigned long vaddr, paddr;
  210. unsigned int step, shift;
  211. int ret = 0;
  212. shift = mmu_psize_defs[psize].shift;
  213. step = 1 << shift;
  214. prot = htab_convert_pte_flags(prot);
  215. DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
  216. vstart, vend, pstart, prot, psize, ssize);
  217. for (vaddr = vstart, paddr = pstart; vaddr < vend;
  218. vaddr += step, paddr += step) {
  219. unsigned long hash, hpteg;
  220. unsigned long vsid = get_kernel_vsid(vaddr, ssize);
  221. unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
  222. unsigned long tprot = prot;
  223. /*
  224. * If we hit a bad address return error.
  225. */
  226. if (!vsid)
  227. return -1;
  228. /* Make kernel text executable */
  229. if (overlaps_kernel_text(vaddr, vaddr + step))
  230. tprot &= ~HPTE_R_N;
  231. /* Make kvm guest trampolines executable */
  232. if (overlaps_kvm_tmp(vaddr, vaddr + step))
  233. tprot &= ~HPTE_R_N;
  234. /*
  235. * If relocatable, check if it overlaps interrupt vectors that
  236. * are copied down to real 0. For relocatable kernel
  237. * (e.g. kdump case) we copy interrupt vectors down to real
  238. * address 0. Mark that region as executable. This is
  239. * because on p8 system with relocation on exception feature
  240. * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
  241. * in order to execute the interrupt handlers in virtual
  242. * mode the vector region need to be marked as executable.
  243. */
  244. if ((PHYSICAL_START > MEMORY_START) &&
  245. overlaps_interrupt_vector_text(vaddr, vaddr + step))
  246. tprot &= ~HPTE_R_N;
  247. hash = hpt_hash(vpn, shift, ssize);
  248. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  249. BUG_ON(!ppc_md.hpte_insert);
  250. ret = ppc_md.hpte_insert(hpteg, vpn, paddr, tprot,
  251. HPTE_V_BOLTED, psize, psize, ssize);
  252. if (ret < 0)
  253. break;
  254. #ifdef CONFIG_DEBUG_PAGEALLOC
  255. if (debug_pagealloc_enabled() &&
  256. (paddr >> PAGE_SHIFT) < linear_map_hash_count)
  257. linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
  258. #endif /* CONFIG_DEBUG_PAGEALLOC */
  259. }
  260. return ret < 0 ? ret : 0;
  261. }
  262. int htab_remove_mapping(unsigned long vstart, unsigned long vend,
  263. int psize, int ssize)
  264. {
  265. unsigned long vaddr;
  266. unsigned int step, shift;
  267. int rc;
  268. int ret = 0;
  269. shift = mmu_psize_defs[psize].shift;
  270. step = 1 << shift;
  271. if (!ppc_md.hpte_removebolted)
  272. return -ENODEV;
  273. for (vaddr = vstart; vaddr < vend; vaddr += step) {
  274. rc = ppc_md.hpte_removebolted(vaddr, psize, ssize);
  275. if (rc == -ENOENT) {
  276. ret = -ENOENT;
  277. continue;
  278. }
  279. if (rc < 0)
  280. return rc;
  281. }
  282. return ret;
  283. }
  284. static int __init htab_dt_scan_seg_sizes(unsigned long node,
  285. const char *uname, int depth,
  286. void *data)
  287. {
  288. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  289. const __be32 *prop;
  290. int size = 0;
  291. /* We are scanning "cpu" nodes only */
  292. if (type == NULL || strcmp(type, "cpu") != 0)
  293. return 0;
  294. prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
  295. if (prop == NULL)
  296. return 0;
  297. for (; size >= 4; size -= 4, ++prop) {
  298. if (be32_to_cpu(prop[0]) == 40) {
  299. DBG("1T segment support detected\n");
  300. cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
  301. return 1;
  302. }
  303. }
  304. cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
  305. return 0;
  306. }
  307. static void __init htab_init_seg_sizes(void)
  308. {
  309. of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
  310. }
  311. static int __init get_idx_from_shift(unsigned int shift)
  312. {
  313. int idx = -1;
  314. switch (shift) {
  315. case 0xc:
  316. idx = MMU_PAGE_4K;
  317. break;
  318. case 0x10:
  319. idx = MMU_PAGE_64K;
  320. break;
  321. case 0x14:
  322. idx = MMU_PAGE_1M;
  323. break;
  324. case 0x18:
  325. idx = MMU_PAGE_16M;
  326. break;
  327. case 0x22:
  328. idx = MMU_PAGE_16G;
  329. break;
  330. }
  331. return idx;
  332. }
  333. static int __init htab_dt_scan_page_sizes(unsigned long node,
  334. const char *uname, int depth,
  335. void *data)
  336. {
  337. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  338. const __be32 *prop;
  339. int size = 0;
  340. /* We are scanning "cpu" nodes only */
  341. if (type == NULL || strcmp(type, "cpu") != 0)
  342. return 0;
  343. prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
  344. if (!prop)
  345. return 0;
  346. pr_info("Page sizes from device-tree:\n");
  347. size /= 4;
  348. cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
  349. while(size > 0) {
  350. unsigned int base_shift = be32_to_cpu(prop[0]);
  351. unsigned int slbenc = be32_to_cpu(prop[1]);
  352. unsigned int lpnum = be32_to_cpu(prop[2]);
  353. struct mmu_psize_def *def;
  354. int idx, base_idx;
  355. size -= 3; prop += 3;
  356. base_idx = get_idx_from_shift(base_shift);
  357. if (base_idx < 0) {
  358. /* skip the pte encoding also */
  359. prop += lpnum * 2; size -= lpnum * 2;
  360. continue;
  361. }
  362. def = &mmu_psize_defs[base_idx];
  363. if (base_idx == MMU_PAGE_16M)
  364. cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
  365. def->shift = base_shift;
  366. if (base_shift <= 23)
  367. def->avpnm = 0;
  368. else
  369. def->avpnm = (1 << (base_shift - 23)) - 1;
  370. def->sllp = slbenc;
  371. /*
  372. * We don't know for sure what's up with tlbiel, so
  373. * for now we only set it for 4K and 64K pages
  374. */
  375. if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
  376. def->tlbiel = 1;
  377. else
  378. def->tlbiel = 0;
  379. while (size > 0 && lpnum) {
  380. unsigned int shift = be32_to_cpu(prop[0]);
  381. int penc = be32_to_cpu(prop[1]);
  382. prop += 2; size -= 2;
  383. lpnum--;
  384. idx = get_idx_from_shift(shift);
  385. if (idx < 0)
  386. continue;
  387. if (penc == -1)
  388. pr_err("Invalid penc for base_shift=%d "
  389. "shift=%d\n", base_shift, shift);
  390. def->penc[idx] = penc;
  391. pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
  392. " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
  393. base_shift, shift, def->sllp,
  394. def->avpnm, def->tlbiel, def->penc[idx]);
  395. }
  396. }
  397. return 1;
  398. }
  399. #ifdef CONFIG_HUGETLB_PAGE
  400. /* Scan for 16G memory blocks that have been set aside for huge pages
  401. * and reserve those blocks for 16G huge pages.
  402. */
  403. static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
  404. const char *uname, int depth,
  405. void *data) {
  406. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  407. const __be64 *addr_prop;
  408. const __be32 *page_count_prop;
  409. unsigned int expected_pages;
  410. long unsigned int phys_addr;
  411. long unsigned int block_size;
  412. /* We are scanning "memory" nodes only */
  413. if (type == NULL || strcmp(type, "memory") != 0)
  414. return 0;
  415. /* This property is the log base 2 of the number of virtual pages that
  416. * will represent this memory block. */
  417. page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
  418. if (page_count_prop == NULL)
  419. return 0;
  420. expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
  421. addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
  422. if (addr_prop == NULL)
  423. return 0;
  424. phys_addr = be64_to_cpu(addr_prop[0]);
  425. block_size = be64_to_cpu(addr_prop[1]);
  426. if (block_size != (16 * GB))
  427. return 0;
  428. printk(KERN_INFO "Huge page(16GB) memory: "
  429. "addr = 0x%lX size = 0x%lX pages = %d\n",
  430. phys_addr, block_size, expected_pages);
  431. if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
  432. memblock_reserve(phys_addr, block_size * expected_pages);
  433. add_gpage(phys_addr, block_size, expected_pages);
  434. }
  435. return 0;
  436. }
  437. #endif /* CONFIG_HUGETLB_PAGE */
  438. static void mmu_psize_set_default_penc(void)
  439. {
  440. int bpsize, apsize;
  441. for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
  442. for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
  443. mmu_psize_defs[bpsize].penc[apsize] = -1;
  444. }
  445. #ifdef CONFIG_PPC_64K_PAGES
  446. static bool might_have_hea(void)
  447. {
  448. /*
  449. * The HEA ethernet adapter requires awareness of the
  450. * GX bus. Without that awareness we can easily assume
  451. * we will never see an HEA ethernet device.
  452. */
  453. #ifdef CONFIG_IBMEBUS
  454. return !cpu_has_feature(CPU_FTR_ARCH_207S);
  455. #else
  456. return false;
  457. #endif
  458. }
  459. #endif /* #ifdef CONFIG_PPC_64K_PAGES */
  460. static void __init htab_init_page_sizes(void)
  461. {
  462. int rc;
  463. /* se the invalid penc to -1 */
  464. mmu_psize_set_default_penc();
  465. /* Default to 4K pages only */
  466. memcpy(mmu_psize_defs, mmu_psize_defaults_old,
  467. sizeof(mmu_psize_defaults_old));
  468. /*
  469. * Try to find the available page sizes in the device-tree
  470. */
  471. rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
  472. if (rc != 0) /* Found */
  473. goto found;
  474. /*
  475. * Not in the device-tree, let's fallback on known size
  476. * list for 16M capable GP & GR
  477. */
  478. if (mmu_has_feature(MMU_FTR_16M_PAGE))
  479. memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
  480. sizeof(mmu_psize_defaults_gp));
  481. found:
  482. if (!debug_pagealloc_enabled()) {
  483. /*
  484. * Pick a size for the linear mapping. Currently, we only
  485. * support 16M, 1M and 4K which is the default
  486. */
  487. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  488. mmu_linear_psize = MMU_PAGE_16M;
  489. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  490. mmu_linear_psize = MMU_PAGE_1M;
  491. }
  492. #ifdef CONFIG_PPC_64K_PAGES
  493. /*
  494. * Pick a size for the ordinary pages. Default is 4K, we support
  495. * 64K for user mappings and vmalloc if supported by the processor.
  496. * We only use 64k for ioremap if the processor
  497. * (and firmware) support cache-inhibited large pages.
  498. * If not, we use 4k and set mmu_ci_restrictions so that
  499. * hash_page knows to switch processes that use cache-inhibited
  500. * mappings to 4k pages.
  501. */
  502. if (mmu_psize_defs[MMU_PAGE_64K].shift) {
  503. mmu_virtual_psize = MMU_PAGE_64K;
  504. mmu_vmalloc_psize = MMU_PAGE_64K;
  505. if (mmu_linear_psize == MMU_PAGE_4K)
  506. mmu_linear_psize = MMU_PAGE_64K;
  507. if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
  508. /*
  509. * When running on pSeries using 64k pages for ioremap
  510. * would stop us accessing the HEA ethernet. So if we
  511. * have the chance of ever seeing one, stay at 4k.
  512. */
  513. if (!might_have_hea() || !machine_is(pseries))
  514. mmu_io_psize = MMU_PAGE_64K;
  515. } else
  516. mmu_ci_restrictions = 1;
  517. }
  518. #endif /* CONFIG_PPC_64K_PAGES */
  519. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  520. /* We try to use 16M pages for vmemmap if that is supported
  521. * and we have at least 1G of RAM at boot
  522. */
  523. if (mmu_psize_defs[MMU_PAGE_16M].shift &&
  524. memblock_phys_mem_size() >= 0x40000000)
  525. mmu_vmemmap_psize = MMU_PAGE_16M;
  526. else if (mmu_psize_defs[MMU_PAGE_64K].shift)
  527. mmu_vmemmap_psize = MMU_PAGE_64K;
  528. else
  529. mmu_vmemmap_psize = MMU_PAGE_4K;
  530. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  531. printk(KERN_DEBUG "Page orders: linear mapping = %d, "
  532. "virtual = %d, io = %d"
  533. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  534. ", vmemmap = %d"
  535. #endif
  536. "\n",
  537. mmu_psize_defs[mmu_linear_psize].shift,
  538. mmu_psize_defs[mmu_virtual_psize].shift,
  539. mmu_psize_defs[mmu_io_psize].shift
  540. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  541. ,mmu_psize_defs[mmu_vmemmap_psize].shift
  542. #endif
  543. );
  544. #ifdef CONFIG_HUGETLB_PAGE
  545. /* Reserve 16G huge page memory sections for huge pages */
  546. of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
  547. #endif /* CONFIG_HUGETLB_PAGE */
  548. }
  549. static int __init htab_dt_scan_pftsize(unsigned long node,
  550. const char *uname, int depth,
  551. void *data)
  552. {
  553. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  554. const __be32 *prop;
  555. /* We are scanning "cpu" nodes only */
  556. if (type == NULL || strcmp(type, "cpu") != 0)
  557. return 0;
  558. prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
  559. if (prop != NULL) {
  560. /* pft_size[0] is the NUMA CEC cookie */
  561. ppc64_pft_size = be32_to_cpu(prop[1]);
  562. return 1;
  563. }
  564. return 0;
  565. }
  566. unsigned htab_shift_for_mem_size(unsigned long mem_size)
  567. {
  568. unsigned memshift = __ilog2(mem_size);
  569. unsigned pshift = mmu_psize_defs[mmu_virtual_psize].shift;
  570. unsigned pteg_shift;
  571. /* round mem_size up to next power of 2 */
  572. if ((1UL << memshift) < mem_size)
  573. memshift += 1;
  574. /* aim for 2 pages / pteg */
  575. pteg_shift = memshift - (pshift + 1);
  576. /*
  577. * 2^11 PTEGS of 128 bytes each, ie. 2^18 bytes is the minimum htab
  578. * size permitted by the architecture.
  579. */
  580. return max(pteg_shift + 7, 18U);
  581. }
  582. static unsigned long __init htab_get_table_size(void)
  583. {
  584. /* If hash size isn't already provided by the platform, we try to
  585. * retrieve it from the device-tree. If it's not there neither, we
  586. * calculate it now based on the total RAM size
  587. */
  588. if (ppc64_pft_size == 0)
  589. of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
  590. if (ppc64_pft_size)
  591. return 1UL << ppc64_pft_size;
  592. return 1UL << htab_shift_for_mem_size(memblock_phys_mem_size());
  593. }
  594. #ifdef CONFIG_MEMORY_HOTPLUG
  595. int create_section_mapping(unsigned long start, unsigned long end)
  596. {
  597. int rc = htab_bolt_mapping(start, end, __pa(start),
  598. pgprot_val(PAGE_KERNEL), mmu_linear_psize,
  599. mmu_kernel_ssize);
  600. if (rc < 0) {
  601. int rc2 = htab_remove_mapping(start, end, mmu_linear_psize,
  602. mmu_kernel_ssize);
  603. BUG_ON(rc2 && (rc2 != -ENOENT));
  604. }
  605. return rc;
  606. }
  607. int remove_section_mapping(unsigned long start, unsigned long end)
  608. {
  609. int rc = htab_remove_mapping(start, end, mmu_linear_psize,
  610. mmu_kernel_ssize);
  611. WARN_ON(rc < 0);
  612. return rc;
  613. }
  614. #endif /* CONFIG_MEMORY_HOTPLUG */
  615. static void __init hash_init_partition_table(phys_addr_t hash_table,
  616. unsigned long pteg_count)
  617. {
  618. unsigned long ps_field;
  619. unsigned long htab_size;
  620. unsigned long patb_size = 1UL << PATB_SIZE_SHIFT;
  621. /*
  622. * slb llp encoding for the page size used in VPM real mode.
  623. * We can ignore that for lpid 0
  624. */
  625. ps_field = 0;
  626. htab_size = __ilog2(pteg_count) - 11;
  627. BUILD_BUG_ON_MSG((PATB_SIZE_SHIFT > 24), "Partition table size too large.");
  628. partition_tb = __va(memblock_alloc_base(patb_size, patb_size,
  629. MEMBLOCK_ALLOC_ANYWHERE));
  630. /* Initialize the Partition Table with no entries */
  631. memset((void *)partition_tb, 0, patb_size);
  632. partition_tb->patb0 = cpu_to_be64(ps_field | hash_table | htab_size);
  633. /*
  634. * FIXME!! This should be done via update_partition table
  635. * For now UPRT is 0 for us.
  636. */
  637. partition_tb->patb1 = 0;
  638. DBG("Partition table %p\n", partition_tb);
  639. /*
  640. * update partition table control register,
  641. * 64 K size.
  642. */
  643. mtspr(SPRN_PTCR, __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
  644. }
  645. static void __init htab_initialize(void)
  646. {
  647. unsigned long table;
  648. unsigned long pteg_count;
  649. unsigned long prot;
  650. unsigned long base = 0, size = 0, limit;
  651. struct memblock_region *reg;
  652. DBG(" -> htab_initialize()\n");
  653. /* Initialize segment sizes */
  654. htab_init_seg_sizes();
  655. /* Initialize page sizes */
  656. htab_init_page_sizes();
  657. if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
  658. mmu_kernel_ssize = MMU_SEGSIZE_1T;
  659. mmu_highuser_ssize = MMU_SEGSIZE_1T;
  660. printk(KERN_INFO "Using 1TB segments\n");
  661. }
  662. /*
  663. * Calculate the required size of the htab. We want the number of
  664. * PTEGs to equal one half the number of real pages.
  665. */
  666. htab_size_bytes = htab_get_table_size();
  667. pteg_count = htab_size_bytes >> 7;
  668. htab_hash_mask = pteg_count - 1;
  669. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  670. /* Using a hypervisor which owns the htab */
  671. htab_address = NULL;
  672. _SDR1 = 0;
  673. #ifdef CONFIG_FA_DUMP
  674. /*
  675. * If firmware assisted dump is active firmware preserves
  676. * the contents of htab along with entire partition memory.
  677. * Clear the htab if firmware assisted dump is active so
  678. * that we dont end up using old mappings.
  679. */
  680. if (is_fadump_active() && ppc_md.hpte_clear_all)
  681. ppc_md.hpte_clear_all();
  682. #endif
  683. } else {
  684. /* Find storage for the HPT. Must be contiguous in
  685. * the absolute address space. On cell we want it to be
  686. * in the first 2 Gig so we can use it for IOMMU hacks.
  687. */
  688. if (machine_is(cell))
  689. limit = 0x80000000;
  690. else
  691. limit = MEMBLOCK_ALLOC_ANYWHERE;
  692. table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
  693. DBG("Hash table allocated at %lx, size: %lx\n", table,
  694. htab_size_bytes);
  695. htab_address = __va(table);
  696. /* htab absolute addr + encoded htabsize */
  697. _SDR1 = table + __ilog2(pteg_count) - 11;
  698. /* Initialize the HPT with no entries */
  699. memset((void *)table, 0, htab_size_bytes);
  700. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  701. /* Set SDR1 */
  702. mtspr(SPRN_SDR1, _SDR1);
  703. else
  704. hash_init_partition_table(table, pteg_count);
  705. }
  706. prot = pgprot_val(PAGE_KERNEL);
  707. #ifdef CONFIG_DEBUG_PAGEALLOC
  708. if (debug_pagealloc_enabled()) {
  709. linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
  710. linear_map_hash_slots = __va(memblock_alloc_base(
  711. linear_map_hash_count, 1, ppc64_rma_size));
  712. memset(linear_map_hash_slots, 0, linear_map_hash_count);
  713. }
  714. #endif /* CONFIG_DEBUG_PAGEALLOC */
  715. /* On U3 based machines, we need to reserve the DART area and
  716. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  717. * cacheable later on
  718. */
  719. /* create bolted the linear mapping in the hash table */
  720. for_each_memblock(memory, reg) {
  721. base = (unsigned long)__va(reg->base);
  722. size = reg->size;
  723. DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
  724. base, size, prot);
  725. #ifdef CONFIG_U3_DART
  726. /* Do not map the DART space. Fortunately, it will be aligned
  727. * in such a way that it will not cross two memblock regions and
  728. * will fit within a single 16Mb page.
  729. * The DART space is assumed to be a full 16Mb region even if
  730. * we only use 2Mb of that space. We will use more of it later
  731. * for AGP GART. We have to use a full 16Mb large page.
  732. */
  733. DBG("DART base: %lx\n", dart_tablebase);
  734. if (dart_tablebase != 0 && dart_tablebase >= base
  735. && dart_tablebase < (base + size)) {
  736. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  737. if (base != dart_tablebase)
  738. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  739. __pa(base), prot,
  740. mmu_linear_psize,
  741. mmu_kernel_ssize));
  742. if ((base + size) > dart_table_end)
  743. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  744. base + size,
  745. __pa(dart_table_end),
  746. prot,
  747. mmu_linear_psize,
  748. mmu_kernel_ssize));
  749. continue;
  750. }
  751. #endif /* CONFIG_U3_DART */
  752. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  753. prot, mmu_linear_psize, mmu_kernel_ssize));
  754. }
  755. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  756. /*
  757. * If we have a memory_limit and we've allocated TCEs then we need to
  758. * explicitly map the TCE area at the top of RAM. We also cope with the
  759. * case that the TCEs start below memory_limit.
  760. * tce_alloc_start/end are 16MB aligned so the mapping should work
  761. * for either 4K or 16MB pages.
  762. */
  763. if (tce_alloc_start) {
  764. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  765. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  766. if (base + size >= tce_alloc_start)
  767. tce_alloc_start = base + size + 1;
  768. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  769. __pa(tce_alloc_start), prot,
  770. mmu_linear_psize, mmu_kernel_ssize));
  771. }
  772. DBG(" <- htab_initialize()\n");
  773. }
  774. #undef KB
  775. #undef MB
  776. void __init hash__early_init_mmu(void)
  777. {
  778. /*
  779. * initialize page table size
  780. */
  781. __pte_frag_nr = H_PTE_FRAG_NR;
  782. __pte_frag_size_shift = H_PTE_FRAG_SIZE_SHIFT;
  783. __pte_index_size = H_PTE_INDEX_SIZE;
  784. __pmd_index_size = H_PMD_INDEX_SIZE;
  785. __pud_index_size = H_PUD_INDEX_SIZE;
  786. __pgd_index_size = H_PGD_INDEX_SIZE;
  787. __pmd_cache_index = H_PMD_CACHE_INDEX;
  788. __pte_table_size = H_PTE_TABLE_SIZE;
  789. __pmd_table_size = H_PMD_TABLE_SIZE;
  790. __pud_table_size = H_PUD_TABLE_SIZE;
  791. __pgd_table_size = H_PGD_TABLE_SIZE;
  792. /*
  793. * 4k use hugepd format, so for hash set then to
  794. * zero
  795. */
  796. __pmd_val_bits = 0;
  797. __pud_val_bits = 0;
  798. __pgd_val_bits = 0;
  799. __kernel_virt_start = H_KERN_VIRT_START;
  800. __kernel_virt_size = H_KERN_VIRT_SIZE;
  801. __vmalloc_start = H_VMALLOC_START;
  802. __vmalloc_end = H_VMALLOC_END;
  803. vmemmap = (struct page *)H_VMEMMAP_BASE;
  804. ioremap_bot = IOREMAP_BASE;
  805. /* Initialize the MMU Hash table and create the linear mapping
  806. * of memory. Has to be done before SLB initialization as this is
  807. * currently where the page size encoding is obtained.
  808. */
  809. htab_initialize();
  810. /* Initialize SLB management */
  811. slb_initialize();
  812. }
  813. #ifdef CONFIG_SMP
  814. void hash__early_init_mmu_secondary(void)
  815. {
  816. /* Initialize hash table for that CPU */
  817. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  818. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  819. mtspr(SPRN_SDR1, _SDR1);
  820. else
  821. mtspr(SPRN_PTCR,
  822. __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
  823. }
  824. /* Initialize SLB */
  825. slb_initialize();
  826. }
  827. #endif /* CONFIG_SMP */
  828. /*
  829. * Called by asm hashtable.S for doing lazy icache flush
  830. */
  831. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  832. {
  833. struct page *page;
  834. if (!pfn_valid(pte_pfn(pte)))
  835. return pp;
  836. page = pte_page(pte);
  837. /* page is dirty */
  838. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  839. if (trap == 0x400) {
  840. flush_dcache_icache_page(page);
  841. set_bit(PG_arch_1, &page->flags);
  842. } else
  843. pp |= HPTE_R_N;
  844. }
  845. return pp;
  846. }
  847. #ifdef CONFIG_PPC_MM_SLICES
  848. static unsigned int get_paca_psize(unsigned long addr)
  849. {
  850. u64 lpsizes;
  851. unsigned char *hpsizes;
  852. unsigned long index, mask_index;
  853. if (addr < SLICE_LOW_TOP) {
  854. lpsizes = get_paca()->mm_ctx_low_slices_psize;
  855. index = GET_LOW_SLICE_INDEX(addr);
  856. return (lpsizes >> (index * 4)) & 0xF;
  857. }
  858. hpsizes = get_paca()->mm_ctx_high_slices_psize;
  859. index = GET_HIGH_SLICE_INDEX(addr);
  860. mask_index = index & 0x1;
  861. return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xF;
  862. }
  863. #else
  864. unsigned int get_paca_psize(unsigned long addr)
  865. {
  866. return get_paca()->mm_ctx_user_psize;
  867. }
  868. #endif
  869. /*
  870. * Demote a segment to using 4k pages.
  871. * For now this makes the whole process use 4k pages.
  872. */
  873. #ifdef CONFIG_PPC_64K_PAGES
  874. void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  875. {
  876. if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
  877. return;
  878. slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
  879. copro_flush_all_slbs(mm);
  880. if ((get_paca_psize(addr) != MMU_PAGE_4K) && (current->mm == mm)) {
  881. copy_mm_to_paca(&mm->context);
  882. slb_flush_and_rebolt();
  883. }
  884. }
  885. #endif /* CONFIG_PPC_64K_PAGES */
  886. #ifdef CONFIG_PPC_SUBPAGE_PROT
  887. /*
  888. * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
  889. * Userspace sets the subpage permissions using the subpage_prot system call.
  890. *
  891. * Result is 0: full permissions, _PAGE_RW: read-only,
  892. * _PAGE_RWX: no access.
  893. */
  894. static int subpage_protection(struct mm_struct *mm, unsigned long ea)
  895. {
  896. struct subpage_prot_table *spt = &mm->context.spt;
  897. u32 spp = 0;
  898. u32 **sbpm, *sbpp;
  899. if (ea >= spt->maxaddr)
  900. return 0;
  901. if (ea < 0x100000000UL) {
  902. /* addresses below 4GB use spt->low_prot */
  903. sbpm = spt->low_prot;
  904. } else {
  905. sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
  906. if (!sbpm)
  907. return 0;
  908. }
  909. sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  910. if (!sbpp)
  911. return 0;
  912. spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
  913. /* extract 2-bit bitfield for this 4k subpage */
  914. spp >>= 30 - 2 * ((ea >> 12) & 0xf);
  915. /*
  916. * 0 -> full premission
  917. * 1 -> Read only
  918. * 2 -> no access.
  919. * We return the flag that need to be cleared.
  920. */
  921. spp = ((spp & 2) ? _PAGE_RWX : 0) | ((spp & 1) ? _PAGE_WRITE : 0);
  922. return spp;
  923. }
  924. #else /* CONFIG_PPC_SUBPAGE_PROT */
  925. static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
  926. {
  927. return 0;
  928. }
  929. #endif
  930. void hash_failure_debug(unsigned long ea, unsigned long access,
  931. unsigned long vsid, unsigned long trap,
  932. int ssize, int psize, int lpsize, unsigned long pte)
  933. {
  934. if (!printk_ratelimit())
  935. return;
  936. pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
  937. ea, access, current->comm);
  938. pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
  939. trap, vsid, ssize, psize, lpsize, pte);
  940. }
  941. static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
  942. int psize, bool user_region)
  943. {
  944. if (user_region) {
  945. if (psize != get_paca_psize(ea)) {
  946. copy_mm_to_paca(&mm->context);
  947. slb_flush_and_rebolt();
  948. }
  949. } else if (get_paca()->vmalloc_sllp !=
  950. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  951. get_paca()->vmalloc_sllp =
  952. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  953. slb_vmalloc_update();
  954. }
  955. }
  956. /* Result code is:
  957. * 0 - handled
  958. * 1 - normal page fault
  959. * -1 - critical hash insertion error
  960. * -2 - access not permitted by subpage protection mechanism
  961. */
  962. int hash_page_mm(struct mm_struct *mm, unsigned long ea,
  963. unsigned long access, unsigned long trap,
  964. unsigned long flags)
  965. {
  966. bool is_thp;
  967. enum ctx_state prev_state = exception_enter();
  968. pgd_t *pgdir;
  969. unsigned long vsid;
  970. pte_t *ptep;
  971. unsigned hugeshift;
  972. const struct cpumask *tmp;
  973. int rc, user_region = 0;
  974. int psize, ssize;
  975. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  976. ea, access, trap);
  977. trace_hash_fault(ea, access, trap);
  978. /* Get region & vsid */
  979. switch (REGION_ID(ea)) {
  980. case USER_REGION_ID:
  981. user_region = 1;
  982. if (! mm) {
  983. DBG_LOW(" user region with no mm !\n");
  984. rc = 1;
  985. goto bail;
  986. }
  987. psize = get_slice_psize(mm, ea);
  988. ssize = user_segment_size(ea);
  989. vsid = get_vsid(mm->context.id, ea, ssize);
  990. break;
  991. case VMALLOC_REGION_ID:
  992. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  993. if (ea < VMALLOC_END)
  994. psize = mmu_vmalloc_psize;
  995. else
  996. psize = mmu_io_psize;
  997. ssize = mmu_kernel_ssize;
  998. break;
  999. default:
  1000. /* Not a valid range
  1001. * Send the problem up to do_page_fault
  1002. */
  1003. rc = 1;
  1004. goto bail;
  1005. }
  1006. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  1007. /* Bad address. */
  1008. if (!vsid) {
  1009. DBG_LOW("Bad address!\n");
  1010. rc = 1;
  1011. goto bail;
  1012. }
  1013. /* Get pgdir */
  1014. pgdir = mm->pgd;
  1015. if (pgdir == NULL) {
  1016. rc = 1;
  1017. goto bail;
  1018. }
  1019. /* Check CPU locality */
  1020. tmp = cpumask_of(smp_processor_id());
  1021. if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
  1022. flags |= HPTE_LOCAL_UPDATE;
  1023. #ifndef CONFIG_PPC_64K_PAGES
  1024. /* If we use 4K pages and our psize is not 4K, then we might
  1025. * be hitting a special driver mapping, and need to align the
  1026. * address before we fetch the PTE.
  1027. *
  1028. * It could also be a hugepage mapping, in which case this is
  1029. * not necessary, but it's not harmful, either.
  1030. */
  1031. if (psize != MMU_PAGE_4K)
  1032. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  1033. #endif /* CONFIG_PPC_64K_PAGES */
  1034. /* Get PTE and page size from page tables */
  1035. ptep = __find_linux_pte_or_hugepte(pgdir, ea, &is_thp, &hugeshift);
  1036. if (ptep == NULL || !pte_present(*ptep)) {
  1037. DBG_LOW(" no PTE !\n");
  1038. rc = 1;
  1039. goto bail;
  1040. }
  1041. /* Add _PAGE_PRESENT to the required access perm */
  1042. access |= _PAGE_PRESENT;
  1043. /* Pre-check access permissions (will be re-checked atomically
  1044. * in __hash_page_XX but this pre-check is a fast path
  1045. */
  1046. if (!check_pte_access(access, pte_val(*ptep))) {
  1047. DBG_LOW(" no access !\n");
  1048. rc = 1;
  1049. goto bail;
  1050. }
  1051. if (hugeshift) {
  1052. if (is_thp)
  1053. rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
  1054. trap, flags, ssize, psize);
  1055. #ifdef CONFIG_HUGETLB_PAGE
  1056. else
  1057. rc = __hash_page_huge(ea, access, vsid, ptep, trap,
  1058. flags, ssize, hugeshift, psize);
  1059. #else
  1060. else {
  1061. /*
  1062. * if we have hugeshift, and is not transhuge with
  1063. * hugetlb disabled, something is really wrong.
  1064. */
  1065. rc = 1;
  1066. WARN_ON(1);
  1067. }
  1068. #endif
  1069. if (current->mm == mm)
  1070. check_paca_psize(ea, mm, psize, user_region);
  1071. goto bail;
  1072. }
  1073. #ifndef CONFIG_PPC_64K_PAGES
  1074. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  1075. #else
  1076. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  1077. pte_val(*(ptep + PTRS_PER_PTE)));
  1078. #endif
  1079. /* Do actual hashing */
  1080. #ifdef CONFIG_PPC_64K_PAGES
  1081. /* If H_PAGE_4K_PFN is set, make sure this is a 4k segment */
  1082. if ((pte_val(*ptep) & H_PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
  1083. demote_segment_4k(mm, ea);
  1084. psize = MMU_PAGE_4K;
  1085. }
  1086. /* If this PTE is non-cacheable and we have restrictions on
  1087. * using non cacheable large pages, then we switch to 4k
  1088. */
  1089. if (mmu_ci_restrictions && psize == MMU_PAGE_64K && pte_ci(*ptep)) {
  1090. if (user_region) {
  1091. demote_segment_4k(mm, ea);
  1092. psize = MMU_PAGE_4K;
  1093. } else if (ea < VMALLOC_END) {
  1094. /*
  1095. * some driver did a non-cacheable mapping
  1096. * in vmalloc space, so switch vmalloc
  1097. * to 4k pages
  1098. */
  1099. printk(KERN_ALERT "Reducing vmalloc segment "
  1100. "to 4kB pages because of "
  1101. "non-cacheable mapping\n");
  1102. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  1103. copro_flush_all_slbs(mm);
  1104. }
  1105. }
  1106. #endif /* CONFIG_PPC_64K_PAGES */
  1107. if (current->mm == mm)
  1108. check_paca_psize(ea, mm, psize, user_region);
  1109. #ifdef CONFIG_PPC_64K_PAGES
  1110. if (psize == MMU_PAGE_64K)
  1111. rc = __hash_page_64K(ea, access, vsid, ptep, trap,
  1112. flags, ssize);
  1113. else
  1114. #endif /* CONFIG_PPC_64K_PAGES */
  1115. {
  1116. int spp = subpage_protection(mm, ea);
  1117. if (access & spp)
  1118. rc = -2;
  1119. else
  1120. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  1121. flags, ssize, spp);
  1122. }
  1123. /* Dump some info in case of hash insertion failure, they should
  1124. * never happen so it is really useful to know if/when they do
  1125. */
  1126. if (rc == -1)
  1127. hash_failure_debug(ea, access, vsid, trap, ssize, psize,
  1128. psize, pte_val(*ptep));
  1129. #ifndef CONFIG_PPC_64K_PAGES
  1130. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  1131. #else
  1132. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  1133. pte_val(*(ptep + PTRS_PER_PTE)));
  1134. #endif
  1135. DBG_LOW(" -> rc=%d\n", rc);
  1136. bail:
  1137. exception_exit(prev_state);
  1138. return rc;
  1139. }
  1140. EXPORT_SYMBOL_GPL(hash_page_mm);
  1141. int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
  1142. unsigned long dsisr)
  1143. {
  1144. unsigned long flags = 0;
  1145. struct mm_struct *mm = current->mm;
  1146. if (REGION_ID(ea) == VMALLOC_REGION_ID)
  1147. mm = &init_mm;
  1148. if (dsisr & DSISR_NOHPTE)
  1149. flags |= HPTE_NOHPTE_UPDATE;
  1150. return hash_page_mm(mm, ea, access, trap, flags);
  1151. }
  1152. EXPORT_SYMBOL_GPL(hash_page);
  1153. int __hash_page(unsigned long ea, unsigned long msr, unsigned long trap,
  1154. unsigned long dsisr)
  1155. {
  1156. unsigned long access = _PAGE_PRESENT | _PAGE_READ;
  1157. unsigned long flags = 0;
  1158. struct mm_struct *mm = current->mm;
  1159. if (REGION_ID(ea) == VMALLOC_REGION_ID)
  1160. mm = &init_mm;
  1161. if (dsisr & DSISR_NOHPTE)
  1162. flags |= HPTE_NOHPTE_UPDATE;
  1163. if (dsisr & DSISR_ISSTORE)
  1164. access |= _PAGE_WRITE;
  1165. /*
  1166. * We set _PAGE_PRIVILEGED only when
  1167. * kernel mode access kernel space.
  1168. *
  1169. * _PAGE_PRIVILEGED is NOT set
  1170. * 1) when kernel mode access user space
  1171. * 2) user space access kernel space.
  1172. */
  1173. access |= _PAGE_PRIVILEGED;
  1174. if ((msr & MSR_PR) || (REGION_ID(ea) == USER_REGION_ID))
  1175. access &= ~_PAGE_PRIVILEGED;
  1176. if (trap == 0x400)
  1177. access |= _PAGE_EXEC;
  1178. return hash_page_mm(mm, ea, access, trap, flags);
  1179. }
  1180. #ifdef CONFIG_PPC_MM_SLICES
  1181. static bool should_hash_preload(struct mm_struct *mm, unsigned long ea)
  1182. {
  1183. int psize = get_slice_psize(mm, ea);
  1184. /* We only prefault standard pages for now */
  1185. if (unlikely(psize != mm->context.user_psize))
  1186. return false;
  1187. /*
  1188. * Don't prefault if subpage protection is enabled for the EA.
  1189. */
  1190. if (unlikely((psize == MMU_PAGE_4K) && subpage_protection(mm, ea)))
  1191. return false;
  1192. return true;
  1193. }
  1194. #else
  1195. static bool should_hash_preload(struct mm_struct *mm, unsigned long ea)
  1196. {
  1197. return true;
  1198. }
  1199. #endif
  1200. void hash_preload(struct mm_struct *mm, unsigned long ea,
  1201. unsigned long access, unsigned long trap)
  1202. {
  1203. int hugepage_shift;
  1204. unsigned long vsid;
  1205. pgd_t *pgdir;
  1206. pte_t *ptep;
  1207. unsigned long flags;
  1208. int rc, ssize, update_flags = 0;
  1209. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  1210. if (!should_hash_preload(mm, ea))
  1211. return;
  1212. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  1213. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  1214. /* Get Linux PTE if available */
  1215. pgdir = mm->pgd;
  1216. if (pgdir == NULL)
  1217. return;
  1218. /* Get VSID */
  1219. ssize = user_segment_size(ea);
  1220. vsid = get_vsid(mm->context.id, ea, ssize);
  1221. if (!vsid)
  1222. return;
  1223. /*
  1224. * Hash doesn't like irqs. Walking linux page table with irq disabled
  1225. * saves us from holding multiple locks.
  1226. */
  1227. local_irq_save(flags);
  1228. /*
  1229. * THP pages use update_mmu_cache_pmd. We don't do
  1230. * hash preload there. Hence can ignore THP here
  1231. */
  1232. ptep = find_linux_pte_or_hugepte(pgdir, ea, NULL, &hugepage_shift);
  1233. if (!ptep)
  1234. goto out_exit;
  1235. WARN_ON(hugepage_shift);
  1236. #ifdef CONFIG_PPC_64K_PAGES
  1237. /* If either H_PAGE_4K_PFN or cache inhibited is set (and we are on
  1238. * a 64K kernel), then we don't preload, hash_page() will take
  1239. * care of it once we actually try to access the page.
  1240. * That way we don't have to duplicate all of the logic for segment
  1241. * page size demotion here
  1242. */
  1243. if ((pte_val(*ptep) & H_PAGE_4K_PFN) || pte_ci(*ptep))
  1244. goto out_exit;
  1245. #endif /* CONFIG_PPC_64K_PAGES */
  1246. /* Is that local to this CPU ? */
  1247. if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  1248. update_flags |= HPTE_LOCAL_UPDATE;
  1249. /* Hash it in */
  1250. #ifdef CONFIG_PPC_64K_PAGES
  1251. if (mm->context.user_psize == MMU_PAGE_64K)
  1252. rc = __hash_page_64K(ea, access, vsid, ptep, trap,
  1253. update_flags, ssize);
  1254. else
  1255. #endif /* CONFIG_PPC_64K_PAGES */
  1256. rc = __hash_page_4K(ea, access, vsid, ptep, trap, update_flags,
  1257. ssize, subpage_protection(mm, ea));
  1258. /* Dump some info in case of hash insertion failure, they should
  1259. * never happen so it is really useful to know if/when they do
  1260. */
  1261. if (rc == -1)
  1262. hash_failure_debug(ea, access, vsid, trap, ssize,
  1263. mm->context.user_psize,
  1264. mm->context.user_psize,
  1265. pte_val(*ptep));
  1266. out_exit:
  1267. local_irq_restore(flags);
  1268. }
  1269. /* WARNING: This is called from hash_low_64.S, if you change this prototype,
  1270. * do not forget to update the assembly call site !
  1271. */
  1272. void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
  1273. unsigned long flags)
  1274. {
  1275. unsigned long hash, index, shift, hidx, slot;
  1276. int local = flags & HPTE_LOCAL_UPDATE;
  1277. DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
  1278. pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
  1279. hash = hpt_hash(vpn, shift, ssize);
  1280. hidx = __rpte_to_hidx(pte, index);
  1281. if (hidx & _PTEIDX_SECONDARY)
  1282. hash = ~hash;
  1283. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1284. slot += hidx & _PTEIDX_GROUP_IX;
  1285. DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
  1286. /*
  1287. * We use same base page size and actual psize, because we don't
  1288. * use these functions for hugepage
  1289. */
  1290. ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
  1291. } pte_iterate_hashed_end();
  1292. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1293. /* Transactions are not aborted by tlbiel, only tlbie.
  1294. * Without, syncing a page back to a block device w/ PIO could pick up
  1295. * transactional data (bad!) so we force an abort here. Before the
  1296. * sync the page will be made read-only, which will flush_hash_page.
  1297. * BIG ISSUE here: if the kernel uses a page from userspace without
  1298. * unmapping it first, it may see the speculated version.
  1299. */
  1300. if (local && cpu_has_feature(CPU_FTR_TM) &&
  1301. current->thread.regs &&
  1302. MSR_TM_ACTIVE(current->thread.regs->msr)) {
  1303. tm_enable();
  1304. tm_abort(TM_CAUSE_TLBI);
  1305. }
  1306. #endif
  1307. }
  1308. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1309. void flush_hash_hugepage(unsigned long vsid, unsigned long addr,
  1310. pmd_t *pmdp, unsigned int psize, int ssize,
  1311. unsigned long flags)
  1312. {
  1313. int i, max_hpte_count, valid;
  1314. unsigned long s_addr;
  1315. unsigned char *hpte_slot_array;
  1316. unsigned long hidx, shift, vpn, hash, slot;
  1317. int local = flags & HPTE_LOCAL_UPDATE;
  1318. s_addr = addr & HPAGE_PMD_MASK;
  1319. hpte_slot_array = get_hpte_slot_array(pmdp);
  1320. /*
  1321. * IF we try to do a HUGE PTE update after a withdraw is done.
  1322. * we will find the below NULL. This happens when we do
  1323. * split_huge_page_pmd
  1324. */
  1325. if (!hpte_slot_array)
  1326. return;
  1327. if (ppc_md.hugepage_invalidate) {
  1328. ppc_md.hugepage_invalidate(vsid, s_addr, hpte_slot_array,
  1329. psize, ssize, local);
  1330. goto tm_abort;
  1331. }
  1332. /*
  1333. * No bluk hpte removal support, invalidate each entry
  1334. */
  1335. shift = mmu_psize_defs[psize].shift;
  1336. max_hpte_count = HPAGE_PMD_SIZE >> shift;
  1337. for (i = 0; i < max_hpte_count; i++) {
  1338. /*
  1339. * 8 bits per each hpte entries
  1340. * 000| [ secondary group (one bit) | hidx (3 bits) | valid bit]
  1341. */
  1342. valid = hpte_valid(hpte_slot_array, i);
  1343. if (!valid)
  1344. continue;
  1345. hidx = hpte_hash_index(hpte_slot_array, i);
  1346. /* get the vpn */
  1347. addr = s_addr + (i * (1ul << shift));
  1348. vpn = hpt_vpn(addr, vsid, ssize);
  1349. hash = hpt_hash(vpn, shift, ssize);
  1350. if (hidx & _PTEIDX_SECONDARY)
  1351. hash = ~hash;
  1352. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1353. slot += hidx & _PTEIDX_GROUP_IX;
  1354. ppc_md.hpte_invalidate(slot, vpn, psize,
  1355. MMU_PAGE_16M, ssize, local);
  1356. }
  1357. tm_abort:
  1358. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1359. /* Transactions are not aborted by tlbiel, only tlbie.
  1360. * Without, syncing a page back to a block device w/ PIO could pick up
  1361. * transactional data (bad!) so we force an abort here. Before the
  1362. * sync the page will be made read-only, which will flush_hash_page.
  1363. * BIG ISSUE here: if the kernel uses a page from userspace without
  1364. * unmapping it first, it may see the speculated version.
  1365. */
  1366. if (local && cpu_has_feature(CPU_FTR_TM) &&
  1367. current->thread.regs &&
  1368. MSR_TM_ACTIVE(current->thread.regs->msr)) {
  1369. tm_enable();
  1370. tm_abort(TM_CAUSE_TLBI);
  1371. }
  1372. #endif
  1373. return;
  1374. }
  1375. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1376. void flush_hash_range(unsigned long number, int local)
  1377. {
  1378. if (ppc_md.flush_hash_range)
  1379. ppc_md.flush_hash_range(number, local);
  1380. else {
  1381. int i;
  1382. struct ppc64_tlb_batch *batch =
  1383. this_cpu_ptr(&ppc64_tlb_batch);
  1384. for (i = 0; i < number; i++)
  1385. flush_hash_page(batch->vpn[i], batch->pte[i],
  1386. batch->psize, batch->ssize, local);
  1387. }
  1388. }
  1389. /*
  1390. * low_hash_fault is called when we the low level hash code failed
  1391. * to instert a PTE due to an hypervisor error
  1392. */
  1393. void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
  1394. {
  1395. enum ctx_state prev_state = exception_enter();
  1396. if (user_mode(regs)) {
  1397. #ifdef CONFIG_PPC_SUBPAGE_PROT
  1398. if (rc == -2)
  1399. _exception(SIGSEGV, regs, SEGV_ACCERR, address);
  1400. else
  1401. #endif
  1402. _exception(SIGBUS, regs, BUS_ADRERR, address);
  1403. } else
  1404. bad_page_fault(regs, address, SIGBUS);
  1405. exception_exit(prev_state);
  1406. }
  1407. long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
  1408. unsigned long pa, unsigned long rflags,
  1409. unsigned long vflags, int psize, int ssize)
  1410. {
  1411. unsigned long hpte_group;
  1412. long slot;
  1413. repeat:
  1414. hpte_group = ((hash & htab_hash_mask) *
  1415. HPTES_PER_GROUP) & ~0x7UL;
  1416. /* Insert into the hash table, primary slot */
  1417. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
  1418. psize, psize, ssize);
  1419. /* Primary is full, try the secondary */
  1420. if (unlikely(slot == -1)) {
  1421. hpte_group = ((~hash & htab_hash_mask) *
  1422. HPTES_PER_GROUP) & ~0x7UL;
  1423. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
  1424. vflags | HPTE_V_SECONDARY,
  1425. psize, psize, ssize);
  1426. if (slot == -1) {
  1427. if (mftb() & 0x1)
  1428. hpte_group = ((hash & htab_hash_mask) *
  1429. HPTES_PER_GROUP)&~0x7UL;
  1430. ppc_md.hpte_remove(hpte_group);
  1431. goto repeat;
  1432. }
  1433. }
  1434. return slot;
  1435. }
  1436. #ifdef CONFIG_DEBUG_PAGEALLOC
  1437. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  1438. {
  1439. unsigned long hash;
  1440. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1441. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1442. unsigned long mode = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL));
  1443. long ret;
  1444. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1445. /* Don't create HPTE entries for bad address */
  1446. if (!vsid)
  1447. return;
  1448. ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
  1449. HPTE_V_BOLTED,
  1450. mmu_linear_psize, mmu_kernel_ssize);
  1451. BUG_ON (ret < 0);
  1452. spin_lock(&linear_map_hash_lock);
  1453. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  1454. linear_map_hash_slots[lmi] = ret | 0x80;
  1455. spin_unlock(&linear_map_hash_lock);
  1456. }
  1457. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  1458. {
  1459. unsigned long hash, hidx, slot;
  1460. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1461. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1462. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1463. spin_lock(&linear_map_hash_lock);
  1464. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  1465. hidx = linear_map_hash_slots[lmi] & 0x7f;
  1466. linear_map_hash_slots[lmi] = 0;
  1467. spin_unlock(&linear_map_hash_lock);
  1468. if (hidx & _PTEIDX_SECONDARY)
  1469. hash = ~hash;
  1470. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1471. slot += hidx & _PTEIDX_GROUP_IX;
  1472. ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
  1473. mmu_kernel_ssize, 0);
  1474. }
  1475. void __kernel_map_pages(struct page *page, int numpages, int enable)
  1476. {
  1477. unsigned long flags, vaddr, lmi;
  1478. int i;
  1479. local_irq_save(flags);
  1480. for (i = 0; i < numpages; i++, page++) {
  1481. vaddr = (unsigned long)page_address(page);
  1482. lmi = __pa(vaddr) >> PAGE_SHIFT;
  1483. if (lmi >= linear_map_hash_count)
  1484. continue;
  1485. if (enable)
  1486. kernel_map_linear_page(vaddr, lmi);
  1487. else
  1488. kernel_unmap_linear_page(vaddr, lmi);
  1489. }
  1490. local_irq_restore(flags);
  1491. }
  1492. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1493. void hash__setup_initial_memory_limit(phys_addr_t first_memblock_base,
  1494. phys_addr_t first_memblock_size)
  1495. {
  1496. /* We don't currently support the first MEMBLOCK not mapping 0
  1497. * physical on those processors
  1498. */
  1499. BUG_ON(first_memblock_base != 0);
  1500. /* On LPAR systems, the first entry is our RMA region,
  1501. * non-LPAR 64-bit hash MMU systems don't have a limitation
  1502. * on real mode access, but using the first entry works well
  1503. * enough. We also clamp it to 1G to avoid some funky things
  1504. * such as RTAS bugs etc...
  1505. */
  1506. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  1507. /* Finally limit subsequent allocations */
  1508. memblock_set_current_limit(ppc64_rma_size);
  1509. }