crtc.c 38 KB

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