drm_dumb_buffers.c 4.4 KB

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