Subject: Re: Intel i845G on-board graphics
To: None <danielb@cat.co.za>
From: Shingo WATANABE / =?ISO-2022-JP?B?GyRCRU9KVRsoQiAbJEI/LThjGyhC?= <nabe@nabechan.org>
List: tech-kern
Date: 12/04/2002 01:38:35
Hello.

The patch for i830/845G agp driver is attached in this mail.

At Tue, 3 Dec 2002 17:08:47 +0200,
"Daniel Brewer" <danielb@cat.co.za> wrote:

> Sorry for the cross-post, but I'm having trouble getting X server to
> recognize the on-boad i845G graphics card. It seems to identify that there
> is an Intel graphics chip, but can't identify the exact part number.

How is the agp driver recognized?

> I'm running v1.5.1 and have checked out & built the XFree86 4.2 branch. I'm
> still looking and trying things, but has anyone had similar problems? Or has
> someone found a solution?

I've just written the agp driver for i830/845G. The patch is
attached for 1.6-current.  The PC is under repair, so I did not test
well, sorry.   Please try it and send comments to me.

I don't know you can use this patch in 1.5.1-release. 


dmesg from ThinkPad X30 (i830MP)
...
pchb0 at pci0 dev 0 function 0
pchb0: Intel 82830MP CPU to I/O Bridge 1 (rev. 0x04)
agp0 at pchb0: detected 8060k stolen memory
agp0: aperture size is 128M
agp0: aperture at 0xe0000000, size 0x8000000
vga1 at pci0 dev 2 function 0: Intel 82830MP Integrated Video (rev. 0x04)
wsdisplay0 at vga1 kbdmux 1: console (80x25, vt100 emulation)
...




diff -urp /home/nabe/work/cvs/NetBSD/src/sys/dev/pci/agp.c dev/pci/agp.c
--- /home/nabe/work/cvs/NetBSD/src/sys/dev/pci/agp.c	Fri Oct 25 01:15:59 2002
+++ dev/pci/agp.c	Tue Dec  3 13:34:20 2002
@@ -151,11 +151,10 @@ const struct agp_product {
 	  NULL,			agp_i810_attach },
 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82840_HB,
 	  NULL,			agp_i810_attach },
-#if 0
-/* XXX needs somewhat different driver */
 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82830MP_IO_1,
 	  NULL,			agp_i810_attach },
-#endif
+	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82845G_DRAM,
+	  NULL,			agp_i810_attach },
 #endif
 
 #if NAGP_INTEL > 0
diff -urp /home/nabe/work/cvs/NetBSD/src/sys/dev/pci/agp_i810.c dev/pci/agp_i810.c
--- /home/nabe/work/cvs/NetBSD/src/sys/dev/pci/agp_i810.c	Thu Aug 15 14:51:39 2002
+++ dev/pci/agp_i810.c	Tue Dec  3 19:59:39 2002
@@ -54,12 +54,18 @@ __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v
 #include <machine/bus.h>
 
 #define READ1(off)	bus_space_read_1(isc->bst, isc->bsh, off)
+#define READ4(off)	bus_space_read_4(isc->bst, isc->bsh, off)
 #define WRITE4(off,v)	bus_space_write_4(isc->bst, isc->bsh, off, v)
 
+#define CHIP_I810 0	/* i810/i815 */
+#define CHIP_I830 1	/* i830/i845 */
+
 struct agp_i810_softc {
 	u_int32_t initial_aperture;	/* aperture size at startup */
 	struct agp_gatt *gatt;
-	u_int32_t dcache_size;
+	int chiptype;			/* i810-like or i830 */
+	u_int32_t dcache_size;		/* i810 only */
+	u_int32_t stolen;		/* number of i830/845 gtt entries for stolen memory */
 	bus_space_tag_t bst;		/* bus_space tag */
 	bus_space_handle_t bsh;		/* bus_space handle */
 	struct pci_attach_args vga_pa;
@@ -104,7 +110,8 @@ agp_i810_vgamatch(struct pci_attach_args
 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
 	case PCI_PRODUCT_INTEL_82810E_GC:
 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
-	/*case PCI_PRODUCT_INTEL_82830MP_IV: XXX needs somewhat different driver */
+	case PCI_PRODUCT_INTEL_82830MP_IV:
+	case PCI_PRODUCT_INTEL_82845G_IGD:
 		return (1);
 	}
 
@@ -143,6 +150,19 @@ agp_i810_attach(struct device *parent, s
 		return error;
 	}
 
+	switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
+	case PCI_PRODUCT_INTEL_82810_GC:
+	case PCI_PRODUCT_INTEL_82810_DC100_GC:
+	case PCI_PRODUCT_INTEL_82810E_GC:
+	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
+		isc->chiptype = CHIP_I810;
+		break;
+	case PCI_PRODUCT_INTEL_82830MP_IV:
+	case PCI_PRODUCT_INTEL_82845G_IGD:
+		isc->chiptype = CHIP_I830;
+		break;
+	}
+
 	error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
 	    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
 	if (error != 0) {
@@ -152,35 +172,83 @@ agp_i810_attach(struct device *parent, s
 
 	isc->initial_aperture = AGP_GET_APERTURE(sc);
 
-	if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
-		isc->dcache_size = 4 * 1024 * 1024;
-	else
-		isc->dcache_size = 0;
-
-	for (;;) {
-		gatt = agp_alloc_gatt(sc);
-		if (gatt)
-			break;
+	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
+	if (!gatt) {
+ 		agp_generic_detach(sc);
+ 		return ENOMEM;
+	}
+	isc->gatt = gatt;
 
-		/*
-		 * Probably contigmalloc failure. Try reducing the
-		 * aperture so that the gatt size reduces.
-		 */
-		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
+	gatt->ag_entries = AGP_GET_APERTURE(sc) >> AGP_PAGE_SHIFT;
+
+	if (isc->chiptype == CHIP_I810 ) {
+		int dummyseg;
+		/* Some i810s have on-chip memory called dcache */
+		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
+			isc->dcache_size = 4 * 1024 * 1024;
+		else
+			isc->dcache_size = 0;
+
+		/* According to the specs the gatt on the i810 must be 64k */
+		if (agp_alloc_dmamem(sc->as_dmat, 64 * 1024,
+		    0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
+		    &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0) {
+			free(gatt, M_AGP);
 			agp_generic_detach(sc);
 			return ENOMEM;
 		}
-	}
-	isc->gatt = gatt;
 
-	/* Install the GATT. */
-	WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
+		gatt->ag_size = gatt->ag_entries * sizeof(u_int32_t);
+		memset(gatt->ag_virtual, 0, gatt->ag_size);
+		
+		agp_flush_cache();
+		/* Install the GATT. */
+		WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
+	} else {
+		/* The i830 automatically initializes the 128k gatt on boot. */
+		unsigned int reg, gcc1, pgtblctl;
+		
+		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
+		gcc1 = reg >> 16;
+		switch (gcc1 & AGP_I830_GCC1_GMS) {
+			case AGP_I830_GCC1_GMS_STOLEN_512:
+				isc->stolen = (512 - 132) * 1024 / 4096;
+				break;
+			case AGP_I830_GCC1_GMS_STOLEN_1024: 
+				isc->stolen = (1024 - 132) * 1024 / 4096;
+				break;
+			case AGP_I830_GCC1_GMS_STOLEN_8192: 
+				isc->stolen = (8192 - 132) * 1024 / 4096;
+				break;
+			default:
+				isc->stolen = 0;
+				printf(": unknown memory configuration, disabling\n");
+				agp_generic_detach(sc);
+				return EINVAL;
+		}
+		if (isc->stolen > 0)
+			printf(": detected %dk stolen memory\n", isc->stolen * 4);
+		printf("%s: aperture size is %dM\n", sc->as_dev.dv_xname,
+		       isc->initial_aperture / 1024 / 1024);
+
+		/* GATT address is already in there, make sure it's enabled */
+		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
+#if 0
+		printf("PGTBL_CTL is 0x%08x\n", pgtblctl);
+#endif
+		pgtblctl |= 1;
+		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
+
+		gatt->ag_physical = pgtblctl & ~1;
+	}
 
 	/*
 	 * Make sure the chipset can see everything.
 	 */
 	agp_flush_cache();
 
+	printf("%s", sc->as_dev.dv_xname);
+
 	return 0;
 }
 
@@ -196,12 +264,23 @@ agp_i810_detach(struct agp_softc *sc)
 		return error;
 
 	/* Clear the GATT base. */
-	WRITE4(AGP_I810_PGTBL_CTL, 0);
+	if (sc->chiptype == CHIP_I810) {
+		WRITE4(AGP_I810_PGTBL_CTL, 0);
+	} else {
+		unsigned int pgtblctl;
+		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
+		pgtblctl &= ~1;
+		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
+	}
 
 	/* Put the aperture back the way it started. */
 	AGP_SET_APERTURE(sc, isc->initial_aperture);
 
-	agp_free_gatt(sc, isc->gatt);
+	if ( sc->chiptype == CHIP_I810 ) {
+		agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
+		    (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
+	}
+	free(sc->gatt, M_AGP);
 
 	return 0;
 }
@@ -210,40 +289,74 @@ agp_i810_detach(struct agp_softc *sc)
 static u_int32_t
 agp_i810_get_aperture(struct agp_softc *sc)
 {
-	u_int16_t miscc;
+	struct agp_i810_softc *isc = sc->as_chipc;
+
+	if (isc->chiptype == CHIP_I810) {
+		u_int16_t miscc;
 
-	miscc = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM) >> 16;
-	if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
-		return 32 * 1024 * 1024;
-	else
-		return 64 * 1024 * 1024;
+		miscc = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM) >> 16;
+		if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
+			return 32 * 1024 * 1024;
+		else
+			return 64 * 1024 * 1024;
+	} else {		/* I830 */
+		unsigned int gcc1;
+
+		gcc1 = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0) >> 16;
+		if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
+			return 64 * 1024 * 1024;
+		else
+			return 128 * 1024 * 1024;
+	}
 }
 
 static int
 agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
 {
-	pcireg_t reg, miscc;
+	struct agp_i810_softc *isc = sc->as_chipc;
 
-	/*
-	 * Double check for sanity.
-	 */
-	if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
-		printf("%s: bad aperture size %d\n", sc->as_dev.dv_xname,
-		       aperture);
-		return EINVAL;
-	}
+	if (isc->chiptype == CHIP_I810) {
+		pcireg_t reg, miscc;
 
-	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
-	miscc = reg >> 16;
-	miscc &= ~AGP_I810_MISCC_WINSIZE;
-	if (aperture == 32 * 1024 * 1024)
-		miscc |= AGP_I810_MISCC_WINSIZE_32;
-	else
-		miscc |= AGP_I810_MISCC_WINSIZE_64;
-
-	reg &= 0x0000ffff;
-	reg |= (miscc << 16);
-	pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, miscc);
+		/*
+		 * Double check for sanity.
+		 */
+		if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
+			printf("%s: bad aperture size %d\n", sc->as_dev.dv_xname,
+			       aperture);
+			return EINVAL;
+		}
+
+		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
+		miscc = reg >> 16;
+		miscc &= ~AGP_I810_MISCC_WINSIZE;
+		if (aperture == 32 * 1024 * 1024)
+			miscc |= AGP_I810_MISCC_WINSIZE_32;
+		else
+			miscc |= AGP_I810_MISCC_WINSIZE_64;
+
+		reg &= 0x0000ffff;
+		reg |= (miscc << 16);
+		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
+	} else {		/* I830 */
+		unsigned int reg, gcc1;
+
+		if (aperture != 64 * 1024 * 1024 && aperture != 128 * 1024 * 1024) {
+			printf("%s: bad aperture size %d\n", sc->as_dev.dv_xname, aperture);
+			return EINVAL;
+		}
+		reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
+		gcc1 = reg >> 16;
+		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
+		if (aperture == 64 * 1024 * 1024)
+			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
+		else
+			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
+
+		reg &= 0x0000ffff;
+		reg |= (gcc1 << 16);
+		pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
+	}
 
 	return 0;
 }
@@ -253,8 +366,17 @@ agp_i810_bind_page(struct agp_softc *sc,
 {
 	struct agp_i810_softc *isc = sc->as_chipc;
 
-	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
+	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
+		printf("%s: failed: offset is 0x%08x, shift is %d, entries is %d\n", sc->as_dev.dv_xname, (int)offset, AGP_PAGE_SHIFT, isc->gatt->ag_entries);
 		return EINVAL;
+	}
+
+	if (isc->chiptype == CHIP_I810) {
+		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
+			printf("%s: trying to bind into stolen memory", sc->as_dev.dv_xname);
+			return EINVAL;
+		}
+	}
 
 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
 	    physical | 1);
@@ -269,6 +391,13 @@ agp_i810_unbind_page(struct agp_softc *s
 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
 		return EINVAL;
 
+	if (isc->chiptype == CHIP_I830 ) {
+		if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
+			printf("%s: trying to unbind from stolen memory", sc->as_dev.dv_xname);
+			return EINVAL;
+		}
+	}
+
 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
 	return 0;
 }
@@ -304,6 +433,8 @@ agp_i810_alloc_memory(struct agp_softc *
 		/*
 		 * Mapping local DRAM into GATT.
 		 */
+		if (isc->chiptype == CHIP_I830 )
+			return 0;
 		if (size != isc->dcache_size)
 			return 0;
 	} else if (type == 2) {
@@ -404,6 +535,9 @@ agp_i810_bind_memory(struct agp_softc *s
 	if (mem->am_type != 1)
 		return agp_generic_bind_memory(sc, mem, offset);
 
+	if (isc->chiptype == CHIP_I830)
+		return EINVAL;
+
 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
 		       i | 3);
@@ -429,6 +563,9 @@ agp_i810_unbind_memory(struct agp_softc 
 
 	if (mem->am_type != 1)
 		return agp_generic_unbind_memory(sc, mem);
+
+	if (isc->chiptype == CHIP_I830)
+		return EINVAL;
 
 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
 		WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
diff -urp /home/nabe/work/cvs/NetBSD/src/sys/dev/pci/agpreg.h dev/pci/agpreg.h
--- /home/nabe/work/cvs/NetBSD/src/sys/dev/pci/agpreg.h	Mon Sep 10 19:01:02 2001
+++ dev/pci/agpreg.h	Tue Dec  3 18:39:25 2002
@@ -153,4 +153,20 @@
 #define AGP_I810_DRT_POPULATED	0x01
 #define AGP_I810_GTT		0x10000
 
+/*
+ * Config registers for i830MG device 0
+ */
+#define AGP_I830_GCC0			0x50
+#define AGP_I830_GCC1			0x52
+#define AGP_I830_GCC1_DEV2		0x08
+#define AGP_I830_GCC1_DEV2_ENABLED	0x00
+#define AGP_I830_GCC1_DEV2_DISABLED	0x08
+#define AGP_I830_GCC1_GMS		0x70
+#define AGP_I830_GCC1_GMS_STOLEN_512	0x20
+#define AGP_I830_GCC1_GMS_STOLEN_1024	0x30
+#define AGP_I830_GCC1_GMS_STOLEN_8192	0x40
+#define AGP_I830_GCC1_GMASIZE		0x01
+#define AGP_I830_GCC1_GMASIZE_64	0x01
+#define AGP_I830_GCC1_GMASIZE_128	0x00
+
 #endif /* !_PCI_AGPREG_H_ */