vmwgfx_gem.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /*
  3. * Copyright (c) 2021-2024 Broadcom. All Rights Reserved. The term
  4. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. *
  26. */
  27. #include "vmwgfx_bo.h"
  28. #include "vmwgfx_drv.h"
  29. #include "drm/drm_prime.h"
  30. #include "drm/drm_gem_ttm_helper.h"
  31. #include <linux/debugfs.h>
  32. static void vmw_gem_object_free(struct drm_gem_object *gobj)
  33. {
  34. struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gobj);
  35. if (bo)
  36. ttm_bo_fini(bo);
  37. }
  38. static int vmw_gem_object_open(struct drm_gem_object *obj,
  39. struct drm_file *file_priv)
  40. {
  41. return 0;
  42. }
  43. static void vmw_gem_object_close(struct drm_gem_object *obj,
  44. struct drm_file *file_priv)
  45. {
  46. }
  47. static int vmw_gem_object_pin(struct drm_gem_object *obj)
  48. {
  49. struct vmw_bo *vbo = to_vmw_bo(obj);
  50. vmw_bo_pin_reserved(vbo, true);
  51. return 0;
  52. }
  53. static void vmw_gem_object_unpin(struct drm_gem_object *obj)
  54. {
  55. struct vmw_bo *vbo = to_vmw_bo(obj);
  56. vmw_bo_pin_reserved(vbo, false);
  57. }
  58. static struct sg_table *vmw_gem_object_get_sg_table(struct drm_gem_object *obj)
  59. {
  60. struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(obj);
  61. struct vmw_ttm_tt *vmw_tt =
  62. container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
  63. if (vmw_tt->vsgt.sgt)
  64. return vmw_tt->vsgt.sgt;
  65. return drm_prime_pages_to_sg(obj->dev, vmw_tt->dma_ttm.pages, vmw_tt->dma_ttm.num_pages);
  66. }
  67. static int vmw_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map)
  68. {
  69. struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(obj);
  70. int ret;
  71. if (drm_gem_is_imported(obj)) {
  72. ret = dma_buf_vmap(obj->import_attach->dmabuf, map);
  73. if (!ret) {
  74. if (drm_WARN_ON(obj->dev, map->is_iomem)) {
  75. dma_buf_vunmap(obj->import_attach->dmabuf, map);
  76. return -EIO;
  77. }
  78. }
  79. } else {
  80. ret = ttm_bo_vmap(bo, map);
  81. }
  82. return ret;
  83. }
  84. static void vmw_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
  85. {
  86. if (drm_gem_is_imported(obj))
  87. dma_buf_vunmap(obj->import_attach->dmabuf, map);
  88. else
  89. drm_gem_ttm_vunmap(obj, map);
  90. }
  91. static int vmw_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  92. {
  93. int ret;
  94. if (drm_gem_is_imported(obj)) {
  95. /*
  96. * Reset both vm_ops and vm_private_data, so we don't end up with
  97. * vm_ops pointing to our implementation if the dma-buf backend
  98. * doesn't set those fields.
  99. */
  100. vma->vm_private_data = NULL;
  101. vma->vm_ops = NULL;
  102. ret = dma_buf_mmap(obj->dma_buf, vma, 0);
  103. /* Drop the reference drm_gem_mmap_obj() acquired.*/
  104. if (!ret)
  105. drm_gem_object_put(obj);
  106. return ret;
  107. }
  108. return drm_gem_ttm_mmap(obj, vma);
  109. }
  110. static const struct vm_operations_struct vmw_vm_ops = {
  111. .pfn_mkwrite = vmw_bo_vm_mkwrite,
  112. .page_mkwrite = vmw_bo_vm_mkwrite,
  113. .fault = vmw_bo_vm_fault,
  114. .open = ttm_bo_vm_open,
  115. .close = ttm_bo_vm_close,
  116. };
  117. const struct drm_gem_object_funcs vmw_gem_object_funcs = {
  118. .free = vmw_gem_object_free,
  119. .open = vmw_gem_object_open,
  120. .close = vmw_gem_object_close,
  121. .print_info = drm_gem_ttm_print_info,
  122. .pin = vmw_gem_object_pin,
  123. .unpin = vmw_gem_object_unpin,
  124. .get_sg_table = vmw_gem_object_get_sg_table,
  125. .vmap = vmw_gem_vmap,
  126. .vunmap = vmw_gem_vunmap,
  127. .mmap = vmw_gem_mmap,
  128. .vm_ops = &vmw_vm_ops,
  129. };
  130. int vmw_gem_object_create_with_handle(struct vmw_private *dev_priv,
  131. struct drm_file *filp,
  132. uint32_t size,
  133. uint32_t *handle,
  134. struct vmw_bo **p_vbo)
  135. {
  136. int ret;
  137. struct vmw_bo_params params = {
  138. .domain = (dev_priv->has_mob) ? VMW_BO_DOMAIN_SYS : VMW_BO_DOMAIN_VRAM,
  139. .busy_domain = VMW_BO_DOMAIN_SYS,
  140. .bo_type = ttm_bo_type_device,
  141. .size = size,
  142. .pin = false
  143. };
  144. ret = vmw_bo_create(dev_priv, &params, p_vbo);
  145. if (ret != 0)
  146. goto out_no_bo;
  147. ret = drm_gem_handle_create(filp, &(*p_vbo)->tbo.base, handle);
  148. out_no_bo:
  149. return ret;
  150. }
  151. struct drm_gem_object *vmw_prime_import_sg_table(struct drm_device *dev,
  152. struct dma_buf_attachment *attach,
  153. struct sg_table *table)
  154. {
  155. int ret;
  156. struct vmw_private *dev_priv = vmw_priv(dev);
  157. struct drm_gem_object *gem = NULL;
  158. struct vmw_bo *vbo;
  159. struct vmw_bo_params params = {
  160. .domain = (dev_priv->has_mob) ? VMW_BO_DOMAIN_SYS : VMW_BO_DOMAIN_VRAM,
  161. .busy_domain = VMW_BO_DOMAIN_SYS,
  162. .bo_type = ttm_bo_type_sg,
  163. .size = attach->dmabuf->size,
  164. .pin = false,
  165. .keep_resv = true,
  166. .resv = attach->dmabuf->resv,
  167. .sg = table,
  168. };
  169. dma_resv_lock(params.resv, NULL);
  170. ret = vmw_bo_create(dev_priv, &params, &vbo);
  171. if (ret != 0)
  172. goto out_no_bo;
  173. vbo->tbo.base.funcs = &vmw_gem_object_funcs;
  174. gem = &vbo->tbo.base;
  175. out_no_bo:
  176. dma_resv_unlock(params.resv);
  177. return gem;
  178. }
  179. int vmw_gem_object_create_ioctl(struct drm_device *dev, void *data,
  180. struct drm_file *filp)
  181. {
  182. struct vmw_private *dev_priv = vmw_priv(dev);
  183. union drm_vmw_alloc_dmabuf_arg *arg =
  184. (union drm_vmw_alloc_dmabuf_arg *)data;
  185. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  186. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  187. struct vmw_bo *vbo;
  188. uint32_t handle;
  189. int ret;
  190. ret = vmw_gem_object_create_with_handle(dev_priv, filp,
  191. req->size, &handle, &vbo);
  192. if (ret)
  193. goto out_no_bo;
  194. rep->handle = handle;
  195. rep->map_handle = drm_vma_node_offset_addr(&vbo->tbo.base.vma_node);
  196. rep->cur_gmr_id = handle;
  197. rep->cur_gmr_offset = 0;
  198. /* drop reference from allocate - handle holds it now */
  199. drm_gem_object_put(&vbo->tbo.base);
  200. out_no_bo:
  201. return ret;
  202. }
  203. #if defined(CONFIG_DEBUG_FS)
  204. static void vmw_bo_print_info(int id, struct vmw_bo *bo, struct seq_file *m)
  205. {
  206. const char *placement;
  207. const char *type;
  208. switch (bo->tbo.resource->mem_type) {
  209. case TTM_PL_SYSTEM:
  210. placement = " CPU";
  211. break;
  212. case VMW_PL_GMR:
  213. placement = " GMR";
  214. break;
  215. case VMW_PL_MOB:
  216. placement = " MOB";
  217. break;
  218. case VMW_PL_SYSTEM:
  219. placement = "VCPU";
  220. break;
  221. case TTM_PL_VRAM:
  222. placement = "VRAM";
  223. break;
  224. default:
  225. placement = "None";
  226. break;
  227. }
  228. switch (bo->tbo.type) {
  229. case ttm_bo_type_device:
  230. type = "device";
  231. break;
  232. case ttm_bo_type_kernel:
  233. type = "kernel";
  234. break;
  235. case ttm_bo_type_sg:
  236. type = "sg ";
  237. break;
  238. default:
  239. type = "none ";
  240. break;
  241. }
  242. seq_printf(m, "\t\t0x%08x: %12zu bytes %s, type = %s",
  243. id, bo->tbo.base.size, placement, type);
  244. seq_printf(m, ", priority = %u, pin_count = %u, GEM refs = %d",
  245. bo->tbo.priority,
  246. bo->tbo.pin_count,
  247. kref_read(&bo->tbo.base.refcount));
  248. seq_puts(m, "\n");
  249. }
  250. static int vmw_debugfs_gem_info_show(struct seq_file *m, void *unused)
  251. {
  252. struct vmw_private *vdev = (struct vmw_private *)m->private;
  253. struct drm_device *dev = &vdev->drm;
  254. struct drm_file *file;
  255. int r;
  256. r = mutex_lock_interruptible(&dev->filelist_mutex);
  257. if (r)
  258. return r;
  259. list_for_each_entry(file, &dev->filelist, lhead) {
  260. struct task_struct *task;
  261. struct drm_gem_object *gobj;
  262. struct pid *pid;
  263. int id;
  264. /*
  265. * Although we have a valid reference on file->pid, that does
  266. * not guarantee that the task_struct who called get_pid() is
  267. * still alive (e.g. get_pid(current) => fork() => exit()).
  268. * Therefore, we need to protect this ->comm access using RCU.
  269. */
  270. rcu_read_lock();
  271. pid = rcu_dereference(file->pid);
  272. task = pid_task(pid, PIDTYPE_TGID);
  273. seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
  274. task ? task->comm : "<unknown>");
  275. rcu_read_unlock();
  276. spin_lock(&file->table_lock);
  277. idr_for_each_entry(&file->object_idr, gobj, id) {
  278. struct vmw_bo *bo = to_vmw_bo(gobj);
  279. vmw_bo_print_info(id, bo, m);
  280. }
  281. spin_unlock(&file->table_lock);
  282. }
  283. mutex_unlock(&dev->filelist_mutex);
  284. return 0;
  285. }
  286. DEFINE_SHOW_ATTRIBUTE(vmw_debugfs_gem_info);
  287. #endif
  288. void vmw_debugfs_gem_init(struct vmw_private *vdev)
  289. {
  290. #if defined(CONFIG_DEBUG_FS)
  291. struct drm_minor *minor = vdev->drm.primary;
  292. struct dentry *root = minor->debugfs_root;
  293. debugfs_create_file("vmwgfx_gem_info", 0444, root, vdev,
  294. &vmw_debugfs_gem_info_fops);
  295. #endif
  296. }