panfrost: Move pan_bo to root panfrost

Now that its Gallium dependencies have been resolved, we can move this
all out to root. The only nontrivial change here is keeping the
pandecode calls in Gallium-panfrost to avoid creating a circular
dependency between encoder/decoder. This could be solved with a third
drm folder but this seems less intrusive for now and Roman would
probably appreciate if I went longer than 8 hours without breaking the
Android build.

Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4382>
diff --git a/src/gallium/drivers/panfrost/Makefile.sources b/src/gallium/drivers/panfrost/Makefile.sources
index 1c79660..c734cd0 100644
--- a/src/gallium/drivers/panfrost/Makefile.sources
+++ b/src/gallium/drivers/panfrost/Makefile.sources
@@ -13,8 +13,6 @@
 	pan_blend_shaders.c \
 	pan_blend_shaders.h \
 	pan_blit.c \
-	pan_bo.c \
-	pan_bo.h \
 	pan_cmdstream.c \
 	pan_cmdstream.h \
 	pan_compute.c \
@@ -31,4 +29,3 @@
 	pan_screen.c \
 	pan_screen.h \
 	pan_sfbd.c \
-	pan_util.h
diff --git a/src/gallium/drivers/panfrost/meson.build b/src/gallium/drivers/panfrost/meson.build
index 92f043e..5a2d466 100644
--- a/src/gallium/drivers/panfrost/meson.build
+++ b/src/gallium/drivers/panfrost/meson.build
@@ -30,7 +30,6 @@
   'nir/nir_lower_framebuffer.c',
 
   'pan_context.c',
-  'pan_bo.c',
   'pan_blit.c',
   'pan_job.c',
   'pan_allocate.c',
diff --git a/src/gallium/drivers/panfrost/pan_assemble.c b/src/gallium/drivers/panfrost/pan_assemble.c
index 63f87dc..5d5a0f3 100644
--- a/src/gallium/drivers/panfrost/pan_assemble.c
+++ b/src/gallium/drivers/panfrost/pan_assemble.c
@@ -76,7 +76,7 @@
          * that's how I'd do it. */
 
         if (size) {
-                state->bo = panfrost_bo_create(dev, size, PAN_BO_EXECUTE);
+                state->bo = pan_bo_create(dev, size, PAN_BO_EXECUTE);
                 memcpy(state->bo->cpu, dst, size);
                 state->first_tag = program.first_tag;
         } else {
diff --git a/src/gallium/drivers/panfrost/pan_context.c b/src/gallium/drivers/panfrost/pan_context.c
index a0111a6..2d26827 100644
--- a/src/gallium/drivers/panfrost/pan_context.c
+++ b/src/gallium/drivers/panfrost/pan_context.c
@@ -922,7 +922,7 @@
                         template->u.tex.last_layer,
                         type, prsrc->layout);
 
-        so->bo = panfrost_bo_create(device, size, 0);
+        so->bo = pan_bo_create(device, size, 0);
 
         panfrost_new_texture(
                         so->bo->cpu,
@@ -1180,7 +1180,7 @@
         case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
                 /* Allocate a bo for the query results to be stored */
                 if (!query->bo) {
-                        query->bo = panfrost_bo_create(
+                        query->bo = pan_bo_create(
                                         pan_device(ctx->base.screen),
                                         sizeof(unsigned), 0);
                 }
diff --git a/src/gallium/drivers/panfrost/pan_job.c b/src/gallium/drivers/panfrost/pan_job.c
index eca2944..8634bf7 100644
--- a/src/gallium/drivers/panfrost/pan_job.c
+++ b/src/gallium/drivers/panfrost/pan_job.c
@@ -603,12 +603,12 @@
 {
         struct panfrost_bo *bo;
 
-        bo = panfrost_bo_create(pan_device(batch->ctx->base.screen), size,
+        bo = pan_bo_create(pan_device(batch->ctx->base.screen), size,
                                 create_flags);
         panfrost_batch_add_bo(batch, bo, access_flags);
 
         /* panfrost_batch_add_bo() has retained a reference and
-         * panfrost_bo_create() initialize the refcnt to 1, so let's
+         * pan_bo_create() initialize the refcnt to 1, so let's
          * unreference the BO here so it gets released when the batch is
          * destroyed (unless it's retained by someone else in the meantime).
          */
diff --git a/src/gallium/drivers/panfrost/pan_resource.c b/src/gallium/drivers/panfrost/pan_resource.c
index 3929ff1..3505f37 100644
--- a/src/gallium/drivers/panfrost/pan_resource.c
+++ b/src/gallium/drivers/panfrost/pan_resource.c
@@ -47,8 +47,26 @@
 #include "pan_resource.h"
 #include "pan_util.h"
 #include "pan_tiling.h"
+#include "pandecode/decode.h"
 #include "panfrost-quirks.h"
 
+/* Wrapper around panfrost_bo_create that handles pandecode */
+
+struct panfrost_bo *
+pan_bo_create(struct panfrost_device *dev, size_t size, uint32_t flags)
+{
+        struct panfrost_bo *bo = panfrost_bo_create(dev, size, flags);
+
+        if (pan_debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {
+                if (flags & PAN_BO_INVISIBLE)
+                        pandecode_inject_mmap(bo->gpu, NULL, bo->size, NULL);
+                else if (!(flags & PAN_BO_DELAY_MMAP))
+                        pandecode_inject_mmap(bo->gpu, bo->cpu, bo->size, NULL);
+        }
+
+        return bo;
+}
+
 void
 panfrost_resource_reset_damage(struct panfrost_resource *pres)
 {
@@ -406,7 +424,7 @@
 
         /* We create a BO immediately but don't bother mapping, since we don't
          * care to map e.g. FBOs which the CPU probably won't touch */
-        pres->bo = panfrost_bo_create(dev, bo_size, PAN_BO_DELAY_MMAP);
+        pres->bo = pan_bo_create(dev, bo_size, PAN_BO_DELAY_MMAP);
 }
 
 void
@@ -562,6 +580,9 @@
         /* If we haven't already mmaped, now's the time */
         panfrost_bo_mmap(bo);
 
+        if (pan_debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))
+                pandecode_inject_mmap(bo->gpu, bo->cpu, bo->size, NULL);
+
         if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
                 /* If the BO is used by one of the pending batches or if it's
                  * not ready yet (still accessed by one of the already flushed
@@ -580,7 +601,7 @@
                          * doing to it.
                          */
                         if (!(bo->flags & (PAN_BO_IMPORTED | PAN_BO_EXPORTED)))
-                                newbo = panfrost_bo_create(dev, bo->size,
+                                newbo = pan_bo_create(dev, bo->size,
                                                            flags);
 
                         if (newbo) {
@@ -862,7 +883,7 @@
         /* If we grew in size, reallocate the BO */
         if (new_size > rsrc->bo->size) {
                 panfrost_bo_unreference(rsrc->bo);
-                rsrc->bo = panfrost_bo_create(dev, new_size, PAN_BO_DELAY_MMAP);
+                rsrc->bo = pan_bo_create(dev, new_size, PAN_BO_DELAY_MMAP);
         }
 
         /* TODO: If there are textures bound, regenerate their descriptors */
diff --git a/src/gallium/drivers/panfrost/pan_resource.h b/src/gallium/drivers/panfrost/pan_resource.h
index a3d78e9..0a25e30 100644
--- a/src/gallium/drivers/panfrost/pan_resource.h
+++ b/src/gallium/drivers/panfrost/pan_resource.h
@@ -118,4 +118,8 @@
                                     unsigned int nrects,
                                     const struct pipe_box *rects);
 
+
+struct panfrost_bo *
+pan_bo_create(struct panfrost_device *dev, size_t size, uint32_t flags);
+
 #endif /* PAN_RESOURCE_H */
diff --git a/src/panfrost/Makefile.sources b/src/panfrost/Makefile.sources
index ad9a230..a92213a 100644
--- a/src/panfrost/Makefile.sources
+++ b/src/panfrost/Makefile.sources
@@ -17,6 +17,8 @@
 encoder_FILES := \
 	encoder/pan_afbc.c \
         encoder/pan_attributes.c \
+        encoder/pan_bo.c \
+        encoder/pan_bo.h \
         encoder/pan_device.h \
         encoder/pan_encoder.h \
         encoder/pan_format.c \
@@ -25,7 +27,8 @@
         encoder/pan_sampler.c \
         encoder/pan_tiler.c \
         encoder/pan_texture.c \
-        encoder/pan_scratch.c
+        encoder/pan_scratch.c \
+        encoder/pan_util.h
 
 midgard_FILES := \
         midgard/compiler.h \
diff --git a/src/panfrost/encoder/meson.build b/src/panfrost/encoder/meson.build
index babf529..de45322 100644
--- a/src/panfrost/encoder/meson.build
+++ b/src/panfrost/encoder/meson.build
@@ -24,6 +24,7 @@
 
   'pan_afbc.c',
   'pan_attributes.c',
+  'pan_bo.c',
   'pan_format.c',
   'pan_invocation.c',
   'pan_sampler.c',
diff --git a/src/gallium/drivers/panfrost/pan_bo.c b/src/panfrost/encoder/pan_bo.c
similarity index 96%
rename from src/gallium/drivers/panfrost/pan_bo.c
rename to src/panfrost/encoder/pan_bo.c
index 62e14a5..aca4f8f 100644
--- a/src/gallium/drivers/panfrost/pan_bo.c
+++ b/src/panfrost/encoder/pan_bo.c
@@ -31,8 +31,6 @@
 #include "drm-uapi/panfrost_drm.h"
 
 #include "pan_bo.h"
-#include "pan_util.h"
-#include "pandecode/decode.h"
 
 #include "os/os_mman.h"
 
@@ -72,7 +70,7 @@
 
         ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_CREATE_BO, &create_bo);
         if (ret) {
-                DBG("DRM_IOCTL_PANFROST_CREATE_BO failed: %m\n");
+                fprintf(stderr, "DRM_IOCTL_PANFROST_CREATE_BO failed: %m\n");
                 return NULL;
         }
 
@@ -344,10 +342,6 @@
                 fprintf(stderr, "mmap failed: %p %m\n", bo->cpu);
                 assert(0);
         }
-
-        /* Record the mmap if we're tracing */
-        if (pan_debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))
-                pandecode_inject_mmap(bo->gpu, bo->cpu, bo->size, NULL);
 }
 
 static void
@@ -404,10 +398,6 @@
 
         if (!(flags & (PAN_BO_INVISIBLE | PAN_BO_DELAY_MMAP)))
                 panfrost_bo_mmap(bo);
-        else if (flags & PAN_BO_INVISIBLE) {
-                if (pan_debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))
-                        pandecode_inject_mmap(bo->gpu, NULL, bo->size, NULL);
-        }
 
         p_atomic_set(&bo->refcnt, 1);
 
diff --git a/src/gallium/drivers/panfrost/pan_bo.h b/src/panfrost/encoder/pan_bo.h
similarity index 100%
rename from src/gallium/drivers/panfrost/pan_bo.h
rename to src/panfrost/encoder/pan_bo.h
diff --git a/src/panfrost/encoder/pan_device.h b/src/panfrost/encoder/pan_device.h
index 07158b9..19aa2df 100644
--- a/src/panfrost/encoder/pan_device.h
+++ b/src/panfrost/encoder/pan_device.h
@@ -34,9 +34,9 @@
 #include "util/u_dynarray.h"
 #include "util/bitset.h"
 #include "util/set.h"
+#include "util/list.h"
 
 #include <panfrost-misc.h>
-#include "pan_allocate.h"
 
 /* Driver limits */
 #define PAN_MAX_CONST_BUFFERS 16
diff --git a/src/gallium/drivers/panfrost/pan_util.h b/src/panfrost/encoder/pan_util.h
similarity index 100%
rename from src/gallium/drivers/panfrost/pan_util.h
rename to src/panfrost/encoder/pan_util.h