xref: /haiku/src/add-ons/accelerants/intel_extreme/mode.cpp (revision a4ef4a49150f118d47324242917a596a3f8f8bd5)
1 /*
2  * Copyright 2006-2008, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Support for i915 chipset and up based on the X driver,
6  * Copyright 2006-2007 Intel Corporation.
7  *
8  * Authors:
9  *		Axel Dörfler, axeld@pinc-software.de
10  */
11 
12 
13 #include "accelerant_protos.h"
14 #include "accelerant.h"
15 #include "utility.h"
16 
17 #include <stdio.h>
18 #include <string.h>
19 #include <math.h>
20 
21 #include <create_display_modes.h>
22 #include <ddc.h>
23 #include <edid.h>
24 
25 
26 #define TRACE_MODE
27 #ifdef TRACE_MODE
28 extern "C" void _sPrintf(const char *format, ...);
29 #	define TRACE(x) _sPrintf x
30 #else
31 #	define TRACE(x) ;
32 #endif
33 
34 
35 struct display_registers {
36 	uint32	pll;
37 	uint32	divisors;
38 	uint32	control;
39 	uint32	pipe_config;
40 	uint32	horiz_total;
41 	uint32	horiz_blank;
42 	uint32	horiz_sync;
43 	uint32	vert_total;
44 	uint32	vert_blank;
45 	uint32	vert_sync;
46 	uint32	size;
47 	uint32	stride;
48 	uint32	position;
49 	uint32	pipe_source;
50 };
51 
52 struct pll_divisors {
53 	uint32	post;
54 	uint32	post1;
55 	uint32	post2;
56 	bool	post2_high;
57 	uint32	n;
58 	uint32	m;
59 	uint32	m1;
60 	uint32	m2;
61 };
62 
63 struct pll_limits {
64 	pll_divisors	min;
65 	pll_divisors	max;
66 	uint32			min_post2_frequency;
67 	uint32			min_vco;
68 	uint32			max_vco;
69 };
70 
71 
72 static status_t
73 get_i2c_signals(void* cookie, int* _clock, int* _data)
74 {
75 	uint32 ioRegister = (uint32)cookie;
76 	uint32 value = read32(ioRegister);
77 
78 	*_clock = (value & I2C_CLOCK_VALUE_IN) != 0;
79 	*_data = (value & I2C_DATA_VALUE_IN) != 0;
80 
81 	return B_OK;
82 }
83 
84 
85 static status_t
86 set_i2c_signals(void* cookie, int clock, int data)
87 {
88 	uint32 ioRegister = (uint32)cookie;
89 	uint32 value;
90 
91 	if (gInfo->shared_info->device_type == INTEL_TYPE_83x) {
92 		// on these chips, the reserved values are fixed
93 		value = 0;
94 	} else {
95 		// on all others, we have to preserve them manually
96 		value = read32(ioRegister) & I2C_RESERVED;
97 	}
98 
99 	if (data != 0)
100 		value |= I2C_DATA_DIRECTION_MASK;
101 	else
102 		value |= I2C_DATA_DIRECTION_MASK | I2C_DATA_DIRECTION_OUT | I2C_DATA_VALUE_MASK;
103 
104 	if (clock != 0)
105 		value |= I2C_CLOCK_DIRECTION_MASK;
106 	else
107 		value |= I2C_CLOCK_DIRECTION_MASK | I2C_CLOCK_DIRECTION_OUT | I2C_CLOCK_VALUE_MASK;
108 
109 	write32(ioRegister, value);
110 	read32(ioRegister);
111 		// make sure the PCI bus has flushed the write
112 
113 	return B_OK;
114 }
115 
116 
117 void
118 set_frame_buffer_base()
119 {
120 	intel_shared_info &sharedInfo = *gInfo->shared_info;
121 	display_mode &mode = sharedInfo.current_mode;
122 	uint32 baseRegister;
123 	uint32 surfaceRegister;
124 
125 	if (gInfo->head_mode & HEAD_MODE_A_ANALOG) {
126 		baseRegister = INTEL_DISPLAY_A_BASE;
127 		surfaceRegister = INTEL_DISPLAY_A_SURFACE;
128 	} else {
129 		baseRegister = INTEL_DISPLAY_B_BASE;
130 		surfaceRegister = INTEL_DISPLAY_B_SURFACE;
131 	}
132 
133 	if (sharedInfo.device_type == INTEL_TYPE_965) {
134 		write32(baseRegister, mode.v_display_start * sharedInfo.bytes_per_row
135 			+ mode.h_display_start * (sharedInfo.bits_per_pixel + 7) / 8);
136 		read32(baseRegister);
137 		write32(surfaceRegister, sharedInfo.frame_buffer_offset);
138 		read32(surfaceRegister);
139 	} else {
140 		write32(baseRegister, sharedInfo.frame_buffer_offset
141 			+ mode.v_display_start * sharedInfo.bytes_per_row
142 			+ mode.h_display_start * (sharedInfo.bits_per_pixel + 7) / 8);
143 		read32(baseRegister);
144 	}
145 }
146 
147 
148 /*!	Creates the initial mode list of the primary accelerant.
149 	It's called from intel_init_accelerant().
150 */
151 status_t
152 create_mode_list(void)
153 {
154 	i2c_bus bus;
155 	bus.cookie = (void*)INTEL_I2C_IO_A;
156 	bus.set_signals = &set_i2c_signals;
157 	bus.get_signals = &get_i2c_signals;
158 	ddc2_init_timing(&bus);
159 
160 	if (ddc2_read_edid1(&bus, &gInfo->edid_info, NULL, NULL) == B_OK) {
161 		edid_dump(&gInfo->edid_info);
162 		gInfo->has_edid = true;
163 	} else {
164 		TRACE(("intel_extreme: getting EDID failed!\n"));
165 	}
166 
167 	// TODO: support lower modes via scaling and windowing
168 	if (gInfo->head_mode & HEAD_MODE_LVDS_PANEL
169 		&& ((gInfo->head_mode & HEAD_MODE_A_ANALOG) == 0)) {
170 		size_t size = (sizeof(display_mode) + B_PAGE_SIZE - 1)
171 			& ~(B_PAGE_SIZE - 1);
172 
173 		display_mode *list;
174 		area_id area = create_area("intel extreme modes", (void **)&list,
175 			B_ANY_ADDRESS, size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
176 		if (area < B_OK)
177 			return area;
178 
179 		memcpy(list, &gInfo->lvds_panel_mode, sizeof(display_mode));
180 
181 		gInfo->mode_list_area = area;
182 		gInfo->mode_list = list;
183 		gInfo->shared_info->mode_list_area = gInfo->mode_list_area;
184 		gInfo->shared_info->mode_count = 1;
185 		return B_OK;
186 	}
187 
188 	// Otherwise return the 'real' list of modes
189 	display_mode *list;
190 	uint32 count = 0;
191 	gInfo->mode_list_area = create_display_modes("intel extreme modes",
192 		gInfo->has_edid ? &gInfo->edid_info : NULL, NULL, 0, NULL, 0, NULL,
193 		&list, &count);
194 	if (gInfo->mode_list_area < B_OK)
195 		return gInfo->mode_list_area;
196 
197 	gInfo->mode_list = list;
198 	gInfo->shared_info->mode_list_area = gInfo->mode_list_area;
199 	gInfo->shared_info->mode_count = count;
200 
201 	return B_OK;
202 }
203 
204 
205 void
206 wait_for_vblank(void)
207 {
208 	acquire_sem_etc(gInfo->shared_info->vblank_sem, 1, B_RELATIVE_TIMEOUT, 25000);
209 		// With the output turned off via DPMS, we might not get any interrupts anymore
210 		// that's why we don't wait forever for it.
211 }
212 
213 
214 static void
215 get_pll_limits(pll_limits &limits)
216 {
217 	// Note, the limits are taken from the X driver; they have not yet been
218 	// tested
219 
220 	if ((gInfo->shared_info->device_type & INTEL_TYPE_9xx) != 0) {
221 		// TODO: support LVDS output limits as well
222 		// (Update: Output limits are adjusted in the computation (post2=7/14))
223 		// Should move them here!
224 		static const pll_limits kLimits = {
225 			// p, p1, p2, high,   n,   m, m1, m2
226 			{  5,  1, 10, false,  5,  70, 12,  7},	// min
227 			{ 80,  8,  5, true,  10, 120, 22, 11},	// max
228 			200000, 1400000, 2800000
229 		};
230 		limits = kLimits;
231 	} else {
232 		// TODO: support LVDS output limits as well
233 		static const pll_limits kLimits = {
234 			// p, p1, p2, high,   n,   m, m1, m2
235 			{  4,  2,  4, false,  5,  96, 20,  8},
236 			{128, 33,  2, true,  18, 140, 28, 18},
237 			165000, 930000, 1400000
238 		};
239 		limits = kLimits;
240 	}
241 
242 	TRACE(("PLL limits, min: p %lu (p1 %lu, p2 %lu), n %lu, m %lu (m1 %lu, m2 %lu)\n",
243 		limits.min.post, limits.min.post1, limits.min.post2, limits.min.n,
244 		limits.min.m, limits.min.m1, limits.min.m2));
245 	TRACE(("PLL limits, max: p %lu (p1 %lu, p2 %lu), n %lu, m %lu (m1 %lu, m2 %lu)\n",
246 		limits.max.post, limits.max.post1, limits.max.post2, limits.max.n,
247 		limits.max.m, limits.max.m1, limits.max.m2));
248 }
249 
250 
251 static bool
252 valid_pll_divisors(const pll_divisors& divisors, const pll_limits& limits)
253 {
254 	pll_info &info = gInfo->shared_info->pll_info;
255 	uint32 vco = info.reference_frequency * divisors.m / divisors.n;
256 	uint32 frequency = vco / divisors.post;
257 
258 	if (divisors.post < limits.min.post || divisors.post > limits.max.post
259 		|| divisors.m < limits.min.m || divisors.m > limits.max.m
260 		|| vco < limits.min_vco || vco > limits.max_vco
261 		|| frequency < info.min_frequency || frequency > info.max_frequency)
262 		return false;
263 
264 	return true;
265 }
266 
267 
268 static void
269 compute_pll_divisors(const display_mode &current, pll_divisors& divisors,
270 	bool isLVDS)
271 {
272 	float requestedPixelClock = current.timing.pixel_clock / 1000.0f;
273 	float referenceClock = gInfo->shared_info->pll_info.reference_frequency / 1000.0f;
274 	pll_limits limits;
275 	get_pll_limits(limits);
276 
277 	TRACE(("required MHz: %g\n", requestedPixelClock));
278 
279 	if (isLVDS) {
280 		if ((read32(INTEL_DISPLAY_LVDS_PORT) & LVDS_CLKB_POWER_MASK)
281 				== LVDS_CLKB_POWER_UP)
282 			divisors.post2 = LVDS_POST2_RATE_FAST;
283 		else
284 			divisors.post2 = LVDS_POST2_RATE_SLOW;
285 	} else {
286 		if (current.timing.pixel_clock < limits.min_post2_frequency) {
287 			// slow DAC timing
288 			divisors.post2 = limits.min.post2;
289 			divisors.post2_high = limits.min.post2_high;
290 		} else {
291 			// fast DAC timing
292 			divisors.post2 = limits.max.post2;
293 			divisors.post2_high = limits.max.post2_high;
294 		}
295 	}
296 
297 	float best = requestedPixelClock;
298 	pll_divisors bestDivisors;
299 
300 	for (divisors.m1 = limits.min.m1; divisors.m1 <= limits.max.m1; divisors.m1++) {
301 		for (divisors.m2 = limits.min.m2; divisors.m2 < divisors.m1
302 				&& divisors.m2 <= limits.max.m2; divisors.m2++) {
303 			for (divisors.n = limits.min.n; divisors.n <= limits.max.n;
304 					divisors.n++) {
305 				for (divisors.post1 = limits.min.post1;
306 						divisors.post1 <= limits.max.post1; divisors.post1++) {
307 					divisors.m = 5 * divisors.m1 + divisors.m2;
308 					divisors.post = divisors.post1 * divisors.post2;
309 
310 					if (!valid_pll_divisors(divisors, limits))
311 						continue;
312 
313 					float error = fabs(requestedPixelClock
314 						- ((referenceClock * divisors.m) / divisors.n) / divisors.post);
315 					if (error < best) {
316 						best = error;
317 						bestDivisors = divisors;
318 
319 						if (error == 0)
320 							break;
321 					}
322 				}
323 			}
324 		}
325 	}
326 
327 	divisors = bestDivisors;
328 
329 	TRACE(("found: %g MHz, p = %lu (p1 = %lu, p2 = %lu), n = %lu, m = %lu (m1 = %lu, m2 = %lu)\n",
330 		((referenceClock * divisors.m) / divisors.n) / divisors.post,
331 		divisors.post, divisors.post1, divisors.post2, divisors.n,
332 		divisors.m, divisors.m1, divisors.m2));
333 }
334 
335 
336 /*! Store away panel information if identified on startup
337 	(used for pipe B->lvds).
338 */
339 void
340 save_lvds_mode(void)
341 {
342 	// dump currently programmed mode.
343 	display_mode biosMode;
344 
345 	uint32 pll = read32(INTEL_DISPLAY_B_PLL);
346 	uint32 pllDivisor = read32(INTEL_DISPLAY_B_PLL_DIVISOR_0);
347 
348 	pll_divisors divisors;
349 	divisors.m1 = (pllDivisor & DISPLAY_PLL_M1_DIVISOR_MASK)
350 		>> DISPLAY_PLL_M1_DIVISOR_SHIFT;
351 	divisors.m2 = (pllDivisor & DISPLAY_PLL_M2_DIVISOR_MASK)
352 		>> DISPLAY_PLL_M2_DIVISOR_SHIFT;
353 	divisors.n = (pllDivisor & DISPLAY_PLL_N_DIVISOR_MASK)
354 		>> DISPLAY_PLL_N_DIVISOR_SHIFT;
355 
356 	pll_limits limits;
357 	get_pll_limits(limits);
358 
359 	if ((gInfo->shared_info->device_type & INTEL_TYPE_9xx) != 0) {
360 		divisors.post1 = (pll & DISPLAY_PLL_9xx_POST1_DIVISOR_MASK)
361 			>> DISPLAY_PLL_POST1_DIVISOR_SHIFT;
362 
363 		if ((pll & DISPLAY_PLL_DIVIDE_HIGH) != 0)
364 			divisors.post2 = limits.max.post2;
365 		else
366 			divisors.post2 = limits.min.post2;
367 
368 		// Fix this? Need to support dual channel LVDS.
369 		divisors.post2 = LVDS_POST2_RATE_SLOW;
370 	} else {
371 		// 8xx
372 		divisors.post1 = (pll & DISPLAY_PLL_POST1_DIVISOR_MASK)
373 			>> DISPLAY_PLL_POST1_DIVISOR_SHIFT;
374 
375 		if ((pll & DISPLAY_PLL_DIVIDE_4X) != 0)
376 			divisors.post2 = limits.max.post2;
377 		else
378 			divisors.post2 = limits.min.post2;
379 	}
380 
381 	divisors.m = 5 * divisors.m1 + divisors.m2;
382 	divisors.post = divisors.post1 * divisors.post2;
383 
384 	float referenceClock = gInfo->shared_info->pll_info.reference_frequency
385 		/ 1000.0f;
386 	float pixelClock = ((referenceClock * divisors.m) / divisors.n)
387 		/ divisors.post;
388 
389 	// timing
390 
391 	biosMode.timing.pixel_clock = uint32(pixelClock * 1000);
392 	biosMode.timing.flags = 0;
393 
394 	uint32 value = read32(INTEL_DISPLAY_B_HTOTAL);
395 	biosMode.timing.h_total = (value >> 16) + 1;
396 	biosMode.timing.h_display = (value & 0xffff) + 1;
397 
398 	value = read32(INTEL_DISPLAY_B_HSYNC);
399 	biosMode.timing.h_sync_end = (value >> 16) + 1;
400 	biosMode.timing.h_sync_start = (value & 0xffff) + 1;
401 
402 	value = read32(INTEL_DISPLAY_B_VTOTAL);
403 	biosMode.timing.v_total = (value >> 16) + 1;
404 	biosMode.timing.v_display = (value & 0xffff) + 1;
405 
406 	value = read32(INTEL_DISPLAY_B_VSYNC);
407 	biosMode.timing.v_sync_end = (value >> 16) + 1;
408 	biosMode.timing.v_sync_start = (value & 0xffff) + 1;
409 
410 	// image size and color space
411 
412 	// using virtual size based on image size is the 'proper' way to do it, however the bios appears to be
413 	// suggesting scaling or somesuch, so ignore the proper virtual way for now.
414 
415 	biosMode.virtual_width = biosMode.timing.h_display;
416 	biosMode.virtual_height = biosMode.timing.v_display;
417 
418 	//value = read32(INTEL_DISPLAY_B_IMAGE_SIZE);
419 	//biosMode.virtual_width = (value >> 16) + 1;
420 	//biosMode.virtual_height = (value & 0xffff) + 1;
421 
422 	value = read32(INTEL_DISPLAY_B_CONTROL);
423 	switch (value & DISPLAY_CONTROL_COLOR_MASK) {
424 		case DISPLAY_CONTROL_RGB32:
425 		default:
426 			biosMode.space = B_RGB32;
427 			break;
428 		case DISPLAY_CONTROL_RGB16:
429 			biosMode.space = B_RGB16;
430 			break;
431 		case DISPLAY_CONTROL_RGB15:
432 			biosMode.space = B_RGB15;
433 			break;
434 		case DISPLAY_CONTROL_CMAP8:
435 			biosMode.space = B_CMAP8;
436 			break;
437 	}
438 
439 	biosMode.h_display_start = 0;
440 	biosMode.v_display_start = 0;
441 	biosMode.flags = 0;
442 
443 	gInfo->lvds_panel_mode = biosMode;
444 }
445 
446 
447 static void
448 get_color_space_format(const display_mode &mode, uint32 &colorMode,
449 	uint32 &bytesPerRow, uint32 &bitsPerPixel)
450 {
451 	uint32 bytesPerPixel;
452 
453 	switch (mode.space) {
454 		case B_RGB32_LITTLE:
455 			colorMode = DISPLAY_CONTROL_RGB32;
456 			bytesPerPixel = 4;
457 			bitsPerPixel = 32;
458 			break;
459 		case B_RGB16_LITTLE:
460 			colorMode = DISPLAY_CONTROL_RGB16;
461 			bytesPerPixel = 2;
462 			bitsPerPixel = 16;
463 			break;
464 		case B_RGB15_LITTLE:
465 			colorMode = DISPLAY_CONTROL_RGB15;
466 			bytesPerPixel = 2;
467 			bitsPerPixel = 15;
468 			break;
469 		case B_CMAP8:
470 		default:
471 			colorMode = DISPLAY_CONTROL_CMAP8;
472 			bytesPerPixel = 1;
473 			bitsPerPixel = 8;
474 			break;
475 	}
476 
477 	bytesPerRow = mode.virtual_width * bytesPerPixel;
478 }
479 
480 
481 //	#pragma mark -
482 
483 
484 uint32
485 intel_accelerant_mode_count(void)
486 {
487 	TRACE(("intel_accelerant_mode_count()\n"));
488 	return gInfo->shared_info->mode_count;
489 }
490 
491 
492 status_t
493 intel_get_mode_list(display_mode *modeList)
494 {
495 	TRACE(("intel_get_mode_info()\n"));
496 	memcpy(modeList, gInfo->mode_list,
497 		gInfo->shared_info->mode_count * sizeof(display_mode));
498 	return B_OK;
499 }
500 
501 
502 status_t
503 intel_propose_display_mode(display_mode *target, const display_mode *low,
504 	const display_mode *high)
505 {
506 	TRACE(("intel_propose_display_mode()\n"));
507 
508 	// just search for the specified mode in the list
509 
510 	for (uint32 i = 0; i < gInfo->shared_info->mode_count; i++) {
511 		display_mode *mode = &gInfo->mode_list[i];
512 
513 		// TODO: improve this, ie. adapt pixel clock to allowed values!!!
514 
515 		if (target->virtual_width != mode->virtual_width
516 			|| target->virtual_height != mode->virtual_height
517 			|| target->space != mode->space)
518 			continue;
519 
520 		*target = *mode;
521 		return B_OK;
522 	}
523 	return B_BAD_VALUE;
524 }
525 
526 
527 status_t
528 intel_set_display_mode(display_mode *mode)
529 {
530 	TRACE(("intel_set_display_mode()\n"));
531 
532 	if (mode == NULL)
533 		return B_BAD_VALUE;
534 
535 	display_mode target = *mode;
536 	if (intel_propose_display_mode(&target, mode, mode))
537 		return B_BAD_VALUE;
538 
539 	uint32 colorMode, bytesPerRow, bitsPerPixel;
540 	get_color_space_format(target, colorMode, bytesPerRow, bitsPerPixel);
541 
542 #if 0
543 static bool first = true;
544 if (first) {
545 	int fd = open("/boot/home/ie_.regs", O_CREAT | O_WRONLY, 0644);
546 	if (fd >= 0) {
547 		for (int32 i = 0; i < 0x80000; i += 16) {
548 			char line[512];
549 			int length = sprintf(line, "%05lx: %08lx %08lx %08lx %08lx\n",
550 				i, read32(i), read32(i + 4), read32(i + 8), read32(i + 12));
551 			write(fd, line, length);
552 		}
553 		close(fd);
554 		sync();
555 	}
556 	first = false;
557 }
558 #endif
559 
560 	intel_shared_info &sharedInfo = *gInfo->shared_info;
561 	Autolock locker(sharedInfo.accelerant_lock);
562 
563 	// TODO: This may not be neccesary
564 	set_display_power_mode(B_DPMS_OFF);
565 
566 	// free old and allocate new frame buffer in graphics memory
567 
568 	intel_free_memory(sharedInfo.frame_buffer);
569 
570 	uint32 base;
571 	if (intel_allocate_memory(bytesPerRow * target.virtual_height, 0,
572 			base) < B_OK) {
573 		// oh, how did that happen? Unfortunately, there is no really good way back
574 		if (intel_allocate_memory(sharedInfo.current_mode.virtual_height
575 				* sharedInfo.bytes_per_row, 0, base) == B_OK) {
576 			sharedInfo.frame_buffer = base;
577 			sharedInfo.frame_buffer_offset = base
578 				- (addr_t)sharedInfo.graphics_memory;
579 			set_frame_buffer_base();
580 		}
581 
582 		return B_NO_MEMORY;
583 	}
584 
585 	// clear frame buffer before using it
586 	memset((uint8 *)base, 0, bytesPerRow * target.virtual_height);
587 	sharedInfo.frame_buffer = base;
588 	sharedInfo.frame_buffer_offset = base - (addr_t)sharedInfo.graphics_memory;
589 
590 	// make sure VGA display is disabled
591 	write32(INTEL_VGA_DISPLAY_CONTROL, VGA_DISPLAY_DISABLED);
592 	read32(INTEL_VGA_DISPLAY_CONTROL);
593 
594 	if (gInfo->shared_info->device_type != INTEL_TYPE_85x) {
595 	}
596 
597 	if ((gInfo->head_mode & HEAD_MODE_B_DIGITAL) != 0) {
598 		pll_divisors divisors;
599 		compute_pll_divisors(target, divisors, true);
600 
601 		uint32 dpll = DISPLAY_PLL_NO_VGA_CONTROL;
602 		if ((gInfo->shared_info->device_type & INTEL_TYPE_9xx) != 0) {
603 			 dpll |= LVDS_PLL_MODE_LVDS;
604 			 	// DPLL mode LVDS for i915+
605 		}
606 
607 		// compute bitmask from p1 value
608 		dpll |= (1 << (divisors.post1 - 1)) << 16;
609 		switch (divisors.post2) {
610 			case 5:
611 			case 7:
612 				dpll |= DISPLAY_PLL_DIVIDE_HIGH;
613 				break;
614 		}
615 
616 		dpll |= (1 << (divisors.post1 - 1)) << DISPLAY_PLL_POST1_DIVISOR_SHIFT;
617 
618 		uint32 displayControl = ~(DISPLAY_CONTROL_COLOR_MASK
619 			| DISPLAY_CONTROL_GAMMA) | colorMode;
620 		displayControl |= 1 << 24; // select pipe B
621 
622 		// runs in dpms also?
623 		displayControl |= DISPLAY_PIPE_ENABLED;
624 		dpll |= DISPLAY_PLL_ENABLED;
625 
626 		write32(INTEL_PANEL_FIT_CONTROL, 0);
627 
628 		if ((dpll & DISPLAY_PLL_ENABLED) != 0) {
629 			write32(INTEL_DISPLAY_B_PLL_DIVISOR_0,
630 				(((divisors.n - 2) << DISPLAY_PLL_N_DIVISOR_SHIFT) & DISPLAY_PLL_N_DIVISOR_MASK)
631 				| (((divisors.m1 - 2) << DISPLAY_PLL_M1_DIVISOR_SHIFT) & DISPLAY_PLL_M1_DIVISOR_MASK)
632 				| (((divisors.m2 - 2) << DISPLAY_PLL_M2_DIVISOR_SHIFT) & DISPLAY_PLL_M2_DIVISOR_MASK));
633 			write32(INTEL_DISPLAY_B_PLL, dpll & ~DISPLAY_PLL_ENABLED);
634 			read32(INTEL_DISPLAY_B_PLL);
635 			spin(150);
636 		}
637 
638 		uint32 lvds = read32(INTEL_DISPLAY_LVDS_PORT)
639 			| LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
640 
641 		float referenceClock = gInfo->shared_info->pll_info.reference_frequency
642 			/ 1000.0f;
643 
644 		// Set the B0-B3 data pairs corresponding to whether we're going to
645 		// set the DPLLs for dual-channel mode or not.
646 		if (divisors.post2 == LVDS_POST2_RATE_FAST)
647 			lvds |= LVDS_B0B3PAIRS_POWER_UP | LVDS_CLKB_POWER_UP;
648 		else
649 			lvds &= ~( LVDS_B0B3PAIRS_POWER_UP | LVDS_CLKB_POWER_UP);
650 
651 		write32(INTEL_DISPLAY_LVDS_PORT, lvds);
652 		read32(INTEL_DISPLAY_LVDS_PORT);
653 
654 		write32(INTEL_DISPLAY_B_PLL_DIVISOR_0,
655 			(((divisors.n - 2) << DISPLAY_PLL_N_DIVISOR_SHIFT) & DISPLAY_PLL_N_DIVISOR_MASK)
656 			| (((divisors.m1 - 2) << DISPLAY_PLL_M1_DIVISOR_SHIFT) & DISPLAY_PLL_M1_DIVISOR_MASK)
657 			| (((divisors.m2 - 2) << DISPLAY_PLL_M2_DIVISOR_SHIFT) & DISPLAY_PLL_M2_DIVISOR_MASK));
658 
659 		write32(INTEL_DISPLAY_B_PLL, dpll);
660 		read32(INTEL_DISPLAY_B_PLL);
661 
662 		// Wait for the clocks to stabilize
663 		spin(150);
664 
665 		if (gInfo->shared_info->device_type == INTEL_TYPE_965) {
666 			float adjusted = ((referenceClock * divisors.m) / divisors.n)
667 				/ divisors.post;
668 			uint32 pixelMultiply = uint32(adjusted
669 				/ (target.timing.pixel_clock / 1000.0f));
670 
671 			write32(INTEL_DISPLAY_B_PLL_MULTIPLIER_DIVISOR, (0 << 24)
672 				| ((pixelMultiply - 1) << 8));
673 		} else
674 			write32(INTEL_DISPLAY_B_PLL, dpll);
675 
676 		read32(INTEL_DISPLAY_B_PLL);
677 		spin(150);
678 
679 		// update timing parameters
680 		write32(INTEL_DISPLAY_B_HTOTAL, ((uint32)(target.timing.h_total - 1) << 16)
681 			| ((uint32)target.timing.h_display - 1));
682 		write32(INTEL_DISPLAY_B_HBLANK, ((uint32)(target.timing.h_total - 1) << 16)
683 			| ((uint32)target.timing.h_display - 1));
684 		write32(INTEL_DISPLAY_B_HSYNC, ((uint32)(target.timing.h_sync_end - 1) << 16)
685 			| ((uint32)target.timing.h_sync_start - 1));
686 
687 		write32(INTEL_DISPLAY_B_VTOTAL, ((uint32)(target.timing.v_total - 1) << 16)
688 			| ((uint32)target.timing.v_display - 1));
689 		write32(INTEL_DISPLAY_B_VBLANK, ((uint32)(target.timing.v_total - 1) << 16)
690 			| ((uint32)target.timing.v_display - 1));
691 		write32(INTEL_DISPLAY_B_VSYNC, ((uint32)(target.timing.v_sync_end - 1) << 16)
692 			| ((uint32)target.timing.v_sync_start - 1));
693 
694 		write32(INTEL_DISPLAY_B_IMAGE_SIZE, ((uint32)(target.timing.h_display - 1) << 16)
695 			| ((uint32)target.timing.v_display - 1));
696 
697 		write32(INTEL_DISPLAY_B_POS, 0);
698 		write32(INTEL_DISPLAY_B_PIPE_SIZE, ((uint32)(target.timing.v_display - 1) << 16)
699 			| ((uint32)target.timing.h_display - 1));
700 
701 		write32(INTEL_DISPLAY_B_PIPE_CONTROL,
702 			read32(INTEL_DISPLAY_B_PIPE_CONTROL) | DISPLAY_PIPE_ENABLED);
703 		read32(INTEL_DISPLAY_B_PIPE_CONTROL);
704 	}
705 
706 	if (gInfo->head_mode & HEAD_MODE_A_ANALOG) {
707 		pll_divisors divisors;
708 		compute_pll_divisors(target, divisors,false);
709 
710 		write32(INTEL_DISPLAY_A_PLL_DIVISOR_0,
711 			(((divisors.n - 2) << DISPLAY_PLL_N_DIVISOR_SHIFT) & DISPLAY_PLL_N_DIVISOR_MASK)
712 			| (((divisors.m1 - 2) << DISPLAY_PLL_M1_DIVISOR_SHIFT) & DISPLAY_PLL_M1_DIVISOR_MASK)
713 			| (((divisors.m2 - 2) << DISPLAY_PLL_M2_DIVISOR_SHIFT) & DISPLAY_PLL_M2_DIVISOR_MASK));
714 
715 		uint32 pll = DISPLAY_PLL_ENABLED | DISPLAY_PLL_NO_VGA_CONTROL;
716 		if ((gInfo->shared_info->device_type & INTEL_TYPE_9xx) != 0) {
717 			pll |= ((1 << (divisors.post1 - 1)) << DISPLAY_PLL_POST1_DIVISOR_SHIFT)
718 				& DISPLAY_PLL_9xx_POST1_DIVISOR_MASK;
719 //			pll |= ((divisors.post1 - 1) << DISPLAY_PLL_POST1_DIVISOR_SHIFT)
720 //				& DISPLAY_PLL_9xx_POST1_DIVISOR_MASK;
721 			if (divisors.post2_high)
722 				pll |= DISPLAY_PLL_DIVIDE_HIGH;
723 
724 			pll |= DISPLAY_PLL_MODE_ANALOG;
725 
726 			if (gInfo->shared_info->device_type == INTEL_TYPE_965)
727 				pll |= 6 << DISPLAY_PLL_PULSE_PHASE_SHIFT;
728 		} else {
729 			if (!divisors.post2_high)
730 				pll |= DISPLAY_PLL_DIVIDE_4X;
731 
732 			pll |= DISPLAY_PLL_2X_CLOCK;
733 
734 			if (divisors.post1 > 2) {
735 				pll |= (((divisors.post1 - 2) << DISPLAY_PLL_POST1_DIVISOR_SHIFT)
736 					& DISPLAY_PLL_POST1_DIVISOR_MASK);
737 			} else
738 				pll |= DISPLAY_PLL_POST1_DIVIDE_2;
739 		}
740 
741 		write32(INTEL_DISPLAY_A_PLL, pll);
742 		read32(INTEL_DISPLAY_A_PLL);
743 		spin(150);
744 		write32(INTEL_DISPLAY_A_PLL, pll);
745 		read32(INTEL_DISPLAY_A_PLL);
746 		spin(150);
747 
748 		// update timing parameters
749 		write32(INTEL_DISPLAY_A_HTOTAL, ((uint32)(target.timing.h_total - 1) << 16)
750 			| ((uint32)target.timing.h_display - 1));
751 		write32(INTEL_DISPLAY_A_HBLANK, ((uint32)(target.timing.h_total - 1) << 16)
752 			| ((uint32)target.timing.h_display - 1));
753 		write32(INTEL_DISPLAY_A_HSYNC, ((uint32)(target.timing.h_sync_end - 1) << 16)
754 			| ((uint32)target.timing.h_sync_start - 1));
755 
756 		write32(INTEL_DISPLAY_A_VTOTAL, ((uint32)(target.timing.v_total - 1) << 16)
757 			| ((uint32)target.timing.v_display - 1));
758 		write32(INTEL_DISPLAY_A_VBLANK, ((uint32)(target.timing.v_total - 1) << 16)
759 			| ((uint32)target.timing.v_display - 1));
760 		write32(INTEL_DISPLAY_A_VSYNC, ((uint32)(target.timing.v_sync_end - 1) << 16)
761 			| ((uint32)target.timing.v_sync_start - 1));
762 
763 		write32(INTEL_DISPLAY_A_IMAGE_SIZE, ((uint32)(target.timing.h_display - 1) << 16)
764 			| ((uint32)target.timing.v_display - 1));
765 
766 		write32(INTEL_DISPLAY_A_ANALOG_PORT, (read32(INTEL_DISPLAY_A_ANALOG_PORT)
767 			& ~(DISPLAY_MONITOR_POLARITY_MASK | DISPLAY_MONITOR_VGA_POLARITY))
768 			| ((target.timing.flags & B_POSITIVE_HSYNC) != 0 ? DISPLAY_MONITOR_POSITIVE_HSYNC : 0)
769 			| ((target.timing.flags & B_POSITIVE_VSYNC) != 0 ? DISPLAY_MONITOR_POSITIVE_VSYNC : 0));
770 
771 		// TODO: verify the two comments below: the X driver doesn't seem to
772 		//		care about both of them!
773 
774 		// These two have to be set for display B, too - this obviously means
775 		// that the second head always must adopt the color space of the first
776 		// head.
777 		write32(INTEL_DISPLAY_A_CONTROL, (read32(INTEL_DISPLAY_A_CONTROL)
778 			& ~(DISPLAY_CONTROL_COLOR_MASK | DISPLAY_CONTROL_GAMMA)) | colorMode);
779 
780 		if (gInfo->head_mode & HEAD_MODE_B_DIGITAL) {
781 			write32(INTEL_DISPLAY_B_IMAGE_SIZE, ((uint32)(target.timing.h_display - 1) << 16)
782 				| ((uint32)target.timing.v_display - 1));
783 
784 			write32(INTEL_DISPLAY_B_CONTROL, (read32(INTEL_DISPLAY_B_CONTROL)
785 				& ~(DISPLAY_CONTROL_COLOR_MASK | DISPLAY_CONTROL_GAMMA)) | colorMode);
786 		}
787 	}
788 
789 	set_display_power_mode(sharedInfo.dpms_mode);
790 
791 	// changing bytes per row seems to be ignored if the plane/pipe is turned off
792 
793 	if (gInfo->head_mode & HEAD_MODE_A_ANALOG)
794 		write32(INTEL_DISPLAY_A_BYTES_PER_ROW, bytesPerRow);
795 	if (gInfo->head_mode & HEAD_MODE_B_DIGITAL)
796 		write32(INTEL_DISPLAY_B_BYTES_PER_ROW, bytesPerRow);
797 
798 	set_frame_buffer_base();
799 		// triggers writing back double-buffered registers
800 
801 	// update shared info
802 	sharedInfo.bytes_per_row = bytesPerRow;
803 	sharedInfo.current_mode = target;
804 	sharedInfo.bits_per_pixel = bitsPerPixel;
805 
806 	return B_OK;
807 }
808 
809 
810 status_t
811 intel_get_display_mode(display_mode *_currentMode)
812 {
813 	TRACE(("intel_get_display_mode()\n"));
814 
815 	display_mode &mode = *_currentMode;
816 
817 	uint32 pll = read32(INTEL_DISPLAY_A_PLL);
818 	uint32 pllDivisor = read32((pll & DISPLAY_PLL_DIVISOR_1) != 0
819 		? INTEL_DISPLAY_A_PLL_DIVISOR_1 : INTEL_DISPLAY_A_PLL_DIVISOR_0);
820 
821 	pll_divisors divisors;
822 	divisors.m1 = (pllDivisor & DISPLAY_PLL_M1_DIVISOR_MASK)
823 		>> DISPLAY_PLL_M1_DIVISOR_SHIFT;
824 	divisors.m2 = (pllDivisor & DISPLAY_PLL_M2_DIVISOR_MASK)
825 		>> DISPLAY_PLL_M2_DIVISOR_SHIFT;
826 	divisors.n = (pllDivisor & DISPLAY_PLL_N_DIVISOR_MASK)
827 		>> DISPLAY_PLL_N_DIVISOR_SHIFT;
828 
829 	pll_limits limits;
830 	get_pll_limits(limits);
831 
832 	if ((gInfo->shared_info->device_type & INTEL_TYPE_9xx) != 0) {
833 		divisors.post1 = (pll & DISPLAY_PLL_9xx_POST1_DIVISOR_MASK)
834 			>> DISPLAY_PLL_POST1_DIVISOR_SHIFT;
835 
836 		if ((pll & DISPLAY_PLL_DIVIDE_HIGH) != 0)
837 			divisors.post2 = limits.max.post2;
838 		else
839 			divisors.post2 = limits.min.post2;
840 	} else {
841 		// 8xx
842 		divisors.post1 = (pll & DISPLAY_PLL_POST1_DIVISOR_MASK)
843 			>> DISPLAY_PLL_POST1_DIVISOR_SHIFT;
844 
845 		if ((pll & DISPLAY_PLL_DIVIDE_4X) != 0)
846 			divisors.post2 = limits.max.post2;
847 		else
848 			divisors.post2 = limits.min.post2;
849 	}
850 
851 	divisors.m = 5 * divisors.m1 + divisors.m2;
852 	divisors.post = divisors.post1 * divisors.post2;
853 
854 	float referenceClock = gInfo->shared_info->pll_info.reference_frequency / 1000.0f;
855 	float pixelClock = ((referenceClock * divisors.m) / divisors.n) / divisors.post;
856 
857 	// timing
858 
859 	mode.timing.pixel_clock = uint32(pixelClock * 1000);
860 	mode.timing.flags = 0;
861 
862 	uint32 value = read32(INTEL_DISPLAY_A_HTOTAL);
863 	mode.timing.h_total = (value >> 16) + 1;
864 	mode.timing.h_display = (value & 0xffff) + 1;
865 
866 	value = read32(INTEL_DISPLAY_A_HSYNC);
867 	mode.timing.h_sync_end = (value >> 16) + 1;
868 	mode.timing.h_sync_start = (value & 0xffff) + 1;
869 
870 	value = read32(INTEL_DISPLAY_A_VTOTAL);
871 	mode.timing.v_total = (value >> 16) + 1;
872 	mode.timing.v_display = (value & 0xffff) + 1;
873 
874 	value = read32(INTEL_DISPLAY_A_VSYNC);
875 	mode.timing.v_sync_end = (value >> 16) + 1;
876 	mode.timing.v_sync_start = (value & 0xffff) + 1;
877 
878 	// image size and color space
879 
880 	value = read32(INTEL_DISPLAY_A_IMAGE_SIZE);
881 	mode.virtual_width = (value >> 16) + 1;
882 	mode.virtual_height = (value & 0xffff) + 1;
883 
884 	value = read32(INTEL_DISPLAY_A_CONTROL);
885 	switch (value & DISPLAY_CONTROL_COLOR_MASK) {
886 		case DISPLAY_CONTROL_RGB32:
887 		default:
888 			mode.space = B_RGB32;
889 			break;
890 		case DISPLAY_CONTROL_RGB16:
891 			mode.space = B_RGB16;
892 			break;
893 		case DISPLAY_CONTROL_RGB15:
894 			mode.space = B_RGB15;
895 			break;
896 		case DISPLAY_CONTROL_CMAP8:
897 			mode.space = B_CMAP8;
898 			break;
899 	}
900 
901 	mode.h_display_start = 0;
902 	mode.v_display_start = 0;
903 	mode.flags = 0;
904 	return B_OK;
905 }
906 
907 #ifdef __HAIKU__
908 
909 status_t
910 intel_get_edid_info(void* info, size_t size, uint32* _version)
911 {
912 	TRACE(("intel_get_edid_info()\n"));
913 
914 	if (!gInfo->has_edid)
915 		return B_ERROR;
916 	if (size < sizeof(struct edid1_info))
917 		return B_BUFFER_OVERFLOW;
918 
919 	memcpy(info, &gInfo->edid_info, sizeof(struct edid1_info));
920 	*_version = EDID_VERSION_1;
921 	return B_OK;
922 }
923 
924 #endif	// __HAIKU__
925 
926 status_t
927 intel_get_frame_buffer_config(frame_buffer_config *config)
928 {
929 	TRACE(("intel_get_frame_buffer_config()\n"));
930 
931 	uint32 offset = gInfo->shared_info->frame_buffer_offset;
932 
933 	config->frame_buffer = gInfo->shared_info->graphics_memory + offset;
934 	config->frame_buffer_dma
935 		= (uint8 *)gInfo->shared_info->physical_graphics_memory + offset;
936 	config->bytes_per_row = gInfo->shared_info->bytes_per_row;
937 
938 	return B_OK;
939 }
940 
941 
942 status_t
943 intel_get_pixel_clock_limits(display_mode *mode, uint32 *_low, uint32 *_high)
944 {
945 	TRACE(("intel_get_pixel_clock_limits()\n"));
946 
947 	if (_low != NULL) {
948 		// lower limit of about 48Hz vertical refresh
949 		uint32 totalClocks = (uint32)mode->timing.h_total * (uint32)mode->timing.v_total;
950 		uint32 low = (totalClocks * 48L) / 1000L;
951 		if (low < gInfo->shared_info->pll_info.min_frequency)
952 			low = gInfo->shared_info->pll_info.min_frequency;
953 		else if (low > gInfo->shared_info->pll_info.max_frequency)
954 			return B_ERROR;
955 
956 		*_low = low;
957 	}
958 
959 	if (_high != NULL)
960 		*_high = gInfo->shared_info->pll_info.max_frequency;
961 
962 	return B_OK;
963 }
964 
965 
966 status_t
967 intel_move_display(uint16 horizontalStart, uint16 verticalStart)
968 {
969 	TRACE(("intel_move_display()\n"));
970 
971 	intel_shared_info &sharedInfo = *gInfo->shared_info;
972 	Autolock locker(sharedInfo.accelerant_lock);
973 
974 	display_mode &mode = sharedInfo.current_mode;
975 
976 	if (horizontalStart + mode.timing.h_display > mode.virtual_width
977 		|| verticalStart + mode.timing.v_display > mode.virtual_height)
978 		return B_BAD_VALUE;
979 
980 	mode.h_display_start = horizontalStart;
981 	mode.v_display_start = verticalStart;
982 
983 	set_frame_buffer_base();
984 
985 	return B_OK;
986 }
987 
988 
989 status_t
990 intel_get_timing_constraints(display_timing_constraints *constraints)
991 {
992 	TRACE(("intel_get_timing_contraints()\n"));
993 	return B_ERROR;
994 }
995 
996 
997 void
998 intel_set_indexed_colors(uint count, uint8 first, uint8 *colors, uint32 flags)
999 {
1000 	TRACE(("intel_set_indexed_colors(colors = %p, first = %u)\n", colors, first));
1001 
1002 	if (colors == NULL)
1003 		return;
1004 
1005 	Autolock locker(gInfo->shared_info->accelerant_lock);
1006 
1007 	for (; count-- > 0; first++) {
1008 		uint32 color = colors[0] << 16 | colors[1] << 8 | colors[2];
1009 		colors += 3;
1010 
1011 		if (gInfo->head_mode & HEAD_MODE_A_ANALOG)
1012 			write32(INTEL_DISPLAY_A_PALETTE + first * sizeof(uint32), color);
1013 		if (gInfo->head_mode & HEAD_MODE_B_DIGITAL)
1014 			write32(INTEL_DISPLAY_B_PALETTE + first * sizeof(uint32), color);
1015 	}
1016 }
1017 
1018