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, ptr::addr_of_mut}; 8 9 pub use bindings::{SysBusDevice, SysBusDeviceClass}; 10 11 use crate::{ 12 bindings, 13 cell::bql_locked, 14 irq::{IRQState, InterruptSource}, 15 memory::MemoryRegion, 16 prelude::*, 17 qdev::{DeviceImpl, DeviceState}, 18 qom::Owned, 19 }; 20 21 unsafe impl ObjectType for SysBusDevice { 22 type Class = SysBusDeviceClass; 23 const TYPE_NAME: &'static CStr = 24 unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) }; 25 } 26 qom_isa!(SysBusDevice: DeviceState, Object); 27 28 // TODO: add virtual methods 29 pub trait SysBusDeviceImpl: DeviceImpl + IsA<SysBusDevice> {} 30 31 impl SysBusDeviceClass { 32 /// Fill in the virtual methods of `SysBusDeviceClass` based on the 33 /// definitions in the `SysBusDeviceImpl` trait. 34 pub fn class_init<T: SysBusDeviceImpl>(self: &mut SysBusDeviceClass) { 35 self.parent_class.class_init::<T>(); 36 } 37 } 38 39 /// Trait for methods of [`SysBusDevice`] and its subclasses. 40 pub trait SysBusDeviceMethods: ObjectDeref 41 where 42 Self::Target: IsA<SysBusDevice>, 43 { 44 /// Expose a memory region to the board so that it can give it an address 45 /// in guest memory. Note that the ordering of calls to `init_mmio` is 46 /// important, since whoever creates the sysbus device will refer to the 47 /// region with a number that corresponds to the order of calls to 48 /// `init_mmio`. 49 fn init_mmio(&self, iomem: &MemoryRegion) { 50 assert!(bql_locked()); 51 unsafe { 52 bindings::sysbus_init_mmio(self.as_mut_ptr(), iomem.as_mut_ptr()); 53 } 54 } 55 56 /// Expose an interrupt source outside the device as a qdev GPIO output. 57 /// Note that the ordering of calls to `init_irq` is important, since 58 /// whoever creates the sysbus device will refer to the interrupts with 59 /// a number that corresponds to the order of calls to `init_irq`. 60 fn init_irq(&self, irq: &InterruptSource) { 61 assert!(bql_locked()); 62 unsafe { 63 bindings::sysbus_init_irq(self.as_mut_ptr(), irq.as_ptr()); 64 } 65 } 66 67 // TODO: do we want a type like GuestAddress here? 68 fn mmio_map(&self, id: u32, addr: u64) { 69 assert!(bql_locked()); 70 let id: i32 = id.try_into().unwrap(); 71 unsafe { 72 bindings::sysbus_mmio_map(self.as_mut_ptr(), id, addr); 73 } 74 } 75 76 // Owned<> is used here because sysbus_connect_irq (via 77 // object_property_set_link) adds a reference to the IRQState, 78 // which can prolong its life 79 fn connect_irq(&self, id: u32, irq: &Owned<IRQState>) { 80 assert!(bql_locked()); 81 let id: i32 = id.try_into().unwrap(); 82 let irq: &IRQState = irq; 83 unsafe { 84 bindings::sysbus_connect_irq(self.as_mut_ptr(), id, irq.as_mut_ptr()); 85 } 86 } 87 88 fn sysbus_realize(&self) { 89 // TODO: return an Error 90 assert!(bql_locked()); 91 unsafe { 92 bindings::sysbus_realize(self.as_mut_ptr(), addr_of_mut!(bindings::error_fatal)); 93 } 94 } 95 } 96 97 impl<R: ObjectDeref> SysBusDeviceMethods for R where R::Target: IsA<SysBusDevice> {} 98