nvidia.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include "paging.h"
  25. #include "vga.h"
  26. #define NV_VENDOR_ID 0x10DE
  27. #define NV_BAR0_SIZE 0x1000000 /* register aperture (16 MB on nv50+) */
  28. static unsigned int nv_present; /* BAR0 mapped + PMC_BOOT_0 sane */
  29. static unsigned int nv_boot0;
  30. static unsigned int nv_chipset;
  31. static unsigned int nv_bar0;
  32. static unsigned int nv_bar1;
  33. static unsigned int nv_bar1_size;
  34. static unsigned int nv_bar3;
  35. static unsigned int nv_bar3_size;
  36. static unsigned int nv_vram_base;
  37. static unsigned int nv_vram_size;
  38. static const char *nv_name = "NVIDIA";
  39. struct nv_name {
  40. unsigned short device_id;
  41. const char *name;
  42. };
  43. /* Known display devices. Not exhaustive -- unknown ids fall back to
  44. * "NVIDIA"; the PCI id is printed separately by `display`. */
  45. static const struct nv_name nv_names[] = {
  46. { 0x0865, "C79 [GeForce 9300 / ION]" },
  47. { 0x0869, "MCP7A [GeForce 9400]" },
  48. { 0x086D, "C79 [GeForce 9200]" },
  49. { 0x0870, "C79 [GeForce 9400M]" },
  50. { 0x0874, "C79 [ION]" },
  51. { 0x0876, "C79 [GeForce 9400M / ION]" },
  52. { 0x087D, "C79 [ION]" },
  53. { 0x087E, "C79 [ION LE]" },
  54. { 0x0A64, "GT218 [ION]" },
  55. { 0x0A6F, "GT218M [ION]" },
  56. { 0x0A76, "GT218M [ION 2]" },
  57. { 0x1401, "GM206 [GeForce GTX 960]" },
  58. };
  59. /* Read a memory BAR's size in bytes (probe-and-restore, like lspci; handles
  60. * 64-bit BARs by clearing/restoring the high dword as well). */
  61. static unsigned int nv_bar_size(unsigned int bus, unsigned int dev,
  62. unsigned int reg, unsigned int is64)
  63. {
  64. unsigned int val = pci_cfg_read32(bus, dev, 0, reg);
  65. unsigned int hi = 0, raw;
  66. if (val & 1)
  67. return 0; /* I/O BAR */
  68. if (is64)
  69. hi = pci_cfg_read32(bus, dev, 0, reg + 4);
  70. pci_cfg_write32(bus, dev, 0, reg, 0xFFFFFFFF);
  71. if (is64)
  72. pci_cfg_write32(bus, dev, 0, reg + 4, 0xFFFFFFFF);
  73. raw = pci_cfg_read32(bus, dev, 0, reg) & 0xFFFFFFF0u;
  74. pci_cfg_write32(bus, dev, 0, reg, val);
  75. if (is64)
  76. pci_cfg_write32(bus, dev, 0, reg + 4, hi);
  77. if (raw == 0)
  78. return 0;
  79. return ~raw + 1;
  80. }
  81. void nvidia_init(unsigned int bus, unsigned int dev, unsigned int device_id)
  82. {
  83. unsigned int bar0, boot0, i;
  84. nv_present = 0;
  85. nv_boot0 = 0;
  86. nv_chipset = 0;
  87. nv_bar0 = 0;
  88. nv_bar1 = 0;
  89. nv_bar1_size = 0;
  90. nv_bar3 = 0;
  91. nv_bar3_size = 0;
  92. nv_vram_base = 0;
  93. nv_vram_size = 0;
  94. nv_name = "NVIDIA";
  95. for (i = 0; i < sizeof(nv_names) / sizeof(nv_names[0]); i++) {
  96. if (nv_names[i].device_id == device_id) {
  97. nv_name = nv_names[i].name;
  98. break;
  99. }
  100. }
  101. /* BAR0: register aperture. The nv50+ display engine registers
  102. * (EVO/PDISPLAY) live in it starting at 0x00610000. */
  103. bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
  104. if (!(bar0 & 1) && (bar0 & 0xFFFFFFF0)) {
  105. nv_bar0 = bar0 & 0xFFFFFFF0;
  106. /* Map into every process page directory: gpu_init() runs
  107. * before the first process exists, but drivers may run in a
  108. * terminal context later (same reason r8168 does this). */
  109. process_map_mmio_all(nv_bar0, NV_BAR0_SIZE);
  110. boot0 = *(volatile unsigned int *)nv_bar0;
  111. if (boot0 != 0 && boot0 != 0xFFFFFFFF) {
  112. nv_boot0 = boot0;
  113. nv_chipset = (boot0 >> 20) & 0xFF;
  114. nv_present = 1;
  115. }
  116. }
  117. /* BAR1: VRAM window (64-bit prefetchable on nv50+); BAR3: a second
  118. * (32 MB) VRAM alias the VBIOS uses for the VBE linear framebuffer. */
  119. {
  120. unsigned int b1 = pci_cfg_read32(bus, dev, 0, 0x14);
  121. unsigned int b1hi = pci_cfg_read32(bus, dev, 0, 0x18);
  122. unsigned int b3, b3hi;
  123. if (!(b1 & 1) && (b1 & 0xFFFFFFF0) && b1hi == 0)
  124. nv_bar1 = b1 & 0xFFFFFFF0;
  125. if (nv_bar1) {
  126. nv_bar1_size = nv_bar_size(bus, dev, 0x14, b1 & 4);
  127. if (nv_bar1_size > 0x10000000u) /* cap the mapping */
  128. nv_bar1_size = 0x10000000u;
  129. process_map_mmio_all(nv_bar1, nv_bar1_size);
  130. }
  131. b3 = pci_cfg_read32(bus, dev, 0, 0x1C);
  132. b3hi = pci_cfg_read32(bus, dev, 0, 0x20);
  133. if (!(b3 & 1) && (b3 & 0xFFFFFFF0) && b3hi == 0)
  134. nv_bar3 = b3 & 0xFFFFFFF0;
  135. if (nv_bar3) {
  136. nv_bar3_size = nv_bar_size(bus, dev, 0x1C, b3 & 4);
  137. if (nv_bar3_size > 0x04000000u)
  138. nv_bar3_size = 0x04000000u;
  139. process_map_mmio_all(nv_bar3, nv_bar3_size);
  140. }
  141. }
  142. /* MCP77/78/79/89 (UMA): the memory controller reports the stolen
  143. * region's physical base and size at 0x100E10/0x100E14 (1 MB units).
  144. * The VBE framebuffer sits at its start, so subtracting the base gives
  145. * the display engine's VRAM address for the LFB. */
  146. if (nv_present && (nv_chipset == 0xaa || nv_chipset == 0xac ||
  147. nv_chipset == 0xaf)) {
  148. unsigned int vb = *(volatile unsigned int *)(nv_bar0 + 0x100E10);
  149. unsigned int vs = *(volatile unsigned int *)(nv_bar0 + 0x100E14);
  150. nv_vram_base = vb << 12;
  151. nv_vram_size = vs << 12;
  152. }
  153. serial_puts(SERIAL_COM1, "nvidia: ");
  154. serial_puts(SERIAL_COM1, nv_name);
  155. serial_puts(SERIAL_COM1, " dev ");
  156. serial_puthex(SERIAL_COM1, (NV_VENDOR_ID << 16) | device_id);
  157. if (nv_present) {
  158. serial_puts(SERIAL_COM1, " chipset ");
  159. serial_puthex(SERIAL_COM1, nv_chipset);
  160. serial_puts(SERIAL_COM1, " boot0 ");
  161. serial_puthex(SERIAL_COM1, nv_boot0);
  162. serial_puts(SERIAL_COM1, " regs ");
  163. serial_puthex(SERIAL_COM1, nv_bar0);
  164. serial_puts(SERIAL_COM1, " bar1 ");
  165. serial_puthex(SERIAL_COM1, nv_bar1);
  166. serial_puts(SERIAL_COM1, " bar3 ");
  167. serial_puthex(SERIAL_COM1, nv_bar3);
  168. } else {
  169. serial_puts(SERIAL_COM1, " regs unavailable");
  170. }
  171. serial_puts(SERIAL_COM1, "\n");
  172. if (nv_vram_size) {
  173. serial_puts(SERIAL_COM1, "nvidia: vram base ");
  174. serial_puthex(SERIAL_COM1, nv_vram_base);
  175. serial_puts(SERIAL_COM1, " size ");
  176. serial_puthex(SERIAL_COM1, nv_vram_size);
  177. serial_puts(SERIAL_COM1, "\n");
  178. }
  179. /* Read-only look at the nv50-family display engine (EVO channels,
  180. * object table, cursor state). The register layout is nv50-specific;
  181. * Maxwell and later parts must not be poked through it. */
  182. if (nv_present && (nv_chipset == 0xaa || nv_chipset == 0xac ||
  183. nv_chipset == 0xaf))
  184. nv50_probe();
  185. }
  186. int nvidia_available(void)
  187. {
  188. return (int)nv_present;
  189. }
  190. const char *nvidia_chip_name(void)
  191. {
  192. return nv_name;
  193. }
  194. unsigned int nvidia_chipset(void)
  195. {
  196. return nv_chipset;
  197. }
  198. unsigned int nvidia_bar0(void)
  199. {
  200. return nv_bar0;
  201. }
  202. unsigned int nvidia_bar1(void)
  203. {
  204. return nv_bar1;
  205. }
  206. unsigned int nvidia_bar1_size(void)
  207. {
  208. return nv_bar1_size;
  209. }
  210. unsigned int nvidia_bar3(void)
  211. {
  212. return nv_bar3;
  213. }
  214. unsigned int nvidia_bar3_size(void)
  215. {
  216. return nv_bar3_size;
  217. }
  218. unsigned int nvidia_vram_base(void)
  219. {
  220. return nv_vram_base;
  221. }
  222. unsigned int nvidia_vram_size(void)
  223. {
  224. return nv_vram_size;
  225. }
  226. /* The VBE LFB is a BAR3 window into the stolen VRAM, which is ordinary DRAM:
  227. * writing it through the PCIe aperture costs ~33 MB/s (separate uncached
  228. * transactions, see the MTRR notes), while the DRAM alias runs at DRAM
  229. * speed. Find where the framebuffer lives in the stolen VRAM by writing a
  230. * marker through the LFB and scanning the VRAM at page granularity (the LFB
  231. * and the frame are page-aligned, so the marker can only start on a page). */
  232. unsigned int nvidia_lfb_in_vram(void)
  233. {
  234. static const unsigned int mark[4] = { 0xA5C3F00Du, 0x5A3C0FF0u,
  235. 0xDEADBEEFu, 0x12345678u };
  236. unsigned int vram = nv_vram_base, vsize = nv_vram_size;
  237. unsigned int lfb = (unsigned int)vga_fb_lfb_addr();
  238. unsigned int off, i, hit = 0;
  239. if (!vram || vsize < 0x01000000u || !nv_bar3 || !nv_bar3_size)
  240. return 0;
  241. if (lfb < nv_bar3 || lfb > nv_bar3 + nv_bar3_size - 0x1000u)
  242. return 0; /* the VBE LFB is not in the VRAM aperture */
  243. if (nv_bar3_size < 0x00801000u)
  244. return 0; /* no room for a marker past the window */
  245. paging_map_ram(vram, vsize);
  246. /* Marker at LFB + 8 MB: past any 1920x1080x32 frame. */
  247. {
  248. volatile unsigned int *p =
  249. (volatile unsigned int *)(lfb + 0x00800000u);
  250. for (i = 0; i < 4; i++)
  251. p[i] = mark[i];
  252. }
  253. for (off = 0; off + 0x00800010u <= vsize; off += 0x1000u) {
  254. const unsigned int *q = (const unsigned int *)(vram + off);
  255. if (q[0] == mark[0] && q[1] == mark[1] &&
  256. q[2] == mark[2] && q[3] == mark[3]) {
  257. hit = off;
  258. break;
  259. }
  260. }
  261. /* Clear the marker again (off-screen VRAM). */
  262. {
  263. volatile unsigned int *p =
  264. (volatile unsigned int *)(lfb + 0x00800000u);
  265. for (i = 0; i < 4; i++)
  266. p[i] = 0;
  267. }
  268. if (!hit)
  269. return 0;
  270. /* The marker was at frame + 8 MB. */
  271. return vram + (hit - 0x00800000u);
  272. }