drm_drawable.c 8.7 KB

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