xref: /haiku/src/add-ons/accelerants/vesa/mode.cpp (revision 2f470aec1c92ce6917b8a903e343795dc77af41f)
1 /*
2  * Copyright 2005-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <string.h>
7 
8 #include "accelerant_protos.h"
9 #include "accelerant.h"
10 #include "utility.h"
11 
12 
13 //#define TRACE_MODE
14 #ifdef TRACE_MODE
15 extern "C" void _sPrintf(const char *format, ...);
16 #	define TRACE(x) _sPrintf x
17 #else
18 #	define TRACE(x) ;
19 #endif
20 
21 
22 #define	T_POSITIVE_SYNC	(B_POSITIVE_HSYNC | B_POSITIVE_VSYNC)
23 #define MODE_FLAGS	(B_8_BIT_DAC | B_HARDWARE_CURSOR | B_PARALLEL_ACCESS | B_DPMS /*| B_SUPPORTS_OVERLAYS*/)
24 
25 static const display_mode kBaseModeList[] = {
26 { { 25175, 640, 656, 752, 800, 350, 387, 389, 449, B_POSITIVE_HSYNC}, B_CMAP8, 640, 350, 0, 0, MODE_FLAGS}, /* 640x350 - www.epanorama.net/documents/pc/vga_timing.html) */
27 { { 25175, 640, 656, 752, 800, 400, 412, 414, 449, B_POSITIVE_VSYNC}, B_CMAP8, 640, 400, 0, 0, MODE_FLAGS}, /* 640x400 - www.epanorama.net/documents/pc/vga_timing.html) */
28 { { 25175, 640, 656, 752, 800, 480, 490, 492, 525, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(640X480X8.Z1) */
29 { { 27500, 640, 672, 768, 864, 480, 488, 494, 530, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* 640X480X60Hz */
30 { { 30500, 640, 672, 768, 864, 480, 517, 523, 588, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* SVGA_640X480X60HzNI */
31 { { 31500, 640, 664, 704, 832, 480, 489, 492, 520, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70-72Hz_(640X480X8.Z1) */
32 { { 31500, 640, 656, 720, 840, 480, 481, 484, 500, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(640X480X8.Z1) */
33 { { 36000, 640, 696, 752, 832, 480, 481, 484, 509, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(640X480X8.Z1) */
34 { { 38100, 800, 832, 960, 1088, 600, 602, 606, 620, 0}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* SVGA_800X600X56HzNI */
35 { { 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) */
36 { { 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) */
37 { { 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) */
38 { { 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) */
39 { { 65000, 1024, 1048, 1184, 1344, 768, 771, 777, 806, 0}, B_CMAP8, 1024, 768, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1024X768X8.Z1) */
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) */
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) */
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) */
43 { { 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) */
44 { { 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) */
45 { { 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) */
46 { { 108000, 1280, 1328, 1440, 1688, 1024, 1025, 1028, 1066, T_POSITIVE_SYNC}, B_CMAP8, 1280, 1024, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1280X1024X8.Z1) */
47 { { 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) */
48 { { 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) */
49 { { 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) */
50 { { 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) */
51 { { 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) */
52 { { 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) */
53 { { 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) */
54 { { 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) */
55 };
56 static const uint32 kNumBaseModes = sizeof(kBaseModeList) / sizeof(display_mode);
57 static const uint32 kMaxNumModes = kNumBaseModes * 4;
58 
59 
60 /**	Creates the initial mode list of the primary accelerant.
61  *	It's called from vesa_init_accelerant().
62  */
63 
64 status_t
65 create_mode_list(void)
66 {
67 	// ToDo: basically, the boot loader should pass a list of all supported
68 	//	VESA modes to us.
69 	color_space spaces[4] = {B_RGB32_LITTLE, B_RGB16_LITTLE, B_RGB15_LITTLE, B_CMAP8};
70 	size_t size = ROUND_TO_PAGE_SIZE(kMaxNumModes * sizeof(display_mode));
71 	display_mode *list;
72 
73 	gInfo->mode_list_area = create_area("vesa modes", (void **)&list, B_ANY_ADDRESS,
74 		size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
75 	if (gInfo->mode_list_area < B_OK)
76 		return gInfo->mode_list_area;
77 
78 	// for now, just copy all of the modes with different color spaces
79 
80 	const display_mode *source = kBaseModeList;
81 	uint32 count = 0;
82 
83 	for (uint32 i = 0; i < kNumBaseModes; i++) {
84 		for (uint32 j = 0; j < (sizeof(spaces) / sizeof(color_space)); j++) {
85 			list[count] = *source;
86 			list[count].space = spaces[j];
87 
88 			count++;
89 		}
90 
91 		source++;
92 	}
93 
94 	gInfo->mode_list = list;
95 	gInfo->shared_info->mode_list_area = gInfo->mode_list_area;
96 	gInfo->shared_info->mode_count = count;
97 
98 	return B_OK;
99 }
100 
101 
102 //	#pragma mark -
103 
104 
105 uint32
106 vesa_accelerant_mode_count(void)
107 {
108 	TRACE(("vesa_accelerant_mode_count()\n"));
109 	return gInfo->shared_info->mode_count;
110 }
111 
112 
113 status_t
114 vesa_get_mode_list(display_mode *modeList)
115 {
116 	TRACE(("vesa_get_mode_info()\n"));
117 	memcpy(modeList, gInfo->mode_list, gInfo->shared_info->mode_count * sizeof(display_mode));
118 	return B_OK;
119 }
120 
121 
122 status_t
123 vesa_propose_display_mode(display_mode *target, const display_mode *low,
124 	const display_mode *high)
125 {
126 	TRACE(("vesa_propose_display_mode()\n"));
127 
128 	// just search for the specified mode in the list
129 
130 	for (uint32 i = 0; i < gInfo->shared_info->mode_count; i++) {
131 		display_mode *current = &gInfo->mode_list[i];
132 
133 		if (target->virtual_width != current->virtual_width
134 			|| target->virtual_height != current->virtual_height
135 			|| target->space != current->space)
136 			continue;
137 
138 		*target = *current;
139 		return B_OK;
140 	}
141 	return B_BAD_VALUE;
142 }
143 
144 
145 status_t
146 vesa_set_display_mode(display_mode *_mode)
147 {
148 	TRACE(("vesa_set_display_mode()\n"));
149 
150 	display_mode mode = *_mode;
151 	if (vesa_propose_display_mode(&mode, &mode, &mode) != B_OK)
152 		return B_BAD_VALUE;
153 
154 	// ToDo: unless we have a VBE3 device, we can't change the display mode
155 	return B_UNSUPPORTED;
156 #if 0
157 	gInfo->shared_info->current_mode = mode;
158 
159 	switch (mode.space) {
160 		case B_RGB32_LITTLE:
161 			gInfo->shared_info->bytes_per_row = mode.virtual_width * 4;
162 			break;
163 		case B_RGB16_LITTLE:
164 		case B_RGB15_LITTLE:
165 			gInfo->shared_info->bytes_per_row = mode.virtual_width * 2;
166 			break;
167 		case B_CMAP8:
168 			gInfo->shared_info->bytes_per_row = mode.virtual_width;
169 			break;
170 	}
171 	return B_OK;
172 #endif
173 }
174 
175 
176 status_t
177 vesa_get_display_mode(display_mode *_currentMode)
178 {
179 	TRACE(("vesa_get_display_mode()\n"));
180 	*_currentMode = gInfo->shared_info->current_mode;
181 	return B_OK;
182 }
183 
184 
185 status_t
186 vesa_get_frame_buffer_config(frame_buffer_config *config)
187 {
188 	TRACE(("vesa_get_frame_buffer_config()\n"));
189 
190 	config->frame_buffer = gInfo->shared_info->frame_buffer;
191 	config->frame_buffer_dma = gInfo->shared_info->physical_frame_buffer;
192 	config->bytes_per_row = gInfo->shared_info->bytes_per_row;
193 
194 	return B_OK;
195 }
196 
197 
198 status_t
199 vesa_get_pixel_clock_limits(display_mode *mode, uint32 *low, uint32 *high)
200 {
201 	TRACE(("vesa_get_pixel_clock_limits()\n"));
202 
203 	// ToDo: do some real stuff here (taken from radeon driver)
204 	uint32 total_pix = (uint32)mode->timing.h_total * (uint32)mode->timing.v_total;
205 	uint32 clock_limit = 2000000;
206 
207 	/* lower limit of about 48Hz vertical refresh */
208 	*low = (total_pix * 48L) / 1000L;
209 	if (*low > clock_limit)
210 		return B_ERROR;
211 
212 	*high = clock_limit;
213 	return B_OK;
214 }
215 
216 
217 status_t
218 vesa_move_display(uint16 h_display_start, uint16 v_display_start)
219 {
220 	TRACE(("vesa_move_display()\n"));
221 	return B_ERROR;
222 }
223 
224 
225 status_t
226 vesa_get_timing_constraints(display_timing_constraints *dtc)
227 {
228 	TRACE(("vesa_get_timing_constraints()\n"));
229 	return B_ERROR;
230 }
231 
232 
233 void
234 vesa_set_indexed_colors(uint count, uint8 first, uint8 *colors, uint32 flags)
235 {
236 	TRACE(("vesa_set_indexed_colors()\n"));
237 	vga_set_indexed_colors_args args;
238 	args.first = first;
239 	args.count = count;
240 	args.colors = colors;
241 	ioctl(gInfo->device, VGA_SET_INDEXED_COLORS, &args, sizeof(args));
242 }
243 
244