lima: put hardware related info to lima_gpu.h
[mesa.git] / src / gallium / drivers / lima / lima_context.c
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_submit.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
48 lima_ctx_buff_va(struct lima_context *ctx, enum lima_ctx_buff buff)
49 {
50 struct lima_ctx_buff_state *cbs = ctx->buffer_state + buff;
51 struct lima_resource *res = lima_resource(cbs->res);
52 int pipe = buff < lima_ctx_buff_num_gp ? LIMA_PIPE_GP : LIMA_PIPE_PP;
53
54 lima_submit_add_bo(ctx->submit, pipe, res->bo, LIMA_SUBMIT_BO_READ);
55
56 return res->bo->va + cbs->offset;
57 }
58
59 void *
60 lima_ctx_buff_map(struct lima_context *ctx, enum lima_ctx_buff buff)
61 {
62 struct lima_ctx_buff_state *cbs = ctx->buffer_state + buff;
63 struct lima_resource *res = lima_resource(cbs->res);
64
65 return lima_bo_map(res->bo) + cbs->offset;
66 }
67
68 void *
69 lima_ctx_buff_alloc(struct lima_context *ctx, enum lima_ctx_buff buff,
70 unsigned size)
71 {
72 struct lima_ctx_buff_state *cbs = ctx->buffer_state + buff;
73 void *ret = NULL;
74
75 cbs->size = align(size, 0x40);
76
77 u_upload_alloc(ctx->uploader, 0, cbs->size, 0x40, &cbs->offset,
78 &cbs->res, &ret);
79
80 return ret;
81 }
82
83 static int
84 lima_context_create_drm_ctx(struct lima_screen *screen)
85 {
86 struct drm_lima_ctx_create req = {0};
87
88 int ret = drmIoctl(screen->fd, DRM_IOCTL_LIMA_CTX_CREATE, &req);
89 if (ret)
90 return errno;
91
92 return req.id;
93 }
94
95 static void
96 lima_context_free_drm_ctx(struct lima_screen *screen, int id)
97 {
98 struct drm_lima_ctx_free req = {
99 .id = id,
100 };
101
102 drmIoctl(screen->fd, DRM_IOCTL_LIMA_CTX_FREE, &req);
103 }
104
105 static void
106 lima_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
107 {
108 struct lima_context *ctx = lima_context(pctx);
109
110 if (ctx->framebuffer.base.zsbuf && (ctx->framebuffer.base.zsbuf->texture == prsc))
111 ctx->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
112
113 if (ctx->framebuffer.base.nr_cbufs &&
114 (ctx->framebuffer.base.cbufs[0]->texture == prsc))
115 ctx->resolve &= ~PIPE_CLEAR_COLOR0;
116 }
117
118 static void
119 lima_context_destroy(struct pipe_context *pctx)
120 {
121 struct lima_context *ctx = lima_context(pctx);
122 struct lima_screen *screen = lima_screen(pctx->screen);
123
124 lima_submit_fini(ctx);
125
126 for (int i = 0; i < lima_ctx_buff_num; i++)
127 pipe_resource_reference(&ctx->buffer_state[i].res, NULL);
128
129 lima_state_fini(ctx);
130
131 if (ctx->blitter)
132 util_blitter_destroy(ctx->blitter);
133
134 if (ctx->uploader)
135 u_upload_destroy(ctx->uploader);
136
137 slab_destroy_child(&ctx->transfer_pool);
138
139 for (int i = 0; i < LIMA_CTX_PLB_MAX_NUM; i++) {
140 if (ctx->plb[i])
141 lima_bo_unreference(ctx->plb[i]);
142 if (ctx->gp_tile_heap[i])
143 lima_bo_unreference(ctx->gp_tile_heap[i]);
144 }
145
146 if (ctx->plb_gp_stream)
147 lima_bo_unreference(ctx->plb_gp_stream);
148
149 if (ctx->gp_output)
150 lima_bo_unreference(ctx->gp_output);
151
152 if (ctx->plb_pp_stream)
153 assert(!_mesa_hash_table_num_entries(ctx->plb_pp_stream));
154
155 lima_context_free_drm_ctx(screen, ctx->id);
156
157 ralloc_free(ctx);
158 }
159
160 static uint32_t
161 plb_pp_stream_hash(const void *key)
162 {
163 return _mesa_hash_data(key, sizeof(struct lima_ctx_plb_pp_stream_key));
164 }
165
166 static bool
167 plb_pp_stream_compare(const void *key1, const void *key2)
168 {
169 return memcmp(key1, key2, sizeof(struct lima_ctx_plb_pp_stream_key)) == 0;
170 }
171
172 static void
173 lima_set_debug_callback(struct pipe_context *pctx,
174 const struct pipe_debug_callback *cb)
175 {
176 struct lima_context *ctx = lima_context(pctx);
177
178 if (cb)
179 ctx->debug = *cb;
180 else
181 memset(&ctx->debug, 0, sizeof(ctx->debug));
182 }
183
184 struct pipe_context *
185 lima_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
186 {
187 struct lima_screen *screen = lima_screen(pscreen);
188 struct lima_context *ctx;
189
190 ctx = rzalloc(screen, struct lima_context);
191 if (!ctx)
192 return NULL;
193
194 ctx->id = lima_context_create_drm_ctx(screen);
195 if (ctx->id < 0) {
196 ralloc_free(ctx);
197 return NULL;
198 }
199
200 ctx->base.screen = pscreen;
201 ctx->base.destroy = lima_context_destroy;
202 ctx->base.set_debug_callback = lima_set_debug_callback;
203 ctx->base.invalidate_resource = lima_invalidate_resource;
204
205 lima_resource_context_init(ctx);
206 lima_fence_context_init(ctx);
207 lima_state_init(ctx);
208 lima_draw_init(ctx);
209 lima_program_init(ctx);
210 lima_query_init(ctx);
211
212 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
213
214 ctx->blitter = util_blitter_create(&ctx->base);
215 if (!ctx->blitter)
216 goto err_out;
217
218 ctx->uploader = u_upload_create_default(&ctx->base);
219 if (!ctx->uploader)
220 goto err_out;
221 ctx->base.stream_uploader = ctx->uploader;
222 ctx->base.const_uploader = ctx->uploader;
223
224 ctx->damage_rect.minx = ctx->damage_rect.miny = 0xffff;
225 ctx->damage_rect.maxx = ctx->damage_rect.maxy = 0;
226
227 util_dynarray_init(&ctx->vs_cmd_array, ctx);
228 util_dynarray_init(&ctx->plbu_cmd_array, ctx);
229 util_dynarray_init(&ctx->plbu_cmd_head, ctx);
230
231 ctx->plb_size = screen->plb_max_blk * LIMA_CTX_PLB_BLK_SIZE;
232 ctx->plb_gp_size = screen->plb_max_blk * 4;
233
234 uint32_t heap_flags;
235 if (screen->has_growable_heap_buffer) {
236 /* growable size buffer, initially will allocate 32K (by default)
237 * backup memory in kernel driver, and will allocate more when GP
238 * get out of memory interrupt. Max to 16M set here.
239 */
240 ctx->gp_tile_heap_size = 0x1000000;
241 heap_flags = LIMA_BO_FLAG_HEAP;
242 } else {
243 /* fix size buffer */
244 ctx->gp_tile_heap_size = 0x100000;
245 heap_flags = 0;
246 }
247
248 for (int i = 0; i < lima_ctx_num_plb; i++) {
249 ctx->plb[i] = lima_bo_create(screen, ctx->plb_size, 0);
250 if (!ctx->plb[i])
251 goto err_out;
252 ctx->gp_tile_heap[i] = lima_bo_create(screen, ctx->gp_tile_heap_size, heap_flags);
253 if (!ctx->gp_tile_heap[i])
254 goto err_out;
255 }
256
257 unsigned plb_gp_stream_size =
258 align(ctx->plb_gp_size * lima_ctx_num_plb, LIMA_PAGE_SIZE);
259 ctx->plb_gp_stream =
260 lima_bo_create(screen, plb_gp_stream_size, 0);
261 if (!ctx->plb_gp_stream)
262 goto err_out;
263 lima_bo_map(ctx->plb_gp_stream);
264
265 /* plb gp stream is static for any framebuffer */
266 for (int i = 0; i < lima_ctx_num_plb; i++) {
267 uint32_t *plb_gp_stream = ctx->plb_gp_stream->map + i * ctx->plb_gp_size;
268 for (int j = 0; j < screen->plb_max_blk; j++)
269 plb_gp_stream[j] = ctx->plb[i]->va + LIMA_CTX_PLB_BLK_SIZE * j;
270 }
271
272 if (screen->gpu_type == DRM_LIMA_PARAM_GPU_ID_MALI400) {
273 ctx->plb_pp_stream = _mesa_hash_table_create(
274 ctx, plb_pp_stream_hash, plb_pp_stream_compare);
275 if (!ctx->plb_pp_stream)
276 goto err_out;
277 }
278
279 if (!lima_submit_init(ctx))
280 goto err_out;
281
282 return &ctx->base;
283
284 err_out:
285 lima_context_destroy(&ctx->base);
286 return NULL;
287 }
288
289 bool
290 lima_need_flush(struct lima_context *ctx, struct lima_bo *bo, bool write)
291 {
292 return lima_submit_has_bo(ctx->submit, bo, write);
293 }