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