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