summaryrefslogtreecommitdiff
path: root/drivers/acpi/irq.c
blob: e4293458bf61986eec45eddfda08054ccce8dd17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// SPDX-License-Identifier: GPL-2.0-only
/*
 * ACPI GSI IRQ layer
 *
 * Copyright (C) 2015 ARM Ltd.
 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
 */
#include <linux/acpi.h>
#include <linux/irq.h>
#include <linux/irqdomain.h>
#include <linux/of.h>

enum acpi_irq_model_id acpi_irq_model;

static acpi_gsi_domain_disp_fn acpi_get_gsi_domain_id;
static acpi_gsi_handle_disp_fn acpi_get_gsi_handle;
static u32 (*acpi_gsi_to_irq_fallback)(u32 gsi);

/**
 * acpi_gsi_to_irq() - Retrieve the linux irq number for a given GSI
 * @gsi: GSI IRQ number to map
 * @irq: pointer where linux IRQ number is stored
 *
 * irq location updated with irq value [>0 on success, 0 on failure]
 *
 * Returns: 0 on success
 *          -EINVAL on failure
 */
int acpi_gsi_to_irq(u32 gsi, unsigned int *irq)
{
	struct irq_domain *d;

	d = irq_find_matching_fwnode(acpi_get_gsi_domain_id(gsi),
					DOMAIN_BUS_ANY);
	*irq = irq_find_mapping(d, gsi);
	/*
	 * *irq == 0 means no mapping, that should be reported as a
	 * failure, unless there is an arch-specific fallback handler.
	 */
	if (!*irq && acpi_gsi_to_irq_fallback)
		*irq = acpi_gsi_to_irq_fallback(gsi);

	return (*irq > 0) ? 0 : -EINVAL;
}
EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);

/**
 * acpi_register_gsi() - Map a GSI to a linux IRQ number
 * @dev: device for which IRQ has to be mapped
 * @gsi: GSI IRQ number
 * @trigger: trigger type of the GSI number to be mapped
 * @polarity: polarity of the GSI to be mapped
 *
 * Returns: a valid linux IRQ number on success
 *          -EINVAL on failure
 */
int acpi_register_gsi(struct device *dev, u32 gsi, int trigger,
		      int polarity)
{
	struct irq_fwspec fwspec;
	unsigned int irq;

	fwspec.fwnode = acpi_get_gsi_domain_id(gsi);
	if (WARN_ON(!fwspec.fwnode)) {
		pr_warn("GSI: No registered irqchip, giving up\n");
		return -EINVAL;
	}

	fwspec.param[0] = gsi;
	fwspec.param[1] = acpi_dev_get_irq_type(trigger, polarity);
	fwspec.param_count = 2;

	irq = irq_create_fwspec_mapping(&fwspec);
	if (!irq)
		return -EINVAL;

	return irq;
}
EXPORT_SYMBOL_GPL(acpi_register_gsi);

/**
 * acpi_unregister_gsi() - Free a GSI<->linux IRQ number mapping
 * @gsi: GSI IRQ number
 */
void acpi_unregister_gsi(u32 gsi)
{
	struct irq_domain *d;
	int irq;

	if (WARN_ON(acpi_irq_model == ACPI_IRQ_MODEL_GIC && gsi < 16))
		return;

	d = irq_find_matching_fwnode(acpi_get_gsi_domain_id(gsi),
				     DOMAIN_BUS_ANY);
	irq = irq_find_mapping(d, gsi);
	irq_dispose_mapping(irq);
}
EXPORT_SYMBOL_GPL(acpi_unregister_gsi);

/**
 * acpi_get_irq_source_fwhandle() - Retrieve fwhandle from IRQ resource source.
 * @source: acpi_resource_source to use for the lookup.
 * @gsi: GSI IRQ number
 *
 * Description:
 * Retrieve the fwhandle of the device referenced by the given IRQ resource
 * source.
 *
 * Return:
 * The referenced device fwhandle or NULL on failure
 */
static struct fwnode_handle *
acpi_get_irq_source_fwhandle(const struct acpi_resource_source *source,
			     u32 gsi)
{
	struct fwnode_handle *result;
	struct acpi_device *device;
	acpi_handle handle;
	acpi_status status;

	if (!source->string_length)
		return acpi_get_gsi_domain_id(gsi);

	status = acpi_get_handle(NULL, source->string_ptr, &handle);
	if (WARN_ON(ACPI_FAILURE(status)))
		return NULL;

	device = acpi_get_acpi_dev(handle);
	if (WARN_ON(!device))
		return NULL;

	result = &device->fwnode;
	acpi_put_acpi_dev(device);
	return result;
}

/*
 * Context for the resource walk used to lookup IRQ resources.
 * Contains a return code, the lookup index, and references to the flags
 * and fwspec where the result is returned.
 */
struct acpi_irq_parse_one_ctx {
	int rc;
	unsigned int index;
	unsigned long *res_flags;
	struct irq_fwspec *fwspec;
};

/**
 * acpi_irq_parse_one_match - Handle a matching IRQ resource.
 * @fwnode: matching fwnode
 * @hwirq: hardware IRQ number
 * @triggering: triggering attributes of hwirq
 * @polarity: polarity attributes of hwirq
 * @polarity: polarity attributes of hwirq
 * @shareable: shareable attributes of hwirq
 * @wake_capable: wake capable attribute of hwirq
 * @ctx: acpi_irq_parse_one_ctx updated by this function
 *
 * Description:
 * Handle a matching IRQ resource by populating the given ctx with
 * the information passed.
 */
static inline void acpi_irq_parse_one_match(struct fwnode_handle *fwnode,
					    u32 hwirq, u8 triggering,
					    u8 polarity, u8 shareable,
					    u8 wake_capable,
					    struct acpi_irq_parse_one_ctx *ctx)
{
	if (!fwnode)
		return;
	ctx->rc = 0;
	*ctx->res_flags = acpi_dev_irq_flags(triggering, polarity, shareable, wake_capable);
	ctx->fwspec->fwnode = fwnode;
	ctx->fwspec->param[0] = hwirq;
	ctx->fwspec->param[1] = acpi_dev_get_irq_type(triggering, polarity);
	ctx->fwspec->param_count = 2;
}

/**
 * acpi_irq_parse_one_cb - Handle the given resource.
 * @ares: resource to handle
 * @context: context for the walk
 *
 * Description:
 * This is called by acpi_walk_resources passing each resource returned by
 * the _CRS method. We only inspect IRQ resources. Since IRQ resources
 * might contain multiple interrupts we check if the index is within this
 * one's interrupt array, otherwise we subtract the current resource IRQ
 * count from the lookup index to prepare for the next resource.
 * Once a match is found we call acpi_irq_parse_one_match to populate
 * the result and end the walk by returning AE_CTRL_TERMINATE.
 *
 * Return:
 * AE_OK if the walk should continue, AE_CTRL_TERMINATE if a matching
 * IRQ resource was found.
 */
static acpi_status acpi_irq_parse_one_cb(struct acpi_resource *ares,
					 void *context)
{
	struct acpi_irq_parse_one_ctx *ctx = context;
	struct acpi_resource_irq *irq;
	struct acpi_resource_extended_irq *eirq;
	struct fwnode_handle *fwnode;

	switch (ares->type) {
	case ACPI_RESOURCE_TYPE_IRQ:
		irq = &ares->data.irq;
		if (ctx->index >= irq->interrupt_count) {
			ctx->index -= irq->interrupt_count;
			return AE_OK;
		}
		fwnode = acpi_get_gsi_domain_id(irq->interrupts[ctx->index]);
		acpi_irq_parse_one_match(fwnode, irq->interrupts[ctx->index],
					 irq->triggering, irq->polarity,
					 irq->shareable, irq->wake_capable, ctx);
		return AE_CTRL_TERMINATE;
	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
		eirq = &ares->data.extended_irq;
		if (eirq->producer_consumer == ACPI_PRODUCER)
			return AE_OK;
		if (ctx->index >= eirq->interrupt_count) {
			ctx->index -= eirq->interrupt_count;
			return AE_OK;
		}
		fwnode = acpi_get_irq_source_fwhandle(&eirq->resource_source,
						      eirq->interrupts[ctx->index]);
		acpi_irq_parse_one_match(fwnode, eirq->interrupts[ctx->index],
					 eirq->triggering, eirq->polarity,
					 eirq->shareable, eirq->wake_capable, ctx);
		return AE_CTRL_TERMINATE;
	}

	return AE_OK;
}

/**
 * acpi_irq_parse_one - Resolve an interrupt for a device
 * @handle: the device whose interrupt is to be resolved
 * @index: index of the interrupt to resolve
 * @fwspec: structure irq_fwspec filled by this function
 * @flags: resource flags filled by this function
 *
 * Description:
 * Resolves an interrupt for a device by walking its CRS resources to find
 * the appropriate ACPI IRQ resource and populating the given struct irq_fwspec
 * and flags.
 *
 * Return:
 * The result stored in ctx.rc by the callback, or the default -EINVAL value
 * if an error occurs.
 */
static int acpi_irq_parse_one(acpi_handle handle, unsigned int index,
			      struct irq_fwspec *fwspec, unsigned long *flags)
{
	struct acpi_irq_parse_one_ctx ctx = { -EINVAL, index, flags, fwspec };

	acpi_walk_resources(handle, METHOD_NAME__CRS, acpi_irq_parse_one_cb, &ctx);
	return ctx.rc;
}

/**
 * acpi_irq_get - Lookup an ACPI IRQ resource and use it to initialize resource.
 * @handle: ACPI device handle
 * @index:  ACPI IRQ resource index to lookup
 * @res:    Linux IRQ resource to initialize
 *
 * Description:
 * Look for the ACPI IRQ resource with the given index and use it to initialize
 * the given Linux IRQ resource.
 *
 * Return:
 * 0 on success
 * -EINVAL if an error occurs
 * -EPROBE_DEFER if the IRQ lookup/conversion failed
 */
int acpi_irq_get(acpi_handle handle, unsigned int index, struct resource *res)
{
	struct irq_fwspec fwspec;
	struct irq_domain *domain;
	unsigned long flags;
	int rc;

	rc = acpi_irq_parse_one(handle, index, &fwspec, &flags);
	if (rc)
		return rc;

	domain = irq_find_matching_fwnode(fwspec.fwnode, DOMAIN_BUS_ANY);
	if (!domain)
		return -EPROBE_DEFER;

	rc = irq_create_fwspec_mapping(&fwspec);
	if (rc <= 0)
		return -EINVAL;

	res->start = rc;
	res->end = rc;
	res->flags = flags;

	return 0;
}
EXPORT_SYMBOL_GPL(acpi_irq_get);

const struct cpumask *acpi_irq_get_affinity(acpi_handle handle,
					    unsigned int index)
{
	struct irq_fwspec_info info;
	struct irq_fwspec fwspec;
	unsigned long flags;

	if (acpi_irq_parse_one(handle, index, &fwspec, &flags))
		return NULL;

	if (irq_populate_fwspec_info(&fwspec, &info))
		return NULL;

	if (!(info.flags & IRQ_FWSPEC_INFO_AFFINITY_VALID))
		return NULL;

	return info.affinity;
}

/**
 * acpi_set_irq_model - Setup the GSI irqdomain information
 * @model:	the value assigned to acpi_irq_model
 * @fn:		a dispatcher function that will return the domain fwnode
 *		for a given GSI
 * @gsi_dep_fn: a function to retrieve the acpi_handle a GSI interrupt is
 *		dependent on
 *
 */
void __init acpi_set_irq_model(enum acpi_irq_model_id model,
			       acpi_gsi_domain_disp_fn fn, acpi_gsi_handle_disp_fn gsi_dep_fn)
{
	acpi_irq_model = model;
	acpi_get_gsi_domain_id = fn;
	acpi_get_gsi_handle = gsi_dep_fn;
}

/*
 * acpi_get_gsi_dispatcher() - Get the GSI dispatcher function
 *
 * Return the dispatcher function that computes the domain fwnode for
 * a given GSI.
 */
acpi_gsi_domain_disp_fn acpi_get_gsi_dispatcher(void)
{
	return acpi_get_gsi_domain_id;
}
EXPORT_SYMBOL_GPL(acpi_get_gsi_dispatcher);

/**
 * acpi_set_gsi_to_irq_fallback - Register a GSI transfer
 * callback to fallback to arch specified implementation.
 * @fn: arch-specific fallback handler
 */
void __init acpi_set_gsi_to_irq_fallback(u32 (*fn)(u32))
{
	acpi_gsi_to_irq_fallback = fn;
}

/**
 * acpi_irq_create_hierarchy - Create a hierarchical IRQ domain with the default
 *                             GSI domain as its parent.
 * @flags:      Irq domain flags associated with the domain
 * @size:       Size of the domain.
 * @fwnode:     Optional fwnode of the interrupt controller
 * @ops:        Pointer to the interrupt domain callbacks
 * @host_data:  Controller private data pointer
 */
struct irq_domain *acpi_irq_create_hierarchy(unsigned int flags,
					     unsigned int size,
					     struct fwnode_handle *fwnode,
					     const struct irq_domain_ops *ops,
					     void *host_data)
{
	struct irq_domain *d;

	/* This only works for the GIC model... */
	if (acpi_irq_model != ACPI_IRQ_MODEL_GIC)
		return NULL;

	d = irq_find_matching_fwnode(acpi_get_gsi_domain_id(0),
				     DOMAIN_BUS_ANY);

	if (!d)
		return NULL;

	return irq_domain_create_hierarchy(d, flags, size, fwnode, ops,
					   host_data);
}
EXPORT_SYMBOL_GPL(acpi_irq_create_hierarchy);

struct acpi_irq_dep_ctx {
	int		rc;
	unsigned int	index;
	acpi_handle	handle;
};

static acpi_status acpi_irq_get_parent(struct acpi_resource *ares, void *context)
{
	struct acpi_irq_dep_ctx *ctx = context;
	struct acpi_resource_irq *irq;
	struct acpi_resource_extended_irq *eirq;

	switch (ares->type) {
	case ACPI_RESOURCE_TYPE_IRQ:
		irq = &ares->data.irq;
		if (ctx->index >= irq->interrupt_count) {
			ctx->index -= irq->interrupt_count;
			return AE_OK;
		}
		ctx->handle = acpi_get_gsi_handle(irq->interrupts[ctx->index]);
		ctx->rc = 0;
		return AE_CTRL_TERMINATE;
	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
		eirq = &ares->data.extended_irq;
		if (eirq->producer_consumer == ACPI_PRODUCER)
			return AE_OK;

		if (ctx->index >= eirq->interrupt_count) {
			ctx->index -= eirq->interrupt_count;
			return AE_OK;
		}

		/* Support GSIs only */
		if (eirq->resource_source.string_length)
			return AE_OK;

		ctx->handle = acpi_get_gsi_handle(eirq->interrupts[ctx->index]);
		ctx->rc = 0;
		return AE_CTRL_TERMINATE;
	}

	return AE_OK;
}

static int acpi_irq_get_dep(acpi_handle handle, unsigned int index, acpi_handle *gsi_handle)
{
	struct acpi_irq_dep_ctx ctx = {-EINVAL, index, NULL};

	if (!gsi_handle)
		return -EINVAL;

	acpi_walk_resources(handle, METHOD_NAME__CRS, acpi_irq_get_parent, &ctx);
	*gsi_handle = ctx.handle;

	return ctx.rc;
}

static bool acpi_prt_entry_valid(void *prt_entry)
{
	struct acpi_pci_routing_table *entry = prt_entry;

	return entry && entry->length > 0;
}

static void *acpi_prt_next_entry(void *prt_entry)
{
	struct acpi_pci_routing_table *entry = prt_entry;

	return prt_entry + entry->length;
}

static u32 acpi_add_prt_dep(acpi_handle handle)
{
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
	struct acpi_pci_routing_table *entry;
	struct acpi_handle_list dep_devices;
	acpi_handle gsi_handle;
	acpi_handle link_handle;
	acpi_status status;
	u32 count = 0;

	status = acpi_get_irq_routing_table(handle, &buffer);
	if (ACPI_FAILURE(status)) {
		acpi_handle_err(handle, "failed to get IRQ routing table\n");
		kfree(buffer.pointer);
		return 0;
	}

	entry = buffer.pointer;
	for (; acpi_prt_entry_valid(entry); entry = acpi_prt_next_entry(entry)) {
		if (entry->source[0]) {
			status = acpi_get_handle(handle, entry->source, &link_handle);
			if (ACPI_FAILURE(status))
				continue;
			dep_devices.count = 1;
			dep_devices.handles = kcalloc(1, sizeof(*dep_devices.handles), GFP_KERNEL);
			if (!dep_devices.handles) {
				acpi_handle_err(handle, "failed to allocate memory\n");
				continue;
			}

			dep_devices.handles[0] = link_handle;
			count += acpi_scan_add_dep(handle, &dep_devices);
		} else {
			gsi_handle = acpi_get_gsi_handle(entry->source_index);
			if (!gsi_handle)
				continue;
			dep_devices.count = 1;
			dep_devices.handles = kcalloc(1, sizeof(*dep_devices.handles), GFP_KERNEL);
			if (!dep_devices.handles) {
				acpi_handle_err(handle, "failed to allocate memory\n");
				continue;
			}

			dep_devices.handles[0] = gsi_handle;
			count += acpi_scan_add_dep(handle, &dep_devices);
		}
	}

	kfree(buffer.pointer);
	return count;
}

static u32 acpi_add_irq_dep(acpi_handle handle)
{
	struct acpi_handle_list dep_devices;
	acpi_handle gsi_handle;
	u32 count = 0;
	int i;

	for (i = 0; !acpi_irq_get_dep(handle, i, &gsi_handle); i++) {
		if (!gsi_handle)
			continue;

		dep_devices.count = 1;
		dep_devices.handles = kcalloc(1, sizeof(*dep_devices.handles), GFP_KERNEL);
		if (!dep_devices.handles) {
			acpi_handle_err(handle, "failed to allocate memory\n");
			continue;
		}

		dep_devices.handles[0] = gsi_handle;
		count += acpi_scan_add_dep(handle, &dep_devices);
	}

	return count;
}

u32 acpi_irq_add_auto_dep(acpi_handle handle)
{
	if (!acpi_get_gsi_handle)
		return 0;

	if (acpi_has_method(handle, "_PRT"))
		return acpi_add_prt_dep(handle);

	return acpi_add_irq_dep(handle);
}