genfb_pci.c revision 1.1.18.5 1 1.1.18.5 yamt /* $NetBSD: genfb_pci.c,v 1.1.18.5 2008/03/17 09:15:11 yamt Exp $ */
2 1.1.18.2 yamt
3 1.1.18.2 yamt /*-
4 1.1.18.2 yamt * Copyright (c) 2007 Michael Lorenz
5 1.1.18.2 yamt * All rights reserved.
6 1.1.18.2 yamt *
7 1.1.18.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.18.2 yamt * modification, are permitted provided that the following conditions
9 1.1.18.2 yamt * are met:
10 1.1.18.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.18.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.18.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.18.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1.18.2 yamt * documentation and/or other materials provided with the distribution.
15 1.1.18.2 yamt * 3. Neither the name of The NetBSD Foundation nor the names of its
16 1.1.18.2 yamt * contributors may be used to endorse or promote products derived
17 1.1.18.2 yamt * from this software without specific prior written permission.
18 1.1.18.2 yamt *
19 1.1.18.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.18.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.18.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.18.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.18.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.18.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.18.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.18.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.18.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.18.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.18.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.1.18.2 yamt */
31 1.1.18.2 yamt
32 1.1.18.2 yamt #include <sys/cdefs.h>
33 1.1.18.5 yamt __KERNEL_RCSID(0, "$NetBSD: genfb_pci.c,v 1.1.18.5 2008/03/17 09:15:11 yamt Exp $");
34 1.1.18.2 yamt
35 1.1.18.2 yamt #include <sys/param.h>
36 1.1.18.2 yamt #include <sys/systm.h>
37 1.1.18.2 yamt #include <sys/kernel.h>
38 1.1.18.2 yamt #include <sys/device.h>
39 1.1.18.2 yamt #include <sys/proc.h>
40 1.1.18.2 yamt #include <sys/mutex.h>
41 1.1.18.2 yamt #include <sys/ioctl.h>
42 1.1.18.2 yamt #include <sys/kernel.h>
43 1.1.18.2 yamt #include <sys/systm.h>
44 1.1.18.2 yamt #include <sys/kauth.h>
45 1.1.18.2 yamt
46 1.1.18.2 yamt #include <dev/pci/pcidevs.h>
47 1.1.18.2 yamt #include <dev/pci/pcireg.h>
48 1.1.18.2 yamt #include <dev/pci/pcivar.h>
49 1.1.18.2 yamt #include <dev/pci/pciio.h>
50 1.1.18.2 yamt
51 1.1.18.2 yamt #include <dev/wsfb/genfbvar.h>
52 1.1.18.2 yamt
53 1.1.18.2 yamt #include "opt_wsfb.h"
54 1.1.18.2 yamt #include "opt_genfb.h"
55 1.1.18.2 yamt
56 1.1.18.3 yamt #ifdef GENFB_PCI_DEBUG
57 1.1.18.3 yamt # define DPRINTF printf
58 1.1.18.3 yamt #else
59 1.1.18.3 yamt # define DPRINTF while (0) printf
60 1.1.18.3 yamt #endif
61 1.1.18.3 yamt
62 1.1.18.2 yamt struct range {
63 1.1.18.2 yamt bus_addr_t offset;
64 1.1.18.2 yamt bus_size_t size;
65 1.1.18.2 yamt int flags;
66 1.1.18.2 yamt };
67 1.1.18.2 yamt
68 1.1.18.2 yamt struct pci_genfb_softc {
69 1.1.18.2 yamt struct genfb_softc sc_gen;
70 1.1.18.2 yamt
71 1.1.18.2 yamt pci_chipset_tag_t sc_pc;
72 1.1.18.2 yamt pcitag_t sc_pcitag;
73 1.1.18.2 yamt bus_space_tag_t sc_memt;
74 1.1.18.2 yamt bus_space_tag_t sc_iot;
75 1.1.18.2 yamt bus_space_handle_t sc_memh;
76 1.1.18.3 yamt pcireg_t sc_bars[9];
77 1.1.18.2 yamt struct range sc_ranges[8];
78 1.1.18.2 yamt int sc_ranges_used;
79 1.1.18.4 yamt int sc_want_wsfb;
80 1.1.18.2 yamt };
81 1.1.18.2 yamt
82 1.1.18.2 yamt static int pci_genfb_match(struct device *, struct cfdata *, void *);
83 1.1.18.2 yamt static void pci_genfb_attach(struct device *, struct device *, void *);
84 1.1.18.2 yamt static int pci_genfb_ioctl(void *, void *, u_long, void *, int,
85 1.1.18.2 yamt struct lwp *);
86 1.1.18.2 yamt static paddr_t pci_genfb_mmap(void *, void *, off_t, int);
87 1.1.18.3 yamt static int pci_genfb_drm_print(void *, const char *);
88 1.1.18.3 yamt
89 1.1.18.2 yamt
90 1.1.18.2 yamt CFATTACH_DECL(genfb_pci, sizeof(struct pci_genfb_softc),
91 1.1.18.2 yamt pci_genfb_match, pci_genfb_attach, NULL, NULL);
92 1.1.18.2 yamt
93 1.1.18.2 yamt static int
94 1.1.18.2 yamt pci_genfb_match(struct device *parent, struct cfdata *match, void *aux)
95 1.1.18.2 yamt {
96 1.1.18.2 yamt struct pci_attach_args *pa = aux;
97 1.1.18.2 yamt
98 1.1.18.2 yamt if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
99 1.1.18.2 yamt PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
100 1.1.18.2 yamt return 1;
101 1.1.18.2 yamt
102 1.1.18.2 yamt if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
103 1.1.18.2 yamt return 1;
104 1.1.18.2 yamt
105 1.1.18.2 yamt return 0;
106 1.1.18.2 yamt }
107 1.1.18.2 yamt
108 1.1.18.2 yamt static void
109 1.1.18.2 yamt pci_genfb_attach(struct device *parent, struct device *self, void *aux)
110 1.1.18.2 yamt {
111 1.1.18.2 yamt struct pci_genfb_softc *sc = (struct pci_genfb_softc *)self;
112 1.1.18.2 yamt struct pci_attach_args *pa = aux;
113 1.1.18.2 yamt struct genfb_ops ops;
114 1.1.18.2 yamt int idx, bar, type;
115 1.1.18.2 yamt char devinfo[256];
116 1.1.18.2 yamt
117 1.1.18.2 yamt pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
118 1.1.18.2 yamt printf(": %s\n", devinfo);
119 1.1.18.2 yamt
120 1.1.18.2 yamt sc->sc_memt = pa->pa_memt;
121 1.1.18.2 yamt sc->sc_iot = pa->pa_iot;
122 1.1.18.2 yamt sc->sc_pc = pa->pa_pc;
123 1.1.18.2 yamt sc->sc_pcitag = pa->pa_tag;
124 1.1.18.4 yamt sc->sc_want_wsfb = 0;
125 1.1.18.4 yamt
126 1.1.18.2 yamt genfb_init(&sc->sc_gen);
127 1.1.18.2 yamt
128 1.1.18.3 yamt if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
129 1.1.18.3 yamt aprint_error("%s: bogus parameters, unable to continue\n",
130 1.1.18.3 yamt device_xname(self));
131 1.1.18.3 yamt return;
132 1.1.18.3 yamt }
133 1.1.18.3 yamt
134 1.1.18.2 yamt if (bus_space_map(sc->sc_memt, sc->sc_gen.sc_fboffset,
135 1.1.18.2 yamt sc->sc_gen.sc_fbsize, BUS_SPACE_MAP_LINEAR, &sc->sc_memh) != 0) {
136 1.1.18.2 yamt
137 1.1.18.2 yamt panic("%s: unable to map the framebuffer\n", self->dv_xname);
138 1.1.18.2 yamt }
139 1.1.18.2 yamt sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_memh);
140 1.1.18.2 yamt
141 1.1.18.2 yamt /* mmap()able bus ranges */
142 1.1.18.2 yamt idx = 0;
143 1.1.18.2 yamt bar = 0x10;
144 1.1.18.2 yamt while (bar < 0x30) {
145 1.1.18.2 yamt
146 1.1.18.2 yamt type = pci_mapreg_type(sc->sc_pc, sc->sc_pcitag, bar);
147 1.1.18.2 yamt if ((type == PCI_MAPREG_TYPE_MEM) ||
148 1.1.18.2 yamt (type == PCI_MAPREG_TYPE_ROM)) {
149 1.1.18.2 yamt
150 1.1.18.2 yamt pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, bar, type,
151 1.1.18.2 yamt &sc->sc_ranges[idx].offset,
152 1.1.18.2 yamt &sc->sc_ranges[idx].size,
153 1.1.18.2 yamt &sc->sc_ranges[idx].flags);
154 1.1.18.2 yamt idx++;
155 1.1.18.2 yamt }
156 1.1.18.3 yamt sc->sc_bars[(bar - 0x10) >> 2] =
157 1.1.18.3 yamt pci_conf_read(sc->sc_pc, sc->sc_pcitag, bar);
158 1.1.18.2 yamt bar += 4;
159 1.1.18.2 yamt }
160 1.1.18.2 yamt sc->sc_ranges_used = idx;
161 1.1.18.2 yamt
162 1.1.18.2 yamt ops.genfb_ioctl = pci_genfb_ioctl;
163 1.1.18.2 yamt ops.genfb_mmap = pci_genfb_mmap;
164 1.1.18.2 yamt
165 1.1.18.5 yamt if (genfb_attach(&sc->sc_gen, &ops) == 0) {
166 1.1.18.3 yamt
167 1.1.18.5 yamt /* now try to attach a DRM */
168 1.1.18.5 yamt config_found_ia(self, "drm", aux, pci_genfb_drm_print);
169 1.1.18.5 yamt }
170 1.1.18.2 yamt }
171 1.1.18.2 yamt
172 1.1.18.2 yamt static int
173 1.1.18.3 yamt pci_genfb_drm_print(void *aux, const char *pnp)
174 1.1.18.3 yamt {
175 1.1.18.3 yamt if (pnp)
176 1.1.18.3 yamt aprint_normal("direct rendering for %s", pnp);
177 1.1.18.3 yamt return (UNSUPP);
178 1.1.18.3 yamt }
179 1.1.18.3 yamt
180 1.1.18.3 yamt
181 1.1.18.3 yamt static int
182 1.1.18.2 yamt pci_genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
183 1.1.18.2 yamt struct lwp *l)
184 1.1.18.2 yamt {
185 1.1.18.2 yamt struct pci_genfb_softc *sc = v;
186 1.1.18.2 yamt
187 1.1.18.2 yamt switch (cmd) {
188 1.1.18.2 yamt case WSDISPLAYIO_GTYPE:
189 1.1.18.2 yamt *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
190 1.1.18.2 yamt return 0;
191 1.1.18.2 yamt
192 1.1.18.2 yamt /* PCI config read/write passthrough. */
193 1.1.18.2 yamt case PCI_IOC_CFGREAD:
194 1.1.18.2 yamt case PCI_IOC_CFGWRITE:
195 1.1.18.2 yamt return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
196 1.1.18.2 yamt cmd, data, flag, l));
197 1.1.18.3 yamt case WSDISPLAYIO_SMODE:
198 1.1.18.3 yamt {
199 1.1.18.3 yamt int new_mode = *(int*)data, i;
200 1.1.18.3 yamt if (new_mode == WSDISPLAYIO_MODE_EMUL) {
201 1.1.18.3 yamt for (i = 0; i < 9; i++)
202 1.1.18.3 yamt pci_conf_write(sc->sc_pc,
203 1.1.18.3 yamt sc->sc_pcitag,
204 1.1.18.3 yamt 0x10 + (i << 2),
205 1.1.18.3 yamt sc->sc_bars[i]);
206 1.1.18.3 yamt }
207 1.1.18.3 yamt }
208 1.1.18.3 yamt return 0;
209 1.1.18.2 yamt }
210 1.1.18.2 yamt
211 1.1.18.2 yamt return EPASSTHROUGH;
212 1.1.18.2 yamt }
213 1.1.18.2 yamt
214 1.1.18.2 yamt static paddr_t
215 1.1.18.2 yamt pci_genfb_mmap(void *v, void *vs, off_t offset, int prot)
216 1.1.18.2 yamt {
217 1.1.18.2 yamt struct pci_genfb_softc *sc = v;
218 1.1.18.2 yamt struct range *r;
219 1.1.18.2 yamt struct lwp *me;
220 1.1.18.2 yamt int i;
221 1.1.18.2 yamt
222 1.1.18.4 yamt if (offset == 0)
223 1.1.18.4 yamt sc->sc_want_wsfb = 1;
224 1.1.18.4 yamt
225 1.1.18.4 yamt /*
226 1.1.18.4 yamt * regular fb mapping at 0
227 1.1.18.4 yamt * since some Sun firmware likes to put PCI resources low enough
228 1.1.18.4 yamt * to collide with the wsfb mapping we only allow it after asking
229 1.1.18.4 yamt * for offset 0
230 1.1.18.4 yamt */
231 1.1.18.3 yamt DPRINTF("%s: %08x limit %08x\n", __func__, (uint32_t)offset,
232 1.1.18.3 yamt (uint32_t)sc->sc_gen.sc_fbsize);
233 1.1.18.4 yamt if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize) &&
234 1.1.18.4 yamt (sc->sc_want_wsfb == 1)) {
235 1.1.18.2 yamt
236 1.1.18.2 yamt return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
237 1.1.18.2 yamt offset, prot, BUS_SPACE_MAP_LINEAR);
238 1.1.18.2 yamt }
239 1.1.18.2 yamt
240 1.1.18.2 yamt /*
241 1.1.18.2 yamt * restrict all other mappings to processes with superuser privileges
242 1.1.18.2 yamt * or the kernel itself
243 1.1.18.2 yamt */
244 1.1.18.2 yamt me = curlwp;
245 1.1.18.2 yamt if (me != NULL) {
246 1.1.18.2 yamt if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
247 1.1.18.2 yamt NULL) != 0) {
248 1.1.18.2 yamt aprint_normal("%s: mmap() rejected.\n",
249 1.1.18.2 yamt sc->sc_gen.sc_dev.dv_xname);
250 1.1.18.2 yamt return -1;
251 1.1.18.2 yamt }
252 1.1.18.2 yamt }
253 1.1.18.2 yamt
254 1.1.18.2 yamt #ifdef WSFB_FAKE_VGA_FB
255 1.1.18.2 yamt if ((offset >= 0xa0000) && (offset < 0xbffff)) {
256 1.1.18.2 yamt
257 1.1.18.2 yamt return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
258 1.1.18.2 yamt offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR);
259 1.1.18.2 yamt }
260 1.1.18.2 yamt #endif
261 1.1.18.2 yamt
262 1.1.18.2 yamt /*
263 1.1.18.2 yamt * XXX this should be generalized, let's just
264 1.1.18.2 yamt * #define PCI_IOAREA_PADDR
265 1.1.18.2 yamt * #define PCI_IOAREA_OFFSET
266 1.1.18.2 yamt * #define PCI_IOAREA_SIZE
267 1.1.18.2 yamt * somewhere in a MD header and compile this code only if all are
268 1.1.18.2 yamt * present
269 1.1.18.2 yamt */
270 1.1.18.5 yamt #ifdef PCI_MAGIC_IO_RANGE
271 1.1.18.2 yamt /* allow to map our IO space */
272 1.1.18.5 yamt if ((offset >= PCI_MAGIC_IO_RANGE) &&
273 1.1.18.5 yamt (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
274 1.1.18.5 yamt return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
275 1.1.18.5 yamt 0, prot, BUS_SPACE_MAP_LINEAR);
276 1.1.18.2 yamt }
277 1.1.18.2 yamt #endif
278 1.1.18.2 yamt
279 1.1.18.2 yamt /* allow to mmap() our BARs */
280 1.1.18.3 yamt /* maybe the ROM BAR too? */
281 1.1.18.2 yamt for (i = 0; i < sc->sc_ranges_used; i++) {
282 1.1.18.2 yamt
283 1.1.18.2 yamt r = &sc->sc_ranges[i];
284 1.1.18.2 yamt if ((offset >= r->offset) && (offset < (r->offset + r->size))) {
285 1.1.18.2 yamt return bus_space_mmap(sc->sc_memt, offset, 0, prot,
286 1.1.18.2 yamt r->flags);
287 1.1.18.2 yamt }
288 1.1.18.2 yamt }
289 1.1.18.2 yamt
290 1.1.18.2 yamt return -1;
291 1.1.18.2 yamt }
292