2017-03-14 22:16:30 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Jean-Paul Etienne <fractalclone@gmail.com>
|
2018-06-10 19:02:14 +02:00
|
|
|
* Contributors: 2018 Antmicro <www.antmicro.com>
|
2017-03-14 22:16:30 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-24 16:21:29 -05:00
|
|
|
#define DT_DRV_COMPAT sifive_plic_1_0_0
|
|
|
|
|
2017-03-14 22:16:30 +01:00
|
|
|
/**
|
|
|
|
* @brief Platform Level Interrupt Controller (PLIC) driver
|
2018-06-10 19:02:14 +02:00
|
|
|
* for RISC-V processors
|
2017-03-14 22:16:30 +01:00
|
|
|
*/
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/arch/cpu.h>
|
2023-09-15 09:31:28 +00:00
|
|
|
#include <zephyr/device.h>
|
2019-08-08 23:07:08 -05:00
|
|
|
#include <soc.h>
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/sw_isr_table.h>
|
2022-07-08 10:32:44 +02:00
|
|
|
#include <zephyr/drivers/interrupt_controller/riscv_plic.h>
|
2022-10-17 10:24:11 +02:00
|
|
|
#include <zephyr/irq.h>
|
2017-03-14 22:16:30 +01:00
|
|
|
|
2020-03-24 16:21:29 -05:00
|
|
|
#define PLIC_MAX_PRIO DT_INST_PROP(0, riscv_max_priority)
|
2023-05-24 09:50:43 +03:00
|
|
|
#define PLIC_PRIO DT_INST_REG_ADDR_BY_NAME_U64(0, prio)
|
|
|
|
#define PLIC_IRQ_EN DT_INST_REG_ADDR_BY_NAME_U64(0, irq_en)
|
|
|
|
#define PLIC_REG DT_INST_REG_ADDR_BY_NAME_U64(0, reg)
|
2020-02-18 07:55:12 -06:00
|
|
|
|
2019-08-08 23:01:37 -05:00
|
|
|
#define PLIC_IRQS (CONFIG_NUM_IRQS - CONFIG_2ND_LVL_ISR_TBL_OFFSET)
|
2019-08-08 23:07:08 -05:00
|
|
|
#define PLIC_EN_SIZE ((PLIC_IRQS >> 5) + 1)
|
2017-03-14 22:16:30 +01:00
|
|
|
|
2023-09-12 21:24:41 +08:00
|
|
|
#define PLIC_EDGE_TRIG_TYPE (DT_INST_REG_ADDR(0) + DT_INST_PROP(0, riscv_trigger_reg_offset))
|
2023-07-26 09:50:10 -07:00
|
|
|
#define PLIC_EDGE_TRIG_SHIFT 5
|
|
|
|
|
2018-06-10 19:02:14 +02:00
|
|
|
struct plic_regs_t {
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t threshold_prio;
|
|
|
|
uint32_t claim_complete;
|
2017-03-14 22:16:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static int save_irq;
|
|
|
|
|
2023-07-26 09:50:10 -07:00
|
|
|
/**
|
|
|
|
* @brief return edge irq value or zero
|
|
|
|
*
|
|
|
|
* In the event edge irq is enable this will return the trigger
|
|
|
|
* value of the irq. In the event edge irq is not supported this
|
|
|
|
* routine will return 0
|
|
|
|
*
|
|
|
|
* @param irq IRQ number to add to the trigger
|
|
|
|
*
|
|
|
|
* @return irq value when enabled 0 otherwise
|
|
|
|
*/
|
|
|
|
static int riscv_plic_is_edge_irq(uint32_t irq)
|
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_PLIC_SUPPORTS_EDGE_IRQ)) {
|
|
|
|
volatile uint32_t *trig = (volatile uint32_t *)PLIC_EDGE_TRIG_TYPE;
|
|
|
|
|
|
|
|
trig += (irq >> PLIC_EDGE_TRIG_SHIFT);
|
|
|
|
return *trig & BIT(irq);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:16:30 +01:00
|
|
|
/**
|
|
|
|
* @brief Enable a riscv PLIC-specific interrupt line
|
|
|
|
*
|
|
|
|
* This routine enables a RISCV PLIC-specific interrupt line.
|
2023-06-09 10:36:55 +02:00
|
|
|
* riscv_plic_irq_enable is called by SOC_FAMILY_RISCV_PRIVILEGED
|
2019-11-07 12:43:29 -08:00
|
|
|
* arch_irq_enable function to enable external interrupts for
|
2019-08-08 23:01:37 -05:00
|
|
|
* IRQS level == 2, whenever CONFIG_RISCV_HAS_PLIC variable is set.
|
2017-03-14 22:16:30 +01:00
|
|
|
*
|
2022-01-06 16:35:43 -08:00
|
|
|
* @param irq IRQ number to enable
|
2017-03-14 22:16:30 +01:00
|
|
|
*/
|
2020-05-27 11:26:57 -05:00
|
|
|
void riscv_plic_irq_enable(uint32_t irq)
|
2017-03-14 22:16:30 +01:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t key;
|
|
|
|
volatile uint32_t *en = (volatile uint32_t *)PLIC_IRQ_EN;
|
2017-03-14 22:16:30 +01:00
|
|
|
|
|
|
|
key = irq_lock();
|
2019-08-08 23:01:37 -05:00
|
|
|
en += (irq >> 5);
|
|
|
|
*en |= (1 << (irq & 31));
|
2017-03-14 22:16:30 +01:00
|
|
|
irq_unlock(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Disable a riscv PLIC-specific interrupt line
|
|
|
|
*
|
|
|
|
* This routine disables a RISCV PLIC-specific interrupt line.
|
2023-06-09 10:36:55 +02:00
|
|
|
* riscv_plic_irq_disable is called by SOC_FAMILY_RISCV_PRIVILEGED
|
2019-11-07 12:43:29 -08:00
|
|
|
* arch_irq_disable function to disable external interrupts, for
|
2019-08-08 23:01:37 -05:00
|
|
|
* IRQS level == 2, whenever CONFIG_RISCV_HAS_PLIC variable is set.
|
2017-03-14 22:16:30 +01:00
|
|
|
*
|
2022-01-06 16:35:43 -08:00
|
|
|
* @param irq IRQ number to disable
|
2017-03-14 22:16:30 +01:00
|
|
|
*/
|
2020-05-27 11:26:57 -05:00
|
|
|
void riscv_plic_irq_disable(uint32_t irq)
|
2017-03-14 22:16:30 +01:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t key;
|
|
|
|
volatile uint32_t *en = (volatile uint32_t *)PLIC_IRQ_EN;
|
2017-03-14 22:16:30 +01:00
|
|
|
|
|
|
|
key = irq_lock();
|
2019-08-08 23:01:37 -05:00
|
|
|
en += (irq >> 5);
|
|
|
|
*en &= ~(1 << (irq & 31));
|
2017-03-14 22:16:30 +01:00
|
|
|
irq_unlock(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if a riscv PLIC-specific interrupt line is enabled
|
|
|
|
*
|
|
|
|
* This routine checks if a RISCV PLIC-specific interrupt line is enabled.
|
|
|
|
* @param irq IRQ number to check
|
|
|
|
*
|
|
|
|
* @return 1 or 0
|
|
|
|
*/
|
2020-05-27 11:26:57 -05:00
|
|
|
int riscv_plic_irq_is_enabled(uint32_t irq)
|
2017-03-14 22:16:30 +01:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
volatile uint32_t *en = (volatile uint32_t *)PLIC_IRQ_EN;
|
2017-03-14 22:16:30 +01:00
|
|
|
|
2019-08-08 23:01:37 -05:00
|
|
|
en += (irq >> 5);
|
|
|
|
return !!(*en & (1 << (irq & 31)));
|
2017-03-14 22:16:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set priority of a riscv PLIC-specific interrupt line
|
|
|
|
*
|
|
|
|
* This routine set the priority of a RISCV PLIC-specific interrupt line.
|
2019-11-07 12:43:29 -08:00
|
|
|
* riscv_plic_irq_set_prio is called by riscv arch_irq_priority_set to set
|
2017-03-14 22:16:30 +01:00
|
|
|
* the priority of an interrupt whenever CONFIG_RISCV_HAS_PLIC variable is set.
|
|
|
|
*
|
2022-01-06 16:35:43 -08:00
|
|
|
* @param irq IRQ number for which to set priority
|
|
|
|
* @param priority Priority of IRQ to set to
|
2017-03-14 22:16:30 +01:00
|
|
|
*/
|
2020-05-27 11:26:57 -05:00
|
|
|
void riscv_plic_set_priority(uint32_t irq, uint32_t priority)
|
2017-03-14 22:16:30 +01:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
volatile uint32_t *prio = (volatile uint32_t *)PLIC_PRIO;
|
2017-03-14 22:16:30 +01:00
|
|
|
|
2020-02-18 07:55:12 -06:00
|
|
|
if (priority > PLIC_MAX_PRIO)
|
|
|
|
priority = PLIC_MAX_PRIO;
|
2017-03-14 22:16:30 +01:00
|
|
|
|
2019-08-08 23:01:37 -05:00
|
|
|
prio += irq;
|
2017-03-14 22:16:30 +01:00
|
|
|
*prio = priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get riscv PLIC-specific interrupt line causing an interrupt
|
|
|
|
*
|
|
|
|
* This routine returns the RISCV PLIC-specific interrupt line causing an
|
|
|
|
* interrupt.
|
|
|
|
*
|
2022-01-06 16:35:43 -08:00
|
|
|
* @return PLIC-specific interrupt line causing an interrupt.
|
2017-03-14 22:16:30 +01:00
|
|
|
*/
|
|
|
|
int riscv_plic_get_irq(void)
|
|
|
|
{
|
|
|
|
return save_irq;
|
|
|
|
}
|
|
|
|
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
|
|
|
static void plic_irq_handler(const void *arg)
|
2017-03-14 22:16:30 +01:00
|
|
|
{
|
2018-06-10 19:02:14 +02:00
|
|
|
volatile struct plic_regs_t *regs =
|
2020-02-18 07:55:12 -06:00
|
|
|
(volatile struct plic_regs_t *) PLIC_REG;
|
2017-03-14 22:16:30 +01:00
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t irq;
|
2017-03-14 22:16:30 +01:00
|
|
|
struct _isr_table_entry *ite;
|
2023-07-26 09:50:10 -07:00
|
|
|
int edge_irq;
|
2017-03-14 22:16:30 +01:00
|
|
|
|
|
|
|
/* Get the IRQ number generating the interrupt */
|
|
|
|
irq = regs->claim_complete;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save IRQ in save_irq. To be used, if need be, by
|
|
|
|
* subsequent handlers registered in the _sw_isr_table table,
|
|
|
|
* as IRQ number held by the claim_complete register is
|
|
|
|
* cleared upon read.
|
|
|
|
*/
|
|
|
|
save_irq = irq;
|
|
|
|
|
|
|
|
/*
|
2019-03-08 14:19:05 -07:00
|
|
|
* If the IRQ is out of range, call z_irq_spurious.
|
|
|
|
* A call to z_irq_spurious will not return.
|
2017-03-14 22:16:30 +01:00
|
|
|
*/
|
2019-03-26 19:57:45 -06:00
|
|
|
if (irq == 0U || irq >= PLIC_IRQS)
|
2019-03-08 14:19:05 -07:00
|
|
|
z_irq_spurious(NULL);
|
2017-03-14 22:16:30 +01:00
|
|
|
|
2023-07-26 09:50:10 -07:00
|
|
|
edge_irq = riscv_plic_is_edge_irq(irq);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For edge triggered interrupts, write to the claim_complete register
|
|
|
|
* to indicate to the PLIC controller that the IRQ has been handled
|
|
|
|
* for edge triggered interrupts.
|
|
|
|
*/
|
|
|
|
if (edge_irq)
|
|
|
|
regs->claim_complete = save_irq;
|
|
|
|
|
2019-08-08 23:01:37 -05:00
|
|
|
irq += CONFIG_2ND_LVL_ISR_TBL_OFFSET;
|
2017-03-14 22:16:30 +01:00
|
|
|
|
|
|
|
/* Call the corresponding IRQ handler in _sw_isr_table */
|
|
|
|
ite = (struct _isr_table_entry *)&_sw_isr_table[irq];
|
|
|
|
ite->isr(ite->arg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write to claim_complete register to indicate to
|
2023-07-26 09:50:10 -07:00
|
|
|
* PLIC controller that the IRQ has been handled
|
|
|
|
* for level triggered interrupts.
|
2017-03-14 22:16:30 +01:00
|
|
|
*/
|
2023-07-26 09:50:10 -07:00
|
|
|
if (!edge_irq)
|
|
|
|
regs->claim_complete = save_irq;
|
2017-03-14 22:16:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-10 19:02:14 +02:00
|
|
|
* @brief Initialize the Platform Level Interrupt Controller
|
2022-01-06 16:35:43 -08:00
|
|
|
*
|
|
|
|
* @retval 0 on success.
|
2017-03-14 22:16:30 +01:00
|
|
|
*/
|
2023-09-15 09:31:28 +00:00
|
|
|
static int plic_init(const struct device *dev)
|
2017-03-14 22:16:30 +01:00
|
|
|
{
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
volatile uint32_t *en = (volatile uint32_t *)PLIC_IRQ_EN;
|
|
|
|
volatile uint32_t *prio = (volatile uint32_t *)PLIC_PRIO;
|
2018-06-10 19:02:14 +02:00
|
|
|
volatile struct plic_regs_t *regs =
|
2020-02-18 07:55:12 -06:00
|
|
|
(volatile struct plic_regs_t *)PLIC_REG;
|
2017-03-14 22:16:30 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Ensure that all interrupts are disabled initially */
|
2018-06-10 19:02:14 +02:00
|
|
|
for (i = 0; i < PLIC_EN_SIZE; i++) {
|
2018-11-29 11:12:22 -08:00
|
|
|
*en = 0U;
|
2017-03-14 22:16:30 +01:00
|
|
|
en++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set priority of each interrupt line to 0 initially */
|
2018-06-10 19:02:14 +02:00
|
|
|
for (i = 0; i < PLIC_IRQS; i++) {
|
2018-11-29 11:12:22 -08:00
|
|
|
*prio = 0U;
|
2017-03-14 22:16:30 +01:00
|
|
|
prio++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set threshold priority to 0 */
|
2018-11-29 11:12:22 -08:00
|
|
|
regs->threshold_prio = 0U;
|
2017-03-14 22:16:30 +01:00
|
|
|
|
|
|
|
/* Setup IRQ handler for PLIC driver */
|
|
|
|
IRQ_CONNECT(RISCV_MACHINE_EXT_IRQ,
|
|
|
|
0,
|
2018-06-10 19:02:14 +02:00
|
|
|
plic_irq_handler,
|
2017-03-14 22:16:30 +01:00
|
|
|
NULL,
|
|
|
|
0);
|
|
|
|
|
|
|
|
/* Enable IRQ for PLIC driver */
|
|
|
|
irq_enable(RISCV_MACHINE_EXT_IRQ);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-15 09:31:28 +00:00
|
|
|
DEVICE_DT_INST_DEFINE(0, plic_init, NULL, NULL, NULL,
|
|
|
|
PRE_KERNEL_1, CONFIG_INTC_INIT_PRIORITY, NULL);
|