1 | /* $Id$ */
|
---|
2 | /** @file
|
---|
3 | * HDAStreamPeriod.cpp - Stream period functions for HD Audio.
|
---|
4 | *
|
---|
5 | * Utility functions for handling HDA audio stream periods.
|
---|
6 | * Stream period handling is needed in order to keep track of a stream's timing
|
---|
7 | * and processed audio data.
|
---|
8 | *
|
---|
9 | * As the HDA device only has one bit clock (WALCLK) but audio streams can be processed
|
---|
10 | * at certain points in time, these functions can be used to estimate and schedule the
|
---|
11 | * wall clock (WALCLK) for all streams accordingly.
|
---|
12 | */
|
---|
13 |
|
---|
14 | /*
|
---|
15 | * Copyright (C) 2017 Oracle Corporation
|
---|
16 | *
|
---|
17 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
18 | * available from http://www.virtualbox.org. This file is free software;
|
---|
19 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
20 | * General Public License (GPL) as published by the Free Software
|
---|
21 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
22 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
23 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
24 | */
|
---|
25 |
|
---|
26 | /*********************************************************************************************************************************
|
---|
27 | * Header Files *
|
---|
28 | *********************************************************************************************************************************/
|
---|
29 | #define LOG_GROUP LOG_GROUP_DEV_HDA
|
---|
30 | #include <VBox/log.h>
|
---|
31 |
|
---|
32 | #include <iprt/asm-math.h> /* For ASMMultU64ByU32DivByU32(). */
|
---|
33 |
|
---|
34 | #include <VBox/vmm/pdmdev.h>
|
---|
35 | #include <VBox/vmm/pdmaudioifs.h>
|
---|
36 |
|
---|
37 | #include "DrvAudio.h"
|
---|
38 | #include "HDAStreamPeriod.h"
|
---|
39 |
|
---|
40 | #ifdef IN_RING3
|
---|
41 | /**
|
---|
42 | * Creates a stream period.
|
---|
43 | *
|
---|
44 | * @return IPRT status code.
|
---|
45 | * @param pPeriod Stream period to initialize.
|
---|
46 | */
|
---|
47 | int hdaStreamPeriodCreate(PHDASTREAMPERIOD pPeriod)
|
---|
48 | {
|
---|
49 | Assert(!(pPeriod->fStatus & HDASTREAMPERIOD_FLAG_VALID));
|
---|
50 |
|
---|
51 | int rc = RTCritSectInit(&pPeriod->CritSect);
|
---|
52 | if (RT_SUCCESS(rc))
|
---|
53 | {
|
---|
54 | pPeriod->fStatus = HDASTREAMPERIOD_FLAG_VALID;
|
---|
55 | }
|
---|
56 |
|
---|
57 | return VINF_SUCCESS;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Destroys a formerly created stream period.
|
---|
62 | *
|
---|
63 | * @param pPeriod Stream period to destroy.
|
---|
64 | */
|
---|
65 | void hdaStreamPeriodDestroy(PHDASTREAMPERIOD pPeriod)
|
---|
66 | {
|
---|
67 | if (pPeriod->fStatus & HDASTREAMPERIOD_FLAG_VALID)
|
---|
68 | {
|
---|
69 | RTCritSectDelete(&pPeriod->CritSect);
|
---|
70 |
|
---|
71 | pPeriod->fStatus = HDASTREAMPERIOD_FLAG_NONE;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Initializes a given stream period with needed parameters.
|
---|
77 | *
|
---|
78 | * @param pPeriod Stream period to (re-)initialize. Must be created with hdaStreamPeriodCreate() first.
|
---|
79 | * @param u8SD Stream descriptor (serial data #) number to assign this stream period to.
|
---|
80 | * @param u16LVI The HDA stream's LVI value to use for the period calculation.
|
---|
81 | * @param u32CBL The HDA stream's CBL value to use for the period calculation.
|
---|
82 | * @param pStreamCfg Audio stream configuration to use for this period.
|
---|
83 | */
|
---|
84 | void hdaStreamPeriodInit(PHDASTREAMPERIOD pPeriod,
|
---|
85 | uint8_t u8SD, uint16_t u16LVI, uint32_t u32CBL, PPDMAUDIOSTREAMCFG pStreamCfg)
|
---|
86 | {
|
---|
87 |
|
---|
88 | /* Sanity. */
|
---|
89 | AssertReturnVoid(u16LVI);
|
---|
90 | AssertReturnVoid(u32CBL);
|
---|
91 | AssertReturnVoid(DrvAudioHlpStreamCfgIsValid(pStreamCfg));
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * Linux guests (at least Ubuntu):
|
---|
95 | * 17632 bytes (CBL) / 4 (frame size) = 4408 frames / 4 (LVI) = 1102 frames per period
|
---|
96 | *
|
---|
97 | * Windows guests (Win10 AU):
|
---|
98 | * 3584 bytes (CBL) / 4 (frame size) = 896 frames / 2 (LVI) = 448 frames per period
|
---|
99 | */
|
---|
100 | unsigned cTotalPeriods = u16LVI;
|
---|
101 |
|
---|
102 | if (cTotalPeriods <= 1)
|
---|
103 | cTotalPeriods = 2; /* At least two periods *must* be present (LVI >= 1). */
|
---|
104 |
|
---|
105 | uint32_t framesToTransfer = (u32CBL / 4 /** @todo Define frame size? */) / cTotalPeriods;
|
---|
106 |
|
---|
107 | pPeriod->u8SD = u8SD;
|
---|
108 | pPeriod->u64StartWalClk = 0;
|
---|
109 | pPeriod->u32Hz = pStreamCfg->Props.uHz;
|
---|
110 | pPeriod->u64DurationWalClk = hdaStreamPeriodFramesToWalClk(pPeriod, framesToTransfer);
|
---|
111 | pPeriod->u64ElapsedWalClk = 0;
|
---|
112 | pPeriod->i64DelayWalClk = 0;
|
---|
113 | pPeriod->framesToTransfer = framesToTransfer;
|
---|
114 | pPeriod->framesTransferred = 0;
|
---|
115 | pPeriod->cIntPending = 0;
|
---|
116 |
|
---|
117 | Log3Func(("[SD%RU8] %RU64 long, Hz=%RU32, CBL=%RU32, LVI=%RU16 -> %u periods, %RU32 frames each\n",
|
---|
118 | pPeriod->u8SD, pPeriod->u64DurationWalClk, pPeriod->u32Hz, u32CBL, u16LVI,
|
---|
119 | cTotalPeriods, pPeriod->framesToTransfer));
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Resets a stream period to its initial state.
|
---|
124 | *
|
---|
125 | * @param pPeriod Stream period to reset.
|
---|
126 | */
|
---|
127 | void hdaStreamPeriodReset(PHDASTREAMPERIOD pPeriod)
|
---|
128 | {
|
---|
129 | Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
|
---|
130 |
|
---|
131 | if (pPeriod->cIntPending)
|
---|
132 | LogRelMax(50, ("HDA: Warning: %RU8 interrupts for stream #%RU8 still pending -- so a period reset might trigger audio hangs\n",
|
---|
133 | pPeriod->cIntPending, pPeriod->u8SD));
|
---|
134 |
|
---|
135 | pPeriod->fStatus &= ~HDASTREAMPERIOD_FLAG_ACTIVE;
|
---|
136 | pPeriod->u64StartWalClk = 0;
|
---|
137 | pPeriod->u64ElapsedWalClk = 0;
|
---|
138 | pPeriod->framesTransferred = 0;
|
---|
139 | pPeriod->cIntPending = 0;
|
---|
140 | #ifdef DEBUG
|
---|
141 | pPeriod->Dbg.tsStartNs = 0;
|
---|
142 | #endif
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Begins a new period life span of a given period.
|
---|
147 | *
|
---|
148 | * @return IPRT status code.
|
---|
149 | * @param pPeriod Stream period to begin new life span for.
|
---|
150 | * @param u64WalClk Wall clock (WALCLK) value to set for the period's starting point.
|
---|
151 | */
|
---|
152 | int hdaStreamPeriodBegin(PHDASTREAMPERIOD pPeriod, uint64_t u64WalClk)
|
---|
153 | {
|
---|
154 | Assert(!(pPeriod->fStatus & HDASTREAMPERIOD_FLAG_ACTIVE)); /* No nested calls. */
|
---|
155 |
|
---|
156 | pPeriod->fStatus |= HDASTREAMPERIOD_FLAG_ACTIVE;
|
---|
157 | pPeriod->u64StartWalClk = u64WalClk;
|
---|
158 | pPeriod->u64ElapsedWalClk = 0;
|
---|
159 | pPeriod->framesTransferred = 0;
|
---|
160 | pPeriod->cIntPending = 0;
|
---|
161 | #ifdef DEBUG
|
---|
162 | pPeriod->Dbg.tsStartNs = RTTimeNanoTS();
|
---|
163 | #endif
|
---|
164 |
|
---|
165 | Log3Func(("[SD%RU8] Starting @ %RU64 (%RU64 long)\n",
|
---|
166 | pPeriod->u8SD, pPeriod->u64StartWalClk, pPeriod->u64DurationWalClk));
|
---|
167 |
|
---|
168 | return VINF_SUCCESS;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Ends a formerly begun period life span.
|
---|
173 | *
|
---|
174 | * @param pPeriod Stream period to end life span for.
|
---|
175 | */
|
---|
176 | void hdaStreamPeriodEnd(PHDASTREAMPERIOD pPeriod)
|
---|
177 | {
|
---|
178 | Log3Func(("[SD%RU8] Took %zuus\n", pPeriod->u8SD, (RTTimeNanoTS() - pPeriod->Dbg.tsStartNs) / 1000));
|
---|
179 |
|
---|
180 | if (!(pPeriod->fStatus & HDASTREAMPERIOD_FLAG_ACTIVE))
|
---|
181 | return;
|
---|
182 |
|
---|
183 | /* Sanity. */
|
---|
184 | AssertMsg(pPeriod->cIntPending == 0,
|
---|
185 | ("%RU8 interrupts for stream #%RU8 still pending -- so ending a period might trigger audio hangs\n",
|
---|
186 | pPeriod->cIntPending, pPeriod->u8SD));
|
---|
187 | Assert(hdaStreamPeriodIsComplete(pPeriod));
|
---|
188 |
|
---|
189 | pPeriod->fStatus &= ~HDASTREAMPERIOD_FLAG_ACTIVE;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Pauses a period. All values remain intact.
|
---|
194 | *
|
---|
195 | * @param pPeriod Stream period to pause.
|
---|
196 | */
|
---|
197 | void hdaStreamPeriodPause(PHDASTREAMPERIOD pPeriod)
|
---|
198 | {
|
---|
199 | AssertMsg((pPeriod->fStatus & HDASTREAMPERIOD_FLAG_ACTIVE), ("Period %p already in inactive state\n", pPeriod));
|
---|
200 |
|
---|
201 | pPeriod->fStatus &= ~HDASTREAMPERIOD_FLAG_ACTIVE;
|
---|
202 |
|
---|
203 | Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Resumes a formerly paused period.
|
---|
208 | *
|
---|
209 | * @param pPeriod Stream period to resume.
|
---|
210 | */
|
---|
211 | void hdaStreamPeriodResume(PHDASTREAMPERIOD pPeriod)
|
---|
212 | {
|
---|
213 | AssertMsg(!(pPeriod->fStatus & HDASTREAMPERIOD_FLAG_ACTIVE), ("Period %p already in active state\n", pPeriod));
|
---|
214 |
|
---|
215 | pPeriod->fStatus |= HDASTREAMPERIOD_FLAG_ACTIVE;
|
---|
216 |
|
---|
217 | Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Locks a stream period for serializing access.
|
---|
222 | *
|
---|
223 | * @return true if locking was successful, false if not.
|
---|
224 | * @param pPeriod Stream period to lock.
|
---|
225 | */
|
---|
226 | bool hdaStreamPeriodLock(PHDASTREAMPERIOD pPeriod)
|
---|
227 | {
|
---|
228 | return RT_SUCCESS(RTCritSectEnter(&pPeriod->CritSect));
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Unlocks a formerly locked stream period.
|
---|
233 | *
|
---|
234 | * @param pPeriod Stream period to unlock.
|
---|
235 | */
|
---|
236 | void hdaStreamPeriodUnlock(PHDASTREAMPERIOD pPeriod)
|
---|
237 | {
|
---|
238 | int rc2 = RTCritSectLeave(&pPeriod->CritSect);
|
---|
239 | AssertRC(rc2);
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Returns the wall clock (WALCLK) value for a given amount of stream period audio frames.
|
---|
244 | *
|
---|
245 | * @return Calculated wall clock value.
|
---|
246 | * @param pPeriod Stream period to calculate wall clock value for.
|
---|
247 | * @param uFrames Number of audio frames to calculate wall clock value for.
|
---|
248 | *
|
---|
249 | * @remark Calculation depends on the given stream period and assumes a 24 MHz wall clock counter (WALCLK).
|
---|
250 | */
|
---|
251 | uint64_t hdaStreamPeriodFramesToWalClk(PHDASTREAMPERIOD pPeriod, uint32_t uFrames)
|
---|
252 | {
|
---|
253 | /* Prevent division by zero. */
|
---|
254 | const uint32_t uHz = (pPeriod->u32Hz ? pPeriod->u32Hz : 1);
|
---|
255 |
|
---|
256 | /* 24 MHz wall clock (WALCLK): 42ns resolution. */
|
---|
257 | return ASMMultU64ByU32DivByU32(uFrames, 24000000, uHz);
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Returns the absolute wall clock (WALCLK) value for the already elapsed time of
|
---|
262 | * a given stream period.
|
---|
263 | *
|
---|
264 | * @return Absolute elapsed time as wall clock (WALCLK) value.
|
---|
265 | * @param pPeriod Stream period to use.
|
---|
266 | */
|
---|
267 | uint64_t hdaStreamPeriodGetAbsElapsedWalClk(PHDASTREAMPERIOD pPeriod)
|
---|
268 | {
|
---|
269 | return pPeriod->u64StartWalClk
|
---|
270 | + pPeriod->u64ElapsedWalClk
|
---|
271 | + pPeriod->i64DelayWalClk;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Returns the absolute wall clock (WALCLK) value for the calculated end time of
|
---|
276 | * a given stream period.
|
---|
277 | *
|
---|
278 | * @return Absolute end time as wall clock (WALCLK) value.
|
---|
279 | * @param pPeriod Stream period to use.
|
---|
280 | */
|
---|
281 | uint64_t hdaStreamPeriodGetAbsEndWalClk(PHDASTREAMPERIOD pPeriod)
|
---|
282 | {
|
---|
283 | return pPeriod->u64StartWalClk + pPeriod->u64DurationWalClk;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Returns the remaining audio frames to process for a given stream period.
|
---|
288 | *
|
---|
289 | * @return Number of remaining audio frames to process. 0 if all were processed.
|
---|
290 | * @param pPeriod Stream period to return value for.
|
---|
291 | */
|
---|
292 | uint32_t hdaStreamPeriodGetRemainingFrames(PHDASTREAMPERIOD pPeriod)
|
---|
293 | {
|
---|
294 | Assert(pPeriod->framesToTransfer >= pPeriod->framesTransferred);
|
---|
295 | return pPeriod->framesToTransfer - pPeriod->framesTransferred;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Tells whether a given stream period has elapsed (time-wise) or not.
|
---|
300 | *
|
---|
301 | * @return true if the stream period has elapsed, false if not.
|
---|
302 | * @param pPeriod Stream period to get status for.
|
---|
303 | */
|
---|
304 | bool hdaStreamPeriodHasElapsed(PHDASTREAMPERIOD pPeriod)
|
---|
305 | {
|
---|
306 | return (pPeriod->u64ElapsedWalClk >= pPeriod->u64DurationWalClk);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Tells whether a given stream period has passed the given absolute wall clock (WALCLK)
|
---|
311 | * time or not
|
---|
312 | *
|
---|
313 | * @return true if the stream period has passed the given time, false if not.
|
---|
314 | * @param pPeriod Stream period to get status for.
|
---|
315 | * @param u64WalClk Absolute wall clock (WALCLK) time to check for.
|
---|
316 | */
|
---|
317 | bool hdaStreamPeriodHasPassedAbsWalClk(PHDASTREAMPERIOD pPeriod, uint64_t u64WalClk)
|
---|
318 | {
|
---|
319 | /* Period not in use? */
|
---|
320 | if (!(pPeriod->fStatus & HDASTREAMPERIOD_FLAG_ACTIVE))
|
---|
321 | return true; /* ... implies that it has passed. */
|
---|
322 |
|
---|
323 | if (hdaStreamPeriodHasElapsed(pPeriod))
|
---|
324 | return true; /* Period already has elapsed. */
|
---|
325 |
|
---|
326 | return (pPeriod->u64StartWalClk + pPeriod->u64ElapsedWalClk) >= u64WalClk;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Tells whether a given stream period has some required interrupts pending or not.
|
---|
331 | *
|
---|
332 | * @return true if period has interrupts pending, false if not.
|
---|
333 | * @param pPeriod Stream period to get status for.
|
---|
334 | */
|
---|
335 | bool hdaStreamPeriodNeedsInterrupt(PHDASTREAMPERIOD pPeriod)
|
---|
336 | {
|
---|
337 | return pPeriod->cIntPending > 0;
|
---|
338 | }
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * Acquires (references) an (pending) interrupt for a given stream period.
|
---|
342 | *
|
---|
343 | * @param pPeriod Stream period to acquire interrupt for.
|
---|
344 | *
|
---|
345 | * @remark This routine does not do any actual interrupt processing; it only
|
---|
346 | * keeps track of the required (pending) interrupts for a stream period.
|
---|
347 | */
|
---|
348 | void hdaStreamPeriodAcquireInterrupt(PHDASTREAMPERIOD pPeriod)
|
---|
349 | {
|
---|
350 | uint32_t cIntPending = pPeriod->cIntPending;
|
---|
351 | if (cIntPending)
|
---|
352 | {
|
---|
353 | Log3Func(("[SD%RU8] Already pending\n", pPeriod->u8SD));
|
---|
354 | return;
|
---|
355 | }
|
---|
356 |
|
---|
357 | pPeriod->cIntPending++;
|
---|
358 |
|
---|
359 | Log3Func(("[SD%RU8] %RU32\n", pPeriod->u8SD, pPeriod->cIntPending));
|
---|
360 | }
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Releases (dereferences) a pending interrupt.
|
---|
364 | *
|
---|
365 | * @param pPeriod Stream period to release pending interrupt for.
|
---|
366 | */
|
---|
367 | void hdaStreamPeriodReleaseInterrupt(PHDASTREAMPERIOD pPeriod)
|
---|
368 | {
|
---|
369 | Assert(pPeriod->cIntPending);
|
---|
370 | pPeriod->cIntPending--;
|
---|
371 |
|
---|
372 | Log3Func(("[SD%RU8] %RU32\n", pPeriod->u8SD, pPeriod->cIntPending));
|
---|
373 | }
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * Adds an amount of (processed) audio frames to a given stream period.
|
---|
377 | *
|
---|
378 | * @return IPRT status code.
|
---|
379 | * @param pPeriod Stream period to add audio frames to.
|
---|
380 | * @param framesInc Audio frames to add.
|
---|
381 | */
|
---|
382 | void hdaStreamPeriodInc(PHDASTREAMPERIOD pPeriod, uint32_t framesInc)
|
---|
383 | {
|
---|
384 | pPeriod->framesTransferred += framesInc;
|
---|
385 | Assert(pPeriod->framesTransferred <= pPeriod->framesToTransfer);
|
---|
386 |
|
---|
387 | pPeriod->u64ElapsedWalClk = hdaStreamPeriodFramesToWalClk(pPeriod, pPeriod->framesTransferred);
|
---|
388 | Assert(pPeriod->u64ElapsedWalClk <= pPeriod->u64DurationWalClk);
|
---|
389 |
|
---|
390 | Log3Func(("[SD%RU8] cbTransferred=%RU32, u64ElapsedWalClk=%RU64\n",
|
---|
391 | pPeriod->u8SD, pPeriod->framesTransferred, pPeriod->u64ElapsedWalClk));
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Tells whether a given stream period is considered as complete or not.
|
---|
396 | *
|
---|
397 | * @return true if stream period is complete, false if not.
|
---|
398 | * @param pPeriod Stream period to report status for.
|
---|
399 | *
|
---|
400 | * @remark A stream period is considered complete if it has 1) passed (elapsed) its calculated period time
|
---|
401 | * and 2) processed all required audio frames.
|
---|
402 | */
|
---|
403 | bool hdaStreamPeriodIsComplete(PHDASTREAMPERIOD pPeriod)
|
---|
404 | {
|
---|
405 | const bool fIsComplete = /* Has the period elapsed time-wise? */
|
---|
406 | hdaStreamPeriodHasElapsed(pPeriod)
|
---|
407 | /* All frames transferred? */
|
---|
408 | && pPeriod->framesTransferred >= pPeriod->framesToTransfer;
|
---|
409 | #ifdef VBOX_STRICT
|
---|
410 | if (fIsComplete)
|
---|
411 | {
|
---|
412 | Assert(pPeriod->framesTransferred == pPeriod->framesToTransfer);
|
---|
413 | Assert(pPeriod->u64ElapsedWalClk == pPeriod->u64DurationWalClk);
|
---|
414 | }
|
---|
415 | #endif
|
---|
416 |
|
---|
417 | Log3Func(("[SD%RU8] Period %s - runtime %RU64 / %RU64 (abs @ %RU64, starts @ %RU64, ends @ %RU64), %RU8 IRQs pending\n",
|
---|
418 | pPeriod->u8SD,
|
---|
419 | fIsComplete ? "COMPLETE" : "NOT COMPLETE YET",
|
---|
420 | pPeriod->u64ElapsedWalClk, pPeriod->u64DurationWalClk,
|
---|
421 | hdaStreamPeriodGetAbsElapsedWalClk(pPeriod), pPeriod->u64StartWalClk,
|
---|
422 | hdaStreamPeriodGetAbsEndWalClk(pPeriod), pPeriod->cIntPending));
|
---|
423 |
|
---|
424 | return fIsComplete;
|
---|
425 | }
|
---|
426 | #endif /* IN_RING3 */
|
---|
427 |
|
---|