1 /* 2 * Copyright 2006-2011, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Alexander von Gluck, kallisti5@unixzen.com 7 */ 8 9 10 #include "pll.h" 11 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include <math.h> 16 17 #include "accelerant_protos.h" 18 #include "accelerant.h" 19 #include "bios.h" 20 #include "connector.h" 21 #include "display.h" 22 #include "displayport.h" 23 #include "encoder.h" 24 #include "utility.h" 25 26 27 #define TRACE_PLL 28 #ifdef TRACE_PLL 29 extern "C" void _sPrintf(const char* format, ...); 30 # define TRACE(x...) _sPrintf("radeon_hd: " x) 31 #else 32 # define TRACE(x...) ; 33 #endif 34 35 #define ERROR(x...) _sPrintf("radeon_hd: " x) 36 37 // Pixel Clock Storage 38 // kHz Value Result 39 // Haiku: 104000 khz 104 Mhz 40 // Linux: 104000 khz 104 Mhz 41 // AtomBIOS: 10400 * 10 khz 104 Mhz 42 // Ghz 43 // Haiku: 162000 * 10 khz 1.62 Ghz 44 // Linux: 162000 * 10 khz 1.62 Ghz 45 // AtomBIOS: 16200 * 10 Khz 0.162 * 10 Ghz 46 47 48 /* The PLL allows to synthesize a clock signal with a range of frequencies 49 * based on a single input reference clock signal. It uses several dividers 50 * to create a rational factor multiple of the input frequency. 51 * 52 * The reference clock signal frequency is pll_info::referenceFreq (in kHz). 53 * It is then, one after another... 54 * (1) divided by the (integer) reference divider (pll_info::referenceDiv). 55 * (2) multiplied by the fractional feedback divider, which sits in the 56 * PLL's feedback loop and thus multiplies the frequency. It allows 57 * using a rational number factor of the form "x.y", with 58 * x = pll_info::feedbackDiv and y = pll_info::feedbackDivFrac. 59 * (3) divided by the (integer) post divider (pll_info::postDiv). 60 * Allowed ranges are given in the pll_info min/max values. 61 * 62 * The resulting output pixel clock frequency is then: 63 * 64 * feedbackDiv + (feedbackDivFrac/10) 65 * f_out = referenceFreq * ------------------------------------ 66 * referenceDiv * postDiv 67 */ 68 69 70 status_t 71 pll_limit_probe(pll_info* pll) 72 { 73 uint8 tableMajor; 74 uint8 tableMinor; 75 uint16 tableOffset; 76 77 int index = GetIndexIntoMasterTable(DATA, FirmwareInfo); 78 if (atom_parse_data_header(gAtomContext, index, NULL, 79 &tableMajor, &tableMinor, &tableOffset) != B_OK) { 80 ERROR("%s: Couldn't parse data header\n", __func__); 81 return B_ERROR; 82 } 83 84 TRACE("%s: table %" B_PRIu8 ".%" B_PRIu8 "\n", __func__, 85 tableMajor, tableMinor); 86 87 union atomFirmwareInfo { 88 ATOM_FIRMWARE_INFO info; 89 ATOM_FIRMWARE_INFO_V1_2 info_12; 90 ATOM_FIRMWARE_INFO_V1_3 info_13; 91 ATOM_FIRMWARE_INFO_V1_4 info_14; 92 ATOM_FIRMWARE_INFO_V2_1 info_21; 93 ATOM_FIRMWARE_INFO_V2_2 info_22; 94 }; 95 union atomFirmwareInfo* firmwareInfo 96 = (union atomFirmwareInfo*)(gAtomContext->bios + tableOffset); 97 98 /* pixel clock limits */ 99 pll->referenceFreq 100 = B_LENDIAN_TO_HOST_INT16(firmwareInfo->info.usReferenceClock) * 10; 101 102 if (tableMinor < 2) { 103 pll->pllOutMin 104 = B_LENDIAN_TO_HOST_INT16( 105 firmwareInfo->info.usMinPixelClockPLL_Output) * 10; 106 } else { 107 pll->pllOutMin 108 = B_LENDIAN_TO_HOST_INT32( 109 firmwareInfo->info_12.ulMinPixelClockPLL_Output) * 10; 110 } 111 112 pll->pllOutMax 113 = B_LENDIAN_TO_HOST_INT32( 114 firmwareInfo->info.ulMaxPixelClockPLL_Output) * 10; 115 116 if (tableMinor >= 4) { 117 pll->lcdPllOutMin 118 = B_LENDIAN_TO_HOST_INT16( 119 firmwareInfo->info_14.usLcdMinPixelClockPLL_Output) * 1000; 120 121 if (pll->lcdPllOutMin == 0) 122 pll->lcdPllOutMin = pll->pllOutMin; 123 124 pll->lcdPllOutMax 125 = B_LENDIAN_TO_HOST_INT16( 126 firmwareInfo->info_14.usLcdMaxPixelClockPLL_Output) * 1000; 127 128 if (pll->lcdPllOutMax == 0) 129 pll->lcdPllOutMax = pll->pllOutMax; 130 131 } else { 132 pll->lcdPllOutMin = pll->pllOutMin; 133 pll->lcdPllOutMax = pll->pllOutMax; 134 } 135 136 if (pll->pllOutMin == 0) { 137 pll->pllOutMin = 64800 * 10; 138 // Avivo+ limit 139 } 140 141 pll->minPostDiv = POST_DIV_MIN; 142 pll->maxPostDiv = POST_DIV_LIMIT; 143 pll->minRefDiv = REF_DIV_MIN; 144 pll->maxRefDiv = REF_DIV_LIMIT; 145 pll->minFeedbackDiv = FB_DIV_MIN; 146 pll->maxFeedbackDiv = FB_DIV_LIMIT; 147 148 pll->pllInMin = B_LENDIAN_TO_HOST_INT16( 149 firmwareInfo->info.usMinPixelClockPLL_Input) * 10; 150 pll->pllInMax = B_LENDIAN_TO_HOST_INT16( 151 firmwareInfo->info.usMaxPixelClockPLL_Input) * 10; 152 153 TRACE("%s: referenceFreq: %" B_PRIu32 "; pllOutMin: %" B_PRIu32 "; " 154 " pllOutMax: %" B_PRIu32 "; pllInMin: %" B_PRIu32 ";" 155 "pllInMax: %" B_PRIu32 "\n", __func__, pll->referenceFreq, 156 pll->pllOutMin, pll->pllOutMax, pll->pllInMin, pll->pllInMax); 157 158 return B_OK; 159 } 160 161 162 status_t 163 pll_ppll_ss_probe(pll_info* pll, uint32 ssID) 164 { 165 uint8 tableMajor; 166 uint8 tableMinor; 167 uint16 headerOffset; 168 uint16 headerSize; 169 170 int index = GetIndexIntoMasterTable(DATA, PPLL_SS_Info); 171 if (atom_parse_data_header(gAtomContext, index, &headerSize, 172 &tableMajor, &tableMinor, &headerOffset) != B_OK) { 173 ERROR("%s: Couldn't parse data header\n", __func__); 174 pll->ssEnabled = false; 175 return B_ERROR; 176 } 177 178 struct _ATOM_SPREAD_SPECTRUM_INFO *ss_info 179 = (struct _ATOM_SPREAD_SPECTRUM_INFO*)((uint16*)gAtomContext->bios 180 + headerOffset); 181 182 int indices = (headerSize - sizeof(ATOM_COMMON_TABLE_HEADER)) 183 / sizeof(ATOM_SPREAD_SPECTRUM_ASSIGNMENT); 184 185 int i; 186 for (i = 0; i < indices; i++) { 187 if (ss_info->asSS_Info[i].ucSS_Id == ssID) { 188 pll->ssPercentage = B_LENDIAN_TO_HOST_INT16( 189 ss_info->asSS_Info[i].usSpreadSpectrumPercentage); 190 pll->ssType = ss_info->asSS_Info[i].ucSpreadSpectrumType; 191 pll->ssStep = ss_info->asSS_Info[i].ucSS_Step; 192 pll->ssDelay = ss_info->asSS_Info[i].ucSS_Delay; 193 pll->ssRange = ss_info->asSS_Info[i].ucSS_Range; 194 pll->ssReferenceDiv 195 = ss_info->asSS_Info[i].ucRecommendedRef_Div; 196 pll->ssEnabled = true; 197 return B_OK; 198 } 199 } 200 201 pll->ssEnabled = false; 202 return B_ERROR; 203 } 204 205 206 status_t 207 pll_asic_ss_probe(pll_info* pll, uint32 ssID) 208 { 209 uint8 tableMajor; 210 uint8 tableMinor; 211 uint16 headerOffset; 212 uint16 headerSize; 213 214 int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info); 215 if (atom_parse_data_header(gAtomContext, index, &headerSize, 216 &tableMajor, &tableMinor, &headerOffset) != B_OK) { 217 ERROR("%s: Couldn't parse data header\n", __func__); 218 pll->ssEnabled = false; 219 return B_ERROR; 220 } 221 222 union asicSSInfo { 223 struct _ATOM_ASIC_INTERNAL_SS_INFO info; 224 struct _ATOM_ASIC_INTERNAL_SS_INFO_V2 info_2; 225 struct _ATOM_ASIC_INTERNAL_SS_INFO_V3 info_3; 226 }; 227 228 union asicSSInfo *ss_info 229 = (union asicSSInfo*)((uint16*)gAtomContext->bios + headerOffset); 230 231 int i; 232 int indices; 233 switch (tableMajor) { 234 case 1: 235 indices = (headerSize - sizeof(ATOM_COMMON_TABLE_HEADER)) 236 / sizeof(ATOM_ASIC_SS_ASSIGNMENT); 237 238 for (i = 0; i < indices; i++) { 239 if (ss_info->info.asSpreadSpectrum[i].ucClockIndication 240 != ssID) { 241 continue; 242 } 243 TRACE("%s: ss match found\n", __func__); 244 if (pll->pixelClock * 10 > B_LENDIAN_TO_HOST_INT32( 245 ss_info->info.asSpreadSpectrum[i].ulTargetClockRange)) { 246 TRACE("%s: pixelClock > targetClockRange!\n", __func__); 247 continue; 248 } 249 250 pll->ssPercentage = B_LENDIAN_TO_HOST_INT16( 251 ss_info->info.asSpreadSpectrum[i].usSpreadSpectrumPercentage 252 ); 253 254 pll->ssType 255 = ss_info->info.asSpreadSpectrum[i].ucSpreadSpectrumMode; 256 pll->ssRate = B_LENDIAN_TO_HOST_INT16( 257 ss_info->info.asSpreadSpectrum[i].usSpreadRateInKhz); 258 pll->ssPercentageDiv = 100; 259 pll->ssEnabled = true; 260 return B_OK; 261 } 262 break; 263 case 2: 264 indices = (headerSize - sizeof(ATOM_COMMON_TABLE_HEADER)) 265 / sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2); 266 267 for (i = 0; i < indices; i++) { 268 if (ss_info->info_2.asSpreadSpectrum[i].ucClockIndication 269 != ssID) { 270 continue; 271 } 272 TRACE("%s: ss match found\n", __func__); 273 if (pll->pixelClock * 10 > B_LENDIAN_TO_HOST_INT32( 274 ss_info->info_2.asSpreadSpectrum[i].ulTargetClockRange)) { 275 TRACE("%s: pixelClock > targetClockRange!\n", __func__); 276 continue; 277 } 278 279 pll->ssPercentage = B_LENDIAN_TO_HOST_INT16( 280 ss_info 281 ->info_2.asSpreadSpectrum[i].usSpreadSpectrumPercentage 282 ); 283 284 pll->ssType 285 = ss_info->info_2.asSpreadSpectrum[i].ucSpreadSpectrumMode; 286 pll->ssRate = B_LENDIAN_TO_HOST_INT16( 287 ss_info->info_2.asSpreadSpectrum[i].usSpreadRateIn10Hz); 288 pll->ssPercentageDiv = 100; 289 pll->ssEnabled = true; 290 return B_OK; 291 } 292 break; 293 case 3: 294 indices = (headerSize - sizeof(ATOM_COMMON_TABLE_HEADER)) 295 / sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3); 296 297 for (i = 0; i < indices; i++) { 298 if (ss_info->info_3.asSpreadSpectrum[i].ucClockIndication 299 != ssID) { 300 continue; 301 } 302 TRACE("%s: ss match found\n", __func__); 303 if (pll->pixelClock * 10 > B_LENDIAN_TO_HOST_INT32( 304 ss_info->info_3.asSpreadSpectrum[i].ulTargetClockRange)) { 305 TRACE("%s: pixelClock > targetClockRange!\n", __func__); 306 continue; 307 } 308 309 pll->ssPercentage = B_LENDIAN_TO_HOST_INT16( 310 ss_info 311 ->info_3.asSpreadSpectrum[i].usSpreadSpectrumPercentage 312 ); 313 pll->ssType 314 = ss_info->info_3.asSpreadSpectrum[i].ucSpreadSpectrumMode; 315 pll->ssRate = B_LENDIAN_TO_HOST_INT16( 316 ss_info->info_3.asSpreadSpectrum[i].usSpreadRateIn10Hz); 317 318 if ((ss_info->info_3.asSpreadSpectrum[i].ucSpreadSpectrumMode 319 & SS_MODE_V3_PERCENTAGE_DIV_BY_1000_MASK) != 0) 320 pll->ssPercentageDiv = 1000; 321 else 322 pll->ssPercentageDiv = 100; 323 324 if (ssID == ASIC_INTERNAL_ENGINE_SS 325 || ssID == ASIC_INTERNAL_MEMORY_SS) 326 pll->ssRate /= 100; 327 328 pll->ssEnabled = true; 329 return B_OK; 330 } 331 break; 332 default: 333 ERROR("%s: Unknown SS table version!\n", __func__); 334 pll->ssEnabled = false; 335 return B_ERROR; 336 } 337 338 ERROR("%s: No potential spread spectrum data found!\n", __func__); 339 pll->ssEnabled = false; 340 return B_ERROR; 341 } 342 343 344 void 345 pll_compute_post_divider(pll_info* pll) 346 { 347 if ((pll->flags & PLL_USE_POST_DIV) != 0) { 348 TRACE("%s: using AtomBIOS post divider\n", __func__); 349 return; 350 } 351 352 uint32 vco; 353 if ((pll->flags & PLL_PREFER_MINM_OVER_MAXP) != 0) { 354 if ((pll->flags & PLL_IS_LCD) != 0) 355 vco = pll->lcdPllOutMin; 356 else 357 vco = pll->pllOutMax; 358 } else { 359 if ((pll->flags & PLL_IS_LCD) != 0) 360 vco = pll->lcdPllOutMax; 361 else 362 vco = pll->pllOutMin; 363 } 364 365 TRACE("%s: vco = %" B_PRIu32 "\n", __func__, vco); 366 367 uint32 postDivider = vco / pll->adjustedClock; 368 uint32 tmp = vco % pll->adjustedClock; 369 370 if ((pll->flags & PLL_PREFER_MINM_OVER_MAXP) != 0) { 371 if (tmp) 372 postDivider++; 373 } else { 374 if (!tmp) 375 postDivider--; 376 } 377 378 if (postDivider > pll->maxPostDiv) 379 postDivider = pll->maxPostDiv; 380 else if (postDivider < pll->minPostDiv) 381 postDivider = pll->minPostDiv; 382 383 pll->postDiv = postDivider; 384 TRACE("%s: postDiv = %" B_PRIu32 "\n", __func__, postDivider); 385 } 386 387 388 /*! Compute values for the fractional feedback divider to match the desired 389 * pixel clock frequency as closely as possible. Reference and post divider 390 * values are already filled in (if used). 391 */ 392 status_t 393 pll_compute(pll_info* pll) 394 { 395 radeon_shared_info &info = *gInfo->shared_info; 396 397 pll_compute_post_divider(pll); 398 399 const uint32 targetClock = pll->adjustedClock; 400 401 pll->feedbackDiv = 0; 402 pll->feedbackDivFrac = 0; 403 404 if ((pll->flags & PLL_USE_REF_DIV) != 0) { 405 TRACE("%s: using AtomBIOS reference divider\n", __func__); 406 } else { 407 TRACE("%s: using minimum reference divider\n", __func__); 408 pll->referenceDiv = pll->minRefDiv; 409 } 410 411 if ((pll->flags & PLL_USE_FRAC_FB_DIV) != 0) { 412 TRACE("%s: using AtomBIOS fractional feedback divider\n", __func__); 413 414 const uint32 numerator = pll->postDiv * pll->referenceDiv 415 * targetClock; 416 pll->feedbackDiv = numerator / pll->referenceFreq; 417 pll->feedbackDivFrac = numerator % pll->referenceFreq; 418 419 if (pll->feedbackDiv > pll->maxFeedbackDiv) 420 pll->feedbackDiv = pll->maxFeedbackDiv; 421 else if (pll->feedbackDiv < pll->minFeedbackDiv) 422 pll->feedbackDiv = pll->minFeedbackDiv; 423 424 // Put first 2 digits after the decimal point into feedbackDivFrac 425 pll->feedbackDivFrac 426 = (100 * pll->feedbackDivFrac) / pll->referenceFreq; 427 428 // Now round it to one digit 429 if (pll->feedbackDivFrac >= 5) { 430 pll->feedbackDivFrac -= 5; 431 pll->feedbackDivFrac /= 10; 432 pll->feedbackDivFrac++; 433 } 434 if (pll->feedbackDivFrac >= 10) { 435 pll->feedbackDiv++; 436 pll->feedbackDivFrac = 0; 437 } 438 } else { 439 TRACE("%s: performing fractional feedback calculations\n", __func__); 440 441 while (pll->referenceDiv <= pll->maxRefDiv) { 442 // get feedback divider 443 uint32 retroEncabulator = pll->postDiv * pll->referenceDiv; 444 445 retroEncabulator *= targetClock; 446 pll->feedbackDiv = retroEncabulator / pll->referenceFreq; 447 pll->feedbackDivFrac 448 = retroEncabulator % pll->referenceFreq; 449 450 if (pll->feedbackDiv > pll->maxFeedbackDiv) 451 pll->feedbackDiv = pll->maxFeedbackDiv; 452 else if (pll->feedbackDiv < pll->minFeedbackDiv) 453 pll->feedbackDiv = pll->minFeedbackDiv; 454 455 if (pll->feedbackDivFrac >= (pll->referenceFreq / 2)) 456 pll->feedbackDiv++; 457 458 pll->feedbackDivFrac = 0; 459 460 if (pll->referenceDiv == 0 461 || pll->postDiv == 0 462 || targetClock == 0) { 463 TRACE("%s: Caught division by zero!\n", __func__); 464 TRACE("%s: referenceDiv %" B_PRIu32 "\n", 465 __func__, pll->referenceDiv); 466 TRACE("%s: postDiv %" B_PRIu32 "\n", 467 __func__, pll->postDiv); 468 TRACE("%s: targetClock %" B_PRIu32 "\n", 469 __func__, targetClock); 470 return B_ERROR; 471 } 472 uint32 tmp = (pll->referenceFreq * pll->feedbackDiv) 473 / (pll->postDiv * pll->referenceDiv); 474 tmp = (tmp * 1000) / targetClock; 475 476 if (tmp > (1000 + (MAX_TOLERANCE / 10))) 477 pll->referenceDiv++; 478 else if (tmp >= (1000 - (MAX_TOLERANCE / 10))) 479 break; 480 else 481 pll->referenceDiv++; 482 } 483 } 484 485 if (pll->referenceDiv == 0 || pll->postDiv == 0) { 486 TRACE("%s: Caught division by zero of post or reference divider\n", 487 __func__); 488 return B_ERROR; 489 } 490 491 uint32 calculatedClock 492 = ((pll->referenceFreq * pll->feedbackDiv * 10) 493 + (pll->referenceFreq * pll->feedbackDivFrac)) 494 / (pll->referenceDiv * pll->postDiv * 10); 495 496 TRACE("%s: Calculated pixel clock of %" B_PRIu32 " based on:\n", __func__, 497 calculatedClock); 498 TRACE("%s: referenceFrequency: %" B_PRIu32 "; " 499 "referenceDivider: %" B_PRIu32 "\n", __func__, pll->referenceFreq, 500 pll->referenceDiv); 501 TRACE("%s: feedbackDivider: %" B_PRIu32 "; " 502 "feedbackDividerFrac: %" B_PRIu32 "\n", __func__, pll->feedbackDiv, 503 pll->feedbackDivFrac); 504 TRACE("%s: postDivider: %" B_PRIu32 "\n", __func__, pll->postDiv); 505 506 if (pll->adjustedClock != calculatedClock) { 507 TRACE("%s: pixel clock %" B_PRIu32 " was changed to %" B_PRIu32 "\n", 508 __func__, pll->adjustedClock, calculatedClock); 509 pll->pixelClock = calculatedClock; 510 } 511 512 // Calcuate needed SS data on DCE4 513 if (info.dceMajor >= 4 && pll->ssEnabled) { 514 if (pll->ssPercentageDiv == 0) { 515 // Avoid div by 0, shouldn't happen but be mindful of it 516 TRACE("%s: ssPercentageDiv is less than 0, aborting SS calcualation", 517 __func__); 518 pll->ssEnabled = false; 519 return B_OK; 520 } 521 uint32 amount = ((pll->feedbackDiv * 10) + pll->feedbackDivFrac); 522 amount *= pll->ssPercentage; 523 amount /= pll->ssPercentageDiv * 100; 524 pll->ssAmount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK; 525 pll->ssAmount |= ((amount - (amount / 10)) 526 << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) & ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK; 527 528 uint32 centerSpreadMultiplier = 2; 529 if ((pll->ssType & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD) != 0) 530 centerSpreadMultiplier = 4; 531 pll->ssStep = (centerSpreadMultiplier * amount * pll->referenceDiv 532 * (pll->ssRate * 2048)) / (125 * 25 * pll->referenceFreq / 100); 533 } 534 535 return B_OK; 536 } 537 538 539 void 540 pll_setup_flags(pll_info* pll, uint8 crtcID) 541 { 542 radeon_shared_info &info = *gInfo->shared_info; 543 uint32 connectorIndex = gDisplay[crtcID]->connectorIndex; 544 uint32 connectorFlags = gConnector[connectorIndex]->flags; 545 546 uint32 dceVersion = (info.dceMajor * 100) + info.dceMinor; 547 548 TRACE("%s: CRTC: %" B_PRIu8 ", PLL: %" B_PRIu8 "\n", __func__, 549 crtcID, pll->id); 550 551 if (dceVersion >= 302 && pll->pixelClock > 200000) 552 pll->flags |= PLL_PREFER_HIGH_FB_DIV; 553 else 554 pll->flags |= PLL_PREFER_LOW_REF_DIV; 555 556 if (info.chipsetID < RADEON_RV770) 557 pll->flags |= PLL_PREFER_MINM_OVER_MAXP; 558 559 if ((connectorFlags & ATOM_DEVICE_LCD_SUPPORT) != 0) { 560 pll->flags |= PLL_IS_LCD; 561 562 // use reference divider for spread spectrum 563 TRACE("%s: Spread Spectrum is %" B_PRIu32 "%%\n", __func__, 564 pll->ssPercentage); 565 if (pll->ssPercentage > 0) { 566 if (pll->ssReferenceDiv > 0) { 567 TRACE("%s: using Spread Spectrum reference divider. " 568 "refDiv was: %" B_PRIu32 ", now: %" B_PRIu32 "\n", 569 __func__, pll->referenceDiv, pll->ssReferenceDiv); 570 pll->flags |= PLL_USE_REF_DIV; 571 pll->referenceDiv = pll->ssReferenceDiv; 572 573 // TODO: IS AVIVO+? 574 pll->flags |= PLL_USE_FRAC_FB_DIV; 575 } 576 } 577 } 578 579 if ((connectorFlags & ATOM_DEVICE_TV_SUPPORT) != 0) 580 pll->flags |= PLL_PREFER_CLOSEST_LOWER; 581 582 if ((info.chipsetFlags & CHIP_APU) != 0) { 583 // Use fractional feedback on APU's 584 pll->flags |= PLL_USE_FRAC_FB_DIV; 585 } 586 } 587 588 589 status_t 590 pll_adjust(pll_info* pll, display_mode* mode, uint8 crtcID) 591 { 592 radeon_shared_info &info = *gInfo->shared_info; 593 594 uint32 pixelClock = pll->pixelClock; 595 // original as pixel_clock will be adjusted 596 597 uint32 connectorIndex = gDisplay[crtcID]->connectorIndex; 598 connector_info* connector = gConnector[connectorIndex]; 599 600 uint32 encoderID = connector->encoder.objectID; 601 uint32 encoderMode = display_get_encoder_mode(connectorIndex); 602 uint32 connectorFlags = connector->flags; 603 604 uint32 externalEncoderID = 0; 605 pll->adjustedClock = pll->pixelClock; 606 if (connector->encoderExternal.isDPBridge) 607 externalEncoderID = connector->encoderExternal.objectID; 608 609 if (info.dceMajor >= 3) { 610 611 uint8 tableMajor; 612 uint8 tableMinor; 613 614 int index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll); 615 if (atom_parse_cmd_header(gAtomContext, index, &tableMajor, &tableMinor) 616 != B_OK) { 617 ERROR("%s: Couldn't find AtomBIOS PLL adjustment\n", __func__); 618 return B_ERROR; 619 } 620 621 TRACE("%s: table %" B_PRIu8 ".%" B_PRIu8 "\n", __func__, 622 tableMajor, tableMinor); 623 624 // Prepare arguments for AtomBIOS call 625 union adjustPixelClock { 626 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1; 627 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3; 628 }; 629 union adjustPixelClock args; 630 memset(&args, 0, sizeof(args)); 631 632 switch (tableMajor) { 633 case 1: 634 switch (tableMinor) { 635 case 1: 636 case 2: 637 args.v1.usPixelClock 638 = B_HOST_TO_LENDIAN_INT16(pixelClock / 10); 639 args.v1.ucTransmitterID = encoderID; 640 args.v1.ucEncodeMode = encoderMode; 641 if (pll->ssPercentage > 0) { 642 args.v1.ucConfig 643 |= ADJUST_DISPLAY_CONFIG_SS_ENABLE; 644 } 645 646 atom_execute_table(gAtomContext, index, (uint32*)&args); 647 // get returned adjusted clock 648 pll->adjustedClock 649 = B_LENDIAN_TO_HOST_INT16(args.v1.usPixelClock); 650 pll->adjustedClock *= 10; 651 break; 652 case 3: 653 args.v3.sInput.usPixelClock 654 = B_HOST_TO_LENDIAN_INT16(pixelClock / 10); 655 args.v3.sInput.ucTransmitterID = encoderID; 656 args.v3.sInput.ucEncodeMode = encoderMode; 657 args.v3.sInput.ucDispPllConfig = 0; 658 if (pll->ssPercentage > 0) { 659 args.v3.sInput.ucDispPllConfig 660 |= DISPPLL_CONFIG_SS_ENABLE; 661 } 662 663 // Handle DP adjustments 664 if (encoderMode == ATOM_ENCODER_MODE_DP 665 || encoderMode == ATOM_ENCODER_MODE_DP_MST) { 666 TRACE("%s: encoderMode is DP\n", __func__); 667 args.v3.sInput.ucDispPllConfig 668 |= DISPPLL_CONFIG_COHERENT_MODE; 669 /* 162000 or 270000 */ 670 uint32 dpLinkSpeed 671 = dp_get_link_rate(connectorIndex, mode); 672 /* 16200 or 27000 */ 673 args.v3.sInput.usPixelClock 674 = B_HOST_TO_LENDIAN_INT16(dpLinkSpeed / 10); 675 } else if ((connectorFlags & ATOM_DEVICE_DFP_SUPPORT) 676 != 0) { 677 #if 0 678 if (encoderMode == ATOM_ENCODER_MODE_HDMI) { 679 /* deep color support */ 680 args.v3.sInput.usPixelClock = 681 cpu_to_le16((mode->clock * bpc / 8) / 10); 682 } 683 #endif 684 if (pixelClock > 165000) { 685 args.v3.sInput.ucDispPllConfig 686 |= DISPPLL_CONFIG_DUAL_LINK; 687 } 688 if (1) { // dig coherent mode? 689 args.v3.sInput.ucDispPllConfig 690 |= DISPPLL_CONFIG_COHERENT_MODE; 691 } 692 } 693 694 args.v3.sInput.ucExtTransmitterID = externalEncoderID; 695 696 atom_execute_table(gAtomContext, index, (uint32*)&args); 697 698 // get returned adjusted clock 699 pll->adjustedClock = B_LENDIAN_TO_HOST_INT32( 700 args.v3.sOutput.ulDispPllFreq); 701 pll->adjustedClock *= 10; 702 // convert to kHz for storage 703 704 if (args.v3.sOutput.ucRefDiv) { 705 pll->flags |= PLL_USE_FRAC_FB_DIV; 706 pll->flags |= PLL_USE_REF_DIV; 707 pll->referenceDiv = args.v3.sOutput.ucRefDiv; 708 } 709 if (args.v3.sOutput.ucPostDiv) { 710 pll->flags |= PLL_USE_FRAC_FB_DIV; 711 pll->flags |= PLL_USE_POST_DIV; 712 pll->postDiv = args.v3.sOutput.ucPostDiv; 713 } 714 break; 715 default: 716 TRACE("%s: ERROR: table version %" B_PRIu8 ".%" B_PRIu8 717 " unknown\n", __func__, tableMajor, tableMinor); 718 return B_ERROR; 719 } 720 break; 721 default: 722 TRACE("%s: ERROR: table version %" B_PRIu8 ".%" B_PRIu8 723 " unknown\n", __func__, tableMajor, tableMinor); 724 return B_ERROR; 725 } 726 } 727 728 TRACE("%s: was: %" B_PRIu32 ", now: %" B_PRIu32 "\n", __func__, 729 pixelClock, pll->adjustedClock); 730 731 return B_OK; 732 } 733 734 735 status_t 736 pll_set(display_mode* mode, uint8 crtcID) 737 { 738 uint32 connectorIndex = gDisplay[crtcID]->connectorIndex; 739 pll_info* pll = &gConnector[connectorIndex]->encoder.pll; 740 uint32 dp_clock = gConnector[connectorIndex]->dpInfo.linkRate; 741 pll->ssEnabled = false; 742 743 pll->pixelClock = mode->timing.pixel_clock; 744 745 radeon_shared_info &info = *gInfo->shared_info; 746 747 // Probe for PLL spread spectrum info; 748 pll->ssPercentage = 0; 749 pll->ssType = 0; 750 pll->ssStep = 0; 751 pll->ssDelay = 0; 752 pll->ssRange = 0; 753 pll->ssReferenceDiv = 0; 754 755 switch (display_get_encoder_mode(connectorIndex)) { 756 case ATOM_ENCODER_MODE_DP_MST: 757 case ATOM_ENCODER_MODE_DP: 758 if (info.dceMajor >= 4) 759 pll_asic_ss_probe(pll, ASIC_INTERNAL_SS_ON_DP); 760 else { 761 if (dp_clock == 162000) { 762 pll_ppll_ss_probe(pll, ATOM_DP_SS_ID2); 763 if (!pll->ssEnabled) 764 pll_ppll_ss_probe(pll, ATOM_DP_SS_ID1); 765 } else 766 pll_ppll_ss_probe(pll, ATOM_DP_SS_ID1); 767 } 768 break; 769 case ATOM_ENCODER_MODE_LVDS: 770 if (info.dceMajor >= 4) 771 pll_asic_ss_probe(pll, gInfo->lvdsSpreadSpectrumID); 772 else 773 pll_ppll_ss_probe(pll, gInfo->lvdsSpreadSpectrumID); 774 break; 775 case ATOM_ENCODER_MODE_DVI: 776 if (info.dceMajor >= 4) 777 pll_asic_ss_probe(pll, ASIC_INTERNAL_SS_ON_TMDS); 778 break; 779 case ATOM_ENCODER_MODE_HDMI: 780 if (info.dceMajor >= 4) 781 pll_asic_ss_probe(pll, ASIC_INTERNAL_SS_ON_HDMI); 782 break; 783 } 784 785 pll_setup_flags(pll, crtcID); 786 // set up any special flags 787 pll_adjust(pll, mode, crtcID); 788 // get any needed clock adjustments, set reference/post dividers 789 pll_compute(pll); 790 // compute dividers and spread spectrum 791 792 uint8 tableMajor; 793 uint8 tableMinor; 794 795 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock); 796 atom_parse_cmd_header(gAtomContext, index, &tableMajor, &tableMinor); 797 798 TRACE("%s: table %" B_PRIu8 ".%" B_PRIu8 "\n", __func__, 799 tableMajor, tableMinor); 800 801 uint32 bitsPerColor = 8; 802 // TODO: Digital Depth, EDID 1.4+ on digital displays 803 // isn't in Haiku edid common code? 804 805 // Prepare arguments for AtomBIOS call 806 union setPixelClock { 807 SET_PIXEL_CLOCK_PS_ALLOCATION base; 808 PIXEL_CLOCK_PARAMETERS v1; 809 PIXEL_CLOCK_PARAMETERS_V2 v2; 810 PIXEL_CLOCK_PARAMETERS_V3 v3; 811 PIXEL_CLOCK_PARAMETERS_V5 v5; 812 PIXEL_CLOCK_PARAMETERS_V6 v6; 813 }; 814 union setPixelClock args; 815 memset(&args, 0, sizeof(args)); 816 817 switch (tableMinor) { 818 case 1: 819 args.v1.usPixelClock 820 = B_HOST_TO_LENDIAN_INT16(pll->pixelClock / 10); 821 args.v1.usRefDiv = B_HOST_TO_LENDIAN_INT16(pll->referenceDiv); 822 args.v1.usFbDiv = B_HOST_TO_LENDIAN_INT16(pll->feedbackDiv); 823 args.v1.ucFracFbDiv = pll->feedbackDivFrac; 824 args.v1.ucPostDiv = pll->postDiv; 825 args.v1.ucPpll = pll->id; 826 args.v1.ucCRTC = crtcID; 827 args.v1.ucRefDivSrc = 1; 828 break; 829 case 2: 830 args.v2.usPixelClock 831 = B_HOST_TO_LENDIAN_INT16(pll->pixelClock / 10); 832 args.v2.usRefDiv = B_HOST_TO_LENDIAN_INT16(pll->referenceDiv); 833 args.v2.usFbDiv = B_HOST_TO_LENDIAN_INT16(pll->feedbackDiv); 834 args.v2.ucFracFbDiv = pll->feedbackDivFrac; 835 args.v2.ucPostDiv = pll->postDiv; 836 args.v2.ucPpll = pll->id; 837 args.v2.ucCRTC = crtcID; 838 args.v2.ucRefDivSrc = 1; 839 break; 840 case 3: 841 args.v3.usPixelClock 842 = B_HOST_TO_LENDIAN_INT16(pll->pixelClock / 10); 843 args.v3.usRefDiv = B_HOST_TO_LENDIAN_INT16(pll->referenceDiv); 844 args.v3.usFbDiv = B_HOST_TO_LENDIAN_INT16(pll->feedbackDiv); 845 args.v3.ucFracFbDiv = pll->feedbackDivFrac; 846 args.v3.ucPostDiv = pll->postDiv; 847 args.v3.ucPpll = pll->id; 848 args.v3.ucMiscInfo = (pll->id << 2); 849 if (pll->ssPercentage > 0 850 && (pll->ssType & ATOM_EXTERNAL_SS_MASK) != 0) { 851 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC; 852 } 853 args.v3.ucTransmitterId 854 = gConnector[connectorIndex]->encoder.objectID; 855 args.v3.ucEncoderMode = display_get_encoder_mode(connectorIndex); 856 break; 857 case 5: 858 args.v5.ucCRTC = crtcID; 859 args.v5.usPixelClock 860 = B_HOST_TO_LENDIAN_INT16(pll->pixelClock / 10); 861 args.v5.ucRefDiv = pll->referenceDiv; 862 args.v5.usFbDiv = B_HOST_TO_LENDIAN_INT16(pll->feedbackDiv); 863 args.v5.ulFbDivDecFrac 864 = B_HOST_TO_LENDIAN_INT32(pll->feedbackDivFrac * 100000); 865 args.v5.ucPostDiv = pll->postDiv; 866 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */ 867 if (pll->ssPercentage > 0 868 && (pll->ssType & ATOM_EXTERNAL_SS_MASK) != 0) { 869 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC; 870 } 871 switch (bitsPerColor) { 872 case 8: 873 default: 874 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP; 875 break; 876 case 10: 877 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP; 878 break; 879 } 880 args.v5.ucTransmitterID 881 = gConnector[connectorIndex]->encoder.objectID; 882 args.v5.ucEncoderMode 883 = display_get_encoder_mode(connectorIndex); 884 args.v5.ucPpll = pll->id; 885 break; 886 case 6: 887 args.v6.ulDispEngClkFreq 888 = B_HOST_TO_LENDIAN_INT32(crtcID << 24 | pll->pixelClock / 10); 889 args.v6.ucRefDiv = pll->referenceDiv; 890 args.v6.usFbDiv = B_HOST_TO_LENDIAN_INT16(pll->feedbackDiv); 891 args.v6.ulFbDivDecFrac 892 = B_HOST_TO_LENDIAN_INT32(pll->feedbackDivFrac * 100000); 893 args.v6.ucPostDiv = pll->postDiv; 894 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */ 895 if (pll->ssPercentage > 0 896 && (pll->ssType & ATOM_EXTERNAL_SS_MASK) != 0) { 897 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC; 898 } 899 switch (bitsPerColor) { 900 case 8: 901 default: 902 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP; 903 break; 904 case 10: 905 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP; 906 break; 907 case 12: 908 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP; 909 break; 910 case 16: 911 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP; 912 break; 913 } 914 args.v6.ucTransmitterID 915 = gConnector[connectorIndex]->encoder.objectID; 916 args.v6.ucEncoderMode = display_get_encoder_mode(connectorIndex); 917 args.v6.ucPpll = pll->id; 918 break; 919 default: 920 TRACE("%s: ERROR: table version %" B_PRIu8 ".%" B_PRIu8 " TODO\n", 921 __func__, tableMajor, tableMinor); 922 return B_ERROR; 923 } 924 925 TRACE("%s: set adjusted pixel clock %" B_PRIu32 " (was %" B_PRIu32 ")\n", 926 __func__, pll->pixelClock, mode->timing.pixel_clock); 927 928 status_t result = atom_execute_table(gAtomContext, index, (uint32*)&args); 929 930 if (pll->ssEnabled) 931 display_crtc_ss(pll, ATOM_ENABLE); 932 else 933 display_crtc_ss(pll, ATOM_DISABLE); 934 935 return result; 936 } 937 938 939 status_t 940 pll_external_set(uint32 clock) 941 { 942 TRACE("%s: set external pll clock to %" B_PRIu32 "\n", __func__, clock); 943 944 if (clock == 0) 945 ERROR("%s: Warning: default display clock is 0?\n", __func__); 946 947 // also known as PLL display engineering 948 uint8 tableMajor; 949 uint8 tableMinor; 950 951 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock); 952 atom_parse_cmd_header(gAtomContext, index, &tableMajor, &tableMinor); 953 954 TRACE("%s: table %" B_PRIu8 ".%" B_PRIu8 "\n", __func__, 955 tableMajor, tableMinor); 956 957 union setPixelClock { 958 SET_PIXEL_CLOCK_PS_ALLOCATION base; 959 PIXEL_CLOCK_PARAMETERS v1; 960 PIXEL_CLOCK_PARAMETERS_V2 v2; 961 PIXEL_CLOCK_PARAMETERS_V3 v3; 962 PIXEL_CLOCK_PARAMETERS_V5 v5; 963 PIXEL_CLOCK_PARAMETERS_V6 v6; 964 }; 965 union setPixelClock args; 966 memset(&args, 0, sizeof(args)); 967 968 radeon_shared_info &info = *gInfo->shared_info; 969 uint32 dceVersion = (info.dceMajor * 100) + info.dceMinor; 970 switch (tableMajor) { 971 case 1: 972 switch(tableMinor) { 973 case 5: 974 // If the default DC PLL clock is specified, 975 // SetPixelClock provides the dividers. 976 args.v5.ucCRTC = ATOM_CRTC_INVALID; 977 args.v5.usPixelClock = B_HOST_TO_LENDIAN_INT16(clock / 10); 978 args.v5.ucPpll = ATOM_DCPLL; 979 break; 980 case 6: 981 // If the default DC PLL clock is specified, 982 // SetPixelClock provides the dividers. 983 args.v6.ulDispEngClkFreq 984 = B_HOST_TO_LENDIAN_INT32(clock / 10); 985 if (dceVersion == 601) 986 args.v6.ucPpll = ATOM_EXT_PLL1; 987 else if (dceVersion >= 600) 988 args.v6.ucPpll = ATOM_PPLL0; 989 else 990 args.v6.ucPpll = ATOM_DCPLL; 991 break; 992 default: 993 ERROR("%s: Unknown table version %" B_PRIu8 994 ".%" B_PRIu8 "\n", __func__, tableMajor, tableMinor); 995 } 996 break; 997 default: 998 ERROR("%s: Unknown table version %" B_PRIu8 999 ".%" B_PRIu8 "\n", __func__, tableMajor, tableMinor); 1000 } 1001 return B_OK; 1002 } 1003 1004 1005 void 1006 pll_external_init() 1007 { 1008 radeon_shared_info &info = *gInfo->shared_info; 1009 1010 if (info.dceMajor >= 6) { 1011 pll_external_set(gInfo->displayClockFrequency); 1012 } else if (info.dceMajor >= 4) { 1013 // Create our own pseudo pll 1014 pll_info pll; 1015 pll_asic_ss_probe(&pll, ASIC_INTERNAL_SS_ON_DCPLL); 1016 if (pll.ssEnabled) 1017 display_crtc_ss(&pll, ATOM_DISABLE); 1018 pll_external_set(gInfo->displayClockFrequency); 1019 if (pll.ssEnabled) 1020 display_crtc_ss(&pll, ATOM_ENABLE); 1021 } 1022 } 1023 1024 /** 1025 * pll_usage_mask - Calculate which PLL's are in use 1026 * 1027 * Returns the mask of which PLL's are in use 1028 */ 1029 uint32 1030 pll_usage_mask() 1031 { 1032 uint32 pllMask = 0; 1033 for (uint32 id = 0; id < ATOM_MAX_SUPPORTED_DEVICE; id++) { 1034 if (gConnector[id]->valid == true) { 1035 pll_info* pll = &gConnector[id]->encoder.pll; 1036 if (pll->id != ATOM_PPLL_INVALID) 1037 pllMask |= (1 << pll->id); 1038 } 1039 } 1040 return pllMask; 1041 } 1042 1043 1044 /** 1045 * pll_usage_count - Find number of connectors attached to a PLL 1046 * 1047 * Returns the count of connectors using specified PLL 1048 */ 1049 uint32 1050 pll_usage_count(uint32 pllID) 1051 { 1052 uint32 pllCount = 0; 1053 for (uint32 id = 0; id < ATOM_MAX_SUPPORTED_DEVICE; id++) { 1054 if (gConnector[id]->valid == true) { 1055 pll_info* pll = &gConnector[id]->encoder.pll; 1056 if (pll->id == pllID) 1057 pllCount++; 1058 } 1059 } 1060 1061 return pllCount; 1062 } 1063 1064 1065 /** 1066 * pll_shared_dp - Find any existing PLL's used for DP connectors 1067 * 1068 * Returns the PLL shared by other DP connectors 1069 */ 1070 uint32 1071 pll_shared_dp() 1072 { 1073 for (uint32 id = 0; id < ATOM_MAX_SUPPORTED_DEVICE; id++) { 1074 if (gConnector[id]->valid == true) { 1075 if (connector_is_dp(id)) { 1076 pll_info* pll = &gConnector[id]->encoder.pll; 1077 return pll->id; 1078 } 1079 } 1080 } 1081 return ATOM_PPLL_INVALID; 1082 } 1083 1084 1085 /** 1086 * pll_next_available - Find the next available PLL 1087 * 1088 * Returns the next available PLL 1089 */ 1090 uint32 1091 pll_next_available() 1092 { 1093 radeon_shared_info &info = *gInfo->shared_info; 1094 uint32 dceVersion = (info.dceMajor * 100) + info.dceMinor; 1095 1096 uint32 pllMask = pll_usage_mask(); 1097 1098 if (dceVersion == 802 || dceVersion == 601) { 1099 if (!(pllMask & (1 << ATOM_PPLL0))) 1100 return ATOM_PPLL0; 1101 } 1102 1103 if (!(pllMask & (1 << ATOM_PPLL1))) 1104 return ATOM_PPLL1; 1105 if (dceVersion != 601) { 1106 if (!(pllMask & (1 << ATOM_PPLL2))) 1107 return ATOM_PPLL2; 1108 } 1109 // TODO: If this starts happening, we likely need to 1110 // add the sharing of PLL's with identical clock rates 1111 // (see radeon_atom_pick_pll in drm) 1112 ERROR("%s: Unable to find a PLL! (0x%" B_PRIX32 ")\n", __func__, pllMask); 1113 return ATOM_PPLL_INVALID; 1114 } 1115 1116 1117 status_t 1118 pll_pick(uint32 connectorIndex) 1119 { 1120 pll_info* pll = &gConnector[connectorIndex]->encoder.pll; 1121 radeon_shared_info &info = *gInfo->shared_info; 1122 uint32 dceVersion = (info.dceMajor * 100) + info.dceMinor; 1123 1124 bool linkB = gConnector[connectorIndex]->encoder.linkEnumeration 1125 == GRAPH_OBJECT_ENUM_ID2 ? true : false; 1126 1127 pll->id = ATOM_PPLL_INVALID; 1128 1129 // DCE 6.1 APU, UNIPHYA requires PLL2 1130 if (gConnector[connectorIndex]->encoder.objectID 1131 == ENCODER_OBJECT_ID_INTERNAL_UNIPHY && !linkB) { 1132 pll->id = ATOM_PPLL2; 1133 return B_OK; 1134 } 1135 1136 if (connector_is_dp(connectorIndex)) { 1137 // If DP external clock, set to invalid except on DCE 6.1 1138 if (gInfo->dpExternalClock && !(dceVersion == 601)) { 1139 pll->id = ATOM_PPLL_INVALID; 1140 return B_OK; 1141 } 1142 1143 // DCE 6.1+, we can share DP PLLs. See if any other DP connectors 1144 // have been assigned a PLL yet. 1145 if (dceVersion >= 601) { 1146 pll->id = pll_shared_dp(); 1147 if (pll->id != ATOM_PPLL_INVALID) 1148 return B_OK; 1149 // Continue through to pll_next_available 1150 } else if (dceVersion == 600) { 1151 pll->id = ATOM_PPLL0; 1152 return B_OK; 1153 } else if (info.dceMajor >= 5) { 1154 pll->id = ATOM_DCPLL; 1155 return B_OK; 1156 } 1157 } 1158 1159 if (info.dceMajor >= 4) { 1160 pll->id = pll_next_available(); 1161 return B_OK; 1162 } 1163 1164 // TODO: Should return the CRTCID here. 1165 pll->id = ATOM_PPLL1; 1166 return B_OK; 1167 } 1168