nv50_display.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  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_raster_vblank_dmi(struct nouveau_crtc *nv_crtc, u32 usec)
  692. {
  693. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  694. u32 *push;
  695. push = evo_wait(mast, 8);
  696. if (!push)
  697. return -ENOMEM;
  698. evo_mthd(push, 0x0828 + (nv_crtc->index * 0x400), 1);
  699. evo_data(push, usec);
  700. evo_kick(push, mast);
  701. return 0;
  702. }
  703. static int
  704. nv50_crtc_set_color_vibrance(struct nouveau_crtc *nv_crtc, bool update)
  705. {
  706. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  707. u32 *push, hue, vib;
  708. int adj;
  709. adj = (nv_crtc->color_vibrance > 0) ? 50 : 0;
  710. vib = ((nv_crtc->color_vibrance * 2047 + adj) / 100) & 0xfff;
  711. hue = ((nv_crtc->vibrant_hue * 2047) / 100) & 0xfff;
  712. push = evo_wait(mast, 16);
  713. if (push) {
  714. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  715. evo_mthd(push, 0x08a8 + (nv_crtc->index * 0x400), 1);
  716. evo_data(push, (hue << 20) | (vib << 8));
  717. } else {
  718. evo_mthd(push, 0x0498 + (nv_crtc->index * 0x300), 1);
  719. evo_data(push, (hue << 20) | (vib << 8));
  720. }
  721. if (update) {
  722. evo_mthd(push, 0x0080, 1);
  723. evo_data(push, 0x00000000);
  724. }
  725. evo_kick(push, mast);
  726. }
  727. return 0;
  728. }
  729. static int
  730. nv50_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
  731. int x, int y, bool update)
  732. {
  733. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
  734. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  735. u32 *push;
  736. push = evo_wait(mast, 16);
  737. if (push) {
  738. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  739. evo_mthd(push, 0x0860 + (nv_crtc->index * 0x400), 1);
  740. evo_data(push, nvfb->nvbo->bo.offset >> 8);
  741. evo_mthd(push, 0x0868 + (nv_crtc->index * 0x400), 3);
  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_mthd(push, 0x08c0 + (nv_crtc->index * 0x400), 1);
  746. evo_data(push, (y << 16) | x);
  747. if (nv50_vers(mast) > NV50_DISP_CORE_CHANNEL_DMA) {
  748. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  749. evo_data(push, nvfb->r_handle);
  750. }
  751. } else {
  752. evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
  753. evo_data(push, nvfb->nvbo->bo.offset >> 8);
  754. evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
  755. evo_data(push, (fb->height << 16) | fb->width);
  756. evo_data(push, nvfb->r_pitch);
  757. evo_data(push, nvfb->r_format);
  758. evo_data(push, nvfb->r_handle);
  759. evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
  760. evo_data(push, (y << 16) | x);
  761. }
  762. if (update) {
  763. evo_mthd(push, 0x0080, 1);
  764. evo_data(push, 0x00000000);
  765. }
  766. evo_kick(push, mast);
  767. }
  768. nv_crtc->fb.handle = nvfb->r_handle;
  769. return 0;
  770. }
  771. static void
  772. nv50_crtc_cursor_show(struct nouveau_crtc *nv_crtc)
  773. {
  774. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  775. u32 *push = evo_wait(mast, 16);
  776. if (push) {
  777. if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
  778. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
  779. evo_data(push, 0x85000000);
  780. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  781. } else
  782. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  783. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 2);
  784. evo_data(push, 0x85000000);
  785. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  786. evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
  787. evo_data(push, mast->base.vram.handle);
  788. } else {
  789. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
  790. evo_data(push, 0x85000000);
  791. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  792. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  793. evo_data(push, mast->base.vram.handle);
  794. }
  795. evo_kick(push, mast);
  796. }
  797. }
  798. static void
  799. nv50_crtc_cursor_hide(struct nouveau_crtc *nv_crtc)
  800. {
  801. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  802. u32 *push = evo_wait(mast, 16);
  803. if (push) {
  804. if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
  805. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
  806. evo_data(push, 0x05000000);
  807. } else
  808. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  809. evo_mthd(push, 0x0880 + (nv_crtc->index * 0x400), 1);
  810. evo_data(push, 0x05000000);
  811. evo_mthd(push, 0x089c + (nv_crtc->index * 0x400), 1);
  812. evo_data(push, 0x00000000);
  813. } else {
  814. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
  815. evo_data(push, 0x05000000);
  816. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  817. evo_data(push, 0x00000000);
  818. }
  819. evo_kick(push, mast);
  820. }
  821. }
  822. static void
  823. nv50_crtc_cursor_show_hide(struct nouveau_crtc *nv_crtc, bool show, bool update)
  824. {
  825. struct nv50_mast *mast = nv50_mast(nv_crtc->base.dev);
  826. if (show)
  827. nv50_crtc_cursor_show(nv_crtc);
  828. else
  829. nv50_crtc_cursor_hide(nv_crtc);
  830. if (update) {
  831. u32 *push = evo_wait(mast, 2);
  832. if (push) {
  833. evo_mthd(push, 0x0080, 1);
  834. evo_data(push, 0x00000000);
  835. evo_kick(push, mast);
  836. }
  837. }
  838. }
  839. static void
  840. nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
  841. {
  842. }
  843. static void
  844. nv50_crtc_prepare(struct drm_crtc *crtc)
  845. {
  846. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  847. struct nv50_mast *mast = nv50_mast(crtc->dev);
  848. u32 *push;
  849. nv50_display_flip_stop(crtc);
  850. push = evo_wait(mast, 6);
  851. if (push) {
  852. if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
  853. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  854. evo_data(push, 0x00000000);
  855. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
  856. evo_data(push, 0x40000000);
  857. } else
  858. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  859. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  860. evo_data(push, 0x00000000);
  861. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 1);
  862. evo_data(push, 0x40000000);
  863. evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
  864. evo_data(push, 0x00000000);
  865. } else {
  866. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  867. evo_data(push, 0x00000000);
  868. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
  869. evo_data(push, 0x03000000);
  870. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  871. evo_data(push, 0x00000000);
  872. }
  873. evo_kick(push, mast);
  874. }
  875. nv50_crtc_cursor_show_hide(nv_crtc, false, false);
  876. }
  877. static void
  878. nv50_crtc_commit(struct drm_crtc *crtc)
  879. {
  880. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  881. struct nv50_mast *mast = nv50_mast(crtc->dev);
  882. u32 *push;
  883. push = evo_wait(mast, 32);
  884. if (push) {
  885. if (nv50_vers(mast) < G82_DISP_CORE_CHANNEL_DMA) {
  886. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  887. evo_data(push, nv_crtc->fb.handle);
  888. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
  889. evo_data(push, 0xc0000000);
  890. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  891. } else
  892. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  893. evo_mthd(push, 0x0874 + (nv_crtc->index * 0x400), 1);
  894. evo_data(push, nv_crtc->fb.handle);
  895. evo_mthd(push, 0x0840 + (nv_crtc->index * 0x400), 2);
  896. evo_data(push, 0xc0000000);
  897. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  898. evo_mthd(push, 0x085c + (nv_crtc->index * 0x400), 1);
  899. evo_data(push, mast->base.vram.handle);
  900. } else {
  901. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  902. evo_data(push, nv_crtc->fb.handle);
  903. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
  904. evo_data(push, 0x83000000);
  905. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  906. evo_data(push, 0x00000000);
  907. evo_data(push, 0x00000000);
  908. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  909. evo_data(push, mast->base.vram.handle);
  910. evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
  911. evo_data(push, 0xffffff00);
  912. }
  913. evo_kick(push, mast);
  914. }
  915. nv50_crtc_cursor_show_hide(nv_crtc, nv_crtc->cursor.visible, true);
  916. nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
  917. }
  918. static bool
  919. nv50_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
  920. struct drm_display_mode *adjusted_mode)
  921. {
  922. drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
  923. return true;
  924. }
  925. static int
  926. nv50_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
  927. {
  928. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
  929. struct nv50_head *head = nv50_head(crtc);
  930. int ret;
  931. ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
  932. if (ret == 0) {
  933. if (head->image)
  934. nouveau_bo_unpin(head->image);
  935. nouveau_bo_ref(nvfb->nvbo, &head->image);
  936. }
  937. return ret;
  938. }
  939. static int
  940. nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
  941. struct drm_display_mode *mode, int x, int y,
  942. struct drm_framebuffer *old_fb)
  943. {
  944. struct nv50_mast *mast = nv50_mast(crtc->dev);
  945. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  946. struct nouveau_connector *nv_connector;
  947. u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
  948. u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
  949. u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
  950. u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
  951. u32 vblan2e = 0, vblan2s = 1, vblankus = 0;
  952. u32 *push;
  953. int ret;
  954. hactive = mode->htotal;
  955. hsynce = mode->hsync_end - mode->hsync_start - 1;
  956. hbackp = mode->htotal - mode->hsync_end;
  957. hblanke = hsynce + hbackp;
  958. hfrontp = mode->hsync_start - mode->hdisplay;
  959. hblanks = mode->htotal - hfrontp - 1;
  960. vactive = mode->vtotal * vscan / ilace;
  961. vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
  962. vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
  963. vblanke = vsynce + vbackp;
  964. vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
  965. vblanks = vactive - vfrontp - 1;
  966. /* XXX: Safe underestimate, even "0" works */
  967. vblankus = (vactive - mode->vdisplay - 2) * hactive;
  968. vblankus *= 1000;
  969. vblankus /= mode->clock;
  970. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  971. vblan2e = vactive + vsynce + vbackp;
  972. vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
  973. vactive = (vactive * 2) + 1;
  974. }
  975. ret = nv50_crtc_swap_fbs(crtc, old_fb);
  976. if (ret)
  977. return ret;
  978. push = evo_wait(mast, 64);
  979. if (push) {
  980. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  981. evo_mthd(push, 0x0804 + (nv_crtc->index * 0x400), 2);
  982. evo_data(push, 0x00800000 | mode->clock);
  983. evo_data(push, (ilace == 2) ? 2 : 0);
  984. evo_mthd(push, 0x0810 + (nv_crtc->index * 0x400), 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, 0x082c + (nv_crtc->index * 0x400), 1);
  992. evo_data(push, 0x00000000);
  993. evo_mthd(push, 0x0900 + (nv_crtc->index * 0x400), 2);
  994. evo_data(push, 0x00000311);
  995. evo_data(push, 0x00000100);
  996. } else {
  997. evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
  998. evo_data(push, 0x00000000);
  999. evo_data(push, (vactive << 16) | hactive);
  1000. evo_data(push, ( vsynce << 16) | hsynce);
  1001. evo_data(push, (vblanke << 16) | hblanke);
  1002. evo_data(push, (vblanks << 16) | hblanks);
  1003. evo_data(push, (vblan2e << 16) | vblan2s);
  1004. evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
  1005. evo_data(push, 0x00000000); /* ??? */
  1006. evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
  1007. evo_data(push, mode->clock * 1000);
  1008. evo_data(push, 0x00200000); /* ??? */
  1009. evo_data(push, mode->clock * 1000);
  1010. evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
  1011. evo_data(push, 0x00000311);
  1012. evo_data(push, 0x00000100);
  1013. }
  1014. evo_kick(push, mast);
  1015. }
  1016. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  1017. nv50_crtc_set_dither(nv_crtc, false);
  1018. nv50_crtc_set_scale(nv_crtc, false);
  1019. /* G94 only accepts this after setting scale */
  1020. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA)
  1021. nv50_crtc_set_raster_vblank_dmi(nv_crtc, vblankus);
  1022. nv50_crtc_set_color_vibrance(nv_crtc, false);
  1023. nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, false);
  1024. return 0;
  1025. }
  1026. static int
  1027. nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  1028. struct drm_framebuffer *old_fb)
  1029. {
  1030. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  1031. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1032. int ret;
  1033. if (!crtc->primary->fb) {
  1034. NV_DEBUG(drm, "No FB bound\n");
  1035. return 0;
  1036. }
  1037. ret = nv50_crtc_swap_fbs(crtc, old_fb);
  1038. if (ret)
  1039. return ret;
  1040. nv50_display_flip_stop(crtc);
  1041. nv50_crtc_set_image(nv_crtc, crtc->primary->fb, x, y, true);
  1042. nv50_display_flip_next(crtc, crtc->primary->fb, NULL, 1);
  1043. return 0;
  1044. }
  1045. static int
  1046. nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
  1047. struct drm_framebuffer *fb, int x, int y,
  1048. enum mode_set_atomic state)
  1049. {
  1050. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1051. nv50_display_flip_stop(crtc);
  1052. nv50_crtc_set_image(nv_crtc, fb, x, y, true);
  1053. return 0;
  1054. }
  1055. static void
  1056. nv50_crtc_lut_load(struct drm_crtc *crtc)
  1057. {
  1058. struct nv50_disp *disp = nv50_disp(crtc->dev);
  1059. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1060. void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
  1061. int i;
  1062. for (i = 0; i < 256; i++) {
  1063. u16 r = nv_crtc->lut.r[i] >> 2;
  1064. u16 g = nv_crtc->lut.g[i] >> 2;
  1065. u16 b = nv_crtc->lut.b[i] >> 2;
  1066. if (disp->disp->oclass < GF110_DISP) {
  1067. writew(r + 0x0000, lut + (i * 0x08) + 0);
  1068. writew(g + 0x0000, lut + (i * 0x08) + 2);
  1069. writew(b + 0x0000, lut + (i * 0x08) + 4);
  1070. } else {
  1071. writew(r + 0x6000, lut + (i * 0x20) + 0);
  1072. writew(g + 0x6000, lut + (i * 0x20) + 2);
  1073. writew(b + 0x6000, lut + (i * 0x20) + 4);
  1074. }
  1075. }
  1076. }
  1077. static void
  1078. nv50_crtc_disable(struct drm_crtc *crtc)
  1079. {
  1080. struct nv50_head *head = nv50_head(crtc);
  1081. evo_sync(crtc->dev);
  1082. if (head->image)
  1083. nouveau_bo_unpin(head->image);
  1084. nouveau_bo_ref(NULL, &head->image);
  1085. }
  1086. static int
  1087. nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  1088. uint32_t handle, uint32_t width, uint32_t height)
  1089. {
  1090. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1091. struct drm_device *dev = crtc->dev;
  1092. struct drm_gem_object *gem;
  1093. struct nouveau_bo *nvbo;
  1094. bool visible = (handle != 0);
  1095. int i, ret = 0;
  1096. if (visible) {
  1097. if (width != 64 || height != 64)
  1098. return -EINVAL;
  1099. gem = drm_gem_object_lookup(dev, file_priv, handle);
  1100. if (unlikely(!gem))
  1101. return -ENOENT;
  1102. nvbo = nouveau_gem_object(gem);
  1103. ret = nouveau_bo_map(nvbo);
  1104. if (ret == 0) {
  1105. for (i = 0; i < 64 * 64; i++) {
  1106. u32 v = nouveau_bo_rd32(nvbo, i);
  1107. nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
  1108. }
  1109. nouveau_bo_unmap(nvbo);
  1110. }
  1111. drm_gem_object_unreference_unlocked(gem);
  1112. }
  1113. if (visible != nv_crtc->cursor.visible) {
  1114. nv50_crtc_cursor_show_hide(nv_crtc, visible, true);
  1115. nv_crtc->cursor.visible = visible;
  1116. }
  1117. return ret;
  1118. }
  1119. static int
  1120. nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  1121. {
  1122. struct nv50_curs *curs = nv50_curs(crtc);
  1123. struct nv50_chan *chan = nv50_chan(curs);
  1124. nvif_wr32(&chan->user, 0x0084, (y << 16) | (x & 0xffff));
  1125. nvif_wr32(&chan->user, 0x0080, 0x00000000);
  1126. return 0;
  1127. }
  1128. static void
  1129. nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  1130. uint32_t start, uint32_t size)
  1131. {
  1132. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1133. u32 end = min_t(u32, start + size, 256);
  1134. u32 i;
  1135. for (i = start; i < end; i++) {
  1136. nv_crtc->lut.r[i] = r[i];
  1137. nv_crtc->lut.g[i] = g[i];
  1138. nv_crtc->lut.b[i] = b[i];
  1139. }
  1140. nv50_crtc_lut_load(crtc);
  1141. }
  1142. static void
  1143. nv50_crtc_destroy(struct drm_crtc *crtc)
  1144. {
  1145. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  1146. struct nv50_disp *disp = nv50_disp(crtc->dev);
  1147. struct nv50_head *head = nv50_head(crtc);
  1148. struct nv50_fbdma *fbdma;
  1149. list_for_each_entry(fbdma, &disp->fbdma, head) {
  1150. nvif_object_fini(&fbdma->base[nv_crtc->index]);
  1151. }
  1152. nv50_dmac_destroy(&head->ovly.base, disp->disp);
  1153. nv50_pioc_destroy(&head->oimm.base);
  1154. nv50_dmac_destroy(&head->sync.base, disp->disp);
  1155. nv50_pioc_destroy(&head->curs.base);
  1156. /*XXX: this shouldn't be necessary, but the core doesn't call
  1157. * disconnect() during the cleanup paths
  1158. */
  1159. if (head->image)
  1160. nouveau_bo_unpin(head->image);
  1161. nouveau_bo_ref(NULL, &head->image);
  1162. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  1163. if (nv_crtc->cursor.nvbo)
  1164. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  1165. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  1166. nouveau_bo_unmap(nv_crtc->lut.nvbo);
  1167. if (nv_crtc->lut.nvbo)
  1168. nouveau_bo_unpin(nv_crtc->lut.nvbo);
  1169. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  1170. drm_crtc_cleanup(crtc);
  1171. kfree(crtc);
  1172. }
  1173. static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
  1174. .dpms = nv50_crtc_dpms,
  1175. .prepare = nv50_crtc_prepare,
  1176. .commit = nv50_crtc_commit,
  1177. .mode_fixup = nv50_crtc_mode_fixup,
  1178. .mode_set = nv50_crtc_mode_set,
  1179. .mode_set_base = nv50_crtc_mode_set_base,
  1180. .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
  1181. .load_lut = nv50_crtc_lut_load,
  1182. .disable = nv50_crtc_disable,
  1183. };
  1184. static const struct drm_crtc_funcs nv50_crtc_func = {
  1185. .cursor_set = nv50_crtc_cursor_set,
  1186. .cursor_move = nv50_crtc_cursor_move,
  1187. .gamma_set = nv50_crtc_gamma_set,
  1188. .set_config = nouveau_crtc_set_config,
  1189. .destroy = nv50_crtc_destroy,
  1190. .page_flip = nouveau_crtc_page_flip,
  1191. };
  1192. static void
  1193. nv50_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
  1194. {
  1195. }
  1196. static void
  1197. nv50_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
  1198. {
  1199. }
  1200. static int
  1201. nv50_crtc_create(struct drm_device *dev, int index)
  1202. {
  1203. struct nv50_disp *disp = nv50_disp(dev);
  1204. struct nv50_head *head;
  1205. struct drm_crtc *crtc;
  1206. int ret, i;
  1207. head = kzalloc(sizeof(*head), GFP_KERNEL);
  1208. if (!head)
  1209. return -ENOMEM;
  1210. head->base.index = index;
  1211. head->base.set_dither = nv50_crtc_set_dither;
  1212. head->base.set_scale = nv50_crtc_set_scale;
  1213. head->base.set_color_vibrance = nv50_crtc_set_color_vibrance;
  1214. head->base.color_vibrance = 50;
  1215. head->base.vibrant_hue = 0;
  1216. head->base.cursor.set_offset = nv50_cursor_set_offset;
  1217. head->base.cursor.set_pos = nv50_cursor_set_pos;
  1218. for (i = 0; i < 256; i++) {
  1219. head->base.lut.r[i] = i << 8;
  1220. head->base.lut.g[i] = i << 8;
  1221. head->base.lut.b[i] = i << 8;
  1222. }
  1223. crtc = &head->base.base;
  1224. drm_crtc_init(dev, crtc, &nv50_crtc_func);
  1225. drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
  1226. drm_mode_crtc_set_gamma_size(crtc, 256);
  1227. ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
  1228. 0, 0x0000, NULL, NULL, &head->base.lut.nvbo);
  1229. if (!ret) {
  1230. ret = nouveau_bo_pin(head->base.lut.nvbo, TTM_PL_FLAG_VRAM);
  1231. if (!ret) {
  1232. ret = nouveau_bo_map(head->base.lut.nvbo);
  1233. if (ret)
  1234. nouveau_bo_unpin(head->base.lut.nvbo);
  1235. }
  1236. if (ret)
  1237. nouveau_bo_ref(NULL, &head->base.lut.nvbo);
  1238. }
  1239. if (ret)
  1240. goto out;
  1241. nv50_crtc_lut_load(crtc);
  1242. /* allocate cursor resources */
  1243. ret = nv50_curs_create(disp->disp, index, &head->curs);
  1244. if (ret)
  1245. goto out;
  1246. ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
  1247. 0, 0x0000, NULL, NULL, &head->base.cursor.nvbo);
  1248. if (!ret) {
  1249. ret = nouveau_bo_pin(head->base.cursor.nvbo, TTM_PL_FLAG_VRAM);
  1250. if (!ret) {
  1251. ret = nouveau_bo_map(head->base.cursor.nvbo);
  1252. if (ret)
  1253. nouveau_bo_unpin(head->base.lut.nvbo);
  1254. }
  1255. if (ret)
  1256. nouveau_bo_ref(NULL, &head->base.cursor.nvbo);
  1257. }
  1258. if (ret)
  1259. goto out;
  1260. /* allocate page flip / sync resources */
  1261. ret = nv50_base_create(disp->disp, index, disp->sync->bo.offset,
  1262. &head->sync);
  1263. if (ret)
  1264. goto out;
  1265. head->sync.addr = EVO_FLIP_SEM0(index);
  1266. head->sync.data = 0x00000000;
  1267. /* allocate overlay resources */
  1268. ret = nv50_oimm_create(disp->disp, index, &head->oimm);
  1269. if (ret)
  1270. goto out;
  1271. ret = nv50_ovly_create(disp->disp, index, disp->sync->bo.offset,
  1272. &head->ovly);
  1273. if (ret)
  1274. goto out;
  1275. out:
  1276. if (ret)
  1277. nv50_crtc_destroy(crtc);
  1278. return ret;
  1279. }
  1280. /******************************************************************************
  1281. * DAC
  1282. *****************************************************************************/
  1283. static void
  1284. nv50_dac_dpms(struct drm_encoder *encoder, int mode)
  1285. {
  1286. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1287. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1288. struct {
  1289. struct nv50_disp_mthd_v1 base;
  1290. struct nv50_disp_dac_pwr_v0 pwr;
  1291. } args = {
  1292. .base.version = 1,
  1293. .base.method = NV50_DISP_MTHD_V1_DAC_PWR,
  1294. .base.hasht = nv_encoder->dcb->hasht,
  1295. .base.hashm = nv_encoder->dcb->hashm,
  1296. .pwr.state = 1,
  1297. .pwr.data = 1,
  1298. .pwr.vsync = (mode != DRM_MODE_DPMS_SUSPEND &&
  1299. mode != DRM_MODE_DPMS_OFF),
  1300. .pwr.hsync = (mode != DRM_MODE_DPMS_STANDBY &&
  1301. mode != DRM_MODE_DPMS_OFF),
  1302. };
  1303. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1304. }
  1305. static bool
  1306. nv50_dac_mode_fixup(struct drm_encoder *encoder,
  1307. const struct drm_display_mode *mode,
  1308. struct drm_display_mode *adjusted_mode)
  1309. {
  1310. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1311. struct nouveau_connector *nv_connector;
  1312. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1313. if (nv_connector && nv_connector->native_mode) {
  1314. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1315. int id = adjusted_mode->base.id;
  1316. *adjusted_mode = *nv_connector->native_mode;
  1317. adjusted_mode->base.id = id;
  1318. }
  1319. }
  1320. return true;
  1321. }
  1322. static void
  1323. nv50_dac_commit(struct drm_encoder *encoder)
  1324. {
  1325. }
  1326. static void
  1327. nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  1328. struct drm_display_mode *adjusted_mode)
  1329. {
  1330. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1331. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1332. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1333. u32 *push;
  1334. nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
  1335. push = evo_wait(mast, 8);
  1336. if (push) {
  1337. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  1338. u32 syncs = 0x00000000;
  1339. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1340. syncs |= 0x00000001;
  1341. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1342. syncs |= 0x00000002;
  1343. evo_mthd(push, 0x0400 + (nv_encoder->or * 0x080), 2);
  1344. evo_data(push, 1 << nv_crtc->index);
  1345. evo_data(push, syncs);
  1346. } else {
  1347. u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
  1348. u32 syncs = 0x00000001;
  1349. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1350. syncs |= 0x00000008;
  1351. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1352. syncs |= 0x00000010;
  1353. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1354. magic |= 0x00000001;
  1355. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1356. evo_data(push, syncs);
  1357. evo_data(push, magic);
  1358. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
  1359. evo_data(push, 1 << nv_crtc->index);
  1360. }
  1361. evo_kick(push, mast);
  1362. }
  1363. nv_encoder->crtc = encoder->crtc;
  1364. }
  1365. static void
  1366. nv50_dac_disconnect(struct drm_encoder *encoder)
  1367. {
  1368. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1369. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1370. const int or = nv_encoder->or;
  1371. u32 *push;
  1372. if (nv_encoder->crtc) {
  1373. nv50_crtc_prepare(nv_encoder->crtc);
  1374. push = evo_wait(mast, 4);
  1375. if (push) {
  1376. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  1377. evo_mthd(push, 0x0400 + (or * 0x080), 1);
  1378. evo_data(push, 0x00000000);
  1379. } else {
  1380. evo_mthd(push, 0x0180 + (or * 0x020), 1);
  1381. evo_data(push, 0x00000000);
  1382. }
  1383. evo_kick(push, mast);
  1384. }
  1385. }
  1386. nv_encoder->crtc = NULL;
  1387. }
  1388. static enum drm_connector_status
  1389. nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  1390. {
  1391. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1392. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1393. struct {
  1394. struct nv50_disp_mthd_v1 base;
  1395. struct nv50_disp_dac_load_v0 load;
  1396. } args = {
  1397. .base.version = 1,
  1398. .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
  1399. .base.hasht = nv_encoder->dcb->hasht,
  1400. .base.hashm = nv_encoder->dcb->hashm,
  1401. };
  1402. int ret;
  1403. args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
  1404. if (args.load.data == 0)
  1405. args.load.data = 340;
  1406. ret = nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1407. if (ret || !args.load.load)
  1408. return connector_status_disconnected;
  1409. return connector_status_connected;
  1410. }
  1411. static void
  1412. nv50_dac_destroy(struct drm_encoder *encoder)
  1413. {
  1414. drm_encoder_cleanup(encoder);
  1415. kfree(encoder);
  1416. }
  1417. static const struct drm_encoder_helper_funcs nv50_dac_hfunc = {
  1418. .dpms = nv50_dac_dpms,
  1419. .mode_fixup = nv50_dac_mode_fixup,
  1420. .prepare = nv50_dac_disconnect,
  1421. .commit = nv50_dac_commit,
  1422. .mode_set = nv50_dac_mode_set,
  1423. .disable = nv50_dac_disconnect,
  1424. .get_crtc = nv50_display_crtc_get,
  1425. .detect = nv50_dac_detect
  1426. };
  1427. static const struct drm_encoder_funcs nv50_dac_func = {
  1428. .destroy = nv50_dac_destroy,
  1429. };
  1430. static int
  1431. nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1432. {
  1433. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  1434. struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
  1435. struct nouveau_encoder *nv_encoder;
  1436. struct drm_encoder *encoder;
  1437. int type = DRM_MODE_ENCODER_DAC;
  1438. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1439. if (!nv_encoder)
  1440. return -ENOMEM;
  1441. nv_encoder->dcb = dcbe;
  1442. nv_encoder->or = ffs(dcbe->or) - 1;
  1443. nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
  1444. encoder = to_drm_encoder(nv_encoder);
  1445. encoder->possible_crtcs = dcbe->heads;
  1446. encoder->possible_clones = 0;
  1447. drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type);
  1448. drm_encoder_helper_add(encoder, &nv50_dac_hfunc);
  1449. drm_mode_connector_attach_encoder(connector, encoder);
  1450. return 0;
  1451. }
  1452. /******************************************************************************
  1453. * Audio
  1454. *****************************************************************************/
  1455. static void
  1456. nv50_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1457. {
  1458. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1459. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1460. struct nouveau_connector *nv_connector;
  1461. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1462. struct __packed {
  1463. struct {
  1464. struct nv50_disp_mthd_v1 mthd;
  1465. struct nv50_disp_sor_hda_eld_v0 eld;
  1466. } base;
  1467. u8 data[sizeof(nv_connector->base.eld)];
  1468. } args = {
  1469. .base.mthd.version = 1,
  1470. .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
  1471. .base.mthd.hasht = nv_encoder->dcb->hasht,
  1472. .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
  1473. (0x0100 << nv_crtc->index),
  1474. };
  1475. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1476. if (!drm_detect_monitor_audio(nv_connector->edid))
  1477. return;
  1478. drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
  1479. memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
  1480. nvif_mthd(disp->disp, 0, &args, sizeof(args.base) + args.data[2] * 4);
  1481. }
  1482. static void
  1483. nv50_audio_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
  1484. {
  1485. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1486. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1487. struct {
  1488. struct nv50_disp_mthd_v1 base;
  1489. struct nv50_disp_sor_hda_eld_v0 eld;
  1490. } args = {
  1491. .base.version = 1,
  1492. .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
  1493. .base.hasht = nv_encoder->dcb->hasht,
  1494. .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
  1495. (0x0100 << nv_crtc->index),
  1496. };
  1497. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1498. }
  1499. /******************************************************************************
  1500. * HDMI
  1501. *****************************************************************************/
  1502. static void
  1503. nv50_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1504. {
  1505. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1506. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1507. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1508. struct {
  1509. struct nv50_disp_mthd_v1 base;
  1510. struct nv50_disp_sor_hdmi_pwr_v0 pwr;
  1511. } args = {
  1512. .base.version = 1,
  1513. .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
  1514. .base.hasht = nv_encoder->dcb->hasht,
  1515. .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
  1516. (0x0100 << nv_crtc->index),
  1517. .pwr.state = 1,
  1518. .pwr.rekey = 56, /* binary driver, and tegra, constant */
  1519. };
  1520. struct nouveau_connector *nv_connector;
  1521. u32 max_ac_packet;
  1522. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1523. if (!drm_detect_hdmi_monitor(nv_connector->edid))
  1524. return;
  1525. max_ac_packet = mode->htotal - mode->hdisplay;
  1526. max_ac_packet -= args.pwr.rekey;
  1527. max_ac_packet -= 18; /* constant from tegra */
  1528. args.pwr.max_ac_packet = max_ac_packet / 32;
  1529. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1530. nv50_audio_mode_set(encoder, mode);
  1531. }
  1532. static void
  1533. nv50_hdmi_disconnect(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
  1534. {
  1535. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1536. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1537. struct {
  1538. struct nv50_disp_mthd_v1 base;
  1539. struct nv50_disp_sor_hdmi_pwr_v0 pwr;
  1540. } args = {
  1541. .base.version = 1,
  1542. .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
  1543. .base.hasht = nv_encoder->dcb->hasht,
  1544. .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
  1545. (0x0100 << nv_crtc->index),
  1546. };
  1547. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1548. }
  1549. /******************************************************************************
  1550. * SOR
  1551. *****************************************************************************/
  1552. static void
  1553. nv50_sor_dpms(struct drm_encoder *encoder, int mode)
  1554. {
  1555. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1556. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1557. struct {
  1558. struct nv50_disp_mthd_v1 base;
  1559. struct nv50_disp_sor_pwr_v0 pwr;
  1560. } args = {
  1561. .base.version = 1,
  1562. .base.method = NV50_DISP_MTHD_V1_SOR_PWR,
  1563. .base.hasht = nv_encoder->dcb->hasht,
  1564. .base.hashm = nv_encoder->dcb->hashm,
  1565. .pwr.state = mode == DRM_MODE_DPMS_ON,
  1566. };
  1567. struct {
  1568. struct nv50_disp_mthd_v1 base;
  1569. struct nv50_disp_sor_dp_pwr_v0 pwr;
  1570. } link = {
  1571. .base.version = 1,
  1572. .base.method = NV50_DISP_MTHD_V1_SOR_DP_PWR,
  1573. .base.hasht = nv_encoder->dcb->hasht,
  1574. .base.hashm = nv_encoder->dcb->hashm,
  1575. .pwr.state = mode == DRM_MODE_DPMS_ON,
  1576. };
  1577. struct drm_device *dev = encoder->dev;
  1578. struct drm_encoder *partner;
  1579. nv_encoder->last_dpms = mode;
  1580. list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
  1581. struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
  1582. if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
  1583. continue;
  1584. if (nv_partner != nv_encoder &&
  1585. nv_partner->dcb->or == nv_encoder->dcb->or) {
  1586. if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
  1587. return;
  1588. break;
  1589. }
  1590. }
  1591. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  1592. args.pwr.state = 1;
  1593. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1594. nvif_mthd(disp->disp, 0, &link, sizeof(link));
  1595. } else {
  1596. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1597. }
  1598. }
  1599. static bool
  1600. nv50_sor_mode_fixup(struct drm_encoder *encoder,
  1601. const struct drm_display_mode *mode,
  1602. struct drm_display_mode *adjusted_mode)
  1603. {
  1604. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1605. struct nouveau_connector *nv_connector;
  1606. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1607. if (nv_connector && nv_connector->native_mode) {
  1608. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1609. int id = adjusted_mode->base.id;
  1610. *adjusted_mode = *nv_connector->native_mode;
  1611. adjusted_mode->base.id = id;
  1612. }
  1613. }
  1614. return true;
  1615. }
  1616. static void
  1617. nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
  1618. {
  1619. struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
  1620. u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
  1621. if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
  1622. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  1623. evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
  1624. evo_data(push, (nv_encoder->ctrl = temp));
  1625. } else {
  1626. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
  1627. evo_data(push, (nv_encoder->ctrl = temp));
  1628. }
  1629. evo_kick(push, mast);
  1630. }
  1631. }
  1632. static void
  1633. nv50_sor_disconnect(struct drm_encoder *encoder)
  1634. {
  1635. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1636. struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
  1637. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1638. nv_encoder->crtc = NULL;
  1639. if (nv_crtc) {
  1640. nv50_crtc_prepare(&nv_crtc->base);
  1641. nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
  1642. nv50_audio_disconnect(encoder, nv_crtc);
  1643. nv50_hdmi_disconnect(&nv_encoder->base.base, nv_crtc);
  1644. }
  1645. }
  1646. static void
  1647. nv50_sor_commit(struct drm_encoder *encoder)
  1648. {
  1649. }
  1650. static void
  1651. nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  1652. struct drm_display_mode *mode)
  1653. {
  1654. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1655. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1656. struct {
  1657. struct nv50_disp_mthd_v1 base;
  1658. struct nv50_disp_sor_lvds_script_v0 lvds;
  1659. } lvds = {
  1660. .base.version = 1,
  1661. .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
  1662. .base.hasht = nv_encoder->dcb->hasht,
  1663. .base.hashm = nv_encoder->dcb->hashm,
  1664. };
  1665. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1666. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1667. struct drm_device *dev = encoder->dev;
  1668. struct nouveau_drm *drm = nouveau_drm(dev);
  1669. struct nouveau_connector *nv_connector;
  1670. struct nvbios *bios = &drm->vbios;
  1671. u32 mask, ctrl;
  1672. u8 owner = 1 << nv_crtc->index;
  1673. u8 proto = 0xf;
  1674. u8 depth = 0x0;
  1675. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1676. nv_encoder->crtc = encoder->crtc;
  1677. switch (nv_encoder->dcb->type) {
  1678. case DCB_OUTPUT_TMDS:
  1679. if (nv_encoder->dcb->sorconf.link & 1) {
  1680. if (mode->clock < 165000)
  1681. proto = 0x1;
  1682. else
  1683. proto = 0x5;
  1684. } else {
  1685. proto = 0x2;
  1686. }
  1687. nv50_hdmi_mode_set(&nv_encoder->base.base, mode);
  1688. break;
  1689. case DCB_OUTPUT_LVDS:
  1690. proto = 0x0;
  1691. if (bios->fp_no_ddc) {
  1692. if (bios->fp.dual_link)
  1693. lvds.lvds.script |= 0x0100;
  1694. if (bios->fp.if_is_24bit)
  1695. lvds.lvds.script |= 0x0200;
  1696. } else {
  1697. if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  1698. if (((u8 *)nv_connector->edid)[121] == 2)
  1699. lvds.lvds.script |= 0x0100;
  1700. } else
  1701. if (mode->clock >= bios->fp.duallink_transition_clk) {
  1702. lvds.lvds.script |= 0x0100;
  1703. }
  1704. if (lvds.lvds.script & 0x0100) {
  1705. if (bios->fp.strapless_is_24bit & 2)
  1706. lvds.lvds.script |= 0x0200;
  1707. } else {
  1708. if (bios->fp.strapless_is_24bit & 1)
  1709. lvds.lvds.script |= 0x0200;
  1710. }
  1711. if (nv_connector->base.display_info.bpc == 8)
  1712. lvds.lvds.script |= 0x0200;
  1713. }
  1714. nvif_mthd(disp->disp, 0, &lvds, sizeof(lvds));
  1715. break;
  1716. case DCB_OUTPUT_DP:
  1717. if (nv_connector->base.display_info.bpc == 6) {
  1718. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  1719. depth = 0x2;
  1720. } else
  1721. if (nv_connector->base.display_info.bpc == 8) {
  1722. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  1723. depth = 0x5;
  1724. } else {
  1725. nv_encoder->dp.datarate = mode->clock * 30 / 8;
  1726. depth = 0x6;
  1727. }
  1728. if (nv_encoder->dcb->sorconf.link & 1)
  1729. proto = 0x8;
  1730. else
  1731. proto = 0x9;
  1732. nv50_audio_mode_set(encoder, mode);
  1733. break;
  1734. default:
  1735. BUG_ON(1);
  1736. break;
  1737. }
  1738. nv50_sor_dpms(&nv_encoder->base.base, DRM_MODE_DPMS_ON);
  1739. if (nv50_vers(mast) >= GF110_DISP) {
  1740. u32 *push = evo_wait(mast, 3);
  1741. if (push) {
  1742. u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
  1743. u32 syncs = 0x00000001;
  1744. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1745. syncs |= 0x00000008;
  1746. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1747. syncs |= 0x00000010;
  1748. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1749. magic |= 0x00000001;
  1750. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1751. evo_data(push, syncs | (depth << 6));
  1752. evo_data(push, magic);
  1753. evo_kick(push, mast);
  1754. }
  1755. ctrl = proto << 8;
  1756. mask = 0x00000f00;
  1757. } else {
  1758. ctrl = (depth << 16) | (proto << 8);
  1759. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1760. ctrl |= 0x00001000;
  1761. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1762. ctrl |= 0x00002000;
  1763. mask = 0x000f3f00;
  1764. }
  1765. nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
  1766. }
  1767. static void
  1768. nv50_sor_destroy(struct drm_encoder *encoder)
  1769. {
  1770. drm_encoder_cleanup(encoder);
  1771. kfree(encoder);
  1772. }
  1773. static const struct drm_encoder_helper_funcs nv50_sor_hfunc = {
  1774. .dpms = nv50_sor_dpms,
  1775. .mode_fixup = nv50_sor_mode_fixup,
  1776. .prepare = nv50_sor_disconnect,
  1777. .commit = nv50_sor_commit,
  1778. .mode_set = nv50_sor_mode_set,
  1779. .disable = nv50_sor_disconnect,
  1780. .get_crtc = nv50_display_crtc_get,
  1781. };
  1782. static const struct drm_encoder_funcs nv50_sor_func = {
  1783. .destroy = nv50_sor_destroy,
  1784. };
  1785. static int
  1786. nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1787. {
  1788. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  1789. struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
  1790. struct nouveau_encoder *nv_encoder;
  1791. struct drm_encoder *encoder;
  1792. int type;
  1793. switch (dcbe->type) {
  1794. case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
  1795. case DCB_OUTPUT_TMDS:
  1796. case DCB_OUTPUT_DP:
  1797. default:
  1798. type = DRM_MODE_ENCODER_TMDS;
  1799. break;
  1800. }
  1801. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1802. if (!nv_encoder)
  1803. return -ENOMEM;
  1804. nv_encoder->dcb = dcbe;
  1805. nv_encoder->or = ffs(dcbe->or) - 1;
  1806. nv_encoder->i2c = i2c->find(i2c, dcbe->i2c_index);
  1807. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1808. encoder = to_drm_encoder(nv_encoder);
  1809. encoder->possible_crtcs = dcbe->heads;
  1810. encoder->possible_clones = 0;
  1811. drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type);
  1812. drm_encoder_helper_add(encoder, &nv50_sor_hfunc);
  1813. drm_mode_connector_attach_encoder(connector, encoder);
  1814. return 0;
  1815. }
  1816. /******************************************************************************
  1817. * PIOR
  1818. *****************************************************************************/
  1819. static void
  1820. nv50_pior_dpms(struct drm_encoder *encoder, int mode)
  1821. {
  1822. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1823. struct nv50_disp *disp = nv50_disp(encoder->dev);
  1824. struct {
  1825. struct nv50_disp_mthd_v1 base;
  1826. struct nv50_disp_pior_pwr_v0 pwr;
  1827. } args = {
  1828. .base.version = 1,
  1829. .base.method = NV50_DISP_MTHD_V1_PIOR_PWR,
  1830. .base.hasht = nv_encoder->dcb->hasht,
  1831. .base.hashm = nv_encoder->dcb->hashm,
  1832. .pwr.state = mode == DRM_MODE_DPMS_ON,
  1833. .pwr.type = nv_encoder->dcb->type,
  1834. };
  1835. nvif_mthd(disp->disp, 0, &args, sizeof(args));
  1836. }
  1837. static bool
  1838. nv50_pior_mode_fixup(struct drm_encoder *encoder,
  1839. const struct drm_display_mode *mode,
  1840. struct drm_display_mode *adjusted_mode)
  1841. {
  1842. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1843. struct nouveau_connector *nv_connector;
  1844. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1845. if (nv_connector && nv_connector->native_mode) {
  1846. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1847. int id = adjusted_mode->base.id;
  1848. *adjusted_mode = *nv_connector->native_mode;
  1849. adjusted_mode->base.id = id;
  1850. }
  1851. }
  1852. adjusted_mode->clock *= 2;
  1853. return true;
  1854. }
  1855. static void
  1856. nv50_pior_commit(struct drm_encoder *encoder)
  1857. {
  1858. }
  1859. static void
  1860. nv50_pior_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  1861. struct drm_display_mode *adjusted_mode)
  1862. {
  1863. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1864. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1865. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1866. struct nouveau_connector *nv_connector;
  1867. u8 owner = 1 << nv_crtc->index;
  1868. u8 proto, depth;
  1869. u32 *push;
  1870. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1871. switch (nv_connector->base.display_info.bpc) {
  1872. case 10: depth = 0x6; break;
  1873. case 8: depth = 0x5; break;
  1874. case 6: depth = 0x2; break;
  1875. default: depth = 0x0; break;
  1876. }
  1877. switch (nv_encoder->dcb->type) {
  1878. case DCB_OUTPUT_TMDS:
  1879. case DCB_OUTPUT_DP:
  1880. proto = 0x0;
  1881. break;
  1882. default:
  1883. BUG_ON(1);
  1884. break;
  1885. }
  1886. nv50_pior_dpms(encoder, DRM_MODE_DPMS_ON);
  1887. push = evo_wait(mast, 8);
  1888. if (push) {
  1889. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  1890. u32 ctrl = (depth << 16) | (proto << 8) | owner;
  1891. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1892. ctrl |= 0x00001000;
  1893. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1894. ctrl |= 0x00002000;
  1895. evo_mthd(push, 0x0700 + (nv_encoder->or * 0x040), 1);
  1896. evo_data(push, ctrl);
  1897. }
  1898. evo_kick(push, mast);
  1899. }
  1900. nv_encoder->crtc = encoder->crtc;
  1901. }
  1902. static void
  1903. nv50_pior_disconnect(struct drm_encoder *encoder)
  1904. {
  1905. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1906. struct nv50_mast *mast = nv50_mast(encoder->dev);
  1907. const int or = nv_encoder->or;
  1908. u32 *push;
  1909. if (nv_encoder->crtc) {
  1910. nv50_crtc_prepare(nv_encoder->crtc);
  1911. push = evo_wait(mast, 4);
  1912. if (push) {
  1913. if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
  1914. evo_mthd(push, 0x0700 + (or * 0x040), 1);
  1915. evo_data(push, 0x00000000);
  1916. }
  1917. evo_kick(push, mast);
  1918. }
  1919. }
  1920. nv_encoder->crtc = NULL;
  1921. }
  1922. static void
  1923. nv50_pior_destroy(struct drm_encoder *encoder)
  1924. {
  1925. drm_encoder_cleanup(encoder);
  1926. kfree(encoder);
  1927. }
  1928. static const struct drm_encoder_helper_funcs nv50_pior_hfunc = {
  1929. .dpms = nv50_pior_dpms,
  1930. .mode_fixup = nv50_pior_mode_fixup,
  1931. .prepare = nv50_pior_disconnect,
  1932. .commit = nv50_pior_commit,
  1933. .mode_set = nv50_pior_mode_set,
  1934. .disable = nv50_pior_disconnect,
  1935. .get_crtc = nv50_display_crtc_get,
  1936. };
  1937. static const struct drm_encoder_funcs nv50_pior_func = {
  1938. .destroy = nv50_pior_destroy,
  1939. };
  1940. static int
  1941. nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1942. {
  1943. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  1944. struct nouveau_i2c *i2c = nvkm_i2c(&drm->device);
  1945. struct nouveau_i2c_port *ddc = NULL;
  1946. struct nouveau_encoder *nv_encoder;
  1947. struct drm_encoder *encoder;
  1948. int type;
  1949. switch (dcbe->type) {
  1950. case DCB_OUTPUT_TMDS:
  1951. ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(dcbe->extdev));
  1952. type = DRM_MODE_ENCODER_TMDS;
  1953. break;
  1954. case DCB_OUTPUT_DP:
  1955. ddc = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(dcbe->extdev));
  1956. type = DRM_MODE_ENCODER_TMDS;
  1957. break;
  1958. default:
  1959. return -ENODEV;
  1960. }
  1961. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1962. if (!nv_encoder)
  1963. return -ENOMEM;
  1964. nv_encoder->dcb = dcbe;
  1965. nv_encoder->or = ffs(dcbe->or) - 1;
  1966. nv_encoder->i2c = ddc;
  1967. encoder = to_drm_encoder(nv_encoder);
  1968. encoder->possible_crtcs = dcbe->heads;
  1969. encoder->possible_clones = 0;
  1970. drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type);
  1971. drm_encoder_helper_add(encoder, &nv50_pior_hfunc);
  1972. drm_mode_connector_attach_encoder(connector, encoder);
  1973. return 0;
  1974. }
  1975. /******************************************************************************
  1976. * Framebuffer
  1977. *****************************************************************************/
  1978. static void
  1979. nv50_fbdma_fini(struct nv50_fbdma *fbdma)
  1980. {
  1981. int i;
  1982. for (i = 0; i < ARRAY_SIZE(fbdma->base); i++)
  1983. nvif_object_fini(&fbdma->base[i]);
  1984. nvif_object_fini(&fbdma->core);
  1985. list_del(&fbdma->head);
  1986. kfree(fbdma);
  1987. }
  1988. static int
  1989. nv50_fbdma_init(struct drm_device *dev, u32 name, u64 offset, u64 length, u8 kind)
  1990. {
  1991. struct nouveau_drm *drm = nouveau_drm(dev);
  1992. struct nv50_disp *disp = nv50_disp(dev);
  1993. struct nv50_mast *mast = nv50_mast(dev);
  1994. struct __attribute__ ((packed)) {
  1995. struct nv_dma_v0 base;
  1996. union {
  1997. struct nv50_dma_v0 nv50;
  1998. struct gf100_dma_v0 gf100;
  1999. struct gf110_dma_v0 gf110;
  2000. };
  2001. } args = {};
  2002. struct nv50_fbdma *fbdma;
  2003. struct drm_crtc *crtc;
  2004. u32 size = sizeof(args.base);
  2005. int ret;
  2006. list_for_each_entry(fbdma, &disp->fbdma, head) {
  2007. if (fbdma->core.handle == name)
  2008. return 0;
  2009. }
  2010. fbdma = kzalloc(sizeof(*fbdma), GFP_KERNEL);
  2011. if (!fbdma)
  2012. return -ENOMEM;
  2013. list_add(&fbdma->head, &disp->fbdma);
  2014. args.base.target = NV_DMA_V0_TARGET_VRAM;
  2015. args.base.access = NV_DMA_V0_ACCESS_RDWR;
  2016. args.base.start = offset;
  2017. args.base.limit = offset + length - 1;
  2018. if (drm->device.info.chipset < 0x80) {
  2019. args.nv50.part = NV50_DMA_V0_PART_256;
  2020. size += sizeof(args.nv50);
  2021. } else
  2022. if (drm->device.info.chipset < 0xc0) {
  2023. args.nv50.part = NV50_DMA_V0_PART_256;
  2024. args.nv50.kind = kind;
  2025. size += sizeof(args.nv50);
  2026. } else
  2027. if (drm->device.info.chipset < 0xd0) {
  2028. args.gf100.kind = kind;
  2029. size += sizeof(args.gf100);
  2030. } else {
  2031. args.gf110.page = GF110_DMA_V0_PAGE_LP;
  2032. args.gf110.kind = kind;
  2033. size += sizeof(args.gf110);
  2034. }
  2035. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2036. struct nv50_head *head = nv50_head(crtc);
  2037. int ret = nvif_object_init(&head->sync.base.base.user, NULL,
  2038. name, NV_DMA_IN_MEMORY, &args, size,
  2039. &fbdma->base[head->base.index]);
  2040. if (ret) {
  2041. nv50_fbdma_fini(fbdma);
  2042. return ret;
  2043. }
  2044. }
  2045. ret = nvif_object_init(&mast->base.base.user, NULL, name,
  2046. NV_DMA_IN_MEMORY, &args, size,
  2047. &fbdma->core);
  2048. if (ret) {
  2049. nv50_fbdma_fini(fbdma);
  2050. return ret;
  2051. }
  2052. return 0;
  2053. }
  2054. static void
  2055. nv50_fb_dtor(struct drm_framebuffer *fb)
  2056. {
  2057. }
  2058. static int
  2059. nv50_fb_ctor(struct drm_framebuffer *fb)
  2060. {
  2061. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  2062. struct nouveau_drm *drm = nouveau_drm(fb->dev);
  2063. struct nouveau_bo *nvbo = nv_fb->nvbo;
  2064. struct nv50_disp *disp = nv50_disp(fb->dev);
  2065. u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
  2066. u8 tile = nvbo->tile_mode;
  2067. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) {
  2068. NV_ERROR(drm, "framebuffer requires contiguous bo\n");
  2069. return -EINVAL;
  2070. }
  2071. if (drm->device.info.chipset >= 0xc0)
  2072. tile >>= 4; /* yep.. */
  2073. switch (fb->depth) {
  2074. case 8: nv_fb->r_format = 0x1e00; break;
  2075. case 15: nv_fb->r_format = 0xe900; break;
  2076. case 16: nv_fb->r_format = 0xe800; break;
  2077. case 24:
  2078. case 32: nv_fb->r_format = 0xcf00; break;
  2079. case 30: nv_fb->r_format = 0xd100; break;
  2080. default:
  2081. NV_ERROR(drm, "unknown depth %d\n", fb->depth);
  2082. return -EINVAL;
  2083. }
  2084. if (disp->disp->oclass < G82_DISP) {
  2085. nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
  2086. (fb->pitches[0] | 0x00100000);
  2087. nv_fb->r_format |= kind << 16;
  2088. } else
  2089. if (disp->disp->oclass < GF110_DISP) {
  2090. nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
  2091. (fb->pitches[0] | 0x00100000);
  2092. } else {
  2093. nv_fb->r_pitch = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
  2094. (fb->pitches[0] | 0x01000000);
  2095. }
  2096. nv_fb->r_handle = 0xffff0000 | kind;
  2097. return nv50_fbdma_init(fb->dev, nv_fb->r_handle, 0,
  2098. drm->device.info.ram_user, kind);
  2099. }
  2100. /******************************************************************************
  2101. * Init
  2102. *****************************************************************************/
  2103. void
  2104. nv50_display_fini(struct drm_device *dev)
  2105. {
  2106. }
  2107. int
  2108. nv50_display_init(struct drm_device *dev)
  2109. {
  2110. struct nv50_disp *disp = nv50_disp(dev);
  2111. struct drm_crtc *crtc;
  2112. u32 *push;
  2113. push = evo_wait(nv50_mast(dev), 32);
  2114. if (!push)
  2115. return -EBUSY;
  2116. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2117. struct nv50_sync *sync = nv50_sync(crtc);
  2118. nouveau_bo_wr32(disp->sync, sync->addr / 4, sync->data);
  2119. }
  2120. evo_mthd(push, 0x0088, 1);
  2121. evo_data(push, nv50_mast(dev)->base.sync.handle);
  2122. evo_kick(push, nv50_mast(dev));
  2123. return 0;
  2124. }
  2125. void
  2126. nv50_display_destroy(struct drm_device *dev)
  2127. {
  2128. struct nv50_disp *disp = nv50_disp(dev);
  2129. struct nv50_fbdma *fbdma, *fbtmp;
  2130. list_for_each_entry_safe(fbdma, fbtmp, &disp->fbdma, head) {
  2131. nv50_fbdma_fini(fbdma);
  2132. }
  2133. nv50_dmac_destroy(&disp->mast.base, disp->disp);
  2134. nouveau_bo_unmap(disp->sync);
  2135. if (disp->sync)
  2136. nouveau_bo_unpin(disp->sync);
  2137. nouveau_bo_ref(NULL, &disp->sync);
  2138. nouveau_display(dev)->priv = NULL;
  2139. kfree(disp);
  2140. }
  2141. int
  2142. nv50_display_create(struct drm_device *dev)
  2143. {
  2144. struct nvif_device *device = &nouveau_drm(dev)->device;
  2145. struct nouveau_drm *drm = nouveau_drm(dev);
  2146. struct dcb_table *dcb = &drm->vbios.dcb;
  2147. struct drm_connector *connector, *tmp;
  2148. struct nv50_disp *disp;
  2149. struct dcb_output *dcbe;
  2150. int crtcs, ret, i;
  2151. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  2152. if (!disp)
  2153. return -ENOMEM;
  2154. INIT_LIST_HEAD(&disp->fbdma);
  2155. nouveau_display(dev)->priv = disp;
  2156. nouveau_display(dev)->dtor = nv50_display_destroy;
  2157. nouveau_display(dev)->init = nv50_display_init;
  2158. nouveau_display(dev)->fini = nv50_display_fini;
  2159. nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
  2160. nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
  2161. disp->disp = &nouveau_display(dev)->disp;
  2162. /* small shared memory area we use for notifiers and semaphores */
  2163. ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
  2164. 0, 0x0000, NULL, NULL, &disp->sync);
  2165. if (!ret) {
  2166. ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
  2167. if (!ret) {
  2168. ret = nouveau_bo_map(disp->sync);
  2169. if (ret)
  2170. nouveau_bo_unpin(disp->sync);
  2171. }
  2172. if (ret)
  2173. nouveau_bo_ref(NULL, &disp->sync);
  2174. }
  2175. if (ret)
  2176. goto out;
  2177. /* allocate master evo channel */
  2178. ret = nv50_core_create(disp->disp, disp->sync->bo.offset,
  2179. &disp->mast);
  2180. if (ret)
  2181. goto out;
  2182. /* create crtc objects to represent the hw heads */
  2183. if (disp->disp->oclass >= GF110_DISP)
  2184. crtcs = nvif_rd32(device, 0x022448);
  2185. else
  2186. crtcs = 2;
  2187. for (i = 0; i < crtcs; i++) {
  2188. ret = nv50_crtc_create(dev, i);
  2189. if (ret)
  2190. goto out;
  2191. }
  2192. /* create encoder/connector objects based on VBIOS DCB table */
  2193. for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
  2194. connector = nouveau_connector_create(dev, dcbe->connector);
  2195. if (IS_ERR(connector))
  2196. continue;
  2197. if (dcbe->location == DCB_LOC_ON_CHIP) {
  2198. switch (dcbe->type) {
  2199. case DCB_OUTPUT_TMDS:
  2200. case DCB_OUTPUT_LVDS:
  2201. case DCB_OUTPUT_DP:
  2202. ret = nv50_sor_create(connector, dcbe);
  2203. break;
  2204. case DCB_OUTPUT_ANALOG:
  2205. ret = nv50_dac_create(connector, dcbe);
  2206. break;
  2207. default:
  2208. ret = -ENODEV;
  2209. break;
  2210. }
  2211. } else {
  2212. ret = nv50_pior_create(connector, dcbe);
  2213. }
  2214. if (ret) {
  2215. NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
  2216. dcbe->location, dcbe->type,
  2217. ffs(dcbe->or) - 1, ret);
  2218. ret = 0;
  2219. }
  2220. }
  2221. /* cull any connectors we created that don't have an encoder */
  2222. list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
  2223. if (connector->encoder_ids[0])
  2224. continue;
  2225. NV_WARN(drm, "%s has no encoders, removing\n",
  2226. connector->name);
  2227. connector->funcs->destroy(connector);
  2228. }
  2229. out:
  2230. if (ret)
  2231. nv50_display_destroy(dev);
  2232. return ret;
  2233. }