VirtualBox

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

最後變更 在這個檔案從88274是 88274,由 vboxsync 提交於 4 年 前

GAs: Linux: make vboxvideo work w/ 5.12-rc4 kernel, bugref:9976.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 18.6 KB
 
1/* $Id: vbox_ttm.c 88274 2021-03-24 12:49:44Z vboxsync $ */
2/** @file
3 * VirtualBox Additions Linux kernel video driver
4 */
5
6/*
7 * Copyright (C) 2013-2020 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#if RTLNX_VER_MIN(5,11,0)
37# include <drm/drm_gem.h>
38# include <drm/drm_gem_ttm_helper.h>
39# include <drm/drm_gem_vram_helper.h>
40#else
41# include <drm/ttm/ttm_page_alloc.h>
42#endif
43
44#if RTLNX_VER_MAX(3,18,0) && !RTLNX_RHEL_MAJ_PREREQ(7,2)
45#define PLACEMENT_FLAGS(placement) (placement)
46#else
47#define PLACEMENT_FLAGS(placement) ((placement).flags)
48#endif
49
50
51static inline struct vbox_private *vbox_bdev(struct ttm_bo_device *bd)
52{
53 return container_of(bd, struct vbox_private, ttm.bdev);
54}
55
56#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
57static int vbox_ttm_mem_global_init(struct drm_global_reference *ref)
58{
59 return ttm_mem_global_init(ref->object);
60}
61
62static void vbox_ttm_mem_global_release(struct drm_global_reference *ref)
63{
64 ttm_mem_global_release(ref->object);
65}
66
67/**
68 * Adds the vbox memory manager object/structures to the global memory manager.
69 */
70static int vbox_ttm_global_init(struct vbox_private *vbox)
71{
72 struct drm_global_reference *global_ref;
73 int ret;
74
75#if RTLNX_VER_MAX(5,0,0)
76 global_ref = &vbox->ttm.mem_global_ref;
77 global_ref->global_type = DRM_GLOBAL_TTM_MEM;
78 global_ref->size = sizeof(struct ttm_mem_global);
79 global_ref->init = &vbox_ttm_mem_global_init;
80 global_ref->release = &vbox_ttm_mem_global_release;
81 ret = drm_global_item_ref(global_ref);
82 if (ret) {
83 DRM_ERROR("Failed setting up TTM memory subsystem.\n");
84 return ret;
85 }
86
87 vbox->ttm.bo_global_ref.mem_glob = vbox->ttm.mem_global_ref.object;
88#endif
89 global_ref = &vbox->ttm.bo_global_ref.ref;
90 global_ref->global_type = DRM_GLOBAL_TTM_BO;
91 global_ref->size = sizeof(struct ttm_bo_global);
92 global_ref->init = &ttm_bo_global_init;
93 global_ref->release = &ttm_bo_global_release;
94
95 ret = drm_global_item_ref(global_ref);
96 if (ret) {
97 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
98#if RTLNX_VER_MAX(5,0,0)
99 drm_global_item_unref(&vbox->ttm.mem_global_ref);
100#endif
101 return ret;
102 }
103
104 return 0;
105}
106
107/**
108 * Removes the vbox memory manager object from the global memory manager.
109 */
110static void vbox_ttm_global_release(struct vbox_private *vbox)
111{
112 drm_global_item_unref(&vbox->ttm.bo_global_ref.ref);
113 drm_global_item_unref(&vbox->ttm.mem_global_ref);
114}
115#endif
116
117static void vbox_bo_ttm_destroy(struct ttm_buffer_object *tbo)
118{
119 struct vbox_bo *bo;
120
121 bo = container_of(tbo, struct vbox_bo, bo);
122
123 drm_gem_object_release(&bo->gem);
124 kfree(bo);
125}
126
127static bool vbox_ttm_bo_is_vbox_bo(struct ttm_buffer_object *bo)
128{
129 if (bo->destroy == &vbox_bo_ttm_destroy)
130 return true;
131
132 return false;
133}
134
135#if RTLNX_VER_MAX(5,10,0)
136static int
137vbox_bo_init_mem_type(struct ttm_bo_device *bdev, u32 type,
138 struct ttm_mem_type_manager *man)
139{
140 switch (type) {
141 case TTM_PL_SYSTEM:
142 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
143 man->available_caching = TTM_PL_MASK_CACHING;
144 man->default_caching = TTM_PL_FLAG_CACHED;
145 break;
146 case TTM_PL_VRAM:
147 man->func = &ttm_bo_manager_func;
148 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
149 man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
150 man->default_caching = TTM_PL_FLAG_WC;
151 break;
152 default:
153 DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
154 return -EINVAL;
155 }
156
157 return 0;
158}
159#endif
160
161static void
162vbox_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
163{
164 struct vbox_bo *vboxbo = vbox_bo(bo);
165
166 if (!vbox_ttm_bo_is_vbox_bo(bo))
167 return;
168
169 vbox_ttm_placement(vboxbo, VBOX_MEM_TYPE_SYSTEM);
170 *pl = vboxbo->placement;
171}
172
173static int vbox_bo_verify_access(struct ttm_buffer_object *bo,
174 struct file *filp)
175{
176 return 0;
177}
178
179#if RTLNX_VER_MAX(5,10,0)
180static int vbox_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
181 struct ttm_mem_reg *mem)
182{
183 struct vbox_private *vbox = vbox_bdev(bdev);
184 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
185
186 mem->bus.addr = NULL;
187 mem->bus.offset = 0;
188 mem->bus.size = mem->num_pages << PAGE_SHIFT;
189 mem->bus.base = 0;
190 mem->bus.is_iomem = false;
191 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
192 return -EINVAL;
193 switch (mem->mem_type) {
194 case TTM_PL_SYSTEM:
195 /* system memory */
196 return 0;
197 case TTM_PL_VRAM:
198 mem->bus.offset = mem->start << PAGE_SHIFT;
199 mem->bus.base = pci_resource_start(vbox->dev->pdev, 0);
200 mem->bus.is_iomem = true;
201 break;
202 default:
203 return -EINVAL;
204 }
205 return 0;
206}
207#else
208static int vbox_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
209 struct ttm_resource *mem)
210{
211 struct vbox_private *vbox = vbox_bdev(bdev);
212 mem->bus.addr = NULL;
213 mem->bus.offset = 0;
214# if RTLNX_VER_MAX(5,12,0)
215 mem->size = mem->num_pages << PAGE_SHIFT;
216# endif
217 mem->start = 0;
218 mem->bus.is_iomem = false;
219 switch (mem->mem_type) {
220 case TTM_PL_SYSTEM:
221 /* system memory */
222 return 0;
223 case TTM_PL_VRAM:
224# if RTLNX_VER_MIN(5,11,0)
225 mem->bus.caching = ttm_write_combined;
226 mem->bus.offset = (mem->start << PAGE_SHIFT) + pci_resource_start(vbox->dev->pdev, 0);
227# else
228 mem->bus.offset = mem->start << PAGE_SHIFT;
229 mem->start = pci_resource_start(vbox->dev->pdev, 0);
230# endif
231 mem->bus.is_iomem = true;
232 break;
233 default:
234 return -EINVAL;
235 }
236 return 0;
237}
238#endif
239
240
241
242#if RTLNX_VER_MIN(5,10,0)
243static void vbox_ttm_io_mem_free(struct ttm_bo_device *bdev,
244 struct ttm_resource *mem)
245{
246}
247#else
248static void vbox_ttm_io_mem_free(struct ttm_bo_device *bdev,
249 struct ttm_mem_reg *mem)
250{
251}
252#endif
253
254#if RTLNX_VER_MIN(5,10,0)
255static void vbox_ttm_tt_destroy(struct ttm_bo_device *bdev, struct ttm_tt *tt)
256{
257 ttm_tt_fini(tt);
258 kfree(tt);
259}
260#else
261static void vbox_ttm_backend_destroy(struct ttm_tt *tt)
262{
263 ttm_tt_fini(tt);
264 kfree(tt);
265}
266
267static struct ttm_backend_func vbox_tt_backend_func = {
268 .destroy = &vbox_ttm_backend_destroy,
269};
270#endif
271
272#if RTLNX_VER_MAX(4,17,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
273static struct ttm_tt *vbox_ttm_tt_create(struct ttm_bo_device *bdev,
274 unsigned long size,
275 u32 page_flags,
276 struct page *dummy_read_page)
277#else
278static struct ttm_tt *vbox_ttm_tt_create(struct ttm_buffer_object *bo,
279 u32 page_flags)
280#endif
281{
282 struct ttm_tt *tt;
283
284 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
285 if (!tt)
286 return NULL;
287
288#if RTLNX_VER_MAX(5,10,0)
289 tt->func = &vbox_tt_backend_func;
290#endif
291#if RTLNX_VER_MAX(4,17,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
292 if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) {
293#elif RTLNX_VER_MAX(5,11,0)
294 if (ttm_tt_init(tt, bo, page_flags)) {
295#else
296 if (ttm_tt_init(tt, bo, page_flags, ttm_write_combined)) {
297#endif
298 kfree(tt);
299 return NULL;
300 }
301
302 return tt;
303}
304
305#if RTLNX_VER_MAX(4,17,0)
306# if RTLNX_VER_MAX(4,16,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
307static int vbox_ttm_tt_populate(struct ttm_tt *ttm)
308{
309 return ttm_pool_populate(ttm);
310}
311# else
312static int vbox_ttm_tt_populate(struct ttm_tt *ttm,
313 struct ttm_operation_ctx *ctx)
314{
315 return ttm_pool_populate(ttm, ctx);
316}
317# endif
318
319static void vbox_ttm_tt_unpopulate(struct ttm_tt *ttm)
320{
321 ttm_pool_unpopulate(ttm);
322}
323#endif
324
325#if RTLNX_VER_MIN(5,11,0)
326static int vbox_bo_move(struct ttm_buffer_object *bo, bool evict,
327 struct ttm_operation_ctx *ctx, struct ttm_resource *new_mem,
328 struct ttm_place *hop)
329{
330 return ttm_bo_move_memcpy(bo, ctx, new_mem);
331}
332#endif
333
334static struct ttm_bo_driver vbox_bo_driver = {
335 .ttm_tt_create = vbox_ttm_tt_create,
336#if RTLNX_VER_MIN(5,10,0)
337 .ttm_tt_destroy = vbox_ttm_tt_destroy,
338#endif
339#if RTLNX_VER_MAX(4,17,0)
340 .ttm_tt_populate = vbox_ttm_tt_populate,
341 .ttm_tt_unpopulate = vbox_ttm_tt_unpopulate,
342#endif
343#if RTLNX_VER_MAX(5,10,0)
344 .init_mem_type = vbox_bo_init_mem_type,
345#endif
346#if RTLNX_VER_MIN(4,10,0) || RTLNX_RHEL_MAJ_PREREQ(7,4)
347 .eviction_valuable = ttm_bo_eviction_valuable,
348#endif
349 .evict_flags = vbox_bo_evict_flags,
350 .verify_access = vbox_bo_verify_access,
351 .io_mem_reserve = &vbox_ttm_io_mem_reserve,
352 .io_mem_free = &vbox_ttm_io_mem_free,
353#if RTLNX_VER_MIN(4,12,0) || RTLNX_RHEL_MAJ_PREREQ(7,5)
354# if RTLNX_VER_MAX(4,16,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
355 .io_mem_pfn = ttm_bo_default_io_mem_pfn,
356# endif
357#endif
358#if (RTLNX_VER_RANGE(4,7,0, 4,11,0) || RTLNX_RHEL_MAJ_PREREQ(7,4)) && !RTLNX_RHEL_MAJ_PREREQ(7,5)
359 .lru_tail = &ttm_bo_default_lru_tail,
360 .swap_lru_tail = &ttm_bo_default_swap_lru_tail,
361#endif
362#if RTLNX_VER_MIN(5,11,0)
363 .move = &vbox_bo_move,
364#endif
365};
366
367int vbox_mm_init(struct vbox_private *vbox)
368{
369 int ret;
370 struct drm_device *dev = vbox->dev;
371 struct ttm_bo_device *bdev = &vbox->ttm.bdev;
372
373#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
374 ret = vbox_ttm_global_init(vbox);
375 if (ret)
376 return ret;
377#endif
378 ret = ttm_bo_device_init(&vbox->ttm.bdev,
379#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
380 vbox->ttm.bo_global_ref.ref.object,
381#endif
382 &vbox_bo_driver,
383#if RTLNX_VER_MIN(5,11,0)
384 dev->dev,
385#endif
386#if RTLNX_VER_MIN(3,15,0) || RTLNX_RHEL_MAJ_PREREQ(7,1)
387 dev->anon_inode->i_mapping,
388#endif
389#if RTLNX_VER_MIN(5,5,0) || RTLNX_RHEL_MIN(8,3)
390 dev->vma_offset_manager,
391#elif RTLNX_VER_MAX(5,2,0) && !RTLNX_RHEL_MAJ_PREREQ(8,2)
392 DRM_FILE_PAGE_OFFSET,
393#endif
394#if RTLNX_VER_MIN(5,11,0)
395 false,
396#endif
397 true);
398 if (ret) {
399 DRM_ERROR("Error initialising bo driver; %d\n", ret);
400#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
401 goto err_ttm_global_release;
402#else
403 return ret;
404#endif
405 }
406
407#if RTLNX_VER_MIN(5,10,0)
408 ret = ttm_range_man_init(bdev, TTM_PL_VRAM, false,
409 vbox->available_vram_size >> PAGE_SHIFT);
410#else
411 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
412 vbox->available_vram_size >> PAGE_SHIFT);
413#endif
414 if (ret) {
415 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
416 goto err_device_release;
417 }
418
419#ifdef DRM_MTRR_WC
420 vbox->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
421 pci_resource_len(dev->pdev, 0),
422 DRM_MTRR_WC);
423#else
424 vbox->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
425 pci_resource_len(dev->pdev, 0));
426#endif
427 return 0;
428
429err_device_release:
430 ttm_bo_device_release(&vbox->ttm.bdev);
431#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
432err_ttm_global_release:
433 vbox_ttm_global_release(vbox);
434#endif
435 return ret;
436}
437
438void vbox_mm_fini(struct vbox_private *vbox)
439{
440#ifdef DRM_MTRR_WC
441 drm_mtrr_del(vbox->fb_mtrr,
442 pci_resource_start(vbox->dev->pdev, 0),
443 pci_resource_len(vbox->dev->pdev, 0), DRM_MTRR_WC);
444#else
445 arch_phys_wc_del(vbox->fb_mtrr);
446#endif
447 ttm_bo_device_release(&vbox->ttm.bdev);
448#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
449 vbox_ttm_global_release(vbox);
450#endif
451}
452
453void vbox_ttm_placement(struct vbox_bo *bo, u32 mem_type)
454{
455 u32 c = 0;
456#if RTLNX_VER_MAX(3,18,0) && !RTLNX_RHEL_MAJ_PREREQ(7,2)
457 bo->placement.fpfn = 0;
458 bo->placement.lpfn = 0;
459#else
460 unsigned int i;
461#endif
462
463 bo->placement.placement = bo->placements;
464 bo->placement.busy_placement = bo->placements;
465
466 if (mem_type & VBOX_MEM_TYPE_VRAM) {
467#if RTLNX_VER_MIN(5,11,0)
468 bo->placements[c].mem_type = TTM_PL_VRAM;
469 PLACEMENT_FLAGS(bo->placements[c++]) = 0;
470#elif RTLNX_VER_MIN(5,10,0)
471 bo->placements[c].mem_type = TTM_PL_VRAM;
472 PLACEMENT_FLAGS(bo->placements[c++]) =
473 TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED;
474#else
475 PLACEMENT_FLAGS(bo->placements[c++]) =
476 TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
477#endif
478 }
479 if (mem_type & VBOX_MEM_TYPE_SYSTEM) {
480#if RTLNX_VER_MIN(5,11,0)
481 bo->placements[c].mem_type = TTM_PL_SYSTEM;
482 PLACEMENT_FLAGS(bo->placements[c++]) = 0;
483#elif RTLNX_VER_MIN(5,10,0)
484 bo->placements[c].mem_type = TTM_PL_SYSTEM;
485 PLACEMENT_FLAGS(bo->placements[c++]) =
486 TTM_PL_MASK_CACHING;
487#else
488 PLACEMENT_FLAGS(bo->placements[c++]) =
489 TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
490#endif
491 }
492 if (!c) {
493#if RTLNX_VER_MIN(5,11,0)
494 bo->placements[c].mem_type = TTM_PL_SYSTEM;
495 PLACEMENT_FLAGS(bo->placements[c++]) = 0;
496#elif RTLNX_VER_MIN(5,10,0)
497 bo->placements[c].mem_type = TTM_PL_SYSTEM;
498 PLACEMENT_FLAGS(bo->placements[c++]) =
499 TTM_PL_MASK_CACHING;
500#else
501 PLACEMENT_FLAGS(bo->placements[c++]) =
502 TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
503#endif
504 }
505
506 bo->placement.num_placement = c;
507 bo->placement.num_busy_placement = c;
508
509#if RTLNX_VER_MIN(3,18,0) || RTLNX_RHEL_MAJ_PREREQ(7,2)
510 for (i = 0; i < c; ++i) {
511 bo->placements[i].fpfn = 0;
512 bo->placements[i].lpfn = 0;
513 }
514#endif
515}
516
517#if RTLNX_VER_MIN(5,11,0)
518static const struct drm_gem_object_funcs vbox_drm_gem_object_funcs = {
519 .free = vbox_gem_free_object,
520 .print_info = drm_gem_ttm_print_info,
521};
522#endif
523
524int vbox_bo_create(struct drm_device *dev, int size, int align,
525 u32 flags, struct vbox_bo **pvboxbo)
526{
527 struct vbox_private *vbox = dev->dev_private;
528 struct vbox_bo *vboxbo;
529 size_t acc_size;
530 int ret;
531
532 vboxbo = kzalloc(sizeof(*vboxbo), GFP_KERNEL);
533 if (!vboxbo)
534 return -ENOMEM;
535
536 ret = drm_gem_object_init(dev, &vboxbo->gem, size);
537 if (ret)
538 goto err_free_vboxbo;
539
540#if RTLNX_VER_MIN(5,11,0)
541 if (!vboxbo->gem.funcs) {
542 vboxbo->gem.funcs = &vbox_drm_gem_object_funcs;
543 }
544#endif
545 vboxbo->bo.bdev = &vbox->ttm.bdev;
546#if RTLNX_VER_MAX(3,15,0) && !RTLNX_RHEL_MAJ_PREREQ(7,1)
547 vboxbo->bo.bdev->dev_mapping = dev->dev_mapping;
548#endif
549
550 vbox_ttm_placement(vboxbo, VBOX_MEM_TYPE_VRAM | VBOX_MEM_TYPE_SYSTEM);
551
552 acc_size = ttm_bo_dma_acc_size(&vbox->ttm.bdev, size,
553 sizeof(struct vbox_bo));
554
555 ret = ttm_bo_init(&vbox->ttm.bdev, &vboxbo->bo, size,
556 ttm_bo_type_device, &vboxbo->placement,
557#if RTLNX_VER_MAX(4,17,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
558 align >> PAGE_SHIFT, false, NULL, acc_size,
559#else
560 align >> PAGE_SHIFT, false, acc_size,
561#endif
562#if RTLNX_VER_MIN(3,18,0) || RTLNX_RHEL_MAJ_PREREQ(7,2)
563 NULL, NULL, vbox_bo_ttm_destroy);
564#else
565 NULL, vbox_bo_ttm_destroy);
566#endif
567 if (ret)
568 goto err_free_vboxbo;
569
570 *pvboxbo = vboxbo;
571
572 return 0;
573
574err_free_vboxbo:
575 kfree(vboxbo);
576 return ret;
577}
578
579static inline u64 vbox_bo_gpu_offset(struct vbox_bo *bo)
580{
581#if RTLNX_VER_MIN(5,9,0)
582 return bo->bo.mem.start << PAGE_SHIFT;
583#else
584 return bo->bo.offset;
585#endif
586}
587
588int vbox_bo_pin(struct vbox_bo *bo, u32 mem_type, u64 *gpu_addr)
589{
590#if RTLNX_VER_MIN(4,16,0) || RTLNX_RHEL_MAJ_PREREQ(7,6) || RTLNX_SUSE_MAJ_PREREQ(15,1) || RTLNX_SUSE_MAJ_PREREQ(12,5)
591 struct ttm_operation_ctx ctx = { false, false };
592#endif
593 int ret;
594#if RTLNX_VER_MAX(5,11,0)
595 int i;
596#endif
597
598 if (bo->pin_count) {
599 bo->pin_count++;
600 if (gpu_addr)
601 *gpu_addr = vbox_bo_gpu_offset(bo);
602
603 return 0;
604 }
605
606 vbox_ttm_placement(bo, mem_type);
607
608#if RTLNX_VER_MAX(5,11,0)
609 for (i = 0; i < bo->placement.num_placement; i++)
610 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
611#endif
612
613#if RTLNX_VER_MAX(4,16,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
614 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
615#else
616 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
617#endif
618 if (ret)
619 return ret;
620
621 bo->pin_count = 1;
622
623#if RTLNX_VER_MIN(5,11,0)
624 ttm_bo_pin(&bo->bo);
625#endif
626
627 if (gpu_addr)
628 *gpu_addr = vbox_bo_gpu_offset(bo);
629
630 return 0;
631}
632
633int vbox_bo_unpin(struct vbox_bo *bo)
634{
635#if RTLNX_VER_MIN(4,16,0) || RTLNX_RHEL_MAJ_PREREQ(7,6) || RTLNX_SUSE_MAJ_PREREQ(15,1) || RTLNX_SUSE_MAJ_PREREQ(12,5)
636# if RTLNX_VER_MAX(5,11,0)
637 struct ttm_operation_ctx ctx = { false, false };
638# endif
639#endif
640 int ret;
641#if RTLNX_VER_MAX(5,11,0)
642 int i;
643#endif
644
645 if (!bo->pin_count) {
646 DRM_ERROR("unpin bad %p\n", bo);
647 return 0;
648 }
649 bo->pin_count--;
650 if (bo->pin_count)
651 return 0;
652
653#if RTLNX_VER_MAX(5,11,0)
654 for (i = 0; i < bo->placement.num_placement; i++)
655 PLACEMENT_FLAGS(bo->placements[i]) &= ~TTM_PL_FLAG_NO_EVICT;
656#endif
657
658#if RTLNX_VER_MAX(4,16,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
659 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
660#elif RTLNX_VER_MAX(5,11,0)
661 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
662#endif
663 if (ret)
664 return ret;
665
666#if RTLNX_VER_MIN(5,11,0)
667 ttm_bo_unpin(&bo->bo);
668#endif
669
670 return 0;
671}
672
673#if RTLNX_VER_MAX(5,11,0)
674/*
675 * Move a vbox-owned buffer object to system memory if no one else has it
676 * pinned. The caller must have pinned it previously, and this call will
677 * release the caller's pin.
678 */
679int vbox_bo_push_sysram(struct vbox_bo *bo)
680{
681# if RTLNX_VER_MIN(4,16,0) || RTLNX_RHEL_MAJ_PREREQ(7,6) || RTLNX_SUSE_MAJ_PREREQ(15,1) || RTLNX_SUSE_MAJ_PREREQ(12,5)
682 struct ttm_operation_ctx ctx = { false, false };
683# endif
684 int i, ret;
685
686 if (!bo->pin_count) {
687 DRM_ERROR("unpin bad %p\n", bo);
688 return 0;
689 }
690 bo->pin_count--;
691 if (bo->pin_count)
692 return 0;
693
694 if (bo->kmap.virtual)
695 ttm_bo_kunmap(&bo->kmap);
696
697 vbox_ttm_placement(bo, VBOX_MEM_TYPE_SYSTEM);
698
699 for (i = 0; i < bo->placement.num_placement; i++)
700 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
701
702# if RTLNX_VER_MAX(4,16,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
703 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
704# else
705 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
706# endif
707 if (ret) {
708 DRM_ERROR("pushing to VRAM failed\n");
709 return ret;
710 }
711
712 return 0;
713}
714#endif
715
716int vbox_mmap(struct file *filp, struct vm_area_struct *vma)
717{
718 struct drm_file *file_priv;
719 struct vbox_private *vbox;
720
721 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
722 return -EINVAL;
723
724 file_priv = filp->private_data;
725 vbox = file_priv->minor->dev->dev_private;
726
727 return ttm_bo_mmap(filp, vma, &vbox->ttm.bdev);
728}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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