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