rockchip_drm_fb.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  3. * Author:Mark Yao <mark.yao@rock-chips.com>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <drm/drm.h>
  16. #include <drm/drmP.h>
  17. #include <drm/drm_fb_helper.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include "rockchip_drm_drv.h"
  20. #include "rockchip_drm_gem.h"
  21. #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
  22. struct rockchip_drm_fb {
  23. struct drm_framebuffer fb;
  24. struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
  25. };
  26. struct drm_gem_object *rockchip_fb_get_gem_obj(struct drm_framebuffer *fb,
  27. unsigned int plane)
  28. {
  29. struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
  30. if (plane >= ROCKCHIP_MAX_FB_BUFFER)
  31. return NULL;
  32. return rk_fb->obj[plane];
  33. }
  34. EXPORT_SYMBOL_GPL(rockchip_fb_get_gem_obj);
  35. static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
  36. {
  37. struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
  38. struct drm_gem_object *obj;
  39. int i;
  40. for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
  41. obj = rockchip_fb->obj[i];
  42. if (obj)
  43. drm_gem_object_unreference_unlocked(obj);
  44. }
  45. drm_framebuffer_cleanup(fb);
  46. kfree(rockchip_fb);
  47. }
  48. static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
  49. struct drm_file *file_priv,
  50. unsigned int *handle)
  51. {
  52. struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
  53. return drm_gem_handle_create(file_priv,
  54. rockchip_fb->obj[0], handle);
  55. }
  56. static struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
  57. .destroy = rockchip_drm_fb_destroy,
  58. .create_handle = rockchip_drm_fb_create_handle,
  59. };
  60. static struct rockchip_drm_fb *
  61. rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
  62. struct drm_gem_object **obj, unsigned int num_planes)
  63. {
  64. struct rockchip_drm_fb *rockchip_fb;
  65. int ret;
  66. int i;
  67. rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
  68. if (!rockchip_fb)
  69. return ERR_PTR(-ENOMEM);
  70. drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
  71. for (i = 0; i < num_planes; i++)
  72. rockchip_fb->obj[i] = obj[i];
  73. ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
  74. &rockchip_drm_fb_funcs);
  75. if (ret) {
  76. dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
  77. ret);
  78. kfree(rockchip_fb);
  79. return ERR_PTR(ret);
  80. }
  81. return rockchip_fb;
  82. }
  83. static struct drm_framebuffer *
  84. rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  85. struct drm_mode_fb_cmd2 *mode_cmd)
  86. {
  87. struct rockchip_drm_fb *rockchip_fb;
  88. struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
  89. struct drm_gem_object *obj;
  90. unsigned int hsub;
  91. unsigned int vsub;
  92. int num_planes;
  93. int ret;
  94. int i;
  95. hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
  96. vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
  97. num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
  98. ROCKCHIP_MAX_FB_BUFFER);
  99. for (i = 0; i < num_planes; i++) {
  100. unsigned int width = mode_cmd->width / (i ? hsub : 1);
  101. unsigned int height = mode_cmd->height / (i ? vsub : 1);
  102. unsigned int min_size;
  103. obj = drm_gem_object_lookup(dev, file_priv,
  104. mode_cmd->handles[i]);
  105. if (!obj) {
  106. dev_err(dev->dev, "Failed to lookup GEM object\n");
  107. ret = -ENXIO;
  108. goto err_gem_object_unreference;
  109. }
  110. min_size = (height - 1) * mode_cmd->pitches[i] +
  111. mode_cmd->offsets[i] +
  112. width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
  113. if (obj->size < min_size) {
  114. drm_gem_object_unreference_unlocked(obj);
  115. ret = -EINVAL;
  116. goto err_gem_object_unreference;
  117. }
  118. objs[i] = obj;
  119. }
  120. rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, objs, i);
  121. if (IS_ERR(rockchip_fb)) {
  122. ret = PTR_ERR(rockchip_fb);
  123. goto err_gem_object_unreference;
  124. }
  125. return &rockchip_fb->fb;
  126. err_gem_object_unreference:
  127. for (i--; i >= 0; i--)
  128. drm_gem_object_unreference_unlocked(objs[i]);
  129. return ERR_PTR(ret);
  130. }
  131. static void rockchip_drm_output_poll_changed(struct drm_device *dev)
  132. {
  133. struct rockchip_drm_private *private = dev->dev_private;
  134. struct drm_fb_helper *fb_helper = &private->fbdev_helper;
  135. if (fb_helper)
  136. drm_fb_helper_hotplug_event(fb_helper);
  137. }
  138. static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
  139. .fb_create = rockchip_user_fb_create,
  140. .output_poll_changed = rockchip_drm_output_poll_changed,
  141. };
  142. struct drm_framebuffer *
  143. rockchip_drm_framebuffer_init(struct drm_device *dev,
  144. struct drm_mode_fb_cmd2 *mode_cmd,
  145. struct drm_gem_object *obj)
  146. {
  147. struct rockchip_drm_fb *rockchip_fb;
  148. rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1);
  149. if (IS_ERR(rockchip_fb))
  150. return NULL;
  151. return &rockchip_fb->fb;
  152. }
  153. void rockchip_drm_mode_config_init(struct drm_device *dev)
  154. {
  155. dev->mode_config.min_width = 0;
  156. dev->mode_config.min_height = 0;
  157. /*
  158. * set max width and height as default value(4096x4096).
  159. * this value would be used to check framebuffer size limitation
  160. * at drm_mode_addfb().
  161. */
  162. dev->mode_config.max_width = 4096;
  163. dev->mode_config.max_height = 4096;
  164. dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
  165. }