]> Git Repo - linux.git/blob - drivers/gpu/drm/bochs/bochs_mm.c
Merge tag 'drm-misc-next-2018-11-21' of git://anongit.freedesktop.org/drm/drm-misc...
[linux.git] / drivers / gpu / drm / bochs / bochs_mm.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  */
7
8 #include "bochs.h"
9
10 static void bochs_ttm_placement(struct bochs_bo *bo, int domain);
11
12 /* ---------------------------------------------------------------------- */
13
14 static inline struct bochs_device *bochs_bdev(struct ttm_bo_device *bd)
15 {
16         return container_of(bd, struct bochs_device, ttm.bdev);
17 }
18
19 static void bochs_bo_ttm_destroy(struct ttm_buffer_object *tbo)
20 {
21         struct bochs_bo *bo;
22
23         bo = container_of(tbo, struct bochs_bo, bo);
24         drm_gem_object_release(&bo->gem);
25         kfree(bo);
26 }
27
28 static bool bochs_ttm_bo_is_bochs_bo(struct ttm_buffer_object *bo)
29 {
30         if (bo->destroy == &bochs_bo_ttm_destroy)
31                 return true;
32         return false;
33 }
34
35 static int bochs_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
36                                   struct ttm_mem_type_manager *man)
37 {
38         switch (type) {
39         case TTM_PL_SYSTEM:
40                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
41                 man->available_caching = TTM_PL_MASK_CACHING;
42                 man->default_caching = TTM_PL_FLAG_CACHED;
43                 break;
44         case TTM_PL_VRAM:
45                 man->func = &ttm_bo_manager_func;
46                 man->flags = TTM_MEMTYPE_FLAG_FIXED |
47                         TTM_MEMTYPE_FLAG_MAPPABLE;
48                 man->available_caching = TTM_PL_FLAG_UNCACHED |
49                         TTM_PL_FLAG_WC;
50                 man->default_caching = TTM_PL_FLAG_WC;
51                 break;
52         default:
53                 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
54                 return -EINVAL;
55         }
56         return 0;
57 }
58
59 static void
60 bochs_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
61 {
62         struct bochs_bo *bochsbo = bochs_bo(bo);
63
64         if (!bochs_ttm_bo_is_bochs_bo(bo))
65                 return;
66
67         bochs_ttm_placement(bochsbo, TTM_PL_FLAG_SYSTEM);
68         *pl = bochsbo->placement;
69 }
70
71 static int bochs_bo_verify_access(struct ttm_buffer_object *bo,
72                                   struct file *filp)
73 {
74         struct bochs_bo *bochsbo = bochs_bo(bo);
75
76         return drm_vma_node_verify_access(&bochsbo->gem.vma_node,
77                                           filp->private_data);
78 }
79
80 static int bochs_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
81                                     struct ttm_mem_reg *mem)
82 {
83         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
84         struct bochs_device *bochs = bochs_bdev(bdev);
85
86         mem->bus.addr = NULL;
87         mem->bus.offset = 0;
88         mem->bus.size = mem->num_pages << PAGE_SHIFT;
89         mem->bus.base = 0;
90         mem->bus.is_iomem = false;
91         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
92                 return -EINVAL;
93         switch (mem->mem_type) {
94         case TTM_PL_SYSTEM:
95                 /* system memory */
96                 return 0;
97         case TTM_PL_VRAM:
98                 mem->bus.offset = mem->start << PAGE_SHIFT;
99                 mem->bus.base = bochs->fb_base;
100                 mem->bus.is_iomem = true;
101                 break;
102         default:
103                 return -EINVAL;
104                 break;
105         }
106         return 0;
107 }
108
109 static void bochs_ttm_io_mem_free(struct ttm_bo_device *bdev,
110                                   struct ttm_mem_reg *mem)
111 {
112 }
113
114 static void bochs_ttm_backend_destroy(struct ttm_tt *tt)
115 {
116         ttm_tt_fini(tt);
117         kfree(tt);
118 }
119
120 static struct ttm_backend_func bochs_tt_backend_func = {
121         .destroy = &bochs_ttm_backend_destroy,
122 };
123
124 static struct ttm_tt *bochs_ttm_tt_create(struct ttm_buffer_object *bo,
125                                           uint32_t page_flags)
126 {
127         struct ttm_tt *tt;
128
129         tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
130         if (tt == NULL)
131                 return NULL;
132         tt->func = &bochs_tt_backend_func;
133         if (ttm_tt_init(tt, bo, page_flags)) {
134                 kfree(tt);
135                 return NULL;
136         }
137         return tt;
138 }
139
140 static struct ttm_bo_driver bochs_bo_driver = {
141         .ttm_tt_create = bochs_ttm_tt_create,
142         .init_mem_type = bochs_bo_init_mem_type,
143         .eviction_valuable = ttm_bo_eviction_valuable,
144         .evict_flags = bochs_bo_evict_flags,
145         .move = NULL,
146         .verify_access = bochs_bo_verify_access,
147         .io_mem_reserve = &bochs_ttm_io_mem_reserve,
148         .io_mem_free = &bochs_ttm_io_mem_free,
149 };
150
151 int bochs_mm_init(struct bochs_device *bochs)
152 {
153         struct ttm_bo_device *bdev = &bochs->ttm.bdev;
154         int ret;
155
156         ret = ttm_bo_device_init(&bochs->ttm.bdev,
157                                  &bochs_bo_driver,
158                                  bochs->dev->anon_inode->i_mapping,
159                                  DRM_FILE_PAGE_OFFSET,
160                                  true);
161         if (ret) {
162                 DRM_ERROR("Error initialising bo driver; %d\n", ret);
163                 return ret;
164         }
165
166         ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
167                              bochs->fb_size >> PAGE_SHIFT);
168         if (ret) {
169                 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
170                 return ret;
171         }
172
173         bochs->ttm.initialized = true;
174         return 0;
175 }
176
177 void bochs_mm_fini(struct bochs_device *bochs)
178 {
179         if (!bochs->ttm.initialized)
180                 return;
181
182         ttm_bo_device_release(&bochs->ttm.bdev);
183         bochs->ttm.initialized = false;
184 }
185
186 static void bochs_ttm_placement(struct bochs_bo *bo, int domain)
187 {
188         unsigned i;
189         u32 c = 0;
190         bo->placement.placement = bo->placements;
191         bo->placement.busy_placement = bo->placements;
192         if (domain & TTM_PL_FLAG_VRAM) {
193                 bo->placements[c++].flags = TTM_PL_FLAG_WC
194                         | TTM_PL_FLAG_UNCACHED
195                         | TTM_PL_FLAG_VRAM;
196         }
197         if (domain & TTM_PL_FLAG_SYSTEM) {
198                 bo->placements[c++].flags = TTM_PL_MASK_CACHING
199                         | TTM_PL_FLAG_SYSTEM;
200         }
201         if (!c) {
202                 bo->placements[c++].flags = TTM_PL_MASK_CACHING
203                         | TTM_PL_FLAG_SYSTEM;
204         }
205         for (i = 0; i < c; ++i) {
206                 bo->placements[i].fpfn = 0;
207                 bo->placements[i].lpfn = 0;
208         }
209         bo->placement.num_placement = c;
210         bo->placement.num_busy_placement = c;
211 }
212
213 static inline u64 bochs_bo_gpu_offset(struct bochs_bo *bo)
214 {
215         return bo->bo.offset;
216 }
217
218 int bochs_bo_pin(struct bochs_bo *bo, u32 pl_flag, u64 *gpu_addr)
219 {
220         struct ttm_operation_ctx ctx = { false, false };
221         int i, ret;
222
223         if (bo->pin_count) {
224                 bo->pin_count++;
225                 if (gpu_addr)
226                         *gpu_addr = bochs_bo_gpu_offset(bo);
227                 return 0;
228         }
229
230         bochs_ttm_placement(bo, pl_flag);
231         for (i = 0; i < bo->placement.num_placement; i++)
232                 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
233         ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
234         if (ret)
235                 return ret;
236
237         bo->pin_count = 1;
238         if (gpu_addr)
239                 *gpu_addr = bochs_bo_gpu_offset(bo);
240         return 0;
241 }
242
243 int bochs_bo_unpin(struct bochs_bo *bo)
244 {
245         struct ttm_operation_ctx ctx = { false, false };
246         int i, ret;
247
248         if (!bo->pin_count) {
249                 DRM_ERROR("unpin bad %p\n", bo);
250                 return 0;
251         }
252         bo->pin_count--;
253
254         if (bo->pin_count)
255                 return 0;
256
257         for (i = 0; i < bo->placement.num_placement; i++)
258                 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
259         ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
260         if (ret)
261                 return ret;
262
263         return 0;
264 }
265
266 int bochs_mmap(struct file *filp, struct vm_area_struct *vma)
267 {
268         struct drm_file *file_priv;
269         struct bochs_device *bochs;
270
271         if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
272                 return -EINVAL;
273
274         file_priv = filp->private_data;
275         bochs = file_priv->minor->dev->dev_private;
276         return ttm_bo_mmap(filp, vma, &bochs->ttm.bdev);
277 }
278
279 /* ---------------------------------------------------------------------- */
280
281 static int bochs_bo_create(struct drm_device *dev, int size, int align,
282                            uint32_t flags, struct bochs_bo **pbochsbo)
283 {
284         struct bochs_device *bochs = dev->dev_private;
285         struct bochs_bo *bochsbo;
286         size_t acc_size;
287         int ret;
288
289         bochsbo = kzalloc(sizeof(struct bochs_bo), GFP_KERNEL);
290         if (!bochsbo)
291                 return -ENOMEM;
292
293         ret = drm_gem_object_init(dev, &bochsbo->gem, size);
294         if (ret) {
295                 kfree(bochsbo);
296                 return ret;
297         }
298
299         bochsbo->bo.bdev = &bochs->ttm.bdev;
300         bochsbo->bo.bdev->dev_mapping = dev->anon_inode->i_mapping;
301
302         bochs_ttm_placement(bochsbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
303
304         acc_size = ttm_bo_dma_acc_size(&bochs->ttm.bdev, size,
305                                        sizeof(struct bochs_bo));
306
307         ret = ttm_bo_init(&bochs->ttm.bdev, &bochsbo->bo, size,
308                           ttm_bo_type_device, &bochsbo->placement,
309                           align >> PAGE_SHIFT, false, acc_size,
310                           NULL, NULL, bochs_bo_ttm_destroy);
311         if (ret)
312                 return ret;
313
314         *pbochsbo = bochsbo;
315         return 0;
316 }
317
318 int bochs_gem_create(struct drm_device *dev, u32 size, bool iskernel,
319                      struct drm_gem_object **obj)
320 {
321         struct bochs_bo *bochsbo;
322         int ret;
323
324         *obj = NULL;
325
326         size = PAGE_ALIGN(size);
327         if (size == 0)
328                 return -EINVAL;
329
330         ret = bochs_bo_create(dev, size, 0, 0, &bochsbo);
331         if (ret) {
332                 if (ret != -ERESTARTSYS)
333                         DRM_ERROR("failed to allocate GEM object\n");
334                 return ret;
335         }
336         *obj = &bochsbo->gem;
337         return 0;
338 }
339
340 int bochs_dumb_create(struct drm_file *file, struct drm_device *dev,
341                       struct drm_mode_create_dumb *args)
342 {
343         struct drm_gem_object *gobj;
344         u32 handle;
345         int ret;
346
347         args->pitch = args->width * ((args->bpp + 7) / 8);
348         args->size = args->pitch * args->height;
349
350         ret = bochs_gem_create(dev, args->size, false,
351                                &gobj);
352         if (ret)
353                 return ret;
354
355         ret = drm_gem_handle_create(file, gobj, &handle);
356         drm_gem_object_put_unlocked(gobj);
357         if (ret)
358                 return ret;
359
360         args->handle = handle;
361         return 0;
362 }
363
364 static void bochs_bo_unref(struct bochs_bo **bo)
365 {
366         struct ttm_buffer_object *tbo;
367
368         if ((*bo) == NULL)
369                 return;
370
371         tbo = &((*bo)->bo);
372         ttm_bo_put(tbo);
373         *bo = NULL;
374 }
375
376 void bochs_gem_free_object(struct drm_gem_object *obj)
377 {
378         struct bochs_bo *bochs_bo = gem_to_bochs_bo(obj);
379
380         bochs_bo_unref(&bochs_bo);
381 }
382
383 int bochs_dumb_mmap_offset(struct drm_file *file, struct drm_device *dev,
384                            uint32_t handle, uint64_t *offset)
385 {
386         struct drm_gem_object *obj;
387         struct bochs_bo *bo;
388
389         obj = drm_gem_object_lookup(file, handle);
390         if (obj == NULL)
391                 return -ENOENT;
392
393         bo = gem_to_bochs_bo(obj);
394         *offset = bochs_bo_mmap_offset(bo);
395
396         drm_gem_object_put_unlocked(obj);
397         return 0;
398 }
This page took 0.059011 seconds and 4 git commands to generate.