1 // SPDX-License-Identifier: GPL-2.0-only 2 /************************************************************************** 3 * Copyright (c) 2007-2011, Intel Corporation. 4 * All Rights Reserved. 5 * 6 **************************************************************************/ 7 8 #include <linux/console.h> 9 #include <linux/delay.h> 10 #include <linux/errno.h> 11 #include <linux/init.h> 12 #include <linux/kernel.h> 13 #include <linux/mm.h> 14 #include <linux/module.h> 15 #include <linux/pfn_t.h> 16 #include <linux/slab.h> 17 #include <linux/string.h> 18 #include <linux/tty.h> 19 20 #include <drm/drm.h> 21 #include <drm/drm_crtc.h> 22 #include <drm/drm_fb_helper.h> 23 #include <drm/drm_fourcc.h> 24 #include <drm/drm_gem_framebuffer_helper.h> 25 26 #include "framebuffer.h" 27 #include "gem.h" 28 #include "gtt.h" 29 #include "psb_drv.h" 30 #include "psb_intel_drv.h" 31 #include "psb_intel_reg.h" 32 33 static const struct drm_framebuffer_funcs psb_fb_funcs = { 34 .destroy = drm_gem_fb_destroy, 35 .create_handle = drm_gem_fb_create_handle, 36 }; 37 38 #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16) 39 40 static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green, 41 unsigned blue, unsigned transp, 42 struct fb_info *info) 43 { 44 struct drm_fb_helper *fb_helper = info->par; 45 struct drm_framebuffer *fb = fb_helper->fb; 46 uint32_t v; 47 48 if (!fb) 49 return -ENOMEM; 50 51 if (regno > 255) 52 return 1; 53 54 red = CMAP_TOHW(red, info->var.red.length); 55 blue = CMAP_TOHW(blue, info->var.blue.length); 56 green = CMAP_TOHW(green, info->var.green.length); 57 transp = CMAP_TOHW(transp, info->var.transp.length); 58 59 v = (red << info->var.red.offset) | 60 (green << info->var.green.offset) | 61 (blue << info->var.blue.offset) | 62 (transp << info->var.transp.offset); 63 64 if (regno < 16) { 65 switch (fb->format->cpp[0] * 8) { 66 case 16: 67 ((uint32_t *) info->pseudo_palette)[regno] = v; 68 break; 69 case 24: 70 case 32: 71 ((uint32_t *) info->pseudo_palette)[regno] = v; 72 break; 73 } 74 } 75 76 return 0; 77 } 78 79 static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info) 80 { 81 struct drm_fb_helper *fb_helper = info->par; 82 struct drm_framebuffer *fb = fb_helper->fb; 83 struct drm_device *dev = fb->dev; 84 struct gtt_range *gtt = to_gtt_range(fb->obj[0]); 85 86 /* 87 * We have to poke our nose in here. The core fb code assumes 88 * panning is part of the hardware that can be invoked before 89 * the actual fb is mapped. In our case that isn't quite true. 90 */ 91 if (gtt->npage) { 92 /* GTT roll shifts in 4K pages, we need to shift the right 93 number of pages */ 94 int pages = info->fix.line_length >> 12; 95 psb_gtt_roll(dev, gtt, var->yoffset * pages); 96 } 97 return 0; 98 } 99 100 static vm_fault_t psbfb_vm_fault(struct vm_fault *vmf) 101 { 102 struct vm_area_struct *vma = vmf->vma; 103 struct drm_framebuffer *fb = vma->vm_private_data; 104 struct drm_device *dev = fb->dev; 105 struct drm_psb_private *dev_priv = dev->dev_private; 106 struct gtt_range *gtt = to_gtt_range(fb->obj[0]); 107 int page_num; 108 int i; 109 unsigned long address; 110 vm_fault_t ret = VM_FAULT_SIGBUS; 111 unsigned long pfn; 112 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base + 113 gtt->offset; 114 115 page_num = vma_pages(vma); 116 address = vmf->address - (vmf->pgoff << PAGE_SHIFT); 117 118 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 119 120 for (i = 0; i < page_num; i++) { 121 pfn = (phys_addr >> PAGE_SHIFT); 122 123 ret = vmf_insert_mixed(vma, address, 124 __pfn_to_pfn_t(pfn, PFN_DEV)); 125 if (unlikely(ret & VM_FAULT_ERROR)) 126 break; 127 address += PAGE_SIZE; 128 phys_addr += PAGE_SIZE; 129 } 130 return ret; 131 } 132 133 static void psbfb_vm_open(struct vm_area_struct *vma) 134 { 135 } 136 137 static void psbfb_vm_close(struct vm_area_struct *vma) 138 { 139 } 140 141 static const struct vm_operations_struct psbfb_vm_ops = { 142 .fault = psbfb_vm_fault, 143 .open = psbfb_vm_open, 144 .close = psbfb_vm_close 145 }; 146 147 static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma) 148 { 149 struct drm_fb_helper *fb_helper = info->par; 150 struct drm_framebuffer *fb = fb_helper->fb; 151 152 if (vma->vm_pgoff != 0) 153 return -EINVAL; 154 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) 155 return -EINVAL; 156 157 /* 158 * If this is a GEM object then info->screen_base is the virtual 159 * kernel remapping of the object. FIXME: Review if this is 160 * suitable for our mmap work 161 */ 162 vma->vm_ops = &psbfb_vm_ops; 163 vma->vm_private_data = (void *)fb; 164 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP; 165 return 0; 166 } 167 168 static const struct fb_ops psbfb_ops = { 169 .owner = THIS_MODULE, 170 DRM_FB_HELPER_DEFAULT_OPS, 171 .fb_setcolreg = psbfb_setcolreg, 172 .fb_fillrect = drm_fb_helper_cfb_fillrect, 173 .fb_copyarea = psbfb_copyarea, 174 .fb_imageblit = drm_fb_helper_cfb_imageblit, 175 .fb_mmap = psbfb_mmap, 176 .fb_sync = psbfb_sync, 177 }; 178 179 static const struct fb_ops psbfb_roll_ops = { 180 .owner = THIS_MODULE, 181 DRM_FB_HELPER_DEFAULT_OPS, 182 .fb_setcolreg = psbfb_setcolreg, 183 .fb_fillrect = drm_fb_helper_cfb_fillrect, 184 .fb_copyarea = drm_fb_helper_cfb_copyarea, 185 .fb_imageblit = drm_fb_helper_cfb_imageblit, 186 .fb_pan_display = psbfb_pan, 187 .fb_mmap = psbfb_mmap, 188 }; 189 190 static const struct fb_ops psbfb_unaccel_ops = { 191 .owner = THIS_MODULE, 192 DRM_FB_HELPER_DEFAULT_OPS, 193 .fb_setcolreg = psbfb_setcolreg, 194 .fb_fillrect = drm_fb_helper_cfb_fillrect, 195 .fb_copyarea = drm_fb_helper_cfb_copyarea, 196 .fb_imageblit = drm_fb_helper_cfb_imageblit, 197 .fb_mmap = psbfb_mmap, 198 }; 199 200 /** 201 * psb_framebuffer_init - initialize a framebuffer 202 * @dev: our DRM device 203 * @fb: framebuffer to set up 204 * @mode_cmd: mode description 205 * @gt: backing object 206 * 207 * Configure and fill in the boilerplate for our frame buffer. Return 208 * 0 on success or an error code if we fail. 209 */ 210 static int psb_framebuffer_init(struct drm_device *dev, 211 struct drm_framebuffer *fb, 212 const struct drm_mode_fb_cmd2 *mode_cmd, 213 struct drm_gem_object *obj) 214 { 215 const struct drm_format_info *info; 216 int ret; 217 218 /* 219 * Reject unknown formats, YUV formats, and formats with more than 220 * 4 bytes per pixel. 221 */ 222 info = drm_get_format_info(dev, mode_cmd); 223 if (!info || !info->depth || info->cpp[0] > 4) 224 return -EINVAL; 225 226 if (mode_cmd->pitches[0] & 63) 227 return -EINVAL; 228 229 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd); 230 fb->obj[0] = obj; 231 ret = drm_framebuffer_init(dev, fb, &psb_fb_funcs); 232 if (ret) { 233 dev_err(dev->dev, "framebuffer init failed: %d\n", ret); 234 return ret; 235 } 236 return 0; 237 } 238 239 /** 240 * psb_framebuffer_create - create a framebuffer backed by gt 241 * @dev: our DRM device 242 * @mode_cmd: the description of the requested mode 243 * @gt: the backing object 244 * 245 * Create a framebuffer object backed by the gt, and fill in the 246 * boilerplate required 247 * 248 * TODO: review object references 249 */ 250 251 static struct drm_framebuffer *psb_framebuffer_create 252 (struct drm_device *dev, 253 const struct drm_mode_fb_cmd2 *mode_cmd, 254 struct drm_gem_object *obj) 255 { 256 struct drm_framebuffer *fb; 257 int ret; 258 259 fb = kzalloc(sizeof(*fb), GFP_KERNEL); 260 if (!fb) 261 return ERR_PTR(-ENOMEM); 262 263 ret = psb_framebuffer_init(dev, fb, mode_cmd, obj); 264 if (ret) { 265 kfree(fb); 266 return ERR_PTR(ret); 267 } 268 return fb; 269 } 270 271 /** 272 * psbfb_alloc - allocate frame buffer memory 273 * @dev: the DRM device 274 * @aligned_size: space needed 275 * 276 * Allocate the frame buffer. In the usual case we get a GTT range that 277 * is stolen memory backed and life is simple. If there isn't sufficient 278 * we fail as we don't have the virtual mapping space to really vmap it 279 * and the kernel console code can't handle non linear framebuffers. 280 * 281 * Re-address this as and if the framebuffer layer grows this ability. 282 */ 283 static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size) 284 { 285 struct gtt_range *backing; 286 /* Begin by trying to use stolen memory backing */ 287 backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE); 288 if (backing) { 289 backing->gem.funcs = &psb_gem_object_funcs; 290 drm_gem_private_object_init(dev, &backing->gem, aligned_size); 291 return backing; 292 } 293 return NULL; 294 } 295 296 /** 297 * psbfb_create - create a framebuffer 298 * @fbdev: the framebuffer device 299 * @sizes: specification of the layout 300 * 301 * Create a framebuffer to the specifications provided 302 */ 303 static int psbfb_create(struct drm_fb_helper *fb_helper, 304 struct drm_fb_helper_surface_size *sizes) 305 { 306 struct drm_device *dev = fb_helper->dev; 307 struct drm_psb_private *dev_priv = dev->dev_private; 308 struct fb_info *info; 309 struct drm_framebuffer *fb; 310 struct drm_mode_fb_cmd2 mode_cmd; 311 int size; 312 int ret; 313 struct gtt_range *backing; 314 u32 bpp, depth; 315 int gtt_roll = 0; 316 int pitch_lines = 0; 317 318 mode_cmd.width = sizes->surface_width; 319 mode_cmd.height = sizes->surface_height; 320 bpp = sizes->surface_bpp; 321 depth = sizes->surface_depth; 322 323 /* No 24bit packed */ 324 if (bpp == 24) 325 bpp = 32; 326 327 do { 328 /* 329 * Acceleration via the GTT requires pitch to be 330 * power of two aligned. Preferably page but less 331 * is ok with some fonts 332 */ 333 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines); 334 335 size = mode_cmd.pitches[0] * mode_cmd.height; 336 size = ALIGN(size, PAGE_SIZE); 337 338 /* Allocate the fb in the GTT with stolen page backing */ 339 backing = psbfb_alloc(dev, size); 340 341 if (pitch_lines) 342 pitch_lines *= 2; 343 else 344 pitch_lines = 1; 345 gtt_roll++; 346 } while (backing == NULL && pitch_lines <= 16); 347 348 /* The final pitch we accepted if we succeeded */ 349 pitch_lines /= 2; 350 351 if (backing == NULL) { 352 /* 353 * We couldn't get the space we wanted, fall back to the 354 * display engine requirement instead. The HW requires 355 * the pitch to be 64 byte aligned 356 */ 357 358 gtt_roll = 0; /* Don't use GTT accelerated scrolling */ 359 pitch_lines = 64; 360 361 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64); 362 363 size = mode_cmd.pitches[0] * mode_cmd.height; 364 size = ALIGN(size, PAGE_SIZE); 365 366 /* Allocate the framebuffer in the GTT with stolen page backing */ 367 backing = psbfb_alloc(dev, size); 368 if (backing == NULL) 369 return -ENOMEM; 370 } 371 372 memset(dev_priv->vram_addr + backing->offset, 0, size); 373 374 info = drm_fb_helper_alloc_fbi(fb_helper); 375 if (IS_ERR(info)) { 376 ret = PTR_ERR(info); 377 goto out; 378 } 379 380 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth); 381 382 fb = psb_framebuffer_create(dev, &mode_cmd, &backing->gem); 383 if (IS_ERR(fb)) { 384 ret = PTR_ERR(fb); 385 goto out; 386 } 387 388 fb_helper->fb = fb; 389 390 if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */ 391 info->fbops = &psbfb_ops; 392 else if (gtt_roll) { /* GTT rolling seems best */ 393 info->fbops = &psbfb_roll_ops; 394 info->flags |= FBINFO_HWACCEL_YPAN; 395 } else /* Software */ 396 info->fbops = &psbfb_unaccel_ops; 397 398 info->fix.smem_start = dev->mode_config.fb_base; 399 info->fix.smem_len = size; 400 info->fix.ywrapstep = gtt_roll; 401 info->fix.ypanstep = 0; 402 403 /* Accessed stolen memory directly */ 404 info->screen_base = dev_priv->vram_addr + backing->offset; 405 info->screen_size = size; 406 407 if (dev_priv->gtt.stolen_size) { 408 info->apertures->ranges[0].base = dev->mode_config.fb_base; 409 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size; 410 } 411 412 drm_fb_helper_fill_info(info, fb_helper, sizes); 413 414 info->fix.mmio_start = pci_resource_start(dev->pdev, 0); 415 info->fix.mmio_len = pci_resource_len(dev->pdev, 0); 416 417 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 418 419 dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height); 420 421 return 0; 422 out: 423 psb_gtt_free_range(dev, backing); 424 return ret; 425 } 426 427 /** 428 * psb_user_framebuffer_create - create framebuffer 429 * @dev: our DRM device 430 * @filp: client file 431 * @cmd: mode request 432 * 433 * Create a new framebuffer backed by a userspace GEM object 434 */ 435 static struct drm_framebuffer *psb_user_framebuffer_create 436 (struct drm_device *dev, struct drm_file *filp, 437 const struct drm_mode_fb_cmd2 *cmd) 438 { 439 struct drm_gem_object *obj; 440 441 /* 442 * Find the GEM object and thus the gtt range object that is 443 * to back this space 444 */ 445 obj = drm_gem_object_lookup(filp, cmd->handles[0]); 446 if (obj == NULL) 447 return ERR_PTR(-ENOENT); 448 449 /* Let the core code do all the work */ 450 return psb_framebuffer_create(dev, cmd, obj); 451 } 452 453 static int psbfb_probe(struct drm_fb_helper *fb_helper, 454 struct drm_fb_helper_surface_size *sizes) 455 { 456 struct drm_device *dev = fb_helper->dev; 457 struct drm_psb_private *dev_priv = dev->dev_private; 458 unsigned int fb_size; 459 int bytespp; 460 461 bytespp = sizes->surface_bpp / 8; 462 if (bytespp == 3) /* no 24bit packed */ 463 bytespp = 4; 464 465 /* If the mode will not fit in 32bit then switch to 16bit to get 466 a console on full resolution. The X mode setting server will 467 allocate its own 32bit GEM framebuffer */ 468 fb_size = ALIGN(sizes->surface_width * bytespp, 64) * 469 sizes->surface_height; 470 fb_size = ALIGN(fb_size, PAGE_SIZE); 471 472 if (fb_size > dev_priv->vram_stolen_size) { 473 sizes->surface_bpp = 16; 474 sizes->surface_depth = 16; 475 } 476 477 return psbfb_create(fb_helper, sizes); 478 } 479 480 static const struct drm_fb_helper_funcs psb_fb_helper_funcs = { 481 .fb_probe = psbfb_probe, 482 }; 483 484 static int psb_fbdev_destroy(struct drm_device *dev, 485 struct drm_fb_helper *fb_helper) 486 { 487 struct drm_framebuffer *fb = fb_helper->fb; 488 489 drm_fb_helper_unregister_fbi(fb_helper); 490 491 drm_fb_helper_fini(fb_helper); 492 drm_framebuffer_unregister_private(fb); 493 drm_framebuffer_cleanup(fb); 494 495 if (fb->obj[0]) 496 drm_gem_object_put(fb->obj[0]); 497 kfree(fb); 498 499 return 0; 500 } 501 502 int psb_fbdev_init(struct drm_device *dev) 503 { 504 struct drm_fb_helper *fb_helper; 505 struct drm_psb_private *dev_priv = dev->dev_private; 506 int ret; 507 508 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); 509 if (!fb_helper) { 510 dev_err(dev->dev, "no memory\n"); 511 return -ENOMEM; 512 } 513 514 dev_priv->fb_helper = fb_helper; 515 516 drm_fb_helper_prepare(dev, fb_helper, &psb_fb_helper_funcs); 517 518 ret = drm_fb_helper_init(dev, fb_helper); 519 if (ret) 520 goto free; 521 522 /* disable all the possible outputs/crtcs before entering KMS mode */ 523 drm_helper_disable_unused_functions(dev); 524 525 ret = drm_fb_helper_initial_config(fb_helper, 32); 526 if (ret) 527 goto fini; 528 529 return 0; 530 531 fini: 532 drm_fb_helper_fini(fb_helper); 533 free: 534 kfree(fb_helper); 535 return ret; 536 } 537 538 static void psb_fbdev_fini(struct drm_device *dev) 539 { 540 struct drm_psb_private *dev_priv = dev->dev_private; 541 542 if (!dev_priv->fb_helper) 543 return; 544 545 psb_fbdev_destroy(dev, dev_priv->fb_helper); 546 kfree(dev_priv->fb_helper); 547 dev_priv->fb_helper = NULL; 548 } 549 550 static const struct drm_mode_config_funcs psb_mode_funcs = { 551 .fb_create = psb_user_framebuffer_create, 552 .output_poll_changed = drm_fb_helper_output_poll_changed, 553 }; 554 555 static void psb_setup_outputs(struct drm_device *dev) 556 { 557 struct drm_psb_private *dev_priv = dev->dev_private; 558 struct drm_connector *connector; 559 560 drm_mode_create_scaling_mode_property(dev); 561 562 /* It is ok for this to fail - we just don't get backlight control */ 563 if (!dev_priv->backlight_property) 564 dev_priv->backlight_property = drm_property_create_range(dev, 0, 565 "backlight", 0, 100); 566 dev_priv->ops->output_init(dev); 567 568 list_for_each_entry(connector, &dev->mode_config.connector_list, 569 head) { 570 struct gma_encoder *gma_encoder = gma_attached_encoder(connector); 571 struct drm_encoder *encoder = &gma_encoder->base; 572 int crtc_mask = 0, clone_mask = 0; 573 574 /* valid crtcs */ 575 switch (gma_encoder->type) { 576 case INTEL_OUTPUT_ANALOG: 577 crtc_mask = (1 << 0); 578 clone_mask = (1 << INTEL_OUTPUT_ANALOG); 579 break; 580 case INTEL_OUTPUT_SDVO: 581 crtc_mask = dev_priv->ops->sdvo_mask; 582 clone_mask = 0; 583 break; 584 case INTEL_OUTPUT_LVDS: 585 crtc_mask = dev_priv->ops->lvds_mask; 586 clone_mask = 0; 587 break; 588 case INTEL_OUTPUT_MIPI: 589 crtc_mask = (1 << 0); 590 clone_mask = 0; 591 break; 592 case INTEL_OUTPUT_MIPI2: 593 crtc_mask = (1 << 2); 594 clone_mask = 0; 595 break; 596 case INTEL_OUTPUT_HDMI: 597 crtc_mask = dev_priv->ops->hdmi_mask; 598 clone_mask = (1 << INTEL_OUTPUT_HDMI); 599 break; 600 case INTEL_OUTPUT_DISPLAYPORT: 601 crtc_mask = (1 << 0) | (1 << 1); 602 clone_mask = 0; 603 break; 604 case INTEL_OUTPUT_EDP: 605 crtc_mask = (1 << 1); 606 clone_mask = 0; 607 } 608 encoder->possible_crtcs = crtc_mask; 609 encoder->possible_clones = 610 gma_connector_clones(dev, clone_mask); 611 } 612 } 613 614 void psb_modeset_init(struct drm_device *dev) 615 { 616 struct drm_psb_private *dev_priv = dev->dev_private; 617 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev; 618 int i; 619 620 drm_mode_config_init(dev); 621 622 dev->mode_config.min_width = 0; 623 dev->mode_config.min_height = 0; 624 625 dev->mode_config.funcs = &psb_mode_funcs; 626 627 /* set memory base */ 628 /* Oaktrail and Poulsbo should use BAR 2*/ 629 pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *) 630 &(dev->mode_config.fb_base)); 631 632 /* num pipes is 2 for PSB but 1 for Mrst */ 633 for (i = 0; i < dev_priv->num_pipe; i++) 634 psb_intel_crtc_init(dev, i, mode_dev); 635 636 dev->mode_config.max_width = 4096; 637 dev->mode_config.max_height = 4096; 638 639 psb_setup_outputs(dev); 640 641 if (dev_priv->ops->errata) 642 dev_priv->ops->errata(dev); 643 644 dev_priv->modeset = true; 645 } 646 647 void psb_modeset_cleanup(struct drm_device *dev) 648 { 649 struct drm_psb_private *dev_priv = dev->dev_private; 650 if (dev_priv->modeset) { 651 drm_kms_helper_poll_fini(dev); 652 psb_fbdev_fini(dev); 653 drm_mode_config_cleanup(dev); 654 } 655 } 656