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 3/2002-4/2006.
8*/
9
10/* standard kernel driver stuff */
11#include <KernelExport.h>
12#include <ISA.h>
13#include <PCI.h>
14#include <OS.h>
15#include <directories.h>
16#include <driver_settings.h>
17#include <malloc.h>
18#include <stdlib.h> // for strtoXX
19#include "AGP.h"
20
21/* this is for the standardized portion of the driver API */
22/* currently only one operation is defined: B_GET_ACCELERANT_SIGNATURE */
23#include <graphic_driver.h>
24
25/* this is for sprintf() */
26#include <stdio.h>
27
28/* this is for string compares */
29#include <string.h>
30
31/* The private interface between the accelerant and the kernel driver. */
32#include "DriverInterface.h"
33#include "macros.h"
34
35#define get_pci(o, s) (*pci_bus->read_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s))
36#define set_pci(o, s, v) (*pci_bus->write_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s), (v))
37
38#define MAX_DEVICES	  8
39
40#define DEVICE_FORMAT "%04x_%04x_%02x%02x%02x" // apsed
41
42/* Tell the kernel what revision of the driver API we support */
43int32	api_version = B_CUR_DRIVER_API_VERSION; // apsed, was 2, is 2 in R5
44
45/* these structures are private to the kernel driver */
46typedef struct device_info device_info;
47
48typedef struct {
49	timer		te;				/* timer entry for add_timer() */
50	device_info	*di;			/* pointer to the owning device */
51	bigtime_t	when_target;	/* when we're supposed to wake up */
52} timer_info;
53
54struct device_info {
55	uint32		is_open;			/* a count of how many times the devices has been opened */
56	area_id		shared_area;		/* the area shared between the driver and all of the accelerants */
57	shared_info	*si;				/* a pointer to the shared area, for convenience */
58	vuint32		*regs;				/* kernel's pointer to memory mapped registers */
59	pci_info	pcii;					/* a convenience copy of the pci info for this device */
60	char		name[B_OS_NAME_LENGTH];	/* where we keep the name of the device for publishing and comparing */
61};
62
63typedef struct {
64	uint32		count;				/* number of devices actually found */
65	benaphore	kernel;				/* for serializing opens/closes */
66	char		*device_names[MAX_DEVICES+1];	/* device name pointer storage */
67	device_info	di[MAX_DEVICES];	/* device specific stuff */
68} DeviceData;
69
70/* prototypes for our private functions */
71static status_t open_hook (const char* name, uint32 flags, void** cookie);
72static status_t close_hook (void* dev);
73static status_t free_hook (void* dev);
74static status_t read_hook (void* dev, off_t pos, void* buf, size_t* len);
75static status_t write_hook (void* dev, off_t pos, const void* buf, size_t* len);
76static status_t control_hook (void* dev, uint32 msg, void *buf, size_t len);
77static status_t map_device(device_info *di);
78static void unmap_device(device_info *di);
79static void probe_devices(void);
80static int32 eng_interrupt(void *data);
81
82static DeviceData		*pd;
83static isa_module_info	*isa_bus = NULL;
84static pci_module_info	*pci_bus = NULL;
85static agp_module_info	*agp_bus = NULL;
86static device_hooks graphics_device_hooks = {
87	open_hook,
88	close_hook,
89	free_hook,
90	control_hook,
91	read_hook,
92	write_hook,
93	NULL,
94	NULL,
95	NULL,
96	NULL
97};
98
99#define VENDOR_ID_NVIDIA	0x1106 /* Via */
100
101static uint16 nvidia_device_list[] = {
102	0x3122, /*  */
103	0
104};
105
106static struct {
107	uint16	vendor;
108	uint16	*devices;
109} SupportedDevices[] = {
110//	{VENDOR_ID_NVIDIA, nvidia_device_list},
111	{0x0000, NULL}
112};
113
114static settings current_settings = { // see comments in skel.settings
115	// for driver
116	DRIVER_PREFIX ".accelerant",
117	false,      // dumprom
118	// for accelerant
119	0x00000000, // logmask
120	0,          // memory
121	true,       // usebios
122	true,       // hardcursor
123	false,		// switchhead
124	false,		// force_pci
125	false,		// unhide_fw
126	true,		// pgm_panel
127};
128
129static void dumprom (void *rom, uint32 size)
130{
131	int fd;
132	uint32 cnt;
133
134	fd = open (kUserDirectory "/" DRIVER_PREFIX ".rom",
135		O_WRONLY | O_CREAT, 0666);
136	if (fd < 0) return;
137
138	/* apparantly max. 32kb may be written at once;
139	 * the ROM size is a multiple of that anyway. */
140	for (cnt = 0; (cnt < size); cnt += 32768)
141		write (fd, ((void *)(((uint8 *)rom) + cnt)), 32768);
142	close (fd);
143}
144
145/* return 1 if vblank interrupt has occured */
146static int caused_vbi(vuint32 * regs)
147{
148//	return (ENG_RG32(RG32_CRTC_INTS) & 0x00000001);
149return 0;
150}
151
152/* clear the vblank interrupt */
153static void clear_vbi(vuint32 * regs)
154{
155//	ENG_RG32(RG32_CRTC_INTS) = 0x00000001;
156}
157
158static void enable_vbi(vuint32 * regs)
159{
160	/* clear the vblank interrupt */
161//	ENG_RG32(RG32_CRTC_INTS) = 0x00000001;
162	/* enable nVidia interrupt source vblank */
163//	ENG_RG32(RG32_CRTC_INTE) |= 0x00000001;
164	/* enable nVidia interrupt system hardware (b0-1) */
165//	ENG_RG32(RG32_MAIN_INTE) = 0x00000001;
166}
167
168static void disable_vbi(vuint32 * regs)
169{
170	/* disable nVidia interrupt source vblank */
171//	ENG_RG32(RG32_CRTC_INTE) &= 0xfffffffe;
172	/* clear the vblank interrupt */
173//	ENG_RG32(RG32_CRTC_INTS) = 0x00000001;
174	/* disable nVidia interrupt system hardware (b0-1) */
175//	ENG_RG32(RG32_MAIN_INTE) = 0x00000000;
176}
177
178/*
179	init_hardware() - Returns B_OK if one is
180	found, otherwise returns B_ERROR so the driver will be unloaded.
181*/
182status_t
183init_hardware(void) {
184	long		pci_index = 0;
185	pci_info	pcii;
186	bool		found_one = false;
187
188	/* choke if we can't find the PCI bus */
189	if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK)
190		return B_ERROR;
191
192	/* choke if we can't find the ISA bus */
193	if (get_module(B_ISA_MODULE_NAME, (module_info **)&isa_bus) != B_OK)
194	{
195		put_module(B_PCI_MODULE_NAME);
196		return B_ERROR;
197	}
198
199	/* while there are more pci devices */
200	while ((*pci_bus->get_nth_pci_info)(pci_index, &pcii) == B_NO_ERROR) {
201		int vendor = 0;
202
203		/* if we match a supported vendor */
204		while (SupportedDevices[vendor].vendor) {
205			if (SupportedDevices[vendor].vendor == pcii.vendor_id) {
206				uint16 *devices = SupportedDevices[vendor].devices;
207				/* while there are more supported devices */
208				while (*devices) {
209					/* if we match a supported device */
210					if (*devices == pcii.device_id ) {
211
212						found_one = true;
213						goto done;
214					}
215					/* next supported device */
216					devices++;
217				}
218			}
219			vendor++;
220		}
221		/* next pci_info struct, please */
222		pci_index++;
223	}
224
225done:
226	/* put away the module manager */
227	put_module(B_PCI_MODULE_NAME);
228	return (found_one ? B_OK : B_ERROR);
229}
230
231status_t
232init_driver(void) {
233	void *settings_handle;
234
235	// get driver/accelerant settings, apsed
236	settings_handle  = load_driver_settings (DRIVER_PREFIX ".settings");
237	if (settings_handle != NULL) {
238		const char *item;
239		char       *end;
240		uint32      value;
241
242		// for driver
243		item = get_driver_parameter (settings_handle, "accelerant", "", "");
244		if ((strlen (item) > 0) && (strlen (item) < sizeof (current_settings.accelerant) - 1)) {
245			strcpy (current_settings.accelerant, item);
246		}
247		current_settings.dumprom = get_driver_boolean_parameter (settings_handle, "dumprom", false, false);
248
249		// for accelerant
250		item = get_driver_parameter (settings_handle, "logmask", "0x00000000", "0x00000000");
251		value = strtoul (item, &end, 0);
252		if (*end == '\0') current_settings.logmask = value;
253
254		item = get_driver_parameter (settings_handle, "memory", "0", "0");
255		value = strtoul (item, &end, 0);
256		if (*end == '\0') current_settings.memory = value;
257
258		current_settings.hardcursor = get_driver_boolean_parameter (settings_handle, "hardcursor", false, false);
259		current_settings.usebios = get_driver_boolean_parameter (settings_handle, "usebios", false, false);
260		current_settings.switchhead = get_driver_boolean_parameter (settings_handle, "switchhead", false, false);
261		current_settings.force_pci = get_driver_boolean_parameter (settings_handle, "force_pci", false, false);
262		current_settings.unhide_fw = get_driver_boolean_parameter (settings_handle, "unhide_fw", false, false);
263		current_settings.pgm_panel = get_driver_boolean_parameter (settings_handle, "pgm_panel", false, false);
264
265		unload_driver_settings (settings_handle);
266	}
267
268	/* get a handle for the pci bus */
269	if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK)
270		return B_ERROR;
271
272	/* get a handle for the isa bus */
273	if (get_module(B_ISA_MODULE_NAME, (module_info **)&isa_bus) != B_OK)
274	{
275		put_module(B_PCI_MODULE_NAME);
276		return B_ERROR;
277	}
278
279	/* get a handle for the agp bus if it exists */
280	get_module(B_AGP_MODULE_NAME, (module_info **)&agp_bus);
281
282	/* driver private data */
283	pd = (DeviceData *)calloc(1, sizeof(DeviceData));
284	if (!pd) {
285		put_module(B_PCI_MODULE_NAME);
286		return B_ERROR;
287	}
288	/* initialize the benaphore */
289	INIT_BEN(pd->kernel);
290	/* find all of our supported devices */
291	probe_devices();
292	return B_OK;
293}
294
295const char **
296publish_devices(void) {
297	/* return the list of supported devices */
298	return (const char **)pd->device_names;
299}
300
301device_hooks *
302find_device(const char *name) {
303	int index = 0;
304	while (pd->device_names[index]) {
305		if (strcmp(name, pd->device_names[index]) == 0)
306			return &graphics_device_hooks;
307		index++;
308	}
309	return NULL;
310
311}
312
313void uninit_driver(void) {
314
315	/* free the driver data */
316	DELETE_BEN(pd->kernel);
317	free(pd);
318	pd = NULL;
319
320	/* put the pci module away */
321	put_module(B_PCI_MODULE_NAME);
322	put_module(B_ISA_MODULE_NAME);
323
324	/* put the agp module away if it's there */
325	if (agp_bus) put_module(B_AGP_MODULE_NAME);
326}
327
328static status_t map_device(device_info *di)
329{
330	char buffer[B_OS_NAME_LENGTH]; /*memory for device name*/
331	shared_info *si = di->si;
332	uint32	tmpUlong;
333	pci_info *pcii = &(di->pcii);
334	system_info sysinfo;
335
336	/*storage for the physical to virtual table (used for dma buffer)*/
337//	physical_entry physical_memory[2];
338//	#define G400_DMA_BUFFER_SIZE 1024*1024
339
340	/* variables for making copy of ROM */
341	uint8* rom_temp;
342	area_id rom_area;
343
344	/* Nvidia cards have registers in [0] and framebuffer in [1] */
345	int registers = 1;
346	int frame_buffer = 0;
347//	int pseudo_dma = 2;
348
349	/* enable memory mapped IO, disable VGA I/O - this is defined in the PCI standard */
350	tmpUlong = get_pci(PCI_command, 2);
351	/* enable PCI access */
352	tmpUlong |= PCI_command_memory;
353	/* enable busmastering */
354	tmpUlong |= PCI_command_master;
355	/* disable ISA I/O access */
356	tmpUlong &= ~PCI_command_io;
357	set_pci(PCI_command, 2, tmpUlong);
358
359 	/*work out which version of BeOS is running*/
360 	get_system_info(&sysinfo);
361 	if (0)//sysinfo.kernel_build_date[0]=='J')/*FIXME - better ID version*/
362 	{
363 		si->use_clone_bugfix = 1;
364 	}
365 	else
366 	{
367 		si->use_clone_bugfix = 0;
368 	}
369
370	/* work out a name for the register mapping */
371	sprintf(buffer, DEVICE_FORMAT " regs",
372		di->pcii.vendor_id, di->pcii.device_id,
373		di->pcii.bus, di->pcii.device, di->pcii.function);
374
375	/* get a virtual memory address for the registers*/
376	si->regs_area = map_physical_memory(
377		buffer,
378		/* WARNING: Nvidia needs to map regs as viewed from PCI space! */
379		di->pcii.u.h0.base_registers_pci[registers],
380		di->pcii.u.h0.base_register_sizes[registers],
381		B_ANY_KERNEL_ADDRESS,
382		B_CLONEABLE_AREA | (si->use_clone_bugfix ? B_READ_AREA|B_WRITE_AREA : 0),
383		(void **)&(di->regs));
384 	si->clone_bugfix_regs = (uint32 *) di->regs;
385
386	/* if mapping registers to vmem failed then pass on error */
387	if (si->regs_area < 0) return si->regs_area;
388
389	/* work out a name for the ROM mapping*/
390	sprintf(buffer, DEVICE_FORMAT " rom",
391		di->pcii.vendor_id, di->pcii.device_id,
392		di->pcii.bus, di->pcii.device, di->pcii.function);
393
394	/* disable ROM shadowing, we want the guaranteed exact contents of the chip */
395	/* warning:
396	 * don't touch: (confirmed) NV04, NV05, NV05-M64, NV11 all shutoff otherwise.
397	 * NV18, NV28 and NV34 keep working.
398	 * confirmed NV28 and NV34 to use upper part of shadowed ROM for scratch purposes,
399	 * however the actual ROM content (so the used part) is intact (confirmed). */
400	//set_pci(ENCFG_ROMSHADOW, 4, 0);
401
402	/* get ROM memory mapped base adress - this is defined in the PCI standard */
403	tmpUlong = get_pci(PCI_rom_base, 4);
404	if (tmpUlong)
405	{
406		/* ROM was assigned an adress, so enable ROM decoding - see PCI standard */
407		tmpUlong |= 0x00000001;
408		set_pci(PCI_rom_base, 4, tmpUlong);
409
410		rom_area = map_physical_memory(
411			buffer,
412			di->pcii.u.h0.rom_base_pci,
413			di->pcii.u.h0.rom_size,
414			B_ANY_KERNEL_ADDRESS,
415			B_READ_AREA,
416			(void **)&(rom_temp)
417		);
418
419		/* check if we got the BIOS signature (might fail on laptops..) */
420		if (rom_temp[0]!=0x55 || rom_temp[1]!=0xaa)
421		{
422			/* apparantly no ROM is mapped here */
423			delete_area(rom_area);
424			rom_area = -1;
425			/* force using ISA legacy map as fall-back */
426			tmpUlong = 0x00000000;
427		}
428	}
429
430	if (!tmpUlong)
431	{
432		/* ROM was not assigned an adress, fetch it from ISA legacy memory map! */
433		rom_area = map_physical_memory(
434			buffer,
435			0x000c0000,
436			65536,
437			B_ANY_KERNEL_ADDRESS,
438			B_READ_AREA,
439			(void **)&(rom_temp)
440		);
441	}
442
443	/* if mapping ROM to vmem failed then clean up and pass on error */
444	if (rom_area < 0) {
445		delete_area(si->regs_area);
446		si->regs_area = -1;
447		return rom_area;
448	}
449
450	/* dump ROM to file if selected in skel.settings
451	 * (ROM always fits in 64Kb: checked TNT1 - FX5950) */
452	if (current_settings.dumprom) dumprom (rom_temp, 65536);
453	/* make a copy of ROM for future reference */
454	memcpy (si->rom_mirror, rom_temp, 65536);
455
456	/* disable ROM decoding - this is defined in the PCI standard, and delete the area */
457	tmpUlong = get_pci(PCI_rom_base, 4);
458	tmpUlong &= 0xfffffffe;
459	set_pci(PCI_rom_base, 4, tmpUlong);
460	delete_area(rom_area);
461
462	/* work out a name for the framebuffer mapping*/
463	sprintf(buffer, DEVICE_FORMAT " framebuffer",
464		di->pcii.vendor_id, di->pcii.device_id,
465		di->pcii.bus, di->pcii.device, di->pcii.function);
466
467	/* map the framebuffer into vmem, using Write Combining*/
468	si->fb_area = map_physical_memory(
469		buffer,
470		/* WARNING: Nvidia needs to map framebuffer as viewed from PCI space! */
471		di->pcii.u.h0.base_registers_pci[frame_buffer],
472		di->pcii.u.h0.base_register_sizes[frame_buffer],
473		B_ANY_KERNEL_BLOCK_ADDRESS | B_MTR_WC,
474		B_READ_AREA | B_WRITE_AREA | B_CLONEABLE_AREA,
475		&(si->framebuffer));
476
477	/*if failed with write combining try again without*/
478	if (si->fb_area < 0) {
479		si->fb_area = map_physical_memory(
480			buffer,
481			/* WARNING: Nvidia needs to map framebuffer as viewed from PCI space! */
482			di->pcii.u.h0.base_registers_pci[frame_buffer],
483			di->pcii.u.h0.base_register_sizes[frame_buffer],
484			B_ANY_KERNEL_BLOCK_ADDRESS,
485			B_READ_AREA | B_WRITE_AREA | B_CLONEABLE_AREA,
486			&(si->framebuffer));
487	}
488
489	/* if there was an error, delete our other areas and pass on error*/
490	if (si->fb_area < 0)
491	{
492		delete_area(si->regs_area);
493		si->regs_area = -1;
494		return si->fb_area;
495	}
496//fixme: retest for card coldstart and PCI/virt_mem mapping!!
497	/* remember the DMA address of the frame buffer for BDirectWindow?? purposes */
498	si->framebuffer_pci = (void *) di->pcii.u.h0.base_registers_pci[frame_buffer];
499
500	// remember settings for use here and in accelerant
501	si->settings = current_settings;
502
503	/* in any case, return the result */
504	return si->fb_area;
505}
506
507static void unmap_device(device_info *di) {
508	shared_info *si = di->si;
509	uint32	tmpUlong;
510	pci_info *pcii = &(di->pcii);
511
512	/* disable memory mapped IO */
513	tmpUlong = get_pci(PCI_command, 4);
514	tmpUlong &= 0xfffffffc;
515	set_pci(PCI_command, 4, tmpUlong);
516	/* delete the areas */
517	if (si->regs_area >= 0) delete_area(si->regs_area);
518	if (si->fb_area >= 0) delete_area(si->fb_area);
519	si->regs_area = si->fb_area = -1;
520	si->framebuffer = NULL;
521	di->regs = NULL;
522}
523
524static void probe_devices(void) {
525	uint32 pci_index = 0;
526	uint32 count = 0;
527	device_info *di = pd->di;
528
529	/* while there are more pci devices */
530	while ((count < MAX_DEVICES) && ((*pci_bus->get_nth_pci_info)(pci_index, &(di->pcii)) == B_NO_ERROR)) {
531		int vendor = 0;
532
533		/* if we match a supported vendor */
534		while (SupportedDevices[vendor].vendor) {
535			if (SupportedDevices[vendor].vendor == di->pcii.vendor_id) {
536				uint16 *devices = SupportedDevices[vendor].devices;
537				/* while there are more supported devices */
538				while (*devices) {
539					/* if we match a supported device */
540					if (*devices == di->pcii.device_id ) {
541						/* publish the device name */
542						sprintf(di->name, "graphics/" DEVICE_FORMAT,
543							di->pcii.vendor_id, di->pcii.device_id,
544							di->pcii.bus, di->pcii.device, di->pcii.function);
545
546						/* remember the name */
547						pd->device_names[count] = di->name;
548						/* mark the driver as available for R/W open */
549						di->is_open = 0;
550						/* mark areas as not yet created */
551						di->shared_area = -1;
552						/* mark pointer to shared data as invalid */
553						di->si = NULL;
554						/* inc pointer to device info */
555						di++;
556						/* inc count */
557						count++;
558						/* break out of these while loops */
559						goto next_device;
560					}
561					/* next supported device */
562					devices++;
563				}
564			}
565			vendor++;
566		}
567next_device:
568		/* next pci_info struct, please */
569		pci_index++;
570	}
571	/* propagate count */
572	pd->count = count;
573	/* terminate list of device names with a null pointer */
574	pd->device_names[pd->count] = NULL;
575}
576
577static uint32 thread_interrupt_work(int32 *flags, vuint32 *regs, shared_info *si) {
578	uint32 handled = B_HANDLED_INTERRUPT;
579	/* release the vblank semaphore */
580	if (si->vblank >= 0) {
581		int32 blocked;
582		if ((get_sem_count(si->vblank, &blocked) == B_OK) && (blocked < 0)) {
583			release_sem_etc(si->vblank, -blocked, B_DO_NOT_RESCHEDULE);
584			handled = B_INVOKE_SCHEDULER;
585		}
586	}
587	return handled;
588}
589
590static int32
591eng_interrupt(void *data)
592{
593	int32 handled = B_UNHANDLED_INTERRUPT;
594	device_info *di = (device_info *)data;
595	shared_info *si = di->si;
596	int32 *flags = &(si->flags);
597	vuint32 *regs;
598
599	/* is someone already handling an interrupt for this device? */
600	if (atomic_or(flags, SKD_HANDLER_INSTALLED) & SKD_HANDLER_INSTALLED) {
601		goto exit0;
602	}
603	/* get regs */
604	regs = di->regs;
605
606	/* was it a VBI? */
607	if (caused_vbi(regs)) {
608		/*clear the interrupt*/
609		clear_vbi(regs);
610		/*release the semaphore*/
611		handled = thread_interrupt_work(flags, regs, si);
612	}
613
614	/* note that we're not in the handler any more */
615	atomic_and(flags, ~SKD_HANDLER_INSTALLED);
616
617exit0:
618	return handled;
619}
620
621static status_t open_hook (const char* name, uint32 flags, void** cookie) {
622	int32 index = 0;
623	device_info *di;
624	shared_info *si;
625	thread_id	thid;
626	thread_info	thinfo;
627	status_t	result = B_OK;
628	vuint32		*regs;
629	char shared_name[B_OS_NAME_LENGTH];
630
631	/* find the device name in the list of devices */
632	/* we're never passed a name we didn't publish */
633	while (pd->device_names[index] && (strcmp(name, pd->device_names[index]) != 0)) index++;
634
635	/* for convienience */
636	di = &(pd->di[index]);
637
638	/* make sure no one else has write access to the common data */
639	AQUIRE_BEN(pd->kernel);
640
641	/* if it's already open for writing */
642	if (di->is_open) {
643		/* mark it open another time */
644		goto mark_as_open;
645	}
646	/* create the shared area */
647	sprintf(shared_name, DEVICE_FORMAT " shared",
648		di->pcii.vendor_id, di->pcii.device_id,
649		di->pcii.bus, di->pcii.device, di->pcii.function);
650	/* create this area with NO user-space read or write permissions, to prevent accidental dammage */
651	di->shared_area = create_area(shared_name, (void **)&(di->si), B_ANY_KERNEL_ADDRESS, ((sizeof(shared_info) + (B_PAGE_SIZE - 1)) & ~(B_PAGE_SIZE - 1)), B_FULL_LOCK,
652		B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA | B_CLONEABLE_AREA);
653	if (di->shared_area < 0) {
654		/* return the error */
655		result = di->shared_area;
656		goto done;
657	}
658
659	/* save a few dereferences */
660	si = di->si;
661
662	/* save the vendor and device IDs */
663	si->vendor_id = di->pcii.vendor_id;
664	si->device_id = di->pcii.device_id;
665	si->revision = di->pcii.revision;
666	si->bus = di->pcii.bus;
667	si->device = di->pcii.device;
668	si->function = di->pcii.function;
669
670	/* note the amount of system RAM the system BIOS assigned to the card if applicable:
671	 * unified memory architecture (UMA) */
672	switch ((((uint32)(si->device_id)) << 16) | si->vendor_id)
673	{
674	case 0x01a010de: /* Nvidia GeForce2 Integrated GPU */
675		/* device at bus #0, device #0, function #1 holds value at byte-index 0x7C */
676		si->ps.memory_size = 1024 * 1024 *
677			(((((*pci_bus->read_pci_config)(0, 0, 1, 0x7c, 4)) & 0x000007c0) >> 6) + 1);
678		/* last 64kB RAM is used for the BIOS (or something else?) */
679		si->ps.memory_size -= (64 * 1024);
680		break;
681	case 0x01f010de: /* Nvidia GeForce4 MX Integrated GPU */
682		/* device at bus #0, device #0, function #1 holds value at byte-index 0x84 */
683		si->ps.memory_size = 1024 * 1024 *
684			(((((*pci_bus->read_pci_config)(0, 0, 1, 0x84, 4)) & 0x000007f0) >> 4) + 1);
685		/* last 64kB RAM is used for the BIOS (or something else?) */
686		si->ps.memory_size -= (64 * 1024);
687		break;
688	default:
689		/* all other cards have own RAM: the amount of which is determined in the
690		 * accelerant. */
691		break;
692	}
693
694	/* map the device */
695	result = map_device(di);
696	if (result < 0) goto free_shared;
697	result = B_OK;
698
699	/* create a semaphore for vertical blank management */
700	si->vblank = create_sem(0, di->name);
701	if (si->vblank < 0) {
702		result = si->vblank;
703		goto unmap;
704	}
705
706	/* change the owner of the semaphores to the opener's team */
707	/* this is required because apps can't aquire kernel semaphores */
708	thid = find_thread(NULL);
709	get_thread_info(thid, &thinfo);
710	set_sem_owner(si->vblank, thinfo.team);
711
712	/* assign local regs pointer for SAMPLExx() macros */
713	regs = di->regs;
714
715	/* disable and clear any pending interrupts */
716	disable_vbi(regs);
717
718	/* If there is a valid interrupt line assigned then set up interrupts */
719	if ((di->pcii.u.h0.interrupt_pin == 0x00) ||
720	    (di->pcii.u.h0.interrupt_line == 0xff) || /* no IRQ assigned */
721	    (di->pcii.u.h0.interrupt_line <= 0x02))   /* system IRQ assigned */
722	{
723		/* we are aborting! */
724		/* Note: the R4 graphics driver kit lacks this statement!! */
725		result = B_ERROR;
726		/* interrupt does not exist so exit without installing our handler */
727		goto delete_the_sem;
728	}
729	else
730	{
731		/* otherwise install our interrupt handler */
732		result = install_io_interrupt_handler(di->pcii.u.h0.interrupt_line, eng_interrupt, (void *)di, 0);
733		/* bail if we couldn't install the handler */
734		if (result != B_OK) goto delete_the_sem;
735	}
736
737mark_as_open:
738	/* mark the device open */
739	di->is_open++;
740
741	/* send the cookie to the opener */
742	*cookie = di;
743
744	goto done;
745
746
747delete_the_sem:
748	delete_sem(si->vblank);
749
750unmap:
751	unmap_device(di);
752
753free_shared:
754	/* clean up our shared area */
755	delete_area(di->shared_area);
756	di->shared_area = -1;
757	di->si = NULL;
758
759done:
760	/* end of critical section */
761	RELEASE_BEN(pd->kernel);
762
763	/* all done, return the status */
764	return result;
765}
766
767/* ----------
768	read_hook - does nothing, gracefully
769----- */
770static status_t
771read_hook (void* dev, off_t pos, void* buf, size_t* len)
772{
773	*len = 0;
774	return B_NOT_ALLOWED;
775}
776
777/* ----------
778	write_hook - does nothing, gracefully
779----- */
780static status_t
781write_hook (void* dev, off_t pos, const void* buf, size_t* len)
782{
783	*len = 0;
784	return B_NOT_ALLOWED;
785}
786
787/* ----------
788	close_hook - does nothing, gracefully
789----- */
790static status_t
791close_hook (void* dev)
792{
793	/* we don't do anything on close: there might be dup'd fd */
794	return B_NO_ERROR;
795}
796
797/* -----------
798	free_hook - close down the device
799----------- */
800static status_t
801free_hook (void* dev) {
802	device_info *di = (device_info *)dev;
803	shared_info	*si = di->si;
804	vuint32 *regs = di->regs;
805
806	/* lock the driver */
807	AQUIRE_BEN(pd->kernel);
808
809	/* if opened multiple times, decrement the open count and exit */
810	if (di->is_open > 1)
811		goto unlock_and_exit;
812
813	/* disable and clear any pending interrupts */
814	disable_vbi(regs);
815
816	/* remove interrupt handler */
817	remove_io_interrupt_handler(di->pcii.u.h0.interrupt_line, eng_interrupt, di);
818
819	/* delete the semaphores, ignoring any errors ('cause the owning team may have died on us) */
820	delete_sem(si->vblank);
821	si->vblank = -1;
822
823	/* free regs and framebuffer areas */
824	unmap_device(di);
825
826	/* clean up our shared area */
827	delete_area(di->shared_area);
828	di->shared_area = -1;
829	di->si = NULL;
830
831unlock_and_exit:
832	/* mark the device available */
833	di->is_open--;
834	/* unlock the driver */
835	RELEASE_BEN(pd->kernel);
836	/* all done */
837	return B_OK;
838}
839
840/* -----------
841	control_hook - where the real work is done
842----------- */
843static status_t
844control_hook (void* dev, uint32 msg, void *buf, size_t len) {
845	device_info *di = (device_info *)dev;
846	status_t result = B_DEV_INVALID_IOCTL;
847	uint32 tmpUlong;
848
849	switch (msg) {
850		/* the only PUBLIC ioctl */
851		case B_GET_ACCELERANT_SIGNATURE: {
852			char *sig = (char *)buf;
853			strcpy(sig, current_settings.accelerant);
854			result = B_OK;
855		} break;
856
857		/* PRIVATE ioctl from here on */
858		case ENG_GET_PRIVATE_DATA: {
859			eng_get_private_data *gpd = (eng_get_private_data *)buf;
860			if (gpd->magic == SKEL_PRIVATE_DATA_MAGIC) {
861				gpd->shared_info_area = di->shared_area;
862				result = B_OK;
863			}
864		} break;
865		case ENG_GET_PCI: {
866			eng_get_set_pci *gsp = (eng_get_set_pci *)buf;
867			if (gsp->magic == SKEL_PRIVATE_DATA_MAGIC) {
868				pci_info *pcii = &(di->pcii);
869				gsp->value = get_pci(gsp->offset, gsp->size);
870				result = B_OK;
871			}
872		} break;
873		case ENG_SET_PCI: {
874			eng_get_set_pci *gsp = (eng_get_set_pci *)buf;
875			if (gsp->magic == SKEL_PRIVATE_DATA_MAGIC) {
876				pci_info *pcii = &(di->pcii);
877				set_pci(gsp->offset, gsp->size, gsp->value);
878				result = B_OK;
879			}
880		} break;
881		case ENG_DEVICE_NAME: { // apsed
882			eng_device_name *dn = (eng_device_name *)buf;
883			if (dn->magic == SKEL_PRIVATE_DATA_MAGIC) {
884				strcpy(dn->name, di->name);
885				result = B_OK;
886			}
887		} break;
888		case ENG_RUN_INTERRUPTS: {
889			eng_set_bool_state *ri = (eng_set_bool_state *)buf;
890			if (ri->magic == SKEL_PRIVATE_DATA_MAGIC) {
891				vuint32 *regs = di->regs;
892				if (ri->do_it) {
893					enable_vbi(regs);
894				} else {
895					disable_vbi(regs);
896				}
897				result = B_OK;
898			}
899		} break;
900		case ENG_GET_NTH_AGP_INFO: {
901			eng_nth_agp_info *nai = (eng_nth_agp_info *)buf;
902			if (nai->magic == SKEL_PRIVATE_DATA_MAGIC) {
903				nai->exist = false;
904				nai->agp_bus = false;
905				if (agp_bus) {
906					nai->agp_bus = true;
907					if ((*agp_bus->get_nth_agp_info)(nai->index, &(nai->agpi)) == B_NO_ERROR) {
908						nai->exist = true;
909					}
910				}
911				result = B_OK;
912			}
913		} break;
914		case ENG_ENABLE_AGP: {
915			eng_cmd_agp *nca = (eng_cmd_agp *)buf;
916			if (nca->magic == SKEL_PRIVATE_DATA_MAGIC) {
917				if (agp_bus) {
918					nca->agp_bus = true;
919					(*agp_bus->enable_agp)(&(nca->cmd));
920				} else {
921					nca->agp_bus = false;
922					nca->cmd = 0;
923				}
924				result = B_OK;
925			}
926		} break;
927		case ENG_ISA_OUT: {
928			eng_in_out_isa *io_isa = (eng_in_out_isa *)buf;
929			if (io_isa->magic == SKEL_PRIVATE_DATA_MAGIC) {
930				pci_info *pcii = &(di->pcii);
931
932				/* lock the driver:
933				 * no other graphics card may have ISA I/O enabled when we enter */
934				AQUIRE_BEN(pd->kernel);
935
936				/* enable ISA I/O access */
937				tmpUlong = get_pci(PCI_command, 2);
938				tmpUlong |= PCI_command_io;
939				set_pci(PCI_command, 2, tmpUlong);
940
941				if (io_isa->size == 1)
942  					isa_bus->write_io_8(io_isa->adress, (uint8)io_isa->data);
943   				else
944   					isa_bus->write_io_16(io_isa->adress, io_isa->data);
945  				result = B_OK;
946
947				/* disable ISA I/O access */
948				tmpUlong = get_pci(PCI_command, 2);
949				tmpUlong &= ~PCI_command_io;
950				set_pci(PCI_command, 2, tmpUlong);
951
952				/* end of critical section */
953				RELEASE_BEN(pd->kernel);
954   			}
955		} break;
956		case ENG_ISA_IN: {
957			eng_in_out_isa *io_isa = (eng_in_out_isa *)buf;
958			if (io_isa->magic == SKEL_PRIVATE_DATA_MAGIC) {
959				pci_info *pcii = &(di->pcii);
960
961				/* lock the driver:
962				 * no other graphics card may have ISA I/O enabled when we enter */
963				AQUIRE_BEN(pd->kernel);
964
965				/* enable ISA I/O access */
966				tmpUlong = get_pci(PCI_command, 2);
967				tmpUlong |= PCI_command_io;
968				set_pci(PCI_command, 2, tmpUlong);
969
970				if (io_isa->size == 1)
971	   				io_isa->data = isa_bus->read_io_8(io_isa->adress);
972	   			else
973	   				io_isa->data = isa_bus->read_io_16(io_isa->adress);
974   				result = B_OK;
975
976				/* disable ISA I/O access */
977				tmpUlong = get_pci(PCI_command, 2);
978				tmpUlong &= ~PCI_command_io;
979				set_pci(PCI_command, 2, tmpUlong);
980
981				/* end of critical section */
982				RELEASE_BEN(pd->kernel);
983   			}
984		} break;
985	}
986	return result;
987}
988