xref: /haiku/src/add-ons/accelerants/matrox/ProposeDisplayMode.c (revision a4f6a81235ca2522c01f532de13cad9b729d4029)
1 /*
2 	Copyright 1999, Be Incorporated.   All Rights Reserved.
3 	This file may be used under the terms of the Be Sample Code License.
4 
5 	Other authors for MGA driver:
6 	Mark Watson,
7 	Rudolf Cornelissen 9/2002-1/2006
8 */
9 
10 #define MODULE_BIT 0x00400000
11 
12 #include "acc_std.h"
13 
14 #define	T_POSITIVE_SYNC	(B_POSITIVE_HSYNC | B_POSITIVE_VSYNC)
15 /* mode flags will be setup as status info by PROPOSEMODE! */
16 #define MODE_FLAGS 0
17 #define MODE_COUNT (sizeof (mode_list) / sizeof (display_mode))
18 
19 /*some monitors only handle a fixed set of modes*/
20 #include "valid_mode_list"
21 
22 /*Standard VESA modes*/
23 static const display_mode mode_list[] = {
24 { { 25175, 640, 656, 752, 800, 480, 490, 492, 525, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(640X480X8.Z1) */
25 { { 27500, 640, 672, 768, 864, 480, 488, 494, 530, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* 640X480X60Hz */
26 { { 30500, 640, 672, 768, 864, 480, 517, 523, 588, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* SVGA_640X480X60HzNI */
27 { { 31500, 640, 664, 704, 832, 480, 489, 492, 520, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70-72Hz_(640X480X8.Z1) */
28 { { 31500, 640, 656, 720, 840, 480, 481, 484, 500, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(640X480X8.Z1) */
29 { { 36000, 640, 696, 752, 832, 480, 481, 484, 509, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(640X480X8.Z1) */
30 { { 36000, 800, 824, 896, 1024, 600, 601, 603, 625, 0}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@56Hz_(800X600) from Be, Inc. driver + XFree86 */
31 { { 38100, 800, 832, 960, 1088, 600, 602, 606, 620, 0}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* SVGA_800X600X56HzNI */
32 { { 40000, 800, 840, 968, 1056, 600, 601, 605, 628, T_POSITIVE_SYNC}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(800X600X8.Z1) + XFree86 */
33 { { 49500, 800, 816, 896, 1056, 600, 601, 604, 625, T_POSITIVE_SYNC}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(800X600X8.Z1) + XFree86 */
34 { { 50000, 800, 856, 976, 1040, 600, 637, 643, 666, T_POSITIVE_SYNC}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70-72Hz_(800X600X8.Z1) + XFree86 */
35 { { 56250, 800, 832, 896, 1048, 600, 601, 604, 631, T_POSITIVE_SYNC}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(800X600X8.Z1) + XFree86 */
36 { { 65000, 1024, 1048, 1184, 1344, 768, 771, 777, 806, 0}, B_CMAP8, 1024, 768, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1024X768X8.Z1) + XFree86 */
37 { { 75000, 1024, 1048, 1184, 1328, 768, 771, 777, 806, 0}, B_CMAP8, 1024, 768, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70-72Hz_(1024X768X8.Z1) + XFree86 */
38 { { 78750, 1024, 1040, 1136, 1312, 768, 769, 772, 800, T_POSITIVE_SYNC}, B_CMAP8, 1024, 768, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1024X768X8.Z1) + XFree86 */
39 { { 94500, 1024, 1072, 1168, 1376, 768, 769, 772, 808, T_POSITIVE_SYNC}, B_CMAP8, 1024, 768, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(1024X768X8.Z1) + XFree86 */
40 { { 94200, 1152, 1184, 1280, 1472, 864, 865, 868, 914, T_POSITIVE_SYNC}, B_CMAP8, 1152, 864, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70Hz_(1152X864X8.Z1) */
41 { { 97800, 1152, 1216, 1344, 1552, 864, 865, 868, 900, T_POSITIVE_SYNC}, B_CMAP8, 1152, 864, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70Hz_(1152X864X8.Z1) */
42 { { 108000, 1152, 1216, 1344, 1600, 864, 865, 868, 900, T_POSITIVE_SYNC}, B_CMAP8, 1152, 864, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1152X864X8.Z1) + XFree86 */
43 { { 121500, 1152, 1216, 1344, 1568, 864, 865, 868, 911, T_POSITIVE_SYNC}, B_CMAP8, 1152, 864, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(1152X864X8.Z1) */
44 { { 108000, 1280, 1328, 1440, 1688, 1024, 1025, 1028, 1066, T_POSITIVE_SYNC}, B_CMAP8, 1280, 1024, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1280X1024) from Be, Inc. driver + XFree86 */
45 { { 135000, 1280, 1296, 1440, 1688, 1024, 1025, 1028, 1066, T_POSITIVE_SYNC}, B_CMAP8, 1280, 1024, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1280X1024X8.Z1) + XFree86 */
46 { { 157500, 1280, 1344, 1504, 1728, 1024, 1025, 1028, 1072, T_POSITIVE_SYNC}, B_CMAP8, 1280, 1024, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(1280X1024X8.Z1) + XFree86 */
47 { { 122600, 1400, 1488, 1640, 1880, 1050, 1051, 1054, 1087, T_POSITIVE_SYNC}, B_CMAP8, 1400, 1050, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1400X1050) */
48 { { 162000, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, T_POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1600X1200X8.Z1) + XFree86 */
49 /* identical lines to above one, apart from refreshrate.. */
50 { { 175500, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, T_POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@65Hz_(1600X1200X8.Z1) + XFree86 */
51 { { 189000, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, T_POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70Hz_(1600X1200X8.Z1) + XFree86 */
52 { { 202500, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, T_POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1600X1200X8.Z1) + XFree86 */
53 { { 216000, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, T_POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@80Hz_(1600X1200X8.Z1) */
54 { { 229500, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, T_POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS},  /* Vesa_Monitor_@85Hz_(1600X1200X8.Z1) + XFree86 */
55 /* end identical lines. */
56 { { 204750, 1792, 1920, 2120, 2448, 1344, 1345, 1348, 1394, B_POSITIVE_VSYNC}, B_CMAP8, 1792, 1344, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1792X1344) from Be, Inc. driver + XFree86 */
57 { { 261000, 1792, 1888, 2104, 2456, 1344, 1345, 1348, 1417, B_POSITIVE_VSYNC}, B_CMAP8, 1792, 1344, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1792X1344) from Be, Inc. driver + XFree86 */
58 { { 218250, 1856, 1952, 2176, 2528, 1392, 1393, 1396, 1439, B_POSITIVE_VSYNC}, B_CMAP8, 1856, 1392, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1856X1392) from Be, Inc. driver + XFree86 */
59 { { 288000, 1856, 1984, 2208, 2560, 1392, 1393, 1396, 1500, B_POSITIVE_VSYNC}, B_CMAP8, 1856, 1392, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1856X1392) from Be, Inc. driver + XFree86 */
60 { { 234000, 1920, 2048, 2256, 2600, 1440, 1441, 1444, 1500, B_POSITIVE_VSYNC}, B_CMAP8, 1920, 1440, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1920X1440) from Be, Inc. driver + XFree86 */
61 { { 297000, 1920, 2064, 2288, 2640, 1440, 1441, 1444, 1500, B_POSITIVE_VSYNC}, B_CMAP8, 1920, 1440, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1920X1440) from Be, Inc. driver + XFree86 */
62 { { 266950, 2048, 2200, 2424, 2800, 1536, 1537, 1540, 1589, B_POSITIVE_VSYNC}, B_CMAP8, 2048, 1536, 0, 0, MODE_FLAGS}, /* From XFree86 posting @60Hz + XFree86 */
63 };
64 
65 /*
66 Check mode is between low and high limits
67 returns:
68 	B_OK - found one
69 	B_BAD_VALUE - mode can be made, but outside limits
70 	B_ERROR - not possible
71 */
72 /* BOUNDS WARNING:
73  * BeOS (tested R5.0.3PE) is failing BWindowScreen.SetFrameBuffer() if PROPOSEMODE
74  * returns B_BAD_VALUE. It's called by the OS with target, low and high set to
75  * have the same settings for BWindowScreen!
76  * Which means we should not return B_BAD_VALUE on anything except for deviations on:
77  * display_mode.virtual_width;
78  * display_mode.virtual_height;
79  * display_mode.timing.h_display;
80  * display_mode.timing.v_display;
81  */
82 /* Note:
83  * The target mode should be modified to correspond to the mode as it can be made. */
84 status_t PROPOSE_DISPLAY_MODE(display_mode *target, const display_mode *low, const display_mode *high)
85 {
86 	status_t status = B_OK;
87 	float pix_clock_found;
88 	uint8 m,n,p, bpp;
89 	status_t result;
90 	uint32 max_vclk, row_bytes, pointer_reservation;
91 	bool acc_mode;
92 	double target_refresh = ((double)target->timing.pixel_clock * 1000.0) /
93 			(
94 				(double)target->timing.h_total *
95 				(double)target->timing.v_total
96 			);
97 	bool
98 		want_same_width = target->timing.h_display == target->virtual_width,
99 		want_same_height = target->timing.v_display == target->virtual_height;
100 
101 	LOG(1, ("PROPOSEMODE: (ENTER) requested virtual_width %d, virtual_height %d\n",
102 									target->virtual_width, target->virtual_height));
103 
104 	/*check valid list:
105 		if (VALID_REQUIRED is set)
106 		{
107 			if (find modes with same size)
108 			{
109 				pick one with nearest pixel clock
110 			}
111 			else
112 			{
113 				pick next largest with nearest pixel clock and modify visible portion as far as possible
114 			}
115 		}
116 	*/
117 	#ifdef VALID_MODE_REQUIRED
118 	{
119 		int i;
120 		int closest_mode_ptr;
121 		uint32 closest_mode_clock;
122 
123 		LOG(1, ("PROPOSEMODE: valid mode required!\n"));
124 
125 		closest_mode_ptr = 0xbad;
126 		closest_mode_clock = 0;
127 		for (i=0;i<VALID_MODES;i++)
128 		{
129 			/*check size is ok and clock is better than any found before*/
130 			if(
131 				target->timing.h_display==valid_mode_list[i].h_display &&
132 				target->timing.v_display==valid_mode_list[i].v_display
133 			)
134 			{
135 				if (
136 					abs(valid_mode_list[i].pixel_clock-target->timing.pixel_clock)<
137 					abs(closest_mode_clock-target->timing.pixel_clock)
138 				)
139 				{
140 					closest_mode_clock=valid_mode_list[i].pixel_clock;
141 					closest_mode_ptr=i;
142 				}
143 			}
144 		}
145 
146 		if (closest_mode_ptr==0xbad)/*if no modes of correct size*/
147 		{
148 			LOG(4, ("PROPOSEMODE: no valid mode found, aborted.\n"));
149 			return B_ERROR;
150 		}
151 		else
152 		{
153 			target->timing=valid_mode_list[closest_mode_ptr];
154 			target_refresh = ((double)target->timing.pixel_clock * 1000.0) /  /*I require this refresh*/
155 			((double)target->timing.h_total * (double)target->timing.v_total);
156 		}
157 	}
158 	#endif
159 
160 	/* check colorspace versus requested dualhead mode */
161 	switch (target->space)
162 	{
163 	case B_CMAP8:
164 	case B_RGB15:
165 		/* mode will be made 'outside limits' when dualhead was requested */
166 		if (target->flags & DUALHEAD_BITS)
167 		{
168 			status = B_BAD_VALUE;
169 			LOG(4, ("PROPOSEMODE: WARNING: Colorspace can't be done in dualhead mode\n"));
170 		}
171 		/* clear dualhead bits if colorspace isn't supported to prevent ProposeMode
172 		 * from aborting */
173 		target->flags &= ~(DUALHEAD_BITS | DUALHEAD_CAPABLE);
174 		break;
175 	}
176 
177 	/*find a nearby valid timing from that given*/
178 	result = gx00_crtc_validate_timing
179 	(
180 		&target->timing.h_display, &target->timing.h_sync_start, &target->timing.h_sync_end, &target->timing.h_total,
181 		&target->timing.v_display, &target->timing.v_sync_start, &target->timing.v_sync_end, &target->timing.v_total
182 	);
183 	if (result == B_ERROR)
184 	{
185 		LOG(4, ("PROPOSEMODE: could not validate timing, aborted.\n"));
186 		return result;
187 	}
188 
189 	/* validate display vs. virtual */
190 	if ((target->timing.h_display > target->virtual_width) || want_same_width)
191 		target->virtual_width = target->timing.h_display;
192 	if ((target->timing.v_display > target->virtual_height) || want_same_height)
193 		target->virtual_height = target->timing.v_display;
194 
195 	/* nail virtual size and 'subsequently' calculate rowbytes */
196 	result = gx00_general_validate_pic_size (target, &row_bytes, &acc_mode);
197 	if (result == B_ERROR)
198 	{
199 		LOG(4, ("PROPOSEMODE: could not validate virtual picture size, aborted.\n"));
200 		return result;
201 	}
202 
203 	/*check if virtual_width is still within the requested limits*/
204 	if ((target->virtual_width < low->virtual_width) ||
205 		(target->virtual_width > high->virtual_width))
206 	{
207 		status = B_BAD_VALUE;
208 		LOG(4, ("PROPOSEMODE: WARNING: virtual_width deviates too much\n"));
209 	}
210 
211 	/*check if timing found is within the requested horizontal limits*/
212 	if ((target->timing.h_display < low->timing.h_display) ||
213 		(target->timing.h_display > high->timing.h_display) ||
214 		(target->timing.h_sync_start < low->timing.h_sync_start) ||
215 		(target->timing.h_sync_start > high->timing.h_sync_start) ||
216 		(target->timing.h_sync_end < low->timing.h_sync_end) ||
217 		(target->timing.h_sync_end > high->timing.h_sync_end) ||
218 		(target->timing.h_total < low->timing.h_total) ||
219 		(target->timing.h_total > high->timing.h_total))
220 	{
221 		/* BWindowScreen workaround: we accept everything except h_display deviations */
222 		if ((target->timing.h_display < low->timing.h_display) ||
223 			(target->timing.h_display > high->timing.h_display))
224 		{
225 			status = B_BAD_VALUE;
226 		}
227 		LOG(4, ("PROPOSEMODE: WARNING: horizontal timing deviates too much\n"));
228 	}
229 
230 	/*check if timing found is within the requested vertical limits*/
231 	if (
232 		(target->timing.v_display < low->timing.v_display) ||
233 		(target->timing.v_display > high->timing.v_display) ||
234 		(target->timing.v_sync_start < low->timing.v_sync_start) ||
235 		(target->timing.v_sync_start > high->timing.v_sync_start) ||
236 		(target->timing.v_sync_end < low->timing.v_sync_end) ||
237 		(target->timing.v_sync_end > high->timing.v_sync_end) ||
238 		(target->timing.v_total < low->timing.v_total) ||
239 		(target->timing.v_total > high->timing.v_total)
240 	)
241 	{
242 		/* BWindowScreen workaround: we accept everything except v_display deviations */
243 		if ((target->timing.v_display < low->timing.v_display) ||
244 			(target->timing.v_display > high->timing.v_display))
245 		{
246 			status = B_BAD_VALUE;
247 		}
248 		LOG(4, ("PROPOSEMODE: WARNING: vertical timing deviates too much\n"));
249 	}
250 
251 	/* adjust pixelclock for possible timing modifications done above */
252 	target->timing.pixel_clock = target_refresh * ((double)target->timing.h_total) * ((double)target->timing.v_total) / 1000.0;
253 
254 	/* Now find the nearest valid pixelclock we actually can setup for the target mode,
255 	 * this also makes sure we don't generate more pixel bandwidth than the device can handle */
256 	/* calculate settings, but do not actually test anything (that costs too much time!) */
257 	result = gx00_dac_pix_pll_find(*target,&pix_clock_found,&m,&n,&p,0);
258 	/* update the target mode */
259 	target->timing.pixel_clock = (pix_clock_found * 1000);
260 
261 	/* note if we fell outside the limits */
262 	if ((target->timing.pixel_clock < low->timing.pixel_clock) ||
263 		(target->timing.pixel_clock > high->timing.pixel_clock)
264 	)
265 	{
266 		/* BWindowScreen workaround: we accept deviations <= 1Mhz */
267 		if ((target->timing.pixel_clock < (low->timing.pixel_clock - 1000)) ||
268 			(target->timing.pixel_clock > (high->timing.pixel_clock + 1000)))
269 		{
270 			status = B_BAD_VALUE;
271 		}
272 		LOG(4, ("PROPOSEMODE: WARNING: pixelclock deviates too much\n"));
273 	}
274 
275 	/* checkout space needed for hardcursor (if any) */
276 	pointer_reservation = 0;
277 	/* MIL 1/2 cards have a seperate buffer for the cursorbitmap inside the DAC */
278 	if ((si->ps.card_type >= G100) && si->settings.hardcursor) pointer_reservation = 1024;
279 	/* memory requirement for frame buffer */
280 	if ((row_bytes * target->virtual_height) >
281 		((si->ps.memory_size * 1024 * 1024) - pointer_reservation))
282 	{
283 		target->virtual_height =
284 			((si->ps.memory_size * 1024 * 1024) - pointer_reservation) / row_bytes;
285 	}
286 	if (target->virtual_height < target->timing.v_display)
287 	{
288 		LOG(4,("PROPOSEMODE: not enough memory for current mode, aborted.\n"));
289 		return B_ERROR;
290 	}
291 	LOG(4,("PROPOSEMODE: validated virtual_width %d, virtual_height %d pixels\n",
292 		target->virtual_width, target->virtual_height));
293 
294 	if ((target->virtual_height < low->virtual_height) ||
295 		(target->virtual_height > high->virtual_height))
296 	{
297 		status = B_BAD_VALUE;
298 		LOG(4, ("PROPOSEMODE: WARNING: virtual_height deviates too much\n"));
299 	}
300 
301 	/* setup status flags */
302 	LOG(1, ("PROPOSEMODE: initial modeflags: $%08x\n", target->flags));
303 	/* preset to singlehead card without TVout, no overlay support and no hardcursor.
304 	 * also advice system that app_server and acc engine may touch the framebuffer
305 	 * simultaneously (fixed). */
306 	target->flags &=
307 		~(DUALHEAD_CAPABLE | TV_CAPABLE | B_SUPPORTS_OVERLAYS | B_HARDWARE_CURSOR | B_IO_FB_NA);
308 	/* we always allow parallel access (fixed), the DAC is always in 'enhanced'
309 	 * mode (fixed), and all modes support DPMS (fixed);
310 	 * We support scrolling and panning in every mode, so we 'send a signal' to
311 	 * BWindowScreen.CanControlFrameBuffer() by setting B_SCROLL.  */
312 	/* BTW: B_PARALLEL_ACCESS in combination with a hardcursor enables
313 	 * BDirectWindow windowed modes. */
314 	target->flags |= (B_PARALLEL_ACCESS | B_8_BIT_DAC | B_DPMS | B_SCROLL);
315 
316 	/* determine the 'would be' max. pixelclock for the second DAC for the current videomode if dualhead were activated */
317 	switch (target->space)
318 	{
319 		case B_CMAP8:
320 			max_vclk = si->ps.max_dac2_clock_8;
321 			bpp = 1;
322 			break;
323 		case B_RGB15_LITTLE:
324 		case B_RGB16_LITTLE:
325 			max_vclk = si->ps.max_dac2_clock_16;
326 			bpp = 2;
327 			break;
328 		case B_RGB24_LITTLE:
329 			max_vclk = si->ps.max_dac2_clock_24;
330 			bpp = 3;
331 			break;
332 		case B_RGB32_LITTLE:
333 			max_vclk = si->ps.max_dac2_clock_32dh;
334 			bpp = 4;
335 			break;
336 		default:
337 			/* use fail-safe value */
338 			max_vclk = si->ps.max_dac2_clock_32dh;
339 			bpp = 4;
340 			break;
341 	}
342 
343 	/* set DUALHEAD_CAPABLE if suitable */
344 	//fixme: update for independant secondary head use! (reserve fixed memory then)
345 	if (si->ps.secondary_head &&
346 	 	((target->space == B_RGB16_LITTLE) || (target->space == B_RGB32_LITTLE)) &&
347 	 	(target->timing.pixel_clock <= (max_vclk * 1000)))
348 	{
349 		/* extra line for G400 MAVEN vblank design fault workaround needed! */
350 		uint16 vblank_fix = 0;
351 		if (si->ps.card_type <= G400MAX) vblank_fix = 1;
352 
353 		switch (target->flags & DUALHEAD_BITS)
354 		{
355 		case DUALHEAD_ON:
356 		case DUALHEAD_SWITCH:
357 			if ((((si->ps.memory_size * 1024 * 1024) - pointer_reservation) >=
358 					(row_bytes * (target->virtual_height + vblank_fix))) &&
359 			 	((uint16)(row_bytes / bpp) >= (target->timing.h_display * 2)))
360 			{
361 				target->flags |= DUALHEAD_CAPABLE;
362 			}
363 			break;
364 		case DUALHEAD_CLONE:
365 			if (((si->ps.memory_size * 1024 * 1024) - pointer_reservation) >=
366 					(row_bytes * (target->virtual_height + vblank_fix)))
367 			{
368 				target->flags |= DUALHEAD_CAPABLE;
369 			}
370 			break;
371 		case DUALHEAD_OFF:
372 			if (((si->ps.memory_size * 1024 * 1024) - pointer_reservation) >=
373 					(row_bytes * (target->virtual_height + vblank_fix) * 2))
374 			{
375 				target->flags |= DUALHEAD_CAPABLE;
376 			}
377 			break;
378 		}
379 	}
380 
381 	/* if not dualhead capable card clear dualhead flags */
382 	if (!(target->flags & DUALHEAD_CAPABLE))
383 	{
384 		target->flags &= ~DUALHEAD_BITS;
385 	}
386 
387 	/* set TV_CAPABLE if suitable: pixelclock is not important (defined by TVstandard) */
388 	switch (si->ps.card_type)
389 	{
390 	case G400:
391 	case G400MAX:
392 		//fixme: in theory singlehead cards can do TVout as well (if MAVEN mounted)
393 		if (target->flags & DUALHEAD_CAPABLE)
394 		{
395 			if (si->ps.secondary_tvout &&
396 				(target->timing.h_display <= 1024) &&
397 				(target->timing.v_display <= 768))
398 			{
399 				target->flags |= TV_CAPABLE;
400 			}
401 		}
402 		break;
403 	case G450:
404 	case G550:
405 		/* G450/G550 can only do secondary TVout in Video modes (no Desktop modes!) */
406 		if ((target->flags & DUALHEAD_CAPABLE) && (target->flags & TV_VIDEO))
407 		{
408 			if (si->ps.secondary_tvout &&
409 				(target->timing.h_display <= 1024) &&	/* Hscaling option exists */
410 				(target->timing.v_display <= 576))		/* Vscaling option lacks */
411 			{
412 				target->flags |= TV_CAPABLE;
413 			}
414 		}
415 		break;
416 	default:
417 		//fixme: setup for G100 and G200 TVout later on...
418 		break;
419 	}
420 
421 	/* TVout can only be done if the interface cable is connected */
422 	if ((i2c_sec_tv_adapter() != B_OK) && (target->flags & TV_CAPABLE))
423 	{
424 		target->flags &= ~TV_CAPABLE;
425 		LOG(1, ("PROPOSEMODE: blocking TVout: no TVout cable connected!\n"));
426 	}
427 
428 	/* if not TVout capable card and mode clear TVout flags */
429 	if (!(target->flags & TV_CAPABLE))
430 	{
431 		target->flags &= ~TV_BITS;
432 	}
433 
434 	/* fixme: currently the matrox driver can only do secondary TVout */
435 	target->flags &= ~TV_PRIMARY;
436 
437 	/* set HARDWARE_CURSOR mode if suitable */
438 	if (si->settings.hardcursor)
439 		target->flags |= B_HARDWARE_CURSOR;
440 
441 	/* set SUPPORTS_OVERLAYS if suitable */
442 	if (si->ps.card_type >= G200)
443 		target->flags |= B_SUPPORTS_OVERLAYS;
444 
445 	LOG(1, ("PROPOSEMODE: validated modeflags: $%08x\n", target->flags));
446 
447 	/* overrule timing command flags to be (fixed) blank_pedestal = 0.0IRE,
448 	 * progressive scan (fixed), and setup sync_on_green flag according to
449 	 * mga.settings options file */
450 	target->timing.flags &= ~(B_BLANK_PEDESTAL | B_TIMING_INTERLACED | B_SYNC_ON_GREEN);
451 	if (si->settings.greensync)
452 		target->timing.flags |= B_SYNC_ON_GREEN;
453 	/* The HSYNC and VSYNC command flags are actually executed by the driver. */
454 
455 	if (status == B_OK)	LOG(4, ("PROPOSEMODE: completed successfully.\n"));
456 	else LOG(4, ("PROPOSEMODE: mode can be made, but outside given limits.\n"));
457 	return status;
458 }
459 
460 /* Return the number of modes this device will return from GET_MODE_LIST().
461 	This is precalculated in create_mode_list (called from InitAccelerant stuff)
462 */
463 uint32 ACCELERANT_MODE_COUNT(void)
464 {
465 	LOG(1, ("ACCELERANT_MODE_COUNT: the modelist contains %d modes\n",si->mode_count));
466 
467 	return si->mode_count;
468 }
469 
470 /* Copy the list of guaranteed supported video modes to the location provided.*/
471 status_t GET_MODE_LIST(display_mode *dm)
472 {
473 	LOG(1, ("GET_MODE_LIST: exporting the modelist created before.\n"));
474 
475 	memcpy(dm, my_mode_list, si->mode_count * sizeof(display_mode));
476 	return B_OK;
477 }
478 
479 /* Create a list of display_modes to pass back to the caller.*/
480 status_t create_mode_list(void) {
481 	size_t max_size;
482 	uint32
483 		i, j,
484 		pix_clk_range;
485 	const display_mode
486 		*src;
487 	display_mode
488 		*dst,
489 		low,
490 		high;
491 
492 	color_space spaces[4] = {B_RGB32_LITTLE,B_RGB16_LITTLE,B_RGB15_LITTLE,B_CMAP8};
493 
494 	/* figure out how big the list could be, and adjust up to nearest multiple of B_PAGE_SIZE*/
495 	max_size = (((MODE_COUNT * 4) * sizeof(display_mode)) + (B_PAGE_SIZE-1)) & ~(B_PAGE_SIZE-1);
496 	/* create an area to hold the info */
497 	si->mode_area = my_mode_list_area =
498 		create_area("MGA accelerant mode info", (void **)&my_mode_list, B_ANY_ADDRESS, max_size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
499 	if (my_mode_list_area < B_OK) return my_mode_list_area;
500 
501 	/* walk through our predefined list and see which modes fit this device */
502 	src = mode_list;
503 	dst = my_mode_list;
504 	si->mode_count = 0;
505 	for (i = 0; i < MODE_COUNT; i++) {
506 		/* set ranges for acceptable values */
507 		low = high = *src;
508 		/* range is 6.25% of default clock: arbitrarily picked */
509 		pix_clk_range = low.timing.pixel_clock >> 5;
510 		low.timing.pixel_clock -= pix_clk_range;
511 		high.timing.pixel_clock += pix_clk_range;
512 		/* 'some cards need wider virtual widths for certain modes':
513 		 * Not true. They might need a wider pitch, but this is _not_ reflected in
514 		 * virtual_width, but in fbc.bytes_per_row. */
515 		//So disable next line:
516 		//high.virtual_width = 4096;
517 		/* do it once for each depth we want to support */
518 		for (j = 0; j < (sizeof(spaces) / sizeof(color_space)); j++)
519 		{
520 			/* set target values */
521 			*dst = *src;
522 			/* poke the specific space */
523 			dst->space = low.space = high.space = spaces[j];
524 			/* ask for a compatible mode */
525 			/* We have to check for B_OK, because otherwise the pix_clk_range
526 			 * won't be taken into account!! */
527 			//So don't do this:
528 			//if (PROPOSE_DISPLAY_MODE(dst, &low, &high) != B_ERROR) {
529 			//Instead, do this:
530 			if (PROPOSE_DISPLAY_MODE(dst, &low, &high) == B_OK) {
531 				/* count it, and move on to next mode */
532 				dst++;
533 				si->mode_count++;
534 			}
535 		}
536 		/* advance to next mode */
537 		src++;
538 	}
539 
540 	return B_OK;
541 }
542