hash_utils_64.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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/system.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/machdep.h>
  43. #include <asm/prom.h>
  44. #include <asm/abs_addr.h>
  45. #include <asm/tlbflush.h>
  46. #include <asm/io.h>
  47. #include <asm/eeh.h>
  48. #include <asm/tlb.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/cputable.h>
  51. #include <asm/sections.h>
  52. #include <asm/spu.h>
  53. #include <asm/udbg.h>
  54. #include <asm/code-patching.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 va = hpt_va(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(va, 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, va, 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. } else {
  554. /* Find storage for the HPT. Must be contiguous in
  555. * the absolute address space. On cell we want it to be
  556. * in the first 2 Gig so we can use it for IOMMU hacks.
  557. */
  558. if (machine_is(cell))
  559. limit = 0x80000000;
  560. else
  561. limit = MEMBLOCK_ALLOC_ANYWHERE;
  562. table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
  563. DBG("Hash table allocated at %lx, size: %lx\n", table,
  564. htab_size_bytes);
  565. htab_address = abs_to_virt(table);
  566. /* htab absolute addr + encoded htabsize */
  567. _SDR1 = table + __ilog2(pteg_count) - 11;
  568. /* Initialize the HPT with no entries */
  569. memset((void *)table, 0, htab_size_bytes);
  570. /* Set SDR1 */
  571. mtspr(SPRN_SDR1, _SDR1);
  572. }
  573. prot = pgprot_val(PAGE_KERNEL);
  574. #ifdef CONFIG_DEBUG_PAGEALLOC
  575. linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
  576. linear_map_hash_slots = __va(memblock_alloc_base(linear_map_hash_count,
  577. 1, ppc64_rma_size));
  578. memset(linear_map_hash_slots, 0, linear_map_hash_count);
  579. #endif /* CONFIG_DEBUG_PAGEALLOC */
  580. /* On U3 based machines, we need to reserve the DART area and
  581. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  582. * cacheable later on
  583. */
  584. /* create bolted the linear mapping in the hash table */
  585. for_each_memblock(memory, reg) {
  586. base = (unsigned long)__va(reg->base);
  587. size = reg->size;
  588. DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
  589. base, size, prot);
  590. #ifdef CONFIG_U3_DART
  591. /* Do not map the DART space. Fortunately, it will be aligned
  592. * in such a way that it will not cross two memblock regions and
  593. * will fit within a single 16Mb page.
  594. * The DART space is assumed to be a full 16Mb region even if
  595. * we only use 2Mb of that space. We will use more of it later
  596. * for AGP GART. We have to use a full 16Mb large page.
  597. */
  598. DBG("DART base: %lx\n", dart_tablebase);
  599. if (dart_tablebase != 0 && dart_tablebase >= base
  600. && dart_tablebase < (base + size)) {
  601. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  602. if (base != dart_tablebase)
  603. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  604. __pa(base), prot,
  605. mmu_linear_psize,
  606. mmu_kernel_ssize));
  607. if ((base + size) > dart_table_end)
  608. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  609. base + size,
  610. __pa(dart_table_end),
  611. prot,
  612. mmu_linear_psize,
  613. mmu_kernel_ssize));
  614. continue;
  615. }
  616. #endif /* CONFIG_U3_DART */
  617. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  618. prot, mmu_linear_psize, mmu_kernel_ssize));
  619. }
  620. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  621. /*
  622. * If we have a memory_limit and we've allocated TCEs then we need to
  623. * explicitly map the TCE area at the top of RAM. We also cope with the
  624. * case that the TCEs start below memory_limit.
  625. * tce_alloc_start/end are 16MB aligned so the mapping should work
  626. * for either 4K or 16MB pages.
  627. */
  628. if (tce_alloc_start) {
  629. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  630. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  631. if (base + size >= tce_alloc_start)
  632. tce_alloc_start = base + size + 1;
  633. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  634. __pa(tce_alloc_start), prot,
  635. mmu_linear_psize, mmu_kernel_ssize));
  636. }
  637. htab_finish_init();
  638. DBG(" <- htab_initialize()\n");
  639. }
  640. #undef KB
  641. #undef MB
  642. void __init early_init_mmu(void)
  643. {
  644. /* Setup initial STAB address in the PACA */
  645. get_paca()->stab_real = __pa((u64)&initial_stab);
  646. get_paca()->stab_addr = (u64)&initial_stab;
  647. /* Initialize the MMU Hash table and create the linear mapping
  648. * of memory. Has to be done before stab/slb initialization as
  649. * this is currently where the page size encoding is obtained
  650. */
  651. htab_initialize();
  652. /* Initialize stab / SLB management except on iSeries
  653. */
  654. if (mmu_has_feature(MMU_FTR_SLB))
  655. slb_initialize();
  656. else if (!firmware_has_feature(FW_FEATURE_ISERIES))
  657. stab_initialize(get_paca()->stab_real);
  658. }
  659. #ifdef CONFIG_SMP
  660. void __cpuinit early_init_mmu_secondary(void)
  661. {
  662. /* Initialize hash table for that CPU */
  663. if (!firmware_has_feature(FW_FEATURE_LPAR))
  664. mtspr(SPRN_SDR1, _SDR1);
  665. /* Initialize STAB/SLB. We use a virtual address as it works
  666. * in real mode on pSeries and we want a virtual address on
  667. * iSeries anyway
  668. */
  669. if (mmu_has_feature(MMU_FTR_SLB))
  670. slb_initialize();
  671. else
  672. stab_initialize(get_paca()->stab_addr);
  673. }
  674. #endif /* CONFIG_SMP */
  675. /*
  676. * Called by asm hashtable.S for doing lazy icache flush
  677. */
  678. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  679. {
  680. struct page *page;
  681. if (!pfn_valid(pte_pfn(pte)))
  682. return pp;
  683. page = pte_page(pte);
  684. /* page is dirty */
  685. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  686. if (trap == 0x400) {
  687. flush_dcache_icache_page(page);
  688. set_bit(PG_arch_1, &page->flags);
  689. } else
  690. pp |= HPTE_R_N;
  691. }
  692. return pp;
  693. }
  694. #ifdef CONFIG_PPC_MM_SLICES
  695. unsigned int get_paca_psize(unsigned long addr)
  696. {
  697. unsigned long index, slices;
  698. if (addr < SLICE_LOW_TOP) {
  699. slices = get_paca()->context.low_slices_psize;
  700. index = GET_LOW_SLICE_INDEX(addr);
  701. } else {
  702. slices = get_paca()->context.high_slices_psize;
  703. index = GET_HIGH_SLICE_INDEX(addr);
  704. }
  705. return (slices >> (index * 4)) & 0xF;
  706. }
  707. #else
  708. unsigned int get_paca_psize(unsigned long addr)
  709. {
  710. return get_paca()->context.user_psize;
  711. }
  712. #endif
  713. /*
  714. * Demote a segment to using 4k pages.
  715. * For now this makes the whole process use 4k pages.
  716. */
  717. #ifdef CONFIG_PPC_64K_PAGES
  718. void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  719. {
  720. if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
  721. return;
  722. slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
  723. #ifdef CONFIG_SPU_BASE
  724. spu_flush_all_slbs(mm);
  725. #endif
  726. if (get_paca_psize(addr) != MMU_PAGE_4K) {
  727. get_paca()->context = mm->context;
  728. slb_flush_and_rebolt();
  729. }
  730. }
  731. #endif /* CONFIG_PPC_64K_PAGES */
  732. #ifdef CONFIG_PPC_SUBPAGE_PROT
  733. /*
  734. * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
  735. * Userspace sets the subpage permissions using the subpage_prot system call.
  736. *
  737. * Result is 0: full permissions, _PAGE_RW: read-only,
  738. * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
  739. */
  740. static int subpage_protection(struct mm_struct *mm, unsigned long ea)
  741. {
  742. struct subpage_prot_table *spt = &mm->context.spt;
  743. u32 spp = 0;
  744. u32 **sbpm, *sbpp;
  745. if (ea >= spt->maxaddr)
  746. return 0;
  747. if (ea < 0x100000000) {
  748. /* addresses below 4GB use spt->low_prot */
  749. sbpm = spt->low_prot;
  750. } else {
  751. sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
  752. if (!sbpm)
  753. return 0;
  754. }
  755. sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  756. if (!sbpp)
  757. return 0;
  758. spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
  759. /* extract 2-bit bitfield for this 4k subpage */
  760. spp >>= 30 - 2 * ((ea >> 12) & 0xf);
  761. /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
  762. spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
  763. return spp;
  764. }
  765. #else /* CONFIG_PPC_SUBPAGE_PROT */
  766. static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
  767. {
  768. return 0;
  769. }
  770. #endif
  771. void hash_failure_debug(unsigned long ea, unsigned long access,
  772. unsigned long vsid, unsigned long trap,
  773. int ssize, int psize, unsigned long pte)
  774. {
  775. if (!printk_ratelimit())
  776. return;
  777. pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
  778. ea, access, current->comm);
  779. pr_info(" trap=0x%lx vsid=0x%lx ssize=%d psize=%d pte=0x%lx\n",
  780. trap, vsid, ssize, psize, pte);
  781. }
  782. /* Result code is:
  783. * 0 - handled
  784. * 1 - normal page fault
  785. * -1 - critical hash insertion error
  786. * -2 - access not permitted by subpage protection mechanism
  787. */
  788. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  789. {
  790. pgd_t *pgdir;
  791. unsigned long vsid;
  792. struct mm_struct *mm;
  793. pte_t *ptep;
  794. unsigned hugeshift;
  795. const struct cpumask *tmp;
  796. int rc, user_region = 0, local = 0;
  797. int psize, ssize;
  798. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  799. ea, access, trap);
  800. if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
  801. DBG_LOW(" out of pgtable range !\n");
  802. return 1;
  803. }
  804. /* Get region & vsid */
  805. switch (REGION_ID(ea)) {
  806. case USER_REGION_ID:
  807. user_region = 1;
  808. mm = current->mm;
  809. if (! mm) {
  810. DBG_LOW(" user region with no mm !\n");
  811. return 1;
  812. }
  813. psize = get_slice_psize(mm, ea);
  814. ssize = user_segment_size(ea);
  815. vsid = get_vsid(mm->context.id, ea, ssize);
  816. break;
  817. case VMALLOC_REGION_ID:
  818. mm = &init_mm;
  819. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  820. if (ea < VMALLOC_END)
  821. psize = mmu_vmalloc_psize;
  822. else
  823. psize = mmu_io_psize;
  824. ssize = mmu_kernel_ssize;
  825. break;
  826. default:
  827. /* Not a valid range
  828. * Send the problem up to do_page_fault
  829. */
  830. return 1;
  831. }
  832. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  833. /* Get pgdir */
  834. pgdir = mm->pgd;
  835. if (pgdir == NULL)
  836. return 1;
  837. /* Check CPU locality */
  838. tmp = cpumask_of(smp_processor_id());
  839. if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
  840. local = 1;
  841. #ifndef CONFIG_PPC_64K_PAGES
  842. /* If we use 4K pages and our psize is not 4K, then we might
  843. * be hitting a special driver mapping, and need to align the
  844. * address before we fetch the PTE.
  845. *
  846. * It could also be a hugepage mapping, in which case this is
  847. * not necessary, but it's not harmful, either.
  848. */
  849. if (psize != MMU_PAGE_4K)
  850. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  851. #endif /* CONFIG_PPC_64K_PAGES */
  852. /* Get PTE and page size from page tables */
  853. ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugeshift);
  854. if (ptep == NULL || !pte_present(*ptep)) {
  855. DBG_LOW(" no PTE !\n");
  856. return 1;
  857. }
  858. /* Add _PAGE_PRESENT to the required access perm */
  859. access |= _PAGE_PRESENT;
  860. /* Pre-check access permissions (will be re-checked atomically
  861. * in __hash_page_XX but this pre-check is a fast path
  862. */
  863. if (access & ~pte_val(*ptep)) {
  864. DBG_LOW(" no access !\n");
  865. return 1;
  866. }
  867. #ifdef CONFIG_HUGETLB_PAGE
  868. if (hugeshift)
  869. return __hash_page_huge(ea, access, vsid, ptep, trap, local,
  870. ssize, hugeshift, psize);
  871. #endif /* CONFIG_HUGETLB_PAGE */
  872. #ifndef CONFIG_PPC_64K_PAGES
  873. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  874. #else
  875. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  876. pte_val(*(ptep + PTRS_PER_PTE)));
  877. #endif
  878. /* Do actual hashing */
  879. #ifdef CONFIG_PPC_64K_PAGES
  880. /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
  881. if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
  882. demote_segment_4k(mm, ea);
  883. psize = MMU_PAGE_4K;
  884. }
  885. /* If this PTE is non-cacheable and we have restrictions on
  886. * using non cacheable large pages, then we switch to 4k
  887. */
  888. if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
  889. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  890. if (user_region) {
  891. demote_segment_4k(mm, ea);
  892. psize = MMU_PAGE_4K;
  893. } else if (ea < VMALLOC_END) {
  894. /*
  895. * some driver did a non-cacheable mapping
  896. * in vmalloc space, so switch vmalloc
  897. * to 4k pages
  898. */
  899. printk(KERN_ALERT "Reducing vmalloc segment "
  900. "to 4kB pages because of "
  901. "non-cacheable mapping\n");
  902. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  903. #ifdef CONFIG_SPU_BASE
  904. spu_flush_all_slbs(mm);
  905. #endif
  906. }
  907. }
  908. if (user_region) {
  909. if (psize != get_paca_psize(ea)) {
  910. get_paca()->context = mm->context;
  911. slb_flush_and_rebolt();
  912. }
  913. } else if (get_paca()->vmalloc_sllp !=
  914. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  915. get_paca()->vmalloc_sllp =
  916. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  917. slb_vmalloc_update();
  918. }
  919. #endif /* CONFIG_PPC_64K_PAGES */
  920. #ifdef CONFIG_PPC_HAS_HASH_64K
  921. if (psize == MMU_PAGE_64K)
  922. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  923. else
  924. #endif /* CONFIG_PPC_HAS_HASH_64K */
  925. {
  926. int spp = subpage_protection(mm, ea);
  927. if (access & spp)
  928. rc = -2;
  929. else
  930. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  931. local, ssize, spp);
  932. }
  933. /* Dump some info in case of hash insertion failure, they should
  934. * never happen so it is really useful to know if/when they do
  935. */
  936. if (rc == -1)
  937. hash_failure_debug(ea, access, vsid, trap, ssize, psize,
  938. pte_val(*ptep));
  939. #ifndef CONFIG_PPC_64K_PAGES
  940. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  941. #else
  942. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  943. pte_val(*(ptep + PTRS_PER_PTE)));
  944. #endif
  945. DBG_LOW(" -> rc=%d\n", rc);
  946. return rc;
  947. }
  948. EXPORT_SYMBOL_GPL(hash_page);
  949. void hash_preload(struct mm_struct *mm, unsigned long ea,
  950. unsigned long access, unsigned long trap)
  951. {
  952. unsigned long vsid;
  953. pgd_t *pgdir;
  954. pte_t *ptep;
  955. unsigned long flags;
  956. int rc, ssize, local = 0;
  957. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  958. #ifdef CONFIG_PPC_MM_SLICES
  959. /* We only prefault standard pages for now */
  960. if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
  961. return;
  962. #endif
  963. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  964. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  965. /* Get Linux PTE if available */
  966. pgdir = mm->pgd;
  967. if (pgdir == NULL)
  968. return;
  969. ptep = find_linux_pte(pgdir, ea);
  970. if (!ptep)
  971. return;
  972. #ifdef CONFIG_PPC_64K_PAGES
  973. /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
  974. * a 64K kernel), then we don't preload, hash_page() will take
  975. * care of it once we actually try to access the page.
  976. * That way we don't have to duplicate all of the logic for segment
  977. * page size demotion here
  978. */
  979. if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
  980. return;
  981. #endif /* CONFIG_PPC_64K_PAGES */
  982. /* Get VSID */
  983. ssize = user_segment_size(ea);
  984. vsid = get_vsid(mm->context.id, ea, ssize);
  985. /* Hash doesn't like irqs */
  986. local_irq_save(flags);
  987. /* Is that local to this CPU ? */
  988. if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  989. local = 1;
  990. /* Hash it in */
  991. #ifdef CONFIG_PPC_HAS_HASH_64K
  992. if (mm->context.user_psize == MMU_PAGE_64K)
  993. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  994. else
  995. #endif /* CONFIG_PPC_HAS_HASH_64K */
  996. rc = __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
  997. subpage_protection(mm, ea));
  998. /* Dump some info in case of hash insertion failure, they should
  999. * never happen so it is really useful to know if/when they do
  1000. */
  1001. if (rc == -1)
  1002. hash_failure_debug(ea, access, vsid, trap, ssize,
  1003. mm->context.user_psize, pte_val(*ptep));
  1004. local_irq_restore(flags);
  1005. }
  1006. /* WARNING: This is called from hash_low_64.S, if you change this prototype,
  1007. * do not forget to update the assembly call site !
  1008. */
  1009. void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
  1010. int local)
  1011. {
  1012. unsigned long hash, index, shift, hidx, slot;
  1013. DBG_LOW("flush_hash_page(va=%016lx)\n", va);
  1014. pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
  1015. hash = hpt_hash(va, shift, ssize);
  1016. hidx = __rpte_to_hidx(pte, index);
  1017. if (hidx & _PTEIDX_SECONDARY)
  1018. hash = ~hash;
  1019. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1020. slot += hidx & _PTEIDX_GROUP_IX;
  1021. DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
  1022. ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
  1023. } pte_iterate_hashed_end();
  1024. }
  1025. void flush_hash_range(unsigned long number, int local)
  1026. {
  1027. if (ppc_md.flush_hash_range)
  1028. ppc_md.flush_hash_range(number, local);
  1029. else {
  1030. int i;
  1031. struct ppc64_tlb_batch *batch =
  1032. &__get_cpu_var(ppc64_tlb_batch);
  1033. for (i = 0; i < number; i++)
  1034. flush_hash_page(batch->vaddr[i], batch->pte[i],
  1035. batch->psize, batch->ssize, local);
  1036. }
  1037. }
  1038. /*
  1039. * low_hash_fault is called when we the low level hash code failed
  1040. * to instert a PTE due to an hypervisor error
  1041. */
  1042. void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
  1043. {
  1044. if (user_mode(regs)) {
  1045. #ifdef CONFIG_PPC_SUBPAGE_PROT
  1046. if (rc == -2)
  1047. _exception(SIGSEGV, regs, SEGV_ACCERR, address);
  1048. else
  1049. #endif
  1050. _exception(SIGBUS, regs, BUS_ADRERR, address);
  1051. } else
  1052. bad_page_fault(regs, address, SIGBUS);
  1053. }
  1054. #ifdef CONFIG_DEBUG_PAGEALLOC
  1055. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  1056. {
  1057. unsigned long hash, hpteg;
  1058. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1059. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  1060. unsigned long mode = htab_convert_pte_flags(PAGE_KERNEL);
  1061. int ret;
  1062. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  1063. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  1064. ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
  1065. mode, HPTE_V_BOLTED,
  1066. mmu_linear_psize, mmu_kernel_ssize);
  1067. BUG_ON (ret < 0);
  1068. spin_lock(&linear_map_hash_lock);
  1069. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  1070. linear_map_hash_slots[lmi] = ret | 0x80;
  1071. spin_unlock(&linear_map_hash_lock);
  1072. }
  1073. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  1074. {
  1075. unsigned long hash, hidx, slot;
  1076. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1077. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  1078. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  1079. spin_lock(&linear_map_hash_lock);
  1080. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  1081. hidx = linear_map_hash_slots[lmi] & 0x7f;
  1082. linear_map_hash_slots[lmi] = 0;
  1083. spin_unlock(&linear_map_hash_lock);
  1084. if (hidx & _PTEIDX_SECONDARY)
  1085. hash = ~hash;
  1086. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1087. slot += hidx & _PTEIDX_GROUP_IX;
  1088. ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
  1089. }
  1090. void kernel_map_pages(struct page *page, int numpages, int enable)
  1091. {
  1092. unsigned long flags, vaddr, lmi;
  1093. int i;
  1094. local_irq_save(flags);
  1095. for (i = 0; i < numpages; i++, page++) {
  1096. vaddr = (unsigned long)page_address(page);
  1097. lmi = __pa(vaddr) >> PAGE_SHIFT;
  1098. if (lmi >= linear_map_hash_count)
  1099. continue;
  1100. if (enable)
  1101. kernel_map_linear_page(vaddr, lmi);
  1102. else
  1103. kernel_unmap_linear_page(vaddr, lmi);
  1104. }
  1105. local_irq_restore(flags);
  1106. }
  1107. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1108. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  1109. phys_addr_t first_memblock_size)
  1110. {
  1111. /* We don't currently support the first MEMBLOCK not mapping 0
  1112. * physical on those processors
  1113. */
  1114. BUG_ON(first_memblock_base != 0);
  1115. /* On LPAR systems, the first entry is our RMA region,
  1116. * non-LPAR 64-bit hash MMU systems don't have a limitation
  1117. * on real mode access, but using the first entry works well
  1118. * enough. We also clamp it to 1G to avoid some funky things
  1119. * such as RTAS bugs etc...
  1120. */
  1121. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  1122. /* Finally limit subsequent allocations */
  1123. memblock_set_current_limit(ppc64_rma_size);
  1124. }