OSDN Git Service

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