1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2017 Advanced Micro Devices, Inc.
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining
5b8e80941Smrg * a copy of this software and associated documentation files (the
6b8e80941Smrg * "Software"), to deal in the Software without restriction, including
7b8e80941Smrg * without limitation the rights to use, copy, modify, merge, publish,
8b8e80941Smrg * distribute, sub license, and/or sell copies of the Software, and to
9b8e80941Smrg * permit persons to whom the Software is furnished to do so, subject to
10b8e80941Smrg * the following conditions:
11b8e80941Smrg *
12b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13b8e80941Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
14b8e80941Smrg * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15b8e80941Smrg * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
16b8e80941Smrg * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18b8e80941Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19b8e80941Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
20b8e80941Smrg *
21b8e80941Smrg * The above copyright notice and this permission notice (including the
22b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions
23b8e80941Smrg * of the Software.
24b8e80941Smrg */
25b8e80941Smrg
26b8e80941Smrg#ifndef AC_SURFACE_H
27b8e80941Smrg#define AC_SURFACE_H
28b8e80941Smrg
29b8e80941Smrg#include <stdint.h>
30b8e80941Smrg#include <stdbool.h>
31b8e80941Smrg
32b8e80941Smrg#include "amd_family.h"
33b8e80941Smrg
34b8e80941Smrg#ifdef __cplusplus
35b8e80941Smrgextern "C" {
36b8e80941Smrg#endif
37b8e80941Smrg
38b8e80941Smrg/* Forward declarations. */
39d4248a18Schristos#ifndef ADDR_HANDLE_TYPEDEF
40d4248a18Schristos#define ADDR_HANDLE_TYPEDEF
41b8e80941Smrgtypedef void* ADDR_HANDLE;
42d4248a18Schristos#endif
43b8e80941Smrg
44b8e80941Smrgstruct amdgpu_gpu_info;
45b8e80941Smrgstruct radeon_info;
46b8e80941Smrg
47b8e80941Smrg#define RADEON_SURF_MAX_LEVELS                  15
48b8e80941Smrg
49b8e80941Smrgenum radeon_surf_mode {
50b8e80941Smrg    RADEON_SURF_MODE_LINEAR_ALIGNED = 1,
51b8e80941Smrg    RADEON_SURF_MODE_1D = 2,
52b8e80941Smrg    RADEON_SURF_MODE_2D = 3,
53b8e80941Smrg};
54b8e80941Smrg
55b8e80941Smrg/* These are defined exactly like GB_TILE_MODEn.MICRO_TILE_MODE_NEW. */
56b8e80941Smrgenum radeon_micro_mode {
57b8e80941Smrg    RADEON_MICRO_MODE_DISPLAY = 0,
58b8e80941Smrg    RADEON_MICRO_MODE_THIN = 1,
59b8e80941Smrg    RADEON_MICRO_MODE_DEPTH = 2,
60b8e80941Smrg    RADEON_MICRO_MODE_ROTATED = 3,
61b8e80941Smrg};
62b8e80941Smrg
63b8e80941Smrg/* the first 16 bits are reserved for libdrm_radeon, don't use them */
64b8e80941Smrg#define RADEON_SURF_SCANOUT                     (1 << 16)
65b8e80941Smrg#define RADEON_SURF_ZBUFFER                     (1 << 17)
66b8e80941Smrg#define RADEON_SURF_SBUFFER                     (1 << 18)
67b8e80941Smrg#define RADEON_SURF_Z_OR_SBUFFER                (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)
68b8e80941Smrg/* bits 19 and 20 are reserved for libdrm_radeon, don't use them */
69b8e80941Smrg#define RADEON_SURF_FMASK                       (1 << 21)
70b8e80941Smrg#define RADEON_SURF_DISABLE_DCC                 (1 << 22)
71b8e80941Smrg#define RADEON_SURF_TC_COMPATIBLE_HTILE         (1 << 23)
72b8e80941Smrg#define RADEON_SURF_IMPORTED                    (1 << 24)
73b8e80941Smrg#define RADEON_SURF_OPTIMIZE_FOR_SPACE          (1 << 25)
74b8e80941Smrg#define RADEON_SURF_SHAREABLE                   (1 << 26)
75b8e80941Smrg#define RADEON_SURF_NO_RENDER_TARGET            (1 << 27)
76b8e80941Smrg
77b8e80941Smrgstruct legacy_surf_level {
78b8e80941Smrg    uint64_t                    offset;
79b8e80941Smrg    uint32_t                    slice_size_dw; /* in dwords; max = 4GB / 4. */
80b8e80941Smrg    uint32_t                    dcc_offset; /* relative offset within DCC mip tree */
81b8e80941Smrg    uint32_t                    dcc_fast_clear_size;
82b8e80941Smrg    unsigned                    nblk_x:15;
83b8e80941Smrg    unsigned                    nblk_y:15;
84b8e80941Smrg    enum radeon_surf_mode       mode:2;
85b8e80941Smrg};
86b8e80941Smrg
87b8e80941Smrgstruct legacy_surf_fmask {
88b8e80941Smrg    unsigned slice_tile_max; /* max 4M */
89b8e80941Smrg    uint8_t tiling_index;    /* max 31 */
90b8e80941Smrg    uint8_t bankh;           /* max 8 */
91b8e80941Smrg    uint16_t pitch_in_pixels;
92b8e80941Smrg};
93b8e80941Smrg
94b8e80941Smrgstruct legacy_surf_layout {
95b8e80941Smrg    unsigned                    bankw:4;  /* max 8 */
96b8e80941Smrg    unsigned                    bankh:4;  /* max 8 */
97b8e80941Smrg    unsigned                    mtilea:4; /* max 8 */
98b8e80941Smrg    unsigned                    tile_split:13;         /* max 4K */
99b8e80941Smrg    unsigned                    stencil_tile_split:13; /* max 4K */
100b8e80941Smrg    unsigned                    pipe_config:5;      /* max 17 */
101b8e80941Smrg    unsigned                    num_banks:5;        /* max 16 */
102b8e80941Smrg    unsigned                    macro_tile_index:4; /* max 15 */
103b8e80941Smrg
104b8e80941Smrg    /* Whether the depth miptree or stencil miptree as used by the DB are
105b8e80941Smrg     * adjusted from their TC compatible form to ensure depth/stencil
106b8e80941Smrg     * compatibility. If either is true, the corresponding plane cannot be
107b8e80941Smrg     * sampled from.
108b8e80941Smrg     */
109b8e80941Smrg    unsigned                    depth_adjusted:1;
110b8e80941Smrg    unsigned                    stencil_adjusted:1;
111b8e80941Smrg
112b8e80941Smrg    struct legacy_surf_level    level[RADEON_SURF_MAX_LEVELS];
113b8e80941Smrg    struct legacy_surf_level    stencil_level[RADEON_SURF_MAX_LEVELS];
114b8e80941Smrg    uint8_t                     tiling_index[RADEON_SURF_MAX_LEVELS];
115b8e80941Smrg    uint8_t                     stencil_tiling_index[RADEON_SURF_MAX_LEVELS];
116b8e80941Smrg    struct legacy_surf_fmask    fmask;
117b8e80941Smrg    unsigned                    cmask_slice_tile_max;
118b8e80941Smrg};
119b8e80941Smrg
120b8e80941Smrg/* Same as addrlib - AddrResourceType. */
121b8e80941Smrgenum gfx9_resource_type {
122b8e80941Smrg    RADEON_RESOURCE_1D = 0,
123b8e80941Smrg    RADEON_RESOURCE_2D,
124b8e80941Smrg    RADEON_RESOURCE_3D,
125b8e80941Smrg};
126b8e80941Smrg
127b8e80941Smrgstruct gfx9_surf_flags {
128b8e80941Smrg    uint16_t                    swizzle_mode; /* tile mode */
129b8e80941Smrg    uint16_t                    epitch; /* (pitch - 1) or (height - 1) */
130b8e80941Smrg};
131b8e80941Smrg
132b8e80941Smrgstruct gfx9_surf_meta_flags {
133b8e80941Smrg    unsigned                    rb_aligned:1;   /* optimal for RBs */
134b8e80941Smrg    unsigned                    pipe_aligned:1; /* optimal for TC */
135b8e80941Smrg};
136b8e80941Smrg
137b8e80941Smrgstruct gfx9_surf_layout {
138b8e80941Smrg    struct gfx9_surf_flags      surf;    /* color or depth surface */
139b8e80941Smrg    struct gfx9_surf_flags      fmask;   /* not added to surf_size */
140b8e80941Smrg    struct gfx9_surf_flags      stencil; /* added to surf_size, use stencil_offset */
141b8e80941Smrg
142b8e80941Smrg    struct gfx9_surf_meta_flags dcc;   /* metadata of color */
143b8e80941Smrg    struct gfx9_surf_meta_flags htile; /* metadata of depth and stencil */
144b8e80941Smrg    struct gfx9_surf_meta_flags cmask; /* metadata of fmask */
145b8e80941Smrg
146b8e80941Smrg    enum gfx9_resource_type     resource_type; /* 1D, 2D or 3D */
147b8e80941Smrg    uint16_t                    surf_pitch; /* in blocks */
148b8e80941Smrg    uint16_t                    surf_height;
149b8e80941Smrg
150b8e80941Smrg    uint64_t                    surf_offset; /* 0 unless imported with an offset */
151b8e80941Smrg    /* The size of the 2D plane containing all mipmap levels. */
152b8e80941Smrg    uint64_t                    surf_slice_size;
153b8e80941Smrg    /* Mipmap level offset within the slice in bytes. Only valid for LINEAR. */
154b8e80941Smrg    uint32_t                    offset[RADEON_SURF_MAX_LEVELS];
155b8e80941Smrg
156b8e80941Smrg    uint64_t                    stencil_offset; /* separate stencil */
157b8e80941Smrg
158b8e80941Smrg    /* Displayable DCC. This is always rb_aligned=0 and pipe_aligned=0.
159b8e80941Smrg     * The 3D engine doesn't support that layout except for chips with 1 RB.
160b8e80941Smrg     * All other chips must set rb_aligned=1.
161b8e80941Smrg     * A compute shader needs to convert from aligned DCC to unaligned.
162b8e80941Smrg     */
163b8e80941Smrg    uint32_t                    display_dcc_size;
164b8e80941Smrg    uint32_t                    display_dcc_alignment;
165b8e80941Smrg    uint16_t                    display_dcc_pitch_max;  /* (mip chain pitch - 1) */
166b8e80941Smrg    bool                        dcc_retile_use_uint16; /* if all values fit into uint16_t */
167b8e80941Smrg    uint32_t                    dcc_retile_num_elements;
168b8e80941Smrg    uint32_t                    *dcc_retile_map;
169b8e80941Smrg};
170b8e80941Smrg
171b8e80941Smrgstruct radeon_surf {
172b8e80941Smrg    /* Format properties. */
173b8e80941Smrg    unsigned                    blk_w:4;
174b8e80941Smrg    unsigned                    blk_h:4;
175b8e80941Smrg    unsigned                    bpe:5;
176b8e80941Smrg    /* Number of mipmap levels where DCC is enabled starting from level 0.
177b8e80941Smrg     * Non-zero levels may be disabled due to alignment constraints, but not
178b8e80941Smrg     * the first level.
179b8e80941Smrg     */
180b8e80941Smrg    unsigned                    num_dcc_levels:4;
181b8e80941Smrg    unsigned                    is_linear:1;
182b8e80941Smrg    unsigned                    has_stencil:1;
183b8e80941Smrg    /* This might be true even if micro_tile_mode isn't displayable or rotated. */
184b8e80941Smrg    unsigned                    is_displayable:1;
185b8e80941Smrg    /* Displayable, thin, depth, rotated. AKA D,S,Z,R swizzle modes. */
186b8e80941Smrg    unsigned                    micro_tile_mode:3;
187b8e80941Smrg    uint32_t                    flags;
188b8e80941Smrg
189b8e80941Smrg    /* These are return values. Some of them can be set by the caller, but
190b8e80941Smrg     * they will be treated as hints (e.g. bankw, bankh) and might be
191b8e80941Smrg     * changed by the calculator.
192b8e80941Smrg     */
193b8e80941Smrg
194b8e80941Smrg    /* Tile swizzle can be OR'd with low bits of the BASE_256B address.
195b8e80941Smrg     * The value is the same for all mipmap levels. Supported tile modes:
196b8e80941Smrg     * - GFX6: Only macro tiling.
197b8e80941Smrg     * - GFX9: Only *_X and *_T swizzle modes. Level 0 must not be in the mip
198b8e80941Smrg     *   tail.
199b8e80941Smrg     *
200b8e80941Smrg     * Only these surfaces are allowed to set it:
201b8e80941Smrg     * - color (if it doesn't have to be displayable)
202b8e80941Smrg     * - DCC (same tile swizzle as color)
203b8e80941Smrg     * - FMASK
204b8e80941Smrg     * - CMASK if it's TC-compatible or if the gen is GFX9
205b8e80941Smrg     * - depth/stencil if HTILE is not TC-compatible and if the gen is not GFX9
206b8e80941Smrg     */
207b8e80941Smrg    uint8_t                     tile_swizzle;
208b8e80941Smrg    uint8_t                     fmask_tile_swizzle;
209b8e80941Smrg
210b8e80941Smrg    uint64_t                    surf_size;
211b8e80941Smrg    uint64_t                    fmask_size;
212b8e80941Smrg    uint32_t                    surf_alignment;
213b8e80941Smrg    uint32_t                    fmask_alignment;
214b8e80941Smrg
215b8e80941Smrg    /* DCC and HTILE are very small. */
216b8e80941Smrg    uint32_t                    dcc_size;
217b8e80941Smrg    uint32_t                    dcc_alignment;
218b8e80941Smrg
219b8e80941Smrg    uint32_t                    htile_size;
220b8e80941Smrg    uint32_t                    htile_slice_size;
221b8e80941Smrg    uint32_t                    htile_alignment;
222b8e80941Smrg
223b8e80941Smrg    uint32_t                    cmask_size;
224b8e80941Smrg    uint32_t                    cmask_alignment;
225b8e80941Smrg
226b8e80941Smrg    union {
227b8e80941Smrg        /* R600-VI return values.
228b8e80941Smrg         *
229b8e80941Smrg         * Some of them can be set by the caller if certain parameters are
230b8e80941Smrg         * desirable. The allocator will try to obey them.
231b8e80941Smrg         */
232b8e80941Smrg        struct legacy_surf_layout legacy;
233b8e80941Smrg
234b8e80941Smrg        /* GFX9+ return values. */
235b8e80941Smrg        struct gfx9_surf_layout gfx9;
236b8e80941Smrg    } u;
237b8e80941Smrg};
238b8e80941Smrg
239b8e80941Smrgstruct ac_surf_info {
240b8e80941Smrg	uint32_t width;
241b8e80941Smrg	uint32_t height;
242b8e80941Smrg	uint32_t depth;
243b8e80941Smrg	uint8_t samples; /* For Z/S: samples; For color: FMASK coverage samples */
244b8e80941Smrg	uint8_t storage_samples; /* For color: allocated samples */
245b8e80941Smrg	uint8_t levels;
246b8e80941Smrg	uint8_t num_channels; /* heuristic for displayability */
247b8e80941Smrg	uint16_t array_size;
248b8e80941Smrg	uint32_t *surf_index; /* Set a monotonic counter for tile swizzling. */
249b8e80941Smrg	uint32_t *fmask_surf_index;
250b8e80941Smrg};
251b8e80941Smrg
252b8e80941Smrgstruct ac_surf_config {
253b8e80941Smrg	struct ac_surf_info info;
254b8e80941Smrg	unsigned is_3d : 1;
255b8e80941Smrg	unsigned is_cube : 1;
256b8e80941Smrg};
257b8e80941Smrg
258b8e80941SmrgADDR_HANDLE amdgpu_addr_create(const struct radeon_info *info,
259b8e80941Smrg			       const struct amdgpu_gpu_info *amdinfo,
260b8e80941Smrg			       uint64_t *max_alignment);
261b8e80941Smrg
262b8e80941Smrgint ac_compute_surface(ADDR_HANDLE addrlib, const struct radeon_info *info,
263b8e80941Smrg		       const struct ac_surf_config * config,
264b8e80941Smrg		       enum radeon_surf_mode mode,
265b8e80941Smrg		       struct radeon_surf *surf);
266b8e80941Smrg
267b8e80941Smrgvoid ac_compute_cmask(const struct radeon_info *info,
268b8e80941Smrg		      const struct ac_surf_config *config,
269b8e80941Smrg		      struct radeon_surf *surf);
270b8e80941Smrg
271b8e80941Smrg#ifdef __cplusplus
272b8e80941Smrg}
273b8e80941Smrg#endif
274b8e80941Smrg
275b8e80941Smrg#endif /* AC_SURFACE_H */
276