gf119.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "nv50.h"
  25. #include "rootnv50.h"
  26. #include <subdev/bios.h>
  27. #include <subdev/bios/disp.h>
  28. #include <subdev/bios/init.h>
  29. #include <subdev/bios/pll.h>
  30. #include <subdev/devinit.h>
  31. void
  32. gf119_disp_vblank_init(struct nv50_disp *disp, int head)
  33. {
  34. struct nvkm_device *device = disp->base.engine.subdev.device;
  35. nvkm_mask(device, 0x6100c0 + (head * 0x800), 0x00000001, 0x00000001);
  36. }
  37. void
  38. gf119_disp_vblank_fini(struct nv50_disp *disp, int head)
  39. {
  40. struct nvkm_device *device = disp->base.engine.subdev.device;
  41. nvkm_mask(device, 0x6100c0 + (head * 0x800), 0x00000001, 0x00000000);
  42. }
  43. static struct nvkm_output *
  44. exec_lookup(struct nv50_disp *disp, int head, int or, u32 ctrl,
  45. u32 *data, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
  46. struct nvbios_outp *info)
  47. {
  48. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  49. struct nvkm_bios *bios = subdev->device->bios;
  50. struct nvkm_output *outp;
  51. u16 mask, type;
  52. if (or < 4) {
  53. type = DCB_OUTPUT_ANALOG;
  54. mask = 0;
  55. } else {
  56. or -= 4;
  57. switch (ctrl & 0x00000f00) {
  58. case 0x00000000: type = DCB_OUTPUT_LVDS; mask = 1; break;
  59. case 0x00000100: type = DCB_OUTPUT_TMDS; mask = 1; break;
  60. case 0x00000200: type = DCB_OUTPUT_TMDS; mask = 2; break;
  61. case 0x00000500: type = DCB_OUTPUT_TMDS; mask = 3; break;
  62. case 0x00000800: type = DCB_OUTPUT_DP; mask = 1; break;
  63. case 0x00000900: type = DCB_OUTPUT_DP; mask = 2; break;
  64. default:
  65. nvkm_error(subdev, "unknown SOR mc %08x\n", ctrl);
  66. return NULL;
  67. }
  68. }
  69. mask = 0x00c0 & (mask << 6);
  70. mask |= 0x0001 << or;
  71. mask |= 0x0100 << head;
  72. list_for_each_entry(outp, &disp->base.outp, head) {
  73. if ((outp->info.hasht & 0xff) == type &&
  74. (outp->info.hashm & mask) == mask) {
  75. *data = nvbios_outp_match(bios, outp->info.hasht, mask,
  76. ver, hdr, cnt, len, info);
  77. if (!*data)
  78. return NULL;
  79. return outp;
  80. }
  81. }
  82. return NULL;
  83. }
  84. static struct nvkm_output *
  85. exec_script(struct nv50_disp *disp, int head, int id)
  86. {
  87. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  88. struct nvkm_device *device = subdev->device;
  89. struct nvkm_bios *bios = device->bios;
  90. struct nvkm_output *outp;
  91. struct nvbios_outp info;
  92. u8 ver, hdr, cnt, len;
  93. u32 data, ctrl = 0;
  94. int or;
  95. for (or = 0; !(ctrl & (1 << head)) && or < 8; or++) {
  96. ctrl = nvkm_rd32(device, 0x640180 + (or * 0x20));
  97. if (ctrl & (1 << head))
  98. break;
  99. }
  100. if (or == 8)
  101. return NULL;
  102. outp = exec_lookup(disp, head, or, ctrl, &data, &ver, &hdr, &cnt, &len, &info);
  103. if (outp) {
  104. struct nvbios_init init = {
  105. .subdev = subdev,
  106. .bios = bios,
  107. .offset = info.script[id],
  108. .outp = &outp->info,
  109. .crtc = head,
  110. .execute = 1,
  111. };
  112. nvbios_exec(&init);
  113. }
  114. return outp;
  115. }
  116. static struct nvkm_output *
  117. exec_clkcmp(struct nv50_disp *disp, int head, int id, u32 pclk, u32 *conf)
  118. {
  119. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  120. struct nvkm_device *device = subdev->device;
  121. struct nvkm_bios *bios = device->bios;
  122. struct nvkm_output *outp;
  123. struct nvbios_outp info1;
  124. struct nvbios_ocfg info2;
  125. u8 ver, hdr, cnt, len;
  126. u32 data, ctrl = 0;
  127. int or;
  128. for (or = 0; !(ctrl & (1 << head)) && or < 8; or++) {
  129. ctrl = nvkm_rd32(device, 0x660180 + (or * 0x20));
  130. if (ctrl & (1 << head))
  131. break;
  132. }
  133. if (or == 8)
  134. return NULL;
  135. outp = exec_lookup(disp, head, or, ctrl, &data, &ver, &hdr, &cnt, &len, &info1);
  136. if (!outp)
  137. return NULL;
  138. *conf = (ctrl & 0x00000f00) >> 8;
  139. switch (outp->info.type) {
  140. case DCB_OUTPUT_TMDS:
  141. if (*conf == 5)
  142. *conf |= 0x0100;
  143. break;
  144. case DCB_OUTPUT_LVDS:
  145. *conf |= disp->sor.lvdsconf;
  146. break;
  147. default:
  148. break;
  149. }
  150. data = nvbios_ocfg_match(bios, data, *conf & 0xff, *conf >> 8,
  151. &ver, &hdr, &cnt, &len, &info2);
  152. if (data && id < 0xff) {
  153. data = nvbios_oclk_match(bios, info2.clkcmp[id], pclk);
  154. if (data) {
  155. struct nvbios_init init = {
  156. .subdev = subdev,
  157. .bios = bios,
  158. .offset = data,
  159. .outp = &outp->info,
  160. .crtc = head,
  161. .execute = 1,
  162. };
  163. nvbios_exec(&init);
  164. }
  165. }
  166. return outp;
  167. }
  168. static void
  169. gf119_disp_intr_unk1_0(struct nv50_disp *disp, int head)
  170. {
  171. exec_script(disp, head, 1);
  172. }
  173. static void
  174. gf119_disp_intr_unk2_0(struct nv50_disp *disp, int head)
  175. {
  176. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  177. struct nvkm_output *outp = exec_script(disp, head, 2);
  178. /* see note in nv50_disp_intr_unk20_0() */
  179. if (outp && outp->info.type == DCB_OUTPUT_DP) {
  180. struct nvkm_output_dp *outpdp = nvkm_output_dp(outp);
  181. if (!outpdp->lt.mst) {
  182. struct nvbios_init init = {
  183. .subdev = subdev,
  184. .bios = subdev->device->bios,
  185. .outp = &outp->info,
  186. .crtc = head,
  187. .offset = outpdp->info.script[4],
  188. .execute = 1,
  189. };
  190. nvkm_notify_put(&outpdp->irq);
  191. nvbios_exec(&init);
  192. atomic_set(&outpdp->lt.done, 0);
  193. }
  194. }
  195. }
  196. static void
  197. gf119_disp_intr_unk2_1(struct nv50_disp *disp, int head)
  198. {
  199. struct nvkm_device *device = disp->base.engine.subdev.device;
  200. struct nvkm_devinit *devinit = device->devinit;
  201. u32 pclk = nvkm_rd32(device, 0x660450 + (head * 0x300)) / 1000;
  202. if (pclk)
  203. nvkm_devinit_pll_set(devinit, PLL_VPLL0 + head, pclk);
  204. nvkm_wr32(device, 0x612200 + (head * 0x800), 0x00000000);
  205. }
  206. static void
  207. gf119_disp_intr_unk2_2_tu(struct nv50_disp *disp, int head,
  208. struct dcb_output *outp)
  209. {
  210. struct nvkm_device *device = disp->base.engine.subdev.device;
  211. const int or = ffs(outp->or) - 1;
  212. const u32 ctrl = nvkm_rd32(device, 0x660200 + (or * 0x020));
  213. const u32 conf = nvkm_rd32(device, 0x660404 + (head * 0x300));
  214. const s32 vactive = nvkm_rd32(device, 0x660414 + (head * 0x300)) & 0xffff;
  215. const s32 vblanke = nvkm_rd32(device, 0x66041c + (head * 0x300)) & 0xffff;
  216. const s32 vblanks = nvkm_rd32(device, 0x660420 + (head * 0x300)) & 0xffff;
  217. const u32 pclk = nvkm_rd32(device, 0x660450 + (head * 0x300)) / 1000;
  218. const u32 link = ((ctrl & 0xf00) == 0x800) ? 0 : 1;
  219. const u32 hoff = (head * 0x800);
  220. const u32 soff = ( or * 0x800);
  221. const u32 loff = (link * 0x080) + soff;
  222. const u32 symbol = 100000;
  223. const u32 TU = 64;
  224. u32 dpctrl = nvkm_rd32(device, 0x61c10c + loff);
  225. u32 clksor = nvkm_rd32(device, 0x612300 + soff);
  226. u32 datarate, link_nr, link_bw, bits;
  227. u64 ratio, value;
  228. link_nr = hweight32(dpctrl & 0x000f0000);
  229. link_bw = (clksor & 0x007c0000) >> 18;
  230. link_bw *= 27000;
  231. /* symbols/hblank - algorithm taken from comments in tegra driver */
  232. value = vblanke + vactive - vblanks - 7;
  233. value = value * link_bw;
  234. do_div(value, pclk);
  235. value = value - (3 * !!(dpctrl & 0x00004000)) - (12 / link_nr);
  236. nvkm_mask(device, 0x616620 + hoff, 0x0000ffff, value);
  237. /* symbols/vblank - algorithm taken from comments in tegra driver */
  238. value = vblanks - vblanke - 25;
  239. value = value * link_bw;
  240. do_div(value, pclk);
  241. value = value - ((36 / link_nr) + 3) - 1;
  242. nvkm_mask(device, 0x616624 + hoff, 0x00ffffff, value);
  243. /* watermark */
  244. if ((conf & 0x3c0) == 0x180) bits = 30;
  245. else if ((conf & 0x3c0) == 0x140) bits = 24;
  246. else bits = 18;
  247. datarate = (pclk * bits) / 8;
  248. ratio = datarate;
  249. ratio *= symbol;
  250. do_div(ratio, link_nr * link_bw);
  251. value = (symbol - ratio) * TU;
  252. value *= ratio;
  253. do_div(value, symbol);
  254. do_div(value, symbol);
  255. value += 5;
  256. value |= 0x08000000;
  257. nvkm_wr32(device, 0x616610 + hoff, value);
  258. }
  259. static void
  260. gf119_disp_intr_unk2_2(struct nv50_disp *disp, int head)
  261. {
  262. struct nvkm_device *device = disp->base.engine.subdev.device;
  263. struct nvkm_output *outp;
  264. u32 pclk = nvkm_rd32(device, 0x660450 + (head * 0x300)) / 1000;
  265. u32 conf, addr, data;
  266. outp = exec_clkcmp(disp, head, 0xff, pclk, &conf);
  267. if (!outp)
  268. return;
  269. /* see note in nv50_disp_intr_unk20_2() */
  270. if (outp->info.type == DCB_OUTPUT_DP) {
  271. u32 sync = nvkm_rd32(device, 0x660404 + (head * 0x300));
  272. switch ((sync & 0x000003c0) >> 6) {
  273. case 6: pclk = pclk * 30; break;
  274. case 5: pclk = pclk * 24; break;
  275. case 2:
  276. default:
  277. pclk = pclk * 18;
  278. break;
  279. }
  280. if (nvkm_output_dp_train(outp, pclk))
  281. OUTP_ERR(outp, "link not trained before attach");
  282. } else {
  283. if (disp->func->sor.magic)
  284. disp->func->sor.magic(outp);
  285. }
  286. exec_clkcmp(disp, head, 0, pclk, &conf);
  287. if (outp->info.type == DCB_OUTPUT_ANALOG) {
  288. addr = 0x612280 + (ffs(outp->info.or) - 1) * 0x800;
  289. data = 0x00000000;
  290. } else {
  291. addr = 0x612300 + (ffs(outp->info.or) - 1) * 0x800;
  292. data = (conf & 0x0100) ? 0x00000101 : 0x00000000;
  293. switch (outp->info.type) {
  294. case DCB_OUTPUT_TMDS:
  295. nvkm_mask(device, addr, 0x007c0000, 0x00280000);
  296. break;
  297. case DCB_OUTPUT_DP:
  298. gf119_disp_intr_unk2_2_tu(disp, head, &outp->info);
  299. break;
  300. default:
  301. break;
  302. }
  303. }
  304. nvkm_mask(device, addr, 0x00000707, data);
  305. }
  306. static void
  307. gf119_disp_intr_unk4_0(struct nv50_disp *disp, int head)
  308. {
  309. struct nvkm_device *device = disp->base.engine.subdev.device;
  310. u32 pclk = nvkm_rd32(device, 0x660450 + (head * 0x300)) / 1000;
  311. u32 conf;
  312. exec_clkcmp(disp, head, 1, pclk, &conf);
  313. }
  314. void
  315. gf119_disp_intr_supervisor(struct work_struct *work)
  316. {
  317. struct nv50_disp *disp =
  318. container_of(work, struct nv50_disp, supervisor);
  319. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  320. struct nvkm_device *device = subdev->device;
  321. u32 mask[4];
  322. int head;
  323. nvkm_debug(subdev, "supervisor %d\n", ffs(disp->super));
  324. for (head = 0; head < disp->base.head.nr; head++) {
  325. mask[head] = nvkm_rd32(device, 0x6101d4 + (head * 0x800));
  326. nvkm_debug(subdev, "head %d: %08x\n", head, mask[head]);
  327. }
  328. if (disp->super & 0x00000001) {
  329. nv50_disp_chan_mthd(disp->chan[0], NV_DBG_DEBUG);
  330. for (head = 0; head < disp->base.head.nr; head++) {
  331. if (!(mask[head] & 0x00001000))
  332. continue;
  333. nvkm_debug(subdev, "supervisor 1.0 - head %d\n", head);
  334. gf119_disp_intr_unk1_0(disp, head);
  335. }
  336. } else
  337. if (disp->super & 0x00000002) {
  338. for (head = 0; head < disp->base.head.nr; head++) {
  339. if (!(mask[head] & 0x00001000))
  340. continue;
  341. nvkm_debug(subdev, "supervisor 2.0 - head %d\n", head);
  342. gf119_disp_intr_unk2_0(disp, head);
  343. }
  344. for (head = 0; head < disp->base.head.nr; head++) {
  345. if (!(mask[head] & 0x00010000))
  346. continue;
  347. nvkm_debug(subdev, "supervisor 2.1 - head %d\n", head);
  348. gf119_disp_intr_unk2_1(disp, head);
  349. }
  350. for (head = 0; head < disp->base.head.nr; head++) {
  351. if (!(mask[head] & 0x00001000))
  352. continue;
  353. nvkm_debug(subdev, "supervisor 2.2 - head %d\n", head);
  354. gf119_disp_intr_unk2_2(disp, head);
  355. }
  356. } else
  357. if (disp->super & 0x00000004) {
  358. for (head = 0; head < disp->base.head.nr; head++) {
  359. if (!(mask[head] & 0x00001000))
  360. continue;
  361. nvkm_debug(subdev, "supervisor 3.0 - head %d\n", head);
  362. gf119_disp_intr_unk4_0(disp, head);
  363. }
  364. }
  365. for (head = 0; head < disp->base.head.nr; head++)
  366. nvkm_wr32(device, 0x6101d4 + (head * 0x800), 0x00000000);
  367. nvkm_wr32(device, 0x6101d0, 0x80000000);
  368. }
  369. void
  370. gf119_disp_intr_error(struct nv50_disp *disp, int chid)
  371. {
  372. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  373. struct nvkm_device *device = subdev->device;
  374. u32 mthd = nvkm_rd32(device, 0x6101f0 + (chid * 12));
  375. u32 data = nvkm_rd32(device, 0x6101f4 + (chid * 12));
  376. u32 unkn = nvkm_rd32(device, 0x6101f8 + (chid * 12));
  377. nvkm_error(subdev, "chid %d mthd %04x data %08x %08x %08x\n",
  378. chid, (mthd & 0x0000ffc), data, mthd, unkn);
  379. if (chid < ARRAY_SIZE(disp->chan)) {
  380. switch (mthd & 0xffc) {
  381. case 0x0080:
  382. nv50_disp_chan_mthd(disp->chan[chid], NV_DBG_ERROR);
  383. break;
  384. default:
  385. break;
  386. }
  387. }
  388. nvkm_wr32(device, 0x61009c, (1 << chid));
  389. nvkm_wr32(device, 0x6101f0 + (chid * 12), 0x90000000);
  390. }
  391. void
  392. gf119_disp_intr(struct nv50_disp *disp)
  393. {
  394. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  395. struct nvkm_device *device = subdev->device;
  396. u32 intr = nvkm_rd32(device, 0x610088);
  397. int i;
  398. if (intr & 0x00000001) {
  399. u32 stat = nvkm_rd32(device, 0x61008c);
  400. while (stat) {
  401. int chid = __ffs(stat); stat &= ~(1 << chid);
  402. nv50_disp_chan_uevent_send(disp, chid);
  403. nvkm_wr32(device, 0x61008c, 1 << chid);
  404. }
  405. intr &= ~0x00000001;
  406. }
  407. if (intr & 0x00000002) {
  408. u32 stat = nvkm_rd32(device, 0x61009c);
  409. int chid = ffs(stat) - 1;
  410. if (chid >= 0)
  411. disp->func->intr_error(disp, chid);
  412. intr &= ~0x00000002;
  413. }
  414. if (intr & 0x00100000) {
  415. u32 stat = nvkm_rd32(device, 0x6100ac);
  416. if (stat & 0x00000007) {
  417. disp->super = (stat & 0x00000007);
  418. schedule_work(&disp->supervisor);
  419. nvkm_wr32(device, 0x6100ac, disp->super);
  420. stat &= ~0x00000007;
  421. }
  422. if (stat) {
  423. nvkm_warn(subdev, "intr24 %08x\n", stat);
  424. nvkm_wr32(device, 0x6100ac, stat);
  425. }
  426. intr &= ~0x00100000;
  427. }
  428. for (i = 0; i < disp->base.head.nr; i++) {
  429. u32 mask = 0x01000000 << i;
  430. if (mask & intr) {
  431. u32 stat = nvkm_rd32(device, 0x6100bc + (i * 0x800));
  432. if (stat & 0x00000001)
  433. nvkm_disp_vblank(&disp->base, i);
  434. nvkm_mask(device, 0x6100bc + (i * 0x800), 0, 0);
  435. nvkm_rd32(device, 0x6100c0 + (i * 0x800));
  436. }
  437. }
  438. }
  439. int
  440. gf119_disp_new_(const struct nv50_disp_func *func, struct nvkm_device *device,
  441. int index, struct nvkm_disp **pdisp)
  442. {
  443. u32 heads = nvkm_rd32(device, 0x022448);
  444. return nv50_disp_new_(func, device, index, heads, pdisp);
  445. }
  446. static const struct nv50_disp_func
  447. gf119_disp = {
  448. .intr = gf119_disp_intr,
  449. .intr_error = gf119_disp_intr_error,
  450. .uevent = &gf119_disp_chan_uevent,
  451. .super = gf119_disp_intr_supervisor,
  452. .root = &gf119_disp_root_oclass,
  453. .head.vblank_init = gf119_disp_vblank_init,
  454. .head.vblank_fini = gf119_disp_vblank_fini,
  455. .head.scanoutpos = gf119_disp_root_scanoutpos,
  456. .outp.internal.crt = nv50_dac_output_new,
  457. .outp.internal.tmds = nv50_sor_output_new,
  458. .outp.internal.lvds = nv50_sor_output_new,
  459. .outp.internal.dp = gf119_sor_dp_new,
  460. .dac.nr = 3,
  461. .dac.power = nv50_dac_power,
  462. .dac.sense = nv50_dac_sense,
  463. .sor.nr = 4,
  464. .sor.power = nv50_sor_power,
  465. .sor.hda_eld = gf119_hda_eld,
  466. .sor.hdmi = gf119_hdmi_ctrl,
  467. };
  468. int
  469. gf119_disp_new(struct nvkm_device *device, int index, struct nvkm_disp **pdisp)
  470. {
  471. return gf119_disp_new_(&gf119_disp, device, index, pdisp);
  472. }