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>
6#include <asm/xive.h>
7#include "ocxl_internal.h"
8#include "trace.h"
9
10struct afu_irq {
11 int id;
12 int hw_irq;
13 unsigned int virq;
14 char *name;
15 irqreturn_t (*handler)(void *private);
16 void (*free_private)(void *private);
17 void *private;
18};
19
20int ocxl_irq_offset_to_id(struct ocxl_context *ctx, u64 offset)
21{
22 return (offset - ctx->afu->irq_base_offset) >> PAGE_SHIFT;
23}
24
25u64 ocxl_irq_id_to_offset(struct ocxl_context *ctx, int irq_id)
26{
27 return ctx->afu->irq_base_offset + (irq_id << PAGE_SHIFT);
28}
29
30int ocxl_irq_set_handler(struct ocxl_context *ctx, int irq_id,
31 irqreturn_t (*handler)(void *private),
32 void (*free_private)(void *private),
33 void *private)
34{
35 struct afu_irq *irq;
36 int rc;
37
38 mutex_lock(&ctx->irq_lock);
39 irq = idr_find(&ctx->irq_idr, id: irq_id);
40 if (!irq) {
41 rc = -EINVAL;
42 goto unlock;
43 }
44
45 irq->handler = handler;
46 irq->private = private;
47 irq->free_private = free_private;
48
49 rc = 0;
50 // Fall through to unlock
51
52unlock:
53 mutex_unlock(lock: &ctx->irq_lock);
54 return rc;
55}
56EXPORT_SYMBOL_GPL(ocxl_irq_set_handler);
57
58static irqreturn_t afu_irq_handler(int virq, void *data)
59{
60 struct afu_irq *irq = data;
61
62 trace_ocxl_afu_irq_receive(virq);
63
64 if (irq->handler)
65 return irq->handler(irq->private);
66
67 return IRQ_HANDLED; // Just drop it on the ground
68}
69
70static int setup_afu_irq(struct ocxl_context *ctx, struct afu_irq *irq)
71{
72 int rc;
73
74 irq->virq = irq_create_mapping(NULL, hwirq: irq->hw_irq);
75 if (!irq->virq) {
76 pr_err("irq_create_mapping failed\n");
77 return -ENOMEM;
78 }
79 pr_debug("hw_irq %d mapped to virq %u\n", irq->hw_irq, irq->virq);
80
81 irq->name = kasprintf(GFP_KERNEL, fmt: "ocxl-afu-%u", irq->virq);
82 if (!irq->name) {
83 irq_dispose_mapping(virq: irq->virq);
84 return -ENOMEM;
85 }
86
87 rc = request_irq(irq: irq->virq, handler: afu_irq_handler, flags: 0, name: irq->name, dev: irq);
88 if (rc) {
89 kfree(objp: irq->name);
90 irq->name = NULL;
91 irq_dispose_mapping(virq: irq->virq);
92 pr_err("request_irq failed: %d\n", rc);
93 return rc;
94 }
95 return 0;
96}
97
98static void release_afu_irq(struct afu_irq *irq)
99{
100 free_irq(irq->virq, irq);
101 irq_dispose_mapping(virq: irq->virq);
102 kfree(objp: irq->name);
103}
104
105int ocxl_afu_irq_alloc(struct ocxl_context *ctx, int *irq_id)
106{
107 struct afu_irq *irq;
108 int rc;
109
110 irq = kzalloc(size: sizeof(struct afu_irq), GFP_KERNEL);
111 if (!irq)
112 return -ENOMEM;
113
114 /*
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
117 */
118
119 mutex_lock(&ctx->irq_lock);
120
121 irq->id = idr_alloc(&ctx->irq_idr, ptr: irq, start: 0, MAX_IRQ_PER_CONTEXT,
122 GFP_KERNEL);
123 if (irq->id < 0) {
124 rc = -ENOSPC;
125 goto err_unlock;
126 }
127
128 rc = ocxl_link_irq_alloc(link_handle: ctx->afu->fn->link, hw_irq: &irq->hw_irq);
129 if (rc)
130 goto err_idr;
131
132 rc = setup_afu_irq(ctx, irq);
133 if (rc)
134 goto err_alloc;
135
136 trace_ocxl_afu_irq_alloc(pasid: ctx->pasid, irq_id: irq->id, virq: irq->virq, hw_irq: irq->hw_irq);
137 mutex_unlock(lock: &ctx->irq_lock);
138
139 *irq_id = irq->id;
140
141 return 0;
142
143err_alloc:
144 ocxl_link_free_irq(link_handle: ctx->afu->fn->link, hw_irq: irq->hw_irq);
145err_idr:
146 idr_remove(&ctx->irq_idr, id: irq->id);
147err_unlock:
148 mutex_unlock(lock: &ctx->irq_lock);
149 kfree(objp: irq);
150 return rc;
151}
152EXPORT_SYMBOL_GPL(ocxl_afu_irq_alloc);
153
154static void afu_irq_free(struct afu_irq *irq, struct ocxl_context *ctx)
155{
156 trace_ocxl_afu_irq_free(pasid: ctx->pasid, irq_id: irq->id);
157 if (ctx->mapping)
158 unmap_mapping_range(mapping: ctx->mapping,
159 holebegin: ocxl_irq_id_to_offset(ctx, irq_id: irq->id),
160 holelen: 1 << PAGE_SHIFT, even_cows: 1);
161 release_afu_irq(irq);
162 if (irq->free_private)
163 irq->free_private(irq->private);
164 ocxl_link_free_irq(link_handle: ctx->afu->fn->link, hw_irq: irq->hw_irq);
165 kfree(objp: irq);
166}
167
168int ocxl_afu_irq_free(struct ocxl_context *ctx, int irq_id)
169{
170 struct afu_irq *irq;
171
172 mutex_lock(&ctx->irq_lock);
173
174 irq = idr_find(&ctx->irq_idr, id: irq_id);
175 if (!irq) {
176 mutex_unlock(lock: &ctx->irq_lock);
177 return -EINVAL;
178 }
179 idr_remove(&ctx->irq_idr, id: irq->id);
180 afu_irq_free(irq, ctx);
181 mutex_unlock(lock: &ctx->irq_lock);
182 return 0;
183}
184EXPORT_SYMBOL_GPL(ocxl_afu_irq_free);
185
186void ocxl_afu_irq_free_all(struct ocxl_context *ctx)
187{
188 struct afu_irq *irq;
189 int id;
190
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(lock: &ctx->irq_lock);
195}
196
197u64 ocxl_afu_irq_get_addr(struct ocxl_context *ctx, int irq_id)
198{
199 struct xive_irq_data *xd;
200 struct afu_irq *irq;
201 u64 addr = 0;
202
203 mutex_lock(&ctx->irq_lock);
204 irq = idr_find(&ctx->irq_idr, id: irq_id);
205 if (irq) {
206 xd = irq_get_handler_data(irq->virq);
207 addr = xd ? xd->trig_page : 0;
208 }
209 mutex_unlock(lock: &ctx->irq_lock);
210 return addr;
211}
212EXPORT_SYMBOL_GPL(ocxl_afu_irq_get_addr);
213

source code of linux/drivers/misc/ocxl/afu_irq.c