VirtualBox

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

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

bugref:8087: Additions/x11: support non-root X server: do some clean-up of the driver and the device initialisation and shut-down code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 KB
Line 
1/* $Id: vbox_ttm.c 59697 2016-02-16 10:32:31Z 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 vbox->ttm.mm_initialised = true;
319 return 0;
320}
321
322void vbox_mm_fini(struct vbox_private *vbox)
323{
324#ifdef DRM_MTRR_WC
325 struct drm_device *dev = vbox->dev;
326#endif
327 if (!vbox->ttm.mm_initialised)
328 return;
329 ttm_bo_device_release(&vbox->ttm.bdev);
330
331 vbox_ttm_global_release(vbox);
332
333#ifdef DRM_MTRR_WC
334 drm_mtrr_del(vbox->fb_mtrr,
335 pci_resource_start(dev->pdev, 0),
336 pci_resource_len(dev->pdev, 0), DRM_MTRR_WC);
337#else
338 arch_phys_wc_del(vbox->fb_mtrr);
339#endif
340}
341
342void vbox_ttm_placement(struct vbox_bo *bo, int domain)
343{
344 u32 c = 0;
345#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
346 bo->placement.fpfn = 0;
347 bo->placement.lpfn = 0;
348#else
349 unsigned i;
350#endif
351
352 bo->placement.placement = bo->placements;
353 bo->placement.busy_placement = bo->placements;
354 if (domain & TTM_PL_FLAG_VRAM)
355 PLACEMENT_FLAGS(bo->placements[c++]) = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
356 if (domain & TTM_PL_FLAG_SYSTEM)
357 PLACEMENT_FLAGS(bo->placements[c++]) = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
358 if (!c)
359 PLACEMENT_FLAGS(bo->placements[c++]) = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
360 bo->placement.num_placement = c;
361 bo->placement.num_busy_placement = c;
362#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
363 for (i = 0; i < c; ++i) {
364 bo->placements[i].fpfn = 0;
365 bo->placements[i].lpfn = 0;
366 }
367#endif
368}
369
370int vbox_bo_create(struct drm_device *dev, int size, int align,
371 uint32_t flags, struct vbox_bo **pvboxbo)
372{
373 struct vbox_private *vbox = dev->dev_private;
374 struct vbox_bo *vboxbo;
375 size_t acc_size;
376 int ret;
377
378 vboxbo = kzalloc(sizeof(struct vbox_bo), GFP_KERNEL);
379 if (!vboxbo)
380 return -ENOMEM;
381
382 ret = drm_gem_object_init(dev, &vboxbo->gem, size);
383 if (ret) {
384 kfree(vboxbo);
385 return ret;
386 }
387
388 vboxbo->bo.bdev = &vbox->ttm.bdev;
389#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0)
390 vboxbo->bo.bdev->dev_mapping = dev->dev_mapping;
391#endif
392
393 vbox_ttm_placement(vboxbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
394
395 acc_size = ttm_bo_dma_acc_size(&vbox->ttm.bdev, size,
396 sizeof(struct vbox_bo));
397
398 ret = ttm_bo_init(&vbox->ttm.bdev, &vboxbo->bo, size,
399 ttm_bo_type_device, &vboxbo->placement,
400 align >> PAGE_SHIFT, false, NULL, acc_size,
401#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
402 NULL,
403#endif
404 NULL, vbox_bo_ttm_destroy);
405 if (ret)
406 return ret;
407
408 *pvboxbo = vboxbo;
409 return 0;
410}
411
412static inline u64 vbox_bo_gpu_offset(struct vbox_bo *bo)
413{
414 return bo->bo.offset;
415}
416
417int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag, u64 *gpu_addr)
418{
419 int i, ret;
420
421 if (bo->pin_count) {
422 bo->pin_count++;
423 if (gpu_addr)
424 *gpu_addr = vbox_bo_gpu_offset(bo);
425 return 0;
426 }
427
428 vbox_ttm_placement(bo, pl_flag);
429 for (i = 0; i < bo->placement.num_placement; i++)
430 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
431 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
432 if (ret)
433 return ret;
434
435 bo->pin_count = 1;
436 if (gpu_addr)
437 *gpu_addr = vbox_bo_gpu_offset(bo);
438 return 0;
439}
440
441int vbox_bo_unpin(struct vbox_bo *bo)
442{
443 int i, ret;
444 if (!bo->pin_count) {
445 DRM_ERROR("unpin bad %p\n", bo);
446 return 0;
447 }
448 bo->pin_count--;
449 if (bo->pin_count)
450 return 0;
451
452 for (i = 0; i < bo->placement.num_placement ; i++)
453 PLACEMENT_FLAGS(bo->placements[i]) &= ~TTM_PL_FLAG_NO_EVICT;
454 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
455 if (ret)
456 return ret;
457
458 return 0;
459}
460
461/* Move a vbox-owned buffer object to system memory if no one else has it
462 * pinned. The caller must have pinned it previously, and this call will
463 * release the caller's pin. */
464int vbox_bo_push_sysram(struct vbox_bo *bo)
465{
466 int i, ret;
467 if (!bo->pin_count) {
468 DRM_ERROR("unpin bad %p\n", bo);
469 return 0;
470 }
471 bo->pin_count--;
472 if (bo->pin_count)
473 return 0;
474
475 if (bo->kmap.virtual)
476 ttm_bo_kunmap(&bo->kmap);
477
478 vbox_ttm_placement(bo, TTM_PL_FLAG_SYSTEM);
479 for (i = 0; i < bo->placement.num_placement ; i++)
480 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
481
482 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
483 if (ret) {
484 DRM_ERROR("pushing to VRAM failed\n");
485 return ret;
486 }
487 return 0;
488}
489
490int vbox_mmap(struct file *filp, struct vm_area_struct *vma)
491{
492 struct drm_file *file_priv;
493 struct vbox_private *vbox;
494
495 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
496 return -EINVAL;
497
498 file_priv = filp->private_data;
499 vbox = file_priv->minor->dev->dev_private;
500 return ttm_bo_mmap(filp, vma, &vbox->ttm.bdev);
501}
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