1 /* 2 * Copyright (c) 2006-2009 Red Hat Inc. 3 * Copyright (c) 2006-2008 Intel Corporation 4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> 5 * 6 * DRM framebuffer helper functions 7 * 8 * Permission to use, copy, modify, distribute, and sell this software and its 9 * documentation for any purpose is hereby granted without fee, provided that 10 * the above copyright notice appear in all copies and that both that copyright 11 * notice and this permission notice appear in supporting documentation, and 12 * that the name of the copyright holders not be used in advertising or 13 * publicity pertaining to distribution of the software without specific, 14 * written prior permission. The copyright holders make no representations 15 * about the suitability of this software for any purpose. It is provided "as 16 * is" without express or implied warranty. 17 * 18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 24 * OF THIS SOFTWARE. 25 * 26 * Authors: 27 * Dave Airlie <airlied@linux.ie> 28 * Jesse Barnes <jesse.barnes@intel.com> 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <dev/drm2/drmP.h> 35 #include <dev/drm2/drm_crtc.h> 36 #include <dev/drm2/drm_fb_helper.h> 37 #include <dev/drm2/drm_crtc_helper.h> 38 39 #include <sys/kdb.h> 40 41 struct vt_kms_softc { 42 struct drm_fb_helper *fb_helper; 43 struct task fb_mode_task; 44 }; 45 46 static fb_enter_t vt_kms_postswitch; 47 static void vt_restore_fbdev_mode(void *, int); 48 49 /* Call restore out of vt(9) locks. */ 50 static void 51 vt_restore_fbdev_mode(void *arg, int pending) 52 { 53 struct drm_fb_helper *fb_helper; 54 struct vt_kms_softc *sc; 55 56 sc = (struct vt_kms_softc *)arg; 57 fb_helper = sc->fb_helper; 58 sx_xlock(&fb_helper->dev->mode_config.mutex); 59 drm_fb_helper_restore_fbdev_mode(fb_helper); 60 sx_xunlock(&fb_helper->dev->mode_config.mutex); 61 } 62 63 static int 64 vt_kms_postswitch(void *arg) 65 { 66 struct vt_kms_softc *sc; 67 68 sc = (struct vt_kms_softc *)arg; 69 70 if (!kdb_active && panicstr == NULL) 71 taskqueue_enqueue_fast(taskqueue_thread, &sc->fb_mode_task); 72 else 73 drm_fb_helper_restore_fbdev_mode(sc->fb_helper); 74 75 return (0); 76 } 77 78 static DRM_LIST_HEAD(kernel_fb_helper_list); 79 80 /* simple single crtc case helper function */ 81 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) 82 { 83 struct drm_device *dev = fb_helper->dev; 84 struct drm_connector *connector; 85 86 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 87 struct drm_fb_helper_connector *fb_helper_connector; 88 89 fb_helper_connector = malloc( 90 sizeof(struct drm_fb_helper_connector), DRM_MEM_KMS, 91 M_WAITOK | M_ZERO); 92 93 fb_helper_connector->connector = connector; 94 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; 95 } 96 return 0; 97 } 98 99 const char *fb_mode_option; 100 101 /** 102 * drm_fb_helper_connector_parse_command_line - parse command line for connector 103 * @connector - connector to parse line for 104 * @mode_option - per connector mode option 105 * 106 * This parses the connector specific then generic command lines for 107 * modes and options to configure the connector. 108 * 109 * This uses the same parameters as the fb modedb.c, except for extra 110 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd] 111 * 112 * enable/enable Digital/disable bit at the end 113 */ 114 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn, 115 const char *mode_option) 116 { 117 const char *name; 118 unsigned int namelen; 119 int res_specified = 0, bpp_specified = 0, refresh_specified = 0; 120 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0; 121 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0; 122 int i; 123 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED; 124 struct drm_fb_helper_cmdline_mode *cmdline_mode; 125 struct drm_connector *connector; 126 127 if (!fb_helper_conn) 128 return false; 129 connector = fb_helper_conn->connector; 130 131 cmdline_mode = &fb_helper_conn->cmdline_mode; 132 if (!mode_option) 133 mode_option = fb_mode_option; 134 135 if (!mode_option) { 136 cmdline_mode->specified = false; 137 return false; 138 } 139 140 name = mode_option; 141 namelen = strlen(name); 142 for (i = namelen-1; i >= 0; i--) { 143 switch (name[i]) { 144 case '@': 145 namelen = i; 146 if (!refresh_specified && !bpp_specified && 147 !yres_specified) { 148 refresh = strtol(&name[i+1], NULL, 10); 149 refresh_specified = 1; 150 if (cvt || rb) 151 cvt = 0; 152 } else 153 goto done; 154 break; 155 case '-': 156 namelen = i; 157 if (!bpp_specified && !yres_specified) { 158 bpp = strtol(&name[i+1], NULL, 10); 159 bpp_specified = 1; 160 if (cvt || rb) 161 cvt = 0; 162 } else 163 goto done; 164 break; 165 case 'x': 166 if (!yres_specified) { 167 yres = strtol(&name[i+1], NULL, 10); 168 yres_specified = 1; 169 } else 170 goto done; 171 case '0' ... '9': 172 break; 173 case 'M': 174 if (!yres_specified) 175 cvt = 1; 176 break; 177 case 'R': 178 if (cvt) 179 rb = 1; 180 break; 181 case 'm': 182 if (!cvt) 183 margins = 1; 184 break; 185 case 'i': 186 if (!cvt) 187 interlace = 1; 188 break; 189 case 'e': 190 force = DRM_FORCE_ON; 191 break; 192 case 'D': 193 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) && 194 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB)) 195 force = DRM_FORCE_ON; 196 else 197 force = DRM_FORCE_ON_DIGITAL; 198 break; 199 case 'd': 200 force = DRM_FORCE_OFF; 201 break; 202 default: 203 goto done; 204 } 205 } 206 if (i < 0 && yres_specified) { 207 xres = strtol(name, NULL, 10); 208 res_specified = 1; 209 } 210 done: 211 212 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n", 213 drm_get_connector_name(connector), xres, yres, 214 (refresh) ? refresh : 60, (rb) ? " reduced blanking" : 215 "", (margins) ? " with margins" : "", (interlace) ? 216 " interlaced" : ""); 217 218 if (force) { 219 const char *s; 220 switch (force) { 221 case DRM_FORCE_OFF: s = "OFF"; break; 222 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break; 223 default: 224 case DRM_FORCE_ON: s = "ON"; break; 225 } 226 227 DRM_INFO("forcing %s connector %s\n", 228 drm_get_connector_name(connector), s); 229 connector->force = force; 230 } 231 232 if (res_specified) { 233 cmdline_mode->specified = true; 234 cmdline_mode->xres = xres; 235 cmdline_mode->yres = yres; 236 } 237 238 if (refresh_specified) { 239 cmdline_mode->refresh_specified = true; 240 cmdline_mode->refresh = refresh; 241 } 242 243 if (bpp_specified) { 244 cmdline_mode->bpp_specified = true; 245 cmdline_mode->bpp = bpp; 246 } 247 cmdline_mode->rb = rb ? true : false; 248 cmdline_mode->cvt = cvt ? true : false; 249 cmdline_mode->interlace = interlace ? true : false; 250 251 return true; 252 } 253 254 static int 255 fb_get_options(const char *connector_name, char **option) 256 { 257 258 /* 259 * TODO: store mode options pointer in ${option} for connector with 260 * name ${connector_name} 261 */ 262 return (1); 263 } 264 265 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper) 266 { 267 struct drm_fb_helper_connector *fb_helper_conn; 268 int i; 269 270 for (i = 0; i < fb_helper->connector_count; i++) { 271 char *option = NULL; 272 273 fb_helper_conn = fb_helper->connector_info[i]; 274 275 /* do something on return - turn off connector maybe */ 276 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option)) 277 continue; 278 279 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option); 280 } 281 return 0; 282 } 283 284 #if 0 285 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper) 286 { 287 uint16_t *r_base, *g_base, *b_base; 288 int i; 289 290 r_base = crtc->gamma_store; 291 g_base = r_base + crtc->gamma_size; 292 b_base = g_base + crtc->gamma_size; 293 294 for (i = 0; i < crtc->gamma_size; i++) 295 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i); 296 } 297 298 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) 299 { 300 uint16_t *r_base, *g_base, *b_base; 301 302 r_base = crtc->gamma_store; 303 g_base = r_base + crtc->gamma_size; 304 b_base = g_base + crtc->gamma_size; 305 306 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size); 307 } 308 #endif 309 310 #if 0 311 int drm_fb_helper_debug_enter(struct fb_info *info) 312 { 313 struct drm_fb_helper *helper = info->par; 314 struct drm_crtc_helper_funcs *funcs; 315 int i; 316 317 if (list_empty(&kernel_fb_helper_list)) 318 return false; 319 320 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 321 for (i = 0; i < helper->crtc_count; i++) { 322 struct drm_mode_set *mode_set = 323 &helper->crtc_info[i].mode_set; 324 325 if (!mode_set->crtc->enabled) 326 continue; 327 328 funcs = mode_set->crtc->helper_private; 329 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper); 330 funcs->mode_set_base_atomic(mode_set->crtc, 331 mode_set->fb, 332 mode_set->x, 333 mode_set->y, 334 ENTER_ATOMIC_MODE_SET); 335 } 336 } 337 338 return 0; 339 } 340 #endif 341 342 #if 0 343 /* Find the real fb for a given fb helper CRTC */ 344 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc) 345 { 346 struct drm_device *dev = crtc->dev; 347 struct drm_crtc *c; 348 349 list_for_each_entry(c, &dev->mode_config.crtc_list, head) { 350 if (crtc->base.id == c->base.id) 351 return c->fb; 352 } 353 354 return NULL; 355 } 356 #endif 357 358 #if 0 359 int drm_fb_helper_debug_leave(struct fb_info *info) 360 { 361 struct drm_fb_helper *helper = info->par; 362 struct drm_crtc *crtc; 363 struct drm_crtc_helper_funcs *funcs; 364 struct drm_framebuffer *fb; 365 int i; 366 367 for (i = 0; i < helper->crtc_count; i++) { 368 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; 369 crtc = mode_set->crtc; 370 funcs = crtc->helper_private; 371 fb = drm_mode_config_fb(crtc); 372 373 if (!crtc->enabled) 374 continue; 375 376 if (!fb) { 377 DRM_ERROR("no fb to restore??\n"); 378 continue; 379 } 380 381 drm_fb_helper_restore_lut_atomic(mode_set->crtc); 382 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, 383 crtc->y, LEAVE_ATOMIC_MODE_SET); 384 } 385 386 return 0; 387 } 388 #endif 389 390 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper) 391 { 392 bool error = false; 393 int i, ret; 394 for (i = 0; i < fb_helper->crtc_count; i++) { 395 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; 396 ret = drm_crtc_helper_set_config(mode_set); 397 if (ret) 398 error = true; 399 } 400 return error; 401 } 402 403 #if 0 404 bool drm_fb_helper_force_kernel_mode(void) 405 { 406 bool ret, error = false; 407 struct drm_fb_helper *helper; 408 409 if (list_empty(&kernel_fb_helper_list)) 410 return false; 411 412 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 413 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF) 414 continue; 415 416 ret = drm_fb_helper_restore_fbdev_mode(helper); 417 if (ret) 418 error = true; 419 } 420 return error; 421 } 422 #endif 423 424 #if 0 425 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed, 426 void *panic_str) 427 { 428 printf("panic occurred, switching back to text console\n"); 429 return drm_fb_helper_force_kernel_mode(); 430 return 0; 431 } 432 433 static struct notifier_block paniced = { 434 .notifier_call = drm_fb_helper_panic, 435 }; 436 437 /** 438 * drm_fb_helper_restore - restore the framebuffer console (kernel) config 439 * 440 * Restore's the kernel's fbcon mode, used for lastclose & panic paths. 441 */ 442 void drm_fb_helper_restore(void) 443 { 444 bool ret; 445 ret = drm_fb_helper_force_kernel_mode(); 446 if (ret == true) 447 DRM_ERROR("Failed to restore crtc configuration\n"); 448 } 449 450 #ifdef CONFIG_MAGIC_SYSRQ 451 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) 452 { 453 drm_fb_helper_restore(); 454 } 455 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); 456 457 static void drm_fb_helper_sysrq(int dummy1) 458 { 459 schedule_work(&drm_fb_helper_restore_work); 460 } 461 462 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { 463 .handler = drm_fb_helper_sysrq, 464 .help_msg = "force-fb(V)", 465 .action_msg = "Restore framebuffer console", 466 }; 467 #else 468 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; 469 #endif 470 #endif 471 472 #if 0 473 static void drm_fb_helper_on(struct fb_info *info) 474 { 475 struct drm_fb_helper *fb_helper = info->par; 476 struct drm_device *dev = fb_helper->dev; 477 struct drm_crtc *crtc; 478 struct drm_crtc_helper_funcs *crtc_funcs; 479 struct drm_connector *connector; 480 struct drm_encoder *encoder; 481 int i, j; 482 483 /* 484 * For each CRTC in this fb, turn the crtc on then, 485 * find all associated encoders and turn them on. 486 */ 487 sx_xlock(&dev->mode_config.mutex); 488 for (i = 0; i < fb_helper->crtc_count; i++) { 489 crtc = fb_helper->crtc_info[i].mode_set.crtc; 490 crtc_funcs = crtc->helper_private; 491 492 if (!crtc->enabled) 493 continue; 494 495 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON); 496 497 /* Walk the connectors & encoders on this fb turning them on */ 498 for (j = 0; j < fb_helper->connector_count; j++) { 499 connector = fb_helper->connector_info[j]->connector; 500 connector->dpms = DRM_MODE_DPMS_ON; 501 drm_connector_property_set_value(connector, 502 dev->mode_config.dpms_property, 503 DRM_MODE_DPMS_ON); 504 } 505 /* Found a CRTC on this fb, now find encoders */ 506 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 507 if (encoder->crtc == crtc) { 508 struct drm_encoder_helper_funcs *encoder_funcs; 509 510 encoder_funcs = encoder->helper_private; 511 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON); 512 } 513 } 514 } 515 sx_xunlock(&dev->mode_config.mutex); 516 } 517 #endif 518 519 #if 0 520 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode) 521 { 522 struct drm_fb_helper *fb_helper = info->par; 523 struct drm_device *dev = fb_helper->dev; 524 struct drm_crtc *crtc; 525 struct drm_crtc_helper_funcs *crtc_funcs; 526 struct drm_connector *connector; 527 struct drm_encoder *encoder; 528 int i, j; 529 530 /* 531 * For each CRTC in this fb, find all associated encoders 532 * and turn them off, then turn off the CRTC. 533 */ 534 sx_xlock(&dev->mode_config.mutex); 535 for (i = 0; i < fb_helper->crtc_count; i++) { 536 crtc = fb_helper->crtc_info[i].mode_set.crtc; 537 crtc_funcs = crtc->helper_private; 538 539 if (!crtc->enabled) 540 continue; 541 542 /* Walk the connectors on this fb and mark them off */ 543 for (j = 0; j < fb_helper->connector_count; j++) { 544 connector = fb_helper->connector_info[j]->connector; 545 connector->dpms = dpms_mode; 546 drm_connector_property_set_value(connector, 547 dev->mode_config.dpms_property, 548 dpms_mode); 549 } 550 /* Found a CRTC on this fb, now find encoders */ 551 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 552 if (encoder->crtc == crtc) { 553 struct drm_encoder_helper_funcs *encoder_funcs; 554 555 encoder_funcs = encoder->helper_private; 556 encoder_funcs->dpms(encoder, dpms_mode); 557 } 558 } 559 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF); 560 } 561 sx_xunlock(&dev->mode_config.mutex); 562 } 563 #endif 564 565 #if 0 566 int drm_fb_helper_blank(int blank, struct fb_info *info) 567 { 568 switch (blank) { 569 /* Display: On; HSync: On, VSync: On */ 570 case FB_BLANK_UNBLANK: 571 drm_fb_helper_on(info); 572 break; 573 /* Display: Off; HSync: On, VSync: On */ 574 case FB_BLANK_NORMAL: 575 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY); 576 break; 577 /* Display: Off; HSync: Off, VSync: On */ 578 case FB_BLANK_HSYNC_SUSPEND: 579 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY); 580 break; 581 /* Display: Off; HSync: On, VSync: Off */ 582 case FB_BLANK_VSYNC_SUSPEND: 583 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND); 584 break; 585 /* Display: Off; HSync: Off, VSync: Off */ 586 case FB_BLANK_POWERDOWN: 587 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF); 588 break; 589 } 590 return 0; 591 } 592 #endif 593 594 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) 595 { 596 int i; 597 598 for (i = 0; i < helper->connector_count; i++) 599 free(helper->connector_info[i], DRM_MEM_KMS); 600 free(helper->connector_info, DRM_MEM_KMS); 601 for (i = 0; i < helper->crtc_count; i++) { 602 free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS); 603 if (helper->crtc_info[i].mode_set.mode) 604 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode); 605 } 606 free(helper->crtc_info, DRM_MEM_KMS); 607 } 608 609 int drm_fb_helper_init(struct drm_device *dev, 610 struct drm_fb_helper *fb_helper, 611 int crtc_count, int max_conn_count) 612 { 613 struct drm_crtc *crtc; 614 int i; 615 616 fb_helper->dev = dev; 617 618 INIT_LIST_HEAD(&fb_helper->kernel_fb_list); 619 620 fb_helper->crtc_info = malloc(crtc_count * 621 sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO); 622 623 fb_helper->crtc_count = crtc_count; 624 fb_helper->connector_info = malloc(dev->mode_config.num_connector * 625 sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS, 626 M_WAITOK | M_ZERO); 627 fb_helper->connector_count = 0; 628 629 for (i = 0; i < crtc_count; i++) { 630 fb_helper->crtc_info[i].mode_set.connectors = 631 malloc(max_conn_count * sizeof(struct drm_connector *), 632 DRM_MEM_KMS, M_WAITOK | M_ZERO); 633 634 fb_helper->crtc_info[i].mode_set.num_connectors = 0; 635 } 636 637 i = 0; 638 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 639 fb_helper->crtc_info[i].crtc_id = crtc->base.id; 640 fb_helper->crtc_info[i].mode_set.crtc = crtc; 641 i++; 642 } 643 fb_helper->conn_limit = max_conn_count; 644 return 0; 645 } 646 647 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) 648 { 649 if (!list_empty(&fb_helper->kernel_fb_list)) { 650 list_del(&fb_helper->kernel_fb_list); 651 if (list_empty(&kernel_fb_helper_list)) { 652 #if 0 653 printk(KERN_INFO "drm: unregistered panic notifier\n"); 654 atomic_notifier_chain_unregister(&panic_notifier_list, 655 &paniced); 656 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 657 #endif 658 } 659 } 660 661 drm_fb_helper_crtc_free(fb_helper); 662 663 } 664 665 #if 0 666 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, 667 u16 blue, u16 regno, struct fb_info *info) 668 { 669 struct drm_fb_helper *fb_helper = info->par; 670 struct drm_framebuffer *fb = fb_helper->fb; 671 int pindex; 672 673 if (info->fix.visual == FB_VISUAL_trueCOLOR) { 674 u32 *palette; 675 u32 value; 676 /* place color in psuedopalette */ 677 if (regno > 16) 678 return -EINVAL; 679 palette = (u32 *)info->pseudo_palette; 680 red >>= (16 - info->var.red.length); 681 green >>= (16 - info->var.green.length); 682 blue >>= (16 - info->var.blue.length); 683 value = (red << info->var.red.offset) | 684 (green << info->var.green.offset) | 685 (blue << info->var.blue.offset); 686 if (info->var.transp.length > 0) { 687 u32 mask = (1 << info->var.transp.length) - 1; 688 mask <<= info->var.transp.offset; 689 value |= mask; 690 } 691 palette[regno] = value; 692 return 0; 693 } 694 695 pindex = regno; 696 697 if (fb->bits_per_pixel == 16) { 698 pindex = regno << 3; 699 700 if (fb->depth == 16 && regno > 63) 701 return -EINVAL; 702 if (fb->depth == 15 && regno > 31) 703 return -EINVAL; 704 705 if (fb->depth == 16) { 706 u16 r, g, b; 707 int i; 708 if (regno < 32) { 709 for (i = 0; i < 8; i++) 710 fb_helper->funcs->gamma_set(crtc, red, 711 green, blue, pindex + i); 712 } 713 714 fb_helper->funcs->gamma_get(crtc, &r, 715 &g, &b, 716 pindex >> 1); 717 718 for (i = 0; i < 4; i++) 719 fb_helper->funcs->gamma_set(crtc, r, 720 green, b, 721 (pindex >> 1) + i); 722 } 723 } 724 725 if (fb->depth != 16) 726 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex); 727 return 0; 728 } 729 #endif 730 731 #if 0 732 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) 733 { 734 struct drm_fb_helper *fb_helper = info->par; 735 struct drm_crtc_helper_funcs *crtc_funcs; 736 u16 *red, *green, *blue, *transp; 737 struct drm_crtc *crtc; 738 int i, j, rc = 0; 739 int start; 740 741 for (i = 0; i < fb_helper->crtc_count; i++) { 742 crtc = fb_helper->crtc_info[i].mode_set.crtc; 743 crtc_funcs = crtc->helper_private; 744 745 red = cmap->red; 746 green = cmap->green; 747 blue = cmap->blue; 748 transp = cmap->transp; 749 start = cmap->start; 750 751 for (j = 0; j < cmap->len; j++) { 752 u16 hred, hgreen, hblue, htransp = 0xffff; 753 754 hred = *red++; 755 hgreen = *green++; 756 hblue = *blue++; 757 758 if (transp) 759 htransp = *transp++; 760 761 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); 762 if (rc) 763 return rc; 764 } 765 crtc_funcs->load_lut(crtc); 766 } 767 return rc; 768 } 769 #endif 770 771 #if 0 772 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 773 struct fb_info *info) 774 { 775 struct drm_fb_helper *fb_helper = info->par; 776 struct drm_framebuffer *fb = fb_helper->fb; 777 int depth; 778 779 if (var->pixclock != 0 || in_dbg_master()) 780 return -EINVAL; 781 782 /* Need to resize the fb object !!! */ 783 if (var->bits_per_pixel > fb->bits_per_pixel || 784 var->xres > fb->width || var->yres > fb->height || 785 var->xres_virtual > fb->width || var->yres_virtual > fb->height) { 786 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " 787 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", 788 var->xres, var->yres, var->bits_per_pixel, 789 var->xres_virtual, var->yres_virtual, 790 fb->width, fb->height, fb->bits_per_pixel); 791 return -EINVAL; 792 } 793 794 switch (var->bits_per_pixel) { 795 case 16: 796 depth = (var->green.length == 6) ? 16 : 15; 797 break; 798 case 32: 799 depth = (var->transp.length > 0) ? 32 : 24; 800 break; 801 default: 802 depth = var->bits_per_pixel; 803 break; 804 } 805 806 switch (depth) { 807 case 8: 808 var->red.offset = 0; 809 var->green.offset = 0; 810 var->blue.offset = 0; 811 var->red.length = 8; 812 var->green.length = 8; 813 var->blue.length = 8; 814 var->transp.length = 0; 815 var->transp.offset = 0; 816 break; 817 case 15: 818 var->red.offset = 10; 819 var->green.offset = 5; 820 var->blue.offset = 0; 821 var->red.length = 5; 822 var->green.length = 5; 823 var->blue.length = 5; 824 var->transp.length = 1; 825 var->transp.offset = 15; 826 break; 827 case 16: 828 var->red.offset = 11; 829 var->green.offset = 5; 830 var->blue.offset = 0; 831 var->red.length = 5; 832 var->green.length = 6; 833 var->blue.length = 5; 834 var->transp.length = 0; 835 var->transp.offset = 0; 836 break; 837 case 24: 838 var->red.offset = 16; 839 var->green.offset = 8; 840 var->blue.offset = 0; 841 var->red.length = 8; 842 var->green.length = 8; 843 var->blue.length = 8; 844 var->transp.length = 0; 845 var->transp.offset = 0; 846 break; 847 case 32: 848 var->red.offset = 16; 849 var->green.offset = 8; 850 var->blue.offset = 0; 851 var->red.length = 8; 852 var->green.length = 8; 853 var->blue.length = 8; 854 var->transp.length = 8; 855 var->transp.offset = 24; 856 break; 857 default: 858 return -EINVAL; 859 } 860 return 0; 861 } 862 #endif 863 864 #if 0 865 /* this will let fbcon do the mode init */ 866 int drm_fb_helper_set_par(struct fb_info *info) 867 { 868 struct drm_fb_helper *fb_helper = info->par; 869 struct drm_device *dev = fb_helper->dev; 870 struct fb_var_screeninfo *var = &info->var; 871 struct drm_crtc *crtc; 872 int ret; 873 int i; 874 875 if (var->pixclock != 0) { 876 DRM_ERROR("PIXEL CLOCK SET\n"); 877 return -EINVAL; 878 } 879 880 mutex_lock(&dev->mode_config.mutex); 881 for (i = 0; i < fb_helper->crtc_count; i++) { 882 crtc = fb_helper->crtc_info[i].mode_set.crtc; 883 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set); 884 if (ret) { 885 mutex_unlock(&dev->mode_config.mutex); 886 return ret; 887 } 888 } 889 mutex_unlock(&dev->mode_config.mutex); 890 891 if (fb_helper->delayed_hotplug) { 892 fb_helper->delayed_hotplug = false; 893 drm_fb_helper_hotplug_event(fb_helper); 894 } 895 return 0; 896 } 897 #endif 898 899 #if 0 900 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 901 struct fb_info *info) 902 { 903 struct drm_fb_helper *fb_helper = info->par; 904 struct drm_device *dev = fb_helper->dev; 905 struct drm_mode_set *modeset; 906 struct drm_crtc *crtc; 907 int ret = 0; 908 int i; 909 910 mutex_lock(&dev->mode_config.mutex); 911 for (i = 0; i < fb_helper->crtc_count; i++) { 912 crtc = fb_helper->crtc_info[i].mode_set.crtc; 913 914 modeset = &fb_helper->crtc_info[i].mode_set; 915 916 modeset->x = var->xoffset; 917 modeset->y = var->yoffset; 918 919 if (modeset->num_connectors) { 920 ret = crtc->funcs->set_config(modeset); 921 if (!ret) { 922 info->var.xoffset = var->xoffset; 923 info->var.yoffset = var->yoffset; 924 } 925 } 926 } 927 mutex_unlock(&dev->mode_config.mutex); 928 return ret; 929 } 930 #endif 931 932 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, 933 int preferred_bpp) 934 { 935 int new_fb = 0; 936 int crtc_count = 0; 937 int i; 938 struct fb_info *info; 939 struct drm_fb_helper_surface_size sizes; 940 int gamma_size = 0; 941 struct vt_kms_softc *sc; 942 device_t kdev; 943 944 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); 945 sizes.surface_depth = 24; 946 sizes.surface_bpp = 32; 947 sizes.fb_width = (unsigned)-1; 948 sizes.fb_height = (unsigned)-1; 949 950 /* if driver picks 8 or 16 by default use that 951 for both depth/bpp */ 952 if (preferred_bpp != sizes.surface_bpp) { 953 sizes.surface_depth = sizes.surface_bpp = preferred_bpp; 954 } 955 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 956 for (i = 0; i < fb_helper->connector_count; i++) { 957 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; 958 struct drm_fb_helper_cmdline_mode *cmdline_mode; 959 960 cmdline_mode = &fb_helper_conn->cmdline_mode; 961 962 if (cmdline_mode->bpp_specified) { 963 switch (cmdline_mode->bpp) { 964 case 8: 965 sizes.surface_depth = sizes.surface_bpp = 8; 966 break; 967 case 15: 968 sizes.surface_depth = 15; 969 sizes.surface_bpp = 16; 970 break; 971 case 16: 972 sizes.surface_depth = sizes.surface_bpp = 16; 973 break; 974 case 24: 975 sizes.surface_depth = sizes.surface_bpp = 24; 976 break; 977 case 32: 978 sizes.surface_depth = 24; 979 sizes.surface_bpp = 32; 980 break; 981 } 982 break; 983 } 984 } 985 986 crtc_count = 0; 987 for (i = 0; i < fb_helper->crtc_count; i++) { 988 struct drm_display_mode *desired_mode; 989 desired_mode = fb_helper->crtc_info[i].desired_mode; 990 991 if (desired_mode) { 992 if (gamma_size == 0) 993 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; 994 if (desired_mode->hdisplay < sizes.fb_width) 995 sizes.fb_width = desired_mode->hdisplay; 996 if (desired_mode->vdisplay < sizes.fb_height) 997 sizes.fb_height = desired_mode->vdisplay; 998 if (desired_mode->hdisplay > sizes.surface_width) 999 sizes.surface_width = desired_mode->hdisplay; 1000 if (desired_mode->vdisplay > sizes.surface_height) 1001 sizes.surface_height = desired_mode->vdisplay; 1002 crtc_count++; 1003 } 1004 } 1005 1006 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { 1007 /* hmm everyone went away - assume VGA cable just fell out 1008 and will come back later. */ 1009 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); 1010 sizes.fb_width = sizes.surface_width = 1024; 1011 sizes.fb_height = sizes.surface_height = 768; 1012 } 1013 1014 /* push down into drivers */ 1015 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); 1016 if (new_fb < 0) 1017 return new_fb; 1018 1019 sc = malloc(sizeof(struct vt_kms_softc), DRM_MEM_KMS, 1020 M_WAITOK | M_ZERO); 1021 sc->fb_helper = fb_helper; 1022 TASK_INIT(&sc->fb_mode_task, 0, vt_restore_fbdev_mode, sc); 1023 1024 info = fb_helper->fbdev; 1025 1026 info->fb_name = device_get_nameunit(fb_helper->dev->device); 1027 info->fb_depth = fb_helper->fb->bits_per_pixel; 1028 info->fb_height = fb_helper->fb->height; 1029 info->fb_width = fb_helper->fb->width; 1030 info->fb_stride = fb_helper->fb->pitches[0]; 1031 info->fb_priv = sc; 1032 info->enter = &vt_kms_postswitch; 1033 1034 /* set the fb pointer */ 1035 for (i = 0; i < fb_helper->crtc_count; i++) { 1036 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; 1037 } 1038 1039 if (new_fb) { 1040 device_t fbd; 1041 int ret; 1042 1043 kdev = fb_helper->dev->device; 1044 fbd = device_add_child(kdev, "fbd", device_get_unit(kdev)); 1045 if (fbd != NULL) 1046 ret = device_probe_and_attach(fbd); 1047 else 1048 ret = ENODEV; 1049 #ifdef DEV_VT 1050 if (ret != 0) 1051 DRM_ERROR("Failed to attach fbd device: %d\n", ret); 1052 #endif 1053 } 1054 return 0; 1055 } 1056 1057 #if 0 1058 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1059 uint32_t depth) 1060 { 1061 info->fix.type = FB_TYPE_PACKED_PIXELS; 1062 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : 1063 FB_VISUAL_trueCOLOR; 1064 info->fix.mmio_start = 0; 1065 info->fix.mmio_len = 0; 1066 info->fix.type_aux = 0; 1067 info->fix.xpanstep = 1; /* doing it in hw */ 1068 info->fix.ypanstep = 1; /* doing it in hw */ 1069 info->fix.ywrapstep = 0; 1070 info->fix.accel = FB_ACCEL_NONE; 1071 info->fix.type_aux = 0; 1072 1073 info->fix.line_length = pitch; 1074 return; 1075 } 1076 1077 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, 1078 uint32_t fb_width, uint32_t fb_height) 1079 { 1080 struct drm_framebuffer *fb = fb_helper->fb; 1081 info->pseudo_palette = fb_helper->pseudo_palette; 1082 info->var.xres_virtual = fb->width; 1083 info->var.yres_virtual = fb->height; 1084 info->var.bits_per_pixel = fb->bits_per_pixel; 1085 info->var.accel_flags = FB_ACCELF_TEXT; 1086 info->var.xoffset = 0; 1087 info->var.yoffset = 0; 1088 info->var.activate = FB_ACTIVATE_NOW; 1089 info->var.height = -1; 1090 info->var.width = -1; 1091 1092 switch (fb->depth) { 1093 case 8: 1094 info->var.red.offset = 0; 1095 info->var.green.offset = 0; 1096 info->var.blue.offset = 0; 1097 info->var.red.length = 8; /* 8bit DAC */ 1098 info->var.green.length = 8; 1099 info->var.blue.length = 8; 1100 info->var.transp.offset = 0; 1101 info->var.transp.length = 0; 1102 break; 1103 case 15: 1104 info->var.red.offset = 10; 1105 info->var.green.offset = 5; 1106 info->var.blue.offset = 0; 1107 info->var.red.length = 5; 1108 info->var.green.length = 5; 1109 info->var.blue.length = 5; 1110 info->var.transp.offset = 15; 1111 info->var.transp.length = 1; 1112 break; 1113 case 16: 1114 info->var.red.offset = 11; 1115 info->var.green.offset = 5; 1116 info->var.blue.offset = 0; 1117 info->var.red.length = 5; 1118 info->var.green.length = 6; 1119 info->var.blue.length = 5; 1120 info->var.transp.offset = 0; 1121 break; 1122 case 24: 1123 info->var.red.offset = 16; 1124 info->var.green.offset = 8; 1125 info->var.blue.offset = 0; 1126 info->var.red.length = 8; 1127 info->var.green.length = 8; 1128 info->var.blue.length = 8; 1129 info->var.transp.offset = 0; 1130 info->var.transp.length = 0; 1131 break; 1132 case 32: 1133 info->var.red.offset = 16; 1134 info->var.green.offset = 8; 1135 info->var.blue.offset = 0; 1136 info->var.red.length = 8; 1137 info->var.green.length = 8; 1138 info->var.blue.length = 8; 1139 info->var.transp.offset = 24; 1140 info->var.transp.length = 8; 1141 break; 1142 default: 1143 break; 1144 } 1145 1146 info->var.xres = fb_width; 1147 info->var.yres = fb_height; 1148 } 1149 #endif 1150 1151 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, 1152 uint32_t maxX, 1153 uint32_t maxY) 1154 { 1155 struct drm_connector *connector; 1156 int count = 0; 1157 int i; 1158 1159 for (i = 0; i < fb_helper->connector_count; i++) { 1160 connector = fb_helper->connector_info[i]->connector; 1161 count += connector->funcs->fill_modes(connector, maxX, maxY); 1162 } 1163 1164 return count; 1165 } 1166 1167 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) 1168 { 1169 struct drm_display_mode *mode; 1170 1171 list_for_each_entry(mode, &fb_connector->connector->modes, head) { 1172 if (drm_mode_width(mode) > width || 1173 drm_mode_height(mode) > height) 1174 continue; 1175 if (mode->type & DRM_MODE_TYPE_PREFERRED) 1176 return mode; 1177 } 1178 return NULL; 1179 } 1180 1181 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) 1182 { 1183 struct drm_fb_helper_cmdline_mode *cmdline_mode; 1184 cmdline_mode = &fb_connector->cmdline_mode; 1185 return cmdline_mode->specified; 1186 } 1187 1188 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, 1189 int width, int height) 1190 { 1191 struct drm_cmdline_mode *cmdline_mode; 1192 struct drm_display_mode *mode = NULL; 1193 1194 cmdline_mode = &fb_helper_conn->cmdline_mode1; 1195 if (cmdline_mode->specified == false && 1196 !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector, 1197 cmdline_mode)) 1198 return (NULL); 1199 1200 /* attempt to find a matching mode in the list of modes 1201 * we have gotten so far, if not add a CVT mode that conforms 1202 */ 1203 if (cmdline_mode->rb || cmdline_mode->margins) 1204 goto create_mode; 1205 1206 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1207 /* check width/height */ 1208 if (mode->hdisplay != cmdline_mode->xres || 1209 mode->vdisplay != cmdline_mode->yres) 1210 continue; 1211 1212 if (cmdline_mode->refresh_specified) { 1213 if (mode->vrefresh != cmdline_mode->refresh) 1214 continue; 1215 } 1216 1217 if (cmdline_mode->interlace) { 1218 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 1219 continue; 1220 } 1221 return mode; 1222 } 1223 1224 create_mode: 1225 if (cmdline_mode->cvt) 1226 mode = drm_cvt_mode(fb_helper_conn->connector->dev, 1227 cmdline_mode->xres, cmdline_mode->yres, 1228 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60, 1229 cmdline_mode->rb, cmdline_mode->interlace, 1230 cmdline_mode->margins); 1231 else 1232 mode = drm_gtf_mode(fb_helper_conn->connector->dev, 1233 cmdline_mode->xres, cmdline_mode->yres, 1234 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60, 1235 cmdline_mode->interlace, 1236 cmdline_mode->margins); 1237 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); 1238 list_add(&mode->head, &fb_helper_conn->connector->modes); 1239 return mode; 1240 } 1241 1242 static bool drm_connector_enabled(struct drm_connector *connector, bool strict) 1243 { 1244 bool enable; 1245 1246 if (strict) { 1247 enable = connector->status == connector_status_connected; 1248 } else { 1249 enable = connector->status != connector_status_disconnected; 1250 } 1251 return enable; 1252 } 1253 1254 static void drm_enable_connectors(struct drm_fb_helper *fb_helper, 1255 bool *enabled) 1256 { 1257 bool any_enabled = false; 1258 struct drm_connector *connector; 1259 int i = 0; 1260 1261 for (i = 0; i < fb_helper->connector_count; i++) { 1262 connector = fb_helper->connector_info[i]->connector; 1263 enabled[i] = drm_connector_enabled(connector, true); 1264 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, 1265 enabled[i] ? "yes" : "no"); 1266 any_enabled |= enabled[i]; 1267 } 1268 1269 if (any_enabled) 1270 return; 1271 1272 for (i = 0; i < fb_helper->connector_count; i++) { 1273 connector = fb_helper->connector_info[i]->connector; 1274 enabled[i] = drm_connector_enabled(connector, false); 1275 } 1276 } 1277 1278 static bool drm_target_cloned(struct drm_fb_helper *fb_helper, 1279 struct drm_display_mode **modes, 1280 bool *enabled, int width, int height) 1281 { 1282 int count, i, j; 1283 bool can_clone = false; 1284 struct drm_fb_helper_connector *fb_helper_conn; 1285 struct drm_display_mode *dmt_mode, *mode; 1286 1287 /* only contemplate cloning in the single crtc case */ 1288 if (fb_helper->crtc_count > 1) 1289 return false; 1290 1291 count = 0; 1292 for (i = 0; i < fb_helper->connector_count; i++) { 1293 if (enabled[i]) 1294 count++; 1295 } 1296 1297 /* only contemplate cloning if more than one connector is enabled */ 1298 if (count <= 1) 1299 return false; 1300 1301 /* check the command line or if nothing common pick 1024x768 */ 1302 can_clone = true; 1303 for (i = 0; i < fb_helper->connector_count; i++) { 1304 if (!enabled[i]) 1305 continue; 1306 fb_helper_conn = fb_helper->connector_info[i]; 1307 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 1308 if (!modes[i]) { 1309 can_clone = false; 1310 break; 1311 } 1312 for (j = 0; j < i; j++) { 1313 if (!enabled[j]) 1314 continue; 1315 if (!drm_mode_equal(modes[j], modes[i])) 1316 can_clone = false; 1317 } 1318 } 1319 1320 if (can_clone) { 1321 DRM_DEBUG_KMS("can clone using command line\n"); 1322 return true; 1323 } 1324 1325 /* try and find a 1024x768 mode on each connector */ 1326 can_clone = true; 1327 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60); 1328 1329 for (i = 0; i < fb_helper->connector_count; i++) { 1330 1331 if (!enabled[i]) 1332 continue; 1333 1334 fb_helper_conn = fb_helper->connector_info[i]; 1335 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1336 if (drm_mode_equal(mode, dmt_mode)) 1337 modes[i] = mode; 1338 } 1339 if (!modes[i]) 1340 can_clone = false; 1341 } 1342 1343 if (can_clone) { 1344 DRM_DEBUG_KMS("can clone using 1024x768\n"); 1345 return true; 1346 } 1347 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); 1348 return false; 1349 } 1350 1351 static bool drm_target_preferred(struct drm_fb_helper *fb_helper, 1352 struct drm_display_mode **modes, 1353 bool *enabled, int width, int height) 1354 { 1355 struct drm_fb_helper_connector *fb_helper_conn; 1356 int i; 1357 1358 for (i = 0; i < fb_helper->connector_count; i++) { 1359 fb_helper_conn = fb_helper->connector_info[i]; 1360 1361 if (enabled[i] == false) 1362 continue; 1363 1364 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", 1365 fb_helper_conn->connector->base.id); 1366 1367 /* got for command line mode first */ 1368 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 1369 if (!modes[i]) { 1370 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n", 1371 fb_helper_conn->connector->base.id); 1372 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); 1373 } 1374 /* No preferred modes, pick one off the list */ 1375 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { 1376 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) 1377 break; 1378 } 1379 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : 1380 "none"); 1381 } 1382 return true; 1383 } 1384 1385 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, 1386 struct drm_fb_helper_crtc **best_crtcs, 1387 struct drm_display_mode **modes, 1388 int n, int width, int height) 1389 { 1390 int c, o; 1391 struct drm_device *dev = fb_helper->dev; 1392 struct drm_connector *connector; 1393 struct drm_connector_helper_funcs *connector_funcs; 1394 struct drm_encoder *encoder; 1395 struct drm_fb_helper_crtc *best_crtc; 1396 int my_score, best_score, score; 1397 struct drm_fb_helper_crtc **crtcs, *crtc; 1398 struct drm_fb_helper_connector *fb_helper_conn; 1399 1400 if (n == fb_helper->connector_count) 1401 return 0; 1402 1403 fb_helper_conn = fb_helper->connector_info[n]; 1404 connector = fb_helper_conn->connector; 1405 1406 best_crtcs[n] = NULL; 1407 best_crtc = NULL; 1408 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); 1409 if (modes[n] == NULL) 1410 return best_score; 1411 1412 crtcs = malloc(dev->mode_config.num_connector * 1413 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS, 1414 M_WAITOK | M_ZERO); 1415 1416 my_score = 1; 1417 if (connector->status == connector_status_connected) 1418 my_score++; 1419 if (drm_has_cmdline_mode(fb_helper_conn)) 1420 my_score++; 1421 if (drm_has_preferred_mode(fb_helper_conn, width, height)) 1422 my_score++; 1423 1424 connector_funcs = connector->helper_private; 1425 encoder = connector_funcs->best_encoder(connector); 1426 if (!encoder) 1427 goto out; 1428 1429 /* select a crtc for this connector and then attempt to configure 1430 remaining connectors */ 1431 for (c = 0; c < fb_helper->crtc_count; c++) { 1432 crtc = &fb_helper->crtc_info[c]; 1433 1434 if ((encoder->possible_crtcs & (1 << c)) == 0) { 1435 continue; 1436 } 1437 1438 for (o = 0; o < n; o++) 1439 if (best_crtcs[o] == crtc) 1440 break; 1441 1442 if (o < n) { 1443 /* ignore cloning unless only a single crtc */ 1444 if (fb_helper->crtc_count > 1) 1445 continue; 1446 1447 if (!drm_mode_equal(modes[o], modes[n])) 1448 continue; 1449 } 1450 1451 crtcs[n] = crtc; 1452 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); 1453 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, 1454 width, height); 1455 if (score > best_score) { 1456 best_crtc = crtc; 1457 best_score = score; 1458 memcpy(best_crtcs, crtcs, 1459 dev->mode_config.num_connector * 1460 sizeof(struct drm_fb_helper_crtc *)); 1461 } 1462 } 1463 out: 1464 free(crtcs, DRM_MEM_KMS); 1465 return best_score; 1466 } 1467 1468 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) 1469 { 1470 struct drm_device *dev = fb_helper->dev; 1471 struct drm_fb_helper_crtc **crtcs; 1472 struct drm_display_mode **modes; 1473 struct drm_encoder *encoder; 1474 struct drm_mode_set *modeset; 1475 bool *enabled; 1476 int width, height; 1477 int i, ret; 1478 1479 DRM_DEBUG_KMS("\n"); 1480 1481 width = dev->mode_config.max_width; 1482 height = dev->mode_config.max_height; 1483 1484 /* clean out all the encoder/crtc combos */ 1485 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 1486 encoder->crtc = NULL; 1487 } 1488 1489 crtcs = malloc(dev->mode_config.num_connector * 1490 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS, 1491 M_WAITOK | M_ZERO); 1492 modes = malloc(dev->mode_config.num_connector * 1493 sizeof(struct drm_display_mode *), DRM_MEM_KMS, 1494 M_WAITOK | M_ZERO); 1495 enabled = malloc(dev->mode_config.num_connector * 1496 sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO); 1497 1498 drm_enable_connectors(fb_helper, enabled); 1499 1500 ret = drm_target_cloned(fb_helper, modes, enabled, width, height); 1501 if (!ret) { 1502 ret = drm_target_preferred(fb_helper, modes, enabled, width, height); 1503 if (!ret) 1504 DRM_ERROR("Unable to find initial modes\n"); 1505 } 1506 1507 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height); 1508 1509 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); 1510 1511 /* need to set the modesets up here for use later */ 1512 /* fill out the connector<->crtc mappings into the modesets */ 1513 for (i = 0; i < fb_helper->crtc_count; i++) { 1514 modeset = &fb_helper->crtc_info[i].mode_set; 1515 modeset->num_connectors = 0; 1516 } 1517 1518 for (i = 0; i < fb_helper->connector_count; i++) { 1519 struct drm_display_mode *mode = modes[i]; 1520 struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; 1521 modeset = &fb_crtc->mode_set; 1522 1523 if (mode && fb_crtc) { 1524 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n", 1525 mode->name, fb_crtc->mode_set.crtc->base.id); 1526 fb_crtc->desired_mode = mode; 1527 if (modeset->mode) 1528 drm_mode_destroy(dev, modeset->mode); 1529 modeset->mode = drm_mode_duplicate(dev, 1530 fb_crtc->desired_mode); 1531 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector; 1532 } 1533 } 1534 1535 free(crtcs, DRM_MEM_KMS); 1536 free(modes, DRM_MEM_KMS); 1537 free(enabled, DRM_MEM_KMS); 1538 } 1539 1540 /** 1541 * drm_helper_initial_config - setup a sane initial connector configuration 1542 * @dev: DRM device 1543 * 1544 * LOCKING: 1545 * Called at init time, must take mode config lock. 1546 * 1547 * Scan the CRTCs and connectors and try to put together an initial setup. 1548 * At the moment, this is a cloned configuration across all heads with 1549 * a new framebuffer object as the backing store. 1550 * 1551 * RETURNS: 1552 * Zero if everything went ok, nonzero otherwise. 1553 */ 1554 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 1555 { 1556 struct drm_device *dev = fb_helper->dev; 1557 int count = 0; 1558 1559 /* disable all the possible outputs/crtcs before entering KMS mode */ 1560 drm_helper_disable_unused_functions(fb_helper->dev); 1561 1562 drm_fb_helper_parse_command_line(fb_helper); 1563 1564 count = drm_fb_helper_probe_connector_modes(fb_helper, 1565 dev->mode_config.max_width, 1566 dev->mode_config.max_height); 1567 /* 1568 * we shouldn't end up with no modes here. 1569 */ 1570 if (count == 0) { 1571 printf("No connectors reported connected with modes\n"); 1572 } 1573 drm_setup_crtcs(fb_helper); 1574 1575 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 1576 } 1577 1578 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 1579 { 1580 struct drm_device *dev = fb_helper->dev; 1581 int count = 0; 1582 u32 max_width, max_height, bpp_sel; 1583 bool bound = false, crtcs_bound = false; 1584 struct drm_crtc *crtc; 1585 1586 if (!fb_helper->fb) 1587 return 0; 1588 1589 sx_xlock(&dev->mode_config.mutex); 1590 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 1591 if (crtc->fb) 1592 crtcs_bound = true; 1593 if (crtc->fb == fb_helper->fb) 1594 bound = true; 1595 } 1596 1597 if (!bound && crtcs_bound) { 1598 fb_helper->delayed_hotplug = true; 1599 sx_xunlock(&dev->mode_config.mutex); 1600 return 0; 1601 } 1602 DRM_DEBUG_KMS("\n"); 1603 1604 max_width = fb_helper->fb->width; 1605 max_height = fb_helper->fb->height; 1606 bpp_sel = fb_helper->fb->bits_per_pixel; 1607 1608 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width, 1609 max_height); 1610 drm_setup_crtcs(fb_helper); 1611 sx_xunlock(&dev->mode_config.mutex); 1612 1613 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 1614 } 1615 1616