drm_dumb_buffers.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. * Copyright (c) 2008 Red Hat Inc.
  5. * Copyright (c) 2016 Intel Corporation
  6. *
  7. * Permission to use, copy, modify, distribute, and sell this software and its
  8. * documentation for any purpose is hereby granted without fee, provided that
  9. * the above copyright notice appear in all copies and that both that copyright
  10. * notice and this permission notice appear in supporting documentation, and
  11. * that the name of the copyright holders not be used in advertising or
  12. * publicity pertaining to distribution of the software without specific,
  13. * written prior permission. The copyright holders make no representations
  14. * about the suitability of this software for any purpose. It is provided "as
  15. * is" without express or implied warranty.
  16. *
  17. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  18. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  19. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  21. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  22. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. #include <drm/drm_device.h>
  26. #include <drm/drm_drv.h>
  27. #include <drm/drm_dumb_buffers.h>
  28. #include <drm/drm_fourcc.h>
  29. #include <drm/drm_gem.h>
  30. #include <drm/drm_mode.h>
  31. #include <drm/drm_print.h>
  32. #include "drm_crtc_internal.h"
  33. #include "drm_internal.h"
  34. /**
  35. * DOC: overview
  36. *
  37. * The KMS API doesn't standardize backing storage object creation and leaves it
  38. * to driver-specific ioctls. Furthermore actually creating a buffer object even
  39. * for GEM-based drivers is done through a driver-specific ioctl - GEM only has
  40. * a common userspace interface for sharing and destroying objects. While not an
  41. * issue for full-fledged graphics stacks that include device-specific userspace
  42. * components (in libdrm for instance), this limit makes DRM-based early boot
  43. * graphics unnecessarily complex.
  44. *
  45. * Dumb objects partly alleviate the problem by providing a standard API to
  46. * create dumb buffers suitable for scanout, which can then be used to create
  47. * KMS frame buffers.
  48. *
  49. * To support dumb objects drivers must implement the &drm_driver.dumb_create
  50. * and &drm_driver.dumb_map_offset operations (the latter defaults to
  51. * drm_gem_dumb_map_offset() if not set). Drivers that don't use GEM handles
  52. * additionally need to implement the &drm_driver.dumb_destroy operation. See
  53. * the callbacks for further details.
  54. *
  55. * Note that dumb objects may not be used for gpu acceleration, as has been
  56. * attempted on some ARM embedded platforms. Such drivers really must have
  57. * a hardware-specific ioctl to allocate suitable buffer objects.
  58. */
  59. static int drm_mode_align_dumb(struct drm_mode_create_dumb *args,
  60. unsigned long hw_pitch_align,
  61. unsigned long hw_size_align)
  62. {
  63. u32 pitch = args->pitch;
  64. u32 size;
  65. if (!pitch)
  66. return -EINVAL;
  67. if (hw_pitch_align)
  68. pitch = roundup(pitch, hw_pitch_align);
  69. if (!hw_size_align)
  70. hw_size_align = PAGE_SIZE;
  71. else if (!IS_ALIGNED(hw_size_align, PAGE_SIZE))
  72. return -EINVAL; /* TODO: handle this if necessary */
  73. if (check_mul_overflow(args->height, pitch, &size))
  74. return -EINVAL;
  75. size = ALIGN(size, hw_size_align);
  76. if (!size)
  77. return -EINVAL;
  78. args->pitch = pitch;
  79. args->size = size;
  80. return 0;
  81. }
  82. /**
  83. * drm_mode_size_dumb - Calculates the scanline and buffer sizes for dumb buffers
  84. * @dev: DRM device
  85. * @args: Parameters for the dumb buffer
  86. * @hw_pitch_align: Hardware scanline alignment in bytes
  87. * @hw_size_align: Hardware buffer-size alignment in bytes
  88. *
  89. * The helper drm_mode_size_dumb() calculates the size of the buffer
  90. * allocation and the scanline size for a dumb buffer. Callers have to
  91. * set the buffers width, height and color mode in the argument @arg.
  92. * The helper validates the correctness of the input and tests for
  93. * possible overflows. If successful, it returns the dumb buffer's
  94. * required scanline pitch and size in &args.
  95. *
  96. * The parameter @hw_pitch_align allows the driver to specifies an
  97. * alignment for the scanline pitch, if the hardware requires any. The
  98. * calculated pitch will be a multiple of the alignment. The parameter
  99. * @hw_size_align allows to specify an alignment for buffer sizes. The
  100. * provided alignment should represent requirements of the graphics
  101. * hardware. drm_mode_size_dumb() handles GEM-related constraints
  102. * automatically across all drivers and hardware. For example, the
  103. * returned buffer size is always a multiple of PAGE_SIZE, which is
  104. * required by mmap().
  105. *
  106. * Returns:
  107. * Zero on success, or a negative error code otherwise.
  108. */
  109. int drm_mode_size_dumb(struct drm_device *dev,
  110. struct drm_mode_create_dumb *args,
  111. unsigned long hw_pitch_align,
  112. unsigned long hw_size_align)
  113. {
  114. u64 pitch = 0;
  115. u32 fourcc;
  116. /*
  117. * The scanline pitch depends on the buffer width and the color
  118. * format. The latter is specified as a color-mode constant for
  119. * which we first have to find the corresponding color format.
  120. *
  121. * Different color formats can have the same color-mode constant.
  122. * For example XRGB8888 and BGRX8888 both have a color mode of 32.
  123. * It is possible to use different formats for dumb-buffer allocation
  124. * and rendering as long as all involved formats share the same
  125. * color-mode constant.
  126. */
  127. fourcc = drm_driver_color_mode_format(dev, args->bpp);
  128. if (fourcc != DRM_FORMAT_INVALID) {
  129. const struct drm_format_info *info = drm_format_info(fourcc);
  130. if (!info)
  131. return -EINVAL;
  132. pitch = drm_format_info_min_pitch(info, 0, args->width);
  133. } else if (args->bpp) {
  134. /*
  135. * Some userspace throws in arbitrary values for bpp and
  136. * relies on the kernel to figure it out. In this case we
  137. * fall back to the old method of using bpp directly. The
  138. * over-commitment of memory from the rounding is acceptable
  139. * for compatibility with legacy userspace. We have a number
  140. * of deprecated legacy values that are explicitly supported.
  141. */
  142. switch (args->bpp) {
  143. default:
  144. drm_warn_once(dev,
  145. "Unknown color mode %u; guessing buffer size.\n",
  146. args->bpp);
  147. fallthrough;
  148. /*
  149. * These constants represent various YUV formats supported by
  150. * drm_gem_afbc_get_bpp().
  151. */
  152. case 12: // DRM_FORMAT_YUV420_8BIT
  153. case 15: // DRM_FORMAT_YUV420_10BIT
  154. case 30: // DRM_FORMAT_VUY101010
  155. fallthrough;
  156. /*
  157. * Used by Mesa and Gstreamer to allocate NV formats and others
  158. * as RGB buffers. Technically, XRGB16161616F formats are RGB,
  159. * but the dumb buffers are not supposed to be used for anything
  160. * beyond 32 bits per pixels.
  161. */
  162. case 10: // DRM_FORMAT_NV{15,20,30}, DRM_FORMAT_P010
  163. case 64: // DRM_FORMAT_{XRGB,XBGR,ARGB,ABGR}16161616F
  164. pitch = args->width * DIV_ROUND_UP(args->bpp, SZ_8);
  165. break;
  166. }
  167. }
  168. if (!pitch || pitch > U32_MAX)
  169. return -EINVAL;
  170. args->pitch = pitch;
  171. return drm_mode_align_dumb(args, hw_pitch_align, hw_size_align);
  172. }
  173. EXPORT_SYMBOL(drm_mode_size_dumb);
  174. int drm_mode_create_dumb(struct drm_device *dev,
  175. struct drm_mode_create_dumb *args,
  176. struct drm_file *file_priv)
  177. {
  178. u32 cpp, stride, size;
  179. if (!dev->driver->dumb_create)
  180. return -ENOSYS;
  181. if (!args->width || !args->height || !args->bpp)
  182. return -EINVAL;
  183. /* overflow checks for 32bit size calculations */
  184. if (args->bpp > U32_MAX - 8)
  185. return -EINVAL;
  186. cpp = DIV_ROUND_UP(args->bpp, 8);
  187. if (cpp > U32_MAX / args->width)
  188. return -EINVAL;
  189. stride = cpp * args->width;
  190. if (args->height > U32_MAX / stride)
  191. return -EINVAL;
  192. /* test for wrap-around */
  193. size = args->height * stride;
  194. if (PAGE_ALIGN(size) == 0)
  195. return -EINVAL;
  196. /*
  197. * handle, pitch and size are output parameters. Zero them out to
  198. * prevent drivers from accidentally using uninitialized data. Since
  199. * not all existing userspace is clearing these fields properly we
  200. * cannot reject IOCTL with garbage in them.
  201. */
  202. args->handle = 0;
  203. args->pitch = 0;
  204. args->size = 0;
  205. return dev->driver->dumb_create(file_priv, dev, args);
  206. }
  207. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  208. void *data, struct drm_file *file_priv)
  209. {
  210. struct drm_mode_create_dumb *args = data;
  211. int err;
  212. err = drm_mode_create_dumb(dev, args, file_priv);
  213. if (err) {
  214. args->handle = 0;
  215. args->pitch = 0;
  216. args->size = 0;
  217. }
  218. return err;
  219. }
  220. static int drm_mode_mmap_dumb(struct drm_device *dev, struct drm_mode_map_dumb *args,
  221. struct drm_file *file_priv)
  222. {
  223. if (!dev->driver->dumb_create)
  224. return -ENOSYS;
  225. if (dev->driver->dumb_map_offset)
  226. return dev->driver->dumb_map_offset(file_priv, dev, args->handle,
  227. &args->offset);
  228. else
  229. return drm_gem_dumb_map_offset(file_priv, dev, args->handle,
  230. &args->offset);
  231. }
  232. /**
  233. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  234. * @dev: DRM device
  235. * @data: ioctl data
  236. * @file_priv: DRM file info
  237. *
  238. * Allocate an offset in the drm device node's address space to be able to
  239. * memory map a dumb buffer.
  240. *
  241. * Called by the user via ioctl.
  242. *
  243. * Returns:
  244. * Zero on success, negative errno on failure.
  245. */
  246. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  247. void *data, struct drm_file *file_priv)
  248. {
  249. struct drm_mode_map_dumb *args = data;
  250. int err;
  251. err = drm_mode_mmap_dumb(dev, args, file_priv);
  252. if (err)
  253. args->offset = 0;
  254. return err;
  255. }
  256. int drm_mode_destroy_dumb(struct drm_device *dev, u32 handle,
  257. struct drm_file *file_priv)
  258. {
  259. if (!dev->driver->dumb_create)
  260. return -ENOSYS;
  261. return drm_gem_handle_delete(file_priv, handle);
  262. }
  263. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  264. void *data, struct drm_file *file_priv)
  265. {
  266. struct drm_mode_destroy_dumb *args = data;
  267. return drm_mode_destroy_dumb(dev, args->handle, file_priv);
  268. }