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