VirtualBox

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

最後變更 在這個檔案從80435是 80270,由 vboxsync 提交於 5 年 前

Additions/linux/drm: back out previous change, needs a different solution

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 14.8 KB
 
1/* $Id: vbox_ttm.c 80270 2019-08-14 13:31:41Z vboxsync $ */
2/** @file
3 * VirtualBox Additions Linux kernel video driver
4 */
5
6/*
7 * Copyright (C) 2013-2019 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
49#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
50static int vbox_ttm_mem_global_init(struct drm_global_reference *ref)
51{
52 return ttm_mem_global_init(ref->object);
53}
54
55static void vbox_ttm_mem_global_release(struct drm_global_reference *ref)
56{
57 ttm_mem_global_release(ref->object);
58}
59
60/**
61 * Adds the vbox memory manager object/structures to the global memory manager.
62 */
63static int vbox_ttm_global_init(struct vbox_private *vbox)
64{
65 struct drm_global_reference *global_ref;
66 int ret;
67
68#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
69 global_ref = &vbox->ttm.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 = &vbox_ttm_mem_global_init;
73 global_ref->release = &vbox_ttm_mem_global_release;
74 ret = drm_global_item_ref(global_ref);
75 if (ret) {
76 DRM_ERROR("Failed setting up TTM memory subsystem.\n");
77 return ret;
78 }
79
80 vbox->ttm.bo_global_ref.mem_glob = vbox->ttm.mem_global_ref.object;
81#endif
82 global_ref = &vbox->ttm.bo_global_ref.ref;
83 global_ref->global_type = DRM_GLOBAL_TTM_BO;
84 global_ref->size = sizeof(struct ttm_bo_global);
85 global_ref->init = &ttm_bo_global_init;
86 global_ref->release = &ttm_bo_global_release;
87
88 ret = drm_global_item_ref(global_ref);
89 if (ret) {
90 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
91#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
92 drm_global_item_unref(&vbox->ttm.mem_global_ref);
93#endif
94 return ret;
95 }
96
97 return 0;
98}
99
100/**
101 * Removes the vbox memory manager object from the global memory manager.
102 */
103static void vbox_ttm_global_release(struct vbox_private *vbox)
104{
105 drm_global_item_unref(&vbox->ttm.bo_global_ref.ref);
106 drm_global_item_unref(&vbox->ttm.mem_global_ref);
107}
108#endif
109
110static void vbox_bo_ttm_destroy(struct ttm_buffer_object *tbo)
111{
112 struct vbox_bo *bo;
113
114 bo = container_of(tbo, struct vbox_bo, bo);
115
116 drm_gem_object_release(&bo->gem);
117 kfree(bo);
118}
119
120static bool vbox_ttm_bo_is_vbox_bo(struct ttm_buffer_object *bo)
121{
122 if (bo->destroy == &vbox_bo_ttm_destroy)
123 return true;
124
125 return false;
126}
127
128static int
129vbox_bo_init_mem_type(struct ttm_bo_device *bdev, u32 type,
130 struct ttm_mem_type_manager *man)
131{
132 switch (type) {
133 case TTM_PL_SYSTEM:
134 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
135 man->available_caching = TTM_PL_MASK_CACHING;
136 man->default_caching = TTM_PL_FLAG_CACHED;
137 break;
138 case TTM_PL_VRAM:
139 man->func = &ttm_bo_manager_func;
140 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
141 man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
142 man->default_caching = TTM_PL_FLAG_WC;
143 break;
144 default:
145 DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
146 return -EINVAL;
147 }
148
149 return 0;
150}
151
152static void
153vbox_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
154{
155 struct vbox_bo *vboxbo = vbox_bo(bo);
156
157 if (!vbox_ttm_bo_is_vbox_bo(bo))
158 return;
159
160 vbox_ttm_placement(vboxbo, TTM_PL_FLAG_SYSTEM);
161 *pl = vboxbo->placement;
162}
163
164static int vbox_bo_verify_access(struct ttm_buffer_object *bo,
165 struct file *filp)
166{
167 return 0;
168}
169
170static int vbox_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
171 struct ttm_mem_reg *mem)
172{
173 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
174 struct vbox_private *vbox = vbox_bdev(bdev);
175
176 mem->bus.addr = NULL;
177 mem->bus.offset = 0;
178 mem->bus.size = mem->num_pages << PAGE_SHIFT;
179 mem->bus.base = 0;
180 mem->bus.is_iomem = false;
181 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
182 return -EINVAL;
183 switch (mem->mem_type) {
184 case TTM_PL_SYSTEM:
185 /* system memory */
186 return 0;
187 case TTM_PL_VRAM:
188 mem->bus.offset = mem->start << PAGE_SHIFT;
189 mem->bus.base = pci_resource_start(vbox->dev->pdev, 0);
190 mem->bus.is_iomem = true;
191 break;
192 default:
193 return -EINVAL;
194 }
195 return 0;
196}
197
198static void vbox_ttm_io_mem_free(struct ttm_bo_device *bdev,
199 struct ttm_mem_reg *mem)
200{
201}
202
203static void vbox_ttm_backend_destroy(struct ttm_tt *tt)
204{
205 ttm_tt_fini(tt);
206 kfree(tt);
207}
208
209static struct ttm_backend_func vbox_tt_backend_func = {
210 .destroy = &vbox_ttm_backend_destroy,
211};
212
213#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)) && !defined(RHEL_76) \
214 && !defined(OPENSUSE_151)
215static struct ttm_tt *vbox_ttm_tt_create(struct ttm_bo_device *bdev,
216 unsigned long size,
217 u32 page_flags,
218 struct page *dummy_read_page)
219#else
220static struct ttm_tt *vbox_ttm_tt_create(struct ttm_buffer_object *bo,
221 u32 page_flags)
222#endif
223{
224 struct ttm_tt *tt;
225
226 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
227 if (!tt)
228 return NULL;
229
230 tt->func = &vbox_tt_backend_func;
231#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)) && !defined(RHEL_76) \
232 && !defined(OPENSUSE_151)
233 if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) {
234#else
235 if (ttm_tt_init(tt, bo, page_flags)) {
236#endif
237 kfree(tt);
238 return NULL;
239 }
240
241 return tt;
242}
243
244#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
245# if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)) && !defined(RHEL_76) \
246 && !defined(OPENSUSE_151)
247static int vbox_ttm_tt_populate(struct ttm_tt *ttm)
248{
249 return ttm_pool_populate(ttm);
250}
251# else
252static int vbox_ttm_tt_populate(struct ttm_tt *ttm,
253 struct ttm_operation_ctx *ctx)
254{
255 return ttm_pool_populate(ttm, ctx);
256}
257# endif
258
259static void vbox_ttm_tt_unpopulate(struct ttm_tt *ttm)
260{
261 ttm_pool_unpopulate(ttm);
262}
263#endif
264
265static struct ttm_bo_driver vbox_bo_driver = {
266 .ttm_tt_create = vbox_ttm_tt_create,
267#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
268 .ttm_tt_populate = vbox_ttm_tt_populate,
269 .ttm_tt_unpopulate = vbox_ttm_tt_unpopulate,
270#endif
271 .init_mem_type = vbox_bo_init_mem_type,
272#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) || defined(RHEL_74)
273 .eviction_valuable = ttm_bo_eviction_valuable,
274#endif
275 .evict_flags = vbox_bo_evict_flags,
276 .verify_access = vbox_bo_verify_access,
277 .io_mem_reserve = &vbox_ttm_io_mem_reserve,
278 .io_mem_free = &vbox_ttm_io_mem_free,
279#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) || defined(RHEL_75)
280# if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)) && !defined(RHEL_76) \
281 && !defined(OPENSUSE_151)
282 .io_mem_pfn = ttm_bo_default_io_mem_pfn,
283# endif
284#endif
285#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)) \
286 || defined(RHEL_74)
287# ifndef RHEL_75
288 .lru_tail = &ttm_bo_default_lru_tail,
289 .swap_lru_tail = &ttm_bo_default_swap_lru_tail,
290# endif
291#endif
292};
293
294int vbox_mm_init(struct vbox_private *vbox)
295{
296 int ret;
297 struct drm_device *dev = vbox->dev;
298 struct ttm_bo_device *bdev = &vbox->ttm.bdev;
299
300#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
301 ret = vbox_ttm_global_init(vbox);
302 if (ret)
303 return ret;
304#endif
305 ret = ttm_bo_device_init(&vbox->ttm.bdev,
306#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
307 vbox->ttm.bo_global_ref.ref.object,
308#endif
309 &vbox_bo_driver,
310#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0) || defined(RHEL_71)
311 dev->anon_inode->i_mapping,
312#endif
313#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 2, 0)
314 DRM_FILE_PAGE_OFFSET, true);
315#else
316 true);
317#endif
318 if (ret) {
319 DRM_ERROR("Error initialising bo driver; %d\n", ret);
320#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
321 goto err_ttm_global_release;
322#else
323 return ret;
324#endif
325 }
326
327 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
328 vbox->available_vram_size >> PAGE_SHIFT);
329 if (ret) {
330 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
331 goto err_device_release;
332 }
333
334#ifdef DRM_MTRR_WC
335 vbox->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
336 pci_resource_len(dev->pdev, 0),
337 DRM_MTRR_WC);
338#else
339 vbox->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
340 pci_resource_len(dev->pdev, 0));
341#endif
342 return 0;
343
344err_device_release:
345 ttm_bo_device_release(&vbox->ttm.bdev);
346#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
347err_ttm_global_release:
348 vbox_ttm_global_release(vbox);
349#endif
350 return ret;
351}
352
353void vbox_mm_fini(struct vbox_private *vbox)
354{
355#ifdef DRM_MTRR_WC
356 drm_mtrr_del(vbox->fb_mtrr,
357 pci_resource_start(vbox->dev->pdev, 0),
358 pci_resource_len(vbox->dev->pdev, 0), DRM_MTRR_WC);
359#else
360 arch_phys_wc_del(vbox->fb_mtrr);
361#endif
362 ttm_bo_device_release(&vbox->ttm.bdev);
363#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
364 vbox_ttm_global_release(vbox);
365#endif
366}
367
368void vbox_ttm_placement(struct vbox_bo *bo, int domain)
369{
370 u32 c = 0;
371#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0) && !defined(RHEL_72)
372 bo->placement.fpfn = 0;
373 bo->placement.lpfn = 0;
374#else
375 unsigned int i;
376#endif
377
378 bo->placement.placement = bo->placements;
379 bo->placement.busy_placement = bo->placements;
380
381 if (domain & TTM_PL_FLAG_VRAM)
382 PLACEMENT_FLAGS(bo->placements[c++]) =
383 TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
384 if (domain & TTM_PL_FLAG_SYSTEM)
385 PLACEMENT_FLAGS(bo->placements[c++]) =
386 TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
387 if (!c)
388 PLACEMENT_FLAGS(bo->placements[c++]) =
389 TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
390
391 bo->placement.num_placement = c;
392 bo->placement.num_busy_placement = c;
393
394#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) || defined(RHEL_72)
395 for (i = 0; i < c; ++i) {
396 bo->placements[i].fpfn = 0;
397 bo->placements[i].lpfn = 0;
398 }
399#endif
400}
401
402int vbox_bo_create(struct drm_device *dev, int size, int align,
403 u32 flags, struct vbox_bo **pvboxbo)
404{
405 struct vbox_private *vbox = dev->dev_private;
406 struct vbox_bo *vboxbo;
407 size_t acc_size;
408 int ret;
409
410 vboxbo = kzalloc(sizeof(*vboxbo), GFP_KERNEL);
411 if (!vboxbo)
412 return -ENOMEM;
413
414 ret = drm_gem_object_init(dev, &vboxbo->gem, size);
415 if (ret)
416 goto err_free_vboxbo;
417
418 vboxbo->bo.bdev = &vbox->ttm.bdev;
419#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0) && !defined(RHEL_71)
420 vboxbo->bo.bdev->dev_mapping = dev->dev_mapping;
421#endif
422
423 vbox_ttm_placement(vboxbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
424
425 acc_size = ttm_bo_dma_acc_size(&vbox->ttm.bdev, size,
426 sizeof(struct vbox_bo));
427
428 ret = ttm_bo_init(&vbox->ttm.bdev, &vboxbo->bo, size,
429 ttm_bo_type_device, &vboxbo->placement,
430#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) && !defined(RHEL_76) \
431 && !defined(OPENSUSE_151)
432 align >> PAGE_SHIFT, false, NULL, acc_size,
433#else
434 align >> PAGE_SHIFT, false, acc_size,
435#endif
436#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) || defined(RHEL_72)
437 NULL, NULL, vbox_bo_ttm_destroy);
438#else
439 NULL, vbox_bo_ttm_destroy);
440#endif
441 if (ret)
442 goto err_free_vboxbo;
443
444 *pvboxbo = vboxbo;
445
446 return 0;
447
448err_free_vboxbo:
449 kfree(vboxbo);
450 return ret;
451}
452
453static inline u64 vbox_bo_gpu_offset(struct vbox_bo *bo)
454{
455 return bo->bo.offset;
456}
457
458int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag, u64 *gpu_addr)
459{
460#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)) || defined(RHEL_76) \
461 || defined(OPENSUSE_151)
462 struct ttm_operation_ctx ctx = { false, false };
463#endif
464 int i, ret;
465
466 if (bo->pin_count) {
467 bo->pin_count++;
468 if (gpu_addr)
469 *gpu_addr = vbox_bo_gpu_offset(bo);
470
471 return 0;
472 }
473
474 vbox_ttm_placement(bo, pl_flag);
475
476 for (i = 0; i < bo->placement.num_placement; i++)
477 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
478
479#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)) && !defined(RHEL_76) \
480 && !defined(OPENSUSE_151)
481 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
482#else
483 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
484#endif
485 if (ret)
486 return ret;
487
488 bo->pin_count = 1;
489
490 if (gpu_addr)
491 *gpu_addr = vbox_bo_gpu_offset(bo);
492
493 return 0;
494}
495
496int vbox_bo_unpin(struct vbox_bo *bo)
497{
498#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)) || defined(RHEL_76) \
499 || defined(OPENSUSE_151)
500 struct ttm_operation_ctx ctx = { false, false };
501#endif
502 int i, ret;
503
504 if (!bo->pin_count) {
505 DRM_ERROR("unpin bad %p\n", bo);
506 return 0;
507 }
508 bo->pin_count--;
509 if (bo->pin_count)
510 return 0;
511
512 for (i = 0; i < bo->placement.num_placement; i++)
513 PLACEMENT_FLAGS(bo->placements[i]) &= ~TTM_PL_FLAG_NO_EVICT;
514
515#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)) && !defined(RHEL_76) \
516 && !defined(OPENSUSE_151)
517 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
518#else
519 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
520#endif
521 if (ret)
522 return ret;
523
524 return 0;
525}
526
527/*
528 * Move a vbox-owned buffer object to system memory if no one else has it
529 * pinned. The caller must have pinned it previously, and this call will
530 * release the caller's pin.
531 */
532int vbox_bo_push_sysram(struct vbox_bo *bo)
533{
534#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)) || defined(RHEL_76) \
535 || defined(OPENSUSE_151)
536 struct ttm_operation_ctx ctx = { false, false };
537#endif
538 int i, ret;
539
540 if (!bo->pin_count) {
541 DRM_ERROR("unpin bad %p\n", bo);
542 return 0;
543 }
544 bo->pin_count--;
545 if (bo->pin_count)
546 return 0;
547
548 if (bo->kmap.virtual)
549 ttm_bo_kunmap(&bo->kmap);
550
551 vbox_ttm_placement(bo, TTM_PL_FLAG_SYSTEM);
552
553 for (i = 0; i < bo->placement.num_placement; i++)
554 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
555
556#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)) && !defined(RHEL_76) \
557 && !defined(OPENSUSE_151)
558 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
559#else
560 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
561#endif
562 if (ret) {
563 DRM_ERROR("pushing to VRAM failed\n");
564 return ret;
565 }
566
567 return 0;
568}
569
570int vbox_mmap(struct file *filp, struct vm_area_struct *vma)
571{
572 struct drm_file *file_priv;
573 struct vbox_private *vbox;
574
575 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
576 return -EINVAL;
577
578 file_priv = filp->private_data;
579 vbox = file_priv->minor->dev->dev_private;
580
581 return ttm_bo_mmap(filp, vma, &vbox->ttm.bdev);
582}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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