hash_utils_64.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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/ctype.h>
  29. #include <linux/cache.h>
  30. #include <linux/init.h>
  31. #include <linux/signal.h>
  32. #include <asm/processor.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/mmu.h>
  35. #include <asm/mmu_context.h>
  36. #include <asm/page.h>
  37. #include <asm/types.h>
  38. #include <asm/system.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/machdep.h>
  41. #include <asm/lmb.h>
  42. #include <asm/abs_addr.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. #ifdef DEBUG
  52. #define DBG(fmt...) udbg_printf(fmt)
  53. #else
  54. #define DBG(fmt...)
  55. #endif
  56. #ifdef DEBUG_LOW
  57. #define DBG_LOW(fmt...) udbg_printf(fmt)
  58. #else
  59. #define DBG_LOW(fmt...)
  60. #endif
  61. #define KB (1024)
  62. #define MB (1024*KB)
  63. /*
  64. * Note: pte --> Linux PTE
  65. * HPTE --> PowerPC Hashed Page Table Entry
  66. *
  67. * Execution context:
  68. * htab_initialize is called with the MMU off (of course), but
  69. * the kernel has been copied down to zero so it can directly
  70. * reference global data. At this point it is very difficult
  71. * to print debug info.
  72. *
  73. */
  74. #ifdef CONFIG_U3_DART
  75. extern unsigned long dart_tablebase;
  76. #endif /* CONFIG_U3_DART */
  77. static unsigned long _SDR1;
  78. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
  79. struct hash_pte *htab_address;
  80. unsigned long htab_size_bytes;
  81. unsigned long htab_hash_mask;
  82. int mmu_linear_psize = MMU_PAGE_4K;
  83. int mmu_virtual_psize = MMU_PAGE_4K;
  84. int mmu_vmalloc_psize = MMU_PAGE_4K;
  85. int mmu_io_psize = MMU_PAGE_4K;
  86. int mmu_kernel_ssize = MMU_SEGSIZE_256M;
  87. int mmu_highuser_ssize = MMU_SEGSIZE_256M;
  88. #ifdef CONFIG_HUGETLB_PAGE
  89. int mmu_huge_psize = MMU_PAGE_16M;
  90. unsigned int HPAGE_SHIFT;
  91. #endif
  92. #ifdef CONFIG_PPC_64K_PAGES
  93. int mmu_ci_restrictions;
  94. #endif
  95. #ifdef CONFIG_DEBUG_PAGEALLOC
  96. static u8 *linear_map_hash_slots;
  97. static unsigned long linear_map_hash_count;
  98. static DEFINE_SPINLOCK(linear_map_hash_lock);
  99. #endif /* CONFIG_DEBUG_PAGEALLOC */
  100. /* There are definitions of page sizes arrays to be used when none
  101. * is provided by the firmware.
  102. */
  103. /* Pre-POWER4 CPUs (4k pages only)
  104. */
  105. struct mmu_psize_def mmu_psize_defaults_old[] = {
  106. [MMU_PAGE_4K] = {
  107. .shift = 12,
  108. .sllp = 0,
  109. .penc = 0,
  110. .avpnm = 0,
  111. .tlbiel = 0,
  112. },
  113. };
  114. /* POWER4, GPUL, POWER5
  115. *
  116. * Support for 16Mb large pages
  117. */
  118. struct mmu_psize_def mmu_psize_defaults_gp[] = {
  119. [MMU_PAGE_4K] = {
  120. .shift = 12,
  121. .sllp = 0,
  122. .penc = 0,
  123. .avpnm = 0,
  124. .tlbiel = 1,
  125. },
  126. [MMU_PAGE_16M] = {
  127. .shift = 24,
  128. .sllp = SLB_VSID_L,
  129. .penc = 0,
  130. .avpnm = 0x1UL,
  131. .tlbiel = 0,
  132. },
  133. };
  134. int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
  135. unsigned long pstart, unsigned long mode,
  136. int psize, int ssize)
  137. {
  138. unsigned long vaddr, paddr;
  139. unsigned int step, shift;
  140. unsigned long tmp_mode;
  141. int ret = 0;
  142. shift = mmu_psize_defs[psize].shift;
  143. step = 1 << shift;
  144. for (vaddr = vstart, paddr = pstart; vaddr < vend;
  145. vaddr += step, paddr += step) {
  146. unsigned long hash, hpteg;
  147. unsigned long vsid = get_kernel_vsid(vaddr, ssize);
  148. unsigned long va = hpt_va(vaddr, vsid, ssize);
  149. tmp_mode = mode;
  150. /* Make non-kernel text non-executable */
  151. if (!in_kernel_text(vaddr))
  152. tmp_mode = mode | HPTE_R_N;
  153. hash = hpt_hash(va, shift, ssize);
  154. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  155. DBG("htab_bolt_mapping: calling %p\n", ppc_md.hpte_insert);
  156. BUG_ON(!ppc_md.hpte_insert);
  157. ret = ppc_md.hpte_insert(hpteg, va, paddr,
  158. tmp_mode, HPTE_V_BOLTED, psize, ssize);
  159. if (ret < 0)
  160. break;
  161. #ifdef CONFIG_DEBUG_PAGEALLOC
  162. if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
  163. linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
  164. #endif /* CONFIG_DEBUG_PAGEALLOC */
  165. }
  166. return ret < 0 ? ret : 0;
  167. }
  168. static int __init htab_dt_scan_seg_sizes(unsigned long node,
  169. const char *uname, int depth,
  170. void *data)
  171. {
  172. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  173. u32 *prop;
  174. unsigned long size = 0;
  175. /* We are scanning "cpu" nodes only */
  176. if (type == NULL || strcmp(type, "cpu") != 0)
  177. return 0;
  178. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
  179. &size);
  180. if (prop == NULL)
  181. return 0;
  182. for (; size >= 4; size -= 4, ++prop) {
  183. if (prop[0] == 40) {
  184. DBG("1T segment support detected\n");
  185. cur_cpu_spec->cpu_features |= CPU_FTR_1T_SEGMENT;
  186. }
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. static void __init htab_init_seg_sizes(void)
  192. {
  193. of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
  194. }
  195. static int __init htab_dt_scan_page_sizes(unsigned long node,
  196. const char *uname, int depth,
  197. void *data)
  198. {
  199. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  200. u32 *prop;
  201. unsigned long size = 0;
  202. /* We are scanning "cpu" nodes only */
  203. if (type == NULL || strcmp(type, "cpu") != 0)
  204. return 0;
  205. prop = (u32 *)of_get_flat_dt_prop(node,
  206. "ibm,segment-page-sizes", &size);
  207. if (prop != NULL) {
  208. DBG("Page sizes from device-tree:\n");
  209. size /= 4;
  210. cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
  211. while(size > 0) {
  212. unsigned int shift = prop[0];
  213. unsigned int slbenc = prop[1];
  214. unsigned int lpnum = prop[2];
  215. unsigned int lpenc = 0;
  216. struct mmu_psize_def *def;
  217. int idx = -1;
  218. size -= 3; prop += 3;
  219. while(size > 0 && lpnum) {
  220. if (prop[0] == shift)
  221. lpenc = prop[1];
  222. prop += 2; size -= 2;
  223. lpnum--;
  224. }
  225. switch(shift) {
  226. case 0xc:
  227. idx = MMU_PAGE_4K;
  228. break;
  229. case 0x10:
  230. idx = MMU_PAGE_64K;
  231. break;
  232. case 0x14:
  233. idx = MMU_PAGE_1M;
  234. break;
  235. case 0x18:
  236. idx = MMU_PAGE_16M;
  237. cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
  238. break;
  239. case 0x22:
  240. idx = MMU_PAGE_16G;
  241. break;
  242. }
  243. if (idx < 0)
  244. continue;
  245. def = &mmu_psize_defs[idx];
  246. def->shift = shift;
  247. if (shift <= 23)
  248. def->avpnm = 0;
  249. else
  250. def->avpnm = (1 << (shift - 23)) - 1;
  251. def->sllp = slbenc;
  252. def->penc = lpenc;
  253. /* We don't know for sure what's up with tlbiel, so
  254. * for now we only set it for 4K and 64K pages
  255. */
  256. if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
  257. def->tlbiel = 1;
  258. else
  259. def->tlbiel = 0;
  260. DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
  261. "tlbiel=%d, penc=%d\n",
  262. idx, shift, def->sllp, def->avpnm, def->tlbiel,
  263. def->penc);
  264. }
  265. return 1;
  266. }
  267. return 0;
  268. }
  269. static void __init htab_init_page_sizes(void)
  270. {
  271. int rc;
  272. /* Default to 4K pages only */
  273. memcpy(mmu_psize_defs, mmu_psize_defaults_old,
  274. sizeof(mmu_psize_defaults_old));
  275. /*
  276. * Try to find the available page sizes in the device-tree
  277. */
  278. rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
  279. if (rc != 0) /* Found */
  280. goto found;
  281. /*
  282. * Not in the device-tree, let's fallback on known size
  283. * list for 16M capable GP & GR
  284. */
  285. if (cpu_has_feature(CPU_FTR_16M_PAGE))
  286. memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
  287. sizeof(mmu_psize_defaults_gp));
  288. found:
  289. #ifndef CONFIG_DEBUG_PAGEALLOC
  290. /*
  291. * Pick a size for the linear mapping. Currently, we only support
  292. * 16M, 1M and 4K which is the default
  293. */
  294. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  295. mmu_linear_psize = MMU_PAGE_16M;
  296. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  297. mmu_linear_psize = MMU_PAGE_1M;
  298. #endif /* CONFIG_DEBUG_PAGEALLOC */
  299. #ifdef CONFIG_PPC_64K_PAGES
  300. /*
  301. * Pick a size for the ordinary pages. Default is 4K, we support
  302. * 64K for user mappings and vmalloc if supported by the processor.
  303. * We only use 64k for ioremap if the processor
  304. * (and firmware) support cache-inhibited large pages.
  305. * If not, we use 4k and set mmu_ci_restrictions so that
  306. * hash_page knows to switch processes that use cache-inhibited
  307. * mappings to 4k pages.
  308. */
  309. if (mmu_psize_defs[MMU_PAGE_64K].shift) {
  310. mmu_virtual_psize = MMU_PAGE_64K;
  311. mmu_vmalloc_psize = MMU_PAGE_64K;
  312. if (mmu_linear_psize == MMU_PAGE_4K)
  313. mmu_linear_psize = MMU_PAGE_64K;
  314. if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE))
  315. mmu_io_psize = MMU_PAGE_64K;
  316. else
  317. mmu_ci_restrictions = 1;
  318. }
  319. #endif /* CONFIG_PPC_64K_PAGES */
  320. printk(KERN_DEBUG "Page orders: linear mapping = %d, "
  321. "virtual = %d, io = %d\n",
  322. mmu_psize_defs[mmu_linear_psize].shift,
  323. mmu_psize_defs[mmu_virtual_psize].shift,
  324. mmu_psize_defs[mmu_io_psize].shift);
  325. #ifdef CONFIG_HUGETLB_PAGE
  326. /* Init large page size. Currently, we pick 16M or 1M depending
  327. * on what is available
  328. */
  329. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  330. mmu_huge_psize = MMU_PAGE_16M;
  331. /* With 4k/4level pagetables, we can't (for now) cope with a
  332. * huge page size < PMD_SIZE */
  333. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  334. mmu_huge_psize = MMU_PAGE_1M;
  335. /* Calculate HPAGE_SHIFT and sanity check it */
  336. if (mmu_psize_defs[mmu_huge_psize].shift > MIN_HUGEPTE_SHIFT &&
  337. mmu_psize_defs[mmu_huge_psize].shift < SID_SHIFT)
  338. HPAGE_SHIFT = mmu_psize_defs[mmu_huge_psize].shift;
  339. else
  340. HPAGE_SHIFT = 0; /* No huge pages dude ! */
  341. #endif /* CONFIG_HUGETLB_PAGE */
  342. }
  343. static int __init htab_dt_scan_pftsize(unsigned long node,
  344. const char *uname, int depth,
  345. void *data)
  346. {
  347. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  348. u32 *prop;
  349. /* We are scanning "cpu" nodes only */
  350. if (type == NULL || strcmp(type, "cpu") != 0)
  351. return 0;
  352. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
  353. if (prop != NULL) {
  354. /* pft_size[0] is the NUMA CEC cookie */
  355. ppc64_pft_size = prop[1];
  356. return 1;
  357. }
  358. return 0;
  359. }
  360. static unsigned long __init htab_get_table_size(void)
  361. {
  362. unsigned long mem_size, rnd_mem_size, pteg_count;
  363. /* If hash size isn't already provided by the platform, we try to
  364. * retrieve it from the device-tree. If it's not there neither, we
  365. * calculate it now based on the total RAM size
  366. */
  367. if (ppc64_pft_size == 0)
  368. of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
  369. if (ppc64_pft_size)
  370. return 1UL << ppc64_pft_size;
  371. /* round mem_size up to next power of 2 */
  372. mem_size = lmb_phys_mem_size();
  373. rnd_mem_size = 1UL << __ilog2(mem_size);
  374. if (rnd_mem_size < mem_size)
  375. rnd_mem_size <<= 1;
  376. /* # pages / 2 */
  377. pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
  378. return pteg_count << 7;
  379. }
  380. #ifdef CONFIG_MEMORY_HOTPLUG
  381. void create_section_mapping(unsigned long start, unsigned long end)
  382. {
  383. BUG_ON(htab_bolt_mapping(start, end, __pa(start),
  384. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
  385. mmu_linear_psize, mmu_kernel_ssize));
  386. }
  387. #endif /* CONFIG_MEMORY_HOTPLUG */
  388. static inline void make_bl(unsigned int *insn_addr, void *func)
  389. {
  390. unsigned long funcp = *((unsigned long *)func);
  391. int offset = funcp - (unsigned long)insn_addr;
  392. *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
  393. flush_icache_range((unsigned long)insn_addr, 4+
  394. (unsigned long)insn_addr);
  395. }
  396. static void __init htab_finish_init(void)
  397. {
  398. extern unsigned int *htab_call_hpte_insert1;
  399. extern unsigned int *htab_call_hpte_insert2;
  400. extern unsigned int *htab_call_hpte_remove;
  401. extern unsigned int *htab_call_hpte_updatepp;
  402. #ifdef CONFIG_PPC_HAS_HASH_64K
  403. extern unsigned int *ht64_call_hpte_insert1;
  404. extern unsigned int *ht64_call_hpte_insert2;
  405. extern unsigned int *ht64_call_hpte_remove;
  406. extern unsigned int *ht64_call_hpte_updatepp;
  407. make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
  408. make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
  409. make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
  410. make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
  411. #endif /* CONFIG_PPC_HAS_HASH_64K */
  412. make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
  413. make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
  414. make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
  415. make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
  416. }
  417. void __init htab_initialize(void)
  418. {
  419. unsigned long table;
  420. unsigned long pteg_count;
  421. unsigned long mode_rw;
  422. unsigned long base = 0, size = 0;
  423. int i;
  424. extern unsigned long tce_alloc_start, tce_alloc_end;
  425. DBG(" -> htab_initialize()\n");
  426. /* Initialize segment sizes */
  427. htab_init_seg_sizes();
  428. /* Initialize page sizes */
  429. htab_init_page_sizes();
  430. if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
  431. mmu_kernel_ssize = MMU_SEGSIZE_1T;
  432. mmu_highuser_ssize = MMU_SEGSIZE_1T;
  433. printk(KERN_INFO "Using 1TB segments\n");
  434. }
  435. /*
  436. * Calculate the required size of the htab. We want the number of
  437. * PTEGs to equal one half the number of real pages.
  438. */
  439. htab_size_bytes = htab_get_table_size();
  440. pteg_count = htab_size_bytes >> 7;
  441. htab_hash_mask = pteg_count - 1;
  442. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  443. /* Using a hypervisor which owns the htab */
  444. htab_address = NULL;
  445. _SDR1 = 0;
  446. } else {
  447. /* Find storage for the HPT. Must be contiguous in
  448. * the absolute address space.
  449. */
  450. table = lmb_alloc(htab_size_bytes, htab_size_bytes);
  451. DBG("Hash table allocated at %lx, size: %lx\n", table,
  452. htab_size_bytes);
  453. htab_address = abs_to_virt(table);
  454. /* htab absolute addr + encoded htabsize */
  455. _SDR1 = table + __ilog2(pteg_count) - 11;
  456. /* Initialize the HPT with no entries */
  457. memset((void *)table, 0, htab_size_bytes);
  458. /* Set SDR1 */
  459. mtspr(SPRN_SDR1, _SDR1);
  460. }
  461. mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
  462. #ifdef CONFIG_DEBUG_PAGEALLOC
  463. linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
  464. linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
  465. 1, lmb.rmo_size));
  466. memset(linear_map_hash_slots, 0, linear_map_hash_count);
  467. #endif /* CONFIG_DEBUG_PAGEALLOC */
  468. /* On U3 based machines, we need to reserve the DART area and
  469. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  470. * cacheable later on
  471. */
  472. /* create bolted the linear mapping in the hash table */
  473. for (i=0; i < lmb.memory.cnt; i++) {
  474. base = (unsigned long)__va(lmb.memory.region[i].base);
  475. size = lmb.memory.region[i].size;
  476. DBG("creating mapping for region: %lx : %lx\n", base, size);
  477. #ifdef CONFIG_U3_DART
  478. /* Do not map the DART space. Fortunately, it will be aligned
  479. * in such a way that it will not cross two lmb regions and
  480. * will fit within a single 16Mb page.
  481. * The DART space is assumed to be a full 16Mb region even if
  482. * we only use 2Mb of that space. We will use more of it later
  483. * for AGP GART. We have to use a full 16Mb large page.
  484. */
  485. DBG("DART base: %lx\n", dart_tablebase);
  486. if (dart_tablebase != 0 && dart_tablebase >= base
  487. && dart_tablebase < (base + size)) {
  488. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  489. if (base != dart_tablebase)
  490. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  491. __pa(base), mode_rw,
  492. mmu_linear_psize,
  493. mmu_kernel_ssize));
  494. if ((base + size) > dart_table_end)
  495. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  496. base + size,
  497. __pa(dart_table_end),
  498. mode_rw,
  499. mmu_linear_psize,
  500. mmu_kernel_ssize));
  501. continue;
  502. }
  503. #endif /* CONFIG_U3_DART */
  504. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  505. mode_rw, mmu_linear_psize, mmu_kernel_ssize));
  506. }
  507. /*
  508. * If we have a memory_limit and we've allocated TCEs then we need to
  509. * explicitly map the TCE area at the top of RAM. We also cope with the
  510. * case that the TCEs start below memory_limit.
  511. * tce_alloc_start/end are 16MB aligned so the mapping should work
  512. * for either 4K or 16MB pages.
  513. */
  514. if (tce_alloc_start) {
  515. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  516. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  517. if (base + size >= tce_alloc_start)
  518. tce_alloc_start = base + size + 1;
  519. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  520. __pa(tce_alloc_start), mode_rw,
  521. mmu_linear_psize, mmu_kernel_ssize));
  522. }
  523. htab_finish_init();
  524. DBG(" <- htab_initialize()\n");
  525. }
  526. #undef KB
  527. #undef MB
  528. void htab_initialize_secondary(void)
  529. {
  530. if (!firmware_has_feature(FW_FEATURE_LPAR))
  531. mtspr(SPRN_SDR1, _SDR1);
  532. }
  533. /*
  534. * Called by asm hashtable.S for doing lazy icache flush
  535. */
  536. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  537. {
  538. struct page *page;
  539. if (!pfn_valid(pte_pfn(pte)))
  540. return pp;
  541. page = pte_page(pte);
  542. /* page is dirty */
  543. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  544. if (trap == 0x400) {
  545. __flush_dcache_icache(page_address(page));
  546. set_bit(PG_arch_1, &page->flags);
  547. } else
  548. pp |= HPTE_R_N;
  549. }
  550. return pp;
  551. }
  552. /*
  553. * Demote a segment to using 4k pages.
  554. * For now this makes the whole process use 4k pages.
  555. */
  556. #ifdef CONFIG_PPC_64K_PAGES
  557. static void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  558. {
  559. if (mm->context.user_psize == MMU_PAGE_4K)
  560. return;
  561. slice_set_user_psize(mm, MMU_PAGE_4K);
  562. #ifdef CONFIG_SPU_BASE
  563. spu_flush_all_slbs(mm);
  564. #endif
  565. }
  566. #endif /* CONFIG_PPC_64K_PAGES */
  567. /* Result code is:
  568. * 0 - handled
  569. * 1 - normal page fault
  570. * -1 - critical hash insertion error
  571. */
  572. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  573. {
  574. void *pgdir;
  575. unsigned long vsid;
  576. struct mm_struct *mm;
  577. pte_t *ptep;
  578. cpumask_t tmp;
  579. int rc, user_region = 0, local = 0;
  580. int psize, ssize;
  581. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  582. ea, access, trap);
  583. if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
  584. DBG_LOW(" out of pgtable range !\n");
  585. return 1;
  586. }
  587. /* Get region & vsid */
  588. switch (REGION_ID(ea)) {
  589. case USER_REGION_ID:
  590. user_region = 1;
  591. mm = current->mm;
  592. if (! mm) {
  593. DBG_LOW(" user region with no mm !\n");
  594. return 1;
  595. }
  596. #ifdef CONFIG_PPC_MM_SLICES
  597. psize = get_slice_psize(mm, ea);
  598. #else
  599. psize = mm->context.user_psize;
  600. #endif
  601. ssize = user_segment_size(ea);
  602. vsid = get_vsid(mm->context.id, ea, ssize);
  603. break;
  604. case VMALLOC_REGION_ID:
  605. mm = &init_mm;
  606. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  607. if (ea < VMALLOC_END)
  608. psize = mmu_vmalloc_psize;
  609. else
  610. psize = mmu_io_psize;
  611. ssize = mmu_kernel_ssize;
  612. break;
  613. default:
  614. /* Not a valid range
  615. * Send the problem up to do_page_fault
  616. */
  617. return 1;
  618. }
  619. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  620. /* Get pgdir */
  621. pgdir = mm->pgd;
  622. if (pgdir == NULL)
  623. return 1;
  624. /* Check CPU locality */
  625. tmp = cpumask_of_cpu(smp_processor_id());
  626. if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
  627. local = 1;
  628. #ifdef CONFIG_HUGETLB_PAGE
  629. /* Handle hugepage regions */
  630. if (HPAGE_SHIFT && psize == mmu_huge_psize) {
  631. DBG_LOW(" -> huge page !\n");
  632. return hash_huge_page(mm, access, ea, vsid, local, trap);
  633. }
  634. #endif /* CONFIG_HUGETLB_PAGE */
  635. #ifndef CONFIG_PPC_64K_PAGES
  636. /* If we use 4K pages and our psize is not 4K, then we are hitting
  637. * a special driver mapping, we need to align the address before
  638. * we fetch the PTE
  639. */
  640. if (psize != MMU_PAGE_4K)
  641. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  642. #endif /* CONFIG_PPC_64K_PAGES */
  643. /* Get PTE and page size from page tables */
  644. ptep = find_linux_pte(pgdir, ea);
  645. if (ptep == NULL || !pte_present(*ptep)) {
  646. DBG_LOW(" no PTE !\n");
  647. return 1;
  648. }
  649. #ifndef CONFIG_PPC_64K_PAGES
  650. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  651. #else
  652. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  653. pte_val(*(ptep + PTRS_PER_PTE)));
  654. #endif
  655. /* Pre-check access permissions (will be re-checked atomically
  656. * in __hash_page_XX but this pre-check is a fast path
  657. */
  658. if (access & ~pte_val(*ptep)) {
  659. DBG_LOW(" no access !\n");
  660. return 1;
  661. }
  662. /* Do actual hashing */
  663. #ifdef CONFIG_PPC_64K_PAGES
  664. /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
  665. if (pte_val(*ptep) & _PAGE_4K_PFN) {
  666. demote_segment_4k(mm, ea);
  667. psize = MMU_PAGE_4K;
  668. }
  669. /* If this PTE is non-cacheable and we have restrictions on
  670. * using non cacheable large pages, then we switch to 4k
  671. */
  672. if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
  673. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  674. if (user_region) {
  675. demote_segment_4k(mm, ea);
  676. psize = MMU_PAGE_4K;
  677. } else if (ea < VMALLOC_END) {
  678. /*
  679. * some driver did a non-cacheable mapping
  680. * in vmalloc space, so switch vmalloc
  681. * to 4k pages
  682. */
  683. printk(KERN_ALERT "Reducing vmalloc segment "
  684. "to 4kB pages because of "
  685. "non-cacheable mapping\n");
  686. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  687. #ifdef CONFIG_SPU_BASE
  688. spu_flush_all_slbs(mm);
  689. #endif
  690. }
  691. }
  692. if (user_region) {
  693. if (psize != get_paca()->context.user_psize) {
  694. get_paca()->context.user_psize =
  695. mm->context.user_psize;
  696. slb_flush_and_rebolt();
  697. }
  698. } else if (get_paca()->vmalloc_sllp !=
  699. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  700. get_paca()->vmalloc_sllp =
  701. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  702. slb_vmalloc_update();
  703. }
  704. #endif /* CONFIG_PPC_64K_PAGES */
  705. #ifdef CONFIG_PPC_HAS_HASH_64K
  706. if (psize == MMU_PAGE_64K)
  707. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  708. else
  709. #endif /* CONFIG_PPC_HAS_HASH_64K */
  710. rc = __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize);
  711. #ifndef CONFIG_PPC_64K_PAGES
  712. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  713. #else
  714. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  715. pte_val(*(ptep + PTRS_PER_PTE)));
  716. #endif
  717. DBG_LOW(" -> rc=%d\n", rc);
  718. return rc;
  719. }
  720. EXPORT_SYMBOL_GPL(hash_page);
  721. void hash_preload(struct mm_struct *mm, unsigned long ea,
  722. unsigned long access, unsigned long trap)
  723. {
  724. unsigned long vsid;
  725. void *pgdir;
  726. pte_t *ptep;
  727. cpumask_t mask;
  728. unsigned long flags;
  729. int local = 0;
  730. int ssize;
  731. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  732. #ifdef CONFIG_PPC_MM_SLICES
  733. /* We only prefault standard pages for now */
  734. if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
  735. return;
  736. #endif
  737. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  738. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  739. /* Get Linux PTE if available */
  740. pgdir = mm->pgd;
  741. if (pgdir == NULL)
  742. return;
  743. ptep = find_linux_pte(pgdir, ea);
  744. if (!ptep)
  745. return;
  746. #ifdef CONFIG_PPC_64K_PAGES
  747. /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
  748. * a 64K kernel), then we don't preload, hash_page() will take
  749. * care of it once we actually try to access the page.
  750. * That way we don't have to duplicate all of the logic for segment
  751. * page size demotion here
  752. */
  753. if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
  754. return;
  755. #endif /* CONFIG_PPC_64K_PAGES */
  756. /* Get VSID */
  757. ssize = user_segment_size(ea);
  758. vsid = get_vsid(mm->context.id, ea, ssize);
  759. /* Hash doesn't like irqs */
  760. local_irq_save(flags);
  761. /* Is that local to this CPU ? */
  762. mask = cpumask_of_cpu(smp_processor_id());
  763. if (cpus_equal(mm->cpu_vm_mask, mask))
  764. local = 1;
  765. /* Hash it in */
  766. #ifdef CONFIG_PPC_HAS_HASH_64K
  767. if (mm->context.user_psize == MMU_PAGE_64K)
  768. __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  769. else
  770. #endif /* CONFIG_PPC_HAS_HASH_64K */
  771. __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize);
  772. local_irq_restore(flags);
  773. }
  774. void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
  775. int local)
  776. {
  777. unsigned long hash, index, shift, hidx, slot;
  778. DBG_LOW("flush_hash_page(va=%016x)\n", va);
  779. pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
  780. hash = hpt_hash(va, shift, ssize);
  781. hidx = __rpte_to_hidx(pte, index);
  782. if (hidx & _PTEIDX_SECONDARY)
  783. hash = ~hash;
  784. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  785. slot += hidx & _PTEIDX_GROUP_IX;
  786. DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
  787. ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
  788. } pte_iterate_hashed_end();
  789. }
  790. void flush_hash_range(unsigned long number, int local)
  791. {
  792. if (ppc_md.flush_hash_range)
  793. ppc_md.flush_hash_range(number, local);
  794. else {
  795. int i;
  796. struct ppc64_tlb_batch *batch =
  797. &__get_cpu_var(ppc64_tlb_batch);
  798. for (i = 0; i < number; i++)
  799. flush_hash_page(batch->vaddr[i], batch->pte[i],
  800. batch->psize, batch->ssize, local);
  801. }
  802. }
  803. /*
  804. * low_hash_fault is called when we the low level hash code failed
  805. * to instert a PTE due to an hypervisor error
  806. */
  807. void low_hash_fault(struct pt_regs *regs, unsigned long address)
  808. {
  809. if (user_mode(regs)) {
  810. siginfo_t info;
  811. info.si_signo = SIGBUS;
  812. info.si_errno = 0;
  813. info.si_code = BUS_ADRERR;
  814. info.si_addr = (void __user *)address;
  815. force_sig_info(SIGBUS, &info, current);
  816. return;
  817. }
  818. bad_page_fault(regs, address, SIGBUS);
  819. }
  820. #ifdef CONFIG_DEBUG_PAGEALLOC
  821. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  822. {
  823. unsigned long hash, hpteg;
  824. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  825. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  826. unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
  827. _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
  828. int ret;
  829. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  830. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  831. ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
  832. mode, HPTE_V_BOLTED,
  833. mmu_linear_psize, mmu_kernel_ssize);
  834. BUG_ON (ret < 0);
  835. spin_lock(&linear_map_hash_lock);
  836. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  837. linear_map_hash_slots[lmi] = ret | 0x80;
  838. spin_unlock(&linear_map_hash_lock);
  839. }
  840. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  841. {
  842. unsigned long hash, hidx, slot;
  843. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  844. unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
  845. hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
  846. spin_lock(&linear_map_hash_lock);
  847. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  848. hidx = linear_map_hash_slots[lmi] & 0x7f;
  849. linear_map_hash_slots[lmi] = 0;
  850. spin_unlock(&linear_map_hash_lock);
  851. if (hidx & _PTEIDX_SECONDARY)
  852. hash = ~hash;
  853. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  854. slot += hidx & _PTEIDX_GROUP_IX;
  855. ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
  856. }
  857. void kernel_map_pages(struct page *page, int numpages, int enable)
  858. {
  859. unsigned long flags, vaddr, lmi;
  860. int i;
  861. local_irq_save(flags);
  862. for (i = 0; i < numpages; i++, page++) {
  863. vaddr = (unsigned long)page_address(page);
  864. lmi = __pa(vaddr) >> PAGE_SHIFT;
  865. if (lmi >= linear_map_hash_count)
  866. continue;
  867. if (enable)
  868. kernel_map_linear_page(vaddr, lmi);
  869. else
  870. kernel_unmap_linear_page(vaddr, lmi);
  871. }
  872. local_irq_restore(flags);
  873. }
  874. #endif /* CONFIG_DEBUG_PAGEALLOC */