master
rs 121 lines 4.04 KB
Raw
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 use common::Opaque;
10 use hwcore::{prelude::*, IRQState};
11 use qom::prelude::*;
12 pub use system_sys::SysBusDeviceClass;
13 use util::{Error, Result};
14
15 use crate::MemoryRegion;
16
17 /// A safe wrapper around [`system_sys::SysBusDevice`].
18 #[repr(transparent)]
19 #[derive(Debug, common::Wrapper)]
20 pub struct SysBusDevice(Opaque<system_sys::SysBusDevice>);
21
22 unsafe impl Send for SysBusDevice {}
23 unsafe impl Sync for SysBusDevice {}
24
25 unsafe impl ObjectType for SysBusDevice {
26 type Class = SysBusDeviceClass;
27 const TYPE_NAME: &'static CStr =
28 unsafe { CStr::from_bytes_with_nul_unchecked(system_sys::TYPE_SYS_BUS_DEVICE) };
29 }
30
31 qom_isa!(SysBusDevice: DeviceState, Object);
32
33 // TODO: add virtual methods
34 pub trait SysBusDeviceImpl: DeviceImpl + IsA<SysBusDevice> {}
35
36 pub trait SysBusDeviceClassExt {
37 fn class_init<T: SysBusDeviceImpl>(&mut self);
38 }
39
40 impl SysBusDeviceClassExt for SysBusDeviceClass {
41 /// Fill in the virtual methods of `SysBusDeviceClass` based on the
42 /// definitions in the `SysBusDeviceImpl` trait.
43 fn class_init<T: SysBusDeviceImpl>(&mut self) {
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 system_sys::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 system_sys::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_mut_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 system_sys::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 system_sys::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 system_sys::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> {}