radeon_winsys.h revision 01e04c3f
1/*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4 * Copyright 2018 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * on the rights to use, copy, modify, merge, publish, distribute, sub
11 * license, and/or sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26#ifndef RADEON_WINSYS_H
27#define RADEON_WINSYS_H
28
29/* The public winsys interface header for the radeon driver. */
30
31/* Whether the next IB can start immediately and not wait for draws and
32 * dispatches from the current IB to finish. */
33#define RADEON_FLUSH_START_NEXT_GFX_IB_NOW	(1u << 31)
34
35#define RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW \
36	(PIPE_FLUSH_ASYNC | RADEON_FLUSH_START_NEXT_GFX_IB_NOW)
37
38#include "pipebuffer/pb_buffer.h"
39
40#include "amd/common/ac_gpu_info.h"
41#include "amd/common/ac_surface.h"
42
43/* Tiling flags. */
44enum radeon_bo_layout {
45    RADEON_LAYOUT_LINEAR = 0,
46    RADEON_LAYOUT_TILED,
47    RADEON_LAYOUT_SQUARETILED,
48
49    RADEON_LAYOUT_UNKNOWN
50};
51
52enum radeon_bo_domain { /* bitfield */
53    RADEON_DOMAIN_GTT  = 2,
54    RADEON_DOMAIN_VRAM = 4,
55    RADEON_DOMAIN_VRAM_GTT = RADEON_DOMAIN_VRAM | RADEON_DOMAIN_GTT
56};
57
58enum radeon_bo_flag { /* bitfield */
59    RADEON_FLAG_GTT_WC =        (1 << 0),
60    RADEON_FLAG_NO_CPU_ACCESS = (1 << 1),
61    RADEON_FLAG_NO_SUBALLOC =   (1 << 2),
62    RADEON_FLAG_SPARSE =        (1 << 3),
63    RADEON_FLAG_NO_INTERPROCESS_SHARING = (1 << 4),
64    RADEON_FLAG_READ_ONLY =     (1 << 5),
65    RADEON_FLAG_32BIT =    (1 << 6),
66};
67
68enum radeon_bo_usage { /* bitfield */
69    RADEON_USAGE_READ = 2,
70    RADEON_USAGE_WRITE = 4,
71    RADEON_USAGE_READWRITE = RADEON_USAGE_READ | RADEON_USAGE_WRITE,
72
73    /* The winsys ensures that the CS submission will be scheduled after
74     * previously flushed CSs referencing this BO in a conflicting way.
75     */
76    RADEON_USAGE_SYNCHRONIZED = 8
77};
78
79#define RADEON_SPARSE_PAGE_SIZE (64 * 1024)
80
81enum ring_type {
82    RING_GFX = 0,
83    RING_COMPUTE,
84    RING_DMA,
85    RING_UVD,
86    RING_VCE,
87    RING_UVD_ENC,
88    RING_VCN_DEC,
89    RING_VCN_ENC,
90    RING_VCN_JPEG,
91    RING_LAST,
92};
93
94enum radeon_value_id {
95    RADEON_REQUESTED_VRAM_MEMORY,
96    RADEON_REQUESTED_GTT_MEMORY,
97    RADEON_MAPPED_VRAM,
98    RADEON_MAPPED_GTT,
99    RADEON_BUFFER_WAIT_TIME_NS,
100    RADEON_NUM_MAPPED_BUFFERS,
101    RADEON_TIMESTAMP,
102    RADEON_NUM_GFX_IBS,
103    RADEON_NUM_SDMA_IBS,
104    RADEON_GFX_BO_LIST_COUNTER, /* number of BOs submitted in gfx IBs */
105    RADEON_GFX_IB_SIZE_COUNTER,
106    RADEON_NUM_BYTES_MOVED,
107    RADEON_NUM_EVICTIONS,
108    RADEON_NUM_VRAM_CPU_PAGE_FAULTS,
109    RADEON_VRAM_USAGE,
110    RADEON_VRAM_VIS_USAGE,
111    RADEON_GTT_USAGE,
112    RADEON_GPU_TEMPERATURE, /* DRM 2.42.0 */
113    RADEON_CURRENT_SCLK,
114    RADEON_CURRENT_MCLK,
115    RADEON_GPU_RESET_COUNTER, /* DRM 2.43.0 */
116    RADEON_CS_THREAD_TIME,
117};
118
119enum radeon_bo_priority {
120    /* Each group of two has the same priority. */
121    RADEON_PRIO_FENCE = 0,
122    RADEON_PRIO_TRACE,
123
124    RADEON_PRIO_SO_FILLED_SIZE = 2,
125    RADEON_PRIO_QUERY,
126
127    RADEON_PRIO_IB1 = 4, /* main IB submitted to the kernel */
128    RADEON_PRIO_IB2, /* IB executed with INDIRECT_BUFFER */
129
130    RADEON_PRIO_DRAW_INDIRECT = 6,
131    RADEON_PRIO_INDEX_BUFFER,
132
133    RADEON_PRIO_CP_DMA = 8,
134    RADEON_PRIO_BORDER_COLORS,
135
136    RADEON_PRIO_CONST_BUFFER = 10,
137    RADEON_PRIO_DESCRIPTORS,
138
139    RADEON_PRIO_SAMPLER_BUFFER = 12,
140    RADEON_PRIO_VERTEX_BUFFER,
141
142    RADEON_PRIO_SHADER_RW_BUFFER = 14,
143    RADEON_PRIO_COMPUTE_GLOBAL,
144
145    RADEON_PRIO_SAMPLER_TEXTURE = 16,
146    RADEON_PRIO_SHADER_RW_IMAGE,
147
148    RADEON_PRIO_SAMPLER_TEXTURE_MSAA = 18,
149    RADEON_PRIO_COLOR_BUFFER,
150
151    RADEON_PRIO_DEPTH_BUFFER = 20,
152
153    RADEON_PRIO_COLOR_BUFFER_MSAA = 22,
154
155    RADEON_PRIO_DEPTH_BUFFER_MSAA = 24,
156
157    RADEON_PRIO_SEPARATE_META = 26,
158    RADEON_PRIO_SHADER_BINARY, /* the hw can't hide instruction cache misses */
159
160    RADEON_PRIO_SHADER_RINGS = 28,
161
162    RADEON_PRIO_SCRATCH_BUFFER = 30,
163    /* 31 is the maximum value */
164};
165
166struct winsys_handle;
167struct radeon_winsys_ctx;
168
169struct radeon_cmdbuf_chunk {
170    unsigned cdw;  /* Number of used dwords. */
171    unsigned max_dw; /* Maximum number of dwords. */
172    uint32_t *buf; /* The base pointer of the chunk. */
173};
174
175struct radeon_cmdbuf {
176    struct radeon_cmdbuf_chunk current;
177    struct radeon_cmdbuf_chunk *prev;
178    unsigned                      num_prev; /* Number of previous chunks. */
179    unsigned                      max_prev; /* Space in array pointed to by prev. */
180    unsigned                      prev_dw; /* Total number of dwords in previous chunks. */
181
182    /* Memory usage of the buffer list. These are always 0 for preamble IBs. */
183    uint64_t                      used_vram;
184    uint64_t                      used_gart;
185};
186
187/* Tiling info for display code, DRI sharing, and other data. */
188struct radeon_bo_metadata {
189    /* Tiling flags describing the texture layout for display code
190     * and DRI sharing.
191     */
192    union {
193        struct {
194            enum radeon_bo_layout   microtile;
195            enum radeon_bo_layout   macrotile;
196            unsigned                pipe_config;
197            unsigned                bankw;
198            unsigned                bankh;
199            unsigned                tile_split;
200            unsigned                mtilea;
201            unsigned                num_banks;
202            unsigned                stride;
203            bool                    scanout;
204        } legacy;
205
206        struct {
207            /* surface flags */
208            unsigned swizzle_mode:5;
209        } gfx9;
210    } u;
211
212    /* Additional metadata associated with the buffer, in bytes.
213     * The maximum size is 64 * 4. This is opaque for the winsys & kernel.
214     * Supported by amdgpu only.
215     */
216    uint32_t                size_metadata;
217    uint32_t                metadata[64];
218};
219
220enum radeon_feature_id {
221    RADEON_FID_R300_HYPERZ_ACCESS,     /* ZMask + HiZ */
222    RADEON_FID_R300_CMASK_ACCESS,
223};
224
225struct radeon_bo_list_item {
226    uint64_t bo_size;
227    uint64_t vm_address;
228    uint32_t priority_usage; /* mask of (1 << RADEON_PRIO_*) */
229};
230
231struct radeon_winsys {
232    /**
233     * The screen object this winsys was created for
234     */
235    struct pipe_screen *screen;
236
237    /**
238     * Decrement the winsys reference count.
239     *
240     * \param ws  The winsys this function is called for.
241     * \return    True if the winsys and screen should be destroyed.
242     */
243    bool (*unref)(struct radeon_winsys *ws);
244
245    /**
246     * Destroy this winsys.
247     *
248     * \param ws        The winsys this function is called from.
249     */
250    void (*destroy)(struct radeon_winsys *ws);
251
252    /**
253     * Query an info structure from winsys.
254     *
255     * \param ws        The winsys this function is called from.
256     * \param info      Return structure
257     */
258    void (*query_info)(struct radeon_winsys *ws,
259                       struct radeon_info *info);
260
261    /**
262     * A hint for the winsys that it should pin its execution threads to
263     * a group of cores sharing a specific L3 cache if the CPU has multiple
264     * L3 caches. This is needed for good multithreading performance on
265     * AMD Zen CPUs.
266     */
267    void (*pin_threads_to_L3_cache)(struct radeon_winsys *ws, unsigned cache);
268
269    /**************************************************************************
270     * Buffer management. Buffer attributes are mostly fixed over its lifetime.
271     *
272     * Remember that gallium gets to choose the interface it needs, and the
273     * window systems must then implement that interface (rather than the
274     * other way around...).
275     *************************************************************************/
276
277    /**
278     * Create a buffer object.
279     *
280     * \param ws        The winsys this function is called from.
281     * \param size      The size to allocate.
282     * \param alignment An alignment of the buffer in memory.
283     * \param use_reusable_pool Whether the cache buffer manager should be used.
284     * \param domain    A bitmask of the RADEON_DOMAIN_* flags.
285     * \return          The created buffer object.
286     */
287    struct pb_buffer *(*buffer_create)(struct radeon_winsys *ws,
288                                       uint64_t size,
289                                       unsigned alignment,
290                                       enum radeon_bo_domain domain,
291                                       enum radeon_bo_flag flags);
292
293    /**
294     * Map the entire data store of a buffer object into the client's address
295     * space.
296     *
297     * \param buf       A winsys buffer object to map.
298     * \param cs        A command stream to flush if the buffer is referenced by it.
299     * \param usage     A bitmask of the PIPE_TRANSFER_* flags.
300     * \return          The pointer at the beginning of the buffer.
301     */
302    void *(*buffer_map)(struct pb_buffer *buf,
303                        struct radeon_cmdbuf *cs,
304                        enum pipe_transfer_usage usage);
305
306    /**
307     * Unmap a buffer object from the client's address space.
308     *
309     * \param buf       A winsys buffer object to unmap.
310     */
311    void (*buffer_unmap)(struct pb_buffer *buf);
312
313    /**
314     * Wait for the buffer and return true if the buffer is not used
315     * by the device.
316     *
317     * The timeout of 0 will only return the status.
318     * The timeout of PIPE_TIMEOUT_INFINITE will always wait until the buffer
319     * is idle.
320     */
321    bool (*buffer_wait)(struct pb_buffer *buf, uint64_t timeout,
322                        enum radeon_bo_usage usage);
323
324    /**
325     * Return buffer metadata.
326     * (tiling info for display code, DRI sharing, and other data)
327     *
328     * \param buf       A winsys buffer object to get the flags from.
329     * \param md        Metadata
330     */
331    void (*buffer_get_metadata)(struct pb_buffer *buf,
332                                struct radeon_bo_metadata *md);
333
334    /**
335     * Set buffer metadata.
336     * (tiling info for display code, DRI sharing, and other data)
337     *
338     * \param buf       A winsys buffer object to set the flags for.
339     * \param md        Metadata
340     */
341    void (*buffer_set_metadata)(struct pb_buffer *buf,
342                                struct radeon_bo_metadata *md);
343
344    /**
345     * Get a winsys buffer from a winsys handle. The internal structure
346     * of the handle is platform-specific and only a winsys should access it.
347     *
348     * \param ws        The winsys this function is called from.
349     * \param whandle   A winsys handle pointer as was received from a state
350     *                  tracker.
351     * \param stride    The returned buffer stride in bytes.
352     */
353    struct pb_buffer *(*buffer_from_handle)(struct radeon_winsys *ws,
354                                            struct winsys_handle *whandle,
355                                            unsigned *stride, unsigned *offset);
356
357    /**
358     * Get a winsys buffer from a user pointer. The resulting buffer can't
359     * be exported. Both pointer and size must be page aligned.
360     *
361     * \param ws        The winsys this function is called from.
362     * \param pointer   User pointer to turn into a buffer object.
363     * \param Size      Size in bytes for the new buffer.
364     */
365    struct pb_buffer *(*buffer_from_ptr)(struct radeon_winsys *ws,
366                                         void *pointer, uint64_t size);
367
368    /**
369     * Whether the buffer was created from a user pointer.
370     *
371     * \param buf       A winsys buffer object
372     * \return          whether \p buf was created via buffer_from_ptr
373     */
374    bool (*buffer_is_user_ptr)(struct pb_buffer *buf);
375
376    /** Whether the buffer was suballocated. */
377    bool (*buffer_is_suballocated)(struct pb_buffer *buf);
378
379    /**
380     * Get a winsys handle from a winsys buffer. The internal structure
381     * of the handle is platform-specific and only a winsys should access it.
382     *
383     * \param buf       A winsys buffer object to get the handle from.
384     * \param whandle   A winsys handle pointer.
385     * \param stride    A stride of the buffer in bytes, for texturing.
386     * \return          true on success.
387     */
388    bool (*buffer_get_handle)(struct pb_buffer *buf,
389                              unsigned stride, unsigned offset,
390                              unsigned slice_size,
391                              struct winsys_handle *whandle);
392
393    /**
394     * Change the commitment of a (64KB-page aligned) region of the given
395     * sparse buffer.
396     *
397     * \warning There is no automatic synchronization with command submission.
398     *
399     * \note Only implemented by the amdgpu winsys.
400     *
401     * \return false on out of memory or other failure, true on success.
402     */
403    bool (*buffer_commit)(struct pb_buffer *buf,
404                          uint64_t offset, uint64_t size,
405                          bool commit);
406
407    /**
408     * Return the virtual address of a buffer.
409     *
410     * When virtual memory is not in use, this is the offset relative to the
411     * relocation base (non-zero for sub-allocated buffers).
412     *
413     * \param buf       A winsys buffer object
414     * \return          virtual address
415     */
416    uint64_t (*buffer_get_virtual_address)(struct pb_buffer *buf);
417
418    /**
419     * Return the offset of this buffer relative to the relocation base.
420     * This is only non-zero for sub-allocated buffers.
421     *
422     * This is only supported in the radeon winsys, since amdgpu uses virtual
423     * addresses in submissions even for the video engines.
424     *
425     * \param buf      A winsys buffer object
426     * \return         the offset for relocations
427     */
428    unsigned (*buffer_get_reloc_offset)(struct pb_buffer *buf);
429
430    /**
431     * Query the initial placement of the buffer from the kernel driver.
432     */
433    enum radeon_bo_domain (*buffer_get_initial_domain)(struct pb_buffer *buf);
434
435    /**************************************************************************
436     * Command submission.
437     *
438     * Each pipe context should create its own command stream and submit
439     * commands independently of other contexts.
440     *************************************************************************/
441
442    /**
443     * Create a command submission context.
444     * Various command streams can be submitted to the same context.
445     */
446    struct radeon_winsys_ctx *(*ctx_create)(struct radeon_winsys *ws);
447
448    /**
449     * Destroy a context.
450     */
451    void (*ctx_destroy)(struct radeon_winsys_ctx *ctx);
452
453    /**
454     * Query a GPU reset status.
455     */
456    enum pipe_reset_status (*ctx_query_reset_status)(struct radeon_winsys_ctx *ctx);
457
458    /**
459     * Create a command stream.
460     *
461     * \param ctx       The submission context
462     * \param ring_type The ring type (GFX, DMA, UVD)
463     * \param flush     Flush callback function associated with the command stream.
464     * \param user      User pointer that will be passed to the flush callback.
465     */
466    struct radeon_cmdbuf *(*cs_create)(struct radeon_winsys_ctx *ctx,
467                                          enum ring_type ring_type,
468                                          void (*flush)(void *ctx, unsigned flags,
469							struct pipe_fence_handle **fence),
470                                          void *flush_ctx);
471
472    /**
473     * Destroy a command stream.
474     *
475     * \param cs        A command stream to destroy.
476     */
477    void (*cs_destroy)(struct radeon_cmdbuf *cs);
478
479    /**
480     * Add a buffer. Each buffer used by a CS must be added using this function.
481     *
482     * \param cs      Command stream
483     * \param buf     Buffer
484     * \param usage   Whether the buffer is used for read and/or write.
485     * \param domain  Bitmask of the RADEON_DOMAIN_* flags.
486     * \param priority  A higher number means a greater chance of being
487     *                  placed in the requested domain. 15 is the maximum.
488     * \return Buffer index.
489     */
490    unsigned (*cs_add_buffer)(struct radeon_cmdbuf *cs,
491                             struct pb_buffer *buf,
492                             enum radeon_bo_usage usage,
493                             enum radeon_bo_domain domain,
494                             enum radeon_bo_priority priority);
495
496    /**
497     * Return the index of an already-added buffer.
498     *
499     * Not supported on amdgpu. Drivers with GPUVM should not care about
500     * buffer indices.
501     *
502     * \param cs        Command stream
503     * \param buf       Buffer
504     * \return          The buffer index, or -1 if the buffer has not been added.
505     */
506    int (*cs_lookup_buffer)(struct radeon_cmdbuf *cs,
507                            struct pb_buffer *buf);
508
509    /**
510     * Return true if there is enough memory in VRAM and GTT for the buffers
511     * added so far. If the validation fails, all buffers which have
512     * been added since the last call of cs_validate will be removed and
513     * the CS will be flushed (provided there are still any buffers).
514     *
515     * \param cs        A command stream to validate.
516     */
517    bool (*cs_validate)(struct radeon_cmdbuf *cs);
518
519    /**
520     * Check whether the given number of dwords is available in the IB.
521     * Optionally chain a new chunk of the IB if necessary and supported.
522     *
523     * \param cs        A command stream.
524     * \param dw        Number of CS dwords requested by the caller.
525     */
526    bool (*cs_check_space)(struct radeon_cmdbuf *cs, unsigned dw);
527
528    /**
529     * Return the buffer list.
530     *
531     * This is the buffer list as passed to the kernel, i.e. it only contains
532     * the parent buffers of sub-allocated buffers.
533     *
534     * \param cs    Command stream
535     * \param list  Returned buffer list. Set to NULL to query the count only.
536     * \return      The buffer count.
537     */
538    unsigned (*cs_get_buffer_list)(struct radeon_cmdbuf *cs,
539                                   struct radeon_bo_list_item *list);
540
541    /**
542     * Flush a command stream.
543     *
544     * \param cs          A command stream to flush.
545     * \param flags,      PIPE_FLUSH_* flags.
546     * \param fence       Pointer to a fence. If non-NULL, a fence is inserted
547     *                    after the CS and is returned through this parameter.
548     * \return Negative POSIX error code or 0 for success.
549     *         Asynchronous submissions never return an error.
550     */
551    int (*cs_flush)(struct radeon_cmdbuf *cs,
552                    unsigned flags,
553                    struct pipe_fence_handle **fence);
554
555    /**
556     * Create a fence before the CS is flushed.
557     * The user must flush manually to complete the initializaton of the fence.
558     *
559     * The fence must not be used for anything except \ref cs_add_fence_dependency
560     * before the flush.
561     */
562    struct pipe_fence_handle *(*cs_get_next_fence)(struct radeon_cmdbuf *cs);
563
564    /**
565     * Return true if a buffer is referenced by a command stream.
566     *
567     * \param cs        A command stream.
568     * \param buf       A winsys buffer.
569     */
570    bool (*cs_is_buffer_referenced)(struct radeon_cmdbuf *cs,
571                                    struct pb_buffer *buf,
572                                    enum radeon_bo_usage usage);
573
574    /**
575     * Request access to a feature for a command stream.
576     *
577     * \param cs        A command stream.
578     * \param fid       Feature ID, one of RADEON_FID_*
579     * \param enable    Whether to enable or disable the feature.
580     */
581    bool (*cs_request_feature)(struct radeon_cmdbuf *cs,
582                               enum radeon_feature_id fid,
583                               bool enable);
584     /**
585      * Make sure all asynchronous flush of the cs have completed
586      *
587      * \param cs        A command stream.
588      */
589    void (*cs_sync_flush)(struct radeon_cmdbuf *cs);
590
591    /**
592     * Add a fence dependency to the CS, so that the CS will wait for
593     * the fence before execution.
594     */
595    void (*cs_add_fence_dependency)(struct radeon_cmdbuf *cs,
596                                    struct pipe_fence_handle *fence);
597
598    /**
599     * Signal a syncobj when the CS finishes execution.
600     */
601    void (*cs_add_syncobj_signal)(struct radeon_cmdbuf *cs,
602				  struct pipe_fence_handle *fence);
603
604    /**
605     * Wait for the fence and return true if the fence has been signalled.
606     * The timeout of 0 will only return the status.
607     * The timeout of PIPE_TIMEOUT_INFINITE will always wait until the fence
608     * is signalled.
609     */
610    bool (*fence_wait)(struct radeon_winsys *ws,
611                       struct pipe_fence_handle *fence,
612                       uint64_t timeout);
613
614    /**
615     * Reference counting for fences.
616     */
617    void (*fence_reference)(struct pipe_fence_handle **dst,
618                            struct pipe_fence_handle *src);
619
620    /**
621     * Create a new fence object corresponding to the given syncobj fd.
622     */
623    struct pipe_fence_handle *(*fence_import_syncobj)(struct radeon_winsys *ws,
624						      int fd);
625
626    /**
627     * Create a new fence object corresponding to the given sync_file.
628     */
629    struct pipe_fence_handle *(*fence_import_sync_file)(struct radeon_winsys *ws,
630							int fd);
631
632    /**
633     * Return a sync_file FD corresponding to the given fence object.
634     */
635    int (*fence_export_sync_file)(struct radeon_winsys *ws,
636				  struct pipe_fence_handle *fence);
637
638    /**
639     * Return a sync file FD that is already signalled.
640     */
641    int (*export_signalled_sync_file)(struct radeon_winsys *ws);
642
643    /**
644     * Initialize surface
645     *
646     * \param ws        The winsys this function is called from.
647     * \param tex       Input texture description
648     * \param flags     Bitmask of RADEON_SURF_* flags
649     * \param bpe       Bytes per pixel, it can be different for Z buffers.
650     * \param mode      Preferred tile mode. (linear, 1D, or 2D)
651     * \param surf      Output structure
652     */
653    int (*surface_init)(struct radeon_winsys *ws,
654                        const struct pipe_resource *tex,
655                        unsigned flags, unsigned bpe,
656                        enum radeon_surf_mode mode,
657                        struct radeon_surf *surf);
658
659    uint64_t (*query_value)(struct radeon_winsys *ws,
660                            enum radeon_value_id value);
661
662    bool (*read_registers)(struct radeon_winsys *ws, unsigned reg_offset,
663                           unsigned num_registers, uint32_t *out);
664
665    const char* (*get_chip_name)(struct radeon_winsys *ws);
666};
667
668static inline bool radeon_emitted(struct radeon_cmdbuf *cs, unsigned num_dw)
669{
670    return cs && (cs->prev_dw + cs->current.cdw > num_dw);
671}
672
673static inline void radeon_emit(struct radeon_cmdbuf *cs, uint32_t value)
674{
675    cs->current.buf[cs->current.cdw++] = value;
676}
677
678static inline void radeon_emit_array(struct radeon_cmdbuf *cs,
679				     const uint32_t *values, unsigned count)
680{
681    memcpy(cs->current.buf + cs->current.cdw, values, count * 4);
682    cs->current.cdw += count;
683}
684
685enum radeon_heap {
686    RADEON_HEAP_VRAM_NO_CPU_ACCESS,
687    RADEON_HEAP_VRAM_READ_ONLY,
688    RADEON_HEAP_VRAM_READ_ONLY_32BIT,
689    RADEON_HEAP_VRAM_32BIT,
690    RADEON_HEAP_VRAM,
691    RADEON_HEAP_GTT_WC,
692    RADEON_HEAP_GTT_WC_READ_ONLY,
693    RADEON_HEAP_GTT_WC_READ_ONLY_32BIT,
694    RADEON_HEAP_GTT_WC_32BIT,
695    RADEON_HEAP_GTT,
696    RADEON_MAX_SLAB_HEAPS,
697    RADEON_MAX_CACHED_HEAPS = RADEON_MAX_SLAB_HEAPS,
698};
699
700static inline enum radeon_bo_domain radeon_domain_from_heap(enum radeon_heap heap)
701{
702    switch (heap) {
703    case RADEON_HEAP_VRAM_NO_CPU_ACCESS:
704    case RADEON_HEAP_VRAM_READ_ONLY:
705    case RADEON_HEAP_VRAM_READ_ONLY_32BIT:
706    case RADEON_HEAP_VRAM_32BIT:
707    case RADEON_HEAP_VRAM:
708        return RADEON_DOMAIN_VRAM;
709    case RADEON_HEAP_GTT_WC:
710    case RADEON_HEAP_GTT_WC_READ_ONLY:
711    case RADEON_HEAP_GTT_WC_READ_ONLY_32BIT:
712    case RADEON_HEAP_GTT_WC_32BIT:
713    case RADEON_HEAP_GTT:
714        return RADEON_DOMAIN_GTT;
715    default:
716        assert(0);
717        return (enum radeon_bo_domain)0;
718    }
719}
720
721static inline unsigned radeon_flags_from_heap(enum radeon_heap heap)
722{
723    unsigned flags = RADEON_FLAG_NO_INTERPROCESS_SHARING |
724                     (heap != RADEON_HEAP_GTT ? RADEON_FLAG_GTT_WC : 0);
725
726    switch (heap) {
727    case RADEON_HEAP_VRAM_NO_CPU_ACCESS:
728        return flags |
729               RADEON_FLAG_NO_CPU_ACCESS;
730
731    case RADEON_HEAP_VRAM_READ_ONLY:
732    case RADEON_HEAP_GTT_WC_READ_ONLY:
733        return flags |
734               RADEON_FLAG_READ_ONLY;
735
736    case RADEON_HEAP_VRAM_READ_ONLY_32BIT:
737    case RADEON_HEAP_GTT_WC_READ_ONLY_32BIT:
738        return flags |
739               RADEON_FLAG_READ_ONLY |
740               RADEON_FLAG_32BIT;
741
742    case RADEON_HEAP_VRAM_32BIT:
743    case RADEON_HEAP_GTT_WC_32BIT:
744        return flags |
745               RADEON_FLAG_32BIT;
746
747    case RADEON_HEAP_VRAM:
748    case RADEON_HEAP_GTT_WC:
749    case RADEON_HEAP_GTT:
750    default:
751        return flags;
752    }
753}
754
755/* Return the heap index for winsys allocators, or -1 on failure. */
756static inline int radeon_get_heap_index(enum radeon_bo_domain domain,
757                                        enum radeon_bo_flag flags)
758{
759    /* VRAM implies WC (write combining) */
760    assert(!(domain & RADEON_DOMAIN_VRAM) || flags & RADEON_FLAG_GTT_WC);
761    /* NO_CPU_ACCESS implies VRAM only. */
762    assert(!(flags & RADEON_FLAG_NO_CPU_ACCESS) || domain == RADEON_DOMAIN_VRAM);
763
764    /* Resources with interprocess sharing don't use any winsys allocators. */
765    if (!(flags & RADEON_FLAG_NO_INTERPROCESS_SHARING))
766        return -1;
767
768    /* Unsupported flags: NO_SUBALLOC, SPARSE. */
769    if (flags & ~(RADEON_FLAG_GTT_WC |
770                  RADEON_FLAG_NO_CPU_ACCESS |
771                  RADEON_FLAG_NO_INTERPROCESS_SHARING |
772                  RADEON_FLAG_READ_ONLY |
773                  RADEON_FLAG_32BIT))
774        return -1;
775
776    switch (domain) {
777    case RADEON_DOMAIN_VRAM:
778        switch (flags & (RADEON_FLAG_NO_CPU_ACCESS |
779                         RADEON_FLAG_READ_ONLY |
780                         RADEON_FLAG_32BIT)) {
781        case RADEON_FLAG_NO_CPU_ACCESS | RADEON_FLAG_READ_ONLY | RADEON_FLAG_32BIT:
782        case RADEON_FLAG_NO_CPU_ACCESS | RADEON_FLAG_READ_ONLY:
783            assert(!"NO_CPU_ACCESS | READ_ONLY doesn't make sense");
784            return -1;
785        case RADEON_FLAG_NO_CPU_ACCESS | RADEON_FLAG_32BIT:
786            assert(!"NO_CPU_ACCESS with 32BIT is disallowed");
787            return -1;
788        case RADEON_FLAG_NO_CPU_ACCESS:
789            return RADEON_HEAP_VRAM_NO_CPU_ACCESS;
790        case RADEON_FLAG_READ_ONLY | RADEON_FLAG_32BIT:
791            return RADEON_HEAP_VRAM_READ_ONLY_32BIT;
792        case RADEON_FLAG_READ_ONLY:
793            return RADEON_HEAP_VRAM_READ_ONLY;
794        case RADEON_FLAG_32BIT:
795            return RADEON_HEAP_VRAM_32BIT;
796        case 0:
797            return RADEON_HEAP_VRAM;
798        }
799        break;
800    case RADEON_DOMAIN_GTT:
801        switch (flags & (RADEON_FLAG_GTT_WC |
802                         RADEON_FLAG_READ_ONLY |
803                         RADEON_FLAG_32BIT)) {
804        case RADEON_FLAG_GTT_WC | RADEON_FLAG_READ_ONLY | RADEON_FLAG_32BIT:
805            return RADEON_HEAP_GTT_WC_READ_ONLY_32BIT;
806        case RADEON_FLAG_GTT_WC | RADEON_FLAG_READ_ONLY:
807            return RADEON_HEAP_GTT_WC_READ_ONLY;
808        case RADEON_FLAG_GTT_WC | RADEON_FLAG_32BIT:
809            return RADEON_HEAP_GTT_WC_32BIT;
810        case RADEON_FLAG_GTT_WC:
811            return RADEON_HEAP_GTT_WC;
812        case RADEON_FLAG_READ_ONLY | RADEON_FLAG_32BIT:
813        case RADEON_FLAG_READ_ONLY:
814            assert(!"READ_ONLY without WC is disallowed");
815            return -1;
816        case RADEON_FLAG_32BIT:
817            assert(!"32BIT without WC is disallowed");
818            return -1;
819        case 0:
820            return RADEON_HEAP_GTT;
821        }
822        break;
823    default:
824        break;
825    }
826    return -1;
827}
828
829#endif
830