VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/drm/vbox_ttm.c@ 74642

Last change on this file since 74642 was 74615, checked in by vboxsync, 6 years ago

Additions/linux/vboxvideo: fix building on various EL 7 minor releases.
bugref:4567: Linux kernel driver maintenance

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 KB
Line 
1/* $Id: vbox_ttm.c 74615 2018-10-04 15:44:01Z vboxsync $ */
2/** @file
3 * VirtualBox Additions Linux kernel video driver
4 */
5
6/*
7 * Copyright (C) 2013-2017 Oracle Corporation
8 * This file is based on ast_ttm.c
9 * Copyright 2012 Red Hat Inc.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the
13 * "Software"), to deal in the Software without restriction, including
14 * without limitation the rights to use, copy, modify, merge, publish,
15 * distribute, sub license, and/or sell copies of the Software, and to
16 * permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
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 NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * The above copyright notice and this permission notice (including the
28 * next paragraph) shall be included in all copies or substantial portions
29 * of the Software.
30 *
31 *
32 * Authors: Dave Airlie <[email protected]>
33 * Michael Thayer <[email protected]>
34 */
35#include "vbox_drv.h"
36#include <ttm/ttm_page_alloc.h>
37
38#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0) && !defined(RHEL_72)
39#define PLACEMENT_FLAGS(placement) (placement)
40#else
41#define PLACEMENT_FLAGS(placement) ((placement).flags)
42#endif
43
44static inline struct vbox_private *vbox_bdev(struct ttm_bo_device *bd)
45{
46 return container_of(bd, struct vbox_private, ttm.bdev);
47}
48
49static int vbox_ttm_mem_global_init(struct drm_global_reference *ref)
50{
51 return ttm_mem_global_init(ref->object);
52}
53
54static void vbox_ttm_mem_global_release(struct drm_global_reference *ref)
55{
56 ttm_mem_global_release(ref->object);
57}
58
59/**
60 * Adds the vbox memory manager object/structures to the global memory manager.
61 */
62static int vbox_ttm_global_init(struct vbox_private *vbox)
63{
64 struct drm_global_reference *global_ref;
65 int r;
66
67 global_ref = &vbox->ttm.mem_global_ref;
68 global_ref->global_type = DRM_GLOBAL_TTM_MEM;
69 global_ref->size = sizeof(struct ttm_mem_global);
70 global_ref->init = &vbox_ttm_mem_global_init;
71 global_ref->release = &vbox_ttm_mem_global_release;
72 r = drm_global_item_ref(global_ref);
73 if (r != 0) {
74 DRM_ERROR("Failed setting up TTM memory accounting subsystem.\n");
75 return r;
76 }
77
78 vbox->ttm.bo_global_ref.mem_glob = vbox->ttm.mem_global_ref.object;
79 global_ref = &vbox->ttm.bo_global_ref.ref;
80 global_ref->global_type = DRM_GLOBAL_TTM_BO;
81 global_ref->size = sizeof(struct ttm_bo_global);
82 global_ref->init = &ttm_bo_global_init;
83 global_ref->release = &ttm_bo_global_release;
84
85 r = drm_global_item_ref(global_ref);
86 if (r != 0) {
87 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
88 drm_global_item_unref(&vbox->ttm.mem_global_ref);
89 return r;
90 }
91
92 return 0;
93}
94
95/**
96 * Removes the vbox memory manager object from the global memory manager.
97 */
98static void vbox_ttm_global_release(struct vbox_private *vbox)
99{
100 if (!vbox->ttm.mem_global_ref.release)
101 return;
102
103 drm_global_item_unref(&vbox->ttm.bo_global_ref.ref);
104 drm_global_item_unref(&vbox->ttm.mem_global_ref);
105 vbox->ttm.mem_global_ref.release = NULL;
106}
107
108static void vbox_bo_ttm_destroy(struct ttm_buffer_object *tbo)
109{
110 struct vbox_bo *bo;
111
112 bo = container_of(tbo, struct vbox_bo, bo);
113
114 drm_gem_object_release(&bo->gem);
115 kfree(bo);
116}
117
118static bool vbox_ttm_bo_is_vbox_bo(struct ttm_buffer_object *bo)
119{
120 if (bo->destroy == &vbox_bo_ttm_destroy)
121 return true;
122
123 return false;
124}
125
126static int
127vbox_bo_init_mem_type(struct ttm_bo_device *bdev, u32 type,
128 struct ttm_mem_type_manager *man)
129{
130 switch (type) {
131 case TTM_PL_SYSTEM:
132 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
133 man->available_caching = TTM_PL_MASK_CACHING;
134 man->default_caching = TTM_PL_FLAG_CACHED;
135 break;
136 case TTM_PL_VRAM:
137 man->func = &ttm_bo_manager_func;
138 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
139 man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
140 man->default_caching = TTM_PL_FLAG_WC;
141 break;
142 default:
143 DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
144 return -EINVAL;
145 }
146
147 return 0;
148}
149
150static void
151vbox_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
152{
153 struct vbox_bo *vboxbo = vbox_bo(bo);
154
155 if (!vbox_ttm_bo_is_vbox_bo(bo))
156 return;
157
158 vbox_ttm_placement(vboxbo, TTM_PL_FLAG_SYSTEM);
159 *pl = vboxbo->placement;
160}
161
162static int vbox_bo_verify_access(struct ttm_buffer_object *bo,
163 struct file *filp)
164{
165 return 0;
166}
167
168static int vbox_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
169 struct ttm_mem_reg *mem)
170{
171 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
172 struct vbox_private *vbox = vbox_bdev(bdev);
173
174 mem->bus.addr = NULL;
175 mem->bus.offset = 0;
176 mem->bus.size = mem->num_pages << PAGE_SHIFT;
177 mem->bus.base = 0;
178 mem->bus.is_iomem = false;
179 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
180 return -EINVAL;
181 switch (mem->mem_type) {
182 case TTM_PL_SYSTEM:
183 /* system memory */
184 return 0;
185 case TTM_PL_VRAM:
186 mem->bus.offset = mem->start << PAGE_SHIFT;
187 mem->bus.base = pci_resource_start(vbox->dev->pdev, 0);
188 mem->bus.is_iomem = true;
189 break;
190 default:
191 return -EINVAL;
192 }
193 return 0;
194}
195
196static void vbox_ttm_io_mem_free(struct ttm_bo_device *bdev,
197 struct ttm_mem_reg *mem)
198{
199}
200
201static void vbox_ttm_backend_destroy(struct ttm_tt *tt)
202{
203 ttm_tt_fini(tt);
204 kfree(tt);
205}
206
207static struct ttm_backend_func vbox_tt_backend_func = {
208 .destroy = &vbox_ttm_backend_destroy,
209};
210
211#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
212static struct ttm_tt *vbox_ttm_tt_create(struct ttm_bo_device *bdev,
213 unsigned long size,
214 u32 page_flags,
215 struct page *dummy_read_page)
216#else
217static struct ttm_tt *vbox_ttm_tt_create(struct ttm_buffer_object *bo,
218 u32 page_flags)
219#endif
220{
221 struct ttm_tt *tt;
222
223 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
224 if (!tt)
225 return NULL;
226
227 tt->func = &vbox_tt_backend_func;
228#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
229 if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) {
230#else
231 if (ttm_tt_init(tt, bo, page_flags)) {
232#endif
233 kfree(tt);
234 return NULL;
235 }
236
237 return tt;
238}
239
240#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
241# if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
242static int vbox_ttm_tt_populate(struct ttm_tt *ttm)
243{
244 return ttm_pool_populate(ttm);
245}
246# else
247static int vbox_ttm_tt_populate(struct ttm_tt *ttm,
248 struct ttm_operation_ctx *ctx)
249{
250 return ttm_pool_populate(ttm, ctx);
251}
252# endif
253
254static void vbox_ttm_tt_unpopulate(struct ttm_tt *ttm)
255{
256 ttm_pool_unpopulate(ttm);
257}
258#endif
259
260struct ttm_bo_driver vbox_bo_driver = {
261 .ttm_tt_create = vbox_ttm_tt_create,
262#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
263 .ttm_tt_populate = vbox_ttm_tt_populate,
264 .ttm_tt_unpopulate = vbox_ttm_tt_unpopulate,
265#endif
266 .init_mem_type = vbox_bo_init_mem_type,
267#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) || defined(RHEL_74)
268 .eviction_valuable = ttm_bo_eviction_valuable,
269#endif
270 .evict_flags = vbox_bo_evict_flags,
271 .verify_access = vbox_bo_verify_access,
272 .io_mem_reserve = &vbox_ttm_io_mem_reserve,
273 .io_mem_free = &vbox_ttm_io_mem_free,
274#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) || defined(RHEL_75)
275# if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
276 .io_mem_pfn = ttm_bo_default_io_mem_pfn,
277# endif
278#endif
279#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)) \
280 || defined(RHEL_74)
281# ifndef RHEL_75
282 .lru_tail = &ttm_bo_default_lru_tail,
283 .swap_lru_tail = &ttm_bo_default_swap_lru_tail,
284# endif
285#endif
286};
287
288int vbox_mm_init(struct vbox_private *vbox)
289{
290 int ret;
291 struct drm_device *dev = vbox->dev;
292 struct ttm_bo_device *bdev = &vbox->ttm.bdev;
293
294 ret = vbox_ttm_global_init(vbox);
295 if (ret)
296 return ret;
297
298 ret = ttm_bo_device_init(&vbox->ttm.bdev,
299 vbox->ttm.bo_global_ref.ref.object,
300 &vbox_bo_driver,
301#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0) || defined(RHEL_71)
302 dev->anon_inode->i_mapping,
303#endif
304 DRM_FILE_PAGE_OFFSET, true);
305 if (ret) {
306 DRM_ERROR("Error initialising bo driver; %d\n", ret);
307 return ret;
308 }
309
310 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
311 vbox->available_vram_size >> PAGE_SHIFT);
312 if (ret) {
313 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
314 return ret;
315 }
316#ifdef DRM_MTRR_WC
317 vbox->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
318 pci_resource_len(dev->pdev, 0),
319 DRM_MTRR_WC);
320#else
321 vbox->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
322 pci_resource_len(dev->pdev, 0));
323#endif
324
325 vbox->ttm.mm_initialised = true;
326
327 return 0;
328}
329
330void vbox_mm_fini(struct vbox_private *vbox)
331{
332#ifdef DRM_MTRR_WC
333 struct drm_device *dev = vbox->dev;
334#endif
335 if (!vbox->ttm.mm_initialised)
336 return;
337 ttm_bo_device_release(&vbox->ttm.bdev);
338
339 vbox_ttm_global_release(vbox);
340
341#ifdef DRM_MTRR_WC
342 drm_mtrr_del(vbox->fb_mtrr,
343 pci_resource_start(dev->pdev, 0),
344 pci_resource_len(dev->pdev, 0), DRM_MTRR_WC);
345#else
346 arch_phys_wc_del(vbox->fb_mtrr);
347#endif
348}
349
350void vbox_ttm_placement(struct vbox_bo *bo, int domain)
351{
352 u32 c = 0;
353#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0) && !defined(RHEL_72)
354 bo->placement.fpfn = 0;
355 bo->placement.lpfn = 0;
356#else
357 unsigned int i;
358#endif
359
360 bo->placement.placement = bo->placements;
361 bo->placement.busy_placement = bo->placements;
362
363 if (domain & TTM_PL_FLAG_VRAM)
364 PLACEMENT_FLAGS(bo->placements[c++]) =
365 TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
366 if (domain & TTM_PL_FLAG_SYSTEM)
367 PLACEMENT_FLAGS(bo->placements[c++]) =
368 TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
369 if (!c)
370 PLACEMENT_FLAGS(bo->placements[c++]) =
371 TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
372
373 bo->placement.num_placement = c;
374 bo->placement.num_busy_placement = c;
375#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) || defined(RHEL_72)
376 for (i = 0; i < c; ++i) {
377 bo->placements[i].fpfn = 0;
378 bo->placements[i].lpfn = 0;
379 }
380#endif
381}
382
383int vbox_bo_create(struct drm_device *dev, int size, int align,
384 u32 flags, struct vbox_bo **pvboxbo)
385{
386 struct vbox_private *vbox = dev->dev_private;
387 struct vbox_bo *vboxbo;
388 size_t acc_size;
389 int ret;
390
391 vboxbo = kzalloc(sizeof(*vboxbo), GFP_KERNEL);
392 if (!vboxbo)
393 return -ENOMEM;
394
395 ret = drm_gem_object_init(dev, &vboxbo->gem, size);
396 if (ret) {
397 kfree(vboxbo);
398 return ret;
399 }
400
401 vboxbo->bo.bdev = &vbox->ttm.bdev;
402#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0) && !defined(RHEL_71)
403 vboxbo->bo.bdev->dev_mapping = dev->dev_mapping;
404#endif
405
406 vbox_ttm_placement(vboxbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
407
408 acc_size = ttm_bo_dma_acc_size(&vbox->ttm.bdev, size,
409 sizeof(struct vbox_bo));
410
411 ret = ttm_bo_init(&vbox->ttm.bdev, &vboxbo->bo, size,
412 ttm_bo_type_device, &vboxbo->placement,
413#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
414 align >> PAGE_SHIFT, false, NULL, acc_size,
415#else
416 align >> PAGE_SHIFT, false, acc_size,
417#endif
418#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) || defined(RHEL_72)
419 NULL,
420#endif
421 NULL, vbox_bo_ttm_destroy);
422 if (ret)
423 return ret;
424
425 *pvboxbo = vboxbo;
426
427 return 0;
428}
429
430static inline u64 vbox_bo_gpu_offset(struct vbox_bo *bo)
431{
432 return bo->bo.offset;
433}
434
435int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag, u64 *gpu_addr)
436{
437#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
438 struct ttm_operation_ctx ctx = { false, false };
439#endif
440 int i, ret;
441
442 if (bo->pin_count) {
443 bo->pin_count++;
444 if (gpu_addr)
445 *gpu_addr = vbox_bo_gpu_offset(bo);
446
447 return 0;
448 }
449
450 vbox_ttm_placement(bo, pl_flag);
451
452 for (i = 0; i < bo->placement.num_placement; i++)
453 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
454
455#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
456 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
457#else
458 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
459#endif
460 if (ret)
461 return ret;
462
463 bo->pin_count = 1;
464
465 if (gpu_addr)
466 *gpu_addr = vbox_bo_gpu_offset(bo);
467
468 return 0;
469}
470
471int vbox_bo_unpin(struct vbox_bo *bo)
472{
473#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
474 struct ttm_operation_ctx ctx = { false, false };
475#endif
476 int i, ret;
477
478 if (!bo->pin_count) {
479 DRM_ERROR("unpin bad %p\n", bo);
480 return 0;
481 }
482 bo->pin_count--;
483 if (bo->pin_count)
484 return 0;
485
486 for (i = 0; i < bo->placement.num_placement; i++)
487 PLACEMENT_FLAGS(bo->placements[i]) &= ~TTM_PL_FLAG_NO_EVICT;
488
489#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
490 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
491#else
492 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
493#endif
494 if (ret)
495 return ret;
496
497 return 0;
498}
499
500/*
501 * Move a vbox-owned buffer object to system memory if no one else has it
502 * pinned. The caller must have pinned it previously, and this call will
503 * release the caller's pin.
504 */
505int vbox_bo_push_sysram(struct vbox_bo *bo)
506{
507#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
508 struct ttm_operation_ctx ctx = { false, false };
509#endif
510 int i, ret;
511
512 if (!bo->pin_count) {
513 DRM_ERROR("unpin bad %p\n", bo);
514 return 0;
515 }
516 bo->pin_count--;
517 if (bo->pin_count)
518 return 0;
519
520 if (bo->kmap.virtual)
521 ttm_bo_kunmap(&bo->kmap);
522
523 vbox_ttm_placement(bo, TTM_PL_FLAG_SYSTEM);
524
525 for (i = 0; i < bo->placement.num_placement; i++)
526 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
527
528#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
529 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
530#else
531 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
532#endif
533 if (ret) {
534 DRM_ERROR("pushing to VRAM failed\n");
535 return ret;
536 }
537
538 return 0;
539}
540
541int vbox_mmap(struct file *filp, struct vm_area_struct *vma)
542{
543 struct drm_file *file_priv;
544 struct vbox_private *vbox;
545
546 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
547 return -EINVAL;
548
549 file_priv = filp->private_data;
550 vbox = file_priv->minor->dev->dev_private;
551
552 return ttm_bo_mmap(filp, vma, &vbox->ttm.bdev);
553}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette