Skip to contentSkip to conversation

Enhance maximum priority support of PLIC

PATCH Superseded Jim Shu 2 participants 6 messages Sep 30, 2022 – Oct 3, 2022 SiFive drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jim Shu

author
#0 lore raw

v2 cover Enhance maximum priority support of PLIC

This patchset fixes hard-coded maximum priority of interrupt priority register and also changes this register to WARL field to align the PLIC spec.

Changelog:

v2:
* change interrupt priority register to WARL field.

Jim Shu (2):
hw/intc: sifive_plic: fix hard-coded max priority level hw/intc: sifive_plic: change interrupt priority register to WARL field

 hw/intc/sifive_plic.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v2 1/2 hw/intc: sifive_plic: fix hard-coded max priority level

The maximum priority level is hard-coded when writing to interrupt priority register. However, when writing to priority threshold register, the maximum priority level is from num_priorities Property which is configured by platform.

Also change interrupt priority register to use num_priorities Property in maximum priority level.

---
 hw/intc/sifive_plic.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/intc/sifive_plic.c +4 -2
index af4ae3630e..f864efa761 100644
@@ -180,8 +180,10 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value,
     if (addr_between(addr, plic->priority_base, plic->num_sources << 2)) {
         uint32_t irq = ((addr - plic->priority_base) >> 2) + 1;
 
-        plic->source_priority[irq] = value & 7;
-        sifive_plic_update(plic);
+        if (value <= plic->num_priorities) {
+            plic->source_priority[irq] = value;
+            sifive_plic_update(plic);
+        }
     } else if (addr_between(addr, plic->pending_base,
                             plic->num_sources >> 3)) {
         qemu_log_mask(LOG_GUEST_ERROR,
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH v2 2/2 hw/intc: sifive_plic: change interrupt priority register to WARL field

In reply to Jim Shu

PLIC spec [1] requires interrupt source priority registers are WARL field and the number of supported priority is power-of-2 to simplify SW discovery.

Existing QEMU RISC-V machine (e.g. shakti_c) don't strictly follow PLIC spec, whose number of supported priority is not power-of-2. Just change each bit of interrupt priority register to WARL field when the number of supported priority is power-of-2.

[1] https://github.com/riscv/riscv-plic-spec/blob/master/riscv-plic.adoc#interrupt-priorities

---
 hw/intc/sifive_plic.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
hw/intc/sifive_plic.c +19 -2
index f864efa761..218ccff8bd 100644
@@ -180,7 +180,15 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value,
     if (addr_between(addr, plic->priority_base, plic->num_sources << 2)) {
         uint32_t irq = ((addr - plic->priority_base) >> 2) + 1;
 
-        if (value <= plic->num_priorities) {
+        if ((plic->num_priorities + 1) & (plic->num_priorities)) {
+            /*
+             * if "num_priorities + 1" is power-of-2, make each register bit of
+             * interrupt priority WARL (Write-Any-Read-Legal). Just filter
+             * out the access to unsupported priority bits.
+             */
+            plic->source_priority[irq] = value % (plic->num_priorities + 1);
+            sifive_plic_update(plic);
+        } else if (value <= plic->num_priorities) {
             plic->source_priority[irq] = value;
             sifive_plic_update(plic);
         }
@@ -207,7 +215,16 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value,
         uint32_t contextid = (addr & (plic->context_stride - 1));
 
         if (contextid == 0) {
-            if (value <= plic->num_priorities) {
+            if ((plic->num_priorities + 1) & (plic->num_priorities)) {
+                /*
+                 * if "num_priorities + 1" is power-of-2, each register bit of
+                 * interrupt priority is WARL (Write-Any-Read-Legal). Just
+                 * filter out the access to unsupported priority bits.
+                 */
+                plic->target_priority[addrid] = value %
+                                                (plic->num_priorities + 1);
+                sifive_plic_update(plic);
+            } else if (value <= plic->num_priorities) {
                 plic->target_priority[addrid] = value;
                 sifive_plic_update(plic);
             }
Signature · 1 line
2.17.1
#3 lore raw

Re: [PATCH v2 2/2] hw/intc: sifive_plic: change interrupt priority register to WARL field

Hi Jim,

Quoted 22 lines · Jim Shu

PLIC spec [1] requires interrupt source priority registers are WARL field and the number of supported priority is power-of-2 to simplify SW discovery.

Existing QEMU RISC-V machine (e.g. shakti_c) don't strictly follow PLIC spec, whose number of supported priority is not power-of-2. Just change each bit of interrupt priority register to WARL field when the number of supported priority is power-of-2.

[1] https://github.com/riscv/riscv-plic-spec/blob/master/riscv-plic.adoc#interrupt-priorities

---
 hw/intc/sifive_plic.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
hw/intc/sifive_plic.c +0 -0
index f864efa761..218ccff8bd 100644

hw/intc/sifive_plic.c static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value, · line 183

Quoted 5 lines
hw/intc/sifive_plic.c +1 -1
@@ -180,7 +180,15 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value,
     if (addr_between(addr, plic->priority_base, plic->num_sources << 2)) {
         uint32_t irq = ((addr - plic->priority_base) >> 2) + 1;
 
-        if (value <= plic->num_priorities) {
+        if ((plic->num_priorities + 1) & (plic->num_priorities)) {

That's the opposite. If n is a power of 2, n & (n-1) == 0 (eg 8 & 7 ==
0, 9 & 8 == 8).
Note that n must be positive too. But I'm not sure it matters here. I'll let you decide.

Quoted 12 lines
hw/intc/sifive_plic.c +8 -0
+            /*
+             * if "num_priorities + 1" is power-of-2, make each register bit of
+             * interrupt priority WARL (Write-Any-Read-Legal). Just filter
+             * out the access to unsupported priority bits.
+             */
+            plic->source_priority[irq] = value % (plic->num_priorities + 1);
+            sifive_plic_update(plic);
+        } else if (value <= plic->num_priorities) {
             plic->source_priority[irq] = value;
             sifive_plic_update(plic);
         }

hw/intc/sifive_plic.c static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value, · line 218

Quoted 5 lines
hw/intc/sifive_plic.c +1 -1
@@ -207,7 +215,16 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value,
         uint32_t contextid = (addr & (plic->context_stride - 1));
 
         if (contextid == 0) {
-            if (value <= plic->num_priorities) {
+            if ((plic->num_priorities + 1) & (plic->num_priorities)) {

Same.

Quoted 13 lines
hw/intc/sifive_plic.c +9 -1
+                /*
+                 * if "num_priorities + 1" is power-of-2, each register bit of
+                 * interrupt priority is WARL (Write-Any-Read-Legal). Just
+                 * filter out the access to unsupported priority bits.
+                 */
+                plic->target_priority[addrid] = value %
+                                                (plic->num_priorities + 1);
+                sifive_plic_update(plic);
+            } else if (value <= plic->num_priorities) {
                 plic->target_priority[addrid] = value;
                 sifive_plic_update(plic);
             }
--
Quoted 1 line

2.17.1

Clément

author
#4 lore raw

Re: [PATCH v2 2/2] hw/intc: sifive_plic: change interrupt priority register to WARL field

hi Clément,

Thank you very much.
I'll fix it in the next version patch.

Thanks,
Jim Shu

Quoted 73 lines · Clément Chigot

Hi Jim,

Quoted 22 lines · Jim Shu

PLIC spec [1] requires interrupt source priority registers are WARL field and the number of supported priority is power-of-2 to simplify SW discovery.

Existing QEMU RISC-V machine (e.g. shakti_c) don't strictly follow PLIC spec, whose number of supported priority is not power-of-2. Just change each bit of interrupt priority register to WARL field when the number of supported priority is power-of-2.

[1] https://github.com/riscv/riscv-plic-spec/blob/master/riscv-plic.adoc#interrupt-priorities

---
 hw/intc/sifive_plic.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
hw/intc/sifive_plic.c +0 -0
index f864efa761..218ccff8bd 100644

hw/intc/sifive_plic.c static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value, · line 183

Quoted 5 lines
hw/intc/sifive_plic.c +1 -1
@@ -180,7 +180,15 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value,
     if (addr_between(addr, plic->priority_base, plic->num_sources << 2)) {
         uint32_t irq = ((addr - plic->priority_base) >> 2) + 1;
 
-        if (value <= plic->num_priorities) {
+        if ((plic->num_priorities + 1) & (plic->num_priorities)) {

That's the opposite. If n is a power of 2, n & (n-1) == 0 (eg 8 & 7 ==
0, 9 & 8 == 8).
Note that n must be positive too. But I'm not sure it matters here. I'll let you decide.

Quoted 12 lines
hw/intc/sifive_plic.c +8 -0
+            /*
+             * if "num_priorities + 1" is power-of-2, make each register bit of
+             * interrupt priority WARL (Write-Any-Read-Legal). Just filter
+             * out the access to unsupported priority bits.
+             */
+            plic->source_priority[irq] = value % (plic->num_priorities + 1);
+            sifive_plic_update(plic);
+        } else if (value <= plic->num_priorities) {
             plic->source_priority[irq] = value;
             sifive_plic_update(plic);
         }

hw/intc/sifive_plic.c static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value, · line 218

Quoted 5 lines
hw/intc/sifive_plic.c +1 -1
@@ -207,7 +215,16 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value,
         uint32_t contextid = (addr & (plic->context_stride - 1));
 
         if (contextid == 0) {
-            if (value <= plic->num_priorities) {
+            if ((plic->num_priorities + 1) & (plic->num_priorities)) {

Same.

Quoted 13 lines
hw/intc/sifive_plic.c +9 -1
+                /*
+                 * if "num_priorities + 1" is power-of-2, each register bit of
+                 * interrupt priority is WARL (Write-Any-Read-Legal). Just
+                 * filter out the access to unsupported priority bits.
+                 */
+                plic->target_priority[addrid] = value %
+                                                (plic->num_priorities + 1);
+                sifive_plic_update(plic);
+            } else if (value <= plic->num_priorities) {
                 plic->target_priority[addrid] = value;
                 sifive_plic_update(plic);
             }
--
Quoted 1 line

2.17.1

Clément

author
#5 lore raw

Re: [PATCH v2 2/2] hw/intc: sifive_plic: change interrupt priority register to WARL field

Hi Clément,

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
hw/intc/sifive_plic.c +0 -0
@@ -180,7 +180,15 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value,
     if (addr_between(addr, plic->priority_base, plic->num_sources << 2)) {
         uint32_t irq = ((addr - plic->priority_base) >> 2) + 1;
 
Quoted 8 lines
Quoted 8 lines

Review static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value, · line 183

Quoted 2 lines
(quoted diff) +1 -1
-        if (value <= plic->num_priorities) {
+        if ((plic->num_priorities + 1) & (plic->num_priorities)) {

That's the opposite. If n is a power of 2, n & (n-1) == 0 (eg 8 & 7 ==
0, 9 & 8 == 8).
Note that n must be positive too. But I'm not sure it matters here. I'll let you decide.

num_priorities is a uint32_t variable so that n is always positive.