]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/misc/ocxl/afu_irq.c
Merge tag 'xfs-5.6-merge-8' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux.git] / drivers / misc / ocxl / afu_irq.c
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
3 #include <linux/interrupt.h>
4 #include <asm/pnv-ocxl.h>
5 #include "ocxl_internal.h"
6 #include "trace.h"
7
8 struct afu_irq {
9         int id;
10         int hw_irq;
11         unsigned int virq;
12         char *name;
13         u64 trigger_page;
14         irqreturn_t (*handler)(void *private);
15         void (*free_private)(void *private);
16         void *private;
17 };
18
19 int ocxl_irq_offset_to_id(struct ocxl_context *ctx, u64 offset)
20 {
21         return (offset - ctx->afu->irq_base_offset) >> PAGE_SHIFT;
22 }
23
24 u64 ocxl_irq_id_to_offset(struct ocxl_context *ctx, int irq_id)
25 {
26         return ctx->afu->irq_base_offset + (irq_id << PAGE_SHIFT);
27 }
28
29 int ocxl_irq_set_handler(struct ocxl_context *ctx, int irq_id,
30                 irqreturn_t (*handler)(void *private),
31                 void (*free_private)(void *private),
32                 void *private)
33 {
34         struct afu_irq *irq;
35         int rc;
36
37         mutex_lock(&ctx->irq_lock);
38         irq = idr_find(&ctx->irq_idr, irq_id);
39         if (!irq) {
40                 rc = -EINVAL;
41                 goto unlock;
42         }
43
44         irq->handler = handler;
45         irq->private = private;
46         irq->free_private = free_private;
47
48         rc = 0;
49         // Fall through to unlock
50
51 unlock:
52         mutex_unlock(&ctx->irq_lock);
53         return rc;
54 }
55 EXPORT_SYMBOL_GPL(ocxl_irq_set_handler);
56
57 static irqreturn_t afu_irq_handler(int virq, void *data)
58 {
59         struct afu_irq *irq = (struct afu_irq *) data;
60
61         trace_ocxl_afu_irq_receive(virq);
62
63         if (irq->handler)
64                 return irq->handler(irq->private);
65
66         return IRQ_HANDLED; // Just drop it on the ground
67 }
68
69 static int setup_afu_irq(struct ocxl_context *ctx, struct afu_irq *irq)
70 {
71         int rc;
72
73         irq->virq = irq_create_mapping(NULL, irq->hw_irq);
74         if (!irq->virq) {
75                 pr_err("irq_create_mapping failed\n");
76                 return -ENOMEM;
77         }
78         pr_debug("hw_irq %d mapped to virq %u\n", irq->hw_irq, irq->virq);
79
80         irq->name = kasprintf(GFP_KERNEL, "ocxl-afu-%u", irq->virq);
81         if (!irq->name) {
82                 irq_dispose_mapping(irq->virq);
83                 return -ENOMEM;
84         }
85
86         rc = request_irq(irq->virq, afu_irq_handler, 0, irq->name, irq);
87         if (rc) {
88                 kfree(irq->name);
89                 irq->name = NULL;
90                 irq_dispose_mapping(irq->virq);
91                 pr_err("request_irq failed: %d\n", rc);
92                 return rc;
93         }
94         return 0;
95 }
96
97 static void release_afu_irq(struct afu_irq *irq)
98 {
99         free_irq(irq->virq, irq);
100         irq_dispose_mapping(irq->virq);
101         kfree(irq->name);
102 }
103
104 int ocxl_afu_irq_alloc(struct ocxl_context *ctx, int *irq_id)
105 {
106         struct afu_irq *irq;
107         int rc;
108
109         irq = kzalloc(sizeof(struct afu_irq), GFP_KERNEL);
110         if (!irq)
111                 return -ENOMEM;
112
113         /*
114          * We limit the number of afu irqs per context and per link to
115          * avoid a single process or user depleting the pool of IPIs
116          */
117
118         mutex_lock(&ctx->irq_lock);
119
120         irq->id = idr_alloc(&ctx->irq_idr, irq, 0, MAX_IRQ_PER_CONTEXT,
121                         GFP_KERNEL);
122         if (irq->id < 0) {
123                 rc = -ENOSPC;
124                 goto err_unlock;
125         }
126
127         rc = ocxl_link_irq_alloc(ctx->afu->fn->link, &irq->hw_irq,
128                                 &irq->trigger_page);
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
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
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
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
197 u64 ocxl_afu_irq_get_addr(struct ocxl_context *ctx, int irq_id)
198 {
199         struct afu_irq *irq;
200         u64 addr = 0;
201
202         mutex_lock(&ctx->irq_lock);
203         irq = idr_find(&ctx->irq_idr, irq_id);
204         if (irq)
205                 addr = irq->trigger_page;
206         mutex_unlock(&ctx->irq_lock);
207         return addr;
208 }
209 EXPORT_SYMBOL_GPL(ocxl_afu_irq_get_addr);