From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8A6C1CD128A for ; Tue, 9 Apr 2024 20:05:21 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C5C7710EE99; Tue, 9 Apr 2024 20:05:16 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="HBkc/UQO"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) by gabe.freedesktop.org (Postfix) with ESMTPS id F0910112F11 for ; Tue, 9 Apr 2024 20:04:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712693094; x=1744229094; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nyyZeaNgbmIEevFgpzQe9GOEFHV56Ay+v9TsDym8Viw=; b=HBkc/UQOQr3QEqhqUgh2+7blKXWhO1lmvbCd6dh35jr3nGnMruhgilmc otrBr0ijLqj5cd7h7L0osqtVy65xDXAXHFQ/2BVLXgPoNHLRLsKcb2gI+ MAVgB0JGpVwgBkkc/svwFk3nrrd1Ml5mv6FXeN3PIH3X+eepcWG+FmfIs rBwQ+eCbU3ZLwivN3NuIl3sLE3KfHDSUZlQGkDKPuPtkosLgKhjzunkEq +av0Zyj4RgWCCIOFMKfv3GqBrhLGyMkElmWUKkoZcbqP9C+NNo9/zU5nL /nwZww+GoeEXrjXEbc10gne5ZQHXR3pK9K0q9XdQxyan9v71OXyesChqo w==; X-CSE-ConnectionGUID: LmxM0hS9RLWexnC0tudK9g== X-CSE-MsgGUID: JczHHvo9T7+vYc+dpxWFHg== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="11803746" X-IronPort-AV: E=Sophos;i="6.07,190,1708416000"; d="scan'208";a="11803746" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Apr 2024 13:04:52 -0700 X-CSE-ConnectionGUID: fG6HTt4/SMymuHMkBpcKFw== X-CSE-MsgGUID: vpnCHpdgS1SZS1zakO9zhg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,190,1708416000"; d="scan'208";a="20773742" Received: from szeng-desk.jf.intel.com ([10.165.21.149]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Apr 2024 13:04:52 -0700 From: Oak Zeng To: intel-xe@lists.freedesktop.org Cc: himal.prasad.ghimiray@intel.com, krishnaiah.bommu@intel.com, matthew.brost@intel.com, Thomas.Hellstrom@linux.intel.com, brian.welty@intel.com Subject: [v2 10/31] drm/xe: Introduce a helper to free sg table Date: Tue, 9 Apr 2024 16:17:21 -0400 Message-Id: <20240409201742.3042626-11-oak.zeng@intel.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20240409201742.3042626-1-oak.zeng@intel.com> References: <20240409201742.3042626-1-oak.zeng@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-xe-bounces@lists.freedesktop.org Sender: "Intel-xe" Introduce xe_userptr_free_sg helper to dma-unmap all addresses in userptr's sg table and free sg table. Signed-off-by: Oak Zeng Suggested by: Matthew Brost --- drivers/gpu/drm/xe/xe_hmm.c | 30 ++++++++++++++++++++++++++++++ drivers/gpu/drm/xe/xe_hmm.h | 1 + 2 files changed, 31 insertions(+) diff --git a/drivers/gpu/drm/xe/xe_hmm.c b/drivers/gpu/drm/xe/xe_hmm.c index 4011207630a5..427c6bc49949 100644 --- a/drivers/gpu/drm/xe/xe_hmm.c +++ b/drivers/gpu/drm/xe/xe_hmm.c @@ -3,6 +3,7 @@ * Copyright © 2024 Intel Corporation */ +#include #include #include #include @@ -107,6 +108,32 @@ static int xe_build_sg(struct xe_device *xe, struct hmm_range *range, return ret; } +/** + * xe_userptr_free_sg() - Free the scatter gather table of userptr + * + * @uvma: the userptr vma which hold the scatter gather table + * + * With function xe_userptr_populate_range, we allocate storage of + * the userptr sg table. This is a helper function to free this + * sg table, and dma unmap the address in the table. + */ +void xe_userptr_free_sg(struct xe_userptr_vma *uvma) +{ + struct xe_userptr *userptr = &uvma->userptr; + struct xe_vma *vma = &uvma->vma; + bool write = !xe_vma_read_only(vma); + struct xe_vm *vm = xe_vma_vm(vma); + struct xe_device *xe = vm->xe; + struct device *dev = xe->drm.dev; + + xe_assert(xe, userptr->sg); + dma_unmap_sgtable(dev, userptr->sg, + write ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE, 0); + + sg_free_table(userptr->sg); + userptr->sg = NULL; +} + /** * xe_userptr_populate_range() - Populate physical pages of a virtual * address range @@ -156,6 +183,9 @@ int xe_userptr_populate_range(struct xe_userptr_vma *uvma) if (notifier_seq == userptr->notifier_seq) return 0; + if (userptr->sg) + xe_userptr_free_sg(uvma); + npages = xe_npages_in_range(start, end); pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL); if (unlikely(!pfns)) diff --git a/drivers/gpu/drm/xe/xe_hmm.h b/drivers/gpu/drm/xe/xe_hmm.h index 91686a751711..7bb49bbde5a4 100644 --- a/drivers/gpu/drm/xe/xe_hmm.h +++ b/drivers/gpu/drm/xe/xe_hmm.h @@ -15,3 +15,4 @@ static inline int xe_userptr_populate_range(struct xe_userptr_vma *uvma) return -ENODEV; } #endif +void xe_userptr_free_sg(struct xe_userptr_vma *uvma); -- 2.26.3