nvd0_display.c 57 KB

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