nv50_display.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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, dev_priv->vram_size);
  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 = dev_priv->vram_size;
  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].index2 ==
  449. dcb->connector.entry[i - 1].index2)
  450. continue;
  451. nouveau_connector_create(dev, &dcb->connector.entry[i]);
  452. }
  453. ret = nv50_display_init(dev);
  454. if (ret) {
  455. nv50_display_destroy(dev);
  456. return ret;
  457. }
  458. return 0;
  459. }
  460. int nv50_display_destroy(struct drm_device *dev)
  461. {
  462. struct drm_nouveau_private *dev_priv = dev->dev_private;
  463. NV_DEBUG_KMS(dev, "\n");
  464. drm_mode_config_cleanup(dev);
  465. nv50_display_disable(dev);
  466. nv50_evo_channel_del(&dev_priv->evo);
  467. return 0;
  468. }
  469. static inline uint32_t
  470. nv50_display_mode_ctrl(struct drm_device *dev, bool sor, int or)
  471. {
  472. struct drm_nouveau_private *dev_priv = dev->dev_private;
  473. uint32_t mc;
  474. if (sor) {
  475. if (dev_priv->chipset < 0x90 ||
  476. dev_priv->chipset == 0x92 || dev_priv->chipset == 0xa0)
  477. mc = nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_P(or));
  478. else
  479. mc = nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_P(or));
  480. } else {
  481. mc = nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_P(or));
  482. }
  483. return mc;
  484. }
  485. static int
  486. nv50_display_irq_head(struct drm_device *dev, int *phead,
  487. struct dcb_entry **pdcbent)
  488. {
  489. struct drm_nouveau_private *dev_priv = dev->dev_private;
  490. uint32_t unk30 = nv_rd32(dev, NV50_PDISPLAY_UNK30_CTRL);
  491. uint32_t dac = 0, sor = 0;
  492. int head, i, or = 0, type = OUTPUT_ANY;
  493. /* We're assuming that head 0 *or* head 1 will be active here,
  494. * and not both. I'm not sure if the hw will even signal both
  495. * ever, but it definitely shouldn't for us as we commit each
  496. * CRTC separately, and submission will be blocked by the GPU
  497. * until we handle each in turn.
  498. */
  499. NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
  500. head = ffs((unk30 >> 9) & 3) - 1;
  501. if (head < 0)
  502. return -EINVAL;
  503. /* This assumes CRTCs are never bound to multiple encoders, which
  504. * should be the case.
  505. */
  506. for (i = 0; i < 3 && type == OUTPUT_ANY; i++) {
  507. uint32_t mc = nv50_display_mode_ctrl(dev, false, i);
  508. if (!(mc & (1 << head)))
  509. continue;
  510. switch ((mc >> 8) & 0xf) {
  511. case 0: type = OUTPUT_ANALOG; break;
  512. case 1: type = OUTPUT_TV; break;
  513. default:
  514. NV_ERROR(dev, "unknown dac mode_ctrl: 0x%08x\n", dac);
  515. return -1;
  516. }
  517. or = i;
  518. }
  519. for (i = 0; i < 4 && type == OUTPUT_ANY; i++) {
  520. uint32_t mc = nv50_display_mode_ctrl(dev, true, i);
  521. if (!(mc & (1 << head)))
  522. continue;
  523. switch ((mc >> 8) & 0xf) {
  524. case 0: type = OUTPUT_LVDS; break;
  525. case 1: type = OUTPUT_TMDS; break;
  526. case 2: type = OUTPUT_TMDS; break;
  527. case 5: type = OUTPUT_TMDS; break;
  528. case 8: type = OUTPUT_DP; break;
  529. case 9: type = OUTPUT_DP; break;
  530. default:
  531. NV_ERROR(dev, "unknown sor mode_ctrl: 0x%08x\n", sor);
  532. return -1;
  533. }
  534. or = i;
  535. }
  536. NV_DEBUG_KMS(dev, "type %d, or %d\n", type, or);
  537. if (type == OUTPUT_ANY) {
  538. NV_ERROR(dev, "unknown encoder!!\n");
  539. return -1;
  540. }
  541. for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
  542. struct dcb_entry *dcbent = &dev_priv->vbios.dcb.entry[i];
  543. if (dcbent->type != type)
  544. continue;
  545. if (!(dcbent->or & (1 << or)))
  546. continue;
  547. *phead = head;
  548. *pdcbent = dcbent;
  549. return 0;
  550. }
  551. NV_ERROR(dev, "no DCB entry for %d %d\n", dac != 0, or);
  552. return 0;
  553. }
  554. static uint32_t
  555. nv50_display_script_select(struct drm_device *dev, struct dcb_entry *dcbent,
  556. int pxclk)
  557. {
  558. struct drm_nouveau_private *dev_priv = dev->dev_private;
  559. struct nouveau_connector *nv_connector = NULL;
  560. struct drm_encoder *encoder;
  561. struct nvbios *bios = &dev_priv->vbios;
  562. uint32_t mc, script = 0, or;
  563. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  564. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  565. if (nv_encoder->dcb != dcbent)
  566. continue;
  567. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  568. break;
  569. }
  570. or = ffs(dcbent->or) - 1;
  571. mc = nv50_display_mode_ctrl(dev, dcbent->type != OUTPUT_ANALOG, or);
  572. switch (dcbent->type) {
  573. case OUTPUT_LVDS:
  574. script = (mc >> 8) & 0xf;
  575. if (bios->fp_no_ddc) {
  576. if (bios->fp.dual_link)
  577. script |= 0x0100;
  578. if (bios->fp.if_is_24bit)
  579. script |= 0x0200;
  580. } else {
  581. if (pxclk >= bios->fp.duallink_transition_clk) {
  582. script |= 0x0100;
  583. if (bios->fp.strapless_is_24bit & 2)
  584. script |= 0x0200;
  585. } else
  586. if (bios->fp.strapless_is_24bit & 1)
  587. script |= 0x0200;
  588. if (nv_connector && nv_connector->edid &&
  589. (nv_connector->edid->revision >= 4) &&
  590. (nv_connector->edid->input & 0x70) >= 0x20)
  591. script |= 0x0200;
  592. }
  593. if (nouveau_uscript_lvds >= 0) {
  594. NV_INFO(dev, "override script 0x%04x with 0x%04x "
  595. "for output LVDS-%d\n", script,
  596. nouveau_uscript_lvds, or);
  597. script = nouveau_uscript_lvds;
  598. }
  599. break;
  600. case OUTPUT_TMDS:
  601. script = (mc >> 8) & 0xf;
  602. if (pxclk >= 165000)
  603. script |= 0x0100;
  604. if (nouveau_uscript_tmds >= 0) {
  605. NV_INFO(dev, "override script 0x%04x with 0x%04x "
  606. "for output TMDS-%d\n", script,
  607. nouveau_uscript_tmds, or);
  608. script = nouveau_uscript_tmds;
  609. }
  610. break;
  611. case OUTPUT_DP:
  612. script = (mc >> 8) & 0xf;
  613. break;
  614. case OUTPUT_ANALOG:
  615. script = 0xff;
  616. break;
  617. default:
  618. NV_ERROR(dev, "modeset on unsupported output type!\n");
  619. break;
  620. }
  621. return script;
  622. }
  623. static void
  624. nv50_display_vblank_crtc_handler(struct drm_device *dev, int crtc)
  625. {
  626. struct drm_nouveau_private *dev_priv = dev->dev_private;
  627. struct nouveau_channel *chan;
  628. struct list_head *entry, *tmp;
  629. list_for_each_safe(entry, tmp, &dev_priv->vbl_waiting) {
  630. chan = list_entry(entry, struct nouveau_channel, nvsw.vbl_wait);
  631. nouveau_bo_wr32(chan->notifier_bo, chan->nvsw.vblsem_offset,
  632. chan->nvsw.vblsem_rval);
  633. list_del(&chan->nvsw.vbl_wait);
  634. }
  635. }
  636. static void
  637. nv50_display_vblank_handler(struct drm_device *dev, uint32_t intr)
  638. {
  639. intr &= NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
  640. if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_0)
  641. nv50_display_vblank_crtc_handler(dev, 0);
  642. if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_1)
  643. nv50_display_vblank_crtc_handler(dev, 1);
  644. nv_wr32(dev, NV50_PDISPLAY_INTR_EN, nv_rd32(dev,
  645. NV50_PDISPLAY_INTR_EN) & ~intr);
  646. nv_wr32(dev, NV50_PDISPLAY_INTR_1, intr);
  647. }
  648. static void
  649. nv50_display_unk10_handler(struct drm_device *dev)
  650. {
  651. struct dcb_entry *dcbent;
  652. int head, ret;
  653. ret = nv50_display_irq_head(dev, &head, &dcbent);
  654. if (ret)
  655. goto ack;
  656. nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) & ~8);
  657. nouveau_bios_run_display_table(dev, dcbent, 0, -1);
  658. ack:
  659. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK10);
  660. nv_wr32(dev, 0x610030, 0x80000000);
  661. }
  662. static void
  663. nv50_display_unk20_handler(struct drm_device *dev)
  664. {
  665. struct dcb_entry *dcbent;
  666. uint32_t tmp, pclk, script;
  667. int head, or, ret;
  668. ret = nv50_display_irq_head(dev, &head, &dcbent);
  669. if (ret)
  670. goto ack;
  671. or = ffs(dcbent->or) - 1;
  672. pclk = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(head, CLOCK)) & 0x3fffff;
  673. script = nv50_display_script_select(dev, dcbent, pclk);
  674. NV_DEBUG_KMS(dev, "head %d pxclk: %dKHz\n", head, pclk);
  675. if (dcbent->type != OUTPUT_DP)
  676. nouveau_bios_run_display_table(dev, dcbent, 0, -2);
  677. nv50_crtc_set_clock(dev, head, pclk);
  678. nouveau_bios_run_display_table(dev, dcbent, script, pclk);
  679. tmp = nv_rd32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(head));
  680. tmp &= ~0x000000f;
  681. nv_wr32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(head), tmp);
  682. if (dcbent->type != OUTPUT_ANALOG) {
  683. tmp = nv_rd32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or));
  684. tmp &= ~0x00000f0f;
  685. if (script & 0x0100)
  686. tmp |= 0x00000101;
  687. nv_wr32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or), tmp);
  688. } else {
  689. nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL2(or), 0);
  690. }
  691. ack:
  692. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK20);
  693. nv_wr32(dev, 0x610030, 0x80000000);
  694. }
  695. static void
  696. nv50_display_unk40_handler(struct drm_device *dev)
  697. {
  698. struct dcb_entry *dcbent;
  699. int head, pclk, script, ret;
  700. ret = nv50_display_irq_head(dev, &head, &dcbent);
  701. if (ret)
  702. goto ack;
  703. pclk = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(head, CLOCK)) & 0x3fffff;
  704. script = nv50_display_script_select(dev, dcbent, pclk);
  705. nouveau_bios_run_display_table(dev, dcbent, script, -pclk);
  706. ack:
  707. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK40);
  708. nv_wr32(dev, 0x610030, 0x80000000);
  709. nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) | 8);
  710. }
  711. void
  712. nv50_display_irq_handler_bh(struct work_struct *work)
  713. {
  714. struct drm_nouveau_private *dev_priv =
  715. container_of(work, struct drm_nouveau_private, irq_work);
  716. struct drm_device *dev = dev_priv->dev;
  717. for (;;) {
  718. uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
  719. uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
  720. NV_DEBUG_KMS(dev, "PDISPLAY_INTR_BH 0x%08x 0x%08x\n", intr0, intr1);
  721. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK10)
  722. nv50_display_unk10_handler(dev);
  723. else
  724. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK20)
  725. nv50_display_unk20_handler(dev);
  726. else
  727. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK40)
  728. nv50_display_unk40_handler(dev);
  729. else
  730. break;
  731. }
  732. nv_wr32(dev, NV03_PMC_INTR_EN_0, 1);
  733. }
  734. static void
  735. nv50_display_error_handler(struct drm_device *dev)
  736. {
  737. uint32_t addr, data;
  738. nv_wr32(dev, NV50_PDISPLAY_INTR_0, 0x00010000);
  739. addr = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_ADDR);
  740. data = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_DATA);
  741. NV_ERROR(dev, "EvoCh %d Mthd 0x%04x Data 0x%08x (0x%04x 0x%02x)\n",
  742. 0, addr & 0xffc, data, addr >> 16, (addr >> 12) & 0xf);
  743. nv_wr32(dev, NV50_PDISPLAY_TRAPPED_ADDR, 0x90000000);
  744. }
  745. void
  746. nv50_display_irq_hotplug_bh(struct work_struct *work)
  747. {
  748. struct drm_nouveau_private *dev_priv =
  749. container_of(work, struct drm_nouveau_private, hpd_work);
  750. struct drm_device *dev = dev_priv->dev;
  751. struct drm_connector *connector;
  752. const uint32_t gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
  753. uint32_t unplug_mask, plug_mask, change_mask;
  754. uint32_t hpd0, hpd1 = 0;
  755. hpd0 = nv_rd32(dev, 0xe054) & nv_rd32(dev, 0xe050);
  756. if (dev_priv->chipset >= 0x90)
  757. hpd1 = nv_rd32(dev, 0xe074) & nv_rd32(dev, 0xe070);
  758. plug_mask = (hpd0 & 0x0000ffff) | (hpd1 << 16);
  759. unplug_mask = (hpd0 >> 16) | (hpd1 & 0xffff0000);
  760. change_mask = plug_mask | unplug_mask;
  761. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  762. struct drm_encoder_helper_funcs *helper;
  763. struct nouveau_connector *nv_connector =
  764. nouveau_connector(connector);
  765. struct nouveau_encoder *nv_encoder;
  766. struct dcb_gpio_entry *gpio;
  767. uint32_t reg;
  768. bool plugged;
  769. if (!nv_connector->dcb)
  770. continue;
  771. gpio = nouveau_bios_gpio_entry(dev, nv_connector->dcb->gpio_tag);
  772. if (!gpio || !(change_mask & (1 << gpio->line)))
  773. continue;
  774. reg = nv_rd32(dev, gpio_reg[gpio->line >> 3]);
  775. plugged = !!(reg & (4 << ((gpio->line & 7) << 2)));
  776. NV_INFO(dev, "%splugged %s\n", plugged ? "" : "un",
  777. drm_get_connector_name(connector)) ;
  778. if (!connector->encoder || !connector->encoder->crtc ||
  779. !connector->encoder->crtc->enabled)
  780. continue;
  781. nv_encoder = nouveau_encoder(connector->encoder);
  782. helper = connector->encoder->helper_private;
  783. if (nv_encoder->dcb->type != OUTPUT_DP)
  784. continue;
  785. if (plugged)
  786. helper->dpms(connector->encoder, DRM_MODE_DPMS_ON);
  787. else
  788. helper->dpms(connector->encoder, DRM_MODE_DPMS_OFF);
  789. }
  790. nv_wr32(dev, 0xe054, nv_rd32(dev, 0xe054));
  791. if (dev_priv->chipset >= 0x90)
  792. nv_wr32(dev, 0xe074, nv_rd32(dev, 0xe074));
  793. }
  794. void
  795. nv50_display_irq_handler(struct drm_device *dev)
  796. {
  797. struct drm_nouveau_private *dev_priv = dev->dev_private;
  798. uint32_t delayed = 0;
  799. if (nv_rd32(dev, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_HOTPLUG) {
  800. if (!work_pending(&dev_priv->hpd_work))
  801. queue_work(dev_priv->wq, &dev_priv->hpd_work);
  802. }
  803. while (nv_rd32(dev, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_DISPLAY) {
  804. uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
  805. uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
  806. uint32_t clock;
  807. NV_DEBUG_KMS(dev, "PDISPLAY_INTR 0x%08x 0x%08x\n", intr0, intr1);
  808. if (!intr0 && !(intr1 & ~delayed))
  809. break;
  810. if (intr0 & 0x00010000) {
  811. nv50_display_error_handler(dev);
  812. intr0 &= ~0x00010000;
  813. }
  814. if (intr1 & NV50_PDISPLAY_INTR_1_VBLANK_CRTC) {
  815. nv50_display_vblank_handler(dev, intr1);
  816. intr1 &= ~NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
  817. }
  818. clock = (intr1 & (NV50_PDISPLAY_INTR_1_CLK_UNK10 |
  819. NV50_PDISPLAY_INTR_1_CLK_UNK20 |
  820. NV50_PDISPLAY_INTR_1_CLK_UNK40));
  821. if (clock) {
  822. nv_wr32(dev, NV03_PMC_INTR_EN_0, 0);
  823. if (!work_pending(&dev_priv->irq_work))
  824. queue_work(dev_priv->wq, &dev_priv->irq_work);
  825. delayed |= clock;
  826. intr1 &= ~clock;
  827. }
  828. if (intr0) {
  829. NV_ERROR(dev, "unknown PDISPLAY_INTR_0: 0x%08x\n", intr0);
  830. nv_wr32(dev, NV50_PDISPLAY_INTR_0, intr0);
  831. }
  832. if (intr1) {
  833. NV_ERROR(dev,
  834. "unknown PDISPLAY_INTR_1: 0x%08x\n", intr1);
  835. nv_wr32(dev, NV50_PDISPLAY_INTR_1, intr1);
  836. }
  837. }
  838. }