Path: blob/master/arch/powerpc/sysdev/xics/xics-common.c
26493 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright 2011 IBM Corporation.3*/4#include <linux/types.h>5#include <linux/threads.h>6#include <linux/kernel.h>7#include <linux/irq.h>8#include <linux/irqdomain.h>9#include <linux/debugfs.h>10#include <linux/smp.h>11#include <linux/interrupt.h>12#include <linux/seq_file.h>13#include <linux/init.h>14#include <linux/cpu.h>15#include <linux/of.h>16#include <linux/slab.h>17#include <linux/spinlock.h>18#include <linux/delay.h>1920#include <asm/io.h>21#include <asm/smp.h>22#include <asm/machdep.h>23#include <asm/irq.h>24#include <asm/errno.h>25#include <asm/rtas.h>26#include <asm/xics.h>27#include <asm/firmware.h>2829/* Globals common to all ICP/ICS implementations */30const struct icp_ops *icp_ops;3132unsigned int xics_default_server = 0xff;33unsigned int xics_default_distrib_server = 0;34unsigned int xics_interrupt_server_size = 8;3536DEFINE_PER_CPU(struct xics_cppr, xics_cppr);3738struct irq_domain *xics_host;3940static struct ics *xics_ics;4142void xics_update_irq_servers(void)43{44int i, j;45struct device_node *np;46u32 ilen;47const __be32 *ireg;48u32 hcpuid;4950/* Find the server numbers for the boot cpu. */51np = of_get_cpu_node(boot_cpuid, NULL);52BUG_ON(!np);5354hcpuid = get_hard_smp_processor_id(boot_cpuid);55xics_default_server = xics_default_distrib_server = hcpuid;5657pr_devel("xics: xics_default_server = 0x%x\n", xics_default_server);5859ireg = of_get_property(np, "ibm,ppc-interrupt-gserver#s", &ilen);60if (!ireg) {61of_node_put(np);62return;63}6465i = ilen / sizeof(int);6667/* Global interrupt distribution server is specified in the last68* entry of "ibm,ppc-interrupt-gserver#s" property. Get the last69* entry fom this property for current boot cpu id and use it as70* default distribution server71*/72for (j = 0; j < i; j += 2) {73if (be32_to_cpu(ireg[j]) == hcpuid) {74xics_default_distrib_server = be32_to_cpu(ireg[j+1]);75break;76}77}78pr_devel("xics: xics_default_distrib_server = 0x%x\n",79xics_default_distrib_server);80of_node_put(np);81}8283/* GIQ stuff, currently only supported on RTAS setups, will have84* to be sorted properly for bare metal85*/86void xics_set_cpu_giq(unsigned int gserver, unsigned int join)87{88#ifdef CONFIG_PPC_RTAS89int index;90int status;9192if (!rtas_indicator_present(GLOBAL_INTERRUPT_QUEUE, NULL))93return;9495index = (1UL << xics_interrupt_server_size) - 1 - gserver;9697status = rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE, index, join);9899WARN(status < 0, "set-indicator(%d, %d, %u) returned %d\n",100GLOBAL_INTERRUPT_QUEUE, index, join, status);101#endif102}103104void xics_setup_cpu(void)105{106icp_ops->set_priority(LOWEST_PRIORITY);107108xics_set_cpu_giq(xics_default_distrib_server, 1);109}110111void xics_mask_unknown_vec(unsigned int vec)112{113pr_err("Interrupt 0x%x (real) is invalid, disabling it.\n", vec);114115if (WARN_ON(!xics_ics))116return;117xics_ics->mask_unknown(xics_ics, vec);118}119120121#ifdef CONFIG_SMP122123static void __init xics_request_ipi(void)124{125unsigned int ipi;126127ipi = irq_create_mapping(xics_host, XICS_IPI);128BUG_ON(!ipi);129130/*131* IPIs are marked IRQF_PERCPU. The handler was set in map.132*/133BUG_ON(request_irq(ipi, icp_ops->ipi_action,134IRQF_NO_DEBUG | IRQF_PERCPU | IRQF_NO_THREAD, "IPI", NULL));135}136137void __init xics_smp_probe(void)138{139/* Register all the IPIs */140xics_request_ipi();141142/* Setup cause_ipi callback based on which ICP is used */143smp_ops->cause_ipi = icp_ops->cause_ipi;144}145146#endif /* CONFIG_SMP */147148noinstr void xics_teardown_cpu(void)149{150struct xics_cppr *os_cppr = this_cpu_ptr(&xics_cppr);151152/*153* we have to reset the cppr index to 0 because we're154* not going to return from the IPI155*/156os_cppr->index = 0;157icp_ops->set_priority(0);158icp_ops->teardown_cpu();159}160161noinstr void xics_kexec_teardown_cpu(int secondary)162{163xics_teardown_cpu();164165icp_ops->flush_ipi();166167/*168* Some machines need to have at least one cpu in the GIQ,169* so leave the master cpu in the group.170*/171if (secondary)172xics_set_cpu_giq(xics_default_distrib_server, 0);173}174175176#ifdef CONFIG_HOTPLUG_CPU177178/* Interrupts are disabled. */179void xics_migrate_irqs_away(void)180{181int cpu = smp_processor_id(), hw_cpu = hard_smp_processor_id();182unsigned int irq, virq;183struct irq_desc *desc;184185pr_debug("%s: CPU %u\n", __func__, cpu);186187/* If we used to be the default server, move to the new "boot_cpuid" */188if (hw_cpu == xics_default_server)189xics_update_irq_servers();190191/* Reject any interrupt that was queued to us... */192icp_ops->set_priority(0);193194/* Remove ourselves from the global interrupt queue */195xics_set_cpu_giq(xics_default_distrib_server, 0);196197for_each_irq_desc(virq, desc) {198struct irq_chip *chip;199long server;200unsigned long flags;201struct irq_data *irqd;202203/* We can't set affinity on ISA interrupts */204if (virq < NR_IRQS_LEGACY)205continue;206/* We only need to migrate enabled IRQS */207if (!desc->action)208continue;209/* We need a mapping in the XICS IRQ domain */210irqd = irq_domain_get_irq_data(xics_host, virq);211if (!irqd)212continue;213irq = irqd_to_hwirq(irqd);214/* We need to get IPIs still. */215if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)216continue;217chip = irq_desc_get_chip(desc);218if (!chip || !chip->irq_set_affinity)219continue;220221raw_spin_lock_irqsave(&desc->lock, flags);222223/* Locate interrupt server */224server = xics_ics->get_server(xics_ics, irq);225if (server < 0) {226pr_err("%s: Can't find server for irq %d/%x\n",227__func__, virq, irq);228goto unlock;229}230231/* We only support delivery to all cpus or to one cpu.232* The irq has to be migrated only in the single cpu233* case.234*/235if (server != hw_cpu)236goto unlock;237238/* This is expected during cpu offline. */239if (cpu_online(cpu))240pr_warn("IRQ %u affinity broken off cpu %u\n",241virq, cpu);242243/* Reset affinity to all cpus */244raw_spin_unlock_irqrestore(&desc->lock, flags);245irq_set_affinity(virq, cpu_all_mask);246continue;247unlock:248raw_spin_unlock_irqrestore(&desc->lock, flags);249}250251/* Allow "sufficient" time to drop any inflight IRQ's */252mdelay(5);253254/*255* Allow IPIs again. This is done at the very end, after migrating all256* interrupts, the expectation is that we'll only get woken up by an IPI257* interrupt beyond this point, but leave externals masked just to be258* safe. If we're using icp-opal this may actually allow all259* interrupts anyway, but that should be OK.260*/261icp_ops->set_priority(DEFAULT_PRIORITY);262263}264#endif /* CONFIG_HOTPLUG_CPU */265266#ifdef CONFIG_SMP267/*268* For the moment we only implement delivery to all cpus or one cpu.269*270* If the requested affinity is cpu_all_mask, we set global affinity.271* If not we set it to the first cpu in the mask, even if multiple cpus272* are set. This is so things like irqbalance (which set core and package273* wide affinities) do the right thing.274*275* We need to fix this to implement support for the links276*/277int xics_get_irq_server(unsigned int virq, const struct cpumask *cpumask,278unsigned int strict_check)279{280281if (!distribute_irqs)282return xics_default_server;283284if (!cpumask_subset(cpu_possible_mask, cpumask)) {285int server = cpumask_first_and(cpu_online_mask, cpumask);286287if (server < nr_cpu_ids)288return get_hard_smp_processor_id(server);289290if (strict_check)291return -1;292}293294/*295* Workaround issue with some versions of JS20 firmware that296* deliver interrupts to cpus which haven't been started. This297* happens when using the maxcpus= boot option.298*/299if (cpumask_equal(cpu_online_mask, cpu_present_mask))300return xics_default_distrib_server;301302return xics_default_server;303}304#endif /* CONFIG_SMP */305306static int xics_host_match(struct irq_domain *h, struct device_node *node,307enum irq_domain_bus_token bus_token)308{309if (WARN_ON(!xics_ics))310return 0;311return xics_ics->host_match(xics_ics, node) ? 1 : 0;312}313314/* Dummies */315static void xics_ipi_unmask(struct irq_data *d) { }316static void xics_ipi_mask(struct irq_data *d) { }317318static struct irq_chip xics_ipi_chip = {319.name = "XICS",320.irq_eoi = NULL, /* Patched at init time */321.irq_mask = xics_ipi_mask,322.irq_unmask = xics_ipi_unmask,323};324325static int xics_host_map(struct irq_domain *domain, unsigned int virq,326irq_hw_number_t hwirq)327{328pr_devel("xics: map virq %d, hwirq 0x%lx\n", virq, hwirq);329330/*331* Mark interrupts as edge sensitive by default so that resend332* actually works. The device-tree parsing will turn the LSIs333* back to level.334*/335irq_clear_status_flags(virq, IRQ_LEVEL);336337/* Don't call into ICS for IPIs */338if (hwirq == XICS_IPI) {339irq_set_chip_and_handler(virq, &xics_ipi_chip,340handle_percpu_irq);341return 0;342}343344if (WARN_ON(!xics_ics))345return -EINVAL;346347if (xics_ics->check(xics_ics, hwirq))348return -EINVAL;349350/* Let the ICS be the chip data for the XICS domain. For ICS native */351irq_domain_set_info(domain, virq, hwirq, xics_ics->chip,352xics_ics, handle_fasteoi_irq, NULL, NULL);353354return 0;355}356357static int xics_host_xlate(struct irq_domain *h, struct device_node *ct,358const u32 *intspec, unsigned int intsize,359irq_hw_number_t *out_hwirq, unsigned int *out_flags)360361{362*out_hwirq = intspec[0];363364/*365* If intsize is at least 2, we look for the type in the second cell,366* we assume the LSB indicates a level interrupt.367*/368if (intsize > 1) {369if (intspec[1] & 1)370*out_flags = IRQ_TYPE_LEVEL_LOW;371else372*out_flags = IRQ_TYPE_EDGE_RISING;373} else374*out_flags = IRQ_TYPE_LEVEL_LOW;375376return 0;377}378379int xics_set_irq_type(struct irq_data *d, unsigned int flow_type)380{381/*382* We only support these. This has really no effect other than setting383* the corresponding descriptor bits mind you but those will in turn384* affect the resend function when re-enabling an edge interrupt.385*386* Set set the default to edge as explained in map().387*/388if (flow_type == IRQ_TYPE_DEFAULT || flow_type == IRQ_TYPE_NONE)389flow_type = IRQ_TYPE_EDGE_RISING;390391if (flow_type != IRQ_TYPE_EDGE_RISING &&392flow_type != IRQ_TYPE_LEVEL_LOW)393return -EINVAL;394395irqd_set_trigger_type(d, flow_type);396397return IRQ_SET_MASK_OK_NOCOPY;398}399400int xics_retrigger(struct irq_data *data)401{402/*403* We need to push a dummy CPPR when retriggering, since the subsequent404* EOI will try to pop it. Passing 0 works, as the function hard codes405* the priority value anyway.406*/407xics_push_cppr(0);408409/* Tell the core to do a soft retrigger */410return 0;411}412413#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY414static int xics_host_domain_translate(struct irq_domain *d, struct irq_fwspec *fwspec,415unsigned long *hwirq, unsigned int *type)416{417return xics_host_xlate(d, to_of_node(fwspec->fwnode), fwspec->param,418fwspec->param_count, hwirq, type);419}420421static int xics_host_domain_alloc(struct irq_domain *domain, unsigned int virq,422unsigned int nr_irqs, void *arg)423{424struct irq_fwspec *fwspec = arg;425irq_hw_number_t hwirq;426unsigned int type = IRQ_TYPE_NONE;427int i, rc;428429rc = xics_host_domain_translate(domain, fwspec, &hwirq, &type);430if (rc)431return rc;432433pr_debug("%s %d/%lx #%d\n", __func__, virq, hwirq, nr_irqs);434435for (i = 0; i < nr_irqs; i++)436irq_domain_set_info(domain, virq + i, hwirq + i, xics_ics->chip,437xics_ics, handle_fasteoi_irq, NULL, NULL);438439return 0;440}441442static void xics_host_domain_free(struct irq_domain *domain,443unsigned int virq, unsigned int nr_irqs)444{445pr_debug("%s %d #%d\n", __func__, virq, nr_irqs);446}447#endif448449static const struct irq_domain_ops xics_host_ops = {450#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY451.alloc = xics_host_domain_alloc,452.free = xics_host_domain_free,453.translate = xics_host_domain_translate,454#endif455.match = xics_host_match,456.map = xics_host_map,457.xlate = xics_host_xlate,458};459460static int __init xics_allocate_domain(void)461{462struct fwnode_handle *fn;463464fn = irq_domain_alloc_named_fwnode("XICS");465if (!fn)466return -ENOMEM;467468xics_host = irq_domain_create_tree(fn, &xics_host_ops, NULL);469if (!xics_host) {470irq_domain_free_fwnode(fn);471return -ENOMEM;472}473474irq_set_default_domain(xics_host);475return 0;476}477478void __init xics_register_ics(struct ics *ics)479{480if (WARN_ONCE(xics_ics, "XICS: Source Controller is already defined !"))481return;482xics_ics = ics;483}484485static void __init xics_get_server_size(void)486{487struct device_node *np;488const __be32 *isize;489490/* We fetch the interrupt server size from the first ICS node491* we find if any492*/493np = of_find_compatible_node(NULL, NULL, "ibm,ppc-xics");494if (!np)495return;496497isize = of_get_property(np, "ibm,interrupt-server#-size", NULL);498if (isize)499xics_interrupt_server_size = be32_to_cpu(*isize);500501of_node_put(np);502}503504void __init xics_init(void)505{506int rc = -1;507508/* Fist locate ICP */509if (firmware_has_feature(FW_FEATURE_LPAR))510rc = icp_hv_init();511if (rc < 0) {512rc = icp_native_init();513if (rc == -ENODEV)514rc = icp_opal_init();515}516if (rc < 0) {517pr_warn("XICS: Cannot find a Presentation Controller !\n");518return;519}520521/* Copy get_irq callback over to ppc_md */522ppc_md.get_irq = icp_ops->get_irq;523524/* Patch up IPI chip EOI */525xics_ipi_chip.irq_eoi = icp_ops->eoi;526527/* Now locate ICS */528rc = ics_rtas_init();529if (rc < 0)530rc = ics_opal_init();531if (rc < 0)532rc = ics_native_init();533if (rc < 0)534pr_warn("XICS: Cannot find a Source Controller !\n");535536/* Initialize common bits */537xics_get_server_size();538xics_update_irq_servers();539rc = xics_allocate_domain();540if (rc < 0)541pr_err("XICS: Failed to create IRQ domain");542xics_setup_cpu();543}544545546