Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/tyr: unconditional sg_table() call in new_object() for
  write-back BOs
- [Medium] drm/tyr: should_map_wc() disables WC on coherent devices
  without GPU coherency
--

commit 86892ff5c6c9751c0ce3610b27d27ca95ece458f
Author: Sami Tolvanen <[email protected]>

drm/tyr: support write-combine BO mappings

This commit queries device DMA coherence at probe time to decide whether
shmem-backed GEM buffer objects should be mapped write-combine or
write-back, and fetches the scatter-gather table at BO creation time to
flush zeroed pages.

> diff --git a/drivers/gpu/drm/tyr/driver.rs b/drivers/gpu/drm/tyr/driver.rs
> index ba03cc4c52539..6869ab345d8fd 100644
> --- a/drivers/gpu/drm/tyr/driver.rs
> +++ b/drivers/gpu/drm/tyr/driver.rs
[ ... ]
> @@ -151,6 +158,8 @@ fn probe<'bound>(
>              // other threads of execution.
>              unsafe { 
> pdev.dma_set_mask_and_coherent(DmaMask::try_new(pa_bits)?)? };
>  
> +            let coherent = pdev.as_ref().dma_attr() == DmaAttr::COHERENT;
> +
>              let unreg_dev = 
> drm::UnregisteredDevice::<TyrDrmDriver>::new(pdev, Ok(()))?;
>  
>              let mmu = Mmu::new(pdev.as_ref(), iomem.as_arc_borrow(), 
> &gpu_info)?;
> @@ -161,6 +170,7 @@ fn probe<'bound>(
>                  &unreg_dev,
>                  mmu.as_arc_borrow(),
>                  &gpu_info,
> +                coherent,
>              )?;

[Severity: Medium]
Can enabling write-back BO mappings when DmaAttr::COHERENT is set break MCU
boot before GPU and MMU coherency are configured?

Tyr currently hardcodes non-coherent operation in the GPU and MMU:
GpuInfo::new() sets selected_coherency to DRM_PANTHOR_GPU_COHERENCY_NONE
without programming COHERENCY_ENABLE, VmAsData::new() sets coherent_walk to
false, and MEMATTR::attribute_from_mair() always encodes
Coherency::MidgardInnerDomain.  On a platform whose firmware marks the GPU
device as dma-coherent, passing coherent = true causes KernelBo::new() to
create MCU firmware section BOs with map_wc: false, so
Firmware::init_section_mem() writes firmware sections through a write-back
cached vmap without CPU cache maintenance while the MCU reads stale memory
from DRAM.

> diff --git a/drivers/gpu/drm/tyr/gem.rs b/drivers/gpu/drm/tyr/gem.rs
> index 60f6363995a5a..55e2d67b96d98 100644
> --- a/drivers/gpu/drm/tyr/gem.rs
> +++ b/drivers/gpu/drm/tyr/gem.rs
[ ... ]
> @@ -56,24 +69,48 @@ fn new(_dev: &TyrDrmDevice, _size: usize, args: 
> BoCreateArgs) -> impl PinInit<Se
>  /// Type alias for Tyr GEM buffer objects.
>  pub(crate) type Bo = gem::shmem::Object<BoData>;
>  
> +/// Returns whether a BO should be mapped write-combine.
> +pub(crate) fn should_map_wc(coherent: bool, flags: u32) -> bool {
> +    if coherent {
> +        return false;
> +    }
> +
> +    if flags & uapi::drm_panthor_bo_flags_DRM_PANTHOR_BO_WB_MMAP != 0 {
> +        return false;
> +    }
> +
> +    true
> +}
> +
>  /// Create a new GEM buffer object.
>  #[expect(dead_code)]
> -pub(crate) fn new_object(ddev: &TyrDrmDevice, size: usize, flags: u32) -> 
> Result<ARef<Bo>> {
> +pub(crate) fn new_object(
> +    ddev: &TyrDrmDevice,
> +    pdev: &Device<Bound>,
> +    size: usize,
> +    flags: u32,
> +    coherent: bool,
> +) -> Result<ARef<Bo>> {
>      if size == 0 {
>          return Err(EINVAL);
>      }
>  
>      let aligned_size = 
> size.checked_next_multiple_of(PAGE_SIZE).ok_or(EINVAL)?;
>  
> -    Bo::new(
> +    let map_wc = should_map_wc(coherent, flags);
> +    let bo = Bo::new(
>          ddev,
>          aligned_size,
>          shmem::ObjectConfig {
> -            map_wc: true,
> +            map_wc,
>              parent_resv_obj: None,
>          },
>          BoCreateArgs { flags },
> -    )
> +    )?;
> +
> +    bo.sg_table(pdev)?;
> +
> +    Ok(bo)
>  }

[Severity: Medium]
Should bo.sg_table(pdev) only be called when map_wc is true?

When map_wc is false (for example with DRM_PANTHOR_BO_WB_MMAP), calling
bo.sg_table(pdev) here eagerly pins all backing pages and runs
dma_map_sgtable() at BO creation time even though CPU accesses use cached
mappings.  Because shmem::Object::sg_table() caches the result in a SetOnce,
consuming that one-shot dma_map_sgtable() call before userspace writes to
the write-back mapping also means the later sg_table() call in
GpuVmData::sm_step_map() during VM_BIND returns the cached table without
flushing dirty CPU cache lines.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=9

Reply via email to