Refactor out shim code
authorCafe <cafe@not.applicable>
Tue, 25 Jul 2017 00:00:00 +0000 (00:00 +0000)
committerCafe <cafe@not.applicable>
Tue, 25 Jul 2017 16:52:34 +0000 (16:52 +0000)
build.sh
re.c
shim.c [new file with mode: 0644]
shim.h

index cefee6e7317c3c9fe406754b16aaa911a43f95b0..0b07c7a4b070c9435c57bf1c0bd9b5155174d18b 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -1,3 +1,3 @@
 #!/bin/sh
-arm-linux-gnueabi-gcc re.c -Wall -Werror -Wextra -Wno-missing-braces \
-       -D_FILE_OFFSET_BITS=64
+arm-linux-gnueabi-gcc re.c shim.c -Wall -Werror -Wextra \
+       -Wno-missing-braces -D_FILE_OFFSET_BITS=64
diff --git a/re.c b/re.c
index 500144920a5779dd918f3b77681b0b1e896d41ec..4219458cd15a1552a674df13a8f36bc5b81738f7 100644 (file)
--- a/re.c
+++ b/re.c
@@ -1,70 +1,12 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/ioctl.h>
+#include <memory.h>
 #include <sys/mman.h>
-#include <fcntl.h>
-#include <string.h>
-#include <errno.h>
-#include <unistd.h>
 
 #include "shim.h"
 #include "jobs.h"
 #include "../oolong/chai-notes.h"
 
-#define m_ioctl(fd, data, no) \
-       data.header.id = no; \
-       if(ioctl(fd, _IOC(_IOC_READ, 0, 0, sizeof(data)), &data)) { \
-               printf("Bad ioctl %d (%s)\n", no, strerror(errno)); \
-               exit(1); \
-       }
-
-uint64_t alloc_gpu_pages(int fd, int pages, int e_flags) {
-       struct kbase_uk_mem_alloc alloc = {
-               .va_pages = pages,
-               .commit_pages = pages,
-               .extent = 0,
-               .flags = BASE_MEM_PROT_CPU_RD | BASE_MEM_PROT_CPU_WR 
-                      | e_flags
-       };
-
-       m_ioctl(fd, alloc, KBASE_FUNC_MEM_ALLOC);
-
-       return alloc.gpu_va;
-}
-
-void free_gpu(int fd, uint64_t addr) {
-       struct kbase_uk_mem_free gfree = { .gpu_addr = addr };
-       m_ioctl(fd, gfree, KBASE_FUNC_MEM_FREE);
-}
-
-uint8_t* mmap_gpu(int fd, uint64_t addr, int page_count) {
-       uint8_t* buffer = mmap(NULL, page_count << PAGE_SHIFT,
-                               PROT_READ | PROT_WRITE, MAP_SHARED,
-                               fd, addr);
-
-       if(buffer == MAP_FAILED) {
-               printf("Buffer map failed (%s)\n", strerror(errno));
-               exit(1);
-       }
-
-       return buffer;
-}
-
-void sync_gpu(int fd, uint8_t* cpu, uint64_t gpu, int pages) {
-       struct basep_syncset base_sset = {
-               .mem_handle = { .basep = { .handle = gpu } },
-               .user_addr = (u32) cpu,
-               .size = pages << PAGE_SHIFT,
-               .type = BASE_SYNCSET_OP_MSYNC
-       };
-
-       struct kbase_uk_sync_now sync = {
-               .sset = { .basep_sset = base_sset}
-       };
-
-       m_ioctl(fd, sync, KBASE_FUNC_SYNC);
-}
-
 int atom_count = 0;
 
 int job_chain_set_value(int fd) {
@@ -119,15 +61,7 @@ int job_chain_set_value(int fd) {
        job.pre_dep[0] = depNoDep;
        job.pre_dep[1] = depNoDep;
 
-       struct kbase_uk_job_submit submit = {
-               .addr = { .value = &job },
-               .nr_atoms = 1,
-               .stride = sizeof(struct base_jd_atom_v2)
-       };
-
-       
-       m_ioctl(fd, submit, KBASE_FUNC_JOB_SUBMIT);
-       usleep(1000 * 50); /* Ensure job returns */
+       submit_job(fd, &job, 1);
 
        memcpy(&header, buffer, sizeof(header));
 
@@ -142,43 +76,7 @@ int job_chain_set_value(int fd) {
 
 int main()
 {
-       /* The kernel module appears at this location */
-
-       int fd = open("/dev/mali0", O_RDWR);
-
-       if(fd == -1) {
-               printf("Failed to open /dev/mali0\n");
-               return 1;
-       }
-
-       /* Declare the ABI version (handshake 1/3) */
-
-       struct uku_version_check_args check = {
-               .major = BASE_UK_VERSION_MAJOR,
-               .minor = BASE_UK_VERSION_MINOR
-       };
-
-       m_ioctl(fd, check, UKP_FUNC_ID_CHECK_VERSION);
-
-       /* Declare special flags (handshake 2/3) */
-
-       struct kbase_uk_set_flags flags = {
-               .create_flags = BASE_CONTEXT_CREATE_FLAG_NONE
-       };
-
-       m_ioctl(fd, flags, KBASE_FUNC_SET_FLAGS);
-
-       /* Map the Memmap Tracking Handle (handshake 3/3) */
-
-       uint8_t *mtp = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd,
-                               BASE_MEM_MAP_TRACKING_HANDLE);
-
-       if(mtp == MAP_FAILED) {
-               printf("MP map failed (%s)\n", strerror(errno));
-               return -1;
-       }
-
-       /* The GPU is now ready for normal operation. */
+       int fd = open_kernel_module();
 
        job_chain_set_value(fd);
 
diff --git a/shim.c b/shim.c
new file mode 100644 (file)
index 0000000..a517ea1
--- /dev/null
+++ b/shim.c
@@ -0,0 +1,125 @@
+/* Raw shim communication appears here.
+ * If/when the shim is replaced by something less hacky,
+ * only this file will change.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "shim.h"
+
+#define m_ioctl(fd, data, no) \
+       data.header.id = no; \
+       if(ioctl(fd, _IOC(_IOC_READ, 0, 0, sizeof(data)), &data)) { \
+               printf("Bad ioctl %d (%s)\n", no, strerror(errno)); \
+               exit(1); \
+       }
+
+int open_kernel_module()
+{
+       int fd = open("/dev/mali0", O_RDWR);
+
+       if(fd == -1) {
+               printf("Failed to open /dev/mali0\n");
+               return 1;
+       }
+
+       /* Declare the ABI version (handshake 1/3) */
+
+       struct uku_version_check_args check = {
+               .major = BASE_UK_VERSION_MAJOR,
+               .minor = BASE_UK_VERSION_MINOR
+       };
+
+       m_ioctl(fd, check, UKP_FUNC_ID_CHECK_VERSION);
+
+       /* Declare special flags (handshake 2/3) */
+
+       struct kbase_uk_set_flags flags = {
+               .create_flags = BASE_CONTEXT_CREATE_FLAG_NONE
+       };
+
+       m_ioctl(fd, flags, KBASE_FUNC_SET_FLAGS);
+
+       /* Map the Memmap Tracking Handle (handshake 3/3) */
+
+       uint8_t *mtp = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd,
+                               BASE_MEM_MAP_TRACKING_HANDLE);
+
+       if(mtp == MAP_FAILED) {
+               printf("MP map failed (%s)\n", strerror(errno));
+               return -1;
+       }
+
+       return fd;
+}
+
+uint64_t alloc_gpu_pages(int fd, int pages, int e_flags)
+{
+       struct kbase_uk_mem_alloc alloc = {
+               .va_pages = pages,
+               .commit_pages = pages,
+               .extent = 0,
+               .flags = BASE_MEM_PROT_CPU_RD | BASE_MEM_PROT_CPU_WR 
+                      | e_flags
+       };
+
+       m_ioctl(fd, alloc, KBASE_FUNC_MEM_ALLOC);
+
+       return alloc.gpu_va;
+}
+
+void free_gpu(int fd, uint64_t addr)
+{
+       struct kbase_uk_mem_free gfree = { .gpu_addr = addr };
+       m_ioctl(fd, gfree, KBASE_FUNC_MEM_FREE);
+}
+
+void sync_gpu(int fd, uint8_t* cpu, uint64_t gpu, int pages)
+{
+       struct basep_syncset base_sset = {
+               .mem_handle = { .basep = { .handle = gpu } },
+               .user_addr = (u32) cpu,
+               .size = pages << PAGE_SHIFT,
+               .type = BASE_SYNCSET_OP_MSYNC
+       };
+
+       struct kbase_uk_sync_now sync = {
+               .sset = { .basep_sset = base_sset}
+       };
+
+       m_ioctl(fd, sync, KBASE_FUNC_SYNC);
+}
+
+void submit_job(int fd, struct base_jd_atom_v2 *atoms, size_t count)
+{
+       struct kbase_uk_job_submit submit = {
+               .addr = { .value = atoms },
+               .nr_atoms = count,
+               .stride = sizeof(struct base_jd_atom_v2)
+       };
+       
+       m_ioctl(fd, submit, KBASE_FUNC_JOB_SUBMIT);
+}
+
+/* Not strictly an ioctl but still shim related */
+
+uint8_t* mmap_gpu(int fd, uint64_t addr, int page_count)
+{
+       uint8_t* buffer = mmap(NULL, page_count << PAGE_SHIFT,
+                               PROT_READ | PROT_WRITE, MAP_SHARED,
+                               fd, addr);
+
+       if(buffer == MAP_FAILED) {
+               printf("Buffer map failed (%s)\n", strerror(errno));
+               exit(1);
+       }
+
+       return buffer;
+}
diff --git a/shim.h b/shim.h
index bc366384082e3574ed8d1fca3a9f86f24ef4320b..ab02d411d2ffbcf2d8096b809445756ba683710a 100644 (file)
--- a/shim.h
+++ b/shim.h
@@ -26,3 +26,12 @@ typedef uint64_t u64;
 
 /* Include the shim directly. */
 #include "midgard/mali_kbase_uku.h"
+
+/* Include definitions for thin chai wrappers */
+
+int open_kernel_module();
+uint64_t alloc_gpu_pages(int fd, int pages, int e_flags);
+void free_gpu(int fd, uint64_t addr);
+void sync_gpu(int fd, uint8_t* cpu, uint64_t gpu, int pages);
+void submit_job(int fd, struct base_jd_atom_v2 *atoms, size_t count);
+uint8_t* mmap_gpu(int fd, uint64_t addr, int page_count);