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