1 /* $NetBSD: ofb.c,v 1.63 2007/11/26 19:58:29 garbled 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@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.63 2007/11/26 19:58:29 garbled 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 #include <sys/lwp.h> 43 44 #include <uvm/uvm_extern.h> 45 46 #include <dev/pci/pcidevs.h> 47 #include <dev/pci/pcireg.h> 48 #include <dev/pci/pcivar.h> 49 #include <dev/pci/pciio.h> 50 51 #include <dev/wscons/wsconsio.h> 52 #include <dev/wscons/wsdisplayvar.h> 53 #include <dev/rasops/rasops.h> 54 #include <dev/wsfont/wsfont.h> 55 56 #include <dev/ofw/openfirm.h> 57 #include <dev/ofw/ofw_pci.h> 58 59 #include <machine/bus.h> 60 #include <machine/autoconf.h> 61 #include <machine/grfioctl.h> 62 63 #include <dev/wscons/wsdisplay_vconsvar.h> 64 65 #include <powerpc/oea/ofw_rasconsvar.h> 66 67 struct ofb_softc { 68 struct device sc_dev; 69 70 pci_chipset_tag_t sc_pc; 71 pcitag_t sc_pcitag; 72 bus_space_tag_t sc_memt; 73 bus_space_tag_t sc_iot; 74 75 u_int32_t sc_addrs[30]; /* "assigned-addresses" storage */ 76 u_char sc_cmap_red[256]; 77 u_char sc_cmap_green[256]; 78 u_char sc_cmap_blue[256]; 79 80 int sc_node, sc_ih, sc_mode; 81 paddr_t sc_fbaddr; 82 uint32_t sc_fbsize; 83 84 struct vcons_data vd; 85 }; 86 87 static int ofbmatch(struct device *, struct cfdata *, void *); 88 static void ofbattach(struct device *, struct device *, void *); 89 90 CFATTACH_DECL(ofb, sizeof(struct ofb_softc), 91 ofbmatch, ofbattach, NULL, NULL); 92 93 const struct wsscreen_descr *_ofb_scrlist[] = { 94 &rascons_stdscreen, 95 /* XXX other formats, graphics screen? */ 96 }; 97 98 struct wsscreen_list ofb_screenlist = { 99 sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist 100 }; 101 102 static int ofb_ioctl(void *, void *, u_long, void *, int, struct lwp *); 103 static paddr_t ofb_mmap(void *, void *, off_t, int); 104 105 static void ofb_init_screen(void *, struct vcons_screen *, int, long *); 106 107 struct wsdisplay_accessops ofb_accessops = { 108 ofb_ioctl, 109 ofb_mmap, 110 NULL, /* vcons_alloc_screen */ 111 NULL, /* vcons_free_screen */ 112 NULL, /* vcons_show_screen */ 113 NULL /* load_font */ 114 }; 115 116 static void ofb_putpalreg(struct ofb_softc *, int, uint8_t, uint8_t, 117 uint8_t); 118 119 static int ofb_getcmap(struct ofb_softc *, struct wsdisplay_cmap *); 120 static int ofb_putcmap(struct ofb_softc *, struct wsdisplay_cmap *); 121 static void ofb_init_cmap(struct ofb_softc *); 122 static int ofb_drm_print(void *, const char *); 123 124 extern const u_char rasops_cmap[768]; 125 126 extern int console_node; 127 extern int console_instance; 128 129 static int 130 ofbmatch(struct device *parent, struct cfdata *match, void *aux) 131 { 132 struct pci_attach_args *pa = aux; 133 134 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE && 135 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL) 136 return 1; 137 138 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY) 139 return 1; 140 141 return 0; 142 } 143 144 static void 145 ofbattach(struct device *parent, struct device *self, void *aux) 146 { 147 struct ofb_softc *sc = (struct ofb_softc *)self; 148 struct pci_attach_args *pa = aux; 149 struct wsemuldisplaydev_attach_args a; 150 struct rasops_info *ri = &rascons_console_screen.scr_ri; 151 long defattr; 152 int console, node, sub; 153 char devinfo[256]; 154 155 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 156 printf(": %s\n", devinfo); 157 158 if (console_node == 0) 159 return; 160 161 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag); 162 console = (node == console_node); 163 if (!console) { 164 /* check if any of the childs matches */ 165 sub = OF_child(node); 166 while ((sub != 0) && (sub != console_node)) { 167 sub = OF_peer(sub); 168 } 169 if (sub == console_node) { 170 console = true; 171 } 172 } 173 174 sc->sc_memt = pa->pa_memt; 175 sc->sc_iot = pa->pa_iot; 176 sc->sc_pc = pa->pa_pc; 177 sc->sc_pcitag = pa->pa_tag; 178 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 179 180 if (!console) 181 return; 182 183 vcons_init(&sc->vd, sc, &rascons_stdscreen, &ofb_accessops); 184 sc->vd.init_screen = ofb_init_screen; 185 186 sc->sc_node = console_node; 187 188 sc->sc_ih = console_instance; 189 vcons_init_screen(&sc->vd, &rascons_console_screen, 1, &defattr); 190 rascons_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 191 192 printf("%s: %d x %d, %dbpp\n", self->dv_xname, 193 ri->ri_width, ri->ri_height, ri->ri_depth); 194 195 sc->sc_fbaddr = 0; 196 if (OF_getprop(sc->sc_node, "address", &sc->sc_fbaddr, 4) != 4) 197 OF_interpret("frame-buffer-adr", 0, 1, &sc->sc_fbaddr); 198 if (sc->sc_fbaddr == 0) { 199 printf("%s: Unable to find the framebuffer address.\n", 200 sc->sc_dev.dv_xname); 201 return; 202 } 203 sc->sc_fbsize = round_page(ri->ri_stride * ri->ri_height); 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 config_found_ia(self, "drm", aux, ofb_drm_print); 223 } 224 225 static int 226 ofb_drm_print(void *aux, const char *pnp) 227 { 228 if (pnp) 229 aprint_normal("direct rendering for %s", pnp); 230 return (UNSUPP); 231 } 232 233 static void 234 ofb_init_screen(void *cookie, struct vcons_screen *scr, 235 int existing, long *defattr) 236 { 237 struct ofb_softc *sc = cookie; 238 struct rasops_info *ri = &scr->scr_ri; 239 240 if (scr != &rascons_console_screen) { 241 rascons_init_rasops(sc->sc_node, ri); 242 } 243 } 244 245 static int 246 ofb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l) 247 { 248 struct vcons_data *vd = v; 249 struct ofb_softc *sc = vd->cookie; 250 struct wsdisplay_fbinfo *wdf; 251 struct vcons_screen *ms = vd->active; 252 struct grfinfo *gm; 253 254 switch (cmd) { 255 case WSDISPLAYIO_GTYPE: 256 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */ 257 return 0; 258 259 case WSDISPLAYIO_GINFO: 260 /* we won't get here without any screen anyway */ 261 if (ms != NULL) { 262 wdf = (void *)data; 263 wdf->height = ms->scr_ri.ri_height; 264 wdf->width = ms->scr_ri.ri_width; 265 wdf->depth = ms->scr_ri.ri_depth; 266 wdf->cmsize = 256; 267 return 0; 268 } else 269 return ENODEV; 270 271 case WSDISPLAYIO_GETCMAP: 272 return ofb_getcmap(sc, (struct wsdisplay_cmap *)data); 273 274 case WSDISPLAYIO_PUTCMAP: 275 return ofb_putcmap(sc, (struct wsdisplay_cmap *)data); 276 277 /* XXX There are no way to know framebuffer pa from a user program. */ 278 case GRFIOCGINFO: 279 if (ms != NULL) { 280 gm = (void *)data; 281 memset(gm, 0, sizeof(struct grfinfo)); 282 gm->gd_fbaddr = (void *)sc->sc_fbaddr; 283 gm->gd_fbrowbytes = ms->scr_ri.ri_stride; 284 return 0; 285 } else 286 return ENODEV; 287 case WSDISPLAYIO_LINEBYTES: 288 { 289 *(int *)data = ms->scr_ri.ri_stride; 290 return 0; 291 } 292 case WSDISPLAYIO_SMODE: 293 { 294 int new_mode = *(int*)data; 295 if (new_mode != sc->sc_mode) 296 { 297 sc->sc_mode = new_mode; 298 if (new_mode == WSDISPLAYIO_MODE_EMUL) 299 { 300 ofb_init_cmap(sc); 301 vcons_redraw_screen(ms); 302 } 303 } 304 } 305 return 0; 306 /* PCI config read/write passthrough. */ 307 case PCI_IOC_CFGREAD: 308 case PCI_IOC_CFGWRITE: 309 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag, 310 cmd, data, flag, l)); 311 } 312 return EPASSTHROUGH; 313 } 314 315 static paddr_t 316 ofb_mmap(void *v, void *vs, off_t offset, int prot) 317 { 318 struct vcons_data *vd = v; 319 struct ofb_softc *sc = vd->cookie; 320 struct rasops_info *ri; 321 u_int32_t *ap = sc->sc_addrs; 322 struct lwp *me; 323 int i; 324 325 if (vd->active == NULL) { 326 printf("%s: no active screen.\n", sc->sc_dev.dv_xname); 327 return -1; 328 } 329 330 ri = &vd->active->scr_ri; 331 332 /* framebuffer at offset 0 */ 333 if ((offset >= 0) && (offset < sc->sc_fbsize)) 334 return bus_space_mmap(sc->sc_memt, sc->sc_fbaddr, offset, prot, 335 BUS_SPACE_MAP_LINEAR); 336 337 /* 338 * restrict all other mappings to processes with superuser privileges 339 * or the kernel itself 340 */ 341 me = curlwp; 342 if (me != NULL) { 343 if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER, 344 NULL) != 0) { 345 printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname); 346 return -1; 347 } 348 } 349 350 /* let them mmap() 0xa0000 - 0xbffff if it's not covered above */ 351 #ifdef OFB_FAKE_VGA_FB 352 if (offset >=0xa0000 && offset < 0xbffff) 353 return sc->sc_fbaddr + offset - 0xa0000; 354 #endif 355 356 /* allow to map our IO space */ 357 if ((offset >= 0xf2000000) && (offset < 0xf2800000)) { 358 return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot, 359 BUS_SPACE_MAP_LINEAR); 360 } 361 362 for (i = 0; i < 6; i++) { 363 switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) { 364 case OFW_PCI_PHYS_HI_SPACE_MEM32: 365 if (offset >= ap[2] && offset < ap[2] + ap[4]) 366 return bus_space_mmap(sc->sc_memt, offset, 0, 367 prot, BUS_SPACE_MAP_LINEAR); 368 } 369 ap += 5; 370 } 371 return -1; 372 } 373 374 static int 375 ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm) 376 { 377 u_int index = cm->index; 378 u_int count = cm->count; 379 int error; 380 381 if (index >= 256 || count > 256 || index + count > 256) 382 return EINVAL; 383 384 error = copyout(&sc->sc_cmap_red[index], cm->red, count); 385 if (error) 386 return error; 387 error = copyout(&sc->sc_cmap_green[index], cm->green, count); 388 if (error) 389 return error; 390 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count); 391 if (error) 392 return error; 393 394 return 0; 395 } 396 397 int 398 ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm) 399 { 400 u_int index = cm->index; 401 u_int count = cm->count; 402 int i, error; 403 u_char rbuf[256], gbuf[256], bbuf[256]; 404 u_char *r, *g, *b; 405 406 if (cm->index >= 256 || cm->count > 256 || 407 (cm->index + cm->count) > 256) 408 return EINVAL; 409 error = copyin(cm->red, &rbuf[index], count); 410 if (error) 411 return error; 412 error = copyin(cm->green, &gbuf[index], count); 413 if (error) 414 return error; 415 error = copyin(cm->blue, &bbuf[index], count); 416 if (error) 417 return error; 418 419 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count); 420 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count); 421 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count); 422 423 r = &sc->sc_cmap_red[index]; 424 g = &sc->sc_cmap_green[index]; 425 b = &sc->sc_cmap_blue[index]; 426 for (i = 0; i < count; i++) { 427 OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index); 428 r++, g++, b++, index++; 429 } 430 return 0; 431 } 432 433 static void 434 ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b) 435 { 436 if ((idx<0) || (idx > 255)) 437 return; 438 if (sc != NULL) { 439 OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx); 440 sc->sc_cmap_red[idx] = r; 441 sc->sc_cmap_green[idx] = g; 442 sc->sc_cmap_blue[idx] = b; 443 } else { 444 OF_call_method_1("color!", console_instance, 4, r, g, b, idx); 445 } 446 } 447 448 static void 449 ofb_init_cmap(struct ofb_softc *sc) 450 { 451 int idx, i; 452 /* mess with the palette only when we're running in 8 bit */ 453 if (rascons_console_screen.scr_ri.ri_depth == 8) { 454 idx = 0; 455 for (i = 0; i < 256; i++) { 456 ofb_putpalreg(sc, i, rasops_cmap[(i * 3) + 0], 457 rasops_cmap[(i * 3) + 1], 458 rasops_cmap[(i * 3) + 2]); 459 } 460 } 461 } 462