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-3/2009.
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, 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\n",
46		si->settings.force_sync, si->settings.gpu_clk, si->settings.ram_clk, si->settings.force_ws, si->settings.block_acc));
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	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	/* a winner! */
218	result = B_OK;
219	/* ensure that INIT_ACCELERANT won't be executed again (copies should be clones) */
220	si->accelerant_in_use = true;
221	goto error0;
222
223error1:
224	/*
225	Initialization failed after init_common() succeeded, so we need to clean
226	up before quiting.
227	*/
228	uninit_common();
229
230error0:
231	return result;
232}
233
234/*
235Return the number of bytes required to hold the information required
236to clone the device.
237*/
238ssize_t ACCELERANT_CLONE_INFO_SIZE(void) {
239	/*
240	Since we're passing the name of the device as the only required
241	info, return the size of the name buffer
242	*/
243	return B_OS_NAME_LENGTH; // apsed, was MAX_NV_DEVICE_NAME_LENGTH;
244}
245
246
247/*
248Return the info required to clone the device.  void *data points to
249a buffer at least ACCELERANT_CLONE_INFO_SIZE() bytes in length.
250*/
251void GET_ACCELERANT_CLONE_INFO(void *data) {
252	nv_device_name dn;
253	status_t result;
254
255	/* call the kernel driver to get the device name */
256	dn.magic = NV_PRIVATE_DATA_MAGIC;
257	/* store the returned info directly into the passed buffer */
258	dn.name = (char *)data;
259	result = ioctl(fd, NV_DEVICE_NAME, &dn, sizeof(dn));
260}
261
262/*
263Initialize a copy of the accelerant as a clone.  void *data points to
264a copy of the data returned by GET_ACCELERANT_CLONE_INFO().
265*/
266status_t CLONE_ACCELERANT(void *data)
267{
268	status_t result;
269	char path[MAXPATHLEN];
270
271	/* the data is the device name */
272	/* Note: the R4 graphics driver kit is in error here (missing trailing '/') */
273	strcpy(path, "/dev/");
274	strcat(path, (const char *)data);
275	/* open the device, the permissions aren't important */
276	fd = open(path, B_READ_WRITE);
277	if (fd < 0)
278	{
279		/* we can't use LOG because we didn't get the shared_info struct.. */
280		char     fname[64];
281		FILE    *myhand = NULL;
282
283		sprintf (fname, "/boot/home/" DRIVER_PREFIX ".accelerant.0.log");
284		myhand=fopen(fname,"a+");
285		fprintf(myhand, "CLONE_ACCELERANT: couldn't open kerneldriver %s! Aborting.\n", path);
286		fclose(myhand);
287
288		/* abort with resultcode from open attempt on kerneldriver */
289		result = fd;
290		goto error0;
291	}
292
293	/* note that we're a clone accelerant */
294	accelerantIsClone = 1;
295
296	/* call the shared initialization code */
297	result = init_common(fd);
298
299	/* bail out if the common initialization failed */
300	if (result != B_OK) goto error1;
301
302	/* ensure that INIT_ACCELERANT is executed first (i.e. primary accelerant exists) */
303	if (!(si->accelerant_in_use))
304	{
305		result = B_NOT_ALLOWED;
306		goto error2;
307	}
308
309	/* setup CRTC and DAC functions access */
310	//fixme: setup_virtualized_heads is a problem for clones: needs to be run
311	//for each clone if the mode is changed!
312	if (si->ps.secondary_head)
313		setup_virtualized_heads(si->crtc_switch_mode);
314	else
315		setup_virtualized_heads(si->ps.crtc2_prim);
316
317	/* get shared area for display modes */
318	result = my_mode_list_area = clone_area(
319		DRIVER_PREFIX " cloned display_modes",
320		(void **)&my_mode_list,
321		B_ANY_ADDRESS,
322		B_READ_AREA,
323		si->mode_area
324	);
325	if (result < B_OK) goto error2;
326
327	/* all done */
328	LOG(4,("CLONE_ACCELERANT: cloning was succesfull.\n"));
329
330	result = B_OK;
331	goto error0;
332
333error2:
334	/* free up the areas we cloned */
335	uninit_common();
336error1:
337	/* close the device we opened */
338	close(fd);
339error0:
340	return result;
341}
342
343void UNINIT_ACCELERANT(void)
344{
345	if (accelerantIsClone)
346	{
347		LOG(4,("UNINIT_ACCELERANT: shutting down clone accelerant.\n"));
348	}
349	else
350	{
351		LOG(4,("UNINIT_ACCELERANT: shutting down primary accelerant.\n"));
352
353		/* delete benaphores ONLY if we are the primary accelerant */
354		DELETE_BEN(si->engine.lock);
355		DELETE_BEN(si->overlay.lock);
356
357		/* ensure that INIT_ACCELERANT can be executed again */
358		si->accelerant_in_use = false;
359	}
360
361	/* free our mode list area */
362	delete_area(my_mode_list_area);
363	/* paranoia */
364	my_mode_list = 0;
365	/* release our cloned data */
366	uninit_common();
367	/* close the file handle ONLY if we're the clone */
368	if (accelerantIsClone) close(fd);
369}
370