1 | /* $Id: vbox_ttm.c 85707 2020-08-11 19:43:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Additions Linux kernel video driver
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2020 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 <drm/ttm/ttm_page_alloc.h>
|
---|
37 |
|
---|
38 | #if RTLNX_VER_MAX(3,18,0) && !RTLNX_RHEL_MAJ_PREREQ(7,2)
|
---|
39 | #define PLACEMENT_FLAGS(placement) (placement)
|
---|
40 | #else
|
---|
41 | #define PLACEMENT_FLAGS(placement) ((placement).flags)
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | static 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 RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
|
---|
50 | static int vbox_ttm_mem_global_init(struct drm_global_reference *ref)
|
---|
51 | {
|
---|
52 | return ttm_mem_global_init(ref->object);
|
---|
53 | }
|
---|
54 |
|
---|
55 | static 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 | */
|
---|
63 | static int vbox_ttm_global_init(struct vbox_private *vbox)
|
---|
64 | {
|
---|
65 | struct drm_global_reference *global_ref;
|
---|
66 | int ret;
|
---|
67 |
|
---|
68 | #if RTLNX_VER_MAX(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 RTLNX_VER_MAX(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 | */
|
---|
103 | static 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 |
|
---|
110 | static 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 |
|
---|
120 | static 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 |
|
---|
128 | static int
|
---|
129 | vbox_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 |
|
---|
152 | static void
|
---|
153 | vbox_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 |
|
---|
164 | static int vbox_bo_verify_access(struct ttm_buffer_object *bo,
|
---|
165 | struct file *filp)
|
---|
166 | {
|
---|
167 | return 0;
|
---|
168 | }
|
---|
169 |
|
---|
170 | static 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 |
|
---|
198 | static void vbox_ttm_io_mem_free(struct ttm_bo_device *bdev,
|
---|
199 | struct ttm_mem_reg *mem)
|
---|
200 | {
|
---|
201 | }
|
---|
202 |
|
---|
203 | static void vbox_ttm_backend_destroy(struct ttm_tt *tt)
|
---|
204 | {
|
---|
205 | ttm_tt_fini(tt);
|
---|
206 | kfree(tt);
|
---|
207 | }
|
---|
208 |
|
---|
209 | static struct ttm_backend_func vbox_tt_backend_func = {
|
---|
210 | .destroy = &vbox_ttm_backend_destroy,
|
---|
211 | };
|
---|
212 |
|
---|
213 | #if RTLNX_VER_MAX(4,17,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
214 | static 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
|
---|
219 | static 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 RTLNX_VER_MAX(4,17,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
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 RTLNX_VER_MAX(4,17,0)
|
---|
243 | # if RTLNX_VER_MAX(4,16,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
244 | static int vbox_ttm_tt_populate(struct ttm_tt *ttm)
|
---|
245 | {
|
---|
246 | return ttm_pool_populate(ttm);
|
---|
247 | }
|
---|
248 | # else
|
---|
249 | static 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 |
|
---|
256 | static void vbox_ttm_tt_unpopulate(struct ttm_tt *ttm)
|
---|
257 | {
|
---|
258 | ttm_pool_unpopulate(ttm);
|
---|
259 | }
|
---|
260 | #endif
|
---|
261 |
|
---|
262 | static struct ttm_bo_driver vbox_bo_driver = {
|
---|
263 | .ttm_tt_create = vbox_ttm_tt_create,
|
---|
264 | #if RTLNX_VER_MAX(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 RTLNX_VER_MIN(4,10,0) || RTLNX_RHEL_MAJ_PREREQ(7,4)
|
---|
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 RTLNX_VER_MIN(4,12,0) || RTLNX_RHEL_MAJ_PREREQ(7,5)
|
---|
277 | # if RTLNX_VER_MAX(4,16,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
278 | .io_mem_pfn = ttm_bo_default_io_mem_pfn,
|
---|
279 | # endif
|
---|
280 | #endif
|
---|
281 | #if (RTLNX_VER_RANGE(4,7,0, 4,11,0) || RTLNX_RHEL_MAJ_PREREQ(7,4)) && !RTLNX_RHEL_MAJ_PREREQ(7,5)
|
---|
282 | .lru_tail = &ttm_bo_default_lru_tail,
|
---|
283 | .swap_lru_tail = &ttm_bo_default_swap_lru_tail,
|
---|
284 | #endif
|
---|
285 | };
|
---|
286 |
|
---|
287 | int vbox_mm_init(struct vbox_private *vbox)
|
---|
288 | {
|
---|
289 | int ret;
|
---|
290 | struct drm_device *dev = vbox->dev;
|
---|
291 | struct ttm_bo_device *bdev = &vbox->ttm.bdev;
|
---|
292 |
|
---|
293 | #if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
|
---|
294 | ret = vbox_ttm_global_init(vbox);
|
---|
295 | if (ret)
|
---|
296 | return ret;
|
---|
297 | #endif
|
---|
298 | ret = ttm_bo_device_init(&vbox->ttm.bdev,
|
---|
299 | #if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
|
---|
300 | vbox->ttm.bo_global_ref.ref.object,
|
---|
301 | #endif
|
---|
302 | &vbox_bo_driver,
|
---|
303 | #if RTLNX_VER_MIN(3,15,0) || RTLNX_RHEL_MAJ_PREREQ(7,1)
|
---|
304 | dev->anon_inode->i_mapping,
|
---|
305 | #endif
|
---|
306 | #if RTLNX_VER_MIN(5,5,0)
|
---|
307 | dev->vma_offset_manager,
|
---|
308 | #elif RTLNX_VER_MAX(5,2,0) && !RTLNX_RHEL_MAJ_PREREQ(8,2)
|
---|
309 | DRM_FILE_PAGE_OFFSET,
|
---|
310 | #endif
|
---|
311 | true);
|
---|
312 | if (ret) {
|
---|
313 | DRM_ERROR("Error initialising bo driver; %d\n", ret);
|
---|
314 | #if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
|
---|
315 | goto err_ttm_global_release;
|
---|
316 | #else
|
---|
317 | return ret;
|
---|
318 | #endif
|
---|
319 | }
|
---|
320 |
|
---|
321 | ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
|
---|
322 | vbox->available_vram_size >> PAGE_SHIFT);
|
---|
323 | if (ret) {
|
---|
324 | DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
|
---|
325 | goto err_device_release;
|
---|
326 | }
|
---|
327 |
|
---|
328 | #ifdef DRM_MTRR_WC
|
---|
329 | vbox->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
|
---|
330 | pci_resource_len(dev->pdev, 0),
|
---|
331 | DRM_MTRR_WC);
|
---|
332 | #else
|
---|
333 | vbox->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
|
---|
334 | pci_resource_len(dev->pdev, 0));
|
---|
335 | #endif
|
---|
336 | return 0;
|
---|
337 |
|
---|
338 | err_device_release:
|
---|
339 | ttm_bo_device_release(&vbox->ttm.bdev);
|
---|
340 | #if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
|
---|
341 | err_ttm_global_release:
|
---|
342 | vbox_ttm_global_release(vbox);
|
---|
343 | #endif
|
---|
344 | return ret;
|
---|
345 | }
|
---|
346 |
|
---|
347 | void vbox_mm_fini(struct vbox_private *vbox)
|
---|
348 | {
|
---|
349 | #ifdef DRM_MTRR_WC
|
---|
350 | drm_mtrr_del(vbox->fb_mtrr,
|
---|
351 | pci_resource_start(vbox->dev->pdev, 0),
|
---|
352 | pci_resource_len(vbox->dev->pdev, 0), DRM_MTRR_WC);
|
---|
353 | #else
|
---|
354 | arch_phys_wc_del(vbox->fb_mtrr);
|
---|
355 | #endif
|
---|
356 | ttm_bo_device_release(&vbox->ttm.bdev);
|
---|
357 | #if RTLNX_VER_MAX(5,0,0) && !RTLNX_RHEL_MAJ_PREREQ(7,7) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
|
---|
358 | vbox_ttm_global_release(vbox);
|
---|
359 | #endif
|
---|
360 | }
|
---|
361 |
|
---|
362 | void vbox_ttm_placement(struct vbox_bo *bo, int domain)
|
---|
363 | {
|
---|
364 | u32 c = 0;
|
---|
365 | #if RTLNX_VER_MAX(3,18,0) && !RTLNX_RHEL_MAJ_PREREQ(7,2)
|
---|
366 | bo->placement.fpfn = 0;
|
---|
367 | bo->placement.lpfn = 0;
|
---|
368 | #else
|
---|
369 | unsigned int i;
|
---|
370 | #endif
|
---|
371 |
|
---|
372 | bo->placement.placement = bo->placements;
|
---|
373 | bo->placement.busy_placement = bo->placements;
|
---|
374 |
|
---|
375 | if (domain & TTM_PL_FLAG_VRAM)
|
---|
376 | PLACEMENT_FLAGS(bo->placements[c++]) =
|
---|
377 | TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
|
---|
378 | if (domain & TTM_PL_FLAG_SYSTEM)
|
---|
379 | PLACEMENT_FLAGS(bo->placements[c++]) =
|
---|
380 | TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
|
---|
381 | if (!c)
|
---|
382 | PLACEMENT_FLAGS(bo->placements[c++]) =
|
---|
383 | TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
|
---|
384 |
|
---|
385 | bo->placement.num_placement = c;
|
---|
386 | bo->placement.num_busy_placement = c;
|
---|
387 |
|
---|
388 | #if RTLNX_VER_MIN(3,18,0) || RTLNX_RHEL_MAJ_PREREQ(7,2)
|
---|
389 | for (i = 0; i < c; ++i) {
|
---|
390 | bo->placements[i].fpfn = 0;
|
---|
391 | bo->placements[i].lpfn = 0;
|
---|
392 | }
|
---|
393 | #endif
|
---|
394 | }
|
---|
395 |
|
---|
396 | int vbox_bo_create(struct drm_device *dev, int size, int align,
|
---|
397 | u32 flags, struct vbox_bo **pvboxbo)
|
---|
398 | {
|
---|
399 | struct vbox_private *vbox = dev->dev_private;
|
---|
400 | struct vbox_bo *vboxbo;
|
---|
401 | size_t acc_size;
|
---|
402 | int ret;
|
---|
403 |
|
---|
404 | vboxbo = kzalloc(sizeof(*vboxbo), GFP_KERNEL);
|
---|
405 | if (!vboxbo)
|
---|
406 | return -ENOMEM;
|
---|
407 |
|
---|
408 | ret = drm_gem_object_init(dev, &vboxbo->gem, size);
|
---|
409 | if (ret)
|
---|
410 | goto err_free_vboxbo;
|
---|
411 |
|
---|
412 | vboxbo->bo.bdev = &vbox->ttm.bdev;
|
---|
413 | #if RTLNX_VER_MAX(3,15,0) && !RTLNX_RHEL_MAJ_PREREQ(7,1)
|
---|
414 | vboxbo->bo.bdev->dev_mapping = dev->dev_mapping;
|
---|
415 | #endif
|
---|
416 |
|
---|
417 | vbox_ttm_placement(vboxbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
|
---|
418 |
|
---|
419 | acc_size = ttm_bo_dma_acc_size(&vbox->ttm.bdev, size,
|
---|
420 | sizeof(struct vbox_bo));
|
---|
421 |
|
---|
422 | ret = ttm_bo_init(&vbox->ttm.bdev, &vboxbo->bo, size,
|
---|
423 | ttm_bo_type_device, &vboxbo->placement,
|
---|
424 | #if RTLNX_VER_MAX(4,17,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
425 | align >> PAGE_SHIFT, false, NULL, acc_size,
|
---|
426 | #else
|
---|
427 | align >> PAGE_SHIFT, false, acc_size,
|
---|
428 | #endif
|
---|
429 | #if RTLNX_VER_MIN(3,18,0) || RTLNX_RHEL_MAJ_PREREQ(7,2)
|
---|
430 | NULL, NULL, vbox_bo_ttm_destroy);
|
---|
431 | #else
|
---|
432 | NULL, vbox_bo_ttm_destroy);
|
---|
433 | #endif
|
---|
434 | if (ret)
|
---|
435 | goto err_free_vboxbo;
|
---|
436 |
|
---|
437 | *pvboxbo = vboxbo;
|
---|
438 |
|
---|
439 | return 0;
|
---|
440 |
|
---|
441 | err_free_vboxbo:
|
---|
442 | kfree(vboxbo);
|
---|
443 | return ret;
|
---|
444 | }
|
---|
445 |
|
---|
446 | static inline u64 vbox_bo_gpu_offset(struct vbox_bo *bo)
|
---|
447 | {
|
---|
448 | return bo->bo.offset;
|
---|
449 | }
|
---|
450 |
|
---|
451 | int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag, u64 *gpu_addr)
|
---|
452 | {
|
---|
453 | #if RTLNX_VER_MIN(4,16,0) || RTLNX_RHEL_MAJ_PREREQ(7,6) || RTLNX_SUSE_MAJ_PREREQ(15,1) || RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
454 | struct ttm_operation_ctx ctx = { false, false };
|
---|
455 | #endif
|
---|
456 | int i, ret;
|
---|
457 |
|
---|
458 | if (bo->pin_count) {
|
---|
459 | bo->pin_count++;
|
---|
460 | if (gpu_addr)
|
---|
461 | *gpu_addr = vbox_bo_gpu_offset(bo);
|
---|
462 |
|
---|
463 | return 0;
|
---|
464 | }
|
---|
465 |
|
---|
466 | vbox_ttm_placement(bo, pl_flag);
|
---|
467 |
|
---|
468 | for (i = 0; i < bo->placement.num_placement; i++)
|
---|
469 | PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
|
---|
470 |
|
---|
471 | #if RTLNX_VER_MAX(4,16,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
472 | ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
|
---|
473 | #else
|
---|
474 | ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
|
---|
475 | #endif
|
---|
476 | if (ret)
|
---|
477 | return ret;
|
---|
478 |
|
---|
479 | bo->pin_count = 1;
|
---|
480 |
|
---|
481 | if (gpu_addr)
|
---|
482 | *gpu_addr = vbox_bo_gpu_offset(bo);
|
---|
483 |
|
---|
484 | return 0;
|
---|
485 | }
|
---|
486 |
|
---|
487 | int vbox_bo_unpin(struct vbox_bo *bo)
|
---|
488 | {
|
---|
489 | #if RTLNX_VER_MIN(4,16,0) || RTLNX_RHEL_MAJ_PREREQ(7,6) || RTLNX_SUSE_MAJ_PREREQ(15,1) || RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
490 | struct ttm_operation_ctx ctx = { false, false };
|
---|
491 | #endif
|
---|
492 | int i, ret;
|
---|
493 |
|
---|
494 | if (!bo->pin_count) {
|
---|
495 | DRM_ERROR("unpin bad %p\n", bo);
|
---|
496 | return 0;
|
---|
497 | }
|
---|
498 | bo->pin_count--;
|
---|
499 | if (bo->pin_count)
|
---|
500 | return 0;
|
---|
501 |
|
---|
502 | for (i = 0; i < bo->placement.num_placement; i++)
|
---|
503 | PLACEMENT_FLAGS(bo->placements[i]) &= ~TTM_PL_FLAG_NO_EVICT;
|
---|
504 |
|
---|
505 | #if RTLNX_VER_MAX(4,16,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
506 | ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
|
---|
507 | #else
|
---|
508 | ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
|
---|
509 | #endif
|
---|
510 | if (ret)
|
---|
511 | return ret;
|
---|
512 |
|
---|
513 | return 0;
|
---|
514 | }
|
---|
515 |
|
---|
516 | /*
|
---|
517 | * Move a vbox-owned buffer object to system memory if no one else has it
|
---|
518 | * pinned. The caller must have pinned it previously, and this call will
|
---|
519 | * release the caller's pin.
|
---|
520 | */
|
---|
521 | int vbox_bo_push_sysram(struct vbox_bo *bo)
|
---|
522 | {
|
---|
523 | #if RTLNX_VER_MIN(4,16,0) || RTLNX_RHEL_MAJ_PREREQ(7,6) || RTLNX_SUSE_MAJ_PREREQ(15,1) || RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
524 | struct ttm_operation_ctx ctx = { false, false };
|
---|
525 | #endif
|
---|
526 | int i, ret;
|
---|
527 |
|
---|
528 | if (!bo->pin_count) {
|
---|
529 | DRM_ERROR("unpin bad %p\n", bo);
|
---|
530 | return 0;
|
---|
531 | }
|
---|
532 | bo->pin_count--;
|
---|
533 | if (bo->pin_count)
|
---|
534 | return 0;
|
---|
535 |
|
---|
536 | if (bo->kmap.virtual)
|
---|
537 | ttm_bo_kunmap(&bo->kmap);
|
---|
538 |
|
---|
539 | vbox_ttm_placement(bo, TTM_PL_FLAG_SYSTEM);
|
---|
540 |
|
---|
541 | for (i = 0; i < bo->placement.num_placement; i++)
|
---|
542 | PLACEMENT_FLAGS(bo->placements[i]) |= TTM_PL_FLAG_NO_EVICT;
|
---|
543 |
|
---|
544 | #if RTLNX_VER_MAX(4,16,0) && !RTLNX_RHEL_MAJ_PREREQ(7,6) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
|
---|
545 | ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
|
---|
546 | #else
|
---|
547 | ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
|
---|
548 | #endif
|
---|
549 | if (ret) {
|
---|
550 | DRM_ERROR("pushing to VRAM failed\n");
|
---|
551 | return ret;
|
---|
552 | }
|
---|
553 |
|
---|
554 | return 0;
|
---|
555 | }
|
---|
556 |
|
---|
557 | int vbox_mmap(struct file *filp, struct vm_area_struct *vma)
|
---|
558 | {
|
---|
559 | struct drm_file *file_priv;
|
---|
560 | struct vbox_private *vbox;
|
---|
561 |
|
---|
562 | if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
|
---|
563 | return -EINVAL;
|
---|
564 |
|
---|
565 | file_priv = filp->private_data;
|
---|
566 | vbox = file_priv->minor->dev->dev_private;
|
---|
567 |
|
---|
568 | return ttm_bo_mmap(filp, vma, &vbox->ttm.bdev);
|
---|
569 | }
|
---|