drm_fb_cma_helper.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * drm kms/fb cma (contiguous memory allocator) helper functions
  3. *
  4. * Copyright (C) 2012 Analog Device Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Based on udl_fbdev.c
  8. * Copyright (C) 2012 Red Hat
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <drm/drmP.h>
  20. #include <drm/drm_crtc.h>
  21. #include <drm/drm_fb_helper.h>
  22. #include <drm/drm_crtc_helper.h>
  23. #include <drm/drm_gem_cma_helper.h>
  24. #include <drm/drm_fb_cma_helper.h>
  25. #include <linux/module.h>
  26. struct drm_fb_cma {
  27. struct drm_framebuffer fb;
  28. struct drm_gem_cma_object *obj[4];
  29. };
  30. struct drm_fbdev_cma {
  31. struct drm_fb_helper fb_helper;
  32. struct drm_fb_cma *fb;
  33. };
  34. static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
  35. {
  36. return container_of(helper, struct drm_fbdev_cma, fb_helper);
  37. }
  38. static inline struct drm_fb_cma *to_fb_cma(struct drm_framebuffer *fb)
  39. {
  40. return container_of(fb, struct drm_fb_cma, fb);
  41. }
  42. static void drm_fb_cma_destroy(struct drm_framebuffer *fb)
  43. {
  44. struct drm_fb_cma *fb_cma = to_fb_cma(fb);
  45. int i;
  46. for (i = 0; i < 4; i++) {
  47. if (fb_cma->obj[i])
  48. drm_gem_object_unreference_unlocked(&fb_cma->obj[i]->base);
  49. }
  50. drm_framebuffer_cleanup(fb);
  51. kfree(fb_cma);
  52. }
  53. static int drm_fb_cma_create_handle(struct drm_framebuffer *fb,
  54. struct drm_file *file_priv, unsigned int *handle)
  55. {
  56. struct drm_fb_cma *fb_cma = to_fb_cma(fb);
  57. return drm_gem_handle_create(file_priv,
  58. &fb_cma->obj[0]->base, handle);
  59. }
  60. static struct drm_framebuffer_funcs drm_fb_cma_funcs = {
  61. .destroy = drm_fb_cma_destroy,
  62. .create_handle = drm_fb_cma_create_handle,
  63. };
  64. static struct drm_fb_cma *drm_fb_cma_alloc(struct drm_device *dev,
  65. struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_cma_object **obj,
  66. unsigned int num_planes)
  67. {
  68. struct drm_fb_cma *fb_cma;
  69. int ret;
  70. int i;
  71. fb_cma = kzalloc(sizeof(*fb_cma), GFP_KERNEL);
  72. if (!fb_cma)
  73. return ERR_PTR(-ENOMEM);
  74. drm_helper_mode_fill_fb_struct(&fb_cma->fb, mode_cmd);
  75. for (i = 0; i < num_planes; i++)
  76. fb_cma->obj[i] = obj[i];
  77. ret = drm_framebuffer_init(dev, &fb_cma->fb, &drm_fb_cma_funcs);
  78. if (ret) {
  79. dev_err(dev->dev, "Failed to initialize framebuffer: %d\n", ret);
  80. kfree(fb_cma);
  81. return ERR_PTR(ret);
  82. }
  83. return fb_cma;
  84. }
  85. /**
  86. * drm_fb_cma_create() - (struct drm_mode_config_funcs *)->fb_create callback function
  87. *
  88. * If your hardware has special alignment or pitch requirements these should be
  89. * checked before calling this function.
  90. */
  91. struct drm_framebuffer *drm_fb_cma_create(struct drm_device *dev,
  92. struct drm_file *file_priv, struct drm_mode_fb_cmd2 *mode_cmd)
  93. {
  94. struct drm_fb_cma *fb_cma;
  95. struct drm_gem_cma_object *objs[4];
  96. struct drm_gem_object *obj;
  97. unsigned int hsub;
  98. unsigned int vsub;
  99. int ret;
  100. int i;
  101. hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
  102. vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
  103. for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) {
  104. unsigned int width = mode_cmd->width / (i ? hsub : 1);
  105. unsigned int height = mode_cmd->height / (i ? vsub : 1);
  106. unsigned int min_size;
  107. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[i]);
  108. if (!obj) {
  109. dev_err(dev->dev, "Failed to lookup GEM object\n");
  110. ret = -ENXIO;
  111. goto err_gem_object_unreference;
  112. }
  113. min_size = (height - 1) * mode_cmd->pitches[i]
  114. + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
  115. + mode_cmd->offsets[i];
  116. if (obj->size < min_size) {
  117. drm_gem_object_unreference_unlocked(obj);
  118. ret = -EINVAL;
  119. goto err_gem_object_unreference;
  120. }
  121. objs[i] = to_drm_gem_cma_obj(obj);
  122. }
  123. fb_cma = drm_fb_cma_alloc(dev, mode_cmd, objs, i);
  124. if (IS_ERR(fb_cma)) {
  125. ret = PTR_ERR(fb_cma);
  126. goto err_gem_object_unreference;
  127. }
  128. return &fb_cma->fb;
  129. err_gem_object_unreference:
  130. for (i--; i >= 0; i--)
  131. drm_gem_object_unreference_unlocked(&objs[i]->base);
  132. return ERR_PTR(ret);
  133. }
  134. EXPORT_SYMBOL_GPL(drm_fb_cma_create);
  135. /**
  136. * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
  137. * @fb: The framebuffer
  138. * @plane: Which plane
  139. *
  140. * Return the CMA GEM object for given framebuffer.
  141. *
  142. * This function will usually be called from the CRTC callback functions.
  143. */
  144. struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
  145. unsigned int plane)
  146. {
  147. struct drm_fb_cma *fb_cma = to_fb_cma(fb);
  148. if (plane >= 4)
  149. return NULL;
  150. return fb_cma->obj[plane];
  151. }
  152. EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
  153. #ifdef CONFIG_DEBUG_FS
  154. /*
  155. * drm_fb_cma_describe() - Helper to dump information about a single
  156. * CMA framebuffer object
  157. */
  158. static void drm_fb_cma_describe(struct drm_framebuffer *fb, struct seq_file *m)
  159. {
  160. struct drm_fb_cma *fb_cma = to_fb_cma(fb);
  161. int i, n = drm_format_num_planes(fb->pixel_format);
  162. seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
  163. (char *)&fb->pixel_format);
  164. for (i = 0; i < n; i++) {
  165. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  166. i, fb->offsets[i], fb->pitches[i]);
  167. drm_gem_cma_describe(fb_cma->obj[i], m);
  168. }
  169. }
  170. /**
  171. * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects
  172. * in debugfs.
  173. */
  174. int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg)
  175. {
  176. struct drm_info_node *node = (struct drm_info_node *) m->private;
  177. struct drm_device *dev = node->minor->dev;
  178. struct drm_framebuffer *fb;
  179. mutex_lock(&dev->mode_config.fb_lock);
  180. drm_for_each_fb(fb, dev)
  181. drm_fb_cma_describe(fb, m);
  182. mutex_unlock(&dev->mode_config.fb_lock);
  183. return 0;
  184. }
  185. EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show);
  186. #endif
  187. static struct fb_ops drm_fbdev_cma_ops = {
  188. .owner = THIS_MODULE,
  189. .fb_fillrect = drm_fb_helper_sys_fillrect,
  190. .fb_copyarea = drm_fb_helper_sys_copyarea,
  191. .fb_imageblit = drm_fb_helper_sys_imageblit,
  192. .fb_check_var = drm_fb_helper_check_var,
  193. .fb_set_par = drm_fb_helper_set_par,
  194. .fb_blank = drm_fb_helper_blank,
  195. .fb_pan_display = drm_fb_helper_pan_display,
  196. .fb_setcmap = drm_fb_helper_setcmap,
  197. };
  198. static int drm_fbdev_cma_create(struct drm_fb_helper *helper,
  199. struct drm_fb_helper_surface_size *sizes)
  200. {
  201. struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
  202. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  203. struct drm_device *dev = helper->dev;
  204. struct drm_gem_cma_object *obj;
  205. struct drm_framebuffer *fb;
  206. unsigned int bytes_per_pixel;
  207. unsigned long offset;
  208. struct fb_info *fbi;
  209. size_t size;
  210. int ret;
  211. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
  212. sizes->surface_width, sizes->surface_height,
  213. sizes->surface_bpp);
  214. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  215. mode_cmd.width = sizes->surface_width;
  216. mode_cmd.height = sizes->surface_height;
  217. mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
  218. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  219. sizes->surface_depth);
  220. size = mode_cmd.pitches[0] * mode_cmd.height;
  221. obj = drm_gem_cma_create(dev, size);
  222. if (IS_ERR(obj))
  223. return -ENOMEM;
  224. fbi = drm_fb_helper_alloc_fbi(helper);
  225. if (IS_ERR(fbi)) {
  226. ret = PTR_ERR(fbi);
  227. goto err_drm_gem_cma_free_object;
  228. }
  229. fbdev_cma->fb = drm_fb_cma_alloc(dev, &mode_cmd, &obj, 1);
  230. if (IS_ERR(fbdev_cma->fb)) {
  231. dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
  232. ret = PTR_ERR(fbdev_cma->fb);
  233. goto err_fb_info_destroy;
  234. }
  235. fb = &fbdev_cma->fb->fb;
  236. helper->fb = fb;
  237. fbi->par = helper;
  238. fbi->flags = FBINFO_FLAG_DEFAULT;
  239. fbi->fbops = &drm_fbdev_cma_ops;
  240. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  241. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  242. offset = fbi->var.xoffset * bytes_per_pixel;
  243. offset += fbi->var.yoffset * fb->pitches[0];
  244. dev->mode_config.fb_base = (resource_size_t)obj->paddr;
  245. fbi->screen_base = obj->vaddr + offset;
  246. fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
  247. fbi->screen_size = size;
  248. fbi->fix.smem_len = size;
  249. return 0;
  250. err_fb_info_destroy:
  251. drm_fb_helper_release_fbi(helper);
  252. err_drm_gem_cma_free_object:
  253. drm_gem_cma_free_object(&obj->base);
  254. return ret;
  255. }
  256. static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
  257. .fb_probe = drm_fbdev_cma_create,
  258. };
  259. /**
  260. * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
  261. * @dev: DRM device
  262. * @preferred_bpp: Preferred bits per pixel for the device
  263. * @num_crtc: Number of CRTCs
  264. * @max_conn_count: Maximum number of connectors
  265. *
  266. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  267. */
  268. struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
  269. unsigned int preferred_bpp, unsigned int num_crtc,
  270. unsigned int max_conn_count)
  271. {
  272. struct drm_fbdev_cma *fbdev_cma;
  273. struct drm_fb_helper *helper;
  274. int ret;
  275. fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
  276. if (!fbdev_cma) {
  277. dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
  278. return ERR_PTR(-ENOMEM);
  279. }
  280. helper = &fbdev_cma->fb_helper;
  281. drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
  282. ret = drm_fb_helper_init(dev, helper, num_crtc, max_conn_count);
  283. if (ret < 0) {
  284. dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
  285. goto err_free;
  286. }
  287. ret = drm_fb_helper_single_add_all_connectors(helper);
  288. if (ret < 0) {
  289. dev_err(dev->dev, "Failed to add connectors.\n");
  290. goto err_drm_fb_helper_fini;
  291. }
  292. /* disable all the possible outputs/crtcs before entering KMS mode */
  293. drm_helper_disable_unused_functions(dev);
  294. ret = drm_fb_helper_initial_config(helper, preferred_bpp);
  295. if (ret < 0) {
  296. dev_err(dev->dev, "Failed to set initial hw configuration.\n");
  297. goto err_drm_fb_helper_fini;
  298. }
  299. return fbdev_cma;
  300. err_drm_fb_helper_fini:
  301. drm_fb_helper_fini(helper);
  302. err_free:
  303. kfree(fbdev_cma);
  304. return ERR_PTR(ret);
  305. }
  306. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
  307. /**
  308. * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
  309. * @fbdev_cma: The drm_fbdev_cma struct
  310. */
  311. void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
  312. {
  313. drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
  314. drm_fb_helper_release_fbi(&fbdev_cma->fb_helper);
  315. if (fbdev_cma->fb) {
  316. drm_framebuffer_unregister_private(&fbdev_cma->fb->fb);
  317. drm_fb_cma_destroy(&fbdev_cma->fb->fb);
  318. }
  319. drm_fb_helper_fini(&fbdev_cma->fb_helper);
  320. kfree(fbdev_cma);
  321. }
  322. EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
  323. /**
  324. * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
  325. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  326. *
  327. * This function is usually called from the DRM drivers lastclose callback.
  328. */
  329. void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
  330. {
  331. if (fbdev_cma)
  332. drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
  333. }
  334. EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
  335. /**
  336. * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
  337. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  338. *
  339. * This function is usually called from the DRM drivers output_poll_changed
  340. * callback.
  341. */
  342. void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
  343. {
  344. if (fbdev_cma)
  345. drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
  346. }
  347. EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);