// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright 2012 Michael Ellerman, IBM Corporation.3* Copyright 2012 Benjamin Herrenschmidt, IBM Corporation4*/56#include <linux/kernel.h>7#include <linux/kvm_host.h>8#include <linux/err.h>9#include <linux/kernel_stat.h>10#include <linux/pgtable.h>1112#include <asm/kvm_book3s.h>13#include <asm/kvm_ppc.h>14#include <asm/hvcall.h>15#include <asm/xics.h>16#include <asm/synch.h>17#include <asm/cputhreads.h>18#include <asm/ppc-opcode.h>19#include <asm/pnv-pci.h>20#include <asm/opal.h>21#include <asm/smp.h>2223#include "book3s_xics.h"2425#define DEBUG_PASSUP2627int h_ipi_redirect = 1;28EXPORT_SYMBOL(h_ipi_redirect);29int kvm_irq_bypass = 1;30EXPORT_SYMBOL(kvm_irq_bypass);3132static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,33u32 new_irq, bool check_resend);34static int xics_opal_set_server(unsigned int hw_irq, int server_cpu);3536/* -- ICS routines -- */37static void ics_rm_check_resend(struct kvmppc_xics *xics,38struct kvmppc_ics *ics, struct kvmppc_icp *icp)39{40int i;4142for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {43struct ics_irq_state *state = &ics->irq_state[i];44if (state->resend)45icp_rm_deliver_irq(xics, icp, state->number, true);46}4748}4950/* -- ICP routines -- */5152#ifdef CONFIG_SMP53static inline void icp_send_hcore_msg(int hcore, struct kvm_vcpu *vcpu)54{55int hcpu;5657hcpu = hcore << threads_shift;58kvmppc_host_rm_ops_hv->rm_core[hcore].rm_data = vcpu;59smp_muxed_ipi_set_message(hcpu, PPC_MSG_RM_HOST_ACTION);60kvmppc_set_host_ipi(hcpu);61smp_mb();62kvmhv_rm_send_ipi(hcpu);63}64#else65static inline void icp_send_hcore_msg(int hcore, struct kvm_vcpu *vcpu) { }66#endif6768/*69* We start the search from our current CPU Id in the core map70* and go in a circle until we get back to our ID looking for a71* core that is running in host context and that hasn't already72* been targeted for another rm_host_ops.73*74* In the future, could consider using a fairer algorithm (one75* that distributes the IPIs better)76*77* Returns -1, if no CPU could be found in the host78* Else, returns a CPU Id which has been reserved for use79*/80static inline int grab_next_hostcore(int start,81struct kvmppc_host_rm_core *rm_core, int max, int action)82{83bool success;84int core;85union kvmppc_rm_state old, new;8687for (core = start + 1; core < max; core++) {88old = new = READ_ONCE(rm_core[core].rm_state);8990if (!old.in_host || old.rm_action)91continue;9293/* Try to grab this host core if not taken already. */94new.rm_action = action;9596success = cmpxchg64(&rm_core[core].rm_state.raw,97old.raw, new.raw) == old.raw;98if (success) {99/*100* Make sure that the store to the rm_action is made101* visible before we return to caller (and the102* subsequent store to rm_data) to synchronize with103* the IPI handler.104*/105smp_wmb();106return core;107}108}109110return -1;111}112113static inline int find_available_hostcore(int action)114{115int core;116int my_core = smp_processor_id() >> threads_shift;117struct kvmppc_host_rm_core *rm_core = kvmppc_host_rm_ops_hv->rm_core;118119core = grab_next_hostcore(my_core, rm_core, cpu_nr_cores(), action);120if (core == -1)121core = grab_next_hostcore(core, rm_core, my_core, action);122123return core;124}125126static void icp_rm_set_vcpu_irq(struct kvm_vcpu *vcpu,127struct kvm_vcpu *this_vcpu)128{129struct kvmppc_icp *this_icp = this_vcpu->arch.icp;130int cpu;131int hcore;132133/* Mark the target VCPU as having an interrupt pending */134vcpu->stat.queue_intr++;135set_bit(BOOK3S_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);136137/* Kick self ? Just set MER and return */138if (vcpu == this_vcpu) {139mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_MER);140return;141}142143/*144* Check if the core is loaded,145* if not, find an available host core to post to wake the VCPU,146* if we can't find one, set up state to eventually return too hard.147*/148cpu = vcpu->arch.thread_cpu;149if (cpu < 0 || cpu >= nr_cpu_ids) {150hcore = -1;151if (kvmppc_host_rm_ops_hv && h_ipi_redirect)152hcore = find_available_hostcore(XICS_RM_KICK_VCPU);153if (hcore != -1) {154icp_send_hcore_msg(hcore, vcpu);155} else {156this_icp->rm_action |= XICS_RM_KICK_VCPU;157this_icp->rm_kick_target = vcpu;158}159return;160}161162smp_mb();163kvmhv_rm_send_ipi(cpu);164}165166static void icp_rm_clr_vcpu_irq(struct kvm_vcpu *vcpu)167{168/* Note: Only called on self ! */169clear_bit(BOOK3S_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);170mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~LPCR_MER);171}172173static inline bool icp_rm_try_update(struct kvmppc_icp *icp,174union kvmppc_icp_state old,175union kvmppc_icp_state new)176{177struct kvm_vcpu *this_vcpu = local_paca->kvm_hstate.kvm_vcpu;178bool success;179180/* Calculate new output value */181new.out_ee = (new.xisr && (new.pending_pri < new.cppr));182183/* Attempt atomic update */184success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;185if (!success)186goto bail;187188/*189* Check for output state update190*191* Note that this is racy since another processor could be updating192* the state already. This is why we never clear the interrupt output193* here, we only ever set it. The clear only happens prior to doing194* an update and only by the processor itself. Currently we do it195* in Accept (H_XIRR) and Up_Cppr (H_XPPR).196*197* We also do not try to figure out whether the EE state has changed,198* we unconditionally set it if the new state calls for it. The reason199* for that is that we opportunistically remove the pending interrupt200* flag when raising CPPR, so we need to set it back here if an201* interrupt is still pending.202*/203if (new.out_ee)204icp_rm_set_vcpu_irq(icp->vcpu, this_vcpu);205206/* Expose the state change for debug purposes */207this_vcpu->arch.icp->rm_dbgstate = new;208this_vcpu->arch.icp->rm_dbgtgt = icp->vcpu;209210bail:211return success;212}213214static inline int check_too_hard(struct kvmppc_xics *xics,215struct kvmppc_icp *icp)216{217return (xics->real_mode_dbg || icp->rm_action) ? H_TOO_HARD : H_SUCCESS;218}219220static void icp_rm_check_resend(struct kvmppc_xics *xics,221struct kvmppc_icp *icp)222{223u32 icsid;224225/* Order this load with the test for need_resend in the caller */226smp_rmb();227for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {228struct kvmppc_ics *ics = xics->ics[icsid];229230if (!test_and_clear_bit(icsid, icp->resend_map))231continue;232if (!ics)233continue;234ics_rm_check_resend(xics, ics, icp);235}236}237238static bool icp_rm_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,239u32 *reject)240{241union kvmppc_icp_state old_state, new_state;242bool success;243244do {245old_state = new_state = READ_ONCE(icp->state);246247*reject = 0;248249/* See if we can deliver */250success = new_state.cppr > priority &&251new_state.mfrr > priority &&252new_state.pending_pri > priority;253254/*255* If we can, check for a rejection and perform the256* delivery257*/258if (success) {259*reject = new_state.xisr;260new_state.xisr = irq;261new_state.pending_pri = priority;262} else {263/*264* If we failed to deliver we set need_resend265* so a subsequent CPPR state change causes us266* to try a new delivery.267*/268new_state.need_resend = true;269}270271} while (!icp_rm_try_update(icp, old_state, new_state));272273return success;274}275276static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,277u32 new_irq, bool check_resend)278{279struct ics_irq_state *state;280struct kvmppc_ics *ics;281u32 reject;282u16 src;283284/*285* This is used both for initial delivery of an interrupt and286* for subsequent rejection.287*288* Rejection can be racy vs. resends. We have evaluated the289* rejection in an atomic ICP transaction which is now complete,290* so potentially the ICP can already accept the interrupt again.291*292* So we need to retry the delivery. Essentially the reject path293* boils down to a failed delivery. Always.294*295* Now the interrupt could also have moved to a different target,296* thus we may need to re-do the ICP lookup as well297*/298299again:300/* Get the ICS state and lock it */301ics = kvmppc_xics_find_ics(xics, new_irq, &src);302if (!ics) {303/* Unsafe increment, but this does not need to be accurate */304xics->err_noics++;305return;306}307state = &ics->irq_state[src];308309/* Get a lock on the ICS */310arch_spin_lock(&ics->lock);311312/* Get our server */313if (!icp || state->server != icp->server_num) {314icp = kvmppc_xics_find_server(xics->kvm, state->server);315if (!icp) {316/* Unsafe increment again*/317xics->err_noicp++;318goto out;319}320}321322if (check_resend)323if (!state->resend)324goto out;325326/* Clear the resend bit of that interrupt */327state->resend = 0;328329/*330* If masked, bail out331*332* Note: PAPR doesn't mention anything about masked pending333* when doing a resend, only when doing a delivery.334*335* However that would have the effect of losing a masked336* interrupt that was rejected and isn't consistent with337* the whole masked_pending business which is about not338* losing interrupts that occur while masked.339*340* I don't differentiate normal deliveries and resends, this341* implementation will differ from PAPR and not lose such342* interrupts.343*/344if (state->priority == MASKED) {345state->masked_pending = 1;346goto out;347}348349/*350* Try the delivery, this will set the need_resend flag351* in the ICP as part of the atomic transaction if the352* delivery is not possible.353*354* Note that if successful, the new delivery might have itself355* rejected an interrupt that was "delivered" before we took the356* ics spin lock.357*358* In this case we do the whole sequence all over again for the359* new guy. We cannot assume that the rejected interrupt is less360* favored than the new one, and thus doesn't need to be delivered,361* because by the time we exit icp_rm_try_to_deliver() the target362* processor may well have already consumed & completed it, and thus363* the rejected interrupt might actually be already acceptable.364*/365if (icp_rm_try_to_deliver(icp, new_irq, state->priority, &reject)) {366/*367* Delivery was successful, did we reject somebody else ?368*/369if (reject && reject != XICS_IPI) {370arch_spin_unlock(&ics->lock);371icp->n_reject++;372new_irq = reject;373check_resend = 0;374goto again;375}376} else {377/*378* We failed to deliver the interrupt we need to set the379* resend map bit and mark the ICS state as needing a resend380*/381state->resend = 1;382383/*384* Make sure when checking resend, we don't miss the resend385* if resend_map bit is seen and cleared.386*/387smp_wmb();388set_bit(ics->icsid, icp->resend_map);389390/*391* If the need_resend flag got cleared in the ICP some time392* between icp_rm_try_to_deliver() atomic update and now, then393* we know it might have missed the resend_map bit. So we394* retry395*/396smp_mb();397if (!icp->state.need_resend) {398state->resend = 0;399arch_spin_unlock(&ics->lock);400check_resend = 0;401goto again;402}403}404out:405arch_spin_unlock(&ics->lock);406}407408static void icp_rm_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,409u8 new_cppr)410{411union kvmppc_icp_state old_state, new_state;412bool resend;413414/*415* This handles several related states in one operation:416*417* ICP State: Down_CPPR418*419* Load CPPR with new value and if the XISR is 0420* then check for resends:421*422* ICP State: Resend423*424* If MFRR is more favored than CPPR, check for IPIs425* and notify ICS of a potential resend. This is done426* asynchronously (when used in real mode, we will have427* to exit here).428*429* We do not handle the complete Check_IPI as documented430* here. In the PAPR, this state will be used for both431* Set_MFRR and Down_CPPR. However, we know that we aren't432* changing the MFRR state here so we don't need to handle433* the case of an MFRR causing a reject of a pending irq,434* this will have been handled when the MFRR was set in the435* first place.436*437* Thus we don't have to handle rejects, only resends.438*439* When implementing real mode for HV KVM, resend will lead to440* a H_TOO_HARD return and the whole transaction will be handled441* in virtual mode.442*/443do {444old_state = new_state = READ_ONCE(icp->state);445446/* Down_CPPR */447new_state.cppr = new_cppr;448449/*450* Cut down Resend / Check_IPI / IPI451*452* The logic is that we cannot have a pending interrupt453* trumped by an IPI at this point (see above), so we454* know that either the pending interrupt is already an455* IPI (in which case we don't care to override it) or456* it's either more favored than us or non existent457*/458if (new_state.mfrr < new_cppr &&459new_state.mfrr <= new_state.pending_pri) {460new_state.pending_pri = new_state.mfrr;461new_state.xisr = XICS_IPI;462}463464/* Latch/clear resend bit */465resend = new_state.need_resend;466new_state.need_resend = 0;467468} while (!icp_rm_try_update(icp, old_state, new_state));469470/*471* Now handle resend checks. Those are asynchronous to the ICP472* state update in HW (ie bus transactions) so we can handle them473* separately here as well.474*/475if (resend) {476icp->n_check_resend++;477icp_rm_check_resend(xics, icp);478}479}480481unsigned long xics_rm_h_xirr_x(struct kvm_vcpu *vcpu)482{483kvmppc_set_gpr(vcpu, 5, get_tb());484return xics_rm_h_xirr(vcpu);485}486487unsigned long xics_rm_h_xirr(struct kvm_vcpu *vcpu)488{489union kvmppc_icp_state old_state, new_state;490struct kvmppc_xics *xics = vcpu->kvm->arch.xics;491struct kvmppc_icp *icp = vcpu->arch.icp;492u32 xirr;493494if (!xics || !xics->real_mode)495return H_TOO_HARD;496497/* First clear the interrupt */498icp_rm_clr_vcpu_irq(icp->vcpu);499500/*501* ICP State: Accept_Interrupt502*503* Return the pending interrupt (if any) along with the504* current CPPR, then clear the XISR & set CPPR to the505* pending priority506*/507do {508old_state = new_state = READ_ONCE(icp->state);509510xirr = old_state.xisr | (((u32)old_state.cppr) << 24);511if (!old_state.xisr)512break;513new_state.cppr = new_state.pending_pri;514new_state.pending_pri = 0xff;515new_state.xisr = 0;516517} while (!icp_rm_try_update(icp, old_state, new_state));518519/* Return the result in GPR4 */520kvmppc_set_gpr(vcpu, 4, xirr);521522return check_too_hard(xics, icp);523}524525int xics_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,526unsigned long mfrr)527{528union kvmppc_icp_state old_state, new_state;529struct kvmppc_xics *xics = vcpu->kvm->arch.xics;530struct kvmppc_icp *icp, *this_icp = vcpu->arch.icp;531u32 reject;532bool resend;533bool local;534535if (!xics || !xics->real_mode)536return H_TOO_HARD;537538local = this_icp->server_num == server;539if (local)540icp = this_icp;541else542icp = kvmppc_xics_find_server(vcpu->kvm, server);543if (!icp)544return H_PARAMETER;545546/*547* ICP state: Set_MFRR548*549* If the CPPR is more favored than the new MFRR, then550* nothing needs to be done as there can be no XISR to551* reject.552*553* ICP state: Check_IPI554*555* If the CPPR is less favored, then we might be replacing556* an interrupt, and thus need to possibly reject it.557*558* ICP State: IPI559*560* Besides rejecting any pending interrupts, we also561* update XISR and pending_pri to mark IPI as pending.562*563* PAPR does not describe this state, but if the MFRR is being564* made less favored than its earlier value, there might be565* a previously-rejected interrupt needing to be resent.566* Ideally, we would want to resend only if567* prio(pending_interrupt) < mfrr &&568* prio(pending_interrupt) < cppr569* where pending interrupt is the one that was rejected. But570* we don't have that state, so we simply trigger a resend571* whenever the MFRR is made less favored.572*/573do {574old_state = new_state = READ_ONCE(icp->state);575576/* Set_MFRR */577new_state.mfrr = mfrr;578579/* Check_IPI */580reject = 0;581resend = false;582if (mfrr < new_state.cppr) {583/* Reject a pending interrupt if not an IPI */584if (mfrr <= new_state.pending_pri) {585reject = new_state.xisr;586new_state.pending_pri = mfrr;587new_state.xisr = XICS_IPI;588}589}590591if (mfrr > old_state.mfrr) {592resend = new_state.need_resend;593new_state.need_resend = 0;594}595} while (!icp_rm_try_update(icp, old_state, new_state));596597/* Handle reject in real mode */598if (reject && reject != XICS_IPI) {599this_icp->n_reject++;600icp_rm_deliver_irq(xics, icp, reject, false);601}602603/* Handle resends in real mode */604if (resend) {605this_icp->n_check_resend++;606icp_rm_check_resend(xics, icp);607}608609return check_too_hard(xics, this_icp);610}611612int xics_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)613{614union kvmppc_icp_state old_state, new_state;615struct kvmppc_xics *xics = vcpu->kvm->arch.xics;616struct kvmppc_icp *icp = vcpu->arch.icp;617u32 reject;618619if (!xics || !xics->real_mode)620return H_TOO_HARD;621622/*623* ICP State: Set_CPPR624*625* We can safely compare the new value with the current626* value outside of the transaction as the CPPR is only627* ever changed by the processor on itself628*/629if (cppr > icp->state.cppr) {630icp_rm_down_cppr(xics, icp, cppr);631goto bail;632} else if (cppr == icp->state.cppr)633return H_SUCCESS;634635/*636* ICP State: Up_CPPR637*638* The processor is raising its priority, this can result639* in a rejection of a pending interrupt:640*641* ICP State: Reject_Current642*643* We can remove EE from the current processor, the update644* transaction will set it again if needed645*/646icp_rm_clr_vcpu_irq(icp->vcpu);647648do {649old_state = new_state = READ_ONCE(icp->state);650651reject = 0;652new_state.cppr = cppr;653654if (cppr <= new_state.pending_pri) {655reject = new_state.xisr;656new_state.xisr = 0;657new_state.pending_pri = 0xff;658}659660} while (!icp_rm_try_update(icp, old_state, new_state));661662/*663* Check for rejects. They are handled by doing a new delivery664* attempt (see comments in icp_rm_deliver_irq).665*/666if (reject && reject != XICS_IPI) {667icp->n_reject++;668icp_rm_deliver_irq(xics, icp, reject, false);669}670bail:671return check_too_hard(xics, icp);672}673674static int ics_rm_eoi(struct kvm_vcpu *vcpu, u32 irq)675{676struct kvmppc_xics *xics = vcpu->kvm->arch.xics;677struct kvmppc_icp *icp = vcpu->arch.icp;678struct kvmppc_ics *ics;679struct ics_irq_state *state;680u16 src;681u32 pq_old, pq_new;682683/*684* ICS EOI handling: For LSI, if P bit is still set, we need to685* resend it.686*687* For MSI, we move Q bit into P (and clear Q). If it is set,688* resend it.689*/690691ics = kvmppc_xics_find_ics(xics, irq, &src);692if (!ics)693goto bail;694695state = &ics->irq_state[src];696697if (state->lsi)698pq_new = state->pq_state;699else700do {701pq_old = state->pq_state;702pq_new = pq_old >> 1;703} while (cmpxchg(&state->pq_state, pq_old, pq_new) != pq_old);704705if (pq_new & PQ_PRESENTED)706icp_rm_deliver_irq(xics, NULL, irq, false);707708if (!hlist_empty(&vcpu->kvm->irq_ack_notifier_list)) {709icp->rm_action |= XICS_RM_NOTIFY_EOI;710icp->rm_eoied_irq = irq;711}712713/* Handle passthrough interrupts */714if (state->host_irq) {715++vcpu->stat.pthru_all;716if (state->intr_cpu != -1) {717int pcpu = raw_smp_processor_id();718719pcpu = cpu_first_thread_sibling(pcpu);720++vcpu->stat.pthru_host;721if (state->intr_cpu != pcpu) {722++vcpu->stat.pthru_bad_aff;723xics_opal_set_server(state->host_irq, pcpu);724}725state->intr_cpu = -1;726}727}728729bail:730return check_too_hard(xics, icp);731}732733int xics_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)734{735struct kvmppc_xics *xics = vcpu->kvm->arch.xics;736struct kvmppc_icp *icp = vcpu->arch.icp;737u32 irq = xirr & 0x00ffffff;738739if (!xics || !xics->real_mode)740return H_TOO_HARD;741742/*743* ICP State: EOI744*745* Note: If EOI is incorrectly used by SW to lower the CPPR746* value (ie more favored), we do not check for rejection of747* a pending interrupt, this is a SW error and PAPR specifies748* that we don't have to deal with it.749*750* The sending of an EOI to the ICS is handled after the751* CPPR update752*753* ICP State: Down_CPPR which we handle754* in a separate function as it's shared with H_CPPR.755*/756icp_rm_down_cppr(xics, icp, xirr >> 24);757758/* IPIs have no EOI */759if (irq == XICS_IPI)760return check_too_hard(xics, icp);761762return ics_rm_eoi(vcpu, irq);763}764765static unsigned long eoi_rc;766767static void icp_eoi(struct irq_data *d, u32 hwirq, __be32 xirr, bool *again)768{769void __iomem *xics_phys;770int64_t rc;771772rc = pnv_opal_pci_msi_eoi(d);773774if (rc)775eoi_rc = rc;776777iosync();778779/* EOI it */780xics_phys = local_paca->kvm_hstate.xics_phys;781if (xics_phys) {782__raw_rm_writel(xirr, xics_phys + XICS_XIRR);783} else {784rc = opal_int_eoi(be32_to_cpu(xirr));785*again = rc > 0;786}787}788789static int xics_opal_set_server(unsigned int hw_irq, int server_cpu)790{791unsigned int mangle_cpu = get_hard_smp_processor_id(server_cpu) << 2;792793return opal_set_xive(hw_irq, mangle_cpu, DEFAULT_PRIORITY);794}795796/*797* Increment a per-CPU 32-bit unsigned integer variable.798* Safe to call in real-mode. Handles vmalloc'ed addresses799*800* ToDo: Make this work for any integral type801*/802803static inline void this_cpu_inc_rm(unsigned int __percpu *addr)804{805unsigned long l;806unsigned int *raddr;807int cpu = smp_processor_id();808809raddr = per_cpu_ptr(addr, cpu);810l = (unsigned long)raddr;811812if (get_region_id(l) == VMALLOC_REGION_ID) {813l = vmalloc_to_phys(raddr);814raddr = (unsigned int *)l;815}816++*raddr;817}818819/*820* We don't try to update the flags in the irq_desc 'istate' field in821* here as would happen in the normal IRQ handling path for several reasons:822* - state flags represent internal IRQ state and are not expected to be823* updated outside the IRQ subsystem824* - more importantly, these are useful for edge triggered interrupts,825* IRQ probing, etc., but we are only handling MSI/MSIx interrupts here826* and these states shouldn't apply to us.827*828* However, we do update irq_stats - we somewhat duplicate the code in829* kstat_incr_irqs_this_cpu() for this since this function is defined830* in irq/internal.h which we don't want to include here.831* The only difference is that desc->kstat_irqs is an allocated per CPU832* variable and could have been vmalloc'ed, so we can't directly833* call __this_cpu_inc() on it. The kstat structure is a static834* per CPU variable and it should be accessible by real-mode KVM.835*836*/837static void kvmppc_rm_handle_irq_desc(struct irq_desc *desc)838{839this_cpu_inc_rm(&desc->kstat_irqs->cnt);840__this_cpu_inc(kstat.irqs_sum);841}842843long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu,844__be32 xirr,845struct kvmppc_irq_map *irq_map,846struct kvmppc_passthru_irqmap *pimap,847bool *again)848{849struct kvmppc_xics *xics;850struct kvmppc_icp *icp;851struct kvmppc_ics *ics;852struct ics_irq_state *state;853u32 irq;854u16 src;855u32 pq_old, pq_new;856857irq = irq_map->v_hwirq;858xics = vcpu->kvm->arch.xics;859icp = vcpu->arch.icp;860861kvmppc_rm_handle_irq_desc(irq_map->desc);862863ics = kvmppc_xics_find_ics(xics, irq, &src);864if (!ics)865return 2;866867state = &ics->irq_state[src];868869/* only MSIs register bypass producers, so it must be MSI here */870do {871pq_old = state->pq_state;872pq_new = ((pq_old << 1) & 3) | PQ_PRESENTED;873} while (cmpxchg(&state->pq_state, pq_old, pq_new) != pq_old);874875/* Test P=1, Q=0, this is the only case where we present */876if (pq_new == PQ_PRESENTED)877icp_rm_deliver_irq(xics, icp, irq, false);878879/* EOI the interrupt */880icp_eoi(irq_desc_get_irq_data(irq_map->desc), irq_map->r_hwirq, xirr, again);881882if (check_too_hard(xics, icp) == H_TOO_HARD)883return 2;884else885return -2;886}887888/* --- Non-real mode XICS-related built-in routines --- */889890/*891* Host Operations poked by RM KVM892*/893static void rm_host_ipi_action(int action, void *data)894{895switch (action) {896case XICS_RM_KICK_VCPU:897kvmppc_host_rm_ops_hv->vcpu_kick(data);898break;899default:900WARN(1, "Unexpected rm_action=%d data=%p\n", action, data);901break;902}903904}905906void kvmppc_xics_ipi_action(void)907{908int core;909unsigned int cpu = smp_processor_id();910struct kvmppc_host_rm_core *rm_corep;911912core = cpu >> threads_shift;913rm_corep = &kvmppc_host_rm_ops_hv->rm_core[core];914915if (rm_corep->rm_data) {916rm_host_ipi_action(rm_corep->rm_state.rm_action,917rm_corep->rm_data);918/* Order these stores against the real mode KVM */919rm_corep->rm_data = NULL;920smp_wmb();921rm_corep->rm_state.rm_action = 0;922}923}924925926