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 vm_fault_t psbfb_vm_fault(struct vm_fault *vmf) 80 { 81 struct vm_area_struct *vma = vmf->vma; 82 struct drm_framebuffer *fb = vma->vm_private_data; 83 struct drm_device *dev = fb->dev; 84 struct drm_psb_private *dev_priv = dev->dev_private; 85 struct gtt_range *gtt = to_gtt_range(fb->obj[0]); 86 int page_num; 87 int i; 88 unsigned long address; 89 vm_fault_t ret = VM_FAULT_SIGBUS; 90 unsigned long pfn; 91 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base + 92 gtt->offset; 93 94 page_num = vma_pages(vma); 95 address = vmf->address - (vmf->pgoff << PAGE_SHIFT); 96 97 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 98 99 for (i = 0; i < page_num; i++) { 100 pfn = (phys_addr >> PAGE_SHIFT); 101 102 ret = vmf_insert_mixed(vma, address, 103 __pfn_to_pfn_t(pfn, PFN_DEV)); 104 if (unlikely(ret & VM_FAULT_ERROR)) 105 break; 106 address += PAGE_SIZE; 107 phys_addr += PAGE_SIZE; 108 } 109 return ret; 110 } 111 112 static void psbfb_vm_open(struct vm_area_struct *vma) 113 { 114 } 115 116 static void psbfb_vm_close(struct vm_area_struct *vma) 117 { 118 } 119 120 static const struct vm_operations_struct psbfb_vm_ops = { 121 .fault = psbfb_vm_fault, 122 .open = psbfb_vm_open, 123 .close = psbfb_vm_close 124 }; 125 126 static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma) 127 { 128 struct drm_fb_helper *fb_helper = info->par; 129 struct drm_framebuffer *fb = fb_helper->fb; 130 131 if (vma->vm_pgoff != 0) 132 return -EINVAL; 133 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) 134 return -EINVAL; 135 136 /* 137 * If this is a GEM object then info->screen_base is the virtual 138 * kernel remapping of the object. FIXME: Review if this is 139 * suitable for our mmap work 140 */ 141 vma->vm_ops = &psbfb_vm_ops; 142 vma->vm_private_data = (void *)fb; 143 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP; 144 return 0; 145 } 146 147 static const struct fb_ops psbfb_unaccel_ops = { 148 .owner = THIS_MODULE, 149 DRM_FB_HELPER_DEFAULT_OPS, 150 .fb_setcolreg = psbfb_setcolreg, 151 .fb_fillrect = drm_fb_helper_cfb_fillrect, 152 .fb_copyarea = drm_fb_helper_cfb_copyarea, 153 .fb_imageblit = drm_fb_helper_cfb_imageblit, 154 .fb_mmap = psbfb_mmap, 155 }; 156 157 /** 158 * psb_framebuffer_init - initialize a framebuffer 159 * @dev: our DRM device 160 * @fb: framebuffer to set up 161 * @mode_cmd: mode description 162 * @obj: backing object 163 * 164 * Configure and fill in the boilerplate for our frame buffer. Return 165 * 0 on success or an error code if we fail. 166 */ 167 static int psb_framebuffer_init(struct drm_device *dev, 168 struct drm_framebuffer *fb, 169 const struct drm_mode_fb_cmd2 *mode_cmd, 170 struct drm_gem_object *obj) 171 { 172 const struct drm_format_info *info; 173 int ret; 174 175 /* 176 * Reject unknown formats, YUV formats, and formats with more than 177 * 4 bytes per pixel. 178 */ 179 info = drm_get_format_info(dev, mode_cmd); 180 if (!info || !info->depth || info->cpp[0] > 4) 181 return -EINVAL; 182 183 if (mode_cmd->pitches[0] & 63) 184 return -EINVAL; 185 186 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd); 187 fb->obj[0] = obj; 188 ret = drm_framebuffer_init(dev, fb, &psb_fb_funcs); 189 if (ret) { 190 dev_err(dev->dev, "framebuffer init failed: %d\n", ret); 191 return ret; 192 } 193 return 0; 194 } 195 196 /** 197 * psb_framebuffer_create - create a framebuffer backed by gt 198 * @dev: our DRM device 199 * @mode_cmd: the description of the requested mode 200 * @obj: the backing object 201 * 202 * Create a framebuffer object backed by the gt, and fill in the 203 * boilerplate required 204 * 205 * TODO: review object references 206 */ 207 208 static struct drm_framebuffer *psb_framebuffer_create 209 (struct drm_device *dev, 210 const struct drm_mode_fb_cmd2 *mode_cmd, 211 struct drm_gem_object *obj) 212 { 213 struct drm_framebuffer *fb; 214 int ret; 215 216 fb = kzalloc(sizeof(*fb), GFP_KERNEL); 217 if (!fb) 218 return ERR_PTR(-ENOMEM); 219 220 ret = psb_framebuffer_init(dev, fb, mode_cmd, obj); 221 if (ret) { 222 kfree(fb); 223 return ERR_PTR(ret); 224 } 225 return fb; 226 } 227 228 /** 229 * psbfb_alloc - allocate frame buffer memory 230 * @dev: the DRM device 231 * @aligned_size: space needed 232 * 233 * Allocate the frame buffer. In the usual case we get a GTT range that 234 * is stolen memory backed and life is simple. If there isn't sufficient 235 * we fail as we don't have the virtual mapping space to really vmap it 236 * and the kernel console code can't handle non linear framebuffers. 237 * 238 * Re-address this as and if the framebuffer layer grows this ability. 239 */ 240 static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size) 241 { 242 struct gtt_range *backing; 243 /* Begin by trying to use stolen memory backing */ 244 backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE); 245 if (backing) { 246 backing->gem.funcs = &psb_gem_object_funcs; 247 drm_gem_private_object_init(dev, &backing->gem, aligned_size); 248 return backing; 249 } 250 return NULL; 251 } 252 253 /** 254 * psbfb_create - create a framebuffer 255 * @fb_helper: the framebuffer helper 256 * @sizes: specification of the layout 257 * 258 * Create a framebuffer to the specifications provided 259 */ 260 static int psbfb_create(struct drm_fb_helper *fb_helper, 261 struct drm_fb_helper_surface_size *sizes) 262 { 263 struct drm_device *dev = fb_helper->dev; 264 struct drm_psb_private *dev_priv = dev->dev_private; 265 struct pci_dev *pdev = to_pci_dev(dev->dev); 266 struct fb_info *info; 267 struct drm_framebuffer *fb; 268 struct drm_mode_fb_cmd2 mode_cmd; 269 int size; 270 int ret; 271 struct gtt_range *backing; 272 u32 bpp, depth; 273 274 mode_cmd.width = sizes->surface_width; 275 mode_cmd.height = sizes->surface_height; 276 bpp = sizes->surface_bpp; 277 depth = sizes->surface_depth; 278 279 /* No 24bit packed */ 280 if (bpp == 24) 281 bpp = 32; 282 283 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * DIV_ROUND_UP(bpp, 8), 64); 284 285 size = mode_cmd.pitches[0] * mode_cmd.height; 286 size = ALIGN(size, PAGE_SIZE); 287 288 /* Allocate the framebuffer in the GTT with stolen page backing */ 289 backing = psbfb_alloc(dev, size); 290 if (backing == NULL) 291 return -ENOMEM; 292 293 memset(dev_priv->vram_addr + backing->offset, 0, size); 294 295 info = drm_fb_helper_alloc_fbi(fb_helper); 296 if (IS_ERR(info)) { 297 ret = PTR_ERR(info); 298 goto out; 299 } 300 301 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth); 302 303 fb = psb_framebuffer_create(dev, &mode_cmd, &backing->gem); 304 if (IS_ERR(fb)) { 305 ret = PTR_ERR(fb); 306 goto out; 307 } 308 309 fb_helper->fb = fb; 310 311 info->fbops = &psbfb_unaccel_ops; 312 313 info->fix.smem_start = dev->mode_config.fb_base; 314 info->fix.smem_len = size; 315 info->fix.ywrapstep = 0; 316 info->fix.ypanstep = 0; 317 318 /* Accessed stolen memory directly */ 319 info->screen_base = dev_priv->vram_addr + backing->offset; 320 info->screen_size = size; 321 322 if (dev_priv->gtt.stolen_size) { 323 info->apertures->ranges[0].base = dev->mode_config.fb_base; 324 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size; 325 } 326 327 drm_fb_helper_fill_info(info, fb_helper, sizes); 328 329 info->fix.mmio_start = pci_resource_start(pdev, 0); 330 info->fix.mmio_len = pci_resource_len(pdev, 0); 331 332 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 333 334 dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height); 335 336 return 0; 337 out: 338 psb_gtt_free_range(dev, backing); 339 return ret; 340 } 341 342 /** 343 * psb_user_framebuffer_create - create framebuffer 344 * @dev: our DRM device 345 * @filp: client file 346 * @cmd: mode request 347 * 348 * Create a new framebuffer backed by a userspace GEM object 349 */ 350 static struct drm_framebuffer *psb_user_framebuffer_create 351 (struct drm_device *dev, struct drm_file *filp, 352 const struct drm_mode_fb_cmd2 *cmd) 353 { 354 struct drm_gem_object *obj; 355 356 /* 357 * Find the GEM object and thus the gtt range object that is 358 * to back this space 359 */ 360 obj = drm_gem_object_lookup(filp, cmd->handles[0]); 361 if (obj == NULL) 362 return ERR_PTR(-ENOENT); 363 364 /* Let the core code do all the work */ 365 return psb_framebuffer_create(dev, cmd, obj); 366 } 367 368 static int psbfb_probe(struct drm_fb_helper *fb_helper, 369 struct drm_fb_helper_surface_size *sizes) 370 { 371 struct drm_device *dev = fb_helper->dev; 372 struct drm_psb_private *dev_priv = dev->dev_private; 373 unsigned int fb_size; 374 int bytespp; 375 376 bytespp = sizes->surface_bpp / 8; 377 if (bytespp == 3) /* no 24bit packed */ 378 bytespp = 4; 379 380 /* If the mode will not fit in 32bit then switch to 16bit to get 381 a console on full resolution. The X mode setting server will 382 allocate its own 32bit GEM framebuffer */ 383 fb_size = ALIGN(sizes->surface_width * bytespp, 64) * 384 sizes->surface_height; 385 fb_size = ALIGN(fb_size, PAGE_SIZE); 386 387 if (fb_size > dev_priv->vram_stolen_size) { 388 sizes->surface_bpp = 16; 389 sizes->surface_depth = 16; 390 } 391 392 return psbfb_create(fb_helper, sizes); 393 } 394 395 static const struct drm_fb_helper_funcs psb_fb_helper_funcs = { 396 .fb_probe = psbfb_probe, 397 }; 398 399 static int psb_fbdev_destroy(struct drm_device *dev, 400 struct drm_fb_helper *fb_helper) 401 { 402 struct drm_framebuffer *fb = fb_helper->fb; 403 404 drm_fb_helper_unregister_fbi(fb_helper); 405 406 drm_fb_helper_fini(fb_helper); 407 drm_framebuffer_unregister_private(fb); 408 drm_framebuffer_cleanup(fb); 409 410 if (fb->obj[0]) 411 drm_gem_object_put(fb->obj[0]); 412 kfree(fb); 413 414 return 0; 415 } 416 417 int psb_fbdev_init(struct drm_device *dev) 418 { 419 struct drm_fb_helper *fb_helper; 420 struct drm_psb_private *dev_priv = dev->dev_private; 421 int ret; 422 423 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); 424 if (!fb_helper) { 425 dev_err(dev->dev, "no memory\n"); 426 return -ENOMEM; 427 } 428 429 dev_priv->fb_helper = fb_helper; 430 431 drm_fb_helper_prepare(dev, fb_helper, &psb_fb_helper_funcs); 432 433 ret = drm_fb_helper_init(dev, fb_helper); 434 if (ret) 435 goto free; 436 437 /* disable all the possible outputs/crtcs before entering KMS mode */ 438 drm_helper_disable_unused_functions(dev); 439 440 ret = drm_fb_helper_initial_config(fb_helper, 32); 441 if (ret) 442 goto fini; 443 444 return 0; 445 446 fini: 447 drm_fb_helper_fini(fb_helper); 448 free: 449 kfree(fb_helper); 450 return ret; 451 } 452 453 static void psb_fbdev_fini(struct drm_device *dev) 454 { 455 struct drm_psb_private *dev_priv = dev->dev_private; 456 457 if (!dev_priv->fb_helper) 458 return; 459 460 psb_fbdev_destroy(dev, dev_priv->fb_helper); 461 kfree(dev_priv->fb_helper); 462 dev_priv->fb_helper = NULL; 463 } 464 465 static const struct drm_mode_config_funcs psb_mode_funcs = { 466 .fb_create = psb_user_framebuffer_create, 467 .output_poll_changed = drm_fb_helper_output_poll_changed, 468 }; 469 470 static void psb_setup_outputs(struct drm_device *dev) 471 { 472 struct drm_psb_private *dev_priv = dev->dev_private; 473 struct drm_connector *connector; 474 475 drm_mode_create_scaling_mode_property(dev); 476 477 /* It is ok for this to fail - we just don't get backlight control */ 478 if (!dev_priv->backlight_property) 479 dev_priv->backlight_property = drm_property_create_range(dev, 0, 480 "backlight", 0, 100); 481 dev_priv->ops->output_init(dev); 482 483 list_for_each_entry(connector, &dev->mode_config.connector_list, 484 head) { 485 struct gma_encoder *gma_encoder = gma_attached_encoder(connector); 486 struct drm_encoder *encoder = &gma_encoder->base; 487 int crtc_mask = 0, clone_mask = 0; 488 489 /* valid crtcs */ 490 switch (gma_encoder->type) { 491 case INTEL_OUTPUT_ANALOG: 492 crtc_mask = (1 << 0); 493 clone_mask = (1 << INTEL_OUTPUT_ANALOG); 494 break; 495 case INTEL_OUTPUT_SDVO: 496 crtc_mask = dev_priv->ops->sdvo_mask; 497 clone_mask = 0; 498 break; 499 case INTEL_OUTPUT_LVDS: 500 crtc_mask = dev_priv->ops->lvds_mask; 501 clone_mask = 0; 502 break; 503 case INTEL_OUTPUT_MIPI: 504 crtc_mask = (1 << 0); 505 clone_mask = 0; 506 break; 507 case INTEL_OUTPUT_MIPI2: 508 crtc_mask = (1 << 2); 509 clone_mask = 0; 510 break; 511 case INTEL_OUTPUT_HDMI: 512 crtc_mask = dev_priv->ops->hdmi_mask; 513 clone_mask = (1 << INTEL_OUTPUT_HDMI); 514 break; 515 case INTEL_OUTPUT_DISPLAYPORT: 516 crtc_mask = (1 << 0) | (1 << 1); 517 clone_mask = 0; 518 break; 519 case INTEL_OUTPUT_EDP: 520 crtc_mask = (1 << 1); 521 clone_mask = 0; 522 } 523 encoder->possible_crtcs = crtc_mask; 524 encoder->possible_clones = 525 gma_connector_clones(dev, clone_mask); 526 } 527 } 528 529 void psb_modeset_init(struct drm_device *dev) 530 { 531 struct drm_psb_private *dev_priv = dev->dev_private; 532 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev; 533 struct pci_dev *pdev = to_pci_dev(dev->dev); 534 int i; 535 536 drm_mode_config_init(dev); 537 538 dev->mode_config.min_width = 0; 539 dev->mode_config.min_height = 0; 540 541 dev->mode_config.funcs = &psb_mode_funcs; 542 543 /* set memory base */ 544 /* Oaktrail and Poulsbo should use BAR 2*/ 545 pci_read_config_dword(pdev, PSB_BSM, (u32 *)&(dev->mode_config.fb_base)); 546 547 /* num pipes is 2 for PSB but 1 for Mrst */ 548 for (i = 0; i < dev_priv->num_pipe; i++) 549 psb_intel_crtc_init(dev, i, mode_dev); 550 551 dev->mode_config.max_width = 4096; 552 dev->mode_config.max_height = 4096; 553 554 psb_setup_outputs(dev); 555 556 if (dev_priv->ops->errata) 557 dev_priv->ops->errata(dev); 558 559 dev_priv->modeset = true; 560 } 561 562 void psb_modeset_cleanup(struct drm_device *dev) 563 { 564 struct drm_psb_private *dev_priv = dev->dev_private; 565 if (dev_priv->modeset) { 566 drm_kms_helper_poll_fini(dev); 567 psb_fbdev_fini(dev); 568 drm_mode_config_cleanup(dev); 569 } 570 } 571