nvidia.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * NVIDIA GPU support.
  3. *
  4. * Unlike ATI Mach64/Rage which need manual CRTC_OFF_PITCH fixups, NVIDIA
  5. * cards handle VBE mode sets cleanly through their VBIOS: the real-mode stage
  6. * provides a working LFB and no display fixup is needed. The standard VGA
  7. * CRTC registers at 0x3D4/0x3D5 must NOT be touched on NVIDIA -- the display
  8. * engine ignores or misinterprets legacy VGA CRTC writes, which can cause
  9. * display corruption or a reboot.
  10. *
  11. * What this file adds on top of the (VBE-only) no-op is identification: the
  12. * register aperture (BAR0, 16 MB on nv50+) is mapped and validated by reading
  13. * NV_PMC_BOOT_0, from which the chipset id is decoded. That aperture is also
  14. * what the nv50-family display engine (ION/MCP79) is programmed through.
  15. *
  16. * NOTE: nv50-era VBIOSes under-report VBE TotalMemory (the AT3IONT-I reports
  17. * 14 MB for a mode that needs 5 MB); don't trust it for VRAM sizing.
  18. */
  19. #include "nvidia.h"
  20. #include "nv50.h"
  21. #include "pci.h"
  22. #include "process.h"
  23. #include "serial.h"
  24. #define NV_VENDOR_ID 0x10DE
  25. #define NV_BAR0_SIZE 0x1000000 /* register aperture (16 MB on nv50+) */
  26. static unsigned int nv_present; /* BAR0 mapped + PMC_BOOT_0 sane */
  27. static unsigned int nv_boot0;
  28. static unsigned int nv_chipset;
  29. static unsigned int nv_bar0;
  30. static unsigned int nv_bar1;
  31. static unsigned int nv_bar1_size;
  32. static unsigned int nv_bar3;
  33. static unsigned int nv_bar3_size;
  34. static unsigned int nv_vram_base;
  35. static unsigned int nv_vram_size;
  36. static const char *nv_name = "NVIDIA";
  37. struct nv_name {
  38. unsigned short device_id;
  39. const char *name;
  40. };
  41. /* Known display devices. Not exhaustive -- unknown ids fall back to
  42. * "NVIDIA"; the PCI id is printed separately by `display`. */
  43. static const struct nv_name nv_names[] = {
  44. { 0x0865, "C79 [GeForce 9300 / ION]" },
  45. { 0x0869, "MCP7A [GeForce 9400]" },
  46. { 0x086D, "C79 [GeForce 9200]" },
  47. { 0x0870, "C79 [GeForce 9400M]" },
  48. { 0x0874, "C79 [ION]" },
  49. { 0x0876, "C79 [GeForce 9400M / ION]" },
  50. { 0x087D, "C79 [ION]" },
  51. { 0x087E, "C79 [ION LE]" },
  52. { 0x0A64, "GT218 [ION]" },
  53. { 0x0A6F, "GT218M [ION]" },
  54. { 0x0A76, "GT218M [ION 2]" },
  55. { 0x1401, "GM206 [GeForce GTX 960]" },
  56. };
  57. /* Read a memory BAR's size in bytes (probe-and-restore, like lspci; handles
  58. * 64-bit BARs by clearing/restoring the high dword as well). */
  59. static unsigned int nv_bar_size(unsigned int bus, unsigned int dev,
  60. unsigned int reg, unsigned int is64)
  61. {
  62. unsigned int val = pci_cfg_read32(bus, dev, 0, reg);
  63. unsigned int hi = 0, raw;
  64. if (val & 1)
  65. return 0; /* I/O BAR */
  66. if (is64)
  67. hi = pci_cfg_read32(bus, dev, 0, reg + 4);
  68. pci_cfg_write32(bus, dev, 0, reg, 0xFFFFFFFF);
  69. if (is64)
  70. pci_cfg_write32(bus, dev, 0, reg + 4, 0xFFFFFFFF);
  71. raw = pci_cfg_read32(bus, dev, 0, reg) & 0xFFFFFFF0u;
  72. pci_cfg_write32(bus, dev, 0, reg, val);
  73. if (is64)
  74. pci_cfg_write32(bus, dev, 0, reg + 4, hi);
  75. if (raw == 0)
  76. return 0;
  77. return ~raw + 1;
  78. }
  79. void nvidia_init(unsigned int bus, unsigned int dev, unsigned int device_id)
  80. {
  81. unsigned int bar0, boot0, i;
  82. nv_present = 0;
  83. nv_boot0 = 0;
  84. nv_chipset = 0;
  85. nv_bar0 = 0;
  86. nv_bar1 = 0;
  87. nv_bar1_size = 0;
  88. nv_bar3 = 0;
  89. nv_bar3_size = 0;
  90. nv_vram_base = 0;
  91. nv_vram_size = 0;
  92. nv_name = "NVIDIA";
  93. for (i = 0; i < sizeof(nv_names) / sizeof(nv_names[0]); i++) {
  94. if (nv_names[i].device_id == device_id) {
  95. nv_name = nv_names[i].name;
  96. break;
  97. }
  98. }
  99. /* BAR0: register aperture. The nv50+ display engine registers
  100. * (EVO/PDISPLAY) live in it starting at 0x00610000. */
  101. bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
  102. if (!(bar0 & 1) && (bar0 & 0xFFFFFFF0)) {
  103. nv_bar0 = bar0 & 0xFFFFFFF0;
  104. /* Map into every process page directory: gpu_init() runs
  105. * before the first process exists, but drivers may run in a
  106. * terminal context later (same reason r8168 does this). */
  107. process_map_mmio_all(nv_bar0, NV_BAR0_SIZE);
  108. boot0 = *(volatile unsigned int *)nv_bar0;
  109. if (boot0 != 0 && boot0 != 0xFFFFFFFF) {
  110. nv_boot0 = boot0;
  111. nv_chipset = (boot0 >> 20) & 0xFF;
  112. nv_present = 1;
  113. }
  114. }
  115. /* BAR1: VRAM window (64-bit prefetchable on nv50+); BAR3: a second
  116. * (32 MB) VRAM alias the VBIOS uses for the VBE linear framebuffer. */
  117. {
  118. unsigned int b1 = pci_cfg_read32(bus, dev, 0, 0x14);
  119. unsigned int b1hi = pci_cfg_read32(bus, dev, 0, 0x18);
  120. unsigned int b3, b3hi;
  121. if (!(b1 & 1) && (b1 & 0xFFFFFFF0) && b1hi == 0)
  122. nv_bar1 = b1 & 0xFFFFFFF0;
  123. if (nv_bar1) {
  124. nv_bar1_size = nv_bar_size(bus, dev, 0x14, b1 & 4);
  125. if (nv_bar1_size > 0x10000000u) /* cap the mapping */
  126. nv_bar1_size = 0x10000000u;
  127. process_map_mmio_all(nv_bar1, nv_bar1_size);
  128. }
  129. b3 = pci_cfg_read32(bus, dev, 0, 0x1C);
  130. b3hi = pci_cfg_read32(bus, dev, 0, 0x20);
  131. if (!(b3 & 1) && (b3 & 0xFFFFFFF0) && b3hi == 0)
  132. nv_bar3 = b3 & 0xFFFFFFF0;
  133. if (nv_bar3) {
  134. nv_bar3_size = nv_bar_size(bus, dev, 0x1C, b3 & 4);
  135. if (nv_bar3_size > 0x04000000u)
  136. nv_bar3_size = 0x04000000u;
  137. process_map_mmio_all(nv_bar3, nv_bar3_size);
  138. }
  139. }
  140. /* MCP77/78/79/89 (UMA): the memory controller reports the stolen
  141. * region's physical base and size at 0x100E10/0x100E14 (1 MB units).
  142. * The VBE framebuffer sits at its start, so subtracting the base gives
  143. * the display engine's VRAM address for the LFB. */
  144. if (nv_present && (nv_chipset == 0xaa || nv_chipset == 0xac ||
  145. nv_chipset == 0xaf)) {
  146. unsigned int vb = *(volatile unsigned int *)(nv_bar0 + 0x100E10);
  147. unsigned int vs = *(volatile unsigned int *)(nv_bar0 + 0x100E14);
  148. nv_vram_base = vb << 12;
  149. nv_vram_size = vs << 12;
  150. }
  151. serial_puts(SERIAL_COM1, "nvidia: ");
  152. serial_puts(SERIAL_COM1, nv_name);
  153. serial_puts(SERIAL_COM1, " dev ");
  154. serial_puthex(SERIAL_COM1, (NV_VENDOR_ID << 16) | device_id);
  155. if (nv_present) {
  156. serial_puts(SERIAL_COM1, " chipset ");
  157. serial_puthex(SERIAL_COM1, nv_chipset);
  158. serial_puts(SERIAL_COM1, " boot0 ");
  159. serial_puthex(SERIAL_COM1, nv_boot0);
  160. serial_puts(SERIAL_COM1, " regs ");
  161. serial_puthex(SERIAL_COM1, nv_bar0);
  162. serial_puts(SERIAL_COM1, " bar1 ");
  163. serial_puthex(SERIAL_COM1, nv_bar1);
  164. serial_puts(SERIAL_COM1, " bar3 ");
  165. serial_puthex(SERIAL_COM1, nv_bar3);
  166. } else {
  167. serial_puts(SERIAL_COM1, " regs unavailable");
  168. }
  169. serial_puts(SERIAL_COM1, "\n");
  170. if (nv_vram_size) {
  171. serial_puts(SERIAL_COM1, "nvidia: vram base ");
  172. serial_puthex(SERIAL_COM1, nv_vram_base);
  173. serial_puts(SERIAL_COM1, " size ");
  174. serial_puthex(SERIAL_COM1, nv_vram_size);
  175. serial_puts(SERIAL_COM1, "\n");
  176. }
  177. /* Read-only look at the display engine (EVO channels, object table,
  178. * cursor state) for boards with no serial console (netmon mirror). */
  179. nv50_probe();
  180. }
  181. int nvidia_available(void)
  182. {
  183. return (int)nv_present;
  184. }
  185. const char *nvidia_chip_name(void)
  186. {
  187. return nv_name;
  188. }
  189. unsigned int nvidia_chipset(void)
  190. {
  191. return nv_chipset;
  192. }
  193. unsigned int nvidia_bar0(void)
  194. {
  195. return nv_bar0;
  196. }
  197. unsigned int nvidia_bar1(void)
  198. {
  199. return nv_bar1;
  200. }
  201. unsigned int nvidia_bar1_size(void)
  202. {
  203. return nv_bar1_size;
  204. }
  205. unsigned int nvidia_bar3(void)
  206. {
  207. return nv_bar3;
  208. }
  209. unsigned int nvidia_bar3_size(void)
  210. {
  211. return nv_bar3_size;
  212. }
  213. unsigned int nvidia_vram_base(void)
  214. {
  215. return nv_vram_base;
  216. }
  217. unsigned int nvidia_vram_size(void)
  218. {
  219. return nv_vram_size;
  220. }