2 * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
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.
10 #include <linux/slab.h>
11 #include <linux/irq_sim.h>
12 #include <linux/irq.h>
14 struct irq_sim_devres {
18 static void irq_sim_irqmask(struct irq_data *data)
20 struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
22 irq_ctx->enabled = false;
25 static void irq_sim_irqunmask(struct irq_data *data)
27 struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
29 irq_ctx->enabled = true;
32 static struct irq_chip irq_sim_irqchip = {
34 .irq_mask = irq_sim_irqmask,
35 .irq_unmask = irq_sim_irqunmask,
38 static void irq_sim_handle_irq(struct irq_work *work)
40 struct irq_sim_work_ctx *work_ctx;
42 work_ctx = container_of(work, struct irq_sim_work_ctx, work);
43 handle_simple_irq(irq_to_desc(work_ctx->irq));
47 * irq_sim_init - Initialize the interrupt simulator: allocate a range of
50 * @sim: The interrupt simulator object to initialize.
51 * @num_irqs: Number of interrupts to allocate
53 * On success: return the base of the allocated interrupt range.
54 * On failure: a negative errno.
56 int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs)
60 sim->irqs = kmalloc_array(num_irqs, sizeof(*sim->irqs), GFP_KERNEL);
64 sim->irq_base = irq_alloc_descs(-1, 0, num_irqs, 0);
65 if (sim->irq_base < 0) {
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);
80 init_irq_work(&sim->work_ctx.work, irq_sim_handle_irq);
81 sim->irq_count = num_irqs;
85 EXPORT_SYMBOL_GPL(irq_sim_init);
88 * irq_sim_fini - Deinitialize the interrupt simulator: free the interrupt
89 * descriptors and allocated memory.
91 * @sim: The interrupt simulator to tear down.
93 void irq_sim_fini(struct irq_sim *sim)
95 irq_work_sync(&sim->work_ctx.work);
96 irq_free_descs(sim->irq_base, sim->irq_count);
99 EXPORT_SYMBOL_GPL(irq_sim_fini);
101 static void devm_irq_sim_release(struct device *dev, void *res)
103 struct irq_sim_devres *this = res;
105 irq_sim_fini(this->sim);
109 * irq_sim_init - Initialize the interrupt simulator for a managed device.
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
115 * On success: return the base of the allocated interrupt range.
116 * On failure: a negative errno.
118 int devm_irq_sim_init(struct device *dev, struct irq_sim *sim,
119 unsigned int num_irqs)
121 struct irq_sim_devres *dr;
124 dr = devres_alloc(devm_irq_sim_release, sizeof(*dr), GFP_KERNEL);
128 rv = irq_sim_init(sim, num_irqs);
139 EXPORT_SYMBOL_GPL(devm_irq_sim_init);
142 * irq_sim_fire - Enqueue an interrupt.
144 * @sim: The interrupt simulator object.
145 * @offset: Offset of the simulated interrupt which should be fired.
147 void irq_sim_fire(struct irq_sim *sim, unsigned int offset)
149 if (sim->irqs[offset].enabled) {
150 sim->work_ctx.irq = irq_sim_irqnum(sim, offset);
151 irq_work_queue(&sim->work_ctx.work);
154 EXPORT_SYMBOL_GPL(irq_sim_fire);
157 * irq_sim_irqnum - Get the allocated number of a dummy interrupt.
159 * @sim: The interrupt simulator object.
160 * @offset: Offset of the simulated interrupt for which to retrieve
163 int irq_sim_irqnum(struct irq_sim *sim, unsigned int offset)
165 return sim->irqs[offset].irqnum;
167 EXPORT_SYMBOL_GPL(irq_sim_irqnum);