drm: move context handling code to use linux idr

main
Dave Airlie 2007-05-27 08:44:38 +10:00
parent f64674743a
commit 056c2f249a
2 changed files with 62 additions and 93 deletions

View File

@ -570,6 +570,12 @@ typedef struct drm_ctx_list {
drm_file_t *tag; /**< associated fd private data */ drm_file_t *tag; /**< associated fd private data */
} drm_ctx_list_t; } drm_ctx_list_t;
struct drm_ctx_sarea_list {
struct list_head head;
int ctx_id;
drm_map_t *map;
};
typedef struct drm_vbl_sig { typedef struct drm_vbl_sig {
struct list_head head; struct list_head head;
unsigned int sequence; unsigned int sequence;
@ -746,8 +752,8 @@ typedef struct drm_device {
int ctx_count; /**< Number of context handles */ int ctx_count; /**< Number of context handles */
struct mutex ctxlist_mutex; /**< For ctxlist */ struct mutex ctxlist_mutex; /**< For ctxlist */
drm_map_t **context_sareas; /**< per-context SAREA's */ struct idr ctx_idr;
int max_context; struct list_head context_sarealist;
struct list_head vmalist; /**< List of vmas (for debugging) */ struct list_head vmalist; /**< List of vmas (for debugging) */
drm_lock_data_t lock; /**< Information on hardware lock */ drm_lock_data_t lock; /**< Information on hardware lock */
@ -808,7 +814,6 @@ typedef struct drm_device {
struct pci_controller *hose; struct pci_controller *hose;
#endif #endif
drm_sg_mem_t *sg; /**< Scatter gather memory */ drm_sg_mem_t *sg; /**< Scatter gather memory */
unsigned long *ctx_bitmap; /**< context bitmap */
void *dev_private; /**< device private data */ void *dev_private; /**< device private data */
drm_sigdata_t sigdata; /**< For block_all_signals */ drm_sigdata_t sigdata; /**< For block_all_signals */
sigset_t sigmask; sigset_t sigmask;

View File

@ -58,20 +58,17 @@
*/ */
void drm_ctxbitmap_free(drm_device_t * dev, int ctx_handle) void drm_ctxbitmap_free(drm_device_t * dev, int ctx_handle)
{ {
if (ctx_handle < 0) struct drm_ctx_sarea_list *ctx_sarea;
goto failed;
if (!dev->ctx_bitmap)
goto failed;
if (ctx_handle < DRM_MAX_CTXBITMAP) { mutex_lock(&dev->struct_mutex);
mutex_lock(&dev->struct_mutex); ctx_sarea = idr_find(&dev->ctx_idr, ctx_handle);
clear_bit(ctx_handle, dev->ctx_bitmap); if (ctx_sarea) {
dev->context_sareas[ctx_handle] = NULL; idr_remove(&dev->ctx_idr, ctx_handle);
mutex_unlock(&dev->struct_mutex); list_del(&ctx_sarea->head);
return; drm_free(ctx_sarea, sizeof(struct drm_ctx_sarea_list), DRM_MEM_BUFS);
} } else
failed: DRM_ERROR("Attempt to free invalid context handle: %d\n", ctx_handle);
DRM_ERROR("Attempt to free invalid context handle: %d\n", ctx_handle); mutex_unlock(&dev->struct_mutex);
return; return;
} }
@ -87,56 +84,32 @@ void drm_ctxbitmap_free(drm_device_t * dev, int ctx_handle)
*/ */
static int drm_ctxbitmap_next(drm_device_t * dev) static int drm_ctxbitmap_next(drm_device_t * dev)
{ {
int bit; int new_id;
int ret;
struct drm_ctx_sarea_list *new_ctx;
if (!dev->ctx_bitmap) new_ctx = drm_calloc(1, sizeof(struct drm_ctx_sarea_list), DRM_MEM_BUFS);
if (!new_ctx)
return -1; return -1;
mutex_lock(&dev->struct_mutex); again:
bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP); if (idr_pre_get(&dev->ctx_idr, GFP_KERNEL) == 0) {
if (bit < DRM_MAX_CTXBITMAP) { DRM_ERROR("Out of memory expanding drawable idr\n");
set_bit(bit, dev->ctx_bitmap); drm_free(new_ctx, sizeof(struct drm_ctx_sarea_list), DRM_MEM_BUFS);
DRM_DEBUG("drm_ctxbitmap_next bit : %d\n", bit); return -ENOMEM;
if ((bit + 1) > dev->max_context) {
dev->max_context = (bit + 1);
if (dev->context_sareas) {
drm_map_t **ctx_sareas;
ctx_sareas = drm_realloc(dev->context_sareas,
(dev->max_context -
1) *
sizeof(*dev->
context_sareas),
dev->max_context *
sizeof(*dev->
context_sareas),
DRM_MEM_MAPS);
if (!ctx_sareas) {
clear_bit(bit, dev->ctx_bitmap);
mutex_unlock(&dev->struct_mutex);
return -1;
}
dev->context_sareas = ctx_sareas;
dev->context_sareas[bit] = NULL;
} else {
/* max_context == 1 at this point */
dev->context_sareas =
drm_alloc(dev->max_context *
sizeof(*dev->context_sareas),
DRM_MEM_MAPS);
if (!dev->context_sareas) {
clear_bit(bit, dev->ctx_bitmap);
mutex_unlock(&dev->struct_mutex);
return -1;
}
dev->context_sareas[bit] = NULL;
}
}
mutex_unlock(&dev->struct_mutex);
return bit;
} }
mutex_lock(&dev->struct_mutex);
ret = idr_get_new_above(&dev->ctx_idr, new_ctx, DRM_RESERVED_CONTEXTS, &new_id);
if (ret == -EAGAIN) {
mutex_unlock(&dev->struct_mutex);
goto again;
}
mutex_unlock(&dev->struct_mutex); mutex_unlock(&dev->struct_mutex);
return -1;
new_ctx->ctx_id = new_id;
list_add(&new_ctx->head, &dev->context_sarealist);
return new_id;
} }
/** /**
@ -149,25 +122,8 @@ static int drm_ctxbitmap_next(drm_device_t * dev)
*/ */
int drm_ctxbitmap_init(drm_device_t * dev) int drm_ctxbitmap_init(drm_device_t * dev)
{ {
int i; idr_init(&dev->ctx_idr);
int temp; INIT_LIST_HEAD(&dev->context_sarealist);
mutex_lock(&dev->struct_mutex);
dev->ctx_bitmap = (unsigned long *)drm_alloc(PAGE_SIZE,
DRM_MEM_CTXBITMAP);
if (dev->ctx_bitmap == NULL) {
mutex_unlock(&dev->struct_mutex);
return -ENOMEM;
}
memset((void *)dev->ctx_bitmap, 0, PAGE_SIZE);
dev->context_sareas = NULL;
dev->max_context = -1;
mutex_unlock(&dev->struct_mutex);
for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
temp = drm_ctxbitmap_next(dev);
DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
}
return 0; return 0;
} }
@ -182,12 +138,13 @@ int drm_ctxbitmap_init(drm_device_t * dev)
*/ */
void drm_ctxbitmap_cleanup(drm_device_t * dev) void drm_ctxbitmap_cleanup(drm_device_t * dev)
{ {
struct drm_ctx_sarea_list *ctx_entry, *ctx_temp;
mutex_lock(&dev->struct_mutex); mutex_lock(&dev->struct_mutex);
if (dev->context_sareas) list_for_each_entry_safe(ctx_entry, ctx_temp, &dev->context_sarealist, head) {
drm_free(dev->context_sareas, idr_remove(&dev->ctx_idr, ctx_entry->ctx_id);
sizeof(*dev->context_sareas) * drm_free(ctx_entry, sizeof(struct drm_ctx_sarea_list), DRM_MEM_MAPS);
dev->max_context, DRM_MEM_MAPS); }
drm_free((void *)dev->ctx_bitmap, PAGE_SIZE, DRM_MEM_CTXBITMAP);
mutex_unlock(&dev->struct_mutex); mutex_unlock(&dev->struct_mutex);
} }
@ -218,18 +175,20 @@ int drm_getsareactx(struct inode *inode, struct file *filp,
drm_ctx_priv_map_t request; drm_ctx_priv_map_t request;
drm_map_t *map; drm_map_t *map;
drm_map_list_t *_entry; drm_map_list_t *_entry;
struct drm_ctx_sarea_list *ctx_sarea;
if (copy_from_user(&request, argp, sizeof(request))) if (copy_from_user(&request, argp, sizeof(request)))
return -EFAULT; return -EFAULT;
mutex_lock(&dev->struct_mutex); mutex_lock(&dev->struct_mutex);
if (dev->max_context < 0
|| request.ctx_id >= (unsigned)dev->max_context) { ctx_sarea = idr_find(&dev->ctx_idr, request.ctx_id);
if (!ctx_sarea) {
mutex_unlock(&dev->struct_mutex); mutex_unlock(&dev->struct_mutex);
return -EINVAL; return -EINVAL;
} }
map = dev->context_sareas[request.ctx_id]; map = ctx_sarea->map;
mutex_unlock(&dev->struct_mutex); mutex_unlock(&dev->struct_mutex);
request.handle = NULL; request.handle = NULL;
@ -268,6 +227,7 @@ int drm_setsareactx(struct inode *inode, struct file *filp,
drm_ctx_priv_map_t request; drm_ctx_priv_map_t request;
drm_map_t *map = NULL; drm_map_t *map = NULL;
drm_map_list_t *r_list = NULL; drm_map_list_t *r_list = NULL;
struct drm_ctx_sarea_list *ctx_sarea;
if (copy_from_user(&request, if (copy_from_user(&request,
(drm_ctx_priv_map_t __user *) arg, sizeof(request))) (drm_ctx_priv_map_t __user *) arg, sizeof(request)))
@ -287,11 +247,15 @@ int drm_setsareactx(struct inode *inode, struct file *filp,
map = r_list->map; map = r_list->map;
if (!map) if (!map)
goto bad; goto bad;
if (dev->max_context < 0)
mutex_lock(&dev->struct_mutex);
ctx_sarea = idr_find(&dev->ctx_idr, request.ctx_id);
if (!ctx_sarea) {
goto bad; goto bad;
if (request.ctx_id >= (unsigned)dev->max_context) }
goto bad;
dev->context_sareas[request.ctx_id] = map; ctx_sarea->map = map;
mutex_unlock(&dev->struct_mutex); mutex_unlock(&dev->struct_mutex);
return 0; return 0;
} }