nvd0_display.c 57 KB

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