vmwgfx_gem.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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_put(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 (obj->import_attach) {
  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 (obj->import_attach)
  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 (obj->import_attach) {
  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. static 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(struct vmw_private *vmw,
  131. struct vmw_bo_params *params,
  132. struct vmw_bo **p_vbo)
  133. {
  134. int ret = vmw_bo_create(vmw, params, p_vbo);
  135. if (ret != 0)
  136. goto out_no_bo;
  137. (*p_vbo)->tbo.base.funcs = &vmw_gem_object_funcs;
  138. out_no_bo:
  139. return ret;
  140. }
  141. int vmw_gem_object_create_with_handle(struct vmw_private *dev_priv,
  142. struct drm_file *filp,
  143. uint32_t size,
  144. uint32_t *handle,
  145. struct vmw_bo **p_vbo)
  146. {
  147. int ret;
  148. struct vmw_bo_params params = {
  149. .domain = (dev_priv->has_mob) ? VMW_BO_DOMAIN_SYS : VMW_BO_DOMAIN_VRAM,
  150. .busy_domain = VMW_BO_DOMAIN_SYS,
  151. .bo_type = ttm_bo_type_device,
  152. .size = size,
  153. .pin = false
  154. };
  155. ret = vmw_gem_object_create(dev_priv, &params, p_vbo);
  156. if (ret != 0)
  157. goto out_no_bo;
  158. ret = drm_gem_handle_create(filp, &(*p_vbo)->tbo.base, handle);
  159. out_no_bo:
  160. return ret;
  161. }
  162. struct drm_gem_object *vmw_prime_import_sg_table(struct drm_device *dev,
  163. struct dma_buf_attachment *attach,
  164. struct sg_table *table)
  165. {
  166. int ret;
  167. struct vmw_private *dev_priv = vmw_priv(dev);
  168. struct drm_gem_object *gem = NULL;
  169. struct vmw_bo *vbo;
  170. struct vmw_bo_params params = {
  171. .domain = (dev_priv->has_mob) ? VMW_BO_DOMAIN_SYS : VMW_BO_DOMAIN_VRAM,
  172. .busy_domain = VMW_BO_DOMAIN_SYS,
  173. .bo_type = ttm_bo_type_sg,
  174. .size = attach->dmabuf->size,
  175. .pin = false,
  176. .keep_resv = true,
  177. .resv = attach->dmabuf->resv,
  178. .sg = table,
  179. };
  180. dma_resv_lock(params.resv, NULL);
  181. ret = vmw_bo_create(dev_priv, &params, &vbo);
  182. if (ret != 0)
  183. goto out_no_bo;
  184. vbo->tbo.base.funcs = &vmw_gem_object_funcs;
  185. gem = &vbo->tbo.base;
  186. out_no_bo:
  187. dma_resv_unlock(params.resv);
  188. return gem;
  189. }
  190. int vmw_gem_object_create_ioctl(struct drm_device *dev, void *data,
  191. struct drm_file *filp)
  192. {
  193. struct vmw_private *dev_priv = vmw_priv(dev);
  194. union drm_vmw_alloc_dmabuf_arg *arg =
  195. (union drm_vmw_alloc_dmabuf_arg *)data;
  196. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  197. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  198. struct vmw_bo *vbo;
  199. uint32_t handle;
  200. int ret;
  201. ret = vmw_gem_object_create_with_handle(dev_priv, filp,
  202. req->size, &handle, &vbo);
  203. if (ret)
  204. goto out_no_bo;
  205. rep->handle = handle;
  206. rep->map_handle = drm_vma_node_offset_addr(&vbo->tbo.base.vma_node);
  207. rep->cur_gmr_id = handle;
  208. rep->cur_gmr_offset = 0;
  209. /* drop reference from allocate - handle holds it now */
  210. drm_gem_object_put(&vbo->tbo.base);
  211. out_no_bo:
  212. return ret;
  213. }
  214. #if defined(CONFIG_DEBUG_FS)
  215. static void vmw_bo_print_info(int id, struct vmw_bo *bo, struct seq_file *m)
  216. {
  217. const char *placement;
  218. const char *type;
  219. switch (bo->tbo.resource->mem_type) {
  220. case TTM_PL_SYSTEM:
  221. placement = " CPU";
  222. break;
  223. case VMW_PL_GMR:
  224. placement = " GMR";
  225. break;
  226. case VMW_PL_MOB:
  227. placement = " MOB";
  228. break;
  229. case VMW_PL_SYSTEM:
  230. placement = "VCPU";
  231. break;
  232. case TTM_PL_VRAM:
  233. placement = "VRAM";
  234. break;
  235. default:
  236. placement = "None";
  237. break;
  238. }
  239. switch (bo->tbo.type) {
  240. case ttm_bo_type_device:
  241. type = "device";
  242. break;
  243. case ttm_bo_type_kernel:
  244. type = "kernel";
  245. break;
  246. case ttm_bo_type_sg:
  247. type = "sg ";
  248. break;
  249. default:
  250. type = "none ";
  251. break;
  252. }
  253. seq_printf(m, "\t\t0x%08x: %12zu bytes %s, type = %s",
  254. id, bo->tbo.base.size, placement, type);
  255. seq_printf(m, ", priority = %u, pin_count = %u, GEM refs = %d, TTM refs = %d",
  256. bo->tbo.priority,
  257. bo->tbo.pin_count,
  258. kref_read(&bo->tbo.base.refcount),
  259. kref_read(&bo->tbo.kref));
  260. seq_puts(m, "\n");
  261. }
  262. static int vmw_debugfs_gem_info_show(struct seq_file *m, void *unused)
  263. {
  264. struct vmw_private *vdev = (struct vmw_private *)m->private;
  265. struct drm_device *dev = &vdev->drm;
  266. struct drm_file *file;
  267. int r;
  268. r = mutex_lock_interruptible(&dev->filelist_mutex);
  269. if (r)
  270. return r;
  271. list_for_each_entry(file, &dev->filelist, lhead) {
  272. struct task_struct *task;
  273. struct drm_gem_object *gobj;
  274. struct pid *pid;
  275. int id;
  276. /*
  277. * Although we have a valid reference on file->pid, that does
  278. * not guarantee that the task_struct who called get_pid() is
  279. * still alive (e.g. get_pid(current) => fork() => exit()).
  280. * Therefore, we need to protect this ->comm access using RCU.
  281. */
  282. rcu_read_lock();
  283. pid = rcu_dereference(file->pid);
  284. task = pid_task(pid, PIDTYPE_TGID);
  285. seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
  286. task ? task->comm : "<unknown>");
  287. rcu_read_unlock();
  288. spin_lock(&file->table_lock);
  289. idr_for_each_entry(&file->object_idr, gobj, id) {
  290. struct vmw_bo *bo = to_vmw_bo(gobj);
  291. vmw_bo_print_info(id, bo, m);
  292. }
  293. spin_unlock(&file->table_lock);
  294. }
  295. mutex_unlock(&dev->filelist_mutex);
  296. return 0;
  297. }
  298. DEFINE_SHOW_ATTRIBUTE(vmw_debugfs_gem_info);
  299. #endif
  300. void vmw_debugfs_gem_init(struct vmw_private *vdev)
  301. {
  302. #if defined(CONFIG_DEBUG_FS)
  303. struct drm_minor *minor = vdev->drm.primary;
  304. struct dentry *root = minor->debugfs_root;
  305. debugfs_create_file("vmwgfx_gem_info", 0444, root, vdev,
  306. &vmw_debugfs_gem_info_fops);
  307. #endif
  308. }