Add a generic mechanism to dynamically allocate an IPI.

With this change the user can call irq_reserve_ipi() to dynamically allocate an
IPI and use the associate virq to send one to 1 or more cpus.

No irq_get_irq_hwcfg() as I hope we can provide an implementation without
hardware specific part. Hopefully I'm not being too optimistic :)

Signed-off-by: Qais Yousef <qais.you...@imgtec.com>
---
 include/linux/irqdomain.h |   4 ++
 kernel/irq/irqdomain.c    | 161 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 164 insertions(+), 1 deletion(-)

diff --git a/include/linux/irqdomain.h b/include/linux/irqdomain.h
index 76a0e7aaa8df..4bdd7b48d205 100644
--- a/include/linux/irqdomain.h
+++ b/include/linux/irqdomain.h
@@ -329,6 +329,10 @@ extern struct ipi_virq *irq_domain_find_ipi_virq(struct 
irq_domain *d,
 extern int irq_domain_ipi_virq_add_hwirq(struct ipi_virq *v,
                                         struct ipi_hwirq *h);
 extern struct ipi_hwirq *irq_domain_ipi_virq_rm_hwirq(struct ipi_virq *v);
+extern unsigned int irq_reserve_ipi(struct irq_domain *domain,
+                               const struct cpumask *dest, void *devid);
+extern void irq_destroy_ipi(unsigned int irq, void *devid);
+extern void irq_send_ipi(unsigned int irq, const struct cpumask *dest, void 
*devid);
 
 /* V2 interfaces to support hierarchy IRQ domains. */
 extern struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c
index 6d911fc8fa52..d38d78a994ca 100644
--- a/kernel/irq/irqdomain.c
+++ b/kernel/irq/irqdomain.c
@@ -873,7 +873,7 @@ struct ipi_virq *irq_domain_find_ipi_virq(struct irq_domain 
*d,
  * returns a pointer to ipi_virq struct on success and NULL on failure
  */
 static struct ipi_virq *irq_domain_alloc_ipi_virq(unsigned int virq,
-                               struct cpumask *cpumask, void *devid)
+                               const struct cpumask *cpumask, void *devid)
 {
        struct ipi_virq *v;
 
@@ -942,6 +942,165 @@ struct ipi_hwirq *irq_domain_ipi_virq_rm_hwirq(struct 
ipi_virq *v)
        return h;
 }
 
+/**
+ * irq_reserve_ipi() - setup an IPI to destination cpumask
+ * @domain: IPI domain
+ * @dest: cpumask of cpus to receive the IPI
+ * @devid: devid that requested the reservation
+ *
+ * Allocate a virq that can be used to send IPI to any CPU in dest mask.
+ *
+ * On success it'll return linux irq number and 0 on failure
+ */
+unsigned int irq_reserve_ipi(struct irq_domain *domain,
+                            const struct cpumask *dest, void *devid)
+{
+       int virq, ret;
+       unsigned int nr_irqs;
+       struct ipi_virq *v;
+
+       if (domain == NULL)
+               domain = irq_default_domain; /* need a separate 
ipi_default_domain? */
+
+       if (domain == NULL) {
+               pr_warn("Must provide a valid IPI domain!\n");
+               return 0;
+       }
+
+       if (!irq_domain_is_ipi(domain)) {
+               pr_warn("Not an IPI domain!\n");
+               return 0;
+       }
+
+       if (cpumask_empty(dest)) {
+               pr_warn("Can't reserve IPI due to empty cpumask\n");
+               return 0;
+       }
+
+       /* always allocate a virq per cpu */
+       nr_irqs = cpumask_weight(dest);
+
+       virq = irq_domain_alloc_descs(-1, nr_irqs, 0, NUMA_NO_NODE);
+       if (virq <= 0) {
+               pr_warn("Can't reserve IPI, failed to alloc descs\n");
+               return 0;
+       }
+
+       v = irq_domain_alloc_ipi_virq(virq, dest, devid);
+       if (!v) {
+               pr_warn("Can't reserve IPI, failed to alloc ipi_virq\n");
+               goto free_descs;
+       }
+
+       /* we are reusing hierarchy alloc function, should we create another 
one? */
+       virq = __irq_domain_alloc_irqs(domain, virq, nr_irqs, NUMA_NO_NODE,
+                                       v, true);
+       if (virq <= 0) {
+               pr_warn("Can't reserve IPI, failed to alloc irqs\n");
+               goto free_ipi;
+       }
+
+       ret = irq_domain_add_ipi_virq(domain, v);
+       if (ret) {
+               pr_warn("Can't reserve IPI, failed to add ipi_virq\n");
+               goto free_ipi;
+       }
+
+       return virq;
+
+free_ipi:
+       irq_domain_free_ipi_virq(v);
+free_descs:
+       irq_free_descs(virq, nr_irqs);
+       return 0;
+}
+
+/**
+ * irq_destroy_ipi() - unreserve an IPI that was previously allocated
+ * @irq: linux irq number to be destroyed
+ * @devid: devid that reserved the IPI
+ *
+ * Return an IPI allocated with irq_reserve_ipi() to the system.
+ */
+void irq_destroy_ipi(unsigned int irq, void *devid)
+{
+       struct irq_data *irq_data = irq_get_irq_data(irq);
+       struct irq_domain *domain;
+       struct ipi_virq *v;
+
+       if (!irq || !irq_data)
+               return;
+
+       domain = irq_data->domain;
+       if (WARN_ON(domain == NULL))
+               return;
+
+       if (!irq_domain_is_ipi(domain)) {
+               pr_warn("Not an IPI domain!\n");
+               return;
+       }
+
+       v = irq_domain_find_ipi_virq(domain, irq);
+       if (!v)
+               return;
+
+       if (v->devid != devid) {
+               pr_warn("Only the device that allocated the IPI can destroy 
it\n");
+               return;
+       }
+
+       irq_domain_free_irqs(irq, cpumask_weight(&v->cpumask));
+
+       v = irq_domain_rm_ipi_virq(domain, irq);
+       irq_domain_free_ipi_virq(v);
+}
+
+/**
+ * irq_send_ipi() - send an IPI to target CPU(s)
+ * @irq: linux irq number from irq_reserve_ipi()
+ * @dest: dest CPU(s), must be the same or a subset of the mask past to
+ *       irq_reserve_ipi()
+ * @devid: devid that reserved the IPI
+ *
+ * Sends an IPI to all cpus in dest mask
+ */
+void irq_send_ipi(unsigned int irq, const struct cpumask *dest, void *devid)
+{
+       struct irq_data *irq_data = irq_get_irq_data(irq);
+       struct irq_domain *domain;
+       struct ipi_virq *v;
+       struct ipi_hwirq *h;
+
+       if (!irq || !irq_data)
+               return;
+
+       domain = irq_data->domain;
+       if (WARN_ON(domain == NULL))
+               return;
+
+       if (!irq_domain_is_ipi(domain)) {
+               pr_warn("Not an IPI domain!\n");
+               return;
+       }
+
+       v = irq_domain_find_ipi_virq(domain, irq);
+       if (!v)
+               return;
+
+       if (v->devid != devid) {
+               pr_warn("Only the device that allocated the IPI can send 
one\n");
+               return;
+       }
+
+       if (!cpumask_intersects(&v->cpumask, dest))
+               return;
+
+       list_for_each_entry(h, &v->mapped_hwirq, link) {
+               if (cpumask_intersects(&h->cpumask, dest))
+                       domain->ops->send_ipi(h->hwirq);
+       }
+}
+
 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
 /**
  * irq_domain_add_hierarchy - Add a irqdomain into the hierarchy
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to