nv50_display.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  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 nv50_chan {
  61. struct nouveau_object *user;
  62. u32 handle;
  63. };
  64. static int
  65. nv50_chan_create(struct nouveau_object *core, u32 bclass, u8 head,
  66. void *data, u32 size, struct nv50_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. nv50_chan_destroy(struct nouveau_object *core, struct nv50_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 nv50_pioc {
  90. struct nv50_chan base;
  91. };
  92. static void
  93. nv50_pioc_destroy(struct nouveau_object *core, struct nv50_pioc *pioc)
  94. {
  95. nv50_chan_destroy(core, &pioc->base);
  96. }
  97. static int
  98. nv50_pioc_create(struct nouveau_object *core, u32 bclass, u8 head,
  99. void *data, u32 size, struct nv50_pioc *pioc)
  100. {
  101. return nv50_chan_create(core, bclass, head, data, size, &pioc->base);
  102. }
  103. /******************************************************************************
  104. * DMA EVO channel
  105. *****************************************************************************/
  106. struct nv50_dmac {
  107. struct nv50_chan base;
  108. dma_addr_t handle;
  109. u32 *ptr;
  110. };
  111. static void
  112. nv50_dmac_destroy(struct nouveau_object *core, struct nv50_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. nv50_chan_destroy(core, &dmac->base);
  119. }
  120. static int
  121. nv50_dmac_create_fbdma(struct nouveau_object *core, u32 parent)
  122. {
  123. struct nouveau_fb *pfb = nouveau_fb(core);
  124. struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
  125. struct nouveau_object *object;
  126. int ret = nouveau_object_new(client, parent, NvEvoVRAM_LP,
  127. NV_DMA_IN_MEMORY_CLASS,
  128. &(struct nv_dma_class) {
  129. .flags = NV_DMA_TARGET_VRAM |
  130. NV_DMA_ACCESS_RDWR,
  131. .start = 0,
  132. .limit = pfb->ram.size - 1,
  133. .conf0 = NV50_DMA_CONF0_ENABLE |
  134. NV50_DMA_CONF0_PART_256,
  135. }, sizeof(struct nv_dma_class), &object);
  136. if (ret)
  137. return ret;
  138. ret = nouveau_object_new(client, parent, NvEvoFB16,
  139. NV_DMA_IN_MEMORY_CLASS,
  140. &(struct nv_dma_class) {
  141. .flags = NV_DMA_TARGET_VRAM |
  142. NV_DMA_ACCESS_RDWR,
  143. .start = 0,
  144. .limit = pfb->ram.size - 1,
  145. .conf0 = NV50_DMA_CONF0_ENABLE | 0x70 |
  146. NV50_DMA_CONF0_PART_256,
  147. }, sizeof(struct nv_dma_class), &object);
  148. if (ret)
  149. return ret;
  150. ret = nouveau_object_new(client, parent, NvEvoFB32,
  151. NV_DMA_IN_MEMORY_CLASS,
  152. &(struct nv_dma_class) {
  153. .flags = NV_DMA_TARGET_VRAM |
  154. NV_DMA_ACCESS_RDWR,
  155. .start = 0,
  156. .limit = pfb->ram.size - 1,
  157. .conf0 = NV50_DMA_CONF0_ENABLE | 0x7a |
  158. NV50_DMA_CONF0_PART_256,
  159. }, sizeof(struct nv_dma_class), &object);
  160. return ret;
  161. }
  162. static int
  163. nvc0_dmac_create_fbdma(struct nouveau_object *core, u32 parent)
  164. {
  165. struct nouveau_fb *pfb = nouveau_fb(core);
  166. struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
  167. struct nouveau_object *object;
  168. int ret = nouveau_object_new(client, parent, NvEvoVRAM_LP,
  169. NV_DMA_IN_MEMORY_CLASS,
  170. &(struct nv_dma_class) {
  171. .flags = NV_DMA_TARGET_VRAM |
  172. NV_DMA_ACCESS_RDWR,
  173. .start = 0,
  174. .limit = pfb->ram.size - 1,
  175. .conf0 = NVC0_DMA_CONF0_ENABLE,
  176. }, sizeof(struct nv_dma_class), &object);
  177. if (ret)
  178. return ret;
  179. ret = nouveau_object_new(client, parent, NvEvoFB16,
  180. NV_DMA_IN_MEMORY_CLASS,
  181. &(struct nv_dma_class) {
  182. .flags = NV_DMA_TARGET_VRAM |
  183. NV_DMA_ACCESS_RDWR,
  184. .start = 0,
  185. .limit = pfb->ram.size - 1,
  186. .conf0 = NVC0_DMA_CONF0_ENABLE | 0xfe,
  187. }, sizeof(struct nv_dma_class), &object);
  188. if (ret)
  189. return ret;
  190. ret = nouveau_object_new(client, parent, NvEvoFB32,
  191. NV_DMA_IN_MEMORY_CLASS,
  192. &(struct nv_dma_class) {
  193. .flags = NV_DMA_TARGET_VRAM |
  194. NV_DMA_ACCESS_RDWR,
  195. .start = 0,
  196. .limit = pfb->ram.size - 1,
  197. .conf0 = NVC0_DMA_CONF0_ENABLE | 0xfe,
  198. }, sizeof(struct nv_dma_class), &object);
  199. return ret;
  200. }
  201. static int
  202. nvd0_dmac_create_fbdma(struct nouveau_object *core, u32 parent)
  203. {
  204. struct nouveau_fb *pfb = nouveau_fb(core);
  205. struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
  206. struct nouveau_object *object;
  207. int ret = nouveau_object_new(client, parent, NvEvoVRAM_LP,
  208. NV_DMA_IN_MEMORY_CLASS,
  209. &(struct nv_dma_class) {
  210. .flags = NV_DMA_TARGET_VRAM |
  211. NV_DMA_ACCESS_RDWR,
  212. .start = 0,
  213. .limit = pfb->ram.size - 1,
  214. .conf0 = NVD0_DMA_CONF0_ENABLE |
  215. NVD0_DMA_CONF0_PAGE_LP,
  216. }, sizeof(struct nv_dma_class), &object);
  217. if (ret)
  218. return ret;
  219. ret = nouveau_object_new(client, parent, NvEvoFB32,
  220. NV_DMA_IN_MEMORY_CLASS,
  221. &(struct nv_dma_class) {
  222. .flags = NV_DMA_TARGET_VRAM |
  223. NV_DMA_ACCESS_RDWR,
  224. .start = 0,
  225. .limit = pfb->ram.size - 1,
  226. .conf0 = NVD0_DMA_CONF0_ENABLE | 0xfe |
  227. NVD0_DMA_CONF0_PAGE_LP,
  228. }, sizeof(struct nv_dma_class), &object);
  229. return ret;
  230. }
  231. static int
  232. nv50_dmac_create(struct nouveau_object *core, u32 bclass, u8 head,
  233. void *data, u32 size, u64 syncbuf,
  234. struct nv50_dmac *dmac)
  235. {
  236. struct nouveau_fb *pfb = nouveau_fb(core);
  237. struct nouveau_object *client = nv_pclass(core, NV_CLIENT_CLASS);
  238. struct nouveau_object *object;
  239. u32 pushbuf = *(u32 *)data;
  240. int ret;
  241. dmac->ptr = pci_alloc_consistent(nv_device(core)->pdev, PAGE_SIZE,
  242. &dmac->handle);
  243. if (!dmac->ptr)
  244. return -ENOMEM;
  245. ret = nouveau_object_new(client, NVDRM_DEVICE, pushbuf,
  246. NV_DMA_FROM_MEMORY_CLASS,
  247. &(struct nv_dma_class) {
  248. .flags = NV_DMA_TARGET_PCI_US |
  249. NV_DMA_ACCESS_RD,
  250. .start = dmac->handle + 0x0000,
  251. .limit = dmac->handle + 0x0fff,
  252. }, sizeof(struct nv_dma_class), &object);
  253. if (ret)
  254. return ret;
  255. ret = nv50_chan_create(core, bclass, head, data, size, &dmac->base);
  256. if (ret)
  257. return ret;
  258. ret = nouveau_object_new(client, dmac->base.handle, NvEvoSync,
  259. NV_DMA_IN_MEMORY_CLASS,
  260. &(struct nv_dma_class) {
  261. .flags = NV_DMA_TARGET_VRAM |
  262. NV_DMA_ACCESS_RDWR,
  263. .start = syncbuf + 0x0000,
  264. .limit = syncbuf + 0x0fff,
  265. }, sizeof(struct nv_dma_class), &object);
  266. if (ret)
  267. return ret;
  268. ret = nouveau_object_new(client, dmac->base.handle, NvEvoVRAM,
  269. NV_DMA_IN_MEMORY_CLASS,
  270. &(struct nv_dma_class) {
  271. .flags = NV_DMA_TARGET_VRAM |
  272. NV_DMA_ACCESS_RDWR,
  273. .start = 0,
  274. .limit = pfb->ram.size - 1,
  275. }, sizeof(struct nv_dma_class), &object);
  276. if (ret)
  277. return ret;
  278. if (nv_device(core)->card_type < NV_C0)
  279. ret = nv50_dmac_create_fbdma(core, dmac->base.handle);
  280. else
  281. if (nv_device(core)->card_type < NV_D0)
  282. ret = nvc0_dmac_create_fbdma(core, dmac->base.handle);
  283. else
  284. ret = nvd0_dmac_create_fbdma(core, dmac->base.handle);
  285. return ret;
  286. }
  287. struct nv50_mast {
  288. struct nv50_dmac base;
  289. };
  290. struct nv50_curs {
  291. struct nv50_pioc base;
  292. };
  293. struct nv50_sync {
  294. struct nv50_dmac base;
  295. struct {
  296. u32 offset;
  297. u16 value;
  298. } sem;
  299. };
  300. struct nv50_ovly {
  301. struct nv50_dmac base;
  302. };
  303. struct nv50_oimm {
  304. struct nv50_pioc base;
  305. };
  306. struct nv50_head {
  307. struct nouveau_crtc base;
  308. struct nv50_curs curs;
  309. struct nv50_sync sync;
  310. struct nv50_ovly ovly;
  311. struct nv50_oimm oimm;
  312. };
  313. #define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
  314. #define nv50_curs(c) (&nv50_head(c)->curs)
  315. #define nv50_sync(c) (&nv50_head(c)->sync)
  316. #define nv50_ovly(c) (&nv50_head(c)->ovly)
  317. #define nv50_oimm(c) (&nv50_head(c)->oimm)
  318. #define nv50_chan(c) (&(c)->base.base)
  319. #define nv50_vers(c) nv_mclass(nv50_chan(c)->user)
  320. struct nv50_disp {
  321. struct nouveau_object *core;
  322. struct nv50_mast mast;
  323. u32 modeset;
  324. struct nouveau_bo *sync;
  325. };
  326. static struct nv50_disp *
  327. nv50_disp(struct drm_device *dev)
  328. {
  329. return nouveau_display(dev)->priv;
  330. }
  331. #define nv50_mast(d) (&nv50_disp(d)->mast)
  332. static struct drm_crtc *
  333. nv50_display_crtc_get(struct drm_encoder *encoder)
  334. {
  335. return nouveau_encoder(encoder)->crtc;
  336. }
  337. /******************************************************************************
  338. * EVO channel helpers
  339. *****************************************************************************/
  340. static u32 *
  341. evo_wait(void *evoc, int nr)
  342. {
  343. struct nv50_dmac *dmac = evoc;
  344. u32 put = nv_ro32(dmac->base.user, 0x0000) / 4;
  345. if (put + nr >= (PAGE_SIZE / 4) - 8) {
  346. dmac->ptr[put] = 0x20000000;
  347. nv_wo32(dmac->base.user, 0x0000, 0x00000000);
  348. if (!nv_wait(dmac->base.user, 0x0004, ~0, 0x00000000)) {
  349. NV_ERROR(dmac->base.user, "channel stalled\n");
  350. return NULL;
  351. }
  352. put = 0;
  353. }
  354. return dmac->ptr + put;
  355. }
  356. static void
  357. evo_kick(u32 *push, void *evoc)
  358. {
  359. struct nv50_dmac *dmac = evoc;
  360. nv_wo32(dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
  361. }
  362. #define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
  363. #define evo_data(p,d) *((p)++) = (d)
  364. static bool
  365. evo_sync_wait(void *data)
  366. {
  367. return nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000;
  368. }
  369. static int
  370. evo_sync(struct drm_device *dev)
  371. {
  372. struct nouveau_device *device = nouveau_dev(dev);
  373. struct nv50_disp *disp = nv50_disp(dev);
  374. struct nv50_mast *mast = nv50_mast(dev);
  375. u32 *push = evo_wait(mast, 8);
  376. if (push) {
  377. nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
  378. evo_mthd(push, 0x0084, 1);
  379. evo_data(push, 0x80000000 | EVO_MAST_NTFY);
  380. evo_mthd(push, 0x0080, 2);
  381. evo_data(push, 0x00000000);
  382. evo_data(push, 0x00000000);
  383. evo_kick(push, mast);
  384. if (nv_wait_cb(device, evo_sync_wait, disp->sync))
  385. return 0;
  386. }
  387. return -EBUSY;
  388. }
  389. /******************************************************************************
  390. * Page flipping channel
  391. *****************************************************************************/
  392. struct nouveau_bo *
  393. nv50_display_crtc_sema(struct drm_device *dev, int crtc)
  394. {
  395. return nv50_disp(dev)->sync;
  396. }
  397. void
  398. nv50_display_flip_stop(struct drm_crtc *crtc)
  399. {
  400. struct nv50_sync *sync = nv50_sync(crtc);
  401. u32 *push;
  402. push = evo_wait(sync, 8);
  403. if (push) {
  404. evo_mthd(push, 0x0084, 1);
  405. evo_data(push, 0x00000000);
  406. evo_mthd(push, 0x0094, 1);
  407. evo_data(push, 0x00000000);
  408. evo_mthd(push, 0x00c0, 1);
  409. evo_data(push, 0x00000000);
  410. evo_mthd(push, 0x0080, 1);
  411. evo_data(push, 0x00000000);
  412. evo_kick(push, sync);
  413. }
  414. }
  415. int
  416. nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  417. struct nouveau_channel *chan, u32 swap_interval)
  418. {
  419. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  420. struct nv50_disp *disp = nv50_disp(crtc->dev);
  421. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  422. struct nv50_sync *sync = nv50_sync(crtc);
  423. u32 *push;
  424. int ret;
  425. swap_interval <<= 4;
  426. if (swap_interval == 0)
  427. swap_interval |= 0x100;
  428. push = evo_wait(sync, 128);
  429. if (unlikely(push == NULL))
  430. return -EBUSY;
  431. /* synchronise with the rendering channel, if necessary */
  432. if (likely(chan)) {
  433. ret = RING_SPACE(chan, 10);
  434. if (ret)
  435. return ret;
  436. if (nv_mclass(chan->object) < NV84_CHANNEL_IND_CLASS) {
  437. BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
  438. OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
  439. OUT_RING (chan, sync->sem.offset);
  440. BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
  441. OUT_RING (chan, 0xf00d0000 | sync->sem.value);
  442. BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
  443. OUT_RING (chan, sync->sem.offset ^ 0x10);
  444. OUT_RING (chan, 0x74b1e000);
  445. BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
  446. OUT_RING (chan, NvSema);
  447. } else
  448. if (nv_mclass(chan->object) < NVC0_CHANNEL_IND_CLASS) {
  449. u64 offset = nv84_fence_crtc(chan, nv_crtc->index);
  450. offset += sync->sem.offset;
  451. BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  452. OUT_RING (chan, upper_32_bits(offset));
  453. OUT_RING (chan, lower_32_bits(offset));
  454. OUT_RING (chan, 0xf00d0000 | sync->sem.value);
  455. OUT_RING (chan, 0x00000002);
  456. BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  457. OUT_RING (chan, upper_32_bits(offset));
  458. OUT_RING (chan, lower_32_bits(offset ^ 0x10));
  459. OUT_RING (chan, 0x74b1e000);
  460. OUT_RING (chan, 0x00000001);
  461. } else {
  462. u64 offset = nv84_fence_crtc(chan, nv_crtc->index);
  463. offset += sync->sem.offset;
  464. BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  465. OUT_RING (chan, upper_32_bits(offset));
  466. OUT_RING (chan, lower_32_bits(offset));
  467. OUT_RING (chan, 0xf00d0000 | sync->sem.value);
  468. OUT_RING (chan, 0x00001002);
  469. BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  470. OUT_RING (chan, upper_32_bits(offset));
  471. OUT_RING (chan, lower_32_bits(offset ^ 0x10));
  472. OUT_RING (chan, 0x74b1e000);
  473. OUT_RING (chan, 0x00001001);
  474. }
  475. FIRE_RING (chan);
  476. } else {
  477. nouveau_bo_wr32(disp->sync, sync->sem.offset / 4,
  478. 0xf00d0000 | sync->sem.value);
  479. evo_sync(crtc->dev);
  480. }
  481. /* queue the flip */
  482. evo_mthd(push, 0x0100, 1);
  483. evo_data(push, 0xfffe0000);
  484. evo_mthd(push, 0x0084, 1);
  485. evo_data(push, swap_interval);
  486. if (!(swap_interval & 0x00000100)) {
  487. evo_mthd(push, 0x00e0, 1);
  488. evo_data(push, 0x40000000);
  489. }
  490. evo_mthd(push, 0x0088, 4);
  491. evo_data(push, sync->sem.offset);
  492. evo_data(push, 0xf00d0000 | sync->sem.value);
  493. evo_data(push, 0x74b1e000);
  494. evo_data(push, NvEvoSync);
  495. evo_mthd(push, 0x00a0, 2);
  496. evo_data(push, 0x00000000);
  497. evo_data(push, 0x00000000);
  498. evo_mthd(push, 0x00c0, 1);
  499. evo_data(push, nv_fb->r_dma);
  500. evo_mthd(push, 0x0110, 2);
  501. evo_data(push, 0x00000000);
  502. evo_data(push, 0x00000000);
  503. if (nv50_vers(sync) < NVD0_DISP_SYNC_CLASS) {
  504. evo_mthd(push, 0x0800, 5);
  505. evo_data(push, nv_fb->nvbo->bo.offset >> 8);
  506. evo_data(push, 0);
  507. evo_data(push, (fb->height << 16) | fb->width);
  508. evo_data(push, nv_fb->r_pitch);
  509. evo_data(push, nv_fb->r_format);
  510. } else {
  511. evo_mthd(push, 0x0400, 5);
  512. evo_data(push, nv_fb->nvbo->bo.offset >> 8);
  513. evo_data(push, 0);
  514. evo_data(push, (fb->height << 16) | fb->width);
  515. evo_data(push, nv_fb->r_pitch);
  516. evo_data(push, nv_fb->r_format);
  517. }
  518. evo_mthd(push, 0x0080, 1);
  519. evo_data(push, 0x00000000);
  520. evo_kick(push, sync);
  521. sync->sem.offset ^= 0x10;
  522. sync->sem.value++;
  523. return 0;
  524. }
  525. /******************************************************************************
  526. * CRTC
  527. *****************************************************************************/
  528. static int
  529. nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
  530. {
  531. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  532. struct nouveau_connector *nv_connector;
  533. struct drm_connector *connector;
  534. u32 *push, mode = 0x00;
  535. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  536. connector = &nv_connector->base;
  537. if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
  538. if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
  539. mode = DITHERING_MODE_DYNAMIC2X2;
  540. } else {
  541. mode = nv_connector->dithering_mode;
  542. }
  543. if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
  544. if (connector->display_info.bpc >= 8)
  545. mode |= DITHERING_DEPTH_8BPC;
  546. } else {
  547. mode |= nv_connector->dithering_depth;
  548. }
  549. push = evo_wait(mast, 4);
  550. if (push) {
  551. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  552. evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
  553. evo_data(push, mode);
  554. } else
  555. if (nv50_vers(mast) < NVE0_DISP_MAST_CLASS) {
  556. evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
  557. evo_data(push, mode);
  558. } else {
  559. evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
  560. evo_data(push, mode);
  561. }
  562. if (update) {
  563. evo_mthd(push, 0x0080, 1);
  564. evo_data(push, 0x00000000);
  565. }
  566. evo_kick(push, mast);
  567. }
  568. return 0;
  569. }
  570. static int
  571. nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
  572. {
  573. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  574. struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
  575. struct drm_crtc *crtc = &nv_crtc->base;
  576. struct nouveau_connector *nv_connector;
  577. int mode = DRM_MODE_SCALE_NONE;
  578. u32 oX, oY, *push;
  579. /* start off at the resolution we programmed the crtc for, this
  580. * effectively handles NONE/FULL scaling
  581. */
  582. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  583. if (nv_connector && nv_connector->native_mode)
  584. mode = nv_connector->scaling_mode;
  585. if (mode != DRM_MODE_SCALE_NONE)
  586. omode = nv_connector->native_mode;
  587. else
  588. omode = umode;
  589. oX = omode->hdisplay;
  590. oY = omode->vdisplay;
  591. if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
  592. oY *= 2;
  593. /* add overscan compensation if necessary, will keep the aspect
  594. * ratio the same as the backend mode unless overridden by the
  595. * user setting both hborder and vborder properties.
  596. */
  597. if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
  598. (nv_connector->underscan == UNDERSCAN_AUTO &&
  599. nv_connector->edid &&
  600. drm_detect_hdmi_monitor(nv_connector->edid)))) {
  601. u32 bX = nv_connector->underscan_hborder;
  602. u32 bY = nv_connector->underscan_vborder;
  603. u32 aspect = (oY << 19) / oX;
  604. if (bX) {
  605. oX -= (bX * 2);
  606. if (bY) oY -= (bY * 2);
  607. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  608. } else {
  609. oX -= (oX >> 4) + 32;
  610. if (bY) oY -= (bY * 2);
  611. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  612. }
  613. }
  614. /* handle CENTER/ASPECT scaling, taking into account the areas
  615. * removed already for overscan compensation
  616. */
  617. switch (mode) {
  618. case DRM_MODE_SCALE_CENTER:
  619. oX = min((u32)umode->hdisplay, oX);
  620. oY = min((u32)umode->vdisplay, oY);
  621. /* fall-through */
  622. case DRM_MODE_SCALE_ASPECT:
  623. if (oY < oX) {
  624. u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
  625. oX = ((oY * aspect) + (aspect / 2)) >> 19;
  626. } else {
  627. u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
  628. oY = ((oX * aspect) + (aspect / 2)) >> 19;
  629. }
  630. break;
  631. default:
  632. break;
  633. }
  634. push = evo_wait(mast, 8);
  635. if (push) {
  636. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  637. /*XXX: SCALE_CTRL_ACTIVE??? */
  638. evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
  639. evo_data(push, (oY << 16) | oX);
  640. evo_data(push, (oY << 16) | oX);
  641. evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
  642. evo_data(push, 0x00000000);
  643. evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
  644. evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
  645. } else {
  646. evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
  647. evo_data(push, (oY << 16) | oX);
  648. evo_data(push, (oY << 16) | oX);
  649. evo_data(push, (oY << 16) | oX);
  650. evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
  651. evo_data(push, 0x00000000);
  652. evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
  653. evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
  654. }
  655. evo_kick(push, mast);
  656. if (update) {
  657. nv50_display_flip_stop(crtc);
  658. nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
  659. }
  660. }
  661. return 0;
  662. }
  663. static int
  664. nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
  665. {
  666. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  667. u32 *push, hue, vib;
  668. int adj;
  669. adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
  670. vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
  671. hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
  672. push = evo_wait(mast, 16);
  673. if (push) {
  674. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  675. evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
  676. evo_data(push, (hue << 20) | (vib << 8));
  677. } else {
  678. evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
  679. evo_data(push, (hue << 20) | (vib << 8));
  680. }
  681. if (update) {
  682. evo_mthd(push, 0x0080, 1);
  683. evo_data(push, 0x00000000);
  684. }
  685. evo_kick(push, mast);
  686. }
  687. return 0;
  688. }
  689. static int
  690. nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
  691. int x, int y, bool update)
  692. {
  693. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
  694. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  695. u32 *push;
  696. push = evo_wait(mast, 16);
  697. if (push) {
  698. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  699. evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
  700. evo_data(push, nvfb->nvbo->bo.offset >> 8);
  701. evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
  702. evo_data(push, (fb->height << 16) | fb->width);
  703. evo_data(push, nvfb->r_pitch);
  704. evo_data(push, nvfb->r_format);
  705. evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
  706. evo_data(push, (y << 16) | x);
  707. if (nv50_vers(mast) > NV50_DISP_MAST_CLASS) {
  708. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  709. evo_data(push, nvfb->r_dma);
  710. }
  711. } else {
  712. evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
  713. evo_data(push, nvfb->nvbo->bo.offset >> 8);
  714. evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
  715. evo_data(push, (fb->height << 16) | fb->width);
  716. evo_data(push, nvfb->r_pitch);
  717. evo_data(push, nvfb->r_format);
  718. evo_data(push, nvfb->r_dma);
  719. evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
  720. evo_data(push, (y << 16) | x);
  721. }
  722. if (update) {
  723. evo_mthd(push, 0x0080, 1);
  724. evo_data(push, 0x00000000);
  725. }
  726. evo_kick(push, mast);
  727. }
  728. nv_crtc->fb.tile_flags = nvfb->r_dma;
  729. return 0;
  730. }
  731. static void
  732. nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
  733. {
  734. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  735. u32 *push = evo_wait(mast, 16);
  736. if (push) {
  737. if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
  738. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
  739. evo_data(push, 0x85000000);
  740. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  741. } else
  742. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  743. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
  744. evo_data(push, 0x85000000);
  745. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  746. evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
  747. evo_data(push, NvEvoVRAM);
  748. } else {
  749. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
  750. evo_data(push, 0x85000000);
  751. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  752. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  753. evo_data(push, NvEvoVRAM);
  754. }
  755. evo_kick(push, mast);
  756. }
  757. }
  758. static void
  759. nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
  760. {
  761. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  762. u32 *push = evo_wait(mast, 16);
  763. if (push) {
  764. if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
  765. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
  766. evo_data(push, 0x05000000);
  767. } else
  768. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  769. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
  770. evo_data(push, 0x05000000);
  771. evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
  772. evo_data(push, 0x00000000);
  773. } else {
  774. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
  775. evo_data(push, 0x05000000);
  776. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  777. evo_data(push, 0x00000000);
  778. }
  779. evo_kick(push, mast);
  780. }
  781. }
  782. static void
  783. nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
  784. {
  785. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  786. if (show)
  787. nv50_crtc_cursor_show(nv_crtc);
  788. else
  789. nv50_crtc_cursor_hide(nv_crtc);
  790. if (update) {
  791. u32 *push = evo_wait(mast, 2);
  792. if (push) {
  793. evo_mthd(push, 0x0080, 1);
  794. evo_data(push, 0x00000000);
  795. evo_kick(push, mast);
  796. }
  797. }
  798. }
  799. static void
  800. nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
  801. {
  802. }
  803. static void
  804. nv50_crtc_prepare(struct drm_crtc *crtc)
  805. {
  806. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  807. struct nv50_mast *mast = nv50_mast(crtc->dev);
  808. u32 *push;
  809. nv50_display_flip_stop(crtc);
  810. push = evo_wait(mast, 2);
  811. if (push) {
  812. if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
  813. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  814. evo_data(push, 0x00000000);
  815. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
  816. evo_data(push, 0x40000000);
  817. } else
  818. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  819. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  820. evo_data(push, 0x00000000);
  821. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
  822. evo_data(push, 0x40000000);
  823. evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
  824. evo_data(push, 0x00000000);
  825. } else {
  826. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  827. evo_data(push, 0x00000000);
  828. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
  829. evo_data(push, 0x03000000);
  830. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  831. evo_data(push, 0x00000000);
  832. }
  833. evo_kick(push, mast);
  834. }
  835. nv50_crtc_cursor_show_hide(nv_crtc, false, false);
  836. }
  837. static void
  838. nv50_crtc_commit(struct drm_crtc *crtc)
  839. {
  840. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  841. struct nv50_mast *mast = nv50_mast(crtc->dev);
  842. u32 *push;
  843. push = evo_wait(mast, 32);
  844. if (push) {
  845. if (nv50_vers(mast) < NV84_DISP_MAST_CLASS) {
  846. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  847. evo_data(push, NvEvoVRAM_LP);
  848. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
  849. evo_data(push, 0xc0000000);
  850. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  851. } else
  852. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  853. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  854. evo_data(push, nv_crtc->fb.tile_flags);
  855. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
  856. evo_data(push, 0xc0000000);
  857. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  858. evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
  859. evo_data(push, NvEvoVRAM);
  860. } else {
  861. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  862. evo_data(push, nv_crtc->fb.tile_flags);
  863. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
  864. evo_data(push, 0x83000000);
  865. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  866. evo_data(push, 0x00000000);
  867. evo_data(push, 0x00000000);
  868. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  869. evo_data(push, NvEvoVRAM);
  870. evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
  871. evo_data(push, 0xffffff00);
  872. }
  873. evo_kick(push, mast);
  874. }
  875. nv50_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
  876. nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
  877. }
  878. static bool
  879. nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
  880. struct drm_display_mode *adjusted_mode)
  881. {
  882. return true;
  883. }
  884. static int
  885. nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
  886. {
  887. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
  888. int ret;
  889. ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
  890. if (ret)
  891. return ret;
  892. if (old_fb) {
  893. nvfb = nouveau_framebuffer(old_fb);
  894. nouveau_bo_unpin(nvfb->nvbo);
  895. }
  896. return 0;
  897. }
  898. static int
  899. nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
  900. struct drm_display_mode *mode, int x, int y,
  901. struct drm_framebuffer *old_fb)
  902. {
  903. struct nv50_mast *mast = nv50_mast(crtc->dev);
  904. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  905. struct nouveau_connector *nv_connector;
  906. u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
  907. u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
  908. u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
  909. u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
  910. u32 vblan2e = 0, vblan2s = 1;
  911. u32 *push;
  912. int ret;
  913. hactive = mode->htotal;
  914. hsynce = mode->hsync_end - mode->hsync_start - 1;
  915. hbackp = mode->htotal - mode->hsync_end;
  916. hblanke = hsynce + hbackp;
  917. hfrontp = mode->hsync_start - mode->hdisplay;
  918. hblanks = mode->htotal - hfrontp - 1;
  919. vactive = mode->vtotal * vscan / ilace;
  920. vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
  921. vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
  922. vblanke = vsynce + vbackp;
  923. vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
  924. vblanks = vactive - vfrontp - 1;
  925. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  926. vblan2e = vactive + vsynce + vbackp;
  927. vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
  928. vactive = (vactive * 2) + 1;
  929. }
  930. ret = nv50_crtc_swap_fbs(crtc, old_fb);
  931. if (ret)
  932. return ret;
  933. push = evo_wait(mast, 64);
  934. if (push) {
  935. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  936. evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
  937. evo_data(push, 0x00800000 | mode->clock);
  938. evo_data(push, (ilace == 2) ? 2 : 0);
  939. evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
  940. evo_data(push, 0x00000000);
  941. evo_data(push, (vactive << 16) | hactive);
  942. evo_data(push, ( vsynce << 16) | hsynce);
  943. evo_data(push, (vblanke << 16) | hblanke);
  944. evo_data(push, (vblanks << 16) | hblanks);
  945. evo_data(push, (vblan2e << 16) | vblan2s);
  946. evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
  947. evo_data(push, 0x00000000);
  948. evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
  949. evo_data(push, 0x00000311);
  950. evo_data(push, 0x00000100);
  951. } else {
  952. evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
  953. evo_data(push, 0x00000000);
  954. evo_data(push, (vactive << 16) | hactive);
  955. evo_data(push, ( vsynce << 16) | hsynce);
  956. evo_data(push, (vblanke << 16) | hblanke);
  957. evo_data(push, (vblanks << 16) | hblanks);
  958. evo_data(push, (vblan2e << 16) | vblan2s);
  959. evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
  960. evo_data(push, 0x00000000); /* ??? */
  961. evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
  962. evo_data(push, mode->clock * 1000);
  963. evo_data(push, 0x00200000); /* ??? */
  964. evo_data(push, mode->clock * 1000);
  965. evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
  966. evo_data(push, 0x00000311);
  967. evo_data(push, 0x00000100);
  968. }
  969. evo_kick(push, mast);
  970. }
  971. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  972. nv50_crtc_set_dither(nv_crtc, false);
  973. nv50_crtc_set_scale(nv_crtc, false);
  974. nv50_crtc_set_color_vibrance(nv_crtc, false);
  975. nv50_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
  976. return 0;
  977. }
  978. static int
  979. nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  980. struct drm_framebuffer *old_fb)
  981. {
  982. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  983. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  984. int ret;
  985. if (!crtc->fb) {
  986. NV_DEBUG(drm, "No FB bound\n");
  987. return 0;
  988. }
  989. ret = nv50_crtc_swap_fbs(crtc, old_fb);
  990. if (ret)
  991. return ret;
  992. nv50_display_flip_stop(crtc);
  993. nv50_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
  994. nv50_display_flip_next(crtc, crtc->fb, NULL, 1);
  995. return 0;
  996. }
  997. static int
  998. nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
  999. struct drm_framebuffer *fb, int x, int y,
  1000. enum mode_set_atomic state)
  1001. {
  1002. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1003. nv50_display_flip_stop(crtc);
  1004. nv50_crtc_set_image(nv_crtc, fb, x, y, true);
  1005. return 0;
  1006. }
  1007. static void
  1008. nv50_crtc_lut_load(struct drm_crtc *crtc)
  1009. {
  1010. struct nv50_disp *disp = nv50_disp(crtc->dev);
  1011. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1012. void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
  1013. int i;
  1014. for (i = 0; i < 256; i++) {
  1015. u16 r = nv_crtc->lut.r[i] >> 2;
  1016. u16 g = nv_crtc->lut.g[i] >> 2;
  1017. u16 b = nv_crtc->lut.b[i] >> 2;
  1018. if (nv_mclass(disp->core) < NVD0_DISP_CLASS) {
  1019. writew(r + 0x0000, lut + (i * 0x08) + 0);
  1020. writew(g + 0x0000, lut + (i * 0x08) + 2);
  1021. writew(b + 0x0000, lut + (i * 0x08) + 4);
  1022. } else {
  1023. writew(r + 0x6000, lut + (i * 0x20) + 0);
  1024. writew(g + 0x6000, lut + (i * 0x20) + 2);
  1025. writew(b + 0x6000, lut + (i * 0x20) + 4);
  1026. }
  1027. }
  1028. }
  1029. static int
  1030. nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  1031. uint32_t handle, uint32_t width, uint32_t height)
  1032. {
  1033. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1034. struct drm_device *dev = crtc->dev;
  1035. struct drm_gem_object *gem;
  1036. struct nouveau_bo *nvbo;
  1037. bool visible = (handle != 0);
  1038. int i, ret = 0;
  1039. if (visible) {
  1040. if (width != 64 || height != 64)
  1041. return -EINVAL;
  1042. gem = drm_gem_object_lookup(dev, file_priv, handle);
  1043. if (unlikely(!gem))
  1044. return -ENOENT;
  1045. nvbo = nouveau_gem_object(gem);
  1046. ret = nouveau_bo_map(nvbo);
  1047. if (ret == 0) {
  1048. for (i = 0; i < 64 * 64; i++) {
  1049. u32 v = nouveau_bo_rd32(nvbo, i);
  1050. nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
  1051. }
  1052. nouveau_bo_unmap(nvbo);
  1053. }
  1054. drm_gem_object_unreference_unlocked(gem);
  1055. }
  1056. if (visible != nv_crtc->cursor.visible) {
  1057. nv50_crtc_cursor_show_hide(nv_crtc, visible, true);
  1058. nv_crtc->cursor.visible = visible;
  1059. }
  1060. return ret;
  1061. }
  1062. static int
  1063. nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  1064. {
  1065. struct nv50_curs *curs = nv50_curs(crtc);
  1066. struct nv50_chan *chan = nv50_chan(curs);
  1067. nv_wo32(chan->user, 0x0084, (y << 16) | (x & 0xffff));
  1068. nv_wo32(chan->user, 0x0080, 0x00000000);
  1069. return 0;
  1070. }
  1071. static void
  1072. nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  1073. uint32_t start, uint32_t size)
  1074. {
  1075. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1076. u32 end = max(start + size, (u32)256);
  1077. u32 i;
  1078. for (i = start; i < end; i++) {
  1079. nv_crtc->lut.r[i] = r[i];
  1080. nv_crtc->lut.g[i] = g[i];
  1081. nv_crtc->lut.b[i] = b[i];
  1082. }
  1083. nv50_crtc_lut_load(crtc);
  1084. }
  1085. static void
  1086. nv50_crtc_destroy(struct drm_crtc *crtc)
  1087. {
  1088. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1089. struct nv50_disp *disp = nv50_disp(crtc->dev);
  1090. struct nv50_head *head = nv50_head(crtc);
  1091. nv50_dmac_destroy(disp->core, &head->ovly.base);
  1092. nv50_pioc_destroy(disp->core, &head->oimm.base);
  1093. nv50_dmac_destroy(disp->core, &head->sync.base);
  1094. nv50_pioc_destroy(disp->core, &head->curs.base);
  1095. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  1096. if (nv_crtc->cursor.nvbo)
  1097. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  1098. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  1099. nouveau_bo_unmap(nv_crtc->lut.nvbo);
  1100. if (nv_crtc->lut.nvbo)
  1101. nouveau_bo_unpin(nv_crtc->lut.nvbo);
  1102. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  1103. drm_crtc_cleanup(crtc);
  1104. kfree(crtc);
  1105. }
  1106. static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
  1107. .dpms = nv50_crtc_dpms,
  1108. .prepare = nv50_crtc_prepare,
  1109. .commit = nv50_crtc_commit,
  1110. .mode_fixup = nv50_crtc_mode_fixup,
  1111. .mode_set = nv50_crtc_mode_set,
  1112. .mode_set_base = nv50_crtc_mode_set_base,
  1113. .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
  1114. .load_lut = nv50_crtc_lut_load,
  1115. };
  1116. static const struct drm_crtc_funcs nv50_crtc_func = {
  1117. .cursor_set = nv50_crtc_cursor_set,
  1118. .cursor_move = nv50_crtc_cursor_move,
  1119. .gamma_set = nv50_crtc_gamma_set,
  1120. .set_config = drm_crtc_helper_set_config,
  1121. .destroy = nv50_crtc_destroy,
  1122. .page_flip = nouveau_crtc_page_flip,
  1123. };
  1124. static void
  1125. nv50_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
  1126. {
  1127. }
  1128. static void
  1129. nv50_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
  1130. {
  1131. }
  1132. static int
  1133. nv50_crtc_create(struct drm_device *dev, struct nouveau_object *core, int index)
  1134. {
  1135. struct nv50_disp *disp = nv50_disp(dev);
  1136. struct nv50_head *head;
  1137. struct drm_crtc *crtc;
  1138. int ret, i;
  1139. head = kzalloc(sizeof(*head), GFP_KERNEL);
  1140. if (!head)
  1141. return -ENOMEM;
  1142. head->base.index = index;
  1143. head->base.set_dither = nv50_crtc_set_dither;
  1144. head->base.set_scale = nv50_crtc_set_scale;
  1145. head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
  1146. head->base.color_vibrance = 50;
  1147. head->base.vibrant_hue = 0;
  1148. head->base.cursor.set_offset = nv50_cursor_set_offset;
  1149. head->base.cursor.set_pos = nv50_cursor_set_pos;
  1150. for (i = 0; i < 256; i++) {
  1151. head->base.lut.r[i] = i << 8;
  1152. head->base.lut.g[i] = i << 8;
  1153. head->base.lut.b[i] = i << 8;
  1154. }
  1155. crtc = &head->base.base;
  1156. drm_crtc_init(dev, crtc, &nv50_crtc_func);
  1157. drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
  1158. drm_mode_crtc_set_gamma_size(crtc, 256);
  1159. ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
  1160. 0, 0x0000, NULL, &head->base.lut.nvbo);
  1161. if (!ret) {
  1162. ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
  1163. if (!ret) {
  1164. ret = nouveau_bo_map(head->base.lut.nvbo);
  1165. if (ret)
  1166. nouveau_bo_unpin(head->base.lut.nvbo);
  1167. }
  1168. if (ret)
  1169. nouveau_bo_ref(NULL, &head->base.lut.nvbo);
  1170. }
  1171. if (ret)
  1172. goto out;
  1173. nv50_crtc_lut_load(crtc);
  1174. /* allocate cursor resources */
  1175. ret = nv50_pioc_create(disp->core, NV50_DISP_CURS_CLASS, index,
  1176. &(struct nv50_display_curs_class) {
  1177. .head = index,
  1178. }, sizeof(struct nv50_display_curs_class),
  1179. &head->curs.base);
  1180. if (ret)
  1181. goto out;
  1182. ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
  1183. 0, 0x0000, NULL, &head->base.cursor.nvbo);
  1184. if (!ret) {
  1185. ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
  1186. if (!ret) {
  1187. ret = nouveau_bo_map(head->base.cursor.nvbo);
  1188. if (ret)
  1189. nouveau_bo_unpin(head->base.lut.nvbo);
  1190. }
  1191. if (ret)
  1192. nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
  1193. }
  1194. if (ret)
  1195. goto out;
  1196. /* allocate page flip / sync resources */
  1197. ret = nv50_dmac_create(disp->core, NV50_DISP_SYNC_CLASS, index,
  1198. &(struct nv50_display_sync_class) {
  1199. .pushbuf = EVO_PUSH_HANDLE(SYNC, index),
  1200. .head = index,
  1201. }, sizeof(struct nv50_display_sync_class),
  1202. disp->sync->bo.offset, &head->sync.base);
  1203. if (ret)
  1204. goto out;
  1205. head->sync.sem.offset = EVO_SYNC(1 + index, 0x00);
  1206. /* allocate overlay resources */
  1207. ret = nv50_pioc_create(disp->core, NV50_DISP_OIMM_CLASS, index,
  1208. &(struct nv50_display_oimm_class) {
  1209. .head = index,
  1210. }, sizeof(struct nv50_display_oimm_class),
  1211. &head->oimm.base);
  1212. if (ret)
  1213. goto out;
  1214. ret = nv50_dmac_create(disp->core, NV50_DISP_OVLY_CLASS, index,
  1215. &(struct nv50_display_ovly_class) {
  1216. .pushbuf = EVO_PUSH_HANDLE(OVLY, index),
  1217. .head = index,
  1218. }, sizeof(struct nv50_display_ovly_class),
  1219. disp->sync->bo.offset, &head->ovly.base);
  1220. if (ret)
  1221. goto out;
  1222. out:
  1223. if (ret)
  1224. nv50_crtc_destroy(crtc);
  1225. return ret;
  1226. }
  1227. /******************************************************************************
  1228. * DAC
  1229. *****************************************************************************/
  1230. static void
  1231. nv50_dac_dpms(struct drm_encoder *encoder, int mode)
  1232. {
  1233. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1234. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1235. int or = nv_encoder->or;
  1236. u32 dpms_ctrl;
  1237. dpms_ctrl = 0x00000000;
  1238. if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
  1239. dpms_ctrl |= 0x00000001;
  1240. if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
  1241. dpms_ctrl |= 0x00000004;
  1242. nv_call(disp->core, NV50_DISP_DAC_PWR + or, dpms_ctrl);
  1243. }
  1244. static bool
  1245. nv50_dac_mode_fixup(struct drm_encoder *encoder,
  1246. const struct drm_display_mode *mode,
  1247. struct drm_display_mode *adjusted_mode)
  1248. {
  1249. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1250. struct nouveau_connector *nv_connector;
  1251. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1252. if (nv_connector && nv_connector->native_mode) {
  1253. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1254. int id = adjusted_mode->base.id;
  1255. *adjusted_mode = *nv_connector->native_mode;
  1256. adjusted_mode->base.id = id;
  1257. }
  1258. }
  1259. return true;
  1260. }
  1261. static void
  1262. nv50_dac_commit(struct drm_encoder *encoder)
  1263. {
  1264. }
  1265. static void
  1266. nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  1267. struct drm_display_mode *adjusted_mode)
  1268. {
  1269. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1270. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1271. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1272. u32 *push;
  1273. nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
  1274. push = evo_wait(mast, 8);
  1275. if (push) {
  1276. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  1277. u32 syncs = 0x00000000;
  1278. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1279. syncs |= 0x00000001;
  1280. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1281. syncs |= 0x00000002;
  1282. evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
  1283. evo_data(push, 1 << nv_crtc->index);
  1284. evo_data(push, syncs);
  1285. } else {
  1286. u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
  1287. u32 syncs = 0x00000001;
  1288. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1289. syncs |= 0x00000008;
  1290. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1291. syncs |= 0x00000010;
  1292. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1293. magic |= 0x00000001;
  1294. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1295. evo_data(push, syncs);
  1296. evo_data(push, magic);
  1297. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
  1298. evo_data(push, 1 << nv_crtc->index);
  1299. }
  1300. evo_kick(push, mast);
  1301. }
  1302. nv_encoder->crtc = encoder->crtc;
  1303. }
  1304. static void
  1305. nv50_dac_disconnect(struct drm_encoder *encoder)
  1306. {
  1307. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1308. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1309. const int or = nv_encoder->or;
  1310. u32 *push;
  1311. if (nv_encoder->crtc) {
  1312. nv50_crtc_prepare(nv_encoder->crtc);
  1313. push = evo_wait(mast, 4);
  1314. if (push) {
  1315. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  1316. evo_mthd(push, 0x0400 + (or * 0x080), 1);
  1317. evo_data(push, 0x00000000);
  1318. } else {
  1319. evo_mthd(push, 0x0180 + (or * 0x020), 1);
  1320. evo_data(push, 0x00000000);
  1321. }
  1322. evo_mthd(push, 0x0080, 1);
  1323. evo_data(push, 0x00000000);
  1324. evo_kick(push, mast);
  1325. }
  1326. }
  1327. nv_encoder->crtc = NULL;
  1328. }
  1329. static enum drm_connector_status
  1330. nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  1331. {
  1332. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1333. int ret, or = nouveau_encoder(encoder)->or;
  1334. u32 load = 0;
  1335. ret = nv_exec(disp->core, NV50_DISP_DAC_LOAD + or, &load, sizeof(load));
  1336. if (ret || load != 7)
  1337. return connector_status_disconnected;
  1338. return connector_status_connected;
  1339. }
  1340. static void
  1341. nv50_dac_destroy(struct drm_encoder *encoder)
  1342. {
  1343. drm_encoder_cleanup(encoder);
  1344. kfree(encoder);
  1345. }
  1346. static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
  1347. .dpms = nv50_dac_dpms,
  1348. .mode_fixup = nv50_dac_mode_fixup,
  1349. .prepare = nv50_dac_disconnect,
  1350. .commit = nv50_dac_commit,
  1351. .mode_set = nv50_dac_mode_set,
  1352. .disable = nv50_dac_disconnect,
  1353. .get_crtc = nv50_display_crtc_get,
  1354. .detect = nv50_dac_detect
  1355. };
  1356. static const struct drm_encoder_funcs nv50_dac_func = {
  1357. .destroy = nv50_dac_destroy,
  1358. };
  1359. static int
  1360. nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1361. {
  1362. struct drm_device *dev = connector->dev;
  1363. struct nouveau_encoder *nv_encoder;
  1364. struct drm_encoder *encoder;
  1365. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1366. if (!nv_encoder)
  1367. return -ENOMEM;
  1368. nv_encoder->dcb = dcbe;
  1369. nv_encoder->or = ffs(dcbe->or) - 1;
  1370. encoder = to_drm_encoder(nv_encoder);
  1371. encoder->possible_crtcs = dcbe->heads;
  1372. encoder->possible_clones = 0;
  1373. drm_encoder_init(dev, encoder, &nv50_dac_func, DRM_MODE_ENCODER_DAC);
  1374. drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
  1375. drm_mode_connector_attach_encoder(connector, encoder);
  1376. return 0;
  1377. }
  1378. /******************************************************************************
  1379. * Audio
  1380. *****************************************************************************/
  1381. static void
  1382. nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1383. {
  1384. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1385. struct nouveau_connector *nv_connector;
  1386. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1387. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1388. if (!drm_detect_monitor_audio(nv_connector->edid))
  1389. return;
  1390. drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
  1391. nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or,
  1392. nv_connector->base.eld,
  1393. nv_connector->base.eld[2] * 4);
  1394. }
  1395. static void
  1396. nv50_audio_disconnect(struct drm_encoder *encoder)
  1397. {
  1398. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1399. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1400. nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or, NULL, 0);
  1401. }
  1402. /******************************************************************************
  1403. * HDMI
  1404. *****************************************************************************/
  1405. static void
  1406. nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1407. {
  1408. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1409. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1410. struct nouveau_connector *nv_connector;
  1411. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1412. const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
  1413. u32 rekey = 56; /* binary driver, and tegra constant */
  1414. u32 max_ac_packet;
  1415. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1416. if (!drm_detect_hdmi_monitor(nv_connector->edid))
  1417. return;
  1418. max_ac_packet = mode->htotal - mode->hdisplay;
  1419. max_ac_packet -= rekey;
  1420. max_ac_packet -= 18; /* constant from tegra */
  1421. max_ac_packet /= 32;
  1422. nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff,
  1423. NV84_DISP_SOR_HDMI_PWR_STATE_ON |
  1424. (max_ac_packet << 16) | rekey);
  1425. nv50_audio_mode_set(encoder, mode);
  1426. }
  1427. static void
  1428. nv50_hdmi_disconnect(struct drm_encoder *encoder)
  1429. {
  1430. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1431. struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
  1432. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1433. const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
  1434. nv50_audio_disconnect(encoder);
  1435. nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff, 0x00000000);
  1436. }
  1437. /******************************************************************************
  1438. * SOR
  1439. *****************************************************************************/
  1440. static void
  1441. nv50_sor_dpms(struct drm_encoder *encoder, int mode)
  1442. {
  1443. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1444. struct drm_device *dev = encoder->dev;
  1445. struct nv50_disp *disp = nv50_disp(dev);
  1446. struct drm_encoder *partner;
  1447. int or = nv_encoder->or;
  1448. nv_encoder->last_dpms = mode;
  1449. list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
  1450. struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
  1451. if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
  1452. continue;
  1453. if (nv_partner != nv_encoder &&
  1454. nv_partner->dcb->or == nv_encoder->dcb->or) {
  1455. if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
  1456. return;
  1457. break;
  1458. }
  1459. }
  1460. nv_call(disp->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
  1461. if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
  1462. nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, disp->core);
  1463. }
  1464. static bool
  1465. nv50_sor_mode_fixup(struct drm_encoder *encoder,
  1466. const struct drm_display_mode *mode,
  1467. struct drm_display_mode *adjusted_mode)
  1468. {
  1469. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1470. struct nouveau_connector *nv_connector;
  1471. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1472. if (nv_connector && nv_connector->native_mode) {
  1473. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1474. int id = adjusted_mode->base.id;
  1475. *adjusted_mode = *nv_connector->native_mode;
  1476. adjusted_mode->base.id = id;
  1477. }
  1478. }
  1479. return true;
  1480. }
  1481. static void
  1482. nv50_sor_disconnect(struct drm_encoder *encoder)
  1483. {
  1484. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1485. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1486. const int or = nv_encoder->or;
  1487. u32 *push;
  1488. if (nv_encoder->crtc) {
  1489. nv50_crtc_prepare(nv_encoder->crtc);
  1490. push = evo_wait(mast, 4);
  1491. if (push) {
  1492. if (nv50_vers(mast) < NVD0_DISP_MAST_CLASS) {
  1493. evo_mthd(push, 0x0600 + (or * 0x40), 1);
  1494. evo_data(push, 0x00000000);
  1495. } else {
  1496. evo_mthd(push, 0x0200 + (or * 0x20), 1);
  1497. evo_data(push, 0x00000000);
  1498. }
  1499. evo_mthd(push, 0x0080, 1);
  1500. evo_data(push, 0x00000000);
  1501. evo_kick(push, mast);
  1502. }
  1503. nv50_hdmi_disconnect(encoder);
  1504. }
  1505. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1506. nv_encoder->crtc = NULL;
  1507. }
  1508. static void
  1509. nv50_sor_prepare(struct drm_encoder *encoder)
  1510. {
  1511. nv50_sor_disconnect(encoder);
  1512. if (nouveau_encoder(encoder)->dcb->type == DCB_OUTPUT_DP)
  1513. evo_sync(encoder->dev);
  1514. }
  1515. static void
  1516. nv50_sor_commit(struct drm_encoder *encoder)
  1517. {
  1518. }
  1519. static void
  1520. nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  1521. struct drm_display_mode *mode)
  1522. {
  1523. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1524. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1525. struct drm_device *dev = encoder->dev;
  1526. struct nouveau_drm *drm = nouveau_drm(dev);
  1527. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1528. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1529. struct nouveau_connector *nv_connector;
  1530. struct nvbios *bios = &drm->vbios;
  1531. u32 *push, lvds = 0;
  1532. u8 owner = 1 << nv_crtc->index;
  1533. u8 proto = 0xf;
  1534. u8 depth = 0x0;
  1535. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1536. switch (nv_encoder->dcb->type) {
  1537. case DCB_OUTPUT_TMDS:
  1538. if (nv_encoder->dcb->sorconf.link & 1) {
  1539. if (mode->clock < 165000)
  1540. proto = 0x1;
  1541. else
  1542. proto = 0x5;
  1543. } else {
  1544. proto = 0x2;
  1545. }
  1546. nv50_hdmi_mode_set(encoder, mode);
  1547. break;
  1548. case DCB_OUTPUT_LVDS:
  1549. proto = 0x0;
  1550. if (bios->fp_no_ddc) {
  1551. if (bios->fp.dual_link)
  1552. lvds |= 0x0100;
  1553. if (bios->fp.if_is_24bit)
  1554. lvds |= 0x0200;
  1555. } else {
  1556. if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  1557. if (((u8 *)nv_connector->edid)[121] == 2)
  1558. lvds |= 0x0100;
  1559. } else
  1560. if (mode->clock >= bios->fp.duallink_transition_clk) {
  1561. lvds |= 0x0100;
  1562. }
  1563. if (lvds & 0x0100) {
  1564. if (bios->fp.strapless_is_24bit & 2)
  1565. lvds |= 0x0200;
  1566. } else {
  1567. if (bios->fp.strapless_is_24bit & 1)
  1568. lvds |= 0x0200;
  1569. }
  1570. if (nv_connector->base.display_info.bpc == 8)
  1571. lvds |= 0x0200;
  1572. }
  1573. nv_call(disp->core, NV50_DISP_SOR_LVDS_SCRIPT + nv_encoder->or, lvds);
  1574. break;
  1575. case DCB_OUTPUT_DP:
  1576. if (nv_connector->base.display_info.bpc == 6) {
  1577. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  1578. depth = 0x2;
  1579. } else
  1580. if (nv_connector->base.display_info.bpc == 8) {
  1581. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  1582. depth = 0x5;
  1583. } else {
  1584. nv_encoder->dp.datarate = mode->clock * 30 / 8;
  1585. depth = 0x6;
  1586. }
  1587. if (nv_encoder->dcb->sorconf.link & 1)
  1588. proto = 0x8;
  1589. else
  1590. proto = 0x9;
  1591. break;
  1592. default:
  1593. BUG_ON(1);
  1594. break;
  1595. }
  1596. nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  1597. push = evo_wait(nv50_mast(dev), 8);
  1598. if (push) {
  1599. if (nv50_vers(mast) < NVD0_DISP_CLASS) {
  1600. u32 ctrl = (depth << 16) | (proto << 8) | owner;
  1601. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1602. ctrl |= 0x00001000;
  1603. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1604. ctrl |= 0x00002000;
  1605. evo_mthd(push, 0x0600 + (nv_encoder->or * 0x040), 1);
  1606. evo_data(push, ctrl);
  1607. } else {
  1608. u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
  1609. u32 syncs = 0x00000001;
  1610. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1611. syncs |= 0x00000008;
  1612. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1613. syncs |= 0x00000010;
  1614. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1615. magic |= 0x00000001;
  1616. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1617. evo_data(push, syncs | (depth << 6));
  1618. evo_data(push, magic);
  1619. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x020), 1);
  1620. evo_data(push, owner | (proto << 8));
  1621. }
  1622. evo_kick(push, mast);
  1623. }
  1624. nv_encoder->crtc = encoder->crtc;
  1625. }
  1626. static void
  1627. nv50_sor_destroy(struct drm_encoder *encoder)
  1628. {
  1629. drm_encoder_cleanup(encoder);
  1630. kfree(encoder);
  1631. }
  1632. static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
  1633. .dpms = nv50_sor_dpms,
  1634. .mode_fixup = nv50_sor_mode_fixup,
  1635. .prepare = nv50_sor_prepare,
  1636. .commit = nv50_sor_commit,
  1637. .mode_set = nv50_sor_mode_set,
  1638. .disable = nv50_sor_disconnect,
  1639. .get_crtc = nv50_display_crtc_get,
  1640. };
  1641. static const struct drm_encoder_funcs nv50_sor_func = {
  1642. .destroy = nv50_sor_destroy,
  1643. };
  1644. static int
  1645. nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1646. {
  1647. struct drm_device *dev = connector->dev;
  1648. struct nouveau_encoder *nv_encoder;
  1649. struct drm_encoder *encoder;
  1650. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1651. if (!nv_encoder)
  1652. return -ENOMEM;
  1653. nv_encoder->dcb = dcbe;
  1654. nv_encoder->or = ffs(dcbe->or) - 1;
  1655. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1656. encoder = to_drm_encoder(nv_encoder);
  1657. encoder->possible_crtcs = dcbe->heads;
  1658. encoder->possible_clones = 0;
  1659. drm_encoder_init(dev, encoder, &nv50_sor_func, DRM_MODE_ENCODER_TMDS);
  1660. drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
  1661. drm_mode_connector_attach_encoder(connector, encoder);
  1662. return 0;
  1663. }
  1664. /******************************************************************************
  1665. * Init
  1666. *****************************************************************************/
  1667. void
  1668. nv50_display_fini(struct drm_device *dev)
  1669. {
  1670. }
  1671. int
  1672. nv50_display_init(struct drm_device *dev)
  1673. {
  1674. u32 *push = evo_wait(nv50_mast(dev), 32);
  1675. if (push) {
  1676. evo_mthd(push, 0x0088, 1);
  1677. evo_data(push, NvEvoSync);
  1678. evo_kick(push, nv50_mast(dev));
  1679. return evo_sync(dev);
  1680. }
  1681. return -EBUSY;
  1682. }
  1683. void
  1684. nv50_display_destroy(struct drm_device *dev)
  1685. {
  1686. struct nv50_disp *disp = nv50_disp(dev);
  1687. nv50_dmac_destroy(disp->core, &disp->mast.base);
  1688. nouveau_bo_unmap(disp->sync);
  1689. if (disp->sync)
  1690. nouveau_bo_unpin(disp->sync);
  1691. nouveau_bo_ref(NULL, &disp->sync);
  1692. nouveau_display(dev)->priv = NULL;
  1693. kfree(disp);
  1694. }
  1695. int
  1696. nv50_display_create(struct drm_device *dev)
  1697. {
  1698. static const u16 oclass[] = {
  1699. NVE0_DISP_CLASS,
  1700. NVD0_DISP_CLASS,
  1701. NVA3_DISP_CLASS,
  1702. NV94_DISP_CLASS,
  1703. NVA0_DISP_CLASS,
  1704. NV84_DISP_CLASS,
  1705. NV50_DISP_CLASS,
  1706. };
  1707. struct nouveau_device *device = nouveau_dev(dev);
  1708. struct nouveau_drm *drm = nouveau_drm(dev);
  1709. struct dcb_table *dcb = &drm->vbios.dcb;
  1710. struct drm_connector *connector, *tmp;
  1711. struct nv50_disp *disp;
  1712. struct dcb_output *dcbe;
  1713. int crtcs, ret, i;
  1714. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  1715. if (!disp)
  1716. return -ENOMEM;
  1717. nouveau_display(dev)->priv = disp;
  1718. nouveau_display(dev)->dtor = nv50_display_destroy;
  1719. nouveau_display(dev)->init = nv50_display_init;
  1720. nouveau_display(dev)->fini = nv50_display_fini;
  1721. /* small shared memory area we use for notifiers and semaphores */
  1722. ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
  1723. 0, 0x0000, NULL, &disp->sync);
  1724. if (!ret) {
  1725. ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
  1726. if (!ret) {
  1727. ret = nouveau_bo_map(disp->sync);
  1728. if (ret)
  1729. nouveau_bo_unpin(disp->sync);
  1730. }
  1731. if (ret)
  1732. nouveau_bo_ref(NULL, &disp->sync);
  1733. }
  1734. if (ret)
  1735. goto out;
  1736. /* attempt to allocate a supported evo display class */
  1737. ret = -ENODEV;
  1738. for (i = 0; ret && i < ARRAY_SIZE(oclass); i++) {
  1739. ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE,
  1740. 0xd1500000, oclass[i], NULL, 0,
  1741. &disp->core);
  1742. }
  1743. if (ret)
  1744. goto out;
  1745. /* allocate master evo channel */
  1746. ret = nv50_dmac_create(disp->core, NV50_DISP_MAST_CLASS, 0,
  1747. &(struct nv50_display_mast_class) {
  1748. .pushbuf = EVO_PUSH_HANDLE(MAST, 0),
  1749. }, sizeof(struct nv50_display_mast_class),
  1750. disp->sync->bo.offset, &disp->mast.base);
  1751. if (ret)
  1752. goto out;
  1753. /* create crtc objects to represent the hw heads */
  1754. if (nv_mclass(disp->core) >= NVD0_DISP_CLASS)
  1755. crtcs = nv_rd32(device, 0x022448);
  1756. else
  1757. crtcs = 2;
  1758. for (i = 0; i < crtcs; i++) {
  1759. ret = nv50_crtc_create(dev, disp->core, i);
  1760. if (ret)
  1761. goto out;
  1762. }
  1763. /* create encoder/connector objects based on VBIOS DCB table */
  1764. for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
  1765. connector = nouveau_connector_create(dev, dcbe->connector);
  1766. if (IS_ERR(connector))
  1767. continue;
  1768. if (dcbe->location != DCB_LOC_ON_CHIP) {
  1769. NV_WARN(drm, "skipping off-chip encoder %d/%d\n",
  1770. dcbe->type, ffs(dcbe->or) - 1);
  1771. continue;
  1772. }
  1773. switch (dcbe->type) {
  1774. case DCB_OUTPUT_TMDS:
  1775. case DCB_OUTPUT_LVDS:
  1776. case DCB_OUTPUT_DP:
  1777. nv50_sor_create(connector, dcbe);
  1778. break;
  1779. case DCB_OUTPUT_ANALOG:
  1780. nv50_dac_create(connector, dcbe);
  1781. break;
  1782. default:
  1783. NV_WARN(drm, "skipping unsupported encoder %d/%d\n",
  1784. dcbe->type, ffs(dcbe->or) - 1);
  1785. continue;
  1786. }
  1787. }
  1788. /* cull any connectors we created that don't have an encoder */
  1789. list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
  1790. if (connector->encoder_ids[0])
  1791. continue;
  1792. NV_WARN(drm, "%s has no encoders, removing\n",
  1793. drm_get_connector_name(connector));
  1794. connector->funcs->destroy(connector);
  1795. }
  1796. out:
  1797. if (ret)
  1798. nv50_display_destroy(dev);
  1799. return ret;
  1800. }