nvd0_display.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  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);
  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_entry *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_entry *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_entry *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_entry *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_entry *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_entry *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_entry *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 == 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 == 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 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 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 OUTPUT_DP:
  1289. if (nv_connector->base.display_info.bpc == 6) {
  1290. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  1291. syncs |= 0x00000140;
  1292. } else {
  1293. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  1294. syncs |= 0x00000180;
  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 == 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_entry *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_entry *
  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 = OUTPUT_ANALOG;
  1371. or = id;
  1372. } else {
  1373. switch (mc & 0x00000f00) {
  1374. case 0x00000000: link = 0; type = OUTPUT_LVDS; break;
  1375. case 0x00000100: link = 0; type = OUTPUT_TMDS; break;
  1376. case 0x00000200: link = 1; type = OUTPUT_TMDS; break;
  1377. case 0x00000500: link = 0; type = OUTPUT_TMDS; break;
  1378. case 0x00000800: link = 0; type = OUTPUT_DP; break;
  1379. case 0x00000900: link = 1; type = 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_entry *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_entry *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_entry *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 OUTPUT_ANALOG:
  1447. nv_wr32(dev, 0x612280 + (or * 0x800), 0x00000000);
  1448. break;
  1449. case OUTPUT_TMDS:
  1450. case OUTPUT_LVDS:
  1451. case 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_entry *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->vinst >> 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 nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  1674. struct dcb_table *dcb = &dev_priv->vbios.dcb;
  1675. struct drm_connector *connector, *tmp;
  1676. struct pci_dev *pdev = dev->pdev;
  1677. struct nvd0_display *disp;
  1678. struct dcb_entry *dcbe;
  1679. int crtcs, ret, i;
  1680. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  1681. if (!disp)
  1682. return -ENOMEM;
  1683. dev_priv->engine.display.priv = disp;
  1684. /* create crtc objects to represent the hw heads */
  1685. crtcs = nv_rd32(dev, 0x022448);
  1686. for (i = 0; i < crtcs; i++) {
  1687. ret = nvd0_crtc_create(dev, i);
  1688. if (ret)
  1689. goto out;
  1690. }
  1691. /* create encoder/connector objects based on VBIOS DCB table */
  1692. for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
  1693. connector = nouveau_connector_create(dev, dcbe->connector);
  1694. if (IS_ERR(connector))
  1695. continue;
  1696. if (dcbe->location != DCB_LOC_ON_CHIP) {
  1697. NV_WARN(dev, "skipping off-chip encoder %d/%d\n",
  1698. dcbe->type, ffs(dcbe->or) - 1);
  1699. continue;
  1700. }
  1701. switch (dcbe->type) {
  1702. case OUTPUT_TMDS:
  1703. case OUTPUT_LVDS:
  1704. case OUTPUT_DP:
  1705. nvd0_sor_create(connector, dcbe);
  1706. break;
  1707. case OUTPUT_ANALOG:
  1708. nvd0_dac_create(connector, dcbe);
  1709. break;
  1710. default:
  1711. NV_WARN(dev, "skipping unsupported encoder %d/%d\n",
  1712. dcbe->type, ffs(dcbe->or) - 1);
  1713. continue;
  1714. }
  1715. }
  1716. /* cull any connectors we created that don't have an encoder */
  1717. list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
  1718. if (connector->encoder_ids[0])
  1719. continue;
  1720. NV_WARN(dev, "%s has no encoders, removing\n",
  1721. drm_get_connector_name(connector));
  1722. connector->funcs->destroy(connector);
  1723. }
  1724. /* setup interrupt handling */
  1725. tasklet_init(&disp->tasklet, nvd0_display_bh, (unsigned long)dev);
  1726. nouveau_irq_register(dev, 26, nvd0_display_intr);
  1727. /* small shared memory area we use for notifiers and semaphores */
  1728. ret = nouveau_bo_new(dev, 4096, 0x1000, TTM_PL_FLAG_VRAM,
  1729. 0, 0x0000, NULL, &disp->sync);
  1730. if (!ret) {
  1731. ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM);
  1732. if (!ret)
  1733. ret = nouveau_bo_map(disp->sync);
  1734. if (ret)
  1735. nouveau_bo_ref(NULL, &disp->sync);
  1736. }
  1737. if (ret)
  1738. goto out;
  1739. /* hash table and dma objects for the memory areas we care about */
  1740. ret = nouveau_gpuobj_new(dev, NULL, 0x4000, 0x10000,
  1741. NVOBJ_FLAG_ZERO_ALLOC, &disp->mem);
  1742. if (ret)
  1743. goto out;
  1744. /* create evo dma channels */
  1745. for (i = 0; i < EVO_DMA_NR; i++) {
  1746. struct evo *evo = &disp->evo[i];
  1747. u64 offset = disp->sync->bo.offset;
  1748. u32 dmao = 0x1000 + (i * 0x100);
  1749. u32 hash = 0x0000 + (i * 0x040);
  1750. evo->idx = i;
  1751. evo->sem.offset = EVO_SYNC(evo->idx, 0x00);
  1752. evo->ptr = pci_alloc_consistent(pdev, PAGE_SIZE, &evo->handle);
  1753. if (!evo->ptr) {
  1754. ret = -ENOMEM;
  1755. goto out;
  1756. }
  1757. nv_wo32(disp->mem, dmao + 0x00, 0x00000049);
  1758. nv_wo32(disp->mem, dmao + 0x04, (offset + 0x0000) >> 8);
  1759. nv_wo32(disp->mem, dmao + 0x08, (offset + 0x0fff) >> 8);
  1760. nv_wo32(disp->mem, dmao + 0x0c, 0x00000000);
  1761. nv_wo32(disp->mem, dmao + 0x10, 0x00000000);
  1762. nv_wo32(disp->mem, dmao + 0x14, 0x00000000);
  1763. nv_wo32(disp->mem, hash + 0x00, NvEvoSync);
  1764. nv_wo32(disp->mem, hash + 0x04, 0x00000001 | (i << 27) |
  1765. ((dmao + 0x00) << 9));
  1766. nv_wo32(disp->mem, dmao + 0x20, 0x00000049);
  1767. nv_wo32(disp->mem, dmao + 0x24, 0x00000000);
  1768. nv_wo32(disp->mem, dmao + 0x28, (dev_priv->vram_size - 1) >> 8);
  1769. nv_wo32(disp->mem, dmao + 0x2c, 0x00000000);
  1770. nv_wo32(disp->mem, dmao + 0x30, 0x00000000);
  1771. nv_wo32(disp->mem, dmao + 0x34, 0x00000000);
  1772. nv_wo32(disp->mem, hash + 0x08, NvEvoVRAM);
  1773. nv_wo32(disp->mem, hash + 0x0c, 0x00000001 | (i << 27) |
  1774. ((dmao + 0x20) << 9));
  1775. nv_wo32(disp->mem, dmao + 0x40, 0x00000009);
  1776. nv_wo32(disp->mem, dmao + 0x44, 0x00000000);
  1777. nv_wo32(disp->mem, dmao + 0x48, (dev_priv->vram_size - 1) >> 8);
  1778. nv_wo32(disp->mem, dmao + 0x4c, 0x00000000);
  1779. nv_wo32(disp->mem, dmao + 0x50, 0x00000000);
  1780. nv_wo32(disp->mem, dmao + 0x54, 0x00000000);
  1781. nv_wo32(disp->mem, hash + 0x10, NvEvoVRAM_LP);
  1782. nv_wo32(disp->mem, hash + 0x14, 0x00000001 | (i << 27) |
  1783. ((dmao + 0x40) << 9));
  1784. nv_wo32(disp->mem, dmao + 0x60, 0x0fe00009);
  1785. nv_wo32(disp->mem, dmao + 0x64, 0x00000000);
  1786. nv_wo32(disp->mem, dmao + 0x68, (dev_priv->vram_size - 1) >> 8);
  1787. nv_wo32(disp->mem, dmao + 0x6c, 0x00000000);
  1788. nv_wo32(disp->mem, dmao + 0x70, 0x00000000);
  1789. nv_wo32(disp->mem, dmao + 0x74, 0x00000000);
  1790. nv_wo32(disp->mem, hash + 0x18, NvEvoFB32);
  1791. nv_wo32(disp->mem, hash + 0x1c, 0x00000001 | (i << 27) |
  1792. ((dmao + 0x60) << 9));
  1793. }
  1794. pinstmem->flush(dev);
  1795. out:
  1796. if (ret)
  1797. nvd0_display_destroy(dev);
  1798. return ret;
  1799. }