xref: /titanic_44/usr/src/uts/intel/io/agpgart/agptarget.c (revision a1e9eea083a8f257157edb8a1efb5bbd300eb4bf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 #include <sys/systm.h>
29 #include <sys/conf.h>
30 #include <sys/modctl.h>
31 #include <sys/file.h>
32 #include <sys/stat.h>
33 #include <sys/ddi.h>
34 #include <sys/sunddi.h>
35 #include <sys/modctl.h>
36 #include <sys/sunldi.h>
37 #include <sys/pci.h>
38 #include <sys/agpgart.h>
39 #include <sys/agp/agpdefs.h>
40 #include <sys/agp/agptarget_io.h>
41 
42 int agptarget_debug_var = 0;
43 #define	TARGETDB_PRINT2(fmt)	if (agptarget_debug_var >= 1) cmn_err fmt
44 #define	INST2NODENUM(inst)	(inst)
45 #define	DEV2INST(dev)		(getminor(dev))
46 
47 typedef struct agp_target_softstate {
48 	dev_info_t		*tsoft_dip;
49 	ddi_acc_handle_t	tsoft_pcihdl;
50 	uint32_t		tsoft_devid;
51 	/* The offset of the ACAPID register */
52 	off_t			tsoft_acaptr;
53 	kmutex_t		tsoft_lock;
54 	int			tsoft_gms_off; /* GMS offset in config */
55 	uint32_t		tsoft_gms;
56 }agp_target_softstate_t;
57 
58 /*
59  * To get the pre-allocated graphics mem size using Graphics Mode Select
60  * (GMS) value.
61  */
62 typedef struct gms_mode {
63 	uint32_t	gm_devid;	/* bridge vendor + device id */
64 	off_t		gm_regoff;	/* mode selection register offset */
65 	uint32_t	gm_mask;	/* GMS mask */
66 	uint32_t	gm_num;		/* number of modes in gm_vec */
67 	int 		*gm_vec;	/* modes array */
68 } gms_mode_t;
69 
70 static void *agptarget_glob_soft_handle;
71 
72 #define	GETSOFTC(instance)	((agp_target_softstate_t *)	\
73     ddi_get_soft_state(agptarget_glob_soft_handle, instance));
74 
75 /*
76  * The AMD8151 bridge is the only supported 64 bit hardware
77  */
78 static int
79 is_64bit_aper(agp_target_softstate_t *softstate)
80 {
81 	return (softstate->tsoft_devid == AMD_BR_8151);
82 }
83 
84 /*
85  * Check if it is an intel bridge
86  */
87 static int
88 is_intel_br(agp_target_softstate_t *softstate)
89 {
90 	return ((softstate->tsoft_devid & VENDOR_ID_MASK) ==
91 	    INTEL_VENDOR_ID);
92 }
93 
94 /*
95  * agp_target_cap_find()
96  *
97  * Description:
98  * 	This function searches the linked capability list to find the offset
99  * 	of the AGP capability register. When it was not found, return 0.
100  * 	This works for standard AGP chipsets, but not for some Intel chipsets,
101  * 	like the I830M/I830MP/I852PM/I852GME/I855GME. It will return 0 for
102  * 	these chipsets even if AGP is supported. So the offset of acapid
103  * 	should be set manually in thoses cases.
104  *
105  * Arguments:
106  * 	pci_handle		ddi acc handle of pci config
107  *
108  * Returns:
109  * 	0			No capability pointer register found
110  * 	nexcap			The AGP capability pointer register offset
111  */
112 static off_t
113 agp_target_cap_find(ddi_acc_handle_t pci_handle)
114 {
115 	off_t		nextcap = 0;
116 	uint32_t	ncapid = 0;
117 	uint8_t		value = 0;
118 
119 	/* Check if this device supports the capability pointer */
120 	value = (uint8_t)(pci_config_get16(pci_handle, PCI_CONF_STAT)
121 	    & PCI_CONF_CAP_MASK);
122 
123 	if (!value)
124 		return (0);
125 	/* Get the offset of the first capability pointer from CAPPTR */
126 	nextcap = (off_t)(pci_config_get8(pci_handle, AGP_CONF_CAPPTR));
127 
128 	/* Check the AGP capability from the first capability pointer */
129 	while (nextcap) {
130 		ncapid = pci_config_get32(pci_handle, nextcap);
131 		/*
132 		 * AGP3.0 rev1.0 127  the capid was assigned by the PCI SIG,
133 		 * 845 data sheet page 69
134 		 */
135 		if ((ncapid & PCI_CONF_CAPID_MASK) ==
136 		    AGP_CAP_ID) /* The AGP cap was found */
137 			break;
138 
139 		nextcap = (off_t)((ncapid & PCI_CONF_NCAPID_MASK) >> 8);
140 	}
141 
142 	return (nextcap);
143 
144 }
145 
146 /*
147  * agp_target_get_aperbase()
148  *
149  * Description:
150  * 	This function gets the AGP aperture base address from the AGP target
151  *	register, the AGP aperture base register was programmed by the BIOS.
152  *
153  * Arguments:
154  * 	softstate		driver soft state pointer
155  *
156  * Returns:
157  * 	aper_base 		AGP aperture base address
158  *
159  * Notes:
160  * 	If a 64bit bridge device is available, the AGP aperture base address
161  * 	can be 64 bit.
162  */
163 static uint64_t
164 agp_target_get_apbase(agp_target_softstate_t *softstate)
165 {
166 	uint64_t aper_base;
167 
168 	if (is_intel_br(softstate)) {
169 		aper_base = pci_config_get32(softstate->tsoft_pcihdl,
170 		    AGP_CONF_APERBASE) & AGP_32_APERBASE_MASK;
171 	} else if (is_64bit_aper(softstate)) {
172 		aper_base = pci_config_get64(softstate->tsoft_pcihdl,
173 		    AGP_CONF_APERBASE);
174 		/* 32-bit or 64-bit aperbase base pointer */
175 		if ((aper_base & AGP_APER_TYPE_MASK) == 0)
176 			aper_base &= AGP_32_APERBASE_MASK;
177 		else
178 			aper_base &= AGP_64_APERBASE_MASK;
179 	}
180 
181 	return (aper_base);
182 }
183 
184 /*
185  * agp_target_get_apsize()
186  *
187  * Description:
188  * 	This function gets the AGP aperture size by reading the AGP aperture
189  * 	size register.
190  * Arguments:
191  * 	softstate		driver soft state pointer
192  *
193  * Return:
194  * 	size		The AGP aperture size in megabytes
195  * 	0		an unexpected error
196  */
197 static size_t
198 agp_target_get_apsize(agp_target_softstate_t *softstate)
199 {
200 	off_t cap;
201 	uint16_t value;
202 	size_t size, regsize;
203 
204 	ASSERT(softstate->tsoft_acaptr);
205 	cap = softstate->tsoft_acaptr;
206 
207 	if (is_intel_br(softstate)) {
208 		/* extend this value to 16 bit for later tests */
209 		value = (uint16_t)pci_config_get8(softstate->tsoft_pcihdl,
210 		    cap + AGP_CONF_APERSIZE) | AGP_APER_SIZE_MASK;
211 	} else if (is_64bit_aper(softstate)) {
212 		value = pci_config_get16(softstate->tsoft_pcihdl,
213 		    cap + AGP_CONF_APERSIZE);
214 	}
215 
216 	if (value & AGP_APER_128M_MASK) {
217 		switch (value & AGP_APER_128M_MASK) {
218 			case AGP_APER_4M:
219 				size = 4; /* 4M */
220 				break;
221 			case AGP_APER_8M:
222 				size = 8; /* 8M */
223 				break;
224 			case AGP_APER_16M:
225 				size = 16; /* 16M */
226 				break;
227 			case AGP_APER_32M:
228 				size = 32; /* 32M */
229 				break;
230 			case AGP_APER_64M:
231 				size = 64; /* 64M */
232 				break;
233 			case AGP_APER_128M:
234 				size = 128; /* 128M */
235 				break;
236 			default:
237 				size = 0; /* not true */
238 		}
239 	} else {
240 		switch (value & AGP_APER_4G_MASK) {
241 			case AGP_APER_256M:
242 				size = 256; /* 256 M */
243 				break;
244 			case AGP_APER_512M:
245 				size = 512; /* 512 M */
246 				break;
247 			case AGP_APER_1024M:
248 				size = 1024; /* 1024 M */
249 				break;
250 			case AGP_APER_2048M:
251 				size = 2048; /* 2048 M */
252 				break;
253 			case AGP_APER_4G:
254 				size = 4096; /* 4096 M */
255 				break;
256 			default:
257 				size = 0; /* not true */
258 		}
259 	}
260 	/*
261 	 * In some cases, there is no APSIZE register, so the size value
262 	 * of 256M could be wrong. Check the value by reading the size of
263 	 * the first register which was set in the PCI configuration space.
264 	 */
265 	if (size == 256) {
266 		if (ddi_dev_regsize(softstate->tsoft_dip,
267 		    AGP_TARGET_BAR1, (off_t *)&regsize) == DDI_FAILURE)
268 			return (0);
269 
270 		if (MB2BYTES(size) != regsize) {
271 			TARGETDB_PRINT2((CE_WARN,
272 			    "APSIZE 256M doesn't match regsize %lx",
273 			    regsize));
274 			TARGETDB_PRINT2((CE_WARN, "Use regsize instead"));
275 			size = BYTES2MB(regsize);
276 		}
277 	}
278 
279 	return (size);
280 }
281 
282 static void
283 agp_target_set_gartaddr(agp_target_softstate_t *softstate, uint32_t gartaddr)
284 {
285 	ASSERT(softstate->tsoft_acaptr);
286 
287 	/* Disable the GTLB for Intel chipsets */
288 	pci_config_put16(softstate->tsoft_pcihdl,
289 	    softstate->tsoft_acaptr + AGP_CONF_CONTROL, 0x0000);
290 
291 	pci_config_put32(softstate->tsoft_pcihdl,
292 	    softstate->tsoft_acaptr + AGP_CONF_ATTBASE,
293 	    gartaddr & AGP_ATTBASE_MASK);
294 }
295 
296 /*
297  * Pre-allocated graphics memory for every type of Intel north bridge, mem size
298  * are specified in kbytes.
299  */
300 #define	GMS_MB(n) 	((n) * 1024)
301 #define	GMS_SHIFT 	4
302 #define	GMS_SIZE(a)	(sizeof (a) / sizeof (int))
303 
304 /*
305  * Since value zero always means "No memory pre-allocated", value of (GMS - 1)
306  * is used to index these arrays, i.e. gms_xxx[1] contains the mem size (in kb)
307  * that GMS value 0x1 corresponding to.
308  *
309  * Assuming all "reserved" GMS value as zero bytes of pre-allocated graphics
310  * memory, unless some special BIOS settings exist.
311  */
312 static int gms_810[12] = {0, 0, 0, 0, 0, 0, 0, 512, 0, 0, 0, GMS_MB(1)};
313 static int gms_830_845[4] = {0, 512, GMS_MB(1), GMS_MB(8)};
314 static int gms_855GM[5] = {GMS_MB(1), GMS_MB(4), GMS_MB(8), GMS_MB(16),
315 	GMS_MB(32)};
316 /* There is no modes for 16M in datasheet, but some BIOS add it. */
317 static int gms_865_915GM[4] = {GMS_MB(1), 0, GMS_MB(8), GMS_MB(16)};
318 static int gms_915_945_965[3] = {GMS_MB(1), 0, GMS_MB(8)};
319 static int gms_965GM[7] = {GMS_MB(1), GMS_MB(4), GMS_MB(8), GMS_MB(16),
320 	GMS_MB(32), GMS_MB(48), GMS_MB(64)};
321 static int gms_X33[9] = {GMS_MB(1), GMS_MB(4), GMS_MB(8), GMS_MB(16),
322 	GMS_MB(32), GMS_MB(48), GMS_MB(64), GMS_MB(128), GMS_MB(256)};
323 static int gms_G4X[13] = {0, 0, 0, 0,
324 	GMS_MB(32), GMS_MB(48), GMS_MB(64), GMS_MB(128), GMS_MB(256),
325 	GMS_MB(96), GMS_MB(160), GMS_MB(224), GMS_MB(352)};
326 
327 static gms_mode_t gms_modes[] = {
328 	{INTEL_BR_810, I810_CONF_SMRAM, I810_GMS_MASK,
329 		GMS_SIZE(gms_810), gms_810},
330 	{INTEL_BR_810DC, I810_CONF_SMRAM, I810_GMS_MASK,
331 		GMS_SIZE(gms_810), gms_810},
332 	{INTEL_BR_810E, I810_CONF_SMRAM, I810_GMS_MASK,
333 		GMS_SIZE(gms_810), gms_810},
334 	{INTEL_BR_830M, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
335 		GMS_SIZE(gms_830_845), gms_830_845},
336 	{INTEL_BR_845, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
337 		GMS_SIZE(gms_830_845), gms_830_845},
338 	{INTEL_BR_855GM, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
339 		GMS_SIZE(gms_855GM), gms_855GM},
340 	{INTEL_BR_865, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
341 		GMS_SIZE(gms_865_915GM), gms_865_915GM},
342 	{INTEL_BR_915GM, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
343 		GMS_SIZE(gms_865_915GM), gms_865_915GM},
344 	{INTEL_BR_915, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
345 		GMS_SIZE(gms_915_945_965), gms_915_945_965},
346 	{INTEL_BR_945, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
347 		GMS_SIZE(gms_915_945_965), gms_915_945_965},
348 	{INTEL_BR_945GM, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
349 		GMS_SIZE(gms_915_945_965), gms_915_945_965},
350 	{INTEL_BR_945GME, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
351 		GMS_SIZE(gms_915_945_965), gms_915_945_965},
352 	{INTEL_BR_946GZ, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
353 		GMS_SIZE(gms_915_945_965), gms_915_945_965},
354 	{INTEL_BR_965G1, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
355 		GMS_SIZE(gms_915_945_965), gms_915_945_965},
356 	{INTEL_BR_965G2, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
357 		GMS_SIZE(gms_915_945_965), gms_915_945_965},
358 	{INTEL_BR_965Q, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
359 		GMS_SIZE(gms_915_945_965), gms_915_945_965},
360 	{INTEL_BR_965GM, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
361 		GMS_SIZE(gms_965GM), gms_965GM},
362 	{INTEL_BR_965GME, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
363 		GMS_SIZE(gms_965GM), gms_965GM},
364 	{INTEL_BR_Q35, I8XX_CONF_GC, IX33_GC_MODE_MASK,
365 		GMS_SIZE(gms_X33), gms_X33},
366 	{INTEL_BR_G33, I8XX_CONF_GC, IX33_GC_MODE_MASK,
367 		GMS_SIZE(gms_X33), gms_X33},
368 	{INTEL_BR_Q33, I8XX_CONF_GC, IX33_GC_MODE_MASK,
369 		GMS_SIZE(gms_X33), gms_X33},
370 	{INTEL_BR_GM45, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
371 		GMS_SIZE(gms_965GM), gms_965GM},
372 	{INTEL_BR_EL, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
373 		GMS_SIZE(gms_G4X), gms_G4X},
374 	{INTEL_BR_Q45, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
375 		GMS_SIZE(gms_G4X), gms_G4X},
376 	{INTEL_BR_G45, I8XX_CONF_GC, I8XX_GC_MODE_MASK,
377 		GMS_SIZE(gms_G4X), gms_G4X}
378 };
379 static int
380 get_chip_gms(uint32_t devid)
381 {
382 	int num_modes;
383 	int i;
384 
385 	num_modes = (sizeof (gms_modes) / sizeof (gms_mode_t));
386 
387 	for (i = 0; i < num_modes; i++) {
388 		if (gms_modes[i].gm_devid == devid)
389 			break;
390 	}
391 
392 	return ((i == num_modes) ? -1 : i);
393 }
394 
395 /* Returns the size (kbytes) of pre-allocated graphics memory */
396 static size_t
397 i8xx_biosmem_detect(agp_target_softstate_t *softstate)
398 {
399 	uint8_t memval;
400 	size_t kbytes;
401 	int	gms_off;
402 
403 	kbytes = 0;
404 	gms_off = softstate->tsoft_gms_off;
405 
406 	/* fetch the GMS value from DRAM controller */
407 	memval = pci_config_get8(softstate->tsoft_pcihdl,
408 	    gms_modes[gms_off].gm_regoff);
409 	TARGETDB_PRINT2((CE_NOTE, "i8xx_biosmem_detect: memval = %x", memval));
410 	memval = (memval & gms_modes[gms_off].gm_mask) >> GMS_SHIFT;
411 	/* assuming zero byte for 0 or "reserved" GMS values */
412 	if (memval == 0 || memval > gms_modes[gms_off].gm_num) {
413 		TARGETDB_PRINT2((CE_WARN, "i8xx_biosmem_detect: "
414 		    "devid = %x, GMS = %x. assuming zero byte of "
415 		    "pre-allocated memory",
416 		    gms_modes[gms_off].gm_devid, memval));
417 		goto done;
418 	}
419 	memval--;	/* use (GMS_value - 1) as index */
420 	kbytes = (gms_modes[gms_off].gm_vec)[memval];
421 
422 done:
423 	TARGETDB_PRINT2((CE_NOTE,
424 	    "i8xx_biosmem_detect: %ldKB BIOS pre-allocated memory detected",
425 	    kbytes));
426 	return (kbytes);
427 }
428 
429 /*ARGSUSED*/
430 static int agptarget_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
431     void *arg, void **resultp)
432 {
433 	agp_target_softstate_t *st;
434 	int instance, rval = DDI_FAILURE;
435 	dev_t dev;
436 
437 	switch (cmd) {
438 	case DDI_INFO_DEVT2DEVINFO:
439 		dev = (dev_t)arg;
440 		instance = DEV2INST(dev);
441 		st = ddi_get_soft_state(agptarget_glob_soft_handle, instance);
442 		if (st != NULL) {
443 			mutex_enter(&st->tsoft_lock);
444 			*resultp = st->tsoft_dip;
445 			mutex_exit(&st->tsoft_lock);
446 			rval = DDI_SUCCESS;
447 		} else
448 			*resultp = NULL;
449 
450 		break;
451 	case DDI_INFO_DEVT2INSTANCE:
452 		dev = (dev_t)arg;
453 		instance = DEV2INST(dev);
454 		*resultp = (void *)(uintptr_t)instance;
455 		rval = DDI_SUCCESS;
456 	default:
457 		break;
458 	}
459 
460 	return (rval);
461 }
462 
463 static int
464 intel_br_resume(agp_target_softstate_t *softstate)
465 {
466 	int gms_off;
467 
468 	gms_off = softstate->tsoft_gms_off;
469 
470 	/*
471 	 * We recover the gmch graphics control register here
472 	 */
473 	pci_config_put16(softstate->tsoft_pcihdl,
474 	    gms_modes[gms_off].gm_regoff, softstate->tsoft_gms);
475 
476 	return (DDI_SUCCESS);
477 }
478 static int
479 intel_br_suspend(agp_target_softstate_t *softstate)
480 {
481 	int gms_off;
482 
483 	gms_off = softstate->tsoft_gms_off;
484 	softstate->tsoft_gms = pci_config_get16(softstate->tsoft_pcihdl,
485 	    gms_modes[gms_off].gm_regoff);
486 
487 	return (DDI_SUCCESS);
488 }
489 
490 static int
491 agp_target_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
492 {
493 	agp_target_softstate_t *softstate;
494 	int instance;
495 	int status;
496 
497 	instance = ddi_get_instance(dip);
498 
499 	switch (cmd) {
500 	case DDI_ATTACH:
501 		break;
502 	case DDI_RESUME:
503 		softstate =
504 		    ddi_get_soft_state(agptarget_glob_soft_handle, instance);
505 		return (intel_br_resume(softstate));
506 	default:
507 		TARGETDB_PRINT2((CE_WARN, "agp_target_attach:"
508 		    "only attach and resume ops are supported"));
509 		return (DDI_FAILURE);
510 	}
511 
512 	if (ddi_soft_state_zalloc(agptarget_glob_soft_handle,
513 	    instance) != DDI_SUCCESS) {
514 		TARGETDB_PRINT2((CE_WARN, "agp_target_attach:"
515 		    "soft state zalloc failed"));
516 		return (DDI_FAILURE);
517 	}
518 
519 	softstate = ddi_get_soft_state(agptarget_glob_soft_handle, instance);
520 	mutex_init(&softstate->tsoft_lock, NULL, MUTEX_DRIVER, NULL);
521 	softstate->tsoft_dip = dip;
522 	status = pci_config_setup(dip, &softstate->tsoft_pcihdl);
523 	if (status != DDI_SUCCESS) {
524 		TARGETDB_PRINT2((CE_WARN, "agp_target_attach:"
525 		    "pci config setup failed"));
526 		ddi_soft_state_free(agptarget_glob_soft_handle,
527 		    instance);
528 		return (DDI_FAILURE);
529 	}
530 
531 	softstate->tsoft_devid = pci_config_get32(softstate->tsoft_pcihdl,
532 	    PCI_CONF_VENID);
533 	softstate->tsoft_gms_off = get_chip_gms(softstate->tsoft_devid);
534 	if (softstate->tsoft_gms_off < 0) {
535 		TARGETDB_PRINT2((CE_WARN, "agp_target_attach:"
536 		    "read gms offset failed"));
537 		pci_config_teardown(&softstate->tsoft_pcihdl);
538 		ddi_soft_state_free(agptarget_glob_soft_handle,
539 		    instance);
540 		return (DDI_FAILURE);
541 	}
542 	softstate->tsoft_acaptr = agp_target_cap_find(softstate->tsoft_pcihdl);
543 	if (softstate->tsoft_acaptr == 0) {
544 		/* Make a correction for some Intel chipsets */
545 		if (is_intel_br(softstate))
546 			softstate->tsoft_acaptr = AGP_CAP_OFF_DEF;
547 		else {
548 			TARGETDB_PRINT2((CE_WARN, "agp_target_attach:"
549 			    "Not a supposed corretion"));
550 			pci_config_teardown(&softstate->tsoft_pcihdl);
551 			ddi_soft_state_free(agptarget_glob_soft_handle,
552 			    instance);
553 			return (DDI_FAILURE);
554 		}
555 	}
556 
557 	status = ddi_create_minor_node(dip, AGPTARGET_NAME, S_IFCHR,
558 	    INST2NODENUM(instance), DDI_NT_AGP_TARGET, 0);
559 
560 	if (status != DDI_SUCCESS) {
561 		TARGETDB_PRINT2((CE_WARN, "agp_target_attach:"
562 		    "Create minor node failed"));
563 		pci_config_teardown(&softstate->tsoft_pcihdl);
564 		ddi_soft_state_free(agptarget_glob_soft_handle, instance);
565 		return (DDI_FAILURE);
566 	}
567 
568 	return (DDI_SUCCESS);
569 }
570 
571 /*ARGSUSED*/
572 static int
573 agp_target_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
574 {
575 	int instance;
576 	agp_target_softstate_t *softstate;
577 
578 	instance = ddi_get_instance(dip);
579 	softstate = ddi_get_soft_state(agptarget_glob_soft_handle, instance);
580 
581 	if (cmd == DDI_SUSPEND) {
582 		/* get GMS modes list entry */
583 		return (intel_br_suspend(softstate));
584 	}
585 
586 	if (cmd != DDI_DETACH) {
587 		TARGETDB_PRINT2((CE_WARN, "agp_target_detach:"
588 		    "only detach and suspend ops are supported"));
589 		return (DDI_FAILURE);
590 	}
591 
592 	ddi_remove_minor_node(dip, AGPTARGET_NAME);
593 	pci_config_teardown(&softstate->tsoft_pcihdl);
594 	mutex_destroy(&softstate->tsoft_lock);
595 	ddi_soft_state_free(agptarget_glob_soft_handle, instance);
596 	return (DDI_SUCCESS);
597 }
598 
599 /*ARGSUSED*/
600 static int
601 agp_target_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
602     cred_t *cred, int *rval)
603 {
604 	int instance = DEV2INST(dev);
605 	agp_target_softstate_t *st;
606 	static char kernel_only[] =
607 	    "amd64_gart_ioctl: is a kernel only ioctl";
608 
609 	if (!(mode & FKIOCTL)) {
610 		TARGETDB_PRINT2((CE_CONT, kernel_only));
611 		return (ENXIO);
612 	}
613 	st = GETSOFTC(instance);
614 
615 	if (st == NULL)
616 		return (ENXIO);
617 
618 	mutex_enter(&st->tsoft_lock);
619 
620 	switch (cmd) {
621 	case CHIP_DETECT:
622 	{
623 		int type = 0;
624 
625 		if (is_intel_br(st))
626 			type = CHIP_IS_INTEL;
627 		else if (is_64bit_aper(st))
628 			type = CHIP_IS_AMD;
629 		else {
630 			type = 0;
631 			TARGETDB_PRINT2((CE_WARN, "Unknown bridge!"));
632 		}
633 
634 		if (ddi_copyout(&type, (void *)data, sizeof (int), mode)) {
635 			mutex_exit(&st->tsoft_lock);
636 			return (EFAULT);
637 		}
638 
639 		break;
640 	}
641 	case I8XX_GET_PREALLOC_SIZE:
642 	{
643 		size_t prealloc_size;
644 
645 		if (!is_intel_br(st)) {
646 			mutex_exit(&st->tsoft_lock);
647 			return (EINVAL);
648 		}
649 
650 		prealloc_size = i8xx_biosmem_detect(st);
651 		if (ddi_copyout(&prealloc_size, (void *)data,
652 		    sizeof (size_t), mode)) {
653 			mutex_exit(&st->tsoft_lock);
654 			return (EFAULT);
655 		}
656 
657 		break;
658 	}
659 	case AGP_TARGET_GETINFO:
660 	{
661 		i_agp_info_t info;
662 		uint32_t value;
663 		off_t cap;
664 
665 		ASSERT(st->tsoft_acaptr);
666 
667 		cap = st->tsoft_acaptr;
668 		value = pci_config_get32(st->tsoft_pcihdl, cap);
669 		info.iagp_ver.agpv_major = (uint16_t)((value >> 20) & 0xf);
670 		info.iagp_ver.agpv_minor = (uint16_t)((value >> 16) & 0xf);
671 		info.iagp_devid = st->tsoft_devid;
672 		info.iagp_mode = pci_config_get32(st->tsoft_pcihdl,
673 		    cap + AGP_CONF_STATUS);
674 		info.iagp_aperbase = agp_target_get_apbase(st);
675 		info.iagp_apersize = agp_target_get_apsize(st);
676 
677 		if (ddi_copyout(&info, (void *)data,
678 		    sizeof (i_agp_info_t), mode)) {
679 			mutex_exit(&st->tsoft_lock);
680 			return (EFAULT);
681 		}
682 		break;
683 
684 	}
685 	/*
686 	 * This ioctl is only for Intel AGP chipsets.
687 	 * It is not necessary for the AMD8151 AGP bridge, because
688 	 * this register in the AMD8151 does not control any hardware.
689 	 * It is only provided for compatibility with an Intel AGP bridge.
690 	 * Please refer to the <<AMD8151 data sheet>> page 24,
691 	 * AGP device GART pointer.
692 	 */
693 	case AGP_TARGET_SET_GATTADDR:
694 	{
695 		uint32_t gartaddr;
696 
697 		if (ddi_copyin((void *)data, &gartaddr,
698 		    sizeof (uint32_t), mode)) {
699 			mutex_exit(&st->tsoft_lock);
700 			return (EFAULT);
701 		}
702 
703 		agp_target_set_gartaddr(st, gartaddr);
704 		break;
705 	}
706 	case AGP_TARGET_SETCMD:
707 	{
708 		uint32_t command;
709 
710 		if (ddi_copyin((void *)data, &command,
711 		    sizeof (uint32_t), mode)) {
712 			mutex_exit(&st->tsoft_lock);
713 			return (EFAULT);
714 		}
715 
716 		ASSERT(st->tsoft_acaptr);
717 
718 		pci_config_put32(st->tsoft_pcihdl,
719 		    st->tsoft_acaptr + AGP_CONF_COMMAND,
720 		    command);
721 		break;
722 
723 	}
724 	case AGP_TARGET_FLUSH_GTLB:
725 	{
726 		uint16_t value;
727 
728 		ASSERT(st->tsoft_acaptr);
729 
730 		value = pci_config_get16(st->tsoft_pcihdl,
731 		    st->tsoft_acaptr + AGP_CONF_CONTROL);
732 		value &= ~AGPCTRL_GTLBEN;
733 		pci_config_put16(st->tsoft_pcihdl,
734 		    st->tsoft_acaptr + AGP_CONF_CONTROL, value);
735 		value |= AGPCTRL_GTLBEN;
736 		pci_config_put16(st->tsoft_pcihdl,
737 		    st->tsoft_acaptr + AGP_CONF_CONTROL, value);
738 
739 		break;
740 	}
741 	case AGP_TARGET_CONFIGURE:
742 	{
743 		uint8_t value;
744 
745 		ASSERT(st->tsoft_acaptr);
746 
747 		/*
748 		 * In Intel agp bridges, agp misc register offset
749 		 * is indexed from 0 instead of capability register.
750 		 * AMD agp bridges have no such misc register
751 		 * to control the aperture access, and they have
752 		 * similar regsiters in CPU gart devices instead.
753 		 */
754 
755 		if (is_intel_br(st)) {
756 			value = pci_config_get8(st->tsoft_pcihdl,
757 			    st->tsoft_acaptr + AGP_CONF_MISC);
758 			value |= AGP_MISC_APEN;
759 			pci_config_put8(st->tsoft_pcihdl,
760 			    st->tsoft_acaptr + AGP_CONF_MISC, value);
761 		}
762 		break;
763 
764 	}
765 	case AGP_TARGET_UNCONFIG:
766 	{
767 		uint32_t value1;
768 		uint8_t value2;
769 
770 		ASSERT(st->tsoft_acaptr);
771 
772 		pci_config_put16(st->tsoft_pcihdl,
773 		    st->tsoft_acaptr + AGP_CONF_CONTROL, 0x0);
774 
775 		if (is_intel_br(st)) {
776 			value2 = pci_config_get8(st->tsoft_pcihdl,
777 			    st->tsoft_acaptr + AGP_CONF_MISC);
778 			value2 &= ~AGP_MISC_APEN;
779 			pci_config_put8(st->tsoft_pcihdl,
780 			    st->tsoft_acaptr + AGP_CONF_MISC, value2);
781 		}
782 
783 		value1 = pci_config_get32(st->tsoft_pcihdl,
784 		    st->tsoft_acaptr + AGP_CONF_COMMAND);
785 		value1 &= ~AGPCMD_AGPEN;
786 		pci_config_put32(st->tsoft_pcihdl,
787 		    st->tsoft_acaptr + AGP_CONF_COMMAND,
788 		    value1);
789 
790 		pci_config_put32(st->tsoft_pcihdl,
791 		    st->tsoft_acaptr + AGP_CONF_ATTBASE, 0x0);
792 
793 		break;
794 	}
795 
796 	default:
797 		mutex_exit(&st->tsoft_lock);
798 		return (ENXIO);
799 	} /* end switch */
800 
801 	mutex_exit(&st->tsoft_lock);
802 
803 	return (0);
804 }
805 
806 /*ARGSUSED*/
807 static int
808 agp_target_open(dev_t *devp, int flag, int otyp, cred_t *cred)
809 {
810 	int instance = DEV2INST(*devp);
811 	agp_target_softstate_t *st;
812 
813 	if (!(flag & FKLYR))
814 		return (ENXIO);
815 
816 	st = GETSOFTC(instance);
817 
818 	if (st == NULL)
819 		return (ENXIO);
820 
821 	return (0);
822 }
823 
824 /*ARGSUSED*/
825 static int
826 agp_target_close(dev_t dev, int flag, int otyp, cred_t *cred)
827 {
828 	int instance = DEV2INST(dev);
829 	agp_target_softstate_t *st;
830 
831 	st = GETSOFTC(instance);
832 
833 	if (st == NULL)
834 		return (ENXIO);
835 
836 	return (0);
837 }
838 
839 static  struct  cb_ops  agp_target_cb_ops = {
840 	agp_target_open,		/* cb_open */
841 	agp_target_close,		/* cb_close */
842 	nodev,				/* cb_strategy */
843 	nodev,				/* cb_print */
844 	nodev,				/* cb_dump */
845 	nodev,				/* cb_read() */
846 	nodev,				/* cb_write() */
847 	agp_target_ioctl,		/* cb_ioctl */
848 	nodev,				/* cb_devmap */
849 	nodev,				/* cb_mmap */
850 	nodev,				/* cb_segmap */
851 	nochpoll,			/* cb_chpoll */
852 	ddi_prop_op,			/* cb_prop_op */
853 	0,				/* cb_stream */
854 	D_NEW | D_MP, 			/* cb_flag */
855 	CB_REV,				/* cb_ops version? */
856 	nodev,				/* cb_aread() */
857 	nodev,				/* cb_awrite() */
858 };
859 
860 /* device operations */
861 static struct dev_ops agp_target_ops = {
862 	DEVO_REV,		/* devo_rev */
863 	0,			/* devo_refcnt */
864 	agptarget_getinfo, 	/* devo_getinfo */
865 	nulldev,		/* devo_identify */
866 	nulldev,		/* devo_probe */
867 	agp_target_attach,	/* devo_attach */
868 	agp_target_detach,	/* devo_detach */
869 	nodev,			/* devo_reset */
870 	&agp_target_cb_ops,	/* devo_cb_ops */
871 	0,			/* devo_bus_ops */
872 	0,			/* devo_power */
873 	ddi_quiesce_not_needed,	/* devo_quiesce */
874 };
875 
876 static  struct modldrv modldrv = {
877 	&mod_driverops,
878 	"AGP target driver",
879 	&agp_target_ops,
880 };
881 
882 static  struct modlinkage modlinkage = {
883 	MODREV_1,		/* MODREV_1 is indicated by manual */
884 	{&modldrv, NULL, NULL, NULL}
885 };
886 
887 int
888 _init(void)
889 {
890 	int ret;
891 
892 	ret = ddi_soft_state_init(&agptarget_glob_soft_handle,
893 	    sizeof (agp_target_softstate_t), 1);
894 
895 	if (ret)
896 		goto err1;
897 
898 	if ((ret = mod_install(&modlinkage)) != 0) {
899 		goto err2;
900 	}
901 
902 	return (DDI_SUCCESS);
903 err2:
904 	ddi_soft_state_fini(&agptarget_glob_soft_handle);
905 err1:
906 	return (ret);
907 }
908 
909 int
910 _info(struct  modinfo *modinfop)
911 {
912 	return (mod_info(&modlinkage, modinfop));
913 }
914 
915 int
916 _fini(void)
917 {
918 	int	ret;
919 
920 	if ((ret = mod_remove(&modlinkage)) == 0) {
921 		ddi_soft_state_fini(&agptarget_glob_soft_handle);
922 	}
923 	return (ret);
924 }
925