1
2
3
4
5
6
7
8#include <linux/acpi.h>
9#include <linux/irq.h>
10#include <linux/irqdomain.h>
11#include <linux/of.h>
12
13enum acpi_irq_model_id acpi_irq_model;
14
15static struct fwnode_handle *acpi_gsi_domain_id;
16
17
18
19
20
21
22
23
24
25
26
27int acpi_gsi_to_irq(u32 gsi, unsigned int *irq)
28{
29 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
30 DOMAIN_BUS_ANY);
31
32 *irq = irq_find_mapping(d, gsi);
33
34
35
36
37 return (*irq > 0) ? 0 : -EINVAL;
38}
39EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
40
41
42
43
44
45
46
47
48
49
50
51int acpi_register_gsi(struct device *dev, u32 gsi, int trigger,
52 int polarity)
53{
54 struct irq_fwspec fwspec;
55
56 if (WARN_ON(!acpi_gsi_domain_id)) {
57 pr_warn("GSI: No registered irqchip, giving up\n");
58 return -EINVAL;
59 }
60
61 fwspec.fwnode = acpi_gsi_domain_id;
62 fwspec.param[0] = gsi;
63 fwspec.param[1] = acpi_dev_get_irq_type(trigger, polarity);
64 fwspec.param_count = 2;
65
66 return irq_create_fwspec_mapping(&fwspec);
67}
68EXPORT_SYMBOL_GPL(acpi_register_gsi);
69
70
71
72
73
74void acpi_unregister_gsi(u32 gsi)
75{
76 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
77 DOMAIN_BUS_ANY);
78 int irq = irq_find_mapping(d, gsi);
79
80 irq_dispose_mapping(irq);
81}
82EXPORT_SYMBOL_GPL(acpi_unregister_gsi);
83
84
85
86
87
88
89
90
91
92
93
94
95static struct fwnode_handle *
96acpi_get_irq_source_fwhandle(const struct acpi_resource_source *source)
97{
98 struct fwnode_handle *result;
99 struct acpi_device *device;
100 acpi_handle handle;
101 acpi_status status;
102
103 if (!source->string_length)
104 return acpi_gsi_domain_id;
105
106 status = acpi_get_handle(NULL, source->string_ptr, &handle);
107 if (WARN_ON(ACPI_FAILURE(status)))
108 return NULL;
109
110 device = acpi_bus_get_acpi_device(handle);
111 if (WARN_ON(!device))
112 return NULL;
113
114 result = &device->fwnode;
115 acpi_bus_put_acpi_device(device);
116 return result;
117}
118
119
120
121
122
123
124struct acpi_irq_parse_one_ctx {
125 int rc;
126 unsigned int index;
127 unsigned long *res_flags;
128 struct irq_fwspec *fwspec;
129};
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145static inline void acpi_irq_parse_one_match(struct fwnode_handle *fwnode,
146 u32 hwirq, u8 triggering,
147 u8 polarity, u8 shareable,
148 struct acpi_irq_parse_one_ctx *ctx)
149{
150 if (!fwnode)
151 return;
152 ctx->rc = 0;
153 *ctx->res_flags = acpi_dev_irq_flags(triggering, polarity, shareable);
154 ctx->fwspec->fwnode = fwnode;
155 ctx->fwspec->param[0] = hwirq;
156 ctx->fwspec->param[1] = acpi_dev_get_irq_type(triggering, polarity);
157 ctx->fwspec->param_count = 2;
158}
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178static acpi_status acpi_irq_parse_one_cb(struct acpi_resource *ares,
179 void *context)
180{
181 struct acpi_irq_parse_one_ctx *ctx = context;
182 struct acpi_resource_irq *irq;
183 struct acpi_resource_extended_irq *eirq;
184 struct fwnode_handle *fwnode;
185
186 switch (ares->type) {
187 case ACPI_RESOURCE_TYPE_IRQ:
188 irq = &ares->data.irq;
189 if (ctx->index >= irq->interrupt_count) {
190 ctx->index -= irq->interrupt_count;
191 return AE_OK;
192 }
193 fwnode = acpi_gsi_domain_id;
194 acpi_irq_parse_one_match(fwnode, irq->interrupts[ctx->index],
195 irq->triggering, irq->polarity,
196 irq->shareable, ctx);
197 return AE_CTRL_TERMINATE;
198 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
199 eirq = &ares->data.extended_irq;
200 if (eirq->producer_consumer == ACPI_PRODUCER)
201 return AE_OK;
202 if (ctx->index >= eirq->interrupt_count) {
203 ctx->index -= eirq->interrupt_count;
204 return AE_OK;
205 }
206 fwnode = acpi_get_irq_source_fwhandle(&eirq->resource_source);
207 acpi_irq_parse_one_match(fwnode, eirq->interrupts[ctx->index],
208 eirq->triggering, eirq->polarity,
209 eirq->shareable, ctx);
210 return AE_CTRL_TERMINATE;
211 }
212
213 return AE_OK;
214}
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232static int acpi_irq_parse_one(acpi_handle handle, unsigned int index,
233 struct irq_fwspec *fwspec, unsigned long *flags)
234{
235 struct acpi_irq_parse_one_ctx ctx = { -EINVAL, index, flags, fwspec };
236
237 acpi_walk_resources(handle, METHOD_NAME__CRS, acpi_irq_parse_one_cb, &ctx);
238 return ctx.rc;
239}
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256int acpi_irq_get(acpi_handle handle, unsigned int index, struct resource *res)
257{
258 struct irq_fwspec fwspec;
259 struct irq_domain *domain;
260 unsigned long flags;
261 int rc;
262
263 rc = acpi_irq_parse_one(handle, index, &fwspec, &flags);
264 if (rc)
265 return rc;
266
267 domain = irq_find_matching_fwnode(fwspec.fwnode, DOMAIN_BUS_ANY);
268 if (!domain)
269 return -EPROBE_DEFER;
270
271 rc = irq_create_fwspec_mapping(&fwspec);
272 if (rc <= 0)
273 return -EINVAL;
274
275 res->start = rc;
276 res->end = rc;
277 res->flags = flags;
278
279 return 0;
280}
281EXPORT_SYMBOL_GPL(acpi_irq_get);
282
283
284
285
286
287
288
289void __init acpi_set_irq_model(enum acpi_irq_model_id model,
290 struct fwnode_handle *fwnode)
291{
292 acpi_irq_model = model;
293 acpi_gsi_domain_id = fwnode;
294}
295
296
297
298
299
300
301
302
303
304
305struct irq_domain *acpi_irq_create_hierarchy(unsigned int flags,
306 unsigned int size,
307 struct fwnode_handle *fwnode,
308 const struct irq_domain_ops *ops,
309 void *host_data)
310{
311 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
312 DOMAIN_BUS_ANY);
313
314 if (!d)
315 return NULL;
316
317 return irq_domain_create_hierarchy(d, flags, size, fwnode, ops,
318 host_data);
319}
320EXPORT_SYMBOL_GPL(acpi_irq_create_hierarchy);
321