OSDN Git Service

drm/ttm: initialize globals during device init (v2)
[uclinux-h8/linux.git] / drivers / gpu / drm / hisilicon / hibmc / hibmc_ttm.c
1 /* Hisilicon Hibmc SoC drm driver
2  *
3  * Based on the bochs drm driver.
4  *
5  * Copyright (c) 2016 Huawei Limited.
6  *
7  * Author:
8  *      Rongrong Zou <zourongrong@huawei.com>
9  *      Rongrong Zou <zourongrong@gmail.com>
10  *      Jianhua Li <lijianhua@huawei.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  */
18
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/ttm/ttm_page_alloc.h>
21
22 #include "hibmc_drm_drv.h"
23
24 #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
25
26 static inline struct hibmc_drm_private *
27 hibmc_bdev(struct ttm_bo_device *bd)
28 {
29         return container_of(bd, struct hibmc_drm_private, bdev);
30 }
31
32 static void hibmc_bo_ttm_destroy(struct ttm_buffer_object *tbo)
33 {
34         struct hibmc_bo *bo = container_of(tbo, struct hibmc_bo, bo);
35
36         drm_gem_object_release(&bo->gem);
37         kfree(bo);
38 }
39
40 static bool hibmc_ttm_bo_is_hibmc_bo(struct ttm_buffer_object *bo)
41 {
42         return bo->destroy == &hibmc_bo_ttm_destroy;
43 }
44
45 static int
46 hibmc_bo_init_mem_type(struct ttm_bo_device *bdev, u32 type,
47                        struct ttm_mem_type_manager *man)
48 {
49         switch (type) {
50         case TTM_PL_SYSTEM:
51                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
52                 man->available_caching = TTM_PL_MASK_CACHING;
53                 man->default_caching = TTM_PL_FLAG_CACHED;
54                 break;
55         case TTM_PL_VRAM:
56                 man->func = &ttm_bo_manager_func;
57                 man->flags = TTM_MEMTYPE_FLAG_FIXED |
58                         TTM_MEMTYPE_FLAG_MAPPABLE;
59                 man->available_caching = TTM_PL_FLAG_UNCACHED |
60                         TTM_PL_FLAG_WC;
61                 man->default_caching = TTM_PL_FLAG_WC;
62                 break;
63         default:
64                 DRM_ERROR("unsupported memory type %u\n", type);
65                 return -EINVAL;
66         }
67         return 0;
68 }
69
70 void hibmc_ttm_placement(struct hibmc_bo *bo, int domain)
71 {
72         u32 count = 0;
73         u32 i;
74
75         bo->placement.placement = bo->placements;
76         bo->placement.busy_placement = bo->placements;
77         if (domain & TTM_PL_FLAG_VRAM)
78                 bo->placements[count++].flags = TTM_PL_FLAG_WC |
79                         TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM;
80         if (domain & TTM_PL_FLAG_SYSTEM)
81                 bo->placements[count++].flags = TTM_PL_MASK_CACHING |
82                         TTM_PL_FLAG_SYSTEM;
83         if (!count)
84                 bo->placements[count++].flags = TTM_PL_MASK_CACHING |
85                         TTM_PL_FLAG_SYSTEM;
86
87         bo->placement.num_placement = count;
88         bo->placement.num_busy_placement = count;
89         for (i = 0; i < count; i++) {
90                 bo->placements[i].fpfn = 0;
91                 bo->placements[i].lpfn = 0;
92         }
93 }
94
95 static void
96 hibmc_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
97 {
98         struct hibmc_bo *hibmcbo = hibmc_bo(bo);
99
100         if (!hibmc_ttm_bo_is_hibmc_bo(bo))
101                 return;
102
103         hibmc_ttm_placement(hibmcbo, TTM_PL_FLAG_SYSTEM);
104         *pl = hibmcbo->placement;
105 }
106
107 static int hibmc_bo_verify_access(struct ttm_buffer_object *bo,
108                                   struct file *filp)
109 {
110         struct hibmc_bo *hibmcbo = hibmc_bo(bo);
111
112         return drm_vma_node_verify_access(&hibmcbo->gem.vma_node,
113                                           filp->private_data);
114 }
115
116 static int hibmc_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
117                                     struct ttm_mem_reg *mem)
118 {
119         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
120         struct hibmc_drm_private *hibmc = hibmc_bdev(bdev);
121
122         mem->bus.addr = NULL;
123         mem->bus.offset = 0;
124         mem->bus.size = mem->num_pages << PAGE_SHIFT;
125         mem->bus.base = 0;
126         mem->bus.is_iomem = false;
127         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
128                 return -EINVAL;
129         switch (mem->mem_type) {
130         case TTM_PL_SYSTEM:
131                 /* system memory */
132                 return 0;
133         case TTM_PL_VRAM:
134                 mem->bus.offset = mem->start << PAGE_SHIFT;
135                 mem->bus.base = pci_resource_start(hibmc->dev->pdev, 0);
136                 mem->bus.is_iomem = true;
137                 break;
138         default:
139                 return -EINVAL;
140         }
141         return 0;
142 }
143
144 static void hibmc_ttm_backend_destroy(struct ttm_tt *tt)
145 {
146         ttm_tt_fini(tt);
147         kfree(tt);
148 }
149
150 static struct ttm_backend_func hibmc_tt_backend_func = {
151         .destroy = &hibmc_ttm_backend_destroy,
152 };
153
154 static struct ttm_tt *hibmc_ttm_tt_create(struct ttm_buffer_object *bo,
155                                           u32 page_flags)
156 {
157         struct ttm_tt *tt;
158         int ret;
159
160         tt = kzalloc(sizeof(*tt), GFP_KERNEL);
161         if (!tt) {
162                 DRM_ERROR("failed to allocate ttm_tt\n");
163                 return NULL;
164         }
165         tt->func = &hibmc_tt_backend_func;
166         ret = ttm_tt_init(tt, bo, page_flags);
167         if (ret) {
168                 DRM_ERROR("failed to initialize ttm_tt: %d\n", ret);
169                 kfree(tt);
170                 return NULL;
171         }
172         return tt;
173 }
174
175 struct ttm_bo_driver hibmc_bo_driver = {
176         .ttm_tt_create          = hibmc_ttm_tt_create,
177         .init_mem_type          = hibmc_bo_init_mem_type,
178         .evict_flags            = hibmc_bo_evict_flags,
179         .move                   = NULL,
180         .verify_access          = hibmc_bo_verify_access,
181         .io_mem_reserve         = &hibmc_ttm_io_mem_reserve,
182         .io_mem_free            = NULL,
183 };
184
185 int hibmc_mm_init(struct hibmc_drm_private *hibmc)
186 {
187         int ret;
188         struct drm_device *dev = hibmc->dev;
189         struct ttm_bo_device *bdev = &hibmc->bdev;
190
191         ret = ttm_bo_device_init(&hibmc->bdev,
192                                  &hibmc_bo_driver,
193                                  dev->anon_inode->i_mapping,
194                                  DRM_FILE_PAGE_OFFSET,
195                                  true);
196         if (ret) {
197                 DRM_ERROR("error initializing bo driver: %d\n", ret);
198                 return ret;
199         }
200
201         ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
202                              hibmc->fb_size >> PAGE_SHIFT);
203         if (ret) {
204                 DRM_ERROR("failed ttm VRAM init: %d\n", ret);
205                 return ret;
206         }
207
208         hibmc->mm_inited = true;
209         return 0;
210 }
211
212 void hibmc_mm_fini(struct hibmc_drm_private *hibmc)
213 {
214         if (!hibmc->mm_inited)
215                 return;
216
217         ttm_bo_device_release(&hibmc->bdev);
218         hibmc->mm_inited = false;
219 }
220
221 static void hibmc_bo_unref(struct hibmc_bo **bo)
222 {
223         struct ttm_buffer_object *tbo;
224
225         if ((*bo) == NULL)
226                 return;
227
228         tbo = &((*bo)->bo);
229         ttm_bo_put(tbo);
230         *bo = NULL;
231 }
232
233 int hibmc_bo_create(struct drm_device *dev, int size, int align,
234                     u32 flags, struct hibmc_bo **phibmcbo)
235 {
236         struct hibmc_drm_private *hibmc = dev->dev_private;
237         struct hibmc_bo *hibmcbo;
238         size_t acc_size;
239         int ret;
240
241         hibmcbo = kzalloc(sizeof(*hibmcbo), GFP_KERNEL);
242         if (!hibmcbo) {
243                 DRM_ERROR("failed to allocate hibmcbo\n");
244                 return -ENOMEM;
245         }
246         ret = drm_gem_object_init(dev, &hibmcbo->gem, size);
247         if (ret) {
248                 DRM_ERROR("failed to initialize drm gem object: %d\n", ret);
249                 kfree(hibmcbo);
250                 return ret;
251         }
252
253         hibmcbo->bo.bdev = &hibmc->bdev;
254
255         hibmc_ttm_placement(hibmcbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
256
257         acc_size = ttm_bo_dma_acc_size(&hibmc->bdev, size,
258                                        sizeof(struct hibmc_bo));
259
260         ret = ttm_bo_init(&hibmc->bdev, &hibmcbo->bo, size,
261                           ttm_bo_type_device, &hibmcbo->placement,
262                           align >> PAGE_SHIFT, false, acc_size,
263                           NULL, NULL, hibmc_bo_ttm_destroy);
264         if (ret) {
265                 hibmc_bo_unref(&hibmcbo);
266                 DRM_ERROR("failed to initialize ttm_bo: %d\n", ret);
267                 return ret;
268         }
269
270         *phibmcbo = hibmcbo;
271         return 0;
272 }
273
274 int hibmc_bo_pin(struct hibmc_bo *bo, u32 pl_flag, u64 *gpu_addr)
275 {
276         struct ttm_operation_ctx ctx = { false, false };
277         int i, ret;
278
279         if (bo->pin_count) {
280                 bo->pin_count++;
281                 if (gpu_addr)
282                         *gpu_addr = bo->bo.offset;
283                 return 0;
284         }
285
286         hibmc_ttm_placement(bo, pl_flag);
287         for (i = 0; i < bo->placement.num_placement; i++)
288                 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
289         ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
290         if (ret)
291                 return ret;
292
293         bo->pin_count = 1;
294         if (gpu_addr)
295                 *gpu_addr = bo->bo.offset;
296         return 0;
297 }
298
299 int hibmc_bo_unpin(struct hibmc_bo *bo)
300 {
301         struct ttm_operation_ctx ctx = { false, false };
302         int i, ret;
303
304         if (!bo->pin_count) {
305                 DRM_ERROR("unpin bad %p\n", bo);
306                 return 0;
307         }
308         bo->pin_count--;
309         if (bo->pin_count)
310                 return 0;
311
312         for (i = 0; i < bo->placement.num_placement ; i++)
313                 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
314         ret = ttm_bo_validate(&bo->bo, &bo->placement, &ctx);
315         if (ret) {
316                 DRM_ERROR("validate failed for unpin: %d\n", ret);
317                 return ret;
318         }
319
320         return 0;
321 }
322
323 int hibmc_mmap(struct file *filp, struct vm_area_struct *vma)
324 {
325         struct drm_file *file_priv;
326         struct hibmc_drm_private *hibmc;
327
328         if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
329                 return -EINVAL;
330
331         file_priv = filp->private_data;
332         hibmc = file_priv->minor->dev->dev_private;
333         return ttm_bo_mmap(filp, vma, &hibmc->bdev);
334 }
335
336 int hibmc_gem_create(struct drm_device *dev, u32 size, bool iskernel,
337                      struct drm_gem_object **obj)
338 {
339         struct hibmc_bo *hibmcbo;
340         int ret;
341
342         *obj = NULL;
343
344         size = PAGE_ALIGN(size);
345         if (size == 0) {
346                 DRM_ERROR("error: zero size\n");
347                 return -EINVAL;
348         }
349
350         ret = hibmc_bo_create(dev, size, 0, 0, &hibmcbo);
351         if (ret) {
352                 if (ret != -ERESTARTSYS)
353                         DRM_ERROR("failed to allocate GEM object: %d\n", ret);
354                 return ret;
355         }
356         *obj = &hibmcbo->gem;
357         return 0;
358 }
359
360 int hibmc_dumb_create(struct drm_file *file, struct drm_device *dev,
361                       struct drm_mode_create_dumb *args)
362 {
363         struct drm_gem_object *gobj;
364         u32 handle;
365         int ret;
366
367         args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 16);
368         args->size = args->pitch * args->height;
369
370         ret = hibmc_gem_create(dev, args->size, false,
371                                &gobj);
372         if (ret) {
373                 DRM_ERROR("failed to create GEM object: %d\n", ret);
374                 return ret;
375         }
376
377         ret = drm_gem_handle_create(file, gobj, &handle);
378         drm_gem_object_put_unlocked(gobj);
379         if (ret) {
380                 DRM_ERROR("failed to unreference GEM object: %d\n", ret);
381                 return ret;
382         }
383
384         args->handle = handle;
385         return 0;
386 }
387
388 void hibmc_gem_free_object(struct drm_gem_object *obj)
389 {
390         struct hibmc_bo *hibmcbo = gem_to_hibmc_bo(obj);
391
392         hibmc_bo_unref(&hibmcbo);
393 }
394
395 static u64 hibmc_bo_mmap_offset(struct hibmc_bo *bo)
396 {
397         return drm_vma_node_offset_addr(&bo->bo.vma_node);
398 }
399
400 int hibmc_dumb_mmap_offset(struct drm_file *file, struct drm_device *dev,
401                            u32 handle, u64 *offset)
402 {
403         struct drm_gem_object *obj;
404         struct hibmc_bo *bo;
405
406         obj = drm_gem_object_lookup(file, handle);
407         if (!obj)
408                 return -ENOENT;
409
410         bo = gem_to_hibmc_bo(obj);
411         *offset = hibmc_bo_mmap_offset(bo);
412
413         drm_gem_object_put_unlocked(obj);
414         return 0;
415 }
416
417 static void hibmc_user_framebuffer_destroy(struct drm_framebuffer *fb)
418 {
419         struct hibmc_framebuffer *hibmc_fb = to_hibmc_framebuffer(fb);
420
421         drm_gem_object_put_unlocked(hibmc_fb->obj);
422         drm_framebuffer_cleanup(fb);
423         kfree(hibmc_fb);
424 }
425
426 static const struct drm_framebuffer_funcs hibmc_fb_funcs = {
427         .destroy = hibmc_user_framebuffer_destroy,
428 };
429
430 struct hibmc_framebuffer *
431 hibmc_framebuffer_init(struct drm_device *dev,
432                        const struct drm_mode_fb_cmd2 *mode_cmd,
433                        struct drm_gem_object *obj)
434 {
435         struct hibmc_framebuffer *hibmc_fb;
436         int ret;
437
438         hibmc_fb = kzalloc(sizeof(*hibmc_fb), GFP_KERNEL);
439         if (!hibmc_fb) {
440                 DRM_ERROR("failed to allocate hibmc_fb\n");
441                 return ERR_PTR(-ENOMEM);
442         }
443
444         drm_helper_mode_fill_fb_struct(dev, &hibmc_fb->fb, mode_cmd);
445         hibmc_fb->obj = obj;
446         ret = drm_framebuffer_init(dev, &hibmc_fb->fb, &hibmc_fb_funcs);
447         if (ret) {
448                 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
449                 kfree(hibmc_fb);
450                 return ERR_PTR(ret);
451         }
452
453         return hibmc_fb;
454 }
455
456 static struct drm_framebuffer *
457 hibmc_user_framebuffer_create(struct drm_device *dev,
458                               struct drm_file *filp,
459                               const struct drm_mode_fb_cmd2 *mode_cmd)
460 {
461         struct drm_gem_object *obj;
462         struct hibmc_framebuffer *hibmc_fb;
463
464         DRM_DEBUG_DRIVER("%dx%d, format %c%c%c%c\n",
465                          mode_cmd->width, mode_cmd->height,
466                          (mode_cmd->pixel_format) & 0xff,
467                          (mode_cmd->pixel_format >> 8)  & 0xff,
468                          (mode_cmd->pixel_format >> 16) & 0xff,
469                          (mode_cmd->pixel_format >> 24) & 0xff);
470
471         obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
472         if (!obj)
473                 return ERR_PTR(-ENOENT);
474
475         hibmc_fb = hibmc_framebuffer_init(dev, mode_cmd, obj);
476         if (IS_ERR(hibmc_fb)) {
477                 drm_gem_object_put_unlocked(obj);
478                 return ERR_PTR((long)hibmc_fb);
479         }
480         return &hibmc_fb->fb;
481 }
482
483 const struct drm_mode_config_funcs hibmc_mode_funcs = {
484         .atomic_check = drm_atomic_helper_check,
485         .atomic_commit = drm_atomic_helper_commit,
486         .fb_create = hibmc_user_framebuffer_create,
487 };