Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.20 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796329443; b=bkAWmt6O1fzWIfV607gEoaHBr5xuSc0/sJGzi3oyty3PJI8kYUyDP5CyB7J8tYWbV6En 6wNFJxVT/K12dh1G7pOTb7Zlgrmd/LDbnuZ/7otpnAo2M+bED5fuo4I3sSENh6XXoTc1U hzKWTLY0PYOm09twYpOnh/a2rDpqlSxEA3AL/Nve8bXiWCPwh0dvK1y3MGDzQyfGnNwzM N1jQ2LccmJ3kY7a9iIX5Q0GD3ONd7XJ7UEJ4vns2IjZ1cy3U8S3TPoH8btjK9a1w6iQrQ sDbv7/Pvgy12UQ6hvlcRZrhwgkBBKUWK3QbhzL7yKTcmfYLXRSMKK/Ks4xygYIZEyzQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796329443; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:To:Cc:Subject: Date:Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=LdfwzKG9u4Xwsxx8HpaF4h/OjOxYiBIFGFIUrC4w2nY=; b=RRDWKNj8YSVoqSM7e/ZfSTR23PElAu5ElhIJo3KsFx1T1PlTz5/7kN3FbR1glvU04LCS LpWcm5/0ohBIu9mRkO24NoyPv1zYg++EqAtljcOWV52+eEbRDJ6aAzi1ncZztnEwMmNcl p1RM9oVg9XB2ZVEErqXWK1vE1fh+Qsi+wq7V1V3JWOHhIiS1Ovowoc27T/fKURta41nli aPX+aGri80YiFC6QjeYCbjWf7IS7X4vXN2NG/LWZ5sPXEzEiw2smOHwkXjV+qXkfSdD+y rAjJsjRTMJl5nxXYcqtdGLgsQ/hPt2zC/+8EBDvlr+iJlybJMWjPLdCa8UIeGYH2WPg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=RRxsV5Mv; arc=none smtp.remote-ip=198.175.65.20 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C27960A2633 for ; Thu, 3 Dec 2026 20:24:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9C27960A2633 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=RRxsV5Mv DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796329443; x=1827865443; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LdfwzKG9u4Xwsxx8HpaF4h/OjOxYiBIFGFIUrC4w2nY=; b=RRxsV5Mv7B9MUo2UHL+XJhlqE6WMjAb8MOiHpRyoCG9KQglVxd9pVNZy 0ylOiD8eyaUm+P5khXpB0VKfxqfKLxxRIcXIZ67DyWp00c5w9HxzzsvsA XjKT28oig8cg3PZQPqpfiZj7IVZiUvLT8kDd38Bbr9aDICxAyZH1Mj+z1 CjVECOacqQLCj433gyn/rpSBbR3M20QADmkY3YOYdJtNp5Kxdgu6jfXpx Opg9XUhSIA1M2Q9PdZXionPvRe+Awtvvuug08G0CJ2AnLrdKE1RSmLZJd pIa5OGHtM0g4+1wvGIz9seENjgLGlpOVcegGlkTde8EIf2TMwuxOZU07u Q==; X-CSE-ConnectionGUID: 6dj3O1w7Q9uIiKJbaU4Yog== X-CSE-MsgGUID: RvgmkMuzRK6Y802tQGTh2A== X-IronPort-AV: E=McAfee;i="6800,10657,11985"; a="5351107" X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="5351107" Received: from fmviesa013.fm.intel.com ([10.60.135.153]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 12:24:02 -0800 X-CSE-ConnectionGUID: rvqRB0u7STeNL6toCAvsvg== X-CSE-MsgGUID: ntDG437URu+D52vlp0dP3g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="15862468" Received: from gsse-cloud1.jf.intel.com ([10.54.39.91]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 12:24:02 -0800 From: Matthew Brost To: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org Cc: Carlos Santa , Ryan Neph , Natalie Vock , Arun Pravin , Matthew Auld , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , linux-kernel@vger.kernel.org, =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= , Niranjana Vishwanathapura Subject: [PATCH v6 15/35] drm/xe: Add xe_migrate_copy_defrag() for on-GPU defrag copies Date: Thu, 3 Dec 2026 12:23:32 -0800 Message-Id: <20261203202352.4153159-16-matthew.brost@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261203202352.4153159-1-matthew.brost@intel.com> References: <20261203202352.4153159-1-matthew.brost@intel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add xe_migrate_copy_defrag(), a copy helper for the upcoming page defragmentation path where a single BO has its backing reallocated at a more beneficial page order and its contents must be relocated from the old pages to the new ones entirely on the GPU. The contents are relocated in up to two passes: - Data pass (always): copy the data pages verbatim. The pages are mapped without the compression PAT, so the (possibly compressed) bytes are copied as-is rather than being decompressed and recompressed, and no CCS aux copy is appended. - CCS pass (need_ccs): copy the CCS aux (compression) state. Both the old and new data pages are mapped with the compression PAT so the copy engine accesses the aux state indirectly, issuing one indirect -> indirect XY_CTRL_SURF_COPY_BLT; the data blit is skipped. The two passes are issued separately because they require opposite compression PAT settings on the data page mappings (cleared for the verbatim data copy, set for the indirect CCS access), which a single mapping cannot satisfy. Both passes run in order on the same migrate queue and are captured by the returned fence. The source pages are passed explicitly via an sg_table argument (the old, still populated backing) since they no longer match the BO's current backing. The boolean arguments to __xe_migrate_copy() are replaced by a single enum xe_migrate_copy_mode, since the copy variants are mutually exclusive. v4: - s/struct xe_migrate_copy_flags/enum xe_migrate_copy_mode (Niranjana) v5: - non-interruptible comment (Niranjana) Cc: Carlos Santa Cc: Ryan Neph Cc: Natalie Vock Cc: Arun Pravin Cc: Matthew Auld Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie Cc: Simona Vetter Cc: dri-devel@lists.freedesktop.org Cc: linux-kernel@vger.kernel.org Cc: Thomas Hellström Signed-off-by: Matthew Brost Reviewed-by: Niranjana Vishwanathapura --- drivers/gpu/drm/xe/xe_migrate.c | 136 ++++++++++++++++++++++++++++---- drivers/gpu/drm/xe/xe_migrate.h | 8 ++ 2 files changed, 130 insertions(+), 14 deletions(-) diff --git a/drivers/gpu/drm/xe/xe_migrate.c b/drivers/gpu/drm/xe/xe_migrate.c index 23575f9dc651..e1ca896c0f31 100644 --- a/drivers/gpu/drm/xe/xe_migrate.c +++ b/drivers/gpu/drm/xe/xe_migrate.c @@ -1293,15 +1293,50 @@ static bool is_devmem(struct xe_bo *bo, struct ttm_resource *res) return true; } +/** + * enum xe_migrate_copy_mode - Mode for __xe_migrate_copy() + * @XE_MIGRATE_COPY_NORMAL: Regular copy of data (and CCS aux state if needed). + * @XE_MIGRATE_COPY_CCS_ONLY: Only copy the CCS aux state, aliasing the + * destination data mapping to the source (used when the data pages are shared + * between src/dst). + * @XE_MIGRATE_COPY_VRAM_RESOLVE: Decompression (VRAM -> system) resolve; + * always uses the compression PAT index for the data copy. + * @XE_MIGRATE_COPY_DEFRAG_DATA: Defrag move pass that copies the data pages + * between two distinct page sets (old @src_sg pages -> new pages) without + * touching the CCS aux state. The data pages are mapped without the + * compression PAT so the (possibly compressed) bytes are copied verbatim + * rather than being decompressed and recompressed, and no CCS aux copy is + * appended; the aux state is migrated separately by the + * @XE_MIGRATE_COPY_DEFRAG_CCS pass. + * @XE_MIGRATE_COPY_DEFRAG_CCS: Defrag move pass that migrates only the CCS aux + * state between two distinct page sets (old @src_sg pages -> new pages). The + * data blit is skipped (the data was copied by the + * @XE_MIGRATE_COPY_DEFRAG_DATA pass) and both the source and destination data + * pages are mapped with the compression PAT so the aux state is copied + * indirect -> indirect. The two defrag passes need opposite compression PAT + * settings, which is why they cannot be combined. + */ +enum xe_migrate_copy_mode { + XE_MIGRATE_COPY_NORMAL, + XE_MIGRATE_COPY_CCS_ONLY, + XE_MIGRATE_COPY_VRAM_RESOLVE, + XE_MIGRATE_COPY_DEFRAG_DATA, + XE_MIGRATE_COPY_DEFRAG_CCS, +}; + static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, struct xe_bo *src_bo, struct xe_bo *dst_bo, struct ttm_resource *src, struct ttm_resource *dst, - bool copy_only_ccs, - bool is_vram_resolve, + struct sg_table *src_sg, + enum xe_migrate_copy_mode mode, bool intr) { + bool copy_only_ccs = mode == XE_MIGRATE_COPY_CCS_ONLY; + bool is_vram_resolve = mode == XE_MIGRATE_COPY_VRAM_RESOLVE; + bool defrag_copy = mode == XE_MIGRATE_COPY_DEFRAG_DATA; + bool defrag_ccs = mode == XE_MIGRATE_COPY_DEFRAG_CCS; struct xe_gt *gt = m->tile->primary_gt; struct xe_device *xe = gt_to_xe(gt); struct dma_fence *fence = NULL; @@ -1317,10 +1352,13 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, bool src_is_vram = is_devmem(src_bo, src); bool dst_is_vram = is_devmem(dst_bo, dst); bool type_device = src_bo->ttm.type == ttm_bo_type_device; - bool needs_ccs_emit = type_device && xe_migrate_needs_ccs_emit(xe); + bool needs_ccs_emit = type_device && xe_migrate_needs_ccs_emit(xe) && + !defrag_copy; bool copy_ccs = xe_device_has_flat_ccs(xe) && - xe_bo_needs_ccs_pages(src_bo) && xe_bo_needs_ccs_pages(dst_bo); - bool copy_system_ccs = copy_ccs && (!src_is_vram || !dst_is_vram); + xe_bo_needs_ccs_pages(src_bo) && xe_bo_needs_ccs_pages(dst_bo) && + !defrag_copy; + bool copy_system_ccs = copy_ccs && (!src_is_vram || !dst_is_vram) && + !defrag_ccs && !defrag_copy; /* * For decompression operation, always use the compression PAT index. @@ -1331,6 +1369,12 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, xe_device_has_flat_ccs(xe) && GRAPHICS_VER(xe) >= 20 && src_is_vram && !dst_is_vram); + /* + * The defrag CCS pass migrates the aux state from the old pages + * (@src_sg) to the new pages with both sides accessed indirectly, so + * keep both src_is_pltt and dst_is_pltt set (no direct staging side). + */ + /* Copying CCS between two different BOs is not supported yet. */ if (XE_WARN_ON(copy_ccs && src_bo != dst_bo)) return ERR_PTR(-EINVAL); @@ -1339,7 +1383,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, return ERR_PTR(-EINVAL); if (!src_is_vram && src->mem_type != XE_PL_STOLEN) - xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it); + xe_res_first_sg(src_sg ? src_sg : xe_bo_sg(src_bo), 0, size, &src_it); else xe_res_first(src, 0, size, &src_it); if (!dst_is_vram && dst->mem_type != XE_PL_STOLEN) @@ -1348,7 +1392,8 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, xe_res_first(dst, 0, size, &dst_it); if (copy_system_ccs) - xe_res_first_sg(xe_bo_sg(src_bo), xe_bo_ccs_pages_start(src_bo), + xe_res_first_sg(src_sg ? src_sg : xe_bo_sg(src_bo), + xe_bo_ccs_pages_start(src_bo), PAGE_ALIGN(xe_device_ccs_bytes(xe, size)), &ccs_it); @@ -1399,7 +1444,8 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, } /* Add copy commands size here */ - batch_size += ((copy_only_ccs) ? 0 : emit_copy_cmd_len(xe)) + + batch_size += ((copy_only_ccs || defrag_ccs) ? 0 : + emit_copy_cmd_len(xe)) + ((needs_ccs_emit ? EMIT_COPY_CCS_DW : 0)); bb = __xe_bb_new(gt, batch_size, usm, !fence && intr); @@ -1411,13 +1457,15 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, if (src_is_vram && xe_migrate_allow_identity(src_L0, &src_it)) xe_res_next(&src_it, src_L0); else - emit_pte(m, bb, src_L0_pt, src_is_vram, copy_system_ccs || use_comp_pat, + emit_pte(m, bb, src_L0_pt, src_is_vram, + copy_system_ccs || use_comp_pat || defrag_ccs, &src_it, src_L0, src); if (dst_is_vram && xe_migrate_allow_identity(src_L0, &dst_it)) xe_res_next(&dst_it, src_L0); else if (!copy_only_ccs) - emit_pte(m, bb, dst_L0_pt, dst_is_vram, copy_system_ccs, + emit_pte(m, bb, dst_L0_pt, dst_is_vram, + copy_system_ccs || defrag_ccs, &dst_it, src_L0, dst); if (copy_system_ccs) @@ -1426,7 +1474,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, bb->cs[bb->len++] = MI_BATCH_BUFFER_END; update_idx = bb->len; - if (!copy_only_ccs) + if (!copy_only_ccs && !defrag_ccs) emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE); if (needs_ccs_emit) @@ -1513,7 +1561,66 @@ struct dma_fence *xe_migrate_copy(struct xe_migrate *m, bool copy_only_ccs, bool intr) { - return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, copy_only_ccs, false, intr); + return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL, + copy_only_ccs ? XE_MIGRATE_COPY_CCS_ONLY : + XE_MIGRATE_COPY_NORMAL, intr); +} + +/** + * xe_migrate_copy_defrag() - Migrate a BO's backing to a reallocated page set. + * @m: The migration context. + * @bo: The buffer object being defragmented. Both @src and @dst belong to @bo. + * @src: The source TTM resource (the old backing). + * @dst: The dst TTM resource (the freshly reallocated backing). + * @src_sg: Scatter-gather table of the source (old) backing pages. + * @need_ccs: Whether the BO has CCS aux state that must be migrated too. + * + * Used by the defrag path where a single BO has its backing reallocated at a + * more beneficial page order. The contents are relocated from the old pages + * (@src_sg) to the new pages (the BO's current backing) entirely on the GPU, + * in up to two passes: + * + * - Data pass (always): copy the data pages verbatim. The pages are mapped + * without the compression PAT, so the (possibly compressed) bytes are copied + * as-is rather than being decompressed and recompressed, and no CCS aux copy + * is appended. + * + * - CCS pass (@need_ccs): copy the CCS aux (compression) state. Both the old + * and new data pages are mapped with the compression PAT so the copy engine + * accesses the aux state indirectly, issuing one indirect -> indirect + * XY_CTRL_SURF_COPY_BLT; the data blit is skipped. + * + * The two passes are issued separately because they require opposite + * compression PAT settings on the data page mappings (cleared for the verbatim + * data copy, set for the indirect CCS access), which a single mapping cannot + * satisfy. Both passes run in order on the same migrate queue. + * + * Always run non-interruptible as defrag are issued from a work queue. + * + * Return: Pointer to a dma_fence representing the last copy batch, or an error + * pointer on failure. + */ +struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m, + struct xe_bo *bo, + struct ttm_resource *src, + struct ttm_resource *dst, + struct sg_table *src_sg, + bool need_ccs) +{ + struct dma_fence *fence2, *fence = + __xe_migrate_copy(m, bo, bo, src, dst, src_sg, + XE_MIGRATE_COPY_DEFRAG_DATA, false); + + if (IS_ERR(fence) || !need_ccs) + return fence; + + fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_sg, + XE_MIGRATE_COPY_DEFRAG_CCS, false); + if (IS_ERR(fence2)) + dma_fence_wait(fence, false); + dma_fence_put(fence); + + return fence2; } /** @@ -1522,7 +1629,7 @@ struct dma_fence *xe_migrate_copy(struct xe_migrate *m, * @bo: The buffer object to resolve * @res: The reservation object * - * Wrapper around __xe_migrate_copy() with is_vram_resolve set to true + * Wrapper around __xe_migrate_copy() with XE_MIGRATE_COPY_VRAM_RESOLVE * to trigger decompression if needed. * * Return: A dma_fence that signals on completion, or an ERR_PTR on failure. @@ -1531,7 +1638,8 @@ struct dma_fence *xe_migrate_resolve(struct xe_migrate *m, struct xe_bo *bo, struct ttm_resource *res) { - return __xe_migrate_copy(m, bo, bo, res, res, false, true, true); + return __xe_migrate_copy(m, bo, bo, res, res, NULL, + XE_MIGRATE_COPY_VRAM_RESOLVE, true); } /** diff --git a/drivers/gpu/drm/xe/xe_migrate.h b/drivers/gpu/drm/xe/xe_migrate.h index 0bf94f5b388d..70451392e0a7 100644 --- a/drivers/gpu/drm/xe/xe_migrate.h +++ b/drivers/gpu/drm/xe/xe_migrate.h @@ -11,6 +11,7 @@ struct dma_fence; struct drm_pagemap_addr; struct iosys_map; +struct sg_table; struct ttm_resource; struct xe_bo; @@ -59,6 +60,13 @@ struct dma_fence *xe_migrate_copy(struct xe_migrate *m, bool copy_only_ccs, bool intr); +struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m, + struct xe_bo *bo, + struct ttm_resource *src, + struct ttm_resource *dst, + struct sg_table *src_sg, + bool need_ccs); + struct dma_fence *xe_migrate_resolve(struct xe_migrate *m, struct xe_bo *bo, struct ttm_resource *res); -- 2.34.1