blob: e549afeece1ff12c899afc9f21dd52ff5e3e64ff [file] [log] [blame]
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001/*
2 * Copyright (C) 2012 Avionic Design GmbH
Terje Bergstromd43f81c2013-03-22 16:34:09 +02003 * Copyright (C) 2012-2013 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
Thierry Reding776dc382013-10-14 14:43:22 +020010#include <linux/host1x.h>
Thierry Redingdf06b752014-06-26 21:41:53 +020011#include <linux/iommu.h>
Thierry Reding776dc382013-10-14 14:43:22 +020012
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000013#include "drm.h"
Arto Merilainende2ba662013-03-22 16:34:08 +020014#include "gem.h"
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000015
16#define DRIVER_NAME "tegra"
17#define DRIVER_DESC "NVIDIA Tegra graphics"
18#define DRIVER_DATE "20120330"
19#define DRIVER_MAJOR 0
20#define DRIVER_MINOR 0
21#define DRIVER_PATCHLEVEL 0
22
Thierry Reding08943e62013-09-26 16:08:18 +020023struct tegra_drm_file {
24 struct list_head contexts;
25};
26
Thierry Reding776dc382013-10-14 14:43:22 +020027static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000028{
Thierry Reding776dc382013-10-14 14:43:22 +020029 struct host1x_device *device = to_host1x_device(drm->dev);
Thierry Reding386a2a72013-09-24 13:22:17 +020030 struct tegra_drm *tegra;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000031 int err;
32
Thierry Reding776dc382013-10-14 14:43:22 +020033 tegra = kzalloc(sizeof(*tegra), GFP_KERNEL);
Thierry Reding386a2a72013-09-24 13:22:17 +020034 if (!tegra)
Terje Bergstrom692e6d72013-03-22 16:34:07 +020035 return -ENOMEM;
36
Thierry Redingdf06b752014-06-26 21:41:53 +020037 if (iommu_present(&platform_bus_type)) {
38 tegra->domain = iommu_domain_alloc(&platform_bus_type);
39 if (IS_ERR(tegra->domain)) {
40 err = PTR_ERR(tegra->domain);
41 goto free;
42 }
43
44 DRM_DEBUG("IOMMU context initialized\n");
45 drm_mm_init(&tegra->mm, 0, SZ_2G);
46 }
47
Thierry Reding386a2a72013-09-24 13:22:17 +020048 mutex_init(&tegra->clients_lock);
49 INIT_LIST_HEAD(&tegra->clients);
Thierry Reding386a2a72013-09-24 13:22:17 +020050 drm->dev_private = tegra;
51 tegra->drm = drm;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000052
53 drm_mode_config_init(drm);
54
Thierry Redinge2215322014-06-27 17:19:25 +020055 err = tegra_drm_fb_prepare(drm);
56 if (err < 0)
Thierry Reding1d1e6fe2014-11-06 14:12:08 +010057 goto config;
Thierry Redinge2215322014-06-27 17:19:25 +020058
59 drm_kms_helper_poll_init(drm);
60
Thierry Reding776dc382013-10-14 14:43:22 +020061 err = host1x_device_init(device);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000062 if (err < 0)
Thierry Reding1d1e6fe2014-11-06 14:12:08 +010063 goto fbdev;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000064
Thierry Reding603f0cc2013-04-22 21:22:14 +020065 /*
66 * We don't use the drm_irq_install() helpers provided by the DRM
67 * core, so we need to set this manually in order to allow the
68 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
69 */
Ville Syrjälä44238432013-10-04 14:53:37 +030070 drm->irq_enabled = true;
Thierry Reding603f0cc2013-04-22 21:22:14 +020071
Thierry Reding6e5ff992012-11-28 11:45:47 +010072 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
73 if (err < 0)
Thierry Reding1d1e6fe2014-11-06 14:12:08 +010074 goto device;
Thierry Reding6e5ff992012-11-28 11:45:47 +010075
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000076 err = tegra_drm_fb_init(drm);
77 if (err < 0)
Thierry Reding1d1e6fe2014-11-06 14:12:08 +010078 goto vblank;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000079
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000080 return 0;
Thierry Reding1d1e6fe2014-11-06 14:12:08 +010081
82vblank:
83 drm_vblank_cleanup(drm);
84device:
85 host1x_device_exit(device);
86fbdev:
87 drm_kms_helper_poll_fini(drm);
88 tegra_drm_fb_free(drm);
89config:
90 drm_mode_config_cleanup(drm);
Thierry Redingdf06b752014-06-26 21:41:53 +020091
92 if (tegra->domain) {
93 iommu_domain_free(tegra->domain);
94 drm_mm_takedown(&tegra->mm);
95 }
96free:
Thierry Reding1d1e6fe2014-11-06 14:12:08 +010097 kfree(tegra);
98 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000099}
100
101static int tegra_drm_unload(struct drm_device *drm)
102{
Thierry Reding776dc382013-10-14 14:43:22 +0200103 struct host1x_device *device = to_host1x_device(drm->dev);
Thierry Redingdf06b752014-06-26 21:41:53 +0200104 struct tegra_drm *tegra = drm->dev_private;
Thierry Reding776dc382013-10-14 14:43:22 +0200105 int err;
106
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000107 drm_kms_helper_poll_fini(drm);
108 tegra_drm_fb_exit(drm);
Thierry Redingf002abc2013-10-14 14:06:02 +0200109 drm_vblank_cleanup(drm);
110 drm_mode_config_cleanup(drm);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000111
Thierry Reding776dc382013-10-14 14:43:22 +0200112 err = host1x_device_exit(device);
113 if (err < 0)
114 return err;
115
Thierry Redingdf06b752014-06-26 21:41:53 +0200116 if (tegra->domain) {
117 iommu_domain_free(tegra->domain);
118 drm_mm_takedown(&tegra->mm);
119 }
120
Thierry Reding1053f4dd2014-11-04 16:17:55 +0100121 kfree(tegra);
122
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000123 return 0;
124}
125
126static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
127{
Thierry Reding08943e62013-09-26 16:08:18 +0200128 struct tegra_drm_file *fpriv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200129
130 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
131 if (!fpriv)
132 return -ENOMEM;
133
134 INIT_LIST_HEAD(&fpriv->contexts);
135 filp->driver_priv = fpriv;
136
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000137 return 0;
138}
139
Thierry Redingc88c3632013-09-26 16:08:22 +0200140static void tegra_drm_context_free(struct tegra_drm_context *context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200141{
142 context->client->ops->close_channel(context);
143 kfree(context);
144}
145
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000146static void tegra_drm_lastclose(struct drm_device *drm)
147{
Paul Bolle6e601632014-02-09 14:01:33 +0100148#ifdef CONFIG_DRM_TEGRA_FBDEV
Thierry Reding386a2a72013-09-24 13:22:17 +0200149 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000150
Thierry Reding386a2a72013-09-24 13:22:17 +0200151 tegra_fbdev_restore_mode(tegra->fbdev);
Thierry Reding60c2f702013-10-31 13:28:50 +0100152#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000153}
154
Thierry Redingc40f0f12013-10-10 11:00:33 +0200155static struct host1x_bo *
156host1x_bo_lookup(struct drm_device *drm, struct drm_file *file, u32 handle)
157{
158 struct drm_gem_object *gem;
159 struct tegra_bo *bo;
160
161 gem = drm_gem_object_lookup(drm, file, handle);
162 if (!gem)
163 return NULL;
164
165 mutex_lock(&drm->struct_mutex);
166 drm_gem_object_unreference(gem);
167 mutex_unlock(&drm->struct_mutex);
168
169 bo = to_tegra_bo(gem);
170 return &bo->base;
171}
172
Thierry Reding961e3be2014-06-10 10:25:00 +0200173static int host1x_reloc_copy_from_user(struct host1x_reloc *dest,
174 struct drm_tegra_reloc __user *src,
175 struct drm_device *drm,
176 struct drm_file *file)
177{
178 u32 cmdbuf, target;
179 int err;
180
181 err = get_user(cmdbuf, &src->cmdbuf.handle);
182 if (err < 0)
183 return err;
184
185 err = get_user(dest->cmdbuf.offset, &src->cmdbuf.offset);
186 if (err < 0)
187 return err;
188
189 err = get_user(target, &src->target.handle);
190 if (err < 0)
191 return err;
192
193 err = get_user(dest->target.offset, &src->cmdbuf.offset);
194 if (err < 0)
195 return err;
196
197 err = get_user(dest->shift, &src->shift);
198 if (err < 0)
199 return err;
200
201 dest->cmdbuf.bo = host1x_bo_lookup(drm, file, cmdbuf);
202 if (!dest->cmdbuf.bo)
203 return -ENOENT;
204
205 dest->target.bo = host1x_bo_lookup(drm, file, target);
206 if (!dest->target.bo)
207 return -ENOENT;
208
209 return 0;
210}
211
Thierry Redingc40f0f12013-10-10 11:00:33 +0200212int tegra_drm_submit(struct tegra_drm_context *context,
213 struct drm_tegra_submit *args, struct drm_device *drm,
214 struct drm_file *file)
215{
216 unsigned int num_cmdbufs = args->num_cmdbufs;
217 unsigned int num_relocs = args->num_relocs;
218 unsigned int num_waitchks = args->num_waitchks;
219 struct drm_tegra_cmdbuf __user *cmdbufs =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100220 (void __user *)(uintptr_t)args->cmdbufs;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200221 struct drm_tegra_reloc __user *relocs =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100222 (void __user *)(uintptr_t)args->relocs;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200223 struct drm_tegra_waitchk __user *waitchks =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100224 (void __user *)(uintptr_t)args->waitchks;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200225 struct drm_tegra_syncpt syncpt;
226 struct host1x_job *job;
227 int err;
228
229 /* We don't yet support other than one syncpt_incr struct per submit */
230 if (args->num_syncpts != 1)
231 return -EINVAL;
232
233 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
234 args->num_relocs, args->num_waitchks);
235 if (!job)
236 return -ENOMEM;
237
238 job->num_relocs = args->num_relocs;
239 job->num_waitchk = args->num_waitchks;
240 job->client = (u32)args->context;
241 job->class = context->client->base.class;
242 job->serialize = true;
243
244 while (num_cmdbufs) {
245 struct drm_tegra_cmdbuf cmdbuf;
246 struct host1x_bo *bo;
247
Dan Carpenter9a991602013-11-08 13:07:37 +0300248 if (copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf))) {
249 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200250 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300251 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200252
253 bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
254 if (!bo) {
255 err = -ENOENT;
256 goto fail;
257 }
258
259 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
260 num_cmdbufs--;
261 cmdbufs++;
262 }
263
Thierry Reding961e3be2014-06-10 10:25:00 +0200264 /* copy and resolve relocations from submit */
Thierry Redingc40f0f12013-10-10 11:00:33 +0200265 while (num_relocs--) {
Thierry Reding961e3be2014-06-10 10:25:00 +0200266 err = host1x_reloc_copy_from_user(&job->relocarray[num_relocs],
267 &relocs[num_relocs], drm,
268 file);
269 if (err < 0)
Thierry Redingc40f0f12013-10-10 11:00:33 +0200270 goto fail;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200271 }
272
Dan Carpenter9a991602013-11-08 13:07:37 +0300273 if (copy_from_user(job->waitchk, waitchks,
274 sizeof(*waitchks) * num_waitchks)) {
275 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200276 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300277 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200278
Dan Carpenter9a991602013-11-08 13:07:37 +0300279 if (copy_from_user(&syncpt, (void __user *)(uintptr_t)args->syncpts,
280 sizeof(syncpt))) {
281 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200282 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300283 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200284
285 job->is_addr_reg = context->client->ops->is_addr_reg;
286 job->syncpt_incrs = syncpt.incrs;
287 job->syncpt_id = syncpt.id;
288 job->timeout = 10000;
289
290 if (args->timeout && args->timeout < 10000)
291 job->timeout = args->timeout;
292
293 err = host1x_job_pin(job, context->client->base.dev);
294 if (err)
295 goto fail;
296
297 err = host1x_job_submit(job);
298 if (err)
299 goto fail_submit;
300
301 args->fence = job->syncpt_end;
302
303 host1x_job_put(job);
304 return 0;
305
306fail_submit:
307 host1x_job_unpin(job);
308fail:
309 host1x_job_put(job);
310 return err;
311}
312
313
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200314#ifdef CONFIG_DRM_TEGRA_STAGING
Thierry Redingc88c3632013-09-26 16:08:22 +0200315static struct tegra_drm_context *tegra_drm_get_context(__u64 context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200316{
Thierry Redingc88c3632013-09-26 16:08:22 +0200317 return (struct tegra_drm_context *)(uintptr_t)context;
318}
319
320static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
321 struct tegra_drm_context *context)
322{
323 struct tegra_drm_context *ctx;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200324
325 list_for_each_entry(ctx, &file->contexts, list)
326 if (ctx == context)
327 return true;
328
329 return false;
330}
331
332static int tegra_gem_create(struct drm_device *drm, void *data,
333 struct drm_file *file)
334{
335 struct drm_tegra_gem_create *args = data;
336 struct tegra_bo *bo;
337
Thierry Reding773af772013-10-04 22:34:01 +0200338 bo = tegra_bo_create_with_handle(file, drm, args->size, args->flags,
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200339 &args->handle);
340 if (IS_ERR(bo))
341 return PTR_ERR(bo);
342
343 return 0;
344}
345
346static int tegra_gem_mmap(struct drm_device *drm, void *data,
347 struct drm_file *file)
348{
349 struct drm_tegra_gem_mmap *args = data;
350 struct drm_gem_object *gem;
351 struct tegra_bo *bo;
352
353 gem = drm_gem_object_lookup(drm, file, args->handle);
354 if (!gem)
355 return -EINVAL;
356
357 bo = to_tegra_bo(gem);
358
David Herrmann2bc7b0c2013-08-13 14:19:58 +0200359 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200360
361 drm_gem_object_unreference(gem);
362
363 return 0;
364}
365
366static int tegra_syncpt_read(struct drm_device *drm, void *data,
367 struct drm_file *file)
368{
Thierry Reding776dc382013-10-14 14:43:22 +0200369 struct host1x *host = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200370 struct drm_tegra_syncpt_read *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200371 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200372
Thierry Reding776dc382013-10-14 14:43:22 +0200373 sp = host1x_syncpt_get(host, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200374 if (!sp)
375 return -EINVAL;
376
377 args->value = host1x_syncpt_read_min(sp);
378 return 0;
379}
380
381static int tegra_syncpt_incr(struct drm_device *drm, void *data,
382 struct drm_file *file)
383{
Thierry Reding776dc382013-10-14 14:43:22 +0200384 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200385 struct drm_tegra_syncpt_incr *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200386 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200387
Thierry Reding776dc382013-10-14 14:43:22 +0200388 sp = host1x_syncpt_get(host1x, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200389 if (!sp)
390 return -EINVAL;
391
Arto Merilainenebae30b2013-05-29 13:26:08 +0300392 return host1x_syncpt_incr(sp);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200393}
394
395static int tegra_syncpt_wait(struct drm_device *drm, void *data,
396 struct drm_file *file)
397{
Thierry Reding776dc382013-10-14 14:43:22 +0200398 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200399 struct drm_tegra_syncpt_wait *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200400 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200401
Thierry Reding776dc382013-10-14 14:43:22 +0200402 sp = host1x_syncpt_get(host1x, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200403 if (!sp)
404 return -EINVAL;
405
406 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
407 &args->value);
408}
409
410static int tegra_open_channel(struct drm_device *drm, void *data,
411 struct drm_file *file)
412{
Thierry Reding08943e62013-09-26 16:08:18 +0200413 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding386a2a72013-09-24 13:22:17 +0200414 struct tegra_drm *tegra = drm->dev_private;
415 struct drm_tegra_open_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200416 struct tegra_drm_context *context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200417 struct tegra_drm_client *client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200418 int err = -ENODEV;
419
420 context = kzalloc(sizeof(*context), GFP_KERNEL);
421 if (!context)
422 return -ENOMEM;
423
Thierry Reding776dc382013-10-14 14:43:22 +0200424 list_for_each_entry(client, &tegra->clients, list)
Thierry Reding53fa7f72013-09-24 15:35:40 +0200425 if (client->base.class == args->client) {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200426 err = client->ops->open_channel(client, context);
427 if (err)
428 break;
429
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200430 list_add(&context->list, &fpriv->contexts);
431 args->context = (uintptr_t)context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200432 context->client = client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200433 return 0;
434 }
435
436 kfree(context);
437 return err;
438}
439
440static int tegra_close_channel(struct drm_device *drm, void *data,
441 struct drm_file *file)
442{
Thierry Reding08943e62013-09-26 16:08:18 +0200443 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding776dc382013-10-14 14:43:22 +0200444 struct drm_tegra_close_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200445 struct tegra_drm_context *context;
446
447 context = tegra_drm_get_context(args->context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200448
Thierry Reding08943e62013-09-26 16:08:18 +0200449 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200450 return -EINVAL;
451
452 list_del(&context->list);
Thierry Redingc88c3632013-09-26 16:08:22 +0200453 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200454
455 return 0;
456}
457
458static int tegra_get_syncpt(struct drm_device *drm, void *data,
459 struct drm_file *file)
460{
Thierry Reding08943e62013-09-26 16:08:18 +0200461 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200462 struct drm_tegra_get_syncpt *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200463 struct tegra_drm_context *context;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200464 struct host1x_syncpt *syncpt;
465
Thierry Redingc88c3632013-09-26 16:08:22 +0200466 context = tegra_drm_get_context(args->context);
467
Thierry Reding08943e62013-09-26 16:08:18 +0200468 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200469 return -ENODEV;
470
Thierry Reding53fa7f72013-09-24 15:35:40 +0200471 if (args->index >= context->client->base.num_syncpts)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200472 return -EINVAL;
473
Thierry Reding53fa7f72013-09-24 15:35:40 +0200474 syncpt = context->client->base.syncpts[args->index];
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200475 args->id = host1x_syncpt_id(syncpt);
476
477 return 0;
478}
479
480static int tegra_submit(struct drm_device *drm, void *data,
481 struct drm_file *file)
482{
Thierry Reding08943e62013-09-26 16:08:18 +0200483 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200484 struct drm_tegra_submit *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200485 struct tegra_drm_context *context;
486
487 context = tegra_drm_get_context(args->context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200488
Thierry Reding08943e62013-09-26 16:08:18 +0200489 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200490 return -ENODEV;
491
492 return context->client->ops->submit(context, args, drm, file);
493}
Arto Merilainenc54a1692013-10-14 15:21:54 +0300494
495static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
496 struct drm_file *file)
497{
498 struct tegra_drm_file *fpriv = file->driver_priv;
499 struct drm_tegra_get_syncpt_base *args = data;
500 struct tegra_drm_context *context;
501 struct host1x_syncpt_base *base;
502 struct host1x_syncpt *syncpt;
503
504 context = tegra_drm_get_context(args->context);
505
506 if (!tegra_drm_file_owns_context(fpriv, context))
507 return -ENODEV;
508
509 if (args->syncpt >= context->client->base.num_syncpts)
510 return -EINVAL;
511
512 syncpt = context->client->base.syncpts[args->syncpt];
513
514 base = host1x_syncpt_get_base(syncpt);
515 if (!base)
516 return -ENXIO;
517
518 args->id = host1x_syncpt_base_id(base);
519
520 return 0;
521}
Thierry Reding7678d712014-06-03 14:56:57 +0200522
523static int tegra_gem_set_tiling(struct drm_device *drm, void *data,
524 struct drm_file *file)
525{
526 struct drm_tegra_gem_set_tiling *args = data;
527 enum tegra_bo_tiling_mode mode;
528 struct drm_gem_object *gem;
529 unsigned long value = 0;
530 struct tegra_bo *bo;
531
532 switch (args->mode) {
533 case DRM_TEGRA_GEM_TILING_MODE_PITCH:
534 mode = TEGRA_BO_TILING_MODE_PITCH;
535
536 if (args->value != 0)
537 return -EINVAL;
538
539 break;
540
541 case DRM_TEGRA_GEM_TILING_MODE_TILED:
542 mode = TEGRA_BO_TILING_MODE_TILED;
543
544 if (args->value != 0)
545 return -EINVAL;
546
547 break;
548
549 case DRM_TEGRA_GEM_TILING_MODE_BLOCK:
550 mode = TEGRA_BO_TILING_MODE_BLOCK;
551
552 if (args->value > 5)
553 return -EINVAL;
554
555 value = args->value;
556 break;
557
558 default:
559 return -EINVAL;
560 }
561
562 gem = drm_gem_object_lookup(drm, file, args->handle);
563 if (!gem)
564 return -ENOENT;
565
566 bo = to_tegra_bo(gem);
567
568 bo->tiling.mode = mode;
569 bo->tiling.value = value;
570
571 drm_gem_object_unreference(gem);
572
573 return 0;
574}
575
576static int tegra_gem_get_tiling(struct drm_device *drm, void *data,
577 struct drm_file *file)
578{
579 struct drm_tegra_gem_get_tiling *args = data;
580 struct drm_gem_object *gem;
581 struct tegra_bo *bo;
582 int err = 0;
583
584 gem = drm_gem_object_lookup(drm, file, args->handle);
585 if (!gem)
586 return -ENOENT;
587
588 bo = to_tegra_bo(gem);
589
590 switch (bo->tiling.mode) {
591 case TEGRA_BO_TILING_MODE_PITCH:
592 args->mode = DRM_TEGRA_GEM_TILING_MODE_PITCH;
593 args->value = 0;
594 break;
595
596 case TEGRA_BO_TILING_MODE_TILED:
597 args->mode = DRM_TEGRA_GEM_TILING_MODE_TILED;
598 args->value = 0;
599 break;
600
601 case TEGRA_BO_TILING_MODE_BLOCK:
602 args->mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
603 args->value = bo->tiling.value;
604 break;
605
606 default:
607 err = -EINVAL;
608 break;
609 }
610
611 drm_gem_object_unreference(gem);
612
613 return err;
614}
Thierry Reding7b129082014-06-10 12:04:03 +0200615
616static int tegra_gem_set_flags(struct drm_device *drm, void *data,
617 struct drm_file *file)
618{
619 struct drm_tegra_gem_set_flags *args = data;
620 struct drm_gem_object *gem;
621 struct tegra_bo *bo;
622
623 if (args->flags & ~DRM_TEGRA_GEM_FLAGS)
624 return -EINVAL;
625
626 gem = drm_gem_object_lookup(drm, file, args->handle);
627 if (!gem)
628 return -ENOENT;
629
630 bo = to_tegra_bo(gem);
631 bo->flags = 0;
632
633 if (args->flags & DRM_TEGRA_GEM_BOTTOM_UP)
634 bo->flags |= TEGRA_BO_BOTTOM_UP;
635
636 drm_gem_object_unreference(gem);
637
638 return 0;
639}
640
641static int tegra_gem_get_flags(struct drm_device *drm, void *data,
642 struct drm_file *file)
643{
644 struct drm_tegra_gem_get_flags *args = data;
645 struct drm_gem_object *gem;
646 struct tegra_bo *bo;
647
648 gem = drm_gem_object_lookup(drm, file, args->handle);
649 if (!gem)
650 return -ENOENT;
651
652 bo = to_tegra_bo(gem);
653 args->flags = 0;
654
655 if (bo->flags & TEGRA_BO_BOTTOM_UP)
656 args->flags |= DRM_TEGRA_GEM_BOTTOM_UP;
657
658 drm_gem_object_unreference(gem);
659
660 return 0;
661}
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200662#endif
663
Rob Clarkbaa70942013-08-02 13:27:49 -0400664static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200665#ifdef CONFIG_DRM_TEGRA_STAGING
Thierry Redingbd4f2362014-06-03 14:59:29 +0200666 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED),
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200667 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
668 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
669 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
670 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
671 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
672 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
673 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
674 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
Arto Merilainenc54a1692013-10-14 15:21:54 +0300675 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base, DRM_UNLOCKED),
Thierry Reding7678d712014-06-03 14:56:57 +0200676 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_TILING, tegra_gem_set_tiling, DRM_UNLOCKED),
677 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_TILING, tegra_gem_get_tiling, DRM_UNLOCKED),
Thierry Reding7b129082014-06-10 12:04:03 +0200678 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_FLAGS, tegra_gem_set_flags, DRM_UNLOCKED),
679 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_FLAGS, tegra_gem_get_flags, DRM_UNLOCKED),
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200680#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000681};
682
683static const struct file_operations tegra_drm_fops = {
684 .owner = THIS_MODULE,
685 .open = drm_open,
686 .release = drm_release,
687 .unlocked_ioctl = drm_ioctl,
Arto Merilainende2ba662013-03-22 16:34:08 +0200688 .mmap = tegra_drm_mmap,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000689 .poll = drm_poll,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000690 .read = drm_read,
691#ifdef CONFIG_COMPAT
692 .compat_ioctl = drm_compat_ioctl,
693#endif
694 .llseek = noop_llseek,
695};
696
Thierry Reding6e5ff992012-11-28 11:45:47 +0100697static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
698{
699 struct drm_crtc *crtc;
700
701 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
702 struct tegra_dc *dc = to_tegra_dc(crtc);
703
704 if (dc->pipe == pipe)
705 return crtc;
706 }
707
708 return NULL;
709}
710
711static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
712{
713 /* TODO: implement real hardware counter using syncpoints */
714 return drm_vblank_count(dev, crtc);
715}
716
717static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
718{
719 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
720 struct tegra_dc *dc = to_tegra_dc(crtc);
721
722 if (!crtc)
723 return -ENODEV;
724
725 tegra_dc_enable_vblank(dc);
726
727 return 0;
728}
729
730static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
731{
732 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
733 struct tegra_dc *dc = to_tegra_dc(crtc);
734
735 if (crtc)
736 tegra_dc_disable_vblank(dc);
737}
738
Thierry Reding3c03c462012-11-28 12:00:18 +0100739static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
740{
Thierry Reding08943e62013-09-26 16:08:18 +0200741 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Redingc88c3632013-09-26 16:08:22 +0200742 struct tegra_drm_context *context, *tmp;
Thierry Reding3c03c462012-11-28 12:00:18 +0100743 struct drm_crtc *crtc;
744
745 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
746 tegra_dc_cancel_page_flip(crtc, file);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200747
748 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
Thierry Redingc88c3632013-09-26 16:08:22 +0200749 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200750
751 kfree(fpriv);
Thierry Reding3c03c462012-11-28 12:00:18 +0100752}
753
Thierry Redinge450fcc2013-02-13 16:13:16 +0100754#ifdef CONFIG_DEBUG_FS
755static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
756{
757 struct drm_info_node *node = (struct drm_info_node *)s->private;
758 struct drm_device *drm = node->minor->dev;
759 struct drm_framebuffer *fb;
760
761 mutex_lock(&drm->mode_config.fb_lock);
762
763 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
764 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
765 fb->base.id, fb->width, fb->height, fb->depth,
766 fb->bits_per_pixel,
767 atomic_read(&fb->refcount.refcount));
768 }
769
770 mutex_unlock(&drm->mode_config.fb_lock);
771
772 return 0;
773}
774
775static struct drm_info_list tegra_debugfs_list[] = {
776 { "framebuffers", tegra_debugfs_framebuffers, 0 },
777};
778
779static int tegra_debugfs_init(struct drm_minor *minor)
780{
781 return drm_debugfs_create_files(tegra_debugfs_list,
782 ARRAY_SIZE(tegra_debugfs_list),
783 minor->debugfs_root, minor);
784}
785
786static void tegra_debugfs_cleanup(struct drm_minor *minor)
787{
788 drm_debugfs_remove_files(tegra_debugfs_list,
789 ARRAY_SIZE(tegra_debugfs_list), minor);
790}
791#endif
792
Thierry Reding9b57f5f2013-11-08 13:17:14 +0100793static struct drm_driver tegra_drm_driver = {
Thierry Reding38003912013-12-12 10:00:43 +0100794 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000795 .load = tegra_drm_load,
796 .unload = tegra_drm_unload,
797 .open = tegra_drm_open,
Thierry Reding3c03c462012-11-28 12:00:18 +0100798 .preclose = tegra_drm_preclose,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000799 .lastclose = tegra_drm_lastclose,
800
Thierry Reding6e5ff992012-11-28 11:45:47 +0100801 .get_vblank_counter = tegra_drm_get_vblank_counter,
802 .enable_vblank = tegra_drm_enable_vblank,
803 .disable_vblank = tegra_drm_disable_vblank,
804
Thierry Redinge450fcc2013-02-13 16:13:16 +0100805#if defined(CONFIG_DEBUG_FS)
806 .debugfs_init = tegra_debugfs_init,
807 .debugfs_cleanup = tegra_debugfs_cleanup,
808#endif
809
Arto Merilainende2ba662013-03-22 16:34:08 +0200810 .gem_free_object = tegra_bo_free_object,
811 .gem_vm_ops = &tegra_bo_vm_ops,
Thierry Reding38003912013-12-12 10:00:43 +0100812
813 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
814 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
815 .gem_prime_export = tegra_gem_prime_export,
816 .gem_prime_import = tegra_gem_prime_import,
817
Arto Merilainende2ba662013-03-22 16:34:08 +0200818 .dumb_create = tegra_bo_dumb_create,
819 .dumb_map_offset = tegra_bo_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200820 .dumb_destroy = drm_gem_dumb_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000821
822 .ioctls = tegra_drm_ioctls,
823 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
824 .fops = &tegra_drm_fops,
825
826 .name = DRIVER_NAME,
827 .desc = DRIVER_DESC,
828 .date = DRIVER_DATE,
829 .major = DRIVER_MAJOR,
830 .minor = DRIVER_MINOR,
831 .patchlevel = DRIVER_PATCHLEVEL,
832};
Thierry Reding776dc382013-10-14 14:43:22 +0200833
834int tegra_drm_register_client(struct tegra_drm *tegra,
835 struct tegra_drm_client *client)
836{
837 mutex_lock(&tegra->clients_lock);
838 list_add_tail(&client->list, &tegra->clients);
839 mutex_unlock(&tegra->clients_lock);
840
841 return 0;
842}
843
844int tegra_drm_unregister_client(struct tegra_drm *tegra,
845 struct tegra_drm_client *client)
846{
847 mutex_lock(&tegra->clients_lock);
848 list_del_init(&client->list);
849 mutex_unlock(&tegra->clients_lock);
850
851 return 0;
852}
853
Thierry Reding9910f5c2014-05-22 09:57:15 +0200854static int host1x_drm_probe(struct host1x_device *dev)
Thierry Reding776dc382013-10-14 14:43:22 +0200855{
Thierry Reding9910f5c2014-05-22 09:57:15 +0200856 struct drm_driver *driver = &tegra_drm_driver;
857 struct drm_device *drm;
858 int err;
859
860 drm = drm_dev_alloc(driver, &dev->dev);
861 if (!drm)
862 return -ENOMEM;
863
864 drm_dev_set_unique(drm, dev_name(&dev->dev));
865 dev_set_drvdata(&dev->dev, drm);
866
867 err = drm_dev_register(drm, 0);
868 if (err < 0)
869 goto unref;
870
871 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", driver->name,
872 driver->major, driver->minor, driver->patchlevel,
873 driver->date, drm->primary->index);
874
875 return 0;
876
877unref:
878 drm_dev_unref(drm);
879 return err;
Thierry Reding776dc382013-10-14 14:43:22 +0200880}
881
Thierry Reding9910f5c2014-05-22 09:57:15 +0200882static int host1x_drm_remove(struct host1x_device *dev)
Thierry Reding776dc382013-10-14 14:43:22 +0200883{
Thierry Reding9910f5c2014-05-22 09:57:15 +0200884 struct drm_device *drm = dev_get_drvdata(&dev->dev);
885
886 drm_dev_unregister(drm);
887 drm_dev_unref(drm);
Thierry Reding776dc382013-10-14 14:43:22 +0200888
889 return 0;
890}
891
892static const struct of_device_id host1x_drm_subdevs[] = {
893 { .compatible = "nvidia,tegra20-dc", },
894 { .compatible = "nvidia,tegra20-hdmi", },
895 { .compatible = "nvidia,tegra20-gr2d", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100896 { .compatible = "nvidia,tegra20-gr3d", },
Thierry Reding776dc382013-10-14 14:43:22 +0200897 { .compatible = "nvidia,tegra30-dc", },
898 { .compatible = "nvidia,tegra30-hdmi", },
899 { .compatible = "nvidia,tegra30-gr2d", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100900 { .compatible = "nvidia,tegra30-gr3d", },
Thierry Redingdec72732013-09-03 08:45:46 +0200901 { .compatible = "nvidia,tegra114-dsi", },
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200902 { .compatible = "nvidia,tegra114-hdmi", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100903 { .compatible = "nvidia,tegra114-gr3d", },
Thierry Reding8620fc62013-12-12 11:03:59 +0100904 { .compatible = "nvidia,tegra124-dc", },
Thierry Reding6b6b6042013-11-15 16:06:05 +0100905 { .compatible = "nvidia,tegra124-sor", },
Thierry Redingfb7be702013-11-15 16:07:32 +0100906 { .compatible = "nvidia,tegra124-hdmi", },
Thierry Reding776dc382013-10-14 14:43:22 +0200907 { /* sentinel */ }
908};
909
910static struct host1x_driver host1x_drm_driver = {
911 .name = "drm",
912 .probe = host1x_drm_probe,
913 .remove = host1x_drm_remove,
914 .subdevs = host1x_drm_subdevs,
915};
916
917static int __init host1x_drm_init(void)
918{
919 int err;
920
921 err = host1x_driver_register(&host1x_drm_driver);
922 if (err < 0)
923 return err;
924
925 err = platform_driver_register(&tegra_dc_driver);
926 if (err < 0)
927 goto unregister_host1x;
928
Thierry Redingdec72732013-09-03 08:45:46 +0200929 err = platform_driver_register(&tegra_dsi_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200930 if (err < 0)
931 goto unregister_dc;
932
Thierry Reding6b6b6042013-11-15 16:06:05 +0100933 err = platform_driver_register(&tegra_sor_driver);
Thierry Redingdec72732013-09-03 08:45:46 +0200934 if (err < 0)
935 goto unregister_dsi;
936
Thierry Reding6b6b6042013-11-15 16:06:05 +0100937 err = platform_driver_register(&tegra_hdmi_driver);
938 if (err < 0)
939 goto unregister_sor;
940
941 err = platform_driver_register(&tegra_dpaux_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200942 if (err < 0)
943 goto unregister_hdmi;
944
Thierry Reding6b6b6042013-11-15 16:06:05 +0100945 err = platform_driver_register(&tegra_gr2d_driver);
946 if (err < 0)
947 goto unregister_dpaux;
948
Thierry Reding5f60ed02013-02-28 08:08:01 +0100949 err = platform_driver_register(&tegra_gr3d_driver);
950 if (err < 0)
951 goto unregister_gr2d;
952
Thierry Reding776dc382013-10-14 14:43:22 +0200953 return 0;
954
Thierry Reding5f60ed02013-02-28 08:08:01 +0100955unregister_gr2d:
956 platform_driver_unregister(&tegra_gr2d_driver);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100957unregister_dpaux:
958 platform_driver_unregister(&tegra_dpaux_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200959unregister_hdmi:
960 platform_driver_unregister(&tegra_hdmi_driver);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100961unregister_sor:
962 platform_driver_unregister(&tegra_sor_driver);
Thierry Redingdec72732013-09-03 08:45:46 +0200963unregister_dsi:
964 platform_driver_unregister(&tegra_dsi_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200965unregister_dc:
966 platform_driver_unregister(&tegra_dc_driver);
967unregister_host1x:
968 host1x_driver_unregister(&host1x_drm_driver);
969 return err;
970}
971module_init(host1x_drm_init);
972
973static void __exit host1x_drm_exit(void)
974{
Thierry Reding5f60ed02013-02-28 08:08:01 +0100975 platform_driver_unregister(&tegra_gr3d_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200976 platform_driver_unregister(&tegra_gr2d_driver);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100977 platform_driver_unregister(&tegra_dpaux_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200978 platform_driver_unregister(&tegra_hdmi_driver);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100979 platform_driver_unregister(&tegra_sor_driver);
Thierry Redingdec72732013-09-03 08:45:46 +0200980 platform_driver_unregister(&tegra_dsi_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200981 platform_driver_unregister(&tegra_dc_driver);
982 host1x_driver_unregister(&host1x_drm_driver);
983}
984module_exit(host1x_drm_exit);
985
986MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
987MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
988MODULE_LICENSE("GPL v2");