xref: /netbsd/sys/dev/pci/agp_sis.c (revision bf9ec67e)
1 /*	$NetBSD: agp_sis.c,v 1.3 2001/11/13 07:48:40 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *	$FreeBSD: src/sys/pci/agp_sis.c,v 1.3 2001/07/05 21:28:47 jhb Exp $
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: agp_sis.c,v 1.3 2001/11/13 07:48:40 lukem Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/proc.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/agpio.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/agpvar.h>
49 #include <dev/pci/agpreg.h>
50 
51 #include <machine/bus.h>
52 
53 struct agp_sis_softc {
54 	u_int32_t	initial_aperture; /* aperture size at startup */
55 	struct agp_gatt *gatt;
56 };
57 
58 static u_int32_t agp_sis_get_aperture(struct agp_softc *);
59 static int agp_sis_set_aperture(struct agp_softc *, u_int32_t);
60 static int agp_sis_bind_page(struct agp_softc *, off_t, bus_addr_t);
61 static int agp_sis_unbind_page(struct agp_softc *, off_t);
62 static void agp_sis_flush_tlb(struct agp_softc *);
63 
64 struct agp_methods agp_sis_methods = {
65 	agp_sis_get_aperture,
66 	agp_sis_set_aperture,
67 	agp_sis_bind_page,
68 	agp_sis_unbind_page,
69 	agp_sis_flush_tlb,
70 	agp_generic_enable,
71 	agp_generic_alloc_memory,
72 	agp_generic_free_memory,
73 	agp_generic_bind_memory,
74 	agp_generic_unbind_memory,
75 };
76 
77 int
78 agp_sis_attach(struct device *parent, struct device *self, void *aux)
79 {
80 	struct agp_softc *sc = (struct agp_softc *)self;
81 	struct pci_attach_args *pa = aux;
82 	struct agp_sis_softc *ssc;
83 	struct agp_gatt *gatt;
84 	pcireg_t reg;
85 
86 	ssc = malloc(sizeof *ssc, M_AGP, M_NOWAIT);
87 	if (ssc == NULL) {
88 		printf(": can't allocate chipset-specific softc\n");
89 		return ENOMEM;
90 	}
91 	sc->as_methods = &agp_sis_methods;
92 	sc->as_chipc = ssc;
93 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
94 	    NULL);
95 
96 	if (agp_map_aperture(pa, sc) != 0) {
97 		printf(": can't map aperture\n");
98 		free(ssc, M_AGP);
99 		return ENXIO;
100 	}
101 
102 	ssc->initial_aperture = AGP_GET_APERTURE(sc);
103 
104 	for (;;) {
105 		gatt = agp_alloc_gatt(sc);
106 		if (gatt)
107 			break;
108 
109 		/*
110 		 * Probably contigmalloc failure. Try reducing the
111 		 * aperture so that the gatt size reduces.
112 		 */
113 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
114 			agp_generic_detach(sc);
115 			printf(": failed to set aperture\n");
116 			return ENOMEM;
117 		}
118 	}
119 	ssc->gatt = gatt;
120 
121 	/* Install the gatt. */
122 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_SIS_ATTBASE,
123 	    gatt->ag_physical);
124 
125 	/* Enable the aperture and auto-tlb-inval */
126 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL);
127 	reg |= (0x05 << 24) | 3;
128 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL, reg);
129 
130 	return 0;
131 }
132 
133 #if 0
134 static int
135 agp_sis_detach(struct agp_softc *sc)
136 {
137 	struct agp_sis_softc *ssc = sc->as_chipc;
138 	pcireg_t reg;
139 	int error;
140 
141 	error = agp_generic_detach(sc);
142 	if (error)
143 		return error;
144 
145 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL);
146 	reg &= ~3;
147 	reg &= 0x00ffffff;
148 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL, reg);
149 
150 	/* Put the aperture back the way it started. */
151 	AGP_SET_APERTURE(sc, ssc->initial_aperture);
152 
153 	agp_free_gatt(sc, ssc->gatt);
154 	return 0;
155 }
156 #endif
157 
158 static u_int32_t
159 agp_sis_get_aperture(struct agp_softc *sc)
160 {
161 	int gws;
162 
163 	/*
164 	 * The aperture size is equal to 4M<<gws.
165 	 */
166 	gws = (pci_conf_read(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL)&0x70) >> 4;
167 	return (4*1024*1024) << gws;
168 }
169 
170 static int
171 agp_sis_set_aperture(struct agp_softc *sc, u_int32_t aperture)
172 {
173 	int gws;
174 	pcireg_t reg;
175 
176 	/*
177 	 * Check for a power of two and make sure its within the
178 	 * programmable range.
179 	 */
180 	if (aperture & (aperture - 1)
181 	    || aperture < 4*1024*1024
182 	    || aperture > 256*1024*1024)
183 		return EINVAL;
184 
185 	gws = ffs(aperture / 4*1024*1024) - 1;
186 
187 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL);
188 	reg &= ~0x00000070;
189 	reg |= gws << 4;
190 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_SIS_WINCTRL, reg);
191 
192 	return 0;
193 }
194 
195 static int
196 agp_sis_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
197 {
198 	struct agp_sis_softc *ssc = sc->as_chipc;
199 
200 	if (offset < 0 || offset >= (ssc->gatt->ag_entries << AGP_PAGE_SHIFT))
201 		return EINVAL;
202 
203 	ssc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
204 	return 0;
205 }
206 
207 static int
208 agp_sis_unbind_page(struct agp_softc *sc, off_t offset)
209 {
210 	struct agp_sis_softc *ssc = sc->as_chipc;
211 
212 	if (offset < 0 || offset >= (ssc->gatt->ag_entries << AGP_PAGE_SHIFT))
213 		return EINVAL;
214 
215 	ssc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
216 	return 0;
217 }
218 
219 static void
220 agp_sis_flush_tlb(struct agp_softc *sc)
221 {
222 	pcireg_t reg;
223 
224 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_SIS_TLBFLUSH);
225 	reg &= 0xffffff00;
226 	reg |= 0x02;
227 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_SIS_TLBFLUSH, reg);
228 }
229