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