intel_display.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/i2c.h>
  27. #include "drmP.h"
  28. #include "intel_drv.h"
  29. #include "i915_drm.h"
  30. #include "i915_drv.h"
  31. #include "drm_crtc_helper.h"
  32. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  33. typedef struct {
  34. /* given values */
  35. int n;
  36. int m1, m2;
  37. int p1, p2;
  38. /* derived values */
  39. int dot;
  40. int vco;
  41. int m;
  42. int p;
  43. } intel_clock_t;
  44. typedef struct {
  45. int min, max;
  46. } intel_range_t;
  47. typedef struct {
  48. int dot_limit;
  49. int p2_slow, p2_fast;
  50. } intel_p2_t;
  51. #define INTEL_P2_NUM 2
  52. typedef struct {
  53. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  54. intel_p2_t p2;
  55. } intel_limit_t;
  56. #define I8XX_DOT_MIN 25000
  57. #define I8XX_DOT_MAX 350000
  58. #define I8XX_VCO_MIN 930000
  59. #define I8XX_VCO_MAX 1400000
  60. #define I8XX_N_MIN 3
  61. #define I8XX_N_MAX 16
  62. #define I8XX_M_MIN 96
  63. #define I8XX_M_MAX 140
  64. #define I8XX_M1_MIN 18
  65. #define I8XX_M1_MAX 26
  66. #define I8XX_M2_MIN 6
  67. #define I8XX_M2_MAX 16
  68. #define I8XX_P_MIN 4
  69. #define I8XX_P_MAX 128
  70. #define I8XX_P1_MIN 2
  71. #define I8XX_P1_MAX 33
  72. #define I8XX_P1_LVDS_MIN 1
  73. #define I8XX_P1_LVDS_MAX 6
  74. #define I8XX_P2_SLOW 4
  75. #define I8XX_P2_FAST 2
  76. #define I8XX_P2_LVDS_SLOW 14
  77. #define I8XX_P2_LVDS_FAST 14 /* No fast option */
  78. #define I8XX_P2_SLOW_LIMIT 165000
  79. #define I9XX_DOT_MIN 20000
  80. #define I9XX_DOT_MAX 400000
  81. #define I9XX_VCO_MIN 1400000
  82. #define I9XX_VCO_MAX 2800000
  83. #define I9XX_N_MIN 3
  84. #define I9XX_N_MAX 8
  85. #define I9XX_M_MIN 70
  86. #define I9XX_M_MAX 120
  87. #define I9XX_M1_MIN 10
  88. #define I9XX_M1_MAX 20
  89. #define I9XX_M2_MIN 5
  90. #define I9XX_M2_MAX 9
  91. #define I9XX_P_SDVO_DAC_MIN 5
  92. #define I9XX_P_SDVO_DAC_MAX 80
  93. #define I9XX_P_LVDS_MIN 7
  94. #define I9XX_P_LVDS_MAX 98
  95. #define I9XX_P1_MIN 1
  96. #define I9XX_P1_MAX 8
  97. #define I9XX_P2_SDVO_DAC_SLOW 10
  98. #define I9XX_P2_SDVO_DAC_FAST 5
  99. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  100. #define I9XX_P2_LVDS_SLOW 14
  101. #define I9XX_P2_LVDS_FAST 7
  102. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  103. #define INTEL_LIMIT_I8XX_DVO_DAC 0
  104. #define INTEL_LIMIT_I8XX_LVDS 1
  105. #define INTEL_LIMIT_I9XX_SDVO_DAC 2
  106. #define INTEL_LIMIT_I9XX_LVDS 3
  107. static const intel_limit_t intel_limits[] = {
  108. { /* INTEL_LIMIT_I8XX_DVO_DAC */
  109. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  110. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  111. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  112. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  113. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  114. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  115. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  116. .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
  117. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  118. .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
  119. },
  120. { /* INTEL_LIMIT_I8XX_LVDS */
  121. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  122. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  123. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  124. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  125. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  126. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  127. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  128. .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
  129. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  130. .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
  131. },
  132. { /* INTEL_LIMIT_I9XX_SDVO_DAC */
  133. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  134. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  135. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  136. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  137. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  138. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  139. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  140. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  141. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  142. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  143. },
  144. { /* INTEL_LIMIT_I9XX_LVDS */
  145. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  146. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  147. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  148. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  149. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  150. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  151. .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
  152. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  153. /* The single-channel range is 25-112Mhz, and dual-channel
  154. * is 80-224Mhz. Prefer single channel as much as possible.
  155. */
  156. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  157. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
  158. },
  159. };
  160. static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
  161. {
  162. struct drm_device *dev = crtc->dev;
  163. const intel_limit_t *limit;
  164. if (IS_I9XX(dev)) {
  165. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  166. limit = &intel_limits[INTEL_LIMIT_I9XX_LVDS];
  167. else
  168. limit = &intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
  169. } else {
  170. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  171. limit = &intel_limits[INTEL_LIMIT_I8XX_LVDS];
  172. else
  173. limit = &intel_limits[INTEL_LIMIT_I8XX_DVO_DAC];
  174. }
  175. return limit;
  176. }
  177. /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
  178. static void i8xx_clock(int refclk, intel_clock_t *clock)
  179. {
  180. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  181. clock->p = clock->p1 * clock->p2;
  182. clock->vco = refclk * clock->m / (clock->n + 2);
  183. clock->dot = clock->vco / clock->p;
  184. }
  185. /** Derive the pixel clock for the given refclk and divisors for 9xx chips. */
  186. static void i9xx_clock(int refclk, intel_clock_t *clock)
  187. {
  188. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  189. clock->p = clock->p1 * clock->p2;
  190. clock->vco = refclk * clock->m / (clock->n + 2);
  191. clock->dot = clock->vco / clock->p;
  192. }
  193. static void intel_clock(struct drm_device *dev, int refclk,
  194. intel_clock_t *clock)
  195. {
  196. if (IS_I9XX(dev))
  197. i9xx_clock (refclk, clock);
  198. else
  199. i8xx_clock (refclk, clock);
  200. }
  201. /**
  202. * Returns whether any output on the specified pipe is of the specified type
  203. */
  204. bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
  205. {
  206. struct drm_device *dev = crtc->dev;
  207. struct drm_mode_config *mode_config = &dev->mode_config;
  208. struct drm_connector *l_entry;
  209. list_for_each_entry(l_entry, &mode_config->connector_list, head) {
  210. if (l_entry->encoder &&
  211. l_entry->encoder->crtc == crtc) {
  212. struct intel_output *intel_output = to_intel_output(l_entry);
  213. if (intel_output->type == type)
  214. return true;
  215. }
  216. }
  217. return false;
  218. }
  219. #define INTELPllInvalid(s) { /* ErrorF (s) */; return false; }
  220. /**
  221. * Returns whether the given set of divisors are valid for a given refclk with
  222. * the given connectors.
  223. */
  224. static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
  225. {
  226. const intel_limit_t *limit = intel_limit (crtc);
  227. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  228. INTELPllInvalid ("p1 out of range\n");
  229. if (clock->p < limit->p.min || limit->p.max < clock->p)
  230. INTELPllInvalid ("p out of range\n");
  231. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  232. INTELPllInvalid ("m2 out of range\n");
  233. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  234. INTELPllInvalid ("m1 out of range\n");
  235. if (clock->m1 <= clock->m2)
  236. INTELPllInvalid ("m1 <= m2\n");
  237. if (clock->m < limit->m.min || limit->m.max < clock->m)
  238. INTELPllInvalid ("m out of range\n");
  239. if (clock->n < limit->n.min || limit->n.max < clock->n)
  240. INTELPllInvalid ("n out of range\n");
  241. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  242. INTELPllInvalid ("vco out of range\n");
  243. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  244. * connector, etc., rather than just a single range.
  245. */
  246. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  247. INTELPllInvalid ("dot out of range\n");
  248. return true;
  249. }
  250. /**
  251. * Returns a set of divisors for the desired target clock with the given
  252. * refclk, or FALSE. The returned values represent the clock equation:
  253. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  254. */
  255. static bool intel_find_best_PLL(struct drm_crtc *crtc, int target,
  256. int refclk, intel_clock_t *best_clock)
  257. {
  258. struct drm_device *dev = crtc->dev;
  259. struct drm_i915_private *dev_priv = dev->dev_private;
  260. intel_clock_t clock;
  261. const intel_limit_t *limit = intel_limit(crtc);
  262. int err = target;
  263. if (IS_I9XX(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  264. (I915_READ(LVDS) & LVDS_PORT_EN) != 0) {
  265. /*
  266. * For LVDS, if the panel is on, just rely on its current
  267. * settings for dual-channel. We haven't figured out how to
  268. * reliably set up different single/dual channel state, if we
  269. * even can.
  270. */
  271. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  272. LVDS_CLKB_POWER_UP)
  273. clock.p2 = limit->p2.p2_fast;
  274. else
  275. clock.p2 = limit->p2.p2_slow;
  276. } else {
  277. if (target < limit->p2.dot_limit)
  278. clock.p2 = limit->p2.p2_slow;
  279. else
  280. clock.p2 = limit->p2.p2_fast;
  281. }
  282. memset (best_clock, 0, sizeof (*best_clock));
  283. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  284. for (clock.m2 = limit->m2.min; clock.m2 < clock.m1 &&
  285. clock.m2 <= limit->m2.max; clock.m2++) {
  286. for (clock.n = limit->n.min; clock.n <= limit->n.max;
  287. clock.n++) {
  288. for (clock.p1 = limit->p1.min;
  289. clock.p1 <= limit->p1.max; clock.p1++) {
  290. int this_err;
  291. intel_clock(dev, refclk, &clock);
  292. if (!intel_PLL_is_valid(crtc, &clock))
  293. continue;
  294. this_err = abs(clock.dot - target);
  295. if (this_err < err) {
  296. *best_clock = clock;
  297. err = this_err;
  298. }
  299. }
  300. }
  301. }
  302. }
  303. return (err != target);
  304. }
  305. void
  306. intel_wait_for_vblank(struct drm_device *dev)
  307. {
  308. /* Wait for 20ms, i.e. one cycle at 50hz. */
  309. udelay(20000);
  310. }
  311. static void
  312. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y)
  313. {
  314. struct drm_device *dev = crtc->dev;
  315. struct drm_i915_private *dev_priv = dev->dev_private;
  316. struct drm_i915_master_private *master_priv;
  317. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  318. struct intel_framebuffer *intel_fb;
  319. struct drm_i915_gem_object *obj_priv;
  320. struct drm_gem_object *obj;
  321. int pipe = intel_crtc->pipe;
  322. unsigned long Start, Offset;
  323. int dspbase = (pipe == 0 ? DSPAADDR : DSPBADDR);
  324. int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
  325. int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
  326. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  327. u32 dspcntr;
  328. /* no fb bound */
  329. if (!crtc->fb) {
  330. DRM_DEBUG("No FB bound\n");
  331. return;
  332. }
  333. intel_fb = to_intel_framebuffer(crtc->fb);
  334. obj = intel_fb->obj;
  335. obj_priv = obj->driver_private;
  336. Start = obj_priv->gtt_offset;
  337. Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
  338. I915_WRITE(dspstride, crtc->fb->pitch);
  339. dspcntr = I915_READ(dspcntr_reg);
  340. switch (crtc->fb->bits_per_pixel) {
  341. case 8:
  342. dspcntr |= DISPPLANE_8BPP;
  343. break;
  344. case 16:
  345. if (crtc->fb->depth == 15)
  346. dspcntr |= DISPPLANE_15_16BPP;
  347. else
  348. dspcntr |= DISPPLANE_16BPP;
  349. break;
  350. case 24:
  351. case 32:
  352. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  353. break;
  354. default:
  355. DRM_ERROR("Unknown color depth\n");
  356. return;
  357. }
  358. I915_WRITE(dspcntr_reg, dspcntr);
  359. DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
  360. if (IS_I965G(dev)) {
  361. I915_WRITE(dspbase, Offset);
  362. I915_READ(dspbase);
  363. I915_WRITE(dspsurf, Start);
  364. I915_READ(dspsurf);
  365. } else {
  366. I915_WRITE(dspbase, Start + Offset);
  367. I915_READ(dspbase);
  368. }
  369. if (!dev->primary->master)
  370. return;
  371. master_priv = dev->primary->master->driver_priv;
  372. if (!master_priv->sarea_priv)
  373. return;
  374. switch (pipe) {
  375. case 0:
  376. master_priv->sarea_priv->pipeA_x = x;
  377. master_priv->sarea_priv->pipeA_y = y;
  378. break;
  379. case 1:
  380. master_priv->sarea_priv->pipeB_x = x;
  381. master_priv->sarea_priv->pipeB_y = y;
  382. break;
  383. default:
  384. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  385. break;
  386. }
  387. }
  388. /**
  389. * Sets the power management mode of the pipe and plane.
  390. *
  391. * This code should probably grow support for turning the cursor off and back
  392. * on appropriately at the same time as we're turning the pipe off/on.
  393. */
  394. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  395. {
  396. struct drm_device *dev = crtc->dev;
  397. struct drm_i915_master_private *master_priv;
  398. struct drm_i915_private *dev_priv = dev->dev_private;
  399. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  400. int pipe = intel_crtc->pipe;
  401. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  402. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  403. int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
  404. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  405. u32 temp;
  406. bool enabled;
  407. /* XXX: When our outputs are all unaware of DPMS modes other than off
  408. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  409. */
  410. switch (mode) {
  411. case DRM_MODE_DPMS_ON:
  412. case DRM_MODE_DPMS_STANDBY:
  413. case DRM_MODE_DPMS_SUSPEND:
  414. /* Enable the DPLL */
  415. temp = I915_READ(dpll_reg);
  416. if ((temp & DPLL_VCO_ENABLE) == 0) {
  417. I915_WRITE(dpll_reg, temp);
  418. I915_READ(dpll_reg);
  419. /* Wait for the clocks to stabilize. */
  420. udelay(150);
  421. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  422. I915_READ(dpll_reg);
  423. /* Wait for the clocks to stabilize. */
  424. udelay(150);
  425. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  426. I915_READ(dpll_reg);
  427. /* Wait for the clocks to stabilize. */
  428. udelay(150);
  429. }
  430. /* Enable the pipe */
  431. temp = I915_READ(pipeconf_reg);
  432. if ((temp & PIPEACONF_ENABLE) == 0)
  433. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  434. /* Enable the plane */
  435. temp = I915_READ(dspcntr_reg);
  436. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  437. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  438. /* Flush the plane changes */
  439. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  440. }
  441. intel_crtc_load_lut(crtc);
  442. /* Give the overlay scaler a chance to enable if it's on this pipe */
  443. //intel_crtc_dpms_video(crtc, true); TODO
  444. break;
  445. case DRM_MODE_DPMS_OFF:
  446. /* Give the overlay scaler a chance to disable if it's on this pipe */
  447. //intel_crtc_dpms_video(crtc, FALSE); TODO
  448. /* Disable the VGA plane that we never use */
  449. I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  450. /* Disable display plane */
  451. temp = I915_READ(dspcntr_reg);
  452. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  453. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  454. /* Flush the plane changes */
  455. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  456. I915_READ(dspbase_reg);
  457. }
  458. if (!IS_I9XX(dev)) {
  459. /* Wait for vblank for the disable to take effect */
  460. intel_wait_for_vblank(dev);
  461. }
  462. /* Next, disable display pipes */
  463. temp = I915_READ(pipeconf_reg);
  464. if ((temp & PIPEACONF_ENABLE) != 0) {
  465. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  466. I915_READ(pipeconf_reg);
  467. }
  468. /* Wait for vblank for the disable to take effect. */
  469. intel_wait_for_vblank(dev);
  470. temp = I915_READ(dpll_reg);
  471. if ((temp & DPLL_VCO_ENABLE) != 0) {
  472. I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  473. I915_READ(dpll_reg);
  474. }
  475. /* Wait for the clocks to turn off. */
  476. udelay(150);
  477. break;
  478. }
  479. if (!dev->primary->master)
  480. return;
  481. master_priv = dev->primary->master->driver_priv;
  482. if (!master_priv->sarea_priv)
  483. return;
  484. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  485. switch (pipe) {
  486. case 0:
  487. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  488. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  489. break;
  490. case 1:
  491. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  492. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  493. break;
  494. default:
  495. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  496. break;
  497. }
  498. intel_crtc->dpms_mode = mode;
  499. }
  500. static void intel_crtc_prepare (struct drm_crtc *crtc)
  501. {
  502. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  503. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  504. }
  505. static void intel_crtc_commit (struct drm_crtc *crtc)
  506. {
  507. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  508. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  509. }
  510. void intel_encoder_prepare (struct drm_encoder *encoder)
  511. {
  512. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  513. /* lvds has its own version of prepare see intel_lvds_prepare */
  514. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  515. }
  516. void intel_encoder_commit (struct drm_encoder *encoder)
  517. {
  518. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  519. /* lvds has its own version of commit see intel_lvds_commit */
  520. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  521. }
  522. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  523. struct drm_display_mode *mode,
  524. struct drm_display_mode *adjusted_mode)
  525. {
  526. return true;
  527. }
  528. /** Returns the core display clock speed for i830 - i945 */
  529. static int intel_get_core_clock_speed(struct drm_device *dev)
  530. {
  531. /* Core clock values taken from the published datasheets.
  532. * The 830 may go up to 166 Mhz, which we should check.
  533. */
  534. if (IS_I945G(dev))
  535. return 400000;
  536. else if (IS_I915G(dev))
  537. return 333000;
  538. else if (IS_I945GM(dev) || IS_845G(dev))
  539. return 200000;
  540. else if (IS_I915GM(dev)) {
  541. u16 gcfgc = 0;
  542. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  543. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  544. return 133000;
  545. else {
  546. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  547. case GC_DISPLAY_CLOCK_333_MHZ:
  548. return 333000;
  549. default:
  550. case GC_DISPLAY_CLOCK_190_200_MHZ:
  551. return 190000;
  552. }
  553. }
  554. } else if (IS_I865G(dev))
  555. return 266000;
  556. else if (IS_I855(dev)) {
  557. u16 hpllcc = 0;
  558. /* Assume that the hardware is in the high speed state. This
  559. * should be the default.
  560. */
  561. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  562. case GC_CLOCK_133_200:
  563. case GC_CLOCK_100_200:
  564. return 200000;
  565. case GC_CLOCK_166_250:
  566. return 250000;
  567. case GC_CLOCK_100_133:
  568. return 133000;
  569. }
  570. } else /* 852, 830 */
  571. return 133000;
  572. return 0; /* Silence gcc warning */
  573. }
  574. /**
  575. * Return the pipe currently connected to the panel fitter,
  576. * or -1 if the panel fitter is not present or not in use
  577. */
  578. static int intel_panel_fitter_pipe (struct drm_device *dev)
  579. {
  580. struct drm_i915_private *dev_priv = dev->dev_private;
  581. u32 pfit_control;
  582. /* i830 doesn't have a panel fitter */
  583. if (IS_I830(dev))
  584. return -1;
  585. pfit_control = I915_READ(PFIT_CONTROL);
  586. /* See if the panel fitter is in use */
  587. if ((pfit_control & PFIT_ENABLE) == 0)
  588. return -1;
  589. /* 965 can place panel fitter on either pipe */
  590. if (IS_I965G(dev))
  591. return (pfit_control >> 29) & 0x3;
  592. /* older chips can only use pipe 1 */
  593. return 1;
  594. }
  595. static void intel_crtc_mode_set(struct drm_crtc *crtc,
  596. struct drm_display_mode *mode,
  597. struct drm_display_mode *adjusted_mode,
  598. int x, int y)
  599. {
  600. struct drm_device *dev = crtc->dev;
  601. struct drm_i915_private *dev_priv = dev->dev_private;
  602. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  603. int pipe = intel_crtc->pipe;
  604. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  605. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  606. int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  607. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  608. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  609. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  610. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  611. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  612. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  613. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  614. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  615. int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
  616. int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
  617. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  618. int refclk;
  619. intel_clock_t clock;
  620. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  621. bool ok, is_sdvo = false, is_dvo = false;
  622. bool is_crt = false, is_lvds = false, is_tv = false;
  623. struct drm_mode_config *mode_config = &dev->mode_config;
  624. struct drm_connector *connector;
  625. drm_vblank_pre_modeset(dev, pipe);
  626. list_for_each_entry(connector, &mode_config->connector_list, head) {
  627. struct intel_output *intel_output = to_intel_output(connector);
  628. if (!connector->encoder || connector->encoder->crtc != crtc)
  629. continue;
  630. switch (intel_output->type) {
  631. case INTEL_OUTPUT_LVDS:
  632. is_lvds = true;
  633. break;
  634. case INTEL_OUTPUT_SDVO:
  635. is_sdvo = true;
  636. break;
  637. case INTEL_OUTPUT_DVO:
  638. is_dvo = true;
  639. break;
  640. case INTEL_OUTPUT_TVOUT:
  641. is_tv = true;
  642. break;
  643. case INTEL_OUTPUT_ANALOG:
  644. is_crt = true;
  645. break;
  646. }
  647. }
  648. if (IS_I9XX(dev)) {
  649. refclk = 96000;
  650. } else {
  651. refclk = 48000;
  652. }
  653. ok = intel_find_best_PLL(crtc, adjusted_mode->clock, refclk, &clock);
  654. if (!ok) {
  655. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  656. return;
  657. }
  658. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  659. dpll = DPLL_VGA_MODE_DIS;
  660. if (IS_I9XX(dev)) {
  661. if (is_lvds)
  662. dpll |= DPLLB_MODE_LVDS;
  663. else
  664. dpll |= DPLLB_MODE_DAC_SERIAL;
  665. if (is_sdvo) {
  666. dpll |= DPLL_DVO_HIGH_SPEED;
  667. if (IS_I945G(dev) || IS_I945GM(dev)) {
  668. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  669. dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  670. }
  671. }
  672. /* compute bitmask from p1 value */
  673. dpll |= (1 << (clock.p1 - 1)) << 16;
  674. switch (clock.p2) {
  675. case 5:
  676. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  677. break;
  678. case 7:
  679. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  680. break;
  681. case 10:
  682. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  683. break;
  684. case 14:
  685. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  686. break;
  687. }
  688. if (IS_I965G(dev))
  689. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  690. } else {
  691. if (is_lvds) {
  692. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  693. } else {
  694. if (clock.p1 == 2)
  695. dpll |= PLL_P1_DIVIDE_BY_TWO;
  696. else
  697. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  698. if (clock.p2 == 4)
  699. dpll |= PLL_P2_DIVIDE_BY_4;
  700. }
  701. }
  702. if (is_tv) {
  703. /* XXX: just matching BIOS for now */
  704. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  705. dpll |= 3;
  706. }
  707. else
  708. dpll |= PLL_REF_INPUT_DREFCLK;
  709. /* setup pipeconf */
  710. pipeconf = I915_READ(pipeconf_reg);
  711. /* Set up the display plane register */
  712. dspcntr = DISPPLANE_GAMMA_ENABLE;
  713. if (pipe == 0)
  714. dspcntr |= DISPPLANE_SEL_PIPE_A;
  715. else
  716. dspcntr |= DISPPLANE_SEL_PIPE_B;
  717. if (pipe == 0 && !IS_I965G(dev)) {
  718. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  719. * core speed.
  720. *
  721. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  722. * pipe == 0 check?
  723. */
  724. if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
  725. pipeconf |= PIPEACONF_DOUBLE_WIDE;
  726. else
  727. pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
  728. }
  729. dspcntr |= DISPLAY_PLANE_ENABLE;
  730. pipeconf |= PIPEACONF_ENABLE;
  731. dpll |= DPLL_VCO_ENABLE;
  732. /* Disable the panel fitter if it was on our pipe */
  733. if (intel_panel_fitter_pipe(dev) == pipe)
  734. I915_WRITE(PFIT_CONTROL, 0);
  735. DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  736. drm_mode_debug_printmodeline(mode);
  737. if (dpll & DPLL_VCO_ENABLE) {
  738. I915_WRITE(fp_reg, fp);
  739. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  740. I915_READ(dpll_reg);
  741. udelay(150);
  742. }
  743. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  744. * This is an exception to the general rule that mode_set doesn't turn
  745. * things on.
  746. */
  747. if (is_lvds) {
  748. u32 lvds = I915_READ(LVDS);
  749. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
  750. /* Set the B0-B3 data pairs corresponding to whether we're going to
  751. * set the DPLLs for dual-channel mode or not.
  752. */
  753. if (clock.p2 == 7)
  754. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  755. else
  756. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  757. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  758. * appropriately here, but we need to look more thoroughly into how
  759. * panels behave in the two modes.
  760. */
  761. I915_WRITE(LVDS, lvds);
  762. I915_READ(LVDS);
  763. }
  764. I915_WRITE(fp_reg, fp);
  765. I915_WRITE(dpll_reg, dpll);
  766. I915_READ(dpll_reg);
  767. /* Wait for the clocks to stabilize. */
  768. udelay(150);
  769. if (IS_I965G(dev)) {
  770. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  771. I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
  772. ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  773. } else {
  774. /* write it again -- the BIOS does, after all */
  775. I915_WRITE(dpll_reg, dpll);
  776. }
  777. I915_READ(dpll_reg);
  778. /* Wait for the clocks to stabilize. */
  779. udelay(150);
  780. I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  781. ((adjusted_mode->crtc_htotal - 1) << 16));
  782. I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  783. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  784. I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  785. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  786. I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  787. ((adjusted_mode->crtc_vtotal - 1) << 16));
  788. I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  789. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  790. I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  791. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  792. /* pipesrc and dspsize control the size that is scaled from, which should
  793. * always be the user's requested size.
  794. */
  795. I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  796. I915_WRITE(dsppos_reg, 0);
  797. I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  798. I915_WRITE(pipeconf_reg, pipeconf);
  799. I915_READ(pipeconf_reg);
  800. intel_wait_for_vblank(dev);
  801. I915_WRITE(dspcntr_reg, dspcntr);
  802. /* Flush the plane changes */
  803. intel_pipe_set_base(crtc, x, y);
  804. intel_wait_for_vblank(dev);
  805. drm_vblank_post_modeset(dev, pipe);
  806. }
  807. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  808. void intel_crtc_load_lut(struct drm_crtc *crtc)
  809. {
  810. struct drm_device *dev = crtc->dev;
  811. struct drm_i915_private *dev_priv = dev->dev_private;
  812. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  813. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  814. int i;
  815. /* The clocks have to be on to load the palette. */
  816. if (!crtc->enabled)
  817. return;
  818. for (i = 0; i < 256; i++) {
  819. I915_WRITE(palreg + 4 * i,
  820. (intel_crtc->lut_r[i] << 16) |
  821. (intel_crtc->lut_g[i] << 8) |
  822. intel_crtc->lut_b[i]);
  823. }
  824. }
  825. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  826. struct drm_file *file_priv,
  827. uint32_t handle,
  828. uint32_t width, uint32_t height)
  829. {
  830. struct drm_device *dev = crtc->dev;
  831. struct drm_i915_private *dev_priv = dev->dev_private;
  832. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  833. struct drm_gem_object *bo;
  834. struct drm_i915_gem_object *obj_priv;
  835. int pipe = intel_crtc->pipe;
  836. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  837. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  838. uint32_t temp;
  839. size_t addr;
  840. DRM_DEBUG("\n");
  841. /* if we want to turn off the cursor ignore width and height */
  842. if (!handle) {
  843. DRM_DEBUG("cursor off\n");
  844. /* turn of the cursor */
  845. temp = 0;
  846. temp |= CURSOR_MODE_DISABLE;
  847. I915_WRITE(control, temp);
  848. I915_WRITE(base, 0);
  849. return 0;
  850. }
  851. /* Currently we only support 64x64 cursors */
  852. if (width != 64 || height != 64) {
  853. DRM_ERROR("we currently only support 64x64 cursors\n");
  854. return -EINVAL;
  855. }
  856. bo = drm_gem_object_lookup(dev, file_priv, handle);
  857. if (!bo)
  858. return -ENOENT;
  859. obj_priv = bo->driver_private;
  860. if (bo->size < width * height * 4) {
  861. DRM_ERROR("buffer is to small\n");
  862. drm_gem_object_unreference(bo);
  863. return -ENOMEM;
  864. }
  865. if (dev_priv->cursor_needs_physical) {
  866. addr = dev->agp->base + obj_priv->gtt_offset;
  867. } else {
  868. addr = obj_priv->gtt_offset;
  869. }
  870. intel_crtc->cursor_addr = addr;
  871. temp = 0;
  872. /* set the pipe for the cursor */
  873. temp |= (pipe << 28);
  874. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  875. I915_WRITE(control, temp);
  876. I915_WRITE(base, addr);
  877. return 0;
  878. }
  879. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  880. {
  881. struct drm_device *dev = crtc->dev;
  882. struct drm_i915_private *dev_priv = dev->dev_private;
  883. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  884. int pipe = intel_crtc->pipe;
  885. uint32_t temp = 0;
  886. uint32_t adder;
  887. if (x < 0) {
  888. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  889. x = -x;
  890. }
  891. if (y < 0) {
  892. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  893. y = -y;
  894. }
  895. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  896. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  897. adder = intel_crtc->cursor_addr;
  898. I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  899. I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  900. return 0;
  901. }
  902. /** Sets the color ramps on behalf of RandR */
  903. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  904. u16 blue, int regno)
  905. {
  906. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  907. intel_crtc->lut_r[regno] = red >> 8;
  908. intel_crtc->lut_g[regno] = green >> 8;
  909. intel_crtc->lut_b[regno] = blue >> 8;
  910. }
  911. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  912. u16 *blue, uint32_t size)
  913. {
  914. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  915. int i;
  916. if (size != 256)
  917. return;
  918. for (i = 0; i < 256; i++) {
  919. intel_crtc->lut_r[i] = red[i] >> 8;
  920. intel_crtc->lut_g[i] = green[i] >> 8;
  921. intel_crtc->lut_b[i] = blue[i] >> 8;
  922. }
  923. intel_crtc_load_lut(crtc);
  924. }
  925. /**
  926. * Get a pipe with a simple mode set on it for doing load-based monitor
  927. * detection.
  928. *
  929. * It will be up to the load-detect code to adjust the pipe as appropriate for
  930. * its requirements. The pipe will be connected to no other outputs.
  931. *
  932. * Currently this code will only succeed if there is a pipe with no outputs
  933. * configured for it. In the future, it could choose to temporarily disable
  934. * some outputs to free up a pipe for its use.
  935. *
  936. * \return crtc, or NULL if no pipes are available.
  937. */
  938. /* VESA 640x480x72Hz mode to set on the pipe */
  939. static struct drm_display_mode load_detect_mode = {
  940. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  941. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  942. };
  943. struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
  944. struct drm_display_mode *mode,
  945. int *dpms_mode)
  946. {
  947. struct intel_crtc *intel_crtc;
  948. struct drm_crtc *possible_crtc;
  949. struct drm_crtc *supported_crtc =NULL;
  950. struct drm_encoder *encoder = &intel_output->enc;
  951. struct drm_crtc *crtc = NULL;
  952. struct drm_device *dev = encoder->dev;
  953. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  954. struct drm_crtc_helper_funcs *crtc_funcs;
  955. int i = -1;
  956. /*
  957. * Algorithm gets a little messy:
  958. * - if the connector already has an assigned crtc, use it (but make
  959. * sure it's on first)
  960. * - try to find the first unused crtc that can drive this connector,
  961. * and use that if we find one
  962. * - if there are no unused crtcs available, try to use the first
  963. * one we found that supports the connector
  964. */
  965. /* See if we already have a CRTC for this connector */
  966. if (encoder->crtc) {
  967. crtc = encoder->crtc;
  968. /* Make sure the crtc and connector are running */
  969. intel_crtc = to_intel_crtc(crtc);
  970. *dpms_mode = intel_crtc->dpms_mode;
  971. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  972. crtc_funcs = crtc->helper_private;
  973. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  974. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  975. }
  976. return crtc;
  977. }
  978. /* Find an unused one (if possible) */
  979. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  980. i++;
  981. if (!(encoder->possible_crtcs & (1 << i)))
  982. continue;
  983. if (!possible_crtc->enabled) {
  984. crtc = possible_crtc;
  985. break;
  986. }
  987. if (!supported_crtc)
  988. supported_crtc = possible_crtc;
  989. }
  990. /*
  991. * If we didn't find an unused CRTC, don't use any.
  992. */
  993. if (!crtc) {
  994. return NULL;
  995. }
  996. encoder->crtc = crtc;
  997. intel_output->load_detect_temp = true;
  998. intel_crtc = to_intel_crtc(crtc);
  999. *dpms_mode = intel_crtc->dpms_mode;
  1000. if (!crtc->enabled) {
  1001. if (!mode)
  1002. mode = &load_detect_mode;
  1003. drm_crtc_helper_set_mode(crtc, mode, 0, 0);
  1004. } else {
  1005. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  1006. crtc_funcs = crtc->helper_private;
  1007. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1008. }
  1009. /* Add this connector to the crtc */
  1010. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  1011. encoder_funcs->commit(encoder);
  1012. }
  1013. /* let the connector get through one full cycle before testing */
  1014. intel_wait_for_vblank(dev);
  1015. return crtc;
  1016. }
  1017. void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
  1018. {
  1019. struct drm_encoder *encoder = &intel_output->enc;
  1020. struct drm_device *dev = encoder->dev;
  1021. struct drm_crtc *crtc = encoder->crtc;
  1022. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1023. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1024. if (intel_output->load_detect_temp) {
  1025. encoder->crtc = NULL;
  1026. intel_output->load_detect_temp = false;
  1027. crtc->enabled = drm_helper_crtc_in_use(crtc);
  1028. drm_helper_disable_unused_functions(dev);
  1029. }
  1030. /* Switch crtc and output back off if necessary */
  1031. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  1032. if (encoder->crtc == crtc)
  1033. encoder_funcs->dpms(encoder, dpms_mode);
  1034. crtc_funcs->dpms(crtc, dpms_mode);
  1035. }
  1036. }
  1037. /* Returns the clock of the currently programmed mode of the given pipe. */
  1038. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  1039. {
  1040. struct drm_i915_private *dev_priv = dev->dev_private;
  1041. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1042. int pipe = intel_crtc->pipe;
  1043. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  1044. u32 fp;
  1045. intel_clock_t clock;
  1046. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1047. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  1048. else
  1049. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  1050. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  1051. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1052. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  1053. if (IS_I9XX(dev)) {
  1054. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  1055. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1056. switch (dpll & DPLL_MODE_MASK) {
  1057. case DPLLB_MODE_DAC_SERIAL:
  1058. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  1059. 5 : 10;
  1060. break;
  1061. case DPLLB_MODE_LVDS:
  1062. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  1063. 7 : 14;
  1064. break;
  1065. default:
  1066. DRM_DEBUG("Unknown DPLL mode %08x in programmed "
  1067. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  1068. return 0;
  1069. }
  1070. /* XXX: Handle the 100Mhz refclk */
  1071. i9xx_clock(96000, &clock);
  1072. } else {
  1073. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  1074. if (is_lvds) {
  1075. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  1076. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1077. clock.p2 = 14;
  1078. if ((dpll & PLL_REF_INPUT_MASK) ==
  1079. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1080. /* XXX: might not be 66MHz */
  1081. i8xx_clock(66000, &clock);
  1082. } else
  1083. i8xx_clock(48000, &clock);
  1084. } else {
  1085. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1086. clock.p1 = 2;
  1087. else {
  1088. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1089. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1090. }
  1091. if (dpll & PLL_P2_DIVIDE_BY_4)
  1092. clock.p2 = 4;
  1093. else
  1094. clock.p2 = 2;
  1095. i8xx_clock(48000, &clock);
  1096. }
  1097. }
  1098. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1099. * i830PllIsValid() because it relies on the xf86_config connector
  1100. * configuration being accurate, which it isn't necessarily.
  1101. */
  1102. return clock.dot;
  1103. }
  1104. /** Returns the currently programmed mode of the given pipe. */
  1105. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  1106. struct drm_crtc *crtc)
  1107. {
  1108. struct drm_i915_private *dev_priv = dev->dev_private;
  1109. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1110. int pipe = intel_crtc->pipe;
  1111. struct drm_display_mode *mode;
  1112. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  1113. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  1114. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  1115. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  1116. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1117. if (!mode)
  1118. return NULL;
  1119. mode->clock = intel_crtc_clock_get(dev, crtc);
  1120. mode->hdisplay = (htot & 0xffff) + 1;
  1121. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1122. mode->hsync_start = (hsync & 0xffff) + 1;
  1123. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1124. mode->vdisplay = (vtot & 0xffff) + 1;
  1125. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1126. mode->vsync_start = (vsync & 0xffff) + 1;
  1127. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1128. drm_mode_set_name(mode);
  1129. drm_mode_set_crtcinfo(mode, 0);
  1130. return mode;
  1131. }
  1132. static void intel_crtc_destroy(struct drm_crtc *crtc)
  1133. {
  1134. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1135. drm_crtc_cleanup(crtc);
  1136. kfree(intel_crtc);
  1137. }
  1138. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  1139. .dpms = intel_crtc_dpms,
  1140. .mode_fixup = intel_crtc_mode_fixup,
  1141. .mode_set = intel_crtc_mode_set,
  1142. .mode_set_base = intel_pipe_set_base,
  1143. .prepare = intel_crtc_prepare,
  1144. .commit = intel_crtc_commit,
  1145. };
  1146. static const struct drm_crtc_funcs intel_crtc_funcs = {
  1147. .cursor_set = intel_crtc_cursor_set,
  1148. .cursor_move = intel_crtc_cursor_move,
  1149. .gamma_set = intel_crtc_gamma_set,
  1150. .set_config = drm_crtc_helper_set_config,
  1151. .destroy = intel_crtc_destroy,
  1152. };
  1153. static void intel_crtc_init(struct drm_device *dev, int pipe)
  1154. {
  1155. struct intel_crtc *intel_crtc;
  1156. int i;
  1157. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  1158. if (intel_crtc == NULL)
  1159. return;
  1160. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  1161. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  1162. intel_crtc->pipe = pipe;
  1163. for (i = 0; i < 256; i++) {
  1164. intel_crtc->lut_r[i] = i;
  1165. intel_crtc->lut_g[i] = i;
  1166. intel_crtc->lut_b[i] = i;
  1167. }
  1168. intel_crtc->cursor_addr = 0;
  1169. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  1170. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  1171. intel_crtc->mode_set.crtc = &intel_crtc->base;
  1172. intel_crtc->mode_set.connectors = (struct drm_connector **)(intel_crtc + 1);
  1173. intel_crtc->mode_set.num_connectors = 0;
  1174. if (i915_fbpercrtc) {
  1175. }
  1176. }
  1177. struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  1178. {
  1179. struct drm_crtc *crtc = NULL;
  1180. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1181. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1182. if (intel_crtc->pipe == pipe)
  1183. break;
  1184. }
  1185. return crtc;
  1186. }
  1187. static int intel_connector_clones(struct drm_device *dev, int type_mask)
  1188. {
  1189. int index_mask = 0;
  1190. struct drm_connector *connector;
  1191. int entry = 0;
  1192. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1193. struct intel_output *intel_output = to_intel_output(connector);
  1194. if (type_mask & (1 << intel_output->type))
  1195. index_mask |= (1 << entry);
  1196. entry++;
  1197. }
  1198. return index_mask;
  1199. }
  1200. static void intel_setup_outputs(struct drm_device *dev)
  1201. {
  1202. struct drm_connector *connector;
  1203. intel_crt_init(dev);
  1204. /* Set up integrated LVDS */
  1205. if (IS_MOBILE(dev) && !IS_I830(dev))
  1206. intel_lvds_init(dev);
  1207. if (IS_I9XX(dev)) {
  1208. intel_sdvo_init(dev, SDVOB);
  1209. intel_sdvo_init(dev, SDVOC);
  1210. } else
  1211. intel_dvo_init(dev);
  1212. if (IS_I9XX(dev) && !IS_I915G(dev))
  1213. intel_tv_init(dev);
  1214. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1215. struct intel_output *intel_output = to_intel_output(connector);
  1216. struct drm_encoder *encoder = &intel_output->enc;
  1217. int crtc_mask = 0, clone_mask = 0;
  1218. /* valid crtcs */
  1219. switch(intel_output->type) {
  1220. case INTEL_OUTPUT_DVO:
  1221. case INTEL_OUTPUT_SDVO:
  1222. crtc_mask = ((1 << 0)|
  1223. (1 << 1));
  1224. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  1225. (1 << INTEL_OUTPUT_DVO) |
  1226. (1 << INTEL_OUTPUT_SDVO));
  1227. break;
  1228. case INTEL_OUTPUT_ANALOG:
  1229. crtc_mask = ((1 << 0)|
  1230. (1 << 1));
  1231. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  1232. (1 << INTEL_OUTPUT_DVO) |
  1233. (1 << INTEL_OUTPUT_SDVO));
  1234. break;
  1235. case INTEL_OUTPUT_LVDS:
  1236. crtc_mask = (1 << 1);
  1237. clone_mask = (1 << INTEL_OUTPUT_LVDS);
  1238. break;
  1239. case INTEL_OUTPUT_TVOUT:
  1240. crtc_mask = ((1 << 0) |
  1241. (1 << 1));
  1242. clone_mask = (1 << INTEL_OUTPUT_TVOUT);
  1243. break;
  1244. }
  1245. encoder->possible_crtcs = crtc_mask;
  1246. encoder->possible_clones = intel_connector_clones(dev, clone_mask);
  1247. }
  1248. }
  1249. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  1250. {
  1251. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1252. struct drm_device *dev = fb->dev;
  1253. if (fb->fbdev)
  1254. intelfb_remove(dev, fb);
  1255. drm_framebuffer_cleanup(fb);
  1256. mutex_lock(&dev->struct_mutex);
  1257. drm_gem_object_unreference(intel_fb->obj);
  1258. mutex_unlock(&dev->struct_mutex);
  1259. kfree(intel_fb);
  1260. }
  1261. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  1262. struct drm_file *file_priv,
  1263. unsigned int *handle)
  1264. {
  1265. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1266. struct drm_gem_object *object = intel_fb->obj;
  1267. return drm_gem_handle_create(file_priv, object, handle);
  1268. }
  1269. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  1270. .destroy = intel_user_framebuffer_destroy,
  1271. .create_handle = intel_user_framebuffer_create_handle,
  1272. };
  1273. int intel_framebuffer_create(struct drm_device *dev,
  1274. struct drm_mode_fb_cmd *mode_cmd,
  1275. struct drm_framebuffer **fb,
  1276. struct drm_gem_object *obj)
  1277. {
  1278. struct intel_framebuffer *intel_fb;
  1279. int ret;
  1280. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  1281. if (!intel_fb)
  1282. return -ENOMEM;
  1283. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  1284. if (ret) {
  1285. DRM_ERROR("framebuffer init failed %d\n", ret);
  1286. return ret;
  1287. }
  1288. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  1289. intel_fb->obj = obj;
  1290. *fb = &intel_fb->base;
  1291. return 0;
  1292. }
  1293. static struct drm_framebuffer *
  1294. intel_user_framebuffer_create(struct drm_device *dev,
  1295. struct drm_file *filp,
  1296. struct drm_mode_fb_cmd *mode_cmd)
  1297. {
  1298. struct drm_gem_object *obj;
  1299. struct drm_framebuffer *fb;
  1300. int ret;
  1301. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
  1302. if (!obj)
  1303. return NULL;
  1304. ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
  1305. if (ret) {
  1306. drm_gem_object_unreference(obj);
  1307. return NULL;
  1308. }
  1309. return fb;
  1310. }
  1311. static const struct drm_mode_config_funcs intel_mode_funcs = {
  1312. .fb_create = intel_user_framebuffer_create,
  1313. .fb_changed = intelfb_probe,
  1314. };
  1315. void intel_modeset_init(struct drm_device *dev)
  1316. {
  1317. int num_pipe;
  1318. int i;
  1319. drm_mode_config_init(dev);
  1320. dev->mode_config.min_width = 0;
  1321. dev->mode_config.min_height = 0;
  1322. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  1323. if (IS_I965G(dev)) {
  1324. dev->mode_config.max_width = 8192;
  1325. dev->mode_config.max_height = 8192;
  1326. } else {
  1327. dev->mode_config.max_width = 2048;
  1328. dev->mode_config.max_height = 2048;
  1329. }
  1330. /* set memory base */
  1331. if (IS_I9XX(dev))
  1332. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  1333. else
  1334. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  1335. if (IS_MOBILE(dev) || IS_I9XX(dev))
  1336. num_pipe = 2;
  1337. else
  1338. num_pipe = 1;
  1339. DRM_DEBUG("%d display pipe%s available.\n",
  1340. num_pipe, num_pipe > 1 ? "s" : "");
  1341. for (i = 0; i < num_pipe; i++) {
  1342. intel_crtc_init(dev, i);
  1343. }
  1344. intel_setup_outputs(dev);
  1345. }
  1346. void intel_modeset_cleanup(struct drm_device *dev)
  1347. {
  1348. drm_mode_config_cleanup(dev);
  1349. }
  1350. /* current intel driver doesn't take advantage of encoders
  1351. always give back the encoder for the connector
  1352. */
  1353. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  1354. {
  1355. struct intel_output *intel_output = to_intel_output(connector);
  1356. return &intel_output->enc;
  1357. }