From patchwork Mon Aug 14 14:53:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 801220 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="lubPJZ4u"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xWJY14fzDz9t2r for ; Tue, 15 Aug 2017 00:54:29 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752878AbdHNOyO (ORCPT ); Mon, 14 Aug 2017 10:54:14 -0400 Received: from mail-wm0-f53.google.com ([74.125.82.53]:34915 "EHLO mail-wm0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752643AbdHNOxZ (ORCPT ); Mon, 14 Aug 2017 10:53:25 -0400 Received: by mail-wm0-f53.google.com with SMTP id m85so39372205wma.0 for ; Mon, 14 Aug 2017 07:53:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+XhZpGJEmeJvoEuFv0n4dznCGaDn+toqwvG+Ytf7f9U=; b=lubPJZ4upI+QPDZlj+C5kXmUc2C/1GwGNzVgx75QcfF4OMFxsQg4M8/n2G/HbWYSWC gZLNuNnAhEJQUIXsTClZAtjm30RlzdM+iSItVLBS7sVKO0WQCL0tX9WnO3l7NMbVKTUG Bqa7Q9kKvQspsw8RuEpnHeXe7OZ87EGwo4y8dNjH6Zw1qszy6sHC4C0hJ9A4N/iYj7t8 Ecs2+Thn49Q2lKaRKx5ndswtJ7vahghxIbzgAzO5wwLf+BH9BA/9jQw3gPOghHaxJVrd WBvNo2C99kRn0ZYWU2dcC931wRd1HYYgcUvnTIQMn8/SlvtPXrSms7pNqyQQ14m4ScLF YOjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+XhZpGJEmeJvoEuFv0n4dznCGaDn+toqwvG+Ytf7f9U=; b=nlZkbPCKYC0Y9bJJKf4sBoJ6peBgfLgAODUIfNw1K2ybKg+NT0wkKSZetVU4DkQ5fj jIQ8aZuDSu/iLzu5Xmr1t7CziZdIJvE11UaU3zMfowc5ZmDtIAQF3fb95U3rREp5e6R4 1PVYsbGOp9jY1ivK3F7VyH0Y+1AF2OcZafYgTAgJcx5HZ8J7X0CsHDdNdihg8Oz9O5Ir tOXKW1ktKxfG2LhDpc4w3Ta4u+UNarhGXUrN0pJkyTME+hL7bcDK+EC5swypgQgE3MS8 HhGRqWRXBUlKocQ4EIDiwfKD1SNLm5n6KoY2HV9+hRHlNihQ+hRBXLbd/+F1pCJ8rhsV EIiQ== X-Gm-Message-State: AHYfb5iGm+LkBc8htBg8LvRqwAaxQF2hIL5bOpbGSVzKfzoA59C3Hunj 5e72Yghr3VpHbjC5 X-Received: by 10.28.222.70 with SMTP id v67mr4628214wmg.156.1502722403927; Mon, 14 Aug 2017 07:53:23 -0700 (PDT) Received: from bgdev-debian.lan (LFbn-1-7079-41.w90-116.abo.wanadoo.fr. [90.116.248.41]) by smtp.gmail.com with ESMTPSA id b125sm4554542wmc.41.2017.08.14.07.53.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Aug 2017 07:53:23 -0700 (PDT) From: Bartosz Golaszewski To: Linus Walleij , Thomas Gleixner , Marc Zyngier , Jonathan Corbet , Bamvor Jian Zhang , Jonathan Cameron , Lars-Peter Clausen Cc: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v4 1/3] irq/irq_sim: add a simple interrupt simulator framework Date: Mon, 14 Aug 2017 16:53:16 +0200 Message-Id: <20170814145318.6495-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.13.2 In-Reply-To: <20170814145318.6495-1-brgl@bgdev.pl> References: <20170814145318.6495-1-brgl@bgdev.pl> Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org 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 Reviewed-by: Jonathan Cameron --- include/linux/irq_sim.h | 44 ++++++++++++++++++ kernel/irq/Kconfig | 5 ++ kernel/irq/Makefile | 1 + kernel/irq/irq_sim.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 include/linux/irq_sim.h create mode 100644 kernel/irq/irq_sim.c diff --git a/include/linux/irq_sim.h b/include/linux/irq_sim.h new file mode 100644 index 000000000000..9ee1a4f8bd94 --- /dev/null +++ b/include/linux/irq_sim.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2017 Bartosz Golaszewski + * + * 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. + */ + +#ifndef _LINUX_IRQ_SIM_H +#define _LINUX_IRQ_SIM_H + +#include + +/* + * Provides a framework for allocating simulated interrupts which can be + * requested like normal irqs and enqueued from process context. + */ + +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/kernel/irq/Kconfig b/kernel/irq/Kconfig index 27c4e774071c..1d06af787932 100644 --- a/kernel/irq/Kconfig +++ b/kernel/irq/Kconfig @@ -63,6 +63,11 @@ config GENERIC_IRQ_CHIP config IRQ_DOMAIN bool +# Support for simulated interrupts +config IRQ_SIM + bool + select IRQ_WORK + # Support for hierarchical irq domains config IRQ_DOMAIN_HIERARCHY bool diff --git a/kernel/irq/Makefile b/kernel/irq/Makefile index e4aef7351f2b..1970cafe8f2a 100644 --- a/kernel/irq/Makefile +++ b/kernel/irq/Makefile @@ -4,6 +4,7 @@ obj-$(CONFIG_IRQ_TIMINGS) += timings.o obj-$(CONFIG_GENERIC_IRQ_CHIP) += generic-chip.o obj-$(CONFIG_GENERIC_IRQ_PROBE) += autoprobe.o obj-$(CONFIG_IRQ_DOMAIN) += irqdomain.o +obj-$(CONFIG_IRQ_SIM) += irq_sim.o obj-$(CONFIG_PROC_FS) += proc.o obj-$(CONFIG_GENERIC_PENDING_IRQ) += migration.o obj-$(CONFIG_GENERIC_IRQ_MIGRATION) += cpuhotplug.o diff --git a/kernel/irq/irq_sim.c b/kernel/irq/irq_sim.c new file mode 100644 index 000000000000..31a2c12a79ae --- /dev/null +++ b/kernel/irq/irq_sim.c @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2017 Bartosz Golaszewski + * + * 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 +#include + +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);