Home | History | Annotate | Line # | Download | only in pci
agp_apple.c revision 1.7
      1 /*	$NetBSD: agp_apple.c,v 1.7 2014/11/02 00:05:03 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Michael Lorenz
      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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: agp_apple.c,v 1.7 2014/11/02 00:05:03 christos Exp $");
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/malloc.h>
     34 #include <sys/kernel.h>
     35 #include <sys/proc.h>
     36 #include <sys/conf.h>
     37 #include <sys/device.h>
     38 #include <sys/agpio.h>
     39 
     40 #include <dev/pci/pcivar.h>
     41 #include <dev/pci/pcireg.h>
     42 #include <dev/pci/agpvar.h>
     43 #include <dev/pci/agpreg.h>
     44 
     45 #include <sys/bus.h>
     46 
     47 #define	APPLE_UNINORTH_GART_BASE	0x8c
     48 #define	APPLE_UNINORTH_GART_BASE_ADDR	0x90
     49 #define APPLE_UNINORTH_GART_CTRL	0x94
     50 #define APPLE_UNINORTH_GART_INVAL	0x00000001
     51 #define APPLE_UNINORTH_GART_ENABLE	0x00000100
     52 #define APPLE_UNINORTH_GART_2XRESET	0x00010000
     53 #define APPLE_UNINORTH_GART_PERFRD	0x00080000
     54 
     55 static u_int32_t agp_apple_get_aperture(struct agp_softc *);
     56 static int agp_apple_set_aperture(struct agp_softc *, u_int32_t);
     57 static int agp_apple_bind_page(struct agp_softc *, off_t, bus_addr_t);
     58 static int agp_apple_unbind_page(struct agp_softc *, off_t);
     59 static void agp_apple_flush_tlb(struct agp_softc *);
     60 
     61 static struct agp_methods agp_apple_methods = {
     62 	agp_apple_get_aperture,
     63 	agp_apple_set_aperture,
     64 	agp_apple_bind_page,
     65 	agp_apple_unbind_page,
     66 	agp_apple_flush_tlb,
     67 	agp_generic_enable,
     68 	agp_generic_alloc_memory,
     69 	agp_generic_free_memory,
     70 	agp_generic_bind_memory,
     71 	agp_generic_unbind_memory,
     72 };
     73 
     74 struct agp_apple_softc {
     75 	u_int32_t	initial_aperture; /* aperture size at startup */
     76 	struct agp_gatt *gatt;
     77 };
     78 
     79 int
     80 agp_apple_attach(device_t parent, device_t self, void *aux)
     81 {
     82 	struct pci_attach_args *pa = aux;
     83 	struct agp_softc *sc = device_private(self);
     84 	struct agp_apple_softc *asc;
     85 	struct agp_gatt *gatt;
     86 
     87 	asc = malloc(sizeof *asc, M_AGP, M_NOWAIT|M_ZERO);
     88 	if (asc == NULL) {
     89 		aprint_error(": can't allocate chipset-specific softc\n");
     90 		return ENOMEM;
     91 	}
     92 	sc->as_chipc = asc;
     93 	sc->as_methods = &agp_apple_methods;
     94 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
     95 	    NULL);
     96 
     97 	sc->as_apaddr = 0;
     98 	sc->as_apsize = 8 * 1024 * 1024;
     99 	sc->as_apflags = 0;
    100 	sc->as_pc = pa->pa_pc;
    101 	sc->as_tag = pa->pa_tag;
    102 	sc->as_apt = pa->pa_memt;
    103 
    104 	asc->initial_aperture = sc->as_apsize;
    105 
    106 	for (;;) {
    107 		gatt = agp_alloc_gatt(sc);
    108 		if (gatt)
    109 			break;
    110 		sc->as_apsize = sc->as_apsize >> 1;
    111 		if (sc->as_apsize == 0) {
    112 			aprint_error(": can't set aperture size\n");
    113 			return ENOMEM;
    114 		}
    115 	}
    116 	asc->gatt = gatt;
    117 
    118 	/* Install the gatt. */
    119 	aprint_error("gatt: %08jx %ju MB\n", (uintmax_t)gatt->ag_physical,
    120 	    (uintmax_t)(sc->as_apsize >> 20));
    121 	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_BASE,
    122 	    (gatt->ag_physical & 0xfffff000) |
    123 	    (sc->as_apsize >> 22));
    124 
    125 	/* Enable the aperture. */
    126 	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
    127 	    APPLE_UNINORTH_GART_ENABLE);
    128 	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
    129 	    APPLE_UNINORTH_GART_ENABLE | APPLE_UNINORTH_GART_INVAL);
    130 	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
    131 	    APPLE_UNINORTH_GART_ENABLE);
    132 	return 0;
    133 }
    134 
    135 static u_int32_t
    136 agp_apple_get_aperture(struct agp_softc *sc)
    137 {
    138 #if 0
    139 	u_int32_t apsize = 0;
    140 
    141 	aprint_error("%s: ", __func__);
    142 	apsize = pci_conf_read(sc->as_pc, sc->as_tag,
    143 	    APPLE_UNINORTH_GART_BASE) & 0x0000ffff;
    144 	aprint_error("%08x\n", apsize);
    145 	return (apsize << 22);
    146 #else
    147 	return sc->as_apsize;
    148 #endif
    149 }
    150 
    151 static int
    152 agp_apple_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    153 {
    154 	pcireg_t reg;
    155 
    156 	aprint_error("%s: %08x\n", __func__, aperture);
    157 	reg = pci_conf_read(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_BASE)
    158 	    & 0xfffff000;
    159 	reg |= ((aperture >> 22) & 0xfff);
    160 	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_BASE, reg);
    161 	sc->as_apsize = aperture;
    162 	return 0;
    163 }
    164 
    165 static int
    166 agp_apple_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    167 {
    168 	struct agp_apple_softc *asc = sc->as_chipc;
    169 
    170 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    171 		return EINVAL;
    172 
    173 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = htole32(physical);
    174 	return 0;
    175 }
    176 
    177 static int
    178 agp_apple_unbind_page(struct agp_softc *sc, off_t offset)
    179 {
    180 	struct agp_apple_softc *asc = sc->as_chipc;
    181 
    182 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    183 		return EINVAL;
    184 
    185 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
    186 	return 0;
    187 }
    188 
    189 static void
    190 agp_apple_flush_tlb(struct agp_softc *sc)
    191 {
    192 
    193 	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
    194 	    APPLE_UNINORTH_GART_ENABLE);
    195 	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
    196 	    APPLE_UNINORTH_GART_ENABLE | APPLE_UNINORTH_GART_INVAL);
    197 	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
    198 	    APPLE_UNINORTH_GART_ENABLE);
    199 }
    200