1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ACPI GSI IRQ layer
4 *
5 * Copyright (C) 2015 ARM Ltd.
6 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
7 */
8 #include <linux/acpi.h>
9 #include <linux/irq.h>
10 #include <linux/irqdomain.h>
11 #include <linux/of.h>
12
13 enum acpi_irq_model_id acpi_irq_model;
14
15 static struct fwnode_handle *acpi_gsi_domain_id;
16
17 /**
18 * acpi_gsi_to_irq() - Retrieve the linux irq number for a given GSI
19 * @gsi: GSI IRQ number to map
20 * @irq: pointer where linux IRQ number is stored
21 *
22 * irq location updated with irq value [>0 on success, 0 on failure]
23 *
24 * Returns: 0 on success
25 * -EINVAL on failure
26 */
acpi_gsi_to_irq(u32 gsi,unsigned int * irq)27 int 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 * *irq == 0 means no mapping, that should
35 * be reported as a failure
36 */
37 return (*irq > 0) ? 0 : -EINVAL;
38 }
39 EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
40
41 /**
42 * acpi_register_gsi() - Map a GSI to a linux IRQ number
43 * @dev: device for which IRQ has to be mapped
44 * @gsi: GSI IRQ number
45 * @trigger: trigger type of the GSI number to be mapped
46 * @polarity: polarity of the GSI to be mapped
47 *
48 * Returns: a valid linux IRQ number on success
49 * -EINVAL on failure
50 */
acpi_register_gsi(struct device * dev,u32 gsi,int trigger,int polarity)51 int acpi_register_gsi(struct device *dev, u32 gsi, int trigger,
52 int polarity)
53 {
54 struct irq_fwspec fwspec;
55 unsigned int irq;
56
57 if (WARN_ON(!acpi_gsi_domain_id)) {
58 pr_warn("GSI: No registered irqchip, giving up\n");
59 return -EINVAL;
60 }
61
62 fwspec.fwnode = acpi_gsi_domain_id;
63 fwspec.param[0] = gsi;
64 fwspec.param[1] = acpi_dev_get_irq_type(trigger, polarity);
65 fwspec.param_count = 2;
66
67 irq = irq_create_fwspec_mapping(&fwspec);
68 if (!irq)
69 return -EINVAL;
70
71 return irq;
72 }
73 EXPORT_SYMBOL_GPL(acpi_register_gsi);
74
75 /**
76 * acpi_unregister_gsi() - Free a GSI<->linux IRQ number mapping
77 * @gsi: GSI IRQ number
78 */
acpi_unregister_gsi(u32 gsi)79 void acpi_unregister_gsi(u32 gsi)
80 {
81 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
82 DOMAIN_BUS_ANY);
83 int irq;
84
85 if (WARN_ON(acpi_irq_model == ACPI_IRQ_MODEL_GIC && gsi < 16))
86 return;
87
88 irq = irq_find_mapping(d, gsi);
89 irq_dispose_mapping(irq);
90 }
91 EXPORT_SYMBOL_GPL(acpi_unregister_gsi);
92
93 /**
94 * acpi_get_irq_source_fwhandle() - Retrieve fwhandle from IRQ resource source.
95 * @source: acpi_resource_source to use for the lookup.
96 *
97 * Description:
98 * Retrieve the fwhandle of the device referenced by the given IRQ resource
99 * source.
100 *
101 * Return:
102 * The referenced device fwhandle or NULL on failure
103 */
104 static struct fwnode_handle *
acpi_get_irq_source_fwhandle(const struct acpi_resource_source * source)105 acpi_get_irq_source_fwhandle(const struct acpi_resource_source *source)
106 {
107 struct fwnode_handle *result;
108 struct acpi_device *device;
109 acpi_handle handle;
110 acpi_status status;
111
112 if (!source->string_length)
113 return acpi_gsi_domain_id;
114
115 status = acpi_get_handle(NULL, source->string_ptr, &handle);
116 if (WARN_ON(ACPI_FAILURE(status)))
117 return NULL;
118
119 device = acpi_bus_get_acpi_device(handle);
120 if (WARN_ON(!device))
121 return NULL;
122
123 result = &device->fwnode;
124 acpi_bus_put_acpi_device(device);
125 return result;
126 }
127
128 /*
129 * Context for the resource walk used to lookup IRQ resources.
130 * Contains a return code, the lookup index, and references to the flags
131 * and fwspec where the result is returned.
132 */
133 struct acpi_irq_parse_one_ctx {
134 int rc;
135 unsigned int index;
136 unsigned long *res_flags;
137 struct irq_fwspec *fwspec;
138 };
139
140 /**
141 * acpi_irq_parse_one_match - Handle a matching IRQ resource.
142 * @fwnode: matching fwnode
143 * @hwirq: hardware IRQ number
144 * @triggering: triggering attributes of hwirq
145 * @polarity: polarity attributes of hwirq
146 * @polarity: polarity attributes of hwirq
147 * @shareable: shareable attributes of hwirq
148 * @ctx: acpi_irq_parse_one_ctx updated by this function
149 *
150 * Description:
151 * Handle a matching IRQ resource by populating the given ctx with
152 * the information passed.
153 */
acpi_irq_parse_one_match(struct fwnode_handle * fwnode,u32 hwirq,u8 triggering,u8 polarity,u8 shareable,struct acpi_irq_parse_one_ctx * ctx)154 static inline void acpi_irq_parse_one_match(struct fwnode_handle *fwnode,
155 u32 hwirq, u8 triggering,
156 u8 polarity, u8 shareable,
157 struct acpi_irq_parse_one_ctx *ctx)
158 {
159 if (!fwnode)
160 return;
161 ctx->rc = 0;
162 *ctx->res_flags = acpi_dev_irq_flags(triggering, polarity, shareable);
163 ctx->fwspec->fwnode = fwnode;
164 ctx->fwspec->param[0] = hwirq;
165 ctx->fwspec->param[1] = acpi_dev_get_irq_type(triggering, polarity);
166 ctx->fwspec->param_count = 2;
167 }
168
169 /**
170 * acpi_irq_parse_one_cb - Handle the given resource.
171 * @ares: resource to handle
172 * @context: context for the walk
173 *
174 * Description:
175 * This is called by acpi_walk_resources passing each resource returned by
176 * the _CRS method. We only inspect IRQ resources. Since IRQ resources
177 * might contain multiple interrupts we check if the index is within this
178 * one's interrupt array, otherwise we subtract the current resource IRQ
179 * count from the lookup index to prepare for the next resource.
180 * Once a match is found we call acpi_irq_parse_one_match to populate
181 * the result and end the walk by returning AE_CTRL_TERMINATE.
182 *
183 * Return:
184 * AE_OK if the walk should continue, AE_CTRL_TERMINATE if a matching
185 * IRQ resource was found.
186 */
acpi_irq_parse_one_cb(struct acpi_resource * ares,void * context)187 static acpi_status acpi_irq_parse_one_cb(struct acpi_resource *ares,
188 void *context)
189 {
190 struct acpi_irq_parse_one_ctx *ctx = context;
191 struct acpi_resource_irq *irq;
192 struct acpi_resource_extended_irq *eirq;
193 struct fwnode_handle *fwnode;
194
195 switch (ares->type) {
196 case ACPI_RESOURCE_TYPE_IRQ:
197 irq = &ares->data.irq;
198 if (ctx->index >= irq->interrupt_count) {
199 ctx->index -= irq->interrupt_count;
200 return AE_OK;
201 }
202 fwnode = acpi_gsi_domain_id;
203 acpi_irq_parse_one_match(fwnode, irq->interrupts[ctx->index],
204 irq->triggering, irq->polarity,
205 irq->shareable, ctx);
206 return AE_CTRL_TERMINATE;
207 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
208 eirq = &ares->data.extended_irq;
209 if (eirq->producer_consumer == ACPI_PRODUCER)
210 return AE_OK;
211 if (ctx->index >= eirq->interrupt_count) {
212 ctx->index -= eirq->interrupt_count;
213 return AE_OK;
214 }
215 fwnode = acpi_get_irq_source_fwhandle(&eirq->resource_source);
216 acpi_irq_parse_one_match(fwnode, eirq->interrupts[ctx->index],
217 eirq->triggering, eirq->polarity,
218 eirq->shareable, ctx);
219 return AE_CTRL_TERMINATE;
220 }
221
222 return AE_OK;
223 }
224
225 /**
226 * acpi_irq_parse_one - Resolve an interrupt for a device
227 * @handle: the device whose interrupt is to be resolved
228 * @index: index of the interrupt to resolve
229 * @fwspec: structure irq_fwspec filled by this function
230 * @flags: resource flags filled by this function
231 *
232 * Description:
233 * Resolves an interrupt for a device by walking its CRS resources to find
234 * the appropriate ACPI IRQ resource and populating the given struct irq_fwspec
235 * and flags.
236 *
237 * Return:
238 * The result stored in ctx.rc by the callback, or the default -EINVAL value
239 * if an error occurs.
240 */
acpi_irq_parse_one(acpi_handle handle,unsigned int index,struct irq_fwspec * fwspec,unsigned long * flags)241 static int acpi_irq_parse_one(acpi_handle handle, unsigned int index,
242 struct irq_fwspec *fwspec, unsigned long *flags)
243 {
244 struct acpi_irq_parse_one_ctx ctx = { -EINVAL, index, flags, fwspec };
245
246 acpi_walk_resources(handle, METHOD_NAME__CRS, acpi_irq_parse_one_cb, &ctx);
247 return ctx.rc;
248 }
249
250 /**
251 * acpi_irq_get - Lookup an ACPI IRQ resource and use it to initialize resource.
252 * @handle: ACPI device handle
253 * @index: ACPI IRQ resource index to lookup
254 * @res: Linux IRQ resource to initialize
255 *
256 * Description:
257 * Look for the ACPI IRQ resource with the given index and use it to initialize
258 * the given Linux IRQ resource.
259 *
260 * Return:
261 * 0 on success
262 * -EINVAL if an error occurs
263 * -EPROBE_DEFER if the IRQ lookup/conversion failed
264 */
acpi_irq_get(acpi_handle handle,unsigned int index,struct resource * res)265 int acpi_irq_get(acpi_handle handle, unsigned int index, struct resource *res)
266 {
267 struct irq_fwspec fwspec;
268 struct irq_domain *domain;
269 unsigned long flags;
270 int rc;
271
272 rc = acpi_irq_parse_one(handle, index, &fwspec, &flags);
273 if (rc)
274 return rc;
275
276 domain = irq_find_matching_fwnode(fwspec.fwnode, DOMAIN_BUS_ANY);
277 if (!domain)
278 return -EPROBE_DEFER;
279
280 rc = irq_create_fwspec_mapping(&fwspec);
281 if (rc <= 0)
282 return -EINVAL;
283
284 res->start = rc;
285 res->end = rc;
286 res->flags = flags;
287
288 return 0;
289 }
290 EXPORT_SYMBOL_GPL(acpi_irq_get);
291
292 /**
293 * acpi_set_irq_model - Setup the GSI irqdomain information
294 * @model: the value assigned to acpi_irq_model
295 * @fwnode: the irq_domain identifier for mapping and looking up
296 * GSI interrupts
297 */
acpi_set_irq_model(enum acpi_irq_model_id model,struct fwnode_handle * fwnode)298 void __init acpi_set_irq_model(enum acpi_irq_model_id model,
299 struct fwnode_handle *fwnode)
300 {
301 acpi_irq_model = model;
302 acpi_gsi_domain_id = fwnode;
303 }
304
305 /**
306 * acpi_irq_create_hierarchy - Create a hierarchical IRQ domain with the default
307 * GSI domain as its parent.
308 * @flags: Irq domain flags associated with the domain
309 * @size: Size of the domain.
310 * @fwnode: Optional fwnode of the interrupt controller
311 * @ops: Pointer to the interrupt domain callbacks
312 * @host_data: Controller private data pointer
313 */
acpi_irq_create_hierarchy(unsigned int flags,unsigned int size,struct fwnode_handle * fwnode,const struct irq_domain_ops * ops,void * host_data)314 struct irq_domain *acpi_irq_create_hierarchy(unsigned int flags,
315 unsigned int size,
316 struct fwnode_handle *fwnode,
317 const struct irq_domain_ops *ops,
318 void *host_data)
319 {
320 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
321 DOMAIN_BUS_ANY);
322
323 if (!d)
324 return NULL;
325
326 return irq_domain_create_hierarchy(d, flags, size, fwnode, ops,
327 host_data);
328 }
329 EXPORT_SYMBOL_GPL(acpi_irq_create_hierarchy);
330