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 int bochs_ttm_mem_global_init(struct drm_global_reference
*ref
)
21 return ttm_mem_global_init(ref
->object
);
24 static void bochs_ttm_mem_global_release(struct drm_global_reference
*ref
)
26 ttm_mem_global_release(ref
->object
);
29 static int bochs_ttm_global_init(struct bochs_device
*bochs
)
31 struct drm_global_reference
*global_ref
;
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
);
41 DRM_ERROR("Failed setting up TTM memory accounting "
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
);
55 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
56 drm_global_item_unref(&bochs
->ttm
.mem_global_ref
);
63 static void bochs_ttm_global_release(struct bochs_device
*bochs
)
65 if (bochs
->ttm
.mem_global_ref
.release
== NULL
)
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
;
74 static void bochs_bo_ttm_destroy(struct ttm_buffer_object
*tbo
)
78 bo
= container_of(tbo
, struct bochs_bo
, bo
);
79 drm_gem_object_release(&bo
->gem
);
83 static bool bochs_ttm_bo_is_bochs_bo(struct ttm_buffer_object
*bo
)
85 if (bo
->destroy
== &bochs_bo_ttm_destroy
)
90 static int bochs_bo_init_mem_type(struct ttm_bo_device
*bdev
, uint32_t type
,
91 struct ttm_mem_type_manager
*man
)
95 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
;
96 man
->available_caching
= TTM_PL_MASK_CACHING
;
97 man
->default_caching
= TTM_PL_FLAG_CACHED
;
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
|
105 man
->default_caching
= TTM_PL_FLAG_WC
;
108 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type
);
115 bochs_bo_evict_flags(struct ttm_buffer_object
*bo
, struct ttm_placement
*pl
)
117 struct bochs_bo
*bochsbo
= bochs_bo(bo
);
119 if (!bochs_ttm_bo_is_bochs_bo(bo
))
122 bochs_ttm_placement(bochsbo
, TTM_PL_FLAG_SYSTEM
);
123 *pl
= bochsbo
->placement
;
126 static int bochs_bo_verify_access(struct ttm_buffer_object
*bo
,
129 struct bochs_bo
*bochsbo
= bochs_bo(bo
);
131 return drm_vma_node_verify_access(&bochsbo
->gem
.vma_node
, filp
);
134 static int bochs_ttm_io_mem_reserve(struct ttm_bo_device
*bdev
,
135 struct ttm_mem_reg
*mem
)
137 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
138 struct bochs_device
*bochs
= bochs_bdev(bdev
);
140 mem
->bus
.addr
= NULL
;
142 mem
->bus
.size
= mem
->num_pages
<< PAGE_SHIFT
;
144 mem
->bus
.is_iomem
= false;
145 if (!(man
->flags
& TTM_MEMTYPE_FLAG_MAPPABLE
))
147 switch (mem
->mem_type
) {
152 mem
->bus
.offset
= mem
->start
<< PAGE_SHIFT
;
153 mem
->bus
.base
= bochs
->fb_base
;
154 mem
->bus
.is_iomem
= true;
163 static void bochs_ttm_io_mem_free(struct ttm_bo_device
*bdev
,
164 struct ttm_mem_reg
*mem
)
168 static int bochs_bo_move(struct ttm_buffer_object
*bo
,
169 bool evict
, bool interruptible
,
171 struct ttm_mem_reg
*new_mem
)
173 return ttm_bo_move_memcpy(bo
, evict
, no_wait_gpu
, new_mem
);
177 static void bochs_ttm_backend_destroy(struct ttm_tt
*tt
)
183 static struct ttm_backend_func bochs_tt_backend_func
= {
184 .destroy
= &bochs_ttm_backend_destroy
,
187 static struct ttm_tt
*bochs_ttm_tt_create(struct ttm_bo_device
*bdev
,
190 struct page
*dummy_read_page
)
194 tt
= kzalloc(sizeof(struct ttm_tt
), GFP_KERNEL
);
197 tt
->func
= &bochs_tt_backend_func
;
198 if (ttm_tt_init(tt
, bdev
, size
, page_flags
, dummy_read_page
)) {
205 struct ttm_bo_driver bochs_bo_driver
= {
206 .ttm_tt_create
= bochs_ttm_tt_create
,
207 .ttm_tt_populate
= ttm_pool_populate
,
208 .ttm_tt_unpopulate
= ttm_pool_unpopulate
,
209 .init_mem_type
= bochs_bo_init_mem_type
,
210 .evict_flags
= bochs_bo_evict_flags
,
211 .move
= bochs_bo_move
,
212 .verify_access
= bochs_bo_verify_access
,
213 .io_mem_reserve
= &bochs_ttm_io_mem_reserve
,
214 .io_mem_free
= &bochs_ttm_io_mem_free
,
217 int bochs_mm_init(struct bochs_device
*bochs
)
219 struct ttm_bo_device
*bdev
= &bochs
->ttm
.bdev
;
222 ret
= bochs_ttm_global_init(bochs
);
226 ret
= ttm_bo_device_init(&bochs
->ttm
.bdev
,
227 bochs
->ttm
.bo_global_ref
.ref
.object
,
229 bochs
->dev
->anon_inode
->i_mapping
,
230 DRM_FILE_PAGE_OFFSET
,
233 DRM_ERROR("Error initialising bo driver; %d\n", ret
);
237 ret
= ttm_bo_init_mm(bdev
, TTM_PL_VRAM
,
238 bochs
->fb_size
>> PAGE_SHIFT
);
240 DRM_ERROR("Failed ttm VRAM init: %d\n", ret
);
244 bochs
->ttm
.initialized
= true;
248 void bochs_mm_fini(struct bochs_device
*bochs
)
250 if (!bochs
->ttm
.initialized
)
253 ttm_bo_device_release(&bochs
->ttm
.bdev
);
254 bochs_ttm_global_release(bochs
);
255 bochs
->ttm
.initialized
= false;
258 static void bochs_ttm_placement(struct bochs_bo
*bo
, int domain
)
262 bo
->placement
.placement
= bo
->placements
;
263 bo
->placement
.busy_placement
= bo
->placements
;
264 if (domain
& TTM_PL_FLAG_VRAM
) {
265 bo
->placements
[c
++].flags
= TTM_PL_FLAG_WC
266 | TTM_PL_FLAG_UNCACHED
269 if (domain
& TTM_PL_FLAG_SYSTEM
) {
270 bo
->placements
[c
++].flags
= TTM_PL_MASK_CACHING
271 | TTM_PL_FLAG_SYSTEM
;
274 bo
->placements
[c
++].flags
= TTM_PL_MASK_CACHING
275 | TTM_PL_FLAG_SYSTEM
;
277 for (i
= 0; i
< c
; ++i
) {
278 bo
->placements
[i
].fpfn
= 0;
279 bo
->placements
[i
].lpfn
= 0;
281 bo
->placement
.num_placement
= c
;
282 bo
->placement
.num_busy_placement
= c
;
285 static inline u64
bochs_bo_gpu_offset(struct bochs_bo
*bo
)
287 return bo
->bo
.offset
;
290 int bochs_bo_pin(struct bochs_bo
*bo
, u32 pl_flag
, u64
*gpu_addr
)
297 *gpu_addr
= bochs_bo_gpu_offset(bo
);
301 bochs_ttm_placement(bo
, pl_flag
);
302 for (i
= 0; i
< bo
->placement
.num_placement
; i
++)
303 bo
->placements
[i
].flags
|= TTM_PL_FLAG_NO_EVICT
;
304 ret
= ttm_bo_validate(&bo
->bo
, &bo
->placement
, false, false);
310 *gpu_addr
= bochs_bo_gpu_offset(bo
);
314 int bochs_bo_unpin(struct bochs_bo
*bo
)
318 if (!bo
->pin_count
) {
319 DRM_ERROR("unpin bad %p\n", bo
);
327 for (i
= 0; i
< bo
->placement
.num_placement
; i
++)
328 bo
->placements
[i
].flags
&= ~TTM_PL_FLAG_NO_EVICT
;
329 ret
= ttm_bo_validate(&bo
->bo
, &bo
->placement
, false, false);
336 int bochs_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
338 struct drm_file
*file_priv
;
339 struct bochs_device
*bochs
;
341 if (unlikely(vma
->vm_pgoff
< DRM_FILE_PAGE_OFFSET
))
344 file_priv
= filp
->private_data
;
345 bochs
= file_priv
->minor
->dev
->dev_private
;
346 return ttm_bo_mmap(filp
, vma
, &bochs
->ttm
.bdev
);
349 /* ---------------------------------------------------------------------- */
351 static int bochs_bo_create(struct drm_device
*dev
, int size
, int align
,
352 uint32_t flags
, struct bochs_bo
**pbochsbo
)
354 struct bochs_device
*bochs
= dev
->dev_private
;
355 struct bochs_bo
*bochsbo
;
359 bochsbo
= kzalloc(sizeof(struct bochs_bo
), GFP_KERNEL
);
363 ret
= drm_gem_object_init(dev
, &bochsbo
->gem
, size
);
369 bochsbo
->bo
.bdev
= &bochs
->ttm
.bdev
;
370 bochsbo
->bo
.bdev
->dev_mapping
= dev
->anon_inode
->i_mapping
;
372 bochs_ttm_placement(bochsbo
, TTM_PL_FLAG_VRAM
| TTM_PL_FLAG_SYSTEM
);
374 acc_size
= ttm_bo_dma_acc_size(&bochs
->ttm
.bdev
, size
,
375 sizeof(struct bochs_bo
));
377 ret
= ttm_bo_init(&bochs
->ttm
.bdev
, &bochsbo
->bo
, size
,
378 ttm_bo_type_device
, &bochsbo
->placement
,
379 align
>> PAGE_SHIFT
, false, NULL
, acc_size
,
380 NULL
, NULL
, bochs_bo_ttm_destroy
);
388 int bochs_gem_create(struct drm_device
*dev
, u32 size
, bool iskernel
,
389 struct drm_gem_object
**obj
)
391 struct bochs_bo
*bochsbo
;
396 size
= PAGE_ALIGN(size
);
400 ret
= bochs_bo_create(dev
, size
, 0, 0, &bochsbo
);
402 if (ret
!= -ERESTARTSYS
)
403 DRM_ERROR("failed to allocate GEM object\n");
406 *obj
= &bochsbo
->gem
;
410 int bochs_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
411 struct drm_mode_create_dumb
*args
)
413 struct drm_gem_object
*gobj
;
417 args
->pitch
= args
->width
* ((args
->bpp
+ 7) / 8);
418 args
->size
= args
->pitch
* args
->height
;
420 ret
= bochs_gem_create(dev
, args
->size
, false,
425 ret
= drm_gem_handle_create(file
, gobj
, &handle
);
426 drm_gem_object_unreference_unlocked(gobj
);
430 args
->handle
= handle
;
434 static void bochs_bo_unref(struct bochs_bo
**bo
)
436 struct ttm_buffer_object
*tbo
;
446 void bochs_gem_free_object(struct drm_gem_object
*obj
)
448 struct bochs_bo
*bochs_bo
= gem_to_bochs_bo(obj
);
450 bochs_bo_unref(&bochs_bo
);
453 int bochs_dumb_mmap_offset(struct drm_file
*file
, struct drm_device
*dev
,
454 uint32_t handle
, uint64_t *offset
)
456 struct drm_gem_object
*obj
;
460 mutex_lock(&dev
->struct_mutex
);
461 obj
= drm_gem_object_lookup(dev
, file
, handle
);
467 bo
= gem_to_bochs_bo(obj
);
468 *offset
= bochs_bo_mmap_offset(bo
);
470 drm_gem_object_unreference(obj
);
473 mutex_unlock(&dev
->struct_mutex
);
478 /* ---------------------------------------------------------------------- */
480 static void bochs_user_framebuffer_destroy(struct drm_framebuffer
*fb
)
482 struct bochs_framebuffer
*bochs_fb
= to_bochs_framebuffer(fb
);
484 drm_gem_object_unreference_unlocked(bochs_fb
->obj
);
485 drm_framebuffer_cleanup(fb
);
489 static const struct drm_framebuffer_funcs bochs_fb_funcs
= {
490 .destroy
= bochs_user_framebuffer_destroy
,
493 int bochs_framebuffer_init(struct drm_device
*dev
,
494 struct bochs_framebuffer
*gfb
,
495 struct drm_mode_fb_cmd2
*mode_cmd
,
496 struct drm_gem_object
*obj
)
500 drm_helper_mode_fill_fb_struct(&gfb
->base
, mode_cmd
);
502 ret
= drm_framebuffer_init(dev
, &gfb
->base
, &bochs_fb_funcs
);
504 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret
);
510 static struct drm_framebuffer
*
511 bochs_user_framebuffer_create(struct drm_device
*dev
,
512 struct drm_file
*filp
,
513 struct drm_mode_fb_cmd2
*mode_cmd
)
515 struct drm_gem_object
*obj
;
516 struct bochs_framebuffer
*bochs_fb
;
519 DRM_DEBUG_DRIVER("%dx%d, format %c%c%c%c\n",
520 mode_cmd
->width
, mode_cmd
->height
,
521 (mode_cmd
->pixel_format
) & 0xff,
522 (mode_cmd
->pixel_format
>> 8) & 0xff,
523 (mode_cmd
->pixel_format
>> 16) & 0xff,
524 (mode_cmd
->pixel_format
>> 24) & 0xff);
526 if (mode_cmd
->pixel_format
!= DRM_FORMAT_XRGB8888
)
527 return ERR_PTR(-ENOENT
);
529 obj
= drm_gem_object_lookup(dev
, filp
, mode_cmd
->handles
[0]);
531 return ERR_PTR(-ENOENT
);
533 bochs_fb
= kzalloc(sizeof(*bochs_fb
), GFP_KERNEL
);
535 drm_gem_object_unreference_unlocked(obj
);
536 return ERR_PTR(-ENOMEM
);
539 ret
= bochs_framebuffer_init(dev
, bochs_fb
, mode_cmd
, obj
);
541 drm_gem_object_unreference_unlocked(obj
);
545 return &bochs_fb
->base
;
548 const struct drm_mode_config_funcs bochs_mode_funcs
= {
549 .fb_create
= bochs_user_framebuffer_create
,