drm/exynos: Stop using drm_framebuffer_unregister_private
[linux/fpc-iii.git] / drivers / gpu / drm / etnaviv / etnaviv_drv.c
blobb92c24e07cea0c200b992411cb06cdeead7cea1e
1 /*
2 * Copyright (C) 2015 Etnaviv Project
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <linux/component.h>
18 #include <linux/of_platform.h>
19 #include <drm/drm_of.h>
21 #include "etnaviv_drv.h"
22 #include "etnaviv_gpu.h"
23 #include "etnaviv_gem.h"
24 #include "etnaviv_mmu.h"
25 #include "etnaviv_gem.h"
27 #ifdef CONFIG_DRM_ETNAVIV_REGISTER_LOGGING
28 static bool reglog;
29 MODULE_PARM_DESC(reglog, "Enable register read/write logging");
30 module_param(reglog, bool, 0600);
31 #else
32 #define reglog 0
33 #endif
35 void __iomem *etnaviv_ioremap(struct platform_device *pdev, const char *name,
36 const char *dbgname)
38 struct resource *res;
39 void __iomem *ptr;
41 if (name)
42 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
43 else
44 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
46 ptr = devm_ioremap_resource(&pdev->dev, res);
47 if (IS_ERR(ptr)) {
48 dev_err(&pdev->dev, "failed to ioremap %s: %ld\n", name,
49 PTR_ERR(ptr));
50 return ptr;
53 if (reglog)
54 dev_printk(KERN_DEBUG, &pdev->dev, "IO:region %s 0x%p %08zx\n",
55 dbgname, ptr, (size_t)resource_size(res));
57 return ptr;
60 void etnaviv_writel(u32 data, void __iomem *addr)
62 if (reglog)
63 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
65 writel(data, addr);
68 u32 etnaviv_readl(const void __iomem *addr)
70 u32 val = readl(addr);
72 if (reglog)
73 printk(KERN_DEBUG "IO:R %p %08x\n", addr, val);
75 return val;
79 * DRM operations:
83 static void load_gpu(struct drm_device *dev)
85 struct etnaviv_drm_private *priv = dev->dev_private;
86 unsigned int i;
88 for (i = 0; i < ETNA_MAX_PIPES; i++) {
89 struct etnaviv_gpu *g = priv->gpu[i];
91 if (g) {
92 int ret;
94 ret = etnaviv_gpu_init(g);
95 if (ret)
96 priv->gpu[i] = NULL;
101 static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
103 struct etnaviv_file_private *ctx;
105 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
106 if (!ctx)
107 return -ENOMEM;
109 file->driver_priv = ctx;
111 return 0;
114 static void etnaviv_preclose(struct drm_device *dev, struct drm_file *file)
116 struct etnaviv_drm_private *priv = dev->dev_private;
117 struct etnaviv_file_private *ctx = file->driver_priv;
118 unsigned int i;
120 for (i = 0; i < ETNA_MAX_PIPES; i++) {
121 struct etnaviv_gpu *gpu = priv->gpu[i];
123 if (gpu) {
124 mutex_lock(&gpu->lock);
125 if (gpu->lastctx == ctx)
126 gpu->lastctx = NULL;
127 mutex_unlock(&gpu->lock);
131 kfree(ctx);
135 * DRM debugfs:
138 #ifdef CONFIG_DEBUG_FS
139 static int etnaviv_gem_show(struct drm_device *dev, struct seq_file *m)
141 struct etnaviv_drm_private *priv = dev->dev_private;
143 etnaviv_gem_describe_objects(priv, m);
145 return 0;
148 static int etnaviv_mm_show(struct drm_device *dev, struct seq_file *m)
150 struct drm_printer p = drm_seq_file_printer(m);
152 read_lock(&dev->vma_offset_manager->vm_lock);
153 drm_mm_print(&dev->vma_offset_manager->vm_addr_space_mm, &p);
154 read_unlock(&dev->vma_offset_manager->vm_lock);
156 return 0;
159 static int etnaviv_mmu_show(struct etnaviv_gpu *gpu, struct seq_file *m)
161 struct drm_printer p = drm_seq_file_printer(m);
163 seq_printf(m, "Active Objects (%s):\n", dev_name(gpu->dev));
165 mutex_lock(&gpu->mmu->lock);
166 drm_mm_print(&gpu->mmu->mm, &p);
167 mutex_unlock(&gpu->mmu->lock);
169 return 0;
172 static void etnaviv_buffer_dump(struct etnaviv_gpu *gpu, struct seq_file *m)
174 struct etnaviv_cmdbuf *buf = gpu->buffer;
175 u32 size = buf->size;
176 u32 *ptr = buf->vaddr;
177 u32 i;
179 seq_printf(m, "virt %p - phys 0x%llx - free 0x%08x\n",
180 buf->vaddr, (u64)buf->paddr, size - buf->user_size);
182 for (i = 0; i < size / 4; i++) {
183 if (i && !(i % 4))
184 seq_puts(m, "\n");
185 if (i % 4 == 0)
186 seq_printf(m, "\t0x%p: ", ptr + i);
187 seq_printf(m, "%08x ", *(ptr + i));
189 seq_puts(m, "\n");
192 static int etnaviv_ring_show(struct etnaviv_gpu *gpu, struct seq_file *m)
194 seq_printf(m, "Ring Buffer (%s): ", dev_name(gpu->dev));
196 mutex_lock(&gpu->lock);
197 etnaviv_buffer_dump(gpu, m);
198 mutex_unlock(&gpu->lock);
200 return 0;
203 static int show_unlocked(struct seq_file *m, void *arg)
205 struct drm_info_node *node = (struct drm_info_node *) m->private;
206 struct drm_device *dev = node->minor->dev;
207 int (*show)(struct drm_device *dev, struct seq_file *m) =
208 node->info_ent->data;
210 return show(dev, m);
213 static int show_each_gpu(struct seq_file *m, void *arg)
215 struct drm_info_node *node = (struct drm_info_node *) m->private;
216 struct drm_device *dev = node->minor->dev;
217 struct etnaviv_drm_private *priv = dev->dev_private;
218 struct etnaviv_gpu *gpu;
219 int (*show)(struct etnaviv_gpu *gpu, struct seq_file *m) =
220 node->info_ent->data;
221 unsigned int i;
222 int ret = 0;
224 for (i = 0; i < ETNA_MAX_PIPES; i++) {
225 gpu = priv->gpu[i];
226 if (!gpu)
227 continue;
229 ret = show(gpu, m);
230 if (ret < 0)
231 break;
234 return ret;
237 static struct drm_info_list etnaviv_debugfs_list[] = {
238 {"gpu", show_each_gpu, 0, etnaviv_gpu_debugfs},
239 {"gem", show_unlocked, 0, etnaviv_gem_show},
240 { "mm", show_unlocked, 0, etnaviv_mm_show },
241 {"mmu", show_each_gpu, 0, etnaviv_mmu_show},
242 {"ring", show_each_gpu, 0, etnaviv_ring_show},
245 static int etnaviv_debugfs_init(struct drm_minor *minor)
247 struct drm_device *dev = minor->dev;
248 int ret;
250 ret = drm_debugfs_create_files(etnaviv_debugfs_list,
251 ARRAY_SIZE(etnaviv_debugfs_list),
252 minor->debugfs_root, minor);
254 if (ret) {
255 dev_err(dev->dev, "could not install etnaviv_debugfs_list\n");
256 return ret;
259 return ret;
262 static void etnaviv_debugfs_cleanup(struct drm_minor *minor)
264 drm_debugfs_remove_files(etnaviv_debugfs_list,
265 ARRAY_SIZE(etnaviv_debugfs_list), minor);
267 #endif
270 * DRM ioctls:
273 static int etnaviv_ioctl_get_param(struct drm_device *dev, void *data,
274 struct drm_file *file)
276 struct etnaviv_drm_private *priv = dev->dev_private;
277 struct drm_etnaviv_param *args = data;
278 struct etnaviv_gpu *gpu;
280 if (args->pipe >= ETNA_MAX_PIPES)
281 return -EINVAL;
283 gpu = priv->gpu[args->pipe];
284 if (!gpu)
285 return -ENXIO;
287 return etnaviv_gpu_get_param(gpu, args->param, &args->value);
290 static int etnaviv_ioctl_gem_new(struct drm_device *dev, void *data,
291 struct drm_file *file)
293 struct drm_etnaviv_gem_new *args = data;
295 if (args->flags & ~(ETNA_BO_CACHED | ETNA_BO_WC | ETNA_BO_UNCACHED |
296 ETNA_BO_FORCE_MMU))
297 return -EINVAL;
299 return etnaviv_gem_new_handle(dev, file, args->size,
300 args->flags, &args->handle);
303 #define TS(t) ((struct timespec){ \
304 .tv_sec = (t).tv_sec, \
305 .tv_nsec = (t).tv_nsec \
308 static int etnaviv_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
309 struct drm_file *file)
311 struct drm_etnaviv_gem_cpu_prep *args = data;
312 struct drm_gem_object *obj;
313 int ret;
315 if (args->op & ~(ETNA_PREP_READ | ETNA_PREP_WRITE | ETNA_PREP_NOSYNC))
316 return -EINVAL;
318 obj = drm_gem_object_lookup(file, args->handle);
319 if (!obj)
320 return -ENOENT;
322 ret = etnaviv_gem_cpu_prep(obj, args->op, &TS(args->timeout));
324 drm_gem_object_unreference_unlocked(obj);
326 return ret;
329 static int etnaviv_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
330 struct drm_file *file)
332 struct drm_etnaviv_gem_cpu_fini *args = data;
333 struct drm_gem_object *obj;
334 int ret;
336 if (args->flags)
337 return -EINVAL;
339 obj = drm_gem_object_lookup(file, args->handle);
340 if (!obj)
341 return -ENOENT;
343 ret = etnaviv_gem_cpu_fini(obj);
345 drm_gem_object_unreference_unlocked(obj);
347 return ret;
350 static int etnaviv_ioctl_gem_info(struct drm_device *dev, void *data,
351 struct drm_file *file)
353 struct drm_etnaviv_gem_info *args = data;
354 struct drm_gem_object *obj;
355 int ret;
357 if (args->pad)
358 return -EINVAL;
360 obj = drm_gem_object_lookup(file, args->handle);
361 if (!obj)
362 return -ENOENT;
364 ret = etnaviv_gem_mmap_offset(obj, &args->offset);
365 drm_gem_object_unreference_unlocked(obj);
367 return ret;
370 static int etnaviv_ioctl_wait_fence(struct drm_device *dev, void *data,
371 struct drm_file *file)
373 struct drm_etnaviv_wait_fence *args = data;
374 struct etnaviv_drm_private *priv = dev->dev_private;
375 struct timespec *timeout = &TS(args->timeout);
376 struct etnaviv_gpu *gpu;
378 if (args->flags & ~(ETNA_WAIT_NONBLOCK))
379 return -EINVAL;
381 if (args->pipe >= ETNA_MAX_PIPES)
382 return -EINVAL;
384 gpu = priv->gpu[args->pipe];
385 if (!gpu)
386 return -ENXIO;
388 if (args->flags & ETNA_WAIT_NONBLOCK)
389 timeout = NULL;
391 return etnaviv_gpu_wait_fence_interruptible(gpu, args->fence,
392 timeout);
395 static int etnaviv_ioctl_gem_userptr(struct drm_device *dev, void *data,
396 struct drm_file *file)
398 struct drm_etnaviv_gem_userptr *args = data;
399 int access;
401 if (args->flags & ~(ETNA_USERPTR_READ|ETNA_USERPTR_WRITE) ||
402 args->flags == 0)
403 return -EINVAL;
405 if (offset_in_page(args->user_ptr | args->user_size) ||
406 (uintptr_t)args->user_ptr != args->user_ptr ||
407 (u32)args->user_size != args->user_size ||
408 args->user_ptr & ~PAGE_MASK)
409 return -EINVAL;
411 if (args->flags & ETNA_USERPTR_WRITE)
412 access = VERIFY_WRITE;
413 else
414 access = VERIFY_READ;
416 if (!access_ok(access, (void __user *)(unsigned long)args->user_ptr,
417 args->user_size))
418 return -EFAULT;
420 return etnaviv_gem_new_userptr(dev, file, args->user_ptr,
421 args->user_size, args->flags,
422 &args->handle);
425 static int etnaviv_ioctl_gem_wait(struct drm_device *dev, void *data,
426 struct drm_file *file)
428 struct etnaviv_drm_private *priv = dev->dev_private;
429 struct drm_etnaviv_gem_wait *args = data;
430 struct timespec *timeout = &TS(args->timeout);
431 struct drm_gem_object *obj;
432 struct etnaviv_gpu *gpu;
433 int ret;
435 if (args->flags & ~(ETNA_WAIT_NONBLOCK))
436 return -EINVAL;
438 if (args->pipe >= ETNA_MAX_PIPES)
439 return -EINVAL;
441 gpu = priv->gpu[args->pipe];
442 if (!gpu)
443 return -ENXIO;
445 obj = drm_gem_object_lookup(file, args->handle);
446 if (!obj)
447 return -ENOENT;
449 if (args->flags & ETNA_WAIT_NONBLOCK)
450 timeout = NULL;
452 ret = etnaviv_gem_wait_bo(gpu, obj, timeout);
454 drm_gem_object_unreference_unlocked(obj);
456 return ret;
459 static const struct drm_ioctl_desc etnaviv_ioctls[] = {
460 #define ETNA_IOCTL(n, func, flags) \
461 DRM_IOCTL_DEF_DRV(ETNAVIV_##n, etnaviv_ioctl_##func, flags)
462 ETNA_IOCTL(GET_PARAM, get_param, DRM_AUTH|DRM_RENDER_ALLOW),
463 ETNA_IOCTL(GEM_NEW, gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
464 ETNA_IOCTL(GEM_INFO, gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
465 ETNA_IOCTL(GEM_CPU_PREP, gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
466 ETNA_IOCTL(GEM_CPU_FINI, gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
467 ETNA_IOCTL(GEM_SUBMIT, gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
468 ETNA_IOCTL(WAIT_FENCE, wait_fence, DRM_AUTH|DRM_RENDER_ALLOW),
469 ETNA_IOCTL(GEM_USERPTR, gem_userptr, DRM_AUTH|DRM_RENDER_ALLOW),
470 ETNA_IOCTL(GEM_WAIT, gem_wait, DRM_AUTH|DRM_RENDER_ALLOW),
473 static const struct vm_operations_struct vm_ops = {
474 .fault = etnaviv_gem_fault,
475 .open = drm_gem_vm_open,
476 .close = drm_gem_vm_close,
479 static const struct file_operations fops = {
480 .owner = THIS_MODULE,
481 .open = drm_open,
482 .release = drm_release,
483 .unlocked_ioctl = drm_ioctl,
484 .compat_ioctl = drm_compat_ioctl,
485 .poll = drm_poll,
486 .read = drm_read,
487 .llseek = no_llseek,
488 .mmap = etnaviv_gem_mmap,
491 static struct drm_driver etnaviv_drm_driver = {
492 .driver_features = DRIVER_GEM |
493 DRIVER_PRIME |
494 DRIVER_RENDER,
495 .open = etnaviv_open,
496 .preclose = etnaviv_preclose,
497 .gem_free_object_unlocked = etnaviv_gem_free_object,
498 .gem_vm_ops = &vm_ops,
499 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
500 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
501 .gem_prime_export = drm_gem_prime_export,
502 .gem_prime_import = drm_gem_prime_import,
503 .gem_prime_pin = etnaviv_gem_prime_pin,
504 .gem_prime_unpin = etnaviv_gem_prime_unpin,
505 .gem_prime_get_sg_table = etnaviv_gem_prime_get_sg_table,
506 .gem_prime_import_sg_table = etnaviv_gem_prime_import_sg_table,
507 .gem_prime_vmap = etnaviv_gem_prime_vmap,
508 .gem_prime_vunmap = etnaviv_gem_prime_vunmap,
509 .gem_prime_mmap = etnaviv_gem_prime_mmap,
510 #ifdef CONFIG_DEBUG_FS
511 .debugfs_init = etnaviv_debugfs_init,
512 .debugfs_cleanup = etnaviv_debugfs_cleanup,
513 #endif
514 .ioctls = etnaviv_ioctls,
515 .num_ioctls = DRM_ETNAVIV_NUM_IOCTLS,
516 .fops = &fops,
517 .name = "etnaviv",
518 .desc = "etnaviv DRM",
519 .date = "20151214",
520 .major = 1,
521 .minor = 0,
525 * Platform driver:
527 static int etnaviv_bind(struct device *dev)
529 struct etnaviv_drm_private *priv;
530 struct drm_device *drm;
531 int ret;
533 drm = drm_dev_alloc(&etnaviv_drm_driver, dev);
534 if (IS_ERR(drm))
535 return PTR_ERR(drm);
537 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
538 if (!priv) {
539 dev_err(dev, "failed to allocate private data\n");
540 ret = -ENOMEM;
541 goto out_unref;
543 drm->dev_private = priv;
545 priv->wq = alloc_ordered_workqueue("etnaviv", 0);
546 if (!priv->wq) {
547 ret = -ENOMEM;
548 goto out_wq;
551 mutex_init(&priv->gem_lock);
552 INIT_LIST_HEAD(&priv->gem_list);
553 priv->num_gpus = 0;
555 dev_set_drvdata(dev, drm);
557 ret = component_bind_all(dev, drm);
558 if (ret < 0)
559 goto out_bind;
561 load_gpu(drm);
563 ret = drm_dev_register(drm, 0);
564 if (ret)
565 goto out_register;
567 return 0;
569 out_register:
570 component_unbind_all(dev, drm);
571 out_bind:
572 flush_workqueue(priv->wq);
573 destroy_workqueue(priv->wq);
574 out_wq:
575 kfree(priv);
576 out_unref:
577 drm_dev_unref(drm);
579 return ret;
582 static void etnaviv_unbind(struct device *dev)
584 struct drm_device *drm = dev_get_drvdata(dev);
585 struct etnaviv_drm_private *priv = drm->dev_private;
587 drm_dev_unregister(drm);
589 flush_workqueue(priv->wq);
590 destroy_workqueue(priv->wq);
592 component_unbind_all(dev, drm);
594 drm->dev_private = NULL;
595 kfree(priv);
597 drm_dev_unref(drm);
600 static const struct component_master_ops etnaviv_master_ops = {
601 .bind = etnaviv_bind,
602 .unbind = etnaviv_unbind,
605 static int compare_of(struct device *dev, void *data)
607 struct device_node *np = data;
609 return dev->of_node == np;
612 static int compare_str(struct device *dev, void *data)
614 return !strcmp(dev_name(dev), data);
617 static int etnaviv_pdev_probe(struct platform_device *pdev)
619 struct device *dev = &pdev->dev;
620 struct device_node *node = dev->of_node;
621 struct component_match *match = NULL;
623 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
625 if (node) {
626 struct device_node *core_node;
627 int i;
629 for (i = 0; ; i++) {
630 core_node = of_parse_phandle(node, "cores", i);
631 if (!core_node)
632 break;
634 drm_of_component_match_add(&pdev->dev, &match,
635 compare_of, core_node);
636 of_node_put(core_node);
638 } else if (dev->platform_data) {
639 char **names = dev->platform_data;
640 unsigned i;
642 for (i = 0; names[i]; i++)
643 component_match_add(dev, &match, compare_str, names[i]);
646 return component_master_add_with_match(dev, &etnaviv_master_ops, match);
649 static int etnaviv_pdev_remove(struct platform_device *pdev)
651 component_master_del(&pdev->dev, &etnaviv_master_ops);
653 return 0;
656 static const struct of_device_id dt_match[] = {
657 { .compatible = "fsl,imx-gpu-subsystem" },
658 { .compatible = "marvell,dove-gpu-subsystem" },
661 MODULE_DEVICE_TABLE(of, dt_match);
663 static struct platform_driver etnaviv_platform_driver = {
664 .probe = etnaviv_pdev_probe,
665 .remove = etnaviv_pdev_remove,
666 .driver = {
667 .name = "etnaviv",
668 .of_match_table = dt_match,
672 static int __init etnaviv_init(void)
674 int ret;
676 etnaviv_validate_init();
678 ret = platform_driver_register(&etnaviv_gpu_driver);
679 if (ret != 0)
680 return ret;
682 ret = platform_driver_register(&etnaviv_platform_driver);
683 if (ret != 0)
684 platform_driver_unregister(&etnaviv_gpu_driver);
686 return ret;
688 module_init(etnaviv_init);
690 static void __exit etnaviv_exit(void)
692 platform_driver_unregister(&etnaviv_gpu_driver);
693 platform_driver_unregister(&etnaviv_platform_driver);
695 module_exit(etnaviv_exit);
697 MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
698 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
699 MODULE_AUTHOR("Lucas Stach <l.stach@pengutronix.de>");
700 MODULE_DESCRIPTION("etnaviv DRM Driver");
701 MODULE_LICENSE("GPL v2");
702 MODULE_ALIAS("platform:etnaviv");