1 /*
2  * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3  * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 
24 #ifndef RADEON_WINSYS_H
25 #define RADEON_WINSYS_H
26 
27 /* The public winsys interface header for the radeon driver. */
28 
29 /* R300 features in DRM.
30  *
31  * 2.6.0:
32  * - Hyper-Z
33  * - GB_Z_PEQ_CONFIG on rv350->r4xx
34  * - R500 FG_ALPHA_VALUE
35  *
36  * 2.8.0:
37  * - R500 US_FORMAT regs
38  * - R500 ARGB2101010 colorbuffer
39  * - CMask and AA regs
40  * - R16F/RG16F
41  */
42 
43 #include "pipebuffer/pb_buffer.h"
44 #include "libdrm/radeon_surface.h"
45 
46 #define RADEON_MAX_CMDBUF_DWORDS (16 * 1024)
47 
48 #define RADEON_FLUSH_ASYNC		(1 << 0)
49 #define RADEON_FLUSH_KEEP_TILING_FLAGS	(1 << 1) /* needs DRM 2.12.0 */
50 #define RADEON_FLUSH_COMPUTE		(1 << 2)
51 
52 /* Tiling flags. */
53 enum radeon_bo_layout {
54     RADEON_LAYOUT_LINEAR = 0,
55     RADEON_LAYOUT_TILED,
56     RADEON_LAYOUT_SQUARETILED,
57 
58     RADEON_LAYOUT_UNKNOWN
59 };
60 
61 enum radeon_bo_domain { /* bitfield */
62     RADEON_DOMAIN_GTT  = 2,
63     RADEON_DOMAIN_VRAM = 4
64 };
65 
66 enum radeon_bo_usage { /* bitfield */
67     RADEON_USAGE_READ = 2,
68     RADEON_USAGE_WRITE = 4,
69     RADEON_USAGE_READWRITE = RADEON_USAGE_READ | RADEON_USAGE_WRITE
70 };
71 
72 struct winsys_handle;
73 struct radeon_winsys_cs_handle;
74 
75 struct radeon_winsys_cs {
76     unsigned cdw;  /* Number of used dwords. */
77     uint32_t *buf; /* The command buffer. */
78 };
79 
80 struct radeon_info {
81     uint32_t pci_id;
82     uint32_t gart_size;
83     uint32_t vram_size;
84 
85     uint32_t drm_major; /* version */
86     uint32_t drm_minor;
87     uint32_t drm_patchlevel;
88 
89     uint32_t r300_num_gb_pipes;
90     uint32_t r300_num_z_pipes;
91 
92     uint32_t r600_num_backends;
93     uint32_t r600_clock_crystal_freq;
94     uint32_t r600_tiling_config;
95     uint32_t r600_num_tile_pipes;
96     uint32_t r600_backend_map;
97     boolean r600_backend_map_valid;
98     boolean r600_virtual_address;
99     uint32_t r600_va_start;
100     uint32_t r600_ib_vm_max_size;
101     uint32_t r600_max_pipes;
102 };
103 
104 enum radeon_feature_id {
105     RADEON_FID_R300_HYPERZ_ACCESS,     /* ZMask + HiZ */
106     RADEON_FID_R300_CMASK_ACCESS,
107 };
108 
109 struct radeon_winsys {
110     /**
111      * Destroy this winsys.
112      *
113      * \param ws        The winsys this function is called from.
114      */
115     void (*destroy)(struct radeon_winsys *ws);
116 
117     /**
118      * Query an info structure from winsys.
119      *
120      * \param ws        The winsys this function is called from.
121      * \param info      Return structure
122      */
123     void (*query_info)(struct radeon_winsys *ws,
124                        struct radeon_info *info);
125 
126     /**************************************************************************
127      * Buffer management. Buffer attributes are mostly fixed over its lifetime.
128      *
129      * Remember that gallium gets to choose the interface it needs, and the
130      * window systems must then implement that interface (rather than the
131      * other way around...).
132      *************************************************************************/
133 
134     /**
135      * Create a buffer object.
136      *
137      * \param ws        The winsys this function is called from.
138      * \param size      The size to allocate.
139      * \param alignment An alignment of the buffer in memory.
140      * \param bind      A bitmask of the PIPE_BIND_* flags.
141      * \param domain    A bitmask of the RADEON_DOMAIN_* flags.
142      * \return          The created buffer object.
143      */
144     struct pb_buffer *(*buffer_create)(struct radeon_winsys *ws,
145                                        unsigned size,
146                                        unsigned alignment,
147                                        unsigned bind,
148                                        enum radeon_bo_domain domain);
149 
150     struct radeon_winsys_cs_handle *(*buffer_get_cs_handle)(
151             struct pb_buffer *buf);
152 
153     /**
154      * Map the entire data store of a buffer object into the client's address
155      * space.
156      *
157      * \param buf       A winsys buffer object to map.
158      * \param cs        A command stream to flush if the buffer is referenced by it.
159      * \param usage     A bitmask of the PIPE_TRANSFER_* flags.
160      * \return          The pointer at the beginning of the buffer.
161      */
162     void *(*buffer_map)(struct radeon_winsys_cs_handle *buf,
163                         struct radeon_winsys_cs *cs,
164                         enum pipe_transfer_usage usage);
165 
166     /**
167      * Unmap a buffer object from the client's address space.
168      *
169      * \param buf       A winsys buffer object to unmap.
170      */
171     void (*buffer_unmap)(struct radeon_winsys_cs_handle *buf);
172 
173     /**
174      * Return TRUE if a buffer object is being used by the GPU.
175      *
176      * \param buf       A winsys buffer object.
177      * \param usage     Only check whether the buffer is busy for the given usage.
178      */
179     boolean (*buffer_is_busy)(struct pb_buffer *buf,
180                               enum radeon_bo_usage usage);
181 
182     /**
183      * Wait for a buffer object until it is not used by a GPU. This is
184      * equivalent to a fence placed after the last command using the buffer,
185      * and synchronizing to the fence.
186      *
187      * \param buf       A winsys buffer object to wait for.
188      * \param usage     Only wait until the buffer is idle for the given usage,
189      *                  but may still be busy for some other usage.
190      */
191     void (*buffer_wait)(struct pb_buffer *buf, enum radeon_bo_usage usage);
192 
193     /**
194      * Return tiling flags describing a memory layout of a buffer object.
195      *
196      * \param buf       A winsys buffer object to get the flags from.
197      * \param macrotile A pointer to the return value of the microtile flag.
198      * \param microtile A pointer to the return value of the macrotile flag.
199      *
200      * \note microtile and macrotile are not bitmasks!
201      */
202     void (*buffer_get_tiling)(struct pb_buffer *buf,
203                               enum radeon_bo_layout *microtile,
204                               enum radeon_bo_layout *macrotile,
205                               unsigned *bankw, unsigned *bankh,
206                               unsigned *tile_split,
207                               unsigned *stencil_tile_split,
208                               unsigned *mtilea);
209 
210     /**
211      * Set tiling flags describing a memory layout of a buffer object.
212      *
213      * \param buf       A winsys buffer object to set the flags for.
214      * \param cs        A command stream to flush if the buffer is referenced by it.
215      * \param macrotile A macrotile flag.
216      * \param microtile A microtile flag.
217      * \param stride    A stride of the buffer in bytes, for texturing.
218      *
219      * \note microtile and macrotile are not bitmasks!
220      */
221     void (*buffer_set_tiling)(struct pb_buffer *buf,
222                               struct radeon_winsys_cs *rcs,
223                               enum radeon_bo_layout microtile,
224                               enum radeon_bo_layout macrotile,
225                               unsigned bankw, unsigned bankh,
226                               unsigned tile_split,
227                               unsigned stencil_tile_split,
228                               unsigned mtilea,
229                               unsigned stride);
230 
231     /**
232      * Get a winsys buffer from a winsys handle. The internal structure
233      * of the handle is platform-specific and only a winsys should access it.
234      *
235      * \param ws        The winsys this function is called from.
236      * \param whandle   A winsys handle pointer as was received from a state
237      *                  tracker.
238      * \param stride    The returned buffer stride in bytes.
239      */
240     struct pb_buffer *(*buffer_from_handle)(struct radeon_winsys *ws,
241                                             struct winsys_handle *whandle,
242                                             unsigned *stride);
243 
244     /**
245      * Get a winsys handle from a winsys buffer. The internal structure
246      * of the handle is platform-specific and only a winsys should access it.
247      *
248      * \param buf       A winsys buffer object to get the handle from.
249      * \param whandle   A winsys handle pointer.
250      * \param stride    A stride of the buffer in bytes, for texturing.
251      * \return          TRUE on success.
252      */
253     boolean (*buffer_get_handle)(struct pb_buffer *buf,
254                                  unsigned stride,
255                                  struct winsys_handle *whandle);
256 
257     /**
258      * Return the virtual address of a buffer.
259      *
260      * \param buf       A winsys buffer object
261      * \return          virtual address
262      */
263     uint64_t (*buffer_get_virtual_address)(struct radeon_winsys_cs_handle *buf);
264 
265     /**************************************************************************
266      * Command submission.
267      *
268      * Each pipe context should create its own command stream and submit
269      * commands independently of other contexts.
270      *************************************************************************/
271 
272     /**
273      * Create a command stream.
274      *
275      * \param ws        The winsys this function is called from.
276      */
277     struct radeon_winsys_cs *(*cs_create)(struct radeon_winsys *ws);
278 
279     /**
280      * Destroy a command stream.
281      *
282      * \param cs        A command stream to destroy.
283      */
284     void (*cs_destroy)(struct radeon_winsys_cs *cs);
285 
286     /**
287      * Add a new buffer relocation. Every relocation must first be added
288      * before it can be written.
289      *
290      * \param cs  A command stream to add buffer for validation against.
291      * \param buf A winsys buffer to validate.
292      * \param usage   Whether the buffer is used for read and/or write.
293      * \param domain  Bitmask of the RADEON_DOMAIN_* flags.
294      * \return Relocation index.
295      */
296     unsigned (*cs_add_reloc)(struct radeon_winsys_cs *cs,
297                              struct radeon_winsys_cs_handle *buf,
298                              enum radeon_bo_usage usage,
299                              enum radeon_bo_domain domain);
300 
301     /**
302      * Return TRUE if there is enough memory in VRAM and GTT for the relocs
303      * added so far. If the validation fails, all the relocations which have
304      * been added since the last call of cs_validate will be removed and
305      * the CS will be flushed (provided there are still any relocations).
306      *
307      * \param cs        A command stream to validate.
308      */
309     boolean (*cs_validate)(struct radeon_winsys_cs *cs);
310 
311     /**
312      * Return TRUE if there is enough memory in VRAM and GTT for the relocs
313      * added so far.
314      *
315      * \param cs        A command stream to validate.
316      * \param vram      VRAM memory size pending to be use
317      * \param gtt       GTT memory size pending to be use
318      */
319     boolean (*cs_memory_below_limit)(struct radeon_winsys_cs *cs, uint64_t vram, uint64_t gtt);
320 
321     /**
322      * Write a relocated dword to a command buffer.
323      *
324      * \param cs        A command stream the relocation is written to.
325      * \param buf       A winsys buffer to write the relocation for.
326      */
327     void (*cs_write_reloc)(struct radeon_winsys_cs *cs,
328                            struct radeon_winsys_cs_handle *buf);
329 
330     /**
331      * Flush a command stream.
332      *
333      * \param cs        A command stream to flush.
334      * \param flags,    RADEON_FLUSH_ASYNC or 0.
335      */
336     void (*cs_flush)(struct radeon_winsys_cs *cs, unsigned flags);
337 
338     /**
339      * Set a flush callback which is called from winsys when flush is
340      * required.
341      *
342      * \param cs        A command stream to set the callback for.
343      * \param flush     A flush callback function associated with the command stream.
344      * \param user      A user pointer that will be passed to the flush callback.
345      */
346     void (*cs_set_flush_callback)(struct radeon_winsys_cs *cs,
347                                   void (*flush)(void *ctx, unsigned flags),
348                                   void *ctx);
349 
350     /**
351      * Return TRUE if a buffer is referenced by a command stream.
352      *
353      * \param cs        A command stream.
354      * \param buf       A winsys buffer.
355      */
356     boolean (*cs_is_buffer_referenced)(struct radeon_winsys_cs *cs,
357                                        struct radeon_winsys_cs_handle *buf,
358                                        enum radeon_bo_usage usage);
359 
360     /**
361      * Request access to a feature for a command stream.
362      *
363      * \param cs        A command stream.
364      * \param fid       Feature ID, one of RADEON_FID_*
365      * \param enable    Whether to enable or disable the feature.
366      */
367     boolean (*cs_request_feature)(struct radeon_winsys_cs *cs,
368                                   enum radeon_feature_id fid,
369                                   boolean enable);
370 
371     /**
372      * Initialize surface
373      *
374      * \param ws        The winsys this function is called from.
375      * \param surf      Surface structure ptr
376      */
377     int (*surface_init)(struct radeon_winsys *ws,
378                         struct radeon_surface *surf);
379 
380     /**
381      * Find best values for a surface
382      *
383      * \param ws        The winsys this function is called from.
384      * \param surf      Surface structure ptr
385      */
386     int (*surface_best)(struct radeon_winsys *ws,
387                         struct radeon_surface *surf);
388 
389     /**
390      * Return the current timestamp (gpu clock) on r600 and later GPUs.
391      *
392      * \param ws        The winsys this function is called from.
393      */
394     uint64_t (*query_timestamp)(struct radeon_winsys *ws);
395 };
396 
397 #endif
398