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