nv04_crtc.c 33 KB

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