2 * Copyright (c) 2017-2019 Lima Project
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:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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.
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"
34 #include "lima_screen.h"
35 #include "lima_context.h"
36 #include "lima_resource.h"
38 #include "lima_submit.h"
39 #include "lima_util.h"
40 #include "lima_fence.h"
42 #include <drm-uapi/lima_drm.h>
45 int lima_ctx_num_plb
= LIMA_CTX_PLB_DEF_NUM
;
48 lima_ctx_buff_va(struct lima_context
*ctx
, enum lima_ctx_buff buff
, unsigned submit
)
50 struct lima_ctx_buff_state
*cbs
= ctx
->buffer_state
+ buff
;
51 struct lima_resource
*res
= lima_resource(cbs
->res
);
53 if (submit
& LIMA_CTX_BUFF_SUBMIT_GP
)
54 lima_submit_add_bo(ctx
->gp_submit
, res
->bo
, LIMA_SUBMIT_BO_READ
);
55 if (submit
& LIMA_CTX_BUFF_SUBMIT_PP
)
56 lima_submit_add_bo(ctx
->pp_submit
, res
->bo
, LIMA_SUBMIT_BO_READ
);
58 return res
->bo
->va
+ cbs
->offset
;
62 lima_ctx_buff_map(struct lima_context
*ctx
, enum lima_ctx_buff buff
)
64 struct lima_ctx_buff_state
*cbs
= ctx
->buffer_state
+ buff
;
65 struct lima_resource
*res
= lima_resource(cbs
->res
);
67 return lima_bo_map(res
->bo
) + cbs
->offset
;
71 lima_ctx_buff_alloc(struct lima_context
*ctx
, enum lima_ctx_buff buff
,
74 struct lima_ctx_buff_state
*cbs
= ctx
->buffer_state
+ buff
;
77 cbs
->size
= align(size
, 0x40);
79 u_upload_alloc(ctx
->uploader
, 0, cbs
->size
, 0x40, &cbs
->offset
,
86 lima_context_create_drm_ctx(struct lima_screen
*screen
)
88 struct drm_lima_ctx_create req
= {0};
90 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_LIMA_CTX_CREATE
, &req
);
98 lima_context_free_drm_ctx(struct lima_screen
*screen
, int id
)
100 struct drm_lima_ctx_free req
= {
104 drmIoctl(screen
->fd
, DRM_IOCTL_LIMA_CTX_FREE
, &req
);
108 lima_context_destroy(struct pipe_context
*pctx
)
110 struct lima_context
*ctx
= lima_context(pctx
);
111 struct lima_screen
*screen
= lima_screen(pctx
->screen
);
114 lima_submit_free(ctx
->pp_submit
);
116 lima_submit_free(ctx
->gp_submit
);
118 for (int i
= 0; i
< lima_ctx_buff_num
; i
++)
119 pipe_resource_reference(&ctx
->buffer_state
[i
].res
, NULL
);
121 lima_state_fini(ctx
);
124 util_blitter_destroy(ctx
->blitter
);
127 u_upload_destroy(ctx
->uploader
);
129 slab_destroy_child(&ctx
->transfer_pool
);
131 for (int i
= 0; i
< LIMA_CTX_PLB_MAX_NUM
; i
++) {
133 lima_bo_unreference(ctx
->plb
[i
]);
134 if (ctx
->gp_tile_heap
[i
])
135 lima_bo_unreference(ctx
->gp_tile_heap
[i
]);
138 if (ctx
->plb_gp_stream
)
139 lima_bo_unreference(ctx
->plb_gp_stream
);
142 lima_bo_unreference(ctx
->gp_output
);
144 if (ctx
->plb_pp_stream
)
145 assert(!_mesa_hash_table_num_entries(ctx
->plb_pp_stream
));
147 lima_context_free_drm_ctx(screen
, ctx
->id
);
153 plb_pp_stream_hash(const void *key
)
155 return _mesa_hash_data(key
, sizeof(struct lima_ctx_plb_pp_stream_key
));
159 plb_pp_stream_compare(const void *key1
, const void *key2
)
161 return memcmp(key1
, key2
, sizeof(struct lima_ctx_plb_pp_stream_key
)) == 0;
165 lima_set_debug_callback(struct pipe_context
*pctx
,
166 const struct pipe_debug_callback
*cb
)
168 struct lima_context
*ctx
= lima_context(pctx
);
173 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
176 struct pipe_context
*
177 lima_context_create(struct pipe_screen
*pscreen
, void *priv
, unsigned flags
)
179 struct lima_screen
*screen
= lima_screen(pscreen
);
180 struct lima_context
*ctx
;
182 ctx
= rzalloc(screen
, struct lima_context
);
186 ctx
->id
= lima_context_create_drm_ctx(screen
);
192 ctx
->base
.screen
= pscreen
;
193 ctx
->base
.destroy
= lima_context_destroy
;
194 ctx
->base
.set_debug_callback
= lima_set_debug_callback
;
196 lima_resource_context_init(ctx
);
197 lima_fence_context_init(ctx
);
198 lima_state_init(ctx
);
200 lima_program_init(ctx
);
201 lima_query_init(ctx
);
203 slab_create_child(&ctx
->transfer_pool
, &screen
->transfer_pool
);
205 ctx
->blitter
= util_blitter_create(&ctx
->base
);
209 ctx
->uploader
= u_upload_create_default(&ctx
->base
);
212 ctx
->base
.stream_uploader
= ctx
->uploader
;
213 ctx
->base
.const_uploader
= ctx
->uploader
;
215 util_dynarray_init(&ctx
->vs_cmd_array
, ctx
);
216 util_dynarray_init(&ctx
->plbu_cmd_array
, ctx
);
218 ctx
->plb_size
= screen
->plb_max_blk
* LIMA_CTX_PLB_BLK_SIZE
;
219 ctx
->plb_gp_size
= screen
->plb_max_blk
* 4;
221 for (int i
= 0; i
< lima_ctx_num_plb
; i
++) {
222 ctx
->plb
[i
] = lima_bo_create(screen
, ctx
->plb_size
, 0);
225 ctx
->gp_tile_heap
[i
] = lima_bo_create(screen
, gp_tile_heap_size
, 0);
226 if (!ctx
->gp_tile_heap
[i
])
230 unsigned plb_gp_stream_size
=
231 align(ctx
->plb_gp_size
* lima_ctx_num_plb
, LIMA_PAGE_SIZE
);
233 lima_bo_create(screen
, plb_gp_stream_size
, 0);
234 if (!ctx
->plb_gp_stream
)
236 lima_bo_map(ctx
->plb_gp_stream
);
238 /* plb gp stream is static for any framebuffer */
239 for (int i
= 0; i
< lima_ctx_num_plb
; i
++) {
240 uint32_t *plb_gp_stream
= ctx
->plb_gp_stream
->map
+ i
* ctx
->plb_gp_size
;
241 for (int j
= 0; j
< screen
->plb_max_blk
; j
++)
242 plb_gp_stream
[j
] = ctx
->plb
[i
]->va
+ LIMA_CTX_PLB_BLK_SIZE
* j
;
245 if (screen
->gpu_type
== DRM_LIMA_PARAM_GPU_ID_MALI400
) {
246 ctx
->plb_pp_stream
= _mesa_hash_table_create(
247 ctx
, plb_pp_stream_hash
, plb_pp_stream_compare
);
248 if (!ctx
->plb_pp_stream
)
252 ctx
->gp_submit
= lima_submit_create(ctx
, LIMA_PIPE_GP
);
256 ctx
->pp_submit
= lima_submit_create(ctx
, LIMA_PIPE_PP
);
263 lima_context_destroy(&ctx
->base
);
268 lima_need_flush(struct lima_context
*ctx
, struct lima_bo
*bo
, bool write
)
270 return lima_submit_has_bo(ctx
->gp_submit
, bo
, write
) ||
271 lima_submit_has_bo(ctx
->pp_submit
, bo
, write
);
275 lima_is_scanout(struct lima_context
*ctx
)
277 /* If there is no color buffer, it's an FBO */
278 if (!ctx
->framebuffer
.base
.nr_cbufs
)
281 return ctx
->framebuffer
.base
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
282 ctx
->framebuffer
.base
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
283 ctx
->framebuffer
.base
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;