hash_utils_64.c 34 KB

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