blob: 33d966145293308e0145e4d2260b77e6f02f42dc [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
Terje Bergstrom692e6d72013-03-22 16:34:07 +020010#include "host1x_client.h"
Arto Merilainende2ba662013-03-22 16:34:08 +020011#include "dev.h"
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000012#include "drm.h"
Arto Merilainende2ba662013-03-22 16:34:08 +020013#include "gem.h"
14#include "syncpt.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 Reding386a2a72013-09-24 13:22:17 +020027struct host1x_subdev {
Terje Bergstrom692e6d72013-03-22 16:34:07 +020028 struct host1x_client *client;
29 struct device_node *np;
30 struct list_head list;
31};
32
Thierry Reding386a2a72013-09-24 13:22:17 +020033static int host1x_subdev_add(struct tegra_drm *tegra, struct device_node *np)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000034{
Thierry Reding386a2a72013-09-24 13:22:17 +020035 struct host1x_subdev *subdev;
Terje Bergstrom692e6d72013-03-22 16:34:07 +020036
Thierry Reding386a2a72013-09-24 13:22:17 +020037 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
38 if (!subdev)
Terje Bergstrom692e6d72013-03-22 16:34:07 +020039 return -ENOMEM;
40
Thierry Reding386a2a72013-09-24 13:22:17 +020041 INIT_LIST_HEAD(&subdev->list);
42 subdev->np = of_node_get(np);
Terje Bergstrom692e6d72013-03-22 16:34:07 +020043
Thierry Reding386a2a72013-09-24 13:22:17 +020044 list_add_tail(&subdev->list, &tegra->subdevs);
Terje Bergstrom692e6d72013-03-22 16:34:07 +020045
46 return 0;
47}
48
Thierry Reding386a2a72013-09-24 13:22:17 +020049static int host1x_subdev_register(struct tegra_drm *tegra,
50 struct host1x_subdev *subdev,
51 struct host1x_client *client)
Terje Bergstrom692e6d72013-03-22 16:34:07 +020052{
Thierry Reding386a2a72013-09-24 13:22:17 +020053 mutex_lock(&tegra->subdevs_lock);
54 list_del_init(&subdev->list);
55 list_add_tail(&subdev->list, &tegra->active);
56 subdev->client = client;
57 mutex_unlock(&tegra->subdevs_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +020058
59 return 0;
60}
61
Thierry Reding386a2a72013-09-24 13:22:17 +020062static int host1x_subdev_unregister(struct tegra_drm *tegra,
63 struct host1x_subdev *subdev)
Terje Bergstrom692e6d72013-03-22 16:34:07 +020064{
Thierry Reding386a2a72013-09-24 13:22:17 +020065 mutex_lock(&tegra->subdevs_lock);
66 list_del_init(&subdev->list);
67 mutex_unlock(&tegra->subdevs_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +020068
Thierry Reding386a2a72013-09-24 13:22:17 +020069 of_node_put(subdev->np);
70 kfree(subdev);
Terje Bergstrom692e6d72013-03-22 16:34:07 +020071
72 return 0;
73}
74
Thierry Reding386a2a72013-09-24 13:22:17 +020075static int tegra_parse_dt(struct tegra_drm *tegra)
Terje Bergstrom692e6d72013-03-22 16:34:07 +020076{
77 static const char * const compat[] = {
78 "nvidia,tegra20-dc",
79 "nvidia,tegra20-hdmi",
Terje Bergstromd43f81c2013-03-22 16:34:09 +020080 "nvidia,tegra20-gr2d",
Terje Bergstrom692e6d72013-03-22 16:34:07 +020081 "nvidia,tegra30-dc",
82 "nvidia,tegra30-hdmi",
Terje Bergstromd43f81c2013-03-22 16:34:09 +020083 "nvidia,tegra30-gr2d",
Terje Bergstrom692e6d72013-03-22 16:34:07 +020084 };
85 unsigned int i;
86 int err;
87
88 for (i = 0; i < ARRAY_SIZE(compat); i++) {
89 struct device_node *np;
90
Thierry Reding386a2a72013-09-24 13:22:17 +020091 for_each_child_of_node(tegra->dev->of_node, np) {
Terje Bergstrom692e6d72013-03-22 16:34:07 +020092 if (of_device_is_compatible(np, compat[i]) &&
93 of_device_is_available(np)) {
Thierry Reding386a2a72013-09-24 13:22:17 +020094 err = host1x_subdev_add(tegra, np);
Terje Bergstrom692e6d72013-03-22 16:34:07 +020095 if (err < 0)
96 return err;
97 }
98 }
99 }
100
101 return 0;
102}
103
Thierry Reding386a2a72013-09-24 13:22:17 +0200104int tegra_drm_alloc(struct platform_device *pdev)
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200105{
Thierry Reding386a2a72013-09-24 13:22:17 +0200106 struct tegra_drm *tegra;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000107 int err;
108
Thierry Reding386a2a72013-09-24 13:22:17 +0200109 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
110 if (!tegra)
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200111 return -ENOMEM;
112
Thierry Reding386a2a72013-09-24 13:22:17 +0200113 mutex_init(&tegra->subdevs_lock);
114 INIT_LIST_HEAD(&tegra->subdevs);
115 INIT_LIST_HEAD(&tegra->active);
116 mutex_init(&tegra->clients_lock);
117 INIT_LIST_HEAD(&tegra->clients);
118 tegra->dev = &pdev->dev;
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200119
Thierry Reding386a2a72013-09-24 13:22:17 +0200120 err = tegra_parse_dt(tegra);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200121 if (err < 0) {
122 dev_err(&pdev->dev, "failed to parse DT: %d\n", err);
123 return err;
124 }
125
Thierry Reding386a2a72013-09-24 13:22:17 +0200126 host1x_set_drm_data(&pdev->dev, tegra);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200127
128 return 0;
129}
130
Thierry Reding386a2a72013-09-24 13:22:17 +0200131int tegra_drm_init(struct tegra_drm *tegra, struct drm_device *drm)
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200132{
133 struct host1x_client *client;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200134 int err;
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200135
Thierry Reding386a2a72013-09-24 13:22:17 +0200136 mutex_lock(&tegra->clients_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200137
Thierry Reding386a2a72013-09-24 13:22:17 +0200138 list_for_each_entry(client, &tegra->clients, list) {
Thierry Reding53fa7f72013-09-24 15:35:40 +0200139 struct tegra_drm_client *tdc = to_tegra_drm_client(client);
140
141 /* associate client with DRM device */
142 tdc->drm = drm;
143
144 if (client->ops && client->ops->init) {
145 err = client->ops->init(client);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200146 if (err < 0) {
Thierry Reding386a2a72013-09-24 13:22:17 +0200147 dev_err(tegra->dev,
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200148 "DRM setup failed for %s: %d\n",
149 dev_name(client->dev), err);
Thierry Reding386a2a72013-09-24 13:22:17 +0200150 mutex_unlock(&tegra->clients_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200151 return err;
152 }
153 }
154 }
155
Thierry Reding386a2a72013-09-24 13:22:17 +0200156 mutex_unlock(&tegra->clients_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200157
158 return 0;
159}
160
Thierry Reding386a2a72013-09-24 13:22:17 +0200161int tegra_drm_exit(struct tegra_drm *tegra)
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200162{
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200163 struct host1x_client *client;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200164 struct platform_device *pdev;
165 int err;
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200166
Thierry Reding386a2a72013-09-24 13:22:17 +0200167 if (!tegra->drm)
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200168 return 0;
169
Thierry Reding386a2a72013-09-24 13:22:17 +0200170 mutex_lock(&tegra->clients_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200171
Thierry Reding386a2a72013-09-24 13:22:17 +0200172 list_for_each_entry_reverse(client, &tegra->clients, list) {
Thierry Reding53fa7f72013-09-24 15:35:40 +0200173 if (client->ops && client->ops->exit) {
174 err = client->ops->exit(client);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200175 if (err < 0) {
Thierry Reding386a2a72013-09-24 13:22:17 +0200176 dev_err(tegra->dev,
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200177 "DRM cleanup failed for %s: %d\n",
178 dev_name(client->dev), err);
Thierry Reding386a2a72013-09-24 13:22:17 +0200179 mutex_unlock(&tegra->clients_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200180 return err;
181 }
182 }
183 }
184
Thierry Reding386a2a72013-09-24 13:22:17 +0200185 mutex_unlock(&tegra->clients_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200186
Thierry Reding53fa7f72013-09-24 15:35:40 +0200187 pdev = to_platform_device(tegra->dev);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200188 drm_platform_exit(&tegra_drm_driver, pdev);
Thierry Reding386a2a72013-09-24 13:22:17 +0200189 tegra->drm = NULL;
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200190
191 return 0;
192}
193
Thierry Reding386a2a72013-09-24 13:22:17 +0200194int host1x_register_client(struct tegra_drm *tegra,
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200195 struct host1x_client *client)
196{
Thierry Reding386a2a72013-09-24 13:22:17 +0200197 struct host1x_subdev *subdev, *tmp;
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200198 int err;
199
Thierry Reding386a2a72013-09-24 13:22:17 +0200200 mutex_lock(&tegra->clients_lock);
201 list_add_tail(&client->list, &tegra->clients);
202 mutex_unlock(&tegra->clients_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200203
Thierry Reding386a2a72013-09-24 13:22:17 +0200204 list_for_each_entry_safe(subdev, tmp, &tegra->subdevs, list)
205 if (subdev->np == client->dev->of_node)
206 host1x_subdev_register(tegra, subdev, client);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200207
Thierry Reding386a2a72013-09-24 13:22:17 +0200208 if (list_empty(&tegra->subdevs)) {
209 struct platform_device *pdev = to_platform_device(tegra->dev);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200210
211 err = drm_platform_init(&tegra_drm_driver, pdev);
212 if (err < 0) {
Thierry Reding386a2a72013-09-24 13:22:17 +0200213 dev_err(tegra->dev, "drm_platform_init(): %d\n", err);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200214 return err;
215 }
216 }
217
218 return 0;
219}
220
Thierry Reding386a2a72013-09-24 13:22:17 +0200221int host1x_unregister_client(struct tegra_drm *tegra,
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200222 struct host1x_client *client)
223{
Thierry Reding386a2a72013-09-24 13:22:17 +0200224 struct host1x_subdev *subdev, *tmp;
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200225 int err;
226
Thierry Reding386a2a72013-09-24 13:22:17 +0200227 list_for_each_entry_safe(subdev, tmp, &tegra->active, list) {
228 if (subdev->client == client) {
229 err = tegra_drm_exit(tegra);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200230 if (err < 0) {
Thierry Reding386a2a72013-09-24 13:22:17 +0200231 dev_err(tegra->dev, "tegra_drm_exit(): %d\n",
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200232 err);
233 return err;
234 }
235
Thierry Reding386a2a72013-09-24 13:22:17 +0200236 host1x_subdev_unregister(tegra, subdev);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200237 break;
238 }
239 }
240
Thierry Reding386a2a72013-09-24 13:22:17 +0200241 mutex_lock(&tegra->clients_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200242 list_del_init(&client->list);
Thierry Reding386a2a72013-09-24 13:22:17 +0200243 mutex_unlock(&tegra->clients_lock);
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200244
245 return 0;
246}
247
248static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
249{
Thierry Reding386a2a72013-09-24 13:22:17 +0200250 struct tegra_drm *tegra;
Terje Bergstrom692e6d72013-03-22 16:34:07 +0200251 int err;
252
Thierry Reding386a2a72013-09-24 13:22:17 +0200253 tegra = host1x_get_drm_data(drm->dev);
254 drm->dev_private = tegra;
255 tegra->drm = drm;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000256
257 drm_mode_config_init(drm);
258
Thierry Reding386a2a72013-09-24 13:22:17 +0200259 err = tegra_drm_init(tegra, drm);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000260 if (err < 0)
261 return err;
262
Thierry Reding603f0cc2013-04-22 21:22:14 +0200263 /*
264 * We don't use the drm_irq_install() helpers provided by the DRM
265 * core, so we need to set this manually in order to allow the
266 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
267 */
Ville Syrjälä44238432013-10-04 14:53:37 +0300268 drm->irq_enabled = true;
Thierry Reding603f0cc2013-04-22 21:22:14 +0200269
Thierry Reding6e5ff992012-11-28 11:45:47 +0100270 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
271 if (err < 0)
272 return err;
273
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000274 err = tegra_drm_fb_init(drm);
275 if (err < 0)
276 return err;
277
278 drm_kms_helper_poll_init(drm);
279
280 return 0;
281}
282
283static int tegra_drm_unload(struct drm_device *drm)
284{
285 drm_kms_helper_poll_fini(drm);
286 tegra_drm_fb_exit(drm);
287
288 drm_mode_config_cleanup(drm);
289
290 return 0;
291}
292
293static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
294{
Thierry Reding08943e62013-09-26 16:08:18 +0200295 struct tegra_drm_file *fpriv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200296
297 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
298 if (!fpriv)
299 return -ENOMEM;
300
301 INIT_LIST_HEAD(&fpriv->contexts);
302 filp->driver_priv = fpriv;
303
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000304 return 0;
305}
306
Thierry Redingc88c3632013-09-26 16:08:22 +0200307static void tegra_drm_context_free(struct tegra_drm_context *context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200308{
309 context->client->ops->close_channel(context);
310 kfree(context);
311}
312
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000313static void tegra_drm_lastclose(struct drm_device *drm)
314{
Thierry Reding386a2a72013-09-24 13:22:17 +0200315 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000316
Thierry Reding386a2a72013-09-24 13:22:17 +0200317 tegra_fbdev_restore_mode(tegra->fbdev);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000318}
319
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200320#ifdef CONFIG_DRM_TEGRA_STAGING
Thierry Redingc88c3632013-09-26 16:08:22 +0200321static struct tegra_drm_context *tegra_drm_get_context(__u64 context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200322{
Thierry Redingc88c3632013-09-26 16:08:22 +0200323 return (struct tegra_drm_context *)(uintptr_t)context;
324}
325
326static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
327 struct tegra_drm_context *context)
328{
329 struct tegra_drm_context *ctx;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200330
331 list_for_each_entry(ctx, &file->contexts, list)
332 if (ctx == context)
333 return true;
334
335 return false;
336}
337
338static int tegra_gem_create(struct drm_device *drm, void *data,
339 struct drm_file *file)
340{
341 struct drm_tegra_gem_create *args = data;
342 struct tegra_bo *bo;
343
344 bo = tegra_bo_create_with_handle(file, drm, args->size,
345 &args->handle);
346 if (IS_ERR(bo))
347 return PTR_ERR(bo);
348
349 return 0;
350}
351
352static int tegra_gem_mmap(struct drm_device *drm, void *data,
353 struct drm_file *file)
354{
355 struct drm_tegra_gem_mmap *args = data;
356 struct drm_gem_object *gem;
357 struct tegra_bo *bo;
358
359 gem = drm_gem_object_lookup(drm, file, args->handle);
360 if (!gem)
361 return -EINVAL;
362
363 bo = to_tegra_bo(gem);
364
David Herrmann2bc7b0c2013-08-13 14:19:58 +0200365 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200366
367 drm_gem_object_unreference(gem);
368
369 return 0;
370}
371
372static int tegra_syncpt_read(struct drm_device *drm, void *data,
373 struct drm_file *file)
374{
375 struct drm_tegra_syncpt_read *args = data;
376 struct host1x *host = dev_get_drvdata(drm->dev);
377 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
378
379 if (!sp)
380 return -EINVAL;
381
382 args->value = host1x_syncpt_read_min(sp);
383 return 0;
384}
385
386static int tegra_syncpt_incr(struct drm_device *drm, void *data,
387 struct drm_file *file)
388{
389 struct drm_tegra_syncpt_incr *args = data;
390 struct host1x *host = dev_get_drvdata(drm->dev);
391 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
392
393 if (!sp)
394 return -EINVAL;
395
Arto Merilainenebae30b2013-05-29 13:26:08 +0300396 return host1x_syncpt_incr(sp);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200397}
398
399static int tegra_syncpt_wait(struct drm_device *drm, void *data,
400 struct drm_file *file)
401{
402 struct drm_tegra_syncpt_wait *args = data;
403 struct host1x *host = dev_get_drvdata(drm->dev);
404 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
405
406 if (!sp)
407 return -EINVAL;
408
409 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
410 &args->value);
411}
412
413static int tegra_open_channel(struct drm_device *drm, void *data,
414 struct drm_file *file)
415{
Thierry Reding08943e62013-09-26 16:08:18 +0200416 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding386a2a72013-09-24 13:22:17 +0200417 struct tegra_drm *tegra = drm->dev_private;
418 struct drm_tegra_open_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200419 struct tegra_drm_context *context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200420 struct tegra_drm_client *client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200421 int err = -ENODEV;
422
423 context = kzalloc(sizeof(*context), GFP_KERNEL);
424 if (!context)
425 return -ENOMEM;
426
Thierry Reding53fa7f72013-09-24 15:35:40 +0200427 list_for_each_entry(client, &tegra->clients, base.list)
428 if (client->base.class == args->client) {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200429 err = client->ops->open_channel(client, context);
430 if (err)
431 break;
432
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200433 list_add(&context->list, &fpriv->contexts);
434 args->context = (uintptr_t)context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200435 context->client = client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200436 return 0;
437 }
438
439 kfree(context);
440 return err;
441}
442
443static int tegra_close_channel(struct drm_device *drm, void *data,
444 struct drm_file *file)
445{
446 struct drm_tegra_close_channel *args = data;
Thierry Reding08943e62013-09-26 16:08:18 +0200447 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Redingc88c3632013-09-26 16:08:22 +0200448 struct tegra_drm_context *context;
449
450 context = tegra_drm_get_context(args->context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200451
Thierry Reding08943e62013-09-26 16:08:18 +0200452 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200453 return -EINVAL;
454
455 list_del(&context->list);
Thierry Redingc88c3632013-09-26 16:08:22 +0200456 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200457
458 return 0;
459}
460
461static int tegra_get_syncpt(struct drm_device *drm, void *data,
462 struct drm_file *file)
463{
Thierry Reding08943e62013-09-26 16:08:18 +0200464 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200465 struct drm_tegra_get_syncpt *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200466 struct tegra_drm_context *context;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200467 struct host1x_syncpt *syncpt;
468
Thierry Redingc88c3632013-09-26 16:08:22 +0200469 context = tegra_drm_get_context(args->context);
470
Thierry Reding08943e62013-09-26 16:08:18 +0200471 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200472 return -ENODEV;
473
Thierry Reding53fa7f72013-09-24 15:35:40 +0200474 if (args->index >= context->client->base.num_syncpts)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200475 return -EINVAL;
476
Thierry Reding53fa7f72013-09-24 15:35:40 +0200477 syncpt = context->client->base.syncpts[args->index];
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200478 args->id = host1x_syncpt_id(syncpt);
479
480 return 0;
481}
482
483static int tegra_submit(struct drm_device *drm, void *data,
484 struct drm_file *file)
485{
Thierry Reding08943e62013-09-26 16:08:18 +0200486 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200487 struct drm_tegra_submit *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200488 struct tegra_drm_context *context;
489
490 context = tegra_drm_get_context(args->context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200491
Thierry Reding08943e62013-09-26 16:08:18 +0200492 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200493 return -ENODEV;
494
495 return context->client->ops->submit(context, args, drm, file);
496}
497#endif
498
Rob Clarkbaa70942013-08-02 13:27:49 -0400499static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200500#ifdef CONFIG_DRM_TEGRA_STAGING
501 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
502 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
503 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
504 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
505 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
506 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
507 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
508 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
509 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
510#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000511};
512
513static const struct file_operations tegra_drm_fops = {
514 .owner = THIS_MODULE,
515 .open = drm_open,
516 .release = drm_release,
517 .unlocked_ioctl = drm_ioctl,
Arto Merilainende2ba662013-03-22 16:34:08 +0200518 .mmap = tegra_drm_mmap,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000519 .poll = drm_poll,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000520 .read = drm_read,
521#ifdef CONFIG_COMPAT
522 .compat_ioctl = drm_compat_ioctl,
523#endif
524 .llseek = noop_llseek,
525};
526
Thierry Reding6e5ff992012-11-28 11:45:47 +0100527static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
528{
529 struct drm_crtc *crtc;
530
531 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
532 struct tegra_dc *dc = to_tegra_dc(crtc);
533
534 if (dc->pipe == pipe)
535 return crtc;
536 }
537
538 return NULL;
539}
540
541static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
542{
543 /* TODO: implement real hardware counter using syncpoints */
544 return drm_vblank_count(dev, crtc);
545}
546
547static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
548{
549 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
550 struct tegra_dc *dc = to_tegra_dc(crtc);
551
552 if (!crtc)
553 return -ENODEV;
554
555 tegra_dc_enable_vblank(dc);
556
557 return 0;
558}
559
560static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
561{
562 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
563 struct tegra_dc *dc = to_tegra_dc(crtc);
564
565 if (crtc)
566 tegra_dc_disable_vblank(dc);
567}
568
Thierry Reding3c03c462012-11-28 12:00:18 +0100569static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
570{
Thierry Reding08943e62013-09-26 16:08:18 +0200571 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Redingc88c3632013-09-26 16:08:22 +0200572 struct tegra_drm_context *context, *tmp;
Thierry Reding3c03c462012-11-28 12:00:18 +0100573 struct drm_crtc *crtc;
574
575 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
576 tegra_dc_cancel_page_flip(crtc, file);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200577
578 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
Thierry Redingc88c3632013-09-26 16:08:22 +0200579 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200580
581 kfree(fpriv);
Thierry Reding3c03c462012-11-28 12:00:18 +0100582}
583
Thierry Redinge450fcc2013-02-13 16:13:16 +0100584#ifdef CONFIG_DEBUG_FS
585static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
586{
587 struct drm_info_node *node = (struct drm_info_node *)s->private;
588 struct drm_device *drm = node->minor->dev;
589 struct drm_framebuffer *fb;
590
591 mutex_lock(&drm->mode_config.fb_lock);
592
593 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
594 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
595 fb->base.id, fb->width, fb->height, fb->depth,
596 fb->bits_per_pixel,
597 atomic_read(&fb->refcount.refcount));
598 }
599
600 mutex_unlock(&drm->mode_config.fb_lock);
601
602 return 0;
603}
604
605static struct drm_info_list tegra_debugfs_list[] = {
606 { "framebuffers", tegra_debugfs_framebuffers, 0 },
607};
608
609static int tegra_debugfs_init(struct drm_minor *minor)
610{
611 return drm_debugfs_create_files(tegra_debugfs_list,
612 ARRAY_SIZE(tegra_debugfs_list),
613 minor->debugfs_root, minor);
614}
615
616static void tegra_debugfs_cleanup(struct drm_minor *minor)
617{
618 drm_debugfs_remove_files(tegra_debugfs_list,
619 ARRAY_SIZE(tegra_debugfs_list), minor);
620}
621#endif
622
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000623struct drm_driver tegra_drm_driver = {
Laurent Pinchart604faa72013-05-29 07:44:34 +0200624 .driver_features = DRIVER_MODESET | DRIVER_GEM,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000625 .load = tegra_drm_load,
626 .unload = tegra_drm_unload,
627 .open = tegra_drm_open,
Thierry Reding3c03c462012-11-28 12:00:18 +0100628 .preclose = tegra_drm_preclose,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000629 .lastclose = tegra_drm_lastclose,
630
Thierry Reding6e5ff992012-11-28 11:45:47 +0100631 .get_vblank_counter = tegra_drm_get_vblank_counter,
632 .enable_vblank = tegra_drm_enable_vblank,
633 .disable_vblank = tegra_drm_disable_vblank,
634
Thierry Redinge450fcc2013-02-13 16:13:16 +0100635#if defined(CONFIG_DEBUG_FS)
636 .debugfs_init = tegra_debugfs_init,
637 .debugfs_cleanup = tegra_debugfs_cleanup,
638#endif
639
Arto Merilainende2ba662013-03-22 16:34:08 +0200640 .gem_free_object = tegra_bo_free_object,
641 .gem_vm_ops = &tegra_bo_vm_ops,
642 .dumb_create = tegra_bo_dumb_create,
643 .dumb_map_offset = tegra_bo_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200644 .dumb_destroy = drm_gem_dumb_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000645
646 .ioctls = tegra_drm_ioctls,
647 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
648 .fops = &tegra_drm_fops,
649
650 .name = DRIVER_NAME,
651 .desc = DRIVER_DESC,
652 .date = DRIVER_DATE,
653 .major = DRIVER_MAJOR,
654 .minor = DRIVER_MINOR,
655 .patchlevel = DRIVER_PATCHLEVEL,
656};