Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 957E2606A322 for ; Fri, 4 Dec 2026 10:57:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=jASnrdJ4 Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2B177112ECC; Fri, 4 Dec 2026 10:57:24 +0000 (UTC) Received: from PH8PR06CU001.outbound.protection.outlook.com (mail-westus3azon11012011.outbound.protection.outlook.com [40.107.209.11]) by gabe.freedesktop.org (Postfix) with ESMTPS id 52200112EC5 for ; Fri, 4 Dec 2026 10:57:17 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=sacfuQNcG6F70vB77tLTl5okkH7L1wLx612HyKC3Ga4MGwJtZKr8FLt/OsaGpcRRWmHEspfHsExm4WZZCTr90GTE7DG5Z9sWA2yFFwtJ6Qg5VPp9Cl6HMwXq6vBUXgOkzE88t5t87nu//5Sqpr2jZ39wEBrDohhHulkGj3OABxPyOZTrOkIzXIf0dnFX/hCPZpFZGznpSrKL9Q/vtdefKTgVuEiRCXsCaeTP2T+v5KBV+onBpqwx0pijwoqGQ4BoQDtEIcrEUBgTO+4jGScDKFtFqpFdRGsr2oYy8InbcizRnEP+2awi0Xgw372rAkwXSoYQ4/VQ0b/Yc9jB/9RolA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=7PeDHVfDw4d88z7LuEnFENeiclVuDI0kJkt93u86MfI=; b=Rl41MzIOSTJq91IpzE05uLPEwnW4fgBRwo1wurdAkWPAsadEEffquuh6iWQEwnbg8oNfWhuu20RjcQfsgPaEdYZhGWn0V8hkN1//XBle4lqFmHEGMM6GLbaTqmnO1/FybyZEbz6bvSXzCvBkI5zM/4exfvARToUvo+Q6xJErW+K+BndmW0H0HweDnfO81byoW37FWF+rRl6n61Ek5kNKsWN5FhwuFOgbXHZxkbxbueB4IFWvkeNgn9Qrb69O3IklDCJFKWKamY0brZ1pjCzC1leNqMe//a4k6FEeNFCAo4CWsr8dYmIMXh78P2JfQ2mAEum4nA5F6/1wP/NIj/sVJA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.freedesktop.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7PeDHVfDw4d88z7LuEnFENeiclVuDI0kJkt93u86MfI=; b=jASnrdJ4GR2ax9CHJ/0W0festiagL2IqvU1HItfLgjPxqFebRaKuyVSXY6Q1w5l1RFOx3+pNxKBPWCI4OfV0ghQM98oLz+FNs2CdNaP6UTkbnO4KW0kr5oC2+Q5lIscbkVnVGlzYWf7pxYV8ye0Wnd+/1k+GzKP/7hUdFJPaCsw= Received: from SJ0PR03CA0145.namprd03.prod.outlook.com (2603:10b6:a03:33c::30) by CY8PR12MB7756.namprd12.prod.outlook.com (2603:10b6:930:85::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.12; Fri, 4 Dec 2026 10:57:12 +0000 Received: from SJ5PEPF000001EA.namprd05.prod.outlook.com (2603:10b6:a03:33c:cafe::48) by SJ0PR03CA0145.outlook.office365.com (2603:10b6:a03:33c::30) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.592.12 via Frontend Transport; Fri, 4 Dec 2026 10:57:12 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb08.amd.com; pr=C Received: from satlexmb08.amd.com (165.204.84.17) by SJ5PEPF000001EA.mail.protection.outlook.com (10.167.242.198) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.709.6 via Frontend Transport; Fri, 4 Dec 2026 10:57:12 +0000 Received: from satlexmb10.amd.com (10.181.42.219) by satlexmb08.amd.com (10.181.42.217) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 04:57:11 -0600 Received: from satlexmb07.amd.com (10.181.42.216) by satlexmb10.amd.com (10.181.42.219) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 04:57:11 -0600 Received: from br10-5-1-emily.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server id 15.2.2562.57 via Frontend Transport; Fri, 4 Dec 2026 04:57:10 -0600 From: Emily Deng To: , , Subject: [RFC PATCH v1 31/34] drm/amd/sriov: add scheduler event handling Date: Fri, 4 Dec 2026 18:57:09 +0800 Message-ID: <730cf864fc4ddb981d1e00cfdad73db5d4eaa2b6.1796380206.git.Emily.Deng@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ5PEPF000001EA:EE_|CY8PR12MB7756:EE_ X-MS-Office365-Filtering-Correlation-Id: 55cd8750-56ea-4fa6-7eb6-08df51f559d1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|36860700016|1800799024|23010399003|82310400026|376014|22082099003|18002099003|10067099003|56012099006|261026130827099003|261026140227099003|261028144727099003|261022113327099003|261022105027099003|261026135527099003|261021133027099003|261026121527099003|261021130227099003|261022075727099003|261021121427099003|261021135927099003|261102123227099003|261022115727099003|261021123027099003|261009221227099003|261009223327099003|261009220027099003|261009223027099003|261009221727099003|11063799006|6133799003|3023799007; X-Microsoft-Antispam-Message-Info: Mv4Zmyjf5kuoaTNnFrKU3Q9GtQXzD6PYW8BumGCOFwaUAkh/QQjvFpr5PL9lfR3qXaBy38ncGzx34IfI7Fr65UuG5vGdMwKDTZQwG4GTi62CExB8yyK+9raDgFOOIbv4xr23DXwNtO5ADAqazCsISTQQpKRSl9+FPyinol1x1aS4IqPvi6pZYlAXQ6q+IjCA6pca0htr/0LVNjrkgLIxg+J07YLajZvJvR1rzoKEPxBxaSmJmopNTBjNH8h2Nzr62WuHibfkVwBRBbImY1fxe0Tm2I6xUYmZ66XzbsY2w6uzQUwPAdQZJhntSIt5NuRbJAd3WDUnBD9yTJ6eI2REXFKCHGGt1Jpf/PMYFj2tC1C5N2puXCuYVezMJaIlsxl7juxesgPQ69OzNuRPz1vTQv+3+Sy+LHirQT6RUDsA1YtaUDa4ewlthZvqEbGAe00uv1pR1k1VfxqppTmpLQxueDoTHNRni6W2ulWock+wrWVBr0Dsu3rrCG2zB8AtFJqEpQ7WtcQBYVyTAHpXPTO8liz0zVcmf/Av/ad4cE3KTCTaCt2Br372UZNXMiTZbw/aiE02EKoA6qgw4RV/H81QzAZnKpfHdij3/UVdYmfxzj+4/wZls2sv6OVJiVQgSNzP6+91noDdLHU0FVOeeEPJGMH4unfQ9orAyngzlVVNYKIakFJshLCr0zwnZ6/z3miJ25hEIURmc3AZlXA8d6UF6g== X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:satlexmb08.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230040)(36860700016)(1800799024)(23010399003)(82310400026)(376014)(22082099003)(18002099003)(10067099003)(56012099006)(261026130827099003)(261026140227099003)(261028144727099003)(261022113327099003)(261022105027099003)(261026135527099003)(261021133027099003)(261026121527099003)(261021130227099003)(261022075727099003)(261021121427099003)(261021135927099003)(261102123227099003)(261022115727099003)(261021123027099003)(261009221227099003)(261009223327099003)(261009220027099003)(261009223027099003)(261009221727099003)(11063799006)(6133799003)(3023799007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: a//nklxKIhn6uwBJNzYJ67PrxriD6/Jx7bp8BsnifytHQmqG2HbgHPO1N3JVDMaApbMmpw7lQ6fiqz6sk4j0bvp+OqOq6W22kX6Sk94I0zuY0nKkFFdNwwjr2WchPFAIvh4bn56+QOobyutiTVSFhPQoD+6q07flPl+Edf3XSpbR6FN1Q0lX9Z8dDOLdcCnFB3gKNWl3+58tNWfb//R/lfTtT01jm1nQa5s9spnii3bKL+U/wIYf8eEBBJNex/9Bm84QWBYGGjwmWbuKw1hIDqKMBtqSiEg7MWglc616qyt7wthHRLIN5hQw7jPpkm1XeM8aagFouESq3h828qBopMbKLiGGvuAATV1Bnm9zW6gTkeCCZHVMdmvldHBhqPApCiwj7SBHRVtqRx1IwNm/NnbNIMgW6R3OD6nUIkKAKcoowOaR8Wgfa1UDCugWINVi X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 10:57:12.4375 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 55cd8750-56ea-4fa6-7eb6-08df51f559d1 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[satlexmb08.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ5PEPF000001EA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7756 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "amd-gfx" X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Add the PF-side scheduler service, software event queue and worker used to serialize VF lifecycle requests from the mailbox layer. Implement event queue init/process start/stop, full-access arbitration for GPU init/fini/reset request and release handling, mailbox response routing and baseline VF event handlers. Signed-off-by: Emily Deng --- drivers/gpu/drm/amd/sriov/Makefile | 4 + drivers/gpu/drm/amd/sriov/sriov.c | 1 + drivers/gpu/drm/amd/sriov/sriov.h | 2 + drivers/gpu/drm/amd/sriov/sriov_mailbox.c | 10 + drivers/gpu/drm/amd/sriov/sriov_sched.c | 128 ++ drivers/gpu/drm/amd/sriov/sriov_sched_event.c | 1125 +++++++++++++++++ drivers/gpu/drm/amd/sriov/sriov_sched_event.h | 177 +++ .../drm/amd/sriov/sriov_sched_event_data.h | 84 ++ .../amd/sriov/sriov_sched_event_dispatch.c | 88 ++ .../amd/sriov/sriov_sched_event_dispatch.h | 62 + .../amd/sriov/sriov_sched_event_handlers.c | 179 +++ .../amd/sriov/sriov_sched_event_handlers.h | 55 + .../drm/amd/sriov/sriov_sched_event_priv.h | 78 ++ drivers/gpu/drm/amd/sriov/sriov_vfmgr.c | 3 + 14 files changed, 1996 insertions(+) create mode 100644 drivers/gpu/drm/amd/sriov/sriov_sched.c create mode 100644 drivers/gpu/drm/amd/sriov/sriov_sched_event.c create mode 100644 drivers/gpu/drm/amd/sriov/sriov_sched_event.h create mode 100644 drivers/gpu/drm/amd/sriov/sriov_sched_event_data.h create mode 100644 drivers/gpu/drm/amd/sriov/sriov_sched_event_dispatch.c create mode 100644 drivers/gpu/drm/amd/sriov/sriov_sched_event_dispatch.h create mode 100644 drivers/gpu/drm/amd/sriov/sriov_sched_event_handlers.c create mode 100644 drivers/gpu/drm/amd/sriov/sriov_sched_event_handlers.h create mode 100644 drivers/gpu/drm/amd/sriov/sriov_sched_event_priv.h diff --git a/drivers/gpu/drm/amd/sriov/Makefile b/drivers/gpu/drm/amd/sriov/Makefile index 455e6301b7f1..00888dbdaec4 100644 --- a/drivers/gpu/drm/amd/sriov/Makefile +++ b/drivers/gpu/drm/amd/sriov/Makefile @@ -34,5 +34,9 @@ AMD_SRIOV_FILES += ../sriov/sriov_mailbox.o AMD_SRIOV_FILES += ../sriov/sriov_nbif_v7_10.o AMD_SRIOV_FILES += ../sriov/sriov_ffbm.o AMD_SRIOV_FILES += ../sriov/sriov_vfmgr.o +AMD_SRIOV_FILES += ../sriov/sriov_sched.o +AMD_SRIOV_FILES += ../sriov/sriov_sched_event.o +AMD_SRIOV_FILES += ../sriov/sriov_sched_event_dispatch.o +AMD_SRIOV_FILES += ../sriov/sriov_sched_event_handlers.o AMD_SRIOV_FILES += ../sriov/sriov_psp_15_0_3.o AMD_SRIOV_FILES += ../sriov/sriov_ip_discovery.o diff --git a/drivers/gpu/drm/amd/sriov/sriov.c b/drivers/gpu/drm/amd/sriov/sriov.c index 18ce8a88d4bf..52d97a8fff91 100644 --- a/drivers/gpu/drm/amd/sriov/sriov.c +++ b/drivers/gpu/drm/amd/sriov/sriov.c @@ -115,6 +115,7 @@ static int sriov_set_init_funcs(struct amdgpu_device *adev) sriov_init_func_add(adev, &sriov_mailbox_func); sriov_init_func_add(adev, &sriov_ffbm_func); sriov_init_func_add(adev, &sriov_vfmgr_func); + sriov_init_func_add(adev, &sriov_sched_func); return 0; } diff --git a/drivers/gpu/drm/amd/sriov/sriov.h b/drivers/gpu/drm/amd/sriov/sriov.h index 0d4d4e61fe85..b01f2a36ebce 100644 --- a/drivers/gpu/drm/amd/sriov/sriov.h +++ b/drivers/gpu/drm/amd/sriov/sriov.h @@ -33,6 +33,7 @@ #include "sriov_mailbox.h" #include "sriov_ffbm.h" #include "sriov_vfmgr.h" +#include "sriov_sched_event.h" #include "sriov_ip_discovery.h" struct amdgpu_device; @@ -142,6 +143,7 @@ struct amdgpu_sriov { struct sriov_vf_state vfs[SRIOV_MAX_VF_SLOT]; u64 vf_reserve_offset; u64 vf_reserve_size; + struct sriov_sched sched; const struct sriov_init_func *init_funcs[SRIOV_MAX_INIT_FUNCS]; unsigned int num_init_funcs; diff --git a/drivers/gpu/drm/amd/sriov/sriov_mailbox.c b/drivers/gpu/drm/amd/sriov/sriov_mailbox.c index 23c0b9a5c79f..a218918990b2 100644 --- a/drivers/gpu/drm/amd/sriov/sriov_mailbox.c +++ b/drivers/gpu/drm/amd/sriov/sriov_mailbox.c @@ -161,6 +161,16 @@ static void sriov_mailbox_handle_vf2pf_valid(struct amdgpu_device *adev, vf_idx, msg_data[1], msg_data[2], msg_data[3]); return; } + r = sriov_sched_queue_event_ex( + adev, vf_idx, sched_event, SRIOV_GPUIOV_BLOCK_ALL, + msg_data); + if (r) { + adev->sriov->sched.dropped_event_cnt++; + dev_warn(adev->dev, + "SR-IOV mailbox: VF%u queue sched_event=%d failed r=%d dropped=%llu\n", + vf_idx, sched_event, r, + adev->sriov->sched.dropped_event_cnt); + } } static void sriov_mailbox_handle_pf2vf_ack(struct amdgpu_device *adev, diff --git a/drivers/gpu/drm/amd/sriov/sriov_sched.c b/drivers/gpu/drm/amd/sriov/sriov_sched.c new file mode 100644 index 000000000000..268a6914f7a5 --- /dev/null +++ b/drivers/gpu/drm/amd/sriov/sriov_sched.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: MIT +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#include "amdgpu.h" +#include "sriov.h" + +static void sriov_sched_reset_vf_runtime_state(struct sriov_sched_vf *vf); + +void sriov_sched_init_vf_state(struct amdgpu_device *adev, + struct sriov_sched_vf *vf) +{ + sriov_sched_reset_vf_runtime_state(vf); +} + +static void +sriov_sched_reset_vf_runtime_state(struct sriov_sched_vf *vf) +{ + vf->state = SRIOV_SCHED_VF_UNAVAILABLE; + vf->full_access_event = SRIOV_EVENT_INVALID; + vf->full_access_start = 0; + vf->init_data_ready = false; + vf->in_full_access = false; + vf->guest_gpu_init_flags = 0; + vf->guest_crit_region_caps = 0; + vf->guest_crit_region_version = 0; +} + +static int sriov_sched_sw_init(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + unsigned int i; + int r; + + if (!adev->num_vfs) + return -EINVAL; + + sched->num_vfs = adev->num_vfs; + for (i = 0; i < SRIOV_MAX_VF_SLOT; i++) + sriov_sched_reset_vf_runtime_state(&sched->vfs[i]); + + sriov_sched_event_queue_init(adev); + + r = sriov_sched_event_queue_process_init(adev); + if (r) + goto err; + + sched->sw_init = true; + + return 0; + +err: + sched->num_vfs = 0; + return r; +} + +static int sriov_sched_sw_fini(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + + sriov_sched_event_queue_process_fini(adev); + sriov_sched_event_queue_fini(adev); + sched->num_vfs = 0; + sched->sw_init = false; + + return 0; +} + +static int sriov_sched_hw_init(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + + if (!sched->sw_init) + return -EINVAL; + + sched->hw_init = true; + + return 0; +} + +static int sriov_sched_hw_fini(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + + sched->hw_init = false; + + return 0; +} + +static int sriov_sched_late_init(struct amdgpu_device *adev) +{ + return sriov_sched_event_queue_process_start(adev); +} + +static void sriov_sched_late_fini(struct amdgpu_device *adev) +{ + sriov_sched_event_queue_process_stop(adev); +} + +const struct sriov_init_func sriov_sched_func = { + .name = "sched", + .sw_init = sriov_sched_sw_init, + .sw_fini = sriov_sched_sw_fini, + .hw_init = sriov_sched_hw_init, + .hw_fini = sriov_sched_hw_fini, + .late_init = sriov_sched_late_init, + .late_fini = sriov_sched_late_fini, +}; diff --git a/drivers/gpu/drm/amd/sriov/sriov_sched_event.c b/drivers/gpu/drm/amd/sriov/sriov_sched_event.c new file mode 100644 index 000000000000..11155c6539ed --- /dev/null +++ b/drivers/gpu/drm/amd/sriov/sriov_sched_event.c @@ -0,0 +1,1125 @@ +// SPDX-License-Identifier: MIT +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#include +#include +#include + +#include "amdgpu.h" +#include "sriov.h" +#include "sriov_sched_event_dispatch.h" +#include "sriov_sched_event_priv.h" + +#define SRIOV_SCHED_EXCLUSIVE_TIMEOUT_MS_COMMON 1500 +#define SRIOV_SCHED_EVENT_QUEUE_SIZE 256 + +static unsigned int +sriov_sched_full_access_timeout_ms(struct amdgpu_device *adev) +{ + return SRIOV_SCHED_EXCLUSIVE_TIMEOUT_MS_COMMON; +} + +static int sriov_sched_sanitize_queue_event(struct amdgpu_device *adev, + unsigned int vf_idx, + enum sriov_sched_event_id id) +{ + struct sriov_sched *sched = &adev->sriov->sched; + const struct sriov_sched_event_desc *desc; + + if (!sched->hw_init || !sched->wq) + return -EINVAL; + if (!sriov_sched_vf_idx_valid(sched, vf_idx)) + return -EINVAL; + + desc = sriov_sched_event_get_desc(id); + if (!desc || desc->priority >= SRIOV_SCHED_EVENT_PRIO_MAX) + return -EINVAL; + + return 0; +} + +static void sriov_sched_remove_duplicated_event( + struct sriov_sched *sched, + enum sriov_sched_event_priority priority, + const struct sriov_sched_event *new_event) +{ + struct sriov_sched_event *event; + struct sriov_sched_event *tmp; + + list_for_each_entry_safe(event, tmp, &sched->event_list[priority], node) { + if (event->idx_vf != new_event->idx_vf) + continue; + + if (sched->next_event == event) + sched->next_event = NULL; + list_del(&event->node); + kfree(event); + sched->dropped_event_cnt++; + } +} + +static void sriov_sched_clear_event_list(struct sriov_sched *sched) +{ + struct sriov_sched_event *event; + struct sriov_sched_event *tmp; + unsigned int i; + + list_for_each_entry_safe(event, tmp, &sched->event_queue, node) { + list_del(&event->node); + kfree(event); + } + sched->queued_event_depth = 0; + + for (i = 0; i < SRIOV_SCHED_EVENT_PRIO_MAX; i++) { + list_for_each_entry_safe(event, tmp, &sched->event_list[i], node) { + list_del(&event->node); + kfree(event); + } + } + + sched->curr_event_priority = 0; + sched->next_event = NULL; +} + +static void sriov_sched_event_queue_pop(struct sriov_sched *sched, + struct list_head *head) +{ + INIT_LIST_HEAD(head); + list_splice_tail_init(&sched->event_queue, head); + sched->queued_event_depth = 0; +} + +static bool sriov_sched_has_pending_events(struct sriov_sched *sched) +{ + unsigned int i; + + if (!list_empty(&sched->event_queue)) + return true; + + for (i = 0; i < SRIOV_SCHED_EVENT_PRIO_MAX; i++) { + if (!list_empty(&sched->event_list[i])) + return true; + } + + return false; +} + +static void sriov_sched_arrange_event_list(struct sriov_sched *sched, + struct list_head *head) +{ + struct sriov_sched_event *event; + struct sriov_sched_event *tmp; + enum sriov_sched_event_priority priority; + + list_for_each_entry_safe(event, tmp, head, node) { + const struct sriov_sched_event_desc *desc; + + desc = sriov_sched_event_get_desc(event->id); + if (!desc || desc->priority >= SRIOV_SCHED_EVENT_PRIO_MAX) { + list_del(&event->node); + kfree(event); + sched->dropped_event_cnt++; + continue; + } + + priority = desc->priority; + if (desc->remove_duplicate) + sriov_sched_remove_duplicated_event(sched, priority, + event); + list_move_tail(&event->node, &sched->event_list[priority]); + } + + sched->curr_event_priority = 0; + sched->next_event = NULL; +} + +static struct sriov_sched_event * +sriov_sched_pick_next_event(struct sriov_sched *sched) +{ + struct sriov_sched_event *event; + struct list_head *event_list = NULL; + struct list_head queue_events; + unsigned int i; + + if (!list_empty(&sched->event_queue)) { + sriov_sched_event_queue_pop(sched, &queue_events); + sriov_sched_arrange_event_list(sched, &queue_events); + } + + if (!sched->next_event) { + for (i = 0; i < SRIOV_SCHED_EVENT_PRIO_MAX; i++) { + if (!list_empty(&sched->event_list[i])) { + event_list = &sched->event_list[i]; + sched->curr_event_priority = i; + break; + } + } + if (!event_list) + return NULL; + + event = list_first_entry(event_list, struct sriov_sched_event, + node); + } else { + event = sched->next_event; + } + + i = sched->curr_event_priority; + event_list = &sched->event_list[i]; + if (event->node.next == event_list) { + for (i++; i < SRIOV_SCHED_EVENT_PRIO_MAX; i++) { + if (!list_empty(&sched->event_list[i])) { + event_list = &sched->event_list[i]; + sched->curr_event_priority = i; + break; + } + } + sched->next_event = i == SRIOV_SCHED_EVENT_PRIO_MAX ? + NULL : list_first_entry(event_list, + struct sriov_sched_event, node); + } else { + sched->next_event = list_next_entry(event, node); + } + + list_del(&event->node); + return event; +} + +bool +sriov_sched_is_event_pair(const struct sriov_sched_vf *vf, + enum sriov_sched_event_id id) +{ + switch (vf->full_access_event) { + case SRIOV_EVENT_REQ_GPU_INIT: + case SRIOV_EVENT_REQ_GPU_RESET: + return id == SRIOV_EVENT_REL_GPU_INIT; + case SRIOV_EVENT_REQ_GPU_FINI: + return id == SRIOV_EVENT_REL_GPU_FINI; + default: + return false; + } +} + +bool +sriov_sched_any_share_engine_vf_in_full_access( + struct sriov_sched *sched, unsigned int vf_idx) +{ + if (!sriov_sched_vf_idx_valid(sched, vf_idx)) + return false; + + return sched->logical_sched_fa_mask & + sched->vfs[vf_idx].ws_mask; +} + +unsigned int +sriov_sched_active_vf_count(struct sriov_sched *sched) +{ + unsigned int i; + unsigned int count = 0; + + for (i = 0; i < sched->num_vfs; i++) { + if (sched->vfs[i].state == SRIOV_SCHED_VF_ACTIVE) + count++; + } + + return count; +} + +static bool sriov_sched_single_vf_path(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + + return sched->num_vfs == 1 || + sriov_sched_active_vf_count(sched) == 0; +} + +static bool sriov_sched_cp_sched_state(struct amdgpu_device *adev, + unsigned int vf_idx) +{ + if (!adev->sriov->gfx_funcs || + !adev->sriov->gfx_funcs->gpuiov_cp_sched_state) + return true; + + return adev->sriov->gfx_funcs->gpuiov_cp_sched_state(adev, vf_idx) != 0; +} + +static void +sriov_sched_queue_timeout_check(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf; + unsigned long flags; + unsigned long timeout; + unsigned long now; + unsigned long delay = 0; + unsigned long remain; + unsigned int i; + bool pending = false; + + timeout = msecs_to_jiffies(sriov_sched_full_access_timeout_ms(adev)); + now = jiffies; + spin_lock_irqsave(&sched->lock, flags); + if (!sched->event_enabled) + goto unlock; + + for (i = 0; i < sched->num_vfs; i++) { + vf = &sched->vfs[i]; + if (!vf->in_full_access) + continue; + if (!time_after(vf->full_access_start + timeout, now)) { + delay = 0; + pending = true; + break; + } + remain = vf->full_access_start + timeout - now; + if (!pending || remain < delay) + delay = remain; + pending = true; + } + + if (pending) + mod_delayed_work(sched->wq, &sched->timeout_work, delay); + else + cancel_delayed_work(&sched->timeout_work); + +unlock: + spin_unlock_irqrestore(&sched->lock, flags); +} + +void sriov_sched_clear_init_data_ready(struct amdgpu_device *adev, + unsigned int vf_idx) +{ + struct sriov_sched *sched; + + if (!adev->sriov) + return; + + sched = &adev->sriov->sched; + if (!sriov_sched_vf_idx_valid(sched, vf_idx)) + return; + + sched->vfs[vf_idx].init_data_ready = false; +} + +static int sriov_sched_psp_set_mb_int(struct amdgpu_device *adev, + unsigned int vf_idx, bool enable) +{ + if (!amdgpu_sriov_pf(adev)) + return 0; + if (!adev->sriov->psp_funcs || !adev->sriov->psp_funcs->set_mb_int) + return 0; + + return adev->sriov->psp_funcs->set_mb_int(adev, vf_idx, enable); +} + +static int +sriov_sched_toggle_rlcg_vf_interface(struct amdgpu_device *adev, + unsigned int vf_idx, bool enable) +{ + if (!adev->sriov->gfx_funcs || + !adev->sriov->gfx_funcs->toggle_rlcg_vf_interface) + return 0; + + return adev->sriov->gfx_funcs->toggle_rlcg_vf_interface(adev, vf_idx, + enable); +} + +static void +sriov_sched_close_full_access_gates(struct amdgpu_device *adev, + unsigned int vf_idx, u32 access) +{ + if (access & SRIOV_VF_ACCESS_MMIO_REG_WRITE) + sriov_sched_toggle_rlcg_vf_interface(adev, vf_idx, false); + sriov_vfmgr_set_vf_access(adev, vf_idx, access, false); + if (access == SRIOV_VF_ACCESS_ALL) + sriov_sched_psp_set_mb_int(adev, vf_idx, false); +} + +int sriov_sched_check_vf2pf_data(struct amdgpu_device *adev, + unsigned int vf_idx) +{ + struct amd_sriov_msg_vf2pf_info *vf2pf; + int r; + + vf2pf = kzalloc_obj(*vf2pf); + if (!vf2pf) + return -ENOMEM; + + r = sriov_vfmgr_retrieve_vf2pf_message(adev, vf_idx, vf2pf); + kfree(vf2pf); + + return r; +} + +int sriov_sched_handle_req_gpu_init_data(struct amdgpu_device *adev, + struct sriov_sched_event *event) +{ + unsigned int vf_idx = event->idx_vf; + const u32 *data = event->data; + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[vf_idx]; + struct sriov_vf_state *state = &adev->sriov->vfs[vf_idx]; + int r; + + vf->guest_gpu_init_flags = data[1]; + state->guest_gpu_init_flags = data[1]; + vf->guest_crit_region_caps = SRIOV_CRIT_CAP_BIT(GPU_CRIT_REGION_V1); + state->guest_crit_region_caps = vf->guest_crit_region_caps; + vf->guest_crit_region_version = GPU_CRIT_REGION_V1; + state->vf_crit_region = GPU_CRIT_REGION_V1; + + r = sriov_vfmgr_set_vf_access(adev, vf_idx, SRIOV_VF_ACCESS_FB, true); + if (r) + dev_warn(adev->dev, + "SR-IOV sched: VF%u enable FB access for init data failed r=%d\n", + vf_idx, r); + + r = sriov_vfmgr_init_crit_region(adev, vf_idx); + if (r) { + dev_warn(adev->dev, + "SR-IOV sched: VF%u init critical region failed r=%d\n", + vf_idx, r); + goto set_cond_avail; + } + + r = sriov_vfmgr_update_init_data(adev, vf_idx); + if (r) { + dev_err(adev->dev, + "SR-IOV sched: VF%u update init data failed r=%d, not sending GPU_INIT_DATA_READY\n", + vf_idx, r); + goto set_cond_avail; + } + + r = sriov_vfmgr_update_pf2vf_message(adev, vf_idx); + if (r) { + dev_err(adev->dev, + "SR-IOV sched: VF%u update PF2VF init data failed r=%d, not sending GPU_INIT_DATA_READY\n", + vf_idx, r); + goto set_cond_avail; + } + + r = sriov_mailbox_send_init_data(adev, vf_idx); + if (r) + return r; + + vf->init_data_ready = true; + if (vf->state == SRIOV_SCHED_VF_UNAVAILABLE || + vf->state == SRIOV_SCHED_VF_COND_AVAIL) + vf->state = SRIOV_SCHED_VF_AVAILABLE; + sched->req_gpu_init_data_cnt++; + + return 0; + +set_cond_avail: + vf->state = SRIOV_SCHED_VF_COND_AVAIL; + vf->init_data_ready = false; + return -EOPNOTSUPP; +} + +static int +sriov_sched_handle_req_gpu_init(struct amdgpu_device *adev, + unsigned int vf_idx, + enum sriov_gpuiov_sched_block sched_block) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[vf_idx]; + int r; + + if (vf->state == SRIOV_SCHED_VF_UNAVAILABLE || + vf->state == SRIOV_SCHED_VF_COND_AVAIL) + return -EINVAL; + + if (!vf->init_data_ready) { + struct sriov_sched_event init_data_event = { + .idx_vf = vf_idx, + .data = { 0, 0, GPU_CRIT_REGION_V1, 0 }, + }; + + r = sriov_sched_handle_req_gpu_init_data(adev, + &init_data_event); + if (r) + return r; + } + + if (!vf->init_data_ready) { + r = sriov_vfmgr_update_init_data(adev, vf_idx); + if (r) + return r; + + r = sriov_vfmgr_update_pf2vf_message(adev, vf_idx); + if (r) + return r; + } + + vf->init_data_ready = false; + sched->req_gpu_init_cnt++; + + return 0; +} + +static int +sriov_sched_handle_req_gpu_fini(struct amdgpu_device *adev, + unsigned int vf_idx, + enum sriov_gpuiov_sched_block sched_block) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[vf_idx]; + int r; + + if (vf->state == SRIOV_SCHED_VF_AVAILABLE) { + dev_warn(adev->dev, + "SR-IOV sched: REQ_GPU_FINI for available VF%u, send READY\n", + vf_idx); + sriov_mailbox_send_ready_to_access(adev, vf_idx); + return -EINVAL; + } + + if (vf->state != SRIOV_SCHED_VF_ACTIVE) { + dev_warn(adev->dev, + "SR-IOV sched: REQ_GPU_FINI invalid for non-active VF%u\n", + vf_idx); + return -EINVAL; + } + + sched->req_gpu_fini_cnt++; + + return 0; +} + +static int +sriov_sched_handle_req_gpu_reset(struct amdgpu_device *adev, + unsigned int vf_idx, + enum sriov_gpuiov_sched_block sched_block) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[vf_idx]; + int r; + + if (vf->state != SRIOV_SCHED_VF_AVAILABLE && + vf->state != SRIOV_SCHED_VF_ACTIVE) + return -EINVAL; + + r = sriov_vfmgr_update_pf2vf_message(adev, vf_idx); + if (r) + return r; + + vf->state = SRIOV_SCHED_VF_AVAILABLE; + vf->init_data_ready = false; + sched->req_gpu_reset_cnt++; + + return 0; +} + +static void sriov_sched_rlc_enter_safe_mode_vf(struct amdgpu_device *adev, + unsigned int vf_idx) +{ + struct sriov_sched_vf *vf = + sriov_sched_vf_slot(&adev->sriov->sched, vf_idx); + u32 xcc_mask = vf ? vf->xcc_mask : 0; + int xcc_id; + + if (!xcc_mask) + xcc_mask = BIT(0); + + for_each_inst(xcc_id, xcc_mask) + amdgpu_gfx_rlc_enter_safe_mode(adev, xcc_id); +} + +static void sriov_sched_rlc_exit_safe_mode_vf(struct amdgpu_device *adev, + unsigned int vf_idx) +{ + struct sriov_sched_vf *vf = + sriov_sched_vf_slot(&adev->sriov->sched, vf_idx); + u32 xcc_mask = vf ? vf->xcc_mask : 0; + int xcc_id; + + if (!xcc_mask) + xcc_mask = BIT(0); + + for_each_inst(xcc_id, xcc_mask) + amdgpu_gfx_rlc_exit_safe_mode(adev, xcc_id); +} + +static void +sriov_sched_clear_full_access(struct amdgpu_device *adev, unsigned int vf_idx) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[vf_idx]; + + vf->in_full_access = false; + vf->full_access_event = SRIOV_EVENT_INVALID; + vf->full_access_start = 0; + vf->used_time_full_access = 0; + sched->logical_sched_fa_mask &= ~vf->ws_mask; + sriov_sched_queue_timeout_check(adev); +} + +void sriov_sched_exit_full_access_timeout(struct amdgpu_device *adev, + unsigned int vf_idx) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[vf_idx]; + + if (!vf->in_full_access) + return; + + dev_warn(adev->dev, + "SR-IOV sched: VF%u full access timeout/mismatch exit event=0x%x\n", + vf_idx, vf->full_access_event); + + vf->state = SRIOV_SCHED_VF_AVAILABLE; + vf->init_data_ready = false; + + sriov_sched_rlc_exit_safe_mode_vf(adev, vf_idx); + sriov_sched_close_full_access_gates( + adev, vf_idx, + SRIOV_VF_ACCESS_DOORBELL | + SRIOV_VF_ACCESS_MMIO_REG_WRITE); + + sriov_sched_psp_set_mb_int(adev, vf_idx, false); + sriov_sched_toggle_rlcg_vf_interface(adev, vf_idx, false); + sriov_sched_clear_full_access(adev, vf_idx); + sched->full_access_timeout_cnt++; +} + +int sriov_sched_handle_orphan_vf(struct amdgpu_device *adev, + unsigned int vf_idx) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[vf_idx]; + + if (vf->state != SRIOV_SCHED_VF_ACTIVE) + return 0; + + vf->state = SRIOV_SCHED_VF_AVAILABLE; + + return 0; +} + +int sriov_sched_enter_full_access(struct amdgpu_device *adev, + const struct sriov_sched_event *event) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[event->idx_vf]; + bool rlc_safe_entered = false; + int r; + + if (vf->in_full_access) { + sched->full_access_error_cnt++; + return -EBUSY; + } + + if (event->id == SRIOV_EVENT_REQ_GPU_INIT || + event->id == SRIOV_EVENT_REQ_GPU_RESET) { + r = sriov_sched_handle_orphan_vf(adev, event->idx_vf); + if (r) + goto err_restart_sched; + } + + r = sriov_sched_psp_set_mb_int(adev, event->idx_vf, true); + if (r) + goto err_restart_sched; + + r = sriov_vfmgr_set_vf_access(adev, event->idx_vf, + SRIOV_VF_ACCESS_ALL, true); + if (r) + goto err_disable_mb_int; + + sriov_sched_rlc_enter_safe_mode_vf(adev, event->idx_vf); + rlc_safe_entered = true; + + switch (event->id) { + case SRIOV_EVENT_REQ_GPU_INIT: + r = sriov_sched_handle_req_gpu_init(adev, + event->idx_vf, + event->sched_block); + break; + case SRIOV_EVENT_REQ_GPU_FINI: + r = sriov_sched_handle_req_gpu_fini(adev, + event->idx_vf, + event->sched_block); + break; + case SRIOV_EVENT_REQ_GPU_RESET: + r = sriov_sched_handle_req_gpu_reset(adev, + event->idx_vf, + event->sched_block); + break; + default: + r = -EINVAL; + break; + } + if (r) + goto err_close_gates; + + vf->in_full_access = true; + vf->full_access_event = event->id; + vf->full_access_start = jiffies; + vf->used_time_full_access = 0; + sched->logical_sched_fa_mask |= vf->ws_mask; + + r = sriov_sched_toggle_rlcg_vf_interface(adev, event->idx_vf, true); + if (r) + goto err_cancel_full_access; + + r = sriov_mailbox_send_ready_to_access(adev, event->idx_vf); + if (r) + goto err_cancel_full_access; + + sriov_sched_queue_timeout_check(adev); + return SRIOV_SCHED_EVENT_STOP_AND_RELEASE; + +err_cancel_full_access: + sriov_sched_clear_full_access(adev, event->idx_vf); +err_close_gates: + if (rlc_safe_entered) + sriov_sched_rlc_exit_safe_mode_vf(adev, event->idx_vf); + sriov_sched_close_full_access_gates( + adev, event->idx_vf, + SRIOV_VF_ACCESS_DOORBELL | + SRIOV_VF_ACCESS_MMIO_REG_WRITE); +err_disable_mb_int: + sriov_sched_psp_set_mb_int(adev, event->idx_vf, false); +err_restart_sched: + return r; +} + +static int +sriov_sched_handle_rel_gpu_init(struct amdgpu_device *adev, + unsigned int vf_idx, bool skip_save, + enum sriov_gpuiov_sched_block sched_block) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[vf_idx]; + + vf->state = SRIOV_SCHED_VF_ACTIVE; + sched->rel_gpu_init_cnt++; + + return 0; +} + +static int +sriov_sched_handle_rel_gpu_fini(struct amdgpu_device *adev, + unsigned int vf_idx, + enum sriov_gpuiov_sched_block sched_block) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[vf_idx]; + int r; + + if (adev->sriov->psp_funcs && adev->sriov->psp_funcs->clear_vf_fw) { + r = adev->sriov->psp_funcs->clear_vf_fw(adev, vf_idx); + if (r) + return r; + } + + vf->state = SRIOV_SCHED_VF_AVAILABLE; + vf->init_data_ready = false; + sched->rel_gpu_fini_cnt++; + + return 0; +} + +int sriov_sched_exit_full_access(struct amdgpu_device *adev, + const struct sriov_sched_event *event) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[event->idx_vf]; + bool one_vf = false; + int r = 0; + int cleanup_r; + + if (!vf->in_full_access || + !sriov_sched_is_event_pair(vf, event->id)) { + sched->full_access_error_cnt++; + return -EINVAL; + } + + one_vf = sriov_sched_single_vf_path(adev); + + sriov_sched_rlc_exit_safe_mode_vf(adev, event->idx_vf); + + switch (event->id) { + case SRIOV_EVENT_REL_GPU_INIT: + cleanup_r = sriov_sched_handle_rel_gpu_init( + adev, event->idx_vf, one_vf, event->sched_block); + if (cleanup_r && !r) + r = cleanup_r; + break; + case SRIOV_EVENT_REL_GPU_FINI: + sriov_sched_close_full_access_gates( + adev, event->idx_vf, + SRIOV_VF_ACCESS_DOORBELL | + SRIOV_VF_ACCESS_FB); + + cleanup_r = sriov_sched_handle_rel_gpu_fini( + adev, event->idx_vf, event->sched_block); + if (cleanup_r && !r) + r = cleanup_r; + break; + default: + r = -EINVAL; + goto out_clear_full_access; + } + + cleanup_r = sriov_sched_toggle_rlcg_vf_interface(adev, event->idx_vf, false); + if (cleanup_r && !r) + r = cleanup_r; + cleanup_r = sriov_vfmgr_set_vf_access(adev, event->idx_vf, + SRIOV_VF_ACCESS_MMIO_REG_WRITE, false); + if (cleanup_r && !r) + r = cleanup_r; + + cleanup_r = sriov_sched_psp_set_mb_int(adev, event->idx_vf, false); + if (cleanup_r && !r) + r = cleanup_r; + +out_clear_full_access: + sriov_sched_clear_full_access(adev, event->idx_vf); + + return r ? r : SRIOV_SCHED_EVENT_CONTINUE; +} + +void sriov_sched_push_back_event(struct sriov_sched *sched, + struct sriov_sched_event *event) +{ + const struct sriov_sched_event_desc *desc; + enum sriov_sched_event_priority priority; + + desc = sriov_sched_event_get_desc(event->id); + if (!desc || desc->priority >= SRIOV_SCHED_EVENT_PRIO_MAX) { + kfree(event); + sched->dropped_event_cnt++; + return; + } + + priority = desc->priority; + sched->curr_event_priority = priority; + list_add(&event->node, &sched->event_list[priority]); + sched->next_event = event; +} + +static int +sriov_sched_process_one_event(struct amdgpu_device *adev, + struct sriov_sched_event *event) +{ + struct sriov_sched *sched = &adev->sriov->sched; + const struct sriov_sched_event_desc *desc; + sriov_sched_event_process_fn fn; + bool in_fa; + + if (!event || !sriov_sched_vf_idx_valid(sched, event->idx_vf)) + return -EINVAL; + + desc = sriov_sched_event_get_desc(event->id); + if (!desc) + return -EINVAL; + + if (sched->vfs[event->idx_vf].init_data_ready && + event->id != SRIOV_EVENT_REQ_GPU_INIT) + sched->vfs[event->idx_vf].init_data_ready = false; + + fn = desc->process; + if (!fn) + return SRIOV_SCHED_EVENT_CONTINUE; + + in_fa = sriov_sched_any_share_engine_vf_in_full_access(sched, + event->idx_vf); + + return fn(adev, event, in_fa); +} + +static void sriov_sched_process_event(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_event *event; + unsigned long flags; + bool has_queued_events; + int stop; + + do { + while (true) { + spin_lock_irqsave(&sched->lock, flags); + event = sched->event_enabled ? + sriov_sched_pick_next_event(sched) : NULL; + spin_unlock_irqrestore(&sched->lock, flags); + if (!event) + break; + + stop = sriov_sched_process_one_event(adev, event); + + if (stop == SRIOV_SCHED_EVENT_STOP_AND_RELEASE) { + sched->processed_event_cnt++; + kfree(event); + spin_lock_irqsave(&sched->lock, flags); + if (sched->event_enabled && + sriov_sched_has_pending_events(sched)) + queue_work(sched->wq, &sched->work); + spin_unlock_irqrestore(&sched->lock, flags); + break; + } else if (stop == SRIOV_SCHED_EVENT_STOP_AND_KEEP) { + sriov_sched_push_back_event(sched, event); + break; + } + + if (stop < 0) + sched->invalid_event_cnt++; + else + sched->processed_event_cnt++; + kfree(event); + } + + spin_lock_irqsave(&sched->lock, flags); + has_queued_events = sched->event_enabled && + !list_empty(&sched->event_queue); + spin_unlock_irqrestore(&sched->lock, flags); + } while (has_queued_events); +} + +static void sriov_sched_work(struct work_struct *work) +{ + struct sriov_sched *sched = + container_of(work, struct sriov_sched, work); + struct amdgpu_sriov *sriov = container_of(sched, struct amdgpu_sriov, sched); + + sriov_sched_process_event(sriov->adev); +} + +static void sriov_sched_timeout_work(struct work_struct *work) +{ + struct sriov_sched *sched = + container_of(to_delayed_work(work), struct sriov_sched, + timeout_work); + struct amdgpu_sriov *sriov = container_of(sched, struct amdgpu_sriov, + sched); + struct sriov_sched_vf *vf; + unsigned long flags; + unsigned long timeout; + unsigned int i; + bool exited = false; + + timeout = msecs_to_jiffies( + sriov_sched_full_access_timeout_ms(sriov->adev)); + spin_lock_irqsave(&sched->lock, flags); + if (!sched->event_enabled) { + spin_unlock_irqrestore(&sched->lock, flags); + return; + } + if (sched->lock_world_switch) { + spin_unlock_irqrestore(&sched->lock, flags); + return; + } + spin_unlock_irqrestore(&sched->lock, flags); + + for (i = 0; i < sched->num_vfs; i++) { + vf = &sched->vfs[i]; + if (!vf->in_full_access) + continue; + if (time_is_after_jiffies(vf->full_access_start + timeout)) + continue; + + dev_warn(sriov->adev->dev, + "SR-IOV sched: VF%u full access timeout event=0x%x\n", + i, vf->full_access_event); + sriov_sched_exit_full_access_timeout(sriov->adev, i); + exited = true; + } + + sriov_sched_queue_timeout_check(sriov->adev); + + /* + * STOP_AND_KEEP events may be waiting on FA. After releasing FA, + * kick the event worker so they are not stuck until an unrelated + * new event arrives. + */ + if (exited) { + spin_lock_irqsave(&sched->lock, flags); + if (sched->event_enabled && + sriov_sched_has_pending_events(sched)) + queue_work(sched->wq, &sched->work); + spin_unlock_irqrestore(&sched->lock, flags); + } +} + +int sriov_sched_queue_event_ex(struct amdgpu_device *adev, + unsigned int vf_idx, + enum sriov_sched_event_id id, + enum sriov_gpuiov_sched_block sched_block, + const u32 *data) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_event *event; + unsigned long flags; + int r; + + r = sriov_sched_sanitize_queue_event(adev, vf_idx, id); + if (r) + return r; + + event = kzalloc_obj(*event, GFP_ATOMIC); + if (!event) + return -ENOMEM; + + INIT_LIST_HEAD(&event->node); + event->idx_vf = vf_idx; + event->id = id; + event->sched_block = sched_block; + if (data) + memcpy(event->data, data, sizeof(event->data)); + + spin_lock_irqsave(&sched->lock, flags); + if (sched->queued_event_depth >= SRIOV_SCHED_EVENT_QUEUE_SIZE) { + spin_unlock_irqrestore(&sched->lock, flags); + kfree(event); + sched->dropped_event_cnt++; + return -EOVERFLOW; + } + list_add_tail(&event->node, &sched->event_queue); + sched->queued_event_depth++; + if (sched->event_enabled) + queue_work(sched->wq, &sched->work); + spin_unlock_irqrestore(&sched->lock, flags); + + sched->queued_event_cnt++; + + return 0; +} + +void sriov_sched_event_queue_init(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + unsigned int i; + + INIT_LIST_HEAD(&sched->event_queue); + for (i = 0; i < SRIOV_SCHED_EVENT_PRIO_MAX; i++) + INIT_LIST_HEAD(&sched->event_list[i]); + sched->curr_event_priority = 0; + sched->next_event = NULL; + spin_lock_init(&sched->lock); + INIT_WORK(&sched->work, sriov_sched_work); + INIT_DELAYED_WORK(&sched->timeout_work, sriov_sched_timeout_work); +} + +void sriov_sched_event_queue_fini(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + + cancel_delayed_work_sync(&sched->timeout_work); + cancel_work_sync(&sched->work); + sriov_sched_clear_event_list(sched); +} + +int sriov_sched_event_queue_process_init(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + + sched->event_enabled = false; + sriov_sched_clear_event_list(sched); + sched->wq = alloc_ordered_workqueue("sriov_sched", 0); + if (!sched->wq) + return -ENOMEM; + + sched->queued_event_cnt = 0; + sched->queued_event_depth = 0; + sched->processed_event_cnt = 0; + sched->dropped_event_cnt = 0; + sched->invalid_event_cnt = 0; + sched->full_access_error_cnt = 0; + sched->full_access_timeout_cnt = 0; + sched->req_gpu_init_cnt = 0; + sched->rel_gpu_init_cnt = 0; + sched->req_gpu_fini_cnt = 0; + sched->rel_gpu_fini_cnt = 0; + sched->req_gpu_init_data_cnt = 0; + sched->req_gpu_reset_cnt = 0; + sched->curr_event_priority = 0; + sched->next_event = NULL; + + return 0; +} + +void sriov_sched_event_queue_process_fini(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + unsigned int i; + bool vfmgr_hw_init; + + sriov_sched_event_queue_process_stop(adev); + + vfmgr_hw_init = adev->sriov->vfmgr.hw_init; + if (vfmgr_hw_init) { + for (i = 0; i < sched->num_vfs; i++) + sriov_sched_close_full_access_gates( + adev, i, SRIOV_VF_ACCESS_ALL); + } + + for (i = 0; i < sched->num_vfs; i++) + sriov_sched_init_vf_state(adev, &sched->vfs[i]); + + if (sched->wq) { + destroy_workqueue(sched->wq); + sched->wq = NULL; + } + + sriov_sched_clear_event_list(sched); +} + +int sriov_sched_event_queue_process_start(struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + unsigned long flags; + + if (!sched->hw_init || !sched->wq) + return -EINVAL; + + spin_lock_irqsave(&sched->lock, flags); + sched->event_enabled = true; + if (sriov_sched_has_pending_events(sched)) + queue_work(sched->wq, &sched->work); + spin_unlock_irqrestore(&sched->lock, flags); + + sriov_sched_queue_timeout_check(adev); + + return 0; +} + +bool sriov_sched_event_queue_process_stop( + struct amdgpu_device *adev) +{ + struct sriov_sched *sched = &adev->sriov->sched; + unsigned long flags; + bool was_enabled; + + if (!sched->sw_init) + return false; + + spin_lock_irqsave(&sched->lock, flags); + was_enabled = sched->event_enabled; + sched->event_enabled = false; + spin_unlock_irqrestore(&sched->lock, flags); + + if (sched->wq) + flush_workqueue(sched->wq); + cancel_delayed_work_sync(&sched->timeout_work); + + return was_enabled; +} diff --git a/drivers/gpu/drm/amd/sriov/sriov_sched_event.h b/drivers/gpu/drm/amd/sriov/sriov_sched_event.h new file mode 100644 index 000000000000..24cd5d0da7ad --- /dev/null +++ b/drivers/gpu/drm/amd/sriov/sriov_sched_event.h @@ -0,0 +1,177 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef __SRIOV_SCHED_EVENT_H__ +#define __SRIOV_SCHED_EVENT_H__ + +#include +#include +#include +#include +#include +#include +#include + +#include "sriov_gpuiov.h" +#include "sriov_mailbox.h" +#include "sriov_sched_event_data.h" + +struct amdgpu_device; +struct sriov_init_func; + +enum sriov_sched_event_priority { + SRIOV_SCHED_EVENT_PRIO_0 = 0, + SRIOV_SCHED_EVENT_PRIO_1, + SRIOV_SCHED_EVENT_PRIO_2, + SRIOV_SCHED_EVENT_PRIO_3, + SRIOV_SCHED_EVENT_PRIO_4, + SRIOV_SCHED_EVENT_PRIO_5, + SRIOV_SCHED_EVENT_PRIO_MAX = 6, +}; + +enum sriov_sched_vf_state { + SRIOV_SCHED_VF_UNAVAILABLE = 0, + SRIOV_SCHED_VF_AVAILABLE, + SRIOV_SCHED_VF_COND_AVAIL, + SRIOV_SCHED_VF_ACTIVE, +}; + +#ifndef SRIOV_CRIT_CAP_BIT +#define SRIOV_CRIT_CAP_BIT(_version) BIT((_version) - 1) +#endif + +struct sriov_sched_event { + struct list_head node; + u32 idx_vf; + enum sriov_sched_event_id id; + enum sriov_gpuiov_sched_block sched_block; + u32 data[SRIOV_SCHED_EVENT_DATA_WORDS]; + struct completion *wait; + int *wait_ret; +}; + +struct sriov_sched_block_map { + u32 ws_mask; + u32 hw_sched_mask; +}; + +struct sriov_sched_vf { + enum sriov_sched_vf_state state; + enum sriov_sched_event_id full_access_event; + u32 ws_mask; + u32 hw_sched_mask; + u32 xcc_mask; + struct sriov_sched_block_map + block_map[SRIOV_GPUIOV_BLOCK_MAX]; + u32 time_slice[SRIOV_GPUIOV_BLOCK_MAX]; + unsigned long full_access_start; + unsigned long used_time_full_access; + struct completion ready_to_reset; + bool init_data_ready; + bool in_full_access; + bool unshutdown; + bool skip_run; + bool auto_run; + u32 guest_gpu_init_flags; + u32 guest_crit_region_caps; + u32 guest_crit_region_version; +}; + +struct sriov_sched { + unsigned int num_vfs; + bool setup_vf_timeslice; + bool sw_init; + bool hw_init; + bool event_enabled; + struct workqueue_struct *wq; + struct work_struct work; + struct delayed_work timeout_work; + struct list_head event_queue; + struct list_head event_list[SRIOV_SCHED_EVENT_PRIO_MAX]; + enum sriov_sched_event_priority curr_event_priority; + struct sriov_sched_event *next_event; + /* Protects the event queues, queue counters and event_enabled. */ + spinlock_t lock; + u64 queued_event_cnt; + u32 queued_event_depth; + u64 processed_event_cnt; + u64 dropped_event_cnt; + u64 invalid_event_cnt; + u64 full_access_error_cnt; + u64 full_access_timeout_cnt; + u64 req_gpu_init_cnt; + u64 rel_gpu_init_cnt; + u64 req_gpu_fini_cnt; + u64 rel_gpu_fini_cnt; + u64 req_gpu_init_data_cnt; + u64 req_gpu_reset_cnt; + bool lock_world_switch; + u32 logical_sched_fa_mask; + struct sriov_sched_vf vfs[SRIOV_MAX_VF_SLOT]; +}; + +static inline bool sriov_sched_vf_idx_valid(const struct sriov_sched *sched, + unsigned int vf_idx) +{ + if (vf_idx >= SRIOV_MAX_VF_SLOT) + return false; + + return vf_idx == SRIOV_GPUIOV_PF_FUNC_ID || vf_idx < sched->num_vfs; +} + +static inline struct sriov_sched_vf * +sriov_sched_vf_slot(struct sriov_sched *sched, unsigned int vf_idx) +{ + if (!sriov_sched_vf_idx_valid(sched, vf_idx)) + return NULL; + + return &sched->vfs[vf_idx]; +} + +int sriov_sched_queue_event_ex(struct amdgpu_device *adev, + unsigned int vf_idx, + enum sriov_sched_event_id id, + enum sriov_gpuiov_sched_block sched_block, + const u32 *data); +int sriov_sched_queue_event_and_wait( + struct amdgpu_device *adev, unsigned int vf_idx, + enum sriov_sched_event_id id, + enum sriov_gpuiov_sched_block sched_block, + const u32 *data); +void sriov_sched_clear_init_data_ready(struct amdgpu_device *adev, + unsigned int vf_idx); +void sriov_sched_init_vf_state(struct amdgpu_device *adev, + struct sriov_sched_vf *vf); +unsigned int sriov_sched_active_vf_count( + struct sriov_sched *sched); +void sriov_sched_event_queue_init(struct amdgpu_device *adev); +void sriov_sched_event_queue_fini(struct amdgpu_device *adev); +int sriov_sched_event_queue_process_init(struct amdgpu_device *adev); +void sriov_sched_event_queue_process_fini(struct amdgpu_device *adev); +int sriov_sched_event_queue_process_start(struct amdgpu_device *adev); +bool sriov_sched_event_queue_process_stop( + struct amdgpu_device *adev); +extern const struct sriov_init_func sriov_sched_func; + +#endif diff --git a/drivers/gpu/drm/amd/sriov/sriov_sched_event_data.h b/drivers/gpu/drm/amd/sriov/sriov_sched_event_data.h new file mode 100644 index 000000000000..13e0455fed1c --- /dev/null +++ b/drivers/gpu/drm/amd/sriov/sriov_sched_event_data.h @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef __SRIOV_SCHED_EVENT_DATA_H__ +#define __SRIOV_SCHED_EVENT_DATA_H__ + +#include +#include + +/* + * Packed sched event payload. + * + * Word layout: + * [0] type-specific primary + * [1..2] primary 64-bit pointer (query, copy, …) + * [3..4] optional int *result + * [5] reserved + */ +#define SRIOV_SCHED_EVENT_DATA_WORDS 6 + +static inline void sriov_sched_event_data_clear(u32 data[SRIOV_SCHED_EVENT_DATA_WORDS]) +{ + memset(data, 0, sizeof(u32) * SRIOV_SCHED_EVENT_DATA_WORDS); +} + +static inline void sriov_sched_event_data_set_ptr(u32 data[SRIOV_SCHED_EVENT_DATA_WORDS], + void *ptr) +{ + data[1] = lower_32_bits((uintptr_t)ptr); + data[2] = upper_32_bits((uintptr_t)ptr); +} + +static inline void *sriov_sched_event_data_ptr(const u32 data[SRIOV_SCHED_EVENT_DATA_WORDS]) +{ + return (void *)((u64)data[2] << 32 | data[1]); +} + +static inline void sriov_sched_event_data_set_result(u32 data[SRIOV_SCHED_EVENT_DATA_WORDS], + int *result) +{ + data[3] = lower_32_bits((uintptr_t)result); + data[4] = upper_32_bits((uintptr_t)result); +} + +static inline int *sriov_sched_event_data_result(const u32 data[SRIOV_SCHED_EVENT_DATA_WORDS]) +{ + uintptr_t addr = (uintptr_t)((u64)data[4] << 32 | data[3]); + + if (!addr) + return NULL; + return (int *)addr; +} + +static inline void sriov_sched_event_write_result( + const u32 data[SRIOV_SCHED_EVENT_DATA_WORDS], int val) +{ + int *result = sriov_sched_event_data_result(data); + + if (result) + *result = val; +} + +#endif diff --git a/drivers/gpu/drm/amd/sriov/sriov_sched_event_dispatch.c b/drivers/gpu/drm/amd/sriov/sriov_sched_event_dispatch.c new file mode 100644 index 000000000000..6db7832632fa --- /dev/null +++ b/drivers/gpu/drm/amd/sriov/sriov_sched_event_dispatch.c @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: MIT +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +/* + * Static event descriptor table: one process handler per event id. + */ + +#include "amdgpu.h" +#include "sriov.h" +#include "sriov_sched_event_dispatch.h" +#include "sriov_sched_event_handlers.h" + +#define SRIOV_EVENT_DESC(_name, _priority, _dedup, _process) \ + { \ + .name = (_name), \ + .priority = (_priority), \ + .remove_duplicate = (_dedup), \ + .process = (_process), \ + } + +static const struct sriov_sched_event_desc sriov_event_desc_req_gpu_init = + SRIOV_EVENT_DESC("REQ_GPU_INIT", + SRIOV_SCHED_EVENT_PRIO_4, true, + sriov_sched_event_req_gpu_init); +static const struct sriov_sched_event_desc sriov_event_desc_rel_gpu_init = + SRIOV_EVENT_DESC("REL_GPU_INIT", + SRIOV_SCHED_EVENT_PRIO_2, true, + sriov_sched_event_rel_gpu_init); +static const struct sriov_sched_event_desc sriov_event_desc_req_gpu_fini = + SRIOV_EVENT_DESC("REQ_GPU_FINI", + SRIOV_SCHED_EVENT_PRIO_4, true, + sriov_sched_event_req_gpu_fini); +static const struct sriov_sched_event_desc sriov_event_desc_rel_gpu_fini = + SRIOV_EVENT_DESC("REL_GPU_FINI", + SRIOV_SCHED_EVENT_PRIO_2, true, + sriov_sched_event_rel_gpu_fini); +static const struct sriov_sched_event_desc sriov_event_desc_req_gpu_reset = + SRIOV_EVENT_DESC("REQ_GPU_RESET", + SRIOV_SCHED_EVENT_PRIO_4, true, + sriov_sched_event_req_gpu_reset); +static const struct sriov_sched_event_desc +sriov_event_desc_req_gpu_init_data = + SRIOV_EVENT_DESC("REQ_GPU_INIT_DATA", + SRIOV_SCHED_EVENT_PRIO_4, true, + sriov_sched_event_req_gpu_init_data); +const struct sriov_sched_event_desc * +sriov_sched_event_get_desc(enum sriov_sched_event_id id) +{ + switch (id) { + case SRIOV_EVENT_REQ_GPU_INIT: + return &sriov_event_desc_req_gpu_init; + case SRIOV_EVENT_REL_GPU_INIT: + return &sriov_event_desc_rel_gpu_init; + case SRIOV_EVENT_REQ_GPU_FINI: + return &sriov_event_desc_req_gpu_fini; + case SRIOV_EVENT_REL_GPU_FINI: + return &sriov_event_desc_rel_gpu_fini; + case SRIOV_EVENT_REQ_GPU_RESET: + return &sriov_event_desc_req_gpu_reset; + case SRIOV_EVENT_REQ_GPU_INIT_DATA: + return &sriov_event_desc_req_gpu_init_data; + default: + return NULL; + } +} + +#undef SRIOV_EVENT_DESC diff --git a/drivers/gpu/drm/amd/sriov/sriov_sched_event_dispatch.h b/drivers/gpu/drm/amd/sriov/sriov_sched_event_dispatch.h new file mode 100644 index 000000000000..791b6bdc92dd --- /dev/null +++ b/drivers/gpu/drm/amd/sriov/sriov_sched_event_dispatch.h @@ -0,0 +1,62 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef __SRIOV_SCHED_EVENT_DISPATCH_H__ +#define __SRIOV_SCHED_EVENT_DISPATCH_H__ + +#include "sriov_mailbox.h" +#include "sriov_sched_event.h" + +struct amdgpu_device; +struct sriov_sched_event; + +enum sriov_sched_event_process_result { + SRIOV_SCHED_EVENT_CONTINUE = 0, + SRIOV_SCHED_EVENT_STOP_AND_RELEASE, + SRIOV_SCHED_EVENT_STOP_AND_KEEP, +}; + +typedef int (*sriov_sched_event_process_fn)( + struct amdgpu_device *adev, struct sriov_sched_event *event, + bool in_fa); + +/** + * struct sriov_sched_event_desc - static descriptor for one sched event. + * + * @priority: drain band (PRIO_0 .. PRIO_5, highest first). + * @remove_duplicate: per-VF dedup within the same priority band. + * @process: single handler for this event; @in_fa is precomputed by the worker + * (shared-engine full-access context for event->idx_vf). + */ +struct sriov_sched_event_desc { + const char *name; + enum sriov_sched_event_priority priority; + bool remove_duplicate; + sriov_sched_event_process_fn process; +}; + +const struct sriov_sched_event_desc * +sriov_sched_event_get_desc(enum sriov_sched_event_id id); + +#endif diff --git a/drivers/gpu/drm/amd/sriov/sriov_sched_event_handlers.c b/drivers/gpu/drm/amd/sriov/sriov_sched_event_handlers.c new file mode 100644 index 000000000000..2f2bfded4989 --- /dev/null +++ b/drivers/gpu/drm/amd/sriov/sriov_sched_event_handlers.c @@ -0,0 +1,179 @@ +// SPDX-License-Identifier: MIT +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +/* + * One handler per sched event. Each handler branches on FA vs NFA + * and owns unavail / lock_world_switch gates for that event on each path. + */ + +#include "amdgpu.h" +#include "sriov.h" +#include "sriov_sched_event_handlers.h" +#include "sriov_sched_event_priv.h" +#include "sriov_sched_event.h" + +int sriov_sched_event_req_gpu_init(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[event->idx_vf]; + int r; + + if (in_fa || sched->lock_world_switch) { + if (is_unavail_vf(vf)) + return 0; + return SRIOV_SCHED_EVENT_STOP_AND_KEEP; + } + + if (is_unavail_vf(vf) || + is_cond_avail_vf(vf)) + return 0; + + r = sriov_sched_enter_full_access(adev, event); + return r < 0 ? r : SRIOV_SCHED_EVENT_STOP_AND_RELEASE; +} + +int sriov_sched_event_rel_gpu_init(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[event->idx_vf]; + int r; + + if (!in_fa || is_unavail_vf(vf) || + !is_full_access_vf(vf)) + return 0; + + if (sched->lock_world_switch) + return SRIOV_SCHED_EVENT_STOP_AND_KEEP; + + if (!sriov_sched_is_event_pair(vf, event->id)) { + dev_info(adev->dev, + "SR-IOV sched: REL_GPU_INIT without REQ_GPU_INIT VF%u\n", + event->idx_vf); + return 0; + } + + r = sriov_sched_exit_full_access(adev, event); + if (r) + return r; + + sriov_sched_check_vf2pf_data(adev, event->idx_vf); + return 0; +} + +int sriov_sched_event_req_gpu_fini(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[event->idx_vf]; + int r; + + if (in_fa || sched->lock_world_switch) { + if (is_unavail_vf(vf)) + return 0; + return SRIOV_SCHED_EVENT_STOP_AND_KEEP; + } + + if (is_unavail_vf(vf)) + return 0; + + if (!is_active_vf(vf)) { + dev_warn(adev->dev, + "SR-IOV sched: REQ_GPU_FINI invalid for non-active VF%u\n", + event->idx_vf); + return 0; + } + + r = sriov_sched_enter_full_access(adev, event); + return r < 0 ? r : SRIOV_SCHED_EVENT_STOP_AND_RELEASE; +} + +int sriov_sched_event_rel_gpu_fini(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[event->idx_vf]; + + if (!in_fa || is_unavail_vf(vf) || + !is_full_access_vf(vf)) + return 0; + + if (sched->lock_world_switch) + return SRIOV_SCHED_EVENT_STOP_AND_KEEP; + + if (!sriov_sched_is_event_pair(vf, event->id)) { + dev_info(adev->dev, + "SR-IOV sched: REL_GPU_FINI without REQ_GPU_FINI VF%u\n", + event->idx_vf); + sriov_sched_exit_full_access_timeout(adev, event->idx_vf); + return 0; + } + + return sriov_sched_exit_full_access(adev, event); +} + +int sriov_sched_event_req_gpu_reset(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[event->idx_vf]; + int r; + + if (in_fa || sched->lock_world_switch) { + if (is_unavail_vf(vf)) + return 0; + return SRIOV_SCHED_EVENT_STOP_AND_KEEP; + } + + if (!is_avail_or_active_vf(vf)) + return 0; + + r = sriov_sched_enter_full_access(adev, event); + return r < 0 ? r : SRIOV_SCHED_EVENT_STOP_AND_RELEASE; +} + +int sriov_sched_event_req_gpu_init_data(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa) +{ + struct sriov_sched *sched = &adev->sriov->sched; + struct sriov_sched_vf *vf = &sched->vfs[event->idx_vf]; + + if (in_fa || sched->lock_world_switch) { + if (is_unavail_vf(vf)) + return 0; + return SRIOV_SCHED_EVENT_STOP_AND_KEEP; + } + + if (is_unavail_vf(vf)) + return 0; + + return sriov_sched_handle_req_gpu_init_data(adev, event); +} diff --git a/drivers/gpu/drm/amd/sriov/sriov_sched_event_handlers.h b/drivers/gpu/drm/amd/sriov/sriov_sched_event_handlers.h new file mode 100644 index 000000000000..1bed50abec39 --- /dev/null +++ b/drivers/gpu/drm/amd/sriov/sriov_sched_event_handlers.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +/* + * One handler per sched event id; FA/NFA gates live inside each handler. + */ +#ifndef __SRIOV_SCHED_EVENT_HANDLERS_H__ +#define __SRIOV_SCHED_EVENT_HANDLERS_H__ + +#include "sriov_sched_event_dispatch.h" + +struct amdgpu_device; +struct sriov_sched_event; + +int sriov_sched_event_req_gpu_init(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa); +int sriov_sched_event_rel_gpu_init(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa); +int sriov_sched_event_req_gpu_fini(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa); +int sriov_sched_event_rel_gpu_fini(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa); +int sriov_sched_event_req_gpu_reset(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa); +int sriov_sched_event_req_gpu_init_data(struct amdgpu_device *adev, + struct sriov_sched_event *event, + bool in_fa); + +#endif diff --git a/drivers/gpu/drm/amd/sriov/sriov_sched_event_priv.h b/drivers/gpu/drm/amd/sriov/sriov_sched_event_priv.h new file mode 100644 index 000000000000..61b6cd782e0e --- /dev/null +++ b/drivers/gpu/drm/amd/sriov/sriov_sched_event_priv.h @@ -0,0 +1,78 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +/* + * Internal APIs shared between sriov_sched_event.c (framework) + * and sriov_sched_event_handlers.c (per-event bodies). + */ +#ifndef __SRIOV_SCHED_EVENT_PRIV_H__ +#define __SRIOV_SCHED_EVENT_PRIV_H__ + +#include "sriov_mailbox.h" +#include "sriov_sched_event.h" + +struct amdgpu_device; +struct sriov_sched; +struct sriov_sched_event; +struct sriov_sched_vf; + +#define is_unavail_vf(_vf) \ + ((_vf)->state == SRIOV_SCHED_VF_UNAVAILABLE) +#define is_avail_vf(_vf) \ + ((_vf)->state == SRIOV_SCHED_VF_AVAILABLE) +#define is_cond_avail_vf(_vf) \ + ((_vf)->state == SRIOV_SCHED_VF_COND_AVAIL) +#define is_active_vf(_vf) \ + ((_vf)->state == SRIOV_SCHED_VF_ACTIVE) +#define is_avail_or_active_vf(_vf) \ + (is_avail_vf(_vf) || is_active_vf(_vf)) +#define is_full_access_vf(_vf) \ + ((_vf)->in_full_access) + +void sriov_sched_push_back_event(struct sriov_sched *sched, + struct sriov_sched_event *event); + +bool sriov_sched_any_share_engine_vf_in_full_access( + struct sriov_sched *sched, unsigned int vf_idx); + +bool sriov_sched_is_event_pair(const struct sriov_sched_vf *vf, + enum sriov_sched_event_id id); +bool sriov_sched_any_ws_engine_shared(struct amdgpu_device *adev, + unsigned int vf_idx); + +int sriov_sched_enter_full_access(struct amdgpu_device *adev, + const struct sriov_sched_event *event); +int sriov_sched_exit_full_access(struct amdgpu_device *adev, + const struct sriov_sched_event *event); +void sriov_sched_exit_full_access_timeout(struct amdgpu_device *adev, + unsigned int vf_idx); + +int sriov_sched_handle_req_gpu_init_data(struct amdgpu_device *adev, + struct sriov_sched_event *event); +int sriov_sched_check_vf2pf_data(struct amdgpu_device *adev, + unsigned int vf_idx); +int sriov_sched_handle_orphan_vf(struct amdgpu_device *adev, + unsigned int vf_idx); + +#endif diff --git a/drivers/gpu/drm/amd/sriov/sriov_vfmgr.c b/drivers/gpu/drm/amd/sriov/sriov_vfmgr.c index a49683fe5725..6e0686e84aa5 100644 --- a/drivers/gpu/drm/amd/sriov/sriov_vfmgr.c +++ b/drivers/gpu/drm/amd/sriov/sriov_vfmgr.c @@ -879,6 +879,8 @@ int sriov_vfmgr_prepare_guest_fb(struct amdgpu_device *adev, if (r) return r; + sriov_sched_clear_init_data_ready(adev, vf_idx); + return 0; } @@ -903,6 +905,7 @@ static int sriov_vfmgr_init_vf_fb(struct amdgpu_device *adev, if (r) return r; + sriov_sched_clear_init_data_ready(adev, vf_idx); return 0; } -- 2.34.1