ofb.c revision 1.49 1 /* $NetBSD: ofb.c,v 1.49 2006/05/14 21:55:38 elad 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.49 2006/05/14 21:55:38 elad 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 proc *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 = __curproc();
437 if (me != NULL) {
438 if (kauth_authorize_generic(me->p_cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
439 printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
440 return -1;
441 }
442 }
443
444 /* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
445 #ifdef OFB_FAKE_VGA_FB
446 if (offset >=0xa0000 && offset < 0xbffff)
447 return sc->sc_fbaddr + offset - 0xa0000;
448 #endif
449
450 /* allow to map our IO space */
451 if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
452 return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
453 BUS_SPACE_MAP_LINEAR);
454 }
455
456 for (i = 0; i < 6; i++) {
457 switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
458 case OFW_PCI_PHYS_HI_SPACE_MEM32:
459 if (offset >= ap[2] && offset < ap[2] + ap[4])
460 return bus_space_mmap(sc->sc_memt, offset, 0,
461 prot, BUS_SPACE_MAP_LINEAR);
462 }
463 ap += 5;
464 }
465
466 return -1;
467 }
468
469 int
470 ofb_cnattach()
471 {
472 struct rasops_info *ri = &ofb_console_screen.scr_ri;
473 long defattr;
474 int crow = 0;
475 int chosen, stdout, node;
476
477 chosen = OF_finddevice("/chosen");
478 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
479 node = OF_instance_to_package(stdout);
480 console_node = node;
481 console_instance = stdout;
482
483 /* get current cursor position */
484 OF_interpret("line#", 1, &crow);
485
486 /* move (rom monitor) cursor to the lowest line - 1 */
487 OF_interpret("#lines 2 - to line#", 0);
488
489 wsfont_init();
490 if (copy_rom_font() == 0) {
491 romfont_loaded = 1;
492 }
493
494 /* set up rasops */
495 ofb_init_rasops(console_node, ri);
496
497 /*
498 * no need to clear the screen here when we're mimicing firmware
499 * output anyway
500 */
501 #if 0
502 if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
503 int i, screenbytes = ri->ri_stride * ri->ri_height;
504
505 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
506 *(u_int32_t *)(fbaddr + i) = 0xffffffff;
507 crow = 0;
508 }
509 #endif
510
511 ofb_stdscreen.nrows = ri->ri_rows;
512 ofb_stdscreen.ncols = ri->ri_cols;
513 ofb_stdscreen.textops = &ri->ri_ops;
514 ofb_stdscreen.capabilities = ri->ri_caps;
515
516 ri->ri_ops.allocattr(ri, 0, 0, 0, &defattr);
517 wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
518
519 return 0;
520 }
521
522 static int
523 copy_rom_font()
524 {
525 u_char *romfont;
526 int char_width, char_height;
527 int chosen, mmu, m, e;
528
529 /* Get ROM FONT address. */
530 OF_interpret("font-adr", 1, &romfont);
531 if (romfont == NULL)
532 return -1;
533
534 chosen = OF_finddevice("/chosen");
535 OF_getprop(chosen, "mmu", &mmu, 4);
536
537 /*
538 * Convert to physcal address. We cannot access to Open Firmware's
539 * virtual address space.
540 */
541 OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
542
543 /* Get character size */
544 OF_interpret("char-width", 1, &char_width);
545 OF_interpret("char-height", 1, &char_height);
546
547 openfirm6x11.name = "Open Firmware";
548 openfirm6x11.firstchar = 32;
549 openfirm6x11.numchars = 96;
550 openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
551 openfirm6x11.fontwidth = char_width;
552 openfirm6x11.fontheight = char_height;
553 openfirm6x11.stride = 1;
554 openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
555 openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
556 openfirm6x11.data = romfont;
557
558 return 0;
559 }
560
561 static int
562 ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
563 {
564 u_int index = cm->index;
565 u_int count = cm->count;
566 int error;
567
568 if (index >= 256 || count > 256 || index + count > 256)
569 return EINVAL;
570
571 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
572 if (error)
573 return error;
574 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
575 if (error)
576 return error;
577 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
578 if (error)
579 return error;
580
581 return 0;
582 }
583
584 int
585 ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
586 {
587 u_int index = cm->index;
588 u_int count = cm->count;
589 int i, error;
590 u_char rbuf[256], gbuf[256], bbuf[256];
591 u_char *r, *g, *b;
592
593 if (cm->index >= 256 || cm->count > 256 ||
594 (cm->index + cm->count) > 256)
595 return EINVAL;
596 error = copyin(cm->red, &rbuf[index], count);
597 if (error)
598 return error;
599 error = copyin(cm->green, &gbuf[index], count);
600 if (error)
601 return error;
602 error = copyin(cm->blue, &bbuf[index], count);
603 if (error)
604 return error;
605
606 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
607 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
608 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
609
610 r = &sc->sc_cmap_red[index];
611 g = &sc->sc_cmap_green[index];
612 b = &sc->sc_cmap_blue[index];
613 for (i = 0; i < count; i++) {
614 OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index);
615 r++, g++, b++, index++;
616 }
617 return 0;
618 }
619
620 static void
621 ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b)
622 {
623 if ((idx<0) || (idx > 255))
624 return;
625 OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx);
626 sc->sc_cmap_red[idx] = r;
627 sc->sc_cmap_green[idx] = g;
628 sc->sc_cmap_blue[idx] = b;
629 }
630
631 static void
632 ofb_init_cmap(struct ofb_softc *sc)
633 {
634 int idx, i;
635 /* mess with the palette only when we're running in 8 bit */
636 if (ofb_console_screen.scr_ri.ri_depth == 8) {
637 idx = 0;
638 for (i = 0; i < 256; i++) {
639 ofb_putpalreg(sc, i, rasops_cmap[idx],
640 rasops_cmap[idx + 1], rasops_cmap[idx + 2]);
641 idx += 3;
642 }
643 }
644 }
645