Skip to contentSkip to conversation

irqchip test driver/sandbox

PATCH Superseded Radu Rendec 2 participants 3 messages Sep 23, 2025 – Sep 25, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover irqchip test driver/sandbox

This is a dummy irqchip driver that implements three different types of IRQ chips, requests interrupts like a real device driver would do, and provides the ability to manually trigger any of these interrupts. The main reason why I wrote it was to be able to hack on the kernel IRQ core without depending on any hardware (even virtual, inside a VM). But it can also be used as an example for implementing irqchip drivers.

The irqchip setup/layout is described in detail in a comment block at the top of the driver's source file, so I'm not repeating it here. Two "leaf" IRQ domains are implemented: hierarchical and multiplexing. These are attached to an additional "root" domain, which is similar to APIC (x86_64) or GIC (arm64). Interrupts are simulated by calling the generic interrupt handling function on the "root" domain, in a workqueue context and with local interrupts disabled.

A few notes on the implementation:
- The driver cannot be compiled as a loadable module, and the only reason is because it uses irq_move_irq(), which is not exported. I could not find a better way to make IRQ affinity control for the "root" domain work on x86. This is also documented in the source code.
- The driver uses a write-only module parameter with a custom handler as the interface to trigger simulated interrupts. While this slightly abuses the module parameter API, it's very simple to implement and also keeps the sysfs file confined in the module's own "namespace" at /sys/module/irqc_test. Of course, setting the parameter on the kernel command line makes no sense but doesn't have any side effect either.

Radu Rendec (1):
samples: Add irqchip test driver

 samples/Kconfig             |  10 +
 samples/Makefile            |   1 +
 samples/irqchip/Makefile    |   1 +
 samples/irqchip/irqc_test.c | 576 ++++++++++++++++++++++++++++++++++++
 4 files changed, 588 insertions(+)
 create mode 100644 samples/irqchip/Makefile
 create mode 100644 samples/irqchip/irqc_test.c
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/1 samples: Add irqchip test driver

Add a test driver that demonstrates implementation of multiple IRQ chip types and their domains. The driver implements:

- Root IRQ chip simulating direct CPU interrupt lines
- Hierarchical IRQ domain with 1:1 parent mapping
- Interrupt multiplexer domain with chained interrupts

The driver supports simulating interrupt handling by triggering fake interrupts via sysfs parameter, with CPU affinity simulation using targeted work items.

This serves as a reference implementation and testing sandbox for IRQ chip driver development and debugging.

---
 samples/Kconfig             |  10 +
 samples/Makefile            |   1 +
 samples/irqchip/Makefile    |   1 +
 samples/irqchip/irqc_test.c | 576 ++++++++++++++++++++++++++++++++++++
 4 files changed, 588 insertions(+)
 create mode 100644 samples/irqchip/Makefile
 create mode 100644 samples/irqchip/irqc_test.c
samples/Kconfig +10 -0
index 6e072a5f1ed86..6a4f696ca4311 100644
@@ -320,6 +320,16 @@ config SAMPLE_HUNG_TASK
 	  Reading these files with multiple processes triggers hung task
 	  detection by holding locks for a long time (256 seconds).
 
+config SAMPLE_IRQCHIP
+	bool "Test IRQ chip driver"
+	help
+	  Build a driver that demonstrates the implementation of multiple IRQ
+	  chip types (root, hierarchical, multiplexing). It serves as a
+	  reference implementation for IRQ chip driver development, showcasing
+	  different domain management patterns and interrupt routing mechanisms.
+	  It supports simulation of interrupt handling with CPU affinity through
+	  sysfs parameters.
+
 source "samples/rust/Kconfig"
 
 source "samples/damon/Kconfig"
samples/Makefile +1 -0
index 07641e177bd8b..380168e6eae55 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_SAMPLE_CONNECTOR)		+= connector/
 obj-$(CONFIG_SAMPLE_FANOTIFY_ERROR)	+= fanotify/
 subdir-$(CONFIG_SAMPLE_HIDRAW)		+= hidraw
 obj-$(CONFIG_SAMPLE_HW_BREAKPOINT)	+= hw_breakpoint/
+obj-$(CONFIG_SAMPLE_IRQCHIP)		+= irqchip/
 obj-$(CONFIG_SAMPLE_KDB)		+= kdb/
 obj-$(CONFIG_SAMPLE_KFIFO)		+= kfifo/
 obj-$(CONFIG_SAMPLE_KOBJECT)		+= kobject/
samples/irqchip/Makefile new file +1 -0
new file mode 100644
index 0000000000000..fb8600ee15e67
@@ -0,0 +1 @@
+obj-$(CONFIG_SAMPLE_IRQCHIP) += irqc_test.o
samples/irqchip/irqc_test.c new file +576 -0

576 lines not on this page · raw message

Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH 1/1] samples: Add irqchip test driver

kernel test robot noticed the following build errors:

Hi Radu,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.17-rc7 next-20250925]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Radu-Rendec/samples-Add-irqchip-test-driver/20250924-073245
base:   linus/master
patch link:    https://lore.kernel.org/r/20250923232905.1510547-2-rrendec%40redhat.com
patch subject: [PATCH 1/1] samples: Add irqchip test driver
config: i386-randconfig-015-20250926 (https://download.01.org/0day-ci/archive/20250926/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250926/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> samples/irqchip/irqc_test.c:187:10: error: use of undeclared identifier 'irq_domain_free_irqs_common'; did you mean 'irq_domain_free_irqs'?
     187 |         .free = irq_domain_free_irqs_common,
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                 irq_domain_free_irqs
   include/linux/irqdomain.h:651:20: note: 'irq_domain_free_irqs' declared here
     651 | static inline void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs) { }
         |                    ^
>> samples/irqchip/irqc_test.c:186:3: error: field designator 'alloc' does not refer to any field in type 'const struct irq_domain_ops'
     186 |         .alloc = root_alloc,
         |         ~^~~~~~~~~~~~~~~~~~
>> samples/irqchip/irqc_test.c:187:3: error: field designator 'free' does not refer to any field in type 'const struct irq_domain_ops'
     187 |         .free = irq_domain_free_irqs_common,
         |         ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> samples/irqchip/irqc_test.c:188:3: error: field designator 'translate' does not refer to any field in type 'const struct irq_domain_ops'
     188 |         .translate = irq_domain_translate_onecell,
         |         ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> samples/irqchip/irqc_test.c:198:13: error: use of undeclared identifier 'irq_chip_ack_parent'
     198 |         .irq_ack = irq_chip_ack_parent,
         |                    ^
>> samples/irqchip/irqc_test.c:199:22: error: use of undeclared identifier 'irq_chip_set_affinity_parent'; did you mean 'irq_set_affinity_hint'?
     199 |         .irq_set_affinity = irq_chip_set_affinity_parent,
         |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                             irq_set_affinity_hint
   include/linux/interrupt.h:404:19: note: 'irq_set_affinity_hint' declared here
     404 | static inline int irq_set_affinity_hint(unsigned int irq,
         |                   ^
>> samples/irqchip/irqc_test.c:219:19: error: no member named 'parent' in 'struct irq_domain'
     219 |                 .fwnode = irqd->parent->fwnode,
         |                           ~~~~  ^
>> samples/irqchip/irqc_test.c:236:20: error: no member named 'parent_data' in 'struct irq_data'
     236 |                         virq + i, d, d->parent_data);
         |                                      ~  ^
   include/linux/printk.h:587:34: note: expanded from macro 'pr_info'
     587 |         printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
         |                                         ^~~~~~~~~~~
   include/linux/printk.h:514:60: note: expanded from macro 'printk'
     514 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
         |                                                            ^~~~~~~~~~~
   include/linux/printk.h:486:19: note: expanded from macro 'printk_index_wrap'
     486 |                 _p_func(_fmt, ##__VA_ARGS__);                           \
         |                                 ^~~~~~~~~~~
>> samples/irqchip/irqc_test.c:237:3: error: call to undeclared function 'irq_domain_set_hwirq_and_chip'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     237 |                 irq_domain_set_hwirq_and_chip(irqd, virq + i, hwirq + i,
         |                 ^
>> samples/irqchip/irqc_test.c:256:9: error: call to undeclared function 'irq_domain_alloc_irqs_parent'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     256 |         return irq_domain_alloc_irqs_parent(irqd, virq, nr_irqs, &parent_fwspec);
         |                ^
   samples/irqchip/irqc_test.c:256:9: note: did you mean 'irq_domain_alloc_irqs'?
   include/linux/irqdomain.h:645:19: note: 'irq_domain_alloc_irqs' declared here
     645 | static inline int irq_domain_alloc_irqs(struct irq_domain *domain, unsigned int nr_irqs,
         |                   ^
   samples/irqchip/irqc_test.c:269:10: error: use of undeclared identifier 'irq_domain_free_irqs_common'; did you mean 'irq_domain_free_irqs'?
     269 |         .free = irq_domain_free_irqs_common,
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                 irq_domain_free_irqs
   include/linux/irqdomain.h:651:20: note: 'irq_domain_free_irqs' declared here
     651 | static inline void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs) { }
         |                    ^
   samples/irqchip/irqc_test.c:268:3: error: field designator 'alloc' does not refer to any field in type 'const struct irq_domain_ops'
     268 |         .alloc = hier_alloc,
         |         ~^~~~~~~~~~~~~~~~~~
   samples/irqchip/irqc_test.c:269:3: error: field designator 'free' does not refer to any field in type 'const struct irq_domain_ops'
     269 |         .free = irq_domain_free_irqs_common,
         |         ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   samples/irqchip/irqc_test.c:270:3: error: field designator 'translate' does not refer to any field in type 'const struct irq_domain_ops'
     270 |         .translate = irq_domain_translate_onecell,
         |         ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> samples/irqchip/irqc_test.c:438:14: error: call to undeclared function 'irq_domain_create_hierarchy'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     438 |         hier_irqd = irq_domain_create_hierarchy(root_irqd, 0, NUM_TOTAL_IRQS_HIER,
         |                     ^
   samples/irqchip/irqc_test.c:438:14: note: did you mean 'irq_domain_create_legacy'?
   include/linux/irqdomain.h:324:20: note: 'irq_domain_create_legacy' declared here
     324 | struct irq_domain *irq_domain_create_legacy(struct fwnode_handle *fwnode, unsigned int size,
         |                    ^
>> samples/irqchip/irqc_test.c:438:12: error: incompatible integer to pointer conversion assigning to 'struct irq_domain *' from 'int' [-Wint-conversion]
     438 |         hier_irqd = irq_domain_create_hierarchy(root_irqd, 0, NUM_TOTAL_IRQS_HIER,
         |                   ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     439 |                                                 hier_fwnode, &hier_ops, NULL);
         |                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

16 errors generated.

vim +187 samples/irqchip/irqc_test.c

166
167 /*
168 * Parent IRQ domains *must* use the new API (i.e. alloc/free vs. map) to
169 * support hierarchy, for the following reasons:
170 * - The child .alloc() function calls irq_domain_alloc_irqs_parent().
171 * - The irq_domain_alloc_irqs_hierarchy() is just a thin wrapper around
172 * irq_domain_alloc_irqs_hierarchy().
173 * - Finally, irq_domain_alloc_irqs_hierarchy() explicitly checks that
174 * the (parent) domain implements the alloc() function and returns an
175 * error otherwise.
176 *
177 * Furthermore, Documentation/core-api/irq/irq-domain.rst clearly states that
178 * the new API must be used for all domains to support hierarchy.
179 *
180 * Note: The .free() function is also part of the new API and is mandatory but

   181	 *       there is a default implementation that we can use. If we do not
   182	 *       implement this function and .alloc() fails for whatever reason, we get
   183	 *       an oops as the IRQ framework tries to rollback whatever has been done.
   184	 */
   185	static const struct irq_domain_ops root_ops = {
 > 186		.alloc = root_alloc,
 > 187		.free = irq_domain_free_irqs_common,
 > 188		.translate = irq_domain_translate_onecell,
   189	};
   190	
   191	/*
   192	 * Since hierarchical interrupts are mapped 1:1 in all domains along the path,
   193	 * CPU affinity is not shared with other interrupts. Use the parent handlers to
   194	 * set the affinity in the root domain.
   195	 */
   196	static const struct irq_chip hier_chip = {
   197		.name = "TEST-HIER-IC",
 > 198		.irq_ack = irq_chip_ack_parent,
 > 199		.irq_set_affinity = irq_chip_set_affinity_parent,
   200	};
   201	
   202	/*
   203	 * For a GPIO IRQ chip, this is gpiochip_hierarchy_irq_domain_alloc(), and the
   204	 * ops pointer is set in gpiochip_hierarchy_setup_domain_ops(), called from
   205	 * gpiochip_hierarchy_add_domain(). See full stack sample below in testirq_init().
   206	 *
   207	 * This function is called indirectly by irq_create_fwspec_mapping() through
   208	 * irq_domain_alloc_irqs_locked().
   209	 *
   210	 * How to search for other examples:
   211	 *   grep -rn '\.alloc\>' drivers/gpio/
   212	 *   grep -rn '\<irq_domain_alloc_irqs_parent\>' drivers/irqchip/
   213	 */
   214	static int hier_alloc(struct irq_domain *irqd, unsigned int virq,
   215			      unsigned int nr_irqs, void *data)
   216	{
   217		struct irq_fwspec *fwspec = data;
   218		struct irq_fwspec parent_fwspec = {
 > 219			.fwnode = irqd->parent->fwnode,
   220			.param_count = 1,
   221		};
   222		irq_hw_number_t hwirq;
   223		unsigned int type;
   224		int ret, i;
   225	
   226		pr_info("[%s] virq = %u, nr_irqs = %u\n", irqd->name, virq, nr_irqs);
   227	
   228		ret = irq_domain_translate_onecell(irqd, fwspec, &hwirq, &type);
   229		if (ret)
   230			return ret;
   231	
   232		for (i = 0; i < nr_irqs; i++) {
   233			struct irq_data *d = irq_get_irq_data(virq + i);
   234	
   235			pr_info("\tvirq = %u, data = %pS, parent = %pS\n",
 > 236				virq + i, d, d->parent_data);
 > 237			irq_domain_set_hwirq_and_chip(irqd, virq + i, hwirq + i,
   238						      &hier_chip, NULL);
   239		}
   240	
   241		/*
   242		 * We're not done yet, we still need to allocate interrupts in the
   243		 * parent domain. If we just "return 0" at this point, the upstream
   244		 * call to irq_create_fwspec_mapping() fails with -EINVAL.
   245		 *
   246		 * By now, the framework has already allocated a struct irq_data for
   247		 * each virq, for both our domain (hier) and the parent domain (root).
   248		 * So, irq_get_irq_data(virq) and irq_get_irq_data(virq)->parent_data
   249		 * are both non-null. The problem is that parent_data->chip is NULL,
   250		 * and this fails the sanity checks in irq_domain_trim_hierarchy()
   251		 * (called from irq_domain_alloc_irqs_locked()).
   252		 */
   253	
   254		parent_fwspec.param[0] = hwirq;
   255	
 > 256		return irq_domain_alloc_irqs_parent(irqd, virq, nr_irqs, &parent_fwspec);

257 }
258

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki