hash_utils_64.c 34 KB

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