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