psb_intel_display.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * Copyright © 2006-2011 Intel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Authors:
  18. * Eric Anholt <eric@anholt.net>
  19. */
  20. #include <linux/i2c.h>
  21. #include <linux/pm_runtime.h>
  22. #include <drm/drmP.h>
  23. #include "framebuffer.h"
  24. #include "psb_drv.h"
  25. #include "psb_intel_drv.h"
  26. #include "psb_intel_reg.h"
  27. #include "gma_display.h"
  28. #include "power.h"
  29. #define INTEL_LIMIT_I9XX_SDVO_DAC 0
  30. #define INTEL_LIMIT_I9XX_LVDS 1
  31. static const struct gma_limit_t psb_intel_limits[] = {
  32. { /* INTEL_LIMIT_I9XX_SDVO_DAC */
  33. .dot = {.min = 20000, .max = 400000},
  34. .vco = {.min = 1400000, .max = 2800000},
  35. .n = {.min = 1, .max = 6},
  36. .m = {.min = 70, .max = 120},
  37. .m1 = {.min = 8, .max = 18},
  38. .m2 = {.min = 3, .max = 7},
  39. .p = {.min = 5, .max = 80},
  40. .p1 = {.min = 1, .max = 8},
  41. .p2 = {.dot_limit = 200000, .p2_slow = 10, .p2_fast = 5},
  42. .find_pll = gma_find_best_pll,
  43. },
  44. { /* INTEL_LIMIT_I9XX_LVDS */
  45. .dot = {.min = 20000, .max = 400000},
  46. .vco = {.min = 1400000, .max = 2800000},
  47. .n = {.min = 1, .max = 6},
  48. .m = {.min = 70, .max = 120},
  49. .m1 = {.min = 8, .max = 18},
  50. .m2 = {.min = 3, .max = 7},
  51. .p = {.min = 7, .max = 98},
  52. .p1 = {.min = 1, .max = 8},
  53. /* The single-channel range is 25-112Mhz, and dual-channel
  54. * is 80-224Mhz. Prefer single channel as much as possible.
  55. */
  56. .p2 = {.dot_limit = 112000, .p2_slow = 14, .p2_fast = 7},
  57. .find_pll = gma_find_best_pll,
  58. },
  59. };
  60. static const struct gma_limit_t *psb_intel_limit(struct drm_crtc *crtc,
  61. int refclk)
  62. {
  63. const struct gma_limit_t *limit;
  64. if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  65. limit = &psb_intel_limits[INTEL_LIMIT_I9XX_LVDS];
  66. else
  67. limit = &psb_intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
  68. return limit;
  69. }
  70. static void psb_intel_clock(int refclk, struct gma_clock_t *clock)
  71. {
  72. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  73. clock->p = clock->p1 * clock->p2;
  74. clock->vco = refclk * clock->m / (clock->n + 2);
  75. clock->dot = clock->vco / clock->p;
  76. }
  77. void psb_intel_encoder_prepare(struct drm_encoder *encoder)
  78. {
  79. struct drm_encoder_helper_funcs *encoder_funcs =
  80. encoder->helper_private;
  81. /* lvds has its own version of prepare see psb_intel_lvds_prepare */
  82. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  83. }
  84. void psb_intel_encoder_commit(struct drm_encoder *encoder)
  85. {
  86. struct drm_encoder_helper_funcs *encoder_funcs =
  87. encoder->helper_private;
  88. /* lvds has its own version of commit see psb_intel_lvds_commit */
  89. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  90. }
  91. void psb_intel_encoder_destroy(struct drm_encoder *encoder)
  92. {
  93. struct psb_intel_encoder *intel_encoder = to_psb_intel_encoder(encoder);
  94. drm_encoder_cleanup(encoder);
  95. kfree(intel_encoder);
  96. }
  97. /**
  98. * Return the pipe currently connected to the panel fitter,
  99. * or -1 if the panel fitter is not present or not in use
  100. */
  101. static int psb_intel_panel_fitter_pipe(struct drm_device *dev)
  102. {
  103. u32 pfit_control;
  104. pfit_control = REG_READ(PFIT_CONTROL);
  105. /* See if the panel fitter is in use */
  106. if ((pfit_control & PFIT_ENABLE) == 0)
  107. return -1;
  108. /* Must be on PIPE 1 for PSB */
  109. return 1;
  110. }
  111. static int psb_intel_crtc_mode_set(struct drm_crtc *crtc,
  112. struct drm_display_mode *mode,
  113. struct drm_display_mode *adjusted_mode,
  114. int x, int y,
  115. struct drm_framebuffer *old_fb)
  116. {
  117. struct drm_device *dev = crtc->dev;
  118. struct drm_psb_private *dev_priv = dev->dev_private;
  119. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  120. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  121. int pipe = psb_intel_crtc->pipe;
  122. const struct psb_offset *map = &dev_priv->regmap[pipe];
  123. int refclk;
  124. struct gma_clock_t clock;
  125. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  126. bool ok, is_sdvo = false;
  127. bool is_lvds = false, is_tv = false;
  128. struct drm_mode_config *mode_config = &dev->mode_config;
  129. struct drm_connector *connector;
  130. const struct gma_limit_t *limit;
  131. /* No scan out no play */
  132. if (crtc->fb == NULL) {
  133. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  134. return 0;
  135. }
  136. list_for_each_entry(connector, &mode_config->connector_list, head) {
  137. struct psb_intel_encoder *psb_intel_encoder =
  138. psb_intel_attached_encoder(connector);
  139. if (!connector->encoder
  140. || connector->encoder->crtc != crtc)
  141. continue;
  142. switch (psb_intel_encoder->type) {
  143. case INTEL_OUTPUT_LVDS:
  144. is_lvds = true;
  145. break;
  146. case INTEL_OUTPUT_SDVO:
  147. is_sdvo = true;
  148. break;
  149. case INTEL_OUTPUT_TVOUT:
  150. is_tv = true;
  151. break;
  152. }
  153. }
  154. refclk = 96000;
  155. limit = psb_intel_crtc->clock_funcs->limit(crtc, refclk);
  156. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk,
  157. &clock);
  158. if (!ok) {
  159. DRM_ERROR("Couldn't find PLL settings for mode! target: %d, actual: %d",
  160. adjusted_mode->clock, clock.dot);
  161. return 0;
  162. }
  163. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  164. dpll = DPLL_VGA_MODE_DIS;
  165. if (is_lvds) {
  166. dpll |= DPLLB_MODE_LVDS;
  167. dpll |= DPLL_DVO_HIGH_SPEED;
  168. } else
  169. dpll |= DPLLB_MODE_DAC_SERIAL;
  170. if (is_sdvo) {
  171. int sdvo_pixel_multiply =
  172. adjusted_mode->clock / mode->clock;
  173. dpll |= DPLL_DVO_HIGH_SPEED;
  174. dpll |=
  175. (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  176. }
  177. /* compute bitmask from p1 value */
  178. dpll |= (1 << (clock.p1 - 1)) << 16;
  179. switch (clock.p2) {
  180. case 5:
  181. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  182. break;
  183. case 7:
  184. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  185. break;
  186. case 10:
  187. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  188. break;
  189. case 14:
  190. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  191. break;
  192. }
  193. if (is_tv) {
  194. /* XXX: just matching BIOS for now */
  195. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  196. dpll |= 3;
  197. }
  198. dpll |= PLL_REF_INPUT_DREFCLK;
  199. /* setup pipeconf */
  200. pipeconf = REG_READ(map->conf);
  201. /* Set up the display plane register */
  202. dspcntr = DISPPLANE_GAMMA_ENABLE;
  203. if (pipe == 0)
  204. dspcntr |= DISPPLANE_SEL_PIPE_A;
  205. else
  206. dspcntr |= DISPPLANE_SEL_PIPE_B;
  207. dspcntr |= DISPLAY_PLANE_ENABLE;
  208. pipeconf |= PIPEACONF_ENABLE;
  209. dpll |= DPLL_VCO_ENABLE;
  210. /* Disable the panel fitter if it was on our pipe */
  211. if (psb_intel_panel_fitter_pipe(dev) == pipe)
  212. REG_WRITE(PFIT_CONTROL, 0);
  213. drm_mode_debug_printmodeline(mode);
  214. if (dpll & DPLL_VCO_ENABLE) {
  215. REG_WRITE(map->fp0, fp);
  216. REG_WRITE(map->dpll, dpll & ~DPLL_VCO_ENABLE);
  217. REG_READ(map->dpll);
  218. udelay(150);
  219. }
  220. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  221. * This is an exception to the general rule that mode_set doesn't turn
  222. * things on.
  223. */
  224. if (is_lvds) {
  225. u32 lvds = REG_READ(LVDS);
  226. lvds &= ~LVDS_PIPEB_SELECT;
  227. if (pipe == 1)
  228. lvds |= LVDS_PIPEB_SELECT;
  229. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  230. /* Set the B0-B3 data pairs corresponding to
  231. * whether we're going to
  232. * set the DPLLs for dual-channel mode or not.
  233. */
  234. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  235. if (clock.p2 == 7)
  236. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  237. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  238. * appropriately here, but we need to look more
  239. * thoroughly into how panels behave in the two modes.
  240. */
  241. REG_WRITE(LVDS, lvds);
  242. REG_READ(LVDS);
  243. }
  244. REG_WRITE(map->fp0, fp);
  245. REG_WRITE(map->dpll, dpll);
  246. REG_READ(map->dpll);
  247. /* Wait for the clocks to stabilize. */
  248. udelay(150);
  249. /* write it again -- the BIOS does, after all */
  250. REG_WRITE(map->dpll, dpll);
  251. REG_READ(map->dpll);
  252. /* Wait for the clocks to stabilize. */
  253. udelay(150);
  254. REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
  255. ((adjusted_mode->crtc_htotal - 1) << 16));
  256. REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
  257. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  258. REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
  259. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  260. REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
  261. ((adjusted_mode->crtc_vtotal - 1) << 16));
  262. REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
  263. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  264. REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
  265. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  266. /* pipesrc and dspsize control the size that is scaled from,
  267. * which should always be the user's requested size.
  268. */
  269. REG_WRITE(map->size,
  270. ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  271. REG_WRITE(map->pos, 0);
  272. REG_WRITE(map->src,
  273. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  274. REG_WRITE(map->conf, pipeconf);
  275. REG_READ(map->conf);
  276. gma_wait_for_vblank(dev);
  277. REG_WRITE(map->cntr, dspcntr);
  278. /* Flush the plane changes */
  279. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  280. gma_wait_for_vblank(dev);
  281. return 0;
  282. }
  283. /**
  284. * Save HW states of giving crtc
  285. */
  286. static void psb_intel_crtc_save(struct drm_crtc *crtc)
  287. {
  288. struct drm_device *dev = crtc->dev;
  289. struct drm_psb_private *dev_priv = dev->dev_private;
  290. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  291. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  292. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  293. uint32_t paletteReg;
  294. int i;
  295. if (!crtc_state) {
  296. dev_err(dev->dev, "No CRTC state found\n");
  297. return;
  298. }
  299. crtc_state->saveDSPCNTR = REG_READ(map->cntr);
  300. crtc_state->savePIPECONF = REG_READ(map->conf);
  301. crtc_state->savePIPESRC = REG_READ(map->src);
  302. crtc_state->saveFP0 = REG_READ(map->fp0);
  303. crtc_state->saveFP1 = REG_READ(map->fp1);
  304. crtc_state->saveDPLL = REG_READ(map->dpll);
  305. crtc_state->saveHTOTAL = REG_READ(map->htotal);
  306. crtc_state->saveHBLANK = REG_READ(map->hblank);
  307. crtc_state->saveHSYNC = REG_READ(map->hsync);
  308. crtc_state->saveVTOTAL = REG_READ(map->vtotal);
  309. crtc_state->saveVBLANK = REG_READ(map->vblank);
  310. crtc_state->saveVSYNC = REG_READ(map->vsync);
  311. crtc_state->saveDSPSTRIDE = REG_READ(map->stride);
  312. /*NOTE: DSPSIZE DSPPOS only for psb*/
  313. crtc_state->saveDSPSIZE = REG_READ(map->size);
  314. crtc_state->saveDSPPOS = REG_READ(map->pos);
  315. crtc_state->saveDSPBASE = REG_READ(map->base);
  316. paletteReg = map->palette;
  317. for (i = 0; i < 256; ++i)
  318. crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
  319. }
  320. /**
  321. * Restore HW states of giving crtc
  322. */
  323. static void psb_intel_crtc_restore(struct drm_crtc *crtc)
  324. {
  325. struct drm_device *dev = crtc->dev;
  326. struct drm_psb_private *dev_priv = dev->dev_private;
  327. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  328. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  329. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  330. uint32_t paletteReg;
  331. int i;
  332. if (!crtc_state) {
  333. dev_err(dev->dev, "No crtc state\n");
  334. return;
  335. }
  336. if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
  337. REG_WRITE(map->dpll,
  338. crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
  339. REG_READ(map->dpll);
  340. udelay(150);
  341. }
  342. REG_WRITE(map->fp0, crtc_state->saveFP0);
  343. REG_READ(map->fp0);
  344. REG_WRITE(map->fp1, crtc_state->saveFP1);
  345. REG_READ(map->fp1);
  346. REG_WRITE(map->dpll, crtc_state->saveDPLL);
  347. REG_READ(map->dpll);
  348. udelay(150);
  349. REG_WRITE(map->htotal, crtc_state->saveHTOTAL);
  350. REG_WRITE(map->hblank, crtc_state->saveHBLANK);
  351. REG_WRITE(map->hsync, crtc_state->saveHSYNC);
  352. REG_WRITE(map->vtotal, crtc_state->saveVTOTAL);
  353. REG_WRITE(map->vblank, crtc_state->saveVBLANK);
  354. REG_WRITE(map->vsync, crtc_state->saveVSYNC);
  355. REG_WRITE(map->stride, crtc_state->saveDSPSTRIDE);
  356. REG_WRITE(map->size, crtc_state->saveDSPSIZE);
  357. REG_WRITE(map->pos, crtc_state->saveDSPPOS);
  358. REG_WRITE(map->src, crtc_state->savePIPESRC);
  359. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  360. REG_WRITE(map->conf, crtc_state->savePIPECONF);
  361. gma_wait_for_vblank(dev);
  362. REG_WRITE(map->cntr, crtc_state->saveDSPCNTR);
  363. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  364. gma_wait_for_vblank(dev);
  365. paletteReg = map->palette;
  366. for (i = 0; i < 256; ++i)
  367. REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
  368. }
  369. static int psb_intel_crtc_cursor_set(struct drm_crtc *crtc,
  370. struct drm_file *file_priv,
  371. uint32_t handle,
  372. uint32_t width, uint32_t height)
  373. {
  374. struct drm_device *dev = crtc->dev;
  375. struct drm_psb_private *dev_priv = dev->dev_private;
  376. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  377. int pipe = psb_intel_crtc->pipe;
  378. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  379. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  380. uint32_t temp;
  381. size_t addr = 0;
  382. struct gtt_range *gt;
  383. struct gtt_range *cursor_gt = psb_intel_crtc->cursor_gt;
  384. struct drm_gem_object *obj;
  385. void *tmp_dst, *tmp_src;
  386. int ret = 0, i, cursor_pages;
  387. /* if we want to turn of the cursor ignore width and height */
  388. if (!handle) {
  389. /* turn off the cursor */
  390. temp = CURSOR_MODE_DISABLE;
  391. if (gma_power_begin(dev, false)) {
  392. REG_WRITE(control, temp);
  393. REG_WRITE(base, 0);
  394. gma_power_end(dev);
  395. }
  396. /* Unpin the old GEM object */
  397. if (psb_intel_crtc->cursor_obj) {
  398. gt = container_of(psb_intel_crtc->cursor_obj,
  399. struct gtt_range, gem);
  400. psb_gtt_unpin(gt);
  401. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  402. psb_intel_crtc->cursor_obj = NULL;
  403. }
  404. return 0;
  405. }
  406. /* Currently we only support 64x64 cursors */
  407. if (width != 64 || height != 64) {
  408. dev_dbg(dev->dev, "we currently only support 64x64 cursors\n");
  409. return -EINVAL;
  410. }
  411. obj = drm_gem_object_lookup(dev, file_priv, handle);
  412. if (!obj)
  413. return -ENOENT;
  414. if (obj->size < width * height * 4) {
  415. dev_dbg(dev->dev, "buffer is to small\n");
  416. ret = -ENOMEM;
  417. goto unref_cursor;
  418. }
  419. gt = container_of(obj, struct gtt_range, gem);
  420. /* Pin the memory into the GTT */
  421. ret = psb_gtt_pin(gt);
  422. if (ret) {
  423. dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
  424. goto unref_cursor;
  425. }
  426. if (dev_priv->ops->cursor_needs_phys) {
  427. if (cursor_gt == NULL) {
  428. dev_err(dev->dev, "No hardware cursor mem available");
  429. ret = -ENOMEM;
  430. goto unref_cursor;
  431. }
  432. /* Prevent overflow */
  433. if (gt->npage > 4)
  434. cursor_pages = 4;
  435. else
  436. cursor_pages = gt->npage;
  437. /* Copy the cursor to cursor mem */
  438. tmp_dst = dev_priv->vram_addr + cursor_gt->offset;
  439. for (i = 0; i < cursor_pages; i++) {
  440. tmp_src = kmap(gt->pages[i]);
  441. memcpy(tmp_dst, tmp_src, PAGE_SIZE);
  442. kunmap(gt->pages[i]);
  443. tmp_dst += PAGE_SIZE;
  444. }
  445. addr = psb_intel_crtc->cursor_addr;
  446. } else {
  447. addr = gt->offset; /* Or resource.start ??? */
  448. psb_intel_crtc->cursor_addr = addr;
  449. }
  450. temp = 0;
  451. /* set the pipe for the cursor */
  452. temp |= (pipe << 28);
  453. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  454. if (gma_power_begin(dev, false)) {
  455. REG_WRITE(control, temp);
  456. REG_WRITE(base, addr);
  457. gma_power_end(dev);
  458. }
  459. /* unpin the old bo */
  460. if (psb_intel_crtc->cursor_obj) {
  461. gt = container_of(psb_intel_crtc->cursor_obj,
  462. struct gtt_range, gem);
  463. psb_gtt_unpin(gt);
  464. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  465. }
  466. psb_intel_crtc->cursor_obj = obj;
  467. return ret;
  468. unref_cursor:
  469. drm_gem_object_unreference(obj);
  470. return ret;
  471. }
  472. static int psb_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  473. {
  474. struct drm_device *dev = crtc->dev;
  475. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  476. int pipe = psb_intel_crtc->pipe;
  477. uint32_t temp = 0;
  478. uint32_t addr;
  479. if (x < 0) {
  480. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  481. x = -x;
  482. }
  483. if (y < 0) {
  484. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  485. y = -y;
  486. }
  487. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  488. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  489. addr = psb_intel_crtc->cursor_addr;
  490. if (gma_power_begin(dev, false)) {
  491. REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  492. REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, addr);
  493. gma_power_end(dev);
  494. }
  495. return 0;
  496. }
  497. static int psb_crtc_set_config(struct drm_mode_set *set)
  498. {
  499. int ret;
  500. struct drm_device *dev = set->crtc->dev;
  501. struct drm_psb_private *dev_priv = dev->dev_private;
  502. if (!dev_priv->rpm_enabled)
  503. return drm_crtc_helper_set_config(set);
  504. pm_runtime_forbid(&dev->pdev->dev);
  505. ret = drm_crtc_helper_set_config(set);
  506. pm_runtime_allow(&dev->pdev->dev);
  507. return ret;
  508. }
  509. /* Returns the clock of the currently programmed mode of the given pipe. */
  510. static int psb_intel_crtc_clock_get(struct drm_device *dev,
  511. struct drm_crtc *crtc)
  512. {
  513. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  514. struct drm_psb_private *dev_priv = dev->dev_private;
  515. int pipe = psb_intel_crtc->pipe;
  516. const struct psb_offset *map = &dev_priv->regmap[pipe];
  517. u32 dpll;
  518. u32 fp;
  519. struct gma_clock_t clock;
  520. bool is_lvds;
  521. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  522. if (gma_power_begin(dev, false)) {
  523. dpll = REG_READ(map->dpll);
  524. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  525. fp = REG_READ(map->fp0);
  526. else
  527. fp = REG_READ(map->fp1);
  528. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  529. gma_power_end(dev);
  530. } else {
  531. dpll = p->dpll;
  532. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  533. fp = p->fp0;
  534. else
  535. fp = p->fp1;
  536. is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS &
  537. LVDS_PORT_EN);
  538. }
  539. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  540. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  541. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  542. if (is_lvds) {
  543. clock.p1 =
  544. ffs((dpll &
  545. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  546. DPLL_FPA01_P1_POST_DIV_SHIFT);
  547. clock.p2 = 14;
  548. if ((dpll & PLL_REF_INPUT_MASK) ==
  549. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  550. /* XXX: might not be 66MHz */
  551. psb_intel_clock(66000, &clock);
  552. } else
  553. psb_intel_clock(48000, &clock);
  554. } else {
  555. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  556. clock.p1 = 2;
  557. else {
  558. clock.p1 =
  559. ((dpll &
  560. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  561. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  562. }
  563. if (dpll & PLL_P2_DIVIDE_BY_4)
  564. clock.p2 = 4;
  565. else
  566. clock.p2 = 2;
  567. psb_intel_clock(48000, &clock);
  568. }
  569. /* XXX: It would be nice to validate the clocks, but we can't reuse
  570. * i830PllIsValid() because it relies on the xf86_config connector
  571. * configuration being accurate, which it isn't necessarily.
  572. */
  573. return clock.dot;
  574. }
  575. /** Returns the currently programmed mode of the given pipe. */
  576. struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
  577. struct drm_crtc *crtc)
  578. {
  579. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  580. int pipe = psb_intel_crtc->pipe;
  581. struct drm_display_mode *mode;
  582. int htot;
  583. int hsync;
  584. int vtot;
  585. int vsync;
  586. struct drm_psb_private *dev_priv = dev->dev_private;
  587. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  588. const struct psb_offset *map = &dev_priv->regmap[pipe];
  589. if (gma_power_begin(dev, false)) {
  590. htot = REG_READ(map->htotal);
  591. hsync = REG_READ(map->hsync);
  592. vtot = REG_READ(map->vtotal);
  593. vsync = REG_READ(map->vsync);
  594. gma_power_end(dev);
  595. } else {
  596. htot = p->htotal;
  597. hsync = p->hsync;
  598. vtot = p->vtotal;
  599. vsync = p->vsync;
  600. }
  601. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  602. if (!mode)
  603. return NULL;
  604. mode->clock = psb_intel_crtc_clock_get(dev, crtc);
  605. mode->hdisplay = (htot & 0xffff) + 1;
  606. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  607. mode->hsync_start = (hsync & 0xffff) + 1;
  608. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  609. mode->vdisplay = (vtot & 0xffff) + 1;
  610. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  611. mode->vsync_start = (vsync & 0xffff) + 1;
  612. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  613. drm_mode_set_name(mode);
  614. drm_mode_set_crtcinfo(mode, 0);
  615. return mode;
  616. }
  617. static void psb_intel_crtc_destroy(struct drm_crtc *crtc)
  618. {
  619. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  620. struct gtt_range *gt;
  621. /* Unpin the old GEM object */
  622. if (psb_intel_crtc->cursor_obj) {
  623. gt = container_of(psb_intel_crtc->cursor_obj,
  624. struct gtt_range, gem);
  625. psb_gtt_unpin(gt);
  626. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  627. psb_intel_crtc->cursor_obj = NULL;
  628. }
  629. if (psb_intel_crtc->cursor_gt != NULL)
  630. psb_gtt_free_range(crtc->dev, psb_intel_crtc->cursor_gt);
  631. kfree(psb_intel_crtc->crtc_state);
  632. drm_crtc_cleanup(crtc);
  633. kfree(psb_intel_crtc);
  634. }
  635. const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
  636. .dpms = gma_crtc_dpms,
  637. .mode_fixup = gma_crtc_mode_fixup,
  638. .mode_set = psb_intel_crtc_mode_set,
  639. .mode_set_base = gma_pipe_set_base,
  640. .prepare = gma_crtc_prepare,
  641. .commit = gma_crtc_commit,
  642. .disable = gma_crtc_disable,
  643. };
  644. const struct drm_crtc_funcs psb_intel_crtc_funcs = {
  645. .save = psb_intel_crtc_save,
  646. .restore = psb_intel_crtc_restore,
  647. .cursor_set = psb_intel_crtc_cursor_set,
  648. .cursor_move = psb_intel_crtc_cursor_move,
  649. .gamma_set = gma_crtc_gamma_set,
  650. .set_config = psb_crtc_set_config,
  651. .destroy = psb_intel_crtc_destroy,
  652. };
  653. const struct gma_clock_funcs psb_clock_funcs = {
  654. .clock = psb_intel_clock,
  655. .limit = psb_intel_limit,
  656. .pll_is_valid = gma_pll_is_valid,
  657. };
  658. /*
  659. * Set the default value of cursor control and base register
  660. * to zero. This is a workaround for h/w defect on Oaktrail
  661. */
  662. static void psb_intel_cursor_init(struct drm_device *dev,
  663. struct psb_intel_crtc *psb_intel_crtc)
  664. {
  665. struct drm_psb_private *dev_priv = dev->dev_private;
  666. u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR };
  667. u32 base[3] = { CURABASE, CURBBASE, CURCBASE };
  668. struct gtt_range *cursor_gt;
  669. if (dev_priv->ops->cursor_needs_phys) {
  670. /* Allocate 4 pages of stolen mem for a hardware cursor. That
  671. * is enough for the 64 x 64 ARGB cursors we support.
  672. */
  673. cursor_gt = psb_gtt_alloc_range(dev, 4 * PAGE_SIZE, "cursor", 1);
  674. if (!cursor_gt) {
  675. psb_intel_crtc->cursor_gt = NULL;
  676. goto out;
  677. }
  678. psb_intel_crtc->cursor_gt = cursor_gt;
  679. psb_intel_crtc->cursor_addr = dev_priv->stolen_base +
  680. cursor_gt->offset;
  681. } else {
  682. psb_intel_crtc->cursor_gt = NULL;
  683. }
  684. out:
  685. REG_WRITE(control[psb_intel_crtc->pipe], 0);
  686. REG_WRITE(base[psb_intel_crtc->pipe], 0);
  687. }
  688. void psb_intel_crtc_init(struct drm_device *dev, int pipe,
  689. struct psb_intel_mode_device *mode_dev)
  690. {
  691. struct drm_psb_private *dev_priv = dev->dev_private;
  692. struct psb_intel_crtc *psb_intel_crtc;
  693. int i;
  694. uint16_t *r_base, *g_base, *b_base;
  695. /* We allocate a extra array of drm_connector pointers
  696. * for fbdev after the crtc */
  697. psb_intel_crtc =
  698. kzalloc(sizeof(struct psb_intel_crtc) +
  699. (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
  700. GFP_KERNEL);
  701. if (psb_intel_crtc == NULL)
  702. return;
  703. psb_intel_crtc->crtc_state =
  704. kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
  705. if (!psb_intel_crtc->crtc_state) {
  706. dev_err(dev->dev, "Crtc state error: No memory\n");
  707. kfree(psb_intel_crtc);
  708. return;
  709. }
  710. /* Set the CRTC operations from the chip specific data */
  711. drm_crtc_init(dev, &psb_intel_crtc->base, dev_priv->ops->crtc_funcs);
  712. /* Set the CRTC clock functions from chip specific data */
  713. psb_intel_crtc->clock_funcs = dev_priv->ops->clock_funcs;
  714. drm_mode_crtc_set_gamma_size(&psb_intel_crtc->base, 256);
  715. psb_intel_crtc->pipe = pipe;
  716. psb_intel_crtc->plane = pipe;
  717. r_base = psb_intel_crtc->base.gamma_store;
  718. g_base = r_base + 256;
  719. b_base = g_base + 256;
  720. for (i = 0; i < 256; i++) {
  721. psb_intel_crtc->lut_r[i] = i;
  722. psb_intel_crtc->lut_g[i] = i;
  723. psb_intel_crtc->lut_b[i] = i;
  724. r_base[i] = i << 8;
  725. g_base[i] = i << 8;
  726. b_base[i] = i << 8;
  727. psb_intel_crtc->lut_adj[i] = 0;
  728. }
  729. psb_intel_crtc->mode_dev = mode_dev;
  730. psb_intel_crtc->cursor_addr = 0;
  731. drm_crtc_helper_add(&psb_intel_crtc->base,
  732. dev_priv->ops->crtc_helper);
  733. /* Setup the array of drm_connector pointer array */
  734. psb_intel_crtc->mode_set.crtc = &psb_intel_crtc->base;
  735. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  736. dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] != NULL);
  737. dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] =
  738. &psb_intel_crtc->base;
  739. dev_priv->pipe_to_crtc_mapping[psb_intel_crtc->pipe] =
  740. &psb_intel_crtc->base;
  741. psb_intel_crtc->mode_set.connectors =
  742. (struct drm_connector **) (psb_intel_crtc + 1);
  743. psb_intel_crtc->mode_set.num_connectors = 0;
  744. psb_intel_cursor_init(dev, psb_intel_crtc);
  745. /* Set to true so that the pipe is forced off on initial config. */
  746. psb_intel_crtc->active = true;
  747. }
  748. int psb_intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  749. struct drm_file *file_priv)
  750. {
  751. struct drm_psb_private *dev_priv = dev->dev_private;
  752. struct drm_psb_get_pipe_from_crtc_id_arg *pipe_from_crtc_id = data;
  753. struct drm_mode_object *drmmode_obj;
  754. struct psb_intel_crtc *crtc;
  755. if (!dev_priv) {
  756. dev_err(dev->dev, "called with no initialization\n");
  757. return -EINVAL;
  758. }
  759. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  760. DRM_MODE_OBJECT_CRTC);
  761. if (!drmmode_obj) {
  762. dev_err(dev->dev, "no such CRTC id\n");
  763. return -EINVAL;
  764. }
  765. crtc = to_psb_intel_crtc(obj_to_crtc(drmmode_obj));
  766. pipe_from_crtc_id->pipe = crtc->pipe;
  767. return 0;
  768. }
  769. struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  770. {
  771. struct drm_crtc *crtc = NULL;
  772. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  773. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  774. if (psb_intel_crtc->pipe == pipe)
  775. break;
  776. }
  777. return crtc;
  778. }
  779. int psb_intel_connector_clones(struct drm_device *dev, int type_mask)
  780. {
  781. int index_mask = 0;
  782. struct drm_connector *connector;
  783. int entry = 0;
  784. list_for_each_entry(connector, &dev->mode_config.connector_list,
  785. head) {
  786. struct psb_intel_encoder *psb_intel_encoder =
  787. psb_intel_attached_encoder(connector);
  788. if (type_mask & (1 << psb_intel_encoder->type))
  789. index_mask |= (1 << entry);
  790. entry++;
  791. }
  792. return index_mask;
  793. }
  794. /* current intel driver doesn't take advantage of encoders
  795. always give back the encoder for the connector
  796. */
  797. struct drm_encoder *psb_intel_best_encoder(struct drm_connector *connector)
  798. {
  799. struct psb_intel_encoder *psb_intel_encoder =
  800. psb_intel_attached_encoder(connector);
  801. return &psb_intel_encoder->base;
  802. }
  803. void psb_intel_connector_attach_encoder(struct psb_intel_connector *connector,
  804. struct psb_intel_encoder *encoder)
  805. {
  806. connector->encoder = encoder;
  807. drm_mode_connector_attach_encoder(&connector->base,
  808. &encoder->base);
  809. }