nvd0_display.c 51 KB

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