drm_drawable.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * \file drm_drawable.c
  3. * IOCTLs for drawables
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. * \author Michel Dänzer <michel@tungstengraphics.com>
  8. */
  9. /*
  10. * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
  11. *
  12. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  13. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  14. * Copyright 2006 Tungsten Graphics, Inc., Bismarck, North Dakota.
  15. * All Rights Reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining a
  18. * copy of this software and associated documentation files (the "Software"),
  19. * to deal in the Software without restriction, including without limitation
  20. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  21. * and/or sell copies of the Software, and to permit persons to whom the
  22. * Software is furnished to do so, subject to the following conditions:
  23. *
  24. * The above copyright notice and this permission notice (including the next
  25. * paragraph) shall be included in all copies or substantial portions of the
  26. * Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  31. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  32. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  33. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  34. * OTHER DEALINGS IN THE SOFTWARE.
  35. */
  36. #include "drmP.h"
  37. /**
  38. * Allocate drawable ID and memory to store information about it.
  39. */
  40. int drm_adddraw(struct drm_device *dev, void *data, struct drm_file *file_priv)
  41. {
  42. unsigned long irqflags;
  43. struct drm_draw *draw = data;
  44. int new_id = 0;
  45. int ret;
  46. again:
  47. if (idr_pre_get(&dev->drw_idr, GFP_KERNEL) == 0) {
  48. DRM_ERROR("Out of memory expanding drawable idr\n");
  49. return -ENOMEM;
  50. }
  51. spin_lock_irqsave(&dev->drw_lock, irqflags);
  52. ret = idr_get_new_above(&dev->drw_idr, NULL, 1, &new_id);
  53. if (ret == -EAGAIN) {
  54. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  55. goto again;
  56. }
  57. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  58. draw->handle = new_id;
  59. DRM_DEBUG("%d\n", draw->handle);
  60. return 0;
  61. }
  62. /**
  63. * Free drawable ID and memory to store information about it.
  64. */
  65. int drm_rmdraw(struct drm_device *dev, void *data, struct drm_file *file_priv)
  66. {
  67. struct drm_draw *draw = data;
  68. unsigned long irqflags;
  69. struct drm_drawable_info *info;
  70. spin_lock_irqsave(&dev->drw_lock, irqflags);
  71. info = drm_get_drawable_info(dev, draw->handle);
  72. if (info == NULL) {
  73. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  74. return -EINVAL;
  75. }
  76. drm_free(info->rects, info->num_rects * sizeof(struct drm_clip_rect),
  77. DRM_MEM_BUFS);
  78. drm_free(info, sizeof(struct drm_drawable_info), DRM_MEM_BUFS);
  79. idr_remove(&dev->drw_idr, draw->handle);
  80. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  81. DRM_DEBUG("%d\n", draw->handle);
  82. return 0;
  83. }
  84. int drm_update_drawable_info(struct drm_device *dev, void *data, struct drm_file *file_priv)
  85. {
  86. struct drm_update_draw *update = data;
  87. unsigned long irqflags;
  88. struct drm_clip_rect *rects;
  89. struct drm_drawable_info *info;
  90. int err;
  91. info = idr_find(&dev->drw_idr, update->handle);
  92. if (!info) {
  93. info = drm_calloc(1, sizeof(*info), DRM_MEM_BUFS);
  94. if (!info)
  95. return -ENOMEM;
  96. if (IS_ERR(idr_replace(&dev->drw_idr, info, update->handle))) {
  97. DRM_ERROR("No such drawable %d\n", update->handle);
  98. drm_free(info, sizeof(*info), DRM_MEM_BUFS);
  99. return -EINVAL;
  100. }
  101. }
  102. switch (update->type) {
  103. case DRM_DRAWABLE_CLIPRECTS:
  104. if (update->num == 0)
  105. rects = NULL;
  106. else if (update->num != info->num_rects) {
  107. rects = drm_alloc(update->num * sizeof(struct drm_clip_rect),
  108. DRM_MEM_BUFS);
  109. } else
  110. rects = info->rects;
  111. if (update->num && !rects) {
  112. DRM_ERROR("Failed to allocate cliprect memory\n");
  113. err = -ENOMEM;
  114. goto error;
  115. }
  116. if (update->num && DRM_COPY_FROM_USER(rects,
  117. (struct drm_clip_rect __user *)
  118. (unsigned long)update->data,
  119. update->num *
  120. sizeof(*rects))) {
  121. DRM_ERROR("Failed to copy cliprects from userspace\n");
  122. err = -EFAULT;
  123. goto error;
  124. }
  125. spin_lock_irqsave(&dev->drw_lock, irqflags);
  126. if (rects != info->rects) {
  127. drm_free(info->rects, info->num_rects *
  128. sizeof(struct drm_clip_rect), DRM_MEM_BUFS);
  129. }
  130. info->rects = rects;
  131. info->num_rects = update->num;
  132. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  133. DRM_DEBUG("Updated %d cliprects for drawable %d\n",
  134. info->num_rects, update->handle);
  135. break;
  136. default:
  137. DRM_ERROR("Invalid update type %d\n", update->type);
  138. return -EINVAL;
  139. }
  140. return 0;
  141. error:
  142. if (rects != info->rects)
  143. drm_free(rects, update->num * sizeof(struct drm_clip_rect),
  144. DRM_MEM_BUFS);
  145. return err;
  146. }
  147. /**
  148. * Caller must hold the drawable spinlock!
  149. */
  150. struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev, drm_drawable_t id)
  151. {
  152. return idr_find(&dev->drw_idr, id);
  153. }
  154. EXPORT_SYMBOL(drm_get_drawable_info);
  155. static int drm_drawable_free(int idr, void *p, void *data)
  156. {
  157. struct drm_drawable_info *info = p;
  158. if (info) {
  159. drm_free(info->rects, info->num_rects *
  160. sizeof(struct drm_clip_rect), DRM_MEM_BUFS);
  161. drm_free(info, sizeof(*info), DRM_MEM_BUFS);
  162. }
  163. return 0;
  164. }
  165. void drm_drawable_free_all(struct drm_device *dev)
  166. {
  167. idr_for_each(&dev->drw_idr, drm_drawable_free, NULL);
  168. idr_remove_all(&dev->drw_idr);
  169. }