crtc.c 37 KB

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