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