nv50_display.c 69 KB

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