Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fc5d1829 authored by Aaron Sierra's avatar Aaron Sierra Committed by Jon Mason
Browse files

NTB: transport: Try harder to alloc an aligned MW buffer



Be a little wasteful if the (likely CMA) message window buffer is not
suitably aligned after our first attempt; allocate a buffer twice as big
as we need and manually align our MW buffer within it.

This was needed on Intel Broadwell DE platforms with intel_iommu=off

Signed-off-by: default avatarAaron Sierra <asierra@xes-inc.com>
Reviewed-by: default avatarDave Jiang <dave.jiang@intel.com>
Signed-off-by: default avatarJon Mason <jdmason@kudzu.us>
parent 846429bc
Loading
Loading
Loading
Loading
+63 −23
Original line number Original line Diff line number Diff line
@@ -194,6 +194,8 @@ struct ntb_transport_mw {
	void __iomem *vbase;
	void __iomem *vbase;
	size_t xlat_size;
	size_t xlat_size;
	size_t buff_size;
	size_t buff_size;
	size_t alloc_size;
	void *alloc_addr;
	void *virt_addr;
	void *virt_addr;
	dma_addr_t dma_addr;
	dma_addr_t dma_addr;
};
};
@@ -672,13 +674,59 @@ static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
		return;
		return;


	ntb_mw_clear_trans(nt->ndev, PIDX, num_mw);
	ntb_mw_clear_trans(nt->ndev, PIDX, num_mw);
	dma_free_coherent(&pdev->dev, mw->buff_size,
	dma_free_coherent(&pdev->dev, mw->alloc_size,
			  mw->virt_addr, mw->dma_addr);
			  mw->alloc_addr, mw->dma_addr);
	mw->xlat_size = 0;
	mw->xlat_size = 0;
	mw->buff_size = 0;
	mw->buff_size = 0;
	mw->alloc_size = 0;
	mw->alloc_addr = NULL;
	mw->virt_addr = NULL;
	mw->virt_addr = NULL;
}
}


static int ntb_alloc_mw_buffer(struct ntb_transport_mw *mw,
			       struct device *dma_dev, size_t align)
{
	dma_addr_t dma_addr;
	void *alloc_addr, *virt_addr;
	int rc;

	alloc_addr = dma_alloc_coherent(dma_dev, mw->alloc_size,
					&dma_addr, GFP_KERNEL);
	if (!alloc_addr) {
		dev_err(dma_dev, "Unable to alloc MW buff of size %zu\n",
			mw->alloc_size);
		return -ENOMEM;
	}
	virt_addr = alloc_addr;

	/*
	 * we must ensure that the memory address allocated is BAR size
	 * aligned in order for the XLAT register to take the value. This
	 * is a requirement of the hardware. It is recommended to setup CMA
	 * for BAR sizes equal or greater than 4MB.
	 */
	if (!IS_ALIGNED(dma_addr, align)) {
		if (mw->alloc_size > mw->buff_size) {
			virt_addr = PTR_ALIGN(alloc_addr, align);
			dma_addr = ALIGN(dma_addr, align);
		} else {
			rc = -ENOMEM;
			goto err;
		}
	}

	mw->alloc_addr = alloc_addr;
	mw->virt_addr = virt_addr;
	mw->dma_addr = dma_addr;

	return 0;

err:
	dma_free_coherent(dma_dev, mw->alloc_size, alloc_addr, dma_addr);

	return rc;
}

static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
		      resource_size_t size)
		      resource_size_t size)
{
{
@@ -710,28 +758,20 @@ static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
	/* Alloc memory for receiving data.  Must be aligned */
	/* Alloc memory for receiving data.  Must be aligned */
	mw->xlat_size = xlat_size;
	mw->xlat_size = xlat_size;
	mw->buff_size = buff_size;
	mw->buff_size = buff_size;
	mw->alloc_size = buff_size;


	mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
	rc = ntb_alloc_mw_buffer(mw, &pdev->dev, xlat_align);
					   &mw->dma_addr, GFP_KERNEL);
	if (rc) {
	if (!mw->virt_addr) {
		mw->alloc_size *= 2;
		rc = ntb_alloc_mw_buffer(mw, &pdev->dev, xlat_align);
		if (rc) {
			dev_err(&pdev->dev,
				"Unable to alloc aligned MW buff\n");
			mw->xlat_size = 0;
			mw->xlat_size = 0;
			mw->buff_size = 0;
			mw->buff_size = 0;
		dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
			mw->alloc_size = 0;
			buff_size);
			return rc;
		return -ENOMEM;
		}
		}

	/*
	 * we must ensure that the memory address allocated is BAR size
	 * aligned in order for the XLAT register to take the value. This
	 * is a requirement of the hardware. It is recommended to setup CMA
	 * for BAR sizes equal or greater than 4MB.
	 */
	if (!IS_ALIGNED(mw->dma_addr, xlat_align)) {
		dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
			&mw->dma_addr);
		ntb_free_mw(nt, num_mw);
		return -ENOMEM;
	}
	}


	/* Notify HW the memory location of the receive buffer */
	/* Notify HW the memory location of the receive buffer */