2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
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:
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
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. */
24 #ifndef RADEON_WINSYS_H
25 #define RADEON_WINSYS_H
27 /* The public winsys interface header for the radeon driver. */
29 /* R300 features in DRM.
33 * - GB_Z_PEQ_CONFIG on rv350->r4xx
34 * - R500 FG_ALPHA_VALUE
37 * - R500 US_FORMAT regs
38 * - R500 ARGB2101010 colorbuffer
43 #include "pipebuffer/pb_buffer.h"
45 #define RADEON_MAX_CMDBUF_DWORDS (16 * 1024)
47 #define RADEON_FLUSH_ASYNC (1 << 0)
48 #define RADEON_FLUSH_KEEP_TILING_FLAGS (1 << 1) /* needs DRM 2.12.0 */
49 #define RADEON_FLUSH_COMPUTE (1 << 2)
50 #define RADEON_FLUSH_END_OF_FRAME (1 << 3)
53 enum radeon_bo_layout
{
54 RADEON_LAYOUT_LINEAR
= 0,
56 RADEON_LAYOUT_SQUARETILED
,
61 enum radeon_bo_domain
{ /* bitfield */
62 RADEON_DOMAIN_GTT
= 2,
63 RADEON_DOMAIN_VRAM
= 4,
64 RADEON_DOMAIN_VRAM_GTT
= RADEON_DOMAIN_VRAM
| RADEON_DOMAIN_GTT
67 enum radeon_bo_flag
{ /* bitfield */
68 RADEON_FLAG_GTT_WC
= (1 << 0),
69 RADEON_FLAG_CPU_ACCESS
= (1 << 1),
70 RADEON_FLAG_NO_CPU_ACCESS
= (1 << 2),
73 enum radeon_bo_usage
{ /* bitfield */
74 RADEON_USAGE_READ
= 2,
75 RADEON_USAGE_WRITE
= 4,
76 RADEON_USAGE_READWRITE
= RADEON_USAGE_READ
| RADEON_USAGE_WRITE
81 CHIP_R300
, /* R3xx-based cores. */
89 CHIP_R420
, /* R4xx-based cores. */
98 CHIP_RV515
, /* R5xx-based cores. */
163 enum radeon_value_id
{
164 RADEON_REQUESTED_VRAM_MEMORY
,
165 RADEON_REQUESTED_GTT_MEMORY
,
166 RADEON_BUFFER_WAIT_TIME_NS
,
168 RADEON_NUM_CS_FLUSHES
,
169 RADEON_NUM_BYTES_MOVED
,
172 RADEON_GPU_TEMPERATURE
, /* DRM 2.42.0 */
175 RADEON_GPU_RESET_COUNTER
, /* DRM 2.43.0 */
178 enum radeon_bo_priority
{
180 RADEON_PRIO_SHADER_DATA
, /* shader code, resource descriptors */
181 RADEON_PRIO_SHADER_BUFFER_RO
, /* read-only */
182 RADEON_PRIO_SHADER_TEXTURE_RO
, /* read-only */
183 RADEON_PRIO_SHADER_RESOURCE_RW
, /* buffers, textures, streamout, GS rings, RATs; read/write */
184 RADEON_PRIO_COLOR_BUFFER
,
185 RADEON_PRIO_DEPTH_BUFFER
,
186 RADEON_PRIO_SHADER_TEXTURE_MSAA
,
187 RADEON_PRIO_COLOR_BUFFER_MSAA
,
188 RADEON_PRIO_DEPTH_BUFFER_MSAA
,
189 RADEON_PRIO_COLOR_META
,
190 RADEON_PRIO_DEPTH_META
,
191 RADEON_PRIO_MAX
/* must be <= 15 */
194 struct winsys_handle
;
195 struct radeon_winsys_cs_handle
;
197 struct radeon_winsys_cs
{
198 unsigned cdw
; /* Number of used dwords. */
199 uint32_t *buf
; /* The command buffer. */
200 enum ring_type ring_type
;
205 enum radeon_family family
;
206 enum chip_class chip_class
;
210 uint32_t max_compute_units
;
212 uint32_t max_sh_per_se
;
214 uint32_t drm_major
; /* version */
216 uint32_t drm_patchlevel
;
219 uint32_t vce_fw_version
;
222 uint32_t r300_num_gb_pipes
;
223 uint32_t r300_num_z_pipes
;
225 uint32_t r600_num_backends
;
226 uint32_t r600_clock_crystal_freq
;
227 uint32_t r600_tiling_config
;
228 uint32_t r600_num_tile_pipes
;
229 uint32_t r600_max_pipes
;
230 boolean r600_virtual_address
;
231 boolean r600_has_dma
;
233 uint32_t r600_backend_map
;
234 boolean r600_backend_map_valid
;
236 boolean si_tile_mode_array_valid
;
237 uint32_t si_tile_mode_array
[32];
238 uint32_t si_backend_enabled_mask
;
240 boolean cik_macrotile_mode_array_valid
;
241 uint32_t cik_macrotile_mode_array
[16];
244 enum radeon_feature_id
{
245 RADEON_FID_R300_HYPERZ_ACCESS
, /* ZMask + HiZ */
246 RADEON_FID_R300_CMASK_ACCESS
,
249 #define RADEON_SURF_MAX_LEVEL 32
251 #define RADEON_SURF_TYPE_MASK 0xFF
252 #define RADEON_SURF_TYPE_SHIFT 0
253 #define RADEON_SURF_TYPE_1D 0
254 #define RADEON_SURF_TYPE_2D 1
255 #define RADEON_SURF_TYPE_3D 2
256 #define RADEON_SURF_TYPE_CUBEMAP 3
257 #define RADEON_SURF_TYPE_1D_ARRAY 4
258 #define RADEON_SURF_TYPE_2D_ARRAY 5
259 #define RADEON_SURF_MODE_MASK 0xFF
260 #define RADEON_SURF_MODE_SHIFT 8
261 #define RADEON_SURF_MODE_LINEAR 0
262 #define RADEON_SURF_MODE_LINEAR_ALIGNED 1
263 #define RADEON_SURF_MODE_1D 2
264 #define RADEON_SURF_MODE_2D 3
265 #define RADEON_SURF_SCANOUT (1 << 16)
266 #define RADEON_SURF_ZBUFFER (1 << 17)
267 #define RADEON_SURF_SBUFFER (1 << 18)
268 #define RADEON_SURF_Z_OR_SBUFFER (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)
269 #define RADEON_SURF_HAS_SBUFFER_MIPTREE (1 << 19)
270 #define RADEON_SURF_HAS_TILE_MODE_INDEX (1 << 20)
271 #define RADEON_SURF_FMASK (1 << 21)
273 #define RADEON_SURF_GET(v, field) (((v) >> RADEON_SURF_ ## field ## _SHIFT) & RADEON_SURF_ ## field ## _MASK)
274 #define RADEON_SURF_SET(v, field) (((v) & RADEON_SURF_ ## field ## _MASK) << RADEON_SURF_ ## field ## _SHIFT)
275 #define RADEON_SURF_CLR(v, field) ((v) & ~(RADEON_SURF_ ## field ## _MASK << RADEON_SURF_ ## field ## _SHIFT))
277 struct radeon_surf_level
{
286 uint32_t pitch_bytes
;
291 /* These are inputs to the calculator. */
304 /* These are return values. Some of them can be set by the caller, but
305 * they will be treated as hints (e.g. bankw, bankh) and might be
306 * changed by the calculator.
309 uint64_t bo_alignment
;
310 /* This applies to EG and later. */
315 uint32_t stencil_tile_split
;
316 uint64_t stencil_offset
;
317 struct radeon_surf_level level
[RADEON_SURF_MAX_LEVEL
];
318 struct radeon_surf_level stencil_level
[RADEON_SURF_MAX_LEVEL
];
319 uint32_t tiling_index
[RADEON_SURF_MAX_LEVEL
];
320 uint32_t stencil_tiling_index
[RADEON_SURF_MAX_LEVEL
];
323 struct radeon_winsys
{
325 * The screen object this winsys was created for
327 struct pipe_screen
*screen
;
330 * Decrement the winsys reference count.
332 * \param ws The winsys this function is called for.
333 * \return True if the winsys and screen should be destroyed.
335 bool (*unref
)(struct radeon_winsys
*ws
);
338 * Destroy this winsys.
340 * \param ws The winsys this function is called from.
342 void (*destroy
)(struct radeon_winsys
*ws
);
345 * Query an info structure from winsys.
347 * \param ws The winsys this function is called from.
348 * \param info Return structure
350 void (*query_info
)(struct radeon_winsys
*ws
,
351 struct radeon_info
*info
);
353 /**************************************************************************
354 * Buffer management. Buffer attributes are mostly fixed over its lifetime.
356 * Remember that gallium gets to choose the interface it needs, and the
357 * window systems must then implement that interface (rather than the
358 * other way around...).
359 *************************************************************************/
362 * Create a buffer object.
364 * \param ws The winsys this function is called from.
365 * \param size The size to allocate.
366 * \param alignment An alignment of the buffer in memory.
367 * \param use_reusable_pool Whether the cache buffer manager should be used.
368 * \param domain A bitmask of the RADEON_DOMAIN_* flags.
369 * \return The created buffer object.
371 struct pb_buffer
*(*buffer_create
)(struct radeon_winsys
*ws
,
374 boolean use_reusable_pool
,
375 enum radeon_bo_domain domain
,
376 enum radeon_bo_flag flags
);
378 struct radeon_winsys_cs_handle
*(*buffer_get_cs_handle
)(
379 struct pb_buffer
*buf
);
382 * Map the entire data store of a buffer object into the client's address
385 * \param buf A winsys buffer object to map.
386 * \param cs A command stream to flush if the buffer is referenced by it.
387 * \param usage A bitmask of the PIPE_TRANSFER_* flags.
388 * \return The pointer at the beginning of the buffer.
390 void *(*buffer_map
)(struct radeon_winsys_cs_handle
*buf
,
391 struct radeon_winsys_cs
*cs
,
392 enum pipe_transfer_usage usage
);
395 * Unmap a buffer object from the client's address space.
397 * \param buf A winsys buffer object to unmap.
399 void (*buffer_unmap
)(struct radeon_winsys_cs_handle
*buf
);
402 * Return TRUE if a buffer object is being used by the GPU.
404 * \param buf A winsys buffer object.
405 * \param usage Only check whether the buffer is busy for the given usage.
407 boolean (*buffer_is_busy
)(struct pb_buffer
*buf
,
408 enum radeon_bo_usage usage
);
411 * Wait for a buffer object until it is not used by a GPU. This is
412 * equivalent to a fence placed after the last command using the buffer,
413 * and synchronizing to the fence.
415 * \param buf A winsys buffer object to wait for.
416 * \param usage Only wait until the buffer is idle for the given usage,
417 * but may still be busy for some other usage.
419 void (*buffer_wait
)(struct pb_buffer
*buf
, enum radeon_bo_usage usage
);
422 * Return tiling flags describing a memory layout of a buffer object.
424 * \param buf A winsys buffer object to get the flags from.
425 * \param macrotile A pointer to the return value of the microtile flag.
426 * \param microtile A pointer to the return value of the macrotile flag.
428 * \note microtile and macrotile are not bitmasks!
430 void (*buffer_get_tiling
)(struct pb_buffer
*buf
,
431 enum radeon_bo_layout
*microtile
,
432 enum radeon_bo_layout
*macrotile
,
433 unsigned *bankw
, unsigned *bankh
,
434 unsigned *tile_split
,
435 unsigned *stencil_tile_split
,
440 * Set tiling flags describing a memory layout of a buffer object.
442 * \param buf A winsys buffer object to set the flags for.
443 * \param cs A command stream to flush if the buffer is referenced by it.
444 * \param macrotile A macrotile flag.
445 * \param microtile A microtile flag.
446 * \param stride A stride of the buffer in bytes, for texturing.
448 * \note microtile and macrotile are not bitmasks!
450 void (*buffer_set_tiling
)(struct pb_buffer
*buf
,
451 struct radeon_winsys_cs
*rcs
,
452 enum radeon_bo_layout microtile
,
453 enum radeon_bo_layout macrotile
,
454 unsigned bankw
, unsigned bankh
,
456 unsigned stencil_tile_split
,
462 * Get a winsys buffer from a winsys handle. The internal structure
463 * of the handle is platform-specific and only a winsys should access it.
465 * \param ws The winsys this function is called from.
466 * \param whandle A winsys handle pointer as was received from a state
468 * \param stride The returned buffer stride in bytes.
470 struct pb_buffer
*(*buffer_from_handle
)(struct radeon_winsys
*ws
,
471 struct winsys_handle
*whandle
,
475 * Get a winsys buffer from a user pointer. The resulting buffer can't
476 * be exported. Both pointer and size must be page aligned.
478 * \param ws The winsys this function is called from.
479 * \param pointer User pointer to turn into a buffer object.
480 * \param Size Size in bytes for the new buffer.
482 struct pb_buffer
*(*buffer_from_ptr
)(struct radeon_winsys
*ws
,
483 void *pointer
, unsigned size
);
486 * Get a winsys handle from a winsys buffer. The internal structure
487 * of the handle is platform-specific and only a winsys should access it.
489 * \param buf A winsys buffer object to get the handle from.
490 * \param whandle A winsys handle pointer.
491 * \param stride A stride of the buffer in bytes, for texturing.
492 * \return TRUE on success.
494 boolean (*buffer_get_handle
)(struct pb_buffer
*buf
,
496 struct winsys_handle
*whandle
);
499 * Return the virtual address of a buffer.
501 * \param buf A winsys buffer object
502 * \return virtual address
504 uint64_t (*buffer_get_virtual_address
)(struct radeon_winsys_cs_handle
*buf
);
507 * Query the initial placement of the buffer from the kernel driver.
509 enum radeon_bo_domain (*buffer_get_initial_domain
)(struct radeon_winsys_cs_handle
*buf
);
511 /**************************************************************************
512 * Command submission.
514 * Each pipe context should create its own command stream and submit
515 * commands independently of other contexts.
516 *************************************************************************/
519 * Create a command stream.
521 * \param ws The winsys this function is called from.
522 * \param ring_type The ring type (GFX, DMA, UVD)
523 * \param flush Flush callback function associated with the command stream.
524 * \param user User pointer that will be passed to the flush callback.
525 * \param trace_buf Trace buffer when tracing is enabled
527 struct radeon_winsys_cs
*(*cs_create
)(struct radeon_winsys
*ws
,
528 enum ring_type ring_type
,
529 void (*flush
)(void *ctx
, unsigned flags
,
530 struct pipe_fence_handle
**fence
),
532 struct radeon_winsys_cs_handle
*trace_buf
);
535 * Destroy a command stream.
537 * \param cs A command stream to destroy.
539 void (*cs_destroy
)(struct radeon_winsys_cs
*cs
);
542 * Add a new buffer relocation. Every relocation must first be added
543 * before it can be written.
545 * \param cs A command stream to add buffer for validation against.
546 * \param buf A winsys buffer to validate.
547 * \param usage Whether the buffer is used for read and/or write.
548 * \param domain Bitmask of the RADEON_DOMAIN_* flags.
549 * \param priority A higher number means a greater chance of being
550 * placed in the requested domain. 15 is the maximum.
551 * \return Relocation index.
553 unsigned (*cs_add_reloc
)(struct radeon_winsys_cs
*cs
,
554 struct radeon_winsys_cs_handle
*buf
,
555 enum radeon_bo_usage usage
,
556 enum radeon_bo_domain domain
,
557 enum radeon_bo_priority priority
);
560 * Return the index of an already-added buffer.
562 * \param cs Command stream
564 * \return The buffer index, or -1 if the buffer has not been added.
566 int (*cs_get_reloc
)(struct radeon_winsys_cs
*cs
,
567 struct radeon_winsys_cs_handle
*buf
);
570 * Return TRUE if there is enough memory in VRAM and GTT for the relocs
571 * added so far. If the validation fails, all the relocations which have
572 * been added since the last call of cs_validate will be removed and
573 * the CS will be flushed (provided there are still any relocations).
575 * \param cs A command stream to validate.
577 boolean (*cs_validate
)(struct radeon_winsys_cs
*cs
);
580 * Return TRUE if there is enough memory in VRAM and GTT for the relocs
583 * \param cs A command stream to validate.
584 * \param vram VRAM memory size pending to be use
585 * \param gtt GTT memory size pending to be use
587 boolean (*cs_memory_below_limit
)(struct radeon_winsys_cs
*cs
, uint64_t vram
, uint64_t gtt
);
590 * Flush a command stream.
592 * \param cs A command stream to flush.
593 * \param flags, RADEON_FLUSH_ASYNC or 0.
594 * \param fence Pointer to a fence. If non-NULL, a fence is inserted
595 * after the CS and is returned through this parameter.
596 * \param cs_trace_id A unique identifier of the cs, used for tracing.
598 void (*cs_flush
)(struct radeon_winsys_cs
*cs
,
600 struct pipe_fence_handle
**fence
,
601 uint32_t cs_trace_id
);
604 * Return TRUE if a buffer is referenced by a command stream.
606 * \param cs A command stream.
607 * \param buf A winsys buffer.
609 boolean (*cs_is_buffer_referenced
)(struct radeon_winsys_cs
*cs
,
610 struct radeon_winsys_cs_handle
*buf
,
611 enum radeon_bo_usage usage
);
614 * Request access to a feature for a command stream.
616 * \param cs A command stream.
617 * \param fid Feature ID, one of RADEON_FID_*
618 * \param enable Whether to enable or disable the feature.
620 boolean (*cs_request_feature
)(struct radeon_winsys_cs
*cs
,
621 enum radeon_feature_id fid
,
624 * Make sure all asynchronous flush of the cs have completed
626 * \param cs A command stream.
628 void (*cs_sync_flush
)(struct radeon_winsys_cs
*cs
);
631 * Wait for the fence and return true if the fence has been signalled.
632 * The timeout of 0 will only return the status.
633 * The timeout of PIPE_TIMEOUT_INFINITE will always wait until the fence
636 bool (*fence_wait
)(struct radeon_winsys
*ws
,
637 struct pipe_fence_handle
*fence
,
641 * Reference counting for fences.
643 void (*fence_reference
)(struct pipe_fence_handle
**dst
,
644 struct pipe_fence_handle
*src
);
649 * \param ws The winsys this function is called from.
650 * \param surf Surface structure ptr
652 int (*surface_init
)(struct radeon_winsys
*ws
,
653 struct radeon_surf
*surf
);
656 * Find best values for a surface
658 * \param ws The winsys this function is called from.
659 * \param surf Surface structure ptr
661 int (*surface_best
)(struct radeon_winsys
*ws
,
662 struct radeon_surf
*surf
);
664 uint64_t (*query_value
)(struct radeon_winsys
*ws
,
665 enum radeon_value_id value
);
667 void (*read_registers
)(struct radeon_winsys
*ws
, unsigned reg_offset
,
668 unsigned num_registers
, uint32_t *out
);
672 static INLINE
void radeon_emit(struct radeon_winsys_cs
*cs
, uint32_t value
)
674 cs
->buf
[cs
->cdw
++] = value
;
677 static INLINE
void radeon_emit_array(struct radeon_winsys_cs
*cs
,
678 const uint32_t *values
, unsigned count
)
680 memcpy(cs
->buf
+cs
->cdw
, values
, count
* 4);