public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Ke Sun <sk.alvin.x@gmail.com>
To: Danilo Krummrich <dakr@kernel.org>, Ke Sun <sunke@kylinos.cn>
Cc: "Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <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>,
	linux-rtc@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [RFC PATCH v1 4/4] rust: add PL031 RTC driver
Date: Tue, 6 Jan 2026 10:51:50 +0800	[thread overview]
Message-ID: <88b1a3dd-e646-4583-bc41-07ff7e9422a7@gmail.com> (raw)
In-Reply-To: <DFFTVRMAFF3S.13N6WCNAVVR6I@kernel.org>


On 1/4/26 21:10, Danilo Krummrich wrote:
> On Sun Jan 4, 2026 at 7:06 AM CET, Ke Sun wrote:
>> +/// PL031 RTC driver private data.
>> +#[pin_data(PinnedDrop)]
>> +struct Pl031DrvData {
>> +    #[pin]
>> +    base: Devres<IoMem<0>>,
> Please do not use 0 as generic argument, this should likely be RTC_YLR + 0x4
> (assuming that this register has a width of 32 bit).
>
> It allows you to perform register accesses until RTC_YLR + 0x4 with infallible
> accessors, since the call to IoMem::new() will validate that the memory region
> has at least a size of RTC_YLR + 0x4.
>
>> +    variant: VendorVariant,
>> +    /// RTC device reference for interrupt handler.
>> +    ///
>> +    /// Set in `init_rtcdevice` and remains valid for the driver's lifetime
>> +    /// because the RTC device is managed by devres.
>> +    rtc_device: Option<ARef<RtcDevice>>,
> I don't see a reason for a separate init_rtcdevice() method. Creating the RTC
> device should happen in probe(), which also gets you rid of this odd Option.
>
Hi Danilo,

I encountered an issue while refactoring the RTC abstraction.

Following the platform driver implementation, the AMBA driver stores its 
drvdata in amba_device->dev. However,
the RTC driver also stores its drvdata in the parent device (which is 
also amba_device->dev), causing a conflict.
This was already encountered in v1, which is why the design was awkward.

Do you have any suggestions on how to address this?

Here is part of the code:

  // rust/kernel/amba.rs
impl<T: Driver + 'static> Adapter<T> {
     extern "C" fn probe_callback(
         adev: *mut bindings::amba_device,
         id: *const bindings::amba_id,
     ) -> kernel::ffi::c_int {
         let adev_internal = unsafe { 
&*adev.cast::<Device<device::CoreInternal>>() };
         let info = Self::amba_id_info(adev_internal, id);

         from_result(|| {
             let data = T::probe(adev_internal, info);

             // Referring to the implementation in platform.rs, here 
`dev_set_drvdata(&adev->dev, data)`
             // will clobber the value that has already been set in 
`amba::Driver::probe`.
             adev_internal.as_ref().set_drvdata(data)?;
             Ok(0)
         })
   }
}

// rust/kernel/rtc.rs
impl<T: RtcOps> Adapter<T> {
     unsafe extern "C" fn read_time(
         dev: *mut bindings::device,      // **pointer to the `struct 
device` embedded in  a `struct amba_device`
         tm: *mut bindings::rtc_time,
     ) -> c_int {
         let bound_dev = unsafe { 
device::Device::<device::Bound>::from_raw(dev) };
         let rtc_tm = unsafe { &mut *tm.cast::<RtcTime>() };

         match T::read_time(bound_dev, rtc_tm) {
             Ok(()) => 0,
             Err(err) => err.to_errno(),
         }
   }
}

// drivers/rtc/rtc_pl031_rust.rs
impl amba::Driver for Pl031AmbaDriver {
     ...
     fn probe(
         adev: &amba::Device<Core>,
         id_info: Option<&Self::IdInfo>,
     ) -> impl PinInit<Self, Error> {
         let dev = adev.as_ref();
         let io_request = adev.io_request().ok_or(ENODEV)?;
         ...
         // Allocate RTC device.
         let rtcdev = RtcDevice::new::<Pl031DrvData>(dev)?;
         let rtcdev_clone = rtcdev.clone();

         // Set driver data with RTC device reference.
         rtcdev.set_drvdata(try_pin_init!(Pl031DrvData {
             base <- IoMem::new(io_request),
             variant,
             rtcdev: rtcdev_clone,
         }))?;
         ...
         // Register RTC device.
         // **If CONFIG_RTC_HCTOSYS is enabled and the device is rtc0, 
rtc_read_time will be
         // called during the registration process. This requires 
drvdata to be set up before registration.
         Registration::register(dev, rtcdev)?;

         Ok(Pl031AmbaDriver)
     }
}

Best regards,
Ke Sun

>> +}
>> +
>> +// SAFETY: `Pl031DrvData` contains only `Send`/`Sync` types: `Devres` (Send+Sync),
>> +// `VendorVariant` (Copy), and `Option<ARef<RtcDevice>>` (Send+Sync because `RtcDevice` is
>> +// Send+Sync).
>> +unsafe impl Send for Pl031DrvData {}
>> +// SAFETY: `Pl031DrvData` contains only `Send`/`Sync` types: `Devres` (Send+Sync),
>> +// `VendorVariant` (Copy), and `Option<ARef<RtcDevice>>` (Send+Sync because `RtcDevice` is
>> +// Send+Sync).
>> +unsafe impl Sync for Pl031DrvData {}
> Why not implement Send + Sync for RtcDevice then?
>
>> +// Use AMBA device table for matching
>> +kernel::amba_device_table!(
>> +    ID_TABLE,
>> +    MODULE_ID_TABLE,
>> +    <Pl031DrvData as rtc::DriverGeneric<rtc::AmbaBus>>::IdInfo,
>> +    [
>> +        (
>> +            amba::DeviceId::new_with_data(0x00041031, 0x000fffff, Pl031Variant::ARM.to_usize()),
>> +            Pl031Variant::ARM
>> +        ),
>> +        (
>> +            amba::DeviceId::new_with_data(0x00180031, 0x00ffffff, Pl031Variant::STV1.to_usize()),
>> +            Pl031Variant::STV1
>> +        ),
>> +        (
>> +            amba::DeviceId::new_with_data(0x00280031, 0x00ffffff, Pl031Variant::STV2.to_usize()),
> Why a constructor new_with_data() if you already store data through the generic
> device ID mechanism right below?
>
>> +            Pl031Variant::STV2
>> +        ),
>> +    ]
>> +);

  reply	other threads:[~2026-01-06  2:51 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-04  6:06 [RFC PATCH v1 0/4] rust: Add RTC driver support Ke Sun
2026-01-04  6:06 ` [RFC PATCH v1 1/4] rust: add AMBA bus abstractions Ke Sun
2026-01-04 11:37   ` Miguel Ojeda
2026-01-04 12:37   ` Danilo Krummrich
2026-01-04  6:06 ` [RFC PATCH v1 2/4] rust: add device wakeup support Ke Sun
2026-01-04 13:31   ` Danilo Krummrich
2026-01-04  6:06 ` [RFC PATCH v1 3/4] rust: add RTC core abstractions and data structures Ke Sun
2026-01-04 13:00   ` Danilo Krummrich
2026-01-04  6:06 ` [RFC PATCH v1 4/4] rust: add PL031 RTC driver Ke Sun
2026-01-04  9:02   ` Dirk Behme
2026-01-07 10:15     ` Danilo Krummrich
2026-01-04 11:40   ` Miguel Ojeda
2026-01-04 13:10   ` Danilo Krummrich
2026-01-06  2:51     ` Ke Sun [this message]
2026-01-06 13:32       ` Danilo Krummrich
2026-01-06 14:44         ` Greg Kroah-Hartman
2026-01-06 15:04           ` Danilo Krummrich
2026-01-06 15:12             ` Greg Kroah-Hartman
2026-01-04 13:36 ` [RFC PATCH v1 0/4] rust: Add RTC driver support Danilo Krummrich
2026-01-04 14:11   ` Ke Sun
2026-01-06  7:41 ` Kari Argillander

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=88b1a3dd-e646-4583-bc41-07ff7e9422a7@gmail.com \
    --to=sk.alvin.x@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alexandre.belloni@bootlin.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-rtc@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sunke@kylinos.cn \
    --cc=tmgross@umich.edu \
    /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