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_relative_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_relative_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();
527 destroy_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);
656 #endif /* CONFIG_PPC_SPLPAR */
658 void vpa_init(int cpu)
660 int hwcpu = get_hard_smp_processor_id(cpu);
665 * The spec says it "may be problematic" if CPU x registers the VPA of
666 * CPU y. We should never do that, but wail if we ever do.
668 WARN_ON(cpu != smp_processor_id());
670 if (cpu_has_feature(CPU_FTR_ALTIVEC))
671 lppaca_of(cpu).vmxregs_in_use = 1;
673 if (cpu_has_feature(CPU_FTR_ARCH_207S))
674 lppaca_of(cpu).ebb_regs_in_use = 1;
676 addr = __pa(&lppaca_of(cpu));
677 ret = register_vpa(hwcpu, addr);
680 pr_err("WARNING: VPA registration for cpu %d (hw %d) of area "
681 "%lx failed with %ld\n", cpu, hwcpu, addr, ret);
685 #ifdef CONFIG_PPC_BOOK3S_64
687 * PAPR says this feature is SLB-Buffer but firmware never
688 * reports that. All SPLPAR support SLB shadow buffer.
690 if (!radix_enabled() && firmware_has_feature(FW_FEATURE_SPLPAR)) {
691 addr = __pa(paca_ptrs[cpu]->slb_shadow_ptr);
692 ret = register_slb_shadow(hwcpu, addr);
694 pr_err("WARNING: SLB shadow buffer registration for "
695 "cpu %d (hw %d) of area %lx failed with %ld\n",
696 cpu, hwcpu, addr, ret);
698 #endif /* CONFIG_PPC_BOOK3S_64 */
701 * Register dispatch trace log, if one has been allocated.
703 register_dtl_buffer(cpu);
706 #ifdef CONFIG_PPC_BOOK3S_64
708 static long pSeries_lpar_hpte_insert(unsigned long hpte_group,
709 unsigned long vpn, unsigned long pa,
710 unsigned long rflags, unsigned long vflags,
711 int psize, int apsize, int ssize)
713 unsigned long lpar_rc;
716 unsigned long hpte_v, hpte_r;
718 if (!(vflags & HPTE_V_BOLTED))
719 pr_devel("hpte_insert(group=%lx, vpn=%016lx, "
720 "pa=%016lx, rflags=%lx, vflags=%lx, psize=%d)\n",
721 hpte_group, vpn, pa, rflags, vflags, psize);
723 hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
724 hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
726 if (!(vflags & HPTE_V_BOLTED))
727 pr_devel(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
729 /* Now fill in the actual HPTE */
730 /* Set CEC cookie to 0 */
732 /* I-cache Invalidate = 0 */
733 /* I-cache synchronize = 0 */
737 if (firmware_has_feature(FW_FEATURE_XCMO) && !(hpte_r & HPTE_R_N))
738 flags |= H_COALESCE_CAND;
740 lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot);
741 if (unlikely(lpar_rc == H_PTEG_FULL)) {
742 pr_devel("Hash table group is full\n");
747 * Since we try and ioremap PHBs we don't own, the pte insert
748 * will fail. However we must catch the failure in hash_page
749 * or we will loop forever, so return -2 in this case.
751 if (unlikely(lpar_rc != H_SUCCESS)) {
752 pr_err("Failed hash pte insert with error %ld\n", lpar_rc);
755 if (!(vflags & HPTE_V_BOLTED))
756 pr_devel(" -> slot: %lu\n", slot & 7);
758 /* Because of iSeries, we have to pass down the secondary
759 * bucket bit here as well
761 return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);
764 static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
766 static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
768 unsigned long slot_offset;
769 unsigned long lpar_rc;
771 unsigned long dummy1, dummy2;
773 /* pick a random slot to start at */
774 slot_offset = mftb() & 0x7;
776 for (i = 0; i < HPTES_PER_GROUP; i++) {
778 /* don't remove a bolted entry */
779 lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
780 HPTE_V_BOLTED, &dummy1, &dummy2);
781 if (lpar_rc == H_SUCCESS)
785 * The test for adjunct partition is performed before the
786 * ANDCOND test. H_RESOURCE may be returned, so we need to
787 * check for that as well.
789 BUG_ON(lpar_rc != H_NOT_FOUND && lpar_rc != H_RESOURCE);
798 static void manual_hpte_clear_all(void)
800 unsigned long size_bytes = 1UL << ppc64_pft_size;
801 unsigned long hpte_count = size_bytes >> 4;
809 /* Read in batches of 4,
810 * invalidate only valid entries not in the VRMA
811 * hpte_count will be a multiple of 4
813 for (i = 0; i < hpte_count; i += 4) {
814 lpar_rc = plpar_pte_read_4_raw(0, i, (void *)ptes);
815 if (lpar_rc != H_SUCCESS) {
816 pr_info("Failed to read hash page table at %ld err %ld\n",
820 for (j = 0; j < 4; j++){
821 if ((ptes[j].pteh & HPTE_V_VRMA_MASK) ==
824 if (ptes[j].pteh & HPTE_V_VALID)
825 plpar_pte_remove_raw(0, i + j, 0,
826 &(ptes[j].pteh), &(ptes[j].ptel));
831 static int hcall_hpte_clear_all(void)
836 rc = plpar_hcall_norets(H_CLEAR_HPT);
837 } while (rc == H_CONTINUE);
842 static void pseries_hpte_clear_all(void)
846 rc = hcall_hpte_clear_all();
848 manual_hpte_clear_all();
850 #ifdef __LITTLE_ENDIAN__
852 * Reset exceptions to big endian.
854 * FIXME this is a hack for kexec, we need to reset the exception
855 * endian before starting the new kernel and this is a convenient place
858 * This is also called on boot when a fadump happens. In that case we
859 * must not change the exception endian mode.
861 if (firmware_has_feature(FW_FEATURE_SET_MODE) && !is_fadump_active())
862 pseries_big_endian_exceptions();
867 * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
868 * the low 3 bits of flags happen to line up. So no transform is needed.
869 * We can probably optimize here and assume the high bits of newpp are
870 * already zero. For now I am paranoid.
872 static long pSeries_lpar_hpte_updatepp(unsigned long slot,
875 int psize, int apsize,
876 int ssize, unsigned long inv_flags)
878 unsigned long lpar_rc;
880 unsigned long want_v;
882 want_v = hpte_encode_avpn(vpn, psize, ssize);
884 flags = (newpp & 7) | H_AVPN;
885 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
886 /* Move pp0 into bit 8 (IBM 55) */
887 flags |= (newpp & HPTE_R_PP0) >> 55;
889 pr_devel(" update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...",
890 want_v, slot, flags, psize);
892 lpar_rc = plpar_pte_protect(flags, slot, want_v);
894 if (lpar_rc == H_NOT_FOUND) {
895 pr_devel("not found !\n");
901 BUG_ON(lpar_rc != H_SUCCESS);
906 static long __pSeries_lpar_hpte_find(unsigned long want_v, unsigned long hpte_group)
915 for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
917 lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
918 if (lpar_rc != H_SUCCESS) {
919 pr_info("Failed to read hash page table at %ld err %ld\n",
920 hpte_group, lpar_rc);
924 for (j = 0; j < 4; j++) {
925 if (HPTE_V_COMPARE(ptes[j].pteh, want_v) &&
926 (ptes[j].pteh & HPTE_V_VALID))
934 static long pSeries_lpar_hpte_find(unsigned long vpn, int psize, int ssize)
938 unsigned long want_v;
939 unsigned long hpte_group;
941 hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
942 want_v = hpte_encode_avpn(vpn, psize, ssize);
945 * We try to keep bolted entries always in primary hash
946 * But in some case we can find them in secondary too.
948 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
949 slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
951 /* Try in secondary */
952 hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
953 slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
957 return hpte_group + slot;
960 static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
962 int psize, int ssize)
965 unsigned long lpar_rc, slot, vsid, flags;
967 vsid = get_kernel_vsid(ea, ssize);
968 vpn = hpt_vpn(ea, vsid, ssize);
970 slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
974 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
975 /* Move pp0 into bit 8 (IBM 55) */
976 flags |= (newpp & HPTE_R_PP0) >> 55;
978 lpar_rc = plpar_pte_protect(flags, slot, 0);
980 BUG_ON(lpar_rc != H_SUCCESS);
983 static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn,
984 int psize, int apsize,
985 int ssize, int local)
987 unsigned long want_v;
988 unsigned long lpar_rc;
989 unsigned long dummy1, dummy2;
991 pr_devel(" inval : slot=%lx, vpn=%016lx, psize: %d, local: %d\n",
992 slot, vpn, psize, local);
994 want_v = hpte_encode_avpn(vpn, psize, ssize);
995 lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2);
996 if (lpar_rc == H_NOT_FOUND)
999 BUG_ON(lpar_rc != H_SUCCESS);
1004 * As defined in the PAPR's section 14.5.4.1.8
1005 * The control mask doesn't include the returned reference and change bit from
1006 * the processed PTE.
1008 #define HBLKR_AVPN 0x0100000000000000UL
1009 #define HBLKR_CTRL_MASK 0xf800000000000000UL
1010 #define HBLKR_CTRL_SUCCESS 0x8000000000000000UL
1011 #define HBLKR_CTRL_ERRNOTFOUND 0x8800000000000000UL
1012 #define HBLKR_CTRL_ERRBUSY 0xa000000000000000UL
1015 * Returned true if we are supporting this block size for the specified segment
1016 * base page size and actual page size.
1018 * Currently, we only support 8 size block.
1020 static inline bool is_supported_hlbkrm(int bpsize, int psize)
1022 return (hblkrm_size[bpsize][psize] == HBLKRM_SUPPORTED_BLOCK_SIZE);
1026 * H_BLOCK_REMOVE caller.
1027 * @idx should point to the latest @param entry set with a PTEX.
1028 * If PTE cannot be processed because another CPUs has already locked that
1029 * group, those entries are put back in @param starting at index 1.
1030 * If entries has to be retried and @retry_busy is set to true, these entries
1031 * are retried until success. If @retry_busy is set to false, the returned
1032 * is the number of entries yet to process.
1034 static unsigned long call_block_remove(unsigned long idx, unsigned long *param,
1037 unsigned long i, rc, new_idx;
1038 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1041 pr_warn("Unexpected empty call to H_BLOCK_REMOVE");
1046 if (idx > PLPAR_HCALL9_BUFSIZE) {
1047 pr_err("Too many PTEs (%lu) for H_BLOCK_REMOVE", idx);
1048 idx = PLPAR_HCALL9_BUFSIZE;
1049 } else if (idx < PLPAR_HCALL9_BUFSIZE)
1050 param[idx] = HBR_END;
1052 rc = plpar_hcall9(H_BLOCK_REMOVE, retbuf,
1054 param[1], param[2], param[3], param[4], /* TS0-7 */
1055 param[5], param[6], param[7], param[8]);
1056 if (rc == H_SUCCESS)
1059 BUG_ON(rc != H_PARTIAL);
1061 /* Check that the unprocessed entries were 'not found' or 'busy' */
1062 for (i = 0; i < idx-1; i++) {
1063 unsigned long ctrl = retbuf[i] & HBLKR_CTRL_MASK;
1065 if (ctrl == HBLKR_CTRL_ERRBUSY) {
1066 param[++new_idx] = param[i+1];
1070 BUG_ON(ctrl != HBLKR_CTRL_SUCCESS
1071 && ctrl != HBLKR_CTRL_ERRNOTFOUND);
1075 * If there were entries found busy, retry these entries if requested,
1076 * of if all the entries have to be retried.
1078 if (new_idx && (retry_busy || new_idx == (PLPAR_HCALL9_BUFSIZE-1))) {
1086 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1088 * Limit iterations holding pSeries_lpar_tlbie_lock to 3. We also need
1089 * to make sure that we avoid bouncing the hypervisor tlbie lock.
1091 #define PPC64_HUGE_HPTE_BATCH 12
1093 static void hugepage_block_invalidate(unsigned long *slot, unsigned long *vpn,
1094 int count, int psize, int ssize)
1096 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1097 unsigned long shift, current_vpgb, vpgb;
1100 shift = mmu_psize_defs[psize].shift;
1102 for (i = 0; i < count; i++) {
1104 * Shifting 3 bits more on the right to get a
1105 * 8 pages aligned virtual addresse.
1107 vpgb = (vpn[i] >> (shift - VPN_SHIFT + 3));
1108 if (!pix || vpgb != current_vpgb) {
1110 * Need to start a new 8 pages block, flush
1111 * the current one if needed.
1114 (void)call_block_remove(pix, param, true);
1115 current_vpgb = vpgb;
1116 param[0] = hpte_encode_avpn(vpn[i], psize, ssize);
1120 param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot[i];
1121 if (pix == PLPAR_HCALL9_BUFSIZE) {
1122 pix = call_block_remove(pix, param, false);
1124 * pix = 0 means that all the entries were
1125 * removed, we can start a new block.
1126 * Otherwise, this means that there are entries
1127 * to retry, and pix points to latest one, so
1128 * we should increment it and try to continue
1136 (void)call_block_remove(pix, param, true);
1139 static void hugepage_bulk_invalidate(unsigned long *slot, unsigned long *vpn,
1140 int count, int psize, int ssize)
1142 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1143 int i = 0, pix = 0, rc;
1145 for (i = 0; i < count; i++) {
1147 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1148 pSeries_lpar_hpte_invalidate(slot[i], vpn[i], psize, 0,
1151 param[pix] = HBR_REQUEST | HBR_AVPN | slot[i];
1152 param[pix+1] = hpte_encode_avpn(vpn[i], psize, ssize);
1155 rc = plpar_hcall9(H_BULK_REMOVE, param,
1156 param[0], param[1], param[2],
1157 param[3], param[4], param[5],
1158 param[6], param[7]);
1159 BUG_ON(rc != H_SUCCESS);
1165 param[pix] = HBR_END;
1166 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1167 param[2], param[3], param[4], param[5],
1168 param[6], param[7]);
1169 BUG_ON(rc != H_SUCCESS);
1173 static inline void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
1175 int count, int psize,
1178 unsigned long flags = 0;
1179 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1182 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1184 /* Assuming THP size is 16M */
1185 if (is_supported_hlbkrm(psize, MMU_PAGE_16M))
1186 hugepage_block_invalidate(slot, vpn, count, psize, ssize);
1188 hugepage_bulk_invalidate(slot, vpn, count, psize, ssize);
1191 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1194 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1196 unsigned char *hpte_slot_array,
1197 int psize, int ssize, int local)
1200 unsigned long s_addr = addr;
1201 unsigned int max_hpte_count, valid;
1202 unsigned long vpn_array[PPC64_HUGE_HPTE_BATCH];
1203 unsigned long slot_array[PPC64_HUGE_HPTE_BATCH];
1204 unsigned long shift, hidx, vpn = 0, hash, slot;
1206 shift = mmu_psize_defs[psize].shift;
1207 max_hpte_count = 1U << (PMD_SHIFT - shift);
1209 for (i = 0; i < max_hpte_count; i++) {
1210 valid = hpte_valid(hpte_slot_array, i);
1213 hidx = hpte_hash_index(hpte_slot_array, i);
1216 addr = s_addr + (i * (1ul << shift));
1217 vpn = hpt_vpn(addr, vsid, ssize);
1218 hash = hpt_hash(vpn, shift, ssize);
1219 if (hidx & _PTEIDX_SECONDARY)
1222 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1223 slot += hidx & _PTEIDX_GROUP_IX;
1225 slot_array[index] = slot;
1226 vpn_array[index] = vpn;
1227 if (index == PPC64_HUGE_HPTE_BATCH - 1) {
1229 * Now do a bluk invalidate
1231 __pSeries_lpar_hugepage_invalidate(slot_array,
1233 PPC64_HUGE_HPTE_BATCH,
1240 __pSeries_lpar_hugepage_invalidate(slot_array, vpn_array,
1241 index, psize, ssize);
1244 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1246 unsigned char *hpte_slot_array,
1247 int psize, int ssize, int local)
1249 WARN(1, "%s called without THP support\n", __func__);
1253 static int pSeries_lpar_hpte_removebolted(unsigned long ea,
1254 int psize, int ssize)
1257 unsigned long slot, vsid;
1259 vsid = get_kernel_vsid(ea, ssize);
1260 vpn = hpt_vpn(ea, vsid, ssize);
1262 slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
1267 * lpar doesn't use the passed actual page size
1269 pSeries_lpar_hpte_invalidate(slot, vpn, psize, 0, ssize, 0);
1274 static inline unsigned long compute_slot(real_pte_t pte,
1276 unsigned long index,
1277 unsigned long shift,
1280 unsigned long slot, hash, hidx;
1282 hash = hpt_hash(vpn, shift, ssize);
1283 hidx = __rpte_to_hidx(pte, index);
1284 if (hidx & _PTEIDX_SECONDARY)
1286 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1287 slot += hidx & _PTEIDX_GROUP_IX;
1292 * The hcall H_BLOCK_REMOVE implies that the virtual pages to processed are
1293 * "all within the same naturally aligned 8 page virtual address block".
1295 static void do_block_remove(unsigned long number, struct ppc64_tlb_batch *batch,
1296 unsigned long *param)
1299 unsigned long i, pix = 0;
1300 unsigned long index, shift, slot, current_vpgb, vpgb;
1304 psize = batch->psize;
1305 ssize = batch->ssize;
1307 for (i = 0; i < number; i++) {
1308 vpn = batch->vpn[i];
1309 pte = batch->pte[i];
1310 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1312 * Shifting 3 bits more on the right to get a
1313 * 8 pages aligned virtual addresse.
1315 vpgb = (vpn >> (shift - VPN_SHIFT + 3));
1316 if (!pix || vpgb != current_vpgb) {
1318 * Need to start a new 8 pages block, flush
1319 * the current one if needed.
1322 (void)call_block_remove(pix, param,
1324 current_vpgb = vpgb;
1325 param[0] = hpte_encode_avpn(vpn, psize,
1330 slot = compute_slot(pte, vpn, index, shift, ssize);
1331 param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot;
1333 if (pix == PLPAR_HCALL9_BUFSIZE) {
1334 pix = call_block_remove(pix, param, false);
1336 * pix = 0 means that all the entries were
1337 * removed, we can start a new block.
1338 * Otherwise, this means that there are entries
1339 * to retry, and pix points to latest one, so
1340 * we should increment it and try to continue
1346 } pte_iterate_hashed_end();
1350 (void)call_block_remove(pix, param, true);
1354 * TLB Block Invalidate Characteristics
1356 * These characteristics define the size of the block the hcall H_BLOCK_REMOVE
1357 * is able to process for each couple segment base page size, actual page size.
1359 * The ibm,get-system-parameter properties is returning a buffer with the
1362 * [ 2 bytes size of the RTAS buffer (excluding these 2 bytes) ]
1364 * TLB Block Invalidate Specifiers:
1365 * [ 1 byte LOG base 2 of the TLB invalidate block size being specified ]
1366 * [ 1 byte Number of page sizes (N) that are supported for the specified
1367 * TLB invalidate block size ]
1368 * [ 1 byte Encoded segment base page size and actual page size
1369 * MSB=0 means 4k segment base page size and actual page size
1370 * MSB=1 the penc value in mmu_psize_def ]
1373 * Next TLB Block Invalidate Specifiers...
1377 static inline void set_hblkrm_bloc_size(int bpsize, int psize,
1378 unsigned int block_size)
1380 if (block_size > hblkrm_size[bpsize][psize])
1381 hblkrm_size[bpsize][psize] = block_size;
1385 * Decode the Encoded segment base page size and actual page size.
1387 * - bit 7 is the L bit
1388 * - bits 0-5 are the penc value
1389 * If the L bit is 0, this means 4K segment base page size and actual page size
1390 * otherwise the penc value should be read.
1392 #define HBLKRM_L_MASK 0x80
1393 #define HBLKRM_PENC_MASK 0x3f
1394 static inline void __init check_lp_set_hblkrm(unsigned int lp,
1395 unsigned int block_size)
1397 unsigned int bpsize, psize;
1399 /* First, check the L bit, if not set, this means 4K */
1400 if ((lp & HBLKRM_L_MASK) == 0) {
1401 set_hblkrm_bloc_size(MMU_PAGE_4K, MMU_PAGE_4K, block_size);
1405 lp &= HBLKRM_PENC_MASK;
1406 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++) {
1407 struct mmu_psize_def *def = &mmu_psize_defs[bpsize];
1409 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1410 if (def->penc[psize] == lp) {
1411 set_hblkrm_bloc_size(bpsize, psize, block_size);
1418 #define SPLPAR_TLB_BIC_TOKEN 50
1421 * The size of the TLB Block Invalidate Characteristics is variable. But at the
1422 * maximum it will be the number of possible page sizes *2 + 10 bytes.
1423 * Currently MMU_PAGE_COUNT is 16, which means 42 bytes. Use a cache line size
1424 * (128 bytes) for the buffer to get plenty of space.
1426 #define SPLPAR_TLB_BIC_MAXLENGTH 128
1428 void __init pseries_lpar_read_hblkrm_characteristics(void)
1430 const s32 token = rtas_token("ibm,get-system-parameter");
1431 unsigned char local_buffer[SPLPAR_TLB_BIC_MAXLENGTH];
1432 int call_status, len, idx, bpsize;
1434 if (!firmware_has_feature(FW_FEATURE_BLOCK_REMOVE))
1438 spin_lock(&rtas_data_buf_lock);
1439 memset(rtas_data_buf, 0, RTAS_DATA_BUF_SIZE);
1440 call_status = rtas_call(token, 3, 1, NULL, SPLPAR_TLB_BIC_TOKEN,
1441 __pa(rtas_data_buf), RTAS_DATA_BUF_SIZE);
1442 memcpy(local_buffer, rtas_data_buf, SPLPAR_TLB_BIC_MAXLENGTH);
1443 local_buffer[SPLPAR_TLB_BIC_MAXLENGTH - 1] = '\0';
1444 spin_unlock(&rtas_data_buf_lock);
1445 } while (rtas_busy_delay(call_status));
1447 if (call_status != 0) {
1448 pr_warn("%s %s Error calling get-system-parameter (0x%x)\n",
1449 __FILE__, __func__, call_status);
1454 * The first two (2) bytes of the data in the buffer are the length of
1455 * the returned data, not counting these first two (2) bytes.
1457 len = be16_to_cpu(*((u16 *)local_buffer)) + 2;
1458 if (len > SPLPAR_TLB_BIC_MAXLENGTH) {
1459 pr_warn("%s too large returned buffer %d", __func__, len);
1465 u8 block_shift = local_buffer[idx++];
1467 unsigned int npsize;
1472 block_size = 1 << block_shift;
1474 for (npsize = local_buffer[idx++];
1475 npsize > 0 && idx < len; npsize--)
1476 check_lp_set_hblkrm((unsigned int) local_buffer[idx++],
1480 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
1481 for (idx = 0; idx < MMU_PAGE_COUNT; idx++)
1482 if (hblkrm_size[bpsize][idx])
1483 pr_info("H_BLOCK_REMOVE supports base psize:%d psize:%d block size:%d",
1484 bpsize, idx, hblkrm_size[bpsize][idx]);
1488 * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
1491 static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
1494 unsigned long i, pix, rc;
1495 unsigned long flags = 0;
1496 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
1497 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1498 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1499 unsigned long index, shift, slot;
1504 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1506 if (is_supported_hlbkrm(batch->psize, batch->psize)) {
1507 do_block_remove(number, batch, param);
1511 psize = batch->psize;
1512 ssize = batch->ssize;
1514 for (i = 0; i < number; i++) {
1515 vpn = batch->vpn[i];
1516 pte = batch->pte[i];
1517 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1518 slot = compute_slot(pte, vpn, index, shift, ssize);
1519 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1521 * lpar doesn't use the passed actual page size
1523 pSeries_lpar_hpte_invalidate(slot, vpn, psize,
1526 param[pix] = HBR_REQUEST | HBR_AVPN | slot;
1527 param[pix+1] = hpte_encode_avpn(vpn, psize,
1531 rc = plpar_hcall9(H_BULK_REMOVE, param,
1532 param[0], param[1], param[2],
1533 param[3], param[4], param[5],
1534 param[6], param[7]);
1535 BUG_ON(rc != H_SUCCESS);
1539 } pte_iterate_hashed_end();
1542 param[pix] = HBR_END;
1543 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1544 param[2], param[3], param[4], param[5],
1545 param[6], param[7]);
1546 BUG_ON(rc != H_SUCCESS);
1551 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1554 static int __init disable_bulk_remove(char *str)
1556 if (strcmp(str, "off") == 0 &&
1557 firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1558 pr_info("Disabling BULK_REMOVE firmware feature");
1559 powerpc_firmware_features &= ~FW_FEATURE_BULK_REMOVE;
1564 __setup("bulk_remove=", disable_bulk_remove);
1566 #define HPT_RESIZE_TIMEOUT 10000 /* ms */
1568 struct hpt_resize_state {
1569 unsigned long shift;
1573 static int pseries_lpar_resize_hpt_commit(void *data)
1575 struct hpt_resize_state *state = data;
1577 state->commit_rc = plpar_resize_hpt_commit(0, state->shift);
1578 if (state->commit_rc != H_SUCCESS)
1581 /* Hypervisor has transitioned the HTAB, update our globals */
1582 ppc64_pft_size = state->shift;
1583 htab_size_bytes = 1UL << ppc64_pft_size;
1584 htab_hash_mask = (htab_size_bytes >> 7) - 1;
1590 * Must be called in process context. The caller must hold the
1593 static int pseries_lpar_resize_hpt(unsigned long shift)
1595 struct hpt_resize_state state = {
1597 .commit_rc = H_FUNCTION,
1599 unsigned int delay, total_delay = 0;
1605 if (!firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1608 pr_info("Attempting to resize HPT to shift %lu\n", shift);
1612 rc = plpar_resize_hpt_prepare(0, shift);
1613 while (H_IS_LONG_BUSY(rc)) {
1614 delay = get_longbusy_msecs(rc);
1615 total_delay += delay;
1616 if (total_delay > HPT_RESIZE_TIMEOUT) {
1617 /* prepare with shift==0 cancels an in-progress resize */
1618 rc = plpar_resize_hpt_prepare(0, 0);
1619 if (rc != H_SUCCESS)
1620 pr_warn("Unexpected error %d cancelling timed out HPT resize\n",
1625 rc = plpar_resize_hpt_prepare(0, shift);
1634 pr_warn("Invalid argument from H_RESIZE_HPT_PREPARE\n");
1637 pr_warn("Operation not permitted from H_RESIZE_HPT_PREPARE\n");
1640 pr_warn("Unexpected error %d from H_RESIZE_HPT_PREPARE\n", rc);
1646 rc = stop_machine_cpuslocked(pseries_lpar_resize_hpt_commit,
1652 switch (state.commit_rc) {
1657 pr_warn("Unexpected error %d from H_RESIZE_HPT_COMMIT\n",
1663 pr_info("HPT resize to shift %lu complete (%lld ms / %lld ms)\n",
1664 shift, (long long) ktime_ms_delta(t1, t0),
1665 (long long) ktime_ms_delta(t2, t1));
1670 static int pseries_lpar_register_process_table(unsigned long base,
1671 unsigned long page_size, unsigned long table_size)
1674 unsigned long flags = 0;
1677 flags |= PROC_TABLE_NEW;
1678 if (radix_enabled()) {
1679 flags |= PROC_TABLE_RADIX;
1680 if (mmu_has_feature(MMU_FTR_GTSE))
1681 flags |= PROC_TABLE_GTSE;
1683 flags |= PROC_TABLE_HPT_SLB;
1685 rc = plpar_hcall_norets(H_REGISTER_PROC_TBL, flags, base,
1686 page_size, table_size);
1687 if (!H_IS_LONG_BUSY(rc))
1689 mdelay(get_longbusy_msecs(rc));
1691 if (rc != H_SUCCESS) {
1692 pr_err("Failed to register process table (rc=%ld)\n", rc);
1698 void __init hpte_init_pseries(void)
1700 mmu_hash_ops.hpte_invalidate = pSeries_lpar_hpte_invalidate;
1701 mmu_hash_ops.hpte_updatepp = pSeries_lpar_hpte_updatepp;
1702 mmu_hash_ops.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
1703 mmu_hash_ops.hpte_insert = pSeries_lpar_hpte_insert;
1704 mmu_hash_ops.hpte_remove = pSeries_lpar_hpte_remove;
1705 mmu_hash_ops.hpte_removebolted = pSeries_lpar_hpte_removebolted;
1706 mmu_hash_ops.flush_hash_range = pSeries_lpar_flush_hash_range;
1707 mmu_hash_ops.hpte_clear_all = pseries_hpte_clear_all;
1708 mmu_hash_ops.hugepage_invalidate = pSeries_lpar_hugepage_invalidate;
1710 if (firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1711 mmu_hash_ops.resize_hpt = pseries_lpar_resize_hpt;
1714 * On POWER9, we need to do a H_REGISTER_PROC_TBL hcall
1715 * to inform the hypervisor that we wish to use the HPT.
1717 if (cpu_has_feature(CPU_FTR_ARCH_300))
1718 pseries_lpar_register_process_table(0, 0, 0);
1721 #ifdef CONFIG_PPC_RADIX_MMU
1722 void 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 * Since the tracing code might execute hcalls we need to guard against
1826 static DEFINE_PER_CPU(unsigned int, hcall_trace_depth);
1829 void __trace_hcall_entry(unsigned long opcode, unsigned long *args)
1831 unsigned long flags;
1832 unsigned int *depth;
1835 * We cannot call tracepoints inside RCU idle regions which
1836 * means we must not trace H_CEDE.
1838 if (opcode == H_CEDE)
1841 local_irq_save(flags);
1843 depth = this_cpu_ptr(&hcall_trace_depth);
1850 trace_hcall_entry(opcode, args);
1854 local_irq_restore(flags);
1857 void __trace_hcall_exit(long opcode, long retval, unsigned long *retbuf)
1859 unsigned long flags;
1860 unsigned int *depth;
1862 if (opcode == H_CEDE)
1865 local_irq_save(flags);
1867 depth = this_cpu_ptr(&hcall_trace_depth);
1873 trace_hcall_exit(opcode, retval, retbuf);
1878 local_irq_restore(flags);
1884 * H_GET_MPP hcall returns info in 7 parms
1886 int h_get_mpp(struct hvcall_mpp_data *mpp_data)
1889 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1891 rc = plpar_hcall9(H_GET_MPP, retbuf);
1893 mpp_data->entitled_mem = retbuf[0];
1894 mpp_data->mapped_mem = retbuf[1];
1896 mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
1897 mpp_data->pool_num = retbuf[2] & 0xffff;
1899 mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
1900 mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
1901 mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffffUL;
1903 mpp_data->pool_size = retbuf[4];
1904 mpp_data->loan_request = retbuf[5];
1905 mpp_data->backing_mem = retbuf[6];
1909 EXPORT_SYMBOL(h_get_mpp);
1911 int h_get_mpp_x(struct hvcall_mpp_x_data *mpp_x_data)
1914 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };
1916 rc = plpar_hcall9(H_GET_MPP_X, retbuf);
1918 mpp_x_data->coalesced_bytes = retbuf[0];
1919 mpp_x_data->pool_coalesced_bytes = retbuf[1];
1920 mpp_x_data->pool_purr_cycles = retbuf[2];
1921 mpp_x_data->pool_spurr_cycles = retbuf[3];
1926 static unsigned long vsid_unscramble(unsigned long vsid, int ssize)
1928 unsigned long protovsid;
1929 unsigned long va_bits = VA_BITS;
1930 unsigned long modinv, vsid_modulus;
1931 unsigned long max_mod_inv, tmp_modinv;
1933 if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
1936 if (ssize == MMU_SEGSIZE_256M) {
1937 modinv = VSID_MULINV_256M;
1938 vsid_modulus = ((1UL << (va_bits - SID_SHIFT)) - 1);
1940 modinv = VSID_MULINV_1T;
1941 vsid_modulus = ((1UL << (va_bits - SID_SHIFT_1T)) - 1);
1945 * vsid outside our range.
1947 if (vsid >= vsid_modulus)
1951 * If modinv is the modular multiplicate inverse of (x % vsid_modulus)
1952 * and vsid = (protovsid * x) % vsid_modulus, then we say:
1953 * protovsid = (vsid * modinv) % vsid_modulus
1956 /* Check if (vsid * modinv) overflow (63 bits) */
1957 max_mod_inv = 0x7fffffffffffffffull / vsid;
1958 if (modinv < max_mod_inv)
1959 return (vsid * modinv) % vsid_modulus;
1961 tmp_modinv = modinv/max_mod_inv;
1962 modinv %= max_mod_inv;
1964 protovsid = (((vsid * max_mod_inv) % vsid_modulus) * tmp_modinv) % vsid_modulus;
1965 protovsid = (protovsid + vsid * modinv) % vsid_modulus;
1970 static int __init reserve_vrma_context_id(void)
1972 unsigned long protovsid;
1975 * Reserve context ids which map to reserved virtual addresses. For now
1976 * we only reserve the context id which maps to the VRMA VSID. We ignore
1977 * the addresses in "ibm,adjunct-virtual-addresses" because we don't
1978 * enable adjunct support via the "ibm,client-architecture-support"
1981 protovsid = vsid_unscramble(VRMA_VSID, MMU_SEGSIZE_1T);
1982 hash__reserve_context_id(protovsid >> ESID_BITS_1T);
1985 machine_device_initcall(pseries, reserve_vrma_context_id);
1987 #ifdef CONFIG_DEBUG_FS
1988 /* debugfs file interface for vpa data */
1989 static ssize_t vpa_file_read(struct file *filp, char __user *buf, size_t len,
1992 int cpu = (long)filp->private_data;
1993 struct lppaca *lppaca = &lppaca_of(cpu);
1995 return simple_read_from_buffer(buf, len, pos, lppaca,
1996 sizeof(struct lppaca));
1999 static const struct file_operations vpa_fops = {
2000 .open = simple_open,
2001 .read = vpa_file_read,
2002 .llseek = default_llseek,
2005 static int __init vpa_debugfs_init(void)
2009 struct dentry *vpa_dir;
2011 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
2014 vpa_dir = debugfs_create_dir("vpa", powerpc_debugfs_root);
2016 /* set up the per-cpu vpa file*/
2017 for_each_possible_cpu(i) {
2018 sprintf(name, "cpu-%ld", i);
2019 debugfs_create_file(name, 0400, vpa_dir, (void *)i, &vpa_fops);
2024 machine_arch_initcall(pseries, vpa_debugfs_init);
2025 #endif /* CONFIG_DEBUG_FS */