nvd0_display.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  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 "drmP.h"
  26. #include "drm_crtc_helper.h"
  27. #include "nouveau_drv.h"
  28. #include "nouveau_connector.h"
  29. #include "nouveau_encoder.h"
  30. #include "nouveau_crtc.h"
  31. #include "nouveau_dma.h"
  32. #include "nouveau_fb.h"
  33. #include "nv50_display.h"
  34. #define EVO_DMA_NR 9
  35. #define EVO_MASTER (0x00)
  36. #define EVO_FLIP(c) (0x01 + (c))
  37. #define EVO_OVLY(c) (0x05 + (c))
  38. #define EVO_OIMM(c) (0x09 + (c))
  39. #define EVO_CURS(c) (0x0d + (c))
  40. /* offsets in shared sync bo of various structures */
  41. #define EVO_SYNC(c, o) ((c) * 0x0100 + (o))
  42. #define EVO_MAST_NTFY EVO_SYNC( 0, 0x00)
  43. #define EVO_FLIP_SEM0(c) EVO_SYNC((c), 0x00)
  44. #define EVO_FLIP_SEM1(c) EVO_SYNC((c), 0x10)
  45. struct evo {
  46. int idx;
  47. dma_addr_t handle;
  48. u32 *ptr;
  49. struct {
  50. u32 offset;
  51. u16 value;
  52. } sem;
  53. };
  54. struct nvd0_display {
  55. struct nouveau_gpuobj *mem;
  56. struct nouveau_bo *sync;
  57. struct evo evo[9];
  58. struct tasklet_struct tasklet;
  59. u32 modeset;
  60. };
  61. static struct nvd0_display *
  62. nvd0_display(struct drm_device *dev)
  63. {
  64. struct drm_nouveau_private *dev_priv = dev->dev_private;
  65. return dev_priv->engine.display.priv;
  66. }
  67. static struct drm_crtc *
  68. nvd0_display_crtc_get(struct drm_encoder *encoder)
  69. {
  70. return nouveau_encoder(encoder)->crtc;
  71. }
  72. /******************************************************************************
  73. * EVO channel helpers
  74. *****************************************************************************/
  75. static inline int
  76. evo_icmd(struct drm_device *dev, int id, u32 mthd, u32 data)
  77. {
  78. int ret = 0;
  79. nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000001);
  80. nv_wr32(dev, 0x610704 + (id * 0x10), data);
  81. nv_mask(dev, 0x610704 + (id * 0x10), 0x80000ffc, 0x80000000 | mthd);
  82. if (!nv_wait(dev, 0x610704 + (id * 0x10), 0x80000000, 0x00000000))
  83. ret = -EBUSY;
  84. nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000000);
  85. return ret;
  86. }
  87. static u32 *
  88. evo_wait(struct drm_device *dev, int id, int nr)
  89. {
  90. struct nvd0_display *disp = nvd0_display(dev);
  91. u32 put = nv_rd32(dev, 0x640000 + (id * 0x1000)) / 4;
  92. if (put + nr >= (PAGE_SIZE / 4)) {
  93. disp->evo[id].ptr[put] = 0x20000000;
  94. nv_wr32(dev, 0x640000 + (id * 0x1000), 0x00000000);
  95. if (!nv_wait(dev, 0x640004 + (id * 0x1000), ~0, 0x00000000)) {
  96. NV_ERROR(dev, "evo %d dma stalled\n", id);
  97. return NULL;
  98. }
  99. put = 0;
  100. }
  101. if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
  102. NV_INFO(dev, "Evo%d: %p START\n", id, disp->evo[id].ptr + put);
  103. return disp->evo[id].ptr + put;
  104. }
  105. static void
  106. evo_kick(u32 *push, struct drm_device *dev, int id)
  107. {
  108. struct nvd0_display *disp = nvd0_display(dev);
  109. if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO) {
  110. u32 curp = nv_rd32(dev, 0x640000 + (id * 0x1000)) >> 2;
  111. u32 *cur = disp->evo[id].ptr + curp;
  112. while (cur < push)
  113. NV_INFO(dev, "Evo%d: 0x%08x\n", id, *cur++);
  114. NV_INFO(dev, "Evo%d: %p KICK!\n", id, push);
  115. }
  116. nv_wr32(dev, 0x640000 + (id * 0x1000), (push - disp->evo[id].ptr) << 2);
  117. }
  118. #define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
  119. #define evo_data(p,d) *((p)++) = (d)
  120. static int
  121. evo_init_dma(struct drm_device *dev, int ch)
  122. {
  123. struct nvd0_display *disp = nvd0_display(dev);
  124. u32 flags;
  125. flags = 0x00000000;
  126. if (ch == EVO_MASTER)
  127. flags |= 0x01000000;
  128. nv_wr32(dev, 0x610494 + (ch * 0x0010), (disp->evo[ch].handle >> 8) | 3);
  129. nv_wr32(dev, 0x610498 + (ch * 0x0010), 0x00010000);
  130. nv_wr32(dev, 0x61049c + (ch * 0x0010), 0x00000001);
  131. nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000010, 0x00000010);
  132. nv_wr32(dev, 0x640000 + (ch * 0x1000), 0x00000000);
  133. nv_wr32(dev, 0x610490 + (ch * 0x0010), 0x00000013 | flags);
  134. if (!nv_wait(dev, 0x610490 + (ch * 0x0010), 0x80000000, 0x00000000)) {
  135. NV_ERROR(dev, "PDISP: ch%d 0x%08x\n", ch,
  136. nv_rd32(dev, 0x610490 + (ch * 0x0010)));
  137. return -EBUSY;
  138. }
  139. nv_mask(dev, 0x610090, (1 << ch), (1 << ch));
  140. nv_mask(dev, 0x6100a0, (1 << ch), (1 << ch));
  141. return 0;
  142. }
  143. static void
  144. evo_fini_dma(struct drm_device *dev, int ch)
  145. {
  146. if (!(nv_rd32(dev, 0x610490 + (ch * 0x0010)) & 0x00000010))
  147. return;
  148. nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000010, 0x00000000);
  149. nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000003, 0x00000000);
  150. nv_wait(dev, 0x610490 + (ch * 0x0010), 0x80000000, 0x00000000);
  151. nv_mask(dev, 0x610090, (1 << ch), 0x00000000);
  152. nv_mask(dev, 0x6100a0, (1 << ch), 0x00000000);
  153. }
  154. static inline void
  155. evo_piow(struct drm_device *dev, int ch, u16 mthd, u32 data)
  156. {
  157. nv_wr32(dev, 0x640000 + (ch * 0x1000) + mthd, data);
  158. }
  159. static int
  160. evo_init_pio(struct drm_device *dev, int ch)
  161. {
  162. nv_wr32(dev, 0x610490 + (ch * 0x0010), 0x00000001);
  163. if (!nv_wait(dev, 0x610490 + (ch * 0x0010), 0x00010000, 0x00010000)) {
  164. NV_ERROR(dev, "PDISP: ch%d 0x%08x\n", ch,
  165. nv_rd32(dev, 0x610490 + (ch * 0x0010)));
  166. return -EBUSY;
  167. }
  168. nv_mask(dev, 0x610090, (1 << ch), (1 << ch));
  169. nv_mask(dev, 0x6100a0, (1 << ch), (1 << ch));
  170. return 0;
  171. }
  172. static void
  173. evo_fini_pio(struct drm_device *dev, int ch)
  174. {
  175. if (!(nv_rd32(dev, 0x610490 + (ch * 0x0010)) & 0x00000001))
  176. return;
  177. nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000010, 0x00000010);
  178. nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000001, 0x00000000);
  179. nv_wait(dev, 0x610490 + (ch * 0x0010), 0x00010000, 0x00000000);
  180. nv_mask(dev, 0x610090, (1 << ch), 0x00000000);
  181. nv_mask(dev, 0x6100a0, (1 << ch), 0x00000000);
  182. }
  183. static bool
  184. evo_sync_wait(void *data)
  185. {
  186. return nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000;
  187. }
  188. static int
  189. evo_sync(struct drm_device *dev, int ch)
  190. {
  191. struct nvd0_display *disp = nvd0_display(dev);
  192. u32 *push = evo_wait(dev, ch, 8);
  193. if (push) {
  194. nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
  195. evo_mthd(push, 0x0084, 1);
  196. evo_data(push, 0x80000000 | EVO_MAST_NTFY);
  197. evo_mthd(push, 0x0080, 2);
  198. evo_data(push, 0x00000000);
  199. evo_data(push, 0x00000000);
  200. evo_kick(push, dev, ch);
  201. if (nv_wait_cb(dev, evo_sync_wait, disp->sync))
  202. return 0;
  203. }
  204. return -EBUSY;
  205. }
  206. /******************************************************************************
  207. * Page flipping channel
  208. *****************************************************************************/
  209. struct nouveau_bo *
  210. nvd0_display_crtc_sema(struct drm_device *dev, int crtc)
  211. {
  212. return nvd0_display(dev)->sync;
  213. }
  214. void
  215. nvd0_display_flip_stop(struct drm_crtc *crtc)
  216. {
  217. struct nvd0_display *disp = nvd0_display(crtc->dev);
  218. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  219. struct evo *evo = &disp->evo[EVO_FLIP(nv_crtc->index)];
  220. u32 *push;
  221. push = evo_wait(crtc->dev, evo->idx, 8);
  222. if (push) {
  223. evo_mthd(push, 0x0084, 1);
  224. evo_data(push, 0x00000000);
  225. evo_mthd(push, 0x0094, 1);
  226. evo_data(push, 0x00000000);
  227. evo_mthd(push, 0x00c0, 1);
  228. evo_data(push, 0x00000000);
  229. evo_mthd(push, 0x0080, 1);
  230. evo_data(push, 0x00000000);
  231. evo_kick(push, crtc->dev, evo->idx);
  232. }
  233. }
  234. int
  235. nvd0_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  236. struct nouveau_channel *chan, u32 swap_interval)
  237. {
  238. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  239. struct nvd0_display *disp = nvd0_display(crtc->dev);
  240. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  241. struct evo *evo = &disp->evo[EVO_FLIP(nv_crtc->index)];
  242. u64 offset;
  243. u32 *push;
  244. int ret;
  245. evo_sync(crtc->dev, EVO_MASTER);
  246. swap_interval <<= 4;
  247. if (swap_interval == 0)
  248. swap_interval |= 0x100;
  249. push = evo_wait(crtc->dev, evo->idx, 128);
  250. if (unlikely(push == NULL))
  251. return -EBUSY;
  252. /* synchronise with the rendering channel, if necessary */
  253. if (likely(chan)) {
  254. ret = RING_SPACE(chan, 10);
  255. if (ret)
  256. return ret;
  257. offset = chan->dispc_vma[nv_crtc->index].offset;
  258. offset += evo->sem.offset;
  259. BEGIN_NVC0(chan, 2, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  260. OUT_RING (chan, upper_32_bits(offset));
  261. OUT_RING (chan, lower_32_bits(offset));
  262. OUT_RING (chan, 0xf00d0000 | evo->sem.value);
  263. OUT_RING (chan, 0x1002);
  264. BEGIN_NVC0(chan, 2, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  265. OUT_RING (chan, upper_32_bits(offset));
  266. OUT_RING (chan, lower_32_bits(offset ^ 0x10));
  267. OUT_RING (chan, 0x74b1e000);
  268. OUT_RING (chan, 0x1001);
  269. FIRE_RING (chan);
  270. } else {
  271. nouveau_bo_wr32(disp->sync, evo->sem.offset / 4,
  272. 0xf00d0000 | evo->sem.value);
  273. evo_sync(crtc->dev, EVO_MASTER);
  274. }
  275. /* queue the flip */
  276. evo_mthd(push, 0x0100, 1);
  277. evo_data(push, 0xfffe0000);
  278. evo_mthd(push, 0x0084, 1);
  279. evo_data(push, swap_interval);
  280. if (!(swap_interval & 0x00000100)) {
  281. evo_mthd(push, 0x00e0, 1);
  282. evo_data(push, 0x40000000);
  283. }
  284. evo_mthd(push, 0x0088, 4);
  285. evo_data(push, evo->sem.offset);
  286. evo_data(push, 0xf00d0000 | evo->sem.value);
  287. evo_data(push, 0x74b1e000);
  288. evo_data(push, NvEvoSync);
  289. evo_mthd(push, 0x00a0, 2);
  290. evo_data(push, 0x00000000);
  291. evo_data(push, 0x00000000);
  292. evo_mthd(push, 0x00c0, 1);
  293. evo_data(push, nv_fb->r_dma);
  294. evo_mthd(push, 0x0110, 2);
  295. evo_data(push, 0x00000000);
  296. evo_data(push, 0x00000000);
  297. evo_mthd(push, 0x0400, 5);
  298. evo_data(push, nv_fb->nvbo->bo.offset >> 8);
  299. evo_data(push, 0);
  300. evo_data(push, (fb->height << 16) | fb->width);
  301. evo_data(push, nv_fb->r_pitch);
  302. evo_data(push, nv_fb->r_format);
  303. evo_mthd(push, 0x0080, 1);
  304. evo_data(push, 0x00000000);
  305. evo_kick(push, crtc->dev, evo->idx);
  306. evo->sem.offset ^= 0x10;
  307. evo->sem.value++;
  308. return 0;
  309. }
  310. /******************************************************************************
  311. * CRTC
  312. *****************************************************************************/
  313. static int
  314. nvd0_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
  315. {
  316. struct drm_nouveau_private *dev_priv = nv_crtc->base.dev->dev_private;
  317. struct drm_device *dev = nv_crtc->base.dev;
  318. struct nouveau_connector *nv_connector;
  319. struct drm_connector *connector;
  320. u32 *push, mode = 0x00;
  321. u32 mthd;
  322. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  323. connector = &nv_connector->base;
  324. if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
  325. if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
  326. mode = DITHERING_MODE_DYNAMIC2X2;
  327. } else {
  328. mode = nv_connector->dithering_mode;
  329. }
  330. if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
  331. if (connector->display_info.bpc >= 8)
  332. mode |= DITHERING_DEPTH_8BPC;
  333. } else {
  334. mode |= nv_connector->dithering_depth;
  335. }
  336. if (dev_priv->card_type < NV_E0)
  337. mthd = 0x0490 + (nv_crtc->index * 0x0300);
  338. else
  339. mthd = 0x04a0 + (nv_crtc->index * 0x0300);
  340. push = evo_wait(dev, EVO_MASTER, 4);
  341. if (push) {
  342. evo_mthd(push, mthd, 1);
  343. evo_data(push, mode);
  344. if (update) {
  345. evo_mthd(push, 0x0080, 1);
  346. evo_data(push, 0x00000000);
  347. }
  348. evo_kick(push, dev, EVO_MASTER);
  349. }
  350. return 0;
  351. }
  352. static int
  353. nvd0_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
  354. {
  355. struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
  356. struct drm_device *dev = nv_crtc->base.dev;
  357. struct drm_crtc *crtc = &nv_crtc->base;
  358. struct nouveau_connector *nv_connector;
  359. int mode = DRM_MODE_SCALE_NONE;
  360. u32 oX, oY, *push;
  361. /* start off at the resolution we programmed the crtc for, this
  362. * effectively handles NONE/FULL scaling
  363. */
  364. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  365. if (nv_connector && nv_connector->native_mode)
  366. mode = nv_connector->scaling_mode;
  367. if (mode != DRM_MODE_SCALE_NONE)
  368. omode = nv_connector->native_mode;
  369. else
  370. omode = umode;
  371. oX = omode->hdisplay;
  372. oY = omode->vdisplay;
  373. if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
  374. oY *= 2;
  375. /* add overscan compensation if necessary, will keep the aspect
  376. * ratio the same as the backend mode unless overridden by the
  377. * user setting both hborder and vborder properties.
  378. */
  379. if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
  380. (nv_connector->underscan == UNDERSCAN_AUTO &&
  381. nv_connector->edid &&
  382. drm_detect_hdmi_monitor(nv_connector->edid)))) {
  383. u32 bX = nv_connector->underscan_hborder;
  384. u32 bY = nv_connector->underscan_vborder;
  385. u32 aspect = (oY << 19) / oX;
  386. if (bX) {
  387. oX -= (bX * 2);
  388. if (bY) oY -= (bY * 2);
  389. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  390. } else {
  391. oX -= (oX >> 4) + 32;
  392. if (bY) oY -= (bY * 2);
  393. else oY = ((oX * aspect) + (aspect / 2)) >> 19;
  394. }
  395. }
  396. /* handle CENTER/ASPECT scaling, taking into account the areas
  397. * removed already for overscan compensation
  398. */
  399. switch (mode) {
  400. case DRM_MODE_SCALE_CENTER:
  401. oX = min((u32)umode->hdisplay, oX);
  402. oY = min((u32)umode->vdisplay, oY);
  403. /* fall-through */
  404. case DRM_MODE_SCALE_ASPECT:
  405. if (oY < oX) {
  406. u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
  407. oX = ((oY * aspect) + (aspect / 2)) >> 19;
  408. } else {
  409. u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
  410. oY = ((oX * aspect) + (aspect / 2)) >> 19;
  411. }
  412. break;
  413. default:
  414. break;
  415. }
  416. push = evo_wait(dev, EVO_MASTER, 8);
  417. if (push) {
  418. evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
  419. evo_data(push, (oY << 16) | oX);
  420. evo_data(push, (oY << 16) | oX);
  421. evo_data(push, (oY << 16) | oX);
  422. evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
  423. evo_data(push, 0x00000000);
  424. evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
  425. evo_data(push, (umode->vdisplay << 16) | umode->hdisplay);
  426. evo_kick(push, dev, EVO_MASTER);
  427. if (update) {
  428. nvd0_display_flip_stop(crtc);
  429. nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
  430. }
  431. }
  432. return 0;
  433. }
  434. static int
  435. nvd0_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
  436. int x, int y, bool update)
  437. {
  438. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
  439. u32 *push;
  440. push = evo_wait(fb->dev, EVO_MASTER, 16);
  441. if (push) {
  442. evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
  443. evo_data(push, nvfb->nvbo->bo.offset >> 8);
  444. evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
  445. evo_data(push, (fb->height << 16) | fb->width);
  446. evo_data(push, nvfb->r_pitch);
  447. evo_data(push, nvfb->r_format);
  448. evo_data(push, nvfb->r_dma);
  449. evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
  450. evo_data(push, (y << 16) | x);
  451. if (update) {
  452. evo_mthd(push, 0x0080, 1);
  453. evo_data(push, 0x00000000);
  454. }
  455. evo_kick(push, fb->dev, EVO_MASTER);
  456. }
  457. nv_crtc->fb.tile_flags = nvfb->r_dma;
  458. return 0;
  459. }
  460. static void
  461. nvd0_crtc_cursor_show(struct nouveau_crtc *nv_crtc, bool show, bool update)
  462. {
  463. struct drm_device *dev = nv_crtc->base.dev;
  464. u32 *push = evo_wait(dev, EVO_MASTER, 16);
  465. if (push) {
  466. if (show) {
  467. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
  468. evo_data(push, 0x85000000);
  469. evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
  470. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  471. evo_data(push, NvEvoVRAM);
  472. } else {
  473. evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
  474. evo_data(push, 0x05000000);
  475. evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
  476. evo_data(push, 0x00000000);
  477. }
  478. if (update) {
  479. evo_mthd(push, 0x0080, 1);
  480. evo_data(push, 0x00000000);
  481. }
  482. evo_kick(push, dev, EVO_MASTER);
  483. }
  484. }
  485. static void
  486. nvd0_crtc_dpms(struct drm_crtc *crtc, int mode)
  487. {
  488. }
  489. static void
  490. nvd0_crtc_prepare(struct drm_crtc *crtc)
  491. {
  492. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  493. u32 *push;
  494. nvd0_display_flip_stop(crtc);
  495. push = evo_wait(crtc->dev, EVO_MASTER, 2);
  496. if (push) {
  497. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  498. evo_data(push, 0x00000000);
  499. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
  500. evo_data(push, 0x03000000);
  501. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  502. evo_data(push, 0x00000000);
  503. evo_kick(push, crtc->dev, EVO_MASTER);
  504. }
  505. nvd0_crtc_cursor_show(nv_crtc, false, false);
  506. }
  507. static void
  508. nvd0_crtc_commit(struct drm_crtc *crtc)
  509. {
  510. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  511. u32 *push;
  512. push = evo_wait(crtc->dev, EVO_MASTER, 32);
  513. if (push) {
  514. evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
  515. evo_data(push, nv_crtc->fb.tile_flags);
  516. evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
  517. evo_data(push, 0x83000000);
  518. evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
  519. evo_data(push, 0x00000000);
  520. evo_data(push, 0x00000000);
  521. evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
  522. evo_data(push, NvEvoVRAM);
  523. evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
  524. evo_data(push, 0xffffff00);
  525. evo_kick(push, crtc->dev, EVO_MASTER);
  526. }
  527. nvd0_crtc_cursor_show(nv_crtc, nv_crtc->cursor.visible, true);
  528. nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
  529. }
  530. static bool
  531. nvd0_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
  532. struct drm_display_mode *adjusted_mode)
  533. {
  534. return true;
  535. }
  536. static int
  537. nvd0_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
  538. {
  539. struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
  540. int ret;
  541. ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
  542. if (ret)
  543. return ret;
  544. if (old_fb) {
  545. nvfb = nouveau_framebuffer(old_fb);
  546. nouveau_bo_unpin(nvfb->nvbo);
  547. }
  548. return 0;
  549. }
  550. static int
  551. nvd0_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
  552. struct drm_display_mode *mode, int x, int y,
  553. struct drm_framebuffer *old_fb)
  554. {
  555. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  556. struct nouveau_connector *nv_connector;
  557. u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
  558. u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
  559. u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
  560. u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
  561. u32 vblan2e = 0, vblan2s = 1;
  562. u32 *push;
  563. int ret;
  564. hactive = mode->htotal;
  565. hsynce = mode->hsync_end - mode->hsync_start - 1;
  566. hbackp = mode->htotal - mode->hsync_end;
  567. hblanke = hsynce + hbackp;
  568. hfrontp = mode->hsync_start - mode->hdisplay;
  569. hblanks = mode->htotal - hfrontp - 1;
  570. vactive = mode->vtotal * vscan / ilace;
  571. vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
  572. vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
  573. vblanke = vsynce + vbackp;
  574. vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
  575. vblanks = vactive - vfrontp - 1;
  576. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  577. vblan2e = vactive + vsynce + vbackp;
  578. vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
  579. vactive = (vactive * 2) + 1;
  580. }
  581. ret = nvd0_crtc_swap_fbs(crtc, old_fb);
  582. if (ret)
  583. return ret;
  584. push = evo_wait(crtc->dev, EVO_MASTER, 64);
  585. if (push) {
  586. evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
  587. evo_data(push, 0x00000000);
  588. evo_data(push, (vactive << 16) | hactive);
  589. evo_data(push, ( vsynce << 16) | hsynce);
  590. evo_data(push, (vblanke << 16) | hblanke);
  591. evo_data(push, (vblanks << 16) | hblanks);
  592. evo_data(push, (vblan2e << 16) | vblan2s);
  593. evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
  594. evo_data(push, 0x00000000); /* ??? */
  595. evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
  596. evo_data(push, mode->clock * 1000);
  597. evo_data(push, 0x00200000); /* ??? */
  598. evo_data(push, mode->clock * 1000);
  599. evo_mthd(push, 0x04d0 + (nv_crtc->index * 0x300), 2);
  600. evo_data(push, 0x00000311);
  601. evo_data(push, 0x00000100);
  602. evo_kick(push, crtc->dev, EVO_MASTER);
  603. }
  604. nv_connector = nouveau_crtc_connector_get(nv_crtc);
  605. nvd0_crtc_set_dither(nv_crtc, false);
  606. nvd0_crtc_set_scale(nv_crtc, false);
  607. nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
  608. return 0;
  609. }
  610. static int
  611. nvd0_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  612. struct drm_framebuffer *old_fb)
  613. {
  614. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  615. int ret;
  616. if (!crtc->fb) {
  617. NV_DEBUG_KMS(crtc->dev, "No FB bound\n");
  618. return 0;
  619. }
  620. ret = nvd0_crtc_swap_fbs(crtc, old_fb);
  621. if (ret)
  622. return ret;
  623. nvd0_display_flip_stop(crtc);
  624. nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
  625. nvd0_display_flip_next(crtc, crtc->fb, NULL, 1);
  626. return 0;
  627. }
  628. static int
  629. nvd0_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
  630. struct drm_framebuffer *fb, int x, int y,
  631. enum mode_set_atomic state)
  632. {
  633. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  634. nvd0_display_flip_stop(crtc);
  635. nvd0_crtc_set_image(nv_crtc, fb, x, y, true);
  636. return 0;
  637. }
  638. static void
  639. nvd0_crtc_lut_load(struct drm_crtc *crtc)
  640. {
  641. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  642. void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
  643. int i;
  644. for (i = 0; i < 256; i++) {
  645. writew(0x6000 + (nv_crtc->lut.r[i] >> 2), lut + (i * 0x20) + 0);
  646. writew(0x6000 + (nv_crtc->lut.g[i] >> 2), lut + (i * 0x20) + 2);
  647. writew(0x6000 + (nv_crtc->lut.b[i] >> 2), lut + (i * 0x20) + 4);
  648. }
  649. }
  650. static int
  651. nvd0_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  652. uint32_t handle, uint32_t width, uint32_t height)
  653. {
  654. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  655. struct drm_device *dev = crtc->dev;
  656. struct drm_gem_object *gem;
  657. struct nouveau_bo *nvbo;
  658. bool visible = (handle != 0);
  659. int i, ret = 0;
  660. if (visible) {
  661. if (width != 64 || height != 64)
  662. return -EINVAL;
  663. gem = drm_gem_object_lookup(dev, file_priv, handle);
  664. if (unlikely(!gem))
  665. return -ENOENT;
  666. nvbo = nouveau_gem_object(gem);
  667. ret = nouveau_bo_map(nvbo);
  668. if (ret == 0) {
  669. for (i = 0; i < 64 * 64; i++) {
  670. u32 v = nouveau_bo_rd32(nvbo, i);
  671. nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
  672. }
  673. nouveau_bo_unmap(nvbo);
  674. }
  675. drm_gem_object_unreference_unlocked(gem);
  676. }
  677. if (visible != nv_crtc->cursor.visible) {
  678. nvd0_crtc_cursor_show(nv_crtc, visible, true);
  679. nv_crtc->cursor.visible = visible;
  680. }
  681. return ret;
  682. }
  683. static int
  684. nvd0_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  685. {
  686. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  687. int ch = EVO_CURS(nv_crtc->index);
  688. evo_piow(crtc->dev, ch, 0x0084, (y << 16) | x);
  689. evo_piow(crtc->dev, ch, 0x0080, 0x00000000);
  690. return 0;
  691. }
  692. static void
  693. nvd0_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  694. uint32_t start, uint32_t size)
  695. {
  696. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  697. u32 end = max(start + size, (u32)256);
  698. u32 i;
  699. for (i = start; i < end; i++) {
  700. nv_crtc->lut.r[i] = r[i];
  701. nv_crtc->lut.g[i] = g[i];
  702. nv_crtc->lut.b[i] = b[i];
  703. }
  704. nvd0_crtc_lut_load(crtc);
  705. }
  706. static void
  707. nvd0_crtc_destroy(struct drm_crtc *crtc)
  708. {
  709. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  710. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  711. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  712. nouveau_bo_unmap(nv_crtc->lut.nvbo);
  713. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  714. drm_crtc_cleanup(crtc);
  715. kfree(crtc);
  716. }
  717. static const struct drm_crtc_helper_funcs nvd0_crtc_hfunc = {
  718. .dpms = nvd0_crtc_dpms,
  719. .prepare = nvd0_crtc_prepare,
  720. .commit = nvd0_crtc_commit,
  721. .mode_fixup = nvd0_crtc_mode_fixup,
  722. .mode_set = nvd0_crtc_mode_set,
  723. .mode_set_base = nvd0_crtc_mode_set_base,
  724. .mode_set_base_atomic = nvd0_crtc_mode_set_base_atomic,
  725. .load_lut = nvd0_crtc_lut_load,
  726. };
  727. static const struct drm_crtc_funcs nvd0_crtc_func = {
  728. .cursor_set = nvd0_crtc_cursor_set,
  729. .cursor_move = nvd0_crtc_cursor_move,
  730. .gamma_set = nvd0_crtc_gamma_set,
  731. .set_config = drm_crtc_helper_set_config,
  732. .destroy = nvd0_crtc_destroy,
  733. .page_flip = nouveau_crtc_page_flip,
  734. };
  735. static void
  736. nvd0_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
  737. {
  738. }
  739. static void
  740. nvd0_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
  741. {
  742. }
  743. static int
  744. nvd0_crtc_create(struct drm_device *dev, int index)
  745. {
  746. struct nouveau_crtc *nv_crtc;
  747. struct drm_crtc *crtc;
  748. int ret, i;
  749. nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
  750. if (!nv_crtc)
  751. return -ENOMEM;
  752. nv_crtc->index = index;
  753. nv_crtc->set_dither = nvd0_crtc_set_dither;
  754. nv_crtc->set_scale = nvd0_crtc_set_scale;
  755. nv_crtc->cursor.set_offset = nvd0_cursor_set_offset;
  756. nv_crtc->cursor.set_pos = nvd0_cursor_set_pos;
  757. for (i = 0; i < 256; i++) {
  758. nv_crtc->lut.r[i] = i << 8;
  759. nv_crtc->lut.g[i] = i << 8;
  760. nv_crtc->lut.b[i] = i << 8;
  761. }
  762. crtc = &nv_crtc->base;
  763. drm_crtc_init(dev, crtc, &nvd0_crtc_func);
  764. drm_crtc_helper_add(crtc, &nvd0_crtc_hfunc);
  765. drm_mode_crtc_set_gamma_size(crtc, 256);
  766. ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
  767. 0, 0x0000, &nv_crtc->cursor.nvbo);
  768. if (!ret) {
  769. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
  770. if (!ret)
  771. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  772. if (ret)
  773. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  774. }
  775. if (ret)
  776. goto out;
  777. ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
  778. 0, 0x0000, &nv_crtc->lut.nvbo);
  779. if (!ret) {
  780. ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
  781. if (!ret)
  782. ret = nouveau_bo_map(nv_crtc->lut.nvbo);
  783. if (ret)
  784. nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
  785. }
  786. if (ret)
  787. goto out;
  788. nvd0_crtc_lut_load(crtc);
  789. out:
  790. if (ret)
  791. nvd0_crtc_destroy(crtc);
  792. return ret;
  793. }
  794. /******************************************************************************
  795. * DAC
  796. *****************************************************************************/
  797. static void
  798. nvd0_dac_dpms(struct drm_encoder *encoder, int mode)
  799. {
  800. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  801. struct drm_device *dev = encoder->dev;
  802. int or = nv_encoder->or;
  803. u32 dpms_ctrl;
  804. dpms_ctrl = 0x80000000;
  805. if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
  806. dpms_ctrl |= 0x00000001;
  807. if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
  808. dpms_ctrl |= 0x00000004;
  809. nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
  810. nv_mask(dev, 0x61a004 + (or * 0x0800), 0xc000007f, dpms_ctrl);
  811. nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
  812. }
  813. static bool
  814. nvd0_dac_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  815. struct drm_display_mode *adjusted_mode)
  816. {
  817. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  818. struct nouveau_connector *nv_connector;
  819. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  820. if (nv_connector && nv_connector->native_mode) {
  821. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  822. int id = adjusted_mode->base.id;
  823. *adjusted_mode = *nv_connector->native_mode;
  824. adjusted_mode->base.id = id;
  825. }
  826. }
  827. return true;
  828. }
  829. static void
  830. nvd0_dac_commit(struct drm_encoder *encoder)
  831. {
  832. }
  833. static void
  834. nvd0_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  835. struct drm_display_mode *adjusted_mode)
  836. {
  837. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  838. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  839. u32 syncs, magic, *push;
  840. syncs = 0x00000001;
  841. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  842. syncs |= 0x00000008;
  843. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  844. syncs |= 0x00000010;
  845. magic = 0x31ec6000 | (nv_crtc->index << 25);
  846. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  847. magic |= 0x00000001;
  848. nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
  849. push = evo_wait(encoder->dev, EVO_MASTER, 8);
  850. if (push) {
  851. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  852. evo_data(push, syncs);
  853. evo_data(push, magic);
  854. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x020), 2);
  855. evo_data(push, 1 << nv_crtc->index);
  856. evo_data(push, 0x00ff);
  857. evo_kick(push, encoder->dev, EVO_MASTER);
  858. }
  859. nv_encoder->crtc = encoder->crtc;
  860. }
  861. static void
  862. nvd0_dac_disconnect(struct drm_encoder *encoder)
  863. {
  864. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  865. struct drm_device *dev = encoder->dev;
  866. u32 *push;
  867. if (nv_encoder->crtc) {
  868. nvd0_crtc_prepare(nv_encoder->crtc);
  869. push = evo_wait(dev, EVO_MASTER, 4);
  870. if (push) {
  871. evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
  872. evo_data(push, 0x00000000);
  873. evo_mthd(push, 0x0080, 1);
  874. evo_data(push, 0x00000000);
  875. evo_kick(push, dev, EVO_MASTER);
  876. }
  877. nv_encoder->crtc = NULL;
  878. }
  879. }
  880. static enum drm_connector_status
  881. nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  882. {
  883. enum drm_connector_status status = connector_status_disconnected;
  884. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  885. struct drm_device *dev = encoder->dev;
  886. int or = nv_encoder->or;
  887. u32 load;
  888. nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00100000);
  889. udelay(9500);
  890. nv_wr32(dev, 0x61a00c + (or * 0x800), 0x80000000);
  891. load = nv_rd32(dev, 0x61a00c + (or * 0x800));
  892. if ((load & 0x38000000) == 0x38000000)
  893. status = connector_status_connected;
  894. nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00000000);
  895. return status;
  896. }
  897. static void
  898. nvd0_dac_destroy(struct drm_encoder *encoder)
  899. {
  900. drm_encoder_cleanup(encoder);
  901. kfree(encoder);
  902. }
  903. static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
  904. .dpms = nvd0_dac_dpms,
  905. .mode_fixup = nvd0_dac_mode_fixup,
  906. .prepare = nvd0_dac_disconnect,
  907. .commit = nvd0_dac_commit,
  908. .mode_set = nvd0_dac_mode_set,
  909. .disable = nvd0_dac_disconnect,
  910. .get_crtc = nvd0_display_crtc_get,
  911. .detect = nvd0_dac_detect
  912. };
  913. static const struct drm_encoder_funcs nvd0_dac_func = {
  914. .destroy = nvd0_dac_destroy,
  915. };
  916. static int
  917. nvd0_dac_create(struct drm_connector *connector, struct dcb_entry *dcbe)
  918. {
  919. struct drm_device *dev = connector->dev;
  920. struct nouveau_encoder *nv_encoder;
  921. struct drm_encoder *encoder;
  922. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  923. if (!nv_encoder)
  924. return -ENOMEM;
  925. nv_encoder->dcb = dcbe;
  926. nv_encoder->or = ffs(dcbe->or) - 1;
  927. encoder = to_drm_encoder(nv_encoder);
  928. encoder->possible_crtcs = dcbe->heads;
  929. encoder->possible_clones = 0;
  930. drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
  931. drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
  932. drm_mode_connector_attach_encoder(connector, encoder);
  933. return 0;
  934. }
  935. /******************************************************************************
  936. * Audio
  937. *****************************************************************************/
  938. static void
  939. nvd0_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  940. {
  941. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  942. struct nouveau_connector *nv_connector;
  943. struct drm_device *dev = encoder->dev;
  944. int i, or = nv_encoder->or * 0x30;
  945. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  946. if (!drm_detect_monitor_audio(nv_connector->edid))
  947. return;
  948. nv_mask(dev, 0x10ec10 + or, 0x80000003, 0x80000001);
  949. drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
  950. if (nv_connector->base.eld[0]) {
  951. u8 *eld = nv_connector->base.eld;
  952. for (i = 0; i < eld[2] * 4; i++)
  953. nv_wr32(dev, 0x10ec00 + or, (i << 8) | eld[i]);
  954. for (i = eld[2] * 4; i < 0x60; i++)
  955. nv_wr32(dev, 0x10ec00 + or, (i << 8) | 0x00);
  956. nv_mask(dev, 0x10ec10 + or, 0x80000002, 0x80000002);
  957. }
  958. }
  959. static void
  960. nvd0_audio_disconnect(struct drm_encoder *encoder)
  961. {
  962. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  963. struct drm_device *dev = encoder->dev;
  964. int or = nv_encoder->or * 0x30;
  965. nv_mask(dev, 0x10ec10 + or, 0x80000003, 0x80000000);
  966. }
  967. /******************************************************************************
  968. * HDMI
  969. *****************************************************************************/
  970. static void
  971. nvd0_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
  972. {
  973. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  974. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  975. struct nouveau_connector *nv_connector;
  976. struct drm_device *dev = encoder->dev;
  977. int head = nv_crtc->index * 0x800;
  978. u32 rekey = 56; /* binary driver, and tegra constant */
  979. u32 max_ac_packet;
  980. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  981. if (!drm_detect_hdmi_monitor(nv_connector->edid))
  982. return;
  983. max_ac_packet = mode->htotal - mode->hdisplay;
  984. max_ac_packet -= rekey;
  985. max_ac_packet -= 18; /* constant from tegra */
  986. max_ac_packet /= 32;
  987. /* AVI InfoFrame */
  988. nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000000);
  989. nv_wr32(dev, 0x61671c + head, 0x000d0282);
  990. nv_wr32(dev, 0x616720 + head, 0x0000006f);
  991. nv_wr32(dev, 0x616724 + head, 0x00000000);
  992. nv_wr32(dev, 0x616728 + head, 0x00000000);
  993. nv_wr32(dev, 0x61672c + head, 0x00000000);
  994. nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000001);
  995. /* ??? InfoFrame? */
  996. nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000000);
  997. nv_wr32(dev, 0x6167ac + head, 0x00000010);
  998. nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000001);
  999. /* HDMI_CTRL */
  1000. nv_mask(dev, 0x616798 + head, 0x401f007f, 0x40000000 | rekey |
  1001. max_ac_packet << 16);
  1002. /* NFI, audio doesn't work without it though.. */
  1003. nv_mask(dev, 0x616548 + head, 0x00000070, 0x00000000);
  1004. nvd0_audio_mode_set(encoder, mode);
  1005. }
  1006. static void
  1007. nvd0_hdmi_disconnect(struct drm_encoder *encoder)
  1008. {
  1009. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1010. struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
  1011. struct drm_device *dev = encoder->dev;
  1012. int head = nv_crtc->index * 0x800;
  1013. nvd0_audio_disconnect(encoder);
  1014. nv_mask(dev, 0x616798 + head, 0x40000000, 0x00000000);
  1015. nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000000);
  1016. nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000000);
  1017. }
  1018. /******************************************************************************
  1019. * SOR
  1020. *****************************************************************************/
  1021. static inline u32
  1022. nvd0_sor_dp_lane_map(struct drm_device *dev, struct dcb_entry *dcb, u8 lane)
  1023. {
  1024. static const u8 nvd0[] = { 16, 8, 0, 24 };
  1025. return nvd0[lane];
  1026. }
  1027. static void
  1028. nvd0_sor_dp_train_set(struct drm_device *dev, struct dcb_entry *dcb, u8 pattern)
  1029. {
  1030. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1031. const u32 loff = (or * 0x800) + (link * 0x80);
  1032. nv_mask(dev, 0x61c110 + loff, 0x0f0f0f0f, 0x01010101 * pattern);
  1033. }
  1034. static void
  1035. nvd0_sor_dp_train_adj(struct drm_device *dev, struct dcb_entry *dcb,
  1036. u8 lane, u8 swing, u8 preem)
  1037. {
  1038. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1039. const u32 loff = (or * 0x800) + (link * 0x80);
  1040. u32 shift = nvd0_sor_dp_lane_map(dev, dcb, lane);
  1041. u32 mask = 0x000000ff << shift;
  1042. u8 *table, *entry, *config = NULL;
  1043. switch (swing) {
  1044. case 0: preem += 0; break;
  1045. case 1: preem += 4; break;
  1046. case 2: preem += 7; break;
  1047. case 3: preem += 9; break;
  1048. }
  1049. table = nouveau_dp_bios_data(dev, dcb, &entry);
  1050. if (table) {
  1051. if (table[0] == 0x30) {
  1052. config = entry + table[4];
  1053. config += table[5] * preem;
  1054. } else
  1055. if (table[0] == 0x40) {
  1056. config = table + table[1];
  1057. config += table[2] * table[3];
  1058. config += table[6] * preem;
  1059. }
  1060. }
  1061. if (!config) {
  1062. NV_ERROR(dev, "PDISP: unsupported DP table for chipset\n");
  1063. return;
  1064. }
  1065. nv_mask(dev, 0x61c118 + loff, mask, config[1] << shift);
  1066. nv_mask(dev, 0x61c120 + loff, mask, config[2] << shift);
  1067. nv_mask(dev, 0x61c130 + loff, 0x0000ff00, config[3] << 8);
  1068. nv_mask(dev, 0x61c13c + loff, 0x00000000, 0x00000000);
  1069. }
  1070. static void
  1071. nvd0_sor_dp_link_set(struct drm_device *dev, struct dcb_entry *dcb, int crtc,
  1072. int link_nr, u32 link_bw, bool enhframe)
  1073. {
  1074. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1075. const u32 loff = (or * 0x800) + (link * 0x80);
  1076. const u32 soff = (or * 0x800);
  1077. u32 dpctrl = nv_rd32(dev, 0x61c10c + loff) & ~0x001f4000;
  1078. u32 clksor = nv_rd32(dev, 0x612300 + soff) & ~0x007c0000;
  1079. u32 script = 0x0000, lane_mask = 0;
  1080. u8 *table, *entry;
  1081. int i;
  1082. link_bw /= 27000;
  1083. table = nouveau_dp_bios_data(dev, dcb, &entry);
  1084. if (table) {
  1085. if (table[0] == 0x30) entry = ROMPTR(dev, entry[10]);
  1086. else if (table[0] == 0x40) entry = ROMPTR(dev, entry[9]);
  1087. else entry = NULL;
  1088. while (entry) {
  1089. if (entry[0] >= link_bw)
  1090. break;
  1091. entry += 3;
  1092. }
  1093. nouveau_bios_run_init_table(dev, script, dcb, crtc);
  1094. }
  1095. clksor |= link_bw << 18;
  1096. dpctrl |= ((1 << link_nr) - 1) << 16;
  1097. if (enhframe)
  1098. dpctrl |= 0x00004000;
  1099. for (i = 0; i < link_nr; i++)
  1100. lane_mask |= 1 << (nvd0_sor_dp_lane_map(dev, dcb, i) >> 3);
  1101. nv_wr32(dev, 0x612300 + soff, clksor);
  1102. nv_wr32(dev, 0x61c10c + loff, dpctrl);
  1103. nv_mask(dev, 0x61c130 + loff, 0x0000000f, lane_mask);
  1104. }
  1105. static void
  1106. nvd0_sor_dp_link_get(struct drm_device *dev, struct dcb_entry *dcb,
  1107. u32 *link_nr, u32 *link_bw)
  1108. {
  1109. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  1110. const u32 loff = (or * 0x800) + (link * 0x80);
  1111. const u32 soff = (or * 0x800);
  1112. u32 dpctrl = nv_rd32(dev, 0x61c10c + loff) & 0x000f0000;
  1113. u32 clksor = nv_rd32(dev, 0x612300 + soff);
  1114. if (dpctrl > 0x00030000) *link_nr = 4;
  1115. else if (dpctrl > 0x00010000) *link_nr = 2;
  1116. else *link_nr = 1;
  1117. *link_bw = (clksor & 0x007c0000) >> 18;
  1118. *link_bw *= 27000;
  1119. }
  1120. static void
  1121. nvd0_sor_dp_calc_tu(struct drm_device *dev, struct dcb_entry *dcb,
  1122. u32 crtc, u32 datarate)
  1123. {
  1124. const u32 symbol = 100000;
  1125. const u32 TU = 64;
  1126. u32 link_nr, link_bw;
  1127. u64 ratio, value;
  1128. nvd0_sor_dp_link_get(dev, dcb, &link_nr, &link_bw);
  1129. ratio = datarate;
  1130. ratio *= symbol;
  1131. do_div(ratio, link_nr * link_bw);
  1132. value = (symbol - ratio) * TU;
  1133. value *= ratio;
  1134. do_div(value, symbol);
  1135. do_div(value, symbol);
  1136. value += 5;
  1137. value |= 0x08000000;
  1138. nv_wr32(dev, 0x616610 + (crtc * 0x800), value);
  1139. }
  1140. static void
  1141. nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
  1142. {
  1143. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1144. struct drm_device *dev = encoder->dev;
  1145. struct drm_encoder *partner;
  1146. int or = nv_encoder->or;
  1147. u32 dpms_ctrl;
  1148. nv_encoder->last_dpms = mode;
  1149. list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
  1150. struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
  1151. if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
  1152. continue;
  1153. if (nv_partner != nv_encoder &&
  1154. nv_partner->dcb->or == nv_encoder->dcb->or) {
  1155. if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
  1156. return;
  1157. break;
  1158. }
  1159. }
  1160. dpms_ctrl = (mode == DRM_MODE_DPMS_ON);
  1161. dpms_ctrl |= 0x80000000;
  1162. nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
  1163. nv_mask(dev, 0x61c004 + (or * 0x0800), 0x80000001, dpms_ctrl);
  1164. nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
  1165. nv_wait(dev, 0x61c030 + (or * 0x0800), 0x10000000, 0x00000000);
  1166. if (nv_encoder->dcb->type == OUTPUT_DP) {
  1167. struct dp_train_func func = {
  1168. .link_set = nvd0_sor_dp_link_set,
  1169. .train_set = nvd0_sor_dp_train_set,
  1170. .train_adj = nvd0_sor_dp_train_adj
  1171. };
  1172. nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, &func);
  1173. }
  1174. }
  1175. static bool
  1176. nvd0_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  1177. struct drm_display_mode *adjusted_mode)
  1178. {
  1179. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1180. struct nouveau_connector *nv_connector;
  1181. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1182. if (nv_connector && nv_connector->native_mode) {
  1183. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  1184. int id = adjusted_mode->base.id;
  1185. *adjusted_mode = *nv_connector->native_mode;
  1186. adjusted_mode->base.id = id;
  1187. }
  1188. }
  1189. return true;
  1190. }
  1191. static void
  1192. nvd0_sor_disconnect(struct drm_encoder *encoder)
  1193. {
  1194. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1195. struct drm_device *dev = encoder->dev;
  1196. u32 *push;
  1197. if (nv_encoder->crtc) {
  1198. nvd0_crtc_prepare(nv_encoder->crtc);
  1199. push = evo_wait(dev, EVO_MASTER, 4);
  1200. if (push) {
  1201. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
  1202. evo_data(push, 0x00000000);
  1203. evo_mthd(push, 0x0080, 1);
  1204. evo_data(push, 0x00000000);
  1205. evo_kick(push, dev, EVO_MASTER);
  1206. }
  1207. nvd0_hdmi_disconnect(encoder);
  1208. nv_encoder->crtc = NULL;
  1209. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1210. }
  1211. }
  1212. static void
  1213. nvd0_sor_prepare(struct drm_encoder *encoder)
  1214. {
  1215. nvd0_sor_disconnect(encoder);
  1216. if (nouveau_encoder(encoder)->dcb->type == OUTPUT_DP)
  1217. evo_sync(encoder->dev, EVO_MASTER);
  1218. }
  1219. static void
  1220. nvd0_sor_commit(struct drm_encoder *encoder)
  1221. {
  1222. }
  1223. static void
  1224. nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  1225. struct drm_display_mode *mode)
  1226. {
  1227. struct drm_device *dev = encoder->dev;
  1228. struct drm_nouveau_private *dev_priv = dev->dev_private;
  1229. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  1230. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  1231. struct nouveau_connector *nv_connector;
  1232. struct nvbios *bios = &dev_priv->vbios;
  1233. u32 mode_ctrl = (1 << nv_crtc->index);
  1234. u32 syncs, magic, *push;
  1235. u32 or_config;
  1236. syncs = 0x00000001;
  1237. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1238. syncs |= 0x00000008;
  1239. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1240. syncs |= 0x00000010;
  1241. magic = 0x31ec6000 | (nv_crtc->index << 25);
  1242. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1243. magic |= 0x00000001;
  1244. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  1245. switch (nv_encoder->dcb->type) {
  1246. case OUTPUT_TMDS:
  1247. if (nv_encoder->dcb->sorconf.link & 1) {
  1248. if (mode->clock < 165000)
  1249. mode_ctrl |= 0x00000100;
  1250. else
  1251. mode_ctrl |= 0x00000500;
  1252. } else {
  1253. mode_ctrl |= 0x00000200;
  1254. }
  1255. or_config = (mode_ctrl & 0x00000f00) >> 8;
  1256. if (mode->clock >= 165000)
  1257. or_config |= 0x0100;
  1258. nvd0_hdmi_mode_set(encoder, mode);
  1259. break;
  1260. case OUTPUT_LVDS:
  1261. or_config = (mode_ctrl & 0x00000f00) >> 8;
  1262. if (bios->fp_no_ddc) {
  1263. if (bios->fp.dual_link)
  1264. or_config |= 0x0100;
  1265. if (bios->fp.if_is_24bit)
  1266. or_config |= 0x0200;
  1267. } else {
  1268. if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  1269. if (((u8 *)nv_connector->edid)[121] == 2)
  1270. or_config |= 0x0100;
  1271. } else
  1272. if (mode->clock >= bios->fp.duallink_transition_clk) {
  1273. or_config |= 0x0100;
  1274. }
  1275. if (or_config & 0x0100) {
  1276. if (bios->fp.strapless_is_24bit & 2)
  1277. or_config |= 0x0200;
  1278. } else {
  1279. if (bios->fp.strapless_is_24bit & 1)
  1280. or_config |= 0x0200;
  1281. }
  1282. if (nv_connector->base.display_info.bpc == 8)
  1283. or_config |= 0x0200;
  1284. }
  1285. break;
  1286. case OUTPUT_DP:
  1287. if (nv_connector->base.display_info.bpc == 6) {
  1288. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  1289. syncs |= 0x00000140;
  1290. } else {
  1291. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  1292. syncs |= 0x00000180;
  1293. }
  1294. if (nv_encoder->dcb->sorconf.link & 1)
  1295. mode_ctrl |= 0x00000800;
  1296. else
  1297. mode_ctrl |= 0x00000900;
  1298. or_config = (mode_ctrl & 0x00000f00) >> 8;
  1299. break;
  1300. default:
  1301. BUG_ON(1);
  1302. break;
  1303. }
  1304. nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  1305. if (nv_encoder->dcb->type == OUTPUT_DP) {
  1306. nvd0_sor_dp_calc_tu(dev, nv_encoder->dcb, nv_crtc->index,
  1307. nv_encoder->dp.datarate);
  1308. }
  1309. push = evo_wait(dev, EVO_MASTER, 8);
  1310. if (push) {
  1311. evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
  1312. evo_data(push, syncs);
  1313. evo_data(push, magic);
  1314. evo_mthd(push, 0x0200 + (nv_encoder->or * 0x020), 2);
  1315. evo_data(push, mode_ctrl);
  1316. evo_data(push, or_config);
  1317. evo_kick(push, dev, EVO_MASTER);
  1318. }
  1319. nv_encoder->crtc = encoder->crtc;
  1320. }
  1321. static void
  1322. nvd0_sor_destroy(struct drm_encoder *encoder)
  1323. {
  1324. drm_encoder_cleanup(encoder);
  1325. kfree(encoder);
  1326. }
  1327. static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
  1328. .dpms = nvd0_sor_dpms,
  1329. .mode_fixup = nvd0_sor_mode_fixup,
  1330. .prepare = nvd0_sor_prepare,
  1331. .commit = nvd0_sor_commit,
  1332. .mode_set = nvd0_sor_mode_set,
  1333. .disable = nvd0_sor_disconnect,
  1334. .get_crtc = nvd0_display_crtc_get,
  1335. };
  1336. static const struct drm_encoder_funcs nvd0_sor_func = {
  1337. .destroy = nvd0_sor_destroy,
  1338. };
  1339. static int
  1340. nvd0_sor_create(struct drm_connector *connector, struct dcb_entry *dcbe)
  1341. {
  1342. struct drm_device *dev = connector->dev;
  1343. struct nouveau_encoder *nv_encoder;
  1344. struct drm_encoder *encoder;
  1345. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  1346. if (!nv_encoder)
  1347. return -ENOMEM;
  1348. nv_encoder->dcb = dcbe;
  1349. nv_encoder->or = ffs(dcbe->or) - 1;
  1350. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  1351. encoder = to_drm_encoder(nv_encoder);
  1352. encoder->possible_crtcs = dcbe->heads;
  1353. encoder->possible_clones = 0;
  1354. drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
  1355. drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
  1356. drm_mode_connector_attach_encoder(connector, encoder);
  1357. return 0;
  1358. }
  1359. /******************************************************************************
  1360. * IRQ
  1361. *****************************************************************************/
  1362. static struct dcb_entry *
  1363. lookup_dcb(struct drm_device *dev, int id, u32 mc)
  1364. {
  1365. struct drm_nouveau_private *dev_priv = dev->dev_private;
  1366. int type, or, i, link = -1;
  1367. if (id < 4) {
  1368. type = OUTPUT_ANALOG;
  1369. or = id;
  1370. } else {
  1371. switch (mc & 0x00000f00) {
  1372. case 0x00000000: link = 0; type = OUTPUT_LVDS; break;
  1373. case 0x00000100: link = 0; type = OUTPUT_TMDS; break;
  1374. case 0x00000200: link = 1; type = OUTPUT_TMDS; break;
  1375. case 0x00000500: link = 0; type = OUTPUT_TMDS; break;
  1376. case 0x00000800: link = 0; type = OUTPUT_DP; break;
  1377. case 0x00000900: link = 1; type = OUTPUT_DP; break;
  1378. default:
  1379. NV_ERROR(dev, "PDISP: unknown SOR mc 0x%08x\n", mc);
  1380. return NULL;
  1381. }
  1382. or = id - 4;
  1383. }
  1384. for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
  1385. struct dcb_entry *dcb = &dev_priv->vbios.dcb.entry[i];
  1386. if (dcb->type == type && (dcb->or & (1 << or)) &&
  1387. (link < 0 || link == !(dcb->sorconf.link & 1)))
  1388. return dcb;
  1389. }
  1390. NV_ERROR(dev, "PDISP: DCB for %d/0x%08x not found\n", id, mc);
  1391. return NULL;
  1392. }
  1393. static void
  1394. nvd0_display_unk1_handler(struct drm_device *dev, u32 crtc, u32 mask)
  1395. {
  1396. struct dcb_entry *dcb;
  1397. int i;
  1398. for (i = 0; mask && i < 8; i++) {
  1399. u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
  1400. if (!(mcc & (1 << crtc)))
  1401. continue;
  1402. dcb = lookup_dcb(dev, i, mcc);
  1403. if (!dcb)
  1404. continue;
  1405. nouveau_bios_run_display_table(dev, 0x0000, -1, dcb, crtc);
  1406. }
  1407. nv_wr32(dev, 0x6101d4, 0x00000000);
  1408. nv_wr32(dev, 0x6109d4, 0x00000000);
  1409. nv_wr32(dev, 0x6101d0, 0x80000000);
  1410. }
  1411. static void
  1412. nvd0_display_unk2_handler(struct drm_device *dev, u32 crtc, u32 mask)
  1413. {
  1414. struct dcb_entry *dcb;
  1415. u32 or, tmp, pclk;
  1416. int i;
  1417. for (i = 0; mask && i < 8; i++) {
  1418. u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
  1419. if (!(mcc & (1 << crtc)))
  1420. continue;
  1421. dcb = lookup_dcb(dev, i, mcc);
  1422. if (!dcb)
  1423. continue;
  1424. nouveau_bios_run_display_table(dev, 0x0000, -2, dcb, crtc);
  1425. }
  1426. pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
  1427. NV_DEBUG_KMS(dev, "PDISP: crtc %d pclk %d mask 0x%08x\n",
  1428. crtc, pclk, mask);
  1429. if (pclk && (mask & 0x00010000)) {
  1430. nv50_crtc_set_clock(dev, crtc, pclk);
  1431. }
  1432. for (i = 0; mask && i < 8; i++) {
  1433. u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
  1434. u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
  1435. if (!(mcp & (1 << crtc)))
  1436. continue;
  1437. dcb = lookup_dcb(dev, i, mcp);
  1438. if (!dcb)
  1439. continue;
  1440. or = ffs(dcb->or) - 1;
  1441. nouveau_bios_run_display_table(dev, cfg, pclk, dcb, crtc);
  1442. nv_wr32(dev, 0x612200 + (crtc * 0x800), 0x00000000);
  1443. switch (dcb->type) {
  1444. case OUTPUT_ANALOG:
  1445. nv_wr32(dev, 0x612280 + (or * 0x800), 0x00000000);
  1446. break;
  1447. case OUTPUT_TMDS:
  1448. case OUTPUT_LVDS:
  1449. case OUTPUT_DP:
  1450. if (cfg & 0x00000100)
  1451. tmp = 0x00000101;
  1452. else
  1453. tmp = 0x00000000;
  1454. nv_mask(dev, 0x612300 + (or * 0x800), 0x00000707, tmp);
  1455. break;
  1456. default:
  1457. break;
  1458. }
  1459. break;
  1460. }
  1461. nv_wr32(dev, 0x6101d4, 0x00000000);
  1462. nv_wr32(dev, 0x6109d4, 0x00000000);
  1463. nv_wr32(dev, 0x6101d0, 0x80000000);
  1464. }
  1465. static void
  1466. nvd0_display_unk4_handler(struct drm_device *dev, u32 crtc, u32 mask)
  1467. {
  1468. struct dcb_entry *dcb;
  1469. int pclk, i;
  1470. pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
  1471. for (i = 0; mask && i < 8; i++) {
  1472. u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
  1473. u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
  1474. if (!(mcp & (1 << crtc)))
  1475. continue;
  1476. dcb = lookup_dcb(dev, i, mcp);
  1477. if (!dcb)
  1478. continue;
  1479. nouveau_bios_run_display_table(dev, cfg, -pclk, dcb, crtc);
  1480. }
  1481. nv_wr32(dev, 0x6101d4, 0x00000000);
  1482. nv_wr32(dev, 0x6109d4, 0x00000000);
  1483. nv_wr32(dev, 0x6101d0, 0x80000000);
  1484. }
  1485. static void
  1486. nvd0_display_bh(unsigned long data)
  1487. {
  1488. struct drm_device *dev = (struct drm_device *)data;
  1489. struct nvd0_display *disp = nvd0_display(dev);
  1490. u32 mask = 0, crtc = ~0;
  1491. int i;
  1492. if (drm_debug & (DRM_UT_DRIVER | DRM_UT_KMS)) {
  1493. NV_INFO(dev, "PDISP: modeset req %d\n", disp->modeset);
  1494. NV_INFO(dev, " STAT: 0x%08x 0x%08x 0x%08x\n",
  1495. nv_rd32(dev, 0x6101d0),
  1496. nv_rd32(dev, 0x6101d4), nv_rd32(dev, 0x6109d4));
  1497. for (i = 0; i < 8; i++) {
  1498. NV_INFO(dev, " %s%d: 0x%08x 0x%08x\n",
  1499. i < 4 ? "DAC" : "SOR", i,
  1500. nv_rd32(dev, 0x640180 + (i * 0x20)),
  1501. nv_rd32(dev, 0x660180 + (i * 0x20)));
  1502. }
  1503. }
  1504. while (!mask && ++crtc < dev->mode_config.num_crtc)
  1505. mask = nv_rd32(dev, 0x6101d4 + (crtc * 0x800));
  1506. if (disp->modeset & 0x00000001)
  1507. nvd0_display_unk1_handler(dev, crtc, mask);
  1508. if (disp->modeset & 0x00000002)
  1509. nvd0_display_unk2_handler(dev, crtc, mask);
  1510. if (disp->modeset & 0x00000004)
  1511. nvd0_display_unk4_handler(dev, crtc, mask);
  1512. }
  1513. static void
  1514. nvd0_display_intr(struct drm_device *dev)
  1515. {
  1516. struct nvd0_display *disp = nvd0_display(dev);
  1517. u32 intr = nv_rd32(dev, 0x610088);
  1518. int i;
  1519. if (intr & 0x00000001) {
  1520. u32 stat = nv_rd32(dev, 0x61008c);
  1521. nv_wr32(dev, 0x61008c, stat);
  1522. intr &= ~0x00000001;
  1523. }
  1524. if (intr & 0x00000002) {
  1525. u32 stat = nv_rd32(dev, 0x61009c);
  1526. int chid = ffs(stat) - 1;
  1527. if (chid >= 0) {
  1528. u32 mthd = nv_rd32(dev, 0x6101f0 + (chid * 12));
  1529. u32 data = nv_rd32(dev, 0x6101f4 + (chid * 12));
  1530. u32 unkn = nv_rd32(dev, 0x6101f8 + (chid * 12));
  1531. NV_INFO(dev, "EvoCh: chid %d mthd 0x%04x data 0x%08x "
  1532. "0x%08x 0x%08x\n",
  1533. chid, (mthd & 0x0000ffc), data, mthd, unkn);
  1534. nv_wr32(dev, 0x61009c, (1 << chid));
  1535. nv_wr32(dev, 0x6101f0 + (chid * 12), 0x90000000);
  1536. }
  1537. intr &= ~0x00000002;
  1538. }
  1539. if (intr & 0x00100000) {
  1540. u32 stat = nv_rd32(dev, 0x6100ac);
  1541. if (stat & 0x00000007) {
  1542. disp->modeset = stat;
  1543. tasklet_schedule(&disp->tasklet);
  1544. nv_wr32(dev, 0x6100ac, (stat & 0x00000007));
  1545. stat &= ~0x00000007;
  1546. }
  1547. if (stat) {
  1548. NV_INFO(dev, "PDISP: unknown intr24 0x%08x\n", stat);
  1549. nv_wr32(dev, 0x6100ac, stat);
  1550. }
  1551. intr &= ~0x00100000;
  1552. }
  1553. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  1554. u32 mask = 0x01000000 << i;
  1555. if (intr & mask) {
  1556. u32 stat = nv_rd32(dev, 0x6100bc + (i * 0x800));
  1557. nv_wr32(dev, 0x6100bc + (i * 0x800), stat);
  1558. intr &= ~mask;
  1559. }
  1560. }
  1561. if (intr)
  1562. NV_INFO(dev, "PDISP: unknown intr 0x%08x\n", intr);
  1563. }
  1564. /******************************************************************************
  1565. * Init
  1566. *****************************************************************************/
  1567. void
  1568. nvd0_display_fini(struct drm_device *dev)
  1569. {
  1570. int i;
  1571. /* fini cursors + overlays + flips */
  1572. for (i = 1; i >= 0; i--) {
  1573. evo_fini_pio(dev, EVO_CURS(i));
  1574. evo_fini_pio(dev, EVO_OIMM(i));
  1575. evo_fini_dma(dev, EVO_OVLY(i));
  1576. evo_fini_dma(dev, EVO_FLIP(i));
  1577. }
  1578. /* fini master */
  1579. evo_fini_dma(dev, EVO_MASTER);
  1580. }
  1581. int
  1582. nvd0_display_init(struct drm_device *dev)
  1583. {
  1584. struct nvd0_display *disp = nvd0_display(dev);
  1585. int ret, i;
  1586. u32 *push;
  1587. if (nv_rd32(dev, 0x6100ac) & 0x00000100) {
  1588. nv_wr32(dev, 0x6100ac, 0x00000100);
  1589. nv_mask(dev, 0x6194e8, 0x00000001, 0x00000000);
  1590. if (!nv_wait(dev, 0x6194e8, 0x00000002, 0x00000000)) {
  1591. NV_ERROR(dev, "PDISP: 0x6194e8 0x%08x\n",
  1592. nv_rd32(dev, 0x6194e8));
  1593. return -EBUSY;
  1594. }
  1595. }
  1596. /* nfi what these are exactly, i do know that SOR_MODE_CTRL won't
  1597. * work at all unless you do the SOR part below.
  1598. */
  1599. for (i = 0; i < 3; i++) {
  1600. u32 dac = nv_rd32(dev, 0x61a000 + (i * 0x800));
  1601. nv_wr32(dev, 0x6101c0 + (i * 0x800), dac);
  1602. }
  1603. for (i = 0; i < 4; i++) {
  1604. u32 sor = nv_rd32(dev, 0x61c000 + (i * 0x800));
  1605. nv_wr32(dev, 0x6301c4 + (i * 0x800), sor);
  1606. }
  1607. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  1608. u32 crtc0 = nv_rd32(dev, 0x616104 + (i * 0x800));
  1609. u32 crtc1 = nv_rd32(dev, 0x616108 + (i * 0x800));
  1610. u32 crtc2 = nv_rd32(dev, 0x61610c + (i * 0x800));
  1611. nv_wr32(dev, 0x6101b4 + (i * 0x800), crtc0);
  1612. nv_wr32(dev, 0x6101b8 + (i * 0x800), crtc1);
  1613. nv_wr32(dev, 0x6101bc + (i * 0x800), crtc2);
  1614. }
  1615. /* point at our hash table / objects, enable interrupts */
  1616. nv_wr32(dev, 0x610010, (disp->mem->vinst >> 8) | 9);
  1617. nv_mask(dev, 0x6100b0, 0x00000307, 0x00000307);
  1618. /* init master */
  1619. ret = evo_init_dma(dev, EVO_MASTER);
  1620. if (ret)
  1621. goto error;
  1622. /* init flips + overlays + cursors */
  1623. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  1624. if ((ret = evo_init_dma(dev, EVO_FLIP(i))) ||
  1625. (ret = evo_init_dma(dev, EVO_OVLY(i))) ||
  1626. (ret = evo_init_pio(dev, EVO_OIMM(i))) ||
  1627. (ret = evo_init_pio(dev, EVO_CURS(i))))
  1628. goto error;
  1629. }
  1630. push = evo_wait(dev, EVO_MASTER, 32);
  1631. if (!push) {
  1632. ret = -EBUSY;
  1633. goto error;
  1634. }
  1635. evo_mthd(push, 0x0088, 1);
  1636. evo_data(push, NvEvoSync);
  1637. evo_mthd(push, 0x0084, 1);
  1638. evo_data(push, 0x00000000);
  1639. evo_mthd(push, 0x0084, 1);
  1640. evo_data(push, 0x80000000);
  1641. evo_mthd(push, 0x008c, 1);
  1642. evo_data(push, 0x00000000);
  1643. evo_kick(push, dev, EVO_MASTER);
  1644. error:
  1645. if (ret)
  1646. nvd0_display_fini(dev);
  1647. return ret;
  1648. }
  1649. void
  1650. nvd0_display_destroy(struct drm_device *dev)
  1651. {
  1652. struct drm_nouveau_private *dev_priv = dev->dev_private;
  1653. struct nvd0_display *disp = nvd0_display(dev);
  1654. struct pci_dev *pdev = dev->pdev;
  1655. int i;
  1656. for (i = 0; i < EVO_DMA_NR; i++) {
  1657. struct evo *evo = &disp->evo[i];
  1658. pci_free_consistent(pdev, PAGE_SIZE, evo->ptr, evo->handle);
  1659. }
  1660. nouveau_gpuobj_ref(NULL, &disp->mem);
  1661. nouveau_bo_unmap(disp->sync);
  1662. nouveau_bo_ref(NULL, &disp->sync);
  1663. nouveau_irq_unregister(dev, 26);
  1664. dev_priv->engine.display.priv = NULL;
  1665. kfree(disp);
  1666. }
  1667. int
  1668. nvd0_display_create(struct drm_device *dev)
  1669. {
  1670. struct drm_nouveau_private *dev_priv = dev->dev_private;
  1671. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  1672. struct dcb_table *dcb = &dev_priv->vbios.dcb;
  1673. struct drm_connector *connector, *tmp;
  1674. struct pci_dev *pdev = dev->pdev;
  1675. struct nvd0_display *disp;
  1676. struct dcb_entry *dcbe;
  1677. int crtcs, ret, i;
  1678. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  1679. if (!disp)
  1680. return -ENOMEM;
  1681. dev_priv->engine.display.priv = disp;
  1682. /* create crtc objects to represent the hw heads */
  1683. crtcs = nv_rd32(dev, 0x022448);
  1684. for (i = 0; i < crtcs; i++) {
  1685. ret = nvd0_crtc_create(dev, i);
  1686. if (ret)
  1687. goto out;
  1688. }
  1689. /* create encoder/connector objects based on VBIOS DCB table */
  1690. for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
  1691. connector = nouveau_connector_create(dev, dcbe->connector);
  1692. if (IS_ERR(connector))
  1693. continue;
  1694. if (dcbe->location != DCB_LOC_ON_CHIP) {
  1695. NV_WARN(dev, "skipping off-chip encoder %d/%d\n",
  1696. dcbe->type, ffs(dcbe->or) - 1);
  1697. continue;
  1698. }
  1699. switch (dcbe->type) {
  1700. case OUTPUT_TMDS:
  1701. case OUTPUT_LVDS:
  1702. case OUTPUT_DP:
  1703. nvd0_sor_create(connector, dcbe);
  1704. break;
  1705. case OUTPUT_ANALOG:
  1706. nvd0_dac_create(connector, dcbe);
  1707. break;
  1708. default:
  1709. NV_WARN(dev, "skipping unsupported encoder %d/%d\n",
  1710. dcbe->type, ffs(dcbe->or) - 1);
  1711. continue;
  1712. }
  1713. }
  1714. /* cull any connectors we created that don't have an encoder */
  1715. list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
  1716. if (connector->encoder_ids[0])
  1717. continue;
  1718. NV_WARN(dev, "%s has no encoders, removing\n",
  1719. drm_get_connector_name(connector));
  1720. connector->funcs->destroy(connector);
  1721. }
  1722. /* setup interrupt handling */
  1723. tasklet_init(&disp->tasklet, nvd0_display_bh, (unsigned long)dev);
  1724. nouveau_irq_register(dev, 26, nvd0_display_intr);
  1725. /* small shared memory area we use for notifiers and semaphores */
  1726. ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
  1727. 0, 0x0000, &disp->sync);
  1728. if (!ret) {
  1729. ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
  1730. if (!ret)
  1731. ret = nouveau_bo_map(disp->sync);
  1732. if (ret)
  1733. nouveau_bo_ref(NULL, &disp->sync);
  1734. }
  1735. if (ret)
  1736. goto out;
  1737. /* hash table and dma objects for the memory areas we care about */
  1738. ret = nouveau_gpuobj_new(dev, NULL, 0x4000, 0x10000,
  1739. NVOBJ_FLAG_ZERO_ALLOC, &disp->mem);
  1740. if (ret)
  1741. goto out;
  1742. /* create evo dma channels */
  1743. for (i = 0; i < EVO_DMA_NR; i++) {
  1744. struct evo *evo = &disp->evo[i];
  1745. u64 offset = disp->sync->bo.offset;
  1746. u32 dmao = 0x1000 + (i * 0x100);
  1747. u32 hash = 0x0000 + (i * 0x040);
  1748. evo->idx = i;
  1749. evo->sem.offset = EVO_SYNC(evo->idx, 0x00);
  1750. evo->ptr = pci_alloc_consistent(pdev, PAGE_SIZE, &evo->handle);
  1751. if (!evo->ptr) {
  1752. ret = -ENOMEM;
  1753. goto out;
  1754. }
  1755. nv_wo32(disp->mem, dmao + 0x00, 0x00000049);
  1756. nv_wo32(disp->mem, dmao + 0x04, (offset + 0x0000) >> 8);
  1757. nv_wo32(disp->mem, dmao + 0x08, (offset + 0x0fff) >> 8);
  1758. nv_wo32(disp->mem, dmao + 0x0c, 0x00000000);
  1759. nv_wo32(disp->mem, dmao + 0x10, 0x00000000);
  1760. nv_wo32(disp->mem, dmao + 0x14, 0x00000000);
  1761. nv_wo32(disp->mem, hash + 0x00, NvEvoSync);
  1762. nv_wo32(disp->mem, hash + 0x04, 0x00000001 | (i << 27) |
  1763. ((dmao + 0x00) << 9));
  1764. nv_wo32(disp->mem, dmao + 0x20, 0x00000049);
  1765. nv_wo32(disp->mem, dmao + 0x24, 0x00000000);
  1766. nv_wo32(disp->mem, dmao + 0x28, (dev_priv->vram_size - 1) >> 8);
  1767. nv_wo32(disp->mem, dmao + 0x2c, 0x00000000);
  1768. nv_wo32(disp->mem, dmao + 0x30, 0x00000000);
  1769. nv_wo32(disp->mem, dmao + 0x34, 0x00000000);
  1770. nv_wo32(disp->mem, hash + 0x08, NvEvoVRAM);
  1771. nv_wo32(disp->mem, hash + 0x0c, 0x00000001 | (i << 27) |
  1772. ((dmao + 0x20) << 9));
  1773. nv_wo32(disp->mem, dmao + 0x40, 0x00000009);
  1774. nv_wo32(disp->mem, dmao + 0x44, 0x00000000);
  1775. nv_wo32(disp->mem, dmao + 0x48, (dev_priv->vram_size - 1) >> 8);
  1776. nv_wo32(disp->mem, dmao + 0x4c, 0x00000000);
  1777. nv_wo32(disp->mem, dmao + 0x50, 0x00000000);
  1778. nv_wo32(disp->mem, dmao + 0x54, 0x00000000);
  1779. nv_wo32(disp->mem, hash + 0x10, NvEvoVRAM_LP);
  1780. nv_wo32(disp->mem, hash + 0x14, 0x00000001 | (i << 27) |
  1781. ((dmao + 0x40) << 9));
  1782. nv_wo32(disp->mem, dmao + 0x60, 0x0fe00009);
  1783. nv_wo32(disp->mem, dmao + 0x64, 0x00000000);
  1784. nv_wo32(disp->mem, dmao + 0x68, (dev_priv->vram_size - 1) >> 8);
  1785. nv_wo32(disp->mem, dmao + 0x6c, 0x00000000);
  1786. nv_wo32(disp->mem, dmao + 0x70, 0x00000000);
  1787. nv_wo32(disp->mem, dmao + 0x74, 0x00000000);
  1788. nv_wo32(disp->mem, hash + 0x18, NvEvoFB32);
  1789. nv_wo32(disp->mem, hash + 0x1c, 0x00000001 | (i << 27) |
  1790. ((dmao + 0x60) << 9));
  1791. }
  1792. pinstmem->flush(dev);
  1793. out:
  1794. if (ret)
  1795. nvd0_display_destroy(dev);
  1796. return ret;
  1797. }