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
10 struct 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
ocxl_irq_offset_to_id(struct ocxl_context * ctx,u64 offset)20 int ocxl_irq_offset_to_id(struct ocxl_context *ctx, u64 offset)
21 {
22 return (offset - ctx->afu->irq_base_offset) >> PAGE_SHIFT;
23 }
24
ocxl_irq_id_to_offset(struct ocxl_context * ctx,int irq_id)25 u64 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
ocxl_irq_set_handler(struct ocxl_context * ctx,int irq_id,irqreturn_t (* handler)(void * private),void (* free_private)(void * private),void * private)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),
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, 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
52 unlock:
53 mutex_unlock(&ctx->irq_lock);
54 return rc;
55 }
56 EXPORT_SYMBOL_GPL(ocxl_irq_set_handler);
57
afu_irq_handler(int virq,void * data)58 static irqreturn_t afu_irq_handler(int virq, void *data)
59 {
60 struct afu_irq *irq = (struct afu_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
setup_afu_irq(struct ocxl_context * ctx,struct afu_irq * irq)70 static int setup_afu_irq(struct ocxl_context *ctx, struct afu_irq *irq)
71 {
72 int rc;
73
74 irq->virq = irq_create_mapping(NULL, 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, "ocxl-afu-%u", irq->virq);
82 if (!irq->name) {
83 irq_dispose_mapping(irq->virq);
84 return -ENOMEM;
85 }
86
87 rc = request_irq(irq->virq, afu_irq_handler, 0, irq->name, irq);
88 if (rc) {
89 kfree(irq->name);
90 irq->name = NULL;
91 irq_dispose_mapping(irq->virq);
92 pr_err("request_irq failed: %d\n", rc);
93 return rc;
94 }
95 return 0;
96 }
97
release_afu_irq(struct afu_irq * irq)98 static void release_afu_irq(struct afu_irq *irq)
99 {
100 free_irq(irq->virq, irq);
101 irq_dispose_mapping(irq->virq);
102 kfree(irq->name);
103 }
104
ocxl_afu_irq_alloc(struct ocxl_context * ctx,int * irq_id)105 int ocxl_afu_irq_alloc(struct ocxl_context *ctx, int *irq_id)
106 {
107 struct afu_irq *irq;
108 int rc;
109
110 irq = kzalloc(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, irq, 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(ctx->afu->fn->link, &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(ctx->pasid, irq->id, irq->virq, irq->hw_irq);
137 mutex_unlock(&ctx->irq_lock);
138
139 *irq_id = irq->id;
140
141 return 0;
142
143 err_alloc:
144 ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
145 err_idr:
146 idr_remove(&ctx->irq_idr, irq->id);
147 err_unlock:
148 mutex_unlock(&ctx->irq_lock);
149 kfree(irq);
150 return rc;
151 }
152 EXPORT_SYMBOL_GPL(ocxl_afu_irq_alloc);
153
afu_irq_free(struct afu_irq * irq,struct ocxl_context * ctx)154 static void afu_irq_free(struct afu_irq *irq, struct ocxl_context *ctx)
155 {
156 trace_ocxl_afu_irq_free(ctx->pasid, irq->id);
157 if (ctx->mapping)
158 unmap_mapping_range(ctx->mapping,
159 ocxl_irq_id_to_offset(ctx, irq->id),
160 1 << PAGE_SHIFT, 1);
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);
165 kfree(irq);
166 }
167
ocxl_afu_irq_free(struct ocxl_context * ctx,int irq_id)168 int 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, irq_id);
175 if (!irq) {
176 mutex_unlock(&ctx->irq_lock);
177 return -EINVAL;
178 }
179 idr_remove(&ctx->irq_idr, irq->id);
180 afu_irq_free(irq, ctx);
181 mutex_unlock(&ctx->irq_lock);
182 return 0;
183 }
184 EXPORT_SYMBOL_GPL(ocxl_afu_irq_free);
185
ocxl_afu_irq_free_all(struct ocxl_context * ctx)186 void 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(&ctx->irq_lock);
195 }
196
ocxl_afu_irq_get_addr(struct ocxl_context * ctx,int irq_id)197 u64 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, irq_id);
205 if (irq) {
206 xd = irq_get_handler_data(irq->virq);
207 addr = xd ? xd->trig_page : 0;
208 }
209 mutex_unlock(&ctx->irq_lock);
210 return addr;
211 }
212 EXPORT_SYMBOL_GPL(ocxl_afu_irq_get_addr);
213