Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1701131 > unrolled thread

[PATCH v2 1/3] irq/irq_sim: add a simple interrupt simulator framework

Started byBartosz Golaszewski <brgl@bgdev.pl>
First post2017-08-01 17:00 +0200
Last post2017-08-14 12:10 +0200
Articles 4 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH v2 1/3] irq/irq_sim: add a simple interrupt simulator framework Bartosz Golaszewski <brgl@bgdev.pl> - 2017-08-01 17:00 +0200
    Re: [PATCH v2 1/3] irq/irq_sim: add a simple interrupt simulator  framework Jonathan Cameron <jic23@kernel.org> - 2017-08-12 13:50 +0200
      Re: [PATCH v2 1/3] irq/irq_sim: add a simple interrupt simulator framework Bartosz Golaszewski <brgl@bgdev.pl> - 2017-08-14 12:00 +0200
        Re: [PATCH v2 1/3] irq/irq_sim: add a simple interrupt simulator framework Jonathan Cameron <jic23@jic23.retrosnub.co.uk> - 2017-08-14 12:10 +0200

#1701131 — [PATCH v2 1/3] irq/irq_sim: add a simple interrupt simulator framework

FromBartosz Golaszewski <brgl@bgdev.pl>
Date2017-08-01 17:00 +0200
Subject[PATCH v2 1/3] irq/irq_sim: add a simple interrupt simulator framework
Message-ID<u9GOm-5h7-15@gated-at.bofh.it>
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>
---
 include/linux/irq_sim.h |  37 +++++++++++++++
 init/Kconfig            |   4 ++
 kernel/Makefile         |   1 +
 kernel/irq_sim.c        | 119 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 161 insertions(+)
 create mode 100644 include/linux/irq_sim.h
 create mode 100644 kernel/irq_sim.c

diff --git a/include/linux/irq_sim.h b/include/linux/irq_sim.h
new file mode 100644
index 000000000000..0c1abf0e3244
--- /dev/null
+++ b/include/linux/irq_sim.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
+ *
+ * Provides a framework for allocating simulated interrupts which can be
+ * requested like normal irqs and enqueued from process context.
+ */
+
+#ifndef _LINUX_IRQ_SIM_H
+#define _LINUX_IRQ_SIM_H
+
+#include <linux/irq_work.h>
+
+struct irq_sim_work_ctx {
+	struct irq_work work;
+	int irq;
+};
+
+struct irq_sim_irq_ctx {
+	int irqnum;
+	bool enabled;
+};
+
+struct irq_sim {
+	struct irq_sim_work_ctx work_ctx;
+	int irq_base;
+	unsigned int irq_count;
+	struct irq_sim_irq_ctx *irqs;
+};
+
+int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs);
+void irq_sim_fini(struct irq_sim *sim);
+
+void irq_sim_fire(struct irq_sim *sim, unsigned int offset);
+
+int irq_sim_irqnum(struct irq_sim *sim, unsigned int offset);
+
+#endif /* _LINUX_IRQ_SIM_H */
diff --git a/init/Kconfig b/init/Kconfig
index 8514b25db21c..220456599c3f 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -23,6 +23,10 @@ config CONSTRUCTORS
 config IRQ_WORK
 	bool
 
+config IRQ_SIM
+	bool
+	select IRQ_WORK
+
 config BUILDTIME_EXTABLE_SORT
 	bool
 
diff --git a/kernel/Makefile b/kernel/Makefile
index 4cb8e8b23c6e..4472567c5835 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -97,6 +97,7 @@ obj-$(CONFIG_TRACE_CLOCK) += trace/
 obj-$(CONFIG_RING_BUFFER) += trace/
 obj-$(CONFIG_TRACEPOINTS) += trace/
 obj-$(CONFIG_IRQ_WORK) += irq_work.o
+obj-$(CONFIG_IRQ_SIM) += irq_sim.o
 obj-$(CONFIG_CPU_PM) += cpu_pm.o
 obj-$(CONFIG_BPF) += bpf/
 
diff --git a/kernel/irq_sim.c b/kernel/irq_sim.c
new file mode 100644
index 000000000000..4387e2bee97c
--- /dev/null
+++ b/kernel/irq_sim.c
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
+ *
+ * Provides a framework for allocating simulated interrupts which can be
+ * requested like normal irqs and enqueued from process context.
+ */
+
+#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);
-- 
2.13.2

[toc] | [next] | [standalone]


#1710238 — Re: [PATCH v2 1/3] irq/irq_sim: add a simple interrupt simulator framework

FromJonathan Cameron <jic23@kernel.org>
Date2017-08-12 13:50 +0200
SubjectRe: [PATCH v2 1/3] irq/irq_sim: add a simple interrupt simulator framework
Message-ID<udD5v-bo-3@gated-at.bofh.it>
In reply to#1701131
On Tue,  1 Aug 2017 16:50:26 +0200
Bartosz Golaszewski <brgl@bgdev.pl> wrote:

> 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>
Looks good to me.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

Only tiny thing is the lack of a specified license for the code...
+ checkpatch is warning about wrong file mode...
#105: 
new file mode 100644

Though I have no idea why...

+ behind a certain firewall so no Google available...
> ---
>  include/linux/irq_sim.h |  37 +++++++++++++++
>  init/Kconfig            |   4 ++
>  kernel/Makefile         |   1 +
>  kernel/irq_sim.c        | 119 ++++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 161 insertions(+)
>  create mode 100644 include/linux/irq_sim.h
>  create mode 100644 kernel/irq_sim.c
> 
> diff --git a/include/linux/irq_sim.h b/include/linux/irq_sim.h
> new file mode 100644
> index 000000000000..0c1abf0e3244
> --- /dev/null
> +++ b/include/linux/irq_sim.h
> @@ -0,0 +1,37 @@
> +/*
> + * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
> + *
> + * Provides a framework for allocating simulated interrupts which can be
> + * requested like normal irqs and enqueued from process context.
> + */
> +
> +#ifndef _LINUX_IRQ_SIM_H
> +#define _LINUX_IRQ_SIM_H
> +
> +#include <linux/irq_work.h>
> +
> +struct irq_sim_work_ctx {
> +	struct irq_work work;
> +	int irq;
> +};
> +
> +struct irq_sim_irq_ctx {
> +	int irqnum;
> +	bool enabled;
> +};
> +
> +struct irq_sim {
> +	struct irq_sim_work_ctx work_ctx;
> +	int irq_base;
> +	unsigned int irq_count;
> +	struct irq_sim_irq_ctx *irqs;
> +};
> +
> +int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs);
> +void irq_sim_fini(struct irq_sim *sim);
> +
> +void irq_sim_fire(struct irq_sim *sim, unsigned int offset);
> +
> +int irq_sim_irqnum(struct irq_sim *sim, unsigned int offset);
> +
> +#endif /* _LINUX_IRQ_SIM_H */
> diff --git a/init/Kconfig b/init/Kconfig
> index 8514b25db21c..220456599c3f 100644
> --- a/init/Kconfig
> +++ b/init/Kconfig
> @@ -23,6 +23,10 @@ config CONSTRUCTORS
>  config IRQ_WORK
>  	bool
>  
> +config IRQ_SIM
> +	bool
You could make this tristate, but then the handling of the
users would get complex so perhaps given it's so small boolean
is the way to go.
> +	select IRQ_WORK
> +
>  config BUILDTIME_EXTABLE_SORT
>  	bool
>  
> diff --git a/kernel/Makefile b/kernel/Makefile
> index 4cb8e8b23c6e..4472567c5835 100644
> --- a/kernel/Makefile
> +++ b/kernel/Makefile
> @@ -97,6 +97,7 @@ obj-$(CONFIG_TRACE_CLOCK) += trace/
>  obj-$(CONFIG_RING_BUFFER) += trace/
>  obj-$(CONFIG_TRACEPOINTS) += trace/
>  obj-$(CONFIG_IRQ_WORK) += irq_work.o
> +obj-$(CONFIG_IRQ_SIM) += irq_sim.o
>  obj-$(CONFIG_CPU_PM) += cpu_pm.o
>  obj-$(CONFIG_BPF) += bpf/
>  
> diff --git a/kernel/irq_sim.c b/kernel/irq_sim.c
> new file mode 100644
> index 000000000000..4387e2bee97c
> --- /dev/null
> +++ b/kernel/irq_sim.c
> @@ -0,0 +1,119 @@
> +/*
> + * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
> + *
> + * Provides a framework for allocating simulated interrupts which can be
> + * requested like normal irqs and enqueued from process context.
> + */
> +
> +#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);

[toc] | [prev] | [next] | [standalone]


#1710775

FromBartosz Golaszewski <brgl@bgdev.pl>
Date2017-08-14 12:00 +0200
Message-ID<uekka-2xb-21@gated-at.bofh.it>
In reply to#1710238
2017-08-12 13:43 GMT+02:00 Jonathan Cameron <jic23@kernel.org>:
> On Tue,  1 Aug 2017 16:50:26 +0200
> Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
>> 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>
> Looks good to me.
>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>
> Only tiny thing is the lack of a specified license for the code...

I'll send a v3 with license added.

> + checkpatch is warning about wrong file mode...
> #105:
> new file mode 100644
>
> Though I have no idea why...
>

I think this only says that a file was created with given mode, it's
not a warning. The actual warning is about missing a new entry in
MAINTAINERS.

>> --- a/init/Kconfig
>> +++ b/init/Kconfig
>> @@ -23,6 +23,10 @@ config CONSTRUCTORS
>>  config IRQ_WORK
>>       bool
>>
>> +config IRQ_SIM
>> +     bool
> You could make this tristate, but then the handling of the
> users would get complex so perhaps given it's so small boolean
> is the way to go.
>

Nah, irq_work is built-in to at even greater size. Let's just leave it
like this, especially when only testing modules select it.

Thanks,
Bartosz

[toc] | [prev] | [next] | [standalone]


#1710787

FromJonathan Cameron <jic23@jic23.retrosnub.co.uk>
Date2017-08-14 12:10 +0200
Message-ID<uektP-2Py-1@gated-at.bofh.it>
In reply to#1710775

On 14 August 2017 17:54:22 GMT+08:00, Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>2017-08-12 13:43 GMT+02:00 Jonathan Cameron <jic23@kernel.org>:
>> On Tue,  1 Aug 2017 16:50:26 +0200
>> Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>>
>>> 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>
>> Looks good to me.
>>
>> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>>
>> Only tiny thing is the lack of a specified license for the code...
>
>I'll send a v3 with license added.
>
>> + checkpatch is warning about wrong file mode...
>> #105:
>> new file mode 100644
>>
>> Though I have no idea why...
>>
>
>I think this only says that a file was created with given mode, it's
>not a warning. The actual warning is about missing a new entry in
>MAINTAINERS.
Doh, how did I miss that!


>
>>> --- a/init/Kconfig
>>> +++ b/init/Kconfig
>>> @@ -23,6 +23,10 @@ config CONSTRUCTORS
>>>  config IRQ_WORK
>>>       bool
>>>
>>> +config IRQ_SIM
>>> +     bool
>> You could make this tristate, but then the handling of the
>> users would get complex so perhaps given it's so small boolean
>> is the way to go.
>>
>
>Nah, irq_work is built-in to at even greater size. Let's just leave it
>like this, especially when only testing modules select it.
>
Fair enough.

>Thanks,
>Bartosz

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web