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
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2017-2018 Bartosz Golaszewski <brgl@bgdev.pl>
* Copyright (C) 2020 Bartosz Golaszewski <bgolaszewski@baylibre.com>
*/
#include <linux/cleanup.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irq_sim.h>
#include <linux/irq_work.h>
#include <linux/slab.h>
struct irq_sim_work_ctx {
struct irq_work work;
unsigned int irq_count;
unsigned long *pending;
struct irq_domain *domain;
struct irq_sim_ops ops;
void *user_data;
};
struct irq_sim_irq_ctx {
bool enabled;
struct irq_sim_work_ctx *work_ctx;
};
static void irq_sim_irqmask(struct irq_data *data)
{
struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
irq_ctx->enabled = false;
}
static void irq_sim_irqunmask(struct irq_data *data)
{
struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
irq_ctx->enabled = true;
}
static int irq_sim_set_type(struct irq_data *data, unsigned int type)
{
/* We only support rising and falling edge trigger types. */
if (type & ~IRQ_TYPE_EDGE_BOTH)
return -EINVAL;
irqd_set_trigger_type(data, type);
return 0;
}
static int irq_sim_get_irqchip_state(struct irq_data *data,
enum irqchip_irq_state which, bool *state)
{
struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
irq_hw_number_t hwirq = irqd_to_hwirq(data);
switch (which) {
case IRQCHIP_STATE_PENDING:
if (irq_ctx->enabled)
*state = test_bit(hwirq, irq_ctx->work_ctx->pending);
break;
default:
return -EINVAL;
}
return 0;
}
static int irq_sim_set_irqchip_state(struct irq_data *data,
enum irqchip_irq_state which, bool state)
{
struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
irq_hw_number_t hwirq = irqd_to_hwirq(data);
switch (which) {
case IRQCHIP_STATE_PENDING:
if (irq_ctx->enabled) {
assign_bit(hwirq, irq_ctx->work_ctx->pending, state);
if (state)
irq_work_queue(&irq_ctx->work_ctx->work);
}
break;
default:
return -EINVAL;
}
return 0;
}
static int irq_sim_request_resources(struct irq_data *data)
{
struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
struct irq_sim_work_ctx *work_ctx = irq_ctx->work_ctx;
irq_hw_number_t hwirq = irqd_to_hwirq(data);
if (work_ctx->ops.irq_sim_irq_requested)
return work_ctx->ops.irq_sim_irq_requested(work_ctx->domain,
hwirq,
work_ctx->user_data);
return 0;
}
static void irq_sim_release_resources(struct irq_data *data)
{
struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
struct irq_sim_work_ctx *work_ctx = irq_ctx->work_ctx;
irq_hw_number_t hwirq = irqd_to_hwirq(data);
if (work_ctx->ops.irq_sim_irq_released)
work_ctx->ops.irq_sim_irq_released(work_ctx->domain, hwirq,
work_ctx->user_data);
}
static struct irq_chip irq_sim_irqchip = {
.name = "irq_sim",
.irq_mask = irq_sim_irqmask,
.irq_unmask = irq_sim_irqunmask,
.irq_set_type = irq_sim_set_type,
.irq_get_irqchip_state = irq_sim_get_irqchip_state,
.irq_set_irqchip_state = irq_sim_set_irqchip_state,
.irq_request_resources = irq_sim_request_resources,
.irq_release_resources = irq_sim_release_resources,
};
static void irq_sim_handle_irq(struct irq_work *work)
{
struct irq_sim_work_ctx *work_ctx;
unsigned int offset = 0;
int irqnum;
work_ctx = container_of(work, struct irq_sim_work_ctx, work);
while (!bitmap_empty(work_ctx->pending, work_ctx->irq_count)) {
offset = find_next_bit(work_ctx->pending,
work_ctx->irq_count, offset);
clear_bit(offset, work_ctx->pending);
irqnum = irq_find_mapping(work_ctx->domain, offset);
handle_simple_irq(irq_to_desc(irqnum));
}
}
static int irq_sim_domain_map(struct irq_domain *domain,
unsigned int virq, irq_hw_number_t hw)
{
struct irq_sim_work_ctx *work_ctx = domain->host_data;
struct irq_sim_irq_ctx *irq_ctx;
irq_ctx = kzalloc(sizeof(*irq_ctx), GFP_KERNEL);
if (!irq_ctx)
return -ENOMEM;
irq_set_chip(virq, &irq_sim_irqchip);
irq_set_chip_data(virq, irq_ctx);
irq_set_handler(virq, handle_simple_irq);
irq_modify_status(virq, IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
irq_ctx->work_ctx = work_ctx;
return 0;
}
static void irq_sim_domain_unmap(struct irq_domain *domain, unsigned int virq)
{
struct irq_sim_irq_ctx *irq_ctx;
struct irq_data *irqd;
irqd = irq_domain_get_irq_data(domain, virq);
irq_ctx = irq_data_get_irq_chip_data(irqd);
irq_set_handler(virq, NULL);
irq_domain_reset_irq_data(irqd);
kfree(irq_ctx);
}
static const struct irq_domain_ops irq_sim_domain_ops = {
.map = irq_sim_domain_map,
.unmap = irq_sim_domain_unmap,
};
/**
* irq_domain_create_sim - Create a new interrupt simulator irq_domain and
* allocate a range of dummy interrupts.
*
* @fwnode: struct fwnode_handle to be associated with this domain.
* @num_irqs: Number of interrupts to allocate.
*
* On success: return a new irq_domain object.
* On failure: a negative errno wrapped with ERR_PTR().
*/
struct irq_domain *irq_domain_create_sim(struct fwnode_handle *fwnode,
unsigned int num_irqs)
{
return irq_domain_create_sim_full(fwnode, num_irqs, NULL, NULL);
}
EXPORT_SYMBOL_GPL(irq_domain_create_sim);
struct irq_domain *irq_domain_create_sim_full(struct fwnode_handle *fwnode,
unsigned int num_irqs,
const struct irq_sim_ops *ops,
void *data)
{
struct irq_sim_work_ctx *work_ctx __free(kfree) =
kmalloc(sizeof(*work_ctx), GFP_KERNEL);
if (!work_ctx)
return ERR_PTR(-ENOMEM);
unsigned long *pending __free(bitmap) = bitmap_zalloc(num_irqs, GFP_KERNEL);
if (!pending)
return ERR_PTR(-ENOMEM);
work_ctx->domain = irq_domain_create_linear(fwnode, num_irqs,
&irq_sim_domain_ops,
work_ctx);
if (!work_ctx->domain)
return ERR_PTR(-ENOMEM);
work_ctx->irq_count = num_irqs;
work_ctx->work = IRQ_WORK_INIT_HARD(irq_sim_handle_irq);
work_ctx->pending = no_free_ptr(pending);
work_ctx->user_data = data;
if (ops)
memcpy(&work_ctx->ops, ops, sizeof(*ops));
return no_free_ptr(work_ctx)->domain;
}
EXPORT_SYMBOL_GPL(irq_domain_create_sim_full);
/**
* irq_domain_remove_sim - Deinitialize the interrupt simulator domain: free
* the interrupt descriptors and allocated memory.
*
* @domain: The interrupt simulator domain to tear down.
*/
void irq_domain_remove_sim(struct irq_domain *domain)
{
struct irq_sim_work_ctx *work_ctx = domain->host_data;
irq_work_sync(&work_ctx->work);
bitmap_free(work_ctx->pending);
kfree(work_ctx);
irq_domain_remove(domain);
}
EXPORT_SYMBOL_GPL(irq_domain_remove_sim);
static void devm_irq_domain_remove_sim(void *data)
{
struct irq_domain *domain = data;
irq_domain_remove_sim(domain);
}
/**
* devm_irq_domain_create_sim - Create a new interrupt simulator for
* a managed device.
*
* @dev: Device to initialize the simulator object for.
* @fwnode: struct fwnode_handle to be associated with this domain.
* @num_irqs: Number of interrupts to allocate
*
* On success: return a new irq_domain object.
* On failure: a negative errno wrapped with ERR_PTR().
*/
struct irq_domain *devm_irq_domain_create_sim(struct device *dev,
struct fwnode_handle *fwnode,
unsigned int num_irqs)
{
return devm_irq_domain_create_sim_full(dev, fwnode, num_irqs,
NULL, NULL);
}
EXPORT_SYMBOL_GPL(devm_irq_domain_create_sim);
struct irq_domain *
devm_irq_domain_create_sim_full(struct device *dev,
struct fwnode_handle *fwnode,
unsigned int num_irqs,
const struct irq_sim_ops *ops,
void *data)
{
struct irq_domain *domain;
int ret;
domain = irq_domain_create_sim_full(fwnode, num_irqs, ops, data);
if (IS_ERR(domain))
return domain;
ret = devm_add_action_or_reset(dev, devm_irq_domain_remove_sim, domain);
if (ret)
return ERR_PTR(ret);
return domain;
}
EXPORT_SYMBOL_GPL(devm_irq_domain_create_sim_full);
|