intel: implement fence functions
diff --git a/icd/intel/CMakeLists.txt b/icd/intel/CMakeLists.txt
index 6ac6700..3ecf72c 100644
--- a/icd/intel/CMakeLists.txt
+++ b/icd/intel/CMakeLists.txt
@@ -20,6 +20,7 @@
     dev.c
     dispatch_tables.c
     init_driver.c
+    fence.c
     gpu.c
     mem.c
     obj.c
diff --git a/icd/intel/dispatch_tables.c b/icd/intel/dispatch_tables.c
index ebebfe1..c2e1a21 100644
--- a/icd/intel/dispatch_tables.c
+++ b/icd/intel/dispatch_tables.c
@@ -24,6 +24,7 @@
 
 #include "icd.h"
 #include "dev.h"
+#include "fence.h"
 #include "gpu.h"
 #include "mem.h"
 #include "obj.h"
@@ -103,30 +104,6 @@
     return XGL_ERROR_UNAVAILABLE;
 }
 
-static XGL_RESULT XGLAPI intelCreateFence(
-    XGL_DEVICE                                  device,
-    const XGL_FENCE_CREATE_INFO*                pCreateInfo,
-    XGL_FENCE*                                  pFence)
-{
-    return XGL_ERROR_UNAVAILABLE;
-}
-
-static XGL_RESULT XGLAPI intelGetFenceStatus(
-    XGL_FENCE                                   fence)
-{
-    return XGL_ERROR_UNAVAILABLE;
-}
-
-static XGL_RESULT XGLAPI intelWaitForFences(
-    XGL_DEVICE                                  device,
-    XGL_UINT                                    fenceCount,
-    const XGL_FENCE*                            pFences,
-    XGL_BOOL                                    waitAll,
-    XGL_UINT64                                  timeout)
-{
-    return XGL_ERROR_UNAVAILABLE;
-}
-
 static XGL_RESULT XGLAPI intelCreateQueueSemaphore(
     XGL_DEVICE                                  device,
     const XGL_QUEUE_SEMAPHORE_CREATE_INFO*      pCreateInfo,
diff --git a/icd/intel/fence.c b/icd/intel/fence.c
new file mode 100644
index 0000000..a27bb85
--- /dev/null
+++ b/icd/intel/fence.c
@@ -0,0 +1,146 @@
+/*
+ * XGL
+ *
+ * Copyright (C) 2014 LunarG, Inc.
+ *
+ * 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 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
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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.
+ */
+
+#include "kmd/winsys.h"
+#include "dev.h"
+#include "fence.h"
+
+static void fence_destroy_callback(struct intel_obj *obj)
+{
+    struct intel_fence *fence = intel_fence_from_obj(obj);
+
+    intel_fence_destroy(fence);
+}
+
+XGL_RESULT intel_fence_create(struct intel_dev *dev,
+                              const XGL_FENCE_CREATE_INFO *info,
+                              struct intel_fence **fence_ret)
+{
+    struct intel_fence *fence;
+
+    fence = icd_alloc(sizeof(*fence), 0, XGL_SYSTEM_ALLOC_API_OBJECT);
+    if (!fence)
+        return XGL_ERROR_OUT_OF_MEMORY;
+
+    memset(fence, 0, sizeof(*fence));
+
+    fence->obj.destroy = fence_destroy_callback;
+
+    fence->obj.base.dispatch = dev->base.dispatch;
+    if (dev->base.dbg) {
+        fence->obj.base.dbg =
+            intel_base_dbg_create(XGL_DBG_OBJECT_FENCE, info, sizeof(*info));
+        if (!fence->obj.base.dbg) {
+            icd_free(fence);
+            return XGL_ERROR_OUT_OF_MEMORY;
+        }
+    }
+
+    *fence_ret = fence;
+
+    return XGL_SUCCESS;
+}
+
+void intel_fence_destroy(struct intel_fence *fence)
+{
+    if (fence->submitted_bo)
+        intel_bo_unreference(fence->submitted_bo);
+
+    if (fence->obj.base.dbg)
+        intel_base_dbg_destroy(fence->obj.base.dbg);
+
+    icd_free(fence);
+}
+
+XGL_RESULT intel_fence_get_status(struct intel_fence *fence)
+{
+    if (!fence->submitted_bo)
+        return XGL_ERROR_UNAVAILABLE;
+
+    return (intel_bo_is_busy(fence->submitted_bo)) ?
+        XGL_NOT_READY : XGL_SUCCESS;
+}
+
+XGL_RESULT intel_fence_wait(struct intel_fence *fence, int64_t timeout_ns)
+{
+    int err;
+
+    if (!fence->submitted_bo)
+        return XGL_ERROR_UNAVAILABLE;
+
+    err = intel_bo_wait(fence->submitted_bo, timeout_ns);
+
+    return (err) ? XGL_NOT_READY : XGL_SUCCESS;
+}
+
+XGL_RESULT XGLAPI intelCreateFence(
+    XGL_DEVICE                                  device,
+    const XGL_FENCE_CREATE_INFO*                pCreateInfo,
+    XGL_FENCE*                                  pFence)
+{
+    struct intel_dev *dev = intel_dev(device);
+
+    return intel_fence_create(dev, pCreateInfo,
+            (struct intel_fence **) pFence);
+}
+
+XGL_RESULT XGLAPI intelGetFenceStatus(
+    XGL_FENCE                                   fence_)
+{
+    struct intel_fence *fence = intel_fence(fence_);
+
+    return intel_fence_get_status(fence);
+}
+
+XGL_RESULT XGLAPI intelWaitForFences(
+    XGL_DEVICE                                  device,
+    XGL_UINT                                    fenceCount,
+    const XGL_FENCE*                            pFences,
+    XGL_BOOL                                    waitAll,
+    XGL_UINT64                                  timeout)
+{
+    XGL_RESULT ret = XGL_SUCCESS;
+    XGL_UINT i;
+
+    for (i = 0; i < fenceCount; i++) {
+        struct intel_fence *fence = intel_fence(pFences[i]);
+        int64_t ns;
+        XGL_RESULT r;
+
+        if (timeout <= (XGL_UINT64) (INT64_MAX / 1000 / 1000 / 1000))
+            ns = timeout * 1000 * 1000 * 1000;
+        else
+            ns = -1;
+
+        r = intel_fence_wait(fence, ns);
+
+        if (!waitAll && r == XGL_SUCCESS)
+            return XGL_SUCCESS;
+
+        if (r != XGL_SUCCESS)
+            ret = r;
+    }
+
+    return ret;
+}
diff --git a/icd/intel/fence.h b/icd/intel/fence.h
new file mode 100644
index 0000000..8dd42f0
--- /dev/null
+++ b/icd/intel/fence.h
@@ -0,0 +1,73 @@
+/*
+ * XGL
+ *
+ * Copyright (C) 2014 LunarG, Inc.
+ *
+ * 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 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
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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 FENCE_H
+#define FENCE_H
+
+#include "intel.h"
+#include "obj.h"
+
+struct intel_bo;
+struct intel_dev;
+
+struct intel_fence {
+    struct intel_obj obj;
+
+    struct intel_bo *submitted_bo;
+};
+
+static inline struct intel_fence *intel_fence(XGL_FENCE fence)
+{
+    return (struct intel_fence *) fence;
+}
+
+static inline struct intel_fence *intel_fence_from_obj(struct intel_obj *obj)
+{
+    return (struct intel_fence *) obj;
+}
+
+XGL_RESULT intel_fence_create(struct intel_dev *dev,
+                              const XGL_FENCE_CREATE_INFO *info,
+                              struct intel_fence **fence_ret);
+void intel_fence_destroy(struct intel_fence *fence);
+
+XGL_RESULT intel_fence_get_status(struct intel_fence *fence);
+XGL_RESULT intel_fence_wait(struct intel_fence *fence, int64_t timeout_ns);
+
+XGL_RESULT XGLAPI intelCreateFence(
+    XGL_DEVICE                                  device,
+    const XGL_FENCE_CREATE_INFO*                pCreateInfo,
+    XGL_FENCE*                                  pFence);
+
+XGL_RESULT XGLAPI intelGetFenceStatus(
+    XGL_FENCE                                   fence);
+
+XGL_RESULT XGLAPI intelWaitForFences(
+    XGL_DEVICE                                  device,
+    XGL_UINT                                    fenceCount,
+    const XGL_FENCE*                            pFences,
+    XGL_BOOL                                    waitAll,
+    XGL_UINT64                                  timeout);
+
+#endif /* FENCE_H */