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 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 10 | #include <linux/host1x.h> |
| 11 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 12 | #include "drm.h" |
Arto Merilainen | de2ba66 | 2013-03-22 16:34:08 +0200 | [diff] [blame] | 13 | #include "gem.h" |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 14 | |
| 15 | #define DRIVER_NAME "tegra" |
| 16 | #define DRIVER_DESC "NVIDIA Tegra graphics" |
| 17 | #define DRIVER_DATE "20120330" |
| 18 | #define DRIVER_MAJOR 0 |
| 19 | #define DRIVER_MINOR 0 |
| 20 | #define DRIVER_PATCHLEVEL 0 |
| 21 | |
Thierry Reding | 08943e6 | 2013-09-26 16:08:18 +0200 | [diff] [blame] | 22 | struct tegra_drm_file { |
| 23 | struct list_head contexts; |
| 24 | }; |
| 25 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 26 | static int tegra_drm_load(struct drm_device *drm, unsigned long flags) |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 27 | { |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 28 | struct host1x_device *device = to_host1x_device(drm->dev); |
Thierry Reding | 386a2a7 | 2013-09-24 13:22:17 +0200 | [diff] [blame] | 29 | struct tegra_drm *tegra; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 30 | int err; |
| 31 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 32 | tegra = kzalloc(sizeof(*tegra), GFP_KERNEL); |
Thierry Reding | 386a2a7 | 2013-09-24 13:22:17 +0200 | [diff] [blame] | 33 | if (!tegra) |
Terje Bergstrom | 692e6d7 | 2013-03-22 16:34:07 +0200 | [diff] [blame] | 34 | return -ENOMEM; |
| 35 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 36 | dev_set_drvdata(drm->dev, tegra); |
Thierry Reding | 386a2a7 | 2013-09-24 13:22:17 +0200 | [diff] [blame] | 37 | mutex_init(&tegra->clients_lock); |
| 38 | INIT_LIST_HEAD(&tegra->clients); |
Thierry Reding | 386a2a7 | 2013-09-24 13:22:17 +0200 | [diff] [blame] | 39 | drm->dev_private = tegra; |
| 40 | tegra->drm = drm; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 41 | |
| 42 | drm_mode_config_init(drm); |
| 43 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 44 | err = host1x_device_init(device); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 45 | if (err < 0) |
| 46 | return err; |
| 47 | |
Thierry Reding | 603f0cc | 2013-04-22 21:22:14 +0200 | [diff] [blame] | 48 | /* |
| 49 | * We don't use the drm_irq_install() helpers provided by the DRM |
| 50 | * core, so we need to set this manually in order to allow the |
| 51 | * DRM_IOCTL_WAIT_VBLANK to operate correctly. |
| 52 | */ |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 53 | drm->irq_enabled = true; |
Thierry Reding | 603f0cc | 2013-04-22 21:22:14 +0200 | [diff] [blame] | 54 | |
Thierry Reding | 6e5ff99 | 2012-11-28 11:45:47 +0100 | [diff] [blame] | 55 | err = drm_vblank_init(drm, drm->mode_config.num_crtc); |
| 56 | if (err < 0) |
| 57 | return err; |
| 58 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 59 | err = tegra_drm_fb_init(drm); |
| 60 | if (err < 0) |
| 61 | return err; |
| 62 | |
| 63 | drm_kms_helper_poll_init(drm); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int tegra_drm_unload(struct drm_device *drm) |
| 69 | { |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 70 | struct host1x_device *device = to_host1x_device(drm->dev); |
| 71 | int err; |
| 72 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 73 | drm_kms_helper_poll_fini(drm); |
| 74 | tegra_drm_fb_exit(drm); |
Thierry Reding | f002abc | 2013-10-14 14:06:02 +0200 | [diff] [blame] | 75 | drm_vblank_cleanup(drm); |
| 76 | drm_mode_config_cleanup(drm); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 77 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 78 | err = host1x_device_exit(device); |
| 79 | if (err < 0) |
| 80 | return err; |
| 81 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp) |
| 86 | { |
Thierry Reding | 08943e6 | 2013-09-26 16:08:18 +0200 | [diff] [blame] | 87 | struct tegra_drm_file *fpriv; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 88 | |
| 89 | fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL); |
| 90 | if (!fpriv) |
| 91 | return -ENOMEM; |
| 92 | |
| 93 | INIT_LIST_HEAD(&fpriv->contexts); |
| 94 | filp->driver_priv = fpriv; |
| 95 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 99 | static void tegra_drm_context_free(struct tegra_drm_context *context) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 100 | { |
| 101 | context->client->ops->close_channel(context); |
| 102 | kfree(context); |
| 103 | } |
| 104 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 105 | static void tegra_drm_lastclose(struct drm_device *drm) |
| 106 | { |
Thierry Reding | 386a2a7 | 2013-09-24 13:22:17 +0200 | [diff] [blame] | 107 | struct tegra_drm *tegra = drm->dev_private; |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 108 | |
Thierry Reding | 386a2a7 | 2013-09-24 13:22:17 +0200 | [diff] [blame] | 109 | tegra_fbdev_restore_mode(tegra->fbdev); |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Thierry Reding | c40f0f1 | 2013-10-10 11:00:33 +0200 | [diff] [blame^] | 112 | static struct host1x_bo * |
| 113 | host1x_bo_lookup(struct drm_device *drm, struct drm_file *file, u32 handle) |
| 114 | { |
| 115 | struct drm_gem_object *gem; |
| 116 | struct tegra_bo *bo; |
| 117 | |
| 118 | gem = drm_gem_object_lookup(drm, file, handle); |
| 119 | if (!gem) |
| 120 | return NULL; |
| 121 | |
| 122 | mutex_lock(&drm->struct_mutex); |
| 123 | drm_gem_object_unreference(gem); |
| 124 | mutex_unlock(&drm->struct_mutex); |
| 125 | |
| 126 | bo = to_tegra_bo(gem); |
| 127 | return &bo->base; |
| 128 | } |
| 129 | |
| 130 | int tegra_drm_submit(struct tegra_drm_context *context, |
| 131 | struct drm_tegra_submit *args, struct drm_device *drm, |
| 132 | struct drm_file *file) |
| 133 | { |
| 134 | unsigned int num_cmdbufs = args->num_cmdbufs; |
| 135 | unsigned int num_relocs = args->num_relocs; |
| 136 | unsigned int num_waitchks = args->num_waitchks; |
| 137 | struct drm_tegra_cmdbuf __user *cmdbufs = |
| 138 | (void * __user)(uintptr_t)args->cmdbufs; |
| 139 | struct drm_tegra_reloc __user *relocs = |
| 140 | (void * __user)(uintptr_t)args->relocs; |
| 141 | struct drm_tegra_waitchk __user *waitchks = |
| 142 | (void * __user)(uintptr_t)args->waitchks; |
| 143 | struct drm_tegra_syncpt syncpt; |
| 144 | struct host1x_job *job; |
| 145 | int err; |
| 146 | |
| 147 | /* We don't yet support other than one syncpt_incr struct per submit */ |
| 148 | if (args->num_syncpts != 1) |
| 149 | return -EINVAL; |
| 150 | |
| 151 | job = host1x_job_alloc(context->channel, args->num_cmdbufs, |
| 152 | args->num_relocs, args->num_waitchks); |
| 153 | if (!job) |
| 154 | return -ENOMEM; |
| 155 | |
| 156 | job->num_relocs = args->num_relocs; |
| 157 | job->num_waitchk = args->num_waitchks; |
| 158 | job->client = (u32)args->context; |
| 159 | job->class = context->client->base.class; |
| 160 | job->serialize = true; |
| 161 | |
| 162 | while (num_cmdbufs) { |
| 163 | struct drm_tegra_cmdbuf cmdbuf; |
| 164 | struct host1x_bo *bo; |
| 165 | |
| 166 | err = copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf)); |
| 167 | if (err) |
| 168 | goto fail; |
| 169 | |
| 170 | bo = host1x_bo_lookup(drm, file, cmdbuf.handle); |
| 171 | if (!bo) { |
| 172 | err = -ENOENT; |
| 173 | goto fail; |
| 174 | } |
| 175 | |
| 176 | host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset); |
| 177 | num_cmdbufs--; |
| 178 | cmdbufs++; |
| 179 | } |
| 180 | |
| 181 | err = copy_from_user(job->relocarray, relocs, |
| 182 | sizeof(*relocs) * num_relocs); |
| 183 | if (err) |
| 184 | goto fail; |
| 185 | |
| 186 | while (num_relocs--) { |
| 187 | struct host1x_reloc *reloc = &job->relocarray[num_relocs]; |
| 188 | struct host1x_bo *cmdbuf, *target; |
| 189 | |
| 190 | cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf); |
| 191 | target = host1x_bo_lookup(drm, file, (u32)reloc->target); |
| 192 | |
| 193 | reloc->cmdbuf = cmdbuf; |
| 194 | reloc->target = target; |
| 195 | |
| 196 | if (!reloc->target || !reloc->cmdbuf) { |
| 197 | err = -ENOENT; |
| 198 | goto fail; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | err = copy_from_user(job->waitchk, waitchks, |
| 203 | sizeof(*waitchks) * num_waitchks); |
| 204 | if (err) |
| 205 | goto fail; |
| 206 | |
| 207 | err = copy_from_user(&syncpt, (void * __user)(uintptr_t)args->syncpts, |
| 208 | sizeof(syncpt)); |
| 209 | if (err) |
| 210 | goto fail; |
| 211 | |
| 212 | job->is_addr_reg = context->client->ops->is_addr_reg; |
| 213 | job->syncpt_incrs = syncpt.incrs; |
| 214 | job->syncpt_id = syncpt.id; |
| 215 | job->timeout = 10000; |
| 216 | |
| 217 | if (args->timeout && args->timeout < 10000) |
| 218 | job->timeout = args->timeout; |
| 219 | |
| 220 | err = host1x_job_pin(job, context->client->base.dev); |
| 221 | if (err) |
| 222 | goto fail; |
| 223 | |
| 224 | err = host1x_job_submit(job); |
| 225 | if (err) |
| 226 | goto fail_submit; |
| 227 | |
| 228 | args->fence = job->syncpt_end; |
| 229 | |
| 230 | host1x_job_put(job); |
| 231 | return 0; |
| 232 | |
| 233 | fail_submit: |
| 234 | host1x_job_unpin(job); |
| 235 | fail: |
| 236 | host1x_job_put(job); |
| 237 | return err; |
| 238 | } |
| 239 | |
| 240 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 241 | #ifdef CONFIG_DRM_TEGRA_STAGING |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 242 | static struct tegra_drm_context *tegra_drm_get_context(__u64 context) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 243 | { |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 244 | return (struct tegra_drm_context *)(uintptr_t)context; |
| 245 | } |
| 246 | |
| 247 | static bool tegra_drm_file_owns_context(struct tegra_drm_file *file, |
| 248 | struct tegra_drm_context *context) |
| 249 | { |
| 250 | struct tegra_drm_context *ctx; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 251 | |
| 252 | list_for_each_entry(ctx, &file->contexts, list) |
| 253 | if (ctx == context) |
| 254 | return true; |
| 255 | |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | static int tegra_gem_create(struct drm_device *drm, void *data, |
| 260 | struct drm_file *file) |
| 261 | { |
| 262 | struct drm_tegra_gem_create *args = data; |
| 263 | struct tegra_bo *bo; |
| 264 | |
| 265 | bo = tegra_bo_create_with_handle(file, drm, args->size, |
| 266 | &args->handle); |
| 267 | if (IS_ERR(bo)) |
| 268 | return PTR_ERR(bo); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int tegra_gem_mmap(struct drm_device *drm, void *data, |
| 274 | struct drm_file *file) |
| 275 | { |
| 276 | struct drm_tegra_gem_mmap *args = data; |
| 277 | struct drm_gem_object *gem; |
| 278 | struct tegra_bo *bo; |
| 279 | |
| 280 | gem = drm_gem_object_lookup(drm, file, args->handle); |
| 281 | if (!gem) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | bo = to_tegra_bo(gem); |
| 285 | |
David Herrmann | 2bc7b0c | 2013-08-13 14:19:58 +0200 | [diff] [blame] | 286 | args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 287 | |
| 288 | drm_gem_object_unreference(gem); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static int tegra_syncpt_read(struct drm_device *drm, void *data, |
| 294 | struct drm_file *file) |
| 295 | { |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 296 | struct host1x *host = dev_get_drvdata(drm->dev->parent); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 297 | struct drm_tegra_syncpt_read *args = data; |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 298 | struct host1x_syncpt *sp; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 299 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 300 | sp = host1x_syncpt_get(host, args->id); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 301 | if (!sp) |
| 302 | return -EINVAL; |
| 303 | |
| 304 | args->value = host1x_syncpt_read_min(sp); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int tegra_syncpt_incr(struct drm_device *drm, void *data, |
| 309 | struct drm_file *file) |
| 310 | { |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 311 | struct host1x *host1x = dev_get_drvdata(drm->dev->parent); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 312 | struct drm_tegra_syncpt_incr *args = data; |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 313 | struct host1x_syncpt *sp; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 314 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 315 | sp = host1x_syncpt_get(host1x, args->id); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 316 | if (!sp) |
| 317 | return -EINVAL; |
| 318 | |
Arto Merilainen | ebae30b | 2013-05-29 13:26:08 +0300 | [diff] [blame] | 319 | return host1x_syncpt_incr(sp); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static int tegra_syncpt_wait(struct drm_device *drm, void *data, |
| 323 | struct drm_file *file) |
| 324 | { |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 325 | struct host1x *host1x = dev_get_drvdata(drm->dev->parent); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 326 | struct drm_tegra_syncpt_wait *args = data; |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 327 | struct host1x_syncpt *sp; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 328 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 329 | sp = host1x_syncpt_get(host1x, args->id); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 330 | if (!sp) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | return host1x_syncpt_wait(sp, args->thresh, args->timeout, |
| 334 | &args->value); |
| 335 | } |
| 336 | |
| 337 | static int tegra_open_channel(struct drm_device *drm, void *data, |
| 338 | struct drm_file *file) |
| 339 | { |
Thierry Reding | 08943e6 | 2013-09-26 16:08:18 +0200 | [diff] [blame] | 340 | struct tegra_drm_file *fpriv = file->driver_priv; |
Thierry Reding | 386a2a7 | 2013-09-24 13:22:17 +0200 | [diff] [blame] | 341 | struct tegra_drm *tegra = drm->dev_private; |
| 342 | struct drm_tegra_open_channel *args = data; |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 343 | struct tegra_drm_context *context; |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 344 | struct tegra_drm_client *client; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 345 | int err = -ENODEV; |
| 346 | |
| 347 | context = kzalloc(sizeof(*context), GFP_KERNEL); |
| 348 | if (!context) |
| 349 | return -ENOMEM; |
| 350 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 351 | list_for_each_entry(client, &tegra->clients, list) |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 352 | if (client->base.class == args->client) { |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 353 | err = client->ops->open_channel(client, context); |
| 354 | if (err) |
| 355 | break; |
| 356 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 357 | list_add(&context->list, &fpriv->contexts); |
| 358 | args->context = (uintptr_t)context; |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 359 | context->client = client; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | kfree(context); |
| 364 | return err; |
| 365 | } |
| 366 | |
| 367 | static int tegra_close_channel(struct drm_device *drm, void *data, |
| 368 | struct drm_file *file) |
| 369 | { |
Thierry Reding | 08943e6 | 2013-09-26 16:08:18 +0200 | [diff] [blame] | 370 | struct tegra_drm_file *fpriv = file->driver_priv; |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 371 | struct drm_tegra_close_channel *args = data; |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 372 | struct tegra_drm_context *context; |
| 373 | |
| 374 | context = tegra_drm_get_context(args->context); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 375 | |
Thierry Reding | 08943e6 | 2013-09-26 16:08:18 +0200 | [diff] [blame] | 376 | if (!tegra_drm_file_owns_context(fpriv, context)) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 377 | return -EINVAL; |
| 378 | |
| 379 | list_del(&context->list); |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 380 | tegra_drm_context_free(context); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | static int tegra_get_syncpt(struct drm_device *drm, void *data, |
| 386 | struct drm_file *file) |
| 387 | { |
Thierry Reding | 08943e6 | 2013-09-26 16:08:18 +0200 | [diff] [blame] | 388 | struct tegra_drm_file *fpriv = file->driver_priv; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 389 | struct drm_tegra_get_syncpt *args = data; |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 390 | struct tegra_drm_context *context; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 391 | struct host1x_syncpt *syncpt; |
| 392 | |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 393 | context = tegra_drm_get_context(args->context); |
| 394 | |
Thierry Reding | 08943e6 | 2013-09-26 16:08:18 +0200 | [diff] [blame] | 395 | if (!tegra_drm_file_owns_context(fpriv, context)) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 396 | return -ENODEV; |
| 397 | |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 398 | if (args->index >= context->client->base.num_syncpts) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 399 | return -EINVAL; |
| 400 | |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 401 | syncpt = context->client->base.syncpts[args->index]; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 402 | args->id = host1x_syncpt_id(syncpt); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int tegra_submit(struct drm_device *drm, void *data, |
| 408 | struct drm_file *file) |
| 409 | { |
Thierry Reding | 08943e6 | 2013-09-26 16:08:18 +0200 | [diff] [blame] | 410 | struct tegra_drm_file *fpriv = file->driver_priv; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 411 | struct drm_tegra_submit *args = data; |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 412 | struct tegra_drm_context *context; |
| 413 | |
| 414 | context = tegra_drm_get_context(args->context); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 415 | |
Thierry Reding | 08943e6 | 2013-09-26 16:08:18 +0200 | [diff] [blame] | 416 | if (!tegra_drm_file_owns_context(fpriv, context)) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 417 | return -ENODEV; |
| 418 | |
| 419 | return context->client->ops->submit(context, args, drm, file); |
| 420 | } |
| 421 | #endif |
| 422 | |
Rob Clark | baa7094 | 2013-08-02 13:27:49 -0400 | [diff] [blame] | 423 | static const struct drm_ioctl_desc tegra_drm_ioctls[] = { |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 424 | #ifdef CONFIG_DRM_TEGRA_STAGING |
| 425 | DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH), |
| 426 | DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED), |
| 427 | DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED), |
| 428 | DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED), |
| 429 | DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED), |
| 430 | DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED), |
| 431 | DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED), |
| 432 | DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED), |
| 433 | DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED), |
| 434 | #endif |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 435 | }; |
| 436 | |
| 437 | static const struct file_operations tegra_drm_fops = { |
| 438 | .owner = THIS_MODULE, |
| 439 | .open = drm_open, |
| 440 | .release = drm_release, |
| 441 | .unlocked_ioctl = drm_ioctl, |
Arto Merilainen | de2ba66 | 2013-03-22 16:34:08 +0200 | [diff] [blame] | 442 | .mmap = tegra_drm_mmap, |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 443 | .poll = drm_poll, |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 444 | .read = drm_read, |
| 445 | #ifdef CONFIG_COMPAT |
| 446 | .compat_ioctl = drm_compat_ioctl, |
| 447 | #endif |
| 448 | .llseek = noop_llseek, |
| 449 | }; |
| 450 | |
Thierry Reding | 6e5ff99 | 2012-11-28 11:45:47 +0100 | [diff] [blame] | 451 | static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe) |
| 452 | { |
| 453 | struct drm_crtc *crtc; |
| 454 | |
| 455 | list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) { |
| 456 | struct tegra_dc *dc = to_tegra_dc(crtc); |
| 457 | |
| 458 | if (dc->pipe == pipe) |
| 459 | return crtc; |
| 460 | } |
| 461 | |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc) |
| 466 | { |
| 467 | /* TODO: implement real hardware counter using syncpoints */ |
| 468 | return drm_vblank_count(dev, crtc); |
| 469 | } |
| 470 | |
| 471 | static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe) |
| 472 | { |
| 473 | struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe); |
| 474 | struct tegra_dc *dc = to_tegra_dc(crtc); |
| 475 | |
| 476 | if (!crtc) |
| 477 | return -ENODEV; |
| 478 | |
| 479 | tegra_dc_enable_vblank(dc); |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe) |
| 485 | { |
| 486 | struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe); |
| 487 | struct tegra_dc *dc = to_tegra_dc(crtc); |
| 488 | |
| 489 | if (crtc) |
| 490 | tegra_dc_disable_vblank(dc); |
| 491 | } |
| 492 | |
Thierry Reding | 3c03c46 | 2012-11-28 12:00:18 +0100 | [diff] [blame] | 493 | static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file) |
| 494 | { |
Thierry Reding | 08943e6 | 2013-09-26 16:08:18 +0200 | [diff] [blame] | 495 | struct tegra_drm_file *fpriv = file->driver_priv; |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 496 | struct tegra_drm_context *context, *tmp; |
Thierry Reding | 3c03c46 | 2012-11-28 12:00:18 +0100 | [diff] [blame] | 497 | struct drm_crtc *crtc; |
| 498 | |
| 499 | list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) |
| 500 | tegra_dc_cancel_page_flip(crtc, file); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 501 | |
| 502 | list_for_each_entry_safe(context, tmp, &fpriv->contexts, list) |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 503 | tegra_drm_context_free(context); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 504 | |
| 505 | kfree(fpriv); |
Thierry Reding | 3c03c46 | 2012-11-28 12:00:18 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Thierry Reding | e450fcc | 2013-02-13 16:13:16 +0100 | [diff] [blame] | 508 | #ifdef CONFIG_DEBUG_FS |
| 509 | static int tegra_debugfs_framebuffers(struct seq_file *s, void *data) |
| 510 | { |
| 511 | struct drm_info_node *node = (struct drm_info_node *)s->private; |
| 512 | struct drm_device *drm = node->minor->dev; |
| 513 | struct drm_framebuffer *fb; |
| 514 | |
| 515 | mutex_lock(&drm->mode_config.fb_lock); |
| 516 | |
| 517 | list_for_each_entry(fb, &drm->mode_config.fb_list, head) { |
| 518 | seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n", |
| 519 | fb->base.id, fb->width, fb->height, fb->depth, |
| 520 | fb->bits_per_pixel, |
| 521 | atomic_read(&fb->refcount.refcount)); |
| 522 | } |
| 523 | |
| 524 | mutex_unlock(&drm->mode_config.fb_lock); |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static struct drm_info_list tegra_debugfs_list[] = { |
| 530 | { "framebuffers", tegra_debugfs_framebuffers, 0 }, |
| 531 | }; |
| 532 | |
| 533 | static int tegra_debugfs_init(struct drm_minor *minor) |
| 534 | { |
| 535 | return drm_debugfs_create_files(tegra_debugfs_list, |
| 536 | ARRAY_SIZE(tegra_debugfs_list), |
| 537 | minor->debugfs_root, minor); |
| 538 | } |
| 539 | |
| 540 | static void tegra_debugfs_cleanup(struct drm_minor *minor) |
| 541 | { |
| 542 | drm_debugfs_remove_files(tegra_debugfs_list, |
| 543 | ARRAY_SIZE(tegra_debugfs_list), minor); |
| 544 | } |
| 545 | #endif |
| 546 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 547 | struct drm_driver tegra_drm_driver = { |
Laurent Pinchart | 604faa7 | 2013-05-29 07:44:34 +0200 | [diff] [blame] | 548 | .driver_features = DRIVER_MODESET | DRIVER_GEM, |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 549 | .load = tegra_drm_load, |
| 550 | .unload = tegra_drm_unload, |
| 551 | .open = tegra_drm_open, |
Thierry Reding | 3c03c46 | 2012-11-28 12:00:18 +0100 | [diff] [blame] | 552 | .preclose = tegra_drm_preclose, |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 553 | .lastclose = tegra_drm_lastclose, |
| 554 | |
Thierry Reding | 6e5ff99 | 2012-11-28 11:45:47 +0100 | [diff] [blame] | 555 | .get_vblank_counter = tegra_drm_get_vblank_counter, |
| 556 | .enable_vblank = tegra_drm_enable_vblank, |
| 557 | .disable_vblank = tegra_drm_disable_vblank, |
| 558 | |
Thierry Reding | e450fcc | 2013-02-13 16:13:16 +0100 | [diff] [blame] | 559 | #if defined(CONFIG_DEBUG_FS) |
| 560 | .debugfs_init = tegra_debugfs_init, |
| 561 | .debugfs_cleanup = tegra_debugfs_cleanup, |
| 562 | #endif |
| 563 | |
Arto Merilainen | de2ba66 | 2013-03-22 16:34:08 +0200 | [diff] [blame] | 564 | .gem_free_object = tegra_bo_free_object, |
| 565 | .gem_vm_ops = &tegra_bo_vm_ops, |
| 566 | .dumb_create = tegra_bo_dumb_create, |
| 567 | .dumb_map_offset = tegra_bo_dumb_map_offset, |
Daniel Vetter | 43387b3 | 2013-07-16 09:12:04 +0200 | [diff] [blame] | 568 | .dumb_destroy = drm_gem_dumb_destroy, |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 569 | |
| 570 | .ioctls = tegra_drm_ioctls, |
| 571 | .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls), |
| 572 | .fops = &tegra_drm_fops, |
| 573 | |
| 574 | .name = DRIVER_NAME, |
| 575 | .desc = DRIVER_DESC, |
| 576 | .date = DRIVER_DATE, |
| 577 | .major = DRIVER_MAJOR, |
| 578 | .minor = DRIVER_MINOR, |
| 579 | .patchlevel = DRIVER_PATCHLEVEL, |
| 580 | }; |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 581 | |
| 582 | int tegra_drm_register_client(struct tegra_drm *tegra, |
| 583 | struct tegra_drm_client *client) |
| 584 | { |
| 585 | mutex_lock(&tegra->clients_lock); |
| 586 | list_add_tail(&client->list, &tegra->clients); |
| 587 | mutex_unlock(&tegra->clients_lock); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | int tegra_drm_unregister_client(struct tegra_drm *tegra, |
| 593 | struct tegra_drm_client *client) |
| 594 | { |
| 595 | mutex_lock(&tegra->clients_lock); |
| 596 | list_del_init(&client->list); |
| 597 | mutex_unlock(&tegra->clients_lock); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static int host1x_drm_probe(struct host1x_device *device) |
| 603 | { |
| 604 | return drm_host1x_init(&tegra_drm_driver, device); |
| 605 | } |
| 606 | |
| 607 | static int host1x_drm_remove(struct host1x_device *device) |
| 608 | { |
| 609 | drm_host1x_exit(&tegra_drm_driver, device); |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | static const struct of_device_id host1x_drm_subdevs[] = { |
| 615 | { .compatible = "nvidia,tegra20-dc", }, |
| 616 | { .compatible = "nvidia,tegra20-hdmi", }, |
| 617 | { .compatible = "nvidia,tegra20-gr2d", }, |
| 618 | { .compatible = "nvidia,tegra30-dc", }, |
| 619 | { .compatible = "nvidia,tegra30-hdmi", }, |
| 620 | { .compatible = "nvidia,tegra30-gr2d", }, |
Mikko Perttunen | 7d1d28a | 2013-09-30 16:54:47 +0200 | [diff] [blame] | 621 | { .compatible = "nvidia,tegra114-hdmi", }, |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 622 | { /* sentinel */ } |
| 623 | }; |
| 624 | |
| 625 | static struct host1x_driver host1x_drm_driver = { |
| 626 | .name = "drm", |
| 627 | .probe = host1x_drm_probe, |
| 628 | .remove = host1x_drm_remove, |
| 629 | .subdevs = host1x_drm_subdevs, |
| 630 | }; |
| 631 | |
| 632 | static int __init host1x_drm_init(void) |
| 633 | { |
| 634 | int err; |
| 635 | |
| 636 | err = host1x_driver_register(&host1x_drm_driver); |
| 637 | if (err < 0) |
| 638 | return err; |
| 639 | |
| 640 | err = platform_driver_register(&tegra_dc_driver); |
| 641 | if (err < 0) |
| 642 | goto unregister_host1x; |
| 643 | |
| 644 | err = platform_driver_register(&tegra_hdmi_driver); |
| 645 | if (err < 0) |
| 646 | goto unregister_dc; |
| 647 | |
| 648 | err = platform_driver_register(&tegra_gr2d_driver); |
| 649 | if (err < 0) |
| 650 | goto unregister_hdmi; |
| 651 | |
| 652 | return 0; |
| 653 | |
| 654 | unregister_hdmi: |
| 655 | platform_driver_unregister(&tegra_hdmi_driver); |
| 656 | unregister_dc: |
| 657 | platform_driver_unregister(&tegra_dc_driver); |
| 658 | unregister_host1x: |
| 659 | host1x_driver_unregister(&host1x_drm_driver); |
| 660 | return err; |
| 661 | } |
| 662 | module_init(host1x_drm_init); |
| 663 | |
| 664 | static void __exit host1x_drm_exit(void) |
| 665 | { |
| 666 | platform_driver_unregister(&tegra_gr2d_driver); |
| 667 | platform_driver_unregister(&tegra_hdmi_driver); |
| 668 | platform_driver_unregister(&tegra_dc_driver); |
| 669 | host1x_driver_unregister(&host1x_drm_driver); |
| 670 | } |
| 671 | module_exit(host1x_drm_exit); |
| 672 | |
| 673 | MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); |
| 674 | MODULE_DESCRIPTION("NVIDIA Tegra DRM driver"); |
| 675 | MODULE_LICENSE("GPL v2"); |