xref: /openbmc/linux/drivers/misc/ocxl/afu_irq.c (revision aeddad17)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
3 #include <linux/interrupt.h>
4 #include <linux/eventfd.h>
5 #include <asm/pnv-ocxl.h>
6 #include "ocxl_internal.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 	struct eventfd_ctx *ev_ctx;
15 };
16 
17 static int irq_offset_to_id(struct ocxl_context *ctx, u64 offset)
18 {
19 	return (offset - ctx->afu->irq_base_offset) >> PAGE_SHIFT;
20 }
21 
22 static u64 irq_id_to_offset(struct ocxl_context *ctx, int id)
23 {
24 	return ctx->afu->irq_base_offset + (id << PAGE_SHIFT);
25 }
26 
27 static irqreturn_t afu_irq_handler(int virq, void *data)
28 {
29 	struct afu_irq *irq = (struct afu_irq *) data;
30 
31 	if (irq->ev_ctx)
32 		eventfd_signal(irq->ev_ctx, 1);
33 	return IRQ_HANDLED;
34 }
35 
36 static int setup_afu_irq(struct ocxl_context *ctx, struct afu_irq *irq)
37 {
38 	int rc;
39 
40 	irq->virq = irq_create_mapping(NULL, irq->hw_irq);
41 	if (!irq->virq) {
42 		pr_err("irq_create_mapping failed\n");
43 		return -ENOMEM;
44 	}
45 	pr_debug("hw_irq %d mapped to virq %u\n", irq->hw_irq, irq->virq);
46 
47 	irq->name = kasprintf(GFP_KERNEL, "ocxl-afu-%u", irq->virq);
48 	if (!irq->name) {
49 		irq_dispose_mapping(irq->virq);
50 		return -ENOMEM;
51 	}
52 
53 	rc = request_irq(irq->virq, afu_irq_handler, 0, irq->name, irq);
54 	if (rc) {
55 		kfree(irq->name);
56 		irq->name = NULL;
57 		irq_dispose_mapping(irq->virq);
58 		pr_err("request_irq failed: %d\n", rc);
59 		return rc;
60 	}
61 	return 0;
62 }
63 
64 static void release_afu_irq(struct afu_irq *irq)
65 {
66 	free_irq(irq->virq, irq);
67 	irq_dispose_mapping(irq->virq);
68 	kfree(irq->name);
69 }
70 
71 int ocxl_afu_irq_alloc(struct ocxl_context *ctx, u64 *irq_offset)
72 {
73 	struct afu_irq *irq;
74 	int rc;
75 
76 	irq = kzalloc(sizeof(struct afu_irq), GFP_KERNEL);
77 	if (!irq)
78 		return -ENOMEM;
79 
80 	/*
81 	 * We limit the number of afu irqs per context and per link to
82 	 * avoid a single process or user depleting the pool of IPIs
83 	 */
84 
85 	mutex_lock(&ctx->irq_lock);
86 
87 	irq->id = idr_alloc(&ctx->irq_idr, irq, 0, MAX_IRQ_PER_CONTEXT,
88 			GFP_KERNEL);
89 	if (irq->id < 0) {
90 		rc = -ENOSPC;
91 		goto err_unlock;
92 	}
93 
94 	rc = ocxl_link_irq_alloc(ctx->afu->fn->link, &irq->hw_irq,
95 				&irq->trigger_page);
96 	if (rc)
97 		goto err_idr;
98 
99 	rc = setup_afu_irq(ctx, irq);
100 	if (rc)
101 		goto err_alloc;
102 
103 	*irq_offset = irq_id_to_offset(ctx, irq->id);
104 
105 	mutex_unlock(&ctx->irq_lock);
106 	return 0;
107 
108 err_alloc:
109 	ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
110 err_idr:
111 	idr_remove(&ctx->irq_idr, irq->id);
112 err_unlock:
113 	mutex_unlock(&ctx->irq_lock);
114 	kfree(irq);
115 	return rc;
116 }
117 
118 static void afu_irq_free(struct afu_irq *irq, struct ocxl_context *ctx)
119 {
120 	if (ctx->mapping)
121 		unmap_mapping_range(ctx->mapping,
122 				irq_id_to_offset(ctx, irq->id),
123 				1 << PAGE_SHIFT, 1);
124 	release_afu_irq(irq);
125 	if (irq->ev_ctx)
126 		eventfd_ctx_put(irq->ev_ctx);
127 	ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
128 	kfree(irq);
129 }
130 
131 int ocxl_afu_irq_free(struct ocxl_context *ctx, u64 irq_offset)
132 {
133 	struct afu_irq *irq;
134 	int id = irq_offset_to_id(ctx, irq_offset);
135 
136 	mutex_lock(&ctx->irq_lock);
137 
138 	irq = idr_find(&ctx->irq_idr, id);
139 	if (!irq) {
140 		mutex_unlock(&ctx->irq_lock);
141 		return -EINVAL;
142 	}
143 	idr_remove(&ctx->irq_idr, irq->id);
144 	afu_irq_free(irq, ctx);
145 	mutex_unlock(&ctx->irq_lock);
146 	return 0;
147 }
148 
149 void ocxl_afu_irq_free_all(struct ocxl_context *ctx)
150 {
151 	struct afu_irq *irq;
152 	int id;
153 
154 	mutex_lock(&ctx->irq_lock);
155 	idr_for_each_entry(&ctx->irq_idr, irq, id)
156 		afu_irq_free(irq, ctx);
157 	mutex_unlock(&ctx->irq_lock);
158 }
159 
160 int ocxl_afu_irq_set_fd(struct ocxl_context *ctx, u64 irq_offset, int eventfd)
161 {
162 	struct afu_irq *irq;
163 	struct eventfd_ctx *ev_ctx;
164 	int rc = 0, id = irq_offset_to_id(ctx, irq_offset);
165 
166 	mutex_lock(&ctx->irq_lock);
167 	irq = idr_find(&ctx->irq_idr, id);
168 	if (!irq) {
169 		rc = -EINVAL;
170 		goto unlock;
171 	}
172 
173 	ev_ctx = eventfd_ctx_fdget(eventfd);
174 	if (IS_ERR(ev_ctx)) {
175 		rc = -EINVAL;
176 		goto unlock;
177 	}
178 
179 	irq->ev_ctx = ev_ctx;
180 unlock:
181 	mutex_unlock(&ctx->irq_lock);
182 	return rc;
183 }
184 
185 u64 ocxl_afu_irq_get_addr(struct ocxl_context *ctx, u64 irq_offset)
186 {
187 	struct afu_irq *irq;
188 	int id = irq_offset_to_id(ctx, irq_offset);
189 	u64 addr = 0;
190 
191 	mutex_lock(&ctx->irq_lock);
192 	irq = idr_find(&ctx->irq_idr, id);
193 	if (irq)
194 		addr = irq->trigger_page;
195 	mutex_unlock(&ctx->irq_lock);
196 	return addr;
197 }
198