nv50.c 26 KB

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