nv50_display.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * Copyright (C) 2008 Maarten Maathuis.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include "nv50_display.h"
  27. #include "nouveau_crtc.h"
  28. #include "nouveau_encoder.h"
  29. #include "nouveau_connector.h"
  30. #include "nouveau_fb.h"
  31. #include "nouveau_fbcon.h"
  32. #include "nouveau_ramht.h"
  33. #include "drm_crtc_helper.h"
  34. static inline int
  35. nv50_sor_nr(struct drm_device *dev)
  36. {
  37. struct drm_nouveau_private *dev_priv = dev->dev_private;
  38. if (dev_priv->chipset < 0x90 ||
  39. dev_priv->chipset == 0x92 ||
  40. dev_priv->chipset == 0xa0)
  41. return 2;
  42. return 4;
  43. }
  44. int
  45. nv50_display_early_init(struct drm_device *dev)
  46. {
  47. return 0;
  48. }
  49. void
  50. nv50_display_late_takedown(struct drm_device *dev)
  51. {
  52. }
  53. int
  54. nv50_display_init(struct drm_device *dev)
  55. {
  56. struct drm_nouveau_private *dev_priv = dev->dev_private;
  57. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  58. struct drm_connector *connector;
  59. struct nouveau_channel *evo;
  60. int ret, i;
  61. u32 val;
  62. NV_DEBUG_KMS(dev, "\n");
  63. nv_wr32(dev, 0x00610184, nv_rd32(dev, 0x00614004));
  64. /*
  65. * I think the 0x006101XX range is some kind of main control area
  66. * that enables things.
  67. */
  68. /* CRTC? */
  69. for (i = 0; i < 2; i++) {
  70. val = nv_rd32(dev, 0x00616100 + (i * 0x800));
  71. nv_wr32(dev, 0x00610190 + (i * 0x10), val);
  72. val = nv_rd32(dev, 0x00616104 + (i * 0x800));
  73. nv_wr32(dev, 0x00610194 + (i * 0x10), val);
  74. val = nv_rd32(dev, 0x00616108 + (i * 0x800));
  75. nv_wr32(dev, 0x00610198 + (i * 0x10), val);
  76. val = nv_rd32(dev, 0x0061610c + (i * 0x800));
  77. nv_wr32(dev, 0x0061019c + (i * 0x10), val);
  78. }
  79. /* DAC */
  80. for (i = 0; i < 3; i++) {
  81. val = nv_rd32(dev, 0x0061a000 + (i * 0x800));
  82. nv_wr32(dev, 0x006101d0 + (i * 0x04), val);
  83. }
  84. /* SOR */
  85. for (i = 0; i < nv50_sor_nr(dev); i++) {
  86. val = nv_rd32(dev, 0x0061c000 + (i * 0x800));
  87. nv_wr32(dev, 0x006101e0 + (i * 0x04), val);
  88. }
  89. /* EXT */
  90. for (i = 0; i < 3; i++) {
  91. val = nv_rd32(dev, 0x0061e000 + (i * 0x800));
  92. nv_wr32(dev, 0x006101f0 + (i * 0x04), val);
  93. }
  94. for (i = 0; i < 3; i++) {
  95. nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(i), 0x00550000 |
  96. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
  97. nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL1(i), 0x00000001);
  98. }
  99. /* The precise purpose is unknown, i suspect it has something to do
  100. * with text mode.
  101. */
  102. if (nv_rd32(dev, NV50_PDISPLAY_INTR_1) & 0x100) {
  103. nv_wr32(dev, NV50_PDISPLAY_INTR_1, 0x100);
  104. nv_wr32(dev, 0x006194e8, nv_rd32(dev, 0x006194e8) & ~1);
  105. if (!nv_wait(dev, 0x006194e8, 2, 0)) {
  106. NV_ERROR(dev, "timeout: (0x6194e8 & 2) != 0\n");
  107. NV_ERROR(dev, "0x6194e8 = 0x%08x\n",
  108. nv_rd32(dev, 0x6194e8));
  109. return -EBUSY;
  110. }
  111. }
  112. for (i = 0; i < 2; i++) {
  113. nv_wr32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i), 0x2000);
  114. if (!nv_wait(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
  115. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS, 0)) {
  116. NV_ERROR(dev, "timeout: CURSOR_CTRL2_STATUS == 0\n");
  117. NV_ERROR(dev, "CURSOR_CTRL2 = 0x%08x\n",
  118. nv_rd32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
  119. return -EBUSY;
  120. }
  121. nv_wr32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
  122. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_ON);
  123. if (!nv_wait(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
  124. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS,
  125. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS_ACTIVE)) {
  126. NV_ERROR(dev, "timeout: "
  127. "CURSOR_CTRL2_STATUS_ACTIVE(%d)\n", i);
  128. NV_ERROR(dev, "CURSOR_CTRL2(%d) = 0x%08x\n", i,
  129. nv_rd32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
  130. return -EBUSY;
  131. }
  132. }
  133. nv_wr32(dev, NV50_PDISPLAY_PIO_CTRL, 0x00000000);
  134. nv_mask(dev, NV50_PDISPLAY_INTR_0, 0x00000000, 0x00000000);
  135. nv_wr32(dev, NV50_PDISPLAY_INTR_EN_0, 0x00000000);
  136. nv_mask(dev, NV50_PDISPLAY_INTR_1, 0x00000000, 0x00000000);
  137. nv_wr32(dev, NV50_PDISPLAY_INTR_EN_1,
  138. NV50_PDISPLAY_INTR_EN_1_CLK_UNK10 |
  139. NV50_PDISPLAY_INTR_EN_1_CLK_UNK20 |
  140. NV50_PDISPLAY_INTR_EN_1_CLK_UNK40);
  141. /* enable hotplug interrupts */
  142. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  143. struct nouveau_connector *conn = nouveau_connector(connector);
  144. if (conn->dcb->gpio_tag == 0xff)
  145. continue;
  146. pgpio->irq_enable(dev, conn->dcb->gpio_tag, true);
  147. }
  148. ret = nv50_evo_init(dev);
  149. if (ret)
  150. return ret;
  151. evo = dev_priv->evo;
  152. nv_wr32(dev, NV50_PDISPLAY_OBJECTS, (evo->ramin->vinst >> 8) | 9);
  153. ret = RING_SPACE(evo, 11);
  154. if (ret)
  155. return ret;
  156. BEGIN_RING(evo, 0, NV50_EVO_UNK84, 2);
  157. OUT_RING(evo, NV50_EVO_UNK84_NOTIFY_DISABLED);
  158. OUT_RING(evo, NV50_EVO_DMA_NOTIFY_HANDLE_NONE);
  159. BEGIN_RING(evo, 0, NV50_EVO_CRTC(0, FB_DMA), 1);
  160. OUT_RING(evo, NV50_EVO_CRTC_FB_DMA_HANDLE_NONE);
  161. BEGIN_RING(evo, 0, NV50_EVO_CRTC(0, UNK0800), 1);
  162. OUT_RING(evo, 0);
  163. BEGIN_RING(evo, 0, NV50_EVO_CRTC(0, DISPLAY_START), 1);
  164. OUT_RING(evo, 0);
  165. BEGIN_RING(evo, 0, NV50_EVO_CRTC(0, UNK082C), 1);
  166. OUT_RING(evo, 0);
  167. FIRE_RING(evo);
  168. if (!nv_wait(dev, 0x640004, 0xffffffff, evo->dma.put << 2))
  169. NV_ERROR(dev, "evo pushbuf stalled\n");
  170. return 0;
  171. }
  172. static int nv50_display_disable(struct drm_device *dev)
  173. {
  174. struct drm_nouveau_private *dev_priv = dev->dev_private;
  175. struct drm_crtc *drm_crtc;
  176. int ret, i;
  177. NV_DEBUG_KMS(dev, "\n");
  178. list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
  179. struct nouveau_crtc *crtc = nouveau_crtc(drm_crtc);
  180. nv50_crtc_blank(crtc, true);
  181. }
  182. ret = RING_SPACE(dev_priv->evo, 2);
  183. if (ret == 0) {
  184. BEGIN_RING(dev_priv->evo, 0, NV50_EVO_UPDATE, 1);
  185. OUT_RING(dev_priv->evo, 0);
  186. }
  187. FIRE_RING(dev_priv->evo);
  188. /* Almost like ack'ing a vblank interrupt, maybe in the spirit of
  189. * cleaning up?
  190. */
  191. list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
  192. struct nouveau_crtc *crtc = nouveau_crtc(drm_crtc);
  193. uint32_t mask = NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(crtc->index);
  194. if (!crtc->base.enabled)
  195. continue;
  196. nv_wr32(dev, NV50_PDISPLAY_INTR_1, mask);
  197. if (!nv_wait(dev, NV50_PDISPLAY_INTR_1, mask, mask)) {
  198. NV_ERROR(dev, "timeout: (0x610024 & 0x%08x) == "
  199. "0x%08x\n", mask, mask);
  200. NV_ERROR(dev, "0x610024 = 0x%08x\n",
  201. nv_rd32(dev, NV50_PDISPLAY_INTR_1));
  202. }
  203. }
  204. nv50_evo_fini(dev);
  205. for (i = 0; i < 3; i++) {
  206. if (!nv_wait(dev, NV50_PDISPLAY_SOR_DPMS_STATE(i),
  207. NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
  208. NV_ERROR(dev, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", i);
  209. NV_ERROR(dev, "SOR_DPMS_STATE(%d) = 0x%08x\n", i,
  210. nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_STATE(i)));
  211. }
  212. }
  213. /* disable interrupts. */
  214. nv_wr32(dev, NV50_PDISPLAY_INTR_EN_1, 0x00000000);
  215. /* disable hotplug interrupts */
  216. nv_wr32(dev, 0xe054, 0xffffffff);
  217. nv_wr32(dev, 0xe050, 0x00000000);
  218. if (dev_priv->chipset >= 0x90) {
  219. nv_wr32(dev, 0xe074, 0xffffffff);
  220. nv_wr32(dev, 0xe070, 0x00000000);
  221. }
  222. return 0;
  223. }
  224. int nv50_display_create(struct drm_device *dev)
  225. {
  226. struct drm_nouveau_private *dev_priv = dev->dev_private;
  227. struct dcb_table *dcb = &dev_priv->vbios.dcb;
  228. struct drm_connector *connector, *ct;
  229. int ret, i;
  230. NV_DEBUG_KMS(dev, "\n");
  231. /* init basic kernel modesetting */
  232. drm_mode_config_init(dev);
  233. /* Initialise some optional connector properties. */
  234. drm_mode_create_scaling_mode_property(dev);
  235. drm_mode_create_dithering_property(dev);
  236. dev->mode_config.min_width = 0;
  237. dev->mode_config.min_height = 0;
  238. dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs;
  239. dev->mode_config.max_width = 8192;
  240. dev->mode_config.max_height = 8192;
  241. dev->mode_config.fb_base = dev_priv->fb_phys;
  242. /* Create CRTC objects */
  243. for (i = 0; i < 2; i++)
  244. nv50_crtc_create(dev, i);
  245. /* We setup the encoders from the BIOS table */
  246. for (i = 0 ; i < dcb->entries; i++) {
  247. struct dcb_entry *entry = &dcb->entry[i];
  248. if (entry->location != DCB_LOC_ON_CHIP) {
  249. NV_WARN(dev, "Off-chip encoder %d/%d unsupported\n",
  250. entry->type, ffs(entry->or) - 1);
  251. continue;
  252. }
  253. connector = nouveau_connector_create(dev, entry->connector);
  254. if (IS_ERR(connector))
  255. continue;
  256. switch (entry->type) {
  257. case OUTPUT_TMDS:
  258. case OUTPUT_LVDS:
  259. case OUTPUT_DP:
  260. nv50_sor_create(connector, entry);
  261. break;
  262. case OUTPUT_ANALOG:
  263. nv50_dac_create(connector, entry);
  264. break;
  265. default:
  266. NV_WARN(dev, "DCB encoder %d unknown\n", entry->type);
  267. continue;
  268. }
  269. }
  270. list_for_each_entry_safe(connector, ct,
  271. &dev->mode_config.connector_list, head) {
  272. if (!connector->encoder_ids[0]) {
  273. NV_WARN(dev, "%s has no encoders, removing\n",
  274. drm_get_connector_name(connector));
  275. connector->funcs->destroy(connector);
  276. }
  277. }
  278. ret = nv50_display_init(dev);
  279. if (ret) {
  280. nv50_display_destroy(dev);
  281. return ret;
  282. }
  283. return 0;
  284. }
  285. void
  286. nv50_display_destroy(struct drm_device *dev)
  287. {
  288. NV_DEBUG_KMS(dev, "\n");
  289. drm_mode_config_cleanup(dev);
  290. nv50_display_disable(dev);
  291. }
  292. static u16
  293. nv50_display_script_select(struct drm_device *dev, struct dcb_entry *dcb,
  294. u32 mc, int pxclk)
  295. {
  296. struct drm_nouveau_private *dev_priv = dev->dev_private;
  297. struct nouveau_connector *nv_connector = NULL;
  298. struct drm_encoder *encoder;
  299. struct nvbios *bios = &dev_priv->vbios;
  300. u32 script = 0, or;
  301. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  302. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  303. if (nv_encoder->dcb != dcb)
  304. continue;
  305. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  306. break;
  307. }
  308. or = ffs(dcb->or) - 1;
  309. switch (dcb->type) {
  310. case OUTPUT_LVDS:
  311. script = (mc >> 8) & 0xf;
  312. if (bios->fp_no_ddc) {
  313. if (bios->fp.dual_link)
  314. script |= 0x0100;
  315. if (bios->fp.if_is_24bit)
  316. script |= 0x0200;
  317. } else {
  318. if (pxclk >= bios->fp.duallink_transition_clk) {
  319. script |= 0x0100;
  320. if (bios->fp.strapless_is_24bit & 2)
  321. script |= 0x0200;
  322. } else
  323. if (bios->fp.strapless_is_24bit & 1)
  324. script |= 0x0200;
  325. if (nv_connector && nv_connector->edid &&
  326. (nv_connector->edid->revision >= 4) &&
  327. (nv_connector->edid->input & 0x70) >= 0x20)
  328. script |= 0x0200;
  329. }
  330. if (nouveau_uscript_lvds >= 0) {
  331. NV_INFO(dev, "override script 0x%04x with 0x%04x "
  332. "for output LVDS-%d\n", script,
  333. nouveau_uscript_lvds, or);
  334. script = nouveau_uscript_lvds;
  335. }
  336. break;
  337. case OUTPUT_TMDS:
  338. script = (mc >> 8) & 0xf;
  339. if (pxclk >= 165000)
  340. script |= 0x0100;
  341. if (nouveau_uscript_tmds >= 0) {
  342. NV_INFO(dev, "override script 0x%04x with 0x%04x "
  343. "for output TMDS-%d\n", script,
  344. nouveau_uscript_tmds, or);
  345. script = nouveau_uscript_tmds;
  346. }
  347. break;
  348. case OUTPUT_DP:
  349. script = (mc >> 8) & 0xf;
  350. break;
  351. case OUTPUT_ANALOG:
  352. script = 0xff;
  353. break;
  354. default:
  355. NV_ERROR(dev, "modeset on unsupported output type!\n");
  356. break;
  357. }
  358. return script;
  359. }
  360. static void
  361. nv50_display_vblank_crtc_handler(struct drm_device *dev, int crtc)
  362. {
  363. struct drm_nouveau_private *dev_priv = dev->dev_private;
  364. struct nouveau_channel *chan, *tmp;
  365. list_for_each_entry_safe(chan, tmp, &dev_priv->vbl_waiting,
  366. nvsw.vbl_wait) {
  367. nouveau_bo_wr32(chan->notifier_bo, chan->nvsw.vblsem_offset,
  368. chan->nvsw.vblsem_rval);
  369. list_del(&chan->nvsw.vbl_wait);
  370. drm_vblank_put(dev, crtc);
  371. }
  372. drm_handle_vblank(dev, crtc);
  373. }
  374. static void
  375. nv50_display_vblank_handler(struct drm_device *dev, uint32_t intr)
  376. {
  377. if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_0)
  378. nv50_display_vblank_crtc_handler(dev, 0);
  379. if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_1)
  380. nv50_display_vblank_crtc_handler(dev, 1);
  381. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_VBLANK_CRTC);
  382. }
  383. static void
  384. nv50_display_unk10_handler(struct drm_device *dev)
  385. {
  386. struct drm_nouveau_private *dev_priv = dev->dev_private;
  387. u32 unk30 = nv_rd32(dev, 0x610030), mc;
  388. int i, crtc, or, type = OUTPUT_ANY;
  389. NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
  390. dev_priv->evo_irq.dcb = NULL;
  391. nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) & ~8);
  392. /* Determine which CRTC we're dealing with, only 1 ever will be
  393. * signalled at the same time with the current nouveau code.
  394. */
  395. crtc = ffs((unk30 & 0x00000060) >> 5) - 1;
  396. if (crtc < 0)
  397. goto ack;
  398. /* Nothing needs to be done for the encoder */
  399. crtc = ffs((unk30 & 0x00000180) >> 7) - 1;
  400. if (crtc < 0)
  401. goto ack;
  402. /* Find which encoder was connected to the CRTC */
  403. for (i = 0; type == OUTPUT_ANY && i < 3; i++) {
  404. mc = nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_C(i));
  405. NV_DEBUG_KMS(dev, "DAC-%d mc: 0x%08x\n", i, mc);
  406. if (!(mc & (1 << crtc)))
  407. continue;
  408. switch ((mc & 0x00000f00) >> 8) {
  409. case 0: type = OUTPUT_ANALOG; break;
  410. case 1: type = OUTPUT_TV; break;
  411. default:
  412. NV_ERROR(dev, "invalid mc, DAC-%d: 0x%08x\n", i, mc);
  413. goto ack;
  414. }
  415. or = i;
  416. }
  417. for (i = 0; type == OUTPUT_ANY && i < nv50_sor_nr(dev); i++) {
  418. if (dev_priv->chipset < 0x90 ||
  419. dev_priv->chipset == 0x92 ||
  420. dev_priv->chipset == 0xa0)
  421. mc = nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_C(i));
  422. else
  423. mc = nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_C(i));
  424. NV_DEBUG_KMS(dev, "SOR-%d mc: 0x%08x\n", i, mc);
  425. if (!(mc & (1 << crtc)))
  426. continue;
  427. switch ((mc & 0x00000f00) >> 8) {
  428. case 0: type = OUTPUT_LVDS; break;
  429. case 1: type = OUTPUT_TMDS; break;
  430. case 2: type = OUTPUT_TMDS; break;
  431. case 5: type = OUTPUT_TMDS; break;
  432. case 8: type = OUTPUT_DP; break;
  433. case 9: type = OUTPUT_DP; break;
  434. default:
  435. NV_ERROR(dev, "invalid mc, SOR-%d: 0x%08x\n", i, mc);
  436. goto ack;
  437. }
  438. or = i;
  439. }
  440. /* There was no encoder to disable */
  441. if (type == OUTPUT_ANY)
  442. goto ack;
  443. /* Disable the encoder */
  444. for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
  445. struct dcb_entry *dcb = &dev_priv->vbios.dcb.entry[i];
  446. if (dcb->type == type && (dcb->or & (1 << or))) {
  447. nouveau_bios_run_display_table(dev, dcb, 0, -1);
  448. dev_priv->evo_irq.dcb = dcb;
  449. goto ack;
  450. }
  451. }
  452. NV_ERROR(dev, "no dcb for %d %d 0x%08x\n", or, type, mc);
  453. ack:
  454. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK10);
  455. nv_wr32(dev, 0x610030, 0x80000000);
  456. }
  457. static void
  458. nv50_display_unk20_dp_hack(struct drm_device *dev, struct dcb_entry *dcb)
  459. {
  460. int or = ffs(dcb->or) - 1, link = !(dcb->dpconf.sor.link & 1);
  461. struct drm_encoder *encoder;
  462. uint32_t tmp, unk0 = 0, unk1 = 0;
  463. if (dcb->type != OUTPUT_DP)
  464. return;
  465. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  466. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  467. if (nv_encoder->dcb == dcb) {
  468. unk0 = nv_encoder->dp.unk0;
  469. unk1 = nv_encoder->dp.unk1;
  470. break;
  471. }
  472. }
  473. if (unk0 || unk1) {
  474. tmp = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
  475. tmp &= 0xfffffe03;
  476. nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp | unk0);
  477. tmp = nv_rd32(dev, NV50_SOR_DP_UNK128(or, link));
  478. tmp &= 0xfef080c0;
  479. nv_wr32(dev, NV50_SOR_DP_UNK128(or, link), tmp | unk1);
  480. }
  481. }
  482. static void
  483. nv50_display_unk20_handler(struct drm_device *dev)
  484. {
  485. struct drm_nouveau_private *dev_priv = dev->dev_private;
  486. u32 unk30 = nv_rd32(dev, 0x610030), tmp, pclk, script, mc;
  487. struct dcb_entry *dcb;
  488. int i, crtc, or, type = OUTPUT_ANY;
  489. NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
  490. dcb = dev_priv->evo_irq.dcb;
  491. if (dcb) {
  492. nouveau_bios_run_display_table(dev, dcb, 0, -2);
  493. dev_priv->evo_irq.dcb = NULL;
  494. }
  495. /* CRTC clock change requested? */
  496. crtc = ffs((unk30 & 0x00000600) >> 9) - 1;
  497. if (crtc >= 0) {
  498. pclk = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(crtc, CLOCK));
  499. pclk &= 0x003fffff;
  500. nv50_crtc_set_clock(dev, crtc, pclk);
  501. tmp = nv_rd32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(crtc));
  502. tmp &= ~0x000000f;
  503. nv_wr32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(crtc), tmp);
  504. }
  505. /* Nothing needs to be done for the encoder */
  506. crtc = ffs((unk30 & 0x00000180) >> 7) - 1;
  507. if (crtc < 0)
  508. goto ack;
  509. pclk = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(crtc, CLOCK)) & 0x003fffff;
  510. /* Find which encoder is connected to the CRTC */
  511. for (i = 0; type == OUTPUT_ANY && i < 3; i++) {
  512. mc = nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_P(i));
  513. NV_DEBUG_KMS(dev, "DAC-%d mc: 0x%08x\n", i, mc);
  514. if (!(mc & (1 << crtc)))
  515. continue;
  516. switch ((mc & 0x00000f00) >> 8) {
  517. case 0: type = OUTPUT_ANALOG; break;
  518. case 1: type = OUTPUT_TV; break;
  519. default:
  520. NV_ERROR(dev, "invalid mc, DAC-%d: 0x%08x\n", i, mc);
  521. goto ack;
  522. }
  523. or = i;
  524. }
  525. for (i = 0; type == OUTPUT_ANY && i < nv50_sor_nr(dev); i++) {
  526. if (dev_priv->chipset < 0x90 ||
  527. dev_priv->chipset == 0x92 ||
  528. dev_priv->chipset == 0xa0)
  529. mc = nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_P(i));
  530. else
  531. mc = nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_P(i));
  532. NV_DEBUG_KMS(dev, "SOR-%d mc: 0x%08x\n", i, mc);
  533. if (!(mc & (1 << crtc)))
  534. continue;
  535. switch ((mc & 0x00000f00) >> 8) {
  536. case 0: type = OUTPUT_LVDS; break;
  537. case 1: type = OUTPUT_TMDS; break;
  538. case 2: type = OUTPUT_TMDS; break;
  539. case 5: type = OUTPUT_TMDS; break;
  540. case 8: type = OUTPUT_DP; break;
  541. case 9: type = OUTPUT_DP; break;
  542. default:
  543. NV_ERROR(dev, "invalid mc, SOR-%d: 0x%08x\n", i, mc);
  544. goto ack;
  545. }
  546. or = i;
  547. }
  548. if (type == OUTPUT_ANY)
  549. goto ack;
  550. /* Enable the encoder */
  551. for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
  552. dcb = &dev_priv->vbios.dcb.entry[i];
  553. if (dcb->type == type && (dcb->or & (1 << or)))
  554. break;
  555. }
  556. if (i == dev_priv->vbios.dcb.entries) {
  557. NV_ERROR(dev, "no dcb for %d %d 0x%08x\n", or, type, mc);
  558. goto ack;
  559. }
  560. script = nv50_display_script_select(dev, dcb, mc, pclk);
  561. nouveau_bios_run_display_table(dev, dcb, script, pclk);
  562. nv50_display_unk20_dp_hack(dev, dcb);
  563. if (dcb->type != OUTPUT_ANALOG) {
  564. tmp = nv_rd32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or));
  565. tmp &= ~0x00000f0f;
  566. if (script & 0x0100)
  567. tmp |= 0x00000101;
  568. nv_wr32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or), tmp);
  569. } else {
  570. nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL2(or), 0);
  571. }
  572. dev_priv->evo_irq.dcb = dcb;
  573. dev_priv->evo_irq.pclk = pclk;
  574. dev_priv->evo_irq.script = script;
  575. ack:
  576. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK20);
  577. nv_wr32(dev, 0x610030, 0x80000000);
  578. }
  579. /* If programming a TMDS output on a SOR that can also be configured for
  580. * DisplayPort, make sure NV50_SOR_DP_CTRL_ENABLE is forced off.
  581. *
  582. * It looks like the VBIOS TMDS scripts make an attempt at this, however,
  583. * the VBIOS scripts on at least one board I have only switch it off on
  584. * link 0, causing a blank display if the output has previously been
  585. * programmed for DisplayPort.
  586. */
  587. static void
  588. nv50_display_unk40_dp_set_tmds(struct drm_device *dev, struct dcb_entry *dcb)
  589. {
  590. int or = ffs(dcb->or) - 1, link = !(dcb->dpconf.sor.link & 1);
  591. struct drm_encoder *encoder;
  592. u32 tmp;
  593. if (dcb->type != OUTPUT_TMDS)
  594. return;
  595. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  596. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  597. if (nv_encoder->dcb->type == OUTPUT_DP &&
  598. nv_encoder->dcb->or & (1 << or)) {
  599. tmp = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
  600. tmp &= ~NV50_SOR_DP_CTRL_ENABLED;
  601. nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);
  602. break;
  603. }
  604. }
  605. }
  606. static void
  607. nv50_display_unk40_handler(struct drm_device *dev)
  608. {
  609. struct drm_nouveau_private *dev_priv = dev->dev_private;
  610. struct dcb_entry *dcb = dev_priv->evo_irq.dcb;
  611. u16 script = dev_priv->evo_irq.script;
  612. u32 unk30 = nv_rd32(dev, 0x610030), pclk = dev_priv->evo_irq.pclk;
  613. NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
  614. dev_priv->evo_irq.dcb = NULL;
  615. if (!dcb)
  616. goto ack;
  617. nouveau_bios_run_display_table(dev, dcb, script, -pclk);
  618. nv50_display_unk40_dp_set_tmds(dev, dcb);
  619. ack:
  620. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK40);
  621. nv_wr32(dev, 0x610030, 0x80000000);
  622. nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) | 8);
  623. }
  624. void
  625. nv50_display_irq_handler_bh(struct work_struct *work)
  626. {
  627. struct drm_nouveau_private *dev_priv =
  628. container_of(work, struct drm_nouveau_private, irq_work);
  629. struct drm_device *dev = dev_priv->dev;
  630. for (;;) {
  631. uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
  632. uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
  633. NV_DEBUG_KMS(dev, "PDISPLAY_INTR_BH 0x%08x 0x%08x\n", intr0, intr1);
  634. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK10)
  635. nv50_display_unk10_handler(dev);
  636. else
  637. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK20)
  638. nv50_display_unk20_handler(dev);
  639. else
  640. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK40)
  641. nv50_display_unk40_handler(dev);
  642. else
  643. break;
  644. }
  645. nv_wr32(dev, NV03_PMC_INTR_EN_0, 1);
  646. }
  647. static void
  648. nv50_display_error_handler(struct drm_device *dev)
  649. {
  650. u32 channels = (nv_rd32(dev, NV50_PDISPLAY_INTR_0) & 0x001f0000) >> 16;
  651. u32 addr, data;
  652. int chid;
  653. for (chid = 0; chid < 5; chid++) {
  654. if (!(channels & (1 << chid)))
  655. continue;
  656. nv_wr32(dev, NV50_PDISPLAY_INTR_0, 0x00010000 << chid);
  657. addr = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_ADDR(chid));
  658. data = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_DATA(chid));
  659. NV_ERROR(dev, "EvoCh %d Mthd 0x%04x Data 0x%08x "
  660. "(0x%04x 0x%02x)\n", chid,
  661. addr & 0xffc, data, addr >> 16, (addr >> 12) & 0xf);
  662. nv_wr32(dev, NV50_PDISPLAY_TRAPPED_ADDR(chid), 0x90000000);
  663. }
  664. }
  665. void
  666. nv50_display_irq_hotplug_bh(struct work_struct *work)
  667. {
  668. struct drm_nouveau_private *dev_priv =
  669. container_of(work, struct drm_nouveau_private, hpd_work);
  670. struct drm_device *dev = dev_priv->dev;
  671. struct drm_connector *connector;
  672. const uint32_t gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
  673. uint32_t unplug_mask, plug_mask, change_mask;
  674. uint32_t hpd0, hpd1;
  675. spin_lock_irq(&dev_priv->hpd_state.lock);
  676. hpd0 = dev_priv->hpd_state.hpd0_bits;
  677. dev_priv->hpd_state.hpd0_bits = 0;
  678. hpd1 = dev_priv->hpd_state.hpd1_bits;
  679. dev_priv->hpd_state.hpd1_bits = 0;
  680. spin_unlock_irq(&dev_priv->hpd_state.lock);
  681. hpd0 &= nv_rd32(dev, 0xe050);
  682. if (dev_priv->chipset >= 0x90)
  683. hpd1 &= nv_rd32(dev, 0xe070);
  684. plug_mask = (hpd0 & 0x0000ffff) | (hpd1 << 16);
  685. unplug_mask = (hpd0 >> 16) | (hpd1 & 0xffff0000);
  686. change_mask = plug_mask | unplug_mask;
  687. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  688. struct drm_encoder_helper_funcs *helper;
  689. struct nouveau_connector *nv_connector =
  690. nouveau_connector(connector);
  691. struct nouveau_encoder *nv_encoder;
  692. struct dcb_gpio_entry *gpio;
  693. uint32_t reg;
  694. bool plugged;
  695. if (!nv_connector->dcb)
  696. continue;
  697. gpio = nouveau_bios_gpio_entry(dev, nv_connector->dcb->gpio_tag);
  698. if (!gpio || !(change_mask & (1 << gpio->line)))
  699. continue;
  700. reg = nv_rd32(dev, gpio_reg[gpio->line >> 3]);
  701. plugged = !!(reg & (4 << ((gpio->line & 7) << 2)));
  702. NV_INFO(dev, "%splugged %s\n", plugged ? "" : "un",
  703. drm_get_connector_name(connector)) ;
  704. if (!connector->encoder || !connector->encoder->crtc ||
  705. !connector->encoder->crtc->enabled)
  706. continue;
  707. nv_encoder = nouveau_encoder(connector->encoder);
  708. helper = connector->encoder->helper_private;
  709. if (nv_encoder->dcb->type != OUTPUT_DP)
  710. continue;
  711. if (plugged)
  712. helper->dpms(connector->encoder, DRM_MODE_DPMS_ON);
  713. else
  714. helper->dpms(connector->encoder, DRM_MODE_DPMS_OFF);
  715. }
  716. drm_helper_hpd_irq_event(dev);
  717. }
  718. void
  719. nv50_display_irq_handler(struct drm_device *dev)
  720. {
  721. struct drm_nouveau_private *dev_priv = dev->dev_private;
  722. uint32_t delayed = 0;
  723. if (nv_rd32(dev, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_HOTPLUG) {
  724. uint32_t hpd0_bits, hpd1_bits = 0;
  725. hpd0_bits = nv_rd32(dev, 0xe054);
  726. nv_wr32(dev, 0xe054, hpd0_bits);
  727. if (dev_priv->chipset >= 0x90) {
  728. hpd1_bits = nv_rd32(dev, 0xe074);
  729. nv_wr32(dev, 0xe074, hpd1_bits);
  730. }
  731. spin_lock(&dev_priv->hpd_state.lock);
  732. dev_priv->hpd_state.hpd0_bits |= hpd0_bits;
  733. dev_priv->hpd_state.hpd1_bits |= hpd1_bits;
  734. spin_unlock(&dev_priv->hpd_state.lock);
  735. queue_work(dev_priv->wq, &dev_priv->hpd_work);
  736. }
  737. while (nv_rd32(dev, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_DISPLAY) {
  738. uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
  739. uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
  740. uint32_t clock;
  741. NV_DEBUG_KMS(dev, "PDISPLAY_INTR 0x%08x 0x%08x\n", intr0, intr1);
  742. if (!intr0 && !(intr1 & ~delayed))
  743. break;
  744. if (intr0 & 0x001f0000) {
  745. nv50_display_error_handler(dev);
  746. intr0 &= ~0x001f0000;
  747. }
  748. if (intr1 & NV50_PDISPLAY_INTR_1_VBLANK_CRTC) {
  749. nv50_display_vblank_handler(dev, intr1);
  750. intr1 &= ~NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
  751. }
  752. clock = (intr1 & (NV50_PDISPLAY_INTR_1_CLK_UNK10 |
  753. NV50_PDISPLAY_INTR_1_CLK_UNK20 |
  754. NV50_PDISPLAY_INTR_1_CLK_UNK40));
  755. if (clock) {
  756. nv_wr32(dev, NV03_PMC_INTR_EN_0, 0);
  757. if (!work_pending(&dev_priv->irq_work))
  758. queue_work(dev_priv->wq, &dev_priv->irq_work);
  759. delayed |= clock;
  760. intr1 &= ~clock;
  761. }
  762. if (intr0) {
  763. NV_ERROR(dev, "unknown PDISPLAY_INTR_0: 0x%08x\n", intr0);
  764. nv_wr32(dev, NV50_PDISPLAY_INTR_0, intr0);
  765. }
  766. if (intr1) {
  767. NV_ERROR(dev,
  768. "unknown PDISPLAY_INTR_1: 0x%08x\n", intr1);
  769. nv_wr32(dev, NV50_PDISPLAY_INTR_1, intr1);
  770. }
  771. }
  772. }