xref: /qemu/rust/hw/char/pl011/src/device.rs (revision 1563f287dc9c4bc6a50d380095e966ac039ac24a)
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 std::{ffi::CStr, mem::size_of};
6 
7 use qemu_api::{
8     chardev::{CharBackend, Chardev, Event},
9     impl_vmstate_forward,
10     irq::{IRQState, InterruptSource},
11     log::Log,
12     log_mask_ln,
13     memory::{hwaddr, MemoryRegion, MemoryRegionOps, MemoryRegionOpsBuilder},
14     prelude::*,
15     qdev::{Clock, ClockEvent, DeviceImpl, DeviceState, Property, ResetType, ResettablePhasesImpl},
16     qom::{ObjectImpl, Owned, ParentField, ParentInit},
17     static_assert,
18     sysbus::{SysBusDevice, SysBusDeviceImpl},
19     uninit_field_mut,
20     vmstate::VMStateDescription,
21 };
22 
23 use crate::{
24     device_class,
25     registers::{self, Interrupt, RegisterOffset},
26 };
27 
28 // TODO: You must disable the UART before any of the control registers are
29 // reprogrammed. When the UART is disabled in the middle of transmission or
30 // reception, it completes the current character before stopping
31 
32 /// Integer Baud Rate Divider, `UARTIBRD`
33 const IBRD_MASK: u32 = 0xffff;
34 
35 /// Fractional Baud Rate Divider, `UARTFBRD`
36 const FBRD_MASK: u32 = 0x3f;
37 
38 /// QEMU sourced constant.
39 pub const PL011_FIFO_DEPTH: u32 = 16;
40 
41 #[derive(Clone, Copy)]
42 struct DeviceId(&'static [u8; 8]);
43 
44 impl std::ops::Index<hwaddr> for DeviceId {
45     type Output = u8;
46 
47     fn index(&self, idx: hwaddr) -> &Self::Output {
48         &self.0[idx as usize]
49     }
50 }
51 
52 // FIFOs use 32-bit indices instead of usize, for compatibility with
53 // the migration stream produced by the C version of this device.
54 #[repr(transparent)]
55 #[derive(Debug, Default)]
56 pub struct Fifo([registers::Data; PL011_FIFO_DEPTH as usize]);
57 impl_vmstate_forward!(Fifo);
58 
59 impl Fifo {
60     const fn len(&self) -> u32 {
61         self.0.len() as u32
62     }
63 }
64 
65 impl std::ops::IndexMut<u32> for Fifo {
66     fn index_mut(&mut self, idx: u32) -> &mut Self::Output {
67         &mut self.0[idx as usize]
68     }
69 }
70 
71 impl std::ops::Index<u32> for Fifo {
72     type Output = registers::Data;
73 
74     fn index(&self, idx: u32) -> &Self::Output {
75         &self.0[idx as usize]
76     }
77 }
78 
79 #[repr(C)]
80 #[derive(Debug, Default)]
81 pub struct PL011Registers {
82     #[doc(alias = "fr")]
83     pub flags: registers::Flags,
84     #[doc(alias = "lcr")]
85     pub line_control: registers::LineControl,
86     #[doc(alias = "rsr")]
87     pub receive_status_error_clear: registers::ReceiveStatusErrorClear,
88     #[doc(alias = "cr")]
89     pub control: registers::Control,
90     pub dmacr: u32,
91     pub int_enabled: Interrupt,
92     pub int_level: Interrupt,
93     pub read_fifo: Fifo,
94     pub ilpr: u32,
95     pub ibrd: u32,
96     pub fbrd: u32,
97     pub ifl: u32,
98     pub read_pos: u32,
99     pub read_count: u32,
100     pub read_trigger: u32,
101 }
102 
103 #[repr(C)]
104 #[derive(qemu_api_macros::Object)]
105 /// PL011 Device Model in QEMU
106 pub struct PL011State {
107     pub parent_obj: ParentField<SysBusDevice>,
108     pub iomem: MemoryRegion,
109     #[doc(alias = "chr")]
110     pub char_backend: CharBackend,
111     pub regs: BqlRefCell<PL011Registers>,
112     /// QEMU interrupts
113     ///
114     /// ```text
115     ///  * sysbus MMIO region 0: device registers
116     ///  * sysbus IRQ 0: `UARTINTR` (combined interrupt line)
117     ///  * sysbus IRQ 1: `UARTRXINTR` (receive FIFO interrupt line)
118     ///  * sysbus IRQ 2: `UARTTXINTR` (transmit FIFO interrupt line)
119     ///  * sysbus IRQ 3: `UARTRTINTR` (receive timeout interrupt line)
120     ///  * sysbus IRQ 4: `UARTMSINTR` (momem status interrupt line)
121     ///  * sysbus IRQ 5: `UARTEINTR` (error interrupt line)
122     /// ```
123     #[doc(alias = "irq")]
124     pub interrupts: [InterruptSource; IRQMASK.len()],
125     #[doc(alias = "clk")]
126     pub clock: Owned<Clock>,
127     #[doc(alias = "migrate_clk")]
128     pub migrate_clock: bool,
129 }
130 
131 // Some C users of this device embed its state struct into their own
132 // structs, so the size of the Rust version must not be any larger
133 // than the size of the C one. If this assert triggers you need to
134 // expand the padding_for_rust[] array in the C PL011State struct.
135 static_assert!(size_of::<PL011State>() <= size_of::<qemu_api::bindings::PL011State>());
136 
137 qom_isa!(PL011State : SysBusDevice, DeviceState, Object);
138 
139 #[repr(C)]
140 pub struct PL011Class {
141     parent_class: <SysBusDevice as ObjectType>::Class,
142     /// The byte string that identifies the device.
143     device_id: DeviceId,
144 }
145 
146 trait PL011Impl: SysBusDeviceImpl + IsA<PL011State> {
147     const DEVICE_ID: DeviceId;
148 }
149 
150 impl PL011Class {
151     fn class_init<T: PL011Impl>(&mut self) {
152         self.device_id = T::DEVICE_ID;
153         self.parent_class.class_init::<T>();
154     }
155 }
156 
157 unsafe impl ObjectType for PL011State {
158     type Class = PL011Class;
159     const TYPE_NAME: &'static CStr = crate::TYPE_PL011;
160 }
161 
162 impl PL011Impl for PL011State {
163     const DEVICE_ID: DeviceId = DeviceId(&[0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1]);
164 }
165 
166 impl ObjectImpl for PL011State {
167     type ParentType = SysBusDevice;
168 
169     const INSTANCE_INIT: Option<unsafe fn(ParentInit<Self>)> = Some(Self::init);
170     const INSTANCE_POST_INIT: Option<fn(&Self)> = Some(Self::post_init);
171     const CLASS_INIT: fn(&mut Self::Class) = Self::Class::class_init::<Self>;
172 }
173 
174 impl DeviceImpl for PL011State {
175     fn properties() -> &'static [Property] {
176         &device_class::PL011_PROPERTIES
177     }
178     fn vmsd() -> Option<&'static VMStateDescription> {
179         Some(&device_class::VMSTATE_PL011)
180     }
181     const REALIZE: Option<fn(&Self) -> qemu_api::Result<()>> = Some(Self::realize);
182 }
183 
184 impl ResettablePhasesImpl for PL011State {
185     const HOLD: Option<fn(&Self, ResetType)> = Some(Self::reset_hold);
186 }
187 
188 impl SysBusDeviceImpl for PL011State {}
189 
190 impl PL011Registers {
191     pub(self) fn read(&mut self, offset: RegisterOffset) -> (bool, u32) {
192         use RegisterOffset::*;
193 
194         let mut update = false;
195         let result = match offset {
196             DR => self.read_data_register(&mut update),
197             RSR => u32::from(self.receive_status_error_clear),
198             FR => u32::from(self.flags),
199             FBRD => self.fbrd,
200             ILPR => self.ilpr,
201             IBRD => self.ibrd,
202             LCR_H => u32::from(self.line_control),
203             CR => u32::from(self.control),
204             FLS => self.ifl,
205             IMSC => u32::from(self.int_enabled),
206             RIS => u32::from(self.int_level),
207             MIS => u32::from(self.int_level & self.int_enabled),
208             ICR => {
209                 // "The UARTICR Register is the interrupt clear register and is write-only"
210                 // Source: ARM DDI 0183G 3.3.13 Interrupt Clear Register, UARTICR
211                 0
212             }
213             DMACR => self.dmacr,
214         };
215         (update, result)
216     }
217 
218     pub(self) fn write(
219         &mut self,
220         offset: RegisterOffset,
221         value: u32,
222         char_backend: &CharBackend,
223     ) -> bool {
224         // eprintln!("write offset {offset} value {value}");
225         use RegisterOffset::*;
226         match offset {
227             DR => return self.write_data_register(value),
228             RSR => {
229                 self.receive_status_error_clear = 0.into();
230             }
231             FR => {
232                 // flag writes are ignored
233             }
234             ILPR => {
235                 self.ilpr = value;
236             }
237             IBRD => {
238                 self.ibrd = value;
239             }
240             FBRD => {
241                 self.fbrd = value;
242             }
243             LCR_H => {
244                 let new_val: registers::LineControl = value.into();
245                 // Reset the FIFO state on FIFO enable or disable
246                 if self.line_control.fifos_enabled() != new_val.fifos_enabled() {
247                     self.reset_rx_fifo();
248                     self.reset_tx_fifo();
249                 }
250                 let update = (self.line_control.send_break() != new_val.send_break()) && {
251                     let break_enable = new_val.send_break();
252                     let _ = char_backend.send_break(break_enable);
253                     self.loopback_break(break_enable)
254                 };
255                 self.line_control = new_val;
256                 self.set_read_trigger();
257                 return update;
258             }
259             CR => {
260                 // ??? Need to implement the enable bit.
261                 self.control = value.into();
262                 return self.loopback_mdmctrl();
263             }
264             FLS => {
265                 self.ifl = value;
266                 self.set_read_trigger();
267             }
268             IMSC => {
269                 self.int_enabled = Interrupt::from(value);
270                 return true;
271             }
272             RIS => {}
273             MIS => {}
274             ICR => {
275                 self.int_level &= !Interrupt::from(value);
276                 return true;
277             }
278             DMACR => {
279                 self.dmacr = value;
280                 if value & 3 > 0 {
281                     log_mask_ln!(Log::Unimp, "pl011: DMA not implemented");
282                 }
283             }
284         }
285         false
286     }
287 
288     fn read_data_register(&mut self, update: &mut bool) -> u32 {
289         self.flags.set_receive_fifo_full(false);
290         let c = self.read_fifo[self.read_pos];
291 
292         if self.read_count > 0 {
293             self.read_count -= 1;
294             self.read_pos = (self.read_pos + 1) & (self.fifo_depth() - 1);
295         }
296         if self.read_count == 0 {
297             self.flags.set_receive_fifo_empty(true);
298         }
299         if self.read_count + 1 == self.read_trigger {
300             self.int_level &= !Interrupt::RX;
301         }
302         self.receive_status_error_clear.set_from_data(c);
303         *update = true;
304         u32::from(c)
305     }
306 
307     fn write_data_register(&mut self, value: u32) -> bool {
308         // interrupts always checked
309         let _ = self.loopback_tx(value.into());
310         self.int_level |= Interrupt::TX;
311         true
312     }
313 
314     #[inline]
315     #[must_use]
316     fn loopback_tx(&mut self, value: registers::Data) -> bool {
317         // Caveat:
318         //
319         // In real hardware, TX loopback happens at the serial-bit level
320         // and then reassembled by the RX logics back into bytes and placed
321         // into the RX fifo. That is, loopback happens after TX fifo.
322         //
323         // Because the real hardware TX fifo is time-drained at the frame
324         // rate governed by the configured serial format, some loopback
325         // bytes in TX fifo may still be able to get into the RX fifo
326         // that could be full at times while being drained at software
327         // pace.
328         //
329         // In such scenario, the RX draining pace is the major factor
330         // deciding which loopback bytes get into the RX fifo, unless
331         // hardware flow-control is enabled.
332         //
333         // For simplicity, the above described is not emulated.
334         self.loopback_enabled() && self.fifo_rx_put(value)
335     }
336 
337     #[must_use]
338     fn loopback_mdmctrl(&mut self) -> bool {
339         if !self.loopback_enabled() {
340             return false;
341         }
342 
343         /*
344          * Loopback software-driven modem control outputs to modem status inputs:
345          *   FR.RI  <= CR.Out2
346          *   FR.DCD <= CR.Out1
347          *   FR.CTS <= CR.RTS
348          *   FR.DSR <= CR.DTR
349          *
350          * The loopback happens immediately even if this call is triggered
351          * by setting only CR.LBE.
352          *
353          * CTS/RTS updates due to enabled hardware flow controls are not
354          * dealt with here.
355          */
356 
357         self.flags.set_ring_indicator(self.control.out_2());
358         self.flags.set_data_carrier_detect(self.control.out_1());
359         self.flags.set_clear_to_send(self.control.request_to_send());
360         self.flags
361             .set_data_set_ready(self.control.data_transmit_ready());
362 
363         // Change interrupts based on updated FR
364         let mut il = self.int_level;
365 
366         il &= !Interrupt::MS;
367 
368         if self.flags.data_set_ready() {
369             il |= Interrupt::DSR;
370         }
371         if self.flags.data_carrier_detect() {
372             il |= Interrupt::DCD;
373         }
374         if self.flags.clear_to_send() {
375             il |= Interrupt::CTS;
376         }
377         if self.flags.ring_indicator() {
378             il |= Interrupt::RI;
379         }
380         self.int_level = il;
381         true
382     }
383 
384     fn loopback_break(&mut self, enable: bool) -> bool {
385         enable && self.loopback_tx(registers::Data::BREAK)
386     }
387 
388     fn set_read_trigger(&mut self) {
389         self.read_trigger = 1;
390     }
391 
392     pub fn reset(&mut self) {
393         self.line_control.reset();
394         self.receive_status_error_clear.reset();
395         self.dmacr = 0;
396         self.int_enabled = 0.into();
397         self.int_level = 0.into();
398         self.ilpr = 0;
399         self.ibrd = 0;
400         self.fbrd = 0;
401         self.read_trigger = 1;
402         self.ifl = 0x12;
403         self.control.reset();
404         self.flags.reset();
405         self.reset_rx_fifo();
406         self.reset_tx_fifo();
407     }
408 
409     pub fn reset_rx_fifo(&mut self) {
410         self.read_count = 0;
411         self.read_pos = 0;
412 
413         // Reset FIFO flags
414         self.flags.set_receive_fifo_full(false);
415         self.flags.set_receive_fifo_empty(true);
416     }
417 
418     pub fn reset_tx_fifo(&mut self) {
419         // Reset FIFO flags
420         self.flags.set_transmit_fifo_full(false);
421         self.flags.set_transmit_fifo_empty(true);
422     }
423 
424     #[inline]
425     pub fn fifo_enabled(&self) -> bool {
426         self.line_control.fifos_enabled() == registers::Mode::FIFO
427     }
428 
429     #[inline]
430     pub fn loopback_enabled(&self) -> bool {
431         self.control.enable_loopback()
432     }
433 
434     #[inline]
435     pub fn fifo_depth(&self) -> u32 {
436         // Note: FIFO depth is expected to be power-of-2
437         if self.fifo_enabled() {
438             return PL011_FIFO_DEPTH;
439         }
440         1
441     }
442 
443     #[must_use]
444     pub fn fifo_rx_put(&mut self, value: registers::Data) -> bool {
445         let depth = self.fifo_depth();
446         assert!(depth > 0);
447         let slot = (self.read_pos + self.read_count) & (depth - 1);
448         self.read_fifo[slot] = value;
449         self.read_count += 1;
450         self.flags.set_receive_fifo_empty(false);
451         if self.read_count == depth {
452             self.flags.set_receive_fifo_full(true);
453         }
454 
455         if self.read_count == self.read_trigger {
456             self.int_level |= Interrupt::RX;
457             return true;
458         }
459         false
460     }
461 
462     pub fn post_load(&mut self) -> Result<(), ()> {
463         /* Sanity-check input state */
464         if self.read_pos >= self.read_fifo.len() || self.read_count > self.read_fifo.len() {
465             return Err(());
466         }
467 
468         if !self.fifo_enabled() && self.read_count > 0 && self.read_pos > 0 {
469             // Older versions of PL011 didn't ensure that the single
470             // character in the FIFO in FIFO-disabled mode is in
471             // element 0 of the array; convert to follow the current
472             // code's assumptions.
473             self.read_fifo[0] = self.read_fifo[self.read_pos];
474             self.read_pos = 0;
475         }
476 
477         self.ibrd &= IBRD_MASK;
478         self.fbrd &= FBRD_MASK;
479 
480         Ok(())
481     }
482 }
483 
484 impl PL011State {
485     /// Initializes a pre-allocated, uninitialized instance of `PL011State`.
486     ///
487     /// # Safety
488     ///
489     /// `self` must point to a correctly sized and aligned location for the
490     /// `PL011State` type. It must not be called more than once on the same
491     /// location/instance. All its fields are expected to hold uninitialized
492     /// values with the sole exception of `parent_obj`.
493     unsafe fn init(mut this: ParentInit<Self>) {
494         static PL011_OPS: MemoryRegionOps<PL011State> = MemoryRegionOpsBuilder::<PL011State>::new()
495             .read(&PL011State::read)
496             .write(&PL011State::write)
497             .native_endian()
498             .impl_sizes(4, 4)
499             .build();
500 
501         // SAFETY: this and this.iomem are guaranteed to be valid at this point
502         MemoryRegion::init_io(
503             &mut uninit_field_mut!(*this, iomem),
504             &PL011_OPS,
505             "pl011",
506             0x1000,
507         );
508 
509         uninit_field_mut!(*this, regs).write(Default::default());
510 
511         let clock = DeviceState::init_clock_in(
512             &mut this,
513             "clk",
514             &Self::clock_update,
515             ClockEvent::ClockUpdate,
516         );
517         uninit_field_mut!(*this, clock).write(clock);
518     }
519 
520     const fn clock_update(&self, _event: ClockEvent) {
521         /* pl011_trace_baudrate_change(s); */
522     }
523 
524     fn post_init(&self) {
525         self.init_mmio(&self.iomem);
526         for irq in self.interrupts.iter() {
527             self.init_irq(irq);
528         }
529     }
530 
531     fn read(&self, offset: hwaddr, _size: u32) -> u64 {
532         match RegisterOffset::try_from(offset) {
533             Err(v) if (0x3f8..0x400).contains(&(v >> 2)) => {
534                 let device_id = self.get_class().device_id;
535                 u64::from(device_id[(offset - 0xfe0) >> 2])
536             }
537             Err(_) => {
538                 log_mask_ln!(Log::GuestError, "PL011State::read: Bad offset {offset}");
539                 0
540             }
541             Ok(field) => {
542                 let (update_irq, result) = self.regs.borrow_mut().read(field);
543                 if update_irq {
544                     self.update();
545                     self.char_backend.accept_input();
546                 }
547                 result.into()
548             }
549         }
550     }
551 
552     fn write(&self, offset: hwaddr, value: u64, _size: u32) {
553         let mut update_irq = false;
554         if let Ok(field) = RegisterOffset::try_from(offset) {
555             // qemu_chr_fe_write_all() calls into the can_receive
556             // callback, so handle writes before entering PL011Registers.
557             if field == RegisterOffset::DR {
558                 // ??? Check if transmitter is enabled.
559                 let ch: [u8; 1] = [value as u8];
560                 // XXX this blocks entire thread. Rewrite to use
561                 // qemu_chr_fe_write and background I/O callbacks
562                 let _ = self.char_backend.write_all(&ch);
563             }
564 
565             update_irq = self
566                 .regs
567                 .borrow_mut()
568                 .write(field, value as u32, &self.char_backend);
569         } else {
570             log_mask_ln!(
571                 Log::GuestError,
572                 "PL011State::write: Bad offset {offset} value {value}"
573             );
574         }
575         if update_irq {
576             self.update();
577         }
578     }
579 
580     fn can_receive(&self) -> u32 {
581         let regs = self.regs.borrow();
582         // trace_pl011_can_receive(s->lcr, s->read_count, r);
583         regs.fifo_depth() - regs.read_count
584     }
585 
586     fn receive(&self, buf: &[u8]) {
587         let mut regs = self.regs.borrow_mut();
588         if regs.loopback_enabled() {
589             // In loopback mode, the RX input signal is internally disconnected
590             // from the entire receiving logics; thus, all inputs are ignored,
591             // and BREAK detection on RX input signal is also not performed.
592             return;
593         }
594 
595         let mut update_irq = false;
596         for &c in buf {
597             let c: u32 = c.into();
598             update_irq |= regs.fifo_rx_put(c.into());
599         }
600 
601         // Release the BqlRefCell before calling self.update()
602         drop(regs);
603         if update_irq {
604             self.update();
605         }
606     }
607 
608     fn event(&self, event: Event) {
609         let mut update_irq = false;
610         let mut regs = self.regs.borrow_mut();
611         if event == Event::CHR_EVENT_BREAK && !regs.loopback_enabled() {
612             update_irq = regs.fifo_rx_put(registers::Data::BREAK);
613         }
614         // Release the BqlRefCell before calling self.update()
615         drop(regs);
616 
617         if update_irq {
618             self.update()
619         }
620     }
621 
622     fn realize(&self) -> qemu_api::Result<()> {
623         self.char_backend
624             .enable_handlers(self, Self::can_receive, Self::receive, Self::event);
625         Ok(())
626     }
627 
628     fn reset_hold(&self, _type: ResetType) {
629         self.regs.borrow_mut().reset();
630     }
631 
632     fn update(&self) {
633         let regs = self.regs.borrow();
634         let flags = regs.int_level & regs.int_enabled;
635         for (irq, i) in self.interrupts.iter().zip(IRQMASK) {
636             irq.set(flags.any_set(i));
637         }
638     }
639 
640     pub fn post_load(&self, _version_id: u32) -> Result<(), ()> {
641         self.regs.borrow_mut().post_load()
642     }
643 }
644 
645 /// Which bits in the interrupt status matter for each outbound IRQ line ?
646 const IRQMASK: [Interrupt; 6] = [
647     Interrupt::all(),
648     Interrupt::RX,
649     Interrupt::TX,
650     Interrupt::RT,
651     Interrupt::MS,
652     Interrupt::E,
653 ];
654 
655 /// # Safety
656 ///
657 /// We expect the FFI user of this function to pass a valid pointer for `chr`
658 /// and `irq`.
659 #[no_mangle]
660 pub unsafe extern "C" fn pl011_create(
661     addr: u64,
662     irq: *mut IRQState,
663     chr: *mut Chardev,
664 ) -> *mut DeviceState {
665     // SAFETY: The callers promise that they have owned references.
666     // They do not gift them to pl011_create, so use `Owned::from`.
667     let irq = unsafe { Owned::<IRQState>::from(&*irq) };
668 
669     let dev = PL011State::new();
670     if !chr.is_null() {
671         let chr = unsafe { Owned::<Chardev>::from(&*chr) };
672         dev.prop_set_chr("chardev", &chr);
673     }
674     dev.sysbus_realize();
675     dev.mmio_map(0, addr);
676     dev.connect_irq(0, &irq);
677 
678     // The pointer is kept alive by the QOM tree; drop the owned ref
679     dev.as_mut_ptr()
680 }
681 
682 #[repr(C)]
683 #[derive(qemu_api_macros::Object)]
684 /// PL011 Luminary device model.
685 pub struct PL011Luminary {
686     parent_obj: ParentField<PL011State>,
687 }
688 
689 qom_isa!(PL011Luminary : PL011State, SysBusDevice, DeviceState, Object);
690 
691 unsafe impl ObjectType for PL011Luminary {
692     type Class = <PL011State as ObjectType>::Class;
693     const TYPE_NAME: &'static CStr = crate::TYPE_PL011_LUMINARY;
694 }
695 
696 impl ObjectImpl for PL011Luminary {
697     type ParentType = PL011State;
698 
699     const CLASS_INIT: fn(&mut Self::Class) = Self::Class::class_init::<Self>;
700 }
701 
702 impl PL011Impl for PL011Luminary {
703     const DEVICE_ID: DeviceId = DeviceId(&[0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1]);
704 }
705 
706 impl DeviceImpl for PL011Luminary {}
707 impl ResettablePhasesImpl for PL011Luminary {}
708 impl SysBusDeviceImpl for PL011Luminary {}
709