drm_buffer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**************************************************************************
  2. *
  3. * Copyright 2010 Pauli Nieminen.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. *
  27. **************************************************************************/
  28. /*
  29. * Multipart buffer for coping data which is larger than the page size.
  30. *
  31. * Authors:
  32. * Pauli Nieminen <suokkos-at-gmail-dot-com>
  33. */
  34. #include <linux/export.h>
  35. #include "drm_buffer.h"
  36. /**
  37. * Allocate the drm buffer object.
  38. *
  39. * buf: Pointer to a pointer where the object is stored.
  40. * size: The number of bytes to allocate.
  41. */
  42. int drm_buffer_alloc(struct drm_buffer **buf, int size)
  43. {
  44. int nr_pages = size / PAGE_SIZE + 1;
  45. int idx;
  46. /* Allocating pointer table to end of structure makes drm_buffer
  47. * variable sized */
  48. *buf = kzalloc(sizeof(struct drm_buffer) + nr_pages*sizeof(char *),
  49. GFP_KERNEL);
  50. if (*buf == NULL) {
  51. DRM_ERROR("Failed to allocate drm buffer object to hold"
  52. " %d bytes in %d pages.\n",
  53. size, nr_pages);
  54. return -ENOMEM;
  55. }
  56. (*buf)->size = size;
  57. for (idx = 0; idx < nr_pages; ++idx) {
  58. (*buf)->data[idx] =
  59. kmalloc(min(PAGE_SIZE, size - idx * PAGE_SIZE),
  60. GFP_KERNEL);
  61. if ((*buf)->data[idx] == NULL) {
  62. DRM_ERROR("Failed to allocate %dth page for drm"
  63. " buffer with %d bytes and %d pages.\n",
  64. idx + 1, size, nr_pages);
  65. goto error_out;
  66. }
  67. }
  68. return 0;
  69. error_out:
  70. /* Only last element can be null pointer so check for it first. */
  71. if ((*buf)->data[idx])
  72. kfree((*buf)->data[idx]);
  73. for (--idx; idx >= 0; --idx)
  74. kfree((*buf)->data[idx]);
  75. kfree(*buf);
  76. return -ENOMEM;
  77. }
  78. EXPORT_SYMBOL(drm_buffer_alloc);
  79. /**
  80. * Copy the user data to the begin of the buffer and reset the processing
  81. * iterator.
  82. *
  83. * user_data: A pointer the data that is copied to the buffer.
  84. * size: The Number of bytes to copy.
  85. */
  86. int drm_buffer_copy_from_user(struct drm_buffer *buf,
  87. void __user *user_data, int size)
  88. {
  89. int nr_pages = size / PAGE_SIZE + 1;
  90. int idx;
  91. if (size > buf->size) {
  92. DRM_ERROR("Requesting to copy %d bytes to a drm buffer with"
  93. " %d bytes space\n",
  94. size, buf->size);
  95. return -EFAULT;
  96. }
  97. for (idx = 0; idx < nr_pages; ++idx) {
  98. if (DRM_COPY_FROM_USER(buf->data[idx],
  99. user_data + idx * PAGE_SIZE,
  100. min(PAGE_SIZE, size - idx * PAGE_SIZE))) {
  101. DRM_ERROR("Failed to copy user data (%p) to drm buffer"
  102. " (%p) %dth page.\n",
  103. user_data, buf, idx);
  104. return -EFAULT;
  105. }
  106. }
  107. buf->iterator = 0;
  108. return 0;
  109. }
  110. EXPORT_SYMBOL(drm_buffer_copy_from_user);
  111. /**
  112. * Free the drm buffer object
  113. */
  114. void drm_buffer_free(struct drm_buffer *buf)
  115. {
  116. if (buf != NULL) {
  117. int nr_pages = buf->size / PAGE_SIZE + 1;
  118. int idx;
  119. for (idx = 0; idx < nr_pages; ++idx)
  120. kfree(buf->data[idx]);
  121. kfree(buf);
  122. }
  123. }
  124. EXPORT_SYMBOL(drm_buffer_free);
  125. /**
  126. * Read an object from buffer that may be split to multiple parts. If object
  127. * is not split function just returns the pointer to object in buffer. But in
  128. * case of split object data is copied to given stack object that is suplied
  129. * by caller.
  130. *
  131. * The processing location of the buffer is also advanced to the next byte
  132. * after the object.
  133. *
  134. * objsize: The size of the objet in bytes.
  135. * stack_obj: A pointer to a memory location where object can be copied.
  136. */
  137. void *drm_buffer_read_object(struct drm_buffer *buf,
  138. int objsize, void *stack_obj)
  139. {
  140. int idx = drm_buffer_index(buf);
  141. int page = drm_buffer_page(buf);
  142. void *obj = NULL;
  143. if (idx + objsize <= PAGE_SIZE) {
  144. obj = &buf->data[page][idx];
  145. } else {
  146. /* The object is split which forces copy to temporary object.*/
  147. int beginsz = PAGE_SIZE - idx;
  148. memcpy(stack_obj, &buf->data[page][idx], beginsz);
  149. memcpy(stack_obj + beginsz, &buf->data[page + 1][0],
  150. objsize - beginsz);
  151. obj = stack_obj;
  152. }
  153. drm_buffer_advance(buf, objsize);
  154. return obj;
  155. }
  156. EXPORT_SYMBOL(drm_buffer_read_object);