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.
10 static void bochs_ttm_placement(struct bochs_bo *bo, int domain);
12 /* ---------------------------------------------------------------------- */
14 static inline struct bochs_device *bochs_bdev(struct ttm_bo_device *bd)
16 return container_of(bd, struct bochs_device, ttm.bdev);
19 static void bochs_bo_ttm_destroy(struct ttm_buffer_object *tbo)
23 bo = container_of(tbo, struct bochs_bo, bo);
24 drm_gem_object_release(&bo->gem);
28 static bool bochs_ttm_bo_is_bochs_bo(struct ttm_buffer_object *bo)
30 if (bo->destroy == &bochs_bo_ttm_destroy)
35 static int bochs_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
36 struct ttm_mem_type_manager *man)
40 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
41 man->available_caching = TTM_PL_MASK_CACHING;
42 man->default_caching = TTM_PL_FLAG_CACHED;
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 |
50 man->default_caching = TTM_PL_FLAG_WC;
53 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
60 bochs_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
62 struct bochs_bo *bochsbo = bochs_bo(bo);
64 if (!bochs_ttm_bo_is_bochs_bo(bo))
67 bochs_ttm_placement(bochsbo, TTM_PL_FLAG_SYSTEM);
68 *pl = bochsbo->placement;
71 static int bochs_bo_verify_access(struct ttm_buffer_object *bo,
74 struct bochs_bo *bochsbo = bochs_bo(bo);
76 return drm_vma_node_verify_access(&bochsbo->gem.vma_node,
80 static int bochs_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
81 struct ttm_mem_reg *mem)
83 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
84 struct bochs_device *bochs = bochs_bdev(bdev);
88 mem->bus.size = mem->num_pages << PAGE_SHIFT;
90 mem->bus.is_iomem = false;
91 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
93 switch (mem->mem_type) {
98 mem->bus.offset = mem->start << PAGE_SHIFT;
99 mem->bus.base = bochs->fb_base;
100 mem->bus.is_iomem = true;
109 static void bochs_ttm_io_mem_free(struct ttm_bo_device *bdev,
110 struct ttm_mem_reg *mem)
114 static void bochs_ttm_backend_destroy(struct ttm_tt *tt)
120 static struct ttm_backend_func bochs_tt_backend_func = {
121 .destroy = &bochs_ttm_backend_destroy,
124 static struct ttm_tt *bochs_ttm_tt_create(struct ttm_buffer_object *bo,
129 tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
132 tt->func = &bochs_tt_backend_func;
133 if (ttm_tt_init(tt, bo, page_flags)) {
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,
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,
151 int bochs_mm_init(struct bochs_device *bochs)
153 struct ttm_bo_device *bdev = &bochs->ttm.bdev;
156 ret = ttm_bo_device_init(&bochs->ttm.bdev,
158 bochs->dev->anon_inode->i_mapping,
159 DRM_FILE_PAGE_OFFSET,
162 DRM_ERROR("Error initialising bo driver; %d\n", ret);
166 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
167 bochs->fb_size >> PAGE_SHIFT);
169 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
173 bochs->ttm.initialized = true;
177 void bochs_mm_fini(struct bochs_device *bochs)
179 if (!bochs->ttm.initialized)
182 ttm_bo_device_release(&bochs->ttm.bdev);
183 bochs->ttm.initialized = false;
186 static void bochs_ttm_placement(struct bochs_bo *bo, int domain)
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
197 if (domain & TTM_PL_FLAG_SYSTEM) {
198 bo->placements[c++].flags = TTM_PL_MASK_CACHING
199 | TTM_PL_FLAG_SYSTEM;
202 bo->placements[c++].flags = TTM_PL_MASK_CACHING
203 | TTM_PL_FLAG_SYSTEM;
205 for (i = 0; i < c; ++i) {
206 bo->placements[i].fpfn = 0;
207 bo->placements[i].lpfn = 0;
209 bo->placement.num_placement = c;
210 bo->placement.num_busy_placement = c;
213 static inline u64 bochs_bo_gpu_offset(struct bochs_bo *bo)
215 return bo->bo.offset;
218 int bochs_bo_pin(struct bochs_bo *bo, u32 pl_flag, u64 *gpu_addr)
220 struct ttm_operation_ctx ctx = { false, false };
226 *gpu_addr = bochs_bo_gpu_offset(bo);
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);
239 *gpu_addr = bochs_bo_gpu_offset(bo);
243 int bochs_bo_unpin(struct bochs_bo *bo)
245 struct ttm_operation_ctx ctx = { false, false };
248 if (!bo->pin_count) {
249 DRM_ERROR("unpin bad %p\n", bo);
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);
266 int bochs_mmap(struct file *filp, struct vm_area_struct *vma)
268 struct drm_file *file_priv;
269 struct bochs_device *bochs;
271 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
274 file_priv = filp->private_data;
275 bochs = file_priv->minor->dev->dev_private;
276 return ttm_bo_mmap(filp, vma, &bochs->ttm.bdev);
279 /* ---------------------------------------------------------------------- */
281 static int bochs_bo_create(struct drm_device *dev, int size, int align,
282 uint32_t flags, struct bochs_bo **pbochsbo)
284 struct bochs_device *bochs = dev->dev_private;
285 struct bochs_bo *bochsbo;
289 bochsbo = kzalloc(sizeof(struct bochs_bo), GFP_KERNEL);
293 ret = drm_gem_object_init(dev, &bochsbo->gem, size);
299 bochsbo->bo.bdev = &bochs->ttm.bdev;
300 bochsbo->bo.bdev->dev_mapping = dev->anon_inode->i_mapping;
302 bochs_ttm_placement(bochsbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
304 acc_size = ttm_bo_dma_acc_size(&bochs->ttm.bdev, size,
305 sizeof(struct bochs_bo));
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);
318 int bochs_gem_create(struct drm_device *dev, u32 size, bool iskernel,
319 struct drm_gem_object **obj)
321 struct bochs_bo *bochsbo;
326 size = PAGE_ALIGN(size);
330 ret = bochs_bo_create(dev, size, 0, 0, &bochsbo);
332 if (ret != -ERESTARTSYS)
333 DRM_ERROR("failed to allocate GEM object\n");
336 *obj = &bochsbo->gem;
340 int bochs_dumb_create(struct drm_file *file, struct drm_device *dev,
341 struct drm_mode_create_dumb *args)
343 struct drm_gem_object *gobj;
347 args->pitch = args->width * ((args->bpp + 7) / 8);
348 args->size = args->pitch * args->height;
350 ret = bochs_gem_create(dev, args->size, false,
355 ret = drm_gem_handle_create(file, gobj, &handle);
356 drm_gem_object_put_unlocked(gobj);
360 args->handle = handle;
364 static void bochs_bo_unref(struct bochs_bo **bo)
366 struct ttm_buffer_object *tbo;
376 void bochs_gem_free_object(struct drm_gem_object *obj)
378 struct bochs_bo *bochs_bo = gem_to_bochs_bo(obj);
380 bochs_bo_unref(&bochs_bo);
383 int bochs_dumb_mmap_offset(struct drm_file *file, struct drm_device *dev,
384 uint32_t handle, uint64_t *offset)
386 struct drm_gem_object *obj;
389 obj = drm_gem_object_lookup(file, handle);
393 bo = gem_to_bochs_bo(obj);
394 *offset = bochs_bo_mmap_offset(bo);
396 drm_gem_object_put_unlocked(obj);