aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSamuel Holland <samuel.holland@sifive.com>2024-11-04 20:10:05 -0800
committerAnup Patel <anup@brainfault.org>2024-11-28 11:28:38 +0530
commitc6c22f00f457264560ca05414e9f9e0c5c3438db (patch)
tree7a9cf27f385ae24cb1ac1ce5a007c12e0e6c7481 /lib
parent69448a07906523c9c798c8f1d02a7be52e96b38e (diff)
downloadopensbi-c6c22f00f457264560ca05414e9f9e0c5c3438db.zip
opensbi-c6c22f00f457264560ca05414e9f9e0c5c3438db.tar.gz
opensbi-c6c22f00f457264560ca05414e9f9e0c5c3438db.tar.bz2
lib: utils/irqchip: plic: Common PM save/restore
Move the PLIC save/restore functions inside the driver, so they can be reused on any platform that needs them. The memory needed to store the PLIC context is also allocated by the driver. The PM data cannot be completely encapsulated, as some platforms (including Allwinner D1) need to program the IRQ enable status to a sideband interrupt controller for wakeup capability. Signed-off-by: Samuel Holland <samuel.holland@sifive.com> Reviewed-by: Anup Patel <anup@brainfault.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/utils/irqchip/fdt_irqchip_plic.c32
-rw-r--r--lib/utils/irqchip/plic.c134
2 files changed, 95 insertions, 71 deletions
diff --git a/lib/utils/irqchip/fdt_irqchip_plic.c b/lib/utils/irqchip/fdt_irqchip_plic.c
index 2ba5674..3826d2a 100644
--- a/lib/utils/irqchip/fdt_irqchip_plic.c
+++ b/lib/utils/irqchip/fdt_irqchip_plic.c
@@ -26,35 +26,25 @@ static unsigned long plic_ptr_offset;
#define plic_set_hart_data_ptr(__scratch, __plic) \
sbi_scratch_write_type((__scratch), void *, plic_ptr_offset, (__plic))
-void fdt_plic_priority_save(u8 *priority, u32 num)
+struct plic_data *fdt_plic_get(void)
{
struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
- plic_priority_save(plic_get_hart_data_ptr(scratch), priority, num);
+ return plic_get_hart_data_ptr(scratch);
}
-void fdt_plic_priority_restore(const u8 *priority, u32 num)
+void fdt_plic_suspend(void)
{
struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
- plic_priority_restore(plic_get_hart_data_ptr(scratch), priority, num);
+ plic_suspend(plic_get_hart_data_ptr(scratch));
}
-void fdt_plic_context_save(bool smode, u32 *enable, u32 *threshold, u32 num)
+void fdt_plic_resume(void)
{
struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
- plic_context_save(plic_get_hart_data_ptr(scratch), smode,
- enable, threshold, num);
-}
-
-void fdt_plic_context_restore(bool smode, const u32 *enable, u32 threshold,
- u32 num)
-{
- struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
-
- plic_context_restore(plic_get_hart_data_ptr(scratch), smode,
- enable, threshold, num);
+ plic_resume(plic_get_hart_data_ptr(scratch));
}
static int irqchip_plic_warm_init(void)
@@ -151,20 +141,12 @@ fail_free_data:
return rc;
}
-void thead_plic_restore(void)
-{
- struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
- struct plic_data *plic = plic_get_hart_data_ptr(scratch);
-
- plic_delegate(plic);
-}
-
static const struct fdt_match irqchip_plic_match[] = {
{ .compatible = "andestech,nceplic100" },
{ .compatible = "riscv,plic0" },
{ .compatible = "sifive,plic-1.0.0" },
{ .compatible = "thead,c900-plic",
- .data = (void *)PLIC_FLAG_THEAD_DELEGATION },
+ .data = (void *)(PLIC_FLAG_THEAD_DELEGATION | PLIC_FLAG_ENABLE_PM) },
{ /* sentinel */ }
};
diff --git a/lib/utils/irqchip/plic.c b/lib/utils/irqchip/plic.c
index c8ea884..ab58e39 100644
--- a/lib/utils/irqchip/plic.c
+++ b/lib/utils/irqchip/plic.c
@@ -14,6 +14,7 @@
#include <sbi/sbi_console.h>
#include <sbi/sbi_domain.h>
#include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
#include <sbi/sbi_string.h>
#include <sbi_utils/irqchip/plic.h>
@@ -40,19 +41,6 @@ static void plic_set_priority(const struct plic_data *plic, u32 source, u32 val)
writel(val, plic_priority);
}
-void plic_priority_save(const struct plic_data *plic, u8 *priority, u32 num)
-{
- for (u32 i = 1; i <= num; i++)
- priority[i] = plic_get_priority(plic, i);
-}
-
-void plic_priority_restore(const struct plic_data *plic, const u8 *priority,
- u32 num)
-{
- for (u32 i = 1; i <= num; i++)
- plic_set_priority(plic, i, priority[i]);
-}
-
static u32 plic_get_thresh(const struct plic_data *plic, u32 cntxid)
{
volatile void *plic_thresh;
@@ -95,62 +83,91 @@ static void plic_set_ie(const struct plic_data *plic, u32 cntxid,
writel(val, plic_ie);
}
-void plic_delegate(const struct plic_data *plic)
+static void plic_delegate(const struct plic_data *plic)
{
/* If this is a T-HEAD PLIC, delegate access to S-mode */
if (plic->flags & PLIC_FLAG_THEAD_DELEGATION)
writel_relaxed(BIT(0), (char *)plic->addr + THEAD_PLIC_CTRL_REG);
}
-void plic_context_save(const struct plic_data *plic, bool smode,
- u32 *enable, u32 *threshold, u32 num)
+static int plic_context_init(const struct plic_data *plic, int context_id,
+ bool enable, u32 threshold)
{
- u32 hartindex = current_hartindex();
- s16 context_id = plic->context_map[hartindex][smode];
- u32 ie_words = plic->num_src / 32 + 1;
+ u32 ie_words, ie_value;
- if (num > ie_words)
- num = ie_words;
+ if (!plic || context_id < 0)
+ return SBI_EINVAL;
- for (u32 i = 0; i < num; i++)
- enable[i] = plic_get_ie(plic, context_id, i);
+ ie_words = PLIC_IE_WORDS(plic);
+ ie_value = enable ? 0xffffffffU : 0U;
+
+ for (u32 i = 0; i < ie_words; i++)
+ plic_set_ie(plic, context_id, i, ie_value);
+
+ plic_set_thresh(plic, context_id, threshold);
- *threshold = plic_get_thresh(plic, context_id);
+ return 0;
}
-void plic_context_restore(const struct plic_data *plic, bool smode,
- const u32 *enable, u32 threshold, u32 num)
+void plic_suspend(const struct plic_data *plic)
{
- u32 hartindex = current_hartindex();
- s16 context_id = plic->context_map[hartindex][smode];
- u32 ie_words = plic->num_src / 32 + 1;
+ u32 ie_words = PLIC_IE_WORDS(plic);
+ u32 *data_word = plic->pm_data;
+ u8 *data_byte;
- if (num > ie_words)
- num = ie_words;
+ if (!data_word)
+ return;
- for (u32 i = 0; i < num; i++)
- plic_set_ie(plic, context_id, i, enable[i]);
+ for (u32 h = 0; h <= sbi_scratch_last_hartindex(); h++) {
+ u32 context_id = plic->context_map[h][PLIC_S_CONTEXT];
- plic_set_thresh(plic, context_id, threshold);
+ if (context_id < 0)
+ continue;
+
+ /* Save the enable bits */
+ for (u32 i = 0; i < ie_words; i++)
+ *data_word++ = plic_get_ie(plic, context_id, i);
+
+ /* Save the context threshold */
+ *data_word++ = plic_get_thresh(plic, context_id);
+ }
+
+ /* Restore the input priorities */
+ data_byte = (u8 *)data_word;
+ for (u32 i = 1; i <= plic->num_src; i++)
+ *data_byte++ = plic_get_priority(plic, i);
}
-static int plic_context_init(const struct plic_data *plic, int context_id,
- bool enable, u32 threshold)
+void plic_resume(const struct plic_data *plic)
{
- u32 ie_words, ie_value;
+ u32 ie_words = PLIC_IE_WORDS(plic);
+ u32 *data_word = plic->pm_data;
+ u8 *data_byte;
- if (!plic || context_id < 0)
- return SBI_EINVAL;
+ if (!data_word)
+ return;
- ie_words = plic->num_src / 32 + 1;
- ie_value = enable ? 0xffffffffU : 0U;
+ for (u32 h = 0; h <= sbi_scratch_last_hartindex(); h++) {
+ u32 context_id = plic->context_map[h][PLIC_S_CONTEXT];
- for (u32 i = 0; i < ie_words; i++)
- plic_set_ie(plic, context_id, i, ie_value);
+ if (context_id < 0)
+ continue;
- plic_set_thresh(plic, context_id, threshold);
+ /* Restore the enable bits */
+ for (u32 i = 0; i < ie_words; i++)
+ plic_set_ie(plic, context_id, i, *data_word++);
- return 0;
+ /* Restore the context threshold */
+ plic_set_thresh(plic, context_id, *data_word++);
+ }
+
+ /* Restore the input priorities */
+ data_byte = (u8 *)data_word;
+ for (u32 i = 1; i <= plic->num_src; i++)
+ plic_set_priority(plic, i, *data_byte++);
+
+ /* Restore the delegation */
+ plic_delegate(plic);
}
int plic_warm_irqchip_init(const struct plic_data *plic)
@@ -182,13 +199,38 @@ int plic_warm_irqchip_init(const struct plic_data *plic)
return 0;
}
-int plic_cold_irqchip_init(const struct plic_data *plic)
+int plic_cold_irqchip_init(struct plic_data *plic)
{
int i;
if (!plic)
return SBI_EINVAL;
+ if (plic->flags & PLIC_FLAG_ENABLE_PM) {
+ unsigned long data_size = 0;
+
+ for (u32 i = 0; i <= sbi_scratch_last_hartindex(); i++) {
+ if (plic->context_map[i][PLIC_S_CONTEXT] < 0)
+ continue;
+
+ /* Allocate space for enable bits */
+ data_size += (plic->num_src / 32 + 1) * sizeof(u32);
+
+ /* Allocate space for the context threshold */
+ data_size += sizeof(u32);
+ }
+
+ /*
+ * Allocate space for the input priorities. So far,
+ * priorities on all known implementations fit in 8 bits.
+ */
+ data_size += plic->num_src * sizeof(u8);
+
+ plic->pm_data = sbi_malloc(data_size);
+ if (!plic->pm_data)
+ return SBI_ENOMEM;
+ }
+
/* Configure default priorities of all IRQs */
for (i = 1; i <= plic->num_src; i++)
plic_set_priority(plic, i, 0);