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 A4BFBCD11BF for ; Wed, 20 Mar 2024 03:32:11 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BE6CE10E839; Wed, 20 Mar 2024 03:32:10 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="PlJe/4zr"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) by gabe.freedesktop.org (Postfix) with ESMTPS id 58E4510E0A8 for ; Wed, 20 Mar 2024 03:32:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710905531; x=1742441531; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GcXNbsBvYK3p7nYSTxcc9hgCXy3FNh6SRBtcW03yMp8=; b=PlJe/4zrPn+WPxLDUHVfsB3wotA3Vyu+orSnDXlRF866Jc9NvoriG4oM pEMn4VaHuTOJTGw6v0D6vXGFNOg0zokQTTVmmmSRaPJMbd88/AZgJ0Ssk LPvEzsdFfh/lhIBsZa7wNrfmeb6ac2XWWC2SJ1dGJCHd9ZD7P+ZMFoDWH 6BU0+FADVTrjtAb5ypy2ThBZcZCXmO66SiWIVwnxP9GZSM2KB6fRBNAZ4 ukNJHohn22ulzV0hbxg8yhYm6TqrijkNV/FbVvlFUWbe5FfyvABF9e6BE nYDIBzq2/BPvZQf3FpJmW/5PjcRlzDDBZ9AGlYUDhpXWpgfEuIT42p0mu A==; X-IronPort-AV: E=McAfee;i="6600,9927,11018"; a="5688810" X-IronPort-AV: E=Sophos;i="6.07,138,1708416000"; d="scan'208";a="5688810" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Mar 2024 20:32:10 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,138,1708416000"; d="scan'208";a="18599111" Received: from szeng-desk.jf.intel.com ([10.165.21.149]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Mar 2024 20:32:10 -0700 From: Oak Zeng To: intel-xe@lists.freedesktop.org Cc: thomas.hellstrom@intel.com, matthew.brost@intel.com, brian.welty@intel.com, himal.prasad.ghimiray@intel.com Subject: [PATCH 7/8] drm/xe: Introduce a helper to free sg table Date: Tue, 19 Mar 2024 23:44:24 -0400 Message-Id: <20240320034425.1785007-8-oak.zeng@intel.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20240320034425.1785007-1-oak.zeng@intel.com> References: <20240320034425.1785007-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 e5719cab0c3d..5c0c3d277096 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 @@ -108,6 +109,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 @@ -157,6 +184,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 fa5ddc11f10b..037ed3bf76da 100644 --- a/drivers/gpu/drm/xe/xe_hmm.h +++ b/drivers/gpu/drm/xe/xe_hmm.h @@ -8,3 +8,4 @@ struct xe_userptr_vma; int xe_userptr_populate_range(struct xe_userptr_vma *uvma); +void xe_userptr_free_sg(struct xe_userptr_vma *uvma); -- 2.26.3