nv50_display.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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 (connector->connector_type != DRM_MODE_CONNECTOR_DVII &&
  327. connector->connector_type != DRM_MODE_CONNECTOR_DVID &&
  328. connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
  329. continue;
  330. gpio = nouveau_bios_gpio_entry(dev, conn->dcb->gpio_tag);
  331. if (!gpio)
  332. continue;
  333. hpd_en[gpio->line >> 4] |= (0x00010001 << (gpio->line & 0xf));
  334. }
  335. nv_wr32(dev, 0xe054, 0xffffffff);
  336. nv_wr32(dev, 0xe050, hpd_en[0]);
  337. if (dev_priv->chipset >= 0x90) {
  338. nv_wr32(dev, 0xe074, 0xffffffff);
  339. nv_wr32(dev, 0xe070, hpd_en[1]);
  340. }
  341. return 0;
  342. }
  343. static int nv50_display_disable(struct drm_device *dev)
  344. {
  345. struct drm_nouveau_private *dev_priv = dev->dev_private;
  346. struct drm_crtc *drm_crtc;
  347. int ret, i;
  348. NV_DEBUG_KMS(dev, "\n");
  349. list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
  350. struct nouveau_crtc *crtc = nouveau_crtc(drm_crtc);
  351. nv50_crtc_blank(crtc, true);
  352. }
  353. ret = RING_SPACE(dev_priv->evo, 2);
  354. if (ret == 0) {
  355. BEGIN_RING(dev_priv->evo, 0, NV50_EVO_UPDATE, 1);
  356. OUT_RING(dev_priv->evo, 0);
  357. }
  358. FIRE_RING(dev_priv->evo);
  359. /* Almost like ack'ing a vblank interrupt, maybe in the spirit of
  360. * cleaning up?
  361. */
  362. list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
  363. struct nouveau_crtc *crtc = nouveau_crtc(drm_crtc);
  364. uint32_t mask = NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(crtc->index);
  365. if (!crtc->base.enabled)
  366. continue;
  367. nv_wr32(dev, NV50_PDISPLAY_INTR_1, mask);
  368. if (!nv_wait(NV50_PDISPLAY_INTR_1, mask, mask)) {
  369. NV_ERROR(dev, "timeout: (0x610024 & 0x%08x) == "
  370. "0x%08x\n", mask, mask);
  371. NV_ERROR(dev, "0x610024 = 0x%08x\n",
  372. nv_rd32(dev, NV50_PDISPLAY_INTR_1));
  373. }
  374. }
  375. nv_wr32(dev, NV50_PDISPLAY_CHANNEL_STAT(0), 0);
  376. nv_wr32(dev, NV50_PDISPLAY_CTRL_STATE, 0);
  377. if (!nv_wait(NV50_PDISPLAY_CHANNEL_STAT(0), 0x1e0000, 0)) {
  378. NV_ERROR(dev, "timeout: (0x610200 & 0x1e0000) == 0\n");
  379. NV_ERROR(dev, "0x610200 = 0x%08x\n",
  380. nv_rd32(dev, NV50_PDISPLAY_CHANNEL_STAT(0)));
  381. }
  382. for (i = 0; i < 3; i++) {
  383. if (!nv_wait(NV50_PDISPLAY_SOR_DPMS_STATE(i),
  384. NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
  385. NV_ERROR(dev, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", i);
  386. NV_ERROR(dev, "SOR_DPMS_STATE(%d) = 0x%08x\n", i,
  387. nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_STATE(i)));
  388. }
  389. }
  390. /* disable interrupts. */
  391. nv_wr32(dev, NV50_PDISPLAY_INTR_EN, 0x00000000);
  392. /* disable hotplug interrupts */
  393. nv_wr32(dev, 0xe054, 0xffffffff);
  394. nv_wr32(dev, 0xe050, 0x00000000);
  395. if (dev_priv->chipset >= 0x90) {
  396. nv_wr32(dev, 0xe074, 0xffffffff);
  397. nv_wr32(dev, 0xe070, 0x00000000);
  398. }
  399. return 0;
  400. }
  401. int nv50_display_create(struct drm_device *dev)
  402. {
  403. struct drm_nouveau_private *dev_priv = dev->dev_private;
  404. struct parsed_dcb *dcb = dev_priv->vbios->dcb;
  405. uint32_t connector[16] = {};
  406. int ret, i;
  407. NV_DEBUG_KMS(dev, "\n");
  408. /* init basic kernel modesetting */
  409. drm_mode_config_init(dev);
  410. /* Initialise some optional connector properties. */
  411. drm_mode_create_scaling_mode_property(dev);
  412. drm_mode_create_dithering_property(dev);
  413. dev->mode_config.min_width = 0;
  414. dev->mode_config.min_height = 0;
  415. dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs;
  416. dev->mode_config.max_width = 8192;
  417. dev->mode_config.max_height = 8192;
  418. dev->mode_config.fb_base = dev_priv->fb_phys;
  419. /* Create EVO channel */
  420. ret = nv50_evo_channel_new(dev, &dev_priv->evo);
  421. if (ret) {
  422. NV_ERROR(dev, "Error creating EVO channel: %d\n", ret);
  423. return ret;
  424. }
  425. /* Create CRTC objects */
  426. for (i = 0; i < 2; i++)
  427. nv50_crtc_create(dev, i);
  428. /* We setup the encoders from the BIOS table */
  429. for (i = 0 ; i < dcb->entries; i++) {
  430. struct dcb_entry *entry = &dcb->entry[i];
  431. if (entry->location != DCB_LOC_ON_CHIP) {
  432. NV_WARN(dev, "Off-chip encoder %d/%d unsupported\n",
  433. entry->type, ffs(entry->or) - 1);
  434. continue;
  435. }
  436. switch (entry->type) {
  437. case OUTPUT_TMDS:
  438. case OUTPUT_LVDS:
  439. case OUTPUT_DP:
  440. nv50_sor_create(dev, entry);
  441. break;
  442. case OUTPUT_ANALOG:
  443. nv50_dac_create(dev, entry);
  444. break;
  445. default:
  446. NV_WARN(dev, "DCB encoder %d unknown\n", entry->type);
  447. continue;
  448. }
  449. connector[entry->connector] |= (1 << entry->type);
  450. }
  451. /* It appears that DCB 3.0+ VBIOS has a connector table, however,
  452. * I'm not 100% certain how to decode it correctly yet so just
  453. * look at what encoders are present on each connector index and
  454. * attempt to derive the connector type from that.
  455. */
  456. for (i = 0 ; i < dcb->entries; i++) {
  457. struct dcb_entry *entry = &dcb->entry[i];
  458. uint16_t encoders;
  459. int type;
  460. encoders = connector[entry->connector];
  461. if (!(encoders & (1 << entry->type)))
  462. continue;
  463. connector[entry->connector] = 0;
  464. if (encoders & (1 << OUTPUT_DP)) {
  465. type = DRM_MODE_CONNECTOR_DisplayPort;
  466. } else if (encoders & (1 << OUTPUT_TMDS)) {
  467. if (encoders & (1 << OUTPUT_ANALOG))
  468. type = DRM_MODE_CONNECTOR_DVII;
  469. else
  470. type = DRM_MODE_CONNECTOR_DVID;
  471. } else if (encoders & (1 << OUTPUT_ANALOG)) {
  472. type = DRM_MODE_CONNECTOR_VGA;
  473. } else if (encoders & (1 << OUTPUT_LVDS)) {
  474. type = DRM_MODE_CONNECTOR_LVDS;
  475. } else {
  476. type = DRM_MODE_CONNECTOR_Unknown;
  477. }
  478. if (type == DRM_MODE_CONNECTOR_Unknown)
  479. continue;
  480. nouveau_connector_create(dev, entry->connector, type);
  481. }
  482. ret = nv50_display_init(dev);
  483. if (ret)
  484. return ret;
  485. return 0;
  486. }
  487. int nv50_display_destroy(struct drm_device *dev)
  488. {
  489. struct drm_nouveau_private *dev_priv = dev->dev_private;
  490. NV_DEBUG_KMS(dev, "\n");
  491. drm_mode_config_cleanup(dev);
  492. nv50_display_disable(dev);
  493. nv50_evo_channel_del(&dev_priv->evo);
  494. return 0;
  495. }
  496. static inline uint32_t
  497. nv50_display_mode_ctrl(struct drm_device *dev, bool sor, int or)
  498. {
  499. struct drm_nouveau_private *dev_priv = dev->dev_private;
  500. uint32_t mc;
  501. if (sor) {
  502. if (dev_priv->chipset < 0x90 ||
  503. dev_priv->chipset == 0x92 || dev_priv->chipset == 0xa0)
  504. mc = nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_P(or));
  505. else
  506. mc = nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_P(or));
  507. } else {
  508. mc = nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_P(or));
  509. }
  510. return mc;
  511. }
  512. static int
  513. nv50_display_irq_head(struct drm_device *dev, int *phead,
  514. struct dcb_entry **pdcbent)
  515. {
  516. struct drm_nouveau_private *dev_priv = dev->dev_private;
  517. uint32_t unk30 = nv_rd32(dev, NV50_PDISPLAY_UNK30_CTRL);
  518. uint32_t dac = 0, sor = 0;
  519. int head, i, or = 0, type = OUTPUT_ANY;
  520. /* We're assuming that head 0 *or* head 1 will be active here,
  521. * and not both. I'm not sure if the hw will even signal both
  522. * ever, but it definitely shouldn't for us as we commit each
  523. * CRTC separately, and submission will be blocked by the GPU
  524. * until we handle each in turn.
  525. */
  526. NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
  527. head = ffs((unk30 >> 9) & 3) - 1;
  528. if (head < 0)
  529. return -EINVAL;
  530. /* This assumes CRTCs are never bound to multiple encoders, which
  531. * should be the case.
  532. */
  533. for (i = 0; i < 3 && type == OUTPUT_ANY; i++) {
  534. uint32_t mc = nv50_display_mode_ctrl(dev, false, i);
  535. if (!(mc & (1 << head)))
  536. continue;
  537. switch ((mc >> 8) & 0xf) {
  538. case 0: type = OUTPUT_ANALOG; break;
  539. case 1: type = OUTPUT_TV; break;
  540. default:
  541. NV_ERROR(dev, "unknown dac mode_ctrl: 0x%08x\n", dac);
  542. return -1;
  543. }
  544. or = i;
  545. }
  546. for (i = 0; i < 4 && type == OUTPUT_ANY; i++) {
  547. uint32_t mc = nv50_display_mode_ctrl(dev, true, i);
  548. if (!(mc & (1 << head)))
  549. continue;
  550. switch ((mc >> 8) & 0xf) {
  551. case 0: type = OUTPUT_LVDS; break;
  552. case 1: type = OUTPUT_TMDS; break;
  553. case 2: type = OUTPUT_TMDS; break;
  554. case 5: type = OUTPUT_TMDS; break;
  555. case 8: type = OUTPUT_DP; break;
  556. case 9: type = OUTPUT_DP; break;
  557. default:
  558. NV_ERROR(dev, "unknown sor mode_ctrl: 0x%08x\n", sor);
  559. return -1;
  560. }
  561. or = i;
  562. }
  563. NV_DEBUG_KMS(dev, "type %d, or %d\n", type, or);
  564. if (type == OUTPUT_ANY) {
  565. NV_ERROR(dev, "unknown encoder!!\n");
  566. return -1;
  567. }
  568. for (i = 0; i < dev_priv->vbios->dcb->entries; i++) {
  569. struct dcb_entry *dcbent = &dev_priv->vbios->dcb->entry[i];
  570. if (dcbent->type != type)
  571. continue;
  572. if (!(dcbent->or & (1 << or)))
  573. continue;
  574. *phead = head;
  575. *pdcbent = dcbent;
  576. return 0;
  577. }
  578. NV_ERROR(dev, "no DCB entry for %d %d\n", dac != 0, or);
  579. return 0;
  580. }
  581. static uint32_t
  582. nv50_display_script_select(struct drm_device *dev, struct dcb_entry *dcbent,
  583. int pxclk)
  584. {
  585. struct drm_nouveau_private *dev_priv = dev->dev_private;
  586. struct nouveau_connector *nv_connector = NULL;
  587. struct drm_encoder *encoder;
  588. struct nvbios *bios = &dev_priv->VBIOS;
  589. uint32_t mc, script = 0, or;
  590. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  591. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  592. if (nv_encoder->dcb != dcbent)
  593. continue;
  594. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  595. break;
  596. }
  597. or = ffs(dcbent->or) - 1;
  598. mc = nv50_display_mode_ctrl(dev, dcbent->type != OUTPUT_ANALOG, or);
  599. switch (dcbent->type) {
  600. case OUTPUT_LVDS:
  601. script = (mc >> 8) & 0xf;
  602. if (bios->pub.fp_no_ddc) {
  603. if (bios->fp.dual_link)
  604. script |= 0x0100;
  605. if (bios->fp.if_is_24bit)
  606. script |= 0x0200;
  607. } else {
  608. if (pxclk >= bios->fp.duallink_transition_clk) {
  609. script |= 0x0100;
  610. if (bios->fp.strapless_is_24bit & 2)
  611. script |= 0x0200;
  612. } else
  613. if (bios->fp.strapless_is_24bit & 1)
  614. script |= 0x0200;
  615. if (nv_connector && nv_connector->edid &&
  616. (nv_connector->edid->revision >= 4) &&
  617. (nv_connector->edid->input & 0x70) >= 0x20)
  618. script |= 0x0200;
  619. }
  620. if (nouveau_uscript_lvds >= 0) {
  621. NV_INFO(dev, "override script 0x%04x with 0x%04x "
  622. "for output LVDS-%d\n", script,
  623. nouveau_uscript_lvds, or);
  624. script = nouveau_uscript_lvds;
  625. }
  626. break;
  627. case OUTPUT_TMDS:
  628. script = (mc >> 8) & 0xf;
  629. if (pxclk >= 165000)
  630. script |= 0x0100;
  631. if (nouveau_uscript_tmds >= 0) {
  632. NV_INFO(dev, "override script 0x%04x with 0x%04x "
  633. "for output TMDS-%d\n", script,
  634. nouveau_uscript_tmds, or);
  635. script = nouveau_uscript_tmds;
  636. }
  637. break;
  638. case OUTPUT_DP:
  639. script = (mc >> 8) & 0xf;
  640. break;
  641. case OUTPUT_ANALOG:
  642. script = 0xff;
  643. break;
  644. default:
  645. NV_ERROR(dev, "modeset on unsupported output type!\n");
  646. break;
  647. }
  648. return script;
  649. }
  650. static void
  651. nv50_display_vblank_crtc_handler(struct drm_device *dev, int crtc)
  652. {
  653. struct drm_nouveau_private *dev_priv = dev->dev_private;
  654. struct nouveau_channel *chan;
  655. struct list_head *entry, *tmp;
  656. list_for_each_safe(entry, tmp, &dev_priv->vbl_waiting) {
  657. chan = list_entry(entry, struct nouveau_channel, nvsw.vbl_wait);
  658. nouveau_bo_wr32(chan->notifier_bo, chan->nvsw.vblsem_offset,
  659. chan->nvsw.vblsem_rval);
  660. list_del(&chan->nvsw.vbl_wait);
  661. }
  662. }
  663. static void
  664. nv50_display_vblank_handler(struct drm_device *dev, uint32_t intr)
  665. {
  666. intr &= NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
  667. if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_0)
  668. nv50_display_vblank_crtc_handler(dev, 0);
  669. if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_1)
  670. nv50_display_vblank_crtc_handler(dev, 1);
  671. nv_wr32(dev, NV50_PDISPLAY_INTR_EN, nv_rd32(dev,
  672. NV50_PDISPLAY_INTR_EN) & ~intr);
  673. nv_wr32(dev, NV50_PDISPLAY_INTR_1, intr);
  674. }
  675. static void
  676. nv50_display_unk10_handler(struct drm_device *dev)
  677. {
  678. struct dcb_entry *dcbent;
  679. int head, ret;
  680. ret = nv50_display_irq_head(dev, &head, &dcbent);
  681. if (ret)
  682. goto ack;
  683. nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) & ~8);
  684. nouveau_bios_run_display_table(dev, dcbent, 0, -1);
  685. ack:
  686. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK10);
  687. nv_wr32(dev, 0x610030, 0x80000000);
  688. }
  689. static void
  690. nv50_display_unk20_handler(struct drm_device *dev)
  691. {
  692. struct dcb_entry *dcbent;
  693. uint32_t tmp, pclk, script;
  694. int head, or, ret;
  695. ret = nv50_display_irq_head(dev, &head, &dcbent);
  696. if (ret)
  697. goto ack;
  698. or = ffs(dcbent->or) - 1;
  699. pclk = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(head, CLOCK)) & 0x3fffff;
  700. script = nv50_display_script_select(dev, dcbent, pclk);
  701. NV_DEBUG_KMS(dev, "head %d pxclk: %dKHz\n", head, pclk);
  702. if (dcbent->type != OUTPUT_DP)
  703. nouveau_bios_run_display_table(dev, dcbent, 0, -2);
  704. nv50_crtc_set_clock(dev, head, pclk);
  705. nouveau_bios_run_display_table(dev, dcbent, script, pclk);
  706. tmp = nv_rd32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(head));
  707. tmp &= ~0x000000f;
  708. nv_wr32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(head), tmp);
  709. if (dcbent->type != OUTPUT_ANALOG) {
  710. tmp = nv_rd32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or));
  711. tmp &= ~0x00000f0f;
  712. if (script & 0x0100)
  713. tmp |= 0x00000101;
  714. nv_wr32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or), tmp);
  715. } else {
  716. nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL2(or), 0);
  717. }
  718. ack:
  719. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK20);
  720. nv_wr32(dev, 0x610030, 0x80000000);
  721. }
  722. static void
  723. nv50_display_unk40_handler(struct drm_device *dev)
  724. {
  725. struct dcb_entry *dcbent;
  726. int head, pclk, script, ret;
  727. ret = nv50_display_irq_head(dev, &head, &dcbent);
  728. if (ret)
  729. goto ack;
  730. pclk = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(head, CLOCK)) & 0x3fffff;
  731. script = nv50_display_script_select(dev, dcbent, pclk);
  732. nouveau_bios_run_display_table(dev, dcbent, script, -pclk);
  733. ack:
  734. nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK40);
  735. nv_wr32(dev, 0x610030, 0x80000000);
  736. nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) | 8);
  737. }
  738. void
  739. nv50_display_irq_handler_bh(struct work_struct *work)
  740. {
  741. struct drm_nouveau_private *dev_priv =
  742. container_of(work, struct drm_nouveau_private, irq_work);
  743. struct drm_device *dev = dev_priv->dev;
  744. for (;;) {
  745. uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
  746. uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
  747. NV_DEBUG_KMS(dev, "PDISPLAY_INTR_BH 0x%08x 0x%08x\n", intr0, intr1);
  748. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK10)
  749. nv50_display_unk10_handler(dev);
  750. else
  751. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK20)
  752. nv50_display_unk20_handler(dev);
  753. else
  754. if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK40)
  755. nv50_display_unk40_handler(dev);
  756. else
  757. break;
  758. }
  759. nv_wr32(dev, NV03_PMC_INTR_EN_0, 1);
  760. }
  761. static void
  762. nv50_display_error_handler(struct drm_device *dev)
  763. {
  764. uint32_t addr, data;
  765. nv_wr32(dev, NV50_PDISPLAY_INTR_0, 0x00010000);
  766. addr = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_ADDR);
  767. data = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_DATA);
  768. NV_ERROR(dev, "EvoCh %d Mthd 0x%04x Data 0x%08x (0x%04x 0x%02x)\n",
  769. 0, addr & 0xffc, data, addr >> 16, (addr >> 12) & 0xf);
  770. nv_wr32(dev, NV50_PDISPLAY_TRAPPED_ADDR, 0x90000000);
  771. }
  772. static void
  773. nv50_display_irq_hotplug(struct drm_device *dev)
  774. {
  775. struct drm_nouveau_private *dev_priv = dev->dev_private;
  776. struct drm_connector *connector;
  777. const uint32_t gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
  778. uint32_t unplug_mask, plug_mask, change_mask;
  779. uint32_t hpd0, hpd1 = 0;
  780. hpd0 = nv_rd32(dev, 0xe054) & nv_rd32(dev, 0xe050);
  781. if (dev_priv->chipset >= 0x90)
  782. hpd1 = nv_rd32(dev, 0xe074) & nv_rd32(dev, 0xe070);
  783. plug_mask = (hpd0 & 0x0000ffff) | (hpd1 << 16);
  784. unplug_mask = (hpd0 >> 16) | (hpd1 & 0xffff0000);
  785. change_mask = plug_mask | unplug_mask;
  786. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  787. struct drm_encoder_helper_funcs *helper;
  788. struct nouveau_connector *nv_connector =
  789. nouveau_connector(connector);
  790. struct nouveau_encoder *nv_encoder;
  791. struct dcb_gpio_entry *gpio;
  792. uint32_t reg;
  793. bool plugged;
  794. if (!nv_connector->dcb)
  795. continue;
  796. gpio = nouveau_bios_gpio_entry(dev, nv_connector->dcb->gpio_tag);
  797. if (!gpio || !(change_mask & (1 << gpio->line)))
  798. continue;
  799. reg = nv_rd32(dev, gpio_reg[gpio->line >> 3]);
  800. plugged = !!(reg & (4 << ((gpio->line & 7) << 2)));
  801. NV_INFO(dev, "%splugged %s\n", plugged ? "" : "un",
  802. drm_get_connector_name(connector)) ;
  803. if (!connector->encoder || !connector->encoder->crtc ||
  804. !connector->encoder->crtc->enabled)
  805. continue;
  806. nv_encoder = nouveau_encoder(connector->encoder);
  807. helper = connector->encoder->helper_private;
  808. if (nv_encoder->dcb->type != OUTPUT_DP)
  809. continue;
  810. if (plugged)
  811. helper->dpms(connector->encoder, DRM_MODE_DPMS_ON);
  812. else
  813. helper->dpms(connector->encoder, DRM_MODE_DPMS_OFF);
  814. }
  815. nv_wr32(dev, 0xe054, nv_rd32(dev, 0xe054));
  816. if (dev_priv->chipset >= 0x90)
  817. nv_wr32(dev, 0xe074, nv_rd32(dev, 0xe074));
  818. }
  819. void
  820. nv50_display_irq_handler(struct drm_device *dev)
  821. {
  822. struct drm_nouveau_private *dev_priv = dev->dev_private;
  823. uint32_t delayed = 0;
  824. while (nv_rd32(dev, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_HOTPLUG)
  825. nv50_display_irq_hotplug(dev);
  826. while (nv_rd32(dev, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_DISPLAY) {
  827. uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
  828. uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
  829. uint32_t clock;
  830. NV_DEBUG_KMS(dev, "PDISPLAY_INTR 0x%08x 0x%08x\n", intr0, intr1);
  831. if (!intr0 && !(intr1 & ~delayed))
  832. break;
  833. if (intr0 & 0x00010000) {
  834. nv50_display_error_handler(dev);
  835. intr0 &= ~0x00010000;
  836. }
  837. if (intr1 & NV50_PDISPLAY_INTR_1_VBLANK_CRTC) {
  838. nv50_display_vblank_handler(dev, intr1);
  839. intr1 &= ~NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
  840. }
  841. clock = (intr1 & (NV50_PDISPLAY_INTR_1_CLK_UNK10 |
  842. NV50_PDISPLAY_INTR_1_CLK_UNK20 |
  843. NV50_PDISPLAY_INTR_1_CLK_UNK40));
  844. if (clock) {
  845. nv_wr32(dev, NV03_PMC_INTR_EN_0, 0);
  846. if (!work_pending(&dev_priv->irq_work))
  847. queue_work(dev_priv->wq, &dev_priv->irq_work);
  848. delayed |= clock;
  849. intr1 &= ~clock;
  850. }
  851. if (intr0) {
  852. NV_ERROR(dev, "unknown PDISPLAY_INTR_0: 0x%08x\n", intr0);
  853. nv_wr32(dev, NV50_PDISPLAY_INTR_0, intr0);
  854. }
  855. if (intr1) {
  856. NV_ERROR(dev,
  857. "unknown PDISPLAY_INTR_1: 0x%08x\n", intr1);
  858. nv_wr32(dev, NV50_PDISPLAY_INTR_1, intr1);
  859. }
  860. }
  861. }