Permalink
Browse files

gdev: refactored code and cleaned up files

  • Loading branch information...
1 parent f2391e5 commit fe052f507fd7444473f63e789f5a54f1e8f408b3 Shinpei Kato committed Jan 6, 2012
View
@@ -1,12 +1,17 @@
/*
* Copyright 2011 Shinpei Kato
+ *
+ * University of California, Santa Cruz
+ * Systems Research Lab.
+ *
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
@@ -22,9 +27,7 @@
*/
#include "gdev_api.h"
-#include "gdev_conf.h"
-#include "gdev_mm.h"
-#include "gdev_proto.h"
+#include "gdev_device.h"
#include "gdev_sched.h"
#define __max(x, y) (x) > (y) ? (x) : (y)
@@ -35,6 +38,7 @@
*/
struct gdev_handle {
struct gdev_device *gdev; /* gdev handle object. */
+ struct gdev_sched_entity *se; /* scheduling entity. */
gdev_vas_t *vas; /* virtual address space object. */
gdev_ctx_t *ctx; /* device context object. */
gdev_mem_t **dma_mem; /* host-side DMA memory object (bounce buffer). */
@@ -119,8 +123,8 @@ static int __gmemcpy_to_device_p
int i;
for (i = 0; i < p_count; i++) {
- dma_addr[i] = GDEV_MEM_ADDR(bmem[i]);
- dma_buf[i] = GDEV_MEM_BUF(bmem[i]);
+ dma_addr[i] = gdev_mem_get_addr(bmem[i]);
+ dma_buf[i] = gdev_mem_get_buf(bmem[i]);
fence[i] = 0;
}
@@ -168,8 +172,8 @@ static int __gmemcpy_to_device_np
uint32_t dma_size;
int ret = 0;
- dma_addr[0] = GDEV_MEM_ADDR(bmem[0]);
- dma_buf[0] = GDEV_MEM_BUF(bmem[0]);
+ dma_addr[0] = gdev_mem_get_addr(bmem[0]);
+ dma_buf[0] = gdev_mem_get_buf(bmem[0]);
/* copy data by the chunk size. */
offset = 0;
@@ -300,8 +304,8 @@ static int __gmemcpy_from_device_p
int i;
for (i = 0; i < p_count; i++) {
- dma_addr[i] = GDEV_MEM_ADDR(bmem[i]);
- dma_buf[i] = GDEV_MEM_BUF(bmem[i]);
+ dma_addr[i] = gdev_mem_get_addr(bmem[i]);
+ dma_buf[i] = gdev_mem_get_buf(bmem[i]);
fence[i] = 0;
}
@@ -357,8 +361,8 @@ static int __gmemcpy_from_device_np
uint32_t dma_size;
int ret = 0;
- dma_addr[0] = GDEV_MEM_ADDR(bmem[0]);
- dma_buf[0] = GDEV_MEM_BUF(bmem[0]);
+ dma_addr[0] = gdev_mem_get_addr(bmem[0]);
+ dma_buf[0] = gdev_mem_get_buf(bmem[0]);
/* copy data by the chunk size. */
offset = 0;
@@ -549,6 +553,7 @@ struct gdev_handle *gopen(int minor)
{
struct gdev_handle *h;
struct gdev_device *gdev;
+ struct gdev_sched_entity *se;
gdev_vas_t *vas;
gdev_ctx_t *ctx;
gdev_mem_t **dma_mem;
@@ -561,24 +566,41 @@ struct gdev_handle *gopen(int minor)
/* open the specified device. */
gdev = gdev_dev_open(minor);
- if (!gdev)
+ if (!gdev) {
+ GDEV_PRINT("Failed to open gdev%d\n", minor);
goto fail_open;
+ }
/* create a new virual address space (VAS) object. */
vas = gdev_vas_new(gdev, GDEV_VAS_SIZE, h);
- if (!vas)
+ if (!vas) {
+ GDEV_PRINT("Failed to create a virtual address space object\n");
goto fail_vas;
+ }
/* create a new GPU context object. */
ctx = gdev_ctx_new(gdev, vas);
- if (!ctx)
+ if (!ctx) {
+ GDEV_PRINT("Failed to create a context object\n");
goto fail_ctx;
+ }
/* allocate static bounce bound buffer objects. */
dma_mem = __malloc_dma(vas, GDEV_CHUNK_DEFAULT_SIZE, h->pipeline_count);
- if (!dma_mem)
+ if (!dma_mem) {
+ GDEV_PRINT("Failed to allocate static DMA buffer object\n");
goto fail_dma;
+ }
+ /* allocate a scheduling entity. */
+ se = gdev_sched_entity_create(gdev, ctx);
+ if (!se) {
+ GDEV_PRINT("Failed to allocate scheduling entity\n");
+ goto fail_se;
+ }
+
+ /* save the objects to the handle. */
+ h->se = se;
h->dma_mem = dma_mem;
h->vas = vas;
h->ctx = ctx;
@@ -592,18 +614,15 @@ struct gdev_handle *gopen(int minor)
return h;
+fail_se:
+ __free_dma(dma_mem, h->pipeline_count);
fail_dma:
- GDEV_PRINT("Failed to allocate static DMA buffer object\n");
gdev_ctx_free(ctx);
fail_ctx:
- GDEV_PRINT("Failed to create a context object\n");
gdev_vas_free(vas);
fail_vas:
- GDEV_PRINT("Failed to create a virtual address space object\n");
gdev_dev_close(gdev);
fail_open:
- GDEV_PRINT("Failed to open gdev%d\n", minor);
-
return NULL;
}
@@ -615,12 +634,15 @@ int gclose(struct gdev_handle *h)
{
if (!h)
return -ENOENT;
- if (!h->gdev || !h->ctx || !h->vas)
+ if (!h->gdev || !h->ctx || !h->vas || !h->se)
return -ENOENT;
/* delete the VAS object from the device VAS list. */
gdev_vas_list_del(h->vas);
+ /* free the scheduling entity. */
+ gdev_sched_entity_destroy(h->se);
+
/* free the bounce buffer. */
if (h->dma_mem)
__free_dma(h->dma_mem, h->pipeline_count);
@@ -662,7 +684,7 @@ uint64_t gmalloc(struct gdev_handle *h, uint64_t size)
gdev_mem_list_add(mem, GDEV_MEM_DEVICE);
- return GDEV_MEM_ADDR(mem);
+ return gdev_mem_get_addr(mem);
fail:
gdev->mem_used -= size;
@@ -682,7 +704,7 @@ uint64_t gfree(struct gdev_handle *h, uint64_t addr)
if (!(mem = gdev_mem_lookup(vas, addr, GDEV_MEM_DEVICE)))
goto fail;
- size = GDEV_MEM_SIZE(mem);
+ size = gdev_mem_get_size(mem);
gdev_mem_list_del(mem);
gdev_mem_free(mem);
@@ -712,7 +734,7 @@ void *gmalloc_dma(struct gdev_handle *h, uint64_t size)
goto fail;
gdev_mem_list_add(mem, GDEV_MEM_DMA);
- return GDEV_MEM_BUF(mem);
+ return gdev_mem_get_buf(mem);
fail:
gdev->dma_mem_used -= size;
@@ -732,7 +754,7 @@ uint64_t gfree_dma(struct gdev_handle *h, void *buf)
if (!(mem = gdev_mem_lookup(vas, (uint64_t)buf, GDEV_MEM_DMA)))
goto fail;
- size = GDEV_MEM_SIZE(mem);
+ size = gdev_mem_get_size(mem);
gdev_mem_list_del(mem);
gdev_mem_free(mem);
@@ -866,8 +888,9 @@ int glaunch(struct gdev_handle *h, struct gdev_kernel *kernel, uint32_t *id)
{
gdev_vas_t *vas = h->vas;
gdev_ctx_t *ctx = h->ctx;
+ struct gdev_sched_entity *se = h->se;
- //gdev_schedule_launch(ctx);
+ gdev_schedule_launch(se);
gdev_shmem_lock_all(vas);
gdev_shmem_reload_all(ctx, vas); /* this reloads data only if necessary */
View
@@ -1,5 +1,9 @@
/*
* Copyright 2011 Shinpei Kato
+ *
+ * University of California, Santa Cruz
+ * Systems Research Lab.
+ *
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
View
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2011 Shinpei Kato
+ *
+ * University of California, Santa Cruz
+ * Systems Research Lab.
+ *
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __GDEV_ARCH_H__
+#define __GDEV_ARCH_H__
+
+#include "gdev_conf.h"
+#include "gdev_time.h"
+
+struct gdev_device; /* prototype declaration */
+
+/**
+ * Gdev types: they are not exposed to end users.
+ */
+typedef struct gdev_vas gdev_vas_t;
+typedef struct gdev_ctx gdev_ctx_t;
+typedef struct gdev_mem gdev_mem_t;
+
+/**
+ * architecture-dependent compute functions.
+ */
+int gdev_compute_setup(struct gdev_device *gdev);
+uint32_t gdev_launch(gdev_ctx_t *ctx, struct gdev_kernel *kern);
+uint32_t gdev_memcpy(gdev_ctx_t *ctx, uint64_t dst_addr, uint64_t src_addr, uint32_t size, int async);
+uint32_t gdev_read32(gdev_mem_t *mem, uint64_t addr);
+void gdev_write32(gdev_mem_t *mem, uint64_t addr, uint32_t val);
+int gdev_read(gdev_mem_t *mem, void *buf, uint64_t addr, uint32_t size);
+int gdev_write(gdev_mem_t *mem, uint64_t addr, const void *buf, uint32_t size);
+int gdev_poll(gdev_ctx_t *ctx, uint32_t seq, struct gdev_time *timeout);
+int gdev_query(struct gdev_device *gdev, uint32_t type, uint64_t *result);
+
+/**
+ * architecture-dependent resource management functions.
+ */
+struct gdev_device *gdev_dev_open(int minor);
+void gdev_dev_close(struct gdev_device *gdev);
+gdev_vas_t *gdev_vas_new(struct gdev_device *gdev, uint64_t size, void *handle);
+void gdev_vas_free(gdev_vas_t *vas);
+gdev_ctx_t *gdev_ctx_new(struct gdev_device *gdev, gdev_vas_t *vas);
+void gdev_ctx_free(gdev_ctx_t *ctx);
+int gdev_ctx_get_cid(gdev_ctx_t *ctx);
+gdev_mem_t *gdev_mem_alloc(gdev_vas_t *vas, uint64_t size, int type);
+void gdev_mem_free(gdev_mem_t *mem);
+void gdev_mem_gc(gdev_vas_t *vas);
+int gdev_shmem_evict(gdev_ctx_t *ctx, gdev_mem_t *mem);
+int gdev_shmem_evict_all(gdev_ctx_t *ctx, gdev_vas_t *vas);
+int gdev_shmem_reload(gdev_ctx_t *ctx, gdev_mem_t *mem);
+int gdev_shmem_reload_all(gdev_ctx_t *ctx, gdev_vas_t *vas);
+gdev_mem_t *gdev_shmem_request(gdev_vas_t *vas, gdev_mem_t *mem, uint64_t size);
+void gdev_shmem_lock(gdev_mem_t *mem);
+void gdev_shmem_unlock(gdev_mem_t *mem);
+void gdev_shmem_lock_all(gdev_vas_t *vas);
+void gdev_shmem_unlock_all(gdev_vas_t *vas);
+int gdev_swap_create(struct gdev_device *gdev, uint32_t size);
+void gdev_swap_destroy(struct gdev_device *gdev);
+void gdev_vas_list_add(gdev_vas_t *vas);
+void gdev_vas_list_del(gdev_vas_t *vas);
+void gdev_mem_list_add(gdev_mem_t *mem, int type);
+void gdev_mem_list_del(gdev_mem_t *mem);
+gdev_mem_t *gdev_mem_lookup(gdev_vas_t *vas, uint64_t addr, int type);
+void *gdev_mem_get_buf(gdev_mem_t *mem);
+uint64_t gdev_mem_get_addr(gdev_mem_t *mem);
+uint64_t gdev_mem_get_size(gdev_mem_t *mem);
+
+#endif
@@ -26,7 +26,14 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/
-#include "gdev_init.h"
+#include "gdev_api.h"
+#include "gdev_device.h"
+#include "gdev_system.h"
+
+int gdev_count = 0; /* # of physical devices. */
+int gdev_vcount = 0; /* # of virtual devices. */
+struct gdev_device *gdevs = NULL; /* physical devices */
+struct gdev_device *gdev_vds = NULL; /* virtual devices */
/* initialize the physical device information. */
int gdev_init_device(struct gdev_device *gdev, int minor, void *priv)
@@ -44,8 +51,8 @@ int gdev_init_device(struct gdev_device *gdev, int minor, void *priv)
gdev->parent = NULL;
gdev->priv = priv; /* this must be set before calls to gdev_query(). */
gdev_list_init(&gdev->vas_list, NULL); /* VAS list. */
- LOCK_INIT(&gdev->vas_lock);
- MUTEX_INIT(&gdev->shmem_mutex);
+ gdev_lock_init(&gdev->vas_lock);
+ gdev_mutex_init(&gdev->shmem_mutex);
/* architecture-dependent chipset.
this call must be prior to the following. */
@@ -63,9 +70,6 @@ int gdev_init_device(struct gdev_device *gdev, int minor, void *priv)
/* set up the compute engine. */
gdev_compute_setup(gdev);
- GDEV_PRINT("Device %d: memory size (device 0x%llx, host 0x%llx)\n",
- gdev->id, gdev->mem_size, gdev->dma_mem_size);
-
return 0;
}
@@ -75,7 +79,7 @@ void gdev_exit_device(struct gdev_device *gdev)
}
/* initialize the virtual device information. */
-int gdev_init_vdevice
+int gdev_init_virtual_device
(struct gdev_device *gdev, int id, uint32_t proc_util, uint32_t mem_util,
struct gdev_device *phys)
{
@@ -92,16 +96,21 @@ int gdev_init_vdevice
gdev->dma_mem_size = phys->dma_mem_size * mem_util / 100;
gdev->chipset = phys->chipset;
gdev_list_init(&gdev->vas_list, NULL); /* VAS list. */
- LOCK_INIT(&gdev->vas_lock);
- MUTEX_INIT(&gdev->shmem_mutex);
+ gdev_lock_init(&gdev->vas_lock);
+ gdev_mutex_init(&gdev->shmem_mutex);
- GDEV_PRINT("Virtual device %d: memory size (device 0x%llx, host 0x%llx)\n",
- gdev->id, gdev->mem_size, gdev->dma_mem_size);
+ /* create the swap memory object, if configured, for the virtual device. */
+ if (GDEV_SWAP_MEM_SIZE > 0) {
+ gdev_swap_create(gdev, GDEV_SWAP_MEM_SIZE);
+ }
return 0;
}
/* finalize the virtual device. */
-void gdev_exit_vdevice(struct gdev_device *gdev)
+void gdev_exit_virtual_device(struct gdev_device *gdev)
{
+ if (GDEV_SWAP_MEM_SIZE > 0) {
+ gdev_swap_destroy(gdev);
+ }
}
Oops, something went wrong.

0 comments on commit fe052f5

Please sign in to comment.