nv50_display.c 70 KB

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