From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 E202D8635C; Mon, 14 Jul 2025 12:09:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752494953; cv=none; b=fWfhl7D81BtQkTLMqatVCxhbsbuNaaRbxpaNaCkG5E8UEt4YWaxkhGdNwjlDUKo1ISKhky9uLUX9klCsr6QkmFh/3RMNhMNz4R9ygmatrfPIecl57B60io7bP0wHzivIw+zCbQmSJEyvjjt2jQdnccjg8H0HjXLQXADyPXkNx60= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752494953; c=relaxed/simple; bh=790j4Ip2q5UeEyImLRL0KLjEyLZD54ZPHny1IERhf4w=; h=Mime-Version:Content-Type:Date:Message-Id:Subject:Cc:To:From: References:In-Reply-To; b=t+WnzDYx3soX4icI/FmKujBwx5zTP85f7Y0rLj9sgldMI2mXyo73CvgERE4Ph964M9f4Bhohg6c9htNsi7488CSDJN65IgETLlkML8HpJAqRk3oJUzIeyCQmRckwv59mtwXI5hNf3/hDmgUYrnC7kPts+u7EQXDJZ9w0m0ouQc0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FgYIuhCo; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FgYIuhCo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ADB59C4CEF4; Mon, 14 Jul 2025 12:09:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752494952; bh=790j4Ip2q5UeEyImLRL0KLjEyLZD54ZPHny1IERhf4w=; h=Date:Subject:Cc:To:From:References:In-Reply-To:From; b=FgYIuhCot+kXZPqKZ3GZV+bptXq5GXUvlf9Z+F4wjculcls+xCDIvWMDz/iVblwiB 2q/i6YLV2qHQhBFGWipPSbBWuJO0GXetI1Qp8wdI597XT+lGrpEHsQ39UoX+OJjehq RBSSMjXsxAvv+FQlRapvRCNaqwW14UOZyFskekdBplPv3FQsbmZzGxWWld+81/LQIF 82Qic6TA8SyTVmI3MUqDYRW0j+yu5I/O0zSfxwjYIoWil+0w1gXTkUMOmKi2xMz5CR bpkcZHJeQkKRF3PG0YIjk+HyxvBAWW9nlJFHxpFrPxSxbk3d4io4xfUqurydNXhVBM +7T1CyvHJPDsA== Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 14 Jul 2025 14:09:06 +0200 Message-Id: Subject: Re: [PATCH v12 2/3] rust: io: mem: add a generic iomem abstraction Cc: "Daniel Almeida" , "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Andreas Hindborg" , "Trevor Gross" , "Greg Kroah-Hartman" , "Rafael J. Wysocki" , "Andrew Morton" , "Andy Shevchenko" , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , "Bjorn Helgaas" , "Mika Westerberg" , "Ying Huang" , "Benno Lossin" , , To: "Alice Ryhl" From: "Danilo Krummrich" References: <20250704-topics-tyr-platform_iomem-v12-0-1d3d4bd8207d@collabora.com> <20250704-topics-tyr-platform_iomem-v12-2-1d3d4bd8207d@collabora.com> <7A0457FA-0E09-451E-B035-E739AED7B2C7@collabora.com> In-Reply-To: On Mon Jul 14, 2025 at 1:51 PM CEST, Alice Ryhl wrote: > On Thu, Jul 10, 2025 at 10:58:13AM -0300, Daniel Almeida wrote: >> Hi Alice, >>=20 >> >> +impl IoMem { >> >> + fn ioremap(resource: &Resource) -> Result { >> >> + let size =3D resource.size(); >> >> + if size =3D=3D 0 { >> >> + return Err(EINVAL); >> >> + } >> >> + >> >> + let res_start =3D resource.start(); >> >> + >> >> + let addr =3D if resource >> >> + .flags() >> >> + .contains(io::resource::flags::IORESOURCE_MEM_NONPOSTED) >> >> + { >> >> + // SAFETY: >> >> + // - `res_start` and `size` are read from a presumably v= alid `struct resource`. >> >> + // - `size` is known not to be zero at this point. >> >> + unsafe { bindings::ioremap_np(res_start, size as usize) = } >> >=20 >> > Here you cast from ResourceSize to usize. Are you sure that is correct= ? >> > I thought those types could be different. >>=20 >> This seems to what C is doing as well, i.e.: >>=20 >> static void __iomem *__devm_ioremap(struct device *dev, resource_size_t = offset, >> resource_size_t size, <--------- >> enum devm_ioremap_type type) >> { >>=20 >> [=E2=80=A6] >>=20 >> case DEVM_IOREMAP_NP: >> addr =3D ioremap_np(offset, size); >> break; >> } >>=20 >>=20 >> Where: >>=20 >> `static inline void __iomem *ioremap_np(phys_addr_t offset, size_t size)= ` >>=20 >> IOW: this stems from the mix and match of types used the C API itself. >>=20 >> What do you suggest here? Maybe a try_into() then? > > What a mess. Yeah, it mixes up types describing CPU word width and bus address width. :( > It looks like there aren't any 32-bit architectures that > define ioremap_np. This means that sometimes this cast will be lossy, > but in those cases the function body just returns NULL and doesn't read > the size. > > I would probably cast to an underscore instead of explicitly mentioning > the target type and make a comment about it. I think fixing up the C side would be even nicer, but for the scope of this series that's fine. The comment should mention that, ultimately, we want to= fix up the C side type wise.