nv50_display.c 70 KB

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