From patchwork Mon Jun 24 16:54:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Michel_D=C3=A4nzer?= X-Patchwork-Id: 11013807 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4BA3113AF for ; Mon, 24 Jun 2019 16:54:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 39E9028AA7 for ; Mon, 24 Jun 2019 16:54:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2E77F28BD0; Mon, 24 Jun 2019 16:54:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B58B828AA7 for ; Mon, 24 Jun 2019 16:54:24 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 656F389CE1; Mon, 24 Jun 2019 16:54:12 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from netline-mail3.netline.ch (mail.netline.ch [148.251.143.178]) by gabe.freedesktop.org (Postfix) with ESMTP id 697AE89CDE; Mon, 24 Jun 2019 16:54:10 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by netline-mail3.netline.ch (Postfix) with ESMTP id BD1DF2A604A; Mon, 24 Jun 2019 18:54:09 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at netline-mail3.netline.ch Received: from netline-mail3.netline.ch ([127.0.0.1]) by localhost (netline-mail3.netline.ch [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 5HERhpnKzLxX; Mon, 24 Jun 2019 18:54:09 +0200 (CEST) Received: from thor (116.245.63.188.dynamic.wline.res.cust.swisscom.ch [188.63.245.116]) by netline-mail3.netline.ch (Postfix) with ESMTPSA id 8802B2A604E; Mon, 24 Jun 2019 18:54:07 +0200 (CEST) Received: from daenzer by thor with local (Exim 4.92) (envelope-from ) id 1hfSEI-0003Zi-Mh; Mon, 24 Jun 2019 18:54:06 +0200 From: =?utf-8?q?Michel_D=C3=A4nzer?= To: amd-gfx@lists.freedesktop.org Subject: [PATCH libdrm 5/9] amdgpu: Add amdgpu_bo_handle_type_kms_user Date: Mon, 24 Jun 2019 18:54:02 +0200 Message-Id: <20190624165406.13682-6-michel@daenzer.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190624165406.13682-1-michel@daenzer.net> References: <20190624165406.13682-1-michel@daenzer.net> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Emil Velikov , dri-devel@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Michel Dänzer amdgpu_bo_handle_type_kms returns the handle valid for the DRM file descriptor (fd) used for CS submission etc. This is also valid for the fd passed to amdgpu_device_initialize the first time for a specific GPU, but in general not for fds passed to amdgpu_device_initialize later for the same GPU. Because some use-cases require a handle valid for the fd passed to amdgpu_device_initialize, amdgpu_bo_handle_type_kms_user is added for this purpose. Signed-off-by: Michel Dänzer --- amdgpu/amdgpu.h | 14 ++++++++- amdgpu/amdgpu_bo.c | 65 +++++++++++++++++++++++++++------------- amdgpu/amdgpu_internal.h | 1 + 3 files changed, 59 insertions(+), 21 deletions(-) diff --git a/amdgpu/amdgpu.h b/amdgpu/amdgpu.h index 66e45f73..f95c0a36 100644 --- a/amdgpu/amdgpu.h +++ b/amdgpu/amdgpu.h @@ -81,7 +81,14 @@ enum amdgpu_bo_handle_type { /** GEM flink name (needs DRM authentication, used by DRI2) */ amdgpu_bo_handle_type_gem_flink_name = 0, - /** KMS handle which is used by all driver ioctls */ + /** KMS handle which is used by all driver ioctls + * + * NOTE: The returned handle is valid for the DRM file description + * used for command submission, which may be different from the one + * referenced by the file descriptor passed to + * amdgpu_device_initialize. Use amdgpu_bo_handle_type_kms_user to + * get a handle valid for the latter. + */ amdgpu_bo_handle_type_kms = 1, /** DMA-buf fd handle */ @@ -91,6 +98,11 @@ enum amdgpu_bo_handle_type { * amdgpu_bo_handle_type_kms, use that instead of this */ amdgpu_bo_handle_type_kms_noimport = 3, + + /** KMS handle valid for the DRM file description referenced by the + * file descriptor passed to amdgpu_device_initialize. + */ + amdgpu_bo_handle_type_kms_user = 4, }; /** Define known types of GPU VM VA ranges */ diff --git a/amdgpu/amdgpu_bo.c b/amdgpu/amdgpu_bo.c index 7fec1f15..8d42db90 100644 --- a/amdgpu/amdgpu_bo.c +++ b/amdgpu/amdgpu_bo.c @@ -229,42 +229,58 @@ drm_public int amdgpu_bo_query_info(amdgpu_bo_handle user_bo, return 0; } +static int amdgpu_bo_get_user_handle(struct amdgpu_bo *user_bo, + uint32_t *user_handle) +{ + struct amdgpu_device *user_dev = user_bo->dev; + struct amdgpu_core_device *dev = user_dev->core; + struct amdgpu_core_bo *bo = user_bo->core; + int dma_fd; + int r; + + if (user_dev->user_fd == dev->fd) { + *user_handle = bo->handle; + return 0; + } + + if (user_bo->user_handle) + goto out; + + r = drmPrimeHandleToFD(dev->fd, bo->handle, DRM_CLOEXEC, &dma_fd); + if (r) + return r; + + r = drmPrimeFDToHandle(user_dev->user_fd, dma_fd, &user_bo->user_handle); + close(dma_fd); + if (r) + return r; + +out: + *user_handle = user_bo->user_handle; + return 0; +} + static int amdgpu_bo_export_flink(amdgpu_bo_handle user_bo) { struct amdgpu_core_device *dev = user_bo->dev->core; struct amdgpu_core_bo *bo = user_bo->core; - int user_fd = user_bo->dev->user_fd; struct drm_gem_flink flink; - int fd, dma_fd; - uint32_t handle; int r; - fd = dev->fd; - handle = bo->handle; if (bo->flink_name) return 0; - if (user_fd != fd) { - r = drmPrimeHandleToFD(fd, bo->handle, DRM_CLOEXEC, &dma_fd); - if (!r) { - r = drmPrimeFDToHandle(user_fd, dma_fd, &handle); - close(dma_fd); - } - if (r) - return r; - fd = user_fd; - } memset(&flink, 0, sizeof(flink)); - flink.handle = handle; - r = drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &flink); + r = amdgpu_bo_get_user_handle(user_bo, &flink.handle); if (r) return r; - bo->flink_name = flink.name; + r = drmIoctl(user_bo->dev->user_fd, DRM_IOCTL_GEM_FLINK, &flink); + if (r) + return r; - if (user_fd != dev->fd) - amdgpu_close_kms_handle(user_fd, handle); + bo->flink_name = flink.name; pthread_mutex_lock(&dev->bo_table_mutex); r = handle_table_insert(&dev->bo_flink_names, bo->flink_name, bo); @@ -294,6 +310,9 @@ drm_public int amdgpu_bo_export(amdgpu_bo_handle user_bo, *shared_handle = bo->handle; return 0; + case amdgpu_bo_handle_type_kms_user: + return amdgpu_bo_get_user_handle(user_bo, shared_handle); + case amdgpu_bo_handle_type_dma_buf_fd: return drmPrimeHandleToFD(user_bo->dev->core->fd, bo->handle, DRM_CLOEXEC | DRM_RDWR, @@ -355,6 +374,7 @@ drm_public int amdgpu_bo_import(amdgpu_device_handle user_dev, case amdgpu_bo_handle_type_kms: case amdgpu_bo_handle_type_kms_noimport: + case amdgpu_bo_handle_type_kms_user: /* Importing a KMS handle in not allowed. */ r = -EPERM; goto unlock; @@ -404,6 +424,7 @@ drm_public int amdgpu_bo_import(amdgpu_device_handle user_dev, case amdgpu_bo_handle_type_kms: case amdgpu_bo_handle_type_kms_noimport: + case amdgpu_bo_handle_type_kms_user: assert(0); /* unreachable */ } @@ -489,6 +510,10 @@ drm_public int amdgpu_bo_free(amdgpu_bo_handle buf_handle) if (update_references(&user_bo->refcount, NULL)) { amdgpu_core_bo_free(user_bo); + if (user_bo->user_handle) { + amdgpu_close_kms_handle(user_bo->dev->user_fd, + user_bo->user_handle); + } free(user_bo); } diff --git a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h index 3a2ab74c..a08a4ae8 100644 --- a/amdgpu/amdgpu_internal.h +++ b/amdgpu/amdgpu_internal.h @@ -111,6 +111,7 @@ struct amdgpu_core_bo { struct amdgpu_bo { atomic_t refcount; + uint32_t user_handle; struct amdgpu_bo *next; struct amdgpu_core_bo *core; struct amdgpu_device *dev;