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