xref: /linux/drivers/gpu/drm/virtio/virtgpu_object.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * Copyright (C) 2015 Red Hat, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * 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 NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include "virtgpu_drv.h"
27 
28 static void virtio_gpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
29 {
30 	struct virtio_gpu_object *bo;
31 	struct virtio_gpu_device *vgdev;
32 
33 	bo = container_of(tbo, struct virtio_gpu_object, tbo);
34 	vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
35 
36 	if (bo->hw_res_handle)
37 		virtio_gpu_cmd_unref_resource(vgdev, bo->hw_res_handle);
38 	if (bo->pages)
39 		virtio_gpu_object_free_sg_table(bo);
40 	drm_gem_object_release(&bo->gem_base);
41 	kfree(bo);
42 }
43 
44 static void virtio_gpu_init_ttm_placement(struct virtio_gpu_object *vgbo,
45 					  bool pinned)
46 {
47 	u32 c = 1;
48 	u32 pflag = pinned ? TTM_PL_FLAG_NO_EVICT : 0;
49 
50 	vgbo->placement.placement = &vgbo->placement_code;
51 	vgbo->placement.busy_placement = &vgbo->placement_code;
52 	vgbo->placement_code.fpfn = 0;
53 	vgbo->placement_code.lpfn = 0;
54 	vgbo->placement_code.flags =
55 		TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT | pflag;
56 	vgbo->placement.num_placement = c;
57 	vgbo->placement.num_busy_placement = c;
58 
59 }
60 
61 int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
62 			     unsigned long size, bool kernel, bool pinned,
63 			     struct virtio_gpu_object **bo_ptr)
64 {
65 	struct virtio_gpu_object *bo;
66 	enum ttm_bo_type type;
67 	size_t acc_size;
68 	int ret;
69 
70 	if (kernel)
71 		type = ttm_bo_type_kernel;
72 	else
73 		type = ttm_bo_type_device;
74 	*bo_ptr = NULL;
75 
76 	acc_size = ttm_bo_dma_acc_size(&vgdev->mman.bdev, size,
77 				       sizeof(struct virtio_gpu_object));
78 
79 	bo = kzalloc(sizeof(struct virtio_gpu_object), GFP_KERNEL);
80 	if (bo == NULL)
81 		return -ENOMEM;
82 	size = roundup(size, PAGE_SIZE);
83 	ret = drm_gem_object_init(vgdev->ddev, &bo->gem_base, size);
84 	if (ret != 0)
85 		goto err_gem_init;
86 	bo->dumb = false;
87 	virtio_gpu_init_ttm_placement(bo, pinned);
88 
89 	ret = ttm_bo_init(&vgdev->mman.bdev, &bo->tbo, size, type,
90 			  &bo->placement, 0, !kernel, NULL, acc_size,
91 			  NULL, NULL, &virtio_gpu_ttm_bo_destroy);
92 	if (ret != 0)
93 		goto err_ttm_init;
94 
95 	*bo_ptr = bo;
96 	return 0;
97 
98 err_ttm_init:
99 	drm_gem_object_release(&bo->gem_base);
100 err_gem_init:
101 	kfree(bo);
102 	return ret;
103 }
104 
105 int virtio_gpu_object_kmap(struct virtio_gpu_object *bo, void **ptr)
106 {
107 	bool is_iomem;
108 	int r;
109 
110 	if (bo->vmap) {
111 		if (ptr)
112 			*ptr = bo->vmap;
113 		return 0;
114 	}
115 	r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
116 	if (r)
117 		return r;
118 	bo->vmap = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
119 	if (ptr)
120 		*ptr = bo->vmap;
121 	return 0;
122 }
123 
124 int virtio_gpu_object_get_sg_table(struct virtio_gpu_device *qdev,
125 				   struct virtio_gpu_object *bo)
126 {
127 	int ret;
128 	struct page **pages = bo->tbo.ttm->pages;
129 	int nr_pages = bo->tbo.num_pages;
130 
131 	/* wtf swapping */
132 	if (bo->pages)
133 		return 0;
134 
135 	if (bo->tbo.ttm->state == tt_unpopulated)
136 		bo->tbo.ttm->bdev->driver->ttm_tt_populate(bo->tbo.ttm);
137 	bo->pages = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
138 	if (!bo->pages)
139 		goto out;
140 
141 	ret = sg_alloc_table_from_pages(bo->pages, pages, nr_pages, 0,
142 					nr_pages << PAGE_SHIFT, GFP_KERNEL);
143 	if (ret)
144 		goto out;
145 	return 0;
146 out:
147 	kfree(bo->pages);
148 	bo->pages = NULL;
149 	return -ENOMEM;
150 }
151 
152 void virtio_gpu_object_free_sg_table(struct virtio_gpu_object *bo)
153 {
154 	sg_free_table(bo->pages);
155 	kfree(bo->pages);
156 	bo->pages = NULL;
157 }
158 
159 int virtio_gpu_object_wait(struct virtio_gpu_object *bo, bool no_wait)
160 {
161 	int r;
162 
163 	r = ttm_bo_reserve(&bo->tbo, true, no_wait, false, NULL);
164 	if (unlikely(r != 0))
165 		return r;
166 	r = ttm_bo_wait(&bo->tbo, true, true, no_wait);
167 	ttm_bo_unreserve(&bo->tbo);
168 	return r;
169 }
170 
171