Windows NT 4.0 source code leak
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.

595 lines
15 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1993 Digital Equipment Corporation
  3. Module Name:
  4. addrsup.c
  5. Abstract:
  6. This module contains the platform dependent code to create bus addreses
  7. and QVAs for the EB64+ system.
  8. Author:
  9. Joe Notarangelo 22-Oct-1993
  10. Environment:
  11. Kernel mode
  12. Revision History:
  13. Dick Bissen (Digital) 30-Jun-1994
  14. Added code to check the new PCI Memory MAp address range which is
  15. impacted by the EPEC HAXR1.
  16. Eric Rehm (Digital) 03-Jan-1994
  17. Added PCIBus(0) and dense space support to all routines.
  18. --*/
  19. #include "halp.h"
  20. #include "eisa.h"
  21. #include "eb64pdef.h"
  22. typedef PVOID QUASI_VIRTUAL_ADDRESS;
  23. QUASI_VIRTUAL_ADDRESS
  24. HalCreateQva(
  25. IN PHYSICAL_ADDRESS PA,
  26. IN PVOID VA
  27. );
  28. BOOLEAN
  29. HalpTranslateSystemBusAddress(
  30. IN PBUS_HANDLER BusHandler,
  31. IN PBUS_HANDLER RootHandler,
  32. IN PHYSICAL_ADDRESS BusAddress,
  33. IN OUT PULONG AddressSpace,
  34. OUT PPHYSICAL_ADDRESS TranslatedAddress
  35. )
  36. /*++
  37. Routine Description:
  38. This function returns the system physical address for a specified I/O bus
  39. address. The return value is suitable for use in a subsequent call to
  40. MmMapIoSpace.
  41. Arguments:
  42. BusHandler - Registered BUSHANDLER for the target configuration space
  43. Supplies the bus handler (bus no, interface type).
  44. RootHandler - Registered BUSHANDLER for the originating
  45. HalTranslateBusAddress request.
  46. BusAddress - Supplies the bus relative address.
  47. AddressSpace - Supplies the address space number for the device: 0 for
  48. memory and 1 for I/O space. If the desired access mode is user mode,
  49. then bit 1 must be TRUE.
  50. TranslatedAddress - Supplies a pointer to return the translated address
  51. Notes:
  52. This is a variation of what began in the MIPS code. The intel code often
  53. assumes that if an address is in I/O space, the bottom 32 bits of the
  54. physical address can be used "like" a virtual address, and are returned
  55. to the user. This doesn't work on MIPs machines where physical
  56. addresses can be larger than 32 bits.
  57. Since we are using superpage addresses for I/O on Alpha, we can do
  58. almost what is done on intel. If AddressSpace is equal to 0 or 1, then
  59. we assume the user is doing kernel I/O and we call
  60. HalCreateQva to build a Quasi Virtual Address and return
  61. that to the caller. We then set AddressSpace to a 1, so that the caller
  62. will not call MmMapIoSpace. The Caller will use the low 32 bits of the
  63. physical address we return as the VA. (Which we built a QVA in).
  64. If the caller wants to access EISA I/O or Memory through user mode, then
  65. the caller must set bit 1 in AddressSpace to a 1 (AddressSpace=2 or 3,
  66. depending on whether EISA I/O or Memory), then the caller is returned the
  67. 34 bit Physical address. The caller will then call MmMapIoSpace, or
  68. ZwMapViewOfSection which in turn calls HalCreateQva to build a QVA out
  69. of a VA mapped through the page tables.
  70. **** Note ****
  71. The QVA in user mode can only be used via the user-mode access macros.
  72. Return Value:
  73. A return value of TRUE indicates that a system physical address
  74. corresponding to the supplied bus relative address and bus address
  75. number has been returned in TranslatedAddress.
  76. A return value of FALSE occurs if the translation for the address was
  77. not possible
  78. --*/
  79. {
  80. INTERFACE_TYPE InterfaceType = BusHandler->InterfaceType;
  81. ULONG BusNumber = BusHandler->BusNumber;
  82. PVOID va = 0; // note, this is used for a placeholder
  83. //
  84. // The only buses available on EB64+ are an ISA bus and a PCI bus.
  85. // We support any translations for EISA devices as well, though
  86. // if they are true EISA devices they won't even be able to plug into
  87. // the connectors!
  88. //
  89. if (InterfaceType != Isa &&
  90. InterfaceType != Eisa &&
  91. InterfaceType != PCIBus) {
  92. //
  93. // Not on this system return nothing.
  94. //
  95. *AddressSpace = 0;
  96. TranslatedAddress->LowPart = 0;
  97. return(FALSE);
  98. }
  99. //
  100. // Determine the address based on whether the bus address is in I/O space
  101. // or bus memory space.
  102. //
  103. switch ( (ADDRESS_SPACE_TYPE)(*AddressSpace) ) {
  104. case BusMemory: {
  105. //
  106. // The address is in PCI memory space, kernel mode.
  107. //
  108. //jnfix - HAE support here
  109. switch( InterfaceType ) {
  110. case Isa: {
  111. //
  112. // Can't go above 16MB (24 Bits) for Isa Buses
  113. //
  114. if( BusAddress.LowPart >= __16MB ){
  115. *AddressSpace = 0;
  116. TranslatedAddress->LowPart = 0;
  117. return(FALSE);
  118. }
  119. break;
  120. } // case Isa
  121. case PCIBus: {
  122. //
  123. // IMPORTANT:
  124. //
  125. // MAX PCI sparse is PCI_MAX_SPARSE_MEMORY_ADDRESS
  126. // MAX PCI dense is PCI_MAX_DENSE_MEMORY_ADDRESS
  127. //
  128. if ( BusAddress.LowPart > PCI_MAX_DENSE_MEMORY_ADDRESS ) {
  129. //
  130. // Unsupported dense PCI bus address.
  131. //
  132. #if HALDBG
  133. DbgPrint ("Unsupported PCI address %x:%x\n",
  134. BusAddress.HighPart,
  135. BusAddress.LowPart);
  136. #endif
  137. *AddressSpace = 0;
  138. TranslatedAddress->LowPart = 0;
  139. return(FALSE);
  140. }
  141. else if( BusAddress.LowPart >= PCI_MIN_DENSE_MEMORY_ADDRESS &&
  142. BusAddress.LowPart <= PCI_MAX_DENSE_MEMORY_ADDRESS ) {
  143. #if HALDBG
  144. DbgPrint ("Translating PCI kernel dense address %x:%x\n",
  145. BusAddress.HighPart,
  146. BusAddress.LowPart);
  147. #endif
  148. //
  149. // Bus Address is in dense PCI memory space
  150. //
  151. //
  152. // QVA, as such, is simply the PCI bus address
  153. //
  154. TranslatedAddress->LowPart = BusAddress.LowPart;
  155. //
  156. // clear high longword for QVA
  157. //
  158. TranslatedAddress->HighPart = 0;
  159. //
  160. // dont let the user call MmMapIoSpace
  161. //
  162. *AddressSpace = 1;
  163. return (TRUE);
  164. }
  165. //
  166. // Bus Address is in sparse PCI memory space
  167. //
  168. #if HALDBG
  169. DbgPrint ("Translating PCI kernel sparse address %x:%x\n",
  170. BusAddress.HighPart,
  171. BusAddress.LowPart);
  172. #endif
  173. break;
  174. } // case PCIBus
  175. case Eisa: {
  176. break;
  177. } // case Eisa
  178. } // switch( InterfaceType )
  179. //
  180. // Start with the base physical address and add the
  181. // bus address by converting it to the physical address.
  182. //
  183. TranslatedAddress->QuadPart = APECS_PCI_MEMORY_BASE_PHYSICAL;
  184. TranslatedAddress->QuadPart += ((ULONGLONG)BusAddress.LowPart << IO_BIT_SHIFT);
  185. //
  186. // Now call HalCreateQva. This will create a QVA
  187. // that we'll return to the caller. Then we will implicitly set
  188. // AddressSpace to a 1. The caller then will not call MmMapIoSpace
  189. // and will use the address we return as a VA.
  190. //
  191. TranslatedAddress->LowPart =
  192. (ULONG) HalCreateQva( *TranslatedAddress, va);
  193. //
  194. // clear high longword for QVA
  195. //
  196. TranslatedAddress->HighPart = 0;
  197. //
  198. // don't let the user call MmMapIoSpace
  199. //
  200. *AddressSpace = 1;
  201. return(TRUE);
  202. } // case BusMemory
  203. case BusIo: {
  204. //
  205. // The address is in PCI I/O space, kernel mode.
  206. //
  207. switch( InterfaceType ) {
  208. case Isa: {
  209. //
  210. // Can't go above 64KB (16 Bits) for Isa Buses
  211. //
  212. if( BusAddress.LowPart >= __64K ){
  213. *AddressSpace = 0;
  214. TranslatedAddress->LowPart = 0;
  215. return(FALSE);
  216. }
  217. break;
  218. } // case Isa
  219. case PCIBus: {
  220. //
  221. // PCI IO space is always below 16MB (24 Bits) BusAddress
  222. // If the address cannot be mapped, just return FALSE.
  223. //
  224. // IMPORTANT: For now we have set HAXR2 to 0(see ebinitnt.c)
  225. //
  226. if( BusAddress.LowPart >= __16MB ){
  227. *AddressSpace = 0;
  228. TranslatedAddress->LowPart = 0;
  229. return(FALSE);
  230. }
  231. //
  232. // if the BusAddress.LowPart is > 64K then we use the HAER2
  233. // register.
  234. //
  235. break;
  236. } // case PCIBus
  237. case Eisa: {
  238. break;
  239. } // case Eisa
  240. } // switch( InterfaceType )
  241. //
  242. // Start with the base physical address and add the
  243. // bus address by converting it to the physical address.
  244. //
  245. TranslatedAddress->QuadPart = APECS_PCI_IO_BASE_PHYSICAL;
  246. TranslatedAddress->QuadPart += ((ULONGLONG)BusAddress.LowPart << IO_BIT_SHIFT);
  247. //
  248. // Now call HalCreateQva. This will create a QVA
  249. // that we'll return to the caller. Then we will implicitly set
  250. // AddressSpace to a 1. The caller then will not call MmMapIoSpace
  251. // and will use the address we return as a VA.
  252. TranslatedAddress->LowPart = (ULONG) HalCreateQva( *TranslatedAddress,
  253. va);
  254. TranslatedAddress->HighPart = 0; // clear high longword for QVA
  255. *AddressSpace = 1; // Make sure user doesn't call
  256. // MmMapIoSpace.
  257. return(TRUE);
  258. } // case BusIo
  259. case UserBusMemory: {
  260. //
  261. // The address is in PCI memory space, user mode.
  262. //
  263. //
  264. // Start with the base physical address and add the
  265. // bus address by converting it to the physical address.
  266. //
  267. TranslatedAddress->QuadPart = APECS_PCI_MEMORY_BASE_PHYSICAL;
  268. TranslatedAddress->QuadPart += ((ULONGLONG)BusAddress.LowPart << IO_BIT_SHIFT);
  269. *AddressSpace = 0; // Let the user call MmMapIoSpace
  270. return(TRUE);
  271. }
  272. case UserBusIo: {
  273. //
  274. // The address is in PCI I/O space, user mode.
  275. //
  276. //
  277. // Start with the base physical address and add the
  278. // bus address by converting it to the physical address.
  279. //
  280. TranslatedAddress->QuadPart = APECS_PCI_IO_BASE_PHYSICAL;
  281. TranslatedAddress->QuadPart += ((ULONGLONG)BusAddress.LowPart << IO_BIT_SHIFT);
  282. *AddressSpace = 0; // Make sure user can call
  283. // MmMapIoSpace.
  284. return(TRUE);
  285. }
  286. case KernelPciDenseMemory:
  287. case UserPciDenseMemory:
  288. {
  289. //
  290. // The address is in PCI memory space, user mode.
  291. // Note that ISA bus devices can also request this space.
  292. //
  293. //
  294. // Start with the base physical address and add the
  295. // bus address by converting it to the physical address.
  296. //
  297. TranslatedAddress->QuadPart = APECS_PCI_DENSE_BASE_PHYSICAL;
  298. TranslatedAddress->QuadPart += BusAddress.LowPart;
  299. *AddressSpace = 0; // Let the user call MmMapIoSpace
  300. return(TRUE);
  301. }
  302. default: {
  303. //
  304. // Unsupported address space.
  305. *AddressSpace = 0;
  306. TranslatedAddress->LowPart = 0;
  307. return(FALSE);
  308. }
  309. }
  310. }
  311. PVOID
  312. HalCreateQva(
  313. IN PHYSICAL_ADDRESS PA,
  314. IN PVOID VA
  315. )
  316. /*++
  317. Routine Description:
  318. This function is called two ways. First, from HalTranslateBusAddress,
  319. if the caller is going to run in kernel mode and use superpages.
  320. The second way is if the user is going to access in user mode.
  321. MmMapIoSpace or ZwViewMapOfSection will call this.
  322. If the input parameter VA is zero, then we assume super page and build
  323. a QUASI virtual address that is only usable by calling the hal I/O
  324. access routines.
  325. if the input parameter VA is non-zero, we assume the user has either
  326. called MmMapIoSpace or ZwMapViewOfSection and will use the user mode
  327. access macros.
  328. If the PA is not a sparse I/O space address (PCI I/O, PCI Memory),
  329. then return the VA as the QVA.
  330. Arguments:
  331. PA - the physical address generated by HalTranslateBusAddress
  332. VA - the virtual address returned by MmMapIoSpace
  333. Return Value:
  334. The returned value is a quasi virtual address in that it can be
  335. added to and subtracted from, but it cannot be used to access the
  336. bus directly. The top bits are set so that we can trap invalid
  337. accesses in the memory management subsystem. All access should be
  338. done through the Hal Access Routines in *ioacc.s if it was a superpage
  339. kernel mode access. If it is usermode, then the user mode access
  340. macros must be used.
  341. --*/
  342. {
  343. PVOID qva;
  344. if ((PA.QuadPart >= APECS_COMANCHE_BASE_PHYSICAL)
  345. &&
  346. (PA.QuadPart < APECS_PCI_DENSE_BASE_PHYSICAL)
  347. ) {
  348. //
  349. // The physical address is within one of the sparse I/O spaces.
  350. //
  351. if (VA == 0) {
  352. qva = (PVOID)(PA.QuadPart >> IO_BIT_SHIFT);
  353. } else {
  354. qva = (PVOID)((ULONG)VA >> IO_BIT_SHIFT);
  355. }
  356. qva = (PVOID)((ULONG)qva | QVA_ENABLE);
  357. return(qva);
  358. }
  359. //
  360. // It is not a sparse I/O space address, return the VA as the QVA
  361. //
  362. return(VA);
  363. }
  364. PVOID
  365. HalDereferenceQva(
  366. PVOID Qva,
  367. INTERFACE_TYPE InterfaceType,
  368. ULONG BusNumber
  369. )
  370. /*++
  371. Routine Description:
  372. This function performs the inverse of the HalCreateQva for I/O addresses
  373. that are memory-mapped (i.e. the quasi-virtual address was created from
  374. a virtual address rather than a physical address).
  375. Arguments:
  376. Qva - Supplies the quasi-virtual address to be converted back to a
  377. virtual address.
  378. InterfaceType - Supplies the interface type of the bus to which the
  379. Qva pertains.
  380. BusNumber - Supplies the bus number of the bus to which the Qva pertains.
  381. Return Value:
  382. The Virtual Address from which the quasi-address was originally created
  383. is returned.
  384. --*/
  385. {
  386. //
  387. // For EB64+ we have only 2 bus types:
  388. //
  389. // Isa
  390. // PCIBus
  391. //
  392. // We will allow Eisa as an alias for Isa. All other values not named
  393. // above will be considered bogus.
  394. //
  395. switch (InterfaceType ){
  396. case Isa:
  397. case Eisa:
  398. case PCIBus:
  399. //
  400. // Support dense space: check to see if it's really
  401. // a sparse space QVA.
  402. //
  403. if ( ((ULONG) Qva & QVA_SELECTORS) == QVA_ENABLE )
  404. {
  405. return( (PVOID)( (ULONG)Qva << IO_BIT_SHIFT ) );
  406. }
  407. else
  408. {
  409. return (Qva);
  410. }
  411. break;
  412. default:
  413. return NULL;
  414. }
  415. }