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