VirtualBox

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

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

bugref:9961: Additions/x11: vboxvideo: introduce support for 5.11.x kernels.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 18.6 KB
 
1/* $Id: vbox_ttm.c 88207 2021-03-19 17:22:21Z 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 mem->size = mem->num_pages << PAGE_SHIFT;
215 mem->start = 0;
216 mem->bus.is_iomem = false;
217 switch (mem->mem_type) {
218 case TTM_PL_SYSTEM:
219 /* system memory */
220 return 0;
221 case TTM_PL_VRAM:
222# if RTLNX_VER_MIN(5,11,0)
223 mem->bus.caching = ttm_write_combined;
224 mem->bus.offset = (mem->start << PAGE_SHIFT) + pci_resource_start(vbox->dev->pdev, 0);
225# else
226 mem->bus.offset = mem->start << PAGE_SHIFT;
227 mem->start = pci_resource_start(vbox->dev->pdev, 0);
228# endif
229 mem->bus.is_iomem = true;
230 break;
231 default:
232 return -EINVAL;
233 }
234 return 0;
235}
236#endif
237
238
239
240#if RTLNX_VER_MIN(5,10,0)
241static void vbox_ttm_io_mem_free(struct ttm_bo_device *bdev,
242 struct ttm_resource *mem)
243{
244}
245#else
246static void vbox_ttm_io_mem_free(struct ttm_bo_device *bdev,
247 struct ttm_mem_reg *mem)
248{
249}
250#endif
251
252#if RTLNX_VER_MIN(5,10,0)
253static void vbox_ttm_tt_destroy(struct ttm_bo_device *bdev, struct ttm_tt *tt)
254{
255 ttm_tt_fini(tt);
256 kfree(tt);
257}
258#else
259static void vbox_ttm_backend_destroy(struct ttm_tt *tt)
260{
261 ttm_tt_fini(tt);
262 kfree(tt);
263}
264
265static struct ttm_backend_func vbox_tt_backend_func = {
266 .destroy = &vbox_ttm_backend_destroy,
267};
268#endif
269
270#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)
271static struct ttm_tt *vbox_ttm_tt_create(struct ttm_bo_device *bdev,
272 unsigned long size,
273 u32 page_flags,
274 struct page *dummy_read_page)
275#else
276static struct ttm_tt *vbox_ttm_tt_create(struct ttm_buffer_object *bo,
277 u32 page_flags)
278#endif
279{
280 struct ttm_tt *tt;
281
282 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
283 if (!tt)
284 return NULL;
285
286#if RTLNX_VER_MAX(5,10,0)
287 tt->func = &vbox_tt_backend_func;
288#endif
289#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)
290 if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) {
291#elif RTLNX_VER_MAX(5,11,0)
292 if (ttm_tt_init(tt, bo, page_flags)) {
293#else
294 if (ttm_tt_init(tt, bo, page_flags, ttm_write_combined)) {
295#endif
296 kfree(tt);
297 return NULL;
298 }
299
300 return tt;
301}
302
303#if RTLNX_VER_MAX(4,17,0)
304# 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)
305static int vbox_ttm_tt_populate(struct ttm_tt *ttm)
306{
307 return ttm_pool_populate(ttm);
308}
309# else
310static int vbox_ttm_tt_populate(struct ttm_tt *ttm,
311 struct ttm_operation_ctx *ctx)
312{
313 return ttm_pool_populate(ttm, ctx);
314}
315# endif
316
317static void vbox_ttm_tt_unpopulate(struct ttm_tt *ttm)
318{
319 ttm_pool_unpopulate(ttm);
320}
321#endif
322
323#if RTLNX_VER_MIN(5,11,0)
324static int vbox_bo_move(struct ttm_buffer_object *bo, bool evict,
325 struct ttm_operation_ctx *ctx, struct ttm_resource *new_mem,
326 struct ttm_place *hop)
327{
328 return ttm_bo_move_memcpy(bo, ctx, new_mem);
329}
330#endif
331
332static struct ttm_bo_driver vbox_bo_driver = {
333 .ttm_tt_create = vbox_ttm_tt_create,
334#if RTLNX_VER_MIN(5,10,0)
335 .ttm_tt_destroy = vbox_ttm_tt_destroy,
336#endif
337#if RTLNX_VER_MAX(4,17,0)
338 .ttm_tt_populate = vbox_ttm_tt_populate,
339 .ttm_tt_unpopulate = vbox_ttm_tt_unpopulate,
340#endif
341#if RTLNX_VER_MAX(5,10,0)
342 .init_mem_type = vbox_bo_init_mem_type,
343#endif
344#if RTLNX_VER_MIN(4,10,0) || RTLNX_RHEL_MAJ_PREREQ(7,4)
345 .eviction_valuable = ttm_bo_eviction_valuable,
346#endif
347 .evict_flags = vbox_bo_evict_flags,
348 .verify_access = vbox_bo_verify_access,
349 .io_mem_reserve = &vbox_ttm_io_mem_reserve,
350 .io_mem_free = &vbox_ttm_io_mem_free,
351#if RTLNX_VER_MIN(4,12,0) || RTLNX_RHEL_MAJ_PREREQ(7,5)
352# 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)
353 .io_mem_pfn = ttm_bo_default_io_mem_pfn,
354# endif
355#endif
356#if (RTLNX_VER_RANGE(4,7,0, 4,11,0) || RTLNX_RHEL_MAJ_PREREQ(7,4)) && !RTLNX_RHEL_MAJ_PREREQ(7,5)
357 .lru_tail = &ttm_bo_default_lru_tail,
358 .swap_lru_tail = &ttm_bo_default_swap_lru_tail,
359#endif
360#if RTLNX_VER_MIN(5,11,0)
361 .move = &vbox_bo_move,
362#endif
363};
364
365int vbox_mm_init(struct vbox_private *vbox)
366{
367 int ret;
368 struct drm_device *dev = vbox->dev;
369 struct ttm_bo_device *bdev = &vbox->ttm.bdev;
370
371#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
372 ret = vbox_ttm_global_init(vbox);
373 if (ret)
374 return ret;
375#endif
376 ret = ttm_bo_device_init(&vbox->ttm.bdev,
377#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
378 vbox->ttm.bo_global_ref.ref.object,
379#endif
380 &vbox_bo_driver,
381#if RTLNX_VER_MIN(5,11,0)
382 dev->dev,
383#endif
384#if RTLNX_VER_MIN(3,15,0) || RTLNX_RHEL_MAJ_PREREQ(7,1)
385 dev->anon_inode->i_mapping,
386#endif
387#if RTLNX_VER_MIN(5,5,0) || RTLNX_RHEL_MIN(8,3)
388 dev->vma_offset_manager,
389#elif RTLNX_VER_MAX(5,2,0) && !RTLNX_RHEL_MAJ_PREREQ(8,2)
390 DRM_FILE_PAGE_OFFSET,
391#endif
392#if RTLNX_VER_MIN(5,11,0)
393 false,
394#endif
395 true);
396 if (ret) {
397 DRM_ERROR("Error initialising bo driver; %d\n", ret);
398#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
399 goto err_ttm_global_release;
400#else
401 return ret;
402#endif
403 }
404
405#if RTLNX_VER_MIN(5,10,0)
406 ret = ttm_range_man_init(bdev, TTM_PL_VRAM, false,
407 vbox->available_vram_size >> PAGE_SHIFT);
408#else
409 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
410 vbox->available_vram_size >> PAGE_SHIFT);
411#endif
412 if (ret) {
413 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
414 goto err_device_release;
415 }
416
417#ifdef DRM_MTRR_WC
418 vbox->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
419 pci_resource_len(dev->pdev, 0),
420 DRM_MTRR_WC);
421#else
422 vbox->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
423 pci_resource_len(dev->pdev, 0));
424#endif
425 return 0;
426
427err_device_release:
428 ttm_bo_device_release(&vbox->ttm.bdev);
429#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
430err_ttm_global_release:
431 vbox_ttm_global_release(vbox);
432#endif
433 return ret;
434}
435
436void vbox_mm_fini(struct vbox_private *vbox)
437{
438#ifdef DRM_MTRR_WC
439 drm_mtrr_del(vbox->fb_mtrr,
440 pci_resource_start(vbox->dev->pdev, 0),
441 pci_resource_len(vbox->dev->pdev, 0), DRM_MTRR_WC);
442#else
443 arch_phys_wc_del(vbox->fb_mtrr);
444#endif
445 ttm_bo_device_release(&vbox->ttm.bdev);
446#if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
447 vbox_ttm_global_release(vbox);
448#endif
449}
450
451void vbox_ttm_placement(struct vbox_bo *bo, u32 mem_type)
452{
453 u32 c = 0;
454#if RTLNX_VER_MAX(3,18,0) && !RTLNX_RHEL_MAJ_PREREQ(7,2)
455 bo->placement.fpfn = 0;
456 bo->placement.lpfn = 0;
457#else
458 unsigned int i;
459#endif
460
461 bo->placement.placement = bo->placements;
462 bo->placement.busy_placement = bo->placements;
463
464 if (mem_type & VBOX_MEM_TYPE_VRAM) {
465#if RTLNX_VER_MIN(5,11,0)
466 bo->placements[c].mem_type = TTM_PL_VRAM;
467 PLACEMENT_FLAGS(bo->placements[c++]) = 0;
468#elif RTLNX_VER_MIN(5,10,0)
469 bo->placements[c].mem_type = TTM_PL_VRAM;
470 PLACEMENT_FLAGS(bo->placements[c++]) =
471 TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED;
472#else
473 PLACEMENT_FLAGS(bo->placements[c++]) =
474 TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
475#endif
476 }
477 if (mem_type & VBOX_MEM_TYPE_SYSTEM) {
478#if RTLNX_VER_MIN(5,11,0)
479 bo->placements[c].mem_type = TTM_PL_SYSTEM;
480 PLACEMENT_FLAGS(bo->placements[c++]) = 0;
481#elif RTLNX_VER_MIN(5,10,0)
482 bo->placements[c].mem_type = TTM_PL_SYSTEM;
483 PLACEMENT_FLAGS(bo->placements[c++]) =
484 TTM_PL_MASK_CACHING;
485#else
486 PLACEMENT_FLAGS(bo->placements[c++]) =
487 TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
488#endif
489 }
490 if (!c) {
491#if RTLNX_VER_MIN(5,11,0)
492 bo->placements[c].mem_type = TTM_PL_SYSTEM;
493 PLACEMENT_FLAGS(bo->placements[c++]) = 0;
494#elif RTLNX_VER_MIN(5,10,0)
495 bo->placements[c].mem_type = TTM_PL_SYSTEM;
496 PLACEMENT_FLAGS(bo->placements[c++]) =
497 TTM_PL_MASK_CACHING;
498#else
499 PLACEMENT_FLAGS(bo->placements[c++]) =
500 TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
501#endif
502 }
503
504 bo->placement.num_placement = c;
505 bo->placement.num_busy_placement = c;
506
507#if RTLNX_VER_MIN(3,18,0) || RTLNX_RHEL_MAJ_PREREQ(7,2)
508 for (i = 0; i < c; ++i) {
509 bo->placements[i].fpfn = 0;
510 bo->placements[i].lpfn = 0;
511 }
512#endif
513}
514
515#if RTLNX_VER_MIN(5,11,0)
516static const struct drm_gem_object_funcs vbox_drm_gem_object_funcs = {
517 .free = vbox_gem_free_object,
518 .print_info = drm_gem_ttm_print_info,
519};
520#endif
521
522int vbox_bo_create(struct drm_device *dev, int size, int align,
523 u32 flags, struct vbox_bo **pvboxbo)
524{
525 struct vbox_private *vbox = dev->dev_private;
526 struct vbox_bo *vboxbo;
527 size_t acc_size;
528 int ret;
529
530 vboxbo = kzalloc(sizeof(*vboxbo), GFP_KERNEL);
531 if (!vboxbo)
532 return -ENOMEM;
533
534 ret = drm_gem_object_init(dev, &vboxbo->gem, size);
535 if (ret)
536 goto err_free_vboxbo;
537
538#if RTLNX_VER_MIN(5,11,0)
539 if (!vboxbo->gem.funcs) {
540 vboxbo->gem.funcs = &vbox_drm_gem_object_funcs;
541 }
542#endif
543 vboxbo->bo.bdev = &vbox->ttm.bdev;
544#if RTLNX_VER_MAX(3,15,0) && !RTLNX_RHEL_MAJ_PREREQ(7,1)
545 vboxbo->bo.bdev->dev_mapping = dev->dev_mapping;
546#endif
547
548 vbox_ttm_placement(vboxbo, VBOX_MEM_TYPE_VRAM | VBOX_MEM_TYPE_SYSTEM);
549
550 acc_size = ttm_bo_dma_acc_size(&vbox->ttm.bdev, size,
551 sizeof(struct vbox_bo));
552
553 ret = ttm_bo_init(&vbox->ttm.bdev, &vboxbo->bo, size,
554 ttm_bo_type_device, &vboxbo->placement,
555#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)
556 align >> PAGE_SHIFT, false, NULL, acc_size,
557#else
558 align >> PAGE_SHIFT, false, acc_size,
559#endif
560#if RTLNX_VER_MIN(3,18,0) || RTLNX_RHEL_MAJ_PREREQ(7,2)
561 NULL, NULL, vbox_bo_ttm_destroy);
562#else
563 NULL, vbox_bo_ttm_destroy);
564#endif
565 if (ret)
566 goto err_free_vboxbo;
567
568 *pvboxbo = vboxbo;
569
570 return 0;
571
572err_free_vboxbo:
573 kfree(vboxbo);
574 return ret;
575}
576
577static inline u64 vbox_bo_gpu_offset(struct vbox_bo *bo)
578{
579#if RTLNX_VER_MIN(5,9,0)
580 return bo->bo.mem.start << PAGE_SHIFT;
581#else
582 return bo->bo.offset;
583#endif
584}
585
586int vbox_bo_pin(struct vbox_bo *bo, u32 mem_type, u64 *gpu_addr)
587{
588#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)
589 struct ttm_operation_ctx ctx = { false, false };
590#endif
591 int ret;
592#if RTLNX_VER_MAX(5,11,0)
593 int i;
594#endif
595
596 if (bo->pin_count) {
597 bo->pin_count++;
598 if (gpu_addr)
599 *gpu_addr = vbox_bo_gpu_offset(bo);
600
601 return 0;
602 }
603
604 vbox_ttm_placement(bo, mem_type);
605
606#if RTLNX_VER_MAX(5,11,0)
607 for (i = 0; i < bo->placement.num_placement; i++)
608 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
609#endif
610
611#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)
612 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
613#else
614 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
615#endif
616 if (ret)
617 return ret;
618
619 bo->pin_count = 1;
620
621#if RTLNX_VER_MIN(5,11,0)
622 ttm_bo_pin(&bo->bo);
623#endif
624
625 if (gpu_addr)
626 *gpu_addr = vbox_bo_gpu_offset(bo);
627
628 return 0;
629}
630
631int vbox_bo_unpin(struct vbox_bo *bo)
632{
633#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)
634# if RTLNX_VER_MAX(5,11,0)
635 struct ttm_operation_ctx ctx = { false, false };
636# endif
637#endif
638 int ret;
639#if RTLNX_VER_MAX(5,11,0)
640 int i;
641#endif
642
643 if (!bo->pin_count) {
644 DRM_ERROR("unpin bad %p\n", bo);
645 return 0;
646 }
647 bo->pin_count--;
648 if (bo->pin_count)
649 return 0;
650
651#if RTLNX_VER_MAX(5,11,0)
652 for (i = 0; i < bo->placement.num_placement; i++)
653 PLACEMENT_FLAGS(bo->placements[i]) &= ~TTM_PL_FLAG_NO_EVICT;
654#endif
655
656#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)
657 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
658#elif RTLNX_VER_MAX(5,11,0)
659 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
660#endif
661 if (ret)
662 return ret;
663
664#if RTLNX_VER_MIN(5,11,0)
665 ttm_bo_unpin(&bo->bo);
666#endif
667
668 return 0;
669}
670
671#if RTLNX_VER_MAX(5,11,0)
672/*
673 * Move a vbox-owned buffer object to system memory if no one else has it
674 * pinned. The caller must have pinned it previously, and this call will
675 * release the caller's pin.
676 */
677int vbox_bo_push_sysram(struct vbox_bo *bo)
678{
679# 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)
680 struct ttm_operation_ctx ctx = { false, false };
681# endif
682 int i, ret;
683
684 if (!bo->pin_count) {
685 DRM_ERROR("unpin bad %p\n", bo);
686 return 0;
687 }
688 bo->pin_count--;
689 if (bo->pin_count)
690 return 0;
691
692 if (bo->kmap.virtual)
693 ttm_bo_kunmap(&bo->kmap);
694
695 vbox_ttm_placement(bo, VBOX_MEM_TYPE_SYSTEM);
696
697 for (i = 0; i < bo->placement.num_placement; i++)
698 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
699
700# 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)
701 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
702# else
703 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
704# endif
705 if (ret) {
706 DRM_ERROR("pushing to VRAM failed\n");
707 return ret;
708 }
709
710 return 0;
711}
712#endif
713
714int vbox_mmap(struct file *filp, struct vm_area_struct *vma)
715{
716 struct drm_file *file_priv;
717 struct vbox_private *vbox;
718
719 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
720 return -EINVAL;
721
722 file_priv = filp->private_data;
723 vbox = file_priv->minor->dev->dev_private;
724
725 return ttm_bo_mmap(filp, vma, &vbox->ttm.bdev);
726}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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