VirtualBox

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

Last change on this file since 55628 was 53793, checked in by vboxsync, 10 years ago

Additions/linux/drm: forgotten to export some files.

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