1 // Copyright 2024 Red Hat, Inc. 2 // Author(s): Paolo Bonzini <pbonzini@redhat.com> 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 5 //! Bindings to access `sysbus` functionality from Rust. 6 7 use std::ffi::CStr; 8 9 pub use bindings::SysBusDeviceClass; 10 use common::Opaque; 11 use qom::{prelude::*, Owned}; 12 use system::MemoryRegion; 13 use util::{Error, Result}; 14 15 use crate::{ 16 bindings, 17 irq::{IRQState, InterruptSource}, 18 qdev::{DeviceImpl, DeviceState}, 19 }; 20 21 /// A safe wrapper around [`bindings::SysBusDevice`]. 22 #[repr(transparent)] 23 #[derive(Debug, common::Wrapper)] 24 pub struct SysBusDevice(Opaque<bindings::SysBusDevice>); 25 26 unsafe impl Send for SysBusDevice {} 27 unsafe impl Sync for SysBusDevice {} 28 29 unsafe impl ObjectType for SysBusDevice { 30 type Class = SysBusDeviceClass; 31 const TYPE_NAME: &'static CStr = 32 unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) }; 33 } 34 35 qom_isa!(SysBusDevice: DeviceState, Object); 36 37 // TODO: add virtual methods 38 pub trait SysBusDeviceImpl: DeviceImpl + IsA<SysBusDevice> {} 39 40 impl SysBusDeviceClass { 41 /// Fill in the virtual methods of `SysBusDeviceClass` based on the 42 /// definitions in the `SysBusDeviceImpl` trait. 43 pub fn class_init<T: SysBusDeviceImpl>(self: &mut SysBusDeviceClass) { 44 self.parent_class.class_init::<T>(); 45 } 46 } 47 48 /// Trait for methods of [`SysBusDevice`] and its subclasses. 49 pub trait SysBusDeviceMethods: ObjectDeref 50 where 51 Self::Target: IsA<SysBusDevice>, 52 { 53 /// Expose a memory region to the board so that it can give it an address 54 /// in guest memory. Note that the ordering of calls to `init_mmio` is 55 /// important, since whoever creates the sysbus device will refer to the 56 /// region with a number that corresponds to the order of calls to 57 /// `init_mmio`. 58 fn init_mmio(&self, iomem: &MemoryRegion) { 59 assert!(bql::is_locked()); 60 unsafe { 61 bindings::sysbus_init_mmio(self.upcast().as_mut_ptr(), iomem.as_mut_ptr()); 62 } 63 } 64 65 /// Expose an interrupt source outside the device as a qdev GPIO output. 66 /// Note that the ordering of calls to `init_irq` is important, since 67 /// whoever creates the sysbus device will refer to the interrupts with 68 /// a number that corresponds to the order of calls to `init_irq`. 69 fn init_irq(&self, irq: &InterruptSource) { 70 assert!(bql::is_locked()); 71 unsafe { 72 bindings::sysbus_init_irq(self.upcast().as_mut_ptr(), irq.as_ptr()); 73 } 74 } 75 76 // TODO: do we want a type like GuestAddress here? 77 fn mmio_addr(&self, id: u32) -> Option<u64> { 78 assert!(bql::is_locked()); 79 // SAFETY: the BQL ensures that no one else writes to sbd.mmio[], and 80 // the SysBusDevice must be initialized to get an IsA<SysBusDevice>. 81 let sbd = unsafe { *self.upcast().as_ptr() }; 82 let id: usize = id.try_into().unwrap(); 83 if sbd.mmio[id].memory.is_null() { 84 None 85 } else { 86 Some(sbd.mmio[id].addr) 87 } 88 } 89 90 // TODO: do we want a type like GuestAddress here? 91 fn mmio_map(&self, id: u32, addr: u64) { 92 assert!(bql::is_locked()); 93 let id: i32 = id.try_into().unwrap(); 94 unsafe { 95 bindings::sysbus_mmio_map(self.upcast().as_mut_ptr(), id, addr); 96 } 97 } 98 99 // Owned<> is used here because sysbus_connect_irq (via 100 // object_property_set_link) adds a reference to the IRQState, 101 // which can prolong its life 102 fn connect_irq(&self, id: u32, irq: &Owned<IRQState>) { 103 assert!(bql::is_locked()); 104 let id: i32 = id.try_into().unwrap(); 105 let irq: &IRQState = irq; 106 unsafe { 107 bindings::sysbus_connect_irq(self.upcast().as_mut_ptr(), id, irq.as_mut_ptr()); 108 } 109 } 110 111 fn sysbus_realize(&self) -> Result<()> { 112 assert!(bql::is_locked()); 113 unsafe { 114 Error::with_errp(|errp| { 115 bindings::sysbus_realize(self.upcast().as_mut_ptr(), errp); 116 }) 117 } 118 } 119 } 120 121 impl<R: ObjectDeref> SysBusDeviceMethods for R where R::Target: IsA<SysBusDevice> {} 122