Commit | Line | Data |
---|---|---|
0a6659bd GH |
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 int bochs_bo_move(struct ttm_buffer_object *bo, | |
169 | bool evict, bool interruptible, | |
170 | bool no_wait_gpu, | |
171 | struct ttm_mem_reg *new_mem) | |
172 | { | |
173 | return ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem); | |
174 | } | |
175 | ||
176 | ||
177 | static void bochs_ttm_backend_destroy(struct ttm_tt *tt) | |
178 | { | |
179 | ttm_tt_fini(tt); | |
180 | kfree(tt); | |
181 | } | |
182 | ||
183 | static struct ttm_backend_func bochs_tt_backend_func = { | |
184 | .destroy = &bochs_ttm_backend_destroy, | |
185 | }; | |
186 | ||
187 | static struct ttm_tt *bochs_ttm_tt_create(struct ttm_bo_device *bdev, | |
188 | unsigned long size, | |
189 | uint32_t page_flags, | |
190 | struct page *dummy_read_page) | |
191 | { | |
192 | struct ttm_tt *tt; | |
193 | ||
194 | tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL); | |
195 | if (tt == NULL) | |
196 | return NULL; | |
197 | tt->func = &bochs_tt_backend_func; | |
198 | if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) { | |
199 | kfree(tt); | |
200 | return NULL; | |
201 | } | |
202 | return tt; | |
203 | } | |
204 | ||
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, | |
215 | }; | |
216 | ||
217 | int bochs_mm_init(struct bochs_device *bochs) | |
218 | { | |
219 | struct ttm_bo_device *bdev = &bochs->ttm.bdev; | |
220 | int ret; | |
221 | ||
222 | ret = bochs_ttm_global_init(bochs); | |
223 | if (ret) | |
224 | return ret; | |
225 | ||
226 | ret = ttm_bo_device_init(&bochs->ttm.bdev, | |
227 | bochs->ttm.bo_global_ref.ref.object, | |
228 | &bochs_bo_driver, DRM_FILE_PAGE_OFFSET, | |
229 | true); | |
230 | if (ret) { | |
231 | DRM_ERROR("Error initialising bo driver; %d\n", ret); | |
232 | return ret; | |
233 | } | |
234 | ||
235 | ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM, | |
236 | bochs->fb_size >> PAGE_SHIFT); | |
237 | if (ret) { | |
238 | DRM_ERROR("Failed ttm VRAM init: %d\n", ret); | |
239 | return ret; | |
240 | } | |
241 | ||
242 | bochs->ttm.initialized = true; | |
243 | return 0; | |
244 | } | |
245 | ||
246 | void bochs_mm_fini(struct bochs_device *bochs) | |
247 | { | |
248 | if (!bochs->ttm.initialized) | |
249 | return; | |
250 | ||
251 | ttm_bo_device_release(&bochs->ttm.bdev); | |
252 | bochs_ttm_global_release(bochs); | |
253 | bochs->ttm.initialized = false; | |
254 | } | |
255 | ||
256 | static void bochs_ttm_placement(struct bochs_bo *bo, int domain) | |
257 | { | |
258 | u32 c = 0; | |
259 | bo->placement.fpfn = 0; | |
260 | bo->placement.lpfn = 0; | |
261 | bo->placement.placement = bo->placements; | |
262 | bo->placement.busy_placement = bo->placements; | |
263 | if (domain & TTM_PL_FLAG_VRAM) { | |
264 | bo->placements[c++] = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | |
265 | | TTM_PL_FLAG_VRAM; | |
266 | } | |
267 | if (domain & TTM_PL_FLAG_SYSTEM) { | |
268 | bo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM; | |
269 | } | |
270 | if (!c) { | |
271 | bo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM; | |
272 | } | |
273 | bo->placement.num_placement = c; | |
274 | bo->placement.num_busy_placement = c; | |
275 | } | |
276 | ||
277 | static inline u64 bochs_bo_gpu_offset(struct bochs_bo *bo) | |
278 | { | |
279 | return bo->bo.offset; | |
280 | } | |
281 | ||
282 | int bochs_bo_pin(struct bochs_bo *bo, u32 pl_flag, u64 *gpu_addr) | |
283 | { | |
284 | int i, ret; | |
285 | ||
286 | if (bo->pin_count) { | |
287 | bo->pin_count++; | |
288 | if (gpu_addr) | |
289 | *gpu_addr = bochs_bo_gpu_offset(bo); | |
290 | return 0; | |
291 | } | |
292 | ||
293 | bochs_ttm_placement(bo, pl_flag); | |
294 | for (i = 0; i < bo->placement.num_placement; i++) | |
295 | bo->placements[i] |= TTM_PL_FLAG_NO_EVICT; | |
296 | ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false); | |
297 | if (ret) | |
298 | return ret; | |
299 | ||
300 | bo->pin_count = 1; | |
301 | if (gpu_addr) | |
302 | *gpu_addr = bochs_bo_gpu_offset(bo); | |
303 | return 0; | |
304 | } | |
305 | ||
306 | int bochs_bo_unpin(struct bochs_bo *bo) | |
307 | { | |
308 | int i, ret; | |
309 | ||
310 | if (!bo->pin_count) { | |
311 | DRM_ERROR("unpin bad %p\n", bo); | |
312 | return 0; | |
313 | } | |
314 | bo->pin_count--; | |
315 | ||
316 | if (bo->pin_count) | |
317 | return 0; | |
318 | ||
319 | for (i = 0; i < bo->placement.num_placement; i++) | |
320 | bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT; | |
321 | ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false); | |
322 | if (ret) | |
323 | return ret; | |
324 | ||
325 | return 0; | |
326 | } | |
327 | ||
328 | int bochs_mmap(struct file *filp, struct vm_area_struct *vma) | |
329 | { | |
330 | struct drm_file *file_priv; | |
331 | struct bochs_device *bochs; | |
332 | ||
333 | if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) | |
334 | return drm_mmap(filp, vma); | |
335 | ||
336 | file_priv = filp->private_data; | |
337 | bochs = file_priv->minor->dev->dev_private; | |
338 | return ttm_bo_mmap(filp, vma, &bochs->ttm.bdev); | |
339 | } | |
340 | ||
341 | /* ---------------------------------------------------------------------- */ | |
342 | ||
343 | static int bochs_bo_create(struct drm_device *dev, int size, int align, | |
344 | uint32_t flags, struct bochs_bo **pbochsbo) | |
345 | { | |
346 | struct bochs_device *bochs = dev->dev_private; | |
347 | struct bochs_bo *bochsbo; | |
348 | size_t acc_size; | |
349 | int ret; | |
350 | ||
351 | bochsbo = kzalloc(sizeof(struct bochs_bo), GFP_KERNEL); | |
352 | if (!bochsbo) | |
353 | return -ENOMEM; | |
354 | ||
355 | ret = drm_gem_object_init(dev, &bochsbo->gem, size); | |
356 | if (ret) { | |
357 | kfree(bochsbo); | |
358 | return ret; | |
359 | } | |
360 | ||
361 | bochsbo->bo.bdev = &bochs->ttm.bdev; | |
362 | bochsbo->bo.bdev->dev_mapping = dev->dev_mapping; | |
363 | ||
364 | bochs_ttm_placement(bochsbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM); | |
365 | ||
366 | acc_size = ttm_bo_dma_acc_size(&bochs->ttm.bdev, size, | |
367 | sizeof(struct bochs_bo)); | |
368 | ||
369 | ret = ttm_bo_init(&bochs->ttm.bdev, &bochsbo->bo, size, | |
370 | ttm_bo_type_device, &bochsbo->placement, | |
371 | align >> PAGE_SHIFT, false, NULL, acc_size, | |
372 | NULL, bochs_bo_ttm_destroy); | |
373 | if (ret) | |
374 | return ret; | |
375 | ||
376 | *pbochsbo = bochsbo; | |
377 | return 0; | |
378 | } | |
379 | ||
380 | int bochs_gem_create(struct drm_device *dev, u32 size, bool iskernel, | |
381 | struct drm_gem_object **obj) | |
382 | { | |
383 | struct bochs_bo *bochsbo; | |
384 | int ret; | |
385 | ||
386 | *obj = NULL; | |
387 | ||
388 | size = ALIGN(size, PAGE_SIZE); | |
389 | if (size == 0) | |
390 | return -EINVAL; | |
391 | ||
392 | ret = bochs_bo_create(dev, size, 0, 0, &bochsbo); | |
393 | if (ret) { | |
394 | if (ret != -ERESTARTSYS) | |
395 | DRM_ERROR("failed to allocate GEM object\n"); | |
396 | return ret; | |
397 | } | |
398 | *obj = &bochsbo->gem; | |
399 | return 0; | |
400 | } | |
401 | ||
402 | int bochs_dumb_create(struct drm_file *file, struct drm_device *dev, | |
403 | struct drm_mode_create_dumb *args) | |
404 | { | |
405 | struct drm_gem_object *gobj; | |
406 | u32 handle; | |
407 | int ret; | |
408 | ||
409 | args->pitch = args->width * ((args->bpp + 7) / 8); | |
410 | args->size = args->pitch * args->height; | |
411 | ||
412 | ret = bochs_gem_create(dev, args->size, false, | |
413 | &gobj); | |
414 | if (ret) | |
415 | return ret; | |
416 | ||
417 | ret = drm_gem_handle_create(file, gobj, &handle); | |
418 | drm_gem_object_unreference_unlocked(gobj); | |
419 | if (ret) | |
420 | return ret; | |
421 | ||
422 | args->handle = handle; | |
423 | return 0; | |
424 | } | |
425 | ||
426 | static void bochs_bo_unref(struct bochs_bo **bo) | |
427 | { | |
428 | struct ttm_buffer_object *tbo; | |
429 | ||
430 | if ((*bo) == NULL) | |
431 | return; | |
432 | ||
433 | tbo = &((*bo)->bo); | |
434 | ttm_bo_unref(&tbo); | |
435 | if (tbo == NULL) | |
436 | *bo = NULL; | |
437 | ||
438 | } | |
439 | ||
440 | void bochs_gem_free_object(struct drm_gem_object *obj) | |
441 | { | |
442 | struct bochs_bo *bochs_bo = gem_to_bochs_bo(obj); | |
443 | ||
444 | if (!bochs_bo) | |
445 | return; | |
446 | bochs_bo_unref(&bochs_bo); | |
447 | } | |
448 | ||
449 | int bochs_dumb_mmap_offset(struct drm_file *file, struct drm_device *dev, | |
450 | uint32_t handle, uint64_t *offset) | |
451 | { | |
452 | struct drm_gem_object *obj; | |
453 | int ret; | |
454 | struct bochs_bo *bo; | |
455 | ||
456 | mutex_lock(&dev->struct_mutex); | |
457 | obj = drm_gem_object_lookup(dev, file, handle); | |
458 | if (obj == NULL) { | |
459 | ret = -ENOENT; | |
460 | goto out_unlock; | |
461 | } | |
462 | ||
463 | bo = gem_to_bochs_bo(obj); | |
464 | *offset = bochs_bo_mmap_offset(bo); | |
465 | ||
466 | drm_gem_object_unreference(obj); | |
467 | ret = 0; | |
468 | out_unlock: | |
469 | mutex_unlock(&dev->struct_mutex); | |
470 | return ret; | |
471 | ||
472 | } | |
473 | ||
474 | /* ---------------------------------------------------------------------- */ | |
475 | ||
476 | static void bochs_user_framebuffer_destroy(struct drm_framebuffer *fb) | |
477 | { | |
478 | struct bochs_framebuffer *bochs_fb = to_bochs_framebuffer(fb); | |
479 | if (bochs_fb->obj) | |
480 | drm_gem_object_unreference_unlocked(bochs_fb->obj); | |
481 | drm_framebuffer_cleanup(fb); | |
482 | kfree(fb); | |
483 | } | |
484 | ||
485 | static const struct drm_framebuffer_funcs bochs_fb_funcs = { | |
486 | .destroy = bochs_user_framebuffer_destroy, | |
487 | }; | |
488 | ||
489 | int bochs_framebuffer_init(struct drm_device *dev, | |
490 | struct bochs_framebuffer *gfb, | |
491 | struct drm_mode_fb_cmd2 *mode_cmd, | |
492 | struct drm_gem_object *obj) | |
493 | { | |
494 | int ret; | |
495 | ||
496 | drm_helper_mode_fill_fb_struct(&gfb->base, mode_cmd); | |
497 | gfb->obj = obj; | |
498 | ret = drm_framebuffer_init(dev, &gfb->base, &bochs_fb_funcs); | |
499 | if (ret) { | |
500 | DRM_ERROR("drm_framebuffer_init failed: %d\n", ret); | |
501 | return ret; | |
502 | } | |
503 | return 0; | |
504 | } | |
505 | ||
506 | static struct drm_framebuffer * | |
507 | bochs_user_framebuffer_create(struct drm_device *dev, | |
508 | struct drm_file *filp, | |
509 | struct drm_mode_fb_cmd2 *mode_cmd) | |
510 | { | |
511 | struct drm_gem_object *obj; | |
512 | struct bochs_framebuffer *bochs_fb; | |
513 | int ret; | |
514 | ||
515 | DRM_DEBUG_DRIVER("%dx%d, format %c%c%c%c\n", | |
516 | mode_cmd->width, mode_cmd->height, | |
517 | (mode_cmd->pixel_format) & 0xff, | |
518 | (mode_cmd->pixel_format >> 8) & 0xff, | |
519 | (mode_cmd->pixel_format >> 16) & 0xff, | |
520 | (mode_cmd->pixel_format >> 24) & 0xff); | |
521 | ||
522 | if (mode_cmd->pixel_format != DRM_FORMAT_XRGB8888) | |
523 | return ERR_PTR(-ENOENT); | |
524 | ||
525 | obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]); | |
526 | if (obj == NULL) | |
527 | return ERR_PTR(-ENOENT); | |
528 | ||
529 | bochs_fb = kzalloc(sizeof(*bochs_fb), GFP_KERNEL); | |
530 | if (!bochs_fb) { | |
531 | drm_gem_object_unreference_unlocked(obj); | |
532 | return ERR_PTR(-ENOMEM); | |
533 | } | |
534 | ||
535 | ret = bochs_framebuffer_init(dev, bochs_fb, mode_cmd, obj); | |
536 | if (ret) { | |
537 | drm_gem_object_unreference_unlocked(obj); | |
538 | kfree(bochs_fb); | |
539 | return ERR_PTR(ret); | |
540 | } | |
541 | return &bochs_fb->base; | |
542 | } | |
543 | ||
544 | const struct drm_mode_config_funcs bochs_mode_funcs = { | |
545 | .fb_create = bochs_user_framebuffer_create, | |
546 | }; |