drm/tegra: Implement syncpoint management UAPI
[linux-2.6-microblaze.git] / drivers / gpu / drm / tegra / drm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Avionic Design GmbH
4  * Copyright (C) 2012-2016 NVIDIA CORPORATION.  All rights reserved.
5  */
6
7 #include <linux/bitops.h>
8 #include <linux/host1x.h>
9 #include <linux/idr.h>
10 #include <linux/iommu.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13
14 #include <drm/drm_aperture.h>
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_debugfs.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fourcc.h>
20 #include <drm/drm_ioctl.h>
21 #include <drm/drm_prime.h>
22 #include <drm/drm_vblank.h>
23
24 #include "uapi.h"
25 #include "drm.h"
26 #include "gem.h"
27
28 #define DRIVER_NAME "tegra"
29 #define DRIVER_DESC "NVIDIA Tegra graphics"
30 #define DRIVER_DATE "20120330"
31 #define DRIVER_MAJOR 0
32 #define DRIVER_MINOR 0
33 #define DRIVER_PATCHLEVEL 0
34
35 #define CARVEOUT_SZ SZ_64M
36 #define CDMA_GATHER_FETCHES_MAX_NB 16383
37
38 static int tegra_atomic_check(struct drm_device *drm,
39                               struct drm_atomic_state *state)
40 {
41         int err;
42
43         err = drm_atomic_helper_check(drm, state);
44         if (err < 0)
45                 return err;
46
47         return tegra_display_hub_atomic_check(drm, state);
48 }
49
50 static const struct drm_mode_config_funcs tegra_drm_mode_config_funcs = {
51         .fb_create = tegra_fb_create,
52 #ifdef CONFIG_DRM_FBDEV_EMULATION
53         .output_poll_changed = drm_fb_helper_output_poll_changed,
54 #endif
55         .atomic_check = tegra_atomic_check,
56         .atomic_commit = drm_atomic_helper_commit,
57 };
58
59 static void tegra_atomic_commit_tail(struct drm_atomic_state *old_state)
60 {
61         struct drm_device *drm = old_state->dev;
62         struct tegra_drm *tegra = drm->dev_private;
63
64         if (tegra->hub) {
65                 bool fence_cookie = dma_fence_begin_signalling();
66
67                 drm_atomic_helper_commit_modeset_disables(drm, old_state);
68                 tegra_display_hub_atomic_commit(drm, old_state);
69                 drm_atomic_helper_commit_planes(drm, old_state, 0);
70                 drm_atomic_helper_commit_modeset_enables(drm, old_state);
71                 drm_atomic_helper_commit_hw_done(old_state);
72                 dma_fence_end_signalling(fence_cookie);
73                 drm_atomic_helper_wait_for_vblanks(drm, old_state);
74                 drm_atomic_helper_cleanup_planes(drm, old_state);
75         } else {
76                 drm_atomic_helper_commit_tail_rpm(old_state);
77         }
78 }
79
80 static const struct drm_mode_config_helper_funcs
81 tegra_drm_mode_config_helpers = {
82         .atomic_commit_tail = tegra_atomic_commit_tail,
83 };
84
85 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
86 {
87         struct tegra_drm_file *fpriv;
88
89         fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
90         if (!fpriv)
91                 return -ENOMEM;
92
93         idr_init_base(&fpriv->legacy_contexts, 1);
94         xa_init_flags(&fpriv->contexts, XA_FLAGS_ALLOC1);
95         xa_init(&fpriv->syncpoints);
96         mutex_init(&fpriv->lock);
97         filp->driver_priv = fpriv;
98
99         return 0;
100 }
101
102 static void tegra_drm_context_free(struct tegra_drm_context *context)
103 {
104         context->client->ops->close_channel(context);
105         kfree(context);
106 }
107
108 static int host1x_reloc_copy_from_user(struct host1x_reloc *dest,
109                                        struct drm_tegra_reloc __user *src,
110                                        struct drm_device *drm,
111                                        struct drm_file *file)
112 {
113         u32 cmdbuf, target;
114         int err;
115
116         err = get_user(cmdbuf, &src->cmdbuf.handle);
117         if (err < 0)
118                 return err;
119
120         err = get_user(dest->cmdbuf.offset, &src->cmdbuf.offset);
121         if (err < 0)
122                 return err;
123
124         err = get_user(target, &src->target.handle);
125         if (err < 0)
126                 return err;
127
128         err = get_user(dest->target.offset, &src->target.offset);
129         if (err < 0)
130                 return err;
131
132         err = get_user(dest->shift, &src->shift);
133         if (err < 0)
134                 return err;
135
136         dest->flags = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE;
137
138         dest->cmdbuf.bo = tegra_gem_lookup(file, cmdbuf);
139         if (!dest->cmdbuf.bo)
140                 return -ENOENT;
141
142         dest->target.bo = tegra_gem_lookup(file, target);
143         if (!dest->target.bo)
144                 return -ENOENT;
145
146         return 0;
147 }
148
149 int tegra_drm_submit(struct tegra_drm_context *context,
150                      struct drm_tegra_submit *args, struct drm_device *drm,
151                      struct drm_file *file)
152 {
153         struct host1x_client *client = &context->client->base;
154         unsigned int num_cmdbufs = args->num_cmdbufs;
155         unsigned int num_relocs = args->num_relocs;
156         struct drm_tegra_cmdbuf __user *user_cmdbufs;
157         struct drm_tegra_reloc __user *user_relocs;
158         struct drm_tegra_syncpt __user *user_syncpt;
159         struct drm_tegra_syncpt syncpt;
160         struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
161         struct drm_gem_object **refs;
162         struct host1x_syncpt *sp = NULL;
163         struct host1x_job *job;
164         unsigned int num_refs;
165         int err;
166
167         user_cmdbufs = u64_to_user_ptr(args->cmdbufs);
168         user_relocs = u64_to_user_ptr(args->relocs);
169         user_syncpt = u64_to_user_ptr(args->syncpts);
170
171         /* We don't yet support other than one syncpt_incr struct per submit */
172         if (args->num_syncpts != 1)
173                 return -EINVAL;
174
175         /* We don't yet support waitchks */
176         if (args->num_waitchks != 0)
177                 return -EINVAL;
178
179         job = host1x_job_alloc(context->channel, args->num_cmdbufs,
180                                args->num_relocs, false);
181         if (!job)
182                 return -ENOMEM;
183
184         job->num_relocs = args->num_relocs;
185         job->client = client;
186         job->class = client->class;
187         job->serialize = true;
188         job->syncpt_recovery = true;
189
190         /*
191          * Track referenced BOs so that they can be unreferenced after the
192          * submission is complete.
193          */
194         num_refs = num_cmdbufs + num_relocs * 2;
195
196         refs = kmalloc_array(num_refs, sizeof(*refs), GFP_KERNEL);
197         if (!refs) {
198                 err = -ENOMEM;
199                 goto put;
200         }
201
202         /* reuse as an iterator later */
203         num_refs = 0;
204
205         while (num_cmdbufs) {
206                 struct drm_tegra_cmdbuf cmdbuf;
207                 struct host1x_bo *bo;
208                 struct tegra_bo *obj;
209                 u64 offset;
210
211                 if (copy_from_user(&cmdbuf, user_cmdbufs, sizeof(cmdbuf))) {
212                         err = -EFAULT;
213                         goto fail;
214                 }
215
216                 /*
217                  * The maximum number of CDMA gather fetches is 16383, a higher
218                  * value means the words count is malformed.
219                  */
220                 if (cmdbuf.words > CDMA_GATHER_FETCHES_MAX_NB) {
221                         err = -EINVAL;
222                         goto fail;
223                 }
224
225                 bo = tegra_gem_lookup(file, cmdbuf.handle);
226                 if (!bo) {
227                         err = -ENOENT;
228                         goto fail;
229                 }
230
231                 offset = (u64)cmdbuf.offset + (u64)cmdbuf.words * sizeof(u32);
232                 obj = host1x_to_tegra_bo(bo);
233                 refs[num_refs++] = &obj->gem;
234
235                 /*
236                  * Gather buffer base address must be 4-bytes aligned,
237                  * unaligned offset is malformed and cause commands stream
238                  * corruption on the buffer address relocation.
239                  */
240                 if (offset & 3 || offset > obj->gem.size) {
241                         err = -EINVAL;
242                         goto fail;
243                 }
244
245                 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
246                 num_cmdbufs--;
247                 user_cmdbufs++;
248         }
249
250         /* copy and resolve relocations from submit */
251         while (num_relocs--) {
252                 struct host1x_reloc *reloc;
253                 struct tegra_bo *obj;
254
255                 err = host1x_reloc_copy_from_user(&job->relocs[num_relocs],
256                                                   &user_relocs[num_relocs], drm,
257                                                   file);
258                 if (err < 0)
259                         goto fail;
260
261                 reloc = &job->relocs[num_relocs];
262                 obj = host1x_to_tegra_bo(reloc->cmdbuf.bo);
263                 refs[num_refs++] = &obj->gem;
264
265                 /*
266                  * The unaligned cmdbuf offset will cause an unaligned write
267                  * during of the relocations patching, corrupting the commands
268                  * stream.
269                  */
270                 if (reloc->cmdbuf.offset & 3 ||
271                     reloc->cmdbuf.offset >= obj->gem.size) {
272                         err = -EINVAL;
273                         goto fail;
274                 }
275
276                 obj = host1x_to_tegra_bo(reloc->target.bo);
277                 refs[num_refs++] = &obj->gem;
278
279                 if (reloc->target.offset >= obj->gem.size) {
280                         err = -EINVAL;
281                         goto fail;
282                 }
283         }
284
285         if (copy_from_user(&syncpt, user_syncpt, sizeof(syncpt))) {
286                 err = -EFAULT;
287                 goto fail;
288         }
289
290         /* Syncpoint ref will be dropped on job release. */
291         sp = host1x_syncpt_get_by_id(host1x, syncpt.id);
292         if (!sp) {
293                 err = -ENOENT;
294                 goto fail;
295         }
296
297         job->is_addr_reg = context->client->ops->is_addr_reg;
298         job->is_valid_class = context->client->ops->is_valid_class;
299         job->syncpt_incrs = syncpt.incrs;
300         job->syncpt = sp;
301         job->timeout = 10000;
302
303         if (args->timeout && args->timeout < 10000)
304                 job->timeout = args->timeout;
305
306         err = host1x_job_pin(job, context->client->base.dev);
307         if (err)
308                 goto fail;
309
310         err = host1x_job_submit(job);
311         if (err) {
312                 host1x_job_unpin(job);
313                 goto fail;
314         }
315
316         args->fence = job->syncpt_end;
317
318 fail:
319         while (num_refs--)
320                 drm_gem_object_put(refs[num_refs]);
321
322         kfree(refs);
323
324 put:
325         host1x_job_put(job);
326         return err;
327 }
328
329
330 #ifdef CONFIG_DRM_TEGRA_STAGING
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, args->flags,
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(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_put(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 host1x *host = dev_get_drvdata(drm->dev->parent);
369         struct drm_tegra_syncpt_read *args = data;
370         struct host1x_syncpt *sp;
371
372         sp = host1x_syncpt_get_by_id_noref(host, args->id);
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 host1x *host1x = dev_get_drvdata(drm->dev->parent);
384         struct drm_tegra_syncpt_incr *args = data;
385         struct host1x_syncpt *sp;
386
387         sp = host1x_syncpt_get_by_id_noref(host1x, args->id);
388         if (!sp)
389                 return -EINVAL;
390
391         return host1x_syncpt_incr(sp);
392 }
393
394 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
395                              struct drm_file *file)
396 {
397         struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
398         struct drm_tegra_syncpt_wait *args = data;
399         struct host1x_syncpt *sp;
400
401         sp = host1x_syncpt_get_by_id_noref(host1x, args->id);
402         if (!sp)
403                 return -EINVAL;
404
405         return host1x_syncpt_wait(sp, args->thresh,
406                                   msecs_to_jiffies(args->timeout),
407                                   &args->value);
408 }
409
410 static int tegra_client_open(struct tegra_drm_file *fpriv,
411                              struct tegra_drm_client *client,
412                              struct tegra_drm_context *context)
413 {
414         int err;
415
416         err = client->ops->open_channel(client, context);
417         if (err < 0)
418                 return err;
419
420         err = idr_alloc(&fpriv->legacy_contexts, context, 1, 0, GFP_KERNEL);
421         if (err < 0) {
422                 client->ops->close_channel(context);
423                 return err;
424         }
425
426         context->client = client;
427         context->id = err;
428
429         return 0;
430 }
431
432 static int tegra_open_channel(struct drm_device *drm, void *data,
433                               struct drm_file *file)
434 {
435         struct tegra_drm_file *fpriv = file->driver_priv;
436         struct tegra_drm *tegra = drm->dev_private;
437         struct drm_tegra_open_channel *args = data;
438         struct tegra_drm_context *context;
439         struct tegra_drm_client *client;
440         int err = -ENODEV;
441
442         context = kzalloc(sizeof(*context), GFP_KERNEL);
443         if (!context)
444                 return -ENOMEM;
445
446         mutex_lock(&fpriv->lock);
447
448         list_for_each_entry(client, &tegra->clients, list)
449                 if (client->base.class == args->client) {
450                         err = tegra_client_open(fpriv, client, context);
451                         if (err < 0)
452                                 break;
453
454                         args->context = context->id;
455                         break;
456                 }
457
458         if (err < 0)
459                 kfree(context);
460
461         mutex_unlock(&fpriv->lock);
462         return err;
463 }
464
465 static int tegra_close_channel(struct drm_device *drm, void *data,
466                                struct drm_file *file)
467 {
468         struct tegra_drm_file *fpriv = file->driver_priv;
469         struct drm_tegra_close_channel *args = data;
470         struct tegra_drm_context *context;
471         int err = 0;
472
473         mutex_lock(&fpriv->lock);
474
475         context = idr_find(&fpriv->legacy_contexts, args->context);
476         if (!context) {
477                 err = -EINVAL;
478                 goto unlock;
479         }
480
481         idr_remove(&fpriv->legacy_contexts, context->id);
482         tegra_drm_context_free(context);
483
484 unlock:
485         mutex_unlock(&fpriv->lock);
486         return err;
487 }
488
489 static int tegra_get_syncpt(struct drm_device *drm, void *data,
490                             struct drm_file *file)
491 {
492         struct tegra_drm_file *fpriv = file->driver_priv;
493         struct drm_tegra_get_syncpt *args = data;
494         struct tegra_drm_context *context;
495         struct host1x_syncpt *syncpt;
496         int err = 0;
497
498         mutex_lock(&fpriv->lock);
499
500         context = idr_find(&fpriv->legacy_contexts, args->context);
501         if (!context) {
502                 err = -ENODEV;
503                 goto unlock;
504         }
505
506         if (args->index >= context->client->base.num_syncpts) {
507                 err = -EINVAL;
508                 goto unlock;
509         }
510
511         syncpt = context->client->base.syncpts[args->index];
512         args->id = host1x_syncpt_id(syncpt);
513
514 unlock:
515         mutex_unlock(&fpriv->lock);
516         return err;
517 }
518
519 static int tegra_submit(struct drm_device *drm, void *data,
520                         struct drm_file *file)
521 {
522         struct tegra_drm_file *fpriv = file->driver_priv;
523         struct drm_tegra_submit *args = data;
524         struct tegra_drm_context *context;
525         int err;
526
527         mutex_lock(&fpriv->lock);
528
529         context = idr_find(&fpriv->legacy_contexts, args->context);
530         if (!context) {
531                 err = -ENODEV;
532                 goto unlock;
533         }
534
535         err = context->client->ops->submit(context, args, drm, file);
536
537 unlock:
538         mutex_unlock(&fpriv->lock);
539         return err;
540 }
541
542 static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
543                                  struct drm_file *file)
544 {
545         struct tegra_drm_file *fpriv = file->driver_priv;
546         struct drm_tegra_get_syncpt_base *args = data;
547         struct tegra_drm_context *context;
548         struct host1x_syncpt_base *base;
549         struct host1x_syncpt *syncpt;
550         int err = 0;
551
552         mutex_lock(&fpriv->lock);
553
554         context = idr_find(&fpriv->legacy_contexts, args->context);
555         if (!context) {
556                 err = -ENODEV;
557                 goto unlock;
558         }
559
560         if (args->syncpt >= context->client->base.num_syncpts) {
561                 err = -EINVAL;
562                 goto unlock;
563         }
564
565         syncpt = context->client->base.syncpts[args->syncpt];
566
567         base = host1x_syncpt_get_base(syncpt);
568         if (!base) {
569                 err = -ENXIO;
570                 goto unlock;
571         }
572
573         args->id = host1x_syncpt_base_id(base);
574
575 unlock:
576         mutex_unlock(&fpriv->lock);
577         return err;
578 }
579
580 static int tegra_gem_set_tiling(struct drm_device *drm, void *data,
581                                 struct drm_file *file)
582 {
583         struct drm_tegra_gem_set_tiling *args = data;
584         enum tegra_bo_tiling_mode mode;
585         struct drm_gem_object *gem;
586         unsigned long value = 0;
587         struct tegra_bo *bo;
588
589         switch (args->mode) {
590         case DRM_TEGRA_GEM_TILING_MODE_PITCH:
591                 mode = TEGRA_BO_TILING_MODE_PITCH;
592
593                 if (args->value != 0)
594                         return -EINVAL;
595
596                 break;
597
598         case DRM_TEGRA_GEM_TILING_MODE_TILED:
599                 mode = TEGRA_BO_TILING_MODE_TILED;
600
601                 if (args->value != 0)
602                         return -EINVAL;
603
604                 break;
605
606         case DRM_TEGRA_GEM_TILING_MODE_BLOCK:
607                 mode = TEGRA_BO_TILING_MODE_BLOCK;
608
609                 if (args->value > 5)
610                         return -EINVAL;
611
612                 value = args->value;
613                 break;
614
615         default:
616                 return -EINVAL;
617         }
618
619         gem = drm_gem_object_lookup(file, args->handle);
620         if (!gem)
621                 return -ENOENT;
622
623         bo = to_tegra_bo(gem);
624
625         bo->tiling.mode = mode;
626         bo->tiling.value = value;
627
628         drm_gem_object_put(gem);
629
630         return 0;
631 }
632
633 static int tegra_gem_get_tiling(struct drm_device *drm, void *data,
634                                 struct drm_file *file)
635 {
636         struct drm_tegra_gem_get_tiling *args = data;
637         struct drm_gem_object *gem;
638         struct tegra_bo *bo;
639         int err = 0;
640
641         gem = drm_gem_object_lookup(file, args->handle);
642         if (!gem)
643                 return -ENOENT;
644
645         bo = to_tegra_bo(gem);
646
647         switch (bo->tiling.mode) {
648         case TEGRA_BO_TILING_MODE_PITCH:
649                 args->mode = DRM_TEGRA_GEM_TILING_MODE_PITCH;
650                 args->value = 0;
651                 break;
652
653         case TEGRA_BO_TILING_MODE_TILED:
654                 args->mode = DRM_TEGRA_GEM_TILING_MODE_TILED;
655                 args->value = 0;
656                 break;
657
658         case TEGRA_BO_TILING_MODE_BLOCK:
659                 args->mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
660                 args->value = bo->tiling.value;
661                 break;
662
663         default:
664                 err = -EINVAL;
665                 break;
666         }
667
668         drm_gem_object_put(gem);
669
670         return err;
671 }
672
673 static int tegra_gem_set_flags(struct drm_device *drm, void *data,
674                                struct drm_file *file)
675 {
676         struct drm_tegra_gem_set_flags *args = data;
677         struct drm_gem_object *gem;
678         struct tegra_bo *bo;
679
680         if (args->flags & ~DRM_TEGRA_GEM_FLAGS)
681                 return -EINVAL;
682
683         gem = drm_gem_object_lookup(file, args->handle);
684         if (!gem)
685                 return -ENOENT;
686
687         bo = to_tegra_bo(gem);
688         bo->flags = 0;
689
690         if (args->flags & DRM_TEGRA_GEM_BOTTOM_UP)
691                 bo->flags |= TEGRA_BO_BOTTOM_UP;
692
693         drm_gem_object_put(gem);
694
695         return 0;
696 }
697
698 static int tegra_gem_get_flags(struct drm_device *drm, void *data,
699                                struct drm_file *file)
700 {
701         struct drm_tegra_gem_get_flags *args = data;
702         struct drm_gem_object *gem;
703         struct tegra_bo *bo;
704
705         gem = drm_gem_object_lookup(file, args->handle);
706         if (!gem)
707                 return -ENOENT;
708
709         bo = to_tegra_bo(gem);
710         args->flags = 0;
711
712         if (bo->flags & TEGRA_BO_BOTTOM_UP)
713                 args->flags |= DRM_TEGRA_GEM_BOTTOM_UP;
714
715         drm_gem_object_put(gem);
716
717         return 0;
718 }
719 #endif
720
721 static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
722 #ifdef CONFIG_DRM_TEGRA_STAGING
723         DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_OPEN, tegra_drm_ioctl_channel_open,
724                           DRM_RENDER_ALLOW),
725         DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_CLOSE, tegra_drm_ioctl_channel_close,
726                           DRM_RENDER_ALLOW),
727         DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_MAP, tegra_drm_ioctl_channel_map,
728                           DRM_RENDER_ALLOW),
729         DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_UNMAP, tegra_drm_ioctl_channel_unmap,
730                           DRM_RENDER_ALLOW),
731         DRM_IOCTL_DEF_DRV(TEGRA_SYNCPOINT_ALLOCATE, tegra_drm_ioctl_syncpoint_allocate,
732                           DRM_RENDER_ALLOW),
733         DRM_IOCTL_DEF_DRV(TEGRA_SYNCPOINT_FREE, tegra_drm_ioctl_syncpoint_free,
734                           DRM_RENDER_ALLOW),
735
736         DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_RENDER_ALLOW),
737         DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_RENDER_ALLOW),
738         DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read,
739                           DRM_RENDER_ALLOW),
740         DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr,
741                           DRM_RENDER_ALLOW),
742         DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait,
743                           DRM_RENDER_ALLOW),
744         DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel,
745                           DRM_RENDER_ALLOW),
746         DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel,
747                           DRM_RENDER_ALLOW),
748         DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt,
749                           DRM_RENDER_ALLOW),
750         DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit,
751                           DRM_RENDER_ALLOW),
752         DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base,
753                           DRM_RENDER_ALLOW),
754         DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_TILING, tegra_gem_set_tiling,
755                           DRM_RENDER_ALLOW),
756         DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_TILING, tegra_gem_get_tiling,
757                           DRM_RENDER_ALLOW),
758         DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_FLAGS, tegra_gem_set_flags,
759                           DRM_RENDER_ALLOW),
760         DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_FLAGS, tegra_gem_get_flags,
761                           DRM_RENDER_ALLOW),
762 #endif
763 };
764
765 static const struct file_operations tegra_drm_fops = {
766         .owner = THIS_MODULE,
767         .open = drm_open,
768         .release = drm_release,
769         .unlocked_ioctl = drm_ioctl,
770         .mmap = tegra_drm_mmap,
771         .poll = drm_poll,
772         .read = drm_read,
773         .compat_ioctl = drm_compat_ioctl,
774         .llseek = noop_llseek,
775 };
776
777 static int tegra_drm_context_cleanup(int id, void *p, void *data)
778 {
779         struct tegra_drm_context *context = p;
780
781         tegra_drm_context_free(context);
782
783         return 0;
784 }
785
786 static void tegra_drm_postclose(struct drm_device *drm, struct drm_file *file)
787 {
788         struct tegra_drm_file *fpriv = file->driver_priv;
789
790         mutex_lock(&fpriv->lock);
791         idr_for_each(&fpriv->legacy_contexts, tegra_drm_context_cleanup, NULL);
792         tegra_drm_uapi_close_file(fpriv);
793         mutex_unlock(&fpriv->lock);
794
795         idr_destroy(&fpriv->legacy_contexts);
796         mutex_destroy(&fpriv->lock);
797         kfree(fpriv);
798 }
799
800 #ifdef CONFIG_DEBUG_FS
801 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
802 {
803         struct drm_info_node *node = (struct drm_info_node *)s->private;
804         struct drm_device *drm = node->minor->dev;
805         struct drm_framebuffer *fb;
806
807         mutex_lock(&drm->mode_config.fb_lock);
808
809         list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
810                 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
811                            fb->base.id, fb->width, fb->height,
812                            fb->format->depth,
813                            fb->format->cpp[0] * 8,
814                            drm_framebuffer_read_refcount(fb));
815         }
816
817         mutex_unlock(&drm->mode_config.fb_lock);
818
819         return 0;
820 }
821
822 static int tegra_debugfs_iova(struct seq_file *s, void *data)
823 {
824         struct drm_info_node *node = (struct drm_info_node *)s->private;
825         struct drm_device *drm = node->minor->dev;
826         struct tegra_drm *tegra = drm->dev_private;
827         struct drm_printer p = drm_seq_file_printer(s);
828
829         if (tegra->domain) {
830                 mutex_lock(&tegra->mm_lock);
831                 drm_mm_print(&tegra->mm, &p);
832                 mutex_unlock(&tegra->mm_lock);
833         }
834
835         return 0;
836 }
837
838 static struct drm_info_list tegra_debugfs_list[] = {
839         { "framebuffers", tegra_debugfs_framebuffers, 0 },
840         { "iova", tegra_debugfs_iova, 0 },
841 };
842
843 static void tegra_debugfs_init(struct drm_minor *minor)
844 {
845         drm_debugfs_create_files(tegra_debugfs_list,
846                                  ARRAY_SIZE(tegra_debugfs_list),
847                                  minor->debugfs_root, minor);
848 }
849 #endif
850
851 static const struct drm_driver tegra_drm_driver = {
852         .driver_features = DRIVER_MODESET | DRIVER_GEM |
853                            DRIVER_ATOMIC | DRIVER_RENDER,
854         .open = tegra_drm_open,
855         .postclose = tegra_drm_postclose,
856         .lastclose = drm_fb_helper_lastclose,
857
858 #if defined(CONFIG_DEBUG_FS)
859         .debugfs_init = tegra_debugfs_init,
860 #endif
861
862         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
863         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
864         .gem_prime_import = tegra_gem_prime_import,
865
866         .dumb_create = tegra_bo_dumb_create,
867
868         .ioctls = tegra_drm_ioctls,
869         .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
870         .fops = &tegra_drm_fops,
871
872         .name = DRIVER_NAME,
873         .desc = DRIVER_DESC,
874         .date = DRIVER_DATE,
875         .major = DRIVER_MAJOR,
876         .minor = DRIVER_MINOR,
877         .patchlevel = DRIVER_PATCHLEVEL,
878 };
879
880 int tegra_drm_register_client(struct tegra_drm *tegra,
881                               struct tegra_drm_client *client)
882 {
883         /*
884          * When MLOCKs are implemented, change to allocate a shared channel
885          * only when MLOCKs are disabled.
886          */
887         client->shared_channel = host1x_channel_request(&client->base);
888         if (!client->shared_channel)
889                 return -EBUSY;
890
891         mutex_lock(&tegra->clients_lock);
892         list_add_tail(&client->list, &tegra->clients);
893         client->drm = tegra;
894         mutex_unlock(&tegra->clients_lock);
895
896         return 0;
897 }
898
899 int tegra_drm_unregister_client(struct tegra_drm *tegra,
900                                 struct tegra_drm_client *client)
901 {
902         mutex_lock(&tegra->clients_lock);
903         list_del_init(&client->list);
904         client->drm = NULL;
905         mutex_unlock(&tegra->clients_lock);
906
907         if (client->shared_channel)
908                 host1x_channel_put(client->shared_channel);
909
910         return 0;
911 }
912
913 int host1x_client_iommu_attach(struct host1x_client *client)
914 {
915         struct iommu_domain *domain = iommu_get_domain_for_dev(client->dev);
916         struct drm_device *drm = dev_get_drvdata(client->host);
917         struct tegra_drm *tegra = drm->dev_private;
918         struct iommu_group *group = NULL;
919         int err;
920
921         /*
922          * If the host1x client is already attached to an IOMMU domain that is
923          * not the shared IOMMU domain, don't try to attach it to a different
924          * domain. This allows using the IOMMU-backed DMA API.
925          */
926         if (domain && domain != tegra->domain)
927                 return 0;
928
929         if (tegra->domain) {
930                 group = iommu_group_get(client->dev);
931                 if (!group)
932                         return -ENODEV;
933
934                 if (domain != tegra->domain) {
935                         err = iommu_attach_group(tegra->domain, group);
936                         if (err < 0) {
937                                 iommu_group_put(group);
938                                 return err;
939                         }
940                 }
941
942                 tegra->use_explicit_iommu = true;
943         }
944
945         client->group = group;
946
947         return 0;
948 }
949
950 void host1x_client_iommu_detach(struct host1x_client *client)
951 {
952         struct drm_device *drm = dev_get_drvdata(client->host);
953         struct tegra_drm *tegra = drm->dev_private;
954         struct iommu_domain *domain;
955
956         if (client->group) {
957                 /*
958                  * Devices that are part of the same group may no longer be
959                  * attached to a domain at this point because their group may
960                  * have been detached by an earlier client.
961                  */
962                 domain = iommu_get_domain_for_dev(client->dev);
963                 if (domain)
964                         iommu_detach_group(tegra->domain, client->group);
965
966                 iommu_group_put(client->group);
967                 client->group = NULL;
968         }
969 }
970
971 void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size, dma_addr_t *dma)
972 {
973         struct iova *alloc;
974         void *virt;
975         gfp_t gfp;
976         int err;
977
978         if (tegra->domain)
979                 size = iova_align(&tegra->carveout.domain, size);
980         else
981                 size = PAGE_ALIGN(size);
982
983         gfp = GFP_KERNEL | __GFP_ZERO;
984         if (!tegra->domain) {
985                 /*
986                  * Many units only support 32-bit addresses, even on 64-bit
987                  * SoCs. If there is no IOMMU to translate into a 32-bit IO
988                  * virtual address space, force allocations to be in the
989                  * lower 32-bit range.
990                  */
991                 gfp |= GFP_DMA;
992         }
993
994         virt = (void *)__get_free_pages(gfp, get_order(size));
995         if (!virt)
996                 return ERR_PTR(-ENOMEM);
997
998         if (!tegra->domain) {
999                 /*
1000                  * If IOMMU is disabled, devices address physical memory
1001                  * directly.
1002                  */
1003                 *dma = virt_to_phys(virt);
1004                 return virt;
1005         }
1006
1007         alloc = alloc_iova(&tegra->carveout.domain,
1008                            size >> tegra->carveout.shift,
1009                            tegra->carveout.limit, true);
1010         if (!alloc) {
1011                 err = -EBUSY;
1012                 goto free_pages;
1013         }
1014
1015         *dma = iova_dma_addr(&tegra->carveout.domain, alloc);
1016         err = iommu_map(tegra->domain, *dma, virt_to_phys(virt),
1017                         size, IOMMU_READ | IOMMU_WRITE);
1018         if (err < 0)
1019                 goto free_iova;
1020
1021         return virt;
1022
1023 free_iova:
1024         __free_iova(&tegra->carveout.domain, alloc);
1025 free_pages:
1026         free_pages((unsigned long)virt, get_order(size));
1027
1028         return ERR_PTR(err);
1029 }
1030
1031 void tegra_drm_free(struct tegra_drm *tegra, size_t size, void *virt,
1032                     dma_addr_t dma)
1033 {
1034         if (tegra->domain)
1035                 size = iova_align(&tegra->carveout.domain, size);
1036         else
1037                 size = PAGE_ALIGN(size);
1038
1039         if (tegra->domain) {
1040                 iommu_unmap(tegra->domain, dma, size);
1041                 free_iova(&tegra->carveout.domain,
1042                           iova_pfn(&tegra->carveout.domain, dma));
1043         }
1044
1045         free_pages((unsigned long)virt, get_order(size));
1046 }
1047
1048 static bool host1x_drm_wants_iommu(struct host1x_device *dev)
1049 {
1050         struct host1x *host1x = dev_get_drvdata(dev->dev.parent);
1051         struct iommu_domain *domain;
1052
1053         /*
1054          * If the Tegra DRM clients are backed by an IOMMU, push buffers are
1055          * likely to be allocated beyond the 32-bit boundary if sufficient
1056          * system memory is available. This is problematic on earlier Tegra
1057          * generations where host1x supports a maximum of 32 address bits in
1058          * the GATHER opcode. In this case, unless host1x is behind an IOMMU
1059          * as well it won't be able to process buffers allocated beyond the
1060          * 32-bit boundary.
1061          *
1062          * The DMA API will use bounce buffers in this case, so that could
1063          * perhaps still be made to work, even if less efficient, but there
1064          * is another catch: in order to perform cache maintenance on pages
1065          * allocated for discontiguous buffers we need to map and unmap the
1066          * SG table representing these buffers. This is fine for something
1067          * small like a push buffer, but it exhausts the bounce buffer pool
1068          * (typically on the order of a few MiB) for framebuffers (many MiB
1069          * for any modern resolution).
1070          *
1071          * Work around this by making sure that Tegra DRM clients only use
1072          * an IOMMU if the parent host1x also uses an IOMMU.
1073          *
1074          * Note that there's still a small gap here that we don't cover: if
1075          * the DMA API is backed by an IOMMU there's no way to control which
1076          * device is attached to an IOMMU and which isn't, except via wiring
1077          * up the device tree appropriately. This is considered an problem
1078          * of integration, so care must be taken for the DT to be consistent.
1079          */
1080         domain = iommu_get_domain_for_dev(dev->dev.parent);
1081
1082         /*
1083          * Tegra20 and Tegra30 don't support addressing memory beyond the
1084          * 32-bit boundary, so the regular GATHER opcodes will always be
1085          * sufficient and whether or not the host1x is attached to an IOMMU
1086          * doesn't matter.
1087          */
1088         if (!domain && host1x_get_dma_mask(host1x) <= DMA_BIT_MASK(32))
1089                 return true;
1090
1091         return domain != NULL;
1092 }
1093
1094 static int host1x_drm_probe(struct host1x_device *dev)
1095 {
1096         struct tegra_drm *tegra;
1097         struct drm_device *drm;
1098         int err;
1099
1100         drm = drm_dev_alloc(&tegra_drm_driver, &dev->dev);
1101         if (IS_ERR(drm))
1102                 return PTR_ERR(drm);
1103
1104         tegra = kzalloc(sizeof(*tegra), GFP_KERNEL);
1105         if (!tegra) {
1106                 err = -ENOMEM;
1107                 goto put;
1108         }
1109
1110         if (host1x_drm_wants_iommu(dev) && iommu_present(&platform_bus_type)) {
1111                 tegra->domain = iommu_domain_alloc(&platform_bus_type);
1112                 if (!tegra->domain) {
1113                         err = -ENOMEM;
1114                         goto free;
1115                 }
1116
1117                 err = iova_cache_get();
1118                 if (err < 0)
1119                         goto domain;
1120         }
1121
1122         mutex_init(&tegra->clients_lock);
1123         INIT_LIST_HEAD(&tegra->clients);
1124
1125         dev_set_drvdata(&dev->dev, drm);
1126         drm->dev_private = tegra;
1127         tegra->drm = drm;
1128
1129         drm_mode_config_init(drm);
1130
1131         drm->mode_config.min_width = 0;
1132         drm->mode_config.min_height = 0;
1133         drm->mode_config.max_width = 0;
1134         drm->mode_config.max_height = 0;
1135
1136         drm->mode_config.normalize_zpos = true;
1137
1138         drm->mode_config.funcs = &tegra_drm_mode_config_funcs;
1139         drm->mode_config.helper_private = &tegra_drm_mode_config_helpers;
1140
1141         err = tegra_drm_fb_prepare(drm);
1142         if (err < 0)
1143                 goto config;
1144
1145         drm_kms_helper_poll_init(drm);
1146
1147         err = host1x_device_init(dev);
1148         if (err < 0)
1149                 goto fbdev;
1150
1151         /*
1152          * Now that all display controller have been initialized, the maximum
1153          * supported resolution is known and the bitmask for horizontal and
1154          * vertical bitfields can be computed.
1155          */
1156         tegra->hmask = drm->mode_config.max_width - 1;
1157         tegra->vmask = drm->mode_config.max_height - 1;
1158
1159         if (tegra->use_explicit_iommu) {
1160                 u64 carveout_start, carveout_end, gem_start, gem_end;
1161                 u64 dma_mask = dma_get_mask(&dev->dev);
1162                 dma_addr_t start, end;
1163                 unsigned long order;
1164
1165                 start = tegra->domain->geometry.aperture_start & dma_mask;
1166                 end = tegra->domain->geometry.aperture_end & dma_mask;
1167
1168                 gem_start = start;
1169                 gem_end = end - CARVEOUT_SZ;
1170                 carveout_start = gem_end + 1;
1171                 carveout_end = end;
1172
1173                 order = __ffs(tegra->domain->pgsize_bitmap);
1174                 init_iova_domain(&tegra->carveout.domain, 1UL << order,
1175                                  carveout_start >> order);
1176
1177                 tegra->carveout.shift = iova_shift(&tegra->carveout.domain);
1178                 tegra->carveout.limit = carveout_end >> tegra->carveout.shift;
1179
1180                 drm_mm_init(&tegra->mm, gem_start, gem_end - gem_start + 1);
1181                 mutex_init(&tegra->mm_lock);
1182
1183                 DRM_DEBUG_DRIVER("IOMMU apertures:\n");
1184                 DRM_DEBUG_DRIVER("  GEM: %#llx-%#llx\n", gem_start, gem_end);
1185                 DRM_DEBUG_DRIVER("  Carveout: %#llx-%#llx\n", carveout_start,
1186                                  carveout_end);
1187         } else if (tegra->domain) {
1188                 iommu_domain_free(tegra->domain);
1189                 tegra->domain = NULL;
1190                 iova_cache_put();
1191         }
1192
1193         if (tegra->hub) {
1194                 err = tegra_display_hub_prepare(tegra->hub);
1195                 if (err < 0)
1196                         goto device;
1197         }
1198
1199         /*
1200          * We don't use the drm_irq_install() helpers provided by the DRM
1201          * core, so we need to set this manually in order to allow the
1202          * DRM_IOCTL_WAIT_VBLANK to operate correctly.
1203          */
1204         drm->irq_enabled = true;
1205
1206         /* syncpoints are used for full 32-bit hardware VBLANK counters */
1207         drm->max_vblank_count = 0xffffffff;
1208
1209         err = drm_vblank_init(drm, drm->mode_config.num_crtc);
1210         if (err < 0)
1211                 goto hub;
1212
1213         drm_mode_config_reset(drm);
1214
1215         err = drm_aperture_remove_framebuffers(false, "tegradrmfb");
1216         if (err < 0)
1217                 goto hub;
1218
1219         err = tegra_drm_fb_init(drm);
1220         if (err < 0)
1221                 goto hub;
1222
1223         err = drm_dev_register(drm, 0);
1224         if (err < 0)
1225                 goto fb;
1226
1227         return 0;
1228
1229 fb:
1230         tegra_drm_fb_exit(drm);
1231 hub:
1232         if (tegra->hub)
1233                 tegra_display_hub_cleanup(tegra->hub);
1234 device:
1235         if (tegra->domain) {
1236                 mutex_destroy(&tegra->mm_lock);
1237                 drm_mm_takedown(&tegra->mm);
1238                 put_iova_domain(&tegra->carveout.domain);
1239                 iova_cache_put();
1240         }
1241
1242         host1x_device_exit(dev);
1243 fbdev:
1244         drm_kms_helper_poll_fini(drm);
1245         tegra_drm_fb_free(drm);
1246 config:
1247         drm_mode_config_cleanup(drm);
1248 domain:
1249         if (tegra->domain)
1250                 iommu_domain_free(tegra->domain);
1251 free:
1252         kfree(tegra);
1253 put:
1254         drm_dev_put(drm);
1255         return err;
1256 }
1257
1258 static int host1x_drm_remove(struct host1x_device *dev)
1259 {
1260         struct drm_device *drm = dev_get_drvdata(&dev->dev);
1261         struct tegra_drm *tegra = drm->dev_private;
1262         int err;
1263
1264         drm_dev_unregister(drm);
1265
1266         drm_kms_helper_poll_fini(drm);
1267         tegra_drm_fb_exit(drm);
1268         drm_atomic_helper_shutdown(drm);
1269         drm_mode_config_cleanup(drm);
1270
1271         if (tegra->hub)
1272                 tegra_display_hub_cleanup(tegra->hub);
1273
1274         err = host1x_device_exit(dev);
1275         if (err < 0)
1276                 dev_err(&dev->dev, "host1x device cleanup failed: %d\n", err);
1277
1278         if (tegra->domain) {
1279                 mutex_destroy(&tegra->mm_lock);
1280                 drm_mm_takedown(&tegra->mm);
1281                 put_iova_domain(&tegra->carveout.domain);
1282                 iova_cache_put();
1283                 iommu_domain_free(tegra->domain);
1284         }
1285
1286         kfree(tegra);
1287         drm_dev_put(drm);
1288
1289         return 0;
1290 }
1291
1292 #ifdef CONFIG_PM_SLEEP
1293 static int host1x_drm_suspend(struct device *dev)
1294 {
1295         struct drm_device *drm = dev_get_drvdata(dev);
1296
1297         return drm_mode_config_helper_suspend(drm);
1298 }
1299
1300 static int host1x_drm_resume(struct device *dev)
1301 {
1302         struct drm_device *drm = dev_get_drvdata(dev);
1303
1304         return drm_mode_config_helper_resume(drm);
1305 }
1306 #endif
1307
1308 static SIMPLE_DEV_PM_OPS(host1x_drm_pm_ops, host1x_drm_suspend,
1309                          host1x_drm_resume);
1310
1311 static const struct of_device_id host1x_drm_subdevs[] = {
1312         { .compatible = "nvidia,tegra20-dc", },
1313         { .compatible = "nvidia,tegra20-hdmi", },
1314         { .compatible = "nvidia,tegra20-gr2d", },
1315         { .compatible = "nvidia,tegra20-gr3d", },
1316         { .compatible = "nvidia,tegra30-dc", },
1317         { .compatible = "nvidia,tegra30-hdmi", },
1318         { .compatible = "nvidia,tegra30-gr2d", },
1319         { .compatible = "nvidia,tegra30-gr3d", },
1320         { .compatible = "nvidia,tegra114-dc", },
1321         { .compatible = "nvidia,tegra114-dsi", },
1322         { .compatible = "nvidia,tegra114-hdmi", },
1323         { .compatible = "nvidia,tegra114-gr2d", },
1324         { .compatible = "nvidia,tegra114-gr3d", },
1325         { .compatible = "nvidia,tegra124-dc", },
1326         { .compatible = "nvidia,tegra124-sor", },
1327         { .compatible = "nvidia,tegra124-hdmi", },
1328         { .compatible = "nvidia,tegra124-dsi", },
1329         { .compatible = "nvidia,tegra124-vic", },
1330         { .compatible = "nvidia,tegra132-dsi", },
1331         { .compatible = "nvidia,tegra210-dc", },
1332         { .compatible = "nvidia,tegra210-dsi", },
1333         { .compatible = "nvidia,tegra210-sor", },
1334         { .compatible = "nvidia,tegra210-sor1", },
1335         { .compatible = "nvidia,tegra210-vic", },
1336         { .compatible = "nvidia,tegra186-display", },
1337         { .compatible = "nvidia,tegra186-dc", },
1338         { .compatible = "nvidia,tegra186-sor", },
1339         { .compatible = "nvidia,tegra186-sor1", },
1340         { .compatible = "nvidia,tegra186-vic", },
1341         { .compatible = "nvidia,tegra194-display", },
1342         { .compatible = "nvidia,tegra194-dc", },
1343         { .compatible = "nvidia,tegra194-sor", },
1344         { .compatible = "nvidia,tegra194-vic", },
1345         { /* sentinel */ }
1346 };
1347
1348 static struct host1x_driver host1x_drm_driver = {
1349         .driver = {
1350                 .name = "drm",
1351                 .pm = &host1x_drm_pm_ops,
1352         },
1353         .probe = host1x_drm_probe,
1354         .remove = host1x_drm_remove,
1355         .subdevs = host1x_drm_subdevs,
1356 };
1357
1358 static struct platform_driver * const drivers[] = {
1359         &tegra_display_hub_driver,
1360         &tegra_dc_driver,
1361         &tegra_hdmi_driver,
1362         &tegra_dsi_driver,
1363         &tegra_dpaux_driver,
1364         &tegra_sor_driver,
1365         &tegra_gr2d_driver,
1366         &tegra_gr3d_driver,
1367         &tegra_vic_driver,
1368 };
1369
1370 static int __init host1x_drm_init(void)
1371 {
1372         int err;
1373
1374         err = host1x_driver_register(&host1x_drm_driver);
1375         if (err < 0)
1376                 return err;
1377
1378         err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
1379         if (err < 0)
1380                 goto unregister_host1x;
1381
1382         return 0;
1383
1384 unregister_host1x:
1385         host1x_driver_unregister(&host1x_drm_driver);
1386         return err;
1387 }
1388 module_init(host1x_drm_init);
1389
1390 static void __exit host1x_drm_exit(void)
1391 {
1392         platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
1393         host1x_driver_unregister(&host1x_drm_driver);
1394 }
1395 module_exit(host1x_drm_exit);
1396
1397 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
1398 MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
1399 MODULE_LICENSE("GPL v2");