nv50.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. case 0xaa:
  387. case 0xac:
  388. return true;
  389. default:
  390. break;
  391. }
  392. return false;
  393. }
  394. static bool
  395. nv50_disp_dptmds_war_needed(struct nv50_disp *disp, struct dcb_output *outp)
  396. {
  397. struct nvkm_device *device = disp->base.engine.subdev.device;
  398. const u32 soff = __ffs(outp->or) * 0x800;
  399. if (nv50_disp_dptmds_war(device) && outp->type == DCB_OUTPUT_TMDS) {
  400. switch (nvkm_rd32(device, 0x614300 + soff) & 0x00030000) {
  401. case 0x00000000:
  402. case 0x00030000:
  403. return true;
  404. default:
  405. break;
  406. }
  407. }
  408. return false;
  409. }
  410. static void
  411. nv50_disp_dptmds_war_2(struct nv50_disp *disp, struct dcb_output *outp)
  412. {
  413. struct nvkm_device *device = disp->base.engine.subdev.device;
  414. const u32 soff = __ffs(outp->or) * 0x800;
  415. if (!nv50_disp_dptmds_war_needed(disp, outp))
  416. return;
  417. nvkm_mask(device, 0x00e840, 0x80000000, 0x80000000);
  418. nvkm_mask(device, 0x614300 + soff, 0x03000000, 0x03000000);
  419. nvkm_mask(device, 0x61c10c + soff, 0x00000001, 0x00000001);
  420. nvkm_mask(device, 0x61c00c + soff, 0x0f000000, 0x00000000);
  421. nvkm_mask(device, 0x61c008 + soff, 0xff000000, 0x14000000);
  422. nvkm_usec(device, 400, NVKM_DELAY);
  423. nvkm_mask(device, 0x61c008 + soff, 0xff000000, 0x00000000);
  424. nvkm_mask(device, 0x61c00c + soff, 0x0f000000, 0x01000000);
  425. if (nvkm_rd32(device, 0x61c004 + soff) & 0x00000001) {
  426. u32 seqctl = nvkm_rd32(device, 0x61c030 + soff);
  427. u32 pu_pc = seqctl & 0x0000000f;
  428. nvkm_wr32(device, 0x61c040 + soff + pu_pc * 4, 0x1f008000);
  429. }
  430. }
  431. static void
  432. nv50_disp_dptmds_war_3(struct nv50_disp *disp, struct dcb_output *outp)
  433. {
  434. struct nvkm_device *device = disp->base.engine.subdev.device;
  435. const u32 soff = __ffs(outp->or) * 0x800;
  436. u32 sorpwr;
  437. if (!nv50_disp_dptmds_war_needed(disp, outp))
  438. return;
  439. sorpwr = nvkm_rd32(device, 0x61c004 + soff);
  440. if (sorpwr & 0x00000001) {
  441. u32 seqctl = nvkm_rd32(device, 0x61c030 + soff);
  442. u32 pd_pc = (seqctl & 0x00000f00) >> 8;
  443. u32 pu_pc = seqctl & 0x0000000f;
  444. nvkm_wr32(device, 0x61c040 + soff + pd_pc * 4, 0x1f008000);
  445. nvkm_msec(device, 2000,
  446. if (!(nvkm_rd32(device, 0x61c030 + soff) & 0x10000000))
  447. break;
  448. );
  449. nvkm_mask(device, 0x61c004 + soff, 0x80000001, 0x80000000);
  450. nvkm_msec(device, 2000,
  451. if (!(nvkm_rd32(device, 0x61c030 + soff) & 0x10000000))
  452. break;
  453. );
  454. nvkm_wr32(device, 0x61c040 + soff + pd_pc * 4, 0x00002000);
  455. nvkm_wr32(device, 0x61c040 + soff + pu_pc * 4, 0x1f000000);
  456. }
  457. nvkm_mask(device, 0x61c10c + soff, 0x00000001, 0x00000000);
  458. nvkm_mask(device, 0x614300 + soff, 0x03000000, 0x00000000);
  459. if (sorpwr & 0x00000001) {
  460. nvkm_mask(device, 0x61c004 + soff, 0x80000001, 0x80000001);
  461. }
  462. }
  463. static void
  464. nv50_disp_update_sppll1(struct nv50_disp *disp)
  465. {
  466. struct nvkm_device *device = disp->base.engine.subdev.device;
  467. bool used = false;
  468. int sor;
  469. if (!nv50_disp_dptmds_war(device))
  470. return;
  471. for (sor = 0; sor < disp->func->sor.nr; sor++) {
  472. u32 clksor = nvkm_rd32(device, 0x614300 + (sor * 0x800));
  473. switch (clksor & 0x03000000) {
  474. case 0x02000000:
  475. case 0x03000000:
  476. used = true;
  477. break;
  478. default:
  479. break;
  480. }
  481. }
  482. if (used)
  483. return;
  484. nvkm_mask(device, 0x00e840, 0x80000000, 0x00000000);
  485. }
  486. static void
  487. nv50_disp_intr_unk10_0(struct nv50_disp *disp, int head)
  488. {
  489. exec_script(disp, head, 1);
  490. }
  491. static void
  492. nv50_disp_intr_unk20_0(struct nv50_disp *disp, int head)
  493. {
  494. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  495. struct nvkm_output *outp = exec_script(disp, head, 2);
  496. /* the binary driver does this outside of the supervisor handling
  497. * (after the third supervisor from a detach). we (currently?)
  498. * allow both detach/attach to happen in the same set of
  499. * supervisor interrupts, so it would make sense to execute this
  500. * (full power down?) script after all the detach phases of the
  501. * supervisor handling. like with training if needed from the
  502. * second supervisor, nvidia doesn't do this, so who knows if it's
  503. * entirely safe, but it does appear to work..
  504. *
  505. * without this script being run, on some configurations i've
  506. * seen, switching from DP to TMDS on a DP connector may result
  507. * in a blank screen (SOR_PWR off/on can restore it)
  508. */
  509. if (outp && outp->info.type == DCB_OUTPUT_DP) {
  510. struct nvkm_output_dp *outpdp = nvkm_output_dp(outp);
  511. struct nvbios_init init = {
  512. .subdev = subdev,
  513. .bios = subdev->device->bios,
  514. .outp = &outp->info,
  515. .crtc = head,
  516. .offset = outpdp->info.script[4],
  517. .execute = 1,
  518. };
  519. nvkm_notify_put(&outpdp->irq);
  520. nvbios_exec(&init);
  521. atomic_set(&outpdp->lt.done, 0);
  522. }
  523. }
  524. static void
  525. nv50_disp_intr_unk20_1(struct nv50_disp *disp, int head)
  526. {
  527. struct nvkm_device *device = disp->base.engine.subdev.device;
  528. struct nvkm_devinit *devinit = device->devinit;
  529. u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
  530. if (pclk)
  531. nvkm_devinit_pll_set(devinit, PLL_VPLL0 + head, pclk);
  532. }
  533. static void
  534. nv50_disp_intr_unk20_2_dp(struct nv50_disp *disp, int head,
  535. struct dcb_output *outp, u32 pclk)
  536. {
  537. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  538. struct nvkm_device *device = subdev->device;
  539. const int link = !(outp->sorconf.link & 1);
  540. const int or = ffs(outp->or) - 1;
  541. const u32 soff = ( or * 0x800);
  542. const u32 loff = (link * 0x080) + soff;
  543. const u32 ctrl = nvkm_rd32(device, 0x610794 + (or * 8));
  544. const u32 symbol = 100000;
  545. const s32 vactive = nvkm_rd32(device, 0x610af8 + (head * 0x540)) & 0xffff;
  546. const s32 vblanke = nvkm_rd32(device, 0x610ae8 + (head * 0x540)) & 0xffff;
  547. const s32 vblanks = nvkm_rd32(device, 0x610af0 + (head * 0x540)) & 0xffff;
  548. u32 dpctrl = nvkm_rd32(device, 0x61c10c + loff);
  549. u32 clksor = nvkm_rd32(device, 0x614300 + soff);
  550. int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
  551. int TU, VTUi, VTUf, VTUa;
  552. u64 link_data_rate, link_ratio, unk;
  553. u32 best_diff = 64 * symbol;
  554. u32 link_nr, link_bw, bits;
  555. u64 value;
  556. link_bw = (clksor & 0x000c0000) ? 270000 : 162000;
  557. link_nr = hweight32(dpctrl & 0x000f0000);
  558. /* symbols/hblank - algorithm taken from comments in tegra driver */
  559. value = vblanke + vactive - vblanks - 7;
  560. value = value * link_bw;
  561. do_div(value, pclk);
  562. value = value - (3 * !!(dpctrl & 0x00004000)) - (12 / link_nr);
  563. nvkm_mask(device, 0x61c1e8 + soff, 0x0000ffff, value);
  564. /* symbols/vblank - algorithm taken from comments in tegra driver */
  565. value = vblanks - vblanke - 25;
  566. value = value * link_bw;
  567. do_div(value, pclk);
  568. value = value - ((36 / link_nr) + 3) - 1;
  569. nvkm_mask(device, 0x61c1ec + soff, 0x00ffffff, value);
  570. /* watermark / activesym */
  571. if ((ctrl & 0xf0000) == 0x60000) bits = 30;
  572. else if ((ctrl & 0xf0000) == 0x50000) bits = 24;
  573. else bits = 18;
  574. link_data_rate = (pclk * bits / 8) / link_nr;
  575. /* calculate ratio of packed data rate to link symbol rate */
  576. link_ratio = link_data_rate * symbol;
  577. do_div(link_ratio, link_bw);
  578. for (TU = 64; TU >= 32; TU--) {
  579. /* calculate average number of valid symbols in each TU */
  580. u32 tu_valid = link_ratio * TU;
  581. u32 calc, diff;
  582. /* find a hw representation for the fraction.. */
  583. VTUi = tu_valid / symbol;
  584. calc = VTUi * symbol;
  585. diff = tu_valid - calc;
  586. if (diff) {
  587. if (diff >= (symbol / 2)) {
  588. VTUf = symbol / (symbol - diff);
  589. if (symbol - (VTUf * diff))
  590. VTUf++;
  591. if (VTUf <= 15) {
  592. VTUa = 1;
  593. calc += symbol - (symbol / VTUf);
  594. } else {
  595. VTUa = 0;
  596. VTUf = 1;
  597. calc += symbol;
  598. }
  599. } else {
  600. VTUa = 0;
  601. VTUf = min((int)(symbol / diff), 15);
  602. calc += symbol / VTUf;
  603. }
  604. diff = calc - tu_valid;
  605. } else {
  606. /* no remainder, but the hw doesn't like the fractional
  607. * part to be zero. decrement the integer part and
  608. * have the fraction add a whole symbol back
  609. */
  610. VTUa = 0;
  611. VTUf = 1;
  612. VTUi--;
  613. }
  614. if (diff < best_diff) {
  615. best_diff = diff;
  616. bestTU = TU;
  617. bestVTUa = VTUa;
  618. bestVTUf = VTUf;
  619. bestVTUi = VTUi;
  620. if (diff == 0)
  621. break;
  622. }
  623. }
  624. if (!bestTU) {
  625. nvkm_error(subdev, "unable to find suitable dp config\n");
  626. return;
  627. }
  628. /* XXX close to vbios numbers, but not right */
  629. unk = (symbol - link_ratio) * bestTU;
  630. unk *= link_ratio;
  631. do_div(unk, symbol);
  632. do_div(unk, symbol);
  633. unk += 6;
  634. nvkm_mask(device, 0x61c10c + loff, 0x000001fc, bestTU << 2);
  635. nvkm_mask(device, 0x61c128 + loff, 0x010f7f3f, bestVTUa << 24 |
  636. bestVTUf << 16 |
  637. bestVTUi << 8 | unk);
  638. }
  639. static void
  640. nv50_disp_intr_unk20_2(struct nv50_disp *disp, int head)
  641. {
  642. struct nvkm_device *device = disp->base.engine.subdev.device;
  643. struct nvkm_output *outp;
  644. u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
  645. u32 hval, hreg = 0x614200 + (head * 0x800);
  646. u32 oval, oreg;
  647. u32 mask, conf;
  648. outp = exec_clkcmp(disp, head, 0xff, pclk, &conf);
  649. if (!outp)
  650. return;
  651. /* we allow both encoder attach and detach operations to occur
  652. * within a single supervisor (ie. modeset) sequence. the
  653. * encoder detach scripts quite often switch off power to the
  654. * lanes, which requires the link to be re-trained.
  655. *
  656. * this is not generally an issue as the sink "must" (heh)
  657. * signal an irq when it's lost sync so the driver can
  658. * re-train.
  659. *
  660. * however, on some boards, if one does not configure at least
  661. * the gpu side of the link *before* attaching, then various
  662. * things can go horribly wrong (PDISP disappearing from mmio,
  663. * third supervisor never happens, etc).
  664. *
  665. * the solution is simply to retrain here, if necessary. last
  666. * i checked, the binary driver userspace does not appear to
  667. * trigger this situation (it forces an UPDATE between steps).
  668. */
  669. if (outp->info.type == DCB_OUTPUT_DP) {
  670. u32 soff = (ffs(outp->info.or) - 1) * 0x08;
  671. u32 ctrl, datarate;
  672. if (outp->info.location == 0) {
  673. ctrl = nvkm_rd32(device, 0x610794 + soff);
  674. soff = 1;
  675. } else {
  676. ctrl = nvkm_rd32(device, 0x610b80 + soff);
  677. soff = 2;
  678. }
  679. switch ((ctrl & 0x000f0000) >> 16) {
  680. case 6: datarate = pclk * 30; break;
  681. case 5: datarate = pclk * 24; break;
  682. case 2:
  683. default:
  684. datarate = pclk * 18;
  685. break;
  686. }
  687. if (nvkm_output_dp_train(outp, datarate / soff))
  688. OUTP_ERR(outp, "link not trained before attach");
  689. }
  690. exec_clkcmp(disp, head, 0, pclk, &conf);
  691. if (!outp->info.location && outp->info.type == DCB_OUTPUT_ANALOG) {
  692. oreg = 0x614280 + (ffs(outp->info.or) - 1) * 0x800;
  693. oval = 0x00000000;
  694. hval = 0x00000000;
  695. mask = 0xffffffff;
  696. } else
  697. if (!outp->info.location) {
  698. if (outp->info.type == DCB_OUTPUT_DP)
  699. nv50_disp_intr_unk20_2_dp(disp, head, &outp->info, pclk);
  700. oreg = 0x614300 + (ffs(outp->info.or) - 1) * 0x800;
  701. oval = (conf & 0x0100) ? 0x00000101 : 0x00000000;
  702. hval = 0x00000000;
  703. mask = 0x00000707;
  704. } else {
  705. oreg = 0x614380 + (ffs(outp->info.or) - 1) * 0x800;
  706. oval = 0x00000001;
  707. hval = 0x00000001;
  708. mask = 0x00000707;
  709. }
  710. nvkm_mask(device, hreg, 0x0000000f, hval);
  711. nvkm_mask(device, oreg, mask, oval);
  712. nv50_disp_dptmds_war_2(disp, &outp->info);
  713. }
  714. /* If programming a TMDS output on a SOR that can also be configured for
  715. * DisplayPort, make sure NV50_SOR_DP_CTRL_ENABLE is forced off.
  716. *
  717. * It looks like the VBIOS TMDS scripts make an attempt at this, however,
  718. * the VBIOS scripts on at least one board I have only switch it off on
  719. * link 0, causing a blank display if the output has previously been
  720. * programmed for DisplayPort.
  721. */
  722. static void
  723. nv50_disp_intr_unk40_0_tmds(struct nv50_disp *disp,
  724. struct dcb_output *outp)
  725. {
  726. struct nvkm_device *device = disp->base.engine.subdev.device;
  727. struct nvkm_bios *bios = device->bios;
  728. const int link = !(outp->sorconf.link & 1);
  729. const int or = ffs(outp->or) - 1;
  730. const u32 loff = (or * 0x800) + (link * 0x80);
  731. const u16 mask = (outp->sorconf.link << 6) | outp->or;
  732. struct dcb_output match;
  733. u8 ver, hdr;
  734. if (dcb_outp_match(bios, DCB_OUTPUT_DP, mask, &ver, &hdr, &match))
  735. nvkm_mask(device, 0x61c10c + loff, 0x00000001, 0x00000000);
  736. }
  737. static void
  738. nv50_disp_intr_unk40_0(struct nv50_disp *disp, int head)
  739. {
  740. struct nvkm_device *device = disp->base.engine.subdev.device;
  741. struct nvkm_output *outp;
  742. u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
  743. u32 conf;
  744. outp = exec_clkcmp(disp, head, 1, pclk, &conf);
  745. if (!outp)
  746. return;
  747. if (outp->info.location == 0 && outp->info.type == DCB_OUTPUT_TMDS)
  748. nv50_disp_intr_unk40_0_tmds(disp, &outp->info);
  749. nv50_disp_dptmds_war_3(disp, &outp->info);
  750. }
  751. void
  752. nv50_disp_intr_supervisor(struct work_struct *work)
  753. {
  754. struct nv50_disp *disp =
  755. container_of(work, struct nv50_disp, supervisor);
  756. struct nvkm_subdev *subdev = &disp->base.engine.subdev;
  757. struct nvkm_device *device = subdev->device;
  758. u32 super = nvkm_rd32(device, 0x610030);
  759. int head;
  760. nvkm_debug(subdev, "supervisor %08x %08x\n", disp->super, super);
  761. if (disp->super & 0x00000010) {
  762. nv50_disp_chan_mthd(disp->chan[0], NV_DBG_DEBUG);
  763. for (head = 0; head < disp->base.head.nr; head++) {
  764. if (!(super & (0x00000020 << head)))
  765. continue;
  766. if (!(super & (0x00000080 << head)))
  767. continue;
  768. nv50_disp_intr_unk10_0(disp, head);
  769. }
  770. } else
  771. if (disp->super & 0x00000020) {
  772. for (head = 0; head < disp->base.head.nr; head++) {
  773. if (!(super & (0x00000080 << head)))
  774. continue;
  775. nv50_disp_intr_unk20_0(disp, head);
  776. }
  777. for (head = 0; head < disp->base.head.nr; head++) {
  778. if (!(super & (0x00000200 << head)))
  779. continue;
  780. nv50_disp_intr_unk20_1(disp, head);
  781. }
  782. for (head = 0; head < disp->base.head.nr; head++) {
  783. if (!(super & (0x00000080 << head)))
  784. continue;
  785. nv50_disp_intr_unk20_2(disp, head);
  786. }
  787. } else
  788. if (disp->super & 0x00000040) {
  789. for (head = 0; head < disp->base.head.nr; head++) {
  790. if (!(super & (0x00000080 << head)))
  791. continue;
  792. nv50_disp_intr_unk40_0(disp, head);
  793. }
  794. nv50_disp_update_sppll1(disp);
  795. }
  796. nvkm_wr32(device, 0x610030, 0x80000000);
  797. }
  798. void
  799. nv50_disp_intr(struct nv50_disp *disp)
  800. {
  801. struct nvkm_device *device = disp->base.engine.subdev.device;
  802. u32 intr0 = nvkm_rd32(device, 0x610020);
  803. u32 intr1 = nvkm_rd32(device, 0x610024);
  804. while (intr0 & 0x001f0000) {
  805. u32 chid = __ffs(intr0 & 0x001f0000) - 16;
  806. nv50_disp_intr_error(disp, chid);
  807. intr0 &= ~(0x00010000 << chid);
  808. }
  809. while (intr0 & 0x0000001f) {
  810. u32 chid = __ffs(intr0 & 0x0000001f);
  811. nv50_disp_chan_uevent_send(disp, chid);
  812. intr0 &= ~(0x00000001 << chid);
  813. }
  814. if (intr1 & 0x00000004) {
  815. nvkm_disp_vblank(&disp->base, 0);
  816. nvkm_wr32(device, 0x610024, 0x00000004);
  817. }
  818. if (intr1 & 0x00000008) {
  819. nvkm_disp_vblank(&disp->base, 1);
  820. nvkm_wr32(device, 0x610024, 0x00000008);
  821. }
  822. if (intr1 & 0x00000070) {
  823. disp->super = (intr1 & 0x00000070);
  824. schedule_work(&disp->supervisor);
  825. nvkm_wr32(device, 0x610024, disp->super);
  826. }
  827. }
  828. static const struct nv50_disp_func
  829. nv50_disp = {
  830. .intr = nv50_disp_intr,
  831. .uevent = &nv50_disp_chan_uevent,
  832. .super = nv50_disp_intr_supervisor,
  833. .root = &nv50_disp_root_oclass,
  834. .head.vblank_init = nv50_disp_vblank_init,
  835. .head.vblank_fini = nv50_disp_vblank_fini,
  836. .head.scanoutpos = nv50_disp_root_scanoutpos,
  837. .outp.internal.crt = nv50_dac_output_new,
  838. .outp.internal.tmds = nv50_sor_output_new,
  839. .outp.internal.lvds = nv50_sor_output_new,
  840. .outp.external.tmds = nv50_pior_output_new,
  841. .outp.external.dp = nv50_pior_dp_new,
  842. .dac.nr = 3,
  843. .dac.power = nv50_dac_power,
  844. .dac.sense = nv50_dac_sense,
  845. .sor.nr = 2,
  846. .sor.power = nv50_sor_power,
  847. .pior.nr = 3,
  848. .pior.power = nv50_pior_power,
  849. };
  850. int
  851. nv50_disp_new(struct nvkm_device *device, int index, struct nvkm_disp **pdisp)
  852. {
  853. return nv50_disp_new_(&nv50_disp, device, index, 2, pdisp);
  854. }