Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dGPU SR-IOV support for virtio-GPU #18

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions drivers/dma-buf/dma-buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,94 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
}
EXPORT_SYMBOL_NS_GPL(dma_buf_dynamic_attach, DMA_BUF);

/**
* This is simply a replication of dma_buf_dynamic_attach with parameter p2p
* added, which allows us to set attach->peer2peer without implementing
* move_notify callback of importer_ops.
*/
struct dma_buf_attachment *
____dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
const struct dma_buf_attach_ops *importer_ops,
void *importer_priv, bool p2p)
{
struct dma_buf_attachment *attach;
int ret;

if (WARN_ON(!dmabuf || !dev))
return ERR_PTR(-EINVAL);

if (WARN_ON(importer_ops && !importer_ops->move_notify))
return ERR_PTR(-EINVAL);

attach = kzalloc(sizeof(*attach), GFP_KERNEL);
if (!attach)
return ERR_PTR(-ENOMEM);

attach->dev = dev;
attach->dmabuf = dmabuf;
if (importer_ops)
attach->peer2peer = importer_ops->allow_peer2peer;
if (p2p)
attach->peer2peer = p2p;
attach->importer_ops = importer_ops;
attach->importer_priv = importer_priv;

if (dmabuf->ops->attach) {
ret = dmabuf->ops->attach(dmabuf, attach);
if (ret)
goto err_attach;
}
dma_resv_lock(dmabuf->resv, NULL);
list_add(&attach->node, &dmabuf->attachments);
dma_resv_unlock(dmabuf->resv);

/* When either the importer or the exporter can't handle dynamic
* mappings we cache the mapping here to avoid issues with the
* reservation object lock.
*/
if (dma_buf_attachment_is_dynamic(attach) !=
dma_buf_is_dynamic(dmabuf)) {
struct sg_table *sgt;

if (dma_buf_is_dynamic(attach->dmabuf)) {
dma_resv_lock(attach->dmabuf->resv, NULL);
ret = dmabuf->ops->pin(attach);
if (ret)
goto err_unlock;
}

sgt = __map_dma_buf(attach, DMA_BIDIRECTIONAL);
if (!sgt)
sgt = ERR_PTR(-ENOMEM);
if (IS_ERR(sgt)) {
ret = PTR_ERR(sgt);
goto err_unpin;
}
if (dma_buf_is_dynamic(attach->dmabuf))
dma_resv_unlock(attach->dmabuf->resv);
attach->sgt = sgt;
attach->dir = DMA_BIDIRECTIONAL;
}

return attach;

err_attach:
kfree(attach);
return ERR_PTR(ret);

err_unpin:
if (dma_buf_is_dynamic(attach->dmabuf))
dmabuf->ops->unpin(attach);

err_unlock:
if (dma_buf_is_dynamic(attach->dmabuf))
dma_resv_unlock(attach->dmabuf->resv);

dma_buf_detach(dmabuf, attach);
return ERR_PTR(ret);
}
EXPORT_SYMBOL_NS_GPL(____dma_buf_dynamic_attach, DMA_BUF);

/**
* dma_buf_attach - Wrapper for dma_buf_dynamic_attach
* @dmabuf: [in] buffer to attach device to.
Expand Down
3 changes: 3 additions & 0 deletions drivers/gpu/drm/virtio/virtgpu_debugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ static int virtio_gpu_features(struct seq_file *m, void *data)

virtio_gpu_add_bool(m, "blob resources", vgdev->has_resource_blob);
virtio_gpu_add_bool(m, "context init", vgdev->has_context_init);
virtio_gpu_add_bool(m, "scaling", vgdev->has_scaling);
virtio_gpu_add_bool(m, "allow_p2p", vgdev->has_allow_p2p);
virtio_gpu_add_bool(m, "modifier", vgdev->has_modifier);
virtio_gpu_add_int(m, "cap sets", vgdev->num_capsets);
virtio_gpu_add_int(m, "scanouts", vgdev->num_scanouts);
if (vgdev->host_visible_region.len) {
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/virtio/virtgpu_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ static unsigned int features[] = {
VIRTIO_GPU_F_MODIFIER,
VIRTIO_GPU_F_SCALING,
VIRTIO_GPU_F_VBLANK,
VIRTIO_GPU_F_ALLOW_P2P,
};

#ifdef CONFIG_PM_SLEEP
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/virtio/virtgpu_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ struct virtio_gpu_device {
bool has_modifier;
bool has_scaling;
bool has_vblank;
bool has_allow_p2p;
bool has_indirect;
bool has_resource_assign_uuid;
bool has_resource_blob;
Expand Down
3 changes: 3 additions & 0 deletions drivers/gpu/drm/virtio/virtgpu_kms.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_VBLANK)) {
vgdev->has_vblank = true;
}
if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_ALLOW_P2P)) {
vgdev->has_allow_p2p = true;
}
if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_RESOURCE_BLOB)) {
vgdev->has_resource_blob = true;
if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_MODIFIER)) {
Expand Down
2 changes: 2 additions & 0 deletions drivers/gpu/drm/virtio/virtgpu_plane.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ static const uint64_t virtio_gpu_format_modifiers[] = {
DRM_FORMAT_MOD_LINEAR,
I915_FORMAT_MOD_X_TILED,
I915_FORMAT_MOD_Y_TILED,
I915_FORMAT_MOD_4_TILED,
DRM_FORMAT_MOD_INVALID
};

Expand All @@ -413,6 +414,7 @@ static bool virtio_gpu_plane_format_mod_supported(struct drm_plane *_plane,
case DRM_FORMAT_MOD_LINEAR:
case I915_FORMAT_MOD_X_TILED:
case I915_FORMAT_MOD_Y_TILED:
case I915_FORMAT_MOD_4_TILED:
return true;
default:
return false;
Expand Down
58 changes: 53 additions & 5 deletions drivers/gpu/drm/virtio/virtgpu_prime.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,17 @@ struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj,
}

struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
struct dma_buf *buf)
struct dma_buf *dma_buf)
{
struct drm_gem_object *obj;
struct dma_buf_attachment *attach;
struct sg_table *sgt;
struct device *attach_dev = dev->dev;
struct virtio_gpu_device *vgdev = dev->dev_private;
int ret;

if (buf->ops == &virtgpu_dmabuf_ops.ops) {
obj = buf->priv;
if (dma_buf->ops == &virtgpu_dmabuf_ops.ops) {
obj = dma_buf->priv;
if (obj->dev == dev) {
/*
* Importing dmabuf exported from our own gem increases
Expand All @@ -159,7 +164,39 @@ struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
}
}

return drm_gem_prime_import(dev, buf);
if (!dev->driver->gem_prime_import_sg_table)
return ERR_PTR(-EINVAL);
attach = ____dma_buf_dynamic_attach(dma_buf, attach_dev, NULL, NULL,
vgdev->has_allow_p2p);
if (IS_ERR(attach))
return ERR_CAST(attach);

get_dma_buf(dma_buf);

sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sgt)) {
ret = PTR_ERR(sgt);
goto fail_detach;
}

obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
if (IS_ERR(obj)) {
ret = PTR_ERR(obj);
goto fail_unmap;
}

obj->import_attach = attach;
obj->resv = dma_buf->resv;

return obj;

fail_unmap:
dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
fail_detach:
dma_buf_detach(dma_buf, attach);
dma_buf_put(dma_buf);

return ERR_PTR(ret);
}

static int virtio_gpu_sgt_to_mem_entry(struct virtio_gpu_device *vgdev,
Expand All @@ -170,7 +207,16 @@ static int virtio_gpu_sgt_to_mem_entry(struct virtio_gpu_device *vgdev,
struct scatterlist *sg;
int si;

bool use_dma_api = !virtio_has_dma_quirk(vgdev->vdev);
/**
* TODO: We must always use DMA addresses for the following two reasons:
*
* 1. By design we are not allowed to access the struct page backing a
* scatter list, especially when config DMABUF_DEBUG is turned on in
* which case the addresses will be mangled by the core.
* 2. DMA addresses are required for dGPU local memory sharing between
* host and guest.
*/
const bool use_dma_api = true;
if (use_dma_api)
*nents = table->nents;
else
Expand Down Expand Up @@ -219,6 +265,8 @@ struct drm_gem_object *virtgpu_gem_prime_import_sg_table(
return ERR_PTR(-ENODEV);
}

drm_info(dev, "%s: table = %p, orig_nents = %u, nents = %u\n",
__func__, table, table->orig_nents, table->nents);
obj = drm_gem_shmem_prime_import_sg_table(dev, attach, table);
if (IS_ERR(obj)) {
return ERR_CAST(obj);
Expand Down
4 changes: 4 additions & 0 deletions include/linux/dma-buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,10 @@ struct dma_buf_attachment *
dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
const struct dma_buf_attach_ops *importer_ops,
void *importer_priv);
struct dma_buf_attachment *
____dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
const struct dma_buf_attach_ops *importer_ops,
void *importer_priv, bool p2p);
void dma_buf_detach(struct dma_buf *dmabuf,
struct dma_buf_attachment *attach);
int dma_buf_pin(struct dma_buf_attachment *attach);
Expand Down
6 changes: 4 additions & 2 deletions include/uapi/linux/virtio_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@
/*
*VIRTIO_GPU_CMD_SET_SCALING
*/
#define VIRTIO_GPU_F_SCALING 6
#define VIRTIO_GPU_F_SCALING 6

#define VIRTIO_GPU_F_VBLANK 7
#define VIRTIO_GPU_F_VBLANK 7

#define VIRTIO_GPU_F_ALLOW_P2P 9

enum virtio_gpu_ctrl_type {
VIRTIO_GPU_UNDEFINED = 0,
Expand Down