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