1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2001 Todd Inglett, IBM Corporation
6 * pSeries LPAR support.
9 /* Enables debugging of low-level hash table routines - careful! */
11 #define pr_fmt(fmt) "lpar: " fmt
13 #include <linux/kernel.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/console.h>
16 #include <linux/export.h>
17 #include <linux/jump_label.h>
18 #include <linux/delay.h>
19 #include <linux/stop_machine.h>
20 #include <linux/spinlock.h>
21 #include <linux/cpuhotplug.h>
22 #include <linux/workqueue.h>
23 #include <linux/proc_fs.h>
24 #include <linux/pgtable.h>
25 #include <linux/debugfs.h>
27 #include <asm/processor.h>
30 #include <asm/setup.h>
31 #include <asm/mmu_context.h>
32 #include <asm/iommu.h>
34 #include <asm/cputable.h>
35 #include <asm/papr-sysparm.h>
38 #include <asm/trace.h>
39 #include <asm/firmware.h>
40 #include <asm/plpar_wrappers.h>
41 #include <asm/kexec.h>
42 #include <asm/fadump.h>
47 /* Flag bits for H_BULK_REMOVE */
48 #define HBR_REQUEST 0x4000000000000000UL
49 #define HBR_RESPONSE 0x8000000000000000UL
50 #define HBR_END 0xc000000000000000UL
51 #define HBR_AVPN 0x0200000000000000UL
52 #define HBR_ANDCOND 0x0100000000000000UL
56 EXPORT_SYMBOL(plpar_hcall);
57 EXPORT_SYMBOL(plpar_hcall9);
58 EXPORT_SYMBOL(plpar_hcall_norets);
60 #ifdef CONFIG_PPC_64S_HASH_MMU
62 * H_BLOCK_REMOVE supported block size for this page size in segment who's base
63 * page size is that page size.
65 * The first index is the segment base page size, the second one is the actual
68 static int hblkrm_size[MMU_PAGE_COUNT][MMU_PAGE_COUNT] __ro_after_init;
72 * Due to the involved complexity, and that the current hypervisor is only
73 * returning this value or 0, we are limiting the support of the H_BLOCK_REMOVE
74 * buffer size to 8 size block.
76 #define HBLKRM_SUPPORTED_BLOCK_SIZE 8
78 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
79 static u8 dtl_mask = DTL_LOG_PREEMPT;
84 void alloc_dtl_buffers(unsigned long *time_limit)
87 struct paca_struct *pp;
88 struct dtl_entry *dtl;
90 for_each_possible_cpu(cpu) {
94 dtl = kmem_cache_alloc(dtl_cache, GFP_KERNEL);
96 pr_warn("Failed to allocate dispatch trace log for cpu %d\n",
98 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
99 pr_warn("Stolen time statistics will be unreliable\n");
105 pp->dispatch_log = dtl;
106 pp->dispatch_log_end = dtl + N_DISPATCH_LOG;
109 if (time_limit && time_after(jiffies, *time_limit)) {
111 *time_limit = jiffies + HZ;
116 void register_dtl_buffer(int cpu)
119 struct paca_struct *pp;
120 struct dtl_entry *dtl;
121 int hwcpu = get_hard_smp_processor_id(cpu);
124 dtl = pp->dispatch_log;
125 if (dtl && dtl_mask) {
128 lppaca_of(cpu).dtl_idx = 0;
130 /* hypervisor reads buffer length from this field */
131 dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);
132 ret = register_dtl(hwcpu, __pa(dtl));
134 pr_err("WARNING: DTL registration of cpu %d (hw %d) failed with %ld\n",
137 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
141 #ifdef CONFIG_PPC_SPLPAR
143 struct delayed_work work;
147 struct vcpu_dispatch_data {
158 int numa_remote_disp;
163 * This represents the number of cpus in the hypervisor. Since there is no
164 * architected way to discover the number of processors in the host, we
165 * provision for dealing with NR_CPUS. This is currently 2048 by default, and
166 * is sufficient for our purposes. This will need to be tweaked if
167 * CONFIG_NR_CPUS is changed.
169 #define NR_CPUS_H NR_CPUS
171 DEFINE_RWLOCK(dtl_access_lock);
172 static DEFINE_PER_CPU(struct vcpu_dispatch_data, vcpu_disp_data);
173 static DEFINE_PER_CPU(u64, dtl_entry_ridx);
174 static DEFINE_PER_CPU(struct dtl_worker, dtl_workers);
175 static enum cpuhp_state dtl_worker_state;
176 static DEFINE_MUTEX(dtl_enable_mutex);
177 static int vcpudispatch_stats_on __read_mostly;
178 static int vcpudispatch_stats_freq = 50;
179 static __be32 *vcpu_associativity, *pcpu_associativity;
182 static void free_dtl_buffers(unsigned long *time_limit)
184 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
186 struct paca_struct *pp;
188 for_each_possible_cpu(cpu) {
190 if (!pp->dispatch_log)
192 kmem_cache_free(dtl_cache, pp->dispatch_log);
194 pp->dispatch_log = 0;
195 pp->dispatch_log_end = 0;
198 if (time_limit && time_after(jiffies, *time_limit)) {
200 *time_limit = jiffies + HZ;
206 static int init_cpu_associativity(void)
208 vcpu_associativity = kcalloc(num_possible_cpus() / threads_per_core,
209 VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
210 pcpu_associativity = kcalloc(NR_CPUS_H / threads_per_core,
211 VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
213 if (!vcpu_associativity || !pcpu_associativity) {
214 pr_err("error allocating memory for associativity information\n");
221 static void destroy_cpu_associativity(void)
223 kfree(vcpu_associativity);
224 kfree(pcpu_associativity);
225 vcpu_associativity = pcpu_associativity = 0;
228 static __be32 *__get_cpu_associativity(int cpu, __be32 *cpu_assoc, int flag)
233 assoc = &cpu_assoc[(int)(cpu / threads_per_core) * VPHN_ASSOC_BUFSIZE];
235 rc = hcall_vphn(cpu, flag, &assoc[0]);
243 static __be32 *get_pcpu_associativity(int cpu)
245 return __get_cpu_associativity(cpu, pcpu_associativity, VPHN_FLAG_PCPU);
248 static __be32 *get_vcpu_associativity(int cpu)
250 return __get_cpu_associativity(cpu, vcpu_associativity, VPHN_FLAG_VCPU);
253 static int cpu_relative_dispatch_distance(int last_disp_cpu, int cur_disp_cpu)
255 __be32 *last_disp_cpu_assoc, *cur_disp_cpu_assoc;
257 if (last_disp_cpu >= NR_CPUS_H || cur_disp_cpu >= NR_CPUS_H)
260 last_disp_cpu_assoc = get_pcpu_associativity(last_disp_cpu);
261 cur_disp_cpu_assoc = get_pcpu_associativity(cur_disp_cpu);
263 if (!last_disp_cpu_assoc || !cur_disp_cpu_assoc)
266 return cpu_relative_distance(last_disp_cpu_assoc, cur_disp_cpu_assoc);
269 static int cpu_home_node_dispatch_distance(int disp_cpu)
271 __be32 *disp_cpu_assoc, *vcpu_assoc;
272 int vcpu_id = smp_processor_id();
274 if (disp_cpu >= NR_CPUS_H) {
275 pr_debug_ratelimited("vcpu dispatch cpu %d > %d\n",
276 disp_cpu, NR_CPUS_H);
280 disp_cpu_assoc = get_pcpu_associativity(disp_cpu);
281 vcpu_assoc = get_vcpu_associativity(vcpu_id);
283 if (!disp_cpu_assoc || !vcpu_assoc)
286 return cpu_relative_distance(disp_cpu_assoc, vcpu_assoc);
289 static void update_vcpu_disp_stat(int disp_cpu)
291 struct vcpu_dispatch_data *disp;
294 disp = this_cpu_ptr(&vcpu_disp_data);
295 if (disp->last_disp_cpu == -1) {
296 disp->last_disp_cpu = disp_cpu;
302 if (disp->last_disp_cpu == disp_cpu ||
303 (cpu_first_thread_sibling(disp->last_disp_cpu) ==
304 cpu_first_thread_sibling(disp_cpu)))
305 disp->same_cpu_disp++;
307 distance = cpu_relative_dispatch_distance(disp->last_disp_cpu,
310 pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
315 disp->same_chip_disp++;
318 disp->diff_chip_disp++;
321 disp->far_chip_disp++;
324 pr_debug_ratelimited("vcpudispatch_stats: cpu %d (%d -> %d): unexpected relative dispatch distance %d\n",
333 distance = cpu_home_node_dispatch_distance(disp_cpu);
335 pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
340 disp->numa_home_disp++;
343 disp->numa_remote_disp++;
346 disp->numa_far_disp++;
349 pr_debug_ratelimited("vcpudispatch_stats: cpu %d on %d: unexpected numa dispatch distance %d\n",
356 disp->last_disp_cpu = disp_cpu;
359 static void process_dtl_buffer(struct work_struct *work)
361 struct dtl_entry dtle;
362 u64 i = __this_cpu_read(dtl_entry_ridx);
363 struct dtl_entry *dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
364 struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
365 struct lppaca *vpa = local_paca->lppaca_ptr;
366 struct dtl_worker *d = container_of(work, struct dtl_worker, work.work);
368 if (!local_paca->dispatch_log)
371 /* if we have been migrated away, we cancel ourself */
372 if (d->cpu != smp_processor_id()) {
373 pr_debug("vcpudispatch_stats: cpu %d worker migrated -- canceling worker\n",
378 if (i == be64_to_cpu(vpa->dtl_idx))
381 while (i < be64_to_cpu(vpa->dtl_idx)) {
384 if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
385 /* buffer has overflowed */
386 pr_debug_ratelimited("vcpudispatch_stats: cpu %d lost %lld DTL samples\n",
388 be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG - i);
389 i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
390 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
393 update_vcpu_disp_stat(be16_to_cpu(dtle.processor_id));
397 dtl = local_paca->dispatch_log;
400 __this_cpu_write(dtl_entry_ridx, i);
403 schedule_delayed_work_on(d->cpu, to_delayed_work(work),
404 HZ / vcpudispatch_stats_freq);
407 static int dtl_worker_online(unsigned int cpu)
409 struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
411 memset(d, 0, sizeof(*d));
412 INIT_DELAYED_WORK(&d->work, process_dtl_buffer);
415 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
416 per_cpu(dtl_entry_ridx, cpu) = 0;
417 register_dtl_buffer(cpu);
419 per_cpu(dtl_entry_ridx, cpu) = be64_to_cpu(lppaca_of(cpu).dtl_idx);
422 schedule_delayed_work_on(cpu, &d->work, HZ / vcpudispatch_stats_freq);
426 static int dtl_worker_offline(unsigned int cpu)
428 struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
430 cancel_delayed_work_sync(&d->work);
432 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
433 unregister_dtl(get_hard_smp_processor_id(cpu));
439 static void set_global_dtl_mask(u8 mask)
444 for_each_present_cpu(cpu)
445 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
448 static void reset_global_dtl_mask(void)
452 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
453 dtl_mask = DTL_LOG_PREEMPT;
457 for_each_present_cpu(cpu)
458 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
461 static int dtl_worker_enable(unsigned long *time_limit)
465 if (!write_trylock(&dtl_access_lock)) {
470 set_global_dtl_mask(DTL_LOG_ALL);
472 /* Setup dtl buffers and register those */
473 alloc_dtl_buffers(time_limit);
475 state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/dtl:online",
476 dtl_worker_online, dtl_worker_offline);
478 pr_err("vcpudispatch_stats: unable to setup workqueue for DTL processing\n");
479 free_dtl_buffers(time_limit);
480 reset_global_dtl_mask();
481 write_unlock(&dtl_access_lock);
485 dtl_worker_state = state;
491 static void dtl_worker_disable(unsigned long *time_limit)
493 cpuhp_remove_state(dtl_worker_state);
494 free_dtl_buffers(time_limit);
495 reset_global_dtl_mask();
496 write_unlock(&dtl_access_lock);
499 static ssize_t vcpudispatch_stats_write(struct file *file, const char __user *p,
500 size_t count, loff_t *ppos)
502 unsigned long time_limit = jiffies + HZ;
503 struct vcpu_dispatch_data *disp;
510 if (copy_from_user(buf, p, count))
514 rc = kstrtoint(buf, 0, &cmd);
515 if (rc || cmd < 0 || cmd > 1) {
516 pr_err("vcpudispatch_stats: please use 0 to disable or 1 to enable dispatch statistics\n");
517 return rc ? rc : -EINVAL;
520 mutex_lock(&dtl_enable_mutex);
522 if ((cmd == 0 && !vcpudispatch_stats_on) ||
523 (cmd == 1 && vcpudispatch_stats_on))
527 rc = init_cpu_associativity();
531 for_each_possible_cpu(cpu) {
532 disp = per_cpu_ptr(&vcpu_disp_data, cpu);
533 memset(disp, 0, sizeof(*disp));
534 disp->last_disp_cpu = -1;
537 rc = dtl_worker_enable(&time_limit);
539 destroy_cpu_associativity();
543 dtl_worker_disable(&time_limit);
544 destroy_cpu_associativity();
547 vcpudispatch_stats_on = cmd;
550 mutex_unlock(&dtl_enable_mutex);
556 static int vcpudispatch_stats_display(struct seq_file *p, void *v)
559 struct vcpu_dispatch_data *disp;
561 if (!vcpudispatch_stats_on) {
562 seq_puts(p, "off\n");
566 for_each_online_cpu(cpu) {
567 disp = per_cpu_ptr(&vcpu_disp_data, cpu);
568 seq_printf(p, "cpu%d", cpu);
569 seq_put_decimal_ull(p, " ", disp->total_disp);
570 seq_put_decimal_ull(p, " ", disp->same_cpu_disp);
571 seq_put_decimal_ull(p, " ", disp->same_chip_disp);
572 seq_put_decimal_ull(p, " ", disp->diff_chip_disp);
573 seq_put_decimal_ull(p, " ", disp->far_chip_disp);
574 seq_put_decimal_ull(p, " ", disp->numa_home_disp);
575 seq_put_decimal_ull(p, " ", disp->numa_remote_disp);
576 seq_put_decimal_ull(p, " ", disp->numa_far_disp);
583 static int vcpudispatch_stats_open(struct inode *inode, struct file *file)
585 return single_open(file, vcpudispatch_stats_display, NULL);
588 static const struct proc_ops vcpudispatch_stats_proc_ops = {
589 .proc_open = vcpudispatch_stats_open,
590 .proc_read = seq_read,
591 .proc_write = vcpudispatch_stats_write,
592 .proc_lseek = seq_lseek,
593 .proc_release = single_release,
596 static ssize_t vcpudispatch_stats_freq_write(struct file *file,
597 const char __user *p, size_t count, loff_t *ppos)
605 if (copy_from_user(buf, p, count))
609 rc = kstrtoint(buf, 0, &freq);
610 if (rc || freq < 1 || freq > HZ) {
611 pr_err("vcpudispatch_stats_freq: please specify a frequency between 1 and %d\n",
613 return rc ? rc : -EINVAL;
616 vcpudispatch_stats_freq = freq;
621 static int vcpudispatch_stats_freq_display(struct seq_file *p, void *v)
623 seq_printf(p, "%d\n", vcpudispatch_stats_freq);
627 static int vcpudispatch_stats_freq_open(struct inode *inode, struct file *file)
629 return single_open(file, vcpudispatch_stats_freq_display, NULL);
632 static const struct proc_ops vcpudispatch_stats_freq_proc_ops = {
633 .proc_open = vcpudispatch_stats_freq_open,
634 .proc_read = seq_read,
635 .proc_write = vcpudispatch_stats_freq_write,
636 .proc_lseek = seq_lseek,
637 .proc_release = single_release,
640 static int __init vcpudispatch_stats_procfs_init(void)
642 if (!lppaca_shared_proc())
645 if (!proc_create("powerpc/vcpudispatch_stats", 0600, NULL,
646 &vcpudispatch_stats_proc_ops))
647 pr_err("vcpudispatch_stats: error creating procfs file\n");
648 else if (!proc_create("powerpc/vcpudispatch_stats_freq", 0600, NULL,
649 &vcpudispatch_stats_freq_proc_ops))
650 pr_err("vcpudispatch_stats_freq: error creating procfs file\n");
655 machine_device_initcall(pseries, vcpudispatch_stats_procfs_init);
657 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
658 u64 pseries_paravirt_steal_clock(int cpu)
660 struct lppaca *lppaca = &lppaca_of(cpu);
662 return be64_to_cpu(READ_ONCE(lppaca->enqueue_dispatch_tb)) +
663 be64_to_cpu(READ_ONCE(lppaca->ready_enqueue_tb));
667 #endif /* CONFIG_PPC_SPLPAR */
669 void vpa_init(int cpu)
671 int hwcpu = get_hard_smp_processor_id(cpu);
676 * The spec says it "may be problematic" if CPU x registers the VPA of
677 * CPU y. We should never do that, but wail if we ever do.
679 WARN_ON(cpu != smp_processor_id());
681 if (cpu_has_feature(CPU_FTR_ALTIVEC))
682 lppaca_of(cpu).vmxregs_in_use = 1;
684 if (cpu_has_feature(CPU_FTR_ARCH_207S))
685 lppaca_of(cpu).ebb_regs_in_use = 1;
687 addr = __pa(&lppaca_of(cpu));
688 ret = register_vpa(hwcpu, addr);
691 pr_err("WARNING: VPA registration for cpu %d (hw %d) of area "
692 "%lx failed with %ld\n", cpu, hwcpu, addr, ret);
696 #ifdef CONFIG_PPC_64S_HASH_MMU
698 * PAPR says this feature is SLB-Buffer but firmware never
699 * reports that. All SPLPAR support SLB shadow buffer.
701 if (!radix_enabled() && firmware_has_feature(FW_FEATURE_SPLPAR)) {
702 addr = __pa(paca_ptrs[cpu]->slb_shadow_ptr);
703 ret = register_slb_shadow(hwcpu, addr);
705 pr_err("WARNING: SLB shadow buffer registration for "
706 "cpu %d (hw %d) of area %lx failed with %ld\n",
707 cpu, hwcpu, addr, ret);
709 #endif /* CONFIG_PPC_64S_HASH_MMU */
712 * Register dispatch trace log, if one has been allocated.
714 register_dtl_buffer(cpu);
717 #ifdef CONFIG_PPC_BOOK3S_64
719 static int __init pseries_lpar_register_process_table(unsigned long base,
720 unsigned long page_size, unsigned long table_size)
723 unsigned long flags = 0;
726 flags |= PROC_TABLE_NEW;
727 if (radix_enabled()) {
728 flags |= PROC_TABLE_RADIX;
729 if (mmu_has_feature(MMU_FTR_GTSE))
730 flags |= PROC_TABLE_GTSE;
732 flags |= PROC_TABLE_HPT_SLB;
734 rc = plpar_hcall_norets(H_REGISTER_PROC_TBL, flags, base,
735 page_size, table_size);
736 if (!H_IS_LONG_BUSY(rc))
738 mdelay(get_longbusy_msecs(rc));
740 if (rc != H_SUCCESS) {
741 pr_err("Failed to register process table (rc=%ld)\n", rc);
747 #ifdef CONFIG_PPC_64S_HASH_MMU
749 static long pSeries_lpar_hpte_insert(unsigned long hpte_group,
750 unsigned long vpn, unsigned long pa,
751 unsigned long rflags, unsigned long vflags,
752 int psize, int apsize, int ssize)
754 unsigned long lpar_rc;
757 unsigned long hpte_v, hpte_r;
759 if (!(vflags & HPTE_V_BOLTED))
760 pr_devel("hpte_insert(group=%lx, vpn=%016lx, "
761 "pa=%016lx, rflags=%lx, vflags=%lx, psize=%d)\n",
762 hpte_group, vpn, pa, rflags, vflags, psize);
764 hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
765 hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
767 if (!(vflags & HPTE_V_BOLTED))
768 pr_devel(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
770 /* Now fill in the actual HPTE */
771 /* Set CEC cookie to 0 */
773 /* I-cache Invalidate = 0 */
774 /* I-cache synchronize = 0 */
778 if (firmware_has_feature(FW_FEATURE_XCMO) && !(hpte_r & HPTE_R_N))
779 flags |= H_COALESCE_CAND;
781 lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot);
782 if (unlikely(lpar_rc == H_PTEG_FULL)) {
783 pr_devel("Hash table group is full\n");
788 * Since we try and ioremap PHBs we don't own, the pte insert
789 * will fail. However we must catch the failure in hash_page
790 * or we will loop forever, so return -2 in this case.
792 if (unlikely(lpar_rc != H_SUCCESS)) {
793 pr_err("Failed hash pte insert with error %ld\n", lpar_rc);
796 if (!(vflags & HPTE_V_BOLTED))
797 pr_devel(" -> slot: %lu\n", slot & 7);
799 /* Because of iSeries, we have to pass down the secondary
800 * bucket bit here as well
802 return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);
805 static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
807 static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
809 unsigned long slot_offset;
810 unsigned long lpar_rc;
812 unsigned long dummy1, dummy2;
814 /* pick a random slot to start at */
815 slot_offset = mftb() & 0x7;
817 for (i = 0; i < HPTES_PER_GROUP; i++) {
819 /* don't remove a bolted entry */
820 lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
821 HPTE_V_BOLTED, &dummy1, &dummy2);
822 if (lpar_rc == H_SUCCESS)
826 * The test for adjunct partition is performed before the
827 * ANDCOND test. H_RESOURCE may be returned, so we need to
828 * check for that as well.
830 BUG_ON(lpar_rc != H_NOT_FOUND && lpar_rc != H_RESOURCE);
839 /* Called during kexec sequence with MMU off */
840 static notrace void manual_hpte_clear_all(void)
842 unsigned long size_bytes = 1UL << ppc64_pft_size;
843 unsigned long hpte_count = size_bytes >> 4;
851 /* Read in batches of 4,
852 * invalidate only valid entries not in the VRMA
853 * hpte_count will be a multiple of 4
855 for (i = 0; i < hpte_count; i += 4) {
856 lpar_rc = plpar_pte_read_4_raw(0, i, (void *)ptes);
857 if (lpar_rc != H_SUCCESS) {
858 pr_info("Failed to read hash page table at %ld err %ld\n",
862 for (j = 0; j < 4; j++){
863 if ((ptes[j].pteh & HPTE_V_VRMA_MASK) ==
866 if (ptes[j].pteh & HPTE_V_VALID)
867 plpar_pte_remove_raw(0, i + j, 0,
868 &(ptes[j].pteh), &(ptes[j].ptel));
873 /* Called during kexec sequence with MMU off */
874 static notrace int hcall_hpte_clear_all(void)
879 rc = plpar_hcall_norets(H_CLEAR_HPT);
880 } while (rc == H_CONTINUE);
885 /* Called during kexec sequence with MMU off */
886 static notrace void pseries_hpte_clear_all(void)
890 rc = hcall_hpte_clear_all();
892 manual_hpte_clear_all();
894 #ifdef __LITTLE_ENDIAN__
896 * Reset exceptions to big endian.
898 * FIXME this is a hack for kexec, we need to reset the exception
899 * endian before starting the new kernel and this is a convenient place
902 * This is also called on boot when a fadump happens. In that case we
903 * must not change the exception endian mode.
905 if (firmware_has_feature(FW_FEATURE_SET_MODE) && !is_fadump_active())
906 pseries_big_endian_exceptions();
911 * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
912 * the low 3 bits of flags happen to line up. So no transform is needed.
913 * We can probably optimize here and assume the high bits of newpp are
914 * already zero. For now I am paranoid.
916 static long pSeries_lpar_hpte_updatepp(unsigned long slot,
919 int psize, int apsize,
920 int ssize, unsigned long inv_flags)
922 unsigned long lpar_rc;
924 unsigned long want_v;
926 want_v = hpte_encode_avpn(vpn, psize, ssize);
928 flags = (newpp & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO)) | H_AVPN;
929 flags |= (newpp & HPTE_R_KEY_HI) >> 48;
930 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
931 /* Move pp0 into bit 8 (IBM 55) */
932 flags |= (newpp & HPTE_R_PP0) >> 55;
934 pr_devel(" update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...",
935 want_v, slot, flags, psize);
937 lpar_rc = plpar_pte_protect(flags, slot, want_v);
939 if (lpar_rc == H_NOT_FOUND) {
940 pr_devel("not found !\n");
946 BUG_ON(lpar_rc != H_SUCCESS);
951 static long __pSeries_lpar_hpte_find(unsigned long want_v, unsigned long hpte_group)
960 for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
962 lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
963 if (lpar_rc != H_SUCCESS) {
964 pr_info("Failed to read hash page table at %ld err %ld\n",
965 hpte_group, lpar_rc);
969 for (j = 0; j < 4; j++) {
970 if (HPTE_V_COMPARE(ptes[j].pteh, want_v) &&
971 (ptes[j].pteh & HPTE_V_VALID))
979 static long pSeries_lpar_hpte_find(unsigned long vpn, int psize, int ssize)
983 unsigned long want_v;
984 unsigned long hpte_group;
986 hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
987 want_v = hpte_encode_avpn(vpn, psize, ssize);
990 * We try to keep bolted entries always in primary hash
991 * But in some case we can find them in secondary too.
993 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
994 slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
996 /* Try in secondary */
997 hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
998 slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
1002 return hpte_group + slot;
1005 static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
1007 int psize, int ssize)
1010 unsigned long lpar_rc, slot, vsid, flags;
1012 vsid = get_kernel_vsid(ea, ssize);
1013 vpn = hpt_vpn(ea, vsid, ssize);
1015 slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
1018 flags = newpp & (HPTE_R_PP | HPTE_R_N);
1019 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
1020 /* Move pp0 into bit 8 (IBM 55) */
1021 flags |= (newpp & HPTE_R_PP0) >> 55;
1023 flags |= ((newpp & HPTE_R_KEY_HI) >> 48) | (newpp & HPTE_R_KEY_LO);
1025 lpar_rc = plpar_pte_protect(flags, slot, 0);
1027 BUG_ON(lpar_rc != H_SUCCESS);
1030 static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn,
1031 int psize, int apsize,
1032 int ssize, int local)
1034 unsigned long want_v;
1035 unsigned long lpar_rc;
1036 unsigned long dummy1, dummy2;
1038 pr_devel(" inval : slot=%lx, vpn=%016lx, psize: %d, local: %d\n",
1039 slot, vpn, psize, local);
1041 want_v = hpte_encode_avpn(vpn, psize, ssize);
1042 lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2);
1043 if (lpar_rc == H_NOT_FOUND)
1046 BUG_ON(lpar_rc != H_SUCCESS);
1051 * As defined in the PAPR's section 14.5.4.1.8
1052 * The control mask doesn't include the returned reference and change bit from
1053 * the processed PTE.
1055 #define HBLKR_AVPN 0x0100000000000000UL
1056 #define HBLKR_CTRL_MASK 0xf800000000000000UL
1057 #define HBLKR_CTRL_SUCCESS 0x8000000000000000UL
1058 #define HBLKR_CTRL_ERRNOTFOUND 0x8800000000000000UL
1059 #define HBLKR_CTRL_ERRBUSY 0xa000000000000000UL
1062 * Returned true if we are supporting this block size for the specified segment
1063 * base page size and actual page size.
1065 * Currently, we only support 8 size block.
1067 static inline bool is_supported_hlbkrm(int bpsize, int psize)
1069 return (hblkrm_size[bpsize][psize] == HBLKRM_SUPPORTED_BLOCK_SIZE);
1073 * H_BLOCK_REMOVE caller.
1074 * @idx should point to the latest @param entry set with a PTEX.
1075 * If PTE cannot be processed because another CPUs has already locked that
1076 * group, those entries are put back in @param starting at index 1.
1077 * If entries has to be retried and @retry_busy is set to true, these entries
1078 * are retried until success. If @retry_busy is set to false, the returned
1079 * is the number of entries yet to process.
1081 static unsigned long call_block_remove(unsigned long idx, unsigned long *param,
1084 unsigned long i, rc, new_idx;
1085 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1088 pr_warn("Unexpected empty call to H_BLOCK_REMOVE");
1093 if (idx > PLPAR_HCALL9_BUFSIZE) {
1094 pr_err("Too many PTEs (%lu) for H_BLOCK_REMOVE", idx);
1095 idx = PLPAR_HCALL9_BUFSIZE;
1096 } else if (idx < PLPAR_HCALL9_BUFSIZE)
1097 param[idx] = HBR_END;
1099 rc = plpar_hcall9(H_BLOCK_REMOVE, retbuf,
1101 param[1], param[2], param[3], param[4], /* TS0-7 */
1102 param[5], param[6], param[7], param[8]);
1103 if (rc == H_SUCCESS)
1106 BUG_ON(rc != H_PARTIAL);
1108 /* Check that the unprocessed entries were 'not found' or 'busy' */
1109 for (i = 0; i < idx-1; i++) {
1110 unsigned long ctrl = retbuf[i] & HBLKR_CTRL_MASK;
1112 if (ctrl == HBLKR_CTRL_ERRBUSY) {
1113 param[++new_idx] = param[i+1];
1117 BUG_ON(ctrl != HBLKR_CTRL_SUCCESS
1118 && ctrl != HBLKR_CTRL_ERRNOTFOUND);
1122 * If there were entries found busy, retry these entries if requested,
1123 * of if all the entries have to be retried.
1125 if (new_idx && (retry_busy || new_idx == (PLPAR_HCALL9_BUFSIZE-1))) {
1133 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1135 * Limit iterations holding pSeries_lpar_tlbie_lock to 3. We also need
1136 * to make sure that we avoid bouncing the hypervisor tlbie lock.
1138 #define PPC64_HUGE_HPTE_BATCH 12
1140 static void hugepage_block_invalidate(unsigned long *slot, unsigned long *vpn,
1141 int count, int psize, int ssize)
1143 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1144 unsigned long shift, current_vpgb, vpgb;
1147 shift = mmu_psize_defs[psize].shift;
1149 for (i = 0; i < count; i++) {
1151 * Shifting 3 bits more on the right to get a
1152 * 8 pages aligned virtual addresse.
1154 vpgb = (vpn[i] >> (shift - VPN_SHIFT + 3));
1155 if (!pix || vpgb != current_vpgb) {
1157 * Need to start a new 8 pages block, flush
1158 * the current one if needed.
1161 (void)call_block_remove(pix, param, true);
1162 current_vpgb = vpgb;
1163 param[0] = hpte_encode_avpn(vpn[i], psize, ssize);
1167 param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot[i];
1168 if (pix == PLPAR_HCALL9_BUFSIZE) {
1169 pix = call_block_remove(pix, param, false);
1171 * pix = 0 means that all the entries were
1172 * removed, we can start a new block.
1173 * Otherwise, this means that there are entries
1174 * to retry, and pix points to latest one, so
1175 * we should increment it and try to continue
1183 (void)call_block_remove(pix, param, true);
1186 static void hugepage_bulk_invalidate(unsigned long *slot, unsigned long *vpn,
1187 int count, int psize, int ssize)
1189 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1190 int i = 0, pix = 0, rc;
1192 for (i = 0; i < count; i++) {
1194 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1195 pSeries_lpar_hpte_invalidate(slot[i], vpn[i], psize, 0,
1198 param[pix] = HBR_REQUEST | HBR_AVPN | slot[i];
1199 param[pix+1] = hpte_encode_avpn(vpn[i], psize, ssize);
1202 rc = plpar_hcall9(H_BULK_REMOVE, param,
1203 param[0], param[1], param[2],
1204 param[3], param[4], param[5],
1205 param[6], param[7]);
1206 BUG_ON(rc != H_SUCCESS);
1212 param[pix] = HBR_END;
1213 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1214 param[2], param[3], param[4], param[5],
1215 param[6], param[7]);
1216 BUG_ON(rc != H_SUCCESS);
1220 static inline void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
1222 int count, int psize,
1225 unsigned long flags = 0;
1226 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1229 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1231 /* Assuming THP size is 16M */
1232 if (is_supported_hlbkrm(psize, MMU_PAGE_16M))
1233 hugepage_block_invalidate(slot, vpn, count, psize, ssize);
1235 hugepage_bulk_invalidate(slot, vpn, count, psize, ssize);
1238 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1241 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1243 unsigned char *hpte_slot_array,
1244 int psize, int ssize, int local)
1247 unsigned long s_addr = addr;
1248 unsigned int max_hpte_count, valid;
1249 unsigned long vpn_array[PPC64_HUGE_HPTE_BATCH];
1250 unsigned long slot_array[PPC64_HUGE_HPTE_BATCH];
1251 unsigned long shift, hidx, vpn = 0, hash, slot;
1253 shift = mmu_psize_defs[psize].shift;
1254 max_hpte_count = 1U << (PMD_SHIFT - shift);
1256 for (i = 0; i < max_hpte_count; i++) {
1257 valid = hpte_valid(hpte_slot_array, i);
1260 hidx = hpte_hash_index(hpte_slot_array, i);
1263 addr = s_addr + (i * (1ul << shift));
1264 vpn = hpt_vpn(addr, vsid, ssize);
1265 hash = hpt_hash(vpn, shift, ssize);
1266 if (hidx & _PTEIDX_SECONDARY)
1269 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1270 slot += hidx & _PTEIDX_GROUP_IX;
1272 slot_array[index] = slot;
1273 vpn_array[index] = vpn;
1274 if (index == PPC64_HUGE_HPTE_BATCH - 1) {
1276 * Now do a bluk invalidate
1278 __pSeries_lpar_hugepage_invalidate(slot_array,
1280 PPC64_HUGE_HPTE_BATCH,
1287 __pSeries_lpar_hugepage_invalidate(slot_array, vpn_array,
1288 index, psize, ssize);
1291 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1293 unsigned char *hpte_slot_array,
1294 int psize, int ssize, int local)
1296 WARN(1, "%s called without THP support\n", __func__);
1300 static int pSeries_lpar_hpte_removebolted(unsigned long ea,
1301 int psize, int ssize)
1304 unsigned long slot, vsid;
1306 vsid = get_kernel_vsid(ea, ssize);
1307 vpn = hpt_vpn(ea, vsid, ssize);
1309 slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
1314 * lpar doesn't use the passed actual page size
1316 pSeries_lpar_hpte_invalidate(slot, vpn, psize, 0, ssize, 0);
1321 static inline unsigned long compute_slot(real_pte_t pte,
1323 unsigned long index,
1324 unsigned long shift,
1327 unsigned long slot, hash, hidx;
1329 hash = hpt_hash(vpn, shift, ssize);
1330 hidx = __rpte_to_hidx(pte, index);
1331 if (hidx & _PTEIDX_SECONDARY)
1333 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1334 slot += hidx & _PTEIDX_GROUP_IX;
1339 * The hcall H_BLOCK_REMOVE implies that the virtual pages to processed are
1340 * "all within the same naturally aligned 8 page virtual address block".
1342 static void do_block_remove(unsigned long number, struct ppc64_tlb_batch *batch,
1343 unsigned long *param)
1346 unsigned long i, pix = 0;
1347 unsigned long index, shift, slot, current_vpgb, vpgb;
1351 psize = batch->psize;
1352 ssize = batch->ssize;
1354 for (i = 0; i < number; i++) {
1355 vpn = batch->vpn[i];
1356 pte = batch->pte[i];
1357 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1359 * Shifting 3 bits more on the right to get a
1360 * 8 pages aligned virtual addresse.
1362 vpgb = (vpn >> (shift - VPN_SHIFT + 3));
1363 if (!pix || vpgb != current_vpgb) {
1365 * Need to start a new 8 pages block, flush
1366 * the current one if needed.
1369 (void)call_block_remove(pix, param,
1371 current_vpgb = vpgb;
1372 param[0] = hpte_encode_avpn(vpn, psize,
1377 slot = compute_slot(pte, vpn, index, shift, ssize);
1378 param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot;
1380 if (pix == PLPAR_HCALL9_BUFSIZE) {
1381 pix = call_block_remove(pix, param, false);
1383 * pix = 0 means that all the entries were
1384 * removed, we can start a new block.
1385 * Otherwise, this means that there are entries
1386 * to retry, and pix points to latest one, so
1387 * we should increment it and try to continue
1393 } pte_iterate_hashed_end();
1397 (void)call_block_remove(pix, param, true);
1401 * TLB Block Invalidate Characteristics
1403 * These characteristics define the size of the block the hcall H_BLOCK_REMOVE
1404 * is able to process for each couple segment base page size, actual page size.
1406 * The ibm,get-system-parameter properties is returning a buffer with the
1409 * [ 2 bytes size of the RTAS buffer (excluding these 2 bytes) ]
1411 * TLB Block Invalidate Specifiers:
1412 * [ 1 byte LOG base 2 of the TLB invalidate block size being specified ]
1413 * [ 1 byte Number of page sizes (N) that are supported for the specified
1414 * TLB invalidate block size ]
1415 * [ 1 byte Encoded segment base page size and actual page size
1416 * MSB=0 means 4k segment base page size and actual page size
1417 * MSB=1 the penc value in mmu_psize_def ]
1420 * Next TLB Block Invalidate Specifiers...
1424 static inline void set_hblkrm_bloc_size(int bpsize, int psize,
1425 unsigned int block_size)
1427 if (block_size > hblkrm_size[bpsize][psize])
1428 hblkrm_size[bpsize][psize] = block_size;
1432 * Decode the Encoded segment base page size and actual page size.
1434 * - bit 7 is the L bit
1435 * - bits 0-5 are the penc value
1436 * If the L bit is 0, this means 4K segment base page size and actual page size
1437 * otherwise the penc value should be read.
1439 #define HBLKRM_L_MASK 0x80
1440 #define HBLKRM_PENC_MASK 0x3f
1441 static inline void __init check_lp_set_hblkrm(unsigned int lp,
1442 unsigned int block_size)
1444 unsigned int bpsize, psize;
1446 /* First, check the L bit, if not set, this means 4K */
1447 if ((lp & HBLKRM_L_MASK) == 0) {
1448 set_hblkrm_bloc_size(MMU_PAGE_4K, MMU_PAGE_4K, block_size);
1452 lp &= HBLKRM_PENC_MASK;
1453 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++) {
1454 struct mmu_psize_def *def = &mmu_psize_defs[bpsize];
1456 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1457 if (def->penc[psize] == lp) {
1458 set_hblkrm_bloc_size(bpsize, psize, block_size);
1466 * The size of the TLB Block Invalidate Characteristics is variable. But at the
1467 * maximum it will be the number of possible page sizes *2 + 10 bytes.
1468 * Currently MMU_PAGE_COUNT is 16, which means 42 bytes. Use a cache line size
1469 * (128 bytes) for the buffer to get plenty of space.
1471 #define SPLPAR_TLB_BIC_MAXLENGTH 128
1473 void __init pseries_lpar_read_hblkrm_characteristics(void)
1475 static struct papr_sysparm_buf buf __initdata;
1476 int len, idx, bpsize;
1478 if (!firmware_has_feature(FW_FEATURE_BLOCK_REMOVE))
1481 if (papr_sysparm_get(PAPR_SYSPARM_TLB_BLOCK_INVALIDATE_ATTRS, &buf))
1484 len = be16_to_cpu(buf.len);
1485 if (len > SPLPAR_TLB_BIC_MAXLENGTH) {
1486 pr_warn("%s too large returned buffer %d", __func__, len);
1492 u8 block_shift = buf.val[idx++];
1494 unsigned int npsize;
1499 block_size = 1 << block_shift;
1501 for (npsize = buf.val[idx++];
1502 npsize > 0 && idx < len; npsize--)
1503 check_lp_set_hblkrm((unsigned int)buf.val[idx++],
1507 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
1508 for (idx = 0; idx < MMU_PAGE_COUNT; idx++)
1509 if (hblkrm_size[bpsize][idx])
1510 pr_info("H_BLOCK_REMOVE supports base psize:%d psize:%d block size:%d",
1511 bpsize, idx, hblkrm_size[bpsize][idx]);
1515 * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
1518 static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
1521 unsigned long i, pix, rc;
1522 unsigned long flags = 0;
1523 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
1524 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1525 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1526 unsigned long index, shift, slot;
1531 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1533 if (is_supported_hlbkrm(batch->psize, batch->psize)) {
1534 do_block_remove(number, batch, param);
1538 psize = batch->psize;
1539 ssize = batch->ssize;
1541 for (i = 0; i < number; i++) {
1542 vpn = batch->vpn[i];
1543 pte = batch->pte[i];
1544 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1545 slot = compute_slot(pte, vpn, index, shift, ssize);
1546 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1548 * lpar doesn't use the passed actual page size
1550 pSeries_lpar_hpte_invalidate(slot, vpn, psize,
1553 param[pix] = HBR_REQUEST | HBR_AVPN | slot;
1554 param[pix+1] = hpte_encode_avpn(vpn, psize,
1558 rc = plpar_hcall9(H_BULK_REMOVE, param,
1559 param[0], param[1], param[2],
1560 param[3], param[4], param[5],
1561 param[6], param[7]);
1562 BUG_ON(rc != H_SUCCESS);
1566 } pte_iterate_hashed_end();
1569 param[pix] = HBR_END;
1570 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1571 param[2], param[3], param[4], param[5],
1572 param[6], param[7]);
1573 BUG_ON(rc != H_SUCCESS);
1578 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1581 static int __init disable_bulk_remove(char *str)
1583 if (strcmp(str, "off") == 0 &&
1584 firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1585 pr_info("Disabling BULK_REMOVE firmware feature");
1586 powerpc_firmware_features &= ~FW_FEATURE_BULK_REMOVE;
1591 __setup("bulk_remove=", disable_bulk_remove);
1593 #define HPT_RESIZE_TIMEOUT 10000 /* ms */
1595 struct hpt_resize_state {
1596 unsigned long shift;
1600 static int pseries_lpar_resize_hpt_commit(void *data)
1602 struct hpt_resize_state *state = data;
1604 state->commit_rc = plpar_resize_hpt_commit(0, state->shift);
1605 if (state->commit_rc != H_SUCCESS)
1608 /* Hypervisor has transitioned the HTAB, update our globals */
1609 ppc64_pft_size = state->shift;
1610 htab_size_bytes = 1UL << ppc64_pft_size;
1611 htab_hash_mask = (htab_size_bytes >> 7) - 1;
1617 * Must be called in process context. The caller must hold the
1620 static int pseries_lpar_resize_hpt(unsigned long shift)
1622 struct hpt_resize_state state = {
1624 .commit_rc = H_FUNCTION,
1626 unsigned int delay, total_delay = 0;
1632 if (!firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1635 pr_info("Attempting to resize HPT to shift %lu\n", shift);
1639 rc = plpar_resize_hpt_prepare(0, shift);
1640 while (H_IS_LONG_BUSY(rc)) {
1641 delay = get_longbusy_msecs(rc);
1642 total_delay += delay;
1643 if (total_delay > HPT_RESIZE_TIMEOUT) {
1644 /* prepare with shift==0 cancels an in-progress resize */
1645 rc = plpar_resize_hpt_prepare(0, 0);
1646 if (rc != H_SUCCESS)
1647 pr_warn("Unexpected error %d cancelling timed out HPT resize\n",
1652 rc = plpar_resize_hpt_prepare(0, shift);
1661 pr_warn("Invalid argument from H_RESIZE_HPT_PREPARE\n");
1664 pr_warn("Operation not permitted from H_RESIZE_HPT_PREPARE\n");
1667 pr_warn("Unexpected error %d from H_RESIZE_HPT_PREPARE\n", rc);
1673 rc = stop_machine_cpuslocked(pseries_lpar_resize_hpt_commit,
1679 switch (state.commit_rc) {
1684 pr_warn("Unexpected error %d from H_RESIZE_HPT_COMMIT\n",
1690 pr_info("HPT resize to shift %lu complete (%lld ms / %lld ms)\n",
1691 shift, (long long) ktime_ms_delta(t1, t0),
1692 (long long) ktime_ms_delta(t2, t1));
1697 void __init hpte_init_pseries(void)
1699 mmu_hash_ops.hpte_invalidate = pSeries_lpar_hpte_invalidate;
1700 mmu_hash_ops.hpte_updatepp = pSeries_lpar_hpte_updatepp;
1701 mmu_hash_ops.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
1702 mmu_hash_ops.hpte_insert = pSeries_lpar_hpte_insert;
1703 mmu_hash_ops.hpte_remove = pSeries_lpar_hpte_remove;
1704 mmu_hash_ops.hpte_removebolted = pSeries_lpar_hpte_removebolted;
1705 mmu_hash_ops.flush_hash_range = pSeries_lpar_flush_hash_range;
1706 mmu_hash_ops.hpte_clear_all = pseries_hpte_clear_all;
1707 mmu_hash_ops.hugepage_invalidate = pSeries_lpar_hugepage_invalidate;
1709 if (firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1710 mmu_hash_ops.resize_hpt = pseries_lpar_resize_hpt;
1713 * On POWER9, we need to do a H_REGISTER_PROC_TBL hcall
1714 * to inform the hypervisor that we wish to use the HPT.
1716 if (cpu_has_feature(CPU_FTR_ARCH_300))
1717 pseries_lpar_register_process_table(0, 0, 0);
1719 #endif /* CONFIG_PPC_64S_HASH_MMU */
1721 #ifdef CONFIG_PPC_RADIX_MMU
1722 void __init radix_init_pseries(void)
1724 pr_info("Using radix MMU under hypervisor\n");
1726 pseries_lpar_register_process_table(__pa(process_tb),
1727 0, PRTB_SIZE_SHIFT - 12);
1731 #ifdef CONFIG_PPC_SMLPAR
1732 #define CMO_FREE_HINT_DEFAULT 1
1733 static int cmo_free_hint_flag = CMO_FREE_HINT_DEFAULT;
1735 static int __init cmo_free_hint(char *str)
1738 parm = strstrip(str);
1740 if (strcasecmp(parm, "no") == 0 || strcasecmp(parm, "off") == 0) {
1741 pr_info("%s: CMO free page hinting is not active.\n", __func__);
1742 cmo_free_hint_flag = 0;
1746 cmo_free_hint_flag = 1;
1747 pr_info("%s: CMO free page hinting is active.\n", __func__);
1749 if (strcasecmp(parm, "yes") == 0 || strcasecmp(parm, "on") == 0)
1755 __setup("cmo_free_hint=", cmo_free_hint);
1757 static void pSeries_set_page_state(struct page *page, int order,
1758 unsigned long state)
1761 unsigned long cmo_page_sz, addr;
1763 cmo_page_sz = cmo_get_page_size();
1764 addr = __pa((unsigned long)page_address(page));
1766 for (i = 0; i < (1 << order); i++, addr += PAGE_SIZE) {
1767 for (j = 0; j < PAGE_SIZE; j += cmo_page_sz)
1768 plpar_hcall_norets(H_PAGE_INIT, state, addr + j, 0);
1772 void arch_free_page(struct page *page, int order)
1774 if (radix_enabled())
1776 if (!cmo_free_hint_flag || !firmware_has_feature(FW_FEATURE_CMO))
1779 pSeries_set_page_state(page, order, H_PAGE_SET_UNUSED);
1781 EXPORT_SYMBOL(arch_free_page);
1783 #endif /* CONFIG_PPC_SMLPAR */
1784 #endif /* CONFIG_PPC_BOOK3S_64 */
1786 #ifdef CONFIG_TRACEPOINTS
1787 #ifdef CONFIG_JUMP_LABEL
1788 struct static_key hcall_tracepoint_key = STATIC_KEY_INIT;
1790 int hcall_tracepoint_regfunc(void)
1792 static_key_slow_inc(&hcall_tracepoint_key);
1796 void hcall_tracepoint_unregfunc(void)
1798 static_key_slow_dec(&hcall_tracepoint_key);
1802 * We optimise our hcall path by placing hcall_tracepoint_refcount
1803 * directly in the TOC so we can check if the hcall tracepoints are
1804 * enabled via a single load.
1807 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
1808 extern long hcall_tracepoint_refcount;
1810 int hcall_tracepoint_regfunc(void)
1812 hcall_tracepoint_refcount++;
1816 void hcall_tracepoint_unregfunc(void)
1818 hcall_tracepoint_refcount--;
1823 * Keep track of hcall tracing depth and prevent recursion. Warn if any is
1824 * detected because it may indicate a problem. This will not catch all
1825 * problems with tracing code making hcalls, because the tracing might have
1826 * been invoked from a non-hcall, so the first hcall could recurse into it
1827 * without warning here, but this better than nothing.
1829 * Hcalls with specific problems being traced should use the _notrace
1830 * plpar_hcall variants.
1832 static DEFINE_PER_CPU(unsigned int, hcall_trace_depth);
1835 notrace void __trace_hcall_entry(unsigned long opcode, unsigned long *args)
1837 unsigned long flags;
1838 unsigned int *depth;
1840 local_irq_save(flags);
1842 depth = this_cpu_ptr(&hcall_trace_depth);
1844 if (WARN_ON_ONCE(*depth))
1849 trace_hcall_entry(opcode, args);
1853 local_irq_restore(flags);
1856 notrace void __trace_hcall_exit(long opcode, long retval, unsigned long *retbuf)
1858 unsigned long flags;
1859 unsigned int *depth;
1861 local_irq_save(flags);
1863 depth = this_cpu_ptr(&hcall_trace_depth);
1865 if (*depth) /* Don't warn again on the way out */
1869 trace_hcall_exit(opcode, retval, retbuf);
1874 local_irq_restore(flags);
1880 * H_GET_MPP hcall returns info in 7 parms
1882 int h_get_mpp(struct hvcall_mpp_data *mpp_data)
1885 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1887 rc = plpar_hcall9(H_GET_MPP, retbuf);
1889 mpp_data->entitled_mem = retbuf[0];
1890 mpp_data->mapped_mem = retbuf[1];
1892 mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
1893 mpp_data->pool_num = retbuf[2] & 0xffff;
1895 mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
1896 mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
1897 mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffffUL;
1899 mpp_data->pool_size = retbuf[4];
1900 mpp_data->loan_request = retbuf[5];
1901 mpp_data->backing_mem = retbuf[6];
1905 EXPORT_SYMBOL(h_get_mpp);
1907 int h_get_mpp_x(struct hvcall_mpp_x_data *mpp_x_data)
1910 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };
1912 rc = plpar_hcall9(H_GET_MPP_X, retbuf);
1914 mpp_x_data->coalesced_bytes = retbuf[0];
1915 mpp_x_data->pool_coalesced_bytes = retbuf[1];
1916 mpp_x_data->pool_purr_cycles = retbuf[2];
1917 mpp_x_data->pool_spurr_cycles = retbuf[3];
1922 #ifdef CONFIG_PPC_64S_HASH_MMU
1923 static unsigned long __init vsid_unscramble(unsigned long vsid, int ssize)
1925 unsigned long protovsid;
1926 unsigned long va_bits = VA_BITS;
1927 unsigned long modinv, vsid_modulus;
1928 unsigned long max_mod_inv, tmp_modinv;
1930 if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
1933 if (ssize == MMU_SEGSIZE_256M) {
1934 modinv = VSID_MULINV_256M;
1935 vsid_modulus = ((1UL << (va_bits - SID_SHIFT)) - 1);
1937 modinv = VSID_MULINV_1T;
1938 vsid_modulus = ((1UL << (va_bits - SID_SHIFT_1T)) - 1);
1942 * vsid outside our range.
1944 if (vsid >= vsid_modulus)
1948 * If modinv is the modular multiplicate inverse of (x % vsid_modulus)
1949 * and vsid = (protovsid * x) % vsid_modulus, then we say:
1950 * protovsid = (vsid * modinv) % vsid_modulus
1953 /* Check if (vsid * modinv) overflow (63 bits) */
1954 max_mod_inv = 0x7fffffffffffffffull / vsid;
1955 if (modinv < max_mod_inv)
1956 return (vsid * modinv) % vsid_modulus;
1958 tmp_modinv = modinv/max_mod_inv;
1959 modinv %= max_mod_inv;
1961 protovsid = (((vsid * max_mod_inv) % vsid_modulus) * tmp_modinv) % vsid_modulus;
1962 protovsid = (protovsid + vsid * modinv) % vsid_modulus;
1967 static int __init reserve_vrma_context_id(void)
1969 unsigned long protovsid;
1972 * Reserve context ids which map to reserved virtual addresses. For now
1973 * we only reserve the context id which maps to the VRMA VSID. We ignore
1974 * the addresses in "ibm,adjunct-virtual-addresses" because we don't
1975 * enable adjunct support via the "ibm,client-architecture-support"
1978 protovsid = vsid_unscramble(VRMA_VSID, MMU_SEGSIZE_1T);
1979 hash__reserve_context_id(protovsid >> ESID_BITS_1T);
1982 machine_device_initcall(pseries, reserve_vrma_context_id);
1985 #ifdef CONFIG_DEBUG_FS
1986 /* debugfs file interface for vpa data */
1987 static ssize_t vpa_file_read(struct file *filp, char __user *buf, size_t len,
1990 int cpu = (long)filp->private_data;
1991 struct lppaca *lppaca = &lppaca_of(cpu);
1993 return simple_read_from_buffer(buf, len, pos, lppaca,
1994 sizeof(struct lppaca));
1997 static const struct file_operations vpa_fops = {
1998 .open = simple_open,
1999 .read = vpa_file_read,
2000 .llseek = default_llseek,
2003 static int __init vpa_debugfs_init(void)
2007 struct dentry *vpa_dir;
2009 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
2012 vpa_dir = debugfs_create_dir("vpa", arch_debugfs_dir);
2014 /* set up the per-cpu vpa file*/
2015 for_each_possible_cpu(i) {
2016 sprintf(name, "cpu-%ld", i);
2017 debugfs_create_file(name, 0400, vpa_dir, (void *)i, &vpa_fops);
2022 machine_arch_initcall(pseries, vpa_debugfs_init);
2023 #endif /* CONFIG_DEBUG_FS */