pci.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. GRUB_EXPORT(grub_pci_make_address);
  22. GRUB_EXPORT(grub_pci_iterate);
  23. grub_pci_address_t
  24. grub_pci_make_address (grub_pci_device_t dev, int reg)
  25. {
  26. return (1 << 31) | (dev.bus << 16) | (dev.device << 11)
  27. | (dev.function << 8) | reg;
  28. }
  29. void
  30. grub_pci_iterate (grub_pci_iteratefunc_t hook, void *closure)
  31. {
  32. grub_pci_device_t dev;
  33. grub_pci_address_t addr;
  34. grub_pci_id_t id;
  35. grub_uint32_t hdr;
  36. for (dev.bus = 0; dev.bus < GRUB_PCI_NUM_BUS; dev.bus++)
  37. {
  38. for (dev.device = 0; dev.device < GRUB_PCI_NUM_DEVICES; dev.device++)
  39. {
  40. for (dev.function = 0; dev.function < 8; dev.function++)
  41. {
  42. addr = grub_pci_make_address (dev, GRUB_PCI_REG_PCI_ID);
  43. id = grub_pci_read (addr);
  44. /* Check if there is a device present. */
  45. if (id >> 16 == 0xFFFF)
  46. continue;
  47. if (hook (dev, id, closure))
  48. return;
  49. /* Probe only func = 0 if the device if not multifunction */
  50. if (dev.function == 0)
  51. {
  52. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CACHELINE);
  53. hdr = grub_pci_read (addr);
  54. if (!(hdr & 0x800000))
  55. break;
  56. }
  57. }
  58. }
  59. }
  60. }