From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.201.71 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1793598414; b=c4VmuZUowiO9P0lrnoGhUnvLk9ZrOXLPr3usc7YYpmyRAyGPMsEF99IekUWXuDaGQNfR pkbdxOO8Pebj1sVeAukTbv/fTuiHJPRLb4mWg6eV1YitUcWr4+J9SGXX1GD/TWNOmatvL ZI/1CkWnPE+Ec0PUzQpSJ9zcieBD/6HZwFVAkLNRVcys+R5wOKt4R0Nw+h6Z4U47G/nGF Le3jjpSKTr/APcTiIgqeMDPdJT2+yMhgApYK1hBZbgZfI/V9MYpQdaJcBH14w2DLunhIu o/k1o4JCjguR68uxdOvjBYf3CD6wJ4P3rdlcfGl6HXvMEDYx1bdgFrmbt4ozLD042cg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793598414; h=DMARC-Filter:DKIM-Signature:Received:Received: Content-Transfer-Encoding:Content-Type:Date:Message-Id:Cc:Subject: From:To:X-Mailer:References:In-Reply-To:X-ClientProxiedBy: MIME-Version:X-MS-PublicTrafficType:X-MS-TrafficTypeDiagnostic: X-MS-Office365-Filtering-Correlation-Id:X-MS-Exchange-SenderADCheck: X-MS-Exchange-AntiSpam-Relay:X-Microsoft-Antispam: X-Microsoft-Antispam-Message-Info:X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0: X-MS-Exchange-AntiSpam-MessageData-1:X-OriginatorOrg: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=o8ed34RJVl0kcaElnRK+vljUwKi4d42jeQ9ACN7mslQ=; b=c2ogUo8j933G2OuxalWZhzd/lXoNN5WCK2wHIM7jvzuzFIfoFXDxjVBN4psPqvSwTwHJ xmKsFdWRVQfgpkDI74LhMujU0riOfVciqKl7Rlq4SGDtvQBIoh3lU5ByeJuPAc0PF1Zjp AiH00Ws4gSCqAFJ8W8VpTMatEfujtIyVSokTfNO1nLTdxJ0Q2TQ26niuyDiK6tnKolzWj 1QaoZiawFnXV6VvVLa2uU6zEatSnbT+83uVq/PueoTY/BSF/h9POy4lIOZcc16sSzIYGm hU7Ik88RzUbsh5mnPnbNFyDlpVeaJaDVMU1Ki61ARnqvRqVCjt7i9wfEvvexj1WQ1gg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=dw+eoPFE; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.201.71 Received: from CH4PR04CU002.outbound.protection.outlook.com (mail-northcentralusazon11013071.outbound.protection.outlook.com [40.107.201.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67134606A32E; Mon, 2 Nov 2026 05:46:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 67134606A32E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=dw+eoPFE ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=dN3nEjKeMTQPhgb32UxaFR3u6HmSkx8M4sTIt8C4TypdqVDD8HtJLAJZlG3Jx3XRgJnabd897GzF29MvoQ0ycyXtIBohWCcjxL+o/zawyfZK77kuugnp0Qg4rJEB4ltn40nQciVolzgwB0AgUhzJsdZp6UKQCrqFbS3BlhZd4YgE3/40VfI5/yNutxohGx8w2k8uDijfN9lRzz7ZmB7Yy6SU56Cev2TFon3dXfsg3NNOd081lAS9IZ0fGNagQ7BvbhmMG68zJtul/czI1hrRfFKJcbw9OGaSVJHxvtMj1FCDjbKJa2eFqQ5yL8MGyBW7/dZULPD2TwIgBgSk7uamNQ== 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=o8ed34RJVl0kcaElnRK+vljUwKi4d42jeQ9ACN7mslQ=; b=Ux3jEJTdLA1LF1IvDNjaCMpFb++pZzIaEib/Iiitfd2zl54S+G+WWuHtVnI0R0hu9edECr/u4JRCq0qz+920NUmIdRF/TFytohG8CkWtVcNUtCYypYQGOs8zAWIlKJ/VMZumZWxu2/8Yz3+sJdPUdcqYDwAnwEUYoD8lZFIkJyuVrss8jU5mJkrTRm0PU6Hr0URjrBGjwTqZ4+xdcEg1Euqqbd2IJL7rBn7ogfgwpKLNcX0xIWxXDYeZgxIHrBCBokF3OLRgT8oclKPMSgPexfZHEpM0h0ckS8nGUlA7ftZuEcR70nYLMjR3N4fb+pQz7ujVNkspm+FfZh+v9mLcig== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=o8ed34RJVl0kcaElnRK+vljUwKi4d42jeQ9ACN7mslQ=; b=dw+eoPFE2EnvhrT6CSLkwIvLK4yoSo0fXipVaxBdoaIYxPMB/S9QuTDooqrh6eBVzWNyJVLq78Oc/sy42qbVibJL0totGYw59r/LcNE93PZ10NEOjtHbTIxJBvCXiu4Rj1z6kqyT+6+0ioBaKvsc59f8ME3AwOk8sS8puR8K23+r5kVZcWN5epi1MlcnTpvOfF9TOHnA6pdIidF8h/aK0VTjTC9yPIxAf3qjsMz1Q9L++ttcTDsPdXSJQlu772dgzFpg3s9cYMbJdTvUF0sTfPDkxorhj0mvI9AkrUbwi8dwUzNAPPRQrEUI0i6h1VYIHzZwIZdHR3G9F8xNqDRbaQ== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS0PR12MB6413.namprd12.prod.outlook.com (2603:10b6:8:ce::10) by CY5PR12MB6646.namprd12.prod.outlook.com (2603:10b6:930:41::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.569.12; Mon, 2 Nov 2026 05:46:49 +0000 Received: from DS0PR12MB6413.namprd12.prod.outlook.com ([fe80::e82a:6673:4142:37fa]) by DS0PR12MB6413.namprd12.prod.outlook.com ([fe80::e82a:6673:4142:37fa%5]) with mapi id 15.21.0569.011; Mon, 2 Nov 2026 05:46:49 +0000 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 02 Nov 2026 14:46:46 +0900 Message-Id: Cc: "John Hubbard" , "Alistair Popple" , "Timur Tabi" , "Eliot Courtney" , "Zhi Wang" , , , Subject: Re: [PATCH 4/4] gpu: nova-core: gsp: cmdq: compute the queue regions from a count and a slot From: "Eliot Courtney" To: "Alexandre Courbot" , "Danilo Krummrich" , "Alice Ryhl" , "David Airlie" , "Simona Vetter" , "Benno Lossin" , "Gary Guo" X-Mailer: aerc 0.22.0-0-gc2f86b7abde3 References: <20261030-r000-cmdq-prep-v1-0-f2befbcd8ba9@nvidia.com> <20261030-r000-cmdq-prep-v1-4-f2befbcd8ba9@nvidia.com> In-Reply-To: <20261030-r000-cmdq-prep-v1-4-f2befbcd8ba9@nvidia.com> X-ClientProxiedBy: TYCP286CA0099.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:2b4::17) To DS0PR12MB6413.namprd12.prod.outlook.com (2603:10b6:8:ce::10) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR12MB6413:EE_|CY5PR12MB6646:EE_ X-MS-Office365-Filtering-Correlation-Id: d55c5d6e-c3c8-4b00-2cf6-08df38a4b049 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|23010399003|366016|376014|10070799003|5023799004|11063799006|4143699003|56012099006|261009223027099003|261028144727099003|261021121427099003|261021135927099003|261021123027099003|261026121527099003|261026140227099003|261022075727099003|261022105027099003|10067099003|18002099003|22082099003|6133799003; X-Microsoft-Antispam-Message-Info: adfPIY3E4d9Gsh1JiWbkTg8IwjljrJ0tW7VUKDO3sarP+/pnHZ3i8k9oT0y7xRMxdofr9cUq96WRNQ3Su+ayGszPl9E23AZAxVq6AXyX63pLLrjO1sZuBcf5f23LP7xwhD8S3fB5JK4vwHcw3hLV1ignGa9HWWDeXtgunl97sbKKXwq8nE5TJuat8hDBwrJMwoprIJb+DRFQ28WMGMd3SbSEox6RpbKdIle/d/b1/4vyiCfehjfI/VGRAxNtkE2QekF7E2f4rQc8qiYkoCJ8CdztKQdp4xYeJQY+QRKXVZR4Pmn7LqLGJkacQcZd7MEtm6ibixRvLgLjvr5bRYf9NXrg4B2trKoJU5kmPnFSiBmnN4t4u24joSOGX7qw8Uj4sBYk8FFfI/nNVgVLXX1CJrA/3xylC4tTo2r4zDEuS3kaZ0k39dK+pP90mk/ALrTerkvHnzRLeVqI4v948pBMf4HB4KIFoLpPO4zGXvsHytfv4LQiyIkXWlHyH9wa5+eVICEG6rxYHOyfdeQ0aRzeAHs2f6Wh0IxPE04Bes3++DxxjStGBa1Nr3RYgUsyi/BJM8ti2CO0wFxn/FoWxkJQM3A717VtT0ksaxNFS7u2mN9QB42eY6jcD79Av+3qf+jQ7PN0cGk8UK4DxMshhJnOE5sxDu2wOc250FtX0QPZ09g= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR12MB6413.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(23010399003)(366016)(376014)(10070799003)(5023799004)(11063799006)(4143699003)(56012099006)(261009223027099003)(261028144727099003)(261021121427099003)(261021135927099003)(261021123027099003)(261026121527099003)(261026140227099003)(261022075727099003)(261022105027099003)(10067099003)(18002099003)(22082099003)(6133799003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 2 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?SU5hNXdwNGRhcHRTTWgvK0NtYS9HSEV3R2dGNU10NEF3YVVEbUUxQkV2SWJa?= =?utf-8?B?aVVlemlZaTdsem10ZmhlOHFFc3ZlMEtpam43UVAydXFCb2hhdHZyQWwwZ0Qy?= =?utf-8?B?ZnpCckZEQ1lYTVN4N3NHY05kZEZWZlBhbUZuNXY0UVh4MFFLVzBjNjB4VU1U?= =?utf-8?B?MHR2bmE3Unk5bWZ4Ym1lS3Y0S29RM21DbEwvMS9SUjNWRUFwTTlUZyt4d2Y1?= =?utf-8?B?R2hPbm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n?= =?utf-8?B?djdBQWVjZmlMMDQxZXI0QUVoUHBzclRBQ2wvU0VwM1k1c1hlbFdNMHdoL0hi?= =?utf-8?B?cHJJbEZGckd4RDBDRFhKVGRlbXB5L2VGeWFnWkM5YUtYeHM4WWZpMWc1MVZD?= =?utf-8?B?ODB3WEtmOVA5VUg2SDlLWDliU2taRDhKQmRYdzNvU1pQQ0RhSmh0cWVsa2J0?= =?utf-8?B?Z05TQXpsRUpSdjJUb3pVV3M1cWRCa0VkTkJTMzN3OXRUNnpMaWxzbm9DZWcr?= =?utf-8?B?TmpKZTBhRmpaR2cxdUhqQTVwTzZDb21mT1VlTmQvcVRYTmVtanp1QXkvd3Qy?= =?utf-8?Q?lsHYe59yGT5LZFsRFAJNygCz/W1wvuEtanQ3ZXY7ZNIjr?= X-MS-Exchange-AntiSpam-MessageData-1: eVnD9zqZsruIAw== X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d55c5d6e-c3c8-4b00-2cf6-08df38a4b049 X-MS-Exchange-CrossTenant-AuthSource: DS0PR12MB6413.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Nov 2026 05:46:49.4877 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ZLyeoAZ79R4yJBDIEQjIbsAUU16dA+xXsSJ9P78m/2TsRnH3ySuU53J4CToxdkX/ONOk6I9/r+EQ7X0mtAcCqg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6646 On Fri Oct 30, 2026 at 4:41 PM JST, Alexandre Courbot wrote: > From: John Hubbard > > The r000 firmware uses msgq v2, the queue layout that keeps the queue > pointers in BAR0 registers as counts that do not wrap at the ring size. > The r570 firmware's layout keeps the pointers in shared memory as > indices into the ring. The two layouts differ in where a pointer is > read and in how the size of the region that the driver may write, and > of the region that it may read, follows from a queue's write pointer > and read pointer. Splitting a region across the end of the ring is the > same in both, and whether a region wraps follows from the order of the > two pointers. > > The functions for the writable region and for the readable region each > branched on the order of the write pointer and the read pointer. Each > branch chose where the two slices ended, and the function then built > the slices with open-coded pointer arithmetic. The SAFETY comments > argued the slice bounds branch by branch, so the switch to msgq v2 > would have had to rewrite the branches and the argument along with the > pointer rules. > > Compute the number of slots in a region and its start slot once. The > first slice ends at the end of the region or at the end of the ring, > whichever comes first, and the second slice carries the rest. > > The number of slots comes from one helper per queue, which are the only > two places the msgq v2 switch needs to change. The two slices are built > with separate from_raw_parts calls over the slots the driver currently > owns, rather than by splitting a slice over the whole ring: a reference > spanning slots the GSP may be accessing would break the contract of > Coherent. > > No functional changes. > > Assisted-by: LLM > Signed-off-by: John Hubbard > Co-developed-by: Alexandre Courbot > Signed-off-by: Alexandre Courbot > --- Do you reckon you could tighten up/rewrite this commit message a bit? For me it's a bit hard to follow.=20 > drivers/gpu/nova-core/gsp/cmdq.rs | 96 +++++++++++++++++++--------------= ------ > 1 file changed, 47 insertions(+), 49 deletions(-) > > diff --git a/drivers/gpu/nova-core/gsp/cmdq.rs b/drivers/gpu/nova-core/gs= p/cmdq.rs > index a271114fb54c..7350074a9f96 100644 > --- a/drivers/gpu/nova-core/gsp/cmdq.rs > +++ b/drivers/gpu/nova-core/gsp/cmdq.rs > @@ -186,60 +186,64 @@ fn new(dev: &'a device::Device, bar:= Bar0<'a>) -> Result { > /// As the message queue is a circular buffer, the region may be dis= contiguous in memory. In > /// that case the second slice will have a non-zero length. > fn driver_write_area(&mut self) -> (&mut [[u8; GSP_PAGE_SIZE]], &mut= [[u8; GSP_PAGE_SIZE]]) { > - let tx =3D self.cpu_write_ptr(); > - let rx =3D self.gsp_read_ptr(); > + let avail =3D self.cpuq_free_slots(); > + let w_slot =3D self.cpu_write_ptr(); > =20 > // Pointer to the first entry of the CPU message queue. > let data =3D ptr::project!(mut self.mem.as_mut_ptr(), .cpuq.msgq= .data[build: 0]); > =20 > - let (tail_end, wrap_end) =3D if rx =3D=3D 0 { > - // The write area is non-wrapping, and stops at the second-t= o-last entry of the command > - // queue (to leave the last one empty). > - (MSGQ_NUM_PAGES - 1, 0) > - } else if rx <=3D tx { > - // The write area wraps and continues until `rx - 1`. > - (MSGQ_NUM_PAGES, rx - 1) > - } else { > - // The write area doesn't wrap and stops at `rx - 1`. > - (rx - 1, 0) > - }; > + // `w_slot` is below `MSGQ_NUM_PAGES` per the invariant of `cpu_= write_ptr`, so the > + // subtraction cannot underflow. > + let in_after =3D avail.min(MSGQ_NUM_PAGES - w_slot); > + let in_before =3D avail - in_after; > =20 > // SAFETY: > - // - `data` was created from a valid pointer, and `rx` and `tx` = are in the > - // `0..MSGQ_NUM_PAGES` range per the invariants of `cpu_write_= ptr` and `gsp_read_ptr`, > - // thus the created slices are valid. > - // - The area starting at `tx` and ending at `rx - 2` modulo `MS= GQ_NUM_PAGES`, > - // inclusive, belongs to the driver for writing and is not acc= essed concurrently by > - // the GSP. > + // - `data` was created from a valid pointer to `MSGQ_NUM_PAGES`= entries. > + // - The `in_after` entries starting at `w_slot` belong to the `= avail` entries that the > + // driver is currently allowed to write, and so do the `in_bef= ore` first entries. > // - The caller holds a reference to `self` for as long as the r= eturned slices are live, > // meaning the CPU write pointer cannot be advanced and thus t= hat the returned area > // remains exclusive to the CPU for the duration of the slices= . > - // - The created slices point to non-overlapping sub-ranges of `= data` in all > - // branches (in the `rx <=3D tx` case, the second slice ends a= t `rx - 1` which is strictly > - // less than `tx` where the first slice starts; in the other c= ases the second slice is > - // empty), so creating two `&mut` references from them does no= t violate aliasing rules. > + // - The slices do not overlap: `avail` is at most `MSGQ_NUM_PAG= ES - 1`, so `in_before` > + // is strictly less than `w_slot` whenever it is non-zero. > unsafe { > ( > core::slice::from_raw_parts_mut( > - data.add(num::u32_as_usize(tx)), > - num::u32_as_usize(tail_end - tx), > + data.add(num::u32_as_usize(w_slot)), > + num::u32_as_usize(in_after), > ), > - core::slice::from_raw_parts_mut(data, num::u32_as_usize(= wrap_end)), > + core::slice::from_raw_parts_mut(data, num::u32_as_usize(= in_before)), Can we use io_project here? then unsafe as_mut() because they are disjoint e.g. ``` let after =3D io_project!(self.mem, .cpuq.msgq.data[panic: w_slot..w_slot += in_after]); let before =3D io_project!(self.mem, .cpuq.msgq.data[panic: ..in_before]); unsafe { (after.as_mut(), before.as_mut()) } ``` Same for the other spot. Gary also suggested this on v3 [1]. [1]: https://lore.kernel.org/all/DLMUKFL5CAZ5.2ICMKG94JXP52@garyguo.net/ > ) > } > } > =20 > - /// Returns the size of the region of the CPU message queue that the= driver is currently allowed > - /// to write to, in bytes. > - fn driver_write_area_size(&self) -> usize { > + /// Returns the number of slots of the CPU message queue that the dr= iver is currently allowed > + /// to write to. > + fn cpuq_free_slots(&self) -> u32 { > let tx =3D self.cpu_write_ptr(); > let rx =3D self.gsp_read_ptr(); > =20 > // `rx` and `tx` are both in `0..MSGQ_NUM_PAGES` per the invaria= nts of `gsp_read_ptr` and > // `cpu_write_ptr`. The minimum value case is where `rx =3D=3D 0= ` and `tx =3D=3D MSGQ_NUM_PAGES - > // 1`, which gives `0 + MSGQ_NUM_PAGES - (MSGQ_NUM_PAGES - 1) - = 1 =3D=3D 0`. > - let slots =3D (rx + MSGQ_NUM_PAGES - tx - 1) % MSGQ_NUM_PAGES; > - num::u32_as_usize(slots) * GSP_PAGE_SIZE > + (rx + MSGQ_NUM_PAGES - tx - 1) % MSGQ_NUM_PAGES > + } optional nit: it may be nicer (for r000 as well) to have the helper e.g. cpuq_free_slots return (u32, u32) with the starting slots and the # of slots. Reviewed-by: Eliot Courtney