drm_fb_cma_helper.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_client.h>
  21. #include <drm/drm_fb_helper.h>
  22. #include <drm/drm_framebuffer.h>
  23. #include <drm/drm_gem_cma_helper.h>
  24. #include <drm/drm_gem_framebuffer_helper.h>
  25. #include <drm/drm_fb_cma_helper.h>
  26. #include <drm/drm_print.h>
  27. #include <linux/module.h>
  28. struct drm_fbdev_cma {
  29. struct drm_fb_helper fb_helper;
  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_fb_cma_fbdev_init(). drm_fb_cma_fbdev_fini() tears it down.
  42. */
  43. static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
  44. {
  45. return container_of(helper, struct drm_fbdev_cma, fb_helper);
  46. }
  47. /**
  48. * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
  49. * @fb: The framebuffer
  50. * @plane: Which plane
  51. *
  52. * Return the CMA GEM object for given framebuffer.
  53. *
  54. * This function will usually be called from the CRTC callback functions.
  55. */
  56. struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
  57. unsigned int plane)
  58. {
  59. struct drm_gem_object *gem;
  60. gem = drm_gem_fb_get_obj(fb, plane);
  61. if (!gem)
  62. return NULL;
  63. return to_drm_gem_cma_obj(gem);
  64. }
  65. EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
  66. /**
  67. * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer
  68. * @fb: The framebuffer
  69. * @state: Which state of drm plane
  70. * @plane: Which plane
  71. * Return the CMA GEM address for given framebuffer.
  72. *
  73. * This function will usually be called from the PLANE callback functions.
  74. */
  75. dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb,
  76. struct drm_plane_state *state,
  77. unsigned int plane)
  78. {
  79. struct drm_gem_cma_object *obj;
  80. dma_addr_t paddr;
  81. obj = drm_fb_cma_get_gem_obj(fb, plane);
  82. if (!obj)
  83. return 0;
  84. paddr = obj->paddr + fb->offsets[plane];
  85. paddr += fb->format->cpp[plane] * (state->src_x >> 16);
  86. paddr += fb->pitches[plane] * (state->src_y >> 16);
  87. return paddr;
  88. }
  89. EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);
  90. /**
  91. * drm_fb_cma_fbdev_init() - Allocate and initialize fbdev emulation
  92. * @dev: DRM device
  93. * @preferred_bpp: Preferred bits per pixel for the device.
  94. * @dev->mode_config.preferred_depth is used if this is zero.
  95. * @max_conn_count: Maximum number of connectors.
  96. * @dev->mode_config.num_connector is used if this is zero.
  97. *
  98. * Returns:
  99. * Zero on success or negative error code on failure.
  100. */
  101. int drm_fb_cma_fbdev_init(struct drm_device *dev, unsigned int preferred_bpp,
  102. unsigned int max_conn_count)
  103. {
  104. struct drm_fbdev_cma *fbdev_cma;
  105. /* dev->fb_helper will indirectly point to fbdev_cma after this call */
  106. fbdev_cma = drm_fbdev_cma_init(dev, preferred_bpp, max_conn_count);
  107. if (IS_ERR(fbdev_cma))
  108. return PTR_ERR(fbdev_cma);
  109. return 0;
  110. }
  111. EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_init);
  112. /**
  113. * drm_fb_cma_fbdev_fini() - Teardown fbdev emulation
  114. * @dev: DRM device
  115. */
  116. void drm_fb_cma_fbdev_fini(struct drm_device *dev)
  117. {
  118. if (dev->fb_helper)
  119. drm_fbdev_cma_fini(to_fbdev_cma(dev->fb_helper));
  120. }
  121. EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_fini);
  122. static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
  123. .fb_probe = drm_fb_helper_generic_probe,
  124. };
  125. /**
  126. * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
  127. * @dev: DRM device
  128. * @preferred_bpp: Preferred bits per pixel for the device
  129. * @max_conn_count: Maximum number of connectors
  130. *
  131. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  132. */
  133. struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
  134. unsigned int preferred_bpp, unsigned int max_conn_count)
  135. {
  136. struct drm_fbdev_cma *fbdev_cma;
  137. struct drm_fb_helper *fb_helper;
  138. int ret;
  139. fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
  140. if (!fbdev_cma)
  141. return ERR_PTR(-ENOMEM);
  142. fb_helper = &fbdev_cma->fb_helper;
  143. ret = drm_client_init(dev, &fb_helper->client, "fbdev", NULL);
  144. if (ret)
  145. goto err_free;
  146. ret = drm_fb_helper_fbdev_setup(dev, fb_helper, &drm_fb_cma_helper_funcs,
  147. preferred_bpp, max_conn_count);
  148. if (ret)
  149. goto err_client_put;
  150. drm_client_add(&fb_helper->client);
  151. return fbdev_cma;
  152. err_client_put:
  153. drm_client_release(&fb_helper->client);
  154. err_free:
  155. kfree(fbdev_cma);
  156. return ERR_PTR(ret);
  157. }
  158. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
  159. /**
  160. * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
  161. * @fbdev_cma: The drm_fbdev_cma struct
  162. */
  163. void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
  164. {
  165. drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
  166. /* All resources have now been freed by drm_fbdev_fb_destroy() */
  167. }
  168. EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
  169. /**
  170. * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
  171. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  172. *
  173. * This function is usually called from the &drm_driver.lastclose callback.
  174. */
  175. void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
  176. {
  177. if (fbdev_cma)
  178. drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
  179. }
  180. EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
  181. /**
  182. * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
  183. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  184. *
  185. * This function is usually called from the &drm_mode_config.output_poll_changed
  186. * callback.
  187. */
  188. void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
  189. {
  190. if (fbdev_cma)
  191. drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
  192. }
  193. EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
  194. /**
  195. * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend
  196. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  197. * @state: desired state, zero to resume, non-zero to suspend
  198. *
  199. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  200. * fb_set_suspend implemented by fbdev core.
  201. */
  202. void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, bool state)
  203. {
  204. if (fbdev_cma)
  205. drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state);
  206. }
  207. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend);
  208. /**
  209. * drm_fbdev_cma_set_suspend_unlocked - wrapper around
  210. * drm_fb_helper_set_suspend_unlocked
  211. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  212. * @state: desired state, zero to resume, non-zero to suspend
  213. *
  214. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  215. * fb_set_suspend implemented by fbdev core.
  216. */
  217. void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma *fbdev_cma,
  218. bool state)
  219. {
  220. if (fbdev_cma)
  221. drm_fb_helper_set_suspend_unlocked(&fbdev_cma->fb_helper,
  222. state);
  223. }
  224. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked);