1 /*
2  * Copyright (c) 2017-2019 Lima Project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sub license,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #include "util/u_memory.h"
26 #include "util/u_blitter.h"
27 #include "util/u_upload_mgr.h"
28 #include "util/u_math.h"
29 #include "util/u_debug.h"
30 #include "util/ralloc.h"
31 #include "util/u_inlines.h"
32 #include "util/hash_table.h"
33 
34 #include "lima_screen.h"
35 #include "lima_context.h"
36 #include "lima_resource.h"
37 #include "lima_bo.h"
38 #include "lima_job.h"
39 #include "lima_util.h"
40 #include "lima_fence.h"
41 
42 #include <drm-uapi/lima_drm.h>
43 #include <xf86drm.h>
44 
45 int lima_ctx_num_plb = LIMA_CTX_PLB_DEF_NUM;
46 
47 uint32_t
lima_ctx_buff_va(struct lima_context * ctx,enum lima_ctx_buff buff)48 lima_ctx_buff_va(struct lima_context *ctx, enum lima_ctx_buff buff)
49 {
50    struct lima_job *job = lima_job_get(ctx);
51    struct lima_ctx_buff_state *cbs = ctx->buffer_state + buff;
52    struct lima_resource *res = lima_resource(cbs->res);
53    int pipe = buff < lima_ctx_buff_num_gp ? LIMA_PIPE_GP : LIMA_PIPE_PP;
54 
55    lima_job_add_bo(job, pipe, res->bo, LIMA_SUBMIT_BO_READ);
56 
57    return res->bo->va + cbs->offset;
58 }
59 
60 void *
lima_ctx_buff_map(struct lima_context * ctx,enum lima_ctx_buff buff)61 lima_ctx_buff_map(struct lima_context *ctx, enum lima_ctx_buff buff)
62 {
63    struct lima_ctx_buff_state *cbs = ctx->buffer_state + buff;
64    struct lima_resource *res = lima_resource(cbs->res);
65 
66    return lima_bo_map(res->bo) + cbs->offset;
67 }
68 
69 void *
lima_ctx_buff_alloc(struct lima_context * ctx,enum lima_ctx_buff buff,unsigned size)70 lima_ctx_buff_alloc(struct lima_context *ctx, enum lima_ctx_buff buff,
71                     unsigned size)
72 {
73    struct lima_ctx_buff_state *cbs = ctx->buffer_state + buff;
74    void *ret = NULL;
75 
76    cbs->size = align(size, 0x40);
77 
78    u_upload_alloc(ctx->uploader, 0, cbs->size, 0x40, &cbs->offset,
79                   &cbs->res, &ret);
80 
81    return ret;
82 }
83 
84 static int
lima_context_create_drm_ctx(struct lima_screen * screen)85 lima_context_create_drm_ctx(struct lima_screen *screen)
86 {
87    struct drm_lima_ctx_create req = {0};
88 
89    int ret = drmIoctl(screen->fd, DRM_IOCTL_LIMA_CTX_CREATE, &req);
90    if (ret)
91       return errno;
92 
93    return req.id;
94 }
95 
96 static void
lima_context_free_drm_ctx(struct lima_screen * screen,int id)97 lima_context_free_drm_ctx(struct lima_screen *screen, int id)
98 {
99    struct drm_lima_ctx_free req = {
100       .id = id,
101    };
102 
103    drmIoctl(screen->fd, DRM_IOCTL_LIMA_CTX_FREE, &req);
104 }
105 
106 static void
lima_invalidate_resource(struct pipe_context * pctx,struct pipe_resource * prsc)107 lima_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
108 {
109    struct lima_context *ctx = lima_context(pctx);
110 
111    struct hash_entry *entry = _mesa_hash_table_search(ctx->write_jobs, prsc);
112    if (!entry)
113       return;
114 
115    struct lima_job *job = entry->data;
116    if (job->key.zsbuf && (job->key.zsbuf->texture == prsc))
117       job->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
118 
119    if (job->key.cbuf && (job->key.cbuf->texture == prsc))
120       job->resolve &= ~PIPE_CLEAR_COLOR0;
121 
122    _mesa_hash_table_remove_key(ctx->write_jobs, prsc);
123 }
124 
125 static void
plb_pp_stream_delete_fn(struct hash_entry * entry)126 plb_pp_stream_delete_fn(struct hash_entry *entry)
127 {
128    struct lima_ctx_plb_pp_stream *s = entry->data;
129 
130    lima_bo_unreference(s->bo);
131    list_del(&s->lru_list);
132    ralloc_free(s);
133 }
134 
135 static void
lima_context_destroy(struct pipe_context * pctx)136 lima_context_destroy(struct pipe_context *pctx)
137 {
138    struct lima_context *ctx = lima_context(pctx);
139    struct lima_screen *screen = lima_screen(pctx->screen);
140 
141    lima_job_fini(ctx);
142 
143    for (int i = 0; i < lima_ctx_buff_num; i++)
144       pipe_resource_reference(&ctx->buffer_state[i].res, NULL);
145 
146    lima_program_fini(ctx);
147    lima_state_fini(ctx);
148 
149    if (ctx->blitter)
150       util_blitter_destroy(ctx->blitter);
151 
152    if (ctx->uploader)
153       u_upload_destroy(ctx->uploader);
154 
155    slab_destroy_child(&ctx->transfer_pool);
156 
157    for (int i = 0; i < LIMA_CTX_PLB_MAX_NUM; i++) {
158       if (ctx->plb[i])
159          lima_bo_unreference(ctx->plb[i]);
160       if (ctx->gp_tile_heap[i])
161          lima_bo_unreference(ctx->gp_tile_heap[i]);
162    }
163 
164    if (ctx->plb_gp_stream)
165       lima_bo_unreference(ctx->plb_gp_stream);
166 
167    if (ctx->gp_output)
168       lima_bo_unreference(ctx->gp_output);
169 
170    _mesa_hash_table_destroy(ctx->plb_pp_stream,
171                             plb_pp_stream_delete_fn);
172 
173    lima_context_free_drm_ctx(screen, ctx->id);
174 
175    ralloc_free(ctx);
176 }
177 
178 static uint32_t
plb_pp_stream_hash(const void * key)179 plb_pp_stream_hash(const void *key)
180 {
181    return _mesa_hash_data(key, sizeof(struct lima_ctx_plb_pp_stream_key));
182 }
183 
184 static bool
plb_pp_stream_compare(const void * key1,const void * key2)185 plb_pp_stream_compare(const void *key1, const void *key2)
186 {
187    return memcmp(key1, key2, sizeof(struct lima_ctx_plb_pp_stream_key)) == 0;
188 }
189 
190 static void
lima_set_debug_callback(struct pipe_context * pctx,const struct pipe_debug_callback * cb)191 lima_set_debug_callback(struct pipe_context *pctx,
192                         const struct pipe_debug_callback *cb)
193 {
194    struct lima_context *ctx = lima_context(pctx);
195 
196    if (cb)
197       ctx->debug = *cb;
198    else
199       memset(&ctx->debug, 0, sizeof(ctx->debug));
200 }
201 
202 struct pipe_context *
lima_context_create(struct pipe_screen * pscreen,void * priv,unsigned flags)203 lima_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
204 {
205    struct lima_screen *screen = lima_screen(pscreen);
206    struct lima_context *ctx;
207 
208    ctx = rzalloc(screen, struct lima_context);
209    if (!ctx)
210       return NULL;
211 
212    ctx->id = lima_context_create_drm_ctx(screen);
213    if (ctx->id < 0) {
214       ralloc_free(ctx);
215       return NULL;
216    }
217 
218    ctx->base.screen = pscreen;
219    ctx->base.destroy = lima_context_destroy;
220    ctx->base.set_debug_callback = lima_set_debug_callback;
221    ctx->base.invalidate_resource = lima_invalidate_resource;
222 
223    lima_resource_context_init(ctx);
224    lima_fence_context_init(ctx);
225    lima_state_init(ctx);
226    lima_draw_init(ctx);
227    lima_program_init(ctx);
228    lima_query_init(ctx);
229 
230    slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
231 
232    ctx->blitter = util_blitter_create(&ctx->base);
233    if (!ctx->blitter)
234       goto err_out;
235 
236    ctx->uploader = u_upload_create_default(&ctx->base);
237    if (!ctx->uploader)
238       goto err_out;
239    ctx->base.stream_uploader = ctx->uploader;
240    ctx->base.const_uploader = ctx->uploader;
241 
242    ctx->plb_size = screen->plb_max_blk * LIMA_CTX_PLB_BLK_SIZE;
243    ctx->plb_gp_size = screen->plb_max_blk * 4;
244 
245    uint32_t heap_flags;
246    if (screen->has_growable_heap_buffer) {
247       /* growable size buffer, initially will allocate 32K (by default)
248        * backup memory in kernel driver, and will allocate more when GP
249        * get out of memory interrupt. Max to 16M set here.
250        */
251       ctx->gp_tile_heap_size = 0x1000000;
252       heap_flags = LIMA_BO_FLAG_HEAP;
253    } else {
254       /* fix size buffer */
255       ctx->gp_tile_heap_size = 0x100000;
256       heap_flags = 0;
257    }
258 
259    for (int i = 0; i < lima_ctx_num_plb; i++) {
260       ctx->plb[i] = lima_bo_create(screen, ctx->plb_size, 0);
261       if (!ctx->plb[i])
262          goto err_out;
263       ctx->gp_tile_heap[i] = lima_bo_create(screen, ctx->gp_tile_heap_size, heap_flags);
264       if (!ctx->gp_tile_heap[i])
265          goto err_out;
266    }
267 
268    unsigned plb_gp_stream_size =
269       align(ctx->plb_gp_size * lima_ctx_num_plb, LIMA_PAGE_SIZE);
270    ctx->plb_gp_stream =
271       lima_bo_create(screen, plb_gp_stream_size, 0);
272    if (!ctx->plb_gp_stream)
273       goto err_out;
274    lima_bo_map(ctx->plb_gp_stream);
275 
276    /* plb gp stream is static for any framebuffer */
277    for (int i = 0; i < lima_ctx_num_plb; i++) {
278       uint32_t *plb_gp_stream = ctx->plb_gp_stream->map + i * ctx->plb_gp_size;
279       for (int j = 0; j < screen->plb_max_blk; j++)
280          plb_gp_stream[j] = ctx->plb[i]->va + LIMA_CTX_PLB_BLK_SIZE * j;
281    }
282 
283    list_inithead(&ctx->plb_pp_stream_lru_list);
284    ctx->plb_pp_stream = _mesa_hash_table_create(
285       ctx, plb_pp_stream_hash, plb_pp_stream_compare);
286    if (!ctx->plb_pp_stream)
287       goto err_out;
288 
289    if (!lima_job_init(ctx))
290       goto err_out;
291 
292    return &ctx->base;
293 
294 err_out:
295    lima_context_destroy(&ctx->base);
296    return NULL;
297 }
298