drm: define drm_compat_ioctl NULL on CONFIG_COMPAT=n and reduce #ifdefs
[sfrench/cifs-2.6.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 <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 <drm/drm_gem_cma_helper.h>
23 #include <drm/drm_of.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/module.h>
27 #include <linux/of_graph.h>
28 #include <linux/component.h>
29 #include <linux/console.h>
30
31 #include "rockchip_drm_drv.h"
32 #include "rockchip_drm_fb.h"
33 #include "rockchip_drm_fbdev.h"
34 #include "rockchip_drm_gem.h"
35
36 #define DRIVER_NAME     "rockchip"
37 #define DRIVER_DESC     "RockChip Soc DRM"
38 #define DRIVER_DATE     "20140818"
39 #define DRIVER_MAJOR    1
40 #define DRIVER_MINOR    0
41
42 static bool is_support_iommu = true;
43 static struct drm_driver rockchip_drm_driver;
44
45 /*
46  * Attach a (component) device to the shared drm dma mapping from master drm
47  * device.  This is used by the VOPs to map GEM buffers to a common DMA
48  * mapping.
49  */
50 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
51                                    struct device *dev)
52 {
53         struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
54         int ret;
55
56         if (!is_support_iommu)
57                 return 0;
58
59         ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
60         if (ret)
61                 return ret;
62
63         dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
64
65         return arm_iommu_attach_device(dev, mapping);
66 }
67
68 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
69                                     struct device *dev)
70 {
71         if (!is_support_iommu)
72                 return;
73
74         arm_iommu_detach_device(dev);
75 }
76
77 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
78                                  const struct rockchip_crtc_funcs *crtc_funcs)
79 {
80         int pipe = drm_crtc_index(crtc);
81         struct rockchip_drm_private *priv = crtc->dev->dev_private;
82
83         if (pipe >= ROCKCHIP_MAX_CRTC)
84                 return -EINVAL;
85
86         priv->crtc_funcs[pipe] = crtc_funcs;
87
88         return 0;
89 }
90
91 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
92 {
93         int pipe = drm_crtc_index(crtc);
94         struct rockchip_drm_private *priv = crtc->dev->dev_private;
95
96         if (pipe >= ROCKCHIP_MAX_CRTC)
97                 return;
98
99         priv->crtc_funcs[pipe] = NULL;
100 }
101
102 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
103                                                 int pipe)
104 {
105         struct drm_crtc *crtc;
106         int i = 0;
107
108         list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
109                 if (i++ == pipe)
110                         return crtc;
111
112         return NULL;
113 }
114
115 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
116                                            unsigned int pipe)
117 {
118         struct rockchip_drm_private *priv = dev->dev_private;
119         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
120
121         if (crtc && priv->crtc_funcs[pipe] &&
122             priv->crtc_funcs[pipe]->enable_vblank)
123                 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
124
125         return 0;
126 }
127
128 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
129                                              unsigned int pipe)
130 {
131         struct rockchip_drm_private *priv = dev->dev_private;
132         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
133
134         if (crtc && priv->crtc_funcs[pipe] &&
135             priv->crtc_funcs[pipe]->enable_vblank)
136                 priv->crtc_funcs[pipe]->disable_vblank(crtc);
137 }
138
139 static int rockchip_drm_bind(struct device *dev)
140 {
141         struct drm_device *drm_dev;
142         struct rockchip_drm_private *private;
143         struct dma_iommu_mapping *mapping = NULL;
144         int ret;
145
146         drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
147         if (IS_ERR(drm_dev))
148                 return PTR_ERR(drm_dev);
149
150         dev_set_drvdata(dev, drm_dev);
151
152         private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
153         if (!private) {
154                 ret = -ENOMEM;
155                 goto err_free;
156         }
157
158         drm_dev->dev_private = private;
159
160         INIT_LIST_HEAD(&private->psr_list);
161         spin_lock_init(&private->psr_list_lock);
162
163         drm_mode_config_init(drm_dev);
164
165         rockchip_drm_mode_config_init(drm_dev);
166
167         dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
168                                       GFP_KERNEL);
169         if (!dev->dma_parms) {
170                 ret = -ENOMEM;
171                 goto err_config_cleanup;
172         }
173
174         if (is_support_iommu) {
175                 /* TODO(djkurtz): fetch the mapping start/size from somewhere */
176                 mapping = arm_iommu_create_mapping(&platform_bus_type,
177                                                    0x00000000,
178                                                    SZ_2G);
179                 if (IS_ERR(mapping)) {
180                         ret = PTR_ERR(mapping);
181                         goto err_config_cleanup;
182                 }
183
184                 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
185                 if (ret)
186                         goto err_release_mapping;
187
188                 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
189
190                 ret = arm_iommu_attach_device(dev, mapping);
191                 if (ret)
192                         goto err_release_mapping;
193         }
194
195         /* Try to bind all sub drivers. */
196         ret = component_bind_all(dev, drm_dev);
197         if (ret)
198                 goto err_detach_device;
199
200         /* init kms poll for handling hpd */
201         drm_kms_helper_poll_init(drm_dev);
202
203         /*
204          * enable drm irq mode.
205          * - with irq_enabled = true, we can use the vblank feature.
206          */
207         drm_dev->irq_enabled = true;
208
209         ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
210         if (ret)
211                 goto err_kms_helper_poll_fini;
212
213         drm_mode_config_reset(drm_dev);
214
215         ret = rockchip_drm_fbdev_init(drm_dev);
216         if (ret)
217                 goto err_vblank_cleanup;
218
219         ret = drm_dev_register(drm_dev, 0);
220         if (ret)
221                 goto err_fbdev_fini;
222
223         if (is_support_iommu)
224                 arm_iommu_release_mapping(mapping);
225         return 0;
226 err_fbdev_fini:
227         rockchip_drm_fbdev_fini(drm_dev);
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         component_unbind_all(dev, drm_dev);
233 err_detach_device:
234         if (is_support_iommu)
235                 arm_iommu_detach_device(dev);
236 err_release_mapping:
237         if (is_support_iommu)
238                 arm_iommu_release_mapping(mapping);
239 err_config_cleanup:
240         drm_mode_config_cleanup(drm_dev);
241         drm_dev->dev_private = NULL;
242 err_free:
243         drm_dev_unref(drm_dev);
244         return ret;
245 }
246
247 static void rockchip_drm_unbind(struct device *dev)
248 {
249         struct drm_device *drm_dev = dev_get_drvdata(dev);
250
251         rockchip_drm_fbdev_fini(drm_dev);
252         drm_vblank_cleanup(drm_dev);
253         drm_kms_helper_poll_fini(drm_dev);
254         component_unbind_all(dev, drm_dev);
255         if (is_support_iommu)
256                 arm_iommu_detach_device(dev);
257         drm_mode_config_cleanup(drm_dev);
258         drm_dev->dev_private = NULL;
259         drm_dev_unregister(drm_dev);
260         drm_dev_unref(drm_dev);
261         dev_set_drvdata(dev, NULL);
262 }
263
264 static void rockchip_drm_lastclose(struct drm_device *dev)
265 {
266         struct rockchip_drm_private *priv = dev->dev_private;
267
268         drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
269 }
270
271 static const struct file_operations rockchip_drm_driver_fops = {
272         .owner = THIS_MODULE,
273         .open = drm_open,
274         .mmap = rockchip_gem_mmap,
275         .poll = drm_poll,
276         .read = drm_read,
277         .unlocked_ioctl = drm_ioctl,
278         .compat_ioctl = drm_compat_ioctl,
279         .release = drm_release,
280 };
281
282 static struct drm_driver rockchip_drm_driver = {
283         .driver_features        = DRIVER_MODESET | DRIVER_GEM |
284                                   DRIVER_PRIME | DRIVER_ATOMIC,
285         .lastclose              = rockchip_drm_lastclose,
286         .get_vblank_counter     = drm_vblank_no_hw_counter,
287         .enable_vblank          = rockchip_drm_crtc_enable_vblank,
288         .disable_vblank         = rockchip_drm_crtc_disable_vblank,
289         .gem_vm_ops             = &drm_gem_cma_vm_ops,
290         .gem_free_object_unlocked = rockchip_gem_free_object,
291         .dumb_create            = rockchip_gem_dumb_create,
292         .dumb_map_offset        = rockchip_gem_dumb_map_offset,
293         .dumb_destroy           = drm_gem_dumb_destroy,
294         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
295         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
296         .gem_prime_import       = drm_gem_prime_import,
297         .gem_prime_export       = drm_gem_prime_export,
298         .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
299         .gem_prime_vmap         = rockchip_gem_prime_vmap,
300         .gem_prime_vunmap       = rockchip_gem_prime_vunmap,
301         .gem_prime_mmap         = rockchip_gem_mmap_buf,
302         .fops                   = &rockchip_drm_driver_fops,
303         .name   = DRIVER_NAME,
304         .desc   = DRIVER_DESC,
305         .date   = DRIVER_DATE,
306         .major  = DRIVER_MAJOR,
307         .minor  = DRIVER_MINOR,
308 };
309
310 #ifdef CONFIG_PM_SLEEP
311 static void rockchip_drm_fb_suspend(struct drm_device *drm)
312 {
313         struct rockchip_drm_private *priv = drm->dev_private;
314
315         console_lock();
316         drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
317         console_unlock();
318 }
319
320 static void rockchip_drm_fb_resume(struct drm_device *drm)
321 {
322         struct rockchip_drm_private *priv = drm->dev_private;
323
324         console_lock();
325         drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
326         console_unlock();
327 }
328
329 static int rockchip_drm_sys_suspend(struct device *dev)
330 {
331         struct drm_device *drm = dev_get_drvdata(dev);
332         struct rockchip_drm_private *priv = drm->dev_private;
333
334         drm_kms_helper_poll_disable(drm);
335         rockchip_drm_fb_suspend(drm);
336
337         priv->state = drm_atomic_helper_suspend(drm);
338         if (IS_ERR(priv->state)) {
339                 rockchip_drm_fb_resume(drm);
340                 drm_kms_helper_poll_enable(drm);
341                 return PTR_ERR(priv->state);
342         }
343
344         return 0;
345 }
346
347 static int rockchip_drm_sys_resume(struct device *dev)
348 {
349         struct drm_device *drm = dev_get_drvdata(dev);
350         struct rockchip_drm_private *priv = drm->dev_private;
351
352         drm_atomic_helper_resume(drm, priv->state);
353         rockchip_drm_fb_resume(drm);
354         drm_kms_helper_poll_enable(drm);
355
356         return 0;
357 }
358 #endif
359
360 static const struct dev_pm_ops rockchip_drm_pm_ops = {
361         SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
362                                 rockchip_drm_sys_resume)
363 };
364
365 static int compare_of(struct device *dev, void *data)
366 {
367         struct device_node *np = data;
368
369         return dev->of_node == np;
370 }
371
372 static void rockchip_add_endpoints(struct device *dev,
373                                    struct component_match **match,
374                                    struct device_node *port)
375 {
376         struct device_node *ep, *remote;
377
378         for_each_child_of_node(port, ep) {
379                 remote = of_graph_get_remote_port_parent(ep);
380                 if (!remote || !of_device_is_available(remote)) {
381                         of_node_put(remote);
382                         continue;
383                 } else if (!of_device_is_available(remote->parent)) {
384                         dev_warn(dev, "parent device of %s is not available\n",
385                                  remote->full_name);
386                         of_node_put(remote);
387                         continue;
388                 }
389
390                 drm_of_component_match_add(dev, match, compare_of, remote);
391                 of_node_put(remote);
392         }
393 }
394
395 static const struct component_master_ops rockchip_drm_ops = {
396         .bind = rockchip_drm_bind,
397         .unbind = rockchip_drm_unbind,
398 };
399
400 static int rockchip_drm_platform_probe(struct platform_device *pdev)
401 {
402         struct device *dev = &pdev->dev;
403         struct component_match *match = NULL;
404         struct device_node *np = dev->of_node;
405         struct device_node *port;
406         int i;
407
408         if (!np)
409                 return -ENODEV;
410         /*
411          * Bind the crtc ports first, so that
412          * drm_of_find_possible_crtcs called from encoder .bind callbacks
413          * works as expected.
414          */
415         for (i = 0;; i++) {
416                 struct device_node *iommu;
417
418                 port = of_parse_phandle(np, "ports", i);
419                 if (!port)
420                         break;
421
422                 if (!of_device_is_available(port->parent)) {
423                         of_node_put(port);
424                         continue;
425                 }
426
427                 iommu = of_parse_phandle(port->parent, "iommus", 0);
428                 if (!iommu || !of_device_is_available(iommu->parent)) {
429                         dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
430                                 port->parent->full_name);
431                         /*
432                          * if there is a crtc not support iommu, force set all
433                          * crtc use non-iommu buffer.
434                          */
435                         is_support_iommu = false;
436                 }
437
438                 of_node_put(iommu);
439                 drm_of_component_match_add(dev, &match, compare_of,
440                                            port->parent);
441                 of_node_put(port);
442         }
443
444         if (i == 0) {
445                 dev_err(dev, "missing 'ports' property\n");
446                 return -ENODEV;
447         }
448
449         if (!match) {
450                 dev_err(dev, "No available vop found for display-subsystem.\n");
451                 return -ENODEV;
452         }
453         /*
454          * For each bound crtc, bind the encoders attached to its
455          * remote endpoint.
456          */
457         for (i = 0;; i++) {
458                 port = of_parse_phandle(np, "ports", i);
459                 if (!port)
460                         break;
461
462                 if (!of_device_is_available(port->parent)) {
463                         of_node_put(port);
464                         continue;
465                 }
466
467                 rockchip_add_endpoints(dev, &match, port);
468                 of_node_put(port);
469         }
470
471         return component_master_add_with_match(dev, &rockchip_drm_ops, match);
472 }
473
474 static int rockchip_drm_platform_remove(struct platform_device *pdev)
475 {
476         component_master_del(&pdev->dev, &rockchip_drm_ops);
477
478         return 0;
479 }
480
481 static const struct of_device_id rockchip_drm_dt_ids[] = {
482         { .compatible = "rockchip,display-subsystem", },
483         { /* sentinel */ },
484 };
485 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
486
487 static struct platform_driver rockchip_drm_platform_driver = {
488         .probe = rockchip_drm_platform_probe,
489         .remove = rockchip_drm_platform_remove,
490         .driver = {
491                 .name = "rockchip-drm",
492                 .of_match_table = rockchip_drm_dt_ids,
493                 .pm = &rockchip_drm_pm_ops,
494         },
495 };
496
497 module_platform_driver(rockchip_drm_platform_driver);
498
499 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
500 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
501 MODULE_LICENSE("GPL v2");