From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796398769; b=Vp20axwya1UzVJTX8Jlmw8PzWpIMu0gbEHw8mVgx/sNz0mjdswhZMsuCGbznNkkw04Is 4JXopHGSAXrfAqZO0x4Ql91RLIc5qSiEx3SIyCPh1fWxyIZPSidh1fZ3eq2Z4t3PjZLIL qmguA75USDHrO1ccpGNvALZW/DCKUXPVUxcXSo3cCdIH8LlzMfXnaRbJbT3AsldGYwQLW C4HxpKITwTux2CzxGbWlOlhbmOxN7dkMgu6VqxONSRh5OvFF0vmrZna4ywVvhwG5bKoKo 0pYbqJDXejxF/6MmXGccWWbGr7WKRxBmhFu4RDp85QYiEE/SwHfVN0rivCNRvRrwGUg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796398769; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: MIME-Version:Content-Type:Content-Disposition; bh=O76S0SADxDrvkvRX+Y/m6LeMy6t0JHcBbaPXKTQO7dc=; b=hJNsr7vRXUNgbsbhDylWCJFhBbFOh4b66vKv/kruvMFF+jY9L7nNpZXFj6tPtwmbAJfm 2juYcWhFFof37Layx1pa4cTZjVIbZZ6KqmlVvOsUzrpYyabSCGF8ZvsBU8PdzwyTblRoC 4GYoWaLDgad8vtDY49YUD0CQBXnA9cIPiZfSFxKaSvInDxmHdhlxcwsZJYYCo6fo9slCP rT4DVSZ/2odzQj1F2m7HFc93bAT6a5iAhtn6hlCUT5ARbAqG7TsYiDeYI7N3AYDME3XEl rgBplpd56C89GYqr9tLIky8ofd7RR9S0wrJzBEKBxGNUkI+2uu+YVIutl7Y0M4sJ8UA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DsMyQBQm; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8996606A322; Fri, 4 Dec 2026 15:39:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DsMyQBQm Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F10E1F000C2; Fri, 4 Dec 2026 15:39:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796398769; bh=O76S0SADxDrvkvRX+Y/m6LeMy6t0JHcBbaPXKTQO7dc=; h=Date:From:To:Cc:Subject; b=DsMyQBQmXmO7/gBMWLhmFRSbn6sC7xsedxLE+57D5T5w2EcDiBgI7FrJEL3swV3dI eyYas1pbbAjWxXTxhHnLjs7MlXCORpHM7twJGbtUtUtszfUPwCZ34zI9P5ZOilYYK2 wPZG6OT5VpeungLji+2NmNoPMbGiQg9/k8LJNnluQ2YMz8/Hq3LFKxxBMyM+wSNLH1 5cGhobkxRTGNZJmlws09mpwEDbYeXu4spY8crWs79XeJq2b7Ftd7EKeghqjrC+ZaFI d0j8kQzBLwQ61A4xmUBYlRlt3U3X6y5Q4g+vKuY03fsSXYz94L0amiU0Op9iK/jvmc Bp0BYGVCcGBHA== Date: Fri, 4 Dec 2026 15:39:15 +0000 From: Mark Brown To: Dave Airlie , DRI Cc: Alex Deucher , Alexey Minnekhanov , Alistair Popple , Aric Cyr , Arnd Bergmann , Badal Nilawar , Bob Zhou , Bommu Krishnaiah , Christian =?iso-8859-1?Q?K=F6nig?= , Christoph Berliner , Conor Dooley , Daniel Charles , Daniele Ceraolo Spurio , Davey Hughes , David Rosca , Dnyaneshwar Bhadane , Hugo Sales , Ivan Lipski , James Lin , Jason Gunthorpe , Jason Gunthorpe , Jia Wang , Jing Zhou , Joonas Lahtinen , Jouni =?iso-8859-1?Q?H=F6gander?= , Karthik Poosa , Linux Kernel Mailing List , Linux Next Mailing List , Luca Ceresoli , Maarten Lankhorst , Marcin Bernatowicz , Mario Limonciello , Matthew Auld , Matthew Brost , Matt Roper , "Michael J. Ruhl" , Michal Wajdeczko , Mika Kahola , Mina Almasry , Neil Armstrong , Osama Abdelkader , Paolo Abeni , Piotr =?iso-8859-1?Q?Pi=F3rkowski?= , Raag Jadav , Ray Wu , Riana Tauro , Rodrigo Vivi , Satyanarayana K V P , Stefan Ghetu , Stuart Summers , Tejas Upadhyay , Thomas =?iso-8859-1?Q?Hellstr=F6m?= , Umesh Nerlige Ramappa , Vinay Belgaumkar , Wayne Lin , Zongyao Bai Subject: linux-next: manual merge of the drm tree with the drm-fixes tree Message-ID: X-Mailing-List: linux-next@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="kwB5Ifurow0h2I5j" Content-Disposition: inline --kwB5Ifurow0h2I5j Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi all, Today's linux-next merge of the drm tree got conflicts in: drivers/gpu/drm/xe/xe_amc.c drivers/gpu/drm/xe/xe_bb.c drivers/gpu/drm/xe/xe_bb.h drivers/gpu/drm/xe/xe_configfs.c drivers/gpu/drm/xe/xe_configfs.h drivers/gpu/drm/xe/xe_device.c drivers/gpu/drm/xe/xe_hwmon.c drivers/gpu/drm/xe/xe_hwmon.h drivers/gpu/drm/xe/xe_migrate.c drivers/gpu/drm/xe/xe_oa.c drivers/gpu/drm/xe/xe_pagefault.c drivers/gpu/drm/xe/xe_pci_sriov.c drivers/gpu/drm/xe/xe_sa.c drivers/gpu/drm/xe/xe_sa.h drivers/gpu/drm/xe/xe_vm.c drivers/gpu/drm/xe/xe_ttm_vram_mgr.c drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h between commits: 6b3ea5a07eac0 ("drm/i915/psr: Flush on cursor update also when not using = Selective Fetch") 4247f4e2c10ea ("drm/i915/psr: Re-arm PSR after flush") 9bf9ced37e60a ("drm/i915/psr: Clear CFF state on flip flush") 87a841d8c0b71 ("drm/amdgpu: Fix VCE buffer size check for DPB and input s= urface") d5900171cd258 ("drm/xe: Drop RCU list handling for VRAM bad-page tracking= , use mgr->lock") 55d37e0c61553 ("drm/xe/ras: Fix sysctrl and drm-ras discrepancy") 7539edbca32d6 ("drm/xe/pat: Return error from xe_pat_init_early() on unkn= own platform") c9d8ef7f6835d ("drm/xe/hwmon: Decode mailbox temperature as sign-magnitud= e") d37dcdaa99b18 ("drm/xe/hwmon: Fix thermal sensor data handling") f2a144e9891b3 ("drm/xe/hwmon: Add helpers to validate thermal sensor read= ings") dbd199c775cbe ("drm/xe/hwmon: Handle unavailable memory controller sensor= s") f8795bced27bb ("drm/xe/gsc: Fix NVL-S GSC compatibility number") da475c094342b ("drm/xe: Fix build with DRM_XE=3Dy and CONFIGFS_FS=3Dm") 8801081e5991e ("drm/xe/gt: Use GT ordered workqueue for wedging") 402567ac0875b ("drm/xe: Drop xe_device_set_wedged_method()") a890a885b982f ("drm/xe/hw_error: Guard csc_hw_error_work() with runtime P= M") 031cc955b33b0 ("drm/xe: Move xe_device_wedged_fini() registration to xe_d= evice_probe_early()") b828d2b7326b5 ("drm/xe: Make xe_device_declare_wedged() IRQ safe") 1eb526467c96d ("drm/xe/pci_error: Flush GT wedge worker before bus reset") c948839c2e81a ("drm/xe/amc: Guard xe_amc_work() with runtime PM") fc0ba46fd8278 ("drm/xe: Access GT MMIO after pcode init") 74f1a6f03f2d4 ("drm/xe/xe_late_bind_fw: Fix firmware leak on missing mani= fest entry") a17c5ed0dc715 ("drm/xe/pm: Apply tile workarounds after PCODE initializat= ion") 3654a4d734df9 ("drm/xe/pf: Program LMTT directory pointer from each GT's = init path") c1234293de147 ("drm: xe: select configfs") eaf4ff850d97e ("drm/amd/display: Seed boot clocks to avoid lowering pre-O= S display clocks") 1ff6e3264ea63 ("drm/amdgpu: fix cleared PDE/PTE flag generation") fff46a322847a ("drm/amd/display: Keep FreeSync flip timing across plane r= esets") 988ab04d773ac ("drm/amd/display: Add the AUX backlight minimum once on th= e custom curve") 808951ee16738 ("drm/xe/hwmon: Report errors using structured xe_log API") 72296e02b46a3 ("drm/xe/hwmon: Unregister hwmon before the device is unplu= gged") 825c746a7b35c ("drm/amdgpu: move tmz init into early_init()") 8836bd1f3e72d ("drm/amdgpu: keep the address of cleared PDEs with transla= te further") 6aa8bc0cd6f3d ("drm/amdgpu/gfx9: deactivate the KIQ before MODE2 on APUs") 9380796af998e ("drm/xe: Hold a fence reference while waiting for migratio= n") 46addf0c61ca3 ("drm/xe/pf: Restore default PF scheduling on failed VF ena= ble") f892fb26089ef ("drm/xe/vf: Free preempt fences on rebind worker retry") 4051da01a5331 ("drm/xe: Unplug the drm device on probe failure") 3f6c98658ae92 ("drm/xe: Bump the LRU on a successful page fault") a2b96a739d0d1 ("drm/xe/oa: Use the drm device address_space for the OA st= ream fd") e825028ae19d6 ("drm/xe/sa: allow callers to specify interruptible suballo= cator waits") d271007c14df6 ("drm/xe/configfs: Fix out-of-bounds read in parse_wa_bb_li= nes()") 69623df21da0b ("drm/panel: raydium-rm68200: Set prepare_prev_first") f61b65abc9f41 ("dt-bindings: vendor-prefixes: Add shenrongda") c4466d2c3e776 ("net: devmem: unmap dma_buf synchronously on queue uninsta= ll") ede1618064a9b ("net: devmem: detach TX bindings on NETDEV_UNREGISTER") =66rom the drm-fixes tree and commits: 9ebe0565cf49d ("drm/panel: Use managed helpers in Novatek and Raydium pan= els") b55dbe9011975 ("dt-bindings: vendor-prefixes: Add Shen Chao Optoelectroni= cs") 03f51884ae681 ("drm/amd/display: Invalidate the DRR shadow after a hardwa= re re-init") de3e402df7f56 ("drm/xe/pat: Report missing PAT table using structured xe_= log API") 323047eeee980 ("drm/xe: Separate drm-ras netlink data from device and fir= mware RAS state") ca6e33701de86 ("drm/xe/hwmon: Fix thermal sensor data handling") 8fdd3d4c55ecb ("drm/xe/hwmon: Detect unavailable PCIe thermal sensors") 87ce4876e3f50 ("drm/xe/hwmon: Cache temperature availability to reduce pr= obe time") 993338420540a ("drm/xe/hwmon: Add platform-aware VRAM thermal channel sup= port") ae4cd659d22dc ("drm/xe/hwmon: Update memory controller temperature offset= for CRI") a3d7376b9d671 ("drm/xe: Validate thermal sensor count before reads") fc8d229d76102 ("drm/xe/hw_error: Guard csc_hw_error_work() with runtime P= M") 82f813f7a0de8 ("drm/xe: Introduce xe_wedge") 84474fbf9ae5a ("drm/xe/ras: Move xe_ras_process_errors() to xe_ras") 638d116d755ed ("drm/xe: Access GT MMIO after pcode init") 1f6ac7c86aa52 ("drm/xe: Memset g2g buffer on resume") 3e4b2a6349ed7 ("drm/xe/xe_late_bind_fw: Fix firmware leak on missing mani= fest entry") 918e85a4f597e ("drm/xe/xe_late_bind_fw: Add support to load Ocode firmwar= e") d7bc407b38090 ("drm/xe/xe_late_bind_fw: Enable late binding support for C= RI") 641ffb2132839 ("drm/xe/pm: Apply tile workarounds after PCODE initializat= ion") a8653bee6a040 ("drm/xe/pf: Program LMTT directory pointer from each GT's = init path") c9edd9cf7e613 ("drm/xe/pf: Use multicast writes for early-generation LMTT= config") c97f2efa0b561 ("drm/xe: Don't ask for bug reports on force probed platfor= ms") b229fdd689c8b ("drm/amdgpu: Allow multiple VCE create commands with same = handle") 24c0be8ac3ef2 ("drm/amdgpu: Fix VCE buffer size check for DPB and input s= urface") b6bd6c613fd5e ("drm/xe: Add support for wa_14028434957") ff03d9510426e ("drm/xe/pm: Introduce xe_device_suspend/resume()") ffa5e5d4e2203 ("drm/xe: Raise gt frequency slightly earlier.") 20470bb876bcf ("drm/i915/psr: Track all joined pipes for frontbuffer trac= king") 043ab39fe2daa ("drm/i915/psr: Flush on cursor update also when not using = Selective Fetch") 731ed8af13bde ("drm/i915/psr: Re-arm PSR after flush") 233e3453eb705 ("drm/i915/psr: Clear CFF state on flip flush") ae7dfdba190ee ("drm/xe/gsc: Define GSC firmware for NVL-P") 6bae3d8f56944 ("drm/xe/hwmon: Unregister hwmon before the device is unplu= gged") 7c830292deb22 ("drm/xe/drm_ras: Protect against unplugged or wedged devic= e") c7e8bd650f5e2 ("drm/xe/wedge: Introduce xe_wedge_mode_is_no_reset()") 78dcd686da6d0 ("drm/xe: Centralize migration fence updates") 344b7ec704ee0 ("drm/xe/vsec: Update VSEC probe order") 3b4cc31db4407 ("drm/xe/vsec: Support late bind fw information") 9a65a6fa8d448 ("drm/xe/pf: Extract VFs enabling revert helper") e29ff14670657 ("drm/xe/pf: Split VFs enabling into prepare and commit ste= ps") f0bb5563a090c ("drm/xe: Requeue the rebind worker instead of retrying in-= line on -EAGAIN") 77e0b2d2f4f37 ("treewide: Rename dma_buf_(un)map_attachment() to dma_buf_= (un)map_sgt()") 2f289740a04f0 ("drm/amd/display: Seed boot clocks to avoid lowering pre-O= S display clocks") 6250a83427847 ("drm/amdgpu: fix cleared PDE/PTE flag generation") 2fe41c5fb704e ("drm/amd: Select the firmware signing scheme the PSP repor= ts") e2830fb628915 ("drm/amd/display: Test FreeSync flip timing across plane r= esets") 127b26805f62d ("drm/amd/display: Pass linear brightness percent to power = module") af33373b09502 ("drm/amdgpu/gfx9: deactivate all compute HQDs before MODE2= on APUs") 6bb1ece53033f ("drm/xe: Unplug the drm device on probe failure") a476811debe02 ("drm/xe: Add a minimum age gate to the shrinker") 87566a32564ac ("drm/xe/oa: Use the drm device address_space for the OA st= ream fd") a70ba039ded24 ("drm/xe/oa: Fence OA ioctls and mmap against device unplug= and wedging") c5d40aafbd48c ("drm/xe/oa: Quiesce OA streams on driver unbind and PCI re= set") 363a5fe2367f0 ("drm/xe/bb: drop per-batch prefetch padding") a04198d180162 ("drm/xe/sa: remove unused shadow BO and two-step bb helper= s") fdadd139fbeac ("drm/xe: Make survivability_mode configfs attribute a bitm= ap") 3596e1932abcc ("drm/xe: Sort xe_config_device fields") bb93eb93dcbd0 ("drm/xe: Request cold-reset recovery on AMC thermal trip a= lert") 10ba9b264e0a5 ("drm/xe: Request cold-reset recovery on AMC OOB request al= ert") 6ea79c76b15dc ("drm/xe: Request cold-reset recovery on AMC CATERR alert") a7203f33910e0 ("drm/xe/i2c: Expose AMC alert reason through sysfs") 35a1828992b0f ("drm/xe/forcewake: flush delayed release at power and tear= down boundaries") 13c06626da71b ("drm/xe/vram: add decay-based reinstatement of soft-offlin= ed VRAM pages") b2d368d1b266e ("drm/xe/vram: don't let firmware-confirmed permanent bad p= ages decay back in") db385de15cebf ("drm/xe/hwmon: Expose pwm[1-3]_enable") 3a816f9409a70 ("drm/xe/hwmon: Enable fan curve control support") e695cb6694134 ("drm/xe/hwmon: Restore fan user tables after resume") 558c61e8b0491 ("drm/xe/hwmon: Refresh fan data after late binding") =66rom the drm tree. I fixed it up (see below) and can carry the fix as necessary. This is now fixed as far as linux-next is concerned, but any non trivial conflicts should be mentioned to your upstream maintainer when your tree is submitted for merging. You may also want to consider cooperating with the maintainer of the conflicting tree to minimise any particularly complex conflicts. diff --combined drivers/gpu/drm/xe/xe_amc.c index d6fe59669a74b,570c0fa1fe4c4..0000000000000 --- a/drivers/gpu/drm/xe/xe_amc.c +++ b/drivers/gpu/drm/xe/xe_amc.c @@@ -4,7 -4,6 +4,6 @@@ */ =20 #include - #include #include #include #include @@@ -12,6 -11,8 +11,8 @@@ #include #include =20 + #include +=20 #include "regs/xe_i2c_regs.h" =20 #include "xe_amc.h" @@@ -20,6 -21,7 +21,7 @@@ #include "xe_i2c.h" #include "xe_mmio.h" #include "xe_pm.h" + #include "xe_wedge.h" =20 /** * DOC: Add-In Management Controller (AMC) @@@ -48,6 -50,7 +50,7 @@@ enum xe_amc_alert=20 }; =20 static const char * const amc_alert[] =3D { + [AMC_ALERT_UNKNOWN] =3D "unknown", [AMC_ALERT_FW_DOWNLOAD] =3D "Firmware Download", [AMC_ALERT_THERMAL_TRIP] =3D "Thermal Trip", [AMC_ALERT_OOB_REQUEST] =3D "OOB Request", @@@ -58,6 -61,8 +61,8 @@@ struct xe_amc { struct xe_i2c *i2c; struct work_struct work; + enum xe_amc_alert alert_reason; + bool alert_seen; }; =20 struct amc_header { @@@ -106,6 -111,51 +111,51 @@@ static const struct amc_request amc_get }, }; =20 + /** + * DOC: AMC Alert Reason + * + * Expose the most recent AMC alert reason through device sysfs. + * + * See Documentation/ABI/testing/sysfs-driver-intel-xe-amc for the ABI + * specification. + */ +=20 + static enum xe_amc_alert amc_alert_reason(struct xe_amc *amc) + { + return READ_ONCE(amc->alert_reason); + } +=20 + static ssize_t amc_alert_reason_show(struct device *dev, + struct device_attribute *attr, char *buf) + { + struct xe_device *xe =3D pdev_to_xe_device(to_pci_dev(dev)); + struct xe_amc *amc =3D xe->i2c->amc; + enum xe_amc_alert alert_reason; +=20 + /* Pairs with smp_store_release() to make alert_reason visible. */ + if (!smp_load_acquire(&amc->alert_seen)) + return sysfs_emit(buf, "none\n"); +=20 + alert_reason =3D amc_alert_reason(amc); +=20 + return sysfs_emit(buf, "%s\n", amc_alert[alert_reason]); + } + static DEVICE_ATTR_RO(amc_alert_reason); +=20 + static void amc_remove_alert_sysfs(struct xe_i2c *i2c) + { + struct device *dev =3D i2c->drm_dev; +=20 + device_remove_file(dev, &dev_attr_amc_alert_reason); + } +=20 + static int amc_create_alert_sysfs(struct xe_i2c *i2c) + { + struct device *dev =3D i2c->drm_dev; +=20 + return device_create_file(dev, &dev_attr_amc_alert_reason); + } +=20 static void xe_amc_work(struct work_struct *work) { const struct amc_request *request =3D &amc_get_alert_reason; @@@ -114,6 -164,8 +164,8 @@@ u8 alert_reason =3D AMC_ALERT_UNKNOWN; struct amc_response response; struct i2c_client *client; + const char *alert_str; + unsigned long recovery =3D 0; int ret; =20 xe_pm_runtime_get(xe); @@@ -158,21 -210,36 +210,36 @@@ out_reassert_interrupt: xe_mmio_rmw32(amc->i2c->mmio, I2C_CONFIG_CMD, PCI_COMMAND_INTX_DISABLE, = 0); =20 + WRITE_ONCE(amc->alert_reason, alert_reason < ARRAY_SIZE(amc_alert) ? + alert_reason : AMC_ALERT_UNKNOWN); + /* Publish alert_reason to readers using smp_load_acquire(). */ + smp_store_release(&amc->alert_seen, true); +=20 switch (alert_reason) { case AMC_ALERT_FW_DOWNLOAD: - case AMC_ALERT_THERMAL_TRIP: - case AMC_ALERT_OOB_REQUEST: + alert_str =3D amc_alert[alert_reason]; + recovery =3D DRM_WEDGE_RECOVERY_VENDOR; + break; case AMC_ALERT_OOB_RESET: + alert_str =3D amc_alert[alert_reason]; + recovery =3D DRM_WEDGE_RECOVERY_REBIND | DRM_WEDGE_RECOVERY_BUS_RESET; + break; case AMC_ALERT_CATERR: - dev_warn(amc->i2c->drm_dev, "AMC Alert: %s\n", amc_alert[alert_reason]); - xe_device_declare_wedged(i2c_client_to_xe_device(client), - DRM_WEDGE_RECOVERY_REBIND | DRM_WEDGE_RECOVERY_BUS_RESET); + case AMC_ALERT_OOB_REQUEST: + case AMC_ALERT_THERMAL_TRIP: + alert_str =3D amc_alert[alert_reason]; + recovery =3D DRM_WEDGE_RECOVERY_COLD_RESET; break; default: - dev_warn(amc->i2c->drm_dev, "unknown AMC alert: %d\n", alert_reason); + alert_str =3D amc_alert[AMC_ALERT_UNKNOWN]; break; } =20 + drm_warn(&xe->drm, "AMC Alert: %s (%u)\n", alert_str, alert_reason); +=20 + if (recovery) + xe_wedge_declare(xe, recovery); +=20 xe_pm_runtime_put(xe); } =20 @@@ -184,6 -251,7 +251,7 @@@ void xe_amc_handle_alert(struct xe_i2c=20 int xe_amc_init(struct xe_i2c *i2c) { struct xe_amc *amc; + int ret; =20 amc =3D kzalloc_obj(*amc); if (!amc) @@@ -193,13 -261,21 +261,21 @@@ i2c->amc =3D amc; amc->i2c =3D i2c; =20 - return 0; + ret =3D amc_create_alert_sysfs(i2c); + if (ret) { + kfree(i2c->amc); + i2c->amc =3D NULL; + } +=20 + return ret; } =20 void xe_amc_exit(struct xe_i2c *i2c) { if (i2c->amc) { + amc_remove_alert_sysfs(i2c); cancel_work_sync(&i2c->amc->work); kfree(i2c->amc); + i2c->amc =3D NULL; } } diff --combined drivers/gpu/drm/xe/xe_bb.c index 6308a1dca4647,7f71233b10360..0000000000000 --- a/drivers/gpu/drm/xe/xe_bb.c +++ b/drivers/gpu/drm/xe/xe_bb.c @@@ -14,20 -14,6 +14,6 @@@ #include "xe_sched_job.h" #include "xe_vm_types.h" =20 - static int bb_prefetch(struct xe_gt *gt) - { - struct xe_device *xe =3D gt_to_xe(gt); -=20 - if (GRAPHICS_VERx100(xe) >=3D 1250 && xe_gt_is_main_type(gt)) - /* - * RCS and CCS require 1K, although other engines would be - * okay with 512. - */ - return SZ_1K; - else - return SZ_512; - } -=20 struct xe_bb *__xe_bb_new(struct xe_gt *gt, u32 dwords, bool usm, bool in= tr) { struct xe_tile *tile =3D gt_to_tile(gt); @@@ -38,13 -24,13 +24,13 @@@ return ERR_PTR(-ENOMEM); =20 /* - * We need to allocate space for the requested number of dwords, - * one additional MI_BATCH_BUFFER_END dword, and additional buffer - * space to accommodate the platform-specific hardware prefetch - * requirements. + * We need to allocate space for the requested number of dwords and + * one additional MI_BATCH_BUFFER_END dword. Hardware CS prefetch + * at the end of the pool BO is covered by the SZ_4K guard region + * reserved by xe_sa_bo_manager_init(). */ bb->bo =3D xe_sa_bo_new(!usm ? tile->mem.kernel_bb_pool : gt->usm.bb_poo= l, - 4 * (dwords + 1) + bb_prefetch(gt), intr); + 4 * (dwords + 1), intr); if (IS_ERR(bb->bo)) { err =3D PTR_ERR(bb->bo); goto err; @@@ -59,68 -45,6 +45,6 @@@ err return ERR_PTR(err); } =20 - /** - * xe_bb_alloc() - Allocate a new batch buffer structure - * @gt: the &xe_gt - * - * Allocates and initializes a new xe_bb structure with an associated - * uninitialized suballoc object. - * - * Returns: Batch buffer structure or an ERR_PTR(-ENOMEM). - */ - struct xe_bb *xe_bb_alloc(struct xe_gt *gt) - { - struct xe_bb *bb =3D kmalloc_obj(*bb); - int err; -=20 - if (!bb) - return ERR_PTR(-ENOMEM); -=20 - bb->bo =3D xe_sa_bo_alloc(GFP_KERNEL); - if (IS_ERR(bb->bo)) { - err =3D PTR_ERR(bb->bo); - goto err; - } -=20 - return bb; -=20 - err: - kfree(bb); - return ERR_PTR(err); - } -=20 - /** - * xe_bb_init() - Initialize a batch buffer with memory from a sub-alloca= tor pool - * @bb: Batch buffer structure to initialize - * @bb_pool: Suballoc memory pool to allocate from - * @dwords: Number of dwords to be allocated - * - * Initializes the batch buffer by allocating memory from the specified - * suballoc pool. - * - * Return: 0 on success, negative error code on failure. - */ - int xe_bb_init(struct xe_bb *bb, struct xe_sa_manager *bb_pool, u32 dword= s) - { - int err; -=20 - /* - * We need to allocate space for the requested number of dwords & - * one additional MI_BATCH_BUFFER_END dword. Since the whole SA - * is submitted to HW, we need to make sure that the last instruction - * is not over written when the last chunk of SA is allocated for BB. - * So, this extra DW acts as a guard here. - */ - err =3D xe_sa_bo_init(bb_pool, bb->bo, 4 * (dwords + 1)); - if (err) - return err; -=20 - bb->cs =3D xe_sa_bo_cpu_addr(bb->bo); - bb->len =3D 0; -=20 - return 0; - } -=20 static struct xe_sched_job * __xe_bb_create_job(struct xe_exec_queue *q, struct xe_bb *bb, u64 *addr) { @@@ -129,7 -53,7 +53,7 @@@ if (bb->len =3D=3D 0 || bb->cs[bb->len - 1] !=3D MI_BATCH_BUFFER_END) bb->cs[bb->len++] =3D MI_BATCH_BUFFER_END; =20 - xe_gt_assert(q->gt, bb->len * 4 + bb_prefetch(q->gt) <=3D size); + xe_gt_assert(q->gt, bb->len * 4 <=3D size); =20 xe_sa_bo_flush_write(bb->bo); =20 diff --combined drivers/gpu/drm/xe/xe_bb.h index 66fcc40c0b002,6f685176d4e87..0000000000000 --- a/drivers/gpu/drm/xe/xe_bb.h +++ b/drivers/gpu/drm/xe/xe_bb.h @@@ -14,7 -14,6 +14,6 @@@ struct dma_fence =20 struct xe_gt; struct xe_exec_queue; - struct xe_sa_manager; struct xe_sched_job; =20 struct xe_bb *__xe_bb_new(struct xe_gt *gt, u32 dwords, bool usm, bool in= tr); @@@ -23,8 -22,6 +22,6 @@@ static inline struct xe_bb *xe_bb_new(s { return __xe_bb_new(gt, dwords, usm, false); } - struct xe_bb *xe_bb_alloc(struct xe_gt *gt); - int xe_bb_init(struct xe_bb *bb, struct xe_sa_manager *bb_pool, u32 dword= s); struct xe_sched_job *xe_bb_create_job(struct xe_exec_queue *q, struct xe_bb *bb); struct xe_sched_job *xe_bb_create_migration_job(struct xe_exec_queue *q, diff --combined drivers/gpu/drm/xe/xe_configfs.c index d3b7833732a71,3a05e1cd9ddad..0000000000000 --- a/drivers/gpu/drm/xe/xe_configfs.c +++ b/drivers/gpu/drm/xe/xe_configfs.c @@@ -4,7 -4,6 +4,6 @@@ */ =20 #include - #include #include #include #include @@@ -15,12 -14,10 +14,10 @@@ =20 #include "instructions/xe_mi_commands.h" #include "xe_configfs.h" + #include "xe_configfs_debug.h" #include "xe_defaults.h" #include "xe_gt_types.h" - #include "xe_hw_engine_types.h" #include "xe_module.h" - #include "xe_pci_types.h" - #include "xe_sriov_types.h" =20 /** * DOC: Xe Configfs @@@ -58,12 -55,17 +55,17 @@@ * =E2=94=82=C2=A0=C2=A0 =E2=94=94=E2=94=80=E2=94=80 ... * : * =E2=94=94=E2=94=80=E2=94=80 0000:03:00.0 - * =E2=94=9C=E2=94=80=E2=94=80 survivability_mode - * =E2=94=9C=E2=94=80=E2=94=80 gt_types_allowed - * =E2=94=9C=E2=94=80=E2=94=80 engines_allowed - * =E2=94=9C=E2=94=80=E2=94=80 migrate_ulls_period_ms + * =E2=94=9C=E2=94=80=E2=94=80 bad_page_decay_secs + * =E2=94=9C=E2=94=80=E2=94=80 ctx_restore_mid_bb + * =E2=94=9C=E2=94=80=E2=94=80 ctx_restore_post_bb + * =E2=94=9C=E2=94=80=E2=94=80 enable_multi_queue * =E2=94=9C=E2=94=80=E2=94=80 enable_psmi - * =E2=94=94=E2=94=80=E2=94=80 disable_vram_page_offline + * =E2=94=9C=E2=94=80=E2=94=80 enable_vram_page_offline + * =E2=94=9C=E2=94=80=E2=94=80 engines_allowed + * =E2=94=9C=E2=94=80=E2=94=80 force_ocode_load + * =E2=94=9C=E2=94=80=E2=94=80 gt_types_allowed + * =E2=94=9C=E2=94=80=E2=94=80 migrate_ulls_period_ms + * =E2=94=94=E2=94=80=E2=94=80 survivability_mode * * After configuring the attributes as per next section, the device can be * probed with:: @@@ -75,104 -77,6 +77,6 @@@ * Configure Attributes * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D * - * Survivability mode: - * ------------------- - * - * Enable survivability mode on supported cards. This setting only takes - * effect when probing the device. Example to enable it:: - * - * # echo 1 > /sys/kernel/config/xe/0000:03:00.0/survivability_mode - * - * This attribute can only be set before binding to the device. - * - * Allowed GT types: - * ----------------- - * - * Allow only specific types of GTs to be detected and initialized by the - * driver. Any combination of GT types can be enabled/disabled, although - * some settings will cause the device to fail to probe. - * - * Writes support both comma- and newline-separated input format. Reads - * will always return one GT type per line. "primary" and "media" are the - * GT type names supported by this interface. - * - * This attribute can only be set before binding to the device. - * - * Examples: - * - * Allow both primary and media GTs to be initialized and used. This mat= ches - * the driver's default behavior:: - * - * # echo 'primary,media' > /sys/kernel/config/xe/0000:03:00.0/gt_types_a= llowed - * - * Allow only the primary GT of each tile to be initialized and used, - * effectively disabling the media GT if it exists on the platform:: - * - * # echo 'primary' > /sys/kernel/config/xe/0000:03:00.0/gt_types_allowed - * - * Allow only the media GT of each tile to be initialized and used, - * effectively disabling the primary GT. **This configuration will cause - * device probe failure on all current platforms, but may be allowed on - * igpu platforms in the future**:: - * - * # echo 'media' > /sys/kernel/config/xe/0000:03:00.0/gt_types_allowed - * - * Disable all GTs. Only other GPU IP (such as display) is potentially u= sable. - * **This configuration will cause device probe failure on all current - * platforms, but may be allowed on igpu platforms in the future**:: - * - * # echo '' > /sys/kernel/config/xe/0000:03:00.0/gt_types_allowed - * - * Allowed engines: - * ---------------- - * - * Allow only a set of engine(s) to be available, disabling the other eng= ines - * even if they are available in hardware. This is applied after HW fuses= are - * considered on each tile. Examples: - * - * Allow only one render and one copy engines, nothing else:: - * - * # echo 'rcs0,bcs0' > /sys/kernel/config/xe/0000:03:00.0/engines_allowed - * - * Allow only compute engines and first copy engine:: - * - * # echo 'ccs*,bcs0' > /sys/kernel/config/xe/0000:03:00.0/engines_allowed - * - * Note that the engine names are the per-GT hardware names. On multi-tile - * platforms, writing ``rcs0,bcs0`` to this file would allow the first re= nder - * and copy engines on each tile. - * - * The requested configuration may not be supported by the platform and d= river - * may fail to probe. For example: if at least one copy engine is expecte= d to be - * available for migrations, but it's disabled. This is intended for debu= gging - * purposes only. - * - * This attribute can only be set before binding to the device. - * - * PSMI - * ---- - * - * Enable extra debugging capabilities to trace engine execution. Only us= eful - * during early platform enabling and requires additional hardware connec= ted. - * Once it's enabled, additionals WAs are added and runtime configuration= is - * done via debugfs. Example to enable it:: - * - * # echo 1 > /sys/kernel/config/xe/0000:03:00.0/enable_psmi - * - * This attribute can only be set before binding to the device. - * - * Disable VRAM page offline: - * ---------------------------- - * - * 0, n, N, false - Do not disable (Offlining is active - default) - * 1, y, Y, true - Disable vram page offline (Logging only) - * - * Example to disable VRAM offline:: - * - * # echo 1 > /sys/kernel/config/xe/0000:03:00.0/disable_vram_page_o= ffline - * - * This attribute can only be set on CRI before binding to the device. - * * Context restore BB * ------------------ * @@@ -225,6 -129,150 +129,150 @@@ * Currently this is implemented only for post and mid context restore and * these attributes can only be set before binding to the device. * + * Enable multi-queue + * ------------------ + * + * Multi-queue support on the device is enabled by default where the + * hardware supports it. Writing 0 force-disables multi-queue support: + * multi-queue exec-queue group creation via ioctl is refused, and the + * GuC feature is disabled:: + * + * # echo 0 > /sys/kernel/config/xe/0000:03:00.0/enable_multi_queue + * + * This attribute can only be set before binding to the device. + * + * PSMI + * ---- + * + * Enable extra debugging capabilities to trace engine execution. Only us= eful + * during early platform enabling and requires additional hardware connec= ted. + * Once it's enabled, additionals WAs are added and runtime configuration= is + * done via debugfs. Example to enable it:: + * + * # echo 1 > /sys/kernel/config/xe/0000:03:00.0/enable_psmi + * + * This attribute can only be set before binding to the device. + * + * Enable VRAM page offline: + * ------------------------- + * + * 0, n, N, false - Disable vram page offline (Logging only) + * 1, y, Y, true - Enable vram page offline (Offlining is active - defa= ult) + * + * Example to disable VRAM offline:: + * + * # echo 0 > /sys/kernel/config/xe/0000:03:00.0/enable_vram_page_of= fline + * + * This attribute can only be set on CRI before binding to the device. + * + * Bad page decay: + * --------------- + * + * Number of seconds without a new report after which a soft-offlined VRAM + * page is reinstated (default 86400, 0 disables). Pages that faulted aga= in + * or came from firmware's permanent list are never reinstated:: + * + * # echo 3600 > /sys/kernel/config/xe/0000:03:00.0/bad_page_decay_s= ecs + * + * This attribute can only be set on CRI before binding to the device. + * + * Allowed engines: + * ---------------- + * + * Allow only a set of engine(s) to be available, disabling the other eng= ines + * even if they are available in hardware. This is applied after HW fuses= are + * considered on each tile. Examples: + * + * Allow only one render and one copy engines, nothing else:: + * + * # echo 'rcs0,bcs0' > /sys/kernel/config/xe/0000:03:00.0/engines_allowed + * + * Allow only compute engines and first copy engine:: + * + * # echo 'ccs*,bcs0' > /sys/kernel/config/xe/0000:03:00.0/engines_allowed + * + * Note that the engine names are the per-GT hardware names. On multi-tile + * platforms, writing ``rcs0,bcs0`` to this file would allow the first re= nder + * and copy engines on each tile. + * + * The requested configuration may not be supported by the platform and d= river + * may fail to probe. For example: if at least one copy engine is expecte= d to be + * available for migrations, but it's disabled. This is intended for debu= gging + * purposes only. + * + * This attribute can only be set before binding to the device. + * + * Force OCODE load: + * ----------------- + * + * With CONFIG_DRM_XE_DEBUG enabled, force an OCODE load at probe and res= ume + * even if OCODE is already running. This attribute can only be set before + * binding. + * + * Allowed GT types: + * ----------------- + * + * Allow only specific types of GTs to be detected and initialized by the + * driver. Any combination of GT types can be enabled/disabled, although + * some settings will cause the device to fail to probe. + * + * Writes support both comma- and newline-separated input format. Reads + * will always return one GT type per line. "primary" and "media" are the + * GT type names supported by this interface. + * + * This attribute can only be set before binding to the device. + * + * Examples: + * + * Allow both primary and media GTs to be initialized and used. This mat= ches + * the driver's default behavior:: + * + * # echo 'primary,media' > /sys/kernel/config/xe/0000:03:00.0/gt_types_a= llowed + * + * Allow only the primary GT of each tile to be initialized and used, + * effectively disabling the media GT if it exists on the platform:: + * + * # echo 'primary' > /sys/kernel/config/xe/0000:03:00.0/gt_types_allowed + * + * Allow only the media GT of each tile to be initialized and used, + * effectively disabling the primary GT. **This configuration will cause + * device probe failure on all current platforms, but may be allowed on + * igpu platforms in the future**:: + * + * # echo 'media' > /sys/kernel/config/xe/0000:03:00.0/gt_types_allowed + * + * Disable all GTs. Only other GPU IP (such as display) is potentially u= sable. + * **This configuration will cause device probe failure on all current + * platforms, but may be allowed on igpu platforms in the future**:: + * + * # echo '' > /sys/kernel/config/xe/0000:03:00.0/gt_types_allowed + * + * Migrate ULLS Period (ms) + * ------------------------ + * + * Migrate ULLS period, in milliseconds. This is the delay between enteri= ng + * migrate ULLS (a continuously running batch) and exiting it. Migrate UL= LS is + * currently entered during page faults and SVM prefetch operations. Defa= ult 5, + * zero indicates ULLS is disabled. + * + * How to disable migration ULLS: + * + * # echo 0 > /sys/kernel/config/xe/0000:03:00.0/migrate_ulls_period_ms + * + * This attribute can only be set before binding to the device. + * + * Survivability mode: + * ------------------- + * + * Enable survivability mode on supported cards. This is a bitmap; bit 0 + * enables survivability mode and is currently the only bit defined, so t= he + * only accepted values remain 0 and 1. This setting only takes effect wh= en + * probing the device. Example to enable it:: + * + * # echo 1 > /sys/kernel/config/xe/0000:03:00.0/survivability_mode + * + * This attribute can only be set before binding to the device. + * * Max SR-IOV Virtual Functions * ---------------------------- * @@@ -251,32 -299,6 +299,6 @@@ * * This setting only takes effect when probing the device. * - * Enable multi-queue - * ------------------ - * - * Multi-queue support on the device is enabled by default where the - * hardware supports it. Writing 0 force-disables multi-queue support: - * multi-queue exec-queue group creation via ioctl is refused, and the - * GuC feature is disabled:: - * - * # echo 0 > /sys/kernel/config/xe/0000:03:00.0/enable_multi_queue - * - * This attribute can only be set before binding to the device. - * - * Migrate ULLS Period (ms) - * ------------------------ - * - * Migrate ULLS period, in milliseconds. This is the delay between enteri= ng - * migrate ULLS (a continuously running batch) and exiting it. Migrate UL= LS is - * currently entered during page faults and SVM prefetch operations. Defa= ult 5, - * zero indicates ULLS is disabled. - * - * How to disable migration ULLS: - * - * # echo 0 > /sys/kernel/config/xe/0000:03:00.0/migrate_ulls_period_ms - * - * This attribute can only be set before binding to the device. - * * Remove devices * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D * @@@ -285,48 -307,16 +307,16 @@@ * # rmdir /sys/kernel/config/xe/0000:03:00.0/ */ =20 - /* Similar to struct xe_bb, but not tied to HW (yet) */ - struct wa_bb { - u32 *cs; - u32 len; /* in dwords */ - }; -=20 - struct xe_config_group_device { - struct config_group group; - struct config_group sriov; -=20 - struct xe_config_device { - u64 gt_types_allowed; - u64 engines_allowed; - u32 migrate_ulls_period_ms; - struct wa_bb ctx_restore_post_bb[XE_ENGINE_CLASS_MAX]; - struct wa_bb ctx_restore_mid_bb[XE_ENGINE_CLASS_MAX]; - bool survivability_mode; - bool enable_psmi; - bool enable_multi_queue; - bool disable_vram_page_offline; - struct { - unsigned int max_vfs; - bool admin_only_pf; - } sriov; - } config; -=20 - /* protects attributes */ - struct mutex lock; - /* matching descriptor */ - const struct xe_device_desc *desc; - /* tentative SR-IOV mode */ - enum xe_sriov_mode mode; - }; -=20 static const struct xe_config_device device_defaults =3D { - .gt_types_allowed =3D U64_MAX, .engines_allowed =3D U64_MAX, + .gt_types_allowed =3D U64_MAX, .migrate_ulls_period_ms =3D 5, - .survivability_mode =3D false, - .enable_psmi =3D false, + .survivability_mode =3D 0, .enable_multi_queue =3D true, - .disable_vram_page_offline =3D false, + .enable_psmi =3D false, + .enable_vram_page_offline =3D true, + .bad_page_decay_secs =3D 24 * 60 * 60, + .force_ocode_load =3D false, .sriov =3D { .max_vfs =3D XE_DEFAULT_MAX_VFS, .admin_only_pf =3D XE_DEFAULT_ADMIN_ONLY_PF, @@@ -403,301 -393,80 +393,80 @@@ static bool is_bound(struct xe_config_g return ret; } =20 - static ssize_t survivability_mode_show(struct config_item *item, char *pa= ge) - { - struct xe_config_device *dev =3D to_xe_config_device(item); + static struct xe_config_group_device *find_xe_config_group_device(struct = pci_dev *pdev); =20 - return sprintf(page, "%d\n", dev->survivability_mode); - } -=20 - static ssize_t survivability_mode_store(struct config_item *item, const c= har *page, size_t len) - { - struct xe_config_group_device *dev =3D to_xe_config_group_device(item); - bool survivability_mode; - int ret; -=20 - ret =3D kstrtobool(page, &survivability_mode); - if (ret) - return ret; -=20 - guard(mutex)(&dev->lock); - if (is_bound(dev)) - return -EBUSY; -=20 - dev->config.survivability_mode =3D survivability_mode; -=20 - return len; - } -=20 - static ssize_t gt_types_allowed_show(struct config_item *item, char *page) - { - struct xe_config_device *dev =3D to_xe_config_device(item); - char *p =3D page; -=20 - for (size_t i =3D 0; i < ARRAY_SIZE(gt_types); i++) - if (dev->gt_types_allowed & BIT_ULL(gt_types[i].type)) - p +=3D sprintf(p, "%s\n", gt_types[i].name); -=20 - return p - page; - } -=20 - static ssize_t gt_types_allowed_store(struct config_item *item, const cha= r *page, - size_t len) - { - struct xe_config_group_device *dev =3D to_xe_config_group_device(item); - char *buf __free(kfree) =3D kstrdup(page, GFP_KERNEL); - char *p =3D buf; - u64 typemask =3D 0; -=20 - if (!buf) - return -ENOMEM; -=20 - while (p) { - char *typename =3D strsep(&p, ",\n"); - bool matched =3D false; -=20 - if (typename[0] =3D=3D '\0') - continue; -=20 - for (size_t i =3D 0; i < ARRAY_SIZE(gt_types); i++) { - if (strcmp(typename, gt_types[i].name) =3D=3D 0) { - typemask |=3D BIT(gt_types[i].type); - matched =3D true; - break; - } - } -=20 - if (!matched) - return -EINVAL; - } -=20 - guard(mutex)(&dev->lock); - if (is_bound(dev)) - return -EBUSY; -=20 - dev->config.gt_types_allowed =3D typemask; -=20 - return len; - } -=20 - static ssize_t engines_allowed_show(struct config_item *item, char *page) - { - struct xe_config_device *dev =3D to_xe_config_device(item); - char *p =3D page; -=20 - for (size_t i =3D 0; i < ARRAY_SIZE(engine_info); i++) { - u64 mask =3D engine_info[i].mask; -=20 - if ((dev->engines_allowed & mask) =3D=3D mask) { - p +=3D sprintf(p, "%s*\n", engine_info[i].cls); - } else if (mask & dev->engines_allowed) { - u16 bit0 =3D __ffs64(mask), bit; -=20 - mask &=3D dev->engines_allowed; -=20 - for_each_set_bit(bit, (const unsigned long *)&mask, 64) - p +=3D sprintf(p, "%s%u\n", engine_info[i].cls, - bit - bit0); - } - } -=20 - return p - page; - } -=20 - /* - * Lookup engine_info. If @mask is not NULL, reduce the mask according to= the - * instance in @pattern. + /** + * xe_configfs_get_ctx_restore_mid_bb - get configfs ctx_restore_mid_bb s= etting + * @pdev: pci device + * @class: hw engine class + * @cs: destination buffer for the batch, or NULL to only query the length + * @max_len: capacity of @cs, in dwords * - * Examples of inputs: - * - lookup_engine_info("rcs0", &mask): return "rcs" entry from @engine_i= nfo and - * mask =3D=3D BIT_ULL(XE_HW_ENGINE_RCS0) - * - lookup_engine_info("rcs*", &mask): return "rcs" entry from @engine_i= nfo and - * mask =3D=3D XE_HW_ENGINE_RCS_MASK - * - lookup_engine_info("rcs", NULL): return "rcs" entry from @engine_info + * Copy the configured batch into @cs while holding the configfs lock, so= the + * caller never gets a pointer to storage owned by the configfs group. + * + * Return: Number of dwords used in the mid_ctx_restore setting in config= fs, or + * -ENOSPC if it does not fit in @max_len */ - static const struct engine_info *lookup_engine_info(const char *pattern, = u64 *mask) + ssize_t xe_configfs_get_ctx_restore_mid_bb(struct pci_dev *pdev, + enum xe_engine_class class, + u32 *cs, size_t max_len) { - for (size_t i =3D 0; i < ARRAY_SIZE(engine_info); i++) { - u8 instance; - u16 bit; + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + ssize_t len; =20 - if (!str_has_prefix(pattern, engine_info[i].cls)) - continue; + if (!dev) + return 0; =20 - pattern +=3D strlen(engine_info[i].cls); - if (!mask) - return *pattern ? NULL : &engine_info[i]; -=20 - if (!strcmp(pattern, "*")) { - *mask =3D engine_info[i].mask; - return &engine_info[i]; + scoped_guard(mutex, &dev->lock) { + len =3D dev->config.ctx_restore_mid_bb[class].len; + if (cs && len) { + if (len > max_len) + len =3D -ENOSPC; + else + memcpy(cs, dev->config.ctx_restore_mid_bb[class].cs, + len * sizeof(u32)); } -=20 - if (kstrtou8(pattern, 10, &instance)) - return NULL; -=20 - bit =3D __ffs64(engine_info[i].mask) + instance; - if (bit >=3D fls64(engine_info[i].mask)) - return NULL; -=20 - *mask =3D BIT_ULL(bit); - return &engine_info[i]; } -=20 - return NULL; - } -=20 - static int parse_engine(const char *s, const char *end_chars, u64 *mask, - const struct engine_info **pinfo) - { - char buf[MAX_ENGINE_CLASS_CHARS + MAX_ENGINE_INSTANCE_CHARS + 1]; - const struct engine_info *info; - size_t len; -=20 - len =3D strcspn(s, end_chars); - if (len >=3D sizeof(buf)) - return -EINVAL; -=20 - memcpy(buf, s, len); - buf[len] =3D '\0'; -=20 - info =3D lookup_engine_info(buf, mask); - if (!info) - return -ENOENT; -=20 - if (pinfo) - *pinfo =3D info; + config_group_put(&dev->group); =20 return len; } =20 - static ssize_t engines_allowed_store(struct config_item *item, const char= *page, - size_t len) + /** + * xe_configfs_get_ctx_restore_post_bb - get configfs ctx_restore_post_bb= setting + * @pdev: pci device + * @class: hw engine class + * @cs: destination buffer for the batch, or NULL to only query the length + * @max_len: capacity of @cs, in dwords + * + * Copy the configured batch into @cs while holding the configfs lock, so= the + * caller never gets a pointer to storage owned by the configfs group. + * + * Return: Number of dwords used in the post_ctx_restore setting in confi= gfs, or + * -ENOSPC if it does not fit in @max_len + */ + ssize_t xe_configfs_get_ctx_restore_post_bb(struct pci_dev *pdev, + enum xe_engine_class class, + u32 *cs, size_t max_len) { - struct xe_config_group_device *dev =3D to_xe_config_group_device(item); - ssize_t patternlen, p; - u64 mask, val =3D 0; + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + ssize_t len; =20 - for (p =3D 0; p < len; p +=3D patternlen + 1) { - patternlen =3D parse_engine(page + p, ",\n", &mask, NULL); - if (patternlen < 0) - return -EINVAL; + if (!dev) + return 0; =20 - val |=3D mask; + scoped_guard(mutex, &dev->lock) { + len =3D dev->config.ctx_restore_post_bb[class].len; + if (cs && len) { + if (len > max_len) + len =3D -ENOSPC; + else + memcpy(cs, dev->config.ctx_restore_post_bb[class].cs, + len * sizeof(u32)); + } } -=20 - guard(mutex)(&dev->lock); - if (is_bound(dev)) - return -EBUSY; -=20 - dev->config.engines_allowed =3D val; -=20 - return len; - } -=20 - static ssize_t enable_psmi_show(struct config_item *item, char *page) - { - struct xe_config_device *dev =3D to_xe_config_device(item); -=20 - return sprintf(page, "%d\n", dev->enable_psmi); - } -=20 - static ssize_t enable_psmi_store(struct config_item *item, const char *pa= ge, size_t len) - { - struct xe_config_group_device *dev =3D to_xe_config_group_device(item); - bool val; - int ret; -=20 - ret =3D kstrtobool(page, &val); - if (ret) - return ret; -=20 - guard(mutex)(&dev->lock); - if (is_bound(dev)) - return -EBUSY; -=20 - dev->config.enable_psmi =3D val; -=20 - return len; - } -=20 - static ssize_t enable_multi_queue_show(struct config_item *item, char *pa= ge) - { - struct xe_config_device *dev =3D to_xe_config_device(item); -=20 - return sprintf(page, "%d\n", dev->enable_multi_queue); - } -=20 - static ssize_t enable_multi_queue_store(struct config_item *item, const c= har *page, - size_t len) - { - struct xe_config_group_device *dev =3D to_xe_config_group_device(item); - bool val; - int ret; -=20 - ret =3D kstrtobool(page, &val); - if (ret) - return ret; -=20 - guard(mutex)(&dev->lock); - if (is_bound(dev)) - return -EBUSY; -=20 - dev->config.enable_multi_queue =3D val; -=20 - return len; - } -=20 - static ssize_t migrate_ulls_period_ms_show(struct config_item *item, char= *page) - { - struct xe_config_device *dev =3D to_xe_config_device(item); -=20 - return sprintf(page, "%u\n", dev->migrate_ulls_period_ms); - } -=20 - static ssize_t migrate_ulls_period_ms_store(struct config_item *item, - const char *page, size_t len) - { - struct xe_config_group_device *dev =3D to_xe_config_group_device(item); - u32 val; - int ret; -=20 - ret =3D kstrtou32(page, 0, &val); - if (ret) - return ret; -=20 - guard(mutex)(&dev->lock); - if (is_bound(dev)) - return -EBUSY; -=20 - dev->config.migrate_ulls_period_ms =3D val; -=20 - return len; - } -=20 - static ssize_t disable_vram_page_offline_show(struct config_item *item, c= har *page) - { - struct xe_config_device *dev =3D to_xe_config_device(item); -=20 - return sprintf(page, "%s\n", str_yes_no(dev->disable_vram_page_offline)); - } -=20 - static ssize_t disable_vram_page_offline_store(struct config_item *item, - const char *page, size_t len) - { - struct xe_config_group_device *dev =3D to_xe_config_group_device(item); - bool val; - int ret; -=20 - ret =3D kstrtobool(page, &val); - if (ret) - return ret; -=20 - guard(mutex)(&dev->lock); - if (is_bound(dev)) - return -EBUSY; -=20 - dev->config.disable_vram_page_offline =3D val; + config_group_put(&dev->group); =20 return len; } @@@ -804,6 -573,9 +573,9 @@@ static ssize_t parse_hex(const char *li return p - line; } =20 + static int parse_engine(const char *s, const char *end_chars, u64 *mask, + const struct engine_info **pinfo); +=20 /* * Parse lines with the format * @@@ -941,26 -713,593 +713,593 @@@ static ssize_t ctx_restore_post_bb_stor return wa_bb_store(dev->config.ctx_restore_post_bb, dev, data, sz); } =20 + /** + * xe_configfs_get_engines_allowed - get engine allowed mask from configfs + * @pdev: pci device + * + * Return: engine mask with allowed engines set in configfs + */ + u64 xe_configfs_get_engines_allowed(struct pci_dev *pdev) + { + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + u64 engines_allowed; +=20 + if (!dev) + return device_defaults.engines_allowed; +=20 + scoped_guard(mutex, &dev->lock) + engines_allowed =3D dev->config.engines_allowed; + config_group_put(&dev->group); +=20 + return engines_allowed; + } +=20 + static ssize_t engines_allowed_show(struct config_item *item, char *page) + { + struct xe_config_device *dev =3D to_xe_config_device(item); + char *p =3D page; +=20 + for (size_t i =3D 0; i < ARRAY_SIZE(engine_info); i++) { + u64 mask =3D engine_info[i].mask; +=20 + if ((dev->engines_allowed & mask) =3D=3D mask) { + p +=3D sprintf(p, "%s*\n", engine_info[i].cls); + } else if (mask & dev->engines_allowed) { + u16 bit0 =3D __ffs64(mask), bit; +=20 + mask &=3D dev->engines_allowed; +=20 + for_each_set_bit(bit, (const unsigned long *)&mask, 64) + p +=3D sprintf(p, "%s%u\n", engine_info[i].cls, + bit - bit0); + } + } +=20 + return p - page; + } +=20 + /* + * Lookup engine_info. If @mask is not NULL, reduce the mask according to= the + * instance in @pattern. + * + * Examples of inputs: + * - lookup_engine_info("rcs0", &mask): return "rcs" entry from @engine_i= nfo and + * mask =3D=3D BIT_ULL(XE_HW_ENGINE_RCS0) + * - lookup_engine_info("rcs*", &mask): return "rcs" entry from @engine_i= nfo and + * mask =3D=3D XE_HW_ENGINE_RCS_MASK + * - lookup_engine_info("rcs", NULL): return "rcs" entry from @engine_info + */ + static const struct engine_info *lookup_engine_info(const char *pattern, = u64 *mask) + { + for (size_t i =3D 0; i < ARRAY_SIZE(engine_info); i++) { + u8 instance; + u16 bit; +=20 + if (!str_has_prefix(pattern, engine_info[i].cls)) + continue; +=20 + pattern +=3D strlen(engine_info[i].cls); + if (!mask) + return *pattern ? NULL : &engine_info[i]; +=20 + if (!strcmp(pattern, "*")) { + *mask =3D engine_info[i].mask; + return &engine_info[i]; + } +=20 + if (kstrtou8(pattern, 10, &instance)) + return NULL; +=20 + bit =3D __ffs64(engine_info[i].mask) + instance; + if (bit >=3D fls64(engine_info[i].mask)) + return NULL; +=20 + *mask =3D BIT_ULL(bit); + return &engine_info[i]; + } +=20 + return NULL; + } +=20 + static int parse_engine(const char *s, const char *end_chars, u64 *mask, + const struct engine_info **pinfo) + { + char buf[MAX_ENGINE_CLASS_CHARS + MAX_ENGINE_INSTANCE_CHARS + 1]; + const struct engine_info *info; + size_t len; +=20 + len =3D strcspn(s, end_chars); + if (len >=3D sizeof(buf)) + return -EINVAL; +=20 + memcpy(buf, s, len); + buf[len] =3D '\0'; +=20 + info =3D lookup_engine_info(buf, mask); + if (!info) + return -ENOENT; +=20 + if (pinfo) + *pinfo =3D info; +=20 + return len; + } +=20 + static ssize_t engines_allowed_store(struct config_item *item, const char= *page, + size_t len) + { + struct xe_config_group_device *dev =3D to_xe_config_group_device(item); + ssize_t patternlen, p; + u64 mask, val =3D 0; +=20 + for (p =3D 0; p < len; p +=3D patternlen + 1) { + patternlen =3D parse_engine(page + p, ",\n", &mask, NULL); + if (patternlen < 0) + return -EINVAL; +=20 + val |=3D mask; + } +=20 + guard(mutex)(&dev->lock); + if (is_bound(dev)) + return -EBUSY; +=20 + dev->config.engines_allowed =3D val; +=20 + return len; + } +=20 + static u64 get_gt_types_allowed(struct pci_dev *pdev) + { + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + u64 mask; +=20 + if (!dev) + return device_defaults.gt_types_allowed; +=20 + scoped_guard(mutex, &dev->lock) + mask =3D dev->config.gt_types_allowed; + config_group_put(&dev->group); +=20 + return mask; + } +=20 + /** + * xe_configfs_primary_gt_allowed - determine whether primary GTs are sup= ported + * @pdev: pci device + * + * Return: True if primary GTs are enabled, false if they have been disab= led via + * configfs. + */ + bool xe_configfs_primary_gt_allowed(struct pci_dev *pdev) + { + return get_gt_types_allowed(pdev) & BIT_ULL(XE_GT_TYPE_MAIN); + } +=20 + /** + * xe_configfs_media_gt_allowed - determine whether media GTs are support= ed + * @pdev: pci device + * + * Return: True if the media GTs are enabled, false if they have been dis= abled + * via configfs. + */ + bool xe_configfs_media_gt_allowed(struct pci_dev *pdev) + { + return get_gt_types_allowed(pdev) & BIT_ULL(XE_GT_TYPE_MEDIA); + } +=20 + static ssize_t gt_types_allowed_show(struct config_item *item, char *page) + { + struct xe_config_device *dev =3D to_xe_config_device(item); + char *p =3D page; +=20 + for (size_t i =3D 0; i < ARRAY_SIZE(gt_types); i++) + if (dev->gt_types_allowed & BIT_ULL(gt_types[i].type)) + p +=3D sprintf(p, "%s\n", gt_types[i].name); +=20 + return p - page; + } +=20 + static ssize_t gt_types_allowed_store(struct config_item *item, const cha= r *page, + size_t len) + { + struct xe_config_group_device *dev =3D to_xe_config_group_device(item); + char *buf __free(kfree) =3D kstrdup(page, GFP_KERNEL); + char *p =3D buf; + u64 typemask =3D 0; +=20 + if (!buf) + return -ENOMEM; +=20 + while (p) { + char *typename =3D strsep(&p, ",\n"); + bool matched =3D false; +=20 + if (typename[0] =3D=3D '\0') + continue; +=20 + for (size_t i =3D 0; i < ARRAY_SIZE(gt_types); i++) { + if (strcmp(typename, gt_types[i].name) =3D=3D 0) { + typemask |=3D BIT(gt_types[i].type); + matched =3D true; + break; + } + } +=20 + if (!matched) + return -EINVAL; + } +=20 + guard(mutex)(&dev->lock); + if (is_bound(dev)) + return -EBUSY; +=20 + dev->config.gt_types_allowed =3D typemask; +=20 + return len; + } +=20 + /** + * xe_configfs_get_migrate_ulls_period_ms - get configfs migrate_ulls_per= iod_ms setting + * @pdev: pci device + * + * Return: Migrate ULLS period in milliseconds (zero is disabled). + */ + u32 xe_configfs_get_migrate_ulls_period_ms(struct pci_dev *pdev) + { + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + u32 ret; +=20 + if (!dev) + return device_defaults.migrate_ulls_period_ms; +=20 + scoped_guard(mutex, &dev->lock) + ret =3D dev->config.migrate_ulls_period_ms; + config_group_put(&dev->group); +=20 + return ret; + } +=20 + static ssize_t migrate_ulls_period_ms_show(struct config_item *item, char= *page) + { + struct xe_config_device *dev =3D to_xe_config_device(item); +=20 + return sprintf(page, "%u\n", dev->migrate_ulls_period_ms); + } +=20 + static ssize_t migrate_ulls_period_ms_store(struct config_item *item, + const char *page, size_t len) + { + struct xe_config_group_device *dev =3D to_xe_config_group_device(item); + u32 val; + int ret; +=20 + ret =3D kstrtou32(page, 0, &val); + if (ret) + return ret; +=20 + guard(mutex)(&dev->lock); + if (is_bound(dev)) + return -EBUSY; +=20 + dev->config.migrate_ulls_period_ms =3D val; +=20 + return len; + } +=20 + /** + * xe_configfs_get_survivability_mode - get configfs survivability mode a= ttribute + * @pdev: pci device + * + * Return: survivability_mode attribute in configfs + */ + u8 xe_configfs_get_survivability_mode(struct pci_dev *pdev) + { + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + u8 mode; +=20 + if (!dev) + return device_defaults.survivability_mode; +=20 + scoped_guard(mutex, &dev->lock) + mode =3D dev->config.survivability_mode; + config_group_put(&dev->group); +=20 + return mode; + } +=20 + static ssize_t survivability_mode_show(struct config_item *item, char *pa= ge) + { + struct xe_config_device *dev =3D to_xe_config_device(item); +=20 + return sprintf(page, "%u\n", dev->survivability_mode); + } +=20 + static ssize_t survivability_mode_store(struct config_item *item, const c= har *page, size_t len) + { + struct xe_config_group_device *dev =3D to_xe_config_group_device(item); + u8 survivability_mode; + int ret; +=20 + ret =3D kstrtou8(page, 0, &survivability_mode); + if (ret) + return ret; +=20 + if (survivability_mode & ~XE_CONFIGFS_SURVIVABILITY_MASK) + return -EINVAL; +=20 + guard(mutex)(&dev->lock); + if (is_bound(dev)) + return -EBUSY; +=20 + dev->config.survivability_mode =3D survivability_mode; +=20 + return len; + } +=20 + /** + * xe_configfs_get_enable_multi_queue - get configfs enable_multi_queue s= etting + * @pdev: pci device + * + * Return: true if multi-queue is enabled for this device (the default), + * false if it has been force-disabled via configfs. + */ + bool xe_configfs_get_enable_multi_queue(struct pci_dev *pdev) + { + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + bool ret; +=20 + if (!dev) + return true; +=20 + scoped_guard(mutex, &dev->lock) + ret =3D dev->config.enable_multi_queue; + config_group_put(&dev->group); +=20 + return ret; + } +=20 + static ssize_t enable_multi_queue_show(struct config_item *item, char *pa= ge) + { + struct xe_config_device *dev =3D to_xe_config_device(item); +=20 + return sprintf(page, "%d\n", dev->enable_multi_queue); + } +=20 + static ssize_t enable_multi_queue_store(struct config_item *item, const c= har *page, + size_t len) + { + struct xe_config_group_device *dev =3D to_xe_config_group_device(item); + bool val; + int ret; +=20 + ret =3D kstrtobool(page, &val); + if (ret) + return ret; +=20 + guard(mutex)(&dev->lock); + if (is_bound(dev)) + return -EBUSY; +=20 + dev->config.enable_multi_queue =3D val; +=20 + return len; + } +=20 + /** + * xe_configfs_get_psmi_enabled - get configfs enable_psmi setting + * @pdev: pci device + * + * Return: enable_psmi setting in configfs + */ + bool xe_configfs_get_psmi_enabled(struct pci_dev *pdev) + { + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + bool ret; +=20 + if (!dev) + return false; +=20 + scoped_guard(mutex, &dev->lock) + ret =3D dev->config.enable_psmi; + config_group_put(&dev->group); +=20 + return ret; + } +=20 + static ssize_t enable_psmi_show(struct config_item *item, char *page) + { + struct xe_config_device *dev =3D to_xe_config_device(item); +=20 + return sprintf(page, "%d\n", dev->enable_psmi); + } +=20 + static ssize_t enable_psmi_store(struct config_item *item, const char *pa= ge, size_t len) + { + struct xe_config_group_device *dev =3D to_xe_config_group_device(item); + bool val; + int ret; +=20 + ret =3D kstrtobool(page, &val); + if (ret) + return ret; +=20 + guard(mutex)(&dev->lock); + if (is_bound(dev)) + return -EBUSY; +=20 + dev->config.enable_psmi =3D val; +=20 + return len; + } +=20 + /** + * xe_configfs_get_enable_vram_page_offline - get configfs enable_vram_pa= ge_offline setting + * @pdev: pci device + * + * Return: enable_vram_page_offline setting in configfs + */ + bool xe_configfs_get_enable_vram_page_offline(struct pci_dev *pdev) + { + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + bool ret; +=20 + if (!dev) + return device_defaults.enable_vram_page_offline; +=20 + scoped_guard(mutex, &dev->lock) + ret =3D dev->config.enable_vram_page_offline; + config_group_put(&dev->group); +=20 + return ret; + } +=20 + static ssize_t enable_vram_page_offline_show(struct config_item *item, ch= ar *page) + { + struct xe_config_device *dev =3D to_xe_config_device(item); +=20 + return sprintf(page, "%s\n", str_yes_no(dev->enable_vram_page_offline)); + } +=20 + static ssize_t enable_vram_page_offline_store(struct config_item *item, + const char *page, size_t len) + { + struct xe_config_group_device *dev =3D to_xe_config_group_device(item); + bool val; + int ret; +=20 + ret =3D kstrtobool(page, &val); + if (ret) + return ret; +=20 + guard(mutex)(&dev->lock); + if (is_bound(dev)) + return -EBUSY; +=20 + dev->config.enable_vram_page_offline =3D val; +=20 + return len; + } +=20 + /** + * xe_configfs_get_bad_page_decay_secs - get configfs bad_page_decay_secs= setting + * @pdev: pci device + * + * Return: bad_page_decay_secs setting in configfs + */ + u32 xe_configfs_get_bad_page_decay_secs(struct pci_dev *pdev) + { + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + u32 ret; +=20 + if (!dev) + return device_defaults.bad_page_decay_secs; +=20 + scoped_guard(mutex, &dev->lock) + ret =3D dev->config.bad_page_decay_secs; + config_group_put(&dev->group); +=20 + return ret; + } +=20 + static ssize_t bad_page_decay_secs_show(struct config_item *item, char *p= age) + { + struct xe_config_device *dev =3D to_xe_config_device(item); +=20 + return sprintf(page, "%u\n", dev->bad_page_decay_secs); + } +=20 + static ssize_t bad_page_decay_secs_store(struct config_item *item, + const char *page, size_t len) + { + struct xe_config_group_device *dev =3D to_xe_config_group_device(item); + u32 val; + int ret; +=20 + ret =3D kstrtou32(page, 0, &val); + if (ret) + return ret; +=20 + if (val > MAX_JIFFY_OFFSET / HZ) + return -EINVAL; +=20 + guard(mutex)(&dev->lock); + if (is_bound(dev)) + return -EBUSY; +=20 + dev->config.bad_page_decay_secs =3D val; +=20 + return len; + } +=20 + bool xe_configfs_get_force_ocode_load(struct pci_dev *pdev) + { + struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); + bool force_load; +=20 + if (!dev || !IS_ENABLED(CONFIG_DRM_XE_DEBUG)) { + if (dev) + config_group_put(&dev->group); + return false; + } +=20 + scoped_guard(mutex, &dev->lock) + force_load =3D dev->config.force_ocode_load; + config_group_put(&dev->group); +=20 + return force_load; + } +=20 + static ssize_t force_ocode_load_show(struct config_item *item, char *page) + { + struct xe_config_device *dev =3D to_xe_config_device(item); +=20 + return sprintf(page, "%d\n", dev->force_ocode_load); + } +=20 + static ssize_t force_ocode_load_store(struct config_item *item, const cha= r *page, size_t len) + { + struct xe_config_group_device *dev =3D to_xe_config_group_device(item); + bool force_load; + int ret; +=20 + ret =3D kstrtobool(page, &force_load); + if (ret) + return ret; +=20 + guard(mutex)(&dev->lock); + if (is_bound(dev)) + return -EBUSY; +=20 + dev->config.force_ocode_load =3D force_load; +=20 + return len; + } +=20 CONFIGFS_ATTR(, ctx_restore_mid_bb); CONFIGFS_ATTR(, ctx_restore_post_bb); - CONFIGFS_ATTR(, enable_multi_queue); - CONFIGFS_ATTR(, migrate_ulls_period_ms); - CONFIGFS_ATTR(, enable_psmi); - CONFIGFS_ATTR(, disable_vram_page_offline); CONFIGFS_ATTR(, engines_allowed); CONFIGFS_ATTR(, gt_types_allowed); + CONFIGFS_ATTR(, migrate_ulls_period_ms); CONFIGFS_ATTR(, survivability_mode); + CONFIGFS_ATTR(, enable_multi_queue); + CONFIGFS_ATTR(, enable_psmi); + CONFIGFS_ATTR(, enable_vram_page_offline); + CONFIGFS_ATTR(, bad_page_decay_secs); + CONFIGFS_ATTR(, force_ocode_load); =20 static struct configfs_attribute *xe_config_device_attrs[] =3D { &attr_ctx_restore_mid_bb, &attr_ctx_restore_post_bb, - &attr_enable_multi_queue, - &attr_migrate_ulls_period_ms, - &attr_enable_psmi, - &attr_disable_vram_page_offline, &attr_engines_allowed, &attr_gt_types_allowed, + &attr_migrate_ulls_period_ms, &attr_survivability_mode, + &attr_enable_multi_queue, + &attr_enable_psmi, + &attr_enable_vram_page_offline, + &attr_bad_page_decay_secs, + &attr_force_ocode_load, NULL, }; =20 @@@ -984,12 -1323,15 +1323,15 @@@ static bool xe_config_device_is_visible { struct xe_config_group_device *dev =3D to_xe_config_group_device(item); =20 + if (attr =3D=3D &attr_force_ocode_load && !IS_ENABLED(CONFIG_DRM_XE_DEBU= G)) + return false; +=20 if (attr =3D=3D &attr_survivability_mode) { if (!dev->desc->is_dgfx || dev->desc->platform < XE_BATTLEMAGE) return false; } =20 - if (attr =3D=3D &attr_disable_vram_page_offline) { + if (attr =3D=3D &attr_enable_vram_page_offline || attr =3D=3D &attr_bad_= page_decay_secs) { if (!dev->desc->is_dgfx || dev->desc->platform !=3D XE_CRESCENTISLAND) return false; } @@@ -1190,6 -1532,10 +1532,10 @@@ static struct config_group *xe_config_m config_group_init_type_name(&dev->sriov, "sriov", &xe_config_sriov_type= ); configfs_add_default_group(&dev->sriov, &dev->group); } + #if IS_ENABLED(CONFIG_DRM_XE_DEBUG) + config_group_init_type_name(&dev->debug, "debug", &xe_configfs_debug_typ= e); + configfs_add_default_group(&dev->debug, &dev->group); + #endif =20 mutex_init(&dev->lock); =20 @@@ -1237,13 -1583,15 +1583,15 @@@ static void dump_custom_dev_config(stru dev->config.attr_); \ } while (0) =20 - PRI_CUSTOM_ATTR("%llx", gt_types_allowed); PRI_CUSTOM_ATTR("%llx", engines_allowed); + PRI_CUSTOM_ATTR("%llx", gt_types_allowed); PRI_CUSTOM_ATTR("%u", migrate_ulls_period_ms); + PRI_CUSTOM_ATTR("%u", survivability_mode); PRI_CUSTOM_ATTR("%d", enable_multi_queue); PRI_CUSTOM_ATTR("%d", enable_psmi); - PRI_CUSTOM_ATTR("%d", disable_vram_page_offline); - PRI_CUSTOM_ATTR("%d", survivability_mode); + PRI_CUSTOM_ATTR("%d", enable_vram_page_offline); + PRI_CUSTOM_ATTR("%u", bad_page_decay_secs); + PRI_CUSTOM_ATTR("%d", force_ocode_load); PRI_CUSTOM_ATTR("%u", sriov.admin_only_pf); =20 #undef PRI_CUSTOM_ATTR @@@ -1272,217 -1620,6 +1620,6 @@@ void xe_configfs_check_device(struct pc config_group_put(&dev->group); } =20 - /** - * xe_configfs_get_survivability_mode - get configfs survivability mode a= ttribute - * @pdev: pci device - * - * Return: survivability_mode attribute in configfs - */ - bool xe_configfs_get_survivability_mode(struct pci_dev *pdev) - { - struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); - bool mode; -=20 - if (!dev) - return device_defaults.survivability_mode; -=20 - mode =3D dev->config.survivability_mode; - config_group_put(&dev->group); -=20 - return mode; - } -=20 - static u64 get_gt_types_allowed(struct pci_dev *pdev) - { - struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); - u64 mask; -=20 - if (!dev) - return device_defaults.gt_types_allowed; -=20 - mask =3D dev->config.gt_types_allowed; - config_group_put(&dev->group); -=20 - return mask; - } -=20 - /** - * xe_configfs_primary_gt_allowed - determine whether primary GTs are sup= ported - * @pdev: pci device - * - * Return: True if primary GTs are enabled, false if they have been disab= led via - * configfs. - */ - bool xe_configfs_primary_gt_allowed(struct pci_dev *pdev) - { - return get_gt_types_allowed(pdev) & BIT_ULL(XE_GT_TYPE_MAIN); - } -=20 - /** - * xe_configfs_media_gt_allowed - determine whether media GTs are support= ed - * @pdev: pci device - * - * Return: True if the media GTs are enabled, false if they have been dis= abled - * via configfs. - */ - bool xe_configfs_media_gt_allowed(struct pci_dev *pdev) - { - return get_gt_types_allowed(pdev) & BIT_ULL(XE_GT_TYPE_MEDIA); - } -=20 - /** - * xe_configfs_get_engines_allowed - get engine allowed mask from configfs - * @pdev: pci device - * - * Return: engine mask with allowed engines set in configfs - */ - u64 xe_configfs_get_engines_allowed(struct pci_dev *pdev) - { - struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); - u64 engines_allowed; -=20 - if (!dev) - return device_defaults.engines_allowed; -=20 - engines_allowed =3D dev->config.engines_allowed; - config_group_put(&dev->group); -=20 - return engines_allowed; - } -=20 - /** - * xe_configfs_get_psmi_enabled - get configfs enable_psmi setting - * @pdev: pci device - * - * Return: enable_psmi setting in configfs - */ - bool xe_configfs_get_psmi_enabled(struct pci_dev *pdev) - { - struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); - bool ret; -=20 - if (!dev) - return false; -=20 - ret =3D dev->config.enable_psmi; - config_group_put(&dev->group); -=20 - return ret; - } -=20 - /** - * xe_configfs_get_enable_multi_queue - get configfs enable_multi_queue s= etting - * @pdev: pci device - * - * Return: true if multi-queue is enabled for this device (the default), - * false if it has been force-disabled via configfs. - */ - bool xe_configfs_get_enable_multi_queue(struct pci_dev *pdev) - { - struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); - bool ret; -=20 - if (!dev) - return true; -=20 - ret =3D dev->config.enable_multi_queue; - config_group_put(&dev->group); -=20 - return ret; - } -=20 - /** - * xe_configfs_get_migrate_ulls_period_ms - get configfs migrate_ulls_per= iod_ms setting - * @pdev: pci device - * - * Return: Migrate ULLS period in milliseconds (zero is disabled). - */ - u32 xe_configfs_get_migrate_ulls_period_ms(struct pci_dev *pdev) - { - struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); - u32 ret; -=20 - if (!dev) - return device_defaults.migrate_ulls_period_ms; -=20 - ret =3D dev->config.migrate_ulls_period_ms; - config_group_put(&dev->group); -=20 - return ret; - } -=20 - /** - * xe_configfs_get_disable_vram_page_offline - get configfs disable_vram_= page_offline setting - * @pdev: pci device - * - * Return: disable_vram_page_offline setting in configfs - */ - bool xe_configfs_get_disable_vram_page_offline(struct pci_dev *pdev) - { - struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); - bool ret; -=20 - if (!dev) - return device_defaults.disable_vram_page_offline; -=20 - ret =3D dev->config.disable_vram_page_offline; - config_group_put(&dev->group); -=20 - return ret; - } -=20 - /** - * xe_configfs_get_ctx_restore_mid_bb - get configfs ctx_restore_mid_bb s= etting - * @pdev: pci device - * @class: hw engine class - * @cs: pointer to the bb to use - only valid during probe - * - * Return: Number of dwords used in the mid_ctx_restore setting in config= fs - */ - u32 xe_configfs_get_ctx_restore_mid_bb(struct pci_dev *pdev, - enum xe_engine_class class, - const u32 **cs) - { - struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); - u32 len; -=20 - if (!dev) - return 0; -=20 - if (cs) - *cs =3D dev->config.ctx_restore_mid_bb[class].cs; -=20 - len =3D dev->config.ctx_restore_mid_bb[class].len; - config_group_put(&dev->group); -=20 - return len; - } -=20 - /** - * xe_configfs_get_ctx_restore_post_bb - get configfs ctx_restore_post_bb= setting - * @pdev: pci device - * @class: hw engine class - * @cs: pointer to the bb to use - only valid during probe - * - * Return: Number of dwords used in the post_ctx_restore setting in confi= gfs - */ - u32 xe_configfs_get_ctx_restore_post_bb(struct pci_dev *pdev, - enum xe_engine_class class, - const u32 **cs) - { - struct xe_config_group_device *dev =3D find_xe_config_group_device(pdev); - u32 len; -=20 - if (!dev) - return 0; -=20 - *cs =3D dev->config.ctx_restore_post_bb[class].cs; - len =3D dev->config.ctx_restore_post_bb[class].len; - config_group_put(&dev->group); -=20 - return len; - } -=20 #ifdef CONFIG_PCI_IOV /** * xe_configfs_admin_only_pf() - Get PF's operational mode. diff --combined drivers/gpu/drm/xe/xe_configfs.h index d726800de26ab,a55eeb3def171..0000000000000 --- a/drivers/gpu/drm/xe/xe_configfs.h +++ b/drivers/gpu/drm/xe/xe_configfs.h @@@ -5,32 -5,47 +5,47 @@@ #ifndef _XE_CONFIGFS_H_ #define _XE_CONFIGFS_H_ =20 + #include #include #include =20 + #include "xe_configfs_types.h" #include "xe_defaults.h" #include "xe_hw_engine_types.h" #include "xe_module.h" =20 struct pci_dev; =20 + /** + * DOC: survivability_mode configfs bits + * + * The survivability_mode attribute is a bitmap so that additional + * survivability behaviors can be requested independently in the future. + * %XE_CONFIGFS_SURVIVABILITY_ENABLE is currently the only defined bit, so + * the attribute still only accepts 0 and 1. + */ + #define XE_CONFIGFS_SURVIVABILITY_ENABLE BIT(0) + #define XE_CONFIGFS_SURVIVABILITY_MASK XE_CONFIGFS_SURVIVABILITY_ENABLE +=20 int xe_configfs_init(void); void xe_configfs_exit(void); void xe_configfs_check_device(struct pci_dev *pdev); - bool xe_configfs_get_survivability_mode(struct pci_dev *pdev); + ssize_t xe_configfs_get_ctx_restore_mid_bb(struct pci_dev *pdev, + enum xe_engine_class class, + u32 *cs, size_t max_len); + ssize_t xe_configfs_get_ctx_restore_post_bb(struct pci_dev *pdev, + enum xe_engine_class class, + u32 *cs, size_t max_len); + u64 xe_configfs_get_engines_allowed(struct pci_dev *pdev); bool xe_configfs_primary_gt_allowed(struct pci_dev *pdev); bool xe_configfs_media_gt_allowed(struct pci_dev *pdev); - u64 xe_configfs_get_engines_allowed(struct pci_dev *pdev); - bool xe_configfs_get_psmi_enabled(struct pci_dev *pdev); - bool xe_configfs_get_enable_multi_queue(struct pci_dev *pdev); u32 xe_configfs_get_migrate_ulls_period_ms(struct pci_dev *pdev); - bool xe_configfs_get_disable_vram_page_offline(struct pci_dev *pdev); - u32 xe_configfs_get_ctx_restore_mid_bb(struct pci_dev *pdev, - enum xe_engine_class class, - const u32 **cs); - u32 xe_configfs_get_ctx_restore_post_bb(struct pci_dev *pdev, - enum xe_engine_class class, - const u32 **cs); + u8 xe_configfs_get_survivability_mode(struct pci_dev *pdev); + bool xe_configfs_get_enable_multi_queue(struct pci_dev *pdev); + bool xe_configfs_get_psmi_enabled(struct pci_dev *pdev); + bool xe_configfs_get_enable_vram_page_offline(struct pci_dev *pdev); + u32 xe_configfs_get_bad_page_decay_secs(struct pci_dev *pdev); + bool xe_configfs_get_force_ocode_load(struct pci_dev *pdev); #ifdef CONFIG_PCI_IOV unsigned int xe_configfs_get_max_vfs(struct pci_dev *pdev); bool xe_configfs_admin_only_pf(struct pci_dev *pdev); diff --combined drivers/gpu/drm/xe/xe_device.c index 7f655f824db54,30710e3aff2a8..0000000000000 --- a/drivers/gpu/drm/xe/xe_device.c +++ b/drivers/gpu/drm/xe/xe_device.c @@@ -8,7 -8,9 +8,9 @@@ #include #include #include + #include #include + #include =20 #include #include @@@ -69,6 -71,7 +71,7 @@@ #include "xe_soc_remapper.h" #include "xe_survivability_mode.h" #include "xe_sriov.h" + #include "xe_sriov_vf_ccs.h" #include "xe_svm.h" #include "xe_sysctrl.h" #include "xe_tile.h" @@@ -81,6 -84,7 +84,7 @@@ #include "xe_vsec.h" #include "xe_wait_user_fence.h" #include "xe_wa.h" + #include "xe_wedge.h" =20 #include #include @@@ -125,6 -129,8 +129,8 @@@ static int xe_file_open(struct drm_devi put_task_struct(task); } =20 + xe_debugfs_client_add(file); +=20 return 0; } =20 @@@ -238,7 -244,7 +244,7 @@@ static long xe_drm_ioctl(struct file *f struct xe_device *xe =3D to_xe_device(file_priv->minor->dev); long ret; =20 - if (xe_device_wedged(xe)) + if (xe_wedged(xe)) return -ECANCELED; =20 ACQUIRE(xe_pm_runtime_ioctl, pm)(xe); @@@ -256,7 -262,7 +262,7 @@@ static long xe_drm_compat_ioctl(struct=20 struct xe_device *xe =3D to_xe_device(file_priv->minor->dev); long ret; =20 - if (xe_device_wedged(xe)) + if (xe_wedged(xe)) return -ECANCELED; =20 ACQUIRE(xe_pm_runtime_ioctl, pm)(xe); @@@ -373,6 -379,9 +379,9 @@@ bool xe_device_is_admin_only(const stru } #endif =20 + /* Number of allocated struct xe_device */ + static atomic_t xe_device_count; +=20 static void xe_device_destroy(struct drm_device *dev, void *dummy) { struct xe_device *xe =3D to_xe_device(dev); @@@ -382,6 -391,9 +391,9 @@@ if (xe->preempt_fence_wq) destroy_workqueue(xe->preempt_fence_wq); =20 + if (xe->rebind_wq) + destroy_workqueue(xe->rebind_wq); +=20 if (xe->ordered_wq) destroy_workqueue(xe->ordered_wq); =20 @@@ -392,6 -404,9 +404,9 @@@ destroy_workqueue(xe->destroy_wq); =20 ttm_device_fini(&xe->ttm); +=20 + if (atomic_dec_and_test(&xe_device_count)) + wake_up_var(&xe_device_count); } =20 /** @@@ -429,6 -444,10 +444,10 @@@ struct xe_device *xe_device_create(stru if (err) return ERR_PTR(err); =20 + xe->userptr_repin_timeout_ms =3D num_online_nodes() > 1 ? + XE_USERPTR_REPIN_TIMEOUT_MS_NUMA_DEFAULT : + XE_USERPTR_REPIN_TIMEOUT_MS_DEFAULT; +=20 return xe; } ALLOW_ERROR_INJECTION(xe_device_create, ERRNO); /* See xe_pci_probe() */ @@@ -462,6 -481,7 +481,7 @@@ int xe_device_init_early(struct xe_devi return err; =20 xe_bo_dev_init(&xe->bo_device); + atomic_inc(&xe_device_count); err =3D drmm_add_action_or_reset(&xe->drm, xe_device_destroy, NULL); if (err) return err; @@@ -507,10 -527,11 +527,11 @@@ =20 xe->preempt_fence_wq =3D alloc_ordered_workqueue("xe-preempt-fence-wq", WQ_MEM_RECLAIM); + xe->rebind_wq =3D alloc_workqueue("xe-rebind-wq", WQ_UNBOUND, 0); xe->ordered_wq =3D alloc_ordered_workqueue("xe-ordered-wq", 0); xe->unordered_wq =3D alloc_workqueue("xe-unordered-wq", WQ_PERCPU, 0); xe->destroy_wq =3D alloc_workqueue("xe-destroy-wq", WQ_PERCPU | WQ_MEM_R= ECLAIM, 0); - if (!xe->ordered_wq || !xe->unordered_wq || + if (!xe->ordered_wq || !xe->unordered_wq || !xe->rebind_wq || !xe->preempt_fence_wq || !xe->destroy_wq) { /* * Cleanup done in xe_device_destroy via @@@ -668,7 -689,7 +689,7 @@@ static void vf_update_device_info(struc xe->info.has_heci_cscfi =3D 0; xe->info.has_heci_gscfi =3D 0; xe->info.has_i2c =3D 0; - xe->info.has_late_bind =3D 0; + xe->info.late_bind_mask =3D 0; xe->info.skip_guc_pc =3D 1; xe->info.skip_pcode =3D 1; xe->info.has_drm_ras =3D false; @@@ -690,16 -711,6 +711,6 @@@ static int xe_device_vram_alloc(struct=20 return 0; } =20 - static void wedged_work(struct work_struct *work); - static void xe_device_wedged_fini(struct drm_device *drm, void *arg); -=20 - static int xe_device_wedged_init(struct xe_device *xe) - { - INIT_WORK(&xe->wedged.work, wedged_work); -=20 - return drmm_add_action_or_reset(&xe->drm, xe_device_wedged_fini, xe); - } -=20 /** * xe_device_probe_early: Device early probe * @xe: xe device instance @@@ -714,9 -725,6 +725,6 @@@ int xe_device_probe_early(struct xe_dev { int err; =20 - xe_wa_device_init(xe); - xe_wa_process_device_oob(xe); -=20 err =3D xe_mmio_probe_early(xe); if (err) return err; @@@ -759,16 -767,11 +767,11 @@@ */ assert_lmem_ready(xe); =20 - xe->wedged.mode =3D xe_device_validate_wedged_mode(xe, xe_modparam.wedge= d_mode) ? - XE_DEFAULT_WEDGED_MODE : xe_modparam.wedged_mode; - drm_dbg(&xe->drm, "wedged_mode: setting mode (%u) %s\n", - xe->wedged.mode, xe_wedged_mode_to_string(xe->wedged.mode)); -=20 err =3D xe_device_vram_alloc(xe); if (err) return err; =20 - err =3D xe_device_wedged_init(xe); + err =3D xe_wedge_init(xe); if (err) return err; =20 @@@ -776,6 -779,25 +779,25 @@@ } ALLOW_ERROR_INJECTION(xe_device_probe_early, ERRNO); /* See xe_pci_probe(= ) */ =20 + static void override_has_cached_pt(struct xe_device *xe) + { + struct xe_gt *gt; + u8 id; +=20 + for_each_gt(gt, xe, id) { + /* + * Wa_16029380221: The affected GT will always use + * non-coherent access to page tables, so we must do + * uncached writes from the CPU. + */ + if (XE_GT_WA(gt, 16029380221)) + xe->info.has_cached_pt =3D false; +=20 + if (XE_GT_WA(gt, 14026539277)) + xe->info.has_cached_pt =3D false; + } + } +=20 static int probe_has_flat_ccs(struct xe_device *xe) { struct xe_gt *gt; @@@ -858,14 -880,6 +880,6 @@@ static void detect_preproduction_hw(str } } =20 - static void xe_device_wedged_fini(struct drm_device *drm, void *arg) - { - struct xe_device *xe =3D arg; -=20 - if (atomic_read(&xe->wedged.flag)) - xe_pm_runtime_put(xe); - } -=20 #ifdef CONFIG_DRM_XE_DEBUG_PAGE_SIZE static int xe_debug_page_size_alloc_ctrl_init(struct xe_device *xe) { @@@ -893,6 -907,15 +907,15 @@@ static void xe_uid_probe(struct xe_devi xe->device_uid =3D xe_mmio_read64_2x32(xe_root_tile_mmio(xe), CRI_DEVIC= E_UID); } =20 + static void xe_device_force_wake_stop(struct xe_device *xe) + { + struct xe_gt *gt; + u8 id; +=20 + for_each_gt(gt, xe, id) + xe_force_wake_stop(gt_to_fw(gt)); + } +=20 int xe_device_probe(struct xe_device *xe) { struct xe_tile *tile; @@@ -926,16 -949,13 +949,13 @@@ return err; } =20 - /* - * Wa_16029380221: The affected GT will always use non-coherent - * access to page tables, so we must do uncached writes from the - * CPU. - */ - for_each_gt(gt, xe, id) - if (XE_GT_WA(gt, 16029380221)) - xe->info.has_cached_pt =3D false; + override_has_cached_pt(xe); =20 for_each_tile(tile, xe, id) { + err =3D xe_tile_wopcm_init(tile); + if (err) + return err; +=20 err =3D xe_ggtt_init_early(tile->mem.ggtt); if (err) return err; @@@ -983,7 -1003,9 +1003,9 @@@ if (err) return err; =20 - xe_ras_init(xe); + err =3D xe_ras_init(xe); + if (err) + return err; =20 /* * Now that GT is initialized (TTM in particular), @@@ -1069,6 -1091,9 +1091,9 @@@ if (err) return err; =20 + for_each_gt(gt, xe, id) + xe_gt_sanitize_freq(gt); +=20 xe_display_register(xe); =20 err =3D xe_oa_register(xe); @@@ -1083,34 -1108,24 +1108,24 @@@ if (err) goto err_unregister_display; =20 + xe_vsec_init(xe); +=20 xe_debugfs_register(xe); =20 err =3D xe_hwmon_register(xe); if (err) - goto err_unregister_display; + goto err_unregister_vsec; =20 err =3D xe_i2c_probe(xe); if (err) goto err_unregister_hwmon; =20 - for_each_gt(gt, xe, id) - xe_gt_sanitize_freq(gt); -=20 - xe_vsec_init(xe); -=20 err =3D xe_sriov_init_late(xe); if (err) goto err_unregister_hwmon; =20 detect_preproduction_hw(xe); =20 - /* - * Process and log any errors detected by hardware. Possible results can - * include declaring the device as wedged, which must be done only after - * xe_device_wedged_fini() is registered. - */ - xe_ras_process_errors(xe); -=20 err =3D devm_add_action_or_reset(xe->drm.dev, xe_device_sanitize, xe); if (err) goto err_unregister_hwmon; @@@ -1119,9 -1134,12 +1134,12 @@@ =20 err_unregister_hwmon: xe_hwmon_unregister(xe); + err_unregister_vsec: + xe_vsec_remove(xe); err_unregister_display: xe_display_unregister(xe); drm_dev_unplug(&xe->drm); + xe_device_force_wake_stop(xe); =20 return err; } @@@ -1129,9 -1147,11 +1147,11 @@@ void xe_device_remove(struct xe_device *xe) { xe_hwmon_unregister(xe); + xe_vsec_remove(xe); xe_display_unregister(xe); =20 drm_dev_unplug(&xe->drm); + xe_device_force_wake_stop(xe); =20 xe_bo_pci_dev_remove_all(xe); } @@@ -1143,6 -1163,8 +1163,8 @@@ void xe_device_shutdown(struct xe_devic =20 drm_dbg(&xe->drm, "Shutting down device\n"); =20 + xe_hwmon_unregister(xe); +=20 xe_display_shutdown(xe); =20 xe_irq_suspend(xe); @@@ -1349,153 -1371,6 +1371,6 @@@ u64 xe_device_uncanonicalize_addr(struc return address & GENMASK_ULL(xe->info.va_bits - 1, 0); } =20 - /** - * DOC: Xe Device Wedging - * - * Xe driver uses drm device wedged uevent as documented in Documentation= /gpu/drm-uapi.rst. - * When device is in wedged state, every IOCTL will be blocked and GT can= not - * be used. The conditions under which the driver declares the device wed= ged - * depend on the wedged mode configuration (see &enum xe_wedged_mode). The - * default recovery method for a wedged state is rebind/bus-reset. - * - * Another recovery method is vendor-specific. Below are the cases that s= end - * ``WEDGED=3Dvendor-specific`` recovery method in drm device wedged ueve= nt. - * - * Case: Firmware Flash - * -------------------- - * - * Identification Hint - * +++++++++++++++++++ - * - * ``WEDGED=3Dvendor-specific`` drm device wedged uevent with - * :ref:`Runtime Survivability mode ` is used to n= otify - * admin/userspace consumer about the need for a firmware flash. - * - * Recovery Procedure - * ++++++++++++++++++ - * - * Once ``WEDGED=3Dvendor-specific`` drm device wedged uevent is received= , follow - * the below steps - * - * - Check Runtime Survivability mode sysfs. - * If enabled, firmware flash is required to recover the device. - * - * /sys/bus/pci/devices//survivability_mode - * - * - Admin/userspace consumer can use firmware flashing tools like fwupd = to flash - * firmware and restore device to normal operation. - */ -=20 - static void wedged_work(struct work_struct *work) - { - struct xe_device *xe =3D container_of(work, struct xe_device, wedged.wor= k); -=20 - /* Notify userspace of wedged device */ - drm_dev_wedged_event(&xe->drm, xe->wedged.method, NULL); - } -=20 - #define WEDGED_URL "https://docs.kernel.org/gpu/drm-uapi.html#device-wedg= ing" - #define XE_BUG_URL "https://gitlab.freedesktop.org/drm/xe/kernel/issues/n= ew" -=20 - /** - * xe_device_declare_wedged - Declare device wedged - * @xe: xe device instance - * @method: method(s) to be used for recovery - * - * This is a final state that can only be cleared with the recovery method - * specified in the drm wedged uevent. - * - * In this state every IOCTL will be blocked so the GT cannot be used. - * In general it will be called upon any critical error such as gt reset - * failure or guc loading failure. Userspace will be notified of this sta= te - * through device wedged uevent. - * If xe.wedged module parameter is set to 2, this function will be called - * on every single execution timeout (a.k.a. GPU hang) right after devcor= edump - * snapshot capture. In this mode, GT reset won't be attempted so the sta= te of - * the issue is preserved for further debugging. - */ - void xe_device_declare_wedged(struct xe_device *xe, unsigned long method) - { - struct xe_gt *gt; - u8 id; -=20 - if (xe->wedged.mode =3D=3D XE_WEDGED_MODE_NEVER) { - drm_dbg(&xe->drm, "Wedged mode is forcibly disabled\n"); - return; - } -=20 - if (!atomic_xchg(&xe->wedged.flag, 1)) { - xe->needs_flr_on_fini =3D true; - xe_pm_runtime_get_noresume(xe); -=20 - xe_log_err_fatal(xe, WEDGED, -EIO, "Device declared wedged!\n"); - xe_err_once(xe, "IOCTLs and executions are now blocked!\n" - "For recovery procedure, refer to %s\n" - "Please file a _new_ bug report at %s\n", - WEDGED_URL, XE_BUG_URL); -=20 - for_each_initialized_gt(gt, xe, id) - xe_gt_declare_wedged(gt); -=20 - /* - * XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET is intended for debugging - * hangs, so wedge the device with 'none' recovery method and have - * it available to the user for debugging. - */ - if (xe->wedged.mode =3D=3D XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) - xe->wedged.method =3D DRM_WEDGE_RECOVERY_NONE; - else - xe->wedged.method =3D method; -=20 - schedule_work(&xe->wedged.work); - } - } -=20 - /** - * xe_device_validate_wedged_mode - Check if given mode is supported - * @xe: the &xe_device - * @mode: requested mode to validate - * - * Check whether the provided wedged mode is supported. - * - * Return: 0 if mode is supported, error code otherwise. - */ - int xe_device_validate_wedged_mode(struct xe_device *xe, unsigned int mod= e) - { - if (mode > XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) { - drm_dbg(&xe->drm, "wedged_mode: invalid value (%u)\n", mode); - return -EINVAL; - } else if (mode =3D=3D XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET && (IS_SRIO= V_VF(xe) || - (IS_SRIOV_PF(xe) && !IS_ENABLED(CONFIG_DRM_XE_DEBUG)))) { - drm_dbg(&xe->drm, "wedged_mode: (%u) %s mode is not supported for %s\n", - mode, xe_wedged_mode_to_string(mode), - xe_sriov_mode_to_string(xe_device_sriov_mode(xe))); - return -EPERM; - } -=20 - return 0; - } -=20 - /** - * xe_wedged_mode_to_string - Convert enum value to string. - * @mode: the &xe_wedged_mode to convert - * - * Returns: wedged mode as a user friendly string. - */ - const char *xe_wedged_mode_to_string(enum xe_wedged_mode mode) - { - switch (mode) { - case XE_WEDGED_MODE_NEVER: - return "never"; - case XE_WEDGED_MODE_UPON_CRITICAL_ERROR: - return "upon-critical-error"; - case XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET: - return "upon-any-hang-no-reset"; - default: - return ""; - } - } -=20 /** * xe_device_asid_to_vm() - Find VM from ASID * @xe: the &xe_device @@@ -1520,3 -1395,189 +1395,189 @@@ struct xe_vm *xe_device_asid_to_vm(stru =20 return vm; } +=20 + /** + * xe_device_exit() - Device subsystem exit function. + * + * Exit function to be called at module unload time. + */ + void xe_device_exit(void) + { + /* + * Wait for all devices to be freed. 20s is well above the typical + * maximum dma_fence signalling time, so warn and keep waiting if + * we're still not done by then, since it may indicate a leaked + * xe_device reference is stalling module unload. + */ + if (!wait_var_event_timeout(&xe_device_count, + !atomic_read(&xe_device_count), + HZ * 20)) { + pr_warn("%s: Waiting for %d xe device(s) to be freed before unloading.\= n", + DRIVER_NAME, atomic_read(&xe_device_count)); + wait_var_event(&xe_device_count, !atomic_read(&xe_device_count)); + } + } +=20 + /** + * xe_device_suspend() - Common helper for system suspend and FLR prepare + * @xe: xe device instance + * @hibernate: true if preparing for S4 hibernation, false for < S4 suspe= nd + * @flr: indicate FLR prepare + * + * Return: 0 on success, negative error code otherwise. + */ + int xe_device_suspend(struct xe_device *xe, bool hibernate, bool flr) + { + struct xe_gt *gt; + u8 id; + int err; +=20 + err =3D xe_pxp_pm_suspend(xe->pxp); + if (err) + return err; +=20 + xe_late_bind_pm_suspend(&xe->late_bind); +=20 + xe_hwmon_suspend(xe, false); +=20 + for_each_gt(gt, xe, id) + xe_gt_suspend_prepare(gt); +=20 + if (flr) { + for_each_gt(gt, xe, id) + xe_gt_flr_prepare(gt); + } else { + xe_display_pm_suspend(xe); +=20 + if (xe_pm_needs_bo_evict(xe, hibernate)) { + /* FIXME: Super racey... */ + err =3D xe_bo_evict_all(xe); + if (err) + goto err_display; + } +=20 + for_each_gt(gt, xe, id) { + err =3D xe_gt_suspend(gt); + if (err) + goto err_display; + } + } +=20 + for_each_gt(gt, xe, id) + xe_force_wake_assert_settled(gt_to_fw(gt)); +=20 + xe_irq_suspend(xe); +=20 + if (flr) { + unmap_mapping_range(xe->drm.anon_inode->i_mapping, 0, 0, 1); + } else { + xe_display_pm_suspend_late(xe); +=20 + xe_i2c_pm_suspend(xe); + } +=20 + return 0; +=20 + err_display: + xe_display_pm_resume(xe); + xe_pxp_pm_resume(xe->pxp); + xe_hwmon_suspend_abort(xe); + return err; + } +=20 + /** + * xe_device_resume() - Common helper for system resume and re-initializa= tion post FLR + * @xe: xe device instance + * @flr: indicate re-initialization post FLR + * + * Return: 0 on success, negative error code otherwise. + */ + int xe_device_resume(struct xe_device *xe, bool flr) + { + struct xe_tile *tile; + struct xe_gt *gt; + u8 id; + int err; +=20 + err =3D xe_pcode_ready(xe, true); + if (err) + return err; +=20 + assert_lmem_ready(xe); +=20 + for_each_tile(tile, xe, id) + xe_wa_apply_tile_workarounds(tile); +=20 + if (flr) { + err =3D xe_bo_restore_map(xe); + if (err) + return err; +=20 + for_each_gt(gt, xe, id) { + err =3D xe_gt_reinit(gt); + if (err) + return err; + } +=20 + err =3D xe_cpu_bind_reinit(xe); + if (err) + return err; + } +=20 + for_each_gt(gt, xe, id) + xe_gt_resume_early(gt); +=20 + if (!flr) { + xe_display_pm_resume_early(xe); +=20 + /* + * This only restores pinned memory which is the memory required for the + * GT(s) to resume. + */ + err =3D xe_bo_restore_early(xe); + if (err) + return err; + } +=20 + for_each_tile(tile, xe, id) + xe_ggtt_resume(tile->mem.ggtt); +=20 + xe_i2c_pm_resume(xe, true); +=20 + xe_sysctrl_pm_resume(xe); +=20 + xe_irq_resume(xe); +=20 + for_each_gt(gt, xe, id) { + err =3D xe_gt_resume(gt); + if (err) + break; + } +=20 + if (flr) { + if (err) + return err; + } else { + /* + * Try to bring up display before bailing from GT resume failure, + * so we don't leave the user clueless with a blank screen. + */ + xe_display_pm_resume(xe); + if (err) + return err; +=20 + err =3D xe_bo_restore_late(xe); + if (err) + return err; +=20 + if (IS_VF_CCS_READY(xe)) + xe_sriov_vf_ccs_register_context(xe); + } +=20 + xe_pxp_pm_resume(xe->pxp); +=20 + xe_late_bind_pm_resume(&xe->late_bind); + xe_hwmon_resume(xe, false); +=20 + return 0; + } diff --combined drivers/gpu/drm/xe/xe_hwmon.c index 4708fc13b2e7e,5416eba404a6a..0000000000000 --- a/drivers/gpu/drm/xe/xe_hwmon.c +++ b/drivers/gpu/drm/xe/xe_hwmon.c @@@ -6,8 -6,10 +6,10 @@@ #include #include #include + #include #include #include + #include =20 #include #include "regs/xe_gt_regs.h" @@@ -15,10 -17,12 +17,12 @@@ #include "regs/xe_pcode_regs.h" #include "xe_device.h" #include "xe_hwmon.h" + #include "xe_late_bind_fw_types.h" #include "xe_log.h" #include "xe_mmio.h" #include "xe_pcode.h" #include "xe_pcode_api.h" + #include "xe_printk.h" #include "xe_sriov.h" #include "xe_pm.h" #include "xe_vsec.h" @@@ -40,7 -44,11 +44,11 @@@ enum xe_hwmon_reg_operation=20 REG_READ64, }; =20 - #define MAX_VRAM_CHANNELS (16) + #define MAX_VRAM_CHANNELS 80 + #define FIXED_VRAM_CHANNELS 16 +=20 + /* Each MSU enable bit maps to one VRAM subsystem of 4 channels. */ + #define VRAM_CHANNELS_PER_MSU 4 =20 enum xe_hwmon_channel { CHANNEL_CARD, @@@ -49,6 -57,7 +57,7 @@@ CHANNEL_MCTRL, CHANNEL_PCIE, CHANNEL_VRAM_N, + /* Compile-time upper bound; actual channel count is hwmon->temp.vram_co= unt */ CHANNEL_VRAM_N_MAX =3D CHANNEL_VRAM_N + MAX_VRAM_CHANNELS - 1, CHANNEL_MAX, }; @@@ -102,8 -111,15 +111,15 @@@ enum sensor_attr_power=20 =20 #define PWR_ATTR_TO_STR(attr) (((attr) =3D=3D hwmon_power_max) ? "PL1" : = "PL2") =20 - /* Index of memory controller in READ_THERMAL_DATA output */ + /* Sensor byte indices in READ_THERMAL_DATA output for BMG. */ #define TEMP_INDEX_MCTRL 2 + #define TEMP_INDEX_PCIE 10 + #define BMG_G21_TEMP_INDEX_PCIE 8 +=20 + /* Sensor byte indices in READ_THERMAL_DATA output for CRI. */ + #define CRI_TEMP_INDEX_MCTRL 16 + #define CRI_TEMP_MCTRL_COUNT 4 + #define CRI_TEMP_INDEX_PCIE 20 =20 /* Mailbox sentinel reported for an unavailable temperature sensor */ #define TEMP_MBX_INVALID 0xFF @@@ -126,13 -142,183 +142,183 @@@ struct xe_hwmon_energy_info=20 }; =20 /** - * struct xe_hwmon_fan_info - to cache previous fan reading + * DOC: Xe fan control + * + * Xe fan control is exposed through the hwmon pwm and fan interfaces whe= n the + * platform advertises PCODE-backed fan management support. + * + * Attribute visibility is determined during hwmon registration. ``fanN_m= ax`` + * requires maximum fan speed data, while ``pwmN``, ``pwmN_enable`` and t= he + * ``pwmN_auto_pointM_*`` curve files require a readable stock fan table = with at + * least two points. Late binding refreshes cached data for existing fans= but + * does not create attributes that were hidden at registration. The stock= table + * can change with the late bound firmware; if a complete table cannot be= read, + * the previous one is kept. + * + * The implementation tracks two fan tables per fan: + * + * - A stock table read from firmware through PCODE. + * - A user table cached in the driver and programmed back through PCODE. + * + * Late-bound fan firmware refreshes the stock table and maximum fan spee= d. + * Active manual and full-speed user tables are re-programmed after the r= efresh, + * while the cached user curve is preserved. Provisioning is polled after= hwmon + * registration and transitions that may reload the firmware. D3cold, sys= tem + * suspend, and FLR mark the fan data refresh as pending; D3hot runtime s= uspend + * keeps firmware and fan state and does not restart provisioning or rest= ore + * fan tables. TjMax fan control is independently saved and restored acro= ss + * state-loss transitions. A failed suspend restarts provisioning polling= if a + * reload might be pending. + * + * The pwmN_enable sysfs value selects how those tables are used: + * + * - 0 (``XE_FAN_PWM_FULL_SPEED``) programs the user table at 100 percent= duty + * cycle as a full-speed override. The cached user curve is kept. + * - 1 (``XE_FAN_PWM_MANUAL_USER_TABLE``) enables the user table for manu= al + * control. Without a cached user curve, it starts from the stock table. + * - 2 (``XE_FAN_PWM_AUTO_STOCK_TABLE``) restores automatic control using= the + * stock firmware table. The cached user curve is kept for a later swit= ch to + * mode 1. + * + * Any table programmed by modes 0 and 1 must meet the curve rules below; + * otherwise the mode switch fails with -EINVAL. + * + * The fan-control sysfs files are: + * + * - ``fanN_input`` reports the measured fan speed in RPM. + * - ``fanN_max`` reports the maximum supported fan speed in RPM. + * - ``pwmN`` reports the active PWM duty cycle in the hwmon 0..255 range. + * Writes set a fixed duty cycle in manual mode. + * - ``pwmN_enable`` selects the mode described above. + * - ``pwmN_auto_pointM_temp`` reports the temperature of point ``M`` of = the + * stock table in mode 2 or of the user table otherwise, in millidegrees + * Celsius. Writes stage the user-table point. + * - ``pwmN_auto_pointM_pwm`` reports the PWM value of point ``M`` of the= same + * table in the hwmon 0..255 range. Writes stage the user-table point. + * + * Writes to ``pwmN``, ``pwmN_auto_pointM_temp`` and ``pwmN_auto_pointM_p= wm`` + * are accepted only while ``pwmN_enable`` is 1; otherwise they fail with + * -EINVAL. Firmware stores duty cycle in percent, so PWM values read back + * rounded to the nearest percent step. Nonzero curve PWM writes are kept= at + * least 1 percent so rounding cannot turn them into an end marker. + * + * Writes to ``pwmN_auto_pointM_temp`` and ``pwmN_auto_pointM_pwm`` are s= taged + * in the driver, so intermediate writes never reach firmware as a partial + * curve. Each write fills the selected point, and the staged curve is co= mmitted + * once a complete point prefix ends with (0, 0) or reaches the last supp= orted + * point. The (0, 0) end marker is not included in the committed curve. + * A curve must have at least two points, temperature must increase and P= WM + * must not decrease from one point to the next; otherwise the completing= write + * fails with -EINVAL. Staged temperature and PWM values are cleared afte= r the + * completing write, whether firmware accepts or rejects the curve. + * Firmware acceptance publishes the staged curve in the cached user tabl= e; + * rejection keeps the previously cached user curve. + * + * Writes to ``pwmN`` replace the user table with a two-point table at a + * constant duty cycle. + * + * If a user-table write is not accepted by firmware, the fan falls back = to + * mode 2 and the previously cached user curve is kept. If that fallback = also + * fails, the fan control mode is not established and reads of ``pwmN``, + * ``pwmN_enable`` and ``pwmN_auto_pointM_*`` fail with -ENODATA while the + * control mode is uninitialized. + */ +=20 + /** + * enum xe_fan_pwm_enable_mode - fan PWM enable modes + */ + enum xe_fan_pwm_enable_mode { + /** @XE_FAN_PWM_FULL_SPEED: run the fan at full speed */ + XE_FAN_PWM_FULL_SPEED =3D 0, + /** @XE_FAN_PWM_MANUAL_USER_TABLE: user table control */ + XE_FAN_PWM_MANUAL_USER_TABLE =3D 1, + /** @XE_FAN_PWM_AUTO_STOCK_TABLE: firmware default behavior using the st= ock table */ + XE_FAN_PWM_AUTO_STOCK_TABLE =3D 2, + /** @XE_FAN_PWM_UNINITIALIZED: fan control mode is not established */ + XE_FAN_PWM_UNINITIALIZED =3D 0xff, + }; +=20 + /** + * enum xe_tjmax_fan_control_state - saved TjMax fan-control state + */ + enum xe_tjmax_fan_control_state { + /** @XE_TJMAX_FAN_CONTROL_UNKNOWN: no state to restore */ + XE_TJMAX_FAN_CONTROL_UNKNOWN, + /** @XE_TJMAX_FAN_CONTROL_DISABLED: state was disabled before suspend */ + XE_TJMAX_FAN_CONTROL_DISABLED, + /** @XE_TJMAX_FAN_CONTROL_ENABLED: state was enabled before suspend */ + XE_TJMAX_FAN_CONTROL_ENABLED, + }; +=20 + /** + * enum fan_table_type - fan table types tracked per fan + */ + enum fan_table_type { + /** @STOCK_FAN_TABLE: firmware-provided fan table */ + STOCK_FAN_TABLE, + /** @USER_FAN_TABLE: user fan table written through sysfs */ + USER_FAN_TABLE, + /** @FAN_TABLE_MAX: number of fan tables tracked per fan */ + FAN_TABLE_MAX, + }; +=20 + /* Check if a fan is enabled based on the fan_enable_mask got from FSC_RE= AD_NUM_FANS. */ + #define IS_FAN_ENABLED(hwmon, fan) ((hwmon)->fan_enable_mask & BIT(fan)) +=20 + /* Maximum number of fan control points supported by each fan. */ + #define MAX_FAN_CONTROL_POINTS 10 +=20 + #define XE_FAN_WORKER_POLL_INTERVAL msecs_to_jiffies(1000) + #define XE_FAN_WORKER_POLL_RETRIES 30 +=20 + /* Minimum number of points required for a user fan table. */ + #define MIN_FAN_CONTROL_POINTS 2 +=20 + /* pwmN_auto_pointM_temp and pwmN_auto_pointM_pwm for every curve point o= f a fan. */ + #define XE_CURVE_ATTRS_PER_FAN (2 * MAX_FAN_CONTROL_POINTS) + #define XE_CURVE_ATTRS (FAN_MAX * XE_CURVE_ATTRS_PER_FAN) + #define XE_CURVE_ATTR_NAME_LEN sizeof("pwmN_auto_pointNN_temp") +=20 + /* Fan control point index: bit31 selects fan table type; bits30:0 contro= l point index. */ + #define FCP_INDEX(table_type, point_num) (((u32)(table_type) << 31) | \ + ((u32)(point_num) & GENMASK(30, 0))) +=20 + /** + * struct xe_hwmon_fan_info - cached fan telemetry and control state + * + * Each fan keeps the latest tachometer sampling state along with two fan + * tables: the stock table discovered from firmware and the user table ma= naged + * by hwmon sysfs writes. */ struct xe_hwmon_fan_info { /** @reg_val_prev: previous fan reg val */ u32 reg_val_prev; /** @time_prev: previous timestamp */ u64 time_prev; + /** @fan_table: fan control tables */ + struct fan_table { + /** @fan_table.point_count: number of fan control points */ + u8 point_count; + /** @fan_table.fcp: array of fan control points */ + struct fan_control_point { + /** @fan_table.fcp.temp: temperature in degree celsius */ + u8 temp; + /** @fan_table.fcp.speed: fan speed in percentage */ + u8 speed; + } fcp[MAX_FAN_CONTROL_POINTS]; + /** @fan_table.staged_fcp: curve supplied via sysfs but not yet committ= ed */ + struct fan_control_point staged_fcp[MAX_FAN_CONTROL_POINTS]; + /** @fan_table.temp_staged: bitmask of temperature fields supplied via = sysfs */ + u16 temp_staged; + /** @fan_table.speed_staged: bitmask of speed fields supplied via sysfs= */ + u16 speed_staged; + } fan_table[FAN_TABLE_MAX]; + /** @max_rps: maximum fan RPS */ + u32 max_rps; + /** @pwm_enable_mode: current xe_fan_pwm_enable_mode */ + u8 pwm_enable_mode; + /** @restore_user_table: restore user fan table on next resume */ + bool restore_user_table; }; =20 /** @@@ -145,16 -331,22 +331,22 @@@ struct xe_hwmon_thermal_info=20 /** @data: temperature limits as little-endian dwords */ __le32 data[DIV_ROUND_UP(TEMP_LIMIT_MAX, sizeof(u32))]; }; - /** @count: no of temperature sensors available for the platform */ + /** @count: temperature sensors count from READ_THERMAL_CONFIG */ u8 count; + /** @vram_count: exclusive upper bound for VRAM temperature channel indi= ces */ + u8 vram_count; + /** @available: temperature sensor availability cached at registration */ + bool available[CHANNEL_MAX]; + /** @msu_mask: VRAM subsystem enable mask read from MMIO */ + u32 msu_mask; union { /** @value: per-sensor raw mailbox temperature; bit7=3Dsign, bits6:0=3D= magnitude */ u8 value[U8_MAX + 1]; /** @dword: sensor values as little-endian dwords, u32-aligned for pcod= e reads */ __le32 dword[DIV_ROUND_UP(U8_MAX + 1, sizeof(u32))]; }; - /** @vram_label: vram label names */ - char vram_label[MAX_VRAM_CHANNELS][MAX_LABEL_SIZE]; + /** @vram_label: vram label names, dynamically allocated based on vram_c= ount */ + char (*vram_label)[MAX_LABEL_SIZE]; }; =20 /** @@@ -165,6 -357,8 +357,8 @@@ struct xe_hwmon=20 struct device *hwmon_dev; /** @xe: Xe device */ struct xe_device *xe; + /** @registration_lock: serialize registration with late binding updates= */ + struct mutex registration_lock; /** @hwmon_lock: lock for rw attributes*/ struct mutex hwmon_lock; /** @scl_shift_power: pkg power unit */ @@@ -175,6 -369,8 +369,8 @@@ int scl_shift_time; /** @ei: Energy info for energyN_input */ struct xe_hwmon_energy_info ei[CHANNEL_MAX]; + /** @fan_enable_mask: bitmask of available fans */ + u32 fan_enable_mask; /** @fi: Fan info for fanN_input */ struct xe_hwmon_fan_info fi[FAN_MAX]; /** @read_power_limit_from_pcode: pcode accepted power limits write for = the channel */ @@@ -185,6 -381,24 +381,24 @@@ u32 pl2_on_boot[CHANNEL_MAX]; /** @temp: Temperature info */ struct xe_hwmon_thermal_info temp; + /** @tjmax_fan_control_state: saved TjMax fan-control state */ + enum xe_tjmax_fan_control_state tjmax_fan_control_state; + /** @fan_post_lb_work: polls for fan firmware provisioning */ + struct delayed_work fan_post_lb_work; + /** @fan_worker_poll_count: polls since the fan firmware check was queue= d */ + u8 fan_worker_poll_count; + /** @fan_refresh_pending: fan firmware may have been reloaded */ + bool fan_refresh_pending; + /** @curve_sda: fan curve sensor attributes */ + struct sensor_device_attribute curve_sda[XE_CURVE_ATTRS]; + /** @curve_names: names of the fan curve sensor attributes */ + char curve_names[XE_CURVE_ATTRS][XE_CURVE_ATTR_NAME_LEN]; + /** @curve_attrs: NULL-terminated list of fan curve attributes */ + struct attribute *curve_attrs[XE_CURVE_ATTRS + 1]; + /** @curve_group: fan curve attribute group */ + struct attribute_group curve_group; + /** @groups: NULL-terminated list of extra attribute groups */ + const struct attribute_group *groups[3]; }; =20 static inline int prepare_power_limit_param2(const struct xe_hwmon *hwmon= , int channel) @@@ -297,6 -511,14 +511,14 @@@ static int xe_hwmon_pcode_set_default_p return ret; } =20 + static bool xe_hwmon_vram_channel_enabled(const struct xe_hwmon *hwmon, i= nt index) + { + if (hwmon->xe->info.platform =3D=3D XE_CRESCENTISLAND) + return hwmon->temp.msu_mask & BIT(index / VRAM_CHANNELS_PER_MSU); +=20 + return true; + } +=20 static struct xe_reg xe_hwmon_get_reg(struct xe_hwmon *hwmon, enum xe_hwm= on_reg hwmon_reg, int channel) { @@@ -304,12 -526,21 +526,21 @@@ =20 switch (hwmon_reg) { case REG_TEMP: - if (xe->info.platform =3D=3D XE_BATTLEMAGE || xe->info.platform =3D=3D = XE_CRESCENTISLAND) { + if (xe->info.platform =3D=3D XE_CRESCENTISLAND) { + if (channel =3D=3D CHANNEL_PKG) + return CRI_PACKAGE_TEMPERATURE; + else if (channel =3D=3D CHANNEL_VRAM) + return CRI_VRAM_TEMPERATURE; + else if (in_range(channel, CHANNEL_VRAM_N, hwmon->temp.vram_count) && + xe_hwmon_vram_channel_enabled(hwmon, channel - CHANNEL_VRAM_N)) + return CRI_VRAM_TEMPERATURE_N(channel - CHANNEL_VRAM_N); + } else if (xe->info.platform =3D=3D XE_BATTLEMAGE) { if (channel =3D=3D CHANNEL_PKG) return BMG_PACKAGE_TEMPERATURE; else if (channel =3D=3D CHANNEL_VRAM) return BMG_VRAM_TEMPERATURE; - else if (in_range(channel, CHANNEL_VRAM_N, MAX_VRAM_CHANNELS)) + else if (in_range(channel, CHANNEL_VRAM_N, hwmon->temp.vram_count) && + xe_hwmon_vram_channel_enabled(hwmon, channel - CHANNEL_VRAM_N)) return BMG_VRAM_TEMPERATURE_N(channel - CHANNEL_VRAM_N); } else if (xe->info.platform =3D=3D XE_DG2) { if (channel =3D=3D CHANNEL_PKG) @@@ -567,9 -798,15 +798,15 @@@ xe_hwmon_energy_get(struct xe_hwmon *hw =20 if (hwmon->xe->info.platform =3D=3D XE_BATTLEMAGE) { u64 pmt_val; + u32 guid; =20 - ret =3D xe_pmt_telem_read(hwmon->xe->drm.dev, - xe_mmio_read32(mmio, PUNIT_TELEMETRY_GUID), + ret =3D xe_vsec_get_guid(hwmon->xe, &guid); + if (ret) { + drm_warn(&hwmon->xe->drm, "PMT device is not available: %d\n", ret); + *energy =3D 0; + return ret; + } + ret =3D xe_pmt_telem_read(hwmon->xe->drm.dev, guid, &pmt_val, BMG_ENERGY_STATUS_PMT_OFFSET, sizeof(pmt_val)); if (ret !=3D sizeof(pmt_val)) { drm_warn(&hwmon->xe->drm, "energy read from pmt failed, ret %d\n", ret= ); @@@ -806,51 -1043,67 +1043,67 @@@ static const struct attribute_group hwm .is_visible =3D xe_hwmon_attributes_visible, }; =20 - static const struct attribute_group *hwmon_groups[] =3D { - &hwmon_attrgroup, - NULL + static const u32 hwmon_temp_config[CHANNEL_MAX + 1] =3D { + [CHANNEL_CARD] =3D HWMON_T_LABEL, + [CHANNEL_PKG] =3D HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | + HWMON_T_LABEL | HWMON_T_MAX, + [CHANNEL_VRAM ... CHANNEL_PCIE] =3D HWMON_T_CRIT | HWMON_T_EMERGENCY | + HWMON_T_INPUT | HWMON_T_LABEL, + [CHANNEL_VRAM_N ... CHANNEL_VRAM_N_MAX] =3D HWMON_T_CRIT | HWMON_T_EMERG= ENCY | + HWMON_T_INPUT | HWMON_T_LABEL, + [CHANNEL_MAX] =3D 0, /* hwmon config terminator */ + }; +=20 + static const struct hwmon_channel_info hwmon_temp_info =3D { + .type =3D hwmon_temp, + .config =3D hwmon_temp_config, }; =20 static const struct hwmon_channel_info * const hwmon_info[] =3D { - HWMON_CHANNEL_INFO(temp, - HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL | - HWMON_T_MAX, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL, - HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL), + &hwmon_temp_info, HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_LABE= L | HWMON_P_CRIT | - HWMON_P_CAP, - HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_LABEL | HWMON_P_CAP), + HWMON_P_CAP | HWMON_P_CAP_MAX, + HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_LABEL | HWMON_P_CAP | + HWMON_P_CAP_MAX), HWMON_CHANNEL_INFO(curr, HWMON_C_LABEL, HWMON_C_CRIT | HWMON_C_LABEL), HWMON_CHANNEL_INFO(in, HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HW= MON_I_LABEL), HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT | HWMON_E_LABEL, HWMON_E_INPUT = | HWMON_E_LABEL), - HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT), + HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_MAX, HWMON_F_INPUT | HWM= ON_F_MAX, + HWMON_F_INPUT | HWMON_F_MAX), + HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE, + HWMON_PWM_INPUT | HWMON_PWM_ENABLE, + HWMON_PWM_INPUT | HWMON_PWM_ENABLE), NULL }; =20 static int xe_hwmon_pcode_read_thermal_info(struct xe_hwmon *hwmon) { struct xe_tile *root_tile =3D xe_device_get_root_tile(hwmon->xe); + struct xe_mmio *mmio =3D xe_root_tile_mmio(hwmon->xe); u32 config =3D 0, data0 =3D 0, data1 =3D 0; int ret; =20 + /* + * Only CRI reports dynamic VRAM channel state. Fixed-channel platforms + * set the count up front so those sensors stay visible even if the ther= mal + * mailbox reads below fail. + */ + if (hwmon->xe->info.has_fixed_vram_channels) + hwmon->temp.vram_count =3D FIXED_VRAM_CHANNELS; +=20 + if (hwmon->xe->info.platform =3D=3D XE_CRESCENTISLAND) { + hwmon->temp.msu_mask =3D xe_mmio_read32(mmio, CRI_MSU_VRAM_ENABLE); + hwmon->temp.vram_count =3D fls(hwmon->temp.msu_mask) * VRAM_CHANNELS_PE= R_MSU; + drm_dbg(&hwmon->xe->drm, "MSU VRAM enable mask 0x%x, VRAM channel bound= %d\n", + hwmon->temp.msu_mask, hwmon->temp.vram_count); + if (hwmon->temp.vram_count > MAX_VRAM_CHANNELS) { + drm_warn(&hwmon->xe->drm, + "VRAM channel bound %d exceeds max %d, clamping\n", + hwmon->temp.vram_count, MAX_VRAM_CHANNELS); + hwmon->temp.vram_count =3D MAX_VRAM_CHANNELS; + } + } +=20 ret =3D xe_pcode_read(root_tile, PCODE_MBOX(PCODE_THERMAL_INFO, READ_THE= RMAL_LIMITS, 0), &data0, &data1); if (ret) @@@ -903,17 -1156,43 +1156,43 @@@ static int mbx_temp_decode(u8 value return (value & TEMP_MBX_SIGN) ? -temp : temp; } =20 + static u8 xe_hwmon_pcie_temp_index(const struct xe_hwmon *hwmon) + { + if (hwmon->xe->info.platform =3D=3D XE_CRESCENTISLAND) + return CRI_TEMP_INDEX_PCIE; +=20 + if (hwmon->xe->info.subplatform =3D=3D XE_SUBPLATFORM_BATTLEMAGE_G21) + return BMG_G21_TEMP_INDEX_PCIE; +=20 + return TEMP_INDEX_PCIE; + } +=20 static int get_mc_temp(struct xe_hwmon *hwmon, long *val) { struct xe_tile *root_tile =3D xe_device_get_root_tile(hwmon->xe); - int ret, i, count =3D 0; + int ret, i, start, end, count =3D 0; s32 average =3D 0; + u32 data =3D 0; =20 guard(mutex)(&hwmon->hwmon_lock); =20 - for (i =3D 0; i < DIV_ROUND_UP(hwmon->temp.count, sizeof(u32)); i++) { - u32 data =3D 0; + /* CRI relocated the memory controller temperatures to READ_THERMAL_DATA= bytes 16..19. */ + if (hwmon->xe->info.platform =3D=3D XE_CRESCENTISLAND) { + start =3D CRI_TEMP_INDEX_MCTRL; + end =3D CRI_TEMP_INDEX_MCTRL + CRI_TEMP_MCTRL_COUNT; + } else { + start =3D TEMP_INDEX_MCTRL; + end =3D min_t(int, hwmon->temp.count - 1, xe_hwmon_pcie_temp_index(hwmo= n)); + } =20 + if (end <=3D start || end > hwmon->temp.count) { + drm_warn(&hwmon->xe->drm, + "memory controller range [%d, %d) is invalid for temperature count %d= \n", + start, end, hwmon->temp.count); + return -ENODATA; + } +=20 + for (i =3D start / sizeof(u32); i < DIV_ROUND_UP(end, sizeof(u32)); i++)= { ret =3D xe_pcode_read(root_tile, PCODE_MBOX(PCODE_THERMAL_INFO, READ_TH= ERMAL_DATA, i), &data, NULL); if (ret) @@@ -922,7 -1201,7 +1201,7 @@@ hwmon->temp.dword[i] =3D cpu_to_le32(data); } =20 - for (i =3D TEMP_INDEX_MCTRL; i < hwmon->temp.count - 1; i++) { + for (i =3D start; i < end; i++) { if (!mbx_temp_valid(hwmon, hwmon->temp.value[i])) continue; average +=3D mbx_temp_decode(hwmon->temp.value[i]); @@@ -942,23 -1221,36 +1221,36 @@@ return 0; } =20 - static int get_pcie_temp(struct xe_hwmon *hwmon, long *val) + static int get_pcie_temp(const struct xe_hwmon *hwmon, long *val) { struct xe_tile *root_tile =3D xe_device_get_root_tile(hwmon->xe); u32 data =3D 0; + int index; int ret; =20 + index =3D xe_hwmon_pcie_temp_index(hwmon); + if (index >=3D hwmon->temp.count) { + drm_warn(&hwmon->xe->drm, + "PCIe temperature index %d out of range for temperature count %d\n", + index, hwmon->temp.count); + return -ENODATA; + } +=20 ret =3D xe_pcode_read(root_tile, PCODE_MBOX(PCODE_THERMAL_INFO, READ_THE= RMAL_DATA, - PCIE_SENSOR_GROUP_ID), &data, NULL); + index / sizeof(data)), + &data, NULL); if (ret) return ret; =20 - /* Sensor offset is different for G21 */ - if (hwmon->xe->info.subplatform !=3D XE_SUBPLATFORM_BATTLEMAGE_G21) - data =3D REG_FIELD_GET(PCIE_SENSOR_MASK, data); -=20 + data >>=3D (index % sizeof(data)) * BITS_PER_BYTE; data =3D REG_FIELD_GET(TEMP_MASK, data); - *val =3D mbx_temp_decode(data) * MILLIDEGREE_PER_DEGREE; + if (!mbx_temp_valid(hwmon, data)) { + drm_dbg(&hwmon->xe->drm, "PCIe temperature not available\n"); + return -ENXIO; + } +=20 + if (val) + *val =3D mbx_temp_decode(data) * MILLIDEGREE_PER_DEGREE; =20 return 0; } @@@ -986,17 -1278,654 +1278,654 @@@ static int xe_hwmon_pcode_write_i1(cons (uval & POWER_SETUP_I1_DATA_MASK)); } =20 - static int xe_hwmon_pcode_read_fan_control(const struct xe_hwmon *hwmon, = u32 subcmd, u32 *uval) + static int xe_hwmon_pcode_read_fan_control(const struct xe_hwmon *hwmon, = u32 subcmd, u8 fan_num, + u32 *uval) { struct xe_tile *root_tile =3D xe_device_get_root_tile(hwmon->xe); =20 + return xe_pcode_read(root_tile, PCODE_MBOX(FAN_SPEED_CONTROL, subcmd, fa= n_num), uval, NULL); + } +=20 + static int xe_hwmon_get_fan_enable_mask(struct xe_hwmon *hwmon) + { + int ret; +=20 /* Platforms that don't return correct value */ - if (hwmon->xe->info.platform =3D=3D XE_DG2 && subcmd =3D=3D FSC_READ_NUM= _FANS) { - *uval =3D 2; + if (hwmon->xe->info.platform =3D=3D XE_DG2) { + hwmon->fan_enable_mask =3D GENMASK(1, 0); return 0; } =20 - return xe_pcode_read(root_tile, PCODE_MBOX(FAN_SPEED_CONTROL, subcmd, 0)= , uval, NULL); + ret =3D xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_NUM_FANS, 0, + &hwmon->fan_enable_mask); + if (ret) { + hwmon->fan_enable_mask =3D 0; + xe_warn(hwmon->xe, "failed to read number of fans, ret=3D%d\n", ret); + return ret; + } +=20 + /* Check if fan mask exceeds FAN_MAX and log a warning if it does */ + if (hwmon->fan_enable_mask & ~GENMASK(FAN_MAX - 1, 0)) { + xe_warn(hwmon->xe, "Fan enable mask exceeds %d supported fans, clamping= \n", + FAN_MAX); + hwmon->fan_enable_mask &=3D GENMASK(FAN_MAX - 1, 0); + } +=20 + xe_info(hwmon->xe, "Number of fans detected: %u\n", hweight32(hwmon->fan= _enable_mask)); +=20 + return 0; + } +=20 + static int xe_hwmon_get_fan_point_count(struct xe_hwmon *hwmon, u8 fan, u= 32 *point_count, + enum fan_table_type table_type) + { + int ret; +=20 + *point_count =3D 0; + ret =3D xe_hwmon_pcode_read_fan_control(hwmon, + (table_type =3D=3D USER_FAN_TABLE) ? + FSC_READ_USER_FAN_CONTROL_POINTS : + FSC_READ_STOCK_FAN_CONTROL_POINTS, + fan, point_count); + if (ret) { + *point_count =3D 0; + xe_err(hwmon->xe, "failed to read fan %d %s point count, ret=3D%d\n", f= an, + (table_type =3D=3D USER_FAN_TABLE) ? "user" : "stock", ret); + return ret; + } +=20 + if (*point_count > MAX_FAN_CONTROL_POINTS) { + xe_err(hwmon->xe, "fan %d %s point count %u exceeds max %u\n", fan, + (table_type =3D=3D USER_FAN_TABLE) ? "user" : "stock", *point_co= unt, + MAX_FAN_CONTROL_POINTS); + return -EINVAL; + } +=20 + return 0; + } +=20 + /* + * Set the fan to automatic mode by clearing the user fan table. + * This will make the firmware run the stock fan table. + */ + static int xe_hwmon_fan_set_auto(struct xe_hwmon *hwmon, u8 fan) + { + struct xe_hwmon_fan_info *fi =3D &hwmon->fi[fan]; + int ret; +=20 + ret =3D xe_pcode_write(xe_device_get_root_tile(hwmon->xe), + PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_NUM_FAN_CONTROL_POINTS, f= an), + 0); + if (ret) + goto fail; +=20 + fi->pwm_enable_mode =3D XE_FAN_PWM_AUTO_STOCK_TABLE; + return 0; +=20 + fail: + fi->pwm_enable_mode =3D XE_FAN_PWM_UNINITIALIZED; + return ret; + } +=20 + static int xe_hwmon_commit_user_fan_table(struct xe_hwmon *hwmon, u8 fan, + const struct fan_control_point *points, u32 point_count) + { + /* The extra write is for the final write that sets the number of fan co= ntrol points */ + struct xe_pcode_write writes[MAX_FAN_CONTROL_POINTS + 1]; + u32 accepted_count =3D 0; + u32 point; + int ret, write_ret, verify_ret; +=20 + if (point_count < MIN_FAN_CONTROL_POINTS || + point_count > MAX_FAN_CONTROL_POINTS) { + xe_err(hwmon->xe, "fan %d table needs %d-%d points, got %u\n", fan, + MIN_FAN_CONTROL_POINTS, MAX_FAN_CONTROL_POINTS, point_count); + return -EINVAL; + } +=20 + for (point =3D 0; point < point_count; point++) { + u8 temp =3D points[point].temp; + u8 speed =3D points[point].speed; +=20 + /* Firmware requires increasing temperature and non-decreasing speed. */ + if (point > 0) { + u8 prev_temp =3D points[point - 1].temp; + u8 prev_speed =3D points[point - 1].speed; +=20 + if (temp <=3D prev_temp || speed < prev_speed) { + xe_dbg(hwmon->xe, + "fan %d point %u not monotonic: %u/%u%% after %u/%u%%\n", + fan, point, temp, speed, prev_temp, prev_speed); + return -EINVAL; + } + } +=20 + /* Prepare the write for this fan control point */ + writes[point].mbox =3D PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_FAN_TABL= E, + fan); + writes[point].data =3D FIELD_PREP(FAN_CONTROL_POINT_TEMP_MASK, temp) | + FIELD_PREP(FAN_CONTROL_POINT_SPEED_MASK, speed); + } +=20 + xe_dbg(hwmon->xe, "committing fan %d user table with %u points\n", fan, = point_count); +=20 + /* pcode fills the user table from point 0 in write order; the count wri= te commits it. */ + writes[point_count].mbox =3D PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_NUM= _FAN_CONTROL_POINTS, + fan); + writes[point_count].data =3D point_count; +=20 + write_ret =3D xe_pcode_write_batch(xe_device_get_root_tile(hwmon->xe), w= rites, + point_count + 1); + if (write_ret) + xe_err(hwmon->xe, "failed to write fan %d user table, ret=3D%d\n", fan,= write_ret); +=20 + verify_ret =3D xe_hwmon_get_fan_point_count(hwmon, fan, &accepted_count,= USER_FAN_TABLE); + if (write_ret || verify_ret || accepted_count !=3D point_count) { + int failure_ret =3D write_ret ?: verify_ret ?: -EIO; +=20 + if (verify_ret || accepted_count !=3D point_count) + xe_err(hwmon->xe, + "fan %d table count verification failed: ret=3D%d, expected=3D%= u, got=3D%u\n", + fan, verify_ret, point_count, accepted_count); + /* Firmware may have accepted only part of the table; fall back to stoc= k. */ + ret =3D xe_hwmon_fan_set_auto(hwmon, fan); + if (ret) + xe_err(hwmon->xe, "failed to restore fan %d to stock mode, ret=3D%d\n"= , fan, + ret); + return failure_ret; + } +=20 + xe_dbg(hwmon->xe, "fan %d user table committed successfully with %u poin= ts\n", fan, + point_count); + return 0; + } +=20 + static int xe_hwmon_activate_user_fan_table(struct xe_hwmon *hwmon, u8 fa= n, + bool is_full_speed, enum fan_table_type table_source) + { + struct xe_hwmon_fan_info *fi =3D &hwmon->fi[fan]; + struct fan_control_point points[MAX_FAN_CONTROL_POINTS]; + const struct fan_table *source_table; + u32 point_count, point; + int ret; +=20 + source_table =3D &fi->fan_table[table_source]; +=20 + xe_dbg(hwmon->xe, "activating fan %d user table from %s table\n", fan, + (table_source =3D=3D USER_FAN_TABLE) ? "user" : "stock"); +=20 + point_count =3D source_table->point_count; + if (!point_count || point_count > MAX_FAN_CONTROL_POINTS) { + xe_err(hwmon->xe, + "fan %d %s table point count %u is invalid, cannot activate user= table\n", + fan, (table_source =3D=3D USER_FAN_TABLE) ? "user" : "stock", po= int_count); + return -EINVAL; + } +=20 + for (point =3D 0; point < point_count; point++) { + points[point].temp =3D source_table->fcp[point].temp; + points[point].speed =3D is_full_speed ? 100 : + min_t(u8, source_table->fcp[point].speed, 100); + } +=20 + ret =3D xe_hwmon_commit_user_fan_table(hwmon, fan, points, point_count); + if (ret) + return ret; +=20 + /* Full speed only overrides firmware; the cache keeps the manual curve.= */ + if (table_source =3D=3D STOCK_FAN_TABLE) { + struct fan_table *user_table =3D &fi->fan_table[USER_FAN_TABLE]; +=20 + for (point =3D 0; point < point_count; point++) { + user_table->fcp[point].temp =3D source_table->fcp[point].temp; + user_table->fcp[point].speed =3D + min_t(u8, source_table->fcp[point].speed, 100); + } + user_table->point_count =3D point_count; + } +=20 + fi->pwm_enable_mode =3D is_full_speed ? XE_FAN_PWM_FULL_SPEED : + XE_FAN_PWM_MANUAL_USER_TABLE; +=20 + return 0; + } +=20 + /* Temperature to set when setting the fan PWM with pwm[1-3]. */ + #define FIXED_PWM_TEMP 1 +=20 + static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 p= wm) + { + struct xe_hwmon_fan_info *fi =3D &hwmon->fi[fan]; + struct fan_table *user_table =3D &fi->fan_table[USER_FAN_TABLE]; + struct fan_control_point points[MIN_FAN_CONTROL_POINTS]; + int ret, i; + u8 speed; +=20 + if (fi->pwm_enable_mode !=3D XE_FAN_PWM_MANUAL_USER_TABLE) { + xe_dbg(hwmon->xe, "fan %d manual mode is not active, cannot set pwm\n",= fan); + return -EINVAL; + } +=20 + /* Fan table speed are in percent. */ + speed =3D min_t(u32, DIV_ROUND_CLOSEST(pwm * 100, U8_MAX), 100); +=20 + /* Fan control needs at least two points; program them all at the same s= peed. */ + for (i =3D 0; i < MIN_FAN_CONTROL_POINTS; i++) { + points[i].temp =3D FIXED_PWM_TEMP + i; + points[i].speed =3D speed; + } +=20 + ret =3D xe_hwmon_commit_user_fan_table(hwmon, fan, points, MIN_FAN_CONTR= OL_POINTS); + if (ret) { + xe_err(hwmon->xe, "failed to set pwm %u (%u%%) for fan %d, ret=3D%d\n", + pwm, speed, fan, ret); + return ret; + } +=20 + /* Update the user fan table cache. */ + memcpy(user_table->fcp, points, sizeof(points)); + user_table->point_count =3D MIN_FAN_CONTROL_POINTS; + user_table->temp_staged =3D 0; + user_table->speed_staged =3D 0; + xe_dbg(hwmon->xe, "applied pwm %u (%u%%) for fan %d\n", pwm, speed, fan); +=20 + return 0; + } +=20 + #define XE_CURVE_IDX(fan, point) (((fan) << 8) | (point)) + #define XE_CURVE_FAN(idx) ((u8)(((idx) >> 8) & 0xff)) + #define XE_CURVE_POINT(idx) ((u8)((idx) & 0xff)) +=20 + static int + xe_hwmon_decode_curve_index(struct xe_hwmon *hwmon, u32 index, u8 *fan, u= 8 *point) + { + u8 local_fan =3D XE_CURVE_FAN(index); + u8 local_point =3D XE_CURVE_POINT(index); +=20 + if (local_fan >=3D FAN_MAX || local_point >=3D MAX_FAN_CONTROL_POINTS) { + xe_dbg(hwmon->xe, "invalid fan curve index 0x%x: fan %u point %u\n", + index, local_fan, local_point); + return -EINVAL; + } +=20 + *fan =3D local_fan; + *point =3D local_point; +=20 + return 0; + } +=20 + static enum fan_table_type xe_hwmon_get_active_fan_table(struct xe_hwmon = *hwmon, u8 fan) + { + return (hwmon->fi[fan].pwm_enable_mode =3D=3D XE_FAN_PWM_AUTO_STOCK_TABL= E) ? STOCK_FAN_TABLE : + USER_FAN_TABLE; + } +=20 + static ssize_t + xe_hwmon_pwm_auto_point_temp_show(struct device *dev, + struct device_attribute *attr, char *buf) + { + struct sensor_device_attribute *sattr =3D to_sensor_dev_attr(attr); + struct xe_hwmon *hwmon =3D dev_get_drvdata(dev); + enum fan_table_type active_table; + struct xe_hwmon_fan_info *fi; + u32 point_count; + u8 point; + u8 temp; + int ret; + u8 fan; +=20 + ret =3D xe_hwmon_decode_curve_index(hwmon, sattr->index, &fan, &point); + if (ret) + return ret; +=20 + guard(mutex)(&hwmon->hwmon_lock); +=20 + fi =3D &hwmon->fi[fan]; + if (fi->pwm_enable_mode =3D=3D XE_FAN_PWM_UNINITIALIZED) + return -ENODATA; +=20 + active_table =3D xe_hwmon_get_active_fan_table(hwmon, fan); +=20 + point_count =3D fi->fan_table[active_table].point_count; +=20 + if (point >=3D point_count) + temp =3D 0; + else + temp =3D fi->fan_table[active_table].fcp[point].temp; + return sysfs_emit(buf, "%u\n", temp * MILLIDEGREE_PER_DEGREE); + } +=20 + static int xe_hwmon_update_user_fan_point(struct xe_hwmon *hwmon, u8 fan,= u32 point, + bool is_temp, u8 value) + { + struct xe_hwmon_fan_info *fi =3D &hwmon->fi[fan]; + struct fan_table *table; + u32 point_count; + int ret; +=20 + table =3D &fi->fan_table[USER_FAN_TABLE]; +=20 + if (is_temp) { + table->staged_fcp[point].temp =3D value; + table->temp_staged |=3D BIT(point); + } else { + table->staged_fcp[point].speed =3D value; + table->speed_staged |=3D BIT(point); + } +=20 + /* Wait for a complete prefix; (0, 0) ends the curve without becoming a = point. */ + for (point_count =3D 0; point_count < MAX_FAN_CONTROL_POINTS; point_coun= t++) { + if (!(table->temp_staged & BIT(point_count)) || + !(table->speed_staged & BIT(point_count))) + return 0; + if (!table->staged_fcp[point_count].temp && + !table->staged_fcp[point_count].speed) + break; + } +=20 + if (point_count < MIN_FAN_CONTROL_POINTS) { + xe_dbg(hwmon->xe, "fan %d curve needs at least %d points, got %u\n", + fan, MIN_FAN_CONTROL_POINTS, point_count); + ret =3D -EINVAL; + goto clear_staged; + } +=20 + ret =3D xe_hwmon_commit_user_fan_table(hwmon, fan, table->staged_fcp, po= int_count); + if (ret) + goto clear_staged; +=20 + /* Publish the new curve only after firmware accepts its point count. */ + memcpy(table->fcp, table->staged_fcp, point_count * sizeof(table->fcp[0]= )); + table->point_count =3D point_count; + fi->pwm_enable_mode =3D XE_FAN_PWM_MANUAL_USER_TABLE; +=20 + clear_staged: + table->temp_staged =3D 0; + table->speed_staged =3D 0; + return ret; + } +=20 + static int xe_hwmon_sysfs_to_hw_val(struct xe_hwmon *hwmon, long val, boo= l is_temp, u8 *out) + { + long max =3D is_temp ? U8_MAX * MILLIDEGREE_PER_DEGREE : U8_MAX; + long hw_val; +=20 + if (val < 0 || val > max) { + xe_dbg(hwmon->xe, "%s value %ld out of range\n", is_temp ? "temp" : "pw= m", val); + return -EINVAL; + } +=20 + /* Temperature is in degrees Celsius, speed in percent. */ + if (is_temp) + hw_val =3D DIV_ROUND_CLOSEST(val, MILLIDEGREE_PER_DEGREE); + else + hw_val =3D DIV_ROUND_CLOSEST(val * 100, U8_MAX); +=20 + if (!is_temp && val && !hw_val) + hw_val =3D 1; +=20 + *out =3D hw_val; +=20 + return 0; + } +=20 + static ssize_t + xe_hwmon_pwm_auto_point_temp_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) + { + struct sensor_device_attribute *sattr =3D to_sensor_dev_attr(attr); + struct xe_hwmon *hwmon =3D dev_get_drvdata(dev); + struct xe_hwmon_fan_info *fi; + long val; + u8 point; + u8 temp; + int ret; + u8 fan; +=20 + ret =3D xe_hwmon_decode_curve_index(hwmon, sattr->index, &fan, &point); + if (ret) + return ret; +=20 + ret =3D kstrtol(buf, 10, &val); + if (ret) + return ret; +=20 + ret =3D xe_hwmon_sysfs_to_hw_val(hwmon, val, true, &temp); + if (ret) + return ret; +=20 + guard(xe_pm_runtime)(hwmon->xe); +=20 + guard(mutex)(&hwmon->hwmon_lock); + fi =3D &hwmon->fi[fan]; +=20 + if (fi->pwm_enable_mode !=3D XE_FAN_PWM_MANUAL_USER_TABLE) { + xe_dbg(hwmon->xe, "pwm%d_enable not in manual mode; cannot update temp = point %d\n", + fan + 1, point + 1); + return -EINVAL; + } +=20 + xe_dbg(hwmon->xe, "staging fan %d point %d temp to %u\n", fan, point, te= mp); + ret =3D xe_hwmon_update_user_fan_point(hwmon, fan, point, true, temp); +=20 + return ret ? ret : count; + } +=20 + static ssize_t + xe_hwmon_pwm_auto_point_pwm_show(struct device *dev, + struct device_attribute *attr, + char *buf) + { + struct sensor_device_attribute *sattr =3D to_sensor_dev_attr(attr); + struct xe_hwmon *hwmon =3D dev_get_drvdata(dev); + enum fan_table_type active_table; + struct xe_hwmon_fan_info *fi; + u32 point_count; + u8 point; + u8 speed; + int ret; + u8 fan; +=20 + ret =3D xe_hwmon_decode_curve_index(hwmon, sattr->index, &fan, &point); + if (ret) + return ret; +=20 + guard(mutex)(&hwmon->hwmon_lock); +=20 + fi =3D &hwmon->fi[fan]; + if (fi->pwm_enable_mode =3D=3D XE_FAN_PWM_UNINITIALIZED) + return -ENODATA; +=20 + active_table =3D xe_hwmon_get_active_fan_table(hwmon, fan); +=20 + point_count =3D fi->fan_table[active_table].point_count; +=20 + if (point >=3D point_count) + speed =3D 0; + else + speed =3D fi->fan_table[active_table].fcp[point].speed; + return sysfs_emit(buf, "%u\n", DIV_ROUND_CLOSEST(speed * U8_MAX, 100)); + } +=20 + static ssize_t + xe_hwmon_pwm_auto_point_pwm_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) + { + struct sensor_device_attribute *sattr =3D to_sensor_dev_attr(attr); + struct xe_hwmon *hwmon =3D dev_get_drvdata(dev); + struct xe_hwmon_fan_info *fi; + long val; + u8 point; + u8 speed; + int ret; + u8 fan; +=20 + ret =3D xe_hwmon_decode_curve_index(hwmon, sattr->index, &fan, &point); + if (ret) + return ret; +=20 + ret =3D kstrtol(buf, 10, &val); + if (ret) + return ret; +=20 + ret =3D xe_hwmon_sysfs_to_hw_val(hwmon, val, false, &speed); + if (ret) + return ret; +=20 + guard(xe_pm_runtime)(hwmon->xe); +=20 + guard(mutex)(&hwmon->hwmon_lock); + fi =3D &hwmon->fi[fan]; +=20 + if (fi->pwm_enable_mode !=3D XE_FAN_PWM_MANUAL_USER_TABLE) { + xe_dbg(hwmon->xe, + "pwm%d_enable is not in manual user mode, cannot update pwm poin= t %d\n", + fan + 1, point + 1); + return -EINVAL; + } +=20 + xe_dbg(hwmon->xe, "staging fan %d point %d speed to %u%%\n", fan, point,= speed); + ret =3D xe_hwmon_update_user_fan_point(hwmon, fan, point, false, speed); +=20 + return ret ? ret : count; + } +=20 + static umode_t xe_hwmon_curve_attributes_visible(struct kobject *kobj, + struct attribute *attr, int index) + { + struct device *dev =3D kobj_to_dev(kobj); + struct xe_hwmon *hwmon =3D dev_get_drvdata(dev); + u32 fan =3D index / XE_CURVE_ATTRS_PER_FAN; +=20 + guard(mutex)(&hwmon->hwmon_lock); +=20 + /* Manual mode, and thus curve writes, need a stock table, as for pwmN_e= nable. */ + if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, fan) || + hwmon->fi[fan].fan_table[STOCK_FAN_TABLE].point_count < MIN_FAN_CONT= ROL_POINTS) + return 0; +=20 + /* All slots must exist at registration, before the user curve has been = staged. */ + return attr->mode; + } +=20 + static void xe_hwmon_init_curve_attrs(struct xe_hwmon *hwmon) + { + int fan, kind, point, i =3D 0; +=20 + /* Per fan: all temp points then all pwm points; is_visible relies on th= is layout. */ + for (fan =3D 0; fan < FAN_MAX; fan++) { + for (kind =3D 0; kind < 2; kind++) { + bool is_temp =3D !kind; +=20 + for (point =3D 0; point < MAX_FAN_CONTROL_POINTS; point++, i++) { + struct sensor_device_attribute *sda =3D &hwmon->curve_sda[i]; +=20 + snprintf(hwmon->curve_names[i], XE_CURVE_ATTR_NAME_LEN, + "pwm%d_auto_point%d_%s", fan + 1, point + 1, + is_temp ? "temp" : "pwm"); + sysfs_attr_init(&sda->dev_attr.attr); + sda->dev_attr.attr.name =3D hwmon->curve_names[i]; + sda->dev_attr.attr.mode =3D 0644; + if (is_temp) { + sda->dev_attr.show =3D xe_hwmon_pwm_auto_point_temp_show; + sda->dev_attr.store =3D xe_hwmon_pwm_auto_point_temp_store; + } else { + sda->dev_attr.show =3D xe_hwmon_pwm_auto_point_pwm_show; + sda->dev_attr.store =3D xe_hwmon_pwm_auto_point_pwm_store; + } + sda->index =3D XE_CURVE_IDX(fan, point); + hwmon->curve_attrs[i] =3D &sda->dev_attr.attr; + } + } + } +=20 + hwmon->curve_group.attrs =3D hwmon->curve_attrs; + hwmon->curve_group.is_visible =3D xe_hwmon_curve_attributes_visible; + } +=20 + static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon) + { + bool found =3D false; + int point; + int ret; + int fan; +=20 + if (hwmon->xe->info.platform =3D=3D XE_DG2) + return -ENODATA; +=20 + /* Stock table failures leave fan_input and fan_max visible; they don't = need it. */ + for (fan =3D 0; fan < FAN_MAX; fan++) { + struct fan_control_point stock_fcp[MAX_FAN_CONTROL_POINTS]; + struct xe_hwmon_fan_info *fi =3D &hwmon->fi[fan]; + u32 stock_fcp_count =3D 0; + u32 max_rps =3D 0; +=20 + if (!IS_FAN_ENABLED(hwmon, fan)) + continue; +=20 + ret =3D xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_MAX_FAN_RPS, fa= n, + &max_rps); + if (ret) { + xe_warn(hwmon->xe, "failed to read fan %d max RPS, ret=3D%d\n", fan, r= et); + } else { + fi->max_rps =3D max_rps; + xe_dbg(hwmon->xe, "fan %d max RPS %u\n", fan, fi->max_rps); + } +=20 + ret =3D xe_hwmon_pcode_read_fan_control(hwmon, + FSC_READ_STOCK_FAN_CONTROL_POINTS, + fan, &stock_fcp_count); + if (ret || !stock_fcp_count) { + xe_warn(hwmon->xe, + "fan %d stock table unavailable, ret=3D%d, point count=3D%u\n", + fan, ret, ret ? 0 : stock_fcp_count); + continue; + } +=20 + if (stock_fcp_count > MAX_FAN_CONTROL_POINTS) { + xe_warn(hwmon->xe, + "fan %d stock table point count %u exceeds maximum %u, clamping\n", + fan, stock_fcp_count, MAX_FAN_CONTROL_POINTS); + stock_fcp_count =3D MAX_FAN_CONTROL_POINTS; + } +=20 + xe_dbg(hwmon->xe, "fan %d stock table point count is %u\n", fan, stock_= fcp_count); +=20 + /* Cache the stock fan table for later use. */ + for (point =3D 0; point < stock_fcp_count; point++) { + u32 fcp =3D FCP_INDEX(STOCK_FAN_TABLE, point); +=20 + ret =3D xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_FAN_TABLE, fan= , &fcp); + if (ret) { + xe_warn(hwmon->xe, "failed to read fan %d stock point %d, ret=3D%d\n", + fan, point, ret); + break; + } +=20 + stock_fcp[point].temp =3D REG_FIELD_GET(FAN_CONTROL_POINT_TEMP_MASK, f= cp); + stock_fcp[point].speed =3D REG_FIELD_GET(FAN_CONTROL_POINT_SPEED_MASK,= fcp); + xe_dbg(hwmon->xe, "fan %d stock point %d: temp %u C, speed %u %%\n", + fan, point, stock_fcp[point].temp, stock_fcp[point].speed); + } +=20 + if (point < stock_fcp_count) { + xe_warn(hwmon->xe, "fan %d stock table incomplete, expected %u points,= got %d\n", + fan, stock_fcp_count, point); + continue; + } +=20 + memcpy(fi->fan_table[STOCK_FAN_TABLE].fcp, stock_fcp, + stock_fcp_count * sizeof(stock_fcp[0])); + fi->fan_table[STOCK_FAN_TABLE].point_count =3D stock_fcp_count; + found =3D true; + if (fi->pwm_enable_mode =3D=3D XE_FAN_PWM_UNINITIALIZED) + fi->pwm_enable_mode =3D XE_FAN_PWM_AUTO_STOCK_TABLE; + } +=20 + /* Succeed if any enabled fan's stock table was read. */ + return found ? 0 : -ENODATA; } =20 static int xe_hwmon_power_curr_crit_read(struct xe_hwmon *hwmon, int chan= nel, @@@ -1057,19 -1986,70 +1986,70 @@@ static void xe_hwmon_get_voltage(struc *value =3D DIV_ROUND_CLOSEST(REG_FIELD_GET(VOLTAGE_MASK, reg_val) * 2500= , SF_VOLTAGE); } =20 - static inline bool is_vram_ch_available(struct xe_hwmon *hwmon, int chann= el) + static bool xe_hwmon_temp_probe(struct xe_hwmon *hwmon, int channel) { struct xe_mmio *mmio =3D xe_root_tile_mmio(hwmon->xe); - int vram_id =3D channel - CHANNEL_VRAM_N; - struct xe_reg vram_reg; + struct xe_reg reg; + u32 reg_val; =20 - vram_reg =3D xe_hwmon_get_reg(hwmon, REG_TEMP, channel); - if (!xe_reg_is_valid(vram_reg) || !xe_mmio_read32(mmio, vram_reg)) + switch (channel) { + case CHANNEL_PKG: + case CHANNEL_VRAM: + case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX: + reg =3D xe_hwmon_get_reg(hwmon, REG_TEMP, channel); + if (!xe_reg_is_valid(reg)) + return false; +=20 + /* + * On CRI, VRAM channel presence comes from the MSU enable mask + * (applied in xe_hwmon_get_reg()); the temperature register's + * 0xffffffff sentinel is not reliable for VRAM channels. + */ + if (hwmon->xe->info.platform =3D=3D XE_CRESCENTISLAND && channel >=3D C= HANNEL_VRAM_N) + return true; +=20 + reg_val =3D xe_mmio_read32(mmio, reg); + if (!mmio_temp_valid(hwmon, reg_val)) { + drm_dbg(&hwmon->xe->drm, + "channel %d temperature unavailable, val 0x%x\n", channel, reg_val); + return false; + } +=20 + return true; + case CHANNEL_MCTRL: + return hwmon->temp.count && !get_mc_temp(hwmon, NULL); + case CHANNEL_PCIE: + return hwmon->temp.count && !get_pcie_temp(hwmon, NULL); + default: return false; + } + } =20 - /* Create label only for available vram channel */ - sprintf(hwmon->temp.vram_label[vram_id], "vram_ch_%d", vram_id); - return true; + static int xe_hwmon_init_temp_info(struct xe_hwmon *hwmon) + { + struct device *dev =3D hwmon->xe->drm.dev; + int channel; +=20 + if (hwmon->xe->info.has_mbx_thermal_info && xe_hwmon_pcode_read_thermal_= info(hwmon)) + drm_warn(&hwmon->xe->drm, "Thermal mailbox not supported by card firmwa= re\n"); +=20 + /* vram_count is known only after reading thermal info above. */ + if (hwmon->temp.vram_count) { + hwmon->temp.vram_label =3D devm_kcalloc(dev, hwmon->temp.vram_count, + MAX_LABEL_SIZE, GFP_KERNEL); + if (!hwmon->temp.vram_label) + return -ENOMEM; + } +=20 + for (channel =3D 0; channel < CHANNEL_MAX; channel++) { + hwmon->temp.available[channel] =3D xe_hwmon_temp_probe(hwmon, channel); + if (hwmon->temp.available[channel] && hwmon->temp.vram_label && + in_range(channel, CHANNEL_VRAM_N, hwmon->temp.vram_count)) + snprintf(hwmon->temp.vram_label[channel - CHANNEL_VRAM_N], MAX_LABEL_S= IZE, + "vram_ch_%d", channel - CHANNEL_VRAM_N); + } +=20 + return 0; } =20 static umode_t @@@ -1079,14 -2059,13 +2059,13 @@@ xe_hwmon_temp_is_visible(struct xe_hwmo case hwmon_temp_emergency: switch (channel) { case CHANNEL_PKG: - return hwmon->temp.limit[TEMP_LIMIT_PKG_SHUTDOWN] ? 0444 : 0; - case CHANNEL_VRAM: - return hwmon->temp.limit[TEMP_LIMIT_MEM_SHUTDOWN] ? 0444 : 0; case CHANNEL_MCTRL: case CHANNEL_PCIE: - return hwmon->temp.count ? 0444 : 0; + return (hwmon->temp.available[channel] && + hwmon->temp.limit[TEMP_LIMIT_PKG_SHUTDOWN]) ? 0444 : 0; + case CHANNEL_VRAM: case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX: - return (is_vram_ch_available(hwmon, channel) && + return (hwmon->temp.available[channel] && hwmon->temp.limit[TEMP_LIMIT_MEM_SHUTDOWN]) ? 0444 : 0; default: return 0; @@@ -1094,14 -2073,13 +2073,13 @@@ case hwmon_temp_crit: switch (channel) { case CHANNEL_PKG: - return hwmon->temp.limit[TEMP_LIMIT_PKG_CRIT] ? 0444 : 0; - case CHANNEL_VRAM: - return hwmon->temp.limit[TEMP_LIMIT_MEM_CRIT] ? 0444 : 0; case CHANNEL_MCTRL: case CHANNEL_PCIE: - return hwmon->temp.count ? 0444 : 0; + return (hwmon->temp.available[channel] && + hwmon->temp.limit[TEMP_LIMIT_PKG_CRIT]) ? 0444 : 0; + case CHANNEL_VRAM: case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX: - return (is_vram_ch_available(hwmon, channel) && + return (hwmon->temp.available[channel] && hwmon->temp.limit[TEMP_LIMIT_MEM_CRIT]) ? 0444 : 0; default: return 0; @@@ -1109,7 -2087,8 +2087,8 @@@ case hwmon_temp_max: switch (channel) { case CHANNEL_PKG: - return hwmon->temp.limit[TEMP_LIMIT_PKG_MAX] ? 0444 : 0; + return (hwmon->temp.available[channel] && + hwmon->temp.limit[TEMP_LIMIT_PKG_MAX]) ? 0444 : 0; default: return 0; } @@@ -1118,13 -2097,10 +2097,10 @@@ switch (channel) { case CHANNEL_PKG: case CHANNEL_VRAM: - return xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_TEMP, - channel)) ? 0444 : 0; case CHANNEL_MCTRL: case CHANNEL_PCIE: - return hwmon->temp.count ? 0444 : 0; case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX: - return is_vram_ch_available(hwmon, channel) ? 0444 : 0; + return hwmon->temp.available[channel] ? 0444 : 0; default: return 0; } @@@ -1133,6 -2109,70 +2109,70 @@@ } } =20 + #define IEEE754_SIGN REG_BIT(31) + #define IEEE754_EXP_MASK REG_GENMASK(30, 23) + #define IEEE754_MANTISSA_MASK REG_GENMASK(22, 0) + #define IEEE754_EXP_BIAS 127 /* Exponent bias to obtain the actual expone= nt */ + #define IEEE754_MANTISSA_BITS 23 /* Mantissa width and implicit leading-1= bit position */ + #define IEEE754_EXP_SPECIAL 0xff /* reserved exponent value encoding infi= nity or NaN */ +=20 + /** + * xe_hwmon_temperature_decode - Decode an IEEE-754 temperature register = value + * @reg_val: The raw register value to decode + * @temperature: Pointer to store the decoded temperature in millidegrees= Celsius. + * + * IEEE-754 single-precision format: + * + * 31 30 23 22 0 + * +--+-----------------------------+----------------------------+ + * | S| exponent | fraction | + * +--+-----------------------------+----------------------------+ + * + * value =3D (-1)^S * 1.fraction * 2^(exponent - 127) + * + * Return: 0 on success or an error if @reg_val is non-finite or cannot be + * represented in millidegrees Celsius. + */ + static int xe_hwmon_temperature_decode(u32 reg_val, long *temperature) + { + u32 exponent =3D REG_FIELD_GET(IEEE754_EXP_MASK, reg_val); + u32 mantissa =3D REG_FIELD_GET(IEEE754_MANTISSA_MASK, reg_val); + s64 value; + int shift; +=20 + /* Exponent 0xff encodes infinity or NaN. */ + if (exponent =3D=3D IEEE754_EXP_SPECIAL) + return -EINVAL; +=20 + /* Subnormal values are smaller than one millidegree Celsius. */ + if (!exponent) { + *temperature =3D 0; + return 0; + } +=20 + value =3D (s64)(mantissa | BIT(IEEE754_MANTISSA_BITS)) * MILLIDEGREE_PER= _DEGREE; + shift =3D exponent - (IEEE754_EXP_BIAS + IEEE754_MANTISSA_BITS); + if (shift >=3D 0) { + if (shift >=3D BITS_PER_LONG || value > (LONG_MAX >> shift)) + return -ERANGE; + value <<=3D shift; + } else if (shift > -64) { + value >>=3D -shift; + } else { + value =3D 0; + } +=20 + /* Ensure the decoded value fits in a long. */ + if (overflows_type(value, *temperature)) + return -ERANGE; +=20 + if (reg_val & IEEE754_SIGN) + value =3D -value; +=20 + *temperature =3D value; + return 0; + } +=20 static int xe_hwmon_temp_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *v= al) { @@@ -1142,26 -2182,33 +2182,33 @@@ switch (attr) { case hwmon_temp_input: switch (channel) { - case CHANNEL_PKG: - case CHANNEL_VRAM: - reg_val =3D xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_TEMP, cha= nnel)); -=20 - /* HW register value is in degrees Celsius, convert to millidegrees. */ - *val =3D REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE; - return 0; case CHANNEL_MCTRL: return get_mc_temp(hwmon, val); case CHANNEL_PCIE: return get_pcie_temp(hwmon, val); + case CHANNEL_PKG: + case CHANNEL_VRAM: case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX: reg_val =3D xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_TEMP, cha= nnel)); - /* - * This temperature format is 24 bit [31:8] signed integer and 8 bit - * [7:0] fraction. - */ - *val =3D (s32)(REG_FIELD_GET(TEMP_MASK_VRAM_N, reg_val)) * - (REG_FIELD_GET(TEMP_SIGN_MASK, reg_val) ? -1 : 1) * - MILLIDEGREE_PER_DEGREE; + if (!mmio_temp_valid(hwmon, reg_val)) { + drm_dbg(&hwmon->xe->drm, + "channel %d temperature invalid, val 0x%llx\n", + channel, reg_val); + return -ENXIO; + } + if (hwmon->xe->info.platform >=3D XE_CRESCENTISLAND) { + return xe_hwmon_temperature_decode((u32)reg_val, val); + } else if (channel >=3D CHANNEL_VRAM_N) { + /* + * This temperature format is 24 bit [31:8] signed integer and 8 bit + * [7:0] fraction for platforms before CRI. + */ + *val =3D (s32)REG_FIELD_GET(TEMP_MASK_VRAM_N, reg_val) * + (REG_FIELD_GET(TEMP_SIGN_MASK, reg_val) ? -1 : 1) * + MILLIDEGREE_PER_DEGREE; + } else { + *val =3D REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE; + } return 0; default: return -EOPNOTSUPP; @@@ -1215,6 -2262,8 +2262,8 @@@ xe_hwmon_power_is_visible(struct xe_hwm struct xe_mmio *mmio =3D xe_root_tile_mmio(hwmon->xe); =20 switch (attr) { + case hwmon_power_cap_max: + return hwmon->pl2_on_boot[channel] ? 0444 : 0; case hwmon_power_max: case hwmon_power_cap: if (hwmon->xe->info.has_mbx_power_limits) { @@@ -1280,6 -2329,10 +2329,10 @@@ static in xe_hwmon_power_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *= val) { switch (attr) { + case hwmon_power_cap_max: + *val =3D mul_u32_u32(REG_FIELD_GET(PWR_LIM_VAL, hwmon->pl2_on_boot[chan= nel]), + SF_POWER) >> hwmon->scl_shift_power; + return 0; case hwmon_power_max: case hwmon_power_cap: return xe_hwmon_power_max_read(hwmon, attr, channel, val); @@@ -1410,17 -2463,36 +2463,36 @@@ xe_hwmon_energy_read(struct xe_hwmon *h static umode_t xe_hwmon_fan_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel) { - u32 uval =3D 0; -=20 - if (!hwmon->xe->info.has_fan_control) + if (!hwmon->xe->info.has_fan) return 0; =20 switch (attr) { case hwmon_fan_input: - if (xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_NUM_FANS, &uval)) - return 0; + return IS_FAN_ENABLED(hwmon, channel) ? 0444 : 0; + case hwmon_fan_max: + { + guard(mutex)(&hwmon->hwmon_lock); + return IS_FAN_ENABLED(hwmon, channel) && hwmon->fi[channel].max_rps ? 0= 444 : 0; + } + default: + return 0; + } + } =20 - return channel < uval ? 0444 : 0; + static umode_t + xe_hwmon_pwm_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel) + { + guard(mutex)(&hwmon->hwmon_lock); +=20 + /* Manual control starts from the stock table, so it needs a usable curv= e. */ + if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, channel) = || + hwmon->fi[channel].fan_table[STOCK_FAN_TABLE].point_count < MIN_FAN_= CONTROL_POINTS) + return 0; +=20 + switch (attr) { + case hwmon_pwm_input: + case hwmon_pwm_enable: + return 0644; default: return 0; } @@@ -1471,11 -2543,251 +2543,251 @@@ xe_hwmon_fan_read(struct xe_hwmon *hwmo switch (attr) { case hwmon_fan_input: return xe_hwmon_fan_input_read(hwmon, channel, val); + case hwmon_fan_max: + scoped_guard(mutex, &hwmon->hwmon_lock) { + *val =3D mul_u32_u32(hwmon->fi[channel].max_rps, 60); + } + return 0; default: return -EOPNOTSUPP; } } =20 + /** + * xe_hwmon_get_active_table_pwm - Calculate PWM from the active fan curve + * @hwmon: hwmon instance, with hwmon_lock held and runtime PM active + * @channel: fan channel + * @val: calculated PWM percentage + * + * Read package temperature and linearly interpolate the fan table select= ed by + * pwm_enable. Temperatures outside the table range use the nearest endpo= int. + * + * Return: 0 on success, or a negative error code. + */ + static int xe_hwmon_get_active_table_pwm(struct xe_hwmon *hwmon, int chan= nel, long *val) + { + const struct fan_table *table; + long temperature, pwm; + int point, ret; +=20 + lockdep_assert_held(&hwmon->hwmon_lock); +=20 + switch (hwmon->fi[channel].pwm_enable_mode) { + case XE_FAN_PWM_UNINITIALIZED: + return -ENODATA; + case XE_FAN_PWM_FULL_SPEED: + *val =3D 100; + return 0; + default: + break; + } +=20 + table =3D &hwmon->fi[channel].fan_table[xe_hwmon_get_active_fan_table(hw= mon, channel)]; +=20 + if (!table->point_count) + return -ENODATA; +=20 + ret =3D xe_hwmon_temp_read(hwmon, hwmon_temp_input, CHANNEL_PKG, &temper= ature); + if (ret) + return ret; +=20 + pwm =3D table->fcp[0].speed; + for (point =3D 1; point < table->point_count; point++) { + const struct fan_control_point *lower =3D &table->fcp[point - 1]; + const struct fan_control_point *upper =3D &table->fcp[point]; + long lower_temp =3D lower->temp * MILLIDEGREE_PER_DEGREE; + long upper_temp =3D upper->temp * MILLIDEGREE_PER_DEGREE; + long pwm_delta =3D upper->speed - lower->speed; +=20 + /* Find the bracketing points; temperatures outside the curve use endpo= ints. */ + if (temperature <=3D lower_temp) + break; + if (temperature < upper_temp) { + pwm +=3D DIV_ROUND_CLOSEST(pwm_delta * (temperature - lower_temp), + upper_temp - lower_temp); + break; + } + pwm =3D upper->speed; + } +=20 + *val =3D pwm; + xe_dbg(hwmon->xe, "fan %d: pwm value from active table cache: %ld\n", ch= annel, pwm); + return 0; + } +=20 + /** + * xe_hwmon_get_pwm_from_pmt - Read active fan PWM from PMT telemetry + * @hwmon: hwmon instance, with hwmon_lock held and runtime PM active + * @channel: fan channel + * @val: raw PWM percentage returned by PMT + * + * Return: 0 on success, or a negative error code. + */ + static int xe_hwmon_get_pwm_from_pmt(struct xe_hwmon *hwmon, int channel,= long *val) + { + u64 pmt_val; + u32 guid, pwm; + int ret; +=20 + lockdep_assert_held(&hwmon->hwmon_lock); +=20 + if (hwmon->xe->info.platform !=3D XE_BATTLEMAGE) + return -EOPNOTSUPP; +=20 + ret =3D xe_vsec_get_guid(hwmon->xe, &guid); + if (ret) { + xe_warn(hwmon->xe, "failed to read pwm, PMT unavailable: %d\n", ret); + return ret; + } +=20 + ret =3D xe_pmt_telem_read(hwmon->xe->drm.dev, guid, &pmt_val, + BMG_FAN_PWM_PMT_OFFSET, sizeof(pmt_val)); + if (ret !=3D sizeof(pmt_val)) { + xe_warn(hwmon->xe, "pwm read from PMT failed: %d\n", ret); + return ret < 0 ? ret : -EIO; + } +=20 + switch (channel) { + case FAN_1: + pwm =3D REG_FIELD_GET64(BMG_FAN1_PWM, pmt_val); + break; + case FAN_2: + pwm =3D REG_FIELD_GET64(BMG_FAN2_PWM, pmt_val); + break; + case FAN_3: + pwm =3D REG_FIELD_GET64(BMG_FAN3_PWM, pmt_val); + break; + default: + return -EOPNOTSUPP; + } +=20 + xe_dbg(hwmon->xe, "fan %d: active pwm from PMT: %u\n", channel, pwm); +=20 + *val =3D pwm; + return 0; + } +=20 + static int + xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *va= l) + { + struct xe_hwmon_fan_info *fi; + long pwm; + int ret =3D 0; +=20 + mutex_lock(&hwmon->hwmon_lock); +=20 + fi =3D &hwmon->fi[channel]; +=20 + switch (attr) { + case hwmon_pwm_input: + switch (hwmon->xe->info.platform) { + case XE_BATTLEMAGE: + if (fi->pwm_enable_mode =3D=3D XE_FAN_PWM_UNINITIALIZED) + ret =3D -ENODATA; + else + ret =3D xe_hwmon_get_pwm_from_pmt(hwmon, channel, &pwm); + break; + default: + ret =3D xe_hwmon_get_active_table_pwm(hwmon, channel, &pwm); + break; + } + if (ret =3D=3D -ENODATA) { + xe_dbg(hwmon->xe, "fan %d control mode not established\n", channel); + goto unlock; + } + if (ret) { + xe_err(hwmon->xe, "failed to get active pwm for fan %d, ret=3D%d\n", + channel, ret); + goto unlock; + } + if (pwm < 0 || pwm > 100) { + xe_dbg(hwmon->xe, "invalid PWM value %ld read for fan %d\n", pwm, chan= nel); + ret =3D -ERANGE; + goto unlock; + } + /* Speed is cached in percent, so the 0..255 round trip may be off by a= step. */ + *val =3D DIV_ROUND_CLOSEST(pwm * U8_MAX, 100); + xe_dbg(hwmon->xe, "pwm read for fan %d: %ld\n", channel, *val); + break; + case hwmon_pwm_enable: + if (fi->pwm_enable_mode =3D=3D XE_FAN_PWM_UNINITIALIZED) { + xe_dbg(hwmon->xe, "fan %d control mode is not initialized\n", channel); + ret =3D -ENODATA; + goto unlock; + } + *val =3D fi->pwm_enable_mode; + break; + default: + ret =3D -EOPNOTSUPP; + break; + } +=20 + unlock: + mutex_unlock(&hwmon->hwmon_lock); + return ret; + } +=20 + static int + xe_hwmon_pwm_write(struct xe_hwmon *hwmon, u32 attr, int channel, long va= l) + { + struct xe_hwmon_fan_info *fi; + int ret =3D 0; +=20 + fi =3D &hwmon->fi[channel]; +=20 + mutex_lock(&hwmon->hwmon_lock); +=20 + switch (attr) { + case hwmon_pwm_input: + if (val < 0 || val > U8_MAX) { + xe_dbg(hwmon->xe, "pwm %ld out of range for fan %d\n", val, channel); + ret =3D -EINVAL; + break; + } + ret =3D xe_hwmon_set_user_fan_pwm(hwmon, channel, (u8)val); + break; + case hwmon_pwm_enable: + if (val =3D=3D XE_FAN_PWM_FULL_SPEED || val =3D=3D XE_FAN_PWM_MANUAL_US= ER_TABLE) { + bool full_speed =3D val =3D=3D XE_FAN_PWM_FULL_SPEED; + /* Without a user curve, start from the clamped stock table. */ + enum fan_table_type source =3D fi->fan_table[USER_FAN_TABLE].point_cou= nt ? + USER_FAN_TABLE : STOCK_FAN_TABLE; +=20 + ret =3D xe_hwmon_activate_user_fan_table(hwmon, channel, full_speed, s= ource); + if (ret) { + xe_warn(hwmon->xe, "failed to set fan %d pwm_enable %ld, ret=3D%d\n", + channel, val, ret); + break; + } + fi->fan_table[USER_FAN_TABLE].temp_staged =3D 0; + fi->fan_table[USER_FAN_TABLE].speed_staged =3D 0; + } else if (val =3D=3D XE_FAN_PWM_AUTO_STOCK_TABLE) { + ret =3D xe_hwmon_fan_set_auto(hwmon, channel); + if (ret) { + xe_warn(hwmon->xe, + "failed to switch fan %d to automatic stock control, ret=3D%d\n", + channel, ret); + break; + } + xe_dbg(hwmon->xe, + "automatic control enabled for fan %d using stock table\n", + channel); + fi->fan_table[USER_FAN_TABLE].temp_staged =3D 0; + fi->fan_table[USER_FAN_TABLE].speed_staged =3D 0; + } else { + xe_dbg(hwmon->xe, "invalid pwm%d_enable value %ld\n", channel + 1, val= ); + ret =3D -EINVAL; + } + break; + default: + ret =3D -EOPNOTSUPP; + break; + } + mutex_unlock(&hwmon->hwmon_lock); +=20 + return ret; + } +=20 static umode_t xe_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr, int channel) @@@ -1502,6 -2814,9 +2814,9 @@@ case hwmon_fan: ret =3D xe_hwmon_fan_is_visible(hwmon, attr, channel); break; + case hwmon_pwm: + ret =3D xe_hwmon_pwm_is_visible(hwmon, attr, channel); + break; default: ret =3D 0; break; @@@ -1531,6 -2846,8 +2846,8 @@@ xe_hwmon_read(struct device *dev, enum=20 return xe_hwmon_energy_read(hwmon, attr, channel, val); case hwmon_fan: return xe_hwmon_fan_read(hwmon, attr, channel, val); + case hwmon_pwm: + return xe_hwmon_pwm_read(hwmon, attr, channel, val); default: return -EOPNOTSUPP; } @@@ -1549,6 -2866,8 +2866,8 @@@ xe_hwmon_write(struct device *dev, enu return xe_hwmon_power_write(hwmon, attr, channel, val); case hwmon_curr: return xe_hwmon_curr_write(hwmon, attr, channel, val); + case hwmon_pwm: + return xe_hwmon_pwm_write(hwmon, attr, channel, val); default: return -EOPNOTSUPP; } @@@ -1570,8 -2889,10 +2889,10 @@@ static int xe_hwmon_read_label(struct d *str =3D "mctrl"; else if (channel =3D=3D CHANNEL_PCIE) *str =3D "pcie"; - else if (in_range(channel, CHANNEL_VRAM_N, MAX_VRAM_CHANNELS)) + else if (in_range(channel, CHANNEL_VRAM_N, hwmon->temp.vram_count)) *str =3D hwmon->temp.vram_label[channel - CHANNEL_VRAM_N]; + else + return -EOPNOTSUPP; return 0; case hwmon_power: case hwmon_energy: @@@ -1599,7 -2920,7 +2920,7 @@@ static const struct hwmon_chip_info hwm .info =3D hwmon_info, }; =20 - static void + static int xe_hwmon_get_preregistration_info(struct xe_hwmon *hwmon) { struct xe_mmio *mmio =3D xe_root_tile_mmio(hwmon->xe); @@@ -1652,20 -2973,29 +2973,29 @@@ if (xe_hwmon_is_visible(hwmon, hwmon_energy, hwmon_energy_input, channe= l)) xe_hwmon_energy_get(hwmon, channel, &energy); =20 - /* Initialize 'struct xe_hwmon_fan_info' with initial fan register readi= ng. */ - for (channel =3D 0; channel < FAN_MAX; channel++) - if (xe_hwmon_is_visible(hwmon, hwmon_fan, hwmon_fan_input, channel)) - xe_hwmon_fan_input_read(hwmon, channel, &fan_speed); + if (hwmon->xe->info.has_fan) { + xe_hwmon_get_fan_enable_mask(hwmon); =20 - if (hwmon->xe->info.has_mbx_thermal_info && xe_hwmon_pcode_read_thermal_= info(hwmon)) - drm_warn(&hwmon->xe->drm, "Thermal mailbox not supported by card firmwa= re\n"); + /* Initialize 'struct xe_hwmon_fan_info' with initial fan register read= ing. */ + for (channel =3D 0; channel < FAN_MAX; channel++) + if (IS_FAN_ENABLED(hwmon, channel)) + xe_hwmon_fan_input_read(hwmon, channel, &fan_speed); +=20 + /* Fan control tables initialization */ + if (hwmon->xe->info.has_fan_control && xe_hwmon_read_fan_control_info(h= wmon)) + xe_warn(hwmon->xe, "Fan control tables are not available\n"); + } + return xe_hwmon_init_temp_info(hwmon); } =20 + static void fan_post_lb_worker(struct work_struct *work); +=20 int xe_hwmon_register(struct xe_device *xe) { struct device *dev =3D xe->drm.dev; struct xe_hwmon *hwmon; - int ret, err; + int fan; + int ret; =20 /* hwmon is available only for dGfx */ if (!IS_DGFX(xe)) @@@ -1675,35 -3005,66 +3005,66 @@@ if (IS_SRIOV_VF(xe)) return 0; =20 - hwmon =3D devm_kzalloc_obj(dev, *hwmon); + /* drmm keeps this state alive until late binding work is gone at device= release. */ + hwmon =3D drmm_kzalloc(&xe->drm, sizeof(*hwmon), GFP_KERNEL); if (!hwmon) return -ENOMEM; =20 - ret =3D devm_mutex_init(dev, &hwmon->hwmon_lock); + INIT_DELAYED_WORK(&hwmon->fan_post_lb_work, fan_post_lb_worker); +=20 + hwmon->tjmax_fan_control_state =3D XE_TJMAX_FAN_CONTROL_UNKNOWN; +=20 + /* Zero is XE_FAN_PWM_FULL_SPEED; stay unset until the stock table is re= ad. */ + for (fan =3D 0; fan < FAN_MAX; fan++) + hwmon->fi[fan].pwm_enable_mode =3D XE_FAN_PWM_UNINITIALIZED; +=20 + ret =3D drmm_mutex_init(&xe->drm, &hwmon->hwmon_lock); if (ret) return ret; =20 + ret =3D drmm_mutex_init(&xe->drm, &hwmon->registration_lock); + if (ret) + return ret; +=20 + guard(mutex)(&hwmon->registration_lock); +=20 /* There's only one instance of hwmon per device */ hwmon->xe =3D xe; - xe->hwmon =3D hwmon; + WRITE_ONCE(xe->hwmon, hwmon); =20 - xe_hwmon_get_preregistration_info(hwmon); + ret =3D xe_hwmon_get_preregistration_info(hwmon); + if (ret) + goto err_null_hwmon; =20 drm_dbg(&xe->drm, "Register xe hwmon interface\n"); =20 + hwmon->groups[0] =3D &hwmon_attrgroup; + if (xe->info.has_fan_control) { + xe_hwmon_init_curve_attrs(hwmon); + hwmon->groups[1] =3D &hwmon->curve_group; + } +=20 /* hwmon_dev points to device hwmon */ hwmon->hwmon_dev =3D hwmon_device_register_with_info(dev, "xe", hwmon, &hwmon_chip_info, - hwmon_groups); + hwmon->groups); if (IS_ERR(hwmon->hwmon_dev)) { - err =3D PTR_ERR(hwmon->hwmon_dev); - xe_log_err(xe, HWMON, err, - "Failed to register xe hwmon\n"); - xe->hwmon =3D NULL; - return err; + ret =3D PTR_ERR(hwmon->hwmon_dev); + xe_log_err(xe, HWMON, ret, "Failed to register xe hwmon\n"); + goto err_null_hwmon; + } + if (xe->info.has_fan_control && + (xe->info.late_bind_mask & BIT(XE_LB_FW_FAN_CONTROL))) { + hwmon->fan_worker_poll_count =3D 0; + xe_dbg(xe, "queue fan firmware provisioning check\n"); + mod_delayed_work(system_dfl_wq, &hwmon->fan_post_lb_work, 0); } =20 return 0; +=20 + err_null_hwmon: + WRITE_ONCE(xe->hwmon, NULL); + return ret; } =20 /** @@@ -1715,10 -3076,284 +3076,284 @@@ */ void xe_hwmon_unregister(struct xe_device *xe) { - if (!xe->hwmon) + struct xe_hwmon *hwmon; +=20 + hwmon =3D READ_ONCE(xe->hwmon); + if (!hwmon) return; =20 - hwmon_device_unregister(xe->hwmon->hwmon_dev); - xe->hwmon =3D NULL; + /* An in-flight late binding update finishes before xe->hwmon is cleared= =2E */ + mutex_lock(&hwmon->registration_lock); + if (READ_ONCE(xe->hwmon) !=3D hwmon) { + mutex_unlock(&hwmon->registration_lock); + return; + } + WRITE_ONCE(xe->hwmon, NULL); + mutex_unlock(&hwmon->registration_lock); + cancel_delayed_work_sync(&hwmon->fan_post_lb_work); +=20 + /* + * Sysfs callbacks may wait on hwmon_lock held by a late-bind update + * that needs registration_lock. + */ + hwmon_device_unregister(hwmon->hwmon_dev); } +=20 + /** + * xe_hwmon_suspend - Save hwmon state before suspend + * @xe: xe device instance + * @runtime: true for runtime suspend, false for system suspend or FLR pr= epare + * + * Save any hwmon runtime state that must survive PM suspend transitions. + */ + void xe_hwmon_suspend(struct xe_device *xe, bool runtime) + { + struct xe_hwmon *hwmon =3D READ_ONCE(xe->hwmon); + int fan; +=20 + if (!hwmon || !xe->info.has_fan_control) + return; +=20 + cancel_delayed_work_sync(&hwmon->fan_post_lb_work); +=20 + scoped_guard(mutex, &hwmon->registration_lock) { + hwmon->fan_refresh_pending =3D !runtime || xe->d3cold.allowed; + } +=20 + /* Runtime suspend keeps the fan tables unless the device enters d3cold.= */ + if (runtime && !xe->d3cold.allowed) { + xe_dbg(xe, "d3cold not allowed, skipping fan state snapshot\n"); + return; + } +=20 + guard(mutex)(&hwmon->hwmon_lock); +=20 + if (!runtime || xe->d3cold.allowed) { + u32 val =3D 0; + int ret; +=20 + hwmon->tjmax_fan_control_state =3D XE_TJMAX_FAN_CONTROL_UNKNOWN; +=20 + ret =3D xe_pcode_read(xe_device_get_root_tile(xe), + PCODE_MBOX(FAN_SPEED_CONTROL, + FSC_READ_TJMAX_FAN_CONTROL_MODE, 0), + &val, NULL); + if (ret) { + xe_warn(xe, "failed to save fan tjmax control, ret=3D%d\n", ret); + } else { + hwmon->tjmax_fan_control_state =3D + (val & TJMAX_FAN_CONTROL_ENABLE) ? + XE_TJMAX_FAN_CONTROL_ENABLED : + XE_TJMAX_FAN_CONTROL_DISABLED; + } + } +=20 + for (fan =3D 0; fan < FAN_MAX; fan++) { + struct xe_hwmon_fan_info *fi =3D &hwmon->fi[fan]; + u32 point_count =3D fi->fan_table[USER_FAN_TABLE].point_count; +=20 + if (!IS_FAN_ENABLED(hwmon, fan)) + continue; +=20 + fi->restore_user_table =3D false; + if (fi->pwm_enable_mode !=3D XE_FAN_PWM_MANUAL_USER_TABLE && + fi->pwm_enable_mode !=3D XE_FAN_PWM_FULL_SPEED) + continue; +=20 + if (point_count < MIN_FAN_CONTROL_POINTS || + point_count > MAX_FAN_CONTROL_POINTS) + continue; + fi->restore_user_table =3D true; + xe_dbg(hwmon->xe, + "fan %d cached user table marked for restore, point_count=3D%u\n= ", fan, + point_count); + } + } +=20 + static void restart_fan_refresh(struct xe_hwmon *hwmon) + { + struct xe_device *xe =3D hwmon->xe; +=20 + mutex_lock(&hwmon->registration_lock); + if (READ_ONCE(xe->hwmon) =3D=3D hwmon && + hwmon->fan_refresh_pending && + (xe->info.late_bind_mask & BIT(XE_LB_FW_FAN_CONTROL))) { + hwmon->fan_worker_poll_count =3D 0; + xe_dbg(xe, "restart fan firmware provisioning checks\n"); + mod_delayed_work(system_dfl_wq, &hwmon->fan_post_lb_work, + XE_FAN_WORKER_POLL_INTERVAL); + } + mutex_unlock(&hwmon->registration_lock); + } +=20 + /** + * xe_hwmon_suspend_abort - Restart fan provisioning checks after failed = suspend + * @xe: xe device instance + */ + void xe_hwmon_suspend_abort(struct xe_device *xe) + { + struct xe_hwmon *hwmon =3D READ_ONCE(xe->hwmon); +=20 + if (!hwmon || !xe->info.has_fan_control) + return; +=20 + restart_fan_refresh(hwmon); + } +=20 + /** + * xe_hwmon_resume - Restore hwmon state after resume + * @xe: xe device instance + * @runtime: true for runtime resume, false for system resume or FLR done + * + * Restore any hwmon runtime state that was saved across suspend. + */ + void xe_hwmon_resume(struct xe_device *xe, bool runtime) + { + struct xe_hwmon *hwmon =3D READ_ONCE(xe->hwmon); + int fan; +=20 + if (!hwmon || !xe->info.has_fan_control) + return; +=20 + restart_fan_refresh(hwmon); +=20 + if (runtime && !xe->d3cold.allowed) { + xe_dbg(xe, "d3cold not allowed, skipping fan state restore\n"); + return; + } +=20 + guard(mutex)(&hwmon->hwmon_lock); +=20 + if (hwmon->tjmax_fan_control_state !=3D XE_TJMAX_FAN_CONTROL_UNKNOWN) { + bool enable =3D hwmon->tjmax_fan_control_state =3D=3D XE_TJMAX_FAN_CONT= ROL_ENABLED; + int ret =3D xe_pcode_write(xe_device_get_root_tile(xe), + PCODE_MBOX(FAN_SPEED_CONTROL, + FSC_WRITE_TJMAX_FAN_CONTROL_MODE, 0), + enable); + if (ret) + xe_warn(xe, "failed to restore fan tjmax control, ret=3D%d\n", ret); + else + hwmon->tjmax_fan_control_state =3D XE_TJMAX_FAN_CONTROL_UNKNOWN; + } +=20 + for (fan =3D 0; fan < FAN_MAX; fan++) { + struct xe_hwmon_fan_info *fi =3D &hwmon->fi[fan]; + int ret; +=20 + if (!IS_FAN_ENABLED(hwmon, fan)) + continue; +=20 + if (!fi->restore_user_table) { + xe_dbg(xe, "fan %d user table restore not required\n", fan); + continue; + } +=20 + /* Re-program cached user-table settings for fans active before suspend= =2E */ + ret =3D xe_hwmon_activate_user_fan_table(hwmon, fan, + fi->pwm_enable_mode =3D=3D XE_FAN_PWM_FULL_SPEED, + USER_FAN_TABLE); + fi->restore_user_table =3D false; + if (ret) { + xe_warn(xe, "failed to restore fan %d user table after resume, ret=3D%= d\n", + fan, ret); + continue; + } +=20 + xe_dbg(xe, "fan %d user table restored\n", fan); + } + } +=20 + static void xe_hwmon_refresh_fan_data(struct xe_hwmon *hwmon) + { + struct xe_device *xe =3D hwmon->xe; + int fan; +=20 + guard(mutex)(&hwmon->registration_lock); + if (READ_ONCE(xe->hwmon) !=3D hwmon) + return; +=20 + guard(mutex)(&hwmon->hwmon_lock); +=20 + if (xe_hwmon_read_fan_control_info(hwmon)) + xe_warn(hwmon->xe, "failed to read stock fan tables after late binding\= n"); +=20 + for (fan =3D 0; fan < FAN_MAX; fan++) { + struct xe_hwmon_fan_info *fi =3D &hwmon->fi[fan]; + int ret; +=20 + if (!IS_FAN_ENABLED(hwmon, fan) || + (fi->pwm_enable_mode !=3D XE_FAN_PWM_MANUAL_USER_TABLE && + fi->pwm_enable_mode !=3D XE_FAN_PWM_FULL_SPEED)) + continue; +=20 + ret =3D xe_hwmon_activate_user_fan_table(hwmon, fan, + fi->pwm_enable_mode =3D=3D XE_FAN_PWM_FULL_SPEED, + USER_FAN_TABLE); + if (ret) + xe_warn(hwmon->xe, + "failed to restore fan %d user table after late binding, ret=3D%d\n", + fan, ret); + } + } +=20 + static void fan_post_lb_worker(struct work_struct *work) + { + struct xe_hwmon *hwmon =3D container_of(to_delayed_work(work), + struct xe_hwmon, fan_post_lb_work); + struct xe_device *xe =3D hwmon->xe; + const char *retry_reason; + u32 status =3D 0; + bool active; + int ret =3D 0; +=20 + active =3D xe_pm_runtime_get_if_active(xe); + if (!active) { + retry_reason =3D "device is not runtime active"; + goto retry; + } +=20 + ret =3D xe_pcode_read(xe_device_get_root_tile(xe), + PCODE_MBOX(PCODE_LATE_BINDING, GET_CAPABILITY_STATUS, 0), + &status, NULL); + if (ret) { + retry_reason =3D "failed to read fan firmware provisioning status"; + goto retry; + } +=20 + if (!(status & V1_FAN_SUPPORTED)) { + xe_dbg(xe, "fan firmware does not support fan control (status=3D%#x)\n"= , status); + mutex_lock(&hwmon->registration_lock); + hwmon->fan_refresh_pending =3D false; + mutex_unlock(&hwmon->registration_lock); + goto out; + } +=20 + if (status & V1_FAN_PROVISIONED) { + xe_dbg(xe, "fan firmware provisioned (status=3D%#x); refreshing fan dat= a\n", status); + mutex_lock(&hwmon->registration_lock); + hwmon->fan_refresh_pending =3D false; + mutex_unlock(&hwmon->registration_lock); + xe_hwmon_refresh_fan_data(hwmon); + goto out; + } +=20 + retry_reason =3D "fan firmware not provisioned yet"; + retry: + mutex_lock(&hwmon->registration_lock); + if (READ_ONCE(xe->hwmon) =3D=3D hwmon && + ++hwmon->fan_worker_poll_count < XE_FAN_WORKER_POLL_RETRIES) { + xe_dbg(xe, "retry fan firmware provisioning check: %s (ret=3D%d, status= =3D%#x, poll %u/%u)\n", + retry_reason, ret, status, hwmon->fan_worker_poll_count, + XE_FAN_WORKER_POLL_RETRIES); + mod_delayed_work(system_dfl_wq, &hwmon->fan_post_lb_work, + XE_FAN_WORKER_POLL_INTERVAL); + } else if (READ_ONCE(xe->hwmon) =3D=3D hwmon) { + xe_dbg(xe, "timed out waiting for fan firmware provisioning\n"); + } + mutex_unlock(&hwmon->registration_lock); + out: + if (active) + xe_pm_runtime_put(xe); + } +=20 MODULE_IMPORT_NS("INTEL_PMT_TELEMETRY"); diff --combined drivers/gpu/drm/xe/xe_hwmon.h index e67e0134f02a5,4eba2bbcc4692..0000000000000 --- a/drivers/gpu/drm/xe/xe_hwmon.h +++ b/drivers/gpu/drm/xe/xe_hwmon.h @@@ -13,9 -13,15 +13,15 @@@ struct xe_device #if IS_REACHABLE(CONFIG_HWMON) int xe_hwmon_register(struct xe_device *xe); void xe_hwmon_unregister(struct xe_device *xe); + void xe_hwmon_suspend(struct xe_device *xe, bool runtime); + void xe_hwmon_suspend_abort(struct xe_device *xe); + void xe_hwmon_resume(struct xe_device *xe, bool runtime); #else static inline int xe_hwmon_register(struct xe_device *xe) { return 0; }; static inline void xe_hwmon_unregister(struct xe_device *xe) { } + static inline void xe_hwmon_suspend(struct xe_device *xe, bool runtime) {} + static inline void xe_hwmon_suspend_abort(struct xe_device *xe) {} + static inline void xe_hwmon_resume(struct xe_device *xe, bool runtime) {} #endif =20 #endif /* _XE_HWMON_H_ */ diff --combined drivers/gpu/drm/xe/xe_migrate.c index a7f91ce4ca944,23575f9dc651c..0000000000000 --- a/drivers/gpu/drm/xe/xe_migrate.c +++ b/drivers/gpu/drm/xe/xe_migrate.c @@@ -44,6 -44,7 +44,7 @@@ #include "xe_svm.h" #include "xe_sync.h" #include "xe_trace_bo.h" + #include "xe_ttm_stolen_mgr.h" #include "xe_validation.h" #include "xe_vm.h" #include "xe_vram.h" @@@ -238,7 -239,9 +239,9 @@@ struct xe_migrate=20 u64 large_page_copy_pdes; /** * @fence: dma-fence representing the last migration job batch. - * Protected by @job_mutex. + * Updated under @job_mutex. Readers must take a reference while holding + * the mutex before using the fence outside the critical section. The + * stored reference is released during final teardown. */ struct dma_fence *fence; /** @min_chunk_size: For dgfx, Minimum chunk size */ @@@ -258,6 -261,7 +261,7 @@@ =20 #define MAX_PREEMPTDISABLE_TRANSFER SZ_8M /* Around 1ms. */ #define MAX_CCS_LIMITED_TRANSFER SZ_4M /* XE_PAGE_SIZE * (FIELD_MAX(XE2_C= CS_SIZE_MASK) + 1) */ + #define MAX_IDENTITY_CLEAR_TRANSFER SZ_128M #define NUM_PT_SLOTS 48 #define LEVEL0_PAGE_TABLE_ENCODE_SIZE SZ_2M #define MAX_NUM_PTE 512 @@@ -284,9 -288,15 +288,15 @@@ static void xe_migrate_ulls_toggle_enab m->ulls.enabled =3D enabled; } =20 - static void xe_migrate_fini(void *arg) + static void xe_migrate_fence_set(struct xe_migrate *m, struct dma_fence *= fence) + { + lockdep_assert_held(&m->job_mutex); + dma_fence_put(m->fence); + m->fence =3D dma_fence_get(fence); + } +=20 + static void xe_migrate_ulls_fini(struct xe_migrate *m) { - struct xe_migrate *m =3D arg; struct xe_device *xe =3D tile_to_xe(m->tile); =20 disable_delayed_work_sync(&m->ulls.exit_work); @@@ -296,6 -306,13 +306,13 @@@ xe_migrate_ulls_toggle_enable(m, false); } } + } +=20 + static void xe_migrate_fini(void *arg) + { + struct xe_migrate *m =3D arg; +=20 + xe_migrate_ulls_fini(m); =20 xe_vm_lock(m->q->vm, false); xe_bo_unpin(m->pt_bo); @@@ -793,6 -810,42 +810,42 @@@ static void xe_migrate_ulls_exit(struc drm_dev_exit(idx); } =20 + /** + * xe_migrate_flr_prepare() - Prepare migrate context for FLR + * @m: The migration context + */ + void xe_migrate_flr_prepare(struct xe_migrate *m) + { + struct xe_device *xe =3D tile_to_xe(m->tile); +=20 + if (xe->info.has_usm) { + flush_workqueue(xe->usm.prefetch_wq); + flush_workqueue(xe->usm.pagefault_wq); + } +=20 + xe_migrate_ulls_fini(m); + } +=20 + /** + * xe_migrate_reinit() - Re-initialize a migrate context + * @m: The migration context + * + * Returns: 0 on success, negative error code otherwise. + */ + int xe_migrate_reinit(struct xe_migrate *m) + { + int err; +=20 + xe_migrate_prepare_vm(m->tile, m, m->q->vm, NULL); +=20 + err =3D xe_exec_queue_reinit(m->q); + if (err) + return err; +=20 + enable_delayed_work(&m->ulls.exit_work); + return 0; + } +=20 /** * xe_migrate_init() - Initialize a migrate context * @m: The migration context @@@ -916,6 -969,10 +969,10 @@@ static u64 xe_migrate_res_sizes(struct=20 =20 static bool xe_migrate_allow_identity(u64 size, const struct xe_res_curso= r *cur) { + /* Stolen is not mapped into identity mapping */ + if (cur->mem_type =3D=3D XE_PL_STOLEN) + return false; +=20 /* If the chunk is not fragmented, allow identity map. */ return cur->size >=3D size; } @@@ -997,26 -1054,23 +1054,23 @@@ static void emit_pte(struct xe_migrate=20 =20 while (chunk--) { u64 addr, flags =3D 0; - bool devmem =3D false; =20 addr =3D xe_res_dma(cur) & PAGE_MASK; - if (is_vram) { - if (vm->flags & XE_VM_FLAG_64K) { - u64 va =3D cur_ofs * XE_PAGE_SIZE / 8; + /* Stolen sysmem also needs gpu offset */ + addr +=3D vram_region_gpu_offset(res); =20 - xe_assert(xe, (va & (SZ_64K - 1)) =3D=3D - (addr & (SZ_64K - 1))); + if (is_vram && vm->flags & XE_VM_FLAG_64K) { + u64 va =3D cur_ofs * XE_PAGE_SIZE / 8; =20 - flags |=3D XE_PTE_PS64; - } + xe_assert(xe, (va & (SZ_64K - 1)) =3D=3D + (addr & (SZ_64K - 1))); =20 - addr +=3D vram_region_gpu_offset(res); - devmem =3D true; + flags |=3D XE_PTE_PS64; } =20 addr =3D vm->pt_ops->pte_encode_addr(m->tile->xe, addr, pat_index, - 0, devmem, flags); + 0, is_vram, flags); bb->cs[bb->len++] =3D lower_32_bits(addr); bb->cs[bb->len++] =3D upper_32_bits(addr); =20 @@@ -1221,13 -1275,32 +1275,32 @@@ static u32 xe_migrate_ccs_copy(struct x return flush_flags; } =20 + static bool is_devmem(struct xe_bo *bo, struct ttm_resource *res) + { + if (mem_type_is_vram(res->mem_type)) + return true; +=20 + /* + * Stolen is only device memory on newer platforms. + * + * On older platforms where stolen is mapped to GGTT, + * it is mapped as system memory. + */ + if (res->mem_type !=3D XE_PL_STOLEN || + xe_ttm_stolen_cpu_access_needs_ggtt(xe_bo_device(bo))) + return false; +=20 + return true; + } +=20 static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, struct xe_bo *src_bo, struct xe_bo *dst_bo, struct ttm_resource *src, struct ttm_resource *dst, bool copy_only_ccs, - bool is_vram_resolve) + bool is_vram_resolve, + bool intr) { struct xe_gt *gt =3D m->tile->primary_gt; struct xe_device *xe =3D gt_to_xe(gt); @@@ -1241,8 -1314,8 +1314,8 @@@ int err; bool src_is_pltt =3D src->mem_type =3D=3D XE_PL_TT; bool dst_is_pltt =3D dst->mem_type =3D=3D XE_PL_TT; - bool src_is_vram =3D mem_type_is_vram(src->mem_type); - bool dst_is_vram =3D mem_type_is_vram(dst->mem_type); + bool src_is_vram =3D is_devmem(src_bo, src); + bool dst_is_vram =3D is_devmem(dst_bo, dst); bool type_device =3D src_bo->ttm.type =3D=3D ttm_bo_type_device; bool needs_ccs_emit =3D type_device && xe_migrate_needs_ccs_emit(xe); bool copy_ccs =3D xe_device_has_flat_ccs(xe) && @@@ -1265,11 -1338,11 +1338,11 @@@ if (src_bo !=3D dst_bo && XE_WARN_ON(xe_bo_size(src_bo) !=3D xe_bo_size(= dst_bo))) return ERR_PTR(-EINVAL); =20 - if (!src_is_vram) + if (!src_is_vram && src->mem_type !=3D XE_PL_STOLEN) xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it); else xe_res_first(src, 0, size, &src_it); - if (!dst_is_vram) + if (!dst_is_vram && dst->mem_type !=3D XE_PL_STOLEN) xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it); else xe_res_first(dst, 0, size, &dst_it); @@@ -1329,7 -1402,7 +1402,7 @@@ batch_size +=3D ((copy_only_ccs) ? 0 : emit_copy_cmd_len(xe)) + ((needs_ccs_emit ? EMIT_COPY_CCS_DW : 0)); =20 - bb =3D xe_bb_new(gt, batch_size, usm); + bb =3D __xe_bb_new(gt, batch_size, usm, !fence && intr); if (IS_ERR(bb)) { err =3D PTR_ERR(bb); goto err_sync; @@@ -1386,8 -1459,7 +1459,7 @@@ dma_fence_put(fence); fence =3D xe_migrate_job_push(m, job); =20 - dma_fence_put(m->fence); - m->fence =3D dma_fence_get(fence); + xe_migrate_fence_set(m, fence); =20 mutex_unlock(&m->job_mutex); =20 @@@ -1423,6 -1495,7 +1495,7 @@@ err_sync * @src: The source TTM resource. * @dst: The dst TTM resource. * @copy_only_ccs: If true copy only CCS metadata + * @intr: Whether to perform any wait interruptibly * * Copies the contents of @src to @dst: On flat CCS devices, * the CCS metadata is copied as well if needed, or if not present, @@@ -1437,9 -1510,10 +1510,10 @@@ struct dma_fence *xe_migrate_copy(struc struct xe_bo *dst_bo, struct ttm_resource *src, struct ttm_resource *dst, - bool copy_only_ccs) + bool copy_only_ccs, + bool intr) { - return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, copy_only_ccs, fal= se); + return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, copy_only_ccs, fal= se, intr); } =20 /** @@@ -1457,7 -1531,7 +1531,7 @@@ struct dma_fence *xe_migrate_resolve(st struct xe_bo *bo, struct ttm_resource *res) { - return __xe_migrate_copy(m, bo, bo, res, res, false, true); + return __xe_migrate_copy(m, bo, bo, res, res, false, true, true); } =20 /** @@@ -1815,8 -1889,7 +1889,7 @@@ struct dma_fence *xe_migrate_vram_copy_ dma_fence_put(fence); fence =3D xe_migrate_job_push(m, job); =20 - dma_fence_put(m->fence); - m->fence =3D dma_fence_get(fence); + xe_migrate_fence_set(m, fence); } =20 xe_bb_free(bb, fence); @@@ -1927,6 -2000,7 +2000,7 @@@ static void emit_clear(struct xe_gt *gt * @bo: The buffer object @dst is currently bound to. * @dst: The dst TTM resource to be cleared. * @clear_flags: flags to specify which data to clear: CCS, BO, or both. + * @intr: Whether to perform any wait interruptibly. * * Clear the contents of @dst to zero when XE_MIGRATE_CLEAR_FLAG_BO_DATA = is set. * On flat CCS devices, the CCS metadata is cleared to zero with XE_MIGRA= TE_CLEAR_FLAG_CCS_DATA. @@@ -1940,9 -2014,10 +2014,10 @@@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m, struct xe_bo *bo, struct ttm_resource *dst, - u32 clear_flags) + u32 clear_flags, + bool intr) { - bool clear_vram =3D mem_type_is_vram(dst->mem_type); + bool clear_vram =3D is_devmem(bo, dst); bool clear_bo_data =3D XE_MIGRATE_CLEAR_FLAG_BO_DATA & clear_flags; bool clear_ccs =3D XE_MIGRATE_CLEAR_FLAG_CCS_DATA & clear_flags; struct xe_gt *gt =3D m->tile->primary_gt; @@@ -1960,7 -2035,7 +2035,7 @@@ if (!clear_bo_data && clear_ccs && !IS_DGFX(xe)) clear_only_system_ccs =3D true; =20 - if (!clear_vram) + if (!clear_vram && dst->mem_type !=3D XE_PL_STOLEN) xe_res_first_sg(xe_bo_sg(bo), 0, xe_bo_size(bo), &src_it); else xe_res_first(src, 0, xe_bo_size(bo), &src_it); @@@ -1979,6 -2054,11 +2054,11 @@@ u32 avail_pts =3D max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENC= ODE_SIZE; =20 clear_L0 =3D xe_migrate_res_sizes(m, &src_it); + if (clear_vram && !xe_migrate_needs_ccs_emit(xe) && + gt->info.has_xe2_blt_instructions && + xe_migrate_allow_identity(clear_L0, &src_it)) + clear_L0 =3D min_t(u64, MAX_IDENTITY_CLEAR_TRANSFER, + src_it.size); =20 /* Calculate final sizes and batch size.. */ pte_flags =3D clear_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0; @@@ -1996,7 -2076,7 +2076,7 @@@ if (WARN_ON_ONCE(!clear_L0)) break; =20 - bb =3D xe_bb_new(gt, batch_size, usm); + bb =3D __xe_bb_new(gt, batch_size, usm, !fence && intr); if (IS_ERR(bb)) { err =3D PTR_ERR(bb); goto err_sync; @@@ -2050,8 -2130,7 +2130,7 @@@ dma_fence_put(fence); fence =3D xe_migrate_job_push(m, job); =20 - dma_fence_put(m->fence); - m->fence =3D dma_fence_get(fence); + xe_migrate_fence_set(m, fence); =20 mutex_unlock(&m->job_mutex); =20 @@@ -2105,9 -2184,9 +2184,9 @@@ void xe_migrate_wait(struct xe_migrate=20 { struct dma_fence *fence; =20 - mutex_lock(&m->job_mutex); - fence =3D dma_fence_get(m->fence); - mutex_unlock(&m->job_mutex); + scoped_guard(mutex, &m->job_mutex) { + fence =3D dma_fence_get(m->fence); + } =20 if (fence) { dma_fence_wait(fence, false); @@@ -2212,17 -2291,18 +2291,18 @@@ static bool xe_migrate_vram_use_pde(str #define XE_CACHELINE_BYTES 64ull #define XE_CACHELINE_MASK (XE_CACHELINE_BYTES - 1) =20 - static u32 xe_migrate_copy_pitch(struct xe_device *xe, u32 len) + static u32 xe_migrate_copy_pitch(struct xe_device *xe, u32 len, u64 dst_a= ddr) { + u64 align_check =3D len | dst_addr; u32 pitch; =20 - if (IS_ALIGNED(len, PAGE_SIZE)) + if (IS_ALIGNED(align_check, PAGE_SIZE)) pitch =3D PAGE_SIZE; - else if (IS_ALIGNED(len, SZ_4K)) + else if (IS_ALIGNED(align_check, SZ_4K)) pitch =3D SZ_4K; - else if (IS_ALIGNED(len, SZ_256)) + else if (IS_ALIGNED(align_check, SZ_256)) pitch =3D SZ_256; - else if (IS_ALIGNED(len, 4)) + else if (IS_ALIGNED(align_check, 4)) pitch =3D 4; else pitch =3D 1; @@@ -2231,6 -2311,86 +2311,86 @@@ return pitch; } =20 + /** + * xe_migrate_dst_pitch() - Determine max pitch supported by dst alignment + * @dst_addr: Destination address (page offset) + * + * Returns the largest pitch (PAGE_SIZE, SZ_4K, SZ_256, or 4) that dst_ad= dr + * supports based solely on its alignment. Returns 1 if not even 4-byte a= ligned. + */ + static u32 xe_migrate_dst_pitch(u64 dst_addr) + { + if (IS_ALIGNED(dst_addr, PAGE_SIZE)) + return PAGE_SIZE; + if (IS_ALIGNED(dst_addr, SZ_4K)) + return SZ_4K; + if (IS_ALIGNED(dst_addr, SZ_256)) + return SZ_256; + if (IS_ALIGNED(dst_addr, 4)) + return 4; + return 1; + } +=20 + /** + * xe_migrate_bytes_to_align() - Calculate bytes needed to reach target a= lignment + * @dst_addr: Current destination address + * @alignment: Target alignment (PAGE_SIZE, SZ_4K, SZ_256, or 4) + * + * Returns: Number of bytes to copy to reach the target alignment. + */ + static u32 xe_migrate_bytes_to_align(u64 dst_addr, u32 alignment) + { + return round_up(dst_addr, alignment) - dst_addr; + } +=20 + /** + * xe_migrate_best_alignment() - Find the best alignment reachable within= max_bytes + * @dst_addr: Current destination address + * @max_bytes: Maximum bytes we can copy to reach alignment (typically U1= 6_MAX) + * + * Returns the largest alignment (PAGE_SIZE, SZ_4K, SZ_256, or 4) that ds= t_addr + * can reach by copying at most max_bytes. Returns 0 if already at best a= lignment + * or if no useful alignment can be reached. + */ + static u32 xe_migrate_best_alignment(u64 dst_addr, u32 max_bytes) + { + u32 bytes_to_page =3D xe_migrate_bytes_to_align(dst_addr, PAGE_SIZE); + u32 bytes_to_4k =3D xe_migrate_bytes_to_align(dst_addr, SZ_4K); + u32 bytes_to_256 =3D xe_migrate_bytes_to_align(dst_addr, SZ_256); + u32 bytes_to_4 =3D xe_migrate_bytes_to_align(dst_addr, 4); +=20 + if (bytes_to_page =3D=3D 0) + return 0; +=20 + if (bytes_to_page <=3D max_bytes) + return PAGE_SIZE; +=20 + if (bytes_to_4k > 0 && bytes_to_4k <=3D max_bytes) + return SZ_4K; +=20 + if (bytes_to_256 > 0 && bytes_to_256 <=3D max_bytes) + return SZ_256; +=20 + if (bytes_to_4 > 0 && bytes_to_4 <=3D max_bytes) + return 4; +=20 + return 0; + } +=20 + /** + * xe_migrate_aligned_copy_len() - Calculate the aligned copy length for = matrix copy + * @len: Remaining bytes + * @pitch: The pitch to use for matrix copy + * + * Returns: Length rounded down to pitch, or 0 if less than pitch. + */ + static u32 xe_migrate_aligned_copy_len(u32 len, u32 pitch) + { + if (len < pitch) + return 0; + return round_down(len, pitch); + } +=20 static struct dma_fence *xe_migrate_vram(struct xe_migrate *m, unsigned long len, unsigned long sram_offset, @@@ -2249,16 -2409,11 +2409,11 @@@ struct xe_bb *bb; u32 update_idx, pt_slot =3D 0; unsigned long npages =3D DIV_ROUND_UP(len + sram_offset, PAGE_SIZE); - unsigned int pitch =3D xe_migrate_copy_pitch(xe, len); + unsigned int pitch; int err; unsigned long i, j; bool use_pde =3D xe_migrate_vram_use_pde(sram_addr, len + sram_offset); =20 - if (!xe->info.has_mem_copy_instr && - drm_WARN_ON(&xe->drm, - (!IS_ALIGNED(len, pitch)) || (sram_offset | vram_addr) & XE_CACHELINE_= MASK)) - return ERR_PTR(-EOPNOTSUPP); -=20 xe_assert(xe, npages * PAGE_SIZE <=3D MAX_PREEMPTDISABLE_TRANSFER); =20 batch_size +=3D pte_update_cmd_size(npages << PAGE_SHIFT); @@@ -2310,6 -2465,15 +2465,15 @@@ dst_L0_ofs =3D xe_migrate_vm_addr(pt_slot, 0) + sram_offset; } =20 + pitch =3D xe_migrate_copy_pitch(xe, len, dst_L0_ofs); + if (!xe->info.has_mem_copy_instr && + drm_WARN_ON(&xe->drm, + (!IS_ALIGNED(len, pitch)) || + (sram_offset | vram_addr) & XE_CACHELINE_MASK)) { + err =3D -EOPNOTSUPP; + goto err; + } +=20 bb->cs[bb->len++] =3D MI_BATCH_BUFFER_END; update_idx =3D bb->len; =20 @@@ -2333,12 -2497,10 +2497,10 @@@ err =3D 0; } =20 - mutex_lock(&m->job_mutex); - fence =3D xe_migrate_job_push(m, job); -=20 - dma_fence_put(m->fence); - m->fence =3D dma_fence_get(fence); - mutex_unlock(&m->job_mutex); + scoped_guard(mutex, &m->job_mutex) { + fence =3D xe_migrate_job_push(m, job); + xe_migrate_fence_set(m, fence); + } =20 xe_bb_free(bb, fence); =20 @@@ -2551,8 -2713,8 +2713,8 @@@ int xe_migrate_access_memory(struct xe_ struct dma_fence *__fence; u64 vram_addr =3D vram_region_gpu_offset(bo->ttm.resource) + cursor.start; + unsigned long sram_offset =3D (unsigned long)buf & ~PAGE_MASK; int current_bytes; - u32 pitch; =20 if (cursor.size > MAX_PREEMPTDISABLE_TRANSFER) current_bytes =3D min_t(int, bytes_left, @@@ -2560,16 -2722,67 +2722,67 @@@ else current_bytes =3D min_t(int, bytes_left, cursor.size); =20 - pitch =3D xe_migrate_copy_pitch(xe, current_bytes); - if (xe->info.has_mem_copy_instr) - current_bytes =3D min_t(int, current_bytes, U16_MAX * pitch); - else - current_bytes =3D min_t(int, current_bytes, - round_down(S16_MAX * pitch, - XE_CACHELINE_BYTES)); + current_bytes =3D min_t(int, current_bytes, + MAX_PREEMPTDISABLE_TRANSFER - sram_offset); =20 - __fence =3D xe_migrate_vram(m, current_bytes, - (unsigned long)buf & ~PAGE_MASK, + { + /* + * Optimized copy strategy to minimize number of jobs: + * 1. Linear copy to align dst to best reachable alignment + * 2. Matrix copy for bulk data copy + * 3. Single linear copy for remaining bytes + * + * The phase logic controls transfer chunking; emit_copy() selects + * MEM_COPY_CMD or XY_FAST_COPY_BLT based on hardware support. + */ + u64 dst_addr =3D write ? vram_addr : + (unsigned long)buf & ~PAGE_MASK; + u32 max_linear =3D xe->info.has_mem_copy_instr ? U16_MAX : + round_down(S16_MAX, XE_CACHELINE_BYTES); + u32 best_align =3D xe_migrate_best_alignment(dst_addr, max_linear); + u32 bytes_to_align =3D best_align ? + xe_migrate_bytes_to_align(dst_addr, best_align) : 0; +=20 + if (bytes_to_align > 0 && bytes_to_align <=3D (u32)current_bytes) { + /* + * Phase 1: Linear copy to reach best alignment. + * Copy just enough bytes to align dst_addr. + */ + current_bytes =3D min_t(int, bytes_to_align, max_linear); + } else if (IS_ALIGNED(dst_addr, 4)) { + /* + * Phase 2: Matrix copy for aligned bulk data. + * Use the best pitch that dst alignment supports, + * then round down len to that pitch. + */ + u32 dst_pitch =3D xe_migrate_dst_pitch(dst_addr); + u32 aligned_len =3D xe_migrate_aligned_copy_len(current_bytes, + dst_pitch); +=20 + if (aligned_len >=3D dst_pitch) { + u32 max_bytes =3D xe->info.has_mem_copy_instr ? + U16_MAX * dst_pitch : + round_down(S16_MAX * dst_pitch, + XE_CACHELINE_BYTES); +=20 + current_bytes =3D min_t(int, aligned_len, max_bytes); + } else { + /* + * Phase 3: Linear copy for remainder. + * Not enough for matrix copy - do single linear copy. + */ + current_bytes =3D min_t(int, current_bytes, max_linear); + } + } else { + /* + * Phase 3: Linear copy for remainder. + * dst not 4-aligned and can't reach alignment - linear copy. + */ + current_bytes =3D min_t(int, current_bytes, max_linear); + } + } +=20 + __fence =3D xe_migrate_vram(m, current_bytes, sram_offset, &pagemap_addr[current_page], vram_addr, NULL, write ? XE_MIGRATE_COPY_TO_VRAM : @@@ -2588,7 -2801,7 +2801,7 @@@ =20 buf +=3D current_bytes; offset +=3D current_bytes; - current_page =3D (int)(buf - orig_buf) / PAGE_SIZE; + current_page =3D (int)(buf - orig_buf + page_offset) / PAGE_SIZE; bytes_left -=3D current_bytes; if (bytes_left) xe_res_next(&cursor, current_bytes); diff --combined drivers/gpu/drm/xe/xe_oa.c index 38522d156ba67,2b88f3fa210d9..0000000000000 --- a/drivers/gpu/drm/xe/xe_oa.c +++ b/drivers/gpu/drm/xe/xe_oa.c @@@ -43,6 -43,7 +43,7 @@@ #include "xe_sriov.h" #include "xe_sync.h" #include "xe_wa.h" + #include "xe_wedge.h" =20 #define DEFAULT_POLL_FREQUENCY_HZ 200 #define DEFAULT_POLL_PERIOD_NS (NSEC_PER_SEC / DEFAULT_POLL_FREQUENCY_HZ) @@@ -260,13 -261,18 +261,18 @@@ static bool mert_wa_14026633728(struct=20 =20 static bool xe_oa_buffer_check_unlocked(struct xe_oa_stream *stream) { - u32 gtt_offset =3D xe_bo_ggtt_addr(stream->oa_buffer.bo); - u32 tail, hw_tail, partial_report_size, available; + u32 gtt_offset, tail, hw_tail, partial_report_size, available; int report_size =3D stream->oa_buffer.format->size; unsigned long flags; =20 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags); =20 + if (stream->dead) { + spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags); + return false; + } +=20 + gtt_offset =3D xe_bo_ggtt_addr(stream->oa_buffer.bo); hw_tail =3D xe_oa_hw_tail_read(stream); hw_tail -=3D gtt_offset; =20 @@@ -327,6 -333,9 +333,9 @@@ static enum hrtimer_restart xe_oa_poll_ struct xe_oa_stream *stream =3D container_of(hrtimer, typeof(*stream), poll_check_timer); =20 + if (READ_ONCE(stream->dead)) + return HRTIMER_NORESTART; +=20 if (xe_oa_buffer_check_unlocked(stream)) wake_up(&stream->poll_wq); =20 @@@ -538,12 -547,19 +547,19 @@@ static void xe_oa_disable(struct xe_oa_ =20 static int xe_oa_wait_unlocked(struct xe_oa_stream *stream) { + int ret; +=20 /* We might wait indefinitely if periodic sampling is not enabled */ if (!stream->periodic) return -EINVAL; =20 - return wait_event_interruptible(stream->poll_wq, - xe_oa_buffer_check_unlocked(stream)); + ret =3D wait_event_interruptible(stream->poll_wq, + READ_ONCE(stream->dead) || + xe_oa_buffer_check_unlocked(stream)); + if (ret) + return ret; +=20 + return READ_ONCE(stream->dead) ? -ENODEV : 0; } =20 #define OASTATUS_RELEVANT_BITS (OASTATUS_MMIO_TRG_Q_FULL | OASTATUS_COUNT= ER_OVERFLOW | \ @@@ -572,6 -588,9 +588,9 @@@ static ssize_t xe_oa_read(struct file * size_t offset =3D 0; int ret; =20 + if (READ_ONCE(stream->dead)) + return -ENODEV; +=20 if (!stream->sample) return -EINVAL; =20 @@@ -582,13 -601,14 +601,14 @@@ return ret; =20 mutex_lock(&stream->stream_lock); - ret =3D __xe_oa_read(stream, buf, count, &offset); + ret =3D stream->dead ? -ENODEV : + __xe_oa_read(stream, buf, count, &offset); mutex_unlock(&stream->stream_lock); } while (!offset && !ret); } else { xe_oa_buffer_check_unlocked(stream); mutex_lock(&stream->stream_lock); - ret =3D __xe_oa_read(stream, buf, count, &offset); + ret =3D stream->dead ? -ENODEV : __xe_oa_read(stream, buf, count, &offs= et); mutex_unlock(&stream->stream_lock); } =20 @@@ -615,6 -635,9 +635,9 @@@ static __poll_t xe_oa_poll_locked(struc =20 poll_wait(file, &stream->poll_wq, wait); =20 + if (stream->dead) + return EPOLLERR | EPOLLHUP; +=20 /* * We don't explicitly check whether there's something to read here sinc= e this * path may be hot depending on what else userspace is polling, or on th= e timeout @@@ -741,6 -764,12 +764,12 @@@ static void xe_oa_free_configs(struct x dma_fence_put(stream->last_fence); } =20 + static void xe_oa_update_last_fence(struct xe_oa_stream *stream, struct d= ma_fence *fence) + { + dma_fence_put(stream->last_fence); + stream->last_fence =3D dma_fence_get(fence); + } +=20 static int xe_oa_load_with_lri(struct xe_oa_stream *stream, struct xe_oa_= reg *reg_lri, u32 count) { struct dma_fence *fence; @@@ -761,6 -790,7 +790,7 @@@ goto free_bb; } xe_bb_free(bb, fence); + xe_oa_update_last_fence(stream, fence); dma_fence_put(fence); =20 return 0; @@@ -848,6 -878,30 +878,30 @@@ static u32 oag_configure_mmio_trigger(c return REG_MASKED_FIELD_ENABLE(OAG_OA_DEBUG_DISABLE_MMIO_TRG); } =20 + /* + * Wait for the last OA batch of the stream, so that it can't program the= HW after the HW + * teardown. + */ + static void xe_oa_stream_drain(struct xe_oa_stream *stream) + { + struct xe_exec_queue *q =3D stream->exec_q ?: stream->k_exec_q; + long timeout; +=20 + /* Nothing in the ring runs anymore on a wedged device */ + if (xe_wedged(stream->oa->xe) || !stream->last_fence || + dma_fence_is_signaled(stream->last_fence)) + return; +=20 + /* + * A batch waiting for an input sync that doesn't signal isn't covered b= y a TDR and still + * runs once the sync signals, possibly reprogramming the OA config. Don= 't kill the exec + * queue to prevent that, the TDR of a killed kernel exec queue resets t= he GT. + */ + timeout =3D msecs_to_jiffies(q->sched_props.job_timeout_ms) + HZ; + if (!dma_fence_wait_timeout(stream->last_fence, false, timeout)) + drm_err(&stream->oa->xe->drm, "Timed out waiting for OA batch\n"); + } +=20 static void xe_oa_disable_metric_set(struct xe_oa_stream *stream) { struct xe_mmio *mmio =3D &stream->gt->mmio; @@@ -864,9 -918,15 +918,15 @@@ xe_mmio_write32(mmio, __oa_regs(stream)->oa_debug, oag_configure_mmio_trigger(stream, false)); =20 - /* disable the context save/restore or OAR counters */ - if (stream->exec_q) + /* + * disable the context save/restore or OAR counters, and wait for that b= atch like for + * the ones before it. Not on a wedged device, the context won't run aga= in, and the + * batch might never complete on a long-running exec queue, which no TDR= covers. + */ + if (stream->exec_q && !xe_wedged(stream->oa->xe)) { xe_oa_configure_oa_context(stream, false); + xe_oa_stream_drain(stream); + } =20 /* Make sure we disable noa to save power. */ if (GT_VER(stream->gt) < 35) @@@ -894,7 -954,7 +954,7 @@@ static void xe_oa_stream_destroy(struc =20 WRITE_ONCE(u->exclusive_stream, NULL); =20 - mutex_destroy(&stream->stream_lock); + xe_oa_stream_drain(stream); =20 if (stream->sample) xe_reg_dewhitelist_oa_regs(stream->gt); @@@ -902,8 -962,6 +962,6 @@@ xe_oa_disable_metric_set(stream); xe_exec_queue_put(stream->k_exec_q); =20 - xe_oa_free_oa_buffer(stream); -=20 xe_force_wake_put(gt_to_fw(gt), stream->fw_ref); xe_pm_runtime_put(stream->oa->xe); =20 @@@ -1002,12 -1060,6 +1060,6 @@@ out return oa_bo; } =20 - static void xe_oa_update_last_fence(struct xe_oa_stream *stream, struct d= ma_fence *fence) - { - dma_fence_put(stream->last_fence); - stream->last_fence =3D dma_fence_get(fence); - } -=20 static void xe_oa_fence_work_fn(struct work_struct *w) { struct xe_oa_fence *ofence =3D container_of(w, typeof(*ofence), work.wor= k); @@@ -1052,6 -1104,7 +1104,7 @@@ static int xe_oa_emit_oa_config(struct=20 struct xe_oa_fence *ofence; int i, err, num_signal =3D 0; struct dma_fence *fence; + int ret =3D 0; =20 ofence =3D kzalloc_obj(*ofence); if (!ofence) { @@@ -1074,7 -1127,7 +1127,7 @@@ =20 /* Point of no return: initialize and set fence to signal */ spin_lock_init(&ofence->lock); - dma_fence_init(&ofence->base, &xe_oa_fence_ops, &ofence->lock, 0, 0); + dma_fence_init(&ofence->base, &xe_oa_fence_ops, &ofence->lock, 0, 0, 0); =20 for (i =3D 0; i < stream->num_syncs; i++) { if (stream->syncs[i].flags & DRM_XE_SYNC_FLAG_SIGNAL) @@@ -1095,12 -1148,28 +1148,28 @@@ if (err =3D=3D -ENOENT) xe_oa_config_cb(fence, &ofence->cb); =20 - /* If nothing needs to be signaled we wait synchronously */ + /* + * If nothing needs to be signaled we wait synchronously unless the devi= ce + * is unplugged/wedged. + */ if (!num_signal) { - dma_fence_wait(&ofence->base, false); + while (!dma_fence_wait_timeout(&ofence->base, false, HZ / 10)) { + if (drm_dev_is_unplugged(>_to_xe(stream->gt)->drm)) { + ret =3D -ENODEV; + break; + } + if (xe_wedged(gt_to_xe(stream->gt))) { + ret =3D -ECANCELED; + break; + } + } dma_fence_put(&ofence->base); } =20 + /* The callers clean up the syncs on error */ + if (ret) + return ret; +=20 /* Done with syncs */ for (i =3D 0; i < stream->num_syncs; i++) xe_sync_entry_cleanup(&stream->syncs[i]); @@@ -1671,6 -1740,9 +1740,9 @@@ static long xe_oa_ioctl_locked(struct x unsigned int cmd, unsigned long arg) { + if (stream->dead) + return -ENODEV; +=20 switch (cmd) { case DRM_XE_OBSERVATION_IOCTL_ENABLE: return xe_oa_enable_locked(stream); @@@ -1692,39 -1764,87 +1764,87 @@@ static long xe_oa_ioctl(struct file *fi unsigned long arg) { struct xe_oa_stream *stream =3D file->private_data; - long ret; + struct xe_device *xe =3D gt_to_xe(stream->gt); + long ret =3D -ECANCELED; + int idx; =20 - mutex_lock(&stream->stream_lock); - ret =3D xe_oa_ioctl_locked(stream, cmd, arg); - mutex_unlock(&stream->stream_lock); + if (!drm_dev_enter(&xe->drm, &idx)) + return -ENODEV; +=20 + if (!xe_wedged(xe)) { + mutex_lock(&stream->stream_lock); + ret =3D xe_oa_ioctl_locked(stream, cmd, arg); + mutex_unlock(&stream->stream_lock); + } +=20 + drm_dev_exit(idx); =20 return ret; } =20 + /* + * Disable the stream, restore the HW state and release everything but th= e OA buffer and the + * stream struct, the same whether the stream fd is closed or the device = goes away, and + * whether or not the HW still responds: all HW waits are bounded. Afterw= ards the stream is + * dead and any further read, poll, mmap or ioctl on the stream fd fails = with -ENODEV. + */ static void xe_oa_destroy_locked(struct xe_oa_stream *stream) { + unsigned long flags; +=20 + mutex_lock(&stream->stream_lock); +=20 + spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags); + WRITE_ONCE(stream->dead, true); + spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags); +=20 if (stream->enabled) xe_oa_disable_locked(stream); =20 + wake_up_all(&stream->poll_wq); +=20 xe_oa_stream_destroy(stream); =20 if (stream->exec_q) xe_exec_queue_put(stream->exec_q); =20 drm_syncobj_put(stream->ufence_syncobj); - kfree(stream); + mutex_unlock(&stream->stream_lock); } =20 static int xe_oa_release(struct inode *inode, struct file *file) { struct xe_oa_stream *stream =3D file->private_data; struct xe_gt *gt =3D stream->gt; + bool rpm; =20 - xe_pm_runtime_get(gt_to_xe(gt)); + /* + * gt_lock serializes against xe_oa_fini(), which destroys the stream on= device removal. + * Unless it did, the device stays bound while this holds gt_lock, so ta= ke a runtime PM + * reference only then, and don't wake a removed device. Free the OA buf= fer under gt_lock + * too, so that it is unpinned before xe_oa_fini() and xe_bo_pinned_fini= () can run. + */ mutex_lock(>->oa.gt_lock); - xe_oa_destroy_locked(stream); + rpm =3D !stream->dead; + if (rpm) { + xe_pm_runtime_get(gt_to_xe(gt)); + xe_oa_destroy_locked(stream); + } +=20 + xe_oa_free_oa_buffer(stream); + mutex_destroy(&stream->stream_lock); + kfree(stream); + if (rpm) + xe_pm_runtime_put(gt_to_xe(gt)); mutex_unlock(>->oa.gt_lock); - xe_pm_runtime_put(gt_to_xe(gt)); +=20 + /* + * Freeing the OA buffer may defer the BO destroy to the TTM workqueue. = Once the device + * is unplugged, this may be the last reference on the drm device, so le= t the destroy + * finish before drm_dev_put() releases the GGTT. + */ + if (drm_dev_is_unplugged(>_to_xe(gt)->drm)) + flush_workqueue(gt_to_xe(gt)->ttm.wq); =20 /* Release the reference the OA stream kept on the driver */ drm_dev_put(>_to_xe(gt)->drm); @@@ -1735,27 -1855,46 +1855,46 @@@ static int xe_oa_mmap(struct file *file, struct vm_area_struct *vma) { struct xe_oa_stream *stream =3D file->private_data; + struct xe_device *xe =3D gt_to_xe(stream->gt); int ret =3D xe_observation_paranoid_check(); - struct xe_bo *bo =3D stream->oa_buffer.bo; + struct xe_bo *bo; + int idx; =20 if (ret) { - drm_dbg(&stream->oa->xe->drm, "Insufficient privilege to map OA buffer\= n"); + drm_dbg(&xe->drm, "Insufficient privilege to map OA buffer\n"); return ret; } =20 + /* + * drm_dev_unplug() zaps the OA buffer mmaps and waits for this section = to finish, so + * no mmap of the OA buffer can be set up after that. Can't use stream_l= ock here, it is + * held across user copies which fault on mmap_lock, held by the caller. + */ + if (!drm_dev_enter(&xe->drm, &idx)) + return -ENODEV; +=20 + if (READ_ONCE(stream->dead)) { + ret =3D -ENODEV; + goto exit; + } +=20 /* * Userspace always maps the OA buffer at offset 0, the mapping is moved= to the OA * buffer's own range in the drm device's address_space below */ if (vma->vm_pgoff) { - drm_dbg(&stream->oa->xe->drm, "Wrong mmap offset, must be 0\n"); - return -EINVAL; + drm_dbg(&xe->drm, "Wrong mmap offset, must be 0\n"); + ret =3D -EINVAL; + goto exit; } =20 + bo =3D stream->oa_buffer.bo; +=20 /* Can mmap the entire OA buffer or nothing (no partial OA buffer mmaps)= */ if (vma->vm_end - vma->vm_start !=3D xe_bo_size(bo)) { - drm_dbg(&stream->oa->xe->drm, "Wrong mmap size, must be OA buffer size\= n"); - return -EINVAL; + drm_dbg(&xe->drm, "Wrong mmap size, must be OA buffer size\n"); + ret =3D -EINVAL; + goto exit; } =20 /* @@@ -1763,15 -1902,19 +1902,19 @@@ * VM_MAYSHARE, don't copy the vma on fork */ if (vma->vm_flags & (VM_WRITE | VM_EXEC | VM_SHARED | VM_MAYSHARE)) { - drm_dbg(&stream->oa->xe->drm, "mmap must be read only\n"); - return -EINVAL; + drm_dbg(&xe->drm, "mmap must be read only\n"); + ret =3D -EINVAL; + goto exit; } vm_flags_mod(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_DONTCOPY, VM_MAYWRITE | VM_MAYEXEC); =20 vma->vm_pgoff =3D drm_vma_node_start(&bo->ttm.base.vma_node); =20 - return drm_gem_mmap_obj(&bo->ttm.base, xe_bo_size(bo), vma); + ret =3D drm_gem_mmap_obj(&bo->ttm.base, xe_bo_size(bo), vma); + exit: + drm_dev_exit(idx); + return ret; } =20 static const struct file_operations xe_oa_fops =3D { @@@ -1783,6 -1926,19 +1926,19 @@@ .mmap =3D xe_oa_mmap, }; =20 + static bool oag_has_tail_address_wrap(struct xe_oa_stream *stream) + { + return GRAPHICS_VER(stream->oa->xe) >=3D 20 && + stream->oa_unit->type =3D=3D DRM_XE_OA_UNIT_TYPE_OAG; + } +=20 + static bool oam_has_tail_address_wrap(struct xe_oa_stream *stream) + { + return MEDIA_VER(stream->oa->xe) >=3D 30 && + (stream->oa_unit->type =3D=3D DRM_XE_OA_UNIT_TYPE_OAM || + stream->oa_unit->type =3D=3D DRM_XE_OA_UNIT_TYPE_OAM_SAG); + } +=20 static int xe_oa_stream_init(struct xe_oa_stream *stream, struct xe_oa_open_param *param) { @@@ -1807,12 -1963,13 +1963,13 @@@ stream->syncs =3D param->syncs; =20 /* - * For Xe2+, when overrun mode is enabled, there are no partial reports = at the end - * of buffer, making the OA buffer effectively a non-power-of-2 size cir= cular - * buffer whose size, circ_size, is a multiple of the report size + * For Xe2+ OAG and Xe3+ OAM, when overrun mode is enabled, there are no + * partial reports at the end of buffer, making the OA buffer effectivel= y a + * non-power-of-2 size circular buffer whose size, circ_size, is + * a multiple of the report size. */ - if (GRAPHICS_VER(stream->oa->xe) >=3D 20 && - stream->oa_unit->type =3D=3D DRM_XE_OA_UNIT_TYPE_OAG && stream->samp= le) + if (stream->sample && + (oag_has_tail_address_wrap(stream) || oam_has_tail_address_wrap(stre= am))) stream->oa_buffer.circ_size =3D param->oa_buffer_size - param->oa_buffer_size % stream->oa_buffer.format->size; @@@ -1869,6 -2026,7 +2026,7 @@@ return 0; =20 err_put_k_exec_q: + xe_oa_stream_drain(stream); xe_oa_disable_metric_set(stream); xe_exec_queue_put(stream->k_exec_q); err_free_oa_buf: @@@ -1962,6 -2120,8 +2120,8 @@@ err_disable xe_oa_disable_locked(stream); err_destroy: xe_oa_stream_destroy(stream); + xe_oa_free_oa_buffer(stream); + mutex_destroy(&stream->stream_lock); err_free: kfree(stream); err_syncobj: @@@ -2583,6 -2743,49 +2743,49 @@@ static void xe_oa_unregister(void *arg oa->metrics_kobj =3D NULL; } =20 + static void xe_oa_quiesce_gt(struct xe_gt *gt) + { + int i; +=20 + mutex_lock(>->oa.gt_lock); + for (i =3D 0; i < gt->oa.num_oa_units; i++) { + struct xe_oa_stream *stream =3D gt->oa.oa_unit[i].exclusive_stream; +=20 + if (!stream) + continue; +=20 + xe_oa_destroy_locked(stream); +=20 + /* + * The OA buffer stays pinned until the stream fd is released, an mmap = may still + * hold a reference on it. Drop its DMA mapping now: the fd may be rele= ased, and + * the BO unpinned, while xe_bo_pinned_fini() DMA-unmaps it without the= BO lock. + */ + xe_bo_dma_unmap_pinned(stream->oa_buffer.bo); + } + mutex_unlock(>->oa.gt_lock); + } +=20 + /** + * xe_oa_quiesce - Disable all active OA streams on driver unbind or PCI = slot reset + * @xe: @xe_device + * + * Destroys every active stream like closing its fd would, see xe_oa_dest= roy_locked(). + * Only the stream struct and the pinned OA buffer stay allocated until u= serspace closes + * the fd, at which point xe_oa_release() frees them. + */ + static void xe_oa_quiesce(struct xe_device *xe) + { + struct xe_gt *gt; + u8 id; +=20 + if (!xe->oa.xe) + return; +=20 + for_each_gt(gt, xe, id) + xe_oa_quiesce_gt(gt); + } +=20 /** * xe_oa_register - Xe OA registration * @xe: @xe_device @@@ -2894,6 -3097,8 +3097,8 @@@ static void xe_oa_fini(void *arg if (!oa->xe) return; =20 + xe_oa_quiesce(xe); +=20 idr_for_each(&oa->metrics_idr, destroy_config, oa); idr_destroy(&oa->metrics_idr); =20 diff --combined drivers/gpu/drm/xe/xe_pagefault.c index dfaba621aac7e,fdc20ddfac27f..0000000000000 --- a/drivers/gpu/drm/xe/xe_pagefault.c +++ b/drivers/gpu/drm/xe/xe_pagefault.c @@@ -191,6 -191,10 +191,10 @@@ static int xe_pagefault_handle_vma(stru struct xe_userptr_vma *uvma =3D to_userptr_vma(vma); =20 err =3D xe_vma_userptr_pin_pages(uvma); + if (err =3D=3D -EAGAIN) { + cond_resched(); + continue; + } if (err) { xe_pagefault_set_error(pf, XE_PAGEFAULT_ERROR_VMA_USERPTR_PIN); return err; @@@ -225,7 -229,8 +229,8 @@@ } =20 if (bo) { - ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); + scoped_guard(spinlock, &vm->xe->ttm.lru_lock) + xe_bo_bump_lru_locked(bo); } } xe_validation_ctx_fini(&ctx); @@@ -679,13 -684,6 +684,6 @@@ static void xe_pagefault_queue_work(str */ guard(xe_pm_runtime)(xe); =20 - /* - * A live VM holds a PM reference, but a torn-down VM does not. - * Guard the entire worker loop to safely drain stale faults and - * prevent autosuspends from desyncing batched CT flushes. - */ - guard(xe_pm_runtime)(xe); -=20 #define USM_QUEUE_MAX_RUNTIME_MS 20 threshold =3D jiffies + msecs_to_jiffies(USM_QUEUE_MAX_RUNTIME_MS); =20 @@@ -813,7 -811,6 +811,6 @@@ static int xe_pagefault_queue_init(stru drm_dbg(&xe->drm, "xe_pagefault_entry_size=3D%d, total_num_eus=3D%d, pf_= queue->size=3D%u", xe_pagefault_entry_size(), total_num_eus, pf_queue->size); =20 - pf_queue->xe =3D xe; spin_lock_init(&pf_queue->lock); =20 pf_queue->data =3D drmm_kzalloc(&xe->drm, pf_queue->size, GFP_KERNEL); diff --combined drivers/gpu/drm/xe/xe_pci_sriov.c index 3bf9957365026,b0cfa6e46d8b1..0000000000000 --- a/drivers/gpu/drm/xe/xe_pci_sriov.c +++ b/drivers/gpu/drm/xe/xe_pci_sriov.c @@@ -125,24 -125,27 +125,27 @@@ static void pf_finish_vfs_enabling(stru xe_sriov_pf_disarm_guard(xe, &xe->sriov.pf.guard_vfs_enabling, false, NU= LL); } =20 - static int pf_enable_vfs(struct xe_device *xe, int num_vfs) + static void pf_enable_vfs_revert(struct xe_device *xe) { - struct pci_dev *pdev =3D to_pci_dev(xe->drm.dev); - int total_vfs =3D xe_sriov_pf_get_totalvfs(xe); - int err; + pf_resize_vf_vram_bar(xe, xe->sriov.pf.device_total_vfs); =20 - xe_assert(xe, IS_SRIOV_PF(xe)); - xe_assert(xe, num_vfs > 0); - xe_assert(xe, num_vfs <=3D total_vfs); - xe_sriov_dbg(xe, "enabling %u VF%s\n", num_vfs, str_plural(num_vfs)); + /* Release additional PM reference obtained in pf_enable_vfs_prepare() */ + xe_pm_runtime_put(xe); +=20 + pf_finish_vfs_enabling(xe); + } +=20 + static int pf_enable_vfs_prepare(struct xe_device *xe) + { + int err; =20 err =3D xe_sriov_pf_wait_ready(xe); if (err) - goto out; + return err; =20 err =3D pf_prepare_vfs_enabling(xe); if (err) - goto out; + return err; =20 /* * We must hold additional reference to the runtime PM to keep PF in D0 @@@ -151,41 -154,71 +154,71 @@@ * With PF being in D0 state, all VFs will also behave as in D0 state. * This will also keep GuC alive with all VFs' configurations. * - * We will release this additional PM reference in pf_disable_vfs(). + * We will release this additional PM reference in pf_enable_vfs_revert(= ). */ xe_pm_runtime_get_noresume(xe); =20 - err =3D xe_sriov_pf_provision_vfs(xe, num_vfs); - if (err < 0) - goto failed; + return 0; + } +=20 + static int pf_enable_vfs_commit(struct xe_device *xe, int num_vfs) + { + struct pci_dev *pdev =3D to_pci_dev(xe->drm.dev); + int err; +=20 + xe_assert(xe, IS_SRIOV_PF(xe)); + xe_assert(xe, num_vfs > 0); + xe_assert(xe, num_vfs <=3D xe_sriov_pf_get_totalvfs(xe)); =20 pf_resize_vf_vram_bar(xe, num_vfs); =20 err =3D pci_enable_sriov(pdev, num_vfs); if (err < 0) - goto failed; + return err; =20 pf_link_vfs(xe, num_vfs); =20 - xe_sriov_info(xe, "Enabled %u of %u VF%s\n", - num_vfs, total_vfs, str_plural(total_vfs)); -=20 xe_sriov_pf_sysfs_link_vfs(xe, num_vfs); =20 pf_engine_activity_stats(xe, num_vfs, true); =20 return num_vfs; + } =20 - failed: - pf_resize_vf_vram_bar(xe, xe->sriov.pf.device_total_vfs); + static int pf_enable_vfs(struct xe_device *xe, int num_vfs) + { + int total_vfs =3D xe_sriov_pf_get_totalvfs(xe); + int ret; +=20 + xe_assert(xe, IS_SRIOV_PF(xe)); + xe_assert(xe, num_vfs > 0); + xe_assert(xe, num_vfs <=3D total_vfs); + xe_sriov_dbg(xe, "enabling %u VF%s\n", num_vfs, str_plural(num_vfs)); +=20 + ret =3D pf_enable_vfs_prepare(xe); + if (ret < 0) + goto failed; +=20 + ret =3D xe_sriov_pf_provision_vfs(xe, num_vfs); + if (ret < 0) + goto revert; +=20 + ret =3D pf_enable_vfs_commit(xe, num_vfs); + if (ret < 0) + goto revert; +=20 + xe_sriov_info(xe, "Enabled %u of %u VF%s\n", + num_vfs, total_vfs, str_plural(total_vfs)); + return num_vfs; +=20 + revert: xe_sriov_pf_reprovision_default(xe); xe_sriov_pf_unprovision_vfs(xe, num_vfs); - xe_pm_runtime_put(xe); - pf_finish_vfs_enabling(xe); - out: + pf_enable_vfs_revert(xe); + failed: xe_sriov_notice(xe, "Failed to enable %u VF%s (%pe)\n", - num_vfs, str_plural(num_vfs), ERR_PTR(err)); - return err; + num_vfs, str_plural(num_vfs), ERR_PTR(ret)); + return ret; } =20 static int pf_disable_vfs(struct xe_device *xe) @@@ -206,18 -239,13 +239,13 @@@ =20 pci_disable_sriov(pdev); =20 - pf_resize_vf_vram_bar(xe, xe->sriov.pf.device_total_vfs); -=20 xe_sriov_pf_reprovision_default(xe); =20 pf_reset_vfs(xe, num_vfs); =20 xe_sriov_pf_unprovision_vfs(xe, num_vfs); =20 - /* not needed anymore - see pf_enable_vfs() */ - xe_pm_runtime_put(xe); -=20 - pf_finish_vfs_enabling(xe); + pf_enable_vfs_revert(xe); =20 xe_sriov_info(xe, "Disabled %u VF%s\n", num_vfs, str_plural(num_vfs)); return 0; diff --combined drivers/gpu/drm/xe/xe_sa.c index e8908c2973ff3,d9da4276b5dd6..0000000000000 --- a/drivers/gpu/drm/xe/xe_sa.c +++ b/drivers/gpu/drm/xe/xe_sa.c @@@ -29,7 -29,6 +29,6 @@@ static void xe_sa_bo_manager_fini(struc kvfree(sa_manager->cpu_ptr); =20 sa_manager->bo =3D NULL; - sa_manager->shadow =3D NULL; } =20 /** @@@ -38,14 -37,12 +37,12 @@@ * @size: number of bytes to allocate * @guard: number of bytes to exclude from suballocations * @align: alignment for each suballocated chunk - * @flags: flags for suballocator * * Prepares the suballocation manager for suballocations. * * Return: a pointer to the &xe_sa_manager or an ERR_PTR on failure. */ - struct xe_sa_manager *__xe_sa_bo_manager_init(struct xe_tile *tile, u32 s= ize, - u32 guard, u32 align, u32 flags) + struct xe_sa_manager *__xe_sa_bo_manager_init(struct xe_tile *tile, u32 s= ize, u32 guard, u32 align) { struct xe_device *xe =3D tile_to_xe(tile); struct xe_sa_manager *sa_manager; @@@ -82,32 -79,6 +79,6 @@@ memset(sa_manager->cpu_ptr, 0, bo->ttm.base.size); } =20 - if (flags & XE_SA_BO_MANAGER_FLAG_SHADOW) { - struct xe_bo *shadow; -=20 - ret =3D drmm_mutex_init(&xe->drm, &sa_manager->swap_guard); - if (ret) - return ERR_PTR(ret); -=20 - if (IS_ENABLED(CONFIG_PROVE_LOCKING)) { - fs_reclaim_acquire(GFP_KERNEL); - might_lock(&sa_manager->swap_guard); - fs_reclaim_release(GFP_KERNEL); - } -=20 - shadow =3D xe_managed_bo_create_pin_map(xe, tile, size, - XE_BO_FLAG_VRAM_IF_DGFX(tile) | - XE_BO_FLAG_GGTT | - XE_BO_FLAG_GGTT_INVALIDATE | - XE_BO_FLAG_PINNED_NORESTORE); - if (IS_ERR(shadow)) { - drm_err(&xe->drm, "Failed to prepare %uKiB BO for SA manager (%pe)\n", - size / SZ_1K, shadow); - return ERR_CAST(shadow); - } - sa_manager->shadow =3D shadow; - } -=20 drm_suballoc_manager_init(&sa_manager->base, managed_size, align); ret =3D drmm_add_action_or_reset(&xe->drm, xe_sa_bo_manager_fini, sa_manager); @@@ -117,48 -88,6 +88,6 @@@ return sa_manager; } =20 - /** - * xe_sa_bo_swap_shadow() - Swap the SA BO with shadow BO. - * @sa_manager: the XE sub allocator manager - * - * Swaps the sub-allocator primary buffer object with shadow buffer objec= t. - * - * Return: None. - */ - void xe_sa_bo_swap_shadow(struct xe_sa_manager *sa_manager) - { - struct xe_device *xe =3D tile_to_xe(sa_manager->bo->tile); -=20 - xe_assert(xe, sa_manager->shadow); - lockdep_assert_held(&sa_manager->swap_guard); -=20 - swap(sa_manager->bo, sa_manager->shadow); - if (!sa_manager->bo->vmap.is_iomem) - sa_manager->cpu_ptr =3D sa_manager->bo->vmap.vaddr; - } -=20 - /** - * xe_sa_bo_sync_shadow() - Sync the SA Shadow BO with primary BO. - * @sa_bo: the sub-allocator buffer object. - * - * Synchronize sub-allocator shadow buffer object with primary buffer obj= ect. - * - * Return: None. - */ - void xe_sa_bo_sync_shadow(struct drm_suballoc *sa_bo) - { - struct xe_sa_manager *sa_manager =3D to_xe_sa_manager(sa_bo->manager); - struct xe_device *xe =3D tile_to_xe(sa_manager->bo->tile); -=20 - xe_assert(xe, sa_manager->shadow); - lockdep_assert_held(&sa_manager->swap_guard); -=20 - xe_map_memcpy_to(xe, &sa_manager->shadow->vmap, - drm_suballoc_soffset(sa_bo), - xe_sa_bo_cpu_addr(sa_bo), - drm_suballoc_size(sa_bo)); - } -=20 /** * __xe_sa_bo_new() - Make a suballocation but use custom gfp flags. * @sa_manager: the &xe_sa_manager @@@ -183,36 -112,6 +112,6 @@@ struct drm_suballoc *__xe_sa_bo_new(str return drm_suballoc_new(&sa_manager->base, size, gfp, intr, 0); } =20 - /** - * xe_sa_bo_alloc() - Allocate uninitialized suballoc object. - * @gfp: gfp flags used for memory allocation. - * - * Allocate memory for an uninitialized suballoc object. Intended usage is - * allocate memory for suballoc object outside of a reclaim tainted conte= xt - * and then be initialized at a later time in a reclaim tainted context. - * - * Return: a new uninitialized suballoc object, or an ERR_PTR(-ENOMEM). - */ - struct drm_suballoc *xe_sa_bo_alloc(gfp_t gfp) - { - return drm_suballoc_alloc(gfp); - } -=20 - /** - * xe_sa_bo_init() - Initialize a suballocation. - * @sa_manager: pointer to the sa_manager - * @sa: The struct drm_suballoc. - * @size: number of bytes we want to suballocate. - * - * Try to make a suballocation on a pre-allocated suballoc object of size= @size. - * - * Return: zero on success, errno on failure. - */ - int xe_sa_bo_init(struct xe_sa_manager *sa_manager, struct drm_suballoc *= sa, size_t size) - { - return drm_suballoc_insert(&sa_manager->base, sa, size, true, 0); - } -=20 /** * xe_sa_bo_flush_write() - Copy the data from the sub-allocation to the = GPU memory. * @sa_bo: the &drm_suballoc to flush diff --combined drivers/gpu/drm/xe/xe_sa.h index e24ed5ceb7dce,bf81be165ea08..0000000000000 --- a/drivers/gpu/drm/xe/xe_sa.h +++ b/drivers/gpu/drm/xe/xe_sa.h @@@ -14,15 -14,13 +14,13 @@@ struct dma_fence; struct xe_tile; =20 - #define XE_SA_BO_MANAGER_FLAG_SHADOW BIT(0) - struct xe_sa_manager *__xe_sa_bo_manager_init(struct xe_tile *tile, u32 s= ize, - u32 guard, u32 align, u32 flags); + struct xe_sa_manager *__xe_sa_bo_manager_init(struct xe_tile *tile, u32 s= ize, u32 guard, u32 align); struct drm_suballoc *__xe_sa_bo_new(struct xe_sa_manager *sa_manager, u32= size, gfp_t gfp, bool intr); =20 static inline struct xe_sa_manager *xe_sa_bo_manager_init(struct xe_tile = *tile, u32 size, u32 align) { - return __xe_sa_bo_manager_init(tile, size, SZ_4K, align, 0); + return __xe_sa_bo_manager_init(tile, size, SZ_4K, align); } =20 /** @@@ -41,8 -39,6 +39,6 @@@ static inline struct drm_suballoc *xe_s return __xe_sa_bo_new(sa_manager, size, GFP_KERNEL, intr); } =20 - struct drm_suballoc *xe_sa_bo_alloc(gfp_t gfp); - int xe_sa_bo_init(struct xe_sa_manager *sa_manager, struct drm_suballoc *= sa, size_t size); void xe_sa_bo_flush_write(struct drm_suballoc *sa_bo); void xe_sa_bo_sync_read(struct drm_suballoc *sa_bo); void xe_sa_bo_free(struct drm_suballoc *sa_bo, struct dma_fence *fence); @@@ -76,18 -72,4 +72,4 @@@ static inline void *xe_sa_bo_cpu_addr(s drm_suballoc_soffset(sa); } =20 - void xe_sa_bo_swap_shadow(struct xe_sa_manager *sa_manager); - void xe_sa_bo_sync_shadow(struct drm_suballoc *sa_bo); -=20 - /** - * xe_sa_bo_swap_guard() - Retrieve the SA BO swap guard within sub-alloc= ator. - * @sa_manager: the &xe_sa_manager - * - * Return: Sub alloctor swap guard mutex. - */ - static inline struct mutex *xe_sa_bo_swap_guard(struct xe_sa_manager *sa_= manager) - { - return &sa_manager->swap_guard; - } -=20 #endif diff --combined drivers/gpu/drm/xe/xe_ttm_vram_mgr.c index f28550b29f6be,c3d2bccaa695b..0000000000000 --- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c @@@ -24,6 -24,7 +24,7 @@@ #include "xe_mmio.h" #include "xe_pm.h" #include "xe_printk.h" + #include "xe_ras.h" #include "xe_res_cursor.h" #include "xe_ttm_stolen_mgr.h" #include "xe_ttm_vram_mgr.h" @@@ -228,6 -229,8 +229,8 @@@ static void xe_ttm_vram_buddy_free(stru * * Called with @mgr->lock held. */ + static unsigned long xe_ttm_vram_decay_gc_period(struct xe_ttm_vram_mgr *= mgr); +=20 static void xe_ttm_vram_retry_queued_pages(struct xe_ttm_vram_mgr *mgr) { struct xe_ttm_vram_offline_resource *pos, *n; @@@ -246,6 -249,11 +249,11 @@@ list_del(&pos->queued_link); ++mgr->n_offlined_pages; list_add(&pos->offlined_link, &mgr->offlined_pages); +=20 + if (pos->mode =3D=3D XE_PAGE_OFFLINE_SOFT && mgr->decay_interval_jiffie= s && + !delayed_work_pending(&mgr->decay_gc_work)) + schedule_delayed_work(&mgr->decay_gc_work, + xe_ttm_vram_decay_gc_period(mgr)); } } =20 @@@ -354,6 -362,59 +362,59 @@@ static const struct ttm_resource_manage .debug =3D xe_ttm_vram_mgr_debug }; =20 + static unsigned long xe_ttm_vram_decay_gc_period(struct xe_ttm_vram_mgr *= mgr) + { + unsigned long decay =3D mgr->decay_interval_jiffies; + unsigned long period =3D decay / 8; + unsigned long floor =3D msecs_to_jiffies(60 * 1000); +=20 + /* Never poll less often than the configured decay itself. */ + if (period < floor) + period =3D min(floor, decay); + return period; + } +=20 + static void xe_ttm_vram_decay_gc_worker(struct work_struct *work) + { + struct xe_ttm_vram_mgr *mgr =3D container_of(to_delayed_work(work), + struct xe_ttm_vram_mgr, + decay_gc_work); + struct xe_ttm_vram_offline_resource *pos, *n; + struct xe_device *xe =3D ttm_to_xe_device(mgr->manager.bdev); + unsigned long now =3D jiffies; + unsigned long decay; + bool soft_remaining =3D false; +=20 + mutex_lock(&mgr->lock); + decay =3D mgr->decay_interval_jiffies; + if (!decay) + goto unlock; +=20 + list_for_each_entry_safe(pos, n, &mgr->offlined_pages, offlined_link) { + if (pos->mode !=3D XE_PAGE_OFFLINE_SOFT) + continue; + if (time_before(now, pos->last_seen + decay)) { + soft_remaining =3D true; + continue; + } +=20 + list_del(&pos->offlined_link); + xe_ttm_vram_buddy_free(mgr, &pos->blocks, pos->used_visible_size); + --mgr->n_offlined_pages; + drm_info(&xe->drm, + "decay: reinstated soft-offlined page 0x%llx\n", + pos->addr); + kfree(pos); + } +=20 + /* Nothing left to track; re-armed on demand when a new SOFT entry appea= rs */ + if (soft_remaining) + schedule_delayed_work(&mgr->decay_gc_work, + xe_ttm_vram_decay_gc_period(mgr)); + unlock: + mutex_unlock(&mgr->lock); + } +=20 static const struct dmem_cgroup_ops xe_ttm_vram_mgr_dmem_ops; =20 static int xe_ttm_vram_mgr_dmem_reclaim(struct dmem_cgroup_pool_state *po= ol, @@@ -414,6 -475,9 +475,9 @@@ static void xe_ttm_vram_mgr_fini(struc struct xe_ttm_vram_mgr *mgr =3D arg; struct ttm_resource_manager *man =3D &mgr->manager; =20 + /* Disable, not cancel: in-flight mgr_del() or RAS paths may re-arm it */ + disable_delayed_work_sync(&mgr->decay_gc_work); +=20 mutex_lock(&mgr->lock); xe_ttm_vram_free_bad_pages(mgr); mutex_unlock(&mgr->lock); @@@ -437,6 -501,7 +501,7 @@@ int __xe_ttm_vram_mgr_init(struct xe_de struct ttm_resource_manager *man =3D &mgr->manager; struct dmem_cgroup_region *cg; const char *name; + u32 decay_secs; int err; =20 man->func =3D &xe_ttm_vram_mgr_func; @@@ -446,6 -511,10 +511,10 @@@ return err; INIT_LIST_HEAD(&mgr->offlined_pages); INIT_LIST_HEAD(&mgr->queued_pages); + INIT_DELAYED_WORK(&mgr->decay_gc_work, xe_ttm_vram_decay_gc_worker); + decay_secs =3D xe_configfs_get_bad_page_decay_secs(to_pci_dev(xe->drm.de= v)); + /* secs_to_jiffies() multiplies in u32 and would overflow */ + mgr->decay_interval_jiffies =3D (unsigned long)decay_secs * HZ; mgr->default_page_size =3D default_page_size; mgr->visible_size =3D io_size; mgr->visible_avail =3D io_size; @@@ -724,13 -793,21 +793,21 @@@ static bool xe_ttm_vram_page_already_pr lockdep_assert_held(&mgr->lock); =20 list_for_each_entry(pos, &mgr->offlined_pages, offlined_link) { - if (pos->addr =3D=3D addr) + if (pos->addr =3D=3D addr) { + if (pos->mode =3D=3D XE_PAGE_OFFLINE_SOFT) + pos->mode =3D XE_PAGE_OFFLINE_HARD; + pos->last_seen =3D jiffies; return true; + } } =20 list_for_each_entry(pos, &mgr->queued_pages, queued_link) { - if (pos->addr =3D=3D addr) + if (pos->addr =3D=3D addr) { + if (pos->mode =3D=3D XE_PAGE_OFFLINE_SOFT) + pos->mode =3D XE_PAGE_OFFLINE_HARD; + pos->last_seen =3D jiffies; return true; + } } =20 return false; @@@ -781,7 -858,8 +858,8 @@@ static struct xe_bo *xe_ttm_vram_block_ } =20 static int xe_ttm_vram_reserve_page_at_addr(struct xe_device *xe, u64 add= r, - struct xe_ttm_vram_mgr *vram_mgr, struct gpu_buddy *mm) + struct xe_ttm_vram_mgr *vram_mgr, struct gpu_buddy *mm, + bool permanent) { struct xe_ttm_vram_offline_resource *nentry; struct xe_bo *pbo_to_put =3D NULL; @@@ -803,6 -881,12 +881,12 @@@ INIT_LIST_HEAD(&nentry->blocks); nentry->status =3D XE_PAGE_RESERVE_PENDING; nentry->addr =3D addr; + /* + * Pages reloaded from firmware's non-volatile permanent list are + * already confirmed bad by a prior boot; never let them decay back in. + */ + nentry->mode =3D permanent ? XE_PAGE_OFFLINE_HARD : XE_PAGE_OFFLINE_SOF= T; + nentry->last_seen =3D jiffies; =20 if (block) { pbo =3D xe_ttm_vram_block_owner_get(xe, block); @@@ -842,6 -926,11 +926,11 @@@ } ++vram_mgr->n_offlined_pages; list_add(&nentry->offlined_link, &vram_mgr->offlined_pages); + if (!permanent && vram_mgr->decay_interval_jiffies && + !delayed_work_pending(&vram_mgr->decay_gc_work)) { + schedule_delayed_work(&vram_mgr->decay_gc_work, + xe_ttm_vram_decay_gc_period(vram_mgr)); + } return ret; } } @@@ -906,11 -995,14 +995,14 @@@ static struct xe_vram_region *xe_ttm_vr } =20 /** - * xe_ttm_vram_handle_addr_fault - Handle vram physical address error fla= ged + * xe_ttm_vram_handle_addr_fault - Handle vram physical address error fla= gged * @xe: pointer to parent device * @addr: physical faulty address + * @permanent: true if the address is already confirmed bad from a prior = boot + * (e.g. sourced from firmware's non-volatile list) and must never be eli= gible + * for decay-based reinstatement * - * Handle the physcial faulty address error on specific tile. + * Handle the physical faulty address error on specific tile. * * Returns 0 for success, negative error code otherwise as follow: * * %-EIO - critical BO or address outside any VRAM region; next action = is reset. @@@ -923,7 -1015,7 +1015,7 @@@ * pending if the blocks backing it could not be freed immediately; the * reservation is then completed from xe_ttm_vram_mgr_del(). */ - int xe_ttm_vram_handle_addr_fault(struct xe_device *xe, u64 addr) + int xe_ttm_vram_handle_addr_fault(struct xe_device *xe, u64 addr, bool pe= rmanent) { struct xe_ttm_vram_mgr *vram_mgr; struct xe_vram_region *vr; @@@ -953,14 -1045,14 +1045,14 @@@ vram_mgr =3D &vr->ttm; mm =3D &vram_mgr->mm; =20 - if (xe->ras.disable_vram_page_offline) { + if (!xe_ras_get_enable_vram_page_offline(xe)) { xe_err(xe, "0x%llx is reported as corrupted address by HW\n", addr); return -EOPNOTSUPP; } =20 /* Reserve page at address */ - return xe_ttm_vram_reserve_page_at_addr(xe, addr - vr->dpa_base, vram_mg= r, mm); + return xe_ttm_vram_reserve_page_at_addr(xe, addr - vr->dpa_base, vram_mg= r, mm, permanent); } EXPORT_SYMBOL(xe_ttm_vram_handle_addr_fault); =20 @@@ -973,7 -1065,7 +1065,7 @@@ * debugfs interface for testing page offlining. * * Note: Executing this test will permanently retire the allocated - * memory tracking pages. The driver must be rebinded (unbind and bind) + * memory tracking pages. The driver must be rebound (unbind and bind) * post-test execution to reclaim the reserved space, as these pages * cannot be freed or reclaimed dynamically while the current instance * remains active. @@@ -1011,7 -1103,7 +1103,7 @@@ int xe_ttm_vram_inject_fault(struct xe_ * test-only path. */ if (found) - return xe_ttm_vram_handle_addr_fault(xe, addr + vr->dpa_base); + return xe_ttm_vram_handle_addr_fault(xe, addr + vr->dpa_base, false); =20 cond_resched(); if (addr =3D=3D 0) @@@ -1032,11 -1124,7 +1124,7 @@@ static int vram_bad_pages_show(struct s struct xe_tile *tile; u8 id; =20 - man =3D ttm_manager_type(&xe->ttm, XE_PL_VRAM0); - if (man) - /* TODO Hook with RAS to show max_pages fetched from FW */ - seq_printf(m, "max_pages: %d\n", - to_xe_ttm_vram_mgr(man)->max_pages); + seq_printf(m, "max_pages: %u\n", xe_ras_get_max_pages(xe)); =20 for_each_tile(tile, xe, id) { struct xe_vram_region *vr =3D tile->mem.vram; diff --combined drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h index 8741b5de6c379,0a6d8f8c2aa83..0000000000000 --- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h +++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h @@@ -7,8 -7,16 +7,16 @@@ #define _XE_TTM_VRAM_MGR_TYPES_H_ =20 #include + #include #include =20 + #define XE_BAD_PAGE_DECAY_SECS_DEFAULT (24 * 60 * 60) +=20 + enum xe_page_offline_mode { + XE_PAGE_OFFLINE_SOFT =3D 0, + XE_PAGE_OFFLINE_HARD, + }; +=20 /** * struct xe_ttm_vram_mgr - Xe TTM VRAM manager * @@@ -27,7 -35,7 +35,7 @@@ struct xe_ttm_vram_mgr=20 struct list_head queued_pages; /** @n_queued_pages: Number of queued pages */ u16 n_queued_pages; - /** @visible_size: Proped size of the CPU visible portion */ + /** @visible_size: Probed size of the CPU visible portion */ u64 visible_size; /** @visible_avail: CPU visible portion still unallocated */ u64 visible_avail; @@@ -37,8 -45,10 +45,10 @@@ struct mutex lock; /** @mem_type: The TTM memory type */ u32 mem_type; - /** @max_pages: max pages that can be in offline queue retrieved from FW= */ - u16 max_pages; + /** @decay_gc_work: reinstates expired soft-offlined pages */ + struct delayed_work decay_gc_work; + /** @decay_interval_jiffies: soft-offline decay interval */ + unsigned long decay_interval_jiffies; }; =20 /** @@@ -81,6 -91,10 +91,10 @@@ struct xe_ttm_vram_offline_resource=20 u64 addr; /** @status: buddy reservation status */ enum xe_page_reserve_status status; + /** @mode: soft or hard offline mode */ + enum xe_page_offline_mode mode; + /** @last_seen: most recent hardware report time */ + unsigned long last_seen; }; =20 #endif diff --combined drivers/gpu/drm/xe/xe_vm.c index 0ecfad82b9e5a,c774804ca5567..0000000000000 --- a/drivers/gpu/drm/xe/xe_vm.c +++ b/drivers/gpu/drm/xe/xe_vm.c @@@ -18,6 -18,7 +18,7 @@@ #include #include #include + #include #include =20 #include @@@ -31,6 -32,7 +32,7 @@@ #include "xe_exec_queue.h" #include "xe_gt.h" #include "xe_migrate.h" + #include "xe_module.h" #include "xe_pagefault.h" #include "xe_pat.h" #include "xe_pm.h" @@@ -366,7 -368,7 +368,7 @@@ static int xe_gpuvm_validate(struct drm =20 /* Skip re-populating purged BOs, rebind maps scratch pages. */ if (xe_bo_is_purged(bo)) { - vm_bo->evicted =3D false; + drm_gpuvm_bo_evict(vm_bo, false); return 0; } =20 @@@ -377,7 -379,7 +379,7 @@@ if (ret) return ret; =20 - vm_bo->evicted =3D false; + drm_gpuvm_bo_evict(vm_bo, false); return 0; } =20 @@@ -386,31 -388,59 +388,59 @@@ * @vm: The vm for which we are rebinding. * @exec: The struct drm_exec with the locked GEM objects. * @num_fences: The number of fences to reserve for the operation, not - * including rebinds and validations. + * including rebinds and validations. Zero reserves none, which is what t= he + * %DRM_GPUVM_EXEC_PASS_EARLY pass wants. + * @pass: The &enum drm_gpuvm_exec_pass @exec was locked for. * * Validates all evicted gem objects and rebinds their vmas. Note that * rebindings may cause evictions and hence the validation-rebind * sequence is rerun until there are no more objects to validate. * + * In the %DRM_GPUVM_EXEC_PASS_EARLY pass only the validation is done, and + * only for the objects whose dma-resv @exec holds. The rest, along with = the + * rebind and the fence reservation, is left to the + * %DRM_GPUVM_EXEC_PASS_LATE pass of the same transaction, which locks + * everything. + * * Return: 0 on success, negative error code on error. In particular, * may return -EINTR or -ERESTARTSYS if interrupted, and -EDEADLK if * the drm_exec transaction needs to be restarted. */ int xe_vm_validate_rebind(struct xe_vm *vm, struct drm_exec *exec, - unsigned int num_fences) + unsigned int num_fences, + enum drm_gpuvm_exec_pass pass) { struct drm_gem_object *obj; int ret; =20 do { - ret =3D drm_gpuvm_validate(&vm->gpuvm, exec); + ret =3D drm_gpuvm_exec_pass_validate(&vm->gpuvm, exec, pass); if (ret) return ret; =20 + /* + * xe_vm_rebind() rebinds the whole rebind list in one go and + * attaches a fence to the dma-resv of every BO on it, so it + * needs all of them locked. The early pass deliberately does + * not lock the resident ones, so leave the rebind to the late + * pass, which holds everything. + */ + if (pass =3D=3D DRM_GPUVM_EXEC_PASS_EARLY) + continue; +=20 ret =3D xe_vm_rebind(vm, false); if (ret) return ret; - } while (!list_empty(&vm->gpuvm.evict.list)); + } while (drm_gpuvm_exec_pass_has_evicted(&vm->gpuvm, pass)); +=20 + /* + * The early pass reserves nothing. It attaches no fence itself, and + * the objects it locks are still locked in the late pass, whose own + * reservation below walks every object the transaction has + * accumulated and so covers them too. + */ + if (!num_fences) + return 0; =20 drm_exec_for_each_locked_object(exec, obj) { ret =3D dma_resv_reserve_fences(obj->resv, num_fences); @@@ -436,13 -466,17 +466,17 @@@ void xe_vm_bump_lru(struct xe_vm *vm, s spinlock_t *lru_lock =3D &vm->xe->ttm.lru_lock; struct drm_gem_object *obj; =20 + xe_vm_assert_held(vm); +=20 + vm->lru_stamp =3D jiffies; +=20 spin_lock(lru_lock); ttm_lru_bulk_move_tail(&vm->lru_bulk_move); drm_exec_for_each_locked_object(exec, obj) { if (!drm_gpuvm_is_extobj(&vm->gpuvm, obj)) continue; =20 - ttm_bo_move_to_lru_tail(&gem_to_xe_bo(obj)->ttm); + xe_bo_bump_lru_locked(gem_to_xe_bo(obj)); cond_resched_lock(lru_lock); } spin_unlock(lru_lock); @@@ -482,7 -516,8 +516,8 @@@ static int xe_preempt_work_begin(struc * The fence reservation here is intended for the new preempt fences * we attach at the end of the rebind work. */ - return xe_vm_validate_rebind(vm, exec, vm->preempt.num_exec_queues); + return xe_vm_validate_rebind(vm, exec, vm->preempt.num_exec_queues, + DRM_GPUVM_EXEC_PASS_ALL); } =20 static bool vm_suspend_rebind_worker(struct xe_vm *vm) @@@ -509,9 -544,34 +544,34 @@@ */ void xe_vm_resume_rebind_worker(struct xe_vm *vm) { - queue_work(vm->xe->ordered_wq, &vm->preempt.rebind_work); + xe_vm_queue_rebind_worker(vm); } =20 + #if IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT) + /* + * Force one retry per rebind sequence. The sequence spans requeues of the + * worker, so the state must live in the vm. + */ + static bool xe_vm_rebind_worker_inject_retry(struct xe_vm *vm) + { + return !vm->preempt.inval_inject_tries++; + } +=20 + static void xe_vm_rebind_worker_reset_inject(struct xe_vm *vm) + { + vm->preempt.inval_inject_tries =3D 0; + } + #else + static bool xe_vm_rebind_worker_inject_retry(struct xe_vm *vm) + { + return false; + } +=20 + static void xe_vm_rebind_worker_reset_inject(struct xe_vm *vm) + { + } + #endif +=20 static void preempt_rebind_work_func(struct work_struct *w) { struct xe_vm *vm =3D container_of(w, struct xe_vm, preempt.rebind_work); @@@ -521,7 -581,6 +581,6 @@@ LIST_HEAD(preempt_fences); int err =3D 0; long wait; - int __maybe_unused tries =3D 0; =20 xe_assert(vm->xe, xe_vm_in_preempt_fence_mode(vm)); trace_xe_vm_rebind_worker_enter(vm); @@@ -534,7 -593,6 +593,6 @@@ return; } =20 - retry: if (!try_wait_for_completion(&vm->xe->pm_block) && vm_suspend_rebind_wor= ker(vm)) { up_write(&vm->lock); /* We don't actually block but don't make progress. */ @@@ -584,20 -642,14 +642,14 @@@ goto out_unlock; } =20 - #define retry_required(__tries, __vm) \ - (IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT) ? \ - (!(__tries)++ || __xe_vm_userptr_needs_repin(__vm)) : \ - __xe_vm_userptr_needs_repin(__vm)) -=20 xe_svm_notifier_lock(vm); - if (retry_required(tries, vm)) { + if (xe_vm_rebind_worker_inject_retry(vm) || + __xe_vm_userptr_needs_repin(vm)) { xe_svm_notifier_unlock(vm); err =3D -EAGAIN; goto out_unlock; } =20 - #undef retry_required -=20 xe_vm_bump_lru(vm, &exec); =20 /* Point of no return. */ @@@ -612,22 -664,20 +664,20 @@@ out_unlock_outer trace_xe_vm_rebind_worker_retry(vm); =20 /* - * We can't block in workers on a VF which supports migration - * given this can block the VF post-migration workers from - * getting scheduled. + * Drop all locks and requeue rather than retrying in-line. + * This avoids blocking other work items, such as VF + * post-migration workers, and lets other users of the vm + * lock make progress between attempts. */ - if (IS_SRIOV_VF(vm->xe) && - xe_sriov_vf_migration_supported(vm->xe)) { - up_write(&vm->lock); - free_preempt_fences(&preempt_fences); - xe_vm_queue_rebind_worker(vm); - trace_xe_vm_rebind_worker_exit(vm); - return; - } -=20 - goto retry; + up_write(&vm->lock); + free_preempt_fences(&preempt_fences); + xe_vm_queue_rebind_worker(vm); + trace_xe_vm_rebind_worker_exit(vm); + return; } =20 + xe_vm_rebind_worker_reset_inject(vm); +=20 if (err) { drm_warn(&vm->xe->drm, "VM worker error: %d\n", err); xe_vm_kill(vm, true); @@@ -1279,7 -1329,14 +1329,14 @@@ static void vma_destroy_cb(struct dma_f struct xe_vma *vma =3D container_of(cb, struct xe_vma, destroy_cb); =20 INIT_WORK(&vma->destroy_work, vma_destroy_work_func); - queue_work(system_dfl_wq, &vma->destroy_work); +=20 + /* + * The destroy work puts a vm reference which may put the last + * device reference. Hence we can't queue this work on the device + * destroy queue since that may deadlock. Use the module-wide + * destroy queue. + */ + xe_destroy_wq_queue(&vma->destroy_work); } =20 static void xe_vm_assert_write_mode_or_garbage_collector(struct xe_vm *vm) @@@ -2081,8 -2138,12 +2138,12 @@@ static void xe_vm_free(struct drm_gpuv { struct xe_vm *vm =3D container_of(gpuvm, struct xe_vm, gpuvm); =20 - /* To destroy the VM we need to be able to sleep */ - queue_work(system_dfl_wq, &vm->destroy_work); + /* + * To destroy the VM we need to be able to sleep. + * drm_gpuvm keeps at least one device reference at this + * point so xe->destroy_wq must still be alive. + */ + queue_work(vm->xe->destroy_wq, &vm->destroy_work); } =20 struct xe_vm *xe_vm_lookup(struct xe_file *xef, u32 id) @@@ -2726,14 -2787,6 +2787,6 @@@ static struct xe_vma *new_vma(struct xe =20 if (xe_vma_is_userptr(vma)) { err =3D xe_vma_userptr_pin_pages(to_userptr_vma(vma)); - /* - * -EBUSY has dedicated meaning that a user fence - * attached to the VMA is busy, in practice - * xe_vma_userptr_pin_pages can only fail with -EBUSY if - * we are low on memory so convert this to -ENOMEM. - */ - if (err =3D=3D -EBUSY) - err =3D -ENOMEM; } } if (err) { @@@ -3822,14 -3875,15 +3875,15 @@@ static int vm_bind_ioctl_check_args(str =20 pat_index =3D array_index_nospec(pat_index, xe->pat.n_entries); (*bind_ops)[i].pat_index =3D pat_index; - coh_mode =3D xe_pat_index_get_coh_mode(xe, pat_index); - comp_en =3D xe_pat_index_get_comp_en(xe, pat_index); - if (XE_IOCTL_DBG(xe, !coh_mode)) { /* hw reserved */ + if (XE_IOCTL_DBG(xe, !xe_pat_index_is_valid(xe, pat_index))) { err =3D -EINVAL; goto free_bind_ops; } =20 - if (XE_WARN_ON(coh_mode > XE_COH_2WAY)) { + coh_mode =3D xe_pat_index_get_coh_mode(xe, pat_index); + comp_en =3D xe_pat_index_get_comp_en(xe, pat_index); +=20 + if (XE_IOCTL_DBG(xe, xe_pat_wa_14026539277_reserved(xe, pat_index))) { err =3D -EINVAL; goto free_bind_ops; } @@@ -4028,11 -4082,13 +4082,13 @@@ int xe_vm_bind_ioctl(struct drm_device= =20 struct drm_gpuva_ops **ops =3D NULL; struct xe_vm *vm; struct xe_exec_queue *q =3D NULL; - u32 num_syncs, num_ufence =3D 0; + u32 num_syncs, num_ufence; struct xe_sync_entry *syncs =3D NULL; struct drm_xe_vm_bind_op *bind_ops =3D NULL; struct xe_vma_ops vops =3D { .flags =3D 0, }; + struct xe_userptr_repin repin =3D {}; struct dma_fence *fence; + bool repin_again; int err; int i; =20 @@@ -4065,6 -4121,9 +4121,9 @@@ /* Ensure all UNMAPs visible */ xe_svm_flush(vm); =20 + retry: + repin_again =3D false; + num_ufence =3D 0; err =3D down_write_killable(&vm->lock); if (err) goto put_exec_queue; @@@ -4168,6 -4227,7 +4227,7 @@@ xe_vma_ops_init(&vops, vm, q, syncs, num_syncs); if (args->num_binds > 1) vops.flags |=3D XE_VMA_OPS_ARRAY_OF_BINDS; + xe_userptr_repin_begin(&repin); for (i =3D 0; i < args->num_binds; ++i) { u64 range =3D bind_ops[i].range; u64 addr =3D bind_ops[i].addr; @@@ -4183,12 -4243,15 +4243,15 @@@ if (IS_ERR(ops[i])) { err =3D PTR_ERR(ops[i]); ops[i] =3D NULL; + repin_again =3D err =3D=3D -EAGAIN; goto unwind_ops; } =20 err =3D vm_bind_ioctl_ops_parse(vm, ops[i], &vops); - if (err) + if (err) { + repin_again =3D err =3D=3D -EAGAIN; goto unwind_ops; + } =20 #ifdef TEST_VM_OPS_ERROR if (flags & FORCE_OP_ERROR) { @@@ -4251,6 -4314,27 +4314,27 @@@ release_vm_lock up_read(&vm->lock); else up_write(&vm->lock); + if (repin_again) { + /* + * A userptr repin keeps failing due to concurrent CPU mapping + * changes. New vmas are created on every attempt so the overall + * timeout is tracked here. Retry with all locks dropped. + */ + u32 timeout_ms =3D READ_ONCE(vm->xe->userptr_repin_timeout_ms); +=20 + if (xe_userptr_repin_should_retry(&repin, timeout_ms)) { + bos =3D NULL; + ops =3D NULL; + syncs =3D NULL; + vops.flags =3D 0; + cond_resched(); + if (!signal_pending(current)) + goto retry; + err =3D -ERESTARTSYS; + } else { + err =3D -EBUSY; + } + } put_exec_queue: if (q) xe_exec_queue_put(q); --kwB5Ifurow0h2I5j Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEreZoqmdXGLWf4p/qJNaLcl1Uh9AFAmsS3qIACgkQJNaLcl1U h9D5jQf/ZwdbsIKmi9P/cwaVcWGneygyY6eIVvRwVjUlG1iEzRrEfBhYuhY2l+hk ogVu/WOb/KrzSQwqQhrLnsViQcNDkt4m8fTWt6SeixUX02A5c4Mhz+NjOU4vfEC+ jdEfn3Ai+EluhLsSM28QAS8Xen/OAxxJRcgUcbW4fvf1qMdHGnDXOizu/SVy0YWr kS4q6BDOnPymR6fowz2FwdAlaQOt7D8YXz+gFlld7S0utCk7ZHF4UBn7KcgdD2hn ZAtmaF0nE4anqkoFaunt3X5K12Pbnuipfg2CVTdVDdh1Ej6e8vYX+XXVuWC0DcxH e6/YWnliBAs4e34/BAbk2bzKbOBBJQ== =46B8 -----END PGP SIGNATURE----- --kwB5Ifurow0h2I5j--