gpu.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* GPU vendor detection and vendor-specific init dispatch. */
  2. #include "gpu.h"
  3. #include "pci.h"
  4. #include "ati.h"
  5. #include "cirrus.h"
  6. #include "nvidia.h"
  7. static int gpu_vendor_id;
  8. static unsigned int gpu_pci_id_val;
  9. /*
  10. * Resolve the *active* display adapter: the device whose framebuffer BAR
  11. * contains the VBE linear framebuffer base. A machine can carry several
  12. * VGA-class devices (e.g. an onboard Cirrus plus a plug-in ATI card), and the
  13. * one that served the VBE mode set is not necessarily the first one the PCI
  14. * scan hits. When no BAR contains the LFB (no VBE mode, or a card that maps
  15. * its framebuffer through a non-BAR0 window), fall back to the first VGA
  16. * device found.
  17. */
  18. static unsigned int gpu_bar_size(unsigned int bus, unsigned int dev, unsigned int bar_reg)
  19. {
  20. unsigned int bar = pci_cfg_read32(bus, dev, 0, bar_reg);
  21. unsigned int raw;
  22. if ((bar & 1) || !(bar & 0xFFFFFFF0))
  23. return 0;
  24. pci_cfg_write32(bus, dev, 0, bar_reg, 0xFFFFFFFF);
  25. raw = pci_cfg_read32(bus, dev, 0, bar_reg);
  26. pci_cfg_write32(bus, dev, 0, bar_reg, bar);
  27. return (~(raw & 0xFFFFFFF0)) + 1;
  28. }
  29. void gpu_init(unsigned int lfb_base)
  30. {
  31. unsigned int bus, dev;
  32. unsigned int active_bus = 0, active_dev = 0, active_vendor = 0, active_id = 0;
  33. unsigned int fb_bus = 0, fb_dev = 0, fb_vendor = 0, fb_id = 0;
  34. for (bus = 0; bus < 256; bus++) {
  35. for (dev = 0; dev < 32; dev++) {
  36. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  37. unsigned int class_rev, vendor, bar0, bar0_base, bar0_size;
  38. if (id == 0xFFFFFFFF || id == 0)
  39. continue;
  40. class_rev = pci_cfg_read32(bus, dev, 0, 0x08);
  41. if ((class_rev >> 24) != 0x03)
  42. continue;
  43. vendor = id & 0xFFFF;
  44. /* Remember the first VGA device as a fallback. */
  45. if (!active_vendor) {
  46. active_bus = bus;
  47. active_dev = dev;
  48. active_vendor = vendor;
  49. active_id = id;
  50. }
  51. /* Match the framebuffer BAR against the LFB base. */
  52. bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
  53. if (lfb_base && !(bar0 & 1) && (bar0 & 0xFFFFFFF0)) {
  54. bar0_base = bar0 & 0xFFFFFFF0;
  55. bar0_size = gpu_bar_size(bus, dev, 0x10);
  56. if (bar0_size && lfb_base >= bar0_base &&
  57. lfb_base < bar0_base + bar0_size) {
  58. fb_bus = bus;
  59. fb_dev = dev;
  60. fb_vendor = vendor;
  61. fb_id = id;
  62. }
  63. }
  64. }
  65. }
  66. if (fb_vendor) {
  67. active_bus = fb_bus;
  68. active_dev = fb_dev;
  69. active_vendor = fb_vendor;
  70. active_id = fb_id;
  71. }
  72. if (!active_vendor) {
  73. gpu_vendor_id = GPU_VENDOR_UNKNOWN;
  74. gpu_pci_id_val = 0;
  75. return;
  76. }
  77. gpu_pci_id_val = active_id;
  78. if (active_vendor == PCI_VENDOR_ATI) {
  79. gpu_vendor_id = GPU_VENDOR_ATI;
  80. ati_init(active_bus, active_dev);
  81. } else if (active_vendor == PCI_VENDOR_NVIDIA) {
  82. gpu_vendor_id = GPU_VENDOR_NVIDIA;
  83. nvidia_init();
  84. } else if (active_vendor == PCI_VENDOR_INTEL) {
  85. gpu_vendor_id = GPU_VENDOR_INTEL;
  86. } else if (active_vendor == PCI_VENDOR_CIRRUS) {
  87. gpu_vendor_id = GPU_VENDOR_CIRRUS;
  88. cirrus_init();
  89. } else {
  90. gpu_vendor_id = GPU_VENDOR_OTHER;
  91. }
  92. }
  93. int gpu_vendor(void)
  94. {
  95. return gpu_vendor_id;
  96. }
  97. unsigned int gpu_pci_id(void)
  98. {
  99. return gpu_pci_id_val;
  100. }
  101. const char *gpu_vendor_name(void)
  102. {
  103. switch (gpu_vendor_id) {
  104. case GPU_VENDOR_ATI: return "ATI/AMD";
  105. case GPU_VENDOR_NVIDIA: return "NVIDIA";
  106. case GPU_VENDOR_INTEL: return "Intel";
  107. case GPU_VENDOR_CIRRUS: return "Cirrus Logic";
  108. case GPU_VENDOR_OTHER: return "Other";
  109. default: return "Unknown";
  110. }
  111. }