drm_drawable.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. int i, j;
  45. u32 *bitfield = dev->drw_bitfield;
  46. unsigned int bitfield_length = dev->drw_bitfield_length;
  47. drm_drawable_info_t **info = dev->drw_info;
  48. unsigned int info_length = dev->drw_info_length;
  49. drm_draw_t draw;
  50. for (i = 0, j = 0; i < bitfield_length; i++) {
  51. if (bitfield[i] == ~0)
  52. continue;
  53. for (; j < 8 * sizeof(*bitfield); j++)
  54. if (!(bitfield[i] & (1 << j)))
  55. goto done;
  56. }
  57. done:
  58. if (i == bitfield_length) {
  59. bitfield_length++;
  60. bitfield = drm_alloc(bitfield_length * sizeof(*bitfield),
  61. DRM_MEM_BUFS);
  62. if (!bitfield) {
  63. DRM_ERROR("Failed to allocate new drawable bitfield\n");
  64. return DRM_ERR(ENOMEM);
  65. }
  66. if (8 * sizeof(*bitfield) * bitfield_length > info_length) {
  67. info_length += 8 * sizeof(*bitfield);
  68. info = drm_alloc(info_length * sizeof(*info),
  69. DRM_MEM_BUFS);
  70. if (!info) {
  71. DRM_ERROR("Failed to allocate new drawable info"
  72. " array\n");
  73. drm_free(bitfield,
  74. bitfield_length * sizeof(*bitfield),
  75. DRM_MEM_BUFS);
  76. return DRM_ERR(ENOMEM);
  77. }
  78. }
  79. bitfield[i] = 0;
  80. }
  81. draw.handle = i * 8 * sizeof(*bitfield) + j;
  82. DRM_DEBUG("%d\n", draw.handle);
  83. spin_lock_irqsave(&dev->drw_lock, irqflags);
  84. bitfield[i] |= 1 << j;
  85. info[draw.handle] = NULL;
  86. if (bitfield != dev->drw_bitfield) {
  87. memcpy(bitfield, dev->drw_bitfield, dev->drw_bitfield_length *
  88. sizeof(*bitfield));
  89. drm_free(dev->drw_bitfield, sizeof(*bitfield) *
  90. dev->drw_bitfield_length, DRM_MEM_BUFS);
  91. dev->drw_bitfield = bitfield;
  92. dev->drw_bitfield_length = bitfield_length;
  93. }
  94. if (info != dev->drw_info) {
  95. memcpy(info, dev->drw_info, dev->drw_info_length *
  96. sizeof(*info));
  97. drm_free(dev->drw_info, sizeof(*info) * dev->drw_info_length,
  98. DRM_MEM_BUFS);
  99. dev->drw_info = info;
  100. dev->drw_info_length = info_length;
  101. }
  102. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  103. DRM_COPY_TO_USER_IOCTL((drm_draw_t __user *)data, draw, sizeof(draw));
  104. return 0;
  105. }
  106. /**
  107. * Free drawable ID and memory to store information about it.
  108. */
  109. int drm_rmdraw(DRM_IOCTL_ARGS)
  110. {
  111. DRM_DEVICE;
  112. drm_draw_t draw;
  113. unsigned int idx, shift;
  114. unsigned long irqflags;
  115. u32 *bitfield = dev->drw_bitfield;
  116. unsigned int bitfield_length = dev->drw_bitfield_length;
  117. drm_drawable_info_t **info = dev->drw_info;
  118. unsigned int info_length = dev->drw_info_length;
  119. DRM_COPY_FROM_USER_IOCTL(draw, (drm_draw_t __user *) data,
  120. sizeof(draw));
  121. idx = draw.handle / (8 * sizeof(*bitfield));
  122. shift = draw.handle % (8 * sizeof(*bitfield));
  123. if (idx >= bitfield_length ||
  124. !(bitfield[idx] & (1 << shift))) {
  125. DRM_DEBUG("No such drawable %d\n", draw.handle);
  126. return 0;
  127. }
  128. spin_lock_irqsave(&dev->drw_lock, irqflags);
  129. bitfield[idx] &= ~(1 << shift);
  130. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  131. /* Can we shrink the arrays? */
  132. if (idx == bitfield_length - 1) {
  133. while (idx >= 0 && !bitfield[idx])
  134. --idx;
  135. bitfield_length = idx + 1;
  136. if (idx != draw.handle / (8 * sizeof(*bitfield)))
  137. bitfield = drm_alloc(bitfield_length *
  138. sizeof(*bitfield), DRM_MEM_BUFS);
  139. if (!bitfield && bitfield_length) {
  140. bitfield = dev->drw_bitfield;
  141. bitfield_length = dev->drw_bitfield_length;
  142. }
  143. }
  144. if (bitfield != dev->drw_bitfield) {
  145. info_length = 8 * sizeof(*bitfield) * bitfield_length;
  146. info = drm_alloc(info_length * sizeof(*info), DRM_MEM_BUFS);
  147. if (!info && info_length) {
  148. info = dev->drw_info;
  149. info_length = dev->drw_info_length;
  150. }
  151. spin_lock_irqsave(&dev->drw_lock, irqflags);
  152. memcpy(bitfield, dev->drw_bitfield, bitfield_length *
  153. sizeof(*bitfield));
  154. drm_free(dev->drw_bitfield, sizeof(*bitfield) *
  155. dev->drw_bitfield_length, DRM_MEM_BUFS);
  156. dev->drw_bitfield = bitfield;
  157. dev->drw_bitfield_length = bitfield_length;
  158. if (info != dev->drw_info) {
  159. memcpy(info, dev->drw_info, info_length *
  160. sizeof(*info));
  161. drm_free(dev->drw_info, sizeof(*info) *
  162. dev->drw_info_length, DRM_MEM_BUFS);
  163. dev->drw_info = info;
  164. dev->drw_info_length = info_length;
  165. }
  166. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  167. }
  168. DRM_DEBUG("%d\n", draw.handle);
  169. return 0;
  170. }
  171. int drm_update_drawable_info(DRM_IOCTL_ARGS) {
  172. DRM_DEVICE;
  173. drm_update_draw_t update;
  174. unsigned int id, idx, shift;
  175. u32 *bitfield = dev->drw_bitfield;
  176. unsigned long irqflags, bitfield_length = dev->drw_bitfield_length;
  177. drm_drawable_info_t *info;
  178. drm_clip_rect_t *rects;
  179. int err;
  180. DRM_COPY_FROM_USER_IOCTL(update, (drm_update_draw_t __user *) data,
  181. sizeof(update));
  182. id = update.handle;
  183. idx = id / (8 * sizeof(*bitfield));
  184. shift = id % (8 * sizeof(*bitfield));
  185. if (idx >= bitfield_length || !(bitfield[idx] & (1 << shift))) {
  186. DRM_ERROR("No such drawable %d\n", update.handle);
  187. return DRM_ERR(EINVAL);
  188. }
  189. info = dev->drw_info[id];
  190. if (!info) {
  191. info = drm_calloc(1, sizeof(drm_drawable_info_t), DRM_MEM_BUFS);
  192. if (!info) {
  193. DRM_ERROR("Failed to allocate drawable info memory\n");
  194. return DRM_ERR(ENOMEM);
  195. }
  196. }
  197. switch (update.type) {
  198. case DRM_DRAWABLE_CLIPRECTS:
  199. if (update.num != info->num_rects) {
  200. rects = drm_alloc(update.num * sizeof(drm_clip_rect_t),
  201. DRM_MEM_BUFS);
  202. } else
  203. rects = info->rects;
  204. if (update.num && !rects) {
  205. DRM_ERROR("Failed to allocate cliprect memory\n");
  206. err = DRM_ERR(ENOMEM);
  207. goto error;
  208. }
  209. if (update.num && DRM_COPY_FROM_USER(rects,
  210. (drm_clip_rect_t __user *)
  211. (unsigned long)update.data,
  212. update.num *
  213. sizeof(*rects))) {
  214. DRM_ERROR("Failed to copy cliprects from userspace\n");
  215. err = DRM_ERR(EFAULT);
  216. goto error;
  217. }
  218. spin_lock_irqsave(&dev->drw_lock, irqflags);
  219. if (rects != info->rects) {
  220. drm_free(info->rects, info->num_rects *
  221. sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
  222. }
  223. info->rects = rects;
  224. info->num_rects = update.num;
  225. dev->drw_info[id] = info;
  226. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  227. DRM_DEBUG("Updated %d cliprects for drawable %d\n",
  228. info->num_rects, id);
  229. break;
  230. default:
  231. DRM_ERROR("Invalid update type %d\n", update.type);
  232. return DRM_ERR(EINVAL);
  233. }
  234. return 0;
  235. error:
  236. if (!dev->drw_info[id])
  237. drm_free(info, sizeof(*info), DRM_MEM_BUFS);
  238. else if (rects != dev->drw_info[id]->rects)
  239. drm_free(rects, update.num *
  240. sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
  241. return err;
  242. }
  243. /**
  244. * Caller must hold the drawable spinlock!
  245. */
  246. drm_drawable_info_t *drm_get_drawable_info(drm_device_t *dev, drm_drawable_t id) {
  247. u32 *bitfield = dev->drw_bitfield;
  248. unsigned int idx = id / (8 * sizeof(*bitfield));
  249. unsigned int shift = id % (8 * sizeof(*bitfield));
  250. if (idx >= dev->drw_bitfield_length ||
  251. !(bitfield[idx] & (1 << shift))) {
  252. DRM_DEBUG("No such drawable %d\n", id);
  253. return NULL;
  254. }
  255. return dev->drw_info[id];
  256. }
  257. EXPORT_SYMBOL(drm_get_drawable_info);