xref: /openbmc/linux/kernel/irq/irq_sim.c (revision ddc141e5)
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);
165