Skip to content

Commit 13abda5

Browse files
icebergfuavpatel
authored andcommitted
lib: sbi_platform: Add platform specific pmp_set() and pmp_disable()
Allow platforms to implement platform specific PMP setup and PMP disable functions which are called before actual PMP CSRs are configured. Also, implement pmp_set() and pmp_disable() for MIPS P8700. Signed-off-by: Chao-ying Fu <[email protected]> Signed-off-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
1 parent 3240214 commit 13abda5

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

include/sbi/sbi_platform.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ struct sbi_platform_operations {
142142
/** platform specific handler to fixup store fault */
143143
int (*emulate_store)(int wlen, unsigned long addr,
144144
union sbi_ldst_data in_val);
145+
146+
/** platform specific pmp setup on current HART */
147+
void (*pmp_set)(unsigned int n, unsigned long flags,
148+
unsigned long prot, unsigned long addr,
149+
unsigned long log2len);
150+
/** platform specific pmp disable on current HART */
151+
void (*pmp_disable)(unsigned int n);
145152
};
146153

147154
/** Platform default per-HART stack size for exception/interrupt handling */
@@ -644,6 +651,38 @@ static inline int sbi_platform_emulate_store(const struct sbi_platform *plat,
644651
return SBI_ENOTSUPP;
645652
}
646653

654+
/**
655+
* Platform specific PMP setup on current HART
656+
*
657+
* @param plat pointer to struct sbi_platform
658+
* @param n index of the pmp entry
659+
* @param flags domain memregion flags
660+
* @param prot attribute of the pmp entry
661+
* @param addr address of the pmp entry
662+
* @param log2len size of the pmp entry as power-of-2
663+
*/
664+
static inline void sbi_platform_pmp_set(const struct sbi_platform *plat,
665+
unsigned int n, unsigned long flags,
666+
unsigned long prot, unsigned long addr,
667+
unsigned long log2len)
668+
{
669+
if (plat && sbi_platform_ops(plat)->pmp_set)
670+
sbi_platform_ops(plat)->pmp_set(n, flags, prot, addr, log2len);
671+
}
672+
673+
/**
674+
* Platform specific PMP disable on current HART
675+
*
676+
* @param plat pointer to struct sbi_platform
677+
* @param n index of the pmp entry
678+
*/
679+
static inline void sbi_platform_pmp_disable(const struct sbi_platform *plat,
680+
unsigned int n)
681+
{
682+
if (plat && sbi_platform_ops(plat)->pmp_disable)
683+
sbi_platform_ops(plat)->pmp_disable(n);
684+
}
685+
647686
#endif
648687

649688
#endif

lib/sbi/sbi_domain_context.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <sbi/sbi_string.h>
1616
#include <sbi/sbi_domain.h>
1717
#include <sbi/sbi_domain_context.h>
18+
#include <sbi/sbi_platform.h>
1819
#include <sbi/sbi_trap.h>
1920

2021
/** Context representation for a hart within a domain */
@@ -115,6 +116,7 @@ static void switch_to_next_domain_context(struct hart_context *ctx,
115116

116117
/* Reconfigure PMP settings for the new domain */
117118
for (int i = 0; i < pmp_count; i++) {
119+
sbi_platform_pmp_disable(sbi_platform_thishart_ptr(), i);
118120
pmp_disable(i);
119121
}
120122
sbi_hart_pmp_configure(scratch);

lib/sbi/sbi_hart.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ static void sbi_hart_smepmp_set(struct sbi_scratch *scratch,
375375
unsigned long pmp_addr = reg->base >> PMP_SHIFT;
376376

377377
if (pmp_log2gran <= reg->order && pmp_addr < pmp_addr_max) {
378+
sbi_platform_pmp_set(sbi_platform_ptr(scratch),
379+
pmp_idx, reg->flags, pmp_flags,
380+
reg->base, reg->order);
378381
pmp_set(pmp_idx, pmp_flags, reg->base, reg->order);
379382
} else {
380383
sbi_printf("Can not configure pmp for domain %s because"
@@ -492,6 +495,9 @@ static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch,
492495

493496
pmp_addr = reg->base >> PMP_SHIFT;
494497
if (pmp_log2gran <= reg->order && pmp_addr < pmp_addr_max) {
498+
sbi_platform_pmp_set(sbi_platform_ptr(scratch),
499+
pmp_idx, reg->flags, pmp_flags,
500+
reg->base, reg->order);
495501
pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order);
496502
} else {
497503
sbi_printf("Can not configure pmp for domain %s because"
@@ -532,6 +538,9 @@ int sbi_hart_map_saddr(unsigned long addr, unsigned long size)
532538
}
533539
}
534540

541+
sbi_platform_pmp_set(sbi_platform_ptr(scratch), SBI_SMEPMP_RESV_ENTRY,
542+
SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW,
543+
pmp_flags, base, order);
535544
pmp_set(SBI_SMEPMP_RESV_ENTRY, pmp_flags, base, order);
536545

537546
return SBI_OK;
@@ -544,6 +553,7 @@ int sbi_hart_unmap_saddr(void)
544553
if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
545554
return SBI_OK;
546555

556+
sbi_platform_pmp_disable(sbi_platform_ptr(scratch), SBI_SMEPMP_RESV_ENTRY);
547557
return pmp_disable(SBI_SMEPMP_RESV_ENTRY);
548558
}
549559

platform/generic/mips/p8700.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,98 @@
1616
#include <mips/p8700.h>
1717
#include <mips/mips-cm.h>
1818

19+
static unsigned long mips_csr_read_num(int csr_num)
20+
{
21+
#define switchcase_csr_read(__csr_num, __val) \
22+
case __csr_num: \
23+
__val = csr_read(__csr_num); \
24+
break;
25+
#define switchcase_csr_read_2(__csr_num, __val) \
26+
switchcase_csr_read(__csr_num + 0, __val) \
27+
switchcase_csr_read(__csr_num + 1, __val)
28+
#define switchcase_csr_read_4(__csr_num, __val) \
29+
switchcase_csr_read_2(__csr_num + 0, __val) \
30+
switchcase_csr_read_2(__csr_num + 2, __val)
31+
#define switchcase_csr_read_8(__csr_num, __val) \
32+
switchcase_csr_read_4(__csr_num + 0, __val) \
33+
switchcase_csr_read_4(__csr_num + 4, __val)
34+
#define switchcase_csr_read_16(__csr_num, __val) \
35+
switchcase_csr_read_8(__csr_num + 0, __val) \
36+
switchcase_csr_read_8(__csr_num + 8, __val)
37+
38+
unsigned long ret = 0;
39+
40+
switch(csr_num) {
41+
switchcase_csr_read_16(CSR_MIPSPMACFG0, ret)
42+
43+
default:
44+
sbi_panic("%s: Unknown CSR %#x", __func__, csr_num);
45+
break;
46+
}
47+
48+
return ret;
49+
50+
#undef switchcase_csr_read_16
51+
#undef switchcase_csr_read_8
52+
#undef switchcase_csr_read_4
53+
#undef switchcase_csr_read_2
54+
#undef switchcase_csr_read
55+
}
56+
57+
static void mips_csr_write_num(int csr_num, unsigned long val)
58+
{
59+
#define switchcase_csr_write(__csr_num, __val) \
60+
case __csr_num: \
61+
csr_write(__csr_num, __val); \
62+
break;
63+
#define switchcase_csr_write_2(__csr_num, __val) \
64+
switchcase_csr_write(__csr_num + 0, __val) \
65+
switchcase_csr_write(__csr_num + 1, __val)
66+
#define switchcase_csr_write_4(__csr_num, __val) \
67+
switchcase_csr_write_2(__csr_num + 0, __val) \
68+
switchcase_csr_write_2(__csr_num + 2, __val)
69+
#define switchcase_csr_write_8(__csr_num, __val) \
70+
switchcase_csr_write_4(__csr_num + 0, __val) \
71+
switchcase_csr_write_4(__csr_num + 4, __val)
72+
#define switchcase_csr_write_16(__csr_num, __val) \
73+
switchcase_csr_write_8(__csr_num + 0, __val) \
74+
switchcase_csr_write_8(__csr_num + 8, __val)
75+
76+
switch(csr_num) {
77+
switchcase_csr_write_16(CSR_MIPSPMACFG0, val)
78+
79+
default:
80+
sbi_panic("%s: Unknown CSR %#x", __func__, csr_num);
81+
break;
82+
}
83+
84+
#undef switchcase_csr_write_16
85+
#undef switchcase_csr_write_8
86+
#undef switchcase_csr_write_4
87+
#undef switchcase_csr_write_2
88+
#undef switchcase_csr_write
89+
}
90+
91+
static void mips_p8700_pmp_set(unsigned int n, unsigned long flags,
92+
unsigned long prot, unsigned long addr,
93+
unsigned long log2len)
94+
{
95+
int pmacfg_csr, pmacfg_shift;
96+
unsigned long cfgmask;
97+
unsigned long pmacfg, cca;
98+
99+
pmacfg_csr = (CSR_MIPSPMACFG0 + (n >> 2)) & ~1;
100+
pmacfg_shift = (n & 7) << 3;
101+
cfgmask = ~(0xffUL << pmacfg_shift);
102+
103+
/* Read pmacfg to change cacheability */
104+
pmacfg = (mips_csr_read_num(pmacfg_csr) & cfgmask);
105+
cca = (flags & SBI_DOMAIN_MEMREGION_MMIO) ? CCA_CACHE_DISABLE :
106+
CCA_CACHE_ENABLE | PMA_SPECULATION;
107+
pmacfg |= ((cca << pmacfg_shift) & ~cfgmask);
108+
mips_csr_write_num(pmacfg_csr, pmacfg);
109+
}
110+
19111
#if CLUSTERS_IN_PLATFORM > 1
20112
static void power_up_other_cluster(u32 hartid)
21113
{
@@ -255,6 +347,7 @@ static int mips_p8700_platform_init(const void *fdt, int nodeoff, const struct f
255347
generic_platform_ops.early_init = mips_p8700_early_init;
256348
generic_platform_ops.final_init = mips_p8700_final_init;
257349
generic_platform_ops.nascent_init = mips_p8700_nascent_init;
350+
generic_platform_ops.pmp_set = mips_p8700_pmp_set;
258351

259352
return 0;
260353
}

0 commit comments

Comments
 (0)