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