APCI: irq: Add support for multiple GSI domains
[platform/kernel/linux-starfive.git] / drivers / acpi / irq.c
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
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  */
27 int acpi_gsi_to_irq(u32 gsi, unsigned int *irq)
28 {
29         struct irq_domain *d;
30
31         d = irq_find_matching_fwnode(acpi_get_gsi_domain_id(gsi),
32                                         DOMAIN_BUS_ANY);
33         *irq = irq_find_mapping(d, gsi);
34         /*
35          * *irq == 0 means no mapping, that should
36          * be reported as a failure
37          */
38         return (*irq > 0) ? 0 : -EINVAL;
39 }
40 EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
41
42 /**
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
48  *
49  * Returns: a valid linux IRQ number on success
50  *          -EINVAL on failure
51  */
52 int acpi_register_gsi(struct device *dev, u32 gsi, int trigger,
53                       int polarity)
54 {
55         struct irq_fwspec fwspec;
56
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");
60                 return -EINVAL;
61         }
62
63         fwspec.param[0] = gsi;
64         fwspec.param[1] = acpi_dev_get_irq_type(trigger, polarity);
65         fwspec.param_count = 2;
66
67         return irq_create_fwspec_mapping(&fwspec);
68 }
69 EXPORT_SYMBOL_GPL(acpi_register_gsi);
70
71 /**
72  * acpi_unregister_gsi() - Free a GSI<->linux IRQ number mapping
73  * @gsi: GSI IRQ number
74  */
75 void acpi_unregister_gsi(u32 gsi)
76 {
77         struct irq_domain *d;
78         int irq;
79
80         if (WARN_ON(acpi_irq_model == ACPI_IRQ_MODEL_GIC && gsi < 16))
81                 return;
82
83         d = irq_find_matching_fwnode(acpi_get_gsi_domain_id(gsi),
84                                      DOMAIN_BUS_ANY);
85         irq = irq_find_mapping(d, gsi);
86         irq_dispose_mapping(irq);
87 }
88 EXPORT_SYMBOL_GPL(acpi_unregister_gsi);
89
90 /**
91  * acpi_get_irq_source_fwhandle() - Retrieve fwhandle from IRQ resource source.
92  * @source: acpi_resource_source to use for the lookup.
93  *
94  * Description:
95  * Retrieve the fwhandle of the device referenced by the given IRQ resource
96  * source.
97  *
98  * Return:
99  * The referenced device fwhandle or NULL on failure
100  */
101 static struct fwnode_handle *
102 acpi_get_irq_source_fwhandle(const struct acpi_resource_source *source,
103                              u32 gsi)
104 {
105         struct fwnode_handle *result;
106         struct acpi_device *device;
107         acpi_handle handle;
108         acpi_status status;
109
110         if (!source->string_length)
111                 return acpi_get_gsi_domain_id(gsi);
112
113         status = acpi_get_handle(NULL, source->string_ptr, &handle);
114         if (WARN_ON(ACPI_FAILURE(status)))
115                 return NULL;
116
117         device = acpi_bus_get_acpi_device(handle);
118         if (WARN_ON(!device))
119                 return NULL;
120
121         result = &device->fwnode;
122         acpi_bus_put_acpi_device(device);
123         return result;
124 }
125
126 /*
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.
130  */
131 struct acpi_irq_parse_one_ctx {
132         int rc;
133         unsigned int index;
134         unsigned long *res_flags;
135         struct irq_fwspec *fwspec;
136 };
137
138 /**
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
147  *
148  * Description:
149  * Handle a matching IRQ resource by populating the given ctx with
150  * the information passed.
151  */
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)
156 {
157         if (!fwnode)
158                 return;
159         ctx->rc = 0;
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;
165 }
166
167 /**
168  * acpi_irq_parse_one_cb - Handle the given resource.
169  * @ares: resource to handle
170  * @context: context for the walk
171  *
172  * Description:
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.
180  *
181  * Return:
182  * AE_OK if the walk should continue, AE_CTRL_TERMINATE if a matching
183  * IRQ resource was found.
184  */
185 static acpi_status acpi_irq_parse_one_cb(struct acpi_resource *ares,
186                                          void *context)
187 {
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;
192
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;
198                         return AE_OK;
199                 }
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)
208                         return AE_OK;
209                 if (ctx->index >= eirq->interrupt_count) {
210                         ctx->index -= eirq->interrupt_count;
211                         return AE_OK;
212                 }
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;
219         }
220
221         return AE_OK;
222 }
223
224 /**
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
230  *
231  * Description:
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
234  * and flags.
235  *
236  * Return:
237  * The result stored in ctx.rc by the callback, or the default -EINVAL value
238  * if an error occurs.
239  */
240 static int acpi_irq_parse_one(acpi_handle handle, unsigned int index,
241                               struct irq_fwspec *fwspec, unsigned long *flags)
242 {
243         struct acpi_irq_parse_one_ctx ctx = { -EINVAL, index, flags, fwspec };
244
245         acpi_walk_resources(handle, METHOD_NAME__CRS, acpi_irq_parse_one_cb, &ctx);
246         return ctx.rc;
247 }
248
249 /**
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
254  *
255  * Description:
256  * Look for the ACPI IRQ resource with the given index and use it to initialize
257  * the given Linux IRQ resource.
258  *
259  * Return:
260  * 0 on success
261  * -EINVAL if an error occurs
262  * -EPROBE_DEFER if the IRQ lookup/conversion failed
263  */
264 int acpi_irq_get(acpi_handle handle, unsigned int index, struct resource *res)
265 {
266         struct irq_fwspec fwspec;
267         struct irq_domain *domain;
268         unsigned long flags;
269         int rc;
270
271         rc = acpi_irq_parse_one(handle, index, &fwspec, &flags);
272         if (rc)
273                 return rc;
274
275         domain = irq_find_matching_fwnode(fwspec.fwnode, DOMAIN_BUS_ANY);
276         if (!domain)
277                 return -EPROBE_DEFER;
278
279         rc = irq_create_fwspec_mapping(&fwspec);
280         if (rc <= 0)
281                 return -EINVAL;
282
283         res->start = rc;
284         res->end = rc;
285         res->flags = flags;
286
287         return 0;
288 }
289 EXPORT_SYMBOL_GPL(acpi_irq_get);
290
291 /**
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
295  *          GSI interrupts
296  */
297 void __init acpi_set_irq_model(enum acpi_irq_model_id model,
298                                struct fwnode_handle *(*fn)(u32))
299 {
300         acpi_irq_model = model;
301         acpi_get_gsi_domain_id = fn;
302 }
303
304 /**
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
312  */
313 struct irq_domain *acpi_irq_create_hierarchy(unsigned int flags,
314                                              unsigned int size,
315                                              struct fwnode_handle *fwnode,
316                                              const struct irq_domain_ops *ops,
317                                              void *host_data)
318 {
319         struct irq_domain *d;
320
321         /* This only works for the GIC model... */
322         if (acpi_irq_model != ACPI_IRQ_MODEL_GIC)
323                 return NULL;
324
325         d = irq_find_matching_fwnode(acpi_get_gsi_domain_id(0),
326                                      DOMAIN_BUS_ANY);
327
328         if (!d)
329                 return NULL;
330
331         return irq_domain_create_hierarchy(d, flags, size, fwnode, ops,
332                                            host_data);
333 }
334 EXPORT_SYMBOL_GPL(acpi_irq_create_hierarchy);