ofb.c revision 1.60 1 /* $NetBSD: ofb.c,v 1.60 2007/07/24 20:09:57 macallan Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: ofb.c,v 1.60 2007/07/24 20:09:57 macallan Exp $");
32
33 #include <sys/param.h>
34 #include <sys/buf.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 #include <sys/ioctl.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
41 #include <sys/kauth.h>
42 #include <sys/lwp.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <dev/pci/pcidevs.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pciio.h>
50
51 #include <dev/wscons/wsconsio.h>
52 #include <dev/wscons/wsdisplayvar.h>
53 #include <dev/rasops/rasops.h>
54 #include <dev/wsfont/wsfont.h>
55
56 #include <dev/ofw/openfirm.h>
57 #include <dev/ofw/ofw_pci.h>
58
59 #include <machine/bus.h>
60 #include <machine/autoconf.h>
61 #include <machine/grfioctl.h>
62
63 #include <dev/wscons/wsdisplay_vconsvar.h>
64
65 #include <macppc/dev/ofbvar.h>
66
67 struct ofb_softc {
68 struct device sc_dev;
69
70 pci_chipset_tag_t sc_pc;
71 pcitag_t sc_pcitag;
72 bus_space_tag_t sc_memt;
73 bus_space_tag_t sc_iot;
74
75 u_int32_t sc_addrs[30]; /* "assigned-addresses" storage */
76 u_char sc_cmap_red[256];
77 u_char sc_cmap_green[256];
78 u_char sc_cmap_blue[256];
79
80 int sc_node, sc_ih, sc_mode;
81 paddr_t sc_fbaddr;
82
83 struct vcons_data vd;
84 };
85
86 static int ofbmatch(struct device *, struct cfdata *, void *);
87 static void ofbattach(struct device *, struct device *, void *);
88
89 CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
90 ofbmatch, ofbattach, NULL, NULL);
91
92 const struct wsscreen_descr *_ofb_scrlist[] = {
93 &ofb_stdscreen,
94 /* XXX other formats, graphics screen? */
95 };
96
97 struct wsscreen_list ofb_screenlist = {
98 sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
99 };
100
101 static int ofb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
102 static paddr_t ofb_mmap(void *, void *, off_t, int);
103
104 static void ofb_init_screen(void *, struct vcons_screen *, int, long *);
105
106 struct wsdisplay_accessops ofb_accessops = {
107 ofb_ioctl,
108 ofb_mmap,
109 NULL, /* vcons_alloc_screen */
110 NULL, /* vcons_free_screen */
111 NULL, /* vcons_show_screen */
112 NULL /* load_font */
113 };
114
115 static void ofb_putpalreg(struct ofb_softc *, int, uint8_t, uint8_t,
116 uint8_t);
117
118 static int ofb_getcmap(struct ofb_softc *, struct wsdisplay_cmap *);
119 static int ofb_putcmap(struct ofb_softc *, struct wsdisplay_cmap *);
120 static void ofb_init_cmap(struct ofb_softc *);
121 static int ofb_drm_print(void *, const char *);
122
123 extern const u_char rasops_cmap[768];
124
125 extern int console_node;
126 extern int console_instance;
127
128 static int
129 ofbmatch(struct device *parent, struct cfdata *match, void *aux)
130 {
131 struct pci_attach_args *pa = aux;
132
133 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
134 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
135 return 1;
136
137 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
138 return 1;
139
140 return 0;
141 }
142
143 static void
144 ofbattach(struct device *parent, struct device *self, void *aux)
145 {
146 struct ofb_softc *sc = (struct ofb_softc *)self;
147 struct pci_attach_args *pa = aux;
148 struct wsemuldisplaydev_attach_args a;
149 struct rasops_info *ri = &ofb_console_screen.scr_ri;
150 long defattr;
151 int console, node, sub;
152 char devinfo[256];
153
154 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
155 printf(": %s\n", devinfo);
156
157 if (console_node == 0)
158 return;
159
160 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
161 console = (node == console_node);
162 if (!console) {
163 /* check if any of the childs matches */
164 sub = OF_child(node);
165 while ((sub != 0) && (sub != console_node)) {
166 sub = OF_peer(sub);
167 }
168 if (sub == console_node) {
169 console = true;
170 }
171 }
172
173 sc->sc_memt = pa->pa_memt;
174 sc->sc_iot = pa->pa_iot;
175 sc->sc_pc = pa->pa_pc;
176 sc->sc_pcitag = pa->pa_tag;
177 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
178
179 if (!console)
180 return;
181
182 vcons_init(&sc->vd, sc, &ofb_stdscreen, &ofb_accessops);
183 sc->vd.init_screen = ofb_init_screen;
184
185 sc->sc_node = console_node;
186
187 sc->sc_ih = console_instance;
188 vcons_init_screen(&sc->vd, &ofb_console_screen, 1, &defattr);
189 ofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
190
191 printf("%s: %d x %d, %dbpp\n", self->dv_xname,
192 ri->ri_width, ri->ri_height, ri->ri_depth);
193
194 sc->sc_fbaddr = 0;
195 if (OF_getprop(sc->sc_node, "address", &sc->sc_fbaddr, 4) != 4)
196 OF_interpret("frame-buffer-adr", 0, 1, &sc->sc_fbaddr);
197 if (sc->sc_fbaddr == 0) {
198 printf("%s: Unable to find the framebuffer address.\n",
199 sc->sc_dev.dv_xname);
200 return;
201 }
202
203 /* XXX */
204 if (OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
205 sizeof(sc->sc_addrs)) == -1) {
206 sc->sc_node = OF_parent(sc->sc_node);
207 OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
208 sizeof(sc->sc_addrs));
209 }
210
211 ofb_init_cmap(sc);
212
213 a.console = console;
214 a.scrdata = &ofb_screenlist;
215 a.accessops = &ofb_accessops;
216 a.accesscookie = &sc->vd;
217
218 config_found(self, &a, wsemuldisplaydevprint);
219
220 config_found_ia(self, "drm", aux, ofb_drm_print);
221 }
222
223 static int
224 ofb_drm_print(void *aux, const char *pnp)
225 {
226 if (pnp)
227 aprint_normal("direct rendering for %s", pnp);
228 return (UNSUPP);
229 }
230
231 static void
232 ofb_init_screen(void *cookie, struct vcons_screen *scr,
233 int existing, long *defattr)
234 {
235 struct ofb_softc *sc = cookie;
236 struct rasops_info *ri = &scr->scr_ri;
237
238 if (scr != &ofb_console_screen) {
239 ofb_init_rasops(sc->sc_node, ri);
240 }
241 }
242
243 static int
244 ofb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
245 {
246 struct vcons_data *vd = v;
247 struct ofb_softc *sc = vd->cookie;
248 struct wsdisplay_fbinfo *wdf;
249 struct vcons_screen *ms = vd->active;
250 struct grfinfo *gm;
251
252 switch (cmd) {
253 case WSDISPLAYIO_GTYPE:
254 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
255 return 0;
256
257 case WSDISPLAYIO_GINFO:
258 /* we won't get here without any screen anyway */
259 if (ms != NULL) {
260 wdf = (void *)data;
261 wdf->height = ms->scr_ri.ri_width;
262 wdf->width = ms->scr_ri.ri_height;
263 wdf->depth = ms->scr_ri.ri_depth;
264 wdf->cmsize = 256;
265 return 0;
266 } else
267 return ENODEV;
268
269 case WSDISPLAYIO_GETCMAP:
270 return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
271
272 case WSDISPLAYIO_PUTCMAP:
273 return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
274
275 /* XXX There are no way to know framebuffer pa from a user program. */
276 case GRFIOCGINFO:
277 if (ms != NULL) {
278 gm = (void *)data;
279 memset(gm, 0, sizeof(struct grfinfo));
280 gm->gd_fbaddr = (void *)sc->sc_fbaddr;
281 gm->gd_fbrowbytes = ms->scr_ri.ri_stride;
282 return 0;
283 } else
284 return ENODEV;
285 case WSDISPLAYIO_LINEBYTES:
286 {
287 *(int *)data = ms->scr_ri.ri_stride;
288 return 0;
289 }
290 case WSDISPLAYIO_SMODE:
291 {
292 int new_mode = *(int*)data;
293 if (new_mode != sc->sc_mode)
294 {
295 sc->sc_mode = new_mode;
296 if (new_mode == WSDISPLAYIO_MODE_EMUL)
297 {
298 ofb_init_cmap(sc);
299 vcons_redraw_screen(ms);
300 }
301 }
302 }
303 return 0;
304 /* PCI config read/write passthrough. */
305 case PCI_IOC_CFGREAD:
306 case PCI_IOC_CFGWRITE:
307 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
308 cmd, data, flag, l));
309 }
310 return EPASSTHROUGH;
311 }
312
313 static paddr_t
314 ofb_mmap(void *v, void *vs, off_t offset, int prot)
315 {
316 struct vcons_data *vd = v;
317 struct ofb_softc *sc = vd->cookie;
318 struct rasops_info *ri;
319 u_int32_t *ap = sc->sc_addrs;
320 struct lwp *me;
321 int i;
322
323 if (vd->active == NULL) {
324 printf("%s: no active screen.\n", sc->sc_dev.dv_xname);
325 return -1;
326 }
327
328 ri = &vd->active->scr_ri;
329
330 /* framebuffer at offset 0 */
331 if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
332 return sc->sc_fbaddr + offset;
333
334 /*
335 * restrict all other mappings to processes with superuser privileges
336 * or the kernel itself
337 */
338 me = curlwp;
339 if (me != NULL) {
340 if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
341 NULL) != 0) {
342 printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
343 return -1;
344 }
345 }
346
347 /* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
348 #ifdef OFB_FAKE_VGA_FB
349 if (offset >=0xa0000 && offset < 0xbffff)
350 return sc->sc_fbaddr + offset - 0xa0000;
351 #endif
352
353 /* allow to map our IO space */
354 if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
355 return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
356 BUS_SPACE_MAP_LINEAR);
357 }
358
359 for (i = 0; i < 6; i++) {
360 switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
361 case OFW_PCI_PHYS_HI_SPACE_MEM32:
362 if (offset >= ap[2] && offset < ap[2] + ap[4])
363 return bus_space_mmap(sc->sc_memt, offset, 0,
364 prot, BUS_SPACE_MAP_LINEAR);
365 }
366 ap += 5;
367 }
368
369 return -1;
370 }
371
372 static int
373 ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
374 {
375 u_int index = cm->index;
376 u_int count = cm->count;
377 int error;
378
379 if (index >= 256 || count > 256 || index + count > 256)
380 return EINVAL;
381
382 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
383 if (error)
384 return error;
385 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
386 if (error)
387 return error;
388 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
389 if (error)
390 return error;
391
392 return 0;
393 }
394
395 int
396 ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
397 {
398 u_int index = cm->index;
399 u_int count = cm->count;
400 int i, error;
401 u_char rbuf[256], gbuf[256], bbuf[256];
402 u_char *r, *g, *b;
403
404 if (cm->index >= 256 || cm->count > 256 ||
405 (cm->index + cm->count) > 256)
406 return EINVAL;
407 error = copyin(cm->red, &rbuf[index], count);
408 if (error)
409 return error;
410 error = copyin(cm->green, &gbuf[index], count);
411 if (error)
412 return error;
413 error = copyin(cm->blue, &bbuf[index], count);
414 if (error)
415 return error;
416
417 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
418 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
419 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
420
421 r = &sc->sc_cmap_red[index];
422 g = &sc->sc_cmap_green[index];
423 b = &sc->sc_cmap_blue[index];
424 for (i = 0; i < count; i++) {
425 OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index);
426 r++, g++, b++, index++;
427 }
428 return 0;
429 }
430
431 static void
432 ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b)
433 {
434 if ((idx<0) || (idx > 255))
435 return;
436 if (sc != NULL) {
437 OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx);
438 sc->sc_cmap_red[idx] = r;
439 sc->sc_cmap_green[idx] = g;
440 sc->sc_cmap_blue[idx] = b;
441 } else {
442 OF_call_method_1("color!", console_instance, 4, r, g, b, idx);
443 }
444 }
445
446 static void
447 ofb_init_cmap(struct ofb_softc *sc)
448 {
449 int idx, i;
450 /* mess with the palette only when we're running in 8 bit */
451 if (ofb_console_screen.scr_ri.ri_depth == 8) {
452 idx = 0;
453 for (i = 0; i < 256; i++) {
454 ofb_putpalreg(sc, i, rasops_cmap[(i * 3) + 0],
455 rasops_cmap[(i * 3) + 1],
456 rasops_cmap[(i * 3) + 2]);
457 }
458 }
459 }
460