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-6/2008.
8*/
9
10#define MODULE_BIT 0x00800000
11
12#include <string.h>
13#include <unistd.h>
14#include "acc_std.h"
15
16static status_t init_common(int the_fd);
17
18/* Initialization code shared between primary and cloned accelerants */
19static 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\n",
42		si->settings.logmask, si->settings.memory, si->settings.hardcursor, si->settings.usebios, si->settings.switchhead));
43	LOG(4,("init_common: dumprom %d, pgm_panel %d\n",
44		si->settings.dumprom, si->settings.pgm_panel));
45	LOG(4,("init_common: force_sync %d, gpu_clk %dMhz, ram_clk %dMhz, force_ws %d\n",
46		si->settings.force_sync, si->settings.gpu_clk, si->settings.ram_clk, si->settings.force_ws));
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
71error1:
72	delete_area(shared_info_area);
73error0:
74	return result;
75}
76
77/* Clean up code shared between primary and cloned accelrants */
78static 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/*
90Initialize 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).
92We need to determine if the kernel driver and the accelerant are compatible.
93If they are, get the accelerant ready to handle other hook functions and
94report success or failure.
95*/
96status_t INIT_ACCELERANT(int the_fd)
97{
98	status_t result;
99	int pointer_reservation; //mem reserved for pointer
100
101	if (0) {
102		time_t now = time (NULL);
103		// LOG not available from here to next LOG: NULL si
104		MSG(("INIT_ACCELERANT: %s", ctime (&now)));
105	}
106
107	/* note that we're the primary accelerant (accelerantIsClone is global) */
108	accelerantIsClone = 0;
109
110	/* do the initialization common to both the primary and the clones */
111	result = init_common(the_fd);
112
113	/* bail out if the common initialization failed */
114	if (result != B_OK) goto error0;
115	// LOG now available: !NULL si
116
117	/* ensure that INIT_ACCELERANT is executed just once (copies should be clones) */
118	if (si->accelerant_in_use)
119	{
120		result = B_NOT_ALLOWED;
121		goto error1;
122	}
123
124	/* call the device specific init code */
125	result = nv_general_powerup();
126
127	/* bail out if it failed */
128	if (result != B_OK) goto error1;
129
130	/*
131	Now would be a good time to figure out what video modes your card supports.
132	We'll place the list of modes in another shared area so all of the copies
133	of the driver can see them.  The primary copy of the accelerant (ie the one
134	initialized with this routine) will own the "one true copy" of the list.
135	Everybody else get's a read-only clone.
136	*/
137	result = create_mode_list();
138	if (result != B_OK)
139	{
140		goto error1;
141	}
142
143	/*
144	Put the cursor at the start of the frame buffer.
145	Nvidia cursor is 32x32 16 color? takes up 4096 bytes of RAM.
146	*/
147	/* Initialize the rest of the cursor information while we're here */
148	si->cursor.width = 16;
149	si->cursor.height = 16;
150	si->cursor.hot_x = 0;
151	si->cursor.hot_y = 0;
152	si->cursor.x = 0;
153	si->cursor.y = 0;
154	si->cursor.dh_right = false;
155
156	/*
157	Put the frame buffer immediately following the cursor data. We store this
158	info in a frame_buffer_config structure to make it convienient to return
159	to the app_server later.
160	*/
161	pointer_reservation = 0;
162	/* Nvidia hardcursor needs 2kB space */
163	if (si->settings.hardcursor) pointer_reservation = 2048;
164
165	si->fbc.frame_buffer = (void *)((char *)si->framebuffer+pointer_reservation);
166	si->fbc.frame_buffer_dma = (void *)((char *)si->framebuffer_pci+pointer_reservation);
167
168	/* count of issued parameters or commands */
169	si->engine.last_idle = si->engine.count = 0;
170	/* no 3D clones are currently loaded */
171	si->engine.threeD.clones = 0;
172	/* tell 3D add-ons that they should reload their rendering states and surfaces */
173	si->engine.threeD.reload = 0xffffffff;
174	INIT_BEN(si->engine.lock);
175
176	/* bail out if something failed */
177	if (result != B_OK) goto error1;
178
179	/* initialise various cursor stuff */
180	head1_cursor_init();
181	if (si->ps.secondary_head) head2_cursor_init();
182
183	/* ensure cursor state */
184	head1_cursor_hide();
185	if (si->ps.secondary_head) head2_cursor_hide();
186
187	/* ensure DPMS state */
188	si->dpms_flags = B_DPMS_ON;
189
190	/* make sure a possible 3D add-on will block rendering and re-initialize itself.
191	 * note: update in _this_ order only */
192	/* SET_DISPLAY_MODE will reset this flag when it's done. */
193	si->engine.threeD.mode_changing = true;
194	/* every 3D add-on will reset this bit-flag when it's done. */
195	si->engine.threeD.newmode = 0xffffffff;
196
197	/* a winner! */
198	result = B_OK;
199	/* ensure that INIT_ACCELERANT won't be executed again (copies should be clones) */
200	si->accelerant_in_use = true;
201	goto error0;
202
203error1:
204	/*
205	Initialization failed after init_common() succeeded, so we need to clean
206	up before quiting.
207	*/
208	uninit_common();
209
210error0:
211	return result;
212}
213
214/*
215Return the number of bytes required to hold the information required
216to clone the device.
217*/
218ssize_t ACCELERANT_CLONE_INFO_SIZE(void) {
219	/*
220	Since we're passing the name of the device as the only required
221	info, return the size of the name buffer
222	*/
223	return B_OS_NAME_LENGTH; // apsed, was MAX_NV_DEVICE_NAME_LENGTH;
224}
225
226
227/*
228Return the info required to clone the device.  void *data points to
229a buffer at least ACCELERANT_CLONE_INFO_SIZE() bytes in length.
230*/
231void GET_ACCELERANT_CLONE_INFO(void *data) {
232	nv_device_name dn;
233	status_t result;
234
235	/* call the kernel driver to get the device name */
236	dn.magic = NV_PRIVATE_DATA_MAGIC;
237	/* store the returned info directly into the passed buffer */
238	dn.name = (char *)data;
239	result = ioctl(fd, NV_DEVICE_NAME, &dn, sizeof(dn));
240}
241
242/*
243Initialize a copy of the accelerant as a clone.  void *data points to
244a copy of the data returned by GET_ACCELERANT_CLONE_INFO().
245*/
246status_t CLONE_ACCELERANT(void *data)
247{
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	/* ensure that INIT_ACCELERANT is executed first (i.e. primary accelerant exists) */
283	if (!(si->accelerant_in_use))
284	{
285		result = B_NOT_ALLOWED;
286		goto error2;
287	}
288
289	/* setup CRTC and DAC functions access */
290	//fixme: setup_virtualized_heads is a problem for clones: needs to be run
291	//for each clone if the mode is changed!
292	if (si->ps.secondary_head)
293		setup_virtualized_heads(si->crtc_switch_mode);
294	else
295		setup_virtualized_heads(si->ps.crtc2_prim);
296
297	/* get shared area for display modes */
298	result = my_mode_list_area = clone_area(
299		DRIVER_PREFIX " cloned display_modes",
300		(void **)&my_mode_list,
301		B_ANY_ADDRESS,
302		B_READ_AREA,
303		si->mode_area
304	);
305	if (result < B_OK) goto error2;
306
307	/* all done */
308	LOG(4,("CLONE_ACCELERANT: cloning was succesfull.\n"));
309
310	result = B_OK;
311	goto error0;
312
313error2:
314	/* free up the areas we cloned */
315	uninit_common();
316error1:
317	/* close the device we opened */
318	close(fd);
319error0:
320	return result;
321}
322
323void UNINIT_ACCELERANT(void)
324{
325	if (accelerantIsClone)
326	{
327		LOG(4,("UNINIT_ACCELERANT: shutting down clone accelerant.\n"));
328	}
329	else
330	{
331		LOG(4,("UNINIT_ACCELERANT: shutting down primary accelerant.\n"));
332
333		/* delete benaphores ONLY if we are the primary accelerant */
334		DELETE_BEN(si->engine.lock);
335
336		/* ensure that INIT_ACCELERANT can be executed again */
337		si->accelerant_in_use = false;
338	}
339
340	/* free our mode list area */
341	delete_area(my_mode_list_area);
342	/* paranoia */
343	my_mode_list = 0;
344	/* release our cloned data */
345	uninit_common();
346	/* close the file handle ONLY if we're the clone */
347	if (accelerantIsClone) close(fd);
348}
349