drm/tegra: Rename host1x_drm structure to tegra_drm
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / host1x / drm / drm.c
1 /*
2  * Copyright (C) 2012 Avionic Design GmbH
3  * Copyright (C) 2012-2013 NVIDIA CORPORATION.  All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
13
14 #include <linux/dma-mapping.h>
15 #include <asm/dma-iommu.h>
16
17 #include <drm/drm.h>
18 #include <drm/drmP.h>
19
20 #include "host1x_client.h"
21 #include "dev.h"
22 #include "drm.h"
23 #include "gem.h"
24 #include "syncpt.h"
25
26 #define DRIVER_NAME "tegra"
27 #define DRIVER_DESC "NVIDIA Tegra graphics"
28 #define DRIVER_DATE "20120330"
29 #define DRIVER_MAJOR 0
30 #define DRIVER_MINOR 0
31 #define DRIVER_PATCHLEVEL 0
32
33 struct host1x_subdev {
34         struct host1x_client *client;
35         struct device_node *np;
36         struct list_head list;
37 };
38
39 static int host1x_subdev_add(struct tegra_drm *tegra, struct device_node *np)
40 {
41         struct host1x_subdev *subdev;
42
43         subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
44         if (!subdev)
45                 return -ENOMEM;
46
47         INIT_LIST_HEAD(&subdev->list);
48         subdev->np = of_node_get(np);
49
50         list_add_tail(&subdev->list, &tegra->subdevs);
51
52         return 0;
53 }
54
55 static int host1x_subdev_register(struct tegra_drm *tegra,
56                                   struct host1x_subdev *subdev,
57                                   struct host1x_client *client)
58 {
59         mutex_lock(&tegra->subdevs_lock);
60         list_del_init(&subdev->list);
61         list_add_tail(&subdev->list, &tegra->active);
62         subdev->client = client;
63         mutex_unlock(&tegra->subdevs_lock);
64
65         return 0;
66 }
67
68 static int host1x_subdev_unregister(struct tegra_drm *tegra,
69                                     struct host1x_subdev *subdev)
70 {
71         mutex_lock(&tegra->subdevs_lock);
72         list_del_init(&subdev->list);
73         mutex_unlock(&tegra->subdevs_lock);
74
75         of_node_put(subdev->np);
76         kfree(subdev);
77
78         return 0;
79 }
80
81 static int tegra_parse_dt(struct tegra_drm *tegra)
82 {
83         static const char * const compat[] = {
84                 "nvidia,tegra20-dc",
85                 "nvidia,tegra20-hdmi",
86                 "nvidia,tegra20-gr2d",
87                 "nvidia,tegra30-dc",
88                 "nvidia,tegra30-hdmi",
89                 "nvidia,tegra30-gr2d",
90         };
91         unsigned int i;
92         int err;
93
94         for (i = 0; i < ARRAY_SIZE(compat); i++) {
95                 struct device_node *np;
96
97                 for_each_child_of_node(tegra->dev->of_node, np) {
98                         if (of_device_is_compatible(np, compat[i]) &&
99                             of_device_is_available(np)) {
100                                 err = host1x_subdev_add(tegra, np);
101                                 if (err < 0)
102                                         return err;
103                         }
104                 }
105         }
106
107         return 0;
108 }
109
110 int tegra_drm_alloc(struct platform_device *pdev)
111 {
112         struct tegra_drm *tegra;
113         int err;
114
115         tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
116         if (!tegra)
117                 return -ENOMEM;
118
119         mutex_init(&tegra->subdevs_lock);
120         INIT_LIST_HEAD(&tegra->subdevs);
121         INIT_LIST_HEAD(&tegra->active);
122         mutex_init(&tegra->clients_lock);
123         INIT_LIST_HEAD(&tegra->clients);
124         tegra->dev = &pdev->dev;
125
126         err = tegra_parse_dt(tegra);
127         if (err < 0) {
128                 dev_err(&pdev->dev, "failed to parse DT: %d\n", err);
129                 return err;
130         }
131
132         host1x_set_drm_data(&pdev->dev, tegra);
133
134         return 0;
135 }
136
137 int tegra_drm_init(struct tegra_drm *tegra, struct drm_device *drm)
138 {
139         struct host1x_client *client;
140
141         mutex_lock(&tegra->clients_lock);
142
143         list_for_each_entry(client, &tegra->clients, list) {
144                 if (client->ops && client->ops->drm_init) {
145                         int err = client->ops->drm_init(client, drm);
146                         if (err < 0) {
147                                 dev_err(tegra->dev,
148                                         "DRM setup failed for %s: %d\n",
149                                         dev_name(client->dev), err);
150                                 mutex_unlock(&tegra->clients_lock);
151                                 return err;
152                         }
153                 }
154         }
155
156         mutex_unlock(&tegra->clients_lock);
157
158         return 0;
159 }
160
161 int tegra_drm_exit(struct tegra_drm *tegra)
162 {
163         struct platform_device *pdev = to_platform_device(tegra->dev);
164         struct host1x_client *client;
165
166         if (!tegra->drm)
167                 return 0;
168
169         mutex_lock(&tegra->clients_lock);
170
171         list_for_each_entry_reverse(client, &tegra->clients, list) {
172                 if (client->ops && client->ops->drm_exit) {
173                         int err = client->ops->drm_exit(client);
174                         if (err < 0) {
175                                 dev_err(tegra->dev,
176                                         "DRM cleanup failed for %s: %d\n",
177                                         dev_name(client->dev), err);
178                                 mutex_unlock(&tegra->clients_lock);
179                                 return err;
180                         }
181                 }
182         }
183
184         mutex_unlock(&tegra->clients_lock);
185
186         drm_platform_exit(&tegra_drm_driver, pdev);
187         tegra->drm = NULL;
188
189         return 0;
190 }
191
192 int host1x_register_client(struct tegra_drm *tegra,
193                            struct host1x_client *client)
194 {
195         struct host1x_subdev *subdev, *tmp;
196         int err;
197
198         mutex_lock(&tegra->clients_lock);
199         list_add_tail(&client->list, &tegra->clients);
200         mutex_unlock(&tegra->clients_lock);
201
202         list_for_each_entry_safe(subdev, tmp, &tegra->subdevs, list)
203                 if (subdev->np == client->dev->of_node)
204                         host1x_subdev_register(tegra, subdev, client);
205
206         if (list_empty(&tegra->subdevs)) {
207                 struct platform_device *pdev = to_platform_device(tegra->dev);
208
209                 err = drm_platform_init(&tegra_drm_driver, pdev);
210                 if (err < 0) {
211                         dev_err(tegra->dev, "drm_platform_init(): %d\n", err);
212                         return err;
213                 }
214         }
215
216         return 0;
217 }
218
219 int host1x_unregister_client(struct tegra_drm *tegra,
220                              struct host1x_client *client)
221 {
222         struct host1x_subdev *subdev, *tmp;
223         int err;
224
225         list_for_each_entry_safe(subdev, tmp, &tegra->active, list) {
226                 if (subdev->client == client) {
227                         err = tegra_drm_exit(tegra);
228                         if (err < 0) {
229                                 dev_err(tegra->dev, "tegra_drm_exit(): %d\n",
230                                         err);
231                                 return err;
232                         }
233
234                         host1x_subdev_unregister(tegra, subdev);
235                         break;
236                 }
237         }
238
239         mutex_lock(&tegra->clients_lock);
240         list_del_init(&client->list);
241         mutex_unlock(&tegra->clients_lock);
242
243         return 0;
244 }
245
246 static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
247 {
248         struct tegra_drm *tegra;
249         int err;
250
251         tegra = host1x_get_drm_data(drm->dev);
252         drm->dev_private = tegra;
253         tegra->drm = drm;
254
255         drm_mode_config_init(drm);
256
257         err = tegra_drm_init(tegra, drm);
258         if (err < 0)
259                 return err;
260
261         /*
262          * We don't use the drm_irq_install() helpers provided by the DRM
263          * core, so we need to set this manually in order to allow the
264          * DRM_IOCTL_WAIT_VBLANK to operate correctly.
265          */
266         drm->irq_enabled = true;
267
268         err = drm_vblank_init(drm, drm->mode_config.num_crtc);
269         if (err < 0)
270                 return err;
271
272         err = tegra_drm_fb_init(drm);
273         if (err < 0)
274                 return err;
275
276         drm_kms_helper_poll_init(drm);
277
278         return 0;
279 }
280
281 static int tegra_drm_unload(struct drm_device *drm)
282 {
283         drm_kms_helper_poll_fini(drm);
284         tegra_drm_fb_exit(drm);
285
286         drm_mode_config_cleanup(drm);
287
288         return 0;
289 }
290
291 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
292 {
293         struct host1x_drm_file *fpriv;
294
295         fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
296         if (!fpriv)
297                 return -ENOMEM;
298
299         INIT_LIST_HEAD(&fpriv->contexts);
300         filp->driver_priv = fpriv;
301
302         return 0;
303 }
304
305 static void host1x_drm_context_free(struct host1x_drm_context *context)
306 {
307         context->client->ops->close_channel(context);
308         kfree(context);
309 }
310
311 static void tegra_drm_lastclose(struct drm_device *drm)
312 {
313         struct tegra_drm *tegra = drm->dev_private;
314
315         tegra_fbdev_restore_mode(tegra->fbdev);
316 }
317
318 #ifdef CONFIG_DRM_TEGRA_STAGING
319 static bool host1x_drm_file_owns_context(struct host1x_drm_file *file,
320                                          struct host1x_drm_context *context)
321 {
322         struct host1x_drm_context *ctx;
323
324         list_for_each_entry(ctx, &file->contexts, list)
325                 if (ctx == context)
326                         return true;
327
328         return false;
329 }
330
331 static int tegra_gem_create(struct drm_device *drm, void *data,
332                             struct drm_file *file)
333 {
334         struct drm_tegra_gem_create *args = data;
335         struct tegra_bo *bo;
336
337         bo = tegra_bo_create_with_handle(file, drm, args->size,
338                                          &args->handle);
339         if (IS_ERR(bo))
340                 return PTR_ERR(bo);
341
342         return 0;
343 }
344
345 static int tegra_gem_mmap(struct drm_device *drm, void *data,
346                           struct drm_file *file)
347 {
348         struct drm_tegra_gem_mmap *args = data;
349         struct drm_gem_object *gem;
350         struct tegra_bo *bo;
351
352         gem = drm_gem_object_lookup(drm, file, args->handle);
353         if (!gem)
354                 return -EINVAL;
355
356         bo = to_tegra_bo(gem);
357
358         args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
359
360         drm_gem_object_unreference(gem);
361
362         return 0;
363 }
364
365 static int tegra_syncpt_read(struct drm_device *drm, void *data,
366                              struct drm_file *file)
367 {
368         struct drm_tegra_syncpt_read *args = data;
369         struct host1x *host = dev_get_drvdata(drm->dev);
370         struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
371
372         if (!sp)
373                 return -EINVAL;
374
375         args->value = host1x_syncpt_read_min(sp);
376         return 0;
377 }
378
379 static int tegra_syncpt_incr(struct drm_device *drm, void *data,
380                              struct drm_file *file)
381 {
382         struct drm_tegra_syncpt_incr *args = data;
383         struct host1x *host = dev_get_drvdata(drm->dev);
384         struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
385
386         if (!sp)
387                 return -EINVAL;
388
389         return host1x_syncpt_incr(sp);
390 }
391
392 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
393                              struct drm_file *file)
394 {
395         struct drm_tegra_syncpt_wait *args = data;
396         struct host1x *host = dev_get_drvdata(drm->dev);
397         struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
398
399         if (!sp)
400                 return -EINVAL;
401
402         return host1x_syncpt_wait(sp, args->thresh, args->timeout,
403                                   &args->value);
404 }
405
406 static int tegra_open_channel(struct drm_device *drm, void *data,
407                               struct drm_file *file)
408 {
409         struct host1x_drm_file *fpriv = file->driver_priv;
410         struct tegra_drm *tegra = drm->dev_private;
411         struct drm_tegra_open_channel *args = data;
412         struct host1x_drm_context *context;
413         struct host1x_client *client;
414         int err = -ENODEV;
415
416         context = kzalloc(sizeof(*context), GFP_KERNEL);
417         if (!context)
418                 return -ENOMEM;
419
420         list_for_each_entry(client, &tegra->clients, list)
421                 if (client->class == args->client) {
422                         err = client->ops->open_channel(client, context);
423                         if (err)
424                                 break;
425
426                         context->client = client;
427                         list_add(&context->list, &fpriv->contexts);
428                         args->context = (uintptr_t)context;
429                         return 0;
430                 }
431
432         kfree(context);
433         return err;
434 }
435
436 static int tegra_close_channel(struct drm_device *drm, void *data,
437                                struct drm_file *file)
438 {
439         struct drm_tegra_close_channel *args = data;
440         struct host1x_drm_file *fpriv = file->driver_priv;
441         struct host1x_drm_context *context =
442                 (struct host1x_drm_context *)(uintptr_t)args->context;
443
444         if (!host1x_drm_file_owns_context(fpriv, context))
445                 return -EINVAL;
446
447         list_del(&context->list);
448         host1x_drm_context_free(context);
449
450         return 0;
451 }
452
453 static int tegra_get_syncpt(struct drm_device *drm, void *data,
454                             struct drm_file *file)
455 {
456         struct drm_tegra_get_syncpt *args = data;
457         struct host1x_drm_file *fpriv = file->driver_priv;
458         struct host1x_drm_context *context =
459                 (struct host1x_drm_context *)(uintptr_t)args->context;
460         struct host1x_syncpt *syncpt;
461
462         if (!host1x_drm_file_owns_context(fpriv, context))
463                 return -ENODEV;
464
465         if (args->index >= context->client->num_syncpts)
466                 return -EINVAL;
467
468         syncpt = context->client->syncpts[args->index];
469         args->id = host1x_syncpt_id(syncpt);
470
471         return 0;
472 }
473
474 static int tegra_submit(struct drm_device *drm, void *data,
475                         struct drm_file *file)
476 {
477         struct drm_tegra_submit *args = data;
478         struct host1x_drm_file *fpriv = file->driver_priv;
479         struct host1x_drm_context *context =
480                 (struct host1x_drm_context *)(uintptr_t)args->context;
481
482         if (!host1x_drm_file_owns_context(fpriv, context))
483                 return -ENODEV;
484
485         return context->client->ops->submit(context, args, drm, file);
486 }
487 #endif
488
489 static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
490 #ifdef CONFIG_DRM_TEGRA_STAGING
491         DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
492         DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
493         DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
494         DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
495         DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
496         DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
497         DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
498         DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
499         DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
500 #endif
501 };
502
503 static const struct file_operations tegra_drm_fops = {
504         .owner = THIS_MODULE,
505         .open = drm_open,
506         .release = drm_release,
507         .unlocked_ioctl = drm_ioctl,
508         .mmap = tegra_drm_mmap,
509         .poll = drm_poll,
510         .read = drm_read,
511 #ifdef CONFIG_COMPAT
512         .compat_ioctl = drm_compat_ioctl,
513 #endif
514         .llseek = noop_llseek,
515 };
516
517 static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
518 {
519         struct drm_crtc *crtc;
520
521         list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
522                 struct tegra_dc *dc = to_tegra_dc(crtc);
523
524                 if (dc->pipe == pipe)
525                         return crtc;
526         }
527
528         return NULL;
529 }
530
531 static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
532 {
533         /* TODO: implement real hardware counter using syncpoints */
534         return drm_vblank_count(dev, crtc);
535 }
536
537 static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
538 {
539         struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
540         struct tegra_dc *dc = to_tegra_dc(crtc);
541
542         if (!crtc)
543                 return -ENODEV;
544
545         tegra_dc_enable_vblank(dc);
546
547         return 0;
548 }
549
550 static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
551 {
552         struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
553         struct tegra_dc *dc = to_tegra_dc(crtc);
554
555         if (crtc)
556                 tegra_dc_disable_vblank(dc);
557 }
558
559 static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
560 {
561         struct host1x_drm_file *fpriv = file->driver_priv;
562         struct host1x_drm_context *context, *tmp;
563         struct drm_crtc *crtc;
564
565         list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
566                 tegra_dc_cancel_page_flip(crtc, file);
567
568         list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
569                 host1x_drm_context_free(context);
570
571         kfree(fpriv);
572 }
573
574 #ifdef CONFIG_DEBUG_FS
575 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
576 {
577         struct drm_info_node *node = (struct drm_info_node *)s->private;
578         struct drm_device *drm = node->minor->dev;
579         struct drm_framebuffer *fb;
580
581         mutex_lock(&drm->mode_config.fb_lock);
582
583         list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
584                 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
585                            fb->base.id, fb->width, fb->height, fb->depth,
586                            fb->bits_per_pixel,
587                            atomic_read(&fb->refcount.refcount));
588         }
589
590         mutex_unlock(&drm->mode_config.fb_lock);
591
592         return 0;
593 }
594
595 static struct drm_info_list tegra_debugfs_list[] = {
596         { "framebuffers", tegra_debugfs_framebuffers, 0 },
597 };
598
599 static int tegra_debugfs_init(struct drm_minor *minor)
600 {
601         return drm_debugfs_create_files(tegra_debugfs_list,
602                                         ARRAY_SIZE(tegra_debugfs_list),
603                                         minor->debugfs_root, minor);
604 }
605
606 static void tegra_debugfs_cleanup(struct drm_minor *minor)
607 {
608         drm_debugfs_remove_files(tegra_debugfs_list,
609                                  ARRAY_SIZE(tegra_debugfs_list), minor);
610 }
611 #endif
612
613 struct drm_driver tegra_drm_driver = {
614         .driver_features = DRIVER_MODESET | DRIVER_GEM,
615         .load = tegra_drm_load,
616         .unload = tegra_drm_unload,
617         .open = tegra_drm_open,
618         .preclose = tegra_drm_preclose,
619         .lastclose = tegra_drm_lastclose,
620
621         .get_vblank_counter = tegra_drm_get_vblank_counter,
622         .enable_vblank = tegra_drm_enable_vblank,
623         .disable_vblank = tegra_drm_disable_vblank,
624
625 #if defined(CONFIG_DEBUG_FS)
626         .debugfs_init = tegra_debugfs_init,
627         .debugfs_cleanup = tegra_debugfs_cleanup,
628 #endif
629
630         .gem_free_object = tegra_bo_free_object,
631         .gem_vm_ops = &tegra_bo_vm_ops,
632         .dumb_create = tegra_bo_dumb_create,
633         .dumb_map_offset = tegra_bo_dumb_map_offset,
634         .dumb_destroy = drm_gem_dumb_destroy,
635
636         .ioctls = tegra_drm_ioctls,
637         .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
638         .fops = &tegra_drm_fops,
639
640         .name = DRIVER_NAME,
641         .desc = DRIVER_DESC,
642         .date = DRIVER_DATE,
643         .major = DRIVER_MAJOR,
644         .minor = DRIVER_MINOR,
645         .patchlevel = DRIVER_PATCHLEVEL,
646 };