radeon/r200/r300/r600: Warn if we emit more than prediction was.
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_cs_legacy.c
index 6bc41bca3bfee35508bed78fec28e2b7f614e790..4f1065ebcf39fa6f791ca4f614d05cd8c6a208e8 100644 (file)
  *      Jérôme Glisse <glisse@freedesktop.org>
  */
 #include <errno.h>
-#include "r300_reg.h"
-#include "r300_emit.h"
-#include "r300_cmdbuf.h"
-#include "radeon_cs.h"
-#include "radeon_cs_legacy.h"
-#include "radeon_bo_legacy.h"
-#include "radeon_context.h"
+
+#include "radeon_bocs_wrapper.h"
 
 struct cs_manager_legacy {
     struct radeon_cs_manager    base;
@@ -44,6 +39,8 @@ struct cs_manager_legacy {
     /* hack for scratch stuff */
     uint32_t                    pending_age;
     uint32_t                    pending_count;
+
+
 };
 
 struct cs_reloc_legacy {
@@ -73,29 +70,8 @@ static struct radeon_cs *cs_create(struct radeon_cs_manager *csm,
     return cs;
 }
 
-static int cs_write_dword(struct radeon_cs *cs, uint32_t dword)
-{
-    if (cs->cdw >= cs->ndw) {
-        uint32_t tmp, *ptr;
-        tmp = (cs->cdw + 1 + 0x3FF) & (~0x3FF);
-        ptr = (uint32_t*)realloc(cs->packets, 4 * tmp);
-        if (ptr == NULL) {
-            return -ENOMEM;
-        }
-        cs->packets = ptr;
-        cs->ndw = tmp;
-    }
-    cs->packets[cs->cdw++] = dword;
-    if (cs->section) {
-        cs->section_cdw++;
-    }
-    return 0;
-}
-
 static int cs_write_reloc(struct radeon_cs *cs,
                           struct radeon_bo *bo,
-                          uint32_t start_offset,
-                          uint32_t end_offset,
                           uint32_t read_domain,
                           uint32_t write_domain,
                           uint32_t flags)
@@ -117,25 +93,11 @@ static int cs_write_reloc(struct radeon_cs *cs,
     if (write_domain == RADEON_GEM_DOMAIN_CPU) {
         return -EINVAL;
     }
-    /* check reloc window */
-    if (end_offset > bo->size) {
-        return -EINVAL;
-    }
-    if (start_offset > end_offset) {
-        return -EINVAL;
-    }
     /* check if bo is already referenced */
     for(i = 0; i < cs->crelocs; i++) {
         uint32_t *indices;
 
         if (relocs[i].base.bo->handle == bo->handle) {
-            /* update start and end offset */
-            if (start_offset < relocs[i].base.start_offset) {
-                relocs[i].base.start_offset = start_offset;
-            }
-            if (end_offset > relocs[i].base.end_offset) {
-                relocs[i].base.end_offset = end_offset;
-            }
             /* Check domains must be in read or write. As we check already
              * checked that in argument one of the read or write domain was
              * set we only need to check that if previous reloc as the read
@@ -151,7 +113,7 @@ static int cs_write_reloc(struct radeon_cs *cs,
             relocs[i].base.read_domain |= read_domain;
             relocs[i].base.write_domain |= write_domain;
             /* save indice */
-            relocs[i].cindices += 1;
+            relocs[i].cindices++;
             indices = (uint32_t*)realloc(relocs[i].indices,
                                          relocs[i].cindices * 4);
             if (indices == NULL) {
@@ -172,8 +134,6 @@ static int cs_write_reloc(struct radeon_cs *cs,
     }
     cs->relocs = relocs;
     relocs[cs->crelocs].base.bo = bo;
-    relocs[cs->crelocs].base.start_offset = start_offset;
-    relocs[cs->crelocs].base.end_offset = end_offset;
     relocs[cs->crelocs].base.read_domain = read_domain;
     relocs[cs->crelocs].base.write_domain = write_domain;
     relocs[cs->crelocs].base.flags = flags;
@@ -208,6 +168,21 @@ static int cs_begin(struct radeon_cs *cs,
     cs->section_file = file;
     cs->section_func = func;
     cs->section_line = line;
+
+
+    if (cs->cdw + ndw > cs->ndw) {
+        uint32_t tmp, *ptr;
+       int num = (ndw > 0x3FF) ? ndw : 0x3FF;
+
+        tmp = (cs->cdw + 1 + num) & (~num);
+        ptr = (uint32_t*)realloc(cs->packets, 4 * tmp);
+        if (ptr == NULL) {
+            return -ENOMEM;
+        }
+        cs->packets = ptr;
+        cs->ndw = tmp;
+    }
+
     return 0;
 }
 
@@ -224,8 +199,8 @@ static int cs_end(struct radeon_cs *cs,
     }
     cs->section = 0;
     if (cs->section_ndw != cs->section_cdw) {
-        fprintf(stderr, "CS section size missmatch start at (%s,%s,%d)\n",
-                cs->section_file, cs->section_func, cs->section_line);
+        fprintf(stderr, "CS section size missmatch start at (%s,%s,%d) %d vs %d\n",
+                cs->section_file, cs->section_func, cs->section_line, cs->section_ndw, cs->section_cdw);
         fprintf(stderr, "CS section end at (%s,%s,%d)\n",
                 file, func, line);
         return -EPIPE;
@@ -239,28 +214,31 @@ static int cs_process_relocs(struct radeon_cs *cs)
     struct cs_reloc_legacy *relocs;
     int i, j, r;
 
-    if (!IS_R300_CLASS(csm->ctx->radeonScreen)) {
-        /* FIXME: r300 only right now */
-        return -EINVAL;
-    }
     csm = (struct cs_manager_legacy*)cs->csm;
     relocs = (struct cs_reloc_legacy *)cs->relocs;
-    for (i = 0; i < cs->crelocs; i++) {
-        for (j = 0; j < relocs[i].cindices; j++) {
+restart:
+    for (i = 0; i < cs->crelocs; i++) 
+    {
+        for (j = 0; j < relocs[i].cindices; j++) 
+        {
             uint32_t soffset, eoffset;
 
-            soffset = relocs[i].base.start_offset;
-            eoffset = relocs[i].base.end_offset;
             r = radeon_bo_legacy_validate(relocs[i].base.bo,
                                            &soffset, &eoffset);
-            if (r) {
+               if (r == -EAGAIN)
+            {
+                    goto restart;
+            }
+            if (r) 
+            {
                 fprintf(stderr, "validated %p [0x%08X, 0x%08X]\n",
                         relocs[i].base.bo, soffset, eoffset);
                 return r;
             }
             cs->packets[relocs[i].indices[j]] += soffset;
-            if (cs->packets[relocs[i].indices[j]] >= eoffset) {
-                radeon_bo_debug(relocs[i].base.bo, 12);
+            if (cs->packets[relocs[i].indices[j]] >= eoffset) 
+            {
+             /*                radeon_bo_debug(relocs[i].base.bo, 12); */
                 fprintf(stderr, "validated %p [0x%08X, 0x%08X]\n",
                         relocs[i].base.bo, soffset, eoffset);
                 fprintf(stderr, "above end: %p 0x%08X 0x%08X\n",
@@ -297,45 +275,23 @@ static int cs_emit(struct radeon_cs *cs)
     uint64_t ull;
     int r;
 
-    /* please flush pipe do all pending work */
-    cs_write_dword(cs, cmdpacket0(csm->ctx->radeonScreen,
-                                  R300_SC_SCREENDOOR, 1));
-    cs_write_dword(cs, 0x0);
-    cs_write_dword(cs, cmdpacket0(csm->ctx->radeonScreen,
-                                  R300_SC_SCREENDOOR, 1));
-    cs_write_dword(cs, 0x00FFFFFF);
-    cs_write_dword(cs, cmdpacket0(csm->ctx->radeonScreen,
-                                  R300_SC_HYPERZ, 1));
-    cs_write_dword(cs, 0x0);
-    cs_write_dword(cs, cmdpacket0(csm->ctx->radeonScreen,
-                                  R300_US_CONFIG, 1));
-    cs_write_dword(cs, 0x0);
-    cs_write_dword(cs, cmdpacket0(csm->ctx->radeonScreen,
-                                  R300_ZB_CNTL, 1));
-    cs_write_dword(cs, 0x0);
-    cs_write_dword(cs, cmdwait(csm->ctx->radeonScreen, R300_WAIT_3D));
-    cs_write_dword(cs, cmdpacket0(csm->ctx->radeonScreen,
-                                  R300_RB3D_DSTCACHE_CTLSTAT, 1));
-    cs_write_dword(cs, R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
-    cs_write_dword(cs, cmdpacket0(csm->ctx->radeonScreen,
-                                  R300_ZB_ZCACHE_CTLSTAT, 1));
-    cs_write_dword(cs, R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE);
-    cs_write_dword(cs, cmdwait(csm->ctx->radeonScreen,
-                               R300_WAIT_3D | R300_WAIT_3D_CLEAN));
+    csm->ctx->vtbl.emit_cs_header(cs, csm->ctx);
 
     /* append buffer age */
-    age.scratch.cmd_type = R300_CMD_SCRATCH;
-    /* Scratch register 2 corresponds to what radeonGetAge polls */
-    csm->pending_age = 0;
-    csm->pending_count = 1;
-    ull = (uint64_t) (intptr_t) &csm->pending_age;
-    age.scratch.reg = 2;
-    age.scratch.n_bufs = 1;
-    age.scratch.flags = 0;
-    radeon_cs_write_dword(cs, age.u);
-    radeon_cs_write_dword(cs, ull & 0xffffffff);
-    radeon_cs_write_dword(cs, ull >> 32);
-    radeon_cs_write_dword(cs, 0);
+    if ( IS_R300_CLASS(csm->ctx->radeonScreen) )
+    { 
+      age.scratch.cmd_type = R300_CMD_SCRATCH;
+      /* Scratch register 2 corresponds to what radeonGetAge polls */
+      csm->pending_age = 0;
+      csm->pending_count = 1;
+      ull = (uint64_t) (intptr_t) &csm->pending_age;
+      age.scratch.reg = 2;
+      age.scratch.n_bufs = 1;
+      age.scratch.flags = 0;
+      radeon_cs_write_dword(cs, age.u);
+      radeon_cs_write_qword(cs, ull);
+      radeon_cs_write_dword(cs, 0);
+    }
 
     r = cs_process_relocs(cs);
     if (r) {
@@ -352,16 +308,42 @@ static int cs_emit(struct radeon_cs *cs)
         cmd.boxes = (drm_clip_rect_t *) csm->ctx->pClipRects;
     }
 
+    //dump_cmdbuf(cs);
+
     r = drmCommandWrite(cs->csm->fd, DRM_RADEON_CMDBUF, &cmd, sizeof(cmd));
     if (r) {
         return r;
     }
+    if ((!IS_R300_CLASS(csm->ctx->radeonScreen)) &&
+        (!IS_R600_CLASS(csm->ctx->radeonScreen))) { /* +r6/r7 : No irq for r6/r7 yet. */
+       drm_radeon_irq_emit_t emit_cmd;
+       emit_cmd.irq_seq = &csm->pending_age;
+       r = drmCommandWrite(cs->csm->fd, DRM_RADEON_IRQ_EMIT, &emit_cmd, sizeof(emit_cmd));
+       if (r) {
+               return r;
+       }
+    }
     cs_set_age(cs);
+
+    cs->csm->read_used = 0;
+    cs->csm->vram_write_used = 0;
+    cs->csm->gart_write_used = 0;
     return 0;
 }
 
+static void inline cs_free_reloc(void *relocs_p, int crelocs)
+{
+    struct cs_reloc_legacy *relocs = relocs_p;
+    int i;
+    if (!relocs_p)
+      return;
+    for (i = 0; i < crelocs; i++)
+      free(relocs[i].indices);
+}
+
 static int cs_destroy(struct radeon_cs *cs)
 {
+    cs_free_reloc(cs->relocs, cs->crelocs);
     free(cs->relocs);
     free(cs->packets);
     free(cs);
@@ -370,6 +352,7 @@ static int cs_destroy(struct radeon_cs *cs)
 
 static int cs_erase(struct radeon_cs *cs)
 {
+    cs_free_reloc(cs->relocs, cs->crelocs);
     free(cs->relocs);
     cs->relocs_total_size = 0;
     cs->relocs = NULL;
@@ -381,20 +364,25 @@ static int cs_erase(struct radeon_cs *cs)
 
 static int cs_need_flush(struct radeon_cs *cs)
 {
-    /* FIXME: we should get the texture heap size */
-    return (cs->relocs_total_size > (7*1024*1024));
+    /* this function used to flush when the BO usage got to
+     * a certain size, now the higher levels handle this better */
+    return 0;
+}
+
+static void cs_print(struct radeon_cs *cs, FILE *file)
+{
 }
 
 static struct radeon_cs_funcs  radeon_cs_legacy_funcs = {
     cs_create,
-    cs_write_dword,
     cs_write_reloc,
     cs_begin,
     cs_end,
     cs_emit,
     cs_destroy,
     cs_erase,
-    cs_need_flush
+    cs_need_flush,
+    cs_print,
 };
 
 struct radeon_cs_manager *radeon_cs_manager_legacy_ctor(struct radeon_context *ctx)
@@ -417,3 +405,4 @@ void radeon_cs_manager_legacy_dtor(struct radeon_cs_manager *csm)
 {
     free(csm);
 }
+