Merge branch 'draw-instanced'
[mesa.git] / src / gallium / winsys / r600 / drm / r600_bomgr.c
1 /*
2 * Copyright 2010 VMWare.
3 * Copyright 2010 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Jose Fonseca <jrfonseca-at-vmware-dot-com>
26 * Thomas Hellström <thomas-at-vmware-dot-com>
27 * Jerome Glisse <jglisse@redhat.com>
28 */
29 #include <util/u_memory.h>
30 #include <util/u_double_list.h>
31 #include <util/u_time.h>
32 #include <pipebuffer/pb_bufmgr.h>
33 #include "r600_priv.h"
34
35 static void r600_bomgr_timeout_flush(struct r600_bomgr *mgr)
36 {
37 struct r600_bo *bo, *tmp;
38 int64_t now;
39
40 now = os_time_get();
41 LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &mgr->delayed, list) {
42 if(!os_time_timeout(bo->start, bo->end, now))
43 break;
44
45 mgr->num_delayed--;
46 bo->manager_id = 0;
47 LIST_DEL(&bo->list);
48 r600_bo_destroy(mgr->radeon, bo);
49 }
50 }
51
52 static INLINE int r600_bo_is_compat(struct r600_bomgr *mgr,
53 struct r600_bo *bo,
54 unsigned size,
55 unsigned alignment,
56 unsigned cfence)
57 {
58 if(bo->size < size) {
59 return 0;
60 }
61
62 /* be lenient with size */
63 if(bo->size >= 2*size) {
64 return 0;
65 }
66
67 if(!pb_check_alignment(alignment, bo->alignment)) {
68 return 0;
69 }
70
71 if (!fence_is_after(cfence, bo->fence)) {
72 return 0;
73 }
74
75 return 1;
76 }
77
78 struct r600_bo *r600_bomgr_bo_create(struct r600_bomgr *mgr,
79 unsigned size,
80 unsigned alignment,
81 unsigned cfence)
82 {
83 struct r600_bo *bo, *tmp;
84 int64_t now;
85
86
87 pipe_mutex_lock(mgr->mutex);
88
89 now = os_time_get();
90 LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &mgr->delayed, list) {
91 if(r600_bo_is_compat(mgr, bo, size, alignment, cfence)) {
92 LIST_DEL(&bo->list);
93 --mgr->num_delayed;
94 r600_bomgr_timeout_flush(mgr);
95 pipe_mutex_unlock(mgr->mutex);
96 LIST_INITHEAD(&bo->list);
97 pipe_reference_init(&bo->reference, 1);
98 return bo;
99 }
100
101 if(os_time_timeout(bo->start, bo->end, now)) {
102 mgr->num_delayed--;
103 bo->manager_id = 0;
104 LIST_DEL(&bo->list);
105 r600_bo_destroy(mgr->radeon, bo);
106 }
107 }
108
109 pipe_mutex_unlock(mgr->mutex);
110 return NULL;
111 }
112
113 void r600_bomgr_bo_init(struct r600_bomgr *mgr, struct r600_bo *bo)
114 {
115 LIST_INITHEAD(&bo->list);
116 bo->manager_id = 1;
117 }
118
119 bool r600_bomgr_bo_destroy(struct r600_bomgr *mgr, struct r600_bo *bo)
120 {
121 bo->start = os_time_get();
122 bo->end = bo->start + mgr->usecs;
123 pipe_mutex_lock(mgr->mutex);
124 LIST_ADDTAIL(&bo->list, &mgr->delayed);
125 ++mgr->num_delayed;
126 pipe_mutex_unlock(mgr->mutex);
127 return FALSE;
128 }
129
130 void r600_bomgr_destroy(struct r600_bomgr *mgr)
131 {
132 struct r600_bo *bo, *tmp;
133
134 pipe_mutex_lock(mgr->mutex);
135 LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &mgr->delayed, list) {
136 mgr->num_delayed--;
137 bo->manager_id = 0;
138 LIST_DEL(&bo->list);
139 r600_bo_destroy(mgr->radeon, bo);
140 }
141 pipe_mutex_unlock(mgr->mutex);
142
143 FREE(mgr);
144 }
145
146 struct r600_bomgr *r600_bomgr_create(struct radeon *radeon, unsigned usecs)
147 {
148 struct r600_bomgr *mgr;
149
150 mgr = CALLOC_STRUCT(r600_bomgr);
151 if (mgr == NULL)
152 return NULL;
153
154 mgr->radeon = radeon;
155 mgr->usecs = usecs;
156 LIST_INITHEAD(&mgr->delayed);
157 mgr->num_delayed = 0;
158 pipe_mutex_init(mgr->mutex);
159
160 return mgr;
161 }