1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2017 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice shall be included
12b8e80941Smrg * in all copies or substantial portions of the Software.
13b8e80941Smrg *
14b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15b8e80941Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20b8e80941Smrg * DEALINGS IN THE SOFTWARE.
21b8e80941Smrg */
22b8e80941Smrg
23b8e80941Smrg#include <stdio.h>
24b8e80941Smrg#include <time.h>
25b8e80941Smrg#include "pipe/p_defines.h"
26b8e80941Smrg#include "pipe/p_state.h"
27b8e80941Smrg#include "util/ralloc.h"
28b8e80941Smrg#include "util/u_inlines.h"
29b8e80941Smrg#include "util/u_format.h"
30b8e80941Smrg#include "util/u_upload_mgr.h"
31b8e80941Smrg#include "drm-uapi/i915_drm.h"
32b8e80941Smrg#include "iris_context.h"
33b8e80941Smrg#include "iris_resource.h"
34b8e80941Smrg#include "iris_screen.h"
35b8e80941Smrg#include "common/gen_defines.h"
36b8e80941Smrg#include "common/gen_sample_positions.h"
37b8e80941Smrg
38b8e80941Smrg/**
39b8e80941Smrg * For debugging purposes, this returns a time in seconds.
40b8e80941Smrg */
41b8e80941Smrgdouble
42b8e80941Smrgget_time(void)
43b8e80941Smrg{
44b8e80941Smrg   struct timespec tp;
45b8e80941Smrg
46b8e80941Smrg   clock_gettime(CLOCK_MONOTONIC, &tp);
47b8e80941Smrg
48b8e80941Smrg   return tp.tv_sec + tp.tv_nsec / 1000000000.0;
49b8e80941Smrg}
50b8e80941Smrg
51b8e80941Smrg/**
52b8e80941Smrg * The pipe->set_debug_callback() driver hook.
53b8e80941Smrg */
54b8e80941Smrgstatic void
55b8e80941Smrgiris_set_debug_callback(struct pipe_context *ctx,
56b8e80941Smrg                        const struct pipe_debug_callback *cb)
57b8e80941Smrg{
58b8e80941Smrg   struct iris_context *ice = (struct iris_context *)ctx;
59b8e80941Smrg
60b8e80941Smrg   if (cb)
61b8e80941Smrg      ice->dbg = *cb;
62b8e80941Smrg   else
63b8e80941Smrg      memset(&ice->dbg, 0, sizeof(ice->dbg));
64b8e80941Smrg}
65b8e80941Smrg
66b8e80941Smrgstatic void
67b8e80941Smrgiris_get_sample_position(struct pipe_context *ctx,
68b8e80941Smrg                         unsigned sample_count,
69b8e80941Smrg                         unsigned sample_index,
70b8e80941Smrg                         float *out_value)
71b8e80941Smrg{
72b8e80941Smrg   union {
73b8e80941Smrg      struct {
74b8e80941Smrg         float x[16];
75b8e80941Smrg         float y[16];
76b8e80941Smrg      } a;
77b8e80941Smrg      struct {
78b8e80941Smrg         float  _0XOffset,  _1XOffset,  _2XOffset,  _3XOffset,
79b8e80941Smrg                _4XOffset,  _5XOffset,  _6XOffset,  _7XOffset,
80b8e80941Smrg                _8XOffset,  _9XOffset, _10XOffset, _11XOffset,
81b8e80941Smrg               _12XOffset, _13XOffset, _14XOffset, _15XOffset;
82b8e80941Smrg         float  _0YOffset,  _1YOffset,  _2YOffset,  _3YOffset,
83b8e80941Smrg                _4YOffset,  _5YOffset,  _6YOffset,  _7YOffset,
84b8e80941Smrg                _8YOffset,  _9YOffset, _10YOffset, _11YOffset,
85b8e80941Smrg               _12YOffset, _13YOffset, _14YOffset, _15YOffset;
86b8e80941Smrg      } v;
87b8e80941Smrg   } u;
88b8e80941Smrg   switch (sample_count) {
89b8e80941Smrg   case 1:  GEN_SAMPLE_POS_1X(u.v._);  break;
90b8e80941Smrg   case 2:  GEN_SAMPLE_POS_2X(u.v._);  break;
91b8e80941Smrg   case 4:  GEN_SAMPLE_POS_4X(u.v._);  break;
92b8e80941Smrg   case 8:  GEN_SAMPLE_POS_8X(u.v._);  break;
93b8e80941Smrg   case 16: GEN_SAMPLE_POS_16X(u.v._); break;
94b8e80941Smrg   default: unreachable("invalid sample count");
95b8e80941Smrg   }
96b8e80941Smrg
97b8e80941Smrg   out_value[0] = u.a.x[sample_index];
98b8e80941Smrg   out_value[1] = u.a.y[sample_index];
99b8e80941Smrg}
100b8e80941Smrg
101b8e80941Smrg/**
102b8e80941Smrg * Destroy a context, freeing any associated memory.
103b8e80941Smrg */
104b8e80941Smrgstatic void
105b8e80941Smrgiris_destroy_context(struct pipe_context *ctx)
106b8e80941Smrg{
107b8e80941Smrg   struct iris_context *ice = (struct iris_context *)ctx;
108b8e80941Smrg
109b8e80941Smrg   if (ctx->stream_uploader)
110b8e80941Smrg      u_upload_destroy(ctx->stream_uploader);
111b8e80941Smrg
112b8e80941Smrg   ice->vtbl.destroy_state(ice);
113b8e80941Smrg   iris_destroy_program_cache(ice);
114b8e80941Smrg   iris_destroy_border_color_pool(ice);
115b8e80941Smrg   u_upload_destroy(ice->state.surface_uploader);
116b8e80941Smrg   u_upload_destroy(ice->state.dynamic_uploader);
117b8e80941Smrg   u_upload_destroy(ice->query_buffer_uploader);
118b8e80941Smrg
119b8e80941Smrg   slab_destroy_child(&ice->transfer_pool);
120b8e80941Smrg
121b8e80941Smrg   iris_batch_free(&ice->batches[IRIS_BATCH_RENDER]);
122b8e80941Smrg   iris_batch_free(&ice->batches[IRIS_BATCH_COMPUTE]);
123b8e80941Smrg   iris_destroy_binder(&ice->state.binder);
124b8e80941Smrg
125b8e80941Smrg   ralloc_free(ice);
126b8e80941Smrg}
127b8e80941Smrg
128b8e80941Smrg#define genX_call(devinfo, func, ...)             \
129b8e80941Smrg   switch (devinfo->gen) {                        \
130b8e80941Smrg   case 11:                                       \
131b8e80941Smrg      gen11_##func(__VA_ARGS__);                  \
132b8e80941Smrg      break;                                      \
133b8e80941Smrg   case 10:                                       \
134b8e80941Smrg      gen10_##func(__VA_ARGS__);                  \
135b8e80941Smrg      break;                                      \
136b8e80941Smrg   case 9:                                        \
137b8e80941Smrg      gen9_##func(__VA_ARGS__);                   \
138b8e80941Smrg      break;                                      \
139b8e80941Smrg   case 8:                                        \
140b8e80941Smrg      gen8_##func(__VA_ARGS__);                   \
141b8e80941Smrg      break;                                      \
142b8e80941Smrg   default:                                       \
143b8e80941Smrg      unreachable("Unknown hardware generation"); \
144b8e80941Smrg   }
145b8e80941Smrg
146b8e80941Smrg/**
147b8e80941Smrg * Create a context.
148b8e80941Smrg *
149b8e80941Smrg * This is where each context begins.
150b8e80941Smrg */
151b8e80941Smrgstruct pipe_context *
152b8e80941Smrgiris_create_context(struct pipe_screen *pscreen, void *priv, unsigned flags)
153b8e80941Smrg{
154b8e80941Smrg   struct iris_screen *screen = (struct iris_screen*)pscreen;
155b8e80941Smrg   const struct gen_device_info *devinfo = &screen->devinfo;
156b8e80941Smrg   struct iris_context *ice = rzalloc(NULL, struct iris_context);
157b8e80941Smrg
158b8e80941Smrg   if (!ice)
159b8e80941Smrg      return NULL;
160b8e80941Smrg
161b8e80941Smrg   struct pipe_context *ctx = &ice->ctx;
162b8e80941Smrg
163b8e80941Smrg   ctx->screen = pscreen;
164b8e80941Smrg   ctx->priv = priv;
165b8e80941Smrg
166b8e80941Smrg   ctx->stream_uploader = u_upload_create_default(ctx);
167b8e80941Smrg   if (!ctx->stream_uploader) {
168b8e80941Smrg      free(ctx);
169b8e80941Smrg      return NULL;
170b8e80941Smrg   }
171b8e80941Smrg   ctx->const_uploader = ctx->stream_uploader;
172b8e80941Smrg
173b8e80941Smrg   ctx->destroy = iris_destroy_context;
174b8e80941Smrg   ctx->set_debug_callback = iris_set_debug_callback;
175b8e80941Smrg   ctx->get_sample_position = iris_get_sample_position;
176b8e80941Smrg
177b8e80941Smrg   ice->shaders.urb_size = devinfo->urb.size;
178b8e80941Smrg
179b8e80941Smrg   iris_init_context_fence_functions(ctx);
180b8e80941Smrg   iris_init_blit_functions(ctx);
181b8e80941Smrg   iris_init_clear_functions(ctx);
182b8e80941Smrg   iris_init_program_functions(ctx);
183b8e80941Smrg   iris_init_resource_functions(ctx);
184b8e80941Smrg   iris_init_query_functions(ctx);
185b8e80941Smrg   iris_init_flush_functions(ctx);
186b8e80941Smrg
187b8e80941Smrg   iris_init_program_cache(ice);
188b8e80941Smrg   iris_init_border_color_pool(ice);
189b8e80941Smrg   iris_init_binder(ice);
190b8e80941Smrg
191b8e80941Smrg   slab_create_child(&ice->transfer_pool, &screen->transfer_pool);
192b8e80941Smrg
193b8e80941Smrg   ice->state.surface_uploader =
194b8e80941Smrg      u_upload_create(ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
195b8e80941Smrg                      IRIS_RESOURCE_FLAG_SURFACE_MEMZONE);
196b8e80941Smrg   ice->state.dynamic_uploader =
197b8e80941Smrg      u_upload_create(ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
198b8e80941Smrg                      IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE);
199b8e80941Smrg
200b8e80941Smrg   ice->query_buffer_uploader =
201b8e80941Smrg      u_upload_create(ctx, 4096, PIPE_BIND_CUSTOM, PIPE_USAGE_STAGING,
202b8e80941Smrg                      0);
203b8e80941Smrg
204b8e80941Smrg   genX_call(devinfo, init_state, ice);
205b8e80941Smrg   genX_call(devinfo, init_blorp, ice);
206b8e80941Smrg
207b8e80941Smrg   int priority = 0;
208b8e80941Smrg   if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
209b8e80941Smrg      priority = GEN_CONTEXT_HIGH_PRIORITY;
210b8e80941Smrg   if (flags & PIPE_CONTEXT_LOW_PRIORITY)
211b8e80941Smrg      priority = GEN_CONTEXT_LOW_PRIORITY;
212b8e80941Smrg
213b8e80941Smrg   for (int i = 0; i < IRIS_BATCH_COUNT; i++) {
214b8e80941Smrg      iris_init_batch(&ice->batches[i], screen, &ice->vtbl, &ice->dbg,
215b8e80941Smrg                      ice->batches, (enum iris_batch_name) i,
216b8e80941Smrg                      I915_EXEC_RENDER, priority);
217b8e80941Smrg   }
218b8e80941Smrg
219b8e80941Smrg   ice->vtbl.init_render_context(screen, &ice->batches[IRIS_BATCH_RENDER],
220b8e80941Smrg                                 &ice->vtbl, &ice->dbg);
221b8e80941Smrg   ice->vtbl.init_compute_context(screen, &ice->batches[IRIS_BATCH_COMPUTE],
222b8e80941Smrg                                  &ice->vtbl, &ice->dbg);
223b8e80941Smrg
224b8e80941Smrg   return ctx;
225b8e80941Smrg}
226