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.

262 lines
9.3 KiB

  1. //
  2. // VIAAGP.sys is a driver, make sure we get the appropriate linkage.
  3. //
  4. #define _NTDRIVER_
  5. #include "stdarg.h"
  6. #include "stdio.h"
  7. #include "ntddk.h"
  8. #include "agp.h"
  9. //
  10. // Define the location of the GART aperture control registers
  11. //
  12. //
  13. // The GART registers on the VIA live in the host-PCI bridge.
  14. // This is unfortunate, since the AGP driver attaches to the PCI-PCI (AGP)
  15. // bridge. So we have to get to the host-PCI bridge config space
  16. // and this is only possible because we KNOW this is bus 0, slot 0.
  17. //
  18. #define AGP_VIA_GART_BUS_ID 0
  19. #define AGP_VIA_GART_SLOT_ID 0
  20. #define AGP_P2P_SLOT_ID 1
  21. #define AGP_VGA_BUS_ID 1
  22. #define AGP_VGA_SLOT_ID 0
  23. #define AGP_VIA_IDENTIFIER 0x00001106
  24. // 0x05971106 -> VT82C597 / VT82C597 AT
  25. // chu
  26. #define GABASE_OFFSET 0x10 // Graphics Aperture Base
  27. #define GARTCTRL_OFFSET 0x80 // GART/TLB Control
  28. #define GASIZE_OFFSET 0x84 // Graphics Aperture Size
  29. #define GATTBASE_OFFSET 0x88 // GA Translation Table Base
  30. #define VREF_OFFSET 0xB0 // AGP VREF control
  31. #define AGPMISC_OFFSET 0xAC // AGP MISC control
  32. #define ReadVIAConfig(_buf_,_offset_,_size_) \
  33. { \
  34. ULONG _len_; \
  35. _len_ = HalGetBusDataByOffset(PCIConfiguration, \
  36. AGP_VIA_GART_BUS_ID, \
  37. AGP_VIA_GART_SLOT_ID, \
  38. (_buf_), \
  39. (_offset_), \
  40. (_size_)); \
  41. ASSERT(_len_ == (_size_)); \
  42. }
  43. #define WriteVIAConfig(_buf_,_offset_,_size_) \
  44. { \
  45. ULONG _len_; \
  46. _len_ = HalSetBusDataByOffset(PCIConfiguration, \
  47. AGP_VIA_GART_BUS_ID, \
  48. AGP_VIA_GART_SLOT_ID, \
  49. (_buf_), \
  50. (_offset_), \
  51. (_size_)); \
  52. ASSERT(_len_ == (_size_)); \
  53. }
  54. #define ReadP2PConfig(_buf_,_offset_,_size_) \
  55. { \
  56. ULONG _len_; \
  57. _len_ = HalGetBusDataByOffset(PCIConfiguration, \
  58. AGP_VIA_GART_BUS_ID, \
  59. AGP_P2P_SLOT_ID, \
  60. (_buf_), \
  61. (_offset_), \
  62. (_size_)); \
  63. ASSERT(_len_ == (_size_)); \
  64. }
  65. #define WriteP2PConfig(_buf_,_offset_,_size_) \
  66. { \
  67. ULONG _len_; \
  68. _len_ = HalSetBusDataByOffset(PCIConfiguration, \
  69. AGP_VIA_GART_BUS_ID, \
  70. AGP_P2P_SLOT_ID, \
  71. (_buf_), \
  72. (_offset_), \
  73. (_size_)); \
  74. ASSERT(_len_ == (_size_)); \
  75. }
  76. #define ReadVGAConfig(_buf_,_offset_,_size_) \
  77. { \
  78. ULONG _len_; \
  79. _len_ = HalGetBusDataByOffset(PCIConfiguration, \
  80. AGP_VGA_BUS_ID, \
  81. AGP_VGA_SLOT_ID, \
  82. (_buf_), \
  83. (_offset_), \
  84. (_size_)); \
  85. ASSERT(_len_ == (_size_)); \
  86. }
  87. #define WriteVGAConfig(_buf_,_offset_,_size_) \
  88. { \
  89. ULONG _len_; \
  90. _len_ = HalSetBusDataByOffset(PCIConfiguration, \
  91. AGP_VGA_BUS_ID, \
  92. AGP_VGA_SLOT_ID, \
  93. (_buf_), \
  94. (_offset_), \
  95. (_size_)); \
  96. ASSERT(_len_ == (_size_)); \
  97. }
  98. #define ON 1
  99. #define OFF 0
  100. #define ViaApertureEnable(Enable) \
  101. { \
  102. VIA_GART_TLB_CTRL AGPCTRL_Config; \
  103. \
  104. ReadVIAConfig(&AGPCTRL_Config, GARTCTRL_OFFSET, sizeof(AGPCTRL_Config)); \
  105. AGPCTRL_Config.AGP_ATFGA = (Enable); \
  106. AGPCTRL_Config.CPU_ATFGA = (Enable); \
  107. AGPCTRL_Config.PCI2_ATFGA = (Enable); \
  108. AGPCTRL_Config.PCI1_ATFGA = (Enable); \
  109. WriteVIAConfig(&AGPCTRL_Config, GARTCTRL_OFFSET, sizeof(AGPCTRL_Config)); \
  110. }
  111. #define ViaGartEnable(Enable) \
  112. { \
  113. VIA_GATT_BASE GARTBASE_Config; \
  114. \
  115. ReadVIAConfig(&GARTBASE_Config, GATTBASE_OFFSET, sizeof(GARTBASE_Config));\
  116. GARTBASE_Config.GA_Enable = (Enable); \
  117. WriteVIAConfig(&GARTBASE_Config, GATTBASE_OFFSET, sizeof(GARTBASE_Config));}
  118. //
  119. // Conversions from Graphics Aperture Size encoding to MB
  120. //
  121. // 0xFF (b 1111 1111) = 1MB
  122. // 0xFE (b 1111 1110) = 2MB
  123. // 0xFC (b 1111 1100) = 4MB
  124. // 0xF8 (b 1111 1000) = 8MB
  125. // 0xF0 (b 1111 0000) = 16MB
  126. // 0xE0 (b 1110 0000) = 32MB
  127. // 0xC0 (b 1100 0000) = 64MB
  128. // 0x80 (b 1000 0000) = 128MB
  129. // 0x00 (b 0000 0000) = 256MB
  130. #define GA_SIZE_1MB 0xFF
  131. #define GA_SIZE_2MB 0xFE
  132. #define GA_SIZE_4MB 0xFC
  133. #define GA_SIZE_8MB 0xF8
  134. #define GA_SIZE_16MB 0xF0
  135. #define GA_SIZE_32MB 0xE0
  136. #define GA_SIZE_64MB 0xC0
  137. #define GA_SIZE_128MB 0x80
  138. #define GA_SIZE_256MB 0x00
  139. #define GA_SIZE_COUNT 7
  140. #define GA_MIN_SIZE (1 * 1024 * 1024)
  141. #define GA_MAX_SIZE (256 * 1024 * 1024)
  142. //
  143. // Define the GART table entry.
  144. //
  145. typedef struct _GART_ENTRY_HW {
  146. ULONG Valid : 1;
  147. ULONG Reserved : 11;
  148. ULONG Page : 20;
  149. } GART_ENTRY_HW, *PGART_ENTRY_HW;
  150. //
  151. // Aperture size in MB is equivalent to gart table allocation
  152. // alignment requirement in KB
  153. //
  154. #define VIA_GART_ALIGN(Aperture) ((Aperture) >> 0xA)
  155. #define VIA_VERIFY_GART_ALIGN(Gart, Aperture) \
  156. (((Gart) & (VIA_GART_ALIGN((Aperture)) - 1)) == 0)
  157. //
  158. // GART Entry states are defined so that all software-only states
  159. // have the Valid bit clear.
  160. //
  161. #define GART_ENTRY_VALID 1 // 001
  162. #define GART_ENTRY_FREE 0 // 000
  163. #define GART_ENTRY_WC 2 // 010
  164. #define GART_ENTRY_UC 4 // 100
  165. #define GART_ENTRY_RESERVED_WC GART_ENTRY_WC
  166. #define GART_ENTRY_RESERVED_UC GART_ENTRY_UC
  167. #define GART_ENTRY_VALID_WC (GART_ENTRY_VALID)
  168. #define GART_ENTRY_VALID_UC (GART_ENTRY_VALID)
  169. typedef struct _GART_ENTRY_SW {
  170. ULONG State : 3;
  171. ULONG Reserved : 29;
  172. } GART_ENTRY_SW, *PGART_ENTRY_SW;
  173. typedef struct _GART_PTE {
  174. union {
  175. GART_ENTRY_HW Hard;
  176. ULONG AsUlong;
  177. GART_ENTRY_SW Soft;
  178. };
  179. } GART_PTE, *PGART_PTE;
  180. //
  181. // Define the layout of the hardware registers
  182. //
  183. typedef struct _VIA_GART_TLB_CTRL {
  184. ULONG AGP_ATFGA : 1; // ATFGA = Address Translation for GA Access
  185. ULONG CPU_ATFGA : 1;
  186. ULONG PCI2_ATFGA : 1;
  187. ULONG PCI1_ATFGA : 1;
  188. ULONG Reserved1 : 3;
  189. ULONG FlushPageTLB : 1;
  190. ULONG Reserved2 : 8; // test mode status
  191. ULONG Reserved3 : 16;
  192. } VIA_GART_TLB_CTRL, *PVIA_GART_TLB_CTRL;
  193. typedef struct _VIA_GATT_BASE {
  194. ULONG TT_NonCache : 1; // Translation Table Noncachable
  195. ULONG GA_Enable : 1; // Graphics Aperture Enable
  196. ULONG TLB_Timing : 1;
  197. ULONG Reserved : 9;
  198. ULONG GATT_Base : 20;
  199. } VIA_GATT_BASE, *PVIA_GATT_BASE;
  200. typedef struct _VREF_REG {
  201. ULONG Reserved1 : 7;
  202. ULONG VREF_Control : 1;
  203. ULONG Reserved2 : 24;
  204. } VREF_REG, *PVREF_REG;
  205. typedef struct _AGPMISC_REG {
  206. ULONG Reserved1 : 18;
  207. ULONG AGP4X_Support : 1;
  208. ULONG Reserved2 : 1;
  209. ULONG FW_Support : 1;
  210. ULONG AGP4G_Support : 1;
  211. ULONG Reserved3 : 10;
  212. } AGPMISC_REG, *PAGPMISC_REG;
  213. //
  214. // Define the VIA-specific extension
  215. //
  216. typedef struct _AGPVIA_EXTENSION {
  217. BOOLEAN GlobalEnable;
  218. BOOLEAN PCIEnable;
  219. PHYSICAL_ADDRESS ApertureStart; //Aperture Phys Base Address
  220. ULONG ApertureLength;
  221. PGART_PTE GartCached;
  222. PGART_PTE Gart;
  223. ULONG GartLength;
  224. PHYSICAL_ADDRESS GartPhysical;
  225. BOOLEAN Cap_FlushTLB; //TRUE: support Flush TLB
  226. ULONGLONG SpecialTarget;
  227. } AGPVIA_EXTENSION, *PAGPVIA_EXTENSION;