Skip to content

Commit

Permalink
arch: sw_isr_table: Implement local interrupt table entry creation
Browse files Browse the repository at this point in the history
This commit implements the possibility to locally create an interrupt
table entry. This changes the way interrput table is created,
now it should not be created as an source file but rather it would be
constructed by the linker.

Signed-off-by: Radoslaw Koppel <[email protected]>
  • Loading branch information
rakons committed Dec 18, 2023
1 parent 33ae98b commit e8dfb7a
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
11 changes: 11 additions & 0 deletions arch/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,17 @@ config NOCACHE_MEMORY

menu "Interrupt Configuration"

config ISR_TABLES_LOCAL_DECLARATION
bool "Enable new isr tables created locally and placed by linker"
help
Enable new scheme of interrupt tables generation.
This is totally different generator that would create tables entries locally
where the IRQ_CONNECT macro is called and then use the linker script to position it
in the right place in memory.
The most important adventage of such approach is that the genreated interrupt tables
are LTO compatible.
The drawback is that the support on the architecture port is required.

config DYNAMIC_INTERRUPTS
bool "Installation of IRQs at runtime"
help
Expand Down
101 changes: 101 additions & 0 deletions include/zephyr/sw_isr_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <zephyr/device.h>
#include <zephyr/types.h>
#include <zephyr/toolchain.h>
#include <zephyr/sys/util.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -56,6 +57,9 @@ struct _irq_parent_entry {
* uses it to create the IRQ vector table and the _sw_isr_table.
*
* More discussion in include/linker/intlist.ld
*
* This is a version used when CONFIG_ISR_TABLES_LOCAL_DECLARATION is disabled.
* See _isr_list_sname used otherwise.
*/
struct _isr_list {
/** IRQ line number */
Expand All @@ -68,6 +72,24 @@ struct _isr_list {
const void *param;
};

/*
* Data structure created in a speciall binary .intlist section for each
* configured interrupt. gen_isq_tables.py pulls this out of the binary and
* uses ti to create linker script chunks that would place interrupt table entries
* in the right place in the memory.
*
* This is a version used when CONFIG_ISR_TABLES_LOCAL_DECLARATION is enabled.
* See _isr_list used otherwise.
*/
struct _isr_list_sname {
/** IRQ line number */
int32_t irq;
/** Flags for this IRQ, see ISR_FLAG_* definitions */
int32_t flags;
/** The section name */
const char sname[];
} __packed;

#ifdef CONFIG_SHARED_INTERRUPTS
struct z_shared_isr_client {
void (*isr)(const void *arg);
Expand All @@ -90,6 +112,77 @@ extern struct z_shared_isr_table_entry z_shared_sw_isr_table[];
#define _MK_ISR_NAME(x, y) __MK_ISR_NAME(x, y)
#define __MK_ISR_NAME(x, y) __isr_ ## x ## _irq_ ## y


#if IS_ENABLED(CONFIG_ISR_TABLES_LOCAL_DECLARATION)

#define _MK_ISR_ELEMENT_NAME(func, id) __MK_ISR_ELEMENT_NAME(func, id)
#define __MK_ISR_ELEMENT_NAME(func, id) __isr_table_entry_ ## func ## _irq_ ## id

#define _MK_IRQ_ELEMENT_NAME(func, id) __MK_ISR_ELEMENT_NAME(func, id)
#define __MK_IRQ_ELEMENT_NAME(func, id) __irq_table_entry_ ## func ## _irq_ ## id

#define _MK_ISR_ELEMENT_SECTION(id) __MK_ISR_ELEMENT_SECTION(id)
#define __MK_ISR_ELEMENT_SECTION(id) irq.id

#define _MK_IRQ_ELEMENT_SECTION(id) __MK_IRQ_ELEMENT_SECTION(id)
#define __MK_IRQ_ELEMENT_SECTION(id) isr.id

/* Separated macro to create ISR table entry only.
* Used by Z_ISR_DECLARE and ISR tables generation script.
*/
#define _Z_ISR_TABLE_ENTRY(irq, func, param, sect) \
static Z_DECL_ALIGN(struct _isr_table_entry) \
Z_GENERIC_SECTION(sect) \
__used _MK_ISR_ELEMENT_NAME(func, __COUNTER__) = { \
.arg = (const void *)(param), \
.isr = (void (*)(const void *))(void *)(func) \
}

/* Create an entry for _isr_table to be then placed by the linker.
* An instance of struct _isr_list which gets put in the .intList
* section is created with the name of the section where _isr_table entry is placed to be then
* used by isr generation script to create linker script chunk.
*/
#define Z_ISR_DECLARE(irq, flags, func, param) \
BUILD_ASSERT(((flags) & ISR_FLAG_DIRECT) == 0, "Use Z_ISR_DECLARE_DIRECT macro"); \
_Z_ISR_TABLE_ENTRY(irq, func, param, _MK_ISR_ELEMENT_SECTION(irq)); \
static struct _isr_list_sname Z_GENERIC_SECTION(.intList) \
__used _MK_ISR_NAME(func, __COUNTER__) = \
{irq, flags, STRINGIFY(_MK_ISR_ELEMENT_SECTION(irq))}

/* Separated macro to create ISR Direct table entry only.
* Used by Z_ISR_DECLARE_DIRECT and ISR tables generation script.
*/
#define _Z_ISR_DIRECT_TABLE_ENTRY(irq, func, sect) \
COND_CODE_1(CONFIG_IRQ_VECTOR_TABLE_JUMP_BY_ADDRESS, ( \
static Z_DECL_ALIGN(uintptr_t) \
Z_GENERIC_SECTION(sect) \
__used _MK_IRQ_ELEMENT_NAME(func, __LINE__) = ((uintptr_t)(func)); \
), ( \
static void Z_GENERIC_SECTION(_MK_IRQ_ELEMENT_SECTION(irq)) \
__used __attribute__((naked)) _MK_IRQ_ELEMENT_NAME(func, __LINE__) { \
__asm(ARCH_IRQ_VECTOR_JUMP_CODE(func)); \
} \
))

/* Create an entry to irq table and place it in specific section which name is then placed
* in an instance of struct _isr_list to be then used by the isr generation script to create
* the linker script chunks.
*/
#define Z_ISR_DECLARE_DIRECT(irq, flags, func) \
BUILD_ASSERT(IS_ENABLED(CONFIG_IRQ_VECTOR_TABLE_JUMP_BY_ADDRESS) || \
IS_ENABLED(CONFIG_IRQ_VECTOR_TABLE_JUMP_BY_CODE), \
"CONFIG_IRQ_VECTOR_TABLE_JUMP_BY_{ADDRESS,CODE} not set"); \
_Z_ISR_DIRECT_TABLE_ENTRY(irq, func, _MK_IRQ_ELEMENT_SECTION(irq)); \
static struct _isr_list_sname Z_GENERIC_SECTION(.intList) \
__used _MK_ISR_NAME(func, __COUNTER__) = { \
irq, \
ISR_FLAG_DIRECT | (flags), \
STRINGIFY(_MK_IRQ_ELEMENT_SECTION(irq))}


#else /* IS_ENABLED(CONFIG_ISR_TABLES_LOCAL_DECLARATION) */

/* Create an instance of struct _isr_list which gets put in the .intList
* section. This gets consumed by gen_isr_tables.py which creates the vector
* and/or SW ISR tables.
Expand All @@ -99,6 +192,14 @@ extern struct z_shared_isr_table_entry z_shared_sw_isr_table[];
__used _MK_ISR_NAME(func, __COUNTER__) = \
{irq, flags, (void *)&func, (const void *)param}

/* The version of the Z_ISR_DECLARE that should be used for direct ISR declaration.
* It is here for the API match the version with CONFIG_ISR_TABLES_LOCAL_DECLARATION enabled.
*/
#define Z_ISR_DECLARE_DIRECT(irq, flags, func) \
Z_ISR_DECLARE(irq, ISR_FLAG_DIRECT | (flags), func, NULL)

#endif

#define IRQ_TABLE_SIZE (CONFIG_NUM_IRQS - CONFIG_GEN_IRQ_START_VECTOR)

#ifdef CONFIG_DYNAMIC_INTERRUPTS
Expand Down

0 comments on commit e8dfb7a

Please sign in to comment.