nvd0_display.c 51 KB

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