• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
4  * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
5  *
6  * This file contains the interrupt descriptor management code. Detailed
7  * information is available in Documentation/core-api/genericirq.rst
8  *
9  */
10 #include <linux/irq.h>
11 #include <linux/slab.h>
12 #include <linux/export.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/maple_tree.h>
16 #include <linux/irqdomain.h>
17 #include <linux/sysfs.h>
18 
19 #include "internals.h"
20 
21 /*
22  * lockdep: we want to handle all irq_desc locks as a single lock-class:
23  */
24 static struct lock_class_key irq_desc_lock_class;
25 
26 #if defined(CONFIG_SMP)
irq_affinity_setup(char * str)27 static int __init irq_affinity_setup(char *str)
28 {
29 	alloc_bootmem_cpumask_var(&irq_default_affinity);
30 	cpulist_parse(str, irq_default_affinity);
31 	/*
32 	 * Set at least the boot cpu. We don't want to end up with
33 	 * bugreports caused by random commandline masks
34 	 */
35 	cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
36 	return 1;
37 }
38 __setup("irqaffinity=", irq_affinity_setup);
39 
init_irq_default_affinity(void)40 static void __init init_irq_default_affinity(void)
41 {
42 	if (!cpumask_available(irq_default_affinity))
43 		zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
44 	if (cpumask_empty(irq_default_affinity))
45 		cpumask_setall(irq_default_affinity);
46 }
47 #else
init_irq_default_affinity(void)48 static void __init init_irq_default_affinity(void)
49 {
50 }
51 #endif
52 
53 #ifdef CONFIG_SMP
alloc_masks(struct irq_desc * desc,int node)54 static int alloc_masks(struct irq_desc *desc, int node)
55 {
56 	if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
57 				     GFP_KERNEL, node))
58 		return -ENOMEM;
59 
60 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
61 	if (!zalloc_cpumask_var_node(&desc->irq_common_data.effective_affinity,
62 				     GFP_KERNEL, node)) {
63 		free_cpumask_var(desc->irq_common_data.affinity);
64 		return -ENOMEM;
65 	}
66 #endif
67 
68 #ifdef CONFIG_GENERIC_PENDING_IRQ
69 	if (!zalloc_cpumask_var_node(&desc->pending_mask, GFP_KERNEL, node)) {
70 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
71 		free_cpumask_var(desc->irq_common_data.effective_affinity);
72 #endif
73 		free_cpumask_var(desc->irq_common_data.affinity);
74 		return -ENOMEM;
75 	}
76 #endif
77 	return 0;
78 }
79 
desc_smp_init(struct irq_desc * desc,int node,const struct cpumask * affinity)80 static void desc_smp_init(struct irq_desc *desc, int node,
81 			  const struct cpumask *affinity)
82 {
83 	if (!affinity)
84 		affinity = irq_default_affinity;
85 	cpumask_copy(desc->irq_common_data.affinity, affinity);
86 
87 #ifdef CONFIG_GENERIC_PENDING_IRQ
88 	cpumask_clear(desc->pending_mask);
89 #endif
90 #ifdef CONFIG_NUMA
91 	desc->irq_common_data.node = node;
92 #endif
93 }
94 
95 #else
96 static inline int
alloc_masks(struct irq_desc * desc,int node)97 alloc_masks(struct irq_desc *desc, int node) { return 0; }
98 static inline void
desc_smp_init(struct irq_desc * desc,int node,const struct cpumask * affinity)99 desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
100 #endif
101 
desc_set_defaults(unsigned int irq,struct irq_desc * desc,int node,const struct cpumask * affinity,struct module * owner)102 static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
103 			      const struct cpumask *affinity, struct module *owner)
104 {
105 	int cpu;
106 
107 	desc->irq_common_data.handler_data = NULL;
108 	desc->irq_common_data.msi_desc = NULL;
109 
110 	desc->irq_data.common = &desc->irq_common_data;
111 	desc->irq_data.irq = irq;
112 	desc->irq_data.chip = &no_irq_chip;
113 	desc->irq_data.chip_data = NULL;
114 	irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
115 	irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
116 	irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
117 	desc->handle_irq = handle_bad_irq;
118 	desc->depth = 1;
119 	desc->irq_count = 0;
120 	desc->irqs_unhandled = 0;
121 	desc->tot_count = 0;
122 	desc->name = NULL;
123 	desc->owner = owner;
124 	for_each_possible_cpu(cpu)
125 		*per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
126 	desc_smp_init(desc, node, affinity);
127 }
128 
129 int nr_irqs = NR_IRQS;
130 EXPORT_SYMBOL_GPL(nr_irqs);
131 
132 static DEFINE_MUTEX(sparse_irq_lock);
133 static struct maple_tree sparse_irqs = MTREE_INIT_EXT(sparse_irqs,
134 					MT_FLAGS_ALLOC_RANGE |
135 					MT_FLAGS_LOCK_EXTERN |
136 					MT_FLAGS_USE_RCU,
137 					sparse_irq_lock);
138 
irq_find_free_area(unsigned int from,unsigned int cnt)139 static int irq_find_free_area(unsigned int from, unsigned int cnt)
140 {
141 	MA_STATE(mas, &sparse_irqs, 0, 0);
142 
143 	if (mas_empty_area(&mas, from, MAX_SPARSE_IRQS, cnt))
144 		return -ENOSPC;
145 	return mas.index;
146 }
147 
irq_find_at_or_after(unsigned int offset)148 static unsigned int irq_find_at_or_after(unsigned int offset)
149 {
150 	unsigned long index = offset;
151 	struct irq_desc *desc;
152 
153 	guard(rcu)();
154 	desc = mt_find(&sparse_irqs, &index, nr_irqs);
155 
156 	return desc ? irq_desc_get_irq(desc) : nr_irqs;
157 }
158 
irq_insert_desc(unsigned int irq,struct irq_desc * desc)159 static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
160 {
161 	MA_STATE(mas, &sparse_irqs, irq, irq);
162 	WARN_ON(mas_store_gfp(&mas, desc, GFP_KERNEL) != 0);
163 }
164 
delete_irq_desc(unsigned int irq)165 static void delete_irq_desc(unsigned int irq)
166 {
167 	MA_STATE(mas, &sparse_irqs, irq, irq);
168 	mas_erase(&mas);
169 }
170 
171 #ifdef CONFIG_SPARSE_IRQ
172 
173 static void irq_kobj_release(struct kobject *kobj);
174 
175 #ifdef CONFIG_SYSFS
176 static struct kobject *irq_kobj_base;
177 
178 #define IRQ_ATTR_RO(_name) \
179 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
180 
per_cpu_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)181 static ssize_t per_cpu_count_show(struct kobject *kobj,
182 				  struct kobj_attribute *attr, char *buf)
183 {
184 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
185 	ssize_t ret = 0;
186 	char *p = "";
187 	int cpu;
188 
189 	for_each_possible_cpu(cpu) {
190 		unsigned int c = irq_desc_kstat_cpu(desc, cpu);
191 
192 		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
193 		p = ",";
194 	}
195 
196 	ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
197 	return ret;
198 }
199 IRQ_ATTR_RO(per_cpu_count);
200 
chip_name_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)201 static ssize_t chip_name_show(struct kobject *kobj,
202 			      struct kobj_attribute *attr, char *buf)
203 {
204 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
205 	ssize_t ret = 0;
206 
207 	raw_spin_lock_irq(&desc->lock);
208 	if (desc->irq_data.chip && desc->irq_data.chip->name) {
209 		ret = scnprintf(buf, PAGE_SIZE, "%s\n",
210 				desc->irq_data.chip->name);
211 	}
212 	raw_spin_unlock_irq(&desc->lock);
213 
214 	return ret;
215 }
216 IRQ_ATTR_RO(chip_name);
217 
hwirq_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)218 static ssize_t hwirq_show(struct kobject *kobj,
219 			  struct kobj_attribute *attr, char *buf)
220 {
221 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
222 	ssize_t ret = 0;
223 
224 	raw_spin_lock_irq(&desc->lock);
225 	if (desc->irq_data.domain)
226 		ret = sprintf(buf, "%lu\n", desc->irq_data.hwirq);
227 	raw_spin_unlock_irq(&desc->lock);
228 
229 	return ret;
230 }
231 IRQ_ATTR_RO(hwirq);
232 
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)233 static ssize_t type_show(struct kobject *kobj,
234 			 struct kobj_attribute *attr, char *buf)
235 {
236 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
237 	ssize_t ret = 0;
238 
239 	raw_spin_lock_irq(&desc->lock);
240 	ret = sprintf(buf, "%s\n",
241 		      irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
242 	raw_spin_unlock_irq(&desc->lock);
243 
244 	return ret;
245 
246 }
247 IRQ_ATTR_RO(type);
248 
wakeup_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)249 static ssize_t wakeup_show(struct kobject *kobj,
250 			   struct kobj_attribute *attr, char *buf)
251 {
252 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
253 	ssize_t ret = 0;
254 
255 	raw_spin_lock_irq(&desc->lock);
256 	ret = sprintf(buf, "%s\n",
257 		      irqd_is_wakeup_set(&desc->irq_data) ? "enabled" : "disabled");
258 	raw_spin_unlock_irq(&desc->lock);
259 
260 	return ret;
261 
262 }
263 IRQ_ATTR_RO(wakeup);
264 
name_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)265 static ssize_t name_show(struct kobject *kobj,
266 			 struct kobj_attribute *attr, char *buf)
267 {
268 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
269 	ssize_t ret = 0;
270 
271 	raw_spin_lock_irq(&desc->lock);
272 	if (desc->name)
273 		ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
274 	raw_spin_unlock_irq(&desc->lock);
275 
276 	return ret;
277 }
278 IRQ_ATTR_RO(name);
279 
actions_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)280 static ssize_t actions_show(struct kobject *kobj,
281 			    struct kobj_attribute *attr, char *buf)
282 {
283 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
284 	struct irqaction *action;
285 	ssize_t ret = 0;
286 	char *p = "";
287 
288 	raw_spin_lock_irq(&desc->lock);
289 	for_each_action_of_desc(desc, action) {
290 		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
291 				 p, action->name);
292 		p = ",";
293 	}
294 	raw_spin_unlock_irq(&desc->lock);
295 
296 	if (ret)
297 		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
298 
299 	return ret;
300 }
301 IRQ_ATTR_RO(actions);
302 
303 static struct attribute *irq_attrs[] = {
304 	&per_cpu_count_attr.attr,
305 	&chip_name_attr.attr,
306 	&hwirq_attr.attr,
307 	&type_attr.attr,
308 	&wakeup_attr.attr,
309 	&name_attr.attr,
310 	&actions_attr.attr,
311 	NULL
312 };
313 ATTRIBUTE_GROUPS(irq);
314 
315 static const struct kobj_type irq_kobj_type = {
316 	.release	= irq_kobj_release,
317 	.sysfs_ops	= &kobj_sysfs_ops,
318 	.default_groups = irq_groups,
319 };
320 
irq_sysfs_add(int irq,struct irq_desc * desc)321 static void irq_sysfs_add(int irq, struct irq_desc *desc)
322 {
323 	if (irq_kobj_base) {
324 		/*
325 		 * Continue even in case of failure as this is nothing
326 		 * crucial and failures in the late irq_sysfs_init()
327 		 * cannot be rolled back.
328 		 */
329 		if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
330 			pr_warn("Failed to add kobject for irq %d\n", irq);
331 		else
332 			desc->istate |= IRQS_SYSFS;
333 	}
334 }
335 
irq_sysfs_del(struct irq_desc * desc)336 static void irq_sysfs_del(struct irq_desc *desc)
337 {
338 	/*
339 	 * Only invoke kobject_del() when kobject_add() was successfully
340 	 * invoked for the descriptor. This covers both early boot, where
341 	 * sysfs is not initialized yet, and the case of a failed
342 	 * kobject_add() invocation.
343 	 */
344 	if (desc->istate & IRQS_SYSFS)
345 		kobject_del(&desc->kobj);
346 }
347 
irq_sysfs_init(void)348 static int __init irq_sysfs_init(void)
349 {
350 	struct irq_desc *desc;
351 	int irq;
352 
353 	/* Prevent concurrent irq alloc/free */
354 	irq_lock_sparse();
355 
356 	irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
357 	if (!irq_kobj_base) {
358 		irq_unlock_sparse();
359 		return -ENOMEM;
360 	}
361 
362 	/* Add the already allocated interrupts */
363 	for_each_irq_desc(irq, desc)
364 		irq_sysfs_add(irq, desc);
365 	irq_unlock_sparse();
366 
367 	return 0;
368 }
369 postcore_initcall(irq_sysfs_init);
370 
371 #else /* !CONFIG_SYSFS */
372 
373 static const struct kobj_type irq_kobj_type = {
374 	.release	= irq_kobj_release,
375 };
376 
irq_sysfs_add(int irq,struct irq_desc * desc)377 static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
irq_sysfs_del(struct irq_desc * desc)378 static void irq_sysfs_del(struct irq_desc *desc) {}
379 
380 #endif /* CONFIG_SYSFS */
381 
irq_to_desc(unsigned int irq)382 struct irq_desc *irq_to_desc(unsigned int irq)
383 {
384 	return mtree_load(&sparse_irqs, irq);
385 }
386 EXPORT_SYMBOL_GPL(irq_to_desc);
387 
388 #ifdef CONFIG_SMP
free_masks(struct irq_desc * desc)389 static void free_masks(struct irq_desc *desc)
390 {
391 #ifdef CONFIG_GENERIC_PENDING_IRQ
392 	free_cpumask_var(desc->pending_mask);
393 #endif
394 	free_cpumask_var(desc->irq_common_data.affinity);
395 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
396 	free_cpumask_var(desc->irq_common_data.effective_affinity);
397 #endif
398 }
399 #else
free_masks(struct irq_desc * desc)400 static inline void free_masks(struct irq_desc *desc) { }
401 #endif
402 
irq_lock_sparse(void)403 void irq_lock_sparse(void)
404 {
405 	mutex_lock(&sparse_irq_lock);
406 }
407 
irq_unlock_sparse(void)408 void irq_unlock_sparse(void)
409 {
410 	mutex_unlock(&sparse_irq_lock);
411 }
412 
alloc_desc(int irq,int node,unsigned int flags,const struct cpumask * affinity,struct module * owner)413 static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
414 				   const struct cpumask *affinity,
415 				   struct module *owner)
416 {
417 	struct irq_desc *desc;
418 
419 	desc = kzalloc_node(sizeof(*desc), GFP_KERNEL, node);
420 	if (!desc)
421 		return NULL;
422 	/* allocate based on nr_cpu_ids */
423 	desc->kstat_irqs = alloc_percpu(unsigned int);
424 	if (!desc->kstat_irqs)
425 		goto err_desc;
426 
427 	if (alloc_masks(desc, node))
428 		goto err_kstat;
429 
430 	raw_spin_lock_init(&desc->lock);
431 	lockdep_set_class(&desc->lock, &irq_desc_lock_class);
432 	mutex_init(&desc->request_mutex);
433 	init_rcu_head(&desc->rcu);
434 	init_waitqueue_head(&desc->wait_for_threads);
435 
436 	desc_set_defaults(irq, desc, node, affinity, owner);
437 	irqd_set(&desc->irq_data, flags);
438 	kobject_init(&desc->kobj, &irq_kobj_type);
439 	irq_resend_init(desc);
440 
441 	return desc;
442 
443 err_kstat:
444 	free_percpu(desc->kstat_irqs);
445 err_desc:
446 	kfree(desc);
447 	return NULL;
448 }
449 
irq_kobj_release(struct kobject * kobj)450 static void irq_kobj_release(struct kobject *kobj)
451 {
452 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
453 
454 	free_masks(desc);
455 	free_percpu(desc->kstat_irqs);
456 	kfree(desc);
457 }
458 
delayed_free_desc(struct rcu_head * rhp)459 static void delayed_free_desc(struct rcu_head *rhp)
460 {
461 	struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
462 
463 	kobject_put(&desc->kobj);
464 }
465 
free_desc(unsigned int irq)466 static void free_desc(unsigned int irq)
467 {
468 	struct irq_desc *desc = irq_to_desc(irq);
469 
470 	irq_remove_debugfs_entry(desc);
471 	unregister_irq_proc(irq, desc);
472 
473 	/*
474 	 * sparse_irq_lock protects also show_interrupts() and
475 	 * kstat_irq_usr(). Once we deleted the descriptor from the
476 	 * sparse tree we can free it. Access in proc will fail to
477 	 * lookup the descriptor.
478 	 *
479 	 * The sysfs entry must be serialized against a concurrent
480 	 * irq_sysfs_init() as well.
481 	 */
482 	irq_sysfs_del(desc);
483 	delete_irq_desc(irq);
484 
485 	/*
486 	 * We free the descriptor, masks and stat fields via RCU. That
487 	 * allows demultiplex interrupts to do rcu based management of
488 	 * the child interrupts.
489 	 * This also allows us to use rcu in kstat_irqs_usr().
490 	 */
491 	call_rcu(&desc->rcu, delayed_free_desc);
492 }
493 
alloc_descs(unsigned int start,unsigned int cnt,int node,const struct irq_affinity_desc * affinity,struct module * owner)494 static int alloc_descs(unsigned int start, unsigned int cnt, int node,
495 		       const struct irq_affinity_desc *affinity,
496 		       struct module *owner)
497 {
498 	struct irq_desc *desc;
499 	int i;
500 
501 	/* Validate affinity mask(s) */
502 	if (affinity) {
503 		for (i = 0; i < cnt; i++) {
504 			if (cpumask_empty(&affinity[i].mask))
505 				return -EINVAL;
506 		}
507 	}
508 
509 	for (i = 0; i < cnt; i++) {
510 		const struct cpumask *mask = NULL;
511 		unsigned int flags = 0;
512 
513 		if (affinity) {
514 			if (affinity->is_managed) {
515 				flags = IRQD_AFFINITY_MANAGED |
516 					IRQD_MANAGED_SHUTDOWN;
517 			}
518 			flags |= IRQD_AFFINITY_SET;
519 			mask = &affinity->mask;
520 			node = cpu_to_node(cpumask_first(mask));
521 			affinity++;
522 		}
523 
524 		desc = alloc_desc(start + i, node, flags, mask, owner);
525 		if (!desc)
526 			goto err;
527 		irq_insert_desc(start + i, desc);
528 		irq_sysfs_add(start + i, desc);
529 		irq_add_debugfs_entry(start + i, desc);
530 	}
531 	return start;
532 
533 err:
534 	for (i--; i >= 0; i--)
535 		free_desc(start + i);
536 	return -ENOMEM;
537 }
538 
irq_expand_nr_irqs(unsigned int nr)539 static int irq_expand_nr_irqs(unsigned int nr)
540 {
541 	if (nr > MAX_SPARSE_IRQS)
542 		return -ENOMEM;
543 	nr_irqs = nr;
544 	return 0;
545 }
546 
early_irq_init(void)547 int __init early_irq_init(void)
548 {
549 	int i, initcnt, node = first_online_node;
550 	struct irq_desc *desc;
551 
552 	init_irq_default_affinity();
553 
554 	/* Let arch update nr_irqs and return the nr of preallocated irqs */
555 	initcnt = arch_probe_nr_irqs();
556 	printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n",
557 	       NR_IRQS, nr_irqs, initcnt);
558 
559 	if (WARN_ON(nr_irqs > MAX_SPARSE_IRQS))
560 		nr_irqs = MAX_SPARSE_IRQS;
561 
562 	if (WARN_ON(initcnt > MAX_SPARSE_IRQS))
563 		initcnt = MAX_SPARSE_IRQS;
564 
565 	if (initcnt > nr_irqs)
566 		nr_irqs = initcnt;
567 
568 	for (i = 0; i < initcnt; i++) {
569 		desc = alloc_desc(i, node, 0, NULL, NULL);
570 		irq_insert_desc(i, desc);
571 	}
572 	return arch_early_irq_init();
573 }
574 
575 #else /* !CONFIG_SPARSE_IRQ */
576 
577 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
578 	[0 ... NR_IRQS-1] = {
579 		.handle_irq	= handle_bad_irq,
580 		.depth		= 1,
581 		.lock		= __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
582 	}
583 };
584 
early_irq_init(void)585 int __init early_irq_init(void)
586 {
587 	int count, i, node = first_online_node;
588 	struct irq_desc *desc;
589 
590 	init_irq_default_affinity();
591 
592 	printk(KERN_INFO "NR_IRQS: %d\n", NR_IRQS);
593 
594 	desc = irq_desc;
595 	count = ARRAY_SIZE(irq_desc);
596 
597 	for (i = 0; i < count; i++) {
598 		desc[i].kstat_irqs = alloc_percpu(unsigned int);
599 		alloc_masks(&desc[i], node);
600 		raw_spin_lock_init(&desc[i].lock);
601 		lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
602 		mutex_init(&desc[i].request_mutex);
603 		init_waitqueue_head(&desc[i].wait_for_threads);
604 		desc_set_defaults(i, &desc[i], node, NULL, NULL);
605 		irq_resend_init(&desc[i]);
606 	}
607 	return arch_early_irq_init();
608 }
609 
irq_to_desc(unsigned int irq)610 struct irq_desc *irq_to_desc(unsigned int irq)
611 {
612 	return (irq < NR_IRQS) ? irq_desc + irq : NULL;
613 }
614 EXPORT_SYMBOL(irq_to_desc);
615 
free_desc(unsigned int irq)616 static void free_desc(unsigned int irq)
617 {
618 	struct irq_desc *desc = irq_to_desc(irq);
619 	unsigned long flags;
620 
621 	raw_spin_lock_irqsave(&desc->lock, flags);
622 	desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
623 	raw_spin_unlock_irqrestore(&desc->lock, flags);
624 	delete_irq_desc(irq);
625 }
626 
alloc_descs(unsigned int start,unsigned int cnt,int node,const struct irq_affinity_desc * affinity,struct module * owner)627 static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
628 			      const struct irq_affinity_desc *affinity,
629 			      struct module *owner)
630 {
631 	u32 i;
632 
633 	for (i = 0; i < cnt; i++) {
634 		struct irq_desc *desc = irq_to_desc(start + i);
635 
636 		desc->owner = owner;
637 		irq_insert_desc(start + i, desc);
638 	}
639 	return start;
640 }
641 
irq_expand_nr_irqs(unsigned int nr)642 static int irq_expand_nr_irqs(unsigned int nr)
643 {
644 	return -ENOMEM;
645 }
646 
irq_mark_irq(unsigned int irq)647 void irq_mark_irq(unsigned int irq)
648 {
649 	mutex_lock(&sparse_irq_lock);
650 	irq_insert_desc(irq, irq_desc + irq);
651 	mutex_unlock(&sparse_irq_lock);
652 }
653 
654 #ifdef CONFIG_GENERIC_IRQ_LEGACY
irq_init_desc(unsigned int irq)655 void irq_init_desc(unsigned int irq)
656 {
657 	free_desc(irq);
658 }
659 #endif
660 
661 #endif /* !CONFIG_SPARSE_IRQ */
662 
handle_irq_desc(struct irq_desc * desc)663 int handle_irq_desc(struct irq_desc *desc)
664 {
665 	struct irq_data *data;
666 
667 	if (!desc)
668 		return -EINVAL;
669 
670 	data = irq_desc_get_irq_data(desc);
671 	if (WARN_ON_ONCE(!in_hardirq() && handle_enforce_irqctx(data)))
672 		return -EPERM;
673 
674 	generic_handle_irq_desc(desc);
675 	return 0;
676 }
677 
678 /**
679  * generic_handle_irq - Invoke the handler for a particular irq
680  * @irq:	The irq number to handle
681  *
682  * Returns:	0 on success, or -EINVAL if conversion has failed
683  *
684  * 		This function must be called from an IRQ context with irq regs
685  * 		initialized.
686   */
generic_handle_irq(unsigned int irq)687 int generic_handle_irq(unsigned int irq)
688 {
689 	return handle_irq_desc(irq_to_desc(irq));
690 }
691 EXPORT_SYMBOL_GPL(generic_handle_irq);
692 
693 /**
694  * generic_handle_irq_safe - Invoke the handler for a particular irq from any
695  *			     context.
696  * @irq:	The irq number to handle
697  *
698  * Returns:	0 on success, a negative value on error.
699  *
700  * This function can be called from any context (IRQ or process context). It
701  * will report an error if not invoked from IRQ context and the irq has been
702  * marked to enforce IRQ-context only.
703  */
generic_handle_irq_safe(unsigned int irq)704 int generic_handle_irq_safe(unsigned int irq)
705 {
706 	unsigned long flags;
707 	int ret;
708 
709 	local_irq_save(flags);
710 	ret = handle_irq_desc(irq_to_desc(irq));
711 	local_irq_restore(flags);
712 	return ret;
713 }
714 EXPORT_SYMBOL_GPL(generic_handle_irq_safe);
715 
716 #ifdef CONFIG_IRQ_DOMAIN
717 /**
718  * generic_handle_domain_irq - Invoke the handler for a HW irq belonging
719  *                             to a domain.
720  * @domain:	The domain where to perform the lookup
721  * @hwirq:	The HW irq number to convert to a logical one
722  *
723  * Returns:	0 on success, or -EINVAL if conversion has failed
724  *
725  * 		This function must be called from an IRQ context with irq regs
726  * 		initialized.
727  */
generic_handle_domain_irq(struct irq_domain * domain,unsigned int hwirq)728 int generic_handle_domain_irq(struct irq_domain *domain, unsigned int hwirq)
729 {
730 	return handle_irq_desc(irq_resolve_mapping(domain, hwirq));
731 }
732 EXPORT_SYMBOL_GPL(generic_handle_domain_irq);
733 
734  /**
735  * generic_handle_irq_safe - Invoke the handler for a HW irq belonging
736  *			     to a domain from any context.
737  * @domain:	The domain where to perform the lookup
738  * @hwirq:	The HW irq number to convert to a logical one
739  *
740  * Returns:	0 on success, a negative value on error.
741  *
742  * This function can be called from any context (IRQ or process
743  * context). If the interrupt is marked as 'enforce IRQ-context only' then
744  * the function must be invoked from hard interrupt context.
745  */
generic_handle_domain_irq_safe(struct irq_domain * domain,unsigned int hwirq)746 int generic_handle_domain_irq_safe(struct irq_domain *domain, unsigned int hwirq)
747 {
748 	unsigned long flags;
749 	int ret;
750 
751 	local_irq_save(flags);
752 	ret = handle_irq_desc(irq_resolve_mapping(domain, hwirq));
753 	local_irq_restore(flags);
754 	return ret;
755 }
756 EXPORT_SYMBOL_GPL(generic_handle_domain_irq_safe);
757 
758 /**
759  * generic_handle_domain_nmi - Invoke the handler for a HW nmi belonging
760  *                             to a domain.
761  * @domain:	The domain where to perform the lookup
762  * @hwirq:	The HW irq number to convert to a logical one
763  *
764  * Returns:	0 on success, or -EINVAL if conversion has failed
765  *
766  * 		This function must be called from an NMI context with irq regs
767  * 		initialized.
768  **/
generic_handle_domain_nmi(struct irq_domain * domain,unsigned int hwirq)769 int generic_handle_domain_nmi(struct irq_domain *domain, unsigned int hwirq)
770 {
771 	WARN_ON_ONCE(!in_nmi());
772 	return handle_irq_desc(irq_resolve_mapping(domain, hwirq));
773 }
774 #endif
775 
776 /* Dynamic interrupt handling */
777 
778 /**
779  * irq_free_descs - free irq descriptors
780  * @from:	Start of descriptor range
781  * @cnt:	Number of consecutive irqs to free
782  */
irq_free_descs(unsigned int from,unsigned int cnt)783 void irq_free_descs(unsigned int from, unsigned int cnt)
784 {
785 	int i;
786 
787 	if (from >= nr_irqs || (from + cnt) > nr_irqs)
788 		return;
789 
790 	mutex_lock(&sparse_irq_lock);
791 	for (i = 0; i < cnt; i++)
792 		free_desc(from + i);
793 
794 	mutex_unlock(&sparse_irq_lock);
795 }
796 EXPORT_SYMBOL_GPL(irq_free_descs);
797 
798 /**
799  * __irq_alloc_descs - allocate and initialize a range of irq descriptors
800  * @irq:	Allocate for specific irq number if irq >= 0
801  * @from:	Start the search from this irq number
802  * @cnt:	Number of consecutive irqs to allocate.
803  * @node:	Preferred node on which the irq descriptor should be allocated
804  * @owner:	Owning module (can be NULL)
805  * @affinity:	Optional pointer to an affinity mask array of size @cnt which
806  *		hints where the irq descriptors should be allocated and which
807  *		default affinities to use
808  *
809  * Returns the first irq number or error code
810  */
811 int __ref
__irq_alloc_descs(int irq,unsigned int from,unsigned int cnt,int node,struct module * owner,const struct irq_affinity_desc * affinity)812 __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
813 		  struct module *owner, const struct irq_affinity_desc *affinity)
814 {
815 	int start, ret;
816 
817 	if (!cnt)
818 		return -EINVAL;
819 
820 	if (irq >= 0) {
821 		if (from > irq)
822 			return -EINVAL;
823 		from = irq;
824 	} else {
825 		/*
826 		 * For interrupts which are freely allocated the
827 		 * architecture can force a lower bound to the @from
828 		 * argument. x86 uses this to exclude the GSI space.
829 		 */
830 		from = arch_dynirq_lower_bound(from);
831 	}
832 
833 	mutex_lock(&sparse_irq_lock);
834 
835 	start = irq_find_free_area(from, cnt);
836 	ret = -EEXIST;
837 	if (irq >=0 && start != irq)
838 		goto unlock;
839 
840 	if (start + cnt > nr_irqs) {
841 		ret = irq_expand_nr_irqs(start + cnt);
842 		if (ret)
843 			goto unlock;
844 	}
845 	ret = alloc_descs(start, cnt, node, affinity, owner);
846 unlock:
847 	mutex_unlock(&sparse_irq_lock);
848 	return ret;
849 }
850 EXPORT_SYMBOL_GPL(__irq_alloc_descs);
851 
852 /**
853  * irq_get_next_irq - get next allocated irq number
854  * @offset:	where to start the search
855  *
856  * Returns next irq number after offset or nr_irqs if none is found.
857  */
irq_get_next_irq(unsigned int offset)858 unsigned int irq_get_next_irq(unsigned int offset)
859 {
860 	return irq_find_at_or_after(offset);
861 }
862 
863 struct irq_desc *
__irq_get_desc_lock(unsigned int irq,unsigned long * flags,bool bus,unsigned int check)864 __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
865 		    unsigned int check)
866 {
867 	struct irq_desc *desc = irq_to_desc(irq);
868 
869 	if (desc) {
870 		if (check & _IRQ_DESC_CHECK) {
871 			if ((check & _IRQ_DESC_PERCPU) &&
872 			    !irq_settings_is_per_cpu_devid(desc))
873 				return NULL;
874 
875 			if (!(check & _IRQ_DESC_PERCPU) &&
876 			    irq_settings_is_per_cpu_devid(desc))
877 				return NULL;
878 		}
879 
880 		if (bus)
881 			chip_bus_lock(desc);
882 		raw_spin_lock_irqsave(&desc->lock, *flags);
883 	}
884 	return desc;
885 }
886 
__irq_put_desc_unlock(struct irq_desc * desc,unsigned long flags,bool bus)887 void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
888 	__releases(&desc->lock)
889 {
890 	raw_spin_unlock_irqrestore(&desc->lock, flags);
891 	if (bus)
892 		chip_bus_sync_unlock(desc);
893 }
894 
irq_set_percpu_devid_partition(unsigned int irq,const struct cpumask * affinity)895 int irq_set_percpu_devid_partition(unsigned int irq,
896 				   const struct cpumask *affinity)
897 {
898 	struct irq_desc *desc = irq_to_desc(irq);
899 
900 	if (!desc)
901 		return -EINVAL;
902 
903 	if (desc->percpu_enabled)
904 		return -EINVAL;
905 
906 	desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
907 
908 	if (!desc->percpu_enabled)
909 		return -ENOMEM;
910 
911 	if (affinity)
912 		desc->percpu_affinity = affinity;
913 	else
914 		desc->percpu_affinity = cpu_possible_mask;
915 
916 	irq_set_percpu_devid_flags(irq);
917 	return 0;
918 }
919 
irq_set_percpu_devid(unsigned int irq)920 int irq_set_percpu_devid(unsigned int irq)
921 {
922 	return irq_set_percpu_devid_partition(irq, NULL);
923 }
924 
irq_get_percpu_devid_partition(unsigned int irq,struct cpumask * affinity)925 int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
926 {
927 	struct irq_desc *desc = irq_to_desc(irq);
928 
929 	if (!desc || !desc->percpu_enabled)
930 		return -EINVAL;
931 
932 	if (affinity)
933 		cpumask_copy(affinity, desc->percpu_affinity);
934 
935 	return 0;
936 }
937 EXPORT_SYMBOL_GPL(irq_get_percpu_devid_partition);
938 
kstat_incr_irq_this_cpu(unsigned int irq)939 void kstat_incr_irq_this_cpu(unsigned int irq)
940 {
941 	kstat_incr_irqs_this_cpu(irq_to_desc(irq));
942 }
943 
944 /**
945  * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
946  * @irq:	The interrupt number
947  * @cpu:	The cpu number
948  *
949  * Returns the sum of interrupt counts on @cpu since boot for
950  * @irq. The caller must ensure that the interrupt is not removed
951  * concurrently.
952  */
kstat_irqs_cpu(unsigned int irq,int cpu)953 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
954 {
955 	struct irq_desc *desc = irq_to_desc(irq);
956 
957 	return desc && desc->kstat_irqs ?
958 			*per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
959 }
960 EXPORT_SYMBOL_GPL(kstat_irqs_cpu);
961 
irq_is_nmi(struct irq_desc * desc)962 static bool irq_is_nmi(struct irq_desc *desc)
963 {
964 	return desc->istate & IRQS_NMI;
965 }
966 
kstat_irqs(unsigned int irq)967 static unsigned int kstat_irqs(unsigned int irq)
968 {
969 	struct irq_desc *desc = irq_to_desc(irq);
970 	unsigned int sum = 0;
971 	int cpu;
972 
973 	if (!desc || !desc->kstat_irqs)
974 		return 0;
975 	if (!irq_settings_is_per_cpu_devid(desc) &&
976 	    !irq_settings_is_per_cpu(desc) &&
977 	    !irq_is_nmi(desc))
978 		return data_race(desc->tot_count);
979 
980 	for_each_possible_cpu(cpu)
981 		sum += data_race(*per_cpu_ptr(desc->kstat_irqs, cpu));
982 	return sum;
983 }
984 
985 /**
986  * kstat_irqs_usr - Get the statistics for an interrupt from thread context
987  * @irq:	The interrupt number
988  *
989  * Returns the sum of interrupt counts on all cpus since boot for @irq.
990  *
991  * It uses rcu to protect the access since a concurrent removal of an
992  * interrupt descriptor is observing an rcu grace period before
993  * delayed_free_desc()/irq_kobj_release().
994  */
kstat_irqs_usr(unsigned int irq)995 unsigned int kstat_irqs_usr(unsigned int irq)
996 {
997 	unsigned int sum;
998 
999 	rcu_read_lock();
1000 	sum = kstat_irqs(irq);
1001 	rcu_read_unlock();
1002 	return sum;
1003 }
1004 
1005 #ifdef CONFIG_LOCKDEP
__irq_set_lockdep_class(unsigned int irq,struct lock_class_key * lock_class,struct lock_class_key * request_class)1006 void __irq_set_lockdep_class(unsigned int irq, struct lock_class_key *lock_class,
1007 			     struct lock_class_key *request_class)
1008 {
1009 	struct irq_desc *desc = irq_to_desc(irq);
1010 
1011 	if (desc) {
1012 		lockdep_set_class(&desc->lock, lock_class);
1013 		lockdep_set_class(&desc->request_mutex, request_class);
1014 	}
1015 }
1016 EXPORT_SYMBOL_GPL(__irq_set_lockdep_class);
1017 #endif
1018 EXPORT_SYMBOL_GPL(kstat_irqs_usr);
1019