CHROMIUM: drm/rockchip: Add GEM create ioctl support
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_drv.c
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 <linux/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 <drm/rockchip_drm.h>
29
30 #include "rockchip_drm_drv.h"
31 #include "rockchip_drm_fb.h"
32 #include "rockchip_drm_fbdev.h"
33 #include "rockchip_drm_gem.h"
34
35 #define DRIVER_NAME     "rockchip"
36 #define DRIVER_DESC     "RockChip Soc DRM"
37 #define DRIVER_DATE     "20140818"
38 #define DRIVER_MAJOR    1
39 #define DRIVER_MINOR    0
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 rockchip_drm_private *private = drm_dev->dev_private;
50         struct iommu_domain *domain = private->domain;
51         int ret;
52
53         ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
54         if (ret)
55                 return ret;
56
57         dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
58         ret = iommu_attach_device(domain, dev);
59         if (ret) {
60                 dev_err(dev, "Failed to attach iommu device\n");
61                 return ret;
62         }
63
64         if (!common_iommu_setup_dma_ops(dev, 0x10000000, SZ_2G, domain->ops)) {
65                 dev_err(dev, "Failed to set dma_ops\n");
66                 iommu_detach_device(domain, dev);
67                 ret = -ENODEV;
68         }
69
70         return ret;
71 }
72
73 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
74                                     struct device *dev)
75 {
76         struct rockchip_drm_private *private = drm_dev->dev_private;
77         struct iommu_domain *domain = private->domain;
78
79         iommu_detach_device(domain, dev);
80 }
81
82 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
83                                  const struct rockchip_crtc_funcs *crtc_funcs)
84 {
85         int pipe = drm_crtc_index(crtc);
86         struct rockchip_drm_private *priv = crtc->dev->dev_private;
87
88         if (pipe > ROCKCHIP_MAX_CRTC)
89                 return -EINVAL;
90
91         priv->crtc_funcs[pipe] = crtc_funcs;
92
93         return 0;
94 }
95
96 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
97 {
98         int pipe = drm_crtc_index(crtc);
99         struct rockchip_drm_private *priv = crtc->dev->dev_private;
100
101         if (pipe > ROCKCHIP_MAX_CRTC)
102                 return;
103
104         priv->crtc_funcs[pipe] = NULL;
105 }
106
107 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
108                                                 int pipe)
109 {
110         struct drm_crtc *crtc;
111         int i = 0;
112
113         list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
114                 if (i++ == pipe)
115                         return crtc;
116
117         return NULL;
118 }
119
120 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
121                                            unsigned int pipe)
122 {
123         struct rockchip_drm_private *priv = dev->dev_private;
124         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
125
126         if (crtc && priv->crtc_funcs[pipe] &&
127             priv->crtc_funcs[pipe]->enable_vblank)
128                 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
129
130         return 0;
131 }
132
133 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
134                                              unsigned int pipe)
135 {
136         struct rockchip_drm_private *priv = dev->dev_private;
137         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
138
139         if (crtc && priv->crtc_funcs[pipe] &&
140             priv->crtc_funcs[pipe]->enable_vblank)
141                 priv->crtc_funcs[pipe]->disable_vblank(crtc);
142 }
143
144 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
145 {
146         struct rockchip_drm_private *private;
147         struct device *dev = drm_dev->dev;
148         struct drm_connector *connector;
149         struct iommu_group *group;
150         int ret;
151
152         private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
153         if (!private)
154                 return -ENOMEM;
155
156         mutex_init(&private->commit.lock);
157         INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
158
159         drm_dev->dev_private = private;
160
161         drm_mode_config_init(drm_dev);
162
163         rockchip_drm_mode_config_init(drm_dev);
164
165         dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
166                                       GFP_KERNEL);
167         if (!dev->dma_parms) {
168                 ret = -ENOMEM;
169                 goto err_config_cleanup;
170         }
171
172         private->domain = iommu_domain_alloc(&platform_bus_type);
173         if (!private->domain)
174                 return -ENOMEM;
175
176         ret = iommu_get_dma_cookie(private->domain);
177         if (ret)
178                 goto err_free_domain;
179
180         group = iommu_group_get(dev);
181         if (!group) {
182                 group = iommu_group_alloc();
183                 if (IS_ERR(group)) {
184                         dev_err(dev, "Failed to allocate IOMMU group\n");
185                         goto err_put_cookie;
186                 }
187
188                 ret = iommu_group_add_device(group, dev);
189                 iommu_group_put(group);
190                 if (ret) {
191                         dev_err(dev, "failed to add device to IOMMU group\n");
192                         goto err_put_cookie;
193                 }
194         }
195         /*
196          * Attach virtual iommu device, sub iommu device can share the same
197          * mapping with it.
198          */
199         ret = rockchip_drm_dma_attach_device(drm_dev, dev);
200         if (ret)
201                 goto err_group_remove_device;
202
203         /* Try to bind all sub drivers. */
204         ret = component_bind_all(dev, drm_dev);
205         if (ret)
206                 goto err_detach_device;
207
208         /*
209          * All components are now added, we can publish the connector sysfs
210          * entries to userspace.  This will generate hotplug events and so
211          * userspace will expect to be able to access DRM at this point.
212          */
213         list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
214                         head) {
215                 ret = drm_connector_register(connector);
216                 if (ret) {
217                         dev_err(drm_dev->dev,
218                                 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
219                                 connector->base.id,
220                                 connector->name, ret);
221                         goto err_unbind;
222                 }
223         }
224
225         /* init kms poll for handling hpd */
226         drm_kms_helper_poll_init(drm_dev);
227
228         /*
229          * enable drm irq mode.
230          * - with irq_enabled = true, we can use the vblank feature.
231          */
232         drm_dev->irq_enabled = true;
233
234         ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
235         if (ret)
236                 goto err_kms_helper_poll_fini;
237
238         /*
239          * with vblank_disable_allowed = true, vblank interrupt will be disabled
240          * by drm timer once a current process gives up ownership of
241          * vblank event.(after drm_vblank_put function is called)
242          */
243         drm_dev->vblank_disable_allowed = true;
244
245         drm_mode_config_reset(drm_dev);
246
247         ret = rockchip_drm_fbdev_init(drm_dev);
248         if (ret)
249                 goto err_vblank_cleanup;
250
251         return 0;
252 err_vblank_cleanup:
253         drm_vblank_cleanup(drm_dev);
254 err_kms_helper_poll_fini:
255         drm_kms_helper_poll_fini(drm_dev);
256 err_unbind:
257         component_unbind_all(dev, drm_dev);
258 err_detach_device:
259         rockchip_drm_dma_detach_device(drm_dev, dev);
260 err_group_remove_device:
261         iommu_group_remove_device(dev);
262 err_put_cookie:
263         iommu_put_dma_cookie(private->domain);
264 err_free_domain:
265         iommu_domain_free(private->domain);
266 err_config_cleanup:
267         drm_mode_config_cleanup(drm_dev);
268         drm_dev->dev_private = NULL;
269         return ret;
270 }
271
272 static int rockchip_drm_unload(struct drm_device *drm_dev)
273 {
274         struct device *dev = drm_dev->dev;
275         struct rockchip_drm_private *private = drm_dev->dev_private;
276
277         rockchip_drm_fbdev_fini(drm_dev);
278         drm_vblank_cleanup(drm_dev);
279         drm_kms_helper_poll_fini(drm_dev);
280         component_unbind_all(dev, drm_dev);
281         rockchip_drm_dma_detach_device(drm_dev, dev);
282         iommu_group_remove_device(dev);
283         iommu_put_dma_cookie(private->domain);
284         iommu_domain_free(private->domain);
285         drm_mode_config_cleanup(drm_dev);
286         drm_dev->dev_private = NULL;
287
288         return 0;
289 }
290
291 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
292                                                     struct drm_file *file_priv)
293 {
294         struct rockchip_drm_private *priv = crtc->dev->dev_private;
295         int pipe = drm_crtc_index(crtc);
296
297         if (pipe < ROCKCHIP_MAX_CRTC &&
298             priv->crtc_funcs[pipe] &&
299             priv->crtc_funcs[pipe]->cancel_pending_vblank)
300                 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
301 }
302
303 static void rockchip_drm_preclose(struct drm_device *dev,
304                                   struct drm_file *file_priv)
305 {
306         struct drm_crtc *crtc;
307
308         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
309                 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
310 }
311
312 void rockchip_drm_lastclose(struct drm_device *dev)
313 {
314         struct rockchip_drm_private *priv = dev->dev_private;
315
316         drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
317 }
318
319 static const struct drm_ioctl_desc rockchip_ioctls[] = {
320         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
321                           DRM_UNLOCKED | DRM_AUTH),
322         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
323                           rockchip_gem_map_offset_ioctl,
324                           DRM_UNLOCKED | DRM_AUTH),
325 };
326
327 static const struct file_operations rockchip_drm_driver_fops = {
328         .owner = THIS_MODULE,
329         .open = drm_open,
330         .mmap = rockchip_gem_mmap,
331         .poll = drm_poll,
332         .read = drm_read,
333         .unlocked_ioctl = drm_ioctl,
334 #ifdef CONFIG_COMPAT
335         .compat_ioctl = drm_compat_ioctl,
336 #endif
337         .release = drm_release,
338 };
339
340 const struct vm_operations_struct rockchip_drm_vm_ops = {
341         .open = drm_gem_vm_open,
342         .close = drm_gem_vm_close,
343 };
344
345 static struct drm_driver rockchip_drm_driver = {
346         .driver_features        = DRIVER_MODESET | DRIVER_GEM |
347                                   DRIVER_PRIME | DRIVER_ATOMIC,
348         .load                   = rockchip_drm_load,
349         .unload                 = rockchip_drm_unload,
350         .preclose               = rockchip_drm_preclose,
351         .lastclose              = rockchip_drm_lastclose,
352         .get_vblank_counter     = drm_vblank_no_hw_counter,
353         .enable_vblank          = rockchip_drm_crtc_enable_vblank,
354         .disable_vblank         = rockchip_drm_crtc_disable_vblank,
355         .gem_vm_ops             = &rockchip_drm_vm_ops,
356         .gem_free_object        = rockchip_gem_free_object,
357         .dumb_create            = rockchip_gem_dumb_create,
358         .dumb_map_offset        = rockchip_gem_dumb_map_offset,
359         .dumb_destroy           = drm_gem_dumb_destroy,
360         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
361         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
362         .gem_prime_import       = drm_gem_prime_import,
363         .gem_prime_export       = drm_gem_prime_export,
364         .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
365         .gem_prime_vmap         = rockchip_gem_prime_vmap,
366         .gem_prime_vunmap       = rockchip_gem_prime_vunmap,
367         .gem_prime_mmap         = rockchip_gem_mmap_buf,
368         .ioctls                 = rockchip_ioctls,
369         .num_ioctls             = ARRAY_SIZE(rockchip_ioctls),
370         .fops                   = &rockchip_drm_driver_fops,
371         .name   = DRIVER_NAME,
372         .desc   = DRIVER_DESC,
373         .date   = DRIVER_DATE,
374         .major  = DRIVER_MAJOR,
375         .minor  = DRIVER_MINOR,
376 };
377
378 #ifdef CONFIG_PM_SLEEP
379 static int rockchip_drm_sys_suspend(struct device *dev)
380 {
381         struct drm_device *drm = dev_get_drvdata(dev);
382         struct drm_connector *connector;
383
384         if (!drm)
385                 return 0;
386
387         drm_modeset_lock_all(drm);
388         list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
389                 int old_dpms = connector->dpms;
390
391                 if (connector->funcs->dpms)
392                         connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
393
394                 /* Set the old mode back to the connector for resume */
395                 connector->dpms = old_dpms;
396         }
397         drm_modeset_unlock_all(drm);
398
399         return 0;
400 }
401
402 static int rockchip_drm_sys_resume(struct device *dev)
403 {
404         struct drm_device *drm = dev_get_drvdata(dev);
405         struct drm_connector *connector;
406         enum drm_connector_status status;
407         bool changed = false;
408
409         if (!drm)
410                 return 0;
411
412         drm_modeset_lock_all(drm);
413         list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
414                 int desired_mode = connector->dpms;
415
416                 /*
417                  * at suspend time, we save dpms to connector->dpms,
418                  * restore the old_dpms, and at current time, the connector
419                  * dpms status must be DRM_MODE_DPMS_OFF.
420                  */
421                 connector->dpms = DRM_MODE_DPMS_OFF;
422
423                 /*
424                  * If the connector has been disconnected during suspend,
425                  * disconnect it from the encoder and leave it off. We'll notify
426                  * userspace at the end.
427                  */
428                 if (desired_mode == DRM_MODE_DPMS_ON) {
429                         status = connector->funcs->detect(connector, true);
430                         if (status == connector_status_disconnected) {
431                                 connector->encoder = NULL;
432                                 connector->status = status;
433                                 changed = true;
434                                 continue;
435                         }
436                 }
437                 if (connector->funcs->dpms)
438                         connector->funcs->dpms(connector, desired_mode);
439         }
440         drm_modeset_unlock_all(drm);
441
442         drm_helper_resume_force_mode(drm);
443
444         if (changed)
445                 drm_kms_helper_hotplug_event(drm);
446
447         return 0;
448 }
449 #endif
450
451 static const struct dev_pm_ops rockchip_drm_pm_ops = {
452         SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
453                                 rockchip_drm_sys_resume)
454 };
455
456 /*
457  * @node: device tree node containing encoder input ports
458  * @encoder: drm_encoder
459  */
460 int rockchip_drm_encoder_get_mux_id(struct device_node *node,
461                                     struct drm_encoder *encoder)
462 {
463         struct device_node *ep;
464         struct drm_crtc *crtc = encoder->crtc;
465         struct of_endpoint endpoint;
466         struct device_node *port;
467         int ret;
468
469         if (!node || !crtc)
470                 return -EINVAL;
471
472         for_each_endpoint_of_node(node, ep) {
473                 port = of_graph_get_remote_port(ep);
474                 of_node_put(port);
475                 if (port == crtc->port) {
476                         ret = of_graph_parse_endpoint(ep, &endpoint);
477                         of_node_put(ep);
478                         return ret ?: endpoint.id;
479                 }
480         }
481
482         return -EINVAL;
483 }
484 EXPORT_SYMBOL_GPL(rockchip_drm_encoder_get_mux_id);
485
486 static int compare_of(struct device *dev, void *data)
487 {
488         struct device_node *np = data;
489
490         return dev->of_node == np;
491 }
492
493 static void rockchip_add_endpoints(struct device *dev,
494                                    struct component_match **match,
495                                    struct device_node *port)
496 {
497         struct device_node *ep, *remote;
498
499         for_each_child_of_node(port, ep) {
500                 remote = of_graph_get_remote_port_parent(ep);
501                 if (!remote || !of_device_is_available(remote)) {
502                         of_node_put(remote);
503                         continue;
504                 } else if (!of_device_is_available(remote->parent)) {
505                         dev_warn(dev, "parent device of %s is not available\n",
506                                  remote->full_name);
507                         of_node_put(remote);
508                         continue;
509                 }
510
511                 component_match_add(dev, match, compare_of, remote);
512                 of_node_put(remote);
513         }
514 }
515
516 static int rockchip_drm_bind(struct device *dev)
517 {
518         struct drm_device *drm;
519         int ret;
520
521         drm = drm_dev_alloc(&rockchip_drm_driver, dev);
522         if (!drm)
523                 return -ENOMEM;
524
525         ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
526         if (ret)
527                 goto err_free;
528
529         ret = drm_dev_register(drm, 0);
530         if (ret)
531                 goto err_free;
532
533         dev_set_drvdata(dev, drm);
534
535         return 0;
536
537 err_free:
538         drm_dev_unref(drm);
539         return ret;
540 }
541
542 static void rockchip_drm_unbind(struct device *dev)
543 {
544         struct drm_device *drm = dev_get_drvdata(dev);
545
546         drm_dev_unregister(drm);
547         drm_dev_unref(drm);
548         dev_set_drvdata(dev, NULL);
549 }
550
551 static const struct component_master_ops rockchip_drm_ops = {
552         .bind = rockchip_drm_bind,
553         .unbind = rockchip_drm_unbind,
554 };
555
556 static int rockchip_drm_platform_probe(struct platform_device *pdev)
557 {
558         struct device *dev = &pdev->dev;
559         struct component_match *match = NULL;
560         struct device_node *np = dev->of_node;
561         struct device_node *port;
562         int i;
563
564         if (!np)
565                 return -ENODEV;
566         /*
567          * Bind the crtc ports first, so that
568          * drm_of_find_possible_crtcs called from encoder .bind callbacks
569          * works as expected.
570          */
571         for (i = 0;; i++) {
572                 port = of_parse_phandle(np, "ports", i);
573                 if (!port)
574                         break;
575
576                 if (!of_device_is_available(port->parent)) {
577                         of_node_put(port);
578                         continue;
579                 }
580
581                 component_match_add(dev, &match, compare_of, port->parent);
582                 of_node_put(port);
583         }
584
585         if (i == 0) {
586                 dev_err(dev, "missing 'ports' property\n");
587                 return -ENODEV;
588         }
589
590         if (!match) {
591                 dev_err(dev, "No available vop found for display-subsystem.\n");
592                 return -ENODEV;
593         }
594         /*
595          * For each bound crtc, bind the encoders attached to its
596          * remote endpoint.
597          */
598         for (i = 0;; i++) {
599                 port = of_parse_phandle(np, "ports", i);
600                 if (!port)
601                         break;
602
603                 if (!of_device_is_available(port->parent)) {
604                         of_node_put(port);
605                         continue;
606                 }
607
608                 rockchip_add_endpoints(dev, &match, port);
609                 of_node_put(port);
610         }
611
612         return component_master_add_with_match(dev, &rockchip_drm_ops, match);
613 }
614
615 static int rockchip_drm_platform_remove(struct platform_device *pdev)
616 {
617         component_master_del(&pdev->dev, &rockchip_drm_ops);
618
619         return 0;
620 }
621
622 static const struct of_device_id rockchip_drm_dt_ids[] = {
623         { .compatible = "rockchip,display-subsystem", },
624         { /* sentinel */ },
625 };
626 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
627
628 static struct platform_driver rockchip_drm_platform_driver = {
629         .probe = rockchip_drm_platform_probe,
630         .remove = rockchip_drm_platform_remove,
631         .driver = {
632                 .name = "rockchip-drm",
633                 .of_match_table = rockchip_drm_dt_ids,
634                 .pm = &rockchip_drm_pm_ops,
635         },
636 };
637
638 module_platform_driver(rockchip_drm_platform_driver);
639
640 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
641 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
642 MODULE_LICENSE("GPL v2");