drm_drawable.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. drm_free(info->rects, info->num_rects * sizeof(struct drm_clip_rect),
  73. DRM_MEM_BUFS);
  74. drm_free(info, sizeof(struct drm_drawable_info), DRM_MEM_BUFS);
  75. idr_remove(&dev->drw_idr, draw->handle);
  76. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  77. DRM_DEBUG("%d\n", draw->handle);
  78. return 0;
  79. }
  80. int drm_update_drawable_info(struct drm_device *dev, void *data, struct drm_file *file_priv)
  81. {
  82. struct drm_update_draw *update = data;
  83. unsigned long irqflags;
  84. struct drm_clip_rect *rects;
  85. struct drm_drawable_info *info;
  86. int err;
  87. info = idr_find(&dev->drw_idr, update->handle);
  88. if (!info) {
  89. info = drm_calloc(1, sizeof(*info), DRM_MEM_BUFS);
  90. if (!info)
  91. return -ENOMEM;
  92. if (IS_ERR(idr_replace(&dev->drw_idr, info, update->handle))) {
  93. DRM_ERROR("No such drawable %d\n", update->handle);
  94. drm_free(info, sizeof(*info), DRM_MEM_BUFS);
  95. return -EINVAL;
  96. }
  97. }
  98. switch (update->type) {
  99. case DRM_DRAWABLE_CLIPRECTS:
  100. if (update->num == 0)
  101. rects = NULL;
  102. else if (update->num != info->num_rects) {
  103. rects = drm_alloc(update->num * sizeof(struct drm_clip_rect),
  104. DRM_MEM_BUFS);
  105. } else
  106. rects = info->rects;
  107. if (update->num && !rects) {
  108. DRM_ERROR("Failed to allocate cliprect memory\n");
  109. err = -ENOMEM;
  110. goto error;
  111. }
  112. if (update->num && DRM_COPY_FROM_USER(rects,
  113. (struct drm_clip_rect __user *)
  114. (unsigned long)update->data,
  115. update->num *
  116. sizeof(*rects))) {
  117. DRM_ERROR("Failed to copy cliprects from userspace\n");
  118. err = -EFAULT;
  119. goto error;
  120. }
  121. spin_lock_irqsave(&dev->drw_lock, irqflags);
  122. if (rects != info->rects) {
  123. drm_free(info->rects, info->num_rects *
  124. sizeof(struct drm_clip_rect), DRM_MEM_BUFS);
  125. }
  126. info->rects = rects;
  127. info->num_rects = update->num;
  128. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  129. DRM_DEBUG("Updated %d cliprects for drawable %d\n",
  130. info->num_rects, update->handle);
  131. break;
  132. default:
  133. DRM_ERROR("Invalid update type %d\n", update->type);
  134. return -EINVAL;
  135. }
  136. return 0;
  137. error:
  138. if (rects != info->rects)
  139. drm_free(rects, update->num * sizeof(struct drm_clip_rect),
  140. DRM_MEM_BUFS);
  141. return err;
  142. }
  143. /**
  144. * Caller must hold the drawable spinlock!
  145. */
  146. struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev, drm_drawable_t id)
  147. {
  148. return idr_find(&dev->drw_idr, id);
  149. }
  150. EXPORT_SYMBOL(drm_get_drawable_info);
  151. static int drm_drawable_free(int idr, void *p, void *data)
  152. {
  153. struct drm_drawable_info *info = p;
  154. if (info) {
  155. drm_free(info->rects, info->num_rects *
  156. sizeof(struct drm_clip_rect), DRM_MEM_BUFS);
  157. drm_free(info, sizeof(*info), DRM_MEM_BUFS);
  158. }
  159. return 0;
  160. }
  161. void drm_drawable_free_all(struct drm_device *dev)
  162. {
  163. idr_for_each(&dev->drw_idr, drm_drawable_free, NULL);
  164. idr_remove_all(&dev->drw_idr);
  165. }