drm_dumb_buffers.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_gem.h>
  28. #include <drm/drm_mode.h>
  29. #include "drm_crtc_internal.h"
  30. /**
  31. * DOC: overview
  32. *
  33. * The KMS API doesn't standardize backing storage object creation and leaves it
  34. * to driver-specific ioctls. Furthermore actually creating a buffer object even
  35. * for GEM-based drivers is done through a driver-specific ioctl - GEM only has
  36. * a common userspace interface for sharing and destroying objects. While not an
  37. * issue for full-fledged graphics stacks that include device-specific userspace
  38. * components (in libdrm for instance), this limit makes DRM-based early boot
  39. * graphics unnecessarily complex.
  40. *
  41. * Dumb objects partly alleviate the problem by providing a standard API to
  42. * create dumb buffers suitable for scanout, which can then be used to create
  43. * KMS frame buffers.
  44. *
  45. * To support dumb objects drivers must implement the &drm_driver.dumb_create
  46. * operation. &drm_driver.dumb_destroy defaults to drm_gem_dumb_destroy() if
  47. * not set and &drm_driver.dumb_map_offset defaults to
  48. * drm_gem_dumb_map_offset(). See the callbacks for further details.
  49. *
  50. * Note that dumb objects may not be used for gpu acceleration, as has been
  51. * attempted on some ARM embedded platforms. Such drivers really must have
  52. * a hardware-specific ioctl to allocate suitable buffer objects.
  53. */
  54. int drm_mode_create_dumb(struct drm_device *dev,
  55. struct drm_mode_create_dumb *args,
  56. struct drm_file *file_priv)
  57. {
  58. u32 cpp, stride, size;
  59. if (!dev->driver->dumb_create)
  60. return -ENOSYS;
  61. if (!args->width || !args->height || !args->bpp)
  62. return -EINVAL;
  63. /* overflow checks for 32bit size calculations */
  64. if (args->bpp > U32_MAX - 8)
  65. return -EINVAL;
  66. cpp = DIV_ROUND_UP(args->bpp, 8);
  67. if (cpp > U32_MAX / args->width)
  68. return -EINVAL;
  69. stride = cpp * args->width;
  70. if (args->height > U32_MAX / stride)
  71. return -EINVAL;
  72. /* test for wrap-around */
  73. size = args->height * stride;
  74. if (PAGE_ALIGN(size) == 0)
  75. return -EINVAL;
  76. /*
  77. * handle, pitch and size are output parameters. Zero them out to
  78. * prevent drivers from accidentally using uninitialized data. Since
  79. * not all existing userspace is clearing these fields properly we
  80. * cannot reject IOCTL with garbage in them.
  81. */
  82. args->handle = 0;
  83. args->pitch = 0;
  84. args->size = 0;
  85. return dev->driver->dumb_create(file_priv, dev, args);
  86. }
  87. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  88. void *data, struct drm_file *file_priv)
  89. {
  90. return drm_mode_create_dumb(dev, data, file_priv);
  91. }
  92. /**
  93. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  94. * @dev: DRM device
  95. * @data: ioctl data
  96. * @file_priv: DRM file info
  97. *
  98. * Allocate an offset in the drm device node's address space to be able to
  99. * memory map a dumb buffer.
  100. *
  101. * Called by the user via ioctl.
  102. *
  103. * Returns:
  104. * Zero on success, negative errno on failure.
  105. */
  106. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  107. void *data, struct drm_file *file_priv)
  108. {
  109. struct drm_mode_map_dumb *args = data;
  110. if (!dev->driver->dumb_create)
  111. return -ENOSYS;
  112. if (dev->driver->dumb_map_offset)
  113. return dev->driver->dumb_map_offset(file_priv, dev,
  114. args->handle,
  115. &args->offset);
  116. else
  117. return drm_gem_dumb_map_offset(file_priv, dev, args->handle,
  118. &args->offset);
  119. }
  120. int drm_mode_destroy_dumb(struct drm_device *dev, u32 handle,
  121. struct drm_file *file_priv)
  122. {
  123. if (!dev->driver->dumb_create)
  124. return -ENOSYS;
  125. if (dev->driver->dumb_destroy)
  126. return dev->driver->dumb_destroy(file_priv, dev, handle);
  127. else
  128. return drm_gem_dumb_destroy(file_priv, dev, handle);
  129. }
  130. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  131. void *data, struct drm_file *file_priv)
  132. {
  133. struct drm_mode_destroy_dumb *args = data;
  134. return drm_mode_destroy_dumb(dev, args->handle, file_priv);
  135. }