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