VirtualBox

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

Last change on this file since 31979 was 31873, checked in by vboxsync, 14 years ago

VBoxVideo: When unmapping adapter memory, make sure interrupt handler will not blow up.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.4 KB
Line 
1/* $Id: VBoxVideoHGSMI.cpp 31873 2010-08-23 16:26: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.pHostFlags = NULL;
744 PrimaryExtension->u.primary.ulMaxFrameBufferSize = 0;
745 PrimaryExtension->u.primary.bHGSMI = VBoxHGSMIIsSupported (PrimaryExtension);
746 VBoxVideoCmnMemZero(&PrimaryExtension->u.primary.areaHostHeap, sizeof(HGSMIAREA));
747 VBoxVideoCmnMemZero(&PrimaryExtension->areaDisplay, sizeof(HGSMIAREA));
748
749 if (PrimaryExtension->u.primary.IOPortGuest == 0)
750 {
751 PrimaryExtension->u.primary.bHGSMI = false;
752 }
753
754 if (PrimaryExtension->u.primary.bHGSMI)
755 {
756 /* Map the adapter information. It will be needed for HGSMI IO. */
757 rc = VBoxMapAdapterMemory (PrimaryExtension,
758 &PrimaryExtension->u.primary.pvAdapterInformation,
759 PrimaryExtension->u.primary.cbVRAM - VBVA_ADAPTER_INFORMATION_SIZE,
760 VBVA_ADAPTER_INFORMATION_SIZE
761 );
762 if (rc != NO_ERROR)
763 {
764 dprintf(("VBoxVideo::VBoxSetupDisplays: VBoxMapAdapterMemory pvAdapterInfoirrmation failed rc = %d\n",
765 rc));
766
767 PrimaryExtension->u.primary.bHGSMI = FALSE;
768 }
769 else
770 {
771 /* Setup a HGSMI heap within the adapter information area. */
772 rc = HGSMIHeapSetup (&PrimaryExtension->u.primary.hgsmiAdapterHeap,
773 PrimaryExtension->u.primary.pvAdapterInformation,
774 VBVA_ADAPTER_INFORMATION_SIZE - sizeof(HGSMIHOSTFLAGS),
775 PrimaryExtension->u.primary.cbVRAM - VBVA_ADAPTER_INFORMATION_SIZE,
776 false /*fOffsetBased*/);
777
778 if (RT_FAILURE (rc))
779 {
780 dprintf(("VBoxVideo::VBoxSetupDisplays: HGSMIHeapSetup failed rc = %d\n",
781 rc));
782
783 PrimaryExtension->u.primary.bHGSMI = FALSE;
784 }
785 else
786 {
787 PrimaryExtension->u.primary.pHostFlags = (HGSMIHOSTFLAGS*)(((uint8_t*)PrimaryExtension->u.primary.pvAdapterInformation)
788 + VBVA_ADAPTER_INFORMATION_SIZE - sizeof(HGSMIHOSTFLAGS));
789 }
790 }
791 }
792
793 /* Setup the host heap and the adapter memory. */
794 if (PrimaryExtension->u.primary.bHGSMI)
795 {
796 /* The miniport heap is used for the host buffers. */
797 ULONG cbMiniportHeap = 0;
798 vboxQueryConfHGSMI (PrimaryExtension, VBOX_VBVA_CONF32_HOST_HEAP_SIZE, &cbMiniportHeap);
799
800 if (cbMiniportHeap != 0)
801 {
802 /* Do not allow too big heap. No more than 25% of VRAM is allowed. */
803 ULONG cbMiniportHeapMaxSize = AdapterMemorySize / 4;
804
805 if (cbMiniportHeapMaxSize >= VBVA_ADAPTER_INFORMATION_SIZE)
806 {
807 cbMiniportHeapMaxSize -= VBVA_ADAPTER_INFORMATION_SIZE;
808 }
809
810 if (cbMiniportHeap > cbMiniportHeapMaxSize)
811 {
812 cbMiniportHeap = cbMiniportHeapMaxSize;
813 }
814
815 /* Round up to 4096 bytes. */
816 PrimaryExtension->u.primary.cbMiniportHeap = (cbMiniportHeap + 0xFFF) & ~0xFFF;
817
818 dprintf(("VBoxVideo::VBoxSetupDisplays: cbMiniportHeap = 0x%08X, PrimaryExtension->u.primary.cbMiniportHeap = 0x%08X, cbMiniportHeapMaxSize = 0x%08X\n",
819 cbMiniportHeap, PrimaryExtension->u.primary.cbMiniportHeap, cbMiniportHeapMaxSize));
820
821 /* Map the heap region.
822 *
823 * Note: the heap will be used for the host buffers submitted to the guest.
824 * The miniport driver is responsible for reading FIFO and notifying
825 * display drivers.
826 */
827 rc = VBoxMapAdapterMemory (PrimaryExtension,
828 &PrimaryExtension->u.primary.pvMiniportHeap,
829 PrimaryExtension->u.primary.cbVRAM
830 - VBVA_ADAPTER_INFORMATION_SIZE
831 - PrimaryExtension->u.primary.cbMiniportHeap,
832 PrimaryExtension->u.primary.cbMiniportHeap
833 );
834 if (rc != NO_ERROR)
835 {
836 PrimaryExtension->u.primary.pvMiniportHeap = NULL;
837 PrimaryExtension->u.primary.cbMiniportHeap = 0;
838 PrimaryExtension->u.primary.bHGSMI = FALSE;
839 }
840 else
841 {
842 HGSMIOFFSET offBase = PrimaryExtension->u.primary.cbVRAM
843 - VBVA_ADAPTER_INFORMATION_SIZE
844 - PrimaryExtension->u.primary.cbMiniportHeap;
845
846 /* Init the host hap area. Buffers from the host will be placed there. */
847 HGSMIAreaInitialize (&PrimaryExtension->u.primary.areaHostHeap,
848 PrimaryExtension->u.primary.pvMiniportHeap,
849 PrimaryExtension->u.primary.cbMiniportHeap,
850 offBase);
851 }
852 }
853 else
854 {
855 /* Host has not requested a heap. */
856 PrimaryExtension->u.primary.pvMiniportHeap = NULL;
857 PrimaryExtension->u.primary.cbMiniportHeap = 0;
858 }
859 }
860
861 /* Check whether the guest supports multimonitors. */
862 if (PrimaryExtension->u.primary.bHGSMI)
863 {
864#ifndef VBOXWDDM
865 typedef VP_STATUS (*PFNCREATESECONDARYDISPLAY)(PVOID, PVOID *, ULONG);
866 PFNCREATESECONDARYDISPLAY pfnCreateSecondaryDisplay = NULL;
867
868 /* Dynamically query the VideoPort import to be binary compatible across Windows versions */
869 if (vboxQueryWinVersion() > WINNT4)
870 {
871 /* This bluescreens on NT4, hence the above version check */
872 pfnCreateSecondaryDisplay = (PFNCREATESECONDARYDISPLAY)(pConfigInfo->VideoPortGetProcAddress)
873 (PrimaryExtension,
874 (PUCHAR)"VideoPortCreateSecondaryDisplay");
875 }
876
877 if (pfnCreateSecondaryDisplay != NULL)
878#endif
879 {
880 /* Query the configured number of displays. */
881 ULONG cDisplays = 0;
882 vboxQueryConfHGSMI (PrimaryExtension, VBOX_VBVA_CONF32_MONITOR_COUNT, &cDisplays);
883
884 dprintf(("VBoxVideo::VBoxSetupDisplays: cDisplays = %d\n",
885 cDisplays));
886
887 if (cDisplays == 0 || cDisplays > VBOX_VIDEO_MAX_SCREENS)
888 {
889 /* Host reported some bad value. Continue in the 1 screen mode. */
890 cDisplays = 1;
891 }
892
893#ifndef VBOXWDDM
894 PDEVICE_EXTENSION pPrev = PrimaryExtension;
895
896 ULONG iDisplay;
897 for (iDisplay = 1; iDisplay < cDisplays; iDisplay++)
898 {
899 PDEVICE_EXTENSION SecondaryExtension = NULL;
900 rc = pfnCreateSecondaryDisplay (PrimaryExtension, (PVOID*)&SecondaryExtension, VIDEO_DUALVIEW_REMOVABLE);
901
902 dprintf(("VBoxVideo::VBoxSetupDisplays: VideoPortCreateSecondaryDisplay returned %#x, SecondaryExtension = %p\n",
903 rc, SecondaryExtension));
904
905 if (rc != NO_ERROR)
906 {
907 break;
908 }
909
910 SecondaryExtension->pNext = NULL;
911 SecondaryExtension->pPrimary = PrimaryExtension;
912 SecondaryExtension->iDevice = iDisplay;
913 SecondaryExtension->ulFrameBufferOffset = 0;
914 SecondaryExtension->ulFrameBufferSize = 0;
915 SecondaryExtension->u.secondary.bEnabled = FALSE;
916
917 /* Update the list pointers. */
918 pPrev->pNext = SecondaryExtension;
919 pPrev = SecondaryExtension;
920
921 /* Take the successfully created display into account. */
922 PrimaryExtension->u.primary.cDisplays++;
923 }
924#else
925 /* simply store the number of monitors, we will deal with VidPN stuff later */
926 PrimaryExtension->u.primary.cDisplays = cDisplays;
927#endif
928 }
929
930 /* Failure to create secondary displays is not fatal */
931 rc = NO_ERROR;
932 }
933
934#ifndef VBOXWDDM
935 /* Now when the number of monitors is known and extensions are created,
936 * calculate the layout of framebuffers.
937 */
938 VBoxComputeFrameBufferSizes (PrimaryExtension);
939#endif
940
941 if (PrimaryExtension->u.primary.bHGSMI)
942 {
943 /* Setup the information for the host. */
944 rc = vboxSetupAdapterInfoHGSMI (PrimaryExtension);
945
946 if (RT_FAILURE (rc))
947 {
948 PrimaryExtension->u.primary.bHGSMI = FALSE;
949 }
950 }
951
952#ifdef VBOXWDDM
953 if (PrimaryExtension->u.primary.bHGSMI)
954 {
955 ULONG ulAvailable = PrimaryExtension->u.primary.cbVRAM
956 - PrimaryExtension->u.primary.cbMiniportHeap
957 - VBVA_ADAPTER_INFORMATION_SIZE;
958
959 ULONG ulSize = ulAvailable / 2;
960
961 if (ulSize > VBOXWDDM_C_VDMA_BUFFER_SIZE)
962 ulSize = VBOXWDDM_C_VDMA_BUFFER_SIZE;
963
964 /* Align down to 4096 bytes. */
965 ulSize &= ~0xFFF;
966 ULONG offset = ulAvailable - ulSize;
967
968 Assert(!(offset & 0xFFF));
969
970 rc = vboxVdmaCreate (PrimaryExtension, &PrimaryExtension->u.primary.Vdma, offset, ulSize);
971 AssertRC(rc);
972 if (RT_SUCCESS(rc))
973 {
974 /* can enable it right away since the host does not need any screen/FB info
975 * for basic DMA functionality */
976 rc = vboxVdmaEnable(PrimaryExtension, &PrimaryExtension->u.primary.Vdma);
977 AssertRC(rc);
978 if (RT_FAILURE(rc))
979 vboxVdmaDestroy(PrimaryExtension, &PrimaryExtension->u.primary.Vdma);
980 }
981
982#ifdef VBOXWDDM_RENDER_FROM_SHADOW
983 if (RT_SUCCESS(rc))
984 {
985 ulAvailable = offset;
986 ulSize = ulAvailable / 2;
987 ulSize /= PrimaryExtension->u.primary.cDisplays;
988 Assert(ulSize > VBVA_MIN_BUFFER_SIZE);
989 if (ulSize > VBVA_MIN_BUFFER_SIZE)
990 {
991 ULONG ulRatio = ulSize/VBVA_MIN_BUFFER_SIZE;
992 ulRatio >>= 4; /* /= 16; */
993 if (ulRatio)
994 ulSize = VBVA_MIN_BUFFER_SIZE * ulRatio;
995 else
996 ulSize = VBVA_MIN_BUFFER_SIZE;
997 }
998 else
999 {
1000 /* todo: ?? */
1001 }
1002
1003 ulSize &= ~0xFFF;
1004 Assert(ulSize);
1005
1006 Assert(ulSize * PrimaryExtension->u.primary.cDisplays < ulAvailable);
1007
1008 for (int i = PrimaryExtension->u.primary.cDisplays-1; i >= 0; --i)
1009 {
1010 offset -= ulSize;
1011 rc = vboxVbvaCreate(PrimaryExtension, &PrimaryExtension->aSources[i].Vbva, offset, ulSize, i);
1012 AssertRC(rc);
1013 if (RT_SUCCESS(rc))
1014 {
1015 rc = vboxVbvaEnable(PrimaryExtension, &PrimaryExtension->aSources[i].Vbva);
1016 AssertRC(rc);
1017 if (RT_FAILURE(rc))
1018 {
1019 /* @todo: de-initialize */
1020 }
1021 }
1022 }
1023 }
1024#endif
1025
1026 rc = VBoxMapAdapterMemory(PrimaryExtension, (void**)&PrimaryExtension->pvVisibleVram,
1027 0,
1028 vboxWddmVramCpuVisibleSize(PrimaryExtension));
1029 Assert(rc == VINF_SUCCESS);
1030 if (rc != VINF_SUCCESS)
1031 PrimaryExtension->pvVisibleVram = NULL;
1032
1033 if (RT_FAILURE(rc))
1034 PrimaryExtension->u.primary.bHGSMI = FALSE;
1035 }
1036#endif
1037
1038 if (!PrimaryExtension->u.primary.bHGSMI)
1039 {
1040 /* Unmap the memory if VBoxVideo is not supported. */
1041 VBoxUnmapAdapterMemory (PrimaryExtension, &PrimaryExtension->u.primary.pvMiniportHeap, PrimaryExtension->u.primary.cbMiniportHeap);
1042 VBoxUnmapAdapterInformation (PrimaryExtension);
1043
1044 HGSMIHeapDestroy (&PrimaryExtension->u.primary.hgsmiAdapterHeap);
1045 }
1046
1047 if (PrimaryExtension->u.primary.bHGSMI)
1048 {
1049 VBoxVideoCmnSpinLockCreate(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock);
1050 }
1051
1052 dprintf(("VBoxVideo::VBoxSetupDisplays: finished\n"));
1053}
1054
1055#ifdef VBOXWDDM
1056int VBoxFreeDisplaysHGSMI(PDEVICE_EXTENSION PrimaryExtension)
1057{
1058 int rc = VINF_SUCCESS;
1059
1060 Assert(PrimaryExtension->pvVisibleVram);
1061 if (PrimaryExtension->pvVisibleVram)
1062 VBoxUnmapAdapterMemory(PrimaryExtension, (void**)&PrimaryExtension->pvVisibleVram, vboxWddmVramCpuVisibleSize(PrimaryExtension));
1063
1064 for (int i = PrimaryExtension->u.primary.cDisplays-1; i >= 0; --i)
1065 {
1066 rc = vboxVbvaDisable(PrimaryExtension, &PrimaryExtension->aSources[i].Vbva);
1067 AssertRC(rc);
1068 if (RT_SUCCESS(rc))
1069 {
1070 rc = vboxVbvaDestroy(PrimaryExtension, &PrimaryExtension->aSources[i].Vbva);
1071 AssertRC(rc);
1072 if (RT_FAILURE(rc))
1073 {
1074 /* @todo: */
1075 }
1076 }
1077 }
1078
1079 rc = vboxVdmaDisable(PrimaryExtension, &PrimaryExtension->u.primary.Vdma);
1080 AssertRC(rc);
1081 if (RT_SUCCESS(rc))
1082 {
1083 rc = vboxVdmaDestroy(PrimaryExtension, &PrimaryExtension->u.primary.Vdma);
1084 AssertRC(rc);
1085 if (RT_SUCCESS(rc))
1086 {
1087 /*rc = */VBoxUnmapAdapterMemory(PrimaryExtension, &PrimaryExtension->u.primary.pvMiniportHeap, PrimaryExtension->u.primary.cbMiniportHeap);
1088/*
1089 AssertRC(rc);
1090 if (RT_SUCCESS(rc))
1091*/
1092 {
1093 HGSMIHeapDestroy(&PrimaryExtension->u.primary.hgsmiAdapterHeap);
1094
1095 /* Map the adapter information. It will be needed for HGSMI IO. */
1096 /*rc = */VBoxUnmapAdapterInformation(PrimaryExtension);
1097/*
1098 AssertRC(rc);
1099 if (RT_FAILURE(rc))
1100 drprintf((__FUNCTION__"VBoxUnmapAdapterMemory PrimaryExtension->u.primary.pvAdapterInformation failed, rc(%d)\n", rc));
1101*/
1102
1103 }
1104 }
1105 }
1106
1107 return rc;
1108}
1109#endif
1110
1111/*
1112 * Send the pointer shape to the host.
1113 */
1114typedef struct _MOUSEPOINTERSHAPECTX
1115{
1116 VIDEO_POINTER_ATTRIBUTES *pPointerAttr;
1117 uint32_t cbData;
1118 int32_t i32Result;
1119} MOUSEPOINTERSHAPECTX;
1120
1121static int vbvaInitMousePointerShape (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
1122{
1123 NOREF (PrimaryExtension);
1124
1125 MOUSEPOINTERSHAPECTX *pCtx = (MOUSEPOINTERSHAPECTX *)pvContext;
1126 VBVAMOUSEPOINTERSHAPE *p = (VBVAMOUSEPOINTERSHAPE *)pvData;
1127
1128 /* Will be updated by the host. */
1129 p->i32Result = VINF_SUCCESS;
1130
1131 /* We have our custom flags in the field */
1132 p->fu32Flags = pCtx->pPointerAttr->Enable & 0x0000FFFF;
1133
1134 p->u32HotX = (pCtx->pPointerAttr->Enable >> 16) & 0xFF;
1135 p->u32HotY = (pCtx->pPointerAttr->Enable >> 24) & 0xFF;
1136 p->u32Width = pCtx->pPointerAttr->Width;
1137 p->u32Height = pCtx->pPointerAttr->Height;
1138
1139 if (p->fu32Flags & VBOX_MOUSE_POINTER_SHAPE)
1140 {
1141 /* If shape is supplied, then alway create the pointer visible.
1142 * See comments in 'vboxUpdatePointerShape'
1143 */
1144 p->fu32Flags |= VBOX_MOUSE_POINTER_VISIBLE;
1145
1146 /* Copy the actual pointer data. */
1147 memcpy (p->au8Data, pCtx->pPointerAttr->Pixels, pCtx->cbData);
1148 }
1149
1150 return VINF_SUCCESS;
1151}
1152
1153static int vbvaFinalizeMousePointerShape (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
1154{
1155 NOREF (PrimaryExtension);
1156
1157 MOUSEPOINTERSHAPECTX *pCtx = (MOUSEPOINTERSHAPECTX *)pvContext;
1158 VBVAMOUSEPOINTERSHAPE *p = (VBVAMOUSEPOINTERSHAPE *)pvData;
1159
1160 pCtx->i32Result = p->i32Result;
1161
1162 return VINF_SUCCESS;
1163}
1164
1165BOOLEAN vboxUpdatePointerShape (PDEVICE_EXTENSION DeviceExtension,
1166 PVIDEO_POINTER_ATTRIBUTES pointerAttr,
1167 uint32_t cbLength)
1168{
1169#ifndef VBOXWDDM
1170 PDEVICE_EXTENSION PrimaryExtension = DeviceExtension->pPrimary;
1171
1172 /* In multimonitor case the HW mouse pointer is the same on all screens,
1173 * and Windows calls each display driver with the same pointer data: visible for
1174 * the screen where the pointer is and invisible for other screens.
1175 *
1176 * This driver passes the shape to the host only from primary screen and makes
1177 * the pointer always visible (in vbvaInitMousePointerShape).
1178 *
1179 * The simple solution makes it impossible to create the shape and keep the mouse
1180 * pointer invisible. New shapes will be created visible.
1181 * But:
1182 * 1) VBox frontends actually ignore the visibility flag if VBOX_MOUSE_POINTER_SHAPE
1183 * is set and always create new pointers visible.
1184 * 2) Windows uses DrvMovePointer to hide the pointer, which will still work.
1185 */
1186
1187 if (DeviceExtension->iDevice != PrimaryExtension->iDevice)
1188 {
1189 dprintf(("vboxUpdatePointerShape: ignore non primary device %d(%d)\n",
1190 DeviceExtension->iDevice, PrimaryExtension->iDevice));
1191 /* Success. */
1192 return TRUE;
1193 }
1194#else
1195 PDEVICE_EXTENSION PrimaryExtension = DeviceExtension;
1196#endif
1197
1198 uint32_t cbData = 0;
1199
1200 if (pointerAttr->Enable & VBOX_MOUSE_POINTER_SHAPE)
1201 {
1202 /* Size of the pointer data: sizeof (AND mask) + sizeof (XOR_MASK) */
1203 cbData = ((((pointerAttr->Width + 7) / 8) * pointerAttr->Height + 3) & ~3)
1204 + pointerAttr->Width * 4 * pointerAttr->Height;
1205 }
1206
1207#ifndef DEBUG_misha
1208 dprintf(("vboxUpdatePointerShape: cbData %d, %dx%d\n",
1209 cbData, pointerAttr->Width, pointerAttr->Height));
1210#endif
1211
1212 if (cbData > cbLength - sizeof(VIDEO_POINTER_ATTRIBUTES))
1213 {
1214 dprintf(("vboxUpdatePointerShape: calculated pointer data size is too big (%d bytes, limit %d)\n",
1215 cbData, cbLength - sizeof(VIDEO_POINTER_ATTRIBUTES)));
1216 return FALSE;
1217 }
1218
1219 MOUSEPOINTERSHAPECTX ctx;
1220
1221 ctx.pPointerAttr = pointerAttr;
1222 ctx.cbData = cbData;
1223 ctx.i32Result = VERR_NOT_SUPPORTED;
1224
1225 int rc = vboxCallVBVA (PrimaryExtension,
1226 VBVA_MOUSE_POINTER_SHAPE,
1227 sizeof (VBVAMOUSEPOINTERSHAPE) + cbData,
1228 vbvaInitMousePointerShape,
1229 vbvaFinalizeMousePointerShape,
1230 &ctx);
1231#ifndef DEBUG_misha
1232 dprintf(("VBoxVideo::vboxMousePointerShape: rc %d, i32Result = %d\n", rc, ctx.i32Result));
1233#endif
1234
1235 return RT_SUCCESS(rc) && RT_SUCCESS(ctx.i32Result);
1236}
1237
1238typedef struct _VBVAMINIPORT_CHANNELCONTEXT
1239{
1240 PFNHGSMICHANNELHANDLER pfnChannelHandler;
1241 void *pvChannelHandler;
1242}VBVAMINIPORT_CHANNELCONTEXT;
1243
1244typedef struct _VBVADISP_CHANNELCONTEXT
1245{
1246 struct _VBVAHOSTCMD * pFirstCmd;
1247 struct _VBVAHOSTCMD * pLastCmd;
1248 VBOXVCMNSPIN_LOCK pSynchLock;
1249#ifdef DEBUG
1250 int cCmds;
1251#endif
1252 bool bValid;
1253}VBVADISP_CHANNELCONTEXT;
1254
1255#ifdef DEBUG
1256void dbgCheckListLocked(const VBVADISP_CHANNELCONTEXT *pList, struct _VBVAHOSTCMD * pCmd)
1257{
1258 int counter = 0;
1259 for(struct _VBVAHOSTCMD * pCur = pList->pFirstCmd; pCur; pCur=pCur->u.pNext)
1260 {
1261 Assert(pCur != pCmd);
1262 if(pCur == pList->pLastCmd)
1263 {
1264 Assert(pCur->u.pNext == NULL);
1265 }
1266 if(pCur->u.pNext == NULL)
1267 {
1268 Assert(pCur == pList->pLastCmd);
1269 }
1270 counter++;
1271 }
1272
1273 Assert(counter == pList->cCmds);
1274}
1275
1276void dbgCheckList(PDEVICE_EXTENSION PrimaryExtension, VBVADISP_CHANNELCONTEXT *pList, struct _VBVAHOSTCMD * pCmd)
1277{
1278 VBOXVCMNIRQL oldIrql;
1279 VBoxVideoCmnSpinLockAcquire(PrimaryExtension, &pList->pSynchLock, &oldIrql);
1280
1281 dbgCheckListLocked(pList, pCmd);
1282
1283 VBoxVideoCmnSpinLockRelease(PrimaryExtension, &pList->pSynchLock, oldIrql);
1284}
1285
1286#define DBG_CHECKLIST_LOCKED(_pl, pc) dbgCheckListLocked(_pl, pc)
1287#define DBG_CHECKLIST(_pe, _pl, pc) dbgCheckList(_pe, _pl, pc)
1288
1289#else
1290#define DBG_CHECKLIST_LOCKED(_pl, pc) do{}while(0)
1291#define DBG_CHECKLIST(_pe, _pl, pc) do{}while(0)
1292#endif
1293
1294
1295typedef struct _VBVA_CHANNELCONTEXTS
1296{
1297 PDEVICE_EXTENSION PrimaryExtension;
1298 uint32_t cUsed;
1299 uint32_t cContexts;
1300 VBVAMINIPORT_CHANNELCONTEXT mpContext;
1301 VBVADISP_CHANNELCONTEXT aContexts[1];
1302}VBVA_CHANNELCONTEXTS;
1303
1304static int vboxVBVADeleteChannelContexts(PDEVICE_EXTENSION PrimaryExtension, VBVA_CHANNELCONTEXTS * pContext)
1305{
1306 VBoxVideoCmnMemFree(PrimaryExtension,pContext);
1307 return VINF_SUCCESS;
1308}
1309
1310static int vboxVBVACreateChannelContexts(PDEVICE_EXTENSION PrimaryExtension, VBVA_CHANNELCONTEXTS ** ppContext)
1311{
1312#ifndef VBOXWDDM
1313 uint32_t cDisplays = (uint32_t)PrimaryExtension->u.primary.cDisplays;
1314#else
1315 uint32_t cDisplays = (uint32_t)PrimaryExtension->u.primary.cDisplays;
1316#endif
1317 const size_t size = RT_OFFSETOF(VBVA_CHANNELCONTEXTS, aContexts[cDisplays]);
1318 VBVA_CHANNELCONTEXTS * pContext = (VBVA_CHANNELCONTEXTS*)VBoxVideoCmnMemAllocNonPaged(PrimaryExtension, size, MEM_TAG);
1319 if(pContext)
1320 {
1321 memset(pContext, 0, size);
1322 pContext->cContexts = cDisplays;
1323 pContext->PrimaryExtension = PrimaryExtension;
1324 *ppContext = pContext;
1325 return VINF_SUCCESS;
1326 }
1327 return VERR_GENERAL_FAILURE;
1328}
1329
1330static VBVADISP_CHANNELCONTEXT* vboxVBVAFindHandlerInfo(VBVA_CHANNELCONTEXTS *pCallbacks, int iId)
1331{
1332 if(iId < 0)
1333 {
1334 return NULL;
1335 }
1336 else if(pCallbacks->cContexts > (uint32_t)iId)
1337 {
1338 return &pCallbacks->aContexts[iId];
1339 }
1340 return NULL;
1341}
1342
1343#ifndef VBOXWDDM
1344DECLCALLBACK(void) hgsmiHostCmdComplete (HVBOXVIDEOHGSMI hHGSMI, struct _VBVAHOSTCMD * pCmd)
1345{
1346 PDEVICE_EXTENSION PrimaryExtension = ((PDEVICE_EXTENSION)hHGSMI)->pPrimary;
1347 HGSMIHostCmdComplete (PrimaryExtension, pCmd);
1348}
1349
1350DECLCALLBACK(int) hgsmiHostCmdRequest (HVBOXVIDEOHGSMI hHGSMI, uint8_t u8Channel, struct _VBVAHOSTCMD ** ppCmd)
1351{
1352// if(display < 0)
1353// return VERR_INVALID_PARAMETER;
1354 if(!ppCmd)
1355 return VERR_INVALID_PARAMETER;
1356
1357 PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)hHGSMI;
1358 PDEVICE_EXTENSION PrimaryExtension = pDevExt->pPrimary;
1359
1360 /* pick up the host commands */
1361 VBoxVideoHGSMIDpc(PrimaryExtension, NULL);
1362
1363 HGSMICHANNEL * pChannel = HGSMIChannelFindById (&PrimaryExtension->u.primary.channels, u8Channel);
1364 if(pChannel)
1365 {
1366 VBVA_CHANNELCONTEXTS * pContexts = (VBVA_CHANNELCONTEXTS *)pChannel->handler.pvHandler;
1367 VBVADISP_CHANNELCONTEXT *pDispContext = vboxVBVAFindHandlerInfo(pContexts, pDevExt->iDevice);
1368 Assert(pDispContext);
1369 if(pDispContext)
1370 {
1371 UCHAR oldIrql;
1372 VBoxVideoCmnSpinLockAcquire(PrimaryExtension, &pDispContext->pSynchLock, &oldIrql);
1373
1374 DBG_CHECKLIST_LOCKED(pDispContext, NULL);
1375
1376 *ppCmd = pDispContext->pFirstCmd;
1377 pDispContext->pFirstCmd = NULL;
1378 pDispContext->pLastCmd = NULL;
1379#ifdef DEBUG
1380 pDispContext->cCmds = 0;
1381#endif
1382 VBoxVideoCmnSpinLockRelease(PrimaryExtension, &pDispContext->pSynchLock, oldIrql);
1383
1384 DBG_CHECKLIST(PrimaryExtension, pDispContext, NULL);
1385
1386 return VINF_SUCCESS;
1387 }
1388 }
1389
1390 return VERR_INVALID_PARAMETER;
1391}
1392#endif
1393
1394static DECLCALLBACK(int) vboxVBVAChannelGenericHandler(void *pvHandler, uint16_t u16ChannelInfo, void *pvBuffer, HGSMISIZE cbBuffer)
1395{
1396 VBVA_CHANNELCONTEXTS *pCallbacks = (VBVA_CHANNELCONTEXTS*)pvHandler;
1397// Assert(0);
1398 Assert(cbBuffer > VBVAHOSTCMD_HDRSIZE);
1399 if(cbBuffer > VBVAHOSTCMD_HDRSIZE)
1400 {
1401 VBVAHOSTCMD *pHdr = (VBVAHOSTCMD*)pvBuffer;
1402 Assert(pHdr->iDstID >= 0);
1403 if(pHdr->iDstID >= 0)
1404 {
1405 VBVADISP_CHANNELCONTEXT* pHandler = vboxVBVAFindHandlerInfo(pCallbacks, pHdr->iDstID);
1406 Assert(pHandler && pHandler->bValid);
1407 if(pHandler && pHandler->bValid)
1408 {
1409 VBVAHOSTCMD *pFirst = NULL, *pLast = NULL;
1410 for(VBVAHOSTCMD *pCur = pHdr; pCur; )
1411 {
1412 Assert(!pCur->u.Data);
1413 Assert(!pFirst);
1414 Assert(!pLast);
1415
1416 switch(u16ChannelInfo)
1417 {
1418 case VBVAHG_DISPLAY_CUSTOM:
1419 {
1420 if(pLast)
1421 {
1422 pLast->u.pNext = pCur;
1423 pLast = pCur;
1424 }
1425 else
1426 {
1427 pFirst = pCur;
1428 pLast = pCur;
1429 }
1430 Assert(!pCur->u.Data);
1431 //TODO: use offset here
1432 pCur = pCur->u.pNext;
1433 Assert(!pCur);
1434 Assert(pFirst);
1435 Assert(pFirst == pLast);
1436 break;
1437 }
1438 case VBVAHG_EVENT:
1439 {
1440 VBVAHOSTCMDEVENT *pEventCmd = VBVAHOSTCMD_BODY(pCur, VBVAHOSTCMDEVENT);
1441#ifndef VBOXWDDM
1442 PEVENT pEvent = (PEVENT)pEventCmd->pEvent;
1443 pCallbacks->PrimaryExtension->u.primary.VideoPortProcs.pfnSetEvent(
1444 pCallbacks->PrimaryExtension,
1445 pEvent);
1446#else
1447 PKEVENT pEvent = (PKEVENT)pEventCmd->pEvent;
1448 KeSetEvent(pEvent, 0, FALSE);
1449#endif
1450 }
1451 default:
1452 {
1453 DBG_CHECKLIST(pCallbacks->PrimaryExtension, pHandler, pCur);
1454 Assert(u16ChannelInfo==VBVAHG_EVENT);
1455 Assert(!pCur->u.Data);
1456 //TODO: use offset here
1457 if(pLast)
1458 pLast->u.pNext = pCur->u.pNext;
1459 VBVAHOSTCMD * pNext = pCur->u.pNext;
1460 pCur->u.pNext = NULL;
1461 HGSMIHostCmdComplete(pCallbacks->PrimaryExtension, pCur);
1462 pCur = pNext;
1463 Assert(!pCur);
1464 Assert(!pFirst);
1465 Assert(pFirst == pLast);
1466 break;
1467 }
1468 }
1469 }
1470
1471 DBG_CHECKLIST(pCallbacks->PrimaryExtension, pHandler, pFirst);
1472
1473 /* we do not support lists currently */
1474 Assert(pFirst == pLast);
1475 if(pLast)
1476 {
1477 Assert(pLast->u.pNext == NULL);
1478 }
1479
1480 if(pFirst)
1481 {
1482 Assert(pLast);
1483 UCHAR oldIrql;
1484 VBoxVideoCmnSpinLockAcquire(pCallbacks->PrimaryExtension,
1485 &pHandler->pSynchLock,
1486 &oldIrql);
1487
1488 DBG_CHECKLIST_LOCKED(pHandler, pFirst);
1489
1490 if(pHandler->pLastCmd)
1491 {
1492 pHandler->pLastCmd->u.pNext = pFirst;
1493 Assert(pHandler->pFirstCmd);
1494 }
1495 else
1496 {
1497 Assert(!pHandler->pFirstCmd);
1498 pHandler->pFirstCmd = pFirst;
1499 }
1500 pHandler->pLastCmd = pLast;
1501#ifdef DEBUG
1502 pHandler->cCmds++;
1503#endif
1504 DBG_CHECKLIST_LOCKED(pHandler, NULL);
1505
1506 VBoxVideoCmnSpinLockRelease(pCallbacks->PrimaryExtension,
1507 &pHandler->pSynchLock,
1508 oldIrql);
1509 }
1510 else
1511 {
1512 Assert(!pLast);
1513 }
1514 return VINF_SUCCESS;
1515 }
1516 }
1517 else
1518 {
1519 //TODO: impl
1520// HGSMIMINIPORT_CHANNELCONTEXT *pHandler = vboxVideoHGSMIFindHandler;
1521// if(pHandler && pHandler->pfnChannelHandler)
1522// {
1523// pHandler->pfnChannelHandler(pHandler->pvChannelHandler, u16ChannelInfo, pHdr, cbBuffer);
1524//
1525// return VINF_SUCCESS;
1526// }
1527 }
1528 }
1529 /* no handlers were found, need to complete the command here */
1530 HGSMIHostCmdComplete(pCallbacks->PrimaryExtension, pvBuffer);
1531 return VINF_SUCCESS;
1532}
1533
1534static HGSMICHANNELHANDLER g_OldHandler;
1535
1536int vboxVBVAChannelDisplayEnable(PDEVICE_EXTENSION PrimaryExtension,
1537 int iDisplay, /* negative would mean this is a miniport handler */
1538 uint8_t u8Channel)
1539{
1540 VBVA_CHANNELCONTEXTS * pContexts;
1541 HGSMICHANNEL * pChannel = HGSMIChannelFindById (&PrimaryExtension->u.primary.channels, u8Channel);
1542 if(!pChannel)
1543 {
1544 int rc = vboxVBVACreateChannelContexts(PrimaryExtension, &pContexts);
1545 if(RT_FAILURE(rc))
1546 {
1547 return rc;
1548 }
1549 }
1550 else
1551 {
1552 pContexts = (VBVA_CHANNELCONTEXTS *)pChannel->handler.pvHandler;
1553 }
1554
1555 VBVADISP_CHANNELCONTEXT *pDispContext = vboxVBVAFindHandlerInfo(pContexts, iDisplay);
1556 Assert(pDispContext);
1557 if(pDispContext)
1558 {
1559#ifdef DEBUGVHWASTRICT
1560 Assert(!pDispContext->bValid);
1561#endif
1562 Assert(!pDispContext->pFirstCmd);
1563 Assert(!pDispContext->pLastCmd);
1564 if(!pDispContext->bValid)
1565 {
1566 pDispContext->bValid = true;
1567 pDispContext->pFirstCmd = NULL;
1568 pDispContext->pLastCmd= NULL;
1569#ifdef DEBUG
1570 pDispContext->cCmds = 0;
1571#endif
1572
1573 VBoxVideoCmnSpinLockCreate(PrimaryExtension, &pDispContext->pSynchLock);
1574
1575 int rc = VINF_SUCCESS;
1576 if(!pChannel)
1577 {
1578 rc = HGSMIChannelRegister (&PrimaryExtension->u.primary.channels,
1579 u8Channel,
1580 "VGA Miniport HGSMI channel",
1581 vboxVBVAChannelGenericHandler,
1582 pContexts,
1583 &g_OldHandler);
1584 }
1585
1586 if(RT_SUCCESS(rc))
1587 {
1588 pContexts->cUsed++;
1589 return VINF_SUCCESS;
1590 }
1591 }
1592 }
1593
1594 if(!pChannel)
1595 {
1596 vboxVBVADeleteChannelContexts(PrimaryExtension, pContexts);
1597 }
1598
1599 return VERR_GENERAL_FAILURE;
1600}
1601
1602/** @todo Mouse pointer position to be read from VMMDev memory, address of the memory region
1603 * can be queried from VMMDev via an IOCTL. This VMMDev memory region will contain
1604 * host information which is needed by the guest.
1605 *
1606 * Reading will not cause a switch to the host.
1607 *
1608 * Have to take into account:
1609 * * synchronization: host must write to the memory only from EMT,
1610 * large structures must be read under flag, which tells the host
1611 * that the guest is currently reading the memory (OWNER flag?).
1612 * * guest writes: may be allocate a page for the host info and make
1613 * the page readonly for the guest.
1614 * * the information should be available only for additions drivers.
1615 * * VMMDev additions driver will inform the host which version of the info it expects,
1616 * host must support all versions.
1617 *
1618 */
1619
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