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