xref: /openbmc/qemu/rust/hw/char/pl011/src/device.rs (revision 718e255f)
137fdb2f5SManos Pitsidianakis // Copyright 2024, Linaro Limited
237fdb2f5SManos Pitsidianakis // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
337fdb2f5SManos Pitsidianakis // SPDX-License-Identifier: GPL-2.0-or-later
437fdb2f5SManos Pitsidianakis 
59f7d4520SPaolo Bonzini use core::ptr::{addr_of, addr_of_mut, NonNull};
69f7d4520SPaolo Bonzini use std::{
79f7d4520SPaolo Bonzini     ffi::CStr,
89f7d4520SPaolo Bonzini     os::raw::{c_int, c_uchar, c_uint, c_void},
937fdb2f5SManos Pitsidianakis };
1037fdb2f5SManos Pitsidianakis 
1137fdb2f5SManos Pitsidianakis use qemu_api::{
1237fdb2f5SManos Pitsidianakis     bindings::{self, *},
13*718e255fSPaolo Bonzini     c_str,
1437fdb2f5SManos Pitsidianakis     definitions::ObjectImpl,
1537fdb2f5SManos Pitsidianakis };
1637fdb2f5SManos Pitsidianakis 
1737fdb2f5SManos Pitsidianakis use crate::{
1837fdb2f5SManos Pitsidianakis     memory_ops::PL011_OPS,
1937fdb2f5SManos Pitsidianakis     registers::{self, Interrupt},
2037fdb2f5SManos Pitsidianakis     RegisterOffset,
2137fdb2f5SManos Pitsidianakis };
2237fdb2f5SManos Pitsidianakis 
2393243319SManos Pitsidianakis /// Integer Baud Rate Divider, `UARTIBRD`
24230b710bSManos Pitsidianakis const IBRD_MASK: u32 = 0xffff;
2593243319SManos Pitsidianakis 
2693243319SManos Pitsidianakis /// Fractional Baud Rate Divider, `UARTFBRD`
27230b710bSManos Pitsidianakis const FBRD_MASK: u32 = 0x3f;
2893243319SManos Pitsidianakis 
2937fdb2f5SManos Pitsidianakis const DATA_BREAK: u32 = 1 << 10;
3037fdb2f5SManos Pitsidianakis 
3137fdb2f5SManos Pitsidianakis /// QEMU sourced constant.
3237fdb2f5SManos Pitsidianakis pub const PL011_FIFO_DEPTH: usize = 16_usize;
3337fdb2f5SManos Pitsidianakis 
342e06e72dSManos Pitsidianakis #[derive(Clone, Copy, Debug)]
352e06e72dSManos Pitsidianakis enum DeviceId {
362e06e72dSManos Pitsidianakis     #[allow(dead_code)]
372e06e72dSManos Pitsidianakis     Arm = 0,
382e06e72dSManos Pitsidianakis     Luminary,
392e06e72dSManos Pitsidianakis }
402e06e72dSManos Pitsidianakis 
412e06e72dSManos Pitsidianakis impl std::ops::Index<hwaddr> for DeviceId {
422e06e72dSManos Pitsidianakis     type Output = c_uchar;
432e06e72dSManos Pitsidianakis 
442e06e72dSManos Pitsidianakis     fn index(&self, idx: hwaddr) -> &Self::Output {
452e06e72dSManos Pitsidianakis         match self {
462e06e72dSManos Pitsidianakis             Self::Arm => &Self::PL011_ID_ARM[idx as usize],
472e06e72dSManos Pitsidianakis             Self::Luminary => &Self::PL011_ID_LUMINARY[idx as usize],
482e06e72dSManos Pitsidianakis         }
492e06e72dSManos Pitsidianakis     }
502e06e72dSManos Pitsidianakis }
512e06e72dSManos Pitsidianakis 
522e06e72dSManos Pitsidianakis impl DeviceId {
532e06e72dSManos Pitsidianakis     const PL011_ID_ARM: [c_uchar; 8] = [0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1];
542e06e72dSManos Pitsidianakis     const PL011_ID_LUMINARY: [c_uchar; 8] = [0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1];
552e06e72dSManos Pitsidianakis }
562e06e72dSManos Pitsidianakis 
5737fdb2f5SManos Pitsidianakis #[repr(C)]
5837fdb2f5SManos Pitsidianakis #[derive(Debug, qemu_api_macros::Object)]
5937fdb2f5SManos Pitsidianakis /// PL011 Device Model in QEMU
6037fdb2f5SManos Pitsidianakis pub struct PL011State {
6137fdb2f5SManos Pitsidianakis     pub parent_obj: SysBusDevice,
6237fdb2f5SManos Pitsidianakis     pub iomem: MemoryRegion,
6337fdb2f5SManos Pitsidianakis     #[doc(alias = "fr")]
6437fdb2f5SManos Pitsidianakis     pub flags: registers::Flags,
6537fdb2f5SManos Pitsidianakis     #[doc(alias = "lcr")]
6637fdb2f5SManos Pitsidianakis     pub line_control: registers::LineControl,
6737fdb2f5SManos Pitsidianakis     #[doc(alias = "rsr")]
6837fdb2f5SManos Pitsidianakis     pub receive_status_error_clear: registers::ReceiveStatusErrorClear,
6937fdb2f5SManos Pitsidianakis     #[doc(alias = "cr")]
7037fdb2f5SManos Pitsidianakis     pub control: registers::Control,
7137fdb2f5SManos Pitsidianakis     pub dmacr: u32,
7237fdb2f5SManos Pitsidianakis     pub int_enabled: u32,
7337fdb2f5SManos Pitsidianakis     pub int_level: u32,
7437fdb2f5SManos Pitsidianakis     pub read_fifo: [u32; PL011_FIFO_DEPTH],
7537fdb2f5SManos Pitsidianakis     pub ilpr: u32,
7637fdb2f5SManos Pitsidianakis     pub ibrd: u32,
7737fdb2f5SManos Pitsidianakis     pub fbrd: u32,
7837fdb2f5SManos Pitsidianakis     pub ifl: u32,
7937fdb2f5SManos Pitsidianakis     pub read_pos: usize,
8037fdb2f5SManos Pitsidianakis     pub read_count: usize,
8137fdb2f5SManos Pitsidianakis     pub read_trigger: usize,
8237fdb2f5SManos Pitsidianakis     #[doc(alias = "chr")]
8337fdb2f5SManos Pitsidianakis     pub char_backend: CharBackend,
8437fdb2f5SManos Pitsidianakis     /// QEMU interrupts
8537fdb2f5SManos Pitsidianakis     ///
8637fdb2f5SManos Pitsidianakis     /// ```text
8737fdb2f5SManos Pitsidianakis     ///  * sysbus MMIO region 0: device registers
8837fdb2f5SManos Pitsidianakis     ///  * sysbus IRQ 0: `UARTINTR` (combined interrupt line)
8937fdb2f5SManos Pitsidianakis     ///  * sysbus IRQ 1: `UARTRXINTR` (receive FIFO interrupt line)
9037fdb2f5SManos Pitsidianakis     ///  * sysbus IRQ 2: `UARTTXINTR` (transmit FIFO interrupt line)
9137fdb2f5SManos Pitsidianakis     ///  * sysbus IRQ 3: `UARTRTINTR` (receive timeout interrupt line)
9237fdb2f5SManos Pitsidianakis     ///  * sysbus IRQ 4: `UARTMSINTR` (momem status interrupt line)
9337fdb2f5SManos Pitsidianakis     ///  * sysbus IRQ 5: `UARTEINTR` (error interrupt line)
9437fdb2f5SManos Pitsidianakis     /// ```
9537fdb2f5SManos Pitsidianakis     #[doc(alias = "irq")]
9637fdb2f5SManos Pitsidianakis     pub interrupts: [qemu_irq; 6usize],
9737fdb2f5SManos Pitsidianakis     #[doc(alias = "clk")]
9837fdb2f5SManos Pitsidianakis     pub clock: NonNull<Clock>,
9937fdb2f5SManos Pitsidianakis     #[doc(alias = "migrate_clk")]
10037fdb2f5SManos Pitsidianakis     pub migrate_clock: bool,
1012e06e72dSManos Pitsidianakis     /// The byte string that identifies the device.
1022e06e72dSManos Pitsidianakis     device_id: DeviceId,
10337fdb2f5SManos Pitsidianakis }
10437fdb2f5SManos Pitsidianakis 
10537fdb2f5SManos Pitsidianakis impl ObjectImpl for PL011State {
10637fdb2f5SManos Pitsidianakis     type Class = PL011Class;
10737fdb2f5SManos Pitsidianakis     const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
10837fdb2f5SManos Pitsidianakis     const TYPE_NAME: &'static CStr = crate::TYPE_PL011;
10937fdb2f5SManos Pitsidianakis     const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_SYS_BUS_DEVICE);
11037fdb2f5SManos Pitsidianakis     const ABSTRACT: bool = false;
11137fdb2f5SManos Pitsidianakis     const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = Some(pl011_init);
11237fdb2f5SManos Pitsidianakis     const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
11337fdb2f5SManos Pitsidianakis     const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
11437fdb2f5SManos Pitsidianakis }
11537fdb2f5SManos Pitsidianakis 
11637fdb2f5SManos Pitsidianakis #[repr(C)]
11737fdb2f5SManos Pitsidianakis pub struct PL011Class {
11837fdb2f5SManos Pitsidianakis     _inner: [u8; 0],
11937fdb2f5SManos Pitsidianakis }
12037fdb2f5SManos Pitsidianakis 
12137fdb2f5SManos Pitsidianakis impl qemu_api::definitions::Class for PL011Class {
1229f7d4520SPaolo Bonzini     const CLASS_INIT: Option<unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void)> =
1239f7d4520SPaolo Bonzini         Some(crate::device_class::pl011_class_init);
12437fdb2f5SManos Pitsidianakis     const CLASS_BASE_INIT: Option<
1259f7d4520SPaolo Bonzini         unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
12637fdb2f5SManos Pitsidianakis     > = None;
12737fdb2f5SManos Pitsidianakis }
12837fdb2f5SManos Pitsidianakis 
12937fdb2f5SManos Pitsidianakis impl PL011State {
13037fdb2f5SManos Pitsidianakis     /// Initializes a pre-allocated, unitialized instance of `PL011State`.
13137fdb2f5SManos Pitsidianakis     ///
13237fdb2f5SManos Pitsidianakis     /// # Safety
13337fdb2f5SManos Pitsidianakis     ///
13437fdb2f5SManos Pitsidianakis     /// `self` must point to a correctly sized and aligned location for the
13537fdb2f5SManos Pitsidianakis     /// `PL011State` type. It must not be called more than once on the same
13637fdb2f5SManos Pitsidianakis     /// location/instance. All its fields are expected to hold unitialized
13737fdb2f5SManos Pitsidianakis     /// values with the sole exception of `parent_obj`.
1382e57bb6bSManos Pitsidianakis     unsafe fn init(&mut self) {
139*718e255fSPaolo Bonzini         const CLK_NAME: &CStr = c_str!("clk");
1402e57bb6bSManos Pitsidianakis 
14137fdb2f5SManos Pitsidianakis         let dev = addr_of_mut!(*self).cast::<DeviceState>();
14237fdb2f5SManos Pitsidianakis         // SAFETY:
14337fdb2f5SManos Pitsidianakis         //
14437fdb2f5SManos Pitsidianakis         // self and self.iomem are guaranteed to be valid at this point since callers
14537fdb2f5SManos Pitsidianakis         // must make sure the `self` reference is valid.
14637fdb2f5SManos Pitsidianakis         unsafe {
14737fdb2f5SManos Pitsidianakis             memory_region_init_io(
14837fdb2f5SManos Pitsidianakis                 addr_of_mut!(self.iomem),
14937fdb2f5SManos Pitsidianakis                 addr_of_mut!(*self).cast::<Object>(),
15037fdb2f5SManos Pitsidianakis                 &PL011_OPS,
15137fdb2f5SManos Pitsidianakis                 addr_of_mut!(*self).cast::<c_void>(),
15237fdb2f5SManos Pitsidianakis                 Self::TYPE_INFO.name,
15337fdb2f5SManos Pitsidianakis                 0x1000,
15437fdb2f5SManos Pitsidianakis             );
15537fdb2f5SManos Pitsidianakis             let sbd = addr_of_mut!(*self).cast::<SysBusDevice>();
15637fdb2f5SManos Pitsidianakis             sysbus_init_mmio(sbd, addr_of_mut!(self.iomem));
15737fdb2f5SManos Pitsidianakis             for irq in self.interrupts.iter_mut() {
15837fdb2f5SManos Pitsidianakis                 sysbus_init_irq(sbd, irq);
15937fdb2f5SManos Pitsidianakis             }
16037fdb2f5SManos Pitsidianakis         }
16137fdb2f5SManos Pitsidianakis         // SAFETY:
16237fdb2f5SManos Pitsidianakis         //
16337fdb2f5SManos Pitsidianakis         // self.clock is not initialized at this point; but since `NonNull<_>` is Copy,
16437fdb2f5SManos Pitsidianakis         // we can overwrite the undefined value without side effects. This is
16537fdb2f5SManos Pitsidianakis         // safe since all PL011State instances are created by QOM code which
16637fdb2f5SManos Pitsidianakis         // calls this function to initialize the fields; therefore no code is
16737fdb2f5SManos Pitsidianakis         // able to access an invalid self.clock value.
16837fdb2f5SManos Pitsidianakis         unsafe {
16937fdb2f5SManos Pitsidianakis             self.clock = NonNull::new(qdev_init_clock_in(
17037fdb2f5SManos Pitsidianakis                 dev,
17137fdb2f5SManos Pitsidianakis                 CLK_NAME.as_ptr(),
17237fdb2f5SManos Pitsidianakis                 None, /* pl011_clock_update */
17337fdb2f5SManos Pitsidianakis                 addr_of_mut!(*self).cast::<c_void>(),
17437fdb2f5SManos Pitsidianakis                 ClockEvent::ClockUpdate.0,
17537fdb2f5SManos Pitsidianakis             ))
17637fdb2f5SManos Pitsidianakis             .unwrap();
17737fdb2f5SManos Pitsidianakis         }
17837fdb2f5SManos Pitsidianakis     }
17937fdb2f5SManos Pitsidianakis 
1809f7d4520SPaolo Bonzini     pub fn read(&mut self, offset: hwaddr, _size: c_uint) -> std::ops::ControlFlow<u64, u64> {
18137fdb2f5SManos Pitsidianakis         use RegisterOffset::*;
18237fdb2f5SManos Pitsidianakis 
18337fdb2f5SManos Pitsidianakis         std::ops::ControlFlow::Break(match RegisterOffset::try_from(offset) {
18437fdb2f5SManos Pitsidianakis             Err(v) if (0x3f8..0x400).contains(&v) => {
1852e06e72dSManos Pitsidianakis                 u64::from(self.device_id[(offset - 0xfe0) >> 2])
18637fdb2f5SManos Pitsidianakis             }
18737fdb2f5SManos Pitsidianakis             Err(_) => {
18837fdb2f5SManos Pitsidianakis                 // qemu_log_mask(LOG_GUEST_ERROR, "pl011_read: Bad offset 0x%x\n", (int)offset);
18937fdb2f5SManos Pitsidianakis                 0
19037fdb2f5SManos Pitsidianakis             }
19137fdb2f5SManos Pitsidianakis             Ok(DR) => {
19237fdb2f5SManos Pitsidianakis                 self.flags.set_receive_fifo_full(false);
19337fdb2f5SManos Pitsidianakis                 let c = self.read_fifo[self.read_pos];
19437fdb2f5SManos Pitsidianakis                 if self.read_count > 0 {
19537fdb2f5SManos Pitsidianakis                     self.read_count -= 1;
19637fdb2f5SManos Pitsidianakis                     self.read_pos = (self.read_pos + 1) & (self.fifo_depth() - 1);
19737fdb2f5SManos Pitsidianakis                 }
19837fdb2f5SManos Pitsidianakis                 if self.read_count == 0 {
19937fdb2f5SManos Pitsidianakis                     self.flags.set_receive_fifo_empty(true);
20037fdb2f5SManos Pitsidianakis                 }
20137fdb2f5SManos Pitsidianakis                 if self.read_count + 1 == self.read_trigger {
20237fdb2f5SManos Pitsidianakis                     self.int_level &= !registers::INT_RX;
20337fdb2f5SManos Pitsidianakis                 }
20437fdb2f5SManos Pitsidianakis                 // Update error bits.
20537fdb2f5SManos Pitsidianakis                 self.receive_status_error_clear = c.to_be_bytes()[3].into();
20637fdb2f5SManos Pitsidianakis                 self.update();
20737fdb2f5SManos Pitsidianakis                 // Must call qemu_chr_fe_accept_input, so return Continue:
20837fdb2f5SManos Pitsidianakis                 return std::ops::ControlFlow::Continue(c.into());
20937fdb2f5SManos Pitsidianakis             }
21037fdb2f5SManos Pitsidianakis             Ok(RSR) => u8::from(self.receive_status_error_clear).into(),
21137fdb2f5SManos Pitsidianakis             Ok(FR) => u16::from(self.flags).into(),
21237fdb2f5SManos Pitsidianakis             Ok(FBRD) => self.fbrd.into(),
21337fdb2f5SManos Pitsidianakis             Ok(ILPR) => self.ilpr.into(),
21437fdb2f5SManos Pitsidianakis             Ok(IBRD) => self.ibrd.into(),
21537fdb2f5SManos Pitsidianakis             Ok(LCR_H) => u16::from(self.line_control).into(),
21637fdb2f5SManos Pitsidianakis             Ok(CR) => {
21737fdb2f5SManos Pitsidianakis                 // We exercise our self-control.
21837fdb2f5SManos Pitsidianakis                 u16::from(self.control).into()
21937fdb2f5SManos Pitsidianakis             }
22037fdb2f5SManos Pitsidianakis             Ok(FLS) => self.ifl.into(),
22137fdb2f5SManos Pitsidianakis             Ok(IMSC) => self.int_enabled.into(),
22237fdb2f5SManos Pitsidianakis             Ok(RIS) => self.int_level.into(),
22337fdb2f5SManos Pitsidianakis             Ok(MIS) => u64::from(self.int_level & self.int_enabled),
22437fdb2f5SManos Pitsidianakis             Ok(ICR) => {
22537fdb2f5SManos Pitsidianakis                 // "The UARTICR Register is the interrupt clear register and is write-only"
22637fdb2f5SManos Pitsidianakis                 // Source: ARM DDI 0183G 3.3.13 Interrupt Clear Register, UARTICR
22737fdb2f5SManos Pitsidianakis                 0
22837fdb2f5SManos Pitsidianakis             }
22937fdb2f5SManos Pitsidianakis             Ok(DMACR) => self.dmacr.into(),
23037fdb2f5SManos Pitsidianakis         })
23137fdb2f5SManos Pitsidianakis     }
23237fdb2f5SManos Pitsidianakis 
23337fdb2f5SManos Pitsidianakis     pub fn write(&mut self, offset: hwaddr, value: u64) {
23437fdb2f5SManos Pitsidianakis         // eprintln!("write offset {offset} value {value}");
23537fdb2f5SManos Pitsidianakis         use RegisterOffset::*;
23637fdb2f5SManos Pitsidianakis         let value: u32 = value as u32;
23737fdb2f5SManos Pitsidianakis         match RegisterOffset::try_from(offset) {
23837fdb2f5SManos Pitsidianakis             Err(_bad_offset) => {
23937fdb2f5SManos Pitsidianakis                 eprintln!("write bad offset {offset} value {value}");
24037fdb2f5SManos Pitsidianakis             }
24137fdb2f5SManos Pitsidianakis             Ok(DR) => {
24237fdb2f5SManos Pitsidianakis                 // ??? Check if transmitter is enabled.
24337fdb2f5SManos Pitsidianakis                 let ch: u8 = value as u8;
24437fdb2f5SManos Pitsidianakis                 // XXX this blocks entire thread. Rewrite to use
24537fdb2f5SManos Pitsidianakis                 // qemu_chr_fe_write and background I/O callbacks
24637fdb2f5SManos Pitsidianakis 
24737fdb2f5SManos Pitsidianakis                 // SAFETY: self.char_backend is a valid CharBackend instance after it's been
24837fdb2f5SManos Pitsidianakis                 // initialized in realize().
24937fdb2f5SManos Pitsidianakis                 unsafe {
25037fdb2f5SManos Pitsidianakis                     qemu_chr_fe_write_all(addr_of_mut!(self.char_backend), &ch, 1);
25137fdb2f5SManos Pitsidianakis                 }
25237fdb2f5SManos Pitsidianakis                 self.loopback_tx(value);
25337fdb2f5SManos Pitsidianakis                 self.int_level |= registers::INT_TX;
25437fdb2f5SManos Pitsidianakis                 self.update();
25537fdb2f5SManos Pitsidianakis             }
25637fdb2f5SManos Pitsidianakis             Ok(RSR) => {
25737fdb2f5SManos Pitsidianakis                 self.receive_status_error_clear = 0.into();
25837fdb2f5SManos Pitsidianakis             }
25937fdb2f5SManos Pitsidianakis             Ok(FR) => {
26037fdb2f5SManos Pitsidianakis                 // flag writes are ignored
26137fdb2f5SManos Pitsidianakis             }
26237fdb2f5SManos Pitsidianakis             Ok(ILPR) => {
26337fdb2f5SManos Pitsidianakis                 self.ilpr = value;
26437fdb2f5SManos Pitsidianakis             }
26537fdb2f5SManos Pitsidianakis             Ok(IBRD) => {
26637fdb2f5SManos Pitsidianakis                 self.ibrd = value;
26737fdb2f5SManos Pitsidianakis             }
26837fdb2f5SManos Pitsidianakis             Ok(FBRD) => {
26937fdb2f5SManos Pitsidianakis                 self.fbrd = value;
27037fdb2f5SManos Pitsidianakis             }
27137fdb2f5SManos Pitsidianakis             Ok(LCR_H) => {
27237fdb2f5SManos Pitsidianakis                 let value = value as u16;
27337fdb2f5SManos Pitsidianakis                 let new_val: registers::LineControl = value.into();
27437fdb2f5SManos Pitsidianakis                 // Reset the FIFO state on FIFO enable or disable
27537fdb2f5SManos Pitsidianakis                 if bool::from(self.line_control.fifos_enabled())
27637fdb2f5SManos Pitsidianakis                     ^ bool::from(new_val.fifos_enabled())
27737fdb2f5SManos Pitsidianakis                 {
27837fdb2f5SManos Pitsidianakis                     self.reset_fifo();
27937fdb2f5SManos Pitsidianakis                 }
28037fdb2f5SManos Pitsidianakis                 if self.line_control.send_break() ^ new_val.send_break() {
28137fdb2f5SManos Pitsidianakis                     let mut break_enable: c_int = new_val.send_break().into();
28237fdb2f5SManos Pitsidianakis                     // SAFETY: self.char_backend is a valid CharBackend instance after it's been
28337fdb2f5SManos Pitsidianakis                     // initialized in realize().
28437fdb2f5SManos Pitsidianakis                     unsafe {
28537fdb2f5SManos Pitsidianakis                         qemu_chr_fe_ioctl(
28637fdb2f5SManos Pitsidianakis                             addr_of_mut!(self.char_backend),
28737fdb2f5SManos Pitsidianakis                             CHR_IOCTL_SERIAL_SET_BREAK as i32,
28837fdb2f5SManos Pitsidianakis                             addr_of_mut!(break_enable).cast::<c_void>(),
28937fdb2f5SManos Pitsidianakis                         );
29037fdb2f5SManos Pitsidianakis                     }
29137fdb2f5SManos Pitsidianakis                     self.loopback_break(break_enable > 0);
29237fdb2f5SManos Pitsidianakis                 }
29337fdb2f5SManos Pitsidianakis                 self.line_control = new_val;
29437fdb2f5SManos Pitsidianakis                 self.set_read_trigger();
29537fdb2f5SManos Pitsidianakis             }
29637fdb2f5SManos Pitsidianakis             Ok(CR) => {
29737fdb2f5SManos Pitsidianakis                 // ??? Need to implement the enable bit.
29837fdb2f5SManos Pitsidianakis                 let value = value as u16;
29937fdb2f5SManos Pitsidianakis                 self.control = value.into();
30037fdb2f5SManos Pitsidianakis                 self.loopback_mdmctrl();
30137fdb2f5SManos Pitsidianakis             }
30237fdb2f5SManos Pitsidianakis             Ok(FLS) => {
30337fdb2f5SManos Pitsidianakis                 self.ifl = value;
30437fdb2f5SManos Pitsidianakis                 self.set_read_trigger();
30537fdb2f5SManos Pitsidianakis             }
30637fdb2f5SManos Pitsidianakis             Ok(IMSC) => {
30737fdb2f5SManos Pitsidianakis                 self.int_enabled = value;
30837fdb2f5SManos Pitsidianakis                 self.update();
30937fdb2f5SManos Pitsidianakis             }
31037fdb2f5SManos Pitsidianakis             Ok(RIS) => {}
31137fdb2f5SManos Pitsidianakis             Ok(MIS) => {}
31237fdb2f5SManos Pitsidianakis             Ok(ICR) => {
31337fdb2f5SManos Pitsidianakis                 self.int_level &= !value;
31437fdb2f5SManos Pitsidianakis                 self.update();
31537fdb2f5SManos Pitsidianakis             }
31637fdb2f5SManos Pitsidianakis             Ok(DMACR) => {
31737fdb2f5SManos Pitsidianakis                 self.dmacr = value;
31837fdb2f5SManos Pitsidianakis                 if value & 3 > 0 {
31937fdb2f5SManos Pitsidianakis                     // qemu_log_mask(LOG_UNIMP, "pl011: DMA not implemented\n");
32037fdb2f5SManos Pitsidianakis                     eprintln!("pl011: DMA not implemented");
32137fdb2f5SManos Pitsidianakis                 }
32237fdb2f5SManos Pitsidianakis             }
32337fdb2f5SManos Pitsidianakis         }
32437fdb2f5SManos Pitsidianakis     }
32537fdb2f5SManos Pitsidianakis 
32637fdb2f5SManos Pitsidianakis     #[inline]
32737fdb2f5SManos Pitsidianakis     fn loopback_tx(&mut self, value: u32) {
32837fdb2f5SManos Pitsidianakis         if !self.loopback_enabled() {
32937fdb2f5SManos Pitsidianakis             return;
33037fdb2f5SManos Pitsidianakis         }
33137fdb2f5SManos Pitsidianakis 
33237fdb2f5SManos Pitsidianakis         // Caveat:
33337fdb2f5SManos Pitsidianakis         //
33437fdb2f5SManos Pitsidianakis         // In real hardware, TX loopback happens at the serial-bit level
33537fdb2f5SManos Pitsidianakis         // and then reassembled by the RX logics back into bytes and placed
33637fdb2f5SManos Pitsidianakis         // into the RX fifo. That is, loopback happens after TX fifo.
33737fdb2f5SManos Pitsidianakis         //
33837fdb2f5SManos Pitsidianakis         // Because the real hardware TX fifo is time-drained at the frame
33937fdb2f5SManos Pitsidianakis         // rate governed by the configured serial format, some loopback
34037fdb2f5SManos Pitsidianakis         // bytes in TX fifo may still be able to get into the RX fifo
34137fdb2f5SManos Pitsidianakis         // that could be full at times while being drained at software
34237fdb2f5SManos Pitsidianakis         // pace.
34337fdb2f5SManos Pitsidianakis         //
34437fdb2f5SManos Pitsidianakis         // In such scenario, the RX draining pace is the major factor
34537fdb2f5SManos Pitsidianakis         // deciding which loopback bytes get into the RX fifo, unless
34637fdb2f5SManos Pitsidianakis         // hardware flow-control is enabled.
34737fdb2f5SManos Pitsidianakis         //
34837fdb2f5SManos Pitsidianakis         // For simplicity, the above described is not emulated.
34937fdb2f5SManos Pitsidianakis         self.put_fifo(value);
35037fdb2f5SManos Pitsidianakis     }
35137fdb2f5SManos Pitsidianakis 
35237fdb2f5SManos Pitsidianakis     fn loopback_mdmctrl(&mut self) {
35337fdb2f5SManos Pitsidianakis         if !self.loopback_enabled() {
35437fdb2f5SManos Pitsidianakis             return;
35537fdb2f5SManos Pitsidianakis         }
35637fdb2f5SManos Pitsidianakis 
35737fdb2f5SManos Pitsidianakis         /*
35837fdb2f5SManos Pitsidianakis          * Loopback software-driven modem control outputs to modem status inputs:
35937fdb2f5SManos Pitsidianakis          *   FR.RI  <= CR.Out2
36037fdb2f5SManos Pitsidianakis          *   FR.DCD <= CR.Out1
36137fdb2f5SManos Pitsidianakis          *   FR.CTS <= CR.RTS
36237fdb2f5SManos Pitsidianakis          *   FR.DSR <= CR.DTR
36337fdb2f5SManos Pitsidianakis          *
36437fdb2f5SManos Pitsidianakis          * The loopback happens immediately even if this call is triggered
36537fdb2f5SManos Pitsidianakis          * by setting only CR.LBE.
36637fdb2f5SManos Pitsidianakis          *
36737fdb2f5SManos Pitsidianakis          * CTS/RTS updates due to enabled hardware flow controls are not
36837fdb2f5SManos Pitsidianakis          * dealt with here.
36937fdb2f5SManos Pitsidianakis          */
37037fdb2f5SManos Pitsidianakis 
37137fdb2f5SManos Pitsidianakis         self.flags.set_ring_indicator(self.control.out_2());
37237fdb2f5SManos Pitsidianakis         self.flags.set_data_carrier_detect(self.control.out_1());
37337fdb2f5SManos Pitsidianakis         self.flags.set_clear_to_send(self.control.request_to_send());
37437fdb2f5SManos Pitsidianakis         self.flags
37537fdb2f5SManos Pitsidianakis             .set_data_set_ready(self.control.data_transmit_ready());
37637fdb2f5SManos Pitsidianakis 
37737fdb2f5SManos Pitsidianakis         // Change interrupts based on updated FR
37837fdb2f5SManos Pitsidianakis         let mut il = self.int_level;
37937fdb2f5SManos Pitsidianakis 
38037fdb2f5SManos Pitsidianakis         il &= !Interrupt::MS;
38137fdb2f5SManos Pitsidianakis 
38237fdb2f5SManos Pitsidianakis         if self.flags.data_set_ready() {
38337fdb2f5SManos Pitsidianakis             il |= Interrupt::DSR as u32;
38437fdb2f5SManos Pitsidianakis         }
38537fdb2f5SManos Pitsidianakis         if self.flags.data_carrier_detect() {
38637fdb2f5SManos Pitsidianakis             il |= Interrupt::DCD as u32;
38737fdb2f5SManos Pitsidianakis         }
38837fdb2f5SManos Pitsidianakis         if self.flags.clear_to_send() {
38937fdb2f5SManos Pitsidianakis             il |= Interrupt::CTS as u32;
39037fdb2f5SManos Pitsidianakis         }
39137fdb2f5SManos Pitsidianakis         if self.flags.ring_indicator() {
39237fdb2f5SManos Pitsidianakis             il |= Interrupt::RI as u32;
39337fdb2f5SManos Pitsidianakis         }
39437fdb2f5SManos Pitsidianakis         self.int_level = il;
39537fdb2f5SManos Pitsidianakis         self.update();
39637fdb2f5SManos Pitsidianakis     }
39737fdb2f5SManos Pitsidianakis 
39837fdb2f5SManos Pitsidianakis     fn loopback_break(&mut self, enable: bool) {
39937fdb2f5SManos Pitsidianakis         if enable {
40037fdb2f5SManos Pitsidianakis             self.loopback_tx(DATA_BREAK);
40137fdb2f5SManos Pitsidianakis         }
40237fdb2f5SManos Pitsidianakis     }
40337fdb2f5SManos Pitsidianakis 
40437fdb2f5SManos Pitsidianakis     fn set_read_trigger(&mut self) {
40537fdb2f5SManos Pitsidianakis         self.read_trigger = 1;
40637fdb2f5SManos Pitsidianakis     }
40737fdb2f5SManos Pitsidianakis 
40837fdb2f5SManos Pitsidianakis     pub fn realize(&mut self) {
40937fdb2f5SManos Pitsidianakis         // SAFETY: self.char_backend has the correct size and alignment for a
41037fdb2f5SManos Pitsidianakis         // CharBackend object, and its callbacks are of the correct types.
41137fdb2f5SManos Pitsidianakis         unsafe {
41237fdb2f5SManos Pitsidianakis             qemu_chr_fe_set_handlers(
41337fdb2f5SManos Pitsidianakis                 addr_of_mut!(self.char_backend),
41437fdb2f5SManos Pitsidianakis                 Some(pl011_can_receive),
41537fdb2f5SManos Pitsidianakis                 Some(pl011_receive),
41637fdb2f5SManos Pitsidianakis                 Some(pl011_event),
41737fdb2f5SManos Pitsidianakis                 None,
41837fdb2f5SManos Pitsidianakis                 addr_of_mut!(*self).cast::<c_void>(),
41937fdb2f5SManos Pitsidianakis                 core::ptr::null_mut(),
42037fdb2f5SManos Pitsidianakis                 true,
42137fdb2f5SManos Pitsidianakis             );
42237fdb2f5SManos Pitsidianakis         }
42337fdb2f5SManos Pitsidianakis     }
42437fdb2f5SManos Pitsidianakis 
42537fdb2f5SManos Pitsidianakis     pub fn reset(&mut self) {
42637fdb2f5SManos Pitsidianakis         self.line_control.reset();
42737fdb2f5SManos Pitsidianakis         self.receive_status_error_clear.reset();
42837fdb2f5SManos Pitsidianakis         self.dmacr = 0;
42937fdb2f5SManos Pitsidianakis         self.int_enabled = 0;
43037fdb2f5SManos Pitsidianakis         self.int_level = 0;
43137fdb2f5SManos Pitsidianakis         self.ilpr = 0;
43237fdb2f5SManos Pitsidianakis         self.ibrd = 0;
43337fdb2f5SManos Pitsidianakis         self.fbrd = 0;
43437fdb2f5SManos Pitsidianakis         self.read_trigger = 1;
43537fdb2f5SManos Pitsidianakis         self.ifl = 0x12;
43637fdb2f5SManos Pitsidianakis         self.control.reset();
43737fdb2f5SManos Pitsidianakis         self.flags = 0.into();
43837fdb2f5SManos Pitsidianakis         self.reset_fifo();
43937fdb2f5SManos Pitsidianakis     }
44037fdb2f5SManos Pitsidianakis 
44137fdb2f5SManos Pitsidianakis     pub fn reset_fifo(&mut self) {
44237fdb2f5SManos Pitsidianakis         self.read_count = 0;
44337fdb2f5SManos Pitsidianakis         self.read_pos = 0;
44437fdb2f5SManos Pitsidianakis 
44537fdb2f5SManos Pitsidianakis         /* Reset FIFO flags */
44637fdb2f5SManos Pitsidianakis         self.flags.reset();
44737fdb2f5SManos Pitsidianakis     }
44837fdb2f5SManos Pitsidianakis 
44937fdb2f5SManos Pitsidianakis     pub fn can_receive(&self) -> bool {
45037fdb2f5SManos Pitsidianakis         // trace_pl011_can_receive(s->lcr, s->read_count, r);
45137fdb2f5SManos Pitsidianakis         self.read_count < self.fifo_depth()
45237fdb2f5SManos Pitsidianakis     }
45337fdb2f5SManos Pitsidianakis 
45437fdb2f5SManos Pitsidianakis     pub fn event(&mut self, event: QEMUChrEvent) {
45537fdb2f5SManos Pitsidianakis         if event == bindings::QEMUChrEvent::CHR_EVENT_BREAK && !self.fifo_enabled() {
45637fdb2f5SManos Pitsidianakis             self.put_fifo(DATA_BREAK);
45737fdb2f5SManos Pitsidianakis             self.receive_status_error_clear.set_break_error(true);
45837fdb2f5SManos Pitsidianakis         }
45937fdb2f5SManos Pitsidianakis     }
46037fdb2f5SManos Pitsidianakis 
46137fdb2f5SManos Pitsidianakis     #[inline]
46237fdb2f5SManos Pitsidianakis     pub fn fifo_enabled(&self) -> bool {
46337fdb2f5SManos Pitsidianakis         matches!(self.line_control.fifos_enabled(), registers::Mode::FIFO)
46437fdb2f5SManos Pitsidianakis     }
46537fdb2f5SManos Pitsidianakis 
46637fdb2f5SManos Pitsidianakis     #[inline]
46737fdb2f5SManos Pitsidianakis     pub fn loopback_enabled(&self) -> bool {
46837fdb2f5SManos Pitsidianakis         self.control.enable_loopback()
46937fdb2f5SManos Pitsidianakis     }
47037fdb2f5SManos Pitsidianakis 
47137fdb2f5SManos Pitsidianakis     #[inline]
47237fdb2f5SManos Pitsidianakis     pub fn fifo_depth(&self) -> usize {
47337fdb2f5SManos Pitsidianakis         // Note: FIFO depth is expected to be power-of-2
47437fdb2f5SManos Pitsidianakis         if self.fifo_enabled() {
47537fdb2f5SManos Pitsidianakis             return PL011_FIFO_DEPTH;
47637fdb2f5SManos Pitsidianakis         }
47737fdb2f5SManos Pitsidianakis         1
47837fdb2f5SManos Pitsidianakis     }
47937fdb2f5SManos Pitsidianakis 
48037fdb2f5SManos Pitsidianakis     pub fn put_fifo(&mut self, value: c_uint) {
48137fdb2f5SManos Pitsidianakis         let depth = self.fifo_depth();
48237fdb2f5SManos Pitsidianakis         assert!(depth > 0);
48337fdb2f5SManos Pitsidianakis         let slot = (self.read_pos + self.read_count) & (depth - 1);
48437fdb2f5SManos Pitsidianakis         self.read_fifo[slot] = value;
48537fdb2f5SManos Pitsidianakis         self.read_count += 1;
48637fdb2f5SManos Pitsidianakis         self.flags.set_receive_fifo_empty(false);
48737fdb2f5SManos Pitsidianakis         if self.read_count == depth {
48837fdb2f5SManos Pitsidianakis             self.flags.set_receive_fifo_full(true);
48937fdb2f5SManos Pitsidianakis         }
49037fdb2f5SManos Pitsidianakis 
49137fdb2f5SManos Pitsidianakis         if self.read_count == self.read_trigger {
49237fdb2f5SManos Pitsidianakis             self.int_level |= registers::INT_RX;
49337fdb2f5SManos Pitsidianakis             self.update();
49437fdb2f5SManos Pitsidianakis         }
49537fdb2f5SManos Pitsidianakis     }
49637fdb2f5SManos Pitsidianakis 
49737fdb2f5SManos Pitsidianakis     pub fn update(&self) {
49837fdb2f5SManos Pitsidianakis         let flags = self.int_level & self.int_enabled;
49937fdb2f5SManos Pitsidianakis         for (irq, i) in self.interrupts.iter().zip(IRQMASK) {
50037fdb2f5SManos Pitsidianakis             // SAFETY: self.interrupts have been initialized in init().
50137fdb2f5SManos Pitsidianakis             unsafe { qemu_set_irq(*irq, i32::from(flags & i != 0)) };
50237fdb2f5SManos Pitsidianakis         }
50337fdb2f5SManos Pitsidianakis     }
50493243319SManos Pitsidianakis 
50593243319SManos Pitsidianakis     pub fn post_load(&mut self, _version_id: u32) -> Result<(), ()> {
50693243319SManos Pitsidianakis         /* Sanity-check input state */
50793243319SManos Pitsidianakis         if self.read_pos >= self.read_fifo.len() || self.read_count > self.read_fifo.len() {
50893243319SManos Pitsidianakis             return Err(());
50993243319SManos Pitsidianakis         }
51093243319SManos Pitsidianakis 
51193243319SManos Pitsidianakis         if !self.fifo_enabled() && self.read_count > 0 && self.read_pos > 0 {
51293243319SManos Pitsidianakis             // Older versions of PL011 didn't ensure that the single
51393243319SManos Pitsidianakis             // character in the FIFO in FIFO-disabled mode is in
51493243319SManos Pitsidianakis             // element 0 of the array; convert to follow the current
51593243319SManos Pitsidianakis             // code's assumptions.
51693243319SManos Pitsidianakis             self.read_fifo[0] = self.read_fifo[self.read_pos];
51793243319SManos Pitsidianakis             self.read_pos = 0;
51893243319SManos Pitsidianakis         }
51993243319SManos Pitsidianakis 
52093243319SManos Pitsidianakis         self.ibrd &= IBRD_MASK;
52193243319SManos Pitsidianakis         self.fbrd &= FBRD_MASK;
52293243319SManos Pitsidianakis 
52393243319SManos Pitsidianakis         Ok(())
52493243319SManos Pitsidianakis     }
52537fdb2f5SManos Pitsidianakis }
52637fdb2f5SManos Pitsidianakis 
52737fdb2f5SManos Pitsidianakis /// Which bits in the interrupt status matter for each outbound IRQ line ?
52837fdb2f5SManos Pitsidianakis pub const IRQMASK: [u32; 6] = [
52937fdb2f5SManos Pitsidianakis     /* combined IRQ */
53037fdb2f5SManos Pitsidianakis     Interrupt::E
53137fdb2f5SManos Pitsidianakis         | Interrupt::MS
53237fdb2f5SManos Pitsidianakis         | Interrupt::RT as u32
53337fdb2f5SManos Pitsidianakis         | Interrupt::TX as u32
53437fdb2f5SManos Pitsidianakis         | Interrupt::RX as u32,
53537fdb2f5SManos Pitsidianakis     Interrupt::RX as u32,
53637fdb2f5SManos Pitsidianakis     Interrupt::TX as u32,
53737fdb2f5SManos Pitsidianakis     Interrupt::RT as u32,
53837fdb2f5SManos Pitsidianakis     Interrupt::MS,
53937fdb2f5SManos Pitsidianakis     Interrupt::E,
54037fdb2f5SManos Pitsidianakis ];
54137fdb2f5SManos Pitsidianakis 
54237fdb2f5SManos Pitsidianakis /// # Safety
54337fdb2f5SManos Pitsidianakis ///
54437fdb2f5SManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer, that has
54537fdb2f5SManos Pitsidianakis /// the same size as [`PL011State`]. We also expect the device is
54637fdb2f5SManos Pitsidianakis /// readable/writeable from one thread at any time.
54737fdb2f5SManos Pitsidianakis pub unsafe extern "C" fn pl011_can_receive(opaque: *mut c_void) -> c_int {
54837fdb2f5SManos Pitsidianakis     unsafe {
54937fdb2f5SManos Pitsidianakis         debug_assert!(!opaque.is_null());
55037fdb2f5SManos Pitsidianakis         let state = NonNull::new_unchecked(opaque.cast::<PL011State>());
55137fdb2f5SManos Pitsidianakis         state.as_ref().can_receive().into()
55237fdb2f5SManos Pitsidianakis     }
55337fdb2f5SManos Pitsidianakis }
55437fdb2f5SManos Pitsidianakis 
55537fdb2f5SManos Pitsidianakis /// # Safety
55637fdb2f5SManos Pitsidianakis ///
55737fdb2f5SManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer, that has
55837fdb2f5SManos Pitsidianakis /// the same size as [`PL011State`]. We also expect the device is
55937fdb2f5SManos Pitsidianakis /// readable/writeable from one thread at any time.
56037fdb2f5SManos Pitsidianakis ///
56137fdb2f5SManos Pitsidianakis /// The buffer and size arguments must also be valid.
5629f7d4520SPaolo Bonzini pub unsafe extern "C" fn pl011_receive(opaque: *mut c_void, buf: *const u8, size: c_int) {
56337fdb2f5SManos Pitsidianakis     unsafe {
56437fdb2f5SManos Pitsidianakis         debug_assert!(!opaque.is_null());
56537fdb2f5SManos Pitsidianakis         let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>());
56637fdb2f5SManos Pitsidianakis         if state.as_ref().loopback_enabled() {
56737fdb2f5SManos Pitsidianakis             return;
56837fdb2f5SManos Pitsidianakis         }
56937fdb2f5SManos Pitsidianakis         if size > 0 {
57037fdb2f5SManos Pitsidianakis             debug_assert!(!buf.is_null());
57137fdb2f5SManos Pitsidianakis             state.as_mut().put_fifo(c_uint::from(buf.read_volatile()))
57237fdb2f5SManos Pitsidianakis         }
57337fdb2f5SManos Pitsidianakis     }
57437fdb2f5SManos Pitsidianakis }
57537fdb2f5SManos Pitsidianakis 
57637fdb2f5SManos Pitsidianakis /// # Safety
57737fdb2f5SManos Pitsidianakis ///
57837fdb2f5SManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer, that has
57937fdb2f5SManos Pitsidianakis /// the same size as [`PL011State`]. We also expect the device is
58037fdb2f5SManos Pitsidianakis /// readable/writeable from one thread at any time.
5819f7d4520SPaolo Bonzini pub unsafe extern "C" fn pl011_event(opaque: *mut c_void, event: QEMUChrEvent) {
58237fdb2f5SManos Pitsidianakis     unsafe {
58337fdb2f5SManos Pitsidianakis         debug_assert!(!opaque.is_null());
58437fdb2f5SManos Pitsidianakis         let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>());
58537fdb2f5SManos Pitsidianakis         state.as_mut().event(event)
58637fdb2f5SManos Pitsidianakis     }
58737fdb2f5SManos Pitsidianakis }
58837fdb2f5SManos Pitsidianakis 
58937fdb2f5SManos Pitsidianakis /// # Safety
59037fdb2f5SManos Pitsidianakis ///
59137fdb2f5SManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer for `chr`.
59237fdb2f5SManos Pitsidianakis #[no_mangle]
59337fdb2f5SManos Pitsidianakis pub unsafe extern "C" fn pl011_create(
59437fdb2f5SManos Pitsidianakis     addr: u64,
59537fdb2f5SManos Pitsidianakis     irq: qemu_irq,
59637fdb2f5SManos Pitsidianakis     chr: *mut Chardev,
59737fdb2f5SManos Pitsidianakis ) -> *mut DeviceState {
59837fdb2f5SManos Pitsidianakis     unsafe {
59937fdb2f5SManos Pitsidianakis         let dev: *mut DeviceState = qdev_new(PL011State::TYPE_INFO.name);
60037fdb2f5SManos Pitsidianakis         let sysbus: *mut SysBusDevice = dev.cast::<SysBusDevice>();
60137fdb2f5SManos Pitsidianakis 
602*718e255fSPaolo Bonzini         qdev_prop_set_chr(dev, c_str!("chardev").as_ptr(), chr);
60337fdb2f5SManos Pitsidianakis         sysbus_realize_and_unref(sysbus, addr_of!(error_fatal) as *mut *mut Error);
60437fdb2f5SManos Pitsidianakis         sysbus_mmio_map(sysbus, 0, addr);
60537fdb2f5SManos Pitsidianakis         sysbus_connect_irq(sysbus, 0, irq);
60637fdb2f5SManos Pitsidianakis         dev
60737fdb2f5SManos Pitsidianakis     }
60837fdb2f5SManos Pitsidianakis }
60937fdb2f5SManos Pitsidianakis 
61037fdb2f5SManos Pitsidianakis /// # Safety
61137fdb2f5SManos Pitsidianakis ///
61237fdb2f5SManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer, that has
61337fdb2f5SManos Pitsidianakis /// the same size as [`PL011State`]. We also expect the device is
61437fdb2f5SManos Pitsidianakis /// readable/writeable from one thread at any time.
61537fdb2f5SManos Pitsidianakis pub unsafe extern "C" fn pl011_init(obj: *mut Object) {
61637fdb2f5SManos Pitsidianakis     unsafe {
61737fdb2f5SManos Pitsidianakis         debug_assert!(!obj.is_null());
61837fdb2f5SManos Pitsidianakis         let mut state = NonNull::new_unchecked(obj.cast::<PL011State>());
61937fdb2f5SManos Pitsidianakis         state.as_mut().init();
62037fdb2f5SManos Pitsidianakis     }
62137fdb2f5SManos Pitsidianakis }
6222e06e72dSManos Pitsidianakis 
6232e06e72dSManos Pitsidianakis #[repr(C)]
6242e06e72dSManos Pitsidianakis #[derive(Debug, qemu_api_macros::Object)]
6252e06e72dSManos Pitsidianakis /// PL011 Luminary device model.
6262e06e72dSManos Pitsidianakis pub struct PL011Luminary {
6272e06e72dSManos Pitsidianakis     parent_obj: PL011State,
6282e06e72dSManos Pitsidianakis }
6292e06e72dSManos Pitsidianakis 
6302e06e72dSManos Pitsidianakis #[repr(C)]
6312e06e72dSManos Pitsidianakis pub struct PL011LuminaryClass {
6322e06e72dSManos Pitsidianakis     _inner: [u8; 0],
6332e06e72dSManos Pitsidianakis }
6342e06e72dSManos Pitsidianakis 
6352e06e72dSManos Pitsidianakis /// Initializes a pre-allocated, unitialized instance of `PL011Luminary`.
6362e06e72dSManos Pitsidianakis ///
6372e06e72dSManos Pitsidianakis /// # Safety
6382e06e72dSManos Pitsidianakis ///
6392e06e72dSManos Pitsidianakis /// We expect the FFI user of this function to pass a valid pointer, that has
6402e06e72dSManos Pitsidianakis /// the same size as [`PL011Luminary`]. We also expect the device is
6412e06e72dSManos Pitsidianakis /// readable/writeable from one thread at any time.
6422e06e72dSManos Pitsidianakis pub unsafe extern "C" fn pl011_luminary_init(obj: *mut Object) {
6432e06e72dSManos Pitsidianakis     unsafe {
6442e06e72dSManos Pitsidianakis         debug_assert!(!obj.is_null());
6452e06e72dSManos Pitsidianakis         let mut state = NonNull::new_unchecked(obj.cast::<PL011Luminary>());
6462e06e72dSManos Pitsidianakis         let state = state.as_mut();
6472e06e72dSManos Pitsidianakis         state.parent_obj.device_id = DeviceId::Luminary;
6482e06e72dSManos Pitsidianakis     }
6492e06e72dSManos Pitsidianakis }
6502e06e72dSManos Pitsidianakis 
6512e06e72dSManos Pitsidianakis impl qemu_api::definitions::Class for PL011LuminaryClass {
6529f7d4520SPaolo Bonzini     const CLASS_INIT: Option<unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void)> =
6539f7d4520SPaolo Bonzini         None;
6542e06e72dSManos Pitsidianakis     const CLASS_BASE_INIT: Option<
6559f7d4520SPaolo Bonzini         unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
6562e06e72dSManos Pitsidianakis     > = None;
6572e06e72dSManos Pitsidianakis }
6582e06e72dSManos Pitsidianakis 
6592e06e72dSManos Pitsidianakis impl ObjectImpl for PL011Luminary {
6602e06e72dSManos Pitsidianakis     type Class = PL011LuminaryClass;
6612e06e72dSManos Pitsidianakis     const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
6622e06e72dSManos Pitsidianakis     const TYPE_NAME: &'static CStr = crate::TYPE_PL011_LUMINARY;
6632e06e72dSManos Pitsidianakis     const PARENT_TYPE_NAME: Option<&'static CStr> = Some(crate::TYPE_PL011);
6642e06e72dSManos Pitsidianakis     const ABSTRACT: bool = false;
6652e06e72dSManos Pitsidianakis     const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = Some(pl011_luminary_init);
6662e06e72dSManos Pitsidianakis     const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
6672e06e72dSManos Pitsidianakis     const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
6682e06e72dSManos Pitsidianakis }
669