drm/tegra: Rename host1x_drm_context to tegra_drm_context
[deliverable/linux.git] / drivers / gpu / host1x / drm / drm.c
1 /*
2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012-2013 NVIDIA CORPORATION. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10 #include <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
13
14 #include <linux/dma-mapping.h>
15 #include <asm/dma-iommu.h>
16
17 #include <drm/drm.h>
18 #include <drm/drmP.h>
19
20 #include "host1x_client.h"
21 #include "dev.h"
22 #include "drm.h"
23 #include "gem.h"
24 #include "syncpt.h"
25
26 #define DRIVER_NAME "tegra"
27 #define DRIVER_DESC "NVIDIA Tegra graphics"
28 #define DRIVER_DATE "20120330"
29 #define DRIVER_MAJOR 0
30 #define DRIVER_MINOR 0
31 #define DRIVER_PATCHLEVEL 0
32
33 struct tegra_drm_file {
34 struct list_head contexts;
35 };
36
37 struct host1x_subdev {
38 struct host1x_client *client;
39 struct device_node *np;
40 struct list_head list;
41 };
42
43 static int host1x_subdev_add(struct tegra_drm *tegra, struct device_node *np)
44 {
45 struct host1x_subdev *subdev;
46
47 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
48 if (!subdev)
49 return -ENOMEM;
50
51 INIT_LIST_HEAD(&subdev->list);
52 subdev->np = of_node_get(np);
53
54 list_add_tail(&subdev->list, &tegra->subdevs);
55
56 return 0;
57 }
58
59 static int host1x_subdev_register(struct tegra_drm *tegra,
60 struct host1x_subdev *subdev,
61 struct host1x_client *client)
62 {
63 mutex_lock(&tegra->subdevs_lock);
64 list_del_init(&subdev->list);
65 list_add_tail(&subdev->list, &tegra->active);
66 subdev->client = client;
67 mutex_unlock(&tegra->subdevs_lock);
68
69 return 0;
70 }
71
72 static int host1x_subdev_unregister(struct tegra_drm *tegra,
73 struct host1x_subdev *subdev)
74 {
75 mutex_lock(&tegra->subdevs_lock);
76 list_del_init(&subdev->list);
77 mutex_unlock(&tegra->subdevs_lock);
78
79 of_node_put(subdev->np);
80 kfree(subdev);
81
82 return 0;
83 }
84
85 static int tegra_parse_dt(struct tegra_drm *tegra)
86 {
87 static const char * const compat[] = {
88 "nvidia,tegra20-dc",
89 "nvidia,tegra20-hdmi",
90 "nvidia,tegra20-gr2d",
91 "nvidia,tegra30-dc",
92 "nvidia,tegra30-hdmi",
93 "nvidia,tegra30-gr2d",
94 };
95 unsigned int i;
96 int err;
97
98 for (i = 0; i < ARRAY_SIZE(compat); i++) {
99 struct device_node *np;
100
101 for_each_child_of_node(tegra->dev->of_node, np) {
102 if (of_device_is_compatible(np, compat[i]) &&
103 of_device_is_available(np)) {
104 err = host1x_subdev_add(tegra, np);
105 if (err < 0)
106 return err;
107 }
108 }
109 }
110
111 return 0;
112 }
113
114 int tegra_drm_alloc(struct platform_device *pdev)
115 {
116 struct tegra_drm *tegra;
117 int err;
118
119 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
120 if (!tegra)
121 return -ENOMEM;
122
123 mutex_init(&tegra->subdevs_lock);
124 INIT_LIST_HEAD(&tegra->subdevs);
125 INIT_LIST_HEAD(&tegra->active);
126 mutex_init(&tegra->clients_lock);
127 INIT_LIST_HEAD(&tegra->clients);
128 tegra->dev = &pdev->dev;
129
130 err = tegra_parse_dt(tegra);
131 if (err < 0) {
132 dev_err(&pdev->dev, "failed to parse DT: %d\n", err);
133 return err;
134 }
135
136 host1x_set_drm_data(&pdev->dev, tegra);
137
138 return 0;
139 }
140
141 int tegra_drm_init(struct tegra_drm *tegra, struct drm_device *drm)
142 {
143 struct host1x_client *client;
144
145 mutex_lock(&tegra->clients_lock);
146
147 list_for_each_entry(client, &tegra->clients, list) {
148 if (client->ops && client->ops->drm_init) {
149 int err = client->ops->drm_init(client, drm);
150 if (err < 0) {
151 dev_err(tegra->dev,
152 "DRM setup failed for %s: %d\n",
153 dev_name(client->dev), err);
154 mutex_unlock(&tegra->clients_lock);
155 return err;
156 }
157 }
158 }
159
160 mutex_unlock(&tegra->clients_lock);
161
162 return 0;
163 }
164
165 int tegra_drm_exit(struct tegra_drm *tegra)
166 {
167 struct platform_device *pdev = to_platform_device(tegra->dev);
168 struct host1x_client *client;
169
170 if (!tegra->drm)
171 return 0;
172
173 mutex_lock(&tegra->clients_lock);
174
175 list_for_each_entry_reverse(client, &tegra->clients, list) {
176 if (client->ops && client->ops->drm_exit) {
177 int err = client->ops->drm_exit(client);
178 if (err < 0) {
179 dev_err(tegra->dev,
180 "DRM cleanup failed for %s: %d\n",
181 dev_name(client->dev), err);
182 mutex_unlock(&tegra->clients_lock);
183 return err;
184 }
185 }
186 }
187
188 mutex_unlock(&tegra->clients_lock);
189
190 drm_platform_exit(&tegra_drm_driver, pdev);
191 tegra->drm = NULL;
192
193 return 0;
194 }
195
196 int host1x_register_client(struct tegra_drm *tegra,
197 struct host1x_client *client)
198 {
199 struct host1x_subdev *subdev, *tmp;
200 int err;
201
202 mutex_lock(&tegra->clients_lock);
203 list_add_tail(&client->list, &tegra->clients);
204 mutex_unlock(&tegra->clients_lock);
205
206 list_for_each_entry_safe(subdev, tmp, &tegra->subdevs, list)
207 if (subdev->np == client->dev->of_node)
208 host1x_subdev_register(tegra, subdev, client);
209
210 if (list_empty(&tegra->subdevs)) {
211 struct platform_device *pdev = to_platform_device(tegra->dev);
212
213 err = drm_platform_init(&tegra_drm_driver, pdev);
214 if (err < 0) {
215 dev_err(tegra->dev, "drm_platform_init(): %d\n", err);
216 return err;
217 }
218 }
219
220 return 0;
221 }
222
223 int host1x_unregister_client(struct tegra_drm *tegra,
224 struct host1x_client *client)
225 {
226 struct host1x_subdev *subdev, *tmp;
227 int err;
228
229 list_for_each_entry_safe(subdev, tmp, &tegra->active, list) {
230 if (subdev->client == client) {
231 err = tegra_drm_exit(tegra);
232 if (err < 0) {
233 dev_err(tegra->dev, "tegra_drm_exit(): %d\n",
234 err);
235 return err;
236 }
237
238 host1x_subdev_unregister(tegra, subdev);
239 break;
240 }
241 }
242
243 mutex_lock(&tegra->clients_lock);
244 list_del_init(&client->list);
245 mutex_unlock(&tegra->clients_lock);
246
247 return 0;
248 }
249
250 static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
251 {
252 struct tegra_drm *tegra;
253 int err;
254
255 tegra = host1x_get_drm_data(drm->dev);
256 drm->dev_private = tegra;
257 tegra->drm = drm;
258
259 drm_mode_config_init(drm);
260
261 err = tegra_drm_init(tegra, drm);
262 if (err < 0)
263 return err;
264
265 /*
266 * We don't use the drm_irq_install() helpers provided by the DRM
267 * core, so we need to set this manually in order to allow the
268 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
269 */
270 drm->irq_enabled = true;
271
272 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
273 if (err < 0)
274 return err;
275
276 err = tegra_drm_fb_init(drm);
277 if (err < 0)
278 return err;
279
280 drm_kms_helper_poll_init(drm);
281
282 return 0;
283 }
284
285 static int tegra_drm_unload(struct drm_device *drm)
286 {
287 drm_kms_helper_poll_fini(drm);
288 tegra_drm_fb_exit(drm);
289
290 drm_mode_config_cleanup(drm);
291
292 return 0;
293 }
294
295 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
296 {
297 struct tegra_drm_file *fpriv;
298
299 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
300 if (!fpriv)
301 return -ENOMEM;
302
303 INIT_LIST_HEAD(&fpriv->contexts);
304 filp->driver_priv = fpriv;
305
306 return 0;
307 }
308
309 static void tegra_drm_context_free(struct tegra_drm_context *context)
310 {
311 context->client->ops->close_channel(context);
312 kfree(context);
313 }
314
315 static void tegra_drm_lastclose(struct drm_device *drm)
316 {
317 struct tegra_drm *tegra = drm->dev_private;
318
319 tegra_fbdev_restore_mode(tegra->fbdev);
320 }
321
322 #ifdef CONFIG_DRM_TEGRA_STAGING
323 static struct tegra_drm_context *tegra_drm_get_context(__u64 context)
324 {
325 return (struct tegra_drm_context *)(uintptr_t)context;
326 }
327
328 static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
329 struct tegra_drm_context *context)
330 {
331 struct tegra_drm_context *ctx;
332
333 list_for_each_entry(ctx, &file->contexts, list)
334 if (ctx == context)
335 return true;
336
337 return false;
338 }
339
340 static int tegra_gem_create(struct drm_device *drm, void *data,
341 struct drm_file *file)
342 {
343 struct drm_tegra_gem_create *args = data;
344 struct tegra_bo *bo;
345
346 bo = tegra_bo_create_with_handle(file, drm, args->size,
347 &args->handle);
348 if (IS_ERR(bo))
349 return PTR_ERR(bo);
350
351 return 0;
352 }
353
354 static int tegra_gem_mmap(struct drm_device *drm, void *data,
355 struct drm_file *file)
356 {
357 struct drm_tegra_gem_mmap *args = data;
358 struct drm_gem_object *gem;
359 struct tegra_bo *bo;
360
361 gem = drm_gem_object_lookup(drm, file, args->handle);
362 if (!gem)
363 return -EINVAL;
364
365 bo = to_tegra_bo(gem);
366
367 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
368
369 drm_gem_object_unreference(gem);
370
371 return 0;
372 }
373
374 static int tegra_syncpt_read(struct drm_device *drm, void *data,
375 struct drm_file *file)
376 {
377 struct drm_tegra_syncpt_read *args = data;
378 struct host1x *host = dev_get_drvdata(drm->dev);
379 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
380
381 if (!sp)
382 return -EINVAL;
383
384 args->value = host1x_syncpt_read_min(sp);
385 return 0;
386 }
387
388 static int tegra_syncpt_incr(struct drm_device *drm, void *data,
389 struct drm_file *file)
390 {
391 struct drm_tegra_syncpt_incr *args = data;
392 struct host1x *host = dev_get_drvdata(drm->dev);
393 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
394
395 if (!sp)
396 return -EINVAL;
397
398 return host1x_syncpt_incr(sp);
399 }
400
401 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
402 struct drm_file *file)
403 {
404 struct drm_tegra_syncpt_wait *args = data;
405 struct host1x *host = dev_get_drvdata(drm->dev);
406 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
407
408 if (!sp)
409 return -EINVAL;
410
411 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
412 &args->value);
413 }
414
415 static int tegra_open_channel(struct drm_device *drm, void *data,
416 struct drm_file *file)
417 {
418 struct tegra_drm_file *fpriv = file->driver_priv;
419 struct tegra_drm *tegra = drm->dev_private;
420 struct drm_tegra_open_channel *args = data;
421 struct tegra_drm_context *context;
422 struct host1x_client *client;
423 int err = -ENODEV;
424
425 context = kzalloc(sizeof(*context), GFP_KERNEL);
426 if (!context)
427 return -ENOMEM;
428
429 list_for_each_entry(client, &tegra->clients, list)
430 if (client->class == args->client) {
431 err = client->ops->open_channel(client, context);
432 if (err)
433 break;
434
435 context->client = client;
436 list_add(&context->list, &fpriv->contexts);
437 args->context = (uintptr_t)context;
438 return 0;
439 }
440
441 kfree(context);
442 return err;
443 }
444
445 static int tegra_close_channel(struct drm_device *drm, void *data,
446 struct drm_file *file)
447 {
448 struct drm_tegra_close_channel *args = data;
449 struct tegra_drm_file *fpriv = file->driver_priv;
450 struct tegra_drm_context *context;
451
452 context = tegra_drm_get_context(args->context);
453
454 if (!tegra_drm_file_owns_context(fpriv, context))
455 return -EINVAL;
456
457 list_del(&context->list);
458 tegra_drm_context_free(context);
459
460 return 0;
461 }
462
463 static int tegra_get_syncpt(struct drm_device *drm, void *data,
464 struct drm_file *file)
465 {
466 struct tegra_drm_file *fpriv = file->driver_priv;
467 struct drm_tegra_get_syncpt *args = data;
468 struct tegra_drm_context *context;
469 struct host1x_syncpt *syncpt;
470
471 context = tegra_drm_get_context(args->context);
472
473 if (!tegra_drm_file_owns_context(fpriv, context))
474 return -ENODEV;
475
476 if (args->index >= context->client->num_syncpts)
477 return -EINVAL;
478
479 syncpt = context->client->syncpts[args->index];
480 args->id = host1x_syncpt_id(syncpt);
481
482 return 0;
483 }
484
485 static int tegra_submit(struct drm_device *drm, void *data,
486 struct drm_file *file)
487 {
488 struct tegra_drm_file *fpriv = file->driver_priv;
489 struct drm_tegra_submit *args = data;
490 struct tegra_drm_context *context;
491
492 context = tegra_drm_get_context(args->context);
493
494 if (!tegra_drm_file_owns_context(fpriv, context))
495 return -ENODEV;
496
497 return context->client->ops->submit(context, args, drm, file);
498 }
499 #endif
500
501 static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
502 #ifdef CONFIG_DRM_TEGRA_STAGING
503 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
504 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
505 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
506 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
507 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
508 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
509 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
510 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
511 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
512 #endif
513 };
514
515 static const struct file_operations tegra_drm_fops = {
516 .owner = THIS_MODULE,
517 .open = drm_open,
518 .release = drm_release,
519 .unlocked_ioctl = drm_ioctl,
520 .mmap = tegra_drm_mmap,
521 .poll = drm_poll,
522 .read = drm_read,
523 #ifdef CONFIG_COMPAT
524 .compat_ioctl = drm_compat_ioctl,
525 #endif
526 .llseek = noop_llseek,
527 };
528
529 static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
530 {
531 struct drm_crtc *crtc;
532
533 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
534 struct tegra_dc *dc = to_tegra_dc(crtc);
535
536 if (dc->pipe == pipe)
537 return crtc;
538 }
539
540 return NULL;
541 }
542
543 static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
544 {
545 /* TODO: implement real hardware counter using syncpoints */
546 return drm_vblank_count(dev, crtc);
547 }
548
549 static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
550 {
551 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
552 struct tegra_dc *dc = to_tegra_dc(crtc);
553
554 if (!crtc)
555 return -ENODEV;
556
557 tegra_dc_enable_vblank(dc);
558
559 return 0;
560 }
561
562 static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
563 {
564 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
565 struct tegra_dc *dc = to_tegra_dc(crtc);
566
567 if (crtc)
568 tegra_dc_disable_vblank(dc);
569 }
570
571 static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
572 {
573 struct tegra_drm_file *fpriv = file->driver_priv;
574 struct tegra_drm_context *context, *tmp;
575 struct drm_crtc *crtc;
576
577 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
578 tegra_dc_cancel_page_flip(crtc, file);
579
580 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
581 tegra_drm_context_free(context);
582
583 kfree(fpriv);
584 }
585
586 #ifdef CONFIG_DEBUG_FS
587 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
588 {
589 struct drm_info_node *node = (struct drm_info_node *)s->private;
590 struct drm_device *drm = node->minor->dev;
591 struct drm_framebuffer *fb;
592
593 mutex_lock(&drm->mode_config.fb_lock);
594
595 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
596 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
597 fb->base.id, fb->width, fb->height, fb->depth,
598 fb->bits_per_pixel,
599 atomic_read(&fb->refcount.refcount));
600 }
601
602 mutex_unlock(&drm->mode_config.fb_lock);
603
604 return 0;
605 }
606
607 static struct drm_info_list tegra_debugfs_list[] = {
608 { "framebuffers", tegra_debugfs_framebuffers, 0 },
609 };
610
611 static int tegra_debugfs_init(struct drm_minor *minor)
612 {
613 return drm_debugfs_create_files(tegra_debugfs_list,
614 ARRAY_SIZE(tegra_debugfs_list),
615 minor->debugfs_root, minor);
616 }
617
618 static void tegra_debugfs_cleanup(struct drm_minor *minor)
619 {
620 drm_debugfs_remove_files(tegra_debugfs_list,
621 ARRAY_SIZE(tegra_debugfs_list), minor);
622 }
623 #endif
624
625 struct drm_driver tegra_drm_driver = {
626 .driver_features = DRIVER_MODESET | DRIVER_GEM,
627 .load = tegra_drm_load,
628 .unload = tegra_drm_unload,
629 .open = tegra_drm_open,
630 .preclose = tegra_drm_preclose,
631 .lastclose = tegra_drm_lastclose,
632
633 .get_vblank_counter = tegra_drm_get_vblank_counter,
634 .enable_vblank = tegra_drm_enable_vblank,
635 .disable_vblank = tegra_drm_disable_vblank,
636
637 #if defined(CONFIG_DEBUG_FS)
638 .debugfs_init = tegra_debugfs_init,
639 .debugfs_cleanup = tegra_debugfs_cleanup,
640 #endif
641
642 .gem_free_object = tegra_bo_free_object,
643 .gem_vm_ops = &tegra_bo_vm_ops,
644 .dumb_create = tegra_bo_dumb_create,
645 .dumb_map_offset = tegra_bo_dumb_map_offset,
646 .dumb_destroy = drm_gem_dumb_destroy,
647
648 .ioctls = tegra_drm_ioctls,
649 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
650 .fops = &tegra_drm_fops,
651
652 .name = DRIVER_NAME,
653 .desc = DRIVER_DESC,
654 .date = DRIVER_DATE,
655 .major = DRIVER_MAJOR,
656 .minor = DRIVER_MINOR,
657 .patchlevel = DRIVER_PATCHLEVEL,
658 };
This page took 0.055295 seconds and 6 git commands to generate.