tcp: Add APICall trace entry and move TRACEs into locked parts.
[haiku.git] / src / add-ons / accelerants / nvidia_gpgpu / InitAccelerant.c
blob4b906a833e40872635aa60c69e96b5f083479419
1 /*
2 Copyright 1999, Be Incorporated. All Rights Reserved.
3 This file may be used under the terms of the Be Sample Code License.
5 Other authors:
6 Mark Watson,
7 Rudolf Cornelissen 10/2002-6/2008.
8 */
10 #define MODULE_BIT 0x00800000
12 #include <string.h>
13 #include <unistd.h>
14 #include "acc_std.h"
16 static status_t init_common(int the_fd);
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;
23 // LOG not available from here to next LOG: NULL si
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;
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;
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));
48 /*Check for R4.5.0 and if it is running, use work around*/
50 if (si->use_clone_bugfix)
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;
56 else
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;
68 /* all done */
69 goto error0;
71 error1:
72 delete_area(shared_info_area);
73 error0:
74 return result;
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;
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.
96 status_t INIT_ACCELERANT(int the_fd)
98 status_t result;
99 int pointer_reservation; //mem reserved for pointer
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)));
107 /* note that we're the primary accelerant (accelerantIsClone is global) */
108 accelerantIsClone = 0;
110 /* do the initialization common to both the primary and the clones */
111 result = init_common(the_fd);
113 /* bail out if the common initialization failed */
114 if (result != B_OK) goto error0;
115 // LOG now available: !NULL si
117 /* ensure that INIT_ACCELERANT is executed just once (copies should be clones) */
118 if (si->accelerant_in_use)
120 result = B_NOT_ALLOWED;
121 goto error1;
124 /* call the device specific init code */
125 result = nv_general_powerup();
127 /* bail out if it failed */
128 if (result != B_OK) goto error1;
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.
137 result = create_mode_list();
138 if (result != B_OK)
140 goto error1;
144 Put the cursor at the start of the frame buffer.
145 Nvidia cursor is 32x32 16 color? takes up 4096 bytes of RAM.
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;
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.
161 pointer_reservation = 0;
162 /* Nvidia hardcursor needs 2kB space */
163 if (si->settings.hardcursor) pointer_reservation = 2048;
165 si->fbc.frame_buffer = (void *)((char *)si->framebuffer+pointer_reservation);
166 si->fbc.frame_buffer_dma = (void *)((char *)si->framebuffer_pci+pointer_reservation);
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);
176 /* bail out if something failed */
177 if (result != B_OK) goto error1;
179 /* initialise various cursor stuff */
180 head1_cursor_init();
181 if (si->ps.secondary_head) head2_cursor_init();
183 /* ensure cursor state */
184 head1_cursor_hide();
185 if (si->ps.secondary_head) head2_cursor_hide();
187 /* ensure DPMS state */
188 si->dpms_flags = B_DPMS_ON;
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;
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;
203 error1:
205 Initialization failed after init_common() succeeded, so we need to clean
206 up before quiting.
208 uninit_common();
210 error0:
211 return result;
215 Return the number of bytes required to hold the information required
216 to clone the device.
218 ssize_t ACCELERANT_CLONE_INFO_SIZE(void) {
220 Since we're passing the name of the device as the only required
221 info, return the size of the name buffer
223 return B_OS_NAME_LENGTH; // apsed, was MAX_NV_DEVICE_NAME_LENGTH;
228 Return the info required to clone the device. void *data points to
229 a buffer at least ACCELERANT_CLONE_INFO_SIZE() bytes in length.
231 void GET_ACCELERANT_CLONE_INFO(void *data) {
232 nv_device_name dn;
233 status_t result;
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));
243 Initialize a copy of the accelerant as a clone. void *data points to
244 a copy of the data returned by GET_ACCELERANT_CLONE_INFO().
246 status_t CLONE_ACCELERANT(void *data)
248 status_t result;
249 char path[MAXPATHLEN];
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)
259 /* we can't use LOG because we didn't get the shared_info struct.. */
260 char fname[64];
261 FILE *myhand = NULL;
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);
268 /* abort with resultcode from open attempt on kerneldriver */
269 result = fd;
270 goto error0;
273 /* note that we're a clone accelerant */
274 accelerantIsClone = 1;
276 /* call the shared initialization code */
277 result = init_common(fd);
279 /* bail out if the common initialization failed */
280 if (result != B_OK) goto error1;
282 /* ensure that INIT_ACCELERANT is executed first (i.e. primary accelerant exists) */
283 if (!(si->accelerant_in_use))
285 result = B_NOT_ALLOWED;
286 goto error2;
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);
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
305 if (result < B_OK) goto error2;
307 /* all done */
308 LOG(4,("CLONE_ACCELERANT: cloning was succesfull.\n"));
310 result = B_OK;
311 goto error0;
313 error2:
314 /* free up the areas we cloned */
315 uninit_common();
316 error1:
317 /* close the device we opened */
318 close(fd);
319 error0:
320 return result;
323 void UNINIT_ACCELERANT(void)
325 if (accelerantIsClone)
327 LOG(4,("UNINIT_ACCELERANT: shutting down clone accelerant.\n"));
329 else
331 LOG(4,("UNINIT_ACCELERANT: shutting down primary accelerant.\n"));
333 /* delete benaphores ONLY if we are the primary accelerant */
334 DELETE_BEN(si->engine.lock);
336 /* ensure that INIT_ACCELERANT can be executed again */
337 si->accelerant_in_use = false;
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);