drm_fb_cma_helper.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. ret = drm_framebuffer_init(dev, &fb_cma->fb, &drm_fb_cma_funcs);
  75. if (ret) {
  76. dev_err(dev->dev, "Failed to initalize framebuffer: %d\n", ret);
  77. kfree(fb_cma);
  78. return ERR_PTR(ret);
  79. }
  80. drm_helper_mode_fill_fb_struct(&fb_cma->fb, mode_cmd);
  81. for (i = 0; i < num_planes; i++)
  82. fb_cma->obj[i] = obj[i];
  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. static struct fb_ops drm_fbdev_cma_ops = {
  154. .owner = THIS_MODULE,
  155. .fb_fillrect = sys_fillrect,
  156. .fb_copyarea = sys_copyarea,
  157. .fb_imageblit = sys_imageblit,
  158. .fb_check_var = drm_fb_helper_check_var,
  159. .fb_set_par = drm_fb_helper_set_par,
  160. .fb_blank = drm_fb_helper_blank,
  161. .fb_pan_display = drm_fb_helper_pan_display,
  162. .fb_setcmap = drm_fb_helper_setcmap,
  163. };
  164. static int drm_fbdev_cma_create(struct drm_fb_helper *helper,
  165. struct drm_fb_helper_surface_size *sizes)
  166. {
  167. struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
  168. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  169. struct drm_device *dev = helper->dev;
  170. struct drm_gem_cma_object *obj;
  171. struct drm_framebuffer *fb;
  172. unsigned int bytes_per_pixel;
  173. unsigned long offset;
  174. struct fb_info *fbi;
  175. size_t size;
  176. int ret;
  177. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
  178. sizes->surface_width, sizes->surface_height,
  179. sizes->surface_bpp);
  180. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  181. mode_cmd.width = sizes->surface_width;
  182. mode_cmd.height = sizes->surface_height;
  183. mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
  184. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  185. sizes->surface_depth);
  186. size = mode_cmd.pitches[0] * mode_cmd.height;
  187. obj = drm_gem_cma_create(dev, size);
  188. if (!obj)
  189. return -ENOMEM;
  190. fbi = framebuffer_alloc(0, dev->dev);
  191. if (!fbi) {
  192. dev_err(dev->dev, "Failed to allocate framebuffer info.\n");
  193. ret = -ENOMEM;
  194. goto err_drm_gem_cma_free_object;
  195. }
  196. fbdev_cma->fb = drm_fb_cma_alloc(dev, &mode_cmd, &obj, 1);
  197. if (IS_ERR(fbdev_cma->fb)) {
  198. dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
  199. ret = PTR_ERR(fbdev_cma->fb);
  200. goto err_framebuffer_release;
  201. }
  202. fb = &fbdev_cma->fb->fb;
  203. helper->fb = fb;
  204. helper->fbdev = fbi;
  205. fbi->par = helper;
  206. fbi->flags = FBINFO_FLAG_DEFAULT;
  207. fbi->fbops = &drm_fbdev_cma_ops;
  208. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  209. if (ret) {
  210. dev_err(dev->dev, "Failed to allocate color map.\n");
  211. goto err_drm_fb_cma_destroy;
  212. }
  213. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  214. drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
  215. offset = fbi->var.xoffset * bytes_per_pixel;
  216. offset += fbi->var.yoffset * fb->pitches[0];
  217. dev->mode_config.fb_base = (resource_size_t)obj->paddr;
  218. fbi->screen_base = obj->vaddr + offset;
  219. fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
  220. fbi->screen_size = size;
  221. fbi->fix.smem_len = size;
  222. return 0;
  223. err_drm_fb_cma_destroy:
  224. drm_fb_cma_destroy(fb);
  225. err_framebuffer_release:
  226. framebuffer_release(fbi);
  227. err_drm_gem_cma_free_object:
  228. drm_gem_cma_free_object(&obj->base);
  229. return ret;
  230. }
  231. static int drm_fbdev_cma_probe(struct drm_fb_helper *helper,
  232. struct drm_fb_helper_surface_size *sizes)
  233. {
  234. int ret = 0;
  235. if (!helper->fb) {
  236. ret = drm_fbdev_cma_create(helper, sizes);
  237. if (ret < 0)
  238. return ret;
  239. ret = 1;
  240. }
  241. return ret;
  242. }
  243. static struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
  244. .fb_probe = drm_fbdev_cma_probe,
  245. };
  246. /**
  247. * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
  248. * @dev: DRM device
  249. * @preferred_bpp: Preferred bits per pixel for the device
  250. * @num_crtc: Number of CRTCs
  251. * @max_conn_count: Maximum number of connectors
  252. *
  253. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  254. */
  255. struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
  256. unsigned int preferred_bpp, unsigned int num_crtc,
  257. unsigned int max_conn_count)
  258. {
  259. struct drm_fbdev_cma *fbdev_cma;
  260. struct drm_fb_helper *helper;
  261. int ret;
  262. fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
  263. if (!fbdev_cma) {
  264. dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
  265. return ERR_PTR(-ENOMEM);
  266. }
  267. fbdev_cma->fb_helper.funcs = &drm_fb_cma_helper_funcs;
  268. helper = &fbdev_cma->fb_helper;
  269. ret = drm_fb_helper_init(dev, helper, num_crtc, max_conn_count);
  270. if (ret < 0) {
  271. dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
  272. goto err_free;
  273. }
  274. ret = drm_fb_helper_single_add_all_connectors(helper);
  275. if (ret < 0) {
  276. dev_err(dev->dev, "Failed to add connectors.\n");
  277. goto err_drm_fb_helper_fini;
  278. }
  279. ret = drm_fb_helper_initial_config(helper, preferred_bpp);
  280. if (ret < 0) {
  281. dev_err(dev->dev, "Failed to set inital hw configuration.\n");
  282. goto err_drm_fb_helper_fini;
  283. }
  284. return fbdev_cma;
  285. err_drm_fb_helper_fini:
  286. drm_fb_helper_fini(helper);
  287. err_free:
  288. kfree(fbdev_cma);
  289. return ERR_PTR(ret);
  290. }
  291. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
  292. /**
  293. * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
  294. * @fbdev_cma: The drm_fbdev_cma struct
  295. */
  296. void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
  297. {
  298. if (fbdev_cma->fb_helper.fbdev) {
  299. struct fb_info *info;
  300. int ret;
  301. info = fbdev_cma->fb_helper.fbdev;
  302. ret = unregister_framebuffer(info);
  303. if (ret < 0)
  304. DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
  305. if (info->cmap.len)
  306. fb_dealloc_cmap(&info->cmap);
  307. framebuffer_release(info);
  308. }
  309. if (fbdev_cma->fb)
  310. drm_fb_cma_destroy(&fbdev_cma->fb->fb);
  311. drm_fb_helper_fini(&fbdev_cma->fb_helper);
  312. kfree(fbdev_cma);
  313. }
  314. EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
  315. /**
  316. * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
  317. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  318. *
  319. * This function is usually called from the DRM drivers lastclose callback.
  320. */
  321. void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
  322. {
  323. if (fbdev_cma)
  324. drm_fb_helper_restore_fbdev_mode(&fbdev_cma->fb_helper);
  325. }
  326. EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
  327. /**
  328. * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
  329. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  330. *
  331. * This function is usually called from the DRM drivers output_poll_changed
  332. * callback.
  333. */
  334. void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
  335. {
  336. if (fbdev_cma)
  337. drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
  338. }
  339. EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);