Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / gpu / drm / virtio / virtgpu_object.c
blob0b90cdb3d9fef2d2dc4717ca450c59acd685c477
1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
3 * All Rights Reserved.
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:
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.
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.
26 #include "virtgpu_drv.h"
28 static void virtio_gpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
30 struct virtio_gpu_object *bo;
31 struct virtio_gpu_device *vgdev;
33 bo = container_of(tbo, struct virtio_gpu_object, tbo);
34 vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
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);
44 static void virtio_gpu_init_ttm_placement(struct virtio_gpu_object *vgbo,
45 bool pinned)
47 u32 c = 1;
48 u32 pflag = pinned ? TTM_PL_FLAG_NO_EVICT : 0;
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;
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)
65 struct virtio_gpu_object *bo;
66 enum ttm_bo_type type;
67 size_t acc_size;
68 int ret;
70 if (kernel)
71 type = ttm_bo_type_kernel;
72 else
73 type = ttm_bo_type_device;
74 *bo_ptr = NULL;
76 acc_size = ttm_bo_dma_acc_size(&vgdev->mman.bdev, size,
77 sizeof(struct virtio_gpu_object));
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 kfree(bo);
86 return ret;
88 bo->dumb = false;
89 virtio_gpu_init_ttm_placement(bo, pinned);
91 ret = ttm_bo_init(&vgdev->mman.bdev, &bo->tbo, size, type,
92 &bo->placement, 0, !kernel, NULL, acc_size,
93 NULL, NULL, &virtio_gpu_ttm_bo_destroy);
94 /* ttm_bo_init failure will call the destroy */
95 if (ret != 0)
96 return ret;
98 *bo_ptr = bo;
99 return 0;
102 int virtio_gpu_object_kmap(struct virtio_gpu_object *bo, void **ptr)
104 bool is_iomem;
105 int r;
107 if (bo->vmap) {
108 if (ptr)
109 *ptr = bo->vmap;
110 return 0;
112 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
113 if (r)
114 return r;
115 bo->vmap = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
116 if (ptr)
117 *ptr = bo->vmap;
118 return 0;
121 int virtio_gpu_object_get_sg_table(struct virtio_gpu_device *qdev,
122 struct virtio_gpu_object *bo)
124 int ret;
125 struct page **pages = bo->tbo.ttm->pages;
126 int nr_pages = bo->tbo.num_pages;
127 struct ttm_operation_ctx ctx = {
128 .interruptible = false,
129 .no_wait_gpu = false
132 /* wtf swapping */
133 if (bo->pages)
134 return 0;
136 if (bo->tbo.ttm->state == tt_unpopulated)
137 bo->tbo.ttm->bdev->driver->ttm_tt_populate(bo->tbo.ttm, &ctx);
138 bo->pages = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
139 if (!bo->pages)
140 goto out;
142 ret = sg_alloc_table_from_pages(bo->pages, pages, nr_pages, 0,
143 nr_pages << PAGE_SHIFT, GFP_KERNEL);
144 if (ret)
145 goto out;
146 return 0;
147 out:
148 kfree(bo->pages);
149 bo->pages = NULL;
150 return -ENOMEM;
153 void virtio_gpu_object_free_sg_table(struct virtio_gpu_object *bo)
155 sg_free_table(bo->pages);
156 kfree(bo->pages);
157 bo->pages = NULL;
160 int virtio_gpu_object_wait(struct virtio_gpu_object *bo, bool no_wait)
162 int r;
164 r = ttm_bo_reserve(&bo->tbo, true, no_wait, NULL);
165 if (unlikely(r != 0))
166 return r;
167 r = ttm_bo_wait(&bo->tbo, true, no_wait);
168 ttm_bo_unreserve(&bo->tbo);
169 return r;