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