summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJérôme Glisse <jglisse@redhat.com>2017-08-12 16:10:17 -0400
committerJérôme Glisse <jglisse@redhat.com>2017-08-12 16:10:17 -0400
commitca39394948c9f78f330c35cc4ba30acd6df8c2ed (patch)
treee14104736d6d3a9e0dac9579586dbf5e28bb27a0
parentc92bc5ceb15d36863f03025620c6ddceba461982 (diff)
compote: split between old way and new way
old way is use device specific memory allocator new way is to use malloc (here we have a limitation due to nouveau the address must be in below 1 << 40 hence why the custom malloc) Signed-off-by: Jérôme Glisse <jglisse@redhat.com>
-rw-r--r--Makefile12
-rw-r--r--compote.c71
-rw-r--r--compote.h20
-rw-r--r--new.c76
-rw-r--r--old.c76
5 files changed, 195 insertions, 60 deletions
diff --git a/Makefile b/Makefile
index 8573309..3f67532 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,13 @@ CFLAGS = -std=c99 -Wall -O0 -g -D_GNU_SOURCE
LFLAGS = -lm
CC = gcc
-all: compote
+all: old new
-compote: compote.o
- $(CC) $(CFLAGS) $< -o $@
+old: compote.o old.o
+ $(CC) $(CFLAGS) $? -o $@
+
+new: compote.o new.o
+ $(CC) $(CFLAGS) $? -o $@
+
+clean:
+ rm old new *.o
diff --git a/compote.c b/compote.c
index ba825c4..d32987b 100644
--- a/compote.c
+++ b/compote.c
@@ -28,22 +28,6 @@
#include "compote.h"
#include "compote-uapi.h"
-static inline uint32_t nvk_sq_cmd(unsigned subc, unsigned method, unsigned len)
-{
- return ((method >> 2) & 0x1fff) |
- ((len & 0xfff) << 16) |
- ((subc & 0x7) << 13) |
- (0x1 << 29);
-}
-
-static inline uint32_t nvk_ni_cmd(unsigned subc, unsigned method, unsigned len)
-{
- return ((method >> 2) & 0x1fff) |
- ((len & 0xfff) << 16) |
- ((subc & 0x7) << 13) |
- (0x3 << 29);
-}
-
int compote_context_new(compote_context_t **ctxp)
{
struct compote_ioctl_channel_alloc arg;
@@ -185,50 +169,23 @@ int compote_context_execute(compote_context_t *ctx,
return compote_context_ioctl(ctx, COMPOTE_IOCTL_CHAN_EXEC, &arg);
}
-int main(int argc, char *argv[])
+void *malloc_below40(unsigned nbytes)
{
- compote_context_t *ctx;
- compote_mo_t *mo;
- int ret;
+ static unsigned long addr = 16 << 20;
+ void *ptr;
- ret = compote_context_new(&ctx);
- if (ret) {
- return ret;
- }
+ nbytes += ((1 << 12) - 1);
+ nbytes &= ~((1 << 12) - 1);
- ret = compote_mo_new(ctx, &mo, 64 << 10);
- if (ret) {
- goto out;
- }
-
- {
- uint32_t *ptr = mo->ptr;
- uint32_t *sem = &ptr[128 >> 2];
-
- ptr[128 >> 2] = 0xcafedead;
-
- ptr[0] = nvk_sq_cmd(1, 0x10, 4);
- ptr[1] = ((unsigned long)sem) >> 32;
- ptr[2] = ((unsigned long)sem) & 0xffffffff;
- ptr[3] = 0xdeadcafe;
- ptr[4] = 0x00000002;
-
- ret = compote_context_execute(ctx, ptr, 5);
- if (ret) {
- goto out;
- }
-
- for (unsigned c = 0; (c < 10) && (ptr[128 >> 2] != 0xdeadcafe); c++) {
- printf("[%4d] = 0x%08x 0x%08x 0x%08x\n", 128 >> 2, sem[0], sem[1], sem[2]);
- sleep(1);
+ do {
+ if ((addr + nbytes) > (1UL << 40)) {
+ return NULL;
}
- printf("[%4d] = 0x%08x 0x%08x 0x%08x\n", 128 >> 2, sem[0], sem[1], sem[2]);
- }
-
- printf("La compote c'est bon !\n");
+ printf("addr 0x%p\n", (void *)addr);
+ ptr = mmap((void *)addr, nbytes, PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
+ addr += nbytes;
+ } while (ptr == MAP_FAILED);
-out:
- compote_mo_del(ctx, &mo);
- compote_context_del(&ctx);
- return ret;
+ return ptr;
}
diff --git a/compote.h b/compote.h
index 3b86673..088e92b 100644
--- a/compote.h
+++ b/compote.h
@@ -36,6 +36,8 @@ typedef struct {
int compote_context_new(compote_context_t **ctxp);
void compote_context_del(compote_context_t **ctxp);
int compote_context_ioctl(compote_context_t *ctx, int command, void *arg);
+int compote_context_execute(compote_context_t *ctx,
+ void *addr, unsigned ndw);
int compote_mo_new(compote_context_t *ctx, compote_mo_t **mop, uint64_t nbytes);
void compote_mo_del(compote_context_t *ctx, compote_mo_t **mop);
@@ -49,4 +51,22 @@ static inline uint64_t compote_mo_gpu_addr(compote_mo_t *mo)
return (unsigned long)mo->ptr;
}
+void *malloc_below40(unsigned nbytes);
+
+static inline uint32_t nvk_sq_cmd(unsigned subc, unsigned method, unsigned len)
+{
+ return ((method >> 2) & 0x1fff) |
+ ((len & 0xfff) << 16) |
+ ((subc & 0x7) << 13) |
+ (0x1 << 29);
+}
+
+static inline uint32_t nvk_ni_cmd(unsigned subc, unsigned method, unsigned len)
+{
+ return ((method >> 2) & 0x1fff) |
+ ((len & 0xfff) << 16) |
+ ((subc & 0x7) << 13) |
+ (0x3 << 29);
+}
+
#endif // COMPOTE_H
diff --git a/new.c b/new.c
new file mode 100644
index 0000000..f066ad9
--- /dev/null
+++ b/new.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2017 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * Authors: Jérôme Glisse <jglisse@redhat.com>
+ */
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <strings.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include <fcntl.h>
+
+#include "compote.h"
+#include "compote-uapi.h"
+
+int main(int argc, char *argv[])
+{
+ compote_context_t *ctx;
+ compote_mo_t *mo;
+ int ret;
+
+ ret = compote_context_new(&ctx);
+ if (ret) {
+ return ret;
+ }
+
+ ret = compote_mo_new(ctx, &mo, 64 << 10);
+ if (ret) {
+ goto out;
+ }
+
+ {
+ uint32_t *ptr = mo->ptr;
+ uint32_t *sem = &ptr[128 >> 2];
+
+ sem[0] = 0xcafedead;
+
+ ptr[0] = nvk_sq_cmd(1, 0x10, 4);
+ ptr[1] = ((unsigned long)sem) >> 32;
+ ptr[2] = ((unsigned long)sem) & 0xffffffff;
+ ptr[3] = 0xdeadcafe;
+ ptr[4] = 0x00000002;
+ ret = compote_context_execute(ctx, ptr, 5);
+ if (ret) {
+ goto out;
+ }
+
+ for (unsigned c = 0; (c < 10) && (ptr[128 >> 2] != 0xdeadcafe); c++) {
+ printf("[%4d] = 0x%08x 0x%08x 0x%08x\n", 128 >> 2, sem[0], sem[1], sem[2]);
+ sleep(1);
+ }
+ printf("[%4d] = 0x%08x 0x%08x 0x%08x\n", 128 >> 2, sem[0], sem[1], sem[2]);
+ }
+
+ printf("La compote c'est bon !\n");
+
+out:
+ compote_mo_del(ctx, &mo);
+ compote_context_del(&ctx);
+ return ret;
+}
diff --git a/old.c b/old.c
new file mode 100644
index 0000000..f066ad9
--- /dev/null
+++ b/old.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2017 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * Authors: Jérôme Glisse <jglisse@redhat.com>
+ */
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <strings.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include <fcntl.h>
+
+#include "compote.h"
+#include "compote-uapi.h"
+
+int main(int argc, char *argv[])
+{
+ compote_context_t *ctx;
+ compote_mo_t *mo;
+ int ret;
+
+ ret = compote_context_new(&ctx);
+ if (ret) {
+ return ret;
+ }
+
+ ret = compote_mo_new(ctx, &mo, 64 << 10);
+ if (ret) {
+ goto out;
+ }
+
+ {
+ uint32_t *ptr = mo->ptr;
+ uint32_t *sem = &ptr[128 >> 2];
+
+ sem[0] = 0xcafedead;
+
+ ptr[0] = nvk_sq_cmd(1, 0x10, 4);
+ ptr[1] = ((unsigned long)sem) >> 32;
+ ptr[2] = ((unsigned long)sem) & 0xffffffff;
+ ptr[3] = 0xdeadcafe;
+ ptr[4] = 0x00000002;
+ ret = compote_context_execute(ctx, ptr, 5);
+ if (ret) {
+ goto out;
+ }
+
+ for (unsigned c = 0; (c < 10) && (ptr[128 >> 2] != 0xdeadcafe); c++) {
+ printf("[%4d] = 0x%08x 0x%08x 0x%08x\n", 128 >> 2, sem[0], sem[1], sem[2]);
+ sleep(1);
+ }
+ printf("[%4d] = 0x%08x 0x%08x 0x%08x\n", 128 >> 2, sem[0], sem[1], sem[2]);
+ }
+
+ printf("La compote c'est bon !\n");
+
+out:
+ compote_mo_del(ctx, &mo);
+ compote_context_del(&ctx);
+ return ret;
+}