1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
3 #include <linux/interrupt.h>
4 #include <linux/eventfd.h>
5 #include "ocxl_internal.h"
14 struct eventfd_ctx *ev_ctx;
17 static int irq_offset_to_id(struct ocxl_context *ctx, u64 offset)
19 return (offset - ctx->afu->irq_base_offset) >> PAGE_SHIFT;
22 static u64 irq_id_to_offset(struct ocxl_context *ctx, int id)
24 return ctx->afu->irq_base_offset + (id << PAGE_SHIFT);
27 static irqreturn_t afu_irq_handler(int virq, void *data)
29 struct afu_irq *irq = (struct afu_irq *) data;
31 trace_ocxl_afu_irq_receive(virq);
33 eventfd_signal(irq->ev_ctx, 1);
37 static int setup_afu_irq(struct ocxl_context *ctx, struct afu_irq *irq)
41 irq->virq = irq_create_mapping(NULL, irq->hw_irq);
43 pr_err("irq_create_mapping failed\n");
46 pr_debug("hw_irq %d mapped to virq %u\n", irq->hw_irq, irq->virq);
48 irq->name = kasprintf(GFP_KERNEL, "ocxl-afu-%u", irq->virq);
50 irq_dispose_mapping(irq->virq);
54 rc = request_irq(irq->virq, afu_irq_handler, 0, irq->name, irq);
58 irq_dispose_mapping(irq->virq);
59 pr_err("request_irq failed: %d\n", rc);
65 static void release_afu_irq(struct afu_irq *irq)
67 free_irq(irq->virq, irq);
68 irq_dispose_mapping(irq->virq);
72 int ocxl_afu_irq_alloc(struct ocxl_context *ctx, u64 *irq_offset)
77 irq = kzalloc(sizeof(struct afu_irq), GFP_KERNEL);
82 * We limit the number of afu irqs per context and per link to
83 * avoid a single process or user depleting the pool of IPIs
86 mutex_lock(&ctx->irq_lock);
88 irq->id = idr_alloc(&ctx->irq_idr, irq, 0, MAX_IRQ_PER_CONTEXT,
95 rc = ocxl_link_irq_alloc(ctx->afu->fn->link, &irq->hw_irq,
100 rc = setup_afu_irq(ctx, irq);
104 *irq_offset = irq_id_to_offset(ctx, irq->id);
106 trace_ocxl_afu_irq_alloc(ctx->pasid, irq->id, irq->virq, irq->hw_irq,
108 mutex_unlock(&ctx->irq_lock);
112 ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
114 idr_remove(&ctx->irq_idr, irq->id);
116 mutex_unlock(&ctx->irq_lock);
121 static void afu_irq_free(struct afu_irq *irq, struct ocxl_context *ctx)
123 trace_ocxl_afu_irq_free(ctx->pasid, irq->id);
125 unmap_mapping_range(ctx->mapping,
126 irq_id_to_offset(ctx, irq->id),
128 release_afu_irq(irq);
130 eventfd_ctx_put(irq->ev_ctx);
131 ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
135 int ocxl_afu_irq_free(struct ocxl_context *ctx, u64 irq_offset)
138 int id = irq_offset_to_id(ctx, irq_offset);
140 mutex_lock(&ctx->irq_lock);
142 irq = idr_find(&ctx->irq_idr, id);
144 mutex_unlock(&ctx->irq_lock);
147 idr_remove(&ctx->irq_idr, irq->id);
148 afu_irq_free(irq, ctx);
149 mutex_unlock(&ctx->irq_lock);
153 void ocxl_afu_irq_free_all(struct ocxl_context *ctx)
158 mutex_lock(&ctx->irq_lock);
159 idr_for_each_entry(&ctx->irq_idr, irq, id)
160 afu_irq_free(irq, ctx);
161 mutex_unlock(&ctx->irq_lock);
164 int ocxl_afu_irq_set_fd(struct ocxl_context *ctx, u64 irq_offset, int eventfd)
167 struct eventfd_ctx *ev_ctx;
168 int rc = 0, id = irq_offset_to_id(ctx, irq_offset);
170 mutex_lock(&ctx->irq_lock);
171 irq = idr_find(&ctx->irq_idr, id);
177 ev_ctx = eventfd_ctx_fdget(eventfd);
178 if (IS_ERR(ev_ctx)) {
183 irq->ev_ctx = ev_ctx;
185 mutex_unlock(&ctx->irq_lock);
189 u64 ocxl_afu_irq_get_addr(struct ocxl_context *ctx, u64 irq_offset)
192 int id = irq_offset_to_id(ctx, irq_offset);
195 mutex_lock(&ctx->irq_lock);
196 irq = idr_find(&ctx->irq_idr, id);
198 addr = irq->trigger_page;
199 mutex_unlock(&ctx->irq_lock);