diff options
author | Thomas Gleixner <tglx@linutronix.de> | 2017-08-16 10:41:28 -0400 |
---|---|---|
committer | Thomas Gleixner <tglx@linutronix.de> | 2017-08-16 10:41:28 -0400 |
commit | 7cb2fad97ee057c7b0cb5913074eee8071490a71 (patch) | |
tree | 119c8b8da45bdbef6b714c5336565f5dc9e917a2 | |
parent | 163616cf2f6ab7a8e37452ec00320039ab65bd45 (diff) | |
parent | 44e72c7ebf294043cfe276f7328b8c0e6a3e50e9 (diff) |
Merge branch 'irq/for-gpio' into irq/core
Merge the irq simulator which is in a separate branch so it can be consumed
by the gpio folks.
-rw-r--r-- | Documentation/driver-model/devres.txt | 1 | ||||
-rw-r--r-- | include/linux/irq_sim.h | 44 | ||||
-rw-r--r-- | kernel/irq/Kconfig | 5 | ||||
-rw-r--r-- | kernel/irq/Makefile | 1 | ||||
-rw-r--r-- | kernel/irq/irq_sim.c | 164 |
5 files changed, 215 insertions, 0 deletions
diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt index 30e04f7a690d..69f08c0f23a8 100644 --- a/Documentation/driver-model/devres.txt +++ b/Documentation/driver-model/devres.txt | |||
@@ -312,6 +312,7 @@ IRQ | |||
312 | devm_irq_alloc_descs_from() | 312 | devm_irq_alloc_descs_from() |
313 | devm_irq_alloc_generic_chip() | 313 | devm_irq_alloc_generic_chip() |
314 | devm_irq_setup_generic_chip() | 314 | devm_irq_setup_generic_chip() |
315 | devm_irq_sim_init() | ||
315 | 316 | ||
316 | LED | 317 | LED |
317 | devm_led_classdev_register() | 318 | devm_led_classdev_register() |
diff --git a/include/linux/irq_sim.h b/include/linux/irq_sim.h new file mode 100644 index 000000000000..0380d899b955 --- /dev/null +++ b/include/linux/irq_sim.h | |||
@@ -0,0 +1,44 @@ | |||
1 | #ifndef _LINUX_IRQ_SIM_H | ||
2 | #define _LINUX_IRQ_SIM_H | ||
3 | /* | ||
4 | * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify it | ||
7 | * under the terms of the GNU General Public License as published by the | ||
8 | * Free Software Foundation; either version 2 of the License, or (at your | ||
9 | * option) any later version. | ||
10 | */ | ||
11 | |||
12 | #include <linux/irq_work.h> | ||
13 | #include <linux/device.h> | ||
14 | |||
15 | /* | ||
16 | * Provides a framework for allocating simulated interrupts which can be | ||
17 | * requested like normal irqs and enqueued from process context. | ||
18 | */ | ||
19 | |||
20 | struct irq_sim_work_ctx { | ||
21 | struct irq_work work; | ||
22 | int irq; | ||
23 | }; | ||
24 | |||
25 | struct irq_sim_irq_ctx { | ||
26 | int irqnum; | ||
27 | bool enabled; | ||
28 | }; | ||
29 | |||
30 | struct irq_sim { | ||
31 | struct irq_sim_work_ctx work_ctx; | ||
32 | int irq_base; | ||
33 | unsigned int irq_count; | ||
34 | struct irq_sim_irq_ctx *irqs; | ||
35 | }; | ||
36 | |||
37 | int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs); | ||
38 | int devm_irq_sim_init(struct device *dev, struct irq_sim *sim, | ||
39 | unsigned int num_irqs); | ||
40 | void irq_sim_fini(struct irq_sim *sim); | ||
41 | void irq_sim_fire(struct irq_sim *sim, unsigned int offset); | ||
42 | int irq_sim_irqnum(struct irq_sim *sim, unsigned int offset); | ||
43 | |||
44 | #endif /* _LINUX_IRQ_SIM_H */ | ||
diff --git a/kernel/irq/Kconfig b/kernel/irq/Kconfig index 27c4e774071c..1d06af787932 100644 --- a/kernel/irq/Kconfig +++ b/kernel/irq/Kconfig | |||
@@ -63,6 +63,11 @@ config GENERIC_IRQ_CHIP | |||
63 | config IRQ_DOMAIN | 63 | config IRQ_DOMAIN |
64 | bool | 64 | bool |
65 | 65 | ||
66 | # Support for simulated interrupts | ||
67 | config IRQ_SIM | ||
68 | bool | ||
69 | select IRQ_WORK | ||
70 | |||
66 | # Support for hierarchical irq domains | 71 | # Support for hierarchical irq domains |
67 | config IRQ_DOMAIN_HIERARCHY | 72 | config IRQ_DOMAIN_HIERARCHY |
68 | bool | 73 | bool |
diff --git a/kernel/irq/Makefile b/kernel/irq/Makefile index e4aef7351f2b..1970cafe8f2a 100644 --- a/kernel/irq/Makefile +++ b/kernel/irq/Makefile | |||
@@ -4,6 +4,7 @@ obj-$(CONFIG_IRQ_TIMINGS) += timings.o | |||
4 | obj-$(CONFIG_GENERIC_IRQ_CHIP) += generic-chip.o | 4 | obj-$(CONFIG_GENERIC_IRQ_CHIP) += generic-chip.o |
5 | obj-$(CONFIG_GENERIC_IRQ_PROBE) += autoprobe.o | 5 | obj-$(CONFIG_GENERIC_IRQ_PROBE) += autoprobe.o |
6 | obj-$(CONFIG_IRQ_DOMAIN) += irqdomain.o | 6 | obj-$(CONFIG_IRQ_DOMAIN) += irqdomain.o |
7 | obj-$(CONFIG_IRQ_SIM) += irq_sim.o | ||
7 | obj-$(CONFIG_PROC_FS) += proc.o | 8 | obj-$(CONFIG_PROC_FS) += proc.o |
8 | obj-$(CONFIG_GENERIC_PENDING_IRQ) += migration.o | 9 | obj-$(CONFIG_GENERIC_PENDING_IRQ) += migration.o |
9 | obj-$(CONFIG_GENERIC_IRQ_MIGRATION) += cpuhotplug.o | 10 | obj-$(CONFIG_GENERIC_IRQ_MIGRATION) += cpuhotplug.o |
diff --git a/kernel/irq/irq_sim.c b/kernel/irq/irq_sim.c new file mode 100644 index 000000000000..24caabf1a0f7 --- /dev/null +++ b/kernel/irq/irq_sim.c | |||
@@ -0,0 +1,164 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify it | ||
5 | * under the terms of the GNU General Public License as published by the | ||
6 | * Free Software Foundation; either version 2 of the License, or (at your | ||
7 | * option) any later version. | ||
8 | */ | ||
9 | |||
10 | #include <linux/irq_sim.h> | ||
11 | #include <linux/irq.h> | ||
12 | |||
13 | struct irq_sim_devres { | ||
14 | struct irq_sim *sim; | ||
15 | }; | ||
16 | |||
17 | static void irq_sim_irqmask(struct irq_data *data) | ||
18 | { | ||
19 | struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data); | ||
20 | |||
21 | irq_ctx->enabled = false; | ||
22 | } | ||
23 | |||
24 | static void irq_sim_irqunmask(struct irq_data *data) | ||
25 | { | ||
26 | struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data); | ||
27 | |||
28 | irq_ctx->enabled = true; | ||
29 | } | ||
30 | |||
31 | static struct irq_chip irq_sim_irqchip = { | ||
32 | .name = "irq_sim", | ||
33 | .irq_mask = irq_sim_irqmask, | ||
34 | .irq_unmask = irq_sim_irqunmask, | ||
35 | }; | ||
36 | |||
37 | static void irq_sim_handle_irq(struct irq_work *work) | ||
38 | { | ||
39 | struct irq_sim_work_ctx *work_ctx; | ||
40 | |||
41 | work_ctx = container_of(work, struct irq_sim_work_ctx, work); | ||
42 | handle_simple_irq(irq_to_desc(work_ctx->irq)); | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * irq_sim_init - Initialize the interrupt simulator: allocate a range of | ||
47 | * dummy interrupts. | ||
48 | * | ||
49 | * @sim: The interrupt simulator object to initialize. | ||
50 | * @num_irqs: Number of interrupts to allocate | ||
51 | * | ||
52 | * Returns 0 on success and a negative error number on failure. | ||
53 | */ | ||
54 | int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs) | ||
55 | { | ||
56 | int i; | ||
57 | |||
58 | sim->irqs = kmalloc_array(num_irqs, sizeof(*sim->irqs), GFP_KERNEL); | ||
59 | if (!sim->irqs) | ||
60 | return -ENOMEM; | ||
61 | |||
62 | sim->irq_base = irq_alloc_descs(-1, 0, num_irqs, 0); | ||
63 | if (sim->irq_base < 0) { | ||
64 | kfree(sim->irqs); | ||
65 | return sim->irq_base; | ||
66 | } | ||
67 | |||
68 | for (i = 0; i < num_irqs; i++) { | ||
69 | sim->irqs[i].irqnum = sim->irq_base + i; | ||
70 | sim->irqs[i].enabled = false; | ||
71 | irq_set_chip(sim->irq_base + i, &irq_sim_irqchip); | ||
72 | irq_set_chip_data(sim->irq_base + i, &sim->irqs[i]); | ||
73 | irq_set_handler(sim->irq_base + i, &handle_simple_irq); | ||
74 | irq_modify_status(sim->irq_base + i, | ||
75 | IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE); | ||
76 | } | ||
77 | |||
78 | init_irq_work(&sim->work_ctx.work, irq_sim_handle_irq); | ||
79 | sim->irq_count = num_irqs; | ||
80 | |||
81 | return 0; | ||
82 | } | ||
83 | EXPORT_SYMBOL_GPL(irq_sim_init); | ||
84 | |||
85 | /** | ||
86 | * irq_sim_fini - Deinitialize the interrupt simulator: free the interrupt | ||
87 | * descriptors and allocated memory. | ||
88 | * | ||
89 | * @sim: The interrupt simulator to tear down. | ||
90 | */ | ||
91 | void irq_sim_fini(struct irq_sim *sim) | ||
92 | { | ||
93 | irq_work_sync(&sim->work_ctx.work); | ||
94 | irq_free_descs(sim->irq_base, sim->irq_count); | ||
95 | kfree(sim->irqs); | ||
96 | } | ||
97 | EXPORT_SYMBOL_GPL(irq_sim_fini); | ||
98 | |||
99 | static void devm_irq_sim_release(struct device *dev, void *res) | ||
100 | { | ||
101 | struct irq_sim_devres *this = res; | ||
102 | |||
103 | irq_sim_fini(this->sim); | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * irq_sim_init - Initialize the interrupt simulator for a managed device. | ||
108 | * | ||
109 | * @dev: Device to initialize the simulator object for. | ||
110 | * @sim: The interrupt simulator object to initialize. | ||
111 | * @num_irqs: Number of interrupts to allocate | ||
112 | * | ||
113 | * Returns 0 on success and a negative error number on failure. | ||
114 | */ | ||
115 | int devm_irq_sim_init(struct device *dev, struct irq_sim *sim, | ||
116 | unsigned int num_irqs) | ||
117 | { | ||
118 | struct irq_sim_devres *dr; | ||
119 | int rv; | ||
120 | |||
121 | dr = devres_alloc(devm_irq_sim_release, sizeof(*dr), GFP_KERNEL); | ||
122 | if (!dr) | ||
123 | return -ENOMEM; | ||
124 | |||
125 | rv = irq_sim_init(sim, num_irqs); | ||
126 | if (rv) { | ||
127 | devres_free(dr); | ||
128 | return rv; | ||
129 | } | ||
130 | |||
131 | dr->sim = sim; | ||
132 | devres_add(dev, dr); | ||
133 | |||
134 | return 0; | ||
135 | } | ||
136 | EXPORT_SYMBOL_GPL(devm_irq_sim_init); | ||
137 | |||
138 | /** | ||
139 | * irq_sim_fire - Enqueue an interrupt. | ||
140 | * | ||
141 | * @sim: The interrupt simulator object. | ||
142 | * @offset: Offset of the simulated interrupt which should be fired. | ||
143 | */ | ||
144 | void irq_sim_fire(struct irq_sim *sim, unsigned int offset) | ||
145 | { | ||
146 | if (sim->irqs[offset].enabled) { | ||
147 | sim->work_ctx.irq = irq_sim_irqnum(sim, offset); | ||
148 | irq_work_queue(&sim->work_ctx.work); | ||
149 | } | ||
150 | } | ||
151 | EXPORT_SYMBOL_GPL(irq_sim_fire); | ||
152 | |||
153 | /** | ||
154 | * irq_sim_irqnum - Get the allocated number of a dummy interrupt. | ||
155 | * | ||
156 | * @sim: The interrupt simulator object. | ||
157 | * @offset: Offset of the simulated interrupt for which to retrieve | ||
158 | * the number. | ||
159 | */ | ||
160 | int irq_sim_irqnum(struct irq_sim *sim, unsigned int offset) | ||
161 | { | ||
162 | return sim->irqs[offset].irqnum; | ||
163 | } | ||
164 | EXPORT_SYMBOL_GPL(irq_sim_irqnum); | ||