drm/amdgpu: Take IOMMU remapping into account for p2p checks
Subsystems (2)
PATCH v5 1/1 drm/amdgpu: Take IOMMU remapping into account for p2p checks
when trying to enable p2p the amdgpu_device_is_peer_accessible() checks the condition where address_mask overlaps the aper_base and hence returns 0, due to which the p2p disables for this platform
IOMMU should remap the BAR addresses so the device can access them. Hence check if peer_adev is remapping DMA
v5: (Felix, Alex)
- fixing comment as per Alex feedback
- refactor code as per Felix
v4: (Alex)
- fix the comment and description
v3:
- remove iommu_remap variable
v2: (Alex)
- Fix as per review comments
- add new function amdgpu_device_check_iommu_remap to check if iommu remap
--- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 44 +++++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +34 -10
@@ -3952,6 +3952,25 @@ static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev) adev->ram_is_direct_mapped = true; } +/** +* amdgpu_device_check_iommu_remap - Check if DMA remapping is enabled. +* +* @adev: amdgpu_device pointer +* +* return if IOMMU remapping bar address +*/ +static bool amdgpu_device_check_iommu_remap (struct amdgpu_device *adev) +{ +struct iommu_domain *domain; + + domain =iommu_get_domain_for_dev (adev->dev); +if (domain && (domain->type ==IOMMU_DOMAIN_DMA || + domain->type ==IOMMU_DOMAIN_DMA_FQ )) +return true; + +return false; +} +static const struct attribute *amdgpu_dev_attributes[] = { &dev_attr_pcie_replay_count.attr,NULL
@@ -6127,21 +6146,26 @@ bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev, struct amdgpu_device *peer_adev) {#ifdef CONFIG_HSA_AMD_P2P -uint64_t address_mask = peer_adev->dev->dma_mask ? - ~*peer_adev->dev->dma_mask : ~((1ULL <<32 ) -1 ); -resource_size_t aper_limit = - adev->gmc.aper_base + adev->gmc.aper_size -1 ;bool p2p_access = !adev->gmc.xgmi.connected_to_cpu && !(pci_p2pdma_distance (adev->pdev, peer_adev->dev, false) <0 ); -return pcie_p2p && p2p_access && (adev->gmc.visible_vram_size && - adev->gmc.real_vram_size == adev->gmc.visible_vram_size && - !(adev->gmc.aper_base & address_mask || - aper_limit & address_mask)); -#else -return false; +bool is_large_bar = adev->gmc.visible_vram_size && + adev->gmc.real_vram_size == adev->gmc.visible_vram_size; +bool p2p_addressable =amdgpu_device_check_iommu_remap (peer_adev); + +if (!p2p_addressable) { +uint64_t address_mask = peer_adev->dev->dma_mask ? + ~*peer_adev->dev->dma_mask : ~((1ULL <<32 ) -1 ); +resource_size_t aper_limit = + adev->gmc.aper_base + adev->gmc.aper_size -1 ; + + p2p_addressable = !(adev->gmc.aper_base & address_mask || + aper_limit & address_mask); + } +return is_large_bar && p2p_access && p2p_addressable;#endif +return false; }int amdgpu_device_baco_enter (struct drm_device *dev)
Signature · 1 line
2.34.1
Quoted 81 lines · Rahul Jain
when trying to enable p2p the
amdgpu_device_is_peer_accessible()checks the condition whereaddress_maskoverlaps theaper_baseand hence returns 0, due to which the p2p disables for this platformIOMMU should remap the BAR addresses so the device can access them. Hence check if
peer_adevis remapping DMAv5: (Felix, Alex)
- fixing comment as per Alex feedback
- refactor code as per Felixv4: (Alex)
- fix the comment and descriptionv3:
- removeiommu_remapvariablev2: (Alex)
- Fix as per review comments
- add new functionamdgpu_device_check_iommu_remapto check if iommu remapSigned-off-by Rahul Jain <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 44 +++++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +25
-10@@ -3952,6 +3952,25 @@ static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev) adev->ram_is_direct_mapped = true; } +/** +* amdgpu_device_check_iommu_remap - Check if DMA remapping is enabled. +* +* @adev: amdgpu_device pointer +* +* return if IOMMU remapping bar address +*/ +static bool amdgpu_device_check_iommu_remap (struct amdgpu_device *adev) +{ +struct iommu_domain *domain; + + domain =iommu_get_domain_for_dev (adev->dev); +if (domain && (domain->type ==IOMMU_DOMAIN_DMA || + domain->type ==IOMMU_DOMAIN_DMA_FQ )) +return true; + +return false; +} +static const struct attribute *amdgpu_dev_attributes[] = { &dev_attr_pcie_replay_count.attr,NULL @@ -6127,21 +6146,26 @@ bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev, struct amdgpu_device *peer_adev) {#ifdef CONFIG_HSA_AMD_P2P -uint64_t address_mask = peer_adev->dev->dma_mask ? - ~*peer_adev->dev->dma_mask : ~((1ULL <<32 ) -1 ); -resource_size_t aper_limit = - adev->gmc.aper_base + adev->gmc.aper_size -1 ;bool p2p_access = !adev->gmc.xgmi.connected_to_cpu && !(pci_p2pdma_distance (adev->pdev, peer_adev->dev, false) <0 ); -return pcie_p2p && p2p_access && (adev->gmc.visible_vram_size && - adev->gmc.real_vram_size == adev->gmc.visible_vram_size && - !(adev->gmc.aper_base & address_mask || - aper_limit & address_mask)); -#else -return false; +bool is_large_bar = adev->gmc.visible_vram_size && + adev->gmc.real_vram_size == adev->gmc.visible_vram_size; +bool p2p_addressable =amdgpu_device_check_iommu_remap (peer_adev); + +if (!p2p_addressable) { + uint64_t address_mask = peer_adev->dev->dma_mask ?
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev, · line 6168
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +9
-0+ ~*peer_adev->dev->dma_mask : ~((1ULL << 32) - 1); +resource_size_t aper_limit = + adev->gmc.aper_base + adev->gmc.aper_size -1 ; + + p2p_addressable = !(adev->gmc.aper_base & address_mask || + aper_limit & address_mask); + } +return is_large_bar && p2p_access && p2p_addressable;#endif +return false;
You changed the #else into a #endif. Logically that's OK, but it may cause a compiler warning about unreachable code because the program can never reach the "return false;" statement when CONFIG_HSA_AMD_P2P is enabled. It's probably safer to leave the #else to make sure this compiles without warnings on current and future compilers.
With that fixed, this patch is
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0
-0}int amdgpu_device_baco_enter (struct drm_device *dev)