nvd0_display.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  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 nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1106. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1107. u32 syncs, magic, *push;
  1108. syncs = 0x00000001;
  1109. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1110. syncs |= 0x00000008;
  1111. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1112. syncs |= 0x00000010;
  1113. magic = 0x31ec6000 | (nv_crtc->index << 25);
  1114. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1115. magic |= 0x00000001;
  1116. nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
  1117. push = evo_wait(nvd0_mast(encoder->dev), 8);
  1118. if (push) {
  1119. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1120. evo_data(push, syncs);
  1121. evo_data(push, magic);
  1122. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 1);
  1123. evo_data(push, 1 << nv_crtc->index);
  1124. evo_kick(push, nvd0_mast(encoder->dev));
  1125. }
  1126. nv_encoder->crtc = encoder->crtc;
  1127. }
  1128. static void
  1129. nvd0_dac_disconnect(struct drm_encoder *encoder)
  1130. {
  1131. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1132. struct drm_device *dev = encoder->dev;
  1133. u32 *push;
  1134. if (nv_encoder->crtc) {
  1135. nvd0_crtc_prepare(nv_encoder->crtc);
  1136. push = evo_wait(nvd0_mast(dev), 4);
  1137. if (push) {
  1138. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
  1139. evo_data(push, 0x00000000);
  1140. evo_mthd(push, 0x0080, 1);
  1141. evo_data(push, 0x00000000);
  1142. evo_kick(push, nvd0_mast(dev));
  1143. }
  1144. nv_encoder->crtc = NULL;
  1145. }
  1146. }
  1147. static enum drm_connector_status
  1148. nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  1149. {
  1150. struct nvd0_disp *disp = nvd0_disp(encoder->dev);
  1151. int ret, or = nouveau_encoder(encoder)->or;
  1152. u32 load = 0;
  1153. ret = nv_exec(disp->core, NV50_DISP_DAC_LOAD + or, &load, sizeof(load));
  1154. if (ret || load != 7)
  1155. return connector_status_disconnected;
  1156. return connector_status_connected;
  1157. }
  1158. static void
  1159. nvd0_dac_destroy(struct drm_encoder *encoder)
  1160. {
  1161. drm_encoder_cleanup(encoder);
  1162. kfree(encoder);
  1163. }
  1164. static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
  1165. .dpms = nvd0_dac_dpms,
  1166. .mode_fixup = nvd0_dac_mode_fixup,
  1167. .prepare = nvd0_dac_disconnect,
  1168. .commit = nvd0_dac_commit,
  1169. .mode_set = nvd0_dac_mode_set,
  1170. .disable = nvd0_dac_disconnect,
  1171. .get_crtc = nvd0_display_crtc_get,
  1172. .detect = nvd0_dac_detect
  1173. };
  1174. static const struct drm_encoder_funcs nvd0_dac_func = {
  1175. .destroy = nvd0_dac_destroy,
  1176. };
  1177. static int
  1178. nvd0_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1179. {
  1180. struct drm_device *dev = connector->dev;
  1181. struct nouveau_encoder *nv_encoder;
  1182. struct drm_encoder *encoder;
  1183. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1184. if (!nv_encoder)
  1185. return -ENOMEM;
  1186. nv_encoder->dcb = dcbe;
  1187. nv_encoder->or = ffs(dcbe->or) - 1;
  1188. encoder = to_drm_encoder(nv_encoder);
  1189. encoder->possible_crtcs = dcbe->heads;
  1190. encoder->possible_clones = 0;
  1191. drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
  1192. drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
  1193. drm_mode_connector_attach_encoder(connector, encoder);
  1194. return 0;
  1195. }
  1196. /******************************************************************************
  1197. * Audio
  1198. *****************************************************************************/
  1199. static void
  1200. nvd0_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1201. {
  1202. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1203. struct nouveau_connector *nv_connector;
  1204. struct nvd0_disp *disp = nvd0_disp(encoder->dev);
  1205. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1206. if (!drm_detect_monitor_audio(nv_connector->edid))
  1207. return;
  1208. drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
  1209. nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or,
  1210. nv_connector->base.eld,
  1211. nv_connector->base.eld[2] * 4);
  1212. }
  1213. static void
  1214. nvd0_audio_disconnect(struct drm_encoder *encoder)
  1215. {
  1216. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1217. struct nvd0_disp *disp = nvd0_disp(encoder->dev);
  1218. nv_exec(disp->core, NVA3_DISP_SOR_HDA_ELD + nv_encoder->or, NULL, 0);
  1219. }
  1220. /******************************************************************************
  1221. * HDMI
  1222. *****************************************************************************/
  1223. static void
  1224. nvd0_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  1225. {
  1226. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1227. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1228. struct nouveau_connector *nv_connector;
  1229. struct nvd0_disp *disp = nvd0_disp(encoder->dev);
  1230. const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
  1231. u32 rekey = 56; /* binary driver, and tegra constant */
  1232. u32 max_ac_packet;
  1233. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1234. if (!drm_detect_hdmi_monitor(nv_connector->edid))
  1235. return;
  1236. max_ac_packet = mode->htotal - mode->hdisplay;
  1237. max_ac_packet -= rekey;
  1238. max_ac_packet -= 18; /* constant from tegra */
  1239. max_ac_packet /= 32;
  1240. nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff,
  1241. NV84_DISP_SOR_HDMI_PWR_STATE_ON |
  1242. (max_ac_packet << 16) | rekey);
  1243. nvd0_audio_mode_set(encoder, mode);
  1244. }
  1245. static void
  1246. nvd0_hdmi_disconnect(struct drm_encoder *encoder)
  1247. {
  1248. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1249. struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
  1250. struct nvd0_disp *disp = nvd0_disp(encoder->dev);
  1251. const u32 moff = (nv_crtc->index << 3) | nv_encoder->or;
  1252. nvd0_audio_disconnect(encoder);
  1253. nv_call(disp->core, NV84_DISP_SOR_HDMI_PWR + moff, 0x00000000);
  1254. }
  1255. /******************************************************************************
  1256. * SOR
  1257. *****************************************************************************/
  1258. static void
  1259. nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
  1260. {
  1261. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1262. struct drm_device *dev = encoder->dev;
  1263. struct nvd0_disp *disp = nvd0_disp(dev);
  1264. struct drm_encoder *partner;
  1265. int or = nv_encoder->or;
  1266. nv_encoder->last_dpms = mode;
  1267. list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
  1268. struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
  1269. if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
  1270. continue;
  1271. if (nv_partner != nv_encoder &&
  1272. nv_partner->dcb->or == nv_encoder->dcb->or) {
  1273. if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
  1274. return;
  1275. break;
  1276. }
  1277. }
  1278. nv_call(disp->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
  1279. if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
  1280. nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, disp->core);
  1281. }
  1282. static bool
  1283. nvd0_sor_mode_fixup(struct drm_encoder *encoder,
  1284. const struct drm_display_mode *mode,
  1285. struct drm_display_mode *adjusted_mode)
  1286. {
  1287. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1288. struct nouveau_connector *nv_connector;
  1289. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1290. if (nv_connector && nv_connector->native_mode) {
  1291. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1292. int id = adjusted_mode->base.id;
  1293. *adjusted_mode = *nv_connector->native_mode;
  1294. adjusted_mode->base.id = id;
  1295. }
  1296. }
  1297. return true;
  1298. }
  1299. static void
  1300. nvd0_sor_disconnect(struct drm_encoder *encoder)
  1301. {
  1302. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1303. struct drm_device *dev = encoder->dev;
  1304. u32 *push;
  1305. if (nv_encoder->crtc) {
  1306. nvd0_crtc_prepare(nv_encoder->crtc);
  1307. push = evo_wait(nvd0_mast(dev), 4);
  1308. if (push) {
  1309. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
  1310. evo_data(push, 0x00000000);
  1311. evo_mthd(push, 0x0080, 1);
  1312. evo_data(push, 0x00000000);
  1313. evo_kick(push, nvd0_mast(dev));
  1314. }
  1315. nvd0_hdmi_disconnect(encoder);
  1316. nv_encoder->crtc = NULL;
  1317. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1318. }
  1319. }
  1320. static void
  1321. nvd0_sor_prepare(struct drm_encoder *encoder)
  1322. {
  1323. nvd0_sor_disconnect(encoder);
  1324. if (nouveau_encoder(encoder)->dcb->type == DCB_OUTPUT_DP)
  1325. evo_sync(encoder->dev);
  1326. }
  1327. static void
  1328. nvd0_sor_commit(struct drm_encoder *encoder)
  1329. {
  1330. }
  1331. static void
  1332. nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  1333. struct drm_display_mode *mode)
  1334. {
  1335. struct nvd0_disp *disp = nvd0_disp(encoder->dev);
  1336. struct drm_device *dev = encoder->dev;
  1337. struct nouveau_drm *drm = nouveau_drm(dev);
  1338. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1339. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1340. struct nouveau_connector *nv_connector;
  1341. struct nvbios *bios = &drm->vbios;
  1342. int or = nv_encoder->or;
  1343. u32 mode_ctrl = (1 << nv_crtc->index);
  1344. u32 syncs, magic, *push;
  1345. u32 or_config;
  1346. syncs = 0x00000001;
  1347. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1348. syncs |= 0x00000008;
  1349. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1350. syncs |= 0x00000010;
  1351. magic = 0x31ec6000 | (nv_crtc->index << 25);
  1352. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1353. magic |= 0x00000001;
  1354. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1355. switch (nv_encoder->dcb->type) {
  1356. case DCB_OUTPUT_TMDS:
  1357. if (nv_encoder->dcb->sorconf.link & 1) {
  1358. if (mode->clock < 165000)
  1359. mode_ctrl |= 0x00000100;
  1360. else
  1361. mode_ctrl |= 0x00000500;
  1362. } else {
  1363. mode_ctrl |= 0x00000200;
  1364. }
  1365. nvd0_hdmi_mode_set(encoder, mode);
  1366. break;
  1367. case DCB_OUTPUT_LVDS:
  1368. or_config = (mode_ctrl & 0x00000f00) >> 8;
  1369. if (bios->fp_no_ddc) {
  1370. if (bios->fp.dual_link)
  1371. or_config |= 0x0100;
  1372. if (bios->fp.if_is_24bit)
  1373. or_config |= 0x0200;
  1374. } else {
  1375. if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  1376. if (((u8 *)nv_connector->edid)[121] == 2)
  1377. or_config |= 0x0100;
  1378. } else
  1379. if (mode->clock >= bios->fp.duallink_transition_clk) {
  1380. or_config |= 0x0100;
  1381. }
  1382. if (or_config & 0x0100) {
  1383. if (bios->fp.strapless_is_24bit & 2)
  1384. or_config |= 0x0200;
  1385. } else {
  1386. if (bios->fp.strapless_is_24bit & 1)
  1387. or_config |= 0x0200;
  1388. }
  1389. if (nv_connector->base.display_info.bpc == 8)
  1390. or_config |= 0x0200;
  1391. }
  1392. nv_call(disp->core, NV50_DISP_SOR_LVDS_SCRIPT + or, or_config);
  1393. break;
  1394. case DCB_OUTPUT_DP:
  1395. if (nv_connector->base.display_info.bpc == 6) {
  1396. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  1397. syncs |= 0x00000002 << 6;
  1398. } else {
  1399. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  1400. syncs |= 0x00000005 << 6;
  1401. }
  1402. if (nv_encoder->dcb->sorconf.link & 1)
  1403. mode_ctrl |= 0x00000800;
  1404. else
  1405. mode_ctrl |= 0x00000900;
  1406. break;
  1407. default:
  1408. BUG_ON(1);
  1409. break;
  1410. }
  1411. nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  1412. push = evo_wait(nvd0_mast(dev), 8);
  1413. if (push) {
  1414. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1415. evo_data(push, syncs);
  1416. evo_data(push, magic);
  1417. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x020), 1);
  1418. evo_data(push, mode_ctrl);
  1419. evo_kick(push, nvd0_mast(dev));
  1420. }
  1421. nv_encoder->crtc = encoder->crtc;
  1422. }
  1423. static void
  1424. nvd0_sor_destroy(struct drm_encoder *encoder)
  1425. {
  1426. drm_encoder_cleanup(encoder);
  1427. kfree(encoder);
  1428. }
  1429. static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
  1430. .dpms = nvd0_sor_dpms,
  1431. .mode_fixup = nvd0_sor_mode_fixup,
  1432. .prepare = nvd0_sor_prepare,
  1433. .commit = nvd0_sor_commit,
  1434. .mode_set = nvd0_sor_mode_set,
  1435. .disable = nvd0_sor_disconnect,
  1436. .get_crtc = nvd0_display_crtc_get,
  1437. };
  1438. static const struct drm_encoder_funcs nvd0_sor_func = {
  1439. .destroy = nvd0_sor_destroy,
  1440. };
  1441. static int
  1442. nvd0_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
  1443. {
  1444. struct drm_device *dev = connector->dev;
  1445. struct nouveau_encoder *nv_encoder;
  1446. struct drm_encoder *encoder;
  1447. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1448. if (!nv_encoder)
  1449. return -ENOMEM;
  1450. nv_encoder->dcb = dcbe;
  1451. nv_encoder->or = ffs(dcbe->or) - 1;
  1452. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1453. encoder = to_drm_encoder(nv_encoder);
  1454. encoder->possible_crtcs = dcbe->heads;
  1455. encoder->possible_clones = 0;
  1456. drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
  1457. drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
  1458. drm_mode_connector_attach_encoder(connector, encoder);
  1459. return 0;
  1460. }
  1461. /******************************************************************************
  1462. * Init
  1463. *****************************************************************************/
  1464. void
  1465. nvd0_display_fini(struct drm_device *dev)
  1466. {
  1467. }
  1468. int
  1469. nvd0_display_init(struct drm_device *dev)
  1470. {
  1471. u32 *push = evo_wait(nvd0_mast(dev), 32);
  1472. if (push) {
  1473. evo_mthd(push, 0x0088, 1);
  1474. evo_data(push, NvEvoSync);
  1475. evo_mthd(push, 0x0084, 1);
  1476. evo_data(push, 0x00000000);
  1477. evo_mthd(push, 0x0084, 1);
  1478. evo_data(push, 0x80000000);
  1479. evo_mthd(push, 0x008c, 1);
  1480. evo_data(push, 0x00000000);
  1481. evo_kick(push, nvd0_mast(dev));
  1482. return 0;
  1483. }
  1484. return -EBUSY;
  1485. }
  1486. void
  1487. nvd0_display_destroy(struct drm_device *dev)
  1488. {
  1489. struct nvd0_disp *disp = nvd0_disp(dev);
  1490. nvd0_dmac_destroy(disp->core, &disp->mast.base);
  1491. nouveau_bo_unmap(disp->sync);
  1492. nouveau_bo_ref(NULL, &disp->sync);
  1493. nouveau_display(dev)->priv = NULL;
  1494. kfree(disp);
  1495. }
  1496. int
  1497. nvd0_display_create(struct drm_device *dev)
  1498. {
  1499. static const u16 oclass[] = {
  1500. NVE0_DISP_CLASS,
  1501. NVD0_DISP_CLASS,
  1502. };
  1503. struct nouveau_device *device = nouveau_dev(dev);
  1504. struct nouveau_drm *drm = nouveau_drm(dev);
  1505. struct dcb_table *dcb = &drm->vbios.dcb;
  1506. struct drm_connector *connector, *tmp;
  1507. struct nvd0_disp *disp;
  1508. struct dcb_output *dcbe;
  1509. int crtcs, ret, i;
  1510. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  1511. if (!disp)
  1512. return -ENOMEM;
  1513. nouveau_display(dev)->priv = disp;
  1514. nouveau_display(dev)->dtor = nvd0_display_destroy;
  1515. nouveau_display(dev)->init = nvd0_display_init;
  1516. nouveau_display(dev)->fini = nvd0_display_fini;
  1517. /* small shared memory area we use for notifiers and semaphores */
  1518. ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
  1519. 0, 0x0000, NULL, &disp->sync);
  1520. if (!ret) {
  1521. ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
  1522. if (!ret)
  1523. ret = nouveau_bo_map(disp->sync);
  1524. if (ret)
  1525. nouveau_bo_ref(NULL, &disp->sync);
  1526. }
  1527. if (ret)
  1528. goto out;
  1529. /* attempt to allocate a supported evo display class */
  1530. ret = -ENODEV;
  1531. for (i = 0; ret && i < ARRAY_SIZE(oclass); i++) {
  1532. ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE,
  1533. 0xd1500000, oclass[i], NULL, 0,
  1534. &disp->core);
  1535. }
  1536. if (ret)
  1537. goto out;
  1538. /* allocate master evo channel */
  1539. ret = nvd0_dmac_create(disp->core, NV50_DISP_MAST_CLASS, 0,
  1540. &(struct nv50_display_mast_class) {
  1541. .pushbuf = EVO_PUSH_HANDLE(MAST, 0),
  1542. }, sizeof(struct nv50_display_mast_class),
  1543. disp->sync->bo.offset, &disp->mast.base);
  1544. if (ret)
  1545. goto out;
  1546. /* create crtc objects to represent the hw heads */
  1547. crtcs = nv_rd32(device, 0x022448);
  1548. for (i = 0; i < crtcs; i++) {
  1549. ret = nvd0_crtc_create(dev, disp->core, i);
  1550. if (ret)
  1551. goto out;
  1552. }
  1553. /* create encoder/connector objects based on VBIOS DCB table */
  1554. for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
  1555. connector = nouveau_connector_create(dev, dcbe->connector);
  1556. if (IS_ERR(connector))
  1557. continue;
  1558. if (dcbe->location != DCB_LOC_ON_CHIP) {
  1559. NV_WARN(drm, "skipping off-chip encoder %d/%d\n",
  1560. dcbe->type, ffs(dcbe->or) - 1);
  1561. continue;
  1562. }
  1563. switch (dcbe->type) {
  1564. case DCB_OUTPUT_TMDS:
  1565. case DCB_OUTPUT_LVDS:
  1566. case DCB_OUTPUT_DP:
  1567. nvd0_sor_create(connector, dcbe);
  1568. break;
  1569. case DCB_OUTPUT_ANALOG:
  1570. nvd0_dac_create(connector, dcbe);
  1571. break;
  1572. default:
  1573. NV_WARN(drm, "skipping unsupported encoder %d/%d\n",
  1574. dcbe->type, ffs(dcbe->or) - 1);
  1575. continue;
  1576. }
  1577. }
  1578. /* cull any connectors we created that don't have an encoder */
  1579. list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
  1580. if (connector->encoder_ids[0])
  1581. continue;
  1582. NV_WARN(drm, "%s has no encoders, removing\n",
  1583. drm_get_connector_name(connector));
  1584. connector->funcs->destroy(connector);
  1585. }
  1586. out:
  1587. if (ret)
  1588. nvd0_display_destroy(dev);
  1589. return ret;
  1590. }