OSDN Git Service

drm/ttm: initialize globals during device init (v2)
[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 void vbox_bo_ttm_destroy(struct ttm_buffer_object *tbo)
39 {
40         struct vbox_bo *bo;
41
42         bo = container_of(tbo, struct vbox_bo, bo);
43
44         drm_gem_object_release(&bo->gem);
45         kfree(bo);
46 }
47
48 static bool vbox_ttm_bo_is_vbox_bo(struct ttm_buffer_object *bo)
49 {
50         if (bo->destroy == &vbox_bo_ttm_destroy)
51                 return true;
52
53         return false;
54 }
55
56 static int
57 vbox_bo_init_mem_type(struct ttm_bo_device *bdev, u32 type,
58                       struct ttm_mem_type_manager *man)
59 {
60         switch (type) {
61         case TTM_PL_SYSTEM:
62                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
63                 man->available_caching = TTM_PL_MASK_CACHING;
64                 man->default_caching = TTM_PL_FLAG_CACHED;
65                 break;
66         case TTM_PL_VRAM:
67                 man->func = &ttm_bo_manager_func;
68                 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
69                 man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
70                 man->default_caching = TTM_PL_FLAG_WC;
71                 break;
72         default:
73                 DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
74                 return -EINVAL;
75         }
76
77         return 0;
78 }
79
80 static void
81 vbox_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
82 {
83         struct vbox_bo *vboxbo = vbox_bo(bo);
84
85         if (!vbox_ttm_bo_is_vbox_bo(bo))
86                 return;
87
88         vbox_ttm_placement(vboxbo, TTM_PL_FLAG_SYSTEM);
89         *pl = vboxbo->placement;
90 }
91
92 static int vbox_bo_verify_access(struct ttm_buffer_object *bo,
93                                  struct file *filp)
94 {
95         return 0;
96 }
97
98 static int vbox_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
99                                    struct ttm_mem_reg *mem)
100 {
101         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
102         struct vbox_private *vbox = vbox_bdev(bdev);
103
104         mem->bus.addr = NULL;
105         mem->bus.offset = 0;
106         mem->bus.size = mem->num_pages << PAGE_SHIFT;
107         mem->bus.base = 0;
108         mem->bus.is_iomem = false;
109         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
110                 return -EINVAL;
111         switch (mem->mem_type) {
112         case TTM_PL_SYSTEM:
113                 /* system memory */
114                 return 0;
115         case TTM_PL_VRAM:
116                 mem->bus.offset = mem->start << PAGE_SHIFT;
117                 mem->bus.base = pci_resource_start(vbox->ddev.pdev, 0);
118                 mem->bus.is_iomem = true;
119                 break;
120         default:
121                 return -EINVAL;
122         }
123         return 0;
124 }
125
126 static void vbox_ttm_io_mem_free(struct ttm_bo_device *bdev,
127                                  struct ttm_mem_reg *mem)
128 {
129 }
130
131 static void vbox_ttm_backend_destroy(struct ttm_tt *tt)
132 {
133         ttm_tt_fini(tt);
134         kfree(tt);
135 }
136
137 static struct ttm_backend_func vbox_tt_backend_func = {
138         .destroy = &vbox_ttm_backend_destroy,
139 };
140
141 static struct ttm_tt *vbox_ttm_tt_create(struct ttm_buffer_object *bo,
142                                          u32 page_flags)
143 {
144         struct ttm_tt *tt;
145
146         tt = kzalloc(sizeof(*tt), GFP_KERNEL);
147         if (!tt)
148                 return NULL;
149
150         tt->func = &vbox_tt_backend_func;
151         if (ttm_tt_init(tt, bo, page_flags)) {
152                 kfree(tt);
153                 return NULL;
154         }
155
156         return tt;
157 }
158
159 static struct ttm_bo_driver vbox_bo_driver = {
160         .ttm_tt_create = vbox_ttm_tt_create,
161         .init_mem_type = vbox_bo_init_mem_type,
162         .eviction_valuable = ttm_bo_eviction_valuable,
163         .evict_flags = vbox_bo_evict_flags,
164         .verify_access = vbox_bo_verify_access,
165         .io_mem_reserve = &vbox_ttm_io_mem_reserve,
166         .io_mem_free = &vbox_ttm_io_mem_free,
167 };
168
169 int vbox_mm_init(struct vbox_private *vbox)
170 {
171         int ret;
172         struct drm_device *dev = &vbox->ddev;
173         struct ttm_bo_device *bdev = &vbox->ttm.bdev;
174
175         ret = ttm_bo_device_init(&vbox->ttm.bdev,
176                                  &vbox_bo_driver,
177                                  dev->anon_inode->i_mapping,
178                                  DRM_FILE_PAGE_OFFSET, true);
179         if (ret) {
180                 DRM_ERROR("Error initialising bo driver; %d\n", ret);
181                 return ret;
182         }
183
184         ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
185                              vbox->available_vram_size >> PAGE_SHIFT);
186         if (ret) {
187                 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
188                 goto err_device_release;
189         }
190
191 #ifdef DRM_MTRR_WC
192         vbox->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
193                                      pci_resource_len(dev->pdev, 0),
194                                      DRM_MTRR_WC);
195 #else
196         vbox->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
197                                          pci_resource_len(dev->pdev, 0));
198 #endif
199         return 0;
200
201 err_device_release:
202         ttm_bo_device_release(&vbox->ttm.bdev);
203         return ret;
204 }
205
206 void vbox_mm_fini(struct vbox_private *vbox)
207 {
208 #ifdef DRM_MTRR_WC
209         drm_mtrr_del(vbox->fb_mtrr,
210                      pci_resource_start(vbox->ddev.pdev, 0),
211                      pci_resource_len(vbox->ddev.pdev, 0), DRM_MTRR_WC);
212 #else
213         arch_phys_wc_del(vbox->fb_mtrr);
214 #endif
215         ttm_bo_device_release(&vbox->ttm.bdev);
216 }
217
218 void vbox_ttm_placement(struct vbox_bo *bo, int domain)
219 {
220         unsigned int i;
221         u32 c = 0;
222
223         bo->placement.placement = bo->placements;
224         bo->placement.busy_placement = bo->placements;
225
226         if (domain & TTM_PL_FLAG_VRAM)
227                 bo->placements[c++].flags =
228                     TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
229         if (domain & TTM_PL_FLAG_SYSTEM)
230                 bo->placements[c++].flags =
231                     TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
232         if (!c)
233                 bo->placements[c++].flags =
234                     TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
235
236         bo->placement.num_placement = c;
237         bo->placement.num_busy_placement = c;
238
239         for (i = 0; i < c; ++i) {
240                 bo->placements[i].fpfn = 0;
241                 bo->placements[i].lpfn = 0;
242         }
243 }
244
245 int vbox_bo_create(struct vbox_private *vbox, int size, int align,
246                    u32 flags, struct vbox_bo **pvboxbo)
247 {
248         struct vbox_bo *vboxbo;
249         size_t acc_size;
250         int ret;
251
252         vboxbo = kzalloc(sizeof(*vboxbo), GFP_KERNEL);
253         if (!vboxbo)
254                 return -ENOMEM;
255
256         ret = drm_gem_object_init(&vbox->ddev, &vboxbo->gem, size);
257         if (ret)
258                 goto err_free_vboxbo;
259
260         vboxbo->bo.bdev = &vbox->ttm.bdev;
261
262         vbox_ttm_placement(vboxbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
263
264         acc_size = ttm_bo_dma_acc_size(&vbox->ttm.bdev, size,
265                                        sizeof(struct vbox_bo));
266
267         ret = ttm_bo_init(&vbox->ttm.bdev, &vboxbo->bo, size,
268                           ttm_bo_type_device, &vboxbo->placement,
269                           align >> PAGE_SHIFT, false, acc_size,
270                           NULL, NULL, vbox_bo_ttm_destroy);
271         if (ret)
272                 goto err_free_vboxbo;
273
274         *pvboxbo = vboxbo;
275
276         return 0;
277
278 err_free_vboxbo:
279         kfree(vboxbo);
280         return ret;
281 }
282
283 int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag)
284 {
285         struct ttm_operation_ctx ctx = { false, false };
286         int i, ret;
287
288         if (bo->pin_count) {
289                 bo->pin_count++;
290                 return 0;
291         }
292
293         ret = vbox_bo_reserve(bo, false);
294         if (ret)
295                 return ret;
296
297         vbox_ttm_placement(bo, pl_flag);
298
299         for (i = 0; i < bo->placement.num_placement; i++)
300                 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
301
302         ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
303         if (ret == 0)
304                 bo->pin_count = 1;
305
306         vbox_bo_unreserve(bo);
307
308         return ret;
309 }
310
311 int vbox_bo_unpin(struct vbox_bo *bo)
312 {
313         struct ttm_operation_ctx ctx = { false, false };
314         int i, ret;
315
316         if (!bo->pin_count) {
317                 DRM_ERROR("unpin bad %p\n", bo);
318                 return 0;
319         }
320         bo->pin_count--;
321         if (bo->pin_count)
322                 return 0;
323
324         ret = vbox_bo_reserve(bo, false);
325         if (ret) {
326                 DRM_ERROR("Error %d reserving bo, leaving it pinned\n", ret);
327                 return ret;
328         }
329
330         for (i = 0; i < bo->placement.num_placement; i++)
331                 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
332
333         ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
334
335         vbox_bo_unreserve(bo);
336
337         return ret;
338 }
339
340 /*
341  * Move a vbox-owned buffer object to system memory if no one else has it
342  * pinned.  The caller must have pinned it previously, and this call will
343  * release the caller's pin.
344  */
345 int vbox_bo_push_sysram(struct vbox_bo *bo)
346 {
347         struct ttm_operation_ctx ctx = { false, false };
348         int i, ret;
349
350         if (!bo->pin_count) {
351                 DRM_ERROR("unpin bad %p\n", bo);
352                 return 0;
353         }
354         bo->pin_count--;
355         if (bo->pin_count)
356                 return 0;
357
358         if (bo->kmap.virtual) {
359                 ttm_bo_kunmap(&bo->kmap);
360                 bo->kmap.virtual = NULL;
361         }
362
363         vbox_ttm_placement(bo, TTM_PL_FLAG_SYSTEM);
364
365         for (i = 0; i < bo->placement.num_placement; i++)
366                 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
367
368         ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
369         if (ret) {
370                 DRM_ERROR("pushing to VRAM failed\n");
371                 return ret;
372         }
373
374         return 0;
375 }
376
377 int vbox_mmap(struct file *filp, struct vm_area_struct *vma)
378 {
379         struct drm_file *file_priv;
380         struct vbox_private *vbox;
381
382         if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
383                 return -EINVAL;
384
385         file_priv = filp->private_data;
386         vbox = file_priv->minor->dev->dev_private;
387
388         return ttm_bo_mmap(filp, vma, &vbox->ttm.bdev);
389 }
390
391 void *vbox_bo_kmap(struct vbox_bo *bo)
392 {
393         int ret;
394
395         if (bo->kmap.virtual)
396                 return bo->kmap.virtual;
397
398         ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
399         if (ret) {
400                 DRM_ERROR("Error kmapping bo: %d\n", ret);
401                 return NULL;
402         }
403
404         return bo->kmap.virtual;
405 }
406
407 void vbox_bo_kunmap(struct vbox_bo *bo)
408 {
409         if (bo->kmap.virtual) {
410                 ttm_bo_kunmap(&bo->kmap);
411                 bo->kmap.virtual = NULL;
412         }
413 }