Home | History | Annotate | Line # | Download | only in pci
agp_apple.c revision 1.2.2.1
      1  1.2.2.1       mjf /*	$NetBSD: agp_apple.c,v 1.2.2.1 2008/02/18 21:05:55 mjf Exp $ */
      2      1.1  macallan 
      3      1.1  macallan /*-
      4      1.1  macallan  * Copyright (c) 2007 Michael Lorenz
      5      1.1  macallan  * All rights reserved.
      6      1.1  macallan  *
      7      1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8      1.1  macallan  * modification, are permitted provided that the following conditions
      9      1.1  macallan  * are met:
     10      1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11      1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12      1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15      1.1  macallan  * 3. Neither the name of The NetBSD Foundation nor the names of its
     16      1.1  macallan  *    contributors may be used to endorse or promote products derived
     17      1.1  macallan  *    from this software without specific prior written permission.
     18      1.1  macallan  *
     19      1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  macallan  */
     31      1.1  macallan 
     32      1.1  macallan #include <sys/cdefs.h>
     33  1.2.2.1       mjf __KERNEL_RCSID(0, "$NetBSD: agp_apple.c,v 1.2.2.1 2008/02/18 21:05:55 mjf Exp $");
     34      1.1  macallan #include <sys/param.h>
     35      1.1  macallan #include <sys/systm.h>
     36      1.1  macallan #include <sys/malloc.h>
     37      1.1  macallan #include <sys/kernel.h>
     38      1.1  macallan #include <sys/proc.h>
     39      1.1  macallan #include <sys/conf.h>
     40      1.1  macallan #include <sys/device.h>
     41      1.1  macallan #include <sys/agpio.h>
     42      1.1  macallan 
     43      1.1  macallan #include <uvm/uvm_extern.h>
     44      1.1  macallan 
     45      1.1  macallan #include <dev/pci/pcivar.h>
     46      1.1  macallan #include <dev/pci/pcireg.h>
     47      1.1  macallan #include <dev/pci/agpvar.h>
     48      1.1  macallan #include <dev/pci/agpreg.h>
     49      1.1  macallan 
     50      1.2        ad #include <sys/bus.h>
     51      1.1  macallan 
     52      1.1  macallan static u_int32_t agp_apple_get_aperture(struct agp_softc *);
     53      1.1  macallan static int agp_apple_set_aperture(struct agp_softc *, u_int32_t);
     54      1.1  macallan static int agp_apple_bind_page(struct agp_softc *, off_t, bus_addr_t);
     55      1.1  macallan static int agp_apple_unbind_page(struct agp_softc *, off_t);
     56      1.1  macallan static void agp_apple_flush_tlb(struct agp_softc *);
     57      1.1  macallan 
     58      1.1  macallan static struct agp_methods agp_apple_methods = {
     59      1.1  macallan 	agp_apple_get_aperture,
     60      1.1  macallan 	agp_apple_set_aperture,
     61      1.1  macallan 	agp_apple_bind_page,
     62      1.1  macallan 	agp_apple_unbind_page,
     63      1.1  macallan 	agp_apple_flush_tlb,
     64      1.1  macallan 	agp_generic_enable,
     65      1.1  macallan 	agp_generic_alloc_memory,
     66      1.1  macallan 	agp_generic_free_memory,
     67      1.1  macallan 	agp_generic_bind_memory,
     68      1.1  macallan 	agp_generic_unbind_memory,
     69      1.1  macallan };
     70      1.1  macallan 
     71      1.1  macallan struct agp_apple_softc {
     72      1.1  macallan 	u_int32_t	initial_aperture; /* aperture size at startup */
     73      1.1  macallan 	struct agp_gatt *gatt;
     74      1.1  macallan };
     75      1.1  macallan 
     76      1.1  macallan int
     77      1.1  macallan agp_apple_attach(struct device *parent, struct device *self, void *aux)
     78      1.1  macallan {
     79      1.1  macallan 	struct pci_attach_args *pa = aux;
     80      1.1  macallan 	struct agp_softc *sc = (void *)self;
     81      1.1  macallan 	struct agp_apple_softc *asc;
     82      1.1  macallan 	struct agp_gatt *gatt;
     83      1.1  macallan 
     84      1.1  macallan 	asc = malloc(sizeof *asc, M_AGP, M_NOWAIT|M_ZERO);
     85      1.1  macallan 	if (asc == NULL) {
     86      1.1  macallan 		aprint_error(": can't allocate chipset-specific softc\n");
     87      1.1  macallan 		return ENOMEM;
     88      1.1  macallan 	}
     89      1.1  macallan 	sc->as_chipc = asc;
     90      1.1  macallan 	sc->as_methods = &agp_apple_methods;
     91      1.1  macallan 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
     92      1.1  macallan 	    NULL);
     93      1.1  macallan 
     94      1.1  macallan 	sc->as_apaddr = 0;
     95      1.1  macallan 	sc->as_apsize = 8 * 1024 * 1024;
     96      1.1  macallan 	sc->as_apflags = 0;
     97      1.1  macallan 	sc->as_pc = pa->pa_pc;
     98      1.1  macallan 	sc->as_tag = pa->pa_tag;
     99      1.1  macallan 	sc->as_apt = pa->pa_memt;
    100      1.1  macallan 
    101      1.1  macallan 	asc->initial_aperture = sc->as_apsize;
    102      1.1  macallan 
    103      1.1  macallan 	for (;;) {
    104      1.1  macallan 		gatt = agp_alloc_gatt(sc);
    105      1.1  macallan 		if (gatt)
    106      1.1  macallan 			break;
    107      1.1  macallan 		sc->as_apsize = sc->as_apsize >> 1;
    108      1.1  macallan 		if (sc->as_apsize == 0) {
    109      1.1  macallan 			aprint_error(": can't set aperture size\n");
    110      1.1  macallan 			return ENOMEM;
    111      1.1  macallan 		}
    112      1.1  macallan 	}
    113      1.1  macallan 	asc->gatt = gatt;
    114      1.1  macallan 
    115      1.1  macallan 	/* Install the gatt. */
    116      1.1  macallan 	aprint_error("gatt: %08x %d MB\n", gatt->ag_physical, sc->as_apsize >> 20);
    117      1.1  macallan 	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_BASE,
    118      1.1  macallan 	    (gatt->ag_physical & 0xfffff000) |
    119      1.1  macallan 	    (sc->as_apsize >> 22));
    120      1.1  macallan 
    121      1.1  macallan 	/* Enable the aperture. */
    122      1.1  macallan 	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
    123      1.1  macallan 	    APPLE_GART_EN);
    124      1.1  macallan 	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
    125      1.1  macallan 	    APPLE_GART_EN | APPLE_GART_INV);
    126      1.1  macallan 	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
    127      1.1  macallan 	    APPLE_GART_EN);
    128      1.1  macallan 	return 0;
    129      1.1  macallan }
    130      1.1  macallan 
    131      1.1  macallan static u_int32_t
    132      1.1  macallan agp_apple_get_aperture(struct agp_softc *sc)
    133      1.1  macallan {
    134      1.1  macallan #if 0
    135      1.1  macallan 	u_int32_t apsize = 0;
    136      1.1  macallan 
    137      1.1  macallan 	aprint_error("%s: ", __func__);
    138      1.1  macallan 	apsize = pci_conf_read(sc->as_pc, sc->as_tag,
    139      1.1  macallan 	    APPLE_UNINORTH_GART_BASE) & 0x0000ffff;
    140      1.1  macallan 	aprint_error("%08x\n", apsize);
    141      1.1  macallan 	return (apsize << 22);
    142      1.1  macallan #else
    143      1.1  macallan 	return sc->as_apsize;
    144      1.1  macallan #endif
    145      1.1  macallan }
    146      1.1  macallan 
    147      1.1  macallan static int
    148      1.1  macallan agp_apple_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    149      1.1  macallan {
    150      1.1  macallan 	pcireg_t reg;
    151      1.1  macallan 
    152      1.1  macallan 	aprint_error("%s: %08x\n", __func__, aperture);
    153      1.1  macallan 	reg = pci_conf_read(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_BASE)
    154      1.1  macallan 	    & 0xfffff000;
    155      1.1  macallan 	reg |= ((aperture >> 22) & 0xfff);
    156      1.1  macallan 	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_BASE, reg);
    157      1.1  macallan 	sc->as_apsize = aperture;
    158      1.1  macallan 	return 0;
    159      1.1  macallan }
    160      1.1  macallan 
    161      1.1  macallan static int
    162      1.1  macallan agp_apple_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    163      1.1  macallan {
    164      1.1  macallan 	struct agp_apple_softc *asc = sc->as_chipc;
    165      1.1  macallan 
    166      1.1  macallan 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    167      1.1  macallan 		return EINVAL;
    168      1.1  macallan 
    169      1.1  macallan 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = htole32(physical);
    170      1.1  macallan 	return 0;
    171      1.1  macallan }
    172      1.1  macallan 
    173      1.1  macallan static int
    174      1.1  macallan agp_apple_unbind_page(struct agp_softc *sc, off_t offset)
    175      1.1  macallan {
    176      1.1  macallan 	struct agp_apple_softc *asc = sc->as_chipc;
    177      1.1  macallan 
    178      1.1  macallan 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    179      1.1  macallan 		return EINVAL;
    180      1.1  macallan 
    181      1.1  macallan 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
    182      1.1  macallan 	return 0;
    183      1.1  macallan }
    184      1.1  macallan 
    185      1.1  macallan static void
    186      1.1  macallan agp_apple_flush_tlb(struct agp_softc *sc)
    187      1.1  macallan {
    188      1.1  macallan 
    189      1.1  macallan 	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
    190      1.1  macallan 	    APPLE_GART_EN);
    191      1.1  macallan 	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
    192      1.1  macallan 	    APPLE_GART_EN | APPLE_GART_INV);
    193      1.1  macallan 	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
    194      1.1  macallan 	    APPLE_GART_EN);
    195      1.1  macallan }
    196