ofb.c revision 1.50 1 /* $NetBSD: ofb.c,v 1.50 2006/07/23 22:06:06 ad 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.50 2006/07/23 22:06:06 ad 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
43 #include <uvm/uvm_extern.h>
44
45 #include <dev/pci/pcidevs.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pciio.h>
49
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wsdisplayvar.h>
52 #include <dev/rasops/rasops.h>
53 #include <dev/wsfont/wsfont.h>
54
55 #include <dev/ofw/openfirm.h>
56 #include <dev/ofw/ofw_pci.h>
57
58 #include <machine/bus.h>
59 #include <machine/autoconf.h>
60 #include <machine/grfioctl.h>
61
62 #include <powerpc/oea/bat.h>
63
64 #include <dev/wscons/wsdisplay_vconsvar.h>
65 #include <macppc/dev/ofbvar.h>
66
67 #if OFB_ENABLE_CACHE
68 int ofb_enable_cache = 1;
69 #else
70 int ofb_enable_cache = 0;
71 #endif
72
73 static int ofbmatch(struct device *, struct cfdata *, void *);
74 static void ofbattach(struct device *, struct device *, void *);
75
76 CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
77 ofbmatch, ofbattach, NULL, NULL);
78
79 struct wsscreen_descr ofb_stdscreen = {
80 "std",
81 0, 0, /* will be filled in -- XXX shouldn't, it's global */
82 0,
83 0, 0,
84 WSSCREEN_REVERSE
85 };
86
87 const struct wsscreen_descr *_ofb_scrlist[] = {
88 &ofb_stdscreen,
89 /* XXX other formats, graphics screen? */
90 };
91
92 struct wsscreen_list ofb_screenlist = {
93 sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
94 };
95
96 static int ofb_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
97 static paddr_t ofb_mmap(void *, void *, off_t, int);
98 static int copy_rom_font(void);
99
100 static int ofb_init_rasops(int, struct rasops_info *);
101 static void ofb_init_screen(void *, struct vcons_screen *, int, long *);
102
103 struct wsdisplay_accessops ofb_accessops = {
104 ofb_ioctl,
105 ofb_mmap,
106 NULL, /* vcons_alloc_screen */
107 NULL, /* vcons_free_screen */
108 NULL, /* vcons_show_screen */
109 NULL /* load_font */
110 };
111
112 static struct vcons_screen ofb_console_screen;
113 static struct wsdisplay_font openfirm6x11;
114 static int console_node, console_instance;
115 static vaddr_t fbaddr;
116 static int romfont_loaded = 0;
117
118 static void ofb_putpalreg(struct ofb_softc *, int, uint8_t, uint8_t,
119 uint8_t);
120
121 static int ofb_getcmap(struct ofb_softc *, struct wsdisplay_cmap *);
122 static int ofb_putcmap(struct ofb_softc *, struct wsdisplay_cmap *);
123 static void ofb_init_cmap(struct ofb_softc *);
124
125 extern const u_char rasops_cmap[768];
126
127 static int
128 ofbmatch(struct device *parent, struct cfdata *match, void *aux)
129 {
130 struct pci_attach_args *pa = aux;
131
132 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
133 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
134 return 1;
135
136 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
137 return 1;
138
139 return 0;
140 }
141
142 static void
143 ofbattach(struct device *parent, struct device *self, void *aux)
144 {
145 struct ofb_softc *sc = (struct ofb_softc *)self;
146 struct pci_attach_args *pa = aux;
147 struct wsemuldisplaydev_attach_args a;
148 struct rasops_info *ri = &ofb_console_screen.scr_ri;
149 long defattr;
150 int console, len, node, sub;
151 char devinfo[256];
152
153 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
154 console = (node == console_node);
155 if (!console) {
156 /* check if any of the childs matches */
157 sub = OF_child(node);
158 while ((sub != 0) && (sub != console_node)) {
159 sub = OF_peer(sub);
160 }
161 if (sub == console_node) {
162 console = TRUE;
163 }
164 }
165
166 sc->sc_memt = pa->pa_memt;
167 sc->sc_iot = pa->pa_iot;
168 sc->sc_pc = pa->pa_pc;
169 sc->sc_pcitag = pa->pa_tag;
170 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
171
172 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
173 printf(": %s\n", devinfo);
174
175 if (!console)
176 return;
177
178 vcons_init(&sc->vd, sc, &ofb_stdscreen, &ofb_accessops);
179 sc->vd.init_screen = ofb_init_screen;
180
181 console = ofb_is_console();
182
183 sc->sc_node = node;
184
185 if (console) {
186 sc->sc_ih = console_instance;
187 vcons_init_screen(&sc->vd, &ofb_console_screen, 1, &defattr);
188 ofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
189 printf("%s: %d x %d, %dbpp\n", self->dv_xname,
190 ri->ri_width, ri->ri_height, ri->ri_depth);
191 } else {
192 char name[64];
193 if (sc->sc_node == 0) {
194 printf(": ofdev not found\n");
195 return;
196 }
197
198 /* XXX There are two child screens on PowerBook. */
199 memset(devinfo, 0, sizeof(devinfo));
200 OF_getprop(sc->sc_node, "device_type", devinfo, sizeof(devinfo));
201 len = strlen(devinfo);
202 if (strcmp(devinfo + len - 7, "-parent") == 0)
203 sc->sc_node = OF_child(sc->sc_node);
204
205 memset(name, 0, 64);
206 OF_package_to_path(sc->sc_node, name, sizeof(name));
207 sc->sc_ih = OF_open(name);
208
209 }
210
211 sc->sc_fbaddr = 0;
212 if (OF_getprop(sc->sc_node, "address", &sc->sc_fbaddr, 4) != 4)
213 OF_interpret("frame-buffer-adr", 1, &sc->sc_fbaddr);
214 if (sc->sc_fbaddr == 0) {
215 printf("%s: Unable to find the framebuffer address.\n",
216 sc->sc_dev.dv_xname);
217 return;
218 }
219
220 /* XXX */
221 if (OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
222 sizeof(sc->sc_addrs)) == -1) {
223 sc->sc_node = OF_parent(sc->sc_node);
224 OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
225 sizeof(sc->sc_addrs));
226 }
227
228 ofb_init_cmap(sc);
229
230 a.console = console;
231 a.scrdata = &ofb_screenlist;
232 a.accessops = &ofb_accessops;
233 a.accesscookie = &sc->vd;
234
235 config_found(self, &a, wsemuldisplaydevprint);
236 }
237
238 static void
239 ofb_init_screen(void *cookie, struct vcons_screen *scr,
240 int existing, long *defattr)
241 {
242 struct ofb_softc *sc = cookie;
243 struct rasops_info *ri = &scr->scr_ri;
244
245 if (scr != &ofb_console_screen) {
246 ofb_init_rasops(sc->sc_node, ri);
247 }
248 }
249
250 static int
251 ofb_init_rasops(int node, struct rasops_info *ri)
252 {
253 int32_t width, height, linebytes, depth;
254
255 /* XXX /chaos/control doesn't have "width", "height", ... */
256 width = height = -1;
257 if (OF_getprop(node, "width", &width, 4) != 4)
258 OF_interpret("screen-width", 1, &width);
259 if (OF_getprop(node, "height", &height, 4) != 4)
260 OF_interpret("screen-height", 1, &height);
261 if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
262 linebytes = width; /* XXX */
263 if (OF_getprop(node, "depth", &depth, 4) != 4)
264 depth = 8; /* XXX */
265 if (OF_getprop(node, "address", &fbaddr, 4) != 4)
266 OF_interpret("frame-buffer-adr", 1, &fbaddr);
267
268 if (width == -1 || height == -1 || fbaddr == 0 || fbaddr == -1)
269 return FALSE;
270
271 /* Enable write-through cache. */
272 if (ofb_enable_cache) {
273 vaddr_t va;
274 /*
275 * Let's try to find an empty BAT to use
276 */
277 for (va = SEGMENT_LENGTH; va < (USER_SR << ADDR_SR_SHFT);
278 va += SEGMENT_LENGTH) {
279 if (battable[va >> ADDR_SR_SHFT].batu == 0) {
280 battable[va >> ADDR_SR_SHFT].batl =
281 BATL(fbaddr & 0xf0000000,
282 BAT_G | BAT_W | BAT_M, BAT_PP_RW);
283 battable[va >> ADDR_SR_SHFT].batu =
284 BATL(va, BAT_BL_256M, BAT_Vs);
285 fbaddr &= 0x0fffffff;
286 fbaddr |= va;
287 break;
288 }
289 }
290 }
291
292 /* initialize rasops */
293 ri->ri_width = width;
294 ri->ri_height = height;
295 ri->ri_depth = depth;
296 ri->ri_stride = linebytes;
297 ri->ri_bits = (char *)fbaddr;
298 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
299
300 /* If screen is smaller than 1024x768, use small font. */
301 if ((width < 1024 || height < 768) && (romfont_loaded)) {
302 int cols, rows;
303
304 /*
305 * XXX this assumes we're the console which may or may not
306 * be the case
307 */
308 OF_interpret("#lines", 1, &rows);
309 OF_interpret("#columns", 1, &cols);
310 ri->ri_font = &openfirm6x11;
311 ri->ri_wsfcookie = -1; /* not using wsfont */
312 rasops_init(ri, rows, cols);
313
314 ri->ri_xorigin = (width - cols * ri->ri_font->fontwidth) >> 1;
315 ri->ri_yorigin = (height - rows * ri->ri_font->fontheight)
316 >> 1;
317 ri->ri_bits = (char *)fbaddr + ri->ri_xorigin +
318 ri->ri_stride * ri->ri_yorigin;
319 } else {
320 /* use as much of the screen as the font permits */
321 rasops_init(ri, height/8, width/8);
322 ri->ri_caps = WSSCREEN_WSCOLORS;
323 rasops_reconfig(ri, height / ri->ri_font->fontheight,
324 width / ri->ri_font->fontwidth);
325 }
326
327 return TRUE;
328 }
329
330 int
331 ofb_is_console()
332 {
333 int chosen, stdout, node;
334 char type[16];
335
336 chosen = OF_finddevice("/chosen");
337 OF_getprop(chosen, "stdout", &stdout, 4);
338 node = OF_instance_to_package(stdout);
339 OF_getprop(node, "device_type", type, sizeof(type));
340 if (strcmp(type, "display") == 0)
341 return 1;
342 else
343 return 0;
344 }
345
346 static int
347 ofb_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag, struct lwp *l)
348 {
349 struct vcons_data *vd = v;
350 struct ofb_softc *sc = vd->cookie;
351 struct wsdisplay_fbinfo *wdf;
352 struct vcons_screen *ms = vd->active;
353 struct grfinfo *gm;
354
355 switch (cmd) {
356 case WSDISPLAYIO_GTYPE:
357 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
358 return 0;
359
360 case WSDISPLAYIO_GINFO:
361 /* we won't get here without any screen anyway */
362 if (ms != NULL) {
363 wdf = (void *)data;
364 wdf->height = ms->scr_ri.ri_width;
365 wdf->width = ms->scr_ri.ri_height;
366 wdf->depth = ms->scr_ri.ri_depth;
367 wdf->cmsize = 256;
368 return 0;
369 } else
370 return ENODEV;
371
372 case WSDISPLAYIO_GETCMAP:
373 return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
374
375 case WSDISPLAYIO_PUTCMAP:
376 return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
377
378 /* XXX There are no way to know framebuffer pa from a user program. */
379 case GRFIOCGINFO:
380 if (ms != NULL) {
381 gm = (void *)data;
382 memset(gm, 0, sizeof(struct grfinfo));
383 gm->gd_fbaddr = (caddr_t)sc->sc_fbaddr;
384 gm->gd_fbrowbytes = ms->scr_ri.ri_stride;
385 return 0;
386 } else
387 return ENODEV;
388 case WSDISPLAYIO_SMODE:
389 {
390 int new_mode = *(int*)data;
391 if (new_mode != sc->sc_mode)
392 {
393 sc->sc_mode = new_mode;
394 if (new_mode == WSDISPLAYIO_MODE_EMUL)
395 {
396 ofb_init_cmap(sc);
397 vcons_redraw_screen(ms);
398 }
399 }
400 }
401 return 0;
402 /* PCI config read/write passthrough. */
403 case PCI_IOC_CFGREAD:
404 case PCI_IOC_CFGWRITE:
405 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
406 cmd, data, flag, l));
407 }
408 return EPASSTHROUGH;
409 }
410
411 static paddr_t
412 ofb_mmap(void *v, void *vs, off_t offset, int prot)
413 {
414 struct vcons_data *vd = v;
415 struct ofb_softc *sc = vd->cookie;
416 struct rasops_info *ri;
417 u_int32_t *ap = sc->sc_addrs;
418 struct lwp *me;
419 int i;
420
421 if (vd->active == NULL) {
422 printf("%s: no active screen.\n", sc->sc_dev.dv_xname);
423 return -1;
424 }
425
426 ri = &vd->active->scr_ri;
427
428 /* framebuffer at offset 0 */
429 if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
430 return sc->sc_fbaddr + offset;
431
432 /*
433 * restrict all other mappings to processes with superuser privileges
434 * or the kernel itself
435 */
436 me = curlwp;
437 if (me != NULL) {
438 if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
439 NULL) != 0) {
440 printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
441 return -1;
442 }
443 }
444
445 /* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
446 #ifdef OFB_FAKE_VGA_FB
447 if (offset >=0xa0000 && offset < 0xbffff)
448 return sc->sc_fbaddr + offset - 0xa0000;
449 #endif
450
451 /* allow to map our IO space */
452 if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
453 return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
454 BUS_SPACE_MAP_LINEAR);
455 }
456
457 for (i = 0; i < 6; i++) {
458 switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
459 case OFW_PCI_PHYS_HI_SPACE_MEM32:
460 if (offset >= ap[2] && offset < ap[2] + ap[4])
461 return bus_space_mmap(sc->sc_memt, offset, 0,
462 prot, BUS_SPACE_MAP_LINEAR);
463 }
464 ap += 5;
465 }
466
467 return -1;
468 }
469
470 int
471 ofb_cnattach()
472 {
473 struct rasops_info *ri = &ofb_console_screen.scr_ri;
474 long defattr;
475 int crow = 0;
476 int chosen, stdout, node;
477
478 chosen = OF_finddevice("/chosen");
479 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
480 node = OF_instance_to_package(stdout);
481 console_node = node;
482 console_instance = stdout;
483
484 /* get current cursor position */
485 OF_interpret("line#", 1, &crow);
486
487 /* move (rom monitor) cursor to the lowest line - 1 */
488 OF_interpret("#lines 2 - to line#", 0);
489
490 wsfont_init();
491 if (copy_rom_font() == 0) {
492 romfont_loaded = 1;
493 }
494
495 /* set up rasops */
496 ofb_init_rasops(console_node, ri);
497
498 /*
499 * no need to clear the screen here when we're mimicing firmware
500 * output anyway
501 */
502 #if 0
503 if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
504 int i, screenbytes = ri->ri_stride * ri->ri_height;
505
506 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
507 *(u_int32_t *)(fbaddr + i) = 0xffffffff;
508 crow = 0;
509 }
510 #endif
511
512 ofb_stdscreen.nrows = ri->ri_rows;
513 ofb_stdscreen.ncols = ri->ri_cols;
514 ofb_stdscreen.textops = &ri->ri_ops;
515 ofb_stdscreen.capabilities = ri->ri_caps;
516
517 ri->ri_ops.allocattr(ri, 0, 0, 0, &defattr);
518 wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
519
520 return 0;
521 }
522
523 static int
524 copy_rom_font()
525 {
526 u_char *romfont;
527 int char_width, char_height;
528 int chosen, mmu, m, e;
529
530 /* Get ROM FONT address. */
531 OF_interpret("font-adr", 1, &romfont);
532 if (romfont == NULL)
533 return -1;
534
535 chosen = OF_finddevice("/chosen");
536 OF_getprop(chosen, "mmu", &mmu, 4);
537
538 /*
539 * Convert to physcal address. We cannot access to Open Firmware's
540 * virtual address space.
541 */
542 OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
543
544 /* Get character size */
545 OF_interpret("char-width", 1, &char_width);
546 OF_interpret("char-height", 1, &char_height);
547
548 openfirm6x11.name = "Open Firmware";
549 openfirm6x11.firstchar = 32;
550 openfirm6x11.numchars = 96;
551 openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
552 openfirm6x11.fontwidth = char_width;
553 openfirm6x11.fontheight = char_height;
554 openfirm6x11.stride = 1;
555 openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
556 openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
557 openfirm6x11.data = romfont;
558
559 return 0;
560 }
561
562 static int
563 ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
564 {
565 u_int index = cm->index;
566 u_int count = cm->count;
567 int error;
568
569 if (index >= 256 || count > 256 || index + count > 256)
570 return EINVAL;
571
572 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
573 if (error)
574 return error;
575 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
576 if (error)
577 return error;
578 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
579 if (error)
580 return error;
581
582 return 0;
583 }
584
585 int
586 ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
587 {
588 u_int index = cm->index;
589 u_int count = cm->count;
590 int i, error;
591 u_char rbuf[256], gbuf[256], bbuf[256];
592 u_char *r, *g, *b;
593
594 if (cm->index >= 256 || cm->count > 256 ||
595 (cm->index + cm->count) > 256)
596 return EINVAL;
597 error = copyin(cm->red, &rbuf[index], count);
598 if (error)
599 return error;
600 error = copyin(cm->green, &gbuf[index], count);
601 if (error)
602 return error;
603 error = copyin(cm->blue, &bbuf[index], count);
604 if (error)
605 return error;
606
607 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
608 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
609 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
610
611 r = &sc->sc_cmap_red[index];
612 g = &sc->sc_cmap_green[index];
613 b = &sc->sc_cmap_blue[index];
614 for (i = 0; i < count; i++) {
615 OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index);
616 r++, g++, b++, index++;
617 }
618 return 0;
619 }
620
621 static void
622 ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b)
623 {
624 if ((idx<0) || (idx > 255))
625 return;
626 OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx);
627 sc->sc_cmap_red[idx] = r;
628 sc->sc_cmap_green[idx] = g;
629 sc->sc_cmap_blue[idx] = b;
630 }
631
632 static void
633 ofb_init_cmap(struct ofb_softc *sc)
634 {
635 int idx, i;
636 /* mess with the palette only when we're running in 8 bit */
637 if (ofb_console_screen.scr_ri.ri_depth == 8) {
638 idx = 0;
639 for (i = 0; i < 256; i++) {
640 ofb_putpalreg(sc, i, rasops_cmap[idx],
641 rasops_cmap[idx + 1], rasops_cmap[idx + 2]);
642 idx += 3;
643 }
644 }
645 }
646