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 use std::{ffi::CStr, ptr::addr_of}; 6 7 pub use bindings::{SysBusDevice, SysBusDeviceClass}; 8 9 use crate::{ 10 bindings, 11 cell::bql_locked, 12 irq::InterruptSource, 13 prelude::*, 14 qdev::{DeviceClass, DeviceState}, 15 qom::ClassInitImpl, 16 }; 17 18 unsafe impl ObjectType for SysBusDevice { 19 type Class = SysBusDeviceClass; 20 const TYPE_NAME: &'static CStr = 21 unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) }; 22 } 23 qom_isa!(SysBusDevice: DeviceState, Object); 24 25 // TODO: add SysBusDeviceImpl 26 impl<T> ClassInitImpl<SysBusDeviceClass> for T 27 where 28 T: ClassInitImpl<DeviceClass>, 29 { 30 fn class_init(sdc: &mut SysBusDeviceClass) { 31 <T as ClassInitImpl<DeviceClass>>::class_init(&mut sdc.parent_class); 32 } 33 } 34 35 /// Trait for methods of [`SysBusDevice`] and its subclasses. 36 pub trait SysBusDeviceMethods: ObjectDeref 37 where 38 Self::Target: IsA<SysBusDevice>, 39 { 40 /// Expose a memory region to the board so that it can give it an address 41 /// in guest memory. Note that the ordering of calls to `init_mmio` is 42 /// important, since whoever creates the sysbus device will refer to the 43 /// region with a number that corresponds to the order of calls to 44 /// `init_mmio`. 45 fn init_mmio(&self, iomem: &bindings::MemoryRegion) { 46 assert!(bql_locked()); 47 unsafe { 48 bindings::sysbus_init_mmio(self.as_mut_ptr(), addr_of!(*iomem) as *mut _); 49 } 50 } 51 52 /// Expose an interrupt source outside the device as a qdev GPIO output. 53 /// Note that the ordering of calls to `init_irq` is important, since 54 /// whoever creates the sysbus device will refer to the interrupts with 55 /// a number that corresponds to the order of calls to `init_irq`. 56 fn init_irq(&self, irq: &InterruptSource) { 57 assert!(bql_locked()); 58 unsafe { 59 bindings::sysbus_init_irq(self.as_mut_ptr(), irq.as_ptr()); 60 } 61 } 62 } 63 64 impl<R: ObjectDeref> SysBusDeviceMethods for R where R::Target: IsA<SysBusDevice> {} 65