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