VirtualBox

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

Last change on this file since 59526 was 59526, checked in by vboxsync, 9 years ago

bugref:8087: Additions/x11: support non-root X server: adjust DRM/KMS driver code to bring it back closer to the original AST driver on which it is based, including enabling ejecting unused frame-buffers from VRAM to save memory.

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

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