VirtualBox

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

Last change on this file since 78314 was 77850, checked in by vboxsync, 6 years ago

Additions/linux/vboxvideo: make driver work with Linux 5.0 and 5.1.
bugref:4567: Linux kernel driver maintenance.
This change mainly consists of adaptations of the parts of the following
kernel changes which affect staging/vboxvideo:

commit c58ac649e3553c23a9bafba8c2e5d040aee87484
Author: Cihangir Akturk <cakturk@…>
Date: Fri Aug 11 15:33:14 2017 +0300

drm: vboxvideo: switch to drm_*_get(), drm_*_put() helpers

Author: Christian König <christian.koenig@…>
Date: Fri Oct 19 13:49:05 2018 +0200

drm/ttm: use a static ttm_mem_global instance

commit a64f784bb14a56bfdfad2dc397dd67e4564e3a29
Author: Christian König <christian.koenig@…>
Date: Fri Oct 19 16:55:26 2018 +0200

drm/ttm: initialize globals during device init (v2)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 KB
Line 
1/* $Id: vbox_ttm.c 77850 2019-03-22 14:49:18Z 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)
214static struct ttm_tt *vbox_ttm_tt_create(struct ttm_bo_device *bdev,
215 unsigned long size,
216 u32 page_flags,
217 struct page *dummy_read_page)
218#else
219static struct ttm_tt *vbox_ttm_tt_create(struct ttm_buffer_object *bo,
220 u32 page_flags)
221#endif
222{
223 struct ttm_tt *tt;
224
225 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
226 if (!tt)
227 return NULL;
228
229 tt->func = &vbox_tt_backend_func;
230#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)) && !defined(RHEL_76)
231 if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) {
232#else
233 if (ttm_tt_init(tt, bo, page_flags)) {
234#endif
235 kfree(tt);
236 return NULL;
237 }
238
239 return tt;
240}
241
242#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
243# if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)) && !defined(RHEL_76)
244static int vbox_ttm_tt_populate(struct ttm_tt *ttm)
245{
246 return ttm_pool_populate(ttm);
247}
248# else
249static int vbox_ttm_tt_populate(struct ttm_tt *ttm,
250 struct ttm_operation_ctx *ctx)
251{
252 return ttm_pool_populate(ttm, ctx);
253}
254# endif
255
256static void vbox_ttm_tt_unpopulate(struct ttm_tt *ttm)
257{
258 ttm_pool_unpopulate(ttm);
259}
260#endif
261
262static struct ttm_bo_driver vbox_bo_driver = {
263 .ttm_tt_create = vbox_ttm_tt_create,
264#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
265 .ttm_tt_populate = vbox_ttm_tt_populate,
266 .ttm_tt_unpopulate = vbox_ttm_tt_unpopulate,
267#endif
268 .init_mem_type = vbox_bo_init_mem_type,
269#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) || defined(RHEL_74)
270 .eviction_valuable = ttm_bo_eviction_valuable,
271#endif
272 .evict_flags = vbox_bo_evict_flags,
273 .verify_access = vbox_bo_verify_access,
274 .io_mem_reserve = &vbox_ttm_io_mem_reserve,
275 .io_mem_free = &vbox_ttm_io_mem_free,
276#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) || defined(RHEL_75)
277# if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)) && !defined(RHEL_76)
278 .io_mem_pfn = ttm_bo_default_io_mem_pfn,
279# endif
280#endif
281#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)) \
282 || defined(RHEL_74)
283# ifndef RHEL_75
284 .lru_tail = &ttm_bo_default_lru_tail,
285 .swap_lru_tail = &ttm_bo_default_swap_lru_tail,
286# endif
287#endif
288};
289
290int vbox_mm_init(struct vbox_private *vbox)
291{
292 int ret;
293 struct drm_device *dev = vbox->dev;
294 struct ttm_bo_device *bdev = &vbox->ttm.bdev;
295
296#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
297 ret = vbox_ttm_global_init(vbox);
298 if (ret)
299 return ret;
300#endif
301
302 ret = ttm_bo_device_init(&vbox->ttm.bdev,
303#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
304 vbox->ttm.bo_global_ref.ref.object,
305#endif
306 &vbox_bo_driver,
307#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0) || defined(RHEL_71)
308 dev->anon_inode->i_mapping,
309#endif
310 DRM_FILE_PAGE_OFFSET, true);
311 if (ret) {
312 DRM_ERROR("Error initialising bo driver; %d\n", ret);
313#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
314 goto err_ttm_global_release;
315#else
316 return ret;
317#endif
318 }
319
320 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
321 vbox->available_vram_size >> PAGE_SHIFT);
322 if (ret) {
323 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
324 goto err_device_release;
325 }
326
327#ifdef DRM_MTRR_WC
328 vbox->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
329 pci_resource_len(dev->pdev, 0),
330 DRM_MTRR_WC);
331#else
332 vbox->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
333 pci_resource_len(dev->pdev, 0));
334#endif
335 return 0;
336
337err_device_release:
338 ttm_bo_device_release(&vbox->ttm.bdev);
339#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
340err_ttm_global_release:
341 vbox_ttm_global_release(vbox);
342#endif
343 return ret;
344}
345
346void vbox_mm_fini(struct vbox_private *vbox)
347{
348#ifdef DRM_MTRR_WC
349 drm_mtrr_del(vbox->fb_mtrr,
350 pci_resource_start(vbox->dev->pdev, 0),
351 pci_resource_len(vbox->dev->pdev, 0), DRM_MTRR_WC);
352#else
353 arch_phys_wc_del(vbox->fb_mtrr);
354#endif
355 ttm_bo_device_release(&vbox->ttm.bdev);
356#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
357 vbox_ttm_global_release(vbox);
358#endif
359}
360
361void vbox_ttm_placement(struct vbox_bo *bo, int domain)
362{
363 u32 c = 0;
364#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0) && !defined(RHEL_72)
365 bo->placement.fpfn = 0;
366 bo->placement.lpfn = 0;
367#else
368 unsigned int i;
369#endif
370
371 bo->placement.placement = bo->placements;
372 bo->placement.busy_placement = bo->placements;
373
374 if (domain & TTM_PL_FLAG_VRAM)
375 PLACEMENT_FLAGS(bo->placements[c++]) =
376 TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
377 if (domain & TTM_PL_FLAG_SYSTEM)
378 PLACEMENT_FLAGS(bo->placements[c++]) =
379 TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
380 if (!c)
381 PLACEMENT_FLAGS(bo->placements[c++]) =
382 TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
383
384 bo->placement.num_placement = c;
385 bo->placement.num_busy_placement = c;
386
387#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) || defined(RHEL_72)
388 for (i = 0; i < c; ++i) {
389 bo->placements[i].fpfn = 0;
390 bo->placements[i].lpfn = 0;
391 }
392#endif
393}
394
395int vbox_bo_create(struct drm_device *dev, int size, int align,
396 u32 flags, struct vbox_bo **pvboxbo)
397{
398 struct vbox_private *vbox = dev->dev_private;
399 struct vbox_bo *vboxbo;
400 size_t acc_size;
401 int ret;
402
403 vboxbo = kzalloc(sizeof(*vboxbo), GFP_KERNEL);
404 if (!vboxbo)
405 return -ENOMEM;
406
407 ret = drm_gem_object_init(dev, &vboxbo->gem, size);
408 if (ret)
409 goto err_free_vboxbo;
410
411 vboxbo->bo.bdev = &vbox->ttm.bdev;
412#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0) && !defined(RHEL_71)
413 vboxbo->bo.bdev->dev_mapping = dev->dev_mapping;
414#endif
415
416 vbox_ttm_placement(vboxbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
417
418 acc_size = ttm_bo_dma_acc_size(&vbox->ttm.bdev, size,
419 sizeof(struct vbox_bo));
420
421 ret = ttm_bo_init(&vbox->ttm.bdev, &vboxbo->bo, size,
422 ttm_bo_type_device, &vboxbo->placement,
423#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) && !defined(RHEL_76)
424 align >> PAGE_SHIFT, false, NULL, acc_size,
425#else
426 align >> PAGE_SHIFT, false, acc_size,
427#endif
428#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) || defined(RHEL_72)
429 NULL, NULL, vbox_bo_ttm_destroy);
430#else
431 NULL, vbox_bo_ttm_destroy);
432#endif
433 if (ret)
434 goto err_free_vboxbo;
435
436 *pvboxbo = vboxbo;
437
438 return 0;
439
440err_free_vboxbo:
441 kfree(vboxbo);
442 return ret;
443}
444
445static inline u64 vbox_bo_gpu_offset(struct vbox_bo *bo)
446{
447 return bo->bo.offset;
448}
449
450int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag, u64 *gpu_addr)
451{
452#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)) || defined(RHEL_76)
453 struct ttm_operation_ctx ctx = { false, false };
454#endif
455 int i, ret;
456
457 if (bo->pin_count) {
458 bo->pin_count++;
459 if (gpu_addr)
460 *gpu_addr = vbox_bo_gpu_offset(bo);
461
462 return 0;
463 }
464
465 vbox_ttm_placement(bo, pl_flag);
466
467 for (i = 0; i < bo->placement.num_placement; i++)
468 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
469
470#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)) && !defined(RHEL_76)
471 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
472#else
473 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
474#endif
475 if (ret)
476 return ret;
477
478 bo->pin_count = 1;
479
480 if (gpu_addr)
481 *gpu_addr = vbox_bo_gpu_offset(bo);
482
483 return 0;
484}
485
486int vbox_bo_unpin(struct vbox_bo *bo)
487{
488#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)) || defined(RHEL_76)
489 struct ttm_operation_ctx ctx = { false, false };
490#endif
491 int i, ret;
492
493 if (!bo->pin_count) {
494 DRM_ERROR("unpin bad %p\n", bo);
495 return 0;
496 }
497 bo->pin_count--;
498 if (bo->pin_count)
499 return 0;
500
501 for (i = 0; i < bo->placement.num_placement; i++)
502 PLACEMENT_FLAGS(bo->placements[i]) &= ~TTM_PL_FLAG_NO_EVICT;
503
504#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)) && !defined(RHEL_76)
505 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
506#else
507 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
508#endif
509 if (ret)
510 return ret;
511
512 return 0;
513}
514
515/*
516 * Move a vbox-owned buffer object to system memory if no one else has it
517 * pinned. The caller must have pinned it previously, and this call will
518 * release the caller's pin.
519 */
520int vbox_bo_push_sysram(struct vbox_bo *bo)
521{
522#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)) || defined(RHEL_76)
523 struct ttm_operation_ctx ctx = { false, false };
524#endif
525 int i, ret;
526
527 if (!bo->pin_count) {
528 DRM_ERROR("unpin bad %p\n", bo);
529 return 0;
530 }
531 bo->pin_count--;
532 if (bo->pin_count)
533 return 0;
534
535 if (bo->kmap.virtual)
536 ttm_bo_kunmap(&bo->kmap);
537
538 vbox_ttm_placement(bo, TTM_PL_FLAG_SYSTEM);
539
540 for (i = 0; i < bo->placement.num_placement; i++)
541 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
542
543#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)) && !defined(RHEL_76)
544 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
545#else
546 ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
547#endif
548 if (ret) {
549 DRM_ERROR("pushing to VRAM failed\n");
550 return ret;
551 }
552
553 return 0;
554}
555
556int vbox_mmap(struct file *filp, struct vm_area_struct *vma)
557{
558 struct drm_file *file_priv;
559 struct vbox_private *vbox;
560
561 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
562 return -EINVAL;
563
564 file_priv = filp->private_data;
565 vbox = file_priv->minor->dev->dev_private;
566
567 return ttm_bo_mmap(filp, vma, &vbox->ttm.bdev);
568}
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