nvd0_display.c 50 KB

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