Source code of Windows XP (NT5)
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.

405 lines
11 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. agplib.h
  5. Abstract:
  6. Private header file for the common AGP library
  7. Author:
  8. John Vert (jvert) 10/22/1997
  9. Revision History:
  10. Elliot Shmukler (elliots) 3/24/1999 - Added support for "favored" memory
  11. ranges for AGP physical memory allocation,
  12. fixed some bugs.
  13. --*/
  14. #include "agp.h"
  15. #include "wdmguid.h"
  16. #include <devioctl.h>
  17. #include <acpiioct.h>
  18. //
  19. // regstr.h uses things of type WORD, which isn't around in kernel mode.
  20. //
  21. #define _IN_KERNEL_
  22. #include "regstr.h"
  23. #define AGP_HACK_FLAG_SUBSYSTEM 0x01
  24. #define AGP_HACK_FLAG_REVISION 0x02
  25. typedef struct _AGP_HACK_TABLE_ENTRY {
  26. USHORT VendorID;
  27. USHORT DeviceID;
  28. USHORT SubVendorID;
  29. USHORT SubSystemID;
  30. ULONGLONG DeviceFlags;
  31. UCHAR RevisionID;
  32. UCHAR Flags;
  33. } AGP_HACK_TABLE_ENTRY, *PAGP_HACK_TABLE_ENTRY;
  34. extern PAGP_HACK_TABLE_ENTRY AgpDeviceHackTable;
  35. extern PAGP_HACK_TABLE_ENTRY AgpGlobalHackTable;
  36. //
  37. // Define common device extension
  38. //
  39. typedef enum _AGP_EXTENSION_TYPE {
  40. AgpTargetFilter,
  41. AgpMasterFilter
  42. } AGP_EXTENSION_TYPE;
  43. #define TARGET_SIG 'TpgA'
  44. #define MASTER_SIG 'MpgA'
  45. typedef struct _COMMON_EXTENSION {
  46. ULONG Signature;
  47. BOOLEAN Deleted;
  48. AGP_EXTENSION_TYPE Type;
  49. PDEVICE_OBJECT AttachedDevice;
  50. BUS_INTERFACE_STANDARD BusInterface;
  51. } COMMON_EXTENSION, *PCOMMON_EXTENSION;
  52. // Structures to maintain a list of "favored" memory ranges
  53. // for AGP allocation.
  54. typedef struct _AGP_MEMORY_RANGE
  55. {
  56. PHYSICAL_ADDRESS Lower;
  57. PHYSICAL_ADDRESS Upper;
  58. } AGP_MEMORY_RANGE, *PAGP_MEMORY_RANGE;
  59. typedef struct _AGP_FAVORED_MEMORY
  60. {
  61. ULONG NumRanges;
  62. PAGP_MEMORY_RANGE Ranges;
  63. } AGP_FAVORED_MEMORY;
  64. typedef struct _TARGET_EXTENSION {
  65. COMMON_EXTENSION CommonExtension;
  66. PFAST_MUTEX Lock;
  67. struct _MASTER_EXTENSION *ChildDevice;
  68. PCM_RESOURCE_LIST Resources;
  69. PCM_RESOURCE_LIST ResourcesTranslated;
  70. AGP_FAVORED_MEMORY FavoredMemory;
  71. PHYSICAL_ADDRESS GartBase;
  72. ULONG GartLengthInPages;
  73. ULONGLONG AgpContext;
  74. } TARGET_EXTENSION, *PTARGET_EXTENSION;
  75. typedef struct _MASTER_EXTENSION {
  76. COMMON_EXTENSION CommonExtension;
  77. PTARGET_EXTENSION Target;
  78. ULONG Capabilities;
  79. ULONG InterfaceCount; // tracks the number of interfaces handed out
  80. ULONG ReservedPages; // tracks the number of pages reserved in the aperture
  81. BOOLEAN StopPending; // TRUE if we have seen a QUERY_STOP
  82. BOOLEAN RemovePending; // TRUE if we have seen a QUERY_REMOVE
  83. ULONG DisableCount; // non-zero if we are in a state where we cannot service requests
  84. } MASTER_EXTENSION, *PMASTER_EXTENSION;
  85. //
  86. // The MBAT - used to retrieve "favored" memory ranges from
  87. // the AGP northbridge via an ACPI BANK method
  88. //
  89. #include <pshpack1.h>
  90. typedef struct _MBAT
  91. {
  92. UCHAR TableVersion;
  93. UCHAR AgpBusNumber;
  94. UCHAR ValidEntryBitmap;
  95. AGP_MEMORY_RANGE DecodeRange[ANYSIZE_ARRAY];
  96. } MBAT, *PMBAT;
  97. #include <poppack.h>
  98. #define MBAT_VERSION 1
  99. #define MAX_MBAT_SIZE sizeof(MBAT) + ((sizeof(UCHAR) * 8) - ANYSIZE_ARRAY) \
  100. * sizeof(AGP_MEMORY_RANGE)
  101. #define CM_BANK_METHOD (ULONG)('KNAB')
  102. //
  103. // The highest memory address supported by AGP
  104. //
  105. #define MAX_MEM(_num_) _num_ = 1; \
  106. _num_ = _num_*1024*1024*1024*4 - 1
  107. #define RELEASE_BUS_INTERFACE(_ext_) (_ext_)->CommonExtension.BusInterface.InterfaceDereference((_ext_)->CommonExtension.BusInterface.Context)
  108. //
  109. // Macros for getting from the chipset-specific context to our structures
  110. //
  111. #define GET_TARGET_EXTENSION(_target_,_agp_context_) (_target_) = (CONTAINING_RECORD((_agp_context_), \
  112. TARGET_EXTENSION, \
  113. AgpContext)); \
  114. ASSERT_TARGET(_target_)
  115. #define GET_MASTER_EXTENSION(_master_,_agp_context_) { \
  116. PTARGET_EXTENSION _targetext_; \
  117. GET_TARGET_EXTENSION(_targetext_, (_agp_context_)); \
  118. (_master_) = _targetext_->ChildDevice; \
  119. ASSERT_MASTER(_master_); \
  120. }
  121. #define GET_TARGET_PDO(_pdo_,_agp_context_) { \
  122. PTARGET_EXTENSION _targetext_; \
  123. GET_TARGET_EXTENSION(_targetext_,(_agp_context_)); \
  124. (_pdo_) = _targetext_->CommonExtension.AttachedDevice; \
  125. }
  126. #define GET_MASTER_PDO(_pdo_,_agp_context_) { \
  127. PMASTER_EXTENSION _masterext_; \
  128. GET_MASTER_EXTENSION(_masterext_, (_agp_context_)); \
  129. (_pdo_) = _masterext_->CommonExtension.AttachedDevice; \
  130. }
  131. #define GET_AGP_CONTEXT(_targetext_) ((PVOID)(&(_targetext_)->AgpContext))
  132. #define GET_AGP_CONTEXT_FROM_MASTER(_masterext_) GET_AGP_CONTEXT((_masterext_)->Target)
  133. //
  134. // Some debugging macros
  135. //
  136. #define ASSERT_TARGET(_target_) ASSERT((_target_)->CommonExtension.Signature == TARGET_SIG); \
  137. ASSERT((_target_)->ChildDevice->CommonExtension.Signature == MASTER_SIG)
  138. #define ASSERT_MASTER(_master_) ASSERT((_master_)->CommonExtension.Signature == MASTER_SIG); \
  139. ASSERT((_master_)->Target->CommonExtension.Signature == TARGET_SIG)
  140. //
  141. // Locking macros
  142. //
  143. #define LOCK_MUTEX(_fm_) ExAcquireFastMutex(_fm_); \
  144. ASSERT((_fm_)->Count == 0)
  145. #define UNLOCK_MUTEX(_fm_) ASSERT((_fm_)->Count == 0); \
  146. ExReleaseFastMutex(_fm_)
  147. #define LOCK_TARGET(_targetext_) ASSERT_TARGET(_targetext_); \
  148. LOCK_MUTEX((_targetext_)->Lock)
  149. #define LOCK_MASTER(_masterext_) ASSERT_MASTER(_masterext_); \
  150. LOCK_MUTEX((_masterext_)->Target->Lock)
  151. #define UNLOCK_TARGET(_targetext_) ASSERT_TARGET(_targetext_); \
  152. UNLOCK_MUTEX((_targetext_)->Lock)
  153. #define UNLOCK_MASTER(_masterext_) ASSERT_MASTER(_masterext_); \
  154. UNLOCK_MUTEX((_masterext_)->Target->Lock)
  155. //
  156. // Private resource type definition
  157. //
  158. typedef enum {
  159. AgpPrivateResource = '0PGA'
  160. } AGP_PRIVATE_RESOURCE_TYPES;
  161. //
  162. // Define function prototypes
  163. //
  164. //
  165. // Driver and device initialization - init.c
  166. //
  167. NTSTATUS
  168. AgpAttachDeviceRelations(
  169. IN PDEVICE_OBJECT DeviceObject,
  170. IN PIRP Irp,
  171. IN PTARGET_EXTENSION Extension
  172. );
  173. //
  174. // IRP Dispatch routines - dispatch.c
  175. //
  176. NTSTATUS
  177. AgpDispatchPnp(
  178. IN PDEVICE_OBJECT DeviceObject,
  179. IN OUT PIRP Irp
  180. );
  181. NTSTATUS
  182. AgpDispatchPower(
  183. IN PDEVICE_OBJECT DeviceObject,
  184. IN OUT PIRP Irp
  185. );
  186. NTSTATUS
  187. AgpDispatchDeviceControl(
  188. IN PDEVICE_OBJECT DeviceObject,
  189. IN OUT PIRP Irp
  190. );
  191. NTSTATUS
  192. AgpDispatchWmi(
  193. IN PDEVICE_OBJECT DeviceObject,
  194. IN OUT PIRP Irp
  195. );
  196. NTSTATUS
  197. AgpSetEventCompletion(
  198. IN PDEVICE_OBJECT DeviceObject,
  199. IN PIRP Irp,
  200. IN PKEVENT Event
  201. );
  202. //
  203. // Config space handling routines - config.c
  204. //
  205. NTSTATUS
  206. ApQueryBusInterface(
  207. IN PDEVICE_OBJECT DeviceObject,
  208. OUT PBUS_INTERFACE_STANDARD BusInterface
  209. );
  210. //
  211. // Resource handing routines - resource.c
  212. //
  213. NTSTATUS
  214. AgpFilterResourceRequirements(
  215. IN PDEVICE_OBJECT DeviceObject,
  216. IN PIRP Irp,
  217. IN PTARGET_EXTENSION Extension
  218. );
  219. NTSTATUS
  220. AgpQueryResources(
  221. IN PDEVICE_OBJECT DeviceObject,
  222. IN PIRP Irp,
  223. IN PTARGET_EXTENSION Extension
  224. );
  225. NTSTATUS
  226. AgpStartTarget(
  227. IN PIRP Irp,
  228. IN PTARGET_EXTENSION Extension
  229. );
  230. VOID
  231. AgpStopTarget(
  232. IN PTARGET_EXTENSION Extension
  233. );
  234. //
  235. // AGP Interface functions
  236. //
  237. NTSTATUS
  238. AgpInterfaceSetRate(
  239. IN PMASTER_EXTENSION Extension,
  240. IN ULONG AgpRate
  241. );
  242. VOID
  243. AgpInterfaceReference(
  244. IN PMASTER_EXTENSION Extension
  245. );
  246. VOID
  247. AgpInterfaceDereference(
  248. IN PMASTER_EXTENSION Extension
  249. );
  250. NTSTATUS
  251. AgpInterfaceReserveMemory(
  252. IN PMASTER_EXTENSION Extension,
  253. IN ULONG NumberOfPages,
  254. IN MEMORY_CACHING_TYPE MemoryType,
  255. OUT PVOID *MapHandle,
  256. OUT OPTIONAL PHYSICAL_ADDRESS *PhysicalAddress
  257. );
  258. NTSTATUS
  259. AgpInterfaceReleaseMemory(
  260. IN PMASTER_EXTENSION Extension,
  261. IN PVOID MapHandle
  262. );
  263. NTSTATUS
  264. AgpInterfaceCommitMemory(
  265. IN PMASTER_EXTENSION Extension,
  266. IN PVOID MapHandle,
  267. IN ULONG NumberOfPages,
  268. IN ULONG OffsetInPages,
  269. IN OUT PMDL Mdl OPTIONAL,
  270. OUT PHYSICAL_ADDRESS *MemoryBase
  271. );
  272. NTSTATUS
  273. AgpInterfaceFreeMemory(
  274. IN PMASTER_EXTENSION Extension,
  275. IN PVOID MapHandle,
  276. IN ULONG NumberOfPages,
  277. IN ULONG OffsetInPages
  278. );
  279. NTSTATUS
  280. AgpInterfaceGetMappedPages(
  281. IN PMASTER_EXTENSION Extension,
  282. IN PVOID MapHandle,
  283. IN ULONG NumberOfPages,
  284. IN ULONG OffsetInPages,
  285. OUT PMDL Mdl
  286. );
  287. //
  288. // Misc utils.c
  289. //
  290. BOOLEAN
  291. AgpOpenKey(
  292. IN PWSTR KeyName,
  293. IN HANDLE ParentHandle,
  294. OUT PHANDLE Handle,
  295. OUT PNTSTATUS Status
  296. );
  297. BOOLEAN
  298. AgpStringToUSHORT(
  299. IN PWCHAR String,
  300. OUT PUSHORT Result
  301. );
  302. ULONGLONG
  303. AgpGetDeviceFlags(
  304. IN PAGP_HACK_TABLE_ENTRY AgpHackTable,
  305. IN USHORT VendorID,
  306. IN USHORT DeviceID,
  307. IN USHORT SubVendorID,
  308. IN USHORT SubSystemID,
  309. IN UCHAR RevisionID
  310. );
  311. //
  312. // AGP Physical Memory allocator
  313. //
  314. PMDL
  315. AgpLibAllocatePhysicalMemory(
  316. IN PVOID AgpContext,
  317. IN ULONG TotalBytes);
  318. //
  319. // Handy structures for mucking about in PCI config space
  320. //
  321. //
  322. // The PCI_COMMON_CONFIG includes the 192 bytes of device specific
  323. // data. The following structure is used to get only the first 64
  324. // bytes which is all we care about most of the time anyway. We cast
  325. // to PCI_COMMON_CONFIG to get at the actual fields.
  326. //
  327. typedef struct {
  328. ULONG Reserved[PCI_COMMON_HDR_LENGTH/sizeof(ULONG)];
  329. } PCI_COMMON_HEADER, *PPCI_COMMON_HEADER;