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