diff mbox series

[v3,1/2] PCI: Add enable_device() and disable_device() callbacks for bridges

Message ID 20241024-imx95_lut-v3-1-7509c9bbab86@nxp.com (mailing list archive)
State Superseded
Headers show
Series PCI: add enabe(disable)_device() hook for bridge | expand

Commit Message

Frank Li Oct. 24, 2024, 10:34 p.m. UTC
Some PCIe host bridges require special handling when enabling or disabling
PCIe Endpoints. For example, the i.MX95 platform has a lookup table to map
Requester IDs to StreamIDs, which are used by the SMMU and MSI controller
to identify the source of DMA accesses.

Without this mapping, DMA accesses may target unintended memory, which
would corrupt memory or read the wrong data.

Add a host bridge .enable_device() hook the imx6 driver can use to
configure the Requester ID to StreamID mapping. The hardware table isn't
big enough to map all possible Requester IDs, so this hook may fail if no
table space is available. In that case, return failure from
pci_enable_device().

It might make more sense to make pci_set_master() decline to enable bus
mastering and return failure, but it currently doesn't have a way to return
failure.

Signed-off-by: Frank Li <Frank.Li@nxp.com>
---
Change from v2 to v3
- use Bjorn suggest's commit message.
- call disable_device() when error happen.

Change from v1 to v2
- move enable(disable)device ops to pci_host_bridge
---
 drivers/pci/pci.c   | 23 ++++++++++++++++++++++-
 include/linux/pci.h |  2 ++
 2 files changed, 24 insertions(+), 1 deletion(-)

Comments

Bjorn Helgaas Oct. 30, 2024, 9 p.m. UTC | #1
On Thu, Oct 24, 2024 at 06:34:44PM -0400, Frank Li wrote:
> Some PCIe host bridges require special handling when enabling or disabling
> PCIe Endpoints. For example, the i.MX95 platform has a lookup table to map
> Requester IDs to StreamIDs, which are used by the SMMU and MSI controller
> to identify the source of DMA accesses.
> 
> Without this mapping, DMA accesses may target unintended memory, which
> would corrupt memory or read the wrong data.
> 
> Add a host bridge .enable_device() hook the imx6 driver can use to
> configure the Requester ID to StreamID mapping. The hardware table isn't
> big enough to map all possible Requester IDs, so this hook may fail if no
> table space is available. In that case, return failure from
> pci_enable_device().
> 
> It might make more sense to make pci_set_master() decline to enable bus
> mastering and return failure, but it currently doesn't have a way to return
> failure.
> 
> Signed-off-by: Frank Li <Frank.Li@nxp.com>

Acked-by: Bjorn Helgaas <bhelgaas@google.com>

Merge along with the imx6 change.

> ---
> Change from v2 to v3
> - use Bjorn suggest's commit message.
> - call disable_device() when error happen.
> 
> Change from v1 to v2
> - move enable(disable)device ops to pci_host_bridge
> ---
>  drivers/pci/pci.c   | 23 ++++++++++++++++++++++-
>  include/linux/pci.h |  2 ++
>  2 files changed, 24 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index 7d85c04fbba2a..5e0cb9b6f4d4f 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -2056,6 +2056,7 @@ int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
>  static int do_pci_enable_device(struct pci_dev *dev, int bars)
>  {
>  	int err;
> +	struct pci_host_bridge *host_bridge;
>  	struct pci_dev *bridge;
>  	u16 cmd;
>  	u8 pin;
> @@ -2068,9 +2069,16 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)
>  	if (bridge)
>  		pcie_aspm_powersave_config_link(bridge);
>  
> +	host_bridge = pci_find_host_bridge(dev->bus);
> +	if (host_bridge && host_bridge->enable_device) {
> +		err = host_bridge->enable_device(host_bridge, dev);
> +		if (err)
> +			return err;
> +	}
> +
>  	err = pcibios_enable_device(dev, bars);
>  	if (err < 0)
> -		return err;
> +		goto err_enable;
>  	pci_fixup_device(pci_fixup_enable, dev);
>  
>  	if (dev->msi_enabled || dev->msix_enabled)
> @@ -2085,6 +2093,13 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)
>  	}
>  
>  	return 0;
> +
> +err_enable:
> +	if (host_bridge && host_bridge->disable_device)
> +		 host_bridge->disable_device(host_bridge, dev);
> +
> +	return err;
> +
>  }
>  
>  /**
> @@ -2262,12 +2277,18 @@ void pci_disable_enabled_device(struct pci_dev *dev)
>   */
>  void pci_disable_device(struct pci_dev *dev)
>  {
> +	struct pci_host_bridge *host_bridge;
> +
>  	dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
>  		      "disabling already-disabled device");
>  
>  	if (atomic_dec_return(&dev->enable_cnt) != 0)
>  		return;
>  
> +	host_bridge = pci_find_host_bridge(dev->bus);
> +	if (host_bridge && host_bridge->disable_device)
> +		host_bridge->disable_device(host_bridge, dev);
> +
>  	do_pci_disable_device(dev);
>  
>  	dev->is_busmaster = 0;
> diff --git a/include/linux/pci.h b/include/linux/pci.h
> index 573b4c4c2be61..ac15b02e14ddd 100644
> --- a/include/linux/pci.h
> +++ b/include/linux/pci.h
> @@ -578,6 +578,8 @@ struct pci_host_bridge {
>  	u8 (*swizzle_irq)(struct pci_dev *, u8 *); /* Platform IRQ swizzler */
>  	int (*map_irq)(const struct pci_dev *, u8, u8);
>  	void (*release_fn)(struct pci_host_bridge *);
> +	int (*enable_device)(struct pci_host_bridge *bridge, struct pci_dev *dev);
> +	void (*disable_device)(struct pci_host_bridge *bridge, struct pci_dev *dev);
>  	void		*release_data;
>  	unsigned int	ignore_reset_delay:1;	/* For entire hierarchy */
>  	unsigned int	no_ext_tags:1;		/* No Extended Tags */
> 
> -- 
> 2.34.1
>
Marc Zyngier Nov. 1, 2024, 4:58 p.m. UTC | #2
On Wed, 30 Oct 2024 21:00:43 +0000,
Bjorn Helgaas <helgaas@kernel.org> wrote:
> 
> On Thu, Oct 24, 2024 at 06:34:44PM -0400, Frank Li wrote:
> > Some PCIe host bridges require special handling when enabling or disabling
> > PCIe Endpoints. For example, the i.MX95 platform has a lookup table to map
> > Requester IDs to StreamIDs, which are used by the SMMU and MSI controller
> > to identify the source of DMA accesses.
> > 
> > Without this mapping, DMA accesses may target unintended memory, which
> > would corrupt memory or read the wrong data.
> > 
> > Add a host bridge .enable_device() hook the imx6 driver can use to
> > configure the Requester ID to StreamID mapping. The hardware table isn't
> > big enough to map all possible Requester IDs, so this hook may fail if no
> > table space is available. In that case, return failure from
> > pci_enable_device().
> > 
> > It might make more sense to make pci_set_master() decline to enable bus
> > mastering and return failure, but it currently doesn't have a way to return
> > failure.
> > 
> > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> 
> Acked-by: Bjorn Helgaas <bhelgaas@google.com>
> 
> Merge along with the imx6 change.

It'd be good to have this on a stable branch somewhere so that I can
refer to it when posting the conversion for the Apple driver to that
infrastructure, removing the need for a bus notifier.

Otherwise, I'll just cherry-pick that patch and post the whole thing.

Thanks,

	M.
Manivannan Sadhasivam Nov. 2, 2024, 11:10 a.m. UTC | #3
On Thu, Oct 24, 2024 at 06:34:44PM -0400, Frank Li wrote:
> Some PCIe host bridges require special handling when enabling or disabling
> PCIe Endpoints. For example, the i.MX95 platform has a lookup table to map
> Requester IDs to StreamIDs, which are used by the SMMU and MSI controller
> to identify the source of DMA accesses.
> 
> Without this mapping, DMA accesses may target unintended memory, which
> would corrupt memory or read the wrong data.
> 
> Add a host bridge .enable_device() hook the imx6 driver can use to
> configure the Requester ID to StreamID mapping. The hardware table isn't
> big enough to map all possible Requester IDs, so this hook may fail if no
> table space is available. In that case, return failure from
> pci_enable_device().
> 
> It might make more sense to make pci_set_master() decline to enable bus
> mastering and return failure, but it currently doesn't have a way to return
> failure.
> 
> Signed-off-by: Frank Li <Frank.Li@nxp.com>
> ---
> Change from v2 to v3
> - use Bjorn suggest's commit message.
> - call disable_device() when error happen.
> 
> Change from v1 to v2
> - move enable(disable)device ops to pci_host_bridge
> ---
>  drivers/pci/pci.c   | 23 ++++++++++++++++++++++-
>  include/linux/pci.h |  2 ++
>  2 files changed, 24 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index 7d85c04fbba2a..5e0cb9b6f4d4f 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -2056,6 +2056,7 @@ int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
>  static int do_pci_enable_device(struct pci_dev *dev, int bars)
>  {
>  	int err;
> +	struct pci_host_bridge *host_bridge;
>  	struct pci_dev *bridge;
>  	u16 cmd;
>  	u8 pin;
> @@ -2068,9 +2069,16 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)
>  	if (bridge)
>  		pcie_aspm_powersave_config_link(bridge);
>  
> +	host_bridge = pci_find_host_bridge(dev->bus);
> +	if (host_bridge && host_bridge->enable_device) {
> +		err = host_bridge->enable_device(host_bridge, dev);
> +		if (err)
> +			return err;
> +	}

How about wrapping the enable/disable part in a helper?

	int pci_host_bridge_enable_device(dev);
	void pci_host_bridge_disable_device(dev);

The definition could be placed in drivers/pci/pci.h as an inline function.

- Mani

> +
>  	err = pcibios_enable_device(dev, bars);
>  	if (err < 0)
> -		return err;
> +		goto err_enable;
>  	pci_fixup_device(pci_fixup_enable, dev);
>  
>  	if (dev->msi_enabled || dev->msix_enabled)
> @@ -2085,6 +2093,13 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)
>  	}
>  
>  	return 0;
> +
> +err_enable:
> +	if (host_bridge && host_bridge->disable_device)
> +		 host_bridge->disable_device(host_bridge, dev);
> +
> +	return err;
> +
>  }
>  
>  /**
> @@ -2262,12 +2277,18 @@ void pci_disable_enabled_device(struct pci_dev *dev)
>   */
>  void pci_disable_device(struct pci_dev *dev)
>  {
> +	struct pci_host_bridge *host_bridge;
> +
>  	dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
>  		      "disabling already-disabled device");
>  
>  	if (atomic_dec_return(&dev->enable_cnt) != 0)
>  		return;
>  
> +	host_bridge = pci_find_host_bridge(dev->bus);
> +	if (host_bridge && host_bridge->disable_device)
> +		host_bridge->disable_device(host_bridge, dev);
> +
>  	do_pci_disable_device(dev);
>  
>  	dev->is_busmaster = 0;
> diff --git a/include/linux/pci.h b/include/linux/pci.h
> index 573b4c4c2be61..ac15b02e14ddd 100644
> --- a/include/linux/pci.h
> +++ b/include/linux/pci.h
> @@ -578,6 +578,8 @@ struct pci_host_bridge {
>  	u8 (*swizzle_irq)(struct pci_dev *, u8 *); /* Platform IRQ swizzler */
>  	int (*map_irq)(const struct pci_dev *, u8, u8);
>  	void (*release_fn)(struct pci_host_bridge *);
> +	int (*enable_device)(struct pci_host_bridge *bridge, struct pci_dev *dev);
> +	void (*disable_device)(struct pci_host_bridge *bridge, struct pci_dev *dev);
>  	void		*release_data;
>  	unsigned int	ignore_reset_delay:1;	/* For entire hierarchy */
>  	unsigned int	no_ext_tags:1;		/* No Extended Tags */
> 
> -- 
> 2.34.1
>
Marc Zyngier Nov. 2, 2024, 11:32 a.m. UTC | #4
On Sat, 02 Nov 2024 11:10:12 +0000,
Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> wrote:
> 
> On Thu, Oct 24, 2024 at 06:34:44PM -0400, Frank Li wrote:
> > Some PCIe host bridges require special handling when enabling or disabling
> > PCIe Endpoints. For example, the i.MX95 platform has a lookup table to map
> > Requester IDs to StreamIDs, which are used by the SMMU and MSI controller
> > to identify the source of DMA accesses.
> > 
> > Without this mapping, DMA accesses may target unintended memory, which
> > would corrupt memory or read the wrong data.
> > 
> > Add a host bridge .enable_device() hook the imx6 driver can use to
> > configure the Requester ID to StreamID mapping. The hardware table isn't
> > big enough to map all possible Requester IDs, so this hook may fail if no
> > table space is available. In that case, return failure from
> > pci_enable_device().
> > 
> > It might make more sense to make pci_set_master() decline to enable bus
> > mastering and return failure, but it currently doesn't have a way to return
> > failure.
> > 
> > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> > ---
> > Change from v2 to v3
> > - use Bjorn suggest's commit message.
> > - call disable_device() when error happen.
> > 
> > Change from v1 to v2
> > - move enable(disable)device ops to pci_host_bridge
> > ---
> >  drivers/pci/pci.c   | 23 ++++++++++++++++++++++-
> >  include/linux/pci.h |  2 ++
> >  2 files changed, 24 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> > index 7d85c04fbba2a..5e0cb9b6f4d4f 100644
> > --- a/drivers/pci/pci.c
> > +++ b/drivers/pci/pci.c
> > @@ -2056,6 +2056,7 @@ int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
> >  static int do_pci_enable_device(struct pci_dev *dev, int bars)
> >  {
> >  	int err;
> > +	struct pci_host_bridge *host_bridge;
> >  	struct pci_dev *bridge;
> >  	u16 cmd;
> >  	u8 pin;
> > @@ -2068,9 +2069,16 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)
> >  	if (bridge)
> >  		pcie_aspm_powersave_config_link(bridge);
> >  
> > +	host_bridge = pci_find_host_bridge(dev->bus);
> > +	if (host_bridge && host_bridge->enable_device) {
> > +		err = host_bridge->enable_device(host_bridge, dev);
> > +		if (err)
> > +			return err;
> > +	}
> 
> How about wrapping the enable/disable part in a helper?
> 
> 	int pci_host_bridge_enable_device(dev);
> 	void pci_host_bridge_disable_device(dev);
> 
> The definition could be placed in drivers/pci/pci.h as an inline
> function.

What does it bring? I would see the point if there was another user.
But this is very much core infrastructure which doesn't lend itself to
duplication.

Unless you have something in mind?

	M.
Manivannan Sadhasivam Nov. 2, 2024, 11:54 a.m. UTC | #5
On Sat, Nov 02, 2024 at 11:32:37AM +0000, Marc Zyngier wrote:
> On Sat, 02 Nov 2024 11:10:12 +0000,
> Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> wrote:
> > 
> > On Thu, Oct 24, 2024 at 06:34:44PM -0400, Frank Li wrote:
> > > Some PCIe host bridges require special handling when enabling or disabling
> > > PCIe Endpoints. For example, the i.MX95 platform has a lookup table to map
> > > Requester IDs to StreamIDs, which are used by the SMMU and MSI controller
> > > to identify the source of DMA accesses.
> > > 
> > > Without this mapping, DMA accesses may target unintended memory, which
> > > would corrupt memory or read the wrong data.
> > > 
> > > Add a host bridge .enable_device() hook the imx6 driver can use to
> > > configure the Requester ID to StreamID mapping. The hardware table isn't
> > > big enough to map all possible Requester IDs, so this hook may fail if no
> > > table space is available. In that case, return failure from
> > > pci_enable_device().
> > > 
> > > It might make more sense to make pci_set_master() decline to enable bus
> > > mastering and return failure, but it currently doesn't have a way to return
> > > failure.
> > > 
> > > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> > > ---
> > > Change from v2 to v3
> > > - use Bjorn suggest's commit message.
> > > - call disable_device() when error happen.
> > > 
> > > Change from v1 to v2
> > > - move enable(disable)device ops to pci_host_bridge
> > > ---
> > >  drivers/pci/pci.c   | 23 ++++++++++++++++++++++-
> > >  include/linux/pci.h |  2 ++
> > >  2 files changed, 24 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> > > index 7d85c04fbba2a..5e0cb9b6f4d4f 100644
> > > --- a/drivers/pci/pci.c
> > > +++ b/drivers/pci/pci.c
> > > @@ -2056,6 +2056,7 @@ int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
> > >  static int do_pci_enable_device(struct pci_dev *dev, int bars)
> > >  {
> > >  	int err;
> > > +	struct pci_host_bridge *host_bridge;
> > >  	struct pci_dev *bridge;
> > >  	u16 cmd;
> > >  	u8 pin;
> > > @@ -2068,9 +2069,16 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)
> > >  	if (bridge)
> > >  		pcie_aspm_powersave_config_link(bridge);
> > >  
> > > +	host_bridge = pci_find_host_bridge(dev->bus);
> > > +	if (host_bridge && host_bridge->enable_device) {
> > > +		err = host_bridge->enable_device(host_bridge, dev);
> > > +		if (err)
> > > +			return err;
> > > +	}
> > 
> > How about wrapping the enable/disable part in a helper?
> > 
> > 	int pci_host_bridge_enable_device(dev);
> > 	void pci_host_bridge_disable_device(dev);
> > 
> > The definition could be placed in drivers/pci/pci.h as an inline
> > function.
> 
> What does it bring? I would see the point if there was another user.
> But this is very much core infrastructure which doesn't lend itself to
> duplication.
> 
> Unless you have something in mind?
> 

IMO, it adds a nice encapsulation to help readers understand what this piece of
code is all about and also keeps the callers short. Plus the disable helper is
reused in both error and pci_disable_device() (if that matters).

- Mani
Marc Zyngier Nov. 2, 2024, 12:24 p.m. UTC | #6
On Sat, 02 Nov 2024 11:54:35 +0000,
Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> wrote:
> 
> On Sat, Nov 02, 2024 at 11:32:37AM +0000, Marc Zyngier wrote:
> > On Sat, 02 Nov 2024 11:10:12 +0000,
> > Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> wrote:
> > > 
> > > On Thu, Oct 24, 2024 at 06:34:44PM -0400, Frank Li wrote:
> > > > Some PCIe host bridges require special handling when enabling or disabling
> > > > PCIe Endpoints. For example, the i.MX95 platform has a lookup table to map
> > > > Requester IDs to StreamIDs, which are used by the SMMU and MSI controller
> > > > to identify the source of DMA accesses.
> > > > 
> > > > Without this mapping, DMA accesses may target unintended memory, which
> > > > would corrupt memory or read the wrong data.
> > > > 
> > > > Add a host bridge .enable_device() hook the imx6 driver can use to
> > > > configure the Requester ID to StreamID mapping. The hardware table isn't
> > > > big enough to map all possible Requester IDs, so this hook may fail if no
> > > > table space is available. In that case, return failure from
> > > > pci_enable_device().
> > > > 
> > > > It might make more sense to make pci_set_master() decline to enable bus
> > > > mastering and return failure, but it currently doesn't have a way to return
> > > > failure.
> > > > 
> > > > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> > > > ---
> > > > Change from v2 to v3
> > > > - use Bjorn suggest's commit message.
> > > > - call disable_device() when error happen.
> > > > 
> > > > Change from v1 to v2
> > > > - move enable(disable)device ops to pci_host_bridge
> > > > ---
> > > >  drivers/pci/pci.c   | 23 ++++++++++++++++++++++-
> > > >  include/linux/pci.h |  2 ++
> > > >  2 files changed, 24 insertions(+), 1 deletion(-)
> > > > 
> > > > diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> > > > index 7d85c04fbba2a..5e0cb9b6f4d4f 100644
> > > > --- a/drivers/pci/pci.c
> > > > +++ b/drivers/pci/pci.c
> > > > @@ -2056,6 +2056,7 @@ int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
> > > >  static int do_pci_enable_device(struct pci_dev *dev, int bars)
> > > >  {
> > > >  	int err;
> > > > +	struct pci_host_bridge *host_bridge;
> > > >  	struct pci_dev *bridge;
> > > >  	u16 cmd;
> > > >  	u8 pin;
> > > > @@ -2068,9 +2069,16 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)
> > > >  	if (bridge)
> > > >  		pcie_aspm_powersave_config_link(bridge);
> > > >  
> > > > +	host_bridge = pci_find_host_bridge(dev->bus);
> > > > +	if (host_bridge && host_bridge->enable_device) {
> > > > +		err = host_bridge->enable_device(host_bridge, dev);
> > > > +		if (err)
> > > > +			return err;
> > > > +	}
> > > 
> > > How about wrapping the enable/disable part in a helper?
> > > 
> > > 	int pci_host_bridge_enable_device(dev);
> > > 	void pci_host_bridge_disable_device(dev);
> > > 
> > > The definition could be placed in drivers/pci/pci.h as an inline
> > > function.
> > 
> > What does it bring? I would see the point if there was another user.
> > But this is very much core infrastructure which doesn't lend itself to
> > duplication.
> > 
> > Unless you have something in mind?
> > 
> 
> IMO, it adds a nice encapsulation to help readers understand what this piece of
> code is all about and also keeps the callers short. Plus the disable helper is
> reused in both error and pci_disable_device() (if that matters).

Having an *internal* helper for disable definitely has its use.

But moving these helpers outside of pci.c opens the door to all sort
of abuse by making it look like an internal API drivers can use, which
is absolutely isn't.

	M.
Manivannan Sadhasivam Nov. 2, 2024, 12:48 p.m. UTC | #7
On Sat, Nov 02, 2024 at 12:24:42PM +0000, Marc Zyngier wrote:
> On Sat, 02 Nov 2024 11:54:35 +0000,
> Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> wrote:
> > 
> > On Sat, Nov 02, 2024 at 11:32:37AM +0000, Marc Zyngier wrote:
> > > On Sat, 02 Nov 2024 11:10:12 +0000,
> > > Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> wrote:
> > > > 
> > > > On Thu, Oct 24, 2024 at 06:34:44PM -0400, Frank Li wrote:
> > > > > Some PCIe host bridges require special handling when enabling or disabling
> > > > > PCIe Endpoints. For example, the i.MX95 platform has a lookup table to map
> > > > > Requester IDs to StreamIDs, which are used by the SMMU and MSI controller
> > > > > to identify the source of DMA accesses.
> > > > > 
> > > > > Without this mapping, DMA accesses may target unintended memory, which
> > > > > would corrupt memory or read the wrong data.
> > > > > 
> > > > > Add a host bridge .enable_device() hook the imx6 driver can use to
> > > > > configure the Requester ID to StreamID mapping. The hardware table isn't
> > > > > big enough to map all possible Requester IDs, so this hook may fail if no
> > > > > table space is available. In that case, return failure from
> > > > > pci_enable_device().
> > > > > 
> > > > > It might make more sense to make pci_set_master() decline to enable bus
> > > > > mastering and return failure, but it currently doesn't have a way to return
> > > > > failure.
> > > > > 
> > > > > Signed-off-by: Frank Li <Frank.Li@nxp.com>
> > > > > ---
> > > > > Change from v2 to v3
> > > > > - use Bjorn suggest's commit message.
> > > > > - call disable_device() when error happen.
> > > > > 
> > > > > Change from v1 to v2
> > > > > - move enable(disable)device ops to pci_host_bridge
> > > > > ---
> > > > >  drivers/pci/pci.c   | 23 ++++++++++++++++++++++-
> > > > >  include/linux/pci.h |  2 ++
> > > > >  2 files changed, 24 insertions(+), 1 deletion(-)
> > > > > 
> > > > > diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> > > > > index 7d85c04fbba2a..5e0cb9b6f4d4f 100644
> > > > > --- a/drivers/pci/pci.c
> > > > > +++ b/drivers/pci/pci.c
> > > > > @@ -2056,6 +2056,7 @@ int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
> > > > >  static int do_pci_enable_device(struct pci_dev *dev, int bars)
> > > > >  {
> > > > >  	int err;
> > > > > +	struct pci_host_bridge *host_bridge;
> > > > >  	struct pci_dev *bridge;
> > > > >  	u16 cmd;
> > > > >  	u8 pin;
> > > > > @@ -2068,9 +2069,16 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars)
> > > > >  	if (bridge)
> > > > >  		pcie_aspm_powersave_config_link(bridge);
> > > > >  
> > > > > +	host_bridge = pci_find_host_bridge(dev->bus);
> > > > > +	if (host_bridge && host_bridge->enable_device) {
> > > > > +		err = host_bridge->enable_device(host_bridge, dev);
> > > > > +		if (err)
> > > > > +			return err;
> > > > > +	}
> > > > 
> > > > How about wrapping the enable/disable part in a helper?
> > > > 
> > > > 	int pci_host_bridge_enable_device(dev);
> > > > 	void pci_host_bridge_disable_device(dev);
> > > > 
> > > > The definition could be placed in drivers/pci/pci.h as an inline
> > > > function.
> > > 
> > > What does it bring? I would see the point if there was another user.
> > > But this is very much core infrastructure which doesn't lend itself to
> > > duplication.
> > > 
> > > Unless you have something in mind?
> > > 
> > 
> > IMO, it adds a nice encapsulation to help readers understand what this piece of
> > code is all about and also keeps the callers short. Plus the disable helper is
> > reused in both error and pci_disable_device() (if that matters).
> 
> Having an *internal* helper for disable definitely has its use.
> 
> But moving these helpers outside of pci.c opens the door to all sort
> of abuse by making it look like an internal API drivers can use, which
> is absolutely isn't.
> 

Hmm, agree with this part, thanks!

Frank, please keep the helpers in pci.c.

- Mani
diff mbox series

Patch

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 7d85c04fbba2a..5e0cb9b6f4d4f 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -2056,6 +2056,7 @@  int __weak pcibios_enable_device(struct pci_dev *dev, int bars)
 static int do_pci_enable_device(struct pci_dev *dev, int bars)
 {
 	int err;
+	struct pci_host_bridge *host_bridge;
 	struct pci_dev *bridge;
 	u16 cmd;
 	u8 pin;
@@ -2068,9 +2069,16 @@  static int do_pci_enable_device(struct pci_dev *dev, int bars)
 	if (bridge)
 		pcie_aspm_powersave_config_link(bridge);
 
+	host_bridge = pci_find_host_bridge(dev->bus);
+	if (host_bridge && host_bridge->enable_device) {
+		err = host_bridge->enable_device(host_bridge, dev);
+		if (err)
+			return err;
+	}
+
 	err = pcibios_enable_device(dev, bars);
 	if (err < 0)
-		return err;
+		goto err_enable;
 	pci_fixup_device(pci_fixup_enable, dev);
 
 	if (dev->msi_enabled || dev->msix_enabled)
@@ -2085,6 +2093,13 @@  static int do_pci_enable_device(struct pci_dev *dev, int bars)
 	}
 
 	return 0;
+
+err_enable:
+	if (host_bridge && host_bridge->disable_device)
+		 host_bridge->disable_device(host_bridge, dev);
+
+	return err;
+
 }
 
 /**
@@ -2262,12 +2277,18 @@  void pci_disable_enabled_device(struct pci_dev *dev)
  */
 void pci_disable_device(struct pci_dev *dev)
 {
+	struct pci_host_bridge *host_bridge;
+
 	dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0,
 		      "disabling already-disabled device");
 
 	if (atomic_dec_return(&dev->enable_cnt) != 0)
 		return;
 
+	host_bridge = pci_find_host_bridge(dev->bus);
+	if (host_bridge && host_bridge->disable_device)
+		host_bridge->disable_device(host_bridge, dev);
+
 	do_pci_disable_device(dev);
 
 	dev->is_busmaster = 0;
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 573b4c4c2be61..ac15b02e14ddd 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -578,6 +578,8 @@  struct pci_host_bridge {
 	u8 (*swizzle_irq)(struct pci_dev *, u8 *); /* Platform IRQ swizzler */
 	int (*map_irq)(const struct pci_dev *, u8, u8);
 	void (*release_fn)(struct pci_host_bridge *);
+	int (*enable_device)(struct pci_host_bridge *bridge, struct pci_dev *dev);
+	void (*disable_device)(struct pci_host_bridge *bridge, struct pci_dev *dev);
 	void		*release_data;
 	unsigned int	ignore_reset_delay:1;	/* For entire hierarchy */
 	unsigned int	no_ext_tags:1;		/* No Extended Tags */