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