nv04_crtc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. #include "nouveau_fbcon.h"
  35. static int
  36. nv04_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  37. struct drm_framebuffer *old_fb);
  38. static void
  39. crtc_wr_cio_state(struct drm_crtc *crtc, struct nv04_crtc_reg *crtcstate, int index)
  40. {
  41. NVWriteVgaCrtc(crtc->dev, nouveau_crtc(crtc)->index, index,
  42. crtcstate->CRTC[index]);
  43. }
  44. static void nv_crtc_set_digital_vibrance(struct drm_crtc *crtc, int level)
  45. {
  46. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  47. struct drm_device *dev = crtc->dev;
  48. struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
  49. regp->CRTC[NV_CIO_CRE_CSB] = nv_crtc->saturation = level;
  50. if (nv_crtc->saturation && nv_gf4_disp_arch(crtc->dev)) {
  51. regp->CRTC[NV_CIO_CRE_CSB] = 0x80;
  52. regp->CRTC[NV_CIO_CRE_5B] = nv_crtc->saturation << 2;
  53. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_5B);
  54. }
  55. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_CSB);
  56. }
  57. static void nv_crtc_set_image_sharpening(struct drm_crtc *crtc, int level)
  58. {
  59. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  60. struct drm_device *dev = crtc->dev;
  61. struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
  62. nv_crtc->sharpness = level;
  63. if (level < 0) /* blur is in hw range 0x3f -> 0x20 */
  64. level += 0x40;
  65. regp->ramdac_634 = level;
  66. NVWriteRAMDAC(crtc->dev, nv_crtc->index, NV_PRAMDAC_634, regp->ramdac_634);
  67. }
  68. #define PLLSEL_VPLL1_MASK \
  69. (NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_VPLL \
  70. | NV_PRAMDAC_PLL_COEFF_SELECT_VCLK_RATIO_DB2)
  71. #define PLLSEL_VPLL2_MASK \
  72. (NV_PRAMDAC_PLL_COEFF_SELECT_PLL_SOURCE_VPLL2 \
  73. | NV_PRAMDAC_PLL_COEFF_SELECT_VCLK2_RATIO_DB2)
  74. #define PLLSEL_TV_MASK \
  75. (NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK1 \
  76. | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK1 \
  77. | NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK2 \
  78. | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK2)
  79. /* NV4x 0x40.. pll notes:
  80. * gpu pll: 0x4000 + 0x4004
  81. * ?gpu? pll: 0x4008 + 0x400c
  82. * vpll1: 0x4010 + 0x4014
  83. * vpll2: 0x4018 + 0x401c
  84. * mpll: 0x4020 + 0x4024
  85. * mpll: 0x4038 + 0x403c
  86. *
  87. * the first register of each pair has some unknown details:
  88. * bits 0-7: redirected values from elsewhere? (similar to PLL_SETUP_CONTROL?)
  89. * bits 20-23: (mpll) something to do with post divider?
  90. * bits 28-31: related to single stage mode? (bit 8/12)
  91. */
  92. static void nv_crtc_calc_state_ext(struct drm_crtc *crtc, struct drm_display_mode * mode, int dot_clock)
  93. {
  94. struct drm_device *dev = crtc->dev;
  95. struct drm_nouveau_private *dev_priv = dev->dev_private;
  96. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  97. struct nv04_mode_state *state = &nv04_display(dev)->mode_reg;
  98. struct nv04_crtc_reg *regp = &state->crtc_reg[nv_crtc->index];
  99. struct nouveau_pll_vals *pv = &regp->pllvals;
  100. struct nvbios_pll pll_lim;
  101. if (get_pll_limits(dev, nv_crtc->index ? PLL_VPLL1 : PLL_VPLL0, &pll_lim))
  102. return;
  103. /* NM2 == 0 is used to determine single stage mode on two stage plls */
  104. pv->NM2 = 0;
  105. /* for newer nv4x the blob uses only the first stage of the vpll below a
  106. * certain clock. for a certain nv4b this is 150MHz. since the max
  107. * output frequency of the first stage for this card is 300MHz, it is
  108. * assumed the threshold is given by vco1 maxfreq/2
  109. */
  110. /* for early nv4x, specifically nv40 and *some* nv43 (devids 0 and 6,
  111. * not 8, others unknown), the blob always uses both plls. no problem
  112. * has yet been observed in allowing the use a single stage pll on all
  113. * nv43 however. the behaviour of single stage use is untested on nv40
  114. */
  115. if (dev_priv->chipset > 0x40 && dot_clock <= (pll_lim.vco1.max_freq / 2))
  116. memset(&pll_lim.vco2, 0, sizeof(pll_lim.vco2));
  117. if (!nouveau_calc_pll_mnp(dev, &pll_lim, dot_clock, pv))
  118. return;
  119. state->pllsel &= PLLSEL_VPLL1_MASK | PLLSEL_VPLL2_MASK | PLLSEL_TV_MASK;
  120. /* The blob uses this always, so let's do the same */
  121. if (dev_priv->card_type == NV_40)
  122. state->pllsel |= NV_PRAMDAC_PLL_COEFF_SELECT_USE_VPLL2_TRUE;
  123. /* again nv40 and some nv43 act more like nv3x as described above */
  124. if (dev_priv->chipset < 0x41)
  125. state->pllsel |= NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_MPLL |
  126. NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_NVPLL;
  127. state->pllsel |= nv_crtc->index ? PLLSEL_VPLL2_MASK : PLLSEL_VPLL1_MASK;
  128. if (pv->NM2)
  129. NV_DEBUG_KMS(dev, "vpll: n1 %d n2 %d m1 %d m2 %d log2p %d\n",
  130. pv->N1, pv->N2, pv->M1, pv->M2, pv->log2P);
  131. else
  132. NV_DEBUG_KMS(dev, "vpll: n %d m %d log2p %d\n",
  133. pv->N1, pv->M1, pv->log2P);
  134. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.offset);
  135. }
  136. static void
  137. nv_crtc_dpms(struct drm_crtc *crtc, int mode)
  138. {
  139. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  140. struct drm_device *dev = crtc->dev;
  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 unnecessary 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. }
  189. static bool
  190. nv_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
  191. struct drm_display_mode *adjusted_mode)
  192. {
  193. return true;
  194. }
  195. static void
  196. nv_crtc_mode_set_vga(struct drm_crtc *crtc, struct drm_display_mode *mode)
  197. {
  198. struct drm_device *dev = crtc->dev;
  199. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  200. struct nv04_crtc_reg *regp = &nv04_display(dev)->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 == DCB_OUTPUT_LVDS ||
  221. nv_encoder->dcb->type == DCB_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->pitches[0] / 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] =
  336. XLATE(fb->pitches[0] / 8, 8, NV_CIO_CRE_RPC0_OFFSET_10_8);
  337. regp->CRTC[NV_CIO_CRE_42] =
  338. XLATE(fb->pitches[0] / 8, 11, NV_CIO_CRE_42_OFFSET_11);
  339. regp->CRTC[NV_CIO_CRE_RPC1_INDEX] = mode->crtc_hdisplay < 1280 ?
  340. MASK(NV_CIO_CRE_RPC1_LARGE) : 0x00;
  341. regp->CRTC[NV_CIO_CRE_LSR_INDEX] = XLATE(horizBlankEnd, 6, NV_CIO_CRE_LSR_HBE_6) |
  342. XLATE(vertBlankStart, 10, NV_CIO_CRE_LSR_VBS_10) |
  343. XLATE(vertStart, 10, NV_CIO_CRE_LSR_VRS_10) |
  344. XLATE(vertDisplay, 10, NV_CIO_CRE_LSR_VDE_10) |
  345. XLATE(vertTotal, 10, NV_CIO_CRE_LSR_VDT_10);
  346. regp->CRTC[NV_CIO_CRE_HEB__INDEX] = XLATE(horizStart, 8, NV_CIO_CRE_HEB_HRS_8) |
  347. XLATE(horizBlankStart, 8, NV_CIO_CRE_HEB_HBS_8) |
  348. XLATE(horizDisplay, 8, NV_CIO_CRE_HEB_HDE_8) |
  349. XLATE(horizTotal, 8, NV_CIO_CRE_HEB_HDT_8);
  350. regp->CRTC[NV_CIO_CRE_EBR_INDEX] = XLATE(vertBlankStart, 11, NV_CIO_CRE_EBR_VBS_11) |
  351. XLATE(vertStart, 11, NV_CIO_CRE_EBR_VRS_11) |
  352. XLATE(vertDisplay, 11, NV_CIO_CRE_EBR_VDE_11) |
  353. XLATE(vertTotal, 11, NV_CIO_CRE_EBR_VDT_11);
  354. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  355. horizTotal = (horizTotal >> 1) & ~1;
  356. regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = horizTotal;
  357. regp->CRTC[NV_CIO_CRE_HEB__INDEX] |= XLATE(horizTotal, 8, NV_CIO_CRE_HEB_ILC_8);
  358. } else
  359. regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = 0xff; /* interlace off */
  360. /*
  361. * Graphics Display Controller
  362. */
  363. regp->Graphics[NV_VIO_GX_SR_INDEX] = 0x00;
  364. regp->Graphics[NV_VIO_GX_SREN_INDEX] = 0x00;
  365. regp->Graphics[NV_VIO_GX_CCOMP_INDEX] = 0x00;
  366. regp->Graphics[NV_VIO_GX_ROP_INDEX] = 0x00;
  367. regp->Graphics[NV_VIO_GX_READ_MAP_INDEX] = 0x00;
  368. regp->Graphics[NV_VIO_GX_MODE_INDEX] = 0x40; /* 256 color mode */
  369. regp->Graphics[NV_VIO_GX_MISC_INDEX] = 0x05; /* map 64k mem + graphic mode */
  370. regp->Graphics[NV_VIO_GX_DONT_CARE_INDEX] = 0x0F;
  371. regp->Graphics[NV_VIO_GX_BIT_MASK_INDEX] = 0xFF;
  372. regp->Attribute[0] = 0x00; /* standard colormap translation */
  373. regp->Attribute[1] = 0x01;
  374. regp->Attribute[2] = 0x02;
  375. regp->Attribute[3] = 0x03;
  376. regp->Attribute[4] = 0x04;
  377. regp->Attribute[5] = 0x05;
  378. regp->Attribute[6] = 0x06;
  379. regp->Attribute[7] = 0x07;
  380. regp->Attribute[8] = 0x08;
  381. regp->Attribute[9] = 0x09;
  382. regp->Attribute[10] = 0x0A;
  383. regp->Attribute[11] = 0x0B;
  384. regp->Attribute[12] = 0x0C;
  385. regp->Attribute[13] = 0x0D;
  386. regp->Attribute[14] = 0x0E;
  387. regp->Attribute[15] = 0x0F;
  388. regp->Attribute[NV_CIO_AR_MODE_INDEX] = 0x01; /* Enable graphic mode */
  389. /* Non-vga */
  390. regp->Attribute[NV_CIO_AR_OSCAN_INDEX] = 0x00;
  391. regp->Attribute[NV_CIO_AR_PLANE_INDEX] = 0x0F; /* enable all color planes */
  392. regp->Attribute[NV_CIO_AR_HPP_INDEX] = 0x00;
  393. regp->Attribute[NV_CIO_AR_CSEL_INDEX] = 0x00;
  394. }
  395. /**
  396. * Sets up registers for the given mode/adjusted_mode pair.
  397. *
  398. * The clocks, CRTCs and outputs attached to this CRTC must be off.
  399. *
  400. * This shouldn't enable any clocks, CRTCs, or outputs, but they should
  401. * be easily turned on/off after this.
  402. */
  403. static void
  404. nv_crtc_mode_set_regs(struct drm_crtc *crtc, struct drm_display_mode * mode)
  405. {
  406. struct drm_device *dev = crtc->dev;
  407. struct drm_nouveau_private *dev_priv = dev->dev_private;
  408. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  409. struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
  410. struct nv04_crtc_reg *savep = &nv04_display(dev)->saved_reg.crtc_reg[nv_crtc->index];
  411. struct drm_encoder *encoder;
  412. bool lvds_output = false, tmds_output = false, tv_output = false,
  413. off_chip_digital = false;
  414. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  415. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  416. bool digital = false;
  417. if (encoder->crtc != crtc)
  418. continue;
  419. if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS)
  420. digital = lvds_output = true;
  421. if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
  422. tv_output = true;
  423. if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
  424. digital = tmds_output = true;
  425. if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP && digital)
  426. off_chip_digital = true;
  427. }
  428. /* Registers not directly related to the (s)vga mode */
  429. /* What is the meaning of this register? */
  430. /* A few popular values are 0x18, 0x1c, 0x38, 0x3c */
  431. regp->CRTC[NV_CIO_CRE_ENH_INDEX] = savep->CRTC[NV_CIO_CRE_ENH_INDEX] & ~(1<<5);
  432. regp->crtc_eng_ctrl = 0;
  433. /* Except for rare conditions I2C is enabled on the primary crtc */
  434. if (nv_crtc->index == 0)
  435. regp->crtc_eng_ctrl |= NV_CRTC_FSEL_I2C;
  436. #if 0
  437. /* Set overlay to desired crtc. */
  438. if (dev->overlayAdaptor) {
  439. NVPortPrivPtr pPriv = GET_OVERLAY_PRIVATE(dev);
  440. if (pPriv->overlayCRTC == nv_crtc->index)
  441. regp->crtc_eng_ctrl |= NV_CRTC_FSEL_OVERLAY;
  442. }
  443. #endif
  444. /* ADDRESS_SPACE_PNVM is the same as setting HCUR_ASI */
  445. regp->cursor_cfg = NV_PCRTC_CURSOR_CONFIG_CUR_LINES_64 |
  446. NV_PCRTC_CURSOR_CONFIG_CUR_PIXELS_64 |
  447. NV_PCRTC_CURSOR_CONFIG_ADDRESS_SPACE_PNVM;
  448. if (dev_priv->chipset >= 0x11)
  449. regp->cursor_cfg |= NV_PCRTC_CURSOR_CONFIG_CUR_BPP_32;
  450. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  451. regp->cursor_cfg |= NV_PCRTC_CURSOR_CONFIG_DOUBLE_SCAN_ENABLE;
  452. /* Unblock some timings */
  453. regp->CRTC[NV_CIO_CRE_53] = 0;
  454. regp->CRTC[NV_CIO_CRE_54] = 0;
  455. /* 0x00 is disabled, 0x11 is lvds, 0x22 crt and 0x88 tmds */
  456. if (lvds_output)
  457. regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x11;
  458. else if (tmds_output)
  459. regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x88;
  460. else
  461. regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x22;
  462. /* These values seem to vary */
  463. /* This register seems to be used by the bios to make certain decisions on some G70 cards? */
  464. regp->CRTC[NV_CIO_CRE_SCRATCH4__INDEX] = savep->CRTC[NV_CIO_CRE_SCRATCH4__INDEX];
  465. nv_crtc_set_digital_vibrance(crtc, nv_crtc->saturation);
  466. /* probably a scratch reg, but kept for cargo-cult purposes:
  467. * bit0: crtc0?, head A
  468. * bit6: lvds, head A
  469. * bit7: (only in X), head A
  470. */
  471. if (nv_crtc->index == 0)
  472. regp->CRTC[NV_CIO_CRE_4B] = savep->CRTC[NV_CIO_CRE_4B] | 0x80;
  473. /* The blob seems to take the current value from crtc 0, add 4 to that
  474. * and reuse the old value for crtc 1 */
  475. regp->CRTC[NV_CIO_CRE_TVOUT_LATENCY] = nv04_display(dev)->saved_reg.crtc_reg[0].CRTC[NV_CIO_CRE_TVOUT_LATENCY];
  476. if (!nv_crtc->index)
  477. regp->CRTC[NV_CIO_CRE_TVOUT_LATENCY] += 4;
  478. /* the blob sometimes sets |= 0x10 (which is the same as setting |=
  479. * 1 << 30 on 0x60.830), for no apparent reason */
  480. regp->CRTC[NV_CIO_CRE_59] = off_chip_digital;
  481. if (dev_priv->card_type >= NV_30)
  482. regp->CRTC[0x9f] = off_chip_digital ? 0x11 : 0x1;
  483. regp->crtc_830 = mode->crtc_vdisplay - 3;
  484. regp->crtc_834 = mode->crtc_vdisplay - 1;
  485. if (dev_priv->card_type == NV_40)
  486. /* This is what the blob does */
  487. regp->crtc_850 = NVReadCRTC(dev, 0, NV_PCRTC_850);
  488. if (dev_priv->card_type >= NV_30)
  489. regp->gpio_ext = NVReadCRTC(dev, 0, NV_PCRTC_GPIO_EXT);
  490. if (dev_priv->card_type >= NV_10)
  491. regp->crtc_cfg = NV10_PCRTC_CONFIG_START_ADDRESS_HSYNC;
  492. else
  493. regp->crtc_cfg = NV04_PCRTC_CONFIG_START_ADDRESS_HSYNC;
  494. /* Some misc regs */
  495. if (dev_priv->card_type == NV_40) {
  496. regp->CRTC[NV_CIO_CRE_85] = 0xFF;
  497. regp->CRTC[NV_CIO_CRE_86] = 0x1;
  498. }
  499. regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] = (crtc->fb->depth + 1) / 8;
  500. /* Enable slaved mode (called MODE_TV in nv4ref.h) */
  501. if (lvds_output || tmds_output || tv_output)
  502. regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (1 << 7);
  503. /* Generic PRAMDAC regs */
  504. if (dev_priv->card_type >= NV_10)
  505. /* Only bit that bios and blob set. */
  506. regp->nv10_cursync = (1 << 25);
  507. regp->ramdac_gen_ctrl = NV_PRAMDAC_GENERAL_CONTROL_BPC_8BITS |
  508. NV_PRAMDAC_GENERAL_CONTROL_VGA_STATE_SEL |
  509. NV_PRAMDAC_GENERAL_CONTROL_PIXMIX_ON;
  510. if (crtc->fb->depth == 16)
  511. regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
  512. if (dev_priv->chipset >= 0x11)
  513. regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_PIPE_LONG;
  514. regp->ramdac_630 = 0; /* turn off green mode (tv test pattern?) */
  515. regp->tv_setup = 0;
  516. nv_crtc_set_image_sharpening(crtc, nv_crtc->sharpness);
  517. /* Some values the blob sets */
  518. regp->ramdac_8c0 = 0x100;
  519. regp->ramdac_a20 = 0x0;
  520. regp->ramdac_a24 = 0xfffff;
  521. regp->ramdac_a34 = 0x1;
  522. }
  523. /**
  524. * Sets up registers for the given mode/adjusted_mode pair.
  525. *
  526. * The clocks, CRTCs and outputs attached to this CRTC must be off.
  527. *
  528. * This shouldn't enable any clocks, CRTCs, or outputs, but they should
  529. * be easily turned on/off after this.
  530. */
  531. static int
  532. nv_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  533. struct drm_display_mode *adjusted_mode,
  534. int x, int y, struct drm_framebuffer *old_fb)
  535. {
  536. struct drm_device *dev = crtc->dev;
  537. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  538. struct drm_nouveau_private *dev_priv = dev->dev_private;
  539. NV_DEBUG_KMS(dev, "CTRC mode on CRTC %d:\n", nv_crtc->index);
  540. drm_mode_debug_printmodeline(adjusted_mode);
  541. /* unlock must come after turning off FP_TG_CONTROL in output_prepare */
  542. nv_lock_vga_crtc_shadow(dev, nv_crtc->index, -1);
  543. nv_crtc_mode_set_vga(crtc, adjusted_mode);
  544. /* calculated in nv04_dfp_prepare, nv40 needs it written before calculating PLLs */
  545. if (dev_priv->card_type == NV_40)
  546. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, nv04_display(dev)->mode_reg.sel_clk);
  547. nv_crtc_mode_set_regs(crtc, adjusted_mode);
  548. nv_crtc_calc_state_ext(crtc, mode, adjusted_mode->clock);
  549. return 0;
  550. }
  551. static void nv_crtc_save(struct drm_crtc *crtc)
  552. {
  553. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  554. struct drm_device *dev = crtc->dev;
  555. struct nv04_mode_state *state = &nv04_display(dev)->mode_reg;
  556. struct nv04_crtc_reg *crtc_state = &state->crtc_reg[nv_crtc->index];
  557. struct nv04_mode_state *saved = &nv04_display(dev)->saved_reg;
  558. struct nv04_crtc_reg *crtc_saved = &saved->crtc_reg[nv_crtc->index];
  559. if (nv_two_heads(crtc->dev))
  560. NVSetOwner(crtc->dev, nv_crtc->index);
  561. nouveau_hw_save_state(crtc->dev, nv_crtc->index, saved);
  562. /* init some state to saved value */
  563. state->sel_clk = saved->sel_clk & ~(0x5 << 16);
  564. crtc_state->CRTC[NV_CIO_CRE_LCD__INDEX] = crtc_saved->CRTC[NV_CIO_CRE_LCD__INDEX];
  565. state->pllsel = saved->pllsel & ~(PLLSEL_VPLL1_MASK | PLLSEL_VPLL2_MASK | PLLSEL_TV_MASK);
  566. crtc_state->gpio_ext = crtc_saved->gpio_ext;
  567. }
  568. static void nv_crtc_restore(struct drm_crtc *crtc)
  569. {
  570. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  571. struct drm_device *dev = crtc->dev;
  572. int head = nv_crtc->index;
  573. uint8_t saved_cr21 = nv04_display(dev)->saved_reg.crtc_reg[head].CRTC[NV_CIO_CRE_21];
  574. if (nv_two_heads(crtc->dev))
  575. NVSetOwner(crtc->dev, head);
  576. nouveau_hw_load_state(crtc->dev, head, &nv04_display(dev)->saved_reg);
  577. nv_lock_vga_crtc_shadow(crtc->dev, head, saved_cr21);
  578. nv_crtc->last_dpms = NV_DPMS_CLEARED;
  579. }
  580. static void nv_crtc_prepare(struct drm_crtc *crtc)
  581. {
  582. struct drm_device *dev = crtc->dev;
  583. struct drm_nouveau_private *dev_priv = dev->dev_private;
  584. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  585. struct drm_crtc_helper_funcs *funcs = crtc->helper_private;
  586. if (nv_two_heads(dev))
  587. NVSetOwner(dev, nv_crtc->index);
  588. drm_vblank_pre_modeset(dev, nv_crtc->index);
  589. funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  590. NVBlankScreen(dev, nv_crtc->index, true);
  591. /* Some more preparation. */
  592. NVWriteCRTC(dev, nv_crtc->index, NV_PCRTC_CONFIG, NV_PCRTC_CONFIG_START_ADDRESS_NON_VGA);
  593. if (dev_priv->card_type == NV_40) {
  594. uint32_t reg900 = NVReadRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_900);
  595. NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_900, reg900 & ~0x10000);
  596. }
  597. }
  598. static void nv_crtc_commit(struct drm_crtc *crtc)
  599. {
  600. struct drm_device *dev = crtc->dev;
  601. struct drm_crtc_helper_funcs *funcs = crtc->helper_private;
  602. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  603. nouveau_hw_load_state(dev, nv_crtc->index, &nv04_display(dev)->mode_reg);
  604. nv04_crtc_mode_set_base(crtc, crtc->x, crtc->y, NULL);
  605. #ifdef __BIG_ENDIAN
  606. /* turn on LFB swapping */
  607. {
  608. uint8_t tmp = NVReadVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RCR);
  609. tmp |= MASK(NV_CIO_CRE_RCR_ENDIAN_BIG);
  610. NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RCR, tmp);
  611. }
  612. #endif
  613. funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  614. drm_vblank_post_modeset(dev, nv_crtc->index);
  615. }
  616. static void nv_crtc_destroy(struct drm_crtc *crtc)
  617. {
  618. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  619. NV_DEBUG_KMS(crtc->dev, "\n");
  620. if (!nv_crtc)
  621. return;
  622. drm_crtc_cleanup(crtc);
  623. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  624. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  625. kfree(nv_crtc);
  626. }
  627. static void
  628. nv_crtc_gamma_load(struct drm_crtc *crtc)
  629. {
  630. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  631. struct drm_device *dev = nv_crtc->base.dev;
  632. struct rgb { uint8_t r, g, b; } __attribute__((packed)) *rgbs;
  633. int i;
  634. rgbs = (struct rgb *)nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index].DAC;
  635. for (i = 0; i < 256; i++) {
  636. rgbs[i].r = nv_crtc->lut.r[i] >> 8;
  637. rgbs[i].g = nv_crtc->lut.g[i] >> 8;
  638. rgbs[i].b = nv_crtc->lut.b[i] >> 8;
  639. }
  640. nouveau_hw_load_state_palette(dev, nv_crtc->index, &nv04_display(dev)->mode_reg);
  641. }
  642. static void
  643. nv_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b, uint32_t start,
  644. uint32_t size)
  645. {
  646. int end = (start + size > 256) ? 256 : start + size, i;
  647. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  648. for (i = start; i < end; i++) {
  649. nv_crtc->lut.r[i] = r[i];
  650. nv_crtc->lut.g[i] = g[i];
  651. nv_crtc->lut.b[i] = b[i];
  652. }
  653. /* We need to know the depth before we upload, but it's possible to
  654. * get called before a framebuffer is bound. If this is the case,
  655. * mark the lut values as dirty by setting depth==0, and it'll be
  656. * uploaded on the first mode_set_base()
  657. */
  658. if (!nv_crtc->base.fb) {
  659. nv_crtc->lut.depth = 0;
  660. return;
  661. }
  662. nv_crtc_gamma_load(crtc);
  663. }
  664. static int
  665. nv04_crtc_do_mode_set_base(struct drm_crtc *crtc,
  666. struct drm_framebuffer *passed_fb,
  667. int x, int y, bool atomic)
  668. {
  669. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  670. struct drm_device *dev = crtc->dev;
  671. struct drm_nouveau_private *dev_priv = dev->dev_private;
  672. struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
  673. struct drm_framebuffer *drm_fb;
  674. struct nouveau_framebuffer *fb;
  675. int arb_burst, arb_lwm;
  676. int ret;
  677. NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
  678. /* no fb bound */
  679. if (!atomic && !crtc->fb) {
  680. NV_DEBUG_KMS(dev, "No FB bound\n");
  681. return 0;
  682. }
  683. /* If atomic, we want to switch to the fb we were passed, so
  684. * now we update pointers to do that. (We don't pin; just
  685. * assume we're already pinned and update the base address.)
  686. */
  687. if (atomic) {
  688. drm_fb = passed_fb;
  689. fb = nouveau_framebuffer(passed_fb);
  690. } else {
  691. drm_fb = crtc->fb;
  692. fb = nouveau_framebuffer(crtc->fb);
  693. /* If not atomic, we can go ahead and pin, and unpin the
  694. * old fb we were passed.
  695. */
  696. ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM);
  697. if (ret)
  698. return ret;
  699. if (passed_fb) {
  700. struct nouveau_framebuffer *ofb = nouveau_framebuffer(passed_fb);
  701. nouveau_bo_unpin(ofb->nvbo);
  702. }
  703. }
  704. nv_crtc->fb.offset = fb->nvbo->bo.offset;
  705. if (nv_crtc->lut.depth != drm_fb->depth) {
  706. nv_crtc->lut.depth = drm_fb->depth;
  707. nv_crtc_gamma_load(crtc);
  708. }
  709. /* Update the framebuffer format. */
  710. regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] &= ~3;
  711. regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (crtc->fb->depth + 1) / 8;
  712. regp->ramdac_gen_ctrl &= ~NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
  713. if (crtc->fb->depth == 16)
  714. regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
  715. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_PIXEL_INDEX);
  716. NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_GENERAL_CONTROL,
  717. regp->ramdac_gen_ctrl);
  718. regp->CRTC[NV_CIO_CR_OFFSET_INDEX] = drm_fb->pitches[0] >> 3;
  719. regp->CRTC[NV_CIO_CRE_RPC0_INDEX] =
  720. XLATE(drm_fb->pitches[0] >> 3, 8, NV_CIO_CRE_RPC0_OFFSET_10_8);
  721. regp->CRTC[NV_CIO_CRE_42] =
  722. XLATE(drm_fb->pitches[0] / 8, 11, NV_CIO_CRE_42_OFFSET_11);
  723. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_RPC0_INDEX);
  724. crtc_wr_cio_state(crtc, regp, NV_CIO_CR_OFFSET_INDEX);
  725. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_42);
  726. /* Update the framebuffer location. */
  727. regp->fb_start = nv_crtc->fb.offset & ~3;
  728. regp->fb_start += (y * drm_fb->pitches[0]) + (x * drm_fb->bits_per_pixel / 8);
  729. nv_set_crtc_base(dev, nv_crtc->index, regp->fb_start);
  730. /* Update the arbitration parameters. */
  731. nouveau_calc_arb(dev, crtc->mode.clock, drm_fb->bits_per_pixel,
  732. &arb_burst, &arb_lwm);
  733. regp->CRTC[NV_CIO_CRE_FF_INDEX] = arb_burst;
  734. regp->CRTC[NV_CIO_CRE_FFLWM__INDEX] = arb_lwm & 0xff;
  735. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_FF_INDEX);
  736. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_FFLWM__INDEX);
  737. if (dev_priv->card_type >= NV_20) {
  738. regp->CRTC[NV_CIO_CRE_47] = arb_lwm >> 8;
  739. crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_47);
  740. }
  741. return 0;
  742. }
  743. static int
  744. nv04_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  745. struct drm_framebuffer *old_fb)
  746. {
  747. return nv04_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
  748. }
  749. static int
  750. nv04_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
  751. struct drm_framebuffer *fb,
  752. int x, int y, enum mode_set_atomic state)
  753. {
  754. struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
  755. struct drm_device *dev = dev_priv->dev;
  756. if (state == ENTER_ATOMIC_MODE_SET)
  757. nouveau_fbcon_save_disable_accel(dev);
  758. else
  759. nouveau_fbcon_restore_accel(dev);
  760. return nv04_crtc_do_mode_set_base(crtc, fb, x, y, true);
  761. }
  762. static void nv04_cursor_upload(struct drm_device *dev, struct nouveau_bo *src,
  763. struct nouveau_bo *dst)
  764. {
  765. int width = nv_cursor_width(dev);
  766. uint32_t pixel;
  767. int i, j;
  768. for (i = 0; i < width; i++) {
  769. for (j = 0; j < width; j++) {
  770. pixel = nouveau_bo_rd32(src, i*64 + j);
  771. nouveau_bo_wr16(dst, i*width + j, (pixel & 0x80000000) >> 16
  772. | (pixel & 0xf80000) >> 9
  773. | (pixel & 0xf800) >> 6
  774. | (pixel & 0xf8) >> 3);
  775. }
  776. }
  777. }
  778. static void nv11_cursor_upload(struct drm_device *dev, struct nouveau_bo *src,
  779. struct nouveau_bo *dst)
  780. {
  781. uint32_t pixel;
  782. int alpha, i;
  783. /* nv11+ supports premultiplied (PM), or non-premultiplied (NPM) alpha
  784. * cursors (though NPM in combination with fp dithering may not work on
  785. * nv11, from "nv" driver history)
  786. * NPM mode needs NV_PCRTC_CURSOR_CONFIG_ALPHA_BLEND set and is what the
  787. * blob uses, however we get given PM cursors so we use PM mode
  788. */
  789. for (i = 0; i < 64 * 64; i++) {
  790. pixel = nouveau_bo_rd32(src, i);
  791. /* hw gets unhappy if alpha <= rgb values. for a PM image "less
  792. * than" shouldn't happen; fix "equal to" case by adding one to
  793. * alpha channel (slightly inaccurate, but so is attempting to
  794. * get back to NPM images, due to limits of integer precision)
  795. */
  796. alpha = pixel >> 24;
  797. if (alpha > 0 && alpha < 255)
  798. pixel = (pixel & 0x00ffffff) | ((alpha + 1) << 24);
  799. #ifdef __BIG_ENDIAN
  800. {
  801. struct drm_nouveau_private *dev_priv = dev->dev_private;
  802. if (dev_priv->chipset == 0x11) {
  803. pixel = ((pixel & 0x000000ff) << 24) |
  804. ((pixel & 0x0000ff00) << 8) |
  805. ((pixel & 0x00ff0000) >> 8) |
  806. ((pixel & 0xff000000) >> 24);
  807. }
  808. }
  809. #endif
  810. nouveau_bo_wr32(dst, i, pixel);
  811. }
  812. }
  813. static int
  814. nv04_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  815. uint32_t buffer_handle, uint32_t width, uint32_t height)
  816. {
  817. struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
  818. struct drm_device *dev = dev_priv->dev;
  819. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  820. struct nouveau_bo *cursor = NULL;
  821. struct drm_gem_object *gem;
  822. int ret = 0;
  823. if (!buffer_handle) {
  824. nv_crtc->cursor.hide(nv_crtc, true);
  825. return 0;
  826. }
  827. if (width != 64 || height != 64)
  828. return -EINVAL;
  829. gem = drm_gem_object_lookup(dev, file_priv, buffer_handle);
  830. if (!gem)
  831. return -ENOENT;
  832. cursor = nouveau_gem_object(gem);
  833. ret = nouveau_bo_map(cursor);
  834. if (ret)
  835. goto out;
  836. if (dev_priv->chipset >= 0x11)
  837. nv11_cursor_upload(dev, cursor, nv_crtc->cursor.nvbo);
  838. else
  839. nv04_cursor_upload(dev, cursor, nv_crtc->cursor.nvbo);
  840. nouveau_bo_unmap(cursor);
  841. nv_crtc->cursor.offset = nv_crtc->cursor.nvbo->bo.offset;
  842. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.offset);
  843. nv_crtc->cursor.show(nv_crtc, true);
  844. out:
  845. drm_gem_object_unreference_unlocked(gem);
  846. return ret;
  847. }
  848. static int
  849. nv04_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  850. {
  851. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  852. nv_crtc->cursor.set_pos(nv_crtc, x, y);
  853. return 0;
  854. }
  855. static const struct drm_crtc_funcs nv04_crtc_funcs = {
  856. .save = nv_crtc_save,
  857. .restore = nv_crtc_restore,
  858. .cursor_set = nv04_crtc_cursor_set,
  859. .cursor_move = nv04_crtc_cursor_move,
  860. .gamma_set = nv_crtc_gamma_set,
  861. .set_config = drm_crtc_helper_set_config,
  862. .page_flip = nouveau_crtc_page_flip,
  863. .destroy = nv_crtc_destroy,
  864. };
  865. static const struct drm_crtc_helper_funcs nv04_crtc_helper_funcs = {
  866. .dpms = nv_crtc_dpms,
  867. .prepare = nv_crtc_prepare,
  868. .commit = nv_crtc_commit,
  869. .mode_fixup = nv_crtc_mode_fixup,
  870. .mode_set = nv_crtc_mode_set,
  871. .mode_set_base = nv04_crtc_mode_set_base,
  872. .mode_set_base_atomic = nv04_crtc_mode_set_base_atomic,
  873. .load_lut = nv_crtc_gamma_load,
  874. };
  875. int
  876. nv04_crtc_create(struct drm_device *dev, int crtc_num)
  877. {
  878. struct nouveau_crtc *nv_crtc;
  879. int ret, i;
  880. nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
  881. if (!nv_crtc)
  882. return -ENOMEM;
  883. for (i = 0; i < 256; i++) {
  884. nv_crtc->lut.r[i] = i << 8;
  885. nv_crtc->lut.g[i] = i << 8;
  886. nv_crtc->lut.b[i] = i << 8;
  887. }
  888. nv_crtc->lut.depth = 0;
  889. nv_crtc->index = crtc_num;
  890. nv_crtc->last_dpms = NV_DPMS_CLEARED;
  891. drm_crtc_init(dev, &nv_crtc->base, &nv04_crtc_funcs);
  892. drm_crtc_helper_add(&nv_crtc->base, &nv04_crtc_helper_funcs);
  893. drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
  894. ret = nouveau_bo_new(dev, 64*64*4, 0x100, TTM_PL_FLAG_VRAM,
  895. 0, 0x0000, NULL, &nv_crtc->cursor.nvbo);
  896. if (!ret) {
  897. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
  898. if (!ret)
  899. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  900. if (ret)
  901. nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
  902. }
  903. nv04_cursor_init(nv_crtc);
  904. return 0;
  905. }