VirtualBox

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

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

linux/drm: Fixed incorrect file headers. (copy + past is difficult, apparently)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 KB
Line 
1/* $Id: vbox_ttm.c 58129 2015-10-08 22:29:48Z 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 {
91 DRM_ERROR("Failed setting up TTM memory accounting "
92 "subsystem.\n");
93 return r;
94 }
95
96 vbox->ttm.bo_global_ref.mem_glob =
97 vbox->ttm.mem_global_ref.object;
98 global_ref = &vbox->ttm.bo_global_ref.ref;
99 global_ref->global_type = DRM_GLOBAL_TTM_BO;
100 global_ref->size = sizeof(struct ttm_bo_global);
101 global_ref->init = &ttm_bo_global_init;
102 global_ref->release = &ttm_bo_global_release;
103 r = drm_global_item_ref(global_ref);
104 if (r != 0)
105 {
106 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
107 drm_global_item_unref(&vbox->ttm.mem_global_ref);
108 return r;
109 }
110 return 0;
111}
112
113/**
114 * Removes the vbox memory manager object from the global memory manager.
115 */
116void
117vbox_ttm_global_release(struct vbox_private *vbox)
118{
119 if (vbox->ttm.mem_global_ref.release == NULL)
120 return;
121
122 drm_global_item_unref(&vbox->ttm.bo_global_ref.ref);
123 drm_global_item_unref(&vbox->ttm.mem_global_ref);
124 vbox->ttm.mem_global_ref.release = NULL;
125}
126
127
128static void vbox_bo_ttm_destroy(struct ttm_buffer_object *tbo)
129{
130 struct vbox_bo *bo;
131
132 bo = container_of(tbo, struct vbox_bo, bo);
133
134 drm_gem_object_release(&bo->gem);
135 kfree(bo);
136}
137
138bool vbox_ttm_bo_is_vbox_bo(struct ttm_buffer_object *bo)
139{
140 if (bo->destroy == &vbox_bo_ttm_destroy)
141 return true;
142 return false;
143}
144
145static int
146vbox_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
147 struct ttm_mem_type_manager *man)
148{
149 switch (type)
150 {
151 case TTM_PL_SYSTEM:
152 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
153 man->available_caching = TTM_PL_MASK_CACHING;
154 man->default_caching = TTM_PL_FLAG_CACHED;
155 break;
156 case TTM_PL_VRAM:
157 man->func = &ttm_bo_manager_func;
158 man->flags = TTM_MEMTYPE_FLAG_FIXED |
159 TTM_MEMTYPE_FLAG_MAPPABLE;
160 man->available_caching = TTM_PL_FLAG_UNCACHED |
161 TTM_PL_FLAG_WC;
162 man->default_caching = TTM_PL_FLAG_WC;
163 break;
164 default:
165 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
166 return -EINVAL;
167 }
168 return 0;
169}
170
171static void
172vbox_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
173{
174 struct vbox_bo *vboxbo = vbox_bo(bo);
175
176 if (!vbox_ttm_bo_is_vbox_bo(bo))
177 return;
178
179 vbox_ttm_placement(vboxbo, TTM_PL_FLAG_SYSTEM);
180 *pl = vboxbo->placement;
181}
182
183static int vbox_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
184{
185 return 0;
186}
187
188static int vbox_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
189 struct ttm_mem_reg *mem)
190{
191 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
192 struct vbox_private *vbox = vbox_bdev(bdev);
193
194 mem->bus.addr = NULL;
195 mem->bus.offset = 0;
196 mem->bus.size = mem->num_pages << PAGE_SHIFT;
197 mem->bus.base = 0;
198 mem->bus.is_iomem = false;
199 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
200 return -EINVAL;
201 switch (mem->mem_type)
202 {
203 case TTM_PL_SYSTEM:
204 /* system memory */
205 return 0;
206 case TTM_PL_VRAM:
207 mem->bus.offset = mem->start << PAGE_SHIFT;
208 mem->bus.base = pci_resource_start(vbox->dev->pdev, 0);
209 mem->bus.is_iomem = true;
210 break;
211 default:
212 return -EINVAL;
213 break;
214 }
215 return 0;
216}
217
218static void vbox_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
219{
220}
221
222static int vbox_bo_move(struct ttm_buffer_object *bo,
223 bool evict, bool interruptible,
224 bool no_wait_gpu,
225 struct ttm_mem_reg *new_mem)
226{
227 int r;
228 r = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
229 return r;
230}
231
232
233static void vbox_ttm_backend_destroy(struct ttm_tt *tt)
234{
235 ttm_tt_fini(tt);
236 kfree(tt);
237}
238
239static struct ttm_backend_func vbox_tt_backend_func =
240{
241 .destroy = &vbox_ttm_backend_destroy,
242};
243
244
245struct ttm_tt *vbox_ttm_tt_create(struct ttm_bo_device *bdev,
246 unsigned long size, uint32_t page_flags,
247 struct page *dummy_read_page)
248{
249 struct ttm_tt *tt;
250
251 tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
252 if (tt == NULL)
253 return NULL;
254 tt->func = &vbox_tt_backend_func;
255 if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page))
256 {
257 kfree(tt);
258 return NULL;
259 }
260 return tt;
261}
262
263static int vbox_ttm_tt_populate(struct ttm_tt *ttm)
264{
265 return ttm_pool_populate(ttm);
266}
267
268static void vbox_ttm_tt_unpopulate(struct ttm_tt *ttm)
269{
270 ttm_pool_unpopulate(ttm);
271}
272
273struct ttm_bo_driver vbox_bo_driver =
274{
275 .ttm_tt_create = vbox_ttm_tt_create,
276 .ttm_tt_populate = vbox_ttm_tt_populate,
277 .ttm_tt_unpopulate = vbox_ttm_tt_unpopulate,
278 .init_mem_type = vbox_bo_init_mem_type,
279 .evict_flags = vbox_bo_evict_flags,
280 .move = vbox_bo_move,
281 .verify_access = vbox_bo_verify_access,
282 .io_mem_reserve = &vbox_ttm_io_mem_reserve,
283 .io_mem_free = &vbox_ttm_io_mem_free,
284};
285
286int vbox_mm_init(struct vbox_private *vbox)
287{
288 int ret;
289 struct drm_device *dev = vbox->dev;
290 struct ttm_bo_device *bdev = &vbox->ttm.bdev;
291
292 ret = vbox_ttm_global_init(vbox);
293 if (ret)
294 return ret;
295
296 ret = ttm_bo_device_init(&vbox->ttm.bdev,
297 vbox->ttm.bo_global_ref.ref.object,
298 &vbox_bo_driver,
299#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)
300 dev->anon_inode->i_mapping,
301#endif
302 DRM_FILE_PAGE_OFFSET,
303 true);
304 if (ret)
305 {
306 DRM_ERROR("Error initialising bo driver; %d\n", ret);
307 return ret;
308 }
309
310 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
311 vbox->vram_size >> PAGE_SHIFT);
312 if (ret)
313 {
314 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
315 return ret;
316 }
317
318#ifdef DRM_MTRR_WC
319 vbox->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
320 pci_resource_len(dev->pdev, 0),
321 DRM_MTRR_WC);
322#else
323 vbox->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
324 pci_resource_len(dev->pdev, 0));
325#endif
326
327 return 0;
328}
329
330void vbox_mm_fini(struct vbox_private *vbox)
331{
332 struct drm_device *dev = vbox->dev;
333 ttm_bo_device_release(&vbox->ttm.bdev);
334
335 vbox_ttm_global_release(vbox);
336
337 if (vbox->fb_mtrr >= 0)
338 {
339#ifdef DRM_MTRR_WC
340 drm_mtrr_del(vbox->fb_mtrr,
341 pci_resource_start(dev->pdev, 0),
342 pci_resource_len(dev->pdev, 0), DRM_MTRR_WC);
343#else
344 arch_phys_wc_del(vbox->fb_mtrr);
345#endif
346 vbox->fb_mtrr = -1;
347 }
348}
349
350void vbox_ttm_placement(struct vbox_bo *bo, int domain)
351{
352 u32 c = 0;
353#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
354 bo->placement.fpfn = 0;
355 bo->placement.lpfn = 0;
356#else
357 unsigned i;
358#endif
359
360 bo->placement.placement = bo->placements;
361 bo->placement.busy_placement = bo->placements;
362 if (domain & TTM_PL_FLAG_VRAM)
363 PLACEMENT_FLAGS(bo->placements[c++]) = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
364 if (domain & TTM_PL_FLAG_SYSTEM)
365 PLACEMENT_FLAGS(bo->placements[c++]) = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
366 if (!c)
367 PLACEMENT_FLAGS(bo->placements[c++]) = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
368 bo->placement.num_placement = c;
369 bo->placement.num_busy_placement = c;
370#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
371 for (i = 0; i < c; ++i)
372 {
373 bo->placements[i].fpfn = 0;
374 bo->placements[i].lpfn = 0;
375 }
376#endif
377}
378
379int vbox_bo_reserve(struct vbox_bo *bo, bool no_wait)
380{
381 int ret;
382
383 ret = ttm_bo_reserve(&bo->bo, true, no_wait, false, 0);
384 if (ret)
385 {
386 if (ret != -ERESTARTSYS && ret != -EBUSY)
387 DRM_ERROR("reserve failed %p\n", bo);
388 return ret;
389 }
390 return 0;
391}
392
393void vbox_bo_unreserve(struct vbox_bo *bo)
394{
395 ttm_bo_unreserve(&bo->bo);
396}
397
398int vbox_bo_create(struct drm_device *dev, int size, int align,
399 uint32_t flags, struct vbox_bo **pvboxbo)
400{
401 struct vbox_private *vbox = dev->dev_private;
402 struct vbox_bo *vboxbo;
403 size_t acc_size;
404 int ret;
405
406 vboxbo = kzalloc(sizeof(struct vbox_bo), GFP_KERNEL);
407 if (!vboxbo)
408 return -ENOMEM;
409
410 ret = drm_gem_object_init(dev, &vboxbo->gem, size);
411 if (ret)
412 {
413 kfree(vboxbo);
414 return ret;
415 }
416
417 vboxbo->bo.bdev = &vbox->ttm.bdev;
418#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0)
419 vboxbo->bo.bdev->dev_mapping = dev->dev_mapping;
420#endif
421
422 vbox_ttm_placement(vboxbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
423
424 acc_size = ttm_bo_dma_acc_size(&vbox->ttm.bdev, size,
425 sizeof(struct vbox_bo));
426
427 ret = ttm_bo_init(&vbox->ttm.bdev, &vboxbo->bo, size,
428 ttm_bo_type_device, &vboxbo->placement,
429 align >> PAGE_SHIFT, false, NULL, acc_size,
430#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
431 NULL,
432#endif
433 NULL, vbox_bo_ttm_destroy);
434 if (ret)
435 return ret;
436
437 *pvboxbo = vboxbo;
438 return 0;
439}
440
441static inline u64 vbox_bo_gpu_offset(struct vbox_bo *bo)
442{
443 return bo->bo.offset;
444}
445
446int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag, u64 *gpu_addr)
447{
448 int i, ret;
449
450 if (bo->pin_count)
451 {
452 bo->pin_count++;
453 if (gpu_addr)
454 *gpu_addr = vbox_bo_gpu_offset(bo);
455 return 0;
456 }
457
458 vbox_ttm_placement(bo, pl_flag);
459 for (i = 0; i < bo->placement.num_placement; i++)
460 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
461 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
462 if (ret)
463 return ret;
464
465 bo->pin_count = 1;
466 if (gpu_addr)
467 *gpu_addr = vbox_bo_gpu_offset(bo);
468 return 0;
469}
470
471int vbox_bo_unpin(struct vbox_bo *bo)
472{
473 int i, ret;
474 if (!bo->pin_count)
475 {
476 DRM_ERROR("unpin bad %p\n", bo);
477 return 0;
478 }
479 bo->pin_count--;
480 if (bo->pin_count)
481 return 0;
482
483 for (i = 0; i < bo->placement.num_placement ; i++)
484 PLACEMENT_FLAGS(bo->placements[i]) &= ~TTM_PL_FLAG_NO_EVICT;
485 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
486 if (ret)
487 return ret;
488
489 return 0;
490}
491
492/* Move a vbox-owned buffer object to system memory if no one else has it
493 * pinned. The caller must have pinned it previously, and this call will
494 * release the caller's pin. */
495int vbox_bo_push_sysram(struct vbox_bo *bo)
496{
497 int i, ret;
498 if (!bo->pin_count)
499 {
500 DRM_ERROR("unpin bad %p\n", bo);
501 return 0;
502 }
503 bo->pin_count--;
504 if (bo->pin_count)
505 return 0;
506
507 if (bo->kmap.virtual)
508 ttm_bo_kunmap(&bo->kmap);
509
510 vbox_ttm_placement(bo, TTM_PL_FLAG_SYSTEM);
511 for (i = 0; i < bo->placement.num_placement ; i++)
512 PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
513
514 ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
515 if (ret)
516 {
517 DRM_ERROR("pushing to VRAM failed\n");
518 return ret;
519 }
520 return 0;
521}
522
523int vbox_mmap(struct file *filp, struct vm_area_struct *vma)
524{
525 struct drm_file *file_priv;
526 struct vbox_private *vbox;
527
528 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
529 return -EINVAL;
530
531 file_priv = filp->private_data;
532 vbox = file_priv->minor->dev->dev_private;
533 return ttm_bo_mmap(filp, vma, &vbox->ttm.bdev);
534}
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