1 /* $NetBSD: genfb.c,v 1.95 2026/11/14 14:49:49 nat 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.95 2026/11/14 14:49:49 nat 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 struct genfb_private { 79 struct genfb_ops sc_ops; 80 struct vcons_screen sc_console_screen; 81 struct wsscreen_descr sc_defaultscreen_descr; 82 const struct wsscreen_descr *sc_screens[1]; 83 struct wsscreen_list sc_screenlist; 84 struct genfb_colormap_callback *sc_cmcb; 85 struct genfb_parameter_callback *sc_backlight; 86 struct genfb_parameter_callback *sc_brightness; 87 struct genfb_mode_callback *sc_modecb; 88 uint32_t *sc_devcmap; 89 int sc_backlight_level, sc_backlight_on; 90 void *sc_shadowfb; 91 bool sc_enable_shadowfb; 92 int sc_mode; 93 u_char sc_cmap_red[256]; 94 u_char sc_cmap_green[256]; 95 u_char sc_cmap_blue[256]; 96 u_char sc_cmap_saved_red[256]; 97 u_char sc_cmap_saved_green[256]; 98 u_char sc_cmap_saved_blue[256]; 99 bool sc_want_clear; 100 #ifdef SPLASHSCREEN 101 struct splash_info sc_splash; 102 #endif 103 struct wsdisplay_accessops sc_accessops; 104 #if GENFB_GLYPHCACHE > 0 105 /* 106 * The generic glyphcache code makes a bunch of assumptions that are 107 * true for most graphics hardware with a directly supported blitter. 108 * For example it assume that 109 * - VRAM access from the host is expensive 110 * - copying data around in VRAM is cheap and can happen in parallel 111 * to the host CPU 112 * -> therefore we draw glyphs normally if we have to, so the ( assumed 113 * to be hardware assisted ) driver supplied putchar() method doesn't 114 * need to be glyphcache aware, then copy them away for later use 115 * for genfb things are a bit different. On most hardware: 116 * - VRAM access from the host is still expensive 117 * - copying data around in VRAM is also expensive since we don't have 118 * a blitter and VRAM is mapped uncached 119 * - VRAM reads are usually slower than writes ( write combining and 120 * such help writes but not reads, and VRAM might be behind an 121 * asymmetric bus like AGP ) and must be avoided, both are much 122 * slower than main memory 123 * -> therefore we cache glyphs in main memory, no reason to map it 124 * uncached, we draw into the cache first and then copy the glyph 125 * into video memory to avoid framebuffer reads and to allow more 126 * efficient write accesses than putchar() would offer 127 * Because of this we can't use the generic code but we can recycle a 128 * few data structures. 129 */ 130 uint8_t *sc_cache; 131 struct rasops_info sc_cache_ri; 132 void (*sc_putchar)(void *, int, int, u_int, long); 133 int sc_cache_cells; 134 int sc_nbuckets; /* buckets allocated */ 135 gc_bucket *sc_buckets; /* we allocate as many as we can get into ram */ 136 int sc_attrmap[256]; /* mapping a colour attribute to a bucket */ 137 #endif 138 }; 139 140 static int genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *); 141 static paddr_t genfb_mmap(void *, void *, off_t, int); 142 static void genfb_pollc(void *, int); 143 144 static void genfb_init_screen(void *, struct vcons_screen *, int, long *); 145 static int genfb_calc_hsize(struct genfb_softc *); 146 static int genfb_calc_cols(struct genfb_softc *, struct rasops_info *); 147 148 static int genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *); 149 static int genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *); 150 static int genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t, 151 uint8_t, uint8_t); 152 static void genfb_init_palette(struct genfb_softc *); 153 154 static void genfb_brightness_up(device_t); 155 static void genfb_brightness_down(device_t); 156 157 #if GENFB_GLYPHCACHE > 0 158 static int genfb_setup_glyphcache(struct genfb_softc *, long); 159 static void genfb_putchar(void *, int, int, u_int, long); 160 #endif 161 162 extern const u_char rasops_cmap[768]; 163 164 static int genfb_cnattach_called = 0; 165 static int genfb_enabled = 1; 166 167 static struct genfb_softc *genfb_softc = NULL; 168 169 void 170 genfb_init(struct genfb_softc *sc) 171 { 172 struct genfb_private *scp; 173 prop_dictionary_t dict; 174 uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb, br_cb, devcmap, fbaddr; 175 uint64_t fboffset; 176 bool console; 177 178 scp = sc->sc_private = kmem_zalloc(sizeof(*sc->sc_private), KM_SLEEP); 179 180 dict = device_properties(sc->sc_dev); 181 #ifdef GENFB_DEBUG 182 printf("%s", prop_dictionary_externalize(dict)); 183 #endif 184 prop_dictionary_get_bool(dict, "is_console", &console); 185 186 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) { 187 GPRINTF("no width property\n"); 188 goto bad; 189 } 190 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) { 191 GPRINTF("no height property\n"); 192 goto bad; 193 } 194 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) { 195 GPRINTF("no depth property\n"); 196 goto bad; 197 } 198 199 if (!prop_dictionary_get_uint64(dict, "address", &fboffset)) { 200 GPRINTF("no address property\n"); 201 goto bad; 202 } 203 204 sc->sc_fboffset = (bus_addr_t)fboffset; 205 206 sc->sc_fbaddr = NULL; 207 if (prop_dictionary_get_uint64(dict, "virtual_address", &fbaddr)) { 208 sc->sc_fbaddr = (void *)(uintptr_t)fbaddr; 209 } 210 211 scp->sc_shadowfb = NULL; 212 if (!prop_dictionary_get_bool(dict, "enable_shadowfb", 213 &scp->sc_enable_shadowfb)) 214 #ifdef GENFB_SHADOWFB 215 scp->sc_enable_shadowfb = true; 216 #else 217 scp->sc_enable_shadowfb = false; 218 #endif 219 220 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) 221 sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3; 222 223 /* 224 * deal with a bug in the Raptor firmware which always sets 225 * stride = width even when depth != 8 226 */ 227 if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3)) 228 sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3); 229 230 sc->sc_fbsize = sc->sc_height * sc->sc_stride; 231 232 /* optional device colour map */ 233 scp->sc_devcmap = NULL; 234 if (prop_dictionary_get_uint64(dict, "devcmap", &devcmap)) { 235 if (devcmap != 0) 236 scp->sc_devcmap = (uint32_t *)(uintptr_t)devcmap; 237 } 238 239 /* optional colour map callback */ 240 scp->sc_cmcb = NULL; 241 if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) { 242 if (cmap_cb != 0) 243 scp->sc_cmcb = (void *)(vaddr_t)cmap_cb; 244 } 245 246 /* optional pmf callback */ 247 sc->sc_pmfcb = NULL; 248 if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) { 249 if (pmf_cb != 0) 250 sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb; 251 } 252 253 /* optional mode callback */ 254 scp->sc_modecb = NULL; 255 if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) { 256 if (mode_cb != 0) 257 scp->sc_modecb = (void *)(vaddr_t)mode_cb; 258 } 259 260 /* optional backlight control callback */ 261 scp->sc_backlight = NULL; 262 if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) { 263 if (bl_cb != 0) { 264 scp->sc_backlight = (void *)(vaddr_t)bl_cb; 265 aprint_naive_dev(sc->sc_dev, 266 "enabling backlight control\n"); 267 } 268 } 269 270 /* optional brightness control callback */ 271 scp->sc_brightness = NULL; 272 if (prop_dictionary_get_uint64(dict, "brightness_callback", &br_cb)) { 273 if (br_cb != 0) { 274 scp->sc_brightness = (void *)(vaddr_t)br_cb; 275 aprint_naive_dev(sc->sc_dev, 276 "enabling brightness control\n"); 277 if (console && 278 scp->sc_brightness->gpc_upd_parameter != NULL) { 279 pmf_event_register(sc->sc_dev, 280 PMFE_DISPLAY_BRIGHTNESS_UP, 281 genfb_brightness_up, TRUE); 282 pmf_event_register(sc->sc_dev, 283 PMFE_DISPLAY_BRIGHTNESS_DOWN, 284 genfb_brightness_down, TRUE); 285 } 286 } 287 } 288 289 return; 290 291 bad: 292 kmem_free(sc->sc_private, sizeof(*sc->sc_private)); 293 sc->sc_private = NULL; 294 } 295 296 int 297 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops) 298 { 299 struct genfb_private *scp = sc->sc_private; 300 struct wsemuldisplaydev_attach_args aa; 301 prop_dictionary_t dict; 302 struct rasops_info *ri; 303 paddr_t fb_phys; 304 uint16_t crow; 305 long defattr; 306 bool console; 307 #ifdef SPLASHSCREEN 308 int i, j; 309 int error = ENXIO; 310 #endif 311 312 KASSERTMSG(scp != NULL, "missing genfb_init"); 313 314 dict = device_properties(sc->sc_dev); 315 prop_dictionary_get_bool(dict, "is_console", &console); 316 317 if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false) 318 crow = 0; 319 if (prop_dictionary_get_bool(dict, "clear-screen", &scp->sc_want_clear) 320 == false) 321 scp->sc_want_clear = true; 322 323 fb_phys = (paddr_t)sc->sc_fboffset; 324 if (fb_phys == 0) { 325 KASSERT(sc->sc_fbaddr != NULL); 326 (void)pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_fbaddr, 327 &fb_phys); 328 } 329 330 aprint_verbose_dev(sc->sc_dev, 331 "framebuffer at %p, size %dx%d, depth %d, stride %d\n", 332 fb_phys ? (void *)(intptr_t)fb_phys : sc->sc_fbaddr, 333 sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride); 334 335 scp->sc_defaultscreen_descr = (struct wsscreen_descr){ 336 "default", 337 0, 0, 338 NULL, 339 8, 16, 340 WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE | 341 WSSCREEN_RESIZE, 342 NULL 343 }; 344 scp->sc_screens[0] = &scp->sc_defaultscreen_descr; 345 scp->sc_screenlist = (struct wsscreen_list){1, scp->sc_screens}; 346 memcpy(&scp->sc_ops, ops, sizeof(struct genfb_ops)); 347 scp->sc_mode = WSDISPLAYIO_MODE_EMUL; 348 if (scp->sc_modecb != NULL) 349 scp->sc_modecb->gmc_setmode(sc, scp->sc_mode); 350 351 scp->sc_accessops.ioctl = genfb_ioctl; 352 scp->sc_accessops.mmap = genfb_mmap; 353 scp->sc_accessops.pollc = genfb_pollc; 354 355 if (scp->sc_enable_shadowfb) { 356 scp->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP); 357 if (scp->sc_want_clear == false) { 358 memcpy(scp->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize); 359 } 360 aprint_verbose_dev(sc->sc_dev, 361 "shadow framebuffer enabled, size %zu KB\n", 362 sc->sc_fbsize >> 10); 363 } 364 365 vcons_init(&sc->vd, sc, &scp->sc_defaultscreen_descr, 366 &scp->sc_accessops); 367 sc->vd.init_screen = genfb_init_screen; 368 369 /* Do not print anything between this point and the screen 370 * clear operation below. Otherwise it will be lost. */ 371 372 ri = &scp->sc_console_screen.scr_ri; 373 374 vcons_init_screen(&sc->vd, &scp->sc_console_screen, 1, 375 &defattr); 376 scp->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 377 378 #if GENFB_GLYPHCACHE > 0 379 genfb_setup_glyphcache(sc, defattr); 380 #endif 381 382 #ifdef SPLASHSCREEN 383 /* 384 * If system isn't going to go multiuser, or user has requested to see 385 * boot text, don't render splash screen immediately 386 */ 387 if (DISABLESPLASH) 388 #endif 389 vcons_redraw_screen(&scp->sc_console_screen); 390 391 scp->sc_defaultscreen_descr.textops = &ri->ri_ops; 392 scp->sc_defaultscreen_descr.capabilities = ri->ri_caps; 393 scp->sc_defaultscreen_descr.nrows = ri->ri_rows; 394 scp->sc_defaultscreen_descr.ncols = ri->ri_cols; 395 396 if (crow >= ri->ri_rows) { 397 crow = 0; 398 scp->sc_want_clear = 1; 399 } 400 401 if (console) 402 wsdisplay_cnattach(&scp->sc_defaultscreen_descr, ri, 0, crow, 403 defattr); 404 405 /* Clear the whole screen to bring it to a known state. */ 406 if (scp->sc_want_clear) 407 (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr); 408 409 #ifdef SPLASHSCREEN 410 j = 0; 411 for (i = 0; i < uimin(1 << sc->sc_depth, 256); i++) { 412 if (i >= SPLASH_CMAP_OFFSET && 413 i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) { 414 splash_get_cmap(i, 415 &scp->sc_cmap_red[i], 416 &scp->sc_cmap_green[i], 417 &scp->sc_cmap_blue[i]); 418 } else { 419 scp->sc_cmap_red[i] = rasops_cmap[j]; 420 scp->sc_cmap_green[i] = rasops_cmap[j + 1]; 421 scp->sc_cmap_blue[i] = rasops_cmap[j + 2]; 422 } 423 j += 3; 424 } 425 genfb_restore_palette(sc); 426 427 scp->sc_splash.si_depth = sc->sc_depth; 428 scp->sc_splash.si_bits = scp->sc_console_screen.scr_ri.ri_origbits; 429 scp->sc_splash.si_hwbits = sc->sc_fbaddr; 430 scp->sc_splash.si_width = sc->sc_width; 431 scp->sc_splash.si_height = sc->sc_height; 432 scp->sc_splash.si_stride = sc->sc_stride; 433 scp->sc_splash.si_fillrect = NULL; 434 if (!DISABLESPLASH) { 435 error = splash_render(&scp->sc_splash, 436 SPLASH_F_CENTER|SPLASH_F_FILL); 437 if (error) { 438 SCREEN_ENABLE_DRAWING(&scp->sc_console_screen); 439 genfb_init_palette(sc); 440 vcons_replay_msgbuf(&scp->sc_console_screen); 441 } 442 } 443 #else 444 genfb_init_palette(sc); 445 if (console && (boothowto & (AB_SILENT|AB_QUIET)) == 0) 446 vcons_replay_msgbuf(&scp->sc_console_screen); 447 #endif 448 449 if (genfb_softc == NULL) 450 genfb_softc = sc; 451 452 aa.console = console; 453 aa.scrdata = &scp->sc_screenlist; 454 aa.accessops = &scp->sc_accessops; 455 aa.accesscookie = &sc->vd; 456 457 #ifdef GENFB_DISABLE_TEXT 458 if (!DISABLESPLASH && error == 0) 459 SCREEN_DISABLE_DRAWING(&scp->sc_console_screen); 460 #endif 461 462 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, 463 CFARGS(.iattr = "wsemuldisplaydev")); 464 465 return 0; 466 } 467 468 static int 469 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 470 struct lwp *l) 471 { 472 struct vcons_data *vd = v; 473 struct genfb_softc *sc = vd->cookie; 474 struct genfb_private *scp = sc->sc_private; 475 struct wsdisplay_fbinfo *wdf; 476 struct vcons_screen *ms = vd->active; 477 struct wsdisplay_param *param; 478 int new_mode, error, val, ret; 479 480 switch (cmd) { 481 case WSDISPLAYIO_GINFO: 482 if (ms == NULL) 483 return ENODEV; 484 wdf = (void *)data; 485 wdf->height = ms->scr_ri.ri_height; 486 wdf->width = ms->scr_ri.ri_width; 487 wdf->depth = ms->scr_ri.ri_depth; 488 wdf->cmsize = 256; 489 return 0; 490 491 case WSDISPLAYIO_GETCMAP: 492 return genfb_getcmap(sc, (struct wsdisplay_cmap *)data); 493 494 case WSDISPLAYIO_PUTCMAP: 495 return genfb_putcmap(sc, (struct wsdisplay_cmap *)data); 496 497 case WSDISPLAYIO_LINEBYTES: 498 *(u_int *)data = sc->sc_stride; 499 return 0; 500 501 case WSDISPLAYIO_SMODE: 502 new_mode = *(int *)data; 503 504 /* notify the bus backend */ 505 error = 0; 506 if (scp->sc_ops.genfb_ioctl) { 507 error = scp->sc_ops.genfb_ioctl(sc, vs, 508 cmd, data, flag, l); 509 } 510 if (error && error != EPASSTHROUGH) 511 return error; 512 513 if (new_mode != scp->sc_mode) { 514 if (scp->sc_mode == WSDISPLAYIO_MODE_EMUL) { 515 memcpy(scp->sc_cmap_saved_red, 516 scp->sc_cmap_red, 256); 517 memcpy(scp->sc_cmap_saved_green, 518 scp->sc_cmap_green, 256); 519 memcpy(scp->sc_cmap_saved_blue, 520 scp->sc_cmap_blue, 256); 521 } 522 scp->sc_mode = new_mode; 523 if (scp->sc_modecb != NULL) { 524 scp->sc_modecb->gmc_setmode(sc, scp->sc_mode); 525 } 526 if (new_mode == WSDISPLAYIO_MODE_EMUL) { 527 memcpy(scp->sc_cmap_red, 528 scp->sc_cmap_saved_red, 256); 529 memcpy(scp->sc_cmap_green, 530 scp->sc_cmap_saved_green, 256); 531 memcpy(scp->sc_cmap_blue, 532 scp->sc_cmap_saved_blue, 256); 533 genfb_restore_palette(sc); 534 vcons_redraw_screen(ms); 535 } 536 } 537 return 0; 538 539 case WSDISPLAYIO_SSPLASH: 540 #if defined(SPLASHSCREEN) 541 if (*(int *)data == 1) { 542 SCREEN_DISABLE_DRAWING(&scp->sc_console_screen); 543 splash_render(&scp->sc_splash, 544 SPLASH_F_CENTER|SPLASH_F_FILL); 545 } else { 546 SCREEN_ENABLE_DRAWING(&scp->sc_console_screen); 547 genfb_init_palette(sc); 548 } 549 vcons_redraw_screen(ms); 550 return 0; 551 #else 552 return ENODEV; 553 #endif 554 case WSDISPLAYIO_GETPARAM: 555 param = (struct wsdisplay_param *)data; 556 switch (param->param) { 557 case WSDISPLAYIO_PARAM_BRIGHTNESS: 558 if (scp->sc_brightness == NULL) 559 return EPASSTHROUGH; 560 param->min = 0; 561 param->max = 255; 562 return scp->sc_brightness->gpc_get_parameter( 563 scp->sc_brightness->gpc_cookie, 564 ¶m->curval); 565 case WSDISPLAYIO_PARAM_BACKLIGHT: 566 if (scp->sc_backlight == NULL) 567 return EPASSTHROUGH; 568 param->min = 0; 569 param->max = 1; 570 return scp->sc_backlight->gpc_get_parameter( 571 scp->sc_backlight->gpc_cookie, 572 ¶m->curval); 573 } 574 return EPASSTHROUGH; 575 576 case WSDISPLAYIO_SETPARAM: 577 param = (struct wsdisplay_param *)data; 578 switch (param->param) { 579 case WSDISPLAYIO_PARAM_BRIGHTNESS: 580 if (scp->sc_brightness == NULL) 581 return EPASSTHROUGH; 582 val = param->curval; 583 if (val < 0) 584 val = 0; 585 if (val > 255) 586 val = 255; 587 return scp->sc_brightness->gpc_set_parameter( 588 scp->sc_brightness->gpc_cookie, val); 589 case WSDISPLAYIO_PARAM_BACKLIGHT: 590 if (scp->sc_backlight == NULL) 591 return EPASSTHROUGH; 592 val = param->curval; 593 if (val < 0) 594 val = 0; 595 if (val > 1) 596 val = 1; 597 return scp->sc_backlight->gpc_set_parameter( 598 scp->sc_backlight->gpc_cookie, val); 599 } 600 return EPASSTHROUGH; 601 } 602 ret = EPASSTHROUGH; 603 if (scp->sc_ops.genfb_ioctl) 604 ret = scp->sc_ops.genfb_ioctl(sc, vs, cmd, data, flag, l); 605 if (ret != EPASSTHROUGH) 606 return ret; 607 /* 608 * XXX 609 * handle these only if there either is no ioctl() handler or it didn't 610 * know how to deal with them. This allows bus frontends to override 611 * them but still provides fallback implementations 612 */ 613 switch (cmd) { 614 case WSDISPLAYIO_GET_EDID: 615 { 616 struct wsdisplayio_edid_info *d = data; 617 return wsdisplayio_get_edid(sc->sc_dev, d); 618 } 619 case WSDISPLAYIO_GET_FBINFO: 620 { 621 struct wsdisplayio_fbinfo *fbi = data; 622 return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi); 623 } 624 } 625 return EPASSTHROUGH; 626 } 627 628 static paddr_t 629 genfb_mmap(void *v, void *vs, off_t offset, int prot) 630 { 631 struct vcons_data *vd = v; 632 struct genfb_softc *sc = vd->cookie; 633 struct genfb_private *scp = sc->sc_private; 634 635 if (scp->sc_ops.genfb_mmap) 636 return scp->sc_ops.genfb_mmap(sc, vs, offset, prot); 637 638 return -1; 639 } 640 641 static void 642 genfb_pollc(void *v, int on) 643 { 644 struct vcons_data *vd = v; 645 struct genfb_softc *sc = vd->cookie; 646 647 if (sc == NULL) 648 return; 649 650 if (on) 651 genfb_enable_polling(sc->sc_dev); 652 else 653 genfb_disable_polling(sc->sc_dev); 654 } 655 656 static void 657 genfb_init_screen(void *cookie, struct vcons_screen *scr, 658 int existing, long *defattr) 659 { 660 struct genfb_softc *sc = cookie; 661 struct genfb_private *scp = sc->sc_private; 662 struct rasops_info *ri = &scr->scr_ri; 663 int wantcols; 664 bool is_bgr, is_swapped, is_10bit; 665 666 ri->ri_depth = sc->sc_depth; 667 ri->ri_width = sc->sc_width; 668 ri->ri_height = sc->sc_height; 669 ri->ri_stride = sc->sc_stride; 670 ri->ri_flg = RI_CENTER; 671 if (scp->sc_want_clear) 672 ri->ri_flg |= RI_FULLCLEAR; 673 674 scr->scr_flags |= VCONS_LOADFONT; 675 676 if (scp->sc_shadowfb != NULL) { 677 ri->ri_hwbits = (char *)sc->sc_fbaddr; 678 ri->ri_bits = (char *)scp->sc_shadowfb; 679 } else { 680 ri->ri_bits = (char *)sc->sc_fbaddr; 681 scr->scr_flags |= VCONS_DONT_READ; 682 } 683 684 if (existing && scp->sc_want_clear) 685 ri->ri_flg |= RI_CLEAR; 686 687 switch (ri->ri_depth) { 688 case 32: 689 case 24: 690 ri->ri_flg |= RI_ENABLE_ALPHA; 691 692 is_bgr = false; 693 prop_dictionary_get_bool(device_properties(sc->sc_dev), 694 "is_bgr", &is_bgr); 695 696 is_swapped = false; 697 prop_dictionary_get_bool(device_properties(sc->sc_dev), 698 "is_swapped", &is_swapped); 699 700 is_10bit = false; 701 prop_dictionary_get_bool(device_properties(sc->sc_dev), 702 "is_10bit", &is_10bit); 703 704 const int bits = is_10bit ? 10 : 8; 705 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = bits; 706 707 if (is_bgr) { 708 /* someone requested BGR */ 709 ri->ri_rpos = bits * 0; 710 ri->ri_gpos = bits * 1; 711 ri->ri_bpos = bits * 2; 712 } else if (is_swapped) { 713 /* byte-swapped, must be 32 bpp */ 714 KASSERT(ri->ri_depth == 32); 715 KASSERT(bits == 8); 716 ri->ri_rpos = 8; 717 ri->ri_gpos = 16; 718 ri->ri_bpos = 24; 719 } else { 720 /* assume RGB */ 721 ri->ri_rpos = bits * 2; 722 ri->ri_gpos = bits * 1; 723 ri->ri_bpos = bits * 0; 724 } 725 break; 726 727 case 16: 728 case 15: 729 ri->ri_flg |= RI_ENABLE_ALPHA; 730 break; 731 732 case 8: 733 if (scp->sc_cmcb != NULL) 734 ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB; 735 break; 736 737 case 2: 738 ri->ri_flg |= RI_ENABLE_ALPHA; 739 break; 740 741 default: 742 break; 743 } 744 745 wantcols = genfb_calc_cols(sc, ri); 746 747 rasops_init(ri, 0, wantcols); 748 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE | 749 WSSCREEN_RESIZE; 750 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 751 sc->sc_width / ri->ri_font->fontwidth); 752 753 if (scp->sc_devcmap != NULL) { 754 memcpy(ri->ri_devcmap, scp->sc_devcmap, sizeof(ri->ri_devcmap)); 755 } 756 757 ri->ri_hw = scr; 758 #if GENFB_GLYPHCACHE > 0 759 scp->sc_putchar = ri->ri_ops.putchar; 760 ri->ri_ops.putchar = genfb_putchar; 761 #endif 762 #ifdef GENFB_DISABLE_TEXT 763 if (scr == &scp->sc_console_screen && !DISABLESPLASH) 764 SCREEN_DISABLE_DRAWING(&scp->sc_console_screen); 765 #endif 766 } 767 768 /* Returns the width of the display in millimeters, or 0 if not known. */ 769 static int 770 genfb_calc_hsize(struct genfb_softc *sc) 771 { 772 device_t dev = sc->sc_dev; 773 prop_dictionary_t dict = device_properties(dev); 774 prop_data_t edid_data; 775 struct edid_info *edid; 776 const char *edid_ptr; 777 int hsize; 778 779 edid_data = prop_dictionary_get(dict, "EDID"); 780 if (edid_data == NULL || prop_data_size(edid_data) < 128) 781 return 0; 782 783 edid = kmem_alloc(sizeof(*edid), KM_SLEEP); 784 785 edid_ptr = prop_data_value(edid_data); 786 if (edid_parse(__UNCONST(edid_ptr), edid) == 0) 787 hsize = (int)edid->edid_max_hsize * 10; 788 else 789 hsize = 0; 790 791 kmem_free(edid, sizeof(*edid)); 792 793 return hsize; 794 } 795 796 /* Return the minimum number of character columns based on DPI */ 797 static int 798 genfb_calc_cols(struct genfb_softc *sc, struct rasops_info *ri) 799 { 800 const int hsize = genfb_calc_hsize(sc); 801 802 if (hsize != 0) { 803 ri->ri_flg |= RI_PREFER_WIDEFONT; 804 } 805 806 return MAX(RASOPS_DEFAULT_WIDTH, hsize / GENFB_CHAR_WIDTH_MM); 807 } 808 809 static int 810 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm) 811 { 812 struct genfb_private *scp = sc->sc_private; 813 u_char *r, *g, *b; 814 u_int index = cm->index; 815 u_int count = cm->count; 816 int i, error; 817 u_char rbuf[256], gbuf[256], bbuf[256]; 818 819 #ifdef GENFB_DEBUG 820 aprint_debug("putcmap: %d %d\n",index, count); 821 #endif 822 if (index >= 256 || count > 256 || index + count > 256) 823 return EINVAL; 824 825 error = copyin(cm->red, &rbuf[index], count); 826 if (error) 827 return error; 828 error = copyin(cm->green, &gbuf[index], count); 829 if (error) 830 return error; 831 error = copyin(cm->blue, &bbuf[index], count); 832 if (error) 833 return error; 834 835 memcpy(&scp->sc_cmap_red[index], &rbuf[index], count); 836 memcpy(&scp->sc_cmap_green[index], &gbuf[index], count); 837 memcpy(&scp->sc_cmap_blue[index], &bbuf[index], count); 838 839 r = &scp->sc_cmap_red[index]; 840 g = &scp->sc_cmap_green[index]; 841 b = &scp->sc_cmap_blue[index]; 842 843 for (i = 0; i < count; i++) { 844 genfb_putpalreg(sc, index, *r, *g, *b); 845 index++; 846 r++, g++, b++; 847 } 848 return 0; 849 } 850 851 static int 852 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm) 853 { 854 struct genfb_private *scp = sc->sc_private; 855 u_int index = cm->index; 856 u_int count = cm->count; 857 int error; 858 859 if (index >= 256 || count > 256 || index + count > 256) 860 return EINVAL; 861 862 error = copyout(&scp->sc_cmap_red[index], cm->red, count); 863 if (error) 864 return error; 865 error = copyout(&scp->sc_cmap_green[index], cm->green, count); 866 if (error) 867 return error; 868 error = copyout(&scp->sc_cmap_blue[index], cm->blue, count); 869 if (error) 870 return error; 871 872 return 0; 873 } 874 875 void 876 genfb_restore_palette(struct genfb_softc *sc) 877 { 878 struct genfb_private *scp = sc->sc_private; 879 int i; 880 881 if (sc->sc_depth <= 8) { 882 for (i = 0; i < (1 << sc->sc_depth); i++) { 883 genfb_putpalreg(sc, i, scp->sc_cmap_red[i], 884 scp->sc_cmap_green[i], scp->sc_cmap_blue[i]); 885 } 886 } 887 } 888 889 static void 890 genfb_init_palette(struct genfb_softc *sc) 891 { 892 struct genfb_private *scp = sc->sc_private; 893 int i, j, tmp; 894 895 if (sc->sc_depth == 8) { 896 /* generate an r3g3b2 colour map */ 897 for (i = 0; i < 256; i++) { 898 tmp = i & 0xe0; 899 /* 900 * replicate bits so 0xe0 maps to a red value of 0xff 901 * in order to make white look actually white 902 */ 903 tmp |= (tmp >> 3) | (tmp >> 6); 904 scp->sc_cmap_red[i] = tmp; 905 906 tmp = (i & 0x1c) << 3; 907 tmp |= (tmp >> 3) | (tmp >> 6); 908 scp->sc_cmap_green[i] = tmp; 909 910 tmp = (i & 0x03) << 6; 911 tmp |= tmp >> 2; 912 tmp |= tmp >> 4; 913 scp->sc_cmap_blue[i] = tmp; 914 915 genfb_putpalreg(sc, i, scp->sc_cmap_red[i], 916 scp->sc_cmap_green[i], 917 scp->sc_cmap_blue[i]); 918 } 919 } else { 920 /* steal rasops' ANSI cmap */ 921 j = 0; 922 for (i = 0; i < 256; i++) { 923 scp->sc_cmap_red[i] = rasops_cmap[j]; 924 scp->sc_cmap_green[i] = rasops_cmap[j + 1]; 925 scp->sc_cmap_blue[i] = rasops_cmap[j + 2]; 926 j += 3; 927 } 928 } 929 } 930 931 static int 932 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g, 933 uint8_t b) 934 { 935 struct genfb_private *scp = sc->sc_private; 936 937 if (scp->sc_cmcb) { 938 scp->sc_cmcb->gcc_set_mapreg(scp->sc_cmcb->gcc_cookie, 939 idx, r, g, b); 940 } 941 return 0; 942 } 943 944 void 945 genfb_cnattach(void) 946 { 947 948 genfb_cnattach_called = 1; 949 } 950 951 int 952 genfb_cndetach(void) 953 { 954 955 if (genfb_cnattach_called) { 956 genfb_cnattach_called = 0; 957 return 1; 958 } 959 return 0; 960 } 961 962 void 963 genfb_disable(void) 964 { 965 966 genfb_enabled = 0; 967 } 968 969 int 970 genfb_is_console(void) 971 { 972 973 return genfb_cnattach_called; 974 } 975 976 int 977 genfb_is_enabled(void) 978 { 979 980 return genfb_enabled; 981 } 982 983 int 984 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp) 985 { 986 struct genfb_softc *sc = genfb_softc; 987 988 if (sc && sc->sc_private && sc->sc_private->sc_ops.genfb_borrow) 989 return sc->sc_private->sc_ops.genfb_borrow(sc, addr, hdlp); 990 return 0; 991 } 992 993 static void 994 genfb_brightness_up(device_t dev) 995 { 996 struct genfb_softc *sc = device_private(dev); 997 struct genfb_private *scp = sc->sc_private; 998 999 KASSERT(scp->sc_brightness != NULL); 1000 KASSERT(scp->sc_brightness->gpc_upd_parameter != NULL); 1001 1002 (void)scp->sc_brightness->gpc_upd_parameter( 1003 scp->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP); 1004 } 1005 1006 static void 1007 genfb_brightness_down(device_t dev) 1008 { 1009 struct genfb_softc *sc = device_private(dev); 1010 struct genfb_private *scp = sc->sc_private; 1011 1012 KASSERT(scp->sc_brightness != NULL); 1013 KASSERT(scp->sc_brightness->gpc_upd_parameter != NULL); 1014 1015 (void)scp->sc_brightness->gpc_upd_parameter( 1016 scp->sc_brightness->gpc_cookie, -GENFB_BRIGHTNESS_STEP); 1017 } 1018 1019 void 1020 genfb_enable_polling(device_t dev) 1021 { 1022 struct genfb_softc *sc = device_private(dev); 1023 struct genfb_private *scp = sc->sc_private; 1024 1025 if (scp == NULL) 1026 return; 1027 1028 if (scp->sc_console_screen.scr_vd) { 1029 SCREEN_ENABLE_DRAWING(&scp->sc_console_screen); 1030 vcons_hard_switch(&scp->sc_console_screen); 1031 vcons_enable_polling(&sc->vd); 1032 if (scp->sc_ops.genfb_enable_polling) 1033 (*scp->sc_ops.genfb_enable_polling)(sc); 1034 } 1035 } 1036 1037 void 1038 genfb_disable_polling(device_t dev) 1039 { 1040 struct genfb_softc *sc = device_private(dev); 1041 struct genfb_private *scp = sc->sc_private; 1042 1043 if (scp == NULL) 1044 return; 1045 1046 if (scp->sc_console_screen.scr_vd) { 1047 if (scp->sc_ops.genfb_disable_polling) 1048 (*scp->sc_ops.genfb_disable_polling)(sc); 1049 vcons_disable_polling(&sc->vd); 1050 } 1051 } 1052 1053 #if GENFB_GLYPHCACHE > 0 1054 #define GLYPHCACHESIZE ((GENFB_GLYPHCACHE) * 1024 * 1024) 1055 1056 static inline int 1057 attr2idx(long attr) 1058 { 1059 if ((attr & 0xf0f00ff8) != 0) 1060 return -1; 1061 1062 return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0)); 1063 } 1064 1065 static int 1066 genfb_setup_glyphcache(struct genfb_softc *sc, long defattr) 1067 { 1068 struct genfb_private *scp = sc->sc_private; 1069 struct rasops_info *ri = &scp->sc_console_screen.scr_ri, 1070 *cri = &scp->sc_cache_ri; 1071 gc_bucket *b; 1072 int i, usedcells = 0, idx, j; 1073 1074 scp->sc_cache = kmem_alloc(GLYPHCACHESIZE, KM_SLEEP); 1075 1076 /* 1077 * now we build a mutant rasops_info for the cache - same pixel type 1078 * and such as the real fb, but only one character per line for 1079 * simplicity and locality 1080 */ 1081 memcpy(cri, ri, sizeof(struct rasops_info)); 1082 cri->ri_ops.putchar = scp->sc_putchar; 1083 cri->ri_width = ri->ri_font->fontwidth; 1084 cri->ri_stride = ri->ri_xscale; 1085 cri->ri_bits = scp->sc_cache; 1086 cri->ri_hwbits = NULL; 1087 cri->ri_origbits = scp->sc_cache; 1088 cri->ri_cols = 1; 1089 cri->ri_rows = GLYPHCACHESIZE / 1090 (cri->ri_stride * cri->ri_font->fontheight); 1091 cri->ri_xorigin = 0; 1092 cri->ri_yorigin = 0; 1093 cri->ri_xscale = ri->ri_xscale; 1094 cri->ri_yscale = ri->ri_font->fontheight * ri->ri_xscale; 1095 1096 printf("size %d %d %d\n", GLYPHCACHESIZE, ri->ri_width, ri->ri_stride); 1097 printf("cells: %d\n", cri->ri_rows); 1098 scp->sc_nbuckets = uimin(256, cri->ri_rows / 223); 1099 scp->sc_buckets = kmem_alloc(sizeof(gc_bucket) * scp->sc_nbuckets, 1100 KM_SLEEP); 1101 printf("buckets: %d\n", scp->sc_nbuckets); 1102 for (i = 0; i < scp->sc_nbuckets; i++) { 1103 b = &scp->sc_buckets[i]; 1104 b->gb_firstcell = usedcells; 1105 b->gb_numcells = uimin(223, cri->ri_rows - usedcells); 1106 usedcells += 223; 1107 b->gb_usedcells = 0; 1108 b->gb_index = -1; 1109 for (j = 0; j < 223; j++) b->gb_map[j] = -1; 1110 } 1111 1112 /* initialize the attribute map... */ 1113 for (i = 0; i < 256; i++) { 1114 scp->sc_attrmap[i] = -1; 1115 } 1116 1117 /* first bucket goes to default attr */ 1118 idx = attr2idx(defattr); 1119 printf("defattr %08lx idx %x\n", defattr, idx); 1120 1121 if (idx >= 0) { 1122 scp->sc_attrmap[idx] = 0; 1123 scp->sc_buckets[0].gb_index = idx; 1124 } 1125 1126 return 0; 1127 } 1128 1129 static void 1130 genfb_putchar(void *cookie, int row, int col, u_int c, long attr) 1131 { 1132 struct rasops_info *ri = cookie; 1133 struct vcons_screen *scr = ri->ri_hw; 1134 struct genfb_softc *sc = scr->scr_cookie; 1135 struct genfb_private *scp = sc->sc_private; 1136 uint8_t *src, *dst, *hwdst; 1137 gc_bucket *b; 1138 int i, idx, bi, cell; 1139 1140 attr &= ~WSATTR_USERMASK; 1141 1142 idx = attr2idx(attr); 1143 if (c < 33 || c > 255 || idx < 0) goto nope; 1144 1145 /* look for a bucket with the right attribute */ 1146 bi = scp->sc_attrmap[idx]; 1147 if (bi == -1) { 1148 /* nope, see if there's an empty one left */ 1149 bi = 1; 1150 while ((bi < scp->sc_nbuckets) && 1151 (scp->sc_buckets[bi].gb_index != -1)) { 1152 bi++; 1153 } 1154 if (bi < scp->sc_nbuckets) { 1155 /* found one -> grab it */ 1156 scp->sc_attrmap[idx] = bi; 1157 b = &scp->sc_buckets[bi]; 1158 b->gb_index = idx; 1159 b->gb_usedcells = 0; 1160 /* make sure this doesn't get evicted right away */ 1161 b->gb_lastread = time_uptime; 1162 } else { 1163 /* 1164 * still nothing 1165 * steal the least recently read bucket 1166 */ 1167 time_t moo = time_uptime; 1168 int oldest = 1; 1169 1170 for (i = 1; i < scp->sc_nbuckets; i++) { 1171 if (scp->sc_buckets[i].gb_lastread < moo) { 1172 oldest = i; 1173 moo = scp->sc_buckets[i].gb_lastread; 1174 } 1175 } 1176 1177 /* if we end up here all buckets must be in use */ 1178 b = &scp->sc_buckets[oldest]; 1179 scp->sc_attrmap[b->gb_index] = -1; 1180 b->gb_index = idx; 1181 b->gb_usedcells = 0; 1182 scp->sc_attrmap[idx] = oldest; 1183 /* now scrub it */ 1184 for (i = 0; i < 223; i++) 1185 b->gb_map[i] = -1; 1186 /* and set the time stamp */ 1187 b->gb_lastread = time_uptime; 1188 } 1189 } else { 1190 /* found one */ 1191 b = &scp->sc_buckets[bi]; 1192 } 1193 1194 /* see if there's room in the bucket */ 1195 if (b->gb_usedcells >= b->gb_numcells) goto nope; 1196 1197 cell = b->gb_map[c - 33]; 1198 if (cell == -1) { 1199 if (b->gb_usedcells >= b->gb_numcells) 1200 goto nope; 1201 cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1; 1202 b->gb_map[c - 33] = cell; 1203 cell += b->gb_firstcell; 1204 scp->sc_putchar(&scp->sc_cache_ri, cell, 0, c, attr); 1205 } else 1206 cell += b->gb_firstcell; 1207 1208 src = scp->sc_cache + cell * scp->sc_cache_ri.ri_yscale; 1209 dst = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale; 1210 for (i = 0; i < ri->ri_font->fontheight; i++) { 1211 memcpy(dst, src, ri->ri_xscale); 1212 src += ri->ri_xscale; 1213 dst += ri->ri_stride; 1214 } 1215 if (ri->ri_hwbits) { 1216 src = scp->sc_cache + cell * scp->sc_cache_ri.ri_yscale; 1217 hwdst = ri->ri_hwbits 1218 + row * ri->ri_yscale + col * ri->ri_xscale; 1219 for (i = 0; i < ri->ri_font->fontheight; i++) { 1220 memcpy(hwdst, src, ri->ri_xscale); 1221 src += ri->ri_xscale; 1222 hwdst += ri->ri_stride; 1223 } 1224 } 1225 b->gb_lastread = time_uptime; 1226 return; 1227 nope: 1228 scp->sc_putchar(cookie, row, col, c, attr); 1229 } 1230 1231 #endif 1232