drm_fb_cma_helper.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. u8 h_div = 1, v_div = 1;
  82. obj = drm_fb_cma_get_gem_obj(fb, plane);
  83. if (!obj)
  84. return 0;
  85. paddr = obj->paddr + fb->offsets[plane];
  86. if (plane > 0) {
  87. h_div = fb->format->hsub;
  88. v_div = fb->format->vsub;
  89. }
  90. paddr += (fb->format->cpp[plane] * (state->src_x >> 16)) / h_div;
  91. paddr += (fb->pitches[plane] * (state->src_y >> 16)) / v_div;
  92. return paddr;
  93. }
  94. EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);
  95. /**
  96. * drm_fb_cma_fbdev_init() - Allocate and initialize fbdev emulation
  97. * @dev: DRM device
  98. * @preferred_bpp: Preferred bits per pixel for the device.
  99. * @dev->mode_config.preferred_depth is used if this is zero.
  100. * @max_conn_count: Maximum number of connectors.
  101. * @dev->mode_config.num_connector is used if this is zero.
  102. *
  103. * Returns:
  104. * Zero on success or negative error code on failure.
  105. */
  106. int drm_fb_cma_fbdev_init(struct drm_device *dev, unsigned int preferred_bpp,
  107. unsigned int max_conn_count)
  108. {
  109. struct drm_fbdev_cma *fbdev_cma;
  110. /* dev->fb_helper will indirectly point to fbdev_cma after this call */
  111. fbdev_cma = drm_fbdev_cma_init(dev, preferred_bpp, max_conn_count);
  112. if (IS_ERR(fbdev_cma))
  113. return PTR_ERR(fbdev_cma);
  114. return 0;
  115. }
  116. EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_init);
  117. /**
  118. * drm_fb_cma_fbdev_fini() - Teardown fbdev emulation
  119. * @dev: DRM device
  120. */
  121. void drm_fb_cma_fbdev_fini(struct drm_device *dev)
  122. {
  123. if (dev->fb_helper)
  124. drm_fbdev_cma_fini(to_fbdev_cma(dev->fb_helper));
  125. }
  126. EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_fini);
  127. static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
  128. .fb_probe = drm_fb_helper_generic_probe,
  129. };
  130. /**
  131. * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
  132. * @dev: DRM device
  133. * @preferred_bpp: Preferred bits per pixel for the device
  134. * @max_conn_count: Maximum number of connectors
  135. *
  136. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  137. */
  138. struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
  139. unsigned int preferred_bpp, unsigned int max_conn_count)
  140. {
  141. struct drm_fbdev_cma *fbdev_cma;
  142. struct drm_fb_helper *fb_helper;
  143. int ret;
  144. fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
  145. if (!fbdev_cma)
  146. return ERR_PTR(-ENOMEM);
  147. fb_helper = &fbdev_cma->fb_helper;
  148. ret = drm_client_init(dev, &fb_helper->client, "fbdev", NULL);
  149. if (ret)
  150. goto err_free;
  151. ret = drm_fb_helper_fbdev_setup(dev, fb_helper, &drm_fb_cma_helper_funcs,
  152. preferred_bpp, max_conn_count);
  153. if (ret)
  154. goto err_client_put;
  155. drm_client_add(&fb_helper->client);
  156. return fbdev_cma;
  157. err_client_put:
  158. drm_client_release(&fb_helper->client);
  159. err_free:
  160. kfree(fbdev_cma);
  161. return ERR_PTR(ret);
  162. }
  163. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
  164. /**
  165. * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
  166. * @fbdev_cma: The drm_fbdev_cma struct
  167. */
  168. void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
  169. {
  170. drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
  171. /* All resources have now been freed by drm_fbdev_fb_destroy() */
  172. }
  173. EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
  174. /**
  175. * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
  176. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  177. *
  178. * This function is usually called from the &drm_driver.lastclose callback.
  179. */
  180. void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
  181. {
  182. if (fbdev_cma)
  183. drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
  184. }
  185. EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
  186. /**
  187. * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
  188. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  189. *
  190. * This function is usually called from the &drm_mode_config.output_poll_changed
  191. * callback.
  192. */
  193. void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
  194. {
  195. if (fbdev_cma)
  196. drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
  197. }
  198. EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
  199. /**
  200. * drm_fbdev_cma_set_suspend_unlocked - wrapper around
  201. * drm_fb_helper_set_suspend_unlocked
  202. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  203. * @state: desired state, zero to resume, non-zero to suspend
  204. *
  205. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  206. * fb_set_suspend implemented by fbdev core.
  207. */
  208. void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma *fbdev_cma,
  209. bool state)
  210. {
  211. if (fbdev_cma)
  212. drm_fb_helper_set_suspend_unlocked(&fbdev_cma->fb_helper,
  213. state);
  214. }
  215. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked);