Path: blob/master/drivers/gpu/drm/i915/intel_display.c
15113 views
/*1* Copyright © 2006-2007 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*22* Authors:23* Eric Anholt <[email protected]>24*/2526#include <linux/module.h>27#include <linux/input.h>28#include <linux/i2c.h>29#include <linux/kernel.h>30#include <linux/slab.h>31#include <linux/vgaarb.h>32#include "drmP.h"33#include "intel_drv.h"34#include "i915_drm.h"35#include "i915_drv.h"36#include "i915_trace.h"37#include "drm_dp_helper.h"3839#include "drm_crtc_helper.h"4041#define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))4243bool intel_pipe_has_type (struct drm_crtc *crtc, int type);44static void intel_update_watermarks(struct drm_device *dev);45static void intel_increase_pllclock(struct drm_crtc *crtc);46static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);4748typedef struct {49/* given values */50int n;51int m1, m2;52int p1, p2;53/* derived values */54int dot;55int vco;56int m;57int p;58} intel_clock_t;5960typedef struct {61int min, max;62} intel_range_t;6364typedef struct {65int dot_limit;66int p2_slow, p2_fast;67} intel_p2_t;6869#define INTEL_P2_NUM 270typedef struct intel_limit intel_limit_t;71struct intel_limit {72intel_range_t dot, vco, n, m, m1, m2, p, p1;73intel_p2_t p2;74bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,75int, int, intel_clock_t *);76};7778/* FDI */79#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */8081static bool82intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,83int target, int refclk, intel_clock_t *best_clock);84static bool85intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,86int target, int refclk, intel_clock_t *best_clock);8788static bool89intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,90int target, int refclk, intel_clock_t *best_clock);91static bool92intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,93int target, int refclk, intel_clock_t *best_clock);9495static inline u32 /* units of 100MHz */96intel_fdi_link_freq(struct drm_device *dev)97{98if (IS_GEN5(dev)) {99struct drm_i915_private *dev_priv = dev->dev_private;100return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;101} else102return 27;103}104105static const intel_limit_t intel_limits_i8xx_dvo = {106.dot = { .min = 25000, .max = 350000 },107.vco = { .min = 930000, .max = 1400000 },108.n = { .min = 3, .max = 16 },109.m = { .min = 96, .max = 140 },110.m1 = { .min = 18, .max = 26 },111.m2 = { .min = 6, .max = 16 },112.p = { .min = 4, .max = 128 },113.p1 = { .min = 2, .max = 33 },114.p2 = { .dot_limit = 165000,115.p2_slow = 4, .p2_fast = 2 },116.find_pll = intel_find_best_PLL,117};118119static const intel_limit_t intel_limits_i8xx_lvds = {120.dot = { .min = 25000, .max = 350000 },121.vco = { .min = 930000, .max = 1400000 },122.n = { .min = 3, .max = 16 },123.m = { .min = 96, .max = 140 },124.m1 = { .min = 18, .max = 26 },125.m2 = { .min = 6, .max = 16 },126.p = { .min = 4, .max = 128 },127.p1 = { .min = 1, .max = 6 },128.p2 = { .dot_limit = 165000,129.p2_slow = 14, .p2_fast = 7 },130.find_pll = intel_find_best_PLL,131};132133static const intel_limit_t intel_limits_i9xx_sdvo = {134.dot = { .min = 20000, .max = 400000 },135.vco = { .min = 1400000, .max = 2800000 },136.n = { .min = 1, .max = 6 },137.m = { .min = 70, .max = 120 },138.m1 = { .min = 10, .max = 22 },139.m2 = { .min = 5, .max = 9 },140.p = { .min = 5, .max = 80 },141.p1 = { .min = 1, .max = 8 },142.p2 = { .dot_limit = 200000,143.p2_slow = 10, .p2_fast = 5 },144.find_pll = intel_find_best_PLL,145};146147static const intel_limit_t intel_limits_i9xx_lvds = {148.dot = { .min = 20000, .max = 400000 },149.vco = { .min = 1400000, .max = 2800000 },150.n = { .min = 1, .max = 6 },151.m = { .min = 70, .max = 120 },152.m1 = { .min = 10, .max = 22 },153.m2 = { .min = 5, .max = 9 },154.p = { .min = 7, .max = 98 },155.p1 = { .min = 1, .max = 8 },156.p2 = { .dot_limit = 112000,157.p2_slow = 14, .p2_fast = 7 },158.find_pll = intel_find_best_PLL,159};160161162static const intel_limit_t intel_limits_g4x_sdvo = {163.dot = { .min = 25000, .max = 270000 },164.vco = { .min = 1750000, .max = 3500000},165.n = { .min = 1, .max = 4 },166.m = { .min = 104, .max = 138 },167.m1 = { .min = 17, .max = 23 },168.m2 = { .min = 5, .max = 11 },169.p = { .min = 10, .max = 30 },170.p1 = { .min = 1, .max = 3},171.p2 = { .dot_limit = 270000,172.p2_slow = 10,173.p2_fast = 10174},175.find_pll = intel_g4x_find_best_PLL,176};177178static const intel_limit_t intel_limits_g4x_hdmi = {179.dot = { .min = 22000, .max = 400000 },180.vco = { .min = 1750000, .max = 3500000},181.n = { .min = 1, .max = 4 },182.m = { .min = 104, .max = 138 },183.m1 = { .min = 16, .max = 23 },184.m2 = { .min = 5, .max = 11 },185.p = { .min = 5, .max = 80 },186.p1 = { .min = 1, .max = 8},187.p2 = { .dot_limit = 165000,188.p2_slow = 10, .p2_fast = 5 },189.find_pll = intel_g4x_find_best_PLL,190};191192static const intel_limit_t intel_limits_g4x_single_channel_lvds = {193.dot = { .min = 20000, .max = 115000 },194.vco = { .min = 1750000, .max = 3500000 },195.n = { .min = 1, .max = 3 },196.m = { .min = 104, .max = 138 },197.m1 = { .min = 17, .max = 23 },198.m2 = { .min = 5, .max = 11 },199.p = { .min = 28, .max = 112 },200.p1 = { .min = 2, .max = 8 },201.p2 = { .dot_limit = 0,202.p2_slow = 14, .p2_fast = 14203},204.find_pll = intel_g4x_find_best_PLL,205};206207static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {208.dot = { .min = 80000, .max = 224000 },209.vco = { .min = 1750000, .max = 3500000 },210.n = { .min = 1, .max = 3 },211.m = { .min = 104, .max = 138 },212.m1 = { .min = 17, .max = 23 },213.m2 = { .min = 5, .max = 11 },214.p = { .min = 14, .max = 42 },215.p1 = { .min = 2, .max = 6 },216.p2 = { .dot_limit = 0,217.p2_slow = 7, .p2_fast = 7218},219.find_pll = intel_g4x_find_best_PLL,220};221222static const intel_limit_t intel_limits_g4x_display_port = {223.dot = { .min = 161670, .max = 227000 },224.vco = { .min = 1750000, .max = 3500000},225.n = { .min = 1, .max = 2 },226.m = { .min = 97, .max = 108 },227.m1 = { .min = 0x10, .max = 0x12 },228.m2 = { .min = 0x05, .max = 0x06 },229.p = { .min = 10, .max = 20 },230.p1 = { .min = 1, .max = 2},231.p2 = { .dot_limit = 0,232.p2_slow = 10, .p2_fast = 10 },233.find_pll = intel_find_pll_g4x_dp,234};235236static const intel_limit_t intel_limits_pineview_sdvo = {237.dot = { .min = 20000, .max = 400000},238.vco = { .min = 1700000, .max = 3500000 },239/* Pineview's Ncounter is a ring counter */240.n = { .min = 3, .max = 6 },241.m = { .min = 2, .max = 256 },242/* Pineview only has one combined m divider, which we treat as m2. */243.m1 = { .min = 0, .max = 0 },244.m2 = { .min = 0, .max = 254 },245.p = { .min = 5, .max = 80 },246.p1 = { .min = 1, .max = 8 },247.p2 = { .dot_limit = 200000,248.p2_slow = 10, .p2_fast = 5 },249.find_pll = intel_find_best_PLL,250};251252static const intel_limit_t intel_limits_pineview_lvds = {253.dot = { .min = 20000, .max = 400000 },254.vco = { .min = 1700000, .max = 3500000 },255.n = { .min = 3, .max = 6 },256.m = { .min = 2, .max = 256 },257.m1 = { .min = 0, .max = 0 },258.m2 = { .min = 0, .max = 254 },259.p = { .min = 7, .max = 112 },260.p1 = { .min = 1, .max = 8 },261.p2 = { .dot_limit = 112000,262.p2_slow = 14, .p2_fast = 14 },263.find_pll = intel_find_best_PLL,264};265266/* Ironlake / Sandybridge267*268* We calculate clock using (register_value + 2) for N/M1/M2, so here269* the range value for them is (actual_value - 2).270*/271static const intel_limit_t intel_limits_ironlake_dac = {272.dot = { .min = 25000, .max = 350000 },273.vco = { .min = 1760000, .max = 3510000 },274.n = { .min = 1, .max = 5 },275.m = { .min = 79, .max = 127 },276.m1 = { .min = 12, .max = 22 },277.m2 = { .min = 5, .max = 9 },278.p = { .min = 5, .max = 80 },279.p1 = { .min = 1, .max = 8 },280.p2 = { .dot_limit = 225000,281.p2_slow = 10, .p2_fast = 5 },282.find_pll = intel_g4x_find_best_PLL,283};284285static const intel_limit_t intel_limits_ironlake_single_lvds = {286.dot = { .min = 25000, .max = 350000 },287.vco = { .min = 1760000, .max = 3510000 },288.n = { .min = 1, .max = 3 },289.m = { .min = 79, .max = 118 },290.m1 = { .min = 12, .max = 22 },291.m2 = { .min = 5, .max = 9 },292.p = { .min = 28, .max = 112 },293.p1 = { .min = 2, .max = 8 },294.p2 = { .dot_limit = 225000,295.p2_slow = 14, .p2_fast = 14 },296.find_pll = intel_g4x_find_best_PLL,297};298299static const intel_limit_t intel_limits_ironlake_dual_lvds = {300.dot = { .min = 25000, .max = 350000 },301.vco = { .min = 1760000, .max = 3510000 },302.n = { .min = 1, .max = 3 },303.m = { .min = 79, .max = 127 },304.m1 = { .min = 12, .max = 22 },305.m2 = { .min = 5, .max = 9 },306.p = { .min = 14, .max = 56 },307.p1 = { .min = 2, .max = 8 },308.p2 = { .dot_limit = 225000,309.p2_slow = 7, .p2_fast = 7 },310.find_pll = intel_g4x_find_best_PLL,311};312313/* LVDS 100mhz refclk limits. */314static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {315.dot = { .min = 25000, .max = 350000 },316.vco = { .min = 1760000, .max = 3510000 },317.n = { .min = 1, .max = 2 },318.m = { .min = 79, .max = 126 },319.m1 = { .min = 12, .max = 22 },320.m2 = { .min = 5, .max = 9 },321.p = { .min = 28, .max = 112 },322.p1 = { .min = 2,.max = 8 },323.p2 = { .dot_limit = 225000,324.p2_slow = 14, .p2_fast = 14 },325.find_pll = intel_g4x_find_best_PLL,326};327328static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {329.dot = { .min = 25000, .max = 350000 },330.vco = { .min = 1760000, .max = 3510000 },331.n = { .min = 1, .max = 3 },332.m = { .min = 79, .max = 126 },333.m1 = { .min = 12, .max = 22 },334.m2 = { .min = 5, .max = 9 },335.p = { .min = 14, .max = 42 },336.p1 = { .min = 2,.max = 6 },337.p2 = { .dot_limit = 225000,338.p2_slow = 7, .p2_fast = 7 },339.find_pll = intel_g4x_find_best_PLL,340};341342static const intel_limit_t intel_limits_ironlake_display_port = {343.dot = { .min = 25000, .max = 350000 },344.vco = { .min = 1760000, .max = 3510000},345.n = { .min = 1, .max = 2 },346.m = { .min = 81, .max = 90 },347.m1 = { .min = 12, .max = 22 },348.m2 = { .min = 5, .max = 9 },349.p = { .min = 10, .max = 20 },350.p1 = { .min = 1, .max = 2},351.p2 = { .dot_limit = 0,352.p2_slow = 10, .p2_fast = 10 },353.find_pll = intel_find_pll_ironlake_dp,354};355356static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,357int refclk)358{359struct drm_device *dev = crtc->dev;360struct drm_i915_private *dev_priv = dev->dev_private;361const intel_limit_t *limit;362363if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {364if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==365LVDS_CLKB_POWER_UP) {366/* LVDS dual channel */367if (refclk == 100000)368limit = &intel_limits_ironlake_dual_lvds_100m;369else370limit = &intel_limits_ironlake_dual_lvds;371} else {372if (refclk == 100000)373limit = &intel_limits_ironlake_single_lvds_100m;374else375limit = &intel_limits_ironlake_single_lvds;376}377} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||378HAS_eDP)379limit = &intel_limits_ironlake_display_port;380else381limit = &intel_limits_ironlake_dac;382383return limit;384}385386static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)387{388struct drm_device *dev = crtc->dev;389struct drm_i915_private *dev_priv = dev->dev_private;390const intel_limit_t *limit;391392if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {393if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==394LVDS_CLKB_POWER_UP)395/* LVDS with dual channel */396limit = &intel_limits_g4x_dual_channel_lvds;397else398/* LVDS with dual channel */399limit = &intel_limits_g4x_single_channel_lvds;400} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||401intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {402limit = &intel_limits_g4x_hdmi;403} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {404limit = &intel_limits_g4x_sdvo;405} else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {406limit = &intel_limits_g4x_display_port;407} else /* The option is for other outputs */408limit = &intel_limits_i9xx_sdvo;409410return limit;411}412413static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)414{415struct drm_device *dev = crtc->dev;416const intel_limit_t *limit;417418if (HAS_PCH_SPLIT(dev))419limit = intel_ironlake_limit(crtc, refclk);420else if (IS_G4X(dev)) {421limit = intel_g4x_limit(crtc);422} else if (IS_PINEVIEW(dev)) {423if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))424limit = &intel_limits_pineview_lvds;425else426limit = &intel_limits_pineview_sdvo;427} else if (!IS_GEN2(dev)) {428if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))429limit = &intel_limits_i9xx_lvds;430else431limit = &intel_limits_i9xx_sdvo;432} else {433if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))434limit = &intel_limits_i8xx_lvds;435else436limit = &intel_limits_i8xx_dvo;437}438return limit;439}440441/* m1 is reserved as 0 in Pineview, n is a ring counter */442static void pineview_clock(int refclk, intel_clock_t *clock)443{444clock->m = clock->m2 + 2;445clock->p = clock->p1 * clock->p2;446clock->vco = refclk * clock->m / clock->n;447clock->dot = clock->vco / clock->p;448}449450static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)451{452if (IS_PINEVIEW(dev)) {453pineview_clock(refclk, clock);454return;455}456clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);457clock->p = clock->p1 * clock->p2;458clock->vco = refclk * clock->m / (clock->n + 2);459clock->dot = clock->vco / clock->p;460}461462/**463* Returns whether any output on the specified pipe is of the specified type464*/465bool intel_pipe_has_type(struct drm_crtc *crtc, int type)466{467struct drm_device *dev = crtc->dev;468struct drm_mode_config *mode_config = &dev->mode_config;469struct intel_encoder *encoder;470471list_for_each_entry(encoder, &mode_config->encoder_list, base.head)472if (encoder->base.crtc == crtc && encoder->type == type)473return true;474475return false;476}477478#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)479/**480* Returns whether the given set of divisors are valid for a given refclk with481* the given connectors.482*/483484static bool intel_PLL_is_valid(struct drm_device *dev,485const intel_limit_t *limit,486const intel_clock_t *clock)487{488if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)489INTELPllInvalid ("p1 out of range\n");490if (clock->p < limit->p.min || limit->p.max < clock->p)491INTELPllInvalid ("p out of range\n");492if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)493INTELPllInvalid ("m2 out of range\n");494if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)495INTELPllInvalid ("m1 out of range\n");496if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))497INTELPllInvalid ("m1 <= m2\n");498if (clock->m < limit->m.min || limit->m.max < clock->m)499INTELPllInvalid ("m out of range\n");500if (clock->n < limit->n.min || limit->n.max < clock->n)501INTELPllInvalid ("n out of range\n");502if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)503INTELPllInvalid ("vco out of range\n");504/* XXX: We may need to be checking "Dot clock" depending on the multiplier,505* connector, etc., rather than just a single range.506*/507if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)508INTELPllInvalid ("dot out of range\n");509510return true;511}512513static bool514intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,515int target, int refclk, intel_clock_t *best_clock)516517{518struct drm_device *dev = crtc->dev;519struct drm_i915_private *dev_priv = dev->dev_private;520intel_clock_t clock;521int err = target;522523if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&524(I915_READ(LVDS)) != 0) {525/*526* For LVDS, if the panel is on, just rely on its current527* settings for dual-channel. We haven't figured out how to528* reliably set up different single/dual channel state, if we529* even can.530*/531if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==532LVDS_CLKB_POWER_UP)533clock.p2 = limit->p2.p2_fast;534else535clock.p2 = limit->p2.p2_slow;536} else {537if (target < limit->p2.dot_limit)538clock.p2 = limit->p2.p2_slow;539else540clock.p2 = limit->p2.p2_fast;541}542543memset (best_clock, 0, sizeof (*best_clock));544545for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;546clock.m1++) {547for (clock.m2 = limit->m2.min;548clock.m2 <= limit->m2.max; clock.m2++) {549/* m1 is always 0 in Pineview */550if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))551break;552for (clock.n = limit->n.min;553clock.n <= limit->n.max; clock.n++) {554for (clock.p1 = limit->p1.min;555clock.p1 <= limit->p1.max; clock.p1++) {556int this_err;557558intel_clock(dev, refclk, &clock);559if (!intel_PLL_is_valid(dev, limit,560&clock))561continue;562563this_err = abs(clock.dot - target);564if (this_err < err) {565*best_clock = clock;566err = this_err;567}568}569}570}571}572573return (err != target);574}575576static bool577intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,578int target, int refclk, intel_clock_t *best_clock)579{580struct drm_device *dev = crtc->dev;581struct drm_i915_private *dev_priv = dev->dev_private;582intel_clock_t clock;583int max_n;584bool found;585/* approximately equals target * 0.00585 */586int err_most = (target >> 8) + (target >> 9);587found = false;588589if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {590int lvds_reg;591592if (HAS_PCH_SPLIT(dev))593lvds_reg = PCH_LVDS;594else595lvds_reg = LVDS;596if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==597LVDS_CLKB_POWER_UP)598clock.p2 = limit->p2.p2_fast;599else600clock.p2 = limit->p2.p2_slow;601} else {602if (target < limit->p2.dot_limit)603clock.p2 = limit->p2.p2_slow;604else605clock.p2 = limit->p2.p2_fast;606}607608memset(best_clock, 0, sizeof(*best_clock));609max_n = limit->n.max;610/* based on hardware requirement, prefer smaller n to precision */611for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {612/* based on hardware requirement, prefere larger m1,m2 */613for (clock.m1 = limit->m1.max;614clock.m1 >= limit->m1.min; clock.m1--) {615for (clock.m2 = limit->m2.max;616clock.m2 >= limit->m2.min; clock.m2--) {617for (clock.p1 = limit->p1.max;618clock.p1 >= limit->p1.min; clock.p1--) {619int this_err;620621intel_clock(dev, refclk, &clock);622if (!intel_PLL_is_valid(dev, limit,623&clock))624continue;625626this_err = abs(clock.dot - target);627if (this_err < err_most) {628*best_clock = clock;629err_most = this_err;630max_n = clock.n;631found = true;632}633}634}635}636}637return found;638}639640static bool641intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,642int target, int refclk, intel_clock_t *best_clock)643{644struct drm_device *dev = crtc->dev;645intel_clock_t clock;646647if (target < 200000) {648clock.n = 1;649clock.p1 = 2;650clock.p2 = 10;651clock.m1 = 12;652clock.m2 = 9;653} else {654clock.n = 2;655clock.p1 = 1;656clock.p2 = 10;657clock.m1 = 14;658clock.m2 = 8;659}660intel_clock(dev, refclk, &clock);661memcpy(best_clock, &clock, sizeof(intel_clock_t));662return true;663}664665/* DisplayPort has only two frequencies, 162MHz and 270MHz */666static bool667intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,668int target, int refclk, intel_clock_t *best_clock)669{670intel_clock_t clock;671if (target < 200000) {672clock.p1 = 2;673clock.p2 = 10;674clock.n = 2;675clock.m1 = 23;676clock.m2 = 8;677} else {678clock.p1 = 1;679clock.p2 = 10;680clock.n = 1;681clock.m1 = 14;682clock.m2 = 2;683}684clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);685clock.p = (clock.p1 * clock.p2);686clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;687clock.vco = 0;688memcpy(best_clock, &clock, sizeof(intel_clock_t));689return true;690}691692/**693* intel_wait_for_vblank - wait for vblank on a given pipe694* @dev: drm device695* @pipe: pipe to wait for696*697* Wait for vblank to occur on a given pipe. Needed for various bits of698* mode setting code.699*/700void intel_wait_for_vblank(struct drm_device *dev, int pipe)701{702struct drm_i915_private *dev_priv = dev->dev_private;703int pipestat_reg = PIPESTAT(pipe);704705/* Clear existing vblank status. Note this will clear any other706* sticky status fields as well.707*708* This races with i915_driver_irq_handler() with the result709* that either function could miss a vblank event. Here it is not710* fatal, as we will either wait upon the next vblank interrupt or711* timeout. Generally speaking intel_wait_for_vblank() is only712* called during modeset at which time the GPU should be idle and713* should *not* be performing page flips and thus not waiting on714* vblanks...715* Currently, the result of us stealing a vblank from the irq716* handler is that a single frame will be skipped during swapbuffers.717*/718I915_WRITE(pipestat_reg,719I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);720721/* Wait for vblank interrupt bit to set */722if (wait_for(I915_READ(pipestat_reg) &723PIPE_VBLANK_INTERRUPT_STATUS,72450))725DRM_DEBUG_KMS("vblank wait timed out\n");726}727728/*729* intel_wait_for_pipe_off - wait for pipe to turn off730* @dev: drm device731* @pipe: pipe to wait for732*733* After disabling a pipe, we can't wait for vblank in the usual way,734* spinning on the vblank interrupt status bit, since we won't actually735* see an interrupt when the pipe is disabled.736*737* On Gen4 and above:738* wait for the pipe register state bit to turn off739*740* Otherwise:741* wait for the display line value to settle (it usually742* ends up stopping at the start of the next frame).743*744*/745void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)746{747struct drm_i915_private *dev_priv = dev->dev_private;748749if (INTEL_INFO(dev)->gen >= 4) {750int reg = PIPECONF(pipe);751752/* Wait for the Pipe State to go off */753if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,754100))755DRM_DEBUG_KMS("pipe_off wait timed out\n");756} else {757u32 last_line;758int reg = PIPEDSL(pipe);759unsigned long timeout = jiffies + msecs_to_jiffies(100);760761/* Wait for the display line to settle */762do {763last_line = I915_READ(reg) & DSL_LINEMASK;764mdelay(5);765} while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&766time_after(timeout, jiffies));767if (time_after(jiffies, timeout))768DRM_DEBUG_KMS("pipe_off wait timed out\n");769}770}771772static const char *state_string(bool enabled)773{774return enabled ? "on" : "off";775}776777/* Only for pre-ILK configs */778static void assert_pll(struct drm_i915_private *dev_priv,779enum pipe pipe, bool state)780{781int reg;782u32 val;783bool cur_state;784785reg = DPLL(pipe);786val = I915_READ(reg);787cur_state = !!(val & DPLL_VCO_ENABLE);788WARN(cur_state != state,789"PLL state assertion failure (expected %s, current %s)\n",790state_string(state), state_string(cur_state));791}792#define assert_pll_enabled(d, p) assert_pll(d, p, true)793#define assert_pll_disabled(d, p) assert_pll(d, p, false)794795/* For ILK+ */796static void assert_pch_pll(struct drm_i915_private *dev_priv,797enum pipe pipe, bool state)798{799int reg;800u32 val;801bool cur_state;802803reg = PCH_DPLL(pipe);804val = I915_READ(reg);805cur_state = !!(val & DPLL_VCO_ENABLE);806WARN(cur_state != state,807"PCH PLL state assertion failure (expected %s, current %s)\n",808state_string(state), state_string(cur_state));809}810#define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)811#define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)812813static void assert_fdi_tx(struct drm_i915_private *dev_priv,814enum pipe pipe, bool state)815{816int reg;817u32 val;818bool cur_state;819820reg = FDI_TX_CTL(pipe);821val = I915_READ(reg);822cur_state = !!(val & FDI_TX_ENABLE);823WARN(cur_state != state,824"FDI TX state assertion failure (expected %s, current %s)\n",825state_string(state), state_string(cur_state));826}827#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)828#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)829830static void assert_fdi_rx(struct drm_i915_private *dev_priv,831enum pipe pipe, bool state)832{833int reg;834u32 val;835bool cur_state;836837reg = FDI_RX_CTL(pipe);838val = I915_READ(reg);839cur_state = !!(val & FDI_RX_ENABLE);840WARN(cur_state != state,841"FDI RX state assertion failure (expected %s, current %s)\n",842state_string(state), state_string(cur_state));843}844#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)845#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)846847static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,848enum pipe pipe)849{850int reg;851u32 val;852853/* ILK FDI PLL is always enabled */854if (dev_priv->info->gen == 5)855return;856857reg = FDI_TX_CTL(pipe);858val = I915_READ(reg);859WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");860}861862static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,863enum pipe pipe)864{865int reg;866u32 val;867868reg = FDI_RX_CTL(pipe);869val = I915_READ(reg);870WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");871}872873static void assert_panel_unlocked(struct drm_i915_private *dev_priv,874enum pipe pipe)875{876int pp_reg, lvds_reg;877u32 val;878enum pipe panel_pipe = PIPE_A;879bool locked = locked;880881if (HAS_PCH_SPLIT(dev_priv->dev)) {882pp_reg = PCH_PP_CONTROL;883lvds_reg = PCH_LVDS;884} else {885pp_reg = PP_CONTROL;886lvds_reg = LVDS;887}888889val = I915_READ(pp_reg);890if (!(val & PANEL_POWER_ON) ||891((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))892locked = false;893894if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)895panel_pipe = PIPE_B;896897WARN(panel_pipe == pipe && locked,898"panel assertion failure, pipe %c regs locked\n",899pipe_name(pipe));900}901902static void assert_pipe(struct drm_i915_private *dev_priv,903enum pipe pipe, bool state)904{905int reg;906u32 val;907bool cur_state;908909reg = PIPECONF(pipe);910val = I915_READ(reg);911cur_state = !!(val & PIPECONF_ENABLE);912WARN(cur_state != state,913"pipe %c assertion failure (expected %s, current %s)\n",914pipe_name(pipe), state_string(state), state_string(cur_state));915}916#define assert_pipe_enabled(d, p) assert_pipe(d, p, true)917#define assert_pipe_disabled(d, p) assert_pipe(d, p, false)918919static void assert_plane_enabled(struct drm_i915_private *dev_priv,920enum plane plane)921{922int reg;923u32 val;924925reg = DSPCNTR(plane);926val = I915_READ(reg);927WARN(!(val & DISPLAY_PLANE_ENABLE),928"plane %c assertion failure, should be active but is disabled\n",929plane_name(plane));930}931932static void assert_planes_disabled(struct drm_i915_private *dev_priv,933enum pipe pipe)934{935int reg, i;936u32 val;937int cur_pipe;938939/* Planes are fixed to pipes on ILK+ */940if (HAS_PCH_SPLIT(dev_priv->dev))941return;942943/* Need to check both planes against the pipe */944for (i = 0; i < 2; i++) {945reg = DSPCNTR(i);946val = I915_READ(reg);947cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>948DISPPLANE_SEL_PIPE_SHIFT;949WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,950"plane %c assertion failure, should be off on pipe %c but is still active\n",951plane_name(i), pipe_name(pipe));952}953}954955static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)956{957u32 val;958bool enabled;959960val = I915_READ(PCH_DREF_CONTROL);961enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |962DREF_SUPERSPREAD_SOURCE_MASK));963WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");964}965966static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,967enum pipe pipe)968{969int reg;970u32 val;971bool enabled;972973reg = TRANSCONF(pipe);974val = I915_READ(reg);975enabled = !!(val & TRANS_ENABLE);976WARN(enabled,977"transcoder assertion failed, should be off on pipe %c but is still active\n",978pipe_name(pipe));979}980981static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,982enum pipe pipe, int reg)983{984u32 val = I915_READ(reg);985WARN(DP_PIPE_ENABLED(val, pipe),986"PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",987reg, pipe_name(pipe));988}989990static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,991enum pipe pipe, int reg)992{993u32 val = I915_READ(reg);994WARN(HDMI_PIPE_ENABLED(val, pipe),995"PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",996reg, pipe_name(pipe));997}998999static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,1000enum pipe pipe)1001{1002int reg;1003u32 val;10041005assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B);1006assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C);1007assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D);10081009reg = PCH_ADPA;1010val = I915_READ(reg);1011WARN(ADPA_PIPE_ENABLED(val, pipe),1012"PCH VGA enabled on transcoder %c, should be disabled\n",1013pipe_name(pipe));10141015reg = PCH_LVDS;1016val = I915_READ(reg);1017WARN(LVDS_PIPE_ENABLED(val, pipe),1018"PCH LVDS enabled on transcoder %c, should be disabled\n",1019pipe_name(pipe));10201021assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);1022assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);1023assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);1024}10251026/**1027* intel_enable_pll - enable a PLL1028* @dev_priv: i915 private structure1029* @pipe: pipe PLL to enable1030*1031* Enable @pipe's PLL so we can start pumping pixels from a plane. Check to1032* make sure the PLL reg is writable first though, since the panel write1033* protect mechanism may be enabled.1034*1035* Note! This is for pre-ILK only.1036*/1037static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)1038{1039int reg;1040u32 val;10411042/* No really, not for ILK+ */1043BUG_ON(dev_priv->info->gen >= 5);10441045/* PLL is protected by panel, make sure we can write it */1046if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))1047assert_panel_unlocked(dev_priv, pipe);10481049reg = DPLL(pipe);1050val = I915_READ(reg);1051val |= DPLL_VCO_ENABLE;10521053/* We do this three times for luck */1054I915_WRITE(reg, val);1055POSTING_READ(reg);1056udelay(150); /* wait for warmup */1057I915_WRITE(reg, val);1058POSTING_READ(reg);1059udelay(150); /* wait for warmup */1060I915_WRITE(reg, val);1061POSTING_READ(reg);1062udelay(150); /* wait for warmup */1063}10641065/**1066* intel_disable_pll - disable a PLL1067* @dev_priv: i915 private structure1068* @pipe: pipe PLL to disable1069*1070* Disable the PLL for @pipe, making sure the pipe is off first.1071*1072* Note! This is for pre-ILK only.1073*/1074static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)1075{1076int reg;1077u32 val;10781079/* Don't disable pipe A or pipe A PLLs if needed */1080if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))1081return;10821083/* Make sure the pipe isn't still relying on us */1084assert_pipe_disabled(dev_priv, pipe);10851086reg = DPLL(pipe);1087val = I915_READ(reg);1088val &= ~DPLL_VCO_ENABLE;1089I915_WRITE(reg, val);1090POSTING_READ(reg);1091}10921093/**1094* intel_enable_pch_pll - enable PCH PLL1095* @dev_priv: i915 private structure1096* @pipe: pipe PLL to enable1097*1098* The PCH PLL needs to be enabled before the PCH transcoder, since it1099* drives the transcoder clock.1100*/1101static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,1102enum pipe pipe)1103{1104int reg;1105u32 val;11061107/* PCH only available on ILK+ */1108BUG_ON(dev_priv->info->gen < 5);11091110/* PCH refclock must be enabled first */1111assert_pch_refclk_enabled(dev_priv);11121113reg = PCH_DPLL(pipe);1114val = I915_READ(reg);1115val |= DPLL_VCO_ENABLE;1116I915_WRITE(reg, val);1117POSTING_READ(reg);1118udelay(200);1119}11201121static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,1122enum pipe pipe)1123{1124int reg;1125u32 val;11261127/* PCH only available on ILK+ */1128BUG_ON(dev_priv->info->gen < 5);11291130/* Make sure transcoder isn't still depending on us */1131assert_transcoder_disabled(dev_priv, pipe);11321133reg = PCH_DPLL(pipe);1134val = I915_READ(reg);1135val &= ~DPLL_VCO_ENABLE;1136I915_WRITE(reg, val);1137POSTING_READ(reg);1138udelay(200);1139}11401141static void intel_enable_transcoder(struct drm_i915_private *dev_priv,1142enum pipe pipe)1143{1144int reg;1145u32 val;11461147/* PCH only available on ILK+ */1148BUG_ON(dev_priv->info->gen < 5);11491150/* Make sure PCH DPLL is enabled */1151assert_pch_pll_enabled(dev_priv, pipe);11521153/* FDI must be feeding us bits for PCH ports */1154assert_fdi_tx_enabled(dev_priv, pipe);1155assert_fdi_rx_enabled(dev_priv, pipe);11561157reg = TRANSCONF(pipe);1158val = I915_READ(reg);1159/*1160* make the BPC in transcoder be consistent with1161* that in pipeconf reg.1162*/1163val &= ~PIPE_BPC_MASK;1164val |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;1165I915_WRITE(reg, val | TRANS_ENABLE);1166if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))1167DRM_ERROR("failed to enable transcoder %d\n", pipe);1168}11691170static void intel_disable_transcoder(struct drm_i915_private *dev_priv,1171enum pipe pipe)1172{1173int reg;1174u32 val;11751176/* FDI relies on the transcoder */1177assert_fdi_tx_disabled(dev_priv, pipe);1178assert_fdi_rx_disabled(dev_priv, pipe);11791180/* Ports must be off as well */1181assert_pch_ports_disabled(dev_priv, pipe);11821183reg = TRANSCONF(pipe);1184val = I915_READ(reg);1185val &= ~TRANS_ENABLE;1186I915_WRITE(reg, val);1187/* wait for PCH transcoder off, transcoder state */1188if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))1189DRM_ERROR("failed to disable transcoder\n");1190}11911192/**1193* intel_enable_pipe - enable a pipe, asserting requirements1194* @dev_priv: i915 private structure1195* @pipe: pipe to enable1196* @pch_port: on ILK+, is this pipe driving a PCH port or not1197*1198* Enable @pipe, making sure that various hardware specific requirements1199* are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.1200*1201* @pipe should be %PIPE_A or %PIPE_B.1202*1203* Will wait until the pipe is actually running (i.e. first vblank) before1204* returning.1205*/1206static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,1207bool pch_port)1208{1209int reg;1210u32 val;12111212/*1213* A pipe without a PLL won't actually be able to drive bits from1214* a plane. On ILK+ the pipe PLLs are integrated, so we don't1215* need the check.1216*/1217if (!HAS_PCH_SPLIT(dev_priv->dev))1218assert_pll_enabled(dev_priv, pipe);1219else {1220if (pch_port) {1221/* if driving the PCH, we need FDI enabled */1222assert_fdi_rx_pll_enabled(dev_priv, pipe);1223assert_fdi_tx_pll_enabled(dev_priv, pipe);1224}1225/* FIXME: assert CPU port conditions for SNB+ */1226}12271228reg = PIPECONF(pipe);1229val = I915_READ(reg);1230if (val & PIPECONF_ENABLE)1231return;12321233I915_WRITE(reg, val | PIPECONF_ENABLE);1234intel_wait_for_vblank(dev_priv->dev, pipe);1235}12361237/**1238* intel_disable_pipe - disable a pipe, asserting requirements1239* @dev_priv: i915 private structure1240* @pipe: pipe to disable1241*1242* Disable @pipe, making sure that various hardware specific requirements1243* are met, if applicable, e.g. plane disabled, panel fitter off, etc.1244*1245* @pipe should be %PIPE_A or %PIPE_B.1246*1247* Will wait until the pipe has shut down before returning.1248*/1249static void intel_disable_pipe(struct drm_i915_private *dev_priv,1250enum pipe pipe)1251{1252int reg;1253u32 val;12541255/*1256* Make sure planes won't keep trying to pump pixels to us,1257* or we might hang the display.1258*/1259assert_planes_disabled(dev_priv, pipe);12601261/* Don't disable pipe A or pipe A PLLs if needed */1262if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))1263return;12641265reg = PIPECONF(pipe);1266val = I915_READ(reg);1267if ((val & PIPECONF_ENABLE) == 0)1268return;12691270I915_WRITE(reg, val & ~PIPECONF_ENABLE);1271intel_wait_for_pipe_off(dev_priv->dev, pipe);1272}12731274/**1275* intel_enable_plane - enable a display plane on a given pipe1276* @dev_priv: i915 private structure1277* @plane: plane to enable1278* @pipe: pipe being fed1279*1280* Enable @plane on @pipe, making sure that @pipe is running first.1281*/1282static void intel_enable_plane(struct drm_i915_private *dev_priv,1283enum plane plane, enum pipe pipe)1284{1285int reg;1286u32 val;12871288/* If the pipe isn't enabled, we can't pump pixels and may hang */1289assert_pipe_enabled(dev_priv, pipe);12901291reg = DSPCNTR(plane);1292val = I915_READ(reg);1293if (val & DISPLAY_PLANE_ENABLE)1294return;12951296I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);1297intel_wait_for_vblank(dev_priv->dev, pipe);1298}12991300/*1301* Plane regs are double buffered, going from enabled->disabled needs a1302* trigger in order to latch. The display address reg provides this.1303*/1304static void intel_flush_display_plane(struct drm_i915_private *dev_priv,1305enum plane plane)1306{1307u32 reg = DSPADDR(plane);1308I915_WRITE(reg, I915_READ(reg));1309}13101311/**1312* intel_disable_plane - disable a display plane1313* @dev_priv: i915 private structure1314* @plane: plane to disable1315* @pipe: pipe consuming the data1316*1317* Disable @plane; should be an independent operation.1318*/1319static void intel_disable_plane(struct drm_i915_private *dev_priv,1320enum plane plane, enum pipe pipe)1321{1322int reg;1323u32 val;13241325reg = DSPCNTR(plane);1326val = I915_READ(reg);1327if ((val & DISPLAY_PLANE_ENABLE) == 0)1328return;13291330I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);1331intel_flush_display_plane(dev_priv, plane);1332intel_wait_for_vblank(dev_priv->dev, pipe);1333}13341335static void disable_pch_dp(struct drm_i915_private *dev_priv,1336enum pipe pipe, int reg)1337{1338u32 val = I915_READ(reg);1339if (DP_PIPE_ENABLED(val, pipe))1340I915_WRITE(reg, val & ~DP_PORT_EN);1341}13421343static void disable_pch_hdmi(struct drm_i915_private *dev_priv,1344enum pipe pipe, int reg)1345{1346u32 val = I915_READ(reg);1347if (HDMI_PIPE_ENABLED(val, pipe))1348I915_WRITE(reg, val & ~PORT_ENABLE);1349}13501351/* Disable any ports connected to this transcoder */1352static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,1353enum pipe pipe)1354{1355u32 reg, val;13561357val = I915_READ(PCH_PP_CONTROL);1358I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);13591360disable_pch_dp(dev_priv, pipe, PCH_DP_B);1361disable_pch_dp(dev_priv, pipe, PCH_DP_C);1362disable_pch_dp(dev_priv, pipe, PCH_DP_D);13631364reg = PCH_ADPA;1365val = I915_READ(reg);1366if (ADPA_PIPE_ENABLED(val, pipe))1367I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);13681369reg = PCH_LVDS;1370val = I915_READ(reg);1371if (LVDS_PIPE_ENABLED(val, pipe)) {1372I915_WRITE(reg, val & ~LVDS_PORT_EN);1373POSTING_READ(reg);1374udelay(100);1375}13761377disable_pch_hdmi(dev_priv, pipe, HDMIB);1378disable_pch_hdmi(dev_priv, pipe, HDMIC);1379disable_pch_hdmi(dev_priv, pipe, HDMID);1380}13811382static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)1383{1384struct drm_device *dev = crtc->dev;1385struct drm_i915_private *dev_priv = dev->dev_private;1386struct drm_framebuffer *fb = crtc->fb;1387struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);1388struct drm_i915_gem_object *obj = intel_fb->obj;1389struct intel_crtc *intel_crtc = to_intel_crtc(crtc);1390int plane, i;1391u32 fbc_ctl, fbc_ctl2;13921393if (fb->pitch == dev_priv->cfb_pitch &&1394obj->fence_reg == dev_priv->cfb_fence &&1395intel_crtc->plane == dev_priv->cfb_plane &&1396I915_READ(FBC_CONTROL) & FBC_CTL_EN)1397return;13981399i8xx_disable_fbc(dev);14001401dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;14021403if (fb->pitch < dev_priv->cfb_pitch)1404dev_priv->cfb_pitch = fb->pitch;14051406/* FBC_CTL wants 64B units */1407dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;1408dev_priv->cfb_fence = obj->fence_reg;1409dev_priv->cfb_plane = intel_crtc->plane;1410plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;14111412/* Clear old tags */1413for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)1414I915_WRITE(FBC_TAG + (i * 4), 0);14151416/* Set it up... */1417fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;1418if (obj->tiling_mode != I915_TILING_NONE)1419fbc_ctl2 |= FBC_CTL_CPU_FENCE;1420I915_WRITE(FBC_CONTROL2, fbc_ctl2);1421I915_WRITE(FBC_FENCE_OFF, crtc->y);14221423/* enable it... */1424fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;1425if (IS_I945GM(dev))1426fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */1427fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;1428fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;1429if (obj->tiling_mode != I915_TILING_NONE)1430fbc_ctl |= dev_priv->cfb_fence;1431I915_WRITE(FBC_CONTROL, fbc_ctl);14321433DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",1434dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);1435}14361437void i8xx_disable_fbc(struct drm_device *dev)1438{1439struct drm_i915_private *dev_priv = dev->dev_private;1440u32 fbc_ctl;14411442/* Disable compression */1443fbc_ctl = I915_READ(FBC_CONTROL);1444if ((fbc_ctl & FBC_CTL_EN) == 0)1445return;14461447fbc_ctl &= ~FBC_CTL_EN;1448I915_WRITE(FBC_CONTROL, fbc_ctl);14491450/* Wait for compressing bit to clear */1451if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {1452DRM_DEBUG_KMS("FBC idle timed out\n");1453return;1454}14551456DRM_DEBUG_KMS("disabled FBC\n");1457}14581459static bool i8xx_fbc_enabled(struct drm_device *dev)1460{1461struct drm_i915_private *dev_priv = dev->dev_private;14621463return I915_READ(FBC_CONTROL) & FBC_CTL_EN;1464}14651466static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)1467{1468struct drm_device *dev = crtc->dev;1469struct drm_i915_private *dev_priv = dev->dev_private;1470struct drm_framebuffer *fb = crtc->fb;1471struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);1472struct drm_i915_gem_object *obj = intel_fb->obj;1473struct intel_crtc *intel_crtc = to_intel_crtc(crtc);1474int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;1475unsigned long stall_watermark = 200;1476u32 dpfc_ctl;14771478dpfc_ctl = I915_READ(DPFC_CONTROL);1479if (dpfc_ctl & DPFC_CTL_EN) {1480if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&1481dev_priv->cfb_fence == obj->fence_reg &&1482dev_priv->cfb_plane == intel_crtc->plane &&1483dev_priv->cfb_y == crtc->y)1484return;14851486I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);1487intel_wait_for_vblank(dev, intel_crtc->pipe);1488}14891490dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;1491dev_priv->cfb_fence = obj->fence_reg;1492dev_priv->cfb_plane = intel_crtc->plane;1493dev_priv->cfb_y = crtc->y;14941495dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;1496if (obj->tiling_mode != I915_TILING_NONE) {1497dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;1498I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);1499} else {1500I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);1501}15021503I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |1504(stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |1505(interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));1506I915_WRITE(DPFC_FENCE_YOFF, crtc->y);15071508/* enable it... */1509I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);15101511DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);1512}15131514void g4x_disable_fbc(struct drm_device *dev)1515{1516struct drm_i915_private *dev_priv = dev->dev_private;1517u32 dpfc_ctl;15181519/* Disable compression */1520dpfc_ctl = I915_READ(DPFC_CONTROL);1521if (dpfc_ctl & DPFC_CTL_EN) {1522dpfc_ctl &= ~DPFC_CTL_EN;1523I915_WRITE(DPFC_CONTROL, dpfc_ctl);15241525DRM_DEBUG_KMS("disabled FBC\n");1526}1527}15281529static bool g4x_fbc_enabled(struct drm_device *dev)1530{1531struct drm_i915_private *dev_priv = dev->dev_private;15321533return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;1534}15351536static void sandybridge_blit_fbc_update(struct drm_device *dev)1537{1538struct drm_i915_private *dev_priv = dev->dev_private;1539u32 blt_ecoskpd;15401541/* Make sure blitter notifies FBC of writes */1542gen6_gt_force_wake_get(dev_priv);1543blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);1544blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<1545GEN6_BLITTER_LOCK_SHIFT;1546I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);1547blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;1548I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);1549blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<1550GEN6_BLITTER_LOCK_SHIFT);1551I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);1552POSTING_READ(GEN6_BLITTER_ECOSKPD);1553gen6_gt_force_wake_put(dev_priv);1554}15551556static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)1557{1558struct drm_device *dev = crtc->dev;1559struct drm_i915_private *dev_priv = dev->dev_private;1560struct drm_framebuffer *fb = crtc->fb;1561struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);1562struct drm_i915_gem_object *obj = intel_fb->obj;1563struct intel_crtc *intel_crtc = to_intel_crtc(crtc);1564int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;1565unsigned long stall_watermark = 200;1566u32 dpfc_ctl;15671568dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);1569if (dpfc_ctl & DPFC_CTL_EN) {1570if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&1571dev_priv->cfb_fence == obj->fence_reg &&1572dev_priv->cfb_plane == intel_crtc->plane &&1573dev_priv->cfb_offset == obj->gtt_offset &&1574dev_priv->cfb_y == crtc->y)1575return;15761577I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);1578intel_wait_for_vblank(dev, intel_crtc->pipe);1579}15801581dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;1582dev_priv->cfb_fence = obj->fence_reg;1583dev_priv->cfb_plane = intel_crtc->plane;1584dev_priv->cfb_offset = obj->gtt_offset;1585dev_priv->cfb_y = crtc->y;15861587dpfc_ctl &= DPFC_RESERVED;1588dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);1589if (obj->tiling_mode != I915_TILING_NONE) {1590dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);1591I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);1592} else {1593I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);1594}15951596I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |1597(stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |1598(interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));1599I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);1600I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);1601/* enable it... */1602I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);16031604if (IS_GEN6(dev)) {1605I915_WRITE(SNB_DPFC_CTL_SA,1606SNB_CPU_FENCE_ENABLE | dev_priv->cfb_fence);1607I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);1608sandybridge_blit_fbc_update(dev);1609}16101611DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);1612}16131614void ironlake_disable_fbc(struct drm_device *dev)1615{1616struct drm_i915_private *dev_priv = dev->dev_private;1617u32 dpfc_ctl;16181619/* Disable compression */1620dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);1621if (dpfc_ctl & DPFC_CTL_EN) {1622dpfc_ctl &= ~DPFC_CTL_EN;1623I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);16241625DRM_DEBUG_KMS("disabled FBC\n");1626}1627}16281629static bool ironlake_fbc_enabled(struct drm_device *dev)1630{1631struct drm_i915_private *dev_priv = dev->dev_private;16321633return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;1634}16351636bool intel_fbc_enabled(struct drm_device *dev)1637{1638struct drm_i915_private *dev_priv = dev->dev_private;16391640if (!dev_priv->display.fbc_enabled)1641return false;16421643return dev_priv->display.fbc_enabled(dev);1644}16451646void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)1647{1648struct drm_i915_private *dev_priv = crtc->dev->dev_private;16491650if (!dev_priv->display.enable_fbc)1651return;16521653dev_priv->display.enable_fbc(crtc, interval);1654}16551656void intel_disable_fbc(struct drm_device *dev)1657{1658struct drm_i915_private *dev_priv = dev->dev_private;16591660if (!dev_priv->display.disable_fbc)1661return;16621663dev_priv->display.disable_fbc(dev);1664}16651666/**1667* intel_update_fbc - enable/disable FBC as needed1668* @dev: the drm_device1669*1670* Set up the framebuffer compression hardware at mode set time. We1671* enable it if possible:1672* - plane A only (on pre-965)1673* - no pixel mulitply/line duplication1674* - no alpha buffer discard1675* - no dual wide1676* - framebuffer <= 2048 in width, 1536 in height1677*1678* We can't assume that any compression will take place (worst case),1679* so the compressed buffer has to be the same size as the uncompressed1680* one. It also must reside (along with the line length buffer) in1681* stolen memory.1682*1683* We need to enable/disable FBC on a global basis.1684*/1685static void intel_update_fbc(struct drm_device *dev)1686{1687struct drm_i915_private *dev_priv = dev->dev_private;1688struct drm_crtc *crtc = NULL, *tmp_crtc;1689struct intel_crtc *intel_crtc;1690struct drm_framebuffer *fb;1691struct intel_framebuffer *intel_fb;1692struct drm_i915_gem_object *obj;16931694DRM_DEBUG_KMS("\n");16951696if (!i915_powersave)1697return;16981699if (!I915_HAS_FBC(dev))1700return;17011702/*1703* If FBC is already on, we just have to verify that we can1704* keep it that way...1705* Need to disable if:1706* - more than one pipe is active1707* - changing FBC params (stride, fence, mode)1708* - new fb is too large to fit in compressed buffer1709* - going to an unsupported config (interlace, pixel multiply, etc.)1710*/1711list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {1712if (tmp_crtc->enabled && tmp_crtc->fb) {1713if (crtc) {1714DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");1715dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;1716goto out_disable;1717}1718crtc = tmp_crtc;1719}1720}17211722if (!crtc || crtc->fb == NULL) {1723DRM_DEBUG_KMS("no output, disabling\n");1724dev_priv->no_fbc_reason = FBC_NO_OUTPUT;1725goto out_disable;1726}17271728intel_crtc = to_intel_crtc(crtc);1729fb = crtc->fb;1730intel_fb = to_intel_framebuffer(fb);1731obj = intel_fb->obj;17321733if (!i915_enable_fbc) {1734DRM_DEBUG_KMS("fbc disabled per module param (default off)\n");1735dev_priv->no_fbc_reason = FBC_MODULE_PARAM;1736goto out_disable;1737}1738if (intel_fb->obj->base.size > dev_priv->cfb_size) {1739DRM_DEBUG_KMS("framebuffer too large, disabling "1740"compression\n");1741dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;1742goto out_disable;1743}1744if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||1745(crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {1746DRM_DEBUG_KMS("mode incompatible with compression, "1747"disabling\n");1748dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;1749goto out_disable;1750}1751if ((crtc->mode.hdisplay > 2048) ||1752(crtc->mode.vdisplay > 1536)) {1753DRM_DEBUG_KMS("mode too large for compression, disabling\n");1754dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;1755goto out_disable;1756}1757if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {1758DRM_DEBUG_KMS("plane not 0, disabling compression\n");1759dev_priv->no_fbc_reason = FBC_BAD_PLANE;1760goto out_disable;1761}1762if (obj->tiling_mode != I915_TILING_X) {1763DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");1764dev_priv->no_fbc_reason = FBC_NOT_TILED;1765goto out_disable;1766}17671768/* If the kernel debugger is active, always disable compression */1769if (in_dbg_master())1770goto out_disable;17711772intel_enable_fbc(crtc, 500);1773return;17741775out_disable:1776/* Multiple disables should be harmless */1777if (intel_fbc_enabled(dev)) {1778DRM_DEBUG_KMS("unsupported config, disabling FBC\n");1779intel_disable_fbc(dev);1780}1781}17821783int1784intel_pin_and_fence_fb_obj(struct drm_device *dev,1785struct drm_i915_gem_object *obj,1786struct intel_ring_buffer *pipelined)1787{1788struct drm_i915_private *dev_priv = dev->dev_private;1789u32 alignment;1790int ret;17911792switch (obj->tiling_mode) {1793case I915_TILING_NONE:1794if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))1795alignment = 128 * 1024;1796else if (INTEL_INFO(dev)->gen >= 4)1797alignment = 4 * 1024;1798else1799alignment = 64 * 1024;1800break;1801case I915_TILING_X:1802/* pin() will align the object as required by fence */1803alignment = 0;1804break;1805case I915_TILING_Y:1806/* FIXME: Is this true? */1807DRM_ERROR("Y tiled not allowed for scan out buffers\n");1808return -EINVAL;1809default:1810BUG();1811}18121813dev_priv->mm.interruptible = false;1814ret = i915_gem_object_pin(obj, alignment, true);1815if (ret)1816goto err_interruptible;18171818ret = i915_gem_object_set_to_display_plane(obj, pipelined);1819if (ret)1820goto err_unpin;18211822/* Install a fence for tiled scan-out. Pre-i965 always needs a1823* fence, whereas 965+ only requires a fence if using1824* framebuffer compression. For simplicity, we always install1825* a fence as the cost is not that onerous.1826*/1827if (obj->tiling_mode != I915_TILING_NONE) {1828ret = i915_gem_object_get_fence(obj, pipelined);1829if (ret)1830goto err_unpin;1831}18321833dev_priv->mm.interruptible = true;1834return 0;18351836err_unpin:1837i915_gem_object_unpin(obj);1838err_interruptible:1839dev_priv->mm.interruptible = true;1840return ret;1841}18421843/* Assume fb object is pinned & idle & fenced and just update base pointers */1844static int1845intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,1846int x, int y, enum mode_set_atomic state)1847{1848struct drm_device *dev = crtc->dev;1849struct drm_i915_private *dev_priv = dev->dev_private;1850struct intel_crtc *intel_crtc = to_intel_crtc(crtc);1851struct intel_framebuffer *intel_fb;1852struct drm_i915_gem_object *obj;1853int plane = intel_crtc->plane;1854unsigned long Start, Offset;1855u32 dspcntr;1856u32 reg;18571858switch (plane) {1859case 0:1860case 1:1861break;1862default:1863DRM_ERROR("Can't update plane %d in SAREA\n", plane);1864return -EINVAL;1865}18661867intel_fb = to_intel_framebuffer(fb);1868obj = intel_fb->obj;18691870reg = DSPCNTR(plane);1871dspcntr = I915_READ(reg);1872/* Mask out pixel format bits in case we change it */1873dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;1874switch (fb->bits_per_pixel) {1875case 8:1876dspcntr |= DISPPLANE_8BPP;1877break;1878case 16:1879if (fb->depth == 15)1880dspcntr |= DISPPLANE_15_16BPP;1881else1882dspcntr |= DISPPLANE_16BPP;1883break;1884case 24:1885case 32:1886dspcntr |= DISPPLANE_32BPP_NO_ALPHA;1887break;1888default:1889DRM_ERROR("Unknown color depth\n");1890return -EINVAL;1891}1892if (INTEL_INFO(dev)->gen >= 4) {1893if (obj->tiling_mode != I915_TILING_NONE)1894dspcntr |= DISPPLANE_TILED;1895else1896dspcntr &= ~DISPPLANE_TILED;1897}18981899if (HAS_PCH_SPLIT(dev))1900/* must disable */1901dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;19021903I915_WRITE(reg, dspcntr);19041905Start = obj->gtt_offset;1906Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);19071908DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",1909Start, Offset, x, y, fb->pitch);1910I915_WRITE(DSPSTRIDE(plane), fb->pitch);1911if (INTEL_INFO(dev)->gen >= 4) {1912I915_WRITE(DSPSURF(plane), Start);1913I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);1914I915_WRITE(DSPADDR(plane), Offset);1915} else1916I915_WRITE(DSPADDR(plane), Start + Offset);1917POSTING_READ(reg);19181919intel_update_fbc(dev);1920intel_increase_pllclock(crtc);19211922return 0;1923}19241925static int1926intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,1927struct drm_framebuffer *old_fb)1928{1929struct drm_device *dev = crtc->dev;1930struct drm_i915_master_private *master_priv;1931struct intel_crtc *intel_crtc = to_intel_crtc(crtc);1932int ret;19331934/* no fb bound */1935if (!crtc->fb) {1936DRM_DEBUG_KMS("No FB bound\n");1937return 0;1938}19391940switch (intel_crtc->plane) {1941case 0:1942case 1:1943break;1944default:1945return -EINVAL;1946}19471948mutex_lock(&dev->struct_mutex);1949ret = intel_pin_and_fence_fb_obj(dev,1950to_intel_framebuffer(crtc->fb)->obj,1951NULL);1952if (ret != 0) {1953mutex_unlock(&dev->struct_mutex);1954return ret;1955}19561957if (old_fb) {1958struct drm_i915_private *dev_priv = dev->dev_private;1959struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;19601961wait_event(dev_priv->pending_flip_queue,1962atomic_read(&dev_priv->mm.wedged) ||1963atomic_read(&obj->pending_flip) == 0);19641965/* Big Hammer, we also need to ensure that any pending1966* MI_WAIT_FOR_EVENT inside a user batch buffer on the1967* current scanout is retired before unpinning the old1968* framebuffer.1969*1970* This should only fail upon a hung GPU, in which case we1971* can safely continue.1972*/1973ret = i915_gem_object_flush_gpu(obj);1974(void) ret;1975}19761977ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,1978LEAVE_ATOMIC_MODE_SET);1979if (ret) {1980i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);1981mutex_unlock(&dev->struct_mutex);1982return ret;1983}19841985if (old_fb) {1986intel_wait_for_vblank(dev, intel_crtc->pipe);1987i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);1988}19891990mutex_unlock(&dev->struct_mutex);19911992if (!dev->primary->master)1993return 0;19941995master_priv = dev->primary->master->driver_priv;1996if (!master_priv->sarea_priv)1997return 0;19981999if (intel_crtc->pipe) {2000master_priv->sarea_priv->pipeB_x = x;2001master_priv->sarea_priv->pipeB_y = y;2002} else {2003master_priv->sarea_priv->pipeA_x = x;2004master_priv->sarea_priv->pipeA_y = y;2005}20062007return 0;2008}20092010static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)2011{2012struct drm_device *dev = crtc->dev;2013struct drm_i915_private *dev_priv = dev->dev_private;2014u32 dpa_ctl;20152016DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);2017dpa_ctl = I915_READ(DP_A);2018dpa_ctl &= ~DP_PLL_FREQ_MASK;20192020if (clock < 200000) {2021u32 temp;2022dpa_ctl |= DP_PLL_FREQ_160MHZ;2023/* workaround for 160Mhz:20241) program 0x4600c bits 15:0 = 0x812420252) program 0x46010 bit 0 = 120263) program 0x46034 bit 24 = 120274) program 0x64000 bit 14 = 12028*/2029temp = I915_READ(0x4600c);2030temp &= 0xffff0000;2031I915_WRITE(0x4600c, temp | 0x8124);20322033temp = I915_READ(0x46010);2034I915_WRITE(0x46010, temp | 1);20352036temp = I915_READ(0x46034);2037I915_WRITE(0x46034, temp | (1 << 24));2038} else {2039dpa_ctl |= DP_PLL_FREQ_270MHZ;2040}2041I915_WRITE(DP_A, dpa_ctl);20422043POSTING_READ(DP_A);2044udelay(500);2045}20462047static void intel_fdi_normal_train(struct drm_crtc *crtc)2048{2049struct drm_device *dev = crtc->dev;2050struct drm_i915_private *dev_priv = dev->dev_private;2051struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2052int pipe = intel_crtc->pipe;2053u32 reg, temp;20542055/* enable normal train */2056reg = FDI_TX_CTL(pipe);2057temp = I915_READ(reg);2058if (IS_IVYBRIDGE(dev)) {2059temp &= ~FDI_LINK_TRAIN_NONE_IVB;2060temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;2061} else {2062temp &= ~FDI_LINK_TRAIN_NONE;2063temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;2064}2065I915_WRITE(reg, temp);20662067reg = FDI_RX_CTL(pipe);2068temp = I915_READ(reg);2069if (HAS_PCH_CPT(dev)) {2070temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;2071temp |= FDI_LINK_TRAIN_NORMAL_CPT;2072} else {2073temp &= ~FDI_LINK_TRAIN_NONE;2074temp |= FDI_LINK_TRAIN_NONE;2075}2076I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);20772078/* wait one idle pattern time */2079POSTING_READ(reg);2080udelay(1000);20812082/* IVB wants error correction enabled */2083if (IS_IVYBRIDGE(dev))2084I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |2085FDI_FE_ERRC_ENABLE);2086}20872088/* The FDI link training functions for ILK/Ibexpeak. */2089static void ironlake_fdi_link_train(struct drm_crtc *crtc)2090{2091struct drm_device *dev = crtc->dev;2092struct drm_i915_private *dev_priv = dev->dev_private;2093struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2094int pipe = intel_crtc->pipe;2095int plane = intel_crtc->plane;2096u32 reg, temp, tries;20972098/* FDI needs bits from pipe & plane first */2099assert_pipe_enabled(dev_priv, pipe);2100assert_plane_enabled(dev_priv, plane);21012102/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit2103for train result */2104reg = FDI_RX_IMR(pipe);2105temp = I915_READ(reg);2106temp &= ~FDI_RX_SYMBOL_LOCK;2107temp &= ~FDI_RX_BIT_LOCK;2108I915_WRITE(reg, temp);2109I915_READ(reg);2110udelay(150);21112112/* enable CPU FDI TX and PCH FDI RX */2113reg = FDI_TX_CTL(pipe);2114temp = I915_READ(reg);2115temp &= ~(7 << 19);2116temp |= (intel_crtc->fdi_lanes - 1) << 19;2117temp &= ~FDI_LINK_TRAIN_NONE;2118temp |= FDI_LINK_TRAIN_PATTERN_1;2119I915_WRITE(reg, temp | FDI_TX_ENABLE);21202121reg = FDI_RX_CTL(pipe);2122temp = I915_READ(reg);2123temp &= ~FDI_LINK_TRAIN_NONE;2124temp |= FDI_LINK_TRAIN_PATTERN_1;2125I915_WRITE(reg, temp | FDI_RX_ENABLE);21262127POSTING_READ(reg);2128udelay(150);21292130/* Ironlake workaround, enable clock pointer after FDI enable*/2131if (HAS_PCH_IBX(dev)) {2132I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);2133I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |2134FDI_RX_PHASE_SYNC_POINTER_EN);2135}21362137reg = FDI_RX_IIR(pipe);2138for (tries = 0; tries < 5; tries++) {2139temp = I915_READ(reg);2140DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);21412142if ((temp & FDI_RX_BIT_LOCK)) {2143DRM_DEBUG_KMS("FDI train 1 done.\n");2144I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);2145break;2146}2147}2148if (tries == 5)2149DRM_ERROR("FDI train 1 fail!\n");21502151/* Train 2 */2152reg = FDI_TX_CTL(pipe);2153temp = I915_READ(reg);2154temp &= ~FDI_LINK_TRAIN_NONE;2155temp |= FDI_LINK_TRAIN_PATTERN_2;2156I915_WRITE(reg, temp);21572158reg = FDI_RX_CTL(pipe);2159temp = I915_READ(reg);2160temp &= ~FDI_LINK_TRAIN_NONE;2161temp |= FDI_LINK_TRAIN_PATTERN_2;2162I915_WRITE(reg, temp);21632164POSTING_READ(reg);2165udelay(150);21662167reg = FDI_RX_IIR(pipe);2168for (tries = 0; tries < 5; tries++) {2169temp = I915_READ(reg);2170DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);21712172if (temp & FDI_RX_SYMBOL_LOCK) {2173I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);2174DRM_DEBUG_KMS("FDI train 2 done.\n");2175break;2176}2177}2178if (tries == 5)2179DRM_ERROR("FDI train 2 fail!\n");21802181DRM_DEBUG_KMS("FDI train done\n");21822183}21842185static const int snb_b_fdi_train_param [] = {2186FDI_LINK_TRAIN_400MV_0DB_SNB_B,2187FDI_LINK_TRAIN_400MV_6DB_SNB_B,2188FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,2189FDI_LINK_TRAIN_800MV_0DB_SNB_B,2190};21912192/* The FDI link training functions for SNB/Cougarpoint. */2193static void gen6_fdi_link_train(struct drm_crtc *crtc)2194{2195struct drm_device *dev = crtc->dev;2196struct drm_i915_private *dev_priv = dev->dev_private;2197struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2198int pipe = intel_crtc->pipe;2199u32 reg, temp, i;22002201/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit2202for train result */2203reg = FDI_RX_IMR(pipe);2204temp = I915_READ(reg);2205temp &= ~FDI_RX_SYMBOL_LOCK;2206temp &= ~FDI_RX_BIT_LOCK;2207I915_WRITE(reg, temp);22082209POSTING_READ(reg);2210udelay(150);22112212/* enable CPU FDI TX and PCH FDI RX */2213reg = FDI_TX_CTL(pipe);2214temp = I915_READ(reg);2215temp &= ~(7 << 19);2216temp |= (intel_crtc->fdi_lanes - 1) << 19;2217temp &= ~FDI_LINK_TRAIN_NONE;2218temp |= FDI_LINK_TRAIN_PATTERN_1;2219temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;2220/* SNB-B */2221temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;2222I915_WRITE(reg, temp | FDI_TX_ENABLE);22232224reg = FDI_RX_CTL(pipe);2225temp = I915_READ(reg);2226if (HAS_PCH_CPT(dev)) {2227temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;2228temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;2229} else {2230temp &= ~FDI_LINK_TRAIN_NONE;2231temp |= FDI_LINK_TRAIN_PATTERN_1;2232}2233I915_WRITE(reg, temp | FDI_RX_ENABLE);22342235POSTING_READ(reg);2236udelay(150);22372238for (i = 0; i < 4; i++ ) {2239reg = FDI_TX_CTL(pipe);2240temp = I915_READ(reg);2241temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;2242temp |= snb_b_fdi_train_param[i];2243I915_WRITE(reg, temp);22442245POSTING_READ(reg);2246udelay(500);22472248reg = FDI_RX_IIR(pipe);2249temp = I915_READ(reg);2250DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);22512252if (temp & FDI_RX_BIT_LOCK) {2253I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);2254DRM_DEBUG_KMS("FDI train 1 done.\n");2255break;2256}2257}2258if (i == 4)2259DRM_ERROR("FDI train 1 fail!\n");22602261/* Train 2 */2262reg = FDI_TX_CTL(pipe);2263temp = I915_READ(reg);2264temp &= ~FDI_LINK_TRAIN_NONE;2265temp |= FDI_LINK_TRAIN_PATTERN_2;2266if (IS_GEN6(dev)) {2267temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;2268/* SNB-B */2269temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;2270}2271I915_WRITE(reg, temp);22722273reg = FDI_RX_CTL(pipe);2274temp = I915_READ(reg);2275if (HAS_PCH_CPT(dev)) {2276temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;2277temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;2278} else {2279temp &= ~FDI_LINK_TRAIN_NONE;2280temp |= FDI_LINK_TRAIN_PATTERN_2;2281}2282I915_WRITE(reg, temp);22832284POSTING_READ(reg);2285udelay(150);22862287for (i = 0; i < 4; i++ ) {2288reg = FDI_TX_CTL(pipe);2289temp = I915_READ(reg);2290temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;2291temp |= snb_b_fdi_train_param[i];2292I915_WRITE(reg, temp);22932294POSTING_READ(reg);2295udelay(500);22962297reg = FDI_RX_IIR(pipe);2298temp = I915_READ(reg);2299DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);23002301if (temp & FDI_RX_SYMBOL_LOCK) {2302I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);2303DRM_DEBUG_KMS("FDI train 2 done.\n");2304break;2305}2306}2307if (i == 4)2308DRM_ERROR("FDI train 2 fail!\n");23092310DRM_DEBUG_KMS("FDI train done.\n");2311}23122313/* Manual link training for Ivy Bridge A0 parts */2314static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)2315{2316struct drm_device *dev = crtc->dev;2317struct drm_i915_private *dev_priv = dev->dev_private;2318struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2319int pipe = intel_crtc->pipe;2320u32 reg, temp, i;23212322/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit2323for train result */2324reg = FDI_RX_IMR(pipe);2325temp = I915_READ(reg);2326temp &= ~FDI_RX_SYMBOL_LOCK;2327temp &= ~FDI_RX_BIT_LOCK;2328I915_WRITE(reg, temp);23292330POSTING_READ(reg);2331udelay(150);23322333/* enable CPU FDI TX and PCH FDI RX */2334reg = FDI_TX_CTL(pipe);2335temp = I915_READ(reg);2336temp &= ~(7 << 19);2337temp |= (intel_crtc->fdi_lanes - 1) << 19;2338temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);2339temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;2340temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;2341temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;2342I915_WRITE(reg, temp | FDI_TX_ENABLE);23432344reg = FDI_RX_CTL(pipe);2345temp = I915_READ(reg);2346temp &= ~FDI_LINK_TRAIN_AUTO;2347temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;2348temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;2349I915_WRITE(reg, temp | FDI_RX_ENABLE);23502351POSTING_READ(reg);2352udelay(150);23532354for (i = 0; i < 4; i++ ) {2355reg = FDI_TX_CTL(pipe);2356temp = I915_READ(reg);2357temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;2358temp |= snb_b_fdi_train_param[i];2359I915_WRITE(reg, temp);23602361POSTING_READ(reg);2362udelay(500);23632364reg = FDI_RX_IIR(pipe);2365temp = I915_READ(reg);2366DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);23672368if (temp & FDI_RX_BIT_LOCK ||2369(I915_READ(reg) & FDI_RX_BIT_LOCK)) {2370I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);2371DRM_DEBUG_KMS("FDI train 1 done.\n");2372break;2373}2374}2375if (i == 4)2376DRM_ERROR("FDI train 1 fail!\n");23772378/* Train 2 */2379reg = FDI_TX_CTL(pipe);2380temp = I915_READ(reg);2381temp &= ~FDI_LINK_TRAIN_NONE_IVB;2382temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;2383temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;2384temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;2385I915_WRITE(reg, temp);23862387reg = FDI_RX_CTL(pipe);2388temp = I915_READ(reg);2389temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;2390temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;2391I915_WRITE(reg, temp);23922393POSTING_READ(reg);2394udelay(150);23952396for (i = 0; i < 4; i++ ) {2397reg = FDI_TX_CTL(pipe);2398temp = I915_READ(reg);2399temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;2400temp |= snb_b_fdi_train_param[i];2401I915_WRITE(reg, temp);24022403POSTING_READ(reg);2404udelay(500);24052406reg = FDI_RX_IIR(pipe);2407temp = I915_READ(reg);2408DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);24092410if (temp & FDI_RX_SYMBOL_LOCK) {2411I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);2412DRM_DEBUG_KMS("FDI train 2 done.\n");2413break;2414}2415}2416if (i == 4)2417DRM_ERROR("FDI train 2 fail!\n");24182419DRM_DEBUG_KMS("FDI train done.\n");2420}24212422static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)2423{2424struct drm_device *dev = crtc->dev;2425struct drm_i915_private *dev_priv = dev->dev_private;2426struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2427int pipe = intel_crtc->pipe;2428u32 reg, temp;24292430/* Write the TU size bits so error detection works */2431I915_WRITE(FDI_RX_TUSIZE1(pipe),2432I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);24332434/* enable PCH FDI RX PLL, wait warmup plus DMI latency */2435reg = FDI_RX_CTL(pipe);2436temp = I915_READ(reg);2437temp &= ~((0x7 << 19) | (0x7 << 16));2438temp |= (intel_crtc->fdi_lanes - 1) << 19;2439temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;2440I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);24412442POSTING_READ(reg);2443udelay(200);24442445/* Switch from Rawclk to PCDclk */2446temp = I915_READ(reg);2447I915_WRITE(reg, temp | FDI_PCDCLK);24482449POSTING_READ(reg);2450udelay(200);24512452/* Enable CPU FDI TX PLL, always on for Ironlake */2453reg = FDI_TX_CTL(pipe);2454temp = I915_READ(reg);2455if ((temp & FDI_TX_PLL_ENABLE) == 0) {2456I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);24572458POSTING_READ(reg);2459udelay(100);2460}2461}24622463static void ironlake_fdi_disable(struct drm_crtc *crtc)2464{2465struct drm_device *dev = crtc->dev;2466struct drm_i915_private *dev_priv = dev->dev_private;2467struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2468int pipe = intel_crtc->pipe;2469u32 reg, temp;24702471/* disable CPU FDI tx and PCH FDI rx */2472reg = FDI_TX_CTL(pipe);2473temp = I915_READ(reg);2474I915_WRITE(reg, temp & ~FDI_TX_ENABLE);2475POSTING_READ(reg);24762477reg = FDI_RX_CTL(pipe);2478temp = I915_READ(reg);2479temp &= ~(0x7 << 16);2480temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;2481I915_WRITE(reg, temp & ~FDI_RX_ENABLE);24822483POSTING_READ(reg);2484udelay(100);24852486/* Ironlake workaround, disable clock pointer after downing FDI */2487if (HAS_PCH_IBX(dev)) {2488I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);2489I915_WRITE(FDI_RX_CHICKEN(pipe),2490I915_READ(FDI_RX_CHICKEN(pipe) &2491~FDI_RX_PHASE_SYNC_POINTER_EN));2492}24932494/* still set train pattern 1 */2495reg = FDI_TX_CTL(pipe);2496temp = I915_READ(reg);2497temp &= ~FDI_LINK_TRAIN_NONE;2498temp |= FDI_LINK_TRAIN_PATTERN_1;2499I915_WRITE(reg, temp);25002501reg = FDI_RX_CTL(pipe);2502temp = I915_READ(reg);2503if (HAS_PCH_CPT(dev)) {2504temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;2505temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;2506} else {2507temp &= ~FDI_LINK_TRAIN_NONE;2508temp |= FDI_LINK_TRAIN_PATTERN_1;2509}2510/* BPC in FDI rx is consistent with that in PIPECONF */2511temp &= ~(0x07 << 16);2512temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;2513I915_WRITE(reg, temp);25142515POSTING_READ(reg);2516udelay(100);2517}25182519/*2520* When we disable a pipe, we need to clear any pending scanline wait events2521* to avoid hanging the ring, which we assume we are waiting on.2522*/2523static void intel_clear_scanline_wait(struct drm_device *dev)2524{2525struct drm_i915_private *dev_priv = dev->dev_private;2526struct intel_ring_buffer *ring;2527u32 tmp;25282529if (IS_GEN2(dev))2530/* Can't break the hang on i8xx */2531return;25322533ring = LP_RING(dev_priv);2534tmp = I915_READ_CTL(ring);2535if (tmp & RING_WAIT)2536I915_WRITE_CTL(ring, tmp);2537}25382539static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)2540{2541struct drm_i915_gem_object *obj;2542struct drm_i915_private *dev_priv;25432544if (crtc->fb == NULL)2545return;25462547obj = to_intel_framebuffer(crtc->fb)->obj;2548dev_priv = crtc->dev->dev_private;2549wait_event(dev_priv->pending_flip_queue,2550atomic_read(&obj->pending_flip) == 0);2551}25522553static bool intel_crtc_driving_pch(struct drm_crtc *crtc)2554{2555struct drm_device *dev = crtc->dev;2556struct drm_mode_config *mode_config = &dev->mode_config;2557struct intel_encoder *encoder;25582559/*2560* If there's a non-PCH eDP on this crtc, it must be DP_A, and that2561* must be driven by its own crtc; no sharing is possible.2562*/2563list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {2564if (encoder->base.crtc != crtc)2565continue;25662567switch (encoder->type) {2568case INTEL_OUTPUT_EDP:2569if (!intel_encoder_is_pch_edp(&encoder->base))2570return false;2571continue;2572}2573}25742575return true;2576}25772578/*2579* Enable PCH resources required for PCH ports:2580* - PCH PLLs2581* - FDI training & RX/TX2582* - update transcoder timings2583* - DP transcoding bits2584* - transcoder2585*/2586static void ironlake_pch_enable(struct drm_crtc *crtc)2587{2588struct drm_device *dev = crtc->dev;2589struct drm_i915_private *dev_priv = dev->dev_private;2590struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2591int pipe = intel_crtc->pipe;2592u32 reg, temp;25932594/* For PCH output, training FDI link */2595dev_priv->display.fdi_link_train(crtc);25962597intel_enable_pch_pll(dev_priv, pipe);25982599if (HAS_PCH_CPT(dev)) {2600/* Be sure PCH DPLL SEL is set */2601temp = I915_READ(PCH_DPLL_SEL);2602if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)2603temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);2604else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)2605temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);2606I915_WRITE(PCH_DPLL_SEL, temp);2607}26082609/* set transcoder timing, panel must allow it */2610assert_panel_unlocked(dev_priv, pipe);2611I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));2612I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));2613I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));26142615I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));2616I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));2617I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));26182619intel_fdi_normal_train(crtc);26202621/* For PCH DP, enable TRANS_DP_CTL */2622if (HAS_PCH_CPT(dev) &&2623intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {2624reg = TRANS_DP_CTL(pipe);2625temp = I915_READ(reg);2626temp &= ~(TRANS_DP_PORT_SEL_MASK |2627TRANS_DP_SYNC_MASK |2628TRANS_DP_BPC_MASK);2629temp |= (TRANS_DP_OUTPUT_ENABLE |2630TRANS_DP_ENH_FRAMING);2631temp |= TRANS_DP_8BPC;26322633if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)2634temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;2635if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)2636temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;26372638switch (intel_trans_dp_port_sel(crtc)) {2639case PCH_DP_B:2640temp |= TRANS_DP_PORT_SEL_B;2641break;2642case PCH_DP_C:2643temp |= TRANS_DP_PORT_SEL_C;2644break;2645case PCH_DP_D:2646temp |= TRANS_DP_PORT_SEL_D;2647break;2648default:2649DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");2650temp |= TRANS_DP_PORT_SEL_B;2651break;2652}26532654I915_WRITE(reg, temp);2655}26562657intel_enable_transcoder(dev_priv, pipe);2658}26592660static void ironlake_crtc_enable(struct drm_crtc *crtc)2661{2662struct drm_device *dev = crtc->dev;2663struct drm_i915_private *dev_priv = dev->dev_private;2664struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2665int pipe = intel_crtc->pipe;2666int plane = intel_crtc->plane;2667u32 temp;2668bool is_pch_port;26692670if (intel_crtc->active)2671return;26722673intel_crtc->active = true;2674intel_update_watermarks(dev);26752676if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {2677temp = I915_READ(PCH_LVDS);2678if ((temp & LVDS_PORT_EN) == 0)2679I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);2680}26812682is_pch_port = intel_crtc_driving_pch(crtc);26832684if (is_pch_port)2685ironlake_fdi_pll_enable(crtc);2686else2687ironlake_fdi_disable(crtc);26882689/* Enable panel fitting for LVDS */2690if (dev_priv->pch_pf_size &&2691(intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {2692/* Force use of hard-coded filter coefficients2693* as some pre-programmed values are broken,2694* e.g. x201.2695*/2696I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);2697I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);2698I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);2699}27002701intel_enable_pipe(dev_priv, pipe, is_pch_port);2702intel_enable_plane(dev_priv, plane, pipe);27032704if (is_pch_port)2705ironlake_pch_enable(crtc);27062707intel_crtc_load_lut(crtc);27082709mutex_lock(&dev->struct_mutex);2710intel_update_fbc(dev);2711mutex_unlock(&dev->struct_mutex);27122713intel_crtc_update_cursor(crtc, true);2714}27152716static void ironlake_crtc_disable(struct drm_crtc *crtc)2717{2718struct drm_device *dev = crtc->dev;2719struct drm_i915_private *dev_priv = dev->dev_private;2720struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2721int pipe = intel_crtc->pipe;2722int plane = intel_crtc->plane;2723u32 reg, temp;27242725if (!intel_crtc->active)2726return;27272728intel_crtc_wait_for_pending_flips(crtc);2729drm_vblank_off(dev, pipe);2730intel_crtc_update_cursor(crtc, false);27312732intel_disable_plane(dev_priv, plane, pipe);27332734if (dev_priv->cfb_plane == plane &&2735dev_priv->display.disable_fbc)2736dev_priv->display.disable_fbc(dev);27372738intel_disable_pipe(dev_priv, pipe);27392740/* Disable PF */2741I915_WRITE(PF_CTL(pipe), 0);2742I915_WRITE(PF_WIN_SZ(pipe), 0);27432744ironlake_fdi_disable(crtc);27452746/* This is a horrible layering violation; we should be doing this in2747* the connector/encoder ->prepare instead, but we don't always have2748* enough information there about the config to know whether it will2749* actually be necessary or just cause undesired flicker.2750*/2751intel_disable_pch_ports(dev_priv, pipe);27522753intel_disable_transcoder(dev_priv, pipe);27542755if (HAS_PCH_CPT(dev)) {2756/* disable TRANS_DP_CTL */2757reg = TRANS_DP_CTL(pipe);2758temp = I915_READ(reg);2759temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);2760temp |= TRANS_DP_PORT_SEL_NONE;2761I915_WRITE(reg, temp);27622763/* disable DPLL_SEL */2764temp = I915_READ(PCH_DPLL_SEL);2765switch (pipe) {2766case 0:2767temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);2768break;2769case 1:2770temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);2771break;2772case 2:2773/* FIXME: manage transcoder PLLs? */2774temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);2775break;2776default:2777BUG(); /* wtf */2778}2779I915_WRITE(PCH_DPLL_SEL, temp);2780}27812782/* disable PCH DPLL */2783intel_disable_pch_pll(dev_priv, pipe);27842785/* Switch from PCDclk to Rawclk */2786reg = FDI_RX_CTL(pipe);2787temp = I915_READ(reg);2788I915_WRITE(reg, temp & ~FDI_PCDCLK);27892790/* Disable CPU FDI TX PLL */2791reg = FDI_TX_CTL(pipe);2792temp = I915_READ(reg);2793I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);27942795POSTING_READ(reg);2796udelay(100);27972798reg = FDI_RX_CTL(pipe);2799temp = I915_READ(reg);2800I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);28012802/* Wait for the clocks to turn off. */2803POSTING_READ(reg);2804udelay(100);28052806intel_crtc->active = false;2807intel_update_watermarks(dev);28082809mutex_lock(&dev->struct_mutex);2810intel_update_fbc(dev);2811intel_clear_scanline_wait(dev);2812mutex_unlock(&dev->struct_mutex);2813}28142815static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)2816{2817struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2818int pipe = intel_crtc->pipe;2819int plane = intel_crtc->plane;28202821/* XXX: When our outputs are all unaware of DPMS modes other than off2822* and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.2823*/2824switch (mode) {2825case DRM_MODE_DPMS_ON:2826case DRM_MODE_DPMS_STANDBY:2827case DRM_MODE_DPMS_SUSPEND:2828DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);2829ironlake_crtc_enable(crtc);2830break;28312832case DRM_MODE_DPMS_OFF:2833DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);2834ironlake_crtc_disable(crtc);2835break;2836}2837}28382839static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)2840{2841if (!enable && intel_crtc->overlay) {2842struct drm_device *dev = intel_crtc->base.dev;2843struct drm_i915_private *dev_priv = dev->dev_private;28442845mutex_lock(&dev->struct_mutex);2846dev_priv->mm.interruptible = false;2847(void) intel_overlay_switch_off(intel_crtc->overlay);2848dev_priv->mm.interruptible = true;2849mutex_unlock(&dev->struct_mutex);2850}28512852/* Let userspace switch the overlay on again. In most cases userspace2853* has to recompute where to put it anyway.2854*/2855}28562857static void i9xx_crtc_enable(struct drm_crtc *crtc)2858{2859struct drm_device *dev = crtc->dev;2860struct drm_i915_private *dev_priv = dev->dev_private;2861struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2862int pipe = intel_crtc->pipe;2863int plane = intel_crtc->plane;28642865if (intel_crtc->active)2866return;28672868intel_crtc->active = true;2869intel_update_watermarks(dev);28702871intel_enable_pll(dev_priv, pipe);2872intel_enable_pipe(dev_priv, pipe, false);2873intel_enable_plane(dev_priv, plane, pipe);28742875intel_crtc_load_lut(crtc);2876intel_update_fbc(dev);28772878/* Give the overlay scaler a chance to enable if it's on this pipe */2879intel_crtc_dpms_overlay(intel_crtc, true);2880intel_crtc_update_cursor(crtc, true);2881}28822883static void i9xx_crtc_disable(struct drm_crtc *crtc)2884{2885struct drm_device *dev = crtc->dev;2886struct drm_i915_private *dev_priv = dev->dev_private;2887struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2888int pipe = intel_crtc->pipe;2889int plane = intel_crtc->plane;28902891if (!intel_crtc->active)2892return;28932894/* Give the overlay scaler a chance to disable if it's on this pipe */2895intel_crtc_wait_for_pending_flips(crtc);2896drm_vblank_off(dev, pipe);2897intel_crtc_dpms_overlay(intel_crtc, false);2898intel_crtc_update_cursor(crtc, false);28992900if (dev_priv->cfb_plane == plane &&2901dev_priv->display.disable_fbc)2902dev_priv->display.disable_fbc(dev);29032904intel_disable_plane(dev_priv, plane, pipe);2905intel_disable_pipe(dev_priv, pipe);2906intel_disable_pll(dev_priv, pipe);29072908intel_crtc->active = false;2909intel_update_fbc(dev);2910intel_update_watermarks(dev);2911intel_clear_scanline_wait(dev);2912}29132914static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)2915{2916/* XXX: When our outputs are all unaware of DPMS modes other than off2917* and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.2918*/2919switch (mode) {2920case DRM_MODE_DPMS_ON:2921case DRM_MODE_DPMS_STANDBY:2922case DRM_MODE_DPMS_SUSPEND:2923i9xx_crtc_enable(crtc);2924break;2925case DRM_MODE_DPMS_OFF:2926i9xx_crtc_disable(crtc);2927break;2928}2929}29302931/**2932* Sets the power management mode of the pipe and plane.2933*/2934static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)2935{2936struct drm_device *dev = crtc->dev;2937struct drm_i915_private *dev_priv = dev->dev_private;2938struct drm_i915_master_private *master_priv;2939struct intel_crtc *intel_crtc = to_intel_crtc(crtc);2940int pipe = intel_crtc->pipe;2941bool enabled;29422943if (intel_crtc->dpms_mode == mode)2944return;29452946intel_crtc->dpms_mode = mode;29472948dev_priv->display.dpms(crtc, mode);29492950if (!dev->primary->master)2951return;29522953master_priv = dev->primary->master->driver_priv;2954if (!master_priv->sarea_priv)2955return;29562957enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;29582959switch (pipe) {2960case 0:2961master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;2962master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;2963break;2964case 1:2965master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;2966master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;2967break;2968default:2969DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));2970break;2971}2972}29732974static void intel_crtc_disable(struct drm_crtc *crtc)2975{2976struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;2977struct drm_device *dev = crtc->dev;29782979crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);29802981if (crtc->fb) {2982mutex_lock(&dev->struct_mutex);2983i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);2984mutex_unlock(&dev->struct_mutex);2985}2986}29872988/* Prepare for a mode set.2989*2990* Note we could be a lot smarter here. We need to figure out which outputs2991* will be enabled, which disabled (in short, how the config will changes)2992* and perform the minimum necessary steps to accomplish that, e.g. updating2993* watermarks, FBC configuration, making sure PLLs are programmed correctly,2994* panel fitting is in the proper state, etc.2995*/2996static void i9xx_crtc_prepare(struct drm_crtc *crtc)2997{2998i9xx_crtc_disable(crtc);2999}30003001static void i9xx_crtc_commit(struct drm_crtc *crtc)3002{3003i9xx_crtc_enable(crtc);3004}30053006static void ironlake_crtc_prepare(struct drm_crtc *crtc)3007{3008ironlake_crtc_disable(crtc);3009}30103011static void ironlake_crtc_commit(struct drm_crtc *crtc)3012{3013ironlake_crtc_enable(crtc);3014}30153016void intel_encoder_prepare (struct drm_encoder *encoder)3017{3018struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;3019/* lvds has its own version of prepare see intel_lvds_prepare */3020encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);3021}30223023void intel_encoder_commit (struct drm_encoder *encoder)3024{3025struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;3026/* lvds has its own version of commit see intel_lvds_commit */3027encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);3028}30293030void intel_encoder_destroy(struct drm_encoder *encoder)3031{3032struct intel_encoder *intel_encoder = to_intel_encoder(encoder);30333034drm_encoder_cleanup(encoder);3035kfree(intel_encoder);3036}30373038static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,3039struct drm_display_mode *mode,3040struct drm_display_mode *adjusted_mode)3041{3042struct drm_device *dev = crtc->dev;30433044if (HAS_PCH_SPLIT(dev)) {3045/* FDI link clock is fixed at 2.7G */3046if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)3047return false;3048}30493050/* XXX some encoders set the crtcinfo, others don't.3051* Obviously we need some form of conflict resolution here...3052*/3053if (adjusted_mode->crtc_htotal == 0)3054drm_mode_set_crtcinfo(adjusted_mode, 0);30553056return true;3057}30583059static int i945_get_display_clock_speed(struct drm_device *dev)3060{3061return 400000;3062}30633064static int i915_get_display_clock_speed(struct drm_device *dev)3065{3066return 333000;3067}30683069static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)3070{3071return 200000;3072}30733074static int i915gm_get_display_clock_speed(struct drm_device *dev)3075{3076u16 gcfgc = 0;30773078pci_read_config_word(dev->pdev, GCFGC, &gcfgc);30793080if (gcfgc & GC_LOW_FREQUENCY_ENABLE)3081return 133000;3082else {3083switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {3084case GC_DISPLAY_CLOCK_333_MHZ:3085return 333000;3086default:3087case GC_DISPLAY_CLOCK_190_200_MHZ:3088return 190000;3089}3090}3091}30923093static int i865_get_display_clock_speed(struct drm_device *dev)3094{3095return 266000;3096}30973098static int i855_get_display_clock_speed(struct drm_device *dev)3099{3100u16 hpllcc = 0;3101/* Assume that the hardware is in the high speed state. This3102* should be the default.3103*/3104switch (hpllcc & GC_CLOCK_CONTROL_MASK) {3105case GC_CLOCK_133_200:3106case GC_CLOCK_100_200:3107return 200000;3108case GC_CLOCK_166_250:3109return 250000;3110case GC_CLOCK_100_133:3111return 133000;3112}31133114/* Shouldn't happen */3115return 0;3116}31173118static int i830_get_display_clock_speed(struct drm_device *dev)3119{3120return 133000;3121}31223123struct fdi_m_n {3124u32 tu;3125u32 gmch_m;3126u32 gmch_n;3127u32 link_m;3128u32 link_n;3129};31303131static void3132fdi_reduce_ratio(u32 *num, u32 *den)3133{3134while (*num > 0xffffff || *den > 0xffffff) {3135*num >>= 1;3136*den >>= 1;3137}3138}31393140static void3141ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,3142int link_clock, struct fdi_m_n *m_n)3143{3144m_n->tu = 64; /* default size */31453146/* BUG_ON(pixel_clock > INT_MAX / 36); */3147m_n->gmch_m = bits_per_pixel * pixel_clock;3148m_n->gmch_n = link_clock * nlanes * 8;3149fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);31503151m_n->link_m = pixel_clock;3152m_n->link_n = link_clock;3153fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);3154}315531563157struct intel_watermark_params {3158unsigned long fifo_size;3159unsigned long max_wm;3160unsigned long default_wm;3161unsigned long guard_size;3162unsigned long cacheline_size;3163};31643165/* Pineview has different values for various configs */3166static const struct intel_watermark_params pineview_display_wm = {3167PINEVIEW_DISPLAY_FIFO,3168PINEVIEW_MAX_WM,3169PINEVIEW_DFT_WM,3170PINEVIEW_GUARD_WM,3171PINEVIEW_FIFO_LINE_SIZE3172};3173static const struct intel_watermark_params pineview_display_hplloff_wm = {3174PINEVIEW_DISPLAY_FIFO,3175PINEVIEW_MAX_WM,3176PINEVIEW_DFT_HPLLOFF_WM,3177PINEVIEW_GUARD_WM,3178PINEVIEW_FIFO_LINE_SIZE3179};3180static const struct intel_watermark_params pineview_cursor_wm = {3181PINEVIEW_CURSOR_FIFO,3182PINEVIEW_CURSOR_MAX_WM,3183PINEVIEW_CURSOR_DFT_WM,3184PINEVIEW_CURSOR_GUARD_WM,3185PINEVIEW_FIFO_LINE_SIZE,3186};3187static const struct intel_watermark_params pineview_cursor_hplloff_wm = {3188PINEVIEW_CURSOR_FIFO,3189PINEVIEW_CURSOR_MAX_WM,3190PINEVIEW_CURSOR_DFT_WM,3191PINEVIEW_CURSOR_GUARD_WM,3192PINEVIEW_FIFO_LINE_SIZE3193};3194static const struct intel_watermark_params g4x_wm_info = {3195G4X_FIFO_SIZE,3196G4X_MAX_WM,3197G4X_MAX_WM,31982,3199G4X_FIFO_LINE_SIZE,3200};3201static const struct intel_watermark_params g4x_cursor_wm_info = {3202I965_CURSOR_FIFO,3203I965_CURSOR_MAX_WM,3204I965_CURSOR_DFT_WM,32052,3206G4X_FIFO_LINE_SIZE,3207};3208static const struct intel_watermark_params i965_cursor_wm_info = {3209I965_CURSOR_FIFO,3210I965_CURSOR_MAX_WM,3211I965_CURSOR_DFT_WM,32122,3213I915_FIFO_LINE_SIZE,3214};3215static const struct intel_watermark_params i945_wm_info = {3216I945_FIFO_SIZE,3217I915_MAX_WM,32181,32192,3220I915_FIFO_LINE_SIZE3221};3222static const struct intel_watermark_params i915_wm_info = {3223I915_FIFO_SIZE,3224I915_MAX_WM,32251,32262,3227I915_FIFO_LINE_SIZE3228};3229static const struct intel_watermark_params i855_wm_info = {3230I855GM_FIFO_SIZE,3231I915_MAX_WM,32321,32332,3234I830_FIFO_LINE_SIZE3235};3236static const struct intel_watermark_params i830_wm_info = {3237I830_FIFO_SIZE,3238I915_MAX_WM,32391,32402,3241I830_FIFO_LINE_SIZE3242};32433244static const struct intel_watermark_params ironlake_display_wm_info = {3245ILK_DISPLAY_FIFO,3246ILK_DISPLAY_MAXWM,3247ILK_DISPLAY_DFTWM,32482,3249ILK_FIFO_LINE_SIZE3250};3251static const struct intel_watermark_params ironlake_cursor_wm_info = {3252ILK_CURSOR_FIFO,3253ILK_CURSOR_MAXWM,3254ILK_CURSOR_DFTWM,32552,3256ILK_FIFO_LINE_SIZE3257};3258static const struct intel_watermark_params ironlake_display_srwm_info = {3259ILK_DISPLAY_SR_FIFO,3260ILK_DISPLAY_MAX_SRWM,3261ILK_DISPLAY_DFT_SRWM,32622,3263ILK_FIFO_LINE_SIZE3264};3265static const struct intel_watermark_params ironlake_cursor_srwm_info = {3266ILK_CURSOR_SR_FIFO,3267ILK_CURSOR_MAX_SRWM,3268ILK_CURSOR_DFT_SRWM,32692,3270ILK_FIFO_LINE_SIZE3271};32723273static const struct intel_watermark_params sandybridge_display_wm_info = {3274SNB_DISPLAY_FIFO,3275SNB_DISPLAY_MAXWM,3276SNB_DISPLAY_DFTWM,32772,3278SNB_FIFO_LINE_SIZE3279};3280static const struct intel_watermark_params sandybridge_cursor_wm_info = {3281SNB_CURSOR_FIFO,3282SNB_CURSOR_MAXWM,3283SNB_CURSOR_DFTWM,32842,3285SNB_FIFO_LINE_SIZE3286};3287static const struct intel_watermark_params sandybridge_display_srwm_info = {3288SNB_DISPLAY_SR_FIFO,3289SNB_DISPLAY_MAX_SRWM,3290SNB_DISPLAY_DFT_SRWM,32912,3292SNB_FIFO_LINE_SIZE3293};3294static const struct intel_watermark_params sandybridge_cursor_srwm_info = {3295SNB_CURSOR_SR_FIFO,3296SNB_CURSOR_MAX_SRWM,3297SNB_CURSOR_DFT_SRWM,32982,3299SNB_FIFO_LINE_SIZE3300};330133023303/**3304* intel_calculate_wm - calculate watermark level3305* @clock_in_khz: pixel clock3306* @wm: chip FIFO params3307* @pixel_size: display pixel size3308* @latency_ns: memory latency for the platform3309*3310* Calculate the watermark level (the level at which the display plane will3311* start fetching from memory again). Each chip has a different display3312* FIFO size and allocation, so the caller needs to figure that out and pass3313* in the correct intel_watermark_params structure.3314*3315* As the pixel clock runs, the FIFO will be drained at a rate that depends3316* on the pixel size. When it reaches the watermark level, it'll start3317* fetching FIFO line sized based chunks from memory until the FIFO fills3318* past the watermark point. If the FIFO drains completely, a FIFO underrun3319* will occur, and a display engine hang could result.3320*/3321static unsigned long intel_calculate_wm(unsigned long clock_in_khz,3322const struct intel_watermark_params *wm,3323int fifo_size,3324int pixel_size,3325unsigned long latency_ns)3326{3327long entries_required, wm_size;33283329/*3330* Note: we need to make sure we don't overflow for various clock &3331* latency values.3332* clocks go from a few thousand to several hundred thousand.3333* latency is usually a few thousand3334*/3335entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /33361000;3337entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);33383339DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);33403341wm_size = fifo_size - (entries_required + wm->guard_size);33423343DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);33443345/* Don't promote wm_size to unsigned... */3346if (wm_size > (long)wm->max_wm)3347wm_size = wm->max_wm;3348if (wm_size <= 0)3349wm_size = wm->default_wm;3350return wm_size;3351}33523353struct cxsr_latency {3354int is_desktop;3355int is_ddr3;3356unsigned long fsb_freq;3357unsigned long mem_freq;3358unsigned long display_sr;3359unsigned long display_hpll_disable;3360unsigned long cursor_sr;3361unsigned long cursor_hpll_disable;3362};33633364static const struct cxsr_latency cxsr_latency_table[] = {3365{1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */3366{1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */3367{1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */3368{1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */3369{1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */33703371{1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */3372{1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */3373{1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */3374{1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */3375{1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */33763377{1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */3378{1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */3379{1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */3380{1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */3381{1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */33823383{0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */3384{0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */3385{0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */3386{0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */3387{0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */33883389{0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */3390{0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */3391{0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */3392{0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */3393{0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */33943395{0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */3396{0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */3397{0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */3398{0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */3399{0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */3400};34013402static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,3403int is_ddr3,3404int fsb,3405int mem)3406{3407const struct cxsr_latency *latency;3408int i;34093410if (fsb == 0 || mem == 0)3411return NULL;34123413for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {3414latency = &cxsr_latency_table[i];3415if (is_desktop == latency->is_desktop &&3416is_ddr3 == latency->is_ddr3 &&3417fsb == latency->fsb_freq && mem == latency->mem_freq)3418return latency;3419}34203421DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");34223423return NULL;3424}34253426static void pineview_disable_cxsr(struct drm_device *dev)3427{3428struct drm_i915_private *dev_priv = dev->dev_private;34293430/* deactivate cxsr */3431I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);3432}34333434/*3435* Latency for FIFO fetches is dependent on several factors:3436* - memory configuration (speed, channels)3437* - chipset3438* - current MCH state3439* It can be fairly high in some situations, so here we assume a fairly3440* pessimal value. It's a tradeoff between extra memory fetches (if we3441* set this value too high, the FIFO will fetch frequently to stay full)3442* and power consumption (set it too low to save power and we might see3443* FIFO underruns and display "flicker").3444*3445* A value of 5us seems to be a good balance; safe for very low end3446* platforms but not overly aggressive on lower latency configs.3447*/3448static const int latency_ns = 5000;34493450static int i9xx_get_fifo_size(struct drm_device *dev, int plane)3451{3452struct drm_i915_private *dev_priv = dev->dev_private;3453uint32_t dsparb = I915_READ(DSPARB);3454int size;34553456size = dsparb & 0x7f;3457if (plane)3458size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;34593460DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,3461plane ? "B" : "A", size);34623463return size;3464}34653466static int i85x_get_fifo_size(struct drm_device *dev, int plane)3467{3468struct drm_i915_private *dev_priv = dev->dev_private;3469uint32_t dsparb = I915_READ(DSPARB);3470int size;34713472size = dsparb & 0x1ff;3473if (plane)3474size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;3475size >>= 1; /* Convert to cachelines */34763477DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,3478plane ? "B" : "A", size);34793480return size;3481}34823483static int i845_get_fifo_size(struct drm_device *dev, int plane)3484{3485struct drm_i915_private *dev_priv = dev->dev_private;3486uint32_t dsparb = I915_READ(DSPARB);3487int size;34883489size = dsparb & 0x7f;3490size >>= 2; /* Convert to cachelines */34913492DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,3493plane ? "B" : "A",3494size);34953496return size;3497}34983499static int i830_get_fifo_size(struct drm_device *dev, int plane)3500{3501struct drm_i915_private *dev_priv = dev->dev_private;3502uint32_t dsparb = I915_READ(DSPARB);3503int size;35043505size = dsparb & 0x7f;3506size >>= 1; /* Convert to cachelines */35073508DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,3509plane ? "B" : "A", size);35103511return size;3512}35133514static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)3515{3516struct drm_crtc *crtc, *enabled = NULL;35173518list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {3519if (crtc->enabled && crtc->fb) {3520if (enabled)3521return NULL;3522enabled = crtc;3523}3524}35253526return enabled;3527}35283529static void pineview_update_wm(struct drm_device *dev)3530{3531struct drm_i915_private *dev_priv = dev->dev_private;3532struct drm_crtc *crtc;3533const struct cxsr_latency *latency;3534u32 reg;3535unsigned long wm;35363537latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,3538dev_priv->fsb_freq, dev_priv->mem_freq);3539if (!latency) {3540DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");3541pineview_disable_cxsr(dev);3542return;3543}35443545crtc = single_enabled_crtc(dev);3546if (crtc) {3547int clock = crtc->mode.clock;3548int pixel_size = crtc->fb->bits_per_pixel / 8;35493550/* Display SR */3551wm = intel_calculate_wm(clock, &pineview_display_wm,3552pineview_display_wm.fifo_size,3553pixel_size, latency->display_sr);3554reg = I915_READ(DSPFW1);3555reg &= ~DSPFW_SR_MASK;3556reg |= wm << DSPFW_SR_SHIFT;3557I915_WRITE(DSPFW1, reg);3558DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);35593560/* cursor SR */3561wm = intel_calculate_wm(clock, &pineview_cursor_wm,3562pineview_display_wm.fifo_size,3563pixel_size, latency->cursor_sr);3564reg = I915_READ(DSPFW3);3565reg &= ~DSPFW_CURSOR_SR_MASK;3566reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;3567I915_WRITE(DSPFW3, reg);35683569/* Display HPLL off SR */3570wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,3571pineview_display_hplloff_wm.fifo_size,3572pixel_size, latency->display_hpll_disable);3573reg = I915_READ(DSPFW3);3574reg &= ~DSPFW_HPLL_SR_MASK;3575reg |= wm & DSPFW_HPLL_SR_MASK;3576I915_WRITE(DSPFW3, reg);35773578/* cursor HPLL off SR */3579wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,3580pineview_display_hplloff_wm.fifo_size,3581pixel_size, latency->cursor_hpll_disable);3582reg = I915_READ(DSPFW3);3583reg &= ~DSPFW_HPLL_CURSOR_MASK;3584reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;3585I915_WRITE(DSPFW3, reg);3586DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);35873588/* activate cxsr */3589I915_WRITE(DSPFW3,3590I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);3591DRM_DEBUG_KMS("Self-refresh is enabled\n");3592} else {3593pineview_disable_cxsr(dev);3594DRM_DEBUG_KMS("Self-refresh is disabled\n");3595}3596}35973598static bool g4x_compute_wm0(struct drm_device *dev,3599int plane,3600const struct intel_watermark_params *display,3601int display_latency_ns,3602const struct intel_watermark_params *cursor,3603int cursor_latency_ns,3604int *plane_wm,3605int *cursor_wm)3606{3607struct drm_crtc *crtc;3608int htotal, hdisplay, clock, pixel_size;3609int line_time_us, line_count;3610int entries, tlb_miss;36113612crtc = intel_get_crtc_for_plane(dev, plane);3613if (crtc->fb == NULL || !crtc->enabled) {3614*cursor_wm = cursor->guard_size;3615*plane_wm = display->guard_size;3616return false;3617}36183619htotal = crtc->mode.htotal;3620hdisplay = crtc->mode.hdisplay;3621clock = crtc->mode.clock;3622pixel_size = crtc->fb->bits_per_pixel / 8;36233624/* Use the small buffer method to calculate plane watermark */3625entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;3626tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;3627if (tlb_miss > 0)3628entries += tlb_miss;3629entries = DIV_ROUND_UP(entries, display->cacheline_size);3630*plane_wm = entries + display->guard_size;3631if (*plane_wm > (int)display->max_wm)3632*plane_wm = display->max_wm;36333634/* Use the large buffer method to calculate cursor watermark */3635line_time_us = ((htotal * 1000) / clock);3636line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;3637entries = line_count * 64 * pixel_size;3638tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;3639if (tlb_miss > 0)3640entries += tlb_miss;3641entries = DIV_ROUND_UP(entries, cursor->cacheline_size);3642*cursor_wm = entries + cursor->guard_size;3643if (*cursor_wm > (int)cursor->max_wm)3644*cursor_wm = (int)cursor->max_wm;36453646return true;3647}36483649/*3650* Check the wm result.3651*3652* If any calculated watermark values is larger than the maximum value that3653* can be programmed into the associated watermark register, that watermark3654* must be disabled.3655*/3656static bool g4x_check_srwm(struct drm_device *dev,3657int display_wm, int cursor_wm,3658const struct intel_watermark_params *display,3659const struct intel_watermark_params *cursor)3660{3661DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",3662display_wm, cursor_wm);36633664if (display_wm > display->max_wm) {3665DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",3666display_wm, display->max_wm);3667return false;3668}36693670if (cursor_wm > cursor->max_wm) {3671DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",3672cursor_wm, cursor->max_wm);3673return false;3674}36753676if (!(display_wm || cursor_wm)) {3677DRM_DEBUG_KMS("SR latency is 0, disabling\n");3678return false;3679}36803681return true;3682}36833684static bool g4x_compute_srwm(struct drm_device *dev,3685int plane,3686int latency_ns,3687const struct intel_watermark_params *display,3688const struct intel_watermark_params *cursor,3689int *display_wm, int *cursor_wm)3690{3691struct drm_crtc *crtc;3692int hdisplay, htotal, pixel_size, clock;3693unsigned long line_time_us;3694int line_count, line_size;3695int small, large;3696int entries;36973698if (!latency_ns) {3699*display_wm = *cursor_wm = 0;3700return false;3701}37023703crtc = intel_get_crtc_for_plane(dev, plane);3704hdisplay = crtc->mode.hdisplay;3705htotal = crtc->mode.htotal;3706clock = crtc->mode.clock;3707pixel_size = crtc->fb->bits_per_pixel / 8;37083709line_time_us = (htotal * 1000) / clock;3710line_count = (latency_ns / line_time_us + 1000) / 1000;3711line_size = hdisplay * pixel_size;37123713/* Use the minimum of the small and large buffer method for primary */3714small = ((clock * pixel_size / 1000) * latency_ns) / 1000;3715large = line_count * line_size;37163717entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);3718*display_wm = entries + display->guard_size;37193720/* calculate the self-refresh watermark for display cursor */3721entries = line_count * pixel_size * 64;3722entries = DIV_ROUND_UP(entries, cursor->cacheline_size);3723*cursor_wm = entries + cursor->guard_size;37243725return g4x_check_srwm(dev,3726*display_wm, *cursor_wm,3727display, cursor);3728}37293730#define single_plane_enabled(mask) is_power_of_2(mask)37313732static void g4x_update_wm(struct drm_device *dev)3733{3734static const int sr_latency_ns = 12000;3735struct drm_i915_private *dev_priv = dev->dev_private;3736int planea_wm, planeb_wm, cursora_wm, cursorb_wm;3737int plane_sr, cursor_sr;3738unsigned int enabled = 0;37393740if (g4x_compute_wm0(dev, 0,3741&g4x_wm_info, latency_ns,3742&g4x_cursor_wm_info, latency_ns,3743&planea_wm, &cursora_wm))3744enabled |= 1;37453746if (g4x_compute_wm0(dev, 1,3747&g4x_wm_info, latency_ns,3748&g4x_cursor_wm_info, latency_ns,3749&planeb_wm, &cursorb_wm))3750enabled |= 2;37513752plane_sr = cursor_sr = 0;3753if (single_plane_enabled(enabled) &&3754g4x_compute_srwm(dev, ffs(enabled) - 1,3755sr_latency_ns,3756&g4x_wm_info,3757&g4x_cursor_wm_info,3758&plane_sr, &cursor_sr))3759I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);3760else3761I915_WRITE(FW_BLC_SELF,3762I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);37633764DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",3765planea_wm, cursora_wm,3766planeb_wm, cursorb_wm,3767plane_sr, cursor_sr);37683769I915_WRITE(DSPFW1,3770(plane_sr << DSPFW_SR_SHIFT) |3771(cursorb_wm << DSPFW_CURSORB_SHIFT) |3772(planeb_wm << DSPFW_PLANEB_SHIFT) |3773planea_wm);3774I915_WRITE(DSPFW2,3775(I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |3776(cursora_wm << DSPFW_CURSORA_SHIFT));3777/* HPLL off in SR has some issues on G4x... disable it */3778I915_WRITE(DSPFW3,3779(I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |3780(cursor_sr << DSPFW_CURSOR_SR_SHIFT));3781}37823783static void i965_update_wm(struct drm_device *dev)3784{3785struct drm_i915_private *dev_priv = dev->dev_private;3786struct drm_crtc *crtc;3787int srwm = 1;3788int cursor_sr = 16;37893790/* Calc sr entries for one plane configs */3791crtc = single_enabled_crtc(dev);3792if (crtc) {3793/* self-refresh has much higher latency */3794static const int sr_latency_ns = 12000;3795int clock = crtc->mode.clock;3796int htotal = crtc->mode.htotal;3797int hdisplay = crtc->mode.hdisplay;3798int pixel_size = crtc->fb->bits_per_pixel / 8;3799unsigned long line_time_us;3800int entries;38013802line_time_us = ((htotal * 1000) / clock);38033804/* Use ns/us then divide to preserve precision */3805entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *3806pixel_size * hdisplay;3807entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);3808srwm = I965_FIFO_SIZE - entries;3809if (srwm < 0)3810srwm = 1;3811srwm &= 0x1ff;3812DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",3813entries, srwm);38143815entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *3816pixel_size * 64;3817entries = DIV_ROUND_UP(entries,3818i965_cursor_wm_info.cacheline_size);3819cursor_sr = i965_cursor_wm_info.fifo_size -3820(entries + i965_cursor_wm_info.guard_size);38213822if (cursor_sr > i965_cursor_wm_info.max_wm)3823cursor_sr = i965_cursor_wm_info.max_wm;38243825DRM_DEBUG_KMS("self-refresh watermark: display plane %d "3826"cursor %d\n", srwm, cursor_sr);38273828if (IS_CRESTLINE(dev))3829I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);3830} else {3831/* Turn off self refresh if both pipes are enabled */3832if (IS_CRESTLINE(dev))3833I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)3834& ~FW_BLC_SELF_EN);3835}38363837DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",3838srwm);38393840/* 965 has limitations... */3841I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |3842(8 << 16) | (8 << 8) | (8 << 0));3843I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));3844/* update cursor SR watermark */3845I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));3846}38473848static void i9xx_update_wm(struct drm_device *dev)3849{3850struct drm_i915_private *dev_priv = dev->dev_private;3851const struct intel_watermark_params *wm_info;3852uint32_t fwater_lo;3853uint32_t fwater_hi;3854int cwm, srwm = 1;3855int fifo_size;3856int planea_wm, planeb_wm;3857struct drm_crtc *crtc, *enabled = NULL;38583859if (IS_I945GM(dev))3860wm_info = &i945_wm_info;3861else if (!IS_GEN2(dev))3862wm_info = &i915_wm_info;3863else3864wm_info = &i855_wm_info;38653866fifo_size = dev_priv->display.get_fifo_size(dev, 0);3867crtc = intel_get_crtc_for_plane(dev, 0);3868if (crtc->enabled && crtc->fb) {3869planea_wm = intel_calculate_wm(crtc->mode.clock,3870wm_info, fifo_size,3871crtc->fb->bits_per_pixel / 8,3872latency_ns);3873enabled = crtc;3874} else3875planea_wm = fifo_size - wm_info->guard_size;38763877fifo_size = dev_priv->display.get_fifo_size(dev, 1);3878crtc = intel_get_crtc_for_plane(dev, 1);3879if (crtc->enabled && crtc->fb) {3880planeb_wm = intel_calculate_wm(crtc->mode.clock,3881wm_info, fifo_size,3882crtc->fb->bits_per_pixel / 8,3883latency_ns);3884if (enabled == NULL)3885enabled = crtc;3886else3887enabled = NULL;3888} else3889planeb_wm = fifo_size - wm_info->guard_size;38903891DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);38923893/*3894* Overlay gets an aggressive default since video jitter is bad.3895*/3896cwm = 2;38973898/* Play safe and disable self-refresh before adjusting watermarks. */3899if (IS_I945G(dev) || IS_I945GM(dev))3900I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);3901else if (IS_I915GM(dev))3902I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);39033904/* Calc sr entries for one plane configs */3905if (HAS_FW_BLC(dev) && enabled) {3906/* self-refresh has much higher latency */3907static const int sr_latency_ns = 6000;3908int clock = enabled->mode.clock;3909int htotal = enabled->mode.htotal;3910int hdisplay = enabled->mode.hdisplay;3911int pixel_size = enabled->fb->bits_per_pixel / 8;3912unsigned long line_time_us;3913int entries;39143915line_time_us = (htotal * 1000) / clock;39163917/* Use ns/us then divide to preserve precision */3918entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *3919pixel_size * hdisplay;3920entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);3921DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);3922srwm = wm_info->fifo_size - entries;3923if (srwm < 0)3924srwm = 1;39253926if (IS_I945G(dev) || IS_I945GM(dev))3927I915_WRITE(FW_BLC_SELF,3928FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));3929else if (IS_I915GM(dev))3930I915_WRITE(FW_BLC_SELF, srwm & 0x3f);3931}39323933DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",3934planea_wm, planeb_wm, cwm, srwm);39353936fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);3937fwater_hi = (cwm & 0x1f);39383939/* Set request length to 8 cachelines per fetch */3940fwater_lo = fwater_lo | (1 << 24) | (1 << 8);3941fwater_hi = fwater_hi | (1 << 8);39423943I915_WRITE(FW_BLC, fwater_lo);3944I915_WRITE(FW_BLC2, fwater_hi);39453946if (HAS_FW_BLC(dev)) {3947if (enabled) {3948if (IS_I945G(dev) || IS_I945GM(dev))3949I915_WRITE(FW_BLC_SELF,3950FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);3951else if (IS_I915GM(dev))3952I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);3953DRM_DEBUG_KMS("memory self refresh enabled\n");3954} else3955DRM_DEBUG_KMS("memory self refresh disabled\n");3956}3957}39583959static void i830_update_wm(struct drm_device *dev)3960{3961struct drm_i915_private *dev_priv = dev->dev_private;3962struct drm_crtc *crtc;3963uint32_t fwater_lo;3964int planea_wm;39653966crtc = single_enabled_crtc(dev);3967if (crtc == NULL)3968return;39693970planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,3971dev_priv->display.get_fifo_size(dev, 0),3972crtc->fb->bits_per_pixel / 8,3973latency_ns);3974fwater_lo = I915_READ(FW_BLC) & ~0xfff;3975fwater_lo |= (3<<8) | planea_wm;39763977DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);39783979I915_WRITE(FW_BLC, fwater_lo);3980}39813982#define ILK_LP0_PLANE_LATENCY 7003983#define ILK_LP0_CURSOR_LATENCY 130039843985/*3986* Check the wm result.3987*3988* If any calculated watermark values is larger than the maximum value that3989* can be programmed into the associated watermark register, that watermark3990* must be disabled.3991*/3992static bool ironlake_check_srwm(struct drm_device *dev, int level,3993int fbc_wm, int display_wm, int cursor_wm,3994const struct intel_watermark_params *display,3995const struct intel_watermark_params *cursor)3996{3997struct drm_i915_private *dev_priv = dev->dev_private;39983999DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"4000" cursor %d\n", level, display_wm, fbc_wm, cursor_wm);40014002if (fbc_wm > SNB_FBC_MAX_SRWM) {4003DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",4004fbc_wm, SNB_FBC_MAX_SRWM, level);40054006/* fbc has it's own way to disable FBC WM */4007I915_WRITE(DISP_ARB_CTL,4008I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);4009return false;4010}40114012if (display_wm > display->max_wm) {4013DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",4014display_wm, SNB_DISPLAY_MAX_SRWM, level);4015return false;4016}40174018if (cursor_wm > cursor->max_wm) {4019DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",4020cursor_wm, SNB_CURSOR_MAX_SRWM, level);4021return false;4022}40234024if (!(fbc_wm || display_wm || cursor_wm)) {4025DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);4026return false;4027}40284029return true;4030}40314032/*4033* Compute watermark values of WM[1-3],4034*/4035static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,4036int latency_ns,4037const struct intel_watermark_params *display,4038const struct intel_watermark_params *cursor,4039int *fbc_wm, int *display_wm, int *cursor_wm)4040{4041struct drm_crtc *crtc;4042unsigned long line_time_us;4043int hdisplay, htotal, pixel_size, clock;4044int line_count, line_size;4045int small, large;4046int entries;40474048if (!latency_ns) {4049*fbc_wm = *display_wm = *cursor_wm = 0;4050return false;4051}40524053crtc = intel_get_crtc_for_plane(dev, plane);4054hdisplay = crtc->mode.hdisplay;4055htotal = crtc->mode.htotal;4056clock = crtc->mode.clock;4057pixel_size = crtc->fb->bits_per_pixel / 8;40584059line_time_us = (htotal * 1000) / clock;4060line_count = (latency_ns / line_time_us + 1000) / 1000;4061line_size = hdisplay * pixel_size;40624063/* Use the minimum of the small and large buffer method for primary */4064small = ((clock * pixel_size / 1000) * latency_ns) / 1000;4065large = line_count * line_size;40664067entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);4068*display_wm = entries + display->guard_size;40694070/*4071* Spec says:4072* FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 24073*/4074*fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;40754076/* calculate the self-refresh watermark for display cursor */4077entries = line_count * pixel_size * 64;4078entries = DIV_ROUND_UP(entries, cursor->cacheline_size);4079*cursor_wm = entries + cursor->guard_size;40804081return ironlake_check_srwm(dev, level,4082*fbc_wm, *display_wm, *cursor_wm,4083display, cursor);4084}40854086static void ironlake_update_wm(struct drm_device *dev)4087{4088struct drm_i915_private *dev_priv = dev->dev_private;4089int fbc_wm, plane_wm, cursor_wm;4090unsigned int enabled;40914092enabled = 0;4093if (g4x_compute_wm0(dev, 0,4094&ironlake_display_wm_info,4095ILK_LP0_PLANE_LATENCY,4096&ironlake_cursor_wm_info,4097ILK_LP0_CURSOR_LATENCY,4098&plane_wm, &cursor_wm)) {4099I915_WRITE(WM0_PIPEA_ILK,4100(plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);4101DRM_DEBUG_KMS("FIFO watermarks For pipe A -"4102" plane %d, " "cursor: %d\n",4103plane_wm, cursor_wm);4104enabled |= 1;4105}41064107if (g4x_compute_wm0(dev, 1,4108&ironlake_display_wm_info,4109ILK_LP0_PLANE_LATENCY,4110&ironlake_cursor_wm_info,4111ILK_LP0_CURSOR_LATENCY,4112&plane_wm, &cursor_wm)) {4113I915_WRITE(WM0_PIPEB_ILK,4114(plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);4115DRM_DEBUG_KMS("FIFO watermarks For pipe B -"4116" plane %d, cursor: %d\n",4117plane_wm, cursor_wm);4118enabled |= 2;4119}41204121/*4122* Calculate and update the self-refresh watermark only when one4123* display plane is used.4124*/4125I915_WRITE(WM3_LP_ILK, 0);4126I915_WRITE(WM2_LP_ILK, 0);4127I915_WRITE(WM1_LP_ILK, 0);41284129if (!single_plane_enabled(enabled))4130return;4131enabled = ffs(enabled) - 1;41324133/* WM1 */4134if (!ironlake_compute_srwm(dev, 1, enabled,4135ILK_READ_WM1_LATENCY() * 500,4136&ironlake_display_srwm_info,4137&ironlake_cursor_srwm_info,4138&fbc_wm, &plane_wm, &cursor_wm))4139return;41404141I915_WRITE(WM1_LP_ILK,4142WM1_LP_SR_EN |4143(ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |4144(fbc_wm << WM1_LP_FBC_SHIFT) |4145(plane_wm << WM1_LP_SR_SHIFT) |4146cursor_wm);41474148/* WM2 */4149if (!ironlake_compute_srwm(dev, 2, enabled,4150ILK_READ_WM2_LATENCY() * 500,4151&ironlake_display_srwm_info,4152&ironlake_cursor_srwm_info,4153&fbc_wm, &plane_wm, &cursor_wm))4154return;41554156I915_WRITE(WM2_LP_ILK,4157WM2_LP_EN |4158(ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |4159(fbc_wm << WM1_LP_FBC_SHIFT) |4160(plane_wm << WM1_LP_SR_SHIFT) |4161cursor_wm);41624163/*4164* WM3 is unsupported on ILK, probably because we don't have latency4165* data for that power state4166*/4167}41684169static void sandybridge_update_wm(struct drm_device *dev)4170{4171struct drm_i915_private *dev_priv = dev->dev_private;4172int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */4173int fbc_wm, plane_wm, cursor_wm;4174unsigned int enabled;41754176enabled = 0;4177if (g4x_compute_wm0(dev, 0,4178&sandybridge_display_wm_info, latency,4179&sandybridge_cursor_wm_info, latency,4180&plane_wm, &cursor_wm)) {4181I915_WRITE(WM0_PIPEA_ILK,4182(plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);4183DRM_DEBUG_KMS("FIFO watermarks For pipe A -"4184" plane %d, " "cursor: %d\n",4185plane_wm, cursor_wm);4186enabled |= 1;4187}41884189if (g4x_compute_wm0(dev, 1,4190&sandybridge_display_wm_info, latency,4191&sandybridge_cursor_wm_info, latency,4192&plane_wm, &cursor_wm)) {4193I915_WRITE(WM0_PIPEB_ILK,4194(plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);4195DRM_DEBUG_KMS("FIFO watermarks For pipe B -"4196" plane %d, cursor: %d\n",4197plane_wm, cursor_wm);4198enabled |= 2;4199}42004201/*4202* Calculate and update the self-refresh watermark only when one4203* display plane is used.4204*4205* SNB support 3 levels of watermark.4206*4207* WM1/WM2/WM2 watermarks have to be enabled in the ascending order,4208* and disabled in the descending order4209*4210*/4211I915_WRITE(WM3_LP_ILK, 0);4212I915_WRITE(WM2_LP_ILK, 0);4213I915_WRITE(WM1_LP_ILK, 0);42144215if (!single_plane_enabled(enabled))4216return;4217enabled = ffs(enabled) - 1;42184219/* WM1 */4220if (!ironlake_compute_srwm(dev, 1, enabled,4221SNB_READ_WM1_LATENCY() * 500,4222&sandybridge_display_srwm_info,4223&sandybridge_cursor_srwm_info,4224&fbc_wm, &plane_wm, &cursor_wm))4225return;42264227I915_WRITE(WM1_LP_ILK,4228WM1_LP_SR_EN |4229(SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |4230(fbc_wm << WM1_LP_FBC_SHIFT) |4231(plane_wm << WM1_LP_SR_SHIFT) |4232cursor_wm);42334234/* WM2 */4235if (!ironlake_compute_srwm(dev, 2, enabled,4236SNB_READ_WM2_LATENCY() * 500,4237&sandybridge_display_srwm_info,4238&sandybridge_cursor_srwm_info,4239&fbc_wm, &plane_wm, &cursor_wm))4240return;42414242I915_WRITE(WM2_LP_ILK,4243WM2_LP_EN |4244(SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |4245(fbc_wm << WM1_LP_FBC_SHIFT) |4246(plane_wm << WM1_LP_SR_SHIFT) |4247cursor_wm);42484249/* WM3 */4250if (!ironlake_compute_srwm(dev, 3, enabled,4251SNB_READ_WM3_LATENCY() * 500,4252&sandybridge_display_srwm_info,4253&sandybridge_cursor_srwm_info,4254&fbc_wm, &plane_wm, &cursor_wm))4255return;42564257I915_WRITE(WM3_LP_ILK,4258WM3_LP_EN |4259(SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |4260(fbc_wm << WM1_LP_FBC_SHIFT) |4261(plane_wm << WM1_LP_SR_SHIFT) |4262cursor_wm);4263}42644265/**4266* intel_update_watermarks - update FIFO watermark values based on current modes4267*4268* Calculate watermark values for the various WM regs based on current mode4269* and plane configuration.4270*4271* There are several cases to deal with here:4272* - normal (i.e. non-self-refresh)4273* - self-refresh (SR) mode4274* - lines are large relative to FIFO size (buffer can hold up to 2)4275* - lines are small relative to FIFO size (buffer can hold more than 24276* lines), so need to account for TLB latency4277*4278* The normal calculation is:4279* watermark = dotclock * bytes per pixel * latency4280* where latency is platform & configuration dependent (we assume pessimal4281* values here).4282*4283* The SR calculation is:4284* watermark = (trunc(latency/line time)+1) * surface width *4285* bytes per pixel4286* where4287* line time = htotal / dotclock4288* surface width = hdisplay for normal plane and 64 for cursor4289* and latency is assumed to be high, as above.4290*4291* The final value programmed to the register should always be rounded up,4292* and include an extra 2 entries to account for clock crossings.4293*4294* We don't use the sprite, so we can ignore that. And on Crestline we have4295* to set the non-SR watermarks to 8.4296*/4297static void intel_update_watermarks(struct drm_device *dev)4298{4299struct drm_i915_private *dev_priv = dev->dev_private;43004301if (dev_priv->display.update_wm)4302dev_priv->display.update_wm(dev);4303}43044305static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)4306{4307return dev_priv->lvds_use_ssc && i915_panel_use_ssc4308&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);4309}43104311static int i9xx_crtc_mode_set(struct drm_crtc *crtc,4312struct drm_display_mode *mode,4313struct drm_display_mode *adjusted_mode,4314int x, int y,4315struct drm_framebuffer *old_fb)4316{4317struct drm_device *dev = crtc->dev;4318struct drm_i915_private *dev_priv = dev->dev_private;4319struct intel_crtc *intel_crtc = to_intel_crtc(crtc);4320int pipe = intel_crtc->pipe;4321int plane = intel_crtc->plane;4322int refclk, num_connectors = 0;4323intel_clock_t clock, reduced_clock;4324u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;4325bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;4326bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;4327struct drm_mode_config *mode_config = &dev->mode_config;4328struct intel_encoder *encoder;4329const intel_limit_t *limit;4330int ret;4331u32 temp;4332u32 lvds_sync = 0;43334334list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {4335if (encoder->base.crtc != crtc)4336continue;43374338switch (encoder->type) {4339case INTEL_OUTPUT_LVDS:4340is_lvds = true;4341break;4342case INTEL_OUTPUT_SDVO:4343case INTEL_OUTPUT_HDMI:4344is_sdvo = true;4345if (encoder->needs_tv_clock)4346is_tv = true;4347break;4348case INTEL_OUTPUT_DVO:4349is_dvo = true;4350break;4351case INTEL_OUTPUT_TVOUT:4352is_tv = true;4353break;4354case INTEL_OUTPUT_ANALOG:4355is_crt = true;4356break;4357case INTEL_OUTPUT_DISPLAYPORT:4358is_dp = true;4359break;4360}43614362num_connectors++;4363}43644365if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {4366refclk = dev_priv->lvds_ssc_freq * 1000;4367DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",4368refclk / 1000);4369} else if (!IS_GEN2(dev)) {4370refclk = 96000;4371} else {4372refclk = 48000;4373}43744375/*4376* Returns a set of divisors for the desired target clock with the given4377* refclk, or FALSE. The returned values represent the clock equation:4378* reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.4379*/4380limit = intel_limit(crtc, refclk);4381ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);4382if (!ok) {4383DRM_ERROR("Couldn't find PLL settings for mode!\n");4384return -EINVAL;4385}43864387/* Ensure that the cursor is valid for the new mode before changing... */4388intel_crtc_update_cursor(crtc, true);43894390if (is_lvds && dev_priv->lvds_downclock_avail) {4391has_reduced_clock = limit->find_pll(limit, crtc,4392dev_priv->lvds_downclock,4393refclk,4394&reduced_clock);4395if (has_reduced_clock && (clock.p != reduced_clock.p)) {4396/*4397* If the different P is found, it means that we can't4398* switch the display clock by using the FP0/FP1.4399* In such case we will disable the LVDS downclock4400* feature.4401*/4402DRM_DEBUG_KMS("Different P is found for "4403"LVDS clock/downclock\n");4404has_reduced_clock = 0;4405}4406}4407/* SDVO TV has fixed PLL values depend on its clock range,4408this mirrors vbios setting. */4409if (is_sdvo && is_tv) {4410if (adjusted_mode->clock >= 1000004411&& adjusted_mode->clock < 140500) {4412clock.p1 = 2;4413clock.p2 = 10;4414clock.n = 3;4415clock.m1 = 16;4416clock.m2 = 8;4417} else if (adjusted_mode->clock >= 1405004418&& adjusted_mode->clock <= 200000) {4419clock.p1 = 1;4420clock.p2 = 10;4421clock.n = 6;4422clock.m1 = 12;4423clock.m2 = 8;4424}4425}44264427if (IS_PINEVIEW(dev)) {4428fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;4429if (has_reduced_clock)4430fp2 = (1 << reduced_clock.n) << 16 |4431reduced_clock.m1 << 8 | reduced_clock.m2;4432} else {4433fp = clock.n << 16 | clock.m1 << 8 | clock.m2;4434if (has_reduced_clock)4435fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |4436reduced_clock.m2;4437}44384439dpll = DPLL_VGA_MODE_DIS;44404441if (!IS_GEN2(dev)) {4442if (is_lvds)4443dpll |= DPLLB_MODE_LVDS;4444else4445dpll |= DPLLB_MODE_DAC_SERIAL;4446if (is_sdvo) {4447int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);4448if (pixel_multiplier > 1) {4449if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))4450dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;4451}4452dpll |= DPLL_DVO_HIGH_SPEED;4453}4454if (is_dp)4455dpll |= DPLL_DVO_HIGH_SPEED;44564457/* compute bitmask from p1 value */4458if (IS_PINEVIEW(dev))4459dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;4460else {4461dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;4462if (IS_G4X(dev) && has_reduced_clock)4463dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;4464}4465switch (clock.p2) {4466case 5:4467dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;4468break;4469case 7:4470dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;4471break;4472case 10:4473dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;4474break;4475case 14:4476dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;4477break;4478}4479if (INTEL_INFO(dev)->gen >= 4)4480dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);4481} else {4482if (is_lvds) {4483dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;4484} else {4485if (clock.p1 == 2)4486dpll |= PLL_P1_DIVIDE_BY_TWO;4487else4488dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;4489if (clock.p2 == 4)4490dpll |= PLL_P2_DIVIDE_BY_4;4491}4492}44934494if (is_sdvo && is_tv)4495dpll |= PLL_REF_INPUT_TVCLKINBC;4496else if (is_tv)4497/* XXX: just matching BIOS for now */4498/* dpll |= PLL_REF_INPUT_TVCLKINBC; */4499dpll |= 3;4500else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)4501dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;4502else4503dpll |= PLL_REF_INPUT_DREFCLK;45044505/* setup pipeconf */4506pipeconf = I915_READ(PIPECONF(pipe));45074508/* Set up the display plane register */4509dspcntr = DISPPLANE_GAMMA_ENABLE;45104511/* Ironlake's plane is forced to pipe, bit 24 is to4512enable color space conversion */4513if (pipe == 0)4514dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;4515else4516dspcntr |= DISPPLANE_SEL_PIPE_B;45174518if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {4519/* Enable pixel doubling when the dot clock is > 90% of the (display)4520* core speed.4521*4522* XXX: No double-wide on 915GM pipe B. Is that the only reason for the4523* pipe == 0 check?4524*/4525if (mode->clock >4526dev_priv->display.get_display_clock_speed(dev) * 9 / 10)4527pipeconf |= PIPECONF_DOUBLE_WIDE;4528else4529pipeconf &= ~PIPECONF_DOUBLE_WIDE;4530}45314532dpll |= DPLL_VCO_ENABLE;45334534DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');4535drm_mode_debug_printmodeline(mode);45364537I915_WRITE(FP0(pipe), fp);4538I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);45394540POSTING_READ(DPLL(pipe));4541udelay(150);45424543/* The LVDS pin pair needs to be on before the DPLLs are enabled.4544* This is an exception to the general rule that mode_set doesn't turn4545* things on.4546*/4547if (is_lvds) {4548temp = I915_READ(LVDS);4549temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;4550if (pipe == 1) {4551temp |= LVDS_PIPEB_SELECT;4552} else {4553temp &= ~LVDS_PIPEB_SELECT;4554}4555/* set the corresponsding LVDS_BORDER bit */4556temp |= dev_priv->lvds_border_bits;4557/* Set the B0-B3 data pairs corresponding to whether we're going to4558* set the DPLLs for dual-channel mode or not.4559*/4560if (clock.p2 == 7)4561temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;4562else4563temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);45644565/* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)4566* appropriately here, but we need to look more thoroughly into how4567* panels behave in the two modes.4568*/4569/* set the dithering flag on LVDS as needed */4570if (INTEL_INFO(dev)->gen >= 4) {4571if (dev_priv->lvds_dither)4572temp |= LVDS_ENABLE_DITHER;4573else4574temp &= ~LVDS_ENABLE_DITHER;4575}4576if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)4577lvds_sync |= LVDS_HSYNC_POLARITY;4578if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)4579lvds_sync |= LVDS_VSYNC_POLARITY;4580if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))4581!= lvds_sync) {4582char flags[2] = "-+";4583DRM_INFO("Changing LVDS panel from "4584"(%chsync, %cvsync) to (%chsync, %cvsync)\n",4585flags[!(temp & LVDS_HSYNC_POLARITY)],4586flags[!(temp & LVDS_VSYNC_POLARITY)],4587flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],4588flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);4589temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);4590temp |= lvds_sync;4591}4592I915_WRITE(LVDS, temp);4593}45944595if (is_dp) {4596intel_dp_set_m_n(crtc, mode, adjusted_mode);4597}45984599I915_WRITE(DPLL(pipe), dpll);46004601/* Wait for the clocks to stabilize. */4602POSTING_READ(DPLL(pipe));4603udelay(150);46044605if (INTEL_INFO(dev)->gen >= 4) {4606temp = 0;4607if (is_sdvo) {4608temp = intel_mode_get_pixel_multiplier(adjusted_mode);4609if (temp > 1)4610temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;4611else4612temp = 0;4613}4614I915_WRITE(DPLL_MD(pipe), temp);4615} else {4616/* The pixel multiplier can only be updated once the4617* DPLL is enabled and the clocks are stable.4618*4619* So write it again.4620*/4621I915_WRITE(DPLL(pipe), dpll);4622}46234624intel_crtc->lowfreq_avail = false;4625if (is_lvds && has_reduced_clock && i915_powersave) {4626I915_WRITE(FP1(pipe), fp2);4627intel_crtc->lowfreq_avail = true;4628if (HAS_PIPE_CXSR(dev)) {4629DRM_DEBUG_KMS("enabling CxSR downclocking\n");4630pipeconf |= PIPECONF_CXSR_DOWNCLOCK;4631}4632} else {4633I915_WRITE(FP1(pipe), fp);4634if (HAS_PIPE_CXSR(dev)) {4635DRM_DEBUG_KMS("disabling CxSR downclocking\n");4636pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;4637}4638}46394640if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {4641pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;4642/* the chip adds 2 halflines automatically */4643adjusted_mode->crtc_vdisplay -= 1;4644adjusted_mode->crtc_vtotal -= 1;4645adjusted_mode->crtc_vblank_start -= 1;4646adjusted_mode->crtc_vblank_end -= 1;4647adjusted_mode->crtc_vsync_end -= 1;4648adjusted_mode->crtc_vsync_start -= 1;4649} else4650pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */46514652I915_WRITE(HTOTAL(pipe),4653(adjusted_mode->crtc_hdisplay - 1) |4654((adjusted_mode->crtc_htotal - 1) << 16));4655I915_WRITE(HBLANK(pipe),4656(adjusted_mode->crtc_hblank_start - 1) |4657((adjusted_mode->crtc_hblank_end - 1) << 16));4658I915_WRITE(HSYNC(pipe),4659(adjusted_mode->crtc_hsync_start - 1) |4660((adjusted_mode->crtc_hsync_end - 1) << 16));46614662I915_WRITE(VTOTAL(pipe),4663(adjusted_mode->crtc_vdisplay - 1) |4664((adjusted_mode->crtc_vtotal - 1) << 16));4665I915_WRITE(VBLANK(pipe),4666(adjusted_mode->crtc_vblank_start - 1) |4667((adjusted_mode->crtc_vblank_end - 1) << 16));4668I915_WRITE(VSYNC(pipe),4669(adjusted_mode->crtc_vsync_start - 1) |4670((adjusted_mode->crtc_vsync_end - 1) << 16));46714672/* pipesrc and dspsize control the size that is scaled from,4673* which should always be the user's requested size.4674*/4675I915_WRITE(DSPSIZE(plane),4676((mode->vdisplay - 1) << 16) |4677(mode->hdisplay - 1));4678I915_WRITE(DSPPOS(plane), 0);4679I915_WRITE(PIPESRC(pipe),4680((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));46814682I915_WRITE(PIPECONF(pipe), pipeconf);4683POSTING_READ(PIPECONF(pipe));4684intel_enable_pipe(dev_priv, pipe, false);46854686intel_wait_for_vblank(dev, pipe);46874688I915_WRITE(DSPCNTR(plane), dspcntr);4689POSTING_READ(DSPCNTR(plane));4690intel_enable_plane(dev_priv, plane, pipe);46914692ret = intel_pipe_set_base(crtc, x, y, old_fb);46934694intel_update_watermarks(dev);46954696return ret;4697}46984699static int ironlake_crtc_mode_set(struct drm_crtc *crtc,4700struct drm_display_mode *mode,4701struct drm_display_mode *adjusted_mode,4702int x, int y,4703struct drm_framebuffer *old_fb)4704{4705struct drm_device *dev = crtc->dev;4706struct drm_i915_private *dev_priv = dev->dev_private;4707struct intel_crtc *intel_crtc = to_intel_crtc(crtc);4708int pipe = intel_crtc->pipe;4709int plane = intel_crtc->plane;4710int refclk, num_connectors = 0;4711intel_clock_t clock, reduced_clock;4712u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;4713bool ok, has_reduced_clock = false, is_sdvo = false;4714bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;4715struct intel_encoder *has_edp_encoder = NULL;4716struct drm_mode_config *mode_config = &dev->mode_config;4717struct intel_encoder *encoder;4718const intel_limit_t *limit;4719int ret;4720struct fdi_m_n m_n = {0};4721u32 temp;4722u32 lvds_sync = 0;4723int target_clock, pixel_multiplier, lane, link_bw, bpp, factor;47244725list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {4726if (encoder->base.crtc != crtc)4727continue;47284729switch (encoder->type) {4730case INTEL_OUTPUT_LVDS:4731is_lvds = true;4732break;4733case INTEL_OUTPUT_SDVO:4734case INTEL_OUTPUT_HDMI:4735is_sdvo = true;4736if (encoder->needs_tv_clock)4737is_tv = true;4738break;4739case INTEL_OUTPUT_TVOUT:4740is_tv = true;4741break;4742case INTEL_OUTPUT_ANALOG:4743is_crt = true;4744break;4745case INTEL_OUTPUT_DISPLAYPORT:4746is_dp = true;4747break;4748case INTEL_OUTPUT_EDP:4749has_edp_encoder = encoder;4750break;4751}47524753num_connectors++;4754}47554756if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {4757refclk = dev_priv->lvds_ssc_freq * 1000;4758DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",4759refclk / 1000);4760} else {4761refclk = 96000;4762if (!has_edp_encoder ||4763intel_encoder_is_pch_edp(&has_edp_encoder->base))4764refclk = 120000; /* 120Mhz refclk */4765}47664767/*4768* Returns a set of divisors for the desired target clock with the given4769* refclk, or FALSE. The returned values represent the clock equation:4770* reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.4771*/4772limit = intel_limit(crtc, refclk);4773ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);4774if (!ok) {4775DRM_ERROR("Couldn't find PLL settings for mode!\n");4776return -EINVAL;4777}47784779/* Ensure that the cursor is valid for the new mode before changing... */4780intel_crtc_update_cursor(crtc, true);47814782if (is_lvds && dev_priv->lvds_downclock_avail) {4783has_reduced_clock = limit->find_pll(limit, crtc,4784dev_priv->lvds_downclock,4785refclk,4786&reduced_clock);4787if (has_reduced_clock && (clock.p != reduced_clock.p)) {4788/*4789* If the different P is found, it means that we can't4790* switch the display clock by using the FP0/FP1.4791* In such case we will disable the LVDS downclock4792* feature.4793*/4794DRM_DEBUG_KMS("Different P is found for "4795"LVDS clock/downclock\n");4796has_reduced_clock = 0;4797}4798}4799/* SDVO TV has fixed PLL values depend on its clock range,4800this mirrors vbios setting. */4801if (is_sdvo && is_tv) {4802if (adjusted_mode->clock >= 1000004803&& adjusted_mode->clock < 140500) {4804clock.p1 = 2;4805clock.p2 = 10;4806clock.n = 3;4807clock.m1 = 16;4808clock.m2 = 8;4809} else if (adjusted_mode->clock >= 1405004810&& adjusted_mode->clock <= 200000) {4811clock.p1 = 1;4812clock.p2 = 10;4813clock.n = 6;4814clock.m1 = 12;4815clock.m2 = 8;4816}4817}48184819/* FDI link */4820pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);4821lane = 0;4822/* CPU eDP doesn't require FDI link, so just set DP M/N4823according to current link config */4824if (has_edp_encoder &&4825!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {4826target_clock = mode->clock;4827intel_edp_link_config(has_edp_encoder,4828&lane, &link_bw);4829} else {4830/* [e]DP over FDI requires target mode clock4831instead of link clock */4832if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))4833target_clock = mode->clock;4834else4835target_clock = adjusted_mode->clock;48364837/* FDI is a binary signal running at ~2.7GHz, encoding4838* each output octet as 10 bits. The actual frequency4839* is stored as a divider into a 100MHz clock, and the4840* mode pixel clock is stored in units of 1KHz.4841* Hence the bw of each lane in terms of the mode signal4842* is:4843*/4844link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;4845}48464847/* determine panel color depth */4848temp = I915_READ(PIPECONF(pipe));4849temp &= ~PIPE_BPC_MASK;4850if (is_lvds) {4851/* the BPC will be 6 if it is 18-bit LVDS panel */4852if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)4853temp |= PIPE_8BPC;4854else4855temp |= PIPE_6BPC;4856} else if (has_edp_encoder) {4857switch (dev_priv->edp.bpp/3) {4858case 8:4859temp |= PIPE_8BPC;4860break;4861case 10:4862temp |= PIPE_10BPC;4863break;4864case 6:4865temp |= PIPE_6BPC;4866break;4867case 12:4868temp |= PIPE_12BPC;4869break;4870}4871} else4872temp |= PIPE_8BPC;4873I915_WRITE(PIPECONF(pipe), temp);48744875switch (temp & PIPE_BPC_MASK) {4876case PIPE_8BPC:4877bpp = 24;4878break;4879case PIPE_10BPC:4880bpp = 30;4881break;4882case PIPE_6BPC:4883bpp = 18;4884break;4885case PIPE_12BPC:4886bpp = 36;4887break;4888default:4889DRM_ERROR("unknown pipe bpc value\n");4890bpp = 24;4891}48924893if (!lane) {4894/*4895* Account for spread spectrum to avoid4896* oversubscribing the link. Max center spread4897* is 2.5%; use 5% for safety's sake.4898*/4899u32 bps = target_clock * bpp * 21 / 20;4900lane = bps / (link_bw * 8) + 1;4901}49024903intel_crtc->fdi_lanes = lane;49044905if (pixel_multiplier > 1)4906link_bw *= pixel_multiplier;4907ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);49084909/* Ironlake: try to setup display ref clock before DPLL4910* enabling. This is only under driver's control after4911* PCH B stepping, previous chipset stepping should be4912* ignoring this setting.4913*/4914temp = I915_READ(PCH_DREF_CONTROL);4915/* Always enable nonspread source */4916temp &= ~DREF_NONSPREAD_SOURCE_MASK;4917temp |= DREF_NONSPREAD_SOURCE_ENABLE;4918temp &= ~DREF_SSC_SOURCE_MASK;4919temp |= DREF_SSC_SOURCE_ENABLE;4920I915_WRITE(PCH_DREF_CONTROL, temp);49214922POSTING_READ(PCH_DREF_CONTROL);4923udelay(200);49244925if (has_edp_encoder) {4926if (intel_panel_use_ssc(dev_priv)) {4927temp |= DREF_SSC1_ENABLE;4928I915_WRITE(PCH_DREF_CONTROL, temp);49294930POSTING_READ(PCH_DREF_CONTROL);4931udelay(200);4932}4933temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;49344935/* Enable CPU source on CPU attached eDP */4936if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {4937if (intel_panel_use_ssc(dev_priv))4938temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;4939else4940temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;4941} else {4942/* Enable SSC on PCH eDP if needed */4943if (intel_panel_use_ssc(dev_priv)) {4944DRM_ERROR("enabling SSC on PCH\n");4945temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;4946}4947}4948I915_WRITE(PCH_DREF_CONTROL, temp);4949POSTING_READ(PCH_DREF_CONTROL);4950udelay(200);4951}49524953fp = clock.n << 16 | clock.m1 << 8 | clock.m2;4954if (has_reduced_clock)4955fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |4956reduced_clock.m2;49574958/* Enable autotuning of the PLL clock (if permissible) */4959factor = 21;4960if (is_lvds) {4961if ((intel_panel_use_ssc(dev_priv) &&4962dev_priv->lvds_ssc_freq == 100) ||4963(I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)4964factor = 25;4965} else if (is_sdvo && is_tv)4966factor = 20;49674968if (clock.m1 < factor * clock.n)4969fp |= FP_CB_TUNE;49704971dpll = 0;49724973if (is_lvds)4974dpll |= DPLLB_MODE_LVDS;4975else4976dpll |= DPLLB_MODE_DAC_SERIAL;4977if (is_sdvo) {4978int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);4979if (pixel_multiplier > 1) {4980dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;4981}4982dpll |= DPLL_DVO_HIGH_SPEED;4983}4984if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))4985dpll |= DPLL_DVO_HIGH_SPEED;49864987/* compute bitmask from p1 value */4988dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;4989/* also FPA1 */4990dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;49914992switch (clock.p2) {4993case 5:4994dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;4995break;4996case 7:4997dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;4998break;4999case 10:5000dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;5001break;5002case 14:5003dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;5004break;5005}50065007if (is_sdvo && is_tv)5008dpll |= PLL_REF_INPUT_TVCLKINBC;5009else if (is_tv)5010/* XXX: just matching BIOS for now */5011/* dpll |= PLL_REF_INPUT_TVCLKINBC; */5012dpll |= 3;5013else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)5014dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;5015else5016dpll |= PLL_REF_INPUT_DREFCLK;50175018/* setup pipeconf */5019pipeconf = I915_READ(PIPECONF(pipe));50205021/* Set up the display plane register */5022dspcntr = DISPPLANE_GAMMA_ENABLE;50235024DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');5025drm_mode_debug_printmodeline(mode);50265027/* PCH eDP needs FDI, but CPU eDP does not */5028if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {5029I915_WRITE(PCH_FP0(pipe), fp);5030I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);50315032POSTING_READ(PCH_DPLL(pipe));5033udelay(150);5034}50355036/* enable transcoder DPLL */5037if (HAS_PCH_CPT(dev)) {5038temp = I915_READ(PCH_DPLL_SEL);5039switch (pipe) {5040case 0:5041temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;5042break;5043case 1:5044temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;5045break;5046case 2:5047/* FIXME: manage transcoder PLLs? */5048temp |= TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL;5049break;5050default:5051BUG();5052}5053I915_WRITE(PCH_DPLL_SEL, temp);50545055POSTING_READ(PCH_DPLL_SEL);5056udelay(150);5057}50585059/* The LVDS pin pair needs to be on before the DPLLs are enabled.5060* This is an exception to the general rule that mode_set doesn't turn5061* things on.5062*/5063if (is_lvds) {5064temp = I915_READ(PCH_LVDS);5065temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;5066if (pipe == 1) {5067if (HAS_PCH_CPT(dev))5068temp |= PORT_TRANS_B_SEL_CPT;5069else5070temp |= LVDS_PIPEB_SELECT;5071} else {5072if (HAS_PCH_CPT(dev))5073temp &= ~PORT_TRANS_SEL_MASK;5074else5075temp &= ~LVDS_PIPEB_SELECT;5076}5077/* set the corresponsding LVDS_BORDER bit */5078temp |= dev_priv->lvds_border_bits;5079/* Set the B0-B3 data pairs corresponding to whether we're going to5080* set the DPLLs for dual-channel mode or not.5081*/5082if (clock.p2 == 7)5083temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;5084else5085temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);50865087/* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)5088* appropriately here, but we need to look more thoroughly into how5089* panels behave in the two modes.5090*/5091if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)5092lvds_sync |= LVDS_HSYNC_POLARITY;5093if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)5094lvds_sync |= LVDS_VSYNC_POLARITY;5095if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))5096!= lvds_sync) {5097char flags[2] = "-+";5098DRM_INFO("Changing LVDS panel from "5099"(%chsync, %cvsync) to (%chsync, %cvsync)\n",5100flags[!(temp & LVDS_HSYNC_POLARITY)],5101flags[!(temp & LVDS_VSYNC_POLARITY)],5102flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],5103flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);5104temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);5105temp |= lvds_sync;5106}5107I915_WRITE(PCH_LVDS, temp);5108}51095110/* set the dithering flag and clear for anything other than a panel. */5111pipeconf &= ~PIPECONF_DITHER_EN;5112pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;5113if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {5114pipeconf |= PIPECONF_DITHER_EN;5115pipeconf |= PIPECONF_DITHER_TYPE_ST1;5116}51175118if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {5119intel_dp_set_m_n(crtc, mode, adjusted_mode);5120} else {5121/* For non-DP output, clear any trans DP clock recovery setting.*/5122I915_WRITE(TRANSDATA_M1(pipe), 0);5123I915_WRITE(TRANSDATA_N1(pipe), 0);5124I915_WRITE(TRANSDPLINK_M1(pipe), 0);5125I915_WRITE(TRANSDPLINK_N1(pipe), 0);5126}51275128if (!has_edp_encoder ||5129intel_encoder_is_pch_edp(&has_edp_encoder->base)) {5130I915_WRITE(PCH_DPLL(pipe), dpll);51315132/* Wait for the clocks to stabilize. */5133POSTING_READ(PCH_DPLL(pipe));5134udelay(150);51355136/* The pixel multiplier can only be updated once the5137* DPLL is enabled and the clocks are stable.5138*5139* So write it again.5140*/5141I915_WRITE(PCH_DPLL(pipe), dpll);5142}51435144intel_crtc->lowfreq_avail = false;5145if (is_lvds && has_reduced_clock && i915_powersave) {5146I915_WRITE(PCH_FP1(pipe), fp2);5147intel_crtc->lowfreq_avail = true;5148if (HAS_PIPE_CXSR(dev)) {5149DRM_DEBUG_KMS("enabling CxSR downclocking\n");5150pipeconf |= PIPECONF_CXSR_DOWNCLOCK;5151}5152} else {5153I915_WRITE(PCH_FP1(pipe), fp);5154if (HAS_PIPE_CXSR(dev)) {5155DRM_DEBUG_KMS("disabling CxSR downclocking\n");5156pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;5157}5158}51595160if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {5161pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;5162/* the chip adds 2 halflines automatically */5163adjusted_mode->crtc_vdisplay -= 1;5164adjusted_mode->crtc_vtotal -= 1;5165adjusted_mode->crtc_vblank_start -= 1;5166adjusted_mode->crtc_vblank_end -= 1;5167adjusted_mode->crtc_vsync_end -= 1;5168adjusted_mode->crtc_vsync_start -= 1;5169} else5170pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */51715172I915_WRITE(HTOTAL(pipe),5173(adjusted_mode->crtc_hdisplay - 1) |5174((adjusted_mode->crtc_htotal - 1) << 16));5175I915_WRITE(HBLANK(pipe),5176(adjusted_mode->crtc_hblank_start - 1) |5177((adjusted_mode->crtc_hblank_end - 1) << 16));5178I915_WRITE(HSYNC(pipe),5179(adjusted_mode->crtc_hsync_start - 1) |5180((adjusted_mode->crtc_hsync_end - 1) << 16));51815182I915_WRITE(VTOTAL(pipe),5183(adjusted_mode->crtc_vdisplay - 1) |5184((adjusted_mode->crtc_vtotal - 1) << 16));5185I915_WRITE(VBLANK(pipe),5186(adjusted_mode->crtc_vblank_start - 1) |5187((adjusted_mode->crtc_vblank_end - 1) << 16));5188I915_WRITE(VSYNC(pipe),5189(adjusted_mode->crtc_vsync_start - 1) |5190((adjusted_mode->crtc_vsync_end - 1) << 16));51915192/* pipesrc controls the size that is scaled from, which should5193* always be the user's requested size.5194*/5195I915_WRITE(PIPESRC(pipe),5196((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));51975198I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);5199I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);5200I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);5201I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);52025203if (has_edp_encoder &&5204!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {5205ironlake_set_pll_edp(crtc, adjusted_mode->clock);5206}52075208I915_WRITE(PIPECONF(pipe), pipeconf);5209POSTING_READ(PIPECONF(pipe));52105211intel_wait_for_vblank(dev, pipe);52125213if (IS_GEN5(dev)) {5214/* enable address swizzle for tiling buffer */5215temp = I915_READ(DISP_ARB_CTL);5216I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);5217}52185219I915_WRITE(DSPCNTR(plane), dspcntr);5220POSTING_READ(DSPCNTR(plane));52215222ret = intel_pipe_set_base(crtc, x, y, old_fb);52235224intel_update_watermarks(dev);52255226return ret;5227}52285229static int intel_crtc_mode_set(struct drm_crtc *crtc,5230struct drm_display_mode *mode,5231struct drm_display_mode *adjusted_mode,5232int x, int y,5233struct drm_framebuffer *old_fb)5234{5235struct drm_device *dev = crtc->dev;5236struct drm_i915_private *dev_priv = dev->dev_private;5237struct intel_crtc *intel_crtc = to_intel_crtc(crtc);5238int pipe = intel_crtc->pipe;5239int ret;52405241drm_vblank_pre_modeset(dev, pipe);52425243ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,5244x, y, old_fb);52455246drm_vblank_post_modeset(dev, pipe);52475248return ret;5249}52505251/** Loads the palette/gamma unit for the CRTC with the prepared values */5252void intel_crtc_load_lut(struct drm_crtc *crtc)5253{5254struct drm_device *dev = crtc->dev;5255struct drm_i915_private *dev_priv = dev->dev_private;5256struct intel_crtc *intel_crtc = to_intel_crtc(crtc);5257int palreg = PALETTE(intel_crtc->pipe);5258int i;52595260/* The clocks have to be on to load the palette. */5261if (!crtc->enabled)5262return;52635264/* use legacy palette for Ironlake */5265if (HAS_PCH_SPLIT(dev))5266palreg = LGC_PALETTE(intel_crtc->pipe);52675268for (i = 0; i < 256; i++) {5269I915_WRITE(palreg + 4 * i,5270(intel_crtc->lut_r[i] << 16) |5271(intel_crtc->lut_g[i] << 8) |5272intel_crtc->lut_b[i]);5273}5274}52755276static void i845_update_cursor(struct drm_crtc *crtc, u32 base)5277{5278struct drm_device *dev = crtc->dev;5279struct drm_i915_private *dev_priv = dev->dev_private;5280struct intel_crtc *intel_crtc = to_intel_crtc(crtc);5281bool visible = base != 0;5282u32 cntl;52835284if (intel_crtc->cursor_visible == visible)5285return;52865287cntl = I915_READ(_CURACNTR);5288if (visible) {5289/* On these chipsets we can only modify the base whilst5290* the cursor is disabled.5291*/5292I915_WRITE(_CURABASE, base);52935294cntl &= ~(CURSOR_FORMAT_MASK);5295/* XXX width must be 64, stride 256 => 0x00 << 28 */5296cntl |= CURSOR_ENABLE |5297CURSOR_GAMMA_ENABLE |5298CURSOR_FORMAT_ARGB;5299} else5300cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);5301I915_WRITE(_CURACNTR, cntl);53025303intel_crtc->cursor_visible = visible;5304}53055306static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)5307{5308struct drm_device *dev = crtc->dev;5309struct drm_i915_private *dev_priv = dev->dev_private;5310struct intel_crtc *intel_crtc = to_intel_crtc(crtc);5311int pipe = intel_crtc->pipe;5312bool visible = base != 0;53135314if (intel_crtc->cursor_visible != visible) {5315uint32_t cntl = I915_READ(CURCNTR(pipe));5316if (base) {5317cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);5318cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;5319cntl |= pipe << 28; /* Connect to correct pipe */5320} else {5321cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);5322cntl |= CURSOR_MODE_DISABLE;5323}5324I915_WRITE(CURCNTR(pipe), cntl);53255326intel_crtc->cursor_visible = visible;5327}5328/* and commit changes on next vblank */5329I915_WRITE(CURBASE(pipe), base);5330}53315332/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */5333static void intel_crtc_update_cursor(struct drm_crtc *crtc,5334bool on)5335{5336struct drm_device *dev = crtc->dev;5337struct drm_i915_private *dev_priv = dev->dev_private;5338struct intel_crtc *intel_crtc = to_intel_crtc(crtc);5339int pipe = intel_crtc->pipe;5340int x = intel_crtc->cursor_x;5341int y = intel_crtc->cursor_y;5342u32 base, pos;5343bool visible;53445345pos = 0;53465347if (on && crtc->enabled && crtc->fb) {5348base = intel_crtc->cursor_addr;5349if (x > (int) crtc->fb->width)5350base = 0;53515352if (y > (int) crtc->fb->height)5353base = 0;5354} else5355base = 0;53565357if (x < 0) {5358if (x + intel_crtc->cursor_width < 0)5359base = 0;53605361pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;5362x = -x;5363}5364pos |= x << CURSOR_X_SHIFT;53655366if (y < 0) {5367if (y + intel_crtc->cursor_height < 0)5368base = 0;53695370pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;5371y = -y;5372}5373pos |= y << CURSOR_Y_SHIFT;53745375visible = base != 0;5376if (!visible && !intel_crtc->cursor_visible)5377return;53785379I915_WRITE(CURPOS(pipe), pos);5380if (IS_845G(dev) || IS_I865G(dev))5381i845_update_cursor(crtc, base);5382else5383i9xx_update_cursor(crtc, base);53845385if (visible)5386intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);5387}53885389static int intel_crtc_cursor_set(struct drm_crtc *crtc,5390struct drm_file *file,5391uint32_t handle,5392uint32_t width, uint32_t height)5393{5394struct drm_device *dev = crtc->dev;5395struct drm_i915_private *dev_priv = dev->dev_private;5396struct intel_crtc *intel_crtc = to_intel_crtc(crtc);5397struct drm_i915_gem_object *obj;5398uint32_t addr;5399int ret;54005401DRM_DEBUG_KMS("\n");54025403/* if we want to turn off the cursor ignore width and height */5404if (!handle) {5405DRM_DEBUG_KMS("cursor off\n");5406addr = 0;5407obj = NULL;5408mutex_lock(&dev->struct_mutex);5409goto finish;5410}54115412/* Currently we only support 64x64 cursors */5413if (width != 64 || height != 64) {5414DRM_ERROR("we currently only support 64x64 cursors\n");5415return -EINVAL;5416}54175418obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));5419if (&obj->base == NULL)5420return -ENOENT;54215422if (obj->base.size < width * height * 4) {5423DRM_ERROR("buffer is to small\n");5424ret = -ENOMEM;5425goto fail;5426}54275428/* we only need to pin inside GTT if cursor is non-phy */5429mutex_lock(&dev->struct_mutex);5430if (!dev_priv->info->cursor_needs_physical) {5431if (obj->tiling_mode) {5432DRM_ERROR("cursor cannot be tiled\n");5433ret = -EINVAL;5434goto fail_locked;5435}54365437ret = i915_gem_object_pin(obj, PAGE_SIZE, true);5438if (ret) {5439DRM_ERROR("failed to pin cursor bo\n");5440goto fail_locked;5441}54425443ret = i915_gem_object_set_to_gtt_domain(obj, 0);5444if (ret) {5445DRM_ERROR("failed to move cursor bo into the GTT\n");5446goto fail_unpin;5447}54485449ret = i915_gem_object_put_fence(obj);5450if (ret) {5451DRM_ERROR("failed to move cursor bo into the GTT\n");5452goto fail_unpin;5453}54545455addr = obj->gtt_offset;5456} else {5457int align = IS_I830(dev) ? 16 * 1024 : 256;5458ret = i915_gem_attach_phys_object(dev, obj,5459(intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,5460align);5461if (ret) {5462DRM_ERROR("failed to attach phys object\n");5463goto fail_locked;5464}5465addr = obj->phys_obj->handle->busaddr;5466}54675468if (IS_GEN2(dev))5469I915_WRITE(CURSIZE, (height << 12) | width);54705471finish:5472if (intel_crtc->cursor_bo) {5473if (dev_priv->info->cursor_needs_physical) {5474if (intel_crtc->cursor_bo != obj)5475i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);5476} else5477i915_gem_object_unpin(intel_crtc->cursor_bo);5478drm_gem_object_unreference(&intel_crtc->cursor_bo->base);5479}54805481mutex_unlock(&dev->struct_mutex);54825483intel_crtc->cursor_addr = addr;5484intel_crtc->cursor_bo = obj;5485intel_crtc->cursor_width = width;5486intel_crtc->cursor_height = height;54875488intel_crtc_update_cursor(crtc, true);54895490return 0;5491fail_unpin:5492i915_gem_object_unpin(obj);5493fail_locked:5494mutex_unlock(&dev->struct_mutex);5495fail:5496drm_gem_object_unreference_unlocked(&obj->base);5497return ret;5498}54995500static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)5501{5502struct intel_crtc *intel_crtc = to_intel_crtc(crtc);55035504intel_crtc->cursor_x = x;5505intel_crtc->cursor_y = y;55065507intel_crtc_update_cursor(crtc, true);55085509return 0;5510}55115512/** Sets the color ramps on behalf of RandR */5513void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,5514u16 blue, int regno)5515{5516struct intel_crtc *intel_crtc = to_intel_crtc(crtc);55175518intel_crtc->lut_r[regno] = red >> 8;5519intel_crtc->lut_g[regno] = green >> 8;5520intel_crtc->lut_b[regno] = blue >> 8;5521}55225523void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,5524u16 *blue, int regno)5525{5526struct intel_crtc *intel_crtc = to_intel_crtc(crtc);55275528*red = intel_crtc->lut_r[regno] << 8;5529*green = intel_crtc->lut_g[regno] << 8;5530*blue = intel_crtc->lut_b[regno] << 8;5531}55325533static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,5534u16 *blue, uint32_t start, uint32_t size)5535{5536int end = (start + size > 256) ? 256 : start + size, i;5537struct intel_crtc *intel_crtc = to_intel_crtc(crtc);55385539for (i = start; i < end; i++) {5540intel_crtc->lut_r[i] = red[i] >> 8;5541intel_crtc->lut_g[i] = green[i] >> 8;5542intel_crtc->lut_b[i] = blue[i] >> 8;5543}55445545intel_crtc_load_lut(crtc);5546}55475548/**5549* Get a pipe with a simple mode set on it for doing load-based monitor5550* detection.5551*5552* It will be up to the load-detect code to adjust the pipe as appropriate for5553* its requirements. The pipe will be connected to no other encoders.5554*5555* Currently this code will only succeed if there is a pipe with no encoders5556* configured for it. In the future, it could choose to temporarily disable5557* some outputs to free up a pipe for its use.5558*5559* \return crtc, or NULL if no pipes are available.5560*/55615562/* VESA 640x480x72Hz mode to set on the pipe */5563static struct drm_display_mode load_detect_mode = {5564DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,5565704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),5566};55675568static struct drm_framebuffer *5569intel_framebuffer_create(struct drm_device *dev,5570struct drm_mode_fb_cmd *mode_cmd,5571struct drm_i915_gem_object *obj)5572{5573struct intel_framebuffer *intel_fb;5574int ret;55755576intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);5577if (!intel_fb) {5578drm_gem_object_unreference_unlocked(&obj->base);5579return ERR_PTR(-ENOMEM);5580}55815582ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);5583if (ret) {5584drm_gem_object_unreference_unlocked(&obj->base);5585kfree(intel_fb);5586return ERR_PTR(ret);5587}55885589return &intel_fb->base;5590}55915592static u325593intel_framebuffer_pitch_for_width(int width, int bpp)5594{5595u32 pitch = DIV_ROUND_UP(width * bpp, 8);5596return ALIGN(pitch, 64);5597}55985599static u325600intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)5601{5602u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);5603return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);5604}56055606static struct drm_framebuffer *5607intel_framebuffer_create_for_mode(struct drm_device *dev,5608struct drm_display_mode *mode,5609int depth, int bpp)5610{5611struct drm_i915_gem_object *obj;5612struct drm_mode_fb_cmd mode_cmd;56135614obj = i915_gem_alloc_object(dev,5615intel_framebuffer_size_for_mode(mode, bpp));5616if (obj == NULL)5617return ERR_PTR(-ENOMEM);56185619mode_cmd.width = mode->hdisplay;5620mode_cmd.height = mode->vdisplay;5621mode_cmd.depth = depth;5622mode_cmd.bpp = bpp;5623mode_cmd.pitch = intel_framebuffer_pitch_for_width(mode_cmd.width, bpp);56245625return intel_framebuffer_create(dev, &mode_cmd, obj);5626}56275628static struct drm_framebuffer *5629mode_fits_in_fbdev(struct drm_device *dev,5630struct drm_display_mode *mode)5631{5632struct drm_i915_private *dev_priv = dev->dev_private;5633struct drm_i915_gem_object *obj;5634struct drm_framebuffer *fb;56355636if (dev_priv->fbdev == NULL)5637return NULL;56385639obj = dev_priv->fbdev->ifb.obj;5640if (obj == NULL)5641return NULL;56425643fb = &dev_priv->fbdev->ifb.base;5644if (fb->pitch < intel_framebuffer_pitch_for_width(mode->hdisplay,5645fb->bits_per_pixel))5646return NULL;56475648if (obj->base.size < mode->vdisplay * fb->pitch)5649return NULL;56505651return fb;5652}56535654bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,5655struct drm_connector *connector,5656struct drm_display_mode *mode,5657struct intel_load_detect_pipe *old)5658{5659struct intel_crtc *intel_crtc;5660struct drm_crtc *possible_crtc;5661struct drm_encoder *encoder = &intel_encoder->base;5662struct drm_crtc *crtc = NULL;5663struct drm_device *dev = encoder->dev;5664struct drm_framebuffer *old_fb;5665int i = -1;56665667DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",5668connector->base.id, drm_get_connector_name(connector),5669encoder->base.id, drm_get_encoder_name(encoder));56705671/*5672* Algorithm gets a little messy:5673*5674* - if the connector already has an assigned crtc, use it (but make5675* sure it's on first)5676*5677* - try to find the first unused crtc that can drive this connector,5678* and use that if we find one5679*/56805681/* See if we already have a CRTC for this connector */5682if (encoder->crtc) {5683crtc = encoder->crtc;56845685intel_crtc = to_intel_crtc(crtc);5686old->dpms_mode = intel_crtc->dpms_mode;5687old->load_detect_temp = false;56885689/* Make sure the crtc and connector are running */5690if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {5691struct drm_encoder_helper_funcs *encoder_funcs;5692struct drm_crtc_helper_funcs *crtc_funcs;56935694crtc_funcs = crtc->helper_private;5695crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);56965697encoder_funcs = encoder->helper_private;5698encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);5699}57005701return true;5702}57035704/* Find an unused one (if possible) */5705list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {5706i++;5707if (!(encoder->possible_crtcs & (1 << i)))5708continue;5709if (!possible_crtc->enabled) {5710crtc = possible_crtc;5711break;5712}5713}57145715/*5716* If we didn't find an unused CRTC, don't use any.5717*/5718if (!crtc) {5719DRM_DEBUG_KMS("no pipe available for load-detect\n");5720return false;5721}57225723encoder->crtc = crtc;5724connector->encoder = encoder;57255726intel_crtc = to_intel_crtc(crtc);5727old->dpms_mode = intel_crtc->dpms_mode;5728old->load_detect_temp = true;5729old->release_fb = NULL;57305731if (!mode)5732mode = &load_detect_mode;57335734old_fb = crtc->fb;57355736/* We need a framebuffer large enough to accommodate all accesses5737* that the plane may generate whilst we perform load detection.5738* We can not rely on the fbcon either being present (we get called5739* during its initialisation to detect all boot displays, or it may5740* not even exist) or that it is large enough to satisfy the5741* requested mode.5742*/5743crtc->fb = mode_fits_in_fbdev(dev, mode);5744if (crtc->fb == NULL) {5745DRM_DEBUG_KMS("creating tmp fb for load-detection\n");5746crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);5747old->release_fb = crtc->fb;5748} else5749DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");5750if (IS_ERR(crtc->fb)) {5751DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");5752crtc->fb = old_fb;5753return false;5754}57555756if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {5757DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");5758if (old->release_fb)5759old->release_fb->funcs->destroy(old->release_fb);5760crtc->fb = old_fb;5761return false;5762}57635764/* let the connector get through one full cycle before testing */5765intel_wait_for_vblank(dev, intel_crtc->pipe);57665767return true;5768}57695770void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,5771struct drm_connector *connector,5772struct intel_load_detect_pipe *old)5773{5774struct drm_encoder *encoder = &intel_encoder->base;5775struct drm_device *dev = encoder->dev;5776struct drm_crtc *crtc = encoder->crtc;5777struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;5778struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;57795780DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",5781connector->base.id, drm_get_connector_name(connector),5782encoder->base.id, drm_get_encoder_name(encoder));57835784if (old->load_detect_temp) {5785connector->encoder = NULL;5786drm_helper_disable_unused_functions(dev);57875788if (old->release_fb)5789old->release_fb->funcs->destroy(old->release_fb);57905791return;5792}57935794/* Switch crtc and encoder back off if necessary */5795if (old->dpms_mode != DRM_MODE_DPMS_ON) {5796encoder_funcs->dpms(encoder, old->dpms_mode);5797crtc_funcs->dpms(crtc, old->dpms_mode);5798}5799}58005801/* Returns the clock of the currently programmed mode of the given pipe. */5802static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)5803{5804struct drm_i915_private *dev_priv = dev->dev_private;5805struct intel_crtc *intel_crtc = to_intel_crtc(crtc);5806int pipe = intel_crtc->pipe;5807u32 dpll = I915_READ(DPLL(pipe));5808u32 fp;5809intel_clock_t clock;58105811if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)5812fp = I915_READ(FP0(pipe));5813else5814fp = I915_READ(FP1(pipe));58155816clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;5817if (IS_PINEVIEW(dev)) {5818clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;5819clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;5820} else {5821clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;5822clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;5823}58245825if (!IS_GEN2(dev)) {5826if (IS_PINEVIEW(dev))5827clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>5828DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);5829else5830clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>5831DPLL_FPA01_P1_POST_DIV_SHIFT);58325833switch (dpll & DPLL_MODE_MASK) {5834case DPLLB_MODE_DAC_SERIAL:5835clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?58365 : 10;5837break;5838case DPLLB_MODE_LVDS:5839clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?58407 : 14;5841break;5842default:5843DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "5844"mode\n", (int)(dpll & DPLL_MODE_MASK));5845return 0;5846}58475848/* XXX: Handle the 100Mhz refclk */5849intel_clock(dev, 96000, &clock);5850} else {5851bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);58525853if (is_lvds) {5854clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>5855DPLL_FPA01_P1_POST_DIV_SHIFT);5856clock.p2 = 14;58575858if ((dpll & PLL_REF_INPUT_MASK) ==5859PLLB_REF_INPUT_SPREADSPECTRUMIN) {5860/* XXX: might not be 66MHz */5861intel_clock(dev, 66000, &clock);5862} else5863intel_clock(dev, 48000, &clock);5864} else {5865if (dpll & PLL_P1_DIVIDE_BY_TWO)5866clock.p1 = 2;5867else {5868clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>5869DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;5870}5871if (dpll & PLL_P2_DIVIDE_BY_4)5872clock.p2 = 4;5873else5874clock.p2 = 2;58755876intel_clock(dev, 48000, &clock);5877}5878}58795880/* XXX: It would be nice to validate the clocks, but we can't reuse5881* i830PllIsValid() because it relies on the xf86_config connector5882* configuration being accurate, which it isn't necessarily.5883*/58845885return clock.dot;5886}58875888/** Returns the currently programmed mode of the given pipe. */5889struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,5890struct drm_crtc *crtc)5891{5892struct drm_i915_private *dev_priv = dev->dev_private;5893struct intel_crtc *intel_crtc = to_intel_crtc(crtc);5894int pipe = intel_crtc->pipe;5895struct drm_display_mode *mode;5896int htot = I915_READ(HTOTAL(pipe));5897int hsync = I915_READ(HSYNC(pipe));5898int vtot = I915_READ(VTOTAL(pipe));5899int vsync = I915_READ(VSYNC(pipe));59005901mode = kzalloc(sizeof(*mode), GFP_KERNEL);5902if (!mode)5903return NULL;59045905mode->clock = intel_crtc_clock_get(dev, crtc);5906mode->hdisplay = (htot & 0xffff) + 1;5907mode->htotal = ((htot & 0xffff0000) >> 16) + 1;5908mode->hsync_start = (hsync & 0xffff) + 1;5909mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;5910mode->vdisplay = (vtot & 0xffff) + 1;5911mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;5912mode->vsync_start = (vsync & 0xffff) + 1;5913mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;59145915drm_mode_set_name(mode);5916drm_mode_set_crtcinfo(mode, 0);59175918return mode;5919}59205921#define GPU_IDLE_TIMEOUT 500 /* ms */59225923/* When this timer fires, we've been idle for awhile */5924static void intel_gpu_idle_timer(unsigned long arg)5925{5926struct drm_device *dev = (struct drm_device *)arg;5927drm_i915_private_t *dev_priv = dev->dev_private;59285929if (!list_empty(&dev_priv->mm.active_list)) {5930/* Still processing requests, so just re-arm the timer. */5931mod_timer(&dev_priv->idle_timer, jiffies +5932msecs_to_jiffies(GPU_IDLE_TIMEOUT));5933return;5934}59355936dev_priv->busy = false;5937queue_work(dev_priv->wq, &dev_priv->idle_work);5938}59395940#define CRTC_IDLE_TIMEOUT 1000 /* ms */59415942static void intel_crtc_idle_timer(unsigned long arg)5943{5944struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;5945struct drm_crtc *crtc = &intel_crtc->base;5946drm_i915_private_t *dev_priv = crtc->dev->dev_private;5947struct intel_framebuffer *intel_fb;59485949intel_fb = to_intel_framebuffer(crtc->fb);5950if (intel_fb && intel_fb->obj->active) {5951/* The framebuffer is still being accessed by the GPU. */5952mod_timer(&intel_crtc->idle_timer, jiffies +5953msecs_to_jiffies(CRTC_IDLE_TIMEOUT));5954return;5955}59565957intel_crtc->busy = false;5958queue_work(dev_priv->wq, &dev_priv->idle_work);5959}59605961static void intel_increase_pllclock(struct drm_crtc *crtc)5962{5963struct drm_device *dev = crtc->dev;5964drm_i915_private_t *dev_priv = dev->dev_private;5965struct intel_crtc *intel_crtc = to_intel_crtc(crtc);5966int pipe = intel_crtc->pipe;5967int dpll_reg = DPLL(pipe);5968int dpll;59695970if (HAS_PCH_SPLIT(dev))5971return;59725973if (!dev_priv->lvds_downclock_avail)5974return;59755976dpll = I915_READ(dpll_reg);5977if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {5978DRM_DEBUG_DRIVER("upclocking LVDS\n");59795980/* Unlock panel regs */5981I915_WRITE(PP_CONTROL,5982I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);59835984dpll &= ~DISPLAY_RATE_SELECT_FPA1;5985I915_WRITE(dpll_reg, dpll);5986intel_wait_for_vblank(dev, pipe);59875988dpll = I915_READ(dpll_reg);5989if (dpll & DISPLAY_RATE_SELECT_FPA1)5990DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");59915992/* ...and lock them again */5993I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);5994}59955996/* Schedule downclock */5997mod_timer(&intel_crtc->idle_timer, jiffies +5998msecs_to_jiffies(CRTC_IDLE_TIMEOUT));5999}60006001static void intel_decrease_pllclock(struct drm_crtc *crtc)6002{6003struct drm_device *dev = crtc->dev;6004drm_i915_private_t *dev_priv = dev->dev_private;6005struct intel_crtc *intel_crtc = to_intel_crtc(crtc);6006int pipe = intel_crtc->pipe;6007int dpll_reg = DPLL(pipe);6008int dpll = I915_READ(dpll_reg);60096010if (HAS_PCH_SPLIT(dev))6011return;60126013if (!dev_priv->lvds_downclock_avail)6014return;60156016/*6017* Since this is called by a timer, we should never get here in6018* the manual case.6019*/6020if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {6021DRM_DEBUG_DRIVER("downclocking LVDS\n");60226023/* Unlock panel regs */6024I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |6025PANEL_UNLOCK_REGS);60266027dpll |= DISPLAY_RATE_SELECT_FPA1;6028I915_WRITE(dpll_reg, dpll);6029intel_wait_for_vblank(dev, pipe);6030dpll = I915_READ(dpll_reg);6031if (!(dpll & DISPLAY_RATE_SELECT_FPA1))6032DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");60336034/* ...and lock them again */6035I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);6036}60376038}60396040/**6041* intel_idle_update - adjust clocks for idleness6042* @work: work struct6043*6044* Either the GPU or display (or both) went idle. Check the busy status6045* here and adjust the CRTC and GPU clocks as necessary.6046*/6047static void intel_idle_update(struct work_struct *work)6048{6049drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,6050idle_work);6051struct drm_device *dev = dev_priv->dev;6052struct drm_crtc *crtc;6053struct intel_crtc *intel_crtc;60546055if (!i915_powersave)6056return;60576058mutex_lock(&dev->struct_mutex);60596060i915_update_gfx_val(dev_priv);60616062list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {6063/* Skip inactive CRTCs */6064if (!crtc->fb)6065continue;60666067intel_crtc = to_intel_crtc(crtc);6068if (!intel_crtc->busy)6069intel_decrease_pllclock(crtc);6070}607160726073mutex_unlock(&dev->struct_mutex);6074}60756076/**6077* intel_mark_busy - mark the GPU and possibly the display busy6078* @dev: drm device6079* @obj: object we're operating on6080*6081* Callers can use this function to indicate that the GPU is busy processing6082* commands. If @obj matches one of the CRTC objects (i.e. it's a scanout6083* buffer), we'll also mark the display as busy, so we know to increase its6084* clock frequency.6085*/6086void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)6087{6088drm_i915_private_t *dev_priv = dev->dev_private;6089struct drm_crtc *crtc = NULL;6090struct intel_framebuffer *intel_fb;6091struct intel_crtc *intel_crtc;60926093if (!drm_core_check_feature(dev, DRIVER_MODESET))6094return;60956096if (!dev_priv->busy)6097dev_priv->busy = true;6098else6099mod_timer(&dev_priv->idle_timer, jiffies +6100msecs_to_jiffies(GPU_IDLE_TIMEOUT));61016102list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {6103if (!crtc->fb)6104continue;61056106intel_crtc = to_intel_crtc(crtc);6107intel_fb = to_intel_framebuffer(crtc->fb);6108if (intel_fb->obj == obj) {6109if (!intel_crtc->busy) {6110/* Non-busy -> busy, upclock */6111intel_increase_pllclock(crtc);6112intel_crtc->busy = true;6113} else {6114/* Busy -> busy, put off timer */6115mod_timer(&intel_crtc->idle_timer, jiffies +6116msecs_to_jiffies(CRTC_IDLE_TIMEOUT));6117}6118}6119}6120}61216122static void intel_crtc_destroy(struct drm_crtc *crtc)6123{6124struct intel_crtc *intel_crtc = to_intel_crtc(crtc);6125struct drm_device *dev = crtc->dev;6126struct intel_unpin_work *work;6127unsigned long flags;61286129spin_lock_irqsave(&dev->event_lock, flags);6130work = intel_crtc->unpin_work;6131intel_crtc->unpin_work = NULL;6132spin_unlock_irqrestore(&dev->event_lock, flags);61336134if (work) {6135cancel_work_sync(&work->work);6136kfree(work);6137}61386139drm_crtc_cleanup(crtc);61406141kfree(intel_crtc);6142}61436144static void intel_unpin_work_fn(struct work_struct *__work)6145{6146struct intel_unpin_work *work =6147container_of(__work, struct intel_unpin_work, work);61486149mutex_lock(&work->dev->struct_mutex);6150i915_gem_object_unpin(work->old_fb_obj);6151drm_gem_object_unreference(&work->pending_flip_obj->base);6152drm_gem_object_unreference(&work->old_fb_obj->base);61536154mutex_unlock(&work->dev->struct_mutex);6155kfree(work);6156}61576158static void do_intel_finish_page_flip(struct drm_device *dev,6159struct drm_crtc *crtc)6160{6161drm_i915_private_t *dev_priv = dev->dev_private;6162struct intel_crtc *intel_crtc = to_intel_crtc(crtc);6163struct intel_unpin_work *work;6164struct drm_i915_gem_object *obj;6165struct drm_pending_vblank_event *e;6166struct timeval tnow, tvbl;6167unsigned long flags;61686169/* Ignore early vblank irqs */6170if (intel_crtc == NULL)6171return;61726173do_gettimeofday(&tnow);61746175spin_lock_irqsave(&dev->event_lock, flags);6176work = intel_crtc->unpin_work;6177if (work == NULL || !work->pending) {6178spin_unlock_irqrestore(&dev->event_lock, flags);6179return;6180}61816182intel_crtc->unpin_work = NULL;61836184if (work->event) {6185e = work->event;6186e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);61876188/* Called before vblank count and timestamps have6189* been updated for the vblank interval of flip6190* completion? Need to increment vblank count and6191* add one videorefresh duration to returned timestamp6192* to account for this. We assume this happened if we6193* get called over 0.9 frame durations after the last6194* timestamped vblank.6195*6196* This calculation can not be used with vrefresh rates6197* below 5Hz (10Hz to be on the safe side) without6198* promoting to 64 integers.6199*/6200if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >62019 * crtc->framedur_ns) {6202e->event.sequence++;6203tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +6204crtc->framedur_ns);6205}62066207e->event.tv_sec = tvbl.tv_sec;6208e->event.tv_usec = tvbl.tv_usec;62096210list_add_tail(&e->base.link,6211&e->base.file_priv->event_list);6212wake_up_interruptible(&e->base.file_priv->event_wait);6213}62146215drm_vblank_put(dev, intel_crtc->pipe);62166217spin_unlock_irqrestore(&dev->event_lock, flags);62186219obj = work->old_fb_obj;62206221atomic_clear_mask(1 << intel_crtc->plane,6222&obj->pending_flip.counter);6223if (atomic_read(&obj->pending_flip) == 0)6224wake_up(&dev_priv->pending_flip_queue);62256226schedule_work(&work->work);62276228trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);6229}62306231void intel_finish_page_flip(struct drm_device *dev, int pipe)6232{6233drm_i915_private_t *dev_priv = dev->dev_private;6234struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];62356236do_intel_finish_page_flip(dev, crtc);6237}62386239void intel_finish_page_flip_plane(struct drm_device *dev, int plane)6240{6241drm_i915_private_t *dev_priv = dev->dev_private;6242struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];62436244do_intel_finish_page_flip(dev, crtc);6245}62466247void intel_prepare_page_flip(struct drm_device *dev, int plane)6248{6249drm_i915_private_t *dev_priv = dev->dev_private;6250struct intel_crtc *intel_crtc =6251to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);6252unsigned long flags;62536254spin_lock_irqsave(&dev->event_lock, flags);6255if (intel_crtc->unpin_work) {6256if ((++intel_crtc->unpin_work->pending) > 1)6257DRM_ERROR("Prepared flip multiple times\n");6258} else {6259DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");6260}6261spin_unlock_irqrestore(&dev->event_lock, flags);6262}62636264static int intel_gen2_queue_flip(struct drm_device *dev,6265struct drm_crtc *crtc,6266struct drm_framebuffer *fb,6267struct drm_i915_gem_object *obj)6268{6269struct drm_i915_private *dev_priv = dev->dev_private;6270struct intel_crtc *intel_crtc = to_intel_crtc(crtc);6271unsigned long offset;6272u32 flip_mask;6273int ret;62746275ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));6276if (ret)6277goto out;62786279/* Offset into the new buffer for cases of shared fbs between CRTCs */6280offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;62816282ret = BEGIN_LP_RING(6);6283if (ret)6284goto out;62856286/* Can't queue multiple flips, so wait for the previous6287* one to finish before executing the next.6288*/6289if (intel_crtc->plane)6290flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;6291else6292flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;6293OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);6294OUT_RING(MI_NOOP);6295OUT_RING(MI_DISPLAY_FLIP |6296MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));6297OUT_RING(fb->pitch);6298OUT_RING(obj->gtt_offset + offset);6299OUT_RING(MI_NOOP);6300ADVANCE_LP_RING();6301out:6302return ret;6303}63046305static int intel_gen3_queue_flip(struct drm_device *dev,6306struct drm_crtc *crtc,6307struct drm_framebuffer *fb,6308struct drm_i915_gem_object *obj)6309{6310struct drm_i915_private *dev_priv = dev->dev_private;6311struct intel_crtc *intel_crtc = to_intel_crtc(crtc);6312unsigned long offset;6313u32 flip_mask;6314int ret;63156316ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));6317if (ret)6318goto out;63196320/* Offset into the new buffer for cases of shared fbs between CRTCs */6321offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;63226323ret = BEGIN_LP_RING(6);6324if (ret)6325goto out;63266327if (intel_crtc->plane)6328flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;6329else6330flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;6331OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);6332OUT_RING(MI_NOOP);6333OUT_RING(MI_DISPLAY_FLIP_I915 |6334MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));6335OUT_RING(fb->pitch);6336OUT_RING(obj->gtt_offset + offset);6337OUT_RING(MI_NOOP);63386339ADVANCE_LP_RING();6340out:6341return ret;6342}63436344static int intel_gen4_queue_flip(struct drm_device *dev,6345struct drm_crtc *crtc,6346struct drm_framebuffer *fb,6347struct drm_i915_gem_object *obj)6348{6349struct drm_i915_private *dev_priv = dev->dev_private;6350struct intel_crtc *intel_crtc = to_intel_crtc(crtc);6351uint32_t pf, pipesrc;6352int ret;63536354ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));6355if (ret)6356goto out;63576358ret = BEGIN_LP_RING(4);6359if (ret)6360goto out;63616362/* i965+ uses the linear or tiled offsets from the6363* Display Registers (which do not change across a page-flip)6364* so we need only reprogram the base address.6365*/6366OUT_RING(MI_DISPLAY_FLIP |6367MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));6368OUT_RING(fb->pitch);6369OUT_RING(obj->gtt_offset | obj->tiling_mode);63706371/* XXX Enabling the panel-fitter across page-flip is so far6372* untested on non-native modes, so ignore it for now.6373* pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;6374*/6375pf = 0;6376pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;6377OUT_RING(pf | pipesrc);6378ADVANCE_LP_RING();6379out:6380return ret;6381}63826383static int intel_gen6_queue_flip(struct drm_device *dev,6384struct drm_crtc *crtc,6385struct drm_framebuffer *fb,6386struct drm_i915_gem_object *obj)6387{6388struct drm_i915_private *dev_priv = dev->dev_private;6389struct intel_crtc *intel_crtc = to_intel_crtc(crtc);6390uint32_t pf, pipesrc;6391int ret;63926393ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));6394if (ret)6395goto out;63966397ret = BEGIN_LP_RING(4);6398if (ret)6399goto out;64006401OUT_RING(MI_DISPLAY_FLIP |6402MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));6403OUT_RING(fb->pitch | obj->tiling_mode);6404OUT_RING(obj->gtt_offset);64056406pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;6407pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;6408OUT_RING(pf | pipesrc);6409ADVANCE_LP_RING();6410out:6411return ret;6412}64136414/*6415* On gen7 we currently use the blit ring because (in early silicon at least)6416* the render ring doesn't give us interrpts for page flip completion, which6417* means clients will hang after the first flip is queued. Fortunately the6418* blit ring generates interrupts properly, so use it instead.6419*/6420static int intel_gen7_queue_flip(struct drm_device *dev,6421struct drm_crtc *crtc,6422struct drm_framebuffer *fb,6423struct drm_i915_gem_object *obj)6424{6425struct drm_i915_private *dev_priv = dev->dev_private;6426struct intel_crtc *intel_crtc = to_intel_crtc(crtc);6427struct intel_ring_buffer *ring = &dev_priv->ring[BCS];6428int ret;64296430ret = intel_pin_and_fence_fb_obj(dev, obj, ring);6431if (ret)6432goto out;64336434ret = intel_ring_begin(ring, 4);6435if (ret)6436goto out;64376438intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));6439intel_ring_emit(ring, (fb->pitch | obj->tiling_mode));6440intel_ring_emit(ring, (obj->gtt_offset));6441intel_ring_emit(ring, (MI_NOOP));6442intel_ring_advance(ring);6443out:6444return ret;6445}64466447static int intel_default_queue_flip(struct drm_device *dev,6448struct drm_crtc *crtc,6449struct drm_framebuffer *fb,6450struct drm_i915_gem_object *obj)6451{6452return -ENODEV;6453}64546455static int intel_crtc_page_flip(struct drm_crtc *crtc,6456struct drm_framebuffer *fb,6457struct drm_pending_vblank_event *event)6458{6459struct drm_device *dev = crtc->dev;6460struct drm_i915_private *dev_priv = dev->dev_private;6461struct intel_framebuffer *intel_fb;6462struct drm_i915_gem_object *obj;6463struct intel_crtc *intel_crtc = to_intel_crtc(crtc);6464struct intel_unpin_work *work;6465unsigned long flags;6466int ret;64676468work = kzalloc(sizeof *work, GFP_KERNEL);6469if (work == NULL)6470return -ENOMEM;64716472work->event = event;6473work->dev = crtc->dev;6474intel_fb = to_intel_framebuffer(crtc->fb);6475work->old_fb_obj = intel_fb->obj;6476INIT_WORK(&work->work, intel_unpin_work_fn);64776478/* We borrow the event spin lock for protecting unpin_work */6479spin_lock_irqsave(&dev->event_lock, flags);6480if (intel_crtc->unpin_work) {6481spin_unlock_irqrestore(&dev->event_lock, flags);6482kfree(work);64836484DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");6485return -EBUSY;6486}6487intel_crtc->unpin_work = work;6488spin_unlock_irqrestore(&dev->event_lock, flags);64896490intel_fb = to_intel_framebuffer(fb);6491obj = intel_fb->obj;64926493mutex_lock(&dev->struct_mutex);64946495/* Reference the objects for the scheduled work. */6496drm_gem_object_reference(&work->old_fb_obj->base);6497drm_gem_object_reference(&obj->base);64986499crtc->fb = fb;65006501ret = drm_vblank_get(dev, intel_crtc->pipe);6502if (ret)6503goto cleanup_objs;65046505work->pending_flip_obj = obj;65066507work->enable_stall_check = true;65086509/* Block clients from rendering to the new back buffer until6510* the flip occurs and the object is no longer visible.6511*/6512atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);65136514ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);6515if (ret)6516goto cleanup_pending;65176518mutex_unlock(&dev->struct_mutex);65196520trace_i915_flip_request(intel_crtc->plane, obj);65216522return 0;65236524cleanup_pending:6525atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);6526cleanup_objs:6527drm_gem_object_unreference(&work->old_fb_obj->base);6528drm_gem_object_unreference(&obj->base);6529mutex_unlock(&dev->struct_mutex);65306531spin_lock_irqsave(&dev->event_lock, flags);6532intel_crtc->unpin_work = NULL;6533spin_unlock_irqrestore(&dev->event_lock, flags);65346535kfree(work);65366537return ret;6538}65396540static void intel_sanitize_modesetting(struct drm_device *dev,6541int pipe, int plane)6542{6543struct drm_i915_private *dev_priv = dev->dev_private;6544u32 reg, val;65456546if (HAS_PCH_SPLIT(dev))6547return;65486549/* Who knows what state these registers were left in by the BIOS or6550* grub?6551*6552* If we leave the registers in a conflicting state (e.g. with the6553* display plane reading from the other pipe than the one we intend6554* to use) then when we attempt to teardown the active mode, we will6555* not disable the pipes and planes in the correct order -- leaving6556* a plane reading from a disabled pipe and possibly leading to6557* undefined behaviour.6558*/65596560reg = DSPCNTR(plane);6561val = I915_READ(reg);65626563if ((val & DISPLAY_PLANE_ENABLE) == 0)6564return;6565if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)6566return;65676568/* This display plane is active and attached to the other CPU pipe. */6569pipe = !pipe;65706571/* Disable the plane and wait for it to stop reading from the pipe. */6572intel_disable_plane(dev_priv, plane, pipe);6573intel_disable_pipe(dev_priv, pipe);6574}65756576static void intel_crtc_reset(struct drm_crtc *crtc)6577{6578struct drm_device *dev = crtc->dev;6579struct intel_crtc *intel_crtc = to_intel_crtc(crtc);65806581/* Reset flags back to the 'unknown' status so that they6582* will be correctly set on the initial modeset.6583*/6584intel_crtc->dpms_mode = -1;65856586/* We need to fix up any BIOS configuration that conflicts with6587* our expectations.6588*/6589intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);6590}65916592static struct drm_crtc_helper_funcs intel_helper_funcs = {6593.dpms = intel_crtc_dpms,6594.mode_fixup = intel_crtc_mode_fixup,6595.mode_set = intel_crtc_mode_set,6596.mode_set_base = intel_pipe_set_base,6597.mode_set_base_atomic = intel_pipe_set_base_atomic,6598.load_lut = intel_crtc_load_lut,6599.disable = intel_crtc_disable,6600};66016602static const struct drm_crtc_funcs intel_crtc_funcs = {6603.reset = intel_crtc_reset,6604.cursor_set = intel_crtc_cursor_set,6605.cursor_move = intel_crtc_cursor_move,6606.gamma_set = intel_crtc_gamma_set,6607.set_config = drm_crtc_helper_set_config,6608.destroy = intel_crtc_destroy,6609.page_flip = intel_crtc_page_flip,6610};66116612static void intel_crtc_init(struct drm_device *dev, int pipe)6613{6614drm_i915_private_t *dev_priv = dev->dev_private;6615struct intel_crtc *intel_crtc;6616int i;66176618intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);6619if (intel_crtc == NULL)6620return;66216622drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);66236624drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);6625for (i = 0; i < 256; i++) {6626intel_crtc->lut_r[i] = i;6627intel_crtc->lut_g[i] = i;6628intel_crtc->lut_b[i] = i;6629}66306631/* Swap pipes & planes for FBC on pre-965 */6632intel_crtc->pipe = pipe;6633intel_crtc->plane = pipe;6634if (IS_MOBILE(dev) && IS_GEN3(dev)) {6635DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");6636intel_crtc->plane = !pipe;6637}66386639BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||6640dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);6641dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;6642dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;66436644intel_crtc_reset(&intel_crtc->base);6645intel_crtc->active = true; /* force the pipe off on setup_init_config */66466647if (HAS_PCH_SPLIT(dev)) {6648intel_helper_funcs.prepare = ironlake_crtc_prepare;6649intel_helper_funcs.commit = ironlake_crtc_commit;6650} else {6651intel_helper_funcs.prepare = i9xx_crtc_prepare;6652intel_helper_funcs.commit = i9xx_crtc_commit;6653}66546655drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);66566657intel_crtc->busy = false;66586659setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,6660(unsigned long)intel_crtc);6661}66626663int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,6664struct drm_file *file)6665{6666drm_i915_private_t *dev_priv = dev->dev_private;6667struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;6668struct drm_mode_object *drmmode_obj;6669struct intel_crtc *crtc;66706671if (!dev_priv) {6672DRM_ERROR("called with no initialization\n");6673return -EINVAL;6674}66756676drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,6677DRM_MODE_OBJECT_CRTC);66786679if (!drmmode_obj) {6680DRM_ERROR("no such CRTC id\n");6681return -EINVAL;6682}66836684crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));6685pipe_from_crtc_id->pipe = crtc->pipe;66866687return 0;6688}66896690static int intel_encoder_clones(struct drm_device *dev, int type_mask)6691{6692struct intel_encoder *encoder;6693int index_mask = 0;6694int entry = 0;66956696list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {6697if (type_mask & encoder->clone_mask)6698index_mask |= (1 << entry);6699entry++;6700}67016702return index_mask;6703}67046705static bool has_edp_a(struct drm_device *dev)6706{6707struct drm_i915_private *dev_priv = dev->dev_private;67086709if (!IS_MOBILE(dev))6710return false;67116712if ((I915_READ(DP_A) & DP_DETECTED) == 0)6713return false;67146715if (IS_GEN5(dev) &&6716(I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))6717return false;67186719return true;6720}67216722static void intel_setup_outputs(struct drm_device *dev)6723{6724struct drm_i915_private *dev_priv = dev->dev_private;6725struct intel_encoder *encoder;6726bool dpd_is_edp = false;6727bool has_lvds = false;67286729if (IS_MOBILE(dev) && !IS_I830(dev))6730has_lvds = intel_lvds_init(dev);6731if (!has_lvds && !HAS_PCH_SPLIT(dev)) {6732/* disable the panel fitter on everything but LVDS */6733I915_WRITE(PFIT_CONTROL, 0);6734}67356736if (HAS_PCH_SPLIT(dev)) {6737dpd_is_edp = intel_dpd_is_edp(dev);67386739if (has_edp_a(dev))6740intel_dp_init(dev, DP_A);67416742if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))6743intel_dp_init(dev, PCH_DP_D);6744}67456746intel_crt_init(dev);67476748if (HAS_PCH_SPLIT(dev)) {6749int found;67506751if (I915_READ(HDMIB) & PORT_DETECTED) {6752/* PCH SDVOB multiplex with HDMIB */6753found = intel_sdvo_init(dev, PCH_SDVOB);6754if (!found)6755intel_hdmi_init(dev, HDMIB);6756if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))6757intel_dp_init(dev, PCH_DP_B);6758}67596760if (I915_READ(HDMIC) & PORT_DETECTED)6761intel_hdmi_init(dev, HDMIC);67626763if (I915_READ(HDMID) & PORT_DETECTED)6764intel_hdmi_init(dev, HDMID);67656766if (I915_READ(PCH_DP_C) & DP_DETECTED)6767intel_dp_init(dev, PCH_DP_C);67686769if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))6770intel_dp_init(dev, PCH_DP_D);67716772} else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {6773bool found = false;67746775if (I915_READ(SDVOB) & SDVO_DETECTED) {6776DRM_DEBUG_KMS("probing SDVOB\n");6777found = intel_sdvo_init(dev, SDVOB);6778if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {6779DRM_DEBUG_KMS("probing HDMI on SDVOB\n");6780intel_hdmi_init(dev, SDVOB);6781}67826783if (!found && SUPPORTS_INTEGRATED_DP(dev)) {6784DRM_DEBUG_KMS("probing DP_B\n");6785intel_dp_init(dev, DP_B);6786}6787}67886789/* Before G4X SDVOC doesn't have its own detect register */67906791if (I915_READ(SDVOB) & SDVO_DETECTED) {6792DRM_DEBUG_KMS("probing SDVOC\n");6793found = intel_sdvo_init(dev, SDVOC);6794}67956796if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {67976798if (SUPPORTS_INTEGRATED_HDMI(dev)) {6799DRM_DEBUG_KMS("probing HDMI on SDVOC\n");6800intel_hdmi_init(dev, SDVOC);6801}6802if (SUPPORTS_INTEGRATED_DP(dev)) {6803DRM_DEBUG_KMS("probing DP_C\n");6804intel_dp_init(dev, DP_C);6805}6806}68076808if (SUPPORTS_INTEGRATED_DP(dev) &&6809(I915_READ(DP_D) & DP_DETECTED)) {6810DRM_DEBUG_KMS("probing DP_D\n");6811intel_dp_init(dev, DP_D);6812}6813} else if (IS_GEN2(dev))6814intel_dvo_init(dev);68156816if (SUPPORTS_TV(dev))6817intel_tv_init(dev);68186819list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {6820encoder->base.possible_crtcs = encoder->crtc_mask;6821encoder->base.possible_clones =6822intel_encoder_clones(dev, encoder->clone_mask);6823}68246825intel_panel_setup_backlight(dev);68266827/* disable all the possible outputs/crtcs before entering KMS mode */6828drm_helper_disable_unused_functions(dev);6829}68306831static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)6832{6833struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);68346835drm_framebuffer_cleanup(fb);6836drm_gem_object_unreference_unlocked(&intel_fb->obj->base);68376838kfree(intel_fb);6839}68406841static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,6842struct drm_file *file,6843unsigned int *handle)6844{6845struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);6846struct drm_i915_gem_object *obj = intel_fb->obj;68476848return drm_gem_handle_create(file, &obj->base, handle);6849}68506851static const struct drm_framebuffer_funcs intel_fb_funcs = {6852.destroy = intel_user_framebuffer_destroy,6853.create_handle = intel_user_framebuffer_create_handle,6854};68556856int intel_framebuffer_init(struct drm_device *dev,6857struct intel_framebuffer *intel_fb,6858struct drm_mode_fb_cmd *mode_cmd,6859struct drm_i915_gem_object *obj)6860{6861int ret;68626863if (obj->tiling_mode == I915_TILING_Y)6864return -EINVAL;68656866if (mode_cmd->pitch & 63)6867return -EINVAL;68686869switch (mode_cmd->bpp) {6870case 8:6871case 16:6872case 24:6873case 32:6874break;6875default:6876return -EINVAL;6877}68786879ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);6880if (ret) {6881DRM_ERROR("framebuffer init failed %d\n", ret);6882return ret;6883}68846885drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);6886intel_fb->obj = obj;6887return 0;6888}68896890static struct drm_framebuffer *6891intel_user_framebuffer_create(struct drm_device *dev,6892struct drm_file *filp,6893struct drm_mode_fb_cmd *mode_cmd)6894{6895struct drm_i915_gem_object *obj;68966897obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));6898if (&obj->base == NULL)6899return ERR_PTR(-ENOENT);69006901return intel_framebuffer_create(dev, mode_cmd, obj);6902}69036904static const struct drm_mode_config_funcs intel_mode_funcs = {6905.fb_create = intel_user_framebuffer_create,6906.output_poll_changed = intel_fb_output_poll_changed,6907};69086909static struct drm_i915_gem_object *6910intel_alloc_context_page(struct drm_device *dev)6911{6912struct drm_i915_gem_object *ctx;6913int ret;69146915WARN_ON(!mutex_is_locked(&dev->struct_mutex));69166917ctx = i915_gem_alloc_object(dev, 4096);6918if (!ctx) {6919DRM_DEBUG("failed to alloc power context, RC6 disabled\n");6920return NULL;6921}69226923ret = i915_gem_object_pin(ctx, 4096, true);6924if (ret) {6925DRM_ERROR("failed to pin power context: %d\n", ret);6926goto err_unref;6927}69286929ret = i915_gem_object_set_to_gtt_domain(ctx, 1);6930if (ret) {6931DRM_ERROR("failed to set-domain on power context: %d\n", ret);6932goto err_unpin;6933}69346935return ctx;69366937err_unpin:6938i915_gem_object_unpin(ctx);6939err_unref:6940drm_gem_object_unreference(&ctx->base);6941mutex_unlock(&dev->struct_mutex);6942return NULL;6943}69446945bool ironlake_set_drps(struct drm_device *dev, u8 val)6946{6947struct drm_i915_private *dev_priv = dev->dev_private;6948u16 rgvswctl;69496950rgvswctl = I915_READ16(MEMSWCTL);6951if (rgvswctl & MEMCTL_CMD_STS) {6952DRM_DEBUG("gpu busy, RCS change rejected\n");6953return false; /* still busy with another command */6954}69556956rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |6957(val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;6958I915_WRITE16(MEMSWCTL, rgvswctl);6959POSTING_READ16(MEMSWCTL);69606961rgvswctl |= MEMCTL_CMD_STS;6962I915_WRITE16(MEMSWCTL, rgvswctl);69636964return true;6965}69666967void ironlake_enable_drps(struct drm_device *dev)6968{6969struct drm_i915_private *dev_priv = dev->dev_private;6970u32 rgvmodectl = I915_READ(MEMMODECTL);6971u8 fmax, fmin, fstart, vstart;69726973/* Enable temp reporting */6974I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);6975I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);69766977/* 100ms RC evaluation intervals */6978I915_WRITE(RCUPEI, 100000);6979I915_WRITE(RCDNEI, 100000);69806981/* Set max/min thresholds to 90ms and 80ms respectively */6982I915_WRITE(RCBMAXAVG, 90000);6983I915_WRITE(RCBMINAVG, 80000);69846985I915_WRITE(MEMIHYST, 1);69866987/* Set up min, max, and cur for interrupt handling */6988fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;6989fmin = (rgvmodectl & MEMMODE_FMIN_MASK);6990fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>6991MEMMODE_FSTART_SHIFT;69926993vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>6994PXVFREQ_PX_SHIFT;69956996dev_priv->fmax = fmax; /* IPS callback will increase this */6997dev_priv->fstart = fstart;69986999dev_priv->max_delay = fstart;7000dev_priv->min_delay = fmin;7001dev_priv->cur_delay = fstart;70027003DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",7004fmax, fmin, fstart);70057006I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);70077008/*7009* Interrupts will be enabled in ironlake_irq_postinstall7010*/70117012I915_WRITE(VIDSTART, vstart);7013POSTING_READ(VIDSTART);70147015rgvmodectl |= MEMMODE_SWMODE_EN;7016I915_WRITE(MEMMODECTL, rgvmodectl);70177018if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))7019DRM_ERROR("stuck trying to change perf mode\n");7020msleep(1);70217022ironlake_set_drps(dev, fstart);70237024dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +7025I915_READ(0x112e0);7026dev_priv->last_time1 = jiffies_to_msecs(jiffies);7027dev_priv->last_count2 = I915_READ(0x112f4);7028getrawmonotonic(&dev_priv->last_time2);7029}70307031void ironlake_disable_drps(struct drm_device *dev)7032{7033struct drm_i915_private *dev_priv = dev->dev_private;7034u16 rgvswctl = I915_READ16(MEMSWCTL);70357036/* Ack interrupts, disable EFC interrupt */7037I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);7038I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);7039I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);7040I915_WRITE(DEIIR, DE_PCU_EVENT);7041I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);70427043/* Go back to the starting frequency */7044ironlake_set_drps(dev, dev_priv->fstart);7045msleep(1);7046rgvswctl |= MEMCTL_CMD_STS;7047I915_WRITE(MEMSWCTL, rgvswctl);7048msleep(1);70497050}70517052void gen6_set_rps(struct drm_device *dev, u8 val)7053{7054struct drm_i915_private *dev_priv = dev->dev_private;7055u32 swreq;70567057swreq = (val & 0x3ff) << 25;7058I915_WRITE(GEN6_RPNSWREQ, swreq);7059}70607061void gen6_disable_rps(struct drm_device *dev)7062{7063struct drm_i915_private *dev_priv = dev->dev_private;70647065I915_WRITE(GEN6_RPNSWREQ, 1 << 31);7066I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);7067I915_WRITE(GEN6_PMIER, 0);70687069spin_lock_irq(&dev_priv->rps_lock);7070dev_priv->pm_iir = 0;7071spin_unlock_irq(&dev_priv->rps_lock);70727073I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));7074}70757076static unsigned long intel_pxfreq(u32 vidfreq)7077{7078unsigned long freq;7079int div = (vidfreq & 0x3f0000) >> 16;7080int post = (vidfreq & 0x3000) >> 12;7081int pre = (vidfreq & 0x7);70827083if (!pre)7084return 0;70857086freq = ((div * 133333) / ((1<<post) * pre));70877088return freq;7089}70907091void intel_init_emon(struct drm_device *dev)7092{7093struct drm_i915_private *dev_priv = dev->dev_private;7094u32 lcfuse;7095u8 pxw[16];7096int i;70977098/* Disable to program */7099I915_WRITE(ECR, 0);7100POSTING_READ(ECR);71017102/* Program energy weights for various events */7103I915_WRITE(SDEW, 0x15040d00);7104I915_WRITE(CSIEW0, 0x007f0000);7105I915_WRITE(CSIEW1, 0x1e220004);7106I915_WRITE(CSIEW2, 0x04000004);71077108for (i = 0; i < 5; i++)7109I915_WRITE(PEW + (i * 4), 0);7110for (i = 0; i < 3; i++)7111I915_WRITE(DEW + (i * 4), 0);71127113/* Program P-state weights to account for frequency power adjustment */7114for (i = 0; i < 16; i++) {7115u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));7116unsigned long freq = intel_pxfreq(pxvidfreq);7117unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>7118PXVFREQ_PX_SHIFT;7119unsigned long val;71207121val = vid * vid;7122val *= (freq / 1000);7123val *= 255;7124val /= (127*127*900);7125if (val > 0xff)7126DRM_ERROR("bad pxval: %ld\n", val);7127pxw[i] = val;7128}7129/* Render standby states get 0 weight */7130pxw[14] = 0;7131pxw[15] = 0;71327133for (i = 0; i < 4; i++) {7134u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |7135(pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);7136I915_WRITE(PXW + (i * 4), val);7137}71387139/* Adjust magic regs to magic values (more experimental results) */7140I915_WRITE(OGW0, 0);7141I915_WRITE(OGW1, 0);7142I915_WRITE(EG0, 0x00007f00);7143I915_WRITE(EG1, 0x0000000e);7144I915_WRITE(EG2, 0x000e0000);7145I915_WRITE(EG3, 0x68000300);7146I915_WRITE(EG4, 0x42000000);7147I915_WRITE(EG5, 0x00140031);7148I915_WRITE(EG6, 0);7149I915_WRITE(EG7, 0);71507151for (i = 0; i < 8; i++)7152I915_WRITE(PXWL + (i * 4), 0);71537154/* Enable PMON + select events */7155I915_WRITE(ECR, 0x80000019);71567157lcfuse = I915_READ(LCFUSE02);71587159dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);7160}71617162void gen6_enable_rps(struct drm_i915_private *dev_priv)7163{7164u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);7165u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);7166u32 pcu_mbox, rc6_mask = 0;7167int cur_freq, min_freq, max_freq;7168int i;71697170/* Here begins a magic sequence of register writes to enable7171* auto-downclocking.7172*7173* Perhaps there might be some value in exposing these to7174* userspace...7175*/7176I915_WRITE(GEN6_RC_STATE, 0);7177mutex_lock(&dev_priv->dev->struct_mutex);7178gen6_gt_force_wake_get(dev_priv);71797180/* disable the counters and set deterministic thresholds */7181I915_WRITE(GEN6_RC_CONTROL, 0);71827183I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);7184I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);7185I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);7186I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);7187I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);71887189for (i = 0; i < I915_NUM_RINGS; i++)7190I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);71917192I915_WRITE(GEN6_RC_SLEEP, 0);7193I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);7194I915_WRITE(GEN6_RC6_THRESHOLD, 50000);7195I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);7196I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */71977198if (i915_enable_rc6)7199rc6_mask = GEN6_RC_CTL_RC6p_ENABLE |7200GEN6_RC_CTL_RC6_ENABLE;72017202I915_WRITE(GEN6_RC_CONTROL,7203rc6_mask |7204GEN6_RC_CTL_EI_MODE(1) |7205GEN6_RC_CTL_HW_ENABLE);72067207I915_WRITE(GEN6_RPNSWREQ,7208GEN6_FREQUENCY(10) |7209GEN6_OFFSET(0) |7210GEN6_AGGRESSIVE_TURBO);7211I915_WRITE(GEN6_RC_VIDEO_FREQ,7212GEN6_FREQUENCY(12));72137214I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);7215I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,721618 << 24 |72176 << 16);7218I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);7219I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);7220I915_WRITE(GEN6_RP_UP_EI, 100000);7221I915_WRITE(GEN6_RP_DOWN_EI, 5000000);7222I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);7223I915_WRITE(GEN6_RP_CONTROL,7224GEN6_RP_MEDIA_TURBO |7225GEN6_RP_USE_NORMAL_FREQ |7226GEN6_RP_MEDIA_IS_GFX |7227GEN6_RP_ENABLE |7228GEN6_RP_UP_BUSY_AVG |7229GEN6_RP_DOWN_IDLE_CONT);72307231if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,7232500))7233DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");72347235I915_WRITE(GEN6_PCODE_DATA, 0);7236I915_WRITE(GEN6_PCODE_MAILBOX,7237GEN6_PCODE_READY |7238GEN6_PCODE_WRITE_MIN_FREQ_TABLE);7239if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,7240500))7241DRM_ERROR("timeout waiting for pcode mailbox to finish\n");72427243min_freq = (rp_state_cap & 0xff0000) >> 16;7244max_freq = rp_state_cap & 0xff;7245cur_freq = (gt_perf_status & 0xff00) >> 8;72467247/* Check for overclock support */7248if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,7249500))7250DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");7251I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);7252pcu_mbox = I915_READ(GEN6_PCODE_DATA);7253if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,7254500))7255DRM_ERROR("timeout waiting for pcode mailbox to finish\n");7256if (pcu_mbox & (1<<31)) { /* OC supported */7257max_freq = pcu_mbox & 0xff;7258DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);7259}72607261/* In units of 100MHz */7262dev_priv->max_delay = max_freq;7263dev_priv->min_delay = min_freq;7264dev_priv->cur_delay = cur_freq;72657266/* requires MSI enabled */7267I915_WRITE(GEN6_PMIER,7268GEN6_PM_MBOX_EVENT |7269GEN6_PM_THERMAL_EVENT |7270GEN6_PM_RP_DOWN_TIMEOUT |7271GEN6_PM_RP_UP_THRESHOLD |7272GEN6_PM_RP_DOWN_THRESHOLD |7273GEN6_PM_RP_UP_EI_EXPIRED |7274GEN6_PM_RP_DOWN_EI_EXPIRED);7275spin_lock_irq(&dev_priv->rps_lock);7276WARN_ON(dev_priv->pm_iir != 0);7277I915_WRITE(GEN6_PMIMR, 0);7278spin_unlock_irq(&dev_priv->rps_lock);7279/* enable all PM interrupts */7280I915_WRITE(GEN6_PMINTRMSK, 0);72817282gen6_gt_force_wake_put(dev_priv);7283mutex_unlock(&dev_priv->dev->struct_mutex);7284}72857286static void ironlake_init_clock_gating(struct drm_device *dev)7287{7288struct drm_i915_private *dev_priv = dev->dev_private;7289uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;72907291/* Required for FBC */7292dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |7293DPFCRUNIT_CLOCK_GATE_DISABLE |7294DPFDUNIT_CLOCK_GATE_DISABLE;7295/* Required for CxSR */7296dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;72977298I915_WRITE(PCH_3DCGDIS0,7299MARIUNIT_CLOCK_GATE_DISABLE |7300SVSMUNIT_CLOCK_GATE_DISABLE);7301I915_WRITE(PCH_3DCGDIS1,7302VFMUNIT_CLOCK_GATE_DISABLE);73037304I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);73057306/*7307* According to the spec the following bits should be set in7308* order to enable memory self-refresh7309* The bit 22/21 of 0x420047310* The bit 5 of 0x420207311* The bit 15 of 0x450007312*/7313I915_WRITE(ILK_DISPLAY_CHICKEN2,7314(I915_READ(ILK_DISPLAY_CHICKEN2) |7315ILK_DPARB_GATE | ILK_VSDPFD_FULL));7316I915_WRITE(ILK_DSPCLK_GATE,7317(I915_READ(ILK_DSPCLK_GATE) |7318ILK_DPARB_CLK_GATE));7319I915_WRITE(DISP_ARB_CTL,7320(I915_READ(DISP_ARB_CTL) |7321DISP_FBC_WM_DIS));7322I915_WRITE(WM3_LP_ILK, 0);7323I915_WRITE(WM2_LP_ILK, 0);7324I915_WRITE(WM1_LP_ILK, 0);73257326/*7327* Based on the document from hardware guys the following bits7328* should be set unconditionally in order to enable FBC.7329* The bit 22 of 0x420007330* The bit 22 of 0x420047331* The bit 7,8,9 of 0x42020.7332*/7333if (IS_IRONLAKE_M(dev)) {7334I915_WRITE(ILK_DISPLAY_CHICKEN1,7335I915_READ(ILK_DISPLAY_CHICKEN1) |7336ILK_FBCQ_DIS);7337I915_WRITE(ILK_DISPLAY_CHICKEN2,7338I915_READ(ILK_DISPLAY_CHICKEN2) |7339ILK_DPARB_GATE);7340I915_WRITE(ILK_DSPCLK_GATE,7341I915_READ(ILK_DSPCLK_GATE) |7342ILK_DPFC_DIS1 |7343ILK_DPFC_DIS2 |7344ILK_CLK_FBC);7345}73467347I915_WRITE(ILK_DISPLAY_CHICKEN2,7348I915_READ(ILK_DISPLAY_CHICKEN2) |7349ILK_ELPIN_409_SELECT);7350I915_WRITE(_3D_CHICKEN2,7351_3D_CHICKEN2_WM_READ_PIPELINED << 16 |7352_3D_CHICKEN2_WM_READ_PIPELINED);7353}73547355static void gen6_init_clock_gating(struct drm_device *dev)7356{7357struct drm_i915_private *dev_priv = dev->dev_private;7358int pipe;7359uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;73607361I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);73627363I915_WRITE(ILK_DISPLAY_CHICKEN2,7364I915_READ(ILK_DISPLAY_CHICKEN2) |7365ILK_ELPIN_409_SELECT);73667367I915_WRITE(WM3_LP_ILK, 0);7368I915_WRITE(WM2_LP_ILK, 0);7369I915_WRITE(WM1_LP_ILK, 0);73707371/*7372* According to the spec the following bits should be7373* set in order to enable memory self-refresh and fbc:7374* The bit21 and bit22 of 0x420007375* The bit21 and bit22 of 0x420047376* The bit5 and bit7 of 0x420207377* The bit14 of 0x701807378* The bit14 of 0x711807379*/7380I915_WRITE(ILK_DISPLAY_CHICKEN1,7381I915_READ(ILK_DISPLAY_CHICKEN1) |7382ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);7383I915_WRITE(ILK_DISPLAY_CHICKEN2,7384I915_READ(ILK_DISPLAY_CHICKEN2) |7385ILK_DPARB_GATE | ILK_VSDPFD_FULL);7386I915_WRITE(ILK_DSPCLK_GATE,7387I915_READ(ILK_DSPCLK_GATE) |7388ILK_DPARB_CLK_GATE |7389ILK_DPFD_CLK_GATE);73907391for_each_pipe(pipe)7392I915_WRITE(DSPCNTR(pipe),7393I915_READ(DSPCNTR(pipe)) |7394DISPPLANE_TRICKLE_FEED_DISABLE);7395}73967397static void ivybridge_init_clock_gating(struct drm_device *dev)7398{7399struct drm_i915_private *dev_priv = dev->dev_private;7400int pipe;7401uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;74027403I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);74047405I915_WRITE(WM3_LP_ILK, 0);7406I915_WRITE(WM2_LP_ILK, 0);7407I915_WRITE(WM1_LP_ILK, 0);74087409I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);74107411for_each_pipe(pipe)7412I915_WRITE(DSPCNTR(pipe),7413I915_READ(DSPCNTR(pipe)) |7414DISPPLANE_TRICKLE_FEED_DISABLE);7415}74167417static void g4x_init_clock_gating(struct drm_device *dev)7418{7419struct drm_i915_private *dev_priv = dev->dev_private;7420uint32_t dspclk_gate;74217422I915_WRITE(RENCLK_GATE_D1, 0);7423I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |7424GS_UNIT_CLOCK_GATE_DISABLE |7425CL_UNIT_CLOCK_GATE_DISABLE);7426I915_WRITE(RAMCLK_GATE_D, 0);7427dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |7428OVRUNIT_CLOCK_GATE_DISABLE |7429OVCUNIT_CLOCK_GATE_DISABLE;7430if (IS_GM45(dev))7431dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;7432I915_WRITE(DSPCLK_GATE_D, dspclk_gate);7433}74347435static void crestline_init_clock_gating(struct drm_device *dev)7436{7437struct drm_i915_private *dev_priv = dev->dev_private;74387439I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);7440I915_WRITE(RENCLK_GATE_D2, 0);7441I915_WRITE(DSPCLK_GATE_D, 0);7442I915_WRITE(RAMCLK_GATE_D, 0);7443I915_WRITE16(DEUC, 0);7444}74457446static void broadwater_init_clock_gating(struct drm_device *dev)7447{7448struct drm_i915_private *dev_priv = dev->dev_private;74497450I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |7451I965_RCC_CLOCK_GATE_DISABLE |7452I965_RCPB_CLOCK_GATE_DISABLE |7453I965_ISC_CLOCK_GATE_DISABLE |7454I965_FBC_CLOCK_GATE_DISABLE);7455I915_WRITE(RENCLK_GATE_D2, 0);7456}74577458static void gen3_init_clock_gating(struct drm_device *dev)7459{7460struct drm_i915_private *dev_priv = dev->dev_private;7461u32 dstate = I915_READ(D_STATE);74627463dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |7464DSTATE_DOT_CLOCK_GATING;7465I915_WRITE(D_STATE, dstate);7466}74677468static void i85x_init_clock_gating(struct drm_device *dev)7469{7470struct drm_i915_private *dev_priv = dev->dev_private;74717472I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);7473}74747475static void i830_init_clock_gating(struct drm_device *dev)7476{7477struct drm_i915_private *dev_priv = dev->dev_private;74787479I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);7480}74817482static void ibx_init_clock_gating(struct drm_device *dev)7483{7484struct drm_i915_private *dev_priv = dev->dev_private;74857486/*7487* On Ibex Peak and Cougar Point, we need to disable clock7488* gating for the panel power sequencer or it will fail to7489* start up when no ports are active.7490*/7491I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);7492}74937494static void cpt_init_clock_gating(struct drm_device *dev)7495{7496struct drm_i915_private *dev_priv = dev->dev_private;74977498/*7499* On Ibex Peak and Cougar Point, we need to disable clock7500* gating for the panel power sequencer or it will fail to7501* start up when no ports are active.7502*/7503I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);7504I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |7505DPLS_EDP_PPS_FIX_DIS);7506}75077508static void ironlake_teardown_rc6(struct drm_device *dev)7509{7510struct drm_i915_private *dev_priv = dev->dev_private;75117512if (dev_priv->renderctx) {7513i915_gem_object_unpin(dev_priv->renderctx);7514drm_gem_object_unreference(&dev_priv->renderctx->base);7515dev_priv->renderctx = NULL;7516}75177518if (dev_priv->pwrctx) {7519i915_gem_object_unpin(dev_priv->pwrctx);7520drm_gem_object_unreference(&dev_priv->pwrctx->base);7521dev_priv->pwrctx = NULL;7522}7523}75247525static void ironlake_disable_rc6(struct drm_device *dev)7526{7527struct drm_i915_private *dev_priv = dev->dev_private;75287529if (I915_READ(PWRCTXA)) {7530/* Wake the GPU, prevent RC6, then restore RSTDBYCTL */7531I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);7532wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),753350);75347535I915_WRITE(PWRCTXA, 0);7536POSTING_READ(PWRCTXA);75377538I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);7539POSTING_READ(RSTDBYCTL);7540}75417542ironlake_teardown_rc6(dev);7543}75447545static int ironlake_setup_rc6(struct drm_device *dev)7546{7547struct drm_i915_private *dev_priv = dev->dev_private;75487549if (dev_priv->renderctx == NULL)7550dev_priv->renderctx = intel_alloc_context_page(dev);7551if (!dev_priv->renderctx)7552return -ENOMEM;75537554if (dev_priv->pwrctx == NULL)7555dev_priv->pwrctx = intel_alloc_context_page(dev);7556if (!dev_priv->pwrctx) {7557ironlake_teardown_rc6(dev);7558return -ENOMEM;7559}75607561return 0;7562}75637564void ironlake_enable_rc6(struct drm_device *dev)7565{7566struct drm_i915_private *dev_priv = dev->dev_private;7567int ret;75687569/* rc6 disabled by default due to repeated reports of hanging during7570* boot and resume.7571*/7572if (!i915_enable_rc6)7573return;75747575mutex_lock(&dev->struct_mutex);7576ret = ironlake_setup_rc6(dev);7577if (ret) {7578mutex_unlock(&dev->struct_mutex);7579return;7580}75817582/*7583* GPU can automatically power down the render unit if given a page7584* to save state.7585*/7586ret = BEGIN_LP_RING(6);7587if (ret) {7588ironlake_teardown_rc6(dev);7589mutex_unlock(&dev->struct_mutex);7590return;7591}75927593OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);7594OUT_RING(MI_SET_CONTEXT);7595OUT_RING(dev_priv->renderctx->gtt_offset |7596MI_MM_SPACE_GTT |7597MI_SAVE_EXT_STATE_EN |7598MI_RESTORE_EXT_STATE_EN |7599MI_RESTORE_INHIBIT);7600OUT_RING(MI_SUSPEND_FLUSH);7601OUT_RING(MI_NOOP);7602OUT_RING(MI_FLUSH);7603ADVANCE_LP_RING();76047605/*7606* Wait for the command parser to advance past MI_SET_CONTEXT. The HW7607* does an implicit flush, combined with MI_FLUSH above, it should be7608* safe to assume that renderctx is valid7609*/7610ret = intel_wait_ring_idle(LP_RING(dev_priv));7611if (ret) {7612DRM_ERROR("failed to enable ironlake power power savings\n");7613ironlake_teardown_rc6(dev);7614mutex_unlock(&dev->struct_mutex);7615return;7616}76177618I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);7619I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);7620mutex_unlock(&dev->struct_mutex);7621}76227623void intel_init_clock_gating(struct drm_device *dev)7624{7625struct drm_i915_private *dev_priv = dev->dev_private;76267627dev_priv->display.init_clock_gating(dev);76287629if (dev_priv->display.init_pch_clock_gating)7630dev_priv->display.init_pch_clock_gating(dev);7631}76327633/* Set up chip specific display functions */7634static void intel_init_display(struct drm_device *dev)7635{7636struct drm_i915_private *dev_priv = dev->dev_private;76377638/* We always want a DPMS function */7639if (HAS_PCH_SPLIT(dev)) {7640dev_priv->display.dpms = ironlake_crtc_dpms;7641dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;7642} else {7643dev_priv->display.dpms = i9xx_crtc_dpms;7644dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;7645}76467647if (I915_HAS_FBC(dev)) {7648if (HAS_PCH_SPLIT(dev)) {7649dev_priv->display.fbc_enabled = ironlake_fbc_enabled;7650dev_priv->display.enable_fbc = ironlake_enable_fbc;7651dev_priv->display.disable_fbc = ironlake_disable_fbc;7652} else if (IS_GM45(dev)) {7653dev_priv->display.fbc_enabled = g4x_fbc_enabled;7654dev_priv->display.enable_fbc = g4x_enable_fbc;7655dev_priv->display.disable_fbc = g4x_disable_fbc;7656} else if (IS_CRESTLINE(dev)) {7657dev_priv->display.fbc_enabled = i8xx_fbc_enabled;7658dev_priv->display.enable_fbc = i8xx_enable_fbc;7659dev_priv->display.disable_fbc = i8xx_disable_fbc;7660}7661/* 855GM needs testing */7662}76637664/* Returns the core display clock speed */7665if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))7666dev_priv->display.get_display_clock_speed =7667i945_get_display_clock_speed;7668else if (IS_I915G(dev))7669dev_priv->display.get_display_clock_speed =7670i915_get_display_clock_speed;7671else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))7672dev_priv->display.get_display_clock_speed =7673i9xx_misc_get_display_clock_speed;7674else if (IS_I915GM(dev))7675dev_priv->display.get_display_clock_speed =7676i915gm_get_display_clock_speed;7677else if (IS_I865G(dev))7678dev_priv->display.get_display_clock_speed =7679i865_get_display_clock_speed;7680else if (IS_I85X(dev))7681dev_priv->display.get_display_clock_speed =7682i855_get_display_clock_speed;7683else /* 852, 830 */7684dev_priv->display.get_display_clock_speed =7685i830_get_display_clock_speed;76867687/* For FIFO watermark updates */7688if (HAS_PCH_SPLIT(dev)) {7689if (HAS_PCH_IBX(dev))7690dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;7691else if (HAS_PCH_CPT(dev))7692dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;76937694if (IS_GEN5(dev)) {7695if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)7696dev_priv->display.update_wm = ironlake_update_wm;7697else {7698DRM_DEBUG_KMS("Failed to get proper latency. "7699"Disable CxSR\n");7700dev_priv->display.update_wm = NULL;7701}7702dev_priv->display.fdi_link_train = ironlake_fdi_link_train;7703dev_priv->display.init_clock_gating = ironlake_init_clock_gating;7704} else if (IS_GEN6(dev)) {7705if (SNB_READ_WM0_LATENCY()) {7706dev_priv->display.update_wm = sandybridge_update_wm;7707} else {7708DRM_DEBUG_KMS("Failed to read display plane latency. "7709"Disable CxSR\n");7710dev_priv->display.update_wm = NULL;7711}7712dev_priv->display.fdi_link_train = gen6_fdi_link_train;7713dev_priv->display.init_clock_gating = gen6_init_clock_gating;7714} else if (IS_IVYBRIDGE(dev)) {7715/* FIXME: detect B0+ stepping and use auto training */7716dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;7717if (SNB_READ_WM0_LATENCY()) {7718dev_priv->display.update_wm = sandybridge_update_wm;7719} else {7720DRM_DEBUG_KMS("Failed to read display plane latency. "7721"Disable CxSR\n");7722dev_priv->display.update_wm = NULL;7723}7724dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;77257726} else7727dev_priv->display.update_wm = NULL;7728} else if (IS_PINEVIEW(dev)) {7729if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),7730dev_priv->is_ddr3,7731dev_priv->fsb_freq,7732dev_priv->mem_freq)) {7733DRM_INFO("failed to find known CxSR latency "7734"(found ddr%s fsb freq %d, mem freq %d), "7735"disabling CxSR\n",7736(dev_priv->is_ddr3 == 1) ? "3": "2",7737dev_priv->fsb_freq, dev_priv->mem_freq);7738/* Disable CxSR and never update its watermark again */7739pineview_disable_cxsr(dev);7740dev_priv->display.update_wm = NULL;7741} else7742dev_priv->display.update_wm = pineview_update_wm;7743dev_priv->display.init_clock_gating = gen3_init_clock_gating;7744} else if (IS_G4X(dev)) {7745dev_priv->display.update_wm = g4x_update_wm;7746dev_priv->display.init_clock_gating = g4x_init_clock_gating;7747} else if (IS_GEN4(dev)) {7748dev_priv->display.update_wm = i965_update_wm;7749if (IS_CRESTLINE(dev))7750dev_priv->display.init_clock_gating = crestline_init_clock_gating;7751else if (IS_BROADWATER(dev))7752dev_priv->display.init_clock_gating = broadwater_init_clock_gating;7753} else if (IS_GEN3(dev)) {7754dev_priv->display.update_wm = i9xx_update_wm;7755dev_priv->display.get_fifo_size = i9xx_get_fifo_size;7756dev_priv->display.init_clock_gating = gen3_init_clock_gating;7757} else if (IS_I865G(dev)) {7758dev_priv->display.update_wm = i830_update_wm;7759dev_priv->display.init_clock_gating = i85x_init_clock_gating;7760dev_priv->display.get_fifo_size = i830_get_fifo_size;7761} else if (IS_I85X(dev)) {7762dev_priv->display.update_wm = i9xx_update_wm;7763dev_priv->display.get_fifo_size = i85x_get_fifo_size;7764dev_priv->display.init_clock_gating = i85x_init_clock_gating;7765} else {7766dev_priv->display.update_wm = i830_update_wm;7767dev_priv->display.init_clock_gating = i830_init_clock_gating;7768if (IS_845G(dev))7769dev_priv->display.get_fifo_size = i845_get_fifo_size;7770else7771dev_priv->display.get_fifo_size = i830_get_fifo_size;7772}77737774/* Default just returns -ENODEV to indicate unsupported */7775dev_priv->display.queue_flip = intel_default_queue_flip;77767777switch (INTEL_INFO(dev)->gen) {7778case 2:7779dev_priv->display.queue_flip = intel_gen2_queue_flip;7780break;77817782case 3:7783dev_priv->display.queue_flip = intel_gen3_queue_flip;7784break;77857786case 4:7787case 5:7788dev_priv->display.queue_flip = intel_gen4_queue_flip;7789break;77907791case 6:7792dev_priv->display.queue_flip = intel_gen6_queue_flip;7793break;7794case 7:7795dev_priv->display.queue_flip = intel_gen7_queue_flip;7796break;7797}7798}77997800/*7801* Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,7802* resume, or other times. This quirk makes sure that's the case for7803* affected systems.7804*/7805static void quirk_pipea_force (struct drm_device *dev)7806{7807struct drm_i915_private *dev_priv = dev->dev_private;78087809dev_priv->quirks |= QUIRK_PIPEA_FORCE;7810DRM_DEBUG_DRIVER("applying pipe a force quirk\n");7811}78127813/*7814* Some machines (Lenovo U160) do not work with SSC on LVDS for some reason7815*/7816static void quirk_ssc_force_disable(struct drm_device *dev)7817{7818struct drm_i915_private *dev_priv = dev->dev_private;7819dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;7820}78217822struct intel_quirk {7823int device;7824int subsystem_vendor;7825int subsystem_device;7826void (*hook)(struct drm_device *dev);7827};78287829struct intel_quirk intel_quirks[] = {7830/* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */7831{ 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },7832/* HP Mini needs pipe A force quirk (LP: #322104) */7833{ 0x27ae,0x103c, 0x361a, quirk_pipea_force },78347835/* Thinkpad R31 needs pipe A force quirk */7836{ 0x3577, 0x1014, 0x0505, quirk_pipea_force },7837/* Toshiba Protege R-205, S-209 needs pipe A force quirk */7838{ 0x2592, 0x1179, 0x0001, quirk_pipea_force },78397840/* ThinkPad X30 needs pipe A force quirk (LP: #304614) */7841{ 0x3577, 0x1014, 0x0513, quirk_pipea_force },7842/* ThinkPad X40 needs pipe A force quirk */78437844/* ThinkPad T60 needs pipe A force quirk (bug #16494) */7845{ 0x2782, 0x17aa, 0x201a, quirk_pipea_force },78467847/* 855 & before need to leave pipe A & dpll A up */7848{ 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },7849{ 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },78507851/* Lenovo U160 cannot use SSC on LVDS */7852{ 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },7853};78547855static void intel_init_quirks(struct drm_device *dev)7856{7857struct pci_dev *d = dev->pdev;7858int i;78597860for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {7861struct intel_quirk *q = &intel_quirks[i];78627863if (d->device == q->device &&7864(d->subsystem_vendor == q->subsystem_vendor ||7865q->subsystem_vendor == PCI_ANY_ID) &&7866(d->subsystem_device == q->subsystem_device ||7867q->subsystem_device == PCI_ANY_ID))7868q->hook(dev);7869}7870}78717872/* Disable the VGA plane that we never use */7873static void i915_disable_vga(struct drm_device *dev)7874{7875struct drm_i915_private *dev_priv = dev->dev_private;7876u8 sr1;7877u32 vga_reg;78787879if (HAS_PCH_SPLIT(dev))7880vga_reg = CPU_VGACNTRL;7881else7882vga_reg = VGACNTRL;78837884vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);7885outb(1, VGA_SR_INDEX);7886sr1 = inb(VGA_SR_DATA);7887outb(sr1 | 1<<5, VGA_SR_DATA);7888vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);7889udelay(300);78907891I915_WRITE(vga_reg, VGA_DISP_DISABLE);7892POSTING_READ(vga_reg);7893}78947895void intel_modeset_init(struct drm_device *dev)7896{7897struct drm_i915_private *dev_priv = dev->dev_private;7898int i;78997900drm_mode_config_init(dev);79017902dev->mode_config.min_width = 0;7903dev->mode_config.min_height = 0;79047905dev->mode_config.funcs = (void *)&intel_mode_funcs;79067907intel_init_quirks(dev);79087909intel_init_display(dev);79107911if (IS_GEN2(dev)) {7912dev->mode_config.max_width = 2048;7913dev->mode_config.max_height = 2048;7914} else if (IS_GEN3(dev)) {7915dev->mode_config.max_width = 4096;7916dev->mode_config.max_height = 4096;7917} else {7918dev->mode_config.max_width = 8192;7919dev->mode_config.max_height = 8192;7920}7921dev->mode_config.fb_base = dev->agp->base;79227923DRM_DEBUG_KMS("%d display pipe%s available.\n",7924dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");79257926for (i = 0; i < dev_priv->num_pipe; i++) {7927intel_crtc_init(dev, i);7928}79297930/* Just disable it once at startup */7931i915_disable_vga(dev);7932intel_setup_outputs(dev);79337934intel_init_clock_gating(dev);79357936if (IS_IRONLAKE_M(dev)) {7937ironlake_enable_drps(dev);7938intel_init_emon(dev);7939}79407941if (IS_GEN6(dev))7942gen6_enable_rps(dev_priv);79437944INIT_WORK(&dev_priv->idle_work, intel_idle_update);7945setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,7946(unsigned long)dev);7947}79487949void intel_modeset_gem_init(struct drm_device *dev)7950{7951if (IS_IRONLAKE_M(dev))7952ironlake_enable_rc6(dev);79537954intel_setup_overlay(dev);7955}79567957void intel_modeset_cleanup(struct drm_device *dev)7958{7959struct drm_i915_private *dev_priv = dev->dev_private;7960struct drm_crtc *crtc;7961struct intel_crtc *intel_crtc;79627963drm_kms_helper_poll_fini(dev);7964mutex_lock(&dev->struct_mutex);79657966intel_unregister_dsm_handler();796779687969list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {7970/* Skip inactive CRTCs */7971if (!crtc->fb)7972continue;79737974intel_crtc = to_intel_crtc(crtc);7975intel_increase_pllclock(crtc);7976}79777978if (dev_priv->display.disable_fbc)7979dev_priv->display.disable_fbc(dev);79807981if (IS_IRONLAKE_M(dev))7982ironlake_disable_drps(dev);7983if (IS_GEN6(dev))7984gen6_disable_rps(dev);79857986if (IS_IRONLAKE_M(dev))7987ironlake_disable_rc6(dev);79887989mutex_unlock(&dev->struct_mutex);79907991/* Disable the irq before mode object teardown, for the irq might7992* enqueue unpin/hotplug work. */7993drm_irq_uninstall(dev);7994cancel_work_sync(&dev_priv->hotplug_work);79957996/* Shut off idle work before the crtcs get freed. */7997list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {7998intel_crtc = to_intel_crtc(crtc);7999del_timer_sync(&intel_crtc->idle_timer);8000}8001del_timer_sync(&dev_priv->idle_timer);8002cancel_work_sync(&dev_priv->idle_work);80038004drm_mode_config_cleanup(dev);8005}80068007/*8008* Return which encoder is currently attached for connector.8009*/8010struct drm_encoder *intel_best_encoder(struct drm_connector *connector)8011{8012return &intel_attached_encoder(connector)->base;8013}80148015void intel_connector_attach_encoder(struct intel_connector *connector,8016struct intel_encoder *encoder)8017{8018connector->encoder = encoder;8019drm_mode_connector_attach_encoder(&connector->base,8020&encoder->base);8021}80228023/*8024* set vga decode state - true == enable VGA decode8025*/8026int intel_modeset_vga_set_state(struct drm_device *dev, bool state)8027{8028struct drm_i915_private *dev_priv = dev->dev_private;8029u16 gmch_ctrl;80308031pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);8032if (state)8033gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;8034else8035gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;8036pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);8037return 0;8038}80398040#ifdef CONFIG_DEBUG_FS8041#include <linux/seq_file.h>80428043struct intel_display_error_state {8044struct intel_cursor_error_state {8045u32 control;8046u32 position;8047u32 base;8048u32 size;8049} cursor[2];80508051struct intel_pipe_error_state {8052u32 conf;8053u32 source;80548055u32 htotal;8056u32 hblank;8057u32 hsync;8058u32 vtotal;8059u32 vblank;8060u32 vsync;8061} pipe[2];80628063struct intel_plane_error_state {8064u32 control;8065u32 stride;8066u32 size;8067u32 pos;8068u32 addr;8069u32 surface;8070u32 tile_offset;8071} plane[2];8072};80738074struct intel_display_error_state *8075intel_display_capture_error_state(struct drm_device *dev)8076{8077drm_i915_private_t *dev_priv = dev->dev_private;8078struct intel_display_error_state *error;8079int i;80808081error = kmalloc(sizeof(*error), GFP_ATOMIC);8082if (error == NULL)8083return NULL;80848085for (i = 0; i < 2; i++) {8086error->cursor[i].control = I915_READ(CURCNTR(i));8087error->cursor[i].position = I915_READ(CURPOS(i));8088error->cursor[i].base = I915_READ(CURBASE(i));80898090error->plane[i].control = I915_READ(DSPCNTR(i));8091error->plane[i].stride = I915_READ(DSPSTRIDE(i));8092error->plane[i].size = I915_READ(DSPSIZE(i));8093error->plane[i].pos= I915_READ(DSPPOS(i));8094error->plane[i].addr = I915_READ(DSPADDR(i));8095if (INTEL_INFO(dev)->gen >= 4) {8096error->plane[i].surface = I915_READ(DSPSURF(i));8097error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));8098}80998100error->pipe[i].conf = I915_READ(PIPECONF(i));8101error->pipe[i].source = I915_READ(PIPESRC(i));8102error->pipe[i].htotal = I915_READ(HTOTAL(i));8103error->pipe[i].hblank = I915_READ(HBLANK(i));8104error->pipe[i].hsync = I915_READ(HSYNC(i));8105error->pipe[i].vtotal = I915_READ(VTOTAL(i));8106error->pipe[i].vblank = I915_READ(VBLANK(i));8107error->pipe[i].vsync = I915_READ(VSYNC(i));8108}81098110return error;8111}81128113void8114intel_display_print_error_state(struct seq_file *m,8115struct drm_device *dev,8116struct intel_display_error_state *error)8117{8118int i;81198120for (i = 0; i < 2; i++) {8121seq_printf(m, "Pipe [%d]:\n", i);8122seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);8123seq_printf(m, " SRC: %08x\n", error->pipe[i].source);8124seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);8125seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);8126seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);8127seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);8128seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);8129seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);81308131seq_printf(m, "Plane [%d]:\n", i);8132seq_printf(m, " CNTR: %08x\n", error->plane[i].control);8133seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);8134seq_printf(m, " SIZE: %08x\n", error->plane[i].size);8135seq_printf(m, " POS: %08x\n", error->plane[i].pos);8136seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);8137if (INTEL_INFO(dev)->gen >= 4) {8138seq_printf(m, " SURF: %08x\n", error->plane[i].surface);8139seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);8140}81418142seq_printf(m, "Cursor [%d]:\n", i);8143seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);8144seq_printf(m, " POS: %08x\n", error->cursor[i].position);8145seq_printf(m, " BASE: %08x\n", error->cursor[i].base);8146}8147}8148#endif814981508151