hash_utils_64.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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 <asm/processor.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/mmu.h>
  37. #include <asm/mmu_context.h>
  38. #include <asm/page.h>
  39. #include <asm/types.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/machdep.h>
  42. #include <asm/prom.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. #include <asm/code-patching.h>
  53. #include <asm/fadump.h>
  54. #include <asm/firmware.h>
  55. #include <asm/tm.h>
  56. #ifdef DEBUG
  57. #define DBG(fmt...) udbg_printf(fmt)
  58. #else
  59. #define DBG(fmt...)
  60. #endif
  61. #ifdef DEBUG_LOW
  62. #define DBG_LOW(fmt...) udbg_printf(fmt)
  63. #else
  64. #define DBG_LOW(fmt...)
  65. #endif
  66. #define KB (1024)
  67. #define MB (1024*KB)
  68. #define GB (1024L*MB)
  69. /*
  70. * Note: pte --> Linux PTE
  71. * HPTE --> PowerPC Hashed Page Table Entry
  72. *
  73. * Execution context:
  74. * htab_initialize is called with the MMU off (of course), but
  75. * the kernel has been copied down to zero so it can directly
  76. * reference global data. At this point it is very difficult
  77. * to print debug info.
  78. *
  79. */
  80. #ifdef CONFIG_U3_DART
  81. extern unsigned long dart_tablebase;
  82. #endif /* CONFIG_U3_DART */
  83. static unsigned long _SDR1;
  84. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
  85. struct hash_pte *htab_address;
  86. unsigned long htab_size_bytes;
  87. unsigned long htab_hash_mask;
  88. EXPORT_SYMBOL_GPL(htab_hash_mask);
  89. int mmu_linear_psize = MMU_PAGE_4K;
  90. int mmu_virtual_psize = MMU_PAGE_4K;
  91. int mmu_vmalloc_psize = MMU_PAGE_4K;
  92. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  93. int mmu_vmemmap_psize = MMU_PAGE_4K;
  94. #endif
  95. int mmu_io_psize = MMU_PAGE_4K;
  96. int mmu_kernel_ssize = MMU_SEGSIZE_256M;
  97. int mmu_highuser_ssize = MMU_SEGSIZE_256M;
  98. u16 mmu_slb_size = 64;
  99. EXPORT_SYMBOL_GPL(mmu_slb_size);
  100. #ifdef CONFIG_PPC_64K_PAGES
  101. int mmu_ci_restrictions;
  102. #endif
  103. #ifdef CONFIG_DEBUG_PAGEALLOC
  104. static u8 *linear_map_hash_slots;
  105. static unsigned long linear_map_hash_count;
  106. static DEFINE_SPINLOCK(linear_map_hash_lock);
  107. #endif /* CONFIG_DEBUG_PAGEALLOC */
  108. /* There are definitions of page sizes arrays to be used when none
  109. * is provided by the firmware.
  110. */
  111. /* Pre-POWER4 CPUs (4k pages only)
  112. */
  113. static struct mmu_psize_def mmu_psize_defaults_old[] = {
  114. [MMU_PAGE_4K] = {
  115. .shift = 12,
  116. .sllp = 0,
  117. .penc = 0,
  118. .avpnm = 0,
  119. .tlbiel = 0,
  120. },
  121. };
  122. /* POWER4, GPUL, POWER5
  123. *
  124. * Support for 16Mb large pages
  125. */
  126. static struct mmu_psize_def mmu_psize_defaults_gp[] = {
  127. [MMU_PAGE_4K] = {
  128. .shift = 12,
  129. .sllp = 0,
  130. .penc = 0,
  131. .avpnm = 0,
  132. .tlbiel = 1,
  133. },
  134. [MMU_PAGE_16M] = {
  135. .shift = 24,
  136. .sllp = SLB_VSID_L,
  137. .penc = 0,
  138. .avpnm = 0x1UL,
  139. .tlbiel = 0,
  140. },
  141. };
  142. static unsigned long htab_convert_pte_flags(unsigned long pteflags)
  143. {
  144. unsigned long rflags = pteflags & 0x1fa;
  145. /* _PAGE_EXEC -> NOEXEC */
  146. if ((pteflags & _PAGE_EXEC) == 0)
  147. rflags |= HPTE_R_N;
  148. /* PP bits. PAGE_USER is already PP bit 0x2, so we only
  149. * need to add in 0x1 if it's a read-only user page
  150. */
  151. if ((pteflags & _PAGE_USER) && !((pteflags & _PAGE_RW) &&
  152. (pteflags & _PAGE_DIRTY)))
  153. rflags |= 1;
  154. /* Always add C */
  155. return rflags | HPTE_R_C;
  156. }
  157. int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
  158. unsigned long pstart, unsigned long prot,
  159. int psize, int ssize)
  160. {
  161. unsigned long vaddr, paddr;
  162. unsigned int step, shift;
  163. int ret = 0;
  164. shift = mmu_psize_defs[psize].shift;
  165. step = 1 << shift;
  166. prot = htab_convert_pte_flags(prot);
  167. DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
  168. vstart, vend, pstart, prot, psize, ssize);
  169. for (vaddr = vstart, paddr = pstart; vaddr < vend;
  170. vaddr += step, paddr += step) {
  171. unsigned long hash, hpteg;
  172. unsigned long vsid = get_kernel_vsid(vaddr, ssize);
  173. unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
  174. unsigned long tprot = prot;
  175. /* Make kernel text executable */
  176. if (overlaps_kernel_text(vaddr, vaddr + step))
  177. tprot &= ~HPTE_R_N;
  178. hash = hpt_hash(vpn, shift, ssize);
  179. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  180. BUG_ON(!ppc_md.hpte_insert);
  181. ret = ppc_md.hpte_insert(hpteg, vpn, paddr, tprot,
  182. HPTE_V_BOLTED, psize, ssize);
  183. if (ret < 0)
  184. break;
  185. #ifdef CONFIG_DEBUG_PAGEALLOC
  186. if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
  187. linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
  188. #endif /* CONFIG_DEBUG_PAGEALLOC */
  189. }
  190. return ret < 0 ? ret : 0;
  191. }
  192. #ifdef CONFIG_MEMORY_HOTPLUG
  193. static int htab_remove_mapping(unsigned long vstart, unsigned long vend,
  194. int psize, int ssize)
  195. {
  196. unsigned long vaddr;
  197. unsigned int step, shift;
  198. shift = mmu_psize_defs[psize].shift;
  199. step = 1 << shift;
  200. if (!ppc_md.hpte_removebolted) {
  201. printk(KERN_WARNING "Platform doesn't implement "
  202. "hpte_removebolted\n");
  203. return -EINVAL;
  204. }
  205. for (vaddr = vstart; vaddr < vend; vaddr += step)
  206. ppc_md.hpte_removebolted(vaddr, psize, ssize);
  207. return 0;
  208. }
  209. #endif /* CONFIG_MEMORY_HOTPLUG */
  210. static int __init htab_dt_scan_seg_sizes(unsigned long node,
  211. const char *uname, int depth,
  212. void *data)
  213. {
  214. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  215. u32 *prop;
  216. unsigned long size = 0;
  217. /* We are scanning "cpu" nodes only */
  218. if (type == NULL || strcmp(type, "cpu") != 0)
  219. return 0;
  220. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
  221. &size);
  222. if (prop == NULL)
  223. return 0;
  224. for (; size >= 4; size -= 4, ++prop) {
  225. if (prop[0] == 40) {
  226. DBG("1T segment support detected\n");
  227. cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
  228. return 1;
  229. }
  230. }
  231. cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
  232. return 0;
  233. }
  234. static void __init htab_init_seg_sizes(void)
  235. {
  236. of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
  237. }
  238. static int __init htab_dt_scan_page_sizes(unsigned long node,
  239. const char *uname, int depth,
  240. void *data)
  241. {
  242. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  243. u32 *prop;
  244. unsigned long size = 0;
  245. /* We are scanning "cpu" nodes only */
  246. if (type == NULL || strcmp(type, "cpu") != 0)
  247. return 0;
  248. prop = (u32 *)of_get_flat_dt_prop(node,
  249. "ibm,segment-page-sizes", &size);
  250. if (prop != NULL) {
  251. DBG("Page sizes from device-tree:\n");
  252. size /= 4;
  253. cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
  254. while(size > 0) {
  255. unsigned int shift = prop[0];
  256. unsigned int slbenc = prop[1];
  257. unsigned int lpnum = prop[2];
  258. unsigned int lpenc = 0;
  259. struct mmu_psize_def *def;
  260. int idx = -1;
  261. size -= 3; prop += 3;
  262. while(size > 0 && lpnum) {
  263. if (prop[0] == shift)
  264. lpenc = prop[1];
  265. prop += 2; size -= 2;
  266. lpnum--;
  267. }
  268. switch(shift) {
  269. case 0xc:
  270. idx = MMU_PAGE_4K;
  271. break;
  272. case 0x10:
  273. idx = MMU_PAGE_64K;
  274. break;
  275. case 0x14:
  276. idx = MMU_PAGE_1M;
  277. break;
  278. case 0x18:
  279. idx = MMU_PAGE_16M;
  280. cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
  281. break;
  282. case 0x22:
  283. idx = MMU_PAGE_16G;
  284. break;
  285. }
  286. if (idx < 0)
  287. continue;
  288. def = &mmu_psize_defs[idx];
  289. def->shift = shift;
  290. if (shift <= 23)
  291. def->avpnm = 0;
  292. else
  293. def->avpnm = (1 << (shift - 23)) - 1;
  294. def->sllp = slbenc;
  295. def->penc = lpenc;
  296. /* We don't know for sure what's up with tlbiel, so
  297. * for now we only set it for 4K and 64K pages
  298. */
  299. if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
  300. def->tlbiel = 1;
  301. else
  302. def->tlbiel = 0;
  303. DBG(" %d: shift=%02x, sllp=%04lx, avpnm=%08lx, "
  304. "tlbiel=%d, penc=%d\n",
  305. idx, shift, def->sllp, def->avpnm, def->tlbiel,
  306. def->penc);
  307. }
  308. return 1;
  309. }
  310. return 0;
  311. }
  312. #ifdef CONFIG_HUGETLB_PAGE
  313. /* Scan for 16G memory blocks that have been set aside for huge pages
  314. * and reserve those blocks for 16G huge pages.
  315. */
  316. static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
  317. const char *uname, int depth,
  318. void *data) {
  319. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  320. unsigned long *addr_prop;
  321. u32 *page_count_prop;
  322. unsigned int expected_pages;
  323. long unsigned int phys_addr;
  324. long unsigned int block_size;
  325. /* We are scanning "memory" nodes only */
  326. if (type == NULL || strcmp(type, "memory") != 0)
  327. return 0;
  328. /* This property is the log base 2 of the number of virtual pages that
  329. * will represent this memory block. */
  330. page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
  331. if (page_count_prop == NULL)
  332. return 0;
  333. expected_pages = (1 << page_count_prop[0]);
  334. addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
  335. if (addr_prop == NULL)
  336. return 0;
  337. phys_addr = addr_prop[0];
  338. block_size = addr_prop[1];
  339. if (block_size != (16 * GB))
  340. return 0;
  341. printk(KERN_INFO "Huge page(16GB) memory: "
  342. "addr = 0x%lX size = 0x%lX pages = %d\n",
  343. phys_addr, block_size, expected_pages);
  344. if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
  345. memblock_reserve(phys_addr, block_size * expected_pages);
  346. add_gpage(phys_addr, block_size, expected_pages);
  347. }
  348. return 0;
  349. }
  350. #endif /* CONFIG_HUGETLB_PAGE */
  351. static void __init htab_init_page_sizes(void)
  352. {
  353. int rc;
  354. /* Default to 4K pages only */
  355. memcpy(mmu_psize_defs, mmu_psize_defaults_old,
  356. sizeof(mmu_psize_defaults_old));
  357. /*
  358. * Try to find the available page sizes in the device-tree
  359. */
  360. rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
  361. if (rc != 0) /* Found */
  362. goto found;
  363. /*
  364. * Not in the device-tree, let's fallback on known size
  365. * list for 16M capable GP & GR
  366. */
  367. if (mmu_has_feature(MMU_FTR_16M_PAGE))
  368. memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
  369. sizeof(mmu_psize_defaults_gp));
  370. found:
  371. #ifndef CONFIG_DEBUG_PAGEALLOC
  372. /*
  373. * Pick a size for the linear mapping. Currently, we only support
  374. * 16M, 1M and 4K which is the default
  375. */
  376. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  377. mmu_linear_psize = MMU_PAGE_16M;
  378. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  379. mmu_linear_psize = MMU_PAGE_1M;
  380. #endif /* CONFIG_DEBUG_PAGEALLOC */
  381. #ifdef CONFIG_PPC_64K_PAGES
  382. /*
  383. * Pick a size for the ordinary pages. Default is 4K, we support
  384. * 64K for user mappings and vmalloc if supported by the processor.
  385. * We only use 64k for ioremap if the processor
  386. * (and firmware) support cache-inhibited large pages.
  387. * If not, we use 4k and set mmu_ci_restrictions so that
  388. * hash_page knows to switch processes that use cache-inhibited
  389. * mappings to 4k pages.
  390. */
  391. if (mmu_psize_defs[MMU_PAGE_64K].shift) {
  392. mmu_virtual_psize = MMU_PAGE_64K;
  393. mmu_vmalloc_psize = MMU_PAGE_64K;
  394. if (mmu_linear_psize == MMU_PAGE_4K)
  395. mmu_linear_psize = MMU_PAGE_64K;
  396. if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
  397. /*
  398. * Don't use 64k pages for ioremap on pSeries, since
  399. * that would stop us accessing the HEA ethernet.
  400. */
  401. if (!machine_is(pseries))
  402. mmu_io_psize = MMU_PAGE_64K;
  403. } else
  404. mmu_ci_restrictions = 1;
  405. }
  406. #endif /* CONFIG_PPC_64K_PAGES */
  407. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  408. /* We try to use 16M pages for vmemmap if that is supported
  409. * and we have at least 1G of RAM at boot
  410. */
  411. if (mmu_psize_defs[MMU_PAGE_16M].shift &&
  412. memblock_phys_mem_size() >= 0x40000000)
  413. mmu_vmemmap_psize = MMU_PAGE_16M;
  414. else if (mmu_psize_defs[MMU_PAGE_64K].shift)
  415. mmu_vmemmap_psize = MMU_PAGE_64K;
  416. else
  417. mmu_vmemmap_psize = MMU_PAGE_4K;
  418. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  419. printk(KERN_DEBUG "Page orders: linear mapping = %d, "
  420. "virtual = %d, io = %d"
  421. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  422. ", vmemmap = %d"
  423. #endif
  424. "\n",
  425. mmu_psize_defs[mmu_linear_psize].shift,
  426. mmu_psize_defs[mmu_virtual_psize].shift,
  427. mmu_psize_defs[mmu_io_psize].shift
  428. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  429. ,mmu_psize_defs[mmu_vmemmap_psize].shift
  430. #endif
  431. );
  432. #ifdef CONFIG_HUGETLB_PAGE
  433. /* Reserve 16G huge page memory sections for huge pages */
  434. of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
  435. #endif /* CONFIG_HUGETLB_PAGE */
  436. }
  437. static int __init htab_dt_scan_pftsize(unsigned long node,
  438. const char *uname, int depth,
  439. void *data)
  440. {
  441. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  442. u32 *prop;
  443. /* We are scanning "cpu" nodes only */
  444. if (type == NULL || strcmp(type, "cpu") != 0)
  445. return 0;
  446. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
  447. if (prop != NULL) {
  448. /* pft_size[0] is the NUMA CEC cookie */
  449. ppc64_pft_size = prop[1];
  450. return 1;
  451. }
  452. return 0;
  453. }
  454. static unsigned long __init htab_get_table_size(void)
  455. {
  456. unsigned long mem_size, rnd_mem_size, pteg_count, psize;
  457. /* If hash size isn't already provided by the platform, we try to
  458. * retrieve it from the device-tree. If it's not there neither, we
  459. * calculate it now based on the total RAM size
  460. */
  461. if (ppc64_pft_size == 0)
  462. of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
  463. if (ppc64_pft_size)
  464. return 1UL << ppc64_pft_size;
  465. /* round mem_size up to next power of 2 */
  466. mem_size = memblock_phys_mem_size();
  467. rnd_mem_size = 1UL << __ilog2(mem_size);
  468. if (rnd_mem_size < mem_size)
  469. rnd_mem_size <<= 1;
  470. /* # pages / 2 */
  471. psize = mmu_psize_defs[mmu_virtual_psize].shift;
  472. pteg_count = max(rnd_mem_size >> (psize + 1), 1UL << 11);
  473. return pteg_count << 7;
  474. }
  475. #ifdef CONFIG_MEMORY_HOTPLUG
  476. int create_section_mapping(unsigned long start, unsigned long end)
  477. {
  478. return htab_bolt_mapping(start, end, __pa(start),
  479. pgprot_val(PAGE_KERNEL), mmu_linear_psize,
  480. mmu_kernel_ssize);
  481. }
  482. int remove_section_mapping(unsigned long start, unsigned long end)
  483. {
  484. return htab_remove_mapping(start, end, mmu_linear_psize,
  485. mmu_kernel_ssize);
  486. }
  487. #endif /* CONFIG_MEMORY_HOTPLUG */
  488. #define FUNCTION_TEXT(A) ((*(unsigned long *)(A)))
  489. static void __init htab_finish_init(void)
  490. {
  491. extern unsigned int *htab_call_hpte_insert1;
  492. extern unsigned int *htab_call_hpte_insert2;
  493. extern unsigned int *htab_call_hpte_remove;
  494. extern unsigned int *htab_call_hpte_updatepp;
  495. #ifdef CONFIG_PPC_HAS_HASH_64K
  496. extern unsigned int *ht64_call_hpte_insert1;
  497. extern unsigned int *ht64_call_hpte_insert2;
  498. extern unsigned int *ht64_call_hpte_remove;
  499. extern unsigned int *ht64_call_hpte_updatepp;
  500. patch_branch(ht64_call_hpte_insert1,
  501. FUNCTION_TEXT(ppc_md.hpte_insert),
  502. BRANCH_SET_LINK);
  503. patch_branch(ht64_call_hpte_insert2,
  504. FUNCTION_TEXT(ppc_md.hpte_insert),
  505. BRANCH_SET_LINK);
  506. patch_branch(ht64_call_hpte_remove,
  507. FUNCTION_TEXT(ppc_md.hpte_remove),
  508. BRANCH_SET_LINK);
  509. patch_branch(ht64_call_hpte_updatepp,
  510. FUNCTION_TEXT(ppc_md.hpte_updatepp),
  511. BRANCH_SET_LINK);
  512. #endif /* CONFIG_PPC_HAS_HASH_64K */
  513. patch_branch(htab_call_hpte_insert1,
  514. FUNCTION_TEXT(ppc_md.hpte_insert),
  515. BRANCH_SET_LINK);
  516. patch_branch(htab_call_hpte_insert2,
  517. FUNCTION_TEXT(ppc_md.hpte_insert),
  518. BRANCH_SET_LINK);
  519. patch_branch(htab_call_hpte_remove,
  520. FUNCTION_TEXT(ppc_md.hpte_remove),
  521. BRANCH_SET_LINK);
  522. patch_branch(htab_call_hpte_updatepp,
  523. FUNCTION_TEXT(ppc_md.hpte_updatepp),
  524. BRANCH_SET_LINK);
  525. }
  526. static void __init htab_initialize(void)
  527. {
  528. unsigned long table;
  529. unsigned long pteg_count;
  530. unsigned long prot;
  531. unsigned long base = 0, size = 0, limit;
  532. struct memblock_region *reg;
  533. DBG(" -> htab_initialize()\n");
  534. /* Initialize segment sizes */
  535. htab_init_seg_sizes();
  536. /* Initialize page sizes */
  537. htab_init_page_sizes();
  538. if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
  539. mmu_kernel_ssize = MMU_SEGSIZE_1T;
  540. mmu_highuser_ssize = MMU_SEGSIZE_1T;
  541. printk(KERN_INFO "Using 1TB segments\n");
  542. }
  543. /*
  544. * Calculate the required size of the htab. We want the number of
  545. * PTEGs to equal one half the number of real pages.
  546. */
  547. htab_size_bytes = htab_get_table_size();
  548. pteg_count = htab_size_bytes >> 7;
  549. htab_hash_mask = pteg_count - 1;
  550. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  551. /* Using a hypervisor which owns the htab */
  552. htab_address = NULL;
  553. _SDR1 = 0;
  554. #ifdef CONFIG_FA_DUMP
  555. /*
  556. * If firmware assisted dump is active firmware preserves
  557. * the contents of htab along with entire partition memory.
  558. * Clear the htab if firmware assisted dump is active so
  559. * that we dont end up using old mappings.
  560. */
  561. if (is_fadump_active() && ppc_md.hpte_clear_all)
  562. ppc_md.hpte_clear_all();
  563. #endif
  564. } else {
  565. /* Find storage for the HPT. Must be contiguous in
  566. * the absolute address space. On cell we want it to be
  567. * in the first 2 Gig so we can use it for IOMMU hacks.
  568. */
  569. if (machine_is(cell))
  570. limit = 0x80000000;
  571. else
  572. limit = MEMBLOCK_ALLOC_ANYWHERE;
  573. table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
  574. DBG("Hash table allocated at %lx, size: %lx\n", table,
  575. htab_size_bytes);
  576. htab_address = __va(table);
  577. /* htab absolute addr + encoded htabsize */
  578. _SDR1 = table + __ilog2(pteg_count) - 11;
  579. /* Initialize the HPT with no entries */
  580. memset((void *)table, 0, htab_size_bytes);
  581. /* Set SDR1 */
  582. mtspr(SPRN_SDR1, _SDR1);
  583. }
  584. prot = pgprot_val(PAGE_KERNEL);
  585. #ifdef CONFIG_DEBUG_PAGEALLOC
  586. linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
  587. linear_map_hash_slots = __va(memblock_alloc_base(linear_map_hash_count,
  588. 1, ppc64_rma_size));
  589. memset(linear_map_hash_slots, 0, linear_map_hash_count);
  590. #endif /* CONFIG_DEBUG_PAGEALLOC */
  591. /* On U3 based machines, we need to reserve the DART area and
  592. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  593. * cacheable later on
  594. */
  595. /* create bolted the linear mapping in the hash table */
  596. for_each_memblock(memory, reg) {
  597. base = (unsigned long)__va(reg->base);
  598. size = reg->size;
  599. DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
  600. base, size, prot);
  601. #ifdef CONFIG_U3_DART
  602. /* Do not map the DART space. Fortunately, it will be aligned
  603. * in such a way that it will not cross two memblock regions and
  604. * will fit within a single 16Mb page.
  605. * The DART space is assumed to be a full 16Mb region even if
  606. * we only use 2Mb of that space. We will use more of it later
  607. * for AGP GART. We have to use a full 16Mb large page.
  608. */
  609. DBG("DART base: %lx\n", dart_tablebase);
  610. if (dart_tablebase != 0 && dart_tablebase >= base
  611. && dart_tablebase < (base + size)) {
  612. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  613. if (base != dart_tablebase)
  614. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  615. __pa(base), prot,
  616. mmu_linear_psize,
  617. mmu_kernel_ssize));
  618. if ((base + size) > dart_table_end)
  619. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  620. base + size,
  621. __pa(dart_table_end),
  622. prot,
  623. mmu_linear_psize,
  624. mmu_kernel_ssize));
  625. continue;
  626. }
  627. #endif /* CONFIG_U3_DART */
  628. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  629. prot, mmu_linear_psize, mmu_kernel_ssize));
  630. }
  631. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  632. /*
  633. * If we have a memory_limit and we've allocated TCEs then we need to
  634. * explicitly map the TCE area at the top of RAM. We also cope with the
  635. * case that the TCEs start below memory_limit.
  636. * tce_alloc_start/end are 16MB aligned so the mapping should work
  637. * for either 4K or 16MB pages.
  638. */
  639. if (tce_alloc_start) {
  640. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  641. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  642. if (base + size >= tce_alloc_start)
  643. tce_alloc_start = base + size + 1;
  644. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  645. __pa(tce_alloc_start), prot,
  646. mmu_linear_psize, mmu_kernel_ssize));
  647. }
  648. htab_finish_init();
  649. DBG(" <- htab_initialize()\n");
  650. }
  651. #undef KB
  652. #undef MB
  653. void __init early_init_mmu(void)
  654. {
  655. /* Setup initial STAB address in the PACA */
  656. get_paca()->stab_real = __pa((u64)&initial_stab);
  657. get_paca()->stab_addr = (u64)&initial_stab;
  658. /* Initialize the MMU Hash table and create the linear mapping
  659. * of memory. Has to be done before stab/slb initialization as
  660. * this is currently where the page size encoding is obtained
  661. */
  662. htab_initialize();
  663. /* Initialize stab / SLB management */
  664. if (mmu_has_feature(MMU_FTR_SLB))
  665. slb_initialize();
  666. else
  667. stab_initialize(get_paca()->stab_real);
  668. }
  669. #ifdef CONFIG_SMP
  670. void __cpuinit early_init_mmu_secondary(void)
  671. {
  672. /* Initialize hash table for that CPU */
  673. if (!firmware_has_feature(FW_FEATURE_LPAR))
  674. mtspr(SPRN_SDR1, _SDR1);
  675. /* Initialize STAB/SLB. We use a virtual address as it works
  676. * in real mode on pSeries.
  677. */
  678. if (mmu_has_feature(MMU_FTR_SLB))
  679. slb_initialize();
  680. else
  681. stab_initialize(get_paca()->stab_addr);
  682. }
  683. #endif /* CONFIG_SMP */
  684. /*
  685. * Called by asm hashtable.S for doing lazy icache flush
  686. */
  687. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  688. {
  689. struct page *page;
  690. if (!pfn_valid(pte_pfn(pte)))
  691. return pp;
  692. page = pte_page(pte);
  693. /* page is dirty */
  694. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  695. if (trap == 0x400) {
  696. flush_dcache_icache_page(page);
  697. set_bit(PG_arch_1, &page->flags);
  698. } else
  699. pp |= HPTE_R_N;
  700. }
  701. return pp;
  702. }
  703. #ifdef CONFIG_PPC_MM_SLICES
  704. unsigned int get_paca_psize(unsigned long addr)
  705. {
  706. u64 lpsizes;
  707. unsigned char *hpsizes;
  708. unsigned long index, mask_index;
  709. if (addr < SLICE_LOW_TOP) {
  710. lpsizes = get_paca()->context.low_slices_psize;
  711. index = GET_LOW_SLICE_INDEX(addr);
  712. return (lpsizes >> (index * 4)) & 0xF;
  713. }
  714. hpsizes = get_paca()->context.high_slices_psize;
  715. index = GET_HIGH_SLICE_INDEX(addr);
  716. mask_index = index & 0x1;
  717. return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xF;
  718. }
  719. #else
  720. unsigned int get_paca_psize(unsigned long addr)
  721. {
  722. return get_paca()->context.user_psize;
  723. }
  724. #endif
  725. /*
  726. * Demote a segment to using 4k pages.
  727. * For now this makes the whole process use 4k pages.
  728. */
  729. #ifdef CONFIG_PPC_64K_PAGES
  730. void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  731. {
  732. if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
  733. return;
  734. slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
  735. #ifdef CONFIG_SPU_BASE
  736. spu_flush_all_slbs(mm);
  737. #endif
  738. if (get_paca_psize(addr) != MMU_PAGE_4K) {
  739. get_paca()->context = mm->context;
  740. slb_flush_and_rebolt();
  741. }
  742. }
  743. #endif /* CONFIG_PPC_64K_PAGES */
  744. #ifdef CONFIG_PPC_SUBPAGE_PROT
  745. /*
  746. * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
  747. * Userspace sets the subpage permissions using the subpage_prot system call.
  748. *
  749. * Result is 0: full permissions, _PAGE_RW: read-only,
  750. * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
  751. */
  752. static int subpage_protection(struct mm_struct *mm, unsigned long ea)
  753. {
  754. struct subpage_prot_table *spt = &mm->context.spt;
  755. u32 spp = 0;
  756. u32 **sbpm, *sbpp;
  757. if (ea >= spt->maxaddr)
  758. return 0;
  759. if (ea < 0x100000000) {
  760. /* addresses below 4GB use spt->low_prot */
  761. sbpm = spt->low_prot;
  762. } else {
  763. sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
  764. if (!sbpm)
  765. return 0;
  766. }
  767. sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  768. if (!sbpp)
  769. return 0;
  770. spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
  771. /* extract 2-bit bitfield for this 4k subpage */
  772. spp >>= 30 - 2 * ((ea >> 12) & 0xf);
  773. /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
  774. spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
  775. return spp;
  776. }
  777. #else /* CONFIG_PPC_SUBPAGE_PROT */
  778. static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
  779. {
  780. return 0;
  781. }
  782. #endif
  783. void hash_failure_debug(unsigned long ea, unsigned long access,
  784. unsigned long vsid, unsigned long trap,
  785. int ssize, int psize, unsigned long pte)
  786. {
  787. if (!printk_ratelimit())
  788. return;
  789. pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
  790. ea, access, current->comm);
  791. pr_info(" trap=0x%lx vsid=0x%lx ssize=%d psize=%d pte=0x%lx\n",
  792. trap, vsid, ssize, psize, pte);
  793. }
  794. /* Result code is:
  795. * 0 - handled
  796. * 1 - normal page fault
  797. * -1 - critical hash insertion error
  798. * -2 - access not permitted by subpage protection mechanism
  799. */
  800. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  801. {
  802. pgd_t *pgdir;
  803. unsigned long vsid;
  804. struct mm_struct *mm;
  805. pte_t *ptep;
  806. unsigned hugeshift;
  807. const struct cpumask *tmp;
  808. int rc, user_region = 0, local = 0;
  809. int psize, ssize;
  810. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  811. ea, access, trap);
  812. if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
  813. DBG_LOW(" out of pgtable range !\n");
  814. return 1;
  815. }
  816. /* Get region & vsid */
  817. switch (REGION_ID(ea)) {
  818. case USER_REGION_ID:
  819. user_region = 1;
  820. mm = current->mm;
  821. if (! mm) {
  822. DBG_LOW(" user region with no mm !\n");
  823. return 1;
  824. }
  825. psize = get_slice_psize(mm, ea);
  826. ssize = user_segment_size(ea);
  827. vsid = get_vsid(mm->context.id, ea, ssize);
  828. break;
  829. case VMALLOC_REGION_ID:
  830. mm = &init_mm;
  831. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  832. if (ea < VMALLOC_END)
  833. psize = mmu_vmalloc_psize;
  834. else
  835. psize = mmu_io_psize;
  836. ssize = mmu_kernel_ssize;
  837. break;
  838. default:
  839. /* Not a valid range
  840. * Send the problem up to do_page_fault
  841. */
  842. return 1;
  843. }
  844. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  845. /* Get pgdir */
  846. pgdir = mm->pgd;
  847. if (pgdir == NULL)
  848. return 1;
  849. /* Check CPU locality */
  850. tmp = cpumask_of(smp_processor_id());
  851. if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
  852. local = 1;
  853. #ifndef CONFIG_PPC_64K_PAGES
  854. /* If we use 4K pages and our psize is not 4K, then we might
  855. * be hitting a special driver mapping, and need to align the
  856. * address before we fetch the PTE.
  857. *
  858. * It could also be a hugepage mapping, in which case this is
  859. * not necessary, but it's not harmful, either.
  860. */
  861. if (psize != MMU_PAGE_4K)
  862. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  863. #endif /* CONFIG_PPC_64K_PAGES */
  864. /* Get PTE and page size from page tables */
  865. ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugeshift);
  866. if (ptep == NULL || !pte_present(*ptep)) {
  867. DBG_LOW(" no PTE !\n");
  868. return 1;
  869. }
  870. /* Add _PAGE_PRESENT to the required access perm */
  871. access |= _PAGE_PRESENT;
  872. /* Pre-check access permissions (will be re-checked atomically
  873. * in __hash_page_XX but this pre-check is a fast path
  874. */
  875. if (access & ~pte_val(*ptep)) {
  876. DBG_LOW(" no access !\n");
  877. return 1;
  878. }
  879. #ifdef CONFIG_HUGETLB_PAGE
  880. if (hugeshift)
  881. return __hash_page_huge(ea, access, vsid, ptep, trap, local,
  882. ssize, hugeshift, psize);
  883. #endif /* CONFIG_HUGETLB_PAGE */
  884. #ifndef CONFIG_PPC_64K_PAGES
  885. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  886. #else
  887. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  888. pte_val(*(ptep + PTRS_PER_PTE)));
  889. #endif
  890. /* Do actual hashing */
  891. #ifdef CONFIG_PPC_64K_PAGES
  892. /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
  893. if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
  894. demote_segment_4k(mm, ea);
  895. psize = MMU_PAGE_4K;
  896. }
  897. /* If this PTE is non-cacheable and we have restrictions on
  898. * using non cacheable large pages, then we switch to 4k
  899. */
  900. if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
  901. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  902. if (user_region) {
  903. demote_segment_4k(mm, ea);
  904. psize = MMU_PAGE_4K;
  905. } else if (ea < VMALLOC_END) {
  906. /*
  907. * some driver did a non-cacheable mapping
  908. * in vmalloc space, so switch vmalloc
  909. * to 4k pages
  910. */
  911. printk(KERN_ALERT "Reducing vmalloc segment "
  912. "to 4kB pages because of "
  913. "non-cacheable mapping\n");
  914. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  915. #ifdef CONFIG_SPU_BASE
  916. spu_flush_all_slbs(mm);
  917. #endif
  918. }
  919. }
  920. if (user_region) {
  921. if (psize != get_paca_psize(ea)) {
  922. get_paca()->context = mm->context;
  923. slb_flush_and_rebolt();
  924. }
  925. } else if (get_paca()->vmalloc_sllp !=
  926. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  927. get_paca()->vmalloc_sllp =
  928. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  929. slb_vmalloc_update();
  930. }
  931. #endif /* CONFIG_PPC_64K_PAGES */
  932. #ifdef CONFIG_PPC_HAS_HASH_64K
  933. if (psize == MMU_PAGE_64K)
  934. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  935. else
  936. #endif /* CONFIG_PPC_HAS_HASH_64K */
  937. {
  938. int spp = subpage_protection(mm, ea);
  939. if (access & spp)
  940. rc = -2;
  941. else
  942. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  943. local, ssize, spp);
  944. }
  945. /* Dump some info in case of hash insertion failure, they should
  946. * never happen so it is really useful to know if/when they do
  947. */
  948. if (rc == -1)
  949. hash_failure_debug(ea, access, vsid, trap, ssize, psize,
  950. pte_val(*ptep));
  951. #ifndef CONFIG_PPC_64K_PAGES
  952. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  953. #else
  954. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  955. pte_val(*(ptep + PTRS_PER_PTE)));
  956. #endif
  957. DBG_LOW(" -> rc=%d\n", rc);
  958. return rc;
  959. }
  960. EXPORT_SYMBOL_GPL(hash_page);
  961. void hash_preload(struct mm_struct *mm, unsigned long ea,
  962. unsigned long access, unsigned long trap)
  963. {
  964. unsigned long vsid;
  965. pgd_t *pgdir;
  966. pte_t *ptep;
  967. unsigned long flags;
  968. int rc, ssize, local = 0;
  969. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  970. #ifdef CONFIG_PPC_MM_SLICES
  971. /* We only prefault standard pages for now */
  972. if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
  973. return;
  974. #endif
  975. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  976. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  977. /* Get Linux PTE if available */
  978. pgdir = mm->pgd;
  979. if (pgdir == NULL)
  980. return;
  981. ptep = find_linux_pte(pgdir, ea);
  982. if (!ptep)
  983. return;
  984. #ifdef CONFIG_PPC_64K_PAGES
  985. /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
  986. * a 64K kernel), then we don't preload, hash_page() will take
  987. * care of it once we actually try to access the page.
  988. * That way we don't have to duplicate all of the logic for segment
  989. * page size demotion here
  990. */
  991. if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
  992. return;
  993. #endif /* CONFIG_PPC_64K_PAGES */
  994. /* Get VSID */
  995. ssize = user_segment_size(ea);
  996. vsid = get_vsid(mm->context.id, ea, ssize);
  997. /* Hash doesn't like irqs */
  998. local_irq_save(flags);
  999. /* Is that local to this CPU ? */
  1000. if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  1001. local = 1;
  1002. /* Hash it in */
  1003. #ifdef CONFIG_PPC_HAS_HASH_64K
  1004. if (mm->context.user_psize == MMU_PAGE_64K)
  1005. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  1006. else
  1007. #endif /* CONFIG_PPC_HAS_HASH_64K */
  1008. rc = __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
  1009. subpage_protection(mm, ea));
  1010. /* Dump some info in case of hash insertion failure, they should
  1011. * never happen so it is really useful to know if/when they do
  1012. */
  1013. if (rc == -1)
  1014. hash_failure_debug(ea, access, vsid, trap, ssize,
  1015. mm->context.user_psize, pte_val(*ptep));
  1016. local_irq_restore(flags);
  1017. }
  1018. /* WARNING: This is called from hash_low_64.S, if you change this prototype,
  1019. * do not forget to update the assembly call site !
  1020. */
  1021. void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
  1022. int local)
  1023. {
  1024. unsigned long hash, index, shift, hidx, slot;
  1025. DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
  1026. pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
  1027. hash = hpt_hash(vpn, shift, ssize);
  1028. hidx = __rpte_to_hidx(pte, index);
  1029. if (hidx & _PTEIDX_SECONDARY)
  1030. hash = ~hash;
  1031. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1032. slot += hidx & _PTEIDX_GROUP_IX;
  1033. DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
  1034. ppc_md.hpte_invalidate(slot, vpn, psize, ssize, local);
  1035. } pte_iterate_hashed_end();
  1036. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1037. /* Transactions are not aborted by tlbiel, only tlbie.
  1038. * Without, syncing a page back to a block device w/ PIO could pick up
  1039. * transactional data (bad!) so we force an abort here. Before the
  1040. * sync the page will be made read-only, which will flush_hash_page.
  1041. * BIG ISSUE here: if the kernel uses a page from userspace without
  1042. * unmapping it first, it may see the speculated version.
  1043. */
  1044. if (local && cpu_has_feature(CPU_FTR_TM) &&
  1045. MSR_TM_ACTIVE(current->thread.regs->msr)) {
  1046. tm_enable();
  1047. tm_abort(TM_CAUSE_TLBI);
  1048. }
  1049. #endif
  1050. }
  1051. void flush_hash_range(unsigned long number, int local)
  1052. {
  1053. if (ppc_md.flush_hash_range)
  1054. ppc_md.flush_hash_range(number, local);
  1055. else {
  1056. int i;
  1057. struct ppc64_tlb_batch *batch =
  1058. &__get_cpu_var(ppc64_tlb_batch);
  1059. for (i = 0; i < number; i++)
  1060. flush_hash_page(batch->vpn[i], batch->pte[i],
  1061. batch->psize, batch->ssize, local);
  1062. }
  1063. }
  1064. /*
  1065. * low_hash_fault is called when we the low level hash code failed
  1066. * to instert a PTE due to an hypervisor error
  1067. */
  1068. void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
  1069. {
  1070. if (user_mode(regs)) {
  1071. #ifdef CONFIG_PPC_SUBPAGE_PROT
  1072. if (rc == -2)
  1073. _exception(SIGSEGV, regs, SEGV_ACCERR, address);
  1074. else
  1075. #endif
  1076. _exception(SIGBUS, regs, BUS_ADRERR, address);
  1077. } else
  1078. bad_page_fault(regs, address, SIGBUS);
  1079. }
  1080. #ifdef CONFIG_DEBUG_PAGEALLOC
  1081. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  1082. {
  1083. unsigned long hash, hpteg;
  1084. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1085. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1086. unsigned long mode = htab_convert_pte_flags(PAGE_KERNEL);
  1087. int ret;
  1088. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1089. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  1090. ret = ppc_md.hpte_insert(hpteg, vpn, __pa(vaddr),
  1091. mode, HPTE_V_BOLTED,
  1092. mmu_linear_psize, mmu_kernel_ssize);
  1093. BUG_ON (ret < 0);
  1094. spin_lock(&linear_map_hash_lock);
  1095. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  1096. linear_map_hash_slots[lmi] = ret | 0x80;
  1097. spin_unlock(&linear_map_hash_lock);
  1098. }
  1099. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  1100. {
  1101. unsigned long hash, hidx, slot;
  1102. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1103. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1104. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1105. spin_lock(&linear_map_hash_lock);
  1106. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  1107. hidx = linear_map_hash_slots[lmi] & 0x7f;
  1108. linear_map_hash_slots[lmi] = 0;
  1109. spin_unlock(&linear_map_hash_lock);
  1110. if (hidx & _PTEIDX_SECONDARY)
  1111. hash = ~hash;
  1112. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1113. slot += hidx & _PTEIDX_GROUP_IX;
  1114. ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_kernel_ssize, 0);
  1115. }
  1116. void kernel_map_pages(struct page *page, int numpages, int enable)
  1117. {
  1118. unsigned long flags, vaddr, lmi;
  1119. int i;
  1120. local_irq_save(flags);
  1121. for (i = 0; i < numpages; i++, page++) {
  1122. vaddr = (unsigned long)page_address(page);
  1123. lmi = __pa(vaddr) >> PAGE_SHIFT;
  1124. if (lmi >= linear_map_hash_count)
  1125. continue;
  1126. if (enable)
  1127. kernel_map_linear_page(vaddr, lmi);
  1128. else
  1129. kernel_unmap_linear_page(vaddr, lmi);
  1130. }
  1131. local_irq_restore(flags);
  1132. }
  1133. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1134. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  1135. phys_addr_t first_memblock_size)
  1136. {
  1137. /* We don't currently support the first MEMBLOCK not mapping 0
  1138. * physical on those processors
  1139. */
  1140. BUG_ON(first_memblock_base != 0);
  1141. /* On LPAR systems, the first entry is our RMA region,
  1142. * non-LPAR 64-bit hash MMU systems don't have a limitation
  1143. * on real mode access, but using the first entry works well
  1144. * enough. We also clamp it to 1G to avoid some funky things
  1145. * such as RTAS bugs etc...
  1146. */
  1147. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  1148. /* Finally limit subsequent allocations */
  1149. memblock_set_current_limit(ppc64_rma_size);
  1150. }