blob: 2872b8775d982e6056d75428c50be02580b5c003 [file] [log] [blame]
Chia-I Wubdf4c562014-08-07 06:36:33 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include "kmd/winsys.h"
Chia-I Wu34f45182014-08-19 14:02:59 +080026#include "cmd.h"
Chia-I Wubdf4c562014-08-07 06:36:33 +080027#include "dev.h"
28#include "fence.h"
29
Chia-I Wu26f0bd02014-08-07 10:38:40 +080030static void fence_destroy(struct intel_obj *obj)
Chia-I Wubdf4c562014-08-07 06:36:33 +080031{
32 struct intel_fence *fence = intel_fence_from_obj(obj);
33
34 intel_fence_destroy(fence);
35}
36
37XGL_RESULT intel_fence_create(struct intel_dev *dev,
38 const XGL_FENCE_CREATE_INFO *info,
39 struct intel_fence **fence_ret)
40{
41 struct intel_fence *fence;
42
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -060043 fence = (struct intel_fence *) intel_base_create(dev, sizeof(*fence),
Chia-I Wubbf2c932014-08-07 12:20:08 +080044 dev->base.dbg, XGL_DBG_OBJECT_FENCE, info, 0);
Chia-I Wubdf4c562014-08-07 06:36:33 +080045 if (!fence)
46 return XGL_ERROR_OUT_OF_MEMORY;
47
Chia-I Wu26f0bd02014-08-07 10:38:40 +080048 fence->obj.destroy = fence_destroy;
Chia-I Wubdf4c562014-08-07 06:36:33 +080049
Chia-I Wubdf4c562014-08-07 06:36:33 +080050 *fence_ret = fence;
51
52 return XGL_SUCCESS;
53}
54
55void intel_fence_destroy(struct intel_fence *fence)
56{
Chia-I Wubbf2c932014-08-07 12:20:08 +080057 intel_base_destroy(&fence->obj.base);
Chia-I Wubdf4c562014-08-07 06:36:33 +080058}
59
60XGL_RESULT intel_fence_get_status(struct intel_fence *fence)
61{
Chia-I Wue24c3292014-08-21 14:05:23 +080062 struct intel_bo *bo = (fence->cmd) ?
63 intel_cmd_get_batch(fence->cmd, NULL) : NULL;
64
65 if (!bo)
Chia-I Wubdf4c562014-08-07 06:36:33 +080066 return XGL_ERROR_UNAVAILABLE;
67
Chia-I Wue24c3292014-08-21 14:05:23 +080068 return (intel_bo_is_busy(bo)) ? XGL_NOT_READY : XGL_SUCCESS;
Chia-I Wubdf4c562014-08-07 06:36:33 +080069}
70
71XGL_RESULT intel_fence_wait(struct intel_fence *fence, int64_t timeout_ns)
72{
Chia-I Wue24c3292014-08-21 14:05:23 +080073 struct intel_bo *bo = (fence->cmd) ?
74 intel_cmd_get_batch(fence->cmd, NULL) : NULL;
Chia-I Wubdf4c562014-08-07 06:36:33 +080075 int err;
76
Chia-I Wue24c3292014-08-21 14:05:23 +080077 if (!bo)
Chia-I Wubdf4c562014-08-07 06:36:33 +080078 return XGL_ERROR_UNAVAILABLE;
79
Chia-I Wue24c3292014-08-21 14:05:23 +080080 err = intel_bo_wait(bo, timeout_ns);
Chia-I Wubdf4c562014-08-07 06:36:33 +080081
82 return (err) ? XGL_NOT_READY : XGL_SUCCESS;
83}
84
85XGL_RESULT XGLAPI intelCreateFence(
86 XGL_DEVICE device,
87 const XGL_FENCE_CREATE_INFO* pCreateInfo,
88 XGL_FENCE* pFence)
89{
90 struct intel_dev *dev = intel_dev(device);
91
92 return intel_fence_create(dev, pCreateInfo,
93 (struct intel_fence **) pFence);
94}
95
96XGL_RESULT XGLAPI intelGetFenceStatus(
97 XGL_FENCE fence_)
98{
99 struct intel_fence *fence = intel_fence(fence_);
100
101 return intel_fence_get_status(fence);
102}
103
104XGL_RESULT XGLAPI intelWaitForFences(
105 XGL_DEVICE device,
106 XGL_UINT fenceCount,
107 const XGL_FENCE* pFences,
108 XGL_BOOL waitAll,
109 XGL_UINT64 timeout)
110{
111 XGL_RESULT ret = XGL_SUCCESS;
112 XGL_UINT i;
113
114 for (i = 0; i < fenceCount; i++) {
115 struct intel_fence *fence = intel_fence(pFences[i]);
116 int64_t ns;
117 XGL_RESULT r;
118
119 if (timeout <= (XGL_UINT64) (INT64_MAX / 1000 / 1000 / 1000))
120 ns = timeout * 1000 * 1000 * 1000;
121 else
122 ns = -1;
123
124 r = intel_fence_wait(fence, ns);
125
126 if (!waitAll && r == XGL_SUCCESS)
127 return XGL_SUCCESS;
128
129 if (r != XGL_SUCCESS)
130 ret = r;
131 }
132
133 return ret;
134}