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