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