objc2_paravirtualized_graphics/generated/
PGDevice.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6use objc2_foundation::*;
7#[cfg(feature = "objc2-metal")]
8use objc2_metal::*;
9
10use crate::*;
11
12/// A struct for a guest physical memory range
13/// Field: physicalAddress The starting physical address of the range
14/// Field: physicalLength The length of the range
15///
16/// See also [Apple's documentation](https://developer.apple.com/documentation/paravirtualizedgraphics/pgphysicalmemoryrange_s?language=objc)
17#[repr(C)]
18#[derive(Clone, Copy, Debug, PartialEq)]
19pub struct PGPhysicalMemoryRange_s {
20    pub physicalAddress: u64,
21    pub physicalLength: u64,
22}
23
24unsafe impl Encode for PGPhysicalMemoryRange_s {
25    const ENCODING: Encoding = Encoding::Struct(
26        "PGPhysicalMemoryRange_s",
27        &[<u64>::ENCODING, <u64>::ENCODING],
28    );
29}
30
31unsafe impl RefEncode for PGPhysicalMemoryRange_s {
32    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
33}
34
35/// A struct for a guest physical memory range
36/// Field: physicalAddress The starting physical address of the range
37/// Field: physicalLength The length of the range
38///
39/// See also [Apple's documentation](https://developer.apple.com/documentation/paravirtualizedgraphics/pgphysicalmemoryrange_t?language=objc)
40pub type PGPhysicalMemoryRange_t = PGPhysicalMemoryRange_s;
41
42/// A block that is invoked to raise an interrupt to the guest.
43///
44/// Parameter `vector`: The MSI vector to raise an interrupt on.
45///
46/// See also [Apple's documentation](https://developer.apple.com/documentation/paravirtualizedgraphics/pgraiseinterrupt?language=objc)
47#[cfg(feature = "block2")]
48pub type PGRaiseInterrupt = *mut block2::DynBlock<dyn Fn(u32)>;
49
50/// A block that will be invoked by the device client code when a trace handler fires.  The device client code should watch the memory identified
51/// by each installed trace range and notify the device when the memory has been changed.  The client is encouraged to coalesce the handling of these notifications
52/// over the course of several milliseconds.  This functionality is used to provide a low overhead framebuffer implementation that is used by the device before the guest
53/// OS has fully booted and entered accelerated rendering and display.
54///
55/// Parameter `dirty`: The range of memory that was detected to be dirtied by the guest.
56///
57/// The returned range may be larger than the range that was actually written, for example, if the client can only determine at page granularity the memory that was
58/// affected by guest writes, then it may report a larger region.  Clients are also encouraged to coalesce writes over a period of time into a single notification of activity.
59///
60/// See also [Apple's documentation](https://developer.apple.com/documentation/paravirtualizedgraphics/pgtracerangehandler?language=objc)
61#[cfg(feature = "block2")]
62pub type PGTraceRangeHandler = *mut block2::DynBlock<dyn Fn(NonNull<PGPhysicalMemoryRange_t>)>;
63
64extern_class!(
65    /// [Apple's documentation](https://developer.apple.com/documentation/paravirtualizedgraphics/pgdevicedescriptor?language=objc)
66    #[unsafe(super(NSObject))]
67    #[derive(Debug, PartialEq, Eq, Hash)]
68    pub struct PGDeviceDescriptor;
69);
70
71extern_conformance!(
72    unsafe impl NSObjectProtocol for PGDeviceDescriptor {}
73);
74
75impl PGDeviceDescriptor {
76    extern_methods!(
77        #[cfg(feature = "objc2-metal")]
78        /// The metal device to use to back the PGDevice
79        #[unsafe(method(device))]
80        #[unsafe(method_family = none)]
81        pub unsafe fn device(&self) -> Option<Retained<ProtocolObject<dyn MTLDevice>>>;
82
83        #[cfg(feature = "objc2-metal")]
84        /// Setter for [`device`][Self::device].
85        #[unsafe(method(setDevice:))]
86        #[unsafe(method_family = none)]
87        pub unsafe fn setDevice(&self, device: Option<&ProtocolObject<dyn MTLDevice>>);
88
89        /// The length, of the memory that backs the APPLEGPU_BAR_MMIO
90        ///
91        /// By default, the value of mmioLength will be the recommended default size for
92        /// the MMIO memory.
93        #[unsafe(method(mmioLength))]
94        #[unsafe(method_family = none)]
95        pub unsafe fn mmioLength(&self) -> usize;
96
97        /// Setter for [`mmioLength`][Self::mmioLength].
98        #[unsafe(method(setMmioLength:))]
99        #[unsafe(method_family = none)]
100        pub unsafe fn setMmioLength(&self, mmio_length: usize);
101
102        #[cfg(feature = "block2")]
103        /// The block to invoke to raise an interrupt to the guest.  May be raised from a dispatch queue
104        /// must be thread safe.
105        #[unsafe(method(raiseInterrupt))]
106        #[unsafe(method_family = none)]
107        pub unsafe fn raiseInterrupt(&self) -> PGRaiseInterrupt;
108
109        #[cfg(feature = "block2")]
110        /// Setter for [`raiseInterrupt`][Self::raiseInterrupt].
111        ///
112        /// This is [copied][objc2_foundation::NSCopying::copy] when set.
113        ///
114        /// # Safety
115        ///
116        /// `raise_interrupt` must be a valid pointer or null.
117        #[unsafe(method(setRaiseInterrupt:))]
118        #[unsafe(method_family = none)]
119        pub unsafe fn setRaiseInterrupt(&self, raise_interrupt: PGRaiseInterrupt);
120
121        /// The number of PGDisplay ports configured into the VM.
122        ///
123        /// By default, the value of displayPortCount will be 1.  Valid values range from 1 to the value returned by PGMaxDisplayPortCount().
124        #[unsafe(method(displayPortCount))]
125        #[unsafe(method_family = none)]
126        pub unsafe fn displayPortCount(&self) -> u32;
127
128        /// Setter for [`displayPortCount`][Self::displayPortCount].
129        #[unsafe(method(setDisplayPortCount:))]
130        #[unsafe(method_family = none)]
131        pub unsafe fn setDisplayPortCount(&self, display_port_count: u32);
132    );
133}
134
135/// Methods declared on superclass `NSObject`.
136impl PGDeviceDescriptor {
137    extern_methods!(
138        #[unsafe(method(init))]
139        #[unsafe(method_family = init)]
140        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
141
142        #[unsafe(method(new))]
143        #[unsafe(method_family = new)]
144        pub unsafe fn new() -> Retained<Self>;
145    );
146}
147
148extern_protocol!(
149    /// The PGDevice protocol represents a paravirtualized GPU device.
150    ///
151    /// See also [Apple's documentation](https://developer.apple.com/documentation/paravirtualizedgraphics/pgdevice?language=objc)
152    pub unsafe trait PGDevice: NSObjectProtocol {
153        /// Perform an MMIO read from the device.
154        ///
155        /// Parameter `offset`: The offset into the MMIO BAR to read from.
156        ///
157        /// Returns: The 32-bit value for the read.
158        #[unsafe(method(mmioReadAtOffset:))]
159        #[unsafe(method_family = none)]
160        unsafe fn mmioReadAtOffset(&self, offset: usize) -> u32;
161
162        /// Perform an MMIO write to the device.
163        ///
164        /// Parameter `offset`: The offset into the MMIO bar to write to.
165        ///
166        /// Parameter `value`: The 32-bit value to write to the device.
167        #[unsafe(method(mmioWriteAtOffset:value:))]
168        #[unsafe(method_family = none)]
169        unsafe fn mmioWriteAtOffset_value(&self, offset: usize, value: u32);
170
171        #[cfg(feature = "PGDisplay")]
172        /// Create a display based on the given descriptor and uniquifying parameters.
173        ///
174        /// Parameter `descriptor`: Description of desired display object.
175        ///
176        /// Parameter `port`: Port number that display will plug into (one display per port - and it must be less than the displayPortCount that PGDevice was created with).
177        ///
178        /// Parameter `serialNum`: Serial Number of display (should be unique so Guest compositor can maintain persistent layout of displays on desktop across boots).
179        ///
180        /// New display won't hot-plug until first modeList is established.  Releasing this object emulates a hot-unplug.
181        #[unsafe(method(newDisplayWithDescriptor:port:serialNum:))]
182        #[unsafe(method_family = new)]
183        unsafe fn newDisplayWithDescriptor_port_serialNum(
184            &self,
185            descriptor: &PGDisplayDescriptor,
186            port: NSUInteger,
187            serial_num: u32,
188        ) -> Option<Retained<ProtocolObject<dyn PGDisplay>>>;
189
190        /// Notify the device that it will suspend.  This will quiesce the device but not begin resource serialization. Upon return from this method,
191        /// no new interrupts will be generated by the device.  The device will stop accepting commands from the guest.
192        ///
193        /// Note: Mac guests have several wall-clock time limits on command buffer completion so the guest CPUs need to be halted within a short interval
194        /// after this method is called.
195        #[unsafe(method(willSuspend))]
196        #[unsafe(method_family = none)]
197        unsafe fn willSuspend(&self);
198
199        /// Finish the suspend.  This method may take an arbitrary amount of time as the device needs to complete any GPU work that is in flight.
200        ///
201        /// Returns: The suspend state data.  This data should be serialized and returned to the device on resume.  Returns nil on suspend failure.
202        ///
203        /// Note: It is not legal to perform any further operations on this device after finishSuspend is called, the device must be released.
204        #[unsafe(method(finishSuspend))]
205        // required for soundness, method has `returns_retained` attribute.
206        #[unsafe(method_family = copy)]
207        unsafe fn finishSuspend(&self) -> Option<Retained<NSData>>;
208
209        /// Begin the resume.  This method will set up the device to appear in the same state as it was in before the suspend.  At this point, the guest CPUs should not
210        /// yet be running and no guest memory accesses will be attempted.
211        ///
212        /// Parameter `suspendState`: The suspend state recorded during the finishSuspend:.
213        ///
214        /// Parameter `error`: An error out parameter, will be populated on resume failure.  Error will be in PGResumeErrorDomain and will have an error code from the PGResumeErrorCode enum.
215        ///
216        /// Returns: Returns YES on success.  On failure will return NO and populate error.
217        ///
218        /// Note: Suspended displays should be reattached after this method has been called, but before -[PGDevice didResume:] is invoked.
219        ///
220        /// Note: For resume, this method must be invoked before any calls to `-[PGDevice mmioWriteAtOffset:value:]` or `-[PGDevice mmioReadAtOffset:]` are made.
221        #[unsafe(method(willResumeWithSuspendState:error:_))]
222        #[unsafe(method_family = none)]
223        unsafe fn willResumeWithSuspendState_error(
224            &self,
225            suspend_state: &NSData,
226        ) -> Result<(), Retained<NSError>>;
227
228        /// Complete the resume.  After this method is invoked, new interrupts may be generated by the device.  Guest memory must be accessible at the time this method is invoked.
229        ///
230        /// Note: An initial interrupt may be generated during the duration of this call.
231        #[unsafe(method(didResume))]
232        #[unsafe(method_family = none)]
233        unsafe fn didResume(&self);
234
235        /// Pause the device, completing all pending operations and writes to guest memory.
236        ///
237        /// Note: This is a lighter weight operation than suspend.  Guest CPUs should be suspended while the device is paused.
238        #[unsafe(method(pause))]
239        #[unsafe(method_family = none)]
240        unsafe fn pause(&self);
241
242        /// Resume the device from pause state, allowing operations to continue.
243        ///
244        /// Note: An initial interrupt may be generated during the duration of this call.
245        #[unsafe(method(unpause))]
246        #[unsafe(method_family = none)]
247        unsafe fn unpause(&self);
248
249        /// Stop the device entirely, releasing all the guest memory.
250        ///
251        /// Note: Guest CPUs should be suspended or stopped when this method is called.
252        #[unsafe(method(stop))]
253        #[unsafe(method_family = none)]
254        unsafe fn stop(&self);
255
256        /// Reset the device to a clean state.  This will block until work is quiesced.
257        #[unsafe(method(reset))]
258        #[unsafe(method_family = none)]
259        unsafe fn reset(&self);
260    }
261);
262
263/// Create a new PGDevice implementation object based on the provided descriptor.
264///
265/// Parameter `descriptor`: The device descriptor for the new device.
266#[inline]
267pub unsafe extern "C-unwind" fn PGNewDeviceWithDescriptor(
268    descriptor: &PGDeviceDescriptor,
269) -> Option<Retained<ProtocolObject<dyn PGDevice>>> {
270    extern "C-unwind" {
271        fn PGNewDeviceWithDescriptor(
272            descriptor: &PGDeviceDescriptor,
273        ) -> *mut ProtocolObject<dyn PGDevice>;
274    }
275    let ret = unsafe { PGNewDeviceWithDescriptor(descriptor) };
276    unsafe { Retained::retain_autoreleased(ret) }
277}
278
279/// Create a new PGDevice implementation object based on the provided descriptor.
280///
281/// Parameter `descriptor`: The device descriptor for the new device.
282#[inline]
283pub unsafe extern "C-unwind" fn PGCreateDeviceWithDescriptor(
284    descriptor: &PGDeviceDescriptor,
285) -> Option<Retained<ProtocolObject<dyn PGDevice>>> {
286    extern "C-unwind" {
287        fn PGCreateDeviceWithDescriptor(
288            descriptor: &PGDeviceDescriptor,
289        ) -> *mut ProtocolObject<dyn PGDevice>;
290    }
291    let ret = unsafe { PGCreateDeviceWithDescriptor(descriptor) };
292    unsafe { Retained::from_raw(ret) }
293}
294
295extern "C-unwind" {
296    /// Returns the maximum number of PGDisplay ports that a PGDevice can be configured with.
297    ///
298    /// Note: See PGDeviceDescriptor's displayPortCount property.
299    pub fn PGMaxDisplayPortCount() -> u32;
300}
301
302extern "C" {
303    /// [Apple's documentation](https://developer.apple.com/documentation/paravirtualizedgraphics/pgresumeerrordomain?language=objc)
304    pub static PGResumeErrorDomain: &'static NSErrorDomain;
305}
306
307/// Resume error codes
308///
309/// See also [Apple's documentation](https://developer.apple.com/documentation/paravirtualizedgraphics/pgresumeerrorcode?language=objc)
310// NS_ENUM
311#[repr(transparent)]
312#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
313pub struct PGResumeErrorCode(pub NSUInteger);
314impl PGResumeErrorCode {
315    #[doc(alias = "PGResumeErrorCodeInternalFault")]
316    pub const InternalFault: Self = Self(0);
317    #[doc(alias = "PGResumeErrorCodeInvalidSuspendStateVersion")]
318    pub const InvalidSuspendStateVersion: Self = Self(1);
319    #[doc(alias = "PGResumeErrorCodeInvalidContent")]
320    pub const InvalidContent: Self = Self(2);
321    #[doc(alias = "PGResumeErrorCodeInvalidGuestVersion")]
322    pub const InvalidGuestVersion: Self = Self(3);
323    #[doc(alias = "PGResumeErrorCodeIncompatibleDevice")]
324    pub const IncompatibleDevice: Self = Self(4);
325    #[doc(alias = "PGResumeErrorCodeInvalidDisplayPortCount")]
326    pub const InvalidDisplayPortCount: Self = Self(5);
327}
328
329unsafe impl Encode for PGResumeErrorCode {
330    const ENCODING: Encoding = NSUInteger::ENCODING;
331}
332
333unsafe impl RefEncode for PGResumeErrorCode {
334    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
335}
336
337/// Copy the URL of the option ROM to be used by the device.  The URL will be a local file path to a flat ROM image.  The client code should
338/// pad the ROM image out to a power of 2 size with a zero-filled trailer and present the resulting bytes as read only memory to the PCI option ROM BAR for
339/// the device.
340///
341/// Returns: The URL.
342#[inline]
343pub unsafe extern "C-unwind" fn PGCopyOptionROMURL() -> Retained<NSURL> {
344    extern "C-unwind" {
345        fn PGCopyOptionROMURL() -> *mut NSURL;
346    }
347    let ret = unsafe { PGCopyOptionROMURL() };
348    unsafe { Retained::from_raw(ret) }
349        .expect("function was marked as returning non-null, but actually returned NULL")
350}