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