drm_fb_cma_helper.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. #ifdef CONFIG_DEBUG_FS
  121. static void drm_fb_cma_describe(struct drm_framebuffer *fb, struct seq_file *m)
  122. {
  123. int i;
  124. seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
  125. (char *)&fb->format->format);
  126. for (i = 0; i < fb->format->num_planes; i++) {
  127. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  128. i, fb->offsets[i], fb->pitches[i]);
  129. drm_gem_cma_describe(drm_fb_cma_get_gem_obj(fb, i), m);
  130. }
  131. }
  132. /**
  133. * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects
  134. * in debugfs.
  135. * @m: output file
  136. * @arg: private data for the callback
  137. */
  138. int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg)
  139. {
  140. struct drm_info_node *node = (struct drm_info_node *) m->private;
  141. struct drm_device *dev = node->minor->dev;
  142. struct drm_framebuffer *fb;
  143. mutex_lock(&dev->mode_config.fb_lock);
  144. drm_for_each_fb(fb, dev)
  145. drm_fb_cma_describe(fb, m);
  146. mutex_unlock(&dev->mode_config.fb_lock);
  147. return 0;
  148. }
  149. EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show);
  150. #endif
  151. static int drm_fb_cma_mmap(struct fb_info *info, struct vm_area_struct *vma)
  152. {
  153. return dma_mmap_writecombine(info->device, vma, info->screen_base,
  154. info->fix.smem_start, info->fix.smem_len);
  155. }
  156. static struct fb_ops drm_fbdev_cma_ops = {
  157. .owner = THIS_MODULE,
  158. DRM_FB_HELPER_DEFAULT_OPS,
  159. .fb_fillrect = drm_fb_helper_sys_fillrect,
  160. .fb_copyarea = drm_fb_helper_sys_copyarea,
  161. .fb_imageblit = drm_fb_helper_sys_imageblit,
  162. .fb_mmap = drm_fb_cma_mmap,
  163. };
  164. static int drm_fbdev_cma_deferred_io_mmap(struct fb_info *info,
  165. struct vm_area_struct *vma)
  166. {
  167. fb_deferred_io_mmap(info, vma);
  168. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  169. return 0;
  170. }
  171. static int drm_fbdev_cma_defio_init(struct fb_info *fbi,
  172. struct drm_gem_cma_object *cma_obj)
  173. {
  174. struct fb_deferred_io *fbdefio;
  175. struct fb_ops *fbops;
  176. /*
  177. * Per device structures are needed because:
  178. * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap
  179. * fbdefio: individual delays
  180. */
  181. fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
  182. fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
  183. if (!fbdefio || !fbops) {
  184. kfree(fbdefio);
  185. kfree(fbops);
  186. return -ENOMEM;
  187. }
  188. /* can't be offset from vaddr since dirty() uses cma_obj */
  189. fbi->screen_buffer = cma_obj->vaddr;
  190. /* fb_deferred_io_fault() needs a physical address */
  191. fbi->fix.smem_start = page_to_phys(virt_to_page(fbi->screen_buffer));
  192. *fbops = *fbi->fbops;
  193. fbi->fbops = fbops;
  194. fbdefio->delay = msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS);
  195. fbdefio->deferred_io = drm_fb_helper_deferred_io;
  196. fbi->fbdefio = fbdefio;
  197. fb_deferred_io_init(fbi);
  198. fbi->fbops->fb_mmap = drm_fbdev_cma_deferred_io_mmap;
  199. return 0;
  200. }
  201. static void drm_fbdev_cma_defio_fini(struct fb_info *fbi)
  202. {
  203. if (!fbi->fbdefio)
  204. return;
  205. fb_deferred_io_cleanup(fbi);
  206. kfree(fbi->fbdefio);
  207. kfree(fbi->fbops);
  208. }
  209. static int
  210. drm_fbdev_cma_create(struct drm_fb_helper *helper,
  211. struct drm_fb_helper_surface_size *sizes)
  212. {
  213. struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
  214. struct drm_device *dev = helper->dev;
  215. struct drm_gem_cma_object *obj;
  216. struct drm_framebuffer *fb;
  217. unsigned int bytes_per_pixel;
  218. unsigned long offset;
  219. struct fb_info *fbi;
  220. size_t size;
  221. int ret;
  222. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
  223. sizes->surface_width, sizes->surface_height,
  224. sizes->surface_bpp);
  225. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  226. size = sizes->surface_width * sizes->surface_height * bytes_per_pixel;
  227. obj = drm_gem_cma_create(dev, size);
  228. if (IS_ERR(obj))
  229. return -ENOMEM;
  230. fbi = drm_fb_helper_alloc_fbi(helper);
  231. if (IS_ERR(fbi)) {
  232. ret = PTR_ERR(fbi);
  233. goto err_gem_free_object;
  234. }
  235. fb = drm_gem_fbdev_fb_create(dev, sizes, 0, &obj->base,
  236. fbdev_cma->fb_funcs);
  237. if (IS_ERR(fb)) {
  238. dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
  239. ret = PTR_ERR(fb);
  240. goto err_fb_info_destroy;
  241. }
  242. helper->fb = fb;
  243. fbi->par = helper;
  244. fbi->flags = FBINFO_FLAG_DEFAULT;
  245. fbi->fbops = &drm_fbdev_cma_ops;
  246. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
  247. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  248. offset = fbi->var.xoffset * bytes_per_pixel;
  249. offset += fbi->var.yoffset * fb->pitches[0];
  250. dev->mode_config.fb_base = (resource_size_t)obj->paddr;
  251. fbi->screen_base = obj->vaddr + offset;
  252. fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
  253. fbi->screen_size = size;
  254. fbi->fix.smem_len = size;
  255. if (fbdev_cma->fb_funcs->dirty) {
  256. ret = drm_fbdev_cma_defio_init(fbi, obj);
  257. if (ret)
  258. goto err_cma_destroy;
  259. }
  260. return 0;
  261. err_cma_destroy:
  262. drm_framebuffer_remove(fb);
  263. err_fb_info_destroy:
  264. drm_fb_helper_fini(helper);
  265. err_gem_free_object:
  266. drm_gem_object_put_unlocked(&obj->base);
  267. return ret;
  268. }
  269. static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
  270. .fb_probe = drm_fbdev_cma_create,
  271. };
  272. /**
  273. * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
  274. * @dev: DRM device
  275. * @preferred_bpp: Preferred bits per pixel for the device
  276. * @max_conn_count: Maximum number of connectors
  277. * @funcs: fb helper functions, in particular a custom dirty() callback
  278. *
  279. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  280. */
  281. struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
  282. unsigned int preferred_bpp, unsigned int max_conn_count,
  283. const struct drm_framebuffer_funcs *funcs)
  284. {
  285. struct drm_fbdev_cma *fbdev_cma;
  286. struct drm_fb_helper *helper;
  287. int ret;
  288. fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
  289. if (!fbdev_cma) {
  290. dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
  291. return ERR_PTR(-ENOMEM);
  292. }
  293. fbdev_cma->fb_funcs = funcs;
  294. helper = &fbdev_cma->fb_helper;
  295. drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
  296. ret = drm_fb_helper_init(dev, helper, max_conn_count);
  297. if (ret < 0) {
  298. dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
  299. goto err_free;
  300. }
  301. ret = drm_fb_helper_single_add_all_connectors(helper);
  302. if (ret < 0) {
  303. dev_err(dev->dev, "Failed to add connectors.\n");
  304. goto err_drm_fb_helper_fini;
  305. }
  306. ret = drm_fb_helper_initial_config(helper, preferred_bpp);
  307. if (ret < 0) {
  308. dev_err(dev->dev, "Failed to set initial hw configuration.\n");
  309. goto err_drm_fb_helper_fini;
  310. }
  311. return fbdev_cma;
  312. err_drm_fb_helper_fini:
  313. drm_fb_helper_fini(helper);
  314. err_free:
  315. kfree(fbdev_cma);
  316. return ERR_PTR(ret);
  317. }
  318. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs);
  319. static const struct drm_framebuffer_funcs drm_fb_cma_funcs = {
  320. .destroy = drm_gem_fb_destroy,
  321. .create_handle = drm_gem_fb_create_handle,
  322. };
  323. /**
  324. * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
  325. * @dev: DRM device
  326. * @preferred_bpp: Preferred bits per pixel for the device
  327. * @max_conn_count: Maximum number of connectors
  328. *
  329. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  330. */
  331. struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
  332. unsigned int preferred_bpp, unsigned int max_conn_count)
  333. {
  334. return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp,
  335. max_conn_count,
  336. &drm_fb_cma_funcs);
  337. }
  338. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
  339. /**
  340. * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
  341. * @fbdev_cma: The drm_fbdev_cma struct
  342. */
  343. void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
  344. {
  345. drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
  346. if (fbdev_cma->fb_helper.fbdev)
  347. drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev);
  348. if (fbdev_cma->fb_helper.fb)
  349. drm_framebuffer_remove(fbdev_cma->fb_helper.fb);
  350. drm_fb_helper_fini(&fbdev_cma->fb_helper);
  351. kfree(fbdev_cma);
  352. }
  353. EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
  354. /**
  355. * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
  356. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  357. *
  358. * This function is usually called from the &drm_driver.lastclose callback.
  359. */
  360. void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
  361. {
  362. if (fbdev_cma)
  363. drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
  364. }
  365. EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
  366. /**
  367. * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
  368. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  369. *
  370. * This function is usually called from the &drm_mode_config.output_poll_changed
  371. * callback.
  372. */
  373. void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
  374. {
  375. if (fbdev_cma)
  376. drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
  377. }
  378. EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
  379. /**
  380. * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend
  381. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  382. * @state: desired state, zero to resume, non-zero to suspend
  383. *
  384. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  385. * fb_set_suspend implemented by fbdev core.
  386. */
  387. void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, bool state)
  388. {
  389. if (fbdev_cma)
  390. drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state);
  391. }
  392. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend);
  393. /**
  394. * drm_fbdev_cma_set_suspend_unlocked - wrapper around
  395. * drm_fb_helper_set_suspend_unlocked
  396. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  397. * @state: desired state, zero to resume, non-zero to suspend
  398. *
  399. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  400. * fb_set_suspend implemented by fbdev core.
  401. */
  402. void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma *fbdev_cma,
  403. bool state)
  404. {
  405. if (fbdev_cma)
  406. drm_fb_helper_set_suspend_unlocked(&fbdev_cma->fb_helper,
  407. state);
  408. }
  409. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked);