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 3B801606A32C for ; Fri, 4 Dec 2026 10:57:23 +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=udV/dn5+ Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C6EF7112EC7; Fri, 4 Dec 2026 10:57:17 +0000 (UTC) Received: from PH7PR06CU001.outbound.protection.outlook.com (mail-westus3azon11010052.outbound.protection.outlook.com [52.101.201.52]) by gabe.freedesktop.org (Postfix) with ESMTPS id 06BEE112EC4 for ; Fri, 4 Dec 2026 10:57:11 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=eiScux2zhOjKO0UGeU7qyQWEI0g68jRrQtfXeG+r0DcQENNzZrZnFTnM9I7wpyPAggZjihFA4lAvlamoyD8XszW0z5oEPFof7F9HWxdlr83L4HhKiFRnxnnTyMCrPVbHRqL/Ji+gA6jCOvHzCz2eIltIGPKn8gzc/59do5UJnGq/PRrbhq69IISTNE3TBtwWwmiH7SNYcogNeyZPmrkYnvUhb+5OpzEGIWpgwl3BAyhdzQtJ0heCjkHSM+Nd9ENyo5UNAPA5OW+cchFmlRngGRcQzBZGGBoOYlYd+EToiGCh32bTZecsJ11eW1GCriC5UrvbpjjIm8shlvUjplllbQ== 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=MIxIbBNC4oUsXa+d6HTqUjTCCjjQSSr/d05Ictt6WWw=; b=TJc2w7okC58TfV3j7acfZ/i14hjEEcTUKO1pSAaWZF0drihPYlqRK/6Ogf4aGyi4SZE/H3AS2LAHfiZGvMyhJiPQmV6k+3IEEAL1RE1ImxU0dv9CIKjawNfxIhae7iLVWV53Ntr9CEVTuSnafJzl7XSVjTYWw5jXs7XNIL6KrRQgi4t3kbwBCxfmnRuxvhgq2/DP5rUjFGmHEWHoYsEhlRAVKw9/I4d0IBCaSfEm4/pMB8iQeNM4njLhyYeDfa89qrzKPhLYI2xkDg0Wjbozjsn1INn6LaWl63vJ5DNKQMLibDzwfFOiiiOmsSFFtloGwV7PNEsvdHIp3HCTv6uINg== 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=MIxIbBNC4oUsXa+d6HTqUjTCCjjQSSr/d05Ictt6WWw=; b=udV/dn5+trKcywsFLcV+blmLmx6FezsyMLjPfRN/ERb7mChgizB9irm5qw1YDISEwbUARItLaK8UyyL/PXJjXhO0TMKmHzbK+IwD/5tCptGe3P40tH2JcatL1ZK+kHrde418MTsNVy/lBbChrKIJzzlH8PoQeJc2B4BfcjMeToE= Received: from PH8P223CA0005.NAMP223.PROD.OUTLOOK.COM (2603:10b6:510:2db::19) by IA1PR12MB8357.namprd12.prod.outlook.com (2603:10b6:208:3ff::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.11; Fri, 4 Dec 2026 10:57:05 +0000 Received: from SJ5PEPF000001EE.namprd05.prod.outlook.com (2603:10b6:510:2db:cafe::82) by PH8P223CA0005.outlook.office365.com (2603:10b6:510:2db::19) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.688.13 via Frontend Transport; Fri, 4 Dec 2026 10:57:05 +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 SJ5PEPF000001EE.mail.protection.outlook.com (10.167.242.202) 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:04 +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:04 -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:03 -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:02 -0600 From: Emily Deng To: , , Subject: [RFC PATCH v1 30/34] drm/amd/sriov: add PF scheduler documentation Date: Fri, 4 Dec 2026 18:57:01 +0800 Message-ID: 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: SJ5PEPF000001EE:EE_|IA1PR12MB8357:EE_ X-MS-Office365-Filtering-Correlation-Id: e2a34abd-c662-4088-82d5-08df51f55555 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|376014|1800799024|36860700016|23010399003|5023799004|11063799006|6133799003|261009223027099003|22082099003|18002099003|3023799007|261021121427099003|10067099003|56012099006|261102123227099003|261102130727099003|261022113327099003|261022075727099003|261021123027099003|261022115727099003|261022105027099003|261028144727099003|261026121527099003|261026140227099003; X-Microsoft-Antispam-Message-Info: 8PlyWOxLVP5A8kZkL0PVNAAHMRbLw+cUMVCONjprc1HQcQiHd8UHwdnoUhMD8U5/QBKYmsRoBayQ5UOi0NUADnXNPn/hOxp2IlZ3a7pyIVmhr28EMl5q6/v4Rj/cOcmiZw8MD54kRLDmN1Ig3z7XHEP9Q26h84+b5OnEuy9aw+wCoyBXwdKUg2UUzvd97KlU5DCM/43dEwI7UW/OIjIlZL/8/qH7ijwZmk4RhZqUc5zfwu3ot3ECI7FNgMm+cRB56LTXxfXwSeraklo7UgzPD/59w7YOpxgNitcQ5RmpEAa4mtPNLR2DlB9y81MonULhAXztNaZ062iVsENB5GHl1bIWsl8pLDlMvPg/O8GVSfQss0QbaI+b5890uvAQvELhQ7O+i/UC/9JSUoR6W2Q+QtmpdsXO9N+Upf8yd/QvTfjTVQybmZhmymVJ8DevaTCqXmPDexrU4oZZKWltpaSOaTGJDhfMOuZ7bqtZljWk1JM2nkQEVSWhAg0FaIGDRMDnPVFqqcoJFp8MTm4j5qlLRAkQp7t5hj0YE5mfh6Z/ZP4E5kqMB0BuutblOJqZ70LLoUWzAJgieAUD7ZIioDGWtdvIfUvHwSRord8KHH3qw1uSXs75lBWp1I470rhAsvruGWGpJF/++ABXtI1DBitt+nhCkVfUnO6Vv/iAljaRi2q5dokiFVvtj/mIAHf1TLC3hgTvCS7q1/Mx75lMeTG31w== 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)(82310400026)(376014)(1800799024)(36860700016)(23010399003)(5023799004)(11063799006)(6133799003)(261009223027099003)(22082099003)(18002099003)(3023799007)(261021121427099003)(10067099003)(56012099006)(261102123227099003)(261102130727099003)(261022113327099003)(261022075727099003)(261021123027099003)(261022115727099003)(261022105027099003)(261028144727099003)(261026121527099003)(261026140227099003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RNxfIw64F5MMNRqXMbqEBfD6qxg/TqhWCtBfbX0J5xESJ/BwHcB5AnHJE/7sht2X7zCtuUm566jx3/qQZ3Pcyt2KVeZnoTmF7/m6YKiFDkGuAa1fXxl+4vx85RVmQy0ka+6qj71eZ4n71/umD4wOMdZWW20ZZfPOQxTsJMx2N/3dN8kiUr+pl8ZXC3wxMB31GMf7cWEcVINB+uZeWOfwZ6D9nBlfZjeoMP9ezyKSx3WPJCVUJMMN7HBxDf0evrRWCYor68cYZpx16fx5sc63M3/C3al6TneCOKxjwFft8hBmkHcy2rfjXBBJt0WRMEXxWYazhXOy55dOjFRnIvDE4jeedra52j8i4NlnZHPvpxFSl4gD+T8bNgdHhuQmREmHEhWafnJo6g+hup/BQIJAnC7bJHdM2w2p49ekei/FVvzjd2K8m4JS3C+g5iE6Ycot X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 10:57:04.9145 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e2a34abd-c662-4088-82d5-08df51f55555 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: SJ5PEPF000001EE.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8357 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 kernel-doc overview of the PF-side scheduler stack: event queue, full-access arbitration, manual topology/world switch and VF guard limits. Signed-off-by: Emily Deng --- Documentation/gpu/amdgpu/index.rst | 1 + Documentation/gpu/amdgpu/sriov-scheduler.rst | 737 +++++++++++++++++++ 2 files changed, 738 insertions(+) create mode 100644 Documentation/gpu/amdgpu/sriov-scheduler.rst diff --git a/Documentation/gpu/amdgpu/index.rst b/Documentation/gpu/amdgpu/index.rst index ba2ee7327867..991275bdcbb4 100644 --- a/Documentation/gpu/amdgpu/index.rst +++ b/Documentation/gpu/amdgpu/index.rst @@ -22,6 +22,7 @@ Next (GCN), Radeon DNA (RDNA), and Compute DNA (CDNA) architectures. debugging debugfs process-isolation + sriov-scheduler amdgpu-glossary ualink ptl diff --git a/Documentation/gpu/amdgpu/sriov-scheduler.rst b/Documentation/gpu/amdgpu/sriov-scheduler.rst new file mode 100644 index 000000000000..e1deb7597ffb --- /dev/null +++ b/Documentation/gpu/amdgpu/sriov-scheduler.rst @@ -0,0 +1,737 @@ +.. SPDX-License-Identifier: MIT +.. _amdgpu-sriov-scheduler: + +====================================== +SR-IOV PF scheduler and event handling +====================================== + +When AMDGPU runs as an SR-IOV PF host, the PF driver mediates VF lifecycle +and hardware access gates. Each enabled VF is an independent PCI function; +guests request init, fini, reset, and init-data through the PF/VF mailbox, +and the software scheduler serializes those lifecycle windows. Once a VF is +guest-visible, it may enqueue GPU work through doorbells and its framebuffer +mapping; GPUIOV scheduling hardware and the ``ws[]`` world-switch policy then +time-slice which VF context is loaded and running on each hardware scheduler +block through the manual timer policy in this RFC baseline—so submission and +on-hardware execution are separate. Broad +MMIO programming and exclusive configuration remain PF-mediated. + +This page is the theory-of-operation overview for the PF-side scheduler stack +in ``drivers/gpu/drm/amd/sriov/``. It covers the event queue and handlers, +scheduler topology, manual world switch, VF guard limits, +and how they relate to guest boot tables built earlier during PF initialization (VFMGR, +IP discovery, guest VBIOS image in VF FB). + +Build integration +================= + +SR-IOV PF host code is part of the standard ``amdgpu`` build. ``sriov/Makefile`` +defines ``AMD_SRIOV_FILES``; ``amdgpu/Makefile`` includes it and links those +objects whenever ``CONFIG_DRM_AMDGPU=y``. PF host mode is selected at boot by +the ``amdgpu.sriov_numvfs`` module parameter; when unset or zero, the SR-IOV +host paths remain dormant. + +Overview +======== + +A portion of VRAM is reserved for VF framebuffer use. The PF maps each VF's +guest framebuffer addresses (GPA) to physical VRAM (SPA) through FFBM page +tables. Spatial partitioning and the scheduler topology bind VFs to +world-switch objects for time-sliced context rotation (manual timer policy). + +At a high level, a lifecycle request flows through four layers: + +1. Mailbox — the guest sends a VF→PF mailbox message (for example + ``REQ_GPU_INIT``). The PF mailbox service decodes the message and calls + into the scheduler to queue a matching software event. +2. Event queue — new events append to an incoming ``event_queue`` (depth limited + to 256). The worker drains that queue through + ``sriov_sched_event_dispatch.c``: each event id maps to a handler in + ``sriov_sched_event_handlers.c`` and one of six fixed priority bands + (``event_list[0..5]``; lower index drains first). Lifecycle release ids + (``REL_*``) sit in a higher-priority band than request ids (``REQ_*``), so + release still runs before request when both are pending. One worker handles + one event at a time so conflicting VF operations never run in parallel. +3. Full-access window — request events (``REQ_*``) open a full-access period + for one VF: ``sriov_sched_stop()`` pauses world switch on that VF's + ``ws_mask``, the PF opens VFMGR ALL access, enters RLC safe mode, runs the + handler, then enables the RLCG VF interface for guest programming. While + any VF holds full-access, all other lifecycle requests and init-data events + are re-queued; only matching release events for the owner VF run. Matching + release events (``REL_*``) exit RLC safe mode, close the RLCG VF interface, + and restore time-sliced scheduling. +4. World switch / topology — outside full-access, the scheduler topology + maps VFs to spatial partitions and hardware scheduler blocks. The bound + manual ``ws[]`` policy saves and loads per-VF contexts as the PF timer + rotates time slices between VFs. + +Guard logic limits queueing: ``ALL_INT`` is counted at mailbox decode; +``EXCLUSIVE_MOD`` and ``EXCLUSIVE_TIMEOUT`` checks run in +``sriov_sched_sanitize_queue_event()`` for lifecycle requests. +Repeated activity can return ``SRIOV_GUARD_EVENT_OVERFLOW_ERR`` without +changing the VF runtime ``state`` field. + +Software stack (top to bottom) +============================== + +The PF scheduling path is layered. Higher levels decide **when** and **for +which VF** work runs; lower levels issue GPUIOV commands on hardware scheduler +blocks. The diagram below is the **source-file** view; **Fig. 1** and **Fig. 2** +in `Topology and world switch`_ show how static tables become runtime objects +and how a VF operation reaches hardware. + +:: + + +-------------------------------------------------------------+ + | Guest VF | + | mailbox REQ/REL, doorbells, interrupts | + +---------------------------+---------------------------------+ + | + v + +-------------------------------------------------------------+ + | sriov_mailbox.c | + | decode VF->PF messages, guard ALL_INT, queue sched events | + +---------------------------+---------------------------------+ + | + v + +-------------------------------------------------------------+ + | sriov_sched.c + sriov_sched_event*.c | + | event_queue / worker, full-access, dispatch + handlers | + +---------------------------+---------------------------------+ + | + full-access | time-sliced (NFA) + stop ws in | manual ws policy + vf->ws_mask | + v + +-------------------------------------------------------------+ + | sriov_sched_switch.c — sched->ws[] ws_save / ws_load | + +---------------------------+---------------------------------+ + | + v + +-------------------------------------------------------------+ + | sriov_sched_ws_manual.c — delayed_work, round-robin | + +---------------------------+---------------------------------+ + | + v + +-------------------------------------------------------------+ + | sriov_hwsm.c — per hw_sched_id IDLE/SAVE/SHUTDOWN/LOAD/RUN | + +---------------------------+---------------------------------+ + | + v + +-------------------------------------------------------------+ + | sriov_gpuiov.c + PSP IOVM -> GPUIOV hardware (GFX_SCH0_RLCV) | + +-------------------------------------------------------------+ + +Main data structures +==================== + +The scheduler state lives in ``struct sriov_sched`` (see +``drivers/gpu/drm/amd/sriov/sriov_sched_event.h``), attached to +``adev->sriov->sched``. + +Per-VF runtime state is ``struct sriov_sched_vf``: + +state + Coarse availability (unavailable, available, conditionally available, + active). ``sriov_sched_hw_init()`` sets **available** when VFMGR hardware + init has completed; active is set on ``REL_GPU_INIT``. + +in_full_access / full_access_event + Whether this VF currently owns the full-access window and which request + opened it. + +ws_mask / hw_sched_mask / block_map / time_slice + Topology and time-slice parameters. ``ws_mask`` is the bitmask of + ``sched->ws[]`` objects a VF participates in; populated by + ``sriov_sched_vf_map_add()`` during ``sriov_sched_ws_remap_vf_assignment()``. + +init_data_ready + Guest boot tables in VF framebuffer are valid. + +guard + Per-VF guard counters (``struct sriov_guard`` embedded in + ``struct sriov_sched_vf``) described in `VF guard limits`_. + +Each logical world switch is ``struct sriov_sched_ws`` in +``sched->ws[]``: + +switch_running / active_vfs / allowed_vf_assignment + Whether manual rotation is armed and which VFs participate on this + ``sched_block`` (``delayed_work`` timer). + +curr_idx_vf / curr_vf_state + Coarse ws-level GPUIOV state for the VF currently loaded. + +hw_sched_mask + Bit mask of ``gpuiov->ctrl_blocks[]`` driven together on each step. + +Each hardware block has ``sched->hw_state_machine[hw_sched_id]`` with +``cur_gpu_state``, ``cur_vf_id``, ``goto_state``, and a mutex. + +Software events are ``struct sriov_sched_event`` entries. New events append +only to ``sched->event_queue``; the worker moves them into +``sched->event_list[priority]`` bands (0..5) using the static descriptor table +before handling. Each event records the VF index, event id, optional GPUIOV +scheduler block, and optional wait completion for synchronous callers. + +Implementation files +-------------------- + +sriov_sched.c + Scheduler IP block init/fini, VF state reset, ties together the event queue + and world switch. + +sriov_sched_event.c + Event queue, worker, full-access logic, VF request helpers, and mailbox + response helpers. + +sriov_sched_event_dispatch.c + Static event descriptor table (priority band, dedup flag, handler pointer). + +sriov_sched_event_handlers.c + Per-event handler bodies (init, fini, reset, init-data). + +sriov_sched_event.h + Shared structures, queue API, and statistics. + +sriov_sched_switch.c + ``sched->ws[]`` allocation, spatial-partition copy, ``ws_save`` / + ``ws_load``, context helpers exported to handlers. + +sriov_sched_ws_manual.c + Manual (timer) world-switch policy. + +sriov_hwsm.c / sriov_hwsm.h + Per-``hw_sched_id`` manual GPUIOV command FSM (``sriov_goto_state_manual``). + +sriov_sched_switch.h + Context-switch helpers exported to the event layer. + +sriov_guard.c / sriov_guard.h + Guard thresholds and active-event accounting. + +sriov_mailbox.c + VF→PF decode and scheduler event queueing. + +Event identifiers +================= + +Event ids are defined in ``enum sriov_sched_event_id`` in +``sriov_mailbox.h``. + +Lifecycle request / release pairs +--------------------------------- + +* ``SRIOV_EVENT_REQ_GPU_INIT`` / ``SRIOV_EVENT_REL_GPU_INIT`` +* ``SRIOV_EVENT_REQ_GPU_FINI`` / ``SRIOV_EVENT_REL_GPU_FINI`` +* ``SRIOV_EVENT_REQ_GPU_RESET`` (paired with ``SRIOV_EVENT_REL_GPU_INIT`` on exit) +* ``SRIOV_EVENT_REQ_GPU_INIT_DATA`` + +Request events are placed in lower-priority bands than their matching release +events (for example ``REQ_GPU_INIT`` in band 4, ``REL_GPU_INIT`` in band 2). +``sriov_sched_pick_next_event()`` scans bands from 0 upward and always +services the first pending entry in the earliest non-empty band. + +Event queue processing +====================== + +Initialization +-------------- + +``sriov_sched_event_queue_init()`` prepares empty lists and +``timeout_work`` during scheduler **software** init +(``sriov_sched_sw_init()``). In the same ``sw_init`` path, +``sriov_sched_event_queue_process_init()`` allocates the ordered workqueue +and clears queue statistics. Scheduler **hardware** init builds spatial +partitions and ``ws[]`` objects; it does not start the event worker. + +After scheduler **late** init, ``sriov_sched_event_queue_process_start()`` +sets ``event_enabled`` and schedules the worker if events are already +pending. ``sriov_sched_event_queue_process_stop()`` clears ``event_enabled`` +and cancels timeout work without tearing down the queue. That path is used +during scheduler late fini teardown. + +Queueing +-------- + +``sriov_sched_queue_event*()`` validates the event via +``sriov_sched_sanitize_queue_event()``: + +* rejects unknown ids and events for out-of-range VF indexes; +* consults guard state for ``REQ_GPU_INIT``, ``REQ_GPU_FINI``, and + ``REQ_GPU_RESET``; +* does not enforce coarse VF state; handlers apply prerequisites (for + example ``REQ_GPU_FINI`` is ignored unless the VF is **active**). + +Accepted events are appended to ``sched->event_queue`` under ``sched->lock``. +If processing is enabled, ``sriov_sched_work()`` is queued. + +Worker +------ + +``sriov_sched_process_event()`` moves pending entries out of +``event_queue`` into priority bands, picks the next event (lowest non-empty +band first), and calls the handler from the dispatch table. Handlers return +one of: + +``SRIOV_SCHED_EVENT_CONTINUE`` + Handler finished; free the event and continue the worker loop. + +``SRIOV_SCHED_EVENT_STOP_AND_KEEP`` + Re-queue the event on the same priority band and stop the worker for now + (for example while another VF holds full-access). + +``SRIOV_SCHED_EVENT_STOP_AND_RELEASE`` + Full-access request accepted; free the event but leave the full-access + window open until the matching release message. + +Duplicate events for the same VF in the same priority band are removed when +a new one is sorted in (when the descriptor sets ``remove_duplicate``). + +Full-access arbitration +======================= + +Only one VF may hold full-access at a time in this RFC baseline. The helper +``sriov_sched_any_share_engine_vf_in_full_access()`` in +``sriov_sched_event.c`` implements this rule by testing +``logical_sched_fa_mask`` against the VF's ``ws_mask``. + +Typical REQ_GPU_INIT sequence +----------------------------- + +1. Mailbox receives guest init request and queues ``REQ_GPU_INIT``. +2. Worker enters ``sriov_sched_enter_full_access()`` for that VF, + calls ``sriov_sched_stop()`` on every ``sched->ws[]`` entry in the VF's + ``ws_mask``, enables PSP mailbox interrupt routing, opens VFMGR ALL access, + and enters RLC safe mode. For ``REQ_GPU_INIT`` or ``REQ_GPU_RESET`` only, + if the VF was already active, ``sriov_sched_handle_orphan_vf()`` removes + it from rotation first. +3. Handler loads guest boot data if needed, runs + ``sriov_sched_context_init()``, updates PF→VF mailbox content, enables the + RLCG VF interface, and sends ``READY_TO_ACCESS``. The VF stays non-active + until release; the worker returns ``SRIOV_SCHED_EVENT_STOP_AND_RELEASE`` + and keeps the full-access window open. +4. Guest completes GPU init inside full-access, then sends ``REL_GPU_INIT``. +5. Worker exits RLC safe mode, saves context unless the single-VF path + skips that save, sets the VF **active**, disables the RLCG VF interface, + calls ``sriov_sched_add_vf()``, and restarts world switch on the + affected ``ws[]`` objects. + +``REQ_GPU_FINI`` and ``REQ_GPU_RESET`` follow the same full-access pattern with +different handler bodies. A delayed timeout work item (3 seconds when +``num_vf_per_gfx_sched`` is 1, otherwise 1.5 seconds; +``sriov_sched_full_access_timeout_ms()``) aborts stuck full-access +sessions from each VF's ``full_access_start`` timestamp and increments +``full_access_timeout_cnt``. + +Topology and world switch +========================= + +In SR-IOV, each VF is given **dedicated** guest-visible resources — its own +doorbell set, MMIO BAR views, and framebuffer (FB) in reserved VRAM — so +normal submission and memory access stay partitioned at the PCI/function +level. Execution still flows through **shared** GPUIOV hardware scheduler +blocks on the same die (for example ``GFX_SCH0_RLCV`` on GFX, MMSCH on +VCN/JPEG): the silicon exposes a fixed number of these blocks, not one +copy per VF. + +A **world switch** is the PF-side operation that changes **which VF's +scheduling context** is loaded on such a shared block — saved GPU state, +register set, and the binding between that block and the VF whose work +should run next — so the common hardware executes the intended guest even +though each VF keeps its own doorbells, MMIO, and FB. + +World switch is needed because those shared blocks can hold only one VF +context at a time. When more than one VF is **ACTIVE** on the same block, +the PF **time-slices** (SAVE the outgoing VF, LOAD the incoming VF, RUN) +through the manual timer in ``sriov_sched_ws_manual.c``. Lifecycle +**full-access** is different: while the PF programs init/fini/reset for one +VF, world switch on that VF's ``ws_mask`` is **stopped** until the matching +release — the guest still uses its dedicated MMIO path inside the window, +but the shared block is not rotated away. + +The rest of this section describes how static ASIC tables become runtime +``ws[]`` objects and how each VF operation reaches RLCV or MMSCH. The stack +is easier to follow as two phases: **build-time tables** that describe the +GPU, and a **runtime path** that walks a VF's hardware list when init, fini, +or world switch runs. + +Build-time topology (Fig. 1) +---------------------------- + +**Step 1 — Static ctrl-block table.** Each ASIC exports a compile-time table +through ``adev->sriov->gfx_funcs->get_gpuiov_ctrl_blocks()`` (for example +``sriov_gfx_v13_0_gpuiov_blocks[]``). Each row is one GPUIOV scheduling +instance the PF may drive — for example ``GFX_SCH0_RLCV`` (RLCV on GFX) or +``VCN_SCH0_MMSCH`` (MMSCH on VCN/JPEG). The row records the block **name**, +``hw_sched_type`` (GFX or MM), ``sched_block`` (GFX / VCN / JPEG / …), +default ``sched_mode``, and ``hw_inst`` (which XCC or VCN instance). + +**Step 2 — Harvest filter → ``ctrl_blocks[]``.** ``sriov_gpuiov_hw_init()`` +calls ``sriov_gpuiov_ctrl_block_setup()``. For each static-table row, +``sriov_gpuiov_skip_block()`` drops blocks that harvest disables (GFX rows +whose ``hw_inst`` is absent from ``adev->gfx.xcc_mask``, VCN/JPEG rows whose +instance is not present, and similar ASIC rules). Surviving rows are copied +into runtime ``gpuiov->ctrl_blocks[hw_sched_id]`` with ``enabled = true``. +These entries are the handles used for all later GPUIOV commands on that +hardware block. + +**Step 3 — Static spatial-partition table.** For a **fixed VF count** layout, +the ASIC also supplies a ``static_spatial_part_table`` (for example +``sriov_gfx_v13_0_block_configs_1x1[]``). Each row is one spatial partition: + +* ``idx_vf_mask`` — which VF function ids (plus PF) belong to this partition; +* ``hw_sched_mask`` — which ``ctrl_blocks[]`` indices those VFs may use. + +``sriov_sched_copy_static_spatial_part_table()`` copies this into runtime +``sched->spatial_parts[]``. + +**Step 4 — ``ws[]`` objects and VF mapping.** ``sriov_sched_ws_init()`` merges +ctrl blocks that share the same ``sched_block`` into one logical world-switch +object ``sched->ws[]`` (OR-ing ``hw_sched_mask``). It then maps each VF in a +partition onto the ``ws[]`` entries and ``ctrl_blocks[]`` bits implied by that +partition's ``hw_sched_mask``, filling ``vfs[vf].ws_mask``, +``vfs[vf].hw_sched_mask``, and ``vfs[vf].block_map[]``. + +**Step 5 — Manual policy.** After ``ws[]`` exists, init walks each ``ws`` +object's ``hw_sched_mask`` and binds ``sriov_sched_ws_manual_funcs`` with +HWSM mode **MANUAL** (PF ``delayed_work`` drives rotation). + +Baseline gfx v13 wires one GFX ctrl block and one spatial partition; the same +machinery scales when the static tables list more blocks. + +:: + + [ASIC static ctrl-block table] + GFX_SCH0_RLCV, VCN_SCH0_MMSCH, ... (name, sched_block, sched_mode, hw_inst) + | + | sriov_gpuiov_ctrl_block_setup() + | sriov_gpuiov_skip_block() (harvest / instance filter) + v + gpuiov->ctrl_blocks[hw_sched_id] <-- runtime HW handles + + [ASIC static spatial-partition table] (fixed num_vf layout) + idx_vf_mask + hw_sched_mask per partition + | + | sriov_sched_copy_static_spatial_part_table() + | -> gfx provider -> sriov_sched_apply_static_spatial_part_table() + v + sched->spatial_parts[] + | + | sriov_sched_ws_init() (merge by sched_block) + v + sched->ws[] + vfs[vf].hw_sched_mask / ws_mask + | + | sriov_sched_ws_manual_funcs (RFC baseline) + v + PF delayed_work timer rotation + +**Fig. 1** Build-time tables to runtime objects. + +Runtime VF operation (Fig. 2) +----------------------------- + +Once topology is built, **hw_init** only adds or removes VFs from rotation; +it does not rebuild the tables. Operating on a VF always reduces to: *find +which hardware blocks this VF owns, then drive each block through HWSM to +program the hardware.* + +**Entry points** + +* **Lifecycle** (``REQ_GPU_INIT``, fini, reset) — the event worker stops the + affected ``ws_mask``, runs handlers such as ``sriov_sched_context_init()``, + then resumes or leaves full-access open. +* **Time-sliced world switch** — ``sriov_sched_ws_manual_work()`` picks the + next ACTIVE VF on a ``ws`` object and calls ``sriov_sched_ws_save()`` / + ``sriov_sched_ws_load()``. + +**Per-VF hardware walk** + +1. Resolve the target ``vf_idx`` and its ``vfs[vf].ws_mask`` / + ``vfs[vf].hw_sched_mask`` (from the spatial-partition map in Fig. 1). +2. For each ``ws`` in ``ws_mask``, run the manual ``delayed_work`` rotation + policy bound at init. +3. For each ``hw_sched_id`` set in that ``ws->hw_sched_mask``, call into + ``sched->hw_state_machine[hw_sched_id]``: + + * **MANUAL** — ``sriov_hwsm_run()`` → ``sriov_goto_state_manual()`` steps + through GPUIOV commands (IDLE → SAVE → SHUTDOWN → LOAD/INIT → RUN). + +4. ``sriov_gpuiov.c`` submits each command through PSP IOVM or MMIO to the + underlying block — RLCV for GFX ctrl blocks. + +Following **Fig. 2**, a manual GFX time slice on VF1: the Layer-2 timer fires +on the GFX ``ws`` object, ``sriov_sched_ws_load()`` loops every +``hw_sched_id`` in ``ws->hw_sched_mask``, and HWSM issues LOAD/RUN GPUIOV +commands to each RLCV instance. A ``REQ_GPU_INIT`` on VF1 instead stops that +``ws_mask``, walks the same hardware list through INIT/RUN inside the +full-access handler, and only re-enters rotation after ``REL_GPU_INIT``. + +:: + + mailbox event or ws timer + | + v + pick vf_idx (lifecycle handler or ws rotation) + | + v + vfs[vf].ws_mask / hw_sched_mask (from spatial-part table) + | + v + +------------------+ + | sched->ws[] | manual: delayed_work, round-robin VF + | (per sched_block)| + +--------+---------+ + | + | for each hw_sched_id in ws->hw_sched_mask + v + +------------------+ + | hw_state_machine | + | [hw_sched_id] | MANUAL: IDLE/SAVE/LOAD/RUN cmd sequence + +--------+---------+ + | + v + gpuiov->ctrl_blocks[hw_sched_id] + | + v + GPUIOV command ---> RLCV (GFX) + +**Fig. 2** Runtime path from event/timer to programmed hardware. + +Init sequence (reference) +------------------------- + +Services register on the SR-IOV ``init_func`` chain in this order: GPUIOV, +mailbox, FFBM, VFMGR, scheduler. Each service runs ``sw_init`` in that +order, then ``hw_init`` in the same order. Scheduler ``late_init`` / ``late_fini`` +run after all services complete hardware init / before hardware fini. + +Scheduler-specific hooks: + +``sriov_sched_sw_init()`` + Event queue, workqueue, and guard init for ``sched->vfs[]``. + +``sriov_gpuiov_hw_init()`` (runs earlier on the chain) + ``sriov_gpuiov_ctrl_block_setup()`` — static table → ``ctrl_blocks[]`` + after harvest filter. + +``sriov_sched_hw_init()`` + ``sriov_sched_copy_static_spatial_part_table()`` → ``spatial_parts[]``, + then ``sriov_sched_ws_init()`` (build ``ws[]``, map ``sched->vfs[]``, + bind ``sriov_sched_ws_manual_funcs``). + +``sriov_sched_late_init()`` + Save PF context and ``sriov_sched_event_queue_process_start()`` (arm the + event worker). + +Runtime membership only: + +``sriov_sched_add_vf()`` / ``sriov_sched_remove_vf()`` + Connect or disconnect a VF from ``ws[]`` rotation after ``REL_GPU_INIT`` / + fini. + +``sriov_sched_start_all()`` + Starts world switches (non-GFX ``ws[]`` first, then GFX). + +Default GFX ``time_slice`` for the 1VF baseline is ``U32_MAX`` when +``SRIOV_FLAG_DISABLE_SELF_SWITCH`` is set (``sriov_sched_get_gfx_time_slice()`` +during ``sriov_sched_hw_init()``); multi-VF layouts use 6000 µs. + +Implementation status (baseline) +------------------------------------------- + +The data structures and merge logic support multiple ctrl blocks and +``sched_block`` types. The current gfx v13 baseline wires only a minimal subset; +full tables are added per ASIC through ``get_gpuiov_ctrl_blocks()``. + ++---------------------------+-----------------------------------------------+ +| Area | Baseline today | ++===========================+===============================================+ +| Static ctrl blocks | One entry: ``GFX_SCH0_RLCV`` (XCC 0 only) | ++---------------------------+-----------------------------------------------+ +| ``hw_sched_type`` enum | ``SRIOV_GPUIOV_SCHED_GFX`` only | ++---------------------------+-----------------------------------------------+ +| World-switch vtable | GFX manual switch only; MM types return NULL | ++---------------------------+-----------------------------------------------+ +| ``sched->ws[]`` | Typically one GFX object at runtime | ++---------------------------+-----------------------------------------------+ +| Spatial partitions | ``num_spatial_partitions = 1`` | ++---------------------------+-----------------------------------------------+ +| Partition hw_sched_mask | GFX blocks only in spatial-part copy | ++---------------------------+-----------------------------------------------+ +| Multi-XCC GFX blocks | ``GFX_SCH0`` only; ``xcc_mask`` hooks present | ++---------------------------+-----------------------------------------------+ +| Event ``sched_block`` | Mostly ``BLOCK_ALL`` (GFX-only equivalent) | ++---------------------------+-----------------------------------------------+ +| Per-block ``time_slice`` | 1VF baseline: ``U32_MAX`` (self-switch off); | +| | multi-VF: GFX 6000 µs | ++---------------------------+-----------------------------------------------+ + +Multi-XCC framework (extension hooks) +------------------------------------- + +Some ASICs expose multiple **XCCs** on one GPU. Each enabled XCC typically has +its own GPUIOV GFX scheduler block (``GFX_SCHx_RLCV`` with ``hw_inst = x``). + +Multi-XCC is independent of multi-VF: IP discovery requires +``hweight32(xcc_mask) % num_vfs == 0``. + +**Shape on a multi-XCC ASIC (example):** one GFX ``ws`` object still +handles VF fairness; its ``hw_sched_mask`` ORs all enabled ``GFX_SCHx_RLCV`` +blocks. Each timer iteration calls ``sriov_sched_ws_load()`` (or ``ws_save`` +before it), which loops every ``hw_sched_id`` in that mask and drives +``sriov_hwsm_run()`` → ``sriov_goto_state_manual()`` on that XCC before moving +to the next VF. VCN/JPEG remain separate +``sched_block`` values with their own world-switch objects. + +**Baseline today:** only ``GFX_SCH0_RLCV`` is registered. The multi-XCC +hooks are: + +``adev->gfx.xcc_mask`` + Harvest mask; stored in ``spatial_parts[].xcc_mask``. + +``sriov_gpuiov_skip_block()`` + Omits **GFX** static-table entries whose ``hw_inst`` is not enabled. + +``sriov_gfx_v13_0_sched_copy_static_spatial_part_table()`` (via + ``adev->sriov->gfx_funcs->sched_copy_static_spatial_part_table``) + Builds ``part->hw_sched_mask`` from **GFX** ctrl blocks matching + ``xcc_mask`` (MM blocks are not included yet). + +``sriov_sched_ws_save()`` / ``sriov_sched_ws_load()`` + One rotation step: for each ctrl block in ``ws->hw_sched_mask``, call + ``sriov_hwsm_pause()`` / ``sriov_hwsm_run()`` which invoke the per-block + GPUIOV FSM. + +``sriov_gpuiov_ctrl_block_xcc_mask()`` + Maps ``hw_inst`` to IOVM ``xcc_mask`` for PSP-mediated GPUIOV commands. + +``sriov_gmc_v13_0_gpuiov_set_vf_fb()`` + Programs VF FB registers on each GC instance in ``xcc_mask``. + +``sriov_ip_discovery_trim_ip_list()`` + Validates ``hweight32(xcc_mask) % num_vfs == 0`` and trims guest IP tables. + +Time-sliced world switch +------------------------ + +Time-sliced world switch loop +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +After ``REL_GPU_INIT``, ``sriov_sched_add_vf()`` then +``sriov_sched_start()`` attach the VF to each active ``ws`` in ``vfs[vf].ws_mask`` +(``add_vf`` sets ``active_vfs`` and may queue ``delayed_work`` for manual ws; +``start()`` arms ``switch_running``). Each manual timer iteration selects the +next ACTIVE VF, runs ``sriov_sched_ws_load()`` (GPUIOV RUN on every ctrl block +in ``hw_sched_mask``), and requeues after the VF's ``time_slice`` when more +than one VF is active on the ``ws`` object. +``sriov_sched_stop()`` (full-access or teardown) calls ``sriov_sched_ws_save()`` +and cancels manual rotation. With one ACTIVE VF the manual loop stops by default +(``SRIOV_FLAG_DISABLE_SELF_SWITCH`` at probe). + +**Manual** ws objects use the policy in ``sriov_sched_ws_manual.c``: + +:: + + sriov_sched_ws_manual_work() + -> next_vf = round-robin (active_vfs & allowed_vf_assignment) + -> sriov_sched_ws_load(ws, next_vf) + for each hw_sched_id in ws->hw_sched_mask: + sriov_hwsm_run() -> sriov_goto_state_manual(..., RUN_GPU) + -> requeue after time_slice if multiple ACTIVE VFs + +``sriov_goto_state_manual()`` in ``sriov_hwsm.c`` steps each hw_sched block +through IDLE → SAVE → SHUTDOWN → LOAD/INIT → RUN until the target VF is loaded. + +``struct sriov_sched_ws_funcs`` (manual vtable in this RFC) provides +``init`` / ``fini``, ``start`` / ``stop``, ``add_vf`` / ``remove_vf``, and +``update_time_slice`` to manage each ``ws`` object. + +Context transfer and teardown are separate helpers exported from +``sriov_sched_switch.h`` and called directly by event handlers: + +``sriov_sched_context_init()`` / ``sriov_sched_context_load()`` / ``sriov_sched_context_save()`` + Prepare or transfer hardware scheduling contexts for a VF on a scheduler + block. + +``sriov_sched_context_switch_to_vf_saved()`` + Load a previously saved VF context during world switch. + +``sriov_sched_shutdown_vf()`` / ``sriov_sched_reset()`` + Tear down or rebuild switch state after FINI or reset. + +Event handlers call these helpers during init, fini, and reset paths. Outside +full-access, ``sriov_sched_start()`` starts the time-sliced world +switch loop. + +VF guard limits +=============== + +Each ``struct sriov_sched_vf`` embeds a ``struct sriov_guard`` that tracks +rolling counts of disruptive events (exclusive-mod, exclusive-timeout, and +excessive interrupts in this RFC). ``sriov_guard_add_active_event()`` +increments the relevant counter inside a configurable time window when mailbox +decode or lifecycle queueing runs. + +Default thresholds (from ``sriov_guard.h``): + +.. list-table:: Default guard thresholds + :header-rows: 1 + :widths: 35 25 20 + + * - Guard event + - Window (default) + - Max count + * - EXCLUSIVE_MOD + - 60 s + - 9 + * - EXCLUSIVE_TIMEOUT + - 500 s + - 2 + * - ALL_INT + - 60 s + - 56 + +If a threshold is exceeded, queueing returns +``SRIOV_GUARD_EVENT_OVERFLOW_ERR`` and the mailbox layer can fail the +guest request. Guard state is reset through ``sriov_guard_vf_init()``, +``sriov_guard_vf_remove()``, or ``sriov_guard_vf_reset()`` (for example +during scheduler ``sw_init`` / ``sw_fini`` or ``sriov_sched_init_vf_state()``). + +This prevents a misbehaving or stuck guest from flooding the scheduler +priority bands with reset or init loops. + +Interaction with other SR-IOV blocks +==================================== + +Mailbox + Produces scheduler events from VF messages and sends PF→VF responses such + as ``READY_TO_ACCESS``. + +VFMGR + Programs VF framebuffer boot tables and opens or closes FB, doorbell, and + ALL access during full-access. + +GPUIOV + Provides spatial-partition and hardware-scheduler tables consumed by + topology setup; receives GPUIOV commands that drive RLCV or MMSCH world + switch. + +PSP / NBIF + Mailbox interrupt routing, RLC safe mode, and RLCG VF interface during + full-access. + +Suggested reading order +======================= + +1. ``enum sriov_sched_event_id`` and mailbox decode paths in + ``sriov_mailbox.c``. +2. Queue and worker in ``sriov_sched_event.c`` + (``sriov_sched_queue_event_ex``, ``sriov_sched_work``, and + ``sriov_sched_enter_full_access``). +3. Topology setup in ``sriov_sched_switch.c`` + (``sriov_sched_ws_init``, ``sriov_sched_ws_remap_vf_assignment``, spatial-partition copy). +4. Manual world-switch policy in ``sriov_sched_ws_manual.c`` and GPUIOV FSM in + ``sriov_hwsm.c`` (``sriov_sched_ws_load`` / ``sriov_goto_state_manual``). +5. Manual switch actions (``context_*`` and ``sriov_sched_reset``). +6. Guard checks in ``sriov_guard.c`` before queueing. -- 2.34.1