nv04_crtc.c 34 KB

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