xref: /haiku/src/add-ons/kernel/drivers/graphics/nvidia/driver.c (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
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-1/2016.
8 */
9 
10 
11 #include "AGP.h"
12 #include "DriverInterface.h"
13 #include "nv_macros.h"
14 
15 #include <graphic_driver.h>
16 #include <KernelExport.h>
17 #include <ISA.h>
18 #include <PCI.h>
19 #include <OS.h>
20 #include <directories.h>
21 #include <driver_settings.h>
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #define get_pci(o, s) (*pci_bus->read_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s))
28 #define set_pci(o, s, v) (*pci_bus->write_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s), (v))
29 
30 #define MAX_DEVICES	  8
31 
32 /* Tell the kernel what revision of the driver API we support */
33 int32 api_version = B_CUR_DRIVER_API_VERSION;
34 
35 /* these structures are private to the kernel driver */
36 typedef struct device_info device_info;
37 
38 typedef struct {
39 	timer		te;				/* timer entry for add_timer() */
40 	device_info	*di;			/* pointer to the owning device */
41 	bigtime_t	when_target;	/* when we're supposed to wake up */
42 } timer_info;
43 
44 struct device_info {
45 	uint32		is_open;			/* a count of how many times the devices has been opened */
46 	area_id		shared_area;		/* the area shared between the driver and all of the accelerants */
47 	shared_info	*si;				/* a pointer to the shared area, for convenience */
48 	vuint32		*regs;				/* kernel's pointer to memory mapped registers */
49 	pci_info	pcii;					/* a convenience copy of the pci info for this device */
50 	char		name[B_OS_NAME_LENGTH];	/* where we keep the name of the device for publishing and comparing */
51 };
52 
53 typedef struct {
54 	uint32		count;				/* number of devices actually found */
55 	benaphore	kernel;				/* for serializing opens/closes */
56 	char		*device_names[MAX_DEVICES+1];	/* device name pointer storage */
57 	device_info	di[MAX_DEVICES];	/* device specific stuff */
58 } DeviceData;
59 
60 /* prototypes for our private functions */
61 static status_t open_hook(const char* name, uint32 flags, void** cookie);
62 static status_t close_hook(void* dev);
63 static status_t free_hook(void* dev);
64 static status_t read_hook(void* dev, off_t pos, void* buf, size_t* len);
65 static status_t write_hook(void* dev, off_t pos, const void* buf, size_t* len);
66 static status_t control_hook(void* dev, uint32 msg, void *buf, size_t len);
67 static status_t map_device(device_info *di);
68 static void unmap_device(device_info *di);
69 static void probe_devices(void);
70 static int32 nv_interrupt(void *data);
71 
72 static DeviceData		*pd;
73 static isa_module_info	*isa_bus = NULL;
74 static pci_module_info	*pci_bus = NULL;
75 static agp_gart_module_info *agp_bus = NULL;
76 static device_hooks graphics_device_hooks = {
77 	open_hook,
78 	close_hook,
79 	free_hook,
80 	control_hook,
81 	read_hook,
82 	write_hook,
83 	NULL,
84 	NULL,
85 	NULL,
86 	NULL
87 };
88 
89 #define VENDOR_ID_NVIDIA	0x10de /* Nvidia */
90 #define VENDOR_ID_ELSA		0x1048 /* Elsa GmbH */
91 #define VENDOR_ID_NVSTBSGS	0x12d2 /* Nvidia STB/SGS-Thompson */
92 #define VENDOR_ID_VARISYS	0x1888 /* Varisys Limited */
93 
94 static uint16 nvidia_device_list[] = {
95 	0x0020, /* Nvidia TNT1 */
96 	0x0028, /* Nvidia TNT2 (pro) */
97 	0x0029, /* Nvidia TNT2 Ultra */
98 	0x002a, /* Nvidia TNT2 */
99 	0x002b, /* Nvidia TNT2 */
100 	0x002c, /* Nvidia Vanta (Lt) */
101 	0x002d, /* Nvidia TNT2-M64 (Pro) */
102 	0x002e, /* Nvidia NV06 Vanta */
103 	0x002f, /* Nvidia NV06 Vanta */
104 	0x0040, /* Nvidia Geforce FX 6800 Ultra */
105 	0x0041, /* Nvidia Geforce FX 6800 */
106 	0x0042, /* Nvidia Geforce FX 6800LE */
107 	0x0043, /* Nvidia Geforce 6800 XE */
108 	0x0045, /* Nvidia Geforce FX 6800 GT */
109 	0x0046, /* Nvidia Geforce FX 6800 GT */
110 	0x0047, /* Nvidia Geforce 6800 GS */
111 	0x0048, /* Nvidia Geforce FX 6800 XT */
112 	0x0049, /* Nvidia unknown FX */
113 	0x004d, /* Nvidia Quadro FX 4400 */
114 	0x004e, /* Nvidia Quadro FX 4000 */
115 	0x0091, /* Nvidia Geforce 7800 GTX PCIe */
116 	0x0092, /* Nvidia Geforce 7800 GT PCIe */
117 	0x0098, /* Nvidia Geforce 7800 Go PCIe */
118 	0x0099, /* Nvidia Geforce 7800 GTX Go PCIe */
119 	0x009d, /* Nvidia Quadro FX 4500 */
120 	0x00a0, /* Nvidia Aladdin TNT2 */
121 	0x00c0,	/* Nvidia Geforce 6800 GS */
122 	0x00c1, /* Nvidia Geforce FX 6800 */
123 	0x00c2, /* Nvidia Geforce FX 6800LE */
124 	0x00c3, /* Nvidia Geforce FX 6800 XT */
125 	0x00c8, /* Nvidia Geforce FX 6800 Go */
126 	0x00c9, /* Nvidia Geforce FX 6800 Ultra Go */
127 	0x00cc, /* Nvidia Quadro FX 1400 Go */
128 	0x00cd, /* Nvidia Quadro FX 3450/4000 SDI */
129 	0x00ce, /* Nvidia Quadro FX 1400 */
130 	0x00f0, /* Nvidia Geforce FX 6800 (Ultra) AGP(?) */
131 	0x00f1, /* Nvidia Geforce FX 6600 GT AGP */
132 	0x00f2, /* Nvidia Geforce FX 6600 AGP */
133 	0x00f3, /* Nvidia Geforce 6200 */
134 	0x00f4, /* Nvidia Geforce 6600 LE */
135 	0x00f5, /* Nvidia Geforce FX 7800 GS AGP */
136 	0x00f6, /* Nvidia Geforce 6800 GS */
137 	0x00f8, /* Nvidia Quadro FX 3400/4400 PCIe */
138 	0x00f9,	/* Nvidia Geforce PCX 6800 PCIe */
139 	0x00fa,	/* Nvidia Geforce PCX 5750 PCIe */
140 	0x00fb,	/* Nvidia Geforce PCX 5900 PCIe */
141 	0x00fc, /* Nvidia Geforce PCX 5300 PCIe */
142 	0x00fd,	/* Nvidia Quadro PCX PCIe */
143 	0x00fe,	/* Nvidia Quadro FX 1300 PCIe(?) */
144 	0x00ff, /* Nvidia Geforce PCX 4300 PCIe */
145 	0x0100, /* Nvidia Geforce256 SDR */
146 	0x0101, /* Nvidia Geforce256 DDR */
147 	0x0102, /* Nvidia Geforce256 Ultra */
148 	0x0103, /* Nvidia Quadro */
149 	0x0110, /* Nvidia Geforce2 MX/MX400 */
150 	0x0111, /* Nvidia Geforce2 MX100/MX200 DDR */
151 	0x0112, /* Nvidia Geforce2 Go */
152 	0x0113, /* Nvidia Quadro2 MXR/EX/Go */
153 	0x0140, /* Nvidia Geforce FX 6600 GT */
154 	0x0141, /* Nvidia Geforce FX 6600 */
155 	0x0142, /* Nvidia Geforce FX 6600LE */
156 	0x0143, /* Nvidia Geforce 6600 VE */
157 	0x0144, /* Nvidia Geforce FX 6600 Go */
158 	0x0145, /* Nvidia Geforce FX 6610 XL */
159 	0x0146, /* Nvidia Geforce FX 6600 TE Go / 6200 TE Go */
160 	0x0147, /* Nvidia Geforce FX 6700 XL */
161 	0x0148, /* Nvidia Geforce FX 6600 Go */
162 	0x0149, /* Nvidia Geforce FX 6600 GT Go */
163 	0x014b, /* Nvidia unknown FX */
164 	0x014c, /* Nvidia Quadro FX 540 MXM */
165 	0x014d, /* Nvidia unknown FX */
166 	0x014e, /* Nvidia Quadro FX 540 */
167 	0x014f, /* Nvidia Geforce 6200 PCIe (128Mb) */
168 	0x0150, /* Nvidia Geforce2 GTS/Pro */
169 	0x0151, /* Nvidia Geforce2 Ti DDR */
170 	0x0152, /* Nvidia Geforce2 Ultra */
171 	0x0153, /* Nvidia Quadro2 Pro */
172 	0x0160, /* Nvidia Geforce 6500 Go */
173 	0x0161, /* Nvidia Geforce 6200 TurboCache */
174 	0x0162, /* Nvidia Geforce 6200SE TurboCache */
175 	0x0163, /* Nvidia Geforce 6200LE */
176 	0x0164, /* Nvidia Geforce FX 6200 Go */
177 	0x0165, /* Nvidia Quadro FX NVS 285 */
178 	0x0166, /* Nvidia Geforce 6400 Go */
179 	0x0167, /* Nvidia Geforce 6200 Go */
180 	0x0168, /* Nvidia Geforce 6400 Go */
181 	0x0169, /* Nvidia Geforce 6250 Go */
182 	0x016a, /* Nvidia Geforce 7100 GS */
183 	0x016b, /* Nvidia unknown FX Go */
184 	0x016c, /* Nvidia unknown FX Go */
185 	0x016d, /* Nvidia unknown FX Go */
186 	0x016e, /* Nvidia unknown FX */
187 	0x0170, /* Nvidia Geforce4 MX 460 */
188 	0x0171, /* Nvidia Geforce4 MX 440 */
189 	0x0172, /* Nvidia Geforce4 MX 420 */
190 	0x0173, /* Nvidia Geforce4 MX 440SE */
191 	0x0174, /* Nvidia Geforce4 440 Go */
192 	0x0175, /* Nvidia Geforce4 420 Go */
193 	0x0176, /* Nvidia Geforce4 420 Go 32M */
194 	0x0177, /* Nvidia Geforce4 460 Go */
195 	0x0178, /* Nvidia Quadro4 500 XGL/550 XGL */
196 	0x0179, /* Nvidia Geforce4 440 Go 64M (PPC: Geforce4 MX) */
197 	0x017a, /* Nvidia Quadro4 200 NVS/400 NVS */
198 	0x017c, /* Nvidia Quadro4 500 GoGL */
199 	0x017d, /* Nvidia Geforce4 410 Go 16M */
200 	0x0181, /* Nvidia Geforce4 MX 440 AGP8X */
201 	0x0182, /* Nvidia Geforce4 MX 440SE AGP8X */
202 	0x0183, /* Nvidia Geforce4 MX 420 AGP8X */
203 	0x0185, /* Nvidia Geforce4 MX 4000 AGP8X */
204 	0x0186, /* Nvidia Geforce4 448 Go */
205 	0x0187, /* Nvidia Geforce4 488 Go */
206 	0x0188, /* Nvidia Quadro4 580 XGL */
207 	0x0189,	/* Nvidia Geforce4 MX AGP8X (PPC) */
208 	0x018a, /* Nvidia Quadro4 280 NVS AGP8X */
209 	0x018b, /* Nvidia Quadro4 380 XGL */
210 	0x018c, /* Nvidia Quadro4 NVS 50 PCI */
211 	0x018d, /* Nvidia Geforce4 448 Go */
212 	0x01a0, /* Nvidia Geforce2 Integrated GPU */
213 	0x01d1, /* Nvidia Geforce 7300 LE */
214 	0x01d3, /* Nvidia Geforce 7300 SE */
215 	0x01d7,	/* Nvidia Quadro NVS 110M/Geforce 7300 Go */
216 	0x01d8,	/* Nvidia Geforce 7400 GO */
217 	0x01dd, /* Nvidia Geforce 7500 LE */
218 	0x01df, /* Nvidia Geforce 7300 GS */
219 	0x01f0, /* Nvidia Geforce4 MX Integrated GPU */
220 	0x0200, /* Nvidia Geforce3 */
221 	0x0201, /* Nvidia Geforce3 Ti 200 */
222 	0x0202, /* Nvidia Geforce3 Ti 500 */
223 	0x0203, /* Nvidia Quadro DCC */
224 	0x0211, /* Nvidia Geforce FX 6800 */
225 	0x0212, /* Nvidia Geforce FX 6800LE */
226 	0x0215, /* Nvidia Geforce FX 6800 GT */
227 	0x0218, /* Nvidia Geforce 6800 XT */
228 	0x0220, /* Nvidia unknown FX */
229 	0x0221, /* Nvidia Geforce 6200 AGP (256Mb - 128bit) */
230 	0x0222, /* Nvidia unknown FX */
231 	0x0228, /* Nvidia unknown FX Go */
232 	0x0240, /* Nvidia Geforce 6150 (NFORCE4 Integr.GPU) */
233 	0x0241, /* Nvidia Geforce 6150 LE (NFORCE4 Integr.GPU) */
234 	0x0242, /* Nvidia Geforce 6100 (NFORCE4 Integr.GPU) */
235 	0x0244, /* Nvidia Geforce Go 6150 (NFORCE4 Integr.GPU) */
236 	0x0245, /* Nvidia Quadro NVS 210S / Geforce 6150LE */
237 	0x0247, /* Nvidia Geforce 6100 Go (NFORCE4 Integr.GPU) */
238 	0x0250, /* Nvidia Geforce4 Ti 4600 */
239 	0x0251, /* Nvidia Geforce4 Ti 4400 */
240 	0x0252, /* Nvidia Geforce4 Ti 4600 */
241 	0x0253, /* Nvidia Geforce4 Ti 4200 */
242 	0x0258, /* Nvidia Quadro4 900 XGL */
243 	0x0259, /* Nvidia Quadro4 750 XGL */
244 	0x025b, /* Nvidia Quadro4 700 XGL */
245 	0x0280, /* Nvidia Geforce4 Ti 4800 AGP8X */
246 	0x0281, /* Nvidia Geforce4 Ti 4200 AGP8X */
247 	0x0282, /* Nvidia Geforce4 Ti 4800SE */
248 	0x0286, /* Nvidia Geforce4 4200 Go */
249 	0x0288, /* Nvidia Quadro4 980 XGL */
250 	0x0289, /* Nvidia Quadro4 780 XGL */
251 	0x028c, /* Nvidia Quadro4 700 GoGL */
252 	0x0290, /* Nvidia Geforce 7900 GTX */
253 	0x0291, /* Nvidia Geforce 7900 GT */
254 	0x0292, /* Nvidia Geforce 7900 GS */
255 	0x0293, /* Nvidia Geforce 7900 GX2 */
256 	0x0294, /* Nvidia Geforce 7950 GX2 */
257 	0x0295, /* Nvidia Geforce 7950 GT */
258 	0x0298, /* Nvidia Geforce Go 7900 GS */
259 	0x0299, /* Nvidia Geforce Go 7900 GTX */
260 	0x029c, /* Nvidia Quadro FX 5500 */
261 	0x029f, /* Nvidia Quadro FX 4500 X2 */
262 	0x02a0, /* Nvidia Geforce3 Integrated GPU */
263 	0x02e0,	/* Nvidia Geforce 7600 GT */
264 	0x02e1,	/* Nvidia Geforce 7600 GS */
265 	0x02e2, /* Nvidia Geforce 7300 GT */
266 	0x0301, /* Nvidia Geforce FX 5800 Ultra */
267 	0x0302, /* Nvidia Geforce FX 5800 */
268 	0x0308, /* Nvidia Quadro FX 2000 */
269 	0x0309, /* Nvidia Quadro FX 1000 */
270 	0x0311, /* Nvidia Geforce FX 5600 Ultra */
271 	0x0312, /* Nvidia Geforce FX 5600 */
272 	0x0313, /* Nvidia unknown FX */
273 	0x0314, /* Nvidia Geforce FX 5600XT */
274 	0x0316, /* Nvidia unknown FX Go */
275 	0x0317, /* Nvidia unknown FX Go */
276 	0x031a, /* Nvidia Geforce FX 5600 Go */
277 	0x031b, /* Nvidia Geforce FX 5650 Go */
278 	0x031c, /* Nvidia Quadro FX 700 Go */
279 	0x031d, /* Nvidia unknown FX Go */
280 	0x031e, /* Nvidia unknown FX Go */
281 	0x031f, /* Nvidia unknown FX Go */
282 	0x0320, /* Nvidia Geforce FX 5200 */
283 	0x0321, /* Nvidia Geforce FX 5200 Ultra */
284 	0x0322, /* Nvidia Geforce FX 5200 */
285 	0x0323, /* Nvidia Geforce FX 5200LE */
286 	0x0324, /* Nvidia Geforce FX 5200 Go */
287 	0x0325, /* Nvidia Geforce FX 5250 Go */
288 	0x0326, /* Nvidia Geforce FX 5500 */
289 	0x0327, /* Nvidia Geforce FX 5100 */
290 	0x0328, /* Nvidia Geforce FX 5200 Go 32M/64M */
291 	0x0329, /* Nvidia Geforce FX 5200 (PPC) */
292 	0x032a, /* Nvidia Quadro NVS 280 PCI */
293 	0x032b, /* Nvidia Quadro FX 500/600 PCI */
294 	0x032c, /* Nvidia Geforce FX 5300 Go */
295 	0x032d, /* Nvidia Geforce FX 5100 Go */
296 	0x032e, /* Nvidia unknown FX Go */
297 	0x032f, /* Nvidia unknown FX Go */
298 	0x0330, /* Nvidia Geforce FX 5900 Ultra */
299 	0x0331, /* Nvidia Geforce FX 5900 */
300 	0x0332, /* Nvidia Geforce FX 5900 XT */
301 	0x0333, /* Nvidia Geforce FX 5950 Ultra */
302 	0x0334, /* Nvidia Geforce FX 5900 ZT */
303 	0x0338, /* Nvidia Quadro FX 3000 */
304 	0x033f, /* Nvidia Quadro FX 700 */
305 	0x0341, /* Nvidia Geforce FX 5700 Ultra */
306 	0x0342, /* Nvidia Geforce FX 5700 */
307 	0x0343, /* Nvidia Geforce FX 5700LE */
308 	0x0344, /* Nvidia Geforce FX 5700VE */
309 	0x0345, /* Nvidia unknown FX */
310 	0x0347, /* Nvidia Geforce FX 5700 Go */
311 	0x0348, /* Nvidia Geforce FX 5700 Go */
312 	0x0349, /* Nvidia unknown FX Go */
313 	0x034b, /* Nvidia unknown FX Go */
314 	0x034c, /* Nvidia Quadro FX 1000 Go */
315 	0x034e, /* Nvidia Quadro FX 1100 */
316 	0x034f, /* Nvidia unknown FX */
317 	0x0391, /* Nvidia Geforce 7600 GT */
318 	0x0392, /* Nvidia Geforce 7600 GS */
319 	0x0393, /* Nvidia Geforce 7300 GT */
320 	0x0394, /* Nvidia Geforce 7600 LE */
321 	0x0398, /* Nvidia Geforce 7600 GO */
322 	0x03d0, /* Nvidia Geforce 6100 nForce 430 */
323 	0x03d1, /* Nvidia Geforce 6100 nForce 405 */
324 	0x03d2, /* Nvidia Geforce 6100 nForce 400 */
325 	0x03d5, /* Nvidia Geforce 6100 nForce 420 */
326 	0x03d6, /* Nvidia Geforce 7025 / nForce 630a */
327 	0x07e1, /* Nvidia Geforce 7100 / nForce 630i */
328 	0
329 };
330 
331 static uint16 elsa_device_list[] = {
332 	0x0c60, /* Elsa Gladiac Geforce2 MX */
333 	0
334 };
335 
336 static uint16 nvstbsgs_device_list[] = {
337 	0x0020, /* Nvidia STB/SGS-Thompson TNT1 */
338 	0x0028, /* Nvidia STB/SGS-Thompson TNT2 (pro) */
339 	0x0029, /* Nvidia STB/SGS-Thompson TNT2 Ultra */
340 	0x002a, /* Nvidia STB/SGS-Thompson TNT2 */
341 	0x002b, /* Nvidia STB/SGS-Thompson TNT2 */
342 	0x002c, /* Nvidia STB/SGS-Thompson Vanta (Lt) */
343 	0x002d, /* Nvidia STB/SGS-Thompson TNT2-M64 (Pro) */
344 	0x002e, /* Nvidia STB/SGS-Thompson NV06 Vanta */
345 	0x002f, /* Nvidia STB/SGS-Thompson NV06 Vanta */
346 	0x00a0, /* Nvidia STB/SGS-Thompson Aladdin TNT2 */
347 	0
348 };
349 
350 static uint16 varisys_device_list[] = {
351 	0x3503, /* Varisys Geforce4 MX440 */
352 	0x3505, /* Varisys Geforce4 Ti 4200 */
353 	0
354 };
355 
356 static struct {
357 	uint16	vendor;
358 	uint16	*devices;
359 } SupportedDevices[] = {
360 	{VENDOR_ID_NVIDIA, nvidia_device_list},
361 	{VENDOR_ID_ELSA, elsa_device_list},
362 	{VENDOR_ID_NVSTBSGS, nvstbsgs_device_list},
363 	{VENDOR_ID_VARISYS, varisys_device_list},
364 	{0x0000, NULL}
365 };
366 
367 static nv_settings sSettings = { // see comments in nvidia.settings
368 	/* for driver */
369 	DRIVER_PREFIX ".accelerant",
370 	"none",					// primary
371 	false,      			// dumprom
372 	/* for accelerant */
373 	0x00000000, 			// logmask
374 	0,          			// memory
375 	0,						// tv_output
376 	true,       			// usebios
377 	true,       			// hardcursor
378 	false,					// switchhead
379 	false,					// force_pci
380 	false,					// unhide_fw
381 	false,					// pgm_panel
382 	true,					// dma_acc
383 	false,					// vga_on_tv
384 	false,					// force_sync
385 	false,					// force_ws
386 	false,					// block_acc
387 	0,						// gpu_clk
388 	0,						// ram_clk
389 	true,					// check_edid
390 };
391 
392 
393 static void
394 dumprom(void *rom, uint32 size, pci_info pcii)
395 {
396 	int fd;
397 	uint32 cnt;
398 	char fname[64];
399 
400 	/* determine the romfile name: we need split-up per card in the system */
401 	sprintf (fname, kUserDirectory "//" DRIVER_PREFIX "." DEVICE_FORMAT ".rom",
402 		pcii.vendor_id, pcii.device_id, pcii.bus, pcii.device, pcii.function);
403 
404 	fd = open (fname, O_WRONLY | O_CREAT, 0666);
405 	if (fd < 0) return;
406 
407 	/* apparantly max. 32kb may be written at once;
408 	 * the ROM size is a multiple of that anyway. */
409 	for (cnt = 0; (cnt < size); cnt += 32768)
410 		write (fd, ((void *)(((uint8 *)rom) + cnt)), 32768);
411 	close (fd);
412 }
413 
414 
415 /*! return 1 if vblank interrupt has occured */
416 static int
417 caused_vbi_crtc1(vuint32 * regs)
418 {
419 	return (NV_REG32(NV32_CRTC_INTS) & 0x00000001);
420 }
421 
422 
423 /*! clear the vblank interrupt */
424 static void
425 clear_vbi_crtc1(vuint32 * regs)
426 {
427 	NV_REG32(NV32_CRTC_INTS) = 0x00000001;
428 }
429 
430 
431 static void
432 enable_vbi_crtc1(vuint32 * regs)
433 {
434 	/* clear the vblank interrupt */
435 	NV_REG32(NV32_CRTC_INTS) = 0x00000001;
436 	/* enable nVidia interrupt source vblank */
437 	NV_REG32(NV32_CRTC_INTE) |= 0x00000001;
438 	/* enable nVidia interrupt system hardware (b0-1) */
439 	NV_REG32(NV32_MAIN_INTE) = 0x00000001;
440 }
441 
442 
443 static void
444 disable_vbi_crtc1(vuint32 * regs)
445 {
446 	/* disable nVidia interrupt source vblank */
447 	NV_REG32(NV32_CRTC_INTE) &= 0xfffffffe;
448 	/* clear the vblank interrupt */
449 	NV_REG32(NV32_CRTC_INTS) = 0x00000001;
450 }
451 
452 
453 /*! return 1 if vblank interrupt has occured */
454 static int
455 caused_vbi_crtc2(vuint32 * regs)
456 {
457 	return (NV_REG32(NV32_CRTC2_INTS) & 0x00000001);
458 }
459 
460 
461 /*! clear the vblank interrupt */
462 static void
463 clear_vbi_crtc2(vuint32 * regs)
464 {
465 	NV_REG32(NV32_CRTC2_INTS) = 0x00000001;
466 }
467 
468 
469 static void
470 enable_vbi_crtc2(vuint32 * regs)
471 {
472 	/* clear the vblank interrupt */
473 	NV_REG32(NV32_CRTC2_INTS) = 0x00000001;
474 	/* enable nVidia interrupt source vblank */
475 	NV_REG32(NV32_CRTC2_INTE) |= 0x00000001;
476 	/* enable nVidia interrupt system hardware (b0-1) */
477 	NV_REG32(NV32_MAIN_INTE) = 0x00000001;
478 }
479 
480 
481 static void
482 disable_vbi_crtc2(vuint32 * regs)
483 {
484 	/* disable nVidia interrupt source vblank */
485 	NV_REG32(NV32_CRTC2_INTE) &= 0xfffffffe;
486 	/* clear the vblank interrupt */
487 	NV_REG32(NV32_CRTC2_INTS) = 0x00000001;
488 }
489 
490 
491 //fixme:
492 //dangerous code, on singlehead cards better not try accessing secondary head
493 //registers (card might react in unpredictable ways, though there's only a small
494 //chance we actually run into this).
495 //fix requires (some) card recognition code to be moved from accelerant to
496 //kerneldriver...
497 static void
498 disable_vbi_all(vuint32 * regs)
499 {
500 	/* disable nVidia interrupt source vblank */
501 	NV_REG32(NV32_CRTC_INTE) &= 0xfffffffe;
502 	/* clear the vblank interrupt */
503 	NV_REG32(NV32_CRTC_INTS) = 0x00000001;
504 
505 	/* disable nVidia interrupt source vblank */
506 	NV_REG32(NV32_CRTC2_INTE) &= 0xfffffffe;
507 	/* clear the vblank interrupt */
508 	NV_REG32(NV32_CRTC2_INTS) = 0x00000001;
509 
510 	/* disable nVidia interrupt system hardware (b0-1) */
511 	NV_REG32(NV32_MAIN_INTE) = 0x00000000;
512 }
513 
514 
515 static status_t
516 map_device(device_info *di)
517 {
518 	char buffer[B_OS_NAME_LENGTH]; /*memory for device name*/
519 	shared_info *si = di->si;
520 	uint32	tmpUlong, tmpROMshadow;
521 	pci_info *pcii = &(di->pcii);
522 	phys_addr_t physicalAddress;
523 	system_info sysinfo;
524 
525 	/* variables for making copy of ROM */
526 	uint8* rom_temp;
527 	area_id rom_area = -1;
528 
529 	/* Nvidia cards have registers in [0] and framebuffer in [1] */
530 	int registers = 0;
531 	int frame_buffer = 1;
532 
533 	/* enable memory mapped IO, disable VGA I/O - this is defined in the PCI standard */
534 	tmpUlong = get_pci(PCI_command, 2);
535 	/* enable PCI access */
536 	tmpUlong |= PCI_command_memory;
537 	/* enable busmastering */
538 	tmpUlong |= PCI_command_master;
539 	/* disable ISA I/O access */
540 	tmpUlong &= ~PCI_command_io;
541 	set_pci(PCI_command, 2, tmpUlong);
542 
543  	/*work out which version of BeOS is running*/
544  	get_system_info(&sysinfo);
545  	if (0)//sysinfo.kernel_build_date[0]=='J')/*FIXME - better ID version*/
546  	{
547  		si->use_clone_bugfix = 1;
548  	}
549  	else
550  	{
551  		si->use_clone_bugfix = 0;
552  	}
553 
554 	/* work out a name for the register mapping */
555 	sprintf(buffer, DEVICE_FORMAT " regs",
556 		di->pcii.vendor_id, di->pcii.device_id,
557 		di->pcii.bus, di->pcii.device, di->pcii.function);
558 
559 	/* get a virtual memory address for the registers*/
560 	si->regs_area = map_physical_memory(
561 		buffer,
562 		/* WARNING: Nvidia needs to map regs as viewed from PCI space! */
563 		di->pcii.u.h0.base_registers_pci[registers],
564 		di->pcii.u.h0.base_register_sizes[registers],
565 		B_ANY_KERNEL_ADDRESS,
566 		B_CLONEABLE_AREA | B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA,
567 		(void **)&(di->regs));
568 	si->clone_bugfix_regs = (uint32 *) di->regs;
569 
570 	/* if mapping registers to vmem failed then pass on error */
571 	if (si->regs_area < 0) return si->regs_area;
572 
573 	/* work out a name for the ROM mapping*/
574 	sprintf(buffer, DEVICE_FORMAT " rom",
575 		di->pcii.vendor_id, di->pcii.device_id,
576 		di->pcii.bus, di->pcii.device, di->pcii.function);
577 
578 	/* preserve ROM shadowing setting, we need to restore the current state later on. */
579 	/* warning:
580 	 * 'don't touch': (confirmed) NV04, NV05, NV05-M64, NV11 all shutoff otherwise.
581 	 * NV18, NV28 and NV34 keep working.
582 	 * confirmed NV28 and NV34 to use upper part of shadowed ROM for scratch purposes,
583 	 * however the actual ROM content (so the used part) is intact (confirmed). */
584 	tmpROMshadow = get_pci(NVCFG_ROMSHADOW, 4);
585 	/* temporary disable ROM shadowing, we want the guaranteed exact contents of the chip */
586 	set_pci(NVCFG_ROMSHADOW, 4, 0);
587 
588 	/* get ROM memory mapped base adress - this is defined in the PCI standard */
589 	tmpUlong = get_pci(PCI_rom_base, 4);
590 	//fixme?: if (!tmpUlong) try to map the ROM ourselves. Confirmed a PCIe system not
591 	//having the ROM mapped on PCI and PCIe cards. Falling back to fetching from ISA
592 	//legacy space will get us into trouble if we aren't the primary graphics card!!
593 	//(as legacy space always has the primary card's ROM 'mapped'!)
594 	if (tmpUlong) {
595 		/* ROM was assigned an adress, so enable ROM decoding - see PCI standard */
596 		tmpUlong |= 0x00000001;
597 		set_pci(PCI_rom_base, 4, tmpUlong);
598 
599 		rom_area = map_physical_memory(
600 			buffer,
601 			di->pcii.u.h0.rom_base_pci,
602 			di->pcii.u.h0.rom_size,
603 			B_ANY_KERNEL_ADDRESS,
604 			B_READ_AREA,
605 			(void **)&(rom_temp)
606 		);
607 
608 		/* check if we got the BIOS and signature (might fail on laptops..) */
609 		if (rom_area >= 0) {
610 			if ((rom_temp[0] != 0x55) || (rom_temp[1] != 0xaa)) {
611 				/* apparantly no ROM is mapped here */
612 				delete_area(rom_area);
613 				rom_area = -1;
614 				/* force using ISA legacy map as fall-back */
615 				tmpUlong = 0x00000000;
616 			}
617 		} else {
618 			/* mapping failed: force using ISA legacy map as fall-back */
619 			tmpUlong = 0x00000000;
620 		}
621 	}
622 
623 	if (!tmpUlong) {
624 		/* ROM was not assigned an adress, fetch it from ISA legacy memory map! */
625 		rom_area = map_physical_memory(buffer, 0x000c0000,
626 			65536, B_ANY_KERNEL_ADDRESS, B_READ_AREA, (void **)&(rom_temp));
627 	}
628 
629 	/* if mapping ROM to vmem failed then clean up and pass on error */
630 	if (rom_area < 0) {
631 		delete_area(si->regs_area);
632 		si->regs_area = -1;
633 		return rom_area;
634 	}
635 
636 	/* dump ROM to file if selected in nvidia.settings
637 	 * (ROM always fits in 64Kb: checked TNT1 - FX5950) */
638 	if (sSettings.dumprom)
639 		dumprom(rom_temp, 65536, di->pcii);
640 
641 	/* make a copy of ROM for future reference */
642 	memcpy(si->rom_mirror, rom_temp, 65536);
643 
644 	/* disable ROM decoding - this is defined in the PCI standard, and delete the area */
645 	tmpUlong = get_pci(PCI_rom_base, 4);
646 	tmpUlong &= 0xfffffffe;
647 	set_pci(PCI_rom_base, 4, tmpUlong);
648 	delete_area(rom_area);
649 
650 	/* restore original ROM shadowing setting to prevent trouble starting (some) cards */
651 	set_pci(NVCFG_ROMSHADOW, 4, tmpROMshadow);
652 
653 	/* work out a name for the framebuffer mapping*/
654 	sprintf(buffer, DEVICE_FORMAT " framebuffer",
655 		di->pcii.vendor_id, di->pcii.device_id,
656 		di->pcii.bus, di->pcii.device, di->pcii.function);
657 
658 	physicalAddress = di->pcii.u.h0.base_registers_pci[frame_buffer];
659 	if ((di->pcii.u.h0.base_register_flags[frame_buffer] & PCI_address_type)
660 			== PCI_address_type_64) {
661 		physicalAddress
662 			|= (uint64)di->pcii.u.h0.base_registers_pci[frame_buffer + 1] << 32;
663 	}
664 
665 	/* map the framebuffer into vmem, using Write Combining*/
666 	si->fb_area = map_physical_memory(buffer,
667 		/* WARNING: Nvidia needs to map framebuffer as viewed from PCI space! */
668 		physicalAddress,
669 		di->pcii.u.h0.base_register_sizes[frame_buffer],
670 		B_ANY_KERNEL_BLOCK_ADDRESS | B_MTR_WC,
671 		B_READ_AREA | B_WRITE_AREA | B_CLONEABLE_AREA,
672 		&(si->framebuffer));
673 
674 	/*if failed with write combining try again without*/
675 	if (si->fb_area < 0) {
676 		si->fb_area = map_physical_memory(buffer,
677 			/* WARNING: Nvidia needs to map framebuffer as viewed from PCI space! */
678 			physicalAddress,
679 			di->pcii.u.h0.base_register_sizes[frame_buffer],
680 			B_ANY_KERNEL_BLOCK_ADDRESS,
681 			B_READ_AREA | B_WRITE_AREA | B_CLONEABLE_AREA,
682 			&(si->framebuffer));
683 	}
684 
685 	/* if there was an error, delete our other areas and pass on error*/
686 	if (si->fb_area < 0) {
687 		delete_area(si->regs_area);
688 		si->regs_area = -1;
689 		return si->fb_area;
690 	}
691 
692 	//fixme: retest for card coldstart and PCI/virt_mem mapping!!
693 	/* remember the DMA address of the frame buffer for BDirectWindow?? purposes */
694 	si->framebuffer_pci = (void *) physicalAddress;
695 
696 	/* note the amount of memory mapped by the kerneldriver so we can make sure we
697 	 * don't attempt to adress more later on */
698 	si->ps.memory_size = di->pcii.u.h0.base_register_sizes[frame_buffer];
699 
700 	// remember settings for use here and in accelerant
701 	si->settings = sSettings;
702 
703 	/* in any case, return the result */
704 	return si->fb_area;
705 }
706 
707 
708 static void
709 unmap_device(device_info *di)
710 {
711 	shared_info *si = di->si;
712 	uint32	tmpUlong;
713 	pci_info *pcii = &(di->pcii);
714 
715 	/* disable memory mapped IO */
716 	tmpUlong = get_pci(PCI_command, 4);
717 	tmpUlong &= 0xfffffffc;
718 	set_pci(PCI_command, 4, tmpUlong);
719 	/* delete the areas */
720 	if (si->regs_area >= 0)
721 		delete_area(si->regs_area);
722 	if (si->fb_area >= 0)
723 		delete_area(si->fb_area);
724 	si->regs_area = si->fb_area = -1;
725 	si->framebuffer = NULL;
726 	di->regs = NULL;
727 }
728 
729 
730 static void
731 probe_devices(void)
732 {
733 	uint32 pci_index = 0;
734 	uint32 count = 0;
735 	device_info *di = pd->di;
736 	char tmp_name[B_OS_NAME_LENGTH];
737 
738 	/* while there are more pci devices */
739 	while (count < MAX_DEVICES
740 		&& (*pci_bus->get_nth_pci_info)(pci_index, &(di->pcii)) == B_OK) {
741 		int vendor = 0;
742 
743 		/* if we match a supported vendor */
744 		while (SupportedDevices[vendor].vendor) {
745 			if (SupportedDevices[vendor].vendor == di->pcii.vendor_id) {
746 				uint16 *devices = SupportedDevices[vendor].devices;
747 				/* while there are more supported devices */
748 				while (*devices) {
749 					/* if we match a supported device */
750 					if (*devices == di->pcii.device_id ) {
751 						/* publish the device name */
752 						sprintf(tmp_name, DEVICE_FORMAT,
753 							di->pcii.vendor_id, di->pcii.device_id,
754 							di->pcii.bus, di->pcii.device, di->pcii.function);
755 						/* tweak the exported name to show first in the alphabetically ordered /dev/
756 						 * hierarchy folder, so the system will use it as primary adaptor if requested
757 						 * via nvidia.settings. */
758 						if (strcmp(tmp_name, sSettings.primary) == 0)
759 							sprintf(tmp_name, "-%s", sSettings.primary);
760 						/* add /dev/ hierarchy path */
761 						sprintf(di->name, "graphics/%s", tmp_name);
762 						/* remember the name */
763 						pd->device_names[count] = di->name;
764 						/* mark the driver as available for R/W open */
765 						di->is_open = 0;
766 						/* mark areas as not yet created */
767 						di->shared_area = -1;
768 						/* mark pointer to shared data as invalid */
769 						di->si = NULL;
770 						/* inc pointer to device info */
771 						di++;
772 						/* inc count */
773 						count++;
774 						/* break out of these while loops */
775 						goto next_device;
776 					}
777 					/* next supported device */
778 					devices++;
779 				}
780 			}
781 			vendor++;
782 		}
783 next_device:
784 		/* next pci_info struct, please */
785 		pci_index++;
786 	}
787 	/* propagate count */
788 	pd->count = count;
789 	/* terminate list of device names with a null pointer */
790 	pd->device_names[pd->count] = NULL;
791 }
792 
793 
794 static uint32
795 thread_interrupt_work(int32 *flags, vuint32 *regs, shared_info *si)
796 {
797 	uint32 handled = B_HANDLED_INTERRUPT;
798 	/* release the vblank semaphore */
799 	if (si->vblank >= 0) {
800 		int32 blocked;
801 		if ((get_sem_count(si->vblank, &blocked) == B_OK) && (blocked < 0)) {
802 			release_sem_etc(si->vblank, -blocked, B_DO_NOT_RESCHEDULE);
803 			handled = B_INVOKE_SCHEDULER;
804 		}
805 	}
806 	return handled;
807 }
808 
809 
810 static int32
811 nv_interrupt(void *data)
812 {
813 	int32 handled = B_UNHANDLED_INTERRUPT;
814 	device_info *di = (device_info *)data;
815 	shared_info *si = di->si;
816 	int32 *flags = &(si->flags);
817 	vuint32 *regs;
818 
819 	/* is someone already handling an interrupt for this device? */
820 	if (atomic_or(flags, SKD_HANDLER_INSTALLED) & SKD_HANDLER_INSTALLED) goto exit0;
821 
822 	/* get regs */
823 	regs = di->regs;
824 
825 	/* was it a VBI? */
826 	/* note: si->ps.secondary_head was cleared by kerneldriver earlier! (at least) */
827 	if (si->ps.secondary_head) {
828 		//fixme:
829 		//rewrite once we use one driver instance 'per head' (instead of 'per card')
830 		if (caused_vbi_crtc1(regs) || caused_vbi_crtc2(regs)) {
831 			/* clear the interrupt(s) */
832 			clear_vbi_crtc1(regs);
833 			clear_vbi_crtc2(regs);
834 			/* release the semaphore */
835 			handled = thread_interrupt_work(flags, regs, si);
836 		}
837 	} else {
838 		if (caused_vbi_crtc1(regs)) {
839 			/* clear the interrupt */
840 			clear_vbi_crtc1(regs);
841 			/* release the semaphore */
842 			handled = thread_interrupt_work(flags, regs, si);
843 		}
844 	}
845 
846 	/* note that we're not in the handler any more */
847 	atomic_and(flags, ~SKD_HANDLER_INSTALLED);
848 
849 exit0:
850 	return handled;
851 }
852 
853 
854 //	#pragma mark - device hooks
855 
856 
857 static status_t
858 open_hook(const char* name, uint32 flags, void** cookie)
859 {
860 	int32 index = 0;
861 	device_info *di;
862 	shared_info *si;
863 	thread_id	thid;
864 	thread_info	thinfo;
865 	status_t	result = B_OK;
866 	char shared_name[B_OS_NAME_LENGTH];
867 	physical_entry map[1];
868 	size_t net_buf_size;
869 	void *unaligned_dma_buffer;
870 	uint32 mem_size;
871 
872 	/* find the device name in the list of devices */
873 	/* we're never passed a name we didn't publish */
874 	while (pd->device_names[index]
875 		&& (strcmp(name, pd->device_names[index]) != 0))
876 		index++;
877 
878 	/* for convienience */
879 	di = &(pd->di[index]);
880 
881 	/* make sure no one else has write access to the common data */
882 	AQUIRE_BEN(pd->kernel);
883 
884 	/* if it's already open for writing */
885 	if (di->is_open) {
886 		/* mark it open another time */
887 		goto mark_as_open;
888 	}
889 	/* create the shared_info area */
890 	sprintf(shared_name, DEVICE_FORMAT " shared",
891 		di->pcii.vendor_id, di->pcii.device_id,
892 		di->pcii.bus, di->pcii.device, di->pcii.function);
893 	/* create this area with NO user-space read or write permissions, to prevent accidental damage */
894 	di->shared_area = create_area(shared_name, (void **)&(di->si), B_ANY_KERNEL_ADDRESS,
895 		((sizeof(shared_info) + (B_PAGE_SIZE - 1)) & ~(B_PAGE_SIZE - 1)), B_FULL_LOCK,
896 		B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA | B_CLONEABLE_AREA);
897 	if (di->shared_area < 0) {
898 		/* return the error */
899 		result = di->shared_area;
900 		goto done;
901 	}
902 
903 	/* save a few dereferences */
904 	si = di->si;
905 
906 	/* create the DMA command buffer area */
907 	//fixme? for R4.5 a workaround for cloning would be needed!
908 	/* we want to setup a 1Mb buffer (size must be multiple of B_PAGE_SIZE) */
909 	net_buf_size = ((1 * 1024 * 1024) + (B_PAGE_SIZE-1)) & ~(B_PAGE_SIZE-1);
910 	/* create the area that will hold the DMA command buffer */
911 	si->unaligned_dma_area =
912 		create_area("NV DMA cmd buffer",
913 			(void **)&unaligned_dma_buffer,
914 			B_ANY_KERNEL_ADDRESS,
915 			2 * net_buf_size, /* take twice the net size so we can have MTRR-WC even on old systems */
916 			B_32_BIT_CONTIGUOUS, /* GPU always needs access */
917 			B_CLONEABLE_AREA | B_READ_AREA | B_WRITE_AREA);
918 			// TODO: Physical aligning can be done without waste using the
919 			// private create_area_etc().
920 	/* on error, abort */
921 	if (si->unaligned_dma_area < 0)
922 	{
923 		/* free the already created shared_info area, and return the error */
924 		result = si->unaligned_dma_area;
925 		goto free_shared;
926 	}
927 	/* we (also) need the physical adress our DMA buffer is at, as this needs to be
928 	 * fed into the GPU's engine later on. Get an aligned adress so we can use MTRR-WC
929 	 * even on older CPU's. */
930 	get_memory_map(unaligned_dma_buffer, B_PAGE_SIZE, map, 1);
931 	si->dma_buffer_pci = (void*)
932 		((map[0].address + net_buf_size - 1) & ~(net_buf_size - 1));
933 
934 	/* map the net DMA command buffer into vmem, using Write Combining */
935 	si->dma_area = map_physical_memory(
936 		"NV aligned DMA cmd buffer", (addr_t)si->dma_buffer_pci, net_buf_size,
937 		B_ANY_KERNEL_BLOCK_ADDRESS | B_MTR_WC,
938 		B_READ_AREA | B_WRITE_AREA, &(si->dma_buffer));
939 	/* if failed with write combining try again without */
940 	if (si->dma_area < 0) {
941 		si->dma_area = map_physical_memory("NV aligned DMA cmd buffer",
942 			(addr_t)si->dma_buffer_pci, net_buf_size,
943 			B_ANY_KERNEL_BLOCK_ADDRESS,
944 			B_READ_AREA | B_WRITE_AREA, &(si->dma_buffer));
945 	}
946 	/* if there was an error, delete our other areas and pass on error*/
947 	if (si->dma_area < 0)
948 	{
949 		/* free the already created areas, and return the error */
950 		result = si->dma_area;
951 		goto free_shared_and_uadma;
952 	}
953 
954 	/* save the vendor and device IDs */
955 	si->vendor_id = di->pcii.vendor_id;
956 	si->device_id = di->pcii.device_id;
957 	si->revision = di->pcii.revision;
958 	si->bus = di->pcii.bus;
959 	si->device = di->pcii.device;
960 	si->function = di->pcii.function;
961 
962 	/* ensure that the accelerant's INIT_ACCELERANT function can be executed */
963 	si->accelerant_in_use = false;
964 	/* preset singlehead card to prevent early INT routine calls (once installed) to
965 	 * wrongly identify the INT request coming from us! */
966 	si->ps.secondary_head = false;
967 
968 	/* map the device */
969 	result = map_device(di);
970 	if (result < 0) goto free_shared_and_alldma;
971 
972 	/* we will be returning OK status for sure now */
973 	result = B_OK;
974 
975 	/* note the amount of system RAM the system BIOS assigned to the card if applicable:
976 	 * unified memory architecture (UMA) */
977 	switch ((((uint32)(si->device_id)) << 16) | si->vendor_id)
978 	{
979 	case 0x01a010de: /* Nvidia Geforce2 Integrated GPU */
980 		/* device at bus #0, device #0, function #1 holds value at byte-index 0x7C */
981 		mem_size = 1024 * 1024 *
982 			(((((*pci_bus->read_pci_config)(0, 0, 1, 0x7c, 4)) & 0x000007c0) >> 6) + 1);
983 		/* don't attempt to adress memory not mapped by the kerneldriver */
984 		if (si->ps.memory_size > mem_size) si->ps.memory_size = mem_size;
985 		/* last 64kB RAM is used for the BIOS (or something else?) */
986 		si->ps.memory_size -= (64 * 1024);
987 		break;
988 	case 0x01f010de: /* Nvidia Geforce4 MX Integrated GPU */
989 		/* device at bus #0, device #0, function #1 holds value at byte-index 0x84 */
990 		mem_size = 1024 * 1024 *
991 			(((((*pci_bus->read_pci_config)(0, 0, 1, 0x84, 4)) & 0x000007f0) >> 4) + 1);
992 		/* don't attempt to adress memory not mapped by the kerneldriver */
993 		if (si->ps.memory_size > mem_size) si->ps.memory_size = mem_size;
994 		/* last 64kB RAM is used for the BIOS (or something else?) */
995 		si->ps.memory_size -= (64 * 1024);
996 		break;
997 	default:
998 		/* all other cards have own RAM: the amount of which is determined in the
999 		 * accelerant. */
1000 		break;
1001 	}
1002 
1003 	/* disable and clear any pending interrupts */
1004 	//fixme:
1005 	//distinquish between crtc1/crtc2 once all heads get seperate driver instances!
1006 	disable_vbi_all(di->regs);
1007 
1008 	/* preset we can't use INT related functions */
1009 	si->ps.int_assigned = false;
1010 
1011 	/* create a semaphore for vertical blank management */
1012 	si->vblank = create_sem(0, di->name);
1013 	if (si->vblank < 0) goto mark_as_open;
1014 
1015 	/* change the owner of the semaphores to the opener's team */
1016 	/* this is required because apps can't aquire kernel semaphores */
1017 	thid = find_thread(NULL);
1018 	get_thread_info(thid, &thinfo);
1019 	set_sem_owner(si->vblank, thinfo.team);
1020 
1021 	/* If there is a valid interrupt line assigned then set up interrupts */
1022 	if ((di->pcii.u.h0.interrupt_pin == 0x00) ||
1023 	    (di->pcii.u.h0.interrupt_line == 0xff) || /* no IRQ assigned */
1024 	    (di->pcii.u.h0.interrupt_line <= 0x02))   /* system IRQ assigned */
1025 	{
1026 		/* delete the semaphore as it won't be used */
1027 		delete_sem(si->vblank);
1028 		si->vblank = -1;
1029 	}
1030 	else
1031 	{
1032 		/* otherwise install our interrupt handler */
1033 		result = install_io_interrupt_handler(di->pcii.u.h0.interrupt_line, nv_interrupt, (void *)di, 0);
1034 		/* bail if we couldn't install the handler */
1035 		if (result != B_OK)
1036 		{
1037 			/* delete the semaphore as it won't be used */
1038 			delete_sem(si->vblank);
1039 			si->vblank = -1;
1040 		}
1041 		else
1042 		{
1043 			/* inform accelerant(s) we can use INT related functions */
1044 			si->ps.int_assigned = true;
1045 		}
1046 	}
1047 
1048 mark_as_open:
1049 	/* mark the device open */
1050 	di->is_open++;
1051 
1052 	/* send the cookie to the opener */
1053 	*cookie = di;
1054 
1055 	goto done;
1056 
1057 
1058 free_shared_and_alldma:
1059 	/* clean up our aligned DMA area */
1060 	delete_area(si->dma_area);
1061 	si->dma_area = -1;
1062 	si->dma_buffer = NULL;
1063 
1064 free_shared_and_uadma:
1065 	/* clean up our unaligned DMA area */
1066 	delete_area(si->unaligned_dma_area);
1067 	si->unaligned_dma_area = -1;
1068 	si->dma_buffer_pci = NULL;
1069 
1070 free_shared:
1071 	/* clean up our shared area */
1072 	delete_area(di->shared_area);
1073 	di->shared_area = -1;
1074 	di->si = NULL;
1075 
1076 done:
1077 	/* end of critical section */
1078 	RELEASE_BEN(pd->kernel);
1079 
1080 	/* all done, return the status */
1081 	return result;
1082 }
1083 
1084 
1085 static status_t
1086 read_hook(void* dev, off_t pos, void* buf, size_t* len)
1087 {
1088 	*len = 0;
1089 	return B_NOT_ALLOWED;
1090 }
1091 
1092 
1093 static status_t
1094 write_hook(void* dev, off_t pos, const void* buf, size_t* len)
1095 {
1096 	*len = 0;
1097 	return B_NOT_ALLOWED;
1098 }
1099 
1100 
1101 static status_t
1102 close_hook(void* dev)
1103 {
1104 	/* we don't do anything on close: there might be dup'd fd */
1105 	return B_NO_ERROR;
1106 }
1107 
1108 
1109 static status_t
1110 free_hook(void* dev)
1111 {
1112 	device_info *di = (device_info *)dev;
1113 	shared_info	*si = di->si;
1114 	vuint32 *regs = di->regs;
1115 
1116 	/* lock the driver */
1117 	AQUIRE_BEN(pd->kernel);
1118 
1119 	/* if opened multiple times, decrement the open count and exit */
1120 	if (di->is_open > 1)
1121 		goto unlock_and_exit;
1122 
1123 	/* disable and clear any pending interrupts */
1124 	//fixme:
1125 	//distinquish between crtc1/crtc2 once all heads get seperate driver instances!
1126 	disable_vbi_all(regs);
1127 
1128 	if (si->ps.int_assigned) {
1129 		/* remove interrupt handler */
1130 		remove_io_interrupt_handler(di->pcii.u.h0.interrupt_line, nv_interrupt, di);
1131 
1132 		/* delete the semaphores, ignoring any errors ('cause the owning
1133 		   team may have died on us) */
1134 		delete_sem(si->vblank);
1135 		si->vblank = -1;
1136 	}
1137 
1138 	/* free regs and framebuffer areas */
1139 	unmap_device(di);
1140 
1141 	/* clean up our aligned DMA area */
1142 	delete_area(si->dma_area);
1143 	si->dma_area = -1;
1144 	si->dma_buffer = NULL;
1145 
1146 	/* clean up our unaligned DMA area */
1147 	delete_area(si->unaligned_dma_area);
1148 	si->unaligned_dma_area = -1;
1149 	si->dma_buffer_pci = NULL;
1150 
1151 	/* clean up our shared area */
1152 	delete_area(di->shared_area);
1153 	di->shared_area = -1;
1154 	di->si = NULL;
1155 
1156 unlock_and_exit:
1157 	/* mark the device available */
1158 	di->is_open--;
1159 	/* unlock the driver */
1160 	RELEASE_BEN(pd->kernel);
1161 	/* all done */
1162 	return B_OK;
1163 }
1164 
1165 
1166 static status_t
1167 control_hook(void* dev, uint32 msg, void *buf, size_t len)
1168 {
1169 	device_info *di = (device_info *)dev;
1170 	status_t result = B_DEV_INVALID_IOCTL;
1171 	uint32 tmpUlong;
1172 
1173 	switch (msg) {
1174 		/* the only PUBLIC ioctl */
1175 		case B_GET_ACCELERANT_SIGNATURE:
1176 		{
1177 			strcpy((char* )buf, sSettings.accelerant);
1178 			result = B_OK;
1179 			break;
1180 		}
1181 
1182 		/* PRIVATE ioctl from here on */
1183 		case NV_GET_PRIVATE_DATA:
1184 		{
1185 			nv_get_private_data *gpd = (nv_get_private_data *)buf;
1186 			if (gpd->magic == NV_PRIVATE_DATA_MAGIC) {
1187 				gpd->shared_info_area = di->shared_area;
1188 				result = B_OK;
1189 			}
1190 			break;
1191 		}
1192 
1193 		case NV_GET_PCI:
1194 		{
1195 			nv_get_set_pci *gsp = (nv_get_set_pci *)buf;
1196 			if (gsp->magic == NV_PRIVATE_DATA_MAGIC) {
1197 				pci_info *pcii = &(di->pcii);
1198 				gsp->value = get_pci(gsp->offset, gsp->size);
1199 				result = B_OK;
1200 			}
1201 			break;
1202 		}
1203 
1204 		case NV_SET_PCI:
1205 		{
1206 			nv_get_set_pci *gsp = (nv_get_set_pci *)buf;
1207 			if (gsp->magic == NV_PRIVATE_DATA_MAGIC) {
1208 				pci_info *pcii = &(di->pcii);
1209 				set_pci(gsp->offset, gsp->size, gsp->value);
1210 				result = B_OK;
1211 			}
1212 			break;
1213 		}
1214 
1215 		case NV_DEVICE_NAME:
1216 		{
1217 			nv_device_name *dn = (nv_device_name *)buf;
1218 			if (dn->magic == NV_PRIVATE_DATA_MAGIC) {
1219 				strcpy(dn->name, di->name);
1220 				result = B_OK;
1221 			}
1222 			break;
1223 		}
1224 
1225 		case NV_RUN_INTERRUPTS:
1226 		{
1227 			nv_set_vblank_int *vi = (nv_set_vblank_int *)buf;
1228 			if (vi->magic == NV_PRIVATE_DATA_MAGIC) {
1229 				vuint32 *regs = di->regs;
1230 				if (!(vi->crtc)) {
1231 					if (vi->do_it) {
1232 						enable_vbi_crtc1(regs);
1233 					} else {
1234 						disable_vbi_crtc1(regs);
1235 					}
1236 				} else {
1237 					if (vi->do_it) {
1238 						enable_vbi_crtc2(regs);
1239 					} else {
1240 						disable_vbi_crtc2(regs);
1241 					}
1242 				}
1243 				result = B_OK;
1244 			}
1245 			break;
1246 		}
1247 
1248 		case NV_GET_NTH_AGP_INFO:
1249 		{
1250 			nv_nth_agp_info *nai = (nv_nth_agp_info *)buf;
1251 			if (nai->magic == NV_PRIVATE_DATA_MAGIC) {
1252 				nai->exist = false;
1253 				nai->agp_bus = false;
1254 				if (agp_bus) {
1255 					nai->agp_bus = true;
1256 					if ((*agp_bus->get_nth_agp_info)(nai->index, &(nai->agpi)) == B_NO_ERROR) {
1257 						nai->exist = true;
1258 					}
1259 				}
1260 				result = B_OK;
1261 			}
1262 			break;
1263 		}
1264 
1265 		case NV_ENABLE_AGP:
1266 		{
1267 			nv_cmd_agp *nca = (nv_cmd_agp *)buf;
1268 			if (nca->magic == NV_PRIVATE_DATA_MAGIC) {
1269 				if (agp_bus) {
1270 					nca->agp_bus = true;
1271 					nca->cmd = agp_bus->set_agp_mode(nca->cmd);
1272 				} else {
1273 					nca->agp_bus = false;
1274 					nca->cmd = 0;
1275 				}
1276 				result = B_OK;
1277 			}
1278 			break;
1279 		}
1280 
1281 		case NV_ISA_OUT:
1282 		{
1283 			nv_in_out_isa *io_isa = (nv_in_out_isa *)buf;
1284 			if (io_isa->magic == NV_PRIVATE_DATA_MAGIC) {
1285 				pci_info *pcii = &(di->pcii);
1286 
1287 				/* lock the driver:
1288 				 * no other graphics card may have ISA I/O enabled when we enter */
1289 				AQUIRE_BEN(pd->kernel);
1290 
1291 				/* enable ISA I/O access */
1292 				tmpUlong = get_pci(PCI_command, 2);
1293 				tmpUlong |= PCI_command_io;
1294 				set_pci(PCI_command, 2, tmpUlong);
1295 
1296 				if (io_isa->size == 1)
1297   					isa_bus->write_io_8(io_isa->adress, (uint8)io_isa->data);
1298    				else
1299    					isa_bus->write_io_16(io_isa->adress, io_isa->data);
1300   				result = B_OK;
1301 
1302 				/* disable ISA I/O access */
1303 				tmpUlong = get_pci(PCI_command, 2);
1304 				tmpUlong &= ~PCI_command_io;
1305 				set_pci(PCI_command, 2, tmpUlong);
1306 
1307 				/* end of critical section */
1308 				RELEASE_BEN(pd->kernel);
1309    			}
1310 			break;
1311 		}
1312 
1313 		case NV_ISA_IN:
1314 		{
1315 			nv_in_out_isa *io_isa = (nv_in_out_isa *)buf;
1316 			if (io_isa->magic == NV_PRIVATE_DATA_MAGIC) {
1317 				pci_info *pcii = &(di->pcii);
1318 
1319 				/* lock the driver:
1320 				 * no other graphics card may have ISA I/O enabled when we enter */
1321 				AQUIRE_BEN(pd->kernel);
1322 
1323 				/* enable ISA I/O access */
1324 				tmpUlong = get_pci(PCI_command, 2);
1325 				tmpUlong |= PCI_command_io;
1326 				set_pci(PCI_command, 2, tmpUlong);
1327 
1328 				if (io_isa->size == 1)
1329 	   				io_isa->data = isa_bus->read_io_8(io_isa->adress);
1330 	   			else
1331 	   				io_isa->data = isa_bus->read_io_16(io_isa->adress);
1332    				result = B_OK;
1333 
1334 				/* disable ISA I/O access */
1335 				tmpUlong = get_pci(PCI_command, 2);
1336 				tmpUlong &= ~PCI_command_io;
1337 				set_pci(PCI_command, 2, tmpUlong);
1338 
1339 				/* end of critical section */
1340 				RELEASE_BEN(pd->kernel);
1341    			}
1342 			break;
1343 		}
1344 	}
1345 
1346 	return result;
1347 }
1348 
1349 
1350 //	#pragma mark - driver API
1351 
1352 
1353 status_t
1354 init_hardware(void)
1355 {
1356 	long index = 0;
1357 	pci_info pcii;
1358 	bool found = false;
1359 
1360 	/* choke if we can't find the PCI bus */
1361 	if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK)
1362 		return B_ERROR;
1363 
1364 	/* choke if we can't find the ISA bus */
1365 	if (get_module(B_ISA_MODULE_NAME, (module_info **)&isa_bus) != B_OK)
1366 	{
1367 		put_module(B_PCI_MODULE_NAME);
1368 		return B_ERROR;
1369 	}
1370 
1371 	/* while there are more pci devices */
1372 	while ((*pci_bus->get_nth_pci_info)(index, &pcii) == B_NO_ERROR) {
1373 		int vendor = 0;
1374 
1375 		/* if we match a supported vendor */
1376 		while (SupportedDevices[vendor].vendor) {
1377 			if (SupportedDevices[vendor].vendor == pcii.vendor_id) {
1378 				uint16 *devices = SupportedDevices[vendor].devices;
1379 				/* while there are more supported devices */
1380 				while (*devices) {
1381 					/* if we match a supported device */
1382 					if (*devices == pcii.device_id ) {
1383 
1384 						found = true;
1385 						goto done;
1386 					}
1387 					/* next supported device */
1388 					devices++;
1389 				}
1390 			}
1391 			vendor++;
1392 		}
1393 		/* next pci_info struct, please */
1394 		index++;
1395 	}
1396 
1397 done:
1398 	/* put away the module manager */
1399 	put_module(B_PCI_MODULE_NAME);
1400 	return found ? B_OK : B_ERROR;
1401 }
1402 
1403 
1404 status_t
1405 init_driver(void)
1406 {
1407 	void *settings;
1408 
1409 	// get driver/accelerant settings
1410 	settings = load_driver_settings(DRIVER_PREFIX ".settings");
1411 	if (settings != NULL) {
1412 		const char *item;
1413 		char *end;
1414 		uint32 value;
1415 
1416 		// for driver
1417 		item = get_driver_parameter(settings, "accelerant", "", "");
1418 		if (item[0] && strlen(item) < sizeof(sSettings.accelerant) - 1)
1419 			strcpy (sSettings.accelerant, item);
1420 
1421 		item = get_driver_parameter(settings, "primary", "", "");
1422 		if (item[0] && strlen(item) < sizeof(sSettings.primary) - 1)
1423 			strcpy(sSettings.primary, item);
1424 
1425 		sSettings.dumprom = get_driver_boolean_parameter(settings,
1426 			"dumprom", false, false);
1427 
1428 		// for accelerant
1429 		item = get_driver_parameter(settings, "logmask",
1430 			"0x00000000", "0x00000000");
1431 		value = strtoul(item, &end, 0);
1432 		if (*end == '\0')
1433 			sSettings.logmask = value;
1434 
1435 		item = get_driver_parameter(settings, "memory", "0", "0");
1436 		value = strtoul(item, &end, 0);
1437 		if (*end == '\0')
1438 			sSettings.memory = value;
1439 
1440 		item = get_driver_parameter(settings, "tv_output", "0", "0");
1441 		value = strtoul(item, &end, 0);
1442 		if (*end == '\0')
1443 			sSettings.tv_output = value;
1444 
1445 		sSettings.hardcursor = get_driver_boolean_parameter(settings,
1446 			"hardcursor", true, true);
1447 		sSettings.usebios = get_driver_boolean_parameter(settings,
1448 			"usebios", true, true);
1449 		sSettings.switchhead = get_driver_boolean_parameter(settings,
1450 			"switchhead", false, false);
1451 		sSettings.force_pci = get_driver_boolean_parameter(settings,
1452 			"force_pci", false, false);
1453 		sSettings.unhide_fw = get_driver_boolean_parameter(settings,
1454 			"unhide_fw", false, false);
1455 		sSettings.pgm_panel = get_driver_boolean_parameter(settings,
1456 			"pgm_panel", false, false);
1457 		sSettings.dma_acc = get_driver_boolean_parameter(settings,
1458 			"dma_acc", true, true);
1459 		sSettings.vga_on_tv = get_driver_boolean_parameter(settings,
1460 			"vga_on_tv", false, false);
1461 		sSettings.force_sync = get_driver_boolean_parameter(settings,
1462 			"force_sync", false, false);
1463 		sSettings.force_ws = get_driver_boolean_parameter(settings,
1464 			"force_ws", false, false);
1465 		sSettings.block_acc = get_driver_boolean_parameter(settings,
1466 			"block_acc", false, false);
1467 		sSettings.check_edid = get_driver_boolean_parameter(settings,
1468 			"check_edid", true, true);
1469 
1470 		item = get_driver_parameter(settings, "gpu_clk", "0", "0");
1471 		value = strtoul(item, &end, 0);
1472 		if (*end == '\0')
1473 			sSettings.gpu_clk = value;
1474 
1475 		item = get_driver_parameter(settings, "ram_clk", "0", "0");
1476 		value = strtoul(item, &end, 0);
1477 		if (*end == '\0')
1478 			sSettings.ram_clk = value;
1479 
1480 		unload_driver_settings(settings);
1481 	}
1482 
1483 	/* get a handle for the pci bus */
1484 	if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK)
1485 		return B_ERROR;
1486 
1487 	/* get a handle for the isa bus */
1488 	if (get_module(B_ISA_MODULE_NAME, (module_info **)&isa_bus) != B_OK) {
1489 		put_module(B_PCI_MODULE_NAME);
1490 		return B_ERROR;
1491 	}
1492 
1493 	/* get a handle for the agp bus if it exists */
1494 	get_module(B_AGP_GART_MODULE_NAME, (module_info **)&agp_bus);
1495 
1496 	/* driver private data */
1497 	pd = (DeviceData *)calloc(1, sizeof(DeviceData));
1498 	if (!pd) {
1499 		put_module(B_PCI_MODULE_NAME);
1500 		return B_ERROR;
1501 	}
1502 	/* initialize the benaphore */
1503 	INIT_BEN(pd->kernel);
1504 	/* find all of our supported devices */
1505 	probe_devices();
1506 	return B_OK;
1507 }
1508 
1509 
1510 const char **
1511 publish_devices(void)
1512 {
1513 	/* return the list of supported devices */
1514 	return (const char **)pd->device_names;
1515 }
1516 
1517 
1518 device_hooks *
1519 find_device(const char *name)
1520 {
1521 	int index = 0;
1522 	while (pd->device_names[index]) {
1523 		if (strcmp(name, pd->device_names[index]) == 0)
1524 			return &graphics_device_hooks;
1525 		index++;
1526 	}
1527 	return NULL;
1528 
1529 }
1530 
1531 
1532 void
1533 uninit_driver(void)
1534 {
1535 	/* free the driver data */
1536 	DELETE_BEN(pd->kernel);
1537 	free(pd);
1538 	pd = NULL;
1539 
1540 	/* put the pci module away */
1541 	put_module(B_PCI_MODULE_NAME);
1542 	put_module(B_ISA_MODULE_NAME);
1543 
1544 	/* put the agp module away if it's there */
1545 	if (agp_bus)
1546 		put_module(B_AGP_GART_MODULE_NAME);
1547 }
1548 
1549