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