forked from Minki/linux
b19af510e6
Implement a simple, irq_work-based framework for simulating interrupts. Currently the API exposes routines for initializing and deinitializing the simulator object, enqueueing the interrupts and retrieving the allocated interrupt numbers based on the offset of the dummy interrupt in the simulator struct. Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Cc: Lars-Peter Clausen <lars@metafoo.de> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Marc Zyngier <marc.zyngier@arm.com> Cc: Linus Walleij <linus.walleij@linaro.org> Cc: linux-doc@vger.kernel.org Cc: linux-gpio@vger.kernel.org Cc: Bamvor Jian Zhang <bamvor.zhangjian@linaro.org> Cc: Jonathan Cameron <jic23@kernel.org> Link: http://lkml.kernel.org/r/20170814145318.6495-2-brgl@bgdev.pl Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
122 lines
3.2 KiB
C
122 lines
3.2 KiB
C
/*
|
|
* Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
* option) any later version.
|
|
*/
|
|
|
|
#include <linux/irq_sim.h>
|
|
#include <linux/irq.h>
|
|
|
|
static void irq_sim_irqmask(struct irq_data *data)
|
|
{
|
|
struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
|
|
|
|
irq_ctx->enabled = false;
|
|
}
|
|
|
|
static void irq_sim_irqunmask(struct irq_data *data)
|
|
{
|
|
struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
|
|
|
|
irq_ctx->enabled = true;
|
|
}
|
|
|
|
static struct irq_chip irq_sim_irqchip = {
|
|
.name = "irq_sim",
|
|
.irq_mask = irq_sim_irqmask,
|
|
.irq_unmask = irq_sim_irqunmask,
|
|
};
|
|
|
|
static void irq_sim_handle_irq(struct irq_work *work)
|
|
{
|
|
struct irq_sim_work_ctx *work_ctx;
|
|
|
|
work_ctx = container_of(work, struct irq_sim_work_ctx, work);
|
|
handle_simple_irq(irq_to_desc(work_ctx->irq));
|
|
}
|
|
|
|
/**
|
|
* irq_sim_init - Initialize the interrupt simulator: allocate a range of
|
|
* dummy interrupts.
|
|
*
|
|
* @sim: The interrupt simulator object to initialize.
|
|
* @num_irqs: Number of interrupts to allocate
|
|
*
|
|
* Returns 0 on success and a negative error number on failure.
|
|
*/
|
|
int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs)
|
|
{
|
|
int i;
|
|
|
|
sim->irqs = kmalloc_array(num_irqs, sizeof(*sim->irqs), GFP_KERNEL);
|
|
if (!sim->irqs)
|
|
return -ENOMEM;
|
|
|
|
sim->irq_base = irq_alloc_descs(-1, 0, num_irqs, 0);
|
|
if (sim->irq_base < 0) {
|
|
kfree(sim->irqs);
|
|
return sim->irq_base;
|
|
}
|
|
|
|
for (i = 0; i < num_irqs; i++) {
|
|
sim->irqs[i].irqnum = sim->irq_base + i;
|
|
sim->irqs[i].enabled = false;
|
|
irq_set_chip(sim->irq_base + i, &irq_sim_irqchip);
|
|
irq_set_chip_data(sim->irq_base + i, &sim->irqs[i]);
|
|
irq_set_handler(sim->irq_base + i, &handle_simple_irq);
|
|
irq_modify_status(sim->irq_base + i,
|
|
IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
|
|
}
|
|
|
|
init_irq_work(&sim->work_ctx.work, irq_sim_handle_irq);
|
|
sim->irq_count = num_irqs;
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(irq_sim_init);
|
|
|
|
/**
|
|
* irq_sim_fini - Deinitialize the interrupt simulator: free the interrupt
|
|
* descriptors and allocated memory.
|
|
*
|
|
* @sim: The interrupt simulator to tear down.
|
|
*/
|
|
void irq_sim_fini(struct irq_sim *sim)
|
|
{
|
|
irq_work_sync(&sim->work_ctx.work);
|
|
irq_free_descs(sim->irq_base, sim->irq_count);
|
|
kfree(sim->irqs);
|
|
}
|
|
EXPORT_SYMBOL_GPL(irq_sim_fini);
|
|
|
|
/**
|
|
* irq_sim_fire - Enqueue an interrupt.
|
|
*
|
|
* @sim: The interrupt simulator object.
|
|
* @offset: Offset of the simulated interrupt which should be fired.
|
|
*/
|
|
void irq_sim_fire(struct irq_sim *sim, unsigned int offset)
|
|
{
|
|
if (sim->irqs[offset].enabled) {
|
|
sim->work_ctx.irq = irq_sim_irqnum(sim, offset);
|
|
irq_work_queue(&sim->work_ctx.work);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_GPL(irq_sim_fire);
|
|
|
|
/**
|
|
* irq_sim_irqnum - Get the allocated number of a dummy interrupt.
|
|
*
|
|
* @sim: The interrupt simulator object.
|
|
* @offset: Offset of the simulated interrupt for which to retrieve
|
|
* the number.
|
|
*/
|
|
int irq_sim_irqnum(struct irq_sim *sim, unsigned int offset)
|
|
{
|
|
return sim->irqs[offset].irqnum;
|
|
}
|
|
EXPORT_SYMBOL_GPL(irq_sim_irqnum);
|