2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012-2013 NVIDIA CORPORATION. All rights reserved.
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.
10 #include "host1x_client.h"
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
23 struct tegra_drm_file {
24 struct list_head contexts;
27 struct host1x_subdev {
28 struct host1x_client *client;
29 struct device_node *np;
30 struct list_head list;
33 static int host1x_subdev_add(struct tegra_drm *tegra, struct device_node *np)
35 struct host1x_subdev *subdev;
37 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
41 INIT_LIST_HEAD(&subdev->list);
42 subdev->np = of_node_get(np);
44 list_add_tail(&subdev->list, &tegra->subdevs);
49 static int host1x_subdev_register(struct tegra_drm *tegra,
50 struct host1x_subdev *subdev,
51 struct host1x_client *client)
53 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);
62 static int host1x_subdev_unregister(struct tegra_drm *tegra,
63 struct host1x_subdev *subdev)
65 mutex_lock(&tegra->subdevs_lock);
66 list_del_init(&subdev->list);
67 mutex_unlock(&tegra->subdevs_lock);
69 of_node_put(subdev->np);
75 static int tegra_parse_dt(struct tegra_drm *tegra)
77 static const char * const compat[] = {
79 "nvidia,tegra20-hdmi",
80 "nvidia,tegra20-gr2d",
82 "nvidia,tegra30-hdmi",
83 "nvidia,tegra30-gr2d",
88 for (i = 0; i < ARRAY_SIZE(compat); i++) {
89 struct device_node *np;
91 for_each_child_of_node(tegra->dev->of_node, np) {
92 if (of_device_is_compatible(np, compat[i]) &&
93 of_device_is_available(np)) {
94 err = host1x_subdev_add(tegra, np);
104 int tegra_drm_alloc(struct platform_device *pdev)
106 struct tegra_drm *tegra;
109 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
113 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;
120 err = tegra_parse_dt(tegra);
122 dev_err(&pdev->dev, "failed to parse DT: %d\n", err);
126 host1x_set_drm_data(&pdev->dev, tegra);
131 int tegra_drm_init(struct tegra_drm *tegra, struct drm_device *drm)
133 struct host1x_client *client;
135 mutex_lock(&tegra->clients_lock);
137 list_for_each_entry(client, &tegra->clients, list) {
138 if (client->ops && client->ops->drm_init) {
139 int err = client->ops->drm_init(client, drm);
142 "DRM setup failed for %s: %d\n",
143 dev_name(client->dev), err);
144 mutex_unlock(&tegra->clients_lock);
150 mutex_unlock(&tegra->clients_lock);
155 int tegra_drm_exit(struct tegra_drm *tegra)
157 struct platform_device *pdev = to_platform_device(tegra->dev);
158 struct host1x_client *client;
163 mutex_lock(&tegra->clients_lock);
165 list_for_each_entry_reverse(client, &tegra->clients, list) {
166 if (client->ops && client->ops->drm_exit) {
167 int err = client->ops->drm_exit(client);
170 "DRM cleanup failed for %s: %d\n",
171 dev_name(client->dev), err);
172 mutex_unlock(&tegra->clients_lock);
178 mutex_unlock(&tegra->clients_lock);
180 drm_platform_exit(&tegra_drm_driver, pdev);
186 int host1x_register_client(struct tegra_drm *tegra,
187 struct host1x_client *client)
189 struct host1x_subdev *subdev, *tmp;
192 mutex_lock(&tegra->clients_lock);
193 list_add_tail(&client->list, &tegra->clients);
194 mutex_unlock(&tegra->clients_lock);
196 list_for_each_entry_safe(subdev, tmp, &tegra->subdevs, list)
197 if (subdev->np == client->dev->of_node)
198 host1x_subdev_register(tegra, subdev, client);
200 if (list_empty(&tegra->subdevs)) {
201 struct platform_device *pdev = to_platform_device(tegra->dev);
203 err = drm_platform_init(&tegra_drm_driver, pdev);
205 dev_err(tegra->dev, "drm_platform_init(): %d\n", err);
213 int host1x_unregister_client(struct tegra_drm *tegra,
214 struct host1x_client *client)
216 struct host1x_subdev *subdev, *tmp;
219 list_for_each_entry_safe(subdev, tmp, &tegra->active, list) {
220 if (subdev->client == client) {
221 err = tegra_drm_exit(tegra);
223 dev_err(tegra->dev, "tegra_drm_exit(): %d\n",
228 host1x_subdev_unregister(tegra, subdev);
233 mutex_lock(&tegra->clients_lock);
234 list_del_init(&client->list);
235 mutex_unlock(&tegra->clients_lock);
240 static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
242 struct tegra_drm *tegra;
245 tegra = host1x_get_drm_data(drm->dev);
246 drm->dev_private = tegra;
249 drm_mode_config_init(drm);
251 err = tegra_drm_init(tegra, drm);
256 * We don't use the drm_irq_install() helpers provided by the DRM
257 * core, so we need to set this manually in order to allow the
258 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
260 drm->irq_enabled = true;
262 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
266 err = tegra_drm_fb_init(drm);
270 drm_kms_helper_poll_init(drm);
275 static int tegra_drm_unload(struct drm_device *drm)
277 drm_kms_helper_poll_fini(drm);
278 tegra_drm_fb_exit(drm);
280 drm_mode_config_cleanup(drm);
285 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
287 struct tegra_drm_file *fpriv;
289 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
293 INIT_LIST_HEAD(&fpriv->contexts);
294 filp->driver_priv = fpriv;
299 static void tegra_drm_context_free(struct tegra_drm_context *context)
301 context->client->ops->close_channel(context);
305 static void tegra_drm_lastclose(struct drm_device *drm)
307 struct tegra_drm *tegra = drm->dev_private;
309 tegra_fbdev_restore_mode(tegra->fbdev);
312 #ifdef CONFIG_DRM_TEGRA_STAGING
313 static struct tegra_drm_context *tegra_drm_get_context(__u64 context)
315 return (struct tegra_drm_context *)(uintptr_t)context;
318 static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
319 struct tegra_drm_context *context)
321 struct tegra_drm_context *ctx;
323 list_for_each_entry(ctx, &file->contexts, list)
330 static int tegra_gem_create(struct drm_device *drm, void *data,
331 struct drm_file *file)
333 struct drm_tegra_gem_create *args = data;
336 bo = tegra_bo_create_with_handle(file, drm, args->size,
344 static int tegra_gem_mmap(struct drm_device *drm, void *data,
345 struct drm_file *file)
347 struct drm_tegra_gem_mmap *args = data;
348 struct drm_gem_object *gem;
351 gem = drm_gem_object_lookup(drm, file, args->handle);
355 bo = to_tegra_bo(gem);
357 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
359 drm_gem_object_unreference(gem);
364 static int tegra_syncpt_read(struct drm_device *drm, void *data,
365 struct drm_file *file)
367 struct drm_tegra_syncpt_read *args = data;
368 struct host1x *host = dev_get_drvdata(drm->dev);
369 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
374 args->value = host1x_syncpt_read_min(sp);
378 static int tegra_syncpt_incr(struct drm_device *drm, void *data,
379 struct drm_file *file)
381 struct drm_tegra_syncpt_incr *args = data;
382 struct host1x *host = dev_get_drvdata(drm->dev);
383 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
388 return host1x_syncpt_incr(sp);
391 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
392 struct drm_file *file)
394 struct drm_tegra_syncpt_wait *args = data;
395 struct host1x *host = dev_get_drvdata(drm->dev);
396 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
401 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
405 static int tegra_open_channel(struct drm_device *drm, void *data,
406 struct drm_file *file)
408 struct tegra_drm_file *fpriv = file->driver_priv;
409 struct tegra_drm *tegra = drm->dev_private;
410 struct drm_tegra_open_channel *args = data;
411 struct tegra_drm_context *context;
412 struct host1x_client *client;
415 context = kzalloc(sizeof(*context), GFP_KERNEL);
419 list_for_each_entry(client, &tegra->clients, list)
420 if (client->class == args->client) {
421 err = client->ops->open_channel(client, context);
425 context->client = client;
426 list_add(&context->list, &fpriv->contexts);
427 args->context = (uintptr_t)context;
435 static int tegra_close_channel(struct drm_device *drm, void *data,
436 struct drm_file *file)
438 struct drm_tegra_close_channel *args = data;
439 struct tegra_drm_file *fpriv = file->driver_priv;
440 struct tegra_drm_context *context;
442 context = tegra_drm_get_context(args->context);
444 if (!tegra_drm_file_owns_context(fpriv, context))
447 list_del(&context->list);
448 tegra_drm_context_free(context);
453 static int tegra_get_syncpt(struct drm_device *drm, void *data,
454 struct drm_file *file)
456 struct tegra_drm_file *fpriv = file->driver_priv;
457 struct drm_tegra_get_syncpt *args = data;
458 struct tegra_drm_context *context;
459 struct host1x_syncpt *syncpt;
461 context = tegra_drm_get_context(args->context);
463 if (!tegra_drm_file_owns_context(fpriv, context))
466 if (args->index >= context->client->num_syncpts)
469 syncpt = context->client->syncpts[args->index];
470 args->id = host1x_syncpt_id(syncpt);
475 static int tegra_submit(struct drm_device *drm, void *data,
476 struct drm_file *file)
478 struct tegra_drm_file *fpriv = file->driver_priv;
479 struct drm_tegra_submit *args = data;
480 struct tegra_drm_context *context;
482 context = tegra_drm_get_context(args->context);
484 if (!tegra_drm_file_owns_context(fpriv, context))
487 return context->client->ops->submit(context, args, drm, file);
491 static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
492 #ifdef CONFIG_DRM_TEGRA_STAGING
493 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
494 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
495 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
496 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
497 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
498 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
499 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
500 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
501 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
505 static const struct file_operations tegra_drm_fops = {
506 .owner = THIS_MODULE,
508 .release = drm_release,
509 .unlocked_ioctl = drm_ioctl,
510 .mmap = tegra_drm_mmap,
514 .compat_ioctl = drm_compat_ioctl,
516 .llseek = noop_llseek,
519 static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
521 struct drm_crtc *crtc;
523 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
524 struct tegra_dc *dc = to_tegra_dc(crtc);
526 if (dc->pipe == pipe)
533 static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
535 /* TODO: implement real hardware counter using syncpoints */
536 return drm_vblank_count(dev, crtc);
539 static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
541 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
542 struct tegra_dc *dc = to_tegra_dc(crtc);
547 tegra_dc_enable_vblank(dc);
552 static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
554 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
555 struct tegra_dc *dc = to_tegra_dc(crtc);
558 tegra_dc_disable_vblank(dc);
561 static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
563 struct tegra_drm_file *fpriv = file->driver_priv;
564 struct tegra_drm_context *context, *tmp;
565 struct drm_crtc *crtc;
567 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
568 tegra_dc_cancel_page_flip(crtc, file);
570 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
571 tegra_drm_context_free(context);
576 #ifdef CONFIG_DEBUG_FS
577 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
579 struct drm_info_node *node = (struct drm_info_node *)s->private;
580 struct drm_device *drm = node->minor->dev;
581 struct drm_framebuffer *fb;
583 mutex_lock(&drm->mode_config.fb_lock);
585 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
586 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
587 fb->base.id, fb->width, fb->height, fb->depth,
589 atomic_read(&fb->refcount.refcount));
592 mutex_unlock(&drm->mode_config.fb_lock);
597 static struct drm_info_list tegra_debugfs_list[] = {
598 { "framebuffers", tegra_debugfs_framebuffers, 0 },
601 static int tegra_debugfs_init(struct drm_minor *minor)
603 return drm_debugfs_create_files(tegra_debugfs_list,
604 ARRAY_SIZE(tegra_debugfs_list),
605 minor->debugfs_root, minor);
608 static void tegra_debugfs_cleanup(struct drm_minor *minor)
610 drm_debugfs_remove_files(tegra_debugfs_list,
611 ARRAY_SIZE(tegra_debugfs_list), minor);
615 struct drm_driver tegra_drm_driver = {
616 .driver_features = DRIVER_MODESET | DRIVER_GEM,
617 .load = tegra_drm_load,
618 .unload = tegra_drm_unload,
619 .open = tegra_drm_open,
620 .preclose = tegra_drm_preclose,
621 .lastclose = tegra_drm_lastclose,
623 .get_vblank_counter = tegra_drm_get_vblank_counter,
624 .enable_vblank = tegra_drm_enable_vblank,
625 .disable_vblank = tegra_drm_disable_vblank,
627 #if defined(CONFIG_DEBUG_FS)
628 .debugfs_init = tegra_debugfs_init,
629 .debugfs_cleanup = tegra_debugfs_cleanup,
632 .gem_free_object = tegra_bo_free_object,
633 .gem_vm_ops = &tegra_bo_vm_ops,
634 .dumb_create = tegra_bo_dumb_create,
635 .dumb_map_offset = tegra_bo_dumb_map_offset,
636 .dumb_destroy = drm_gem_dumb_destroy,
638 .ioctls = tegra_drm_ioctls,
639 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
640 .fops = &tegra_drm_fops,
645 .major = DRIVER_MAJOR,
646 .minor = DRIVER_MINOR,
647 .patchlevel = DRIVER_PATCHLEVEL,