nvd0_display.c 56 KB

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