route80h.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <efi.h>
  2. #include <efilib.h>
  3. /* this example program changes the Reserved Page Route (RPR) bit on ICH10's General
  4. * Control And Status Register (GCS) from LPC to PCI. In practical terms, it routes
  5. * outb to port 80h to the PCI bus. */
  6. #define GCS_OFFSET_ADDR 0x3410
  7. #define GCS_RPR_SHIFT 2
  8. #define GCS_RPR_PCI 1
  9. #define GCS_RPR_LPC 0
  10. #define VENDOR_ID_INTEL 0x8086
  11. #define DEVICE_ID_LPCIF 0x3a16
  12. #define DEVICE_ID_COUGARPOINT_LPCIF 0x1c56
  13. static EFI_HANDLE ImageHandle;
  14. typedef struct {
  15. uint16_t vendor_id; /* 00-01 */
  16. uint16_t device_id; /* 02-03 */
  17. char pad[0xEB]; /* 04-EF */
  18. uint32_t rcba; /* F0-F3 */
  19. uint32_t reserved[3]; /* F4-FF */
  20. } lpcif_t;
  21. static inline void set_bit(volatile uint32_t *flag, int bit, int value)
  22. {
  23. uint32_t val = *flag;
  24. Print(L"current value is 0x%2x\n", val);
  25. if (value) {
  26. val |= (1 << bit);
  27. } else {
  28. val &= ~(1 << bit);
  29. }
  30. Print(L"setting value to 0x%2x\n", val);
  31. *flag = val;
  32. val = *flag;
  33. Print(L"new value is 0x%2x\n", val);
  34. }
  35. static inline int configspace_matches_ids(void *config, uint32_t vendor_id,
  36. uint32_t device_id)
  37. {
  38. uint32_t *cfg = config;
  39. if (cfg[0] == vendor_id && cfg[1] == device_id)
  40. return 1;
  41. return 0;
  42. }
  43. static int is_device(EFI_PCI_IO *pciio, uint16_t vendor_id, uint16_t device_id)
  44. {
  45. lpcif_t lpcif;
  46. EFI_STATUS rc;
  47. rc = uefi_call_wrapper(pciio->Pci.Read, 5, pciio, EfiPciIoWidthUint16, 0, 2, &lpcif);
  48. if (EFI_ERROR(rc))
  49. return 0;
  50. if (vendor_id == lpcif.vendor_id && device_id == lpcif.device_id)
  51. return 1;
  52. return 0;
  53. }
  54. static EFI_STATUS find_pci_device(uint16_t vendor_id, uint16_t device_id,
  55. EFI_PCI_IO **pciio)
  56. {
  57. EFI_STATUS rc;
  58. EFI_HANDLE *Handles;
  59. UINTN NoHandles;
  60. int i;
  61. if (!pciio)
  62. return EFI_INVALID_PARAMETER;
  63. rc = LibLocateHandle(ByProtocol, &PciIoProtocol, NULL, &NoHandles,
  64. &Handles);
  65. if (EFI_ERROR(rc))
  66. return rc;
  67. for (i = 0; i < NoHandles; i++) {
  68. void *pciio_tmp = NULL;
  69. rc = uefi_call_wrapper(BS->OpenProtocol, 6, Handles[i],
  70. &PciIoProtocol, &pciio_tmp, ImageHandle,
  71. NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  72. if (EFI_ERROR(rc))
  73. continue;
  74. *pciio = pciio_tmp;
  75. if (!is_device(*pciio, vendor_id, device_id)) {
  76. *pciio = NULL;
  77. continue;
  78. }
  79. return EFI_SUCCESS;
  80. }
  81. return EFI_NOT_FOUND;
  82. }
  83. EFI_STATUS
  84. efi_main (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
  85. {
  86. InitializeLib(image_handle, systab);
  87. EFI_PCI_IO *pciio = NULL;
  88. lpcif_t lpcif;
  89. EFI_STATUS rc;
  90. struct {
  91. uint16_t vendor;
  92. uint16_t device;
  93. } devices[] = {
  94. { VENDOR_ID_INTEL, DEVICE_ID_LPCIF },
  95. { VENDOR_ID_INTEL, DEVICE_ID_COUGARPOINT_LPCIF },
  96. { 0, 0 }
  97. };
  98. int i;
  99. ImageHandle = image_handle;
  100. for (i = 0; devices[i].vendor != 0; i++) {
  101. rc = find_pci_device(devices[i].vendor, devices[i].device, &pciio);
  102. if (EFI_ERROR(rc))
  103. continue;
  104. }
  105. if (rc == EFI_NOT_FOUND) {
  106. Print(L"Device not found.\n");
  107. return rc;
  108. } else if (EFI_ERROR(rc)) {
  109. return rc;
  110. }
  111. rc = uefi_call_wrapper(pciio->Pci.Read, 5, pciio, EfiPciIoWidthUint32,
  112. EFI_FIELD_OFFSET(lpcif_t, rcba), 1, &lpcif.rcba);
  113. if (EFI_ERROR(rc))
  114. return rc;
  115. if (!(lpcif.rcba & 1)) {
  116. Print(L"rcrb is not mapped, cannot route port 80h\n");
  117. return EFI_UNSUPPORTED;
  118. }
  119. lpcif.rcba &= ~1UL;
  120. Print(L"rcba: 0x%8x\n", lpcif.rcba, lpcif.rcba);
  121. set_bit((uint32_t *)(uint64_t)(lpcif.rcba + GCS_OFFSET_ADDR),
  122. GCS_RPR_SHIFT, GCS_RPR_PCI);
  123. return EFI_SUCCESS;
  124. }