-
Notifications
You must be signed in to change notification settings - Fork 2
/
irqrelay.c
309 lines (244 loc) · 5.85 KB
/
irqrelay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// SPDX-License-Identifier: GPL-2.0
/*
* Kernel interrupt relay module.
*
* Copyright (C) 2019 Fredrik Noring
*/
#include "iopmod/errno.h"
#include "iopmod/interrupt.h"
#include "iopmod/iop-error.h"
#include "iopmod/irqs.h"
#include "iopmod/module.h"
#include "iopmod/printk.h"
#include "iopmod/sif.h"
#include "iopmod/sifcmd.h"
#include "iopmod/sifman.h"
#include "iopmod/thread.h"
#include "iopmod/asm/macro.h"
#define MAX_IRQ_RELAYS 16
/**
* enum iop_irq_relay_rpc_ops - IOP IRQ relay RPC operations
* @rpo_request_irq: request IRQ mapping
* @rpo_release_irq: release IRQ mapping
* @rpo_remap_irq: remap existing IRQ mapping
*/
enum iop_irq_relay_rpc_ops {
rpo_request_irq = 1,
rpo_release_irq = 2,
rpo_remap_irq = 3,
};
/**
* struct iop_rpc_relay_map - IOP IRQ relay mapping
* @u8 iop: IOP IRQ map source
* @u8 map: main IRQ map target
* @u8 rpc: %true for RPC relay, %false for SMFLAG relay
*/
struct iop_rpc_relay_map {
u8 iop;
u8 map;
u8 rpc;
};
/**
* struct iop_rpc_relay_release - IOP IRQ relay to release
* @iop: IOP IRQ to release mapping for
*/
struct iop_rpc_relay_release {
u8 iop;
};
/**
* struct iop_irq_map - IRQ relay map
* @set: IRQ is active if %true, otherwise unused
* @iop: IRQ for the IOP
* @map: IRQ to map to in main if @rpc is %true, otherwise bit in SMFLAG
* @rpc: relay via RPC if %true, otherwise via SMFLAG
*/
struct iop_irq_map {
u16 set : 1;
u16 iop : 7;
u16 map : 7;
u16 rpc : 1;
};
static struct iop_irq_map irqs[MAX_IRQ_RELAYS];
static int rpc_stid;
static struct sifcmd_rpc_data_queue rpc_qdata;
static struct sifcmd_rpc_server_data rpc_sdata;
static u8 rpc_buffer[16] __attribute__((aligned(4)));
static enum irq_status service_irq(void *arg)
{
struct iop_irq_map *m = arg;
if (m->rpc) {
const u32 irq = m->map;
int err;
err = sif_cmd(SIF_CMD_IRQ_RELAY, &irq, sizeof(irq));
if (err < 0)
pr_err("%s: sif_cmd failed with %d\n", __func__, err);
} else {
sifman_set_sm_flag(1 << m->map);
sifman_intr_main();
}
return IRQ_HANDLED;
}
static struct iop_irq_map *find_map(unsigned int iop_irq)
{
for (int i = 0; i < ARRAY_SIZE(irqs); i++)
if (irqs[i].set && irqs[i].iop == iop_irq)
return &irqs[i];
return NULL;
}
static struct iop_irq_map *find_unset(unsigned int iop_irq)
{
for (int i = 0; i < ARRAY_SIZE(irqs); i++)
if (!irqs[i].set)
return &irqs[i];
return NULL;
}
static int request_map(unsigned int iop_irq, unsigned map_irq, bool rpc)
{
const struct iop_irq_map w = {
.set = true,
.iop = iop_irq,
.map = map_irq,
.rpc = rpc,
};
unsigned int flags;
int err = 0;
pr_debug("%s: request iop irq %u for %u with %s\n",
__func__, iop_irq, map_irq, rpc ? "RPC" : "SMFLAG");
if (w.iop != iop_irq || w.map != map_irq)
return -EINVAL;
irq_save(flags);
if (find_map(iop_irq)) {
err = -EBUSY;
goto out;
}
struct iop_irq_map *m = find_unset(iop_irq);
if (!m) {
err = -ENOMEM;
goto out;
}
*m = w;
err = request_irq(m->iop, service_irq, m);
if (err < 0)
*m = (struct iop_irq_map) { };
out:
irq_restore(flags);
return err;
}
static int remap(unsigned int iop_irq, unsigned map_irq, bool rpc)
{
const struct iop_irq_map w = {
.map = map_irq,
.rpc = rpc,
};
unsigned int flags;
int err = 0;
pr_debug("%s: remap iop irq %u to %u with %s\n",
__func__, iop_irq, map_irq, rpc ? "RPC" : "SMFLAG");
if (w.map != map_irq)
return -EINVAL;
irq_save(flags);
struct iop_irq_map *m = find_map(iop_irq);
if (!m) {
err = -ENXIO;
goto out;
}
m->map = w.map;
m->rpc = w.rpc;
out:
irq_restore(flags);
return err;
}
static int release_map(unsigned int iop_irq)
{
unsigned int flags;
int err = 0;
pr_debug("%s: release %u\n", __func__, iop_irq);
irq_save(flags);
struct iop_irq_map *m = find_map(iop_irq);
if (!m) {
err = -ENXIO;
goto out;
}
err = release_irq(iop_irq);
if (!err)
*m = (struct iop_irq_map) { };
out:
irq_restore(flags);
return err;
}
static void *irqrelay_service_rpc(int rpo, void *buffer, size_t size)
{
static int status;
switch (rpo) {
case rpo_request_irq: {
const struct iop_rpc_relay_map *request = buffer;
if (size != sizeof(*request)) {
status = -EINVAL;
break;
}
status = request_map(request->iop, request->map, request->rpc);
break;
}
case rpo_remap_irq: {
const struct iop_rpc_relay_map *request = buffer;
if (size != sizeof(*request)) {
status = -EINVAL;
break;
}
status = remap(request->iop, request->map, request->rpc);
break;
}
case rpo_release_irq: {
const struct iop_rpc_relay_release *request = buffer;
if (size != sizeof(*request)) {
status = -EINVAL;
break;
}
status = release_map(request->iop);
break;
}
default:
pr_err("%s: Invalid RPC %d size %zu\n", __func__, rpo, size);
status = -EINVAL;
}
return &status;
}
static void irqrelay_rpc_server(void *arg)
{
pr_debug("%s: Calling sifcmd_rpc_loop\n", __func__);
sifcmd_rpc_loop(&rpc_qdata);
}
static enum module_init_status irqrelay_init(int argc, char *argv[])
{
int ioperr;
const struct iop_thread th = {
.attr = THREAD_ATTR_C,
.thread = irqrelay_rpc_server,
.stacksize = 1024,
.priority = 0x27, /* FIXME: What are reasonable priorities? */
};
rpc_stid = thbase_create(&th);
if (rpc_stid < 0) {
pr_err("%s: thbase_create failed with %d: %s\n",
__func__, rpc_stid, iop_error_message(rpc_stid));
goto err_create;
}
sifcmd_set_rpc_queue(&rpc_qdata, rpc_stid);
sifcmd_register_rpc(&rpc_sdata, SIF_SID_IRQ_RELAY,
irqrelay_service_rpc, rpc_buffer, NULL, NULL, &rpc_qdata);
ioperr = thbase_start(rpc_stid, NULL);
if (ioperr < 0) {
pr_err("%s: thbase_start failed with %d: %s\n",
__func__, ioperr, iop_error_message(ioperr));
goto err_start;
}
pr_info("irqrelay: IRQ relay ready\n");
return MODULE_RESIDENT;
err_start:
sifcmd_remove_rpc(&rpc_sdata, &rpc_qdata);
sifcmd_remove_rpc_queue(&rpc_qdata);
thbase_delete(rpc_stid);
err_create:
return MODULE_EXIT;
}
module_init(irqrelay_init);