drm_fb_cma_helper.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_fb_helper.h>
  21. #include <drm/drm_framebuffer.h>
  22. #include <drm/drm_gem_cma_helper.h>
  23. #include <drm/drm_gem_framebuffer_helper.h>
  24. #include <drm/drm_fb_cma_helper.h>
  25. #include <linux/module.h>
  26. #define DEFAULT_FBDEFIO_DELAY_MS 50
  27. struct drm_fbdev_cma {
  28. struct drm_fb_helper fb_helper;
  29. const struct drm_framebuffer_funcs *fb_funcs;
  30. };
  31. /**
  32. * DOC: framebuffer cma helper functions
  33. *
  34. * Provides helper functions for creating a cma (contiguous memory allocator)
  35. * backed framebuffer.
  36. *
  37. * drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create
  38. * callback function to create a cma backed framebuffer.
  39. *
  40. * An fbdev framebuffer backed by cma is also available by calling
  41. * drm_fbdev_cma_init(). drm_fbdev_cma_fini() tears it down.
  42. * If the &drm_framebuffer_funcs.dirty callback is set, fb_deferred_io will be
  43. * set up automatically. &drm_framebuffer_funcs.dirty is called by
  44. * drm_fb_helper_deferred_io() in process context (&struct delayed_work).
  45. *
  46. * Example fbdev deferred io code::
  47. *
  48. * static int driver_fb_dirty(struct drm_framebuffer *fb,
  49. * struct drm_file *file_priv,
  50. * unsigned flags, unsigned color,
  51. * struct drm_clip_rect *clips,
  52. * unsigned num_clips)
  53. * {
  54. * struct drm_gem_cma_object *cma = drm_fb_cma_get_gem_obj(fb, 0);
  55. * ... push changes ...
  56. * return 0;
  57. * }
  58. *
  59. * static struct drm_framebuffer_funcs driver_fb_funcs = {
  60. * .destroy = drm_gem_fb_destroy,
  61. * .create_handle = drm_gem_fb_create_handle,
  62. * .dirty = driver_fb_dirty,
  63. * };
  64. *
  65. * Initialize::
  66. *
  67. * fbdev = drm_fbdev_cma_init_with_funcs(dev, 16,
  68. * dev->mode_config.num_crtc,
  69. * dev->mode_config.num_connector,
  70. * &driver_fb_funcs);
  71. *
  72. */
  73. static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
  74. {
  75. return container_of(helper, struct drm_fbdev_cma, fb_helper);
  76. }
  77. /**
  78. * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
  79. * @fb: The framebuffer
  80. * @plane: Which plane
  81. *
  82. * Return the CMA GEM object for given framebuffer.
  83. *
  84. * This function will usually be called from the CRTC callback functions.
  85. */
  86. struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
  87. unsigned int plane)
  88. {
  89. struct drm_gem_object *gem;
  90. gem = drm_gem_fb_get_obj(fb, plane);
  91. if (!gem)
  92. return NULL;
  93. return to_drm_gem_cma_obj(gem);
  94. }
  95. EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
  96. /**
  97. * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer
  98. * @fb: The framebuffer
  99. * @state: Which state of drm plane
  100. * @plane: Which plane
  101. * Return the CMA GEM address for given framebuffer.
  102. *
  103. * This function will usually be called from the PLANE callback functions.
  104. */
  105. dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb,
  106. struct drm_plane_state *state,
  107. unsigned int plane)
  108. {
  109. struct drm_gem_cma_object *obj;
  110. dma_addr_t paddr;
  111. obj = drm_fb_cma_get_gem_obj(fb, plane);
  112. if (!obj)
  113. return 0;
  114. paddr = obj->paddr + fb->offsets[plane];
  115. paddr += fb->format->cpp[plane] * (state->src_x >> 16);
  116. paddr += fb->pitches[plane] * (state->src_y >> 16);
  117. return paddr;
  118. }
  119. EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);
  120. static int drm_fb_cma_mmap(struct fb_info *info, struct vm_area_struct *vma)
  121. {
  122. return dma_mmap_writecombine(info->device, vma, info->screen_base,
  123. info->fix.smem_start, info->fix.smem_len);
  124. }
  125. static struct fb_ops drm_fbdev_cma_ops = {
  126. .owner = THIS_MODULE,
  127. DRM_FB_HELPER_DEFAULT_OPS,
  128. .fb_fillrect = drm_fb_helper_sys_fillrect,
  129. .fb_copyarea = drm_fb_helper_sys_copyarea,
  130. .fb_imageblit = drm_fb_helper_sys_imageblit,
  131. .fb_mmap = drm_fb_cma_mmap,
  132. };
  133. static int drm_fbdev_cma_deferred_io_mmap(struct fb_info *info,
  134. struct vm_area_struct *vma)
  135. {
  136. fb_deferred_io_mmap(info, vma);
  137. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  138. return 0;
  139. }
  140. static int drm_fbdev_cma_defio_init(struct fb_info *fbi,
  141. struct drm_gem_cma_object *cma_obj)
  142. {
  143. struct fb_deferred_io *fbdefio;
  144. struct fb_ops *fbops;
  145. /*
  146. * Per device structures are needed because:
  147. * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap
  148. * fbdefio: individual delays
  149. */
  150. fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
  151. fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
  152. if (!fbdefio || !fbops) {
  153. kfree(fbdefio);
  154. kfree(fbops);
  155. return -ENOMEM;
  156. }
  157. /* can't be offset from vaddr since dirty() uses cma_obj */
  158. fbi->screen_buffer = cma_obj->vaddr;
  159. /* fb_deferred_io_fault() needs a physical address */
  160. fbi->fix.smem_start = page_to_phys(virt_to_page(fbi->screen_buffer));
  161. *fbops = *fbi->fbops;
  162. fbi->fbops = fbops;
  163. fbdefio->delay = msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS);
  164. fbdefio->deferred_io = drm_fb_helper_deferred_io;
  165. fbi->fbdefio = fbdefio;
  166. fb_deferred_io_init(fbi);
  167. fbi->fbops->fb_mmap = drm_fbdev_cma_deferred_io_mmap;
  168. return 0;
  169. }
  170. static void drm_fbdev_cma_defio_fini(struct fb_info *fbi)
  171. {
  172. if (!fbi->fbdefio)
  173. return;
  174. fb_deferred_io_cleanup(fbi);
  175. kfree(fbi->fbdefio);
  176. kfree(fbi->fbops);
  177. }
  178. static int
  179. drm_fbdev_cma_create(struct drm_fb_helper *helper,
  180. struct drm_fb_helper_surface_size *sizes)
  181. {
  182. struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
  183. struct drm_device *dev = helper->dev;
  184. struct drm_gem_cma_object *obj;
  185. struct drm_framebuffer *fb;
  186. unsigned int bytes_per_pixel;
  187. unsigned long offset;
  188. struct fb_info *fbi;
  189. size_t size;
  190. int ret;
  191. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
  192. sizes->surface_width, sizes->surface_height,
  193. sizes->surface_bpp);
  194. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  195. size = sizes->surface_width * sizes->surface_height * bytes_per_pixel;
  196. obj = drm_gem_cma_create(dev, size);
  197. if (IS_ERR(obj))
  198. return -ENOMEM;
  199. fbi = drm_fb_helper_alloc_fbi(helper);
  200. if (IS_ERR(fbi)) {
  201. ret = PTR_ERR(fbi);
  202. goto err_gem_free_object;
  203. }
  204. fb = drm_gem_fbdev_fb_create(dev, sizes, 0, &obj->base,
  205. fbdev_cma->fb_funcs);
  206. if (IS_ERR(fb)) {
  207. dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
  208. ret = PTR_ERR(fb);
  209. goto err_fb_info_destroy;
  210. }
  211. helper->fb = fb;
  212. fbi->par = helper;
  213. fbi->flags = FBINFO_FLAG_DEFAULT;
  214. fbi->fbops = &drm_fbdev_cma_ops;
  215. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
  216. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  217. offset = fbi->var.xoffset * bytes_per_pixel;
  218. offset += fbi->var.yoffset * fb->pitches[0];
  219. dev->mode_config.fb_base = (resource_size_t)obj->paddr;
  220. fbi->screen_base = obj->vaddr + offset;
  221. fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
  222. fbi->screen_size = size;
  223. fbi->fix.smem_len = size;
  224. if (fbdev_cma->fb_funcs->dirty) {
  225. ret = drm_fbdev_cma_defio_init(fbi, obj);
  226. if (ret)
  227. goto err_cma_destroy;
  228. }
  229. return 0;
  230. err_cma_destroy:
  231. drm_framebuffer_remove(fb);
  232. err_fb_info_destroy:
  233. drm_fb_helper_fini(helper);
  234. err_gem_free_object:
  235. drm_gem_object_put_unlocked(&obj->base);
  236. return ret;
  237. }
  238. static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
  239. .fb_probe = drm_fbdev_cma_create,
  240. };
  241. /**
  242. * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
  243. * @dev: DRM device
  244. * @preferred_bpp: Preferred bits per pixel for the device
  245. * @max_conn_count: Maximum number of connectors
  246. * @funcs: fb helper functions, in particular a custom dirty() callback
  247. *
  248. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  249. */
  250. struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
  251. unsigned int preferred_bpp, unsigned int max_conn_count,
  252. const struct drm_framebuffer_funcs *funcs)
  253. {
  254. struct drm_fbdev_cma *fbdev_cma;
  255. struct drm_fb_helper *helper;
  256. int ret;
  257. fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
  258. if (!fbdev_cma) {
  259. dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
  260. return ERR_PTR(-ENOMEM);
  261. }
  262. fbdev_cma->fb_funcs = funcs;
  263. helper = &fbdev_cma->fb_helper;
  264. drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
  265. ret = drm_fb_helper_init(dev, helper, max_conn_count);
  266. if (ret < 0) {
  267. dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
  268. goto err_free;
  269. }
  270. ret = drm_fb_helper_single_add_all_connectors(helper);
  271. if (ret < 0) {
  272. dev_err(dev->dev, "Failed to add connectors.\n");
  273. goto err_drm_fb_helper_fini;
  274. }
  275. ret = drm_fb_helper_initial_config(helper, preferred_bpp);
  276. if (ret < 0) {
  277. dev_err(dev->dev, "Failed to set initial hw configuration.\n");
  278. goto err_drm_fb_helper_fini;
  279. }
  280. return fbdev_cma;
  281. err_drm_fb_helper_fini:
  282. drm_fb_helper_fini(helper);
  283. err_free:
  284. kfree(fbdev_cma);
  285. return ERR_PTR(ret);
  286. }
  287. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs);
  288. static const struct drm_framebuffer_funcs drm_fb_cma_funcs = {
  289. .destroy = drm_gem_fb_destroy,
  290. .create_handle = drm_gem_fb_create_handle,
  291. };
  292. /**
  293. * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
  294. * @dev: DRM device
  295. * @preferred_bpp: Preferred bits per pixel for the device
  296. * @max_conn_count: Maximum number of connectors
  297. *
  298. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  299. */
  300. struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
  301. unsigned int preferred_bpp, unsigned int max_conn_count)
  302. {
  303. return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp,
  304. max_conn_count,
  305. &drm_fb_cma_funcs);
  306. }
  307. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
  308. /**
  309. * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
  310. * @fbdev_cma: The drm_fbdev_cma struct
  311. */
  312. void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
  313. {
  314. drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
  315. if (fbdev_cma->fb_helper.fbdev)
  316. drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev);
  317. if (fbdev_cma->fb_helper.fb)
  318. drm_framebuffer_remove(fbdev_cma->fb_helper.fb);
  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_driver.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_mode_config.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);
  348. /**
  349. * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend
  350. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  351. * @state: desired state, zero to resume, non-zero to suspend
  352. *
  353. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  354. * fb_set_suspend implemented by fbdev core.
  355. */
  356. void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, bool state)
  357. {
  358. if (fbdev_cma)
  359. drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state);
  360. }
  361. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend);
  362. /**
  363. * drm_fbdev_cma_set_suspend_unlocked - wrapper around
  364. * drm_fb_helper_set_suspend_unlocked
  365. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  366. * @state: desired state, zero to resume, non-zero to suspend
  367. *
  368. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  369. * fb_set_suspend implemented by fbdev core.
  370. */
  371. void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma *fbdev_cma,
  372. bool state)
  373. {
  374. if (fbdev_cma)
  375. drm_fb_helper_set_suspend_unlocked(&fbdev_cma->fb_helper,
  376. state);
  377. }
  378. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked);