1 | /* $Id: acpi.cpp 106344 2024-10-16 09:06:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Advanced Configuration and Power Interface (ACPI) Table generation API.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define LOG_GROUP RTLOGGROUP_ACPI
|
---|
42 | #include <iprt/acpi.h>
|
---|
43 | #include <iprt/file.h>
|
---|
44 | #include <iprt/mem.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 | #include <iprt/formats/acpi-aml.h>
|
---|
48 | #include <iprt/formats/acpi-resources.h>
|
---|
49 |
|
---|
50 |
|
---|
51 | /*********************************************************************************************************************************
|
---|
52 | * Defined Constants And Macros *
|
---|
53 | *********************************************************************************************************************************/
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | /*********************************************************************************************************************************
|
---|
58 | * Structures and Typedefs *
|
---|
59 | *********************************************************************************************************************************/
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Package stack element.
|
---|
63 | */
|
---|
64 | typedef struct RTACPITBLSTACKELEM
|
---|
65 | {
|
---|
66 | /** Pointer to the table buffer memory where the PkgLength object starts. */
|
---|
67 | uint8_t *pbPkgLength;
|
---|
68 | /** Current size of the package in bytes, without the PkgLength object. */
|
---|
69 | uint32_t cbPkg;
|
---|
70 | /** The operator creating the package, UINT8_MAX denotes the special root operator. */
|
---|
71 | uint8_t bOp;
|
---|
72 | } RTACPITBLSTACKELEM;
|
---|
73 | /** Pointer to a package stack element. */
|
---|
74 | typedef RTACPITBLSTACKELEM *PRTACPITBLSTACKELEM;
|
---|
75 | /** Pointer to a const package stack element. */
|
---|
76 | typedef const RTACPITBLSTACKELEM *PCRTACPITBLSTACKELEM;
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * ACPI table generator instance.
|
---|
81 | */
|
---|
82 | typedef struct RTACPITBLINT
|
---|
83 | {
|
---|
84 | /** Pointer to the ACPI table header, needed when finalizing the table. */
|
---|
85 | PACPITBLHDR pHdr;
|
---|
86 | /** Byte buffer holding the actual table. */
|
---|
87 | uint8_t *pbTblBuf;
|
---|
88 | /** Size of the table buffer. */
|
---|
89 | uint32_t cbTblBuf;
|
---|
90 | /** Current offset into the table buffer. */
|
---|
91 | uint32_t offTblBuf;
|
---|
92 | /** Flag whether the table is finalized. */
|
---|
93 | bool fFinalized;
|
---|
94 | /** First error code encountered. */
|
---|
95 | int rcErr;
|
---|
96 | /** Pointer to the package element stack. */
|
---|
97 | PRTACPITBLSTACKELEM paPkgStack;
|
---|
98 | /** Number of elements the package stack can hold. */
|
---|
99 | uint32_t cPkgStackElems;
|
---|
100 | /** Index of the current package in the package stack. */
|
---|
101 | uint32_t idxPkgStackElem;
|
---|
102 | } RTACPITBLINT;
|
---|
103 | /** Pointer to an ACPI table generator instance. */
|
---|
104 | typedef RTACPITBLINT *PRTACPITBLINT;
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * ACPI resource builder instance.
|
---|
109 | */
|
---|
110 | typedef struct RTACPIRESINT
|
---|
111 | {
|
---|
112 | /** Byte buffer holding the resource. */
|
---|
113 | uint8_t *pbResBuf;
|
---|
114 | /** Size of the resource buffer. */
|
---|
115 | size_t cbResBuf;
|
---|
116 | /** Current offset into the resource buffer. */
|
---|
117 | uint32_t offResBuf;
|
---|
118 | /** Flag whether the resource is sealed. */
|
---|
119 | bool fSealed;
|
---|
120 | /** First error code encountered. */
|
---|
121 | int rcErr;
|
---|
122 | } RTACPIRESINT;
|
---|
123 | /** Pointer to an ACPI resource builder instance. */
|
---|
124 | typedef RTACPIRESINT *PRTACPIRESINT;
|
---|
125 |
|
---|
126 |
|
---|
127 | /*********************************************************************************************************************************
|
---|
128 | * Global Variables *
|
---|
129 | *********************************************************************************************************************************/
|
---|
130 |
|
---|
131 |
|
---|
132 | /*********************************************************************************************************************************
|
---|
133 | * Internal Functions *
|
---|
134 | *********************************************************************************************************************************/
|
---|
135 |
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Copies the given string into the given buffer padding the remainder with the given character.
|
---|
139 | *
|
---|
140 | * @param pbId The destination to copy the string to.
|
---|
141 | * @param cbId Size of the buffer in bytes.
|
---|
142 | * @param pszStr The string to copy.
|
---|
143 | * @param chPad The character to pad with.
|
---|
144 | */
|
---|
145 | static void rtAcpiTblCopyStringPadWith(uint8_t *pbId, size_t cbId, const char *pszStr, char chPad)
|
---|
146 | {
|
---|
147 | Assert(strlen(pszStr) <= cbId);
|
---|
148 |
|
---|
149 | uint32_t idx = 0;
|
---|
150 | while (*pszStr != '\0')
|
---|
151 | pbId[idx++] = (uint8_t)*pszStr++;
|
---|
152 |
|
---|
153 | while (idx < cbId)
|
---|
154 | pbId[idx++] = chPad;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Updates the package length of the current package in the stack
|
---|
160 | *
|
---|
161 | * @param pThis The ACPI table instance.
|
---|
162 | * @param cbAdd How many bytes to add to the package length.
|
---|
163 | */
|
---|
164 | DECL_FORCE_INLINE(void) rtAcpiTblUpdatePkgLength(PRTACPITBLINT pThis, uint32_t cbAdd)
|
---|
165 | {
|
---|
166 | PRTACPITBLSTACKELEM pPkgElem = &pThis->paPkgStack[pThis->idxPkgStackElem];
|
---|
167 | pPkgElem->cbPkg += cbAdd;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Ensures there is the given amount of room in the ACPI table buffer returning the pointer.
|
---|
173 | *
|
---|
174 | * @returns The pointer to the free space on success or NULL if out of memory.
|
---|
175 | * @param pThis The ACPI table instance.
|
---|
176 | * @param cbReq Amount of bytes requested.
|
---|
177 | */
|
---|
178 | static uint8_t *rtAcpiTblBufEnsureSpace(PRTACPITBLINT pThis, uint32_t cbReq)
|
---|
179 | {
|
---|
180 | if (RT_LIKELY(pThis->cbTblBuf - pThis->offTblBuf >= cbReq))
|
---|
181 | {
|
---|
182 | uint8_t *pb = &pThis->pbTblBuf[pThis->offTblBuf];
|
---|
183 | pThis->offTblBuf += cbReq;
|
---|
184 | return pb;
|
---|
185 | }
|
---|
186 |
|
---|
187 | uint32_t const cbNew = RT_ALIGN_32(pThis->cbTblBuf + cbReq, _4K);
|
---|
188 | uint8_t *pbNew = (uint8_t *)RTMemRealloc(pThis->pbTblBuf, cbNew);
|
---|
189 | if (RT_UNLIKELY(!pbNew))
|
---|
190 | {
|
---|
191 | pThis->rcErr = VERR_NO_MEMORY;
|
---|
192 | return NULL;
|
---|
193 | }
|
---|
194 |
|
---|
195 | pThis->pbTblBuf = pbNew;
|
---|
196 | pThis->cbTblBuf = cbNew;
|
---|
197 |
|
---|
198 | uint8_t *pb = &pThis->pbTblBuf[pThis->offTblBuf];
|
---|
199 | pThis->offTblBuf += cbReq;
|
---|
200 | return pb;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Appends a new package in the given ACPI table instance package stack.
|
---|
206 | *
|
---|
207 | * @returns IPRT status code.
|
---|
208 | * @retval VERR_NO_MEMORY if allocating additional resources to hold the new package failed.
|
---|
209 | * @param pThis The ACPI table instance.
|
---|
210 | * @param bOp The opcode byte the package starts with (for verification purposes when finalizing the package).
|
---|
211 | * @param pbPkgBuf The Start of the package buffer.
|
---|
212 | */
|
---|
213 | static int rtAcpiTblPkgAppendEx(PRTACPITBLINT pThis, uint8_t bOp, uint8_t *pbPkgBuf)
|
---|
214 | {
|
---|
215 | /* Get a new stack element. */
|
---|
216 | if (pThis->idxPkgStackElem + 1 == pThis->cPkgStackElems)
|
---|
217 | {
|
---|
218 | uint32_t const cPkgElemsNew = pThis->cPkgStackElems + 8;
|
---|
219 | PRTACPITBLSTACKELEM paPkgStackNew = (PRTACPITBLSTACKELEM)RTMemRealloc(pThis->paPkgStack, cPkgElemsNew * sizeof(*paPkgStackNew));
|
---|
220 | if (!paPkgStackNew)
|
---|
221 | {
|
---|
222 | pThis->rcErr = VERR_NO_MEMORY;
|
---|
223 | return VERR_NO_MEMORY;
|
---|
224 | }
|
---|
225 |
|
---|
226 | pThis->paPkgStack = paPkgStackNew;
|
---|
227 | pThis->cPkgStackElems = cPkgElemsNew;
|
---|
228 | }
|
---|
229 |
|
---|
230 | PRTACPITBLSTACKELEM pStackElem = &pThis->paPkgStack[++pThis->idxPkgStackElem];
|
---|
231 | pStackElem->pbPkgLength = pbPkgBuf;
|
---|
232 | pStackElem->cbPkg = 0;
|
---|
233 | pStackElem->bOp = bOp;
|
---|
234 | return VINF_SUCCESS;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Starts a new ACPI package in the given ACPI table instance.
|
---|
240 | *
|
---|
241 | * @returns IPRT status code.
|
---|
242 | * @retval VERR_NO_MEMORY if allocating additional resources to hold the new package failed.
|
---|
243 | * @param pThis The ACPI table instance.
|
---|
244 | * @param bOp The opcode byte identifying the package content.
|
---|
245 | */
|
---|
246 | static int rtAcpiTblPkgStart(PRTACPITBLINT pThis, uint8_t bOp)
|
---|
247 | {
|
---|
248 | /*
|
---|
249 | * Allocate 1 byte for opcode + always 4 bytes for the PkgLength, as we don't know how much we will need upfront.
|
---|
250 | * This will be corrected when the package is finalized.
|
---|
251 | */
|
---|
252 | uint8_t *pbPkg = rtAcpiTblBufEnsureSpace(pThis, 5);
|
---|
253 | if (!pbPkg)
|
---|
254 | {
|
---|
255 | pThis->rcErr = VERR_NO_MEMORY;
|
---|
256 | return VERR_NO_MEMORY;
|
---|
257 | }
|
---|
258 |
|
---|
259 | *pbPkg = bOp;
|
---|
260 | /*
|
---|
261 | * Update the package length of the outer package for the opcode,
|
---|
262 | * the PkgLength object's final length will be added in rtAcpiTblPkgFinish().
|
---|
263 | */
|
---|
264 | rtAcpiTblUpdatePkgLength(pThis, sizeof(bOp));
|
---|
265 | return rtAcpiTblPkgAppendEx(pThis, bOp, pbPkg + 1);
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Starts a new ACPI package in the given ACPI table instance. This is for opcodes prefixed with
|
---|
271 | * ACPI_AML_BYTE_CODE_PREFIX_EXT_O, which will be added automatically.
|
---|
272 | *
|
---|
273 | * @returns IPRT status code.
|
---|
274 | * @retval VERR_NO_MEMORY if allocating additional resources to hold the new package failed.
|
---|
275 | * @param pThis The ACPI table instance.
|
---|
276 | * @param bOp The opcode byte identifying the package content.
|
---|
277 | */
|
---|
278 | static int rtAcpiTblPkgStartExt(PRTACPITBLINT pThis, uint8_t bOp)
|
---|
279 | {
|
---|
280 | /*
|
---|
281 | * Allocate 2 bytes for ExtOpPrefix opcode + always 4 bytes for the PkgLength, as we don't know how much we will need upfront.
|
---|
282 | * This will be corrected when the package is finalized.
|
---|
283 | */
|
---|
284 | uint8_t *pbPkg = rtAcpiTblBufEnsureSpace(pThis, 6);
|
---|
285 | if (!pbPkg)
|
---|
286 | {
|
---|
287 | pThis->rcErr = VERR_NO_MEMORY;
|
---|
288 | return VERR_NO_MEMORY;
|
---|
289 | }
|
---|
290 |
|
---|
291 | pbPkg[0] = ACPI_AML_BYTE_CODE_PREFIX_EXT_OP;
|
---|
292 | pbPkg[1] = bOp;
|
---|
293 |
|
---|
294 | /*
|
---|
295 | * Update the package length of the outer package for the opcode,
|
---|
296 | * the PkgLength object's final length will be added in rtAcpiTblPkgFinish().
|
---|
297 | */
|
---|
298 | rtAcpiTblUpdatePkgLength(pThis, sizeof(uint8_t) + sizeof(bOp));
|
---|
299 | return rtAcpiTblPkgAppendEx(pThis, bOp, pbPkg + 2);
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Finishes the current package on the top of the package stack, setting the
|
---|
305 | * package length accordingly.
|
---|
306 | *
|
---|
307 | * @returns IPRT status code.
|
---|
308 | * @retval VERR_INVALID_STATE if bOp doesn't match the opcode the package was started with (asserted in debug builds).
|
---|
309 | * @retval VERR_BUFFER_OVERFLOW if the package length exceeds what can be encoded in the package length field.
|
---|
310 | * @param pThis The ACPI table instance.
|
---|
311 | * @param bOp The opcode byte identifying the package content the package was started with.
|
---|
312 | */
|
---|
313 | static int rtAcpiTblPkgFinish(PRTACPITBLINT pThis, uint8_t bOp)
|
---|
314 | {
|
---|
315 | /* Ensure the op matches what is current on the top of the stack. */
|
---|
316 | AssertReturn(pThis->paPkgStack[pThis->idxPkgStackElem].bOp == bOp, VERR_INVALID_STATE);
|
---|
317 |
|
---|
318 | /* Pop the topmost stack element from the stack. */
|
---|
319 | PRTACPITBLSTACKELEM pPkgElem = &pThis->paPkgStack[pThis->idxPkgStackElem--];
|
---|
320 |
|
---|
321 | /*
|
---|
322 | * Determine how many bytes we actually need for the PkgLength and re-arrange the ACPI table.
|
---|
323 | *
|
---|
324 | * Note! PkgLength will also include its own length.
|
---|
325 | */
|
---|
326 | uint8_t *pbPkgLength = pPkgElem->pbPkgLength;
|
---|
327 | uint32_t cbThisPkg = pPkgElem->cbPkg;
|
---|
328 | if (cbThisPkg + 1 <= 63)
|
---|
329 | {
|
---|
330 | /* Remove the gap. */
|
---|
331 | memmove(pbPkgLength + 1, pbPkgLength + 4, cbThisPkg);
|
---|
332 | pThis->offTblBuf -= 3;
|
---|
333 |
|
---|
334 | /* PkgLength only consists of the package lead byte. */
|
---|
335 | cbThisPkg += 1;
|
---|
336 | *pbPkgLength = (cbThisPkg & 0x3f);
|
---|
337 | }
|
---|
338 | else if (cbThisPkg + 2 < RT_BIT_32(12))
|
---|
339 | {
|
---|
340 | /* Remove the gap. */
|
---|
341 | memmove(pbPkgLength + 2, pbPkgLength + 4, cbThisPkg);
|
---|
342 | pThis->offTblBuf -= 2;
|
---|
343 |
|
---|
344 | cbThisPkg += 2;
|
---|
345 | pbPkgLength[0] = (1 << 6) | (cbThisPkg & 0xf);
|
---|
346 | pbPkgLength[1] = (cbThisPkg >> 4) & 0xff;
|
---|
347 | }
|
---|
348 | else if (cbThisPkg + 3 < RT_BIT_32(20))
|
---|
349 | {
|
---|
350 | /* Remove the gap. */
|
---|
351 | memmove(pbPkgLength + 3, pbPkgLength + 4, cbThisPkg);
|
---|
352 | pThis->offTblBuf -= 1;
|
---|
353 |
|
---|
354 | cbThisPkg += 3;
|
---|
355 | pbPkgLength[0] = (2 << 6) | (cbThisPkg & 0xf);
|
---|
356 | pbPkgLength[1] = (cbThisPkg >> 4) & 0xff;
|
---|
357 | pbPkgLength[2] = (cbThisPkg >> 12) & 0xff;
|
---|
358 | }
|
---|
359 | else if (cbThisPkg + 4 < RT_BIT_32(28))
|
---|
360 | {
|
---|
361 | cbThisPkg += 4;
|
---|
362 | pbPkgLength[0] = (3 << 6) | (cbThisPkg & 0xf);
|
---|
363 | pbPkgLength[1] = (cbThisPkg >> 4) & 0xff;
|
---|
364 | pbPkgLength[2] = (cbThisPkg >> 12) & 0xff;
|
---|
365 | pbPkgLength[3] = (cbThisPkg >> 20) & 0xff;
|
---|
366 | }
|
---|
367 | else
|
---|
368 | return VERR_BUFFER_OVERFLOW;
|
---|
369 |
|
---|
370 | /* Update the size of the outer package. */
|
---|
371 | pThis->paPkgStack[pThis->idxPkgStackElem].cbPkg += cbThisPkg;
|
---|
372 |
|
---|
373 | return VINF_SUCCESS;
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Appends the given byte to the ACPI table, updating the package length of the current package.
|
---|
379 | *
|
---|
380 | * @param pThis The ACPI table instance.
|
---|
381 | * @param bData The byte data to append.
|
---|
382 | */
|
---|
383 | DECLINLINE(void) rtAcpiTblAppendByte(PRTACPITBLINT pThis, uint8_t bData)
|
---|
384 | {
|
---|
385 | uint8_t *pb = rtAcpiTblBufEnsureSpace(pThis, sizeof(bData));
|
---|
386 | if (pb)
|
---|
387 | {
|
---|
388 | *pb = bData;
|
---|
389 | rtAcpiTblUpdatePkgLength(pThis, sizeof(bData));
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Appends the given date to the ACPI table, updating the package length of the current package.
|
---|
396 | *
|
---|
397 | * @param pThis The ACPI table instance.
|
---|
398 | * @param pvData The data to append.
|
---|
399 | * @param cbData Size of the data in bytes.
|
---|
400 | */
|
---|
401 | DECLINLINE(void) rtAcpiTblAppendData(PRTACPITBLINT pThis, const void *pvData, uint32_t cbData)
|
---|
402 | {
|
---|
403 | uint8_t *pb = rtAcpiTblBufEnsureSpace(pThis, cbData);
|
---|
404 | if (pb)
|
---|
405 | {
|
---|
406 | memcpy(pb, pvData, cbData);
|
---|
407 | rtAcpiTblUpdatePkgLength(pThis, cbData);
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 |
|
---|
412 | /**
|
---|
413 | * Appends the given namestring to the ACPI table, updating the package length of the current package
|
---|
414 | * and padding the name with _ if too short.
|
---|
415 | *
|
---|
416 | * @param pThis The ACPI table instance.
|
---|
417 | * @param pszName The name to append, maximum is 4 bytes (or 5 if \\ is the first character).
|
---|
418 | */
|
---|
419 | DECLINLINE(void) rtAcpiTblAppendNameString(PRTACPITBLINT pThis, const char *pszName)
|
---|
420 | {
|
---|
421 | uint32_t cbName = *pszName == '\\' ? 5 : 4;
|
---|
422 | uint8_t *pb = rtAcpiTblBufEnsureSpace(pThis, cbName);
|
---|
423 | if (pb)
|
---|
424 | {
|
---|
425 | rtAcpiTblCopyStringPadWith(pb, cbName, pszName, '_');
|
---|
426 | rtAcpiTblUpdatePkgLength(pThis, cbName);
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 | RTDECL(uint8_t) RTAcpiChecksumGenerate(const void *pvData, size_t cbData)
|
---|
432 | {
|
---|
433 | uint8_t const *pbSrc = (uint8_t const *)pvData;
|
---|
434 | uint8_t bSum = 0;
|
---|
435 | for (size_t i = 0; i < cbData; ++i)
|
---|
436 | bSum += pbSrc[i];
|
---|
437 |
|
---|
438 | return -bSum;
|
---|
439 | }
|
---|
440 |
|
---|
441 |
|
---|
442 | RTDECL(void) RTAcpiTblHdrChecksumGenerate(PACPITBLHDR pTbl, size_t cbTbl)
|
---|
443 | {
|
---|
444 | pTbl->bChkSum = 0;
|
---|
445 | pTbl->bChkSum = RTAcpiChecksumGenerate(pTbl, cbTbl);
|
---|
446 | }
|
---|
447 |
|
---|
448 |
|
---|
449 | RTDECL(int) RTAcpiTblCreate(PRTACPITBL phAcpiTbl, uint32_t u32TblSig, uint8_t bRevision, const char *pszOemId,
|
---|
450 | const char *pszOemTblId, uint32_t u32OemRevision, const char *pszCreatorId,
|
---|
451 | uint32_t u32CreatorRevision)
|
---|
452 | {
|
---|
453 | AssertPtrReturn(phAcpiTbl, VERR_INVALID_POINTER);
|
---|
454 | AssertPtrReturn(pszOemId, VERR_INVALID_POINTER);
|
---|
455 | AssertPtrReturn(pszOemTblId, VERR_INVALID_POINTER);
|
---|
456 | AssertReturn(strlen(pszOemId) <= 6, VERR_INVALID_PARAMETER);
|
---|
457 | AssertReturn(strlen(pszOemTblId) <= 8, VERR_INVALID_PARAMETER);
|
---|
458 | AssertReturn(!pszCreatorId || strlen(pszCreatorId) <= 4, VERR_INVALID_PARAMETER);
|
---|
459 |
|
---|
460 | PRTACPITBLINT pThis = (PRTACPITBLINT)RTMemAllocZ(sizeof(*pThis));
|
---|
461 | if (pThis)
|
---|
462 | {
|
---|
463 | pThis->pbTblBuf = (uint8_t *)RTMemAlloc(_4K);
|
---|
464 | if (pThis->pbTblBuf)
|
---|
465 | {
|
---|
466 | pThis->pHdr = (PACPITBLHDR)pThis->pbTblBuf;
|
---|
467 | pThis->offTblBuf = sizeof(*pThis->pHdr);
|
---|
468 | pThis->cbTblBuf = _4K;
|
---|
469 | pThis->fFinalized = false;
|
---|
470 | pThis->rcErr = VINF_SUCCESS;
|
---|
471 | pThis->paPkgStack = NULL;
|
---|
472 | pThis->cPkgStackElems = 0;
|
---|
473 | pThis->idxPkgStackElem = 0;
|
---|
474 |
|
---|
475 | /* Add the root stack element for the table, aka DefinitionBlock() in ASL. */
|
---|
476 | uint32_t const cPkgElemsInitial = 8;
|
---|
477 | pThis->paPkgStack = (PRTACPITBLSTACKELEM)RTMemAlloc(cPkgElemsInitial * sizeof(*pThis->paPkgStack));
|
---|
478 | if (pThis->paPkgStack)
|
---|
479 | {
|
---|
480 | pThis->cPkgStackElems = cPkgElemsInitial;
|
---|
481 |
|
---|
482 | PRTACPITBLSTACKELEM pStackElem = &pThis->paPkgStack[pThis->idxPkgStackElem];
|
---|
483 | pStackElem->pbPkgLength = pThis->pbTblBuf; /* Starts with the header. */
|
---|
484 | pStackElem->cbPkg = sizeof(*pThis->pHdr);
|
---|
485 | pStackElem->bOp = UINT8_MAX;
|
---|
486 |
|
---|
487 | /* Init the table header with static things. */
|
---|
488 | pThis->pHdr->u32Signature = u32TblSig;
|
---|
489 | pThis->pHdr->bRevision = bRevision;
|
---|
490 | pThis->pHdr->u32OemRevision = RT_H2LE_U32(u32OemRevision);
|
---|
491 | pThis->pHdr->u32CreatorRevision = RT_H2LE_U32(u32CreatorRevision);
|
---|
492 |
|
---|
493 | rtAcpiTblCopyStringPadWith(&pThis->pHdr->abOemId[0], sizeof(pThis->pHdr->abOemId), pszOemId, ' ');
|
---|
494 | rtAcpiTblCopyStringPadWith(&pThis->pHdr->abOemTblId[0], sizeof(pThis->pHdr->abOemTblId), pszOemTblId, ' ');
|
---|
495 | rtAcpiTblCopyStringPadWith(&pThis->pHdr->abCreatorId[0], sizeof(pThis->pHdr->abCreatorId),
|
---|
496 | pszCreatorId ? pszCreatorId : "IPRT", ' ');
|
---|
497 |
|
---|
498 | *phAcpiTbl = pThis;
|
---|
499 | return VINF_SUCCESS;
|
---|
500 | }
|
---|
501 |
|
---|
502 | RTMemFree(pThis->pbTblBuf);
|
---|
503 | }
|
---|
504 |
|
---|
505 | RTMemFree(pThis);
|
---|
506 | }
|
---|
507 |
|
---|
508 | return VERR_NO_MEMORY;
|
---|
509 | }
|
---|
510 |
|
---|
511 |
|
---|
512 | RTDECL(void) RTAcpiTblDestroy(RTACPITBL hAcpiTbl)
|
---|
513 | {
|
---|
514 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
515 | AssertPtrReturnVoid(pThis);
|
---|
516 |
|
---|
517 | RTMemFree(pThis->paPkgStack);
|
---|
518 | RTMemFree(pThis->pbTblBuf);
|
---|
519 | pThis->pHdr = NULL;
|
---|
520 | pThis->pbTblBuf = NULL;
|
---|
521 | pThis->cbTblBuf = 0;
|
---|
522 | pThis->offTblBuf = 0;
|
---|
523 | pThis->paPkgStack = NULL;
|
---|
524 | pThis->cPkgStackElems = 0;
|
---|
525 | pThis->idxPkgStackElem = 0;
|
---|
526 | RTMemFree(pThis);
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | RTDECL(int) RTAcpiTblFinalize(RTACPITBL hAcpiTbl)
|
---|
531 | {
|
---|
532 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
533 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
534 | AssertRCReturn(pThis->rcErr, pThis->rcErr);
|
---|
535 | AssertReturn(!pThis->fFinalized, VERR_INVALID_PARAMETER);
|
---|
536 | AssertReturn(pThis->idxPkgStackElem == 0, VERR_INVALID_STATE); /** @todo Better status code. */
|
---|
537 | AssertReturn(pThis->paPkgStack[0].bOp == UINT8_MAX, VERR_INVALID_STATE);
|
---|
538 |
|
---|
539 | pThis->pHdr->cbTbl = RT_H2LE_U32(pThis->paPkgStack[0].cbPkg);
|
---|
540 | RTAcpiTblHdrChecksumGenerate(pThis->pHdr, pThis->paPkgStack[0].cbPkg);
|
---|
541 |
|
---|
542 | pThis->fFinalized = true;
|
---|
543 | return VINF_SUCCESS;
|
---|
544 | }
|
---|
545 |
|
---|
546 |
|
---|
547 | RTDECL(uint32_t) RTAcpiTblGetSize(RTACPITBL hAcpiTbl)
|
---|
548 | {
|
---|
549 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
550 | AssertPtrReturn(pThis, 0);
|
---|
551 | AssertRCReturn(pThis->rcErr, 0);
|
---|
552 | AssertReturn(pThis->fFinalized, 0);
|
---|
553 |
|
---|
554 | return pThis->paPkgStack[0].cbPkg;
|
---|
555 | }
|
---|
556 |
|
---|
557 |
|
---|
558 | RTDECL(int) RTAcpiTblDumpToVfsIoStrm(RTACPITBL hAcpiTbl, RTVFSIOSTREAM hVfsIos)
|
---|
559 | {
|
---|
560 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
561 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
562 | AssertRCReturn(pThis->rcErr, 0);
|
---|
563 |
|
---|
564 | return RTVfsIoStrmWrite(hVfsIos, pThis->pbTblBuf, pThis->paPkgStack[0].cbPkg,
|
---|
565 | true /*fBlocking*/, NULL /*pcbWritten*/);
|
---|
566 | }
|
---|
567 |
|
---|
568 |
|
---|
569 | RTDECL(int) RTAcpiTblDumpToFile(RTACPITBL hAcpiTbl, const char *pszFilename)
|
---|
570 | {
|
---|
571 | RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
572 | int rc = RTVfsChainOpenIoStream(pszFilename, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE,
|
---|
573 | &hVfsIos, NULL /*poffError*/, NULL);
|
---|
574 | if (RT_FAILURE(rc))
|
---|
575 | return rc;
|
---|
576 |
|
---|
577 | rc = RTAcpiTblDumpToVfsIoStrm(hAcpiTbl, hVfsIos);
|
---|
578 | RTVfsIoStrmRelease(hVfsIos);
|
---|
579 | return rc;
|
---|
580 | }
|
---|
581 |
|
---|
582 |
|
---|
583 | RTDECL(int) RTAcpiTblScopeFinalize(RTACPITBL hAcpiTbl)
|
---|
584 | {
|
---|
585 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
586 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
587 |
|
---|
588 | return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_OP_SCOPE);
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 | RTDECL(int) RTAcpiTblScopeStart(RTACPITBL hAcpiTbl, const char *pszName)
|
---|
593 | {
|
---|
594 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
595 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
596 |
|
---|
597 | rtAcpiTblPkgStart(pThis, ACPI_AML_BYTE_CODE_OP_SCOPE);
|
---|
598 | rtAcpiTblAppendNameString(pThis, pszName);
|
---|
599 | return pThis->rcErr;
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | RTDECL(int) RTAcpiTblPackageStart(RTACPITBL hAcpiTbl, uint8_t cElements)
|
---|
604 | {
|
---|
605 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
606 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
607 |
|
---|
608 | rtAcpiTblPkgStart(pThis, ACPI_AML_BYTE_CODE_OP_PACKAGE);
|
---|
609 | rtAcpiTblAppendByte(pThis, cElements);
|
---|
610 | return pThis->rcErr;
|
---|
611 | }
|
---|
612 |
|
---|
613 |
|
---|
614 | RTDECL(int) RTAcpiTblPackageFinalize(RTACPITBL hAcpiTbl)
|
---|
615 | {
|
---|
616 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
617 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
618 |
|
---|
619 | return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_OP_PACKAGE);
|
---|
620 | }
|
---|
621 |
|
---|
622 |
|
---|
623 | RTDECL(int) RTAcpiTblDeviceStart(RTACPITBL hAcpiTbl, const char *pszName)
|
---|
624 | {
|
---|
625 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
626 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
627 |
|
---|
628 | rtAcpiTblPkgStartExt(pThis, ACPI_AML_BYTE_CODE_EXT_OP_DEVICE);
|
---|
629 | rtAcpiTblAppendNameString(pThis, pszName);
|
---|
630 | return pThis->rcErr;
|
---|
631 | }
|
---|
632 |
|
---|
633 |
|
---|
634 | RTDECL(int) RTAcpiTblDeviceStartF(RTACPITBL hAcpiTbl, const char *pszNameFmt, ...)
|
---|
635 | {
|
---|
636 | va_list va;
|
---|
637 | va_start(va, pszNameFmt);
|
---|
638 | int rc = RTAcpiTblDeviceStartV(hAcpiTbl, pszNameFmt, va);
|
---|
639 | va_end(va);
|
---|
640 | return rc;
|
---|
641 | }
|
---|
642 |
|
---|
643 |
|
---|
644 | RTDECL(int) RTAcpiTblDeviceStartV(RTACPITBL hAcpiTbl, const char *pszNameFmt, va_list va)
|
---|
645 | {
|
---|
646 | char szName[5];
|
---|
647 | ssize_t cch = RTStrPrintf2V(&szName[0], sizeof(szName), pszNameFmt, va);
|
---|
648 | if (cch <= 0)
|
---|
649 | return VERR_BUFFER_OVERFLOW;
|
---|
650 |
|
---|
651 | return RTAcpiTblDeviceStart(hAcpiTbl, &szName[0]);
|
---|
652 | }
|
---|
653 |
|
---|
654 |
|
---|
655 | RTDECL(int) RTAcpiTblDeviceFinalize(RTACPITBL hAcpiTbl)
|
---|
656 | {
|
---|
657 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
658 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
659 |
|
---|
660 | return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_EXT_OP_DEVICE);
|
---|
661 | }
|
---|
662 |
|
---|
663 |
|
---|
664 | RTDECL(int) RTAcpiTblMethodStart(RTACPITBL hAcpiTbl, const char *pszName, uint8_t cArgs, uint32_t fFlags, uint8_t uSyncLvl)
|
---|
665 | {
|
---|
666 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
667 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
668 | AssertReturn(cArgs < 8, VERR_INVALID_PARAMETER);
|
---|
669 | AssertReturn(uSyncLvl < 0x10, VERR_INVALID_PARAMETER);
|
---|
670 |
|
---|
671 | rtAcpiTblPkgStart(pThis, ACPI_AML_BYTE_CODE_OP_METHOD);
|
---|
672 | rtAcpiTblAppendNameString(pThis, pszName);
|
---|
673 |
|
---|
674 | uint8_t bFlags = cArgs;
|
---|
675 | bFlags |= fFlags & RTACPI_METHOD_F_SERIALIZED ? RT_BIT(3) : 0;
|
---|
676 | bFlags |= uSyncLvl << 4;
|
---|
677 |
|
---|
678 | rtAcpiTblAppendByte(pThis, bFlags);
|
---|
679 | return pThis->rcErr;
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | RTDECL(int) RTAcpiTblMethodFinalize(RTACPITBL hAcpiTbl)
|
---|
684 | {
|
---|
685 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
686 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
687 |
|
---|
688 | return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_OP_METHOD);
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | RTDECL(int) RTAcpiTblNameAppend(RTACPITBL hAcpiTbl, const char *pszName)
|
---|
693 | {
|
---|
694 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
695 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
696 |
|
---|
697 | rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_OP_NAME);
|
---|
698 | rtAcpiTblAppendNameString(pThis, pszName);
|
---|
699 | return pThis->rcErr;
|
---|
700 | }
|
---|
701 |
|
---|
702 |
|
---|
703 | RTDECL(int) RTAcpiTblStringAppend(RTACPITBL hAcpiTbl, const char *psz)
|
---|
704 | {
|
---|
705 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
706 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
707 |
|
---|
708 | rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_PREFIX_STRING);
|
---|
709 | rtAcpiTblAppendData(pThis, psz, (uint32_t)strlen(psz) + 1);
|
---|
710 | return pThis->rcErr;
|
---|
711 | }
|
---|
712 |
|
---|
713 |
|
---|
714 | RTDECL(int) RTAcpiTblIntegerAppend(RTACPITBL hAcpiTbl, uint64_t u64)
|
---|
715 | {
|
---|
716 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
717 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
718 |
|
---|
719 | if (!u64)
|
---|
720 | rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_OP_ZERO);
|
---|
721 | else if (u64 == 1)
|
---|
722 | rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_OP_ONE);
|
---|
723 | else if (u64 <= UINT8_MAX)
|
---|
724 | {
|
---|
725 | rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_PREFIX_BYTE);
|
---|
726 | rtAcpiTblAppendByte(pThis, (uint8_t)u64);
|
---|
727 | }
|
---|
728 | else if (u64 <= UINT16_MAX)
|
---|
729 | {
|
---|
730 | rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_PREFIX_WORD);
|
---|
731 | rtAcpiTblAppendByte(pThis, (uint8_t)u64);
|
---|
732 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 8));
|
---|
733 | }
|
---|
734 | else if (u64 <= UINT32_MAX)
|
---|
735 | {
|
---|
736 | rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_PREFIX_DWORD);
|
---|
737 | rtAcpiTblAppendByte(pThis, (uint8_t)u64);
|
---|
738 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 8));
|
---|
739 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 16));
|
---|
740 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 24));
|
---|
741 | }
|
---|
742 | else
|
---|
743 | {
|
---|
744 | rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_PREFIX_QWORD);
|
---|
745 | rtAcpiTblAppendByte(pThis, (uint8_t)u64);
|
---|
746 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 8));
|
---|
747 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 16));
|
---|
748 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 24));
|
---|
749 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 32));
|
---|
750 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 40));
|
---|
751 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 48));
|
---|
752 | rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 56));
|
---|
753 | }
|
---|
754 | return pThis->rcErr;
|
---|
755 | }
|
---|
756 |
|
---|
757 |
|
---|
758 | RTDECL(int) RTAcpiTblBufferAppend(RTACPITBL hAcpiTbl, const void *pvBuf, size_t cbBuf)
|
---|
759 | {
|
---|
760 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
761 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
762 | AssertReturn(!cbBuf || RT_VALID_PTR(pvBuf), VERR_INVALID_PARAMETER);
|
---|
763 | AssertReturn(cbBuf <= UINT32_MAX, VERR_BUFFER_OVERFLOW);
|
---|
764 |
|
---|
765 | rtAcpiTblPkgStart(pThis, ACPI_AML_BYTE_CODE_OP_BUFFER);
|
---|
766 | RTAcpiTblIntegerAppend(hAcpiTbl, cbBuf);
|
---|
767 | if (pvBuf)
|
---|
768 | rtAcpiTblAppendData(pThis, pvBuf, (uint32_t)cbBuf);
|
---|
769 | return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_OP_BUFFER);
|
---|
770 | }
|
---|
771 |
|
---|
772 |
|
---|
773 | RTDECL(int) RTAcpiTblResourceAppend(RTACPITBL hAcpiTbl, RTACPIRES hAcpiRes)
|
---|
774 | {
|
---|
775 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
776 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
777 | AssertRCReturn(pThis->rcErr, pThis->rcErr);
|
---|
778 |
|
---|
779 | const void *pvRes = NULL;
|
---|
780 | size_t cbRes = 0;
|
---|
781 | int rc = RTAcpiResourceQueryBuffer(hAcpiRes, &pvRes, &cbRes);
|
---|
782 | if (RT_SUCCESS(rc))
|
---|
783 | rc = RTAcpiTblBufferAppend(pThis, pvRes, cbRes);
|
---|
784 |
|
---|
785 | return rc;
|
---|
786 | }
|
---|
787 |
|
---|
788 |
|
---|
789 | RTDECL(int) RTAcpiTblStmtSimpleAppend(RTACPITBL hAcpiTbl, RTACPISTMT enmStmt)
|
---|
790 | {
|
---|
791 | PRTACPITBLINT pThis = hAcpiTbl;
|
---|
792 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
793 |
|
---|
794 | uint8_t bOp;
|
---|
795 | switch (enmStmt)
|
---|
796 | {
|
---|
797 | case kAcpiStmt_Return: bOp = ACPI_AML_BYTE_CODE_OP_RETURN; break;
|
---|
798 | case kAcpiStmt_Breakpoint: bOp = ACPI_AML_BYTE_CODE_OP_BREAK_POINT; break;
|
---|
799 | case kAcpiStmt_Nop: bOp = ACPI_AML_BYTE_CODE_OP_NOOP; break;
|
---|
800 | case kAcpiStmt_Break: bOp = ACPI_AML_BYTE_CODE_OP_BREAK; break;
|
---|
801 | case kAcpiStmt_Continue: bOp = ACPI_AML_BYTE_CODE_OP_CONTINUE; break;
|
---|
802 | default:
|
---|
803 | AssertFailedReturn(VERR_INVALID_PARAMETER);
|
---|
804 | }
|
---|
805 | rtAcpiTblAppendByte(pThis, bOp);
|
---|
806 | return pThis->rcErr;
|
---|
807 | }
|
---|
808 |
|
---|
809 |
|
---|
810 | /**
|
---|
811 | * Ensures there is at least the given amount of space in the given ACPI resource.
|
---|
812 | *
|
---|
813 | * @returns Pointer to the free buffer space or NULL if out of memory.
|
---|
814 | * @param pThis The ACPI resource instance.
|
---|
815 | * @param cbReq Number of free bytes required.
|
---|
816 | */
|
---|
817 | static uint8_t *rtAcpiResBufEnsureSpace(PRTACPIRESINT pThis, uint32_t cbReq)
|
---|
818 | {
|
---|
819 | if (RT_LIKELY(pThis->cbResBuf - pThis->offResBuf >= cbReq))
|
---|
820 | {
|
---|
821 | uint8_t *pb = &pThis->pbResBuf[pThis->offResBuf];
|
---|
822 | pThis->offResBuf += cbReq;
|
---|
823 | return pb;
|
---|
824 | }
|
---|
825 |
|
---|
826 | size_t const cbNew = RT_ALIGN_Z(pThis->cbResBuf + cbReq, _4K);
|
---|
827 | uint8_t *pbNew = (uint8_t *)RTMemRealloc(pThis->pbResBuf, cbNew);
|
---|
828 | if (RT_UNLIKELY(!pbNew))
|
---|
829 | {
|
---|
830 | pThis->rcErr = VERR_NO_MEMORY;
|
---|
831 | return NULL;
|
---|
832 | }
|
---|
833 |
|
---|
834 | pThis->pbResBuf = pbNew;
|
---|
835 | pThis->cbResBuf = cbNew;
|
---|
836 |
|
---|
837 | uint8_t *pb = &pThis->pbResBuf[pThis->offResBuf];
|
---|
838 | pThis->offResBuf += cbReq;
|
---|
839 | return pb;
|
---|
840 | }
|
---|
841 |
|
---|
842 |
|
---|
843 | /**
|
---|
844 | * Encodes an ACPI 16-bit integer in the given byte buffer.
|
---|
845 | *
|
---|
846 | * @returns Pointer to after the encoded integer.
|
---|
847 | * @param pb Where to encode the integer into.
|
---|
848 | * @param u16 The 16-bit unsigned integere to encode.
|
---|
849 | */
|
---|
850 | DECLINLINE(uint8_t *) rtAcpiResEncode16BitInteger(uint8_t *pb, uint16_t u16)
|
---|
851 | {
|
---|
852 | *pb++ = (uint8_t)u16;
|
---|
853 | *pb++ = (uint8_t)(u16 >> 8);
|
---|
854 | return pb;
|
---|
855 | }
|
---|
856 |
|
---|
857 |
|
---|
858 | /**
|
---|
859 | * Encodes an ACPI 32-bit integer in the given byte buffer.
|
---|
860 | *
|
---|
861 | * @returns Pointer to after the encoded integer.
|
---|
862 | * @param pb Where to encode the integer into.
|
---|
863 | * @param u32 The 32-bit unsigned integere to encode.
|
---|
864 | */
|
---|
865 | DECLINLINE(uint8_t *) rtAcpiResEncode32BitInteger(uint8_t *pb, uint32_t u32)
|
---|
866 | {
|
---|
867 | *pb++ = (uint8_t)u32;
|
---|
868 | *pb++ = (uint8_t)(u32 >> 8);
|
---|
869 | *pb++ = (uint8_t)(u32 >> 16);
|
---|
870 | *pb++ = (uint8_t)(u32 >> 24);
|
---|
871 | return pb;
|
---|
872 | }
|
---|
873 |
|
---|
874 | /**
|
---|
875 | * Encodes an ACPI 64-bit integer in the given byte buffer.
|
---|
876 | *
|
---|
877 | * @returns Pointer to after the encoded integer.
|
---|
878 | * @param pb Where to encode the integer into.
|
---|
879 | * @param u64 The 64-bit unsigned integere to encode.
|
---|
880 | */
|
---|
881 |
|
---|
882 | DECLINLINE(uint8_t *) rtAcpiResEncode64BitInteger(uint8_t *pb, uint64_t u64)
|
---|
883 | {
|
---|
884 | *pb++ = (uint8_t)u64;
|
---|
885 | *pb++ = (uint8_t)(u64 >> 8);
|
---|
886 | *pb++ = (uint8_t)(u64 >> 16);
|
---|
887 | *pb++ = (uint8_t)(u64 >> 24);
|
---|
888 | *pb++ = (uint8_t)(u64 >> 32);
|
---|
889 | *pb++ = (uint8_t)(u64 >> 40);
|
---|
890 | *pb++ = (uint8_t)(u64 >> 48);
|
---|
891 | *pb++ = (uint8_t)(u64 >> 56);
|
---|
892 | return pb;
|
---|
893 | }
|
---|
894 |
|
---|
895 |
|
---|
896 | RTDECL(int) RTAcpiResourceCreate(PRTACPIRES phAcpiRes)
|
---|
897 | {
|
---|
898 | AssertPtrReturn(phAcpiRes, VERR_INVALID_POINTER);
|
---|
899 |
|
---|
900 | PRTACPIRESINT pThis = (PRTACPIRESINT)RTMemAllocZ(sizeof(*pThis));
|
---|
901 | if (pThis)
|
---|
902 | {
|
---|
903 | pThis->pbResBuf = (uint8_t *)RTMemAlloc(64);
|
---|
904 | if (pThis->pbResBuf)
|
---|
905 | {
|
---|
906 | pThis->offResBuf = 0;
|
---|
907 | pThis->cbResBuf = 64;
|
---|
908 | pThis->fSealed = false;
|
---|
909 | pThis->rcErr = VINF_SUCCESS;
|
---|
910 |
|
---|
911 | *phAcpiRes = pThis;
|
---|
912 | return VINF_SUCCESS;
|
---|
913 | }
|
---|
914 |
|
---|
915 | RTMemFree(pThis);
|
---|
916 | }
|
---|
917 |
|
---|
918 | return VERR_NO_MEMORY;
|
---|
919 | }
|
---|
920 |
|
---|
921 |
|
---|
922 | RTDECL(void) RTAcpiResourceDestroy(RTACPIRES hAcpiRes)
|
---|
923 | {
|
---|
924 | PRTACPIRESINT pThis = hAcpiRes;
|
---|
925 | AssertPtrReturnVoid(pThis);
|
---|
926 |
|
---|
927 | RTMemFree(pThis->pbResBuf);
|
---|
928 | pThis->pbResBuf = NULL;
|
---|
929 | pThis->cbResBuf = 0;
|
---|
930 | pThis->offResBuf = 0;
|
---|
931 | RTMemFree(pThis);
|
---|
932 | }
|
---|
933 |
|
---|
934 |
|
---|
935 | RTDECL(void) RTAcpiResourceReset(RTACPIRES hAcpiRes)
|
---|
936 | {
|
---|
937 | PRTACPIRESINT pThis = hAcpiRes;
|
---|
938 | AssertPtrReturnVoid(pThis);
|
---|
939 |
|
---|
940 | pThis->offResBuf = 0;
|
---|
941 | pThis->fSealed = false;
|
---|
942 | pThis->rcErr = VINF_SUCCESS;
|
---|
943 | }
|
---|
944 |
|
---|
945 |
|
---|
946 | RTDECL(int) RTAcpiResourceSeal(RTACPIRES hAcpiRes)
|
---|
947 | {
|
---|
948 | PRTACPIRESINT pThis = hAcpiRes;
|
---|
949 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
950 | AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
|
---|
951 | AssertRCReturn(pThis->rcErr, pThis->rcErr);
|
---|
952 |
|
---|
953 | /* Add the end tag. */
|
---|
954 | uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 2);
|
---|
955 | if (!pb)
|
---|
956 | return VERR_NO_MEMORY;
|
---|
957 |
|
---|
958 | *pb++ = ACPI_RSRCS_TAG_END;
|
---|
959 | /*
|
---|
960 | * Generate checksum, we could just write 0 here which will be treated as checksum operation succeeded,
|
---|
961 | * but having this might catch some bugs.
|
---|
962 | *
|
---|
963 | * Checksum algorithm is the same as with the ACPI tables.
|
---|
964 | */
|
---|
965 | *pb = RTAcpiChecksumGenerate(pThis->pbResBuf, pThis->offResBuf - 1); /* Exclude the checksum field. */
|
---|
966 |
|
---|
967 | pThis->fSealed = true;
|
---|
968 | return VINF_SUCCESS;
|
---|
969 | }
|
---|
970 |
|
---|
971 |
|
---|
972 | RTDECL(int) RTAcpiResourceQueryBuffer(RTACPIRES hAcpiRes, const void **ppvRes, size_t *pcbRes)
|
---|
973 | {
|
---|
974 | PRTACPIRESINT pThis = hAcpiRes;
|
---|
975 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
976 | AssertReturn(pThis->fSealed, VERR_INVALID_STATE);
|
---|
977 | AssertRCReturn(pThis->rcErr, pThis->rcErr);
|
---|
978 |
|
---|
979 | *ppvRes = pThis->pbResBuf;
|
---|
980 | *pcbRes = pThis->offResBuf;
|
---|
981 | return VINF_SUCCESS;
|
---|
982 | }
|
---|
983 |
|
---|
984 |
|
---|
985 | RTDECL(int) RTAcpiResourceAdd32BitFixedMemoryRange(RTACPIRES hAcpiRes, uint32_t u32AddrBase, uint32_t cbRange,
|
---|
986 | bool fRw)
|
---|
987 | {
|
---|
988 | PRTACPIRESINT pThis = hAcpiRes;
|
---|
989 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
990 | AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
|
---|
991 | AssertRCReturn(pThis->rcErr, pThis->rcErr);
|
---|
992 |
|
---|
993 | uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 12);
|
---|
994 | if (!pb)
|
---|
995 | return VERR_NO_MEMORY;
|
---|
996 |
|
---|
997 | pb[0] = ACPI_RSRCS_LARGE_TYPE | ACPI_RSRCS_ITEM_32BIT_FIXED_MEMORY_RANGE; /* Tag */
|
---|
998 | pb[1] = 9; /* Length[7:0] */
|
---|
999 | pb[2] = 0; /* Length[15:8] */
|
---|
1000 | pb[3] = fRw ? 1 : 0; /* Information */
|
---|
1001 | rtAcpiResEncode32BitInteger(&pb[4], u32AddrBase);
|
---|
1002 | rtAcpiResEncode32BitInteger(&pb[8], cbRange);
|
---|
1003 | return VINF_SUCCESS;
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 |
|
---|
1007 | RTDECL(int) RTAcpiResourceAddExtendedInterrupt(RTACPIRES hAcpiRes, bool fConsumer, bool fEdgeTriggered, bool fActiveLow, bool fShared,
|
---|
1008 | bool fWakeCapable, uint8_t cIntrs, uint32_t *pau32Intrs)
|
---|
1009 | {
|
---|
1010 | PRTACPIRESINT pThis = hAcpiRes;
|
---|
1011 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1012 | AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
|
---|
1013 | AssertRCReturn(pThis->rcErr, pThis->rcErr);
|
---|
1014 |
|
---|
1015 | uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 3 + 2 + cIntrs * sizeof(uint32_t));
|
---|
1016 | if (!pb)
|
---|
1017 | return VERR_NO_MEMORY;
|
---|
1018 |
|
---|
1019 | pb[0] = ACPI_RSRCS_LARGE_TYPE | ACPI_RSRCS_ITEM_EXTENDED_INTERRUPT; /* Tag */
|
---|
1020 | rtAcpiResEncode16BitInteger(&pb[1], 2 + cIntrs * sizeof(uint32_t)); /* Length[15:0] */
|
---|
1021 | pb[3] = (fConsumer ? ACPI_RSRCS_EXT_INTR_VEC_F_CONSUMER : ACPI_RSRCS_EXT_INTR_VEC_F_PRODUCER)
|
---|
1022 | | (fEdgeTriggered ? ACPI_RSRCS_EXT_INTR_VEC_F_EDGE_TRIGGERED : ACPI_RSRCS_EXT_INTR_VEC_F_LEVEL_TRIGGERED)
|
---|
1023 | | (fActiveLow ? ACPI_RSRCS_EXT_INTR_VEC_F_ACTIVE_LOW : ACPI_RSRCS_EXT_INTR_VEC_F_ACTIVE_HIGH)
|
---|
1024 | | (fShared ? ACPI_RSRCS_EXT_INTR_VEC_F_SHARED : ACPI_RSRCS_EXT_INTR_VEC_F_EXCLUSIVE)
|
---|
1025 | | (fWakeCapable ? ACPI_RSRCS_EXT_INTR_VEC_F_WAKE_CAP : ACPI_RSRCS_EXT_INTR_VEC_F_NOT_WAKE_CAP);
|
---|
1026 | pb[4] = cIntrs;
|
---|
1027 | pb = &pb[5];
|
---|
1028 | for (uint32_t i = 0; i < cIntrs; i++)
|
---|
1029 | pb = rtAcpiResEncode32BitInteger(pb, pau32Intrs[i]);
|
---|
1030 |
|
---|
1031 | return VINF_SUCCESS;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 |
|
---|
1035 | /**
|
---|
1036 | * Common worker for encoding a new quad word (64-bit) address range.
|
---|
1037 | *
|
---|
1038 | * @returns IPRT status code
|
---|
1039 | * @retval VERR_NO_MEMORY if not enough memory could be reserved in the ACPI resource descriptor.
|
---|
1040 | * @param pThis The ACPI resource instance.
|
---|
1041 | * @param bType The ACPI address range type.
|
---|
1042 | * @param fAddrSpace Combination of RTACPI_RESOURCE_ADDR_RANGE_F_XXX.
|
---|
1043 | * @param fType The range flags returned from rtAcpiResourceMemoryRangeToTypeFlags().
|
---|
1044 | * @param u64AddrMin The start address of the memory range.
|
---|
1045 | * @param u64AddrMax Last valid address of the range.
|
---|
1046 | * @param u64OffTrans Translation offset being applied to the address (for a PCIe bridge or IOMMU for example).
|
---|
1047 | * @param u64Granularity The access granularity of the range in bytes.
|
---|
1048 | * @param u64Length Length of the memory range in bytes.
|
---|
1049 | */
|
---|
1050 | static int rtAcpiResourceAddQWordAddressRange(PRTACPIRESINT pThis, uint8_t bType, uint32_t fAddrSpace, uint8_t fType,
|
---|
1051 | uint64_t u64AddrMin, uint64_t u64AddrMax, uint64_t u64OffTrans,
|
---|
1052 | uint64_t u64Granularity, uint64_t u64Length)
|
---|
1053 | {
|
---|
1054 | uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 3 + 43);
|
---|
1055 | if (!pb)
|
---|
1056 | return VERR_NO_MEMORY;
|
---|
1057 |
|
---|
1058 | pb[0] = ACPI_RSRCS_LARGE_TYPE | ACPI_RSRCS_ITEM_QWORD_ADDR_SPACE; /* Tag */
|
---|
1059 | pb[1] = 43; /* Length[7:0] */
|
---|
1060 | pb[2] = 0; /* Length[15:8] */
|
---|
1061 | pb[3] = bType;
|
---|
1062 | pb[4] = (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_DECODE_TYPE_SUB ? ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_SUB : ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_POS)
|
---|
1063 | | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MIN_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_CHANGEABLE)
|
---|
1064 | | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MAX_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_CHANGEABLE);
|
---|
1065 | pb[5] = fType;
|
---|
1066 |
|
---|
1067 | pb = rtAcpiResEncode64BitInteger(&pb[6], u64Granularity);
|
---|
1068 | pb = rtAcpiResEncode64BitInteger(pb, u64AddrMin);
|
---|
1069 | pb = rtAcpiResEncode64BitInteger(pb, u64AddrMax);
|
---|
1070 | pb = rtAcpiResEncode64BitInteger(pb, u64OffTrans);
|
---|
1071 | rtAcpiResEncode64BitInteger(pb, u64Length);
|
---|
1072 | return VINF_SUCCESS;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 |
|
---|
1076 | /**
|
---|
1077 | * Common worker for encoding a new double word (32-bit) address range.
|
---|
1078 | *
|
---|
1079 | * @returns IPRT status code
|
---|
1080 | * @retval VERR_NO_MEMORY if not enough memory could be reserved in the ACPI resource descriptor.
|
---|
1081 | * @param pThis The ACPI resource instance.
|
---|
1082 | * @param bType The ACPI address range type.
|
---|
1083 | * @param fAddrSpace Combination of RTACPI_RESOURCE_ADDR_RANGE_F_XXX.
|
---|
1084 | * @param fType The range flags returned from rtAcpiResourceMemoryRangeToTypeFlags().
|
---|
1085 | * @param u32AddrMin The start address of the memory range.
|
---|
1086 | * @param u32AddrMax Last valid address of the range.
|
---|
1087 | * @param u32OffTrans Translation offset being applied to the address (for a PCIe bridge or IOMMU for example).
|
---|
1088 | * @param u32Granularity The access granularity of the range in bytes.
|
---|
1089 | * @param u32Length Length of the memory range in bytes.
|
---|
1090 | */
|
---|
1091 | static int rtAcpiResourceAddDWordAddressRange(PRTACPIRESINT pThis, uint8_t bType, uint32_t fAddrSpace, uint8_t fType,
|
---|
1092 | uint32_t u32AddrMin, uint32_t u32AddrMax, uint32_t u32OffTrans,
|
---|
1093 | uint32_t u32Granularity, uint32_t u32Length)
|
---|
1094 | {
|
---|
1095 | uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 3 + 23);
|
---|
1096 | if (!pb)
|
---|
1097 | return VERR_NO_MEMORY;
|
---|
1098 |
|
---|
1099 | pb[0] = ACPI_RSRCS_LARGE_TYPE | ACPI_RSRCS_ITEM_DWORD_ADDR_SPACE; /* Tag */
|
---|
1100 | pb[1] = 23; /* Length[7:0] */
|
---|
1101 | pb[2] = 0; /* Length[15:8] */
|
---|
1102 | pb[3] = bType;
|
---|
1103 | pb[4] = (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_DECODE_TYPE_SUB ? ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_SUB : ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_POS)
|
---|
1104 | | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MIN_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_CHANGEABLE)
|
---|
1105 | | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MAX_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_CHANGEABLE);
|
---|
1106 | pb[5] = fType;
|
---|
1107 |
|
---|
1108 | pb = rtAcpiResEncode32BitInteger(&pb[6], u32Granularity);
|
---|
1109 | pb = rtAcpiResEncode32BitInteger(pb, u32AddrMin);
|
---|
1110 | pb = rtAcpiResEncode32BitInteger(pb, u32AddrMax);
|
---|
1111 | pb = rtAcpiResEncode32BitInteger(pb, u32OffTrans);
|
---|
1112 | rtAcpiResEncode32BitInteger(pb, u32Length);
|
---|
1113 | return VINF_SUCCESS;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 |
|
---|
1117 | /**
|
---|
1118 | * Converts the given cacheability, range type and R/W flag to the ACPI resource flags.
|
---|
1119 | *
|
---|
1120 | * @returns Converted ACPI resource flags.
|
---|
1121 | * @param enmCacheability The cacheability enum to convert.
|
---|
1122 | * @param enmType THe memory range type enum to convert.
|
---|
1123 | * @param fRw The read/write flag.
|
---|
1124 | */
|
---|
1125 | DECLINLINE(uint8_t) rtAcpiResourceMemoryRangeToTypeFlags(RTACPIRESMEMRANGECACHEABILITY enmCacheability, RTACPIRESMEMRANGETYPE enmType,
|
---|
1126 | bool fRw)
|
---|
1127 | {
|
---|
1128 | uint8_t fType = fRw ? ACPI_RSRCS_ADDR_SPACE_MEM_F_RW : ACPI_RSRCS_ADDR_SPACE_MEM_F_RO;
|
---|
1129 |
|
---|
1130 | switch (enmCacheability)
|
---|
1131 | {
|
---|
1132 | case kAcpiResMemRangeCacheability_NonCacheable:
|
---|
1133 | fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_CACHE_NON_CACHEABLE;
|
---|
1134 | break;
|
---|
1135 | case kAcpiResMemRangeCacheability_Cacheable:
|
---|
1136 | fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_CACHE_CACHEABLE;
|
---|
1137 | break;
|
---|
1138 | case kAcpiResMemRangeCacheability_CacheableWriteCombining:
|
---|
1139 | fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_CACHE_CACHEABLE_WR_COMB;
|
---|
1140 | break;
|
---|
1141 | case kAcpiResMemRangeCacheability_CacheablePrefetchable:
|
---|
1142 | fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_CACHE_CACHEABLE_PREFETCHABLE;
|
---|
1143 | break;
|
---|
1144 | case kAcpiResMemRangeCacheability_Invalid:
|
---|
1145 | default:
|
---|
1146 | AssertFailedReturn(0);
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | switch (enmType)
|
---|
1150 | {
|
---|
1151 | case kAcpiResMemType_Memory:
|
---|
1152 | fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_ATTR_MEMORY;
|
---|
1153 | break;
|
---|
1154 | case kAcpiResMemType_Reserved:
|
---|
1155 | fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_ATTR_RESERVED;
|
---|
1156 | break;
|
---|
1157 | case kAcpiResMemType_Acpi:
|
---|
1158 | fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_ATTR_ACPI;
|
---|
1159 | break;
|
---|
1160 | case kAcpiResMemType_Nvs:
|
---|
1161 | fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_ATTR_NVS;
|
---|
1162 | break;
|
---|
1163 | case kAcpiResMemType_Invalid:
|
---|
1164 | default:
|
---|
1165 | AssertFailedReturn(0);
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | return fType;
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 |
|
---|
1172 | RTDECL(int) RTAcpiResourceAddQWordMemoryRange(RTACPIRES hAcpiRes, RTACPIRESMEMRANGECACHEABILITY enmCacheability,
|
---|
1173 | RTACPIRESMEMRANGETYPE enmType, bool fRw, uint32_t fAddrSpace,
|
---|
1174 | uint64_t u64AddrMin, uint64_t u64AddrMax, uint64_t u64OffTrans,
|
---|
1175 | uint64_t u64Granularity, uint64_t u64Length)
|
---|
1176 | {
|
---|
1177 | PRTACPIRESINT pThis = hAcpiRes;
|
---|
1178 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1179 | AssertReturn(enmCacheability != kAcpiResMemRangeCacheability_Invalid, VERR_INVALID_PARAMETER);
|
---|
1180 | AssertReturn(enmType != kAcpiResMemType_Invalid, VERR_INVALID_PARAMETER);
|
---|
1181 | AssertReturn(!(fAddrSpace & ~RTACPI_RESOURCE_ADDR_RANGE_F_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
1182 | AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
|
---|
1183 | AssertRCReturn(pThis->rcErr, pThis->rcErr);
|
---|
1184 |
|
---|
1185 | uint8_t fType = rtAcpiResourceMemoryRangeToTypeFlags(enmCacheability, enmType, fRw);
|
---|
1186 | return rtAcpiResourceAddQWordAddressRange(pThis, ACPI_RSRCS_ADDR_SPACE_TYPE_MEMORY, fAddrSpace, fType,
|
---|
1187 | u64AddrMin, u64AddrMax, u64OffTrans, u64Granularity, u64Length);
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 |
|
---|
1191 | RTDECL(int) RTAcpiResourceAddDWordMemoryRange(RTACPIRES hAcpiRes, RTACPIRESMEMRANGECACHEABILITY enmCacheability,
|
---|
1192 | RTACPIRESMEMRANGETYPE enmType, bool fRw, uint32_t fAddrSpace,
|
---|
1193 | uint32_t u32AddrMin, uint32_t u32AddrMax, uint32_t u32OffTrans,
|
---|
1194 | uint32_t u32Granularity, uint32_t u32Length)
|
---|
1195 | {
|
---|
1196 | PRTACPIRESINT pThis = hAcpiRes;
|
---|
1197 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1198 | AssertReturn(enmCacheability != kAcpiResMemRangeCacheability_Invalid, VERR_INVALID_PARAMETER);
|
---|
1199 | AssertReturn(enmType != kAcpiResMemType_Invalid, VERR_INVALID_PARAMETER);
|
---|
1200 | AssertReturn(!(fAddrSpace & ~RTACPI_RESOURCE_ADDR_RANGE_F_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
1201 | AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
|
---|
1202 | AssertRCReturn(pThis->rcErr, pThis->rcErr);
|
---|
1203 |
|
---|
1204 | uint8_t fType = rtAcpiResourceMemoryRangeToTypeFlags(enmCacheability, enmType, fRw);
|
---|
1205 | return rtAcpiResourceAddDWordAddressRange(pThis, ACPI_RSRCS_ADDR_SPACE_TYPE_MEMORY, fAddrSpace, fType,
|
---|
1206 | u32AddrMin, u32AddrMax, u32OffTrans, u32Granularity, u32Length);
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 |
|
---|
1210 | RTDECL(int) RTAcpiResourceAddQWordIoRange(RTACPIRES hAcpiRes, RTACPIRESIORANGETYPE enmIoType, RTACPIRESIORANGE enmIoRange,
|
---|
1211 | uint32_t fAddrSpace, uint64_t u64AddrMin, uint64_t u64AddrMax, uint64_t u64OffTrans,
|
---|
1212 | uint64_t u64Granularity, uint64_t u64Length)
|
---|
1213 | {
|
---|
1214 | PRTACPIRESINT pThis = hAcpiRes;
|
---|
1215 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1216 | AssertReturn(enmIoType != kAcpiResIoRangeType_Invalid, VERR_INVALID_PARAMETER);
|
---|
1217 | AssertReturn(enmIoRange != kAcpiResIoRange_Invalid, VERR_INVALID_PARAMETER);
|
---|
1218 | AssertReturn(!(fAddrSpace & ~RTACPI_RESOURCE_ADDR_RANGE_F_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
1219 | AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
|
---|
1220 | AssertRCReturn(pThis->rcErr, pThis->rcErr);
|
---|
1221 |
|
---|
1222 | uint8_t fType = 0;
|
---|
1223 | switch (enmIoType)
|
---|
1224 | {
|
---|
1225 | case kAcpiResIoRangeType_Static:
|
---|
1226 | fType = ACPI_RSRCS_ADDR_SPACE_IO_F_TYPE_STATIC;
|
---|
1227 | break;
|
---|
1228 | case kAcpiResIoRangeType_Translation_Sparse:
|
---|
1229 | fType = ACPI_RSRCS_ADDR_SPACE_IO_F_TYPE_TRANSLATION | ACPI_RSRCS_ADDR_SPACE_IO_F_TRANSLATION_SPARSE;
|
---|
1230 | break;
|
---|
1231 | case kAcpiResIoRangeType_Translation_Dense:
|
---|
1232 | fType = ACPI_RSRCS_ADDR_SPACE_IO_F_TYPE_TRANSLATION | ACPI_RSRCS_ADDR_SPACE_IO_F_TRANSLATION_DENSE;
|
---|
1233 | break;
|
---|
1234 | case kAcpiResIoRangeType_Invalid:
|
---|
1235 | default:
|
---|
1236 | AssertFailedReturn(VERR_INVALID_PARAMETER);
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | switch (enmIoRange)
|
---|
1240 | {
|
---|
1241 | case kAcpiResIoRange_NonIsaOnly:
|
---|
1242 | fType |= ACPI_RSRCS_ADDR_SPACE_IO_F_RANGE_NON_ISA_ONLY;
|
---|
1243 | break;
|
---|
1244 | case kAcpiResIoRange_IsaOnly:
|
---|
1245 | fType |= ACPI_RSRCS_ADDR_SPACE_IO_F_RANGE_ISA_ONLY;
|
---|
1246 | break;
|
---|
1247 | case kAcpiResIoRange_Whole:
|
---|
1248 | fType |= ACPI_RSRCS_ADDR_SPACE_IO_F_RANGE_WHOLE;
|
---|
1249 | break;
|
---|
1250 | case kAcpiResIoRange_Invalid:
|
---|
1251 | default:
|
---|
1252 | AssertFailedReturn(VERR_INVALID_PARAMETER);
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | return rtAcpiResourceAddQWordAddressRange(pThis, ACPI_RSRCS_ADDR_SPACE_TYPE_IO, fAddrSpace, fType,
|
---|
1256 | u64AddrMin, u64AddrMax, u64OffTrans, u64Granularity, u64Length);
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 |
|
---|
1260 | RTDECL(int) RTAcpiResourceAddWordBusNumber(RTACPIRES hAcpiRes, uint32_t fAddrSpace, uint16_t u16BusMin, uint16_t u16BusMax,
|
---|
1261 | uint16_t u16OffTrans, uint16_t u16Granularity, uint16_t u16Length)
|
---|
1262 | {
|
---|
1263 | PRTACPIRESINT pThis = hAcpiRes;
|
---|
1264 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1265 | AssertReturn(!(fAddrSpace & ~RTACPI_RESOURCE_ADDR_RANGE_F_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
1266 | AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
|
---|
1267 | AssertRCReturn(pThis->rcErr, pThis->rcErr);
|
---|
1268 |
|
---|
1269 | uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 3 + 13);
|
---|
1270 | if (!pb)
|
---|
1271 | return VERR_NO_MEMORY;
|
---|
1272 |
|
---|
1273 | pb[0] = ACPI_RSRCS_LARGE_TYPE | ACPI_RSRCS_ITEM_WORD_ADDR_SPACE; /* Tag */
|
---|
1274 | pb[1] = 13; /* Length[7:0] */
|
---|
1275 | pb[2] = 0; /* Length[15:8] */
|
---|
1276 | pb[3] = ACPI_RSRCS_ADDR_SPACE_TYPE_BUS_NUM_RANGE;
|
---|
1277 | pb[4] = (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_DECODE_TYPE_SUB ? ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_SUB : ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_POS)
|
---|
1278 | | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MIN_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_CHANGEABLE)
|
---|
1279 | | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MAX_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_CHANGEABLE);
|
---|
1280 | pb[5] = 0;
|
---|
1281 |
|
---|
1282 | pb = rtAcpiResEncode16BitInteger(&pb[6], u16Granularity);
|
---|
1283 | pb = rtAcpiResEncode16BitInteger(pb, u16BusMin);
|
---|
1284 | pb = rtAcpiResEncode16BitInteger(pb, u16BusMax);
|
---|
1285 | pb = rtAcpiResEncode16BitInteger(pb, u16OffTrans);
|
---|
1286 | rtAcpiResEncode16BitInteger(pb, u16Length);
|
---|
1287 | return VINF_SUCCESS;
|
---|
1288 |
|
---|
1289 | }
|
---|