Re: [PATCH 1/2] PCI: endpoint: Enhance pci_epf_alloc_space() and rename to pci_epf_set_inbound_space()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Aug 29, 2025 at 10:51:56PM +0530, ALOK TIWARI wrote:
>
>
> On 8/16/2025 3:50 AM, Frank Li wrote:
> > +int pci_epf_set_inbound_space(struct pci_epf *epf, size_t size,
> > +			      enum pci_barno bar,
> > +			      const struct pci_epc_features *epc_features,
> > +			      enum pci_epc_interface_type type,
> > +			      bool from_memory,
> > +			      dma_addr_t inbound_addr)
> >   {
> >   	u64 bar_fixed_size = epc_features->bar[bar].fixed_size;
> >   	size_t aligned_size, align = epc_features->align;
> > @@ -270,7 +276,32 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
> >   	dma_addr_t phys_addr;
> >   	struct pci_epc *epc;
> >   	struct device *dev;
> > -	void *space;
> > +	void *space = NULL;
> > +	dma_addr_t up;
> > +
> > +	up = inbound_addr + size - 1;
> > +
> > +	/*
> > +	 *  Bits:            15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
> > +	 *  Inbound_addr:    U  U  U  U  U  U  0 X X X X X X X X X
> > +	 *  Up:              U  U  U  U  U  U  1 X X X X X X X X X
> > +	 *
> > +	 *  U means address bits have not change in Range [Inbound_Addr, Up]
> > +	 *  X means bit 0 or 1.
> > +	 *
> > +	 *  Inbound_addr^Up  0  0  0  0  0  0  1 X X X X X X X X X
> > +	 *  Find first bit 1 pos from MSB, 2 ^ pos windows will cover
> > +	 *  [Inbound_Addr, Up] range.
> > +	 */
> > +	if (!from_memory) {
> > +		int pos;
> > +
> > +		for (pos = sizeof(dma_addr_t) - 1; pos > 0; pos--)
> > +			if ((up ^ inbound_addr) & BIT_ULL(pos))
> > +				break;
> > +
>
> sizeof(dma_addr_t) returns bytes, not bits.
> so 7..1 time loop in enough here ?

good find.

Frank
>
> > +		size = 1 << pos;
> > +	}
> >   	if (size < 128)
> >   		size = 128;
> > @@ -283,7 +314,7 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
> >   		if (size > bar_fixed_size) {
> >   			dev_err(&epf->dev,
> >   				"requested BAR size is larger than fixed size\n");
> > -			return NULL;
> > +			return -EINVAL;
> >   		}
> >   		size = bar_fixed_size;
> >   	} else {
> > @@ -308,13 +339,25 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
> >   	}
> >   	dev = epc->dev.parent;
> > -	space = dma_alloc_coherent(dev, aligned_size, &phys_addr, GFP_KERNEL);
> > -	if (!space) {
> > -		dev_err(dev, "failed to allocate mem space\n");
> > -		return NULL;
> > +
> > +	if (from_memory) {
> > +		space = dma_alloc_coherent(dev, aligned_size,
> > +					   &phys_addr, GFP_KERNEL);
> > +		if (!space) {
> > +			dev_err(dev, "failed to allocate mem space\n");
> > +			return -ENOMEM;
> > +		}
> > +	}
> > +
> > +	epf_bar[bar].phys_addr = from_memory ?
> > +			phys_addr : ALIGN_DOWN(inbound_addr, aligned_size);
> > +
> > +	if (!from_memory && (epf_bar[bar].phys_addr + size < up)) {
> > +		dev_err(&epf->dev,
> > +			"Given bar can't fit required inbound memory region\n");
>
> consider bar -> BAR
>
> > +		return -EINVAL;
> >   	}
>
>
> Thanks,
> Alok




[Index of Archives]     [DMA Engine]     [Linux Coverity]     [Linux USB]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Greybus]

  Powered by Linux