Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Legacy: Generic DRM Contexts
0003  *
0004  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
0005  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
0006  * All Rights Reserved.
0007  *
0008  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
0009  * Author: Gareth Hughes <gareth@valinux.com>
0010  *
0011  * Permission is hereby granted, free of charge, to any person obtaining a
0012  * copy of this software and associated documentation files (the "Software"),
0013  * to deal in the Software without restriction, including without limitation
0014  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0015  * and/or sell copies of the Software, and to permit persons to whom the
0016  * Software is furnished to do so, subject to the following conditions:
0017  *
0018  * The above copyright notice and this permission notice (including the next
0019  * paragraph) shall be included in all copies or substantial portions of the
0020  * Software.
0021  *
0022  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0023  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0024  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0025  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
0026  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0027  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0028  * OTHER DEALINGS IN THE SOFTWARE.
0029  */
0030 
0031 #include <linux/slab.h>
0032 #include <linux/uaccess.h>
0033 
0034 #include <drm/drm_drv.h>
0035 #include <drm/drm_file.h>
0036 #include <drm/drm_print.h>
0037 
0038 #include "drm_legacy.h"
0039 
0040 struct drm_ctx_list {
0041     struct list_head head;
0042     drm_context_t handle;
0043     struct drm_file *tag;
0044 };
0045 
0046 /******************************************************************/
0047 /** \name Context bitmap support */
0048 /*@{*/
0049 
0050 /*
0051  * Free a handle from the context bitmap.
0052  *
0053  * \param dev DRM device.
0054  * \param ctx_handle context handle.
0055  *
0056  * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
0057  * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
0058  * lock.
0059  */
0060 void drm_legacy_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
0061 {
0062     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0063         !drm_core_check_feature(dev, DRIVER_LEGACY))
0064         return;
0065 
0066     mutex_lock(&dev->struct_mutex);
0067     idr_remove(&dev->ctx_idr, ctx_handle);
0068     mutex_unlock(&dev->struct_mutex);
0069 }
0070 
0071 /*
0072  * Context bitmap allocation.
0073  *
0074  * \param dev DRM device.
0075  * \return (non-negative) context handle on success or a negative number on failure.
0076  *
0077  * Allocate a new idr from drm_device::ctx_idr while holding the
0078  * drm_device::struct_mutex lock.
0079  */
0080 static int drm_legacy_ctxbitmap_next(struct drm_device * dev)
0081 {
0082     int ret;
0083 
0084     mutex_lock(&dev->struct_mutex);
0085     ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
0086             GFP_KERNEL);
0087     mutex_unlock(&dev->struct_mutex);
0088     return ret;
0089 }
0090 
0091 /*
0092  * Context bitmap initialization.
0093  *
0094  * \param dev DRM device.
0095  *
0096  * Initialise the drm_device::ctx_idr
0097  */
0098 void drm_legacy_ctxbitmap_init(struct drm_device * dev)
0099 {
0100     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0101         !drm_core_check_feature(dev, DRIVER_LEGACY))
0102         return;
0103 
0104     idr_init(&dev->ctx_idr);
0105 }
0106 
0107 /*
0108  * Context bitmap cleanup.
0109  *
0110  * \param dev DRM device.
0111  *
0112  * Free all idr members using drm_ctx_sarea_free helper function
0113  * while holding the drm_device::struct_mutex lock.
0114  */
0115 void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
0116 {
0117     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0118         !drm_core_check_feature(dev, DRIVER_LEGACY))
0119         return;
0120 
0121     mutex_lock(&dev->struct_mutex);
0122     idr_destroy(&dev->ctx_idr);
0123     mutex_unlock(&dev->struct_mutex);
0124 }
0125 
0126 /**
0127  * drm_legacy_ctxbitmap_flush() - Flush all contexts owned by a file
0128  * @dev: DRM device to operate on
0129  * @file: Open file to flush contexts for
0130  *
0131  * This iterates over all contexts on @dev and drops them if they're owned by
0132  * @file. Note that after this call returns, new contexts might be added if
0133  * the file is still alive.
0134  */
0135 void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
0136 {
0137     struct drm_ctx_list *pos, *tmp;
0138 
0139     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0140         !drm_core_check_feature(dev, DRIVER_LEGACY))
0141         return;
0142 
0143     mutex_lock(&dev->ctxlist_mutex);
0144 
0145     list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) {
0146         if (pos->tag == file &&
0147             pos->handle != DRM_KERNEL_CONTEXT) {
0148             if (dev->driver->context_dtor)
0149                 dev->driver->context_dtor(dev, pos->handle);
0150 
0151             drm_legacy_ctxbitmap_free(dev, pos->handle);
0152             list_del(&pos->head);
0153             kfree(pos);
0154         }
0155     }
0156 
0157     mutex_unlock(&dev->ctxlist_mutex);
0158 }
0159 
0160 /*@}*/
0161 
0162 /******************************************************************/
0163 /** \name Per Context SAREA Support */
0164 /*@{*/
0165 
0166 /*
0167  * Get per-context SAREA.
0168  *
0169  * \param inode device inode.
0170  * \param file_priv DRM file private.
0171  * \param cmd command.
0172  * \param arg user argument pointing to a drm_ctx_priv_map structure.
0173  * \return zero on success or a negative number on failure.
0174  *
0175  * Gets the map from drm_device::ctx_idr with the handle specified and
0176  * returns its handle.
0177  */
0178 int drm_legacy_getsareactx(struct drm_device *dev, void *data,
0179                struct drm_file *file_priv)
0180 {
0181     struct drm_ctx_priv_map *request = data;
0182     struct drm_local_map *map;
0183     struct drm_map_list *_entry;
0184 
0185     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0186         !drm_core_check_feature(dev, DRIVER_LEGACY))
0187         return -EOPNOTSUPP;
0188 
0189     mutex_lock(&dev->struct_mutex);
0190 
0191     map = idr_find(&dev->ctx_idr, request->ctx_id);
0192     if (!map) {
0193         mutex_unlock(&dev->struct_mutex);
0194         return -EINVAL;
0195     }
0196 
0197     request->handle = NULL;
0198     list_for_each_entry(_entry, &dev->maplist, head) {
0199         if (_entry->map == map) {
0200             request->handle =
0201                 (void *)(unsigned long)_entry->user_token;
0202             break;
0203         }
0204     }
0205 
0206     mutex_unlock(&dev->struct_mutex);
0207 
0208     if (request->handle == NULL)
0209         return -EINVAL;
0210 
0211     return 0;
0212 }
0213 
0214 /*
0215  * Set per-context SAREA.
0216  *
0217  * \param inode device inode.
0218  * \param file_priv DRM file private.
0219  * \param cmd command.
0220  * \param arg user argument pointing to a drm_ctx_priv_map structure.
0221  * \return zero on success or a negative number on failure.
0222  *
0223  * Searches the mapping specified in \p arg and update the entry in
0224  * drm_device::ctx_idr with it.
0225  */
0226 int drm_legacy_setsareactx(struct drm_device *dev, void *data,
0227                struct drm_file *file_priv)
0228 {
0229     struct drm_ctx_priv_map *request = data;
0230     struct drm_local_map *map = NULL;
0231     struct drm_map_list *r_list = NULL;
0232 
0233     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0234         !drm_core_check_feature(dev, DRIVER_LEGACY))
0235         return -EOPNOTSUPP;
0236 
0237     mutex_lock(&dev->struct_mutex);
0238     list_for_each_entry(r_list, &dev->maplist, head) {
0239         if (r_list->map
0240             && r_list->user_token == (unsigned long) request->handle)
0241             goto found;
0242     }
0243       bad:
0244     mutex_unlock(&dev->struct_mutex);
0245     return -EINVAL;
0246 
0247       found:
0248     map = r_list->map;
0249     if (!map)
0250         goto bad;
0251 
0252     if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
0253         goto bad;
0254 
0255     mutex_unlock(&dev->struct_mutex);
0256 
0257     return 0;
0258 }
0259 
0260 /*@}*/
0261 
0262 /******************************************************************/
0263 /** \name The actual DRM context handling routines */
0264 /*@{*/
0265 
0266 /*
0267  * Switch context.
0268  *
0269  * \param dev DRM device.
0270  * \param old old context handle.
0271  * \param new new context handle.
0272  * \return zero on success or a negative number on failure.
0273  *
0274  * Attempt to set drm_device::context_flag.
0275  */
0276 static int drm_context_switch(struct drm_device * dev, int old, int new)
0277 {
0278     if (test_and_set_bit(0, &dev->context_flag)) {
0279         DRM_ERROR("Reentering -- FIXME\n");
0280         return -EBUSY;
0281     }
0282 
0283     DRM_DEBUG("Context switch from %d to %d\n", old, new);
0284 
0285     if (new == dev->last_context) {
0286         clear_bit(0, &dev->context_flag);
0287         return 0;
0288     }
0289 
0290     return 0;
0291 }
0292 
0293 /*
0294  * Complete context switch.
0295  *
0296  * \param dev DRM device.
0297  * \param new new context handle.
0298  * \return zero on success or a negative number on failure.
0299  *
0300  * Updates drm_device::last_context and drm_device::last_switch. Verifies the
0301  * hardware lock is held, clears the drm_device::context_flag and wakes up
0302  * drm_device::context_wait.
0303  */
0304 static int drm_context_switch_complete(struct drm_device *dev,
0305                        struct drm_file *file_priv, int new)
0306 {
0307     dev->last_context = new;    /* PRE/POST: This is the _only_ writer. */
0308 
0309     if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
0310         DRM_ERROR("Lock isn't held after context switch\n");
0311     }
0312 
0313     /* If a context switch is ever initiated
0314        when the kernel holds the lock, release
0315        that lock here.
0316      */
0317     clear_bit(0, &dev->context_flag);
0318 
0319     return 0;
0320 }
0321 
0322 /*
0323  * Reserve contexts.
0324  *
0325  * \param inode device inode.
0326  * \param file_priv DRM file private.
0327  * \param cmd command.
0328  * \param arg user argument pointing to a drm_ctx_res structure.
0329  * \return zero on success or a negative number on failure.
0330  */
0331 int drm_legacy_resctx(struct drm_device *dev, void *data,
0332               struct drm_file *file_priv)
0333 {
0334     struct drm_ctx_res *res = data;
0335     struct drm_ctx ctx;
0336     int i;
0337 
0338     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0339         !drm_core_check_feature(dev, DRIVER_LEGACY))
0340         return -EOPNOTSUPP;
0341 
0342     if (res->count >= DRM_RESERVED_CONTEXTS) {
0343         memset(&ctx, 0, sizeof(ctx));
0344         for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
0345             ctx.handle = i;
0346             if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
0347                 return -EFAULT;
0348         }
0349     }
0350     res->count = DRM_RESERVED_CONTEXTS;
0351 
0352     return 0;
0353 }
0354 
0355 /*
0356  * Add context.
0357  *
0358  * \param inode device inode.
0359  * \param file_priv DRM file private.
0360  * \param cmd command.
0361  * \param arg user argument pointing to a drm_ctx structure.
0362  * \return zero on success or a negative number on failure.
0363  *
0364  * Get a new handle for the context and copy to userspace.
0365  */
0366 int drm_legacy_addctx(struct drm_device *dev, void *data,
0367               struct drm_file *file_priv)
0368 {
0369     struct drm_ctx_list *ctx_entry;
0370     struct drm_ctx *ctx = data;
0371     int tmp_handle;
0372 
0373     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0374         !drm_core_check_feature(dev, DRIVER_LEGACY))
0375         return -EOPNOTSUPP;
0376 
0377     tmp_handle = drm_legacy_ctxbitmap_next(dev);
0378     if (tmp_handle == DRM_KERNEL_CONTEXT) {
0379         /* Skip kernel's context and get a new one. */
0380         tmp_handle = drm_legacy_ctxbitmap_next(dev);
0381     }
0382     DRM_DEBUG("%d\n", tmp_handle);
0383     if (tmp_handle < 0) {
0384         DRM_DEBUG("Not enough free contexts.\n");
0385         /* Should this return -EBUSY instead? */
0386         return tmp_handle;
0387     }
0388 
0389     ctx->handle = tmp_handle;
0390 
0391     ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
0392     if (!ctx_entry) {
0393         DRM_DEBUG("out of memory\n");
0394         return -ENOMEM;
0395     }
0396 
0397     INIT_LIST_HEAD(&ctx_entry->head);
0398     ctx_entry->handle = ctx->handle;
0399     ctx_entry->tag = file_priv;
0400 
0401     mutex_lock(&dev->ctxlist_mutex);
0402     list_add(&ctx_entry->head, &dev->ctxlist);
0403     mutex_unlock(&dev->ctxlist_mutex);
0404 
0405     return 0;
0406 }
0407 
0408 /*
0409  * Get context.
0410  *
0411  * \param inode device inode.
0412  * \param file_priv DRM file private.
0413  * \param cmd command.
0414  * \param arg user argument pointing to a drm_ctx structure.
0415  * \return zero on success or a negative number on failure.
0416  */
0417 int drm_legacy_getctx(struct drm_device *dev, void *data,
0418               struct drm_file *file_priv)
0419 {
0420     struct drm_ctx *ctx = data;
0421 
0422     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0423         !drm_core_check_feature(dev, DRIVER_LEGACY))
0424         return -EOPNOTSUPP;
0425 
0426     /* This is 0, because we don't handle any context flags */
0427     ctx->flags = 0;
0428 
0429     return 0;
0430 }
0431 
0432 /*
0433  * Switch context.
0434  *
0435  * \param inode device inode.
0436  * \param file_priv DRM file private.
0437  * \param cmd command.
0438  * \param arg user argument pointing to a drm_ctx structure.
0439  * \return zero on success or a negative number on failure.
0440  *
0441  * Calls context_switch().
0442  */
0443 int drm_legacy_switchctx(struct drm_device *dev, void *data,
0444              struct drm_file *file_priv)
0445 {
0446     struct drm_ctx *ctx = data;
0447 
0448     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0449         !drm_core_check_feature(dev, DRIVER_LEGACY))
0450         return -EOPNOTSUPP;
0451 
0452     DRM_DEBUG("%d\n", ctx->handle);
0453     return drm_context_switch(dev, dev->last_context, ctx->handle);
0454 }
0455 
0456 /*
0457  * New context.
0458  *
0459  * \param inode device inode.
0460  * \param file_priv DRM file private.
0461  * \param cmd command.
0462  * \param arg user argument pointing to a drm_ctx structure.
0463  * \return zero on success or a negative number on failure.
0464  *
0465  * Calls context_switch_complete().
0466  */
0467 int drm_legacy_newctx(struct drm_device *dev, void *data,
0468               struct drm_file *file_priv)
0469 {
0470     struct drm_ctx *ctx = data;
0471 
0472     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0473         !drm_core_check_feature(dev, DRIVER_LEGACY))
0474         return -EOPNOTSUPP;
0475 
0476     DRM_DEBUG("%d\n", ctx->handle);
0477     drm_context_switch_complete(dev, file_priv, ctx->handle);
0478 
0479     return 0;
0480 }
0481 
0482 /*
0483  * Remove context.
0484  *
0485  * \param inode device inode.
0486  * \param file_priv DRM file private.
0487  * \param cmd command.
0488  * \param arg user argument pointing to a drm_ctx structure.
0489  * \return zero on success or a negative number on failure.
0490  *
0491  * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
0492  */
0493 int drm_legacy_rmctx(struct drm_device *dev, void *data,
0494              struct drm_file *file_priv)
0495 {
0496     struct drm_ctx *ctx = data;
0497 
0498     if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
0499         !drm_core_check_feature(dev, DRIVER_LEGACY))
0500         return -EOPNOTSUPP;
0501 
0502     DRM_DEBUG("%d\n", ctx->handle);
0503     if (ctx->handle != DRM_KERNEL_CONTEXT) {
0504         if (dev->driver->context_dtor)
0505             dev->driver->context_dtor(dev, ctx->handle);
0506         drm_legacy_ctxbitmap_free(dev, ctx->handle);
0507     }
0508 
0509     mutex_lock(&dev->ctxlist_mutex);
0510     if (!list_empty(&dev->ctxlist)) {
0511         struct drm_ctx_list *pos, *n;
0512 
0513         list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
0514             if (pos->handle == ctx->handle) {
0515                 list_del(&pos->head);
0516                 kfree(pos);
0517             }
0518         }
0519     }
0520     mutex_unlock(&dev->ctxlist_mutex);
0521 
0522     return 0;
0523 }
0524 
0525 /*@}*/