1/*
2 * Copyright © 2017 Intel Corporation
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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 AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#ifndef CROCUS_SCREEN_H
24#define CROCUS_SCREEN_H
25
26#include "pipe/p_screen.h"
27#include "pipe/p_state.h"
28#include "frontend/drm_driver.h"
29#include "util/disk_cache.h"
30#include "util/slab.h"
31#include "util/u_screen.h"
32#include "intel/dev/intel_device_info.h"
33#include "intel/isl/isl.h"
34#include "crocus_bufmgr.h"
35#include "compiler/shader_enums.h"
36
37struct crocus_monitor_config;
38struct crocus_resource;
39struct crocus_context;
40struct crocus_sampler_state;
41struct brw_vue_map;
42struct brw_tcs_prog_key;
43struct brw_tes_prog_key;
44struct brw_cs_prog_key;
45struct brw_wm_prog_key;
46struct brw_vs_prog_key;
47struct brw_gs_prog_key;
48struct shader_info;
49
50#define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
51#define WRITE_ONCE(x, v) *(volatile __typeof__(x) *)&(x) = (v)
52
53#define CROCUS_MAX_TEXTURE_SAMPLERS 32
54#define CROCUS_MAX_SOL_BUFFERS 4
55#define CROCUS_MAP_BUFFER_ALIGNMENT 64
56
57
58/**
59 * Virtual table for generation-specific (genxml) function calls.
60 */
61struct crocus_vtable {
62   void (*destroy_state)(struct crocus_context *ice);
63   void (*init_render_context)(struct crocus_batch *batch);
64   void (*init_compute_context)(struct crocus_batch *batch);
65   void (*upload_render_state)(struct crocus_context *ice,
66                               struct crocus_batch *batch,
67                               const struct pipe_draw_info *draw,
68                               unsigned drawid_offset,
69                               const struct pipe_draw_indirect_info *indirect,
70                               const struct pipe_draw_start_count_bias *sc);
71   void (*update_surface_base_address)(struct crocus_batch *batch);
72
73   void (*upload_compute_state)(struct crocus_context *ice,
74                                struct crocus_batch *batch,
75                                const struct pipe_grid_info *grid);
76   void (*rebind_buffer)(struct crocus_context *ice,
77                         struct crocus_resource *res);
78   void (*resolve_conditional_render)(struct crocus_context *ice);
79   void (*emit_compute_predicate)(struct crocus_batch *batch);
80   void (*load_register_reg32)(struct crocus_batch *batch, uint32_t dst,
81                               uint32_t src);
82   void (*load_register_reg64)(struct crocus_batch *batch, uint32_t dst,
83                               uint32_t src);
84   void (*load_register_imm32)(struct crocus_batch *batch, uint32_t reg,
85                               uint32_t val);
86   void (*load_register_imm64)(struct crocus_batch *batch, uint32_t reg,
87                               uint64_t val);
88   void (*load_register_mem32)(struct crocus_batch *batch, uint32_t reg,
89                               struct crocus_bo *bo, uint32_t offset);
90   void (*load_register_mem64)(struct crocus_batch *batch, uint32_t reg,
91                               struct crocus_bo *bo, uint32_t offset);
92   void (*store_register_mem32)(struct crocus_batch *batch, uint32_t reg,
93                                struct crocus_bo *bo, uint32_t offset,
94                                bool predicated);
95   void (*store_register_mem64)(struct crocus_batch *batch, uint32_t reg,
96                                struct crocus_bo *bo, uint32_t offset,
97                                bool predicated);
98   void (*store_data_imm32)(struct crocus_batch *batch,
99                            struct crocus_bo *bo, uint32_t offset,
100                            uint32_t value);
101   void (*store_data_imm64)(struct crocus_batch *batch,
102                            struct crocus_bo *bo, uint32_t offset,
103                            uint64_t value);
104   void (*copy_mem_mem)(struct crocus_batch *batch,
105                        struct crocus_bo *dst_bo, uint32_t dst_offset,
106                        struct crocus_bo *src_bo, uint32_t src_offset,
107                        unsigned bytes);
108   void (*emit_raw_pipe_control)(struct crocus_batch *batch,
109                                 const char *reason, uint32_t flags,
110                                 struct crocus_bo *bo, uint32_t offset,
111                                 uint64_t imm);
112
113   void (*emit_mi_report_perf_count)(struct crocus_batch *batch,
114                                     struct crocus_bo *bo,
115                                     uint32_t offset_in_bytes,
116                                     uint32_t report_id);
117
118   uint32_t *(*create_so_decl_list)(const struct pipe_stream_output_info *sol,
119                                    const struct brw_vue_map *vue_map);
120   void (*populate_vs_key)(const struct crocus_context *ice,
121                           const struct shader_info *info,
122                           gl_shader_stage last_stage,
123                           struct brw_vs_prog_key *key);
124   void (*populate_tcs_key)(const struct crocus_context *ice,
125                            struct brw_tcs_prog_key *key);
126   void (*populate_tes_key)(const struct crocus_context *ice,
127                            const struct shader_info *info,
128                            gl_shader_stage last_stage,
129                            struct brw_tes_prog_key *key);
130   void (*populate_gs_key)(const struct crocus_context *ice,
131                           const struct shader_info *info,
132                           gl_shader_stage last_stage,
133                           struct brw_gs_prog_key *key);
134   void (*populate_fs_key)(const struct crocus_context *ice,
135                           const struct shader_info *info,
136                           struct brw_wm_prog_key *key);
137   void (*populate_cs_key)(const struct crocus_context *ice,
138                           struct brw_cs_prog_key *key);
139   void (*fill_clamp_mask)(const struct crocus_sampler_state *state,
140                           int s,
141                           uint32_t *clamp_mask);
142   void (*lost_genx_state)(struct crocus_context *ice, struct crocus_batch *batch);
143
144   void (*finish_batch)(struct crocus_batch *batch); /* haswell only */
145
146   void (*upload_urb_fence)(struct crocus_batch *batch); /* gen4/5 only */
147
148   bool (*blit_blt)(struct crocus_batch *batch,
149                    const struct pipe_blit_info *info);
150   bool (*copy_region_blt)(struct crocus_batch *batch,
151                           struct crocus_resource *dst,
152                           unsigned dst_level,
153                           unsigned dstx, unsigned dsty, unsigned dstz,
154                           struct crocus_resource *src,
155                           unsigned src_level,
156                           const struct pipe_box *src_box);
157   bool (*calculate_urb_fence)(struct crocus_batch *batch, unsigned csize,
158                               unsigned vsize, unsigned sfsize);
159   void (*batch_reset_dirty)(struct crocus_batch *batch);
160   unsigned (*translate_prim_type)(enum pipe_prim_type prim, uint8_t verts_per_patch);
161
162   void (*update_so_strides)(struct crocus_context *ice,
163                             uint16_t *strides);
164
165   uint32_t (*get_so_offset)(struct pipe_stream_output_target *tgt);
166};
167
168struct crocus_screen {
169   struct pipe_screen base;
170
171   uint32_t refcount;
172
173   /** Global slab allocator for crocus_transfer_map objects */
174   struct slab_parent_pool transfer_pool;
175
176   /** drm device file descriptor, shared with bufmgr, do not close. */
177   int fd;
178
179   /**
180    * drm device file descriptor to used for window system integration, owned
181    * by iris_screen, can be a different DRM instance than fd.
182    */
183   int winsys_fd;
184
185   /** PCI ID for our GPU device */
186   int pci_id;
187
188   struct crocus_vtable vtbl;
189
190   /** Global program_string_id counter (see get_program_string_id()) */
191   unsigned program_id;
192
193   /** Precompile shaders at link time?  (Can be disabled for debugging.) */
194   bool precompile;
195
196   /** driconf options and application workarounds */
197   struct {
198      /** Dual color blend by location instead of index (for broken apps) */
199      bool dual_color_blend_by_location;
200      bool disable_throttling;
201      bool always_flush_cache;
202      bool limit_trig_input_range;
203   } driconf;
204
205   uint64_t aperture_bytes;
206   uint64_t aperture_threshold;
207
208   struct intel_device_info devinfo;
209   struct isl_device isl_dev;
210   struct crocus_bufmgr *bufmgr;
211   struct brw_compiler *compiler;
212   struct crocus_monitor_config *monitor_cfg;
213
214   const struct intel_l3_config *l3_config_3d;
215   const struct intel_l3_config *l3_config_cs;
216
217   struct disk_cache *disk_cache;
218};
219
220struct pipe_screen *
221crocus_screen_create(int fd, const struct pipe_screen_config *config);
222
223void crocus_screen_destroy(struct crocus_screen *screen);
224
225UNUSED static inline struct pipe_screen *
226crocus_pscreen_ref(struct pipe_screen *pscreen)
227{
228   struct crocus_screen *screen = (struct crocus_screen *) pscreen;
229
230   p_atomic_inc(&screen->refcount);
231   return pscreen;
232}
233
234UNUSED static inline void
235crocus_pscreen_unref(struct pipe_screen *pscreen)
236{
237   struct crocus_screen *screen = (struct crocus_screen *) pscreen;
238
239   if (p_atomic_dec_zero(&screen->refcount))
240      crocus_screen_destroy(screen);
241}
242
243bool
244crocus_is_format_supported(struct pipe_screen *pscreen,
245                           enum pipe_format format,
246                           enum pipe_texture_target target,
247                           unsigned sample_count,
248                           unsigned storage_sample_count,
249                           unsigned usage);
250
251void crocus_disk_cache_init(struct crocus_screen *screen);
252
253#endif
254