mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dirk Behme <dirk.behme@de.bosch.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: Joel Fernandes <joelagnelf@nvidia.com>,
	<linux-kernel@vger.kernel.org>, <rust-for-linux@vger.kernel.org>,
	<dri-devel@lists.freedesktop.org>, <acourbot@nvidia.com>,
	Alistair Popple <apopple@nvidia.com>,
	Miguel Ojeda <ojeda@kernel.org>,
	Alex Gaynor <alex.gaynor@gmail.com>,
	Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>,
	<bjorn3_gh@protonmail.com>, Benno Lossin <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	Alice Ryhl <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
	Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	John Hubbard <jhubbard@nvidia.com>, Timur Tabi <ttabi@nvidia.com>,
	<joel@joelfernandes.org>, Elle Rhumsaa <elle@weathered-steel.dev>,
	Yury Norov <yury.norov@gmail.com>,
	Daniel Almeida <daniel.almeida@collabora.com>,
	"Andrea Righi" <arighi@nvidia.com>,
	<nouveau@lists.freedesktop.org>
Subject: Re: [PATCH v6 4/5] rust: Move register and bitfield macros out of Nova
Date: Fri, 10 Oct 2025 09:28:40 +0200	[thread overview]
Message-ID: <992d2e12-72ec-4692-aa64-73e8f3cce0c2@de.bosch.com> (raw)
In-Reply-To: <DDDQZ8LM2OGP.VSEG03ZE0K04@kernel.org>

On 09/10/2025 13:16, Danilo Krummrich wrote:
> On Thu Oct 9, 2025 at 8:59 AM CEST, Dirk Behme wrote:
>> Assuming that register.rs is supposed to become the "generic" way to
>> access hardware registers I started to have a look to it. Some weeks
>> back testing interrupts I used some quite simple timer with 4 registers
>> [1]. Now, thinking about converting it to register!() I have three
>> understanding / usage questions:
>>
>> * At the moment register!() is for 32-bit registers, only? So it can't
>> be used for my example having 8-bit and 16-bit registers as well?
> 
> Yes, currently the register!() macro always generates a 32-bit register type
> (mainly because nova-core did not need anything else). However, this will of
> course be generalized (which should be pretty straight forward).
> 
> Having a brief look at the TMU datasheet it looks like you should be able to
> treat TSTR and TCR as 32-bit registers without any issues for testing the
> register!() macro today. I.e. you can just define it as:
> 
> 	register!(TSTR @ 0x04, "Timer Start Register" {
> 	    2:2    str2 as bool, "Specifies whether TCNT2 is operated or stopped.";
> 	    1:1    str1 as bool, "Specifies whether TCNT1 is operated or stopped.";
> 	    0:0    str0 as bool, "Specifies whether TCNT0 is operated or stopped.";
> 	});
> 
> Same for TCR.
> 
>> * In my example I used io.try_write*() and io.try_read*() for the
>> register access. What is the relationship between these and the
>> register!() accessors (e.g. from the examples BOOT_0::read(&bar);)? Will
>> both stay? When to use which?
> 
> The try_*() variants should only be used of the offset of the register is not
> known at compile time.
> 
> If it is known at compile time (such as in your case) you should use the
> infallible variants that perform a range check at compile time.
> 
> For this to work you need to specify the minimum MMIO range your driver expects,
> i.e. instead of
> 
> 	let iomem = Arc::pin_init(request.iomap()?, GFP_KERNEL)?;
> 
> you call
> 
> 	let iomem = Arc::pin_init(request.iomap_sized::<TMU_MMIO_SIZE>()?, GFP_KERNEL)?;
> 
> where TMU_MMIO_SIZE is the minimum MMIO region size your driver is able to
> operate on. In your case this would be 0x12, given that TCR has a width of
> 16-bit. However, if you treat it as 32-bit register it would be 0x14.
> 
> Even without the register!() macro this would be a huge improvement. For
> instance, your IRQ handler from [1] can do
> 
> 	let tcr = io.read16_relaxed(TCR);
> 	if tcr & (0x1 << 8) != 0 {
> 	    io.write16_relaxed(tcr & !(0x1 << 8), TCR);
> 	}
> 
> instead of
> 
> 	let tcr = io.try_read16_relaxed(TCR).unwrap_or(0);
> 	if tcr & (0x1 << 8) != 0 {
> 	    io.try_write16_relaxed(tcr & !(0x1 << 8), TCR).unwrap_or(());
> 	}
> 
> And with the register macro it becomes
> 
> 	let tcr = TCR::read(io);
> 	if tcr.underflow() {
> 		tcr.set_underflow(false);
> 		tcr.write(io);
> 	}
> 
> Note that you can also extend the generated TCR type accordingly, for instance:
> 
> 	impl TCR {
> 	    fn handle_underflow<const SIZE: usize, T>(io: &T)
> 	    where
> 	        T: Deref<Target = Io<SIZE>>,
> 	    {
> 	        let this = Self::read(io);
> 	        if this.underflow() {
> 	            this.set_underflow(false);
> 	            this.write(io);
> 	        }
> 	    }
> 	}
> 
> and then from your IRQ handler you can just call
> 
> 	TCR::handle_underflow();


Many thanks! I think such verbose explanation helps a lot!


>> Note: Due to the file move obviously not the full content of the "new"
>> file register.rs is shown in this patch. Therefore, let me try it this
>> way, citing from register.rs:
>>
>> -- cut --
>> ...
>> /// This defines a `BOOT_0` type which can be read or written from
>> offset `0x100` of an `Io`
>> /// region
>> ....
>> /// ```ignore
>> /// // Read from the register's defined offset (0x100).
>> /// let boot0 = BOOT_0::read(&bar);
>> -- cut --
>>
>> * What is "&bar" in this example? Is it the `Io` region the explanation
>> talks about?
> 
> Yes, it's the I/O backend (a pci::Bar in this case). However, we should probably
> use a more generic name in the examples.

Intuitively for this example I was thinking about &io ;)

Thanks again!

Dirk

  parent reply	other threads:[~2025-10-10  7:28 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-03 15:47 [PATCH v6 0/5] Introduce bitfield and move register macro to rust/kernel/ Joel Fernandes
2025-10-03 15:47 ` [PATCH v6 1/5] nova-core: bitfield: Move bitfield-specific code from register! into new macro Joel Fernandes
2025-10-06 17:56   ` Edwin Peer
2025-10-07  6:49     ` Alexandre Courbot
2025-10-03 15:47 ` [PATCH v6 2/5] nova-core: bitfield: Add support for different storage widths Joel Fernandes
2025-10-03 15:47 ` [PATCH v6 3/5] nova-core: bitfield: Add support for custom visiblity Joel Fernandes
2025-10-03 15:47 ` [PATCH v6 4/5] rust: Move register and bitfield macros out of Nova Joel Fernandes
2025-10-06 10:38   ` Alexandre Courbot
2025-10-09  6:59   ` Dirk Behme
2025-10-09 11:16     ` Danilo Krummrich
2025-10-09 11:28       ` Alexandre Courbot
2025-10-09 12:54         ` Danilo Krummrich
2025-11-01 18:51         ` Dirk Behme
2025-11-02  3:00           ` Alexandre Courbot
2025-11-13  6:26             ` Dirk Behme
2025-10-10  7:28       ` Dirk Behme [this message]
2025-10-22 18:40   ` Beata Michalska
2025-10-22 19:37     ` Joel Fernandes
2025-10-23 13:55       ` Beata Michalska
2025-10-23 14:07         ` Danilo Krummrich
2025-10-23 21:47         ` Joel Fernandes
2025-10-23 21:50           ` Joel Fernandes
2025-10-27  9:06           ` Beata Michalska
2025-10-27  9:56             ` Danilo Krummrich
2025-10-27 15:05               ` Beata Michalska
2025-10-03 15:47 ` [PATCH v6 5/5] rust: bitfield: Add KUNIT tests for bitfield Joel Fernandes
2025-10-06 10:37   ` Alexandre Courbot
2025-10-06 19:38     ` Joel Fernandes
2025-10-06 20:36   ` [PATCH v7] " Joel Fernandes
2025-10-06 18:05 ` [PATCH v6 0/5] Introduce bitfield and move register macro to rust/kernel/ Edwin Peer
2025-10-06 22:29 ` Yury Norov
2025-10-07 10:36   ` Alexandre Courbot
2025-10-07 10:42     ` Miguel Ojeda
2025-10-07 13:20       ` Alexandre Courbot
2025-10-07 16:06         ` Yury Norov
2025-10-07 16:12         ` Miguel Ojeda
2025-10-07 13:16     ` Danilo Krummrich
2025-10-07 21:08       ` Joel Fernandes
2025-10-07 22:08         ` Danilo Krummrich
2025-10-08 14:28           ` Yury Norov
2025-10-08 15:00             ` Danilo Krummrich
2025-10-07 15:41     ` Yury Norov
2025-10-07 21:41       ` Daniel Almeida
2025-10-08 15:49         ` Yury Norov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=992d2e12-72ec-4692-aa64-73e8f3cce0c2@de.bosch.com \
    --to=dirk.behme@de.bosch.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=arighi@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=elle@weathered-steel.dev \
    --cc=gary@garyguo.net \
    --cc=jhubbard@nvidia.com \
    --cc=joel@joelfernandes.org \
    --cc=joelagnelf@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=nouveau@lists.freedesktop.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=tzimmermann@suse.de \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®