2010-01-14 03:24:16 -07:00
|
|
|
/*
|
2009-07-05 21:34:24 -06:00
|
|
|
* Copyright © 2009 Red Hat Inc.
|
|
|
|
* All Rights Reserved.
|
2010-01-14 03:24:16 -07:00
|
|
|
*
|
2009-07-05 21:34:24 -06:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
2010-01-14 03:24:16 -07:00
|
|
|
*
|
2009-07-05 21:34:24 -06:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
|
|
|
|
* AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
2010-01-14 03:24:16 -07:00
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
2009-07-05 21:34:24 -06:00
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
*/
|
2014-07-31 07:39:15 -06:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2009-07-05 21:34:24 -06:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2015-04-05 08:51:59 -06:00
|
|
|
#include "libdrm_macros.h"
|
2009-07-05 21:34:24 -06:00
|
|
|
#include "radeon_cs.h"
|
2009-12-16 21:11:55 -07:00
|
|
|
#include "radeon_bo_int.h"
|
|
|
|
#include "radeon_cs_int.h"
|
2009-07-05 21:34:24 -06:00
|
|
|
|
|
|
|
struct rad_sizes {
|
|
|
|
int32_t op_read;
|
|
|
|
int32_t op_gart_write;
|
|
|
|
int32_t op_vram_write;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline int radeon_cs_setup_bo(struct radeon_cs_space_check *sc, struct rad_sizes *sizes)
|
|
|
|
{
|
|
|
|
uint32_t read_domains, write_domain;
|
2009-12-16 21:11:55 -07:00
|
|
|
struct radeon_bo_int *bo;
|
2009-07-05 21:34:24 -06:00
|
|
|
|
|
|
|
bo = sc->bo;
|
|
|
|
sc->new_accounted = 0;
|
|
|
|
read_domains = sc->read_domains;
|
|
|
|
write_domain = sc->write_domain;
|
|
|
|
|
|
|
|
/* legacy needs a static check */
|
2009-12-16 21:11:55 -07:00
|
|
|
if (radeon_bo_is_static((struct radeon_bo *)sc->bo)) {
|
2010-01-14 03:24:16 -07:00
|
|
|
bo->space_accounted = sc->new_accounted = (read_domains << 16) | write_domain;
|
|
|
|
return 0;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* already accounted this bo */
|
|
|
|
if (write_domain && (write_domain == bo->space_accounted)) {
|
2010-01-14 03:24:16 -07:00
|
|
|
sc->new_accounted = bo->space_accounted;
|
|
|
|
return 0;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
if (read_domains && ((read_domains << 16) == bo->space_accounted)) {
|
2010-01-14 03:24:16 -07:00
|
|
|
sc->new_accounted = bo->space_accounted;
|
|
|
|
return 0;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bo->space_accounted == 0) {
|
2012-02-08 02:49:08 -07:00
|
|
|
if (write_domain) {
|
|
|
|
if (write_domain == RADEON_GEM_DOMAIN_VRAM)
|
|
|
|
sizes->op_vram_write += bo->size;
|
|
|
|
else if (write_domain == RADEON_GEM_DOMAIN_GTT)
|
|
|
|
sizes->op_gart_write += bo->size;
|
|
|
|
sc->new_accounted = write_domain;
|
|
|
|
} else {
|
2010-01-14 03:24:16 -07:00
|
|
|
sizes->op_read += bo->size;
|
2012-02-08 02:49:08 -07:00
|
|
|
sc->new_accounted = read_domains << 16;
|
|
|
|
}
|
2009-07-05 21:34:24 -06:00
|
|
|
} else {
|
2010-01-14 03:24:16 -07:00
|
|
|
uint16_t old_read, old_write;
|
|
|
|
|
|
|
|
old_read = bo->space_accounted >> 16;
|
|
|
|
old_write = bo->space_accounted & 0xffff;
|
|
|
|
|
|
|
|
if (write_domain && (old_read & write_domain)) {
|
|
|
|
sc->new_accounted = write_domain;
|
|
|
|
/* moving from read to a write domain */
|
|
|
|
if (write_domain == RADEON_GEM_DOMAIN_VRAM) {
|
|
|
|
sizes->op_read -= bo->size;
|
|
|
|
sizes->op_vram_write += bo->size;
|
|
|
|
} else if (write_domain == RADEON_GEM_DOMAIN_GTT) {
|
|
|
|
sizes->op_read -= bo->size;
|
|
|
|
sizes->op_gart_write += bo->size;
|
|
|
|
}
|
|
|
|
} else if (read_domains & old_write) {
|
|
|
|
sc->new_accounted = bo->space_accounted & 0xffff;
|
|
|
|
} else {
|
|
|
|
/* rewrite the domains */
|
|
|
|
if (write_domain != old_write)
|
|
|
|
fprintf(stderr,"WRITE DOMAIN RELOC FAILURE 0x%x %d %d\n", bo->handle, write_domain, old_write);
|
|
|
|
if (read_domains != old_read)
|
|
|
|
fprintf(stderr,"READ DOMAIN RELOC FAILURE 0x%x %d %d\n", bo->handle, read_domains, old_read);
|
|
|
|
return RADEON_CS_SPACE_FLUSH;
|
|
|
|
}
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-16 21:11:55 -07:00
|
|
|
static int radeon_cs_do_space_check(struct radeon_cs_int *cs, struct radeon_cs_space_check *new_tmp)
|
2009-07-05 21:34:24 -06:00
|
|
|
{
|
|
|
|
struct radeon_cs_manager *csm = cs->csm;
|
|
|
|
int i;
|
2009-12-16 21:11:55 -07:00
|
|
|
struct radeon_bo_int *bo;
|
2009-07-05 21:34:24 -06:00
|
|
|
struct rad_sizes sizes;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* check the totals for this operation */
|
|
|
|
|
|
|
|
if (cs->bo_count == 0 && !new_tmp)
|
2010-01-14 03:24:16 -07:00
|
|
|
return 0;
|
2009-07-05 21:34:24 -06:00
|
|
|
|
|
|
|
memset(&sizes, 0, sizeof(struct rad_sizes));
|
|
|
|
|
|
|
|
/* prepare */
|
|
|
|
for (i = 0; i < cs->bo_count; i++) {
|
2010-01-14 03:24:16 -07:00
|
|
|
ret = radeon_cs_setup_bo(&cs->bos[i], &sizes);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_tmp) {
|
2010-01-14 03:24:16 -07:00
|
|
|
ret = radeon_cs_setup_bo(new_tmp, &sizes);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
2010-01-14 03:24:16 -07:00
|
|
|
|
2009-07-05 21:34:24 -06:00
|
|
|
if (sizes.op_read < 0)
|
2010-01-14 03:24:16 -07:00
|
|
|
sizes.op_read = 0;
|
2009-07-05 21:34:24 -06:00
|
|
|
|
|
|
|
/* check sizes - operation first */
|
|
|
|
if ((sizes.op_read + sizes.op_gart_write > csm->gart_limit) ||
|
2010-01-14 03:24:16 -07:00
|
|
|
(sizes.op_vram_write > csm->vram_limit)) {
|
|
|
|
return RADEON_CS_SPACE_OP_TO_BIG;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
2010-01-14 03:24:16 -07:00
|
|
|
|
2009-07-05 21:34:24 -06:00
|
|
|
if (((csm->vram_write_used + sizes.op_vram_write) > csm->vram_limit) ||
|
2010-01-14 03:24:16 -07:00
|
|
|
((csm->read_used + csm->gart_write_used + sizes.op_gart_write + sizes.op_read) > csm->gart_limit)) {
|
|
|
|
return RADEON_CS_SPACE_FLUSH;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
2010-01-14 03:24:16 -07:00
|
|
|
|
2009-07-05 21:34:24 -06:00
|
|
|
csm->gart_write_used += sizes.op_gart_write;
|
|
|
|
csm->vram_write_used += sizes.op_vram_write;
|
|
|
|
csm->read_used += sizes.op_read;
|
|
|
|
/* commit */
|
|
|
|
for (i = 0; i < cs->bo_count; i++) {
|
2010-01-14 03:24:16 -07:00
|
|
|
bo = cs->bos[i].bo;
|
|
|
|
bo->space_accounted = cs->bos[i].new_accounted;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
if (new_tmp)
|
2010-01-14 03:24:16 -07:00
|
|
|
new_tmp->bo->space_accounted = new_tmp->new_accounted;
|
|
|
|
|
2009-07-05 21:34:24 -06:00
|
|
|
return RADEON_CS_SPACE_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-31 15:32:11 -06:00
|
|
|
void
|
2014-07-31 07:39:15 -06:00
|
|
|
radeon_cs_space_add_persistent_bo(struct radeon_cs *cs, struct radeon_bo *bo,
|
|
|
|
uint32_t read_domains, uint32_t write_domain)
|
2009-07-05 21:34:24 -06:00
|
|
|
{
|
2009-12-16 21:11:55 -07:00
|
|
|
struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
|
|
|
|
struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
|
2009-07-05 21:34:24 -06:00
|
|
|
int i;
|
2009-12-16 21:11:55 -07:00
|
|
|
for (i = 0; i < csi->bo_count; i++) {
|
2010-01-14 03:24:16 -07:00
|
|
|
if (csi->bos[i].bo == boi &&
|
|
|
|
csi->bos[i].read_domains == read_domains &&
|
|
|
|
csi->bos[i].write_domain == write_domain)
|
|
|
|
return;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
radeon_bo_ref(bo);
|
2009-12-16 21:11:55 -07:00
|
|
|
i = csi->bo_count;
|
|
|
|
csi->bos[i].bo = boi;
|
|
|
|
csi->bos[i].read_domains = read_domains;
|
|
|
|
csi->bos[i].write_domain = write_domain;
|
|
|
|
csi->bos[i].new_accounted = 0;
|
|
|
|
csi->bo_count++;
|
|
|
|
|
|
|
|
assert(csi->bo_count < MAX_SPACE_BOS);
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
|
2009-12-16 21:11:55 -07:00
|
|
|
static int radeon_cs_check_space_internal(struct radeon_cs_int *cs,
|
2010-01-14 03:24:16 -07:00
|
|
|
struct radeon_cs_space_check *tmp_bo)
|
2009-07-05 21:34:24 -06:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int flushed = 0;
|
|
|
|
|
|
|
|
again:
|
|
|
|
ret = radeon_cs_do_space_check(cs, tmp_bo);
|
|
|
|
if (ret == RADEON_CS_SPACE_OP_TO_BIG)
|
2010-01-14 03:24:16 -07:00
|
|
|
return -1;
|
2009-07-05 21:34:24 -06:00
|
|
|
if (ret == RADEON_CS_SPACE_FLUSH) {
|
2010-01-14 03:24:16 -07:00
|
|
|
(*cs->space_flush_fn)(cs->space_flush_data);
|
|
|
|
if (flushed)
|
|
|
|
return -1;
|
|
|
|
flushed = 1;
|
|
|
|
goto again;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-31 15:32:11 -06:00
|
|
|
int
|
2014-07-31 07:39:15 -06:00
|
|
|
radeon_cs_space_check_with_bo(struct radeon_cs *cs, struct radeon_bo *bo,
|
|
|
|
uint32_t read_domains, uint32_t write_domain)
|
2009-12-16 21:11:55 -07:00
|
|
|
{
|
|
|
|
struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
|
|
|
|
struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
|
2009-07-05 21:34:24 -06:00
|
|
|
struct radeon_cs_space_check temp_bo;
|
2010-01-14 03:24:16 -07:00
|
|
|
|
2009-07-05 21:34:24 -06:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (bo) {
|
2010-01-14 03:24:16 -07:00
|
|
|
temp_bo.bo = boi;
|
|
|
|
temp_bo.read_domains = read_domains;
|
|
|
|
temp_bo.write_domain = write_domain;
|
|
|
|
temp_bo.new_accounted = 0;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
|
2009-12-16 21:11:55 -07:00
|
|
|
ret = radeon_cs_check_space_internal(csi, bo ? &temp_bo : NULL);
|
2009-07-05 21:34:24 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-31 15:32:11 -06:00
|
|
|
int radeon_cs_space_check(struct radeon_cs *cs)
|
2009-07-05 21:34:24 -06:00
|
|
|
{
|
2009-12-16 21:11:55 -07:00
|
|
|
struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
|
|
|
|
return radeon_cs_check_space_internal(csi, NULL);
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
|
|
|
|
2015-03-31 15:32:11 -06:00
|
|
|
void radeon_cs_space_reset_bos(struct radeon_cs *cs)
|
2009-07-05 21:34:24 -06:00
|
|
|
{
|
2009-12-16 21:11:55 -07:00
|
|
|
struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
|
2009-07-05 21:34:24 -06:00
|
|
|
int i;
|
2009-12-16 21:11:55 -07:00
|
|
|
for (i = 0; i < csi->bo_count; i++) {
|
2010-01-14 03:24:16 -07:00
|
|
|
radeon_bo_unref((struct radeon_bo *)csi->bos[i].bo);
|
|
|
|
csi->bos[i].bo = NULL;
|
|
|
|
csi->bos[i].read_domains = 0;
|
|
|
|
csi->bos[i].write_domain = 0;
|
|
|
|
csi->bos[i].new_accounted = 0;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|
2009-12-16 21:11:55 -07:00
|
|
|
csi->bo_count = 0;
|
2009-07-05 21:34:24 -06:00
|
|
|
}
|