1 /* 2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd 3 * Author:Mark Yao <mark.yao@rock-chips.com> 4 * 5 * based on exynos_drm_drv.c 6 * 7 * This software is licensed under the terms of the GNU General Public 8 * License version 2, as published by the Free Software Foundation, and 9 * may be copied, distributed, and modified under those terms. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 */ 16 17 #include <asm/dma-iommu.h> 18 19 #include <drm/drmP.h> 20 #include <drm/drm_crtc_helper.h> 21 #include <drm/drm_fb_helper.h> 22 #include <linux/dma-mapping.h> 23 #include <linux/pm_runtime.h> 24 #include <linux/module.h> 25 #include <linux/of_graph.h> 26 #include <linux/component.h> 27 28 #include "rockchip_drm_drv.h" 29 #include "rockchip_drm_fb.h" 30 #include "rockchip_drm_fbdev.h" 31 #include "rockchip_drm_gem.h" 32 33 #define DRIVER_NAME "rockchip" 34 #define DRIVER_DESC "RockChip Soc DRM" 35 #define DRIVER_DATE "20140818" 36 #define DRIVER_MAJOR 1 37 #define DRIVER_MINOR 0 38 39 static bool is_support_iommu = true; 40 41 /* 42 * Attach a (component) device to the shared drm dma mapping from master drm 43 * device. This is used by the VOPs to map GEM buffers to a common DMA 44 * mapping. 45 */ 46 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev, 47 struct device *dev) 48 { 49 struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping; 50 int ret; 51 52 if (!is_support_iommu) 53 return 0; 54 55 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); 56 if (ret) 57 return ret; 58 59 dma_set_max_seg_size(dev, DMA_BIT_MASK(32)); 60 61 return arm_iommu_attach_device(dev, mapping); 62 } 63 64 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev, 65 struct device *dev) 66 { 67 if (!is_support_iommu) 68 return; 69 70 arm_iommu_detach_device(dev); 71 } 72 73 int rockchip_register_crtc_funcs(struct drm_crtc *crtc, 74 const struct rockchip_crtc_funcs *crtc_funcs) 75 { 76 int pipe = drm_crtc_index(crtc); 77 struct rockchip_drm_private *priv = crtc->dev->dev_private; 78 79 if (pipe > ROCKCHIP_MAX_CRTC) 80 return -EINVAL; 81 82 priv->crtc_funcs[pipe] = crtc_funcs; 83 84 return 0; 85 } 86 87 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc) 88 { 89 int pipe = drm_crtc_index(crtc); 90 struct rockchip_drm_private *priv = crtc->dev->dev_private; 91 92 if (pipe > ROCKCHIP_MAX_CRTC) 93 return; 94 95 priv->crtc_funcs[pipe] = NULL; 96 } 97 98 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm, 99 int pipe) 100 { 101 struct drm_crtc *crtc; 102 int i = 0; 103 104 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) 105 if (i++ == pipe) 106 return crtc; 107 108 return NULL; 109 } 110 111 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev, 112 unsigned int pipe) 113 { 114 struct rockchip_drm_private *priv = dev->dev_private; 115 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe); 116 117 if (crtc && priv->crtc_funcs[pipe] && 118 priv->crtc_funcs[pipe]->enable_vblank) 119 return priv->crtc_funcs[pipe]->enable_vblank(crtc); 120 121 return 0; 122 } 123 124 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev, 125 unsigned int pipe) 126 { 127 struct rockchip_drm_private *priv = dev->dev_private; 128 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe); 129 130 if (crtc && priv->crtc_funcs[pipe] && 131 priv->crtc_funcs[pipe]->enable_vblank) 132 priv->crtc_funcs[pipe]->disable_vblank(crtc); 133 } 134 135 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags) 136 { 137 struct rockchip_drm_private *private; 138 struct dma_iommu_mapping *mapping = NULL; 139 struct device *dev = drm_dev->dev; 140 struct drm_connector *connector; 141 int ret; 142 143 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL); 144 if (!private) 145 return -ENOMEM; 146 147 mutex_init(&private->commit.lock); 148 INIT_WORK(&private->commit.work, rockchip_drm_atomic_work); 149 150 drm_dev->dev_private = private; 151 152 drm_mode_config_init(drm_dev); 153 154 rockchip_drm_mode_config_init(drm_dev); 155 156 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms), 157 GFP_KERNEL); 158 if (!dev->dma_parms) { 159 ret = -ENOMEM; 160 goto err_config_cleanup; 161 } 162 163 if (is_support_iommu) { 164 /* TODO(djkurtz): fetch the mapping start/size from somewhere */ 165 mapping = arm_iommu_create_mapping(&platform_bus_type, 166 0x00000000, 167 SZ_2G); 168 if (IS_ERR(mapping)) { 169 ret = PTR_ERR(mapping); 170 goto err_config_cleanup; 171 } 172 173 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); 174 if (ret) 175 goto err_release_mapping; 176 177 dma_set_max_seg_size(dev, DMA_BIT_MASK(32)); 178 179 ret = arm_iommu_attach_device(dev, mapping); 180 if (ret) 181 goto err_release_mapping; 182 } 183 184 /* Try to bind all sub drivers. */ 185 ret = component_bind_all(dev, drm_dev); 186 if (ret) 187 goto err_detach_device; 188 189 /* 190 * All components are now added, we can publish the connector sysfs 191 * entries to userspace. This will generate hotplug events and so 192 * userspace will expect to be able to access DRM at this point. 193 */ 194 list_for_each_entry(connector, &drm_dev->mode_config.connector_list, 195 head) { 196 ret = drm_connector_register(connector); 197 if (ret) { 198 dev_err(drm_dev->dev, 199 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n", 200 connector->base.id, 201 connector->name, ret); 202 goto err_unbind; 203 } 204 } 205 206 /* init kms poll for handling hpd */ 207 drm_kms_helper_poll_init(drm_dev); 208 209 /* 210 * enable drm irq mode. 211 * - with irq_enabled = true, we can use the vblank feature. 212 */ 213 drm_dev->irq_enabled = true; 214 215 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC); 216 if (ret) 217 goto err_kms_helper_poll_fini; 218 219 drm_mode_config_reset(drm_dev); 220 221 ret = rockchip_drm_fbdev_init(drm_dev); 222 if (ret) 223 goto err_vblank_cleanup; 224 225 if (is_support_iommu) 226 arm_iommu_release_mapping(mapping); 227 return 0; 228 err_vblank_cleanup: 229 drm_vblank_cleanup(drm_dev); 230 err_kms_helper_poll_fini: 231 drm_kms_helper_poll_fini(drm_dev); 232 err_unbind: 233 component_unbind_all(dev, drm_dev); 234 err_detach_device: 235 if (is_support_iommu) 236 arm_iommu_detach_device(dev); 237 err_release_mapping: 238 if (is_support_iommu) 239 arm_iommu_release_mapping(mapping); 240 err_config_cleanup: 241 drm_mode_config_cleanup(drm_dev); 242 drm_dev->dev_private = NULL; 243 return ret; 244 } 245 246 static int rockchip_drm_unload(struct drm_device *drm_dev) 247 { 248 struct device *dev = drm_dev->dev; 249 250 rockchip_drm_fbdev_fini(drm_dev); 251 drm_vblank_cleanup(drm_dev); 252 drm_kms_helper_poll_fini(drm_dev); 253 component_unbind_all(dev, drm_dev); 254 if (is_support_iommu) 255 arm_iommu_detach_device(dev); 256 drm_mode_config_cleanup(drm_dev); 257 drm_dev->dev_private = NULL; 258 259 return 0; 260 } 261 262 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc, 263 struct drm_file *file_priv) 264 { 265 struct rockchip_drm_private *priv = crtc->dev->dev_private; 266 int pipe = drm_crtc_index(crtc); 267 268 if (pipe < ROCKCHIP_MAX_CRTC && 269 priv->crtc_funcs[pipe] && 270 priv->crtc_funcs[pipe]->cancel_pending_vblank) 271 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv); 272 } 273 274 static void rockchip_drm_preclose(struct drm_device *dev, 275 struct drm_file *file_priv) 276 { 277 struct drm_crtc *crtc; 278 279 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) 280 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv); 281 } 282 283 void rockchip_drm_lastclose(struct drm_device *dev) 284 { 285 struct rockchip_drm_private *priv = dev->dev_private; 286 287 drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper); 288 } 289 290 static const struct file_operations rockchip_drm_driver_fops = { 291 .owner = THIS_MODULE, 292 .open = drm_open, 293 .mmap = rockchip_gem_mmap, 294 .poll = drm_poll, 295 .read = drm_read, 296 .unlocked_ioctl = drm_ioctl, 297 #ifdef CONFIG_COMPAT 298 .compat_ioctl = drm_compat_ioctl, 299 #endif 300 .release = drm_release, 301 }; 302 303 const struct vm_operations_struct rockchip_drm_vm_ops = { 304 .open = drm_gem_vm_open, 305 .close = drm_gem_vm_close, 306 }; 307 308 static struct drm_driver rockchip_drm_driver = { 309 .driver_features = DRIVER_MODESET | DRIVER_GEM | 310 DRIVER_PRIME | DRIVER_ATOMIC, 311 .load = rockchip_drm_load, 312 .unload = rockchip_drm_unload, 313 .preclose = rockchip_drm_preclose, 314 .lastclose = rockchip_drm_lastclose, 315 .get_vblank_counter = drm_vblank_no_hw_counter, 316 .enable_vblank = rockchip_drm_crtc_enable_vblank, 317 .disable_vblank = rockchip_drm_crtc_disable_vblank, 318 .gem_vm_ops = &rockchip_drm_vm_ops, 319 .gem_free_object = rockchip_gem_free_object, 320 .dumb_create = rockchip_gem_dumb_create, 321 .dumb_map_offset = rockchip_gem_dumb_map_offset, 322 .dumb_destroy = drm_gem_dumb_destroy, 323 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 324 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 325 .gem_prime_import = drm_gem_prime_import, 326 .gem_prime_export = drm_gem_prime_export, 327 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table, 328 .gem_prime_vmap = rockchip_gem_prime_vmap, 329 .gem_prime_vunmap = rockchip_gem_prime_vunmap, 330 .gem_prime_mmap = rockchip_gem_mmap_buf, 331 .fops = &rockchip_drm_driver_fops, 332 .name = DRIVER_NAME, 333 .desc = DRIVER_DESC, 334 .date = DRIVER_DATE, 335 .major = DRIVER_MAJOR, 336 .minor = DRIVER_MINOR, 337 }; 338 339 #ifdef CONFIG_PM_SLEEP 340 static int rockchip_drm_sys_suspend(struct device *dev) 341 { 342 struct drm_device *drm = dev_get_drvdata(dev); 343 struct drm_connector *connector; 344 345 if (!drm) 346 return 0; 347 348 drm_modeset_lock_all(drm); 349 list_for_each_entry(connector, &drm->mode_config.connector_list, head) { 350 int old_dpms = connector->dpms; 351 352 if (connector->funcs->dpms) 353 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF); 354 355 /* Set the old mode back to the connector for resume */ 356 connector->dpms = old_dpms; 357 } 358 drm_modeset_unlock_all(drm); 359 360 return 0; 361 } 362 363 static int rockchip_drm_sys_resume(struct device *dev) 364 { 365 struct drm_device *drm = dev_get_drvdata(dev); 366 struct drm_connector *connector; 367 enum drm_connector_status status; 368 bool changed = false; 369 370 if (!drm) 371 return 0; 372 373 drm_modeset_lock_all(drm); 374 list_for_each_entry(connector, &drm->mode_config.connector_list, head) { 375 int desired_mode = connector->dpms; 376 377 /* 378 * at suspend time, we save dpms to connector->dpms, 379 * restore the old_dpms, and at current time, the connector 380 * dpms status must be DRM_MODE_DPMS_OFF. 381 */ 382 connector->dpms = DRM_MODE_DPMS_OFF; 383 384 /* 385 * If the connector has been disconnected during suspend, 386 * disconnect it from the encoder and leave it off. We'll notify 387 * userspace at the end. 388 */ 389 if (desired_mode == DRM_MODE_DPMS_ON) { 390 status = connector->funcs->detect(connector, true); 391 if (status == connector_status_disconnected) { 392 connector->encoder = NULL; 393 connector->status = status; 394 changed = true; 395 continue; 396 } 397 } 398 if (connector->funcs->dpms) 399 connector->funcs->dpms(connector, desired_mode); 400 } 401 drm_modeset_unlock_all(drm); 402 403 drm_helper_resume_force_mode(drm); 404 405 if (changed) 406 drm_kms_helper_hotplug_event(drm); 407 408 return 0; 409 } 410 #endif 411 412 static const struct dev_pm_ops rockchip_drm_pm_ops = { 413 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend, 414 rockchip_drm_sys_resume) 415 }; 416 417 static int compare_of(struct device *dev, void *data) 418 { 419 struct device_node *np = data; 420 421 return dev->of_node == np; 422 } 423 424 static void rockchip_add_endpoints(struct device *dev, 425 struct component_match **match, 426 struct device_node *port) 427 { 428 struct device_node *ep, *remote; 429 430 for_each_child_of_node(port, ep) { 431 remote = of_graph_get_remote_port_parent(ep); 432 if (!remote || !of_device_is_available(remote)) { 433 of_node_put(remote); 434 continue; 435 } else if (!of_device_is_available(remote->parent)) { 436 dev_warn(dev, "parent device of %s is not available\n", 437 remote->full_name); 438 of_node_put(remote); 439 continue; 440 } 441 442 component_match_add(dev, match, compare_of, remote); 443 of_node_put(remote); 444 } 445 } 446 447 static int rockchip_drm_bind(struct device *dev) 448 { 449 struct drm_device *drm; 450 int ret; 451 452 drm = drm_dev_alloc(&rockchip_drm_driver, dev); 453 if (!drm) 454 return -ENOMEM; 455 456 ret = drm_dev_register(drm, 0); 457 if (ret) 458 goto err_free; 459 460 dev_set_drvdata(dev, drm); 461 462 return 0; 463 464 err_free: 465 drm_dev_unref(drm); 466 return ret; 467 } 468 469 static void rockchip_drm_unbind(struct device *dev) 470 { 471 struct drm_device *drm = dev_get_drvdata(dev); 472 473 drm_dev_unregister(drm); 474 drm_dev_unref(drm); 475 dev_set_drvdata(dev, NULL); 476 } 477 478 static const struct component_master_ops rockchip_drm_ops = { 479 .bind = rockchip_drm_bind, 480 .unbind = rockchip_drm_unbind, 481 }; 482 483 static int rockchip_drm_platform_probe(struct platform_device *pdev) 484 { 485 struct device *dev = &pdev->dev; 486 struct component_match *match = NULL; 487 struct device_node *np = dev->of_node; 488 struct device_node *port; 489 int i; 490 491 if (!np) 492 return -ENODEV; 493 /* 494 * Bind the crtc ports first, so that 495 * drm_of_find_possible_crtcs called from encoder .bind callbacks 496 * works as expected. 497 */ 498 for (i = 0;; i++) { 499 struct device_node *iommu; 500 501 port = of_parse_phandle(np, "ports", i); 502 if (!port) 503 break; 504 505 if (!of_device_is_available(port->parent)) { 506 of_node_put(port); 507 continue; 508 } 509 510 iommu = of_parse_phandle(port->parent, "iommus", 0); 511 if (!iommu || !of_device_is_available(iommu->parent)) { 512 dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n", 513 port->parent->full_name); 514 /* 515 * if there is a crtc not support iommu, force set all 516 * crtc use non-iommu buffer. 517 */ 518 is_support_iommu = false; 519 } 520 521 component_match_add(dev, &match, compare_of, port->parent); 522 of_node_put(port); 523 } 524 525 if (i == 0) { 526 dev_err(dev, "missing 'ports' property\n"); 527 return -ENODEV; 528 } 529 530 if (!match) { 531 dev_err(dev, "No available vop found for display-subsystem.\n"); 532 return -ENODEV; 533 } 534 /* 535 * For each bound crtc, bind the encoders attached to its 536 * remote endpoint. 537 */ 538 for (i = 0;; i++) { 539 port = of_parse_phandle(np, "ports", i); 540 if (!port) 541 break; 542 543 if (!of_device_is_available(port->parent)) { 544 of_node_put(port); 545 continue; 546 } 547 548 rockchip_add_endpoints(dev, &match, port); 549 of_node_put(port); 550 } 551 552 return component_master_add_with_match(dev, &rockchip_drm_ops, match); 553 } 554 555 static int rockchip_drm_platform_remove(struct platform_device *pdev) 556 { 557 component_master_del(&pdev->dev, &rockchip_drm_ops); 558 559 return 0; 560 } 561 562 static const struct of_device_id rockchip_drm_dt_ids[] = { 563 { .compatible = "rockchip,display-subsystem", }, 564 { /* sentinel */ }, 565 }; 566 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids); 567 568 static struct platform_driver rockchip_drm_platform_driver = { 569 .probe = rockchip_drm_platform_probe, 570 .remove = rockchip_drm_platform_remove, 571 .driver = { 572 .name = "rockchip-drm", 573 .of_match_table = rockchip_drm_dt_ids, 574 .pm = &rockchip_drm_pm_ops, 575 }, 576 }; 577 578 module_platform_driver(rockchip_drm_platform_driver); 579 580 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>"); 581 MODULE_DESCRIPTION("ROCKCHIP DRM Driver"); 582 MODULE_LICENSE("GPL v2"); 583