nv04_crtc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * Copyright 1993-2003 NVIDIA, Corporation
  3. * Copyright 2006 Dave Airlie
  4. * Copyright 2007 Maarten Maathuis
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "drmP.h"
  26. #include "drm_crtc_helper.h"
  27. #include "nouveau_drv.h"
  28. #include "nouveau_encoder.h"
  29. #include "nouveau_connector.h"
  30. #include "nouveau_crtc.h"
  31. #include "nouveau_fb.h"
  32. #include "nouveau_hw.h"
  33. #include "nvreg.h"
  34. static int
  35. nv04_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  36. struct drm_framebuffer *old_fb);
  37. static void
  38. crtc_wr_cio_state(struct drm_crtc *crtc, struct nv04_crtc_reg *crtcstate, int index)
  39. {
  40. NVWriteVgaCrtc(crtc->dev, nouveau_crtc(crtc)->index, index,
  41. crtcstate->CRTC[index]);
  42. }
  43. static void nv_crtc_set_digital_vibrance(struct drm_crtc *crtc, int level)
  44. {
  45. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  46. struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
  47. struct nv04_crtc_reg *regp = &dev_priv->mode_reg.crtc_reg[nv_crtc->index];
  48. regp->CRTC[NV_CIO_CRE_CSB] = nv_crtc->saturation = level;
  49. if (nv_crtc->saturation && nv_gf4_disp_arch(crtc->dev)) {
  50. regp->CRTC[NV_CIO_CRE_CSB] = 0x80;
  51. regp->CRTC[NV_CIO_CRE_5B] = nv_crtc->saturation << 2;
  52. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_5B);
  53. }
  54. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_CSB);
  55. }
  56. static void nv_crtc_set_image_sharpening(struct drm_crtc *crtc, int level)
  57. {
  58. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  59. struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
  60. struct nv04_crtc_reg *regp = &dev_priv->mode_reg.crtc_reg[nv_crtc->index];
  61. nv_crtc->sharpness = level;
  62. if (level < 0) /* blur is in hw range 0x3f -> 0x20 */
  63. level += 0x40;
  64. regp->ramdac_634 = level;
  65. NVWriteRAMDAC(crtc->dev, nv_crtc->index, NV_PRAMDAC_634, regp->ramdac_634);
  66. }
  67. #define PLLSEL_VPLL1_MASK \
  68. (NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_VPLL \
  69. | NV_PRAMDAC_PLL_COEFF_SELECT_VCLK_RATIO_DB2)
  70. #define PLLSEL_VPLL2_MASK \
  71. (NV_PRAMDAC_PLL_COEFF_SELECT_PLL_SOURCE_VPLL2 \
  72. | NV_PRAMDAC_PLL_COEFF_SELECT_VCLK2_RATIO_DB2)
  73. #define PLLSEL_TV_MASK \
  74. (NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK1 \
  75. | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK1 \
  76. | NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK2 \
  77. | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK2)
  78. /* NV4x 0x40.. pll notes:
  79. * gpu pll: 0x4000 + 0x4004
  80. * ?gpu? pll: 0x4008 + 0x400c
  81. * vpll1: 0x4010 + 0x4014
  82. * vpll2: 0x4018 + 0x401c
  83. * mpll: 0x4020 + 0x4024
  84. * mpll: 0x4038 + 0x403c
  85. *
  86. * the first register of each pair has some unknown details:
  87. * bits 0-7: redirected values from elsewhere? (similar to PLL_SETUP_CONTROL?)
  88. * bits 20-23: (mpll) something to do with post divider?
  89. * bits 28-31: related to single stage mode? (bit 8/12)
  90. */
  91. static void nv_crtc_calc_state_ext(struct drm_crtc *crtc, struct drm_display_mode * mode, int dot_clock)
  92. {
  93. struct drm_device *dev = crtc->dev;
  94. struct drm_nouveau_private *dev_priv = dev->dev_private;
  95. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  96. struct nv04_mode_state *state = &dev_priv->mode_reg;
  97. struct nv04_crtc_reg *regp = &state->crtc_reg[nv_crtc->index];
  98. struct nouveau_pll_vals *pv = &regp->pllvals;
  99. struct pll_lims pll_lim;
  100. if (get_pll_limits(dev, nv_crtc->index ? VPLL2 : VPLL1, &pll_lim))
  101. return;
  102. /* NM2 == 0 is used to determine single stage mode on two stage plls */
  103. pv->NM2 = 0;
  104. /* for newer nv4x the blob uses only the first stage of the vpll below a
  105. * certain clock. for a certain nv4b this is 150MHz. since the max
  106. * output frequency of the first stage for this card is 300MHz, it is
  107. * assumed the threshold is given by vco1 maxfreq/2
  108. */
  109. /* for early nv4x, specifically nv40 and *some* nv43 (devids 0 and 6,
  110. * not 8, others unknown), the blob always uses both plls. no problem
  111. * has yet been observed in allowing the use a single stage pll on all
  112. * nv43 however. the behaviour of single stage use is untested on nv40
  113. */
  114. if (dev_priv->chipset > 0x40 && dot_clock <= (pll_lim.vco1.maxfreq / 2))
  115. memset(&pll_lim.vco2, 0, sizeof(pll_lim.vco2));
  116. if (!nouveau_calc_pll_mnp(dev, &pll_lim, dot_clock, pv))
  117. return;
  118. state->pllsel &= PLLSEL_VPLL1_MASK | PLLSEL_VPLL2_MASK | PLLSEL_TV_MASK;
  119. /* The blob uses this always, so let's do the same */
  120. if (dev_priv->card_type == NV_40)
  121. state->pllsel |= NV_PRAMDAC_PLL_COEFF_SELECT_USE_VPLL2_TRUE;
  122. /* again nv40 and some nv43 act more like nv3x as described above */
  123. if (dev_priv->chipset < 0x41)
  124. state->pllsel |= NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_MPLL |
  125. NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_NVPLL;
  126. state->pllsel |= nv_crtc->index ? PLLSEL_VPLL2_MASK : PLLSEL_VPLL1_MASK;
  127. if (pv->NM2)
  128. NV_DEBUG_KMS(dev, "vpll: n1 %d n2 %d m1 %d m2 %d log2p %d\n",
  129. pv->N1, pv->N2, pv->M1, pv->M2, pv->log2P);
  130. else
  131. NV_DEBUG_KMS(dev, "vpll: n %d m %d log2p %d\n",
  132. pv->N1, pv->M1, pv->log2P);
  133. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.offset);
  134. }
  135. static void
  136. nv_crtc_dpms(struct drm_crtc *crtc, int mode)
  137. {
  138. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  139. struct drm_device *dev = crtc->dev;
  140. unsigned char seq1 = 0, crtc17 = 0;
  141. unsigned char crtc1A;
  142. NV_DEBUG_KMS(dev, "Setting dpms mode %d on CRTC %d\n", mode,
  143. nv_crtc->index);
  144. if (nv_crtc->last_dpms == mode) /* Don't do unnecesary mode changes. */
  145. return;
  146. nv_crtc->last_dpms = mode;
  147. if (nv_two_heads(dev))
  148. NVSetOwner(dev, nv_crtc->index);
  149. /* nv4ref indicates these two RPC1 bits inhibit h/v sync */
  150. crtc1A = NVReadVgaCrtc(dev, nv_crtc->index,
  151. NV_CIO_CRE_RPC1_INDEX) & ~0xC0;
  152. switch (mode) {
  153. case DRM_MODE_DPMS_STANDBY:
  154. /* Screen: Off; HSync: Off, VSync: On -- Not Supported */
  155. seq1 = 0x20;
  156. crtc17 = 0x80;
  157. crtc1A |= 0x80;
  158. break;
  159. case DRM_MODE_DPMS_SUSPEND:
  160. /* Screen: Off; HSync: On, VSync: Off -- Not Supported */
  161. seq1 = 0x20;
  162. crtc17 = 0x80;
  163. crtc1A |= 0x40;
  164. break;
  165. case DRM_MODE_DPMS_OFF:
  166. /* Screen: Off; HSync: Off, VSync: Off */
  167. seq1 = 0x20;
  168. crtc17 = 0x00;
  169. crtc1A |= 0xC0;
  170. break;
  171. case DRM_MODE_DPMS_ON:
  172. default:
  173. /* Screen: On; HSync: On, VSync: On */
  174. seq1 = 0x00;
  175. crtc17 = 0x80;
  176. break;
  177. }
  178. NVVgaSeqReset(dev, nv_crtc->index, true);
  179. /* Each head has it's own sequencer, so we can turn it off when we want */
  180. seq1 |= (NVReadVgaSeq(dev, nv_crtc->index, NV_VIO_SR_CLOCK_INDEX) & ~0x20);
  181. NVWriteVgaSeq(dev, nv_crtc->index, NV_VIO_SR_CLOCK_INDEX, seq1);
  182. crtc17 |= (NVReadVgaCrtc(dev, nv_crtc->index, NV_CIO_CR_MODE_INDEX) & ~0x80);
  183. mdelay(10);
  184. NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CR_MODE_INDEX, crtc17);
  185. NVVgaSeqReset(dev, nv_crtc->index, false);
  186. NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RPC1_INDEX, crtc1A);
  187. }
  188. static bool
  189. nv_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
  190. struct drm_display_mode *adjusted_mode)
  191. {
  192. return true;
  193. }
  194. static void
  195. nv_crtc_mode_set_vga(struct drm_crtc *crtc, struct drm_display_mode *mode)
  196. {
  197. struct drm_device *dev = crtc->dev;
  198. struct drm_nouveau_private *dev_priv = dev->dev_private;
  199. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  200. struct nv04_crtc_reg *regp = &dev_priv->mode_reg.crtc_reg[nv_crtc->index];
  201. struct drm_framebuffer *fb = crtc->fb;
  202. /* Calculate our timings */
  203. int horizDisplay = (mode->crtc_hdisplay >> 3) - 1;
  204. int horizStart = (mode->crtc_hsync_start >> 3) - 1;
  205. int horizEnd = (mode->crtc_hsync_end >> 3) - 1;
  206. int horizTotal = (mode->crtc_htotal >> 3) - 5;
  207. int horizBlankStart = (mode->crtc_hdisplay >> 3) - 1;
  208. int horizBlankEnd = (mode->crtc_htotal >> 3) - 1;
  209. int vertDisplay = mode->crtc_vdisplay - 1;
  210. int vertStart = mode->crtc_vsync_start - 1;
  211. int vertEnd = mode->crtc_vsync_end - 1;
  212. int vertTotal = mode->crtc_vtotal - 2;
  213. int vertBlankStart = mode->crtc_vdisplay - 1;
  214. int vertBlankEnd = mode->crtc_vtotal - 1;
  215. struct drm_encoder *encoder;
  216. bool fp_output = false;
  217. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  218. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  219. if (encoder->crtc == crtc &&
  220. (nv_encoder->dcb->type == OUTPUT_LVDS ||
  221. nv_encoder->dcb->type == OUTPUT_TMDS))
  222. fp_output = true;
  223. }
  224. if (fp_output) {
  225. vertStart = vertTotal - 3;
  226. vertEnd = vertTotal - 2;
  227. vertBlankStart = vertStart;
  228. horizStart = horizTotal - 5;
  229. horizEnd = horizTotal - 2;
  230. horizBlankEnd = horizTotal + 4;
  231. #if 0
  232. if (dev->overlayAdaptor && dev_priv->card_type >= NV_10)
  233. /* This reportedly works around some video overlay bandwidth problems */
  234. horizTotal += 2;
  235. #endif
  236. }
  237. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  238. vertTotal |= 1;
  239. #if 0
  240. ErrorF("horizDisplay: 0x%X \n", horizDisplay);
  241. ErrorF("horizStart: 0x%X \n", horizStart);
  242. ErrorF("horizEnd: 0x%X \n", horizEnd);
  243. ErrorF("horizTotal: 0x%X \n", horizTotal);
  244. ErrorF("horizBlankStart: 0x%X \n", horizBlankStart);
  245. ErrorF("horizBlankEnd: 0x%X \n", horizBlankEnd);
  246. ErrorF("vertDisplay: 0x%X \n", vertDisplay);
  247. ErrorF("vertStart: 0x%X \n", vertStart);
  248. ErrorF("vertEnd: 0x%X \n", vertEnd);
  249. ErrorF("vertTotal: 0x%X \n", vertTotal);
  250. ErrorF("vertBlankStart: 0x%X \n", vertBlankStart);
  251. ErrorF("vertBlankEnd: 0x%X \n", vertBlankEnd);
  252. #endif
  253. /*
  254. * compute correct Hsync & Vsync polarity
  255. */
  256. if ((mode->flags & (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC))
  257. && (mode->flags & (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC))) {
  258. regp->MiscOutReg = 0x23;
  259. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  260. regp->MiscOutReg |= 0x40;
  261. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  262. regp->MiscOutReg |= 0x80;
  263. } else {
  264. int vdisplay = mode->vdisplay;
  265. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  266. vdisplay *= 2;
  267. if (mode->vscan > 1)
  268. vdisplay *= mode->vscan;
  269. if (vdisplay < 400)
  270. regp->MiscOutReg = 0xA3; /* +hsync -vsync */
  271. else if (vdisplay < 480)
  272. regp->MiscOutReg = 0x63; /* -hsync +vsync */
  273. else if (vdisplay < 768)
  274. regp->MiscOutReg = 0xE3; /* -hsync -vsync */
  275. else
  276. regp->MiscOutReg = 0x23; /* +hsync +vsync */
  277. }
  278. regp->MiscOutReg |= (mode->clock_index & 0x03) << 2;
  279. /*
  280. * Time Sequencer
  281. */
  282. regp->Sequencer[NV_VIO_SR_RESET_INDEX] = 0x00;
  283. /* 0x20 disables the sequencer */
  284. if (mode->flags & DRM_MODE_FLAG_CLKDIV2)
  285. regp->Sequencer[NV_VIO_SR_CLOCK_INDEX] = 0x29;
  286. else
  287. regp->Sequencer[NV_VIO_SR_CLOCK_INDEX] = 0x21;
  288. regp->Sequencer[NV_VIO_SR_PLANE_MASK_INDEX] = 0x0F;
  289. regp->Sequencer[NV_VIO_SR_CHAR_MAP_INDEX] = 0x00;
  290. regp->Sequencer[NV_VIO_SR_MEM_MODE_INDEX] = 0x0E;
  291. /*
  292. * CRTC
  293. */
  294. regp->CRTC[NV_CIO_CR_HDT_INDEX] = horizTotal;
  295. regp->CRTC[NV_CIO_CR_HDE_INDEX] = horizDisplay;
  296. regp->CRTC[NV_CIO_CR_HBS_INDEX] = horizBlankStart;
  297. regp->CRTC[NV_CIO_CR_HBE_INDEX] = (1 << 7) |
  298. XLATE(horizBlankEnd, 0, NV_CIO_CR_HBE_4_0);
  299. regp->CRTC[NV_CIO_CR_HRS_INDEX] = horizStart;
  300. regp->CRTC[NV_CIO_CR_HRE_INDEX] = XLATE(horizBlankEnd, 5, NV_CIO_CR_HRE_HBE_5) |
  301. XLATE(horizEnd, 0, NV_CIO_CR_HRE_4_0);
  302. regp->CRTC[NV_CIO_CR_VDT_INDEX] = vertTotal;
  303. regp->CRTC[NV_CIO_CR_OVL_INDEX] = XLATE(vertStart, 9, NV_CIO_CR_OVL_VRS_9) |
  304. XLATE(vertDisplay, 9, NV_CIO_CR_OVL_VDE_9) |
  305. XLATE(vertTotal, 9, NV_CIO_CR_OVL_VDT_9) |
  306. (1 << 4) |
  307. XLATE(vertBlankStart, 8, NV_CIO_CR_OVL_VBS_8) |
  308. XLATE(vertStart, 8, NV_CIO_CR_OVL_VRS_8) |
  309. XLATE(vertDisplay, 8, NV_CIO_CR_OVL_VDE_8) |
  310. XLATE(vertTotal, 8, NV_CIO_CR_OVL_VDT_8);
  311. regp->CRTC[NV_CIO_CR_RSAL_INDEX] = 0x00;
  312. regp->CRTC[NV_CIO_CR_CELL_HT_INDEX] = ((mode->flags & DRM_MODE_FLAG_DBLSCAN) ? MASK(NV_CIO_CR_CELL_HT_SCANDBL) : 0) |
  313. 1 << 6 |
  314. XLATE(vertBlankStart, 9, NV_CIO_CR_CELL_HT_VBS_9);
  315. regp->CRTC[NV_CIO_CR_CURS_ST_INDEX] = 0x00;
  316. regp->CRTC[NV_CIO_CR_CURS_END_INDEX] = 0x00;
  317. regp->CRTC[NV_CIO_CR_SA_HI_INDEX] = 0x00;
  318. regp->CRTC[NV_CIO_CR_SA_LO_INDEX] = 0x00;
  319. regp->CRTC[NV_CIO_CR_TCOFF_HI_INDEX] = 0x00;
  320. regp->CRTC[NV_CIO_CR_TCOFF_LO_INDEX] = 0x00;
  321. regp->CRTC[NV_CIO_CR_VRS_INDEX] = vertStart;
  322. regp->CRTC[NV_CIO_CR_VRE_INDEX] = 1 << 5 | XLATE(vertEnd, 0, NV_CIO_CR_VRE_3_0);
  323. regp->CRTC[NV_CIO_CR_VDE_INDEX] = vertDisplay;
  324. /* framebuffer can be larger than crtc scanout area. */
  325. regp->CRTC[NV_CIO_CR_OFFSET_INDEX] = fb->pitch / 8;
  326. regp->CRTC[NV_CIO_CR_ULINE_INDEX] = 0x00;
  327. regp->CRTC[NV_CIO_CR_VBS_INDEX] = vertBlankStart;
  328. regp->CRTC[NV_CIO_CR_VBE_INDEX] = vertBlankEnd;
  329. regp->CRTC[NV_CIO_CR_MODE_INDEX] = 0x43;
  330. regp->CRTC[NV_CIO_CR_LCOMP_INDEX] = 0xff;
  331. /*
  332. * Some extended CRTC registers (they are not saved with the rest of the vga regs).
  333. */
  334. /* framebuffer can be larger than crtc scanout area. */
  335. regp->CRTC[NV_CIO_CRE_RPC0_INDEX] = XLATE(fb->pitch / 8, 8, NV_CIO_CRE_RPC0_OFFSET_10_8);
  336. regp->CRTC[NV_CIO_CRE_RPC1_INDEX] = mode->crtc_hdisplay < 1280 ?
  337. MASK(NV_CIO_CRE_RPC1_LARGE) : 0x00;
  338. regp->CRTC[NV_CIO_CRE_LSR_INDEX] = XLATE(horizBlankEnd, 6, NV_CIO_CRE_LSR_HBE_6) |
  339. XLATE(vertBlankStart, 10, NV_CIO_CRE_LSR_VBS_10) |
  340. XLATE(vertStart, 10, NV_CIO_CRE_LSR_VRS_10) |
  341. XLATE(vertDisplay, 10, NV_CIO_CRE_LSR_VDE_10) |
  342. XLATE(vertTotal, 10, NV_CIO_CRE_LSR_VDT_10);
  343. regp->CRTC[NV_CIO_CRE_HEB__INDEX] = XLATE(horizStart, 8, NV_CIO_CRE_HEB_HRS_8) |
  344. XLATE(horizBlankStart, 8, NV_CIO_CRE_HEB_HBS_8) |
  345. XLATE(horizDisplay, 8, NV_CIO_CRE_HEB_HDE_8) |
  346. XLATE(horizTotal, 8, NV_CIO_CRE_HEB_HDT_8);
  347. regp->CRTC[NV_CIO_CRE_EBR_INDEX] = XLATE(vertBlankStart, 11, NV_CIO_CRE_EBR_VBS_11) |
  348. XLATE(vertStart, 11, NV_CIO_CRE_EBR_VRS_11) |
  349. XLATE(vertDisplay, 11, NV_CIO_CRE_EBR_VDE_11) |
  350. XLATE(vertTotal, 11, NV_CIO_CRE_EBR_VDT_11);
  351. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  352. horizTotal = (horizTotal >> 1) & ~1;
  353. regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = horizTotal;
  354. regp->CRTC[NV_CIO_CRE_HEB__INDEX] |= XLATE(horizTotal, 8, NV_CIO_CRE_HEB_ILC_8);
  355. } else
  356. regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = 0xff; /* interlace off */
  357. /*
  358. * Graphics Display Controller
  359. */
  360. regp->Graphics[NV_VIO_GX_SR_INDEX] = 0x00;
  361. regp->Graphics[NV_VIO_GX_SREN_INDEX] = 0x00;
  362. regp->Graphics[NV_VIO_GX_CCOMP_INDEX] = 0x00;
  363. regp->Graphics[NV_VIO_GX_ROP_INDEX] = 0x00;
  364. regp->Graphics[NV_VIO_GX_READ_MAP_INDEX] = 0x00;
  365. regp->Graphics[NV_VIO_GX_MODE_INDEX] = 0x40; /* 256 color mode */
  366. regp->Graphics[NV_VIO_GX_MISC_INDEX] = 0x05; /* map 64k mem + graphic mode */
  367. regp->Graphics[NV_VIO_GX_DONT_CARE_INDEX] = 0x0F;
  368. regp->Graphics[NV_VIO_GX_BIT_MASK_INDEX] = 0xFF;
  369. regp->Attribute[0] = 0x00; /* standard colormap translation */
  370. regp->Attribute[1] = 0x01;
  371. regp->Attribute[2] = 0x02;
  372. regp->Attribute[3] = 0x03;
  373. regp->Attribute[4] = 0x04;
  374. regp->Attribute[5] = 0x05;
  375. regp->Attribute[6] = 0x06;
  376. regp->Attribute[7] = 0x07;
  377. regp->Attribute[8] = 0x08;
  378. regp->Attribute[9] = 0x09;
  379. regp->Attribute[10] = 0x0A;
  380. regp->Attribute[11] = 0x0B;
  381. regp->Attribute[12] = 0x0C;
  382. regp->Attribute[13] = 0x0D;
  383. regp->Attribute[14] = 0x0E;
  384. regp->Attribute[15] = 0x0F;
  385. regp->Attribute[NV_CIO_AR_MODE_INDEX] = 0x01; /* Enable graphic mode */
  386. /* Non-vga */
  387. regp->Attribute[NV_CIO_AR_OSCAN_INDEX] = 0x00;
  388. regp->Attribute[NV_CIO_AR_PLANE_INDEX] = 0x0F; /* enable all color planes */
  389. regp->Attribute[NV_CIO_AR_HPP_INDEX] = 0x00;
  390. regp->Attribute[NV_CIO_AR_CSEL_INDEX] = 0x00;
  391. }
  392. /**
  393. * Sets up registers for the given mode/adjusted_mode pair.
  394. *
  395. * The clocks, CRTCs and outputs attached to this CRTC must be off.
  396. *
  397. * This shouldn't enable any clocks, CRTCs, or outputs, but they should
  398. * be easily turned on/off after this.
  399. */
  400. static void
  401. nv_crtc_mode_set_regs(struct drm_crtc *crtc, struct drm_display_mode * mode)
  402. {
  403. struct drm_device *dev = crtc->dev;
  404. struct drm_nouveau_private *dev_priv = dev->dev_private;
  405. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  406. struct nv04_crtc_reg *regp = &dev_priv->mode_reg.crtc_reg[nv_crtc->index];
  407. struct nv04_crtc_reg *savep = &dev_priv->saved_reg.crtc_reg[nv_crtc->index];
  408. struct drm_encoder *encoder;
  409. bool lvds_output = false, tmds_output = false, tv_output = false,
  410. off_chip_digital = false;
  411. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  412. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  413. bool digital = false;
  414. if (encoder->crtc != crtc)
  415. continue;
  416. if (nv_encoder->dcb->type == OUTPUT_LVDS)
  417. digital = lvds_output = true;
  418. if (nv_encoder->dcb->type == OUTPUT_TV)
  419. tv_output = true;
  420. if (nv_encoder->dcb->type == OUTPUT_TMDS)
  421. digital = tmds_output = true;
  422. if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP && digital)
  423. off_chip_digital = true;
  424. }
  425. /* Registers not directly related to the (s)vga mode */
  426. /* What is the meaning of this register? */
  427. /* A few popular values are 0x18, 0x1c, 0x38, 0x3c */
  428. regp->CRTC[NV_CIO_CRE_ENH_INDEX] = savep->CRTC[NV_CIO_CRE_ENH_INDEX] & ~(1<<5);
  429. regp->crtc_eng_ctrl = 0;
  430. /* Except for rare conditions I2C is enabled on the primary crtc */
  431. if (nv_crtc->index == 0)
  432. regp->crtc_eng_ctrl |= NV_CRTC_FSEL_I2C;
  433. #if 0
  434. /* Set overlay to desired crtc. */
  435. if (dev->overlayAdaptor) {
  436. NVPortPrivPtr pPriv = GET_OVERLAY_PRIVATE(dev);
  437. if (pPriv->overlayCRTC == nv_crtc->index)
  438. regp->crtc_eng_ctrl |= NV_CRTC_FSEL_OVERLAY;
  439. }
  440. #endif
  441. /* ADDRESS_SPACE_PNVM is the same as setting HCUR_ASI */
  442. regp->cursor_cfg = NV_PCRTC_CURSOR_CONFIG_CUR_LINES_64 |
  443. NV_PCRTC_CURSOR_CONFIG_CUR_PIXELS_64 |
  444. NV_PCRTC_CURSOR_CONFIG_ADDRESS_SPACE_PNVM;
  445. if (dev_priv->chipset >= 0x11)
  446. regp->cursor_cfg |= NV_PCRTC_CURSOR_CONFIG_CUR_BPP_32;
  447. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  448. regp->cursor_cfg |= NV_PCRTC_CURSOR_CONFIG_DOUBLE_SCAN_ENABLE;
  449. /* Unblock some timings */
  450. regp->CRTC[NV_CIO_CRE_53] = 0;
  451. regp->CRTC[NV_CIO_CRE_54] = 0;
  452. /* 0x00 is disabled, 0x11 is lvds, 0x22 crt and 0x88 tmds */
  453. if (lvds_output)
  454. regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x11;
  455. else if (tmds_output)
  456. regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x88;
  457. else
  458. regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x22;
  459. /* These values seem to vary */
  460. /* This register seems to be used by the bios to make certain decisions on some G70 cards? */
  461. regp->CRTC[NV_CIO_CRE_SCRATCH4__INDEX] = savep->CRTC[NV_CIO_CRE_SCRATCH4__INDEX];
  462. nv_crtc_set_digital_vibrance(crtc, nv_crtc->saturation);
  463. /* probably a scratch reg, but kept for cargo-cult purposes:
  464. * bit0: crtc0?, head A
  465. * bit6: lvds, head A
  466. * bit7: (only in X), head A
  467. */
  468. if (nv_crtc->index == 0)
  469. regp->CRTC[NV_CIO_CRE_4B] = savep->CRTC[NV_CIO_CRE_4B] | 0x80;
  470. /* The blob seems to take the current value from crtc 0, add 4 to that
  471. * and reuse the old value for crtc 1 */
  472. regp->CRTC[NV_CIO_CRE_TVOUT_LATENCY] = dev_priv->saved_reg.crtc_reg[0].CRTC[NV_CIO_CRE_TVOUT_LATENCY];
  473. if (!nv_crtc->index)
  474. regp->CRTC[NV_CIO_CRE_TVOUT_LATENCY] += 4;
  475. /* the blob sometimes sets |= 0x10 (which is the same as setting |=
  476. * 1 << 30 on 0x60.830), for no apparent reason */
  477. regp->CRTC[NV_CIO_CRE_59] = off_chip_digital;
  478. regp->crtc_830 = mode->crtc_vdisplay - 3;
  479. regp->crtc_834 = mode->crtc_vdisplay - 1;
  480. if (dev_priv->card_type == NV_40)
  481. /* This is what the blob does */
  482. regp->crtc_850 = NVReadCRTC(dev, 0, NV_PCRTC_850);
  483. if (dev_priv->card_type >= NV_30)
  484. regp->gpio_ext = NVReadCRTC(dev, 0, NV_PCRTC_GPIO_EXT);
  485. regp->crtc_cfg = NV_PCRTC_CONFIG_START_ADDRESS_HSYNC;
  486. /* Some misc regs */
  487. if (dev_priv->card_type == NV_40) {
  488. regp->CRTC[NV_CIO_CRE_85] = 0xFF;
  489. regp->CRTC[NV_CIO_CRE_86] = 0x1;
  490. }
  491. regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] = (crtc->fb->depth + 1) / 8;
  492. /* Enable slaved mode (called MODE_TV in nv4ref.h) */
  493. if (lvds_output || tmds_output || tv_output)
  494. regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (1 << 7);
  495. /* Generic PRAMDAC regs */
  496. if (dev_priv->card_type >= NV_10)
  497. /* Only bit that bios and blob set. */
  498. regp->nv10_cursync = (1 << 25);
  499. regp->ramdac_gen_ctrl = NV_PRAMDAC_GENERAL_CONTROL_BPC_8BITS |
  500. NV_PRAMDAC_GENERAL_CONTROL_VGA_STATE_SEL |
  501. NV_PRAMDAC_GENERAL_CONTROL_PIXMIX_ON;
  502. if (crtc->fb->depth == 16)
  503. regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
  504. if (dev_priv->chipset >= 0x11)
  505. regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_PIPE_LONG;
  506. regp->ramdac_630 = 0; /* turn off green mode (tv test pattern?) */
  507. regp->tv_setup = 0;
  508. nv_crtc_set_image_sharpening(crtc, nv_crtc->sharpness);
  509. /* Some values the blob sets */
  510. regp->ramdac_8c0 = 0x100;
  511. regp->ramdac_a20 = 0x0;
  512. regp->ramdac_a24 = 0xfffff;
  513. regp->ramdac_a34 = 0x1;
  514. }
  515. /**
  516. * Sets up registers for the given mode/adjusted_mode pair.
  517. *
  518. * The clocks, CRTCs and outputs attached to this CRTC must be off.
  519. *
  520. * This shouldn't enable any clocks, CRTCs, or outputs, but they should
  521. * be easily turned on/off after this.
  522. */
  523. static int
  524. nv_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  525. struct drm_display_mode *adjusted_mode,
  526. int x, int y, struct drm_framebuffer *old_fb)
  527. {
  528. struct drm_device *dev = crtc->dev;
  529. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  530. struct drm_nouveau_private *dev_priv = dev->dev_private;
  531. NV_DEBUG_KMS(dev, "CTRC mode on CRTC %d:\n", nv_crtc->index);
  532. drm_mode_debug_printmodeline(adjusted_mode);
  533. /* unlock must come after turning off FP_TG_CONTROL in output_prepare */
  534. nv_lock_vga_crtc_shadow(dev, nv_crtc->index, -1);
  535. nv_crtc_mode_set_vga(crtc, adjusted_mode);
  536. /* calculated in nv04_dfp_prepare, nv40 needs it written before calculating PLLs */
  537. if (dev_priv->card_type == NV_40)
  538. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, dev_priv->mode_reg.sel_clk);
  539. nv_crtc_mode_set_regs(crtc, adjusted_mode);
  540. nv_crtc_calc_state_ext(crtc, mode, adjusted_mode->clock);
  541. return 0;
  542. }
  543. static void nv_crtc_save(struct drm_crtc *crtc)
  544. {
  545. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  546. struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
  547. struct nv04_mode_state *state = &dev_priv->mode_reg;
  548. struct nv04_crtc_reg *crtc_state = &state->crtc_reg[nv_crtc->index];
  549. struct nv04_mode_state *saved = &dev_priv->saved_reg;
  550. struct nv04_crtc_reg *crtc_saved = &saved->crtc_reg[nv_crtc->index];
  551. if (nv_two_heads(crtc->dev))
  552. NVSetOwner(crtc->dev, nv_crtc->index);
  553. nouveau_hw_save_state(crtc->dev, nv_crtc->index, saved);
  554. /* init some state to saved value */
  555. state->sel_clk = saved->sel_clk & ~(0x5 << 16);
  556. crtc_state->CRTC[NV_CIO_CRE_LCD__INDEX] = crtc_saved->CRTC[NV_CIO_CRE_LCD__INDEX];
  557. state->pllsel = saved->pllsel & ~(PLLSEL_VPLL1_MASK | PLLSEL_VPLL2_MASK | PLLSEL_TV_MASK);
  558. crtc_state->gpio_ext = crtc_saved->gpio_ext;
  559. }
  560. static void nv_crtc_restore(struct drm_crtc *crtc)
  561. {
  562. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  563. struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
  564. int head = nv_crtc->index;
  565. uint8_t saved_cr21 = dev_priv->saved_reg.crtc_reg[head].CRTC[NV_CIO_CRE_21];
  566. if (nv_two_heads(crtc->dev))
  567. NVSetOwner(crtc->dev, head);
  568. nouveau_hw_load_state(crtc->dev, head, &dev_priv->saved_reg);
  569. nv_lock_vga_crtc_shadow(crtc->dev, head, saved_cr21);
  570. nv_crtc->last_dpms = NV_DPMS_CLEARED;
  571. }
  572. static void nv_crtc_prepare(struct drm_crtc *crtc)
  573. {
  574. struct drm_device *dev = crtc->dev;
  575. struct drm_nouveau_private *dev_priv = dev->dev_private;
  576. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  577. struct drm_crtc_helper_funcs *funcs = crtc->helper_private;
  578. if (nv_two_heads(dev))
  579. NVSetOwner(dev, nv_crtc->index);
  580. funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  581. NVBlankScreen(dev, nv_crtc->index, true);
  582. /* Some more preperation. */
  583. NVWriteCRTC(dev, nv_crtc->index, NV_PCRTC_CONFIG, NV_PCRTC_CONFIG_START_ADDRESS_NON_VGA);
  584. if (dev_priv->card_type == NV_40) {
  585. uint32_t reg900 = NVReadRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_900);
  586. NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_900, reg900 & ~0x10000);
  587. }
  588. }
  589. static void nv_crtc_commit(struct drm_crtc *crtc)
  590. {
  591. struct drm_device *dev = crtc->dev;
  592. struct drm_crtc_helper_funcs *funcs = crtc->helper_private;
  593. struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
  594. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  595. nouveau_hw_load_state(dev, nv_crtc->index, &dev_priv->mode_reg);
  596. nv04_crtc_mode_set_base(crtc, crtc->x, crtc->y, NULL);
  597. #ifdef __BIG_ENDIAN
  598. /* turn on LFB swapping */
  599. {
  600. uint8_t tmp = NVReadVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RCR);
  601. tmp |= MASK(NV_CIO_CRE_RCR_ENDIAN_BIG);
  602. NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RCR, tmp);
  603. }
  604. #endif
  605. funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  606. }
  607. static void nv_crtc_destroy(struct drm_crtc *crtc)
  608. {
  609. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  610. NV_DEBUG_KMS(crtc->dev, "\n");
  611. if (!nv_crtc)
  612. return;
  613. drm_crtc_cleanup(crtc);
  614. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  615. kfree(nv_crtc);
  616. }
  617. static void
  618. nv_crtc_gamma_load(struct drm_crtc *crtc)
  619. {
  620. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  621. struct drm_device *dev = nv_crtc->base.dev;
  622. struct drm_nouveau_private *dev_priv = dev->dev_private;
  623. struct rgb { uint8_t r, g, b; } __attribute__((packed)) *rgbs;
  624. int i;
  625. rgbs = (struct rgb *)dev_priv->mode_reg.crtc_reg[nv_crtc->index].DAC;
  626. for (i = 0; i < 256; i++) {
  627. rgbs[i].r = nv_crtc->lut.r[i] >> 8;
  628. rgbs[i].g = nv_crtc->lut.g[i] >> 8;
  629. rgbs[i].b = nv_crtc->lut.b[i] >> 8;
  630. }
  631. nouveau_hw_load_state_palette(dev, nv_crtc->index, &dev_priv->mode_reg);
  632. }
  633. static void
  634. nv_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b, uint32_t size)
  635. {
  636. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  637. int i;
  638. if (size != 256)
  639. return;
  640. for (i = 0; i < 256; i++) {
  641. nv_crtc->lut.r[i] = r[i];
  642. nv_crtc->lut.g[i] = g[i];
  643. nv_crtc->lut.b[i] = b[i];
  644. }
  645. /* We need to know the depth before we upload, but it's possible to
  646. * get called before a framebuffer is bound. If this is the case,
  647. * mark the lut values as dirty by setting depth==0, and it'll be
  648. * uploaded on the first mode_set_base()
  649. */
  650. if (!nv_crtc->base.fb) {
  651. nv_crtc->lut.depth = 0;
  652. return;
  653. }
  654. nv_crtc_gamma_load(crtc);
  655. }
  656. static int
  657. nv04_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  658. struct drm_framebuffer *old_fb)
  659. {
  660. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  661. struct drm_device *dev = crtc->dev;
  662. struct drm_nouveau_private *dev_priv = dev->dev_private;
  663. struct nv04_crtc_reg *regp = &dev_priv->mode_reg.crtc_reg[nv_crtc->index];
  664. struct drm_framebuffer *drm_fb = nv_crtc->base.fb;
  665. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  666. int arb_burst, arb_lwm;
  667. int ret;
  668. ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM);
  669. if (ret)
  670. return ret;
  671. if (old_fb) {
  672. struct nouveau_framebuffer *ofb = nouveau_framebuffer(old_fb);
  673. nouveau_bo_unpin(ofb->nvbo);
  674. }
  675. nv_crtc->fb.offset = fb->nvbo->bo.offset;
  676. if (nv_crtc->lut.depth != drm_fb->depth) {
  677. nv_crtc->lut.depth = drm_fb->depth;
  678. nv_crtc_gamma_load(crtc);
  679. }
  680. /* Update the framebuffer format. */
  681. regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] &= ~3;
  682. regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (crtc->fb->depth + 1) / 8;
  683. regp->ramdac_gen_ctrl &= ~NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
  684. if (crtc->fb->depth == 16)
  685. regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
  686. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_PIXEL_INDEX);
  687. NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_GENERAL_CONTROL,
  688. regp->ramdac_gen_ctrl);
  689. regp->CRTC[NV_CIO_CR_OFFSET_INDEX] = drm_fb->pitch >> 3;
  690. regp->CRTC[NV_CIO_CRE_RPC0_INDEX] =
  691. XLATE(drm_fb->pitch >> 3, 8, NV_CIO_CRE_RPC0_OFFSET_10_8);
  692. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_RPC0_INDEX);
  693. crtc_wr_cio_state(crtc, regp, NV_CIO_CR_OFFSET_INDEX);
  694. /* Update the framebuffer location. */
  695. regp->fb_start = nv_crtc->fb.offset & ~3;
  696. regp->fb_start += (y * drm_fb->pitch) + (x * drm_fb->bits_per_pixel / 8);
  697. NVWriteCRTC(dev, nv_crtc->index, NV_PCRTC_START, regp->fb_start);
  698. /* Update the arbitration parameters. */
  699. nouveau_calc_arb(dev, crtc->mode.clock, drm_fb->bits_per_pixel,
  700. &arb_burst, &arb_lwm);
  701. regp->CRTC[NV_CIO_CRE_FF_INDEX] = arb_burst;
  702. regp->CRTC[NV_CIO_CRE_FFLWM__INDEX] = arb_lwm & 0xff;
  703. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_FF_INDEX);
  704. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_FFLWM__INDEX);
  705. if (dev_priv->card_type >= NV_30) {
  706. regp->CRTC[NV_CIO_CRE_47] = arb_lwm >> 8;
  707. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_47);
  708. }
  709. return 0;
  710. }
  711. static void nv04_cursor_upload(struct drm_device *dev, struct nouveau_bo *src,
  712. struct nouveau_bo *dst)
  713. {
  714. int width = nv_cursor_width(dev);
  715. uint32_t pixel;
  716. int i, j;
  717. for (i = 0; i < width; i++) {
  718. for (j = 0; j < width; j++) {
  719. pixel = nouveau_bo_rd32(src, i*64 + j);
  720. nouveau_bo_wr16(dst, i*width + j, (pixel & 0x80000000) >> 16
  721. | (pixel & 0xf80000) >> 9
  722. | (pixel & 0xf800) >> 6
  723. | (pixel & 0xf8) >> 3);
  724. }
  725. }
  726. }
  727. static void nv11_cursor_upload(struct drm_device *dev, struct nouveau_bo *src,
  728. struct nouveau_bo *dst)
  729. {
  730. uint32_t pixel;
  731. int alpha, i;
  732. /* nv11+ supports premultiplied (PM), or non-premultiplied (NPM) alpha
  733. * cursors (though NPM in combination with fp dithering may not work on
  734. * nv11, from "nv" driver history)
  735. * NPM mode needs NV_PCRTC_CURSOR_CONFIG_ALPHA_BLEND set and is what the
  736. * blob uses, however we get given PM cursors so we use PM mode
  737. */
  738. for (i = 0; i < 64 * 64; i++) {
  739. pixel = nouveau_bo_rd32(src, i);
  740. /* hw gets unhappy if alpha <= rgb values. for a PM image "less
  741. * than" shouldn't happen; fix "equal to" case by adding one to
  742. * alpha channel (slightly inaccurate, but so is attempting to
  743. * get back to NPM images, due to limits of integer precision)
  744. */
  745. alpha = pixel >> 24;
  746. if (alpha > 0 && alpha < 255)
  747. pixel = (pixel & 0x00ffffff) | ((alpha + 1) << 24);
  748. #ifdef __BIG_ENDIAN
  749. {
  750. struct drm_nouveau_private *dev_priv = dev->dev_private;
  751. if (dev_priv->chipset == 0x11) {
  752. pixel = ((pixel & 0x000000ff) << 24) |
  753. ((pixel & 0x0000ff00) << 8) |
  754. ((pixel & 0x00ff0000) >> 8) |
  755. ((pixel & 0xff000000) >> 24);
  756. }
  757. }
  758. #endif
  759. nouveau_bo_wr32(dst, i, pixel);
  760. }
  761. }
  762. static int
  763. nv04_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  764. uint32_t buffer_handle, uint32_t width, uint32_t height)
  765. {
  766. struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
  767. struct drm_device *dev = dev_priv->dev;
  768. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  769. struct nouveau_bo *cursor = NULL;
  770. struct drm_gem_object *gem;
  771. int ret = 0;
  772. if (width != 64 || height != 64)
  773. return -EINVAL;
  774. if (!buffer_handle) {
  775. nv_crtc->cursor.hide(nv_crtc, true);
  776. return 0;
  777. }
  778. gem = drm_gem_object_lookup(dev, file_priv, buffer_handle);
  779. if (!gem)
  780. return -EINVAL;
  781. cursor = nouveau_gem_object(gem);
  782. ret = nouveau_bo_map(cursor);
  783. if (ret)
  784. goto out;
  785. if (dev_priv->chipset >= 0x11)
  786. nv11_cursor_upload(dev, cursor, nv_crtc->cursor.nvbo);
  787. else
  788. nv04_cursor_upload(dev, cursor, nv_crtc->cursor.nvbo);
  789. nouveau_bo_unmap(cursor);
  790. nv_crtc->cursor.offset = nv_crtc->cursor.nvbo->bo.offset;
  791. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.offset);
  792. nv_crtc->cursor.show(nv_crtc, true);
  793. out:
  794. mutex_lock(&dev->struct_mutex);
  795. drm_gem_object_unreference(gem);
  796. mutex_unlock(&dev->struct_mutex);
  797. return ret;
  798. }
  799. static int
  800. nv04_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  801. {
  802. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  803. nv_crtc->cursor.set_pos(nv_crtc, x, y);
  804. return 0;
  805. }
  806. static const struct drm_crtc_funcs nv04_crtc_funcs = {
  807. .save = nv_crtc_save,
  808. .restore = nv_crtc_restore,
  809. .cursor_set = nv04_crtc_cursor_set,
  810. .cursor_move = nv04_crtc_cursor_move,
  811. .gamma_set = nv_crtc_gamma_set,
  812. .set_config = drm_crtc_helper_set_config,
  813. .destroy = nv_crtc_destroy,
  814. };
  815. static const struct drm_crtc_helper_funcs nv04_crtc_helper_funcs = {
  816. .dpms = nv_crtc_dpms,
  817. .prepare = nv_crtc_prepare,
  818. .commit = nv_crtc_commit,
  819. .mode_fixup = nv_crtc_mode_fixup,
  820. .mode_set = nv_crtc_mode_set,
  821. .mode_set_base = nv04_crtc_mode_set_base,
  822. .load_lut = nv_crtc_gamma_load,
  823. };
  824. int
  825. nv04_crtc_create(struct drm_device *dev, int crtc_num)
  826. {
  827. struct nouveau_crtc *nv_crtc;
  828. int ret, i;
  829. nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
  830. if (!nv_crtc)
  831. return -ENOMEM;
  832. for (i = 0; i < 256; i++) {
  833. nv_crtc->lut.r[i] = i << 8;
  834. nv_crtc->lut.g[i] = i << 8;
  835. nv_crtc->lut.b[i] = i << 8;
  836. }
  837. nv_crtc->lut.depth = 0;
  838. nv_crtc->index = crtc_num;
  839. nv_crtc->last_dpms = NV_DPMS_CLEARED;
  840. drm_crtc_init(dev, &nv_crtc->base, &nv04_crtc_funcs);
  841. drm_crtc_helper_add(&nv_crtc->base, &nv04_crtc_helper_funcs);
  842. drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
  843. ret = nouveau_bo_new(dev, NULL, 64*64*4, 0x100, TTM_PL_FLAG_VRAM,
  844. 0, 0x0000, false, true, &nv_crtc->cursor.nvbo);
  845. if (!ret) {
  846. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
  847. if (!ret)
  848. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  849. if (ret)
  850. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  851. }
  852. nv04_cursor_init(nv_crtc);
  853. return 0;
  854. }