1 /* $NetBSD: genfb.c,v 1.85 2021/12/24 18:12:58 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 Michael Lorenz 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.85 2021/12/24 18:12:58 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/device.h> 36 #include <sys/proc.h> 37 #include <sys/mutex.h> 38 #include <sys/ioctl.h> 39 #include <sys/kernel.h> 40 #include <sys/systm.h> 41 #include <sys/kmem.h> 42 #include <sys/reboot.h> 43 44 #include <uvm/uvm_extern.h> 45 46 #include <dev/wscons/wsconsio.h> 47 #include <dev/wscons/wsdisplayvar.h> 48 #include <dev/rasops/rasops.h> 49 #include <dev/wsfont/wsfont.h> 50 51 #include <dev/wscons/wsdisplay_vconsvar.h> 52 53 #include <dev/wsfb/genfbvar.h> 54 55 #include <dev/videomode/videomode.h> 56 #include <dev/videomode/edidvar.h> 57 58 #ifdef GENFB_DISABLE_TEXT 59 #define DISABLESPLASH (boothowto & (RB_SINGLE | RB_USERCONF | RB_ASKNAME | \ 60 AB_VERBOSE | AB_DEBUG) ) 61 #endif 62 63 #ifdef _KERNEL_OPT 64 #include "opt_genfb.h" 65 #include "opt_wsfb.h" 66 #include "opt_rasops.h" 67 #endif 68 69 #ifdef GENFB_DEBUG 70 #define GPRINTF panic 71 #else 72 #define GPRINTF aprint_debug 73 #endif 74 75 #define GENFB_BRIGHTNESS_STEP 15 76 #define GENFB_CHAR_WIDTH_MM 3 77 78 static int genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *); 79 static paddr_t genfb_mmap(void *, void *, off_t, int); 80 static void genfb_pollc(void *, int); 81 82 static void genfb_init_screen(void *, struct vcons_screen *, int, long *); 83 static int genfb_calc_hsize(struct genfb_softc *); 84 static int genfb_calc_cols(struct genfb_softc *, struct rasops_info *); 85 86 static int genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *); 87 static int genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *); 88 static int genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t, 89 uint8_t, uint8_t); 90 static void genfb_init_palette(struct genfb_softc *); 91 92 static void genfb_brightness_up(device_t); 93 static void genfb_brightness_down(device_t); 94 95 #if GENFB_GLYPHCACHE > 0 96 static int genfb_setup_glyphcache(struct genfb_softc *, long); 97 static void genfb_putchar(void *, int, int, u_int, long); 98 #endif 99 100 extern const u_char rasops_cmap[768]; 101 102 static int genfb_cnattach_called = 0; 103 static int genfb_enabled = 1; 104 105 static struct genfb_softc *genfb_softc = NULL; 106 107 void 108 genfb_init(struct genfb_softc *sc) 109 { 110 prop_dictionary_t dict; 111 uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb, br_cb, fbaddr; 112 uint64_t fboffset; 113 bool console; 114 115 dict = device_properties(sc->sc_dev); 116 #ifdef GENFB_DEBUG 117 printf("%s", prop_dictionary_externalize(dict)); 118 #endif 119 prop_dictionary_get_bool(dict, "is_console", &console); 120 121 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) { 122 GPRINTF("no width property\n"); 123 return; 124 } 125 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) { 126 GPRINTF("no height property\n"); 127 return; 128 } 129 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) { 130 GPRINTF("no depth property\n"); 131 return; 132 } 133 134 if (!prop_dictionary_get_uint64(dict, "address", &fboffset)) { 135 GPRINTF("no address property\n"); 136 return; 137 } 138 139 sc->sc_fboffset = (bus_addr_t)fboffset; 140 141 sc->sc_fbaddr = NULL; 142 if (prop_dictionary_get_uint64(dict, "virtual_address", &fbaddr)) { 143 sc->sc_fbaddr = (void *)(uintptr_t)fbaddr; 144 } 145 146 sc->sc_shadowfb = NULL; 147 if (!prop_dictionary_get_bool(dict, "enable_shadowfb", 148 &sc->sc_enable_shadowfb)) 149 #ifdef GENFB_SHADOWFB 150 sc->sc_enable_shadowfb = true; 151 #else 152 sc->sc_enable_shadowfb = false; 153 #endif 154 155 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) 156 sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3; 157 158 /* 159 * deal with a bug in the Raptor firmware which always sets 160 * stride = width even when depth != 8 161 */ 162 if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3)) 163 sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3); 164 165 sc->sc_fbsize = sc->sc_height * sc->sc_stride; 166 167 /* optional colour map callback */ 168 sc->sc_cmcb = NULL; 169 if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) { 170 if (cmap_cb != 0) 171 sc->sc_cmcb = (void *)(vaddr_t)cmap_cb; 172 } 173 174 /* optional pmf callback */ 175 sc->sc_pmfcb = NULL; 176 if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) { 177 if (pmf_cb != 0) 178 sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb; 179 } 180 181 /* optional mode callback */ 182 sc->sc_modecb = NULL; 183 if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) { 184 if (mode_cb != 0) 185 sc->sc_modecb = (void *)(vaddr_t)mode_cb; 186 } 187 188 /* optional backlight control callback */ 189 sc->sc_backlight = NULL; 190 if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) { 191 if (bl_cb != 0) { 192 sc->sc_backlight = (void *)(vaddr_t)bl_cb; 193 aprint_naive_dev(sc->sc_dev, 194 "enabling backlight control\n"); 195 } 196 } 197 198 /* optional brightness control callback */ 199 sc->sc_brightness = NULL; 200 if (prop_dictionary_get_uint64(dict, "brightness_callback", &br_cb)) { 201 if (br_cb != 0) { 202 sc->sc_brightness = (void *)(vaddr_t)br_cb; 203 aprint_naive_dev(sc->sc_dev, 204 "enabling brightness control\n"); 205 if (console && 206 sc->sc_brightness->gpc_upd_parameter != NULL) { 207 pmf_event_register(sc->sc_dev, 208 PMFE_DISPLAY_BRIGHTNESS_UP, 209 genfb_brightness_up, TRUE); 210 pmf_event_register(sc->sc_dev, 211 PMFE_DISPLAY_BRIGHTNESS_DOWN, 212 genfb_brightness_down, TRUE); 213 } 214 } 215 } 216 } 217 218 int 219 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops) 220 { 221 struct wsemuldisplaydev_attach_args aa; 222 prop_dictionary_t dict; 223 struct rasops_info *ri; 224 paddr_t fb_phys; 225 uint16_t crow; 226 long defattr; 227 bool console; 228 #ifdef SPLASHSCREEN 229 int i, j; 230 int error = ENXIO; 231 #endif 232 233 dict = device_properties(sc->sc_dev); 234 prop_dictionary_get_bool(dict, "is_console", &console); 235 236 if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false) 237 crow = 0; 238 if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear) 239 == false) 240 sc->sc_want_clear = true; 241 242 fb_phys = (paddr_t)sc->sc_fboffset; 243 if (fb_phys == 0) { 244 KASSERT(sc->sc_fbaddr != NULL); 245 (void)pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_fbaddr, 246 &fb_phys); 247 } 248 249 aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, " 250 "stride %d\n", 251 fb_phys ? (void *)(intptr_t)fb_phys : sc->sc_fbaddr, 252 sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride); 253 254 sc->sc_defaultscreen_descr = (struct wsscreen_descr){ 255 "default", 256 0, 0, 257 NULL, 258 8, 16, 259 WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE | 260 WSSCREEN_RESIZE, 261 NULL 262 }; 263 sc->sc_screens[0] = &sc->sc_defaultscreen_descr; 264 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens}; 265 memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops)); 266 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 267 if (sc->sc_modecb != NULL) 268 sc->sc_modecb->gmc_setmode(sc, sc->sc_mode); 269 270 sc->sc_accessops.ioctl = genfb_ioctl; 271 sc->sc_accessops.mmap = genfb_mmap; 272 sc->sc_accessops.pollc = genfb_pollc; 273 274 if (sc->sc_enable_shadowfb) { 275 sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP); 276 if (sc->sc_want_clear == false) 277 memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize); 278 aprint_verbose_dev(sc->sc_dev, 279 "shadow framebuffer enabled, size %zu KB\n", 280 sc->sc_fbsize >> 10); 281 } 282 283 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, 284 &sc->sc_accessops); 285 sc->vd.init_screen = genfb_init_screen; 286 287 /* Do not print anything between this point and the screen 288 * clear operation below. Otherwise it will be lost. */ 289 290 ri = &sc->sc_console_screen.scr_ri; 291 292 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, 293 &defattr); 294 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 295 296 #if GENFB_GLYPHCACHE > 0 297 genfb_setup_glyphcache(sc, defattr); 298 #endif 299 300 #ifdef SPLASHSCREEN 301 /* 302 * If system isn't going to go multiuser, or user has requested to see 303 * boot text, don't render splash screen immediately 304 */ 305 if (DISABLESPLASH) 306 #endif 307 vcons_redraw_screen(&sc->sc_console_screen); 308 309 sc->sc_defaultscreen_descr.textops = &ri->ri_ops; 310 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps; 311 sc->sc_defaultscreen_descr.nrows = ri->ri_rows; 312 sc->sc_defaultscreen_descr.ncols = ri->ri_cols; 313 314 if (crow >= ri->ri_rows) { 315 crow = 0; 316 sc->sc_want_clear = 1; 317 } 318 319 if (console) 320 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow, 321 defattr); 322 323 /* Clear the whole screen to bring it to a known state. */ 324 if (sc->sc_want_clear) 325 (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr); 326 327 #ifdef SPLASHSCREEN 328 j = 0; 329 for (i = 0; i < uimin(1 << sc->sc_depth, 256); i++) { 330 if (i >= SPLASH_CMAP_OFFSET && 331 i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) { 332 splash_get_cmap(i, 333 &sc->sc_cmap_red[i], 334 &sc->sc_cmap_green[i], 335 &sc->sc_cmap_blue[i]); 336 } else { 337 sc->sc_cmap_red[i] = rasops_cmap[j]; 338 sc->sc_cmap_green[i] = rasops_cmap[j + 1]; 339 sc->sc_cmap_blue[i] = rasops_cmap[j + 2]; 340 } 341 j += 3; 342 } 343 genfb_restore_palette(sc); 344 345 sc->sc_splash.si_depth = sc->sc_depth; 346 sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_origbits; 347 sc->sc_splash.si_hwbits = sc->sc_fbaddr; 348 sc->sc_splash.si_width = sc->sc_width; 349 sc->sc_splash.si_height = sc->sc_height; 350 sc->sc_splash.si_stride = sc->sc_stride; 351 sc->sc_splash.si_fillrect = NULL; 352 if (!DISABLESPLASH) { 353 error = splash_render(&sc->sc_splash, 354 SPLASH_F_CENTER|SPLASH_F_FILL); 355 if (error) { 356 SCREEN_ENABLE_DRAWING(&sc->sc_console_screen); 357 genfb_init_palette(sc); 358 vcons_replay_msgbuf(&sc->sc_console_screen); 359 } 360 } 361 #else 362 genfb_init_palette(sc); 363 if (console && (boothowto & (AB_SILENT|AB_QUIET)) == 0) 364 vcons_replay_msgbuf(&sc->sc_console_screen); 365 #endif 366 367 if (genfb_softc == NULL) 368 genfb_softc = sc; 369 370 aa.console = console; 371 aa.scrdata = &sc->sc_screenlist; 372 aa.accessops = &sc->sc_accessops; 373 aa.accesscookie = &sc->vd; 374 375 #ifdef GENFB_DISABLE_TEXT 376 if (!DISABLESPLASH && error == 0) 377 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen); 378 #endif 379 380 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, 381 CFARGS(.iattr = "wsemuldisplaydev")); 382 383 return 0; 384 } 385 386 static int 387 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 388 struct lwp *l) 389 { 390 struct vcons_data *vd = v; 391 struct genfb_softc *sc = vd->cookie; 392 struct wsdisplay_fbinfo *wdf; 393 struct vcons_screen *ms = vd->active; 394 struct wsdisplay_param *param; 395 int new_mode, error, val, ret; 396 397 switch (cmd) { 398 case WSDISPLAYIO_GINFO: 399 if (ms == NULL) 400 return ENODEV; 401 wdf = (void *)data; 402 wdf->height = ms->scr_ri.ri_height; 403 wdf->width = ms->scr_ri.ri_width; 404 wdf->depth = ms->scr_ri.ri_depth; 405 wdf->cmsize = 256; 406 return 0; 407 408 case WSDISPLAYIO_GETCMAP: 409 return genfb_getcmap(sc, 410 (struct wsdisplay_cmap *)data); 411 412 case WSDISPLAYIO_PUTCMAP: 413 return genfb_putcmap(sc, 414 (struct wsdisplay_cmap *)data); 415 416 case WSDISPLAYIO_LINEBYTES: 417 *(u_int *)data = sc->sc_stride; 418 return 0; 419 420 case WSDISPLAYIO_SMODE: 421 new_mode = *(int *)data; 422 423 /* notify the bus backend */ 424 error = 0; 425 if (sc->sc_ops.genfb_ioctl) 426 error = sc->sc_ops.genfb_ioctl(sc, vs, 427 cmd, data, flag, l); 428 if (error && error != EPASSTHROUGH) 429 return error; 430 431 if (new_mode != sc->sc_mode) { 432 sc->sc_mode = new_mode; 433 if (sc->sc_modecb != NULL) 434 sc->sc_modecb->gmc_setmode(sc, 435 sc->sc_mode); 436 if (new_mode == WSDISPLAYIO_MODE_EMUL) { 437 genfb_restore_palette(sc); 438 vcons_redraw_screen(ms); 439 } 440 } 441 return 0; 442 443 case WSDISPLAYIO_SSPLASH: 444 #if defined(SPLASHSCREEN) 445 if(*(int *)data == 1) { 446 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen); 447 splash_render(&sc->sc_splash, 448 SPLASH_F_CENTER|SPLASH_F_FILL); 449 } else { 450 SCREEN_ENABLE_DRAWING(&sc->sc_console_screen); 451 genfb_init_palette(sc); 452 } 453 vcons_redraw_screen(ms); 454 return 0; 455 #else 456 return ENODEV; 457 #endif 458 case WSDISPLAYIO_GETPARAM: 459 param = (struct wsdisplay_param *)data; 460 switch (param->param) { 461 case WSDISPLAYIO_PARAM_BRIGHTNESS: 462 if (sc->sc_brightness == NULL) 463 return EPASSTHROUGH; 464 param->min = 0; 465 param->max = 255; 466 return sc->sc_brightness->gpc_get_parameter( 467 sc->sc_brightness->gpc_cookie, 468 ¶m->curval); 469 case WSDISPLAYIO_PARAM_BACKLIGHT: 470 if (sc->sc_backlight == NULL) 471 return EPASSTHROUGH; 472 param->min = 0; 473 param->max = 1; 474 return sc->sc_backlight->gpc_get_parameter( 475 sc->sc_backlight->gpc_cookie, 476 ¶m->curval); 477 } 478 return EPASSTHROUGH; 479 480 case WSDISPLAYIO_SETPARAM: 481 param = (struct wsdisplay_param *)data; 482 switch (param->param) { 483 case WSDISPLAYIO_PARAM_BRIGHTNESS: 484 if (sc->sc_brightness == NULL) 485 return EPASSTHROUGH; 486 val = param->curval; 487 if (val < 0) val = 0; 488 if (val > 255) val = 255; 489 return sc->sc_brightness->gpc_set_parameter( 490 sc->sc_brightness->gpc_cookie, val); 491 case WSDISPLAYIO_PARAM_BACKLIGHT: 492 if (sc->sc_backlight == NULL) 493 return EPASSTHROUGH; 494 val = param->curval; 495 if (val < 0) val = 0; 496 if (val > 1) val = 1; 497 return sc->sc_backlight->gpc_set_parameter( 498 sc->sc_backlight->gpc_cookie, val); 499 } 500 return EPASSTHROUGH; 501 } 502 ret = EPASSTHROUGH; 503 if (sc->sc_ops.genfb_ioctl) 504 ret = sc->sc_ops.genfb_ioctl(sc, vs, cmd, data, flag, l); 505 if (ret != EPASSTHROUGH) 506 return ret; 507 /* 508 * XXX 509 * handle these only if there either is no ioctl() handler or it didn't 510 * know how to deal with them. This allows bus frontends to override 511 * them but still provides fallback implementations 512 */ 513 switch (cmd) { 514 case WSDISPLAYIO_GET_EDID: { 515 struct wsdisplayio_edid_info *d = data; 516 return wsdisplayio_get_edid(sc->sc_dev, d); 517 } 518 519 case WSDISPLAYIO_GET_FBINFO: { 520 struct wsdisplayio_fbinfo *fbi = data; 521 return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi); 522 } 523 } 524 return EPASSTHROUGH; 525 } 526 527 static paddr_t 528 genfb_mmap(void *v, void *vs, off_t offset, int prot) 529 { 530 struct vcons_data *vd = v; 531 struct genfb_softc *sc = vd->cookie; 532 533 if (sc->sc_ops.genfb_mmap) 534 return sc->sc_ops.genfb_mmap(sc, vs, offset, prot); 535 536 return -1; 537 } 538 539 static void 540 genfb_pollc(void *v, int on) 541 { 542 struct vcons_data *vd = v; 543 struct genfb_softc *sc = vd->cookie; 544 545 if (sc == NULL) 546 return; 547 548 if (on) 549 genfb_enable_polling(sc->sc_dev); 550 else 551 genfb_disable_polling(sc->sc_dev); 552 } 553 554 static void 555 genfb_init_screen(void *cookie, struct vcons_screen *scr, 556 int existing, long *defattr) 557 { 558 struct genfb_softc *sc = cookie; 559 struct rasops_info *ri = &scr->scr_ri; 560 int wantcols; 561 bool is_bgr, is_swapped, is_10bit; 562 563 ri->ri_depth = sc->sc_depth; 564 ri->ri_width = sc->sc_width; 565 ri->ri_height = sc->sc_height; 566 ri->ri_stride = sc->sc_stride; 567 ri->ri_flg = RI_CENTER; 568 if (sc->sc_want_clear) 569 ri->ri_flg |= RI_FULLCLEAR; 570 571 scr->scr_flags |= VCONS_LOADFONT; 572 573 if (sc->sc_shadowfb != NULL) { 574 ri->ri_hwbits = (char *)sc->sc_fbaddr; 575 ri->ri_bits = (char *)sc->sc_shadowfb; 576 } else { 577 ri->ri_bits = (char *)sc->sc_fbaddr; 578 scr->scr_flags |= VCONS_DONT_READ; 579 } 580 581 if (existing && sc->sc_want_clear) 582 ri->ri_flg |= RI_CLEAR; 583 584 switch (ri->ri_depth) { 585 case 32: 586 case 24: 587 ri->ri_flg |= RI_ENABLE_ALPHA; 588 589 is_bgr = false; 590 prop_dictionary_get_bool(device_properties(sc->sc_dev), 591 "is_bgr", &is_bgr); 592 593 is_swapped = false; 594 prop_dictionary_get_bool(device_properties(sc->sc_dev), 595 "is_swapped", &is_swapped); 596 597 is_10bit = false; 598 prop_dictionary_get_bool(device_properties(sc->sc_dev), 599 "is_10bit", &is_10bit); 600 601 const int bits = is_10bit ? 10 : 8; 602 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = bits; 603 604 if (is_bgr) { 605 /* someone requested BGR */ 606 ri->ri_rpos = bits * 0; 607 ri->ri_gpos = bits * 1; 608 ri->ri_bpos = bits * 2; 609 } else if (is_swapped) { 610 /* byte-swapped, must be 32 bpp */ 611 KASSERT(ri->ri_depth == 32 && bits == 8); 612 ri->ri_rpos = 8; 613 ri->ri_gpos = 16; 614 ri->ri_bpos = 24; 615 } else { 616 /* assume RGB */ 617 ri->ri_rpos = bits * 2; 618 ri->ri_gpos = bits * 1; 619 ri->ri_bpos = bits * 0; 620 } 621 break; 622 623 case 16: 624 case 15: 625 ri->ri_flg |= RI_ENABLE_ALPHA; 626 break; 627 628 case 8: 629 if (sc->sc_cmcb != NULL) 630 ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB; 631 break; 632 633 case 2: 634 ri->ri_flg |= RI_ENABLE_ALPHA; 635 break; 636 637 default: 638 break; 639 } 640 641 wantcols = genfb_calc_cols(sc, ri); 642 643 rasops_init(ri, 0, wantcols); 644 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE | 645 WSSCREEN_RESIZE; 646 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 647 sc->sc_width / ri->ri_font->fontwidth); 648 649 ri->ri_hw = scr; 650 #if GENFB_GLYPHCACHE > 0 651 sc->sc_putchar = ri->ri_ops.putchar; 652 ri->ri_ops.putchar = genfb_putchar; 653 #endif 654 #ifdef GENFB_DISABLE_TEXT 655 if (scr == &sc->sc_console_screen && !DISABLESPLASH) 656 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen); 657 #endif 658 } 659 660 /* Returns the width of the display in millimeters, or 0 if not known. */ 661 static int 662 genfb_calc_hsize(struct genfb_softc *sc) 663 { 664 device_t dev = sc->sc_dev; 665 prop_dictionary_t dict = device_properties(dev); 666 prop_data_t edid_data; 667 struct edid_info *edid; 668 const char *edid_ptr; 669 int hsize; 670 671 edid_data = prop_dictionary_get(dict, "EDID"); 672 if (edid_data == NULL || prop_data_size(edid_data) < 128) 673 return 0; 674 675 edid = kmem_alloc(sizeof(*edid), KM_SLEEP); 676 677 edid_ptr = prop_data_value(edid_data); 678 if (edid_parse(__UNCONST(edid_ptr), edid) == 0) 679 hsize = (int)edid->edid_max_hsize * 10; 680 else 681 hsize = 0; 682 683 kmem_free(edid, sizeof(*edid)); 684 685 return hsize; 686 } 687 688 /* Return the minimum number of character columns based on DPI */ 689 static int 690 genfb_calc_cols(struct genfb_softc *sc, struct rasops_info *ri) 691 { 692 const int hsize = genfb_calc_hsize(sc); 693 694 if (hsize != 0) { 695 ri->ri_flg |= RI_PREFER_WIDEFONT; 696 } 697 698 return MAX(RASOPS_DEFAULT_WIDTH, hsize / GENFB_CHAR_WIDTH_MM); 699 } 700 701 static int 702 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm) 703 { 704 u_char *r, *g, *b; 705 u_int index = cm->index; 706 u_int count = cm->count; 707 int i, error; 708 u_char rbuf[256], gbuf[256], bbuf[256]; 709 710 #ifdef GENFB_DEBUG 711 aprint_debug("putcmap: %d %d\n",index, count); 712 #endif 713 if (index >= 256 || count > 256 || index + count > 256) 714 return EINVAL; 715 716 error = copyin(cm->red, &rbuf[index], count); 717 if (error) 718 return error; 719 error = copyin(cm->green, &gbuf[index], count); 720 if (error) 721 return error; 722 error = copyin(cm->blue, &bbuf[index], count); 723 if (error) 724 return error; 725 726 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count); 727 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count); 728 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count); 729 730 r = &sc->sc_cmap_red[index]; 731 g = &sc->sc_cmap_green[index]; 732 b = &sc->sc_cmap_blue[index]; 733 734 for (i = 0; i < count; i++) { 735 genfb_putpalreg(sc, index, *r, *g, *b); 736 index++; 737 r++, g++, b++; 738 } 739 return 0; 740 } 741 742 static int 743 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm) 744 { 745 u_int index = cm->index; 746 u_int count = cm->count; 747 int error; 748 749 if (index >= 256 || count > 256 || index + count > 256) 750 return EINVAL; 751 752 error = copyout(&sc->sc_cmap_red[index], cm->red, count); 753 if (error) 754 return error; 755 error = copyout(&sc->sc_cmap_green[index], cm->green, count); 756 if (error) 757 return error; 758 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count); 759 if (error) 760 return error; 761 762 return 0; 763 } 764 765 void 766 genfb_restore_palette(struct genfb_softc *sc) 767 { 768 int i; 769 770 if (sc->sc_depth <= 8) { 771 for (i = 0; i < (1 << sc->sc_depth); i++) { 772 genfb_putpalreg(sc, i, sc->sc_cmap_red[i], 773 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]); 774 } 775 } 776 } 777 778 static void 779 genfb_init_palette(struct genfb_softc *sc) 780 { 781 int i, j, tmp; 782 783 if (sc->sc_depth == 8) { 784 /* generate an r3g3b2 colour map */ 785 for (i = 0; i < 256; i++) { 786 tmp = i & 0xe0; 787 /* 788 * replicate bits so 0xe0 maps to a red value of 0xff 789 * in order to make white look actually white 790 */ 791 tmp |= (tmp >> 3) | (tmp >> 6); 792 sc->sc_cmap_red[i] = tmp; 793 794 tmp = (i & 0x1c) << 3; 795 tmp |= (tmp >> 3) | (tmp >> 6); 796 sc->sc_cmap_green[i] = tmp; 797 798 tmp = (i & 0x03) << 6; 799 tmp |= tmp >> 2; 800 tmp |= tmp >> 4; 801 sc->sc_cmap_blue[i] = tmp; 802 803 genfb_putpalreg(sc, i, sc->sc_cmap_red[i], 804 sc->sc_cmap_green[i], 805 sc->sc_cmap_blue[i]); 806 } 807 } else { 808 /* steal rasops' ANSI cmap */ 809 j = 0; 810 for (i = 0; i < 256; i++) { 811 sc->sc_cmap_red[i] = rasops_cmap[j]; 812 sc->sc_cmap_green[i] = rasops_cmap[j + 1]; 813 sc->sc_cmap_blue[i] = rasops_cmap[j + 2]; 814 j += 3; 815 } 816 } 817 } 818 819 static int 820 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g, 821 uint8_t b) 822 { 823 824 if (sc->sc_cmcb) { 825 826 sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie, 827 idx, r, g, b); 828 } 829 return 0; 830 } 831 832 void 833 genfb_cnattach(void) 834 { 835 genfb_cnattach_called = 1; 836 } 837 838 void 839 genfb_disable(void) 840 { 841 genfb_enabled = 0; 842 } 843 844 int 845 genfb_is_console(void) 846 { 847 return genfb_cnattach_called; 848 } 849 850 int 851 genfb_is_enabled(void) 852 { 853 return genfb_enabled; 854 } 855 856 int 857 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp) 858 { 859 struct genfb_softc *sc = genfb_softc; 860 861 if (sc && sc->sc_ops.genfb_borrow) 862 return sc->sc_ops.genfb_borrow(sc, addr, hdlp); 863 return 0; 864 } 865 866 static void 867 genfb_brightness_up(device_t dev) 868 { 869 struct genfb_softc *sc = device_private(dev); 870 871 KASSERT(sc->sc_brightness != NULL && 872 sc->sc_brightness->gpc_upd_parameter != NULL); 873 874 (void)sc->sc_brightness->gpc_upd_parameter( 875 sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP); 876 } 877 878 static void 879 genfb_brightness_down(device_t dev) 880 { 881 struct genfb_softc *sc = device_private(dev); 882 883 KASSERT(sc->sc_brightness != NULL && 884 sc->sc_brightness->gpc_upd_parameter != NULL); 885 886 (void)sc->sc_brightness->gpc_upd_parameter( 887 sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP); 888 } 889 890 void 891 genfb_enable_polling(device_t dev) 892 { 893 struct genfb_softc *sc = device_private(dev); 894 895 if (sc->sc_console_screen.scr_vd) { 896 SCREEN_ENABLE_DRAWING(&sc->sc_console_screen); 897 vcons_hard_switch(&sc->sc_console_screen); 898 vcons_enable_polling(&sc->vd); 899 if (sc->sc_ops.genfb_enable_polling) 900 (*sc->sc_ops.genfb_enable_polling)(sc); 901 } 902 } 903 904 void 905 genfb_disable_polling(device_t dev) 906 { 907 struct genfb_softc *sc = device_private(dev); 908 909 if (sc->sc_console_screen.scr_vd) { 910 if (sc->sc_ops.genfb_disable_polling) 911 (*sc->sc_ops.genfb_disable_polling)(sc); 912 vcons_disable_polling(&sc->vd); 913 } 914 } 915 916 #if GENFB_GLYPHCACHE > 0 917 #define GLYPHCACHESIZE ((GENFB_GLYPHCACHE) * 1024 * 1024) 918 919 static inline int 920 attr2idx(long attr) 921 { 922 if ((attr & 0xf0f00ff8) != 0) 923 return -1; 924 925 return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0)); 926 } 927 928 static int 929 genfb_setup_glyphcache(struct genfb_softc *sc, long defattr) 930 { 931 struct rasops_info *ri = &sc->sc_console_screen.scr_ri, 932 *cri = &sc->sc_cache_ri; 933 gc_bucket *b; 934 int i, usedcells = 0, idx, j; 935 936 sc->sc_cache = kmem_alloc(GLYPHCACHESIZE, KM_SLEEP); 937 938 /* 939 * now we build a mutant rasops_info for the cache - same pixel type 940 * and such as the real fb, but only one character per line for 941 * simplicity and locality 942 */ 943 memcpy(cri, ri, sizeof(struct rasops_info)); 944 cri->ri_ops.putchar = sc->sc_putchar; 945 cri->ri_width = ri->ri_font->fontwidth; 946 cri->ri_stride = ri->ri_xscale; 947 cri->ri_bits = sc->sc_cache; 948 cri->ri_hwbits = NULL; 949 cri->ri_origbits = sc->sc_cache; 950 cri->ri_cols = 1; 951 cri->ri_rows = GLYPHCACHESIZE / 952 (cri->ri_stride * cri->ri_font->fontheight); 953 cri->ri_xorigin = 0; 954 cri->ri_yorigin = 0; 955 cri->ri_xscale = ri->ri_xscale; 956 cri->ri_yscale = ri->ri_font->fontheight * ri->ri_xscale; 957 958 printf("size %d %d %d\n", GLYPHCACHESIZE, ri->ri_width, ri->ri_stride); 959 printf("cells: %d\n", cri->ri_rows); 960 sc->sc_nbuckets = uimin(256, cri->ri_rows / 223); 961 sc->sc_buckets = kmem_alloc(sizeof(gc_bucket) * sc->sc_nbuckets, KM_SLEEP); 962 printf("buckets: %d\n", sc->sc_nbuckets); 963 for (i = 0; i < sc->sc_nbuckets; i++) { 964 b = &sc->sc_buckets[i]; 965 b->gb_firstcell = usedcells; 966 b->gb_numcells = uimin(223, cri->ri_rows - usedcells); 967 usedcells += 223; 968 b->gb_usedcells = 0; 969 b->gb_index = -1; 970 for (j = 0; j < 223; j++) b->gb_map[j] = -1; 971 } 972 973 /* initialize the attribute map... */ 974 for (i = 0; i < 256; i++) { 975 sc->sc_attrmap[i] = -1; 976 } 977 978 /* first bucket goes to default attr */ 979 idx = attr2idx(defattr); 980 printf("defattr %08lx idx %x\n", defattr, idx); 981 982 if (idx >= 0) { 983 sc->sc_attrmap[idx] = 0; 984 sc->sc_buckets[0].gb_index = idx; 985 } 986 987 return 0; 988 } 989 990 static void 991 genfb_putchar(void *cookie, int row, int col, u_int c, long attr) 992 { 993 struct rasops_info *ri = cookie; 994 struct vcons_screen *scr = ri->ri_hw; 995 struct genfb_softc *sc = scr->scr_cookie; 996 uint8_t *src, *dst; 997 gc_bucket *b; 998 int i, idx, bi, cell; 999 1000 attr &= ~WSATTR_USERMASK; 1001 1002 idx = attr2idx(attr); 1003 if (c < 33 || c > 255 || idx < 0) goto nope; 1004 1005 /* look for a bucket with the right attribute */ 1006 bi = sc->sc_attrmap[idx]; 1007 if (bi == -1) { 1008 /* nope, see if there's an empty one left */ 1009 bi = 1; 1010 while ((bi < sc->sc_nbuckets) && 1011 (sc->sc_buckets[bi].gb_index != -1)) { 1012 bi++; 1013 } 1014 if (bi < sc->sc_nbuckets) { 1015 /* found one -> grab it */ 1016 sc->sc_attrmap[idx] = bi; 1017 b = &sc->sc_buckets[bi]; 1018 b->gb_index = idx; 1019 b->gb_usedcells = 0; 1020 /* make sure this doesn't get evicted right away */ 1021 b->gb_lastread = time_uptime; 1022 } else { 1023 /* 1024 * still nothing 1025 * steal the least recently read bucket 1026 */ 1027 time_t moo = time_uptime; 1028 int oldest = 1; 1029 1030 for (i = 1; i < sc->sc_nbuckets; i++) { 1031 if (sc->sc_buckets[i].gb_lastread < moo) { 1032 oldest = i; 1033 moo = sc->sc_buckets[i].gb_lastread; 1034 } 1035 } 1036 1037 /* if we end up here all buckets must be in use */ 1038 b = &sc->sc_buckets[oldest]; 1039 sc->sc_attrmap[b->gb_index] = -1; 1040 b->gb_index = idx; 1041 b->gb_usedcells = 0; 1042 sc->sc_attrmap[idx] = oldest; 1043 /* now scrub it */ 1044 for (i = 0; i < 223; i++) 1045 b->gb_map[i] = -1; 1046 /* and set the time stamp */ 1047 b->gb_lastread = time_uptime; 1048 } 1049 } else { 1050 /* found one */ 1051 b = &sc->sc_buckets[bi]; 1052 } 1053 1054 /* see if there's room in the bucket */ 1055 if (b->gb_usedcells >= b->gb_numcells) goto nope; 1056 1057 cell = b->gb_map[c - 33]; 1058 if (cell == -1) { 1059 if (b->gb_usedcells >= b->gb_numcells) 1060 goto nope; 1061 cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1; 1062 b->gb_map[c - 33] = cell; 1063 cell += b->gb_firstcell; 1064 sc->sc_putchar(&sc->sc_cache_ri, cell, 0, c, attr); 1065 1066 } else 1067 cell += b->gb_firstcell; 1068 1069 src = sc->sc_cache + cell * sc->sc_cache_ri.ri_yscale; 1070 dst = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale; 1071 for (i = 0; i < ri->ri_font->fontheight; i++) { 1072 memcpy(dst, src, ri->ri_xscale); 1073 src += ri->ri_xscale; 1074 dst += ri->ri_stride; 1075 } 1076 b->gb_lastread = time_uptime; 1077 return; 1078 nope: 1079 sc->sc_putchar(cookie, row, col, c, attr); 1080 } 1081 1082 #endif 1083