From nobody Tue Feb 10 05:26:35 2026 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2060.outbound.protection.outlook.com [40.107.237.60]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B99F146A99 for ; Fri, 19 Jul 2024 17:51:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721411498; cv=fail; b=MF+xzxg4URoW5wTrtSPaVD9ttQrgCgOPKciSpLBEXrg5GJFfEZ8aukSeMKqE6gJ9emSkhqHeb+v4zJdVGyB/dmQa6xoJrnw2DKzbYQLNhFRw3V9ELbws0GdpknZg47E8Ew4vTjQ3NiGoIbtWA6Zjxl9+bMAhGAXKu0Hyr5soRCw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721411498; c=relaxed/simple; bh=2Lfi8jFWcZtCRIuJ9y/CTZnUjlCdQgvK/QBPKoflEgo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=sorPM3/DUlSP6ILf//c3SADWoVVcxnpRy0OCcz+utqASvU7YISH470GqvoXOVft938m/4C/88Ba+NZsgjOCQ19VxNDtt+CcfZ3dk5ODDAvhLouYhQJN2MLsTrv2ai9hbJzBl1q9uqvaJ8KeNPK0mWBM89B2AqId3nTlktWPCwiU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=tFCG3Avm; arc=fail smtp.client-ip=40.107.237.60 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="tFCG3Avm" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=NpGpHhr/kUa6YuleMkOaNA7wJAMv0QIJkANSoeherSSqhliTTtGIPNHJdtLTVhc0cSDAngfC+zvEfJWjBqDkzURCWH/125B+3Ovy3z8zGuHw70E1yuL5moxVYvQL4AeOQ7Ct8bsuUO9mY7FFBcvkDmaDH2FBKmmvloj67+LhguZCMUzNdQKxtV3/3crnkQ+wB5SgHZuK5m9IqXCxWofJ4KN4BrM/lFy+/dKO/5ouGXMbtDRtkr2FOTT+HwSAvzFAW2V/oODeShQazzmdNXgfh4WSdqxaXLQPifjq8iQQe0m5t9JHyITylo3L6stMNuceo9R78axufOyvkoJeo4KWug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1/437n0BeDo2StvfzCLu1unf8LUhwGn5i9Jb/pwQ4Ks=; b=cTXdFl4KElG/kHZDl7Ngv1zTChAlo0FABXhqYH0rZBLzy4Bq3tL6TL7mCsVRPtCQXT64eIOcQ2Og4M7Zjsyx/qxl7HdsnHsj2P6naTOVwP5ilIzVbny6OE+r7qjHMpUTvHK/3Ubiir8VkGGIdViOjYTmEpjw7k/97+YurTMYbfxz91z3TT2INC9nED7OREofgbGKMmU77fYhVB0Uuxf1vNZLK2W7SqMBVfakK6QHoVzRCXSk3kGRM/BO4VuqD1+Gy/zIUR/TzhkKLVz+G7D+BLecX3QHhpx9k8iexsJxj93e/sK0zAlZha9pXDM5jfUNnSzXI+6lyzvhN6LUzqqfHw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1/437n0BeDo2StvfzCLu1unf8LUhwGn5i9Jb/pwQ4Ks=; b=tFCG3AvmE5H8pD1Vmk65gdakymrv1EE5HPv0xLzU6piJYv/zWjIqJOjcUwvagLQGE3YJLXG+VL+YMzSZfEMZGBr2hNYX+S5d316Qvk2vfMSTGvEXveNA0KhPR6VEis+wZ7wiqV9x57au8xvtjM+COrBdemlNAT3sqEZCLUXBtKo= Received: from SJ0PR03CA0294.namprd03.prod.outlook.com (2603:10b6:a03:39e::29) by DS7PR12MB6119.namprd12.prod.outlook.com (2603:10b6:8:99::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7784.16; Fri, 19 Jul 2024 17:51:28 +0000 Received: from SJ1PEPF000023DA.namprd21.prod.outlook.com (2603:10b6:a03:39e:cafe::d0) by SJ0PR03CA0294.outlook.office365.com (2603:10b6:a03:39e::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7784.20 via Frontend Transport; Fri, 19 Jul 2024 17:51:28 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF000023DA.mail.protection.outlook.com (10.167.244.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7784.5 via Frontend Transport; Fri, 19 Jul 2024 17:51:27 +0000 Received: from SATLEXMB05.amd.com (10.181.40.146) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 19 Jul 2024 12:51:27 -0500 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB05.amd.com (10.181.40.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 19 Jul 2024 12:51:26 -0500 Received: from xsjlizhih51.xilinx.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.39 via Frontend Transport; Fri, 19 Jul 2024 12:51:26 -0500 From: Lizhi Hou To: , CC: Lizhi Hou , , , , , Narendra Gutta , Xiaoming Ren Subject: [PATCH V1 07/10] accel/amdxdna: Add suspend and resume Date: Fri, 19 Jul 2024 10:51:25 -0700 Message-ID: <20240719175128.2257677-8-lizhi.hou@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240719175128.2257677-1-lizhi.hou@amd.com> References: <20240719175128.2257677-1-lizhi.hou@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: None (SATLEXMB05.amd.com: lizhi.hou@amd.com does not designate permitted sender hosts) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF000023DA:EE_|DS7PR12MB6119:EE_ X-MS-Office365-Filtering-Correlation-Id: f964797d-7ce8-4c73-8429-08dca81b6a43 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|82310400026|376014|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?SeLeQEyvgWqUfiFlgUmn+ICfp4o6Fw807K4sF/CpQqIQd3d1gBm3r49GZPtW?= =?us-ascii?Q?cmk5GSdtJlI+gdzhdfBxmZHWIZpnt9H8t4V2utyMR6L6RLd1cQDSYKfquSdK?= =?us-ascii?Q?tIBVltN0D0OWDCiX0L9+bk5/34okmcx0KfJ2R0eAPilKaFTuj5IujOvIayO5?= =?us-ascii?Q?UhZ5Jp4/ALTAwCPxKefS1yL7IFOvhxGlQT51PKHwO84htu8IvGEiB0l1Kw5R?= =?us-ascii?Q?GBFCuQAnCFClgRs7f279D0FZ/7Pc/seuqP/zkcVjp1kwIO5Bjfh3lX/vspGJ?= =?us-ascii?Q?KqS8O4qrqPWFGsBTMlo57qy6k5DY5Caz7J8d66eFLyhhJx1ZBLpRBXcZZxh7?= =?us-ascii?Q?5Rmnutc2E+gSq+aBvzdNlzUeYDs8lmb/vvdIcvLIBcWvF2GdN18FJychhrSh?= =?us-ascii?Q?vrf24LIbpsL4kyG5ExCBbikAUD70ztIag2keD1UnIFtYyXqTtVBcCROA8LBG?= =?us-ascii?Q?fDJ2NRA715aCVZdDx0xyViEpDG7fpCFyvUugyLBjSsYVXXs2BBv4gmwu/+i3?= =?us-ascii?Q?o3KPJ65D5ve4yK/xXtnZ3e7eRH6miQYW4enDmIFi4Lw3fAII7gbaAz8c1l9h?= =?us-ascii?Q?Oh8mCAke4pkguArJbkqDU0AddzTYlguzosfhAYEvtB3XizRcxx7+vNPsUzeW?= =?us-ascii?Q?UkzWYOzNfftRVEkOmCnoauhwxD1zd5QfcllWX77vnSEukO9uMmg5VnnMe7P8?= =?us-ascii?Q?kZFMUTZaeiOvZS/u48Tqq9V7rmJxEkJ3ruaRRh2U5hxIWS2Txrn0JdgzK68p?= =?us-ascii?Q?yyMn0+tiTGunoTCUfvBIGaclxHONnR2Tz+mIsSoYZW8bhEPj9EI5xtHBh4rd?= =?us-ascii?Q?6CFdIqheBQMV/0QatigomVXwgdYlkNPCyA1hmx5sUS8LN+363pzUa4KGApZN?= =?us-ascii?Q?5ldv0Uk7LJdePybDZEoPs9GoJ6qyd0auaOBoRrokM7Uro3ef8cp9k+1Or613?= =?us-ascii?Q?7/9+wxsecuvR/QoyG00tlVQQFs5z9C+yJFMADdgEXBX9UbSqPGWraTd9X6ki?= =?us-ascii?Q?AxktAXsAym4pp/ihAMt6cvpCbk9zfhh2FGajIcOG/GXS7v4UdwU3T0/jQ1R3?= =?us-ascii?Q?rhmTq1Pw/pDdwd+X5+E2Pl++zWlAolLtw1TCvj/HT+L+xxkwoEbGBJiodqIM?= =?us-ascii?Q?afsasozskBstIa2n6pDHl+AE3rqGctf3TMeAGkyUvLt3aM91iQ7rX13gfPeN?= =?us-ascii?Q?q2kXAOYY2XcXq3dnd8OHWIPAEuFx8aioWDLTY57LFzdPsDqK/EVuPMzbnV+I?= =?us-ascii?Q?Yvt1K5xr/ftYgWSlDoiMl8TWX4jJw2jcFARAeb0210GqixV6liST6v82pvHG?= =?us-ascii?Q?POS8rfo9IJ+ZhBMhqoMFUYPrlO6B9y/buJ0jm0KePCzP0MMk4awQl/IwmSpF?= =?us-ascii?Q?X+pm1/LF5TpBIjSeQD535BUpPc7Y0m1ejKHa7yKpAk5379rnmPj1rTDxnB1W?= =?us-ascii?Q?81koTopy7ENFH2exyHal2XclvQNhXDmH?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(82310400026)(376014)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jul 2024 17:51:27.8754 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f964797d-7ce8-4c73-8429-08dca81b6a43 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF000023DA.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6119 Content-Type: text/plain; charset="utf-8" Implement PCI power management suspend and resume callbacks. Co-developed-by: Narendra Gutta Signed-off-by: Narendra Gutta Co-developed-by: Xiaoming Ren Signed-off-by: Xiaoming Ren Co-developed-by: Min Ma Signed-off-by: Min Ma Signed-off-by: Lizhi Hou --- drivers/accel/amdxdna/aie2_ctx.c | 30 +++++++ drivers/accel/amdxdna/aie2_pci.c | 4 + drivers/accel/amdxdna/aie2_pci.h | 2 + drivers/accel/amdxdna/amdxdna_ctx.c | 26 ++++++ drivers/accel/amdxdna/amdxdna_ctx.h | 2 + drivers/accel/amdxdna/amdxdna_drm.c | 17 +++- drivers/accel/amdxdna/amdxdna_drm.h | 5 ++ drivers/accel/amdxdna/amdxdna_pci_drv.c | 103 +++++++++++++++++++++++- 8 files changed, 186 insertions(+), 3 deletions(-) diff --git a/drivers/accel/amdxdna/aie2_ctx.c b/drivers/accel/amdxdna/aie2_= ctx.c index bec3ed9756f0..a392cc2b9237 100644 --- a/drivers/accel/amdxdna/aie2_ctx.c +++ b/drivers/accel/amdxdna/aie2_ctx.c @@ -168,6 +168,36 @@ static int aie2_hwctx_wait_for_idle(struct amdxdna_hwc= tx *hwctx) return 0; } =20 +void aie2_hwctx_suspend(struct amdxdna_hwctx *hwctx) +{ + struct amdxdna_dev *xdna =3D hwctx->client->xdna; + + /* + * Command timeout is unlikely. But if it happens, it doesn't + * break the system. aie2_hwctx_stop() will destroy mailbox + * and abort all commands. + */ + drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock)); + aie2_hwctx_wait_for_idle(hwctx); + aie2_hwctx_stop(xdna, hwctx, NULL); + hwctx->old_status =3D hwctx->status; + hwctx->status =3D HWCTX_STAT_STOP; +} + +void aie2_hwctx_resume(struct amdxdna_hwctx *hwctx) +{ + struct amdxdna_dev *xdna =3D hwctx->client->xdna; + + /* + * The resume path cannot guarantee that mailbox channel can be + * regenerated. If this happen, when submit message to this + * mailbox channel, error will return. + */ + drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock)); + hwctx->status =3D hwctx->old_status; + aie2_hwctx_restart(xdna, hwctx); +} + static inline void aie2_sched_notify(struct amdxdna_sched_job *job) { diff --git a/drivers/accel/amdxdna/aie2_pci.c b/drivers/accel/amdxdna/aie2_= pci.c index 8efff1ba6319..18c3f37d2324 100644 --- a/drivers/accel/amdxdna/aie2_pci.c +++ b/drivers/accel/amdxdna/aie2_pci.c @@ -490,10 +490,14 @@ static void aie2_fini(struct amdxdna_dev *xdna) const struct amdxdna_dev_ops aie2_ops =3D { .init =3D aie2_init, .fini =3D aie2_fini, + .resume =3D aie2_hw_start, + .suspend =3D aie2_hw_stop, .hwctx_init =3D aie2_hwctx_init, .hwctx_fini =3D aie2_hwctx_fini, .hwctx_config =3D aie2_hwctx_config, .cmd_submit =3D aie2_cmd_submit, .cmd_wait =3D aie2_cmd_wait, .hmm_invalidate =3D aie2_hmm_invalidate, + .hwctx_suspend =3D aie2_hwctx_suspend, + .hwctx_resume =3D aie2_hwctx_resume, }; diff --git a/drivers/accel/amdxdna/aie2_pci.h b/drivers/accel/amdxdna/aie2_= pci.h index 26d271a1040b..113395ab5e33 100644 --- a/drivers/accel/amdxdna/aie2_pci.h +++ b/drivers/accel/amdxdna/aie2_pci.h @@ -243,6 +243,8 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct am= dxdna_sched_job *job, int aie2_hwctx_init(struct amdxdna_hwctx *hwctx); void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx); int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, vo= id *buf, u32 size); +void aie2_hwctx_suspend(struct amdxdna_hwctx *hwctx); +void aie2_hwctx_resume(struct amdxdna_hwctx *hwctx); int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job = *job, u64 *seq); int aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout); void aie2_hmm_invalidate(struct amdxdna_gem_obj *abo, unsigned long cur_se= q); diff --git a/drivers/accel/amdxdna/amdxdna_ctx.c b/drivers/accel/amdxdna/am= dxdna_ctx.c index 399f9c359ac7..bafca7f0415f 100644 --- a/drivers/accel/amdxdna/amdxdna_ctx.c +++ b/drivers/accel/amdxdna/amdxdna_ctx.c @@ -48,6 +48,32 @@ static struct dma_fence *amdxdna_fence_create(struct amd= xdna_hwctx *hwctx) return &fence->base; } =20 +void amdxdna_hwctx_suspend(struct amdxdna_client *client) +{ + struct amdxdna_dev *xdna =3D client->xdna; + struct amdxdna_hwctx *hwctx; + int next =3D 0; + + drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock)); + mutex_lock(&client->hwctx_lock); + idr_for_each_entry_continue(&client->hwctx_idr, hwctx, next) + xdna->dev_info->ops->hwctx_suspend(hwctx); + mutex_unlock(&client->hwctx_lock); +} + +void amdxdna_hwctx_resume(struct amdxdna_client *client) +{ + struct amdxdna_dev *xdna =3D client->xdna; + struct amdxdna_hwctx *hwctx; + int next =3D 0; + + drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock)); + mutex_lock(&client->hwctx_lock); + idr_for_each_entry_continue(&client->hwctx_idr, hwctx, next) + xdna->dev_info->ops->hwctx_resume(hwctx); + mutex_unlock(&client->hwctx_lock); +} + static void amdxdna_hwctx_destroy_rcu(struct amdxdna_hwctx *hwctx, struct srcu_struct *ss) { diff --git a/drivers/accel/amdxdna/amdxdna_ctx.h b/drivers/accel/amdxdna/am= dxdna_ctx.h index c0761527e0b2..1a42964a2f87 100644 --- a/drivers/accel/amdxdna/amdxdna_ctx.h +++ b/drivers/accel/amdxdna/amdxdna_ctx.h @@ -187,6 +187,8 @@ static inline u32 amdxdna_hwctx_col_map(struct amdxdna_= hwctx *hwctx) void amdxdna_job_put(struct amdxdna_sched_job *job); =20 void amdxdna_hwctx_remove_all(struct amdxdna_client *client); +void amdxdna_hwctx_suspend(struct amdxdna_client *client); +void amdxdna_hwctx_resume(struct amdxdna_client *client); =20 int amdxdna_cmd_submit(struct amdxdna_client *client, u32 cmd_bo_hdls, u32 *arg_bo_hdls, u32 arg_bo_cnt, diff --git a/drivers/accel/amdxdna/amdxdna_drm.c b/drivers/accel/amdxdna/am= dxdna_drm.c index 3b15eae65e6d..a4c189b4953e 100644 --- a/drivers/accel/amdxdna/amdxdna_drm.c +++ b/drivers/accel/amdxdna/amdxdna_drm.c @@ -15,9 +15,17 @@ static int amdxdna_drm_open(struct drm_device *ddev, str= uct drm_file *filp) struct amdxdna_client *client; int ret; =20 + ret =3D pm_runtime_resume_and_get(ddev->dev); + if (ret) { + XDNA_ERR(xdna, "Failed to get rpm, ret %d", ret); + return ret; + } + client =3D kzalloc(sizeof(*client), GFP_KERNEL); - if (!client) - return -ENOMEM; + if (!client) { + ret =3D -ENOMEM; + goto put_rpm; + } =20 client->pid =3D pid_nr(filp->pid); client->xdna =3D xdna; @@ -53,6 +61,9 @@ static int amdxdna_drm_open(struct drm_device *ddev, stru= ct drm_file *filp) iommu_sva_unbind_device(client->sva); failed: kfree(client); +put_rpm: + pm_runtime_mark_last_busy(ddev->dev); + pm_runtime_put_autosuspend(ddev->dev); =20 return ret; } @@ -75,6 +86,8 @@ static void amdxdna_drm_close(struct drm_device *ddev, st= ruct drm_file *filp) =20 XDNA_DBG(xdna, "pid %d closed", client->pid); kfree(client); + pm_runtime_mark_last_busy(ddev->dev); + pm_runtime_put_autosuspend(ddev->dev); } =20 static int amdxdna_flush(struct file *f, fl_owner_t id) diff --git a/drivers/accel/amdxdna/amdxdna_drm.h b/drivers/accel/amdxdna/am= dxdna_drm.h index 6f1eaed46382..79eb9b4f6574 100644 --- a/drivers/accel/amdxdna/amdxdna_drm.h +++ b/drivers/accel/amdxdna/amdxdna_drm.h @@ -14,6 +14,7 @@ #include #include #include +#include #include =20 #include "amdxdna_ctx.h" @@ -38,10 +39,14 @@ struct amdxdna_dev; struct amdxdna_dev_ops { int (*init)(struct amdxdna_dev *xdna); void (*fini)(struct amdxdna_dev *xdna); + int (*resume)(struct amdxdna_dev *xdna); + void (*suspend)(struct amdxdna_dev *xdna); int (*hwctx_init)(struct amdxdna_hwctx *hwctx); void (*hwctx_fini)(struct amdxdna_hwctx *hwctx); int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, voi= d *buf, u32 size); void (*hmm_invalidate)(struct amdxdna_gem_obj *abo, unsigned long cur_seq= ); + void (*hwctx_suspend)(struct amdxdna_hwctx *hwctx); + void (*hwctx_resume)(struct amdxdna_hwctx *hwctx); int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *= job, u64 *seq); int (*cmd_wait)(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout); }; diff --git a/drivers/accel/amdxdna/amdxdna_pci_drv.c b/drivers/accel/amdxdn= a/amdxdna_pci_drv.c index 924ee8b96ebb..b472c4d7342a 100644 --- a/drivers/accel/amdxdna/amdxdna_pci_drv.c +++ b/drivers/accel/amdxdna/amdxdna_pci_drv.c @@ -7,6 +7,8 @@ =20 #include "amdxdna_pci_drv.h" =20 +#define AMDXDNA_AUTOSUSPEND_DELAY 5000 /* miliseconds */ + /* * There are platforms which share the same PCI device ID * but have different PCI revision IDs. So, let the PCI class @@ -46,10 +48,11 @@ amdxdna_get_dev_info(struct pci_dev *pdev) =20 static int amdxdna_probe(struct pci_dev *pdev, const struct pci_device_id = *id) { + struct device *dev =3D &pdev->dev; struct amdxdna_dev *xdna; int ret; =20 - xdna =3D devm_drm_dev_alloc(&pdev->dev, &amdxdna_drm_drv, typeof(*xdna), = ddev); + xdna =3D devm_drm_dev_alloc(dev, &amdxdna_drm_drv, typeof(*xdna), ddev); if (IS_ERR(xdna)) return PTR_ERR(xdna); =20 @@ -75,12 +78,19 @@ static int amdxdna_probe(struct pci_dev *pdev, const st= ruct pci_device_id *id) goto failed_dev_fini; } =20 + pm_runtime_set_autosuspend_delay(dev, AMDXDNA_AUTOSUSPEND_DELAY); + pm_runtime_use_autosuspend(dev); + pm_runtime_allow(dev); + ret =3D drm_dev_register(&xdna->ddev, 0); if (ret) { XDNA_ERR(xdna, "DRM register failed, ret %d", ret); + pm_runtime_forbid(dev); goto failed_sysfs_fini; } =20 + pm_runtime_mark_last_busy(dev); + pm_runtime_put_autosuspend(dev); return 0; =20 failed_sysfs_fini: @@ -95,8 +105,12 @@ static int amdxdna_probe(struct pci_dev *pdev, const st= ruct pci_device_id *id) static void amdxdna_remove(struct pci_dev *pdev) { struct amdxdna_dev *xdna =3D pci_get_drvdata(pdev); + struct device *dev =3D &pdev->dev; struct amdxdna_client *client; =20 + pm_runtime_get_noresume(dev); + pm_runtime_forbid(dev); + drm_dev_unplug(&xdna->ddev); amdxdna_sysfs_fini(xdna); =20 @@ -118,11 +132,98 @@ static void amdxdna_remove(struct pci_dev *pdev) mutex_unlock(&xdna->dev_lock); } =20 +static int amdxdna_dev_suspend_nolock(struct amdxdna_dev *xdna) +{ + if (xdna->dev_info->ops->suspend) + xdna->dev_info->ops->suspend(xdna); + + return 0; +} + +static int amdxdna_dev_resume_nolock(struct amdxdna_dev *xdna) +{ + if (xdna->dev_info->ops->resume) + return xdna->dev_info->ops->resume(xdna); + + return 0; +} + +static int amdxdna_pmops_suspend(struct device *dev) +{ + struct amdxdna_dev *xdna =3D pci_get_drvdata(to_pci_dev(dev)); + struct amdxdna_client *client; + + mutex_lock(&xdna->dev_lock); + list_for_each_entry(client, &xdna->client_list, node) + amdxdna_hwctx_suspend(client); + + amdxdna_dev_suspend_nolock(xdna); + mutex_unlock(&xdna->dev_lock); + + return 0; +} + +static int amdxdna_pmops_resume(struct device *dev) +{ + struct amdxdna_dev *xdna =3D pci_get_drvdata(to_pci_dev(dev)); + struct amdxdna_client *client; + int ret; + + XDNA_INFO(xdna, "firmware resuming..."); + mutex_lock(&xdna->dev_lock); + ret =3D amdxdna_dev_resume_nolock(xdna); + if (ret) { + XDNA_ERR(xdna, "resume NPU firmware failed"); + mutex_unlock(&xdna->dev_lock); + return ret; + } + + XDNA_INFO(xdna, "hardware context resuming..."); + list_for_each_entry(client, &xdna->client_list, node) + amdxdna_hwctx_resume(client); + mutex_unlock(&xdna->dev_lock); + + return 0; +} + +static int amdxdna_rpmops_suspend(struct device *dev) +{ + struct amdxdna_dev *xdna =3D pci_get_drvdata(to_pci_dev(dev)); + int ret; + + mutex_lock(&xdna->dev_lock); + WARN_ON(!list_empty(&xdna->client_list)); + ret =3D amdxdna_dev_suspend_nolock(xdna); + mutex_unlock(&xdna->dev_lock); + + XDNA_DBG(xdna, "Runtime suspend done ret: %d", ret); + return ret; +} + +static int amdxdna_rpmops_resume(struct device *dev) +{ + struct amdxdna_dev *xdna =3D pci_get_drvdata(to_pci_dev(dev)); + int ret; + + mutex_lock(&xdna->dev_lock); + ret =3D amdxdna_dev_resume_nolock(xdna); + mutex_unlock(&xdna->dev_lock); + + XDNA_DBG(xdna, "Runtime resume done ret: %d", ret); + return ret; +} + +static const struct dev_pm_ops amdxdna_pm_ops =3D { + SET_SYSTEM_SLEEP_PM_OPS(amdxdna_pmops_suspend, amdxdna_pmops_resume) + SET_RUNTIME_PM_OPS(amdxdna_rpmops_suspend, amdxdna_rpmops_resume, NULL) +}; + static struct pci_driver amdxdna_pci_driver =3D { .name =3D KBUILD_MODNAME, .id_table =3D pci_ids, .probe =3D amdxdna_probe, .remove =3D amdxdna_remove, + .driver.pm =3D &amdxdna_pm_ops, }; =20 module_pci_driver(amdxdna_pci_driver); --=20 2.34.1