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