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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
* Copyright (c) 2017-2019 Lima Project
*
* 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, sub license,
* 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 NON-INFRINGEMENT. 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 "util/u_memory.h"
#include "util/u_blitter.h"
#include "util/u_upload_mgr.h"
#include "util/u_math.h"
#include "util/u_debug.h"
#include "util/ralloc.h"
#include "util/u_inlines.h"
#include "util/hash_table.h"
#include "lima_screen.h"
#include "lima_context.h"
#include "lima_resource.h"
#include "lima_bo.h"
#include "lima_job.h"
#include "lima_util.h"
#include "lima_fence.h"
#include <drm-uapi/lima_drm.h>
#include <xf86drm.h>
int lima_ctx_num_plb = LIMA_CTX_PLB_DEF_NUM;
uint32_t
lima_ctx_buff_va(struct lima_context *ctx, enum lima_ctx_buff buff)
{
struct lima_job *job = lima_job_get(ctx);
struct lima_ctx_buff_state *cbs = ctx->buffer_state + buff;
struct lima_resource *res = lima_resource(cbs->res);
int pipe = buff < lima_ctx_buff_num_gp ? LIMA_PIPE_GP : LIMA_PIPE_PP;
lima_job_add_bo(job, pipe, res->bo, LIMA_SUBMIT_BO_READ);
return res->bo->va + cbs->offset;
}
void *
lima_ctx_buff_map(struct lima_context *ctx, enum lima_ctx_buff buff)
{
struct lima_ctx_buff_state *cbs = ctx->buffer_state + buff;
struct lima_resource *res = lima_resource(cbs->res);
return lima_bo_map(res->bo) + cbs->offset;
}
void *
lima_ctx_buff_alloc(struct lima_context *ctx, enum lima_ctx_buff buff,
unsigned size)
{
struct lima_ctx_buff_state *cbs = ctx->buffer_state + buff;
void *ret = NULL;
cbs->size = align(size, 0x40);
u_upload_alloc(ctx->uploader, 0, cbs->size, 0x40, &cbs->offset,
&cbs->res, &ret);
return ret;
}
static int
lima_context_create_drm_ctx(struct lima_screen *screen)
{
struct drm_lima_ctx_create req = {0};
int ret = drmIoctl(screen->fd, DRM_IOCTL_LIMA_CTX_CREATE, &req);
if (ret)
return errno;
return req.id;
}
static void
lima_context_free_drm_ctx(struct lima_screen *screen, int id)
{
struct drm_lima_ctx_free req = {
.id = id,
};
drmIoctl(screen->fd, DRM_IOCTL_LIMA_CTX_FREE, &req);
}
static void
lima_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
{
struct lima_context *ctx = lima_context(pctx);
struct hash_entry *entry = _mesa_hash_table_search(ctx->write_jobs, prsc);
if (!entry)
return;
struct lima_job *job = entry->data;
if (job->key.zsbuf && (job->key.zsbuf->texture == prsc))
job->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
if (job->key.cbuf && (job->key.cbuf->texture == prsc))
job->resolve &= ~PIPE_CLEAR_COLOR0;
_mesa_hash_table_remove_key(ctx->write_jobs, prsc);
}
static void
plb_pp_stream_delete_fn(struct hash_entry *entry)
{
struct lima_ctx_plb_pp_stream *s = entry->data;
lima_bo_unreference(s->bo);
list_del(&s->lru_list);
ralloc_free(s);
}
static void
lima_context_destroy(struct pipe_context *pctx)
{
struct lima_context *ctx = lima_context(pctx);
struct lima_screen *screen = lima_screen(pctx->screen);
lima_job_fini(ctx);
for (int i = 0; i < lima_ctx_buff_num; i++)
pipe_resource_reference(&ctx->buffer_state[i].res, NULL);
lima_state_fini(ctx);
if (ctx->blitter)
util_blitter_destroy(ctx->blitter);
if (ctx->uploader)
u_upload_destroy(ctx->uploader);
slab_destroy_child(&ctx->transfer_pool);
for (int i = 0; i < LIMA_CTX_PLB_MAX_NUM; i++) {
if (ctx->plb[i])
lima_bo_unreference(ctx->plb[i]);
if (ctx->gp_tile_heap[i])
lima_bo_unreference(ctx->gp_tile_heap[i]);
}
if (ctx->plb_gp_stream)
lima_bo_unreference(ctx->plb_gp_stream);
if (ctx->gp_output)
lima_bo_unreference(ctx->gp_output);
_mesa_hash_table_destroy(ctx->plb_pp_stream,
plb_pp_stream_delete_fn);
lima_context_free_drm_ctx(screen, ctx->id);
ralloc_free(ctx);
}
static uint32_t
plb_pp_stream_hash(const void *key)
{
return _mesa_hash_data(key, sizeof(struct lima_ctx_plb_pp_stream_key));
}
static bool
plb_pp_stream_compare(const void *key1, const void *key2)
{
return memcmp(key1, key2, sizeof(struct lima_ctx_plb_pp_stream_key)) == 0;
}
static void
lima_set_debug_callback(struct pipe_context *pctx,
const struct pipe_debug_callback *cb)
{
struct lima_context *ctx = lima_context(pctx);
if (cb)
ctx->debug = *cb;
else
memset(&ctx->debug, 0, sizeof(ctx->debug));
}
struct pipe_context *
lima_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
{
struct lima_screen *screen = lima_screen(pscreen);
struct lima_context *ctx;
ctx = rzalloc(screen, struct lima_context);
if (!ctx)
return NULL;
ctx->id = lima_context_create_drm_ctx(screen);
if (ctx->id < 0) {
ralloc_free(ctx);
return NULL;
}
ctx->base.screen = pscreen;
ctx->base.destroy = lima_context_destroy;
ctx->base.set_debug_callback = lima_set_debug_callback;
ctx->base.invalidate_resource = lima_invalidate_resource;
lima_resource_context_init(ctx);
lima_fence_context_init(ctx);
lima_state_init(ctx);
lima_draw_init(ctx);
lima_program_init(ctx);
lima_query_init(ctx);
slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
ctx->blitter = util_blitter_create(&ctx->base);
if (!ctx->blitter)
goto err_out;
ctx->uploader = u_upload_create_default(&ctx->base);
if (!ctx->uploader)
goto err_out;
ctx->base.stream_uploader = ctx->uploader;
ctx->base.const_uploader = ctx->uploader;
ctx->plb_size = screen->plb_max_blk * LIMA_CTX_PLB_BLK_SIZE;
ctx->plb_gp_size = screen->plb_max_blk * 4;
uint32_t heap_flags;
if (screen->has_growable_heap_buffer) {
/* growable size buffer, initially will allocate 32K (by default)
* backup memory in kernel driver, and will allocate more when GP
* get out of memory interrupt. Max to 16M set here.
*/
ctx->gp_tile_heap_size = 0x1000000;
heap_flags = LIMA_BO_FLAG_HEAP;
} else {
/* fix size buffer */
ctx->gp_tile_heap_size = 0x100000;
heap_flags = 0;
}
for (int i = 0; i < lima_ctx_num_plb; i++) {
ctx->plb[i] = lima_bo_create(screen, ctx->plb_size, 0);
if (!ctx->plb[i])
goto err_out;
ctx->gp_tile_heap[i] = lima_bo_create(screen, ctx->gp_tile_heap_size, heap_flags);
if (!ctx->gp_tile_heap[i])
goto err_out;
}
unsigned plb_gp_stream_size =
align(ctx->plb_gp_size * lima_ctx_num_plb, LIMA_PAGE_SIZE);
ctx->plb_gp_stream =
lima_bo_create(screen, plb_gp_stream_size, 0);
if (!ctx->plb_gp_stream)
goto err_out;
lima_bo_map(ctx->plb_gp_stream);
/* plb gp stream is static for any framebuffer */
for (int i = 0; i < lima_ctx_num_plb; i++) {
uint32_t *plb_gp_stream = ctx->plb_gp_stream->map + i * ctx->plb_gp_size;
for (int j = 0; j < screen->plb_max_blk; j++)
plb_gp_stream[j] = ctx->plb[i]->va + LIMA_CTX_PLB_BLK_SIZE * j;
}
list_inithead(&ctx->plb_pp_stream_lru_list);
ctx->plb_pp_stream = _mesa_hash_table_create(
ctx, plb_pp_stream_hash, plb_pp_stream_compare);
if (!ctx->plb_pp_stream)
goto err_out;
if (!lima_job_init(ctx))
goto err_out;
return &ctx->base;
err_out:
lima_context_destroy(&ctx->base);
return NULL;
}
|