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 <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
14 #include <linux/dma-mapping.h>
15 #include <asm/dma-iommu.h>
20 #include "host1x_client.h"
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
33 struct host1x_drm_client {
34 struct host1x_client *client;
35 struct device_node *np;
36 struct list_head list;
39 static int host1x_add_drm_client(struct host1x_drm *host1x,
40 struct device_node *np)
42 struct host1x_drm_client *client;
44 client = kzalloc(sizeof(*client), GFP_KERNEL);
48 INIT_LIST_HEAD(&client->list);
49 client->np = of_node_get(np);
51 list_add_tail(&client->list, &host1x->drm_clients);
56 static int host1x_activate_drm_client(struct host1x_drm *host1x,
57 struct host1x_drm_client *drm,
58 struct host1x_client *client)
60 mutex_lock(&host1x->drm_clients_lock);
61 list_del_init(&drm->list);
62 list_add_tail(&drm->list, &host1x->drm_active);
64 mutex_unlock(&host1x->drm_clients_lock);
69 static int host1x_remove_drm_client(struct host1x_drm *host1x,
70 struct host1x_drm_client *client)
72 mutex_lock(&host1x->drm_clients_lock);
73 list_del_init(&client->list);
74 mutex_unlock(&host1x->drm_clients_lock);
76 of_node_put(client->np);
82 static int host1x_parse_dt(struct host1x_drm *host1x)
84 static const char * const compat[] = {
86 "nvidia,tegra20-hdmi",
87 "nvidia,tegra20-gr2d",
89 "nvidia,tegra30-hdmi",
90 "nvidia,tegra30-gr2d",
95 for (i = 0; i < ARRAY_SIZE(compat); i++) {
96 struct device_node *np;
98 for_each_child_of_node(host1x->dev->of_node, np) {
99 if (of_device_is_compatible(np, compat[i]) &&
100 of_device_is_available(np)) {
101 err = host1x_add_drm_client(host1x, np);
111 int host1x_drm_alloc(struct platform_device *pdev)
113 struct host1x_drm *host1x;
116 host1x = devm_kzalloc(&pdev->dev, sizeof(*host1x), GFP_KERNEL);
120 mutex_init(&host1x->drm_clients_lock);
121 INIT_LIST_HEAD(&host1x->drm_clients);
122 INIT_LIST_HEAD(&host1x->drm_active);
123 mutex_init(&host1x->clients_lock);
124 INIT_LIST_HEAD(&host1x->clients);
125 host1x->dev = &pdev->dev;
127 err = host1x_parse_dt(host1x);
129 dev_err(&pdev->dev, "failed to parse DT: %d\n", err);
133 host1x_set_drm_data(&pdev->dev, host1x);
138 int host1x_drm_init(struct host1x_drm *host1x, struct drm_device *drm)
140 struct host1x_client *client;
142 mutex_lock(&host1x->clients_lock);
144 list_for_each_entry(client, &host1x->clients, list) {
145 if (client->ops && client->ops->drm_init) {
146 int err = client->ops->drm_init(client, drm);
149 "DRM setup failed for %s: %d\n",
150 dev_name(client->dev), err);
151 mutex_unlock(&host1x->clients_lock);
157 mutex_unlock(&host1x->clients_lock);
162 int host1x_drm_exit(struct host1x_drm *host1x)
164 struct platform_device *pdev = to_platform_device(host1x->dev);
165 struct host1x_client *client;
170 mutex_lock(&host1x->clients_lock);
172 list_for_each_entry_reverse(client, &host1x->clients, list) {
173 if (client->ops && client->ops->drm_exit) {
174 int err = client->ops->drm_exit(client);
177 "DRM cleanup failed for %s: %d\n",
178 dev_name(client->dev), err);
179 mutex_unlock(&host1x->clients_lock);
185 mutex_unlock(&host1x->clients_lock);
187 drm_platform_exit(&tegra_drm_driver, pdev);
193 int host1x_register_client(struct host1x_drm *host1x,
194 struct host1x_client *client)
196 struct host1x_drm_client *drm, *tmp;
199 mutex_lock(&host1x->clients_lock);
200 list_add_tail(&client->list, &host1x->clients);
201 mutex_unlock(&host1x->clients_lock);
203 list_for_each_entry_safe(drm, tmp, &host1x->drm_clients, list)
204 if (drm->np == client->dev->of_node)
205 host1x_activate_drm_client(host1x, drm, client);
207 if (list_empty(&host1x->drm_clients)) {
208 struct platform_device *pdev = to_platform_device(host1x->dev);
210 err = drm_platform_init(&tegra_drm_driver, pdev);
212 dev_err(host1x->dev, "drm_platform_init(): %d\n", err);
220 int host1x_unregister_client(struct host1x_drm *host1x,
221 struct host1x_client *client)
223 struct host1x_drm_client *drm, *tmp;
226 list_for_each_entry_safe(drm, tmp, &host1x->drm_active, list) {
227 if (drm->client == client) {
228 err = host1x_drm_exit(host1x);
230 dev_err(host1x->dev, "host1x_drm_exit(): %d\n",
235 host1x_remove_drm_client(host1x, drm);
240 mutex_lock(&host1x->clients_lock);
241 list_del_init(&client->list);
242 mutex_unlock(&host1x->clients_lock);
247 static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
249 struct host1x_drm *host1x;
252 host1x = host1x_get_drm_data(drm->dev);
253 drm->dev_private = host1x;
256 drm_mode_config_init(drm);
258 err = host1x_drm_init(host1x, drm);
263 * We don't use the drm_irq_install() helpers provided by the DRM
264 * core, so we need to set this manually in order to allow the
265 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
267 drm->irq_enabled = 1;
269 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
273 err = tegra_drm_fb_init(drm);
277 drm_kms_helper_poll_init(drm);
282 static int tegra_drm_unload(struct drm_device *drm)
284 drm_kms_helper_poll_fini(drm);
285 tegra_drm_fb_exit(drm);
287 drm_mode_config_cleanup(drm);
292 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
294 struct host1x_drm_file *fpriv;
296 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
300 INIT_LIST_HEAD(&fpriv->contexts);
301 filp->driver_priv = fpriv;
306 static void host1x_drm_context_free(struct host1x_drm_context *context)
308 context->client->ops->close_channel(context);
312 static void tegra_drm_lastclose(struct drm_device *drm)
314 struct host1x_drm *host1x = drm->dev_private;
316 tegra_fbdev_restore_mode(host1x->fbdev);
319 #ifdef CONFIG_DRM_TEGRA_STAGING
320 static bool host1x_drm_file_owns_context(struct host1x_drm_file *file,
321 struct host1x_drm_context *context)
323 struct host1x_drm_context *ctx;
325 list_for_each_entry(ctx, &file->contexts, list)
332 static int tegra_gem_create(struct drm_device *drm, void *data,
333 struct drm_file *file)
335 struct drm_tegra_gem_create *args = data;
338 bo = tegra_bo_create_with_handle(file, drm, args->size,
346 static int tegra_gem_mmap(struct drm_device *drm, void *data,
347 struct drm_file *file)
349 struct drm_tegra_gem_mmap *args = data;
350 struct drm_gem_object *gem;
353 gem = drm_gem_object_lookup(drm, file, args->handle);
357 bo = to_tegra_bo(gem);
359 args->offset = tegra_bo_get_mmap_offset(bo);
361 drm_gem_object_unreference(gem);
366 static int tegra_syncpt_read(struct drm_device *drm, void *data,
367 struct drm_file *file)
369 struct drm_tegra_syncpt_read *args = data;
370 struct host1x *host = dev_get_drvdata(drm->dev);
371 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
376 args->value = host1x_syncpt_read_min(sp);
380 static int tegra_syncpt_incr(struct drm_device *drm, void *data,
381 struct drm_file *file)
383 struct drm_tegra_syncpt_incr *args = data;
384 struct host1x *host = dev_get_drvdata(drm->dev);
385 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
390 return host1x_syncpt_incr(sp);
393 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
394 struct drm_file *file)
396 struct drm_tegra_syncpt_wait *args = data;
397 struct host1x *host = dev_get_drvdata(drm->dev);
398 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
403 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
407 static int tegra_open_channel(struct drm_device *drm, void *data,
408 struct drm_file *file)
410 struct drm_tegra_open_channel *args = data;
411 struct host1x_client *client;
412 struct host1x_drm_context *context;
413 struct host1x_drm_file *fpriv = file->driver_priv;
414 struct host1x_drm *host1x = drm->dev_private;
417 context = kzalloc(sizeof(*context), GFP_KERNEL);
421 list_for_each_entry(client, &host1x->clients, list)
422 if (client->class == args->client) {
423 err = client->ops->open_channel(client, context);
427 context->client = client;
428 list_add(&context->list, &fpriv->contexts);
429 args->context = (uintptr_t)context;
437 static int tegra_close_channel(struct drm_device *drm, void *data,
438 struct drm_file *file)
440 struct drm_tegra_close_channel *args = data;
441 struct host1x_drm_file *fpriv = file->driver_priv;
442 struct host1x_drm_context *context =
443 (struct host1x_drm_context *)(uintptr_t)args->context;
445 if (!host1x_drm_file_owns_context(fpriv, context))
448 list_del(&context->list);
449 host1x_drm_context_free(context);
454 static int tegra_get_syncpt(struct drm_device *drm, void *data,
455 struct drm_file *file)
457 struct drm_tegra_get_syncpt *args = data;
458 struct host1x_drm_file *fpriv = file->driver_priv;
459 struct host1x_drm_context *context =
460 (struct host1x_drm_context *)(uintptr_t)args->context;
461 struct host1x_syncpt *syncpt;
463 if (!host1x_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 drm_tegra_submit *args = data;
479 struct host1x_drm_file *fpriv = file->driver_priv;
480 struct host1x_drm_context *context =
481 (struct host1x_drm_context *)(uintptr_t)args->context;
483 if (!host1x_drm_file_owns_context(fpriv, context))
486 return context->client->ops->submit(context, args, drm, file);
490 static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
491 #ifdef CONFIG_DRM_TEGRA_STAGING
492 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
493 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
494 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
495 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
496 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
497 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
498 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
499 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
500 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
504 static const struct file_operations tegra_drm_fops = {
505 .owner = THIS_MODULE,
507 .release = drm_release,
508 .unlocked_ioctl = drm_ioctl,
509 .mmap = tegra_drm_mmap,
513 .compat_ioctl = drm_compat_ioctl,
515 .llseek = noop_llseek,
518 static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
520 struct drm_crtc *crtc;
522 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
523 struct tegra_dc *dc = to_tegra_dc(crtc);
525 if (dc->pipe == pipe)
532 static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
534 /* TODO: implement real hardware counter using syncpoints */
535 return drm_vblank_count(dev, crtc);
538 static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
540 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
541 struct tegra_dc *dc = to_tegra_dc(crtc);
546 tegra_dc_enable_vblank(dc);
551 static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
553 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
554 struct tegra_dc *dc = to_tegra_dc(crtc);
557 tegra_dc_disable_vblank(dc);
560 static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
562 struct host1x_drm_file *fpriv = file->driver_priv;
563 struct host1x_drm_context *context, *tmp;
564 struct drm_crtc *crtc;
566 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
567 tegra_dc_cancel_page_flip(crtc, file);
569 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
570 host1x_drm_context_free(context);
575 #ifdef CONFIG_DEBUG_FS
576 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
578 struct drm_info_node *node = (struct drm_info_node *)s->private;
579 struct drm_device *drm = node->minor->dev;
580 struct drm_framebuffer *fb;
582 mutex_lock(&drm->mode_config.fb_lock);
584 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
585 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
586 fb->base.id, fb->width, fb->height, fb->depth,
588 atomic_read(&fb->refcount.refcount));
591 mutex_unlock(&drm->mode_config.fb_lock);
596 static struct drm_info_list tegra_debugfs_list[] = {
597 { "framebuffers", tegra_debugfs_framebuffers, 0 },
600 static int tegra_debugfs_init(struct drm_minor *minor)
602 return drm_debugfs_create_files(tegra_debugfs_list,
603 ARRAY_SIZE(tegra_debugfs_list),
604 minor->debugfs_root, minor);
607 static void tegra_debugfs_cleanup(struct drm_minor *minor)
609 drm_debugfs_remove_files(tegra_debugfs_list,
610 ARRAY_SIZE(tegra_debugfs_list), minor);
614 struct drm_driver tegra_drm_driver = {
615 .driver_features = DRIVER_MODESET | DRIVER_GEM,
616 .load = tegra_drm_load,
617 .unload = tegra_drm_unload,
618 .open = tegra_drm_open,
619 .preclose = tegra_drm_preclose,
620 .lastclose = tegra_drm_lastclose,
622 .get_vblank_counter = tegra_drm_get_vblank_counter,
623 .enable_vblank = tegra_drm_enable_vblank,
624 .disable_vblank = tegra_drm_disable_vblank,
626 #if defined(CONFIG_DEBUG_FS)
627 .debugfs_init = tegra_debugfs_init,
628 .debugfs_cleanup = tegra_debugfs_cleanup,
631 .gem_free_object = tegra_bo_free_object,
632 .gem_vm_ops = &tegra_bo_vm_ops,
633 .dumb_create = tegra_bo_dumb_create,
634 .dumb_map_offset = tegra_bo_dumb_map_offset,
635 .dumb_destroy = drm_gem_dumb_destroy,
637 .ioctls = tegra_drm_ioctls,
638 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
639 .fops = &tegra_drm_fops,
644 .major = DRIVER_MAJOR,
645 .minor = DRIVER_MINOR,
646 .patchlevel = DRIVER_PATCHLEVEL,