blob: 69287be5bc89f815eb6e33510d697878c6bdaaa9 [file] [log] [blame]
Jerome Glisse6bf1ed22010-01-14 11:24:16 +01001/*
Dave Airlie39970c62009-07-06 13:34:24 +10002 * Copyright © 2009 Red Hat Inc.
3 * All Rights Reserved.
Jerome Glisse6bf1ed22010-01-14 11:24:16 +01004 *
Dave Airlie39970c62009-07-06 13:34:24 +10005 * 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:
Jerome Glisse6bf1ed22010-01-14 11:24:16 +010012 *
Dave Airlie39970c62009-07-06 13:34:24 +100013 * 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,
Jerome Glisse6bf1ed22010-01-14 11:24:16 +010019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
Dave Airlie39970c62009-07-06 13:34:24 +100020 * 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 */
Maarten Lankhorst58ce9d62014-07-31 15:39:15 +020028#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
Dave Airlie39970c62009-07-06 13:34:24 +100031#include <assert.h>
32#include <errno.h>
33#include <stdlib.h>
Emil Velikov42465fe2015-04-05 15:51:59 +010034#include "libdrm_macros.h"
Dave Airlie39970c62009-07-06 13:34:24 +100035#include "radeon_cs.h"
Dave Airlie125994a2009-12-17 14:11:55 +100036#include "radeon_bo_int.h"
37#include "radeon_cs_int.h"
Dave Airlie39970c62009-07-06 13:34:24 +100038
39struct rad_sizes {
40 int32_t op_read;
41 int32_t op_gart_write;
42 int32_t op_vram_write;
43};
44
45static inline int radeon_cs_setup_bo(struct radeon_cs_space_check *sc, struct rad_sizes *sizes)
46{
47 uint32_t read_domains, write_domain;
Dave Airlie125994a2009-12-17 14:11:55 +100048 struct radeon_bo_int *bo;
Dave Airlie39970c62009-07-06 13:34:24 +100049
50 bo = sc->bo;
51 sc->new_accounted = 0;
52 read_domains = sc->read_domains;
53 write_domain = sc->write_domain;
54
55 /* legacy needs a static check */
Dave Airlie125994a2009-12-17 14:11:55 +100056 if (radeon_bo_is_static((struct radeon_bo *)sc->bo)) {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +010057 bo->space_accounted = sc->new_accounted = (read_domains << 16) | write_domain;
58 return 0;
Dave Airlie39970c62009-07-06 13:34:24 +100059 }
60
61 /* already accounted this bo */
62 if (write_domain && (write_domain == bo->space_accounted)) {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +010063 sc->new_accounted = bo->space_accounted;
64 return 0;
Dave Airlie39970c62009-07-06 13:34:24 +100065 }
66 if (read_domains && ((read_domains << 16) == bo->space_accounted)) {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +010067 sc->new_accounted = bo->space_accounted;
68 return 0;
Dave Airlie39970c62009-07-06 13:34:24 +100069 }
70
71 if (bo->space_accounted == 0) {
Michel Dänzer2cfac572012-02-08 10:49:08 +010072 if (write_domain) {
73 if (write_domain == RADEON_GEM_DOMAIN_VRAM)
74 sizes->op_vram_write += bo->size;
75 else if (write_domain == RADEON_GEM_DOMAIN_GTT)
76 sizes->op_gart_write += bo->size;
77 sc->new_accounted = write_domain;
78 } else {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +010079 sizes->op_read += bo->size;
Michel Dänzer2cfac572012-02-08 10:49:08 +010080 sc->new_accounted = read_domains << 16;
81 }
Dave Airlie39970c62009-07-06 13:34:24 +100082 } else {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +010083 uint16_t old_read, old_write;
84
85 old_read = bo->space_accounted >> 16;
86 old_write = bo->space_accounted & 0xffff;
87
88 if (write_domain && (old_read & write_domain)) {
89 sc->new_accounted = write_domain;
90 /* moving from read to a write domain */
91 if (write_domain == RADEON_GEM_DOMAIN_VRAM) {
92 sizes->op_read -= bo->size;
93 sizes->op_vram_write += bo->size;
94 } else if (write_domain == RADEON_GEM_DOMAIN_GTT) {
95 sizes->op_read -= bo->size;
96 sizes->op_gart_write += bo->size;
97 }
98 } else if (read_domains & old_write) {
99 sc->new_accounted = bo->space_accounted & 0xffff;
100 } else {
101 /* rewrite the domains */
102 if (write_domain != old_write)
103 fprintf(stderr,"WRITE DOMAIN RELOC FAILURE 0x%x %d %d\n", bo->handle, write_domain, old_write);
104 if (read_domains != old_read)
105 fprintf(stderr,"READ DOMAIN RELOC FAILURE 0x%x %d %d\n", bo->handle, read_domains, old_read);
106 return RADEON_CS_SPACE_FLUSH;
107 }
Dave Airlie39970c62009-07-06 13:34:24 +1000108 }
109 return 0;
110}
111
Dave Airlie125994a2009-12-17 14:11:55 +1000112static int radeon_cs_do_space_check(struct radeon_cs_int *cs, struct radeon_cs_space_check *new_tmp)
Dave Airlie39970c62009-07-06 13:34:24 +1000113{
114 struct radeon_cs_manager *csm = cs->csm;
115 int i;
Dave Airlie125994a2009-12-17 14:11:55 +1000116 struct radeon_bo_int *bo;
Dave Airlie39970c62009-07-06 13:34:24 +1000117 struct rad_sizes sizes;
118 int ret;
119
120 /* check the totals for this operation */
121
122 if (cs->bo_count == 0 && !new_tmp)
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100123 return 0;
Dave Airlie39970c62009-07-06 13:34:24 +1000124
125 memset(&sizes, 0, sizeof(struct rad_sizes));
126
127 /* prepare */
128 for (i = 0; i < cs->bo_count; i++) {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100129 ret = radeon_cs_setup_bo(&cs->bos[i], &sizes);
130 if (ret)
131 return ret;
Dave Airlie39970c62009-07-06 13:34:24 +1000132 }
133
134 if (new_tmp) {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100135 ret = radeon_cs_setup_bo(new_tmp, &sizes);
136 if (ret)
137 return ret;
Dave Airlie39970c62009-07-06 13:34:24 +1000138 }
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100139
Dave Airlie39970c62009-07-06 13:34:24 +1000140 if (sizes.op_read < 0)
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100141 sizes.op_read = 0;
Dave Airlie39970c62009-07-06 13:34:24 +1000142
143 /* check sizes - operation first */
144 if ((sizes.op_read + sizes.op_gart_write > csm->gart_limit) ||
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100145 (sizes.op_vram_write > csm->vram_limit)) {
146 return RADEON_CS_SPACE_OP_TO_BIG;
Dave Airlie39970c62009-07-06 13:34:24 +1000147 }
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100148
Dave Airlie39970c62009-07-06 13:34:24 +1000149 if (((csm->vram_write_used + sizes.op_vram_write) > csm->vram_limit) ||
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100150 ((csm->read_used + csm->gart_write_used + sizes.op_gart_write + sizes.op_read) > csm->gart_limit)) {
151 return RADEON_CS_SPACE_FLUSH;
Dave Airlie39970c62009-07-06 13:34:24 +1000152 }
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100153
Dave Airlie39970c62009-07-06 13:34:24 +1000154 csm->gart_write_used += sizes.op_gart_write;
155 csm->vram_write_used += sizes.op_vram_write;
156 csm->read_used += sizes.op_read;
157 /* commit */
158 for (i = 0; i < cs->bo_count; i++) {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100159 bo = cs->bos[i].bo;
160 bo->space_accounted = cs->bos[i].new_accounted;
Dave Airlie39970c62009-07-06 13:34:24 +1000161 }
162 if (new_tmp)
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100163 new_tmp->bo->space_accounted = new_tmp->new_accounted;
164
Dave Airlie39970c62009-07-06 13:34:24 +1000165 return RADEON_CS_SPACE_OK;
166}
167
Emil Velikov0f8da822015-03-31 22:32:11 +0100168void
Maarten Lankhorst58ce9d62014-07-31 15:39:15 +0200169radeon_cs_space_add_persistent_bo(struct radeon_cs *cs, struct radeon_bo *bo,
170 uint32_t read_domains, uint32_t write_domain)
Dave Airlie39970c62009-07-06 13:34:24 +1000171{
Dave Airlie125994a2009-12-17 14:11:55 +1000172 struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
173 struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
Dave Airlie39970c62009-07-06 13:34:24 +1000174 int i;
Dave Airlie125994a2009-12-17 14:11:55 +1000175 for (i = 0; i < csi->bo_count; i++) {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100176 if (csi->bos[i].bo == boi &&
177 csi->bos[i].read_domains == read_domains &&
178 csi->bos[i].write_domain == write_domain)
179 return;
Dave Airlie39970c62009-07-06 13:34:24 +1000180 }
181 radeon_bo_ref(bo);
Dave Airlie125994a2009-12-17 14:11:55 +1000182 i = csi->bo_count;
183 csi->bos[i].bo = boi;
184 csi->bos[i].read_domains = read_domains;
185 csi->bos[i].write_domain = write_domain;
186 csi->bos[i].new_accounted = 0;
187 csi->bo_count++;
Dave Airlie39970c62009-07-06 13:34:24 +1000188
Dave Airlie125994a2009-12-17 14:11:55 +1000189 assert(csi->bo_count < MAX_SPACE_BOS);
Dave Airlie39970c62009-07-06 13:34:24 +1000190}
191
Dave Airlie125994a2009-12-17 14:11:55 +1000192static int radeon_cs_check_space_internal(struct radeon_cs_int *cs,
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100193 struct radeon_cs_space_check *tmp_bo)
Dave Airlie39970c62009-07-06 13:34:24 +1000194{
195 int ret;
196 int flushed = 0;
197
198again:
199 ret = radeon_cs_do_space_check(cs, tmp_bo);
200 if (ret == RADEON_CS_SPACE_OP_TO_BIG)
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100201 return -1;
Dave Airlie39970c62009-07-06 13:34:24 +1000202 if (ret == RADEON_CS_SPACE_FLUSH) {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100203 (*cs->space_flush_fn)(cs->space_flush_data);
204 if (flushed)
205 return -1;
206 flushed = 1;
207 goto again;
Dave Airlie39970c62009-07-06 13:34:24 +1000208 }
209 return 0;
210}
211
Emil Velikov0f8da822015-03-31 22:32:11 +0100212int
Maarten Lankhorst58ce9d62014-07-31 15:39:15 +0200213radeon_cs_space_check_with_bo(struct radeon_cs *cs, struct radeon_bo *bo,
214 uint32_t read_domains, uint32_t write_domain)
Dave Airlie125994a2009-12-17 14:11:55 +1000215{
216 struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
217 struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
Dave Airlie39970c62009-07-06 13:34:24 +1000218 struct radeon_cs_space_check temp_bo;
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100219
Dave Airlie39970c62009-07-06 13:34:24 +1000220 int ret = 0;
221
222 if (bo) {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100223 temp_bo.bo = boi;
224 temp_bo.read_domains = read_domains;
225 temp_bo.write_domain = write_domain;
226 temp_bo.new_accounted = 0;
Dave Airlie39970c62009-07-06 13:34:24 +1000227 }
228
Dave Airlie125994a2009-12-17 14:11:55 +1000229 ret = radeon_cs_check_space_internal(csi, bo ? &temp_bo : NULL);
Dave Airlie39970c62009-07-06 13:34:24 +1000230 return ret;
231}
232
Emil Velikov0f8da822015-03-31 22:32:11 +0100233int radeon_cs_space_check(struct radeon_cs *cs)
Dave Airlie39970c62009-07-06 13:34:24 +1000234{
Dave Airlie125994a2009-12-17 14:11:55 +1000235 struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
236 return radeon_cs_check_space_internal(csi, NULL);
Dave Airlie39970c62009-07-06 13:34:24 +1000237}
238
Emil Velikov0f8da822015-03-31 22:32:11 +0100239void radeon_cs_space_reset_bos(struct radeon_cs *cs)
Dave Airlie39970c62009-07-06 13:34:24 +1000240{
Dave Airlie125994a2009-12-17 14:11:55 +1000241 struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
Dave Airlie39970c62009-07-06 13:34:24 +1000242 int i;
Dave Airlie125994a2009-12-17 14:11:55 +1000243 for (i = 0; i < csi->bo_count; i++) {
Jerome Glisse6bf1ed22010-01-14 11:24:16 +0100244 radeon_bo_unref((struct radeon_bo *)csi->bos[i].bo);
245 csi->bos[i].bo = NULL;
246 csi->bos[i].read_domains = 0;
247 csi->bos[i].write_domain = 0;
248 csi->bos[i].new_accounted = 0;
Dave Airlie39970c62009-07-06 13:34:24 +1000249 }
Dave Airlie125994a2009-12-17 14:11:55 +1000250 csi->bo_count = 0;
Dave Airlie39970c62009-07-06 13:34:24 +1000251}