1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2015 ARM Ltd.
6 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
8 #include <linux/acpi.h>
10 #include <linux/irqdomain.h>
13 enum acpi_irq_model_id acpi_irq_model;
15 static struct fwnode_handle *(*acpi_get_gsi_domain_id)(u32 gsi);
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
22 * irq location updated with irq value [>0 on success, 0 on failure]
24 * Returns: 0 on success
27 int acpi_gsi_to_irq(u32 gsi, unsigned int *irq)
31 d = irq_find_matching_fwnode(acpi_get_gsi_domain_id(gsi),
33 *irq = irq_find_mapping(d, gsi);
35 * *irq == 0 means no mapping, that should
36 * be reported as a failure
38 return (*irq > 0) ? 0 : -EINVAL;
40 EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
43 * acpi_register_gsi() - Map a GSI to a linux IRQ number
44 * @dev: device for which IRQ has to be mapped
45 * @gsi: GSI IRQ number
46 * @trigger: trigger type of the GSI number to be mapped
47 * @polarity: polarity of the GSI to be mapped
49 * Returns: a valid linux IRQ number on success
52 int acpi_register_gsi(struct device *dev, u32 gsi, int trigger,
55 struct irq_fwspec fwspec;
57 fwspec.fwnode = acpi_get_gsi_domain_id(gsi);
58 if (WARN_ON(!fwspec.fwnode)) {
59 pr_warn("GSI: No registered irqchip, giving up\n");
63 fwspec.param[0] = gsi;
64 fwspec.param[1] = acpi_dev_get_irq_type(trigger, polarity);
65 fwspec.param_count = 2;
67 return irq_create_fwspec_mapping(&fwspec);
69 EXPORT_SYMBOL_GPL(acpi_register_gsi);
72 * acpi_unregister_gsi() - Free a GSI<->linux IRQ number mapping
73 * @gsi: GSI IRQ number
75 void acpi_unregister_gsi(u32 gsi)
80 if (WARN_ON(acpi_irq_model == ACPI_IRQ_MODEL_GIC && gsi < 16))
83 d = irq_find_matching_fwnode(acpi_get_gsi_domain_id(gsi),
85 irq = irq_find_mapping(d, gsi);
86 irq_dispose_mapping(irq);
88 EXPORT_SYMBOL_GPL(acpi_unregister_gsi);
91 * acpi_get_irq_source_fwhandle() - Retrieve fwhandle from IRQ resource source.
92 * @source: acpi_resource_source to use for the lookup.
95 * Retrieve the fwhandle of the device referenced by the given IRQ resource
99 * The referenced device fwhandle or NULL on failure
101 static struct fwnode_handle *
102 acpi_get_irq_source_fwhandle(const struct acpi_resource_source *source,
105 struct fwnode_handle *result;
106 struct acpi_device *device;
110 if (!source->string_length)
111 return acpi_get_gsi_domain_id(gsi);
113 status = acpi_get_handle(NULL, source->string_ptr, &handle);
114 if (WARN_ON(ACPI_FAILURE(status)))
117 device = acpi_bus_get_acpi_device(handle);
118 if (WARN_ON(!device))
121 result = &device->fwnode;
122 acpi_bus_put_acpi_device(device);
127 * Context for the resource walk used to lookup IRQ resources.
128 * Contains a return code, the lookup index, and references to the flags
129 * and fwspec where the result is returned.
131 struct acpi_irq_parse_one_ctx {
134 unsigned long *res_flags;
135 struct irq_fwspec *fwspec;
139 * acpi_irq_parse_one_match - Handle a matching IRQ resource.
140 * @fwnode: matching fwnode
141 * @hwirq: hardware IRQ number
142 * @triggering: triggering attributes of hwirq
143 * @polarity: polarity attributes of hwirq
144 * @polarity: polarity attributes of hwirq
145 * @shareable: shareable attributes of hwirq
146 * @ctx: acpi_irq_parse_one_ctx updated by this function
149 * Handle a matching IRQ resource by populating the given ctx with
150 * the information passed.
152 static inline void acpi_irq_parse_one_match(struct fwnode_handle *fwnode,
153 u32 hwirq, u8 triggering,
154 u8 polarity, u8 shareable,
155 struct acpi_irq_parse_one_ctx *ctx)
160 *ctx->res_flags = acpi_dev_irq_flags(triggering, polarity, shareable);
161 ctx->fwspec->fwnode = fwnode;
162 ctx->fwspec->param[0] = hwirq;
163 ctx->fwspec->param[1] = acpi_dev_get_irq_type(triggering, polarity);
164 ctx->fwspec->param_count = 2;
168 * acpi_irq_parse_one_cb - Handle the given resource.
169 * @ares: resource to handle
170 * @context: context for the walk
173 * This is called by acpi_walk_resources passing each resource returned by
174 * the _CRS method. We only inspect IRQ resources. Since IRQ resources
175 * might contain multiple interrupts we check if the index is within this
176 * one's interrupt array, otherwise we subtract the current resource IRQ
177 * count from the lookup index to prepare for the next resource.
178 * Once a match is found we call acpi_irq_parse_one_match to populate
179 * the result and end the walk by returning AE_CTRL_TERMINATE.
182 * AE_OK if the walk should continue, AE_CTRL_TERMINATE if a matching
183 * IRQ resource was found.
185 static acpi_status acpi_irq_parse_one_cb(struct acpi_resource *ares,
188 struct acpi_irq_parse_one_ctx *ctx = context;
189 struct acpi_resource_irq *irq;
190 struct acpi_resource_extended_irq *eirq;
191 struct fwnode_handle *fwnode;
193 switch (ares->type) {
194 case ACPI_RESOURCE_TYPE_IRQ:
195 irq = &ares->data.irq;
196 if (ctx->index >= irq->interrupt_count) {
197 ctx->index -= irq->interrupt_count;
200 fwnode = acpi_get_gsi_domain_id(irq->interrupts[ctx->index]);
201 acpi_irq_parse_one_match(fwnode, irq->interrupts[ctx->index],
202 irq->triggering, irq->polarity,
203 irq->shareable, ctx);
204 return AE_CTRL_TERMINATE;
205 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
206 eirq = &ares->data.extended_irq;
207 if (eirq->producer_consumer == ACPI_PRODUCER)
209 if (ctx->index >= eirq->interrupt_count) {
210 ctx->index -= eirq->interrupt_count;
213 fwnode = acpi_get_irq_source_fwhandle(&eirq->resource_source,
214 eirq->interrupts[ctx->index]);
215 acpi_irq_parse_one_match(fwnode, eirq->interrupts[ctx->index],
216 eirq->triggering, eirq->polarity,
217 eirq->shareable, ctx);
218 return AE_CTRL_TERMINATE;
225 * acpi_irq_parse_one - Resolve an interrupt for a device
226 * @handle: the device whose interrupt is to be resolved
227 * @index: index of the interrupt to resolve
228 * @fwspec: structure irq_fwspec filled by this function
229 * @flags: resource flags filled by this function
232 * Resolves an interrupt for a device by walking its CRS resources to find
233 * the appropriate ACPI IRQ resource and populating the given struct irq_fwspec
237 * The result stored in ctx.rc by the callback, or the default -EINVAL value
238 * if an error occurs.
240 static int acpi_irq_parse_one(acpi_handle handle, unsigned int index,
241 struct irq_fwspec *fwspec, unsigned long *flags)
243 struct acpi_irq_parse_one_ctx ctx = { -EINVAL, index, flags, fwspec };
245 acpi_walk_resources(handle, METHOD_NAME__CRS, acpi_irq_parse_one_cb, &ctx);
250 * acpi_irq_get - Lookup an ACPI IRQ resource and use it to initialize resource.
251 * @handle: ACPI device handle
252 * @index: ACPI IRQ resource index to lookup
253 * @res: Linux IRQ resource to initialize
256 * Look for the ACPI IRQ resource with the given index and use it to initialize
257 * the given Linux IRQ resource.
261 * -EINVAL if an error occurs
262 * -EPROBE_DEFER if the IRQ lookup/conversion failed
264 int acpi_irq_get(acpi_handle handle, unsigned int index, struct resource *res)
266 struct irq_fwspec fwspec;
267 struct irq_domain *domain;
271 rc = acpi_irq_parse_one(handle, index, &fwspec, &flags);
275 domain = irq_find_matching_fwnode(fwspec.fwnode, DOMAIN_BUS_ANY);
277 return -EPROBE_DEFER;
279 rc = irq_create_fwspec_mapping(&fwspec);
289 EXPORT_SYMBOL_GPL(acpi_irq_get);
292 * acpi_set_irq_model - Setup the GSI irqdomain information
293 * @model: the value assigned to acpi_irq_model
294 * @fwnode: the irq_domain identifier for mapping and looking up
297 void __init acpi_set_irq_model(enum acpi_irq_model_id model,
298 struct fwnode_handle *(*fn)(u32))
300 acpi_irq_model = model;
301 acpi_get_gsi_domain_id = fn;
305 * acpi_irq_create_hierarchy - Create a hierarchical IRQ domain with the default
306 * GSI domain as its parent.
307 * @flags: Irq domain flags associated with the domain
308 * @size: Size of the domain.
309 * @fwnode: Optional fwnode of the interrupt controller
310 * @ops: Pointer to the interrupt domain callbacks
311 * @host_data: Controller private data pointer
313 struct irq_domain *acpi_irq_create_hierarchy(unsigned int flags,
315 struct fwnode_handle *fwnode,
316 const struct irq_domain_ops *ops,
319 struct irq_domain *d;
321 /* This only works for the GIC model... */
322 if (acpi_irq_model != ACPI_IRQ_MODEL_GIC)
325 d = irq_find_matching_fwnode(acpi_get_gsi_domain_id(0),
331 return irq_domain_create_hierarchy(d, flags, size, fwnode, ops,
334 EXPORT_SYMBOL_GPL(acpi_irq_create_hierarchy);