xref: /openbmc/linux/kernel/irq/irq_sim.c (revision 9fb29c73)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017-2018 Bartosz Golaszewski <brgl@bgdev.pl>
4  */
5 
6 #include <linux/slab.h>
7 #include <linux/irq_sim.h>
8 #include <linux/irq.h>
9 
10 struct irq_sim_devres {
11 	struct irq_sim		*sim;
12 };
13 
14 static void irq_sim_irqmask(struct irq_data *data)
15 {
16 	struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
17 
18 	irq_ctx->enabled = false;
19 }
20 
21 static void irq_sim_irqunmask(struct irq_data *data)
22 {
23 	struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
24 
25 	irq_ctx->enabled = true;
26 }
27 
28 static struct irq_chip irq_sim_irqchip = {
29 	.name		= "irq_sim",
30 	.irq_mask	= irq_sim_irqmask,
31 	.irq_unmask	= irq_sim_irqunmask,
32 };
33 
34 static void irq_sim_handle_irq(struct irq_work *work)
35 {
36 	struct irq_sim_work_ctx *work_ctx;
37 	unsigned int offset = 0;
38 	struct irq_sim *sim;
39 	int irqnum;
40 
41 	work_ctx = container_of(work, struct irq_sim_work_ctx, work);
42 	sim = container_of(work_ctx, struct irq_sim, work_ctx);
43 
44 	while (!bitmap_empty(work_ctx->pending, sim->irq_count)) {
45 		offset = find_next_bit(work_ctx->pending,
46 				       sim->irq_count, offset);
47 		clear_bit(offset, work_ctx->pending);
48 		irqnum = irq_sim_irqnum(sim, offset);
49 		handle_simple_irq(irq_to_desc(irqnum));
50 	}
51 }
52 
53 /**
54  * irq_sim_init - Initialize the interrupt simulator: allocate a range of
55  *                dummy interrupts.
56  *
57  * @sim:        The interrupt simulator object to initialize.
58  * @num_irqs:   Number of interrupts to allocate
59  *
60  * On success: return the base of the allocated interrupt range.
61  * On failure: a negative errno.
62  */
63 int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs)
64 {
65 	int i;
66 
67 	sim->irqs = kmalloc_array(num_irqs, sizeof(*sim->irqs), GFP_KERNEL);
68 	if (!sim->irqs)
69 		return -ENOMEM;
70 
71 	sim->irq_base = irq_alloc_descs(-1, 0, num_irqs, 0);
72 	if (sim->irq_base < 0) {
73 		kfree(sim->irqs);
74 		return sim->irq_base;
75 	}
76 
77 	sim->work_ctx.pending = bitmap_zalloc(num_irqs, GFP_KERNEL);
78 	if (!sim->work_ctx.pending) {
79 		kfree(sim->irqs);
80 		irq_free_descs(sim->irq_base, num_irqs);
81 		return -ENOMEM;
82 	}
83 
84 	for (i = 0; i < num_irqs; i++) {
85 		sim->irqs[i].irqnum = sim->irq_base + i;
86 		sim->irqs[i].enabled = false;
87 		irq_set_chip(sim->irq_base + i, &irq_sim_irqchip);
88 		irq_set_chip_data(sim->irq_base + i, &sim->irqs[i]);
89 		irq_set_handler(sim->irq_base + i, &handle_simple_irq);
90 		irq_modify_status(sim->irq_base + i,
91 				  IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
92 	}
93 
94 	init_irq_work(&sim->work_ctx.work, irq_sim_handle_irq);
95 	sim->irq_count = num_irqs;
96 
97 	return sim->irq_base;
98 }
99 EXPORT_SYMBOL_GPL(irq_sim_init);
100 
101 /**
102  * irq_sim_fini - Deinitialize the interrupt simulator: free the interrupt
103  *                descriptors and allocated memory.
104  *
105  * @sim:        The interrupt simulator to tear down.
106  */
107 void irq_sim_fini(struct irq_sim *sim)
108 {
109 	irq_work_sync(&sim->work_ctx.work);
110 	bitmap_free(sim->work_ctx.pending);
111 	irq_free_descs(sim->irq_base, sim->irq_count);
112 	kfree(sim->irqs);
113 }
114 EXPORT_SYMBOL_GPL(irq_sim_fini);
115 
116 static void devm_irq_sim_release(struct device *dev, void *res)
117 {
118 	struct irq_sim_devres *this = res;
119 
120 	irq_sim_fini(this->sim);
121 }
122 
123 /**
124  * irq_sim_init - Initialize the interrupt simulator for a managed device.
125  *
126  * @dev:        Device to initialize the simulator object for.
127  * @sim:        The interrupt simulator object to initialize.
128  * @num_irqs:   Number of interrupts to allocate
129  *
130  * On success: return the base of the allocated interrupt range.
131  * On failure: a negative errno.
132  */
133 int devm_irq_sim_init(struct device *dev, struct irq_sim *sim,
134 		      unsigned int num_irqs)
135 {
136 	struct irq_sim_devres *dr;
137 	int rv;
138 
139 	dr = devres_alloc(devm_irq_sim_release, sizeof(*dr), GFP_KERNEL);
140 	if (!dr)
141 		return -ENOMEM;
142 
143 	rv = irq_sim_init(sim, num_irqs);
144 	if (rv < 0) {
145 		devres_free(dr);
146 		return rv;
147 	}
148 
149 	dr->sim = sim;
150 	devres_add(dev, dr);
151 
152 	return rv;
153 }
154 EXPORT_SYMBOL_GPL(devm_irq_sim_init);
155 
156 /**
157  * irq_sim_fire - Enqueue an interrupt.
158  *
159  * @sim:        The interrupt simulator object.
160  * @offset:     Offset of the simulated interrupt which should be fired.
161  */
162 void irq_sim_fire(struct irq_sim *sim, unsigned int offset)
163 {
164 	if (sim->irqs[offset].enabled) {
165 		set_bit(offset, sim->work_ctx.pending);
166 		irq_work_queue(&sim->work_ctx.work);
167 	}
168 }
169 EXPORT_SYMBOL_GPL(irq_sim_fire);
170 
171 /**
172  * irq_sim_irqnum - Get the allocated number of a dummy interrupt.
173  *
174  * @sim:        The interrupt simulator object.
175  * @offset:     Offset of the simulated interrupt for which to retrieve
176  *              the number.
177  */
178 int irq_sim_irqnum(struct irq_sim *sim, unsigned int offset)
179 {
180 	return sim->irqs[offset].irqnum;
181 }
182 EXPORT_SYMBOL_GPL(irq_sim_irqnum);
183