drm_fb_cma_helper.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. int ret;
  180. ret = mutex_lock_interruptible(&dev->mode_config.mutex);
  181. if (ret)
  182. return ret;
  183. ret = mutex_lock_interruptible(&dev->struct_mutex);
  184. if (ret) {
  185. mutex_unlock(&dev->mode_config.mutex);
  186. return ret;
  187. }
  188. list_for_each_entry(fb, &dev->mode_config.fb_list, head)
  189. drm_fb_cma_describe(fb, m);
  190. mutex_unlock(&dev->struct_mutex);
  191. mutex_unlock(&dev->mode_config.mutex);
  192. return 0;
  193. }
  194. EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show);
  195. #endif
  196. static struct fb_ops drm_fbdev_cma_ops = {
  197. .owner = THIS_MODULE,
  198. .fb_fillrect = sys_fillrect,
  199. .fb_copyarea = sys_copyarea,
  200. .fb_imageblit = sys_imageblit,
  201. .fb_check_var = drm_fb_helper_check_var,
  202. .fb_set_par = drm_fb_helper_set_par,
  203. .fb_blank = drm_fb_helper_blank,
  204. .fb_pan_display = drm_fb_helper_pan_display,
  205. .fb_setcmap = drm_fb_helper_setcmap,
  206. };
  207. static int drm_fbdev_cma_create(struct drm_fb_helper *helper,
  208. struct drm_fb_helper_surface_size *sizes)
  209. {
  210. struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
  211. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  212. struct drm_device *dev = helper->dev;
  213. struct drm_gem_cma_object *obj;
  214. struct drm_framebuffer *fb;
  215. unsigned int bytes_per_pixel;
  216. unsigned long offset;
  217. struct fb_info *fbi;
  218. size_t size;
  219. int ret;
  220. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
  221. sizes->surface_width, sizes->surface_height,
  222. sizes->surface_bpp);
  223. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  224. mode_cmd.width = sizes->surface_width;
  225. mode_cmd.height = sizes->surface_height;
  226. mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
  227. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  228. sizes->surface_depth);
  229. size = mode_cmd.pitches[0] * mode_cmd.height;
  230. obj = drm_gem_cma_create(dev, size);
  231. if (IS_ERR(obj))
  232. return -ENOMEM;
  233. fbi = framebuffer_alloc(0, dev->dev);
  234. if (!fbi) {
  235. dev_err(dev->dev, "Failed to allocate framebuffer info.\n");
  236. ret = -ENOMEM;
  237. goto err_drm_gem_cma_free_object;
  238. }
  239. fbdev_cma->fb = drm_fb_cma_alloc(dev, &mode_cmd, &obj, 1);
  240. if (IS_ERR(fbdev_cma->fb)) {
  241. dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
  242. ret = PTR_ERR(fbdev_cma->fb);
  243. goto err_framebuffer_release;
  244. }
  245. fb = &fbdev_cma->fb->fb;
  246. helper->fb = fb;
  247. helper->fbdev = fbi;
  248. fbi->par = helper;
  249. fbi->flags = FBINFO_FLAG_DEFAULT;
  250. fbi->fbops = &drm_fbdev_cma_ops;
  251. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  252. if (ret) {
  253. dev_err(dev->dev, "Failed to allocate color map.\n");
  254. goto err_drm_fb_cma_destroy;
  255. }
  256. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  257. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  258. offset = fbi->var.xoffset * bytes_per_pixel;
  259. offset += fbi->var.yoffset * fb->pitches[0];
  260. dev->mode_config.fb_base = (resource_size_t)obj->paddr;
  261. fbi->screen_base = obj->vaddr + offset;
  262. fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
  263. fbi->screen_size = size;
  264. fbi->fix.smem_len = size;
  265. return 0;
  266. err_drm_fb_cma_destroy:
  267. drm_framebuffer_unregister_private(fb);
  268. drm_fb_cma_destroy(fb);
  269. err_framebuffer_release:
  270. framebuffer_release(fbi);
  271. err_drm_gem_cma_free_object:
  272. drm_gem_cma_free_object(&obj->base);
  273. return ret;
  274. }
  275. static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
  276. .fb_probe = drm_fbdev_cma_create,
  277. };
  278. /**
  279. * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
  280. * @dev: DRM device
  281. * @preferred_bpp: Preferred bits per pixel for the device
  282. * @num_crtc: Number of CRTCs
  283. * @max_conn_count: Maximum number of connectors
  284. *
  285. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  286. */
  287. struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
  288. unsigned int preferred_bpp, unsigned int num_crtc,
  289. unsigned int max_conn_count)
  290. {
  291. struct drm_fbdev_cma *fbdev_cma;
  292. struct drm_fb_helper *helper;
  293. int ret;
  294. fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
  295. if (!fbdev_cma) {
  296. dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
  297. return ERR_PTR(-ENOMEM);
  298. }
  299. helper = &fbdev_cma->fb_helper;
  300. drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
  301. ret = drm_fb_helper_init(dev, helper, num_crtc, max_conn_count);
  302. if (ret < 0) {
  303. dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
  304. goto err_free;
  305. }
  306. ret = drm_fb_helper_single_add_all_connectors(helper);
  307. if (ret < 0) {
  308. dev_err(dev->dev, "Failed to add connectors.\n");
  309. goto err_drm_fb_helper_fini;
  310. }
  311. /* disable all the possible outputs/crtcs before entering KMS mode */
  312. drm_helper_disable_unused_functions(dev);
  313. ret = drm_fb_helper_initial_config(helper, preferred_bpp);
  314. if (ret < 0) {
  315. dev_err(dev->dev, "Failed to set initial hw configuration.\n");
  316. goto err_drm_fb_helper_fini;
  317. }
  318. return fbdev_cma;
  319. err_drm_fb_helper_fini:
  320. drm_fb_helper_fini(helper);
  321. err_free:
  322. kfree(fbdev_cma);
  323. return ERR_PTR(ret);
  324. }
  325. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
  326. /**
  327. * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
  328. * @fbdev_cma: The drm_fbdev_cma struct
  329. */
  330. void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
  331. {
  332. if (fbdev_cma->fb_helper.fbdev) {
  333. struct fb_info *info;
  334. int ret;
  335. info = fbdev_cma->fb_helper.fbdev;
  336. ret = unregister_framebuffer(info);
  337. if (ret < 0)
  338. DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
  339. if (info->cmap.len)
  340. fb_dealloc_cmap(&info->cmap);
  341. framebuffer_release(info);
  342. }
  343. if (fbdev_cma->fb) {
  344. drm_framebuffer_unregister_private(&fbdev_cma->fb->fb);
  345. drm_fb_cma_destroy(&fbdev_cma->fb->fb);
  346. }
  347. drm_fb_helper_fini(&fbdev_cma->fb_helper);
  348. kfree(fbdev_cma);
  349. }
  350. EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
  351. /**
  352. * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
  353. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  354. *
  355. * This function is usually called from the DRM drivers lastclose callback.
  356. */
  357. void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
  358. {
  359. if (fbdev_cma)
  360. drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
  361. }
  362. EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
  363. /**
  364. * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
  365. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  366. *
  367. * This function is usually called from the DRM drivers output_poll_changed
  368. * callback.
  369. */
  370. void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
  371. {
  372. if (fbdev_cma)
  373. drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
  374. }
  375. EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);