pci.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* pci.c - Generic PCI interfaces. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2007,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/pci.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm_private.h>
  24. #include <grub/cache.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. /* FIXME: correctly support 64-bit architectures. */
  27. /* #if GRUB_TARGET_SIZEOF_VOID_P == 4 */
  28. struct grub_pci_dma_chunk *
  29. grub_memalign_dma32 (grub_size_t align, grub_size_t size)
  30. {
  31. void *ret;
  32. if (align < 64)
  33. align = 64;
  34. size = ALIGN_UP (size, align);
  35. ret = grub_memalign (align, size);
  36. #if GRUB_CPU_SIZEOF_VOID_P == 8
  37. if ((grub_addr_t) ret >> 32)
  38. {
  39. /* Shouldn't happend since the only platform in this case is
  40. x86_64-efi and it skips any regions > 4GiB because
  41. of EFI bugs anyway. */
  42. grub_error (GRUB_ERR_BUG, "allocation outside 32-bit range");
  43. return 0;
  44. }
  45. #endif
  46. if (!ret)
  47. return 0;
  48. grub_arch_sync_dma_caches (ret, size);
  49. return ret;
  50. }
  51. /* FIXME: evil. */
  52. void
  53. grub_dma_free (struct grub_pci_dma_chunk *ch)
  54. {
  55. grub_size_t size = (((struct grub_mm_header *) ch) - 1)->size * GRUB_MM_ALIGN;
  56. grub_arch_sync_dma_caches (ch, size);
  57. grub_free (ch);
  58. }
  59. /* #endif */
  60. #ifdef GRUB_MACHINE_MIPS_LOONGSON
  61. volatile void *
  62. grub_dma_get_virt (struct grub_pci_dma_chunk *ch)
  63. {
  64. return (void *) ((((grub_uint32_t) ch) & 0x1fffffff) | 0xa0000000);
  65. }
  66. grub_uint32_t
  67. grub_dma_get_phys (struct grub_pci_dma_chunk *ch)
  68. {
  69. return (((grub_uint32_t) ch) & 0x1fffffff) | 0x80000000;
  70. }
  71. #else
  72. volatile void *
  73. grub_dma_get_virt (struct grub_pci_dma_chunk *ch)
  74. {
  75. return (void *) ch;
  76. }
  77. grub_uint32_t
  78. grub_dma_get_phys (struct grub_pci_dma_chunk *ch)
  79. {
  80. return (grub_uint32_t) (grub_addr_t) ch;
  81. }
  82. #endif
  83. grub_pci_address_t
  84. grub_pci_make_address (grub_pci_device_t dev, int reg)
  85. {
  86. return (1 << 31) | (dev.bus << 16) | (dev.device << 11)
  87. | (dev.function << 8) | reg;
  88. }
  89. void
  90. grub_pci_iterate (grub_pci_iteratefunc_t hook, void *hook_data)
  91. {
  92. grub_pci_device_t dev;
  93. grub_pci_address_t addr;
  94. grub_pci_id_t id;
  95. grub_uint32_t hdr;
  96. for (dev.bus = 0; dev.bus < GRUB_PCI_NUM_BUS; dev.bus++)
  97. {
  98. for (dev.device = 0; dev.device < GRUB_PCI_NUM_DEVICES; dev.device++)
  99. {
  100. for (dev.function = 0; dev.function < 8; dev.function++)
  101. {
  102. addr = grub_pci_make_address (dev, GRUB_PCI_REG_PCI_ID);
  103. id = grub_pci_read (addr);
  104. /* Check if there is a device present. */
  105. if (id >> 16 == 0xFFFF)
  106. {
  107. if (dev.function == 0)
  108. /* Devices are required to implement function 0, so if
  109. it's missing then there is no device here. */
  110. break;
  111. else
  112. continue;
  113. }
  114. if (hook (dev, id, hook_data))
  115. return;
  116. /* Probe only func = 0 if the device if not multifunction */
  117. if (dev.function == 0)
  118. {
  119. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CACHELINE);
  120. hdr = grub_pci_read (addr);
  121. if (!(hdr & 0x800000))
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. grub_uint8_t
  129. grub_pci_find_capability (grub_pci_device_t dev, grub_uint8_t cap)
  130. {
  131. grub_uint8_t pos = 0x34;
  132. int ttl = 48;
  133. while (ttl--)
  134. {
  135. grub_uint8_t id;
  136. grub_pci_address_t addr;
  137. addr = grub_pci_make_address (dev, pos);
  138. pos = grub_pci_read_byte (addr);
  139. if (pos < 0x40)
  140. break;
  141. pos &= ~3;
  142. addr = grub_pci_make_address (dev, pos);
  143. id = grub_pci_read_byte (addr);
  144. if (id == 0xff)
  145. break;
  146. if (id == cap)
  147. return pos;
  148. pos++;
  149. }
  150. return 0;
  151. }