Path: blob/master/arch/powerpc/platforms/pseries/lpar.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* pSeries_lpar.c3* Copyright (C) 2001 Todd Inglett, IBM Corporation4*5* pSeries LPAR support.6*/78/* Enables debugging of low-level hash table routines - careful! */9#undef DEBUG10#define pr_fmt(fmt) "lpar: " fmt1112#include <linux/kernel.h>13#include <linux/dma-mapping.h>14#include <linux/console.h>15#include <linux/export.h>16#include <linux/jump_label.h>17#include <linux/delay.h>18#include <linux/seq_file.h>19#include <linux/stop_machine.h>20#include <linux/spinlock.h>21#include <linux/cpuhotplug.h>22#include <linux/workqueue.h>23#include <linux/proc_fs.h>24#include <linux/pgtable.h>25#include <linux/debugfs.h>2627#include <asm/processor.h>28#include <asm/mmu.h>29#include <asm/page.h>30#include <asm/setup.h>31#include <asm/mmu_context.h>32#include <asm/iommu.h>33#include <asm/tlb.h>34#include <asm/cputable.h>35#include <asm/papr-sysparm.h>36#include <asm/udbg.h>37#include <asm/smp.h>38#include <asm/trace.h>39#include <asm/firmware.h>40#include <asm/plpar_wrappers.h>41#include <asm/kexec.h>42#include <asm/fadump.h>43#include <asm/dtl.h>44#include <asm/vphn.h>4546#include "pseries.h"4748/* Flag bits for H_BULK_REMOVE */49#define HBR_REQUEST 0x4000000000000000UL50#define HBR_RESPONSE 0x8000000000000000UL51#define HBR_END 0xc000000000000000UL52#define HBR_AVPN 0x0200000000000000UL53#define HBR_ANDCOND 0x0100000000000000UL545556/* in hvCall.S */57EXPORT_SYMBOL(plpar_hcall);58EXPORT_SYMBOL(plpar_hcall9);59EXPORT_SYMBOL(plpar_hcall_norets);6061#ifdef CONFIG_PPC_64S_HASH_MMU62/*63* H_BLOCK_REMOVE supported block size for this page size in segment who's base64* page size is that page size.65*66* The first index is the segment base page size, the second one is the actual67* page size.68*/69static int hblkrm_size[MMU_PAGE_COUNT][MMU_PAGE_COUNT] __ro_after_init;70#endif7172/*73* Due to the involved complexity, and that the current hypervisor is only74* returning this value or 0, we are limiting the support of the H_BLOCK_REMOVE75* buffer size to 8 size block.76*/77#define HBLKRM_SUPPORTED_BLOCK_SIZE 87879#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE80static u8 dtl_mask = DTL_LOG_PREEMPT;81#else82static u8 dtl_mask;83#endif8485void alloc_dtl_buffers(unsigned long *time_limit)86{87int cpu;88struct paca_struct *pp;89struct dtl_entry *dtl;9091for_each_possible_cpu(cpu) {92pp = paca_ptrs[cpu];93if (pp->dispatch_log)94continue;95dtl = kmem_cache_alloc(dtl_cache, GFP_KERNEL);96if (!dtl) {97pr_warn("Failed to allocate dispatch trace log for cpu %d\n",98cpu);99#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE100pr_warn("Stolen time statistics will be unreliable\n");101#endif102break;103}104105pp->dtl_ridx = 0;106pp->dispatch_log = dtl;107pp->dispatch_log_end = dtl + N_DISPATCH_LOG;108pp->dtl_curr = dtl;109110if (time_limit && time_after(jiffies, *time_limit)) {111cond_resched();112*time_limit = jiffies + HZ;113}114}115}116117void register_dtl_buffer(int cpu)118{119long ret;120struct paca_struct *pp;121struct dtl_entry *dtl;122int hwcpu = get_hard_smp_processor_id(cpu);123124pp = paca_ptrs[cpu];125dtl = pp->dispatch_log;126if (dtl && dtl_mask) {127pp->dtl_ridx = 0;128pp->dtl_curr = dtl;129lppaca_of(cpu).dtl_idx = 0;130131/* hypervisor reads buffer length from this field */132dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);133ret = register_dtl(hwcpu, __pa(dtl));134if (ret)135pr_err("WARNING: DTL registration of cpu %d (hw %d) failed with %ld\n",136cpu, hwcpu, ret);137138lppaca_of(cpu).dtl_enable_mask = dtl_mask;139}140}141142#ifdef CONFIG_PPC_SPLPAR143struct dtl_worker {144struct delayed_work work;145int cpu;146};147148struct vcpu_dispatch_data {149int last_disp_cpu;150151int total_disp;152153int same_cpu_disp;154int same_chip_disp;155int diff_chip_disp;156int far_chip_disp;157158int numa_home_disp;159int numa_remote_disp;160int numa_far_disp;161};162163/*164* This represents the number of cpus in the hypervisor. Since there is no165* architected way to discover the number of processors in the host, we166* provision for dealing with NR_CPUS. This is currently 2048 by default, and167* is sufficient for our purposes. This will need to be tweaked if168* CONFIG_NR_CPUS is changed.169*/170#define NR_CPUS_H NR_CPUS171172DECLARE_RWSEM(dtl_access_lock);173static DEFINE_PER_CPU(struct vcpu_dispatch_data, vcpu_disp_data);174static DEFINE_PER_CPU(u64, dtl_entry_ridx);175static DEFINE_PER_CPU(struct dtl_worker, dtl_workers);176static enum cpuhp_state dtl_worker_state;177static DEFINE_MUTEX(dtl_enable_mutex);178static int vcpudispatch_stats_on __read_mostly;179static int vcpudispatch_stats_freq = 50;180static __be32 *vcpu_associativity, *pcpu_associativity;181182183static void free_dtl_buffers(unsigned long *time_limit)184{185#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE186int cpu;187struct paca_struct *pp;188189for_each_possible_cpu(cpu) {190pp = paca_ptrs[cpu];191if (!pp->dispatch_log)192continue;193kmem_cache_free(dtl_cache, pp->dispatch_log);194pp->dtl_ridx = 0;195pp->dispatch_log = NULL;196pp->dispatch_log_end = NULL;197pp->dtl_curr = NULL;198199if (time_limit && time_after(jiffies, *time_limit)) {200cond_resched();201*time_limit = jiffies + HZ;202}203}204#endif205}206207static int init_cpu_associativity(void)208{209vcpu_associativity = kcalloc(num_possible_cpus() / threads_per_core,210VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);211pcpu_associativity = kcalloc(NR_CPUS_H / threads_per_core,212VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);213214if (!vcpu_associativity || !pcpu_associativity) {215pr_err("error allocating memory for associativity information\n");216return -ENOMEM;217}218219return 0;220}221222static void destroy_cpu_associativity(void)223{224kfree(vcpu_associativity);225kfree(pcpu_associativity);226vcpu_associativity = pcpu_associativity = NULL;227}228229static __be32 *__get_cpu_associativity(int cpu, __be32 *cpu_assoc, int flag)230{231__be32 *assoc;232int rc = 0;233234assoc = &cpu_assoc[(int)(cpu / threads_per_core) * VPHN_ASSOC_BUFSIZE];235if (!assoc[0]) {236rc = hcall_vphn(cpu, flag, &assoc[0]);237if (rc)238return NULL;239}240241return assoc;242}243244static __be32 *get_pcpu_associativity(int cpu)245{246return __get_cpu_associativity(cpu, pcpu_associativity, VPHN_FLAG_PCPU);247}248249static __be32 *get_vcpu_associativity(int cpu)250{251return __get_cpu_associativity(cpu, vcpu_associativity, VPHN_FLAG_VCPU);252}253254static int cpu_relative_dispatch_distance(int last_disp_cpu, int cur_disp_cpu)255{256__be32 *last_disp_cpu_assoc, *cur_disp_cpu_assoc;257258if (last_disp_cpu >= NR_CPUS_H || cur_disp_cpu >= NR_CPUS_H)259return -EINVAL;260261last_disp_cpu_assoc = get_pcpu_associativity(last_disp_cpu);262cur_disp_cpu_assoc = get_pcpu_associativity(cur_disp_cpu);263264if (!last_disp_cpu_assoc || !cur_disp_cpu_assoc)265return -EIO;266267return cpu_relative_distance(last_disp_cpu_assoc, cur_disp_cpu_assoc);268}269270static int cpu_home_node_dispatch_distance(int disp_cpu)271{272__be32 *disp_cpu_assoc, *vcpu_assoc;273int vcpu_id = smp_processor_id();274275if (disp_cpu >= NR_CPUS_H) {276pr_debug_ratelimited("vcpu dispatch cpu %d > %d\n",277disp_cpu, NR_CPUS_H);278return -EINVAL;279}280281disp_cpu_assoc = get_pcpu_associativity(disp_cpu);282vcpu_assoc = get_vcpu_associativity(vcpu_id);283284if (!disp_cpu_assoc || !vcpu_assoc)285return -EIO;286287return cpu_relative_distance(disp_cpu_assoc, vcpu_assoc);288}289290static void update_vcpu_disp_stat(int disp_cpu)291{292struct vcpu_dispatch_data *disp;293int distance;294295disp = this_cpu_ptr(&vcpu_disp_data);296if (disp->last_disp_cpu == -1) {297disp->last_disp_cpu = disp_cpu;298return;299}300301disp->total_disp++;302303if (disp->last_disp_cpu == disp_cpu ||304(cpu_first_thread_sibling(disp->last_disp_cpu) ==305cpu_first_thread_sibling(disp_cpu)))306disp->same_cpu_disp++;307else {308distance = cpu_relative_dispatch_distance(disp->last_disp_cpu,309disp_cpu);310if (distance < 0)311pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",312smp_processor_id());313else {314switch (distance) {315case 0:316disp->same_chip_disp++;317break;318case 1:319disp->diff_chip_disp++;320break;321case 2:322disp->far_chip_disp++;323break;324default:325pr_debug_ratelimited("vcpudispatch_stats: cpu %d (%d -> %d): unexpected relative dispatch distance %d\n",326smp_processor_id(),327disp->last_disp_cpu,328disp_cpu,329distance);330}331}332}333334distance = cpu_home_node_dispatch_distance(disp_cpu);335if (distance < 0)336pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",337smp_processor_id());338else {339switch (distance) {340case 0:341disp->numa_home_disp++;342break;343case 1:344disp->numa_remote_disp++;345break;346case 2:347disp->numa_far_disp++;348break;349default:350pr_debug_ratelimited("vcpudispatch_stats: cpu %d on %d: unexpected numa dispatch distance %d\n",351smp_processor_id(),352disp_cpu,353distance);354}355}356357disp->last_disp_cpu = disp_cpu;358}359360static void process_dtl_buffer(struct work_struct *work)361{362struct dtl_entry dtle;363u64 i = __this_cpu_read(dtl_entry_ridx);364struct dtl_entry *dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);365struct dtl_entry *dtl_end = local_paca->dispatch_log_end;366struct lppaca *vpa = local_paca->lppaca_ptr;367struct dtl_worker *d = container_of(work, struct dtl_worker, work.work);368369if (!local_paca->dispatch_log)370return;371372/* if we have been migrated away, we cancel ourself */373if (d->cpu != smp_processor_id()) {374pr_debug("vcpudispatch_stats: cpu %d worker migrated -- canceling worker\n",375smp_processor_id());376return;377}378379if (i == be64_to_cpu(vpa->dtl_idx))380goto out;381382while (i < be64_to_cpu(vpa->dtl_idx)) {383dtle = *dtl;384barrier();385if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {386/* buffer has overflowed */387pr_debug_ratelimited("vcpudispatch_stats: cpu %d lost %lld DTL samples\n",388d->cpu,389be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG - i);390i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;391dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);392continue;393}394update_vcpu_disp_stat(be16_to_cpu(dtle.processor_id));395++i;396++dtl;397if (dtl == dtl_end)398dtl = local_paca->dispatch_log;399}400401__this_cpu_write(dtl_entry_ridx, i);402403out:404schedule_delayed_work_on(d->cpu, to_delayed_work(work),405HZ / vcpudispatch_stats_freq);406}407408static int dtl_worker_online(unsigned int cpu)409{410struct dtl_worker *d = &per_cpu(dtl_workers, cpu);411412memset(d, 0, sizeof(*d));413INIT_DELAYED_WORK(&d->work, process_dtl_buffer);414d->cpu = cpu;415416#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE417per_cpu(dtl_entry_ridx, cpu) = 0;418register_dtl_buffer(cpu);419#else420per_cpu(dtl_entry_ridx, cpu) = be64_to_cpu(lppaca_of(cpu).dtl_idx);421#endif422423schedule_delayed_work_on(cpu, &d->work, HZ / vcpudispatch_stats_freq);424return 0;425}426427static int dtl_worker_offline(unsigned int cpu)428{429struct dtl_worker *d = &per_cpu(dtl_workers, cpu);430431cancel_delayed_work_sync(&d->work);432433#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE434unregister_dtl(get_hard_smp_processor_id(cpu));435#endif436437return 0;438}439440static void set_global_dtl_mask(u8 mask)441{442int cpu;443444dtl_mask = mask;445for_each_present_cpu(cpu)446lppaca_of(cpu).dtl_enable_mask = dtl_mask;447}448449static void reset_global_dtl_mask(void)450{451int cpu;452453#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE454dtl_mask = DTL_LOG_PREEMPT;455#else456dtl_mask = 0;457#endif458for_each_present_cpu(cpu)459lppaca_of(cpu).dtl_enable_mask = dtl_mask;460}461462static int dtl_worker_enable(unsigned long *time_limit)463{464int rc = 0, state;465466if (!down_write_trylock(&dtl_access_lock)) {467rc = -EBUSY;468goto out;469}470471set_global_dtl_mask(DTL_LOG_ALL);472473/* Setup dtl buffers and register those */474alloc_dtl_buffers(time_limit);475476state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/dtl:online",477dtl_worker_online, dtl_worker_offline);478if (state < 0) {479pr_err("vcpudispatch_stats: unable to setup workqueue for DTL processing\n");480free_dtl_buffers(time_limit);481reset_global_dtl_mask();482up_write(&dtl_access_lock);483rc = -EINVAL;484goto out;485}486dtl_worker_state = state;487488out:489return rc;490}491492static void dtl_worker_disable(unsigned long *time_limit)493{494cpuhp_remove_state(dtl_worker_state);495free_dtl_buffers(time_limit);496reset_global_dtl_mask();497up_write(&dtl_access_lock);498}499500static ssize_t vcpudispatch_stats_write(struct file *file, const char __user *p,501size_t count, loff_t *ppos)502{503unsigned long time_limit = jiffies + HZ;504struct vcpu_dispatch_data *disp;505int rc, cmd, cpu;506char buf[16];507508if (count > 15)509return -EINVAL;510511if (copy_from_user(buf, p, count))512return -EFAULT;513514buf[count] = 0;515rc = kstrtoint(buf, 0, &cmd);516if (rc || cmd < 0 || cmd > 1) {517pr_err("vcpudispatch_stats: please use 0 to disable or 1 to enable dispatch statistics\n");518return rc ? rc : -EINVAL;519}520521mutex_lock(&dtl_enable_mutex);522523if ((cmd == 0 && !vcpudispatch_stats_on) ||524(cmd == 1 && vcpudispatch_stats_on))525goto out;526527if (cmd) {528rc = init_cpu_associativity();529if (rc) {530destroy_cpu_associativity();531goto out;532}533534for_each_possible_cpu(cpu) {535disp = per_cpu_ptr(&vcpu_disp_data, cpu);536memset(disp, 0, sizeof(*disp));537disp->last_disp_cpu = -1;538}539540rc = dtl_worker_enable(&time_limit);541if (rc) {542destroy_cpu_associativity();543goto out;544}545} else {546dtl_worker_disable(&time_limit);547destroy_cpu_associativity();548}549550vcpudispatch_stats_on = cmd;551552out:553mutex_unlock(&dtl_enable_mutex);554if (rc)555return rc;556return count;557}558559static int vcpudispatch_stats_display(struct seq_file *p, void *v)560{561int cpu;562struct vcpu_dispatch_data *disp;563564if (!vcpudispatch_stats_on) {565seq_puts(p, "off\n");566return 0;567}568569for_each_online_cpu(cpu) {570disp = per_cpu_ptr(&vcpu_disp_data, cpu);571seq_printf(p, "cpu%d", cpu);572seq_put_decimal_ull(p, " ", disp->total_disp);573seq_put_decimal_ull(p, " ", disp->same_cpu_disp);574seq_put_decimal_ull(p, " ", disp->same_chip_disp);575seq_put_decimal_ull(p, " ", disp->diff_chip_disp);576seq_put_decimal_ull(p, " ", disp->far_chip_disp);577seq_put_decimal_ull(p, " ", disp->numa_home_disp);578seq_put_decimal_ull(p, " ", disp->numa_remote_disp);579seq_put_decimal_ull(p, " ", disp->numa_far_disp);580seq_puts(p, "\n");581}582583return 0;584}585586static int vcpudispatch_stats_open(struct inode *inode, struct file *file)587{588return single_open(file, vcpudispatch_stats_display, NULL);589}590591static const struct proc_ops vcpudispatch_stats_proc_ops = {592.proc_open = vcpudispatch_stats_open,593.proc_read = seq_read,594.proc_write = vcpudispatch_stats_write,595.proc_lseek = seq_lseek,596.proc_release = single_release,597};598599static ssize_t vcpudispatch_stats_freq_write(struct file *file,600const char __user *p, size_t count, loff_t *ppos)601{602int rc, freq;603char buf[16];604605if (count > 15)606return -EINVAL;607608if (copy_from_user(buf, p, count))609return -EFAULT;610611buf[count] = 0;612rc = kstrtoint(buf, 0, &freq);613if (rc || freq < 1 || freq > HZ) {614pr_err("vcpudispatch_stats_freq: please specify a frequency between 1 and %d\n",615HZ);616return rc ? rc : -EINVAL;617}618619vcpudispatch_stats_freq = freq;620621return count;622}623624static int vcpudispatch_stats_freq_display(struct seq_file *p, void *v)625{626seq_printf(p, "%d\n", vcpudispatch_stats_freq);627return 0;628}629630static int vcpudispatch_stats_freq_open(struct inode *inode, struct file *file)631{632return single_open(file, vcpudispatch_stats_freq_display, NULL);633}634635static const struct proc_ops vcpudispatch_stats_freq_proc_ops = {636.proc_open = vcpudispatch_stats_freq_open,637.proc_read = seq_read,638.proc_write = vcpudispatch_stats_freq_write,639.proc_lseek = seq_lseek,640.proc_release = single_release,641};642643static int __init vcpudispatch_stats_procfs_init(void)644{645if (!lppaca_shared_proc())646return 0;647648if (!proc_create("powerpc/vcpudispatch_stats", 0600, NULL,649&vcpudispatch_stats_proc_ops))650pr_err("vcpudispatch_stats: error creating procfs file\n");651else if (!proc_create("powerpc/vcpudispatch_stats_freq", 0600, NULL,652&vcpudispatch_stats_freq_proc_ops))653pr_err("vcpudispatch_stats_freq: error creating procfs file\n");654655return 0;656}657658machine_device_initcall(pseries, vcpudispatch_stats_procfs_init);659660#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING661u64 pseries_paravirt_steal_clock(int cpu)662{663struct lppaca *lppaca = &lppaca_of(cpu);664665/*666* VPA steal time counters are reported at TB frequency. Hence do a667* conversion to ns before returning668*/669return tb_to_ns(be64_to_cpu(READ_ONCE(lppaca->enqueue_dispatch_tb)) +670be64_to_cpu(READ_ONCE(lppaca->ready_enqueue_tb)));671}672#endif673674#endif /* CONFIG_PPC_SPLPAR */675676void vpa_init(int cpu)677{678int hwcpu = get_hard_smp_processor_id(cpu);679unsigned long addr;680long ret;681682/*683* The spec says it "may be problematic" if CPU x registers the VPA of684* CPU y. We should never do that, but wail if we ever do.685*/686WARN_ON(cpu != smp_processor_id());687688if (cpu_has_feature(CPU_FTR_ALTIVEC))689lppaca_of(cpu).vmxregs_in_use = 1;690691if (cpu_has_feature(CPU_FTR_ARCH_207S))692lppaca_of(cpu).ebb_regs_in_use = 1;693694addr = __pa(&lppaca_of(cpu));695ret = register_vpa(hwcpu, addr);696697if (ret) {698pr_err("WARNING: VPA registration for cpu %d (hw %d) of area "699"%lx failed with %ld\n", cpu, hwcpu, addr, ret);700return;701}702703#ifdef CONFIG_PPC_64S_HASH_MMU704/*705* PAPR says this feature is SLB-Buffer but firmware never706* reports that. All SPLPAR support SLB shadow buffer.707*/708if (!radix_enabled() && firmware_has_feature(FW_FEATURE_SPLPAR)) {709addr = __pa(paca_ptrs[cpu]->slb_shadow_ptr);710ret = register_slb_shadow(hwcpu, addr);711if (ret)712pr_err("WARNING: SLB shadow buffer registration for "713"cpu %d (hw %d) of area %lx failed with %ld\n",714cpu, hwcpu, addr, ret);715}716#endif /* CONFIG_PPC_64S_HASH_MMU */717718/*719* Register dispatch trace log, if one has been allocated.720*/721register_dtl_buffer(cpu);722}723724#ifdef CONFIG_PPC_BOOK3S_64725726static int __init pseries_lpar_register_process_table(unsigned long base,727unsigned long page_size, unsigned long table_size)728{729long rc;730unsigned long flags = 0;731732if (table_size)733flags |= PROC_TABLE_NEW;734if (radix_enabled()) {735flags |= PROC_TABLE_RADIX;736if (mmu_has_feature(MMU_FTR_GTSE))737flags |= PROC_TABLE_GTSE;738} else739flags |= PROC_TABLE_HPT_SLB;740for (;;) {741rc = plpar_hcall_norets(H_REGISTER_PROC_TBL, flags, base,742page_size, table_size);743if (!H_IS_LONG_BUSY(rc))744break;745mdelay(get_longbusy_msecs(rc));746}747if (rc != H_SUCCESS) {748pr_err("Failed to register process table (rc=%ld)\n", rc);749BUG();750}751return rc;752}753754#ifdef CONFIG_PPC_64S_HASH_MMU755756static long pSeries_lpar_hpte_insert(unsigned long hpte_group,757unsigned long vpn, unsigned long pa,758unsigned long rflags, unsigned long vflags,759int psize, int apsize, int ssize)760{761unsigned long lpar_rc;762unsigned long flags;763unsigned long slot;764unsigned long hpte_v, hpte_r;765766if (!(vflags & HPTE_V_BOLTED))767pr_devel("hpte_insert(group=%lx, vpn=%016lx, "768"pa=%016lx, rflags=%lx, vflags=%lx, psize=%d)\n",769hpte_group, vpn, pa, rflags, vflags, psize);770771hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;772hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;773774if (!(vflags & HPTE_V_BOLTED))775pr_devel(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);776777/* Now fill in the actual HPTE */778/* Set CEC cookie to 0 */779/* Zero page = 0 */780/* I-cache Invalidate = 0 */781/* I-cache synchronize = 0 */782/* Exact = 0 */783flags = 0;784785if (firmware_has_feature(FW_FEATURE_XCMO) && !(hpte_r & HPTE_R_N))786flags |= H_COALESCE_CAND;787788lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot);789if (unlikely(lpar_rc == H_PTEG_FULL)) {790pr_devel("Hash table group is full\n");791return -1;792}793794/*795* Since we try and ioremap PHBs we don't own, the pte insert796* will fail. However we must catch the failure in hash_page797* or we will loop forever, so return -2 in this case.798*/799if (unlikely(lpar_rc != H_SUCCESS)) {800pr_err("Failed hash pte insert with error %ld\n", lpar_rc);801return -2;802}803if (!(vflags & HPTE_V_BOLTED))804pr_devel(" -> slot: %lu\n", slot & 7);805806/* Because of iSeries, we have to pass down the secondary807* bucket bit here as well808*/809return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);810}811812static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);813814static long pSeries_lpar_hpte_remove(unsigned long hpte_group)815{816unsigned long slot_offset;817unsigned long lpar_rc;818int i;819unsigned long dummy1, dummy2;820821/* pick a random slot to start at */822slot_offset = mftb() & 0x7;823824for (i = 0; i < HPTES_PER_GROUP; i++) {825826/* don't remove a bolted entry */827lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,828HPTE_V_BOLTED, &dummy1, &dummy2);829if (lpar_rc == H_SUCCESS)830return i;831832/*833* The test for adjunct partition is performed before the834* ANDCOND test. H_RESOURCE may be returned, so we need to835* check for that as well.836*/837BUG_ON(lpar_rc != H_NOT_FOUND && lpar_rc != H_RESOURCE);838839slot_offset++;840slot_offset &= 0x7;841}842843return -1;844}845846/* Called during kexec sequence with MMU off */847static notrace void manual_hpte_clear_all(void)848{849unsigned long size_bytes = 1UL << ppc64_pft_size;850unsigned long hpte_count = size_bytes >> 4;851struct {852unsigned long pteh;853unsigned long ptel;854} ptes[4];855long lpar_rc;856unsigned long i, j;857858/* Read in batches of 4,859* invalidate only valid entries not in the VRMA860* hpte_count will be a multiple of 4861*/862for (i = 0; i < hpte_count; i += 4) {863lpar_rc = plpar_pte_read_4_raw(0, i, (void *)ptes);864if (lpar_rc != H_SUCCESS) {865pr_info("Failed to read hash page table at %ld err %ld\n",866i, lpar_rc);867continue;868}869for (j = 0; j < 4; j++){870if ((ptes[j].pteh & HPTE_V_VRMA_MASK) ==871HPTE_V_VRMA_MASK)872continue;873if (ptes[j].pteh & HPTE_V_VALID)874plpar_pte_remove_raw(0, i + j, 0,875&(ptes[j].pteh), &(ptes[j].ptel));876}877}878}879880/* Called during kexec sequence with MMU off */881static notrace int hcall_hpte_clear_all(void)882{883int rc;884885do {886rc = plpar_hcall_norets(H_CLEAR_HPT);887} while (rc == H_CONTINUE);888889return rc;890}891892/* Called during kexec sequence with MMU off */893static notrace void pseries_hpte_clear_all(void)894{895int rc;896897rc = hcall_hpte_clear_all();898if (rc != H_SUCCESS)899manual_hpte_clear_all();900901#ifdef __LITTLE_ENDIAN__902/*903* Reset exceptions to big endian.904*905* FIXME this is a hack for kexec, we need to reset the exception906* endian before starting the new kernel and this is a convenient place907* to do it.908*909* This is also called on boot when a fadump happens. In that case we910* must not change the exception endian mode.911*/912if (firmware_has_feature(FW_FEATURE_SET_MODE) && !is_fadump_active())913pseries_big_endian_exceptions();914#endif915}916917/*918* NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and919* the low 3 bits of flags happen to line up. So no transform is needed.920* We can probably optimize here and assume the high bits of newpp are921* already zero. For now I am paranoid.922*/923static long pSeries_lpar_hpte_updatepp(unsigned long slot,924unsigned long newpp,925unsigned long vpn,926int psize, int apsize,927int ssize, unsigned long inv_flags)928{929unsigned long lpar_rc;930unsigned long flags;931unsigned long want_v;932933want_v = hpte_encode_avpn(vpn, psize, ssize);934935flags = (newpp & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO)) | H_AVPN;936flags |= (newpp & HPTE_R_KEY_HI) >> 48;937if (mmu_has_feature(MMU_FTR_KERNEL_RO))938/* Move pp0 into bit 8 (IBM 55) */939flags |= (newpp & HPTE_R_PP0) >> 55;940941pr_devel(" update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...",942want_v, slot, flags, psize);943944lpar_rc = plpar_pte_protect(flags, slot, want_v);945946if (lpar_rc == H_NOT_FOUND) {947pr_devel("not found !\n");948return -1;949}950951pr_devel("ok\n");952953BUG_ON(lpar_rc != H_SUCCESS);954955return 0;956}957958static long __pSeries_lpar_hpte_find(unsigned long want_v, unsigned long hpte_group)959{960long lpar_rc;961unsigned long i, j;962struct {963unsigned long pteh;964unsigned long ptel;965} ptes[4];966967for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {968969lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);970if (lpar_rc != H_SUCCESS) {971pr_info("Failed to read hash page table at %ld err %ld\n",972hpte_group, lpar_rc);973continue;974}975976for (j = 0; j < 4; j++) {977if (HPTE_V_COMPARE(ptes[j].pteh, want_v) &&978(ptes[j].pteh & HPTE_V_VALID))979return i + j;980}981}982983return -1;984}985986static long pSeries_lpar_hpte_find(unsigned long vpn, int psize, int ssize)987{988long slot;989unsigned long hash;990unsigned long want_v;991unsigned long hpte_group;992993hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);994want_v = hpte_encode_avpn(vpn, psize, ssize);995996/*997* We try to keep bolted entries always in primary hash998* But in some case we can find them in secondary too.999*/1000hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;1001slot = __pSeries_lpar_hpte_find(want_v, hpte_group);1002if (slot < 0) {1003/* Try in secondary */1004hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;1005slot = __pSeries_lpar_hpte_find(want_v, hpte_group);1006if (slot < 0)1007return -1;1008}1009return hpte_group + slot;1010}10111012static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,1013unsigned long ea,1014int psize, int ssize)1015{1016unsigned long vpn;1017unsigned long lpar_rc, slot, vsid, flags;10181019vsid = get_kernel_vsid(ea, ssize);1020vpn = hpt_vpn(ea, vsid, ssize);10211022slot = pSeries_lpar_hpte_find(vpn, psize, ssize);1023BUG_ON(slot == -1);10241025flags = newpp & (HPTE_R_PP | HPTE_R_N);1026if (mmu_has_feature(MMU_FTR_KERNEL_RO))1027/* Move pp0 into bit 8 (IBM 55) */1028flags |= (newpp & HPTE_R_PP0) >> 55;10291030flags |= ((newpp & HPTE_R_KEY_HI) >> 48) | (newpp & HPTE_R_KEY_LO);10311032lpar_rc = plpar_pte_protect(flags, slot, 0);10331034BUG_ON(lpar_rc != H_SUCCESS);1035}10361037static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn,1038int psize, int apsize,1039int ssize, int local)1040{1041unsigned long want_v;1042unsigned long lpar_rc;1043unsigned long dummy1, dummy2;10441045pr_devel(" inval : slot=%lx, vpn=%016lx, psize: %d, local: %d\n",1046slot, vpn, psize, local);10471048want_v = hpte_encode_avpn(vpn, psize, ssize);1049lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2);1050if (lpar_rc == H_NOT_FOUND)1051return;10521053BUG_ON(lpar_rc != H_SUCCESS);1054}105510561057/*1058* As defined in the PAPR's section 14.5.4.1.81059* The control mask doesn't include the returned reference and change bit from1060* the processed PTE.1061*/1062#define HBLKR_AVPN 0x0100000000000000UL1063#define HBLKR_CTRL_MASK 0xf800000000000000UL1064#define HBLKR_CTRL_SUCCESS 0x8000000000000000UL1065#define HBLKR_CTRL_ERRNOTFOUND 0x8800000000000000UL1066#define HBLKR_CTRL_ERRBUSY 0xa000000000000000UL10671068/*1069* Returned true if we are supporting this block size for the specified segment1070* base page size and actual page size.1071*1072* Currently, we only support 8 size block.1073*/1074static inline bool is_supported_hlbkrm(int bpsize, int psize)1075{1076return (hblkrm_size[bpsize][psize] == HBLKRM_SUPPORTED_BLOCK_SIZE);1077}10781079/**1080* H_BLOCK_REMOVE caller.1081* @idx should point to the latest @param entry set with a PTEX.1082* If PTE cannot be processed because another CPUs has already locked that1083* group, those entries are put back in @param starting at index 1.1084* If entries has to be retried and @retry_busy is set to true, these entries1085* are retried until success. If @retry_busy is set to false, the returned1086* is the number of entries yet to process.1087*/1088static unsigned long call_block_remove(unsigned long idx, unsigned long *param,1089bool retry_busy)1090{1091unsigned long i, rc, new_idx;1092unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];10931094if (idx < 2) {1095pr_warn("Unexpected empty call to H_BLOCK_REMOVE");1096return 0;1097}1098again:1099new_idx = 0;1100if (idx > PLPAR_HCALL9_BUFSIZE) {1101pr_err("Too many PTEs (%lu) for H_BLOCK_REMOVE", idx);1102idx = PLPAR_HCALL9_BUFSIZE;1103} else if (idx < PLPAR_HCALL9_BUFSIZE)1104param[idx] = HBR_END;11051106rc = plpar_hcall9(H_BLOCK_REMOVE, retbuf,1107param[0], /* AVA */1108param[1], param[2], param[3], param[4], /* TS0-7 */1109param[5], param[6], param[7], param[8]);1110if (rc == H_SUCCESS)1111return 0;11121113BUG_ON(rc != H_PARTIAL);11141115/* Check that the unprocessed entries were 'not found' or 'busy' */1116for (i = 0; i < idx-1; i++) {1117unsigned long ctrl = retbuf[i] & HBLKR_CTRL_MASK;11181119if (ctrl == HBLKR_CTRL_ERRBUSY) {1120param[++new_idx] = param[i+1];1121continue;1122}11231124BUG_ON(ctrl != HBLKR_CTRL_SUCCESS1125&& ctrl != HBLKR_CTRL_ERRNOTFOUND);1126}11271128/*1129* If there were entries found busy, retry these entries if requested,1130* of if all the entries have to be retried.1131*/1132if (new_idx && (retry_busy || new_idx == (PLPAR_HCALL9_BUFSIZE-1))) {1133idx = new_idx + 1;1134goto again;1135}11361137return new_idx;1138}11391140#ifdef CONFIG_TRANSPARENT_HUGEPAGE1141/*1142* Limit iterations holding pSeries_lpar_tlbie_lock to 3. We also need1143* to make sure that we avoid bouncing the hypervisor tlbie lock.1144*/1145#define PPC64_HUGE_HPTE_BATCH 1211461147static void hugepage_block_invalidate(unsigned long *slot, unsigned long *vpn,1148int count, int psize, int ssize)1149{1150unsigned long param[PLPAR_HCALL9_BUFSIZE];1151unsigned long shift, current_vpgb, vpgb;1152int i, pix = 0;11531154shift = mmu_psize_defs[psize].shift;11551156for (i = 0; i < count; i++) {1157/*1158* Shifting 3 bits more on the right to get a1159* 8 pages aligned virtual addresse.1160*/1161vpgb = (vpn[i] >> (shift - VPN_SHIFT + 3));1162if (!pix || vpgb != current_vpgb) {1163/*1164* Need to start a new 8 pages block, flush1165* the current one if needed.1166*/1167if (pix)1168(void)call_block_remove(pix, param, true);1169current_vpgb = vpgb;1170param[0] = hpte_encode_avpn(vpn[i], psize, ssize);1171pix = 1;1172}11731174param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot[i];1175if (pix == PLPAR_HCALL9_BUFSIZE) {1176pix = call_block_remove(pix, param, false);1177/*1178* pix = 0 means that all the entries were1179* removed, we can start a new block.1180* Otherwise, this means that there are entries1181* to retry, and pix points to latest one, so1182* we should increment it and try to continue1183* the same block.1184*/1185if (pix)1186pix++;1187}1188}1189if (pix)1190(void)call_block_remove(pix, param, true);1191}11921193static void hugepage_bulk_invalidate(unsigned long *slot, unsigned long *vpn,1194int count, int psize, int ssize)1195{1196unsigned long param[PLPAR_HCALL9_BUFSIZE];1197int i = 0, pix = 0, rc;11981199for (i = 0; i < count; i++) {12001201if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {1202pSeries_lpar_hpte_invalidate(slot[i], vpn[i], psize, 0,1203ssize, 0);1204} else {1205param[pix] = HBR_REQUEST | HBR_AVPN | slot[i];1206param[pix+1] = hpte_encode_avpn(vpn[i], psize, ssize);1207pix += 2;1208if (pix == 8) {1209rc = plpar_hcall9(H_BULK_REMOVE, param,1210param[0], param[1], param[2],1211param[3], param[4], param[5],1212param[6], param[7]);1213BUG_ON(rc != H_SUCCESS);1214pix = 0;1215}1216}1217}1218if (pix) {1219param[pix] = HBR_END;1220rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],1221param[2], param[3], param[4], param[5],1222param[6], param[7]);1223BUG_ON(rc != H_SUCCESS);1224}1225}12261227static inline void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,1228unsigned long *vpn,1229int count, int psize,1230int ssize)1231{1232unsigned long flags = 0;1233int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);12341235if (lock_tlbie)1236spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);12371238/* Assuming THP size is 16M */1239if (is_supported_hlbkrm(psize, MMU_PAGE_16M))1240hugepage_block_invalidate(slot, vpn, count, psize, ssize);1241else1242hugepage_bulk_invalidate(slot, vpn, count, psize, ssize);12431244if (lock_tlbie)1245spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);1246}12471248static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,1249unsigned long addr,1250unsigned char *hpte_slot_array,1251int psize, int ssize, int local)1252{1253int i, index = 0;1254unsigned long s_addr = addr;1255unsigned int max_hpte_count, valid;1256unsigned long vpn_array[PPC64_HUGE_HPTE_BATCH];1257unsigned long slot_array[PPC64_HUGE_HPTE_BATCH];1258unsigned long shift, hidx, vpn = 0, hash, slot;12591260shift = mmu_psize_defs[psize].shift;1261max_hpte_count = 1U << (PMD_SHIFT - shift);12621263for (i = 0; i < max_hpte_count; i++) {1264valid = hpte_valid(hpte_slot_array, i);1265if (!valid)1266continue;1267hidx = hpte_hash_index(hpte_slot_array, i);12681269/* get the vpn */1270addr = s_addr + (i * (1ul << shift));1271vpn = hpt_vpn(addr, vsid, ssize);1272hash = hpt_hash(vpn, shift, ssize);1273if (hidx & _PTEIDX_SECONDARY)1274hash = ~hash;12751276slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;1277slot += hidx & _PTEIDX_GROUP_IX;12781279slot_array[index] = slot;1280vpn_array[index] = vpn;1281if (index == PPC64_HUGE_HPTE_BATCH - 1) {1282/*1283* Now do a bluk invalidate1284*/1285__pSeries_lpar_hugepage_invalidate(slot_array,1286vpn_array,1287PPC64_HUGE_HPTE_BATCH,1288psize, ssize);1289index = 0;1290} else1291index++;1292}1293if (index)1294__pSeries_lpar_hugepage_invalidate(slot_array, vpn_array,1295index, psize, ssize);1296}1297#else1298static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,1299unsigned long addr,1300unsigned char *hpte_slot_array,1301int psize, int ssize, int local)1302{1303WARN(1, "%s called without THP support\n", __func__);1304}1305#endif13061307static int pSeries_lpar_hpte_removebolted(unsigned long ea,1308int psize, int ssize)1309{1310unsigned long vpn;1311unsigned long slot, vsid;13121313vsid = get_kernel_vsid(ea, ssize);1314vpn = hpt_vpn(ea, vsid, ssize);13151316slot = pSeries_lpar_hpte_find(vpn, psize, ssize);1317if (slot == -1)1318return -ENOENT;13191320/*1321* lpar doesn't use the passed actual page size1322*/1323pSeries_lpar_hpte_invalidate(slot, vpn, psize, 0, ssize, 0);1324return 0;1325}132613271328static inline unsigned long compute_slot(real_pte_t pte,1329unsigned long vpn,1330unsigned long index,1331unsigned long shift,1332int ssize)1333{1334unsigned long slot, hash, hidx;13351336hash = hpt_hash(vpn, shift, ssize);1337hidx = __rpte_to_hidx(pte, index);1338if (hidx & _PTEIDX_SECONDARY)1339hash = ~hash;1340slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;1341slot += hidx & _PTEIDX_GROUP_IX;1342return slot;1343}13441345/**1346* The hcall H_BLOCK_REMOVE implies that the virtual pages to processed are1347* "all within the same naturally aligned 8 page virtual address block".1348*/1349static void do_block_remove(unsigned long number, struct ppc64_tlb_batch *batch,1350unsigned long *param)1351{1352unsigned long vpn;1353unsigned long i, pix = 0;1354unsigned long index, shift, slot, current_vpgb, vpgb;1355real_pte_t pte;1356int psize, ssize;13571358psize = batch->psize;1359ssize = batch->ssize;13601361for (i = 0; i < number; i++) {1362vpn = batch->vpn[i];1363pte = batch->pte[i];1364pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {1365/*1366* Shifting 3 bits more on the right to get a1367* 8 pages aligned virtual addresse.1368*/1369vpgb = (vpn >> (shift - VPN_SHIFT + 3));1370if (!pix || vpgb != current_vpgb) {1371/*1372* Need to start a new 8 pages block, flush1373* the current one if needed.1374*/1375if (pix)1376(void)call_block_remove(pix, param,1377true);1378current_vpgb = vpgb;1379param[0] = hpte_encode_avpn(vpn, psize,1380ssize);1381pix = 1;1382}13831384slot = compute_slot(pte, vpn, index, shift, ssize);1385param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot;13861387if (pix == PLPAR_HCALL9_BUFSIZE) {1388pix = call_block_remove(pix, param, false);1389/*1390* pix = 0 means that all the entries were1391* removed, we can start a new block.1392* Otherwise, this means that there are entries1393* to retry, and pix points to latest one, so1394* we should increment it and try to continue1395* the same block.1396*/1397if (pix)1398pix++;1399}1400} pte_iterate_hashed_end();1401}14021403if (pix)1404(void)call_block_remove(pix, param, true);1405}14061407/*1408* TLB Block Invalidate Characteristics1409*1410* These characteristics define the size of the block the hcall H_BLOCK_REMOVE1411* is able to process for each couple segment base page size, actual page size.1412*1413* The ibm,get-system-parameter properties is returning a buffer with the1414* following layout:1415*1416* [ 2 bytes size of the RTAS buffer (excluding these 2 bytes) ]1417* -----------------1418* TLB Block Invalidate Specifiers:1419* [ 1 byte LOG base 2 of the TLB invalidate block size being specified ]1420* [ 1 byte Number of page sizes (N) that are supported for the specified1421* TLB invalidate block size ]1422* [ 1 byte Encoded segment base page size and actual page size1423* MSB=0 means 4k segment base page size and actual page size1424* MSB=1 the penc value in mmu_psize_def ]1425* ...1426* -----------------1427* Next TLB Block Invalidate Specifiers...1428* -----------------1429* [ 0 ]1430*/1431static inline void set_hblkrm_bloc_size(int bpsize, int psize,1432unsigned int block_size)1433{1434if (block_size > hblkrm_size[bpsize][psize])1435hblkrm_size[bpsize][psize] = block_size;1436}14371438/*1439* Decode the Encoded segment base page size and actual page size.1440* PAPR specifies:1441* - bit 7 is the L bit1442* - bits 0-5 are the penc value1443* If the L bit is 0, this means 4K segment base page size and actual page size1444* otherwise the penc value should be read.1445*/1446#define HBLKRM_L_MASK 0x801447#define HBLKRM_PENC_MASK 0x3f1448static inline void __init check_lp_set_hblkrm(unsigned int lp,1449unsigned int block_size)1450{1451unsigned int bpsize, psize;14521453/* First, check the L bit, if not set, this means 4K */1454if ((lp & HBLKRM_L_MASK) == 0) {1455set_hblkrm_bloc_size(MMU_PAGE_4K, MMU_PAGE_4K, block_size);1456return;1457}14581459lp &= HBLKRM_PENC_MASK;1460for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++) {1461struct mmu_psize_def *def = &mmu_psize_defs[bpsize];14621463for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {1464if (def->penc[psize] == lp) {1465set_hblkrm_bloc_size(bpsize, psize, block_size);1466return;1467}1468}1469}1470}14711472/*1473* The size of the TLB Block Invalidate Characteristics is variable. But at the1474* maximum it will be the number of possible page sizes *2 + 10 bytes.1475* Currently MMU_PAGE_COUNT is 16, which means 42 bytes. Use a cache line size1476* (128 bytes) for the buffer to get plenty of space.1477*/1478#define SPLPAR_TLB_BIC_MAXLENGTH 12814791480void __init pseries_lpar_read_hblkrm_characteristics(void)1481{1482static struct papr_sysparm_buf buf __initdata;1483int len, idx, bpsize;14841485if (!firmware_has_feature(FW_FEATURE_BLOCK_REMOVE))1486return;14871488if (papr_sysparm_get(PAPR_SYSPARM_TLB_BLOCK_INVALIDATE_ATTRS, &buf))1489return;14901491len = be16_to_cpu(buf.len);1492if (len > SPLPAR_TLB_BIC_MAXLENGTH) {1493pr_warn("%s too large returned buffer %d", __func__, len);1494return;1495}14961497idx = 0;1498while (idx < len) {1499u8 block_shift = buf.val[idx++];1500u32 block_size;1501unsigned int npsize;15021503if (!block_shift)1504break;15051506block_size = 1 << block_shift;15071508for (npsize = buf.val[idx++];1509npsize > 0 && idx < len; npsize--)1510check_lp_set_hblkrm((unsigned int)buf.val[idx++],1511block_size);1512}15131514for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)1515for (idx = 0; idx < MMU_PAGE_COUNT; idx++)1516if (hblkrm_size[bpsize][idx])1517pr_info("H_BLOCK_REMOVE supports base psize:%d psize:%d block size:%d",1518bpsize, idx, hblkrm_size[bpsize][idx]);1519}15201521/*1522* Take a spinlock around flushes to avoid bouncing the hypervisor tlbie1523* lock.1524*/1525static void pSeries_lpar_flush_hash_range(unsigned long number, int local)1526{1527unsigned long vpn;1528unsigned long i, pix, rc;1529unsigned long flags = 0;1530struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);1531int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);1532unsigned long param[PLPAR_HCALL9_BUFSIZE];1533unsigned long index, shift, slot;1534real_pte_t pte;1535int psize, ssize;15361537if (lock_tlbie)1538spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);15391540if (is_supported_hlbkrm(batch->psize, batch->psize)) {1541do_block_remove(number, batch, param);1542goto out;1543}15441545psize = batch->psize;1546ssize = batch->ssize;1547pix = 0;1548for (i = 0; i < number; i++) {1549vpn = batch->vpn[i];1550pte = batch->pte[i];1551pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {1552slot = compute_slot(pte, vpn, index, shift, ssize);1553if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {1554/*1555* lpar doesn't use the passed actual page size1556*/1557pSeries_lpar_hpte_invalidate(slot, vpn, psize,15580, ssize, local);1559} else {1560param[pix] = HBR_REQUEST | HBR_AVPN | slot;1561param[pix+1] = hpte_encode_avpn(vpn, psize,1562ssize);1563pix += 2;1564if (pix == 8) {1565rc = plpar_hcall9(H_BULK_REMOVE, param,1566param[0], param[1], param[2],1567param[3], param[4], param[5],1568param[6], param[7]);1569BUG_ON(rc != H_SUCCESS);1570pix = 0;1571}1572}1573} pte_iterate_hashed_end();1574}1575if (pix) {1576param[pix] = HBR_END;1577rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],1578param[2], param[3], param[4], param[5],1579param[6], param[7]);1580BUG_ON(rc != H_SUCCESS);1581}15821583out:1584if (lock_tlbie)1585spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);1586}15871588static int __init disable_bulk_remove(char *str)1589{1590if (strcmp(str, "off") == 0 &&1591firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {1592pr_info("Disabling BULK_REMOVE firmware feature");1593powerpc_firmware_features &= ~FW_FEATURE_BULK_REMOVE;1594}1595return 1;1596}15971598__setup("bulk_remove=", disable_bulk_remove);15991600#define HPT_RESIZE_TIMEOUT 10000 /* ms */16011602struct hpt_resize_state {1603unsigned long shift;1604int commit_rc;1605};16061607static int pseries_lpar_resize_hpt_commit(void *data)1608{1609struct hpt_resize_state *state = data;16101611state->commit_rc = plpar_resize_hpt_commit(0, state->shift);1612if (state->commit_rc != H_SUCCESS)1613return -EIO;16141615/* Hypervisor has transitioned the HTAB, update our globals */1616ppc64_pft_size = state->shift;1617htab_size_bytes = 1UL << ppc64_pft_size;1618htab_hash_mask = (htab_size_bytes >> 7) - 1;16191620return 0;1621}16221623/*1624* Must be called in process context. The caller must hold the1625* cpus_lock.1626*/1627static int pseries_lpar_resize_hpt(unsigned long shift)1628{1629struct hpt_resize_state state = {1630.shift = shift,1631.commit_rc = H_FUNCTION,1632};1633unsigned int delay, total_delay = 0;1634int rc;1635ktime_t t0, t1, t2;16361637might_sleep();16381639if (!firmware_has_feature(FW_FEATURE_HPT_RESIZE))1640return -ENODEV;16411642pr_info("Attempting to resize HPT to shift %lu\n", shift);16431644t0 = ktime_get();16451646rc = plpar_resize_hpt_prepare(0, shift);1647while (H_IS_LONG_BUSY(rc)) {1648delay = get_longbusy_msecs(rc);1649total_delay += delay;1650if (total_delay > HPT_RESIZE_TIMEOUT) {1651/* prepare with shift==0 cancels an in-progress resize */1652rc = plpar_resize_hpt_prepare(0, 0);1653if (rc != H_SUCCESS)1654pr_warn("Unexpected error %d cancelling timed out HPT resize\n",1655rc);1656return -ETIMEDOUT;1657}1658msleep(delay);1659rc = plpar_resize_hpt_prepare(0, shift);1660}16611662switch (rc) {1663case H_SUCCESS:1664/* Continue on */1665break;16661667case H_PARAMETER:1668pr_warn("Invalid argument from H_RESIZE_HPT_PREPARE\n");1669return -EINVAL;1670case H_RESOURCE:1671pr_warn("Operation not permitted from H_RESIZE_HPT_PREPARE\n");1672return -EPERM;1673default:1674pr_warn("Unexpected error %d from H_RESIZE_HPT_PREPARE\n", rc);1675return -EIO;1676}16771678t1 = ktime_get();16791680rc = stop_machine_cpuslocked(pseries_lpar_resize_hpt_commit,1681&state, NULL);16821683t2 = ktime_get();16841685if (rc != 0) {1686switch (state.commit_rc) {1687case H_PTEG_FULL:1688return -ENOSPC;16891690default:1691pr_warn("Unexpected error %d from H_RESIZE_HPT_COMMIT\n",1692state.commit_rc);1693return -EIO;1694};1695}16961697pr_info("HPT resize to shift %lu complete (%lld ms / %lld ms)\n",1698shift, (long long) ktime_ms_delta(t1, t0),1699(long long) ktime_ms_delta(t2, t1));17001701return 0;1702}17031704void __init hpte_init_pseries(void)1705{1706mmu_hash_ops.hpte_invalidate = pSeries_lpar_hpte_invalidate;1707mmu_hash_ops.hpte_updatepp = pSeries_lpar_hpte_updatepp;1708mmu_hash_ops.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;1709mmu_hash_ops.hpte_insert = pSeries_lpar_hpte_insert;1710mmu_hash_ops.hpte_remove = pSeries_lpar_hpte_remove;1711mmu_hash_ops.hpte_removebolted = pSeries_lpar_hpte_removebolted;1712mmu_hash_ops.flush_hash_range = pSeries_lpar_flush_hash_range;1713mmu_hash_ops.hpte_clear_all = pseries_hpte_clear_all;1714mmu_hash_ops.hugepage_invalidate = pSeries_lpar_hugepage_invalidate;17151716if (firmware_has_feature(FW_FEATURE_HPT_RESIZE))1717mmu_hash_ops.resize_hpt = pseries_lpar_resize_hpt;17181719/*1720* On POWER9, we need to do a H_REGISTER_PROC_TBL hcall1721* to inform the hypervisor that we wish to use the HPT.1722*/1723if (cpu_has_feature(CPU_FTR_ARCH_300))1724pseries_lpar_register_process_table(0, 0, 0);1725}1726#endif /* CONFIG_PPC_64S_HASH_MMU */17271728#ifdef CONFIG_PPC_RADIX_MMU1729void __init radix_init_pseries(void)1730{1731pr_info("Using radix MMU under hypervisor\n");17321733pseries_lpar_register_process_table(__pa(process_tb),17340, PRTB_SIZE_SHIFT - 12);1735}1736#endif17371738#ifdef CONFIG_PPC_SMLPAR1739#define CMO_FREE_HINT_DEFAULT 11740static int cmo_free_hint_flag = CMO_FREE_HINT_DEFAULT;17411742static int __init cmo_free_hint(char *str)1743{1744char *parm;1745parm = strstrip(str);17461747if (strcasecmp(parm, "no") == 0 || strcasecmp(parm, "off") == 0) {1748pr_info("%s: CMO free page hinting is not active.\n", __func__);1749cmo_free_hint_flag = 0;1750return 1;1751}17521753cmo_free_hint_flag = 1;1754pr_info("%s: CMO free page hinting is active.\n", __func__);17551756if (strcasecmp(parm, "yes") == 0 || strcasecmp(parm, "on") == 0)1757return 1;17581759return 0;1760}17611762__setup("cmo_free_hint=", cmo_free_hint);17631764static void pSeries_set_page_state(struct page *page, int order,1765unsigned long state)1766{1767int i, j;1768unsigned long cmo_page_sz, addr;17691770cmo_page_sz = cmo_get_page_size();1771addr = __pa((unsigned long)page_address(page));17721773for (i = 0; i < (1 << order); i++, addr += PAGE_SIZE) {1774for (j = 0; j < PAGE_SIZE; j += cmo_page_sz)1775plpar_hcall_norets(H_PAGE_INIT, state, addr + j, 0);1776}1777}17781779void arch_free_page(struct page *page, int order)1780{1781if (radix_enabled())1782return;1783if (!cmo_free_hint_flag || !firmware_has_feature(FW_FEATURE_CMO))1784return;17851786pSeries_set_page_state(page, order, H_PAGE_SET_UNUSED);1787}1788EXPORT_SYMBOL(arch_free_page);17891790#endif /* CONFIG_PPC_SMLPAR */1791#endif /* CONFIG_PPC_BOOK3S_64 */17921793#ifdef CONFIG_TRACEPOINTS1794#ifdef CONFIG_JUMP_LABEL1795struct static_key hcall_tracepoint_key = STATIC_KEY_INIT;17961797int hcall_tracepoint_regfunc(void)1798{1799static_key_slow_inc(&hcall_tracepoint_key);1800return 0;1801}18021803void hcall_tracepoint_unregfunc(void)1804{1805static_key_slow_dec(&hcall_tracepoint_key);1806}1807#else1808/*1809* We optimise our hcall path by placing hcall_tracepoint_refcount1810* directly in the TOC so we can check if the hcall tracepoints are1811* enabled via a single load.1812*/18131814/* NB: reg/unreg are called while guarded with the tracepoints_mutex */1815extern long hcall_tracepoint_refcount;18161817int hcall_tracepoint_regfunc(void)1818{1819hcall_tracepoint_refcount++;1820return 0;1821}18221823void hcall_tracepoint_unregfunc(void)1824{1825hcall_tracepoint_refcount--;1826}1827#endif18281829/*1830* Keep track of hcall tracing depth and prevent recursion. Warn if any is1831* detected because it may indicate a problem. This will not catch all1832* problems with tracing code making hcalls, because the tracing might have1833* been invoked from a non-hcall, so the first hcall could recurse into it1834* without warning here, but this better than nothing.1835*1836* Hcalls with specific problems being traced should use the _notrace1837* plpar_hcall variants.1838*/1839static DEFINE_PER_CPU(unsigned int, hcall_trace_depth);184018411842notrace void __trace_hcall_entry(unsigned long opcode, unsigned long *args)1843{1844unsigned long flags;1845unsigned int *depth;18461847local_irq_save(flags);18481849depth = this_cpu_ptr(&hcall_trace_depth);18501851if (WARN_ON_ONCE(*depth))1852goto out;18531854(*depth)++;1855preempt_disable();1856trace_hcall_entry(opcode, args);1857(*depth)--;18581859out:1860local_irq_restore(flags);1861}18621863notrace void __trace_hcall_exit(long opcode, long retval, unsigned long *retbuf)1864{1865unsigned long flags;1866unsigned int *depth;18671868local_irq_save(flags);18691870depth = this_cpu_ptr(&hcall_trace_depth);18711872if (*depth) /* Don't warn again on the way out */1873goto out;18741875(*depth)++;1876trace_hcall_exit(opcode, retval, retbuf);1877preempt_enable();1878(*depth)--;18791880out:1881local_irq_restore(flags);1882}1883#endif18841885/**1886* h_get_mpp1887* H_GET_MPP hcall returns info in 7 parms1888*/1889long h_get_mpp(struct hvcall_mpp_data *mpp_data)1890{1891unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};1892long rc;18931894rc = plpar_hcall9(H_GET_MPP, retbuf);18951896mpp_data->entitled_mem = retbuf[0];1897mpp_data->mapped_mem = retbuf[1];18981899mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;1900mpp_data->pool_num = retbuf[2] & 0xffff;19011902mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;1903mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;1904mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffffUL;19051906mpp_data->pool_size = retbuf[4];1907mpp_data->loan_request = retbuf[5];1908mpp_data->backing_mem = retbuf[6];19091910return rc;1911}1912EXPORT_SYMBOL(h_get_mpp);19131914int h_get_mpp_x(struct hvcall_mpp_x_data *mpp_x_data)1915{1916int rc;1917unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };19181919rc = plpar_hcall9(H_GET_MPP_X, retbuf);19201921mpp_x_data->coalesced_bytes = retbuf[0];1922mpp_x_data->pool_coalesced_bytes = retbuf[1];1923mpp_x_data->pool_purr_cycles = retbuf[2];1924mpp_x_data->pool_spurr_cycles = retbuf[3];19251926return rc;1927}19281929#ifdef CONFIG_PPC_64S_HASH_MMU1930static unsigned long __init vsid_unscramble(unsigned long vsid, int ssize)1931{1932unsigned long protovsid;1933unsigned long va_bits = VA_BITS;1934unsigned long modinv, vsid_modulus;1935unsigned long max_mod_inv, tmp_modinv;19361937if (!mmu_has_feature(MMU_FTR_68_BIT_VA))1938va_bits = 65;19391940if (ssize == MMU_SEGSIZE_256M) {1941modinv = VSID_MULINV_256M;1942vsid_modulus = ((1UL << (va_bits - SID_SHIFT)) - 1);1943} else {1944modinv = VSID_MULINV_1T;1945vsid_modulus = ((1UL << (va_bits - SID_SHIFT_1T)) - 1);1946}19471948/*1949* vsid outside our range.1950*/1951if (vsid >= vsid_modulus)1952return 0;19531954/*1955* If modinv is the modular multiplicate inverse of (x % vsid_modulus)1956* and vsid = (protovsid * x) % vsid_modulus, then we say:1957* protovsid = (vsid * modinv) % vsid_modulus1958*/19591960/* Check if (vsid * modinv) overflow (63 bits) */1961max_mod_inv = 0x7fffffffffffffffull / vsid;1962if (modinv < max_mod_inv)1963return (vsid * modinv) % vsid_modulus;19641965tmp_modinv = modinv/max_mod_inv;1966modinv %= max_mod_inv;19671968protovsid = (((vsid * max_mod_inv) % vsid_modulus) * tmp_modinv) % vsid_modulus;1969protovsid = (protovsid + vsid * modinv) % vsid_modulus;19701971return protovsid;1972}19731974static int __init reserve_vrma_context_id(void)1975{1976unsigned long protovsid;19771978/*1979* Reserve context ids which map to reserved virtual addresses. For now1980* we only reserve the context id which maps to the VRMA VSID. We ignore1981* the addresses in "ibm,adjunct-virtual-addresses" because we don't1982* enable adjunct support via the "ibm,client-architecture-support"1983* interface.1984*/1985protovsid = vsid_unscramble(VRMA_VSID, MMU_SEGSIZE_1T);1986hash__reserve_context_id(protovsid >> ESID_BITS_1T);1987return 0;1988}1989machine_device_initcall(pseries, reserve_vrma_context_id);1990#endif19911992#ifdef CONFIG_DEBUG_FS1993/* debugfs file interface for vpa data */1994static ssize_t vpa_file_read(struct file *filp, char __user *buf, size_t len,1995loff_t *pos)1996{1997int cpu = (long)filp->private_data;1998struct lppaca *lppaca = &lppaca_of(cpu);19992000return simple_read_from_buffer(buf, len, pos, lppaca,2001sizeof(struct lppaca));2002}20032004static const struct file_operations vpa_fops = {2005.open = simple_open,2006.read = vpa_file_read,2007.llseek = default_llseek,2008};20092010static int __init vpa_debugfs_init(void)2011{2012char name[16];2013long i;2014struct dentry *vpa_dir;20152016if (!firmware_has_feature(FW_FEATURE_SPLPAR))2017return 0;20182019vpa_dir = debugfs_create_dir("vpa", arch_debugfs_dir);20202021/* set up the per-cpu vpa file*/2022for_each_possible_cpu(i) {2023sprintf(name, "cpu-%ld", i);2024debugfs_create_file(name, 0400, vpa_dir, (void *)i, &vpa_fops);2025}20262027return 0;2028}2029machine_arch_initcall(pseries, vpa_debugfs_init);2030#endif /* CONFIG_DEBUG_FS */203120322033