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"
14 #define DRIVER_NAME "tegra"
15 #define DRIVER_DESC "NVIDIA Tegra graphics"
16 #define DRIVER_DATE "20120330"
17 #define DRIVER_MAJOR 0
18 #define DRIVER_MINOR 0
19 #define DRIVER_PATCHLEVEL 0
21 struct tegra_drm_file {
22 struct list_head contexts;
25 struct host1x_subdev {
26 struct host1x_client *client;
27 struct device_node *np;
28 struct list_head list;
31 static int host1x_subdev_add(struct tegra_drm *tegra, struct device_node *np)
33 struct host1x_subdev *subdev;
35 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
39 INIT_LIST_HEAD(&subdev->list);
40 subdev->np = of_node_get(np);
42 list_add_tail(&subdev->list, &tegra->subdevs);
47 static int host1x_subdev_register(struct tegra_drm *tegra,
48 struct host1x_subdev *subdev,
49 struct host1x_client *client)
51 mutex_lock(&tegra->subdevs_lock);
52 list_del_init(&subdev->list);
53 list_add_tail(&subdev->list, &tegra->active);
54 subdev->client = client;
55 mutex_unlock(&tegra->subdevs_lock);
60 static int host1x_subdev_unregister(struct tegra_drm *tegra,
61 struct host1x_subdev *subdev)
63 mutex_lock(&tegra->subdevs_lock);
64 list_del_init(&subdev->list);
65 mutex_unlock(&tegra->subdevs_lock);
67 of_node_put(subdev->np);
73 static int tegra_parse_dt(struct tegra_drm *tegra)
75 static const char * const compat[] = {
77 "nvidia,tegra20-hdmi",
78 "nvidia,tegra20-gr2d",
80 "nvidia,tegra30-hdmi",
81 "nvidia,tegra30-gr2d",
86 for (i = 0; i < ARRAY_SIZE(compat); i++) {
87 struct device_node *np;
89 for_each_child_of_node(tegra->dev->of_node, np) {
90 if (of_device_is_compatible(np, compat[i]) &&
91 of_device_is_available(np)) {
92 err = host1x_subdev_add(tegra, np);
102 int tegra_drm_alloc(struct platform_device *pdev)
104 struct tegra_drm *tegra;
107 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
111 mutex_init(&tegra->subdevs_lock);
112 INIT_LIST_HEAD(&tegra->subdevs);
113 INIT_LIST_HEAD(&tegra->active);
114 mutex_init(&tegra->clients_lock);
115 INIT_LIST_HEAD(&tegra->clients);
116 tegra->dev = &pdev->dev;
118 err = tegra_parse_dt(tegra);
120 dev_err(&pdev->dev, "failed to parse DT: %d\n", err);
124 host1x_set_drm_data(&pdev->dev, tegra);
129 int tegra_drm_init(struct tegra_drm *tegra, struct drm_device *drm)
131 struct host1x_client *client;
134 mutex_lock(&tegra->clients_lock);
136 list_for_each_entry(client, &tegra->clients, list) {
137 struct tegra_drm_client *tdc = to_tegra_drm_client(client);
139 /* associate client with DRM device */
142 if (client->ops && client->ops->init) {
143 err = client->ops->init(client);
146 "DRM setup failed for %s: %d\n",
147 dev_name(client->dev), err);
148 mutex_unlock(&tegra->clients_lock);
154 mutex_unlock(&tegra->clients_lock);
159 int tegra_drm_exit(struct tegra_drm *tegra)
161 struct host1x_client *client;
162 struct platform_device *pdev;
168 mutex_lock(&tegra->clients_lock);
170 list_for_each_entry_reverse(client, &tegra->clients, list) {
171 if (client->ops && client->ops->exit) {
172 err = client->ops->exit(client);
175 "DRM cleanup failed for %s: %d\n",
176 dev_name(client->dev), err);
177 mutex_unlock(&tegra->clients_lock);
183 mutex_unlock(&tegra->clients_lock);
185 pdev = to_platform_device(tegra->dev);
186 drm_platform_exit(&tegra_drm_driver, pdev);
192 int host1x_register_client(struct tegra_drm *tegra,
193 struct host1x_client *client)
195 struct host1x_subdev *subdev, *tmp;
198 mutex_lock(&tegra->clients_lock);
199 list_add_tail(&client->list, &tegra->clients);
200 mutex_unlock(&tegra->clients_lock);
202 list_for_each_entry_safe(subdev, tmp, &tegra->subdevs, list)
203 if (subdev->np == client->dev->of_node)
204 host1x_subdev_register(tegra, subdev, client);
206 if (list_empty(&tegra->subdevs)) {
207 struct platform_device *pdev = to_platform_device(tegra->dev);
209 err = drm_platform_init(&tegra_drm_driver, pdev);
211 dev_err(tegra->dev, "drm_platform_init(): %d\n", err);
219 int host1x_unregister_client(struct tegra_drm *tegra,
220 struct host1x_client *client)
222 struct host1x_subdev *subdev, *tmp;
225 list_for_each_entry_safe(subdev, tmp, &tegra->active, list) {
226 if (subdev->client == client) {
227 err = tegra_drm_exit(tegra);
229 dev_err(tegra->dev, "tegra_drm_exit(): %d\n",
234 host1x_subdev_unregister(tegra, subdev);
239 mutex_lock(&tegra->clients_lock);
240 list_del_init(&client->list);
241 mutex_unlock(&tegra->clients_lock);
246 static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
248 struct tegra_drm *tegra;
251 tegra = host1x_get_drm_data(drm->dev);
252 drm->dev_private = tegra;
255 drm_mode_config_init(drm);
257 err = tegra_drm_init(tegra, drm);
262 * We don't use the drm_irq_install() helpers provided by the DRM
263 * core, so we need to set this manually in order to allow the
264 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
266 drm->irq_enabled = true;
268 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
272 err = tegra_drm_fb_init(drm);
276 drm_kms_helper_poll_init(drm);
281 static int tegra_drm_unload(struct drm_device *drm)
283 drm_kms_helper_poll_fini(drm);
284 tegra_drm_fb_exit(drm);
286 drm_mode_config_cleanup(drm);
291 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
293 struct tegra_drm_file *fpriv;
295 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
299 INIT_LIST_HEAD(&fpriv->contexts);
300 filp->driver_priv = fpriv;
305 static void tegra_drm_context_free(struct tegra_drm_context *context)
307 context->client->ops->close_channel(context);
311 static void tegra_drm_lastclose(struct drm_device *drm)
313 struct tegra_drm *tegra = drm->dev_private;
315 tegra_fbdev_restore_mode(tegra->fbdev);
318 #ifdef CONFIG_DRM_TEGRA_STAGING
319 static struct tegra_drm_context *tegra_drm_get_context(__u64 context)
321 return (struct tegra_drm_context *)(uintptr_t)context;
324 static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
325 struct tegra_drm_context *context)
327 struct tegra_drm_context *ctx;
329 list_for_each_entry(ctx, &file->contexts, list)
336 static int tegra_gem_create(struct drm_device *drm, void *data,
337 struct drm_file *file)
339 struct drm_tegra_gem_create *args = data;
342 bo = tegra_bo_create_with_handle(file, drm, args->size,
350 static int tegra_gem_mmap(struct drm_device *drm, void *data,
351 struct drm_file *file)
353 struct drm_tegra_gem_mmap *args = data;
354 struct drm_gem_object *gem;
357 gem = drm_gem_object_lookup(drm, file, args->handle);
361 bo = to_tegra_bo(gem);
363 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
365 drm_gem_object_unreference(gem);
370 static int tegra_syncpt_read(struct drm_device *drm, void *data,
371 struct drm_file *file)
373 struct drm_tegra_syncpt_read *args = data;
374 struct host1x *host = dev_get_drvdata(drm->dev);
375 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
380 args->value = host1x_syncpt_read_min(sp);
384 static int tegra_syncpt_incr(struct drm_device *drm, void *data,
385 struct drm_file *file)
387 struct drm_tegra_syncpt_incr *args = data;
388 struct host1x *host = dev_get_drvdata(drm->dev);
389 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
394 return host1x_syncpt_incr(sp);
397 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
398 struct drm_file *file)
400 struct drm_tegra_syncpt_wait *args = data;
401 struct host1x *host = dev_get_drvdata(drm->dev);
402 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
407 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
411 static int tegra_open_channel(struct drm_device *drm, void *data,
412 struct drm_file *file)
414 struct tegra_drm_file *fpriv = file->driver_priv;
415 struct tegra_drm *tegra = drm->dev_private;
416 struct drm_tegra_open_channel *args = data;
417 struct tegra_drm_context *context;
418 struct tegra_drm_client *client;
421 context = kzalloc(sizeof(*context), GFP_KERNEL);
425 list_for_each_entry(client, &tegra->clients, base.list)
426 if (client->base.class == args->client) {
427 err = client->ops->open_channel(client, context);
431 list_add(&context->list, &fpriv->contexts);
432 args->context = (uintptr_t)context;
433 context->client = client;
441 static int tegra_close_channel(struct drm_device *drm, void *data,
442 struct drm_file *file)
444 struct drm_tegra_close_channel *args = data;
445 struct tegra_drm_file *fpriv = file->driver_priv;
446 struct tegra_drm_context *context;
448 context = tegra_drm_get_context(args->context);
450 if (!tegra_drm_file_owns_context(fpriv, context))
453 list_del(&context->list);
454 tegra_drm_context_free(context);
459 static int tegra_get_syncpt(struct drm_device *drm, void *data,
460 struct drm_file *file)
462 struct tegra_drm_file *fpriv = file->driver_priv;
463 struct drm_tegra_get_syncpt *args = data;
464 struct tegra_drm_context *context;
465 struct host1x_syncpt *syncpt;
467 context = tegra_drm_get_context(args->context);
469 if (!tegra_drm_file_owns_context(fpriv, context))
472 if (args->index >= context->client->base.num_syncpts)
475 syncpt = context->client->base.syncpts[args->index];
476 args->id = host1x_syncpt_id(syncpt);
481 static int tegra_submit(struct drm_device *drm, void *data,
482 struct drm_file *file)
484 struct tegra_drm_file *fpriv = file->driver_priv;
485 struct drm_tegra_submit *args = data;
486 struct tegra_drm_context *context;
488 context = tegra_drm_get_context(args->context);
490 if (!tegra_drm_file_owns_context(fpriv, context))
493 return context->client->ops->submit(context, args, drm, file);
497 static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
498 #ifdef CONFIG_DRM_TEGRA_STAGING
499 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
500 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
501 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
502 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
503 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
504 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
505 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
506 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
507 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
511 static const struct file_operations tegra_drm_fops = {
512 .owner = THIS_MODULE,
514 .release = drm_release,
515 .unlocked_ioctl = drm_ioctl,
516 .mmap = tegra_drm_mmap,
520 .compat_ioctl = drm_compat_ioctl,
522 .llseek = noop_llseek,
525 static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
527 struct drm_crtc *crtc;
529 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
530 struct tegra_dc *dc = to_tegra_dc(crtc);
532 if (dc->pipe == pipe)
539 static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
541 /* TODO: implement real hardware counter using syncpoints */
542 return drm_vblank_count(dev, crtc);
545 static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
547 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
548 struct tegra_dc *dc = to_tegra_dc(crtc);
553 tegra_dc_enable_vblank(dc);
558 static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
560 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
561 struct tegra_dc *dc = to_tegra_dc(crtc);
564 tegra_dc_disable_vblank(dc);
567 static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
569 struct tegra_drm_file *fpriv = file->driver_priv;
570 struct tegra_drm_context *context, *tmp;
571 struct drm_crtc *crtc;
573 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
574 tegra_dc_cancel_page_flip(crtc, file);
576 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
577 tegra_drm_context_free(context);
582 #ifdef CONFIG_DEBUG_FS
583 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
585 struct drm_info_node *node = (struct drm_info_node *)s->private;
586 struct drm_device *drm = node->minor->dev;
587 struct drm_framebuffer *fb;
589 mutex_lock(&drm->mode_config.fb_lock);
591 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
592 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
593 fb->base.id, fb->width, fb->height, fb->depth,
595 atomic_read(&fb->refcount.refcount));
598 mutex_unlock(&drm->mode_config.fb_lock);
603 static struct drm_info_list tegra_debugfs_list[] = {
604 { "framebuffers", tegra_debugfs_framebuffers, 0 },
607 static int tegra_debugfs_init(struct drm_minor *minor)
609 return drm_debugfs_create_files(tegra_debugfs_list,
610 ARRAY_SIZE(tegra_debugfs_list),
611 minor->debugfs_root, minor);
614 static void tegra_debugfs_cleanup(struct drm_minor *minor)
616 drm_debugfs_remove_files(tegra_debugfs_list,
617 ARRAY_SIZE(tegra_debugfs_list), minor);
621 struct drm_driver tegra_drm_driver = {
622 .driver_features = DRIVER_MODESET | DRIVER_GEM,
623 .load = tegra_drm_load,
624 .unload = tegra_drm_unload,
625 .open = tegra_drm_open,
626 .preclose = tegra_drm_preclose,
627 .lastclose = tegra_drm_lastclose,
629 .get_vblank_counter = tegra_drm_get_vblank_counter,
630 .enable_vblank = tegra_drm_enable_vblank,
631 .disable_vblank = tegra_drm_disable_vblank,
633 #if defined(CONFIG_DEBUG_FS)
634 .debugfs_init = tegra_debugfs_init,
635 .debugfs_cleanup = tegra_debugfs_cleanup,
638 .gem_free_object = tegra_bo_free_object,
639 .gem_vm_ops = &tegra_bo_vm_ops,
640 .dumb_create = tegra_bo_dumb_create,
641 .dumb_map_offset = tegra_bo_dumb_map_offset,
642 .dumb_destroy = drm_gem_dumb_destroy,
644 .ioctls = tegra_drm_ioctls,
645 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
646 .fops = &tegra_drm_fops,
651 .major = DRIVER_MAJOR,
652 .minor = DRIVER_MINOR,
653 .patchlevel = DRIVER_PATCHLEVEL,