drm_drawable.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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(DRM_IOCTL_ARGS)
  41. {
  42. DRM_DEVICE;
  43. unsigned long irqflags;
  44. struct drm_draw draw;
  45. int new_id = 0;
  46. int ret;
  47. again:
  48. if (idr_pre_get(&dev->drw_idr, GFP_KERNEL) == 0) {
  49. DRM_ERROR("Out of memory expanding drawable idr\n");
  50. return -ENOMEM;
  51. }
  52. spin_lock_irqsave(&dev->drw_lock, irqflags);
  53. ret = idr_get_new_above(&dev->drw_idr, NULL, 1, &new_id);
  54. if (ret == -EAGAIN) {
  55. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  56. goto again;
  57. }
  58. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  59. draw.handle = new_id;
  60. DRM_DEBUG("%d\n", draw.handle);
  61. DRM_COPY_TO_USER_IOCTL((struct drm_draw __user *)data, draw, sizeof(draw));
  62. return 0;
  63. }
  64. /**
  65. * Free drawable ID and memory to store information about it.
  66. */
  67. int drm_rmdraw(DRM_IOCTL_ARGS)
  68. {
  69. DRM_DEVICE;
  70. struct drm_draw draw;
  71. unsigned long irqflags;
  72. DRM_COPY_FROM_USER_IOCTL(draw, (struct drm_draw __user *) data,
  73. sizeof(draw));
  74. spin_lock_irqsave(&dev->drw_lock, irqflags);
  75. drm_free(drm_get_drawable_info(dev, draw.handle),
  76. sizeof(struct drm_drawable_info), DRM_MEM_BUFS);
  77. idr_remove(&dev->drw_idr, draw.handle);
  78. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  79. DRM_DEBUG("%d\n", draw.handle);
  80. return 0;
  81. }
  82. int drm_update_drawable_info(DRM_IOCTL_ARGS)
  83. {
  84. DRM_DEVICE;
  85. struct drm_update_draw update;
  86. unsigned long irqflags;
  87. struct drm_clip_rect *rects;
  88. struct drm_drawable_info *info;
  89. int err;
  90. DRM_COPY_FROM_USER_IOCTL(update, (struct drm_update_draw __user *) data,
  91. sizeof(update));
  92. info = idr_find(&dev->drw_idr, update.handle);
  93. if (!info) {
  94. info = drm_calloc(1, sizeof(*info), DRM_MEM_BUFS);
  95. if (!info)
  96. return -ENOMEM;
  97. if (IS_ERR(idr_replace(&dev->drw_idr, info, update.handle))) {
  98. DRM_ERROR("No such drawable %d\n", update.handle);
  99. drm_free(info, sizeof(*info), DRM_MEM_BUFS);
  100. return -EINVAL;
  101. }
  102. }
  103. switch (update.type) {
  104. case DRM_DRAWABLE_CLIPRECTS:
  105. if (update.num != info->num_rects) {
  106. rects = drm_alloc(update.num * sizeof(struct drm_clip_rect),
  107. DRM_MEM_BUFS);
  108. } else
  109. rects = info->rects;
  110. if (update.num && !rects) {
  111. DRM_ERROR("Failed to allocate cliprect memory\n");
  112. err = DRM_ERR(ENOMEM);
  113. goto error;
  114. }
  115. if (update.num && DRM_COPY_FROM_USER(rects,
  116. (struct drm_clip_rect __user *)
  117. (unsigned long)update.data,
  118. update.num *
  119. sizeof(*rects))) {
  120. DRM_ERROR("Failed to copy cliprects from userspace\n");
  121. err = DRM_ERR(EFAULT);
  122. goto error;
  123. }
  124. spin_lock_irqsave(&dev->drw_lock, irqflags);
  125. if (rects != info->rects) {
  126. drm_free(info->rects, info->num_rects *
  127. sizeof(struct drm_clip_rect), DRM_MEM_BUFS);
  128. }
  129. info->rects = rects;
  130. info->num_rects = update.num;
  131. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  132. DRM_DEBUG("Updated %d cliprects for drawable %d\n",
  133. info->num_rects, update.handle);
  134. break;
  135. default:
  136. DRM_ERROR("Invalid update type %d\n", update.type);
  137. return DRM_ERR(EINVAL);
  138. }
  139. return 0;
  140. error:
  141. if (rects != info->rects)
  142. drm_free(rects, update.num * sizeof(struct drm_clip_rect),
  143. DRM_MEM_BUFS);
  144. return err;
  145. }
  146. /**
  147. * Caller must hold the drawable spinlock!
  148. */
  149. struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev, drm_drawable_t id)
  150. {
  151. return idr_find(&dev->drw_idr, id);
  152. }
  153. EXPORT_SYMBOL(drm_get_drawable_info);
  154. static int drm_drawable_free(int idr, void *p, void *data)
  155. {
  156. struct drm_drawable_info *info = p;
  157. if (info) {
  158. drm_free(info->rects, info->num_rects *
  159. sizeof(struct drm_clip_rect), DRM_MEM_BUFS);
  160. drm_free(info, sizeof(*info), DRM_MEM_BUFS);
  161. }
  162. return 0;
  163. }
  164. void drm_drawable_free_all(struct drm_device *dev)
  165. {
  166. idr_for_each(&dev->drw_idr, drm_drawable_free, NULL);
  167. idr_remove_all(&dev->drw_idr);
  168. }