crtc.c 37 KB

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