summaryrefslogtreecommitdiff
path: root/sys/dev/pci/agp_apple.c
blob: 8bc99bed46ac583341643657537501e5f487b007 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*	$NetBSD: agp_apple.c,v 1.8 2019/11/10 21:16:36 chs Exp $ */

/*-
 * Copyright (c) 2007 Michael Lorenz
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: agp_apple.c,v 1.8 2019/11/10 21:16:36 chs Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/agpio.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/agpvar.h>
#include <dev/pci/agpreg.h>

#include <sys/bus.h>

#define	APPLE_UNINORTH_GART_BASE	0x8c
#define	APPLE_UNINORTH_GART_BASE_ADDR	0x90
#define APPLE_UNINORTH_GART_CTRL	0x94
#define APPLE_UNINORTH_GART_INVAL	0x00000001
#define APPLE_UNINORTH_GART_ENABLE	0x00000100
#define APPLE_UNINORTH_GART_2XRESET	0x00010000
#define APPLE_UNINORTH_GART_PERFRD	0x00080000

static u_int32_t agp_apple_get_aperture(struct agp_softc *);
static int agp_apple_set_aperture(struct agp_softc *, u_int32_t);
static int agp_apple_bind_page(struct agp_softc *, off_t, bus_addr_t);
static int agp_apple_unbind_page(struct agp_softc *, off_t);
static void agp_apple_flush_tlb(struct agp_softc *);

static struct agp_methods agp_apple_methods = {
	agp_apple_get_aperture,
	agp_apple_set_aperture,
	agp_apple_bind_page,
	agp_apple_unbind_page,
	agp_apple_flush_tlb,
	agp_generic_enable,
	agp_generic_alloc_memory,
	agp_generic_free_memory,
	agp_generic_bind_memory,
	agp_generic_unbind_memory,
};

struct agp_apple_softc {
	u_int32_t	initial_aperture; /* aperture size at startup */
	struct agp_gatt *gatt;
};

int
agp_apple_attach(device_t parent, device_t self, void *aux)
{
	struct pci_attach_args *pa = aux;
	struct agp_softc *sc = device_private(self);
	struct agp_apple_softc *asc;
	struct agp_gatt *gatt;

	asc = malloc(sizeof *asc, M_AGP, M_WAITOK|M_ZERO);
	sc->as_chipc = asc;
	sc->as_methods = &agp_apple_methods;
	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
	    NULL);

	sc->as_apaddr = 0;
	sc->as_apsize = 8 * 1024 * 1024;
	sc->as_apflags = 0;
	sc->as_pc = pa->pa_pc;
	sc->as_tag = pa->pa_tag;
	sc->as_apt = pa->pa_memt;

	asc->initial_aperture = sc->as_apsize;

	for (;;) {
		gatt = agp_alloc_gatt(sc);
		if (gatt)
			break;
		sc->as_apsize = sc->as_apsize >> 1;
		if (sc->as_apsize == 0) {
			aprint_error(": can't set aperture size\n");
			return ENOMEM;
		}
	}
	asc->gatt = gatt;

	/* Install the gatt. */
	aprint_error("gatt: %08jx %ju MB\n", (uintmax_t)gatt->ag_physical,
	    (uintmax_t)(sc->as_apsize >> 20));
	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_BASE,
	    (gatt->ag_physical & 0xfffff000) |
	    (sc->as_apsize >> 22));

	/* Enable the aperture. */
	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
	    APPLE_UNINORTH_GART_ENABLE);
	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
	    APPLE_UNINORTH_GART_ENABLE | APPLE_UNINORTH_GART_INVAL);
	pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
	    APPLE_UNINORTH_GART_ENABLE);
	return 0;
}

static u_int32_t
agp_apple_get_aperture(struct agp_softc *sc)
{
#if 0
	u_int32_t apsize = 0;

	aprint_error("%s: ", __func__);
	apsize = pci_conf_read(sc->as_pc, sc->as_tag,
	    APPLE_UNINORTH_GART_BASE) & 0x0000ffff;
	aprint_error("%08x\n", apsize);
	return (apsize << 22);
#else
	return sc->as_apsize;
#endif
}

static int
agp_apple_set_aperture(struct agp_softc *sc, u_int32_t aperture)
{
	pcireg_t reg;

	aprint_error("%s: %08x\n", __func__, aperture);
	reg = pci_conf_read(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_BASE)
	    & 0xfffff000;
	reg |= ((aperture >> 22) & 0xfff);
	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_BASE, reg);
	sc->as_apsize = aperture;
	return 0;
}

static int
agp_apple_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
{
	struct agp_apple_softc *asc = sc->as_chipc;

	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
		return EINVAL;

	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = htole32(physical);
	return 0;
}

static int
agp_apple_unbind_page(struct agp_softc *sc, off_t offset)
{
	struct agp_apple_softc *asc = sc->as_chipc;

	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
		return EINVAL;

	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
	return 0;
}

static void
agp_apple_flush_tlb(struct agp_softc *sc)
{

	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
	    APPLE_UNINORTH_GART_ENABLE);
	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
	    APPLE_UNINORTH_GART_ENABLE | APPLE_UNINORTH_GART_INVAL);
	pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
	    APPLE_UNINORTH_GART_ENABLE);
}