xref: /haiku/src/add-ons/accelerants/nvidia/ProposeDisplayMode.c (revision d29f332ad4210e2e2b5ef42231531dd937ebc52e)
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 NV driver:
6 	Mark Watson,
7 	Rudolf Cornelissen 9/2002-11/2005
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  * plus panel specific resolution modes which are internally modified during run-time depending on the requirements of the actual
24  * panel connected. The modes as listed here, should timing-wise be as compatible with analog (CRT) monitors as can be... */
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 { { 193200, 1920, 2048, 2256, 2592, 1200, 1201, 1204, 1242, T_POSITIVE_SYNC}, B_CMAP8, 1920, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1920X1200) */
90 };
91 
92 /*
93 Check mode is between low and high limits
94 returns:
95 	B_OK - found one
96 	B_BAD_VALUE - mode can be made, but outside limits
97 	B_ERROR - not possible
98 */
99 /* BOUNDS WARNING:
100  * BeOS (tested R5.0.3PE) is failing BWindowScreen.SetFrameBuffer() if PROPOSEMODE
101  * returns B_BAD_VALUE. It's called by the OS with target, low and high set to
102  * have the same settings for BWindowScreen!
103  * Which means we should not return B_BAD_VALUE on anything except for deviations on:
104  * display_mode.virtual_width;
105  * display_mode.virtual_height;
106  * display_mode.timing.h_display;
107  * display_mode.timing.v_display;
108  */
109 /* Note:
110  * The target mode should be modified to correspond to the mode as it can be made. */
111 status_t PROPOSE_DISPLAY_MODE(display_mode *target, const display_mode *low, const display_mode *high)
112 {
113 	status_t status = B_OK;
114 	float pix_clock_found, target_aspect;
115 	uint8 m,n,p, bpp;
116 	status_t result;
117 	uint32 max_vclk, row_bytes, mem_reservation;
118 	bool acc_mode;
119 	double target_refresh = ((double)target->timing.pixel_clock * 1000.0) /
120 			(
121 				(double)target->timing.h_total *
122 				(double)target->timing.v_total
123 			);
124 	bool
125 		want_same_width = target->timing.h_display == target->virtual_width,
126 		want_same_height = target->timing.v_display == target->virtual_height;
127 
128 	LOG(1, ("PROPOSEMODE: (ENTER) requested virtual_width %d, virtual_height %d\n",
129 									target->virtual_width, target->virtual_height));
130 
131 	/*check valid list:
132 		if (VALID_REQUIRED is set)
133 		{
134 			if (find modes with same size)
135 			{
136 				pick one with nearest pixel clock
137 			}
138 			else
139 			{
140 				pick next largest with nearest pixel clock and modify visible portion as far as possible
141 			}
142 		}
143 	*/
144 	#ifdef VALID_MODE_REQUIRED
145 	{
146 		int i;
147 		int closest_mode_ptr;
148 		uint32 closest_mode_clock;
149 
150 		LOG(1, ("PROPOSEMODE: valid mode required!\n"));
151 
152 		closest_mode_ptr = 0xbad;
153 		closest_mode_clock = 0;
154 		for (i=0;i<VALID_MODES;i++)
155 		{
156 			/*check size is ok and clock is better than any found before*/
157 			if(
158 				target->timing.h_display==valid_mode_list[i].h_display &&
159 				target->timing.v_display==valid_mode_list[i].v_display
160 			)
161 			{
162 				if (
163 					abs(valid_mode_list[i].pixel_clock-target->timing.pixel_clock)<
164 					abs(closest_mode_clock-target->timing.pixel_clock)
165 				)
166 				{
167 					closest_mode_clock=valid_mode_list[i].pixel_clock;
168 					closest_mode_ptr=i;
169 				}
170 			}
171 		}
172 
173 		if (closest_mode_ptr==0xbad)/*if no modes of correct size*/
174 		{
175 			LOG(4, ("PROPOSEMODE: no valid mode found, aborted.\n"));
176 			return B_ERROR;
177 		}
178 		else
179 		{
180 			target->timing=valid_mode_list[closest_mode_ptr];
181 			target_refresh = ((double)target->timing.pixel_clock * 1000.0) /  /*I require this refresh*/
182 			((double)target->timing.h_total * (double)target->timing.v_total);
183 		}
184 	}
185 	#endif
186 
187 	/*find a nearby valid timing from that given*/
188 	result = head1_validate_timing
189 	(
190 		&target->timing.h_display, &target->timing.h_sync_start, &target->timing.h_sync_end, &target->timing.h_total,
191 		&target->timing.v_display, &target->timing.v_sync_start, &target->timing.v_sync_end, &target->timing.v_total
192 	);
193 	if (result == B_ERROR)
194 	{
195 		LOG(4, ("PROPOSEMODE: could not validate timing, aborted.\n"));
196 		return result;
197 	}
198 
199 	/* disable aspect checks for a requested TVout mode when mode is TVout capable */
200 	if (!(si->ps.tvout) ||
201 		!(BT_check_tvmode(*target) && (target->flags & TV_BITS)))
202 	{
203 		/* check if all connected output devices can display the requested mode's aspect: */
204 		/* calculate display mode aspect */
205 		target_aspect = (target->timing.h_display / ((float)target->timing.v_display));
206 		/* NOTE:
207 		 * allow 0.10 difference so 5:4 aspect panels will be able to use 4:3 aspect modes! */
208 		switch (si->ps.monitors)
209 		{
210 		case 0x01: /* digital panel on head 1, nothing on head 2 */
211 			if (si->ps.panel1_aspect < (target_aspect - 0.10))
212 			{
213 				LOG(4, ("PROPOSEMODE: connected panel1 is not widescreen type, aborted.\n"));
214 				return B_ERROR;
215 			}
216 			break;
217 		case 0x10: /* nothing on head 1, digital panel on head 2 */
218 			if (si->ps.panel2_aspect < (target_aspect - 0.10))
219 			{
220 				LOG(4, ("PROPOSEMODE: connected panel2 is not widescreen type, aborted.\n"));
221 				return B_ERROR;
222 			}
223 			break;
224 		case 0x11: /* digital panels on both heads */
225 			if ((si->ps.panel1_aspect < (target_aspect - 0.10)) ||
226 				(si->ps.panel2_aspect < (target_aspect - 0.10)))
227 			{
228 				LOG(4, ("PROPOSEMODE: not all connected panels are widescreen type, aborted.\n"));
229 				return B_ERROR;
230 			}
231 			break;
232 		default: /* at least one analog monitor is connected, or nothing detected at all */
233 			if (target_aspect > 1.34)
234 			{
235 				LOG(4, ("PROPOSEMODE: not all output devices can display widescreen modes, aborted.\n"));
236 				return B_ERROR;
237 			}
238 			break;
239 		}
240 
241 		/* only export widescreen panel-TV modes when an exact resolution match exists,
242 		 * to prevent the modelist from becoming too crowded */
243 		if (target_aspect > 1.61)
244 		{
245 			status_t panel_TV_stat = B_ERROR;
246 
247 			if (si->ps.tmds1_active)
248 			{
249 				if ((target->timing.h_display == si->ps.p1_timing.h_display) &&
250 					(target->timing.v_display == si->ps.p1_timing.v_display))
251 				{
252 					panel_TV_stat = B_OK;
253 				}
254 			}
255 			if (si->ps.tmds2_active)
256 			{
257 				if ((target->timing.h_display == si->ps.p2_timing.h_display) &&
258 					(target->timing.v_display == si->ps.p2_timing.v_display))
259 				{
260 					panel_TV_stat = B_OK;
261 				}
262 			}
263 			if (panel_TV_stat != B_OK)
264 			{
265 				LOG(4, ("PROPOSEMODE: WS panel_TV mode requested but no such TV here, aborted.\n"));
266 				return B_ERROR;
267 			}
268 		}
269 	}
270 
271 	/* check if panel(s) can display the requested resolution (if connected) */
272 	if (si->ps.tmds1_active)
273 	{
274 		if ((target->timing.h_display > si->ps.p1_timing.h_display) ||
275 			(target->timing.v_display > si->ps.p1_timing.v_display))
276 		{
277 			LOG(4, ("PROPOSEMODE: panel1 can't display requested resolution, aborted.\n"));
278 			return B_ERROR;
279 		}
280 	}
281 	if (si->ps.tmds2_active)
282 	{
283 		if ((target->timing.h_display > si->ps.p2_timing.h_display) ||
284 			(target->timing.v_display > si->ps.p2_timing.v_display))
285 		{
286 			LOG(4, ("PROPOSEMODE: panel2 can't display requested resolution, aborted.\n"));
287 			return B_ERROR;
288 		}
289 	}
290 
291 	/* validate display vs. virtual */
292 	if ((target->timing.h_display > target->virtual_width) || want_same_width)
293 		target->virtual_width = target->timing.h_display;
294 	if ((target->timing.v_display > target->virtual_height) || want_same_height)
295 		target->virtual_height = target->timing.v_display;
296 
297 	/* nail virtual size and 'subsequently' calculate rowbytes */
298 	result = nv_general_validate_pic_size (target, &row_bytes, &acc_mode);
299 	if (result == B_ERROR)
300 	{
301 		LOG(4, ("PROPOSEMODE: could not validate virtual picture size, aborted.\n"));
302 		return result;
303 	}
304 
305 	/*check if virtual_width is still within the requested limits*/
306 	if ((target->virtual_width < low->virtual_width) ||
307 		(target->virtual_width > high->virtual_width))
308 	{
309 		status = B_BAD_VALUE;
310 		LOG(4, ("PROPOSEMODE: WARNING: virtual_width deviates too much\n"));
311 	}
312 
313 	/*check if timing found is within the requested horizontal limits*/
314 	if ((target->timing.h_display < low->timing.h_display) ||
315 		(target->timing.h_display > high->timing.h_display) ||
316 		(target->timing.h_sync_start < low->timing.h_sync_start) ||
317 		(target->timing.h_sync_start > high->timing.h_sync_start) ||
318 		(target->timing.h_sync_end < low->timing.h_sync_end) ||
319 		(target->timing.h_sync_end > high->timing.h_sync_end) ||
320 		(target->timing.h_total < low->timing.h_total) ||
321 		(target->timing.h_total > high->timing.h_total))
322 	{
323 		/* BWindowScreen workaround: we accept everything except h_display deviations */
324 		if ((target->timing.h_display < low->timing.h_display) ||
325 			(target->timing.h_display > high->timing.h_display))
326 		{
327 			status = B_BAD_VALUE;
328 		}
329 		else
330 		{
331 			status = B_OK;
332 		}
333 		LOG(4, ("PROPOSEMODE: WARNING: horizontal timing deviates too much\n"));
334 	}
335 
336 	/*check if timing found is within the requested vertical limits*/
337 	if (
338 		(target->timing.v_display < low->timing.v_display) ||
339 		(target->timing.v_display > high->timing.v_display) ||
340 		(target->timing.v_sync_start < low->timing.v_sync_start) ||
341 		(target->timing.v_sync_start > high->timing.v_sync_start) ||
342 		(target->timing.v_sync_end < low->timing.v_sync_end) ||
343 		(target->timing.v_sync_end > high->timing.v_sync_end) ||
344 		(target->timing.v_total < low->timing.v_total) ||
345 		(target->timing.v_total > high->timing.v_total)
346 	)
347 	{
348 		/* BWindowScreen workaround: we accept everything except v_display deviations */
349 		if ((target->timing.v_display < low->timing.v_display) ||
350 			(target->timing.v_display > high->timing.v_display))
351 		{
352 			status = B_BAD_VALUE;
353 		}
354 		else
355 		{
356 			status = B_OK;
357 		}
358 		LOG(4, ("PROPOSEMODE: WARNING: vertical timing deviates too much\n"));
359 	}
360 
361 	/* adjust pixelclock for possible timing modifications done above */
362 	target->timing.pixel_clock = target_refresh * ((double)target->timing.h_total) * ((double)target->timing.v_total) / 1000.0;
363 
364 	/* Now find the nearest valid pixelclock we actually can setup for the target mode,
365 	 * this also makes sure we don't generate more pixel bandwidth than the device can handle */
366 	/* calculate settings, but do not actually test anything (that costs too much time!) */
367 	result = head1_pix_pll_find(*target,&pix_clock_found,&m,&n,&p,0);
368 	/* update the target mode */
369 	target->timing.pixel_clock = (pix_clock_found * 1000);
370 
371 	/* note if we fell outside the limits */
372 	if ((target->timing.pixel_clock < low->timing.pixel_clock) ||
373 		(target->timing.pixel_clock > high->timing.pixel_clock)
374 	)
375 	{
376 		/* BWindowScreen workaround: we accept deviations <= 1Mhz */
377 		if ((target->timing.pixel_clock < (low->timing.pixel_clock - 1000)) ||
378 			(target->timing.pixel_clock > (high->timing.pixel_clock + 1000)))
379 		{
380 			status = B_BAD_VALUE;
381 		}
382 		else
383 		{
384 			status = B_OK;
385 		}
386 		LOG(4, ("PROPOSEMODE: WARNING: pixelclock deviates too much\n"));
387 	}
388 
389 	mem_reservation = 0;
390 	/* checkout space needed for hardcursor (if any) */
391 	if (si->settings.hardcursor) mem_reservation = 2048;
392 	/* reserve space for DMA acceleration engine command buffer (if active) */
393 	/* note:
394 	 * the buffer is 32kB in size. Keep some extra distance for safety (faulty apps). */
395 	if (si->settings.dma_acc)
396 	{
397 		if (si->ps.card_arch < NV40A)
398 		{
399 			/* keeping 32kB distance from the DMA buffer */
400 			mem_reservation += (64 * 1024);
401 		}
402 		else
403 		{
404 			/* 416kB distance is just OK: keeping another 64kB distance for safety;
405 			 * confirmed for NV43. */
406 			/* note:
407 			 * if you get too close to the DMA command buffer on NV40 and NV43 at
408 			 * least (both confirmed), the source DMA instance will mess-up for
409 			 * at least engine cmd NV_IMAGE_BLIT and NV12_IMAGE_BLIT. */
410 			mem_reservation += (512 * 1024);
411 		}
412 	}
413 
414 	/* memory requirement for frame buffer */
415 	if ((row_bytes * target->virtual_height) >
416 		(si->ps.memory_size - mem_reservation))
417 	{
418 		target->virtual_height =
419 			(si->ps.memory_size - mem_reservation) / row_bytes;
420 	}
421 	if (target->virtual_height < target->timing.v_display)
422 	{
423 		LOG(4,("PROPOSEMODE: not enough memory for current mode, aborted.\n"));
424 		return B_ERROR;
425 	}
426 	LOG(4,("PROPOSEMODE: validated virtual_width %d, virtual_height %d pixels\n",
427 		target->virtual_width, target->virtual_height));
428 
429 	if ((target->virtual_height < low->virtual_height) ||
430 		(target->virtual_height > high->virtual_height))
431 	{
432 		status = B_BAD_VALUE;
433 		LOG(4, ("PROPOSEMODE: WARNING: virtual_height deviates too much\n"));
434 	}
435 
436 	/* setup status flags */
437 	LOG(1, ("PROPOSEMODE: initial modeflags: $%08x\n", target->flags));
438 	/* preset to singlehead card without TVout, no overlay support and no hardcursor.
439 	 * also advice system that app_server and acc engine may touch the framebuffer
440 	 * simultaneously (fixed). */
441 	target->flags &=
442 		~(DUALHEAD_CAPABLE | TV_CAPABLE | B_SUPPORTS_OVERLAYS | B_HARDWARE_CURSOR | B_IO_FB_NA);
443 	/* we always allow parallel access (fixed), the DAC is always in 'enhanced'
444 	 * mode (fixed), and all modes support DPMS (fixed);
445 	 * We support scrolling and panning in every mode, so we 'send a signal' to
446 	 * BWindowScreen.CanControlFrameBuffer() by setting B_SCROLL.  */
447 	/* BTW: B_PARALLEL_ACCESS in combination with a hardcursor enables
448 	 * BDirectWindow windowed modes. */
449 	target->flags |= (B_PARALLEL_ACCESS | B_8_BIT_DAC | B_DPMS | B_SCROLL);
450 
451 	/* determine the 'would be' max. pixelclock for the second DAC for the current videomode if dualhead were activated */
452 	switch (target->space)
453 	{
454 		case B_CMAP8:
455 			max_vclk = si->ps.max_dac2_clock_8;
456 			bpp = 1;
457 			break;
458 		case B_RGB15_LITTLE:
459 		case B_RGB16_LITTLE:
460 			max_vclk = si->ps.max_dac2_clock_16;
461 			bpp = 2;
462 			break;
463 		case B_RGB24_LITTLE:
464 			max_vclk = si->ps.max_dac2_clock_24;
465 			bpp = 3;
466 			break;
467 		case B_RGB32_LITTLE:
468 			max_vclk = si->ps.max_dac2_clock_32dh;
469 			bpp = 4;
470 			break;
471 		default:
472 			/* use fail-safe value */
473 			max_vclk = si->ps.max_dac2_clock_32dh;
474 			bpp = 4;
475 			break;
476 	}
477 
478 	/* set DUALHEAD_CAPABLE if suitable */
479 	//fixme: update for independant secondary head use! (reserve fixed memory then)
480 	if (si->ps.secondary_head && (target->timing.pixel_clock <= (max_vclk * 1000)))
481 	{
482 		switch (target->flags & DUALHEAD_BITS)
483 		{
484 		case DUALHEAD_ON:
485 		case DUALHEAD_SWITCH:
486 			if (((si->ps.memory_size - mem_reservation) >=
487 					(row_bytes * target->virtual_height)) &&
488 			 	((uint16)(row_bytes / bpp) >= (target->timing.h_display * 2)))
489 			{
490 				target->flags |= DUALHEAD_CAPABLE;
491 			}
492 			break;
493 		case DUALHEAD_CLONE:
494 			if ((si->ps.memory_size - mem_reservation) >=
495 					(row_bytes * target->virtual_height))
496 			{
497 				target->flags |= DUALHEAD_CAPABLE;
498 			}
499 			break;
500 		case DUALHEAD_OFF:
501 			if ((si->ps.memory_size - mem_reservation) >=
502 					(row_bytes * target->virtual_height * 2))
503 			{
504 				target->flags |= DUALHEAD_CAPABLE;
505 			}
506 			break;
507 		}
508 	}
509 
510 	/* if not dualhead capable card clear dualhead flags */
511 	if (!(target->flags & DUALHEAD_CAPABLE))
512 	{
513 		target->flags &= ~DUALHEAD_BITS;
514 	}
515 
516 	/* set TV_CAPABLE if suitable: pixelclock is not important (defined by TVstandard) */
517 	if (si->ps.tvout && BT_check_tvmode(*target))
518 	{
519 		target->flags |= TV_CAPABLE;
520 	}
521 
522 	/* if not TVout capable card clear TVout flags */
523 	if (!(target->flags & TV_CAPABLE))
524 	{
525 		target->flags &= ~TV_BITS;
526 	}
527 
528 	/* make sure TV head assignment is sane */
529 	if (target->flags & TV_BITS)
530 	{
531 		if (!si->ps.secondary_head)
532 		{
533 			target->flags |= TV_PRIMARY;
534 		}
535 		else
536 		{
537 			if ((target->flags & DUALHEAD_BITS) == DUALHEAD_OFF)
538 				target->flags |= TV_PRIMARY;
539 		}
540 	}
541 	else
542 	{
543 		target->flags &= ~TV_PRIMARY;
544 	}
545 
546 	/* set HARDWARE_CURSOR mode if suitable */
547 	if (si->settings.hardcursor)
548 		target->flags |= B_HARDWARE_CURSOR;
549 
550 	/* set SUPPORTS_OVERLAYS */
551 	target->flags |= B_SUPPORTS_OVERLAYS;
552 
553 	LOG(1, ("PROPOSEMODE: validated modeflags: $%08x\n", target->flags));
554 
555 	/* overrule timing command flags to be (fixed) blank_pedestal = 0.0IRE,
556 	 * progressive scan (fixed), and sync_on_green not avaible. */
557 	target->timing.flags &= ~(B_BLANK_PEDESTAL | B_TIMING_INTERLACED | B_SYNC_ON_GREEN);
558 	/* The HSYNC and VSYNC command flags are actually executed by the driver. */
559 
560 	if (status == B_OK)	LOG(4, ("PROPOSEMODE: completed successfully.\n"));
561 	else LOG(4, ("PROPOSEMODE: mode can be made, but outside given limits.\n"));
562 	return status;
563 }
564 
565 /* Return the number of modes this device will return from GET_MODE_LIST().
566 	This is precalculated in create_mode_list (called from InitAccelerant stuff)
567 */
568 uint32 ACCELERANT_MODE_COUNT(void)
569 {
570 	LOG(1, ("ACCELERANT_MODE_COUNT: the modelist contains %d modes\n",si->mode_count));
571 
572 	return si->mode_count;
573 }
574 
575 /* Copy the list of guaranteed supported video modes to the location provided.*/
576 status_t GET_MODE_LIST(display_mode *dm)
577 {
578 	LOG(1, ("GET_MODE_LIST: exporting the modelist created before.\n"));
579 
580 	memcpy(dm, my_mode_list, si->mode_count * sizeof(display_mode));
581 	return B_OK;
582 }
583 
584 /* Create a list of display_modes to pass back to the caller.*/
585 status_t create_mode_list(void)
586 {
587 	size_t max_size;
588 	uint32
589 		i, j,
590 		pix_clk_range;
591 	const display_mode
592 		*src;
593 	display_mode
594 		*dst,
595 		low,
596 		high;
597 
598 	color_space spaces[4] = {B_RGB32_LITTLE,B_RGB16_LITTLE,B_RGB15_LITTLE,B_CMAP8};
599 
600 	/* figure out how big the list could be, and adjust up to nearest multiple of B_PAGE_SIZE */
601 	max_size = (((MODE_COUNT * 4) * sizeof(display_mode)) + (B_PAGE_SIZE-1)) & ~(B_PAGE_SIZE-1);
602 	/* create an area to hold the info */
603 	si->mode_area = my_mode_list_area =
604 		create_area("NV accelerant mode info", (void **)&my_mode_list, B_ANY_ADDRESS, max_size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
605 	if (my_mode_list_area < B_OK) return my_mode_list_area;
606 
607 	/* walk through our predefined list and see which modes fit this device */
608 	src = mode_list;
609 	dst = my_mode_list;
610 	si->mode_count = 0;
611 	for (i = 0; i < MODE_COUNT; i++)
612 	{
613 		/* set ranges for acceptable values */
614 		low = high = *src;
615 		/* range is 6.25% of default clock: arbitrarily picked */
616 		pix_clk_range = low.timing.pixel_clock >> 5;
617 		low.timing.pixel_clock -= pix_clk_range;
618 		high.timing.pixel_clock += pix_clk_range;
619 		/* 'some cards need wider virtual widths for certain modes':
620 		 * Not true. They might need a wider pitch, but this is _not_ reflected in
621 		 * virtual_width, but in fbc.bytes_per_row. */
622 		//So disable next line:
623 		//high.virtual_width = 4096;
624 		/* do it once for each depth we want to support */
625 		for (j = 0; j < (sizeof(spaces) / sizeof(color_space)); j++)
626 		{
627 			/* set target values */
628 			*dst = *src;
629 			/* poke the specific space */
630 			dst->space = low.space = high.space = spaces[j];
631 			/* ask for a compatible mode */
632 			/* We have to check for B_OK, because otherwise the pix_clk_range
633 			 * won't be taken into account!! */
634 			//So don't do this:
635 			//if (PROPOSE_DISPLAY_MODE(dst, &low, &high) != B_ERROR) {
636 			//Instead, do this:
637 			if (PROPOSE_DISPLAY_MODE(dst, &low, &high) == B_OK) {
638 				/* count it, and move on to next mode */
639 				dst++;
640 				si->mode_count++;
641 			}
642 		}
643 		/* advance to next mode */
644 		src++;
645 	}
646 
647 	return B_OK;
648 }
649