util: add anon_file.h for all memfd/temp file usage
[mesa.git] / src / intel / tools / aub_mem.c
index 9248427290607ea3487cacd2996419c5aea49512..f436627d19dfae84d90ac4d814c028c9ea4dee45 100644 (file)
 #include <sys/mman.h>
 
 #include "aub_mem.h"
-
-#ifndef HAVE_MEMFD_CREATE
-#include <sys/syscall.h>
-
-static inline int
-memfd_create(const char *name, unsigned int flags)
-{
-   return syscall(SYS_memfd_create, name, flags);
-}
-#endif
+#include "util/anon_file.h"
 
 struct bo_map {
    struct list_head link;
    struct gen_batch_decode_bo bo;
    bool unmap_after_use;
+   bool ppgtt;
 };
 
 struct ggtt_entry {
@@ -55,13 +47,15 @@ struct phys_mem {
    uint64_t fd_offset;
    uint64_t phys_addr;
    uint8_t *data;
+   const uint8_t *aub_data;
 };
 
 static void
-add_gtt_bo_map(struct aub_mem *mem, struct gen_batch_decode_bo bo, bool unmap_after_use)
+add_gtt_bo_map(struct aub_mem *mem, struct gen_batch_decode_bo bo, bool ppgtt, bool unmap_after_use)
 {
    struct bo_map *m = calloc(1, sizeof(*m));
 
+   m->ppgtt = ppgtt;
    m->bo = bo;
    m->unmap_after_use = unmap_after_use;
    list_add(&m->link, &mem->maps);
@@ -152,7 +146,7 @@ ensure_phys_mem(struct aub_mem *mem, uint64_t phys_addr)
       new_mem->phys_addr = phys_addr;
       new_mem->fd_offset = mem->mem_fd_len;
 
-      MAYBE_UNUSED int ftruncate_res = ftruncate(mem->mem_fd, mem->mem_fd_len += 4096);
+      ASSERTED int ftruncate_res = ftruncate(mem->mem_fd, mem->mem_fd_len += 4096);
       assert(ftruncate_res == 0);
 
       new_mem->data = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED,
@@ -189,7 +183,7 @@ aub_mem_local_write(void *_mem, uint64_t address,
       .addr = address,
       .size = size,
    };
-   add_gtt_bo_map(mem, bo, false);
+   add_gtt_bo_map(mem, bo, false, false);
 }
 
 void
@@ -220,6 +214,7 @@ aub_mem_phys_write(void *_mem, uint64_t phys_address,
       uint32_t size_this_page = MIN2(to_write, 4096 - offset);
       to_write -= size_this_page;
       memcpy(pmem->data + offset, data, size_this_page);
+      pmem->aub_data = data - offset;
       data = (const uint8_t *)data + size_this_page;
    }
 }
@@ -251,7 +246,7 @@ aub_mem_get_ggtt_bo(void *_mem, uint64_t address)
    struct gen_batch_decode_bo bo = {0};
 
    list_for_each_entry(struct bo_map, i, &mem->maps, link)
-      if (i->bo.addr <= address && i->bo.addr + i->bo.size > address)
+      if (!i->ppgtt && i->bo.addr <= address && i->bo.addr + i->bo.size > address)
          return i->bo;
 
    address &= ~0xfff;
@@ -285,12 +280,13 @@ aub_mem_get_ggtt_bo(void *_mem, uint64_t address)
          continue;
 
       uint32_t map_offset = i->virt_addr - address;
-      void *res = mmap((uint8_t *)bo.map + map_offset, 4096, PROT_READ,
-                       MAP_SHARED | MAP_FIXED, mem->mem_fd, phys_mem->fd_offset);
+      ASSERTED void *res =
+            mmap((uint8_t *)bo.map + map_offset, 4096, PROT_READ,
+                  MAP_SHARED | MAP_FIXED, mem->mem_fd, phys_mem->fd_offset);
       assert(res != MAP_FAILED);
    }
 
-   add_gtt_bo_map(mem, bo, true);
+   add_gtt_bo_map(mem, bo, false, true);
 
    return bo;
 }
@@ -326,6 +322,10 @@ aub_mem_get_ppgtt_bo(void *_mem, uint64_t address)
    struct aub_mem *mem = _mem;
    struct gen_batch_decode_bo bo = {0};
 
+   list_for_each_entry(struct bo_map, i, &mem->maps, link)
+      if (i->ppgtt && i->bo.addr <= address && i->bo.addr + i->bo.size > address)
+         return i->bo;
+
    address &= ~0xfff;
 
    if (!ppgtt_mapped(mem, mem->pml4, address))
@@ -346,12 +346,13 @@ aub_mem_get_ppgtt_bo(void *_mem, uint64_t address)
    for (uint64_t page = address; page < end; page += 4096) {
       struct phys_mem *phys_mem = ppgtt_walk(mem, mem->pml4, page);
 
-      void *res = mmap((uint8_t *)bo.map + (page - bo.addr), 4096, PROT_READ,
-                       MAP_SHARED | MAP_FIXED, mem->mem_fd, phys_mem->fd_offset);
+      ASSERTED void *res =
+            mmap((uint8_t *)bo.map + (page - bo.addr), 4096, PROT_READ,
+                  MAP_SHARED | MAP_FIXED, mem->mem_fd, phys_mem->fd_offset);
       assert(res != MAP_FAILED);
    }
 
-   add_gtt_bo_map(mem, bo, true);
+   add_gtt_bo_map(mem, bo, true, true);
 
    return bo;
 }
@@ -363,7 +364,7 @@ aub_mem_init(struct aub_mem *mem)
 
    list_inithead(&mem->maps);
 
-   mem->mem_fd = memfd_create("phys memory", 0);
+   mem->mem_fd = os_create_anonymous_file(0, "phys memory");
 
    return mem->mem_fd != -1;
 }
@@ -389,3 +390,30 @@ aub_mem_fini(struct aub_mem *mem)
    close(mem->mem_fd);
    mem->mem_fd = -1;
 }
+
+struct gen_batch_decode_bo
+aub_mem_get_phys_addr_data(struct aub_mem *mem, uint64_t phys_addr)
+{
+   struct phys_mem *page = search_phys_mem(mem, phys_addr);
+   return page ?
+      (struct gen_batch_decode_bo) { .map = page->data, .addr = page->phys_addr, .size = 4096 } :
+      (struct gen_batch_decode_bo) {};
+}
+
+struct gen_batch_decode_bo
+aub_mem_get_ppgtt_addr_data(struct aub_mem *mem, uint64_t virt_addr)
+{
+   struct phys_mem *page = ppgtt_walk(mem, mem->pml4, virt_addr);
+   return page ?
+      (struct gen_batch_decode_bo) { .map = page->data, .addr = virt_addr & ~((1ULL << 12) - 1), .size = 4096 } :
+      (struct gen_batch_decode_bo) {};
+}
+
+struct gen_batch_decode_bo
+aub_mem_get_ppgtt_addr_aub_data(struct aub_mem *mem, uint64_t virt_addr)
+{
+   struct phys_mem *page = ppgtt_walk(mem, mem->pml4, virt_addr);
+   return page ?
+      (struct gen_batch_decode_bo) { .map = page->aub_data, .addr = virt_addr & ~((1ULL << 12) - 1), .size = 4096 } :
+      (struct gen_batch_decode_bo) {};
+}