nv50.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 <core/client.h>
  27. #include <core/enum.h>
  28. #include <core/gpuobj.h>
  29. #include <subdev/bios.h>
  30. #include <subdev/bios/disp.h>
  31. #include <subdev/bios/init.h>
  32. #include <subdev/bios/pll.h>
  33. #include <subdev/devinit.h>
  34. static const struct nvkm_disp_oclass *
  35. nv50_disp_root_(struct nvkm_disp *base)
  36. {
  37. return nv50_disp(base)->func->root;
  38. }
  39. static int
  40. nv50_disp_outp_internal_crt_(struct nvkm_disp *base, int index,
  41. struct dcb_output *dcb, struct nvkm_output **poutp)
  42. {
  43. struct nv50_disp *disp = nv50_disp(base);
  44. return disp->func->outp.internal.crt(base, index, dcb, poutp);
  45. }
  46. static int
  47. nv50_disp_outp_internal_tmds_(struct nvkm_disp *base, int index,
  48. struct dcb_output *dcb,
  49. struct nvkm_output **poutp)
  50. {
  51. struct nv50_disp *disp = nv50_disp(base);
  52. return disp->func->outp.internal.tmds(base, index, dcb, poutp);
  53. }
  54. static int
  55. nv50_disp_outp_internal_lvds_(struct nvkm_disp *base, int index,
  56. struct dcb_output *dcb,
  57. struct nvkm_output **poutp)
  58. {
  59. struct nv50_disp *disp = nv50_disp(base);
  60. return disp->func->outp.internal.lvds(base, index, dcb, poutp);
  61. }
  62. static int
  63. nv50_disp_outp_internal_dp_(struct nvkm_disp *base, int index,
  64. struct dcb_output *dcb, struct nvkm_output **poutp)
  65. {
  66. struct nv50_disp *disp = nv50_disp(base);
  67. if (disp->func->outp.internal.dp)
  68. return disp->func->outp.internal.dp(base, index, dcb, poutp);
  69. return -ENODEV;
  70. }
  71. static int
  72. nv50_disp_outp_external_tmds_(struct nvkm_disp *base, int index,
  73. struct dcb_output *dcb,
  74. struct nvkm_output **poutp)
  75. {
  76. struct nv50_disp *disp = nv50_disp(base);
  77. if (disp->func->outp.external.tmds)
  78. return disp->func->outp.external.tmds(base, index, dcb, poutp);
  79. return -ENODEV;
  80. }
  81. static int
  82. nv50_disp_outp_external_dp_(struct nvkm_disp *base, int index,
  83. struct dcb_output *dcb, struct nvkm_output **poutp)
  84. {
  85. struct nv50_disp *disp = nv50_disp(base);
  86. if (disp->func->outp.external.dp)
  87. return disp->func->outp.external.dp(base, index, dcb, poutp);
  88. return -ENODEV;
  89. }
  90. static void
  91. nv50_disp_vblank_fini_(struct nvkm_disp *base, int head)
  92. {
  93. struct nv50_disp *disp = nv50_disp(base);
  94. disp->func->head.vblank_fini(disp, head);
  95. }
  96. static void
  97. nv50_disp_vblank_init_(struct nvkm_disp *base, int head)
  98. {
  99. struct nv50_disp *disp = nv50_disp(base);
  100. disp->func->head.vblank_init(disp, head);
  101. }
  102. static void
  103. nv50_disp_intr_(struct nvkm_disp *base)
  104. {
  105. struct nv50_disp *disp = nv50_disp(base);
  106. disp->func->intr(disp);
  107. }
  108. static void *
  109. nv50_disp_dtor_(struct nvkm_disp *base)
  110. {
  111. struct nv50_disp *disp = nv50_disp(base);
  112. nvkm_event_fini(&disp->uevent);
  113. return disp;
  114. }
  115. static const struct nvkm_disp_func
  116. nv50_disp_ = {
  117. .dtor = nv50_disp_dtor_,
  118. .intr = nv50_disp_intr_,
  119. .root = nv50_disp_root_,
  120. .outp.internal.crt = nv50_disp_outp_internal_crt_,
  121. .outp.internal.tmds = nv50_disp_outp_internal_tmds_,
  122. .outp.internal.lvds = nv50_disp_outp_internal_lvds_,
  123. .outp.internal.dp = nv50_disp_outp_internal_dp_,
  124. .outp.external.tmds = nv50_disp_outp_external_tmds_,
  125. .outp.external.dp = nv50_disp_outp_external_dp_,
  126. .head.vblank_init = nv50_disp_vblank_init_,
  127. .head.vblank_fini = nv50_disp_vblank_fini_,
  128. };
  129. int
  130. nv50_disp_new_(const struct nv50_disp_func *func, struct nvkm_device *device,
  131. int index, int heads, struct nvkm_disp **pdisp)
  132. {
  133. struct nv50_disp *disp;
  134. int ret;
  135. if (!(disp = kzalloc(sizeof(*disp), GFP_KERNEL)))
  136. return -ENOMEM;
  137. INIT_WORK(&disp->supervisor, func->super);
  138. disp->func = func;
  139. *pdisp = &disp->base;
  140. ret = nvkm_disp_ctor(&nv50_disp_, device, index, heads, &disp->base);
  141. if (ret)
  142. return ret;
  143. return nvkm_event_init(func->uevent, 1, 1 + (heads * 4), &disp->uevent);
  144. }
  145. void
  146. nv50_disp_vblank_fini(struct nv50_disp *disp, int head)
  147. {
  148. struct nvkm_device *device = disp->base.engine.subdev.device;
  149. nvkm_mask(device, 0x61002c, (4 << head), 0);
  150. }
  151. void
  152. nv50_disp_vblank_init(struct nv50_disp *disp, int head)
  153. {
  154. struct nvkm_device *device = disp->base.engine.subdev.device;
  155. nvkm_mask(device, 0x61002c, (4 << head), (4 << head));
  156. }
  157. static const struct nvkm_enum
  158. nv50_disp_intr_error_type[] = {
  159. { 3, "ILLEGAL_MTHD" },
  160. { 4, "INVALID_VALUE" },
  161. { 5, "INVALID_STATE" },
  162. { 7, "INVALID_HANDLE" },
  163. {}
  164. };
  165. static const struct nvkm_enum
  166. nv50_disp_intr_error_code[] = {
  167. { 0x00, "" },
  168. {}
  169. };
  170. static void
  171. nv50_disp_intr_error(struct nv50_disp *disp, int chid)
  172. {
  173. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  174. struct nvkm_device *device = subdev->device;
  175. u32 data = nvkm_rd32(device, 0x610084 + (chid * 0x08));
  176. u32 addr = nvkm_rd32(device, 0x610080 + (chid * 0x08));
  177. u32 code = (addr & 0x00ff0000) >> 16;
  178. u32 type = (addr & 0x00007000) >> 12;
  179. u32 mthd = (addr & 0x00000ffc);
  180. const struct nvkm_enum *ec, *et;
  181. et = nvkm_enum_find(nv50_disp_intr_error_type, type);
  182. ec = nvkm_enum_find(nv50_disp_intr_error_code, code);
  183. nvkm_error(subdev,
  184. "ERROR %d [%s] %02x [%s] chid %d mthd %04x data %08x\n",
  185. type, et ? et->name : "", code, ec ? ec->name : "",
  186. chid, mthd, data);
  187. if (chid < ARRAY_SIZE(disp->chan)) {
  188. switch (mthd) {
  189. case 0x0080:
  190. nv50_disp_chan_mthd(disp->chan[chid], NV_DBG_ERROR);
  191. break;
  192. default:
  193. break;
  194. }
  195. }
  196. nvkm_wr32(device, 0x610020, 0x00010000 << chid);
  197. nvkm_wr32(device, 0x610080 + (chid * 0x08), 0x90000000);
  198. }
  199. static struct nvkm_output *
  200. exec_lookup(struct nv50_disp *disp, int head, int or, u32 ctrl,
  201. u32 *data, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
  202. struct nvbios_outp *info)
  203. {
  204. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  205. struct nvkm_bios *bios = subdev->device->bios;
  206. struct nvkm_output *outp;
  207. u16 mask, type;
  208. if (or < 4) {
  209. type = DCB_OUTPUT_ANALOG;
  210. mask = 0;
  211. } else
  212. if (or < 8) {
  213. switch (ctrl & 0x00000f00) {
  214. case 0x00000000: type = DCB_OUTPUT_LVDS; mask = 1; break;
  215. case 0x00000100: type = DCB_OUTPUT_TMDS; mask = 1; break;
  216. case 0x00000200: type = DCB_OUTPUT_TMDS; mask = 2; break;
  217. case 0x00000500: type = DCB_OUTPUT_TMDS; mask = 3; break;
  218. case 0x00000800: type = DCB_OUTPUT_DP; mask = 1; break;
  219. case 0x00000900: type = DCB_OUTPUT_DP; mask = 2; break;
  220. default:
  221. nvkm_error(subdev, "unknown SOR mc %08x\n", ctrl);
  222. return NULL;
  223. }
  224. or -= 4;
  225. } else {
  226. or = or - 8;
  227. type = 0x0010;
  228. mask = 0;
  229. switch (ctrl & 0x00000f00) {
  230. case 0x00000000: type |= disp->pior.type[or]; break;
  231. default:
  232. nvkm_error(subdev, "unknown PIOR mc %08x\n", ctrl);
  233. return NULL;
  234. }
  235. }
  236. mask = 0x00c0 & (mask << 6);
  237. mask |= 0x0001 << or;
  238. mask |= 0x0100 << head;
  239. list_for_each_entry(outp, &disp->base.outp, head) {
  240. if ((outp->info.hasht & 0xff) == type &&
  241. (outp->info.hashm & mask) == mask) {
  242. *data = nvbios_outp_match(bios, outp->info.hasht,
  243. outp->info.hashm,
  244. ver, hdr, cnt, len, info);
  245. if (!*data)
  246. return NULL;
  247. return outp;
  248. }
  249. }
  250. return NULL;
  251. }
  252. static struct nvkm_output *
  253. exec_script(struct nv50_disp *disp, int head, int id)
  254. {
  255. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  256. struct nvkm_device *device = subdev->device;
  257. struct nvkm_bios *bios = device->bios;
  258. struct nvkm_output *outp;
  259. struct nvbios_outp info;
  260. u8 ver, hdr, cnt, len;
  261. u32 data, ctrl = 0;
  262. u32 reg;
  263. int i;
  264. /* DAC */
  265. for (i = 0; !(ctrl & (1 << head)) && i < disp->func->dac.nr; i++)
  266. ctrl = nvkm_rd32(device, 0x610b5c + (i * 8));
  267. /* SOR */
  268. if (!(ctrl & (1 << head))) {
  269. if (device->chipset < 0x90 ||
  270. device->chipset == 0x92 ||
  271. device->chipset == 0xa0) {
  272. reg = 0x610b74;
  273. } else {
  274. reg = 0x610798;
  275. }
  276. for (i = 0; !(ctrl & (1 << head)) && i < disp->func->sor.nr; i++)
  277. ctrl = nvkm_rd32(device, reg + (i * 8));
  278. i += 4;
  279. }
  280. /* PIOR */
  281. if (!(ctrl & (1 << head))) {
  282. for (i = 0; !(ctrl & (1 << head)) && i < disp->func->pior.nr; i++)
  283. ctrl = nvkm_rd32(device, 0x610b84 + (i * 8));
  284. i += 8;
  285. }
  286. if (!(ctrl & (1 << head)))
  287. return NULL;
  288. i--;
  289. outp = exec_lookup(disp, head, i, ctrl, &data, &ver, &hdr, &cnt, &len, &info);
  290. if (outp) {
  291. struct nvbios_init init = {
  292. .subdev = subdev,
  293. .bios = bios,
  294. .offset = info.script[id],
  295. .outp = &outp->info,
  296. .crtc = head,
  297. .execute = 1,
  298. };
  299. nvbios_exec(&init);
  300. }
  301. return outp;
  302. }
  303. static struct nvkm_output *
  304. exec_clkcmp(struct nv50_disp *disp, int head, int id, u32 pclk, u32 *conf)
  305. {
  306. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  307. struct nvkm_device *device = subdev->device;
  308. struct nvkm_bios *bios = device->bios;
  309. struct nvkm_output *outp;
  310. struct nvbios_outp info1;
  311. struct nvbios_ocfg info2;
  312. u8 ver, hdr, cnt, len;
  313. u32 data, ctrl = 0;
  314. u32 reg;
  315. int i;
  316. /* DAC */
  317. for (i = 0; !(ctrl & (1 << head)) && i < disp->func->dac.nr; i++)
  318. ctrl = nvkm_rd32(device, 0x610b58 + (i * 8));
  319. /* SOR */
  320. if (!(ctrl & (1 << head))) {
  321. if (device->chipset < 0x90 ||
  322. device->chipset == 0x92 ||
  323. device->chipset == 0xa0) {
  324. reg = 0x610b70;
  325. } else {
  326. reg = 0x610794;
  327. }
  328. for (i = 0; !(ctrl & (1 << head)) && i < disp->func->sor.nr; i++)
  329. ctrl = nvkm_rd32(device, reg + (i * 8));
  330. i += 4;
  331. }
  332. /* PIOR */
  333. if (!(ctrl & (1 << head))) {
  334. for (i = 0; !(ctrl & (1 << head)) && i < disp->func->pior.nr; i++)
  335. ctrl = nvkm_rd32(device, 0x610b80 + (i * 8));
  336. i += 8;
  337. }
  338. if (!(ctrl & (1 << head)))
  339. return NULL;
  340. i--;
  341. outp = exec_lookup(disp, head, i, ctrl, &data, &ver, &hdr, &cnt, &len, &info1);
  342. if (!outp)
  343. return NULL;
  344. if (outp->info.location == 0) {
  345. switch (outp->info.type) {
  346. case DCB_OUTPUT_TMDS:
  347. *conf = (ctrl & 0x00000f00) >> 8;
  348. if (pclk >= 165000)
  349. *conf |= 0x0100;
  350. break;
  351. case DCB_OUTPUT_LVDS:
  352. *conf = disp->sor.lvdsconf;
  353. break;
  354. case DCB_OUTPUT_DP:
  355. *conf = (ctrl & 0x00000f00) >> 8;
  356. break;
  357. case DCB_OUTPUT_ANALOG:
  358. default:
  359. *conf = 0x00ff;
  360. break;
  361. }
  362. } else {
  363. *conf = (ctrl & 0x00000f00) >> 8;
  364. pclk = pclk / 2;
  365. }
  366. data = nvbios_ocfg_match(bios, data, *conf, &ver, &hdr, &cnt, &len, &info2);
  367. if (data && id < 0xff) {
  368. data = nvbios_oclk_match(bios, info2.clkcmp[id], pclk);
  369. if (data) {
  370. struct nvbios_init init = {
  371. .subdev = subdev,
  372. .bios = bios,
  373. .offset = data,
  374. .outp = &outp->info,
  375. .crtc = head,
  376. .execute = 1,
  377. };
  378. nvbios_exec(&init);
  379. }
  380. }
  381. return outp;
  382. }
  383. static void
  384. nv50_disp_intr_unk10_0(struct nv50_disp *disp, int head)
  385. {
  386. exec_script(disp, head, 1);
  387. }
  388. static void
  389. nv50_disp_intr_unk20_0(struct nv50_disp *disp, int head)
  390. {
  391. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  392. struct nvkm_output *outp = exec_script(disp, head, 2);
  393. /* the binary driver does this outside of the supervisor handling
  394. * (after the third supervisor from a detach). we (currently?)
  395. * allow both detach/attach to happen in the same set of
  396. * supervisor interrupts, so it would make sense to execute this
  397. * (full power down?) script after all the detach phases of the
  398. * supervisor handling. like with training if needed from the
  399. * second supervisor, nvidia doesn't do this, so who knows if it's
  400. * entirely safe, but it does appear to work..
  401. *
  402. * without this script being run, on some configurations i've
  403. * seen, switching from DP to TMDS on a DP connector may result
  404. * in a blank screen (SOR_PWR off/on can restore it)
  405. */
  406. if (outp && outp->info.type == DCB_OUTPUT_DP) {
  407. struct nvkm_output_dp *outpdp = nvkm_output_dp(outp);
  408. struct nvbios_init init = {
  409. .subdev = subdev,
  410. .bios = subdev->device->bios,
  411. .outp = &outp->info,
  412. .crtc = head,
  413. .offset = outpdp->info.script[4],
  414. .execute = 1,
  415. };
  416. nvbios_exec(&init);
  417. atomic_set(&outpdp->lt.done, 0);
  418. }
  419. }
  420. static void
  421. nv50_disp_intr_unk20_1(struct nv50_disp *disp, int head)
  422. {
  423. struct nvkm_device *device = disp->base.engine.subdev.device;
  424. struct nvkm_devinit *devinit = device->devinit;
  425. u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
  426. if (pclk)
  427. nvkm_devinit_pll_set(devinit, PLL_VPLL0 + head, pclk);
  428. }
  429. static void
  430. nv50_disp_intr_unk20_2_dp(struct nv50_disp *disp, int head,
  431. struct dcb_output *outp, u32 pclk)
  432. {
  433. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  434. struct nvkm_device *device = subdev->device;
  435. const int link = !(outp->sorconf.link & 1);
  436. const int or = ffs(outp->or) - 1;
  437. const u32 soff = ( or * 0x800);
  438. const u32 loff = (link * 0x080) + soff;
  439. const u32 ctrl = nvkm_rd32(device, 0x610794 + (or * 8));
  440. const u32 symbol = 100000;
  441. const s32 vactive = nvkm_rd32(device, 0x610af8 + (head * 0x540)) & 0xffff;
  442. const s32 vblanke = nvkm_rd32(device, 0x610ae8 + (head * 0x540)) & 0xffff;
  443. const s32 vblanks = nvkm_rd32(device, 0x610af0 + (head * 0x540)) & 0xffff;
  444. u32 dpctrl = nvkm_rd32(device, 0x61c10c + loff);
  445. u32 clksor = nvkm_rd32(device, 0x614300 + soff);
  446. int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
  447. int TU, VTUi, VTUf, VTUa;
  448. u64 link_data_rate, link_ratio, unk;
  449. u32 best_diff = 64 * symbol;
  450. u32 link_nr, link_bw, bits;
  451. u64 value;
  452. link_bw = (clksor & 0x000c0000) ? 270000 : 162000;
  453. link_nr = hweight32(dpctrl & 0x000f0000);
  454. /* symbols/hblank - algorithm taken from comments in tegra driver */
  455. value = vblanke + vactive - vblanks - 7;
  456. value = value * link_bw;
  457. do_div(value, pclk);
  458. value = value - (3 * !!(dpctrl & 0x00004000)) - (12 / link_nr);
  459. nvkm_mask(device, 0x61c1e8 + soff, 0x0000ffff, value);
  460. /* symbols/vblank - algorithm taken from comments in tegra driver */
  461. value = vblanks - vblanke - 25;
  462. value = value * link_bw;
  463. do_div(value, pclk);
  464. value = value - ((36 / link_nr) + 3) - 1;
  465. nvkm_mask(device, 0x61c1ec + soff, 0x00ffffff, value);
  466. /* watermark / activesym */
  467. if ((ctrl & 0xf0000) == 0x60000) bits = 30;
  468. else if ((ctrl & 0xf0000) == 0x50000) bits = 24;
  469. else bits = 18;
  470. link_data_rate = (pclk * bits / 8) / link_nr;
  471. /* calculate ratio of packed data rate to link symbol rate */
  472. link_ratio = link_data_rate * symbol;
  473. do_div(link_ratio, link_bw);
  474. for (TU = 64; TU >= 32; TU--) {
  475. /* calculate average number of valid symbols in each TU */
  476. u32 tu_valid = link_ratio * TU;
  477. u32 calc, diff;
  478. /* find a hw representation for the fraction.. */
  479. VTUi = tu_valid / symbol;
  480. calc = VTUi * symbol;
  481. diff = tu_valid - calc;
  482. if (diff) {
  483. if (diff >= (symbol / 2)) {
  484. VTUf = symbol / (symbol - diff);
  485. if (symbol - (VTUf * diff))
  486. VTUf++;
  487. if (VTUf <= 15) {
  488. VTUa = 1;
  489. calc += symbol - (symbol / VTUf);
  490. } else {
  491. VTUa = 0;
  492. VTUf = 1;
  493. calc += symbol;
  494. }
  495. } else {
  496. VTUa = 0;
  497. VTUf = min((int)(symbol / diff), 15);
  498. calc += symbol / VTUf;
  499. }
  500. diff = calc - tu_valid;
  501. } else {
  502. /* no remainder, but the hw doesn't like the fractional
  503. * part to be zero. decrement the integer part and
  504. * have the fraction add a whole symbol back
  505. */
  506. VTUa = 0;
  507. VTUf = 1;
  508. VTUi--;
  509. }
  510. if (diff < best_diff) {
  511. best_diff = diff;
  512. bestTU = TU;
  513. bestVTUa = VTUa;
  514. bestVTUf = VTUf;
  515. bestVTUi = VTUi;
  516. if (diff == 0)
  517. break;
  518. }
  519. }
  520. if (!bestTU) {
  521. nvkm_error(subdev, "unable to find suitable dp config\n");
  522. return;
  523. }
  524. /* XXX close to vbios numbers, but not right */
  525. unk = (symbol - link_ratio) * bestTU;
  526. unk *= link_ratio;
  527. do_div(unk, symbol);
  528. do_div(unk, symbol);
  529. unk += 6;
  530. nvkm_mask(device, 0x61c10c + loff, 0x000001fc, bestTU << 2);
  531. nvkm_mask(device, 0x61c128 + loff, 0x010f7f3f, bestVTUa << 24 |
  532. bestVTUf << 16 |
  533. bestVTUi << 8 | unk);
  534. }
  535. static void
  536. nv50_disp_intr_unk20_2(struct nv50_disp *disp, int head)
  537. {
  538. struct nvkm_device *device = disp->base.engine.subdev.device;
  539. struct nvkm_output *outp;
  540. u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
  541. u32 hval, hreg = 0x614200 + (head * 0x800);
  542. u32 oval, oreg;
  543. u32 mask, conf;
  544. outp = exec_clkcmp(disp, head, 0xff, pclk, &conf);
  545. if (!outp)
  546. return;
  547. /* we allow both encoder attach and detach operations to occur
  548. * within a single supervisor (ie. modeset) sequence. the
  549. * encoder detach scripts quite often switch off power to the
  550. * lanes, which requires the link to be re-trained.
  551. *
  552. * this is not generally an issue as the sink "must" (heh)
  553. * signal an irq when it's lost sync so the driver can
  554. * re-train.
  555. *
  556. * however, on some boards, if one does not configure at least
  557. * the gpu side of the link *before* attaching, then various
  558. * things can go horribly wrong (PDISP disappearing from mmio,
  559. * third supervisor never happens, etc).
  560. *
  561. * the solution is simply to retrain here, if necessary. last
  562. * i checked, the binary driver userspace does not appear to
  563. * trigger this situation (it forces an UPDATE between steps).
  564. */
  565. if (outp->info.type == DCB_OUTPUT_DP) {
  566. u32 soff = (ffs(outp->info.or) - 1) * 0x08;
  567. u32 ctrl, datarate;
  568. if (outp->info.location == 0) {
  569. ctrl = nvkm_rd32(device, 0x610794 + soff);
  570. soff = 1;
  571. } else {
  572. ctrl = nvkm_rd32(device, 0x610b80 + soff);
  573. soff = 2;
  574. }
  575. switch ((ctrl & 0x000f0000) >> 16) {
  576. case 6: datarate = pclk * 30; break;
  577. case 5: datarate = pclk * 24; break;
  578. case 2:
  579. default:
  580. datarate = pclk * 18;
  581. break;
  582. }
  583. if (nvkm_output_dp_train(outp, datarate / soff, true))
  584. OUTP_ERR(outp, "link not trained before attach");
  585. }
  586. exec_clkcmp(disp, head, 0, pclk, &conf);
  587. if (!outp->info.location && outp->info.type == DCB_OUTPUT_ANALOG) {
  588. oreg = 0x614280 + (ffs(outp->info.or) - 1) * 0x800;
  589. oval = 0x00000000;
  590. hval = 0x00000000;
  591. mask = 0xffffffff;
  592. } else
  593. if (!outp->info.location) {
  594. if (outp->info.type == DCB_OUTPUT_DP)
  595. nv50_disp_intr_unk20_2_dp(disp, head, &outp->info, pclk);
  596. oreg = 0x614300 + (ffs(outp->info.or) - 1) * 0x800;
  597. oval = (conf & 0x0100) ? 0x00000101 : 0x00000000;
  598. hval = 0x00000000;
  599. mask = 0x00000707;
  600. } else {
  601. oreg = 0x614380 + (ffs(outp->info.or) - 1) * 0x800;
  602. oval = 0x00000001;
  603. hval = 0x00000001;
  604. mask = 0x00000707;
  605. }
  606. nvkm_mask(device, hreg, 0x0000000f, hval);
  607. nvkm_mask(device, oreg, mask, oval);
  608. }
  609. /* If programming a TMDS output on a SOR that can also be configured for
  610. * DisplayPort, make sure NV50_SOR_DP_CTRL_ENABLE is forced off.
  611. *
  612. * It looks like the VBIOS TMDS scripts make an attempt at this, however,
  613. * the VBIOS scripts on at least one board I have only switch it off on
  614. * link 0, causing a blank display if the output has previously been
  615. * programmed for DisplayPort.
  616. */
  617. static void
  618. nv50_disp_intr_unk40_0_tmds(struct nv50_disp *disp,
  619. struct dcb_output *outp)
  620. {
  621. struct nvkm_device *device = disp->base.engine.subdev.device;
  622. struct nvkm_bios *bios = device->bios;
  623. const int link = !(outp->sorconf.link & 1);
  624. const int or = ffs(outp->or) - 1;
  625. const u32 loff = (or * 0x800) + (link * 0x80);
  626. const u16 mask = (outp->sorconf.link << 6) | outp->or;
  627. struct dcb_output match;
  628. u8 ver, hdr;
  629. if (dcb_outp_match(bios, DCB_OUTPUT_DP, mask, &ver, &hdr, &match))
  630. nvkm_mask(device, 0x61c10c + loff, 0x00000001, 0x00000000);
  631. }
  632. static void
  633. nv50_disp_intr_unk40_0(struct nv50_disp *disp, int head)
  634. {
  635. struct nvkm_device *device = disp->base.engine.subdev.device;
  636. struct nvkm_output *outp;
  637. u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
  638. u32 conf;
  639. outp = exec_clkcmp(disp, head, 1, pclk, &conf);
  640. if (!outp)
  641. return;
  642. if (outp->info.location == 0 && outp->info.type == DCB_OUTPUT_TMDS)
  643. nv50_disp_intr_unk40_0_tmds(disp, &outp->info);
  644. }
  645. void
  646. nv50_disp_intr_supervisor(struct work_struct *work)
  647. {
  648. struct nv50_disp *disp =
  649. container_of(work, struct nv50_disp, supervisor);
  650. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  651. struct nvkm_device *device = subdev->device;
  652. u32 super = nvkm_rd32(device, 0x610030);
  653. int head;
  654. nvkm_debug(subdev, "supervisor %08x %08x\n", disp->super, super);
  655. if (disp->super & 0x00000010) {
  656. nv50_disp_chan_mthd(disp->chan[0], NV_DBG_DEBUG);
  657. for (head = 0; head < disp->base.head.nr; head++) {
  658. if (!(super & (0x00000020 << head)))
  659. continue;
  660. if (!(super & (0x00000080 << head)))
  661. continue;
  662. nv50_disp_intr_unk10_0(disp, head);
  663. }
  664. } else
  665. if (disp->super & 0x00000020) {
  666. for (head = 0; head < disp->base.head.nr; head++) {
  667. if (!(super & (0x00000080 << head)))
  668. continue;
  669. nv50_disp_intr_unk20_0(disp, head);
  670. }
  671. for (head = 0; head < disp->base.head.nr; head++) {
  672. if (!(super & (0x00000200 << head)))
  673. continue;
  674. nv50_disp_intr_unk20_1(disp, head);
  675. }
  676. for (head = 0; head < disp->base.head.nr; head++) {
  677. if (!(super & (0x00000080 << head)))
  678. continue;
  679. nv50_disp_intr_unk20_2(disp, head);
  680. }
  681. } else
  682. if (disp->super & 0x00000040) {
  683. for (head = 0; head < disp->base.head.nr; head++) {
  684. if (!(super & (0x00000080 << head)))
  685. continue;
  686. nv50_disp_intr_unk40_0(disp, head);
  687. }
  688. }
  689. nvkm_wr32(device, 0x610030, 0x80000000);
  690. }
  691. void
  692. nv50_disp_intr(struct nv50_disp *disp)
  693. {
  694. struct nvkm_device *device = disp->base.engine.subdev.device;
  695. u32 intr0 = nvkm_rd32(device, 0x610020);
  696. u32 intr1 = nvkm_rd32(device, 0x610024);
  697. while (intr0 & 0x001f0000) {
  698. u32 chid = __ffs(intr0 & 0x001f0000) - 16;
  699. nv50_disp_intr_error(disp, chid);
  700. intr0 &= ~(0x00010000 << chid);
  701. }
  702. while (intr0 & 0x0000001f) {
  703. u32 chid = __ffs(intr0 & 0x0000001f);
  704. nv50_disp_chan_uevent_send(disp, chid);
  705. intr0 &= ~(0x00000001 << chid);
  706. }
  707. if (intr1 & 0x00000004) {
  708. nvkm_disp_vblank(&disp->base, 0);
  709. nvkm_wr32(device, 0x610024, 0x00000004);
  710. }
  711. if (intr1 & 0x00000008) {
  712. nvkm_disp_vblank(&disp->base, 1);
  713. nvkm_wr32(device, 0x610024, 0x00000008);
  714. }
  715. if (intr1 & 0x00000070) {
  716. disp->super = (intr1 & 0x00000070);
  717. schedule_work(&disp->supervisor);
  718. nvkm_wr32(device, 0x610024, disp->super);
  719. }
  720. }
  721. static const struct nv50_disp_func
  722. nv50_disp = {
  723. .intr = nv50_disp_intr,
  724. .uevent = &nv50_disp_chan_uevent,
  725. .super = nv50_disp_intr_supervisor,
  726. .root = &nv50_disp_root_oclass,
  727. .head.vblank_init = nv50_disp_vblank_init,
  728. .head.vblank_fini = nv50_disp_vblank_fini,
  729. .head.scanoutpos = nv50_disp_root_scanoutpos,
  730. .outp.internal.crt = nv50_dac_output_new,
  731. .outp.internal.tmds = nv50_sor_output_new,
  732. .outp.internal.lvds = nv50_sor_output_new,
  733. .outp.external.tmds = nv50_pior_output_new,
  734. .outp.external.dp = nv50_pior_dp_new,
  735. .dac.nr = 3,
  736. .dac.power = nv50_dac_power,
  737. .dac.sense = nv50_dac_sense,
  738. .sor.nr = 2,
  739. .sor.power = nv50_sor_power,
  740. .pior.nr = 3,
  741. .pior.power = nv50_pior_power,
  742. };
  743. int
  744. nv50_disp_new(struct nvkm_device *device, int index, struct nvkm_disp **pdisp)
  745. {
  746. return nv50_disp_new_(&nv50_disp, device, index, 2, pdisp);
  747. }