/**1* \file drm_context.c2* IOCTLs for generic contexts3*4* \author Rickard E. (Rik) Faith <[email protected]>5* \author Gareth Hughes <[email protected]>6*/78/*9* Created: Fri Nov 24 18:31:37 2000 by [email protected]10*11* Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.12* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.13* All Rights Reserved.14*15* Permission is hereby granted, free of charge, to any person obtaining a16* copy of this software and associated documentation files (the "Software"),17* to deal in the Software without restriction, including without limitation18* the rights to use, copy, modify, merge, publish, distribute, sublicense,19* and/or sell copies of the Software, and to permit persons to whom the20* Software is furnished to do so, subject to the following conditions:21*22* The above copyright notice and this permission notice (including the next23* paragraph) shall be included in all copies or substantial portions of the24* Software.25*26* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR27* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,28* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL29* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR30* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,31* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR32* OTHER DEALINGS IN THE SOFTWARE.33*/3435/*36* ChangeLog:37* 2001-11-16 Torsten Duwe <[email protected]>38* added context constructor/destructor hooks,39* needed by SiS driver's memory management.40*/4142#include "drmP.h"4344/******************************************************************/45/** \name Context bitmap support */46/*@{*/4748/**49* Free a handle from the context bitmap.50*51* \param dev DRM device.52* \param ctx_handle context handle.53*54* Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry55* in drm_device::ctx_idr, while holding the drm_device::struct_mutex56* lock.57*/58void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)59{60mutex_lock(&dev->struct_mutex);61idr_remove(&dev->ctx_idr, ctx_handle);62mutex_unlock(&dev->struct_mutex);63}6465/**66* Context bitmap allocation.67*68* \param dev DRM device.69* \return (non-negative) context handle on success or a negative number on failure.70*71* Allocate a new idr from drm_device::ctx_idr while holding the72* drm_device::struct_mutex lock.73*/74static int drm_ctxbitmap_next(struct drm_device * dev)75{76int new_id;77int ret;7879again:80if (idr_pre_get(&dev->ctx_idr, GFP_KERNEL) == 0) {81DRM_ERROR("Out of memory expanding drawable idr\n");82return -ENOMEM;83}84mutex_lock(&dev->struct_mutex);85ret = idr_get_new_above(&dev->ctx_idr, NULL,86DRM_RESERVED_CONTEXTS, &new_id);87if (ret == -EAGAIN) {88mutex_unlock(&dev->struct_mutex);89goto again;90}91mutex_unlock(&dev->struct_mutex);92return new_id;93}9495/**96* Context bitmap initialization.97*98* \param dev DRM device.99*100* Initialise the drm_device::ctx_idr101*/102int drm_ctxbitmap_init(struct drm_device * dev)103{104idr_init(&dev->ctx_idr);105return 0;106}107108/**109* Context bitmap cleanup.110*111* \param dev DRM device.112*113* Free all idr members using drm_ctx_sarea_free helper function114* while holding the drm_device::struct_mutex lock.115*/116void drm_ctxbitmap_cleanup(struct drm_device * dev)117{118mutex_lock(&dev->struct_mutex);119idr_remove_all(&dev->ctx_idr);120mutex_unlock(&dev->struct_mutex);121}122123/*@}*/124125/******************************************************************/126/** \name Per Context SAREA Support */127/*@{*/128129/**130* Get per-context SAREA.131*132* \param inode device inode.133* \param file_priv DRM file private.134* \param cmd command.135* \param arg user argument pointing to a drm_ctx_priv_map structure.136* \return zero on success or a negative number on failure.137*138* Gets the map from drm_device::ctx_idr with the handle specified and139* returns its handle.140*/141int drm_getsareactx(struct drm_device *dev, void *data,142struct drm_file *file_priv)143{144struct drm_ctx_priv_map *request = data;145struct drm_local_map *map;146struct drm_map_list *_entry;147148mutex_lock(&dev->struct_mutex);149150map = idr_find(&dev->ctx_idr, request->ctx_id);151if (!map) {152mutex_unlock(&dev->struct_mutex);153return -EINVAL;154}155156mutex_unlock(&dev->struct_mutex);157158request->handle = NULL;159list_for_each_entry(_entry, &dev->maplist, head) {160if (_entry->map == map) {161request->handle =162(void *)(unsigned long)_entry->user_token;163break;164}165}166if (request->handle == NULL)167return -EINVAL;168169return 0;170}171172/**173* Set per-context SAREA.174*175* \param inode device inode.176* \param file_priv DRM file private.177* \param cmd command.178* \param arg user argument pointing to a drm_ctx_priv_map structure.179* \return zero on success or a negative number on failure.180*181* Searches the mapping specified in \p arg and update the entry in182* drm_device::ctx_idr with it.183*/184int drm_setsareactx(struct drm_device *dev, void *data,185struct drm_file *file_priv)186{187struct drm_ctx_priv_map *request = data;188struct drm_local_map *map = NULL;189struct drm_map_list *r_list = NULL;190191mutex_lock(&dev->struct_mutex);192list_for_each_entry(r_list, &dev->maplist, head) {193if (r_list->map194&& r_list->user_token == (unsigned long) request->handle)195goto found;196}197bad:198mutex_unlock(&dev->struct_mutex);199return -EINVAL;200201found:202map = r_list->map;203if (!map)204goto bad;205206if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))207goto bad;208209mutex_unlock(&dev->struct_mutex);210211return 0;212}213214/*@}*/215216/******************************************************************/217/** \name The actual DRM context handling routines */218/*@{*/219220/**221* Switch context.222*223* \param dev DRM device.224* \param old old context handle.225* \param new new context handle.226* \return zero on success or a negative number on failure.227*228* Attempt to set drm_device::context_flag.229*/230static int drm_context_switch(struct drm_device * dev, int old, int new)231{232if (test_and_set_bit(0, &dev->context_flag)) {233DRM_ERROR("Reentering -- FIXME\n");234return -EBUSY;235}236237DRM_DEBUG("Context switch from %d to %d\n", old, new);238239if (new == dev->last_context) {240clear_bit(0, &dev->context_flag);241return 0;242}243244return 0;245}246247/**248* Complete context switch.249*250* \param dev DRM device.251* \param new new context handle.252* \return zero on success or a negative number on failure.253*254* Updates drm_device::last_context and drm_device::last_switch. Verifies the255* hardware lock is held, clears the drm_device::context_flag and wakes up256* drm_device::context_wait.257*/258static int drm_context_switch_complete(struct drm_device *dev,259struct drm_file *file_priv, int new)260{261dev->last_context = new; /* PRE/POST: This is the _only_ writer. */262dev->last_switch = jiffies;263264if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {265DRM_ERROR("Lock isn't held after context switch\n");266}267268/* If a context switch is ever initiated269when the kernel holds the lock, release270that lock here. */271clear_bit(0, &dev->context_flag);272wake_up(&dev->context_wait);273274return 0;275}276277/**278* Reserve contexts.279*280* \param inode device inode.281* \param file_priv DRM file private.282* \param cmd command.283* \param arg user argument pointing to a drm_ctx_res structure.284* \return zero on success or a negative number on failure.285*/286int drm_resctx(struct drm_device *dev, void *data,287struct drm_file *file_priv)288{289struct drm_ctx_res *res = data;290struct drm_ctx ctx;291int i;292293if (res->count >= DRM_RESERVED_CONTEXTS) {294memset(&ctx, 0, sizeof(ctx));295for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {296ctx.handle = i;297if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))298return -EFAULT;299}300}301res->count = DRM_RESERVED_CONTEXTS;302303return 0;304}305306/**307* Add context.308*309* \param inode device inode.310* \param file_priv DRM file private.311* \param cmd command.312* \param arg user argument pointing to a drm_ctx structure.313* \return zero on success or a negative number on failure.314*315* Get a new handle for the context and copy to userspace.316*/317int drm_addctx(struct drm_device *dev, void *data,318struct drm_file *file_priv)319{320struct drm_ctx_list *ctx_entry;321struct drm_ctx *ctx = data;322323ctx->handle = drm_ctxbitmap_next(dev);324if (ctx->handle == DRM_KERNEL_CONTEXT) {325/* Skip kernel's context and get a new one. */326ctx->handle = drm_ctxbitmap_next(dev);327}328DRM_DEBUG("%d\n", ctx->handle);329if (ctx->handle == -1) {330DRM_DEBUG("Not enough free contexts.\n");331/* Should this return -EBUSY instead? */332return -ENOMEM;333}334335ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);336if (!ctx_entry) {337DRM_DEBUG("out of memory\n");338return -ENOMEM;339}340341INIT_LIST_HEAD(&ctx_entry->head);342ctx_entry->handle = ctx->handle;343ctx_entry->tag = file_priv;344345mutex_lock(&dev->ctxlist_mutex);346list_add(&ctx_entry->head, &dev->ctxlist);347++dev->ctx_count;348mutex_unlock(&dev->ctxlist_mutex);349350return 0;351}352353int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)354{355/* This does nothing */356return 0;357}358359/**360* Get context.361*362* \param inode device inode.363* \param file_priv DRM file private.364* \param cmd command.365* \param arg user argument pointing to a drm_ctx structure.366* \return zero on success or a negative number on failure.367*/368int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)369{370struct drm_ctx *ctx = data;371372/* This is 0, because we don't handle any context flags */373ctx->flags = 0;374375return 0;376}377378/**379* Switch context.380*381* \param inode device inode.382* \param file_priv DRM file private.383* \param cmd command.384* \param arg user argument pointing to a drm_ctx structure.385* \return zero on success or a negative number on failure.386*387* Calls context_switch().388*/389int drm_switchctx(struct drm_device *dev, void *data,390struct drm_file *file_priv)391{392struct drm_ctx *ctx = data;393394DRM_DEBUG("%d\n", ctx->handle);395return drm_context_switch(dev, dev->last_context, ctx->handle);396}397398/**399* New context.400*401* \param inode device inode.402* \param file_priv DRM file private.403* \param cmd command.404* \param arg user argument pointing to a drm_ctx structure.405* \return zero on success or a negative number on failure.406*407* Calls context_switch_complete().408*/409int drm_newctx(struct drm_device *dev, void *data,410struct drm_file *file_priv)411{412struct drm_ctx *ctx = data;413414DRM_DEBUG("%d\n", ctx->handle);415drm_context_switch_complete(dev, file_priv, ctx->handle);416417return 0;418}419420/**421* Remove context.422*423* \param inode device inode.424* \param file_priv DRM file private.425* \param cmd command.426* \param arg user argument pointing to a drm_ctx structure.427* \return zero on success or a negative number on failure.428*429* If not the special kernel context, calls ctxbitmap_free() to free the specified context.430*/431int drm_rmctx(struct drm_device *dev, void *data,432struct drm_file *file_priv)433{434struct drm_ctx *ctx = data;435436DRM_DEBUG("%d\n", ctx->handle);437if (ctx->handle != DRM_KERNEL_CONTEXT) {438if (dev->driver->context_dtor)439dev->driver->context_dtor(dev, ctx->handle);440drm_ctxbitmap_free(dev, ctx->handle);441}442443mutex_lock(&dev->ctxlist_mutex);444if (!list_empty(&dev->ctxlist)) {445struct drm_ctx_list *pos, *n;446447list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {448if (pos->handle == ctx->handle) {449list_del(&pos->head);450kfree(pos);451--dev->ctx_count;452}453}454}455mutex_unlock(&dev->ctxlist_mutex);456457return 0;458}459460/*@}*/461462463