xref: /haiku/src/add-ons/accelerants/neomagic/ProposeDisplayMode.c (revision 1a76488fc88584bf66b9751d7fb9b6527ac20d87)
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 nm driver:
6 	Rudolf Cornelissen 4/2003-1/2006
7 */
8 
9 #define MODULE_BIT 0x00400000
10 
11 #include "acc_std.h"
12 
13 #define	T_POSITIVE_SYNC	(B_POSITIVE_HSYNC | B_POSITIVE_VSYNC)
14 /* mode flags will be setup as status info by PROPOSEMODE! */
15 #define MODE_FLAGS 0
16 #define MODE_COUNT (sizeof (mode_list) / sizeof (display_mode))
17 
18 /*some monitors only handle a fixed set of modes*/
19 #include "valid_mode_list"
20 
21 /* Standard VESA modes within NM2380 range, which is the high-end supported NeoMagic card */
22 static const display_mode mode_list[] = {
23 { { 25175, 640, 656, 752, 800, 480, 490, 492, 525, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(640X480X8.Z1) */
24 { { 27500, 640, 672, 768, 864, 480, 488, 494, 530, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* 640X480X60Hz */
25 { { 30500, 640, 672, 768, 864, 480, 517, 523, 588, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* SVGA_640X480X60HzNI */
26 { { 31500, 640, 664, 704, 832, 480, 489, 492, 520, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70-72Hz_(640X480X8.Z1) */
27 { { 31500, 640, 656, 720, 840, 480, 481, 484, 500, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(640X480X8.Z1) */
28 { { 36000, 640, 696, 752, 832, 480, 481, 484, 509, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(640X480X8.Z1) */
29 { { 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 */
30 { { 38100, 800, 832, 960, 1088, 600, 602, 606, 620, 0}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* SVGA_800X600X56HzNI */
31 { { 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 */
32 { { 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 */
33 { { 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 */
34 { { 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 */
35 { { 65000, 1024, 1048, 1184, 1344, 768, 771, 777, 806, 0}, B_CMAP8, 1024, 768, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1024X768X8.Z1) + XFree86 */
36 { { 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 */
37 { { 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 */
38 { { 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 */
39 { { 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) */
40 { { 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) */
41 { { 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 */
42 { { 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 */
43 };
44 
45 /*
46 Check mode is between low and high limits
47 returns:
48 	B_OK - found one
49 	B_BAD_VALUE - mode can be made, but outside limits
50 	B_ERROR - not possible
51 */
52 /* BOUNDS WARNING:
53  * BeOS (tested R5.0.3PE) is failing BWindowScreen.SetFrameBuffer() if PROPOSEMODE
54  * returns B_BAD_VALUE. It's called by the OS with target, low and high set to
55  * have the same settings for BWindowScreen!
56  * Which means we should not return B_BAD_VALUE on anything except for deviations on:
57  * display_mode.virtual_width;
58  * display_mode.virtual_height;
59  * display_mode.timing.h_display;
60  * display_mode.timing.v_display;
61  */
62 /* Note:
63  * The target mode should be modified to correspond to the mode as it can be made. */
64 status_t PROPOSE_DISPLAY_MODE(display_mode *target, const display_mode *low, const display_mode *high)
65 {
66 	status_t status = B_OK;
67 	float pix_clock_found;
68 	uint8 m,n,p;
69 	status_t result;
70 	uint32 row_bytes, pointer_reservation;
71 	bool acc_mode;
72 	double target_refresh = ((double)target->timing.pixel_clock * 1000.0) /
73 			(
74 				(double)target->timing.h_total *
75 				(double)target->timing.v_total
76 			);
77 	bool
78 		want_same_width = target->timing.h_display == target->virtual_width,
79 		want_same_height = target->timing.v_display == target->virtual_height;
80 
81 	LOG(1, ("PROPOSEMODE: (ENTER) requested virtual_width %d, virtual_height %d\n",
82 									target->virtual_width, target->virtual_height));
83 
84 	/*check valid list:
85 		if (VALID_REQUIRED is set)
86 		{
87 			if (find modes with same size)
88 			{
89 				pick one with nearest pixel clock
90 			}
91 			else
92 			{
93 				pick next largest with nearest pixel clock and modify visible portion as far as possible
94 			}
95 		}
96 	*/
97 	#ifdef VALID_MODE_REQUIRED
98 	{
99 		int i;
100 		int closest_mode_ptr;
101 		uint32 closest_mode_clock;
102 
103 		LOG(1, ("PROPOSEMODE: valid mode required!\n"));
104 
105 		closest_mode_ptr = 0xbad;
106 		closest_mode_clock = 0;
107 		for (i=0;i<VALID_MODES;i++)
108 		{
109 			/*check size is ok and clock is better than any found before*/
110 			if(
111 				target->timing.h_display==valid_mode_list[i].h_display &&
112 				target->timing.v_display==valid_mode_list[i].v_display
113 			)
114 			{
115 				if (
116 					abs(valid_mode_list[i].pixel_clock-target->timing.pixel_clock)<
117 					abs(closest_mode_clock-target->timing.pixel_clock)
118 				)
119 				{
120 					closest_mode_clock=valid_mode_list[i].pixel_clock;
121 					closest_mode_ptr=i;
122 				}
123 			}
124 		}
125 
126 		if (closest_mode_ptr==0xbad)/*if no modes of correct size*/
127 		{
128 			LOG(4, ("PROPOSEMODE: no valid mode found, aborted.\n"));
129 			return B_ERROR;
130 		}
131 		else
132 		{
133 			target->timing=valid_mode_list[closest_mode_ptr];
134 			target_refresh = ((double)target->timing.pixel_clock * 1000.0) /  /*I require this refresh*/
135 			((double)target->timing.h_total * (double)target->timing.v_total);
136 		}
137 	}
138 	#endif
139 
140 	/* NM2070 cannot do 24 bit color */
141 	if ((si->ps.card_type == NM2070) && (target->space == B_RGB24_LITTLE))
142 	{
143 		LOG(4, ("PROPOSEMODE: 24bit color not supported on NM2070, aborted.\n"));
144 		return B_ERROR;
145 	}
146 
147 	/*find a nearby valid timing from that given*/
148 	result = nm_crtc_validate_timing
149 	(
150 		&target->timing.h_display, &target->timing.h_sync_start, &target->timing.h_sync_end, &target->timing.h_total,
151 		&target->timing.v_display, &target->timing.v_sync_start, &target->timing.v_sync_end, &target->timing.v_total
152 	);
153 	if (result == B_ERROR)
154 	{
155 		LOG(4, ("PROPOSEMODE: could not validate timing, aborted.\n"));
156 		return result;
157 	}
158 
159 	/* validate display vs. virtual */
160 	if ((target->timing.h_display > target->virtual_width) || want_same_width)
161 		target->virtual_width = target->timing.h_display;
162 	if ((target->timing.v_display > target->virtual_height) || want_same_height)
163 		target->virtual_height = target->timing.v_display;
164 
165 	/* nail virtual size and 'subsequently' calculate rowbytes */
166 	result = nm_general_validate_pic_size (target, &row_bytes, &acc_mode);
167 	if (result == B_ERROR)
168 	{
169 		LOG(4, ("PROPOSEMODE: could not validate virtual picture size, aborted.\n"));
170 		return result;
171 	}
172 
173 	/*check if virtual_width is still within the requested limits*/
174 	if ((target->virtual_width < low->virtual_width) ||
175 		(target->virtual_width > high->virtual_width))
176 	{
177 		status = B_BAD_VALUE;
178 		LOG(4, ("PROPOSEMODE: WARNING: virtual_width deviates too much\n"));
179 	}
180 
181 	/*check if timing found is within the requested horizontal limits*/
182 	if ((target->timing.h_display < low->timing.h_display) ||
183 		(target->timing.h_display > high->timing.h_display) ||
184 		(target->timing.h_sync_start < low->timing.h_sync_start) ||
185 		(target->timing.h_sync_start > high->timing.h_sync_start) ||
186 		(target->timing.h_sync_end < low->timing.h_sync_end) ||
187 		(target->timing.h_sync_end > high->timing.h_sync_end) ||
188 		(target->timing.h_total < low->timing.h_total) ||
189 		(target->timing.h_total > high->timing.h_total))
190 	{
191 		/* BWindowScreen workaround: we accept everything except h_display deviations */
192 		if ((target->timing.h_display < low->timing.h_display) ||
193 			(target->timing.h_display > high->timing.h_display))
194 		{
195 			status = B_BAD_VALUE;
196 		}
197 		LOG(4, ("PROPOSEMODE: WARNING: horizontal timing deviates too much\n"));
198 	}
199 
200 	/*check if timing found is within the requested vertical limits*/
201 	if (
202 		(target->timing.v_display < low->timing.v_display) ||
203 		(target->timing.v_display > high->timing.v_display) ||
204 		(target->timing.v_sync_start < low->timing.v_sync_start) ||
205 		(target->timing.v_sync_start > high->timing.v_sync_start) ||
206 		(target->timing.v_sync_end < low->timing.v_sync_end) ||
207 		(target->timing.v_sync_end > high->timing.v_sync_end) ||
208 		(target->timing.v_total < low->timing.v_total) ||
209 		(target->timing.v_total > high->timing.v_total)
210 	)
211 	{
212 		/* BWindowScreen workaround: we accept everything except v_display deviations */
213 		if ((target->timing.v_display < low->timing.v_display) ||
214 			(target->timing.v_display > high->timing.v_display))
215 		{
216 			status = B_BAD_VALUE;
217 		}
218 		LOG(4, ("PROPOSEMODE: WARNING: vertical timing deviates too much\n"));
219 	}
220 
221 	/* adjust pixelclock for possible timing modifications done above */
222 	target->timing.pixel_clock = target_refresh * ((double)target->timing.h_total) * ((double)target->timing.v_total) / 1000.0;
223 
224 	/* Now find the nearest valid pixelclock we actually can setup for the target mode,
225 	 * this also makes sure we don't generate more pixel bandwidth than the device can handle */
226 	result = nm_dac_pix_pll_find(*target,&pix_clock_found,&m,&n,&p);
227 	/* update the target mode */
228 	target->timing.pixel_clock = (pix_clock_found * 1000);
229 
230 	/* note if we fell outside the limits */
231 	if ((target->timing.pixel_clock < low->timing.pixel_clock) ||
232 		(target->timing.pixel_clock > high->timing.pixel_clock)
233 	)
234 	{
235 		/* BWindowScreen workaround: we accept deviations <= 1Mhz */
236 		if ((target->timing.pixel_clock < (low->timing.pixel_clock - 1000)) ||
237 			(target->timing.pixel_clock > (high->timing.pixel_clock + 1000)))
238 		{
239 			status = B_BAD_VALUE;
240 		}
241 		LOG(4, ("PROPOSEMODE: WARNING: pixelclock deviates too much\n"));
242 	}
243 
244 	/* checkout space needed for hardcursor (if any) */
245 	pointer_reservation = 0;
246 	if (si->settings.hardcursor) pointer_reservation = si->ps.curmem_size;
247 	/* memory requirement for frame buffer */
248 	if ((row_bytes * target->virtual_height) >
249 		((si->ps.memory_size * 1024) - pointer_reservation))
250 	{
251 		target->virtual_height =
252 			((si->ps.memory_size * 1024) - pointer_reservation) / row_bytes;
253 	}
254 	if (target->virtual_height < target->timing.v_display)
255 	{
256 		LOG(4,("PROPOSEMODE: not enough memory for current mode, aborted.\n"));
257 		return B_ERROR;
258 	}
259 	LOG(4,("PROPOSEMODE: validated virtual_width %d, virtual_height %d pixels\n",
260 		target->virtual_width, target->virtual_height));
261 
262 	if ((target->virtual_height < low->virtual_height) ||
263 		(target->virtual_height > high->virtual_height))
264 	{
265 		status = B_BAD_VALUE;
266 		LOG(4, ("PROPOSEMODE: WARNING: virtual_height deviates too much\n"));
267 	}
268 
269 	/* setup status flags */
270 	LOG(1, ("PROPOSEMODE: initial modeflags: $%08x\n", target->flags));
271 	/* preset to singlehead card without TVout, no overlay support and no hardcursor.
272 	 * also advice system that app_server and acc engine may touch the framebuffer
273 	 * simultaneously (fixed). */
274 //fixme: introduce dualhead_clone_only flag compatible with matrox so the same prefs
275 //util can be used
276 	target->flags &=
277 		~(DUALHEAD_CAPABLE | TV_CAPABLE | B_SUPPORTS_OVERLAYS | B_HARDWARE_CURSOR | B_IO_FB_NA);
278 	/* we always allow parallel access (fixed), the DAC is always in 'enhanced'
279 	 * mode (fixed), and all modes support DPMS (fixed);
280 	 * We support scrolling and panning in every mode, so we 'send a signal' to
281 	 * BWindowScreen.CanControlFrameBuffer() by setting B_SCROLL.  */
282 	/* BTW: B_PARALLEL_ACCESS in combination with a hardcursor enables
283 	 * BDirectWindow windowed modes. */
284 	target->flags |= (B_PARALLEL_ACCESS | B_8_BIT_DAC | B_DPMS | B_SCROLL);
285 
286 	/* if not dualhead capable card clear dualhead flags */
287 	if (!(target->flags & DUALHEAD_CAPABLE))
288 	{
289 		target->flags &= ~DUALHEAD_BITS;
290 	}
291 
292 	/* if not TVout capable card clear TVout flags */
293 	if (!(target->flags & TV_CAPABLE))
294 	{
295 		target->flags &= ~TV_BITS;
296 	}
297 
298 	/* TVout is on primary head (presumably, if we'd have that) */
299 	target->flags |= TV_PRIMARY;
300 
301 	/* set HARDWARE_CURSOR mode if suitable */
302 	if (si->settings.hardcursor)
303 		target->flags |= B_HARDWARE_CURSOR;
304 
305 	/* set SUPPORTS_OVERLAYS if suitable */
306 	if (si->ps.card_type > NM2070)
307 		target->flags |= B_SUPPORTS_OVERLAYS;
308 
309 	LOG(1, ("PROPOSEMODE: validated modeflags: $%08x\n", target->flags));
310 
311 	/* overrule timing command flags to be (fixed) blank_pedestal = 0.0IRE,
312 	 * progressive scan (fixed), and sync_on_green not used */
313 	target->timing.flags &= ~(B_BLANK_PEDESTAL | B_TIMING_INTERLACED | B_SYNC_ON_GREEN);
314 	/* The HSYNC and VSYNC command flags are actually executed by the driver. */
315 
316 	if (status == B_OK)	LOG(4, ("PROPOSEMODE: completed successfully.\n"));
317 	else LOG(4, ("PROPOSEMODE: mode can be made, but outside given limits.\n"));
318 	return status;
319 }
320 
321 /* Return the number of modes this device will return from GET_MODE_LIST().
322 	This is precalculated in create_mode_list (called from InitAccelerant stuff)
323 */
324 uint32 ACCELERANT_MODE_COUNT(void)
325 {
326 	LOG(1, ("ACCELERANT_MODE_COUNT: the modelist contains %d modes\n",si->mode_count));
327 
328 	return si->mode_count;
329 }
330 
331 /* Copy the list of guaranteed supported video modes to the location provided.*/
332 status_t GET_MODE_LIST(display_mode *dm)
333 {
334 	LOG(1, ("GET_MODE_LIST: exporting the modelist created before.\n"));
335 
336 	memcpy(dm, my_mode_list, si->mode_count * sizeof(display_mode));
337 	return B_OK;
338 }
339 
340 /* Create a list of display_modes to pass back to the caller.*/
341 status_t create_mode_list(void) {
342 	size_t max_size;
343 	uint32
344 		i, j,
345 		pix_clk_range;
346 	const display_mode
347 		*src;
348 	display_mode
349 		*dst,
350 		low,
351 		high;
352 
353 	color_space spaces[4] = {B_RGB24_LITTLE,B_RGB16_LITTLE,B_RGB15_LITTLE,B_CMAP8};
354 
355 	/* figure out how big the list could be, and adjust up to nearest multiple of B_PAGE_SIZE*/
356 	max_size = (((MODE_COUNT * 4) * sizeof(display_mode)) + (B_PAGE_SIZE-1)) & ~(B_PAGE_SIZE-1);
357 	/* create an area to hold the info */
358 	si->mode_area = my_mode_list_area =
359 		create_area("nm accelerant mode info", (void **)&my_mode_list, B_ANY_ADDRESS, max_size,
360 			B_NO_LOCK, B_READ_AREA | B_WRITE_AREA | B_CLONEABLE_AREA);
361 	if (my_mode_list_area < B_OK) return my_mode_list_area;
362 
363 	/* walk through our predefined list and see which modes fit this device */
364 	src = mode_list;
365 	dst = my_mode_list;
366 	si->mode_count = 0;
367 	for (i = 0; i < MODE_COUNT; i++) {
368 		/* set ranges for acceptable values */
369 		low = high = *src;
370 		/* range is 6.25% of default clock: arbitrarily picked */
371 		pix_clk_range = low.timing.pixel_clock >> 5;
372 		low.timing.pixel_clock -= pix_clk_range;
373 		high.timing.pixel_clock += pix_clk_range;
374 		/* 'some cards need wider virtual widths for certain modes':
375 		 * Not true. They might need a wider pitch, but this is _not_ reflected in
376 		 * virtual_width, but in fbc.bytes_per_row. */
377 		//So disable next line:
378 		//high.virtual_width = 4096;
379 		/* do it once for each depth we want to support */
380 		for (j = 0; j < (sizeof(spaces) / sizeof(color_space)); j++)
381 		{
382 			/* set target values */
383 			*dst = *src;
384 			/* poke the specific space */
385 			dst->space = low.space = high.space = spaces[j];
386 			/* ask for a compatible mode */
387 			/* We have to check for B_OK, because otherwise the pix_clk_range
388 			 * won't be taken into account!! */
389 			//So don't do this:
390 			//if (PROPOSE_DISPLAY_MODE(dst, &low, &high) != B_ERROR) {
391 			//Instead, do this:
392 			if (PROPOSE_DISPLAY_MODE(dst, &low, &high) == B_OK) {
393 				/* count it, and move on to next mode */
394 				dst++;
395 				si->mode_count++;
396 			}
397 		}
398 		/* advance to next mode */
399 		src++;
400 	}
401 
402 	return B_OK;
403 }
404