nvd0_display.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. /*
  2. * Copyright 2011 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include <linux/dma-mapping.h>
  25. #include <drm/drmP.h>
  26. #include <drm/drm_crtc_helper.h>
  27. #include "nouveau_drm.h"
  28. #include "nouveau_dma.h"
  29. #include "nouveau_gem.h"
  30. #include "nouveau_connector.h"
  31. #include "nouveau_encoder.h"
  32. #include "nouveau_crtc.h"
  33. #include "nouveau_fence.h"
  34. #include "nv50_display.h"
  35. #include <core/client.h>
  36. #include <core/gpuobj.h>
  37. #include <core/class.h>
  38. #include <subdev/timer.h>
  39. #include <subdev/bar.h>
  40. #include <subdev/fb.h>
  41. #define EVO_DMA_NR 9
  42. #define EVO_MASTER (0x00)
  43. #define EVO_FLIP(c) (0x01 + (c))
  44. #define EVO_OVLY(c) (0x05 + (c))
  45. #define EVO_OIMM(c) (0x09 + (c))
  46. #define EVO_CURS(c) (0x0d + (c))
  47. /* offsets in shared sync bo of various structures */
  48. #define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
  49. #define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
  50. #define EVO_FLIP_SEM0(c) EVO_SYNC((c), 0x00)
  51. #define EVO_FLIP_SEM1(c) EVO_SYNC((c), 0x10)
  52. #define EVO_CORE_HANDLE (0xd1500000)
  53. #define EVO_CHAN_HANDLE(t,i) (0xd15c0000 | (((t) & 0x00ff) << 8) | (i))
  54. #define EVO_CHAN_OCLASS(t,c) ((nv_hclass(c) & 0xff00) | ((t) & 0x00ff))
  55. #define EVO_PUSH_HANDLE(t,i) (0xd15b0000 | (i) | \
  56. (((NV50_DISP_##t##_CLASS) & 0x00ff) << 8))
  57. /******************************************************************************
  58. * EVO channel
  59. *****************************************************************************/
  60. struct nvd0_chan {
  61. struct nouveau_object *user;
  62. u32 handle;
  63. };
  64. static int
  65. nvd0_chan_create(struct nouveau_object *core, u32 bclass, u8 head,
  66. void *data, u32 size, struct nvd0_chan *chan)
  67. {
  68. struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
  69. const u32 oclass = EVO_CHAN_OCLASS(bclass, core);
  70. const u32 handle = EVO_CHAN_HANDLE(bclass, head);
  71. int ret;
  72. ret = nouveau_object_new(client, EVO_CORE_HANDLE, handle,
  73. oclass, data, size, &chan->user);
  74. if (ret)
  75. return ret;
  76. chan->handle = handle;
  77. return 0;
  78. }
  79. static void
  80. nvd0_chan_destroy(struct nouveau_object *core, struct nvd0_chan *chan)
  81. {
  82. struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
  83. if (chan->handle)
  84. nouveau_object_del(client, EVO_CORE_HANDLE, chan->handle);
  85. }
  86. /******************************************************************************
  87. * PIO EVO channel
  88. *****************************************************************************/
  89. struct nvd0_pioc {
  90. struct nvd0_chan base;
  91. };
  92. static void
  93. nvd0_pioc_destroy(struct nouveau_object *core, struct nvd0_pioc *pioc)
  94. {
  95. nvd0_chan_destroy(core, &pioc->base);
  96. }
  97. static int
  98. nvd0_pioc_create(struct nouveau_object *core, u32 bclass, u8 head,
  99. void *data, u32 size, struct nvd0_pioc *pioc)
  100. {
  101. return nvd0_chan_create(core, bclass, head, data, size, &pioc->base);
  102. }
  103. /******************************************************************************
  104. * DMA EVO channel
  105. *****************************************************************************/
  106. struct nvd0_dmac {
  107. struct nvd0_chan base;
  108. dma_addr_t handle;
  109. u32 *ptr;
  110. };
  111. static void
  112. nvd0_dmac_destroy(struct nouveau_object *core, struct nvd0_dmac *dmac)
  113. {
  114. if (dmac->ptr) {
  115. struct pci_dev *pdev = nv_device(core)->pdev;
  116. pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
  117. }
  118. nvd0_chan_destroy(core, &dmac->base);
  119. }
  120. static int
  121. nvd0_dmac_create(struct nouveau_object *core, u32 bclass, u8 head,
  122. void *data, u32 size, u64 syncbuf,
  123. struct nvd0_dmac *dmac)
  124. {
  125. struct nouveau_fb *pfb = nouveau_fb(core);
  126. struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
  127. struct nouveau_object *object;
  128. u32 pushbuf = *(u32 *)data;
  129. dma_addr_t handle;
  130. void *ptr;
  131. int ret;
  132. ptr = pci_alloc_consistent(nv_device(core)->pdev, PAGE_SIZE, &handle);
  133. if (!ptr)
  134. return -ENOMEM;
  135. ret = nouveau_object_new(client, NVDRM_DEVICE, pushbuf,
  136. NV_DMA_FROM_MEMORY_CLASS,
  137. &(struct nv_dma_class) {
  138. .flags = NV_DMA_TARGET_PCI_US |
  139. NV_DMA_ACCESS_RD,
  140. .start = handle + 0x0000,
  141. .limit = handle + 0x0fff,
  142. }, sizeof(struct nv_dma_class), &object);
  143. if (ret)
  144. return ret;
  145. ret = nvd0_chan_create(core, bclass, head, data, size, &dmac->base);
  146. if (ret)
  147. return ret;
  148. dmac->handle = handle;
  149. dmac->ptr = ptr;
  150. ret = nouveau_object_new(client, dmac->base.handle, NvEvoSync,
  151. NV_DMA_IN_MEMORY_CLASS,
  152. &(struct nv_dma_class) {
  153. .flags = NV_DMA_TARGET_VRAM |
  154. NV_DMA_ACCESS_RDWR,
  155. .start = syncbuf + 0x0000,
  156. .limit = syncbuf + 0x0fff,
  157. }, sizeof(struct nv_dma_class), &object);
  158. if (ret)
  159. goto out;
  160. ret = nouveau_object_new(client, dmac->base.handle, NvEvoVRAM,
  161. NV_DMA_IN_MEMORY_CLASS,
  162. &(struct nv_dma_class) {
  163. .flags = NV_DMA_TARGET_VRAM |
  164. NV_DMA_ACCESS_RDWR,
  165. .start = 0,
  166. .limit = pfb->ram.size - 1,
  167. }, sizeof(struct nv_dma_class), &object);
  168. if (ret)
  169. goto out;
  170. ret = nouveau_object_new(client, dmac->base.handle, NvEvoVRAM_LP,
  171. NV_DMA_IN_MEMORY_CLASS,
  172. &(struct nv_dma_class) {
  173. .flags = NV_DMA_TARGET_VRAM |
  174. NV_DMA_ACCESS_RDWR,
  175. .start = 0,
  176. .limit = pfb->ram.size - 1,
  177. .conf0 = NVD0_DMA_CONF0_ENABLE |
  178. NVD0_DMA_CONF0_PAGE_LP,
  179. }, sizeof(struct nv_dma_class), &object);
  180. if (ret)
  181. goto out;
  182. ret = nouveau_object_new(client, dmac->base.handle, NvEvoFB32,
  183. NV_DMA_IN_MEMORY_CLASS,
  184. &(struct nv_dma_class) {
  185. .flags = NV_DMA_TARGET_VRAM |
  186. NV_DMA_ACCESS_RDWR,
  187. .start = 0,
  188. .limit = pfb->ram.size - 1,
  189. .conf0 = 0x00fe |
  190. NVD0_DMA_CONF0_ENABLE |
  191. NVD0_DMA_CONF0_PAGE_LP,
  192. }, sizeof(struct nv_dma_class), &object);
  193. out:
  194. if (ret)
  195. nvd0_dmac_destroy(core, dmac);
  196. return ret;
  197. }
  198. struct nvd0_mast {
  199. struct nvd0_dmac base;
  200. };
  201. struct nvd0_curs {
  202. struct nvd0_pioc base;
  203. };
  204. struct nvd0_sync {
  205. struct nvd0_dmac base;
  206. struct {
  207. u32 offset;
  208. u16 value;
  209. } sem;
  210. };
  211. struct nvd0_ovly {
  212. struct nvd0_dmac base;
  213. };
  214. struct nvd0_oimm {
  215. struct nvd0_pioc base;
  216. };
  217. struct nvd0_head {
  218. struct nouveau_crtc base;
  219. struct nvd0_curs curs;
  220. struct nvd0_sync sync;
  221. struct nvd0_ovly ovly;
  222. struct nvd0_oimm oimm;
  223. };
  224. #define nvd0_head(c) ((struct nvd0_head *)nouveau_crtc(c))
  225. #define nvd0_curs(c) (&nvd0_head(c)->curs)
  226. #define nvd0_sync(c) (&nvd0_head(c)->sync)
  227. #define nvd0_ovly(c) (&nvd0_head(c)->ovly)
  228. #define nvd0_oimm(c) (&nvd0_head(c)->oimm)
  229. #define nvd0_chan(c) (&(c)->base.base)
  230. struct nvd0_disp {
  231. struct nouveau_object *core;
  232. struct nvd0_mast mast;
  233. u32 modeset;
  234. struct nouveau_bo *sync;
  235. };
  236. static struct nvd0_disp *
  237. nvd0_disp(struct drm_device *dev)
  238. {
  239. return nouveau_display(dev)->priv;
  240. }
  241. #define nvd0_mast(d) (&nvd0_disp(d)->mast)
  242. static struct drm_crtc *
  243. nvd0_display_crtc_get(struct drm_encoder *encoder)
  244. {
  245. return nouveau_encoder(encoder)->crtc;
  246. }
  247. /******************************************************************************
  248. * EVO channel helpers
  249. *****************************************************************************/
  250. static u32 *
  251. evo_wait(void *evoc, int nr)
  252. {
  253. struct nvd0_dmac *dmac = evoc;
  254. u32 put = nv_ro32(dmac->base.user, 0x0000) / 4;
  255. if (put + nr >= (PAGE_SIZE / 4)) {
  256. dmac->ptr[put] = 0x20000000;
  257. nv_wo32(dmac->base.user, 0x0000, 0x00000000);
  258. if (!nv_wait(dmac->base.user, 0x0004, ~0, 0x00000000)) {
  259. NV_ERROR(dmac->base.user, "channel stalled\n");
  260. return NULL;
  261. }
  262. put = 0;
  263. }
  264. return dmac->ptr + put;
  265. }
  266. static void
  267. evo_kick(u32 *push, void *evoc)
  268. {
  269. struct nvd0_dmac *dmac = evoc;
  270. nv_wo32(dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
  271. }
  272. #define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
  273. #define evo_data(p,d) *((p)++) = (d)
  274. static bool
  275. evo_sync_wait(void *data)
  276. {
  277. return nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000;
  278. }
  279. static int
  280. evo_sync(struct drm_device *dev)
  281. {
  282. struct nouveau_device *device = nouveau_dev(dev);
  283. struct nvd0_disp *disp = nvd0_disp(dev);
  284. struct nvd0_mast *mast = nvd0_mast(dev);
  285. u32 *push = evo_wait(mast, 8);
  286. if (push) {
  287. nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
  288. evo_mthd(push, 0x0084, 1);
  289. evo_data(push, 0x80000000 | EVO_MAST_NTFY);
  290. evo_mthd(push, 0x0080, 2);
  291. evo_data(push, 0x00000000);
  292. evo_data(push, 0x00000000);
  293. evo_kick(push, mast);
  294. if (nv_wait_cb(device, evo_sync_wait, disp->sync))
  295. return 0;
  296. }
  297. return -EBUSY;
  298. }
  299. /******************************************************************************
  300. * Page flipping channel
  301. *****************************************************************************/
  302. struct nouveau_bo *
  303. nvd0_display_crtc_sema(struct drm_device *dev, int crtc)
  304. {
  305. return nvd0_disp(dev)->sync;
  306. }
  307. void
  308. nvd0_display_flip_stop(struct drm_crtc *crtc)
  309. {
  310. struct nvd0_sync *sync = nvd0_sync(crtc);
  311. u32 *push;
  312. push = evo_wait(sync, 8);
  313. if (push) {
  314. evo_mthd(push, 0x0084, 1);
  315. evo_data(push, 0x00000000);
  316. evo_mthd(push, 0x0094, 1);
  317. evo_data(push, 0x00000000);
  318. evo_mthd(push, 0x00c0, 1);
  319. evo_data(push, 0x00000000);
  320. evo_mthd(push, 0x0080, 1);
  321. evo_data(push, 0x00000000);
  322. evo_kick(push, sync);
  323. }
  324. }
  325. int
  326. nvd0_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  327. struct nouveau_channel *chan, u32 swap_interval)
  328. {
  329. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  330. struct nvd0_disp *disp = nvd0_disp(crtc->dev);
  331. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  332. struct nvd0_sync *sync = nvd0_sync(crtc);
  333. u64 offset;
  334. u32 *push;
  335. int ret;
  336. swap_interval <<= 4;
  337. if (swap_interval == 0)
  338. swap_interval |= 0x100;
  339. push = evo_wait(sync, 128);
  340. if (unlikely(push == NULL))
  341. return -EBUSY;
  342. /* synchronise with the rendering channel, if necessary */
  343. if (likely(chan)) {
  344. ret = RING_SPACE(chan, 10);
  345. if (ret)
  346. return ret;
  347. offset = nvc0_fence_crtc(chan, nv_crtc->index);
  348. offset += sync->sem.offset;
  349. BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  350. OUT_RING (chan, upper_32_bits(offset));
  351. OUT_RING (chan, lower_32_bits(offset));
  352. OUT_RING (chan, 0xf00d0000 | sync->sem.value);
  353. OUT_RING (chan, 0x1002);
  354. BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  355. OUT_RING (chan, upper_32_bits(offset));
  356. OUT_RING (chan, lower_32_bits(offset ^ 0x10));
  357. OUT_RING (chan, 0x74b1e000);
  358. OUT_RING (chan, 0x1001);
  359. FIRE_RING (chan);
  360. } else {
  361. nouveau_bo_wr32(disp->sync, sync->sem.offset / 4,
  362. 0xf00d0000 | sync->sem.value);
  363. evo_sync(crtc->dev);
  364. }
  365. /* queue the flip */
  366. evo_mthd(push, 0x0100, 1);
  367. evo_data(push, 0xfffe0000);
  368. evo_mthd(push, 0x0084, 1);
  369. evo_data(push, swap_interval);
  370. if (!(swap_interval & 0x00000100)) {
  371. evo_mthd(push, 0x00e0, 1);
  372. evo_data(push, 0x40000000);
  373. }
  374. evo_mthd(push, 0x0088, 4);
  375. evo_data(push, sync->sem.offset);
  376. evo_data(push, 0xf00d0000 | sync->sem.value);
  377. evo_data(push, 0x74b1e000);
  378. evo_data(push, NvEvoSync);
  379. evo_mthd(push, 0x00a0, 2);
  380. evo_data(push, 0x00000000);
  381. evo_data(push, 0x00000000);
  382. evo_mthd(push, 0x00c0, 1);
  383. evo_data(push, nv_fb->r_dma);
  384. evo_mthd(push, 0x0110, 2);
  385. evo_data(push, 0x00000000);
  386. evo_data(push, 0x00000000);
  387. evo_mthd(push, 0x0400, 5);
  388. evo_data(push, nv_fb->nvbo->bo.offset >> 8);
  389. evo_data(push, 0);
  390. evo_data(push, (fb->height << 16) | fb->width);
  391. evo_data(push, nv_fb->r_pitch);
  392. evo_data(push, nv_fb->r_format);
  393. evo_mthd(push, 0x0080, 1);
  394. evo_data(push, 0x00000000);
  395. evo_kick(push, sync);
  396. sync->sem.offset ^= 0x10;
  397. sync->sem.value++;
  398. return 0;
  399. }
  400. /******************************************************************************
  401. * CRTC
  402. *****************************************************************************/
  403. static int
  404. nvd0_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
  405. {
  406. struct nouveau_drm *drm = nouveau_drm(nv_crtc->base.dev);
  407. struct drm_device *dev = nv_crtc->base.dev;
  408. struct nouveau_connector *nv_connector;
  409. struct drm_connector *connector;
  410. u32 *push, mode = 0x00;
  411. u32 mthd;
  412. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  413. connector = &nv_connector->base;
  414. if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
  415. if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
  416. mode = DITHERING_MODE_DYNAMIC2X2;
  417. } else {
  418. mode = nv_connector->dithering_mode;
  419. }
  420. if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
  421. if (connector->display_info.bpc >= 8)
  422. mode |= DITHERING_DEPTH_8BPC;
  423. } else {
  424. mode |= nv_connector->dithering_depth;
  425. }
  426. if (nv_device(drm->device)->card_type < NV_E0)
  427. mthd = 0x0490 + (nv_crtc->index * 0x0300);
  428. else
  429. mthd = 0x04a0 + (nv_crtc->index * 0x0300);
  430. push = evo_wait(nvd0_mast(dev), 4);
  431. if (push) {
  432. evo_mthd(push, mthd, 1);
  433. evo_data(push, mode);
  434. if (update) {
  435. evo_mthd(push, 0x0080, 1);
  436. evo_data(push, 0x00000000);
  437. }
  438. evo_kick(push, nvd0_mast(dev));
  439. }
  440. return 0;
  441. }
  442. static int
  443. nvd0_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
  444. {
  445. struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
  446. struct drm_device *dev = nv_crtc->base.dev;
  447. struct drm_crtc *crtc = &nv_crtc->base;
  448. struct nouveau_connector *nv_connector;
  449. int mode = DRM_MODE_SCALE_NONE;
  450. u32 oX, oY, *push;
  451. /* start off at the resolution we programmed the crtc for, this
  452. * effectively handles NONE/FULL scaling
  453. */
  454. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  455. if (nv_connector && nv_connector->native_mode)
  456. mode = nv_connector->scaling_mode;
  457. if (mode != DRM_MODE_SCALE_NONE)
  458. omode = nv_connector->native_mode;
  459. else
  460. omode = umode;
  461. oX = omode->hdisplay;
  462. oY = omode->vdisplay;
  463. if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
  464. oY *= 2;
  465. /* add overscan compensation if necessary, will keep the aspect
  466. * ratio the same as the backend mode unless overridden by the
  467. * user setting both hborder and vborder properties.
  468. */
  469. if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
  470. (nv_connector->underscan == UNDERSCAN_AUTO &&
  471. nv_connector->edid &&
  472. drm_detect_hdmi_monitor(nv_connector->edid)))) {
  473. u32 bX = nv_connector->underscan_hborder;
  474. u32 bY = nv_connector->underscan_vborder;
  475. u32 aspect = (oY << 19) / oX;
  476. if (bX) {
  477. oX -= (bX * 2);
  478. if (bY) oY -= (bY * 2);
  479. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  480. } else {
  481. oX -= (oX >> 4) + 32;
  482. if (bY) oY -= (bY * 2);
  483. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  484. }
  485. }
  486. /* handle CENTER/ASPECT scaling, taking into account the areas
  487. * removed already for overscan compensation
  488. */
  489. switch (mode) {
  490. case DRM_MODE_SCALE_CENTER:
  491. oX = min((u32)umode->hdisplay, oX);
  492. oY = min((u32)umode->vdisplay, oY);
  493. /* fall-through */
  494. case DRM_MODE_SCALE_ASPECT:
  495. if (oY < oX) {
  496. u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
  497. oX = ((oY * aspect) + (aspect / 2)) >> 19;
  498. } else {
  499. u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
  500. oY = ((oX * aspect) + (aspect / 2)) >> 19;
  501. }
  502. break;
  503. default:
  504. break;
  505. }
  506. push = evo_wait(nvd0_mast(dev), 8);
  507. if (push) {
  508. evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
  509. evo_data(push, (oY << 16) | oX);
  510. evo_data(push, (oY << 16) | oX);
  511. evo_data(push, (oY << 16) | oX);
  512. evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
  513. evo_data(push, 0x00000000);
  514. evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
  515. evo_data(push, (umode->vdisplay << 16) | umode->hdisplay);
  516. evo_kick(push, nvd0_mast(dev));
  517. if (update) {
  518. nvd0_display_flip_stop(crtc);
  519. nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
  520. }
  521. }
  522. return 0;
  523. }
  524. static int
  525. nvd0_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
  526. int x, int y, bool update)
  527. {
  528. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
  529. u32 *push;
  530. push = evo_wait(nvd0_mast(fb->dev), 16);
  531. if (push) {
  532. evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
  533. evo_data(push, nvfb->nvbo->bo.offset >> 8);
  534. evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
  535. evo_data(push, (fb->height << 16) | fb->width);
  536. evo_data(push, nvfb->r_pitch);
  537. evo_data(push, nvfb->r_format);
  538. evo_data(push, nvfb->r_dma);
  539. evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
  540. evo_data(push, (y << 16) | x);
  541. if (update) {
  542. evo_mthd(push, 0x0080, 1);
  543. evo_data(push, 0x00000000);
  544. }
  545. evo_kick(push, nvd0_mast(fb->dev));
  546. }
  547. nv_crtc->fb.tile_flags = nvfb->r_dma;
  548. return 0;
  549. }
  550. static void
  551. nvd0_crtc_cursor_show(struct nouveau_crtc *nv_crtc, bool show, bool update)
  552. {
  553. struct drm_device *dev = nv_crtc->base.dev;
  554. u32 *push = evo_wait(nvd0_mast(dev), 16);
  555. if (push) {
  556. if (show) {
  557. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
  558. evo_data(push, 0x85000000);
  559. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  560. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  561. evo_data(push, NvEvoVRAM);
  562. } else {
  563. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
  564. evo_data(push, 0x05000000);
  565. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  566. evo_data(push, 0x00000000);
  567. }
  568. if (update) {
  569. evo_mthd(push, 0x0080, 1);
  570. evo_data(push, 0x00000000);
  571. }
  572. evo_kick(push, nvd0_mast(dev));
  573. }
  574. }
  575. static void
  576. nvd0_crtc_dpms(struct drm_crtc *crtc, int mode)
  577. {
  578. }
  579. static void
  580. nvd0_crtc_prepare(struct drm_crtc *crtc)
  581. {
  582. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  583. u32 *push;
  584. nvd0_display_flip_stop(crtc);
  585. push = evo_wait(nvd0_mast(crtc->dev), 2);
  586. if (push) {
  587. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  588. evo_data(push, 0x00000000);
  589. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
  590. evo_data(push, 0x03000000);
  591. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  592. evo_data(push, 0x00000000);
  593. evo_kick(push, nvd0_mast(crtc->dev));
  594. }
  595. nvd0_crtc_cursor_show(nv_crtc, false, false);
  596. }
  597. static void
  598. nvd0_crtc_commit(struct drm_crtc *crtc)
  599. {
  600. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  601. u32 *push;
  602. push = evo_wait(nvd0_mast(crtc->dev), 32);
  603. if (push) {
  604. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  605. evo_data(push, nv_crtc->fb.tile_flags);
  606. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
  607. evo_data(push, 0x83000000);
  608. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  609. evo_data(push, 0x00000000);
  610. evo_data(push, 0x00000000);
  611. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  612. evo_data(push, NvEvoVRAM);
  613. evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
  614. evo_data(push, 0xffffff00);
  615. evo_kick(push, nvd0_mast(crtc->dev));
  616. }
  617. nvd0_crtc_cursor_show(nv_crtc, nv_crtc->cursor.visible, true);
  618. nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
  619. }
  620. static bool
  621. nvd0_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
  622. struct drm_display_mode *adjusted_mode)
  623. {
  624. return true;
  625. }
  626. static int
  627. nvd0_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
  628. {
  629. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
  630. int ret;
  631. ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
  632. if (ret)
  633. return ret;
  634. if (old_fb) {
  635. nvfb = nouveau_framebuffer(old_fb);
  636. nouveau_bo_unpin(nvfb->nvbo);
  637. }
  638. return 0;
  639. }
  640. static int
  641. nvd0_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
  642. struct drm_display_mode *mode, int x, int y,
  643. struct drm_framebuffer *old_fb)
  644. {
  645. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  646. struct nouveau_connector *nv_connector;
  647. u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
  648. u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
  649. u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
  650. u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
  651. u32 vblan2e = 0, vblan2s = 1;
  652. u32 *push;
  653. int ret;
  654. hactive = mode->htotal;
  655. hsynce = mode->hsync_end - mode->hsync_start - 1;
  656. hbackp = mode->htotal - mode->hsync_end;
  657. hblanke = hsynce + hbackp;
  658. hfrontp = mode->hsync_start - mode->hdisplay;
  659. hblanks = mode->htotal - hfrontp - 1;
  660. vactive = mode->vtotal * vscan / ilace;
  661. vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
  662. vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
  663. vblanke = vsynce + vbackp;
  664. vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
  665. vblanks = vactive - vfrontp - 1;
  666. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  667. vblan2e = vactive + vsynce + vbackp;
  668. vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
  669. vactive = (vactive * 2) + 1;
  670. }
  671. ret = nvd0_crtc_swap_fbs(crtc, old_fb);
  672. if (ret)
  673. return ret;
  674. push = evo_wait(nvd0_mast(crtc->dev), 64);
  675. if (push) {
  676. evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
  677. evo_data(push, 0x00000000);
  678. evo_data(push, (vactive << 16) | hactive);
  679. evo_data(push, ( vsynce << 16) | hsynce);
  680. evo_data(push, (vblanke << 16) | hblanke);
  681. evo_data(push, (vblanks << 16) | hblanks);
  682. evo_data(push, (vblan2e << 16) | vblan2s);
  683. evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
  684. evo_data(push, 0x00000000); /* ??? */
  685. evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
  686. evo_data(push, mode->clock * 1000);
  687. evo_data(push, 0x00200000); /* ??? */
  688. evo_data(push, mode->clock * 1000);
  689. evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
  690. evo_data(push, 0x00000311);
  691. evo_data(push, 0x00000100);
  692. evo_kick(push, nvd0_mast(crtc->dev));
  693. }
  694. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  695. nvd0_crtc_set_dither(nv_crtc, false);
  696. nvd0_crtc_set_scale(nv_crtc, false);
  697. nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
  698. return 0;
  699. }
  700. static int
  701. nvd0_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  702. struct drm_framebuffer *old_fb)
  703. {
  704. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  705. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  706. int ret;
  707. if (!crtc->fb) {
  708. NV_DEBUG(drm, "No FB bound\n");
  709. return 0;
  710. }
  711. ret = nvd0_crtc_swap_fbs(crtc, old_fb);
  712. if (ret)
  713. return ret;
  714. nvd0_display_flip_stop(crtc);
  715. nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
  716. nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
  717. return 0;
  718. }
  719. static int
  720. nvd0_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
  721. struct drm_framebuffer *fb, int x, int y,
  722. enum mode_set_atomic state)
  723. {
  724. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  725. nvd0_display_flip_stop(crtc);
  726. nvd0_crtc_set_image(nv_crtc, fb, x, y, true);
  727. return 0;
  728. }
  729. static void
  730. nvd0_crtc_lut_load(struct drm_crtc *crtc)
  731. {
  732. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  733. void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
  734. int i;
  735. for (i = 0; i < 256; i++) {
  736. writew(0x6000 + (nv_crtc->lut.r[i] >> 2), lut + (i * 0x20) + 0);
  737. writew(0x6000 + (nv_crtc->lut.g[i] >> 2), lut + (i * 0x20) + 2);
  738. writew(0x6000 + (nv_crtc->lut.b[i] >> 2), lut + (i * 0x20) + 4);
  739. }
  740. }
  741. static int
  742. nvd0_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  743. uint32_t handle, uint32_t width, uint32_t height)
  744. {
  745. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  746. struct drm_device *dev = crtc->dev;
  747. struct drm_gem_object *gem;
  748. struct nouveau_bo *nvbo;
  749. bool visible = (handle != 0);
  750. int i, ret = 0;
  751. if (visible) {
  752. if (width != 64 || height != 64)
  753. return -EINVAL;
  754. gem = drm_gem_object_lookup(dev, file_priv, handle);
  755. if (unlikely(!gem))
  756. return -ENOENT;
  757. nvbo = nouveau_gem_object(gem);
  758. ret = nouveau_bo_map(nvbo);
  759. if (ret == 0) {
  760. for (i = 0; i < 64 * 64; i++) {
  761. u32 v = nouveau_bo_rd32(nvbo, i);
  762. nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
  763. }
  764. nouveau_bo_unmap(nvbo);
  765. }
  766. drm_gem_object_unreference_unlocked(gem);
  767. }
  768. if (visible != nv_crtc->cursor.visible) {
  769. nvd0_crtc_cursor_show(nv_crtc, visible, true);
  770. nv_crtc->cursor.visible = visible;
  771. }
  772. return ret;
  773. }
  774. static int
  775. nvd0_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  776. {
  777. struct nvd0_curs *curs = nvd0_curs(crtc);
  778. struct nvd0_chan *chan = nvd0_chan(curs);
  779. nv_wo32(chan->user, 0x0084, (y << 16) | (x & 0xffff));
  780. nv_wo32(chan->user, 0x0080, 0x00000000);
  781. return 0;
  782. }
  783. static void
  784. nvd0_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  785. uint32_t start, uint32_t size)
  786. {
  787. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  788. u32 end = max(start + size, (u32)256);
  789. u32 i;
  790. for (i = start; i < end; i++) {
  791. nv_crtc->lut.r[i] = r[i];
  792. nv_crtc->lut.g[i] = g[i];
  793. nv_crtc->lut.b[i] = b[i];
  794. }
  795. nvd0_crtc_lut_load(crtc);
  796. }
  797. static void
  798. nvd0_crtc_destroy(struct drm_crtc *crtc)
  799. {
  800. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  801. struct nvd0_disp *disp = nvd0_disp(crtc->dev);
  802. struct nvd0_head *head = nvd0_head(crtc);
  803. nvd0_dmac_destroy(disp->core, &head->ovly.base);
  804. nvd0_pioc_destroy(disp->core, &head->oimm.base);
  805. nvd0_dmac_destroy(disp->core, &head->sync.base);
  806. nvd0_pioc_destroy(disp->core, &head->curs.base);
  807. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  808. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  809. nouveau_bo_unmap(nv_crtc->lut.nvbo);
  810. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  811. drm_crtc_cleanup(crtc);
  812. kfree(crtc);
  813. }
  814. static const struct drm_crtc_helper_funcs nvd0_crtc_hfunc = {
  815. .dpms = nvd0_crtc_dpms,
  816. .prepare = nvd0_crtc_prepare,
  817. .commit = nvd0_crtc_commit,
  818. .mode_fixup = nvd0_crtc_mode_fixup,
  819. .mode_set = nvd0_crtc_mode_set,
  820. .mode_set_base = nvd0_crtc_mode_set_base,
  821. .mode_set_base_atomic = nvd0_crtc_mode_set_base_atomic,
  822. .load_lut = nvd0_crtc_lut_load,
  823. };
  824. static const struct drm_crtc_funcs nvd0_crtc_func = {
  825. .cursor_set = nvd0_crtc_cursor_set,
  826. .cursor_move = nvd0_crtc_cursor_move,
  827. .gamma_set = nvd0_crtc_gamma_set,
  828. .set_config = drm_crtc_helper_set_config,
  829. .destroy = nvd0_crtc_destroy,
  830. .page_flip = nouveau_crtc_page_flip,
  831. };
  832. static void
  833. nvd0_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
  834. {
  835. }
  836. static void
  837. nvd0_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
  838. {
  839. }
  840. static int
  841. nvd0_crtc_create(struct drm_device *dev, struct nouveau_object *core, int index)
  842. {
  843. struct nvd0_disp *disp = nvd0_disp(dev);
  844. struct nvd0_head *head;
  845. struct drm_crtc *crtc;
  846. int ret, i;
  847. head = kzalloc(sizeof(*head), GFP_KERNEL);
  848. if (!head)
  849. return -ENOMEM;
  850. head->base.index = index;
  851. head->base.set_dither = nvd0_crtc_set_dither;
  852. head->base.set_scale = nvd0_crtc_set_scale;
  853. head->base.cursor.set_offset = nvd0_cursor_set_offset;
  854. head->base.cursor.set_pos = nvd0_cursor_set_pos;
  855. for (i = 0; i < 256; i++) {
  856. head->base.lut.r[i] = i << 8;
  857. head->base.lut.g[i] = i << 8;
  858. head->base.lut.b[i] = i << 8;
  859. }
  860. crtc = &head->base.base;
  861. drm_crtc_init(dev, crtc, &nvd0_crtc_func);
  862. drm_crtc_helper_add(crtc, &nvd0_crtc_hfunc);
  863. drm_mode_crtc_set_gamma_size(crtc, 256);
  864. ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
  865. 0, 0x0000, NULL, &head->base.lut.nvbo);
  866. if (!ret) {
  867. ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
  868. if (!ret)
  869. ret = nouveau_bo_map(head->base.lut.nvbo);
  870. if (ret)
  871. nouveau_bo_ref(NULL, &head->base.lut.nvbo);
  872. }
  873. if (ret)
  874. goto out;
  875. nvd0_crtc_lut_load(crtc);
  876. /* allocate cursor resources */
  877. ret = nvd0_pioc_create(disp->core, NV50_DISP_CURS_CLASS, index,
  878. &(struct nv50_display_curs_class) {
  879. .head = index,
  880. }, sizeof(struct nv50_display_curs_class),
  881. &head->curs.base);
  882. if (ret)
  883. goto out;
  884. ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
  885. 0, 0x0000, NULL, &head->base.cursor.nvbo);
  886. if (!ret) {
  887. ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
  888. if (!ret)
  889. ret = nouveau_bo_map(head->base.cursor.nvbo);
  890. if (ret)
  891. nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
  892. }
  893. if (ret)
  894. goto out;
  895. /* allocate page flip / sync resources */
  896. ret = nvd0_dmac_create(disp->core, NV50_DISP_SYNC_CLASS, index,
  897. &(struct nv50_display_sync_class) {
  898. .pushbuf = EVO_PUSH_HANDLE(SYNC, index),
  899. .head = index,
  900. }, sizeof(struct nv50_display_sync_class),
  901. disp->sync->bo.offset, &head->sync.base);
  902. if (ret)
  903. goto out;
  904. head->sync.sem.offset = EVO_SYNC(1 + index, 0x00);
  905. /* allocate overlay resources */
  906. ret = nvd0_pioc_create(disp->core, NV50_DISP_OIMM_CLASS, index,
  907. &(struct nv50_display_oimm_class) {
  908. .head = index,
  909. }, sizeof(struct nv50_display_oimm_class),
  910. &head->oimm.base);
  911. if (ret)
  912. goto out;
  913. ret = nvd0_dmac_create(disp->core, NV50_DISP_OVLY_CLASS, index,
  914. &(struct nv50_display_ovly_class) {
  915. .pushbuf = EVO_PUSH_HANDLE(OVLY, index),
  916. .head = index,
  917. }, sizeof(struct nv50_display_ovly_class),
  918. disp->sync->bo.offset, &head->ovly.base);
  919. if (ret)
  920. goto out;
  921. out:
  922. if (ret)
  923. nvd0_crtc_destroy(crtc);
  924. return ret;
  925. }
  926. /******************************************************************************
  927. * DAC
  928. *****************************************************************************/
  929. static void
  930. nvd0_dac_dpms(struct drm_encoder *encoder, int mode)
  931. {
  932. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  933. struct drm_device *dev = encoder->dev;
  934. struct nouveau_device *device = nouveau_dev(dev);
  935. int or = nv_encoder->or;
  936. u32 dpms_ctrl;
  937. dpms_ctrl = 0x80000000;
  938. if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
  939. dpms_ctrl |= 0x00000001;
  940. if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
  941. dpms_ctrl |= 0x00000004;
  942. nv_wait(device, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
  943. nv_mask(device, 0x61a004 + (or * 0x0800), 0xc000007f, dpms_ctrl);
  944. nv_wait(device, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
  945. }
  946. static bool
  947. nvd0_dac_mode_fixup(struct drm_encoder *encoder,
  948. const struct drm_display_mode *mode,
  949. struct drm_display_mode *adjusted_mode)
  950. {
  951. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  952. struct nouveau_connector *nv_connector;
  953. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  954. if (nv_connector && nv_connector->native_mode) {
  955. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  956. int id = adjusted_mode->base.id;
  957. *adjusted_mode = *nv_connector->native_mode;
  958. adjusted_mode->base.id = id;
  959. }
  960. }
  961. return true;
  962. }
  963. static void
  964. nvd0_dac_commit(struct drm_encoder *encoder)
  965. {
  966. }
  967. static void
  968. nvd0_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  969. struct drm_display_mode *adjusted_mode)
  970. {
  971. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  972. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  973. u32 syncs, magic, *push;
  974. syncs = 0x00000001;
  975. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  976. syncs |= 0x00000008;
  977. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  978. syncs |= 0x00000010;
  979. magic = 0x31ec6000 | (nv_crtc->index << 25);
  980. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  981. magic |= 0x00000001;
  982. nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
  983. push = evo_wait(nvd0_mast(encoder->dev), 8);
  984. if (push) {
  985. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  986. evo_data(push, syncs);
  987. evo_data(push, magic);
  988. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 2);
  989. evo_data(push, 1 << nv_crtc->index);
  990. evo_data(push, 0x00ff);
  991. evo_kick(push, nvd0_mast(encoder->dev));
  992. }
  993. nv_encoder->crtc = encoder->crtc;
  994. }
  995. static void
  996. nvd0_dac_disconnect(struct drm_encoder *encoder)
  997. {
  998. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  999. struct drm_device *dev = encoder->dev;
  1000. u32 *push;
  1001. if (nv_encoder->crtc) {
  1002. nvd0_crtc_prepare(nv_encoder->crtc);
  1003. push = evo_wait(nvd0_mast(dev), 4);
  1004. if (push) {
  1005. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
  1006. evo_data(push, 0x00000000);
  1007. evo_mthd(push, 0x0080, 1);
  1008. evo_data(push, 0x00000000);
  1009. evo_kick(push, nvd0_mast(dev));
  1010. }
  1011. nv_encoder->crtc = NULL;
  1012. }
  1013. }
  1014. static enum drm_connector_status
  1015. nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  1016. {
  1017. enum drm_connector_status status = connector_status_disconnected;
  1018. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1019. struct drm_device *dev = encoder->dev;
  1020. struct nouveau_device *device = nouveau_dev(dev);
  1021. int or = nv_encoder->or;
  1022. u32 load;
  1023. nv_wr32(device, 0x61a00c + (or * 0x800), 0x00100000);
  1024. udelay(9500);
  1025. nv_wr32(device, 0x61a00c + (or * 0x800), 0x80000000);
  1026. load = nv_rd32(device, 0x61a00c + (or * 0x800));
  1027. if ((load & 0x38000000) == 0x38000000)
  1028. status = connector_status_connected;
  1029. nv_wr32(device, 0x61a00c + (or * 0x800), 0x00000000);
  1030. return status;
  1031. }
  1032. static void
  1033. nvd0_dac_destroy(struct drm_encoder *encoder)
  1034. {
  1035. drm_encoder_cleanup(encoder);
  1036. kfree(encoder);
  1037. }
  1038. static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
  1039. .dpms = nvd0_dac_dpms,
  1040. .mode_fixup = nvd0_dac_mode_fixup,
  1041. .prepare = nvd0_dac_disconnect,
  1042. .commit = nvd0_dac_commit,
  1043. .mode_set = nvd0_dac_mode_set,
  1044. .disable = nvd0_dac_disconnect,
  1045. .get_crtc = nvd0_display_crtc_get,
  1046. .detect = nvd0_dac_detect
  1047. };
  1048. static const struct drm_encoder_funcs nvd0_dac_func = {
  1049. .destroy = nvd0_dac_destroy,
  1050. };
  1051. static int
  1052. nvd0_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1053. {
  1054. struct drm_device *dev = connector->dev;
  1055. struct nouveau_encoder *nv_encoder;
  1056. struct drm_encoder *encoder;
  1057. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1058. if (!nv_encoder)
  1059. return -ENOMEM;
  1060. nv_encoder->dcb = dcbe;
  1061. nv_encoder->or = ffs(dcbe->or) - 1;
  1062. encoder = to_drm_encoder(nv_encoder);
  1063. encoder->possible_crtcs = dcbe->heads;
  1064. encoder->possible_clones = 0;
  1065. drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
  1066. drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
  1067. drm_mode_connector_attach_encoder(connector, encoder);
  1068. return 0;
  1069. }
  1070. /******************************************************************************
  1071. * Audio
  1072. *****************************************************************************/
  1073. static void
  1074. nvd0_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1075. {
  1076. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1077. struct nouveau_connector *nv_connector;
  1078. struct drm_device *dev = encoder->dev;
  1079. struct nouveau_device *device = nouveau_dev(dev);
  1080. int i, or = nv_encoder->or * 0x30;
  1081. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1082. if (!drm_detect_monitor_audio(nv_connector->edid))
  1083. return;
  1084. nv_mask(device, 0x10ec10 + or, 0x80000003, 0x80000001);
  1085. drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
  1086. if (nv_connector->base.eld[0]) {
  1087. u8 *eld = nv_connector->base.eld;
  1088. for (i = 0; i < eld[2] * 4; i++)
  1089. nv_wr32(device, 0x10ec00 + or, (i << 8) | eld[i]);
  1090. for (i = eld[2] * 4; i < 0x60; i++)
  1091. nv_wr32(device, 0x10ec00 + or, (i << 8) | 0x00);
  1092. nv_mask(device, 0x10ec10 + or, 0x80000002, 0x80000002);
  1093. }
  1094. }
  1095. static void
  1096. nvd0_audio_disconnect(struct drm_encoder *encoder)
  1097. {
  1098. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1099. struct drm_device *dev = encoder->dev;
  1100. struct nouveau_device *device = nouveau_dev(dev);
  1101. int or = nv_encoder->or * 0x30;
  1102. nv_mask(device, 0x10ec10 + or, 0x80000003, 0x80000000);
  1103. }
  1104. /******************************************************************************
  1105. * HDMI
  1106. *****************************************************************************/
  1107. static void
  1108. nvd0_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1109. {
  1110. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1111. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1112. struct nouveau_connector *nv_connector;
  1113. struct drm_device *dev = encoder->dev;
  1114. struct nouveau_device *device = nouveau_dev(dev);
  1115. int head = nv_crtc->index * 0x800;
  1116. u32 rekey = 56; /* binary driver, and tegra constant */
  1117. u32 max_ac_packet;
  1118. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1119. if (!drm_detect_hdmi_monitor(nv_connector->edid))
  1120. return;
  1121. max_ac_packet = mode->htotal - mode->hdisplay;
  1122. max_ac_packet -= rekey;
  1123. max_ac_packet -= 18; /* constant from tegra */
  1124. max_ac_packet /= 32;
  1125. /* AVI InfoFrame */
  1126. nv_mask(device, 0x616714 + head, 0x00000001, 0x00000000);
  1127. nv_wr32(device, 0x61671c + head, 0x000d0282);
  1128. nv_wr32(device, 0x616720 + head, 0x0000006f);
  1129. nv_wr32(device, 0x616724 + head, 0x00000000);
  1130. nv_wr32(device, 0x616728 + head, 0x00000000);
  1131. nv_wr32(device, 0x61672c + head, 0x00000000);
  1132. nv_mask(device, 0x616714 + head, 0x00000001, 0x00000001);
  1133. /* ??? InfoFrame? */
  1134. nv_mask(device, 0x6167a4 + head, 0x00000001, 0x00000000);
  1135. nv_wr32(device, 0x6167ac + head, 0x00000010);
  1136. nv_mask(device, 0x6167a4 + head, 0x00000001, 0x00000001);
  1137. /* HDMI_CTRL */
  1138. nv_mask(device, 0x616798 + head, 0x401f007f, 0x40000000 | rekey |
  1139. max_ac_packet << 16);
  1140. /* NFI, audio doesn't work without it though.. */
  1141. nv_mask(device, 0x616548 + head, 0x00000070, 0x00000000);
  1142. nvd0_audio_mode_set(encoder, mode);
  1143. }
  1144. static void
  1145. nvd0_hdmi_disconnect(struct drm_encoder *encoder)
  1146. {
  1147. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1148. struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
  1149. struct drm_device *dev = encoder->dev;
  1150. struct nouveau_device *device = nouveau_dev(dev);
  1151. int head = nv_crtc->index * 0x800;
  1152. nvd0_audio_disconnect(encoder);
  1153. nv_mask(device, 0x616798 + head, 0x40000000, 0x00000000);
  1154. nv_mask(device, 0x6167a4 + head, 0x00000001, 0x00000000);
  1155. nv_mask(device, 0x616714 + head, 0x00000001, 0x00000000);
  1156. }
  1157. /******************************************************************************
  1158. * SOR
  1159. *****************************************************************************/
  1160. static void
  1161. nvd0_sor_dp_train_set(struct drm_device *dev, struct dcb_output *dcb, u8 pattern)
  1162. {
  1163. struct nvd0_disp *disp = nvd0_disp(dev);
  1164. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1165. const u32 moff = (link << 2) | or;
  1166. nv_call(disp->core, NV94_DISP_SOR_DP_TRAIN + moff, pattern);
  1167. }
  1168. static void
  1169. nvd0_sor_dp_train_adj(struct drm_device *dev, struct dcb_output *dcb,
  1170. u8 lane, u8 swing, u8 preem)
  1171. {
  1172. struct nvd0_disp *disp = nvd0_disp(dev);
  1173. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1174. const u32 moff = (link << 2) | or;
  1175. const u32 data = (swing << 8) | preem;
  1176. nv_call(disp->core, NV94_DISP_SOR_DP_DRVCTL(lane) + moff, data);
  1177. }
  1178. static void
  1179. nvd0_sor_dp_link_set(struct drm_device *dev, struct dcb_output *dcb, int crtc,
  1180. int link_nr, u32 link_bw, bool enhframe)
  1181. {
  1182. struct nvd0_disp *disp = nvd0_disp(dev);
  1183. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1184. const u32 moff = (crtc << 3) | (link << 2) | or;
  1185. u32 data = ((link_bw / 27000) << 8) | link_nr;
  1186. if (enhframe)
  1187. data |= NV94_DISP_SOR_DP_LNKCTL_FRAME_ENH;
  1188. nv_call(disp->core, NV94_DISP_SOR_DP_LNKCTL + moff, data);
  1189. }
  1190. static void
  1191. nvd0_sor_dp_link_get(struct drm_device *dev, struct dcb_output *dcb,
  1192. u32 *link_nr, u32 *link_bw)
  1193. {
  1194. struct nouveau_device *device = nouveau_dev(dev);
  1195. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1196. const u32 loff = (or * 0x800) + (link * 0x80);
  1197. const u32 soff = (or * 0x800);
  1198. u32 dpctrl = nv_rd32(device, 0x61c10c + loff) & 0x000f0000;
  1199. u32 clksor = nv_rd32(device, 0x612300 + soff);
  1200. if (dpctrl > 0x00030000) *link_nr = 4;
  1201. else if (dpctrl > 0x00010000) *link_nr = 2;
  1202. else *link_nr = 1;
  1203. *link_bw = (clksor & 0x007c0000) >> 18;
  1204. *link_bw *= 27000;
  1205. }
  1206. static void
  1207. nvd0_sor_dp_calc_tu(struct drm_device *dev, struct dcb_output *dcb,
  1208. u32 crtc, u32 datarate)
  1209. {
  1210. struct nouveau_device *device = nouveau_dev(dev);
  1211. const u32 symbol = 100000;
  1212. const u32 TU = 64;
  1213. u32 link_nr, link_bw;
  1214. u64 ratio, value;
  1215. nvd0_sor_dp_link_get(dev, dcb, &link_nr, &link_bw);
  1216. ratio = datarate;
  1217. ratio *= symbol;
  1218. do_div(ratio, link_nr * link_bw);
  1219. value = (symbol - ratio) * TU;
  1220. value *= ratio;
  1221. do_div(value, symbol);
  1222. do_div(value, symbol);
  1223. value += 5;
  1224. value |= 0x08000000;
  1225. nv_wr32(device, 0x616610 + (crtc * 0x800), value);
  1226. }
  1227. static void
  1228. nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
  1229. {
  1230. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1231. struct drm_device *dev = encoder->dev;
  1232. struct nouveau_device *device = nouveau_dev(dev);
  1233. struct drm_encoder *partner;
  1234. int or = nv_encoder->or;
  1235. u32 dpms_ctrl;
  1236. nv_encoder->last_dpms = mode;
  1237. list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
  1238. struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
  1239. if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
  1240. continue;
  1241. if (nv_partner != nv_encoder &&
  1242. nv_partner->dcb->or == nv_encoder->dcb->or) {
  1243. if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
  1244. return;
  1245. break;
  1246. }
  1247. }
  1248. dpms_ctrl = (mode == DRM_MODE_DPMS_ON);
  1249. dpms_ctrl |= 0x80000000;
  1250. nv_wait(device, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
  1251. nv_mask(device, 0x61c004 + (or * 0x0800), 0x80000001, dpms_ctrl);
  1252. nv_wait(device, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
  1253. nv_wait(device, 0x61c030 + (or * 0x0800), 0x10000000, 0x00000000);
  1254. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  1255. struct dp_train_func func = {
  1256. .link_set = nvd0_sor_dp_link_set,
  1257. .train_set = nvd0_sor_dp_train_set,
  1258. .train_adj = nvd0_sor_dp_train_adj
  1259. };
  1260. nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, &func);
  1261. }
  1262. }
  1263. static bool
  1264. nvd0_sor_mode_fixup(struct drm_encoder *encoder,
  1265. const struct drm_display_mode *mode,
  1266. struct drm_display_mode *adjusted_mode)
  1267. {
  1268. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1269. struct nouveau_connector *nv_connector;
  1270. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1271. if (nv_connector && nv_connector->native_mode) {
  1272. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1273. int id = adjusted_mode->base.id;
  1274. *adjusted_mode = *nv_connector->native_mode;
  1275. adjusted_mode->base.id = id;
  1276. }
  1277. }
  1278. return true;
  1279. }
  1280. static void
  1281. nvd0_sor_disconnect(struct drm_encoder *encoder)
  1282. {
  1283. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1284. struct drm_device *dev = encoder->dev;
  1285. u32 *push;
  1286. if (nv_encoder->crtc) {
  1287. nvd0_crtc_prepare(nv_encoder->crtc);
  1288. push = evo_wait(nvd0_mast(dev), 4);
  1289. if (push) {
  1290. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
  1291. evo_data(push, 0x00000000);
  1292. evo_mthd(push, 0x0080, 1);
  1293. evo_data(push, 0x00000000);
  1294. evo_kick(push, nvd0_mast(dev));
  1295. }
  1296. nvd0_hdmi_disconnect(encoder);
  1297. nv_encoder->crtc = NULL;
  1298. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1299. }
  1300. }
  1301. static void
  1302. nvd0_sor_prepare(struct drm_encoder *encoder)
  1303. {
  1304. nvd0_sor_disconnect(encoder);
  1305. if (nouveau_encoder(encoder)->dcb->type == DCB_OUTPUT_DP)
  1306. evo_sync(encoder->dev);
  1307. }
  1308. static void
  1309. nvd0_sor_commit(struct drm_encoder *encoder)
  1310. {
  1311. }
  1312. static void
  1313. nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  1314. struct drm_display_mode *mode)
  1315. {
  1316. struct drm_device *dev = encoder->dev;
  1317. struct nouveau_drm *drm = nouveau_drm(dev);
  1318. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1319. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1320. struct nouveau_connector *nv_connector;
  1321. struct nvbios *bios = &drm->vbios;
  1322. u32 mode_ctrl = (1 << nv_crtc->index);
  1323. u32 syncs, magic, *push;
  1324. u32 or_config;
  1325. syncs = 0x00000001;
  1326. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1327. syncs |= 0x00000008;
  1328. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1329. syncs |= 0x00000010;
  1330. magic = 0x31ec6000 | (nv_crtc->index << 25);
  1331. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1332. magic |= 0x00000001;
  1333. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1334. switch (nv_encoder->dcb->type) {
  1335. case DCB_OUTPUT_TMDS:
  1336. if (nv_encoder->dcb->sorconf.link & 1) {
  1337. if (mode->clock < 165000)
  1338. mode_ctrl |= 0x00000100;
  1339. else
  1340. mode_ctrl |= 0x00000500;
  1341. } else {
  1342. mode_ctrl |= 0x00000200;
  1343. }
  1344. or_config = (mode_ctrl & 0x00000f00) >> 8;
  1345. if (mode->clock >= 165000)
  1346. or_config |= 0x0100;
  1347. nvd0_hdmi_mode_set(encoder, mode);
  1348. break;
  1349. case DCB_OUTPUT_LVDS:
  1350. or_config = (mode_ctrl & 0x00000f00) >> 8;
  1351. if (bios->fp_no_ddc) {
  1352. if (bios->fp.dual_link)
  1353. or_config |= 0x0100;
  1354. if (bios->fp.if_is_24bit)
  1355. or_config |= 0x0200;
  1356. } else {
  1357. if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  1358. if (((u8 *)nv_connector->edid)[121] == 2)
  1359. or_config |= 0x0100;
  1360. } else
  1361. if (mode->clock >= bios->fp.duallink_transition_clk) {
  1362. or_config |= 0x0100;
  1363. }
  1364. if (or_config & 0x0100) {
  1365. if (bios->fp.strapless_is_24bit & 2)
  1366. or_config |= 0x0200;
  1367. } else {
  1368. if (bios->fp.strapless_is_24bit & 1)
  1369. or_config |= 0x0200;
  1370. }
  1371. if (nv_connector->base.display_info.bpc == 8)
  1372. or_config |= 0x0200;
  1373. }
  1374. break;
  1375. case DCB_OUTPUT_DP:
  1376. if (nv_connector->base.display_info.bpc == 6) {
  1377. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  1378. syncs |= 0x00000002 << 6;
  1379. } else {
  1380. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  1381. syncs |= 0x00000005 << 6;
  1382. }
  1383. if (nv_encoder->dcb->sorconf.link & 1)
  1384. mode_ctrl |= 0x00000800;
  1385. else
  1386. mode_ctrl |= 0x00000900;
  1387. or_config = (mode_ctrl & 0x00000f00) >> 8;
  1388. break;
  1389. default:
  1390. BUG_ON(1);
  1391. break;
  1392. }
  1393. nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  1394. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  1395. nvd0_sor_dp_calc_tu(dev, nv_encoder->dcb, nv_crtc->index,
  1396. nv_encoder->dp.datarate);
  1397. }
  1398. push = evo_wait(nvd0_mast(dev), 8);
  1399. if (push) {
  1400. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1401. evo_data(push, syncs);
  1402. evo_data(push, magic);
  1403. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x020), 2);
  1404. evo_data(push, mode_ctrl);
  1405. evo_data(push, or_config);
  1406. evo_kick(push, nvd0_mast(dev));
  1407. }
  1408. nv_encoder->crtc = encoder->crtc;
  1409. }
  1410. static void
  1411. nvd0_sor_destroy(struct drm_encoder *encoder)
  1412. {
  1413. drm_encoder_cleanup(encoder);
  1414. kfree(encoder);
  1415. }
  1416. static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
  1417. .dpms = nvd0_sor_dpms,
  1418. .mode_fixup = nvd0_sor_mode_fixup,
  1419. .prepare = nvd0_sor_prepare,
  1420. .commit = nvd0_sor_commit,
  1421. .mode_set = nvd0_sor_mode_set,
  1422. .disable = nvd0_sor_disconnect,
  1423. .get_crtc = nvd0_display_crtc_get,
  1424. };
  1425. static const struct drm_encoder_funcs nvd0_sor_func = {
  1426. .destroy = nvd0_sor_destroy,
  1427. };
  1428. static int
  1429. nvd0_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1430. {
  1431. struct drm_device *dev = connector->dev;
  1432. struct nouveau_encoder *nv_encoder;
  1433. struct drm_encoder *encoder;
  1434. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1435. if (!nv_encoder)
  1436. return -ENOMEM;
  1437. nv_encoder->dcb = dcbe;
  1438. nv_encoder->or = ffs(dcbe->or) - 1;
  1439. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1440. encoder = to_drm_encoder(nv_encoder);
  1441. encoder->possible_crtcs = dcbe->heads;
  1442. encoder->possible_clones = 0;
  1443. drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
  1444. drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
  1445. drm_mode_connector_attach_encoder(connector, encoder);
  1446. return 0;
  1447. }
  1448. /******************************************************************************
  1449. * Init
  1450. *****************************************************************************/
  1451. void
  1452. nvd0_display_fini(struct drm_device *dev)
  1453. {
  1454. }
  1455. int
  1456. nvd0_display_init(struct drm_device *dev)
  1457. {
  1458. u32 *push = evo_wait(nvd0_mast(dev), 32);
  1459. if (push) {
  1460. evo_mthd(push, 0x0088, 1);
  1461. evo_data(push, NvEvoSync);
  1462. evo_mthd(push, 0x0084, 1);
  1463. evo_data(push, 0x00000000);
  1464. evo_mthd(push, 0x0084, 1);
  1465. evo_data(push, 0x80000000);
  1466. evo_mthd(push, 0x008c, 1);
  1467. evo_data(push, 0x00000000);
  1468. evo_kick(push, nvd0_mast(dev));
  1469. return 0;
  1470. }
  1471. return -EBUSY;
  1472. }
  1473. void
  1474. nvd0_display_destroy(struct drm_device *dev)
  1475. {
  1476. struct nvd0_disp *disp = nvd0_disp(dev);
  1477. nvd0_dmac_destroy(disp->core, &disp->mast.base);
  1478. nouveau_bo_unmap(disp->sync);
  1479. nouveau_bo_ref(NULL, &disp->sync);
  1480. nouveau_display(dev)->priv = NULL;
  1481. kfree(disp);
  1482. }
  1483. int
  1484. nvd0_display_create(struct drm_device *dev)
  1485. {
  1486. static const u16 oclass[] = {
  1487. NVE0_DISP_CLASS,
  1488. NVD0_DISP_CLASS,
  1489. };
  1490. struct nouveau_device *device = nouveau_dev(dev);
  1491. struct nouveau_drm *drm = nouveau_drm(dev);
  1492. struct dcb_table *dcb = &drm->vbios.dcb;
  1493. struct drm_connector *connector, *tmp;
  1494. struct nvd0_disp *disp;
  1495. struct dcb_output *dcbe;
  1496. int crtcs, ret, i;
  1497. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  1498. if (!disp)
  1499. return -ENOMEM;
  1500. nouveau_display(dev)->priv = disp;
  1501. nouveau_display(dev)->dtor = nvd0_display_destroy;
  1502. nouveau_display(dev)->init = nvd0_display_init;
  1503. nouveau_display(dev)->fini = nvd0_display_fini;
  1504. /* small shared memory area we use for notifiers and semaphores */
  1505. ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
  1506. 0, 0x0000, NULL, &disp->sync);
  1507. if (!ret) {
  1508. ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
  1509. if (!ret)
  1510. ret = nouveau_bo_map(disp->sync);
  1511. if (ret)
  1512. nouveau_bo_ref(NULL, &disp->sync);
  1513. }
  1514. if (ret)
  1515. goto out;
  1516. /* attempt to allocate a supported evo display class */
  1517. ret = -ENODEV;
  1518. for (i = 0; ret && i < ARRAY_SIZE(oclass); i++) {
  1519. ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE,
  1520. 0xd1500000, oclass[i], NULL, 0,
  1521. &disp->core);
  1522. }
  1523. if (ret)
  1524. goto out;
  1525. /* allocate master evo channel */
  1526. ret = nvd0_dmac_create(disp->core, NV50_DISP_MAST_CLASS, 0,
  1527. &(struct nv50_display_mast_class) {
  1528. .pushbuf = EVO_PUSH_HANDLE(MAST, 0),
  1529. }, sizeof(struct nv50_display_mast_class),
  1530. disp->sync->bo.offset, &disp->mast.base);
  1531. if (ret)
  1532. goto out;
  1533. /* create crtc objects to represent the hw heads */
  1534. crtcs = nv_rd32(device, 0x022448);
  1535. for (i = 0; i < crtcs; i++) {
  1536. ret = nvd0_crtc_create(dev, disp->core, i);
  1537. if (ret)
  1538. goto out;
  1539. }
  1540. /* create encoder/connector objects based on VBIOS DCB table */
  1541. for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
  1542. connector = nouveau_connector_create(dev, dcbe->connector);
  1543. if (IS_ERR(connector))
  1544. continue;
  1545. if (dcbe->location != DCB_LOC_ON_CHIP) {
  1546. NV_WARN(drm, "skipping off-chip encoder %d/%d\n",
  1547. dcbe->type, ffs(dcbe->or) - 1);
  1548. continue;
  1549. }
  1550. switch (dcbe->type) {
  1551. case DCB_OUTPUT_TMDS:
  1552. case DCB_OUTPUT_LVDS:
  1553. case DCB_OUTPUT_DP:
  1554. nvd0_sor_create(connector, dcbe);
  1555. break;
  1556. case DCB_OUTPUT_ANALOG:
  1557. nvd0_dac_create(connector, dcbe);
  1558. break;
  1559. default:
  1560. NV_WARN(drm, "skipping unsupported encoder %d/%d\n",
  1561. dcbe->type, ffs(dcbe->or) - 1);
  1562. continue;
  1563. }
  1564. }
  1565. /* cull any connectors we created that don't have an encoder */
  1566. list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
  1567. if (connector->encoder_ids[0])
  1568. continue;
  1569. NV_WARN(drm, "%s has no encoders, removing\n",
  1570. drm_get_connector_name(connector));
  1571. connector->funcs->destroy(connector);
  1572. }
  1573. out:
  1574. if (ret)
  1575. nvd0_display_destroy(dev);
  1576. return ret;
  1577. }