nv50_display.c 63 KB

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