blob: 81f86a67c10d28416a3fbe3d69694b8c7dfc34ee [file] [log] [blame]
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001/*
2 * Copyright (C) 2012 Avionic Design GmbH
Mikko Perttunenad926012016-12-14 13:16:11 +02003 * Copyright (C) 2012-2016 NVIDIA CORPORATION. All rights reserved.
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00004 *
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
Mikko Perttunenad926012016-12-14 13:16:11 +020010#include <linux/bitops.h>
Thierry Reding776dc382013-10-14 14:43:22 +020011#include <linux/host1x.h>
Thierry Redingbdd2f9c2017-03-09 20:04:55 +010012#include <linux/idr.h>
Thierry Redingdf06b752014-06-26 21:41:53 +020013#include <linux/iommu.h>
Thierry Reding776dc382013-10-14 14:43:22 +020014
Thierry Reding1503ca42014-11-24 17:41:23 +010015#include <drm/drm_atomic.h>
Thierry Reding07866962014-11-24 17:08:06 +010016#include <drm/drm_atomic_helper.h>
17
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000018#include "drm.h"
Arto Merilainende2ba662013-03-22 16:34:08 +020019#include "gem.h"
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000020
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
Mikko Perttunenad926012016-12-14 13:16:11 +020028#define CARVEOUT_SZ SZ_64M
29
Thierry Reding08943e62013-09-26 16:08:18 +020030struct tegra_drm_file {
Thierry Redingbdd2f9c2017-03-09 20:04:55 +010031 struct idr contexts;
32 struct mutex lock;
Thierry Reding08943e62013-09-26 16:08:18 +020033};
34
Thierry Reding1503ca42014-11-24 17:41:23 +010035static void tegra_atomic_schedule(struct tegra_drm *tegra,
36 struct drm_atomic_state *state)
37{
38 tegra->commit.state = state;
39 schedule_work(&tegra->commit.work);
40}
41
42static void tegra_atomic_complete(struct tegra_drm *tegra,
43 struct drm_atomic_state *state)
44{
45 struct drm_device *drm = tegra->drm;
46
47 /*
48 * Everything below can be run asynchronously without the need to grab
49 * any modeset locks at all under one condition: It must be guaranteed
50 * that the asynchronous work has either been cancelled (if the driver
51 * supports it, which at least requires that the framebuffers get
52 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
53 * before the new state gets committed on the software side with
54 * drm_atomic_helper_swap_state().
55 *
56 * This scheme allows new atomic state updates to be prepared and
57 * checked in parallel to the asynchronous completion of the previous
58 * update. Which is important since compositors need to figure out the
59 * composition of the next frame right after having submitted the
60 * current layout.
61 */
62
Daniel Vetter1af434a2015-02-22 12:24:19 +010063 drm_atomic_helper_commit_modeset_disables(drm, state);
Daniel Vetter1af434a2015-02-22 12:24:19 +010064 drm_atomic_helper_commit_modeset_enables(drm, state);
Liu Ying2b58e982016-08-29 17:12:03 +080065 drm_atomic_helper_commit_planes(drm, state,
66 DRM_PLANE_COMMIT_ACTIVE_ONLY);
Thierry Reding1503ca42014-11-24 17:41:23 +010067
68 drm_atomic_helper_wait_for_vblanks(drm, state);
69
70 drm_atomic_helper_cleanup_planes(drm, state);
Chris Wilson08536952016-10-14 13:18:18 +010071 drm_atomic_state_put(state);
Thierry Reding1503ca42014-11-24 17:41:23 +010072}
73
74static void tegra_atomic_work(struct work_struct *work)
75{
76 struct tegra_drm *tegra = container_of(work, struct tegra_drm,
77 commit.work);
78
79 tegra_atomic_complete(tegra, tegra->commit.state);
80}
81
82static int tegra_atomic_commit(struct drm_device *drm,
Maarten Lankhorst2dacdd72016-04-26 16:11:42 +020083 struct drm_atomic_state *state, bool nonblock)
Thierry Reding1503ca42014-11-24 17:41:23 +010084{
85 struct tegra_drm *tegra = drm->dev_private;
86 int err;
87
88 err = drm_atomic_helper_prepare_planes(drm, state);
89 if (err)
90 return err;
91
Maarten Lankhorst2dacdd72016-04-26 16:11:42 +020092 /* serialize outstanding nonblocking commits */
Thierry Reding1503ca42014-11-24 17:41:23 +010093 mutex_lock(&tegra->commit.lock);
94 flush_work(&tegra->commit.work);
95
96 /*
97 * This is the point of no return - everything below never fails except
98 * when the hw goes bonghits. Which means we can commit the new state on
99 * the software side now.
100 */
101
Daniel Vetter5e84c262016-06-10 00:06:32 +0200102 drm_atomic_helper_swap_state(state, true);
Thierry Reding1503ca42014-11-24 17:41:23 +0100103
Chris Wilson08536952016-10-14 13:18:18 +0100104 drm_atomic_state_get(state);
Maarten Lankhorst2dacdd72016-04-26 16:11:42 +0200105 if (nonblock)
Thierry Reding1503ca42014-11-24 17:41:23 +0100106 tegra_atomic_schedule(tegra, state);
107 else
108 tegra_atomic_complete(tegra, state);
109
110 mutex_unlock(&tegra->commit.lock);
111 return 0;
112}
113
Thierry Redingf9914212014-11-26 13:03:57 +0100114static const struct drm_mode_config_funcs tegra_drm_mode_funcs = {
115 .fb_create = tegra_fb_create,
Archit Tanejab110ef32015-10-27 13:40:59 +0530116#ifdef CONFIG_DRM_FBDEV_EMULATION
Thierry Redingf9914212014-11-26 13:03:57 +0100117 .output_poll_changed = tegra_fb_output_poll_changed,
118#endif
Thierry Reding07866962014-11-24 17:08:06 +0100119 .atomic_check = drm_atomic_helper_check,
Thierry Reding1503ca42014-11-24 17:41:23 +0100120 .atomic_commit = tegra_atomic_commit,
Thierry Redingf9914212014-11-26 13:03:57 +0100121};
122
Thierry Reding776dc382013-10-14 14:43:22 +0200123static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000124{
Thierry Reding776dc382013-10-14 14:43:22 +0200125 struct host1x_device *device = to_host1x_device(drm->dev);
Thierry Reding386a2a72013-09-24 13:22:17 +0200126 struct tegra_drm *tegra;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000127 int err;
128
Thierry Reding776dc382013-10-14 14:43:22 +0200129 tegra = kzalloc(sizeof(*tegra), GFP_KERNEL);
Thierry Reding386a2a72013-09-24 13:22:17 +0200130 if (!tegra)
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200131 return -ENOMEM;
132
Thierry Redingdf06b752014-06-26 21:41:53 +0200133 if (iommu_present(&platform_bus_type)) {
Mikko Perttunenad926012016-12-14 13:16:11 +0200134 u64 carveout_start, carveout_end, gem_start, gem_end;
Thierry Reding4553f732015-01-19 16:15:04 +0100135 struct iommu_domain_geometry *geometry;
Mikko Perttunenad926012016-12-14 13:16:11 +0200136 unsigned long order;
Thierry Reding4553f732015-01-19 16:15:04 +0100137
Thierry Redingdf06b752014-06-26 21:41:53 +0200138 tegra->domain = iommu_domain_alloc(&platform_bus_type);
Dan Carpenterbf19b882014-12-04 14:00:35 +0300139 if (!tegra->domain) {
140 err = -ENOMEM;
Thierry Redingdf06b752014-06-26 21:41:53 +0200141 goto free;
142 }
143
Thierry Reding4553f732015-01-19 16:15:04 +0100144 geometry = &tegra->domain->geometry;
Mikko Perttunenad926012016-12-14 13:16:11 +0200145 gem_start = geometry->aperture_start;
146 gem_end = geometry->aperture_end - CARVEOUT_SZ;
147 carveout_start = gem_end + 1;
148 carveout_end = geometry->aperture_end;
Thierry Reding4553f732015-01-19 16:15:04 +0100149
Mikko Perttunenad926012016-12-14 13:16:11 +0200150 order = __ffs(tegra->domain->pgsize_bitmap);
151 init_iova_domain(&tegra->carveout.domain, 1UL << order,
152 carveout_start >> order,
153 carveout_end >> order);
154
155 tegra->carveout.shift = iova_shift(&tegra->carveout.domain);
156 tegra->carveout.limit = carveout_end >> tegra->carveout.shift;
157
158 drm_mm_init(&tegra->mm, gem_start, gem_end - gem_start + 1);
Thierry Reding347ad49d2017-03-09 20:04:56 +0100159 mutex_init(&tegra->mm_lock);
Mikko Perttunenad926012016-12-14 13:16:11 +0200160
161 DRM_DEBUG("IOMMU apertures:\n");
162 DRM_DEBUG(" GEM: %#llx-%#llx\n", gem_start, gem_end);
163 DRM_DEBUG(" Carveout: %#llx-%#llx\n", carveout_start,
164 carveout_end);
Thierry Redingdf06b752014-06-26 21:41:53 +0200165 }
166
Thierry Reding386a2a72013-09-24 13:22:17 +0200167 mutex_init(&tegra->clients_lock);
168 INIT_LIST_HEAD(&tegra->clients);
Thierry Reding1503ca42014-11-24 17:41:23 +0100169
170 mutex_init(&tegra->commit.lock);
171 INIT_WORK(&tegra->commit.work, tegra_atomic_work);
172
Thierry Reding386a2a72013-09-24 13:22:17 +0200173 drm->dev_private = tegra;
174 tegra->drm = drm;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000175
176 drm_mode_config_init(drm);
177
Thierry Redingf9914212014-11-26 13:03:57 +0100178 drm->mode_config.min_width = 0;
179 drm->mode_config.min_height = 0;
180
181 drm->mode_config.max_width = 4096;
182 drm->mode_config.max_height = 4096;
183
Alexandre Courbot5e911442016-11-08 16:50:42 +0900184 drm->mode_config.allow_fb_modifiers = true;
185
Thierry Redingf9914212014-11-26 13:03:57 +0100186 drm->mode_config.funcs = &tegra_drm_mode_funcs;
187
Thierry Redinge2215322014-06-27 17:19:25 +0200188 err = tegra_drm_fb_prepare(drm);
189 if (err < 0)
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100190 goto config;
Thierry Redinge2215322014-06-27 17:19:25 +0200191
192 drm_kms_helper_poll_init(drm);
193
Thierry Reding776dc382013-10-14 14:43:22 +0200194 err = host1x_device_init(device);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000195 if (err < 0)
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100196 goto fbdev;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000197
Thierry Reding603f0cc2013-04-22 21:22:14 +0200198 /*
199 * We don't use the drm_irq_install() helpers provided by the DRM
200 * core, so we need to set this manually in order to allow the
201 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
202 */
Ville Syrjälä44238432013-10-04 14:53:37 +0300203 drm->irq_enabled = true;
Thierry Reding603f0cc2013-04-22 21:22:14 +0200204
Thierry Reding42e9ce02015-01-28 14:43:05 +0100205 /* syncpoints are used for full 32-bit hardware VBLANK counters */
Thierry Reding42e9ce02015-01-28 14:43:05 +0100206 drm->max_vblank_count = 0xffffffff;
207
Thierry Reding6e5ff992012-11-28 11:45:47 +0100208 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
209 if (err < 0)
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100210 goto device;
Thierry Reding6e5ff992012-11-28 11:45:47 +0100211
Thierry Reding31930d42015-07-02 17:04:06 +0200212 drm_mode_config_reset(drm);
213
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000214 err = tegra_drm_fb_init(drm);
215 if (err < 0)
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100216 goto vblank;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000217
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000218 return 0;
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100219
220vblank:
221 drm_vblank_cleanup(drm);
222device:
223 host1x_device_exit(device);
224fbdev:
225 drm_kms_helper_poll_fini(drm);
226 tegra_drm_fb_free(drm);
227config:
228 drm_mode_config_cleanup(drm);
Thierry Redingdf06b752014-06-26 21:41:53 +0200229
230 if (tegra->domain) {
231 iommu_domain_free(tegra->domain);
232 drm_mm_takedown(&tegra->mm);
Thierry Reding347ad49d2017-03-09 20:04:56 +0100233 mutex_destroy(&tegra->mm_lock);
Mikko Perttunenad926012016-12-14 13:16:11 +0200234 put_iova_domain(&tegra->carveout.domain);
Thierry Redingdf06b752014-06-26 21:41:53 +0200235 }
236free:
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100237 kfree(tegra);
238 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000239}
240
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -0200241static void tegra_drm_unload(struct drm_device *drm)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000242{
Thierry Reding776dc382013-10-14 14:43:22 +0200243 struct host1x_device *device = to_host1x_device(drm->dev);
Thierry Redingdf06b752014-06-26 21:41:53 +0200244 struct tegra_drm *tegra = drm->dev_private;
Thierry Reding776dc382013-10-14 14:43:22 +0200245 int err;
246
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000247 drm_kms_helper_poll_fini(drm);
248 tegra_drm_fb_exit(drm);
Thierry Redingf002abc2013-10-14 14:06:02 +0200249 drm_mode_config_cleanup(drm);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100250 drm_vblank_cleanup(drm);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000251
Thierry Reding776dc382013-10-14 14:43:22 +0200252 err = host1x_device_exit(device);
253 if (err < 0)
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -0200254 return;
Thierry Reding776dc382013-10-14 14:43:22 +0200255
Thierry Redingdf06b752014-06-26 21:41:53 +0200256 if (tegra->domain) {
257 iommu_domain_free(tegra->domain);
258 drm_mm_takedown(&tegra->mm);
Thierry Reding347ad49d2017-03-09 20:04:56 +0100259 mutex_destroy(&tegra->mm_lock);
Mikko Perttunenad926012016-12-14 13:16:11 +0200260 put_iova_domain(&tegra->carveout.domain);
Thierry Redingdf06b752014-06-26 21:41:53 +0200261 }
262
Thierry Reding1053f4dd2014-11-04 16:17:55 +0100263 kfree(tegra);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000264}
265
266static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
267{
Thierry Reding08943e62013-09-26 16:08:18 +0200268 struct tegra_drm_file *fpriv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200269
270 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
271 if (!fpriv)
272 return -ENOMEM;
273
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100274 idr_init(&fpriv->contexts);
275 mutex_init(&fpriv->lock);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200276 filp->driver_priv = fpriv;
277
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000278 return 0;
279}
280
Thierry Redingc88c3632013-09-26 16:08:22 +0200281static void tegra_drm_context_free(struct tegra_drm_context *context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200282{
283 context->client->ops->close_channel(context);
284 kfree(context);
285}
286
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000287static void tegra_drm_lastclose(struct drm_device *drm)
288{
Archit Tanejab110ef32015-10-27 13:40:59 +0530289#ifdef CONFIG_DRM_FBDEV_EMULATION
Thierry Reding386a2a72013-09-24 13:22:17 +0200290 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000291
Thierry Reding386a2a72013-09-24 13:22:17 +0200292 tegra_fbdev_restore_mode(tegra->fbdev);
Thierry Reding60c2f702013-10-31 13:28:50 +0100293#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000294}
295
Thierry Redingc40f0f12013-10-10 11:00:33 +0200296static struct host1x_bo *
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100297host1x_bo_lookup(struct drm_file *file, u32 handle)
Thierry Redingc40f0f12013-10-10 11:00:33 +0200298{
299 struct drm_gem_object *gem;
300 struct tegra_bo *bo;
301
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100302 gem = drm_gem_object_lookup(file, handle);
Thierry Redingc40f0f12013-10-10 11:00:33 +0200303 if (!gem)
304 return NULL;
305
Daniel Vettera07cdfe2015-11-23 10:32:48 +0100306 drm_gem_object_unreference_unlocked(gem);
Thierry Redingc40f0f12013-10-10 11:00:33 +0200307
308 bo = to_tegra_bo(gem);
309 return &bo->base;
310}
311
Thierry Reding961e3be2014-06-10 10:25:00 +0200312static int host1x_reloc_copy_from_user(struct host1x_reloc *dest,
313 struct drm_tegra_reloc __user *src,
314 struct drm_device *drm,
315 struct drm_file *file)
316{
317 u32 cmdbuf, target;
318 int err;
319
320 err = get_user(cmdbuf, &src->cmdbuf.handle);
321 if (err < 0)
322 return err;
323
324 err = get_user(dest->cmdbuf.offset, &src->cmdbuf.offset);
325 if (err < 0)
326 return err;
327
328 err = get_user(target, &src->target.handle);
329 if (err < 0)
330 return err;
331
David Ung31f40f82015-01-20 18:37:35 -0800332 err = get_user(dest->target.offset, &src->target.offset);
Thierry Reding961e3be2014-06-10 10:25:00 +0200333 if (err < 0)
334 return err;
335
336 err = get_user(dest->shift, &src->shift);
337 if (err < 0)
338 return err;
339
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100340 dest->cmdbuf.bo = host1x_bo_lookup(file, cmdbuf);
Thierry Reding961e3be2014-06-10 10:25:00 +0200341 if (!dest->cmdbuf.bo)
342 return -ENOENT;
343
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100344 dest->target.bo = host1x_bo_lookup(file, target);
Thierry Reding961e3be2014-06-10 10:25:00 +0200345 if (!dest->target.bo)
346 return -ENOENT;
347
348 return 0;
349}
350
Thierry Redingc40f0f12013-10-10 11:00:33 +0200351int tegra_drm_submit(struct tegra_drm_context *context,
352 struct drm_tegra_submit *args, struct drm_device *drm,
353 struct drm_file *file)
354{
355 unsigned int num_cmdbufs = args->num_cmdbufs;
356 unsigned int num_relocs = args->num_relocs;
357 unsigned int num_waitchks = args->num_waitchks;
358 struct drm_tegra_cmdbuf __user *cmdbufs =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100359 (void __user *)(uintptr_t)args->cmdbufs;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200360 struct drm_tegra_reloc __user *relocs =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100361 (void __user *)(uintptr_t)args->relocs;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200362 struct drm_tegra_waitchk __user *waitchks =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100363 (void __user *)(uintptr_t)args->waitchks;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200364 struct drm_tegra_syncpt syncpt;
365 struct host1x_job *job;
366 int err;
367
368 /* We don't yet support other than one syncpt_incr struct per submit */
369 if (args->num_syncpts != 1)
370 return -EINVAL;
371
372 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
373 args->num_relocs, args->num_waitchks);
374 if (!job)
375 return -ENOMEM;
376
377 job->num_relocs = args->num_relocs;
378 job->num_waitchk = args->num_waitchks;
379 job->client = (u32)args->context;
380 job->class = context->client->base.class;
381 job->serialize = true;
382
383 while (num_cmdbufs) {
384 struct drm_tegra_cmdbuf cmdbuf;
385 struct host1x_bo *bo;
386
Dan Carpenter9a991602013-11-08 13:07:37 +0300387 if (copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf))) {
388 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200389 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300390 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200391
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100392 bo = host1x_bo_lookup(file, cmdbuf.handle);
Thierry Redingc40f0f12013-10-10 11:00:33 +0200393 if (!bo) {
394 err = -ENOENT;
395 goto fail;
396 }
397
398 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
399 num_cmdbufs--;
400 cmdbufs++;
401 }
402
Thierry Reding961e3be2014-06-10 10:25:00 +0200403 /* copy and resolve relocations from submit */
Thierry Redingc40f0f12013-10-10 11:00:33 +0200404 while (num_relocs--) {
Thierry Reding961e3be2014-06-10 10:25:00 +0200405 err = host1x_reloc_copy_from_user(&job->relocarray[num_relocs],
406 &relocs[num_relocs], drm,
407 file);
408 if (err < 0)
Thierry Redingc40f0f12013-10-10 11:00:33 +0200409 goto fail;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200410 }
411
Dan Carpenter9a991602013-11-08 13:07:37 +0300412 if (copy_from_user(job->waitchk, waitchks,
413 sizeof(*waitchks) * num_waitchks)) {
414 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200415 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300416 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200417
Dan Carpenter9a991602013-11-08 13:07:37 +0300418 if (copy_from_user(&syncpt, (void __user *)(uintptr_t)args->syncpts,
419 sizeof(syncpt))) {
420 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200421 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300422 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200423
424 job->is_addr_reg = context->client->ops->is_addr_reg;
425 job->syncpt_incrs = syncpt.incrs;
426 job->syncpt_id = syncpt.id;
427 job->timeout = 10000;
428
429 if (args->timeout && args->timeout < 10000)
430 job->timeout = args->timeout;
431
432 err = host1x_job_pin(job, context->client->base.dev);
433 if (err)
434 goto fail;
435
436 err = host1x_job_submit(job);
437 if (err)
438 goto fail_submit;
439
440 args->fence = job->syncpt_end;
441
442 host1x_job_put(job);
443 return 0;
444
445fail_submit:
446 host1x_job_unpin(job);
447fail:
448 host1x_job_put(job);
449 return err;
450}
451
452
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200453#ifdef CONFIG_DRM_TEGRA_STAGING
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200454static int tegra_gem_create(struct drm_device *drm, void *data,
455 struct drm_file *file)
456{
457 struct drm_tegra_gem_create *args = data;
458 struct tegra_bo *bo;
459
Thierry Reding773af772013-10-04 22:34:01 +0200460 bo = tegra_bo_create_with_handle(file, drm, args->size, args->flags,
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200461 &args->handle);
462 if (IS_ERR(bo))
463 return PTR_ERR(bo);
464
465 return 0;
466}
467
468static int tegra_gem_mmap(struct drm_device *drm, void *data,
469 struct drm_file *file)
470{
471 struct drm_tegra_gem_mmap *args = data;
472 struct drm_gem_object *gem;
473 struct tegra_bo *bo;
474
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100475 gem = drm_gem_object_lookup(file, args->handle);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200476 if (!gem)
477 return -EINVAL;
478
479 bo = to_tegra_bo(gem);
480
David Herrmann2bc7b0c2013-08-13 14:19:58 +0200481 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200482
Daniel Vetter11533302015-11-23 10:32:40 +0100483 drm_gem_object_unreference_unlocked(gem);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200484
485 return 0;
486}
487
488static int tegra_syncpt_read(struct drm_device *drm, void *data,
489 struct drm_file *file)
490{
Thierry Reding776dc382013-10-14 14:43:22 +0200491 struct host1x *host = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200492 struct drm_tegra_syncpt_read *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200493 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200494
Thierry Reding776dc382013-10-14 14:43:22 +0200495 sp = host1x_syncpt_get(host, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200496 if (!sp)
497 return -EINVAL;
498
499 args->value = host1x_syncpt_read_min(sp);
500 return 0;
501}
502
503static int tegra_syncpt_incr(struct drm_device *drm, void *data,
504 struct drm_file *file)
505{
Thierry Reding776dc382013-10-14 14:43:22 +0200506 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200507 struct drm_tegra_syncpt_incr *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200508 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200509
Thierry Reding776dc382013-10-14 14:43:22 +0200510 sp = host1x_syncpt_get(host1x, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200511 if (!sp)
512 return -EINVAL;
513
Arto Merilainenebae30b2013-05-29 13:26:08 +0300514 return host1x_syncpt_incr(sp);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200515}
516
517static int tegra_syncpt_wait(struct drm_device *drm, void *data,
518 struct drm_file *file)
519{
Thierry Reding776dc382013-10-14 14:43:22 +0200520 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200521 struct drm_tegra_syncpt_wait *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200522 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200523
Thierry Reding776dc382013-10-14 14:43:22 +0200524 sp = host1x_syncpt_get(host1x, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200525 if (!sp)
526 return -EINVAL;
527
528 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
529 &args->value);
530}
531
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100532static int tegra_client_open(struct tegra_drm_file *fpriv,
533 struct tegra_drm_client *client,
534 struct tegra_drm_context *context)
535{
536 int err;
537
538 err = client->ops->open_channel(client, context);
539 if (err < 0)
540 return err;
541
Dmitry Osipenkod6c153e2017-06-15 02:18:25 +0300542 err = idr_alloc(&fpriv->contexts, context, 1, 0, GFP_KERNEL);
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100543 if (err < 0) {
544 client->ops->close_channel(context);
545 return err;
546 }
547
548 context->client = client;
549 context->id = err;
550
551 return 0;
552}
553
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200554static int tegra_open_channel(struct drm_device *drm, void *data,
555 struct drm_file *file)
556{
Thierry Reding08943e62013-09-26 16:08:18 +0200557 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding386a2a72013-09-24 13:22:17 +0200558 struct tegra_drm *tegra = drm->dev_private;
559 struct drm_tegra_open_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200560 struct tegra_drm_context *context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200561 struct tegra_drm_client *client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200562 int err = -ENODEV;
563
564 context = kzalloc(sizeof(*context), GFP_KERNEL);
565 if (!context)
566 return -ENOMEM;
567
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100568 mutex_lock(&fpriv->lock);
569
Thierry Reding776dc382013-10-14 14:43:22 +0200570 list_for_each_entry(client, &tegra->clients, list)
Thierry Reding53fa7f72013-09-24 15:35:40 +0200571 if (client->base.class == args->client) {
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100572 err = tegra_client_open(fpriv, client, context);
573 if (err < 0)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200574 break;
575
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100576 args->context = context->id;
577 break;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200578 }
579
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100580 if (err < 0)
581 kfree(context);
582
583 mutex_unlock(&fpriv->lock);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200584 return err;
585}
586
587static int tegra_close_channel(struct drm_device *drm, void *data,
588 struct drm_file *file)
589{
Thierry Reding08943e62013-09-26 16:08:18 +0200590 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding776dc382013-10-14 14:43:22 +0200591 struct drm_tegra_close_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200592 struct tegra_drm_context *context;
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100593 int err = 0;
Thierry Redingc88c3632013-09-26 16:08:22 +0200594
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100595 mutex_lock(&fpriv->lock);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200596
Dmitry Osipenko1066a892017-06-15 02:18:24 +0300597 context = idr_find(&fpriv->contexts, args->context);
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100598 if (!context) {
599 err = -EINVAL;
600 goto unlock;
601 }
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200602
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100603 idr_remove(&fpriv->contexts, context->id);
Thierry Redingc88c3632013-09-26 16:08:22 +0200604 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200605
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100606unlock:
607 mutex_unlock(&fpriv->lock);
608 return err;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200609}
610
611static int tegra_get_syncpt(struct drm_device *drm, void *data,
612 struct drm_file *file)
613{
Thierry Reding08943e62013-09-26 16:08:18 +0200614 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200615 struct drm_tegra_get_syncpt *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200616 struct tegra_drm_context *context;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200617 struct host1x_syncpt *syncpt;
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100618 int err = 0;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200619
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100620 mutex_lock(&fpriv->lock);
Thierry Redingc88c3632013-09-26 16:08:22 +0200621
Dmitry Osipenko1066a892017-06-15 02:18:24 +0300622 context = idr_find(&fpriv->contexts, args->context);
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100623 if (!context) {
624 err = -ENODEV;
625 goto unlock;
626 }
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200627
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100628 if (args->index >= context->client->base.num_syncpts) {
629 err = -EINVAL;
630 goto unlock;
631 }
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200632
Thierry Reding53fa7f72013-09-24 15:35:40 +0200633 syncpt = context->client->base.syncpts[args->index];
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200634 args->id = host1x_syncpt_id(syncpt);
635
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100636unlock:
637 mutex_unlock(&fpriv->lock);
638 return err;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200639}
640
641static int tegra_submit(struct drm_device *drm, void *data,
642 struct drm_file *file)
643{
Thierry Reding08943e62013-09-26 16:08:18 +0200644 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200645 struct drm_tegra_submit *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200646 struct tegra_drm_context *context;
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100647 int err;
Thierry Redingc88c3632013-09-26 16:08:22 +0200648
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100649 mutex_lock(&fpriv->lock);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200650
Dmitry Osipenko1066a892017-06-15 02:18:24 +0300651 context = idr_find(&fpriv->contexts, args->context);
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100652 if (!context) {
653 err = -ENODEV;
654 goto unlock;
655 }
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200656
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100657 err = context->client->ops->submit(context, args, drm, file);
658
659unlock:
660 mutex_unlock(&fpriv->lock);
661 return err;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200662}
Arto Merilainenc54a1692013-10-14 15:21:54 +0300663
664static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
665 struct drm_file *file)
666{
667 struct tegra_drm_file *fpriv = file->driver_priv;
668 struct drm_tegra_get_syncpt_base *args = data;
669 struct tegra_drm_context *context;
670 struct host1x_syncpt_base *base;
671 struct host1x_syncpt *syncpt;
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100672 int err = 0;
Arto Merilainenc54a1692013-10-14 15:21:54 +0300673
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100674 mutex_lock(&fpriv->lock);
Arto Merilainenc54a1692013-10-14 15:21:54 +0300675
Dmitry Osipenko1066a892017-06-15 02:18:24 +0300676 context = idr_find(&fpriv->contexts, args->context);
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100677 if (!context) {
678 err = -ENODEV;
679 goto unlock;
680 }
Arto Merilainenc54a1692013-10-14 15:21:54 +0300681
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100682 if (args->syncpt >= context->client->base.num_syncpts) {
683 err = -EINVAL;
684 goto unlock;
685 }
Arto Merilainenc54a1692013-10-14 15:21:54 +0300686
687 syncpt = context->client->base.syncpts[args->syncpt];
688
689 base = host1x_syncpt_get_base(syncpt);
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100690 if (!base) {
691 err = -ENXIO;
692 goto unlock;
693 }
Arto Merilainenc54a1692013-10-14 15:21:54 +0300694
695 args->id = host1x_syncpt_base_id(base);
696
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100697unlock:
698 mutex_unlock(&fpriv->lock);
699 return err;
Arto Merilainenc54a1692013-10-14 15:21:54 +0300700}
Thierry Reding7678d712014-06-03 14:56:57 +0200701
702static int tegra_gem_set_tiling(struct drm_device *drm, void *data,
703 struct drm_file *file)
704{
705 struct drm_tegra_gem_set_tiling *args = data;
706 enum tegra_bo_tiling_mode mode;
707 struct drm_gem_object *gem;
708 unsigned long value = 0;
709 struct tegra_bo *bo;
710
711 switch (args->mode) {
712 case DRM_TEGRA_GEM_TILING_MODE_PITCH:
713 mode = TEGRA_BO_TILING_MODE_PITCH;
714
715 if (args->value != 0)
716 return -EINVAL;
717
718 break;
719
720 case DRM_TEGRA_GEM_TILING_MODE_TILED:
721 mode = TEGRA_BO_TILING_MODE_TILED;
722
723 if (args->value != 0)
724 return -EINVAL;
725
726 break;
727
728 case DRM_TEGRA_GEM_TILING_MODE_BLOCK:
729 mode = TEGRA_BO_TILING_MODE_BLOCK;
730
731 if (args->value > 5)
732 return -EINVAL;
733
734 value = args->value;
735 break;
736
737 default:
738 return -EINVAL;
739 }
740
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100741 gem = drm_gem_object_lookup(file, args->handle);
Thierry Reding7678d712014-06-03 14:56:57 +0200742 if (!gem)
743 return -ENOENT;
744
745 bo = to_tegra_bo(gem);
746
747 bo->tiling.mode = mode;
748 bo->tiling.value = value;
749
Daniel Vetter11533302015-11-23 10:32:40 +0100750 drm_gem_object_unreference_unlocked(gem);
Thierry Reding7678d712014-06-03 14:56:57 +0200751
752 return 0;
753}
754
755static int tegra_gem_get_tiling(struct drm_device *drm, void *data,
756 struct drm_file *file)
757{
758 struct drm_tegra_gem_get_tiling *args = data;
759 struct drm_gem_object *gem;
760 struct tegra_bo *bo;
761 int err = 0;
762
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100763 gem = drm_gem_object_lookup(file, args->handle);
Thierry Reding7678d712014-06-03 14:56:57 +0200764 if (!gem)
765 return -ENOENT;
766
767 bo = to_tegra_bo(gem);
768
769 switch (bo->tiling.mode) {
770 case TEGRA_BO_TILING_MODE_PITCH:
771 args->mode = DRM_TEGRA_GEM_TILING_MODE_PITCH;
772 args->value = 0;
773 break;
774
775 case TEGRA_BO_TILING_MODE_TILED:
776 args->mode = DRM_TEGRA_GEM_TILING_MODE_TILED;
777 args->value = 0;
778 break;
779
780 case TEGRA_BO_TILING_MODE_BLOCK:
781 args->mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
782 args->value = bo->tiling.value;
783 break;
784
785 default:
786 err = -EINVAL;
787 break;
788 }
789
Daniel Vetter11533302015-11-23 10:32:40 +0100790 drm_gem_object_unreference_unlocked(gem);
Thierry Reding7678d712014-06-03 14:56:57 +0200791
792 return err;
793}
Thierry Reding7b129082014-06-10 12:04:03 +0200794
795static int tegra_gem_set_flags(struct drm_device *drm, void *data,
796 struct drm_file *file)
797{
798 struct drm_tegra_gem_set_flags *args = data;
799 struct drm_gem_object *gem;
800 struct tegra_bo *bo;
801
802 if (args->flags & ~DRM_TEGRA_GEM_FLAGS)
803 return -EINVAL;
804
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100805 gem = drm_gem_object_lookup(file, args->handle);
Thierry Reding7b129082014-06-10 12:04:03 +0200806 if (!gem)
807 return -ENOENT;
808
809 bo = to_tegra_bo(gem);
810 bo->flags = 0;
811
812 if (args->flags & DRM_TEGRA_GEM_BOTTOM_UP)
813 bo->flags |= TEGRA_BO_BOTTOM_UP;
814
Daniel Vetter11533302015-11-23 10:32:40 +0100815 drm_gem_object_unreference_unlocked(gem);
Thierry Reding7b129082014-06-10 12:04:03 +0200816
817 return 0;
818}
819
820static int tegra_gem_get_flags(struct drm_device *drm, void *data,
821 struct drm_file *file)
822{
823 struct drm_tegra_gem_get_flags *args = data;
824 struct drm_gem_object *gem;
825 struct tegra_bo *bo;
826
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100827 gem = drm_gem_object_lookup(file, args->handle);
Thierry Reding7b129082014-06-10 12:04:03 +0200828 if (!gem)
829 return -ENOENT;
830
831 bo = to_tegra_bo(gem);
832 args->flags = 0;
833
834 if (bo->flags & TEGRA_BO_BOTTOM_UP)
835 args->flags |= DRM_TEGRA_GEM_BOTTOM_UP;
836
Daniel Vetter11533302015-11-23 10:32:40 +0100837 drm_gem_object_unreference_unlocked(gem);
Thierry Reding7b129082014-06-10 12:04:03 +0200838
839 return 0;
840}
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200841#endif
842
Rob Clarkbaa70942013-08-02 13:27:49 -0400843static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200844#ifdef CONFIG_DRM_TEGRA_STAGING
Daniel Vetterf8c47142015-09-08 13:56:30 +0200845 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, 0),
846 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, 0),
847 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, 0),
848 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, 0),
849 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, 0),
850 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, 0),
851 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, 0),
852 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, 0),
853 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, 0),
854 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base, 0),
855 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_TILING, tegra_gem_set_tiling, 0),
856 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_TILING, tegra_gem_get_tiling, 0),
857 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_FLAGS, tegra_gem_set_flags, 0),
858 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_FLAGS, tegra_gem_get_flags, 0),
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200859#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000860};
861
862static const struct file_operations tegra_drm_fops = {
863 .owner = THIS_MODULE,
864 .open = drm_open,
865 .release = drm_release,
866 .unlocked_ioctl = drm_ioctl,
Arto Merilainende2ba662013-03-22 16:34:08 +0200867 .mmap = tegra_drm_mmap,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000868 .poll = drm_poll,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000869 .read = drm_read,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000870 .compat_ioctl = drm_compat_ioctl,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000871 .llseek = noop_llseek,
872};
873
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100874static int tegra_drm_context_cleanup(int id, void *p, void *data)
875{
876 struct tegra_drm_context *context = p;
877
878 tegra_drm_context_free(context);
879
880 return 0;
881}
882
Thierry Reding3c03c462012-11-28 12:00:18 +0100883static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
884{
Thierry Reding08943e62013-09-26 16:08:18 +0200885 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding3c03c462012-11-28 12:00:18 +0100886
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100887 mutex_lock(&fpriv->lock);
888 idr_for_each(&fpriv->contexts, tegra_drm_context_cleanup, NULL);
889 mutex_unlock(&fpriv->lock);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200890
Thierry Redingbdd2f9c2017-03-09 20:04:55 +0100891 idr_destroy(&fpriv->contexts);
892 mutex_destroy(&fpriv->lock);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200893 kfree(fpriv);
Thierry Reding3c03c462012-11-28 12:00:18 +0100894}
895
Thierry Redinge450fcc2013-02-13 16:13:16 +0100896#ifdef CONFIG_DEBUG_FS
897static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
898{
899 struct drm_info_node *node = (struct drm_info_node *)s->private;
900 struct drm_device *drm = node->minor->dev;
901 struct drm_framebuffer *fb;
902
903 mutex_lock(&drm->mode_config.fb_lock);
904
905 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
906 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200907 fb->base.id, fb->width, fb->height,
908 fb->format->depth,
Ville Syrjälä272725c2016-12-14 23:32:20 +0200909 fb->format->cpp[0] * 8,
Dave Airlie747a5982016-04-15 15:10:35 +1000910 drm_framebuffer_read_refcount(fb));
Thierry Redinge450fcc2013-02-13 16:13:16 +0100911 }
912
913 mutex_unlock(&drm->mode_config.fb_lock);
914
915 return 0;
916}
917
Thierry Reding28c23372015-01-23 09:16:03 +0100918static int tegra_debugfs_iova(struct seq_file *s, void *data)
919{
920 struct drm_info_node *node = (struct drm_info_node *)s->private;
921 struct drm_device *drm = node->minor->dev;
922 struct tegra_drm *tegra = drm->dev_private;
Daniel Vetterb5c37142016-12-29 12:09:24 +0100923 struct drm_printer p = drm_seq_file_printer(s);
Thierry Reding28c23372015-01-23 09:16:03 +0100924
Thierry Reding347ad49d2017-03-09 20:04:56 +0100925 mutex_lock(&tegra->mm_lock);
Daniel Vetterb5c37142016-12-29 12:09:24 +0100926 drm_mm_print(&tegra->mm, &p);
Thierry Reding347ad49d2017-03-09 20:04:56 +0100927 mutex_unlock(&tegra->mm_lock);
Daniel Vetterb5c37142016-12-29 12:09:24 +0100928
929 return 0;
Thierry Reding28c23372015-01-23 09:16:03 +0100930}
931
Thierry Redinge450fcc2013-02-13 16:13:16 +0100932static struct drm_info_list tegra_debugfs_list[] = {
933 { "framebuffers", tegra_debugfs_framebuffers, 0 },
Thierry Reding28c23372015-01-23 09:16:03 +0100934 { "iova", tegra_debugfs_iova, 0 },
Thierry Redinge450fcc2013-02-13 16:13:16 +0100935};
936
937static int tegra_debugfs_init(struct drm_minor *minor)
938{
939 return drm_debugfs_create_files(tegra_debugfs_list,
940 ARRAY_SIZE(tegra_debugfs_list),
941 minor->debugfs_root, minor);
942}
Thierry Redinge450fcc2013-02-13 16:13:16 +0100943#endif
944
Thierry Reding9b57f5f2013-11-08 13:17:14 +0100945static struct drm_driver tegra_drm_driver = {
Thierry Redingad906592015-09-24 18:38:09 +0200946 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
947 DRIVER_ATOMIC,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000948 .load = tegra_drm_load,
949 .unload = tegra_drm_unload,
950 .open = tegra_drm_open,
Thierry Reding3c03c462012-11-28 12:00:18 +0100951 .preclose = tegra_drm_preclose,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000952 .lastclose = tegra_drm_lastclose,
953
Thierry Redinge450fcc2013-02-13 16:13:16 +0100954#if defined(CONFIG_DEBUG_FS)
955 .debugfs_init = tegra_debugfs_init,
Thierry Redinge450fcc2013-02-13 16:13:16 +0100956#endif
957
Daniel Vetter1ddbdbd2016-04-26 19:30:00 +0200958 .gem_free_object_unlocked = tegra_bo_free_object,
Arto Merilainende2ba662013-03-22 16:34:08 +0200959 .gem_vm_ops = &tegra_bo_vm_ops,
Thierry Reding38003912013-12-12 10:00:43 +0100960
961 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
962 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
963 .gem_prime_export = tegra_gem_prime_export,
964 .gem_prime_import = tegra_gem_prime_import,
965
Arto Merilainende2ba662013-03-22 16:34:08 +0200966 .dumb_create = tegra_bo_dumb_create,
967 .dumb_map_offset = tegra_bo_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200968 .dumb_destroy = drm_gem_dumb_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000969
970 .ioctls = tegra_drm_ioctls,
971 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
972 .fops = &tegra_drm_fops,
973
974 .name = DRIVER_NAME,
975 .desc = DRIVER_DESC,
976 .date = DRIVER_DATE,
977 .major = DRIVER_MAJOR,
978 .minor = DRIVER_MINOR,
979 .patchlevel = DRIVER_PATCHLEVEL,
980};
Thierry Reding776dc382013-10-14 14:43:22 +0200981
982int tegra_drm_register_client(struct tegra_drm *tegra,
983 struct tegra_drm_client *client)
984{
985 mutex_lock(&tegra->clients_lock);
986 list_add_tail(&client->list, &tegra->clients);
987 mutex_unlock(&tegra->clients_lock);
988
989 return 0;
990}
991
992int tegra_drm_unregister_client(struct tegra_drm *tegra,
993 struct tegra_drm_client *client)
994{
995 mutex_lock(&tegra->clients_lock);
996 list_del_init(&client->list);
997 mutex_unlock(&tegra->clients_lock);
998
999 return 0;
1000}
1001
Mikko Perttunenad926012016-12-14 13:16:11 +02001002void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size,
1003 dma_addr_t *dma)
1004{
1005 struct iova *alloc;
1006 void *virt;
1007 gfp_t gfp;
1008 int err;
1009
1010 if (tegra->domain)
1011 size = iova_align(&tegra->carveout.domain, size);
1012 else
1013 size = PAGE_ALIGN(size);
1014
1015 gfp = GFP_KERNEL | __GFP_ZERO;
1016 if (!tegra->domain) {
1017 /*
1018 * Many units only support 32-bit addresses, even on 64-bit
1019 * SoCs. If there is no IOMMU to translate into a 32-bit IO
1020 * virtual address space, force allocations to be in the
1021 * lower 32-bit range.
1022 */
1023 gfp |= GFP_DMA;
1024 }
1025
1026 virt = (void *)__get_free_pages(gfp, get_order(size));
1027 if (!virt)
1028 return ERR_PTR(-ENOMEM);
1029
1030 if (!tegra->domain) {
1031 /*
1032 * If IOMMU is disabled, devices address physical memory
1033 * directly.
1034 */
1035 *dma = virt_to_phys(virt);
1036 return virt;
1037 }
1038
1039 alloc = alloc_iova(&tegra->carveout.domain,
1040 size >> tegra->carveout.shift,
1041 tegra->carveout.limit, true);
1042 if (!alloc) {
1043 err = -EBUSY;
1044 goto free_pages;
1045 }
1046
1047 *dma = iova_dma_addr(&tegra->carveout.domain, alloc);
1048 err = iommu_map(tegra->domain, *dma, virt_to_phys(virt),
1049 size, IOMMU_READ | IOMMU_WRITE);
1050 if (err < 0)
1051 goto free_iova;
1052
1053 return virt;
1054
1055free_iova:
1056 __free_iova(&tegra->carveout.domain, alloc);
1057free_pages:
1058 free_pages((unsigned long)virt, get_order(size));
1059
1060 return ERR_PTR(err);
1061}
1062
1063void tegra_drm_free(struct tegra_drm *tegra, size_t size, void *virt,
1064 dma_addr_t dma)
1065{
1066 if (tegra->domain)
1067 size = iova_align(&tegra->carveout.domain, size);
1068 else
1069 size = PAGE_ALIGN(size);
1070
1071 if (tegra->domain) {
1072 iommu_unmap(tegra->domain, dma, size);
1073 free_iova(&tegra->carveout.domain,
1074 iova_pfn(&tegra->carveout.domain, dma));
1075 }
1076
1077 free_pages((unsigned long)virt, get_order(size));
1078}
1079
Thierry Reding9910f5c2014-05-22 09:57:15 +02001080static int host1x_drm_probe(struct host1x_device *dev)
Thierry Reding776dc382013-10-14 14:43:22 +02001081{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001082 struct drm_driver *driver = &tegra_drm_driver;
1083 struct drm_device *drm;
1084 int err;
1085
1086 drm = drm_dev_alloc(driver, &dev->dev);
Tom Gundersen0f288602016-09-21 16:59:19 +02001087 if (IS_ERR(drm))
1088 return PTR_ERR(drm);
Thierry Reding9910f5c2014-05-22 09:57:15 +02001089
Thierry Reding9910f5c2014-05-22 09:57:15 +02001090 dev_set_drvdata(&dev->dev, drm);
1091
1092 err = drm_dev_register(drm, 0);
1093 if (err < 0)
1094 goto unref;
1095
Thierry Reding9910f5c2014-05-22 09:57:15 +02001096 return 0;
1097
1098unref:
1099 drm_dev_unref(drm);
1100 return err;
Thierry Reding776dc382013-10-14 14:43:22 +02001101}
1102
Thierry Reding9910f5c2014-05-22 09:57:15 +02001103static int host1x_drm_remove(struct host1x_device *dev)
Thierry Reding776dc382013-10-14 14:43:22 +02001104{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001105 struct drm_device *drm = dev_get_drvdata(&dev->dev);
1106
1107 drm_dev_unregister(drm);
1108 drm_dev_unref(drm);
Thierry Reding776dc382013-10-14 14:43:22 +02001109
1110 return 0;
1111}
1112
Thierry Reding359ae682014-12-18 17:15:25 +01001113#ifdef CONFIG_PM_SLEEP
1114static int host1x_drm_suspend(struct device *dev)
1115{
1116 struct drm_device *drm = dev_get_drvdata(dev);
Thierry Reding986c58d2015-08-11 13:11:49 +02001117 struct tegra_drm *tegra = drm->dev_private;
Thierry Reding359ae682014-12-18 17:15:25 +01001118
1119 drm_kms_helper_poll_disable(drm);
Thierry Reding986c58d2015-08-11 13:11:49 +02001120 tegra_drm_fb_suspend(drm);
1121
1122 tegra->state = drm_atomic_helper_suspend(drm);
1123 if (IS_ERR(tegra->state)) {
1124 tegra_drm_fb_resume(drm);
1125 drm_kms_helper_poll_enable(drm);
1126 return PTR_ERR(tegra->state);
1127 }
Thierry Reding359ae682014-12-18 17:15:25 +01001128
1129 return 0;
1130}
1131
1132static int host1x_drm_resume(struct device *dev)
1133{
1134 struct drm_device *drm = dev_get_drvdata(dev);
Thierry Reding986c58d2015-08-11 13:11:49 +02001135 struct tegra_drm *tegra = drm->dev_private;
Thierry Reding359ae682014-12-18 17:15:25 +01001136
Thierry Reding986c58d2015-08-11 13:11:49 +02001137 drm_atomic_helper_resume(drm, tegra->state);
1138 tegra_drm_fb_resume(drm);
Thierry Reding359ae682014-12-18 17:15:25 +01001139 drm_kms_helper_poll_enable(drm);
1140
1141 return 0;
1142}
1143#endif
1144
Thierry Redinga13f1dc2015-08-11 13:22:44 +02001145static SIMPLE_DEV_PM_OPS(host1x_drm_pm_ops, host1x_drm_suspend,
1146 host1x_drm_resume);
Thierry Reding359ae682014-12-18 17:15:25 +01001147
Thierry Reding776dc382013-10-14 14:43:22 +02001148static const struct of_device_id host1x_drm_subdevs[] = {
1149 { .compatible = "nvidia,tegra20-dc", },
1150 { .compatible = "nvidia,tegra20-hdmi", },
1151 { .compatible = "nvidia,tegra20-gr2d", },
Thierry Reding5f60ed02013-02-28 08:08:01 +01001152 { .compatible = "nvidia,tegra20-gr3d", },
Thierry Reding776dc382013-10-14 14:43:22 +02001153 { .compatible = "nvidia,tegra30-dc", },
1154 { .compatible = "nvidia,tegra30-hdmi", },
1155 { .compatible = "nvidia,tegra30-gr2d", },
Thierry Reding5f60ed02013-02-28 08:08:01 +01001156 { .compatible = "nvidia,tegra30-gr3d", },
Thierry Redingdec72732013-09-03 08:45:46 +02001157 { .compatible = "nvidia,tegra114-dsi", },
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +02001158 { .compatible = "nvidia,tegra114-hdmi", },
Thierry Reding5f60ed02013-02-28 08:08:01 +01001159 { .compatible = "nvidia,tegra114-gr3d", },
Thierry Reding8620fc62013-12-12 11:03:59 +01001160 { .compatible = "nvidia,tegra124-dc", },
Thierry Reding6b6b6042013-11-15 16:06:05 +01001161 { .compatible = "nvidia,tegra124-sor", },
Thierry Redingfb7be702013-11-15 16:07:32 +01001162 { .compatible = "nvidia,tegra124-hdmi", },
Thierry Reding7d338582015-04-10 11:35:21 +02001163 { .compatible = "nvidia,tegra124-dsi", },
Arto Merilainen0ae797a2016-12-14 13:16:13 +02001164 { .compatible = "nvidia,tegra124-vic", },
Thierry Redingc06c7932015-04-10 11:35:21 +02001165 { .compatible = "nvidia,tegra132-dsi", },
Thierry Reding5b4f5162015-03-27 10:31:58 +01001166 { .compatible = "nvidia,tegra210-dc", },
Thierry Redingddfb4062015-04-08 16:56:22 +02001167 { .compatible = "nvidia,tegra210-dsi", },
Thierry Reding3309ac82015-07-30 10:32:46 +02001168 { .compatible = "nvidia,tegra210-sor", },
Thierry Reding459cc2c2015-07-30 10:34:24 +02001169 { .compatible = "nvidia,tegra210-sor1", },
Arto Merilainen0ae797a2016-12-14 13:16:13 +02001170 { .compatible = "nvidia,tegra210-vic", },
Thierry Reding776dc382013-10-14 14:43:22 +02001171 { /* sentinel */ }
1172};
1173
1174static struct host1x_driver host1x_drm_driver = {
Thierry Redingf4c5cf82014-12-18 15:29:14 +01001175 .driver = {
1176 .name = "drm",
Thierry Reding359ae682014-12-18 17:15:25 +01001177 .pm = &host1x_drm_pm_ops,
Thierry Redingf4c5cf82014-12-18 15:29:14 +01001178 },
Thierry Reding776dc382013-10-14 14:43:22 +02001179 .probe = host1x_drm_probe,
1180 .remove = host1x_drm_remove,
1181 .subdevs = host1x_drm_subdevs,
1182};
1183
Thierry Reding473112e2015-09-10 16:07:14 +02001184static struct platform_driver * const drivers[] = {
1185 &tegra_dc_driver,
1186 &tegra_hdmi_driver,
1187 &tegra_dsi_driver,
1188 &tegra_dpaux_driver,
1189 &tegra_sor_driver,
1190 &tegra_gr2d_driver,
1191 &tegra_gr3d_driver,
Arto Merilainen0ae797a2016-12-14 13:16:13 +02001192 &tegra_vic_driver,
Thierry Reding473112e2015-09-10 16:07:14 +02001193};
1194
Thierry Reding776dc382013-10-14 14:43:22 +02001195static int __init host1x_drm_init(void)
1196{
1197 int err;
1198
1199 err = host1x_driver_register(&host1x_drm_driver);
1200 if (err < 0)
1201 return err;
1202
Thierry Reding473112e2015-09-10 16:07:14 +02001203 err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Thierry Reding776dc382013-10-14 14:43:22 +02001204 if (err < 0)
1205 goto unregister_host1x;
1206
Thierry Reding776dc382013-10-14 14:43:22 +02001207 return 0;
1208
Thierry Reding776dc382013-10-14 14:43:22 +02001209unregister_host1x:
1210 host1x_driver_unregister(&host1x_drm_driver);
1211 return err;
1212}
1213module_init(host1x_drm_init);
1214
1215static void __exit host1x_drm_exit(void)
1216{
Thierry Reding473112e2015-09-10 16:07:14 +02001217 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Thierry Reding776dc382013-10-14 14:43:22 +02001218 host1x_driver_unregister(&host1x_drm_driver);
1219}
1220module_exit(host1x_drm_exit);
1221
1222MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
1223MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
1224MODULE_LICENSE("GPL v2");