1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
3 #include <linux/interrupt.h>
4 #include <linux/irqdomain.h>
5 #include <asm/pnv-ocxl.h>
7 #include "ocxl_internal.h"
15 irqreturn_t (*handler)(void *private);
16 void (*free_private)(void *private);
20 int ocxl_irq_offset_to_id(struct ocxl_context *ctx, u64 offset)
22 return (offset - ctx->afu->irq_base_offset) >> PAGE_SHIFT;
25 u64 ocxl_irq_id_to_offset(struct ocxl_context *ctx, int irq_id)
27 return ctx->afu->irq_base_offset + (irq_id << PAGE_SHIFT);
30 int ocxl_irq_set_handler(struct ocxl_context *ctx, int irq_id,
31 irqreturn_t (*handler)(void *private),
32 void (*free_private)(void *private),
38 mutex_lock(&ctx->irq_lock);
39 irq = idr_find(&ctx->irq_idr, irq_id);
45 irq->handler = handler;
46 irq->private = private;
47 irq->free_private = free_private;
50 // Fall through to unlock
53 mutex_unlock(&ctx->irq_lock);
56 EXPORT_SYMBOL_GPL(ocxl_irq_set_handler);
58 static irqreturn_t afu_irq_handler(int virq, void *data)
60 struct afu_irq *irq = (struct afu_irq *) data;
62 trace_ocxl_afu_irq_receive(virq);
65 return irq->handler(irq->private);
67 return IRQ_HANDLED; // Just drop it on the ground
70 static int setup_afu_irq(struct ocxl_context *ctx, struct afu_irq *irq)
74 irq->virq = irq_create_mapping(NULL, irq->hw_irq);
76 pr_err("irq_create_mapping failed\n");
79 pr_debug("hw_irq %d mapped to virq %u\n", irq->hw_irq, irq->virq);
81 irq->name = kasprintf(GFP_KERNEL, "ocxl-afu-%u", irq->virq);
83 irq_dispose_mapping(irq->virq);
87 rc = request_irq(irq->virq, afu_irq_handler, 0, irq->name, irq);
91 irq_dispose_mapping(irq->virq);
92 pr_err("request_irq failed: %d\n", rc);
98 static void release_afu_irq(struct afu_irq *irq)
100 free_irq(irq->virq, irq);
101 irq_dispose_mapping(irq->virq);
105 int ocxl_afu_irq_alloc(struct ocxl_context *ctx, int *irq_id)
110 irq = kzalloc(sizeof(struct afu_irq), GFP_KERNEL);
115 * We limit the number of afu irqs per context and per link to
116 * avoid a single process or user depleting the pool of IPIs
119 mutex_lock(&ctx->irq_lock);
121 irq->id = idr_alloc(&ctx->irq_idr, irq, 0, MAX_IRQ_PER_CONTEXT,
128 rc = ocxl_link_irq_alloc(ctx->afu->fn->link, &irq->hw_irq);
132 rc = setup_afu_irq(ctx, irq);
136 trace_ocxl_afu_irq_alloc(ctx->pasid, irq->id, irq->virq, irq->hw_irq);
137 mutex_unlock(&ctx->irq_lock);
144 ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
146 idr_remove(&ctx->irq_idr, irq->id);
148 mutex_unlock(&ctx->irq_lock);
152 EXPORT_SYMBOL_GPL(ocxl_afu_irq_alloc);
154 static void afu_irq_free(struct afu_irq *irq, struct ocxl_context *ctx)
156 trace_ocxl_afu_irq_free(ctx->pasid, irq->id);
158 unmap_mapping_range(ctx->mapping,
159 ocxl_irq_id_to_offset(ctx, irq->id),
161 release_afu_irq(irq);
162 if (irq->free_private)
163 irq->free_private(irq->private);
164 ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
168 int ocxl_afu_irq_free(struct ocxl_context *ctx, int irq_id)
172 mutex_lock(&ctx->irq_lock);
174 irq = idr_find(&ctx->irq_idr, irq_id);
176 mutex_unlock(&ctx->irq_lock);
179 idr_remove(&ctx->irq_idr, irq->id);
180 afu_irq_free(irq, ctx);
181 mutex_unlock(&ctx->irq_lock);
184 EXPORT_SYMBOL_GPL(ocxl_afu_irq_free);
186 void ocxl_afu_irq_free_all(struct ocxl_context *ctx)
191 mutex_lock(&ctx->irq_lock);
192 idr_for_each_entry(&ctx->irq_idr, irq, id)
193 afu_irq_free(irq, ctx);
194 mutex_unlock(&ctx->irq_lock);
197 u64 ocxl_afu_irq_get_addr(struct ocxl_context *ctx, int irq_id)
199 struct xive_irq_data *xd;
203 mutex_lock(&ctx->irq_lock);
204 irq = idr_find(&ctx->irq_idr, irq_id);
206 xd = irq_get_handler_data(irq->virq);
207 addr = xd ? xd->trig_page : 0;
209 mutex_unlock(&ctx->irq_lock);
212 EXPORT_SYMBOL_GPL(ocxl_afu_irq_get_addr);