ctx id will be used across process and across device. Change-Id: I3f4f99b75f457d60ae0e5c2a9ab126dff6f3418f Signed-off-by: Chunming Zhou <David1.Zhou at amd.com> --- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 59 +++++++++++---------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 3 ++ 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c index 17e1362..bd74a57 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c @@ -25,6 +25,9 @@ #include <drm/drmP.h> #include "amdgpu.h" +static DEFINE_MUTEX(amdgpu_ctx_lock); +extern struct idr amdgpu_ctx_idr; + static int amdgpu_ctx_init(struct amdgpu_device *adev, struct amdgpu_ctx *ctx) { unsigned i, j; @@ -87,7 +90,6 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t *id) { - struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr; struct amdgpu_ctx *ctx; int r; @@ -95,21 +97,21 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev, if (!ctx) return -ENOMEM; - mutex_lock(&mgr->lock); - r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL); + mutex_lock(&amdgpu_ctx_lock); + r = idr_alloc(&amdgpu_ctx_idr, ctx, 1, 0, GFP_KERNEL); if (r < 0) { - mutex_unlock(&mgr->lock); + mutex_unlock(&amdgpu_ctx_lock); kfree(ctx); return r; } *id = (uint32_t)r; r = amdgpu_ctx_init(adev, ctx); if (r) { - idr_remove(&mgr->ctx_handles, *id); + idr_remove(&amdgpu_ctx_idr, *id); *id = 0; kfree(ctx); } - mutex_unlock(&mgr->lock); + mutex_unlock(&amdgpu_ctx_lock); return r; } @@ -126,18 +128,17 @@ static void amdgpu_ctx_do_release(struct kref *ref) static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id) { - struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr; struct amdgpu_ctx *ctx; - mutex_lock(&mgr->lock); - ctx = idr_find(&mgr->ctx_handles, id); + mutex_lock(&amdgpu_ctx_lock); + ctx = idr_find(&amdgpu_ctx_idr, id); if (ctx) { - idr_remove(&mgr->ctx_handles, id); + idr_remove(&amdgpu_ctx_idr, id); kref_put(&ctx->refcount, amdgpu_ctx_do_release); - mutex_unlock(&mgr->lock); + mutex_unlock(&amdgpu_ctx_lock); return 0; } - mutex_unlock(&mgr->lock); + mutex_unlock(&amdgpu_ctx_lock); return -EINVAL; } @@ -146,17 +147,15 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev, union drm_amdgpu_ctx_out *out) { struct amdgpu_ctx *ctx; - struct amdgpu_ctx_mgr *mgr; unsigned reset_counter; if (!fpriv) return -EINVAL; - mgr = &fpriv->ctx_mgr; - mutex_lock(&mgr->lock); - ctx = idr_find(&mgr->ctx_handles, id); + mutex_lock(&amdgpu_ctx_lock); + ctx = idr_find(&amdgpu_ctx_idr, id); if (!ctx) { - mutex_unlock(&mgr->lock); + mutex_unlock(&amdgpu_ctx_lock); return -EINVAL; } @@ -173,7 +172,7 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev, out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET; ctx->reset_counter = reset_counter; - mutex_unlock(&mgr->lock); + mutex_unlock(&amdgpu_ctx_lock); return 0; } @@ -211,18 +210,15 @@ int amdgpu_ctx_ioctl(struct drm_device *dev, void *data, struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id) { struct amdgpu_ctx *ctx; - struct amdgpu_ctx_mgr *mgr; if (!fpriv) return NULL; - mgr = &fpriv->ctx_mgr; - - mutex_lock(&mgr->lock); - ctx = idr_find(&mgr->ctx_handles, id); + mutex_lock(&amdgpu_ctx_lock); + ctx = idr_find(&amdgpu_ctx_idr, id); if (ctx) kref_get(&ctx->refcount); - mutex_unlock(&mgr->lock); + mutex_unlock(&amdgpu_ctx_lock); return ctx; } @@ -291,23 +287,8 @@ struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx, void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr) { - mutex_init(&mgr->lock); - idr_init(&mgr->ctx_handles); } void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr) { - struct amdgpu_ctx *ctx; - struct idr *idp; - uint32_t id; - - idp = &mgr->ctx_handles; - - idr_for_each_entry(idp, ctx, id) { - if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1) - DRM_ERROR("ctx %p is still alive\n", ctx); - } - - idr_destroy(&mgr->ctx_handles); - mutex_destroy(&mgr->lock); } diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c index 209a539..222da53 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c @@ -334,6 +334,8 @@ MODULE_DEVICE_TABLE(pci, pciidlist); static struct drm_driver kms_driver; +struct idr amdgpu_ctx_idr; + static int amdgpu_kick_out_firmware_fb(struct pci_dev *pdev) { struct apertures_struct *ap; @@ -624,6 +626,7 @@ static int __init amdgpu_init(void) pdriver = &amdgpu_kms_pci_driver; driver->num_ioctls = amdgpu_max_kms_ioctl; amdgpu_register_atpx_handler(); + idr_init(&amdgpu_ctx_idr); /* let modprobe override vga console setting */ return drm_pci_init(driver, pdriver); } -- 1.9.1