PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/PciRootBridgeNoEnumerationDxe/DeviceIo.c

https://github.com/SunnyKi/bareBoot
C | 845 lines | 440 code | 78 blank | 327 comment | 56 complexity | 201f66a47750291c58a6b0c73f4a442d MD5 | raw file
  1. /*++
  2. Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials
  4. are licensed and made available under the terms and conditions of the BSD License
  5. which accompanies this distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. DeviceIo.c
  11. Abstract:
  12. EFI PC-AT PCI Device IO driver
  13. --*/
  14. #include "PcatPciRootBridge.h"
  15. #include "DeviceIo.h"
  16. EFI_STATUS
  17. DeviceIoConstructor (
  18. IN EFI_HANDLE Handle,
  19. IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo,
  20. IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  21. IN UINT16 PrimaryBus,
  22. IN UINT16 SubordinateBus
  23. )
  24. /*++
  25. Routine Description:
  26. Initialize and install a Device IO protocol on a empty device path handle.
  27. Arguments:
  28. Handle - Handle of PCI RootBridge IO instance
  29. PciRootBridgeIo - PCI RootBridge IO instance
  30. DevicePath - Device Path of PCI RootBridge IO instance
  31. PrimaryBus - Primary Bus
  32. SubordinateBus - Subordinate Bus
  33. Returns:
  34. EFI_SUCCESS - This driver is added to ControllerHandle.
  35. EFI_ALREADY_STARTED - This driver is already running on ControllerHandle.
  36. Others - This driver does not support this device.
  37. --*/
  38. {
  39. EFI_STATUS Status;
  40. DEVICE_IO_PRIVATE_DATA *Private;
  41. //
  42. // Initialize the Device IO device instance.
  43. //
  44. Private = AllocateZeroPool (sizeof (DEVICE_IO_PRIVATE_DATA));
  45. if (Private == NULL) {
  46. return EFI_OUT_OF_RESOURCES;
  47. }
  48. Private->Signature = DEVICE_IO_PRIVATE_DATA_SIGNATURE;
  49. Private->Handle = Handle;
  50. Private->PciRootBridgeIo = PciRootBridgeIo;
  51. Private->DevicePath = DevicePath;
  52. Private->PrimaryBus = PrimaryBus;
  53. Private->SubordinateBus = SubordinateBus;
  54. Private->DeviceIo.Mem.Read = DeviceIoMemRead;
  55. Private->DeviceIo.Mem.Write = DeviceIoMemWrite;
  56. Private->DeviceIo.Io.Read = DeviceIoIoRead;
  57. Private->DeviceIo.Io.Write = DeviceIoIoWrite;
  58. Private->DeviceIo.Pci.Read = DeviceIoPciRead;
  59. Private->DeviceIo.Pci.Write = DeviceIoPciWrite;
  60. Private->DeviceIo.PciDevicePath = DeviceIoPciDevicePath;
  61. Private->DeviceIo.Map = DeviceIoMap;
  62. Private->DeviceIo.Unmap = DeviceIoUnmap;
  63. Private->DeviceIo.AllocateBuffer = DeviceIoAllocateBuffer;
  64. Private->DeviceIo.Flush = DeviceIoFlush;
  65. Private->DeviceIo.FreeBuffer = DeviceIoFreeBuffer;
  66. //
  67. // Install protocol interfaces for the Device IO device.
  68. //
  69. Status = gBS->InstallMultipleProtocolInterfaces (
  70. &Private->Handle,
  71. &gEfiDeviceIoProtocolGuid,
  72. &Private->DeviceIo,
  73. NULL
  74. );
  75. ASSERT_EFI_ERROR (Status);
  76. return Status;
  77. }
  78. EFI_STATUS
  79. EFIAPI
  80. DeviceIoMemRead (
  81. IN EFI_DEVICE_IO_PROTOCOL *This,
  82. IN EFI_IO_WIDTH Width,
  83. IN UINT64 Address,
  84. IN UINTN Count,
  85. IN OUT VOID *Buffer
  86. )
  87. /*++
  88. Routine Description:
  89. Perform reading memory mapped I/O space of device.
  90. Arguments:
  91. This - A pointer to EFI_DEVICE_IO protocol instance.
  92. Width - Width of I/O operations.
  93. Address - The base address of I/O operations.
  94. Count - The number of I/O operations to perform.
  95. Bytes moves is Width size * Count, starting at Address.
  96. Buffer - The destination buffer to store results.
  97. Returns:
  98. EFI_SUCCESS - The data was read from the device.
  99. EFI_INVALID_PARAMETER - Width is invalid.
  100. EFI_OUT_OF_RESOURCES - The request could not be completed due to lack of resources.
  101. --*/
  102. {
  103. EFI_STATUS Status;
  104. DEVICE_IO_PRIVATE_DATA *Private;
  105. Private = DEVICE_IO_PRIVATE_DATA_FROM_THIS (This);
  106. if (Width > MMIO_COPY_UINT64) {
  107. return EFI_INVALID_PARAMETER;
  108. }
  109. if (Width >= MMIO_COPY_UINT8) {
  110. Width = (EFI_IO_WIDTH) (Width - MMIO_COPY_UINT8);
  111. Status = Private->PciRootBridgeIo->CopyMem (
  112. Private->PciRootBridgeIo,
  113. (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,
  114. (UINT64)(UINTN) Buffer,
  115. Address,
  116. Count
  117. );
  118. } else {
  119. Status = Private->PciRootBridgeIo->Mem.Read (
  120. Private->PciRootBridgeIo,
  121. (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,
  122. Address,
  123. Count,
  124. Buffer
  125. );
  126. }
  127. return Status;
  128. }
  129. EFI_STATUS
  130. EFIAPI
  131. DeviceIoMemWrite (
  132. IN EFI_DEVICE_IO_PROTOCOL *This,
  133. IN EFI_IO_WIDTH Width,
  134. IN UINT64 Address,
  135. IN UINTN Count,
  136. IN OUT VOID *Buffer
  137. )
  138. /*++
  139. Routine Description:
  140. Perform writing memory mapped I/O space of device.
  141. Arguments:
  142. This - A pointer to EFI_DEVICE_IO protocol instance.
  143. Width - Width of I/O operations.
  144. Address - The base address of I/O operations.
  145. Count - The number of I/O operations to perform.
  146. Bytes moves is Width size * Count, starting at Address.
  147. Buffer - The source buffer of data to be written.
  148. Returns:
  149. EFI_SUCCESS - The data was written to the device.
  150. EFI_INVALID_PARAMETER - Width is invalid.
  151. EFI_OUT_OF_RESOURCES - The request could not be completed due to lack of resources.
  152. --*/
  153. {
  154. EFI_STATUS Status;
  155. DEVICE_IO_PRIVATE_DATA *Private;
  156. Private = DEVICE_IO_PRIVATE_DATA_FROM_THIS (This);
  157. if (Width > MMIO_COPY_UINT64) {
  158. return EFI_INVALID_PARAMETER;
  159. }
  160. if (Width >= MMIO_COPY_UINT8) {
  161. Width = (EFI_IO_WIDTH) (Width - MMIO_COPY_UINT8);
  162. Status = Private->PciRootBridgeIo->CopyMem (
  163. Private->PciRootBridgeIo,
  164. (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,
  165. Address,
  166. (UINT64)(UINTN) Buffer,
  167. Count
  168. );
  169. } else {
  170. Status = Private->PciRootBridgeIo->Mem.Write (
  171. Private->PciRootBridgeIo,
  172. (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,
  173. Address,
  174. Count,
  175. Buffer
  176. );
  177. }
  178. return Status;
  179. }
  180. EFI_STATUS
  181. EFIAPI
  182. DeviceIoIoRead (
  183. IN EFI_DEVICE_IO_PROTOCOL *This,
  184. IN EFI_IO_WIDTH Width,
  185. IN UINT64 Address,
  186. IN UINTN Count,
  187. IN OUT VOID *Buffer
  188. )
  189. /*++
  190. Routine Description:
  191. Perform reading I/O space of device.
  192. Arguments:
  193. This - A pointer to EFI_DEVICE_IO protocol instance.
  194. Width - Width of I/O operations.
  195. Address - The base address of I/O operations.
  196. Count - The number of I/O operations to perform.
  197. Bytes moves is Width size * Count, starting at Address.
  198. Buffer - The destination buffer to store results.
  199. Returns:
  200. EFI_SUCCESS - The data was read from the device.
  201. EFI_INVALID_PARAMETER - Width is invalid.
  202. EFI_OUT_OF_RESOURCES - The request could not be completed due to lack of resources.
  203. --*/
  204. {
  205. EFI_STATUS Status;
  206. DEVICE_IO_PRIVATE_DATA *Private;
  207. Private = DEVICE_IO_PRIVATE_DATA_FROM_THIS (This);
  208. if (Width >= MMIO_COPY_UINT8) {
  209. return EFI_INVALID_PARAMETER;
  210. }
  211. Status = Private->PciRootBridgeIo->Io.Read (
  212. Private->PciRootBridgeIo,
  213. (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,
  214. Address,
  215. Count,
  216. Buffer
  217. );
  218. return Status;
  219. }
  220. EFI_STATUS
  221. EFIAPI
  222. DeviceIoIoWrite (
  223. IN EFI_DEVICE_IO_PROTOCOL *This,
  224. IN EFI_IO_WIDTH Width,
  225. IN UINT64 Address,
  226. IN UINTN Count,
  227. IN OUT VOID *Buffer
  228. )
  229. /*++
  230. Routine Description:
  231. Perform writing I/O space of device.
  232. Arguments:
  233. This - A pointer to EFI_DEVICE_IO protocol instance.
  234. Width - Width of I/O operations.
  235. Address - The base address of I/O operations.
  236. Count - The number of I/O operations to perform.
  237. Bytes moves is Width size * Count, starting at Address.
  238. Buffer - The source buffer of data to be written.
  239. Returns:
  240. EFI_SUCCESS - The data was written to the device.
  241. EFI_INVALID_PARAMETER - Width is invalid.
  242. EFI_OUT_OF_RESOURCES - The request could not be completed due to lack of resources.
  243. --*/
  244. {
  245. EFI_STATUS Status;
  246. DEVICE_IO_PRIVATE_DATA *Private;
  247. Private = DEVICE_IO_PRIVATE_DATA_FROM_THIS (This);
  248. if (Width >= MMIO_COPY_UINT8) {
  249. return EFI_INVALID_PARAMETER;
  250. }
  251. Status = Private->PciRootBridgeIo->Io.Write (
  252. Private->PciRootBridgeIo,
  253. (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,
  254. Address,
  255. Count,
  256. Buffer
  257. );
  258. return Status;
  259. }
  260. EFI_STATUS
  261. EFIAPI
  262. DeviceIoPciRead (
  263. IN EFI_DEVICE_IO_PROTOCOL *This,
  264. IN EFI_IO_WIDTH Width,
  265. IN UINT64 Address,
  266. IN UINTN Count,
  267. IN OUT VOID *Buffer
  268. )
  269. /*++
  270. Routine Description:
  271. Perform reading PCI configuration space of device
  272. Arguments:
  273. This - A pointer to EFI_DEVICE_IO protocol instance.
  274. Width - Width of I/O operations.
  275. Address - The base address of I/O operations.
  276. Count - The number of I/O operations to perform.
  277. Bytes moves is Width size * Count, starting at Address.
  278. Buffer - The destination buffer to store results.
  279. Returns:
  280. EFI_SUCCESS - The data was read from the device.
  281. EFI_INVALID_PARAMETER - Width is invalid.
  282. EFI_OUT_OF_RESOURCES - The request could not be completed due to lack of resources.
  283. --*/
  284. {
  285. EFI_STATUS Status;
  286. DEVICE_IO_PRIVATE_DATA *Private;
  287. Private = DEVICE_IO_PRIVATE_DATA_FROM_THIS (This);
  288. if ((UINT32)Width >= MMIO_COPY_UINT8) {
  289. return EFI_INVALID_PARAMETER;
  290. }
  291. Status = Private->PciRootBridgeIo->Pci.Read (
  292. Private->PciRootBridgeIo,
  293. (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,
  294. Address,
  295. Count,
  296. Buffer
  297. );
  298. return Status;
  299. }
  300. EFI_STATUS
  301. EFIAPI
  302. DeviceIoPciWrite (
  303. IN EFI_DEVICE_IO_PROTOCOL *This,
  304. IN EFI_IO_WIDTH Width,
  305. IN UINT64 Address,
  306. IN UINTN Count,
  307. IN OUT VOID *Buffer
  308. )
  309. /*++
  310. Routine Description:
  311. Perform writing PCI configuration space of device.
  312. Arguments:
  313. This - A pointer to EFI_DEVICE_IO protocol instance.
  314. Width - Width of I/O operations.
  315. Address - The base address of I/O operations.
  316. Count - The number of I/O operations to perform.
  317. Bytes moves is Width size * Count, starting at Address.
  318. Buffer - The source buffer of data to be written.
  319. Returns:
  320. EFI_SUCCESS - The data was written to the device.
  321. EFI_INVALID_PARAMETER - Width is invalid.
  322. EFI_OUT_OF_RESOURCES - The request could not be completed due to lack of resources.
  323. --*/
  324. {
  325. EFI_STATUS Status;
  326. DEVICE_IO_PRIVATE_DATA *Private;
  327. Private = DEVICE_IO_PRIVATE_DATA_FROM_THIS (This);
  328. if ((UINT32)Width >= MMIO_COPY_UINT8) {
  329. return EFI_INVALID_PARAMETER;
  330. }
  331. Status = Private->PciRootBridgeIo->Pci.Write (
  332. Private->PciRootBridgeIo,
  333. (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,
  334. Address,
  335. Count,
  336. Buffer
  337. );
  338. return Status;
  339. }
  340. EFI_DEVICE_PATH_PROTOCOL *
  341. AppendPciDevicePath (
  342. IN DEVICE_IO_PRIVATE_DATA *Private,
  343. IN UINT8 Bus,
  344. IN UINT8 Device,
  345. IN UINT8 Function,
  346. IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  347. IN OUT UINT16 *BridgePrimaryBus,
  348. IN OUT UINT16 *BridgeSubordinateBus
  349. )
  350. /*++
  351. Routine Description:
  352. Append a PCI device path node to another device path.
  353. Arguments:
  354. Private - A pointer to DEVICE_IO_PRIVATE_DATA instance.
  355. Bus - PCI bus number of the device.
  356. Device - PCI device number of the device.
  357. Function - PCI function number of the device.
  358. DevicePath - Original device path which will be appended a PCI device path node.
  359. BridgePrimaryBus - Primary bus number of the bridge.
  360. BridgeSubordinateBus - Subordinate bus number of the bridge.
  361. Returns:
  362. Pointer to the appended PCI device path.
  363. --*/
  364. {
  365. UINT16 ThisBus;
  366. UINT8 ThisDevice;
  367. UINT8 ThisFunc;
  368. UINT64 Address;
  369. PCI_TYPE01 PciBridge;
  370. PCI_TYPE01 *PciPtr;
  371. EFI_DEVICE_PATH_PROTOCOL *ReturnDevicePath;
  372. PCI_DEVICE_PATH PciNode;
  373. PciPtr = &PciBridge;
  374. for (ThisBus = *BridgePrimaryBus; ThisBus <= *BridgeSubordinateBus; ThisBus++) {
  375. for (ThisDevice = 0; ThisDevice <= PCI_MAX_DEVICE; ThisDevice++) {
  376. for (ThisFunc = 0; ThisFunc <= PCI_MAX_FUNC; ThisFunc++) {
  377. Address = EFI_PCI_ADDRESS (ThisBus, ThisDevice, ThisFunc, 0);
  378. ZeroMem (PciPtr, sizeof (PCI_TYPE01));
  379. Private->DeviceIo.Pci.Read (
  380. &Private->DeviceIo,
  381. IO_UINT32,
  382. Address,
  383. 1,
  384. &(PciPtr->Hdr.VendorId)
  385. );
  386. if ((PciPtr->Hdr.VendorId == 0xffff) && (ThisFunc == 0)) {
  387. break;
  388. }
  389. if (PciPtr->Hdr.VendorId == 0xffff) {
  390. continue;
  391. }
  392. Private->DeviceIo.Pci.Read (
  393. &Private->DeviceIo,
  394. IO_UINT32,
  395. Address,
  396. sizeof (PCI_TYPE01) / sizeof (UINT32),
  397. PciPtr
  398. );
  399. if (IS_PCI_BRIDGE (PciPtr)) {
  400. if (Bus >= PciPtr->Bridge.SecondaryBus && Bus <= PciPtr->Bridge.SubordinateBus) {
  401. PciNode.Header.Type = HARDWARE_DEVICE_PATH;
  402. PciNode.Header.SubType = HW_PCI_DP;
  403. SetDevicePathNodeLength (&PciNode.Header, sizeof (PciNode));
  404. PciNode.Device = ThisDevice;
  405. PciNode.Function = ThisFunc;
  406. ReturnDevicePath = AppendDevicePathNode (DevicePath, &PciNode.Header);
  407. *BridgePrimaryBus = PciPtr->Bridge.SecondaryBus;
  408. *BridgeSubordinateBus = PciPtr->Bridge.SubordinateBus;
  409. return ReturnDevicePath;
  410. }
  411. }
  412. if ((ThisFunc == 0) && ((PciPtr->Hdr.HeaderType & HEADER_TYPE_MULTI_FUNCTION) == 0x0)) {
  413. //
  414. // Skip sub functions, this is not a multi function device
  415. //
  416. break;
  417. }
  418. }
  419. }
  420. }
  421. ZeroMem (&PciNode, sizeof (PciNode));
  422. PciNode.Header.Type = HARDWARE_DEVICE_PATH;
  423. PciNode.Header.SubType = HW_PCI_DP;
  424. SetDevicePathNodeLength (&PciNode.Header, sizeof (PciNode));
  425. PciNode.Device = Device;
  426. PciNode.Function = Function;
  427. ReturnDevicePath = AppendDevicePathNode (DevicePath, &PciNode.Header);
  428. *BridgePrimaryBus = 0xffff;
  429. *BridgeSubordinateBus = 0xffff;
  430. return ReturnDevicePath;
  431. }
  432. EFI_STATUS
  433. EFIAPI
  434. DeviceIoPciDevicePath (
  435. IN EFI_DEVICE_IO_PROTOCOL *This,
  436. IN UINT64 Address,
  437. IN OUT EFI_DEVICE_PATH_PROTOCOL **PciDevicePath
  438. )
  439. /*++
  440. Routine Description:
  441. Provides an EFI Device Path for a PCI device with the given PCI configuration space address.
  442. Arguments:
  443. This - A pointer to the EFI_DEVICE_IO_INTERFACE instance.
  444. Address - The PCI configuration space address of the device whose Device Path
  445. is going to be returned.
  446. PciDevicePath - A pointer to the pointer for the EFI Device Path for PciAddress.
  447. Memory for the Device Path is allocated from the pool.
  448. Returns:
  449. EFI_SUCCESS - The PciDevicePath returns a pointer to a valid EFI Device Path.
  450. EFI_UNSUPPORTED - The PciAddress does not map to a valid EFI Device Path.
  451. EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
  452. --*/
  453. {
  454. DEVICE_IO_PRIVATE_DATA *Private;
  455. UINT16 PrimaryBus;
  456. UINT16 SubordinateBus;
  457. UINT8 Bus;
  458. UINT8 Device;
  459. UINT8 Func;
  460. Private = DEVICE_IO_PRIVATE_DATA_FROM_THIS (This);
  461. Bus = (UINT8) (((UINT32) Address >> 24) & 0xff);
  462. Device = (UINT8) (((UINT32) Address >> 16) & 0xff);
  463. Func = (UINT8) (((UINT32) Address >> 8) & 0xff);
  464. if (Bus < Private->PrimaryBus || Bus > Private->SubordinateBus) {
  465. return EFI_UNSUPPORTED;
  466. }
  467. *PciDevicePath = Private->DevicePath;
  468. PrimaryBus = Private->PrimaryBus;
  469. SubordinateBus = Private->SubordinateBus;
  470. do {
  471. *PciDevicePath = AppendPciDevicePath (
  472. Private,
  473. Bus,
  474. Device,
  475. Func,
  476. *PciDevicePath,
  477. &PrimaryBus,
  478. &SubordinateBus
  479. );
  480. if (*PciDevicePath == NULL) {
  481. return EFI_OUT_OF_RESOURCES;
  482. }
  483. } while (PrimaryBus != 0xffff);
  484. return EFI_SUCCESS;
  485. }
  486. EFI_STATUS
  487. EFIAPI
  488. DeviceIoMap (
  489. IN EFI_DEVICE_IO_PROTOCOL *This,
  490. IN EFI_IO_OPERATION_TYPE Operation,
  491. IN EFI_PHYSICAL_ADDRESS *HostAddress,
  492. IN OUT UINTN *NumberOfBytes,
  493. OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
  494. OUT VOID **Mapping
  495. )
  496. /*++
  497. Routine Description:
  498. Provides the device-specific addresses needed to access system memory.
  499. Arguments:
  500. This - A pointer to the EFI_DEVICE_IO_INTERFACE instance.
  501. Operation - Indicates if the bus master is going to read or write to system memory.
  502. HostAddress - The system memory address to map to the device.
  503. NumberOfBytes - On input the number of bytes to map. On output the number of bytes
  504. that were mapped.
  505. DeviceAddress - The resulting map address for the bus master device to use to access the
  506. hosts HostAddress.
  507. Mapping - A resulting value to pass to Unmap().
  508. Returns:
  509. EFI_SUCCESS - The range was mapped for the returned NumberOfBytes.
  510. EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
  511. EFI_UNSUPPORTED - The HostAddress cannot be mapped as a common buffer.
  512. EFI_DEVICE_ERROR - The system hardware could not map the requested address.
  513. EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
  514. --*/
  515. {
  516. EFI_STATUS Status;
  517. DEVICE_IO_PRIVATE_DATA *Private;
  518. Private = DEVICE_IO_PRIVATE_DATA_FROM_THIS (This);
  519. if ((UINT32)Operation > EfiBusMasterCommonBuffer) {
  520. return EFI_INVALID_PARAMETER;
  521. }
  522. if (((UINTN) (*HostAddress) != (*HostAddress)) && Operation == EfiBusMasterCommonBuffer) {
  523. return EFI_UNSUPPORTED;
  524. }
  525. Status = Private->PciRootBridgeIo->Map (
  526. Private->PciRootBridgeIo,
  527. (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION) Operation,
  528. (VOID *) (UINTN) (*HostAddress),
  529. NumberOfBytes,
  530. DeviceAddress,
  531. Mapping
  532. );
  533. return Status;
  534. }
  535. EFI_STATUS
  536. EFIAPI
  537. DeviceIoUnmap (
  538. IN EFI_DEVICE_IO_PROTOCOL *This,
  539. IN VOID *Mapping
  540. )
  541. /*++
  542. Routine Description:
  543. Completes the Map() operation and releases any corresponding resources.
  544. Arguments:
  545. This - A pointer to the EFI_DEVICE_IO_INTERFACE instance.
  546. Mapping - The mapping value returned from Map().
  547. Returns:
  548. EFI_SUCCESS - The range was unmapped.
  549. EFI_DEVICE_ERROR - The data was not committed to the target system memory.
  550. --*/
  551. {
  552. EFI_STATUS Status;
  553. DEVICE_IO_PRIVATE_DATA *Private;
  554. Private = DEVICE_IO_PRIVATE_DATA_FROM_THIS (This);
  555. Status = Private->PciRootBridgeIo->Unmap (
  556. Private->PciRootBridgeIo,
  557. Mapping
  558. );
  559. return Status;
  560. }
  561. EFI_STATUS
  562. EFIAPI
  563. DeviceIoAllocateBuffer (
  564. IN EFI_DEVICE_IO_PROTOCOL *This,
  565. IN EFI_ALLOCATE_TYPE Type,
  566. IN EFI_MEMORY_TYPE MemoryType,
  567. IN UINTN Pages,
  568. IN OUT EFI_PHYSICAL_ADDRESS *PhysicalAddress
  569. )
  570. /*++
  571. Routine Description:
  572. Allocates pages that are suitable for an EFIBusMasterCommonBuffer mapping.
  573. Arguments:
  574. This - A pointer to the EFI_DEVICE_IO_INTERFACE instance.
  575. Type - The type allocation to perform.
  576. MemoryType - The type of memory to allocate, EfiBootServicesData or
  577. EfiRuntimeServicesData.
  578. Pages - The number of pages to allocate.
  579. PhysicalAddress - A pointer to store the base address of the allocated range.
  580. Returns:
  581. EFI_SUCCESS - The requested memory pages were allocated.
  582. EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
  583. EFI_INVALID_PARAMETER - The requested memory type is invalid.
  584. EFI_UNSUPPORTED - The requested PhysicalAddress is not supported on
  585. this platform.
  586. --*/
  587. {
  588. EFI_STATUS Status;
  589. EFI_PHYSICAL_ADDRESS HostAddress;
  590. HostAddress = *PhysicalAddress;
  591. if ((MemoryType != EfiBootServicesData) && (MemoryType != EfiRuntimeServicesData)) {
  592. return EFI_INVALID_PARAMETER;
  593. }
  594. if ((UINT32)Type >= MaxAllocateType) {
  595. return EFI_INVALID_PARAMETER;
  596. }
  597. if ((Type == AllocateAddress) && (HostAddress + EFI_PAGES_TO_SIZE (Pages) - 1 > MAX_COMMON_BUFFER)) {
  598. return EFI_UNSUPPORTED;
  599. }
  600. if ((AllocateAnyPages == Type) || (AllocateMaxAddress == Type && HostAddress > MAX_COMMON_BUFFER)) {
  601. Type = AllocateMaxAddress;
  602. HostAddress = MAX_COMMON_BUFFER;
  603. }
  604. Status = gBS->AllocatePages (
  605. Type,
  606. MemoryType,
  607. Pages,
  608. &HostAddress
  609. );
  610. if (EFI_ERROR (Status)) {
  611. return Status;
  612. }
  613. *PhysicalAddress = HostAddress;
  614. return EFI_SUCCESS;
  615. }
  616. EFI_STATUS
  617. EFIAPI
  618. DeviceIoFlush (
  619. IN EFI_DEVICE_IO_PROTOCOL *This
  620. )
  621. /*++
  622. Routine Description:
  623. Flushes any posted write data to the device.
  624. Arguments:
  625. This - A pointer to the EFI_DEVICE_IO_INTERFACE instance.
  626. Returns:
  627. EFI_SUCCESS - The buffers were flushed.
  628. EFI_DEVICE_ERROR - The buffers were not flushed due to a hardware error.
  629. --*/
  630. {
  631. EFI_STATUS Status;
  632. DEVICE_IO_PRIVATE_DATA *Private;
  633. Private = DEVICE_IO_PRIVATE_DATA_FROM_THIS (This);
  634. Status = Private->PciRootBridgeIo->Flush (Private->PciRootBridgeIo);
  635. return Status;
  636. }
  637. EFI_STATUS
  638. EFIAPI
  639. DeviceIoFreeBuffer (
  640. IN EFI_DEVICE_IO_PROTOCOL *This,
  641. IN UINTN Pages,
  642. IN EFI_PHYSICAL_ADDRESS HostAddress
  643. )
  644. /*++
  645. Routine Description:
  646. Frees pages that were allocated with AllocateBuffer().
  647. Arguments:
  648. This - A pointer to the EFI_DEVICE_IO_INTERFACE instance.
  649. Pages - The number of pages to free.
  650. HostAddress - The base address of the range to free.
  651. Returns:
  652. EFI_SUCCESS - The requested memory pages were freed.
  653. EFI_NOT_FOUND - The requested memory pages were not allocated with
  654. AllocateBuffer().
  655. EFI_INVALID_PARAMETER - HostAddress is not page aligned or Pages is invalid.
  656. --*/
  657. {
  658. if (((HostAddress & EFI_PAGE_MASK) != 0) || (Pages < 1)) {
  659. return EFI_INVALID_PARAMETER;
  660. }
  661. return gBS->FreePages (HostAddress, Pages);
  662. }