drm_drawable.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 + 1;
  82. DRM_DEBUG("%d\n", draw.handle);
  83. spin_lock_irqsave(&dev->drw_lock, irqflags);
  84. bitfield[i] |= 1 << j;
  85. info[draw.handle - 1] = 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. int id, idx;
  114. unsigned int shift;
  115. unsigned long irqflags;
  116. u32 *bitfield = dev->drw_bitfield;
  117. unsigned int bitfield_length = dev->drw_bitfield_length;
  118. drm_drawable_info_t **info = dev->drw_info;
  119. unsigned int info_length = dev->drw_info_length;
  120. DRM_COPY_FROM_USER_IOCTL(draw, (drm_draw_t __user *) data,
  121. sizeof(draw));
  122. id = draw.handle - 1;
  123. idx = id / (8 * sizeof(*bitfield));
  124. shift = id % (8 * sizeof(*bitfield));
  125. if (idx < 0 || idx >= bitfield_length ||
  126. !(bitfield[idx] & (1 << shift))) {
  127. DRM_DEBUG("No such drawable %d\n", draw.handle);
  128. return 0;
  129. }
  130. spin_lock_irqsave(&dev->drw_lock, irqflags);
  131. bitfield[idx] &= ~(1 << shift);
  132. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  133. if (info[id]) {
  134. drm_free(info[id]->rects, info[id]->num_rects *
  135. sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
  136. drm_free(info[id], sizeof(**info), DRM_MEM_BUFS);
  137. }
  138. /* Can we shrink the arrays? */
  139. if (idx == bitfield_length - 1) {
  140. while (idx >= 0 && !bitfield[idx])
  141. --idx;
  142. bitfield_length = idx + 1;
  143. if (idx != id / (8 * sizeof(*bitfield)))
  144. bitfield = drm_alloc(bitfield_length *
  145. sizeof(*bitfield), DRM_MEM_BUFS);
  146. if (!bitfield && bitfield_length) {
  147. bitfield = dev->drw_bitfield;
  148. bitfield_length = dev->drw_bitfield_length;
  149. }
  150. }
  151. if (bitfield != dev->drw_bitfield) {
  152. info_length = 8 * sizeof(*bitfield) * bitfield_length;
  153. info = drm_alloc(info_length * sizeof(*info), DRM_MEM_BUFS);
  154. if (!info && info_length) {
  155. info = dev->drw_info;
  156. info_length = dev->drw_info_length;
  157. }
  158. spin_lock_irqsave(&dev->drw_lock, irqflags);
  159. memcpy(bitfield, dev->drw_bitfield, bitfield_length *
  160. sizeof(*bitfield));
  161. drm_free(dev->drw_bitfield, sizeof(*bitfield) *
  162. dev->drw_bitfield_length, DRM_MEM_BUFS);
  163. dev->drw_bitfield = bitfield;
  164. dev->drw_bitfield_length = bitfield_length;
  165. if (info != dev->drw_info) {
  166. memcpy(info, dev->drw_info, info_length *
  167. sizeof(*info));
  168. drm_free(dev->drw_info, sizeof(*info) *
  169. dev->drw_info_length, DRM_MEM_BUFS);
  170. dev->drw_info = info;
  171. dev->drw_info_length = info_length;
  172. }
  173. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  174. }
  175. DRM_DEBUG("%d\n", draw.handle);
  176. return 0;
  177. }
  178. int drm_update_drawable_info(DRM_IOCTL_ARGS) {
  179. DRM_DEVICE;
  180. drm_update_draw_t update;
  181. unsigned int id, idx, shift;
  182. u32 *bitfield = dev->drw_bitfield;
  183. unsigned long irqflags, bitfield_length = dev->drw_bitfield_length;
  184. drm_drawable_info_t *info;
  185. drm_clip_rect_t *rects;
  186. int err;
  187. DRM_COPY_FROM_USER_IOCTL(update, (drm_update_draw_t __user *) data,
  188. sizeof(update));
  189. id = update.handle - 1;
  190. idx = id / (8 * sizeof(*bitfield));
  191. shift = id % (8 * sizeof(*bitfield));
  192. if (idx < 0 || idx >= bitfield_length ||
  193. !(bitfield[idx] & (1 << shift))) {
  194. DRM_ERROR("No such drawable %d\n", update.handle);
  195. return DRM_ERR(EINVAL);
  196. }
  197. info = dev->drw_info[id];
  198. if (!info) {
  199. info = drm_calloc(1, sizeof(drm_drawable_info_t), DRM_MEM_BUFS);
  200. if (!info) {
  201. DRM_ERROR("Failed to allocate drawable info memory\n");
  202. return DRM_ERR(ENOMEM);
  203. }
  204. }
  205. switch (update.type) {
  206. case DRM_DRAWABLE_CLIPRECTS:
  207. if (update.num != info->num_rects) {
  208. rects = drm_alloc(update.num * sizeof(drm_clip_rect_t),
  209. DRM_MEM_BUFS);
  210. } else
  211. rects = info->rects;
  212. if (update.num && !rects) {
  213. DRM_ERROR("Failed to allocate cliprect memory\n");
  214. err = DRM_ERR(ENOMEM);
  215. goto error;
  216. }
  217. if (update.num && DRM_COPY_FROM_USER(rects,
  218. (drm_clip_rect_t __user *)
  219. (unsigned long)update.data,
  220. update.num *
  221. sizeof(*rects))) {
  222. DRM_ERROR("Failed to copy cliprects from userspace\n");
  223. err = DRM_ERR(EFAULT);
  224. goto error;
  225. }
  226. spin_lock_irqsave(&dev->drw_lock, irqflags);
  227. if (rects != info->rects) {
  228. drm_free(info->rects, info->num_rects *
  229. sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
  230. }
  231. info->rects = rects;
  232. info->num_rects = update.num;
  233. dev->drw_info[id] = info;
  234. spin_unlock_irqrestore(&dev->drw_lock, irqflags);
  235. DRM_DEBUG("Updated %d cliprects for drawable %d\n",
  236. info->num_rects, id);
  237. break;
  238. default:
  239. DRM_ERROR("Invalid update type %d\n", update.type);
  240. return DRM_ERR(EINVAL);
  241. }
  242. return 0;
  243. error:
  244. if (!dev->drw_info[id])
  245. drm_free(info, sizeof(*info), DRM_MEM_BUFS);
  246. else if (rects != dev->drw_info[id]->rects)
  247. drm_free(rects, update.num *
  248. sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
  249. return err;
  250. }
  251. /**
  252. * Caller must hold the drawable spinlock!
  253. */
  254. drm_drawable_info_t *drm_get_drawable_info(drm_device_t *dev, drm_drawable_t id) {
  255. u32 *bitfield = dev->drw_bitfield;
  256. unsigned int idx, shift;
  257. id--;
  258. idx = id / (8 * sizeof(*bitfield));
  259. shift = id % (8 * sizeof(*bitfield));
  260. if (idx < 0 || idx >= dev->drw_bitfield_length ||
  261. !(bitfield[idx] & (1 << shift))) {
  262. DRM_DEBUG("No such drawable %d\n", id);
  263. return NULL;
  264. }
  265. return dev->drw_info[id];
  266. }
  267. EXPORT_SYMBOL(drm_get_drawable_info);