xref: /haiku/src/add-ons/accelerants/matrox/InitAccelerant.c (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
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:
6 	Mark Watson,
7 	Rudolf Cornelissen 10/2002-11/2005.
8 */
9 
10 #define MODULE_BIT 0x00800000
11 
12 #include <string.h>
13 #include <unistd.h>
14 #include "acc_std.h"
15 
16 static status_t init_common(int the_fd);
17 
18 /* Initialization code shared between primary and cloned accelerants */
19 static status_t init_common(int the_fd) {
20 	status_t result;
21 	gx00_get_private_data gpd;
22 
23 	// LOG not available from here to next LOG: NULL si
24 
25 	/* memorize the file descriptor */
26 	fd = the_fd;
27 	/* set the magic number so the driver knows we're for real */
28 	gpd.magic = GX00_PRIVATE_DATA_MAGIC;
29 	/* contact driver and get a pointer to the registers and shared data */
30 	result = ioctl(fd, GX00_GET_PRIVATE_DATA, &gpd, sizeof(gpd));
31 	if (result != B_OK) goto error0;
32 
33 	/* clone the shared area for our use */
34 	shared_info_area = clone_area(DRIVER_PREFIX " shared", (void **)&si, B_ANY_ADDRESS,
35 		B_READ_AREA | B_WRITE_AREA, gpd.shared_info_area);
36 	if (shared_info_area < 0) {
37 			result = shared_info_area;
38 			goto error0;
39 	}
40 	// LOG is now available, si !NULL
41 	LOG(4,("init_common: logmask 0x%08x, memory %dMB, hardcursor %d, usebios %d, greensync %d\n",
42 		si->settings.logmask, si->settings.memory, si->settings.hardcursor, si->settings.usebios, si->settings.greensync));
43 
44  	/*Check for R4.5.0 and if it is running, use work around*/
45  	{
46  		if (si->use_clone_bugfix)
47  		{
48  			/*check for R4.5.0 bug and attempt to work around*/
49  			LOG(2,("InitACC: Found R4.5.0 bug - attempting to work around\n"));
50  			regs = si->clone_bugfix_regs;
51  		}
52  		else
53  		{
54 			/* clone the memory mapped registers for our use  - does not work on <4.5.2 (but is better this way)*/
55 			regs_area = clone_area(DRIVER_PREFIX " regs", (void **)&regs, B_ANY_ADDRESS,
56 				B_READ_AREA | B_WRITE_AREA, si->regs_area);
57 			if (regs_area < 0) {
58 				result = regs_area;
59 				goto error1;
60 			}
61  		}
62  	}
63 
64 	/*FIXME - print dma addresses*/
65 	//LOG(4,("DMA_virtual:%x\tDMA_physical:%x\tDMA_area:%x\n",si->dma_buffer,si->dma_buffer_pci,si->dma_buffer_area));
66 
67 	/* all done */
68 	goto error0;
69 
70 error1:
71 	delete_area(shared_info_area);
72 error0:
73 	return result;
74 }
75 
76 /* Clean up code shared between primary and cloned accelerants */
77 static void uninit_common(void) {
78 	/* release the memory mapped registers */
79 	delete_area(regs_area);
80 	/* a little cheap paranoia */
81 	regs = 0;
82 	/* release our copy of the shared info from the kernel driver */
83 	delete_area(shared_info_area);
84 	/* more cheap paranoia */
85 	si = 0;
86 }
87 
88 /*
89 Initialize the accelerant.  the_fd is the file handle of the device (in
90 /dev/graphics) that has been opened by the app_server (or some test harness).
91 We need to determine if the kernel driver and the accelerant are compatible.
92 If they are, get the accelerant ready to handle other hook functions and
93 report success or failure.
94 */
95 status_t INIT_ACCELERANT(int the_fd) {
96 	status_t result;
97 	int pointer_reservation; //mem reserved for pointer
98 	int cnt; 				 //used for iteration through the overlay buffers
99 
100 	if (0) {
101 		time_t now = time (NULL);
102 		// LOG not available from here to next LOG: NULL si
103 //		MSG(("INIT_ACCELERANT: booted since %f ms %s\n", system_time()/1000.0, real_time_clock()));
104 		MSG(("INIT_ACCELERANT: %s", ctime (&now)));
105 	}
106 
107 	/* note that we're the primary accelerant (accelerantIsClone is global) */
108 	accelerantIsClone = 0;
109 
110 	/* do the initialization common to both the primary and the clones */
111 	result = init_common(the_fd);
112 
113 	/* bail out if the common initialization failed */
114 	if (result != B_OK) goto error0;
115 	// LOG now available: !NULL si
116 
117 	/* assume G450/G550 signals are connected straight through (before powerup) */
118 	si->crossed_conns = false;
119 
120 	/* call the device specific init code */
121 	result = gx00_general_powerup();
122 
123 	/* bail out if it failed */
124 	if (result != B_OK) goto error1;
125 
126 	/*
127 	Now would be a good time to figure out what video modes your card supports.
128 	We'll place the list of modes in another shared area so all of the copies
129 	of the driver can see them.  The primary copy of the accelerant (ie the one
130 	initialized with this routine) will own the "one true copy" of the list.
131 	Everybody else get's a read-only clone.
132 	*/
133 	result = create_mode_list();
134 	if (result != B_OK)
135 	{
136 		goto error1;
137 	}
138 
139 	/*
140 	Put the cursor at the start of the frame buffer.  The typical 64x64 4 color
141 	(black, white, transparent, inverse) takes up 1024 bytes of RAM.
142 	*/
143 	/* Initialize the rest of the cursor information while we're here */
144 	si->cursor.width = 16;
145 	si->cursor.height = 16;
146 	si->cursor.hot_x = 0;
147 	si->cursor.hot_y = 0;
148 	si->cursor.x = 0;
149 	si->cursor.y = 0;
150 
151 	/*
152 	Put the frame buffer immediately following the cursor data. We store this
153 	info in a frame_buffer_config structure to make it convienient to return
154 	to the app_server later.
155 	*/
156 	pointer_reservation = 0;
157 	/* MIL 1/2 cards have a seperate buffer for the cursorbitmap inside the DAC */
158 	if ((si->ps.card_type >= G100) && si->settings.hardcursor)
159 		pointer_reservation = 1024;
160 
161 	si->fbc.frame_buffer = (void *)((char *)si->framebuffer+pointer_reservation);
162 	si->fbc.frame_buffer_dma = (void *)((char *)si->framebuffer_pci+pointer_reservation);
163 
164 	/* count of issued parameters or commands */
165 	si->engine.last_idle = si->engine.count = 0;
166 	INIT_BEN(si->engine.lock);
167 
168 	INIT_BEN(si->overlay.lock);
169 	for (cnt = 0; cnt < MAXBUFFERS; cnt++)
170 	{
171 		/* make sure overlay buffers are 'marked' as being free */
172 		si->overlay.myBuffer[cnt].buffer = NULL;
173 		si->overlay.myBuffer[cnt].buffer_dma = NULL;
174 	}
175 	/* make sure overlay unit is 'marked' as being free */
176 	si->overlay.myToken = NULL;
177 
178 	/* note that overlay is not in use (for gx00_bes_move_overlay()) */
179 	si->overlay.active = false;
180 
181 	/* bail out if something failed */
182 	if (result != B_OK) goto error1;
183 
184 	/* initialise various cursor stuff*/
185 	gx00_crtc_cursor_init();
186 
187 	/* ensure cursor state */
188 	SHOW_CURSOR(false);
189 
190 	/* ensure DPMS state */
191 	si->dpms_flags = B_DPMS_ON;
192 
193 	/* a winner! */
194 	result = B_OK;
195 	goto error0;
196 
197 error1:
198 	/*
199 	Initialization failed after init_common() succeeded, so we need to clean
200 	up before quiting.
201 	*/
202 	uninit_common();
203 
204 error0:
205 	return result;
206 }
207 
208 /*
209 Return the number of bytes required to hold the information required
210 to clone the device.
211 */
212 ssize_t ACCELERANT_CLONE_INFO_SIZE(void) {
213 	/*
214 	Since we're passing the name of the device as the only required
215 	info, return the size of the name buffer
216 	*/
217 	return B_OS_NAME_LENGTH; // apsed, was MAX_GX00_DEVICE_NAME_LENGTH;
218 }
219 
220 
221 /*
222 Return the info required to clone the device.  void *data points to
223 a buffer at least ACCELERANT_CLONE_INFO_SIZE() bytes in length.
224 */
225 void GET_ACCELERANT_CLONE_INFO(void *data) {
226 	gx00_device_name dn;
227 	status_t result;
228 
229 	/* call the kernel driver to get the device name */
230 	dn.magic = GX00_PRIVATE_DATA_MAGIC;
231 	/* store the returned info directly into the passed buffer */
232 	dn.name = (char *)data;
233 	result = ioctl(fd, GX00_DEVICE_NAME, &dn, sizeof(dn));
234 }
235 
236 /*
237 Initialize a copy of the accelerant as a clone.  void *data points to
238 a copy of the data returned by GET_ACCELERANT_CLONE_INFO().
239 */
240 status_t CLONE_ACCELERANT(void *data) {
241 	status_t result;
242 	char path[MAXPATHLEN];
243 
244 	/* the data is the device name */
245 	/* Note: the R4 graphics driver kit is in error here (missing trailing '/') */
246 	strcpy(path, "/dev/");
247 	strcat(path, (const char *)data);
248 	/* open the device, the permissions aren't important */
249 	fd = open(path, B_READ_WRITE);
250 	if (fd < 0)
251 	{
252 		/* we can't use LOG because we didn't get the shared_info struct.. */
253 		char     fname[64];
254 		FILE    *myhand = NULL;
255 
256 		sprintf (fname, "/boot/home/" DRIVER_PREFIX ".accelerant.0.log");
257 		myhand=fopen(fname,"a+");
258 		fprintf(myhand, "CLONE_ACCELERANT: couldn't open kerneldriver %s! Aborting.\n", path);
259 		fclose(myhand);
260 
261 		/* abort with resultcode from open attempt on kerneldriver */
262 		result = fd;
263 		goto error0;
264 	}
265 
266 	/* note that we're a clone accelerant */
267 	accelerantIsClone = 1;
268 
269 	/* call the shared initialization code */
270 	result = init_common(fd);
271 
272 	/* bail out if the common initialization failed */
273 	if (result != B_OK) goto error1;
274 
275 	/* get shared area for display modes */
276 	result = my_mode_list_area = clone_area(
277 		DRIVER_PREFIX " cloned display_modes",
278 		(void **)&my_mode_list,
279 		B_ANY_ADDRESS,
280 		B_READ_AREA,
281 		si->mode_area
282 	);
283 	if (result < B_OK) goto error2;
284 
285 	/* all done */
286 	LOG(4,("CLONE_ACCELERANT: cloning was succesfull.\n"));
287 
288 	result = B_OK;
289 	goto error0;
290 
291 error2:
292 	/* free up the areas we cloned */
293 	uninit_common();
294 error1:
295 	/* close the device we opened */
296 	close(fd);
297 error0:
298 	return result;
299 }
300 
301 void UNINIT_ACCELERANT(void)
302 {
303 	if (accelerantIsClone)
304 	{
305 		LOG(4,("UNINIT_ACCELERANT: shutting down clone accelerant.\n"));
306 	}
307 	else
308 	{
309 		LOG(4,("UNINIT_ACCELERANT: shutting down primary accelerant.\n"));
310 
311 		/* delete benaphores ONLY if we are the primary accelerant */
312 		DELETE_BEN(si->engine.lock);
313 		DELETE_BEN(si->overlay.lock);
314 	}
315 
316 	/* free our mode list area */
317 	delete_area(my_mode_list_area);
318 	/* paranoia */
319 	my_mode_list = 0;
320 	/* release our cloned data */
321 	uninit_common();
322 	/* close the file handle ONLY if we're the clone */
323 	if (accelerantIsClone) close(fd);
324 }
325