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_gsi_domain_id;
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)
29 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
32 *irq = irq_find_mapping(d, gsi);
34 * *irq == 0 means no mapping, that should
35 * be reported as a failure
37 return (*irq > 0) ? 0 : -EINVAL;
39 EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
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
48 * Returns: a valid linux IRQ number on success
51 int acpi_register_gsi(struct device *dev, u32 gsi, int trigger,
54 struct irq_fwspec fwspec;
57 if (WARN_ON(!acpi_gsi_domain_id)) {
58 pr_warn("GSI: No registered irqchip, giving up\n");
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;
67 irq = irq_create_fwspec_mapping(&fwspec);
73 EXPORT_SYMBOL_GPL(acpi_register_gsi);
76 * acpi_unregister_gsi() - Free a GSI<->linux IRQ number mapping
77 * @gsi: GSI IRQ number
79 void acpi_unregister_gsi(u32 gsi)
81 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
83 int irq = irq_find_mapping(d, gsi);
85 irq_dispose_mapping(irq);
87 EXPORT_SYMBOL_GPL(acpi_unregister_gsi);
90 * acpi_get_irq_source_fwhandle() - Retrieve fwhandle from IRQ resource source.
91 * @source: acpi_resource_source to use for the lookup.
94 * Retrieve the fwhandle of the device referenced by the given IRQ resource
98 * The referenced device fwhandle or NULL on failure
100 static struct fwnode_handle *
101 acpi_get_irq_source_fwhandle(const struct acpi_resource_source *source)
103 struct fwnode_handle *result;
104 struct acpi_device *device;
108 if (!source->string_length)
109 return acpi_gsi_domain_id;
111 status = acpi_get_handle(NULL, source->string_ptr, &handle);
112 if (WARN_ON(ACPI_FAILURE(status)))
115 device = acpi_bus_get_acpi_device(handle);
116 if (WARN_ON(!device))
119 result = &device->fwnode;
120 acpi_bus_put_acpi_device(device);
125 * Context for the resource walk used to lookup IRQ resources.
126 * Contains a return code, the lookup index, and references to the flags
127 * and fwspec where the result is returned.
129 struct acpi_irq_parse_one_ctx {
132 unsigned long *res_flags;
133 struct irq_fwspec *fwspec;
137 * acpi_irq_parse_one_match - Handle a matching IRQ resource.
138 * @fwnode: matching fwnode
139 * @hwirq: hardware IRQ number
140 * @triggering: triggering attributes of hwirq
141 * @polarity: polarity attributes of hwirq
142 * @polarity: polarity attributes of hwirq
143 * @shareable: shareable attributes of hwirq
144 * @ctx: acpi_irq_parse_one_ctx updated by this function
147 * Handle a matching IRQ resource by populating the given ctx with
148 * the information passed.
150 static inline void acpi_irq_parse_one_match(struct fwnode_handle *fwnode,
151 u32 hwirq, u8 triggering,
152 u8 polarity, u8 shareable,
153 struct acpi_irq_parse_one_ctx *ctx)
158 *ctx->res_flags = acpi_dev_irq_flags(triggering, polarity, shareable);
159 ctx->fwspec->fwnode = fwnode;
160 ctx->fwspec->param[0] = hwirq;
161 ctx->fwspec->param[1] = acpi_dev_get_irq_type(triggering, polarity);
162 ctx->fwspec->param_count = 2;
166 * acpi_irq_parse_one_cb - Handle the given resource.
167 * @ares: resource to handle
168 * @context: context for the walk
171 * This is called by acpi_walk_resources passing each resource returned by
172 * the _CRS method. We only inspect IRQ resources. Since IRQ resources
173 * might contain multiple interrupts we check if the index is within this
174 * one's interrupt array, otherwise we subtract the current resource IRQ
175 * count from the lookup index to prepare for the next resource.
176 * Once a match is found we call acpi_irq_parse_one_match to populate
177 * the result and end the walk by returning AE_CTRL_TERMINATE.
180 * AE_OK if the walk should continue, AE_CTRL_TERMINATE if a matching
181 * IRQ resource was found.
183 static acpi_status acpi_irq_parse_one_cb(struct acpi_resource *ares,
186 struct acpi_irq_parse_one_ctx *ctx = context;
187 struct acpi_resource_irq *irq;
188 struct acpi_resource_extended_irq *eirq;
189 struct fwnode_handle *fwnode;
191 switch (ares->type) {
192 case ACPI_RESOURCE_TYPE_IRQ:
193 irq = &ares->data.irq;
194 if (ctx->index >= irq->interrupt_count) {
195 ctx->index -= irq->interrupt_count;
198 fwnode = acpi_gsi_domain_id;
199 acpi_irq_parse_one_match(fwnode, irq->interrupts[ctx->index],
200 irq->triggering, irq->polarity,
201 irq->shareable, ctx);
202 return AE_CTRL_TERMINATE;
203 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
204 eirq = &ares->data.extended_irq;
205 if (eirq->producer_consumer == ACPI_PRODUCER)
207 if (ctx->index >= eirq->interrupt_count) {
208 ctx->index -= eirq->interrupt_count;
211 fwnode = acpi_get_irq_source_fwhandle(&eirq->resource_source);
212 acpi_irq_parse_one_match(fwnode, eirq->interrupts[ctx->index],
213 eirq->triggering, eirq->polarity,
214 eirq->shareable, ctx);
215 return AE_CTRL_TERMINATE;
222 * acpi_irq_parse_one - Resolve an interrupt for a device
223 * @handle: the device whose interrupt is to be resolved
224 * @index: index of the interrupt to resolve
225 * @fwspec: structure irq_fwspec filled by this function
226 * @flags: resource flags filled by this function
229 * Resolves an interrupt for a device by walking its CRS resources to find
230 * the appropriate ACPI IRQ resource and populating the given struct irq_fwspec
234 * The result stored in ctx.rc by the callback, or the default -EINVAL value
235 * if an error occurs.
237 static int acpi_irq_parse_one(acpi_handle handle, unsigned int index,
238 struct irq_fwspec *fwspec, unsigned long *flags)
240 struct acpi_irq_parse_one_ctx ctx = { -EINVAL, index, flags, fwspec };
242 acpi_walk_resources(handle, METHOD_NAME__CRS, acpi_irq_parse_one_cb, &ctx);
247 * acpi_irq_get - Lookup an ACPI IRQ resource and use it to initialize resource.
248 * @handle: ACPI device handle
249 * @index: ACPI IRQ resource index to lookup
250 * @res: Linux IRQ resource to initialize
253 * Look for the ACPI IRQ resource with the given index and use it to initialize
254 * the given Linux IRQ resource.
258 * -EINVAL if an error occurs
259 * -EPROBE_DEFER if the IRQ lookup/conversion failed
261 int acpi_irq_get(acpi_handle handle, unsigned int index, struct resource *res)
263 struct irq_fwspec fwspec;
264 struct irq_domain *domain;
268 rc = acpi_irq_parse_one(handle, index, &fwspec, &flags);
272 domain = irq_find_matching_fwnode(fwspec.fwnode, DOMAIN_BUS_ANY);
274 return -EPROBE_DEFER;
276 rc = irq_create_fwspec_mapping(&fwspec);
286 EXPORT_SYMBOL_GPL(acpi_irq_get);
289 * acpi_set_irq_model - Setup the GSI irqdomain information
290 * @model: the value assigned to acpi_irq_model
291 * @fwnode: the irq_domain identifier for mapping and looking up
294 void __init acpi_set_irq_model(enum acpi_irq_model_id model,
295 struct fwnode_handle *fwnode)
297 acpi_irq_model = model;
298 acpi_gsi_domain_id = fwnode;
302 * acpi_irq_create_hierarchy - Create a hierarchical IRQ domain with the default
303 * GSI domain as its parent.
304 * @flags: Irq domain flags associated with the domain
305 * @size: Size of the domain.
306 * @fwnode: Optional fwnode of the interrupt controller
307 * @ops: Pointer to the interrupt domain callbacks
308 * @host_data: Controller private data pointer
310 struct irq_domain *acpi_irq_create_hierarchy(unsigned int flags,
312 struct fwnode_handle *fwnode,
313 const struct irq_domain_ops *ops,
316 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
322 return irq_domain_create_hierarchy(d, flags, size, fwnode, ops,
325 EXPORT_SYMBOL_GPL(acpi_irq_create_hierarchy);