rockchip_drm_fb.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_atomic.h>
  18. #include <drm/drm_fb_helper.h>
  19. #include <drm/drm_crtc_helper.h>
  20. #include "rockchip_drm_drv.h"
  21. #include "rockchip_drm_gem.h"
  22. #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
  23. struct rockchip_drm_fb {
  24. struct drm_framebuffer fb;
  25. struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
  26. };
  27. struct drm_gem_object *rockchip_fb_get_gem_obj(struct drm_framebuffer *fb,
  28. unsigned int plane)
  29. {
  30. struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
  31. if (plane >= ROCKCHIP_MAX_FB_BUFFER)
  32. return NULL;
  33. return rk_fb->obj[plane];
  34. }
  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 const 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, const 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. const 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(file_priv, mode_cmd->handles[i]);
  104. if (!obj) {
  105. dev_err(dev->dev, "Failed to lookup GEM object\n");
  106. ret = -ENXIO;
  107. goto err_gem_object_unreference;
  108. }
  109. min_size = (height - 1) * mode_cmd->pitches[i] +
  110. mode_cmd->offsets[i] +
  111. width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
  112. if (obj->size < min_size) {
  113. drm_gem_object_unreference_unlocked(obj);
  114. ret = -EINVAL;
  115. goto err_gem_object_unreference;
  116. }
  117. objs[i] = obj;
  118. }
  119. rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, objs, i);
  120. if (IS_ERR(rockchip_fb)) {
  121. ret = PTR_ERR(rockchip_fb);
  122. goto err_gem_object_unreference;
  123. }
  124. return &rockchip_fb->fb;
  125. err_gem_object_unreference:
  126. for (i--; i >= 0; i--)
  127. drm_gem_object_unreference_unlocked(objs[i]);
  128. return ERR_PTR(ret);
  129. }
  130. static void rockchip_drm_output_poll_changed(struct drm_device *dev)
  131. {
  132. struct rockchip_drm_private *private = dev->dev_private;
  133. struct drm_fb_helper *fb_helper = &private->fbdev_helper;
  134. if (fb_helper)
  135. drm_fb_helper_hotplug_event(fb_helper);
  136. }
  137. static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
  138. {
  139. struct rockchip_drm_private *priv = crtc->dev->dev_private;
  140. int pipe = drm_crtc_index(crtc);
  141. const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
  142. if (crtc_funcs && crtc_funcs->wait_for_update)
  143. crtc_funcs->wait_for_update(crtc);
  144. }
  145. /*
  146. * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
  147. * have hardware counters for neither vblanks nor scanlines, which results in
  148. * a race where:
  149. * | <-- HW vsync irq and reg take effect
  150. * plane_commit --> |
  151. * get_vblank and wait --> |
  152. * | <-- handle_vblank, vblank->count + 1
  153. * cleanup_fb --> |
  154. * iommu crash --> |
  155. * | <-- HW vsync irq and reg take effect
  156. *
  157. * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
  158. * of waiting for vblank_count to change.
  159. */
  160. static void
  161. rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
  162. {
  163. struct drm_crtc_state *old_crtc_state;
  164. struct drm_crtc *crtc;
  165. int i, ret;
  166. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  167. /* No one cares about the old state, so abuse it for tracking
  168. * and store whether we hold a vblank reference (and should do a
  169. * vblank wait) in the ->enable boolean.
  170. */
  171. old_crtc_state->enable = false;
  172. if (!crtc->state->active)
  173. continue;
  174. if (!drm_atomic_helper_framebuffer_changed(dev,
  175. old_state, crtc))
  176. continue;
  177. ret = drm_crtc_vblank_get(crtc);
  178. if (ret != 0)
  179. continue;
  180. old_crtc_state->enable = true;
  181. }
  182. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  183. if (!old_crtc_state->enable)
  184. continue;
  185. rockchip_crtc_wait_for_update(crtc);
  186. drm_crtc_vblank_put(crtc);
  187. }
  188. }
  189. static void
  190. rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
  191. {
  192. struct drm_atomic_state *state = commit->state;
  193. struct drm_device *dev = commit->dev;
  194. /*
  195. * TODO: do fence wait here.
  196. */
  197. /*
  198. * Rockchip crtc support runtime PM, can't update display planes
  199. * when crtc is disabled.
  200. *
  201. * drm_atomic_helper_commit comments detail that:
  202. * For drivers supporting runtime PM the recommended sequence is
  203. *
  204. * drm_atomic_helper_commit_modeset_disables(dev, state);
  205. *
  206. * drm_atomic_helper_commit_modeset_enables(dev, state);
  207. *
  208. * drm_atomic_helper_commit_planes(dev, state, true);
  209. *
  210. * See the kerneldoc entries for these three functions for more details.
  211. */
  212. drm_atomic_helper_commit_modeset_disables(dev, state);
  213. drm_atomic_helper_commit_modeset_enables(dev, state);
  214. drm_atomic_helper_commit_planes(dev, state, true);
  215. rockchip_atomic_wait_for_complete(dev, state);
  216. drm_atomic_helper_cleanup_planes(dev, state);
  217. drm_atomic_state_free(state);
  218. }
  219. void rockchip_drm_atomic_work(struct work_struct *work)
  220. {
  221. struct rockchip_atomic_commit *commit = container_of(work,
  222. struct rockchip_atomic_commit, work);
  223. rockchip_atomic_commit_complete(commit);
  224. }
  225. int rockchip_drm_atomic_commit(struct drm_device *dev,
  226. struct drm_atomic_state *state,
  227. bool nonblock)
  228. {
  229. struct rockchip_drm_private *private = dev->dev_private;
  230. struct rockchip_atomic_commit *commit = &private->commit;
  231. int ret;
  232. ret = drm_atomic_helper_prepare_planes(dev, state);
  233. if (ret)
  234. return ret;
  235. /* serialize outstanding nonblocking commits */
  236. mutex_lock(&commit->lock);
  237. flush_work(&commit->work);
  238. drm_atomic_helper_swap_state(dev, state);
  239. commit->dev = dev;
  240. commit->state = state;
  241. if (nonblock)
  242. schedule_work(&commit->work);
  243. else
  244. rockchip_atomic_commit_complete(commit);
  245. mutex_unlock(&commit->lock);
  246. return 0;
  247. }
  248. static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
  249. .fb_create = rockchip_user_fb_create,
  250. .output_poll_changed = rockchip_drm_output_poll_changed,
  251. .atomic_check = drm_atomic_helper_check,
  252. .atomic_commit = rockchip_drm_atomic_commit,
  253. };
  254. struct drm_framebuffer *
  255. rockchip_drm_framebuffer_init(struct drm_device *dev,
  256. const struct drm_mode_fb_cmd2 *mode_cmd,
  257. struct drm_gem_object *obj)
  258. {
  259. struct rockchip_drm_fb *rockchip_fb;
  260. rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1);
  261. if (IS_ERR(rockchip_fb))
  262. return NULL;
  263. return &rockchip_fb->fb;
  264. }
  265. void rockchip_drm_mode_config_init(struct drm_device *dev)
  266. {
  267. dev->mode_config.min_width = 0;
  268. dev->mode_config.min_height = 0;
  269. /*
  270. * set max width and height as default value(4096x4096).
  271. * this value would be used to check framebuffer size limitation
  272. * at drm_mode_addfb().
  273. */
  274. dev->mode_config.max_width = 4096;
  275. dev->mode_config.max_height = 4096;
  276. dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
  277. }