blob: 1483daebe057d099dcaf05cfe964b241efec12f9 [file] [log] [blame]
Dave Airliedc5698e2013-09-09 10:02:56 +10001/*
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 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
44static 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
61int 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)
Dave Airlie7552ed82015-10-13 16:55:48 +100085 return ret;
Dave Airliedc5698e2013-09-09 10:02:56 +100086 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);
Dave Airlie7552ed82015-10-13 16:55:48 +100092 /* ttm_bo_init failure will call the destroy */
Dave Airliedc5698e2013-09-09 10:02:56 +100093 if (ret != 0)
Dave Airlie7552ed82015-10-13 16:55:48 +100094 return ret;
Dave Airliedc5698e2013-09-09 10:02:56 +100095
96 *bo_ptr = bo;
97 return 0;
Dave Airliedc5698e2013-09-09 10:02:56 +100098}
99
100int virtio_gpu_object_kmap(struct virtio_gpu_object *bo, void **ptr)
101{
102 bool is_iomem;
103 int r;
104
105 if (bo->vmap) {
106 if (ptr)
107 *ptr = bo->vmap;
108 return 0;
109 }
110 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
111 if (r)
112 return r;
113 bo->vmap = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
114 if (ptr)
115 *ptr = bo->vmap;
116 return 0;
117}
118
119int virtio_gpu_object_get_sg_table(struct virtio_gpu_device *qdev,
120 struct virtio_gpu_object *bo)
121{
122 int ret;
123 struct page **pages = bo->tbo.ttm->pages;
124 int nr_pages = bo->tbo.num_pages;
125
126 /* wtf swapping */
127 if (bo->pages)
128 return 0;
129
130 if (bo->tbo.ttm->state == tt_unpopulated)
131 bo->tbo.ttm->bdev->driver->ttm_tt_populate(bo->tbo.ttm);
132 bo->pages = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
133 if (!bo->pages)
134 goto out;
135
136 ret = sg_alloc_table_from_pages(bo->pages, pages, nr_pages, 0,
137 nr_pages << PAGE_SHIFT, GFP_KERNEL);
138 if (ret)
139 goto out;
140 return 0;
141out:
142 kfree(bo->pages);
143 bo->pages = NULL;
144 return -ENOMEM;
145}
146
147void virtio_gpu_object_free_sg_table(struct virtio_gpu_object *bo)
148{
149 sg_free_table(bo->pages);
150 kfree(bo->pages);
151 bo->pages = NULL;
152}
153
154int virtio_gpu_object_wait(struct virtio_gpu_object *bo, bool no_wait)
155{
156 int r;
157
Christian Königdfd5e502016-04-06 11:12:03 +0200158 r = ttm_bo_reserve(&bo->tbo, true, no_wait, NULL);
Dave Airliedc5698e2013-09-09 10:02:56 +1000159 if (unlikely(r != 0))
160 return r;
Christian König8aa6d4f2016-04-06 11:12:04 +0200161 r = ttm_bo_wait(&bo->tbo, true, no_wait);
Dave Airliedc5698e2013-09-09 10:02:56 +1000162 ttm_bo_unreserve(&bo->tbo);
163 return r;
164}
165