powerpc/dma: Define map/unmap mmio resource callbacks
PATCH v2 1/2 powerpc/dma: Define map/unmap mmio resource callbacks
Define the map_resource/unmap_resource callbacks for the dma_iommu_ops used by several powerpc platforms. The map_resource callback is called when trying to map a mmio resource through the dma_map_resource() driver API.
For now, the callback returns an invalid address for devices using translations, but will "direct" map the resource when in bypass mode. Previous behavior for dma_map_resource() was to always return an invalid address.
We also call an optional platform-specific controller op in case some setup is needed for the platform.
---
changes from v1:
- rename pci_controller_ops callback to dma_direct_map_resource/dma_direct_unmap_resource
- cosmetic changes to make the code more readable
--- arch/powerpc/include/asm/pci-bridge.h | 7 +++++++ arch/powerpc/kernel/dma-iommu.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)
arch/powerpc/include/asm/pci-bridge.h +7 -0
@@ -44,6 +44,13 @@ struct pci_controller_ops {#endif void (*shutdown)(struct pci_controller *hose); +int (*dma_direct_map_resource)(struct pci_dev *pdev, +phys_addr_t phys_addr, +size_t size, +enum dma_data_direction dir); +void (*dma_direct_unmap_resource)(struct pci_dev *pdev, +dma_addr_t addr,size_t size, +enum dma_data_direction dir); };/*
arch/powerpc/kernel/dma-iommu.c +31 -0
@@ -108,6 +108,35 @@ static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,dma_direct_unmap_sg (dev, sglist, nelems, direction, attrs); } +static dma_addr_t dma_iommu_map_resource (struct device *dev, +phys_addr_t phys_addr,size_t size, +enum dma_data_direction dir, +unsigned long attrs) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct pci_controller *phb =pci_bus_to_host (pdev->bus); +struct pci_controller_ops *ops = &phb->controller_ops; + +if (!dma_iommu_map_bypass (dev, attrs) || + !ops->dma_direct_map_resource || + ops->dma_direct_map_resource (pdev, phys_addr, size, dir)) +return DMA_MAPPING_ERROR ; + +return dma_direct_map_resource (dev, phys_addr, size, dir, attrs); +} + +static void dma_iommu_unmap_resource (struct device *dev,dma_addr_t dma_handle, +size_t size,enum dma_data_direction dir, +unsigned long attrs) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct pci_controller *phb =pci_bus_to_host (pdev->bus); +struct pci_controller_ops *ops = &phb->controller_ops; + +if (dma_iommu_map_bypass (dev, attrs) && ops->dma_direct_unmap_resource) + ops->dma_direct_unmap_resource (pdev, dma_handle, size, dir); +} +static bool dma_iommu_bypass_supported (struct device *dev,u64 mask) {struct pci_dev *pdev =to_pci_dev (dev);
@@ -199,6 +228,8 @@ extern void dma_iommu_sync_sg_for_device(struct device *dev, .free = dma_iommu_free_coherent, .map_sg = dma_iommu_map_sg, .unmap_sg = dma_iommu_unmap_sg, + .map_resource = dma_iommu_map_resource, + .unmap_resource = dma_iommu_unmap_resource, .dma_supported = dma_iommu_dma_supported, .map_page = dma_iommu_map_page, .unmap_page = dma_iommu_unmap_page,
Signature · 1 line
1.8.3.1
PATCH v2 2/2 powerpc/powernv: Enable and setup PCI P2P
Implement the generic dma_map_resource callback on the PCI controller for powernv. This will enable PCI P2P on POWER9 architecture. It will allow catching a cross-PHB mmio mapping, which needs to be setup in hardware by calling opal. Both the initiator and target PHBs need to be configured, so we look for which PHB owns the mmio address being mapped.
---
changes from v1:
- remove CONFIG_PCI_P2PDMA around opal_pci_set_p2p decleration
- divide pnv_pci_ioda_set_p2p to pnv_pci_ioda_enable_p2p/pnv_pci_ioda_disable_p2p
- added pnv_pci_dma_dir_to_opal_p2p static helper
--- arch/powerpc/include/asm/opal.h | 3 +- arch/powerpc/platforms/powernv/opal-call.c | 1 + arch/powerpc/platforms/powernv/pci-ioda.c | 212 +++++++++++++++++++++++++++-- arch/powerpc/platforms/powernv/pci.h | 9 ++ 4 files changed, 213 insertions(+), 12 deletions(-)
arch/powerpc/include/asm/opal.h +2 -1
@@ -284,7 +284,8 @@ int64_t opal_xive_set_queue_state(uint64_t vp, uint32_t prio,uint32_t qtoggle, uint32_t qindex);int64_t opal_xive_get_vp_state (uint64_t vp,__be64 *out_w01); - +int64_t opal_pci_set_p2p (uint64_t phb_init,uint64_t phb_target, +uint64_t desc,uint16_t pe_number);int64_t opal_imc_counters_init (uint32_t type,uint64_t address,uint64_t cpu_pir);int64_t opal_imc_counters_start (uint32_t type,uint64_t cpu_pir);
arch/powerpc/platforms/powernv/opal-call.c +1 -0
@@ -273,6 +273,7 @@ int64_t name(int64_t a0, int64_t a1, int64_t a2, int64_t a3, \OPAL_CALL (opal_imc_counters_init,OPAL_IMC_COUNTERS_INIT );OPAL_CALL (opal_imc_counters_start,OPAL_IMC_COUNTERS_START );OPAL_CALL (opal_imc_counters_stop,OPAL_IMC_COUNTERS_STOP ); +OPAL_CALL (opal_pci_set_p2p,OPAL_PCI_SET_P2P );OPAL_CALL (opal_get_powercap,OPAL_GET_POWERCAP );OPAL_CALL (opal_set_powercap,OPAL_SET_POWERCAP );OPAL_CALL (opal_get_power_shift_ratio,OPAL_GET_POWER_SHIFT_RATIO );
arch/powerpc/platforms/powernv/pci-ioda.c +201 -11
219 lines not on this page · raw message
arch/powerpc/platforms/powernv/pci.h +9 -0
@@ -79,6 +79,10 @@ struct pnv_ioda_pe {struct pnv_ioda_pe *master;struct list_head slaves; +#ifdef CONFIG_PCI_P2PDMA +/* PCI peer-to-peer*/ +int p2p_initiator_count; +#endif /* Link in list of PE#s */ struct list_head list; };
@@ -168,6 +172,11 @@ struct pnv_phb {/* PHB and hub diagnostics */ unsigned int diag_data_size;u8 *diag_data; + +#ifdef CONFIG_PCI_P2PDMA +/* PCI peer-to-peer*/ +int p2p_target_count; +#endif };extern struct pci_ops pnv_pci_ops;
Signature · 1 line
1.8.3.1
Re: [PATCH 2/2 v2] powerpc/powernv: Enable and setup PCI P2P
Quoted 30 lines · Max Gurtovoy
arch/powerpc/platforms/powernv/pci-ioda.c +22
-0@@ -3706,18 +3706,208 @@ static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) } } +#ifdef CONFIG_PCI_P2PDMA +static DEFINE_MUTEX (p2p_mutex); + +static bool pnv_pci_controller_owns_addr (struct pci_controller *hose, +phys_addr_t addr,size_t size) +{ +int i; + +/* +* It seems safe to assume the full range is under the same PHB, so we +* can ignore the size. +*/ +for (i =0 ; i <ARRAY_SIZE (hose->mem_resources); i++) { +struct resource *res = &hose->mem_resources[i]; + +if (res->flags && addr >= res->start && addr < res->end) +return true; + } +return false; +} + +/*
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3740
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+* find the phb owning a mmio address if not owned locally +*/ +static struct pnv_phb *pnv_pci_find_owning_phb (struct pci_dev *pdev, +phys_addr_t addr,size_t size) +{ +struct pci_controller *hose; + +/* fast path */ +if (pnv_pci_controller_owns_addr (pdev->bus->sysdata, addr, size)) +return NULL ;
Do we actually need this fast path? It's going to be slow either way. Also if a device is doing p2p to another device under the same PHB then it should not be happening via the root complex. Is this a case you've tested?
Quoted 30 lines
arch/powerpc/platforms/powernv/pci-ioda.c +30
-0+list_for_each_entry (hose, &hose_list, list_node) { +struct pnv_phb *phb = hose->private_data; + +if (phb->type !=PNV_PHB_NPU_NVLINK && + phb->type !=PNV_PHB_NPU_OCAPI ) { +if (pnv_pci_controller_owns_addr (hose, addr, size)) +return phb; + } + } +return NULL ; +} + +static u64 pnv_pci_dma_dir_to_opal_p2p (enum dma_data_direction dir) +{ +if (dir ==DMA_TO_DEVICE ) +return OPAL_PCI_P2P_STORE ; +else if (dir ==DMA_FROM_DEVICE ) +return OPAL_PCI_P2P_LOAD ; +else if (dir ==DMA_BIDIRECTIONAL ) +return OPAL_PCI_P2P_LOAD |OPAL_PCI_P2P_STORE ; +else +return 0 ; +} + +static int pnv_pci_ioda_enable_p2p (struct pci_dev *initiator, +struct pnv_phb *phb_target, +enum dma_data_direction dir) +{ +struct pci_controller *hose; +struct pnv_phb *phb_init;
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3780
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +9
-0+struct pnv_ioda_pe *pe_init; +u64 desc; +int rc; + +if (!opal_check_token (OPAL_PCI_SET_P2P )) +return -ENXIO ; + + hose =pci_bus_to_host (initiator->bus); + phb_init = hose->private_data;
You can use the pci_bus_to_pnvhb() helper
Quoted 9 lines
arch/powerpc/platforms/powernv/pci-ioda.c +9
-0+ + pe_init =pnv_ioda_get_pe (initiator); +if (!pe_init) +return -ENODEV ; + +if (!pe_init->tce_bypass_enabled) +return -EINVAL ; + +/*
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3799
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+* Configuring the initiator's PHB requires to adjust its TVE#1 +* setting. Since the same device can be an initiator several times for +* different target devices, we need to keep a reference count to know +* when we can restore the default bypass setting on its TVE#1 when +* disabling. Opal is not tracking PE states, so we add a reference +* count on the PE in linux. +* +* For the target, the configuration is per PHB, so we keep a +* target reference count on the PHB. +*/
This irks me a bit because configuring the DMA address limits for the TVE is the kernel's job. What we really should be doing is using opal_pci_map_pe_dma_window_real() to set the bypass-mode address limit for the TVE to something large enough to hit the MMIO ranges rather than having set_p2p do it as a side effect. Unfortunately, for some reason skiboot doesn't implement support for enabling 56bit addressing using opal_pci_map_pe_dma_window_real() and we do need to support older kernel's which used this stuff so I guess we're stuck with it for now. It'd be nice if we could fix this in the longer term though...
Quoted 33 lines
arch/powerpc/platforms/powernv/pci-ioda.c +33
-0+mutex_lock (&p2p_mutex); + + desc =OPAL_PCI_P2P_ENABLE |pnv_pci_dma_dir_to_opal_p2p (dir); +/* always go to opal to validate the configuration */ + rc =opal_pci_set_p2p (phb_init->opal_id, phb_target->opal_id, desc, + pe_init->pe_number); +if (rc !=OPAL_SUCCESS ) { + rc = -EIO ; + goto out; + } + + pe_init->p2p_initiator_count++; + phb_target->p2p_target_count++; + + rc =0 ; +out: +mutex_unlock (&p2p_mutex); +return rc; +} + +static int pnv_pci_dma_map_resource (struct pci_dev *pdev, +phys_addr_t phys_addr,size_t size, +enum dma_data_direction dir) +{ +struct pnv_phb *target_phb; + + target_phb =pnv_pci_find_owning_phb (pdev, phys_addr, size); +if (!target_phb) +return 0 ; + +return pnv_pci_ioda_enable_p2p (pdev, target_phb, dir); +} +
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3843
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+static int pnv_pci_ioda_disable_p2p (struct pci_dev *initiator, +struct pnv_phb *phb_target) +{ +struct pci_controller *hose; +struct pnv_phb *phb_init; +struct pnv_ioda_pe *pe_init; +int rc; + +if (!opal_check_token (OPAL_PCI_SET_P2P )) +return -ENXIO ;
This should probably have a WARN_ON() since we can't hit this path unless the initial map succeeds.
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3844
Quoted 2 lines
arch/powerpc/platforms/powernv/pci-ioda.c +2
-0+ hose =pci_bus_to_host (initiator->bus); + phb_init = hose->private_data;
pci_bus_to_pnvhb()
Quoted 34 lines
arch/powerpc/platforms/powernv/pci-ioda.c +34
-0+ pe_init =pnv_ioda_get_pe (initiator); +if (!pe_init) +return -ENODEV ; + +mutex_lock (&p2p_mutex); + +if (!pe_init->p2p_initiator_count || !phb_target->p2p_target_count) { + rc = -EINVAL ; + goto out; + } + +if (--pe_init->p2p_initiator_count ==0 ) +pnv_pci_ioda2_set_bypass (pe_init, true); + +if (--phb_target->p2p_target_count ==0 ) { + rc =opal_pci_set_p2p (phb_init->opal_id, phb_target->opal_id, +0 , pe_init->pe_number); +if (rc !=OPAL_SUCCESS ) { + rc = -EIO ; + goto out; + } + } + + rc =0 ; +out: +mutex_unlock (&p2p_mutex); +return rc; +} + +static void pnv_pci_dma_unmap_resource (struct pci_dev *pdev, +dma_addr_t addr,size_t size, +enum dma_data_direction dir) +{ +struct pnv_phb *target_phb;
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3891
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+int rc; + + target_phb =pnv_pci_find_owning_phb (pdev, addr, size); +if (!target_phb) +return ; + + rc =pnv_pci_ioda_disable_p2p (pdev, target_phb); +if (rc) +dev_err (&pdev->dev,"Failed to undo PCI peer-to-peer setup for address %llx: %d\n" , + addr, rc);
Use pci_err() or pe_err().
RE: [PATCH 2/2 v2] powerpc/powernv: Enable and setup PCI P2P
+ David from IBM.
-----Original Message-----
From: Oliver O'Halloran <[email protected]>
Sent: Monday, August 3, 2020 2:35 AM
To: Max Gurtovoy <[email protected]>
Cc: Christoph Hellwig <[email protected]>; linux-pci <[email protected]>; linuxppc-dev <[email protected]>; Israel Rukshin <[email protected]>; Idan Werpoler <[email protected]>; Vladimir Koushnir <[email protected]>; Shlomi Nimrodi <[email protected]>; Frederic Barrat <[email protected]>; Carol Soto <[email protected]>; Aneela Devarasetty <[email protected]>
Subject: Re: [PATCH 2/2 v2] powerpc/powernv: Enable and setup PCI P2P
Quoted 30 lines · Max Gurtovoy
arch/powerpc/platforms/powernv/pci-ioda.c +21
-0@@ -3706,18 +3706,208 @@ static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) } } +#ifdef CONFIG_PCI_P2PDMA +static DEFINE_MUTEX (p2p_mutex); + +static bool pnv_pci_controller_owns_addr (struct pci_controller *hose, +phys_addr_t addr,size_t +size) { +int i; + +/* +* It seems safe to assume the full range is under the same PHB, so we +* can ignore the size. +*/ +for (i =0 ; i <ARRAY_SIZE (hose->mem_resources); i++) { +struct resource *res = &hose->mem_resources[i]; + +if (res->flags && addr >= res->start && addr < res->end) +return true; + } +return false; +} +
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3739
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+/* +* find the phb owning a mmio address if not owned locally */ static +struct pnv_phb *pnv_pci_find_owning_phb (struct pci_dev *pdev, +phys_addr_t addr, +size_t size) { +struct pci_controller *hose; + +/* fast path */ +if (pnv_pci_controller_owns_addr (pdev->bus->sysdata, addr, size)) +return NULL ;
Do we actually need this fast path? It's going to be slow either way.
Also if a device is doing p2p to another device under the same PHB then it should not be happening via the root complex. Is this a case you've tested?
Quoted 28 lines
arch/powerpc/platforms/powernv/pci-ioda.c +28
-0+list_for_each_entry (hose, &hose_list, list_node) { +struct pnv_phb *phb = hose->private_data; + +if (phb->type !=PNV_PHB_NPU_NVLINK && + phb->type !=PNV_PHB_NPU_OCAPI ) { +if (pnv_pci_controller_owns_addr (hose, addr, size)) +return phb; + } + } +return NULL ; +} + +static u64 pnv_pci_dma_dir_to_opal_p2p (enum dma_data_direction dir) { +if (dir ==DMA_TO_DEVICE ) +return OPAL_PCI_P2P_STORE ; +else if (dir ==DMA_FROM_DEVICE ) +return OPAL_PCI_P2P_LOAD ; +else if (dir ==DMA_BIDIRECTIONAL ) +return OPAL_PCI_P2P_LOAD |OPAL_PCI_P2P_STORE ; +else +return 0 ; +} + +static int pnv_pci_ioda_enable_p2p (struct pci_dev *initiator, +struct pnv_phb *phb_target, +enum dma_data_direction dir) { +struct pci_controller *hose; +struct pnv_phb *phb_init;
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3780
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +9
-0+struct pnv_ioda_pe *pe_init; +u64 desc; +int rc; + +if (!opal_check_token (OPAL_PCI_SET_P2P )) +return -ENXIO ; + + hose =pci_bus_to_host (initiator->bus); + phb_init = hose->private_data;
You can use the pci_bus_to_pnvhb() helper
Quoted 9 lines
arch/powerpc/platforms/powernv/pci-ioda.c +9
-0+ + pe_init =pnv_ioda_get_pe (initiator); +if (!pe_init) +return -ENODEV ; + +if (!pe_init->tce_bypass_enabled) +return -EINVAL ; + +/*
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3799
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+* Configuring the initiator's PHB requires to adjust its TVE#1 +* setting. Since the same device can be an initiator several times for +* different target devices, we need to keep a reference count to know +* when we can restore the default bypass setting on its TVE#1 when +* disabling. Opal is not tracking PE states, so we add a reference +* count on the PE in linux. +* +* For the target, the configuration is per PHB, so we keep a +* target reference count on the PHB. +*/
This irks me a bit because configuring the DMA address limits for the TVE is the kernel's job. What we really should be doing is usingopal_pci_map_pe_dma_window_real() to set the bypass-mode address limit for the TVE to something large enough to hit the MMIO ranges rather than having set_p2p do it as a side effect. Unfortunately, for some reason skiboot doesn't implement support for enabling 56bit addressing using opal_pci_map_pe_dma_window_real() and we do need to support older kernel's which used this stuff so I guess we're stuck with it for now. It'd be nice if we could fix this in the longer term though...
Quoted 30 lines
arch/powerpc/platforms/powernv/pci-ioda.c +30
-0+mutex_lock (&p2p_mutex); + + desc =OPAL_PCI_P2P_ENABLE |pnv_pci_dma_dir_to_opal_p2p (dir); +/* always go to opal to validate the configuration */ + rc =opal_pci_set_p2p (phb_init->opal_id, phb_target->opal_id, desc, + pe_init->pe_number); +if (rc !=OPAL_SUCCESS ) { + rc = -EIO ; + goto out; + } + + pe_init->p2p_initiator_count++; + phb_target->p2p_target_count++; + + rc =0 ; +out: +mutex_unlock (&p2p_mutex); +return rc; +} + +static int pnv_pci_dma_map_resource (struct pci_dev *pdev, +phys_addr_t phys_addr,size_t size, +enum dma_data_direction dir) { +struct pnv_phb *target_phb; + + target_phb =pnv_pci_find_owning_phb (pdev, phys_addr, size); +if (!target_phb) +return 0 ; + +return pnv_pci_ioda_enable_p2p (pdev, target_phb, dir); }
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3843
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+ +static int pnv_pci_ioda_disable_p2p (struct pci_dev *initiator, +struct pnv_phb *phb_target) { +struct pci_controller *hose; +struct pnv_phb *phb_init; +struct pnv_ioda_pe *pe_init; +int rc; + +if (!opal_check_token (OPAL_PCI_SET_P2P )) +return -ENXIO ;
This should probably have a WARN_ON() since we can't hit this path unless the initial map succeeds.
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3838
Quoted 2 lines
arch/powerpc/platforms/powernv/pci-ioda.c +2
-0+ hose =pci_bus_to_host (initiator->bus); + phb_init = hose->private_data;
pci_bus_to_pnvhb()
Quoted 33 lines
arch/powerpc/platforms/powernv/pci-ioda.c +33
-0+ pe_init =pnv_ioda_get_pe (initiator); +if (!pe_init) +return -ENODEV ; + +mutex_lock (&p2p_mutex); + +if (!pe_init->p2p_initiator_count || !phb_target->p2p_target_count) { + rc = -EINVAL ; + goto out; + } + +if (--pe_init->p2p_initiator_count ==0 ) +pnv_pci_ioda2_set_bypass (pe_init, true); + +if (--phb_target->p2p_target_count ==0 ) { + rc =opal_pci_set_p2p (phb_init->opal_id, phb_target->opal_id, +0 , pe_init->pe_number); +if (rc !=OPAL_SUCCESS ) { + rc = -EIO ; + goto out; + } + } + + rc =0 ; +out: +mutex_unlock (&p2p_mutex); +return rc; +} + +static void pnv_pci_dma_unmap_resource (struct pci_dev *pdev, +dma_addr_t addr,size_t size, +enum dma_data_direction dir) { +struct pnv_phb *target_phb;
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3891
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+int rc; + + target_phb =pnv_pci_find_owning_phb (pdev, addr, size); +if (!target_phb) +return ; + + rc =pnv_pci_ioda_disable_p2p (pdev, target_phb); +if (rc) +dev_err (&pdev->dev,"Failed to undo PCI peer-to-peer setup for address %llx: %d\n" , + addr, rc);
Use pci_err() or pe_err().
Re: [PATCH 2/2 v2] powerpc/powernv: Enable and setup PCI P2P
In reply to Oliver O'HalloranLe 03/08/2020 à 09:35, Oliver O'Halloran a écrit :
Quoted 38 lines · Max Gurtovoy
Quoted 37 lines
arch/powerpc/platforms/powernv/pci-ioda.c +29
-0@@ -3706,18 +3706,208 @@ static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) } } +#ifdef CONFIG_PCI_P2PDMA +static DEFINE_MUTEX (p2p_mutex); + +static bool pnv_pci_controller_owns_addr (struct pci_controller *hose, +phys_addr_t addr,size_t size) +{ +int i; + +/* +* It seems safe to assume the full range is under the same PHB, so we +* can ignore the size. +*/ +for (i =0 ; i <ARRAY_SIZE (hose->mem_resources); i++) { +struct resource *res = &hose->mem_resources[i]; + +if (res->flags && addr >= res->start && addr < res->end) +return true; + } +return false; +} + +/* +* find the phb owning a mmio address if not owned locally +*/ +static struct pnv_phb *pnv_pci_find_owning_phb (struct pci_dev *pdev, +phys_addr_t addr,size_t size) +{ +struct pci_controller *hose; +
Quoted 8 lines
arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3740
Quoted 3 lines
arch/powerpc/platforms/powernv/pci-ioda.c +3
-0+/* fast path */ +if (pnv_pci_controller_owns_addr (pdev->bus->sysdata, addr, size)) +return NULL ;Do we actually need this fast path? It's going to be slow either way. Also if a device is doing p2p to another device under the same PHB then it should not be happening via the root complex. Is this a case you've tested?
The "fast path" comment is misleading and we should rephrase. The point is to catch if we're mapping a resource under the same PHB, in which case we don't modify the PHB configuration. So we need to catch it early, but it's not a fast path. If the 2 devices are under the same PHB, the code above shouldn't do anything. So I guess behavior depends on the underlying bridge? We'll need another platform than witherspoon to test it. Probably worth checking.
Quoted 65 lines
Quoted 30 lines
arch/powerpc/platforms/powernv/pci-ioda.c +30
-0+list_for_each_entry (hose, &hose_list, list_node) { +struct pnv_phb *phb = hose->private_data; + +if (phb->type !=PNV_PHB_NPU_NVLINK && + phb->type !=PNV_PHB_NPU_OCAPI ) { +if (pnv_pci_controller_owns_addr (hose, addr, size)) +return phb; + } + } +return NULL ; +} + +static u64 pnv_pci_dma_dir_to_opal_p2p (enum dma_data_direction dir) +{ +if (dir ==DMA_TO_DEVICE ) +return OPAL_PCI_P2P_STORE ; +else if (dir ==DMA_FROM_DEVICE ) +return OPAL_PCI_P2P_LOAD ; +else if (dir ==DMA_BIDIRECTIONAL ) +return OPAL_PCI_P2P_LOAD |OPAL_PCI_P2P_STORE ; +else +return 0 ; +} + +static int pnv_pci_ioda_enable_p2p (struct pci_dev *initiator, +struct pnv_phb *phb_target, +enum dma_data_direction dir) +{ +struct pci_controller *hose; +struct pnv_phb *phb_init;arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3780
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +9
-0+struct pnv_ioda_pe *pe_init; +u64 desc; +int rc; + +if (!opal_check_token (OPAL_PCI_SET_P2P )) +return -ENXIO ; + + hose =pci_bus_to_host (initiator->bus); + phb_init = hose->private_data;You can use the
pci_bus_to_pnvhb()helperQuoted 9 lines
arch/powerpc/platforms/powernv/pci-ioda.c +9
-0+ + pe_init =pnv_ioda_get_pe (initiator); +if (!pe_init) +return -ENODEV ; + +if (!pe_init->tce_bypass_enabled) +return -EINVAL ; + +/* arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3799
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+* Configuring the initiator's PHB requires to adjust its TVE#1 +* setting. Since the same device can be an initiator several times for +* different target devices, we need to keep a reference count to know +* when we can restore the default bypass setting on its TVE#1 when +* disabling. Opal is not tracking PE states, so we add a reference +* count on the PE in linux. +* +* For the target, the configuration is per PHB, so we keep a +* target reference count on the PHB. +*/ This irks me a bit because configuring the DMA address limits for the TVE is the kernel's job. What we really should be doing is using
Quoted 8 lines
opal_pci_map_pe_dma_window_real()to set the bypass-mode address limit for the TVE to something large enough to hit the MMIO ranges rather than havingset_p2pdo it as a side effect. Unfortunately, for some reason skiboot doesn't implement support for enabling 56bit addressing usingopal_pci_map_pe_dma_window_real()and we do need to support older kernel's which used this stuff so I guess we're stuck with it for now. It'd be nice if we could fix this in the longer term though...
OK. We'd need more than a 56-bit opal_pci_map_pe_dma_window_real() though, there's also a queue setting change on the target PHB.
Fred
Quoted 99 lines
Quoted 33 lines
arch/powerpc/platforms/powernv/pci-ioda.c +33
-0+mutex_lock (&p2p_mutex); + + desc =OPAL_PCI_P2P_ENABLE |pnv_pci_dma_dir_to_opal_p2p (dir); +/* always go to opal to validate the configuration */ + rc =opal_pci_set_p2p (phb_init->opal_id, phb_target->opal_id, desc, + pe_init->pe_number); +if (rc !=OPAL_SUCCESS ) { + rc = -EIO ; + goto out; + } + + pe_init->p2p_initiator_count++; + phb_target->p2p_target_count++; + + rc =0 ; +out: +mutex_unlock (&p2p_mutex); +return rc; +} + +static int pnv_pci_dma_map_resource (struct pci_dev *pdev, +phys_addr_t phys_addr,size_t size, +enum dma_data_direction dir) +{ +struct pnv_phb *target_phb; + + target_phb =pnv_pci_find_owning_phb (pdev, phys_addr, size); +if (!target_phb) +return 0 ; + +return pnv_pci_ioda_enable_p2p (pdev, target_phb, dir); +} +arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3843
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+static int pnv_pci_ioda_disable_p2p (struct pci_dev *initiator, +struct pnv_phb *phb_target) +{ +struct pci_controller *hose; +struct pnv_phb *phb_init; +struct pnv_ioda_pe *pe_init; +int rc; + +if (!opal_check_token (OPAL_PCI_SET_P2P )) +return -ENXIO ;This should probably have a
WARN_ON()since we can't hit this path unless the initial map succeeds.arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3844
Quoted 2 lines
arch/powerpc/platforms/powernv/pci-ioda.c +2
-0+ hose =pci_bus_to_host (initiator->bus); + phb_init = hose->private_data;
pci_bus_to_pnvhb()Quoted 34 lines
arch/powerpc/platforms/powernv/pci-ioda.c +34
-0+ pe_init =pnv_ioda_get_pe (initiator); +if (!pe_init) +return -ENODEV ; + +mutex_lock (&p2p_mutex); + +if (!pe_init->p2p_initiator_count || !phb_target->p2p_target_count) { + rc = -EINVAL ; + goto out; + } + +if (--pe_init->p2p_initiator_count ==0 ) +pnv_pci_ioda2_set_bypass (pe_init, true); + +if (--phb_target->p2p_target_count ==0 ) { + rc =opal_pci_set_p2p (phb_init->opal_id, phb_target->opal_id, +0 , pe_init->pe_number); +if (rc !=OPAL_SUCCESS ) { + rc = -EIO ; + goto out; + } + } + + rc =0 ; +out: +mutex_unlock (&p2p_mutex); +return rc; +} + +static void pnv_pci_dma_unmap_resource (struct pci_dev *pdev, +dma_addr_t addr,size_t size, +enum dma_data_direction dir) +{ +struct pnv_phb *target_phb;arch/powerpc/platforms/powernv/pci-ioda.c static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) · line 3891
Quoted 10 lines
arch/powerpc/platforms/powernv/pci-ioda.c +10
-0+int rc; + + target_phb =pnv_pci_find_owning_phb (pdev, addr, size); +if (!target_phb) +return ; + + rc =pnv_pci_ioda_disable_p2p (pdev, target_phb); +if (rc) +dev_err (&pdev->dev,"Failed to undo PCI peer-to-peer setup for address %llx: %d\n" , + addr, rc);Use
pci_err()orpe_err().
Quoted 6 lines · Max Gurtovoy
Define the map_resource/unmap_resource callbacks for the
dma_iommu_opsused by several powerpc platforms. Themap_resourcecallback is called when trying to map a mmio resource through thedma_map_resource()driver API.
Quoted 7 lines
For now, the callback returns an invalid address for devices using translations, but will "direct" map the resource when in bypass mode. Previous behavior for
dma_map_resource()was to always return an invalid address.We also call an optional platform-specific controller op in case some setup is needed for the platform.
Hey Max,
Sorry for not getting to this sooner. Fred has been dutifully nagging me to look at it, but people are constantly throwing stuff at me so it's slipped through the cracks.
Anyway, the changes here are fine IMO. The only real suggestion I have is that we might want to move the direct / bypass mode check out of the arch/powerpc/kernel/dma-iommu.c and into the PHB specific function in pci_controller_ops. I don't see any real reason p2p support should be limited to devices using bypass mode since the data path is the same for translated and untranslated DMAs. We do need to impose that restriction for OPAL / PowerNV IODA PHBs due to the implementation of the opal_pci_set_p2p() has the side effect of forcing the TVE into no-translate mode. However, that's a platform issue so the restriction should be imposed in platform code.
I'd like to fix that, but I'd prefer to do it as a follow up change since I need to have a think about how to fix the firmware bits.