drm_drawable.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. kfree(info->rects);
  77. kfree(info);
  78. idr_remove(&dev->drw_idr, draw->handle);
  79. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  80. DRM_DEBUG("%d\n", draw->handle);
  81. return 0;
  82. }
  83. int drm_update_drawable_info(struct drm_device *dev, void *data, struct drm_file *file_priv)
  84. {
  85. struct drm_update_draw *update = data;
  86. unsigned long irqflags;
  87. struct drm_clip_rect *rects;
  88. struct drm_drawable_info *info;
  89. int err;
  90. info = idr_find(&dev->drw_idr, update->handle);
  91. if (!info) {
  92. info = kzalloc(sizeof(*info), GFP_KERNEL);
  93. if (!info)
  94. return -ENOMEM;
  95. if (IS_ERR(idr_replace(&dev->drw_idr, info, update->handle))) {
  96. DRM_ERROR("No such drawable %d\n", update->handle);
  97. kfree(info);
  98. return -EINVAL;
  99. }
  100. }
  101. switch (update->type) {
  102. case DRM_DRAWABLE_CLIPRECTS:
  103. if (update->num == 0)
  104. rects = NULL;
  105. else if (update->num != info->num_rects) {
  106. rects = kmalloc(update->num *
  107. sizeof(struct drm_clip_rect),
  108. GFP_KERNEL);
  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. kfree(info->rects);
  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 -EINVAL;
  138. }
  139. return 0;
  140. error:
  141. if (rects != info->rects)
  142. kfree(rects);
  143. return err;
  144. }
  145. /**
  146. * Caller must hold the drawable spinlock!
  147. */
  148. struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev, drm_drawable_t id)
  149. {
  150. return idr_find(&dev->drw_idr, id);
  151. }
  152. EXPORT_SYMBOL(drm_get_drawable_info);
  153. static int drm_drawable_free(int idr, void *p, void *data)
  154. {
  155. struct drm_drawable_info *info = p;
  156. if (info) {
  157. kfree(info->rects);
  158. kfree(info);
  159. }
  160. return 0;
  161. }
  162. void drm_drawable_free_all(struct drm_device *dev)
  163. {
  164. idr_for_each(&dev->drw_idr, drm_drawable_free, NULL);
  165. idr_remove_all(&dev->drw_idr);
  166. }