nv50_display.c 56 KB

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