nv50_display.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  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 <drm/drm_dp_helper.h>
  28. #include <nvif/class.h>
  29. #include "nouveau_drm.h"
  30. #include "nouveau_dma.h"
  31. #include "nouveau_gem.h"
  32. #include "nouveau_connector.h"
  33. #include "nouveau_encoder.h"
  34. #include "nouveau_crtc.h"
  35. #include "nouveau_fence.h"
  36. #include "nv50_display.h"
  37. #define EVO_DMA_NR 9
  38. #define EVO_MASTER (0x00)
  39. #define EVO_FLIP(c) (0x01 + (c))
  40. #define EVO_OVLY(c) (0x05 + (c))
  41. #define EVO_OIMM(c) (0x09 + (c))
  42. #define EVO_CURS(c) (0x0d + (c))
  43. /* offsets in shared sync bo of various structures */
  44. #define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
  45. #define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
  46. #define EVO_FLIP_SEM0(c) EVO_SYNC((c) + 1, 0x00)
  47. #define EVO_FLIP_SEM1(c) EVO_SYNC((c) + 1, 0x10)
  48. /******************************************************************************
  49. * EVO channel
  50. *****************************************************************************/
  51. struct nv50_chan {
  52. struct nvif_object user;
  53. };
  54. static int
  55. nv50_chan_create(struct nvif_object *disp, const u32 *oclass, u8 head,
  56. void *data, u32 size, struct nv50_chan *chan)
  57. {
  58. while (oclass[0]) {
  59. int ret = nvif_object_init(disp, NULL, (oclass[0] << 16) | head,
  60. oclass[0], data, size,
  61. &chan->user);
  62. if (oclass++, ret == 0) {
  63. nvif_object_map(&chan->user);
  64. return ret;
  65. }
  66. }
  67. return -ENOSYS;
  68. }
  69. static void
  70. nv50_chan_destroy(struct nv50_chan *chan)
  71. {
  72. nvif_object_fini(&chan->user);
  73. }
  74. /******************************************************************************
  75. * PIO EVO channel
  76. *****************************************************************************/
  77. struct nv50_pioc {
  78. struct nv50_chan base;
  79. };
  80. static void
  81. nv50_pioc_destroy(struct nv50_pioc *pioc)
  82. {
  83. nv50_chan_destroy(&pioc->base);
  84. }
  85. static int
  86. nv50_pioc_create(struct nvif_object *disp, const u32 *oclass, u8 head,
  87. void *data, u32 size, struct nv50_pioc *pioc)
  88. {
  89. return nv50_chan_create(disp, oclass, head, data, size, &pioc->base);
  90. }
  91. /******************************************************************************
  92. * Cursor Immediate
  93. *****************************************************************************/
  94. struct nv50_curs {
  95. struct nv50_pioc base;
  96. };
  97. static int
  98. nv50_curs_create(struct nvif_object *disp, int head, struct nv50_curs *curs)
  99. {
  100. struct nv50_disp_cursor_v0 args = {
  101. .head = head,
  102. };
  103. static const u32 oclass[] = {
  104. GK104_DISP_CURSOR,
  105. GF110_DISP_CURSOR,
  106. GT214_DISP_CURSOR,
  107. G82_DISP_CURSOR,
  108. NV50_DISP_CURSOR,
  109. 0
  110. };
  111. return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
  112. &curs->base);
  113. }
  114. /******************************************************************************
  115. * Overlay Immediate
  116. *****************************************************************************/
  117. struct nv50_oimm {
  118. struct nv50_pioc base;
  119. };
  120. static int
  121. nv50_oimm_create(struct nvif_object *disp, int head, struct nv50_oimm *oimm)
  122. {
  123. struct nv50_disp_cursor_v0 args = {
  124. .head = head,
  125. };
  126. static const u32 oclass[] = {
  127. GK104_DISP_OVERLAY,
  128. GF110_DISP_OVERLAY,
  129. GT214_DISP_OVERLAY,
  130. G82_DISP_OVERLAY,
  131. NV50_DISP_OVERLAY,
  132. 0
  133. };
  134. return nv50_pioc_create(disp, oclass, head, &args, sizeof(args),
  135. &oimm->base);
  136. }
  137. /******************************************************************************
  138. * DMA EVO channel
  139. *****************************************************************************/
  140. struct nv50_dmac {
  141. struct nv50_chan base;
  142. dma_addr_t handle;
  143. u32 *ptr;
  144. struct nvif_object sync;
  145. struct nvif_object vram;
  146. /* Protects against concurrent pushbuf access to this channel, lock is
  147. * grabbed by evo_wait (if the pushbuf reservation is successful) and
  148. * dropped again by evo_kick. */
  149. struct mutex lock;
  150. };
  151. static void
  152. nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
  153. {
  154. nvif_object_fini(&dmac->vram);
  155. nvif_object_fini(&dmac->sync);
  156. nv50_chan_destroy(&dmac->base);
  157. if (dmac->ptr) {
  158. struct pci_dev *pdev = nvkm_device(nvif_device(disp))->pdev;
  159. pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
  160. }
  161. }
  162. static int
  163. nv50_dmac_create(struct nvif_object *disp, const u32 *oclass, u8 head,
  164. void *data, u32 size, u64 syncbuf,
  165. struct nv50_dmac *dmac)
  166. {
  167. struct nvif_device *device = nvif_device(disp);
  168. struct nv50_disp_core_channel_dma_v0 *args = data;
  169. struct nvif_object pushbuf;
  170. int ret;
  171. mutex_init(&dmac->lock);
  172. dmac->ptr = pci_alloc_consistent(nvkm_device(device)->pdev,
  173. PAGE_SIZE, &dmac->handle);
  174. if (!dmac->ptr)
  175. return -ENOMEM;
  176. ret = nvif_object_init(nvif_object(device), NULL,
  177. args->pushbuf, NV_DMA_FROM_MEMORY,
  178. &(struct nv_dma_v0) {
  179. .target = NV_DMA_V0_TARGET_PCI_US,
  180. .access = NV_DMA_V0_ACCESS_RD,
  181. .start = dmac->handle + 0x0000,
  182. .limit = dmac->handle + 0x0fff,
  183. }, sizeof(struct nv_dma_v0), &pushbuf);
  184. if (ret)
  185. return ret;
  186. ret = nv50_chan_create(disp, oclass, head, data, size, &dmac->base);
  187. nvif_object_fini(&pushbuf);
  188. if (ret)
  189. return ret;
  190. ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000000,
  191. NV_DMA_IN_MEMORY,
  192. &(struct nv_dma_v0) {
  193. .target = NV_DMA_V0_TARGET_VRAM,
  194. .access = NV_DMA_V0_ACCESS_RDWR,
  195. .start = syncbuf + 0x0000,
  196. .limit = syncbuf + 0x0fff,
  197. }, sizeof(struct nv_dma_v0),
  198. &dmac->sync);
  199. if (ret)
  200. return ret;
  201. ret = nvif_object_init(&dmac->base.user, NULL, 0xf0000001,
  202. NV_DMA_IN_MEMORY,
  203. &(struct nv_dma_v0) {
  204. .target = NV_DMA_V0_TARGET_VRAM,
  205. .access = NV_DMA_V0_ACCESS_RDWR,
  206. .start = 0,
  207. .limit = device->info.ram_user - 1,
  208. }, sizeof(struct nv_dma_v0),
  209. &dmac->vram);
  210. if (ret)
  211. return ret;
  212. return ret;
  213. }
  214. /******************************************************************************
  215. * Core
  216. *****************************************************************************/
  217. struct nv50_mast {
  218. struct nv50_dmac base;
  219. };
  220. static int
  221. nv50_core_create(struct nvif_object *disp, u64 syncbuf, struct nv50_mast *core)
  222. {
  223. struct nv50_disp_core_channel_dma_v0 args = {
  224. .pushbuf = 0xb0007d00,
  225. };
  226. static const u32 oclass[] = {
  227. GM107_DISP_CORE_CHANNEL_DMA,
  228. GK110_DISP_CORE_CHANNEL_DMA,
  229. GK104_DISP_CORE_CHANNEL_DMA,
  230. GF110_DISP_CORE_CHANNEL_DMA,
  231. GT214_DISP_CORE_CHANNEL_DMA,
  232. GT206_DISP_CORE_CHANNEL_DMA,
  233. GT200_DISP_CORE_CHANNEL_DMA,
  234. G82_DISP_CORE_CHANNEL_DMA,
  235. NV50_DISP_CORE_CHANNEL_DMA,
  236. 0
  237. };
  238. return nv50_dmac_create(disp, oclass, 0, &args, sizeof(args), syncbuf,
  239. &core->base);
  240. }
  241. /******************************************************************************
  242. * Base
  243. *****************************************************************************/
  244. struct nv50_sync {
  245. struct nv50_dmac base;
  246. u32 addr;
  247. u32 data;
  248. };
  249. static int
  250. nv50_base_create(struct nvif_object *disp, int head, u64 syncbuf,
  251. struct nv50_sync *base)
  252. {
  253. struct nv50_disp_base_channel_dma_v0 args = {
  254. .pushbuf = 0xb0007c00 | head,
  255. .head = head,
  256. };
  257. static const u32 oclass[] = {
  258. GK110_DISP_BASE_CHANNEL_DMA,
  259. GK104_DISP_BASE_CHANNEL_DMA,
  260. GF110_DISP_BASE_CHANNEL_DMA,
  261. GT214_DISP_BASE_CHANNEL_DMA,
  262. GT200_DISP_BASE_CHANNEL_DMA,
  263. G82_DISP_BASE_CHANNEL_DMA,
  264. NV50_DISP_BASE_CHANNEL_DMA,
  265. 0
  266. };
  267. return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
  268. syncbuf, &base->base);
  269. }
  270. /******************************************************************************
  271. * Overlay
  272. *****************************************************************************/
  273. struct nv50_ovly {
  274. struct nv50_dmac base;
  275. };
  276. static int
  277. nv50_ovly_create(struct nvif_object *disp, int head, u64 syncbuf,
  278. struct nv50_ovly *ovly)
  279. {
  280. struct nv50_disp_overlay_channel_dma_v0 args = {
  281. .pushbuf = 0xb0007e00 | head,
  282. .head = head,
  283. };
  284. static const u32 oclass[] = {
  285. GK104_DISP_OVERLAY_CONTROL_DMA,
  286. GF110_DISP_OVERLAY_CONTROL_DMA,
  287. GT214_DISP_OVERLAY_CHANNEL_DMA,
  288. GT200_DISP_OVERLAY_CHANNEL_DMA,
  289. G82_DISP_OVERLAY_CHANNEL_DMA,
  290. NV50_DISP_OVERLAY_CHANNEL_DMA,
  291. 0
  292. };
  293. return nv50_dmac_create(disp, oclass, head, &args, sizeof(args),
  294. syncbuf, &ovly->base);
  295. }
  296. struct nv50_head {
  297. struct nouveau_crtc base;
  298. struct nouveau_bo *image;
  299. struct nv50_curs curs;
  300. struct nv50_sync sync;
  301. struct nv50_ovly ovly;
  302. struct nv50_oimm oimm;
  303. };
  304. #define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
  305. #define nv50_curs(c) (&nv50_head(c)->curs)
  306. #define nv50_sync(c) (&nv50_head(c)->sync)
  307. #define nv50_ovly(c) (&nv50_head(c)->ovly)
  308. #define nv50_oimm(c) (&nv50_head(c)->oimm)
  309. #define nv50_chan(c) (&(c)->base.base)
  310. #define nv50_vers(c) nv50_chan(c)->user.oclass
  311. struct nv50_fbdma {
  312. struct list_head head;
  313. struct nvif_object core;
  314. struct nvif_object base[4];
  315. };
  316. struct nv50_disp {
  317. struct nvif_object *disp;
  318. struct nv50_mast mast;
  319. struct list_head fbdma;
  320. struct nouveau_bo *sync;
  321. };
  322. static struct nv50_disp *
  323. nv50_disp(struct drm_device *dev)
  324. {
  325. return nouveau_display(dev)->priv;
  326. }
  327. #define nv50_mast(d) (&nv50_disp(d)->mast)
  328. static struct drm_crtc *
  329. nv50_display_crtc_get(struct drm_encoder *encoder)
  330. {
  331. return nouveau_encoder(encoder)->crtc;
  332. }
  333. /******************************************************************************
  334. * EVO channel helpers
  335. *****************************************************************************/
  336. static u32 *
  337. evo_wait(void *evoc, int nr)
  338. {
  339. struct nv50_dmac *dmac = evoc;
  340. u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
  341. mutex_lock(&dmac->lock);
  342. if (put + nr >= (PAGE_SIZE / 4) - 8) {
  343. dmac->ptr[put] = 0x20000000;
  344. nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
  345. if (!nvkm_wait(&dmac->base.user, 0x0004, ~0, 0x00000000)) {
  346. mutex_unlock(&dmac->lock);
  347. nv_error(nvkm_object(&dmac->base.user), "channel stalled\n");
  348. return NULL;
  349. }
  350. put = 0;
  351. }
  352. return dmac->ptr + put;
  353. }
  354. static void
  355. evo_kick(u32 *push, void *evoc)
  356. {
  357. struct nv50_dmac *dmac = evoc;
  358. nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
  359. mutex_unlock(&dmac->lock);
  360. }
  361. #define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
  362. #define evo_data(p,d) *((p)++) = (d)
  363. static bool
  364. evo_sync_wait(void *data)
  365. {
  366. if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
  367. return true;
  368. usleep_range(1, 2);
  369. return false;
  370. }
  371. static int
  372. evo_sync(struct drm_device *dev)
  373. {
  374. struct nvif_device *device = &nouveau_drm(dev)->device;
  375. struct nv50_disp *disp = nv50_disp(dev);
  376. struct nv50_mast *mast = nv50_mast(dev);
  377. u32 *push = evo_wait(mast, 8);
  378. if (push) {
  379. nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
  380. evo_mthd(push, 0x0084, 1);
  381. evo_data(push, 0x80000000 | EVO_MAST_NTFY);
  382. evo_mthd(push, 0x0080, 2);
  383. evo_data(push, 0x00000000);
  384. evo_data(push, 0x00000000);
  385. evo_kick(push, mast);
  386. if (nv_wait_cb(nvkm_device(device), evo_sync_wait, disp->sync))
  387. return 0;
  388. }
  389. return -EBUSY;
  390. }
  391. /******************************************************************************
  392. * Page flipping channel
  393. *****************************************************************************/
  394. struct nouveau_bo *
  395. nv50_display_crtc_sema(struct drm_device *dev, int crtc)
  396. {
  397. return nv50_disp(dev)->sync;
  398. }
  399. struct nv50_display_flip {
  400. struct nv50_disp *disp;
  401. struct nv50_sync *chan;
  402. };
  403. static bool
  404. nv50_display_flip_wait(void *data)
  405. {
  406. struct nv50_display_flip *flip = data;
  407. if (nouveau_bo_rd32(flip->disp->sync, flip->chan->addr / 4) ==
  408. flip->chan->data)
  409. return true;
  410. usleep_range(1, 2);
  411. return false;
  412. }
  413. void
  414. nv50_display_flip_stop(struct drm_crtc *crtc)
  415. {
  416. struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
  417. struct nv50_display_flip flip = {
  418. .disp = nv50_disp(crtc->dev),
  419. .chan = nv50_sync(crtc),
  420. };
  421. u32 *push;
  422. push = evo_wait(flip.chan, 8);
  423. if (push) {
  424. evo_mthd(push, 0x0084, 1);
  425. evo_data(push, 0x00000000);
  426. evo_mthd(push, 0x0094, 1);
  427. evo_data(push, 0x00000000);
  428. evo_mthd(push, 0x00c0, 1);
  429. evo_data(push, 0x00000000);
  430. evo_mthd(push, 0x0080, 1);
  431. evo_data(push, 0x00000000);
  432. evo_kick(push, flip.chan);
  433. }
  434. nv_wait_cb(nvkm_device(device), nv50_display_flip_wait, &flip);
  435. }
  436. int
  437. nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  438. struct nouveau_channel *chan, u32 swap_interval)
  439. {
  440. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  441. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  442. struct nv50_head *head = nv50_head(crtc);
  443. struct nv50_sync *sync = nv50_sync(crtc);
  444. u32 *push;
  445. int ret;
  446. swap_interval <<= 4;
  447. if (swap_interval == 0)
  448. swap_interval |= 0x100;
  449. if (chan == NULL)
  450. evo_sync(crtc->dev);
  451. push = evo_wait(sync, 128);
  452. if (unlikely(push == NULL))
  453. return -EBUSY;
  454. if (chan && chan->object->oclass < G82_CHANNEL_GPFIFO) {
  455. ret = RING_SPACE(chan, 8);
  456. if (ret)
  457. return ret;
  458. BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
  459. OUT_RING (chan, NvEvoSema0 + nv_crtc->index);
  460. OUT_RING (chan, sync->addr ^ 0x10);
  461. BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
  462. OUT_RING (chan, sync->data + 1);
  463. BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
  464. OUT_RING (chan, sync->addr);
  465. OUT_RING (chan, sync->data);
  466. } else
  467. if (chan && chan->object->oclass < FERMI_CHANNEL_GPFIFO) {
  468. u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
  469. ret = RING_SPACE(chan, 12);
  470. if (ret)
  471. return ret;
  472. BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
  473. OUT_RING (chan, chan->vram.handle);
  474. BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  475. OUT_RING (chan, upper_32_bits(addr ^ 0x10));
  476. OUT_RING (chan, lower_32_bits(addr ^ 0x10));
  477. OUT_RING (chan, sync->data + 1);
  478. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
  479. BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  480. OUT_RING (chan, upper_32_bits(addr));
  481. OUT_RING (chan, lower_32_bits(addr));
  482. OUT_RING (chan, sync->data);
  483. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
  484. } else
  485. if (chan) {
  486. u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + sync->addr;
  487. ret = RING_SPACE(chan, 10);
  488. if (ret)
  489. return ret;
  490. BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  491. OUT_RING (chan, upper_32_bits(addr ^ 0x10));
  492. OUT_RING (chan, lower_32_bits(addr ^ 0x10));
  493. OUT_RING (chan, sync->data + 1);
  494. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
  495. NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
  496. BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  497. OUT_RING (chan, upper_32_bits(addr));
  498. OUT_RING (chan, lower_32_bits(addr));
  499. OUT_RING (chan, sync->data);
  500. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
  501. NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
  502. }
  503. if (chan) {
  504. sync->addr ^= 0x10;
  505. sync->data++;
  506. FIRE_RING (chan);
  507. }
  508. /* queue the flip */
  509. evo_mthd(push, 0x0100, 1);
  510. evo_data(push, 0xfffe0000);
  511. evo_mthd(push, 0x0084, 1);
  512. evo_data(push, swap_interval);
  513. if (!(swap_interval & 0x00000100)) {
  514. evo_mthd(push, 0x00e0, 1);
  515. evo_data(push, 0x40000000);
  516. }
  517. evo_mthd(push, 0x0088, 4);
  518. evo_data(push, sync->addr);
  519. evo_data(push, sync->data++);
  520. evo_data(push, sync->data);
  521. evo_data(push, sync->base.sync.handle);
  522. evo_mthd(push, 0x00a0, 2);
  523. evo_data(push, 0x00000000);
  524. evo_data(push, 0x00000000);
  525. evo_mthd(push, 0x00c0, 1);
  526. evo_data(push, nv_fb->r_handle);
  527. evo_mthd(push, 0x0110, 2);
  528. evo_data(push, 0x00000000);
  529. evo_data(push, 0x00000000);
  530. if (nv50_vers(sync) < GF110_DISP_BASE_CHANNEL_DMA) {
  531. evo_mthd(push, 0x0800, 5);
  532. evo_data(push, nv_fb->nvbo->bo.offset >> 8);
  533. evo_data(push, 0);
  534. evo_data(push, (fb->height << 16) | fb->width);
  535. evo_data(push, nv_fb->r_pitch);
  536. evo_data(push, nv_fb->r_format);
  537. } else {
  538. evo_mthd(push, 0x0400, 5);
  539. evo_data(push, nv_fb->nvbo->bo.offset >> 8);
  540. evo_data(push, 0);
  541. evo_data(push, (fb->height << 16) | fb->width);
  542. evo_data(push, nv_fb->r_pitch);
  543. evo_data(push, nv_fb->r_format);
  544. }
  545. evo_mthd(push, 0x0080, 1);
  546. evo_data(push, 0x00000000);
  547. evo_kick(push, sync);
  548. nouveau_bo_ref(nv_fb->nvbo, &head->image);
  549. return 0;
  550. }
  551. /******************************************************************************
  552. * CRTC
  553. *****************************************************************************/
  554. static int
  555. nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
  556. {
  557. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  558. struct nouveau_connector *nv_connector;
  559. struct drm_connector *connector;
  560. u32 *push, mode = 0x00;
  561. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  562. connector = &nv_connector->base;
  563. if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
  564. if (nv_crtc->base.primary->fb->depth > connector->display_info.bpc * 3)
  565. mode = DITHERING_MODE_DYNAMIC2X2;
  566. } else {
  567. mode = nv_connector->dithering_mode;
  568. }
  569. if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
  570. if (connector->display_info.bpc >= 8)
  571. mode |= DITHERING_DEPTH_8BPC;
  572. } else {
  573. mode |= nv_connector->dithering_depth;
  574. }
  575. push = evo_wait(mast, 4);
  576. if (push) {
  577. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  578. evo_mthd(push, 0x08a0 + (nv_crtc->index * 0x0400), 1);
  579. evo_data(push, mode);
  580. } else
  581. if (nv50_vers(mast) < GK104_DISP_CORE_CHANNEL_DMA) {
  582. evo_mthd(push, 0x0490 + (nv_crtc->index * 0x0300), 1);
  583. evo_data(push, mode);
  584. } else {
  585. evo_mthd(push, 0x04a0 + (nv_crtc->index * 0x0300), 1);
  586. evo_data(push, mode);
  587. }
  588. if (update) {
  589. evo_mthd(push, 0x0080, 1);
  590. evo_data(push, 0x00000000);
  591. }
  592. evo_kick(push, mast);
  593. }
  594. return 0;
  595. }
  596. static int
  597. nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
  598. {
  599. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  600. struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
  601. struct drm_crtc *crtc = &nv_crtc->base;
  602. struct nouveau_connector *nv_connector;
  603. int mode = DRM_MODE_SCALE_NONE;
  604. u32 oX, oY, *push;
  605. /* start off at the resolution we programmed the crtc for, this
  606. * effectively handles NONE/FULL scaling
  607. */
  608. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  609. if (nv_connector && nv_connector->native_mode)
  610. mode = nv_connector->scaling_mode;
  611. if (mode != DRM_MODE_SCALE_NONE)
  612. omode = nv_connector->native_mode;
  613. else
  614. omode = umode;
  615. oX = omode->hdisplay;
  616. oY = omode->vdisplay;
  617. if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
  618. oY *= 2;
  619. /* add overscan compensation if necessary, will keep the aspect
  620. * ratio the same as the backend mode unless overridden by the
  621. * user setting both hborder and vborder properties.
  622. */
  623. if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
  624. (nv_connector->underscan == UNDERSCAN_AUTO &&
  625. nv_connector->edid &&
  626. drm_detect_hdmi_monitor(nv_connector->edid)))) {
  627. u32 bX = nv_connector->underscan_hborder;
  628. u32 bY = nv_connector->underscan_vborder;
  629. u32 aspect = (oY << 19) / oX;
  630. if (bX) {
  631. oX -= (bX * 2);
  632. if (bY) oY -= (bY * 2);
  633. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  634. } else {
  635. oX -= (oX >> 4) + 32;
  636. if (bY) oY -= (bY * 2);
  637. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  638. }
  639. }
  640. /* handle CENTER/ASPECT scaling, taking into account the areas
  641. * removed already for overscan compensation
  642. */
  643. switch (mode) {
  644. case DRM_MODE_SCALE_CENTER:
  645. oX = min((u32)umode->hdisplay, oX);
  646. oY = min((u32)umode->vdisplay, oY);
  647. /* fall-through */
  648. case DRM_MODE_SCALE_ASPECT:
  649. if (oY < oX) {
  650. u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
  651. oX = ((oY * aspect) + (aspect / 2)) >> 19;
  652. } else {
  653. u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
  654. oY = ((oX * aspect) + (aspect / 2)) >> 19;
  655. }
  656. break;
  657. default:
  658. break;
  659. }
  660. push = evo_wait(mast, 8);
  661. if (push) {
  662. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  663. /*XXX: SCALE_CTRL_ACTIVE??? */
  664. evo_mthd(push, 0x08d8 + (nv_crtc->index * 0x400), 2);
  665. evo_data(push, (oY << 16) | oX);
  666. evo_data(push, (oY << 16) | oX);
  667. evo_mthd(push, 0x08a4 + (nv_crtc->index * 0x400), 1);
  668. evo_data(push, 0x00000000);
  669. evo_mthd(push, 0x08c8 + (nv_crtc->index * 0x400), 1);
  670. evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
  671. } else {
  672. evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
  673. evo_data(push, (oY << 16) | oX);
  674. evo_data(push, (oY << 16) | oX);
  675. evo_data(push, (oY << 16) | oX);
  676. evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
  677. evo_data(push, 0x00000000);
  678. evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
  679. evo_data(push, umode->vdisplay << 16 | umode->hdisplay);
  680. }
  681. evo_kick(push, mast);
  682. if (update) {
  683. nv50_display_flip_stop(crtc);
  684. nv50_display_flip_next(crtc, crtc->primary->fb,
  685. NULL, 1);
  686. }
  687. }
  688. return 0;
  689. }
  690. static int
  691. nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
  692. {
  693. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  694. u32 *push, hue, vib;
  695. int adj;
  696. adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
  697. vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
  698. hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
  699. push = evo_wait(mast, 16);
  700. if (push) {
  701. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  702. evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
  703. evo_data(push, (hue << 20) | (vib << 8));
  704. } else {
  705. evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
  706. evo_data(push, (hue << 20) | (vib << 8));
  707. }
  708. if (update) {
  709. evo_mthd(push, 0x0080, 1);
  710. evo_data(push, 0x00000000);
  711. }
  712. evo_kick(push, mast);
  713. }
  714. return 0;
  715. }
  716. static int
  717. nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
  718. int x, int y, bool update)
  719. {
  720. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
  721. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  722. u32 *push;
  723. push = evo_wait(mast, 16);
  724. if (push) {
  725. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  726. evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
  727. evo_data(push, nvfb->nvbo->bo.offset >> 8);
  728. evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
  729. evo_data(push, (fb->height << 16) | fb->width);
  730. evo_data(push, nvfb->r_pitch);
  731. evo_data(push, nvfb->r_format);
  732. evo_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
  733. evo_data(push, (y << 16) | x);
  734. if (nv50_vers(mast) > NV50_DISP_CORE_CHANNEL_DMA) {
  735. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  736. evo_data(push, nvfb->r_handle);
  737. }
  738. } else {
  739. evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
  740. evo_data(push, nvfb->nvbo->bo.offset >> 8);
  741. evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
  742. evo_data(push, (fb->height << 16) | fb->width);
  743. evo_data(push, nvfb->r_pitch);
  744. evo_data(push, nvfb->r_format);
  745. evo_data(push, nvfb->r_handle);
  746. evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
  747. evo_data(push, (y << 16) | x);
  748. }
  749. if (update) {
  750. evo_mthd(push, 0x0080, 1);
  751. evo_data(push, 0x00000000);
  752. }
  753. evo_kick(push, mast);
  754. }
  755. nv_crtc->fb.handle = nvfb->r_handle;
  756. return 0;
  757. }
  758. static void
  759. nv50_crtc_cursor_show(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) < G82_DISP_CORE_CHANNEL_DMA) {
  765. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
  766. evo_data(push, 0x85000000);
  767. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  768. } else
  769. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  770. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
  771. evo_data(push, 0x85000000);
  772. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  773. evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
  774. evo_data(push, mast->base.vram.handle);
  775. } else {
  776. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
  777. evo_data(push, 0x85000000);
  778. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  779. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  780. evo_data(push, mast->base.vram.handle);
  781. }
  782. evo_kick(push, mast);
  783. }
  784. }
  785. static void
  786. nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
  787. {
  788. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  789. u32 *push = evo_wait(mast, 16);
  790. if (push) {
  791. if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
  792. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
  793. evo_data(push, 0x05000000);
  794. } else
  795. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  796. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
  797. evo_data(push, 0x05000000);
  798. evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
  799. evo_data(push, 0x00000000);
  800. } else {
  801. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
  802. evo_data(push, 0x05000000);
  803. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  804. evo_data(push, 0x00000000);
  805. }
  806. evo_kick(push, mast);
  807. }
  808. }
  809. static void
  810. nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
  811. {
  812. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  813. if (show)
  814. nv50_crtc_cursor_show(nv_crtc);
  815. else
  816. nv50_crtc_cursor_hide(nv_crtc);
  817. if (update) {
  818. u32 *push = evo_wait(mast, 2);
  819. if (push) {
  820. evo_mthd(push, 0x0080, 1);
  821. evo_data(push, 0x00000000);
  822. evo_kick(push, mast);
  823. }
  824. }
  825. }
  826. static void
  827. nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
  828. {
  829. }
  830. static void
  831. nv50_crtc_prepare(struct drm_crtc *crtc)
  832. {
  833. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  834. struct nv50_mast *mast = nv50_mast(crtc->dev);
  835. u32 *push;
  836. nv50_display_flip_stop(crtc);
  837. push = evo_wait(mast, 6);
  838. if (push) {
  839. if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
  840. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  841. evo_data(push, 0x00000000);
  842. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
  843. evo_data(push, 0x40000000);
  844. } else
  845. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  846. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  847. evo_data(push, 0x00000000);
  848. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
  849. evo_data(push, 0x40000000);
  850. evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
  851. evo_data(push, 0x00000000);
  852. } else {
  853. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  854. evo_data(push, 0x00000000);
  855. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
  856. evo_data(push, 0x03000000);
  857. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  858. evo_data(push, 0x00000000);
  859. }
  860. evo_kick(push, mast);
  861. }
  862. nv50_crtc_cursor_show_hide(nv_crtc, false, false);
  863. }
  864. static void
  865. nv50_crtc_commit(struct drm_crtc *crtc)
  866. {
  867. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  868. struct nv50_mast *mast = nv50_mast(crtc->dev);
  869. u32 *push;
  870. push = evo_wait(mast, 32);
  871. if (push) {
  872. if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
  873. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  874. evo_data(push, nv_crtc->fb.handle);
  875. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
  876. evo_data(push, 0xc0000000);
  877. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  878. } else
  879. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  880. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  881. evo_data(push, nv_crtc->fb.handle);
  882. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
  883. evo_data(push, 0xc0000000);
  884. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  885. evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
  886. evo_data(push, mast->base.vram.handle);
  887. } else {
  888. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  889. evo_data(push, nv_crtc->fb.handle);
  890. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
  891. evo_data(push, 0x83000000);
  892. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  893. evo_data(push, 0x00000000);
  894. evo_data(push, 0x00000000);
  895. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  896. evo_data(push, mast->base.vram.handle);
  897. evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
  898. evo_data(push, 0xffffff00);
  899. }
  900. evo_kick(push, mast);
  901. }
  902. nv50_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
  903. nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
  904. }
  905. static bool
  906. nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
  907. struct drm_display_mode *adjusted_mode)
  908. {
  909. drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
  910. return true;
  911. }
  912. static int
  913. nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
  914. {
  915. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
  916. struct nv50_head *head = nv50_head(crtc);
  917. int ret;
  918. ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
  919. if (ret == 0) {
  920. if (head->image)
  921. nouveau_bo_unpin(head->image);
  922. nouveau_bo_ref(nvfb->nvbo, &head->image);
  923. }
  924. return ret;
  925. }
  926. static int
  927. nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
  928. struct drm_display_mode *mode, int x, int y,
  929. struct drm_framebuffer *old_fb)
  930. {
  931. struct nv50_mast *mast = nv50_mast(crtc->dev);
  932. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  933. struct nouveau_connector *nv_connector;
  934. u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
  935. u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
  936. u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
  937. u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
  938. u32 vblan2e = 0, vblan2s = 1;
  939. u32 *push;
  940. int ret;
  941. hactive = mode->htotal;
  942. hsynce = mode->hsync_end - mode->hsync_start - 1;
  943. hbackp = mode->htotal - mode->hsync_end;
  944. hblanke = hsynce + hbackp;
  945. hfrontp = mode->hsync_start - mode->hdisplay;
  946. hblanks = mode->htotal - hfrontp - 1;
  947. vactive = mode->vtotal * vscan / ilace;
  948. vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
  949. vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
  950. vblanke = vsynce + vbackp;
  951. vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
  952. vblanks = vactive - vfrontp - 1;
  953. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  954. vblan2e = vactive + vsynce + vbackp;
  955. vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
  956. vactive = (vactive * 2) + 1;
  957. }
  958. ret = nv50_crtc_swap_fbs(crtc, old_fb);
  959. if (ret)
  960. return ret;
  961. push = evo_wait(mast, 64);
  962. if (push) {
  963. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  964. evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
  965. evo_data(push, 0x00800000 | mode->clock);
  966. evo_data(push, (ilace == 2) ? 2 : 0);
  967. evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 6);
  968. evo_data(push, 0x00000000);
  969. evo_data(push, (vactive << 16) | hactive);
  970. evo_data(push, ( vsynce << 16) | hsynce);
  971. evo_data(push, (vblanke << 16) | hblanke);
  972. evo_data(push, (vblanks << 16) | hblanks);
  973. evo_data(push, (vblan2e << 16) | vblan2s);
  974. evo_mthd(push, 0x082c + (nv_crtc->index * 0x400), 1);
  975. evo_data(push, 0x00000000);
  976. evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
  977. evo_data(push, 0x00000311);
  978. evo_data(push, 0x00000100);
  979. } else {
  980. evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
  981. evo_data(push, 0x00000000);
  982. evo_data(push, (vactive << 16) | hactive);
  983. evo_data(push, ( vsynce << 16) | hsynce);
  984. evo_data(push, (vblanke << 16) | hblanke);
  985. evo_data(push, (vblanks << 16) | hblanks);
  986. evo_data(push, (vblan2e << 16) | vblan2s);
  987. evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
  988. evo_data(push, 0x00000000); /* ??? */
  989. evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
  990. evo_data(push, mode->clock * 1000);
  991. evo_data(push, 0x00200000); /* ??? */
  992. evo_data(push, mode->clock * 1000);
  993. evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
  994. evo_data(push, 0x00000311);
  995. evo_data(push, 0x00000100);
  996. }
  997. evo_kick(push, mast);
  998. }
  999. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  1000. nv50_crtc_set_dither(nv_crtc, false);
  1001. nv50_crtc_set_scale(nv_crtc, false);
  1002. nv50_crtc_set_color_vibrance(nv_crtc, false);
  1003. nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
  1004. return 0;
  1005. }
  1006. static int
  1007. nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  1008. struct drm_framebuffer *old_fb)
  1009. {
  1010. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  1011. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1012. int ret;
  1013. if (!crtc->primary->fb) {
  1014. NV_DEBUG(drm, "No FB bound\n");
  1015. return 0;
  1016. }
  1017. ret = nv50_crtc_swap_fbs(crtc, old_fb);
  1018. if (ret)
  1019. return ret;
  1020. nv50_display_flip_stop(crtc);
  1021. nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
  1022. nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
  1023. return 0;
  1024. }
  1025. static int
  1026. nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
  1027. struct drm_framebuffer *fb, int x, int y,
  1028. enum mode_set_atomic state)
  1029. {
  1030. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1031. nv50_display_flip_stop(crtc);
  1032. nv50_crtc_set_image(nv_crtc, fb, x, y, true);
  1033. return 0;
  1034. }
  1035. static void
  1036. nv50_crtc_lut_load(struct drm_crtc *crtc)
  1037. {
  1038. struct nv50_disp *disp = nv50_disp(crtc->dev);
  1039. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1040. void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
  1041. int i;
  1042. for (i = 0; i < 256; i++) {
  1043. u16 r = nv_crtc->lut.r[i] >> 2;
  1044. u16 g = nv_crtc->lut.g[i] >> 2;
  1045. u16 b = nv_crtc->lut.b[i] >> 2;
  1046. if (disp->disp->oclass < GF110_DISP) {
  1047. writew(r + 0x0000, lut + (i * 0x08) + 0);
  1048. writew(g + 0x0000, lut + (i * 0x08) + 2);
  1049. writew(b + 0x0000, lut + (i * 0x08) + 4);
  1050. } else {
  1051. writew(r + 0x6000, lut + (i * 0x20) + 0);
  1052. writew(g + 0x6000, lut + (i * 0x20) + 2);
  1053. writew(b + 0x6000, lut + (i * 0x20) + 4);
  1054. }
  1055. }
  1056. }
  1057. static void
  1058. nv50_crtc_disable(struct drm_crtc *crtc)
  1059. {
  1060. struct nv50_head *head = nv50_head(crtc);
  1061. evo_sync(crtc->dev);
  1062. if (head->image)
  1063. nouveau_bo_unpin(head->image);
  1064. nouveau_bo_ref(NULL, &head->image);
  1065. }
  1066. static int
  1067. nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  1068. uint32_t handle, uint32_t width, uint32_t height)
  1069. {
  1070. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1071. struct drm_device *dev = crtc->dev;
  1072. struct drm_gem_object *gem;
  1073. struct nouveau_bo *nvbo;
  1074. bool visible = (handle != 0);
  1075. int i, ret = 0;
  1076. if (visible) {
  1077. if (width != 64 || height != 64)
  1078. return -EINVAL;
  1079. gem = drm_gem_object_lookup(dev, file_priv, handle);
  1080. if (unlikely(!gem))
  1081. return -ENOENT;
  1082. nvbo = nouveau_gem_object(gem);
  1083. ret = nouveau_bo_map(nvbo);
  1084. if (ret == 0) {
  1085. for (i = 0; i < 64 * 64; i++) {
  1086. u32 v = nouveau_bo_rd32(nvbo, i);
  1087. nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
  1088. }
  1089. nouveau_bo_unmap(nvbo);
  1090. }
  1091. drm_gem_object_unreference_unlocked(gem);
  1092. }
  1093. if (visible != nv_crtc->cursor.visible) {
  1094. nv50_crtc_cursor_show_hide(nv_crtc, visible, true);
  1095. nv_crtc->cursor.visible = visible;
  1096. }
  1097. return ret;
  1098. }
  1099. static int
  1100. nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  1101. {
  1102. struct nv50_curs *curs = nv50_curs(crtc);
  1103. struct nv50_chan *chan = nv50_chan(curs);
  1104. nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
  1105. nvif_wr32(&chan->user, 0x0080, 0x00000000);
  1106. return 0;
  1107. }
  1108. static void
  1109. nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  1110. uint32_t start, uint32_t size)
  1111. {
  1112. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1113. u32 end = min_t(u32, start + size, 256);
  1114. u32 i;
  1115. for (i = start; i < end; i++) {
  1116. nv_crtc->lut.r[i] = r[i];
  1117. nv_crtc->lut.g[i] = g[i];
  1118. nv_crtc->lut.b[i] = b[i];
  1119. }
  1120. nv50_crtc_lut_load(crtc);
  1121. }
  1122. static void
  1123. nv50_crtc_destroy(struct drm_crtc *crtc)
  1124. {
  1125. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1126. struct nv50_disp *disp = nv50_disp(crtc->dev);
  1127. struct nv50_head *head = nv50_head(crtc);
  1128. struct nv50_fbdma *fbdma;
  1129. list_for_each_entry(fbdma, &disp->fbdma, head) {
  1130. nvif_object_fini(&fbdma->base[nv_crtc->index]);
  1131. }
  1132. nv50_dmac_destroy(&head->ovly.base, disp->disp);
  1133. nv50_pioc_destroy(&head->oimm.base);
  1134. nv50_dmac_destroy(&head->sync.base, disp->disp);
  1135. nv50_pioc_destroy(&head->curs.base);
  1136. /*XXX: this shouldn't be necessary, but the core doesn't call
  1137. * disconnect() during the cleanup paths
  1138. */
  1139. if (head->image)
  1140. nouveau_bo_unpin(head->image);
  1141. nouveau_bo_ref(NULL, &head->image);
  1142. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  1143. if (nv_crtc->cursor.nvbo)
  1144. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  1145. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  1146. nouveau_bo_unmap(nv_crtc->lut.nvbo);
  1147. if (nv_crtc->lut.nvbo)
  1148. nouveau_bo_unpin(nv_crtc->lut.nvbo);
  1149. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  1150. drm_crtc_cleanup(crtc);
  1151. kfree(crtc);
  1152. }
  1153. static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
  1154. .dpms = nv50_crtc_dpms,
  1155. .prepare = nv50_crtc_prepare,
  1156. .commit = nv50_crtc_commit,
  1157. .mode_fixup = nv50_crtc_mode_fixup,
  1158. .mode_set = nv50_crtc_mode_set,
  1159. .mode_set_base = nv50_crtc_mode_set_base,
  1160. .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
  1161. .load_lut = nv50_crtc_lut_load,
  1162. .disable = nv50_crtc_disable,
  1163. };
  1164. static const struct drm_crtc_funcs nv50_crtc_func = {
  1165. .cursor_set = nv50_crtc_cursor_set,
  1166. .cursor_move = nv50_crtc_cursor_move,
  1167. .gamma_set = nv50_crtc_gamma_set,
  1168. .set_config = nouveau_crtc_set_config,
  1169. .destroy = nv50_crtc_destroy,
  1170. .page_flip = nouveau_crtc_page_flip,
  1171. };
  1172. static void
  1173. nv50_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
  1174. {
  1175. }
  1176. static void
  1177. nv50_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
  1178. {
  1179. }
  1180. static int
  1181. nv50_crtc_create(struct drm_device *dev, int index)
  1182. {
  1183. struct nv50_disp *disp = nv50_disp(dev);
  1184. struct nv50_head *head;
  1185. struct drm_crtc *crtc;
  1186. int ret, i;
  1187. head = kzalloc(sizeof(*head), GFP_KERNEL);
  1188. if (!head)
  1189. return -ENOMEM;
  1190. head->base.index = index;
  1191. head->base.set_dither = nv50_crtc_set_dither;
  1192. head->base.set_scale = nv50_crtc_set_scale;
  1193. head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
  1194. head->base.color_vibrance = 50;
  1195. head->base.vibrant_hue = 0;
  1196. head->base.cursor.set_offset = nv50_cursor_set_offset;
  1197. head->base.cursor.set_pos = nv50_cursor_set_pos;
  1198. for (i = 0; i < 256; i++) {
  1199. head->base.lut.r[i] = i << 8;
  1200. head->base.lut.g[i] = i << 8;
  1201. head->base.lut.b[i] = i << 8;
  1202. }
  1203. crtc = &head->base.base;
  1204. drm_crtc_init(dev, crtc, &nv50_crtc_func);
  1205. drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
  1206. drm_mode_crtc_set_gamma_size(crtc, 256);
  1207. ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
  1208. 0, 0x0000, NULL, &head->base.lut.nvbo);
  1209. if (!ret) {
  1210. ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
  1211. if (!ret) {
  1212. ret = nouveau_bo_map(head->base.lut.nvbo);
  1213. if (ret)
  1214. nouveau_bo_unpin(head->base.lut.nvbo);
  1215. }
  1216. if (ret)
  1217. nouveau_bo_ref(NULL, &head->base.lut.nvbo);
  1218. }
  1219. if (ret)
  1220. goto out;
  1221. nv50_crtc_lut_load(crtc);
  1222. /* allocate cursor resources */
  1223. ret = nv50_curs_create(disp->disp, index, &head->curs);
  1224. if (ret)
  1225. goto out;
  1226. ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
  1227. 0, 0x0000, NULL, &head->base.cursor.nvbo);
  1228. if (!ret) {
  1229. ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
  1230. if (!ret) {
  1231. ret = nouveau_bo_map(head->base.cursor.nvbo);
  1232. if (ret)
  1233. nouveau_bo_unpin(head->base.lut.nvbo);
  1234. }
  1235. if (ret)
  1236. nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
  1237. }
  1238. if (ret)
  1239. goto out;
  1240. /* allocate page flip / sync resources */
  1241. ret = nv50_base_create(disp->disp, index, disp->sync->bo.offset,
  1242. &head->sync);
  1243. if (ret)
  1244. goto out;
  1245. head->sync.addr = EVO_FLIP_SEM0(index);
  1246. head->sync.data = 0x00000000;
  1247. /* allocate overlay resources */
  1248. ret = nv50_oimm_create(disp->disp, index, &head->oimm);
  1249. if (ret)
  1250. goto out;
  1251. ret = nv50_ovly_create(disp->disp, index, disp->sync->bo.offset,
  1252. &head->ovly);
  1253. if (ret)
  1254. goto out;
  1255. out:
  1256. if (ret)
  1257. nv50_crtc_destroy(crtc);
  1258. return ret;
  1259. }
  1260. /******************************************************************************
  1261. * DAC
  1262. *****************************************************************************/
  1263. static void
  1264. nv50_dac_dpms(struct drm_encoder *encoder, int mode)
  1265. {
  1266. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1267. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1268. struct {
  1269. struct nv50_disp_mthd_v1 base;
  1270. struct nv50_disp_dac_pwr_v0 pwr;
  1271. } args = {
  1272. .base.version = 1,
  1273. .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
  1274. .base.hasht = nv_encoder->dcb->hasht,
  1275. .base.hashm = nv_encoder->dcb->hashm,
  1276. .pwr.state = 1,
  1277. .pwr.data = 1,
  1278. .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
  1279. mode != DRM_MODE_DPMS_OFF),
  1280. .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
  1281. mode != DRM_MODE_DPMS_OFF),
  1282. };
  1283. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1284. }
  1285. static bool
  1286. nv50_dac_mode_fixup(struct drm_encoder *encoder,
  1287. const struct drm_display_mode *mode,
  1288. struct drm_display_mode *adjusted_mode)
  1289. {
  1290. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1291. struct nouveau_connector *nv_connector;
  1292. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1293. if (nv_connector && nv_connector->native_mode) {
  1294. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1295. int id = adjusted_mode->base.id;
  1296. *adjusted_mode = *nv_connector->native_mode;
  1297. adjusted_mode->base.id = id;
  1298. }
  1299. }
  1300. return true;
  1301. }
  1302. static void
  1303. nv50_dac_commit(struct drm_encoder *encoder)
  1304. {
  1305. }
  1306. static void
  1307. nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  1308. struct drm_display_mode *adjusted_mode)
  1309. {
  1310. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1311. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1312. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1313. u32 *push;
  1314. nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
  1315. push = evo_wait(mast, 8);
  1316. if (push) {
  1317. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  1318. u32 syncs = 0x00000000;
  1319. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1320. syncs |= 0x00000001;
  1321. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1322. syncs |= 0x00000002;
  1323. evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
  1324. evo_data(push, 1 << nv_crtc->index);
  1325. evo_data(push, syncs);
  1326. } else {
  1327. u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
  1328. u32 syncs = 0x00000001;
  1329. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1330. syncs |= 0x00000008;
  1331. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1332. syncs |= 0x00000010;
  1333. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1334. magic |= 0x00000001;
  1335. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1336. evo_data(push, syncs);
  1337. evo_data(push, magic);
  1338. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
  1339. evo_data(push, 1 << nv_crtc->index);
  1340. }
  1341. evo_kick(push, mast);
  1342. }
  1343. nv_encoder->crtc = encoder->crtc;
  1344. }
  1345. static void
  1346. nv50_dac_disconnect(struct drm_encoder *encoder)
  1347. {
  1348. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1349. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1350. const int or = nv_encoder->or;
  1351. u32 *push;
  1352. if (nv_encoder->crtc) {
  1353. nv50_crtc_prepare(nv_encoder->crtc);
  1354. push = evo_wait(mast, 4);
  1355. if (push) {
  1356. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  1357. evo_mthd(push, 0x0400 + (or * 0x080), 1);
  1358. evo_data(push, 0x00000000);
  1359. } else {
  1360. evo_mthd(push, 0x0180 + (or * 0x020), 1);
  1361. evo_data(push, 0x00000000);
  1362. }
  1363. evo_kick(push, mast);
  1364. }
  1365. }
  1366. nv_encoder->crtc = NULL;
  1367. }
  1368. static enum drm_connector_status
  1369. nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  1370. {
  1371. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1372. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1373. struct {
  1374. struct nv50_disp_mthd_v1 base;
  1375. struct nv50_disp_dac_load_v0 load;
  1376. } args = {
  1377. .base.version = 1,
  1378. .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
  1379. .base.hasht = nv_encoder->dcb->hasht,
  1380. .base.hashm = nv_encoder->dcb->hashm,
  1381. };
  1382. int ret;
  1383. args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
  1384. if (args.load.data == 0)
  1385. args.load.data = 340;
  1386. ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1387. if (ret || !args.load.load)
  1388. return connector_status_disconnected;
  1389. return connector_status_connected;
  1390. }
  1391. static void
  1392. nv50_dac_destroy(struct drm_encoder *encoder)
  1393. {
  1394. drm_encoder_cleanup(encoder);
  1395. kfree(encoder);
  1396. }
  1397. static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
  1398. .dpms = nv50_dac_dpms,
  1399. .mode_fixup = nv50_dac_mode_fixup,
  1400. .prepare = nv50_dac_disconnect,
  1401. .commit = nv50_dac_commit,
  1402. .mode_set = nv50_dac_mode_set,
  1403. .disable = nv50_dac_disconnect,
  1404. .get_crtc = nv50_display_crtc_get,
  1405. .detect = nv50_dac_detect
  1406. };
  1407. static const struct drm_encoder_funcs nv50_dac_func = {
  1408. .destroy = nv50_dac_destroy,
  1409. };
  1410. static int
  1411. nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1412. {
  1413. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  1414. struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
  1415. struct nouveau_encoder *nv_encoder;
  1416. struct drm_encoder *encoder;
  1417. int type = DRM_MODE_ENCODER_DAC;
  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->i2c = i2c->find(i2c, dcbe->i2c_index);
  1424. encoder = to_drm_encoder(nv_encoder);
  1425. encoder->possible_crtcs = dcbe->heads;
  1426. encoder->possible_clones = 0;
  1427. drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
  1428. drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
  1429. drm_mode_connector_attach_encoder(connector, encoder);
  1430. return 0;
  1431. }
  1432. /******************************************************************************
  1433. * Audio
  1434. *****************************************************************************/
  1435. static void
  1436. nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1437. {
  1438. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1439. struct nouveau_connector *nv_connector;
  1440. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1441. struct {
  1442. struct nv50_disp_mthd_v1 base;
  1443. struct nv50_disp_sor_hda_eld_v0 eld;
  1444. u8 data[sizeof(nv_connector->base.eld)];
  1445. } args = {
  1446. .base.version = 1,
  1447. .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
  1448. .base.hasht = nv_encoder->dcb->hasht,
  1449. .base.hashm = nv_encoder->dcb->hashm,
  1450. };
  1451. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1452. if (!drm_detect_monitor_audio(nv_connector->edid))
  1453. return;
  1454. drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
  1455. memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
  1456. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1457. }
  1458. static void
  1459. nv50_audio_disconnect(struct drm_encoder *encoder)
  1460. {
  1461. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1462. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1463. struct {
  1464. struct nv50_disp_mthd_v1 base;
  1465. struct nv50_disp_sor_hda_eld_v0 eld;
  1466. } args = {
  1467. .base.version = 1,
  1468. .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
  1469. .base.hasht = nv_encoder->dcb->hasht,
  1470. .base.hashm = nv_encoder->dcb->hashm,
  1471. };
  1472. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1473. }
  1474. /******************************************************************************
  1475. * HDMI
  1476. *****************************************************************************/
  1477. static void
  1478. nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1479. {
  1480. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1481. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1482. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1483. struct {
  1484. struct nv50_disp_mthd_v1 base;
  1485. struct nv50_disp_sor_hdmi_pwr_v0 pwr;
  1486. } args = {
  1487. .base.version = 1,
  1488. .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
  1489. .base.hasht = nv_encoder->dcb->hasht,
  1490. .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
  1491. (0x0100 << nv_crtc->index),
  1492. .pwr.state = 1,
  1493. .pwr.rekey = 56, /* binary driver, and tegra, constant */
  1494. };
  1495. struct nouveau_connector *nv_connector;
  1496. u32 max_ac_packet;
  1497. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1498. if (!drm_detect_hdmi_monitor(nv_connector->edid))
  1499. return;
  1500. max_ac_packet = mode->htotal - mode->hdisplay;
  1501. max_ac_packet -= args.pwr.rekey;
  1502. max_ac_packet -= 18; /* constant from tegra */
  1503. args.pwr.max_ac_packet = max_ac_packet / 32;
  1504. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1505. nv50_audio_mode_set(encoder, mode);
  1506. }
  1507. static void
  1508. nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
  1509. {
  1510. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1511. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1512. struct {
  1513. struct nv50_disp_mthd_v1 base;
  1514. struct nv50_disp_sor_hdmi_pwr_v0 pwr;
  1515. } args = {
  1516. .base.version = 1,
  1517. .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
  1518. .base.hasht = nv_encoder->dcb->hasht,
  1519. .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
  1520. (0x0100 << nv_crtc->index),
  1521. };
  1522. nv50_audio_disconnect(encoder);
  1523. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1524. }
  1525. /******************************************************************************
  1526. * SOR
  1527. *****************************************************************************/
  1528. static void
  1529. nv50_sor_dpms(struct drm_encoder *encoder, int mode)
  1530. {
  1531. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1532. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1533. struct {
  1534. struct nv50_disp_mthd_v1 base;
  1535. struct nv50_disp_sor_pwr_v0 pwr;
  1536. } args = {
  1537. .base.version = 1,
  1538. .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
  1539. .base.hasht = nv_encoder->dcb->hasht,
  1540. .base.hashm = nv_encoder->dcb->hashm,
  1541. .pwr.state = mode == DRM_MODE_DPMS_ON,
  1542. };
  1543. struct {
  1544. struct nv50_disp_mthd_v1 base;
  1545. struct nv50_disp_sor_dp_pwr_v0 pwr;
  1546. } link = {
  1547. .base.version = 1,
  1548. .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
  1549. .base.hasht = nv_encoder->dcb->hasht,
  1550. .base.hashm = nv_encoder->dcb->hashm,
  1551. .pwr.state = mode == DRM_MODE_DPMS_ON,
  1552. };
  1553. struct drm_device *dev = encoder->dev;
  1554. struct drm_encoder *partner;
  1555. nv_encoder->last_dpms = mode;
  1556. list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
  1557. struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
  1558. if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
  1559. continue;
  1560. if (nv_partner != nv_encoder &&
  1561. nv_partner->dcb->or == nv_encoder->dcb->or) {
  1562. if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
  1563. return;
  1564. break;
  1565. }
  1566. }
  1567. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  1568. args.pwr.state = 1;
  1569. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1570. nvif_mthd(disp->disp, 0, &link, sizeof(link));
  1571. } else {
  1572. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1573. }
  1574. }
  1575. static bool
  1576. nv50_sor_mode_fixup(struct drm_encoder *encoder,
  1577. const struct drm_display_mode *mode,
  1578. struct drm_display_mode *adjusted_mode)
  1579. {
  1580. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1581. struct nouveau_connector *nv_connector;
  1582. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1583. if (nv_connector && nv_connector->native_mode) {
  1584. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1585. int id = adjusted_mode->base.id;
  1586. *adjusted_mode = *nv_connector->native_mode;
  1587. adjusted_mode->base.id = id;
  1588. }
  1589. }
  1590. return true;
  1591. }
  1592. static void
  1593. nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
  1594. {
  1595. struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
  1596. u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
  1597. if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
  1598. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  1599. evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
  1600. evo_data(push, (nv_encoder->ctrl = temp));
  1601. } else {
  1602. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
  1603. evo_data(push, (nv_encoder->ctrl = temp));
  1604. }
  1605. evo_kick(push, mast);
  1606. }
  1607. }
  1608. static void
  1609. nv50_sor_disconnect(struct drm_encoder *encoder)
  1610. {
  1611. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1612. struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
  1613. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1614. nv_encoder->crtc = NULL;
  1615. if (nv_crtc) {
  1616. nv50_crtc_prepare(&nv_crtc->base);
  1617. nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
  1618. nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
  1619. }
  1620. }
  1621. static void
  1622. nv50_sor_commit(struct drm_encoder *encoder)
  1623. {
  1624. }
  1625. static void
  1626. nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  1627. struct drm_display_mode *mode)
  1628. {
  1629. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1630. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1631. struct {
  1632. struct nv50_disp_mthd_v1 base;
  1633. struct nv50_disp_sor_lvds_script_v0 lvds;
  1634. } lvds = {
  1635. .base.version = 1,
  1636. .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
  1637. .base.hasht = nv_encoder->dcb->hasht,
  1638. .base.hashm = nv_encoder->dcb->hashm,
  1639. };
  1640. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1641. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1642. struct drm_device *dev = encoder->dev;
  1643. struct nouveau_drm *drm = nouveau_drm(dev);
  1644. struct nouveau_connector *nv_connector;
  1645. struct nvbios *bios = &drm->vbios;
  1646. u32 mask, ctrl;
  1647. u8 owner = 1 << nv_crtc->index;
  1648. u8 proto = 0xf;
  1649. u8 depth = 0x0;
  1650. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1651. nv_encoder->crtc = encoder->crtc;
  1652. switch (nv_encoder->dcb->type) {
  1653. case DCB_OUTPUT_TMDS:
  1654. if (nv_encoder->dcb->sorconf.link & 1) {
  1655. if (mode->clock < 165000)
  1656. proto = 0x1;
  1657. else
  1658. proto = 0x5;
  1659. } else {
  1660. proto = 0x2;
  1661. }
  1662. nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
  1663. break;
  1664. case DCB_OUTPUT_LVDS:
  1665. proto = 0x0;
  1666. if (bios->fp_no_ddc) {
  1667. if (bios->fp.dual_link)
  1668. lvds.lvds.script |= 0x0100;
  1669. if (bios->fp.if_is_24bit)
  1670. lvds.lvds.script |= 0x0200;
  1671. } else {
  1672. if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  1673. if (((u8 *)nv_connector->edid)[121] == 2)
  1674. lvds.lvds.script |= 0x0100;
  1675. } else
  1676. if (mode->clock >= bios->fp.duallink_transition_clk) {
  1677. lvds.lvds.script |= 0x0100;
  1678. }
  1679. if (lvds.lvds.script & 0x0100) {
  1680. if (bios->fp.strapless_is_24bit & 2)
  1681. lvds.lvds.script |= 0x0200;
  1682. } else {
  1683. if (bios->fp.strapless_is_24bit & 1)
  1684. lvds.lvds.script |= 0x0200;
  1685. }
  1686. if (nv_connector->base.display_info.bpc == 8)
  1687. lvds.lvds.script |= 0x0200;
  1688. }
  1689. nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
  1690. break;
  1691. case DCB_OUTPUT_DP:
  1692. if (nv_connector->base.display_info.bpc == 6) {
  1693. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  1694. depth = 0x2;
  1695. } else
  1696. if (nv_connector->base.display_info.bpc == 8) {
  1697. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  1698. depth = 0x5;
  1699. } else {
  1700. nv_encoder->dp.datarate = mode->clock * 30 / 8;
  1701. depth = 0x6;
  1702. }
  1703. if (nv_encoder->dcb->sorconf.link & 1)
  1704. proto = 0x8;
  1705. else
  1706. proto = 0x9;
  1707. break;
  1708. default:
  1709. BUG_ON(1);
  1710. break;
  1711. }
  1712. nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
  1713. if (nv50_vers(mast) >= GF110_DISP) {
  1714. u32 *push = evo_wait(mast, 3);
  1715. if (push) {
  1716. u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
  1717. u32 syncs = 0x00000001;
  1718. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1719. syncs |= 0x00000008;
  1720. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1721. syncs |= 0x00000010;
  1722. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1723. magic |= 0x00000001;
  1724. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1725. evo_data(push, syncs | (depth << 6));
  1726. evo_data(push, magic);
  1727. evo_kick(push, mast);
  1728. }
  1729. ctrl = proto << 8;
  1730. mask = 0x00000f00;
  1731. } else {
  1732. ctrl = (depth << 16) | (proto << 8);
  1733. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1734. ctrl |= 0x00001000;
  1735. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1736. ctrl |= 0x00002000;
  1737. mask = 0x000f3f00;
  1738. }
  1739. nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
  1740. }
  1741. static void
  1742. nv50_sor_destroy(struct drm_encoder *encoder)
  1743. {
  1744. drm_encoder_cleanup(encoder);
  1745. kfree(encoder);
  1746. }
  1747. static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
  1748. .dpms = nv50_sor_dpms,
  1749. .mode_fixup = nv50_sor_mode_fixup,
  1750. .prepare = nv50_sor_disconnect,
  1751. .commit = nv50_sor_commit,
  1752. .mode_set = nv50_sor_mode_set,
  1753. .disable = nv50_sor_disconnect,
  1754. .get_crtc = nv50_display_crtc_get,
  1755. };
  1756. static const struct drm_encoder_funcs nv50_sor_func = {
  1757. .destroy = nv50_sor_destroy,
  1758. };
  1759. static int
  1760. nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1761. {
  1762. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  1763. struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
  1764. struct nouveau_encoder *nv_encoder;
  1765. struct drm_encoder *encoder;
  1766. int type;
  1767. switch (dcbe->type) {
  1768. case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
  1769. case DCB_OUTPUT_TMDS:
  1770. case DCB_OUTPUT_DP:
  1771. default:
  1772. type = DRM_MODE_ENCODER_TMDS;
  1773. break;
  1774. }
  1775. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1776. if (!nv_encoder)
  1777. return -ENOMEM;
  1778. nv_encoder->dcb = dcbe;
  1779. nv_encoder->or = ffs(dcbe->or) - 1;
  1780. nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
  1781. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1782. encoder = to_drm_encoder(nv_encoder);
  1783. encoder->possible_crtcs = dcbe->heads;
  1784. encoder->possible_clones = 0;
  1785. drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
  1786. drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
  1787. drm_mode_connector_attach_encoder(connector, encoder);
  1788. return 0;
  1789. }
  1790. /******************************************************************************
  1791. * PIOR
  1792. *****************************************************************************/
  1793. static void
  1794. nv50_pior_dpms(struct drm_encoder *encoder, int mode)
  1795. {
  1796. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1797. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1798. struct {
  1799. struct nv50_disp_mthd_v1 base;
  1800. struct nv50_disp_pior_pwr_v0 pwr;
  1801. } args = {
  1802. .base.version = 1,
  1803. .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
  1804. .base.hasht = nv_encoder->dcb->hasht,
  1805. .base.hashm = nv_encoder->dcb->hashm,
  1806. .pwr.state = mode == DRM_MODE_DPMS_ON,
  1807. .pwr.type = nv_encoder->dcb->type,
  1808. };
  1809. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1810. }
  1811. static bool
  1812. nv50_pior_mode_fixup(struct drm_encoder *encoder,
  1813. const struct drm_display_mode *mode,
  1814. struct drm_display_mode *adjusted_mode)
  1815. {
  1816. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1817. struct nouveau_connector *nv_connector;
  1818. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1819. if (nv_connector && nv_connector->native_mode) {
  1820. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1821. int id = adjusted_mode->base.id;
  1822. *adjusted_mode = *nv_connector->native_mode;
  1823. adjusted_mode->base.id = id;
  1824. }
  1825. }
  1826. adjusted_mode->clock *= 2;
  1827. return true;
  1828. }
  1829. static void
  1830. nv50_pior_commit(struct drm_encoder *encoder)
  1831. {
  1832. }
  1833. static void
  1834. nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  1835. struct drm_display_mode *adjusted_mode)
  1836. {
  1837. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1838. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1839. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1840. struct nouveau_connector *nv_connector;
  1841. u8 owner = 1 << nv_crtc->index;
  1842. u8 proto, depth;
  1843. u32 *push;
  1844. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1845. switch (nv_connector->base.display_info.bpc) {
  1846. case 10: depth = 0x6; break;
  1847. case 8: depth = 0x5; break;
  1848. case 6: depth = 0x2; break;
  1849. default: depth = 0x0; break;
  1850. }
  1851. switch (nv_encoder->dcb->type) {
  1852. case DCB_OUTPUT_TMDS:
  1853. case DCB_OUTPUT_DP:
  1854. proto = 0x0;
  1855. break;
  1856. default:
  1857. BUG_ON(1);
  1858. break;
  1859. }
  1860. nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
  1861. push = evo_wait(mast, 8);
  1862. if (push) {
  1863. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  1864. u32 ctrl = (depth << 16) | (proto << 8) | owner;
  1865. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1866. ctrl |= 0x00001000;
  1867. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1868. ctrl |= 0x00002000;
  1869. evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
  1870. evo_data(push, ctrl);
  1871. }
  1872. evo_kick(push, mast);
  1873. }
  1874. nv_encoder->crtc = encoder->crtc;
  1875. }
  1876. static void
  1877. nv50_pior_disconnect(struct drm_encoder *encoder)
  1878. {
  1879. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1880. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1881. const int or = nv_encoder->or;
  1882. u32 *push;
  1883. if (nv_encoder->crtc) {
  1884. nv50_crtc_prepare(nv_encoder->crtc);
  1885. push = evo_wait(mast, 4);
  1886. if (push) {
  1887. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  1888. evo_mthd(push, 0x0700 + (or * 0x040), 1);
  1889. evo_data(push, 0x00000000);
  1890. }
  1891. evo_kick(push, mast);
  1892. }
  1893. }
  1894. nv_encoder->crtc = NULL;
  1895. }
  1896. static void
  1897. nv50_pior_destroy(struct drm_encoder *encoder)
  1898. {
  1899. drm_encoder_cleanup(encoder);
  1900. kfree(encoder);
  1901. }
  1902. static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
  1903. .dpms = nv50_pior_dpms,
  1904. .mode_fixup = nv50_pior_mode_fixup,
  1905. .prepare = nv50_pior_disconnect,
  1906. .commit = nv50_pior_commit,
  1907. .mode_set = nv50_pior_mode_set,
  1908. .disable = nv50_pior_disconnect,
  1909. .get_crtc = nv50_display_crtc_get,
  1910. };
  1911. static const struct drm_encoder_funcs nv50_pior_func = {
  1912. .destroy = nv50_pior_destroy,
  1913. };
  1914. static int
  1915. nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1916. {
  1917. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  1918. struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
  1919. struct nouveau_i2c_port *ddc = NULL;
  1920. struct nouveau_encoder *nv_encoder;
  1921. struct drm_encoder *encoder;
  1922. int type;
  1923. switch (dcbe->type) {
  1924. case DCB_OUTPUT_TMDS:
  1925. ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(dcbe->extdev));
  1926. type = DRM_MODE_ENCODER_TMDS;
  1927. break;
  1928. case DCB_OUTPUT_DP:
  1929. ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(dcbe->extdev));
  1930. type = DRM_MODE_ENCODER_TMDS;
  1931. break;
  1932. default:
  1933. return -ENODEV;
  1934. }
  1935. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1936. if (!nv_encoder)
  1937. return -ENOMEM;
  1938. nv_encoder->dcb = dcbe;
  1939. nv_encoder->or = ffs(dcbe->or) - 1;
  1940. nv_encoder->i2c = ddc;
  1941. encoder = to_drm_encoder(nv_encoder);
  1942. encoder->possible_crtcs = dcbe->heads;
  1943. encoder->possible_clones = 0;
  1944. drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type);
  1945. drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
  1946. drm_mode_connector_attach_encoder(connector, encoder);
  1947. return 0;
  1948. }
  1949. /******************************************************************************
  1950. * Framebuffer
  1951. *****************************************************************************/
  1952. static void
  1953. nv50_fbdma_fini(struct nv50_fbdma *fbdma)
  1954. {
  1955. int i;
  1956. for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
  1957. nvif_object_fini(&fbdma->base[i]);
  1958. nvif_object_fini(&fbdma->core);
  1959. list_del(&fbdma->head);
  1960. kfree(fbdma);
  1961. }
  1962. static int
  1963. nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
  1964. {
  1965. struct nouveau_drm *drm = nouveau_drm(dev);
  1966. struct nv50_disp *disp = nv50_disp(dev);
  1967. struct nv50_mast *mast = nv50_mast(dev);
  1968. struct __attribute__ ((packed)) {
  1969. struct nv_dma_v0 base;
  1970. union {
  1971. struct nv50_dma_v0 nv50;
  1972. struct gf100_dma_v0 gf100;
  1973. struct gf110_dma_v0 gf110;
  1974. };
  1975. } args = {};
  1976. struct nv50_fbdma *fbdma;
  1977. struct drm_crtc *crtc;
  1978. u32 size = sizeof(args.base);
  1979. int ret;
  1980. list_for_each_entry(fbdma, &disp->fbdma, head) {
  1981. if (fbdma->core.handle == name)
  1982. return 0;
  1983. }
  1984. fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
  1985. if (!fbdma)
  1986. return -ENOMEM;
  1987. list_add(&fbdma->head, &disp->fbdma);
  1988. args.base.target = NV_DMA_V0_TARGET_VRAM;
  1989. args.base.access = NV_DMA_V0_ACCESS_RDWR;
  1990. args.base.start = offset;
  1991. args.base.limit = offset + length - 1;
  1992. if (drm->device.info.chipset < 0x80) {
  1993. args.nv50.part = NV50_DMA_V0_PART_256;
  1994. size += sizeof(args.nv50);
  1995. } else
  1996. if (drm->device.info.chipset < 0xc0) {
  1997. args.nv50.part = NV50_DMA_V0_PART_256;
  1998. args.nv50.kind = kind;
  1999. size += sizeof(args.nv50);
  2000. } else
  2001. if (drm->device.info.chipset < 0xd0) {
  2002. args.gf100.kind = kind;
  2003. size += sizeof(args.gf100);
  2004. } else {
  2005. args.gf110.page = GF110_DMA_V0_PAGE_LP;
  2006. args.gf110.kind = kind;
  2007. size += sizeof(args.gf110);
  2008. }
  2009. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2010. struct nv50_head *head = nv50_head(crtc);
  2011. int ret = nvif_object_init(&head->sync.base.base.user, NULL,
  2012. name, NV_DMA_IN_MEMORY, &args, size,
  2013. &fbdma->base[head->base.index]);
  2014. if (ret) {
  2015. nv50_fbdma_fini(fbdma);
  2016. return ret;
  2017. }
  2018. }
  2019. ret = nvif_object_init(&mast->base.base.user, NULL, name,
  2020. NV_DMA_IN_MEMORY, &args, size,
  2021. &fbdma->core);
  2022. if (ret) {
  2023. nv50_fbdma_fini(fbdma);
  2024. return ret;
  2025. }
  2026. return 0;
  2027. }
  2028. static void
  2029. nv50_fb_dtor(struct drm_framebuffer *fb)
  2030. {
  2031. }
  2032. static int
  2033. nv50_fb_ctor(struct drm_framebuffer *fb)
  2034. {
  2035. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  2036. struct nouveau_drm *drm = nouveau_drm(fb->dev);
  2037. struct nouveau_bo *nvbo = nv_fb->nvbo;
  2038. struct nv50_disp *disp = nv50_disp(fb->dev);
  2039. u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
  2040. u8 tile = nvbo->tile_mode;
  2041. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) {
  2042. NV_ERROR(drm, "framebuffer requires contiguous bo\n");
  2043. return -EINVAL;
  2044. }
  2045. if (drm->device.info.chipset >= 0xc0)
  2046. tile >>= 4; /* yep.. */
  2047. switch (fb->depth) {
  2048. case 8: nv_fb->r_format = 0x1e00; break;
  2049. case 15: nv_fb->r_format = 0xe900; break;
  2050. case 16: nv_fb->r_format = 0xe800; break;
  2051. case 24:
  2052. case 32: nv_fb->r_format = 0xcf00; break;
  2053. case 30: nv_fb->r_format = 0xd100; break;
  2054. default:
  2055. NV_ERROR(drm, "unknown depth %d\n", fb->depth);
  2056. return -EINVAL;
  2057. }
  2058. if (disp->disp->oclass < G82_DISP) {
  2059. nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
  2060. (fb->pitches[0] | 0x00100000);
  2061. nv_fb->r_format |= kind << 16;
  2062. } else
  2063. if (disp->disp->oclass < GF110_DISP) {
  2064. nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
  2065. (fb->pitches[0] | 0x00100000);
  2066. } else {
  2067. nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
  2068. (fb->pitches[0] | 0x01000000);
  2069. }
  2070. nv_fb->r_handle = 0xffff0000 | kind;
  2071. return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0,
  2072. drm->device.info.ram_user, kind);
  2073. }
  2074. /******************************************************************************
  2075. * Init
  2076. *****************************************************************************/
  2077. void
  2078. nv50_display_fini(struct drm_device *dev)
  2079. {
  2080. }
  2081. int
  2082. nv50_display_init(struct drm_device *dev)
  2083. {
  2084. struct nv50_disp *disp = nv50_disp(dev);
  2085. struct drm_crtc *crtc;
  2086. u32 *push;
  2087. push = evo_wait(nv50_mast(dev), 32);
  2088. if (!push)
  2089. return -EBUSY;
  2090. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2091. struct nv50_sync *sync = nv50_sync(crtc);
  2092. nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
  2093. }
  2094. evo_mthd(push, 0x0088, 1);
  2095. evo_data(push, nv50_mast(dev)->base.sync.handle);
  2096. evo_kick(push, nv50_mast(dev));
  2097. return 0;
  2098. }
  2099. void
  2100. nv50_display_destroy(struct drm_device *dev)
  2101. {
  2102. struct nv50_disp *disp = nv50_disp(dev);
  2103. struct nv50_fbdma *fbdma, *fbtmp;
  2104. list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
  2105. nv50_fbdma_fini(fbdma);
  2106. }
  2107. nv50_dmac_destroy(&disp->mast.base, disp->disp);
  2108. nouveau_bo_unmap(disp->sync);
  2109. if (disp->sync)
  2110. nouveau_bo_unpin(disp->sync);
  2111. nouveau_bo_ref(NULL, &disp->sync);
  2112. nouveau_display(dev)->priv = NULL;
  2113. kfree(disp);
  2114. }
  2115. int
  2116. nv50_display_create(struct drm_device *dev)
  2117. {
  2118. struct nvif_device *device = &nouveau_drm(dev)->device;
  2119. struct nouveau_drm *drm = nouveau_drm(dev);
  2120. struct dcb_table *dcb = &drm->vbios.dcb;
  2121. struct drm_connector *connector, *tmp;
  2122. struct nv50_disp *disp;
  2123. struct dcb_output *dcbe;
  2124. int crtcs, ret, i;
  2125. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  2126. if (!disp)
  2127. return -ENOMEM;
  2128. INIT_LIST_HEAD(&disp->fbdma);
  2129. nouveau_display(dev)->priv = disp;
  2130. nouveau_display(dev)->dtor = nv50_display_destroy;
  2131. nouveau_display(dev)->init = nv50_display_init;
  2132. nouveau_display(dev)->fini = nv50_display_fini;
  2133. nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
  2134. nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
  2135. disp->disp = &nouveau_display(dev)->disp;
  2136. /* small shared memory area we use for notifiers and semaphores */
  2137. ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
  2138. 0, 0x0000, NULL, &disp->sync);
  2139. if (!ret) {
  2140. ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
  2141. if (!ret) {
  2142. ret = nouveau_bo_map(disp->sync);
  2143. if (ret)
  2144. nouveau_bo_unpin(disp->sync);
  2145. }
  2146. if (ret)
  2147. nouveau_bo_ref(NULL, &disp->sync);
  2148. }
  2149. if (ret)
  2150. goto out;
  2151. /* allocate master evo channel */
  2152. ret = nv50_core_create(disp->disp, disp->sync->bo.offset,
  2153. &disp->mast);
  2154. if (ret)
  2155. goto out;
  2156. /* create crtc objects to represent the hw heads */
  2157. if (disp->disp->oclass >= GF110_DISP)
  2158. crtcs = nvif_rd32(device, 0x022448);
  2159. else
  2160. crtcs = 2;
  2161. for (i = 0; i < crtcs; i++) {
  2162. ret = nv50_crtc_create(dev, i);
  2163. if (ret)
  2164. goto out;
  2165. }
  2166. /* create encoder/connector objects based on VBIOS DCB table */
  2167. for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
  2168. connector = nouveau_connector_create(dev, dcbe->connector);
  2169. if (IS_ERR(connector))
  2170. continue;
  2171. if (dcbe->location == DCB_LOC_ON_CHIP) {
  2172. switch (dcbe->type) {
  2173. case DCB_OUTPUT_TMDS:
  2174. case DCB_OUTPUT_LVDS:
  2175. case DCB_OUTPUT_DP:
  2176. ret = nv50_sor_create(connector, dcbe);
  2177. break;
  2178. case DCB_OUTPUT_ANALOG:
  2179. ret = nv50_dac_create(connector, dcbe);
  2180. break;
  2181. default:
  2182. ret = -ENODEV;
  2183. break;
  2184. }
  2185. } else {
  2186. ret = nv50_pior_create(connector, dcbe);
  2187. }
  2188. if (ret) {
  2189. NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
  2190. dcbe->location, dcbe->type,
  2191. ffs(dcbe->or) - 1, ret);
  2192. ret = 0;
  2193. }
  2194. }
  2195. /* cull any connectors we created that don't have an encoder */
  2196. list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
  2197. if (connector->encoder_ids[0])
  2198. continue;
  2199. NV_WARN(drm, "%s has no encoders, removing\n",
  2200. connector->name);
  2201. connector->funcs->destroy(connector);
  2202. }
  2203. out:
  2204. if (ret)
  2205. nv50_display_destroy(dev);
  2206. return ret;
  2207. }