gpu.c 3.6 KB

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