|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: BSD-2-Clause |
| 3 | + */ |
| 4 | + |
| 5 | +#include <sbi/riscv_asm.h> |
| 6 | +#include <sbi/riscv_encoding.h> |
| 7 | +#include <sbi/sbi_bitops.h> |
| 8 | +#include <sbi/sbi_ecall_interface.h> |
| 9 | +#include <sbi/sbi_pmu.h> |
| 10 | +#include <thead/c9xx_encoding.h> |
| 11 | +#include <xuantie/xuantie_pmu.h> |
| 12 | + |
| 13 | +/* |
| 14 | + * Sscofpmf does not provide OF interrupts for the fixed cycle/instret |
| 15 | + * counters. XuanTie cores back those counters with vendor mhpmevent0/2 |
| 16 | + * CSRs whose top bits follow the same OF/MINH/...INH layout as the |
| 17 | + * standard mhpmevent3+. The generic sbi_pmu_device hooks let us layer |
| 18 | + * the vendor OF handling on top of the regular Sscofpmf path: the core |
| 19 | + * already calls hw_counter_enable_irq/disable_irq with ctr_idx 0 and 2 |
| 20 | + * via the fixed-counter fallback in pmu_fixed_ctr_update_inhibit_bits(). |
| 21 | + * |
| 22 | + * Only ctr_idx == 0 (cycle) and ctr_idx == 2 (instret) need vendor |
| 23 | + * handling. Other counters are programmable mhpmevent3+ and follow the |
| 24 | + * standard Sscofpmf code path unchanged. |
| 25 | + */ |
| 26 | + |
| 27 | +static void xuantie_pmu_ctr_enable_irq(uint32_t ctr_idx) |
| 28 | +{ |
| 29 | + if (ctr_idx != 0 && ctr_idx != 2) |
| 30 | + return; |
| 31 | + |
| 32 | + /* |
| 33 | + * Mirror pmu_ctr_enable_irq_hw(): only clear OF when no lcofip is |
| 34 | + * still pending, so we don't race with software that hasn't yet |
| 35 | + * handled the previous overflow. |
| 36 | + */ |
| 37 | + if (csr_read(CSR_MIP) & MIP_LCOFIP) |
| 38 | + return; |
| 39 | + |
| 40 | +#if __riscv_xlen == 32 |
| 41 | + /* OF lives in the H half on RV32; clear via mhpmevent0H/mhpmevent2H. */ |
| 42 | + if (ctr_idx == 0) |
| 43 | + csr_clear(CSR_MHPMEVENT0H, MHPMEVENTH_OF); |
| 44 | + else |
| 45 | + csr_clear(CSR_MHPMEVENT2H, MHPMEVENTH_OF); |
| 46 | +#else |
| 47 | + if (ctr_idx == 0) |
| 48 | + csr_clear(CSR_MHPMEVENT0, MHPMEVENT_OF); |
| 49 | + else |
| 50 | + csr_clear(CSR_MHPMEVENT2, MHPMEVENT_OF); |
| 51 | +#endif |
| 52 | +} |
| 53 | + |
| 54 | +static void xuantie_pmu_ctr_disable_irq(uint32_t ctr_idx) |
| 55 | +{ |
| 56 | + if (ctr_idx != 0 && ctr_idx != 2) |
| 57 | + return; |
| 58 | + |
| 59 | + /* |
| 60 | + * Setting OF latches the counter so a subsequent overflow cannot |
| 61 | + * raise lcofip until enable_irq clears it again. This matches the |
| 62 | + * "OF set = disabled" convention used by pmu_update_hw_mhpmevent(). |
| 63 | + */ |
| 64 | +#if __riscv_xlen == 32 |
| 65 | + if (ctr_idx == 0) |
| 66 | + csr_set(CSR_MHPMEVENT0H, MHPMEVENTH_OF); |
| 67 | + else |
| 68 | + csr_set(CSR_MHPMEVENT2H, MHPMEVENTH_OF); |
| 69 | +#else |
| 70 | + if (ctr_idx == 0) |
| 71 | + csr_set(CSR_MHPMEVENT0, MHPMEVENT_OF); |
| 72 | + else |
| 73 | + csr_set(CSR_MHPMEVENT2, MHPMEVENT_OF); |
| 74 | +#endif |
| 75 | +} |
| 76 | + |
| 77 | +static const struct sbi_pmu_device xuantie_pmu_device = { |
| 78 | + .name = "xuantie,pmu", |
| 79 | + .hw_counter_enable_irq = xuantie_pmu_ctr_enable_irq, |
| 80 | + .hw_counter_disable_irq = xuantie_pmu_ctr_disable_irq, |
| 81 | +}; |
| 82 | + |
| 83 | +void xuantie_pmu_register_device(void) |
| 84 | +{ |
| 85 | + sbi_pmu_set_device(&xuantie_pmu_device); |
| 86 | +} |
| 87 | + |
| 88 | +void xuantie_pmu_enable_ofint(void) |
| 89 | +{ |
| 90 | + csr_set(THEAD_C9XX_CSR_MXSTATUS, MXSTATUS_OFINT); |
| 91 | +} |
0 commit comments