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 <asm/processor.h>
28 #include <asm/machdep.h>
29 #include <asm/mmu_context.h>
30 #include <asm/iommu.h>
33 #include <asm/cputable.h>
36 #include <asm/trace.h>
37 #include <asm/firmware.h>
38 #include <asm/plpar_wrappers.h>
39 #include <asm/kexec.h>
40 #include <asm/fadump.h>
41 #include <asm/asm-prototypes.h>
42 #include <asm/debugfs.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);
61 * H_BLOCK_REMOVE supported block size for this page size in segment who's base
62 * page size is that page size.
64 * The first index is the segment base page size, the second one is the actual
67 static int hblkrm_size[MMU_PAGE_COUNT][MMU_PAGE_COUNT] __ro_after_init;
70 * Due to the involved complexity, and that the current hypervisor is only
71 * returning this value or 0, we are limiting the support of the H_BLOCK_REMOVE
72 * buffer size to 8 size block.
74 #define HBLKRM_SUPPORTED_BLOCK_SIZE 8
76 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
77 static u8 dtl_mask = DTL_LOG_PREEMPT;
82 void alloc_dtl_buffers(unsigned long *time_limit)
85 struct paca_struct *pp;
86 struct dtl_entry *dtl;
88 for_each_possible_cpu(cpu) {
92 dtl = kmem_cache_alloc(dtl_cache, GFP_KERNEL);
94 pr_warn("Failed to allocate dispatch trace log for cpu %d\n",
96 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
97 pr_warn("Stolen time statistics will be unreliable\n");
103 pp->dispatch_log = dtl;
104 pp->dispatch_log_end = dtl + N_DISPATCH_LOG;
107 if (time_limit && time_after(jiffies, *time_limit)) {
109 *time_limit = jiffies + HZ;
114 void register_dtl_buffer(int cpu)
117 struct paca_struct *pp;
118 struct dtl_entry *dtl;
119 int hwcpu = get_hard_smp_processor_id(cpu);
122 dtl = pp->dispatch_log;
123 if (dtl && dtl_mask) {
126 lppaca_of(cpu).dtl_idx = 0;
128 /* hypervisor reads buffer length from this field */
129 dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);
130 ret = register_dtl(hwcpu, __pa(dtl));
132 pr_err("WARNING: DTL registration of cpu %d (hw %d) failed with %ld\n",
135 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
139 #ifdef CONFIG_PPC_SPLPAR
141 struct delayed_work work;
145 struct vcpu_dispatch_data {
156 int numa_remote_disp;
161 * This represents the number of cpus in the hypervisor. Since there is no
162 * architected way to discover the number of processors in the host, we
163 * provision for dealing with NR_CPUS. This is currently 2048 by default, and
164 * is sufficient for our purposes. This will need to be tweaked if
165 * CONFIG_NR_CPUS is changed.
167 #define NR_CPUS_H NR_CPUS
169 DEFINE_RWLOCK(dtl_access_lock);
170 static DEFINE_PER_CPU(struct vcpu_dispatch_data, vcpu_disp_data);
171 static DEFINE_PER_CPU(u64, dtl_entry_ridx);
172 static DEFINE_PER_CPU(struct dtl_worker, dtl_workers);
173 static enum cpuhp_state dtl_worker_state;
174 static DEFINE_MUTEX(dtl_enable_mutex);
175 static int vcpudispatch_stats_on __read_mostly;
176 static int vcpudispatch_stats_freq = 50;
177 static __be32 *vcpu_associativity, *pcpu_associativity;
180 static void free_dtl_buffers(unsigned long *time_limit)
182 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
184 struct paca_struct *pp;
186 for_each_possible_cpu(cpu) {
188 if (!pp->dispatch_log)
190 kmem_cache_free(dtl_cache, pp->dispatch_log);
192 pp->dispatch_log = 0;
193 pp->dispatch_log_end = 0;
196 if (time_limit && time_after(jiffies, *time_limit)) {
198 *time_limit = jiffies + HZ;
204 static int init_cpu_associativity(void)
206 vcpu_associativity = kcalloc(num_possible_cpus() / threads_per_core,
207 VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
208 pcpu_associativity = kcalloc(NR_CPUS_H / threads_per_core,
209 VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
211 if (!vcpu_associativity || !pcpu_associativity) {
212 pr_err("error allocating memory for associativity information\n");
219 static void destroy_cpu_associativity(void)
221 kfree(vcpu_associativity);
222 kfree(pcpu_associativity);
223 vcpu_associativity = pcpu_associativity = 0;
226 static __be32 *__get_cpu_associativity(int cpu, __be32 *cpu_assoc, int flag)
231 assoc = &cpu_assoc[(int)(cpu / threads_per_core) * VPHN_ASSOC_BUFSIZE];
233 rc = hcall_vphn(cpu, flag, &assoc[0]);
241 static __be32 *get_pcpu_associativity(int cpu)
243 return __get_cpu_associativity(cpu, pcpu_associativity, VPHN_FLAG_PCPU);
246 static __be32 *get_vcpu_associativity(int cpu)
248 return __get_cpu_associativity(cpu, vcpu_associativity, VPHN_FLAG_VCPU);
251 static int cpu_relative_dispatch_distance(int last_disp_cpu, int cur_disp_cpu)
253 __be32 *last_disp_cpu_assoc, *cur_disp_cpu_assoc;
255 if (last_disp_cpu >= NR_CPUS_H || cur_disp_cpu >= NR_CPUS_H)
258 last_disp_cpu_assoc = get_pcpu_associativity(last_disp_cpu);
259 cur_disp_cpu_assoc = get_pcpu_associativity(cur_disp_cpu);
261 if (!last_disp_cpu_assoc || !cur_disp_cpu_assoc)
264 return cpu_distance(last_disp_cpu_assoc, cur_disp_cpu_assoc);
267 static int cpu_home_node_dispatch_distance(int disp_cpu)
269 __be32 *disp_cpu_assoc, *vcpu_assoc;
270 int vcpu_id = smp_processor_id();
272 if (disp_cpu >= NR_CPUS_H) {
273 pr_debug_ratelimited("vcpu dispatch cpu %d > %d\n",
274 disp_cpu, NR_CPUS_H);
278 disp_cpu_assoc = get_pcpu_associativity(disp_cpu);
279 vcpu_assoc = get_vcpu_associativity(vcpu_id);
281 if (!disp_cpu_assoc || !vcpu_assoc)
284 return cpu_distance(disp_cpu_assoc, vcpu_assoc);
287 static void update_vcpu_disp_stat(int disp_cpu)
289 struct vcpu_dispatch_data *disp;
292 disp = this_cpu_ptr(&vcpu_disp_data);
293 if (disp->last_disp_cpu == -1) {
294 disp->last_disp_cpu = disp_cpu;
300 if (disp->last_disp_cpu == disp_cpu ||
301 (cpu_first_thread_sibling(disp->last_disp_cpu) ==
302 cpu_first_thread_sibling(disp_cpu)))
303 disp->same_cpu_disp++;
305 distance = cpu_relative_dispatch_distance(disp->last_disp_cpu,
308 pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
313 disp->same_chip_disp++;
316 disp->diff_chip_disp++;
319 disp->far_chip_disp++;
322 pr_debug_ratelimited("vcpudispatch_stats: cpu %d (%d -> %d): unexpected relative dispatch distance %d\n",
331 distance = cpu_home_node_dispatch_distance(disp_cpu);
333 pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
338 disp->numa_home_disp++;
341 disp->numa_remote_disp++;
344 disp->numa_far_disp++;
347 pr_debug_ratelimited("vcpudispatch_stats: cpu %d on %d: unexpected numa dispatch distance %d\n",
354 disp->last_disp_cpu = disp_cpu;
357 static void process_dtl_buffer(struct work_struct *work)
359 struct dtl_entry dtle;
360 u64 i = __this_cpu_read(dtl_entry_ridx);
361 struct dtl_entry *dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
362 struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
363 struct lppaca *vpa = local_paca->lppaca_ptr;
364 struct dtl_worker *d = container_of(work, struct dtl_worker, work.work);
366 if (!local_paca->dispatch_log)
369 /* if we have been migrated away, we cancel ourself */
370 if (d->cpu != smp_processor_id()) {
371 pr_debug("vcpudispatch_stats: cpu %d worker migrated -- canceling worker\n",
376 if (i == be64_to_cpu(vpa->dtl_idx))
379 while (i < be64_to_cpu(vpa->dtl_idx)) {
382 if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
383 /* buffer has overflowed */
384 pr_debug_ratelimited("vcpudispatch_stats: cpu %d lost %lld DTL samples\n",
386 be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG - i);
387 i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
388 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
391 update_vcpu_disp_stat(be16_to_cpu(dtle.processor_id));
395 dtl = local_paca->dispatch_log;
398 __this_cpu_write(dtl_entry_ridx, i);
401 schedule_delayed_work_on(d->cpu, to_delayed_work(work),
402 HZ / vcpudispatch_stats_freq);
405 static int dtl_worker_online(unsigned int cpu)
407 struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
409 memset(d, 0, sizeof(*d));
410 INIT_DELAYED_WORK(&d->work, process_dtl_buffer);
413 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
414 per_cpu(dtl_entry_ridx, cpu) = 0;
415 register_dtl_buffer(cpu);
417 per_cpu(dtl_entry_ridx, cpu) = be64_to_cpu(lppaca_of(cpu).dtl_idx);
420 schedule_delayed_work_on(cpu, &d->work, HZ / vcpudispatch_stats_freq);
424 static int dtl_worker_offline(unsigned int cpu)
426 struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
428 cancel_delayed_work_sync(&d->work);
430 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
431 unregister_dtl(get_hard_smp_processor_id(cpu));
437 static void set_global_dtl_mask(u8 mask)
442 for_each_present_cpu(cpu)
443 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
446 static void reset_global_dtl_mask(void)
450 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
451 dtl_mask = DTL_LOG_PREEMPT;
455 for_each_present_cpu(cpu)
456 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
459 static int dtl_worker_enable(unsigned long *time_limit)
463 if (!write_trylock(&dtl_access_lock)) {
468 set_global_dtl_mask(DTL_LOG_ALL);
470 /* Setup dtl buffers and register those */
471 alloc_dtl_buffers(time_limit);
473 state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/dtl:online",
474 dtl_worker_online, dtl_worker_offline);
476 pr_err("vcpudispatch_stats: unable to setup workqueue for DTL processing\n");
477 free_dtl_buffers(time_limit);
478 reset_global_dtl_mask();
479 write_unlock(&dtl_access_lock);
483 dtl_worker_state = state;
489 static void dtl_worker_disable(unsigned long *time_limit)
491 cpuhp_remove_state(dtl_worker_state);
492 free_dtl_buffers(time_limit);
493 reset_global_dtl_mask();
494 write_unlock(&dtl_access_lock);
497 static ssize_t vcpudispatch_stats_write(struct file *file, const char __user *p,
498 size_t count, loff_t *ppos)
500 unsigned long time_limit = jiffies + HZ;
501 struct vcpu_dispatch_data *disp;
508 if (copy_from_user(buf, p, count))
512 rc = kstrtoint(buf, 0, &cmd);
513 if (rc || cmd < 0 || cmd > 1) {
514 pr_err("vcpudispatch_stats: please use 0 to disable or 1 to enable dispatch statistics\n");
515 return rc ? rc : -EINVAL;
518 mutex_lock(&dtl_enable_mutex);
520 if ((cmd == 0 && !vcpudispatch_stats_on) ||
521 (cmd == 1 && vcpudispatch_stats_on))
525 rc = init_cpu_associativity();
529 for_each_possible_cpu(cpu) {
530 disp = per_cpu_ptr(&vcpu_disp_data, cpu);
531 memset(disp, 0, sizeof(*disp));
532 disp->last_disp_cpu = -1;
535 rc = dtl_worker_enable(&time_limit);
537 destroy_cpu_associativity();
541 dtl_worker_disable(&time_limit);
542 destroy_cpu_associativity();
545 vcpudispatch_stats_on = cmd;
548 mutex_unlock(&dtl_enable_mutex);
554 static int vcpudispatch_stats_display(struct seq_file *p, void *v)
557 struct vcpu_dispatch_data *disp;
559 if (!vcpudispatch_stats_on) {
560 seq_puts(p, "off\n");
564 for_each_online_cpu(cpu) {
565 disp = per_cpu_ptr(&vcpu_disp_data, cpu);
566 seq_printf(p, "cpu%d", cpu);
567 seq_put_decimal_ull(p, " ", disp->total_disp);
568 seq_put_decimal_ull(p, " ", disp->same_cpu_disp);
569 seq_put_decimal_ull(p, " ", disp->same_chip_disp);
570 seq_put_decimal_ull(p, " ", disp->diff_chip_disp);
571 seq_put_decimal_ull(p, " ", disp->far_chip_disp);
572 seq_put_decimal_ull(p, " ", disp->numa_home_disp);
573 seq_put_decimal_ull(p, " ", disp->numa_remote_disp);
574 seq_put_decimal_ull(p, " ", disp->numa_far_disp);
581 static int vcpudispatch_stats_open(struct inode *inode, struct file *file)
583 return single_open(file, vcpudispatch_stats_display, NULL);
586 static const struct proc_ops vcpudispatch_stats_proc_ops = {
587 .proc_open = vcpudispatch_stats_open,
588 .proc_read = seq_read,
589 .proc_write = vcpudispatch_stats_write,
590 .proc_lseek = seq_lseek,
591 .proc_release = single_release,
594 static ssize_t vcpudispatch_stats_freq_write(struct file *file,
595 const char __user *p, size_t count, loff_t *ppos)
603 if (copy_from_user(buf, p, count))
607 rc = kstrtoint(buf, 0, &freq);
608 if (rc || freq < 1 || freq > HZ) {
609 pr_err("vcpudispatch_stats_freq: please specify a frequency between 1 and %d\n",
611 return rc ? rc : -EINVAL;
614 vcpudispatch_stats_freq = freq;
619 static int vcpudispatch_stats_freq_display(struct seq_file *p, void *v)
621 seq_printf(p, "%d\n", vcpudispatch_stats_freq);
625 static int vcpudispatch_stats_freq_open(struct inode *inode, struct file *file)
627 return single_open(file, vcpudispatch_stats_freq_display, NULL);
630 static const struct proc_ops vcpudispatch_stats_freq_proc_ops = {
631 .proc_open = vcpudispatch_stats_freq_open,
632 .proc_read = seq_read,
633 .proc_write = vcpudispatch_stats_freq_write,
634 .proc_lseek = seq_lseek,
635 .proc_release = single_release,
638 static int __init vcpudispatch_stats_procfs_init(void)
641 * Avoid smp_processor_id while preemptible. All CPUs should have
642 * the same value for lppaca_shared_proc.
645 if (!lppaca_shared_proc(get_lppaca())) {
651 if (!proc_create("powerpc/vcpudispatch_stats", 0600, NULL,
652 &vcpudispatch_stats_proc_ops))
653 pr_err("vcpudispatch_stats: error creating procfs file\n");
654 else if (!proc_create("powerpc/vcpudispatch_stats_freq", 0600, NULL,
655 &vcpudispatch_stats_freq_proc_ops))
656 pr_err("vcpudispatch_stats_freq: error creating procfs file\n");
661 machine_device_initcall(pseries, vcpudispatch_stats_procfs_init);
662 #endif /* CONFIG_PPC_SPLPAR */
664 void vpa_init(int cpu)
666 int hwcpu = get_hard_smp_processor_id(cpu);
671 * The spec says it "may be problematic" if CPU x registers the VPA of
672 * CPU y. We should never do that, but wail if we ever do.
674 WARN_ON(cpu != smp_processor_id());
676 if (cpu_has_feature(CPU_FTR_ALTIVEC))
677 lppaca_of(cpu).vmxregs_in_use = 1;
679 if (cpu_has_feature(CPU_FTR_ARCH_207S))
680 lppaca_of(cpu).ebb_regs_in_use = 1;
682 addr = __pa(&lppaca_of(cpu));
683 ret = register_vpa(hwcpu, addr);
686 pr_err("WARNING: VPA registration for cpu %d (hw %d) of area "
687 "%lx failed with %ld\n", cpu, hwcpu, addr, ret);
691 #ifdef CONFIG_PPC_BOOK3S_64
693 * PAPR says this feature is SLB-Buffer but firmware never
694 * reports that. All SPLPAR support SLB shadow buffer.
696 if (!radix_enabled() && firmware_has_feature(FW_FEATURE_SPLPAR)) {
697 addr = __pa(paca_ptrs[cpu]->slb_shadow_ptr);
698 ret = register_slb_shadow(hwcpu, addr);
700 pr_err("WARNING: SLB shadow buffer registration for "
701 "cpu %d (hw %d) of area %lx failed with %ld\n",
702 cpu, hwcpu, addr, ret);
704 #endif /* CONFIG_PPC_BOOK3S_64 */
707 * Register dispatch trace log, if one has been allocated.
709 register_dtl_buffer(cpu);
712 #ifdef CONFIG_PPC_BOOK3S_64
714 static long pSeries_lpar_hpte_insert(unsigned long hpte_group,
715 unsigned long vpn, unsigned long pa,
716 unsigned long rflags, unsigned long vflags,
717 int psize, int apsize, int ssize)
719 unsigned long lpar_rc;
722 unsigned long hpte_v, hpte_r;
724 if (!(vflags & HPTE_V_BOLTED))
725 pr_devel("hpte_insert(group=%lx, vpn=%016lx, "
726 "pa=%016lx, rflags=%lx, vflags=%lx, psize=%d)\n",
727 hpte_group, vpn, pa, rflags, vflags, psize);
729 hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
730 hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
732 if (!(vflags & HPTE_V_BOLTED))
733 pr_devel(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
735 /* Now fill in the actual HPTE */
736 /* Set CEC cookie to 0 */
738 /* I-cache Invalidate = 0 */
739 /* I-cache synchronize = 0 */
743 if (firmware_has_feature(FW_FEATURE_XCMO) && !(hpte_r & HPTE_R_N))
744 flags |= H_COALESCE_CAND;
746 lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot);
747 if (unlikely(lpar_rc == H_PTEG_FULL)) {
748 pr_devel("Hash table group is full\n");
753 * Since we try and ioremap PHBs we don't own, the pte insert
754 * will fail. However we must catch the failure in hash_page
755 * or we will loop forever, so return -2 in this case.
757 if (unlikely(lpar_rc != H_SUCCESS)) {
758 pr_err("Failed hash pte insert with error %ld\n", lpar_rc);
761 if (!(vflags & HPTE_V_BOLTED))
762 pr_devel(" -> slot: %lu\n", slot & 7);
764 /* Because of iSeries, we have to pass down the secondary
765 * bucket bit here as well
767 return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);
770 static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
772 static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
774 unsigned long slot_offset;
775 unsigned long lpar_rc;
777 unsigned long dummy1, dummy2;
779 /* pick a random slot to start at */
780 slot_offset = mftb() & 0x7;
782 for (i = 0; i < HPTES_PER_GROUP; i++) {
784 /* don't remove a bolted entry */
785 lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
786 HPTE_V_BOLTED, &dummy1, &dummy2);
787 if (lpar_rc == H_SUCCESS)
791 * The test for adjunct partition is performed before the
792 * ANDCOND test. H_RESOURCE may be returned, so we need to
793 * check for that as well.
795 BUG_ON(lpar_rc != H_NOT_FOUND && lpar_rc != H_RESOURCE);
804 static void manual_hpte_clear_all(void)
806 unsigned long size_bytes = 1UL << ppc64_pft_size;
807 unsigned long hpte_count = size_bytes >> 4;
815 /* Read in batches of 4,
816 * invalidate only valid entries not in the VRMA
817 * hpte_count will be a multiple of 4
819 for (i = 0; i < hpte_count; i += 4) {
820 lpar_rc = plpar_pte_read_4_raw(0, i, (void *)ptes);
821 if (lpar_rc != H_SUCCESS) {
822 pr_info("Failed to read hash page table at %ld err %ld\n",
826 for (j = 0; j < 4; j++){
827 if ((ptes[j].pteh & HPTE_V_VRMA_MASK) ==
830 if (ptes[j].pteh & HPTE_V_VALID)
831 plpar_pte_remove_raw(0, i + j, 0,
832 &(ptes[j].pteh), &(ptes[j].ptel));
837 static int hcall_hpte_clear_all(void)
842 rc = plpar_hcall_norets(H_CLEAR_HPT);
843 } while (rc == H_CONTINUE);
848 static void pseries_hpte_clear_all(void)
852 rc = hcall_hpte_clear_all();
854 manual_hpte_clear_all();
856 #ifdef __LITTLE_ENDIAN__
858 * Reset exceptions to big endian.
860 * FIXME this is a hack for kexec, we need to reset the exception
861 * endian before starting the new kernel and this is a convenient place
864 * This is also called on boot when a fadump happens. In that case we
865 * must not change the exception endian mode.
867 if (firmware_has_feature(FW_FEATURE_SET_MODE) && !is_fadump_active())
868 pseries_big_endian_exceptions();
873 * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
874 * the low 3 bits of flags happen to line up. So no transform is needed.
875 * We can probably optimize here and assume the high bits of newpp are
876 * already zero. For now I am paranoid.
878 static long pSeries_lpar_hpte_updatepp(unsigned long slot,
881 int psize, int apsize,
882 int ssize, unsigned long inv_flags)
884 unsigned long lpar_rc;
886 unsigned long want_v;
888 want_v = hpte_encode_avpn(vpn, psize, ssize);
890 flags = (newpp & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO)) | H_AVPN;
891 flags |= (newpp & HPTE_R_KEY_HI) >> 48;
892 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
893 /* Move pp0 into bit 8 (IBM 55) */
894 flags |= (newpp & HPTE_R_PP0) >> 55;
896 pr_devel(" update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...",
897 want_v, slot, flags, psize);
899 lpar_rc = plpar_pte_protect(flags, slot, want_v);
901 if (lpar_rc == H_NOT_FOUND) {
902 pr_devel("not found !\n");
908 BUG_ON(lpar_rc != H_SUCCESS);
913 static long __pSeries_lpar_hpte_find(unsigned long want_v, unsigned long hpte_group)
922 for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
924 lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
925 if (lpar_rc != H_SUCCESS) {
926 pr_info("Failed to read hash page table at %ld err %ld\n",
927 hpte_group, lpar_rc);
931 for (j = 0; j < 4; j++) {
932 if (HPTE_V_COMPARE(ptes[j].pteh, want_v) &&
933 (ptes[j].pteh & HPTE_V_VALID))
941 static long pSeries_lpar_hpte_find(unsigned long vpn, int psize, int ssize)
945 unsigned long want_v;
946 unsigned long hpte_group;
948 hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
949 want_v = hpte_encode_avpn(vpn, psize, ssize);
952 * We try to keep bolted entries always in primary hash
953 * But in some case we can find them in secondary too.
955 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
956 slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
958 /* Try in secondary */
959 hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
960 slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
964 return hpte_group + slot;
967 static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
969 int psize, int ssize)
972 unsigned long lpar_rc, slot, vsid, flags;
974 vsid = get_kernel_vsid(ea, ssize);
975 vpn = hpt_vpn(ea, vsid, ssize);
977 slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
981 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
982 /* Move pp0 into bit 8 (IBM 55) */
983 flags |= (newpp & HPTE_R_PP0) >> 55;
985 lpar_rc = plpar_pte_protect(flags, slot, 0);
987 BUG_ON(lpar_rc != H_SUCCESS);
990 static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn,
991 int psize, int apsize,
992 int ssize, int local)
994 unsigned long want_v;
995 unsigned long lpar_rc;
996 unsigned long dummy1, dummy2;
998 pr_devel(" inval : slot=%lx, vpn=%016lx, psize: %d, local: %d\n",
999 slot, vpn, psize, local);
1001 want_v = hpte_encode_avpn(vpn, psize, ssize);
1002 lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2);
1003 if (lpar_rc == H_NOT_FOUND)
1006 BUG_ON(lpar_rc != H_SUCCESS);
1011 * As defined in the PAPR's section 14.5.4.1.8
1012 * The control mask doesn't include the returned reference and change bit from
1013 * the processed PTE.
1015 #define HBLKR_AVPN 0x0100000000000000UL
1016 #define HBLKR_CTRL_MASK 0xf800000000000000UL
1017 #define HBLKR_CTRL_SUCCESS 0x8000000000000000UL
1018 #define HBLKR_CTRL_ERRNOTFOUND 0x8800000000000000UL
1019 #define HBLKR_CTRL_ERRBUSY 0xa000000000000000UL
1022 * Returned true if we are supporting this block size for the specified segment
1023 * base page size and actual page size.
1025 * Currently, we only support 8 size block.
1027 static inline bool is_supported_hlbkrm(int bpsize, int psize)
1029 return (hblkrm_size[bpsize][psize] == HBLKRM_SUPPORTED_BLOCK_SIZE);
1033 * H_BLOCK_REMOVE caller.
1034 * @idx should point to the latest @param entry set with a PTEX.
1035 * If PTE cannot be processed because another CPUs has already locked that
1036 * group, those entries are put back in @param starting at index 1.
1037 * If entries has to be retried and @retry_busy is set to true, these entries
1038 * are retried until success. If @retry_busy is set to false, the returned
1039 * is the number of entries yet to process.
1041 static unsigned long call_block_remove(unsigned long idx, unsigned long *param,
1044 unsigned long i, rc, new_idx;
1045 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1048 pr_warn("Unexpected empty call to H_BLOCK_REMOVE");
1053 if (idx > PLPAR_HCALL9_BUFSIZE) {
1054 pr_err("Too many PTEs (%lu) for H_BLOCK_REMOVE", idx);
1055 idx = PLPAR_HCALL9_BUFSIZE;
1056 } else if (idx < PLPAR_HCALL9_BUFSIZE)
1057 param[idx] = HBR_END;
1059 rc = plpar_hcall9(H_BLOCK_REMOVE, retbuf,
1061 param[1], param[2], param[3], param[4], /* TS0-7 */
1062 param[5], param[6], param[7], param[8]);
1063 if (rc == H_SUCCESS)
1066 BUG_ON(rc != H_PARTIAL);
1068 /* Check that the unprocessed entries were 'not found' or 'busy' */
1069 for (i = 0; i < idx-1; i++) {
1070 unsigned long ctrl = retbuf[i] & HBLKR_CTRL_MASK;
1072 if (ctrl == HBLKR_CTRL_ERRBUSY) {
1073 param[++new_idx] = param[i+1];
1077 BUG_ON(ctrl != HBLKR_CTRL_SUCCESS
1078 && ctrl != HBLKR_CTRL_ERRNOTFOUND);
1082 * If there were entries found busy, retry these entries if requested,
1083 * of if all the entries have to be retried.
1085 if (new_idx && (retry_busy || new_idx == (PLPAR_HCALL9_BUFSIZE-1))) {
1093 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1095 * Limit iterations holding pSeries_lpar_tlbie_lock to 3. We also need
1096 * to make sure that we avoid bouncing the hypervisor tlbie lock.
1098 #define PPC64_HUGE_HPTE_BATCH 12
1100 static void hugepage_block_invalidate(unsigned long *slot, unsigned long *vpn,
1101 int count, int psize, int ssize)
1103 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1104 unsigned long shift, current_vpgb, vpgb;
1107 shift = mmu_psize_defs[psize].shift;
1109 for (i = 0; i < count; i++) {
1111 * Shifting 3 bits more on the right to get a
1112 * 8 pages aligned virtual addresse.
1114 vpgb = (vpn[i] >> (shift - VPN_SHIFT + 3));
1115 if (!pix || vpgb != current_vpgb) {
1117 * Need to start a new 8 pages block, flush
1118 * the current one if needed.
1121 (void)call_block_remove(pix, param, true);
1122 current_vpgb = vpgb;
1123 param[0] = hpte_encode_avpn(vpn[i], psize, ssize);
1127 param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot[i];
1128 if (pix == PLPAR_HCALL9_BUFSIZE) {
1129 pix = call_block_remove(pix, param, false);
1131 * pix = 0 means that all the entries were
1132 * removed, we can start a new block.
1133 * Otherwise, this means that there are entries
1134 * to retry, and pix points to latest one, so
1135 * we should increment it and try to continue
1143 (void)call_block_remove(pix, param, true);
1146 static void hugepage_bulk_invalidate(unsigned long *slot, unsigned long *vpn,
1147 int count, int psize, int ssize)
1149 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1150 int i = 0, pix = 0, rc;
1152 for (i = 0; i < count; i++) {
1154 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1155 pSeries_lpar_hpte_invalidate(slot[i], vpn[i], psize, 0,
1158 param[pix] = HBR_REQUEST | HBR_AVPN | slot[i];
1159 param[pix+1] = hpte_encode_avpn(vpn[i], psize, ssize);
1162 rc = plpar_hcall9(H_BULK_REMOVE, param,
1163 param[0], param[1], param[2],
1164 param[3], param[4], param[5],
1165 param[6], param[7]);
1166 BUG_ON(rc != H_SUCCESS);
1172 param[pix] = HBR_END;
1173 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1174 param[2], param[3], param[4], param[5],
1175 param[6], param[7]);
1176 BUG_ON(rc != H_SUCCESS);
1180 static inline void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
1182 int count, int psize,
1185 unsigned long flags = 0;
1186 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1189 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1191 /* Assuming THP size is 16M */
1192 if (is_supported_hlbkrm(psize, MMU_PAGE_16M))
1193 hugepage_block_invalidate(slot, vpn, count, psize, ssize);
1195 hugepage_bulk_invalidate(slot, vpn, count, psize, ssize);
1198 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1201 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1203 unsigned char *hpte_slot_array,
1204 int psize, int ssize, int local)
1207 unsigned long s_addr = addr;
1208 unsigned int max_hpte_count, valid;
1209 unsigned long vpn_array[PPC64_HUGE_HPTE_BATCH];
1210 unsigned long slot_array[PPC64_HUGE_HPTE_BATCH];
1211 unsigned long shift, hidx, vpn = 0, hash, slot;
1213 shift = mmu_psize_defs[psize].shift;
1214 max_hpte_count = 1U << (PMD_SHIFT - shift);
1216 for (i = 0; i < max_hpte_count; i++) {
1217 valid = hpte_valid(hpte_slot_array, i);
1220 hidx = hpte_hash_index(hpte_slot_array, i);
1223 addr = s_addr + (i * (1ul << shift));
1224 vpn = hpt_vpn(addr, vsid, ssize);
1225 hash = hpt_hash(vpn, shift, ssize);
1226 if (hidx & _PTEIDX_SECONDARY)
1229 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1230 slot += hidx & _PTEIDX_GROUP_IX;
1232 slot_array[index] = slot;
1233 vpn_array[index] = vpn;
1234 if (index == PPC64_HUGE_HPTE_BATCH - 1) {
1236 * Now do a bluk invalidate
1238 __pSeries_lpar_hugepage_invalidate(slot_array,
1240 PPC64_HUGE_HPTE_BATCH,
1247 __pSeries_lpar_hugepage_invalidate(slot_array, vpn_array,
1248 index, psize, ssize);
1251 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1253 unsigned char *hpte_slot_array,
1254 int psize, int ssize, int local)
1256 WARN(1, "%s called without THP support\n", __func__);
1260 static int pSeries_lpar_hpte_removebolted(unsigned long ea,
1261 int psize, int ssize)
1264 unsigned long slot, vsid;
1266 vsid = get_kernel_vsid(ea, ssize);
1267 vpn = hpt_vpn(ea, vsid, ssize);
1269 slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
1274 * lpar doesn't use the passed actual page size
1276 pSeries_lpar_hpte_invalidate(slot, vpn, psize, 0, ssize, 0);
1281 static inline unsigned long compute_slot(real_pte_t pte,
1283 unsigned long index,
1284 unsigned long shift,
1287 unsigned long slot, hash, hidx;
1289 hash = hpt_hash(vpn, shift, ssize);
1290 hidx = __rpte_to_hidx(pte, index);
1291 if (hidx & _PTEIDX_SECONDARY)
1293 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1294 slot += hidx & _PTEIDX_GROUP_IX;
1299 * The hcall H_BLOCK_REMOVE implies that the virtual pages to processed are
1300 * "all within the same naturally aligned 8 page virtual address block".
1302 static void do_block_remove(unsigned long number, struct ppc64_tlb_batch *batch,
1303 unsigned long *param)
1306 unsigned long i, pix = 0;
1307 unsigned long index, shift, slot, current_vpgb, vpgb;
1311 psize = batch->psize;
1312 ssize = batch->ssize;
1314 for (i = 0; i < number; i++) {
1315 vpn = batch->vpn[i];
1316 pte = batch->pte[i];
1317 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1319 * Shifting 3 bits more on the right to get a
1320 * 8 pages aligned virtual addresse.
1322 vpgb = (vpn >> (shift - VPN_SHIFT + 3));
1323 if (!pix || vpgb != current_vpgb) {
1325 * Need to start a new 8 pages block, flush
1326 * the current one if needed.
1329 (void)call_block_remove(pix, param,
1331 current_vpgb = vpgb;
1332 param[0] = hpte_encode_avpn(vpn, psize,
1337 slot = compute_slot(pte, vpn, index, shift, ssize);
1338 param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot;
1340 if (pix == PLPAR_HCALL9_BUFSIZE) {
1341 pix = call_block_remove(pix, param, false);
1343 * pix = 0 means that all the entries were
1344 * removed, we can start a new block.
1345 * Otherwise, this means that there are entries
1346 * to retry, and pix points to latest one, so
1347 * we should increment it and try to continue
1353 } pte_iterate_hashed_end();
1357 (void)call_block_remove(pix, param, true);
1361 * TLB Block Invalidate Characteristics
1363 * These characteristics define the size of the block the hcall H_BLOCK_REMOVE
1364 * is able to process for each couple segment base page size, actual page size.
1366 * The ibm,get-system-parameter properties is returning a buffer with the
1369 * [ 2 bytes size of the RTAS buffer (excluding these 2 bytes) ]
1371 * TLB Block Invalidate Specifiers:
1372 * [ 1 byte LOG base 2 of the TLB invalidate block size being specified ]
1373 * [ 1 byte Number of page sizes (N) that are supported for the specified
1374 * TLB invalidate block size ]
1375 * [ 1 byte Encoded segment base page size and actual page size
1376 * MSB=0 means 4k segment base page size and actual page size
1377 * MSB=1 the penc value in mmu_psize_def ]
1380 * Next TLB Block Invalidate Specifiers...
1384 static inline void set_hblkrm_bloc_size(int bpsize, int psize,
1385 unsigned int block_size)
1387 if (block_size > hblkrm_size[bpsize][psize])
1388 hblkrm_size[bpsize][psize] = block_size;
1392 * Decode the Encoded segment base page size and actual page size.
1394 * - bit 7 is the L bit
1395 * - bits 0-5 are the penc value
1396 * If the L bit is 0, this means 4K segment base page size and actual page size
1397 * otherwise the penc value should be read.
1399 #define HBLKRM_L_MASK 0x80
1400 #define HBLKRM_PENC_MASK 0x3f
1401 static inline void __init check_lp_set_hblkrm(unsigned int lp,
1402 unsigned int block_size)
1404 unsigned int bpsize, psize;
1406 /* First, check the L bit, if not set, this means 4K */
1407 if ((lp & HBLKRM_L_MASK) == 0) {
1408 set_hblkrm_bloc_size(MMU_PAGE_4K, MMU_PAGE_4K, block_size);
1412 lp &= HBLKRM_PENC_MASK;
1413 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++) {
1414 struct mmu_psize_def *def = &mmu_psize_defs[bpsize];
1416 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1417 if (def->penc[psize] == lp) {
1418 set_hblkrm_bloc_size(bpsize, psize, block_size);
1425 #define SPLPAR_TLB_BIC_TOKEN 50
1428 * The size of the TLB Block Invalidate Characteristics is variable. But at the
1429 * maximum it will be the number of possible page sizes *2 + 10 bytes.
1430 * Currently MMU_PAGE_COUNT is 16, which means 42 bytes. Use a cache line size
1431 * (128 bytes) for the buffer to get plenty of space.
1433 #define SPLPAR_TLB_BIC_MAXLENGTH 128
1435 void __init pseries_lpar_read_hblkrm_characteristics(void)
1437 unsigned char local_buffer[SPLPAR_TLB_BIC_MAXLENGTH];
1438 int call_status, len, idx, bpsize;
1440 if (!firmware_has_feature(FW_FEATURE_BLOCK_REMOVE))
1443 spin_lock(&rtas_data_buf_lock);
1444 memset(rtas_data_buf, 0, RTAS_DATA_BUF_SIZE);
1445 call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
1447 SPLPAR_TLB_BIC_TOKEN,
1448 __pa(rtas_data_buf),
1449 RTAS_DATA_BUF_SIZE);
1450 memcpy(local_buffer, rtas_data_buf, SPLPAR_TLB_BIC_MAXLENGTH);
1451 local_buffer[SPLPAR_TLB_BIC_MAXLENGTH - 1] = '\0';
1452 spin_unlock(&rtas_data_buf_lock);
1454 if (call_status != 0) {
1455 pr_warn("%s %s Error calling get-system-parameter (0x%x)\n",
1456 __FILE__, __func__, call_status);
1461 * The first two (2) bytes of the data in the buffer are the length of
1462 * the returned data, not counting these first two (2) bytes.
1464 len = be16_to_cpu(*((u16 *)local_buffer)) + 2;
1465 if (len > SPLPAR_TLB_BIC_MAXLENGTH) {
1466 pr_warn("%s too large returned buffer %d", __func__, len);
1472 u8 block_shift = local_buffer[idx++];
1474 unsigned int npsize;
1479 block_size = 1 << block_shift;
1481 for (npsize = local_buffer[idx++];
1482 npsize > 0 && idx < len; npsize--)
1483 check_lp_set_hblkrm((unsigned int) local_buffer[idx++],
1487 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
1488 for (idx = 0; idx < MMU_PAGE_COUNT; idx++)
1489 if (hblkrm_size[bpsize][idx])
1490 pr_info("H_BLOCK_REMOVE supports base psize:%d psize:%d block size:%d",
1491 bpsize, idx, hblkrm_size[bpsize][idx]);
1495 * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
1498 static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
1501 unsigned long i, pix, rc;
1502 unsigned long flags = 0;
1503 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
1504 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1505 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1506 unsigned long index, shift, slot;
1511 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1513 if (is_supported_hlbkrm(batch->psize, batch->psize)) {
1514 do_block_remove(number, batch, param);
1518 psize = batch->psize;
1519 ssize = batch->ssize;
1521 for (i = 0; i < number; i++) {
1522 vpn = batch->vpn[i];
1523 pte = batch->pte[i];
1524 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1525 slot = compute_slot(pte, vpn, index, shift, ssize);
1526 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1528 * lpar doesn't use the passed actual page size
1530 pSeries_lpar_hpte_invalidate(slot, vpn, psize,
1533 param[pix] = HBR_REQUEST | HBR_AVPN | slot;
1534 param[pix+1] = hpte_encode_avpn(vpn, psize,
1538 rc = plpar_hcall9(H_BULK_REMOVE, param,
1539 param[0], param[1], param[2],
1540 param[3], param[4], param[5],
1541 param[6], param[7]);
1542 BUG_ON(rc != H_SUCCESS);
1546 } pte_iterate_hashed_end();
1549 param[pix] = HBR_END;
1550 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1551 param[2], param[3], param[4], param[5],
1552 param[6], param[7]);
1553 BUG_ON(rc != H_SUCCESS);
1558 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1561 static int __init disable_bulk_remove(char *str)
1563 if (strcmp(str, "off") == 0 &&
1564 firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1565 pr_info("Disabling BULK_REMOVE firmware feature");
1566 powerpc_firmware_features &= ~FW_FEATURE_BULK_REMOVE;
1571 __setup("bulk_remove=", disable_bulk_remove);
1573 #define HPT_RESIZE_TIMEOUT 10000 /* ms */
1575 struct hpt_resize_state {
1576 unsigned long shift;
1580 static int pseries_lpar_resize_hpt_commit(void *data)
1582 struct hpt_resize_state *state = data;
1584 state->commit_rc = plpar_resize_hpt_commit(0, state->shift);
1585 if (state->commit_rc != H_SUCCESS)
1588 /* Hypervisor has transitioned the HTAB, update our globals */
1589 ppc64_pft_size = state->shift;
1590 htab_size_bytes = 1UL << ppc64_pft_size;
1591 htab_hash_mask = (htab_size_bytes >> 7) - 1;
1597 * Must be called in process context. The caller must hold the
1600 static int pseries_lpar_resize_hpt(unsigned long shift)
1602 struct hpt_resize_state state = {
1604 .commit_rc = H_FUNCTION,
1606 unsigned int delay, total_delay = 0;
1612 if (!firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1615 pr_info("Attempting to resize HPT to shift %lu\n", shift);
1619 rc = plpar_resize_hpt_prepare(0, shift);
1620 while (H_IS_LONG_BUSY(rc)) {
1621 delay = get_longbusy_msecs(rc);
1622 total_delay += delay;
1623 if (total_delay > HPT_RESIZE_TIMEOUT) {
1624 /* prepare with shift==0 cancels an in-progress resize */
1625 rc = plpar_resize_hpt_prepare(0, 0);
1626 if (rc != H_SUCCESS)
1627 pr_warn("Unexpected error %d cancelling timed out HPT resize\n",
1632 rc = plpar_resize_hpt_prepare(0, shift);
1641 pr_warn("Invalid argument from H_RESIZE_HPT_PREPARE\n");
1644 pr_warn("Operation not permitted from H_RESIZE_HPT_PREPARE\n");
1647 pr_warn("Unexpected error %d from H_RESIZE_HPT_PREPARE\n", rc);
1653 rc = stop_machine_cpuslocked(pseries_lpar_resize_hpt_commit,
1659 switch (state.commit_rc) {
1664 pr_warn("Unexpected error %d from H_RESIZE_HPT_COMMIT\n",
1670 pr_info("HPT resize to shift %lu complete (%lld ms / %lld ms)\n",
1671 shift, (long long) ktime_ms_delta(t1, t0),
1672 (long long) ktime_ms_delta(t2, t1));
1677 static int pseries_lpar_register_process_table(unsigned long base,
1678 unsigned long page_size, unsigned long table_size)
1681 unsigned long flags = 0;
1684 flags |= PROC_TABLE_NEW;
1685 if (radix_enabled()) {
1686 flags |= PROC_TABLE_RADIX;
1687 if (mmu_has_feature(MMU_FTR_GTSE))
1688 flags |= PROC_TABLE_GTSE;
1690 flags |= PROC_TABLE_HPT_SLB;
1692 rc = plpar_hcall_norets(H_REGISTER_PROC_TBL, flags, base,
1693 page_size, table_size);
1694 if (!H_IS_LONG_BUSY(rc))
1696 mdelay(get_longbusy_msecs(rc));
1698 if (rc != H_SUCCESS) {
1699 pr_err("Failed to register process table (rc=%ld)\n", rc);
1705 void __init hpte_init_pseries(void)
1707 mmu_hash_ops.hpte_invalidate = pSeries_lpar_hpte_invalidate;
1708 mmu_hash_ops.hpte_updatepp = pSeries_lpar_hpte_updatepp;
1709 mmu_hash_ops.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
1710 mmu_hash_ops.hpte_insert = pSeries_lpar_hpte_insert;
1711 mmu_hash_ops.hpte_remove = pSeries_lpar_hpte_remove;
1712 mmu_hash_ops.hpte_removebolted = pSeries_lpar_hpte_removebolted;
1713 mmu_hash_ops.flush_hash_range = pSeries_lpar_flush_hash_range;
1714 mmu_hash_ops.hpte_clear_all = pseries_hpte_clear_all;
1715 mmu_hash_ops.hugepage_invalidate = pSeries_lpar_hugepage_invalidate;
1717 if (firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1718 mmu_hash_ops.resize_hpt = pseries_lpar_resize_hpt;
1721 * On POWER9, we need to do a H_REGISTER_PROC_TBL hcall
1722 * to inform the hypervisor that we wish to use the HPT.
1724 if (cpu_has_feature(CPU_FTR_ARCH_300))
1725 pseries_lpar_register_process_table(0, 0, 0);
1728 #ifdef CONFIG_PPC_RADIX_MMU
1729 void radix_init_pseries(void)
1731 pr_info("Using radix MMU under hypervisor\n");
1733 pseries_lpar_register_process_table(__pa(process_tb),
1734 0, PRTB_SIZE_SHIFT - 12);
1738 #ifdef CONFIG_PPC_SMLPAR
1739 #define CMO_FREE_HINT_DEFAULT 1
1740 static int cmo_free_hint_flag = CMO_FREE_HINT_DEFAULT;
1742 static int __init cmo_free_hint(char *str)
1745 parm = strstrip(str);
1747 if (strcasecmp(parm, "no") == 0 || strcasecmp(parm, "off") == 0) {
1748 pr_info("%s: CMO free page hinting is not active.\n", __func__);
1749 cmo_free_hint_flag = 0;
1753 cmo_free_hint_flag = 1;
1754 pr_info("%s: CMO free page hinting is active.\n", __func__);
1756 if (strcasecmp(parm, "yes") == 0 || strcasecmp(parm, "on") == 0)
1762 __setup("cmo_free_hint=", cmo_free_hint);
1764 static void pSeries_set_page_state(struct page *page, int order,
1765 unsigned long state)
1768 unsigned long cmo_page_sz, addr;
1770 cmo_page_sz = cmo_get_page_size();
1771 addr = __pa((unsigned long)page_address(page));
1773 for (i = 0; i < (1 << order); i++, addr += PAGE_SIZE) {
1774 for (j = 0; j < PAGE_SIZE; j += cmo_page_sz)
1775 plpar_hcall_norets(H_PAGE_INIT, state, addr + j, 0);
1779 void arch_free_page(struct page *page, int order)
1781 if (radix_enabled())
1783 if (!cmo_free_hint_flag || !firmware_has_feature(FW_FEATURE_CMO))
1786 pSeries_set_page_state(page, order, H_PAGE_SET_UNUSED);
1788 EXPORT_SYMBOL(arch_free_page);
1790 #endif /* CONFIG_PPC_SMLPAR */
1791 #endif /* CONFIG_PPC_BOOK3S_64 */
1793 #ifdef CONFIG_TRACEPOINTS
1794 #ifdef CONFIG_JUMP_LABEL
1795 struct static_key hcall_tracepoint_key = STATIC_KEY_INIT;
1797 int hcall_tracepoint_regfunc(void)
1799 static_key_slow_inc(&hcall_tracepoint_key);
1803 void hcall_tracepoint_unregfunc(void)
1805 static_key_slow_dec(&hcall_tracepoint_key);
1809 * We optimise our hcall path by placing hcall_tracepoint_refcount
1810 * directly in the TOC so we can check if the hcall tracepoints are
1811 * enabled via a single load.
1814 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
1815 extern long hcall_tracepoint_refcount;
1817 int hcall_tracepoint_regfunc(void)
1819 hcall_tracepoint_refcount++;
1823 void hcall_tracepoint_unregfunc(void)
1825 hcall_tracepoint_refcount--;
1830 * Since the tracing code might execute hcalls we need to guard against
1831 * recursion. One example of this are spinlocks calling H_YIELD on
1832 * shared processor partitions.
1834 static DEFINE_PER_CPU(unsigned int, hcall_trace_depth);
1837 void __trace_hcall_entry(unsigned long opcode, unsigned long *args)
1839 unsigned long flags;
1840 unsigned int *depth;
1843 * We cannot call tracepoints inside RCU idle regions which
1844 * means we must not trace H_CEDE.
1846 if (opcode == H_CEDE)
1849 local_irq_save(flags);
1851 depth = this_cpu_ptr(&hcall_trace_depth);
1858 trace_hcall_entry(opcode, args);
1862 local_irq_restore(flags);
1865 void __trace_hcall_exit(long opcode, long retval, unsigned long *retbuf)
1867 unsigned long flags;
1868 unsigned int *depth;
1870 if (opcode == H_CEDE)
1873 local_irq_save(flags);
1875 depth = this_cpu_ptr(&hcall_trace_depth);
1881 trace_hcall_exit(opcode, retval, retbuf);
1886 local_irq_restore(flags);
1892 * H_GET_MPP hcall returns info in 7 parms
1894 int h_get_mpp(struct hvcall_mpp_data *mpp_data)
1897 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1899 rc = plpar_hcall9(H_GET_MPP, retbuf);
1901 mpp_data->entitled_mem = retbuf[0];
1902 mpp_data->mapped_mem = retbuf[1];
1904 mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
1905 mpp_data->pool_num = retbuf[2] & 0xffff;
1907 mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
1908 mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
1909 mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffffUL;
1911 mpp_data->pool_size = retbuf[4];
1912 mpp_data->loan_request = retbuf[5];
1913 mpp_data->backing_mem = retbuf[6];
1917 EXPORT_SYMBOL(h_get_mpp);
1919 int h_get_mpp_x(struct hvcall_mpp_x_data *mpp_x_data)
1922 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };
1924 rc = plpar_hcall9(H_GET_MPP_X, retbuf);
1926 mpp_x_data->coalesced_bytes = retbuf[0];
1927 mpp_x_data->pool_coalesced_bytes = retbuf[1];
1928 mpp_x_data->pool_purr_cycles = retbuf[2];
1929 mpp_x_data->pool_spurr_cycles = retbuf[3];
1934 static unsigned long vsid_unscramble(unsigned long vsid, int ssize)
1936 unsigned long protovsid;
1937 unsigned long va_bits = VA_BITS;
1938 unsigned long modinv, vsid_modulus;
1939 unsigned long max_mod_inv, tmp_modinv;
1941 if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
1944 if (ssize == MMU_SEGSIZE_256M) {
1945 modinv = VSID_MULINV_256M;
1946 vsid_modulus = ((1UL << (va_bits - SID_SHIFT)) - 1);
1948 modinv = VSID_MULINV_1T;
1949 vsid_modulus = ((1UL << (va_bits - SID_SHIFT_1T)) - 1);
1953 * vsid outside our range.
1955 if (vsid >= vsid_modulus)
1959 * If modinv is the modular multiplicate inverse of (x % vsid_modulus)
1960 * and vsid = (protovsid * x) % vsid_modulus, then we say:
1961 * protovsid = (vsid * modinv) % vsid_modulus
1964 /* Check if (vsid * modinv) overflow (63 bits) */
1965 max_mod_inv = 0x7fffffffffffffffull / vsid;
1966 if (modinv < max_mod_inv)
1967 return (vsid * modinv) % vsid_modulus;
1969 tmp_modinv = modinv/max_mod_inv;
1970 modinv %= max_mod_inv;
1972 protovsid = (((vsid * max_mod_inv) % vsid_modulus) * tmp_modinv) % vsid_modulus;
1973 protovsid = (protovsid + vsid * modinv) % vsid_modulus;
1978 static int __init reserve_vrma_context_id(void)
1980 unsigned long protovsid;
1983 * Reserve context ids which map to reserved virtual addresses. For now
1984 * we only reserve the context id which maps to the VRMA VSID. We ignore
1985 * the addresses in "ibm,adjunct-virtual-addresses" because we don't
1986 * enable adjunct support via the "ibm,client-architecture-support"
1989 protovsid = vsid_unscramble(VRMA_VSID, MMU_SEGSIZE_1T);
1990 hash__reserve_context_id(protovsid >> ESID_BITS_1T);
1993 machine_device_initcall(pseries, reserve_vrma_context_id);
1995 #ifdef CONFIG_DEBUG_FS
1996 /* debugfs file interface for vpa data */
1997 static ssize_t vpa_file_read(struct file *filp, char __user *buf, size_t len,
2000 int cpu = (long)filp->private_data;
2001 struct lppaca *lppaca = &lppaca_of(cpu);
2003 return simple_read_from_buffer(buf, len, pos, lppaca,
2004 sizeof(struct lppaca));
2007 static const struct file_operations vpa_fops = {
2008 .open = simple_open,
2009 .read = vpa_file_read,
2010 .llseek = default_llseek,
2013 static int __init vpa_debugfs_init(void)
2017 struct dentry *vpa_dir;
2019 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
2022 vpa_dir = debugfs_create_dir("vpa", powerpc_debugfs_root);
2024 /* set up the per-cpu vpa file*/
2025 for_each_possible_cpu(i) {
2026 sprintf(name, "cpu-%ld", i);
2027 debugfs_create_file(name, 0400, vpa_dir, (void *)i, &vpa_fops);
2032 machine_arch_initcall(pseries, vpa_debugfs_init);
2033 #endif /* CONFIG_DEBUG_FS */