Skip to content

Conversation

@wxjstz
Copy link

@wxjstz wxjstz commented Jan 7, 2026

Summary by Sourcery

Introduce platform-specific hooks for local TLB and cache fence operations and wire them into the TLB handling path, with Thead generic platforms optionally overriding default behavior via errata quirks.

New Features:

  • Add optional platform operations to override local fence.i, sfence.vma, sfence.vma.asid, and hypervisor fence variants using TLB flush metadata.
  • Allow Thead generic platforms with JTLB errata to provide custom sfence.vma and sfence.vma.asid implementations selected via device-tree quirks.

Enhancements:

  • Update core TLB handling to invoke platform-specific local fence callbacks when available, falling back to the existing generic instructions otherwise.
  • Extend Thead errata flags to cover JTLB behavior and adjust compatible matching to enable the new TLB handling on affected SoCs.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 7, 2026

Reviewer's Guide

Adds a platform hook layer for local TLB-related fence operations and wires it up for the T-Head generic platform (including a new JTLB erratum quirk), so that platforms can override the default TLB flush behavior used by the core sbi_tlb logic.

Sequence diagram for platform-overridable local TLB flush (sfence_vma path)

sequenceDiagram
    participant Hart as Hart
    participant sbi_tlb as sbi_tlb_local_sfence_vma
    participant PlatWrap as sbi_platform_local_sfence_vma
    participant Plat as sbi_platform
    participant Ops as sbi_platform_operations
    participant THead as thead_jtlb_local_sfence_vma

    Hart->>sbi_tlb: sbi_tlb_local_sfence_vma(tinfo)
    sbi_tlb->>sbi_tlb: sbi_pmu_ctr_incr_fw(SBI_PMU_FW_SFENCE_VMA_RCVD)
    sbi_tlb->>PlatWrap: sbi_platform_local_sfence_vma(plat, tinfo)
    PlatWrap->>Plat: sbi_platform_ops(plat)
    Plat->>Ops: get local_sfence_vma
    alt local_sfence_vma implemented
        Ops->>THead: local_sfence_vma(tinfo)
        THead-->>PlatWrap: return
        PlatWrap-->>sbi_tlb: return SBI_OK
        sbi_tlb-->>Hart: return (platform handled TLB flush)
    else local_sfence_vma not implemented
        PlatWrap-->>sbi_tlb: return SBI_ENOTSUPP
        sbi_tlb->>sbi_tlb: execute default sfence.vma sequence
        sbi_tlb-->>Hart: return (default handled TLB flush)
    end
Loading

Class diagram for updated sbi_platform_operations and TLB hook integration

classDiagram
    class sbi_tlb_info {
        +unsigned long start
        +unsigned long size
        +unsigned long asid
        +unsigned long vmid
    }

    class sbi_platform_operations {
        +get_tlb_num_entries() u32
        +local_fence_i(tinfo sbi_tlb_info) void
        +local_sfence_vma(tinfo sbi_tlb_info) void
        +local_sfence_vma_asid(tinfo sbi_tlb_info) void
        +local_hfence_gvma_vmid(tinfo sbi_tlb_info) void
        +local_hfence_gvma(tinfo sbi_tlb_info) void
        +local_hfence_vvma_asid(tinfo sbi_tlb_info) void
        +local_hfence_vvma(tinfo sbi_tlb_info) void
    }

    class sbi_platform {
        +sbi_platform_operations *platform_ops
    }

    class sbi_tlb {
        +sbi_tlb_local_fence_i(tinfo sbi_tlb_info) void
        +sbi_tlb_local_sfence_vma(tinfo sbi_tlb_info) void
        +sbi_tlb_local_sfence_vma_asid(tinfo sbi_tlb_info) void
        +sbi_tlb_local_hfence_gvma_vmid(tinfo sbi_tlb_info) void
        +sbi_tlb_local_hfence_gvma(tinfo sbi_tlb_info) void
        +sbi_tlb_local_hfence_vvma_asid(tinfo sbi_tlb_info) void
        +sbi_tlb_local_hfence_vvma(tinfo sbi_tlb_info) void
    }

    class sbi_platform_wrappers {
        +sbi_platform_local_fence_i(plat sbi_platform, tinfo sbi_tlb_info) u32
        +sbi_platform_local_sfence_vma(plat sbi_platform, tinfo sbi_tlb_info) u32
        +sbi_platform_local_sfence_vma_asid(plat sbi_platform, tinfo sbi_tlb_info) u32
        +sbi_platform_local_hfence_gvma_vmid(plat sbi_platform, tinfo sbi_tlb_info) u32
        +sbi_platform_local_hfence_gvma(plat sbi_platform, tinfo sbi_tlb_info) u32
        +sbi_platform_local_hfence_vvma_asid(plat sbi_platform, tinfo sbi_tlb_info) u32
        +sbi_platform_local_hfence_vvma(plat sbi_platform, tinfo sbi_tlb_info) u32
    }

    class thead_generic_quirks {
        +unsigned long errata
    }

    class thead_generic_quirks_instances {
        +thead_pmu_quirks
        +thead_pmu_jtlb_quirks
    }

    class thead_jtlb_hooks {
        +thead_jtlb_local_sfence_vma(tinfo sbi_tlb_info) void
        +thead_jtlb_local_hfence_vvma_asid(tinfo sbi_tlb_info) void
    }

    sbi_platform o-- sbi_platform_operations : has
    sbi_tlb --> sbi_platform_wrappers : calls
    sbi_platform_wrappers --> sbi_platform_operations : dispatches
    sbi_platform_operations --> sbi_tlb_info : uses

    thead_generic_quirks_instances ..> thead_generic_quirks : instance_of
    thead_generic_quirks_instances ..> sbi_platform_operations : configures
    thead_generic_quirks_instances ..> thead_jtlb_hooks : selects
    sbi_platform_operations ..> thead_jtlb_hooks : function_pointers
Loading

Flow diagram for T-Head generic platform init with JTLB erratum TLB hooks

flowchart TD
    A[thead_generic_platform_init] --> B[Read compatible from FDT]
    B --> C[Match in thead_generic_match]
    C --> D[Get thead_generic_quirks *quirks]

    D --> E{quirks->errata & THEAD_QUIRK_ERRATA_THEAD_PMU}
    E -- yes --> F[generic_platform_ops.extensions_init = thead_pmu_extensions_init]
    E -- no --> G[Skip PMU extensions init]

    D --> H{quirks->errata & THEAD_QUIRK_ERRATA_JTLB}
    H -- yes --> I[generic_platform_ops.local_sfence_vma = thead_jtlb_local_sfence_vma]
    I --> J[generic_platform_ops.local_sfence_vma_asid = thead_jtlb_local_hfence_vvma_asid]
    H -- no --> K[Keep default TLB flush behavior]

    F --> L[Return 0]
    G --> L
    J --> L
    K --> L
Loading

File-Level Changes

Change Details Files
Introduce platform-operation hooks and inline helpers for local TLB fence operations, returning ENOTSUPP when a platform does not override them.
  • Forward declare struct sbi_tlb_info so it can be used in platform operations signatures.
  • Extend sbi_platform_operations with function pointers for local_fence_i, local_sfence_vma, local_sfence_vma_asid, local_hfence_gvma_vmid, local_hfence_gvma, local_hfence_vvma_asid, and local_hfence_vvma.
  • Add static inline wrapper functions that invoke the corresponding platform hook if present and otherwise return SBI_ENOTSUPP, taking a const sbi_platform pointer and sbi_tlb_info parameter.
include/sbi/sbi_platform.h
Allow sbi_tlb local operations to defer to platform-specific TLB fence hooks before executing the generic SBI implementation.
  • In each sbi_tlb_local_* handler, increment the PMU counter as before, then call the new sbi_platform_local_* helper with sbi_platform_thishart_ptr() and return early if it reports success.
  • Keep the existing fallback behavior (csr swaps and _sbi* / inline asm) when the platform hook is absent or reports ENOTSUPP, preserving current semantics for platforms that do not implement overrides.
lib/sbi/sbi_tlb.c
Add T-Head generic platform JTLB-specific implementations for selected local TLB fences and wire them via a new erratum quirk and DT match variant.
  • Include sbi_tlb.h so thead-generic platform code can use struct sbi_tlb_info and TLB flush helpers.
  • Implement thead_jtlb_local_sfence_vma using a bare sfence.vma instruction (ignoring tinfo).
  • Implement thead_jtlb_local_hfence_vvma_asid (named asid handler) to perform sfence.vma with the ASID from tinfo for both full-context and non-full flush cases.
  • Extend thead_generic_platform_init to, when THEAD_QUIRK_ERRATA_JTLB is set, assign generic_platform_ops.local_sfence_vma and local_sfence_vma_asid to the new JTLB handlers.
  • Define THEAD_QUIRK_ERRATA_JTLB in c9xx_errata.h and create a thead_pmu_jtlb_quirks struct combining THEAD_QUIRK_ERRATA_THEAD_PMU and THEAD_QUIRK_ERRATA_JTLB, then update the sg2044 FDT match to use this combined quirk struct.
platform/generic/thead/thead-generic.c
platform/generic/include/thead/c9xx_errata.h

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues, and left some high level feedback:

  • In sbi_tlb_local_hfence_vvma() the platform hook check calls sbi_platform_local_hfence_gvma(...) instead of the corresponding sbi_platform_local_hfence_vvma(...), which looks like a copy/paste error and will prevent platform-specific vvma handling from being used.
  • In thead_jtlb_local_hfence_vvma_asid(), both the full-context and partial flush paths execute the same sfence.vma x0, asid sequence, so the conditional on start/size is redundant; either simplify this or implement the intended range-based behavior.
  • In thead_generic_platform_init(), the new condition if (quirks->errata &THEAD_QUIRK_ERRATA_JTLB) is missing a space after &, which is inconsistent with surrounding style and existing code patterns for errata checks.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `sbi_tlb_local_hfence_vvma()` the platform hook check calls `sbi_platform_local_hfence_gvma(...)` instead of the corresponding `sbi_platform_local_hfence_vvma(...)`, which looks like a copy/paste error and will prevent platform-specific vvma handling from being used.
- In `thead_jtlb_local_hfence_vvma_asid()`, both the full-context and partial flush paths execute the same `sfence.vma x0, asid` sequence, so the conditional on `start/size` is redundant; either simplify this or implement the intended range-based behavior.
- In `thead_generic_platform_init()`, the new condition `if (quirks->errata &THEAD_QUIRK_ERRATA_JTLB)` is missing a space after `&`, which is inconsistent with surrounding style and existing code patterns for errata checks.

## Individual Comments

### Comment 1
<location> `lib/sbi/sbi_tlb.c:46-47` </location>
<code_context>

 	sbi_pmu_ctr_incr_fw(SBI_PMU_FW_HFENCE_VVMA_RCVD);

+	if (sbi_platform_local_hfence_gvma(
+			sbi_platform_thishart_ptr(), tinfo) == SBI_OK)
+		return;
+
</code_context>

<issue_to_address>
**issue (bug_risk):** sbi_tlb_local_hfence_vvma is calling the GVMA hook instead of the VVMA hook, which looks like a copy-paste bug.

The new fast-path in `sbi_tlb_local_hfence_vvma` calls `sbi_platform_local_hfence_gvma(...)`, but there is now a dedicated `local_hfence_vvma` hook (with a wrapper in `sbi_platform.h`). As a result, any vvma-specific platform override is bypassed and the wrong hook type is used. This should instead call `sbi_platform_local_hfence_vvma(...)`.
</issue_to_address>

### Comment 2
<location> `platform/generic/thead/thead-generic.c:48-57` </location>
<code_context>
+	__asm__ __volatile__("sfence.vma");
+}
+
+static void thead_jtlb_local_hfence_vvma_asid(struct sbi_tlb_info *tinfo)
+{
+	unsigned long start = tinfo->start;
+	unsigned long size  = tinfo->size;
+	unsigned long asid  = tinfo->asid;
+
+	/* Flush entire MM context for a given ASID */
+	if ((start == 0 && size == 0) || (size == SBI_TLB_FLUSH_ALL)) {
+		__asm__ __volatile__("sfence.vma x0, %0"
+				     :
+				     : "r"(asid)
+				     : "memory");
+		return;
+	}
+
+	__asm__ __volatile__("sfence.vma x0, %0"
+			     :
+			     : "r"(asid)
</code_context>

<issue_to_address>
**suggestion:** The hfence_vvma_asid helper always issues a full sfence.vma by ASID, making the range check redundant and the name misleading.

In `thead_jtlb_local_hfence_vvma_asid`, both the `if` branch and the fall-through execute `sfence.vma x0, asid`, so the `(start, size)` range logic is dead code. If the JTLB errata require a full ASID flush, consider removing the unused range handling and renaming the helper to indicate that behavior. If a partial flush is actually required, this helper should honor `start` and `size` instead of always flushing the full ASID.

Suggested implementation:

```c
static void thead_jtlb_local_hfence_vvma_asid_full(struct sbi_tlb_info *tinfo)
{
	unsigned long asid = tinfo->asid;

	/* Flush entire MM context for a given ASID */
	__asm__ __volatile__("sfence.vma x0, %0"
			     :
			     : "r"(asid)
			     : "memory");
}

```

1. Update all call sites and function pointers that currently reference `thead_jtlb_local_hfence_vvma_asid` to use the new name `thead_jtlb_local_hfence_vvma_asid_full`, so the build does not break.
2. If this function is exposed via any ops table (e.g. `generic_platform_ops` or similar), adjust the corresponding entry to use the new symbol name.
3. If there is any documentation or comments referring to the old helper name, update them to clarify that this helper always performs a full ASID-based `sfence.vma` and does not honor address ranges.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +48 to +57
static void thead_jtlb_local_hfence_vvma_asid(struct sbi_tlb_info *tinfo)
{
unsigned long start = tinfo->start;
unsigned long size = tinfo->size;
unsigned long asid = tinfo->asid;

/* Flush entire MM context for a given ASID */
if ((start == 0 && size == 0) || (size == SBI_TLB_FLUSH_ALL)) {
__asm__ __volatile__("sfence.vma x0, %0"
:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The hfence_vvma_asid helper always issues a full sfence.vma by ASID, making the range check redundant and the name misleading.

In thead_jtlb_local_hfence_vvma_asid, both the if branch and the fall-through execute sfence.vma x0, asid, so the (start, size) range logic is dead code. If the JTLB errata require a full ASID flush, consider removing the unused range handling and renaming the helper to indicate that behavior. If a partial flush is actually required, this helper should honor start and size instead of always flushing the full ASID.

Suggested implementation:

static void thead_jtlb_local_hfence_vvma_asid_full(struct sbi_tlb_info *tinfo)
{
	unsigned long asid = tinfo->asid;

	/* Flush entire MM context for a given ASID */
	__asm__ __volatile__("sfence.vma x0, %0"
			     :
			     : "r"(asid)
			     : "memory");
}
  1. Update all call sites and function pointers that currently reference thead_jtlb_local_hfence_vvma_asid to use the new name thead_jtlb_local_hfence_vvma_asid_full, so the build does not break.
  2. If this function is exposed via any ops table (e.g. generic_platform_ops or similar), adjust the corresponding entry to use the new symbol name.
  3. If there is any documentation or comments referring to the old helper name, update them to clarify that this helper always performs a full ASID-based sfence.vma and does not honor address ranges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant