drm/virtio: move virtio_gpu_object_{attach, detach} calls.
[sfrench/cifs-2.6.git] / drivers / gpu / drm / virtio / virtgpu_ttm.c
1 /*
2  * Copyright (C) 2015 Red Hat, Inc.
3  * All Rights Reserved.
4  *
5  * Authors:
6  *    Dave Airlie
7  *    Alon Levy
8  *
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:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
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.
26  */
27
28 #include <drm/ttm/ttm_bo_api.h>
29 #include <drm/ttm/ttm_bo_driver.h>
30 #include <drm/ttm/ttm_placement.h>
31 #include <drm/ttm/ttm_page_alloc.h>
32 #include <drm/ttm/ttm_module.h>
33 #include <drm/drmP.h>
34 #include <drm/drm.h>
35 #include <drm/virtgpu_drm.h>
36 #include "virtgpu_drv.h"
37
38 #include <linux/delay.h>
39
40 #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
41
42 static struct
43 virtio_gpu_device *virtio_gpu_get_vgdev(struct ttm_bo_device *bdev)
44 {
45         struct virtio_gpu_mman *mman;
46         struct virtio_gpu_device *vgdev;
47
48         mman = container_of(bdev, struct virtio_gpu_mman, bdev);
49         vgdev = container_of(mman, struct virtio_gpu_device, mman);
50         return vgdev;
51 }
52
53 int virtio_gpu_mmap(struct file *filp, struct vm_area_struct *vma)
54 {
55         struct drm_file *file_priv;
56         struct virtio_gpu_device *vgdev;
57         int r;
58
59         file_priv = filp->private_data;
60         vgdev = file_priv->minor->dev->dev_private;
61         if (vgdev == NULL) {
62                 DRM_ERROR(
63                  "filp->private_data->minor->dev->dev_private == NULL\n");
64                 return -EINVAL;
65         }
66         r = ttm_bo_mmap(filp, vma, &vgdev->mman.bdev);
67
68         return r;
69 }
70
71 static int virtio_gpu_invalidate_caches(struct ttm_bo_device *bdev,
72                                         uint32_t flags)
73 {
74         return 0;
75 }
76
77 static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
78                                struct ttm_buffer_object *bo,
79                                const struct ttm_place *place,
80                                struct ttm_mem_reg *mem)
81 {
82         mem->mm_node = (void *)1;
83         return 0;
84 }
85
86 static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
87                                 struct ttm_mem_reg *mem)
88 {
89         mem->mm_node = (void *)NULL;
90 }
91
92 static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
93                            unsigned long p_size)
94 {
95         return 0;
96 }
97
98 static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
99 {
100         return 0;
101 }
102
103 static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
104                              struct drm_printer *printer)
105 {
106 }
107
108 static const struct ttm_mem_type_manager_func virtio_gpu_bo_manager_func = {
109         .init = ttm_bo_man_init,
110         .takedown = ttm_bo_man_takedown,
111         .get_node = ttm_bo_man_get_node,
112         .put_node = ttm_bo_man_put_node,
113         .debug = ttm_bo_man_debug
114 };
115
116 static int virtio_gpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
117                                     struct ttm_mem_type_manager *man)
118 {
119         switch (type) {
120         case TTM_PL_SYSTEM:
121                 /* System memory */
122                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
123                 man->available_caching = TTM_PL_MASK_CACHING;
124                 man->default_caching = TTM_PL_FLAG_CACHED;
125                 break;
126         case TTM_PL_TT:
127                 man->func = &virtio_gpu_bo_manager_func;
128                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
129                 man->available_caching = TTM_PL_MASK_CACHING;
130                 man->default_caching = TTM_PL_FLAG_CACHED;
131                 break;
132         default:
133                 DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
134                 return -EINVAL;
135         }
136         return 0;
137 }
138
139 static void virtio_gpu_evict_flags(struct ttm_buffer_object *bo,
140                                 struct ttm_placement *placement)
141 {
142         static const struct ttm_place placements = {
143                 .fpfn  = 0,
144                 .lpfn  = 0,
145                 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM,
146         };
147
148         placement->placement = &placements;
149         placement->busy_placement = &placements;
150         placement->num_placement = 1;
151         placement->num_busy_placement = 1;
152 }
153
154 static int virtio_gpu_verify_access(struct ttm_buffer_object *bo,
155                                     struct file *filp)
156 {
157         return 0;
158 }
159
160 static int virtio_gpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
161                                          struct ttm_mem_reg *mem)
162 {
163         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
164
165         mem->bus.addr = NULL;
166         mem->bus.offset = 0;
167         mem->bus.size = mem->num_pages << PAGE_SHIFT;
168         mem->bus.base = 0;
169         mem->bus.is_iomem = false;
170         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
171                 return -EINVAL;
172         switch (mem->mem_type) {
173         case TTM_PL_SYSTEM:
174         case TTM_PL_TT:
175                 /* system memory */
176                 return 0;
177         default:
178                 return -EINVAL;
179         }
180         return 0;
181 }
182
183 static void virtio_gpu_ttm_io_mem_free(struct ttm_bo_device *bdev,
184                                        struct ttm_mem_reg *mem)
185 {
186 }
187
188 /*
189  * TTM backend functions.
190  */
191 struct virtio_gpu_ttm_tt {
192         struct ttm_dma_tt               ttm;
193         struct virtio_gpu_object        *obj;
194 };
195
196 static int virtio_gpu_ttm_tt_bind(struct ttm_tt *ttm,
197                                   struct ttm_mem_reg *bo_mem)
198 {
199         struct virtio_gpu_ttm_tt *gtt =
200                 container_of(ttm, struct virtio_gpu_ttm_tt, ttm.ttm);
201         struct virtio_gpu_device *vgdev =
202                 virtio_gpu_get_vgdev(gtt->obj->tbo.bdev);
203
204         virtio_gpu_object_attach(vgdev, gtt->obj, NULL);
205         return 0;
206 }
207
208 static int virtio_gpu_ttm_tt_unbind(struct ttm_tt *ttm)
209 {
210         struct virtio_gpu_ttm_tt *gtt =
211                 container_of(ttm, struct virtio_gpu_ttm_tt, ttm.ttm);
212         struct virtio_gpu_device *vgdev =
213                 virtio_gpu_get_vgdev(gtt->obj->tbo.bdev);
214
215         virtio_gpu_object_detach(vgdev, gtt->obj);
216         return 0;
217 }
218
219 static void virtio_gpu_ttm_tt_destroy(struct ttm_tt *ttm)
220 {
221         struct virtio_gpu_ttm_tt *gtt =
222                 container_of(ttm, struct virtio_gpu_ttm_tt, ttm.ttm);
223
224         ttm_dma_tt_fini(&gtt->ttm);
225         kfree(gtt);
226 }
227
228 static struct ttm_backend_func virtio_gpu_tt_func = {
229         .bind = &virtio_gpu_ttm_tt_bind,
230         .unbind = &virtio_gpu_ttm_tt_unbind,
231         .destroy = &virtio_gpu_ttm_tt_destroy,
232 };
233
234 static struct ttm_tt *virtio_gpu_ttm_tt_create(struct ttm_buffer_object *bo,
235                                                uint32_t page_flags)
236 {
237         struct virtio_gpu_device *vgdev;
238         struct virtio_gpu_ttm_tt *gtt;
239
240         vgdev = virtio_gpu_get_vgdev(bo->bdev);
241         gtt = kzalloc(sizeof(struct virtio_gpu_ttm_tt), GFP_KERNEL);
242         if (gtt == NULL)
243                 return NULL;
244         gtt->ttm.ttm.func = &virtio_gpu_tt_func;
245         gtt->obj = container_of(bo, struct virtio_gpu_object, tbo);
246         if (ttm_dma_tt_init(&gtt->ttm, bo, page_flags)) {
247                 kfree(gtt);
248                 return NULL;
249         }
250         return &gtt->ttm.ttm;
251 }
252
253 static void virtio_gpu_bo_swap_notify(struct ttm_buffer_object *tbo)
254 {
255         struct virtio_gpu_object *bo;
256
257         bo = container_of(tbo, struct virtio_gpu_object, tbo);
258
259         if (bo->pages)
260                 virtio_gpu_object_free_sg_table(bo);
261 }
262
263 static struct ttm_bo_driver virtio_gpu_bo_driver = {
264         .ttm_tt_create = &virtio_gpu_ttm_tt_create,
265         .invalidate_caches = &virtio_gpu_invalidate_caches,
266         .init_mem_type = &virtio_gpu_init_mem_type,
267         .eviction_valuable = ttm_bo_eviction_valuable,
268         .evict_flags = &virtio_gpu_evict_flags,
269         .verify_access = &virtio_gpu_verify_access,
270         .io_mem_reserve = &virtio_gpu_ttm_io_mem_reserve,
271         .io_mem_free = &virtio_gpu_ttm_io_mem_free,
272         .swap_notify = &virtio_gpu_bo_swap_notify,
273 };
274
275 int virtio_gpu_ttm_init(struct virtio_gpu_device *vgdev)
276 {
277         int r;
278
279         /* No others user of address space so set it to 0 */
280         r = ttm_bo_device_init(&vgdev->mman.bdev,
281                                &virtio_gpu_bo_driver,
282                                vgdev->ddev->anon_inode->i_mapping,
283                                DRM_FILE_PAGE_OFFSET, 0);
284         if (r) {
285                 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
286                 goto err_dev_init;
287         }
288
289         r = ttm_bo_init_mm(&vgdev->mman.bdev, TTM_PL_TT, 0);
290         if (r) {
291                 DRM_ERROR("Failed initializing GTT heap.\n");
292                 goto err_mm_init;
293         }
294         return 0;
295
296 err_mm_init:
297         ttm_bo_device_release(&vgdev->mman.bdev);
298 err_dev_init:
299         return r;
300 }
301
302 void virtio_gpu_ttm_fini(struct virtio_gpu_device *vgdev)
303 {
304         ttm_bo_device_release(&vgdev->mman.bdev);
305         DRM_INFO("virtio_gpu: ttm finalized\n");
306 }