nv50_display.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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 "drm_crtc_helper.h"
  32. static void
  33. nv50_evo_channel_del(struct nouveau_channel **pchan)
  34. {
  35. struct nouveau_channel *chan = *pchan;
  36. if (!chan)
  37. return;
  38. *pchan = NULL;
  39. nouveau_gpuobj_channel_takedown(chan);
  40. nouveau_bo_ref(NULL, &chan->pushbuf_bo);
  41. if (chan->user)
  42. iounmap(chan->user);
  43. kfree(chan);
  44. }
  45. static int
  46. nv50_evo_dmaobj_new(struct nouveau_channel *evo, uint32_t class, uint32_t name,
  47. uint32_t tile_flags, uint32_t magic_flags,
  48. uint32_t offset, uint32_t limit)
  49. {
  50. struct drm_nouveau_private *dev_priv = evo->dev->dev_private;
  51. struct drm_device *dev = evo->dev;
  52. struct nouveau_gpuobj *obj = NULL;
  53. int ret;
  54. ret = nouveau_gpuobj_new(dev, evo, 6*4, 32, 0, &obj);
  55. if (ret)
  56. return ret;
  57. obj->engine = NVOBJ_ENGINE_DISPLAY;
  58. ret = nouveau_gpuobj_ref_add(dev, evo, name, obj, NULL);
  59. if (ret) {
  60. nouveau_gpuobj_del(dev, &obj);
  61. return ret;
  62. }
  63. dev_priv->engine.instmem.prepare_access(dev, true);
  64. nv_wo32(dev, obj, 0, (tile_flags << 22) | (magic_flags << 16) | class);
  65. nv_wo32(dev, obj, 1, limit);
  66. nv_wo32(dev, obj, 2, offset);
  67. nv_wo32(dev, obj, 3, 0x00000000);
  68. nv_wo32(dev, obj, 4, 0x00000000);
  69. nv_wo32(dev, obj, 5, 0x00010000);
  70. dev_priv->engine.instmem.finish_access(dev);
  71. return 0;
  72. }
  73. static int
  74. nv50_evo_channel_new(struct drm_device *dev, struct nouveau_channel **pchan)
  75. {
  76. struct drm_nouveau_private *dev_priv = dev->dev_private;
  77. struct nouveau_channel *chan;
  78. int ret;
  79. chan = kzalloc(sizeof(struct nouveau_channel), GFP_KERNEL);
  80. if (!chan)
  81. return -ENOMEM;
  82. *pchan = chan;
  83. chan->id = -1;
  84. chan->dev = dev;
  85. chan->user_get = 4;
  86. chan->user_put = 0;
  87. INIT_LIST_HEAD(&chan->ramht_refs);
  88. ret = nouveau_gpuobj_new_ref(dev, NULL, NULL, 0, 32768, 0x1000,
  89. NVOBJ_FLAG_ZERO_ALLOC, &chan->ramin);
  90. if (ret) {
  91. NV_ERROR(dev, "Error allocating EVO channel memory: %d\n", ret);
  92. nv50_evo_channel_del(pchan);
  93. return ret;
  94. }
  95. ret = nouveau_mem_init_heap(&chan->ramin_heap, chan->ramin->gpuobj->
  96. im_pramin->start, 32768);
  97. if (ret) {
  98. NV_ERROR(dev, "Error initialising EVO PRAMIN heap: %d\n", ret);
  99. nv50_evo_channel_del(pchan);
  100. return ret;
  101. }
  102. ret = nouveau_gpuobj_new_ref(dev, chan, chan, 0, 4096, 16,
  103. 0, &chan->ramht);
  104. if (ret) {
  105. NV_ERROR(dev, "Unable to allocate EVO RAMHT: %d\n", ret);
  106. nv50_evo_channel_del(pchan);
  107. return ret;
  108. }
  109. if (dev_priv->chipset != 0x50) {
  110. ret = nv50_evo_dmaobj_new(chan, 0x3d, NvEvoFB16, 0x70, 0x19,
  111. 0, 0xffffffff);
  112. if (ret) {
  113. nv50_evo_channel_del(pchan);
  114. return ret;
  115. }
  116. ret = nv50_evo_dmaobj_new(chan, 0x3d, NvEvoFB32, 0x7a, 0x19,
  117. 0, 0xffffffff);
  118. if (ret) {
  119. nv50_evo_channel_del(pchan);
  120. return ret;
  121. }
  122. }
  123. ret = nv50_evo_dmaobj_new(chan, 0x3d, NvEvoVRAM, 0, 0x19,
  124. 0, nouveau_mem_fb_amount(dev));
  125. if (ret) {
  126. nv50_evo_channel_del(pchan);
  127. return ret;
  128. }
  129. ret = nouveau_bo_new(dev, NULL, 4096, 0, TTM_PL_FLAG_VRAM, 0, 0,
  130. false, true, &chan->pushbuf_bo);
  131. if (ret == 0)
  132. ret = nouveau_bo_pin(chan->pushbuf_bo, TTM_PL_FLAG_VRAM);
  133. if (ret) {
  134. NV_ERROR(dev, "Error creating EVO DMA push buffer: %d\n", ret);
  135. nv50_evo_channel_del(pchan);
  136. return ret;
  137. }
  138. ret = nouveau_bo_map(chan->pushbuf_bo);
  139. if (ret) {
  140. NV_ERROR(dev, "Error mapping EVO DMA push buffer: %d\n", ret);
  141. nv50_evo_channel_del(pchan);
  142. return ret;
  143. }
  144. chan->user = ioremap(pci_resource_start(dev->pdev, 0) +
  145. NV50_PDISPLAY_USER(0), PAGE_SIZE);
  146. if (!chan->user) {
  147. NV_ERROR(dev, "Error mapping EVO control regs.\n");
  148. nv50_evo_channel_del(pchan);
  149. return -ENOMEM;
  150. }
  151. return 0;
  152. }
  153. int
  154. nv50_display_init(struct drm_device *dev)
  155. {
  156. struct drm_nouveau_private *dev_priv = dev->dev_private;
  157. struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
  158. struct nouveau_channel *evo = dev_priv->evo;
  159. struct drm_connector *connector;
  160. uint32_t val, ram_amount, hpd_en[2];
  161. uint64_t start;
  162. int ret, i;
  163. NV_DEBUG_KMS(dev, "\n");
  164. nv_wr32(dev, 0x00610184, nv_rd32(dev, 0x00614004));
  165. /*
  166. * I think the 0x006101XX range is some kind of main control area
  167. * that enables things.
  168. */
  169. /* CRTC? */
  170. for (i = 0; i < 2; i++) {
  171. val = nv_rd32(dev, 0x00616100 + (i * 0x800));
  172. nv_wr32(dev, 0x00610190 + (i * 0x10), val);
  173. val = nv_rd32(dev, 0x00616104 + (i * 0x800));
  174. nv_wr32(dev, 0x00610194 + (i * 0x10), val);
  175. val = nv_rd32(dev, 0x00616108 + (i * 0x800));
  176. nv_wr32(dev, 0x00610198 + (i * 0x10), val);
  177. val = nv_rd32(dev, 0x0061610c + (i * 0x800));
  178. nv_wr32(dev, 0x0061019c + (i * 0x10), val);
  179. }
  180. /* DAC */
  181. for (i = 0; i < 3; i++) {
  182. val = nv_rd32(dev, 0x0061a000 + (i * 0x800));
  183. nv_wr32(dev, 0x006101d0 + (i * 0x04), val);
  184. }
  185. /* SOR */
  186. for (i = 0; i < 4; i++) {
  187. val = nv_rd32(dev, 0x0061c000 + (i * 0x800));
  188. nv_wr32(dev, 0x006101e0 + (i * 0x04), val);
  189. }
  190. /* Something not yet in use, tv-out maybe. */
  191. for (i = 0; i < 3; i++) {
  192. val = nv_rd32(dev, 0x0061e000 + (i * 0x800));
  193. nv_wr32(dev, 0x006101f0 + (i * 0x04), val);
  194. }
  195. for (i = 0; i < 3; i++) {
  196. nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(i), 0x00550000 |
  197. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
  198. nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL1(i), 0x00000001);
  199. }
  200. /* This used to be in crtc unblank, but seems out of place there. */
  201. nv_wr32(dev, NV50_PDISPLAY_UNK_380, 0);
  202. /* RAM is clamped to 256 MiB. */
  203. ram_amount = nouveau_mem_fb_amount(dev);
  204. NV_DEBUG_KMS(dev, "ram_amount %d\n", ram_amount);
  205. if (ram_amount > 256*1024*1024)
  206. ram_amount = 256*1024*1024;
  207. nv_wr32(dev, NV50_PDISPLAY_RAM_AMOUNT, ram_amount - 1);
  208. nv_wr32(dev, NV50_PDISPLAY_UNK_388, 0x150000);
  209. nv_wr32(dev, NV50_PDISPLAY_UNK_38C, 0);
  210. /* The precise purpose is unknown, i suspect it has something to do
  211. * with text mode.
  212. */
  213. if (nv_rd32(dev, NV50_PDISPLAY_INTR_1) & 0x100) {
  214. nv_wr32(dev, NV50_PDISPLAY_INTR_1, 0x100);
  215. nv_wr32(dev, 0x006194e8, nv_rd32(dev, 0x006194e8) & ~1);
  216. if (!nv_wait(0x006194e8, 2, 0)) {
  217. NV_ERROR(dev, "timeout: (0x6194e8 & 2) != 0\n");
  218. NV_ERROR(dev, "0x6194e8 = 0x%08x\n",
  219. nv_rd32(dev, 0x6194e8));
  220. return -EBUSY;
  221. }
  222. }
  223. /* taken from nv bug #12637, attempts to un-wedge the hw if it's
  224. * stuck in some unspecified state
  225. */
  226. start = ptimer->read(dev);
  227. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_STAT(0), 0x2b00);
  228. while ((val = nv_rd32(dev, NV50_PDISPLAY_CHANNEL_STAT(0))) & 0x1e0000) {
  229. if ((val & 0x9f0000) == 0x20000)
  230. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_STAT(0),
  231. val | 0x800000);
  232. if ((val & 0x3f0000) == 0x30000)
  233. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_STAT(0),
  234. val | 0x200000);
  235. if (ptimer->read(dev) - start > 1000000000ULL) {
  236. NV_ERROR(dev, "timeout: (0x610200 & 0x1e0000) != 0\n");
  237. NV_ERROR(dev, "0x610200 = 0x%08x\n", val);
  238. return -EBUSY;
  239. }
  240. }
  241. nv_wr32(dev, NV50_PDISPLAY_CTRL_STATE, NV50_PDISPLAY_CTRL_STATE_ENABLE);
  242. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_STAT(0), 0x1000b03);
  243. if (!nv_wait(NV50_PDISPLAY_CHANNEL_STAT(0), 0x40000000, 0x40000000)) {
  244. NV_ERROR(dev, "timeout: (0x610200 & 0x40000000) == 0x40000000\n");
  245. NV_ERROR(dev, "0x610200 = 0x%08x\n",
  246. nv_rd32(dev, NV50_PDISPLAY_CHANNEL_STAT(0)));
  247. return -EBUSY;
  248. }
  249. for (i = 0; i < 2; i++) {
  250. nv_wr32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i), 0x2000);
  251. if (!nv_wait(NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
  252. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS, 0)) {
  253. NV_ERROR(dev, "timeout: CURSOR_CTRL2_STATUS == 0\n");
  254. NV_ERROR(dev, "CURSOR_CTRL2 = 0x%08x\n",
  255. nv_rd32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
  256. return -EBUSY;
  257. }
  258. nv_wr32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
  259. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_ON);
  260. if (!nv_wait(NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
  261. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS,
  262. NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS_ACTIVE)) {
  263. NV_ERROR(dev, "timeout: "
  264. "CURSOR_CTRL2_STATUS_ACTIVE(%d)\n", i);
  265. NV_ERROR(dev, "CURSOR_CTRL2(%d) = 0x%08x\n", i,
  266. nv_rd32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
  267. return -EBUSY;
  268. }
  269. }
  270. nv_wr32(dev, NV50_PDISPLAY_OBJECTS, (evo->ramin->instance >> 8) | 9);
  271. /* initialise fifo */
  272. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_DMA_CB(0),
  273. ((evo->pushbuf_bo->bo.mem.mm_node->start << PAGE_SHIFT) >> 8) |
  274. NV50_PDISPLAY_CHANNEL_DMA_CB_LOCATION_VRAM |
  275. NV50_PDISPLAY_CHANNEL_DMA_CB_VALID);
  276. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_UNK2(0), 0x00010000);
  277. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_UNK3(0), 0x00000002);
  278. if (!nv_wait(0x610200, 0x80000000, 0x00000000)) {
  279. NV_ERROR(dev, "timeout: (0x610200 & 0x80000000) == 0\n");
  280. NV_ERROR(dev, "0x610200 = 0x%08x\n", nv_rd32(dev, 0x610200));
  281. return -EBUSY;
  282. }
  283. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_STAT(0),
  284. (nv_rd32(dev, NV50_PDISPLAY_CHANNEL_STAT(0)) & ~0x00000003) |
  285. NV50_PDISPLAY_CHANNEL_STAT_DMA_ENABLED);
  286. nv_wr32(dev, NV50_PDISPLAY_USER_PUT(0), 0);
  287. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_STAT(0), 0x01000003 |
  288. NV50_PDISPLAY_CHANNEL_STAT_DMA_ENABLED);
  289. nv_wr32(dev, 0x610300, nv_rd32(dev, 0x610300) & ~1);
  290. evo->dma.max = (4096/4) - 2;
  291. evo->dma.put = 0;
  292. evo->dma.cur = evo->dma.put;
  293. evo->dma.free = evo->dma.max - evo->dma.cur;
  294. ret = RING_SPACE(evo, NOUVEAU_DMA_SKIPS);
  295. if (ret)
  296. return ret;
  297. for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
  298. OUT_RING(evo, 0);
  299. ret = RING_SPACE(evo, 11);
  300. if (ret)
  301. return ret;
  302. BEGIN_RING(evo, 0, NV50_EVO_UNK84, 2);
  303. OUT_RING(evo, NV50_EVO_UNK84_NOTIFY_DISABLED);
  304. OUT_RING(evo, NV50_EVO_DMA_NOTIFY_HANDLE_NONE);
  305. BEGIN_RING(evo, 0, NV50_EVO_CRTC(0, FB_DMA), 1);
  306. OUT_RING(evo, NV50_EVO_CRTC_FB_DMA_HANDLE_NONE);
  307. BEGIN_RING(evo, 0, NV50_EVO_CRTC(0, UNK0800), 1);
  308. OUT_RING(evo, 0);
  309. BEGIN_RING(evo, 0, NV50_EVO_CRTC(0, DISPLAY_START), 1);
  310. OUT_RING(evo, 0);
  311. BEGIN_RING(evo, 0, NV50_EVO_CRTC(0, UNK082C), 1);
  312. OUT_RING(evo, 0);
  313. FIRE_RING(evo);
  314. if (!nv_wait(0x640004, 0xffffffff, evo->dma.put << 2))
  315. NV_ERROR(dev, "evo pushbuf stalled\n");
  316. /* enable clock change interrupts. */
  317. nv_wr32(dev, 0x610028, 0x00010001);
  318. nv_wr32(dev, NV50_PDISPLAY_INTR_EN, (NV50_PDISPLAY_INTR_EN_CLK_UNK10 |
  319. NV50_PDISPLAY_INTR_EN_CLK_UNK20 |
  320. NV50_PDISPLAY_INTR_EN_CLK_UNK40));
  321. /* enable hotplug interrupts */
  322. hpd_en[0] = hpd_en[1] = 0;
  323. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  324. struct nouveau_connector *conn = nouveau_connector(connector);
  325. struct dcb_gpio_entry *gpio;
  326. if (conn->dcb->gpio_tag == 0xff)
  327. continue;
  328. gpio = nouveau_bios_gpio_entry(dev, conn->dcb->gpio_tag);
  329. if (!gpio)
  330. continue;
  331. hpd_en[gpio->line >> 4] |= (0x00010001 << (gpio->line & 0xf));
  332. }
  333. nv_wr32(dev, 0xe054, 0xffffffff);
  334. nv_wr32(dev, 0xe050, hpd_en[0]);
  335. if (dev_priv->chipset >= 0x90) {
  336. nv_wr32(dev, 0xe074, 0xffffffff);
  337. nv_wr32(dev, 0xe070, hpd_en[1]);
  338. }
  339. return 0;
  340. }
  341. static int nv50_display_disable(struct drm_device *dev)
  342. {
  343. struct drm_nouveau_private *dev_priv = dev->dev_private;
  344. struct drm_crtc *drm_crtc;
  345. int ret, i;
  346. NV_DEBUG_KMS(dev, "\n");
  347. list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
  348. struct nouveau_crtc *crtc = nouveau_crtc(drm_crtc);
  349. nv50_crtc_blank(crtc, true);
  350. }
  351. ret = RING_SPACE(dev_priv->evo, 2);
  352. if (ret == 0) {
  353. BEGIN_RING(dev_priv->evo, 0, NV50_EVO_UPDATE, 1);
  354. OUT_RING(dev_priv->evo, 0);
  355. }
  356. FIRE_RING(dev_priv->evo);
  357. /* Almost like ack'ing a vblank interrupt, maybe in the spirit of
  358. * cleaning up?
  359. */
  360. list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
  361. struct nouveau_crtc *crtc = nouveau_crtc(drm_crtc);
  362. uint32_t mask = NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(crtc->index);
  363. if (!crtc->base.enabled)
  364. continue;
  365. nv_wr32(dev, NV50_PDISPLAY_INTR_1, mask);
  366. if (!nv_wait(NV50_PDISPLAY_INTR_1, mask, mask)) {
  367. NV_ERROR(dev, "timeout: (0x610024 & 0x%08x) == "
  368. "0x%08x\n", mask, mask);
  369. NV_ERROR(dev, "0x610024 = 0x%08x\n",
  370. nv_rd32(dev, NV50_PDISPLAY_INTR_1));
  371. }
  372. }
  373. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_STAT(0), 0);
  374. nv_wr32(dev, NV50_PDISPLAY_CTRL_STATE, 0);
  375. if (!nv_wait(NV50_PDISPLAY_CHANNEL_STAT(0), 0x1e0000, 0)) {
  376. NV_ERROR(dev, "timeout: (0x610200 & 0x1e0000) == 0\n");
  377. NV_ERROR(dev, "0x610200 = 0x%08x\n",
  378. nv_rd32(dev, NV50_PDISPLAY_CHANNEL_STAT(0)));
  379. }
  380. for (i = 0; i < 3; i++) {
  381. if (!nv_wait(NV50_PDISPLAY_SOR_DPMS_STATE(i),
  382. NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
  383. NV_ERROR(dev, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", i);
  384. NV_ERROR(dev, "SOR_DPMS_STATE(%d) = 0x%08x\n", i,
  385. nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_STATE(i)));
  386. }
  387. }
  388. /* disable interrupts. */
  389. nv_wr32(dev, NV50_PDISPLAY_INTR_EN, 0x00000000);
  390. /* disable hotplug interrupts */
  391. nv_wr32(dev, 0xe054, 0xffffffff);
  392. nv_wr32(dev, 0xe050, 0x00000000);
  393. if (dev_priv->chipset >= 0x90) {
  394. nv_wr32(dev, 0xe074, 0xffffffff);
  395. nv_wr32(dev, 0xe070, 0x00000000);
  396. }
  397. return 0;
  398. }
  399. int nv50_display_create(struct drm_device *dev)
  400. {
  401. struct drm_nouveau_private *dev_priv = dev->dev_private;
  402. struct dcb_table *dcb = &dev_priv->vbios.dcb;
  403. int ret, i;
  404. NV_DEBUG_KMS(dev, "\n");
  405. /* init basic kernel modesetting */
  406. drm_mode_config_init(dev);
  407. /* Initialise some optional connector properties. */
  408. drm_mode_create_scaling_mode_property(dev);
  409. drm_mode_create_dithering_property(dev);
  410. dev->mode_config.min_width = 0;
  411. dev->mode_config.min_height = 0;
  412. dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs;
  413. dev->mode_config.max_width = 8192;
  414. dev->mode_config.max_height = 8192;
  415. dev->mode_config.fb_base = dev_priv->fb_phys;
  416. /* Create EVO channel */
  417. ret = nv50_evo_channel_new(dev, &dev_priv->evo);
  418. if (ret) {
  419. NV_ERROR(dev, "Error creating EVO channel: %d\n", ret);
  420. return ret;
  421. }
  422. /* Create CRTC objects */
  423. for (i = 0; i < 2; i++)
  424. nv50_crtc_create(dev, i);
  425. /* We setup the encoders from the BIOS table */
  426. for (i = 0 ; i < dcb->entries; i++) {
  427. struct dcb_entry *entry = &dcb->entry[i];
  428. if (entry->location != DCB_LOC_ON_CHIP) {
  429. NV_WARN(dev, "Off-chip encoder %d/%d unsupported\n",
  430. entry->type, ffs(entry->or) - 1);
  431. continue;
  432. }
  433. switch (entry->type) {
  434. case OUTPUT_TMDS:
  435. case OUTPUT_LVDS:
  436. case OUTPUT_DP:
  437. nv50_sor_create(dev, entry);
  438. break;
  439. case OUTPUT_ANALOG:
  440. nv50_dac_create(dev, entry);
  441. break;
  442. default:
  443. NV_WARN(dev, "DCB encoder %d unknown\n", entry->type);
  444. continue;
  445. }
  446. }
  447. for (i = 0 ; i < dcb->connector.entries; i++) {
  448. if (i != 0 && dcb->connector.entry[i].index ==
  449. dcb->connector.entry[i - 1].index)
  450. continue;
  451. nouveau_connector_create(dev, &dcb->connector.entry[i]);
  452. }
  453. ret = nv50_display_init(dev);
  454. if (ret)
  455. return ret;
  456. return 0;
  457. }
  458. int nv50_display_destroy(struct drm_device *dev)
  459. {
  460. struct drm_nouveau_private *dev_priv = dev->dev_private;
  461. NV_DEBUG_KMS(dev, "\n");
  462. drm_mode_config_cleanup(dev);
  463. nv50_display_disable(dev);
  464. nv50_evo_channel_del(&dev_priv->evo);
  465. return 0;
  466. }
  467. static inline uint32_t
  468. nv50_display_mode_ctrl(struct drm_device *dev, bool sor, int or)
  469. {
  470. struct drm_nouveau_private *dev_priv = dev->dev_private;
  471. uint32_t mc;
  472. if (sor) {
  473. if (dev_priv->chipset < 0x90 ||
  474. dev_priv->chipset == 0x92 || dev_priv->chipset == 0xa0)
  475. mc = nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_P(or));
  476. else
  477. mc = nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_P(or));
  478. } else {
  479. mc = nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_P(or));
  480. }
  481. return mc;
  482. }
  483. static int
  484. nv50_display_irq_head(struct drm_device *dev, int *phead,
  485. struct dcb_entry **pdcbent)
  486. {
  487. struct drm_nouveau_private *dev_priv = dev->dev_private;
  488. uint32_t unk30 = nv_rd32(dev, NV50_PDISPLAY_UNK30_CTRL);
  489. uint32_t dac = 0, sor = 0;
  490. int head, i, or = 0, type = OUTPUT_ANY;
  491. /* We're assuming that head 0 *or* head 1 will be active here,
  492. * and not both. I'm not sure if the hw will even signal both
  493. * ever, but it definitely shouldn't for us as we commit each
  494. * CRTC separately, and submission will be blocked by the GPU
  495. * until we handle each in turn.
  496. */
  497. NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
  498. head = ffs((unk30 >> 9) & 3) - 1;
  499. if (head < 0)
  500. return -EINVAL;
  501. /* This assumes CRTCs are never bound to multiple encoders, which
  502. * should be the case.
  503. */
  504. for (i = 0; i < 3 && type == OUTPUT_ANY; i++) {
  505. uint32_t mc = nv50_display_mode_ctrl(dev, false, i);
  506. if (!(mc & (1 << head)))
  507. continue;
  508. switch ((mc >> 8) & 0xf) {
  509. case 0: type = OUTPUT_ANALOG; break;
  510. case 1: type = OUTPUT_TV; break;
  511. default:
  512. NV_ERROR(dev, "unknown dac mode_ctrl: 0x%08x\n", dac);
  513. return -1;
  514. }
  515. or = i;
  516. }
  517. for (i = 0; i < 4 && type == OUTPUT_ANY; i++) {
  518. uint32_t mc = nv50_display_mode_ctrl(dev, true, i);
  519. if (!(mc & (1 << head)))
  520. continue;
  521. switch ((mc >> 8) & 0xf) {
  522. case 0: type = OUTPUT_LVDS; break;
  523. case 1: type = OUTPUT_TMDS; break;
  524. case 2: type = OUTPUT_TMDS; break;
  525. case 5: type = OUTPUT_TMDS; break;
  526. case 8: type = OUTPUT_DP; break;
  527. case 9: type = OUTPUT_DP; break;
  528. default:
  529. NV_ERROR(dev, "unknown sor mode_ctrl: 0x%08x\n", sor);
  530. return -1;
  531. }
  532. or = i;
  533. }
  534. NV_DEBUG_KMS(dev, "type %d, or %d\n", type, or);
  535. if (type == OUTPUT_ANY) {
  536. NV_ERROR(dev, "unknown encoder!!\n");
  537. return -1;
  538. }
  539. for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
  540. struct dcb_entry *dcbent = &dev_priv->vbios.dcb.entry[i];
  541. if (dcbent->type != type)
  542. continue;
  543. if (!(dcbent->or & (1 << or)))
  544. continue;
  545. *phead = head;
  546. *pdcbent = dcbent;
  547. return 0;
  548. }
  549. NV_ERROR(dev, "no DCB entry for %d %d\n", dac != 0, or);
  550. return 0;
  551. }
  552. static uint32_t
  553. nv50_display_script_select(struct drm_device *dev, struct dcb_entry *dcbent,
  554. int pxclk)
  555. {
  556. struct drm_nouveau_private *dev_priv = dev->dev_private;
  557. struct nouveau_connector *nv_connector = NULL;
  558. struct drm_encoder *encoder;
  559. struct nvbios *bios = &dev_priv->vbios;
  560. uint32_t mc, script = 0, or;
  561. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  562. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  563. if (nv_encoder->dcb != dcbent)
  564. continue;
  565. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  566. break;
  567. }
  568. or = ffs(dcbent->or) - 1;
  569. mc = nv50_display_mode_ctrl(dev, dcbent->type != OUTPUT_ANALOG, or);
  570. switch (dcbent->type) {
  571. case OUTPUT_LVDS:
  572. script = (mc >> 8) & 0xf;
  573. if (bios->fp_no_ddc) {
  574. if (bios->fp.dual_link)
  575. script |= 0x0100;
  576. if (bios->fp.if_is_24bit)
  577. script |= 0x0200;
  578. } else {
  579. if (pxclk >= bios->fp.duallink_transition_clk) {
  580. script |= 0x0100;
  581. if (bios->fp.strapless_is_24bit & 2)
  582. script |= 0x0200;
  583. } else
  584. if (bios->fp.strapless_is_24bit & 1)
  585. script |= 0x0200;
  586. if (nv_connector && nv_connector->edid &&
  587. (nv_connector->edid->revision >= 4) &&
  588. (nv_connector->edid->input & 0x70) >= 0x20)
  589. script |= 0x0200;
  590. }
  591. if (nouveau_uscript_lvds >= 0) {
  592. NV_INFO(dev, "override script 0x%04x with 0x%04x "
  593. "for output LVDS-%d\n", script,
  594. nouveau_uscript_lvds, or);
  595. script = nouveau_uscript_lvds;
  596. }
  597. break;
  598. case OUTPUT_TMDS:
  599. script = (mc >> 8) & 0xf;
  600. if (pxclk >= 165000)
  601. script |= 0x0100;
  602. if (nouveau_uscript_tmds >= 0) {
  603. NV_INFO(dev, "override script 0x%04x with 0x%04x "
  604. "for output TMDS-%d\n", script,
  605. nouveau_uscript_tmds, or);
  606. script = nouveau_uscript_tmds;
  607. }
  608. break;
  609. case OUTPUT_DP:
  610. script = (mc >> 8) & 0xf;
  611. break;
  612. case OUTPUT_ANALOG:
  613. script = 0xff;
  614. break;
  615. default:
  616. NV_ERROR(dev, "modeset on unsupported output type!\n");
  617. break;
  618. }
  619. return script;
  620. }
  621. static void
  622. nv50_display_vblank_crtc_handler(struct drm_device *dev, int crtc)
  623. {
  624. struct drm_nouveau_private *dev_priv = dev->dev_private;
  625. struct nouveau_channel *chan;
  626. struct list_head *entry, *tmp;
  627. list_for_each_safe(entry, tmp, &dev_priv->vbl_waiting) {
  628. chan = list_entry(entry, struct nouveau_channel, nvsw.vbl_wait);
  629. nouveau_bo_wr32(chan->notifier_bo, chan->nvsw.vblsem_offset,
  630. chan->nvsw.vblsem_rval);
  631. list_del(&chan->nvsw.vbl_wait);
  632. }
  633. }
  634. static void
  635. nv50_display_vblank_handler(struct drm_device *dev, uint32_t intr)
  636. {
  637. intr &= NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
  638. if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_0)
  639. nv50_display_vblank_crtc_handler(dev, 0);
  640. if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_1)
  641. nv50_display_vblank_crtc_handler(dev, 1);
  642. nv_wr32(dev, NV50_PDISPLAY_INTR_EN, nv_rd32(dev,
  643. NV50_PDISPLAY_INTR_EN) & ~intr);
  644. nv_wr32(dev, NV50_PDISPLAY_INTR_1, intr);
  645. }
  646. static void
  647. nv50_display_unk10_handler(struct drm_device *dev)
  648. {
  649. struct dcb_entry *dcbent;
  650. int head, ret;
  651. ret = nv50_display_irq_head(dev, &head, &dcbent);
  652. if (ret)
  653. goto ack;
  654. nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) & ~8);
  655. nouveau_bios_run_display_table(dev, dcbent, 0, -1);
  656. ack:
  657. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK10);
  658. nv_wr32(dev, 0x610030, 0x80000000);
  659. }
  660. static void
  661. nv50_display_unk20_handler(struct drm_device *dev)
  662. {
  663. struct dcb_entry *dcbent;
  664. uint32_t tmp, pclk, script;
  665. int head, or, ret;
  666. ret = nv50_display_irq_head(dev, &head, &dcbent);
  667. if (ret)
  668. goto ack;
  669. or = ffs(dcbent->or) - 1;
  670. pclk = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(head, CLOCK)) & 0x3fffff;
  671. script = nv50_display_script_select(dev, dcbent, pclk);
  672. NV_DEBUG_KMS(dev, "head %d pxclk: %dKHz\n", head, pclk);
  673. if (dcbent->type != OUTPUT_DP)
  674. nouveau_bios_run_display_table(dev, dcbent, 0, -2);
  675. nv50_crtc_set_clock(dev, head, pclk);
  676. nouveau_bios_run_display_table(dev, dcbent, script, pclk);
  677. tmp = nv_rd32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(head));
  678. tmp &= ~0x000000f;
  679. nv_wr32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(head), tmp);
  680. if (dcbent->type != OUTPUT_ANALOG) {
  681. tmp = nv_rd32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or));
  682. tmp &= ~0x00000f0f;
  683. if (script & 0x0100)
  684. tmp |= 0x00000101;
  685. nv_wr32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or), tmp);
  686. } else {
  687. nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL2(or), 0);
  688. }
  689. ack:
  690. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK20);
  691. nv_wr32(dev, 0x610030, 0x80000000);
  692. }
  693. static void
  694. nv50_display_unk40_handler(struct drm_device *dev)
  695. {
  696. struct dcb_entry *dcbent;
  697. int head, pclk, script, ret;
  698. ret = nv50_display_irq_head(dev, &head, &dcbent);
  699. if (ret)
  700. goto ack;
  701. pclk = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(head, CLOCK)) & 0x3fffff;
  702. script = nv50_display_script_select(dev, dcbent, pclk);
  703. nouveau_bios_run_display_table(dev, dcbent, script, -pclk);
  704. ack:
  705. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK40);
  706. nv_wr32(dev, 0x610030, 0x80000000);
  707. nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) | 8);
  708. }
  709. void
  710. nv50_display_irq_handler_bh(struct work_struct *work)
  711. {
  712. struct drm_nouveau_private *dev_priv =
  713. container_of(work, struct drm_nouveau_private, irq_work);
  714. struct drm_device *dev = dev_priv->dev;
  715. for (;;) {
  716. uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
  717. uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
  718. NV_DEBUG_KMS(dev, "PDISPLAY_INTR_BH 0x%08x 0x%08x\n", intr0, intr1);
  719. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK10)
  720. nv50_display_unk10_handler(dev);
  721. else
  722. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK20)
  723. nv50_display_unk20_handler(dev);
  724. else
  725. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK40)
  726. nv50_display_unk40_handler(dev);
  727. else
  728. break;
  729. }
  730. nv_wr32(dev, NV03_PMC_INTR_EN_0, 1);
  731. }
  732. static void
  733. nv50_display_error_handler(struct drm_device *dev)
  734. {
  735. uint32_t addr, data;
  736. nv_wr32(dev, NV50_PDISPLAY_INTR_0, 0x00010000);
  737. addr = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_ADDR);
  738. data = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_DATA);
  739. NV_ERROR(dev, "EvoCh %d Mthd 0x%04x Data 0x%08x (0x%04x 0x%02x)\n",
  740. 0, addr & 0xffc, data, addr >> 16, (addr >> 12) & 0xf);
  741. nv_wr32(dev, NV50_PDISPLAY_TRAPPED_ADDR, 0x90000000);
  742. }
  743. static void
  744. nv50_display_irq_hotplug(struct drm_device *dev)
  745. {
  746. struct drm_nouveau_private *dev_priv = dev->dev_private;
  747. struct drm_connector *connector;
  748. const uint32_t gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
  749. uint32_t unplug_mask, plug_mask, change_mask;
  750. uint32_t hpd0, hpd1 = 0;
  751. hpd0 = nv_rd32(dev, 0xe054) & nv_rd32(dev, 0xe050);
  752. if (dev_priv->chipset >= 0x90)
  753. hpd1 = nv_rd32(dev, 0xe074) & nv_rd32(dev, 0xe070);
  754. plug_mask = (hpd0 & 0x0000ffff) | (hpd1 << 16);
  755. unplug_mask = (hpd0 >> 16) | (hpd1 & 0xffff0000);
  756. change_mask = plug_mask | unplug_mask;
  757. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  758. struct drm_encoder_helper_funcs *helper;
  759. struct nouveau_connector *nv_connector =
  760. nouveau_connector(connector);
  761. struct nouveau_encoder *nv_encoder;
  762. struct dcb_gpio_entry *gpio;
  763. uint32_t reg;
  764. bool plugged;
  765. if (!nv_connector->dcb)
  766. continue;
  767. gpio = nouveau_bios_gpio_entry(dev, nv_connector->dcb->gpio_tag);
  768. if (!gpio || !(change_mask & (1 << gpio->line)))
  769. continue;
  770. reg = nv_rd32(dev, gpio_reg[gpio->line >> 3]);
  771. plugged = !!(reg & (4 << ((gpio->line & 7) << 2)));
  772. NV_INFO(dev, "%splugged %s\n", plugged ? "" : "un",
  773. drm_get_connector_name(connector)) ;
  774. if (!connector->encoder || !connector->encoder->crtc ||
  775. !connector->encoder->crtc->enabled)
  776. continue;
  777. nv_encoder = nouveau_encoder(connector->encoder);
  778. helper = connector->encoder->helper_private;
  779. if (nv_encoder->dcb->type != OUTPUT_DP)
  780. continue;
  781. if (plugged)
  782. helper->dpms(connector->encoder, DRM_MODE_DPMS_ON);
  783. else
  784. helper->dpms(connector->encoder, DRM_MODE_DPMS_OFF);
  785. }
  786. nv_wr32(dev, 0xe054, nv_rd32(dev, 0xe054));
  787. if (dev_priv->chipset >= 0x90)
  788. nv_wr32(dev, 0xe074, nv_rd32(dev, 0xe074));
  789. }
  790. void
  791. nv50_display_irq_handler(struct drm_device *dev)
  792. {
  793. struct drm_nouveau_private *dev_priv = dev->dev_private;
  794. uint32_t delayed = 0;
  795. while (nv_rd32(dev, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_HOTPLUG)
  796. nv50_display_irq_hotplug(dev);
  797. while (nv_rd32(dev, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_DISPLAY) {
  798. uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
  799. uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
  800. uint32_t clock;
  801. NV_DEBUG_KMS(dev, "PDISPLAY_INTR 0x%08x 0x%08x\n", intr0, intr1);
  802. if (!intr0 && !(intr1 & ~delayed))
  803. break;
  804. if (intr0 & 0x00010000) {
  805. nv50_display_error_handler(dev);
  806. intr0 &= ~0x00010000;
  807. }
  808. if (intr1 & NV50_PDISPLAY_INTR_1_VBLANK_CRTC) {
  809. nv50_display_vblank_handler(dev, intr1);
  810. intr1 &= ~NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
  811. }
  812. clock = (intr1 & (NV50_PDISPLAY_INTR_1_CLK_UNK10 |
  813. NV50_PDISPLAY_INTR_1_CLK_UNK20 |
  814. NV50_PDISPLAY_INTR_1_CLK_UNK40));
  815. if (clock) {
  816. nv_wr32(dev, NV03_PMC_INTR_EN_0, 0);
  817. if (!work_pending(&dev_priv->irq_work))
  818. queue_work(dev_priv->wq, &dev_priv->irq_work);
  819. delayed |= clock;
  820. intr1 &= ~clock;
  821. }
  822. if (intr0) {
  823. NV_ERROR(dev, "unknown PDISPLAY_INTR_0: 0x%08x\n", intr0);
  824. nv_wr32(dev, NV50_PDISPLAY_INTR_0, intr0);
  825. }
  826. if (intr1) {
  827. NV_ERROR(dev,
  828. "unknown PDISPLAY_INTR_1: 0x%08x\n", intr1);
  829. nv_wr32(dev, NV50_PDISPLAY_INTR_1, intr1);
  830. }
  831. }
  832. }