drm_global.c revision df748b025d1357c2b9659e16a6040596e60e4257
1ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom/************************************************************************** 2ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * 3ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * Copyright 2008-2009 VMware, Inc., Palo Alto, CA., USA 4ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * All Rights Reserved. 5ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * 6ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * Permission is hereby granted, free of charge, to any person obtaining a 7ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * copy of this software and associated documentation files (the 8ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * "Software"), to deal in the Software without restriction, including 9ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * without limitation the rights to use, copy, modify, merge, publish, 10ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * distribute, sub license, and/or sell copies of the Software, and to 11ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * permit persons to whom the Software is furnished to do so, subject to 12ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * the following conditions: 13ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * 14ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * The above copyright notice and this permission notice (including the 15ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * next paragraph) shall be included in all copies or substantial portions 16ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * of the Software. 17ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * 18ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * USE OR OTHER DEALINGS IN THE SOFTWARE. 25ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * 26ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom **************************************************************************/ 27ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom/* 28ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 29ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom */ 30ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 31ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom#include "ttm/ttm_module.h" 32ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom#include <linux/mutex.h> 33ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom#include <linux/slab.h> 34ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom#include <linux/module.h> 35ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 36ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstromstruct ttm_global_item { 37ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom struct mutex mutex; 38ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom void *object; 39ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom int refcount; 40ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom}; 41ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 42ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstromstatic struct ttm_global_item glob[TTM_GLOBAL_NUM]; 43ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 44ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstromvoid ttm_global_init(void) 45ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom{ 46ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom int i; 47ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 48ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom for (i = 0; i < TTM_GLOBAL_NUM; ++i) { 49ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom struct ttm_global_item *item = &glob[i]; 50ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom mutex_init(&item->mutex); 51ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom item->object = NULL; 52ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom item->refcount = 0; 53ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom } 54ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom} 55ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 56ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstromvoid ttm_global_release(void) 57ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom{ 58ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom int i; 59ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom for (i = 0; i < TTM_GLOBAL_NUM; ++i) { 60ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom struct ttm_global_item *item = &glob[i]; 61ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom BUG_ON(item->object != NULL); 62ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom BUG_ON(item->refcount != 0); 63ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom } 64ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom} 65ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 66ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstromint ttm_global_item_ref(struct ttm_global_reference *ref) 67ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom{ 68ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom int ret; 69ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom struct ttm_global_item *item = &glob[ref->global_type]; 70ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom void *object; 71ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 72ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom mutex_lock(&item->mutex); 73ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom if (item->refcount == 0) { 745fd9cbad3a4ae82c83c55b9c621d156c326724efThomas Hellstrom item->object = kzalloc(ref->size, GFP_KERNEL); 75ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom if (unlikely(item->object == NULL)) { 76ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom ret = -ENOMEM; 77ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom goto out_err; 78ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom } 79ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 80ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom ref->object = item->object; 81ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom ret = ref->init(ref); 82ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom if (unlikely(ret != 0)) 83ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom goto out_err; 84ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 85ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom } 86df748b025d1357c2b9659e16a6040596e60e4257Dave Airlie ++item->refcount; 87ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom ref->object = item->object; 88ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom object = item->object; 89ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom mutex_unlock(&item->mutex); 90ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom return 0; 91ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstromout_err: 92ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom mutex_unlock(&item->mutex); 93ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom item->object = NULL; 94ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom return ret; 95ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom} 96ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas HellstromEXPORT_SYMBOL(ttm_global_item_ref); 97ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 98ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstromvoid ttm_global_item_unref(struct ttm_global_reference *ref) 99ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom{ 100ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom struct ttm_global_item *item = &glob[ref->global_type]; 101ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 102ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom mutex_lock(&item->mutex); 103ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom BUG_ON(item->refcount == 0); 104ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom BUG_ON(ref->object != item->object); 105ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom if (--item->refcount == 0) { 106ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom ref->release(ref); 107ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom item->object = NULL; 108ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom } 109ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom mutex_unlock(&item->mutex); 110ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom} 111ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas HellstromEXPORT_SYMBOL(ttm_global_item_unref); 112ba4e7d973dd09b66912ac4c0856add8b0703a997Thomas Hellstrom 113