xref: /reactos/sdk/lib/drivers/arbiter/arbiter.h (revision fec440d8)
1 /*
2  * PROJECT:     ReactOS Kernel&Driver SDK
3  * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE:     Hardware Resources Arbiter Library
5  * COPYRIGHT:   Copyright 2020 Vadim Galyant <vgal@rambler.ru>
6  */
7 
8 #pragma once
9 
10 #define ARBITER_SIGNATURE  'sbrA'
11 #define TAG_ARBITER        'MbrA'
12 #define TAG_ARB_ALLOCATION 'AbrA'
13 #define TAG_ARB_RANGE      'RbrA'
14 
15 typedef struct _ARBITER_ORDERING
16 {
17     UINT64 Start;
18     UINT64 End;
19 } ARBITER_ORDERING, *PARBITER_ORDERING;
20 
21 typedef struct _ARBITER_ORDERING_LIST
22 {
23     UINT16 Count;
24     UINT16 Maximum;
25     PARBITER_ORDERING Orderings;
26 } ARBITER_ORDERING_LIST, *PARBITER_ORDERING_LIST;
27 
28 typedef struct _ARBITER_ALTERNATIVE
29 {
30     UINT64 Minimum;
31     UINT64 Maximum;
32     UINT32 Length;
33     UINT32 Alignment;
34     INT32 Priority;
35     UINT32 Flags;
36     PIO_RESOURCE_DESCRIPTOR Descriptor;
37     UINT32 Reserved[3];
38 } ARBITER_ALTERNATIVE, *PARBITER_ALTERNATIVE;
39 
40 typedef struct _ARBITER_ALLOCATION_STATE
41 {
42     UINT64 Start;
43     UINT64 End;
44     UINT64 CurrentMinimum;
45     UINT64 CurrentMaximum;
46     PARBITER_LIST_ENTRY Entry;
47     PARBITER_ALTERNATIVE CurrentAlternative;
48     UINT32 AlternativeCount;
49     PARBITER_ALTERNATIVE Alternatives;
50     UINT16 Flags;
51     UCHAR RangeAttributes;
52     UCHAR RangeAvailableAttributes;
53     ULONG_PTR WorkSpace;
54 } ARBITER_ALLOCATION_STATE, *PARBITER_ALLOCATION_STATE;
55 
56 typedef struct _ARBITER_INSTANCE *PARBITER_INSTANCE;
57 
58 typedef NTSTATUS
59 (NTAPI * PARB_UNPACK_REQUIREMENT)(
60     _In_ PIO_RESOURCE_DESCRIPTOR IoDescriptor,
61     _Out_ PUINT64 OutMinimumAddress,
62     _Out_ PUINT64 OutMaximumAddress,
63     _Out_ PUINT32 OutLength,
64     _Out_ PUINT32 OutAlignment
65 );
66 
67 typedef NTSTATUS
68 (NTAPI * PARB_PACK_RESOURCE)(
69     _In_ PIO_RESOURCE_DESCRIPTOR IoDescriptor,
70     _In_ UINT64 Start,
71     _Out_ PCM_PARTIAL_RESOURCE_DESCRIPTOR CmDescriptor
72 );
73 
74 typedef NTSTATUS
75 (NTAPI * PARB_UNPACK_RESOURCE)(
76     _In_ PCM_PARTIAL_RESOURCE_DESCRIPTOR CmDescriptor,
77     _Out_ PUINT64 Start,
78     _Out_ PUINT32 OutLength
79 );
80 
81 typedef INT32
82 (NTAPI * PARB_SCORE_REQUIREMENT)(
83     _In_ PIO_RESOURCE_DESCRIPTOR IoDescriptor
84 );
85 
86 typedef NTSTATUS
87 (NTAPI * PARB_TEST_ALLOCATION)(
88     _In_ PARBITER_INSTANCE Arbiter,
89     _In_ PLIST_ENTRY ArbitrationList
90 );
91 
92 typedef NTSTATUS
93 (NTAPI * PARB_RETEST_ALLOCATION)(
94     _In_ PARBITER_INSTANCE Arbiter,
95     _In_ PLIST_ENTRY ArbitrationList
96 );
97 
98 typedef NTSTATUS
99 (NTAPI * PARB_COMMIT_ALLOCATION)(
100     _In_ PARBITER_INSTANCE Arbiter
101 );
102 
103 typedef NTSTATUS
104 (NTAPI * PARB_ROLLBACK_ALLOCATION)(
105     _In_ PARBITER_INSTANCE Arbiter
106 );
107 
108 typedef NTSTATUS
109 (NTAPI * PARB_BOOT_ALLOCATION)(
110     _In_ PARBITER_INSTANCE Arbiter,
111     _In_ PLIST_ENTRY ArbitrationList
112 );
113 
114 /*  Not correct yet, FIXME! */
115 typedef NTSTATUS
116 (NTAPI * PARB_QUERY_ARBITRATE)(
117     _In_ PARBITER_INSTANCE Arbiter
118 );
119 
120 /*  Not correct yet, FIXME! */
121 typedef NTSTATUS
122 (NTAPI * PARB_QUERY_CONFLICT)(
123     _In_ PARBITER_INSTANCE Arbiter
124 );
125 
126 /*  Not correct yet, FIXME! */
127 typedef NTSTATUS
128 (NTAPI * PARB_ADD_RESERVED)(
129     _In_ PARBITER_INSTANCE Arbiter
130 );
131 
132 /*  Not correct yet, FIXME! */
133 typedef NTSTATUS
134 (NTAPI * PARB_START_ARBITER)(
135     _In_ PARBITER_INSTANCE Arbiter
136 );
137 
138 typedef NTSTATUS
139 (NTAPI * PARB_PREPROCESS_ENTRY)(
140     _In_ PARBITER_INSTANCE Arbiter,
141     _Inout_ PARBITER_ALLOCATION_STATE ArbState
142 );
143 
144 typedef NTSTATUS
145 (NTAPI * PARB_ALLOCATE_ENTRY)(
146     _In_ PARBITER_INSTANCE Arbiter,
147     _Inout_ PARBITER_ALLOCATION_STATE ArbState
148 );
149 
150 typedef BOOLEAN
151 (NTAPI * PARB_GET_NEXT_ALLOCATION_RANGE)(
152     _In_ PARBITER_INSTANCE Arbiter,
153     _Inout_ PARBITER_ALLOCATION_STATE ArbState
154 );
155 
156 typedef BOOLEAN
157 (NTAPI * PARB_FIND_SUITABLE_RANGE)(
158     _In_ PARBITER_INSTANCE Arbiter,
159     _Inout_ PARBITER_ALLOCATION_STATE ArbState
160 );
161 
162 typedef VOID
163 (NTAPI * PARB_ADD_ALLOCATION)(
164     _In_ PARBITER_INSTANCE Arbiter,
165     _Inout_ PARBITER_ALLOCATION_STATE ArbState
166 );
167 
168 typedef VOID
169 (NTAPI * PARB_BACKTRACK_ALLOCATION)(
170     _In_ PARBITER_INSTANCE Arbiter,
171     _Inout_ PARBITER_ALLOCATION_STATE ArbState
172 );
173 
174 /*  Not correct yet, FIXME! */
175 typedef NTSTATUS
176 (NTAPI * PARB_OVERRIDE_CONFLICT)(
177     _In_ PARBITER_INSTANCE Arbiter
178 );
179 
180 typedef struct _ARBITER_INSTANCE
181 {
182     UINT32 Signature;
183     PKEVENT MutexEvent;
184     PCWSTR Name;
185     CM_RESOURCE_TYPE ResourceType;
186     PRTL_RANGE_LIST Allocation;
187     PRTL_RANGE_LIST PossibleAllocation;
188     ARBITER_ORDERING_LIST OrderingList;
189     ARBITER_ORDERING_LIST ReservedList;
190     INT32 ReferenceCount;
191     PARBITER_INTERFACE Interface;
192     UINT32 AllocationStackMaxSize;
193     PARBITER_ALLOCATION_STATE AllocationStack;
194     PARB_UNPACK_REQUIREMENT UnpackRequirement;
195     PARB_PACK_RESOURCE PackResource;
196     PARB_UNPACK_RESOURCE UnpackResource;
197     PARB_SCORE_REQUIREMENT ScoreRequirement;
198     PARB_TEST_ALLOCATION TestAllocation;
199     PARB_RETEST_ALLOCATION RetestAllocation;
200     PARB_COMMIT_ALLOCATION CommitAllocation;
201     PARB_ROLLBACK_ALLOCATION RollbackAllocation;
202     PARB_BOOT_ALLOCATION BootAllocation;
203     PARB_QUERY_ARBITRATE QueryArbitrate; // Not used yet
204     PARB_QUERY_CONFLICT QueryConflict; // Not used yet
205     PARB_ADD_RESERVED AddReserved; // Not used yet
206     PARB_START_ARBITER StartArbiter; // Not used yet
207     PARB_PREPROCESS_ENTRY PreprocessEntry;
208     PARB_ALLOCATE_ENTRY AllocateEntry;
209     PARB_GET_NEXT_ALLOCATION_RANGE GetNextAllocationRange;
210     PARB_FIND_SUITABLE_RANGE FindSuitableRange;
211     PARB_ADD_ALLOCATION AddAllocation;
212     PARB_BACKTRACK_ALLOCATION BacktrackAllocation;
213     PARB_OVERRIDE_CONFLICT OverrideConflict; // Not used yet
214     BOOLEAN TransactionInProgress;
215     PVOID Extension;
216     PDEVICE_OBJECT BusDeviceObject;
217     PVOID ConflictCallbackContext;
218     PVOID ConflictCallback;
219 } ARBITER_INSTANCE, *PARBITER_INSTANCE;
220 
221 typedef NTSTATUS
222 (NTAPI * PARB_TRANSLATE_ORDERING)(
223     _Out_ PIO_RESOURCE_DESCRIPTOR OutIoDescriptor,
224     _In_ PIO_RESOURCE_DESCRIPTOR IoDescriptor
225 );
226 
227 CODE_SEG("PAGE")
228 NTSTATUS
229 NTAPI
230 ArbInitializeArbiterInstance(
231     _Inout_ PARBITER_INSTANCE Arbiter,
232     _In_ PDEVICE_OBJECT BusDeviceObject,
233     _In_ CM_RESOURCE_TYPE ResourceType,
234     _In_ PCWSTR ArbiterName,
235     _In_ PCWSTR OrderName,
236     _In_ PARB_TRANSLATE_ORDERING TranslateOrderingFunction
237 );
238