summaryrefslogtreecommitdiff
path: root/src/tegra_stream.c
blob: 03b4f2945b38664a3865ac778e36a1fba497c614 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (c) 2016-2017 Dmitry Osipenko <digetx@gmail.com>
 * Copyright (C) 2012-2013 NVIDIA Corporation.
 *
 * 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\n", 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.
 *
 * Authors:
 *    Arto Merilainen <amerilainen@nvidia.com>
 */

#include "vdpau_tegra.h"

/*
 * tegra_stream_create(channel)
 *
 * Create a stream for given channel. This function preallocates several
 * command buffers for later usage to improve performance. Streams are
 * used for generating command buffers opcode by opcode using
 * tegra_stream_push().
 */

int tegra_stream_create(struct drm_tegra *drm,
                        struct drm_tegra_channel *channel,
                        struct tegra_stream *stream,
                        uint32_t words_num)
{
    stream->status    = TEGRADRM_STREAM_FREE;
    stream->channel   = channel;
    stream->num_words = words_num ?: 1;

    return 0;
}

/*
 * tegra_stream_destroy(stream)
 *
 * Destroy the given stream object. All resrouces are released.
 */

void tegra_stream_destroy(struct tegra_stream *stream)
{
    if (!stream)
        return;

    drm_tegra_job_free(stream->job);
}

/*
 * tegra_stream_flush(stream, fence)
 *
 * Send the current contents of stream buffer. The stream must be
 * synchronized correctly (we cannot send partial streams). If
 * pointer to fence is given, the fence will contain the syncpoint value
 * that is reached when operations in the buffer are finished.
 */

int tegra_stream_flush(struct tegra_stream *stream)
{
    int result = 0;

    if (!stream)
        return -1;

    /* Reflushing is fine */
    if (stream->status == TEGRADRM_STREAM_FREE)
        return 0;

    /* Return error if stream is constructed badly */
    if (stream->status != TEGRADRM_STREAM_READY) {
        result = -1;
        goto cleanup;
    }

    result = drm_tegra_job_submit(stream->job, 1000);
    if (result != 0) {
        ErrorMsg("drm_tegra_job_submit() failed %d\n", result);
        result = -1;
        goto cleanup;
    }

    result = drm_tegra_job_wait(stream->job);
    if (result != 0) {
        ErrorMsg("drm_tegra_fence_wait_timeout() failed %d\n", result);
        result = -1;
    }

cleanup:
    drm_tegra_job_free(stream->job);

    stream->job = NULL;
    stream->status = TEGRADRM_STREAM_FREE;

    return result;
}

/*
 * tegra_stream_begin(stream, num_words, fence, num_fences, num_syncpt_incrs,
 *          num_relocs, class_id)
 *
 * Start constructing a stream.
 *  - num_words refer to the maximum number of words the stream can contain.
 *  - fence is a pointer to a table that contains syncpoint preconditions
 *    before the stream execution can start.
 *  - num_fences indicate the number of elements in the fence table.
 *  - num_relocs indicate the number of memory references in the buffer.
 *  - class_id refers to the class_id that is selected in the beginning of a
 *    stream. If no class id is given, the default class id (=usually the
 *    client device's class) is selected.
 *
 * This function verifies that the current buffer has enough room for holding
 * the whole stream (this is computed using num_words and num_relocs). The
 * function blocks until the stream buffer is ready for use.
 */

int tegra_stream_begin(struct tegra_stream *stream)
{
    int ret;

    if (!stream->channel) {
        return -1;
    }

    /* check stream and its state */
    if (!(stream && stream->status == TEGRADRM_STREAM_FREE)) {
        ErrorMsg("Stream status isn't FREE\n");
        return -1;
    }

    ret = drm_tegra_job_new(&stream->job, stream->channel);
    if (ret != 0) {
        ErrorMsg("drm_tegra_job_new() failed %d\n", ret);
        return -1;
    }

    ret = drm_tegra_pushbuf_new(&stream->buffer.pushbuf, stream->job);
    if (ret != 0) {
        ErrorMsg("drm_tegra_pushbuf_new() failed %d\n", ret);
        drm_tegra_job_free(stream->job);
        return -1;
    }

    ret = drm_tegra_pushbuf_prepare(stream->buffer.pushbuf, stream->num_words);
    if (ret != 0) {
        ErrorMsg("drm_tegra_pushbuf_prepare() failed %d\n", ret);
        drm_tegra_job_free(stream->job);
        return -1;
    }

    stream->class_id = 0;
    stream->status = TEGRADRM_STREAM_CONSTRUCT;

    return 0;
}

/*
 * tegra_stream_push_reloc(stream, h, offset)
 *
 * Push a memory reference to the stream.
 */

int tegra_stream_push_reloc(struct tegra_stream *stream,
                            struct drm_tegra_bo *bo,
                            unsigned offset)
{
    int ret;

    if (!(stream && stream->status == TEGRADRM_STREAM_CONSTRUCT)) {
        ErrorMsg("Stream status isn't CONSTRUCT\n");
        return -1;
    }

    ret = drm_tegra_pushbuf_relocate(stream->buffer.pushbuf,
                                     bo, offset, 0);
    if (ret != 0) {
        stream->status = TEGRADRM_STREAM_CONSTRUCTION_FAILED;
        ErrorMsg("drm_tegra_pushbuf_relocate() failed %d\n", ret);
        return -1;
    }

    return 0;
}

/*
 * tegra_stream_push(stream, word)
 *
 * Push a single word to given stream.
 */

int tegra_stream_push(struct tegra_stream *stream, uint32_t word)
{
    int ret;

    if (!(stream && stream->status == TEGRADRM_STREAM_CONSTRUCT)) {
        ErrorMsg("Stream status isn't CONSTRUCT\n");
        return -1;
    }

    ret = drm_tegra_pushbuf_prepare(stream->buffer.pushbuf, 1);
    if (ret != 0) {
        stream->status = TEGRADRM_STREAM_CONSTRUCTION_FAILED;
        ErrorMsg("drm_tegra_pushbuf_prepare() failed %d\n", ret);
        return -1;
    }

    *stream->buffer.pushbuf->ptr++ = word;

    return 0;
}

/*
 * tegra_stream_push_setclass(stream, class_id)
 *
 * Push "set class" opcode to the stream. Do nothing if the class is already
 * active
 */

int tegra_stream_push_setclass(struct tegra_stream *stream, unsigned class_id)
{
    int result;

    if (stream->class_id == class_id)
        return 0;

    result = tegra_stream_push(stream, HOST1X_OPCODE_SETCL(0, class_id, 0));

    if (result == 0)
        stream->class_id = class_id;

    return result;
}

/*
 * tegra_stream_end(stream)
 *
 * Mark end of stream. This function pushes last syncpoint increment for
 * marking end of stream.
 */

int tegra_stream_end(struct tegra_stream *stream)
{
    int ret;

    if (!(stream && stream->status == TEGRADRM_STREAM_CONSTRUCT)) {
        ErrorMsg("Stream status isn't CONSTRUCT\n");
        return -1;
    }

    ret = drm_tegra_pushbuf_sync(stream->buffer.pushbuf, 0, 1,
                                 DRM_TEGRA_SYNC_COND_OP_DONE);
    if (ret != 0) {
        stream->status = TEGRADRM_STREAM_CONSTRUCTION_FAILED;
        ErrorMsg("drm_tegra_pushbuf_sync() failed %d\n", ret);
        return -1;
    }

    stream->status = TEGRADRM_STREAM_READY;

    return 0;
}