From patchwork Tue Jan 23 13:04:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Coster X-Patchwork-Id: 13527371 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 48572C47DDB for ; Tue, 23 Jan 2024 13:04:57 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 70E0110E76C; Tue, 23 Jan 2024 13:04:56 +0000 (UTC) Received: from mx08-00376f01.pphosted.com (mx08-00376f01.pphosted.com [91.207.212.86]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7671210E76C for ; Tue, 23 Jan 2024 13:04:54 +0000 (UTC) Received: from pps.filterd (m0168888.ppops.net [127.0.0.1]) by mx08-00376f01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 40NAQkmi018258; Tue, 23 Jan 2024 13:04:29 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=imgtec.com; h= from:to:cc:subject:date:message-id:content-type:mime-version; s= dk201812; bh=4JJmqrO98xwXxGapTfR4su9sv0Rj24qNM0UEKKaDGw8=; b=XzU l5dhExObRhs+5htsRWS49X25jZe2gfBwjbaCRcPqp01igqrRAQvWJamQ2Nnq6HNz P1B1tOXkCpu3NwZHmS5d7eo3fms2QDnvnUUMF/4dOhr/SgAbc+INN2p/Tvc1nmv4 1U7ZS9nsvA4juVnVCW9AiVnGZ2awzUSSj2zRX/MTxUkscgHOWt/ScoW0IJzSrwjW Y9t28QV2vZxPS7tv1Qlu6LALULZvmx9xC/qzZ7f7Hom6qI2s739O64s3LDTEGgC7 /XJIDEbVf6DdrABDX2+qdOdlTVQ+tHZ64Yi3KOxk+U52gNzyqPxJnvYXXLd5qd9J ra2ELCMnptPdx4vREiw== Received: from hhmail04.hh.imgtec.org ([217.156.249.195]) by mx08-00376f01.pphosted.com (PPS) with ESMTPS id 3vr5at2bnt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Tue, 23 Jan 2024 13:04:28 +0000 (GMT) Received: from HHMAIL04.hh.imgtec.org (10.100.10.119) by HHMAIL04.hh.imgtec.org (10.100.10.119) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Tue, 23 Jan 2024 13:04:27 +0000 Received: from GBR01-LO4-obe.outbound.protection.outlook.com (104.47.85.105) by email.imgtec.com (10.100.10.121) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35 via Frontend Transport; Tue, 23 Jan 2024 13:04:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EVINLuvtC23iuqliopG8gBtmMNTQ4txtpWZgea//RY8Ss1vs0SybBti1XQrGWswHJj+ppDC4rnwXTNHB8dAXu5Q0AN3tTxyDClLcKGAtJWaNcLVPFX5X0rnSSqDaDo8ql5j7Fp1Y/2TRQAuEamGRYcGxn6HoNR8hu4qD6aWXWKaWRFMmoLOz6RMzhxiiJKftXKagdFwNG5iaNrc+lAxHGaKNhNeSD0H2EmTMD1/BPP6eA58MvWQDt7DRjBbFH5rSuAR4SoEJEATGp1Fbc/kn8diAf1j44clcrDzhHxPHrrf+rXDvZBpIVKQcu2JdQJG4Yf+wEVeE0VQpv0ZzPDv42Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=4JJmqrO98xwXxGapTfR4su9sv0Rj24qNM0UEKKaDGw8=; b=YU58obbKZ03qFowOJLeoM7T7xAMk71KoLCx6L+H2u/NEdDDRS8LKVA2cEeSrNPx74MWf1BQt7TBijgsIbN+GrH7oEUrjqveKwjKZUar4hYS9xznUmfCVBHQjrpC6rZ6rIkL+SVQG77n8Y3uvjeMFvwu+/8I+kP+O0Zg2d4O9CijpUX3GxbftkIb0Mz7DZ/cGVM+n5IWStmh4AdXu/qm1m3ElHcaM42z+G+VRtMdMm3VeVulup6Fla0Rwh+iDG48m3fBD31vAr4KUEkCIKA0pXz4mfQPS9dzRun9GNtfIJRepGCx5JlmobKwmeI3juOAqD96LjjcQRvoFdh5FTixozQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=imgtec.com; dmarc=pass action=none header.from=imgtec.com; dkim=pass header.d=imgtec.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=IMGTecCRM.onmicrosoft.com; s=selector2-IMGTecCRM-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4JJmqrO98xwXxGapTfR4su9sv0Rj24qNM0UEKKaDGw8=; b=ALg6yXLfiooKBuBZXPM50H6psDHh1U1Zs3ihh+JoFm4z2/HMraeeYtiEBhtlQw686tLqK0bdFZw+8usYlQtLNCkgJ7ZFsbpXW5MUA6QR/9bXAI97nW1z/vAHwkV7PKDoY/gtuaYNiPsPZyGTa1bMhqVRzPcC+EaTVoA8IPgvnS8= Received: from LO0P265MB3404.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:16c::5) by LO2P265MB5088.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:253::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.36; Tue, 23 Jan 2024 13:04:24 +0000 Received: from LO0P265MB3404.GBRP265.PROD.OUTLOOK.COM ([fe80::7238:be9:1fb8:596c]) by LO0P265MB3404.GBRP265.PROD.OUTLOOK.COM ([fe80::7238:be9:1fb8:596c%3]) with mapi id 15.20.7202.028; Tue, 23 Jan 2024 13:04:24 +0000 From: Matt Coster To: "dri-devel@lists.freedesktop.org" , Steven Price Subject: [PATCH] drm/imagination: On device loss, handle unplug after critical section Thread-Topic: [PATCH] drm/imagination: On device loss, handle unplug after critical section Thread-Index: AQHaTfyvO3XTIeZj0U2UK+bGG2THtg== Date: Tue, 23 Jan 2024 13:04:24 +0000 Message-ID: <59bb4827-43a5-4e87-8026-77777b2a8087@imgtec.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: LO0P265MB3404:EE_|LO2P265MB5088:EE_ x-ms-office365-filtering-correlation-id: f33f038c-f04e-40fb-235f-08dc1c13d279 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 7+JdCpiv9ly0K0VFiI19w9p6nmZGLQgCTjgMdwbdE/hV3kZVIafrwezVvGlsrr8vhDKjpfhHbSwAQSTK43sMRRxv7K6gzeG/7DHfNCdJk7bw4RabvPXBzaigZAAlVWCxXJisE3hK2K267pY5170QeAi+U57OKsjuH7CXcnmUIxYPdzubSEoXP6f04oE5QXqSeR/i3GXGJgcp7GE8APLkw0TBmz2PfkIcHVzaNScAJ1IoCLAYOsiXyKPsC/uqSmWBqItrQ5FM8IgFmev8TRq2yOMbwQr1/yC5FNEKQt5hBBhK1XfCKszDzCm4vgh0ns6T9xaeepWx2eIatapNwF0SSf4XyM5Y7kNqBzxhFPaN42c+DEkyQewJsLKWfe9MBWNRzjsCo0yiLgm2jCU2sC337P+tvc43mh5xQgFunx7iQGEqkRV14dY9err3OO+B9DxGPe3LfXv7AKGcJipqcnDkH/P4IfR7UblvBlTtAdG0qsD683bjA42LARcKJr8k+bjilXEOoCj5lUxnxetsRH/CQyasgY4Oqy7bD3Ixsg1iM9de84YElZnRwrLvdG7Fb7xA2IQNyxoVAb8gzSAhDxockFrnISBKwGVhnf7wF0nKaszufKi1L4K8gvYVgnxIYWDuLLh8B/eHb5hzbu4IoA7b9Q== x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LO0P265MB3404.GBRP265.PROD.OUTLOOK.COM; PTR:; CAT:NONE; SFS:(13230031)(346002)(39850400004)(136003)(376002)(366004)(396003)(230922051799003)(451199024)(64100799003)(1800799012)(186009)(38070700009)(4326008)(83380400001)(8676002)(8936002)(122000001)(38100700002)(99936003)(86362001)(31696002)(41300700001)(36756003)(2906002)(30864003)(5660300002)(76116006)(64756008)(66946007)(66476007)(110136005)(66556008)(316002)(54906003)(66446008)(2616005)(31686004)(6506007)(6486002)(6512007)(966005)(478600001)(71200400001)(26005)(45980500001)(559001)(579004); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?3YyEjsTna4cZDaj4SSvn3vTNMORW?= =?utf-8?q?WmJ6/d+ptVJbvIVlNyDxttB+FIS4MmWO6kPxxMAz58vhvvcYXPTcqPTnz1whrpB2Z?= =?utf-8?q?Sb9Yr0aGpgW0AS+rZNx3W+jzFkk74ODcryq6d4/DEjUXurDuUaif/+UICMsmV6B4f?= =?utf-8?q?1Ur9PepjebTC6VqvgJgqBp7ROCxqkl/NCZKWqyzeuv0SpEkMZkandcBA94vBTTzTq?= =?utf-8?q?fEn8lJkOCfKcp529OBwnkHkgT6+hzcUinQZQSOeCcZir/d9o8/CtaHr1SHetsZyWF?= =?utf-8?q?2XG39YYfmQaTJBCyc+YT8t/b7ph0y4CWrYpeqS1rlysQeVcTPqqAKiGlAQWycFLna?= =?utf-8?q?CduXsteSYmMk71zzQjxGPe+tmk8w2M1BKgC9X/EC7EcMkZHINQ15+/vIZH5ORmghJ?= =?utf-8?q?R0gg+USKBbQEtDhtBDDuLdBnXzspU/etsIAvEyZ0jdSZ36Plka/YdA0zAGaedJKAk?= =?utf-8?q?0qOdyxa/JLTMz0Yl4g4+m/jQCBl+9Z0166SYT+fAmqWGqM82UvZJPoXKaxaZ3WKDV?= =?utf-8?q?BP8G084M9VkrKTZd5+hBNHn/Ze4ZtevFf/HxRedlFsD1aP1KZaIwuXCoKdPYjEQjV?= =?utf-8?q?Qn7OGumDy2o7BEaWJ7PCLez1k7pZQoIkE3qeRvGGYzAIr2JAB+hmtoPn2UI4sweMd?= =?utf-8?q?7gLrApzIyuPgufUqCX5vkn1w4NR7yjIZZP48FxCM3cESvg2ULWWUUXciCUroqveXn?= =?utf-8?q?eoWR7z+iF+nbKwcTU0n4dQBWHEgtpkQpILtGqSFIAXdNTVw+s6fO2hVW2gSsRdLSE?= =?utf-8?q?28uPJhi+oan8enoTNkuQ2us37pkVOGif9QC3BwHCHAfjS+wGINDEXJYGOb2taBzoV?= =?utf-8?q?Kpap0OAZWFgMUn3DEZ89Ub3c6gR1MXa1N5+0zoLvwpxxizfcdJ+rKhQpB6dO3+B9v?= =?utf-8?q?nsm85ur+w9mti7PabpnGDCTBvqG9XV923/knmMPspfS9ooeINpQCTR/t/kehjvjrF?= =?utf-8?q?clQvJDKp7vbGOzYOB1IDircOFRxll5QVL1xgFhsXqMzCOTiJX0PiFZNqkxbQzoAol?= =?utf-8?q?0EuQEu/1KeF0yP4JcJW+wb9bPDoAQZA40YZGWpkHj9AftEx4Es0UgU5gKtfOkO4T0?= =?utf-8?q?00oZjJQbSVo4C+gTKnBWFTQBqLHf+/qCoOgw4ZCEvUQuYJL9dIRKO7lx3yAlHiFNM?= =?utf-8?q?Ot/gCxYrTyPWoKvH2NL9RAZ4V/JK3ZuqoFUGxHjJ3JR5z25+vV2pCjvwu373hFapR?= =?utf-8?q?Pr1sWTb/rpX6gY2s11ijhWhjDX6nMnBX7xCAJyqFvUFvNqcWDXK3lQQ0LuYMPH4kX?= =?utf-8?q?6YYy1IZWdldvs7MTJOEUorGizh1BaDAUUYp9cQHUBU/xCyZg7sawga5vAU8iTNqKl?= =?utf-8?q?3ophZP/A9cXjEQBJQ9RnD76TZaL/ua3wfDfxowaMQ7WL/blbKoI+rOElP+Ge+l1dl?= =?utf-8?q?f65FNUva0DOxsvuoIizZbZNTp2udFW3SaciYHNMtg12pyksgztsNnHUc2kRyWkTTD?= =?utf-8?q?Po/V6BUFACMB8HdUn7pdcpHp0gKnYnQf1w0GhgVii2zA48/ZJwMSpqvPoNdulRCDy?= =?utf-8?q?v+AvlCL+ZG/HZYRBHh3XkmCPFq1BwD/Nrg=3D=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: LO0P265MB3404.GBRP265.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: f33f038c-f04e-40fb-235f-08dc1c13d279 X-MS-Exchange-CrossTenant-originalarrivaltime: 23 Jan 2024 13:04:24.0458 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 0d5fd8bb-e8c2-4e0a-8dd5-2c264f7140fe X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: 3DbZsfTibXM6loHMRb7f9J+PWjTNovSA5bxvOvFpZEeFtPD11bcIplj8d+8yotUFBDkCbrUPwx0xs7G+brKCeQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LO2P265MB5088 X-OriginatorOrg: imgtec.com X-EXCLAIMER-MD-CONFIG: 15a78312-3e47-46eb-9010-2e54d84a9631 X-Proofpoint-ORIG-GUID: fHJKybeNfXiO8Z8TPcVawM-3CaBAW4AW X-Proofpoint-GUID: fHJKybeNfXiO8Z8TPcVawM-3CaBAW4AW X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Thomas Zimmermann , Maxime Ripard , Daniel Vetter , David Airlie Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Donald Robson When the kernel driver 'loses' the device, for instance if the firmware stops communicating, the driver calls drm_dev_unplug(). This is currently done inside the drm_dev_enter() critical section, which isn't permitted. In addition, the bool that marks the device as lost is not atomic or protected by a lock. This fix replaces the bool with an atomic that also acts as a mechanism to ensure the device is unplugged after drm_dev_exit(), preventing a concurrent call to drm_dev_enter() from succeeding in a race between it and drm_dev_unplug(). Reported-by: Steven Price Closes: https://lore.kernel.org/dri-devel/1b957ca4-71cf-42fd-ac81-1920592b952d@arm.com/ Fixes: cc1aeedb98ad ("drm/imagination: Implement firmware infrastructure and META FW support") Signed-off-by: Donald Robson Signed-off-by: Matt Coster --- drivers/gpu/drm/imagination/pvr_ccb.c | 2 +- drivers/gpu/drm/imagination/pvr_device.c | 98 +++++++++++++++++++++- drivers/gpu/drm/imagination/pvr_device.h | 72 +++++++++++++--- drivers/gpu/drm/imagination/pvr_drv.c | 87 ++++++++++--------- drivers/gpu/drm/imagination/pvr_fw.c | 12 +-- drivers/gpu/drm/imagination/pvr_fw_trace.c | 4 +- drivers/gpu/drm/imagination/pvr_mmu.c | 20 ++--- drivers/gpu/drm/imagination/pvr_power.c | 42 +++------- drivers/gpu/drm/imagination/pvr_power.h | 2 - 9 files changed, 237 insertions(+), 102 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_ccb.c b/drivers/gpu/drm/imagination/pvr_ccb.c index 4deeac7ed40a..1fe64adc0c2c 100644 --- a/drivers/gpu/drm/imagination/pvr_ccb.c +++ b/drivers/gpu/drm/imagination/pvr_ccb.c @@ -247,7 +247,7 @@ pvr_kccb_send_cmd_reserved_powered(struct pvr_device *pvr_dev, u32 old_write_offset; u32 new_write_offset; - WARN_ON(pvr_dev->lost); + WARN_ON(pvr_device_is_lost(pvr_dev)); mutex_lock(&pvr_ccb->lock); diff --git a/drivers/gpu/drm/imagination/pvr_device.c b/drivers/gpu/drm/imagination/pvr_device.c index 1704c0268589..397491375b7d 100644 --- a/drivers/gpu/drm/imagination/pvr_device.c +++ b/drivers/gpu/drm/imagination/pvr_device.c @@ -6,14 +6,15 @@ #include "pvr_fw.h" #include "pvr_params.h" -#include "pvr_power.h" #include "pvr_queue.h" #include "pvr_rogue_cr_defs.h" #include "pvr_stream.h" #include "pvr_vm.h" +#include #include +#include #include #include #include @@ -556,6 +557,101 @@ pvr_device_fini(struct pvr_device *pvr_dev) pvr_device_gpu_fini(pvr_dev); } +/** + * pvr_device_enter() - Try to enter device critical section. + * @pvr_dev: Target PowerVR device. + * @idx: Pointer to index that will be passed to the matching pvr_device_exit(). + * + * Use this in place of drm_dev_enter() within this driver. + * + * Returns: + * * %true if the critical section was entered, or + * * %false otherwise. + */ +bool pvr_device_enter(struct pvr_device *pvr_dev, int *idx) +{ + const enum pvr_device_state old_state = + atomic_cmpxchg(&pvr_dev->state, + PVR_DEVICE_STATE_PRESENT, + PVR_DEVICE_STATE_ENTERED); + + switch (old_state) { + case PVR_DEVICE_STATE_PRESENT: + case PVR_DEVICE_STATE_ENTERED: + return drm_dev_enter(from_pvr_device(pvr_dev), idx); + + case PVR_DEVICE_STATE_LOST: + case PVR_DEVICE_STATE_LOST_UNPLUGGED: + WARN_ONCE(1, "Attempt to use GPU after becoming lost."); + break; + } + + return false; +} + +/** + * pvr_device_exit() - Exit a device critical section. + * @pvr_dev: Target PowerVR device. + * @idx: Index given by matching pvr_device_enter(). + * + * Use this in place of drm_dev_exit() within this driver. + */ +void pvr_device_exit(struct pvr_device *pvr_dev, int idx) +{ + const enum pvr_device_state old_state = + atomic_cmpxchg(&pvr_dev->state, + PVR_DEVICE_STATE_ENTERED, + PVR_DEVICE_STATE_PRESENT); + + switch (old_state) { + case PVR_DEVICE_STATE_PRESENT: + case PVR_DEVICE_STATE_ENTERED: + drm_dev_exit(idx); + return; + + case PVR_DEVICE_STATE_LOST: + /* Unplug the device if it was lost during the critical section. */ + atomic_set(&pvr_dev->state, PVR_DEVICE_STATE_LOST_UNPLUGGED); + + drm_dev_exit(idx); + + WARN_ONCE(1, "GPU lost and now unplugged."); + drm_dev_unplug(from_pvr_device(pvr_dev)); + + return; + + case PVR_DEVICE_STATE_LOST_UNPLUGGED: + WARN_ONCE(1, "GPU unplugged during critical section."); + drm_dev_exit(idx); + return; + } +} + +/** + * pvr_device_set_lost() - Mark GPU device as lost. + * @pvr_dev: Target PowerVR device. + * + * This will cause the DRM device to be unplugged at the next call to + * pvr_device_exit(). + */ +void pvr_device_set_lost(struct pvr_device *pvr_dev) +{ + /* + * Unplug the device immediately if the device is not in a critical + * section. + */ + const bool unplug = atomic_cmpxchg(&pvr_dev->state, + PVR_DEVICE_STATE_PRESENT, + PVR_DEVICE_STATE_LOST_UNPLUGGED) == + PVR_DEVICE_STATE_PRESENT; + + if (unplug) + drm_dev_unplug(from_pvr_device(pvr_dev)); + else + atomic_cmpxchg(&pvr_dev->state, PVR_DEVICE_STATE_ENTERED, + PVR_DEVICE_STATE_LOST); +} + bool pvr_device_has_uapi_quirk(struct pvr_device *pvr_dev, u32 quirk) { diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h index ecdd5767d8ef..7c08b2bda395 100644 --- a/drivers/gpu/drm/imagination/pvr_device.h +++ b/drivers/gpu/drm/imagination/pvr_device.h @@ -274,20 +274,19 @@ struct pvr_device { } kccb; /** - * @lost: %true if the device has been lost. - * - * This variable is set if the device has become irretrievably unavailable, e.g. if the - * firmware processor has stopped responding and can not be revived via a hard reset. + * @state: Indicates whether the device is present and in use. One of + * &enum pvr_device_state. */ - bool lost; + atomic_t state; /** * @reset_sem: Reset semaphore. * - * GPU reset code will lock this for writing. Any code that submits commands to the firmware - * that isn't in an IRQ handler or on the scheduler workqueue must lock this for reading. - * Once this has been successfully locked, &pvr_dev->lost _must_ be checked, and -%EIO must - * be returned if it is set. + * GPU reset code will lock this for writing. Any code that submits + * commands to the firmware that isn't in an IRQ handler or on the + * scheduler workqueue must lock this for reading. + * Once this has been successfully locked, pvr_device_is_lost() _must_ + * be checked, and -%EIO must be returned if it is. */ struct rw_semaphore reset_sem; @@ -487,7 +486,60 @@ packed_bvnc_to_pvr_gpu_id(u64 bvnc, struct pvr_gpu_id *gpu_id) int pvr_device_init(struct pvr_device *pvr_dev); void pvr_device_fini(struct pvr_device *pvr_dev); -void pvr_device_reset(struct pvr_device *pvr_dev); + +/** + * enum pvr_device_state - States used by &struct pvr_device.state. + */ +enum pvr_device_state { + /** @PVR_DEVICE_STATE_PRESENT: The device is available for use. */ + PVR_DEVICE_STATE_PRESENT, + + /** @PVR_DEVICE_STATE_ENTERED: The device is in a critical section. */ + PVR_DEVICE_STATE_ENTERED, + + /** + * @PVR_DEVICE_STATE_LOST: The device was lost during the current device + * critical section and will be unplugged once the section exits. + */ + PVR_DEVICE_STATE_LOST, + + /** + * @PVR_DEVICE_STATE_LOST_UNPLUGGED: The device was lost and + * subsequently unplugged. + * + * The device may become irretrievably unavailable, e.g. if the firmware + * processor has stopped responding and can not be revived via a hard + * reset. + */ + PVR_DEVICE_STATE_LOST_UNPLUGGED, +}; + +/** + * pvr_device_is_lost() - Check if GPU device has been marked lost. + * @pvr_dev: Target PowerVR device. + * + * Returns: + * * %true if device is lost, or + * * %false otherwise. + */ +static __always_inline bool pvr_device_is_lost(struct pvr_device *pvr_dev) +{ + switch ((enum pvr_device_state)atomic_read(&pvr_dev->state)) { + case PVR_DEVICE_STATE_PRESENT: + case PVR_DEVICE_STATE_ENTERED: + return false; + + case PVR_DEVICE_STATE_LOST: + case PVR_DEVICE_STATE_LOST_UNPLUGGED: + break; + } + + return true; +} + +bool pvr_device_enter(struct pvr_device *pvr_dev, int *idx); +void pvr_device_exit(struct pvr_device *pvr_dev, int idx); +void pvr_device_set_lost(struct pvr_device *pvr_dev); bool pvr_device_has_uapi_quirk(struct pvr_device *pvr_dev, u32 quirk); diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c index 5c3b2d58d766..55bea656a40f 100644 --- a/drivers/gpu/drm/imagination/pvr_drv.c +++ b/drivers/gpu/drm/imagination/pvr_drv.c @@ -81,13 +81,13 @@ pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args, int idx; int err; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; /* All padding fields must be zeroed. */ if (args->_padding_c != 0) { err = -EINVAL; - goto err_drm_dev_exit; + goto err_pvr_device_exit; } /* @@ -101,7 +101,7 @@ pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args, if (args->size > SIZE_MAX || args->size == 0 || args->flags & ~DRM_PVR_BO_FLAGS_MASK || args->size & (PVR_DEVICE_PAGE_SIZE - 1)) { err = -EINVAL; - goto err_drm_dev_exit; + goto err_pvr_device_exit; } sanitized_size = (size_t)args->size; @@ -113,7 +113,7 @@ pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args, pvr_obj = pvr_gem_object_create(pvr_dev, sanitized_size, args->flags); if (IS_ERR(pvr_obj)) { err = PTR_ERR(pvr_obj); - goto err_drm_dev_exit; + goto err_pvr_device_exit; } /* This function will not modify &args->handle unless it succeeds. */ @@ -121,7 +121,7 @@ pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args, if (err) goto err_destroy_obj; - drm_dev_exit(idx); + pvr_device_exit(pvr_dev, idx); return 0; @@ -133,8 +133,8 @@ pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args, */ pvr_gem_object_put(pvr_obj); -err_drm_dev_exit: - drm_dev_exit(idx); +err_pvr_device_exit: + pvr_device_exit(pvr_dev, idx); return err; } @@ -164,19 +164,20 @@ pvr_ioctl_get_bo_mmap_offset(struct drm_device *drm_dev, void *raw_args, struct drm_file *file) { struct drm_pvr_ioctl_get_bo_mmap_offset_args *args = raw_args; + struct pvr_device *pvr_dev = to_pvr_device(drm_dev); struct pvr_file *pvr_file = to_pvr_file(file); struct pvr_gem_object *pvr_obj; struct drm_gem_object *gem_obj; int idx; int ret; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; /* All padding fields must be zeroed. */ if (args->_padding_4 != 0) { ret = -EINVAL; - goto err_drm_dev_exit; + goto err_pvr_device_exit; } /* @@ -188,7 +189,7 @@ pvr_ioctl_get_bo_mmap_offset(struct drm_device *drm_dev, void *raw_args, pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle); if (!pvr_obj) { ret = -ENOENT; - goto err_drm_dev_exit; + goto err_pvr_device_exit; } gem_obj = gem_from_pvr_gem(pvr_obj); @@ -202,7 +203,7 @@ pvr_ioctl_get_bo_mmap_offset(struct drm_device *drm_dev, void *raw_args, if (ret != 0) { /* Drop our reference to the buffer object. */ drm_gem_object_put(gem_obj); - goto err_drm_dev_exit; + goto err_pvr_device_exit; } /* @@ -214,8 +215,8 @@ pvr_ioctl_get_bo_mmap_offset(struct drm_device *drm_dev, void *raw_args, /* Drop our reference to the buffer object. */ pvr_gem_object_put(pvr_obj); -err_drm_dev_exit: - drm_dev_exit(idx); +err_pvr_device_exit: + pvr_device_exit(pvr_dev, idx); return ret; } @@ -626,7 +627,7 @@ pvr_ioctl_dev_query(struct drm_device *drm_dev, void *raw_args, int idx; int ret = -EINVAL; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; switch ((enum drm_pvr_dev_query)args->type) { @@ -655,7 +656,7 @@ pvr_ioctl_dev_query(struct drm_device *drm_dev, void *raw_args, break; } - drm_dev_exit(idx); + pvr_device_exit(pvr_dev, idx); return ret; } @@ -680,16 +681,17 @@ pvr_ioctl_create_context(struct drm_device *drm_dev, void *raw_args, struct drm_file *file) { struct drm_pvr_ioctl_create_context_args *args = raw_args; + struct pvr_device *pvr_dev = to_pvr_device(drm_dev); struct pvr_file *pvr_file = file->driver_priv; int idx; int ret; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; ret = pvr_context_create(pvr_file, args); - drm_dev_exit(idx); + pvr_device_exit(pvr_dev, idx); return ret; } @@ -738,18 +740,19 @@ pvr_ioctl_create_free_list(struct drm_device *drm_dev, void *raw_args, struct drm_file *file) { struct drm_pvr_ioctl_create_free_list_args *args = raw_args; + struct pvr_device *pvr_dev = to_pvr_device(drm_dev); struct pvr_file *pvr_file = to_pvr_file(file); struct pvr_free_list *free_list; int idx; int err; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; free_list = pvr_free_list_create(pvr_file, args); if (IS_ERR(free_list)) { err = PTR_ERR(free_list); - goto err_drm_dev_exit; + goto err_pvr_device_exit; } /* Allocate object handle for userspace. */ @@ -761,15 +764,15 @@ pvr_ioctl_create_free_list(struct drm_device *drm_dev, void *raw_args, if (err < 0) goto err_cleanup; - drm_dev_exit(idx); + pvr_device_exit(pvr_dev, idx); return 0; err_cleanup: pvr_free_list_put(free_list); -err_drm_dev_exit: - drm_dev_exit(idx); +err_pvr_device_exit: + pvr_device_exit(pvr_dev, idx); return err; } @@ -824,18 +827,19 @@ pvr_ioctl_create_hwrt_dataset(struct drm_device *drm_dev, void *raw_args, struct drm_file *file) { struct drm_pvr_ioctl_create_hwrt_dataset_args *args = raw_args; + struct pvr_device *pvr_dev = to_pvr_device(drm_dev); struct pvr_file *pvr_file = to_pvr_file(file); struct pvr_hwrt_dataset *hwrt; int idx; int err; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; hwrt = pvr_hwrt_dataset_create(pvr_file, args); if (IS_ERR(hwrt)) { err = PTR_ERR(hwrt); - goto err_drm_dev_exit; + goto err_pvr_device_exit; } /* Allocate object handle for userspace. */ @@ -847,15 +851,15 @@ pvr_ioctl_create_hwrt_dataset(struct drm_device *drm_dev, void *raw_args, if (err < 0) goto err_cleanup; - drm_dev_exit(idx); + pvr_device_exit(pvr_dev, idx); return 0; err_cleanup: pvr_hwrt_dataset_put(hwrt); -err_drm_dev_exit: - drm_dev_exit(idx); +err_pvr_device_exit: + pvr_device_exit(pvr_dev, idx); return err; } @@ -910,23 +914,24 @@ pvr_ioctl_create_vm_context(struct drm_device *drm_dev, void *raw_args, struct drm_file *file) { struct drm_pvr_ioctl_create_vm_context_args *args = raw_args; + struct pvr_device *pvr_dev = to_pvr_device(drm_dev); struct pvr_file *pvr_file = to_pvr_file(file); struct pvr_vm_context *vm_ctx; int idx; int err; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; if (args->_padding_4) { err = -EINVAL; - goto err_drm_dev_exit; + goto err_pvr_device_exit; } vm_ctx = pvr_vm_create_context(pvr_file->pvr_dev, true); if (IS_ERR(vm_ctx)) { err = PTR_ERR(vm_ctx); - goto err_drm_dev_exit; + goto err_pvr_device_exit; } /* Allocate object handle for userspace. */ @@ -938,15 +943,15 @@ pvr_ioctl_create_vm_context(struct drm_device *drm_dev, void *raw_args, if (err < 0) goto err_cleanup; - drm_dev_exit(idx); + pvr_device_exit(pvr_dev, idx); return 0; err_cleanup: pvr_vm_context_put(vm_ctx); -err_drm_dev_exit: - drm_dev_exit(idx); +err_pvr_device_exit: + pvr_device_exit(pvr_dev, idx); return err; } @@ -1022,26 +1027,26 @@ pvr_ioctl_vm_map(struct drm_device *drm_dev, void *raw_args, int idx; int err; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; /* Initial validation of args. */ if (args->_padding_14) { err = -EINVAL; - goto err_drm_dev_exit; + goto err_pvr_device_exit; } if (args->flags != 0 || check_add_overflow(args->offset, args->size, &offset_plus_size) || !pvr_find_heap_containing(pvr_dev, args->device_addr, args->size)) { err = -EINVAL; - goto err_drm_dev_exit; + goto err_pvr_device_exit; } vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle); if (!vm_ctx) { err = -EINVAL; - goto err_drm_dev_exit; + goto err_pvr_device_exit; } pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle); @@ -1079,8 +1084,8 @@ pvr_ioctl_vm_map(struct drm_device *drm_dev, void *raw_args, err_put_vm_context: pvr_vm_context_put(vm_ctx); -err_drm_dev_exit: - drm_dev_exit(idx); +err_pvr_device_exit: + pvr_device_exit(pvr_dev, idx); return err; } @@ -1148,12 +1153,12 @@ pvr_ioctl_submit_jobs(struct drm_device *drm_dev, void *raw_args, int idx; int err; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; err = pvr_submit_jobs(pvr_dev, pvr_file, args); - drm_dev_exit(idx); + pvr_device_exit(pvr_dev, idx); return err; } diff --git a/drivers/gpu/drm/imagination/pvr_fw.c b/drivers/gpu/drm/imagination/pvr_fw.c index 3debc9870a82..07547e167963 100644 --- a/drivers/gpu/drm/imagination/pvr_fw.c +++ b/drivers/gpu/drm/imagination/pvr_fw.c @@ -1094,7 +1094,7 @@ pvr_fw_structure_cleanup(struct pvr_device *pvr_dev, u32 type, struct pvr_fw_obj down_read(&pvr_dev->reset_sem); - if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx)) { + if (!pvr_device_enter(pvr_dev, &idx)) { err = -EIO; goto err_up_read; } @@ -1118,22 +1118,22 @@ pvr_fw_structure_cleanup(struct pvr_device *pvr_dev, u32 type, struct pvr_fw_obj break; default: err = -EINVAL; - goto err_drm_dev_exit; + goto err_pvr_device_exit; } err = pvr_kccb_send_cmd(pvr_dev, &cmd, &slot_nr); if (err) - goto err_drm_dev_exit; + goto err_pvr_device_exit; err = pvr_kccb_wait_for_completion(pvr_dev, slot_nr, HZ, &rtn); if (err) - goto err_drm_dev_exit; + goto err_pvr_device_exit; if (rtn & ROGUE_FWIF_KCCB_RTN_SLOT_CLEANUP_BUSY) err = -EBUSY; -err_drm_dev_exit: - drm_dev_exit(idx); +err_pvr_device_exit: + pvr_device_exit(pvr_dev, idx); err_up_read: up_read(&pvr_dev->reset_sem); diff --git a/drivers/gpu/drm/imagination/pvr_fw_trace.c b/drivers/gpu/drm/imagination/pvr_fw_trace.c index 31199e45b72e..26d67483eac2 100644 --- a/drivers/gpu/drm/imagination/pvr_fw_trace.c +++ b/drivers/gpu/drm/imagination/pvr_fw_trace.c @@ -149,7 +149,7 @@ update_logtype(struct pvr_device *pvr_dev, u32 group_mask) fw_trace->group_mask = group_mask; down_read(&pvr_dev->reset_sem); - if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx)) { + if (!pvr_device_enter(pvr_dev, &idx)) { err = -EIO; goto err_up_read; } @@ -159,7 +159,7 @@ update_logtype(struct pvr_device *pvr_dev, u32 group_mask) err = pvr_kccb_send_cmd(pvr_dev, &cmd, NULL); - drm_dev_exit(idx); + pvr_device_exit(pvr_dev, idx); err_up_read: up_read(&pvr_dev->reset_sem); diff --git a/drivers/gpu/drm/imagination/pvr_mmu.c b/drivers/gpu/drm/imagination/pvr_mmu.c index 4fe70610ed94..59911f70e9ca 100644 --- a/drivers/gpu/drm/imagination/pvr_mmu.c +++ b/drivers/gpu/drm/imagination/pvr_mmu.c @@ -129,18 +129,18 @@ int pvr_mmu_flush_exec(struct pvr_device *pvr_dev, bool wait) u32 slot; int idx; - if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; /* Can't flush MMU if the firmware hasn't booted yet. */ if (!pvr_dev->fw_dev.booted) - goto err_drm_dev_exit; + goto err_pvr_device_exit; cmd_mmu_cache_data->cache_flags = atomic_xchg(&pvr_dev->mmu_flush_cache_flags, 0); if (!cmd_mmu_cache_data->cache_flags) - goto err_drm_dev_exit; + goto err_pvr_device_exit; cmd_mmu_cache.cmd_type = ROGUE_FWIF_KCCB_CMD_MMUCACHE; @@ -156,7 +156,7 @@ int pvr_mmu_flush_exec(struct pvr_device *pvr_dev, bool wait) if (err) goto err_reset_and_retry; - drm_dev_exit(idx); + pvr_device_exit(pvr_dev, idx); return 0; @@ -167,23 +167,23 @@ int pvr_mmu_flush_exec(struct pvr_device *pvr_dev, bool wait) */ err = pvr_power_reset(pvr_dev, true); if (err) - goto err_drm_dev_exit; /* Device is lost. */ + goto err_pvr_device_exit; /* Device is lost. */ /* Retry sending flush request. */ err = pvr_kccb_send_cmd(pvr_dev, &cmd_mmu_cache, &slot); if (err) { - pvr_device_lost(pvr_dev); - goto err_drm_dev_exit; + pvr_device_set_lost(pvr_dev); + goto err_pvr_device_exit; } if (wait) { err = pvr_kccb_wait_for_completion(pvr_dev, slot, HZ, NULL); if (err) - pvr_device_lost(pvr_dev); + pvr_device_set_lost(pvr_dev); } -err_drm_dev_exit: - drm_dev_exit(idx); +err_pvr_device_exit: + pvr_device_exit(pvr_dev, idx); return err; } diff --git a/drivers/gpu/drm/imagination/pvr_power.c b/drivers/gpu/drm/imagination/pvr_power.c index ba7816fd28ec..c927def3d3f3 100644 --- a/drivers/gpu/drm/imagination/pvr_power.c +++ b/drivers/gpu/drm/imagination/pvr_power.c @@ -23,21 +23,6 @@ #define WATCHDOG_TIME_MS (500) -/** - * pvr_device_lost() - Mark GPU device as lost - * @pvr_dev: Target PowerVR device. - * - * This will cause the DRM device to be unplugged. - */ -void -pvr_device_lost(struct pvr_device *pvr_dev) -{ - if (!pvr_dev->lost) { - pvr_dev->lost = true; - drm_dev_unplug(from_pvr_device(pvr_dev)); - } -} - static int pvr_power_send_command(struct pvr_device *pvr_dev, struct rogue_fwif_kccb_cmd *pow_cmd) { @@ -186,7 +171,7 @@ pvr_watchdog_worker(struct work_struct *work) watchdog.work.work); bool stalled; - if (pvr_dev->lost) + if (pvr_device_is_lost(pvr_dev)) return; if (pm_runtime_get_if_in_use(from_pvr_device(pvr_dev)->dev) <= 0) @@ -208,10 +193,9 @@ pvr_watchdog_worker(struct work_struct *work) pm_runtime_put(from_pvr_device(pvr_dev)->dev); out_requeue: - if (!pvr_dev->lost) { + if (!pvr_device_is_lost(pvr_dev)) queue_delayed_work(pvr_dev->sched_wq, &pvr_dev->watchdog.work, msecs_to_jiffies(WATCHDOG_TIME_MS)); - } } /** @@ -239,21 +223,21 @@ pvr_power_device_suspend(struct device *dev) int err = 0; int idx; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; if (pvr_dev->fw_dev.booted) { err = pvr_power_fw_disable(pvr_dev, false); if (err) - goto err_drm_dev_exit; + goto err_pvr_device_exit; } clk_disable_unprepare(pvr_dev->mem_clk); clk_disable_unprepare(pvr_dev->sys_clk); clk_disable_unprepare(pvr_dev->core_clk); -err_drm_dev_exit: - drm_dev_exit(idx); +err_pvr_device_exit: + pvr_device_exit(pvr_dev, idx); return err; } @@ -267,12 +251,12 @@ pvr_power_device_resume(struct device *dev) int idx; int err; - if (!drm_dev_enter(drm_dev, &idx)) + if (!pvr_device_enter(pvr_dev, &idx)) return -EIO; err = clk_prepare_enable(pvr_dev->core_clk); if (err) - goto err_drm_dev_exit; + goto err_pvr_device_exit; err = clk_prepare_enable(pvr_dev->sys_clk); if (err) @@ -288,7 +272,7 @@ pvr_power_device_resume(struct device *dev) goto err_mem_clk_disable; } - drm_dev_exit(idx); + pvr_device_exit(pvr_dev, idx); return 0; @@ -301,8 +285,8 @@ pvr_power_device_resume(struct device *dev) err_core_clk_disable: clk_disable_unprepare(pvr_dev->core_clk); -err_drm_dev_exit: - drm_dev_exit(idx); +err_pvr_device_exit: + pvr_device_exit(pvr_dev, idx); return err; } @@ -345,7 +329,7 @@ pvr_power_reset(struct pvr_device *pvr_dev, bool hard_reset) down_write(&pvr_dev->reset_sem); - if (pvr_dev->lost) { + if (pvr_device_is_lost(pvr_dev)) { err = -EIO; goto err_up_write; } @@ -407,7 +391,7 @@ pvr_power_reset(struct pvr_device *pvr_dev, bool hard_reset) err_device_lost: drm_err(from_pvr_device(pvr_dev), "GPU device lost"); - pvr_device_lost(pvr_dev); + pvr_device_set_lost(pvr_dev); /* Leave IRQs disabled if the device is lost. */ diff --git a/drivers/gpu/drm/imagination/pvr_power.h b/drivers/gpu/drm/imagination/pvr_power.h index 9a9312dcb2da..360980f454d7 100644 --- a/drivers/gpu/drm/imagination/pvr_power.h +++ b/drivers/gpu/drm/imagination/pvr_power.h @@ -12,8 +12,6 @@ int pvr_watchdog_init(struct pvr_device *pvr_dev);