ofb.c revision 1.44 1 /* $NetBSD: ofb.c,v 1.44 2006/02/07 02:29:28 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.44 2006/02/07 02:29:28 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
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_pci.h>
55
56 #include <machine/bus.h>
57 #include <machine/autoconf.h>
58 #include <machine/grfioctl.h>
59
60 #include <powerpc/oea/bat.h>
61
62 #include <macppc/dev/ofbvar.h>
63
64 #if OFB_ENABLE_CACHE
65 int ofb_enable_cache = 1;
66 #else
67 int ofb_enable_cache = 0;
68 #endif
69
70 static int ofbmatch(struct device *, struct cfdata *, void *);
71 static void ofbattach(struct device *, struct device *, void *);
72
73 CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
74 ofbmatch, ofbattach, NULL, NULL);
75
76 struct ofb_devconfig ofb_console_dc;
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 ofb_alloc_screen(void *, const struct wsscreen_descr *, void **,
98 int *, int *, long *);
99 static void ofb_free_screen(void *, void *);
100 static int ofb_show_screen(void *, void *, int, void (*) (void *, int, int),
101 void *);
102 static int copy_rom_font(void);
103
104 struct wsdisplay_accessops ofb_accessops = {
105 ofb_ioctl,
106 ofb_mmap,
107 ofb_alloc_screen,
108 ofb_free_screen,
109 ofb_show_screen,
110 0 /* load_font */
111 };
112
113 static struct wsdisplay_font openfirm6x11;
114
115 static void ofb_common_init(int, struct ofb_devconfig *);
116 static int ofb_getcmap(struct ofb_softc *, struct wsdisplay_cmap *);
117 static int ofb_putcmap(struct ofb_softc *, struct wsdisplay_cmap *);
118
119 int
120 ofbmatch(struct device *parent, struct cfdata *match, void *aux)
121 {
122 struct pci_attach_args *pa = aux;
123
124 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
125 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
126 return 1;
127
128 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
129 return 1;
130
131 return 0;
132 }
133
134 static void
135 ofbattach(struct device *parent, struct device *self, void *aux)
136 {
137 struct ofb_softc *sc = (struct ofb_softc *)self;
138 struct pci_attach_args *pa = aux;
139 struct wsemuldisplaydev_attach_args a;
140 int console, node;
141 struct ofb_devconfig *dc;
142 char devinfo[256];
143
144 sc->sc_memt = pa->pa_memt;
145 sc->sc_iot = pa->pa_iot;
146
147 console = ofb_is_console();
148
149 if (console) {
150 dc = &ofb_console_dc;
151 node = dc->dc_node;
152 sc->nscreens = 1;
153 } else {
154 int len;
155
156 dc = malloc(sizeof(struct ofb_devconfig), M_DEVBUF, M_WAITOK);
157 memset(dc, 0, sizeof(struct ofb_devconfig));
158 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
159 if (node == 0) {
160 printf(": ofdev not found\n");
161 return;
162 }
163
164 /* XXX There are two child screens on PowerBook. */
165 memset(devinfo, 0, sizeof(devinfo));
166 OF_getprop(node, "device_type", devinfo, sizeof(devinfo));
167 len = strlen(devinfo);
168 if (strcmp(devinfo + len - 7, "-parent") == 0)
169 node = OF_child(node);
170
171 ofb_common_init(node, dc);
172 }
173 sc->sc_dc = dc;
174
175 sc->sc_pc = pa->pa_pc;
176 sc->sc_pcitag = pa->pa_tag;
177
178 /* XXX */
179 if (OF_getprop(node, "assigned-addresses", sc->sc_addrs,
180 sizeof(sc->sc_addrs)) == -1) {
181 node = OF_parent(node);
182 OF_getprop(node, "assigned-addresses", sc->sc_addrs,
183 sizeof(sc->sc_addrs));
184 }
185
186 if (dc->dc_paddr == 0) {
187 printf(": cannot map framebuffer\n");
188 return;
189 }
190
191 /*
192 * Clear the screen here, instead of above, in case
193 * ofb_common_init() failed to map the framebuffer.
194 */
195 if (!console) {
196 int i, screenbytes;
197
198 screenbytes = dc->dc_ri.ri_stride * dc->dc_ri.ri_height;
199 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
200 *(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
201 }
202
203 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
204 printf(": %s\n", devinfo);
205 printf("%s: %d x %d, %dbpp\n", self->dv_xname,
206 dc->dc_ri.ri_width, dc->dc_ri.ri_height, dc->dc_ri.ri_depth);
207
208 sc->sc_cmap_red[0] = sc->sc_cmap_green[0] = sc->sc_cmap_blue[0] = 0;
209 sc->sc_cmap_red[15] = sc->sc_cmap_red[255] = 0xff;
210 sc->sc_cmap_green[15] = sc->sc_cmap_green[255] = 0xff;
211 sc->sc_cmap_blue[15] = sc->sc_cmap_blue[255] = 0xff;
212
213 a.console = console;
214 a.scrdata = &ofb_screenlist;
215 a.accessops = &ofb_accessops;
216 a.accesscookie = sc;
217
218 config_found(self, &a, wsemuldisplaydevprint);
219 }
220
221 static void
222 ofb_common_init(int node, struct ofb_devconfig *dc)
223 {
224 struct rasops_info *ri = &dc->dc_ri;
225 int32_t addr, width, height, linebytes, depth;
226
227 dc->dc_node = node;
228 if (dc->dc_ih == 0) {
229 char name[64];
230
231 memset(name, 0, 64);
232 OF_package_to_path(node, name, sizeof(name));
233 dc->dc_ih = OF_open(name);
234 }
235
236 /* XXX /chaos/control doesn't have "width", "height", ... */
237 width = height = -1;
238 if (OF_getprop(node, "width", &width, 4) != 4)
239 OF_interpret("screen-width", 1, &width);
240 if (OF_getprop(node, "height", &height, 4) != 4)
241 OF_interpret("screen-height", 1, &height);
242 if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
243 linebytes = width; /* XXX */
244 if (OF_getprop(node, "depth", &depth, 4) != 4)
245 depth = 8; /* XXX */
246 if (OF_getprop(node, "address", &addr, 4) != 4)
247 OF_interpret("frame-buffer-adr", 1, &addr);
248
249 if (width == -1 || height == -1 || addr == 0 || addr == -1)
250 return;
251
252 dc->dc_paddr = addr; /* PA of the frame buffer */
253
254 /* Make sure 0/0/0 is black and 255/255/255 is white. */
255 OF_call_method_1("color!", dc->dc_ih, 4, 0, 0, 0, 0);
256 OF_call_method_1("color!", dc->dc_ih, 4, 255, 255, 255, 255);
257
258 /* Enable write-through cache. */
259 if (ofb_enable_cache) {
260 vaddr_t va;
261 /*
262 * Let's try to find an empty BAT to use
263 */
264 for (va = SEGMENT_LENGTH; va < (USER_SR << ADDR_SR_SHFT);
265 va += SEGMENT_LENGTH) {
266 if (battable[va >> ADDR_SR_SHFT].batu == 0) {
267 battable[va >> ADDR_SR_SHFT].batl =
268 BATL(addr & 0xf0000000,
269 BAT_G | BAT_W | BAT_M, BAT_PP_RW);
270 battable[va >> ADDR_SR_SHFT].batu =
271 BATL(va, BAT_BL_256M, BAT_Vs);
272 addr &= 0x0fffffff;
273 addr |= va;
274 break;
275 }
276 }
277 }
278
279 /* initialize rasops */
280 ri->ri_width = width;
281 ri->ri_height = height;
282 ri->ri_depth = depth;
283 ri->ri_stride = linebytes;
284 ri->ri_bits = (char *)addr;
285 ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR | RI_CENTER;
286
287 /* If screen is smaller than 1024x768, use small font. */
288 if ((width < 1024 || height < 768) && copy_rom_font() == 0) {
289 int cols, rows;
290
291 OF_interpret("#lines", 1, &rows);
292 OF_interpret("#columns", 1, &cols);
293
294 ri->ri_font = &openfirm6x11;
295 ri->ri_wsfcookie = -1; /* not using wsfont */
296 rasops_init(ri, rows, cols);
297
298 ri->ri_xorigin = (width - cols * ri->ri_font->fontwidth) >> 1;
299 ri->ri_yorigin = (height - rows * ri->ri_font->fontheight)
300 >> 1;
301 ri->ri_bits = (char *)addr + ri->ri_xorigin +
302 ri->ri_stride * ri->ri_yorigin;
303 } else {
304 rasops_init(ri, 24, 80);
305 rasops_reconfig(ri, (height - 2) / ri->ri_font->fontheight,
306 ((width - 2) / ri->ri_font->fontwidth) & ~7);
307 }
308
309 /* black on white */
310 ri->ri_devcmap[0] = 0xffffffff; /* bg */
311 ri->ri_devcmap[1] = 0; /* fg */
312
313 ofb_stdscreen.nrows = ri->ri_rows;
314 ofb_stdscreen.ncols = ri->ri_cols;
315 ofb_stdscreen.textops = &ri->ri_ops;
316 ofb_stdscreen.capabilities = ri->ri_caps;
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 ofb_softc *sc = v;
339 struct ofb_devconfig *dc = sc->sc_dc;
340 struct wsdisplay_fbinfo *wdf;
341 struct grfinfo *gm;
342
343 switch (cmd) {
344 case WSDISPLAYIO_GTYPE:
345 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
346 return 0;
347
348 case WSDISPLAYIO_GINFO:
349 wdf = (void *)data;
350 wdf->height = dc->dc_ri.ri_height;
351 wdf->width = dc->dc_ri.ri_width;
352 wdf->depth = dc->dc_ri.ri_depth;
353 wdf->cmsize = 256;
354 return 0;
355
356 case WSDISPLAYIO_GETCMAP:
357 return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
358
359 case WSDISPLAYIO_PUTCMAP:
360 return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
361
362 /* XXX There are no way to know framebuffer pa from a user program. */
363 case GRFIOCGINFO:
364 gm = (void *)data;
365 memset(gm, 0, sizeof(struct grfinfo));
366 gm->gd_fbaddr = (caddr_t)dc->dc_paddr;
367 gm->gd_fbrowbytes = dc->dc_ri.ri_stride;
368 return 0;
369 /* PCI config read/write passthrough. */
370 case PCI_IOC_CFGREAD:
371 case PCI_IOC_CFGWRITE:
372 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
373 cmd, data, flag, l));
374 }
375 return EPASSTHROUGH;
376 }
377
378 static paddr_t
379 ofb_mmap(void *v, off_t offset, int prot)
380 {
381 struct ofb_softc *sc = v;
382 struct ofb_devconfig *dc = sc->sc_dc;
383 struct rasops_info *ri = &dc->dc_ri;
384 u_int32_t *ap = sc->sc_addrs;
385 struct proc *me;
386 int i;
387
388 /* framebuffer at offset 0 */
389 if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
390 return dc->dc_paddr + offset;
391
392 /*
393 * restrict all other mappings to processes with superuser privileges
394 * or the kernel itself
395 */
396 me = __curproc();
397 if (me != NULL) {
398 if (suser(me->p_ucred, NULL) != 0) {
399 return -1;
400 }
401 }
402
403 /* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
404 #ifdef OFB_FAKE_VGA_FB
405 if (offset >=0xa0000 && offset < 0xbffff)
406 return dc->dc_paddr + offset - 0xa0000;
407 #endif
408
409 /* allow to map our IO space */
410 if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
411 return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
412 BUS_SPACE_MAP_LINEAR);
413 }
414
415 for (i = 0; i < 6; i++) {
416 switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
417 case OFW_PCI_PHYS_HI_SPACE_MEM32:
418 if (offset >= ap[2] && offset < ap[2] + ap[4])
419 return bus_space_mmap(sc->sc_memt, offset, 0,
420 prot, BUS_SPACE_MAP_LINEAR);
421 }
422 ap += 5;
423 }
424
425 return -1;
426 }
427
428 static int
429 ofb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
430 int *curxp, int *curyp, long *attrp)
431 {
432 struct ofb_softc *sc = v;
433 struct rasops_info *ri = &sc->sc_dc->dc_ri;
434 long defattr;
435
436 if (sc->nscreens > 0)
437 return (ENOMEM);
438
439 *cookiep = ri; /* one and only for now */
440 *curxp = 0;
441 *curyp = 0;
442 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
443 *attrp = defattr;
444 sc->nscreens++;
445 return 0;
446 }
447
448 static void
449 ofb_free_screen(void *v, void *cookie)
450 {
451 struct ofb_softc *sc = v;
452
453 if (sc->sc_dc == &ofb_console_dc)
454 panic("ofb_free_screen: console");
455
456 sc->nscreens--;
457 }
458
459 static int
460 ofb_show_screen(void *v, void *cookie, int waitok,
461 void (*cb)(void *, int, int), void *cbarg)
462 {
463
464 return (0);
465 }
466
467 int
468 ofb_cnattach()
469 {
470 struct ofb_devconfig *dc = &ofb_console_dc;
471 struct rasops_info *ri = &dc->dc_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 dc->dc_ih = stdout;
480
481 /* get current cursor position */
482 OF_interpret("line#", 1, &crow);
483
484 /* move (rom monitor) cursor to the lowest line - 1 */
485 OF_interpret("#lines 2 - to line#", 0);
486
487 ofb_common_init(node, dc);
488
489 if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
490 int i, screenbytes = ri->ri_stride * ri->ri_height;
491
492 for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
493 *(u_int32_t *)(dc->dc_paddr + i) = 0xffffffff;
494 crow = 0;
495 }
496
497 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
498 wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
499
500 return 0;
501 }
502
503 static int
504 copy_rom_font()
505 {
506 u_char *romfont;
507 int char_width, char_height;
508 int chosen, mmu, m, e;
509
510 /* Get ROM FONT address. */
511 OF_interpret("font-adr", 1, &romfont);
512 if (romfont == NULL)
513 return -1;
514
515 chosen = OF_finddevice("/chosen");
516 OF_getprop(chosen, "mmu", &mmu, 4);
517
518 /*
519 * Convert to physcal address. We cannot access to Open Firmware's
520 * virtual address space.
521 */
522 OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
523
524 /* Get character size */
525 OF_interpret("char-width", 1, &char_width);
526 OF_interpret("char-height", 1, &char_height);
527
528 openfirm6x11.name = "Open Firmware";
529 openfirm6x11.firstchar = 32;
530 openfirm6x11.numchars = 96;
531 openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
532 openfirm6x11.fontwidth = char_width;
533 openfirm6x11.fontheight = char_height;
534 openfirm6x11.stride = 1;
535 openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
536 openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
537 openfirm6x11.data = romfont;
538
539 return 0;
540 }
541
542 static int
543 ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
544 {
545 u_int index = cm->index;
546 u_int count = cm->count;
547 int error;
548
549 if (index >= 256 || count > 256 || index + count > 256)
550 return EINVAL;
551
552 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
553 if (error)
554 return error;
555 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
556 if (error)
557 return error;
558 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
559 if (error)
560 return error;
561
562 return 0;
563 }
564
565 static int
566 ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
567 {
568 struct ofb_devconfig *dc = sc->sc_dc;
569 u_int index = cm->index;
570 u_int count = cm->count;
571 int i, error;
572 u_char rbuf[256], gbuf[256], bbuf[256];
573 u_char *r, *g, *b;
574
575 if (cm->index >= 256 || cm->count > 256 ||
576 (cm->index + cm->count) > 256)
577 return EINVAL;
578 error = copyin(cm->red, &rbuf[index], count);
579 if (error)
580 return error;
581 error = copyin(cm->green, &gbuf[index], count);
582 if (error)
583 return error;
584 error = copyin(cm->blue, &bbuf[index], count);
585 if (error)
586 return error;
587
588 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
589 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
590 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
591
592 r = &sc->sc_cmap_red[index];
593 g = &sc->sc_cmap_green[index];
594 b = &sc->sc_cmap_blue[index];
595 for (i = 0; i < count; i++) {
596 OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
597 r++, g++, b++, index++;
598 }
599 return 0;
600 }
601