Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
7.5 KiB

  1. /******************************Module*Header**********************************\
  2. *
  3. * ***************
  4. * * SAMPLE CODE *
  5. * ***************
  6. *
  7. * Module Name: mini.c
  8. *
  9. * Content:
  10. *
  11. * Copyright (c) 1994-1998 3Dlabs Inc. Ltd. All rights reserved.
  12. * Copyright (c) 1995-1999 Microsoft Corporation. All rights reserved.
  13. \*****************************************************************************/
  14. #include "precomp.h"
  15. //-----------------------------------------------------------------------------
  16. //
  17. // AllocateDMABuffer
  18. //
  19. // Allocate physical continous memory for DMA operation. This function returns
  20. // a pointer to a previously allocated DMA buffer if there is still an unfreed
  21. // allocation left. That way the reallocation of continous memory can be
  22. // avoided when a new ppdev is created on lets say a mode switch, since
  23. // allocation of continous memory cannot be guaranteed. The memory is only
  24. // physically freed after all allocations have called a FreeDMABuffer.
  25. //
  26. // Calls to AllocateDMABuffer and FreeDMABuffer should be paired, otherwise
  27. // the usage count logic in the miniport driver gets confused!
  28. //
  29. // The VideoPort currently restricts the size of a DMA buffer to 256kb.
  30. //
  31. // hDriver-------videoport driver handle
  32. // plSize--------pointer to LONG size of requested DMA buffer. Returns size
  33. // of allocated DMA buffer
  34. // (return value can be smaller than requested size)
  35. // ppVirtAddr----returns virtual address of requested DMA buffer.
  36. // pPhysAddr-----returns physical address of DMA buffer as seen from graphics
  37. // device.
  38. //
  39. // return TRUE, allocation successful
  40. // FALSE, allocation failed
  41. //
  42. //-----------------------------------------------------------------------------
  43. BOOL
  44. AllocateDMABuffer( HANDLE hDriver,
  45. PLONG plSize,
  46. PULONG *ppVirtAddr,
  47. LARGE_INTEGER *pPhysAddr)
  48. {
  49. LINE_DMA_BUFFER ldb;
  50. ldb.size = *plSize;
  51. ldb.virtAddr = 0;
  52. ldb.cacheEnabled = TRUE;
  53. *ppVirtAddr=0;
  54. pPhysAddr->HighPart=
  55. pPhysAddr->LowPart=0;
  56. ULONG ulLength = sizeof(LINE_DMA_BUFFER);
  57. if (EngDeviceIoControl( hDriver,
  58. IOCTL_VIDEO_QUERY_LINE_DMA_BUFFER,
  59. (PVOID)&ldb,
  60. ulLength,
  61. (PVOID)&ldb,
  62. ulLength,
  63. &ulLength))
  64. {
  65. return(FALSE);
  66. }
  67. *ppVirtAddr=(PULONG)ldb.virtAddr;
  68. if (ldb.virtAddr!=NULL)
  69. {
  70. *pPhysAddr=ldb.physAddr;
  71. *plSize=ldb.size;
  72. return TRUE;
  73. }
  74. return FALSE;
  75. }
  76. //-----------------------------------------------------------------------------
  77. //
  78. // FreeDMABuffer
  79. //
  80. // free continous buffer previously allocated by AllocateDMABuffer.
  81. //
  82. //-----------------------------------------------------------------------------
  83. BOOL
  84. FreeDMABuffer( HANDLE hDriver,
  85. PVOID pVirtAddr)
  86. {
  87. LINE_DMA_BUFFER ldb;
  88. ldb.size = 0;
  89. ldb.virtAddr = pVirtAddr;
  90. ULONG ulLength = sizeof(LINE_DMA_BUFFER);
  91. if (EngDeviceIoControl( hDriver,
  92. IOCTL_VIDEO_QUERY_LINE_DMA_BUFFER,
  93. (PVOID)&ldb,
  94. ulLength,
  95. NULL,
  96. 0,
  97. &ulLength))
  98. {
  99. return FALSE;
  100. }
  101. return TRUE;
  102. }
  103. //-----------------------------------------------------------------------------
  104. //
  105. // AllocateEmulatedDMABuffer
  106. //
  107. // Allocate memory for emulated DMA operation.
  108. //
  109. // hDriver-------videoport driver handle
  110. // ulSize--------ULONG size of requested DMA buffer
  111. // ulTag---------ULONG tag to mark allocation
  112. //
  113. // return NULL, allocation failed
  114. // otherwise, virtual address of emulated DMA buffer
  115. //
  116. //-----------------------------------------------------------------------------
  117. PULONG
  118. AllocateEmulatedDMABuffer(
  119. HANDLE hDriver,
  120. ULONG ulSize,
  121. ULONG ulTag
  122. )
  123. {
  124. EMULATED_DMA_BUFFER edb;
  125. edb.virtAddr = NULL;
  126. edb.size = ulSize;
  127. edb.tag = ulTag;
  128. ULONG ulLength = sizeof(edb);
  129. if (EngDeviceIoControl( hDriver,
  130. IOCTL_VIDEO_QUERY_EMULATED_DMA_BUFFER,
  131. (PVOID)&edb,
  132. ulLength,
  133. (PVOID)&edb,
  134. ulLength,
  135. &ulLength))
  136. {
  137. return (NULL);
  138. }
  139. return (PULONG)(edb.virtAddr);
  140. }
  141. //-----------------------------------------------------------------------------
  142. //
  143. // FreeEmulatedDMABuffer
  144. //
  145. // free buffer previously allocated by AllocateEmulatedDMABuffer.
  146. //
  147. //-----------------------------------------------------------------------------
  148. BOOL
  149. FreeEmulatedDMABuffer(
  150. HANDLE hDriver,
  151. PVOID pVirtAddr
  152. )
  153. {
  154. EMULATED_DMA_BUFFER edb;
  155. edb.virtAddr = pVirtAddr;
  156. ULONG ulLength = sizeof(edb);
  157. if (EngDeviceIoControl( hDriver,
  158. IOCTL_VIDEO_QUERY_EMULATED_DMA_BUFFER,
  159. (PVOID)&edb,
  160. ulLength,
  161. NULL,
  162. 0,
  163. &ulLength))
  164. {
  165. return FALSE;
  166. }
  167. return TRUE;
  168. }
  169. //-----------------------------------------------------------------------------
  170. //
  171. // StallExecution
  172. //
  173. // calls VideoPortStallExecution in the miniport for defined delay when
  174. // polling Permedia registers. VideoPortStallexecution does not yield
  175. // to another process and should only be used in rare cases.
  176. //
  177. // hDriver--------handle to videoport
  178. // ulMicroSeconds-number of microseconds to stall CPU execution
  179. //
  180. //-----------------------------------------------------------------------------
  181. VOID
  182. StallExecution( HANDLE hDriver, ULONG ulMicroSeconds)
  183. {
  184. ULONG Length = 0;
  185. EngDeviceIoControl(hDriver,
  186. IOCTL_VIDEO_STALL_EXECUTION,
  187. &ulMicroSeconds,
  188. sizeof(ULONG),
  189. NULL,
  190. 0,
  191. &Length);
  192. }
  193. //-----------------------------------------------------------------------------
  194. //
  195. // GetPInterlockedExchange
  196. //
  197. // We need to call the same InterlockedExchange function from the display
  198. // driver and the miniport to make sure they work properly. The miniport
  199. // will give us a pointer to the function which we will call directly...
  200. // On Alpha and Risc machines, InterlockedExchange compiles as inline and
  201. // we don't need to call in the kernel.
  202. //
  203. // note: the InterlockedExchange function exported from ntoskrnl has calling
  204. // convention __fastcall.
  205. //
  206. //-----------------------------------------------------------------------------
  207. #if defined(_X86_)
  208. PVOID
  209. GetPInterlockedExchange( HANDLE hDriver)
  210. {
  211. ULONG Length = 0;
  212. PVOID pWorkPtr=NULL;
  213. if (EngDeviceIoControl( hDriver,
  214. IOCTL_VIDEO_QUERY_INTERLOCKEDEXCHANGE,
  215. NULL,
  216. 0,
  217. &pWorkPtr,
  218. sizeof(pWorkPtr),
  219. &Length))
  220. {
  221. return NULL;
  222. }
  223. return pWorkPtr;
  224. }
  225. #endif