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