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