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