winsys/radeon: use pb_cache instead of pb_cache_manager
[mesa.git] / src / gallium / winsys / radeon / drm / radeon_drm_bo.c
1 /*
2 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 */
26
27 #include "radeon_drm_cs.h"
28
29 #include "util/u_hash_table.h"
30 #include "util/u_memory.h"
31 #include "util/simple_list.h"
32 #include "os/os_thread.h"
33 #include "os/os_mman.h"
34 #include "os/os_time.h"
35
36 #include "state_tracker/drm_driver.h"
37
38 #include <sys/ioctl.h>
39 #include <xf86drm.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43
44 static inline struct radeon_bo *radeon_bo(struct pb_buffer *bo)
45 {
46 return (struct radeon_bo *)bo;
47 }
48
49 struct radeon_bo_va_hole {
50 struct list_head list;
51 uint64_t offset;
52 uint64_t size;
53 };
54
55 static bool radeon_bo_is_busy(struct radeon_bo *bo)
56 {
57 struct drm_radeon_gem_busy args = {0};
58
59 args.handle = bo->handle;
60 return drmCommandWriteRead(bo->rws->fd, DRM_RADEON_GEM_BUSY,
61 &args, sizeof(args)) != 0;
62 }
63
64 static void radeon_bo_wait_idle(struct radeon_bo *bo)
65 {
66 struct drm_radeon_gem_wait_idle args = {0};
67
68 args.handle = bo->handle;
69 while (drmCommandWrite(bo->rws->fd, DRM_RADEON_GEM_WAIT_IDLE,
70 &args, sizeof(args)) == -EBUSY);
71 }
72
73 static bool radeon_bo_wait(struct pb_buffer *_buf, uint64_t timeout,
74 enum radeon_bo_usage usage)
75 {
76 struct radeon_bo *bo = radeon_bo(_buf);
77 int64_t abs_timeout;
78
79 /* No timeout. Just query. */
80 if (timeout == 0)
81 return !bo->num_active_ioctls && !radeon_bo_is_busy(bo);
82
83 abs_timeout = os_time_get_absolute_timeout(timeout);
84
85 /* Wait if any ioctl is being submitted with this buffer. */
86 if (!os_wait_until_zero_abs_timeout(&bo->num_active_ioctls, abs_timeout))
87 return false;
88
89 /* Infinite timeout. */
90 if (abs_timeout == PIPE_TIMEOUT_INFINITE) {
91 radeon_bo_wait_idle(bo);
92 return true;
93 }
94
95 /* Other timeouts need to be emulated with a loop. */
96 while (radeon_bo_is_busy(bo)) {
97 if (os_time_get_nano() >= abs_timeout)
98 return false;
99 os_time_sleep(10);
100 }
101
102 return true;
103 }
104
105 static enum radeon_bo_domain get_valid_domain(enum radeon_bo_domain domain)
106 {
107 /* Zero domains the driver doesn't understand. */
108 domain &= RADEON_DOMAIN_VRAM_GTT;
109
110 /* If no domain is set, we must set something... */
111 if (!domain)
112 domain = RADEON_DOMAIN_VRAM_GTT;
113
114 return domain;
115 }
116
117 static enum radeon_bo_domain radeon_bo_get_initial_domain(
118 struct radeon_winsys_cs_handle *buf)
119 {
120 struct radeon_bo *bo = (struct radeon_bo*)buf;
121 struct drm_radeon_gem_op args;
122
123 if (bo->rws->info.drm_minor < 38)
124 return RADEON_DOMAIN_VRAM_GTT;
125
126 memset(&args, 0, sizeof(args));
127 args.handle = bo->handle;
128 args.op = RADEON_GEM_OP_GET_INITIAL_DOMAIN;
129
130 drmCommandWriteRead(bo->rws->fd, DRM_RADEON_GEM_OP,
131 &args, sizeof(args));
132
133 /* GEM domains and winsys domains are defined the same. */
134 return get_valid_domain(args.value);
135 }
136
137 static uint64_t radeon_bomgr_find_va(struct radeon_drm_winsys *rws,
138 uint64_t size, uint64_t alignment)
139 {
140 struct radeon_bo_va_hole *hole, *n;
141 uint64_t offset = 0, waste = 0;
142
143 /* All VM address space holes will implicitly start aligned to the
144 * size alignment, so we don't need to sanitize the alignment here
145 */
146 size = align(size, rws->size_align);
147
148 pipe_mutex_lock(rws->bo_va_mutex);
149 /* first look for a hole */
150 LIST_FOR_EACH_ENTRY_SAFE(hole, n, &rws->va_holes, list) {
151 offset = hole->offset;
152 waste = offset % alignment;
153 waste = waste ? alignment - waste : 0;
154 offset += waste;
155 if (offset >= (hole->offset + hole->size)) {
156 continue;
157 }
158 if (!waste && hole->size == size) {
159 offset = hole->offset;
160 list_del(&hole->list);
161 FREE(hole);
162 pipe_mutex_unlock(rws->bo_va_mutex);
163 return offset;
164 }
165 if ((hole->size - waste) > size) {
166 if (waste) {
167 n = CALLOC_STRUCT(radeon_bo_va_hole);
168 n->size = waste;
169 n->offset = hole->offset;
170 list_add(&n->list, &hole->list);
171 }
172 hole->size -= (size + waste);
173 hole->offset += size + waste;
174 pipe_mutex_unlock(rws->bo_va_mutex);
175 return offset;
176 }
177 if ((hole->size - waste) == size) {
178 hole->size = waste;
179 pipe_mutex_unlock(rws->bo_va_mutex);
180 return offset;
181 }
182 }
183
184 offset = rws->va_offset;
185 waste = offset % alignment;
186 waste = waste ? alignment - waste : 0;
187 if (waste) {
188 n = CALLOC_STRUCT(radeon_bo_va_hole);
189 n->size = waste;
190 n->offset = offset;
191 list_add(&n->list, &rws->va_holes);
192 }
193 offset += waste;
194 rws->va_offset += size + waste;
195 pipe_mutex_unlock(rws->bo_va_mutex);
196 return offset;
197 }
198
199 static void radeon_bomgr_free_va(struct radeon_drm_winsys *rws,
200 uint64_t va, uint64_t size)
201 {
202 struct radeon_bo_va_hole *hole;
203
204 size = align(size, rws->size_align);
205
206 pipe_mutex_lock(rws->bo_va_mutex);
207 if ((va + size) == rws->va_offset) {
208 rws->va_offset = va;
209 /* Delete uppermost hole if it reaches the new top */
210 if (!LIST_IS_EMPTY(&rws->va_holes)) {
211 hole = container_of(rws->va_holes.next, hole, list);
212 if ((hole->offset + hole->size) == va) {
213 rws->va_offset = hole->offset;
214 list_del(&hole->list);
215 FREE(hole);
216 }
217 }
218 } else {
219 struct radeon_bo_va_hole *next;
220
221 hole = container_of(&rws->va_holes, hole, list);
222 LIST_FOR_EACH_ENTRY(next, &rws->va_holes, list) {
223 if (next->offset < va)
224 break;
225 hole = next;
226 }
227
228 if (&hole->list != &rws->va_holes) {
229 /* Grow upper hole if it's adjacent */
230 if (hole->offset == (va + size)) {
231 hole->offset = va;
232 hole->size += size;
233 /* Merge lower hole if it's adjacent */
234 if (next != hole && &next->list != &rws->va_holes &&
235 (next->offset + next->size) == va) {
236 next->size += hole->size;
237 list_del(&hole->list);
238 FREE(hole);
239 }
240 goto out;
241 }
242 }
243
244 /* Grow lower hole if it's adjacent */
245 if (next != hole && &next->list != &rws->va_holes &&
246 (next->offset + next->size) == va) {
247 next->size += size;
248 goto out;
249 }
250
251 /* FIXME on allocation failure we just lose virtual address space
252 * maybe print a warning
253 */
254 next = CALLOC_STRUCT(radeon_bo_va_hole);
255 if (next) {
256 next->size = size;
257 next->offset = va;
258 list_add(&next->list, &hole->list);
259 }
260 }
261 out:
262 pipe_mutex_unlock(rws->bo_va_mutex);
263 }
264
265 void radeon_bo_destroy(struct pb_buffer *_buf)
266 {
267 struct radeon_bo *bo = radeon_bo(_buf);
268 struct radeon_drm_winsys *rws = bo->rws;
269 struct drm_gem_close args;
270
271 memset(&args, 0, sizeof(args));
272
273 pipe_mutex_lock(rws->bo_handles_mutex);
274 util_hash_table_remove(rws->bo_handles, (void*)(uintptr_t)bo->handle);
275 if (bo->flink_name) {
276 util_hash_table_remove(rws->bo_names,
277 (void*)(uintptr_t)bo->flink_name);
278 }
279 pipe_mutex_unlock(rws->bo_handles_mutex);
280
281 if (bo->ptr)
282 os_munmap(bo->ptr, bo->base.size);
283
284 if (rws->info.r600_virtual_address) {
285 if (rws->va_unmap_working) {
286 struct drm_radeon_gem_va va;
287
288 va.handle = bo->handle;
289 va.vm_id = 0;
290 va.operation = RADEON_VA_UNMAP;
291 va.flags = RADEON_VM_PAGE_READABLE |
292 RADEON_VM_PAGE_WRITEABLE |
293 RADEON_VM_PAGE_SNOOPED;
294 va.offset = bo->va;
295
296 if (drmCommandWriteRead(rws->fd, DRM_RADEON_GEM_VA, &va,
297 sizeof(va)) != 0 &&
298 va.operation == RADEON_VA_RESULT_ERROR) {
299 fprintf(stderr, "radeon: Failed to deallocate virtual address for buffer:\n");
300 fprintf(stderr, "radeon: size : %d bytes\n", bo->base.size);
301 fprintf(stderr, "radeon: va : 0x%016llx\n", (unsigned long long)bo->va);
302 }
303 }
304
305 radeon_bomgr_free_va(rws, bo->va, bo->base.size);
306 }
307
308 /* Close object. */
309 args.handle = bo->handle;
310 drmIoctl(rws->fd, DRM_IOCTL_GEM_CLOSE, &args);
311
312 pipe_mutex_destroy(bo->map_mutex);
313
314 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
315 rws->allocated_vram -= align(bo->base.size, rws->size_align);
316 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
317 rws->allocated_gtt -= align(bo->base.size, rws->size_align);
318 FREE(bo);
319 }
320
321 static void radeon_bo_destroy_or_cache(struct pb_buffer *_buf)
322 {
323 struct radeon_bo *bo = radeon_bo(_buf);
324
325 if (bo->use_reusable_pool)
326 pb_cache_add_buffer(&bo->cache_entry);
327 else
328 radeon_bo_destroy(_buf);
329 }
330
331 void *radeon_bo_do_map(struct radeon_bo *bo)
332 {
333 struct drm_radeon_gem_mmap args = {0};
334 void *ptr;
335
336 /* If the buffer is created from user memory, return the user pointer. */
337 if (bo->user_ptr)
338 return bo->user_ptr;
339
340 /* Map the buffer. */
341 pipe_mutex_lock(bo->map_mutex);
342 /* Return the pointer if it's already mapped. */
343 if (bo->ptr) {
344 bo->map_count++;
345 pipe_mutex_unlock(bo->map_mutex);
346 return bo->ptr;
347 }
348 args.handle = bo->handle;
349 args.offset = 0;
350 args.size = (uint64_t)bo->base.size;
351 if (drmCommandWriteRead(bo->rws->fd,
352 DRM_RADEON_GEM_MMAP,
353 &args,
354 sizeof(args))) {
355 pipe_mutex_unlock(bo->map_mutex);
356 fprintf(stderr, "radeon: gem_mmap failed: %p 0x%08X\n",
357 bo, bo->handle);
358 return NULL;
359 }
360
361 ptr = os_mmap(0, args.size, PROT_READ|PROT_WRITE, MAP_SHARED,
362 bo->rws->fd, args.addr_ptr);
363 if (ptr == MAP_FAILED) {
364 pipe_mutex_unlock(bo->map_mutex);
365 fprintf(stderr, "radeon: mmap failed, errno: %i\n", errno);
366 return NULL;
367 }
368 bo->ptr = ptr;
369 bo->map_count = 1;
370 pipe_mutex_unlock(bo->map_mutex);
371
372 return bo->ptr;
373 }
374
375 static void *radeon_bo_map(struct radeon_winsys_cs_handle *buf,
376 struct radeon_winsys_cs *rcs,
377 enum pipe_transfer_usage usage)
378 {
379 struct radeon_bo *bo = (struct radeon_bo*)buf;
380 struct radeon_drm_cs *cs = (struct radeon_drm_cs*)rcs;
381
382 /* If it's not unsynchronized bo_map, flush CS if needed and then wait. */
383 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
384 /* DONTBLOCK doesn't make sense with UNSYNCHRONIZED. */
385 if (usage & PIPE_TRANSFER_DONTBLOCK) {
386 if (!(usage & PIPE_TRANSFER_WRITE)) {
387 /* Mapping for read.
388 *
389 * Since we are mapping for read, we don't need to wait
390 * if the GPU is using the buffer for read too
391 * (neither one is changing it).
392 *
393 * Only check whether the buffer is being used for write. */
394 if (cs && radeon_bo_is_referenced_by_cs_for_write(cs, bo)) {
395 cs->flush_cs(cs->flush_data, RADEON_FLUSH_ASYNC, NULL);
396 return NULL;
397 }
398
399 if (!radeon_bo_wait((struct pb_buffer*)bo, 0,
400 RADEON_USAGE_WRITE)) {
401 return NULL;
402 }
403 } else {
404 if (cs && radeon_bo_is_referenced_by_cs(cs, bo)) {
405 cs->flush_cs(cs->flush_data, RADEON_FLUSH_ASYNC, NULL);
406 return NULL;
407 }
408
409 if (!radeon_bo_wait((struct pb_buffer*)bo, 0,
410 RADEON_USAGE_READWRITE)) {
411 return NULL;
412 }
413 }
414 } else {
415 uint64_t time = os_time_get_nano();
416
417 if (!(usage & PIPE_TRANSFER_WRITE)) {
418 /* Mapping for read.
419 *
420 * Since we are mapping for read, we don't need to wait
421 * if the GPU is using the buffer for read too
422 * (neither one is changing it).
423 *
424 * Only check whether the buffer is being used for write. */
425 if (cs && radeon_bo_is_referenced_by_cs_for_write(cs, bo)) {
426 cs->flush_cs(cs->flush_data, 0, NULL);
427 }
428 radeon_bo_wait((struct pb_buffer*)bo, PIPE_TIMEOUT_INFINITE,
429 RADEON_USAGE_WRITE);
430 } else {
431 /* Mapping for write. */
432 if (cs) {
433 if (radeon_bo_is_referenced_by_cs(cs, bo)) {
434 cs->flush_cs(cs->flush_data, 0, NULL);
435 } else {
436 /* Try to avoid busy-waiting in radeon_bo_wait. */
437 if (p_atomic_read(&bo->num_active_ioctls))
438 radeon_drm_cs_sync_flush(rcs);
439 }
440 }
441
442 radeon_bo_wait((struct pb_buffer*)bo, PIPE_TIMEOUT_INFINITE,
443 RADEON_USAGE_READWRITE);
444 }
445
446 bo->rws->buffer_wait_time += os_time_get_nano() - time;
447 }
448 }
449
450 return radeon_bo_do_map(bo);
451 }
452
453 static void radeon_bo_unmap(struct radeon_winsys_cs_handle *_buf)
454 {
455 struct radeon_bo *bo = (struct radeon_bo*)_buf;
456
457 if (bo->user_ptr)
458 return;
459
460 pipe_mutex_lock(bo->map_mutex);
461 if (!bo->ptr) {
462 pipe_mutex_unlock(bo->map_mutex);
463 return; /* it's not been mapped */
464 }
465
466 assert(bo->map_count);
467 if (--bo->map_count) {
468 pipe_mutex_unlock(bo->map_mutex);
469 return; /* it's been mapped multiple times */
470 }
471
472 os_munmap(bo->ptr, bo->base.size);
473 bo->ptr = NULL;
474 pipe_mutex_unlock(bo->map_mutex);
475 }
476
477 static const struct pb_vtbl radeon_bo_vtbl = {
478 radeon_bo_destroy_or_cache
479 /* other functions are never called */
480 };
481
482 #ifndef RADEON_GEM_GTT_WC
483 #define RADEON_GEM_GTT_WC (1 << 2)
484 #endif
485 #ifndef RADEON_GEM_CPU_ACCESS
486 /* BO is expected to be accessed by the CPU */
487 #define RADEON_GEM_CPU_ACCESS (1 << 3)
488 #endif
489 #ifndef RADEON_GEM_NO_CPU_ACCESS
490 /* CPU access is not expected to work for this BO */
491 #define RADEON_GEM_NO_CPU_ACCESS (1 << 4)
492 #endif
493
494 static struct radeon_bo *radeon_create_bo(struct radeon_drm_winsys *rws,
495 unsigned size, unsigned alignment,
496 unsigned usage,
497 unsigned initial_domains,
498 unsigned flags)
499 {
500 struct radeon_bo *bo;
501 struct drm_radeon_gem_create args;
502 int r;
503
504 memset(&args, 0, sizeof(args));
505
506 assert(initial_domains);
507 assert((initial_domains &
508 ~(RADEON_GEM_DOMAIN_GTT | RADEON_GEM_DOMAIN_VRAM)) == 0);
509
510 args.size = size;
511 args.alignment = alignment;
512 args.initial_domain = initial_domains;
513 args.flags = 0;
514
515 if (flags & RADEON_FLAG_GTT_WC)
516 args.flags |= RADEON_GEM_GTT_WC;
517 if (flags & RADEON_FLAG_CPU_ACCESS)
518 args.flags |= RADEON_GEM_CPU_ACCESS;
519 if (flags & RADEON_FLAG_NO_CPU_ACCESS)
520 args.flags |= RADEON_GEM_NO_CPU_ACCESS;
521
522 if (drmCommandWriteRead(rws->fd, DRM_RADEON_GEM_CREATE,
523 &args, sizeof(args))) {
524 fprintf(stderr, "radeon: Failed to allocate a buffer:\n");
525 fprintf(stderr, "radeon: size : %d bytes\n", size);
526 fprintf(stderr, "radeon: alignment : %d bytes\n", alignment);
527 fprintf(stderr, "radeon: domains : %d\n", args.initial_domain);
528 fprintf(stderr, "radeon: flags : %d\n", args.flags);
529 return NULL;
530 }
531
532 bo = CALLOC_STRUCT(radeon_bo);
533 if (!bo)
534 return NULL;
535
536 pipe_reference_init(&bo->base.reference, 1);
537 bo->base.alignment = alignment;
538 bo->base.usage = usage;
539 bo->base.size = size;
540 bo->base.vtbl = &radeon_bo_vtbl;
541 bo->rws = rws;
542 bo->handle = args.handle;
543 bo->va = 0;
544 bo->initial_domain = initial_domains;
545 pipe_mutex_init(bo->map_mutex);
546 pb_cache_init_entry(&rws->bo_cache, &bo->cache_entry, &bo->base);
547
548 if (rws->info.r600_virtual_address) {
549 struct drm_radeon_gem_va va;
550
551 bo->va = radeon_bomgr_find_va(rws, size, alignment);
552
553 va.handle = bo->handle;
554 va.vm_id = 0;
555 va.operation = RADEON_VA_MAP;
556 va.flags = RADEON_VM_PAGE_READABLE |
557 RADEON_VM_PAGE_WRITEABLE |
558 RADEON_VM_PAGE_SNOOPED;
559 va.offset = bo->va;
560 r = drmCommandWriteRead(rws->fd, DRM_RADEON_GEM_VA, &va, sizeof(va));
561 if (r && va.operation == RADEON_VA_RESULT_ERROR) {
562 fprintf(stderr, "radeon: Failed to allocate virtual address for buffer:\n");
563 fprintf(stderr, "radeon: size : %d bytes\n", size);
564 fprintf(stderr, "radeon: alignment : %d bytes\n", alignment);
565 fprintf(stderr, "radeon: domains : %d\n", args.initial_domain);
566 fprintf(stderr, "radeon: va : 0x%016llx\n", (unsigned long long)bo->va);
567 radeon_bo_destroy(&bo->base);
568 return NULL;
569 }
570 pipe_mutex_lock(rws->bo_handles_mutex);
571 if (va.operation == RADEON_VA_RESULT_VA_EXIST) {
572 struct pb_buffer *b = &bo->base;
573 struct radeon_bo *old_bo =
574 util_hash_table_get(rws->bo_vas, (void*)(uintptr_t)va.offset);
575
576 pipe_mutex_unlock(rws->bo_handles_mutex);
577 pb_reference(&b, &old_bo->base);
578 return b;
579 }
580
581 util_hash_table_set(rws->bo_vas, (void*)(uintptr_t)bo->va, bo);
582 pipe_mutex_unlock(rws->bo_handles_mutex);
583 }
584
585 if (initial_domains & RADEON_DOMAIN_VRAM)
586 rws->allocated_vram += align(size, rws->size_align);
587 else if (initial_domains & RADEON_DOMAIN_GTT)
588 rws->allocated_gtt += align(size, rws->size_align);
589
590 return &bo->base;
591 }
592
593 bool radeon_bo_can_reclaim(struct pb_buffer *_buf)
594 {
595 struct radeon_bo *bo = radeon_bo(_buf);
596
597 if (radeon_bo_is_referenced_by_any_cs(bo))
598 return false;
599
600 return radeon_bo_wait(_buf, 0, RADEON_USAGE_READWRITE);
601 }
602
603 static unsigned eg_tile_split(unsigned tile_split)
604 {
605 switch (tile_split) {
606 case 0: tile_split = 64; break;
607 case 1: tile_split = 128; break;
608 case 2: tile_split = 256; break;
609 case 3: tile_split = 512; break;
610 default:
611 case 4: tile_split = 1024; break;
612 case 5: tile_split = 2048; break;
613 case 6: tile_split = 4096; break;
614 }
615 return tile_split;
616 }
617
618 static unsigned eg_tile_split_rev(unsigned eg_tile_split)
619 {
620 switch (eg_tile_split) {
621 case 64: return 0;
622 case 128: return 1;
623 case 256: return 2;
624 case 512: return 3;
625 default:
626 case 1024: return 4;
627 case 2048: return 5;
628 case 4096: return 6;
629 }
630 }
631
632 static void radeon_bo_get_tiling(struct pb_buffer *_buf,
633 enum radeon_bo_layout *microtiled,
634 enum radeon_bo_layout *macrotiled,
635 unsigned *bankw, unsigned *bankh,
636 unsigned *tile_split,
637 unsigned *stencil_tile_split,
638 unsigned *mtilea,
639 bool *scanout)
640 {
641 struct radeon_bo *bo = radeon_bo(_buf);
642 struct drm_radeon_gem_set_tiling args;
643
644 memset(&args, 0, sizeof(args));
645
646 args.handle = bo->handle;
647
648 drmCommandWriteRead(bo->rws->fd,
649 DRM_RADEON_GEM_GET_TILING,
650 &args,
651 sizeof(args));
652
653 *microtiled = RADEON_LAYOUT_LINEAR;
654 *macrotiled = RADEON_LAYOUT_LINEAR;
655 if (args.tiling_flags & RADEON_TILING_MICRO)
656 *microtiled = RADEON_LAYOUT_TILED;
657 else if (args.tiling_flags & RADEON_TILING_MICRO_SQUARE)
658 *microtiled = RADEON_LAYOUT_SQUARETILED;
659
660 if (args.tiling_flags & RADEON_TILING_MACRO)
661 *macrotiled = RADEON_LAYOUT_TILED;
662 if (bankw && tile_split && stencil_tile_split && mtilea && tile_split) {
663 *bankw = (args.tiling_flags >> RADEON_TILING_EG_BANKW_SHIFT) & RADEON_TILING_EG_BANKW_MASK;
664 *bankh = (args.tiling_flags >> RADEON_TILING_EG_BANKH_SHIFT) & RADEON_TILING_EG_BANKH_MASK;
665 *tile_split = (args.tiling_flags >> RADEON_TILING_EG_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_TILE_SPLIT_MASK;
666 *stencil_tile_split = (args.tiling_flags >> RADEON_TILING_EG_STENCIL_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_STENCIL_TILE_SPLIT_MASK;
667 *mtilea = (args.tiling_flags >> RADEON_TILING_EG_MACRO_TILE_ASPECT_SHIFT) & RADEON_TILING_EG_MACRO_TILE_ASPECT_MASK;
668 *tile_split = eg_tile_split(*tile_split);
669 }
670 if (scanout)
671 *scanout = bo->rws->gen >= DRV_SI && !(args.tiling_flags & RADEON_TILING_R600_NO_SCANOUT);
672 }
673
674 static void radeon_bo_set_tiling(struct pb_buffer *_buf,
675 struct radeon_winsys_cs *rcs,
676 enum radeon_bo_layout microtiled,
677 enum radeon_bo_layout macrotiled,
678 unsigned pipe_config,
679 unsigned bankw, unsigned bankh,
680 unsigned tile_split,
681 unsigned stencil_tile_split,
682 unsigned mtilea, unsigned num_banks,
683 uint32_t pitch,
684 bool scanout)
685 {
686 struct radeon_bo *bo = radeon_bo(_buf);
687 struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
688 struct drm_radeon_gem_set_tiling args;
689
690 memset(&args, 0, sizeof(args));
691
692 /* Tiling determines how DRM treats the buffer data.
693 * We must flush CS when changing it if the buffer is referenced. */
694 if (cs && radeon_bo_is_referenced_by_cs(cs, bo)) {
695 cs->flush_cs(cs->flush_data, 0, NULL);
696 }
697
698 os_wait_until_zero(&bo->num_active_ioctls, PIPE_TIMEOUT_INFINITE);
699
700 if (microtiled == RADEON_LAYOUT_TILED)
701 args.tiling_flags |= RADEON_TILING_MICRO;
702 else if (microtiled == RADEON_LAYOUT_SQUARETILED)
703 args.tiling_flags |= RADEON_TILING_MICRO_SQUARE;
704
705 if (macrotiled == RADEON_LAYOUT_TILED)
706 args.tiling_flags |= RADEON_TILING_MACRO;
707
708 args.tiling_flags |= (bankw & RADEON_TILING_EG_BANKW_MASK) <<
709 RADEON_TILING_EG_BANKW_SHIFT;
710 args.tiling_flags |= (bankh & RADEON_TILING_EG_BANKH_MASK) <<
711 RADEON_TILING_EG_BANKH_SHIFT;
712 if (tile_split) {
713 args.tiling_flags |= (eg_tile_split_rev(tile_split) &
714 RADEON_TILING_EG_TILE_SPLIT_MASK) <<
715 RADEON_TILING_EG_TILE_SPLIT_SHIFT;
716 }
717 args.tiling_flags |= (stencil_tile_split &
718 RADEON_TILING_EG_STENCIL_TILE_SPLIT_MASK) <<
719 RADEON_TILING_EG_STENCIL_TILE_SPLIT_SHIFT;
720 args.tiling_flags |= (mtilea & RADEON_TILING_EG_MACRO_TILE_ASPECT_MASK) <<
721 RADEON_TILING_EG_MACRO_TILE_ASPECT_SHIFT;
722
723 if (bo->rws->gen >= DRV_SI && !scanout)
724 args.tiling_flags |= RADEON_TILING_R600_NO_SCANOUT;
725
726 args.handle = bo->handle;
727 args.pitch = pitch;
728
729 drmCommandWriteRead(bo->rws->fd,
730 DRM_RADEON_GEM_SET_TILING,
731 &args,
732 sizeof(args));
733 }
734
735 static struct radeon_winsys_cs_handle *radeon_drm_get_cs_handle(struct pb_buffer *_buf)
736 {
737 /* return radeon_bo. */
738 return (struct radeon_winsys_cs_handle*)radeon_bo(_buf);
739 }
740
741 static struct pb_buffer *
742 radeon_winsys_bo_create(struct radeon_winsys *rws,
743 unsigned size,
744 unsigned alignment,
745 boolean use_reusable_pool,
746 enum radeon_bo_domain domain,
747 enum radeon_bo_flag flags)
748 {
749 struct radeon_drm_winsys *ws = radeon_drm_winsys(rws);
750 struct radeon_bo *bo;
751 unsigned usage = 0;
752
753 /* Align size to page size. This is the minimum alignment for normal
754 * BOs. Aligning this here helps the cached bufmgr. Especially small BOs,
755 * like constant/uniform buffers, can benefit from better and more reuse.
756 */
757 size = align(size, ws->size_align);
758
759 /* Only set one usage bit each for domains and flags, or the cache manager
760 * might consider different sets of domains / flags compatible
761 */
762 if (domain == RADEON_DOMAIN_VRAM_GTT)
763 usage = 1 << 2;
764 else
765 usage = domain >> 1;
766 assert(flags < sizeof(usage) * 8 - 3);
767 usage |= 1 << (flags + 3);
768
769 if (use_reusable_pool) {
770 bo = pb_cache_reclaim_buffer(&ws->bo_cache, size, alignment, usage);
771 if (bo)
772 return bo;
773 }
774
775 bo = radeon_create_bo(ws, size, alignment, usage, domain, flags);
776 if (!bo)
777 return NULL;
778
779 bo->use_reusable_pool = use_reusable_pool;
780
781 pipe_mutex_lock(ws->bo_handles_mutex);
782 util_hash_table_set(ws->bo_handles, (void*)(uintptr_t)bo->handle, bo);
783 pipe_mutex_unlock(ws->bo_handles_mutex);
784
785 return &bo->base;
786 }
787
788 static struct pb_buffer *radeon_winsys_bo_from_ptr(struct radeon_winsys *rws,
789 void *pointer, unsigned size)
790 {
791 struct radeon_drm_winsys *ws = radeon_drm_winsys(rws);
792 struct drm_radeon_gem_userptr args;
793 struct radeon_bo *bo;
794 int r;
795
796 bo = CALLOC_STRUCT(radeon_bo);
797 if (!bo)
798 return NULL;
799
800 memset(&args, 0, sizeof(args));
801 args.addr = (uintptr_t)pointer;
802 args.size = align(size, sysconf(_SC_PAGE_SIZE));
803 args.flags = RADEON_GEM_USERPTR_ANONONLY |
804 RADEON_GEM_USERPTR_VALIDATE |
805 RADEON_GEM_USERPTR_REGISTER;
806 if (drmCommandWriteRead(ws->fd, DRM_RADEON_GEM_USERPTR,
807 &args, sizeof(args))) {
808 FREE(bo);
809 return NULL;
810 }
811
812 pipe_mutex_lock(ws->bo_handles_mutex);
813
814 /* Initialize it. */
815 pipe_reference_init(&bo->base.reference, 1);
816 bo->handle = args.handle;
817 bo->base.alignment = 0;
818 bo->base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
819 bo->base.size = size;
820 bo->base.vtbl = &radeon_bo_vtbl;
821 bo->rws = ws;
822 bo->user_ptr = pointer;
823 bo->va = 0;
824 bo->initial_domain = RADEON_DOMAIN_GTT;
825 pipe_mutex_init(bo->map_mutex);
826
827 util_hash_table_set(ws->bo_handles, (void*)(uintptr_t)bo->handle, bo);
828
829 pipe_mutex_unlock(ws->bo_handles_mutex);
830
831 if (ws->info.r600_virtual_address) {
832 struct drm_radeon_gem_va va;
833
834 bo->va = radeon_bomgr_find_va(rws, bo->base.size, 1 << 20);
835
836 va.handle = bo->handle;
837 va.operation = RADEON_VA_MAP;
838 va.vm_id = 0;
839 va.offset = bo->va;
840 va.flags = RADEON_VM_PAGE_READABLE |
841 RADEON_VM_PAGE_WRITEABLE |
842 RADEON_VM_PAGE_SNOOPED;
843 va.offset = bo->va;
844 r = drmCommandWriteRead(ws->fd, DRM_RADEON_GEM_VA, &va, sizeof(va));
845 if (r && va.operation == RADEON_VA_RESULT_ERROR) {
846 fprintf(stderr, "radeon: Failed to assign virtual address space\n");
847 radeon_bo_destroy(&bo->base);
848 return NULL;
849 }
850 pipe_mutex_lock(ws->bo_handles_mutex);
851 if (va.operation == RADEON_VA_RESULT_VA_EXIST) {
852 struct pb_buffer *b = &bo->base;
853 struct radeon_bo *old_bo =
854 util_hash_table_get(ws->bo_vas, (void*)(uintptr_t)va.offset);
855
856 pipe_mutex_unlock(ws->bo_handles_mutex);
857 pb_reference(&b, &old_bo->base);
858 return b;
859 }
860
861 util_hash_table_set(ws->bo_vas, (void*)(uintptr_t)bo->va, bo);
862 pipe_mutex_unlock(ws->bo_handles_mutex);
863 }
864
865 ws->allocated_gtt += align(bo->base.size, ws->size_align);
866
867 return (struct pb_buffer*)bo;
868 }
869
870 static struct pb_buffer *radeon_winsys_bo_from_handle(struct radeon_winsys *rws,
871 struct winsys_handle *whandle,
872 unsigned *stride)
873 {
874 struct radeon_drm_winsys *ws = radeon_drm_winsys(rws);
875 struct radeon_bo *bo;
876 int r;
877 unsigned handle;
878 uint64_t size = 0;
879
880 /* We must maintain a list of pairs <handle, bo>, so that we always return
881 * the same BO for one particular handle. If we didn't do that and created
882 * more than one BO for the same handle and then relocated them in a CS,
883 * we would hit a deadlock in the kernel.
884 *
885 * The list of pairs is guarded by a mutex, of course. */
886 pipe_mutex_lock(ws->bo_handles_mutex);
887
888 if (whandle->type == DRM_API_HANDLE_TYPE_SHARED) {
889 /* First check if there already is an existing bo for the handle. */
890 bo = util_hash_table_get(ws->bo_names, (void*)(uintptr_t)whandle->handle);
891 } else if (whandle->type == DRM_API_HANDLE_TYPE_FD) {
892 /* We must first get the GEM handle, as fds are unreliable keys */
893 r = drmPrimeFDToHandle(ws->fd, whandle->handle, &handle);
894 if (r)
895 goto fail;
896 bo = util_hash_table_get(ws->bo_handles, (void*)(uintptr_t)handle);
897 } else {
898 /* Unknown handle type */
899 goto fail;
900 }
901
902 if (bo) {
903 /* Increase the refcount. */
904 struct pb_buffer *b = NULL;
905 pb_reference(&b, &bo->base);
906 goto done;
907 }
908
909 /* There isn't, create a new one. */
910 bo = CALLOC_STRUCT(radeon_bo);
911 if (!bo) {
912 goto fail;
913 }
914
915 if (whandle->type == DRM_API_HANDLE_TYPE_SHARED) {
916 struct drm_gem_open open_arg = {};
917 memset(&open_arg, 0, sizeof(open_arg));
918 /* Open the BO. */
919 open_arg.name = whandle->handle;
920 if (drmIoctl(ws->fd, DRM_IOCTL_GEM_OPEN, &open_arg)) {
921 FREE(bo);
922 goto fail;
923 }
924 handle = open_arg.handle;
925 size = open_arg.size;
926 bo->flink_name = whandle->handle;
927 } else if (whandle->type == DRM_API_HANDLE_TYPE_FD) {
928 size = lseek(whandle->handle, 0, SEEK_END);
929 /*
930 * Could check errno to determine whether the kernel is new enough, but
931 * it doesn't really matter why this failed, just that it failed.
932 */
933 if (size == (off_t)-1) {
934 FREE(bo);
935 goto fail;
936 }
937 lseek(whandle->handle, 0, SEEK_SET);
938 }
939
940 bo->handle = handle;
941
942 /* Initialize it. */
943 pipe_reference_init(&bo->base.reference, 1);
944 bo->base.alignment = 0;
945 bo->base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
946 bo->base.size = (unsigned) size;
947 bo->base.vtbl = &radeon_bo_vtbl;
948 bo->rws = ws;
949 bo->va = 0;
950 pipe_mutex_init(bo->map_mutex);
951
952 if (bo->flink_name)
953 util_hash_table_set(ws->bo_names, (void*)(uintptr_t)bo->flink_name, bo);
954
955 util_hash_table_set(ws->bo_handles, (void*)(uintptr_t)bo->handle, bo);
956
957 done:
958 pipe_mutex_unlock(ws->bo_handles_mutex);
959
960 if (stride)
961 *stride = whandle->stride;
962
963 if (ws->info.r600_virtual_address && !bo->va) {
964 struct drm_radeon_gem_va va;
965
966 bo->va = radeon_bomgr_find_va(rws, bo->base.size, 1 << 20);
967
968 va.handle = bo->handle;
969 va.operation = RADEON_VA_MAP;
970 va.vm_id = 0;
971 va.offset = bo->va;
972 va.flags = RADEON_VM_PAGE_READABLE |
973 RADEON_VM_PAGE_WRITEABLE |
974 RADEON_VM_PAGE_SNOOPED;
975 va.offset = bo->va;
976 r = drmCommandWriteRead(ws->fd, DRM_RADEON_GEM_VA, &va, sizeof(va));
977 if (r && va.operation == RADEON_VA_RESULT_ERROR) {
978 fprintf(stderr, "radeon: Failed to assign virtual address space\n");
979 radeon_bo_destroy(&bo->base);
980 return NULL;
981 }
982 pipe_mutex_lock(ws->bo_handles_mutex);
983 if (va.operation == RADEON_VA_RESULT_VA_EXIST) {
984 struct pb_buffer *b = &bo->base;
985 struct radeon_bo *old_bo =
986 util_hash_table_get(ws->bo_vas, (void*)(uintptr_t)va.offset);
987
988 pipe_mutex_unlock(ws->bo_handles_mutex);
989 pb_reference(&b, &old_bo->base);
990 return b;
991 }
992
993 util_hash_table_set(ws->bo_vas, (void*)(uintptr_t)bo->va, bo);
994 pipe_mutex_unlock(ws->bo_handles_mutex);
995 }
996
997 bo->initial_domain = radeon_bo_get_initial_domain((void*)bo);
998
999 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
1000 ws->allocated_vram += align(bo->base.size, ws->size_align);
1001 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
1002 ws->allocated_gtt += align(bo->base.size, ws->size_align);
1003
1004 return (struct pb_buffer*)bo;
1005
1006 fail:
1007 pipe_mutex_unlock(ws->bo_handles_mutex);
1008 return NULL;
1009 }
1010
1011 static boolean radeon_winsys_bo_get_handle(struct pb_buffer *buffer,
1012 unsigned stride,
1013 struct winsys_handle *whandle)
1014 {
1015 struct drm_gem_flink flink;
1016 struct radeon_bo *bo = radeon_bo(buffer);
1017 struct radeon_drm_winsys *ws = bo->rws;
1018
1019 memset(&flink, 0, sizeof(flink));
1020
1021 bo->use_reusable_pool = false;
1022
1023 if (whandle->type == DRM_API_HANDLE_TYPE_SHARED) {
1024 if (!bo->flink_name) {
1025 flink.handle = bo->handle;
1026
1027 if (ioctl(ws->fd, DRM_IOCTL_GEM_FLINK, &flink)) {
1028 return FALSE;
1029 }
1030
1031 bo->flink_name = flink.name;
1032
1033 pipe_mutex_lock(ws->bo_handles_mutex);
1034 util_hash_table_set(ws->bo_names, (void*)(uintptr_t)bo->flink_name, bo);
1035 pipe_mutex_unlock(ws->bo_handles_mutex);
1036 }
1037 whandle->handle = bo->flink_name;
1038 } else if (whandle->type == DRM_API_HANDLE_TYPE_KMS) {
1039 whandle->handle = bo->handle;
1040 } else if (whandle->type == DRM_API_HANDLE_TYPE_FD) {
1041 if (drmPrimeHandleToFD(ws->fd, bo->handle, DRM_CLOEXEC, (int*)&whandle->handle))
1042 return FALSE;
1043 }
1044
1045 whandle->stride = stride;
1046 return TRUE;
1047 }
1048
1049 static uint64_t radeon_winsys_bo_va(struct radeon_winsys_cs_handle *buf)
1050 {
1051 return ((struct radeon_bo*)buf)->va;
1052 }
1053
1054 void radeon_drm_bo_init_functions(struct radeon_drm_winsys *ws)
1055 {
1056 ws->base.buffer_get_cs_handle = radeon_drm_get_cs_handle;
1057 ws->base.buffer_set_tiling = radeon_bo_set_tiling;
1058 ws->base.buffer_get_tiling = radeon_bo_get_tiling;
1059 ws->base.buffer_map = radeon_bo_map;
1060 ws->base.buffer_unmap = radeon_bo_unmap;
1061 ws->base.buffer_wait = radeon_bo_wait;
1062 ws->base.buffer_create = radeon_winsys_bo_create;
1063 ws->base.buffer_from_handle = radeon_winsys_bo_from_handle;
1064 ws->base.buffer_from_ptr = radeon_winsys_bo_from_ptr;
1065 ws->base.buffer_get_handle = radeon_winsys_bo_get_handle;
1066 ws->base.buffer_get_virtual_address = radeon_winsys_bo_va;
1067 ws->base.buffer_get_initial_domain = radeon_bo_get_initial_domain;
1068 }