xref: /haiku/src/add-ons/accelerants/intel_extreme/Ports.cpp (revision 9e25244c5e9051f6cd333820d6332397361abd6c)
1 /*
2  * Copyright 2006-2015, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  *		Michael Lotz, mmlr@mlotz.ch
8  *		Alexander von Gluck IV, kallisti5@unixzen.com
9  *		Rudolf Cornelissen, ruud@highsand-juicylake.nl
10  */
11 
12 
13 #include "Ports.h"
14 
15 #include <ddc.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <Debug.h>
19 #include <KernelExport.h>
20 
21 #include "accelerant.h"
22 #include "accelerant_protos.h"
23 #include "FlexibleDisplayInterface.h"
24 #include "intel_extreme.h"
25 #include "PanelFitter.h"
26 
27 #include <new>
28 
29 
30 #undef TRACE
31 #define TRACE_PORTS
32 #ifdef TRACE_PORTS
33 #   define TRACE(x...) _sPrintf("intel_extreme: " x)
34 #else
35 #   define TRACE(x...)
36 #endif
37 
38 #define ERROR(x...) _sPrintf("intel_extreme: " x)
39 #define CALLED(x...) TRACE("CALLED %s\n", __PRETTY_FUNCTION__)
40 
41 
42 static bool
43 wait_for_set(addr_t address, uint32 mask, uint32 timeout)
44 {
45 	int interval = 50;
46 	uint32 i = 0;
47 	for(i = 0; i <= timeout; i += interval) {
48 		spin(interval);
49 		if ((read32(address) & mask) != 0)
50 			return true;
51 	}
52 	return false;
53 }
54 
55 
56 static bool
57 wait_for_clear(addr_t address, uint32 mask, uint32 timeout)
58 {
59 	int interval = 50;
60 	uint32 i = 0;
61 	for(i = 0; i <= timeout; i += interval) {
62 		spin(interval);
63 		if ((read32(address) & mask) == 0)
64 			return true;
65 	}
66 	return false;
67 }
68 
69 
70 Port::Port(port_index index, const char* baseName)
71 	:
72 	fPipe(NULL),
73 	fEDIDState(B_NO_INIT),
74 	fPortIndex(index),
75 	fPortName(NULL)
76 {
77 	char portID[2];
78 	portID[0] = 'A' + index - INTEL_PORT_A;
79 	portID[1] = 0;
80 
81 	char buffer[32];
82 	buffer[0] = 0;
83 
84 	strlcat(buffer, baseName, sizeof(buffer));
85 	strlcat(buffer, " ", sizeof(buffer));
86 	strlcat(buffer, portID, sizeof(buffer));
87 	fPortName = strdup(buffer);
88 }
89 
90 
91 Port::~Port()
92 {
93 	free(fPortName);
94 }
95 
96 
97 bool
98 Port::HasEDID()
99 {
100 	if (fEDIDState == B_NO_INIT)
101 		GetEDID(NULL);
102 
103 	return fEDIDState == B_OK;
104 }
105 
106 
107 status_t
108 Port::SetPipe(Pipe* pipe)
109 {
110 	CALLED();
111 
112 	if (pipe == NULL) {
113 		ERROR("%s: Invalid pipe provided!\n", __func__);
114 		return B_ERROR;
115 	}
116 
117 	uint32 portRegister = _PortRegister();
118 	if (portRegister == 0) {
119 		ERROR("%s: Invalid PortRegister ((0x%" B_PRIx32 ") for %s\n", __func__,
120 			portRegister, PortName());
121 		return B_ERROR;
122 	}
123 
124 	// TODO: UnAssignPipe?  This likely needs reworked a little
125 	if (fPipe != NULL) {
126 		ERROR("%s: Can't reassign display pipe (yet)\n", __func__);
127 		return B_ERROR;
128 	}
129 
130 	switch (pipe->Index()) {
131 		case INTEL_PIPE_B:
132 			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe B\n", __func__,
133 				PortName(), portRegister);
134 			break;
135 		case INTEL_PIPE_C:
136 			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe C\n", __func__,
137 				PortName(), portRegister);
138 			break;
139 		case INTEL_PIPE_D:
140 			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe D\n", __func__,
141 				PortName(), portRegister);
142 			break;
143 		default:
144 			TRACE("%s: Assigning %s (0x%" B_PRIx32 ") to pipe A\n", __func__,
145 				PortName(), portRegister);
146 			break;
147 	}
148 
149 	uint32 portState = read32(portRegister);
150 
151 	// generation 6 gfx SandyBridge/SNB non-DP use the same 2 bits on all ports (eDP = 1 bit).
152 	// generation 7 gfx IvyBridge/IVB non-DP use the same 2 bits on all ports (eDP = 2 bits).
153 	// DP ports/all DDI ports: Pipe selections works differently, via own SetPipe() implementation.
154 	if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
155 		portState &= ~PORT_TRANS_SEL_MASK;
156 		switch (pipe->Index()) {
157 			case INTEL_PIPE_B:
158 				write32(portRegister, portState | PORT_TRANS_B_SEL_CPT);
159 				break;
160 			case INTEL_PIPE_C:
161 				write32(portRegister, portState | PORT_TRANS_C_SEL_CPT);
162 				break;
163 			default:
164 				write32(portRegister, portState | PORT_TRANS_A_SEL_CPT);
165 				break;
166 		}
167 	} else {
168 		// generation 3/4/5 gfx uses the same single bit on all ports
169 		if (pipe->Index() == INTEL_PIPE_A)
170 			write32(portRegister, portState & ~DISPLAY_MONITOR_PIPE_B);
171 		else
172 			write32(portRegister, portState | DISPLAY_MONITOR_PIPE_B);
173 	}
174 	fPipe = pipe;
175 
176 	if (fPipe == NULL)
177 		return B_NO_MEMORY;
178 
179 	// Disable display pipe until modesetting enables it
180 	if (fPipe->IsEnabled())
181 		fPipe->Enable(false);
182 
183 	read32(portRegister);
184 
185 	return B_OK;
186 }
187 
188 
189 status_t
190 Port::Power(bool enabled)
191 {
192 	if (fPipe == NULL) {
193 		ERROR("%s: Setting power mode without assigned pipe!\n", __func__);
194 		return B_ERROR;
195 	}
196 
197 	fPipe->Enable(enabled);
198 
199 	return B_OK;
200 }
201 
202 
203 status_t
204 Port::GetEDID(edid1_info* edid, bool forceRead)
205 {
206 	CALLED();
207 
208 	if (fEDIDState == B_NO_INIT || forceRead) {
209 		TRACE("%s: trying to read EDID\n", PortName());
210 
211 		addr_t ddcRegister = _DDCRegister();
212 		if (ddcRegister == 0) {
213 			TRACE("%s: no DDC register found\n", PortName());
214 			fEDIDState = B_ERROR;
215 			return fEDIDState;
216 		}
217 
218 		TRACE("%s: using ddc @ 0x%" B_PRIxADDR "\n", PortName(), ddcRegister);
219 
220 		i2c_bus bus;
221 		bus.cookie = (void*)ddcRegister;
222 		bus.set_signals = &_SetI2CSignals;
223 		bus.get_signals = &_GetI2CSignals;
224 		ddc2_init_timing(&bus);
225 
226 		fEDIDState = ddc2_read_edid1(&bus, &fEDIDInfo, NULL, NULL);
227 
228 		if (fEDIDState == B_OK) {
229 			TRACE("%s: found EDID information!\n", PortName());
230 			edid_dump(&fEDIDInfo);
231 		}
232 	}
233 
234 	if (fEDIDState != B_OK) {
235 		TRACE("%s: no EDID information found.\n", PortName());
236 		return fEDIDState;
237 	}
238 
239 	if (edid != NULL)
240 		memcpy(edid, &fEDIDInfo, sizeof(edid1_info));
241 
242 	return B_OK;
243 }
244 
245 
246 status_t
247 Port::GetPLLLimits(pll_limits& limits)
248 {
249 	return B_ERROR;
250 }
251 
252 
253 pipe_index
254 Port::PipePreference()
255 {
256 	CALLED();
257 	// Ideally we could just return INTEL_PIPE_ANY for all devices by default, but
258 	// this doesn't quite work yet. We need to use the BIOS presetup pipes for now.
259 	if (gInfo->shared_info->device_type.Generation() < 4)
260 		return INTEL_PIPE_ANY;
261 
262 	// Notes:
263 	// - The BIOSes seen sofar do not use PIPE C by default.
264 	// - The BIOSes seen sofar program transcoder A to PIPE A, etc.
265 	// - Later devices add a pipe C alongside the added transcoder C.
266 
267 	if ((gInfo->shared_info->device_type.Generation() <= 7) &&
268 		(!gInfo->shared_info->device_type.HasDDI())) {
269 		uint32 portState = read32(_PortRegister());
270 		if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
271 			portState &= PORT_TRANS_SEL_MASK;
272 			if (portState == PORT_TRANS_B_SEL_CPT)
273 				return INTEL_PIPE_B;
274 			else
275 				return INTEL_PIPE_A;
276 		} else {
277 			if (portState & DISPLAY_MONITOR_PIPE_B)
278 				return INTEL_PIPE_B;
279 			else
280 				return INTEL_PIPE_A;
281 		}
282 	}
283 
284 	if (gInfo->shared_info->device_type.HasDDI()) {
285 		// scan all our pipes to find the one connected to the current port
286 		uint32 pipeState = 0;
287 		for (uint32 pipeCnt = 0; pipeCnt < 4; pipeCnt++) {
288 			switch (pipeCnt) {
289 				case 0:
290 					pipeState = read32(PIPE_DDI_FUNC_CTL_A);
291 					break;
292 				case 1:
293 					pipeState = read32(PIPE_DDI_FUNC_CTL_B);
294 					break;
295 				case 2:
296 					pipeState = read32(PIPE_DDI_FUNC_CTL_C);
297 					break;
298 				default:
299 					pipeState = read32(PIPE_DDI_FUNC_CTL_EDP);
300 					break;
301 			}
302 
303 			if ((((pipeState & PIPE_DDI_SELECT_MASK) >> PIPE_DDI_SELECT_SHIFT) + 1)
304 				== (uint32)PortIndex()) {
305 				switch (pipeCnt) {
306 					case 0:
307 						return INTEL_PIPE_A;
308 					case 1:
309 						return INTEL_PIPE_B;
310 					case 2:
311 						return INTEL_PIPE_C;
312 					default:
313 						return INTEL_PIPE_D;
314 				}
315 			}
316 		}
317 	}
318 
319 	return INTEL_PIPE_ANY;
320 }
321 
322 
323 status_t
324 Port::_GetI2CSignals(void* cookie, int* _clock, int* _data)
325 {
326 	addr_t ioRegister = (addr_t)cookie;
327 	uint32 value = read32(ioRegister);
328 
329 	*_clock = (value & I2C_CLOCK_VALUE_IN) != 0;
330 	*_data = (value & I2C_DATA_VALUE_IN) != 0;
331 
332 	return B_OK;
333 }
334 
335 
336 status_t
337 Port::_SetI2CSignals(void* cookie, int clock, int data)
338 {
339 	addr_t ioRegister = (addr_t)cookie;
340 	uint32 value;
341 
342 	if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_83x)) {
343 		// on these chips, the reserved values are fixed
344 		value = 0;
345 	} else {
346 		// on all others, we have to preserve them manually
347 		value = read32(ioRegister) & I2C_RESERVED;
348 	}
349 
350 	// if we send clk or data, we always send low logic level;
351 	// if we want to send high level, we actually receive and let the
352 	// external pullup resistors create the high level on the bus.
353 	value |= I2C_DATA_VALUE_MASK;  //sets data = 0, always latch
354 	value |= I2C_CLOCK_VALUE_MASK; //sets clock = 0, always latch
355 
356 	if (data != 0)
357 		value |= I2C_DATA_DIRECTION_MASK;
358 	else {
359 		value |= I2C_DATA_DIRECTION_MASK | I2C_DATA_DIRECTION_OUT;
360 	}
361 
362 	if (clock != 0)
363 		value |= I2C_CLOCK_DIRECTION_MASK;
364 	else {
365 		value |= I2C_CLOCK_DIRECTION_MASK | I2C_CLOCK_DIRECTION_OUT;
366 	}
367 
368 	write32(ioRegister, value);
369 	read32(ioRegister);
370 		// make sure the PCI bus has flushed the write
371 
372 	return B_OK;
373 }
374 
375 
376 // #pragma mark - Analog Port
377 
378 
379 AnalogPort::AnalogPort()
380 	:
381 	Port(INTEL_PORT_A, "Analog")
382 {
383 }
384 
385 
386 bool
387 AnalogPort::IsConnected()
388 {
389 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
390 		_PortRegister());
391 	return HasEDID();
392 }
393 
394 
395 addr_t
396 AnalogPort::_DDCRegister()
397 {
398 	// always fixed
399 	return INTEL_I2C_IO_A;
400 }
401 
402 
403 addr_t
404 AnalogPort::_PortRegister()
405 {
406 	// always fixed
407 	return INTEL_ANALOG_PORT;
408 }
409 
410 
411 status_t
412 AnalogPort::SetDisplayMode(display_mode* target, uint32 colorMode)
413 {
414 	CALLED();
415 	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
416 		target->timing.v_display);
417 
418 	if (fPipe == NULL) {
419 		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
420 		return B_ERROR;
421 	}
422 
423 	// Setup PanelFitter and Train FDI if it exists
424 	PanelFitter* fitter = fPipe->PFT();
425 	if (fitter != NULL)
426 		fitter->Enable(target->timing);
427 	FDILink* link = fPipe->FDI();
428 	if (link != NULL) {
429 		uint32 lanes = 0;
430 		uint32 linkBandwidth = 0;
431 		uint32 bitsPerPixel = 0;
432 		link->PreTrain(&target->timing, &linkBandwidth, &lanes, &bitsPerPixel);
433 		fPipe->SetFDILink(target->timing, linkBandwidth, lanes, bitsPerPixel);
434 		link->Train(&target->timing, lanes);
435 	}
436 	pll_divisors divisors;
437 	compute_pll_divisors(&target->timing, &divisors, false);
438 
439 	uint32 extraPLLFlags = 0;
440 	if (gInfo->shared_info->device_type.Generation() >= 3)
441 		extraPLLFlags |= DISPLAY_PLL_MODE_NORMAL;
442 
443 	// Program general pipe config
444 	fPipe->Configure(target);
445 
446 	// Program pipe PLL's
447 	fPipe->ConfigureClocks(divisors, target->timing.pixel_clock, extraPLLFlags);
448 
449 	write32(_PortRegister(), (read32(_PortRegister())
450 		& ~(DISPLAY_MONITOR_POLARITY_MASK | DISPLAY_MONITOR_VGA_POLARITY))
451 		| ((target->timing.flags & B_POSITIVE_HSYNC) != 0
452 			? DISPLAY_MONITOR_POSITIVE_HSYNC : 0)
453 		| ((target->timing.flags & B_POSITIVE_VSYNC) != 0
454 			? DISPLAY_MONITOR_POSITIVE_VSYNC : 0));
455 
456 	// Program target display mode
457 	fPipe->ConfigureTimings(target);
458 
459 	// Set fCurrentMode to our set display mode
460 	memcpy(&fCurrentMode, target, sizeof(display_mode));
461 
462 	return B_OK;
463 }
464 
465 
466 // #pragma mark - LVDS Panel
467 
468 
469 LVDSPort::LVDSPort()
470 	:
471 	Port(INTEL_PORT_C, "LVDS")
472 {
473 	// Always unlock LVDS port as soon as we start messing with it.
474 	uint32 panelControl = INTEL_PANEL_CONTROL;
475 	if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) {
476 		// FIXME writing there results in black screen on SandyBridge
477 		return;
478 		// panelControl = PCH_PANEL_CONTROL;
479 	}
480 	write32(panelControl, read32(panelControl) | PANEL_REGISTER_UNLOCK);
481 }
482 
483 
484 pipe_index
485 LVDSPort::PipePreference()
486 {
487 	CALLED();
488 	// Older devices have hardcoded pipe/port mappings, so just use that
489 	if (gInfo->shared_info->device_type.Generation() < 4)
490 		return INTEL_PIPE_B;
491 
492 	// Ideally we could just return INTEL_PIPE_ANY for the newer devices, but
493 	// this doesn't quite work yet.
494 
495 	// On SandyBridge and later, there is a transcoder C. On SandyBridge at least
496 	// that can't be used by the LVDS port (but A and B would be fine).
497 	// On Ibex Point, SandyBridge and IvyBridge (tested) changing pipes does not
498 	// work yet.
499 	// Notes:
500 	// - Switching Pipes only works reliably when a 'full modeswitch' is executed
501 	//   (FDI training) so we have to reuse the BIOS preset setup always for now.
502 	// - The BIOSes seen sofar do not use PIPE C by default.
503 	// - The BIOSes seen sofar program transcoder A to PIPE A, etc.
504 	// - Later devices add a pipe C alongside the added transcoder C.
505 
506 	// FIXME How's this setup in newer gens? Currently return Pipe B fixed there..
507 	if (gInfo->shared_info->device_type.Generation() <= 7) {
508 		uint32 portState = read32(_PortRegister());
509 		if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
510 			portState &= PORT_TRANS_SEL_MASK;
511 			if (portState == PORT_TRANS_B_SEL_CPT)
512 				return INTEL_PIPE_B;
513 			else
514 				return INTEL_PIPE_A;
515 		} else {
516 			if (portState & DISPLAY_MONITOR_PIPE_B)
517 				return INTEL_PIPE_B;
518 			else
519 				return INTEL_PIPE_A;
520 		}
521 	}
522 
523 	return INTEL_PIPE_B;
524 }
525 
526 
527 bool
528 LVDSPort::IsConnected()
529 {
530 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
531 		_PortRegister());
532 
533 	if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) {
534 		uint32 registerValue = read32(_PortRegister());
535 		// there's a detection bit we can use
536 		if ((registerValue & PCH_LVDS_DETECTED) == 0) {
537 			TRACE("LVDS: Not detected\n");
538 			return false;
539 		}
540 		// TODO: Skip if eDP support
541 	} else if (gInfo->shared_info->device_type.Generation() <= 4) {
542 		// Older generations don't have LVDS detection. If not mobile skip.
543 		if (!gInfo->shared_info->device_type.IsMobile()) {
544 			TRACE("LVDS: Skipping LVDS detection due to gen and not mobile\n");
545 			return false;
546 		}
547 		// If mobile, try to grab EDID
548 		// Linux seems to look at lid status for LVDS port detection
549 		// If we don't get EDID, we can use vbios native mode or vesa?
550 		if (!HasEDID()) {
551 			if (gInfo->shared_info->has_vesa_edid_info) {
552 				TRACE("LVDS: Using VESA edid info\n");
553 				memcpy(&fEDIDInfo, &gInfo->shared_info->vesa_edid_info,
554 					sizeof(edid1_info));
555 				if (fEDIDState != B_OK) {
556 					fEDIDState = B_OK;
557 					// HasEDID now true
558 					edid_dump(&fEDIDInfo);
559 				}
560 			} else if (gInfo->shared_info->got_vbt) {
561 				TRACE("LVDS: No EDID, but force enabled as we have a VBT\n");
562 				return true;
563 			} else {
564 				TRACE("LVDS: Couldn't find any valid EDID!\n");
565 				return false;
566 			}
567 		}
568 	}
569 
570 	// Try getting EDID, as the LVDS port doesn't overlap with anything else,
571 	// we don't run the risk of getting someone else's data.
572 	return HasEDID();
573 }
574 
575 
576 addr_t
577 LVDSPort::_DDCRegister()
578 {
579 	// always fixed
580 	return INTEL_I2C_IO_C;
581 }
582 
583 
584 addr_t
585 LVDSPort::_PortRegister()
586 {
587 	// always fixed
588 	return INTEL_DIGITAL_LVDS_PORT;
589 }
590 
591 
592 status_t
593 LVDSPort::SetDisplayMode(display_mode* target, uint32 colorMode)
594 {
595 	CALLED();
596 	if (target == NULL) {
597 		ERROR("%s: Invalid target mode passed!\n", __func__);
598 		return B_ERROR;
599 	}
600 
601 	TRACE("%s: %s-%d %dx%d\n", __func__, PortName(), PortIndex(),
602 		target->timing.h_display, target->timing.v_display);
603 
604 	if (fPipe == NULL) {
605 		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
606 		return B_ERROR;
607 	}
608 
609 	addr_t panelControl = INTEL_PANEL_CONTROL;
610 	addr_t panelStatus = INTEL_PANEL_STATUS;
611 	if (gInfo->shared_info->pch_info != INTEL_PCH_NONE) {
612 		panelControl = PCH_PANEL_CONTROL;
613 		panelStatus = PCH_PANEL_STATUS;
614 	}
615 
616 	if (gInfo->shared_info->device_type.Generation() != 4) {
617 		// TODO not needed on any generation if we are using the panel fitter
618 		// Power off Panel
619 		write32(panelControl,
620 			read32(panelControl) & ~PANEL_CONTROL_POWER_TARGET_ON);
621 		read32(panelControl);
622 
623 		if (!wait_for_clear(panelStatus, PANEL_STATUS_POWER_ON, 1000)) {
624 			ERROR("%s: %s didn't power off within 1000ms!\n", __func__,
625 				PortName());
626 		}
627 	}
628 
629 	// For LVDS panels, we may need to set the timings according to the panel
630 	// native video mode, and let the panel fitter do the scaling. But the
631 	// place where the scaling happens varies accross generations of devices.
632 	display_timing hardwareTarget;
633 	bool needsScaling = false;
634 
635 	// TODO figure out how it's done (or if we need to configure something at
636 	// all) for other generations
637 	if (gInfo->shared_info->device_type.Generation() <= 6
638 		&& gInfo->shared_info->device_type.Generation() >= 3
639 		&& gInfo->shared_info->got_vbt) {
640 		// Set vbios hardware panel mode as base
641 		hardwareTarget = gInfo->shared_info->panel_timing;
642 
643 		if (hardwareTarget.h_display == target->timing.h_display
644 				&& hardwareTarget.v_display == target->timing.v_display) {
645 			// We are setting the native video mode, nothing special to do
646 			// Note: this means refresh and timing might vary according to requested mode.
647 			hardwareTarget = target->timing;
648 			TRACE("%s: Setting LVDS to native resolution at %" B_PRIu32 "Hz\n", __func__,
649 				hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
650 		} else {
651 			// We need to enable the panel fitter
652 			TRACE("%s: Hardware mode will actually be %dx%d at %" B_PRIu32 "Hz\n", __func__,
653 				hardwareTarget.h_display, hardwareTarget.v_display,
654 				hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
655 
656 			// FIXME we should also get the refresh frequency from the target
657 			// mode, and then "sanitize" the resulting mode we made up.
658 			needsScaling = true;
659 		}
660 	} else {
661 		TRACE("Setting LVDS mode without VBT info or on unhandled hardware "
662 			"generation, scaling may not work\n");
663 		// We don't have VBT data, try to set the requested mode directly
664 		// and hope for the best
665 		hardwareTarget = target->timing;
666 	}
667 
668 	// Setup PanelFitter and Train FDI if it exists
669 	PanelFitter* fitter = fPipe->PFT();
670 	if (fitter != NULL)
671 		fitter->Enable(hardwareTarget);
672 	FDILink* link = fPipe->FDI();
673 	if (link != NULL) {
674 		uint32 lanes = 0;
675 		uint32 linkBandwidth = 0;
676 		uint32 bitsPerPixel = 0;
677 		link->PreTrain(&hardwareTarget, &linkBandwidth, &lanes, &bitsPerPixel);
678 		fPipe->SetFDILink(hardwareTarget, linkBandwidth, lanes, bitsPerPixel);
679 		link->Train(&hardwareTarget, lanes);
680 	}
681 
682 	pll_divisors divisors;
683 	compute_pll_divisors(&hardwareTarget, &divisors, true);
684 
685 	uint32 lvds = read32(_PortRegister())
686 		| LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
687 
688 	if (gInfo->shared_info->device_type.Generation() == 4) {
689 		// LVDS_A3_POWER_UP == 24bpp
690 		// otherwise, 18bpp
691 		if ((lvds & LVDS_A3_POWER_MASK) != LVDS_A3_POWER_UP)
692 			lvds |= LVDS_18BIT_DITHER;
693 	}
694 
695 	// LVDS on PCH needs set before display enable
696 	if (gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
697 		lvds &= ~PORT_TRANS_SEL_MASK;
698 		if (fPipe->Index() == INTEL_PIPE_A)
699 			lvds |= PORT_TRANS_A_SEL_CPT;
700 		else
701 			lvds |= PORT_TRANS_B_SEL_CPT;
702 	}
703 
704 	// Set the B0-B3 data pairs corresponding to whether we're going to
705 	// set the DPLLs for dual-channel mode or not.
706 	if (divisors.p2 == 5 || divisors.p2 == 7) {
707 		TRACE("LVDS: dual channel\n");
708 		lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
709 	} else {
710 		TRACE("LVDS: single channel\n");
711 		lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
712 	}
713 
714 	// LVDS port control moves polarity bits because Intel hates you.
715 	// Set LVDS sync polarity
716 	lvds &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
717 
718 	// set on - polarity.
719 	if ((target->timing.flags & B_POSITIVE_HSYNC) == 0)
720 		lvds |= LVDS_HSYNC_POLARITY;
721 	if ((target->timing.flags & B_POSITIVE_VSYNC) == 0)
722 		lvds |= LVDS_VSYNC_POLARITY;
723 
724 	TRACE("%s: LVDS Write: 0x%" B_PRIx32 "\n", __func__, lvds);
725 	write32(_PortRegister(), lvds);
726 	read32(_PortRegister());
727 
728 	uint32 extraPLLFlags = 0;
729 
730 	// DPLL mode LVDS for i915+
731 	if (gInfo->shared_info->device_type.Generation() >= 3)
732 		extraPLLFlags |= DISPLAY_PLL_MODE_LVDS | DISPLAY_PLL_2X_CLOCK;
733 
734 	// Program general pipe config
735 	fPipe->Configure(target);
736 
737 	// Program pipe PLL's (using the hardware mode timings, since that's what
738 	// the PLL is used for)
739 	fPipe->ConfigureClocks(divisors, hardwareTarget.pixel_clock,
740 		extraPLLFlags);
741 
742 	if (gInfo->shared_info->device_type.Generation() != 4) {
743 		// G45: no need to power the panel off
744 		// Power on Panel
745 		write32(panelControl,
746 			read32(panelControl) | PANEL_CONTROL_POWER_TARGET_ON);
747 		read32(panelControl);
748 
749 		if (!wait_for_set(panelStatus, PANEL_STATUS_POWER_ON, 1000)) {
750 			ERROR("%s: %s didn't power on within 1000ms!\n", __func__,
751 				PortName());
752 		}
753 	}
754 
755 	// Program target display mode
756 	fPipe->ConfigureTimings(target, !needsScaling);
757 
758 	if (needsScaling) {
759 		if (gInfo->shared_info->device_type.Generation() <= 4) {
760 			// Enable panel fitter in automatic mode. It will figure out
761 			// the scaling ratios automatically.
762 			uint32 panelFitterControl = read32(INTEL_PANEL_FIT_CONTROL);
763 			panelFitterControl |= PANEL_FITTER_ENABLED;
764 			panelFitterControl &= ~(PANEL_FITTER_SCALING_MODE_MASK
765 				| PANEL_FITTER_PIPE_MASK);
766 			panelFitterControl |= PANEL_FITTER_PIPE_B;
767 			// LVDS is always on pipe B.
768 			write32(INTEL_PANEL_FIT_CONTROL, panelFitterControl);
769 		}
770 		// TODO do we need to do anything on later generations?
771 	} else {
772 		if (gInfo->shared_info->device_type.Generation() == 4
773 			|| gInfo->shared_info->device_type.Generation() == 3) {
774 			// Bypass the panel fitter
775 			uint32 panelFitterControl = read32(INTEL_PANEL_FIT_CONTROL);
776 			panelFitterControl &= ~PANEL_FITTER_ENABLED;
777 			write32(INTEL_PANEL_FIT_CONTROL, panelFitterControl);
778 		} else {
779 			// We don't need to do anything more for later generations, the
780 			// scaling is handled at the transcoder level. We may want to
781 			// configure dithering, but the code below ignores the previous
782 			// value in the register and may mess things up so we should do
783 			// this in a safeer way. For now, assume the BIOS did the right
784 			// thing.
785 #if 0
786 			// Disable panel fitting, but enable 8 to 6-bit dithering
787 			write32(INTEL_PANEL_FIT_CONTROL, 0x4);
788 				// TODO: do not do this if the connected panel is 24-bit
789 				// (I don't know how to detect that)
790 #endif
791 		}
792 	}
793 
794 	// Set fCurrentMode to our set display mode
795 	memcpy(&fCurrentMode, target, sizeof(display_mode));
796 
797 	return B_OK;
798 }
799 
800 
801 // #pragma mark - DVI/SDVO/generic
802 
803 
804 DigitalPort::DigitalPort(port_index index, const char* baseName)
805 	:
806 	Port(index, baseName)
807 {
808 }
809 
810 
811 bool
812 DigitalPort::IsConnected()
813 {
814 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
815 		_PortRegister());
816 
817 	// As this port overlaps with pretty much everything, this must be called
818 	// after having ruled out all other port types.
819 	return HasEDID();
820 }
821 
822 
823 addr_t
824 DigitalPort::_DDCRegister()
825 {
826 	//TODO: IS BROXTON, B = B, C = C, D = NIL
827 	switch (PortIndex()) {
828 		case INTEL_PORT_B:
829 			return INTEL_I2C_IO_E;
830 		case INTEL_PORT_C:
831 			return INTEL_I2C_IO_D;
832 		case INTEL_PORT_D:
833 			return INTEL_I2C_IO_F;
834 		default:
835 			return 0;
836 	}
837 
838 	return 0;
839 }
840 
841 
842 addr_t
843 DigitalPort::_PortRegister()
844 {
845 	switch (PortIndex()) {
846 		case INTEL_PORT_A:
847 			return INTEL_DIGITAL_PORT_A;
848 		case INTEL_PORT_B:
849 			return INTEL_DIGITAL_PORT_B;
850 		case INTEL_PORT_C:
851 			return INTEL_DIGITAL_PORT_C;
852 		default:
853 			return 0;
854 	}
855 	return 0;
856 }
857 
858 
859 status_t
860 DigitalPort::SetDisplayMode(display_mode* target, uint32 colorMode)
861 {
862 	CALLED();
863 	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
864 		target->timing.v_display);
865 
866 	if (fPipe == NULL) {
867 		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
868 		return B_ERROR;
869 	}
870 
871 	// Setup PanelFitter and Train FDI if it exists
872 	PanelFitter* fitter = fPipe->PFT();
873 	if (fitter != NULL)
874 		fitter->Enable(target->timing);
875 	FDILink* link = fPipe->FDI();
876 	if (link != NULL) {
877 		uint32 lanes = 0;
878 		uint32 linkBandwidth = 0;
879 		uint32 bitsPerPixel = 0;
880 		link->PreTrain(&target->timing, &linkBandwidth, &lanes, &bitsPerPixel);
881 		fPipe->SetFDILink(target->timing, linkBandwidth, lanes, bitsPerPixel);
882 		link->Train(&target->timing, lanes);
883 	}
884 
885 	pll_divisors divisors;
886 	compute_pll_divisors(&target->timing, &divisors, false);
887 
888 	uint32 extraPLLFlags = 0;
889 	if (gInfo->shared_info->device_type.Generation() >= 3)
890 		extraPLLFlags |= DISPLAY_PLL_MODE_NORMAL | DISPLAY_PLL_2X_CLOCK;
891 
892 	// Program general pipe config
893 	fPipe->Configure(target);
894 
895 	// Program pipe PLL's
896 	fPipe->ConfigureClocks(divisors, target->timing.pixel_clock, extraPLLFlags);
897 
898 	// Program target display mode
899 	fPipe->ConfigureTimings(target);
900 
901 	// Set fCurrentMode to our set display mode
902 	memcpy(&fCurrentMode, target, sizeof(display_mode));
903 
904 	return B_OK;
905 }
906 
907 
908 // #pragma mark - LVDS Panel
909 // #pragma mark - HDMI
910 
911 
912 HDMIPort::HDMIPort(port_index index)
913 	:
914 	DigitalPort(index, "HDMI")
915 {
916 }
917 
918 
919 bool
920 HDMIPort::IsConnected()
921 {
922 	if (!gInfo->shared_info->device_type.SupportsHDMI())
923 		return false;
924 
925 	addr_t portRegister = _PortRegister();
926 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
927 		portRegister);
928 
929 	if (portRegister == 0)
930 		return false;
931 
932 	//Notes:
933 	//- DISPLAY_MONITOR_PORT_DETECTED does only tell you *some* sort of digital display is
934 	//  connected to the port *if* you have the AUX channel stuff under power. It does not
935 	//  tell you which -type- of digital display is connected.
936 	//- Since we rely on the BIOS anyway, let's just use the conclusions it made for us :)
937 	//  Beware though: set_display_power_mode() uses this DISPLAY_MONITOR_PORT_ENABLED bit
938 	//  for DPMS as well. So we should better buffer our findings here for i.e. possible
939 	//  accelerant clones starting up. For DPMS there's currently no problem as this bit
940 	//  is only programmed for LVDS, DVI and VGA while we detect presence only for DP and HDMI.
941 	//
942 	//if ((read32(portRegister) & DISPLAY_MONITOR_PORT_DETECTED) == 0)
943 	if ((read32(portRegister) & DISPLAY_MONITOR_PORT_ENABLED) == 0)
944 		return false;
945 
946 	return HasEDID();
947 }
948 
949 
950 addr_t
951 HDMIPort::_PortRegister()
952 {
953 	// on PCH there's an additional port sandwiched in
954 	bool hasPCH = (gInfo->shared_info->pch_info != INTEL_PCH_NONE);
955 	bool fourthGen = gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV);
956 
957 	switch (PortIndex()) {
958 		case INTEL_PORT_B:
959 			if (fourthGen)
960 				return GEN4_HDMI_PORT_B;
961 			return hasPCH ? PCH_HDMI_PORT_B : INTEL_HDMI_PORT_B;
962 		case INTEL_PORT_C:
963 			if (fourthGen)
964 				return GEN4_HDMI_PORT_C;
965 			return hasPCH ? PCH_HDMI_PORT_C : INTEL_HDMI_PORT_C;
966 		case INTEL_PORT_D:
967 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV))
968 				return CHV_HDMI_PORT_D;
969 			return hasPCH ? PCH_HDMI_PORT_D : 0;
970 		default:
971 			return 0;
972 		}
973 
974 	return 0;
975 }
976 
977 
978 // #pragma mark - DisplayPort
979 
980 
981 DisplayPort::DisplayPort(port_index index, const char* baseName)
982 	:
983 	Port(index, baseName)
984 {
985 }
986 
987 
988 pipe_index
989 DisplayPort::PipePreference()
990 {
991 	CALLED();
992 	if (gInfo->shared_info->device_type.Generation() <= 4)
993 		return INTEL_PIPE_ANY;
994 
995 	// Notes:
996 	// - The BIOSes seen sofar do not use PIPE C by default.
997 	// - Looks like BIOS selected Transcoder (A,B,C) is not always same as selected Pipe (A,B,C)
998 	//   so these should probably be handled seperately. For now this is OK as we don't touch
999 	//   the pipe for DisplayPort, only the transcoder..
1000 	uint32 TranscoderPort = INTEL_TRANS_DP_PORT_NONE;
1001 	switch (PortIndex()) {
1002 		case INTEL_PORT_A:
1003 			if (gInfo->shared_info->device_type.Generation() == 6) {
1004 				if (((read32(INTEL_DISPLAY_PORT_A) & INTEL_DISP_PORTA_SNB_PIPE_MASK)
1005 					>> INTEL_DISP_PORTA_SNB_PIPE_SHIFT) == INTEL_DISP_PORTA_SNB_PIPE_A) {
1006 					return INTEL_PIPE_A;
1007 				} else {
1008 					return INTEL_PIPE_B;
1009 				}
1010 			}
1011 			if (gInfo->shared_info->device_type.Generation() == 7) {
1012 				uint32 Pipe = (read32(INTEL_DISPLAY_PORT_A) & INTEL_DISP_PORTA_IVB_PIPE_MASK)
1013 					>> INTEL_DISP_PORTA_IVB_PIPE_SHIFT;
1014 				switch (Pipe) {
1015 					case INTEL_DISP_PORTA_IVB_PIPE_A:
1016 						return INTEL_PIPE_A;
1017 					case INTEL_DISP_PORTA_IVB_PIPE_B:
1018 						return INTEL_PIPE_B;
1019 					case INTEL_DISP_PORTA_IVB_PIPE_C:
1020 						return INTEL_PIPE_C;
1021 					default:
1022 						return INTEL_PIPE_ANY;
1023 				}
1024 			}
1025 			return INTEL_PIPE_ANY;
1026 		case INTEL_PORT_B:
1027 			TranscoderPort = INTEL_TRANS_DP_PORT_B;
1028 			break;
1029 		case INTEL_PORT_C:
1030 			TranscoderPort = INTEL_TRANS_DP_PORT_C;
1031 			break;
1032 		case INTEL_PORT_D:
1033 			TranscoderPort = INTEL_TRANS_DP_PORT_D;
1034 			break;
1035 		default:
1036 			return INTEL_PIPE_ANY;
1037 	}
1038 
1039 	for (uint32 Transcoder = 0; Transcoder < 3; Transcoder++) {
1040 		if ((read32(INTEL_TRANSCODER_A_DP_CTL + (Transcoder << 12)) & INTEL_TRANS_DP_PORT_MASK) ==
1041 			INTEL_TRANS_DP_PORT(TranscoderPort)) {
1042 			switch (Transcoder) {
1043 				case 0:
1044 					return INTEL_PIPE_A;
1045 				case 1:
1046 					return INTEL_PIPE_B;
1047 				case 2:
1048 					return INTEL_PIPE_C;
1049 			}
1050 		}
1051 	}
1052 
1053 	return INTEL_PIPE_ANY;
1054 }
1055 
1056 
1057 status_t
1058 DisplayPort::SetPipe(Pipe* pipe)
1059 {
1060 	CALLED();
1061 
1062 	if (pipe == NULL) {
1063 		ERROR("%s: Invalid pipe provided!\n", __func__);
1064 		return B_ERROR;
1065 	}
1066 
1067 	// TODO: UnAssignPipe?  This likely needs reworked a little
1068 	if (fPipe != NULL) {
1069 		ERROR("%s: Can't reassign display pipe (yet)\n", __func__);
1070 		return B_ERROR;
1071 	}
1072 
1073 	// generation 3/4/5 gfx have no eDP ports.
1074 	// generation 6 gfx SandyBridge/SNB uses one bit (b30) on the eDP port.
1075 	// generation 7 gfx IvyBridge/IVB uses 2 bits (b29-30) on the eDP port.
1076 	// on all other DP ports pipe selections works differently (indirect).
1077 	// fixme: implement..
1078 	TRACE("%s: Assuming pipe is assigned by BIOS (fixme)\n", __func__);
1079 
1080 	fPipe = pipe;
1081 
1082 	if (fPipe == NULL)
1083 		return B_NO_MEMORY;
1084 
1085 	// Disable display pipe until modesetting enables it
1086 	if (fPipe->IsEnabled())
1087 		fPipe->Enable(false);
1088 
1089 	return B_OK;
1090 }
1091 
1092 
1093 bool
1094 DisplayPort::IsConnected()
1095 {
1096 	addr_t portRegister = _PortRegister();
1097 
1098 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
1099 		portRegister);
1100 
1101 	if (portRegister == 0)
1102 		return false;
1103 
1104 	//Notes:
1105 	//- DISPLAY_MONITOR_PORT_DETECTED does only tell you *some* sort of digital display is
1106 	//  connected to the port *if* you have the AUX channel stuff under power. It does not
1107 	//  tell you which -type- of digital display is connected.
1108 	//- Since we rely on the BIOS anyway, let's just use the conclusions it made for us :)
1109 	//  Beware though: set_display_power_mode() uses this DISPLAY_MONITOR_PORT_ENABLED bit
1110 	//  for DPMS as well. So we should better buffer our findings here for i.e. possible
1111 	//  accelerant clones starting up. For DPMS there's currently no problem as this bit
1112 	//  is only programmed for LVDS, DVI and VGA while we detect presence only for DP and HDMI.
1113 	//
1114 	//if ((read32(portRegister) & DISPLAY_MONITOR_PORT_DETECTED) == 0) {
1115 	if ((read32(portRegister) & DISPLAY_MONITOR_PORT_ENABLED) == 0) {
1116 		TRACE("%s: %s link not detected\n", __func__, PortName());
1117 		return false;
1118 	}
1119 
1120 	TRACE("%s: %s link detected\n", __func__, PortName());
1121 
1122 	// On laptops we always have an internal panel.. (this is on the eDP port)
1123 	if (gInfo->shared_info->device_type.IsMobile() && (PortIndex() == INTEL_PORT_A)) {
1124 		if (gInfo->shared_info->has_vesa_edid_info) {
1125 			TRACE("%s: Laptop. Using VESA edid info\n", __func__);
1126 			memcpy(&fEDIDInfo, &gInfo->shared_info->vesa_edid_info,
1127 				sizeof(edid1_info));
1128 			if (fEDIDState != B_OK) {
1129 				fEDIDState = B_OK;
1130 				// HasEDID now true
1131 				edid_dump(&fEDIDInfo);
1132 			}
1133 			return true;
1134 		} else if (gInfo->shared_info->got_vbt) {
1135 			TRACE("%s: Laptop. No EDID, but force enabled as we have a VBT\n", __func__);
1136 			return true;
1137 		}
1138 	}
1139 
1140 	//since EDID is not correctly implemented yet for this connection type we'll do without it for now
1141 	//return HasEDID();
1142 	return true;
1143 }
1144 
1145 
1146 addr_t
1147 DisplayPort::_DDCRegister()
1148 {
1149 	// TODO: Do VLV + CHV use the VLV_DP_AUX_CTL_B + VLV_DP_AUX_CTL_C?
1150 	switch (PortIndex()) {
1151 		case INTEL_PORT_A:
1152 			return INTEL_DP_AUX_CTL_A;
1153 		case INTEL_PORT_B:
1154 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1155 				return VLV_DP_AUX_CTL_B;
1156 			return INTEL_DP_AUX_CTL_B;
1157 		case INTEL_PORT_C:
1158 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1159 				return VLV_DP_AUX_CTL_C;
1160 			return INTEL_DP_AUX_CTL_C;
1161 		case INTEL_PORT_D:
1162 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV))
1163 				return CHV_DP_AUX_CTL_D;
1164 			else if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1165 				return 0;
1166 			return INTEL_DP_AUX_CTL_D;
1167 		default:
1168 			return 0;
1169 	}
1170 
1171 	return 0;
1172 }
1173 
1174 
1175 addr_t
1176 DisplayPort::_PortRegister()
1177 {
1178 	// There are 6000 lines of intel linux code probing DP registers
1179 	// to properly detect DP vs eDP to then in-turn properly figure out
1180 	// what is DP and what is HDMI. It only takes 3 lines to
1181 	// ignore DisplayPort on ValleyView / CherryView
1182 
1183 	if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV)
1184 		|| gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV)) {
1185 		ERROR("TODO: DisplayPort on ValleyView / CherryView");
1186 		return 0;
1187 	}
1188 
1189 	// Intel, are humans even involved anymore?
1190 	// This is a lot more complex than this code makes it look. (see defines)
1191 	// INTEL_DISPLAY_PORT_X moves around a lot based on PCH
1192 	// except on ValleyView and CherryView.
1193 	switch (PortIndex()) {
1194 		case INTEL_PORT_A:
1195 			return INTEL_DISPLAY_PORT_A;
1196 		case INTEL_PORT_B:
1197 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1198 				return VLV_DISPLAY_PORT_B;
1199 			return INTEL_DISPLAY_PORT_B;
1200 		case INTEL_PORT_C:
1201 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1202 				return VLV_DISPLAY_PORT_C;
1203 			return INTEL_DISPLAY_PORT_C;
1204 		case INTEL_PORT_D:
1205 			if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_CHV))
1206 				return CHV_DISPLAY_PORT_D;
1207 			else if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_VLV))
1208 				return 0;
1209 			return INTEL_DISPLAY_PORT_D;
1210 		default:
1211 			return 0;
1212 	}
1213 
1214 	return 0;
1215 }
1216 
1217 
1218 status_t
1219 DisplayPort::_SetPortLinkGen4(const display_timing& timing)
1220 {
1221 	// Khz / 10. ( each output octet encoded as 10 bits.
1222 	//fixme: always so?
1223 	uint32 linkBandwidth = 270000; //khz
1224 	uint32 fPipeOffset = 0;
1225 	if (fPipe->Index() == INTEL_PIPE_B)
1226 		fPipeOffset = 0x1000;
1227 
1228 	TRACE("%s: DP M1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_M + fPipeOffset));
1229 	TRACE("%s: DP N1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_N + fPipeOffset));
1230 	TRACE("%s: DP M1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_M + fPipeOffset));
1231 	TRACE("%s: DP N1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_N + fPipeOffset));
1232 
1233 	uint32 bitsPerPixel = 24;	//fixme: always so?
1234 	uint32 lanes = 4;			//fixme: always so?
1235 
1236 	//Setup Data M/N
1237 	uint64 linkspeed = lanes * linkBandwidth * 8;
1238 	uint64 ret_n = 1;
1239 	while(ret_n < linkspeed) {
1240 		ret_n *= 2;
1241 	}
1242 	if (ret_n > 0x800000) {
1243 		ret_n = 0x800000;
1244 	}
1245 	uint64 ret_m = timing.pixel_clock * ret_n * bitsPerPixel / linkspeed;
1246 	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1247 		ret_m >>= 1;
1248 		ret_n >>= 1;
1249 	}
1250 	//Set TU size bits (to default, max) before link training so that error detection works
1251 	write32(INTEL_PIPE_A_DATA_M + fPipeOffset, ret_m | FDI_PIPE_MN_TU_SIZE_MASK);
1252 	write32(INTEL_PIPE_A_DATA_N + fPipeOffset, ret_n);
1253 
1254 	//Setup Link M/N
1255 	linkspeed = linkBandwidth;
1256 	ret_n = 1;
1257 	while(ret_n < linkspeed) {
1258 		ret_n *= 2;
1259 	}
1260 	if (ret_n > 0x800000) {
1261 		ret_n = 0x800000;
1262 	}
1263 	ret_m = timing.pixel_clock * ret_n / linkspeed;
1264 	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1265 		ret_m >>= 1;
1266 		ret_n >>= 1;
1267 	}
1268 	write32(INTEL_PIPE_A_LINK_M + fPipeOffset, ret_m);
1269 	//Writing Link N triggers all four registers to be activated also (on next VBlank)
1270 	write32(INTEL_PIPE_A_LINK_N + fPipeOffset, ret_n);
1271 
1272 	TRACE("%s: DP M1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_M + fPipeOffset));
1273 	TRACE("%s: DP N1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_DATA_N + fPipeOffset));
1274 	TRACE("%s: DP M1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_M + fPipeOffset));
1275 	TRACE("%s: DP N1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_PIPE_A_LINK_N + fPipeOffset));
1276 
1277 	return B_OK;
1278 }
1279 
1280 
1281 status_t
1282 DisplayPort::_SetPortLinkGen6(const display_timing& timing)
1283 {
1284 	// Khz / 10. ( each output octet encoded as 10 bits.
1285 	//note: (fixme) eDP is fixed option 162 or 270Mc, other DPs go via DPLL programming to one of the same vals.
1286 	uint32 linkBandwidth = 270000; //khz
1287 	TRACE("%s: DP link reference clock is %gMhz\n", __func__, linkBandwidth / 1000.0f);
1288 
1289 	uint32 fPipeOffset = 0;
1290 	switch (fPipe->Index()) {
1291 		case INTEL_PIPE_B:
1292 			fPipeOffset = 0x1000;
1293 			break;
1294 		case INTEL_PIPE_C:
1295 			fPipeOffset = 0x2000;
1296 			break;
1297 		default:
1298 			break;
1299 	}
1300 
1301 	TRACE("%s: DP M1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_DATA_M1 + fPipeOffset));
1302 	TRACE("%s: DP N1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_DATA_N1 + fPipeOffset));
1303 	TRACE("%s: DP M1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_LINK_M1 + fPipeOffset));
1304 	TRACE("%s: DP N1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_LINK_N1 + fPipeOffset));
1305 
1306 	uint32 bitsPerPixel =
1307 		(read32(INTEL_TRANSCODER_A_DP_CTL + fPipeOffset) & INTEL_TRANS_DP_BPC_MASK) >> INTEL_TRANS_DP_COLOR_SHIFT;
1308 	switch (bitsPerPixel) {
1309 		case PIPE_DDI_8BPC:
1310 			bitsPerPixel = 24;
1311 			break;
1312 		case PIPE_DDI_10BPC:
1313 			bitsPerPixel = 30;
1314 			break;
1315 		case PIPE_DDI_6BPC:
1316 			bitsPerPixel = 18;
1317 			break;
1318 		case PIPE_DDI_12BPC:
1319 			bitsPerPixel = 36;
1320 			break;
1321 		default:
1322 			ERROR("%s: DP illegal link colordepth set.\n", __func__);
1323 			return B_ERROR;
1324 	}
1325 	TRACE("%s: DP link colordepth: %" B_PRIu32 "\n", __func__, bitsPerPixel);
1326 
1327 	uint32 lanes = ((read32(_PortRegister()) & INTEL_DISP_PORT_WIDTH_MASK) >> INTEL_DISP_PORT_WIDTH_SHIFT) + 1;
1328 	if (lanes > 4) {
1329 		ERROR("%s: DP illegal number of lanes set.\n", __func__);
1330 		return B_ERROR;
1331 	}
1332 	TRACE("%s: DP mode with %" B_PRIx32 " lane(s) in use\n", __func__, lanes);
1333 
1334 	//Reserving 5% bandwidth for possible spread spectrum clock use
1335 	uint32 bps = timing.pixel_clock * bitsPerPixel * 21 / 20;
1336 	//use DIV_ROUND_UP:
1337 	uint32 required_lanes = (bps + (linkBandwidth * 8) - 1) / (linkBandwidth * 8);
1338 	TRACE("%s: DP mode needs %" B_PRIx32 " lane(s) in use\n", __func__, required_lanes);
1339 	if (required_lanes > lanes) {
1340 		//Note that we *must* abort as otherwise the PIPE/DP-link hangs forever (without retraining!).
1341 		ERROR("%s: DP not enough lanes active for requested mode.\n", __func__);
1342 		return B_ERROR;
1343 	}
1344 
1345 	//Setup Data M/N
1346 	uint64 linkspeed = lanes * linkBandwidth * 8;
1347 	uint64 ret_n = 1;
1348 	while(ret_n < linkspeed) {
1349 		ret_n *= 2;
1350 	}
1351 	if (ret_n > 0x800000) {
1352 		ret_n = 0x800000;
1353 	}
1354 	uint64 ret_m = timing.pixel_clock * ret_n * bitsPerPixel / linkspeed;
1355 	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1356 		ret_m >>= 1;
1357 		ret_n >>= 1;
1358 	}
1359 	//Set TU size bits (to default, max) before link training so that error detection works
1360 	write32(INTEL_TRANSCODER_A_DATA_M1 + fPipeOffset, ret_m | INTEL_TRANSCODER_MN_TU_SIZE_MASK);
1361 	write32(INTEL_TRANSCODER_A_DATA_N1 + fPipeOffset, ret_n);
1362 
1363 	//Setup Link M/N
1364 	linkspeed = linkBandwidth;
1365 	ret_n = 1;
1366 	while(ret_n < linkspeed) {
1367 		ret_n *= 2;
1368 	}
1369 	if (ret_n > 0x800000) {
1370 		ret_n = 0x800000;
1371 	}
1372 	ret_m = timing.pixel_clock * ret_n / linkspeed;
1373 	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1374 		ret_m >>= 1;
1375 		ret_n >>= 1;
1376 	}
1377 	write32(INTEL_TRANSCODER_A_LINK_M1 + fPipeOffset, ret_m);
1378 	//Writing Link N triggers all four registers to be activated also (on next VBlank)
1379 	write32(INTEL_TRANSCODER_A_LINK_N1 + fPipeOffset, ret_n);
1380 
1381 	TRACE("%s: DP M1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_DATA_M1 + fPipeOffset));
1382 	TRACE("%s: DP N1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_DATA_N1 + fPipeOffset));
1383 	TRACE("%s: DP M1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_LINK_M1 + fPipeOffset));
1384 	TRACE("%s: DP N1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_TRANSCODER_A_LINK_N1 + fPipeOffset));
1385 
1386 	return B_OK;
1387 }
1388 
1389 
1390 status_t
1391 DisplayPort::SetDisplayMode(display_mode* target, uint32 colorMode)
1392 {
1393 	CALLED();
1394 	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
1395 		target->timing.v_display);
1396 
1397 	if (fPipe == NULL) {
1398 		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
1399 		return B_ERROR;
1400 	}
1401 
1402 	status_t result = B_OK;
1403 	if (gInfo->shared_info->device_type.Generation() <= 4) {
1404 		fPipe->ConfigureTimings(target);
1405 		result = _SetPortLinkGen4(target->timing);
1406 	} else {
1407 		display_timing hardwareTarget = target->timing;
1408 		bool needsScaling = false;
1409 		if ((PortIndex() == INTEL_PORT_A) && gInfo->shared_info->device_type.IsMobile()) {
1410 			// For internal panels, we may need to set the timings according to the panel
1411 			// native video mode, and let the panel fitter do the scaling.
1412 			// note: upto/including generation 5 laptop panels are still LVDS types, handled elsewhere.
1413 
1414 			if (gInfo->shared_info->got_vbt) {
1415 				// Set vbios hardware panel mode as base
1416 				hardwareTarget = gInfo->shared_info->panel_timing;
1417 
1418 				if (hardwareTarget.h_display == target->timing.h_display
1419 						&& hardwareTarget.v_display == target->timing.v_display) {
1420 					// We are feeding the native video mode, nothing to do: disable scaling
1421 					TRACE("%s: Requested mode is panel's native resolution: disabling scaling\n", __func__);
1422 				} else {
1423 					// We need to enable the panel fitter
1424 					TRACE("%s: Requested mode is not panel's native resolution: enabling scaling\n", __func__);
1425 					needsScaling = true;
1426 				}
1427 			} else {
1428 				//fixme: We should now first try for EDID info detailed timing, highest res in list: that's the
1429 				//native mode as well. If we also have no EDID, then fallback to setting mode directly as below.
1430 
1431 				TRACE("%s: Setting internal panel mode without VBT info generation, scaling may not work\n",
1432 					__func__);
1433 				// We don't have VBT data, try to set the requested mode directly
1434 				// and hope for the best
1435 				hardwareTarget = target->timing;
1436 			}
1437 		}
1438 
1439 		result = B_OK;
1440 		if (PortIndex() != INTEL_PORT_A)
1441 			result = _SetPortLinkGen6(hardwareTarget);
1442 
1443 		if (result == B_OK) {
1444 			// Setup PanelFitter and Train FDI if it exists
1445 			PanelFitter* fitter = fPipe->PFT();
1446 			if (fitter != NULL)
1447 				fitter->Enable(hardwareTarget);
1448 
1449 			uint32 lanes = 0;
1450 			uint32 linkBandwidth = 0;
1451 			uint32 bitsPerPixel = 0;
1452 			if (PortIndex() != INTEL_PORT_A) {
1453 				FDILink* link = fPipe->FDI();
1454 				if (link != NULL) {
1455 					link->PreTrain(&hardwareTarget, &linkBandwidth, &lanes, &bitsPerPixel);
1456 					fPipe->SetFDILink(hardwareTarget, linkBandwidth, lanes, bitsPerPixel);
1457 					link->Train(&hardwareTarget, lanes);
1458 				}
1459 			} else {
1460 				// 'local' eDP port is in use
1461 				linkBandwidth =
1462 					(read32(INTEL_DISPLAY_PORT_A) & INTEL_DISP_EDP_PLL_FREQ_MASK) >> INTEL_DISP_EDP_PLL_FREQ_SHIFT;
1463 				switch (linkBandwidth) {
1464 					case INTEL_DISP_EDP_PLL_FREQ_270:
1465 						linkBandwidth = 270000; //khz
1466 						break;
1467 					case INTEL_DISP_EDP_PLL_FREQ_162:
1468 						linkBandwidth = 162000; //khz
1469 						break;
1470 					default:
1471 						TRACE("%s: eDP illegal reference clock ID set, assuming 270Mhz.\n", __func__);
1472 						linkBandwidth = 270000; //khz
1473 				}
1474 
1475 				bitsPerPixel =
1476 					(read32(INTEL_DISPLAY_A_PIPE_CONTROL) & INTEL_PIPE_BPC_MASK) >> INTEL_PIPE_COLOR_SHIFT;
1477 				switch (bitsPerPixel) {
1478 					case INTEL_PIPE_8BPC:
1479 						bitsPerPixel = 24;
1480 						break;
1481 					case INTEL_PIPE_10BPC:
1482 						bitsPerPixel = 30;
1483 						break;
1484 					case INTEL_PIPE_6BPC:
1485 						bitsPerPixel = 18;
1486 						break;
1487 					case INTEL_PIPE_12BPC:
1488 						bitsPerPixel = 36;
1489 						break;
1490 					default:
1491 						bitsPerPixel = 0;
1492 				}
1493 
1494 				lanes =
1495 					((read32(INTEL_DISPLAY_PORT_A) & INTEL_DISP_PORT_WIDTH_MASK) >> INTEL_DISP_PORT_WIDTH_SHIFT) + 1;
1496 
1497 				fPipe->SetFDILink(hardwareTarget, linkBandwidth, lanes, bitsPerPixel);
1498 			}
1499 
1500 			// Program general pipe config
1501 			fPipe->Configure(target);
1502 
1503 			// Pll programming is not needed for (e)DP..
1504 
1505 			// Program target display mode
1506 			fPipe->ConfigureTimings(target, !needsScaling, PortIndex());
1507 		} else {
1508 			TRACE("%s: Setting display mode via fallback: using scaling!\n", __func__);
1509 			// Keep monitor at native mode and scale image to that
1510 			fPipe->ConfigureScalePos(target);
1511 		}
1512 	}
1513 
1514 	// Set fCurrentMode to our set display mode
1515 	memcpy(&fCurrentMode, target, sizeof(display_mode));
1516 
1517 	return result;
1518 }
1519 
1520 
1521 // #pragma mark - Embedded DisplayPort
1522 
1523 
1524 EmbeddedDisplayPort::EmbeddedDisplayPort()
1525 	:
1526 	DisplayPort(INTEL_PORT_A, "Embedded DisplayPort")
1527 {
1528 }
1529 
1530 
1531 bool
1532 EmbeddedDisplayPort::IsConnected()
1533 {
1534 	addr_t portRegister = _PortRegister();
1535 
1536 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
1537 		portRegister);
1538 
1539 	if (!gInfo->shared_info->device_type.IsMobile()) {
1540 		TRACE("%s: skipping eDP on non-mobile GPU\n", __func__);
1541 		return false;
1542 	}
1543 
1544 	if ((read32(portRegister) & DISPLAY_MONITOR_PORT_DETECTED) == 0) {
1545 		TRACE("%s: %s link not detected\n", __func__, PortName());
1546 		return false;
1547 	}
1548 
1549 	HasEDID();
1550 
1551 	// If eDP has EDID, awesome. We use it.
1552 	// No EDID? The modesetting code falls back to VBIOS panel_mode
1553 	return true;
1554 }
1555 
1556 
1557 // #pragma mark - Digital Display Port
1558 
1559 
1560 DigitalDisplayInterface::DigitalDisplayInterface(port_index index,
1561 		const char* baseName)
1562 	:
1563 	Port(index, baseName)
1564 {
1565 	// As of Haswell, Intel decided to change eDP ports to a "DDI" bus...
1566 	// on a dare because the hardware engineers were drunk one night.
1567 }
1568 
1569 
1570 addr_t
1571 DigitalDisplayInterface::_PortRegister()
1572 {
1573 	// TODO: Linux does a DDI_BUF_CTL(INTEL_PORT_A) which is cleaner
1574 	// (but we have to ensure the offsets + region base is correct)
1575 	switch (PortIndex()) {
1576 		case INTEL_PORT_A:
1577 			return DDI_BUF_CTL_A;
1578 		case INTEL_PORT_B:
1579 			return DDI_BUF_CTL_B;
1580 		case INTEL_PORT_C:
1581 			return DDI_BUF_CTL_C;
1582 		case INTEL_PORT_D:
1583 			return DDI_BUF_CTL_D;
1584 		case INTEL_PORT_E:
1585 			return DDI_BUF_CTL_E;
1586 		case INTEL_PORT_F:
1587 			if ((gInfo->shared_info->device_type.Generation() > 8) &&
1588 				!gInfo->shared_info->device_type.InGroup(INTEL_GROUP_SKY))
1589 				return DDI_BUF_CTL_F;
1590 			return 0;
1591 		default:
1592 			return 0;
1593 	}
1594 	return 0;
1595 }
1596 
1597 
1598 addr_t
1599 DigitalDisplayInterface::_DDCRegister()
1600 {
1601 	switch (PortIndex()) {
1602 		case INTEL_PORT_B:
1603 			return INTEL_I2C_IO_E;
1604 		case INTEL_PORT_C:
1605 			return INTEL_I2C_IO_D;
1606 		case INTEL_PORT_D:
1607 			return INTEL_I2C_IO_F;
1608 		default:
1609 			return 0;
1610 	}
1611 
1612 	return 0;
1613 }
1614 
1615 
1616 status_t
1617 DigitalDisplayInterface::Power(bool enabled)
1618 {
1619 	if (fPipe == NULL) {
1620 		ERROR("%s: Setting power without assigned pipe!\n", __func__);
1621 		return B_ERROR;
1622 	}
1623 	TRACE("%s: %s DDI enabled: %s\n", __func__, PortName(),
1624 		enabled ? "true" : "false");
1625 
1626 	fPipe->Enable(enabled);
1627 
1628 	//nogo currently.. (kills output forever)
1629 #if 0
1630 	addr_t portRegister = _PortRegister();
1631 	uint32 state = read32(portRegister);
1632 	write32(portRegister,
1633 		enabled ? (state | DDI_BUF_CTL_ENABLE) : (state & ~DDI_BUF_CTL_ENABLE));
1634 	read32(portRegister);
1635 #endif
1636 
1637 	return B_OK;
1638 }
1639 
1640 
1641 status_t
1642 DigitalDisplayInterface::SetPipe(Pipe* pipe)
1643 {
1644 	CALLED();
1645 
1646 	if (pipe == NULL) {
1647 		ERROR("%s: Invalid pipe provided!\n", __func__);
1648 		return B_ERROR;
1649 	}
1650 
1651 	// TODO: UnAssignPipe?  This likely needs reworked a little
1652 	if (fPipe != NULL) {
1653 		ERROR("%s: Can't reassign display pipe (yet)\n", __func__);
1654 		return B_ERROR;
1655 	}
1656 
1657 	// all DDI ports pipe selections works differently than on the old port types (indirect).
1658 	// fixme: implement..
1659 	TRACE("%s: Assuming pipe is assigned by BIOS (fixme)\n", __func__);
1660 
1661 	fPipe = pipe;
1662 
1663 	if (fPipe == NULL)
1664 		return B_NO_MEMORY;
1665 
1666 	// Disable display pipe until modesetting enables it
1667 	if (fPipe->IsEnabled())
1668 		fPipe->Enable(false);
1669 
1670 	return B_OK;
1671 }
1672 
1673 
1674 bool
1675 DigitalDisplayInterface::IsConnected()
1676 {
1677 	addr_t portRegister = _PortRegister();
1678 
1679 	TRACE("%s: %s PortRegister: 0x%" B_PRIxADDR "\n", __func__, PortName(),
1680 		portRegister);
1681 
1682 	// Please note: Skylake and up (Desktop) might use eDP for a seperate active VGA converter chip.
1683 	if (portRegister == 0) {
1684 		TRACE("%s: Port not implemented\n", __func__);
1685 		return false;
1686 	}
1687 
1688 	// newer chipsets support 4 lanes on all ports
1689 	fMaxLanes = 4;
1690 	if ((gInfo->shared_info->device_type.Generation() < 9) ||
1691 		gInfo->shared_info->device_type.InGroup(INTEL_GROUP_SKY)) {
1692 		// Probe a little port info.
1693 		if ((read32(DDI_BUF_CTL_A) & DDI_A_4_LANES) != 0) {
1694 			switch (PortIndex()) {
1695 				case INTEL_PORT_A:
1696 					fMaxLanes = 4;
1697 					break;
1698 				case INTEL_PORT_E:
1699 					fMaxLanes = 0;
1700 					break;
1701 				default:
1702 					fMaxLanes = 4;
1703 					break;
1704 			}
1705 		} else {
1706 			switch (PortIndex()) {
1707 				case INTEL_PORT_A:
1708 					fMaxLanes = 2;
1709 					break;
1710 				case INTEL_PORT_E:
1711 					fMaxLanes = 2;
1712 					break;
1713 				default:
1714 					fMaxLanes = 4;
1715 					break;
1716 			}
1717 		}
1718 	}
1719 
1720 	TRACE("%s: %s Maximum Lanes: %" B_PRId8 "\n", __func__,
1721 		PortName(), fMaxLanes);
1722 
1723 	// fetch EDID but determine 'in use' later (below) so we also catch screens that fail EDID
1724 	HasEDID();
1725 
1726 	// scan all our pipes to find the one connected to the current port and check it's enabled
1727 	uint32 pipeState = 0;
1728 	for (uint32 pipeCnt = 0; pipeCnt < 4; pipeCnt++) {
1729 		switch (pipeCnt) {
1730 			case 0:
1731 				pipeState = read32(PIPE_DDI_FUNC_CTL_A);
1732 				break;
1733 			case 1:
1734 				pipeState = read32(PIPE_DDI_FUNC_CTL_B);
1735 				break;
1736 			case 2:
1737 				pipeState = read32(PIPE_DDI_FUNC_CTL_C);
1738 				break;
1739 			default:
1740 				pipeState = read32(PIPE_DDI_FUNC_CTL_EDP);
1741 				break;
1742 		}
1743 
1744 		if ((((pipeState & PIPE_DDI_SELECT_MASK) >> PIPE_DDI_SELECT_SHIFT) + 1) == (uint32)PortIndex()) {
1745 			// See if the BIOS enabled our output as it indicates it's in use
1746 			if (pipeState & PIPE_DDI_FUNC_CTL_ENABLE) {
1747 				TRACE("%s: Connected\n", __func__);
1748 				return true;
1749 			}
1750 		}
1751 	}
1752 
1753 	// On laptops we always have an internal panel.. (this is on the eDP port)
1754 	if (gInfo->shared_info->device_type.IsMobile() && (PortIndex() == INTEL_PORT_E)) {
1755 		if (gInfo->shared_info->has_vesa_edid_info) {
1756 			TRACE("%s: Laptop. Using VESA edid info\n", __func__);
1757 			memcpy(&fEDIDInfo, &gInfo->shared_info->vesa_edid_info,
1758 				sizeof(edid1_info));
1759 			if (fEDIDState != B_OK) {
1760 				fEDIDState = B_OK;
1761 				// HasEDID now true
1762 				edid_dump(&fEDIDInfo);
1763 			}
1764 			return true;
1765 		} else if (gInfo->shared_info->got_vbt) {
1766 			TRACE("%s: Laptop. No EDID, but force enabled as we have a VBT\n", __func__);
1767 			return true;
1768 		}
1769 	}
1770 
1771 	TRACE("%s: Not connected\n", __func__);
1772 	return false;
1773 }
1774 
1775 status_t
1776 DigitalDisplayInterface::_SetPortLinkGen8(const display_timing& timing, uint32 pllSel)
1777 {
1778 	//fixme: always so on pre gen 9?
1779 	uint32 linkBandwidth = 270000; //khz
1780 
1781 	if (gInfo->shared_info->device_type.Generation() >= 9) {
1782 		if (pllSel != 0xff) {
1783 			linkBandwidth = (read32(SKL_DPLL_CTRL1) >> (1 + 6 * pllSel)) & SKL_DPLL_DP_LINKRATE_MASK;
1784 			switch (linkBandwidth) {
1785 				case SKL_DPLL_CTRL1_2700:
1786 					linkBandwidth = 2700000 / 5;
1787 					break;
1788 				case SKL_DPLL_CTRL1_1350:
1789 					linkBandwidth = 1350000 / 5;
1790 					break;
1791 				case SKL_DPLL_CTRL1_810:
1792 					linkBandwidth =  810000 / 5;
1793 					break;
1794 				case SKL_DPLL_CTRL1_1620:
1795 					linkBandwidth = 1620000 / 5;
1796 					break;
1797 				case SKL_DPLL_CTRL1_1080:
1798 					linkBandwidth = 1080000 / 5;
1799 					break;
1800 				case SKL_DPLL_CTRL1_2160:
1801 					linkBandwidth = 2160000 / 5;
1802 					break;
1803 				default:
1804 					linkBandwidth = 270000;
1805 					ERROR("%s: DDI No known DP-link reference clock selected, assuming default\n", __func__);
1806 					break;
1807 			}
1808 		} else {
1809 			ERROR("%s: DDI No known PLL selected, assuming default DP-link reference\n", __func__);
1810 		}
1811 	}
1812 	TRACE("%s: DDI DP-link reference clock is %gMhz\n", __func__, linkBandwidth / 1000.0f);
1813 
1814 	uint32 fPipeOffset = 0;
1815 	switch (fPipe->Index()) {
1816 		case INTEL_PIPE_B:
1817 			fPipeOffset = 0x1000;
1818 			break;
1819 		case INTEL_PIPE_C:
1820 			fPipeOffset = 0x2000;
1821 			break;
1822 		case INTEL_PIPE_D:
1823 			fPipeOffset = 0xf000;
1824 			break;
1825 		default:
1826 			break;
1827 	}
1828 
1829 	TRACE("%s: DDI M1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_M + fPipeOffset));
1830 	TRACE("%s: DDI N1 data before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_N + fPipeOffset));
1831 	TRACE("%s: DDI M1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_M + fPipeOffset));
1832 	TRACE("%s: DDI N1 link before: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_N + fPipeOffset));
1833 
1834 	uint32 pipeFunc = read32(PIPE_DDI_FUNC_CTL_A + fPipeOffset);
1835 	uint32 bitsPerPixel = (pipeFunc & PIPE_DDI_BPC_MASK) >> PIPE_DDI_COLOR_SHIFT;
1836 	switch (bitsPerPixel) {
1837 		case PIPE_DDI_8BPC:
1838 			bitsPerPixel = 24;
1839 			break;
1840 		case PIPE_DDI_10BPC:
1841 			bitsPerPixel = 30;
1842 			break;
1843 		case PIPE_DDI_6BPC:
1844 			bitsPerPixel = 18;
1845 			break;
1846 		case PIPE_DDI_12BPC:
1847 			bitsPerPixel = 36;
1848 			break;
1849 		default:
1850 			ERROR("%s: DDI illegal link colordepth set.\n", __func__);
1851 			return B_ERROR;
1852 	}
1853 	TRACE("%s: DDI Link Colordepth: %" B_PRIu32 "\n", __func__, bitsPerPixel);
1854 
1855 	uint32 lanes = 4;
1856 	// Only DP modes supports less than 4 lanes: read current config
1857 	if (((pipeFunc & PIPE_DDI_MODESEL_MASK) >> PIPE_DDI_MODESEL_SHIFT) >= PIPE_DDI_MODE_DP_SST) {
1858 		// On gen 9.5 IceLake 3x mode exists (DSI only), earlier models: reserved value.
1859 		lanes = ((pipeFunc & PIPE_DDI_DP_WIDTH_MASK) >> PIPE_DDI_DP_WIDTH_SHIFT) + 1;
1860 		TRACE("%s: DDI in DP mode with %" B_PRIx32 " lane(s) in use\n", __func__, lanes);
1861 	} else {
1862 		TRACE("%s: DDI in non-DP mode with %" B_PRIx32 " lane(s) in use\n", __func__, lanes);
1863 	}
1864 
1865 	//Setup Data M/N
1866 	uint64 linkspeed = lanes * linkBandwidth * 8;
1867 	uint64 ret_n = 1;
1868 	while(ret_n < linkspeed) {
1869 		ret_n *= 2;
1870 	}
1871 	if (ret_n > 0x800000) {
1872 		ret_n = 0x800000;
1873 	}
1874 	uint64 ret_m = timing.pixel_clock * ret_n * bitsPerPixel / linkspeed;
1875 	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1876 		ret_m >>= 1;
1877 		ret_n >>= 1;
1878 	}
1879 	//Set TU size bits (to default, max) before link training so that error detection works
1880 	write32(INTEL_DDI_PIPE_A_DATA_M + fPipeOffset, ret_m | FDI_PIPE_MN_TU_SIZE_MASK);
1881 	write32(INTEL_DDI_PIPE_A_DATA_N + fPipeOffset, ret_n);
1882 
1883 	//Setup Link M/N
1884 	linkspeed = linkBandwidth;
1885 	ret_n = 1;
1886 	while(ret_n < linkspeed) {
1887 		ret_n *= 2;
1888 	}
1889 	if (ret_n > 0x800000) {
1890 		ret_n = 0x800000;
1891 	}
1892 	ret_m = timing.pixel_clock * ret_n / linkspeed;
1893 	while ((ret_n > 0xffffff) || (ret_m > 0xffffff)) {
1894 		ret_m >>= 1;
1895 		ret_n >>= 1;
1896 	}
1897 	write32(INTEL_DDI_PIPE_A_LINK_M + fPipeOffset, ret_m);
1898 	//Writing Link N triggers all four registers to be activated also (on next VBlank)
1899 	write32(INTEL_DDI_PIPE_A_LINK_N + fPipeOffset, ret_n);
1900 
1901 	TRACE("%s: DDI M1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_M + fPipeOffset));
1902 	TRACE("%s: DDI N1 data after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_DATA_N + fPipeOffset));
1903 	TRACE("%s: DDI M1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_M + fPipeOffset));
1904 	TRACE("%s: DDI N1 link after: 0x%" B_PRIx32 "\n", __func__, read32(INTEL_DDI_PIPE_A_LINK_N + fPipeOffset));
1905 
1906 	return B_OK;
1907 }
1908 
1909 status_t
1910 DigitalDisplayInterface::SetDisplayMode(display_mode* target, uint32 colorMode)
1911 {
1912 	CALLED();
1913 	TRACE("%s: %s %dx%d\n", __func__, PortName(), target->timing.h_display,
1914 		target->timing.v_display);
1915 
1916 	if (fPipe == NULL) {
1917 		ERROR("%s: Setting display mode without assigned pipe!\n", __func__);
1918 		return B_ERROR;
1919 	}
1920 
1921 	display_timing hardwareTarget = target->timing;
1922 	bool needsScaling = false;
1923 	if ((PortIndex() == INTEL_PORT_E) && gInfo->shared_info->device_type.IsMobile()) {
1924 		// For internal panels, we may need to set the timings according to the panel
1925 		// native video mode, and let the panel fitter do the scaling.
1926 
1927 		if (gInfo->shared_info->got_vbt) {
1928 			// Set vbios hardware panel mode as base
1929 			hardwareTarget = gInfo->shared_info->panel_timing;
1930 
1931 			if (hardwareTarget.h_display == target->timing.h_display
1932 					&& hardwareTarget.v_display == target->timing.v_display) {
1933 				// We are setting the native video mode, nothing special to do
1934 				// Note: this means refresh and timing might vary according to requested mode.
1935 				hardwareTarget = target->timing;
1936 				TRACE("%s: Setting internal panel to native resolution at %" B_PRIu32 "Hz\n", __func__,
1937 					hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
1938 			} else {
1939 				// We need to enable the panel fitter
1940 				TRACE("%s: Hardware mode will actually be %dx%d at %" B_PRIu32 "Hz\n", __func__,
1941 					hardwareTarget.h_display, hardwareTarget.v_display,
1942 					hardwareTarget.pixel_clock * 1000 / (hardwareTarget.h_total * hardwareTarget.v_total));
1943 
1944 				// FIXME we should also get the refresh frequency from the target
1945 				// mode, and then "sanitize" the resulting mode we made up.
1946 				needsScaling = true;
1947 			}
1948 		} else {
1949 			TRACE("%s: Setting internal panel mode without VBT info generation, scaling may not work\n",
1950 				__func__);
1951 			// We don't have VBT data, try to set the requested mode directly
1952 			// and hope for the best
1953 			hardwareTarget = target->timing;
1954 		}
1955 	}
1956 
1957 	// Setup PanelFitter
1958 	PanelFitter* fitter = fPipe->PFT();
1959 	if (fitter != NULL)
1960 		fitter->Enable(hardwareTarget);
1961 
1962 	// skip FDI as it never applies to DDI (on gen7 and 8 only for the real analog VGA port)
1963 
1964 	// Program general pipe config
1965 	fPipe->Configure(target);
1966 
1967 	uint32 pllSel = 0xff; // no PLL selected
1968 	if (gInfo->shared_info->device_type.Generation() <= 8) {
1969 		unsigned int r2_out, n2_out, p_out;
1970 		hsw_ddi_calculate_wrpll(
1971 			hardwareTarget.pixel_clock * 1000 /* in Hz */,
1972 			&r2_out, &n2_out, &p_out);
1973 	} else {
1974 		skl_wrpll_params wrpll_params;
1975 		skl_ddi_calculate_wrpll(
1976 			hardwareTarget.pixel_clock * 1000 /* in Hz */,
1977 			gInfo->shared_info->pll_info.reference_frequency,
1978 			&wrpll_params);
1979 		fPipe->ConfigureClocksSKL(wrpll_params,
1980 			hardwareTarget.pixel_clock,
1981 			PortIndex(),
1982 			&pllSel);
1983 	}
1984 
1985 	// Program target display mode
1986 	fPipe->ConfigureTimings(target, !needsScaling);
1987 	_SetPortLinkGen8(hardwareTarget, pllSel);
1988 
1989 	// Set fCurrentMode to our set display mode
1990 	memcpy(&fCurrentMode, target, sizeof(display_mode));
1991 
1992 	return B_OK;
1993 }
1994