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 impl SysBusDevice { 36 /// Return `self` cast to a mutable pointer, for use in calls to C code. 37 const fn as_mut_ptr(&self) -> *mut SysBusDevice { 38 addr_of!(*self) as *mut _ 39 } 40 41 /// Expose an interrupt source outside the device as a qdev GPIO output. 42 /// Note that the ordering of calls to `init_irq` is important, since 43 /// whoever creates the sysbus device will refer to the interrupts with 44 /// a number that corresponds to the order of calls to `init_irq`. 45 pub fn init_irq(&self, irq: &InterruptSource) { 46 assert!(bql_locked()); 47 unsafe { 48 bindings::sysbus_init_irq(self.as_mut_ptr(), irq.as_ptr()); 49 } 50 } 51 } 52