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

Commit f13e143e authored by Christian König's avatar Christian König
Browse files

dma-buf: start caching of sg_table objects v2



To allow a smooth transition from pinning buffer objects to dynamic
invalidation we first start to cache the sg_table for an attachment.

v2: keep closer to the DRM implementation

Signed-off-by: default avatarChristian König <christian.koenig@amd.com>
Reviewed-by: default avatarDaniel Vetter <daniel.vetter@ffwll.ch>
Link: https://patchwork.kernel.org/patch/10943053/
parent 0c7b178a
Loading
Loading
Loading
Loading
+25 −2
Original line number Original line Diff line number Diff line
@@ -576,6 +576,7 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
	list_add(&attach->node, &dmabuf->attachments);
	list_add(&attach->node, &dmabuf->attachments);


	mutex_unlock(&dmabuf->lock);
	mutex_unlock(&dmabuf->lock);

	return attach;
	return attach;


err_attach:
err_attach:
@@ -598,6 +599,9 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
	if (WARN_ON(!dmabuf || !attach))
	if (WARN_ON(!dmabuf || !attach))
		return;
		return;


	if (attach->sgt)
		dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);

	mutex_lock(&dmabuf->lock);
	mutex_lock(&dmabuf->lock);
	list_del(&attach->node);
	list_del(&attach->node);
	if (dmabuf->ops->detach)
	if (dmabuf->ops->detach)
@@ -633,10 +637,27 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
	if (WARN_ON(!attach || !attach->dmabuf))
	if (WARN_ON(!attach || !attach->dmabuf))
		return ERR_PTR(-EINVAL);
		return ERR_PTR(-EINVAL);


	if (attach->sgt) {
		/*
		 * Two mappings with different directions for the same
		 * attachment are not allowed.
		 */
		if (attach->dir != direction &&
		    attach->dir != DMA_BIDIRECTIONAL)
			return ERR_PTR(-EBUSY);

		return attach->sgt;
	}

	sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
	sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
	if (!sg_table)
	if (!sg_table)
		sg_table = ERR_PTR(-ENOMEM);
		sg_table = ERR_PTR(-ENOMEM);


	if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
		attach->sgt = sg_table;
		attach->dir = direction;
	}

	return sg_table;
	return sg_table;
}
}
EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
@@ -660,8 +681,10 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
	if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
	if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
		return;
		return;


	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
	if (attach->sgt == sg_table)
						direction);
		return;

	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
}
}
EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);


+13 −0
Original line number Original line Diff line number Diff line
@@ -44,6 +44,15 @@ struct dma_buf_attachment;
 * @vunmap: [optional] unmaps a vmap from the buffer
 * @vunmap: [optional] unmaps a vmap from the buffer
 */
 */
struct dma_buf_ops {
struct dma_buf_ops {
	/**
	  * @cache_sgt_mapping:
	  *
	  * If true the framework will cache the first mapping made for each
	  * attachment. This avoids creating mappings for attachments multiple
	  * times.
	  */
	bool cache_sgt_mapping;

	/**
	/**
	 * @attach:
	 * @attach:
	 *
	 *
@@ -323,6 +332,8 @@ struct dma_buf {
 * @dmabuf: buffer for this attachment.
 * @dmabuf: buffer for this attachment.
 * @dev: device attached to the buffer.
 * @dev: device attached to the buffer.
 * @node: list of dma_buf_attachment.
 * @node: list of dma_buf_attachment.
 * @sgt: cached mapping.
 * @dir: direction of cached mapping.
 * @priv: exporter specific attachment data.
 * @priv: exporter specific attachment data.
 *
 *
 * This structure holds the attachment information between the dma_buf buffer
 * This structure holds the attachment information between the dma_buf buffer
@@ -338,6 +349,8 @@ struct dma_buf_attachment {
	struct dma_buf *dmabuf;
	struct dma_buf *dmabuf;
	struct device *dev;
	struct device *dev;
	struct list_head node;
	struct list_head node;
	struct sg_table *sgt;
	enum dma_data_direction dir;
	void *priv;
	void *priv;
};
};