Merge tag 'powerpc-4.8-2' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[cascardo/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 int bochs_ttm_mem_global_init(struct drm_global_reference *ref)
20 {
21         return ttm_mem_global_init(ref->object);
22 }
23
24 static void bochs_ttm_mem_global_release(struct drm_global_reference *ref)
25 {
26         ttm_mem_global_release(ref->object);
27 }
28
29 static int bochs_ttm_global_init(struct bochs_device *bochs)
30 {
31         struct drm_global_reference *global_ref;
32         int r;
33
34         global_ref = &bochs->ttm.mem_global_ref;
35         global_ref->global_type = DRM_GLOBAL_TTM_MEM;
36         global_ref->size = sizeof(struct ttm_mem_global);
37         global_ref->init = &bochs_ttm_mem_global_init;
38         global_ref->release = &bochs_ttm_mem_global_release;
39         r = drm_global_item_ref(global_ref);
40         if (r != 0) {
41                 DRM_ERROR("Failed setting up TTM memory accounting "
42                           "subsystem.\n");
43                 return r;
44         }
45
46         bochs->ttm.bo_global_ref.mem_glob =
47                 bochs->ttm.mem_global_ref.object;
48         global_ref = &bochs->ttm.bo_global_ref.ref;
49         global_ref->global_type = DRM_GLOBAL_TTM_BO;
50         global_ref->size = sizeof(struct ttm_bo_global);
51         global_ref->init = &ttm_bo_global_init;
52         global_ref->release = &ttm_bo_global_release;
53         r = drm_global_item_ref(global_ref);
54         if (r != 0) {
55                 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
56                 drm_global_item_unref(&bochs->ttm.mem_global_ref);
57                 return r;
58         }
59
60         return 0;
61 }
62
63 static void bochs_ttm_global_release(struct bochs_device *bochs)
64 {
65         if (bochs->ttm.mem_global_ref.release == NULL)
66                 return;
67
68         drm_global_item_unref(&bochs->ttm.bo_global_ref.ref);
69         drm_global_item_unref(&bochs->ttm.mem_global_ref);
70         bochs->ttm.mem_global_ref.release = NULL;
71 }
72
73
74 static void bochs_bo_ttm_destroy(struct ttm_buffer_object *tbo)
75 {
76         struct bochs_bo *bo;
77
78         bo = container_of(tbo, struct bochs_bo, bo);
79         drm_gem_object_release(&bo->gem);
80         kfree(bo);
81 }
82
83 static bool bochs_ttm_bo_is_bochs_bo(struct ttm_buffer_object *bo)
84 {
85         if (bo->destroy == &bochs_bo_ttm_destroy)
86                 return true;
87         return false;
88 }
89
90 static int bochs_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
91                                   struct ttm_mem_type_manager *man)
92 {
93         switch (type) {
94         case TTM_PL_SYSTEM:
95                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
96                 man->available_caching = TTM_PL_MASK_CACHING;
97                 man->default_caching = TTM_PL_FLAG_CACHED;
98                 break;
99         case TTM_PL_VRAM:
100                 man->func = &ttm_bo_manager_func;
101                 man->flags = TTM_MEMTYPE_FLAG_FIXED |
102                         TTM_MEMTYPE_FLAG_MAPPABLE;
103                 man->available_caching = TTM_PL_FLAG_UNCACHED |
104                         TTM_PL_FLAG_WC;
105                 man->default_caching = TTM_PL_FLAG_WC;
106                 break;
107         default:
108                 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
109                 return -EINVAL;
110         }
111         return 0;
112 }
113
114 static void
115 bochs_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
116 {
117         struct bochs_bo *bochsbo = bochs_bo(bo);
118
119         if (!bochs_ttm_bo_is_bochs_bo(bo))
120                 return;
121
122         bochs_ttm_placement(bochsbo, TTM_PL_FLAG_SYSTEM);
123         *pl = bochsbo->placement;
124 }
125
126 static int bochs_bo_verify_access(struct ttm_buffer_object *bo,
127                                   struct file *filp)
128 {
129         struct bochs_bo *bochsbo = bochs_bo(bo);
130
131         return drm_vma_node_verify_access(&bochsbo->gem.vma_node, filp);
132 }
133
134 static int bochs_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
135                                     struct ttm_mem_reg *mem)
136 {
137         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
138         struct bochs_device *bochs = bochs_bdev(bdev);
139
140         mem->bus.addr = NULL;
141         mem->bus.offset = 0;
142         mem->bus.size = mem->num_pages << PAGE_SHIFT;
143         mem->bus.base = 0;
144         mem->bus.is_iomem = false;
145         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
146                 return -EINVAL;
147         switch (mem->mem_type) {
148         case TTM_PL_SYSTEM:
149                 /* system memory */
150                 return 0;
151         case TTM_PL_VRAM:
152                 mem->bus.offset = mem->start << PAGE_SHIFT;
153                 mem->bus.base = bochs->fb_base;
154                 mem->bus.is_iomem = true;
155                 break;
156         default:
157                 return -EINVAL;
158                 break;
159         }
160         return 0;
161 }
162
163 static void bochs_ttm_io_mem_free(struct ttm_bo_device *bdev,
164                                   struct ttm_mem_reg *mem)
165 {
166 }
167
168 static void bochs_ttm_backend_destroy(struct ttm_tt *tt)
169 {
170         ttm_tt_fini(tt);
171         kfree(tt);
172 }
173
174 static struct ttm_backend_func bochs_tt_backend_func = {
175         .destroy = &bochs_ttm_backend_destroy,
176 };
177
178 static struct ttm_tt *bochs_ttm_tt_create(struct ttm_bo_device *bdev,
179                                           unsigned long size,
180                                           uint32_t page_flags,
181                                           struct page *dummy_read_page)
182 {
183         struct ttm_tt *tt;
184
185         tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
186         if (tt == NULL)
187                 return NULL;
188         tt->func = &bochs_tt_backend_func;
189         if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) {
190                 kfree(tt);
191                 return NULL;
192         }
193         return tt;
194 }
195
196 struct ttm_bo_driver bochs_bo_driver = {
197         .ttm_tt_create = bochs_ttm_tt_create,
198         .ttm_tt_populate = ttm_pool_populate,
199         .ttm_tt_unpopulate = ttm_pool_unpopulate,
200         .init_mem_type = bochs_bo_init_mem_type,
201         .evict_flags = bochs_bo_evict_flags,
202         .move = NULL,
203         .verify_access = bochs_bo_verify_access,
204         .io_mem_reserve = &bochs_ttm_io_mem_reserve,
205         .io_mem_free = &bochs_ttm_io_mem_free,
206         .lru_tail = &ttm_bo_default_lru_tail,
207         .swap_lru_tail = &ttm_bo_default_swap_lru_tail,
208 };
209
210 int bochs_mm_init(struct bochs_device *bochs)
211 {
212         struct ttm_bo_device *bdev = &bochs->ttm.bdev;
213         int ret;
214
215         ret = bochs_ttm_global_init(bochs);
216         if (ret)
217                 return ret;
218
219         ret = ttm_bo_device_init(&bochs->ttm.bdev,
220                                  bochs->ttm.bo_global_ref.ref.object,
221                                  &bochs_bo_driver,
222                                  bochs->dev->anon_inode->i_mapping,
223                                  DRM_FILE_PAGE_OFFSET,
224                                  true);
225         if (ret) {
226                 DRM_ERROR("Error initialising bo driver; %d\n", ret);
227                 return ret;
228         }
229
230         ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
231                              bochs->fb_size >> PAGE_SHIFT);
232         if (ret) {
233                 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
234                 return ret;
235         }
236
237         bochs->ttm.initialized = true;
238         return 0;
239 }
240
241 void bochs_mm_fini(struct bochs_device *bochs)
242 {
243         if (!bochs->ttm.initialized)
244                 return;
245
246         ttm_bo_device_release(&bochs->ttm.bdev);
247         bochs_ttm_global_release(bochs);
248         bochs->ttm.initialized = false;
249 }
250
251 static void bochs_ttm_placement(struct bochs_bo *bo, int domain)
252 {
253         unsigned i;
254         u32 c = 0;
255         bo->placement.placement = bo->placements;
256         bo->placement.busy_placement = bo->placements;
257         if (domain & TTM_PL_FLAG_VRAM) {
258                 bo->placements[c++].flags = TTM_PL_FLAG_WC
259                         | TTM_PL_FLAG_UNCACHED
260                         | TTM_PL_FLAG_VRAM;
261         }
262         if (domain & TTM_PL_FLAG_SYSTEM) {
263                 bo->placements[c++].flags = TTM_PL_MASK_CACHING
264                         | TTM_PL_FLAG_SYSTEM;
265         }
266         if (!c) {
267                 bo->placements[c++].flags = TTM_PL_MASK_CACHING
268                         | TTM_PL_FLAG_SYSTEM;
269         }
270         for (i = 0; i < c; ++i) {
271                 bo->placements[i].fpfn = 0;
272                 bo->placements[i].lpfn = 0;
273         }
274         bo->placement.num_placement = c;
275         bo->placement.num_busy_placement = c;
276 }
277
278 static inline u64 bochs_bo_gpu_offset(struct bochs_bo *bo)
279 {
280         return bo->bo.offset;
281 }
282
283 int bochs_bo_pin(struct bochs_bo *bo, u32 pl_flag, u64 *gpu_addr)
284 {
285         int i, ret;
286
287         if (bo->pin_count) {
288                 bo->pin_count++;
289                 if (gpu_addr)
290                         *gpu_addr = bochs_bo_gpu_offset(bo);
291                 return 0;
292         }
293
294         bochs_ttm_placement(bo, pl_flag);
295         for (i = 0; i < bo->placement.num_placement; i++)
296                 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
297         ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
298         if (ret)
299                 return ret;
300
301         bo->pin_count = 1;
302         if (gpu_addr)
303                 *gpu_addr = bochs_bo_gpu_offset(bo);
304         return 0;
305 }
306
307 int bochs_bo_unpin(struct bochs_bo *bo)
308 {
309         int i, ret;
310
311         if (!bo->pin_count) {
312                 DRM_ERROR("unpin bad %p\n", bo);
313                 return 0;
314         }
315         bo->pin_count--;
316
317         if (bo->pin_count)
318                 return 0;
319
320         for (i = 0; i < bo->placement.num_placement; i++)
321                 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
322         ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
323         if (ret)
324                 return ret;
325
326         return 0;
327 }
328
329 int bochs_mmap(struct file *filp, struct vm_area_struct *vma)
330 {
331         struct drm_file *file_priv;
332         struct bochs_device *bochs;
333
334         if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
335                 return -EINVAL;
336
337         file_priv = filp->private_data;
338         bochs = file_priv->minor->dev->dev_private;
339         return ttm_bo_mmap(filp, vma, &bochs->ttm.bdev);
340 }
341
342 /* ---------------------------------------------------------------------- */
343
344 static int bochs_bo_create(struct drm_device *dev, int size, int align,
345                            uint32_t flags, struct bochs_bo **pbochsbo)
346 {
347         struct bochs_device *bochs = dev->dev_private;
348         struct bochs_bo *bochsbo;
349         size_t acc_size;
350         int ret;
351
352         bochsbo = kzalloc(sizeof(struct bochs_bo), GFP_KERNEL);
353         if (!bochsbo)
354                 return -ENOMEM;
355
356         ret = drm_gem_object_init(dev, &bochsbo->gem, size);
357         if (ret) {
358                 kfree(bochsbo);
359                 return ret;
360         }
361
362         bochsbo->bo.bdev = &bochs->ttm.bdev;
363         bochsbo->bo.bdev->dev_mapping = dev->anon_inode->i_mapping;
364
365         bochs_ttm_placement(bochsbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
366
367         acc_size = ttm_bo_dma_acc_size(&bochs->ttm.bdev, size,
368                                        sizeof(struct bochs_bo));
369
370         ret = ttm_bo_init(&bochs->ttm.bdev, &bochsbo->bo, size,
371                           ttm_bo_type_device, &bochsbo->placement,
372                           align >> PAGE_SHIFT, false, NULL, acc_size,
373                           NULL, NULL, bochs_bo_ttm_destroy);
374         if (ret)
375                 return ret;
376
377         *pbochsbo = bochsbo;
378         return 0;
379 }
380
381 int bochs_gem_create(struct drm_device *dev, u32 size, bool iskernel,
382                      struct drm_gem_object **obj)
383 {
384         struct bochs_bo *bochsbo;
385         int ret;
386
387         *obj = NULL;
388
389         size = PAGE_ALIGN(size);
390         if (size == 0)
391                 return -EINVAL;
392
393         ret = bochs_bo_create(dev, size, 0, 0, &bochsbo);
394         if (ret) {
395                 if (ret != -ERESTARTSYS)
396                         DRM_ERROR("failed to allocate GEM object\n");
397                 return ret;
398         }
399         *obj = &bochsbo->gem;
400         return 0;
401 }
402
403 int bochs_dumb_create(struct drm_file *file, struct drm_device *dev,
404                       struct drm_mode_create_dumb *args)
405 {
406         struct drm_gem_object *gobj;
407         u32 handle;
408         int ret;
409
410         args->pitch = args->width * ((args->bpp + 7) / 8);
411         args->size = args->pitch * args->height;
412
413         ret = bochs_gem_create(dev, args->size, false,
414                                &gobj);
415         if (ret)
416                 return ret;
417
418         ret = drm_gem_handle_create(file, gobj, &handle);
419         drm_gem_object_unreference_unlocked(gobj);
420         if (ret)
421                 return ret;
422
423         args->handle = handle;
424         return 0;
425 }
426
427 static void bochs_bo_unref(struct bochs_bo **bo)
428 {
429         struct ttm_buffer_object *tbo;
430
431         if ((*bo) == NULL)
432                 return;
433
434         tbo = &((*bo)->bo);
435         ttm_bo_unref(&tbo);
436         *bo = NULL;
437 }
438
439 void bochs_gem_free_object(struct drm_gem_object *obj)
440 {
441         struct bochs_bo *bochs_bo = gem_to_bochs_bo(obj);
442
443         bochs_bo_unref(&bochs_bo);
444 }
445
446 int bochs_dumb_mmap_offset(struct drm_file *file, struct drm_device *dev,
447                            uint32_t handle, uint64_t *offset)
448 {
449         struct drm_gem_object *obj;
450         struct bochs_bo *bo;
451
452         obj = drm_gem_object_lookup(file, handle);
453         if (obj == NULL)
454                 return -ENOENT;
455
456         bo = gem_to_bochs_bo(obj);
457         *offset = bochs_bo_mmap_offset(bo);
458
459         drm_gem_object_unreference_unlocked(obj);
460         return 0;
461 }
462
463 /* ---------------------------------------------------------------------- */
464
465 static void bochs_user_framebuffer_destroy(struct drm_framebuffer *fb)
466 {
467         struct bochs_framebuffer *bochs_fb = to_bochs_framebuffer(fb);
468
469         drm_gem_object_unreference_unlocked(bochs_fb->obj);
470         drm_framebuffer_cleanup(fb);
471         kfree(fb);
472 }
473
474 static const struct drm_framebuffer_funcs bochs_fb_funcs = {
475         .destroy = bochs_user_framebuffer_destroy,
476 };
477
478 int bochs_framebuffer_init(struct drm_device *dev,
479                            struct bochs_framebuffer *gfb,
480                            const struct drm_mode_fb_cmd2 *mode_cmd,
481                            struct drm_gem_object *obj)
482 {
483         int ret;
484
485         drm_helper_mode_fill_fb_struct(&gfb->base, mode_cmd);
486         gfb->obj = obj;
487         ret = drm_framebuffer_init(dev, &gfb->base, &bochs_fb_funcs);
488         if (ret) {
489                 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
490                 return ret;
491         }
492         return 0;
493 }
494
495 static struct drm_framebuffer *
496 bochs_user_framebuffer_create(struct drm_device *dev,
497                               struct drm_file *filp,
498                               const struct drm_mode_fb_cmd2 *mode_cmd)
499 {
500         struct drm_gem_object *obj;
501         struct bochs_framebuffer *bochs_fb;
502         int ret;
503
504         DRM_DEBUG_DRIVER("%dx%d, format %c%c%c%c\n",
505                mode_cmd->width, mode_cmd->height,
506                (mode_cmd->pixel_format)       & 0xff,
507                (mode_cmd->pixel_format >> 8)  & 0xff,
508                (mode_cmd->pixel_format >> 16) & 0xff,
509                (mode_cmd->pixel_format >> 24) & 0xff);
510
511         if (mode_cmd->pixel_format != DRM_FORMAT_XRGB8888)
512                 return ERR_PTR(-ENOENT);
513
514         obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
515         if (obj == NULL)
516                 return ERR_PTR(-ENOENT);
517
518         bochs_fb = kzalloc(sizeof(*bochs_fb), GFP_KERNEL);
519         if (!bochs_fb) {
520                 drm_gem_object_unreference_unlocked(obj);
521                 return ERR_PTR(-ENOMEM);
522         }
523
524         ret = bochs_framebuffer_init(dev, bochs_fb, mode_cmd, obj);
525         if (ret) {
526                 drm_gem_object_unreference_unlocked(obj);
527                 kfree(bochs_fb);
528                 return ERR_PTR(ret);
529         }
530         return &bochs_fb->base;
531 }
532
533 const struct drm_mode_config_funcs bochs_mode_funcs = {
534         .fb_create = bochs_user_framebuffer_create,
535 };