nv50_display.c 70 KB

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