1 /* $NetBSD: agp_sis.c,v 1.12 2008/01/04 21:18:01 ad Exp $ */
4 * Copyright (c) 2000 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $FreeBSD: src/sys/pci/agp_sis.c,v 1.3 2001/07/05 21:28:47 jhb Exp $
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: agp_sis.c,v 1.12 2008/01/04 21:18:01 ad Exp $");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/agpio.h>
43 #include <uvm/uvm_extern.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/agpvar.h>
48 #include <dev/pci/agpreg.h>
52 struct agp_sis_softc
{
53 u_int32_t initial_aperture
; /* aperture size at startup */
54 struct agp_gatt
*gatt
;
57 static u_int32_t
agp_sis_get_aperture(struct agp_softc
*);
58 static int agp_sis_set_aperture(struct agp_softc
*, u_int32_t
);
59 static int agp_sis_bind_page(struct agp_softc
*, off_t
, bus_addr_t
);
60 static int agp_sis_unbind_page(struct agp_softc
*, off_t
);
61 static void agp_sis_flush_tlb(struct agp_softc
*);
63 static struct agp_methods agp_sis_methods
= {
70 agp_generic_alloc_memory
,
71 agp_generic_free_memory
,
72 agp_generic_bind_memory
,
73 agp_generic_unbind_memory
,
77 agp_sis_attach(device_t parent
, device_t self
, void *aux
)
79 struct agp_softc
*sc
= device_private(self
);
80 struct pci_attach_args
*pa
= aux
;
81 struct agp_sis_softc
*ssc
;
82 struct agp_gatt
*gatt
;
85 ssc
= malloc(sizeof *ssc
, M_AGP
, M_NOWAIT
);
87 aprint_error(": can't allocate chipset-specific softc\n");
90 sc
->as_methods
= &agp_sis_methods
;
92 pci_get_capability(pa
->pa_pc
, pa
->pa_tag
, PCI_CAP_AGP
, &sc
->as_capoff
,
95 if (agp_map_aperture(pa
, sc
, AGP_APBASE
) != 0) {
96 aprint_error(": can't map aperture\n");
101 ssc
->initial_aperture
= AGP_GET_APERTURE(sc
);
104 gatt
= agp_alloc_gatt(sc
);
109 * Probably contigmalloc failure. Try reducing the
110 * aperture so that the gatt size reduces.
112 if (AGP_SET_APERTURE(sc
, AGP_GET_APERTURE(sc
) / 2)) {
113 agp_generic_detach(sc
);
114 aprint_error(": failed to set aperture\n");
120 /* Install the gatt. */
121 pci_conf_write(sc
->as_pc
, sc
->as_tag
, AGP_SIS_ATTBASE
,
124 /* Enable the aperture and auto-tlb-inval */
125 reg
= pci_conf_read(sc
->as_pc
, sc
->as_tag
, AGP_SIS_WINCTRL
);
126 reg
|= (0x05 << 24) | 3;
127 pci_conf_write(sc
->as_pc
, sc
->as_tag
, AGP_SIS_WINCTRL
, reg
);
134 agp_sis_detach(struct agp_softc
*sc
)
136 struct agp_sis_softc
*ssc
= sc
->as_chipc
;
140 error
= agp_generic_detach(sc
);
144 reg
= pci_conf_read(sc
->as_pc
, sc
->as_tag
, AGP_SIS_WINCTRL
);
147 pci_conf_write(sc
->as_pc
, sc
->as_tag
, AGP_SIS_WINCTRL
, reg
);
149 /* Put the aperture back the way it started. */
150 AGP_SET_APERTURE(sc
, ssc
->initial_aperture
);
152 agp_free_gatt(sc
, ssc
->gatt
);
158 agp_sis_get_aperture(struct agp_softc
*sc
)
163 * The aperture size is equal to 4M<<gws.
165 gws
= (pci_conf_read(sc
->as_pc
, sc
->as_tag
, AGP_SIS_WINCTRL
)&0x70) >> 4;
166 return (4*1024*1024) << gws
;
170 agp_sis_set_aperture(struct agp_softc
*sc
, u_int32_t aperture
)
176 * Check for a power of two and make sure its within the
177 * programmable range.
179 if (aperture
& (aperture
- 1)
180 || aperture
< 4*1024*1024
181 || aperture
> 256*1024*1024)
184 gws
= ffs(aperture
/ 4*1024*1024) - 1;
186 reg
= pci_conf_read(sc
->as_pc
, sc
->as_tag
, AGP_SIS_WINCTRL
);
189 pci_conf_write(sc
->as_pc
, sc
->as_tag
, AGP_SIS_WINCTRL
, reg
);
195 agp_sis_bind_page(struct agp_softc
*sc
, off_t offset
, bus_addr_t physical
)
197 struct agp_sis_softc
*ssc
= sc
->as_chipc
;
199 if (offset
< 0 || offset
>= (ssc
->gatt
->ag_entries
<< AGP_PAGE_SHIFT
))
202 ssc
->gatt
->ag_virtual
[offset
>> AGP_PAGE_SHIFT
] = physical
;
207 agp_sis_unbind_page(struct agp_softc
*sc
, off_t offset
)
209 struct agp_sis_softc
*ssc
= sc
->as_chipc
;
211 if (offset
< 0 || offset
>= (ssc
->gatt
->ag_entries
<< AGP_PAGE_SHIFT
))
214 ssc
->gatt
->ag_virtual
[offset
>> AGP_PAGE_SHIFT
] = 0;
219 agp_sis_flush_tlb(struct agp_softc
*sc
)
223 reg
= pci_conf_read(sc
->as_pc
, sc
->as_tag
, AGP_SIS_TLBFLUSH
);
226 pci_conf_write(sc
->as_pc
, sc
->as_tag
, AGP_SIS_TLBFLUSH
, reg
);