]> Git Repo - linux.git/blob - drivers/gpu/drm/virtio/virtgpu_ttm.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.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 static int virtio_gpu_ttm_mem_global_init(struct drm_global_reference *ref)
54 {
55         return ttm_mem_global_init(ref->object);
56 }
57
58 static void virtio_gpu_ttm_mem_global_release(struct drm_global_reference *ref)
59 {
60         ttm_mem_global_release(ref->object);
61 }
62
63 static int virtio_gpu_ttm_global_init(struct virtio_gpu_device *vgdev)
64 {
65         struct drm_global_reference *global_ref;
66         int r;
67
68         vgdev->mman.mem_global_referenced = false;
69         global_ref = &vgdev->mman.mem_global_ref;
70         global_ref->global_type = DRM_GLOBAL_TTM_MEM;
71         global_ref->size = sizeof(struct ttm_mem_global);
72         global_ref->init = &virtio_gpu_ttm_mem_global_init;
73         global_ref->release = &virtio_gpu_ttm_mem_global_release;
74
75         r = drm_global_item_ref(global_ref);
76         if (r != 0) {
77                 DRM_ERROR("Failed setting up TTM memory accounting "
78                           "subsystem.\n");
79                 return r;
80         }
81
82         vgdev->mman.bo_global_ref.mem_glob =
83                 vgdev->mman.mem_global_ref.object;
84         global_ref = &vgdev->mman.bo_global_ref.ref;
85         global_ref->global_type = DRM_GLOBAL_TTM_BO;
86         global_ref->size = sizeof(struct ttm_bo_global);
87         global_ref->init = &ttm_bo_global_init;
88         global_ref->release = &ttm_bo_global_release;
89         r = drm_global_item_ref(global_ref);
90         if (r != 0) {
91                 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
92                 drm_global_item_unref(&vgdev->mman.mem_global_ref);
93                 return r;
94         }
95
96         vgdev->mman.mem_global_referenced = true;
97         return 0;
98 }
99
100 static void virtio_gpu_ttm_global_fini(struct virtio_gpu_device *vgdev)
101 {
102         if (vgdev->mman.mem_global_referenced) {
103                 drm_global_item_unref(&vgdev->mman.bo_global_ref.ref);
104                 drm_global_item_unref(&vgdev->mman.mem_global_ref);
105                 vgdev->mman.mem_global_referenced = false;
106         }
107 }
108
109 int virtio_gpu_mmap(struct file *filp, struct vm_area_struct *vma)
110 {
111         struct drm_file *file_priv;
112         struct virtio_gpu_device *vgdev;
113         int r;
114
115         file_priv = filp->private_data;
116         vgdev = file_priv->minor->dev->dev_private;
117         if (vgdev == NULL) {
118                 DRM_ERROR(
119                  "filp->private_data->minor->dev->dev_private == NULL\n");
120                 return -EINVAL;
121         }
122         r = ttm_bo_mmap(filp, vma, &vgdev->mman.bdev);
123
124         return r;
125 }
126
127 static int virtio_gpu_invalidate_caches(struct ttm_bo_device *bdev,
128                                         uint32_t flags)
129 {
130         return 0;
131 }
132
133 static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
134                                struct ttm_buffer_object *bo,
135                                const struct ttm_place *place,
136                                struct ttm_mem_reg *mem)
137 {
138         mem->mm_node = (void *)1;
139         return 0;
140 }
141
142 static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
143                                 struct ttm_mem_reg *mem)
144 {
145         mem->mm_node = (void *)NULL;
146 }
147
148 static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
149                            unsigned long p_size)
150 {
151         return 0;
152 }
153
154 static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
155 {
156         return 0;
157 }
158
159 static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
160                              struct drm_printer *printer)
161 {
162 }
163
164 static const struct ttm_mem_type_manager_func virtio_gpu_bo_manager_func = {
165         .init = ttm_bo_man_init,
166         .takedown = ttm_bo_man_takedown,
167         .get_node = ttm_bo_man_get_node,
168         .put_node = ttm_bo_man_put_node,
169         .debug = ttm_bo_man_debug
170 };
171
172 static int virtio_gpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
173                                     struct ttm_mem_type_manager *man)
174 {
175         struct virtio_gpu_device *vgdev;
176
177         vgdev = virtio_gpu_get_vgdev(bdev);
178
179         switch (type) {
180         case TTM_PL_SYSTEM:
181                 /* System memory */
182                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
183                 man->available_caching = TTM_PL_MASK_CACHING;
184                 man->default_caching = TTM_PL_FLAG_CACHED;
185                 break;
186         case TTM_PL_TT:
187                 man->func = &virtio_gpu_bo_manager_func;
188                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
189                 man->available_caching = TTM_PL_MASK_CACHING;
190                 man->default_caching = TTM_PL_FLAG_CACHED;
191                 break;
192         default:
193                 DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
194                 return -EINVAL;
195         }
196         return 0;
197 }
198
199 static void virtio_gpu_evict_flags(struct ttm_buffer_object *bo,
200                                 struct ttm_placement *placement)
201 {
202         static const struct ttm_place placements = {
203                 .fpfn  = 0,
204                 .lpfn  = 0,
205                 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM,
206         };
207
208         placement->placement = &placements;
209         placement->busy_placement = &placements;
210         placement->num_placement = 1;
211         placement->num_busy_placement = 1;
212 }
213
214 static int virtio_gpu_verify_access(struct ttm_buffer_object *bo,
215                                     struct file *filp)
216 {
217         return 0;
218 }
219
220 static int virtio_gpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
221                                          struct ttm_mem_reg *mem)
222 {
223         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
224
225         mem->bus.addr = NULL;
226         mem->bus.offset = 0;
227         mem->bus.size = mem->num_pages << PAGE_SHIFT;
228         mem->bus.base = 0;
229         mem->bus.is_iomem = false;
230         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
231                 return -EINVAL;
232         switch (mem->mem_type) {
233         case TTM_PL_SYSTEM:
234         case TTM_PL_TT:
235                 /* system memory */
236                 return 0;
237         default:
238                 return -EINVAL;
239         }
240         return 0;
241 }
242
243 static void virtio_gpu_ttm_io_mem_free(struct ttm_bo_device *bdev,
244                                        struct ttm_mem_reg *mem)
245 {
246 }
247
248 /*
249  * TTM backend functions.
250  */
251 struct virtio_gpu_ttm_tt {
252         struct ttm_dma_tt               ttm;
253         struct virtio_gpu_device        *vgdev;
254         u64                             offset;
255 };
256
257 static int virtio_gpu_ttm_backend_bind(struct ttm_tt *ttm,
258                                        struct ttm_mem_reg *bo_mem)
259 {
260         struct virtio_gpu_ttm_tt *gtt = (void *)ttm;
261
262         gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
263         if (!ttm->num_pages)
264                 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
265                      ttm->num_pages, bo_mem, ttm);
266
267         /* Not implemented */
268         return 0;
269 }
270
271 static int virtio_gpu_ttm_backend_unbind(struct ttm_tt *ttm)
272 {
273         /* Not implemented */
274         return 0;
275 }
276
277 static void virtio_gpu_ttm_backend_destroy(struct ttm_tt *ttm)
278 {
279         struct virtio_gpu_ttm_tt *gtt = (void *)ttm;
280
281         ttm_dma_tt_fini(&gtt->ttm);
282         kfree(gtt);
283 }
284
285 static struct ttm_backend_func virtio_gpu_backend_func = {
286         .bind = &virtio_gpu_ttm_backend_bind,
287         .unbind = &virtio_gpu_ttm_backend_unbind,
288         .destroy = &virtio_gpu_ttm_backend_destroy,
289 };
290
291 static struct ttm_tt *virtio_gpu_ttm_tt_create(struct ttm_buffer_object *bo,
292                                                uint32_t page_flags)
293 {
294         struct virtio_gpu_device *vgdev;
295         struct virtio_gpu_ttm_tt *gtt;
296
297         vgdev = virtio_gpu_get_vgdev(bo->bdev);
298         gtt = kzalloc(sizeof(struct virtio_gpu_ttm_tt), GFP_KERNEL);
299         if (gtt == NULL)
300                 return NULL;
301         gtt->ttm.ttm.func = &virtio_gpu_backend_func;
302         gtt->vgdev = vgdev;
303         if (ttm_dma_tt_init(&gtt->ttm, bo, page_flags)) {
304                 kfree(gtt);
305                 return NULL;
306         }
307         return &gtt->ttm.ttm;
308 }
309
310 static void virtio_gpu_move_null(struct ttm_buffer_object *bo,
311                                  struct ttm_mem_reg *new_mem)
312 {
313         struct ttm_mem_reg *old_mem = &bo->mem;
314
315         BUG_ON(old_mem->mm_node != NULL);
316         *old_mem = *new_mem;
317         new_mem->mm_node = NULL;
318 }
319
320 static int virtio_gpu_bo_move(struct ttm_buffer_object *bo, bool evict,
321                               struct ttm_operation_ctx *ctx,
322                               struct ttm_mem_reg *new_mem)
323 {
324         int ret;
325
326         ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
327         if (ret)
328                 return ret;
329
330         virtio_gpu_move_null(bo, new_mem);
331         return 0;
332 }
333
334 static void virtio_gpu_bo_move_notify(struct ttm_buffer_object *tbo,
335                                       bool evict,
336                                       struct ttm_mem_reg *new_mem)
337 {
338         struct virtio_gpu_object *bo;
339         struct virtio_gpu_device *vgdev;
340
341         bo = container_of(tbo, struct virtio_gpu_object, tbo);
342         vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
343
344         if (!new_mem || (new_mem->placement & TTM_PL_FLAG_SYSTEM)) {
345                 if (bo->hw_res_handle)
346                         virtio_gpu_object_detach(vgdev, bo);
347
348         } else if (new_mem->placement & TTM_PL_FLAG_TT) {
349                 if (bo->hw_res_handle) {
350                         virtio_gpu_object_attach(vgdev, bo, bo->hw_res_handle,
351                                                  NULL);
352                 }
353         }
354 }
355
356 static void virtio_gpu_bo_swap_notify(struct ttm_buffer_object *tbo)
357 {
358         struct virtio_gpu_object *bo;
359         struct virtio_gpu_device *vgdev;
360
361         bo = container_of(tbo, struct virtio_gpu_object, tbo);
362         vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
363
364         if (bo->pages)
365                 virtio_gpu_object_free_sg_table(bo);
366 }
367
368 static struct ttm_bo_driver virtio_gpu_bo_driver = {
369         .ttm_tt_create = &virtio_gpu_ttm_tt_create,
370         .invalidate_caches = &virtio_gpu_invalidate_caches,
371         .init_mem_type = &virtio_gpu_init_mem_type,
372         .eviction_valuable = ttm_bo_eviction_valuable,
373         .evict_flags = &virtio_gpu_evict_flags,
374         .move = &virtio_gpu_bo_move,
375         .verify_access = &virtio_gpu_verify_access,
376         .io_mem_reserve = &virtio_gpu_ttm_io_mem_reserve,
377         .io_mem_free = &virtio_gpu_ttm_io_mem_free,
378         .move_notify = &virtio_gpu_bo_move_notify,
379         .swap_notify = &virtio_gpu_bo_swap_notify,
380 };
381
382 int virtio_gpu_ttm_init(struct virtio_gpu_device *vgdev)
383 {
384         int r;
385
386         r = virtio_gpu_ttm_global_init(vgdev);
387         if (r)
388                 return r;
389         /* No others user of address space so set it to 0 */
390         r = ttm_bo_device_init(&vgdev->mman.bdev,
391                                vgdev->mman.bo_global_ref.ref.object,
392                                &virtio_gpu_bo_driver,
393                                vgdev->ddev->anon_inode->i_mapping,
394                                DRM_FILE_PAGE_OFFSET, 0);
395         if (r) {
396                 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
397                 goto err_dev_init;
398         }
399
400         r = ttm_bo_init_mm(&vgdev->mman.bdev, TTM_PL_TT, 0);
401         if (r) {
402                 DRM_ERROR("Failed initializing GTT heap.\n");
403                 goto err_mm_init;
404         }
405         return 0;
406
407 err_mm_init:
408         ttm_bo_device_release(&vgdev->mman.bdev);
409 err_dev_init:
410         virtio_gpu_ttm_global_fini(vgdev);
411         return r;
412 }
413
414 void virtio_gpu_ttm_fini(struct virtio_gpu_device *vgdev)
415 {
416         ttm_bo_device_release(&vgdev->mman.bdev);
417         virtio_gpu_ttm_global_fini(vgdev);
418         DRM_INFO("virtio_gpu: ttm finalized\n");
419 }
This page took 0.059752 seconds and 4 git commands to generate.