Skip to content

Commit 7cb2fad

Browse files
committed
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.
2 parents 163616c + 44e72c7 commit 7cb2fad

5 files changed

Lines changed: 215 additions & 0 deletions

File tree

‎Documentation/driver-model/devres.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ IRQ
312312
devm_irq_alloc_descs_from()
313313
devm_irq_alloc_generic_chip()
314314
devm_irq_setup_generic_chip()
315+
devm_irq_sim_init()
315316

316317
LED
317318
devm_led_classdev_register()

‎include/linux/irq_sim.h‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */

‎kernel/irq/Kconfig‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ config GENERIC_IRQ_CHIP
6363
config IRQ_DOMAIN
6464
bool
6565

66+
# Support for simulated interrupts
67+
config IRQ_SIM
68+
bool
69+
select IRQ_WORK
70+
6671
# Support for hierarchical irq domains
6772
config IRQ_DOMAIN_HIERARCHY
6873
bool

‎kernel/irq/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ obj-$(CONFIG_IRQ_TIMINGS) += timings.o
44
obj-$(CONFIG_GENERIC_IRQ_CHIP) += generic-chip.o
55
obj-$(CONFIG_GENERIC_IRQ_PROBE) += autoprobe.o
66
obj-$(CONFIG_IRQ_DOMAIN) += irqdomain.o
7+
obj-$(CONFIG_IRQ_SIM) += irq_sim.o
78
obj-$(CONFIG_PROC_FS) += proc.o
89
obj-$(CONFIG_GENERIC_PENDING_IRQ) += migration.o
910
obj-$(CONFIG_GENERIC_IRQ_MIGRATION) += cpuhotplug.o

‎kernel/irq/irq_sim.c‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

0 commit comments

Comments
 (0)