1 /* 2 * Copyright 2011-2015, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Michael Lotz, mmlr@mlotz.ch 7 * Alexander von Gluck IV, kallisti5@unixzen.com 8 */ 9 #include "Pipes.h" 10 11 #include "accelerant.h" 12 #include "intel_extreme.h" 13 #include <KernelExport.h> 14 15 #include <stdlib.h> 16 #include <string.h> 17 18 #include <new> 19 20 21 #define TRACE_PIPE 22 #ifdef TRACE_PIPE 23 extern "C" void _sPrintf(const char* format, ...); 24 # define TRACE(x...) _sPrintf("intel_extreme: " x) 25 #else 26 # define TRACE(x...) ; 27 #endif 28 29 #define ERROR(x...) _sPrintf("intel_extreme: " x) 30 #define CALLED(x...) TRACE("CALLED %s\n", __PRETTY_FUNCTION__) 31 32 33 // PIPE: 6 34 // PLANE: 7 35 36 37 void 38 program_pipe_color_modes(uint32 colorMode) 39 { 40 // All pipes get the same color mode 41 write32(INTEL_DISPLAY_A_CONTROL, (read32(INTEL_DISPLAY_A_CONTROL) 42 & ~(DISPLAY_CONTROL_COLOR_MASK | DISPLAY_CONTROL_GAMMA)) 43 | colorMode); 44 write32(INTEL_DISPLAY_B_CONTROL, (read32(INTEL_DISPLAY_B_CONTROL) 45 & ~(DISPLAY_CONTROL_COLOR_MASK | DISPLAY_CONTROL_GAMMA)) 46 | colorMode); 47 } 48 49 50 // #pragma mark - Pipe 51 52 53 Pipe::Pipe(pipe_index pipeIndex) 54 : 55 fHasTranscoder(false), 56 fFDILink(NULL), 57 fPanelFitter(NULL), 58 fPipeIndex(pipeIndex), 59 fPipeOffset(0), 60 fPlaneOffset(0) 61 { 62 if (pipeIndex == INTEL_PIPE_B) { 63 fPipeOffset = INTEL_DISPLAY_OFFSET; 64 fPlaneOffset = INTEL_PLANE_OFFSET; 65 } 66 67 // IvyBridge: Analog + Digital Ports behind FDI (on northbridge) 68 // Haswell: Only VGA behind FDI (on northbridge) 69 // SkyLake: FDI gone. No more northbridge video. 70 if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) { 71 TRACE("%s: Pipe %s routed through FDI\n", __func__, 72 (pipeIndex == INTEL_PIPE_A) ? "A" : "B"); 73 74 fHasTranscoder = true; 75 76 // Program FDILink if PCH 77 fFDILink = new(std::nothrow) FDILink(pipeIndex); 78 // Program gen5(+) style panelfitter as well 79 fPanelFitter = new(std::nothrow) PanelFitter(pipeIndex); 80 } 81 82 TRACE("Pipe %s. Pipe Base: 0x%" B_PRIxADDR 83 " Plane Base: 0x% " B_PRIxADDR "\n", (pipeIndex == INTEL_PIPE_A) 84 ? "A" : "B", fPipeOffset, fPlaneOffset); 85 } 86 87 88 Pipe::~Pipe() 89 { 90 } 91 92 93 bool 94 Pipe::IsEnabled() 95 { 96 CALLED(); 97 98 return (read32(INTEL_DISPLAY_A_PIPE_CONTROL + fPipeOffset) 99 & INTEL_PIPE_ENABLED) != 0; 100 } 101 102 103 void 104 Pipe::Configure(display_mode* mode) 105 { 106 uint32 pipeControl = read32(INTEL_DISPLAY_A_PIPE_CONTROL + fPipeOffset); 107 108 // TODO: Haswell+ dithering changes. 109 //if (gInfo->shared_info->device_type.Generation() >= 4) { 110 // pipeControl |= (INTEL_PIPE_DITHER_EN | INTEL_PIPE_DITHER_TYPE_SP); 111 112 //Link bit depth: this should be globally known per FDI link (i.e. laptop panel 3x6, rest 3x8) 113 //currently using BIOS preconfigured setup 114 //pipeControl = (pipeControl & ~INTEL_PIPE_BPC_MASK) | INTEL_PIPE_BPC(INTEL_PIPE_8BPC); 115 116 // TODO: CxSR downclocking? 117 118 // TODO: Interlaced modes 119 pipeControl = (pipeControl & ~(0x7 << 21)) | INTEL_PIPE_PROGRESSIVE; 120 121 write32(INTEL_DISPLAY_A_PIPE_CONTROL + fPipeOffset, pipeControl); 122 read32(INTEL_DISPLAY_A_PIPE_CONTROL + fPipeOffset); 123 124 if (gInfo->shared_info->device_type.Generation() >= 6) { 125 // According to SandyBridge modesetting sequence, pipe must be enabled 126 // before PLL are configured. 127 addr_t pipeReg = INTEL_DISPLAY_A_PIPE_CONTROL + fPipeOffset; 128 write32(pipeReg, read32(pipeReg) | INTEL_PIPE_ENABLED); 129 } 130 } 131 132 133 void 134 Pipe::_ConfigureTranscoder(display_mode* target) 135 { 136 CALLED(); 137 138 TRACE("%s: fPipeOffset: 0x%" B_PRIx32"\n", __func__, fPipeOffset); 139 140 // update timing (fPipeOffset bumps the DISPLAY_A to B when needed) 141 write32(INTEL_TRANSCODER_A_HTOTAL + fPipeOffset, 142 ((uint32)(target->timing.h_total - 1) << 16) 143 | ((uint32)target->timing.h_display - 1)); 144 write32(INTEL_TRANSCODER_A_HBLANK + fPipeOffset, 145 ((uint32)(target->timing.h_total - 1) << 16) 146 | ((uint32)target->timing.h_display - 1)); 147 write32(INTEL_TRANSCODER_A_HSYNC + fPipeOffset, 148 ((uint32)(target->timing.h_sync_end - 1) << 16) 149 | ((uint32)target->timing.h_sync_start - 1)); 150 151 write32(INTEL_TRANSCODER_A_VTOTAL + fPipeOffset, 152 ((uint32)(target->timing.v_total - 1) << 16) 153 | ((uint32)target->timing.v_display - 1)); 154 write32(INTEL_TRANSCODER_A_VBLANK + fPipeOffset, 155 ((uint32)(target->timing.v_total - 1) << 16) 156 | ((uint32)target->timing.v_display - 1)); 157 write32(INTEL_TRANSCODER_A_VSYNC + fPipeOffset, 158 ((uint32)(target->timing.v_sync_end - 1) << 16) 159 | ((uint32)target->timing.v_sync_start - 1)); 160 161 #if 0 162 // XXX: Is it ok to do these on non-digital? 163 write32(INTEL_TRANSCODER_A_POS + fPipeOffset, 0); 164 write32(INTEL_TRANSCODER_A_IMAGE_SIZE + fPipeOffset, 165 ((uint32)(target->virtual_width - 1) << 16) 166 | ((uint32)target->virtual_height - 1)); 167 #endif 168 } 169 170 171 void 172 Pipe::ConfigureTimings(display_mode* target, bool hardware) 173 { 174 CALLED(); 175 176 TRACE("%s(%d): fPipeOffset: 0x%" B_PRIx32"\n", __func__, hardware, 177 fPipeOffset); 178 179 if (target == NULL) { 180 ERROR("%s: Invalid display mode!\n", __func__); 181 return; 182 } 183 184 /* If using the transcoder, leave the display at its native resolution, 185 * and configure only the transcoder (panel fitting will match them 186 * together). */ 187 if (!fHasTranscoder || hardware) 188 { 189 // update timing (fPipeOffset bumps the DISPLAY_A to B when needed) 190 write32(INTEL_DISPLAY_A_HTOTAL + fPipeOffset, 191 ((uint32)(target->timing.h_total - 1) << 16) 192 | ((uint32)target->timing.h_display - 1)); 193 write32(INTEL_DISPLAY_A_HBLANK + fPipeOffset, 194 ((uint32)(target->timing.h_total - 1) << 16) 195 | ((uint32)target->timing.h_display - 1)); 196 write32(INTEL_DISPLAY_A_HSYNC + fPipeOffset, 197 ((uint32)(target->timing.h_sync_end - 1) << 16) 198 | ((uint32)target->timing.h_sync_start - 1)); 199 200 write32(INTEL_DISPLAY_A_VTOTAL + fPipeOffset, 201 ((uint32)(target->timing.v_total - 1) << 16) 202 | ((uint32)target->timing.v_display - 1)); 203 write32(INTEL_DISPLAY_A_VBLANK + fPipeOffset, 204 ((uint32)(target->timing.v_total - 1) << 16) 205 | ((uint32)target->timing.v_display - 1)); 206 write32(INTEL_DISPLAY_A_VSYNC + fPipeOffset, 207 ((uint32)(target->timing.v_sync_end - 1) << 16) 208 | ((uint32)target->timing.v_sync_start - 1)); 209 } 210 211 if (gInfo->shared_info->device_type.Generation() < 6) { 212 // FIXME check on which generations this register exists 213 // (it appears it would be available only for cursor planes, not 214 // display planes) 215 // Since we set the plane to be the same size as the display, we can 216 // just show it starting at top-left. 217 write32(INTEL_DISPLAY_A_POS + fPipeOffset, 0); 218 } 219 220 // The only thing that really matters: set the image size and let the 221 // panel fitter or the transcoder worry about the rest 222 write32(INTEL_DISPLAY_A_PIPE_SIZE + fPipeOffset, 223 ((uint32)(target->virtual_width - 1) << 16) 224 | ((uint32)target->virtual_height - 1)); 225 226 // Set the plane size as well while we're at it (this is independant, we 227 // could have a larger plane and scroll through it). 228 if (gInfo->shared_info->device_type.Generation() <= 4) { 229 // This is "reserved" on G35 and GMA965, but needed on 945 (for which 230 // there is no public documentation), and I assume earlier devices as 231 // well. Note that the height and width are swapped when compared to 232 // the other registers. 233 write32(INTEL_DISPLAY_A_IMAGE_SIZE + fPipeOffset, 234 ((uint32)(target->virtual_height - 1) << 16) 235 | ((uint32)target->virtual_width - 1)); 236 } 237 238 if (fHasTranscoder && hardware) { 239 _ConfigureTranscoder(target); 240 } 241 } 242 243 244 void 245 Pipe::ConfigureClocks(const pll_divisors& divisors, uint32 pixelClock, 246 uint32 extraFlags) 247 { 248 CALLED(); 249 250 addr_t pllDivisorA = INTEL_DISPLAY_A_PLL_DIVISOR_0; 251 addr_t pllDivisorB = INTEL_DISPLAY_A_PLL_DIVISOR_1; 252 addr_t pllControl = INTEL_DISPLAY_A_PLL; 253 addr_t pllMD = INTEL_DISPLAY_A_PLL_MD; 254 255 if (fPipeIndex == INTEL_PIPE_B) { 256 pllDivisorA = INTEL_DISPLAY_B_PLL_DIVISOR_0; 257 pllDivisorB = INTEL_DISPLAY_B_PLL_DIVISOR_1; 258 pllControl = INTEL_DISPLAY_B_PLL; 259 pllMD = INTEL_DISPLAY_B_PLL_MD; 260 } 261 262 // Disable DPLL first 263 write32(pllControl, read32(pllControl) & ~DISPLAY_PLL_ENABLED); 264 spin(150); 265 266 float refFreq = gInfo->shared_info->pll_info.reference_frequency / 1000.0f; 267 268 if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_96x)) { 269 float adjusted = ((refFreq * divisors.m) / divisors.n) / divisors.p; 270 uint32 pixelMultiply = uint32(adjusted / (pixelClock / 1000.0f)); 271 write32(pllMD, (0 << 24) | ((pixelMultiply - 1) << 8)); 272 } 273 274 // XXX: For now we assume no LVDS downclocking and program the same divisor 275 // value to both divisor 0 (standard) and 1 (reduced divisor) 276 if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_PIN)) { 277 write32(pllDivisorA, (((1 << divisors.n) << DISPLAY_PLL_N_DIVISOR_SHIFT) 278 & DISPLAY_PLL_IGD_N_DIVISOR_MASK) 279 | (((divisors.m2 - 2) << DISPLAY_PLL_M2_DIVISOR_SHIFT) 280 & DISPLAY_PLL_IGD_M2_DIVISOR_MASK)); 281 write32(pllDivisorB, (((1 << divisors.n) << DISPLAY_PLL_N_DIVISOR_SHIFT) 282 & DISPLAY_PLL_IGD_N_DIVISOR_MASK) 283 | (((divisors.m2 - 2) << DISPLAY_PLL_M2_DIVISOR_SHIFT) 284 & DISPLAY_PLL_IGD_M2_DIVISOR_MASK)); 285 } else { 286 write32(pllDivisorA, (((divisors.n - 2) << DISPLAY_PLL_N_DIVISOR_SHIFT) 287 & DISPLAY_PLL_N_DIVISOR_MASK) 288 | (((divisors.m1 - 2) << DISPLAY_PLL_M1_DIVISOR_SHIFT) 289 & DISPLAY_PLL_M1_DIVISOR_MASK) 290 | (((divisors.m2 - 2) << DISPLAY_PLL_M2_DIVISOR_SHIFT) 291 & DISPLAY_PLL_M2_DIVISOR_MASK)); 292 write32(pllDivisorB, (((divisors.n - 2) << DISPLAY_PLL_N_DIVISOR_SHIFT) 293 & DISPLAY_PLL_N_DIVISOR_MASK) 294 | (((divisors.m1 - 2) << DISPLAY_PLL_M1_DIVISOR_SHIFT) 295 & DISPLAY_PLL_M1_DIVISOR_MASK) 296 | (((divisors.m2 - 2) << DISPLAY_PLL_M2_DIVISOR_SHIFT) 297 & DISPLAY_PLL_M2_DIVISOR_MASK)); 298 } 299 300 //note: bit DISPLAY_PLL_NO_VGA_CONTROL does not exist on IvyBridge and should be left 301 // zero there. It does not influence it though. 302 uint32 pll = DISPLAY_PLL_ENABLED | DISPLAY_PLL_NO_VGA_CONTROL | extraFlags; 303 304 if (gInfo->shared_info->device_type.Generation() >= 3) { 305 // p1 divisor << 1 , 1-8 306 if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_PIN)) { 307 pll |= ((1 << (divisors.p1 - 1)) 308 << DISPLAY_PLL_IGD_POST1_DIVISOR_SHIFT) 309 & DISPLAY_PLL_IGD_POST1_DIVISOR_MASK; 310 } else { 311 pll |= ((1 << (divisors.p1 - 1)) 312 << DISPLAY_PLL_POST1_DIVISOR_SHIFT) 313 & DISPLAY_PLL_9xx_POST1_DIVISOR_MASK; 314 // pll |= ((divisors.p1 - 1) << DISPLAY_PLL_POST1_DIVISOR_SHIFT) 315 // & DISPLAY_PLL_9xx_POST1_DIVISOR_MASK; 316 } 317 318 // Also configure the FP0 divisor on SandyBridge 319 if (gInfo->shared_info->device_type.Generation() == 6) { 320 pll |= ((1 << (divisors.p1 - 1)) 321 << DISPLAY_PLL_SNB_FP0_POST1_DIVISOR_SHIFT) 322 & DISPLAY_PLL_SNB_FP0_POST1_DIVISOR_MASK; 323 } 324 325 if (divisors.p2 == 5 || divisors.p2 == 7) 326 pll |= DISPLAY_PLL_DIVIDE_HIGH; 327 328 if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_96x)) 329 pll |= 6 << DISPLAY_PLL_PULSE_PHASE_SHIFT; 330 } else { 331 if (divisors.p2 != 5 && divisors.p2 != 7) 332 pll |= DISPLAY_PLL_DIVIDE_4X; 333 334 pll |= DISPLAY_PLL_2X_CLOCK; 335 336 // TODO: Is this supposed to be DISPLAY_PLL_IGD_POST1_DIVISOR_MASK?? 337 if (divisors.p1 > 2) { 338 pll |= ((divisors.p1 - 2) << DISPLAY_PLL_POST1_DIVISOR_SHIFT) 339 & DISPLAY_PLL_POST1_DIVISOR_MASK; 340 } else 341 pll |= DISPLAY_PLL_POST1_DIVIDE_2; 342 } 343 344 // Configure PLL while -keeping- it disabled 345 //note: on older chipsets DISPLAY_PLL_NO_VGA_CONTROL probably enables the PLL and locks regs; 346 // on newer chipsets DISPLAY_PLL_ENABLED does this. 347 write32(pllControl, pll & ~DISPLAY_PLL_ENABLED & ~DISPLAY_PLL_NO_VGA_CONTROL); 348 read32(pllControl); 349 spin(150); 350 351 // enable pre-configured PLL (locks PLL settings directly blocking changes in this write even) 352 write32(pllControl, pll); 353 read32(pllControl); 354 355 // Allow the PLL to warm up. 356 spin(150); 357 358 if (gInfo->shared_info->device_type.Generation() >= 6) { 359 // SandyBridge has 3 transcoders, but only 2 PLLs. So there is a new 360 // register which routes the PLL output to the transcoder that we need 361 // to configure 362 uint32 pllSel = read32(SNB_DPLL_SEL); 363 TRACE("Old PLL selection: 0x%" B_PRIx32 "\n", pllSel); 364 uint32 shift = 0; 365 uint32 pllIndex = 0; 366 367 // FIXME we assume that pipe A is used with transcoder A, and pipe B 368 // with transcoder B, that may not always be the case 369 if (fPipeIndex == INTEL_PIPE_A) { 370 shift = 0; 371 pllIndex = 0; 372 TRACE("Route PLL A to transcoder A\n"); 373 } else if (fPipeIndex == INTEL_PIPE_B) { 374 shift = 4; 375 pllIndex = 1; 376 TRACE("Route PLL B to transcoder B\n"); 377 } else { 378 ERROR("Attempting to configure PLL for unhandled pipe"); 379 return; 380 } 381 382 // Mask out the previous PLL configuration for this transcoder 383 pllSel &= ~(0xF << shift); 384 385 // Set up the new configuration for this transcoder and enable it 386 pllSel |= (8 | pllIndex) << shift; 387 388 TRACE("New PLL selection: 0x%" B_PRIx32 "\n", pllSel); 389 write32(SNB_DPLL_SEL, pllSel); 390 } 391 } 392 393 void 394 Pipe::Enable(bool enable) 395 { 396 CALLED(); 397 398 addr_t pipeReg = INTEL_DISPLAY_A_PIPE_CONTROL + fPipeOffset; 399 addr_t planeReg = INTEL_DISPLAY_A_CONTROL + fPlaneOffset; 400 401 // Planes always have to operate on an enabled pipe 402 403 if (enable) { 404 write32(pipeReg, read32(pipeReg) | INTEL_PIPE_ENABLED); 405 wait_for_vblank(); 406 write32(planeReg, read32(planeReg) | DISPLAY_CONTROL_ENABLED); 407 408 //Enable default display main watermarks 409 if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) { 410 if (fPipeOffset == 0) 411 write32(INTEL_DISPLAY_A_PIPE_WATERMARK, 0x0783818); 412 else 413 write32(INTEL_DISPLAY_B_PIPE_WATERMARK, 0x0783818); 414 } 415 } else { 416 write32(planeReg, read32(planeReg) & ~DISPLAY_CONTROL_ENABLED); 417 wait_for_vblank(); 418 //Sandy+: when link training is to be done re-enable this line but otherwise don't touch! 419 //GMA(Q45): must disable PIPE or DPLL programming fails. 420 if (gInfo->shared_info->device_type.Generation() <= 5) { 421 write32(pipeReg, read32(pipeReg) & ~INTEL_PIPE_ENABLED); 422 } 423 } 424 425 // flush the eventually cached PCI bus writes 426 read32(INTEL_DISPLAY_A_BASE); 427 } 428