2017-02-09 09:16:29 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright (c) 2017 Intel Corporation
|
2018-11-25 14:43:06 +08:00
|
|
|
# Copyright (c) 2018 Foundries.io
|
|
|
|
#
|
2017-02-09 09:16:29 +08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import struct
|
|
|
|
import sys
|
|
|
|
import os
|
2017-12-24 17:48:57 +08:00
|
|
|
from elftools.elf.elffile import ELFFile
|
|
|
|
from elftools.elf.sections import SymbolTableSection
|
2017-02-09 09:16:29 +08:00
|
|
|
|
|
|
|
ISR_FLAG_DIRECT = (1 << 0)
|
|
|
|
|
2017-12-24 17:48:57 +08:00
|
|
|
# The below few hardware independent magic numbers represent various
|
|
|
|
# levels of interrupts in a multi-level interrupt system.
|
|
|
|
# 0x000000FF - represents the 1st level (i.e. the interrupts
|
|
|
|
# that directly go to the processor).
|
|
|
|
# 0x0000FF00 - represents the 2nd level (i.e. the interrupts funnel
|
|
|
|
# into 1 line which then goes into the 1st level)
|
|
|
|
# 0x00FF0000 - represents the 3rd level (i.e. the interrupts funnel
|
|
|
|
# into 1 line which then goes into the 2nd level)
|
|
|
|
FIRST_LVL_INTERRUPTS = 0x000000FF
|
|
|
|
SECND_LVL_INTERRUPTS = 0x0000FF00
|
|
|
|
THIRD_LVL_INTERRUPTS = 0x00FF0000
|
|
|
|
|
2017-02-09 09:16:29 +08:00
|
|
|
def debug(text):
|
2018-01-04 21:07:03 +08:00
|
|
|
if args.debug:
|
|
|
|
sys.stdout.write(os.path.basename(sys.argv[0]) + ": " + text + "\n")
|
2017-02-09 09:16:29 +08:00
|
|
|
|
|
|
|
def error(text):
|
2020-04-15 12:24:35 +08:00
|
|
|
sys.exit(os.path.basename(sys.argv[0]) + ": error: " + text + "\n")
|
2017-02-09 09:16:29 +08:00
|
|
|
|
|
|
|
def endian_prefix():
|
|
|
|
if args.big_endian:
|
|
|
|
return ">"
|
|
|
|
else:
|
|
|
|
return "<"
|
|
|
|
|
2019-05-18 03:15:24 +08:00
|
|
|
def read_intlist(intlist_path, syms):
|
2017-02-09 09:16:29 +08:00
|
|
|
"""read a binary file containing the contents of the kernel's .intList
|
|
|
|
section. This is an instance of a header created by
|
2022-05-09 19:52:12 +08:00
|
|
|
include/zephyr/linker/intlist.ld:
|
2017-02-09 09:16:29 +08:00
|
|
|
|
|
|
|
struct {
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t num_vectors; <- typically CONFIG_NUM_IRQS
|
2018-06-25 22:46:13 +08:00
|
|
|
struct _isr_list isrs[]; <- Usually of smaller size than num_vectors
|
2017-02-09 09:16:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Followed by instances of struct _isr_list created by IRQ_CONNECT()
|
|
|
|
calls:
|
|
|
|
|
|
|
|
struct _isr_list {
|
|
|
|
/** IRQ line number */
|
2020-05-28 00:26:57 +08:00
|
|
|
int32_t irq;
|
2017-02-09 09:16:29 +08:00
|
|
|
/** Flags for this IRQ, see ISR_FLAG_* definitions */
|
2020-05-28 00:26:57 +08:00
|
|
|
int32_t flags;
|
2017-02-09 09:16:29 +08:00
|
|
|
/** ISR to call */
|
|
|
|
void *func;
|
|
|
|
/** Parameter for non-direct IRQs */
|
2020-07-10 20:17:20 +08:00
|
|
|
const void *param;
|
2017-02-09 09:16:29 +08:00
|
|
|
};
|
|
|
|
"""
|
|
|
|
|
|
|
|
intlist = {}
|
|
|
|
|
|
|
|
prefix = endian_prefix()
|
|
|
|
|
2018-06-25 22:46:13 +08:00
|
|
|
intlist_header_fmt = prefix + "II"
|
2019-05-18 03:15:24 +08:00
|
|
|
if "CONFIG_64BIT" in syms:
|
|
|
|
intlist_entry_fmt = prefix + "iiQQ"
|
|
|
|
else:
|
|
|
|
intlist_entry_fmt = prefix + "iiII"
|
2017-02-09 09:16:29 +08:00
|
|
|
|
|
|
|
with open(intlist_path, "rb") as fp:
|
|
|
|
intdata = fp.read()
|
|
|
|
|
|
|
|
header_sz = struct.calcsize(intlist_header_fmt)
|
|
|
|
header = struct.unpack_from(intlist_header_fmt, intdata, 0)
|
|
|
|
intdata = intdata[header_sz:]
|
|
|
|
|
|
|
|
debug(str(header))
|
|
|
|
|
2018-05-16 20:08:47 +08:00
|
|
|
intlist["num_vectors"] = header[0]
|
|
|
|
intlist["offset"] = header[1]
|
2017-02-09 09:16:29 +08:00
|
|
|
|
|
|
|
intlist["interrupts"] = [i for i in
|
|
|
|
struct.iter_unpack(intlist_entry_fmt, intdata)]
|
|
|
|
|
|
|
|
debug("Configured interrupt routing")
|
|
|
|
debug("handler irq flags param")
|
|
|
|
debug("--------------------------")
|
|
|
|
|
|
|
|
for irq in intlist["interrupts"]:
|
|
|
|
debug("{0:<10} {1:<3} {2:<3} {3}".format(
|
|
|
|
hex(irq[2]), irq[0], irq[1], hex(irq[3])))
|
|
|
|
|
|
|
|
return intlist
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
global args
|
|
|
|
|
2019-09-06 17:13:19 +08:00
|
|
|
parser = argparse.ArgumentParser(description=__doc__,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
2017-02-09 09:16:29 +08:00
|
|
|
|
|
|
|
parser.add_argument("-e", "--big-endian", action="store_true",
|
|
|
|
help="Target encodes data in big-endian format (little endian is "
|
|
|
|
"the default)")
|
|
|
|
parser.add_argument("-d", "--debug", action="store_true",
|
|
|
|
help="Print additional debugging information")
|
|
|
|
parser.add_argument("-o", "--output-source", required=True,
|
|
|
|
help="Output source file")
|
2017-12-24 17:48:57 +08:00
|
|
|
parser.add_argument("-k", "--kernel", required=True,
|
|
|
|
help="Zephyr kernel image")
|
2017-02-09 09:16:29 +08:00
|
|
|
parser.add_argument("-s", "--sw-isr-table", action="store_true",
|
|
|
|
help="Generate SW ISR table")
|
|
|
|
parser.add_argument("-V", "--vector-table", action="store_true",
|
|
|
|
help="Generate vector table")
|
|
|
|
parser.add_argument("-i", "--intlist", required=True,
|
|
|
|
help="Zephyr intlist binary for intList extraction")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
source_header = """
|
|
|
|
/* AUTO-GENERATED by gen_isr_tables.py, do not edit! */
|
|
|
|
|
2022-05-09 19:52:12 +08:00
|
|
|
#include <zephyr/toolchain.h>
|
|
|
|
#include <zephyr/linker/sections.h>
|
|
|
|
#include <zephyr/sw_isr_table.h>
|
|
|
|
#include <zephyr/arch/cpu.h>
|
2017-02-09 09:16:29 +08:00
|
|
|
|
2020-09-30 16:36:26 +08:00
|
|
|
typedef void (* ISR)(const void *);
|
2017-02-09 09:16:29 +08:00
|
|
|
"""
|
|
|
|
|
2018-11-25 14:43:06 +08:00
|
|
|
def write_source_file(fp, vt, swt, intlist, syms):
|
2017-02-09 09:16:29 +08:00
|
|
|
fp.write(source_header)
|
|
|
|
|
|
|
|
nv = intlist["num_vectors"]
|
|
|
|
|
|
|
|
if vt:
|
2020-12-25 04:24:32 +08:00
|
|
|
fp.write("uintptr_t __irq_vector_table _irq_vector_table[%d] = {\n" % nv)
|
2017-02-09 09:16:29 +08:00
|
|
|
for i in range(nv):
|
2018-05-16 20:08:47 +08:00
|
|
|
fp.write("\t{},\n".format(vt[i]))
|
2017-02-09 09:16:29 +08:00
|
|
|
fp.write("};\n")
|
|
|
|
|
|
|
|
if not swt:
|
|
|
|
return
|
|
|
|
|
|
|
|
fp.write("struct _isr_table_entry __sw_isr_table _sw_isr_table[%d] = {\n"
|
|
|
|
% nv)
|
2018-11-25 14:43:06 +08:00
|
|
|
|
|
|
|
level2_offset = syms.get("CONFIG_2ND_LVL_ISR_TBL_OFFSET")
|
|
|
|
level3_offset = syms.get("CONFIG_3RD_LVL_ISR_TBL_OFFSET")
|
|
|
|
|
2017-02-09 09:16:29 +08:00
|
|
|
for i in range(nv):
|
|
|
|
param, func = swt[i]
|
2019-09-02 17:39:28 +08:00
|
|
|
if isinstance(func, int):
|
2018-05-09 18:00:24 +08:00
|
|
|
func_as_string = "{0:#x}".format(func)
|
|
|
|
else:
|
|
|
|
func_as_string = func
|
|
|
|
|
2018-11-25 14:43:06 +08:00
|
|
|
if level2_offset is not None and i == level2_offset:
|
|
|
|
fp.write("\t/* Level 2 interrupts start here (offset: {}) */\n".
|
|
|
|
format(level2_offset))
|
|
|
|
if level3_offset is not None and i == level3_offset:
|
|
|
|
fp.write("\t/* Level 3 interrupts start here (offset: {}) */\n".
|
|
|
|
format(level3_offset))
|
|
|
|
|
2020-09-30 16:36:26 +08:00
|
|
|
fp.write("\t{{(const void *){0:#x}, (ISR){1}}},\n".format(param, func_as_string))
|
2017-02-09 09:16:29 +08:00
|
|
|
fp.write("};\n")
|
|
|
|
|
2017-12-24 17:48:57 +08:00
|
|
|
def get_symbols(obj):
|
|
|
|
for section in obj.iter_sections():
|
|
|
|
if isinstance(section, SymbolTableSection):
|
|
|
|
return {sym.name: sym.entry.st_value
|
|
|
|
for sym in section.iter_symbols()}
|
|
|
|
|
|
|
|
error("Could not find symbol table")
|
|
|
|
|
|
|
|
def getindex(irq, irq_aggregator_pos):
|
2018-11-25 14:43:06 +08:00
|
|
|
try:
|
|
|
|
return irq_aggregator_pos.index(irq)
|
|
|
|
except ValueError:
|
|
|
|
error("IRQ {} not present in parent offsets ({}). ".
|
|
|
|
format(irq, irq_aggregator_pos) +
|
|
|
|
" Recheck interrupt configuration.")
|
2017-12-24 17:48:57 +08:00
|
|
|
|
2017-02-09 09:16:29 +08:00
|
|
|
def main():
|
|
|
|
parse_args()
|
|
|
|
|
2017-12-24 17:48:57 +08:00
|
|
|
with open(args.kernel, "rb") as fp:
|
|
|
|
kernel = ELFFile(fp)
|
|
|
|
syms = get_symbols(kernel)
|
|
|
|
|
|
|
|
if "CONFIG_MULTI_LEVEL_INTERRUPTS" in syms:
|
2018-11-25 14:43:06 +08:00
|
|
|
max_irq_per = syms["CONFIG_MAX_IRQ_PER_AGGREGATOR"]
|
|
|
|
|
2017-12-24 17:48:57 +08:00
|
|
|
if "CONFIG_2ND_LEVEL_INTERRUPTS" in syms:
|
2018-11-25 14:43:06 +08:00
|
|
|
num_aggregators = syms["CONFIG_NUM_2ND_LEVEL_AGGREGATORS"]
|
|
|
|
irq2_baseoffset = syms["CONFIG_2ND_LVL_ISR_TBL_OFFSET"]
|
|
|
|
list_2nd_lvl_offsets = [syms['CONFIG_2ND_LVL_INTR_{}_OFFSET'.
|
|
|
|
format(str(i).zfill(2))] for i in
|
|
|
|
range(num_aggregators)]
|
2017-12-24 17:48:57 +08:00
|
|
|
|
2018-11-25 14:43:06 +08:00
|
|
|
debug('2nd level offsets: {}'.format(list_2nd_lvl_offsets))
|
2017-12-24 17:48:57 +08:00
|
|
|
|
2018-11-15 03:06:50 +08:00
|
|
|
if "CONFIG_3RD_LEVEL_INTERRUPTS" in syms:
|
2018-11-25 14:43:06 +08:00
|
|
|
num_aggregators = syms["CONFIG_NUM_3RD_LEVEL_AGGREGATORS"]
|
|
|
|
irq3_baseoffset = syms["CONFIG_3RD_LVL_ISR_TBL_OFFSET"]
|
2019-01-26 01:44:41 +08:00
|
|
|
list_3rd_lvl_offsets = [syms['CONFIG_3RD_LVL_INTR_{}_OFFSET'.
|
2018-11-25 14:43:06 +08:00
|
|
|
format(str(i).zfill(2))] for i in
|
|
|
|
range(num_aggregators)]
|
2017-12-24 17:48:57 +08:00
|
|
|
|
2018-11-25 14:43:06 +08:00
|
|
|
debug('3rd level offsets: {}'.format(list_3rd_lvl_offsets))
|
2017-12-24 17:48:57 +08:00
|
|
|
|
2019-05-18 03:15:24 +08:00
|
|
|
intlist = read_intlist(args.intlist, syms)
|
2017-02-09 09:16:29 +08:00
|
|
|
nvec = intlist["num_vectors"]
|
2017-02-14 01:56:25 +08:00
|
|
|
offset = intlist["offset"]
|
2017-12-24 17:48:57 +08:00
|
|
|
|
2019-06-15 00:57:35 +08:00
|
|
|
if nvec > pow(2, 15):
|
|
|
|
raise ValueError('nvec is too large, check endianness.')
|
|
|
|
|
2022-06-23 19:23:46 +08:00
|
|
|
spurious_handler = "((uintptr_t)&z_irq_spurious)"
|
|
|
|
sw_irq_handler = "((uintptr_t)&_isr_wrapper)"
|
2018-05-09 18:00:24 +08:00
|
|
|
|
2017-12-24 17:48:57 +08:00
|
|
|
debug('offset is ' + str(offset))
|
|
|
|
debug('num_vectors is ' + str(nvec))
|
2017-02-09 09:16:29 +08:00
|
|
|
|
|
|
|
# Set default entries in both tables
|
|
|
|
if args.sw_isr_table:
|
|
|
|
# All vectors just jump to the common sw_irq_handler. If some entries
|
|
|
|
# are used for direct interrupts, they will be replaced later.
|
|
|
|
if args.vector_table:
|
2018-05-16 20:08:47 +08:00
|
|
|
vt = [sw_irq_handler for i in range(nvec)]
|
2017-02-09 09:16:29 +08:00
|
|
|
else:
|
|
|
|
vt = None
|
|
|
|
# Default to spurious interrupt handler. Configured interrupts
|
|
|
|
# will replace these entries.
|
2018-05-09 18:00:24 +08:00
|
|
|
swt = [(0, spurious_handler) for i in range(nvec)]
|
2017-02-09 09:16:29 +08:00
|
|
|
else:
|
|
|
|
if args.vector_table:
|
2018-05-09 18:00:24 +08:00
|
|
|
vt = [spurious_handler for i in range(nvec)]
|
2017-02-09 09:16:29 +08:00
|
|
|
else:
|
|
|
|
error("one or both of -s or -V needs to be specified on command line")
|
|
|
|
swt = None
|
|
|
|
|
|
|
|
for irq, flags, func, param in intlist["interrupts"]:
|
2019-03-21 02:30:29 +08:00
|
|
|
if flags & ISR_FLAG_DIRECT:
|
|
|
|
if param != 0:
|
2017-02-09 09:16:29 +08:00
|
|
|
error("Direct irq %d declared, but has non-NULL parameter"
|
|
|
|
% irq)
|
2020-11-08 03:12:08 +08:00
|
|
|
if not 0 <= irq - offset < len(vt):
|
|
|
|
error("IRQ %d (offset=%d) exceeds the maximum of %d" %
|
|
|
|
(irq - offset, offset, len(vt) - 1))
|
2017-02-14 01:56:25 +08:00
|
|
|
vt[irq - offset] = func
|
2017-02-09 09:16:29 +08:00
|
|
|
else:
|
|
|
|
# Regular interrupt
|
|
|
|
if not swt:
|
|
|
|
error("Regular Interrupt %d declared with parameter 0x%x "
|
|
|
|
"but no SW ISR_TABLE in use"
|
|
|
|
% (irq, param))
|
2017-12-24 17:48:57 +08:00
|
|
|
|
|
|
|
if not "CONFIG_MULTI_LEVEL_INTERRUPTS" in syms:
|
2018-11-10 05:23:33 +08:00
|
|
|
table_index = irq - offset
|
2017-12-24 17:48:57 +08:00
|
|
|
else:
|
|
|
|
# Figure out third level interrupt position
|
|
|
|
debug('IRQ = ' + hex(irq))
|
|
|
|
irq3 = (irq & THIRD_LVL_INTERRUPTS) >> 16
|
2018-11-25 14:43:06 +08:00
|
|
|
irq2 = (irq & SECND_LVL_INTERRUPTS) >> 8
|
|
|
|
irq1 = (irq & FIRST_LVL_INTERRUPTS)
|
|
|
|
|
2017-12-24 17:48:57 +08:00
|
|
|
if irq3:
|
2018-11-25 14:43:06 +08:00
|
|
|
irq_parent = irq2
|
2017-12-24 17:48:57 +08:00
|
|
|
list_index = getindex(irq_parent, list_3rd_lvl_offsets)
|
2018-11-15 03:19:40 +08:00
|
|
|
irq3_pos = irq3_baseoffset + max_irq_per*list_index + irq3 - 1
|
2018-11-25 14:43:06 +08:00
|
|
|
debug('IRQ_level = 3')
|
2017-12-24 17:48:57 +08:00
|
|
|
debug('IRQ_Indx = ' + str(irq3))
|
|
|
|
debug('IRQ_Pos = ' + str(irq3_pos))
|
2018-11-10 05:23:33 +08:00
|
|
|
table_index = irq3_pos - offset
|
2017-12-24 17:48:57 +08:00
|
|
|
|
|
|
|
# Figure out second level interrupt position
|
2019-02-06 15:30:00 +08:00
|
|
|
elif irq2:
|
2018-11-25 14:43:06 +08:00
|
|
|
irq_parent = irq1
|
|
|
|
list_index = getindex(irq_parent, list_2nd_lvl_offsets)
|
|
|
|
irq2_pos = irq2_baseoffset + max_irq_per*list_index + irq2 - 1
|
|
|
|
debug('IRQ_level = 2')
|
|
|
|
debug('IRQ_Indx = ' + str(irq2))
|
|
|
|
debug('IRQ_Pos = ' + str(irq2_pos))
|
2018-11-10 05:23:33 +08:00
|
|
|
table_index = irq2_pos - offset
|
2017-12-24 17:48:57 +08:00
|
|
|
|
|
|
|
# Figure out first level interrupt position
|
2019-02-06 15:30:00 +08:00
|
|
|
else:
|
2018-11-25 14:43:06 +08:00
|
|
|
debug('IRQ_level = 1')
|
2017-12-24 17:48:57 +08:00
|
|
|
debug('IRQ_Indx = ' + str(irq1))
|
|
|
|
debug('IRQ_Pos = ' + str(irq1))
|
2018-11-10 05:23:33 +08:00
|
|
|
table_index = irq1 - offset
|
|
|
|
|
2020-11-08 03:12:08 +08:00
|
|
|
if not 0 <= table_index < len(swt):
|
|
|
|
error("IRQ %d (offset=%d) exceeds the maximum of %d" %
|
|
|
|
(table_index, offset, len(swt) - 1))
|
2018-11-10 05:23:33 +08:00
|
|
|
if swt[table_index] != (0, spurious_handler):
|
2020-01-29 17:41:47 +08:00
|
|
|
error(f"multiple registrations at table_index {table_index} for irq {irq} (0x{irq:x})"
|
2020-04-15 12:24:35 +08:00
|
|
|
+ f"\nExisting handler 0x{swt[table_index][1]:x}, new handler 0x{func:x}"
|
2020-01-29 17:41:47 +08:00
|
|
|
+ "\nHas IRQ_CONNECT or IRQ_DIRECT_CONNECT accidentally been invoked on the same irq multiple times?"
|
|
|
|
)
|
2018-11-10 05:23:33 +08:00
|
|
|
|
|
|
|
swt[table_index] = (param, func)
|
2017-02-09 09:16:29 +08:00
|
|
|
|
|
|
|
with open(args.output_source, "w") as fp:
|
2018-11-25 14:43:06 +08:00
|
|
|
write_source_file(fp, vt, swt, intlist, syms)
|
2017-02-09 09:16:29 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|