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