rockchip_drm_gem.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  3. * Author:Mark Yao <mark.yao@rock-chips.com>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <drm/drm.h>
  15. #include <drm/drmP.h>
  16. #include <drm/drm_gem.h>
  17. #include <drm/drm_vma_manager.h>
  18. #include <linux/dma-attrs.h>
  19. #include "rockchip_drm_drv.h"
  20. #include "rockchip_drm_gem.h"
  21. static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj,
  22. bool alloc_kmap)
  23. {
  24. struct drm_gem_object *obj = &rk_obj->base;
  25. struct drm_device *drm = obj->dev;
  26. init_dma_attrs(&rk_obj->dma_attrs);
  27. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &rk_obj->dma_attrs);
  28. if (!alloc_kmap)
  29. dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs);
  30. rk_obj->kvaddr = dma_alloc_attrs(drm->dev, obj->size,
  31. &rk_obj->dma_addr, GFP_KERNEL,
  32. &rk_obj->dma_attrs);
  33. if (!rk_obj->kvaddr) {
  34. DRM_ERROR("failed to allocate %#x byte dma buffer", obj->size);
  35. return -ENOMEM;
  36. }
  37. return 0;
  38. }
  39. static void rockchip_gem_free_buf(struct rockchip_gem_object *rk_obj)
  40. {
  41. struct drm_gem_object *obj = &rk_obj->base;
  42. struct drm_device *drm = obj->dev;
  43. dma_free_attrs(drm->dev, obj->size, rk_obj->kvaddr, rk_obj->dma_addr,
  44. &rk_obj->dma_attrs);
  45. }
  46. static int rockchip_drm_gem_object_mmap(struct drm_gem_object *obj,
  47. struct vm_area_struct *vma)
  48. {
  49. int ret;
  50. struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
  51. struct drm_device *drm = obj->dev;
  52. /*
  53. * dma_alloc_attrs() allocated a struct page table for rk_obj, so clear
  54. * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
  55. */
  56. vma->vm_flags &= ~VM_PFNMAP;
  57. ret = dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr,
  58. obj->size, &rk_obj->dma_attrs);
  59. if (ret)
  60. drm_gem_vm_close(vma);
  61. return ret;
  62. }
  63. int rockchip_gem_mmap_buf(struct drm_gem_object *obj,
  64. struct vm_area_struct *vma)
  65. {
  66. int ret;
  67. ret = drm_gem_mmap_obj(obj, obj->size, vma);
  68. if (ret)
  69. return ret;
  70. return rockchip_drm_gem_object_mmap(obj, vma);
  71. }
  72. /* drm driver mmap file operations */
  73. int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  74. {
  75. struct drm_gem_object *obj;
  76. int ret;
  77. ret = drm_gem_mmap(filp, vma);
  78. if (ret)
  79. return ret;
  80. /*
  81. * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the
  82. * whole buffer from the start.
  83. */
  84. vma->vm_pgoff = 0;
  85. obj = vma->vm_private_data;
  86. return rockchip_drm_gem_object_mmap(obj, vma);
  87. }
  88. struct rockchip_gem_object *
  89. rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
  90. bool alloc_kmap)
  91. {
  92. struct rockchip_gem_object *rk_obj;
  93. struct drm_gem_object *obj;
  94. int ret;
  95. size = round_up(size, PAGE_SIZE);
  96. rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
  97. if (!rk_obj)
  98. return ERR_PTR(-ENOMEM);
  99. obj = &rk_obj->base;
  100. drm_gem_private_object_init(drm, obj, size);
  101. ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap);
  102. if (ret)
  103. goto err_free_rk_obj;
  104. return rk_obj;
  105. err_free_rk_obj:
  106. kfree(rk_obj);
  107. return ERR_PTR(ret);
  108. }
  109. /*
  110. * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
  111. * function
  112. */
  113. void rockchip_gem_free_object(struct drm_gem_object *obj)
  114. {
  115. struct rockchip_gem_object *rk_obj;
  116. drm_gem_free_mmap_offset(obj);
  117. rk_obj = to_rockchip_obj(obj);
  118. rockchip_gem_free_buf(rk_obj);
  119. kfree(rk_obj);
  120. }
  121. /*
  122. * rockchip_gem_create_with_handle - allocate an object with the given
  123. * size and create a gem handle on it
  124. *
  125. * returns a struct rockchip_gem_object* on success or ERR_PTR values
  126. * on failure.
  127. */
  128. static struct rockchip_gem_object *
  129. rockchip_gem_create_with_handle(struct drm_file *file_priv,
  130. struct drm_device *drm, unsigned int size,
  131. unsigned int *handle)
  132. {
  133. struct rockchip_gem_object *rk_obj;
  134. struct drm_gem_object *obj;
  135. int ret;
  136. rk_obj = rockchip_gem_create_object(drm, size, false);
  137. if (IS_ERR(rk_obj))
  138. return ERR_CAST(rk_obj);
  139. obj = &rk_obj->base;
  140. /*
  141. * allocate a id of idr table where the obj is registered
  142. * and handle has the id what user can see.
  143. */
  144. ret = drm_gem_handle_create(file_priv, obj, handle);
  145. if (ret)
  146. goto err_handle_create;
  147. /* drop reference from allocate - handle holds it now. */
  148. drm_gem_object_unreference_unlocked(obj);
  149. return rk_obj;
  150. err_handle_create:
  151. rockchip_gem_free_object(obj);
  152. return ERR_PTR(ret);
  153. }
  154. int rockchip_gem_dumb_map_offset(struct drm_file *file_priv,
  155. struct drm_device *dev, uint32_t handle,
  156. uint64_t *offset)
  157. {
  158. struct drm_gem_object *obj;
  159. int ret;
  160. obj = drm_gem_object_lookup(dev, file_priv, handle);
  161. if (!obj) {
  162. DRM_ERROR("failed to lookup gem object.\n");
  163. return -EINVAL;
  164. }
  165. ret = drm_gem_create_mmap_offset(obj);
  166. if (ret)
  167. goto out;
  168. *offset = drm_vma_node_offset_addr(&obj->vma_node);
  169. DRM_DEBUG_KMS("offset = 0x%llx\n", *offset);
  170. out:
  171. drm_gem_object_unreference_unlocked(obj);
  172. return 0;
  173. }
  174. /*
  175. * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
  176. * function
  177. *
  178. * This aligns the pitch and size arguments to the minimum required. wrap
  179. * this into your own function if you need bigger alignment.
  180. */
  181. int rockchip_gem_dumb_create(struct drm_file *file_priv,
  182. struct drm_device *dev,
  183. struct drm_mode_create_dumb *args)
  184. {
  185. struct rockchip_gem_object *rk_obj;
  186. int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  187. /*
  188. * align to 64 bytes since Mali requires it.
  189. */
  190. min_pitch = ALIGN(min_pitch, 64);
  191. if (args->pitch < min_pitch)
  192. args->pitch = min_pitch;
  193. if (args->size < args->pitch * args->height)
  194. args->size = args->pitch * args->height;
  195. rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size,
  196. &args->handle);
  197. return PTR_ERR_OR_ZERO(rk_obj);
  198. }
  199. /*
  200. * Allocate a sg_table for this GEM object.
  201. * Note: Both the table's contents, and the sg_table itself must be freed by
  202. * the caller.
  203. * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
  204. */
  205. struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj)
  206. {
  207. struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
  208. struct drm_device *drm = obj->dev;
  209. struct sg_table *sgt;
  210. int ret;
  211. sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
  212. if (!sgt)
  213. return ERR_PTR(-ENOMEM);
  214. ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr,
  215. rk_obj->dma_addr, obj->size,
  216. &rk_obj->dma_attrs);
  217. if (ret) {
  218. DRM_ERROR("failed to allocate sgt, %d\n", ret);
  219. kfree(sgt);
  220. return ERR_PTR(ret);
  221. }
  222. return sgt;
  223. }
  224. void *rockchip_gem_prime_vmap(struct drm_gem_object *obj)
  225. {
  226. struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
  227. if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs))
  228. return NULL;
  229. return rk_obj->kvaddr;
  230. }
  231. void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
  232. {
  233. /* Nothing to do */
  234. }