drm_drawable.c 5.2 KB

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