nvd0_display.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  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 nvd0_disp *disp = nvd0_disp(encoder->dev);
  934. int or = nv_encoder->or;
  935. u32 dpms_ctrl;
  936. dpms_ctrl = 0x00000000;
  937. if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
  938. dpms_ctrl |= 0x00000001;
  939. if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
  940. dpms_ctrl |= 0x00000004;
  941. nv_call(disp->core, NV50_DISP_DAC_PWR + or, dpms_ctrl);
  942. }
  943. static bool
  944. nvd0_dac_mode_fixup(struct drm_encoder *encoder,
  945. const struct drm_display_mode *mode,
  946. struct drm_display_mode *adjusted_mode)
  947. {
  948. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  949. struct nouveau_connector *nv_connector;
  950. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  951. if (nv_connector && nv_connector->native_mode) {
  952. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  953. int id = adjusted_mode->base.id;
  954. *adjusted_mode = *nv_connector->native_mode;
  955. adjusted_mode->base.id = id;
  956. }
  957. }
  958. return true;
  959. }
  960. static void
  961. nvd0_dac_commit(struct drm_encoder *encoder)
  962. {
  963. }
  964. static void
  965. nvd0_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  966. struct drm_display_mode *adjusted_mode)
  967. {
  968. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  969. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  970. u32 syncs, magic, *push;
  971. syncs = 0x00000001;
  972. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  973. syncs |= 0x00000008;
  974. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  975. syncs |= 0x00000010;
  976. magic = 0x31ec6000 | (nv_crtc->index << 25);
  977. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  978. magic |= 0x00000001;
  979. nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
  980. push = evo_wait(nvd0_mast(encoder->dev), 8);
  981. if (push) {
  982. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  983. evo_data(push, syncs);
  984. evo_data(push, magic);
  985. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 2);
  986. evo_data(push, 1 << nv_crtc->index);
  987. evo_data(push, 0x00ff);
  988. evo_kick(push, nvd0_mast(encoder->dev));
  989. }
  990. nv_encoder->crtc = encoder->crtc;
  991. }
  992. static void
  993. nvd0_dac_disconnect(struct drm_encoder *encoder)
  994. {
  995. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  996. struct drm_device *dev = encoder->dev;
  997. u32 *push;
  998. if (nv_encoder->crtc) {
  999. nvd0_crtc_prepare(nv_encoder->crtc);
  1000. push = evo_wait(nvd0_mast(dev), 4);
  1001. if (push) {
  1002. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
  1003. evo_data(push, 0x00000000);
  1004. evo_mthd(push, 0x0080, 1);
  1005. evo_data(push, 0x00000000);
  1006. evo_kick(push, nvd0_mast(dev));
  1007. }
  1008. nv_encoder->crtc = NULL;
  1009. }
  1010. }
  1011. static enum drm_connector_status
  1012. nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  1013. {
  1014. struct nvd0_disp *disp = nvd0_disp(encoder->dev);
  1015. int ret, or = nouveau_encoder(encoder)->or;
  1016. u32 load;
  1017. ret = nv_exec(disp->core, NV50_DISP_DAC_LOAD + or, &load, sizeof(load));
  1018. if (ret || load != 7)
  1019. return connector_status_disconnected;
  1020. return connector_status_connected;
  1021. }
  1022. static void
  1023. nvd0_dac_destroy(struct drm_encoder *encoder)
  1024. {
  1025. drm_encoder_cleanup(encoder);
  1026. kfree(encoder);
  1027. }
  1028. static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
  1029. .dpms = nvd0_dac_dpms,
  1030. .mode_fixup = nvd0_dac_mode_fixup,
  1031. .prepare = nvd0_dac_disconnect,
  1032. .commit = nvd0_dac_commit,
  1033. .mode_set = nvd0_dac_mode_set,
  1034. .disable = nvd0_dac_disconnect,
  1035. .get_crtc = nvd0_display_crtc_get,
  1036. .detect = nvd0_dac_detect
  1037. };
  1038. static const struct drm_encoder_funcs nvd0_dac_func = {
  1039. .destroy = nvd0_dac_destroy,
  1040. };
  1041. static int
  1042. nvd0_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1043. {
  1044. struct drm_device *dev = connector->dev;
  1045. struct nouveau_encoder *nv_encoder;
  1046. struct drm_encoder *encoder;
  1047. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1048. if (!nv_encoder)
  1049. return -ENOMEM;
  1050. nv_encoder->dcb = dcbe;
  1051. nv_encoder->or = ffs(dcbe->or) - 1;
  1052. encoder = to_drm_encoder(nv_encoder);
  1053. encoder->possible_crtcs = dcbe->heads;
  1054. encoder->possible_clones = 0;
  1055. drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
  1056. drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
  1057. drm_mode_connector_attach_encoder(connector, encoder);
  1058. return 0;
  1059. }
  1060. /******************************************************************************
  1061. * Audio
  1062. *****************************************************************************/
  1063. static void
  1064. nvd0_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1065. {
  1066. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1067. struct nouveau_connector *nv_connector;
  1068. struct drm_device *dev = encoder->dev;
  1069. struct nouveau_device *device = nouveau_dev(dev);
  1070. int i, or = nv_encoder->or * 0x30;
  1071. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1072. if (!drm_detect_monitor_audio(nv_connector->edid))
  1073. return;
  1074. nv_mask(device, 0x10ec10 + or, 0x80000003, 0x80000001);
  1075. drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
  1076. if (nv_connector->base.eld[0]) {
  1077. u8 *eld = nv_connector->base.eld;
  1078. for (i = 0; i < eld[2] * 4; i++)
  1079. nv_wr32(device, 0x10ec00 + or, (i << 8) | eld[i]);
  1080. for (i = eld[2] * 4; i < 0x60; i++)
  1081. nv_wr32(device, 0x10ec00 + or, (i << 8) | 0x00);
  1082. nv_mask(device, 0x10ec10 + or, 0x80000002, 0x80000002);
  1083. }
  1084. }
  1085. static void
  1086. nvd0_audio_disconnect(struct drm_encoder *encoder)
  1087. {
  1088. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1089. struct drm_device *dev = encoder->dev;
  1090. struct nouveau_device *device = nouveau_dev(dev);
  1091. int or = nv_encoder->or * 0x30;
  1092. nv_mask(device, 0x10ec10 + or, 0x80000003, 0x80000000);
  1093. }
  1094. /******************************************************************************
  1095. * HDMI
  1096. *****************************************************************************/
  1097. static void
  1098. nvd0_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1099. {
  1100. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1101. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1102. struct nouveau_connector *nv_connector;
  1103. struct drm_device *dev = encoder->dev;
  1104. struct nouveau_device *device = nouveau_dev(dev);
  1105. int head = nv_crtc->index * 0x800;
  1106. u32 rekey = 56; /* binary driver, and tegra constant */
  1107. u32 max_ac_packet;
  1108. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1109. if (!drm_detect_hdmi_monitor(nv_connector->edid))
  1110. return;
  1111. max_ac_packet = mode->htotal - mode->hdisplay;
  1112. max_ac_packet -= rekey;
  1113. max_ac_packet -= 18; /* constant from tegra */
  1114. max_ac_packet /= 32;
  1115. /* AVI InfoFrame */
  1116. nv_mask(device, 0x616714 + head, 0x00000001, 0x00000000);
  1117. nv_wr32(device, 0x61671c + head, 0x000d0282);
  1118. nv_wr32(device, 0x616720 + head, 0x0000006f);
  1119. nv_wr32(device, 0x616724 + head, 0x00000000);
  1120. nv_wr32(device, 0x616728 + head, 0x00000000);
  1121. nv_wr32(device, 0x61672c + head, 0x00000000);
  1122. nv_mask(device, 0x616714 + head, 0x00000001, 0x00000001);
  1123. /* ??? InfoFrame? */
  1124. nv_mask(device, 0x6167a4 + head, 0x00000001, 0x00000000);
  1125. nv_wr32(device, 0x6167ac + head, 0x00000010);
  1126. nv_mask(device, 0x6167a4 + head, 0x00000001, 0x00000001);
  1127. /* HDMI_CTRL */
  1128. nv_mask(device, 0x616798 + head, 0x401f007f, 0x40000000 | rekey |
  1129. max_ac_packet << 16);
  1130. /* NFI, audio doesn't work without it though.. */
  1131. nv_mask(device, 0x616548 + head, 0x00000070, 0x00000000);
  1132. nvd0_audio_mode_set(encoder, mode);
  1133. }
  1134. static void
  1135. nvd0_hdmi_disconnect(struct drm_encoder *encoder)
  1136. {
  1137. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1138. struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
  1139. struct drm_device *dev = encoder->dev;
  1140. struct nouveau_device *device = nouveau_dev(dev);
  1141. int head = nv_crtc->index * 0x800;
  1142. nvd0_audio_disconnect(encoder);
  1143. nv_mask(device, 0x616798 + head, 0x40000000, 0x00000000);
  1144. nv_mask(device, 0x6167a4 + head, 0x00000001, 0x00000000);
  1145. nv_mask(device, 0x616714 + head, 0x00000001, 0x00000000);
  1146. }
  1147. /******************************************************************************
  1148. * SOR
  1149. *****************************************************************************/
  1150. static void
  1151. nvd0_sor_dp_train_set(struct drm_device *dev, struct dcb_output *dcb, u8 pattern)
  1152. {
  1153. struct nvd0_disp *disp = nvd0_disp(dev);
  1154. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1155. const u32 moff = (link << 2) | or;
  1156. nv_call(disp->core, NV94_DISP_SOR_DP_TRAIN + moff, pattern);
  1157. }
  1158. static void
  1159. nvd0_sor_dp_train_adj(struct drm_device *dev, struct dcb_output *dcb,
  1160. u8 lane, u8 swing, u8 preem)
  1161. {
  1162. struct nvd0_disp *disp = nvd0_disp(dev);
  1163. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1164. const u32 moff = (link << 2) | or;
  1165. const u32 data = (swing << 8) | preem;
  1166. nv_call(disp->core, NV94_DISP_SOR_DP_DRVCTL(lane) + moff, data);
  1167. }
  1168. static void
  1169. nvd0_sor_dp_link_set(struct drm_device *dev, struct dcb_output *dcb, int crtc,
  1170. int link_nr, u32 link_bw, bool enhframe)
  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 = (crtc << 3) | (link << 2) | or;
  1175. u32 data = ((link_bw / 27000) << 8) | link_nr;
  1176. if (enhframe)
  1177. data |= NV94_DISP_SOR_DP_LNKCTL_FRAME_ENH;
  1178. nv_call(disp->core, NV94_DISP_SOR_DP_LNKCTL + moff, data);
  1179. }
  1180. static void
  1181. nvd0_sor_dp_link_get(struct drm_device *dev, struct dcb_output *dcb,
  1182. u32 *link_nr, u32 *link_bw)
  1183. {
  1184. struct nouveau_device *device = nouveau_dev(dev);
  1185. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1186. const u32 loff = (or * 0x800) + (link * 0x80);
  1187. const u32 soff = (or * 0x800);
  1188. u32 dpctrl = nv_rd32(device, 0x61c10c + loff) & 0x000f0000;
  1189. u32 clksor = nv_rd32(device, 0x612300 + soff);
  1190. if (dpctrl > 0x00030000) *link_nr = 4;
  1191. else if (dpctrl > 0x00010000) *link_nr = 2;
  1192. else *link_nr = 1;
  1193. *link_bw = (clksor & 0x007c0000) >> 18;
  1194. *link_bw *= 27000;
  1195. }
  1196. static void
  1197. nvd0_sor_dp_calc_tu(struct drm_device *dev, struct dcb_output *dcb,
  1198. u32 crtc, u32 datarate)
  1199. {
  1200. struct nouveau_device *device = nouveau_dev(dev);
  1201. const u32 symbol = 100000;
  1202. const u32 TU = 64;
  1203. u32 link_nr, link_bw;
  1204. u64 ratio, value;
  1205. nvd0_sor_dp_link_get(dev, dcb, &link_nr, &link_bw);
  1206. ratio = datarate;
  1207. ratio *= symbol;
  1208. do_div(ratio, link_nr * link_bw);
  1209. value = (symbol - ratio) * TU;
  1210. value *= ratio;
  1211. do_div(value, symbol);
  1212. do_div(value, symbol);
  1213. value += 5;
  1214. value |= 0x08000000;
  1215. nv_wr32(device, 0x616610 + (crtc * 0x800), value);
  1216. }
  1217. static void
  1218. nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
  1219. {
  1220. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1221. struct drm_device *dev = encoder->dev;
  1222. struct nvd0_disp *disp = nvd0_disp(dev);
  1223. struct drm_encoder *partner;
  1224. int or = nv_encoder->or;
  1225. nv_encoder->last_dpms = mode;
  1226. list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
  1227. struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
  1228. if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
  1229. continue;
  1230. if (nv_partner != nv_encoder &&
  1231. nv_partner->dcb->or == nv_encoder->dcb->or) {
  1232. if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
  1233. return;
  1234. break;
  1235. }
  1236. }
  1237. nv_call(disp->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
  1238. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  1239. struct dp_train_func func = {
  1240. .link_set = nvd0_sor_dp_link_set,
  1241. .train_set = nvd0_sor_dp_train_set,
  1242. .train_adj = nvd0_sor_dp_train_adj
  1243. };
  1244. nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, &func);
  1245. }
  1246. }
  1247. static bool
  1248. nvd0_sor_mode_fixup(struct drm_encoder *encoder,
  1249. const struct drm_display_mode *mode,
  1250. struct drm_display_mode *adjusted_mode)
  1251. {
  1252. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1253. struct nouveau_connector *nv_connector;
  1254. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1255. if (nv_connector && nv_connector->native_mode) {
  1256. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1257. int id = adjusted_mode->base.id;
  1258. *adjusted_mode = *nv_connector->native_mode;
  1259. adjusted_mode->base.id = id;
  1260. }
  1261. }
  1262. return true;
  1263. }
  1264. static void
  1265. nvd0_sor_disconnect(struct drm_encoder *encoder)
  1266. {
  1267. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1268. struct drm_device *dev = encoder->dev;
  1269. u32 *push;
  1270. if (nv_encoder->crtc) {
  1271. nvd0_crtc_prepare(nv_encoder->crtc);
  1272. push = evo_wait(nvd0_mast(dev), 4);
  1273. if (push) {
  1274. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
  1275. evo_data(push, 0x00000000);
  1276. evo_mthd(push, 0x0080, 1);
  1277. evo_data(push, 0x00000000);
  1278. evo_kick(push, nvd0_mast(dev));
  1279. }
  1280. nvd0_hdmi_disconnect(encoder);
  1281. nv_encoder->crtc = NULL;
  1282. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1283. }
  1284. }
  1285. static void
  1286. nvd0_sor_prepare(struct drm_encoder *encoder)
  1287. {
  1288. nvd0_sor_disconnect(encoder);
  1289. if (nouveau_encoder(encoder)->dcb->type == DCB_OUTPUT_DP)
  1290. evo_sync(encoder->dev);
  1291. }
  1292. static void
  1293. nvd0_sor_commit(struct drm_encoder *encoder)
  1294. {
  1295. }
  1296. static void
  1297. nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  1298. struct drm_display_mode *mode)
  1299. {
  1300. struct drm_device *dev = encoder->dev;
  1301. struct nouveau_drm *drm = nouveau_drm(dev);
  1302. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1303. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1304. struct nouveau_connector *nv_connector;
  1305. struct nvbios *bios = &drm->vbios;
  1306. u32 mode_ctrl = (1 << nv_crtc->index);
  1307. u32 syncs, magic, *push;
  1308. u32 or_config;
  1309. syncs = 0x00000001;
  1310. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1311. syncs |= 0x00000008;
  1312. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1313. syncs |= 0x00000010;
  1314. magic = 0x31ec6000 | (nv_crtc->index << 25);
  1315. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1316. magic |= 0x00000001;
  1317. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1318. switch (nv_encoder->dcb->type) {
  1319. case DCB_OUTPUT_TMDS:
  1320. if (nv_encoder->dcb->sorconf.link & 1) {
  1321. if (mode->clock < 165000)
  1322. mode_ctrl |= 0x00000100;
  1323. else
  1324. mode_ctrl |= 0x00000500;
  1325. } else {
  1326. mode_ctrl |= 0x00000200;
  1327. }
  1328. or_config = (mode_ctrl & 0x00000f00) >> 8;
  1329. if (mode->clock >= 165000)
  1330. or_config |= 0x0100;
  1331. nvd0_hdmi_mode_set(encoder, mode);
  1332. break;
  1333. case DCB_OUTPUT_LVDS:
  1334. or_config = (mode_ctrl & 0x00000f00) >> 8;
  1335. if (bios->fp_no_ddc) {
  1336. if (bios->fp.dual_link)
  1337. or_config |= 0x0100;
  1338. if (bios->fp.if_is_24bit)
  1339. or_config |= 0x0200;
  1340. } else {
  1341. if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  1342. if (((u8 *)nv_connector->edid)[121] == 2)
  1343. or_config |= 0x0100;
  1344. } else
  1345. if (mode->clock >= bios->fp.duallink_transition_clk) {
  1346. or_config |= 0x0100;
  1347. }
  1348. if (or_config & 0x0100) {
  1349. if (bios->fp.strapless_is_24bit & 2)
  1350. or_config |= 0x0200;
  1351. } else {
  1352. if (bios->fp.strapless_is_24bit & 1)
  1353. or_config |= 0x0200;
  1354. }
  1355. if (nv_connector->base.display_info.bpc == 8)
  1356. or_config |= 0x0200;
  1357. }
  1358. break;
  1359. case DCB_OUTPUT_DP:
  1360. if (nv_connector->base.display_info.bpc == 6) {
  1361. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  1362. syncs |= 0x00000002 << 6;
  1363. } else {
  1364. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  1365. syncs |= 0x00000005 << 6;
  1366. }
  1367. if (nv_encoder->dcb->sorconf.link & 1)
  1368. mode_ctrl |= 0x00000800;
  1369. else
  1370. mode_ctrl |= 0x00000900;
  1371. or_config = (mode_ctrl & 0x00000f00) >> 8;
  1372. break;
  1373. default:
  1374. BUG_ON(1);
  1375. break;
  1376. }
  1377. nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  1378. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  1379. nvd0_sor_dp_calc_tu(dev, nv_encoder->dcb, nv_crtc->index,
  1380. nv_encoder->dp.datarate);
  1381. }
  1382. push = evo_wait(nvd0_mast(dev), 8);
  1383. if (push) {
  1384. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1385. evo_data(push, syncs);
  1386. evo_data(push, magic);
  1387. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x020), 2);
  1388. evo_data(push, mode_ctrl);
  1389. evo_data(push, or_config);
  1390. evo_kick(push, nvd0_mast(dev));
  1391. }
  1392. nv_encoder->crtc = encoder->crtc;
  1393. }
  1394. static void
  1395. nvd0_sor_destroy(struct drm_encoder *encoder)
  1396. {
  1397. drm_encoder_cleanup(encoder);
  1398. kfree(encoder);
  1399. }
  1400. static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
  1401. .dpms = nvd0_sor_dpms,
  1402. .mode_fixup = nvd0_sor_mode_fixup,
  1403. .prepare = nvd0_sor_prepare,
  1404. .commit = nvd0_sor_commit,
  1405. .mode_set = nvd0_sor_mode_set,
  1406. .disable = nvd0_sor_disconnect,
  1407. .get_crtc = nvd0_display_crtc_get,
  1408. };
  1409. static const struct drm_encoder_funcs nvd0_sor_func = {
  1410. .destroy = nvd0_sor_destroy,
  1411. };
  1412. static int
  1413. nvd0_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1414. {
  1415. struct drm_device *dev = connector->dev;
  1416. struct nouveau_encoder *nv_encoder;
  1417. struct drm_encoder *encoder;
  1418. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1419. if (!nv_encoder)
  1420. return -ENOMEM;
  1421. nv_encoder->dcb = dcbe;
  1422. nv_encoder->or = ffs(dcbe->or) - 1;
  1423. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1424. encoder = to_drm_encoder(nv_encoder);
  1425. encoder->possible_crtcs = dcbe->heads;
  1426. encoder->possible_clones = 0;
  1427. drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
  1428. drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
  1429. drm_mode_connector_attach_encoder(connector, encoder);
  1430. return 0;
  1431. }
  1432. /******************************************************************************
  1433. * Init
  1434. *****************************************************************************/
  1435. void
  1436. nvd0_display_fini(struct drm_device *dev)
  1437. {
  1438. }
  1439. int
  1440. nvd0_display_init(struct drm_device *dev)
  1441. {
  1442. u32 *push = evo_wait(nvd0_mast(dev), 32);
  1443. if (push) {
  1444. evo_mthd(push, 0x0088, 1);
  1445. evo_data(push, NvEvoSync);
  1446. evo_mthd(push, 0x0084, 1);
  1447. evo_data(push, 0x00000000);
  1448. evo_mthd(push, 0x0084, 1);
  1449. evo_data(push, 0x80000000);
  1450. evo_mthd(push, 0x008c, 1);
  1451. evo_data(push, 0x00000000);
  1452. evo_kick(push, nvd0_mast(dev));
  1453. return 0;
  1454. }
  1455. return -EBUSY;
  1456. }
  1457. void
  1458. nvd0_display_destroy(struct drm_device *dev)
  1459. {
  1460. struct nvd0_disp *disp = nvd0_disp(dev);
  1461. nvd0_dmac_destroy(disp->core, &disp->mast.base);
  1462. nouveau_bo_unmap(disp->sync);
  1463. nouveau_bo_ref(NULL, &disp->sync);
  1464. nouveau_display(dev)->priv = NULL;
  1465. kfree(disp);
  1466. }
  1467. int
  1468. nvd0_display_create(struct drm_device *dev)
  1469. {
  1470. static const u16 oclass[] = {
  1471. NVE0_DISP_CLASS,
  1472. NVD0_DISP_CLASS,
  1473. };
  1474. struct nouveau_device *device = nouveau_dev(dev);
  1475. struct nouveau_drm *drm = nouveau_drm(dev);
  1476. struct dcb_table *dcb = &drm->vbios.dcb;
  1477. struct drm_connector *connector, *tmp;
  1478. struct nvd0_disp *disp;
  1479. struct dcb_output *dcbe;
  1480. int crtcs, ret, i;
  1481. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  1482. if (!disp)
  1483. return -ENOMEM;
  1484. nouveau_display(dev)->priv = disp;
  1485. nouveau_display(dev)->dtor = nvd0_display_destroy;
  1486. nouveau_display(dev)->init = nvd0_display_init;
  1487. nouveau_display(dev)->fini = nvd0_display_fini;
  1488. /* small shared memory area we use for notifiers and semaphores */
  1489. ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
  1490. 0, 0x0000, NULL, &disp->sync);
  1491. if (!ret) {
  1492. ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
  1493. if (!ret)
  1494. ret = nouveau_bo_map(disp->sync);
  1495. if (ret)
  1496. nouveau_bo_ref(NULL, &disp->sync);
  1497. }
  1498. if (ret)
  1499. goto out;
  1500. /* attempt to allocate a supported evo display class */
  1501. ret = -ENODEV;
  1502. for (i = 0; ret && i < ARRAY_SIZE(oclass); i++) {
  1503. ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE,
  1504. 0xd1500000, oclass[i], NULL, 0,
  1505. &disp->core);
  1506. }
  1507. if (ret)
  1508. goto out;
  1509. /* allocate master evo channel */
  1510. ret = nvd0_dmac_create(disp->core, NV50_DISP_MAST_CLASS, 0,
  1511. &(struct nv50_display_mast_class) {
  1512. .pushbuf = EVO_PUSH_HANDLE(MAST, 0),
  1513. }, sizeof(struct nv50_display_mast_class),
  1514. disp->sync->bo.offset, &disp->mast.base);
  1515. if (ret)
  1516. goto out;
  1517. /* create crtc objects to represent the hw heads */
  1518. crtcs = nv_rd32(device, 0x022448);
  1519. for (i = 0; i < crtcs; i++) {
  1520. ret = nvd0_crtc_create(dev, disp->core, i);
  1521. if (ret)
  1522. goto out;
  1523. }
  1524. /* create encoder/connector objects based on VBIOS DCB table */
  1525. for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
  1526. connector = nouveau_connector_create(dev, dcbe->connector);
  1527. if (IS_ERR(connector))
  1528. continue;
  1529. if (dcbe->location != DCB_LOC_ON_CHIP) {
  1530. NV_WARN(drm, "skipping off-chip encoder %d/%d\n",
  1531. dcbe->type, ffs(dcbe->or) - 1);
  1532. continue;
  1533. }
  1534. switch (dcbe->type) {
  1535. case DCB_OUTPUT_TMDS:
  1536. case DCB_OUTPUT_LVDS:
  1537. case DCB_OUTPUT_DP:
  1538. nvd0_sor_create(connector, dcbe);
  1539. break;
  1540. case DCB_OUTPUT_ANALOG:
  1541. nvd0_dac_create(connector, dcbe);
  1542. break;
  1543. default:
  1544. NV_WARN(drm, "skipping unsupported encoder %d/%d\n",
  1545. dcbe->type, ffs(dcbe->or) - 1);
  1546. continue;
  1547. }
  1548. }
  1549. /* cull any connectors we created that don't have an encoder */
  1550. list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
  1551. if (connector->encoder_ids[0])
  1552. continue;
  1553. NV_WARN(drm, "%s has no encoders, removing\n",
  1554. drm_get_connector_name(connector));
  1555. connector->funcs->destroy(connector);
  1556. }
  1557. out:
  1558. if (ret)
  1559. nvd0_display_destroy(dev);
  1560. return ret;
  1561. }