intel_display.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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 int
  312. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  313. struct drm_framebuffer *old_fb)
  314. {
  315. struct drm_device *dev = crtc->dev;
  316. struct drm_i915_private *dev_priv = dev->dev_private;
  317. struct drm_i915_master_private *master_priv;
  318. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  319. struct intel_framebuffer *intel_fb;
  320. struct drm_i915_gem_object *obj_priv;
  321. struct drm_gem_object *obj;
  322. int pipe = intel_crtc->pipe;
  323. unsigned long Start, Offset;
  324. int dspbase = (pipe == 0 ? DSPAADDR : DSPBADDR);
  325. int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
  326. int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
  327. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  328. u32 dspcntr, alignment;
  329. int ret;
  330. /* no fb bound */
  331. if (!crtc->fb) {
  332. DRM_DEBUG("No FB bound\n");
  333. return 0;
  334. }
  335. switch (pipe) {
  336. case 0:
  337. case 1:
  338. break;
  339. default:
  340. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  341. return -EINVAL;
  342. }
  343. intel_fb = to_intel_framebuffer(crtc->fb);
  344. obj = intel_fb->obj;
  345. obj_priv = obj->driver_private;
  346. switch (obj_priv->tiling_mode) {
  347. case I915_TILING_NONE:
  348. alignment = 64 * 1024;
  349. break;
  350. case I915_TILING_X:
  351. /* pin() will align the object as required by fence */
  352. alignment = 0;
  353. break;
  354. case I915_TILING_Y:
  355. /* FIXME: Is this true? */
  356. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  357. return -EINVAL;
  358. default:
  359. BUG();
  360. }
  361. mutex_lock(&dev->struct_mutex);
  362. ret = i915_gem_object_pin(intel_fb->obj, alignment);
  363. if (ret != 0) {
  364. mutex_unlock(&dev->struct_mutex);
  365. return ret;
  366. }
  367. ret = i915_gem_object_set_to_gtt_domain(intel_fb->obj, 1);
  368. if (ret != 0) {
  369. i915_gem_object_unpin(intel_fb->obj);
  370. mutex_unlock(&dev->struct_mutex);
  371. return ret;
  372. }
  373. dspcntr = I915_READ(dspcntr_reg);
  374. /* Mask out pixel format bits in case we change it */
  375. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  376. switch (crtc->fb->bits_per_pixel) {
  377. case 8:
  378. dspcntr |= DISPPLANE_8BPP;
  379. break;
  380. case 16:
  381. if (crtc->fb->depth == 15)
  382. dspcntr |= DISPPLANE_15_16BPP;
  383. else
  384. dspcntr |= DISPPLANE_16BPP;
  385. break;
  386. case 24:
  387. case 32:
  388. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  389. break;
  390. default:
  391. DRM_ERROR("Unknown color depth\n");
  392. i915_gem_object_unpin(intel_fb->obj);
  393. mutex_unlock(&dev->struct_mutex);
  394. return -EINVAL;
  395. }
  396. I915_WRITE(dspcntr_reg, dspcntr);
  397. Start = obj_priv->gtt_offset;
  398. Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
  399. DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
  400. I915_WRITE(dspstride, crtc->fb->pitch);
  401. if (IS_I965G(dev)) {
  402. I915_WRITE(dspbase, Offset);
  403. I915_READ(dspbase);
  404. I915_WRITE(dspsurf, Start);
  405. I915_READ(dspsurf);
  406. } else {
  407. I915_WRITE(dspbase, Start + Offset);
  408. I915_READ(dspbase);
  409. }
  410. intel_wait_for_vblank(dev);
  411. if (old_fb) {
  412. intel_fb = to_intel_framebuffer(old_fb);
  413. i915_gem_object_unpin(intel_fb->obj);
  414. }
  415. mutex_unlock(&dev->struct_mutex);
  416. if (!dev->primary->master)
  417. return 0;
  418. master_priv = dev->primary->master->driver_priv;
  419. if (!master_priv->sarea_priv)
  420. return 0;
  421. if (pipe) {
  422. master_priv->sarea_priv->pipeB_x = x;
  423. master_priv->sarea_priv->pipeB_y = y;
  424. } else {
  425. master_priv->sarea_priv->pipeA_x = x;
  426. master_priv->sarea_priv->pipeA_y = y;
  427. }
  428. return 0;
  429. }
  430. /**
  431. * Sets the power management mode of the pipe and plane.
  432. *
  433. * This code should probably grow support for turning the cursor off and back
  434. * on appropriately at the same time as we're turning the pipe off/on.
  435. */
  436. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  437. {
  438. struct drm_device *dev = crtc->dev;
  439. struct drm_i915_master_private *master_priv;
  440. struct drm_i915_private *dev_priv = dev->dev_private;
  441. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  442. int pipe = intel_crtc->pipe;
  443. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  444. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  445. int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
  446. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  447. u32 temp;
  448. bool enabled;
  449. /* XXX: When our outputs are all unaware of DPMS modes other than off
  450. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  451. */
  452. switch (mode) {
  453. case DRM_MODE_DPMS_ON:
  454. case DRM_MODE_DPMS_STANDBY:
  455. case DRM_MODE_DPMS_SUSPEND:
  456. /* Enable the DPLL */
  457. temp = I915_READ(dpll_reg);
  458. if ((temp & DPLL_VCO_ENABLE) == 0) {
  459. I915_WRITE(dpll_reg, temp);
  460. I915_READ(dpll_reg);
  461. /* Wait for the clocks to stabilize. */
  462. udelay(150);
  463. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  464. I915_READ(dpll_reg);
  465. /* Wait for the clocks to stabilize. */
  466. udelay(150);
  467. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  468. I915_READ(dpll_reg);
  469. /* Wait for the clocks to stabilize. */
  470. udelay(150);
  471. }
  472. /* Enable the pipe */
  473. temp = I915_READ(pipeconf_reg);
  474. if ((temp & PIPEACONF_ENABLE) == 0)
  475. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  476. /* Enable the plane */
  477. temp = I915_READ(dspcntr_reg);
  478. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  479. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  480. /* Flush the plane changes */
  481. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  482. }
  483. intel_crtc_load_lut(crtc);
  484. /* Give the overlay scaler a chance to enable if it's on this pipe */
  485. //intel_crtc_dpms_video(crtc, true); TODO
  486. break;
  487. case DRM_MODE_DPMS_OFF:
  488. /* Give the overlay scaler a chance to disable if it's on this pipe */
  489. //intel_crtc_dpms_video(crtc, FALSE); TODO
  490. /* Disable the VGA plane that we never use */
  491. I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  492. /* Disable display plane */
  493. temp = I915_READ(dspcntr_reg);
  494. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  495. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  496. /* Flush the plane changes */
  497. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  498. I915_READ(dspbase_reg);
  499. }
  500. if (!IS_I9XX(dev)) {
  501. /* Wait for vblank for the disable to take effect */
  502. intel_wait_for_vblank(dev);
  503. }
  504. /* Next, disable display pipes */
  505. temp = I915_READ(pipeconf_reg);
  506. if ((temp & PIPEACONF_ENABLE) != 0) {
  507. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  508. I915_READ(pipeconf_reg);
  509. }
  510. /* Wait for vblank for the disable to take effect. */
  511. intel_wait_for_vblank(dev);
  512. temp = I915_READ(dpll_reg);
  513. if ((temp & DPLL_VCO_ENABLE) != 0) {
  514. I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  515. I915_READ(dpll_reg);
  516. }
  517. /* Wait for the clocks to turn off. */
  518. udelay(150);
  519. break;
  520. }
  521. if (!dev->primary->master)
  522. return;
  523. master_priv = dev->primary->master->driver_priv;
  524. if (!master_priv->sarea_priv)
  525. return;
  526. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  527. switch (pipe) {
  528. case 0:
  529. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  530. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  531. break;
  532. case 1:
  533. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  534. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  535. break;
  536. default:
  537. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  538. break;
  539. }
  540. intel_crtc->dpms_mode = mode;
  541. }
  542. static void intel_crtc_prepare (struct drm_crtc *crtc)
  543. {
  544. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  545. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  546. }
  547. static void intel_crtc_commit (struct drm_crtc *crtc)
  548. {
  549. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  550. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  551. }
  552. void intel_encoder_prepare (struct drm_encoder *encoder)
  553. {
  554. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  555. /* lvds has its own version of prepare see intel_lvds_prepare */
  556. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  557. }
  558. void intel_encoder_commit (struct drm_encoder *encoder)
  559. {
  560. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  561. /* lvds has its own version of commit see intel_lvds_commit */
  562. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  563. }
  564. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  565. struct drm_display_mode *mode,
  566. struct drm_display_mode *adjusted_mode)
  567. {
  568. return true;
  569. }
  570. /** Returns the core display clock speed for i830 - i945 */
  571. static int intel_get_core_clock_speed(struct drm_device *dev)
  572. {
  573. /* Core clock values taken from the published datasheets.
  574. * The 830 may go up to 166 Mhz, which we should check.
  575. */
  576. if (IS_I945G(dev))
  577. return 400000;
  578. else if (IS_I915G(dev))
  579. return 333000;
  580. else if (IS_I945GM(dev) || IS_845G(dev))
  581. return 200000;
  582. else if (IS_I915GM(dev)) {
  583. u16 gcfgc = 0;
  584. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  585. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  586. return 133000;
  587. else {
  588. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  589. case GC_DISPLAY_CLOCK_333_MHZ:
  590. return 333000;
  591. default:
  592. case GC_DISPLAY_CLOCK_190_200_MHZ:
  593. return 190000;
  594. }
  595. }
  596. } else if (IS_I865G(dev))
  597. return 266000;
  598. else if (IS_I855(dev)) {
  599. u16 hpllcc = 0;
  600. /* Assume that the hardware is in the high speed state. This
  601. * should be the default.
  602. */
  603. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  604. case GC_CLOCK_133_200:
  605. case GC_CLOCK_100_200:
  606. return 200000;
  607. case GC_CLOCK_166_250:
  608. return 250000;
  609. case GC_CLOCK_100_133:
  610. return 133000;
  611. }
  612. } else /* 852, 830 */
  613. return 133000;
  614. return 0; /* Silence gcc warning */
  615. }
  616. /**
  617. * Return the pipe currently connected to the panel fitter,
  618. * or -1 if the panel fitter is not present or not in use
  619. */
  620. static int intel_panel_fitter_pipe (struct drm_device *dev)
  621. {
  622. struct drm_i915_private *dev_priv = dev->dev_private;
  623. u32 pfit_control;
  624. /* i830 doesn't have a panel fitter */
  625. if (IS_I830(dev))
  626. return -1;
  627. pfit_control = I915_READ(PFIT_CONTROL);
  628. /* See if the panel fitter is in use */
  629. if ((pfit_control & PFIT_ENABLE) == 0)
  630. return -1;
  631. /* 965 can place panel fitter on either pipe */
  632. if (IS_I965G(dev))
  633. return (pfit_control >> 29) & 0x3;
  634. /* older chips can only use pipe 1 */
  635. return 1;
  636. }
  637. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  638. struct drm_display_mode *mode,
  639. struct drm_display_mode *adjusted_mode,
  640. int x, int y,
  641. struct drm_framebuffer *old_fb)
  642. {
  643. struct drm_device *dev = crtc->dev;
  644. struct drm_i915_private *dev_priv = dev->dev_private;
  645. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  646. int pipe = intel_crtc->pipe;
  647. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  648. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  649. int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  650. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  651. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  652. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  653. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  654. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  655. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  656. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  657. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  658. int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
  659. int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
  660. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  661. int refclk;
  662. intel_clock_t clock;
  663. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  664. bool ok, is_sdvo = false, is_dvo = false;
  665. bool is_crt = false, is_lvds = false, is_tv = false;
  666. struct drm_mode_config *mode_config = &dev->mode_config;
  667. struct drm_connector *connector;
  668. int ret;
  669. drm_vblank_pre_modeset(dev, pipe);
  670. list_for_each_entry(connector, &mode_config->connector_list, head) {
  671. struct intel_output *intel_output = to_intel_output(connector);
  672. if (!connector->encoder || connector->encoder->crtc != crtc)
  673. continue;
  674. switch (intel_output->type) {
  675. case INTEL_OUTPUT_LVDS:
  676. is_lvds = true;
  677. break;
  678. case INTEL_OUTPUT_SDVO:
  679. case INTEL_OUTPUT_HDMI:
  680. is_sdvo = true;
  681. if (intel_output->needs_tv_clock)
  682. is_tv = true;
  683. break;
  684. case INTEL_OUTPUT_DVO:
  685. is_dvo = true;
  686. break;
  687. case INTEL_OUTPUT_TVOUT:
  688. is_tv = true;
  689. break;
  690. case INTEL_OUTPUT_ANALOG:
  691. is_crt = true;
  692. break;
  693. }
  694. }
  695. if (IS_I9XX(dev)) {
  696. refclk = 96000;
  697. } else {
  698. refclk = 48000;
  699. }
  700. ok = intel_find_best_PLL(crtc, adjusted_mode->clock, refclk, &clock);
  701. if (!ok) {
  702. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  703. return -EINVAL;
  704. }
  705. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  706. dpll = DPLL_VGA_MODE_DIS;
  707. if (IS_I9XX(dev)) {
  708. if (is_lvds)
  709. dpll |= DPLLB_MODE_LVDS;
  710. else
  711. dpll |= DPLLB_MODE_DAC_SERIAL;
  712. if (is_sdvo) {
  713. dpll |= DPLL_DVO_HIGH_SPEED;
  714. if (IS_I945G(dev) || IS_I945GM(dev)) {
  715. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  716. dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  717. }
  718. }
  719. /* compute bitmask from p1 value */
  720. dpll |= (1 << (clock.p1 - 1)) << 16;
  721. switch (clock.p2) {
  722. case 5:
  723. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  724. break;
  725. case 7:
  726. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  727. break;
  728. case 10:
  729. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  730. break;
  731. case 14:
  732. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  733. break;
  734. }
  735. if (IS_I965G(dev))
  736. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  737. } else {
  738. if (is_lvds) {
  739. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  740. } else {
  741. if (clock.p1 == 2)
  742. dpll |= PLL_P1_DIVIDE_BY_TWO;
  743. else
  744. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  745. if (clock.p2 == 4)
  746. dpll |= PLL_P2_DIVIDE_BY_4;
  747. }
  748. }
  749. if (is_tv) {
  750. /* XXX: just matching BIOS for now */
  751. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  752. dpll |= 3;
  753. }
  754. else
  755. dpll |= PLL_REF_INPUT_DREFCLK;
  756. /* setup pipeconf */
  757. pipeconf = I915_READ(pipeconf_reg);
  758. /* Set up the display plane register */
  759. dspcntr = DISPPLANE_GAMMA_ENABLE;
  760. if (pipe == 0)
  761. dspcntr |= DISPPLANE_SEL_PIPE_A;
  762. else
  763. dspcntr |= DISPPLANE_SEL_PIPE_B;
  764. if (pipe == 0 && !IS_I965G(dev)) {
  765. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  766. * core speed.
  767. *
  768. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  769. * pipe == 0 check?
  770. */
  771. if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
  772. pipeconf |= PIPEACONF_DOUBLE_WIDE;
  773. else
  774. pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
  775. }
  776. dspcntr |= DISPLAY_PLANE_ENABLE;
  777. pipeconf |= PIPEACONF_ENABLE;
  778. dpll |= DPLL_VCO_ENABLE;
  779. /* Disable the panel fitter if it was on our pipe */
  780. if (intel_panel_fitter_pipe(dev) == pipe)
  781. I915_WRITE(PFIT_CONTROL, 0);
  782. DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  783. drm_mode_debug_printmodeline(mode);
  784. if (dpll & DPLL_VCO_ENABLE) {
  785. I915_WRITE(fp_reg, fp);
  786. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  787. I915_READ(dpll_reg);
  788. udelay(150);
  789. }
  790. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  791. * This is an exception to the general rule that mode_set doesn't turn
  792. * things on.
  793. */
  794. if (is_lvds) {
  795. u32 lvds = I915_READ(LVDS);
  796. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
  797. /* Set the B0-B3 data pairs corresponding to whether we're going to
  798. * set the DPLLs for dual-channel mode or not.
  799. */
  800. if (clock.p2 == 7)
  801. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  802. else
  803. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  804. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  805. * appropriately here, but we need to look more thoroughly into how
  806. * panels behave in the two modes.
  807. */
  808. I915_WRITE(LVDS, lvds);
  809. I915_READ(LVDS);
  810. }
  811. I915_WRITE(fp_reg, fp);
  812. I915_WRITE(dpll_reg, dpll);
  813. I915_READ(dpll_reg);
  814. /* Wait for the clocks to stabilize. */
  815. udelay(150);
  816. if (IS_I965G(dev)) {
  817. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  818. I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
  819. ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  820. } else {
  821. /* write it again -- the BIOS does, after all */
  822. I915_WRITE(dpll_reg, dpll);
  823. }
  824. I915_READ(dpll_reg);
  825. /* Wait for the clocks to stabilize. */
  826. udelay(150);
  827. I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  828. ((adjusted_mode->crtc_htotal - 1) << 16));
  829. I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  830. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  831. I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  832. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  833. I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  834. ((adjusted_mode->crtc_vtotal - 1) << 16));
  835. I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  836. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  837. I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  838. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  839. /* pipesrc and dspsize control the size that is scaled from, which should
  840. * always be the user's requested size.
  841. */
  842. I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  843. I915_WRITE(dsppos_reg, 0);
  844. I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  845. I915_WRITE(pipeconf_reg, pipeconf);
  846. I915_READ(pipeconf_reg);
  847. intel_wait_for_vblank(dev);
  848. I915_WRITE(dspcntr_reg, dspcntr);
  849. /* Flush the plane changes */
  850. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  851. if (ret != 0)
  852. return ret;
  853. drm_vblank_post_modeset(dev, pipe);
  854. return 0;
  855. }
  856. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  857. void intel_crtc_load_lut(struct drm_crtc *crtc)
  858. {
  859. struct drm_device *dev = crtc->dev;
  860. struct drm_i915_private *dev_priv = dev->dev_private;
  861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  862. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  863. int i;
  864. /* The clocks have to be on to load the palette. */
  865. if (!crtc->enabled)
  866. return;
  867. for (i = 0; i < 256; i++) {
  868. I915_WRITE(palreg + 4 * i,
  869. (intel_crtc->lut_r[i] << 16) |
  870. (intel_crtc->lut_g[i] << 8) |
  871. intel_crtc->lut_b[i]);
  872. }
  873. }
  874. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  875. struct drm_file *file_priv,
  876. uint32_t handle,
  877. uint32_t width, uint32_t height)
  878. {
  879. struct drm_device *dev = crtc->dev;
  880. struct drm_i915_private *dev_priv = dev->dev_private;
  881. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  882. struct drm_gem_object *bo;
  883. struct drm_i915_gem_object *obj_priv;
  884. int pipe = intel_crtc->pipe;
  885. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  886. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  887. uint32_t temp;
  888. size_t addr;
  889. int ret;
  890. DRM_DEBUG("\n");
  891. /* if we want to turn off the cursor ignore width and height */
  892. if (!handle) {
  893. DRM_DEBUG("cursor off\n");
  894. temp = CURSOR_MODE_DISABLE;
  895. addr = 0;
  896. bo = NULL;
  897. goto finish;
  898. }
  899. /* Currently we only support 64x64 cursors */
  900. if (width != 64 || height != 64) {
  901. DRM_ERROR("we currently only support 64x64 cursors\n");
  902. return -EINVAL;
  903. }
  904. bo = drm_gem_object_lookup(dev, file_priv, handle);
  905. if (!bo)
  906. return -ENOENT;
  907. obj_priv = bo->driver_private;
  908. if (bo->size < width * height * 4) {
  909. DRM_ERROR("buffer is to small\n");
  910. ret = -ENOMEM;
  911. goto fail;
  912. }
  913. /* we only need to pin inside GTT if cursor is non-phy */
  914. if (!dev_priv->cursor_needs_physical) {
  915. ret = i915_gem_object_pin(bo, PAGE_SIZE);
  916. if (ret) {
  917. DRM_ERROR("failed to pin cursor bo\n");
  918. goto fail;
  919. }
  920. addr = obj_priv->gtt_offset;
  921. } else {
  922. ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
  923. if (ret) {
  924. DRM_ERROR("failed to attach phys object\n");
  925. goto fail;
  926. }
  927. addr = obj_priv->phys_obj->handle->busaddr;
  928. }
  929. temp = 0;
  930. /* set the pipe for the cursor */
  931. temp |= (pipe << 28);
  932. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  933. finish:
  934. I915_WRITE(control, temp);
  935. I915_WRITE(base, addr);
  936. if (intel_crtc->cursor_bo) {
  937. if (dev_priv->cursor_needs_physical) {
  938. if (intel_crtc->cursor_bo != bo)
  939. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  940. } else
  941. i915_gem_object_unpin(intel_crtc->cursor_bo);
  942. mutex_lock(&dev->struct_mutex);
  943. drm_gem_object_unreference(intel_crtc->cursor_bo);
  944. mutex_unlock(&dev->struct_mutex);
  945. }
  946. intel_crtc->cursor_addr = addr;
  947. intel_crtc->cursor_bo = bo;
  948. return 0;
  949. fail:
  950. mutex_lock(&dev->struct_mutex);
  951. drm_gem_object_unreference(bo);
  952. mutex_unlock(&dev->struct_mutex);
  953. return ret;
  954. }
  955. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  956. {
  957. struct drm_device *dev = crtc->dev;
  958. struct drm_i915_private *dev_priv = dev->dev_private;
  959. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  960. int pipe = intel_crtc->pipe;
  961. uint32_t temp = 0;
  962. uint32_t adder;
  963. if (x < 0) {
  964. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  965. x = -x;
  966. }
  967. if (y < 0) {
  968. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  969. y = -y;
  970. }
  971. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  972. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  973. adder = intel_crtc->cursor_addr;
  974. I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  975. I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  976. return 0;
  977. }
  978. /** Sets the color ramps on behalf of RandR */
  979. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  980. u16 blue, int regno)
  981. {
  982. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  983. intel_crtc->lut_r[regno] = red >> 8;
  984. intel_crtc->lut_g[regno] = green >> 8;
  985. intel_crtc->lut_b[regno] = blue >> 8;
  986. }
  987. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  988. u16 *blue, uint32_t size)
  989. {
  990. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  991. int i;
  992. if (size != 256)
  993. return;
  994. for (i = 0; i < 256; i++) {
  995. intel_crtc->lut_r[i] = red[i] >> 8;
  996. intel_crtc->lut_g[i] = green[i] >> 8;
  997. intel_crtc->lut_b[i] = blue[i] >> 8;
  998. }
  999. intel_crtc_load_lut(crtc);
  1000. }
  1001. /**
  1002. * Get a pipe with a simple mode set on it for doing load-based monitor
  1003. * detection.
  1004. *
  1005. * It will be up to the load-detect code to adjust the pipe as appropriate for
  1006. * its requirements. The pipe will be connected to no other outputs.
  1007. *
  1008. * Currently this code will only succeed if there is a pipe with no outputs
  1009. * configured for it. In the future, it could choose to temporarily disable
  1010. * some outputs to free up a pipe for its use.
  1011. *
  1012. * \return crtc, or NULL if no pipes are available.
  1013. */
  1014. /* VESA 640x480x72Hz mode to set on the pipe */
  1015. static struct drm_display_mode load_detect_mode = {
  1016. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  1017. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  1018. };
  1019. struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
  1020. struct drm_display_mode *mode,
  1021. int *dpms_mode)
  1022. {
  1023. struct intel_crtc *intel_crtc;
  1024. struct drm_crtc *possible_crtc;
  1025. struct drm_crtc *supported_crtc =NULL;
  1026. struct drm_encoder *encoder = &intel_output->enc;
  1027. struct drm_crtc *crtc = NULL;
  1028. struct drm_device *dev = encoder->dev;
  1029. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1030. struct drm_crtc_helper_funcs *crtc_funcs;
  1031. int i = -1;
  1032. /*
  1033. * Algorithm gets a little messy:
  1034. * - if the connector already has an assigned crtc, use it (but make
  1035. * sure it's on first)
  1036. * - try to find the first unused crtc that can drive this connector,
  1037. * and use that if we find one
  1038. * - if there are no unused crtcs available, try to use the first
  1039. * one we found that supports the connector
  1040. */
  1041. /* See if we already have a CRTC for this connector */
  1042. if (encoder->crtc) {
  1043. crtc = encoder->crtc;
  1044. /* Make sure the crtc and connector are running */
  1045. intel_crtc = to_intel_crtc(crtc);
  1046. *dpms_mode = intel_crtc->dpms_mode;
  1047. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  1048. crtc_funcs = crtc->helper_private;
  1049. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1050. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  1051. }
  1052. return crtc;
  1053. }
  1054. /* Find an unused one (if possible) */
  1055. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  1056. i++;
  1057. if (!(encoder->possible_crtcs & (1 << i)))
  1058. continue;
  1059. if (!possible_crtc->enabled) {
  1060. crtc = possible_crtc;
  1061. break;
  1062. }
  1063. if (!supported_crtc)
  1064. supported_crtc = possible_crtc;
  1065. }
  1066. /*
  1067. * If we didn't find an unused CRTC, don't use any.
  1068. */
  1069. if (!crtc) {
  1070. return NULL;
  1071. }
  1072. encoder->crtc = crtc;
  1073. intel_output->load_detect_temp = true;
  1074. intel_crtc = to_intel_crtc(crtc);
  1075. *dpms_mode = intel_crtc->dpms_mode;
  1076. if (!crtc->enabled) {
  1077. if (!mode)
  1078. mode = &load_detect_mode;
  1079. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  1080. } else {
  1081. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  1082. crtc_funcs = crtc->helper_private;
  1083. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1084. }
  1085. /* Add this connector to the crtc */
  1086. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  1087. encoder_funcs->commit(encoder);
  1088. }
  1089. /* let the connector get through one full cycle before testing */
  1090. intel_wait_for_vblank(dev);
  1091. return crtc;
  1092. }
  1093. void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
  1094. {
  1095. struct drm_encoder *encoder = &intel_output->enc;
  1096. struct drm_device *dev = encoder->dev;
  1097. struct drm_crtc *crtc = encoder->crtc;
  1098. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1099. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1100. if (intel_output->load_detect_temp) {
  1101. encoder->crtc = NULL;
  1102. intel_output->load_detect_temp = false;
  1103. crtc->enabled = drm_helper_crtc_in_use(crtc);
  1104. drm_helper_disable_unused_functions(dev);
  1105. }
  1106. /* Switch crtc and output back off if necessary */
  1107. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  1108. if (encoder->crtc == crtc)
  1109. encoder_funcs->dpms(encoder, dpms_mode);
  1110. crtc_funcs->dpms(crtc, dpms_mode);
  1111. }
  1112. }
  1113. /* Returns the clock of the currently programmed mode of the given pipe. */
  1114. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  1115. {
  1116. struct drm_i915_private *dev_priv = dev->dev_private;
  1117. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1118. int pipe = intel_crtc->pipe;
  1119. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  1120. u32 fp;
  1121. intel_clock_t clock;
  1122. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1123. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  1124. else
  1125. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  1126. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  1127. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1128. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  1129. if (IS_I9XX(dev)) {
  1130. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  1131. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1132. switch (dpll & DPLL_MODE_MASK) {
  1133. case DPLLB_MODE_DAC_SERIAL:
  1134. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  1135. 5 : 10;
  1136. break;
  1137. case DPLLB_MODE_LVDS:
  1138. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  1139. 7 : 14;
  1140. break;
  1141. default:
  1142. DRM_DEBUG("Unknown DPLL mode %08x in programmed "
  1143. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  1144. return 0;
  1145. }
  1146. /* XXX: Handle the 100Mhz refclk */
  1147. i9xx_clock(96000, &clock);
  1148. } else {
  1149. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  1150. if (is_lvds) {
  1151. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  1152. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1153. clock.p2 = 14;
  1154. if ((dpll & PLL_REF_INPUT_MASK) ==
  1155. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1156. /* XXX: might not be 66MHz */
  1157. i8xx_clock(66000, &clock);
  1158. } else
  1159. i8xx_clock(48000, &clock);
  1160. } else {
  1161. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1162. clock.p1 = 2;
  1163. else {
  1164. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1165. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1166. }
  1167. if (dpll & PLL_P2_DIVIDE_BY_4)
  1168. clock.p2 = 4;
  1169. else
  1170. clock.p2 = 2;
  1171. i8xx_clock(48000, &clock);
  1172. }
  1173. }
  1174. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1175. * i830PllIsValid() because it relies on the xf86_config connector
  1176. * configuration being accurate, which it isn't necessarily.
  1177. */
  1178. return clock.dot;
  1179. }
  1180. /** Returns the currently programmed mode of the given pipe. */
  1181. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  1182. struct drm_crtc *crtc)
  1183. {
  1184. struct drm_i915_private *dev_priv = dev->dev_private;
  1185. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1186. int pipe = intel_crtc->pipe;
  1187. struct drm_display_mode *mode;
  1188. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  1189. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  1190. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  1191. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  1192. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1193. if (!mode)
  1194. return NULL;
  1195. mode->clock = intel_crtc_clock_get(dev, crtc);
  1196. mode->hdisplay = (htot & 0xffff) + 1;
  1197. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1198. mode->hsync_start = (hsync & 0xffff) + 1;
  1199. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1200. mode->vdisplay = (vtot & 0xffff) + 1;
  1201. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1202. mode->vsync_start = (vsync & 0xffff) + 1;
  1203. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1204. drm_mode_set_name(mode);
  1205. drm_mode_set_crtcinfo(mode, 0);
  1206. return mode;
  1207. }
  1208. static void intel_crtc_destroy(struct drm_crtc *crtc)
  1209. {
  1210. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1211. drm_crtc_cleanup(crtc);
  1212. kfree(intel_crtc);
  1213. }
  1214. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  1215. .dpms = intel_crtc_dpms,
  1216. .mode_fixup = intel_crtc_mode_fixup,
  1217. .mode_set = intel_crtc_mode_set,
  1218. .mode_set_base = intel_pipe_set_base,
  1219. .prepare = intel_crtc_prepare,
  1220. .commit = intel_crtc_commit,
  1221. };
  1222. static const struct drm_crtc_funcs intel_crtc_funcs = {
  1223. .cursor_set = intel_crtc_cursor_set,
  1224. .cursor_move = intel_crtc_cursor_move,
  1225. .gamma_set = intel_crtc_gamma_set,
  1226. .set_config = drm_crtc_helper_set_config,
  1227. .destroy = intel_crtc_destroy,
  1228. };
  1229. static void intel_crtc_init(struct drm_device *dev, int pipe)
  1230. {
  1231. struct intel_crtc *intel_crtc;
  1232. int i;
  1233. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  1234. if (intel_crtc == NULL)
  1235. return;
  1236. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  1237. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  1238. intel_crtc->pipe = pipe;
  1239. for (i = 0; i < 256; i++) {
  1240. intel_crtc->lut_r[i] = i;
  1241. intel_crtc->lut_g[i] = i;
  1242. intel_crtc->lut_b[i] = i;
  1243. }
  1244. intel_crtc->cursor_addr = 0;
  1245. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  1246. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  1247. intel_crtc->mode_set.crtc = &intel_crtc->base;
  1248. intel_crtc->mode_set.connectors = (struct drm_connector **)(intel_crtc + 1);
  1249. intel_crtc->mode_set.num_connectors = 0;
  1250. if (i915_fbpercrtc) {
  1251. }
  1252. }
  1253. struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  1254. {
  1255. struct drm_crtc *crtc = NULL;
  1256. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1257. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1258. if (intel_crtc->pipe == pipe)
  1259. break;
  1260. }
  1261. return crtc;
  1262. }
  1263. static int intel_connector_clones(struct drm_device *dev, int type_mask)
  1264. {
  1265. int index_mask = 0;
  1266. struct drm_connector *connector;
  1267. int entry = 0;
  1268. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1269. struct intel_output *intel_output = to_intel_output(connector);
  1270. if (type_mask & (1 << intel_output->type))
  1271. index_mask |= (1 << entry);
  1272. entry++;
  1273. }
  1274. return index_mask;
  1275. }
  1276. static void intel_setup_outputs(struct drm_device *dev)
  1277. {
  1278. struct drm_i915_private *dev_priv = dev->dev_private;
  1279. struct drm_connector *connector;
  1280. intel_crt_init(dev);
  1281. /* Set up integrated LVDS */
  1282. if (IS_MOBILE(dev) && !IS_I830(dev))
  1283. intel_lvds_init(dev);
  1284. if (IS_I9XX(dev)) {
  1285. int found;
  1286. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  1287. found = intel_sdvo_init(dev, SDVOB);
  1288. if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
  1289. intel_hdmi_init(dev, SDVOB);
  1290. }
  1291. if (!IS_G4X(dev) || (I915_READ(SDVOB) & SDVO_DETECTED)) {
  1292. found = intel_sdvo_init(dev, SDVOC);
  1293. if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
  1294. intel_hdmi_init(dev, SDVOC);
  1295. }
  1296. } else
  1297. intel_dvo_init(dev);
  1298. if (IS_I9XX(dev) && IS_MOBILE(dev))
  1299. intel_tv_init(dev);
  1300. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1301. struct intel_output *intel_output = to_intel_output(connector);
  1302. struct drm_encoder *encoder = &intel_output->enc;
  1303. int crtc_mask = 0, clone_mask = 0;
  1304. /* valid crtcs */
  1305. switch(intel_output->type) {
  1306. case INTEL_OUTPUT_HDMI:
  1307. crtc_mask = ((1 << 0)|
  1308. (1 << 1));
  1309. clone_mask = ((1 << INTEL_OUTPUT_HDMI));
  1310. break;
  1311. case INTEL_OUTPUT_DVO:
  1312. case INTEL_OUTPUT_SDVO:
  1313. crtc_mask = ((1 << 0)|
  1314. (1 << 1));
  1315. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  1316. (1 << INTEL_OUTPUT_DVO) |
  1317. (1 << INTEL_OUTPUT_SDVO));
  1318. break;
  1319. case INTEL_OUTPUT_ANALOG:
  1320. crtc_mask = ((1 << 0)|
  1321. (1 << 1));
  1322. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  1323. (1 << INTEL_OUTPUT_DVO) |
  1324. (1 << INTEL_OUTPUT_SDVO));
  1325. break;
  1326. case INTEL_OUTPUT_LVDS:
  1327. crtc_mask = (1 << 1);
  1328. clone_mask = (1 << INTEL_OUTPUT_LVDS);
  1329. break;
  1330. case INTEL_OUTPUT_TVOUT:
  1331. crtc_mask = ((1 << 0) |
  1332. (1 << 1));
  1333. clone_mask = (1 << INTEL_OUTPUT_TVOUT);
  1334. break;
  1335. }
  1336. encoder->possible_crtcs = crtc_mask;
  1337. encoder->possible_clones = intel_connector_clones(dev, clone_mask);
  1338. }
  1339. }
  1340. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  1341. {
  1342. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1343. struct drm_device *dev = fb->dev;
  1344. if (fb->fbdev)
  1345. intelfb_remove(dev, fb);
  1346. drm_framebuffer_cleanup(fb);
  1347. mutex_lock(&dev->struct_mutex);
  1348. drm_gem_object_unreference(intel_fb->obj);
  1349. mutex_unlock(&dev->struct_mutex);
  1350. kfree(intel_fb);
  1351. }
  1352. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  1353. struct drm_file *file_priv,
  1354. unsigned int *handle)
  1355. {
  1356. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1357. struct drm_gem_object *object = intel_fb->obj;
  1358. return drm_gem_handle_create(file_priv, object, handle);
  1359. }
  1360. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  1361. .destroy = intel_user_framebuffer_destroy,
  1362. .create_handle = intel_user_framebuffer_create_handle,
  1363. };
  1364. int intel_framebuffer_create(struct drm_device *dev,
  1365. struct drm_mode_fb_cmd *mode_cmd,
  1366. struct drm_framebuffer **fb,
  1367. struct drm_gem_object *obj)
  1368. {
  1369. struct intel_framebuffer *intel_fb;
  1370. int ret;
  1371. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  1372. if (!intel_fb)
  1373. return -ENOMEM;
  1374. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  1375. if (ret) {
  1376. DRM_ERROR("framebuffer init failed %d\n", ret);
  1377. return ret;
  1378. }
  1379. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  1380. intel_fb->obj = obj;
  1381. *fb = &intel_fb->base;
  1382. return 0;
  1383. }
  1384. static struct drm_framebuffer *
  1385. intel_user_framebuffer_create(struct drm_device *dev,
  1386. struct drm_file *filp,
  1387. struct drm_mode_fb_cmd *mode_cmd)
  1388. {
  1389. struct drm_gem_object *obj;
  1390. struct drm_framebuffer *fb;
  1391. int ret;
  1392. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
  1393. if (!obj)
  1394. return NULL;
  1395. ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
  1396. if (ret) {
  1397. drm_gem_object_unreference(obj);
  1398. return NULL;
  1399. }
  1400. return fb;
  1401. }
  1402. static const struct drm_mode_config_funcs intel_mode_funcs = {
  1403. .fb_create = intel_user_framebuffer_create,
  1404. .fb_changed = intelfb_probe,
  1405. };
  1406. void intel_modeset_init(struct drm_device *dev)
  1407. {
  1408. int num_pipe;
  1409. int i;
  1410. drm_mode_config_init(dev);
  1411. dev->mode_config.min_width = 0;
  1412. dev->mode_config.min_height = 0;
  1413. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  1414. if (IS_I965G(dev)) {
  1415. dev->mode_config.max_width = 8192;
  1416. dev->mode_config.max_height = 8192;
  1417. } else {
  1418. dev->mode_config.max_width = 2048;
  1419. dev->mode_config.max_height = 2048;
  1420. }
  1421. /* set memory base */
  1422. if (IS_I9XX(dev))
  1423. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  1424. else
  1425. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  1426. if (IS_MOBILE(dev) || IS_I9XX(dev))
  1427. num_pipe = 2;
  1428. else
  1429. num_pipe = 1;
  1430. DRM_DEBUG("%d display pipe%s available.\n",
  1431. num_pipe, num_pipe > 1 ? "s" : "");
  1432. for (i = 0; i < num_pipe; i++) {
  1433. intel_crtc_init(dev, i);
  1434. }
  1435. intel_setup_outputs(dev);
  1436. }
  1437. void intel_modeset_cleanup(struct drm_device *dev)
  1438. {
  1439. drm_mode_config_cleanup(dev);
  1440. }
  1441. /* current intel driver doesn't take advantage of encoders
  1442. always give back the encoder for the connector
  1443. */
  1444. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  1445. {
  1446. struct intel_output *intel_output = to_intel_output(connector);
  1447. return &intel_output->enc;
  1448. }