OSDN Git Service

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