VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Miniport/VBoxVideoHGSMI.cpp@ 30462

Last change on this file since 30462 was 30169, checked in by vboxsync, 15 years ago

wddm: scanline info support basics, some tooling for complex vidpn topology handling

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 54.9 KB
Line 
1/* $Id: VBoxVideoHGSMI.cpp 30169 2010-06-11 18:24:42Z vboxsync $ */
2/** @file
3 * VirtualBox Video miniport driver for NT/2k/XP - HGSMI related functions.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19#include "VBoxVideo.h"
20#include "Helper.h"
21
22#include <iprt/asm.h>
23#include <iprt/log.h>
24#include <iprt/thread.h>
25#include <VBox/VMMDev.h>
26#include <VBox/VBoxGuest.h>
27#include <VBox/VBoxVideo.h>
28
29#include <VBoxDisplay.h>
30
31#include "vboxioctl.h"
32
33#define MEM_TAG 'HVBV'
34
35void HGSMINotifyHostCmdComplete (PDEVICE_EXTENSION PrimaryExtension, HGSMIOFFSET offt)
36{
37 VBoxHGSMIHostWrite(PrimaryExtension, offt);
38}
39
40void HGSMIClearIrq (PDEVICE_EXTENSION PrimaryExtension)
41{
42 VBoxHGSMIHostWrite(PrimaryExtension, HGSMIOFFSET_VOID);
43}
44
45static void HGSMIHostCmdComplete (PDEVICE_EXTENSION PrimaryExtension, void * pvMem)
46{
47 HGSMIOFFSET offMem = HGSMIPointerToOffset (&PrimaryExtension->u.primary.areaHostHeap, HGSMIBufferHeaderFromData (pvMem));
48 Assert(offMem != HGSMIOFFSET_VOID);
49 if(offMem != HGSMIOFFSET_VOID)
50 {
51 HGSMINotifyHostCmdComplete (PrimaryExtension, offMem);
52 }
53}
54
55static void hgsmiHostCmdProcess(PDEVICE_EXTENSION PrimaryExtension, HGSMIOFFSET offBuffer)
56{
57 int rc = HGSMIBufferProcess (&PrimaryExtension->u.primary.areaHostHeap,
58 &PrimaryExtension->u.primary.channels,
59 offBuffer);
60 Assert(!RT_FAILURE(rc));
61 if(RT_FAILURE(rc))
62 {
63 /* failure means the command was not submitted to the handler for some reason
64 * it's our responsibility to notify its completion in this case */
65 HGSMINotifyHostCmdComplete(PrimaryExtension, offBuffer);
66 }
67 /* if the cmd succeeded it's responsibility of the callback to complete it */
68}
69
70static HGSMIOFFSET hgsmiGetHostBuffer (PDEVICE_EXTENSION PrimaryExtension)
71{
72 return VBoxHGSMIHostRead(PrimaryExtension);
73}
74
75static void hgsmiHostCommandQueryProcess (PDEVICE_EXTENSION PrimaryExtension)
76{
77 HGSMIOFFSET offset = hgsmiGetHostBuffer (PrimaryExtension);
78 Assert(offset != HGSMIOFFSET_VOID);
79 if(offset != HGSMIOFFSET_VOID)
80 {
81 hgsmiHostCmdProcess(PrimaryExtension, offset);
82 }
83}
84
85#define VBOX_HGSMI_LOCK(_pe, _plock, _dpc, _pold) \
86 do { \
87 if(_dpc) \
88 { \
89 VBoxVideoCmnSpinLockAcquireAtDpcLevel(_pe, _plock); \
90 } \
91 else \
92 {\
93 VBoxVideoCmnSpinLockAcquire(_pe, _plock, _pold); \
94 }\
95 } while(0)
96
97#define VBOX_HGSMI_UNLOCK(_pe, _plock, _dpc, _pold) \
98 do { \
99 if(_dpc) \
100 { \
101 VBoxVideoCmnSpinLockReleaseFromDpcLevel(_pe, _plock); \
102 } \
103 else \
104 {\
105 VBoxVideoCmnSpinLockRelease(_pe, _plock, _pold); \
106 }\
107 } while(0)
108
109VOID VBoxVideoHGSMIDpc(
110 IN PVOID HwDeviceExtension,
111 IN PVOID Context
112 )
113{
114 PDEVICE_EXTENSION PrimaryExtension = (PDEVICE_EXTENSION)HwDeviceExtension;
115 uint32_t flags = (uint32_t)Context;
116 bool bProcessing = false;
117 VBOXVCMNIRQL OldIrql;
118 /* we check if another thread is processing the queue and exit if so */
119 do
120 {
121 bool bLock = false;
122 if(!(PrimaryExtension->u.primary.pHostFlags->u32HostFlags & HGSMIHOSTFLAGS_COMMANDS_PENDING))
123 {
124 if(!bProcessing)
125 {
126 break;
127 }
128 VBOX_HGSMI_LOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, &OldIrql);
129 if(!(PrimaryExtension->u.primary.pHostFlags->u32HostFlags & HGSMIHOSTFLAGS_COMMANDS_PENDING))
130 {
131 Assert(PrimaryExtension->u.primary.bHostCmdProcessing);
132 PrimaryExtension->u.primary.bHostCmdProcessing = false;
133 VBOX_HGSMI_UNLOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, OldIrql);
134 break;
135 }
136 VBOX_HGSMI_UNLOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, OldIrql);
137 }
138 else
139 {
140 if(!bProcessing)
141 {
142 VBOX_HGSMI_LOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, &OldIrql);
143 if(!(PrimaryExtension->u.primary.pHostFlags->u32HostFlags & HGSMIHOSTFLAGS_COMMANDS_PENDING)
144 || PrimaryExtension->u.primary.bHostCmdProcessing)
145 {
146 VBOX_HGSMI_UNLOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, OldIrql);
147 break;
148 }
149 Assert(!PrimaryExtension->u.primary.bHostCmdProcessing);
150 PrimaryExtension->u.primary.bHostCmdProcessing = true;
151 VBOX_HGSMI_UNLOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, OldIrql);
152 bProcessing = true;
153 }
154 }
155
156 Assert(bProcessing);
157 Assert(PrimaryExtension->u.primary.bHostCmdProcessing);
158 Assert((PrimaryExtension->u.primary.pHostFlags->u32HostFlags & HGSMIHOSTFLAGS_COMMANDS_PENDING) != 0);
159 bProcessing = true;
160
161 hgsmiHostCommandQueryProcess (PrimaryExtension);
162 } while(true/*!PrimaryExtension->u.primary.bPollingStop*/);
163}
164
165/* Detect whether HGSMI is supported by the host. */
166BOOLEAN VBoxHGSMIIsSupported (PDEVICE_EXTENSION PrimaryExtension)
167{
168 USHORT DispiId;
169
170 VBoxVideoCmnPortWriteUshort((PUSHORT)VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ID);
171 VBoxVideoCmnPortWriteUshort((PUSHORT)VBE_DISPI_IOPORT_DATA, VBE_DISPI_ID_HGSMI);
172
173 DispiId = VBoxVideoCmnPortReadUshort((PUSHORT)VBE_DISPI_IOPORT_DATA);
174
175 return (DispiId == VBE_DISPI_ID_HGSMI);
176}
177
178typedef int FNHGSMICALLINIT (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData);
179typedef FNHGSMICALLINIT *PFNHGSMICALLINIT;
180
181typedef int FNHGSMICALLFINALIZE (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData);
182typedef FNHGSMICALLFINALIZE *PFNHGSMICALLFINALIZE;
183
184void* vboxHGSMIBufferAlloc(PDEVICE_EXTENSION PrimaryExtension,
185 HGSMISIZE cbData,
186 uint8_t u8Ch,
187 uint16_t u16Op)
188{
189#ifdef VBOXWDDM
190 /* @todo: add synchronization */
191#endif
192 return HGSMIHeapAlloc (&PrimaryExtension->u.primary.hgsmiAdapterHeap, cbData, u8Ch, u16Op);
193}
194
195void vboxHGSMIBufferFree (PDEVICE_EXTENSION PrimaryExtension, void *pvBuffer)
196{
197#ifdef VBOXWDDM
198 /* @todo: add synchronization */
199#endif
200 HGSMIHeapFree (&PrimaryExtension->u.primary.hgsmiAdapterHeap, pvBuffer);
201}
202
203int vboxHGSMIBufferSubmit (PDEVICE_EXTENSION PrimaryExtension, void *pvBuffer)
204{
205 /* Initialize the buffer and get the offset for port IO. */
206 HGSMIOFFSET offBuffer = HGSMIHeapBufferOffset (&PrimaryExtension->u.primary.hgsmiAdapterHeap, pvBuffer);
207
208 Assert(offBuffer != HGSMIOFFSET_VOID);
209 if (offBuffer != HGSMIOFFSET_VOID)
210 {
211 /* Submit the buffer to the host. */
212 VBoxHGSMIGuestWrite(PrimaryExtension, offBuffer);
213 return VINF_SUCCESS;
214 }
215
216 return VERR_INVALID_PARAMETER;
217}
218
219static int vboxCallChannel (PDEVICE_EXTENSION PrimaryExtension,
220 uint8_t u8Ch,
221 uint16_t u16Op,
222 HGSMISIZE cbData,
223 PFNHGSMICALLINIT pfnInit,
224 PFNHGSMICALLFINALIZE pfnFinalize,
225 void *pvContext)
226{
227 int rc = VINF_SUCCESS;
228
229 /* Allocate the IO buffer. */
230#ifndef VBOXWDDM
231 if (PrimaryExtension->pPrimary != PrimaryExtension)
232 {
233 dprintf(("VBoxVideo::vboxCallChannel: not primary extension %p!!!\n", PrimaryExtension));
234 return VERR_INVALID_PARAMETER;
235 }
236#endif
237
238 void *p = HGSMIHeapAlloc (&PrimaryExtension->u.primary.hgsmiAdapterHeap, cbData, u8Ch, u16Op);
239
240 if (!p)
241 {
242 rc = VERR_NO_MEMORY;
243 }
244 else
245 {
246 /* Prepare data to be sent to the host. */
247 if (pfnInit)
248 {
249 rc = pfnInit (PrimaryExtension, pvContext, p);
250 }
251
252 if (RT_SUCCESS (rc))
253 {
254 /* Initialize the buffer and get the offset for port IO. */
255 HGSMIOFFSET offBuffer = HGSMIHeapBufferOffset (&PrimaryExtension->u.primary.hgsmiAdapterHeap,
256 p);
257
258 /* Submit the buffer to the host. */
259 VBoxHGSMIGuestWrite(PrimaryExtension, offBuffer);
260
261 if (pfnFinalize)
262 {
263 rc = pfnFinalize (PrimaryExtension, pvContext, p);
264 }
265 }
266 else
267 {
268 AssertFailed ();
269 rc = VERR_INTERNAL_ERROR;
270 }
271
272 /* Free the IO buffer. */
273 HGSMIHeapFree (&PrimaryExtension->u.primary.hgsmiAdapterHeap, p);
274 }
275
276 return rc;
277}
278
279static int vboxCallVBVA (PDEVICE_EXTENSION PrimaryExtension,
280 uint16_t u16Op,
281 HGSMISIZE cbData,
282 PFNHGSMICALLINIT pfnInit,
283 PFNHGSMICALLFINALIZE pfnFinalize,
284 void *pvContext)
285{
286 return vboxCallChannel (PrimaryExtension,
287 HGSMI_CH_VBVA,
288 u16Op,
289 cbData,
290 pfnInit,
291 pfnFinalize,
292 pvContext);
293}
294
295typedef struct _QUERYCONFCTX
296{
297 uint32_t u32Index;
298 ULONG *pulValue;
299} QUERYCONFCTX;
300
301static int vbvaInitQueryConf (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
302{
303 NOREF (PrimaryExtension);
304
305 QUERYCONFCTX *pCtx = (QUERYCONFCTX *)pvContext;
306 VBVACONF32 *p = (VBVACONF32 *)pvData;
307
308 p->u32Index = pCtx->u32Index;
309 p->u32Value = 0;
310
311 return VINF_SUCCESS;
312}
313
314static int vbvaFinalizeQueryConf (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
315{
316 NOREF (PrimaryExtension);
317
318 QUERYCONFCTX *pCtx = (QUERYCONFCTX *)pvContext;
319 VBVACONF32 *p = (VBVACONF32 *)pvData;
320
321 if (pCtx->pulValue)
322 {
323 *pCtx->pulValue = p->u32Value;
324 }
325
326 dprintf(("VBoxVideo::vboxQueryConf: u32Value = %d\n", p->u32Value));
327 return VINF_SUCCESS;
328}
329
330static int vboxQueryConfHGSMI (PDEVICE_EXTENSION PrimaryExtension, uint32_t u32Index, ULONG *pulValue)
331{
332 dprintf(("VBoxVideo::vboxQueryConf: u32Index = %d\n", u32Index));
333
334 QUERYCONFCTX context;
335
336 context.u32Index = u32Index;
337 context.pulValue = pulValue;
338
339 int rc = vboxCallVBVA (PrimaryExtension,
340 VBVA_QUERY_CONF32,
341 sizeof (VBVACONF32),
342 vbvaInitQueryConf,
343 vbvaFinalizeQueryConf,
344 &context);
345
346 dprintf(("VBoxVideo::vboxQueryConf: rc = %d\n", rc));
347
348 return rc;
349}
350#ifndef VBOXWDDM
351static int vbvaInitInfoDisplay (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
352{
353 NOREF (pvContext);
354 VBVAINFOVIEW *p = (VBVAINFOVIEW *)pvData;
355
356 int i;
357 PDEVICE_EXTENSION Extension;
358
359 for (i = 0, Extension = PrimaryExtension;
360 i < PrimaryExtension->u.primary.cDisplays && Extension;
361 i++, Extension = Extension->pNext)
362 {
363 p[i].u32ViewIndex = Extension->iDevice;
364 p[i].u32ViewOffset = Extension->ulFrameBufferOffset;
365 p[i].u32ViewSize = PrimaryExtension->u.primary.ulMaxFrameBufferSize;
366
367 /* How much VRAM should be reserved for the guest drivers to use VBVA. */
368 const uint32_t cbReservedVRAM = VBVA_DISPLAY_INFORMATION_SIZE + VBVA_MIN_BUFFER_SIZE;
369
370 p[i].u32MaxScreenSize = p[i].u32ViewSize > cbReservedVRAM?
371 p[i].u32ViewSize - cbReservedVRAM:
372 0;
373 }
374
375 if (i == PrimaryExtension->u.primary.cDisplays && Extension == NULL)
376 {
377 return VINF_SUCCESS;
378 }
379
380 AssertFailed ();
381 return VERR_INTERNAL_ERROR;
382}
383#else
384int vbvaInitInfoCaps (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
385{
386 VBVACAPS *pCaps = (VBVACAPS*)pvData;
387 pCaps->rc = VERR_NOT_IMPLEMENTED;
388 pCaps->fCaps = VBVACAPS_COMPLETEGCMD_BY_IOREAD | VBVACAPS_IRQ;
389 return VINF_SUCCESS;
390}
391
392
393int vbvaFinalizeInfoCaps (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
394{
395 VBVACAPS *pCaps = (VBVACAPS*)pvData;
396 AssertRC(pCaps->rc);
397 return pCaps->rc;
398}
399#endif
400
401static int vbvaInitInfoHeap (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
402{
403 NOREF (pvContext);
404 VBVAINFOHEAP *p = (VBVAINFOHEAP *)pvData;
405
406 p->u32HeapOffset = PrimaryExtension->u.primary.cbVRAM
407 - PrimaryExtension->u.primary.cbMiniportHeap
408 - VBVA_ADAPTER_INFORMATION_SIZE;
409 p->u32HeapSize = PrimaryExtension->u.primary.cbMiniportHeap;
410
411 return VINF_SUCCESS;
412}
413
414static int hgsmiInitFlagsLocation (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
415{
416 NOREF (pvContext);
417 HGSMIBUFFERLOCATION *p = (HGSMIBUFFERLOCATION *)pvData;
418
419 p->offLocation = PrimaryExtension->u.primary.cbVRAM - sizeof (HGSMIHOSTFLAGS);
420 p->cbLocation = sizeof (HGSMIHOSTFLAGS);
421
422 return VINF_SUCCESS;
423}
424
425
426static int vboxSetupAdapterInfoHGSMI (PDEVICE_EXTENSION PrimaryExtension)
427{
428 dprintf(("VBoxVideo::vboxSetupAdapterInfo\n"));
429
430 /* setup the flags first to ensure they are initialized by the time the host heap is ready */
431 int rc = vboxCallChannel(PrimaryExtension,
432 HGSMI_CH_HGSMI,
433 HGSMI_CC_HOST_FLAGS_LOCATION,
434 sizeof (HGSMIBUFFERLOCATION),
435 hgsmiInitFlagsLocation,
436 NULL,
437 NULL);
438 AssertRC(rc);
439 if(RT_SUCCESS (rc))
440 {
441#ifndef VBOXWDDM
442 rc = vboxCallVBVA (PrimaryExtension,
443 VBVA_INFO_VIEW,
444 sizeof (VBVAINFOVIEW) * PrimaryExtension->u.primary.cDisplays,
445 vbvaInitInfoDisplay,
446 NULL,
447 NULL);
448 AssertRC(rc);
449 if (RT_SUCCESS (rc))
450#else
451 /* in case of WDDM we do not control the framebuffer location,
452 * i.e. it is assigned by Video Memory Manager,
453 * The FB information should be passed to guest from our DxgkDdiSetVidPnSourceAddress callback */
454
455 /* Inform about caps */
456 rc = vboxCallVBVA (PrimaryExtension,
457 VBVA_INFO_CAPS,
458 sizeof (VBVACAPS),
459 vbvaInitInfoCaps,
460 vbvaFinalizeInfoCaps,
461 NULL);
462 AssertRC(rc);
463 if (RT_SUCCESS (rc))
464#endif
465 {
466 /* Report the host heap location. */
467 rc = vboxCallVBVA (PrimaryExtension,
468 VBVA_INFO_HEAP,
469 sizeof (VBVAINFOHEAP),
470 vbvaInitInfoHeap,
471 NULL,
472 NULL);
473 AssertRC(rc);
474 }
475 }
476
477
478 dprintf(("VBoxVideo::vboxSetupAdapterInfo finished rc = %d\n", rc));
479
480 return rc;
481}
482
483#ifndef VBOXWDDM
484VP_STATUS vboxWaitForSingleObjectVoid(IN PVOID HwDeviceExtension, IN PVOID Object, IN PLARGE_INTEGER Timeout OPTIONAL)
485{
486 return ERROR_INVALID_FUNCTION;
487}
488
489LONG vboxSetEventVoid(IN PVOID HwDeviceExtension, IN PEVENT pEvent)
490{
491 return 0;
492}
493
494VOID vboxClearEventVoid (IN PVOID HwDeviceExtension, IN PEVENT pEvent)
495{
496}
497
498VP_STATUS vboxCreateEventVoid(IN PVOID HwDeviceExtension, IN ULONG EventFlag, IN PVOID Unused, OUT PEVENT *ppEvent)
499{
500 return ERROR_INVALID_FUNCTION;
501}
502
503VP_STATUS vboxDeleteEventVoid(IN PVOID HwDeviceExtension, IN PEVENT pEvent)
504{
505 return ERROR_INVALID_FUNCTION;
506}
507
508VP_STATUS vboxCreateSpinLockVoid (IN PVOID HwDeviceExtension, OUT PSPIN_LOCK *SpinLock)
509{
510 return ERROR_INVALID_FUNCTION;
511}
512
513VP_STATUS vboxDeleteSpinLockVoid (IN PVOID HwDeviceExtension, IN PSPIN_LOCK SpinLock)
514{
515 return ERROR_INVALID_FUNCTION;
516}
517
518VOID vboxAcquireSpinLockVoid (IN PVOID HwDeviceExtension, IN PSPIN_LOCK SpinLock, OUT PUCHAR OldIrql)
519{
520}
521
522VOID vboxReleaseSpinLockVoid (IN PVOID HwDeviceExtension, IN PSPIN_LOCK SpinLock, IN UCHAR NewIrql)
523{
524}
525
526VOID vboxAcquireSpinLockAtDpcLevelVoid (IN PVOID HwDeviceExtension, IN PSPIN_LOCK SpinLock)
527{
528}
529
530VOID vboxReleaseSpinLockFromDpcLevelVoid (IN PVOID HwDeviceExtension, IN PSPIN_LOCK SpinLock)
531{
532}
533
534PVOID vboxAllocatePoolVoid(IN PVOID HwDeviceExtension, IN VBOXVP_POOL_TYPE PoolType, IN size_t NumberOfBytes, IN ULONG Tag)
535{
536 return NULL;
537}
538
539VOID vboxFreePoolVoid(IN PVOID HwDeviceExtension, IN PVOID Ptr)
540{
541}
542
543BOOLEAN vboxQueueDpcVoid(IN PVOID HwDeviceExtension, IN PMINIPORT_DPC_ROUTINE CallbackRoutine, IN PVOID Context)
544{
545 return FALSE;
546}
547
548void VBoxSetupVideoPortFunctions(PDEVICE_EXTENSION PrimaryExtension, VBOXVIDEOPORTPROCS *pCallbacks, PVIDEO_PORT_CONFIG_INFO pConfigInfo)
549{
550 memset(pCallbacks, 0, sizeof(VBOXVIDEOPORTPROCS));
551
552 if (vboxQueryWinVersion() <= WINNT4)
553 {
554 /* VideoPortGetProcAddress is available for >= win2k */
555 pCallbacks->pfnWaitForSingleObject = vboxWaitForSingleObjectVoid;
556 pCallbacks->pfnSetEvent = vboxSetEventVoid;
557 pCallbacks->pfnClearEvent = vboxClearEventVoid;
558 pCallbacks->pfnCreateEvent = vboxCreateEventVoid;
559 pCallbacks->pfnDeleteEvent = vboxDeleteEventVoid;
560 pCallbacks->pfnCreateSpinLock = vboxCreateSpinLockVoid;
561 pCallbacks->pfnDeleteSpinLock = vboxDeleteSpinLockVoid;
562 pCallbacks->pfnAcquireSpinLock = vboxAcquireSpinLockVoid;
563 pCallbacks->pfnReleaseSpinLock = vboxReleaseSpinLockVoid;
564 pCallbacks->pfnAcquireSpinLockAtDpcLevel = vboxAcquireSpinLockAtDpcLevelVoid;
565 pCallbacks->pfnReleaseSpinLockFromDpcLevel = vboxReleaseSpinLockFromDpcLevelVoid;
566 pCallbacks->pfnAllocatePool = vboxAllocatePoolVoid;
567 pCallbacks->pfnFreePool = vboxFreePoolVoid;
568 pCallbacks->pfnQueueDpc = vboxQueueDpcVoid;
569 return;
570 }
571
572 pCallbacks->pfnWaitForSingleObject = (PFNWAITFORSINGLEOBJECT)(pConfigInfo->VideoPortGetProcAddress)
573 (PrimaryExtension,
574 (PUCHAR)"VideoPortWaitForSingleObject");
575 Assert(pCallbacks->pfnWaitForSingleObject);
576
577 pCallbacks->pfnSetEvent = (PFNSETEVENT)(pConfigInfo->VideoPortGetProcAddress)
578 (PrimaryExtension,
579 (PUCHAR)"VideoPortSetEvent");
580 Assert(pCallbacks->pfnSetEvent);
581
582 pCallbacks->pfnClearEvent = (PFNCLEAREVENT)(pConfigInfo->VideoPortGetProcAddress)
583 (PrimaryExtension,
584 (PUCHAR)"VideoPortClearEvent");
585 Assert(pCallbacks->pfnClearEvent);
586
587 pCallbacks->pfnCreateEvent = (PFNCREATEEVENT)(pConfigInfo->VideoPortGetProcAddress)
588 (PrimaryExtension,
589 (PUCHAR)"VideoPortCreateEvent");
590 Assert(pCallbacks->pfnCreateEvent);
591
592 pCallbacks->pfnDeleteEvent = (PFNDELETEEVENT)(pConfigInfo->VideoPortGetProcAddress)
593 (PrimaryExtension,
594 (PUCHAR)"VideoPortDeleteEvent");
595 Assert(pCallbacks->pfnDeleteEvent);
596
597 if(pCallbacks->pfnWaitForSingleObject
598 && pCallbacks->pfnSetEvent
599 && pCallbacks->pfnClearEvent
600 && pCallbacks->pfnCreateEvent
601 && pCallbacks->pfnDeleteEvent)
602 {
603 pCallbacks->fSupportedTypes |= VBOXVIDEOPORTPROCS_EVENT;
604 }
605 else
606 {
607 pCallbacks->pfnWaitForSingleObject = vboxWaitForSingleObjectVoid;
608 pCallbacks->pfnSetEvent = vboxSetEventVoid;
609 pCallbacks->pfnClearEvent = vboxClearEventVoid;
610 pCallbacks->pfnCreateEvent = vboxCreateEventVoid;
611 pCallbacks->pfnDeleteEvent = vboxDeleteEventVoid;
612 }
613
614 pCallbacks->pfnCreateSpinLock = (PFNCREATESPINLOCK)(pConfigInfo->VideoPortGetProcAddress)
615 (PrimaryExtension,
616 (PUCHAR)"VideoPortCreateSpinLock");
617 Assert(pCallbacks->pfnCreateSpinLock);
618
619 pCallbacks->pfnDeleteSpinLock = (PFNDELETESPINLOCK)(pConfigInfo->VideoPortGetProcAddress)
620 (PrimaryExtension,
621 (PUCHAR)"VideoPortDeleteSpinLock");
622 Assert(pCallbacks->pfnDeleteSpinLock);
623
624 pCallbacks->pfnAcquireSpinLock = (PFNACQUIRESPINLOCK)(pConfigInfo->VideoPortGetProcAddress)
625 (PrimaryExtension,
626 (PUCHAR)"VideoPortAcquireSpinLock");
627 Assert(pCallbacks->pfnAcquireSpinLock);
628
629 pCallbacks->pfnReleaseSpinLock = (PFNRELEASESPINLOCK)(pConfigInfo->VideoPortGetProcAddress)
630 (PrimaryExtension,
631 (PUCHAR)"VideoPortReleaseSpinLock");
632 Assert(pCallbacks->pfnReleaseSpinLock);
633
634 pCallbacks->pfnAcquireSpinLockAtDpcLevel = (PFNACQUIRESPINLOCKATDPCLEVEL)(pConfigInfo->VideoPortGetProcAddress)
635 (PrimaryExtension,
636 (PUCHAR)"VideoPortAcquireSpinLockAtDpcLevel");
637 Assert(pCallbacks->pfnAcquireSpinLockAtDpcLevel);
638
639 pCallbacks->pfnReleaseSpinLockFromDpcLevel = (PFNRELEASESPINLOCKFROMDPCLEVEL)(pConfigInfo->VideoPortGetProcAddress)
640 (PrimaryExtension,
641 (PUCHAR)"VideoPortReleaseSpinLockFromDpcLevel");
642 Assert(pCallbacks->pfnReleaseSpinLockFromDpcLevel);
643
644 if(pCallbacks->pfnCreateSpinLock
645 && pCallbacks->pfnDeleteSpinLock
646 && pCallbacks->pfnAcquireSpinLock
647 && pCallbacks->pfnReleaseSpinLock
648 && pCallbacks->pfnAcquireSpinLockAtDpcLevel
649 && pCallbacks->pfnReleaseSpinLockFromDpcLevel)
650 {
651 pCallbacks->fSupportedTypes |= VBOXVIDEOPORTPROCS_SPINLOCK;
652 }
653 else
654 {
655 pCallbacks->pfnCreateSpinLock = vboxCreateSpinLockVoid;
656 pCallbacks->pfnDeleteSpinLock = vboxDeleteSpinLockVoid;
657 pCallbacks->pfnAcquireSpinLock = vboxAcquireSpinLockVoid;
658 pCallbacks->pfnReleaseSpinLock = vboxReleaseSpinLockVoid;
659 pCallbacks->pfnAcquireSpinLockAtDpcLevel = vboxAcquireSpinLockAtDpcLevelVoid;
660 pCallbacks->pfnReleaseSpinLockFromDpcLevel = vboxReleaseSpinLockFromDpcLevelVoid;
661 }
662
663 pCallbacks->pfnAllocatePool = (PFNALLOCATEPOOL)(pConfigInfo->VideoPortGetProcAddress)
664 (PrimaryExtension,
665 (PUCHAR)"VideoPortAllocatePool");
666 Assert(pCallbacks->pfnAllocatePool);
667
668 pCallbacks->pfnFreePool = (PFNFREEPOOL)(pConfigInfo->VideoPortGetProcAddress)
669 (PrimaryExtension,
670 (PUCHAR)"VideoPortFreePool");
671 Assert(pCallbacks->pfnFreePool);
672
673 if(pCallbacks->pfnAllocatePool
674 && pCallbacks->pfnFreePool)
675 {
676 pCallbacks->fSupportedTypes |= VBOXVIDEOPORTPROCS_POOL;
677 }
678 else
679 {
680 pCallbacks->pfnAllocatePool = vboxAllocatePoolVoid;
681 pCallbacks->pfnFreePool = vboxFreePoolVoid;
682 }
683
684 pCallbacks->pfnQueueDpc = (PFNQUEUEDPC)(pConfigInfo->VideoPortGetProcAddress)
685 (PrimaryExtension,
686 (PUCHAR)"VideoPortQueueDpc");
687 Assert(pCallbacks->pfnQueueDpc);
688
689 if(pCallbacks->pfnQueueDpc)
690 {
691 pCallbacks->fSupportedTypes |= VBOXVIDEOPORTPROCS_DPC;
692 }
693 else
694 {
695 pCallbacks->pfnQueueDpc = vboxQueueDpcVoid;
696 }
697
698#ifdef DEBUG_misha
699 Assert(pCallbacks->fSupportedTypes & VBOXVIDEOPORTPROCS_EVENT);
700 Assert(pCallbacks->fSupportedTypes & VBOXVIDEOPORTPROCS_SPINLOCK);
701#endif
702}
703#endif
704
705/**
706 * Helper function to register secondary displays (DualView). Note that this will not
707 * be available on pre-XP versions, and some editions on XP will fail because they are
708 * intentionally crippled.
709 *
710 * HGSMI variant is a bit different because it uses only HGSMI interface (VBVA channel)
711 * to talk to the host.
712 */
713VOID VBoxSetupDisplaysHGSMI(PDEVICE_EXTENSION PrimaryExtension,
714#ifndef VBOXWDDM
715 PVIDEO_PORT_CONFIG_INFO pConfigInfo,
716#endif
717 ULONG AdapterMemorySize)
718{
719 VP_STATUS rc = NO_ERROR;
720
721 dprintf(("VBoxVideo::VBoxSetupDisplays: PrimaryExtension = %p\n",
722 PrimaryExtension));
723
724 /* Preinitialize the primary extension.
725 * Note: bVBoxVideoSupported is set to FALSE, because HGSMI is active instead.
726 */
727 PrimaryExtension->pNext = NULL;
728#ifndef VBOXWDDM
729 PrimaryExtension->pPrimary = PrimaryExtension;
730 PrimaryExtension->iDevice = 0;
731 PrimaryExtension->ulFrameBufferOffset = 0;
732 PrimaryExtension->ulFrameBufferSize = 0;
733#endif
734 PrimaryExtension->u.primary.ulVbvaEnabled = 0;
735 PrimaryExtension->u.primary.bVBoxVideoSupported = FALSE;
736#ifndef VBOXWDDM
737 PrimaryExtension->u.primary.cDisplays = 1;
738#endif
739 PrimaryExtension->u.primary.cbVRAM = AdapterMemorySize;
740 PrimaryExtension->u.primary.cbMiniportHeap = 0;
741 PrimaryExtension->u.primary.pvMiniportHeap = NULL;
742 PrimaryExtension->u.primary.pvAdapterInformation = NULL;
743 PrimaryExtension->u.primary.ulMaxFrameBufferSize = 0;
744 PrimaryExtension->u.primary.bHGSMI = VBoxHGSMIIsSupported (PrimaryExtension);
745 VBoxVideoCmnMemZero(&PrimaryExtension->u.primary.areaHostHeap, sizeof(HGSMIAREA));
746 VBoxVideoCmnMemZero(&PrimaryExtension->areaDisplay, sizeof(HGSMIAREA));
747
748 if (PrimaryExtension->u.primary.IOPortGuest == 0)
749 {
750 PrimaryExtension->u.primary.bHGSMI = false;
751 }
752
753 if (PrimaryExtension->u.primary.bHGSMI)
754 {
755 /* Map the adapter information. It will be needed for HGSMI IO. */
756 rc = VBoxMapAdapterMemory (PrimaryExtension,
757 &PrimaryExtension->u.primary.pvAdapterInformation,
758 PrimaryExtension->u.primary.cbVRAM - VBVA_ADAPTER_INFORMATION_SIZE,
759 VBVA_ADAPTER_INFORMATION_SIZE
760 );
761 if (rc != NO_ERROR)
762 {
763 dprintf(("VBoxVideo::VBoxSetupDisplays: VBoxMapAdapterMemory pvAdapterInfoirrmation failed rc = %d\n",
764 rc));
765
766 PrimaryExtension->u.primary.bHGSMI = FALSE;
767 }
768 else
769 {
770 /* Setup a HGSMI heap within the adapter information area. */
771 rc = HGSMIHeapSetup (&PrimaryExtension->u.primary.hgsmiAdapterHeap,
772 PrimaryExtension->u.primary.pvAdapterInformation,
773 VBVA_ADAPTER_INFORMATION_SIZE - sizeof(HGSMIHOSTFLAGS),
774 PrimaryExtension->u.primary.cbVRAM - VBVA_ADAPTER_INFORMATION_SIZE,
775 false /*fOffsetBased*/);
776
777 if (RT_FAILURE (rc))
778 {
779 dprintf(("VBoxVideo::VBoxSetupDisplays: HGSMIHeapSetup failed rc = %d\n",
780 rc));
781
782 PrimaryExtension->u.primary.bHGSMI = FALSE;
783 }
784 else
785 {
786 PrimaryExtension->u.primary.pHostFlags = (HGSMIHOSTFLAGS*)(((uint8_t*)PrimaryExtension->u.primary.pvAdapterInformation)
787 + VBVA_ADAPTER_INFORMATION_SIZE - sizeof(HGSMIHOSTFLAGS));
788 }
789 }
790 }
791
792 /* Setup the host heap and the adapter memory. */
793 if (PrimaryExtension->u.primary.bHGSMI)
794 {
795 /* The miniport heap is used for the host buffers. */
796 ULONG cbMiniportHeap = 0;
797 vboxQueryConfHGSMI (PrimaryExtension, VBOX_VBVA_CONF32_HOST_HEAP_SIZE, &cbMiniportHeap);
798
799 if (cbMiniportHeap != 0)
800 {
801 /* Do not allow too big heap. No more than 25% of VRAM is allowed. */
802 ULONG cbMiniportHeapMaxSize = AdapterMemorySize / 4;
803
804 if (cbMiniportHeapMaxSize >= VBVA_ADAPTER_INFORMATION_SIZE)
805 {
806 cbMiniportHeapMaxSize -= VBVA_ADAPTER_INFORMATION_SIZE;
807 }
808
809 if (cbMiniportHeap > cbMiniportHeapMaxSize)
810 {
811 cbMiniportHeap = cbMiniportHeapMaxSize;
812 }
813
814 /* Round up to 4096 bytes. */
815 PrimaryExtension->u.primary.cbMiniportHeap = (cbMiniportHeap + 0xFFF) & ~0xFFF;
816
817 dprintf(("VBoxVideo::VBoxSetupDisplays: cbMiniportHeap = 0x%08X, PrimaryExtension->u.primary.cbMiniportHeap = 0x%08X, cbMiniportHeapMaxSize = 0x%08X\n",
818 cbMiniportHeap, PrimaryExtension->u.primary.cbMiniportHeap, cbMiniportHeapMaxSize));
819
820 /* Map the heap region.
821 *
822 * Note: the heap will be used for the host buffers submitted to the guest.
823 * The miniport driver is responsible for reading FIFO and notifying
824 * display drivers.
825 */
826 rc = VBoxMapAdapterMemory (PrimaryExtension,
827 &PrimaryExtension->u.primary.pvMiniportHeap,
828 PrimaryExtension->u.primary.cbVRAM
829 - VBVA_ADAPTER_INFORMATION_SIZE
830 - PrimaryExtension->u.primary.cbMiniportHeap,
831 PrimaryExtension->u.primary.cbMiniportHeap
832 );
833 if (rc != NO_ERROR)
834 {
835 PrimaryExtension->u.primary.pvMiniportHeap = NULL;
836 PrimaryExtension->u.primary.cbMiniportHeap = 0;
837 PrimaryExtension->u.primary.bHGSMI = FALSE;
838 }
839 else
840 {
841 HGSMIOFFSET offBase = PrimaryExtension->u.primary.cbVRAM
842 - VBVA_ADAPTER_INFORMATION_SIZE
843 - PrimaryExtension->u.primary.cbMiniportHeap;
844
845 /* Init the host hap area. Buffers from the host will be placed there. */
846 HGSMIAreaInitialize (&PrimaryExtension->u.primary.areaHostHeap,
847 PrimaryExtension->u.primary.pvMiniportHeap,
848 PrimaryExtension->u.primary.cbMiniportHeap,
849 offBase);
850 }
851 }
852 else
853 {
854 /* Host has not requested a heap. */
855 PrimaryExtension->u.primary.pvMiniportHeap = NULL;
856 PrimaryExtension->u.primary.cbMiniportHeap = 0;
857 }
858 }
859
860 /* Check whether the guest supports multimonitors. */
861 if (PrimaryExtension->u.primary.bHGSMI)
862 {
863#ifndef VBOXWDDM
864 typedef VP_STATUS (*PFNCREATESECONDARYDISPLAY)(PVOID, PVOID *, ULONG);
865 PFNCREATESECONDARYDISPLAY pfnCreateSecondaryDisplay = NULL;
866
867 /* Dynamically query the VideoPort import to be binary compatible across Windows versions */
868 if (vboxQueryWinVersion() > WINNT4)
869 {
870 /* This bluescreens on NT4, hence the above version check */
871 pfnCreateSecondaryDisplay = (PFNCREATESECONDARYDISPLAY)(pConfigInfo->VideoPortGetProcAddress)
872 (PrimaryExtension,
873 (PUCHAR)"VideoPortCreateSecondaryDisplay");
874 }
875
876 if (pfnCreateSecondaryDisplay != NULL)
877#endif
878 {
879 /* Query the configured number of displays. */
880 ULONG cDisplays = 0;
881 vboxQueryConfHGSMI (PrimaryExtension, VBOX_VBVA_CONF32_MONITOR_COUNT, &cDisplays);
882
883 dprintf(("VBoxVideo::VBoxSetupDisplays: cDisplays = %d\n",
884 cDisplays));
885
886 if (cDisplays == 0 || cDisplays > VBOX_VIDEO_MAX_SCREENS)
887 {
888 /* Host reported some bad value. Continue in the 1 screen mode. */
889 cDisplays = 1;
890 }
891
892#ifndef VBOXWDDM
893 PDEVICE_EXTENSION pPrev = PrimaryExtension;
894
895 ULONG iDisplay;
896 for (iDisplay = 1; iDisplay < cDisplays; iDisplay++)
897 {
898 PDEVICE_EXTENSION SecondaryExtension = NULL;
899 rc = pfnCreateSecondaryDisplay (PrimaryExtension, (PVOID*)&SecondaryExtension, VIDEO_DUALVIEW_REMOVABLE);
900
901 dprintf(("VBoxVideo::VBoxSetupDisplays: VideoPortCreateSecondaryDisplay returned %#x, SecondaryExtension = %p\n",
902 rc, SecondaryExtension));
903
904 if (rc != NO_ERROR)
905 {
906 break;
907 }
908
909 SecondaryExtension->pNext = NULL;
910 SecondaryExtension->pPrimary = PrimaryExtension;
911 SecondaryExtension->iDevice = iDisplay;
912 SecondaryExtension->ulFrameBufferOffset = 0;
913 SecondaryExtension->ulFrameBufferSize = 0;
914 SecondaryExtension->u.secondary.bEnabled = FALSE;
915
916 /* Update the list pointers. */
917 pPrev->pNext = SecondaryExtension;
918 pPrev = SecondaryExtension;
919
920 /* Take the successfully created display into account. */
921 PrimaryExtension->u.primary.cDisplays++;
922 }
923#else
924 /* simply store the number of monitors, we will deal with VidPN stuff later */
925 PrimaryExtension->u.primary.cDisplays = cDisplays;
926#endif
927 }
928
929 /* Failure to create secondary displays is not fatal */
930 rc = NO_ERROR;
931 }
932
933#ifndef VBOXWDDM
934 /* Now when the number of monitors is known and extensions are created,
935 * calculate the layout of framebuffers.
936 */
937 VBoxComputeFrameBufferSizes (PrimaryExtension);
938#endif
939
940 if (PrimaryExtension->u.primary.bHGSMI)
941 {
942 /* Setup the information for the host. */
943 rc = vboxSetupAdapterInfoHGSMI (PrimaryExtension);
944
945 if (RT_FAILURE (rc))
946 {
947 PrimaryExtension->u.primary.bHGSMI = FALSE;
948 }
949 }
950
951#ifdef VBOXWDDM
952 if (PrimaryExtension->u.primary.bHGSMI)
953 {
954 ULONG ulAvailable = PrimaryExtension->u.primary.cbVRAM
955 - PrimaryExtension->u.primary.cbMiniportHeap
956 - VBVA_ADAPTER_INFORMATION_SIZE;
957
958 ULONG ulSize = ulAvailable / 2;
959
960 if (ulSize > VBOXWDDM_C_VDMA_BUFFER_SIZE)
961 ulSize = VBOXWDDM_C_VDMA_BUFFER_SIZE;
962
963 /* Align down to 4096 bytes. */
964 ulSize &= ~0xFFF;
965 ULONG offset = ulAvailable - ulSize;
966
967 Assert(!(offset & 0xFFF));
968
969 rc = vboxVdmaCreate (PrimaryExtension, &PrimaryExtension->u.primary.Vdma, offset, ulSize);
970 AssertRC(rc);
971 if (RT_SUCCESS(rc))
972 {
973 /* can enable it right away since the host does not need any screen/FB info
974 * for basic DMA functionality */
975 rc = vboxVdmaEnable(PrimaryExtension, &PrimaryExtension->u.primary.Vdma);
976 AssertRC(rc);
977 if (RT_FAILURE(rc))
978 vboxVdmaDestroy(PrimaryExtension, &PrimaryExtension->u.primary.Vdma);
979 }
980
981#ifdef VBOXWDDM_RENDER_FROM_SHADOW
982 if (RT_SUCCESS(rc))
983 {
984 ulAvailable = offset;
985 ulSize = ulAvailable / 2;
986 ulSize /= PrimaryExtension->u.primary.cDisplays;
987 Assert(ulSize > VBVA_MIN_BUFFER_SIZE);
988 if (ulSize > VBVA_MIN_BUFFER_SIZE)
989 {
990 ULONG ulRatio = ulSize/VBVA_MIN_BUFFER_SIZE;
991 ulRatio >>= 4; /* /= 16; */
992 if (ulRatio)
993 ulSize = VBVA_MIN_BUFFER_SIZE * ulRatio;
994 else
995 ulSize = VBVA_MIN_BUFFER_SIZE;
996 }
997 else
998 {
999 /* todo: ?? */
1000 }
1001
1002 ulSize &= ~0xFFF;
1003 Assert(ulSize);
1004
1005 Assert(ulSize * PrimaryExtension->u.primary.cDisplays < ulAvailable);
1006
1007 for (int i = PrimaryExtension->u.primary.cDisplays-1; i >= 0; --i)
1008 {
1009 offset -= ulSize;
1010 rc = vboxVbvaCreate(PrimaryExtension, &PrimaryExtension->aSources[i].Vbva, offset, ulSize, i);
1011 AssertRC(rc);
1012 if (RT_SUCCESS(rc))
1013 {
1014 rc = vboxVbvaEnable(PrimaryExtension, &PrimaryExtension->aSources[i].Vbva);
1015 AssertRC(rc);
1016 if (RT_FAILURE(rc))
1017 {
1018 /* @todo: de-initialize */
1019 }
1020 }
1021 }
1022 }
1023#endif
1024
1025 if (RT_FAILURE(rc))
1026 PrimaryExtension->u.primary.bHGSMI = FALSE;
1027 }
1028#endif
1029
1030 if (!PrimaryExtension->u.primary.bHGSMI)
1031 {
1032 /* Unmap the memory if VBoxVideo is not supported. */
1033 VBoxUnmapAdapterMemory (PrimaryExtension, &PrimaryExtension->u.primary.pvMiniportHeap, PrimaryExtension->u.primary.cbMiniportHeap);
1034 VBoxUnmapAdapterMemory (PrimaryExtension, &PrimaryExtension->u.primary.pvAdapterInformation, VBVA_ADAPTER_INFORMATION_SIZE);
1035
1036 HGSMIHeapDestroy (&PrimaryExtension->u.primary.hgsmiAdapterHeap);
1037 }
1038
1039 if (PrimaryExtension->u.primary.bHGSMI)
1040 {
1041 VBoxVideoCmnSpinLockCreate(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock);
1042 }
1043
1044 dprintf(("VBoxVideo::VBoxSetupDisplays: finished\n"));
1045}
1046
1047#ifdef VBOXWDDM
1048int VBoxFreeDisplaysHGSMI(PDEVICE_EXTENSION PrimaryExtension)
1049{
1050 int rc = VINF_SUCCESS;
1051 for (int i = PrimaryExtension->u.primary.cDisplays-1; i >= 0; --i)
1052 {
1053 rc = vboxVbvaDisable(PrimaryExtension, &PrimaryExtension->aSources[i].Vbva);
1054 AssertRC(rc);
1055 if (RT_SUCCESS(rc))
1056 {
1057 rc = vboxVbvaDestroy(PrimaryExtension, &PrimaryExtension->aSources[i].Vbva);
1058 AssertRC(rc);
1059 if (RT_FAILURE(rc))
1060 {
1061 /* @todo: */
1062 }
1063 }
1064 }
1065
1066 rc = vboxVdmaDisable(PrimaryExtension, &PrimaryExtension->u.primary.Vdma);
1067 AssertRC(rc);
1068 if (RT_SUCCESS(rc))
1069 {
1070 rc = vboxVdmaDestroy(PrimaryExtension, &PrimaryExtension->u.primary.Vdma);
1071 AssertRC(rc);
1072 if (RT_SUCCESS(rc))
1073 {
1074 /*rc = */VBoxUnmapAdapterMemory(PrimaryExtension, &PrimaryExtension->u.primary.pvMiniportHeap, PrimaryExtension->u.primary.cbMiniportHeap);
1075/*
1076 AssertRC(rc);
1077 if (RT_SUCCESS(rc))
1078*/
1079 {
1080 HGSMIHeapDestroy(&PrimaryExtension->u.primary.hgsmiAdapterHeap);
1081
1082 /* Map the adapter information. It will be needed for HGSMI IO. */
1083 /*rc = */VBoxUnmapAdapterMemory(PrimaryExtension, &PrimaryExtension->u.primary.pvAdapterInformation, VBVA_ADAPTER_INFORMATION_SIZE);
1084/*
1085 AssertRC(rc);
1086 if (RT_FAILURE(rc))
1087 drprintf((__FUNCTION__"VBoxUnmapAdapterMemory PrimaryExtension->u.primary.pvAdapterInformation failed, rc(%d)\n", rc));
1088*/
1089
1090 }
1091 }
1092 }
1093
1094 return rc;
1095}
1096#endif
1097
1098/*
1099 * Send the pointer shape to the host.
1100 */
1101typedef struct _MOUSEPOINTERSHAPECTX
1102{
1103 VIDEO_POINTER_ATTRIBUTES *pPointerAttr;
1104 uint32_t cbData;
1105 int32_t i32Result;
1106} MOUSEPOINTERSHAPECTX;
1107
1108static int vbvaInitMousePointerShape (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
1109{
1110 NOREF (PrimaryExtension);
1111
1112 MOUSEPOINTERSHAPECTX *pCtx = (MOUSEPOINTERSHAPECTX *)pvContext;
1113 VBVAMOUSEPOINTERSHAPE *p = (VBVAMOUSEPOINTERSHAPE *)pvData;
1114
1115 /* Will be updated by the host. */
1116 p->i32Result = VINF_SUCCESS;
1117
1118 /* We have our custom flags in the field */
1119 p->fu32Flags = pCtx->pPointerAttr->Enable & 0x0000FFFF;
1120
1121 p->u32HotX = (pCtx->pPointerAttr->Enable >> 16) & 0xFF;
1122 p->u32HotY = (pCtx->pPointerAttr->Enable >> 24) & 0xFF;
1123 p->u32Width = pCtx->pPointerAttr->Width;
1124 p->u32Height = pCtx->pPointerAttr->Height;
1125
1126 if (p->fu32Flags & VBOX_MOUSE_POINTER_SHAPE)
1127 {
1128 /* If shape is supplied, then alway create the pointer visible.
1129 * See comments in 'vboxUpdatePointerShape'
1130 */
1131 p->fu32Flags |= VBOX_MOUSE_POINTER_VISIBLE;
1132
1133 /* Copy the actual pointer data. */
1134 memcpy (p->au8Data, pCtx->pPointerAttr->Pixels, pCtx->cbData);
1135 }
1136
1137 return VINF_SUCCESS;
1138}
1139
1140static int vbvaFinalizeMousePointerShape (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
1141{
1142 NOREF (PrimaryExtension);
1143
1144 MOUSEPOINTERSHAPECTX *pCtx = (MOUSEPOINTERSHAPECTX *)pvContext;
1145 VBVAMOUSEPOINTERSHAPE *p = (VBVAMOUSEPOINTERSHAPE *)pvData;
1146
1147 pCtx->i32Result = p->i32Result;
1148
1149 return VINF_SUCCESS;
1150}
1151
1152BOOLEAN vboxUpdatePointerShape (PDEVICE_EXTENSION DeviceExtension,
1153 PVIDEO_POINTER_ATTRIBUTES pointerAttr,
1154 uint32_t cbLength)
1155{
1156#ifndef VBOXWDDM
1157 PDEVICE_EXTENSION PrimaryExtension = DeviceExtension->pPrimary;
1158
1159 /* In multimonitor case the HW mouse pointer is the same on all screens,
1160 * and Windows calls each display driver with the same pointer data: visible for
1161 * the screen where the pointer is and invisible for other screens.
1162 *
1163 * This driver passes the shape to the host only from primary screen and makes
1164 * the pointer always visible (in vbvaInitMousePointerShape).
1165 *
1166 * The simple solution makes it impossible to create the shape and keep the mouse
1167 * pointer invisible. New shapes will be created visible.
1168 * But:
1169 * 1) VBox frontends actually ignore the visibility flag if VBOX_MOUSE_POINTER_SHAPE
1170 * is set and always create new pointers visible.
1171 * 2) Windows uses DrvMovePointer to hide the pointer, which will still work.
1172 */
1173
1174 if (DeviceExtension->iDevice != PrimaryExtension->iDevice)
1175 {
1176 dprintf(("vboxUpdatePointerShape: ignore non primary device %d(%d)\n",
1177 DeviceExtension->iDevice, PrimaryExtension->iDevice));
1178 /* Success. */
1179 return TRUE;
1180 }
1181#else
1182 PDEVICE_EXTENSION PrimaryExtension = DeviceExtension;
1183#endif
1184
1185 uint32_t cbData = 0;
1186
1187 if (pointerAttr->Enable & VBOX_MOUSE_POINTER_SHAPE)
1188 {
1189 /* Size of the pointer data: sizeof (AND mask) + sizeof (XOR_MASK) */
1190 cbData = ((((pointerAttr->Width + 7) / 8) * pointerAttr->Height + 3) & ~3)
1191 + pointerAttr->Width * 4 * pointerAttr->Height;
1192 }
1193
1194#ifndef DEBUG_misha
1195 dprintf(("vboxUpdatePointerShape: cbData %d, %dx%d\n",
1196 cbData, pointerAttr->Width, pointerAttr->Height));
1197#endif
1198
1199 if (cbData > cbLength - sizeof(VIDEO_POINTER_ATTRIBUTES))
1200 {
1201 dprintf(("vboxUpdatePointerShape: calculated pointer data size is too big (%d bytes, limit %d)\n",
1202 cbData, cbLength - sizeof(VIDEO_POINTER_ATTRIBUTES)));
1203 return FALSE;
1204 }
1205
1206 MOUSEPOINTERSHAPECTX ctx;
1207
1208 ctx.pPointerAttr = pointerAttr;
1209 ctx.cbData = cbData;
1210 ctx.i32Result = VERR_NOT_SUPPORTED;
1211
1212 int rc = vboxCallVBVA (PrimaryExtension,
1213 VBVA_MOUSE_POINTER_SHAPE,
1214 sizeof (VBVAMOUSEPOINTERSHAPE) + cbData,
1215 vbvaInitMousePointerShape,
1216 vbvaFinalizeMousePointerShape,
1217 &ctx);
1218#ifndef DEBUG_misha
1219 dprintf(("VBoxVideo::vboxMousePointerShape: rc %d, i32Result = %d\n", rc, ctx.i32Result));
1220#endif
1221
1222 return RT_SUCCESS(rc) && RT_SUCCESS(ctx.i32Result);
1223}
1224
1225typedef struct _VBVAMINIPORT_CHANNELCONTEXT
1226{
1227 PFNHGSMICHANNELHANDLER pfnChannelHandler;
1228 void *pvChannelHandler;
1229}VBVAMINIPORT_CHANNELCONTEXT;
1230
1231typedef struct _VBVADISP_CHANNELCONTEXT
1232{
1233 struct _VBVAHOSTCMD * pFirstCmd;
1234 struct _VBVAHOSTCMD * pLastCmd;
1235 VBOXVCMNSPIN_LOCK pSynchLock;
1236#ifdef DEBUG
1237 int cCmds;
1238#endif
1239 bool bValid;
1240}VBVADISP_CHANNELCONTEXT;
1241
1242#ifdef DEBUG
1243void dbgCheckListLocked(const VBVADISP_CHANNELCONTEXT *pList, struct _VBVAHOSTCMD * pCmd)
1244{
1245 int counter = 0;
1246 for(struct _VBVAHOSTCMD * pCur = pList->pFirstCmd; pCur; pCur=pCur->u.pNext)
1247 {
1248 Assert(pCur != pCmd);
1249 if(pCur == pList->pLastCmd)
1250 {
1251 Assert(pCur->u.pNext == NULL);
1252 }
1253 if(pCur->u.pNext == NULL)
1254 {
1255 Assert(pCur == pList->pLastCmd);
1256 }
1257 counter++;
1258 }
1259
1260 Assert(counter == pList->cCmds);
1261}
1262
1263void dbgCheckList(PDEVICE_EXTENSION PrimaryExtension, VBVADISP_CHANNELCONTEXT *pList, struct _VBVAHOSTCMD * pCmd)
1264{
1265 VBOXVCMNIRQL oldIrql;
1266 VBoxVideoCmnSpinLockAcquire(PrimaryExtension, &pList->pSynchLock, &oldIrql);
1267
1268 dbgCheckListLocked(pList, pCmd);
1269
1270 VBoxVideoCmnSpinLockRelease(PrimaryExtension, &pList->pSynchLock, oldIrql);
1271}
1272
1273#define DBG_CHECKLIST_LOCKED(_pl, pc) dbgCheckListLocked(_pl, pc)
1274#define DBG_CHECKLIST(_pe, _pl, pc) dbgCheckList(_pe, _pl, pc)
1275
1276#else
1277#define DBG_CHECKLIST_LOCKED(_pl, pc) do{}while(0)
1278#define DBG_CHECKLIST(_pe, _pl, pc) do{}while(0)
1279#endif
1280
1281
1282typedef struct _VBVA_CHANNELCONTEXTS
1283{
1284 PDEVICE_EXTENSION PrimaryExtension;
1285 uint32_t cUsed;
1286 uint32_t cContexts;
1287 VBVAMINIPORT_CHANNELCONTEXT mpContext;
1288 VBVADISP_CHANNELCONTEXT aContexts[1];
1289}VBVA_CHANNELCONTEXTS;
1290
1291static int vboxVBVADeleteChannelContexts(PDEVICE_EXTENSION PrimaryExtension, VBVA_CHANNELCONTEXTS * pContext)
1292{
1293 VBoxVideoCmnMemFree(PrimaryExtension,pContext);
1294 return VINF_SUCCESS;
1295}
1296
1297static int vboxVBVACreateChannelContexts(PDEVICE_EXTENSION PrimaryExtension, VBVA_CHANNELCONTEXTS ** ppContext)
1298{
1299#ifndef VBOXWDDM
1300 uint32_t cDisplays = (uint32_t)PrimaryExtension->u.primary.cDisplays;
1301#else
1302 uint32_t cDisplays = (uint32_t)PrimaryExtension->u.primary.cDisplays;
1303#endif
1304 const size_t size = RT_OFFSETOF(VBVA_CHANNELCONTEXTS, aContexts[cDisplays]);
1305 VBVA_CHANNELCONTEXTS * pContext = (VBVA_CHANNELCONTEXTS*)VBoxVideoCmnMemAllocNonPaged(PrimaryExtension, size, MEM_TAG);
1306 if(pContext)
1307 {
1308 memset(pContext, 0, size);
1309 pContext->cContexts = cDisplays;
1310 pContext->PrimaryExtension = PrimaryExtension;
1311 *ppContext = pContext;
1312 return VINF_SUCCESS;
1313 }
1314 return VERR_GENERAL_FAILURE;
1315}
1316
1317static VBVADISP_CHANNELCONTEXT* vboxVBVAFindHandlerInfo(VBVA_CHANNELCONTEXTS *pCallbacks, int iId)
1318{
1319 if(iId < 0)
1320 {
1321 return NULL;
1322 }
1323 else if(pCallbacks->cContexts > (uint32_t)iId)
1324 {
1325 return &pCallbacks->aContexts[iId];
1326 }
1327 return NULL;
1328}
1329
1330#ifndef VBOXWDDM
1331DECLCALLBACK(void) hgsmiHostCmdComplete (HVBOXVIDEOHGSMI hHGSMI, struct _VBVAHOSTCMD * pCmd)
1332{
1333 PDEVICE_EXTENSION PrimaryExtension = ((PDEVICE_EXTENSION)hHGSMI)->pPrimary;
1334 HGSMIHostCmdComplete (PrimaryExtension, pCmd);
1335}
1336
1337DECLCALLBACK(int) hgsmiHostCmdRequest (HVBOXVIDEOHGSMI hHGSMI, uint8_t u8Channel, struct _VBVAHOSTCMD ** ppCmd)
1338{
1339// if(display < 0)
1340// return VERR_INVALID_PARAMETER;
1341 if(!ppCmd)
1342 return VERR_INVALID_PARAMETER;
1343
1344 PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)hHGSMI;
1345 PDEVICE_EXTENSION PrimaryExtension = pDevExt->pPrimary;
1346
1347 /* pick up the host commands */
1348 VBoxVideoHGSMIDpc(PrimaryExtension, NULL);
1349
1350 HGSMICHANNEL * pChannel = HGSMIChannelFindById (&PrimaryExtension->u.primary.channels, u8Channel);
1351 if(pChannel)
1352 {
1353 VBVA_CHANNELCONTEXTS * pContexts = (VBVA_CHANNELCONTEXTS *)pChannel->handler.pvHandler;
1354 VBVADISP_CHANNELCONTEXT *pDispContext = vboxVBVAFindHandlerInfo(pContexts, pDevExt->iDevice);
1355 Assert(pDispContext);
1356 if(pDispContext)
1357 {
1358 UCHAR oldIrql;
1359 VBoxVideoCmnSpinLockAcquire(PrimaryExtension, &pDispContext->pSynchLock, &oldIrql);
1360
1361 DBG_CHECKLIST_LOCKED(pDispContext, NULL);
1362
1363 *ppCmd = pDispContext->pFirstCmd;
1364 pDispContext->pFirstCmd = NULL;
1365 pDispContext->pLastCmd = NULL;
1366#ifdef DEBUG
1367 pDispContext->cCmds = 0;
1368#endif
1369 VBoxVideoCmnSpinLockRelease(PrimaryExtension, &pDispContext->pSynchLock, oldIrql);
1370
1371 DBG_CHECKLIST(PrimaryExtension, pDispContext, NULL);
1372
1373 return VINF_SUCCESS;
1374 }
1375 }
1376
1377 return VERR_INVALID_PARAMETER;
1378}
1379#endif
1380
1381static DECLCALLBACK(int) vboxVBVAChannelGenericHandler(void *pvHandler, uint16_t u16ChannelInfo, void *pvBuffer, HGSMISIZE cbBuffer)
1382{
1383 VBVA_CHANNELCONTEXTS *pCallbacks = (VBVA_CHANNELCONTEXTS*)pvHandler;
1384// Assert(0);
1385 Assert(cbBuffer > VBVAHOSTCMD_HDRSIZE);
1386 if(cbBuffer > VBVAHOSTCMD_HDRSIZE)
1387 {
1388 VBVAHOSTCMD *pHdr = (VBVAHOSTCMD*)pvBuffer;
1389 Assert(pHdr->iDstID >= 0);
1390 if(pHdr->iDstID >= 0)
1391 {
1392 VBVADISP_CHANNELCONTEXT* pHandler = vboxVBVAFindHandlerInfo(pCallbacks, pHdr->iDstID);
1393 Assert(pHandler && pHandler->bValid);
1394 if(pHandler && pHandler->bValid)
1395 {
1396 VBVAHOSTCMD *pFirst = NULL, *pLast = NULL;
1397 for(VBVAHOSTCMD *pCur = pHdr; pCur; )
1398 {
1399 Assert(!pCur->u.Data);
1400 Assert(!pFirst);
1401 Assert(!pLast);
1402
1403 switch(u16ChannelInfo)
1404 {
1405 case VBVAHG_DISPLAY_CUSTOM:
1406 {
1407 if(pLast)
1408 {
1409 pLast->u.pNext = pCur;
1410 pLast = pCur;
1411 }
1412 else
1413 {
1414 pFirst = pCur;
1415 pLast = pCur;
1416 }
1417 Assert(!pCur->u.Data);
1418 //TODO: use offset here
1419 pCur = pCur->u.pNext;
1420 Assert(!pCur);
1421 Assert(pFirst);
1422 Assert(pFirst == pLast);
1423 break;
1424 }
1425 case VBVAHG_EVENT:
1426 {
1427 VBVAHOSTCMDEVENT *pEventCmd = VBVAHOSTCMD_BODY(pCur, VBVAHOSTCMDEVENT);
1428#ifndef VBOXWDDM
1429 PEVENT pEvent = (PEVENT)pEventCmd->pEvent;
1430 pCallbacks->PrimaryExtension->u.primary.VideoPortProcs.pfnSetEvent(
1431 pCallbacks->PrimaryExtension,
1432 pEvent);
1433#else
1434 PKEVENT pEvent = (PKEVENT)pEventCmd->pEvent;
1435 KeSetEvent(pEvent, 0, FALSE);
1436#endif
1437 }
1438 default:
1439 {
1440 DBG_CHECKLIST(pCallbacks->PrimaryExtension, pHandler, pCur);
1441 Assert(u16ChannelInfo==VBVAHG_EVENT);
1442 Assert(!pCur->u.Data);
1443 //TODO: use offset here
1444 if(pLast)
1445 pLast->u.pNext = pCur->u.pNext;
1446 VBVAHOSTCMD * pNext = pCur->u.pNext;
1447 pCur->u.pNext = NULL;
1448 HGSMIHostCmdComplete(pCallbacks->PrimaryExtension, pCur);
1449 pCur = pNext;
1450 Assert(!pCur);
1451 Assert(!pFirst);
1452 Assert(pFirst == pLast);
1453 break;
1454 }
1455 }
1456 }
1457
1458 DBG_CHECKLIST(pCallbacks->PrimaryExtension, pHandler, pFirst);
1459
1460 /* we do not support lists currently */
1461 Assert(pFirst == pLast);
1462 if(pLast)
1463 {
1464 Assert(pLast->u.pNext == NULL);
1465 }
1466
1467 if(pFirst)
1468 {
1469 Assert(pLast);
1470 UCHAR oldIrql;
1471 VBoxVideoCmnSpinLockAcquire(pCallbacks->PrimaryExtension,
1472 &pHandler->pSynchLock,
1473 &oldIrql);
1474
1475 DBG_CHECKLIST_LOCKED(pHandler, pFirst);
1476
1477 if(pHandler->pLastCmd)
1478 {
1479 pHandler->pLastCmd->u.pNext = pFirst;
1480 Assert(pHandler->pFirstCmd);
1481 }
1482 else
1483 {
1484 Assert(!pHandler->pFirstCmd);
1485 pHandler->pFirstCmd = pFirst;
1486 }
1487 pHandler->pLastCmd = pLast;
1488#ifdef DEBUG
1489 pHandler->cCmds++;
1490#endif
1491 DBG_CHECKLIST_LOCKED(pHandler, NULL);
1492
1493 VBoxVideoCmnSpinLockRelease(pCallbacks->PrimaryExtension,
1494 &pHandler->pSynchLock,
1495 oldIrql);
1496 }
1497 else
1498 {
1499 Assert(!pLast);
1500 }
1501 return VINF_SUCCESS;
1502 }
1503 }
1504 else
1505 {
1506 //TODO: impl
1507// HGSMIMINIPORT_CHANNELCONTEXT *pHandler = vboxVideoHGSMIFindHandler;
1508// if(pHandler && pHandler->pfnChannelHandler)
1509// {
1510// pHandler->pfnChannelHandler(pHandler->pvChannelHandler, u16ChannelInfo, pHdr, cbBuffer);
1511//
1512// return VINF_SUCCESS;
1513// }
1514 }
1515 }
1516 /* no handlers were found, need to complete the command here */
1517 HGSMIHostCmdComplete(pCallbacks->PrimaryExtension, pvBuffer);
1518 return VINF_SUCCESS;
1519}
1520
1521static HGSMICHANNELHANDLER g_OldHandler;
1522
1523int vboxVBVAChannelDisplayEnable(PDEVICE_EXTENSION PrimaryExtension,
1524 int iDisplay, /* negative would mean this is a miniport handler */
1525 uint8_t u8Channel)
1526{
1527 VBVA_CHANNELCONTEXTS * pContexts;
1528 HGSMICHANNEL * pChannel = HGSMIChannelFindById (&PrimaryExtension->u.primary.channels, u8Channel);
1529 if(!pChannel)
1530 {
1531 int rc = vboxVBVACreateChannelContexts(PrimaryExtension, &pContexts);
1532 if(RT_FAILURE(rc))
1533 {
1534 return rc;
1535 }
1536 }
1537 else
1538 {
1539 pContexts = (VBVA_CHANNELCONTEXTS *)pChannel->handler.pvHandler;
1540 }
1541
1542 VBVADISP_CHANNELCONTEXT *pDispContext = vboxVBVAFindHandlerInfo(pContexts, iDisplay);
1543 Assert(pDispContext);
1544 if(pDispContext)
1545 {
1546#ifdef DEBUGVHWASTRICT
1547 Assert(!pDispContext->bValid);
1548#endif
1549 Assert(!pDispContext->pFirstCmd);
1550 Assert(!pDispContext->pLastCmd);
1551 if(!pDispContext->bValid)
1552 {
1553 pDispContext->bValid = true;
1554 pDispContext->pFirstCmd = NULL;
1555 pDispContext->pLastCmd= NULL;
1556#ifdef DEBUG
1557 pDispContext->cCmds = 0;
1558#endif
1559
1560 VBoxVideoCmnSpinLockCreate(PrimaryExtension, &pDispContext->pSynchLock);
1561
1562 int rc = VINF_SUCCESS;
1563 if(!pChannel)
1564 {
1565 rc = HGSMIChannelRegister (&PrimaryExtension->u.primary.channels,
1566 u8Channel,
1567 "VGA Miniport HGSMI channel",
1568 vboxVBVAChannelGenericHandler,
1569 pContexts,
1570 &g_OldHandler);
1571 }
1572
1573 if(RT_SUCCESS(rc))
1574 {
1575 pContexts->cUsed++;
1576 return VINF_SUCCESS;
1577 }
1578 }
1579 }
1580
1581 if(!pChannel)
1582 {
1583 vboxVBVADeleteChannelContexts(PrimaryExtension, pContexts);
1584 }
1585
1586 return VERR_GENERAL_FAILURE;
1587}
1588
1589/** @todo Mouse pointer position to be read from VMMDev memory, address of the memory region
1590 * can be queried from VMMDev via an IOCTL. This VMMDev memory region will contain
1591 * host information which is needed by the guest.
1592 *
1593 * Reading will not cause a switch to the host.
1594 *
1595 * Have to take into account:
1596 * * synchronization: host must write to the memory only from EMT,
1597 * large structures must be read under flag, which tells the host
1598 * that the guest is currently reading the memory (OWNER flag?).
1599 * * guest writes: may be allocate a page for the host info and make
1600 * the page readonly for the guest.
1601 * * the information should be available only for additions drivers.
1602 * * VMMDev additions driver will inform the host which version of the info it expects,
1603 * host must support all versions.
1604 *
1605 */
1606
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette