xref: /openbmc/linux/kernel/irq/irq_sim.c (revision 8bd1369b)
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 
38 	work_ctx = container_of(work, struct irq_sim_work_ctx, work);
39 	handle_simple_irq(irq_to_desc(work_ctx->irq));
40 }
41 
42 /**
43  * irq_sim_init - Initialize the interrupt simulator: allocate a range of
44  *                dummy interrupts.
45  *
46  * @sim:        The interrupt simulator object to initialize.
47  * @num_irqs:   Number of interrupts to allocate
48  *
49  * On success: return the base of the allocated interrupt range.
50  * On failure: a negative errno.
51  */
52 int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs)
53 {
54 	int i;
55 
56 	sim->irqs = kmalloc_array(num_irqs, sizeof(*sim->irqs), GFP_KERNEL);
57 	if (!sim->irqs)
58 		return -ENOMEM;
59 
60 	sim->irq_base = irq_alloc_descs(-1, 0, num_irqs, 0);
61 	if (sim->irq_base < 0) {
62 		kfree(sim->irqs);
63 		return sim->irq_base;
64 	}
65 
66 	for (i = 0; i < num_irqs; i++) {
67 		sim->irqs[i].irqnum = sim->irq_base + i;
68 		sim->irqs[i].enabled = false;
69 		irq_set_chip(sim->irq_base + i, &irq_sim_irqchip);
70 		irq_set_chip_data(sim->irq_base + i, &sim->irqs[i]);
71 		irq_set_handler(sim->irq_base + i, &handle_simple_irq);
72 		irq_modify_status(sim->irq_base + i,
73 				  IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
74 	}
75 
76 	init_irq_work(&sim->work_ctx.work, irq_sim_handle_irq);
77 	sim->irq_count = num_irqs;
78 
79 	return sim->irq_base;
80 }
81 EXPORT_SYMBOL_GPL(irq_sim_init);
82 
83 /**
84  * irq_sim_fini - Deinitialize the interrupt simulator: free the interrupt
85  *                descriptors and allocated memory.
86  *
87  * @sim:        The interrupt simulator to tear down.
88  */
89 void irq_sim_fini(struct irq_sim *sim)
90 {
91 	irq_work_sync(&sim->work_ctx.work);
92 	irq_free_descs(sim->irq_base, sim->irq_count);
93 	kfree(sim->irqs);
94 }
95 EXPORT_SYMBOL_GPL(irq_sim_fini);
96 
97 static void devm_irq_sim_release(struct device *dev, void *res)
98 {
99 	struct irq_sim_devres *this = res;
100 
101 	irq_sim_fini(this->sim);
102 }
103 
104 /**
105  * irq_sim_init - Initialize the interrupt simulator for a managed device.
106  *
107  * @dev:        Device to initialize the simulator object for.
108  * @sim:        The interrupt simulator object to initialize.
109  * @num_irqs:   Number of interrupts to allocate
110  *
111  * On success: return the base of the allocated interrupt range.
112  * On failure: a negative errno.
113  */
114 int devm_irq_sim_init(struct device *dev, struct irq_sim *sim,
115 		      unsigned int num_irqs)
116 {
117 	struct irq_sim_devres *dr;
118 	int rv;
119 
120 	dr = devres_alloc(devm_irq_sim_release, sizeof(*dr), GFP_KERNEL);
121 	if (!dr)
122 		return -ENOMEM;
123 
124 	rv = irq_sim_init(sim, num_irqs);
125 	if (rv < 0) {
126 		devres_free(dr);
127 		return rv;
128 	}
129 
130 	dr->sim = sim;
131 	devres_add(dev, dr);
132 
133 	return rv;
134 }
135 EXPORT_SYMBOL_GPL(devm_irq_sim_init);
136 
137 /**
138  * irq_sim_fire - Enqueue an interrupt.
139  *
140  * @sim:        The interrupt simulator object.
141  * @offset:     Offset of the simulated interrupt which should be fired.
142  */
143 void irq_sim_fire(struct irq_sim *sim, unsigned int offset)
144 {
145 	if (sim->irqs[offset].enabled) {
146 		sim->work_ctx.irq = irq_sim_irqnum(sim, offset);
147 		irq_work_queue(&sim->work_ctx.work);
148 	}
149 }
150 EXPORT_SYMBOL_GPL(irq_sim_fire);
151 
152 /**
153  * irq_sim_irqnum - Get the allocated number of a dummy interrupt.
154  *
155  * @sim:        The interrupt simulator object.
156  * @offset:     Offset of the simulated interrupt for which to retrieve
157  *              the number.
158  */
159 int irq_sim_irqnum(struct irq_sim *sim, unsigned int offset)
160 {
161 	return sim->irqs[offset].irqnum;
162 }
163 EXPORT_SYMBOL_GPL(irq_sim_irqnum);
164