xref: /openbmc/qemu/rust/hw/char/pl011/src/device.rs (revision 2e06e72d)
1 // Copyright 2024, Linaro Limited
2 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 use core::{
6     ffi::{c_int, c_uchar, c_uint, c_void, CStr},
7     ptr::{addr_of, addr_of_mut, NonNull},
8 };
9 
10 use qemu_api::{
11     bindings::{self, *},
12     definitions::ObjectImpl,
13 };
14 
15 use crate::{
16     memory_ops::PL011_OPS,
17     registers::{self, Interrupt},
18     RegisterOffset,
19 };
20 
21 /// Integer Baud Rate Divider, `UARTIBRD`
22 const IBRD_MASK: u32 = 0x3f;
23 
24 /// Fractional Baud Rate Divider, `UARTFBRD`
25 const FBRD_MASK: u32 = 0xffff;
26 
27 const DATA_BREAK: u32 = 1 << 10;
28 
29 /// QEMU sourced constant.
30 pub const PL011_FIFO_DEPTH: usize = 16_usize;
31 
32 #[derive(Clone, Copy, Debug)]
33 enum DeviceId {
34     #[allow(dead_code)]
35     Arm = 0,
36     Luminary,
37 }
38 
39 impl std::ops::Index<hwaddr> for DeviceId {
40     type Output = c_uchar;
41 
42     fn index(&self, idx: hwaddr) -> &Self::Output {
43         match self {
44             Self::Arm => &Self::PL011_ID_ARM[idx as usize],
45             Self::Luminary => &Self::PL011_ID_LUMINARY[idx as usize],
46         }
47     }
48 }
49 
50 impl DeviceId {
51     const PL011_ID_ARM: [c_uchar; 8] = [0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1];
52     const PL011_ID_LUMINARY: [c_uchar; 8] = [0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1];
53 }
54 
55 #[repr(C)]
56 #[derive(Debug, qemu_api_macros::Object)]
57 /// PL011 Device Model in QEMU
58 pub struct PL011State {
59     pub parent_obj: SysBusDevice,
60     pub iomem: MemoryRegion,
61     #[doc(alias = "fr")]
62     pub flags: registers::Flags,
63     #[doc(alias = "lcr")]
64     pub line_control: registers::LineControl,
65     #[doc(alias = "rsr")]
66     pub receive_status_error_clear: registers::ReceiveStatusErrorClear,
67     #[doc(alias = "cr")]
68     pub control: registers::Control,
69     pub dmacr: u32,
70     pub int_enabled: u32,
71     pub int_level: u32,
72     pub read_fifo: [u32; PL011_FIFO_DEPTH],
73     pub ilpr: u32,
74     pub ibrd: u32,
75     pub fbrd: u32,
76     pub ifl: u32,
77     pub read_pos: usize,
78     pub read_count: usize,
79     pub read_trigger: usize,
80     #[doc(alias = "chr")]
81     pub char_backend: CharBackend,
82     /// QEMU interrupts
83     ///
84     /// ```text
85     ///  * sysbus MMIO region 0: device registers
86     ///  * sysbus IRQ 0: `UARTINTR` (combined interrupt line)
87     ///  * sysbus IRQ 1: `UARTRXINTR` (receive FIFO interrupt line)
88     ///  * sysbus IRQ 2: `UARTTXINTR` (transmit FIFO interrupt line)
89     ///  * sysbus IRQ 3: `UARTRTINTR` (receive timeout interrupt line)
90     ///  * sysbus IRQ 4: `UARTMSINTR` (momem status interrupt line)
91     ///  * sysbus IRQ 5: `UARTEINTR` (error interrupt line)
92     /// ```
93     #[doc(alias = "irq")]
94     pub interrupts: [qemu_irq; 6usize],
95     #[doc(alias = "clk")]
96     pub clock: NonNull<Clock>,
97     #[doc(alias = "migrate_clk")]
98     pub migrate_clock: bool,
99     /// The byte string that identifies the device.
100     device_id: DeviceId,
101 }
102 
103 impl ObjectImpl for PL011State {
104     type Class = PL011Class;
105     const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
106     const TYPE_NAME: &'static CStr = crate::TYPE_PL011;
107     const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_SYS_BUS_DEVICE);
108     const ABSTRACT: bool = false;
109     const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = Some(pl011_init);
110     const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
111     const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
112 }
113 
114 #[repr(C)]
115 pub struct PL011Class {
116     _inner: [u8; 0],
117 }
118 
119 impl qemu_api::definitions::Class for PL011Class {
120     const CLASS_INIT: Option<
121         unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void),
122     > = Some(crate::device_class::pl011_class_init);
123     const CLASS_BASE_INIT: Option<
124         unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void),
125     > = None;
126 }
127 
128 impl PL011State {
129     /// Initializes a pre-allocated, unitialized instance of `PL011State`.
130     ///
131     /// # Safety
132     ///
133     /// `self` must point to a correctly sized and aligned location for the
134     /// `PL011State` type. It must not be called more than once on the same
135     /// location/instance. All its fields are expected to hold unitialized
136     /// values with the sole exception of `parent_obj`.
137     unsafe fn init(&mut self) {
138         const CLK_NAME: &CStr = c"clk";
139 
140         let dev = addr_of_mut!(*self).cast::<DeviceState>();
141         // SAFETY:
142         //
143         // self and self.iomem are guaranteed to be valid at this point since callers
144         // must make sure the `self` reference is valid.
145         unsafe {
146             memory_region_init_io(
147                 addr_of_mut!(self.iomem),
148                 addr_of_mut!(*self).cast::<Object>(),
149                 &PL011_OPS,
150                 addr_of_mut!(*self).cast::<c_void>(),
151                 Self::TYPE_INFO.name,
152                 0x1000,
153             );
154             let sbd = addr_of_mut!(*self).cast::<SysBusDevice>();
155             sysbus_init_mmio(sbd, addr_of_mut!(self.iomem));
156             for irq in self.interrupts.iter_mut() {
157                 sysbus_init_irq(sbd, irq);
158             }
159         }
160         // SAFETY:
161         //
162         // self.clock is not initialized at this point; but since `NonNull<_>` is Copy,
163         // we can overwrite the undefined value without side effects. This is
164         // safe since all PL011State instances are created by QOM code which
165         // calls this function to initialize the fields; therefore no code is
166         // able to access an invalid self.clock value.
167         unsafe {
168             self.clock = NonNull::new(qdev_init_clock_in(
169                 dev,
170                 CLK_NAME.as_ptr(),
171                 None, /* pl011_clock_update */
172                 addr_of_mut!(*self).cast::<c_void>(),
173                 ClockEvent::ClockUpdate.0,
174             ))
175             .unwrap();
176         }
177     }
178 
179     pub fn read(
180         &mut self,
181         offset: hwaddr,
182         _size: core::ffi::c_uint,
183     ) -> std::ops::ControlFlow<u64, u64> {
184         use RegisterOffset::*;
185 
186         std::ops::ControlFlow::Break(match RegisterOffset::try_from(offset) {
187             Err(v) if (0x3f8..0x400).contains(&v) => {
188                 u64::from(self.device_id[(offset - 0xfe0) >> 2])
189             }
190             Err(_) => {
191                 // qemu_log_mask(LOG_GUEST_ERROR, "pl011_read: Bad offset 0x%x\n", (int)offset);
192                 0
193             }
194             Ok(DR) => {
195                 // s->flags &= ~PL011_FLAG_RXFF;
196                 self.flags.set_receive_fifo_full(false);
197                 let c = self.read_fifo[self.read_pos];
198                 if self.read_count > 0 {
199                     self.read_count -= 1;
200                     self.read_pos = (self.read_pos + 1) & (self.fifo_depth() - 1);
201                 }
202                 if self.read_count == 0 {
203                     // self.flags |= PL011_FLAG_RXFE;
204                     self.flags.set_receive_fifo_empty(true);
205                 }
206                 if self.read_count + 1 == self.read_trigger {
207                     //self.int_level &= ~ INT_RX;
208                     self.int_level &= !registers::INT_RX;
209                 }
210                 // Update error bits.
211                 self.receive_status_error_clear = c.to_be_bytes()[3].into();
212                 self.update();
213                 // Must call qemu_chr_fe_accept_input, so return Continue:
214                 return std::ops::ControlFlow::Continue(c.into());
215             }
216             Ok(RSR) => u8::from(self.receive_status_error_clear).into(),
217             Ok(FR) => u16::from(self.flags).into(),
218             Ok(FBRD) => self.fbrd.into(),
219             Ok(ILPR) => self.ilpr.into(),
220             Ok(IBRD) => self.ibrd.into(),
221             Ok(LCR_H) => u16::from(self.line_control).into(),
222             Ok(CR) => {
223                 // We exercise our self-control.
224                 u16::from(self.control).into()
225             }
226             Ok(FLS) => self.ifl.into(),
227             Ok(IMSC) => self.int_enabled.into(),
228             Ok(RIS) => self.int_level.into(),
229             Ok(MIS) => u64::from(self.int_level & self.int_enabled),
230             Ok(ICR) => {
231                 // "The UARTICR Register is the interrupt clear register and is write-only"
232                 // Source: ARM DDI 0183G 3.3.13 Interrupt Clear Register, UARTICR
233                 0
234             }
235             Ok(DMACR) => self.dmacr.into(),
236         })
237     }
238 
239     pub fn write(&mut self, offset: hwaddr, value: u64) {
240         // eprintln!("write offset {offset} value {value}");
241         use RegisterOffset::*;
242         let value: u32 = value as u32;
243         match RegisterOffset::try_from(offset) {
244             Err(_bad_offset) => {
245                 eprintln!("write bad offset {offset} value {value}");
246             }
247             Ok(DR) => {
248                 // ??? Check if transmitter is enabled.
249                 let ch: u8 = value as u8;
250                 // XXX this blocks entire thread. Rewrite to use
251                 // qemu_chr_fe_write and background I/O callbacks
252 
253                 // SAFETY: self.char_backend is a valid CharBackend instance after it's been
254                 // initialized in realize().
255                 unsafe {
256                     qemu_chr_fe_write_all(addr_of_mut!(self.char_backend), &ch, 1);
257                 }
258                 self.loopback_tx(value);
259                 self.int_level |= registers::INT_TX;
260                 self.update();
261             }
262             Ok(RSR) => {
263                 self.receive_status_error_clear = 0.into();
264             }
265             Ok(FR) => {
266                 // flag writes are ignored
267             }
268             Ok(ILPR) => {
269                 self.ilpr = value;
270             }
271             Ok(IBRD) => {
272                 self.ibrd = value;
273             }
274             Ok(FBRD) => {
275                 self.fbrd = value;
276             }
277             Ok(LCR_H) => {
278                 let value = value as u16;
279                 let new_val: registers::LineControl = value.into();
280                 // Reset the FIFO state on FIFO enable or disable
281                 if bool::from(self.line_control.fifos_enabled())
282                     ^ bool::from(new_val.fifos_enabled())
283                 {
284                     self.reset_fifo();
285                 }
286                 if self.line_control.send_break() ^ new_val.send_break() {
287                     let mut break_enable: c_int = new_val.send_break().into();
288                     // SAFETY: self.char_backend is a valid CharBackend instance after it's been
289                     // initialized in realize().
290                     unsafe {
291                         qemu_chr_fe_ioctl(
292                             addr_of_mut!(self.char_backend),
293                             CHR_IOCTL_SERIAL_SET_BREAK as i32,
294                             addr_of_mut!(break_enable).cast::<c_void>(),
295                         );
296                     }
297                     self.loopback_break(break_enable > 0);
298                 }
299                 self.line_control = new_val;
300                 self.set_read_trigger();
301             }
302             Ok(CR) => {
303                 // ??? Need to implement the enable bit.
304                 let value = value as u16;
305                 self.control = value.into();
306                 self.loopback_mdmctrl();
307             }
308             Ok(FLS) => {
309                 self.ifl = value;
310                 self.set_read_trigger();
311             }
312             Ok(IMSC) => {
313                 self.int_enabled = value;
314                 self.update();
315             }
316             Ok(RIS) => {}
317             Ok(MIS) => {}
318             Ok(ICR) => {
319                 self.int_level &= !value;
320                 self.update();
321             }
322             Ok(DMACR) => {
323                 self.dmacr = value;
324                 if value & 3 > 0 {
325                     // qemu_log_mask(LOG_UNIMP, "pl011: DMA not implemented\n");
326                     eprintln!("pl011: DMA not implemented");
327                 }
328             }
329         }
330     }
331 
332     #[inline]
333     fn loopback_tx(&mut self, value: u32) {
334         if !self.loopback_enabled() {
335             return;
336         }
337 
338         // Caveat:
339         //
340         // In real hardware, TX loopback happens at the serial-bit level
341         // and then reassembled by the RX logics back into bytes and placed
342         // into the RX fifo. That is, loopback happens after TX fifo.
343         //
344         // Because the real hardware TX fifo is time-drained at the frame
345         // rate governed by the configured serial format, some loopback
346         // bytes in TX fifo may still be able to get into the RX fifo
347         // that could be full at times while being drained at software
348         // pace.
349         //
350         // In such scenario, the RX draining pace is the major factor
351         // deciding which loopback bytes get into the RX fifo, unless
352         // hardware flow-control is enabled.
353         //
354         // For simplicity, the above described is not emulated.
355         self.put_fifo(value);
356     }
357 
358     fn loopback_mdmctrl(&mut self) {
359         if !self.loopback_enabled() {
360             return;
361         }
362 
363         /*
364          * Loopback software-driven modem control outputs to modem status inputs:
365          *   FR.RI  <= CR.Out2
366          *   FR.DCD <= CR.Out1
367          *   FR.CTS <= CR.RTS
368          *   FR.DSR <= CR.DTR
369          *
370          * The loopback happens immediately even if this call is triggered
371          * by setting only CR.LBE.
372          *
373          * CTS/RTS updates due to enabled hardware flow controls are not
374          * dealt with here.
375          */
376 
377         //fr = s->flags & ~(PL011_FLAG_RI | PL011_FLAG_DCD |
378         //                  PL011_FLAG_DSR | PL011_FLAG_CTS);
379         //fr |= (cr & CR_OUT2) ? PL011_FLAG_RI  : 0;
380         //fr |= (cr & CR_OUT1) ? PL011_FLAG_DCD : 0;
381         //fr |= (cr & CR_RTS)  ? PL011_FLAG_CTS : 0;
382         //fr |= (cr & CR_DTR)  ? PL011_FLAG_DSR : 0;
383         //
384         self.flags.set_ring_indicator(self.control.out_2());
385         self.flags.set_data_carrier_detect(self.control.out_1());
386         self.flags.set_clear_to_send(self.control.request_to_send());
387         self.flags
388             .set_data_set_ready(self.control.data_transmit_ready());
389 
390         // Change interrupts based on updated FR
391         let mut il = self.int_level;
392 
393         il &= !Interrupt::MS;
394         //il |= (fr & PL011_FLAG_DSR) ? INT_DSR : 0;
395         //il |= (fr & PL011_FLAG_DCD) ? INT_DCD : 0;
396         //il |= (fr & PL011_FLAG_CTS) ? INT_CTS : 0;
397         //il |= (fr & PL011_FLAG_RI)  ? INT_RI  : 0;
398 
399         if self.flags.data_set_ready() {
400             il |= Interrupt::DSR as u32;
401         }
402         if self.flags.data_carrier_detect() {
403             il |= Interrupt::DCD as u32;
404         }
405         if self.flags.clear_to_send() {
406             il |= Interrupt::CTS as u32;
407         }
408         if self.flags.ring_indicator() {
409             il |= Interrupt::RI as u32;
410         }
411         self.int_level = il;
412         self.update();
413     }
414 
415     fn loopback_break(&mut self, enable: bool) {
416         if enable {
417             self.loopback_tx(DATA_BREAK);
418         }
419     }
420 
421     fn set_read_trigger(&mut self) {
422         self.read_trigger = 1;
423     }
424 
425     pub fn realize(&mut self) {
426         // SAFETY: self.char_backend has the correct size and alignment for a
427         // CharBackend object, and its callbacks are of the correct types.
428         unsafe {
429             qemu_chr_fe_set_handlers(
430                 addr_of_mut!(self.char_backend),
431                 Some(pl011_can_receive),
432                 Some(pl011_receive),
433                 Some(pl011_event),
434                 None,
435                 addr_of_mut!(*self).cast::<c_void>(),
436                 core::ptr::null_mut(),
437                 true,
438             );
439         }
440     }
441 
442     pub fn reset(&mut self) {
443         self.line_control.reset();
444         self.receive_status_error_clear.reset();
445         self.dmacr = 0;
446         self.int_enabled = 0;
447         self.int_level = 0;
448         self.ilpr = 0;
449         self.ibrd = 0;
450         self.fbrd = 0;
451         self.read_trigger = 1;
452         self.ifl = 0x12;
453         self.control.reset();
454         self.flags = 0.into();
455         self.reset_fifo();
456     }
457 
458     pub fn reset_fifo(&mut self) {
459         self.read_count = 0;
460         self.read_pos = 0;
461 
462         /* Reset FIFO flags */
463         self.flags.reset();
464     }
465 
466     pub fn can_receive(&self) -> bool {
467         // trace_pl011_can_receive(s->lcr, s->read_count, r);
468         self.read_count < self.fifo_depth()
469     }
470 
471     pub fn event(&mut self, event: QEMUChrEvent) {
472         if event == bindings::QEMUChrEvent::CHR_EVENT_BREAK && !self.fifo_enabled() {
473             self.put_fifo(DATA_BREAK);
474             self.receive_status_error_clear.set_break_error(true);
475         }
476     }
477 
478     #[inline]
479     pub fn fifo_enabled(&self) -> bool {
480         matches!(self.line_control.fifos_enabled(), registers::Mode::FIFO)
481     }
482 
483     #[inline]
484     pub fn loopback_enabled(&self) -> bool {
485         self.control.enable_loopback()
486     }
487 
488     #[inline]
489     pub fn fifo_depth(&self) -> usize {
490         // Note: FIFO depth is expected to be power-of-2
491         if self.fifo_enabled() {
492             return PL011_FIFO_DEPTH;
493         }
494         1
495     }
496 
497     pub fn put_fifo(&mut self, value: c_uint) {
498         let depth = self.fifo_depth();
499         assert!(depth > 0);
500         let slot = (self.read_pos + self.read_count) & (depth - 1);
501         self.read_fifo[slot] = value;
502         self.read_count += 1;
503         // s->flags &= ~PL011_FLAG_RXFE;
504         self.flags.set_receive_fifo_empty(false);
505         if self.read_count == depth {
506             //s->flags |= PL011_FLAG_RXFF;
507             self.flags.set_receive_fifo_full(true);
508         }
509 
510         if self.read_count == self.read_trigger {
511             self.int_level |= registers::INT_RX;
512             self.update();
513         }
514     }
515 
516     pub fn update(&self) {
517         let flags = self.int_level & self.int_enabled;
518         for (irq, i) in self.interrupts.iter().zip(IRQMASK) {
519             // SAFETY: self.interrupts have been initialized in init().
520             unsafe { qemu_set_irq(*irq, i32::from(flags & i != 0)) };
521         }
522     }
523 
524     pub fn post_load(&mut self, _version_id: u32) -> Result<(), ()> {
525         /* Sanity-check input state */
526         if self.read_pos >= self.read_fifo.len() || self.read_count > self.read_fifo.len() {
527             return Err(());
528         }
529 
530         if !self.fifo_enabled() && self.read_count > 0 && self.read_pos > 0 {
531             // Older versions of PL011 didn't ensure that the single
532             // character in the FIFO in FIFO-disabled mode is in
533             // element 0 of the array; convert to follow the current
534             // code's assumptions.
535             self.read_fifo[0] = self.read_fifo[self.read_pos];
536             self.read_pos = 0;
537         }
538 
539         self.ibrd &= IBRD_MASK;
540         self.fbrd &= FBRD_MASK;
541 
542         Ok(())
543     }
544 }
545 
546 /// Which bits in the interrupt status matter for each outbound IRQ line ?
547 pub const IRQMASK: [u32; 6] = [
548     /* combined IRQ */
549     Interrupt::E
550         | Interrupt::MS
551         | Interrupt::RT as u32
552         | Interrupt::TX as u32
553         | Interrupt::RX as u32,
554     Interrupt::RX as u32,
555     Interrupt::TX as u32,
556     Interrupt::RT as u32,
557     Interrupt::MS,
558     Interrupt::E,
559 ];
560 
561 /// # Safety
562 ///
563 /// We expect the FFI user of this function to pass a valid pointer, that has
564 /// the same size as [`PL011State`]. We also expect the device is
565 /// readable/writeable from one thread at any time.
566 pub unsafe extern "C" fn pl011_can_receive(opaque: *mut c_void) -> c_int {
567     unsafe {
568         debug_assert!(!opaque.is_null());
569         let state = NonNull::new_unchecked(opaque.cast::<PL011State>());
570         state.as_ref().can_receive().into()
571     }
572 }
573 
574 /// # Safety
575 ///
576 /// We expect the FFI user of this function to pass a valid pointer, that has
577 /// the same size as [`PL011State`]. We also expect the device is
578 /// readable/writeable from one thread at any time.
579 ///
580 /// The buffer and size arguments must also be valid.
581 pub unsafe extern "C" fn pl011_receive(
582     opaque: *mut core::ffi::c_void,
583     buf: *const u8,
584     size: core::ffi::c_int,
585 ) {
586     unsafe {
587         debug_assert!(!opaque.is_null());
588         let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>());
589         if state.as_ref().loopback_enabled() {
590             return;
591         }
592         if size > 0 {
593             debug_assert!(!buf.is_null());
594             state.as_mut().put_fifo(c_uint::from(buf.read_volatile()))
595         }
596     }
597 }
598 
599 /// # Safety
600 ///
601 /// We expect the FFI user of this function to pass a valid pointer, that has
602 /// the same size as [`PL011State`]. We also expect the device is
603 /// readable/writeable from one thread at any time.
604 pub unsafe extern "C" fn pl011_event(opaque: *mut core::ffi::c_void, event: QEMUChrEvent) {
605     unsafe {
606         debug_assert!(!opaque.is_null());
607         let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>());
608         state.as_mut().event(event)
609     }
610 }
611 
612 /// # Safety
613 ///
614 /// We expect the FFI user of this function to pass a valid pointer for `chr`.
615 #[no_mangle]
616 pub unsafe extern "C" fn pl011_create(
617     addr: u64,
618     irq: qemu_irq,
619     chr: *mut Chardev,
620 ) -> *mut DeviceState {
621     unsafe {
622         let dev: *mut DeviceState = qdev_new(PL011State::TYPE_INFO.name);
623         let sysbus: *mut SysBusDevice = dev.cast::<SysBusDevice>();
624 
625         qdev_prop_set_chr(dev, c"chardev".as_ptr(), chr);
626         sysbus_realize_and_unref(sysbus, addr_of!(error_fatal) as *mut *mut Error);
627         sysbus_mmio_map(sysbus, 0, addr);
628         sysbus_connect_irq(sysbus, 0, irq);
629         dev
630     }
631 }
632 
633 /// # Safety
634 ///
635 /// We expect the FFI user of this function to pass a valid pointer, that has
636 /// the same size as [`PL011State`]. We also expect the device is
637 /// readable/writeable from one thread at any time.
638 pub unsafe extern "C" fn pl011_init(obj: *mut Object) {
639     unsafe {
640         debug_assert!(!obj.is_null());
641         let mut state = NonNull::new_unchecked(obj.cast::<PL011State>());
642         state.as_mut().init();
643     }
644 }
645 
646 #[repr(C)]
647 #[derive(Debug, qemu_api_macros::Object)]
648 /// PL011 Luminary device model.
649 pub struct PL011Luminary {
650     parent_obj: PL011State,
651 }
652 
653 #[repr(C)]
654 pub struct PL011LuminaryClass {
655     _inner: [u8; 0],
656 }
657 
658 /// Initializes a pre-allocated, unitialized instance of `PL011Luminary`.
659 ///
660 /// # Safety
661 ///
662 /// We expect the FFI user of this function to pass a valid pointer, that has
663 /// the same size as [`PL011Luminary`]. We also expect the device is
664 /// readable/writeable from one thread at any time.
665 pub unsafe extern "C" fn pl011_luminary_init(obj: *mut Object) {
666     unsafe {
667         debug_assert!(!obj.is_null());
668         let mut state = NonNull::new_unchecked(obj.cast::<PL011Luminary>());
669         let state = state.as_mut();
670         state.parent_obj.device_id = DeviceId::Luminary;
671     }
672 }
673 
674 impl qemu_api::definitions::Class for PL011LuminaryClass {
675     const CLASS_INIT: Option<
676         unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void),
677     > = None;
678     const CLASS_BASE_INIT: Option<
679         unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void),
680     > = None;
681 }
682 
683 impl ObjectImpl for PL011Luminary {
684     type Class = PL011LuminaryClass;
685     const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
686     const TYPE_NAME: &'static CStr = crate::TYPE_PL011_LUMINARY;
687     const PARENT_TYPE_NAME: Option<&'static CStr> = Some(crate::TYPE_PL011);
688     const ABSTRACT: bool = false;
689     const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = Some(pl011_luminary_init);
690     const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
691     const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
692 }
693