2 * Copyright (C) 2015 Red Hat, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 #include <linux/delay.h>
31 #include <drm/drm_file.h>
32 #include <drm/ttm/ttm_bo_api.h>
33 #include <drm/ttm/ttm_bo_driver.h>
34 #include <drm/ttm/ttm_module.h>
35 #include <drm/ttm/ttm_page_alloc.h>
36 #include <drm/ttm/ttm_placement.h>
37 #include <drm/virtgpu_drm.h>
39 #include "virtgpu_drv.h"
42 virtio_gpu_device *virtio_gpu_get_vgdev(struct ttm_bo_device *bdev)
44 struct virtio_gpu_mman *mman;
45 struct virtio_gpu_device *vgdev;
47 mman = container_of(bdev, struct virtio_gpu_mman, bdev);
48 vgdev = container_of(mman, struct virtio_gpu_device, mman);
52 int virtio_gpu_mmap(struct file *filp, struct vm_area_struct *vma)
54 struct drm_file *file_priv;
55 struct virtio_gpu_device *vgdev;
58 file_priv = filp->private_data;
59 vgdev = file_priv->minor->dev->dev_private;
62 "filp->private_data->minor->dev->dev_private == NULL\n");
65 r = ttm_bo_mmap(filp, vma, &vgdev->mman.bdev);
70 static int virtio_gpu_invalidate_caches(struct ttm_bo_device *bdev,
76 static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
77 struct ttm_buffer_object *bo,
78 const struct ttm_place *place,
79 struct ttm_mem_reg *mem)
81 mem->mm_node = (void *)1;
85 static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
86 struct ttm_mem_reg *mem)
88 mem->mm_node = (void *)NULL;
91 static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
97 static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
102 static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
103 struct drm_printer *printer)
107 static const struct ttm_mem_type_manager_func virtio_gpu_bo_manager_func = {
108 .init = ttm_bo_man_init,
109 .takedown = ttm_bo_man_takedown,
110 .get_node = ttm_bo_man_get_node,
111 .put_node = ttm_bo_man_put_node,
112 .debug = ttm_bo_man_debug
115 static int virtio_gpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
116 struct ttm_mem_type_manager *man)
121 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
122 man->available_caching = TTM_PL_MASK_CACHING;
123 man->default_caching = TTM_PL_FLAG_CACHED;
126 man->func = &virtio_gpu_bo_manager_func;
127 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
128 man->available_caching = TTM_PL_MASK_CACHING;
129 man->default_caching = TTM_PL_FLAG_CACHED;
132 DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
138 static void virtio_gpu_evict_flags(struct ttm_buffer_object *bo,
139 struct ttm_placement *placement)
141 static const struct ttm_place placements = {
144 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM,
147 placement->placement = &placements;
148 placement->busy_placement = &placements;
149 placement->num_placement = 1;
150 placement->num_busy_placement = 1;
153 static int virtio_gpu_verify_access(struct ttm_buffer_object *bo,
159 static int virtio_gpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
160 struct ttm_mem_reg *mem)
162 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
164 mem->bus.addr = NULL;
166 mem->bus.size = mem->num_pages << PAGE_SHIFT;
168 mem->bus.is_iomem = false;
169 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
171 switch (mem->mem_type) {
182 static void virtio_gpu_ttm_io_mem_free(struct ttm_bo_device *bdev,
183 struct ttm_mem_reg *mem)
188 * TTM backend functions.
190 struct virtio_gpu_ttm_tt {
191 struct ttm_dma_tt ttm;
192 struct virtio_gpu_object *obj;
195 static int virtio_gpu_ttm_tt_bind(struct ttm_tt *ttm,
196 struct ttm_mem_reg *bo_mem)
198 struct virtio_gpu_ttm_tt *gtt =
199 container_of(ttm, struct virtio_gpu_ttm_tt, ttm.ttm);
200 struct virtio_gpu_device *vgdev =
201 virtio_gpu_get_vgdev(gtt->obj->tbo.bdev);
203 virtio_gpu_object_attach(vgdev, gtt->obj, NULL);
207 static int virtio_gpu_ttm_tt_unbind(struct ttm_tt *ttm)
209 struct virtio_gpu_ttm_tt *gtt =
210 container_of(ttm, struct virtio_gpu_ttm_tt, ttm.ttm);
211 struct virtio_gpu_device *vgdev =
212 virtio_gpu_get_vgdev(gtt->obj->tbo.bdev);
214 virtio_gpu_object_detach(vgdev, gtt->obj);
218 static void virtio_gpu_ttm_tt_destroy(struct ttm_tt *ttm)
220 struct virtio_gpu_ttm_tt *gtt =
221 container_of(ttm, struct virtio_gpu_ttm_tt, ttm.ttm);
223 ttm_dma_tt_fini(>t->ttm);
227 static struct ttm_backend_func virtio_gpu_tt_func = {
228 .bind = &virtio_gpu_ttm_tt_bind,
229 .unbind = &virtio_gpu_ttm_tt_unbind,
230 .destroy = &virtio_gpu_ttm_tt_destroy,
233 static struct ttm_tt *virtio_gpu_ttm_tt_create(struct ttm_buffer_object *bo,
236 struct virtio_gpu_device *vgdev;
237 struct virtio_gpu_ttm_tt *gtt;
239 vgdev = virtio_gpu_get_vgdev(bo->bdev);
240 gtt = kzalloc(sizeof(struct virtio_gpu_ttm_tt), GFP_KERNEL);
243 gtt->ttm.ttm.func = &virtio_gpu_tt_func;
244 gtt->obj = container_of(bo, struct virtio_gpu_object, tbo);
245 if (ttm_dma_tt_init(>t->ttm, bo, page_flags)) {
249 return >t->ttm.ttm;
252 static void virtio_gpu_bo_swap_notify(struct ttm_buffer_object *tbo)
254 struct virtio_gpu_object *bo;
256 bo = container_of(tbo, struct virtio_gpu_object, tbo);
259 virtio_gpu_object_free_sg_table(bo);
262 static struct ttm_bo_driver virtio_gpu_bo_driver = {
263 .ttm_tt_create = &virtio_gpu_ttm_tt_create,
264 .invalidate_caches = &virtio_gpu_invalidate_caches,
265 .init_mem_type = &virtio_gpu_init_mem_type,
266 .eviction_valuable = ttm_bo_eviction_valuable,
267 .evict_flags = &virtio_gpu_evict_flags,
268 .verify_access = &virtio_gpu_verify_access,
269 .io_mem_reserve = &virtio_gpu_ttm_io_mem_reserve,
270 .io_mem_free = &virtio_gpu_ttm_io_mem_free,
271 .swap_notify = &virtio_gpu_bo_swap_notify,
274 int virtio_gpu_ttm_init(struct virtio_gpu_device *vgdev)
278 /* No others user of address space so set it to 0 */
279 r = ttm_bo_device_init(&vgdev->mman.bdev,
280 &virtio_gpu_bo_driver,
281 vgdev->ddev->anon_inode->i_mapping,
284 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
288 r = ttm_bo_init_mm(&vgdev->mman.bdev, TTM_PL_TT, 0);
290 DRM_ERROR("Failed initializing GTT heap.\n");
296 ttm_bo_device_release(&vgdev->mman.bdev);
301 void virtio_gpu_ttm_fini(struct virtio_gpu_device *vgdev)
303 ttm_bo_device_release(&vgdev->mman.bdev);
304 DRM_INFO("virtio_gpu: ttm finalized\n");