nvidia.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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_regs; /* kernel mapping of BAR0 */
  33. static unsigned int nv_bar1;
  34. static unsigned int nv_bar1_size;
  35. static unsigned int nv_bar3;
  36. static unsigned int nv_bar3_size;
  37. static unsigned int nv_vram_base;
  38. static unsigned int nv_vram_size;
  39. static const char *nv_name = "NVIDIA";
  40. struct nv_name {
  41. unsigned short device_id;
  42. const char *name;
  43. };
  44. /* Known display devices. Not exhaustive -- unknown ids fall back to
  45. * "NVIDIA"; the PCI id is printed separately by `display`. */
  46. static const struct nv_name nv_names[] = {
  47. { 0x0865, "C79 [GeForce 9300 / ION]" },
  48. { 0x0869, "MCP7A [GeForce 9400]" },
  49. { 0x086D, "C79 [GeForce 9200]" },
  50. { 0x0870, "C79 [GeForce 9400M]" },
  51. { 0x0874, "C79 [ION]" },
  52. { 0x0876, "C79 [GeForce 9400M / ION]" },
  53. { 0x087D, "C79 [ION]" },
  54. { 0x087E, "C79 [ION LE]" },
  55. { 0x0A64, "GT218 [ION]" },
  56. { 0x0A6F, "GT218M [ION]" },
  57. { 0x0A76, "GT218M [ION 2]" },
  58. { 0x1401, "GM206 [GeForce GTX 960]" },
  59. };
  60. /* Read a memory BAR's size in bytes (probe-and-restore, like lspci; handles
  61. * 64-bit BARs by clearing/restoring the high dword as well). */
  62. static unsigned int nv_bar_size(unsigned int bus, unsigned int dev,
  63. unsigned int reg, unsigned int is64)
  64. {
  65. unsigned int val = pci_cfg_read32(bus, dev, 0, reg);
  66. unsigned int hi = 0, raw;
  67. if (val & 1)
  68. return 0; /* I/O BAR */
  69. if (is64)
  70. hi = pci_cfg_read32(bus, dev, 0, reg + 4);
  71. pci_cfg_write32(bus, dev, 0, reg, 0xFFFFFFFF);
  72. if (is64)
  73. pci_cfg_write32(bus, dev, 0, reg + 4, 0xFFFFFFFF);
  74. raw = pci_cfg_read32(bus, dev, 0, reg) & 0xFFFFFFF0u;
  75. pci_cfg_write32(bus, dev, 0, reg, val);
  76. if (is64)
  77. pci_cfg_write32(bus, dev, 0, reg + 4, hi);
  78. if (raw == 0)
  79. return 0;
  80. return ~raw + 1;
  81. }
  82. void nvidia_init(unsigned int bus, unsigned int dev, unsigned int device_id)
  83. {
  84. unsigned int bar0, boot0, i;
  85. nv_present = 0;
  86. nv_boot0 = 0;
  87. nv_chipset = 0;
  88. nv_bar0 = 0;
  89. nv_bar1 = 0;
  90. nv_bar1_size = 0;
  91. nv_bar3 = 0;
  92. nv_bar3_size = 0;
  93. nv_vram_base = 0;
  94. nv_vram_size = 0;
  95. nv_name = "NVIDIA";
  96. for (i = 0; i < sizeof(nv_names) / sizeof(nv_names[0]); i++) {
  97. if (nv_names[i].device_id == device_id) {
  98. nv_name = nv_names[i].name;
  99. break;
  100. }
  101. }
  102. /* BAR0: register aperture. The nv50+ display engine registers
  103. * (EVO/PDISPLAY) live in it starting at 0x00610000. */
  104. bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
  105. if (!(bar0 & 1) && (bar0 & 0xFFFFFFF0)) {
  106. nv_bar0 = bar0 & 0xFFFFFFF0;
  107. nv_regs = (unsigned int)ioremap(nv_bar0, NV_BAR0_SIZE,
  108. IOREMAP_MMIO);
  109. boot0 = nv_regs ? *(volatile unsigned int *)nv_regs : 0;
  110. if (boot0 != 0 && boot0 != 0xFFFFFFFF) {
  111. nv_boot0 = boot0;
  112. nv_chipset = (boot0 >> 20) & 0xFF;
  113. nv_present = 1;
  114. }
  115. }
  116. /* BAR1: VRAM window (64-bit prefetchable on nv50+); BAR3: a second
  117. * (32 MB) VRAM alias the VBIOS uses for the VBE linear framebuffer. */
  118. {
  119. unsigned int b1 = pci_cfg_read32(bus, dev, 0, 0x14);
  120. unsigned int b1hi = pci_cfg_read32(bus, dev, 0, 0x18);
  121. unsigned int b3, b3hi;
  122. if (!(b1 & 1) && (b1 & 0xFFFFFFF0) && b1hi == 0)
  123. nv_bar1 = b1 & 0xFFFFFFF0;
  124. if (nv_bar1) {
  125. nv_bar1_size = nv_bar_size(bus, dev, 0x14, b1 & 4);
  126. if (nv_bar1_size > 0x10000000u) /* cap the size */
  127. nv_bar1_size = 0x10000000u;
  128. /* Not mapped: nothing dereferences BAR1. */
  129. }
  130. b3 = pci_cfg_read32(bus, dev, 0, 0x1C);
  131. b3hi = pci_cfg_read32(bus, dev, 0, 0x20);
  132. if (!(b3 & 1) && (b3 & 0xFFFFFFF0) && b3hi == 0)
  133. nv_bar3 = b3 & 0xFFFFFFF0;
  134. if (nv_bar3) {
  135. nv_bar3_size = nv_bar_size(bus, dev, 0x1C, b3 & 4);
  136. if (nv_bar3_size > 0x04000000u)
  137. nv_bar3_size = 0x04000000u;
  138. /* Not mapped: the VBE LFB inside it has its own
  139. * mapping (paging_lfb_va()). */
  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_regs + 0x100E10);
  149. unsigned int vs = *(volatile unsigned int *)(nv_regs + 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_regs(void)
  203. {
  204. return nv_regs;
  205. }
  206. unsigned int nvidia_bar1(void)
  207. {
  208. return nv_bar1;
  209. }
  210. unsigned int nvidia_bar1_size(void)
  211. {
  212. return nv_bar1_size;
  213. }
  214. unsigned int nvidia_bar3(void)
  215. {
  216. return nv_bar3;
  217. }
  218. unsigned int nvidia_bar3_size(void)
  219. {
  220. return nv_bar3_size;
  221. }
  222. unsigned int nvidia_vram_base(void)
  223. {
  224. return nv_vram_base;
  225. }
  226. unsigned int nvidia_vram_size(void)
  227. {
  228. return nv_vram_size;
  229. }
  230. /* The VBE LFB is a BAR3 window into the stolen VRAM, which is ordinary DRAM:
  231. * writing it through the PCIe aperture costs ~33 MB/s (separate uncached
  232. * transactions, see the MTRR notes), while the DRAM alias runs at DRAM
  233. * speed. Find where the framebuffer lives in the stolen VRAM by writing a
  234. * marker through the LFB and scanning the VRAM at page granularity (the LFB
  235. * and the frame are page-aligned, so the marker can only start on a page). */
  236. unsigned int nvidia_lfb_in_vram(void)
  237. {
  238. static const unsigned int mark[4] = { 0xA5C3F00Du, 0x5A3C0FF0u,
  239. 0xDEADBEEFu, 0x12345678u };
  240. unsigned int vram = nv_vram_base, vsize = nv_vram_size;
  241. unsigned int lfb_phys = vga_fb_lfb_phys();
  242. unsigned int lfb = (unsigned int)vga_fb_lfb_addr(); /* mapped */
  243. unsigned int off, i, hit = 0;
  244. if (!vram || vsize < 0x01000000u || !nv_bar3 || !nv_bar3_size)
  245. return 0;
  246. if (lfb_phys < nv_bar3 || lfb_phys > nv_bar3 + nv_bar3_size - 0x1000u)
  247. return 0; /* the VBE LFB is not in the VRAM aperture */
  248. if (nv_bar3_size < 0x00801000u)
  249. return 0; /* no room for a marker past the window */
  250. /* The stolen VRAM is DRAM: map it write-back (the WC MTRR the caller
  251. * adds over it makes the effective type WC). */
  252. vram = (unsigned int)ioremap(vram, vsize, IOREMAP_RAM);
  253. if (!vram)
  254. return 0;
  255. /* Marker at LFB + 8 MB: past any 1920x1080x32 frame. */
  256. {
  257. volatile unsigned int *p =
  258. (volatile unsigned int *)(lfb + 0x00800000u);
  259. for (i = 0; i < 4; i++)
  260. p[i] = mark[i];
  261. }
  262. for (off = 0; off + 0x00800010u <= vsize; off += 0x1000u) {
  263. const unsigned int *q = (const unsigned int *)(vram + off);
  264. if (q[0] == mark[0] && q[1] == mark[1] &&
  265. q[2] == mark[2] && q[3] == mark[3]) {
  266. hit = off;
  267. break;
  268. }
  269. }
  270. /* Clear the marker again (off-screen VRAM). */
  271. {
  272. volatile unsigned int *p =
  273. (volatile unsigned int *)(lfb + 0x00800000u);
  274. for (i = 0; i < 4; i++)
  275. p[i] = 0;
  276. }
  277. if (!hit)
  278. return 0;
  279. /* The marker was at frame + 8 MB. */
  280. return vram + (hit - 0x00800000u);
  281. }