Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
28static int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
29 uint32_t *resid)
30{
31 int handle = ida_alloc(&vgdev->resource_ida, GFP_KERNEL);
32
33 if (handle < 0)
34 return handle;
35
36 *resid = handle + 1;
37 return 0;
38}
39
40static void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t id)
41{
42 ida_free(&vgdev->resource_ida, id - 1);
43}
44
45static void virtio_gpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
46{
47 struct virtio_gpu_object *bo;
48 struct virtio_gpu_device *vgdev;
49
50 bo = container_of(tbo, struct virtio_gpu_object, tbo);
51 vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
52
53 if (bo->created)
54 virtio_gpu_cmd_unref_resource(vgdev, bo->hw_res_handle);
55 if (bo->pages)
56 virtio_gpu_object_free_sg_table(bo);
57 if (bo->vmap)
58 virtio_gpu_object_kunmap(bo);
59 drm_gem_object_release(&bo->gem_base);
60 virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
61 kfree(bo);
62}
63
64static void virtio_gpu_init_ttm_placement(struct virtio_gpu_object *vgbo,
65 bool pinned)
66{
67 u32 c = 1;
68 u32 pflag = pinned ? TTM_PL_FLAG_NO_EVICT : 0;
69
70 vgbo->placement.placement = &vgbo->placement_code;
71 vgbo->placement.busy_placement = &vgbo->placement_code;
72 vgbo->placement_code.fpfn = 0;
73 vgbo->placement_code.lpfn = 0;
74 vgbo->placement_code.flags =
75 TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT | pflag;
76 vgbo->placement.num_placement = c;
77 vgbo->placement.num_busy_placement = c;
78
79}
80
81int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
82 unsigned long size, bool kernel, bool pinned,
83 struct virtio_gpu_object **bo_ptr)
84{
85 struct virtio_gpu_object *bo;
86 enum ttm_bo_type type;
87 size_t acc_size;
88 int ret;
89
90 if (kernel)
91 type = ttm_bo_type_kernel;
92 else
93 type = ttm_bo_type_device;
94 *bo_ptr = NULL;
95
96 acc_size = ttm_bo_dma_acc_size(&vgdev->mman.bdev, size,
97 sizeof(struct virtio_gpu_object));
98
99 bo = kzalloc(sizeof(struct virtio_gpu_object), GFP_KERNEL);
100 if (bo == NULL)
101 return -ENOMEM;
102 ret = virtio_gpu_resource_id_get(vgdev, &bo->hw_res_handle);
103 if (ret < 0) {
104 kfree(bo);
105 return ret;
106 }
107 size = roundup(size, PAGE_SIZE);
108 ret = drm_gem_object_init(vgdev->ddev, &bo->gem_base, size);
109 if (ret != 0) {
110 virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
111 kfree(bo);
112 return ret;
113 }
114 bo->dumb = false;
115 virtio_gpu_init_ttm_placement(bo, pinned);
116
117 ret = ttm_bo_init(&vgdev->mman.bdev, &bo->tbo, size, type,
118 &bo->placement, 0, !kernel, acc_size,
119 NULL, NULL, &virtio_gpu_ttm_bo_destroy);
120 /* ttm_bo_init failure will call the destroy */
121 if (ret != 0)
122 return ret;
123
124 *bo_ptr = bo;
125 return 0;
126}
127
128void virtio_gpu_object_kunmap(struct virtio_gpu_object *bo)
129{
130 bo->vmap = NULL;
131 ttm_bo_kunmap(&bo->kmap);
132}
133
134int virtio_gpu_object_kmap(struct virtio_gpu_object *bo)
135{
136 bool is_iomem;
137 int r;
138
139 WARN_ON(bo->vmap);
140
141 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
142 if (r)
143 return r;
144 bo->vmap = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
145 return 0;
146}
147
148int virtio_gpu_object_get_sg_table(struct virtio_gpu_device *qdev,
149 struct virtio_gpu_object *bo)
150{
151 int ret;
152 struct page **pages = bo->tbo.ttm->pages;
153 int nr_pages = bo->tbo.num_pages;
154 struct ttm_operation_ctx ctx = {
155 .interruptible = false,
156 .no_wait_gpu = false
157 };
158
159 /* wtf swapping */
160 if (bo->pages)
161 return 0;
162
163 if (bo->tbo.ttm->state == tt_unpopulated)
164 bo->tbo.ttm->bdev->driver->ttm_tt_populate(bo->tbo.ttm, &ctx);
165 bo->pages = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
166 if (!bo->pages)
167 goto out;
168
169 ret = sg_alloc_table_from_pages(bo->pages, pages, nr_pages, 0,
170 nr_pages << PAGE_SHIFT, GFP_KERNEL);
171 if (ret)
172 goto out;
173 return 0;
174out:
175 kfree(bo->pages);
176 bo->pages = NULL;
177 return -ENOMEM;
178}
179
180void virtio_gpu_object_free_sg_table(struct virtio_gpu_object *bo)
181{
182 sg_free_table(bo->pages);
183 kfree(bo->pages);
184 bo->pages = NULL;
185}
186
187int virtio_gpu_object_wait(struct virtio_gpu_object *bo, bool no_wait)
188{
189 int r;
190
191 r = ttm_bo_reserve(&bo->tbo, true, no_wait, NULL);
192 if (unlikely(r != 0))
193 return r;
194 r = ttm_bo_wait(&bo->tbo, true, no_wait);
195 ttm_bo_unreserve(&bo->tbo);
196 return r;
197}
198