nv50_display.c 68 KB

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