1e88f27b3Smrg/* 2e88f27b3Smrg * Copyright © 2011 Red Hat All Rights Reserved. 3e88f27b3Smrg * 4e88f27b3Smrg * Permission is hereby granted, free of charge, to any person obtaining 5e88f27b3Smrg * a copy of this software and associated documentation files (the 6e88f27b3Smrg * "Software"), to deal in the Software without restriction, including 7e88f27b3Smrg * without limitation the rights to use, copy, modify, merge, publish, 8e88f27b3Smrg * distribute, sub license, and/or sell copies of the Software, and to 9e88f27b3Smrg * permit persons to whom the Software is furnished to do so, subject to 10e88f27b3Smrg * the following conditions: 11e88f27b3Smrg * 12e88f27b3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 13e88f27b3Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 14e88f27b3Smrg * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 15e88f27b3Smrg * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS 16e88f27b3Smrg * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17e88f27b3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 18e88f27b3Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 19e88f27b3Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. 20e88f27b3Smrg * 21e88f27b3Smrg * The above copyright notice and this permission notice (including the 22e88f27b3Smrg * next paragraph) shall be included in all copies or substantial portions 23e88f27b3Smrg * of the Software. 24e88f27b3Smrg */ 25e88f27b3Smrg/* 26e88f27b3Smrg * Authors: 27e88f27b3Smrg * Jérôme Glisse <jglisse@redhat.com> 28e88f27b3Smrg */ 29e88f27b3Smrg#include <stdbool.h> 30e88f27b3Smrg#include <assert.h> 31e88f27b3Smrg#include <errno.h> 32e88f27b3Smrg#include <stdio.h> 33e88f27b3Smrg#include <stdlib.h> 34e88f27b3Smrg#include <string.h> 35e88f27b3Smrg#include <sys/ioctl.h> 36e88f27b3Smrg#include "drm.h" 37424e9256Smrg#include "libdrm_macros.h" 38e88f27b3Smrg#include "xf86drm.h" 39e88f27b3Smrg#include "radeon_drm.h" 40e88f27b3Smrg#include "radeon_surface.h" 41e88f27b3Smrg 422ee35494Smrg#define CIK_TILE_MODE_COLOR_2D 14 432ee35494Smrg#define CIK_TILE_MODE_COLOR_2D_SCANOUT 10 442ee35494Smrg#define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_64 0 452ee35494Smrg#define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_128 1 462ee35494Smrg#define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_256 2 472ee35494Smrg#define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_512 3 482ee35494Smrg#define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_ROW_SIZE 4 492ee35494Smrg 50e88f27b3Smrg#define ALIGN(value, alignment) (((value) + alignment - 1) & ~(alignment - 1)) 51e88f27b3Smrg#define MAX2(A, B) ((A) > (B) ? (A) : (B)) 52e88f27b3Smrg#define MIN2(A, B) ((A) < (B) ? (A) : (B)) 53e88f27b3Smrg 54e88f27b3Smrg/* keep this private */ 55e88f27b3Smrgenum radeon_family { 56e88f27b3Smrg CHIP_UNKNOWN, 57e88f27b3Smrg CHIP_R600, 58e88f27b3Smrg CHIP_RV610, 59e88f27b3Smrg CHIP_RV630, 60e88f27b3Smrg CHIP_RV670, 61e88f27b3Smrg CHIP_RV620, 62e88f27b3Smrg CHIP_RV635, 63e88f27b3Smrg CHIP_RS780, 64e88f27b3Smrg CHIP_RS880, 65e88f27b3Smrg CHIP_RV770, 66e88f27b3Smrg CHIP_RV730, 67e88f27b3Smrg CHIP_RV710, 68e88f27b3Smrg CHIP_RV740, 69e88f27b3Smrg CHIP_CEDAR, 70e88f27b3Smrg CHIP_REDWOOD, 71e88f27b3Smrg CHIP_JUNIPER, 72e88f27b3Smrg CHIP_CYPRESS, 73e88f27b3Smrg CHIP_HEMLOCK, 74e88f27b3Smrg CHIP_PALM, 75e88f27b3Smrg CHIP_SUMO, 76e88f27b3Smrg CHIP_SUMO2, 77e88f27b3Smrg CHIP_BARTS, 78e88f27b3Smrg CHIP_TURKS, 79e88f27b3Smrg CHIP_CAICOS, 80e88f27b3Smrg CHIP_CAYMAN, 81e88f27b3Smrg CHIP_ARUBA, 82e88f27b3Smrg CHIP_TAHITI, 83e88f27b3Smrg CHIP_PITCAIRN, 84e88f27b3Smrg CHIP_VERDE, 85e88f27b3Smrg CHIP_OLAND, 86e88f27b3Smrg CHIP_HAINAN, 87e88f27b3Smrg CHIP_BONAIRE, 88e88f27b3Smrg CHIP_KAVERI, 89e88f27b3Smrg CHIP_KABINI, 90e88f27b3Smrg CHIP_HAWAII, 91a7d7de1eSmrg CHIP_MULLINS, 92e88f27b3Smrg CHIP_LAST, 93e88f27b3Smrg}; 94e88f27b3Smrg 95e88f27b3Smrgtypedef int (*hw_init_surface_t)(struct radeon_surface_manager *surf_man, 96e88f27b3Smrg struct radeon_surface *surf); 97e88f27b3Smrgtypedef int (*hw_best_surface_t)(struct radeon_surface_manager *surf_man, 98e88f27b3Smrg struct radeon_surface *surf); 99e88f27b3Smrg 100e88f27b3Smrgstruct radeon_hw_info { 101e88f27b3Smrg /* apply to r6, eg */ 102e88f27b3Smrg uint32_t group_bytes; 103e88f27b3Smrg uint32_t num_banks; 104e88f27b3Smrg uint32_t num_pipes; 105e88f27b3Smrg /* apply to eg */ 106e88f27b3Smrg uint32_t row_size; 107e88f27b3Smrg unsigned allow_2d; 108e88f27b3Smrg /* apply to si */ 109e88f27b3Smrg uint32_t tile_mode_array[32]; 110e88f27b3Smrg /* apply to cik */ 111e88f27b3Smrg uint32_t macrotile_mode_array[16]; 112e88f27b3Smrg}; 113e88f27b3Smrg 114e88f27b3Smrgstruct radeon_surface_manager { 115e88f27b3Smrg int fd; 116e88f27b3Smrg uint32_t device_id; 117e88f27b3Smrg struct radeon_hw_info hw_info; 118e88f27b3Smrg unsigned family; 119e88f27b3Smrg hw_init_surface_t surface_init; 120e88f27b3Smrg hw_best_surface_t surface_best; 121e88f27b3Smrg}; 122e88f27b3Smrg 123e88f27b3Smrg/* helper */ 124e88f27b3Smrgstatic int radeon_get_value(int fd, unsigned req, uint32_t *value) 125e88f27b3Smrg{ 126e88f27b3Smrg struct drm_radeon_info info = {}; 127e88f27b3Smrg int r; 128e88f27b3Smrg 129e88f27b3Smrg *value = 0; 130e88f27b3Smrg info.request = req; 131e88f27b3Smrg info.value = (uintptr_t)value; 132e88f27b3Smrg r = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, 133e88f27b3Smrg sizeof(struct drm_radeon_info)); 134e88f27b3Smrg return r; 135e88f27b3Smrg} 136e88f27b3Smrg 137e88f27b3Smrgstatic int radeon_get_family(struct radeon_surface_manager *surf_man) 138e88f27b3Smrg{ 139e88f27b3Smrg switch (surf_man->device_id) { 140e88f27b3Smrg#define CHIPSET(pci_id, name, fam) case pci_id: surf_man->family = CHIP_##fam; break; 141e88f27b3Smrg#include "r600_pci_ids.h" 142e88f27b3Smrg#undef CHIPSET 143e88f27b3Smrg default: 144e88f27b3Smrg return -EINVAL; 145e88f27b3Smrg } 146e88f27b3Smrg return 0; 147e88f27b3Smrg} 148e88f27b3Smrg 149e88f27b3Smrgstatic unsigned next_power_of_two(unsigned x) 150e88f27b3Smrg{ 151e88f27b3Smrg if (x <= 1) 152e88f27b3Smrg return 1; 153e88f27b3Smrg 154e88f27b3Smrg return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1))); 155e88f27b3Smrg} 156e88f27b3Smrg 157e88f27b3Smrgstatic unsigned mip_minify(unsigned size, unsigned level) 158e88f27b3Smrg{ 159e88f27b3Smrg unsigned val; 160e88f27b3Smrg 161e88f27b3Smrg val = MAX2(1, size >> level); 162e88f27b3Smrg if (level > 0) 163e88f27b3Smrg val = next_power_of_two(val); 164e88f27b3Smrg return val; 165e88f27b3Smrg} 166e88f27b3Smrg 167e88f27b3Smrgstatic void surf_minify(struct radeon_surface *surf, 168e88f27b3Smrg struct radeon_surface_level *surflevel, 169e88f27b3Smrg unsigned bpe, unsigned level, 170e88f27b3Smrg uint32_t xalign, uint32_t yalign, uint32_t zalign, 171fe517fc9Smrg uint64_t offset) 172e88f27b3Smrg{ 173e88f27b3Smrg surflevel->npix_x = mip_minify(surf->npix_x, level); 174e88f27b3Smrg surflevel->npix_y = mip_minify(surf->npix_y, level); 175e88f27b3Smrg surflevel->npix_z = mip_minify(surf->npix_z, level); 176e88f27b3Smrg surflevel->nblk_x = (surflevel->npix_x + surf->blk_w - 1) / surf->blk_w; 177e88f27b3Smrg surflevel->nblk_y = (surflevel->npix_y + surf->blk_h - 1) / surf->blk_h; 178e88f27b3Smrg surflevel->nblk_z = (surflevel->npix_z + surf->blk_d - 1) / surf->blk_d; 179e88f27b3Smrg if (surf->nsamples == 1 && surflevel->mode == RADEON_SURF_MODE_2D && 180e88f27b3Smrg !(surf->flags & RADEON_SURF_FMASK)) { 181e88f27b3Smrg if (surflevel->nblk_x < xalign || surflevel->nblk_y < yalign) { 182e88f27b3Smrg surflevel->mode = RADEON_SURF_MODE_1D; 183e88f27b3Smrg return; 184e88f27b3Smrg } 185e88f27b3Smrg } 186e88f27b3Smrg surflevel->nblk_x = ALIGN(surflevel->nblk_x, xalign); 187e88f27b3Smrg surflevel->nblk_y = ALIGN(surflevel->nblk_y, yalign); 188e88f27b3Smrg surflevel->nblk_z = ALIGN(surflevel->nblk_z, zalign); 189e88f27b3Smrg 190e88f27b3Smrg surflevel->offset = offset; 191e88f27b3Smrg surflevel->pitch_bytes = surflevel->nblk_x * bpe * surf->nsamples; 192fe517fc9Smrg surflevel->slice_size = (uint64_t)surflevel->pitch_bytes * surflevel->nblk_y; 193e88f27b3Smrg 194e88f27b3Smrg surf->bo_size = offset + surflevel->slice_size * surflevel->nblk_z * surf->array_size; 195e88f27b3Smrg} 196e88f27b3Smrg 197e88f27b3Smrg/* =========================================================================== 198e88f27b3Smrg * r600/r700 family 199e88f27b3Smrg */ 200e88f27b3Smrgstatic int r6_init_hw_info(struct radeon_surface_manager *surf_man) 201e88f27b3Smrg{ 202e88f27b3Smrg uint32_t tiling_config; 203e88f27b3Smrg drmVersionPtr version; 204e88f27b3Smrg int r; 205e88f27b3Smrg 206e88f27b3Smrg r = radeon_get_value(surf_man->fd, RADEON_INFO_TILING_CONFIG, 207e88f27b3Smrg &tiling_config); 208e88f27b3Smrg if (r) { 209e88f27b3Smrg return r; 210e88f27b3Smrg } 211e88f27b3Smrg 212e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 213e88f27b3Smrg version = drmGetVersion(surf_man->fd); 214e88f27b3Smrg if (version && version->version_minor >= 14) { 215e88f27b3Smrg surf_man->hw_info.allow_2d = 1; 216e88f27b3Smrg } 217e88f27b3Smrg drmFreeVersion(version); 218e88f27b3Smrg 219e88f27b3Smrg switch ((tiling_config & 0xe) >> 1) { 220e88f27b3Smrg case 0: 221e88f27b3Smrg surf_man->hw_info.num_pipes = 1; 222e88f27b3Smrg break; 223e88f27b3Smrg case 1: 224e88f27b3Smrg surf_man->hw_info.num_pipes = 2; 225e88f27b3Smrg break; 226e88f27b3Smrg case 2: 227e88f27b3Smrg surf_man->hw_info.num_pipes = 4; 228e88f27b3Smrg break; 229e88f27b3Smrg case 3: 230e88f27b3Smrg surf_man->hw_info.num_pipes = 8; 231e88f27b3Smrg break; 232e88f27b3Smrg default: 233e88f27b3Smrg surf_man->hw_info.num_pipes = 8; 234e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 235e88f27b3Smrg break; 236e88f27b3Smrg } 237e88f27b3Smrg 238e88f27b3Smrg switch ((tiling_config & 0x30) >> 4) { 239e88f27b3Smrg case 0: 240e88f27b3Smrg surf_man->hw_info.num_banks = 4; 241e88f27b3Smrg break; 242e88f27b3Smrg case 1: 243e88f27b3Smrg surf_man->hw_info.num_banks = 8; 244e88f27b3Smrg break; 245e88f27b3Smrg default: 246e88f27b3Smrg surf_man->hw_info.num_banks = 8; 247e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 248e88f27b3Smrg break; 249e88f27b3Smrg } 250e88f27b3Smrg 251e88f27b3Smrg switch ((tiling_config & 0xc0) >> 6) { 252e88f27b3Smrg case 0: 253e88f27b3Smrg surf_man->hw_info.group_bytes = 256; 254e88f27b3Smrg break; 255e88f27b3Smrg case 1: 256e88f27b3Smrg surf_man->hw_info.group_bytes = 512; 257e88f27b3Smrg break; 258e88f27b3Smrg default: 259e88f27b3Smrg surf_man->hw_info.group_bytes = 256; 260e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 261e88f27b3Smrg break; 262e88f27b3Smrg } 263e88f27b3Smrg return 0; 264e88f27b3Smrg} 265e88f27b3Smrg 266e88f27b3Smrgstatic int r6_surface_init_linear(struct radeon_surface_manager *surf_man, 267e88f27b3Smrg struct radeon_surface *surf, 268e88f27b3Smrg uint64_t offset, unsigned start_level) 269e88f27b3Smrg{ 270e88f27b3Smrg uint32_t xalign, yalign, zalign; 271e88f27b3Smrg unsigned i; 272e88f27b3Smrg 273e88f27b3Smrg /* compute alignment */ 274e88f27b3Smrg if (!start_level) { 275e88f27b3Smrg surf->bo_alignment = MAX2(256, surf_man->hw_info.group_bytes); 276e88f27b3Smrg } 277e88f27b3Smrg /* the 32 alignment is for scanout, cb or db but to allow texture to be 278e88f27b3Smrg * easily bound as such we force this alignment to all surface 279e88f27b3Smrg */ 280e88f27b3Smrg xalign = MAX2(1, surf_man->hw_info.group_bytes / surf->bpe); 281e88f27b3Smrg yalign = 1; 282e88f27b3Smrg zalign = 1; 283e88f27b3Smrg if (surf->flags & RADEON_SURF_SCANOUT) { 284e88f27b3Smrg xalign = MAX2((surf->bpe == 1) ? 64 : 32, xalign); 285e88f27b3Smrg } 286e88f27b3Smrg 287e88f27b3Smrg /* build mipmap tree */ 288e88f27b3Smrg for (i = start_level; i <= surf->last_level; i++) { 289e88f27b3Smrg surf->level[i].mode = RADEON_SURF_MODE_LINEAR; 290e88f27b3Smrg surf_minify(surf, surf->level+i, surf->bpe, i, xalign, yalign, zalign, offset); 291e88f27b3Smrg /* level0 and first mipmap need to have alignment */ 292e88f27b3Smrg offset = surf->bo_size; 293a884aba1Smrg if (i == 0) { 294e88f27b3Smrg offset = ALIGN(offset, surf->bo_alignment); 295e88f27b3Smrg } 296e88f27b3Smrg } 297e88f27b3Smrg return 0; 298e88f27b3Smrg} 299e88f27b3Smrg 300e88f27b3Smrgstatic int r6_surface_init_linear_aligned(struct radeon_surface_manager *surf_man, 301e88f27b3Smrg struct radeon_surface *surf, 302e88f27b3Smrg uint64_t offset, unsigned start_level) 303e88f27b3Smrg{ 304e88f27b3Smrg uint32_t xalign, yalign, zalign; 305e88f27b3Smrg unsigned i; 306e88f27b3Smrg 307e88f27b3Smrg /* compute alignment */ 308e88f27b3Smrg if (!start_level) { 309e88f27b3Smrg surf->bo_alignment = MAX2(256, surf_man->hw_info.group_bytes); 310e88f27b3Smrg } 311e88f27b3Smrg xalign = MAX2(64, surf_man->hw_info.group_bytes / surf->bpe); 312e88f27b3Smrg yalign = 1; 313e88f27b3Smrg zalign = 1; 314e88f27b3Smrg 315e88f27b3Smrg /* build mipmap tree */ 316e88f27b3Smrg for (i = start_level; i <= surf->last_level; i++) { 317e88f27b3Smrg surf->level[i].mode = RADEON_SURF_MODE_LINEAR_ALIGNED; 318e88f27b3Smrg surf_minify(surf, surf->level+i, surf->bpe, i, xalign, yalign, zalign, offset); 319e88f27b3Smrg /* level0 and first mipmap need to have alignment */ 320e88f27b3Smrg offset = surf->bo_size; 321a884aba1Smrg if (i == 0) { 322e88f27b3Smrg offset = ALIGN(offset, surf->bo_alignment); 323e88f27b3Smrg } 324e88f27b3Smrg } 325e88f27b3Smrg return 0; 326e88f27b3Smrg} 327e88f27b3Smrg 328e88f27b3Smrgstatic int r6_surface_init_1d(struct radeon_surface_manager *surf_man, 329e88f27b3Smrg struct radeon_surface *surf, 330e88f27b3Smrg uint64_t offset, unsigned start_level) 331e88f27b3Smrg{ 332e88f27b3Smrg uint32_t xalign, yalign, zalign, tilew; 333e88f27b3Smrg unsigned i; 334e88f27b3Smrg 335e88f27b3Smrg /* compute alignment */ 336e88f27b3Smrg tilew = 8; 337e88f27b3Smrg xalign = surf_man->hw_info.group_bytes / (tilew * surf->bpe * surf->nsamples); 338e88f27b3Smrg xalign = MAX2(tilew, xalign); 339e88f27b3Smrg yalign = tilew; 340e88f27b3Smrg zalign = 1; 341e88f27b3Smrg if (surf->flags & RADEON_SURF_SCANOUT) { 342e88f27b3Smrg xalign = MAX2((surf->bpe == 1) ? 64 : 32, xalign); 343e88f27b3Smrg } 344e88f27b3Smrg if (!start_level) { 345e88f27b3Smrg surf->bo_alignment = MAX2(256, surf_man->hw_info.group_bytes); 346e88f27b3Smrg } 347e88f27b3Smrg 348e88f27b3Smrg /* build mipmap tree */ 349e88f27b3Smrg for (i = start_level; i <= surf->last_level; i++) { 350e88f27b3Smrg surf->level[i].mode = RADEON_SURF_MODE_1D; 351e88f27b3Smrg surf_minify(surf, surf->level+i, surf->bpe, i, xalign, yalign, zalign, offset); 352e88f27b3Smrg /* level0 and first mipmap need to have alignment */ 353e88f27b3Smrg offset = surf->bo_size; 354a884aba1Smrg if (i == 0) { 355e88f27b3Smrg offset = ALIGN(offset, surf->bo_alignment); 356e88f27b3Smrg } 357e88f27b3Smrg } 358e88f27b3Smrg return 0; 359e88f27b3Smrg} 360e88f27b3Smrg 361e88f27b3Smrgstatic int r6_surface_init_2d(struct radeon_surface_manager *surf_man, 362e88f27b3Smrg struct radeon_surface *surf, 363e88f27b3Smrg uint64_t offset, unsigned start_level) 364e88f27b3Smrg{ 365e88f27b3Smrg uint32_t xalign, yalign, zalign, tilew; 366e88f27b3Smrg unsigned i; 367e88f27b3Smrg 368e88f27b3Smrg /* compute alignment */ 369e88f27b3Smrg tilew = 8; 370e88f27b3Smrg zalign = 1; 371e88f27b3Smrg xalign = (surf_man->hw_info.group_bytes * surf_man->hw_info.num_banks) / 372e88f27b3Smrg (tilew * surf->bpe * surf->nsamples); 373e88f27b3Smrg xalign = MAX2(tilew * surf_man->hw_info.num_banks, xalign); 3743c748557Ssnj if (surf->flags & RADEON_SURF_FMASK) 3753c748557Ssnj xalign = MAX2(128, xalign); 376e88f27b3Smrg yalign = tilew * surf_man->hw_info.num_pipes; 377e88f27b3Smrg if (surf->flags & RADEON_SURF_SCANOUT) { 378e88f27b3Smrg xalign = MAX2((surf->bpe == 1) ? 64 : 32, xalign); 379e88f27b3Smrg } 380e88f27b3Smrg if (!start_level) { 381e88f27b3Smrg surf->bo_alignment = 382e88f27b3Smrg MAX2(surf_man->hw_info.num_pipes * 383e88f27b3Smrg surf_man->hw_info.num_banks * 384e88f27b3Smrg surf->nsamples * surf->bpe * 64, 385e88f27b3Smrg xalign * yalign * surf->nsamples * surf->bpe); 386e88f27b3Smrg } 387e88f27b3Smrg 388e88f27b3Smrg /* build mipmap tree */ 389e88f27b3Smrg for (i = start_level; i <= surf->last_level; i++) { 390e88f27b3Smrg surf->level[i].mode = RADEON_SURF_MODE_2D; 391e88f27b3Smrg surf_minify(surf, surf->level+i, surf->bpe, i, xalign, yalign, zalign, offset); 392e88f27b3Smrg if (surf->level[i].mode == RADEON_SURF_MODE_1D) { 393e88f27b3Smrg return r6_surface_init_1d(surf_man, surf, offset, i); 394e88f27b3Smrg } 395e88f27b3Smrg /* level0 and first mipmap need to have alignment */ 396e88f27b3Smrg offset = surf->bo_size; 397a884aba1Smrg if (i == 0) { 398e88f27b3Smrg offset = ALIGN(offset, surf->bo_alignment); 399e88f27b3Smrg } 400e88f27b3Smrg } 401e88f27b3Smrg return 0; 402e88f27b3Smrg} 403e88f27b3Smrg 404e88f27b3Smrgstatic int r6_surface_init(struct radeon_surface_manager *surf_man, 405e88f27b3Smrg struct radeon_surface *surf) 406e88f27b3Smrg{ 407e88f27b3Smrg unsigned mode; 408e88f27b3Smrg int r; 409e88f27b3Smrg 410e88f27b3Smrg /* MSAA surfaces support the 2D mode only. */ 411e88f27b3Smrg if (surf->nsamples > 1) { 412e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 413e88f27b3Smrg surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE); 414e88f27b3Smrg } 415e88f27b3Smrg 416e88f27b3Smrg /* tiling mode */ 417e88f27b3Smrg mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK; 418e88f27b3Smrg 419e88f27b3Smrg if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)) { 420e88f27b3Smrg /* zbuffer only support 1D or 2D tiled surface */ 421e88f27b3Smrg switch (mode) { 422e88f27b3Smrg case RADEON_SURF_MODE_1D: 423e88f27b3Smrg case RADEON_SURF_MODE_2D: 424e88f27b3Smrg break; 425e88f27b3Smrg default: 426e88f27b3Smrg mode = RADEON_SURF_MODE_1D; 427e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 428e88f27b3Smrg surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE); 429e88f27b3Smrg break; 430e88f27b3Smrg } 431e88f27b3Smrg } 432e88f27b3Smrg 433e88f27b3Smrg /* force 1d on kernel that can't do 2d */ 434e88f27b3Smrg if (!surf_man->hw_info.allow_2d && mode > RADEON_SURF_MODE_1D) { 435e88f27b3Smrg if (surf->nsamples > 1) { 436e88f27b3Smrg fprintf(stderr, "radeon: Cannot use 2D tiling for an MSAA surface (%i).\n", __LINE__); 437e88f27b3Smrg return -EFAULT; 438e88f27b3Smrg } 439e88f27b3Smrg mode = RADEON_SURF_MODE_1D; 440e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 441e88f27b3Smrg surf->flags |= RADEON_SURF_SET(mode, MODE); 442e88f27b3Smrg } 443e88f27b3Smrg 444e88f27b3Smrg /* check surface dimension */ 445e88f27b3Smrg if (surf->npix_x > 8192 || surf->npix_y > 8192 || surf->npix_z > 8192) { 446e88f27b3Smrg return -EINVAL; 447e88f27b3Smrg } 448e88f27b3Smrg 449e88f27b3Smrg /* check mipmap last_level */ 450e88f27b3Smrg if (surf->last_level > 14) { 451e88f27b3Smrg return -EINVAL; 452e88f27b3Smrg } 453e88f27b3Smrg 454e88f27b3Smrg /* check tiling mode */ 455e88f27b3Smrg switch (mode) { 456e88f27b3Smrg case RADEON_SURF_MODE_LINEAR: 457e88f27b3Smrg r = r6_surface_init_linear(surf_man, surf, 0, 0); 458e88f27b3Smrg break; 459e88f27b3Smrg case RADEON_SURF_MODE_LINEAR_ALIGNED: 460e88f27b3Smrg r = r6_surface_init_linear_aligned(surf_man, surf, 0, 0); 461e88f27b3Smrg break; 462e88f27b3Smrg case RADEON_SURF_MODE_1D: 463e88f27b3Smrg r = r6_surface_init_1d(surf_man, surf, 0, 0); 464e88f27b3Smrg break; 465e88f27b3Smrg case RADEON_SURF_MODE_2D: 466e88f27b3Smrg r = r6_surface_init_2d(surf_man, surf, 0, 0); 467e88f27b3Smrg break; 468e88f27b3Smrg default: 469e88f27b3Smrg return -EINVAL; 470e88f27b3Smrg } 471e88f27b3Smrg return r; 472e88f27b3Smrg} 473e88f27b3Smrg 474e88f27b3Smrgstatic int r6_surface_best(struct radeon_surface_manager *surf_man, 475e88f27b3Smrg struct radeon_surface *surf) 476e88f27b3Smrg{ 477e88f27b3Smrg /* no value to optimize for r6xx/r7xx */ 478e88f27b3Smrg return 0; 479e88f27b3Smrg} 480e88f27b3Smrg 481e88f27b3Smrg 482e88f27b3Smrg/* =========================================================================== 483e88f27b3Smrg * evergreen family 484e88f27b3Smrg */ 485e88f27b3Smrgstatic int eg_init_hw_info(struct radeon_surface_manager *surf_man) 486e88f27b3Smrg{ 487e88f27b3Smrg uint32_t tiling_config; 488e88f27b3Smrg drmVersionPtr version; 489e88f27b3Smrg int r; 490e88f27b3Smrg 491e88f27b3Smrg r = radeon_get_value(surf_man->fd, RADEON_INFO_TILING_CONFIG, 492e88f27b3Smrg &tiling_config); 493e88f27b3Smrg if (r) { 494e88f27b3Smrg return r; 495e88f27b3Smrg } 496e88f27b3Smrg 497e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 498e88f27b3Smrg version = drmGetVersion(surf_man->fd); 499e88f27b3Smrg if (version && version->version_minor >= 16) { 500e88f27b3Smrg surf_man->hw_info.allow_2d = 1; 501e88f27b3Smrg } 502e88f27b3Smrg drmFreeVersion(version); 503e88f27b3Smrg 504e88f27b3Smrg switch (tiling_config & 0xf) { 505e88f27b3Smrg case 0: 506e88f27b3Smrg surf_man->hw_info.num_pipes = 1; 507e88f27b3Smrg break; 508e88f27b3Smrg case 1: 509e88f27b3Smrg surf_man->hw_info.num_pipes = 2; 510e88f27b3Smrg break; 511e88f27b3Smrg case 2: 512e88f27b3Smrg surf_man->hw_info.num_pipes = 4; 513e88f27b3Smrg break; 514e88f27b3Smrg case 3: 515e88f27b3Smrg surf_man->hw_info.num_pipes = 8; 516e88f27b3Smrg break; 517e88f27b3Smrg default: 518e88f27b3Smrg surf_man->hw_info.num_pipes = 8; 519e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 520e88f27b3Smrg break; 521e88f27b3Smrg } 522e88f27b3Smrg 523e88f27b3Smrg switch ((tiling_config & 0xf0) >> 4) { 524e88f27b3Smrg case 0: 525e88f27b3Smrg surf_man->hw_info.num_banks = 4; 526e88f27b3Smrg break; 527e88f27b3Smrg case 1: 528e88f27b3Smrg surf_man->hw_info.num_banks = 8; 529e88f27b3Smrg break; 530e88f27b3Smrg case 2: 531e88f27b3Smrg surf_man->hw_info.num_banks = 16; 532e88f27b3Smrg break; 533e88f27b3Smrg default: 534e88f27b3Smrg surf_man->hw_info.num_banks = 8; 535e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 536e88f27b3Smrg break; 537e88f27b3Smrg } 538e88f27b3Smrg 539e88f27b3Smrg switch ((tiling_config & 0xf00) >> 8) { 540e88f27b3Smrg case 0: 541e88f27b3Smrg surf_man->hw_info.group_bytes = 256; 542e88f27b3Smrg break; 543e88f27b3Smrg case 1: 544e88f27b3Smrg surf_man->hw_info.group_bytes = 512; 545e88f27b3Smrg break; 546e88f27b3Smrg default: 547e88f27b3Smrg surf_man->hw_info.group_bytes = 256; 548e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 549e88f27b3Smrg break; 550e88f27b3Smrg } 551e88f27b3Smrg 552e88f27b3Smrg switch ((tiling_config & 0xf000) >> 12) { 553e88f27b3Smrg case 0: 554e88f27b3Smrg surf_man->hw_info.row_size = 1024; 555e88f27b3Smrg break; 556e88f27b3Smrg case 1: 557e88f27b3Smrg surf_man->hw_info.row_size = 2048; 558e88f27b3Smrg break; 559e88f27b3Smrg case 2: 560e88f27b3Smrg surf_man->hw_info.row_size = 4096; 561e88f27b3Smrg break; 562e88f27b3Smrg default: 563e88f27b3Smrg surf_man->hw_info.row_size = 4096; 564e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 565e88f27b3Smrg break; 566e88f27b3Smrg } 567e88f27b3Smrg return 0; 568e88f27b3Smrg} 569e88f27b3Smrg 570e88f27b3Smrgstatic void eg_surf_minify(struct radeon_surface *surf, 571e88f27b3Smrg struct radeon_surface_level *surflevel, 572e88f27b3Smrg unsigned bpe, 573e88f27b3Smrg unsigned level, 574e88f27b3Smrg unsigned slice_pt, 575e88f27b3Smrg unsigned mtilew, 576e88f27b3Smrg unsigned mtileh, 577e88f27b3Smrg unsigned mtileb, 578fe517fc9Smrg uint64_t offset) 579e88f27b3Smrg{ 580e88f27b3Smrg unsigned mtile_pr, mtile_ps; 581e88f27b3Smrg 582e88f27b3Smrg surflevel->npix_x = mip_minify(surf->npix_x, level); 583e88f27b3Smrg surflevel->npix_y = mip_minify(surf->npix_y, level); 584e88f27b3Smrg surflevel->npix_z = mip_minify(surf->npix_z, level); 585e88f27b3Smrg surflevel->nblk_x = (surflevel->npix_x + surf->blk_w - 1) / surf->blk_w; 586e88f27b3Smrg surflevel->nblk_y = (surflevel->npix_y + surf->blk_h - 1) / surf->blk_h; 587e88f27b3Smrg surflevel->nblk_z = (surflevel->npix_z + surf->blk_d - 1) / surf->blk_d; 588e88f27b3Smrg if (surf->nsamples == 1 && surflevel->mode == RADEON_SURF_MODE_2D && 589e88f27b3Smrg !(surf->flags & RADEON_SURF_FMASK)) { 590e88f27b3Smrg if (surflevel->nblk_x < mtilew || surflevel->nblk_y < mtileh) { 591e88f27b3Smrg surflevel->mode = RADEON_SURF_MODE_1D; 592e88f27b3Smrg return; 593e88f27b3Smrg } 594e88f27b3Smrg } 595e88f27b3Smrg surflevel->nblk_x = ALIGN(surflevel->nblk_x, mtilew); 596e88f27b3Smrg surflevel->nblk_y = ALIGN(surflevel->nblk_y, mtileh); 597e88f27b3Smrg surflevel->nblk_z = ALIGN(surflevel->nblk_z, 1); 598e88f27b3Smrg 599e88f27b3Smrg /* macro tile per row */ 600e88f27b3Smrg mtile_pr = surflevel->nblk_x / mtilew; 601e88f27b3Smrg /* macro tile per slice */ 602e88f27b3Smrg mtile_ps = (mtile_pr * surflevel->nblk_y) / mtileh; 603e88f27b3Smrg 604e88f27b3Smrg surflevel->offset = offset; 6053c748557Ssnj surflevel->pitch_bytes = surflevel->nblk_x * bpe * surf->nsamples; 606fe517fc9Smrg surflevel->slice_size = (uint64_t)mtile_ps * mtileb * slice_pt; 607e88f27b3Smrg 608e88f27b3Smrg surf->bo_size = offset + surflevel->slice_size * surflevel->nblk_z * surf->array_size; 609e88f27b3Smrg} 610e88f27b3Smrg 611e88f27b3Smrgstatic int eg_surface_init_1d(struct radeon_surface_manager *surf_man, 612e88f27b3Smrg struct radeon_surface *surf, 613e88f27b3Smrg struct radeon_surface_level *level, 614e88f27b3Smrg unsigned bpe, 615e88f27b3Smrg uint64_t offset, unsigned start_level) 616e88f27b3Smrg{ 617e88f27b3Smrg uint32_t xalign, yalign, zalign, tilew; 618e88f27b3Smrg unsigned i; 619e88f27b3Smrg 620e88f27b3Smrg /* compute alignment */ 621e88f27b3Smrg tilew = 8; 622e88f27b3Smrg xalign = surf_man->hw_info.group_bytes / (tilew * bpe * surf->nsamples); 623e88f27b3Smrg xalign = MAX2(tilew, xalign); 624e88f27b3Smrg yalign = tilew; 625e88f27b3Smrg zalign = 1; 626e88f27b3Smrg if (surf->flags & RADEON_SURF_SCANOUT) { 627e88f27b3Smrg xalign = MAX2((bpe == 1) ? 64 : 32, xalign); 628e88f27b3Smrg } 629e88f27b3Smrg 630e88f27b3Smrg if (!start_level) { 631e88f27b3Smrg unsigned alignment = MAX2(256, surf_man->hw_info.group_bytes); 632e88f27b3Smrg surf->bo_alignment = MAX2(surf->bo_alignment, alignment); 633e88f27b3Smrg 634e88f27b3Smrg if (offset) { 635e88f27b3Smrg offset = ALIGN(offset, alignment); 636e88f27b3Smrg } 637e88f27b3Smrg } 638e88f27b3Smrg 639e88f27b3Smrg /* build mipmap tree */ 640e88f27b3Smrg for (i = start_level; i <= surf->last_level; i++) { 641e88f27b3Smrg level[i].mode = RADEON_SURF_MODE_1D; 642e88f27b3Smrg surf_minify(surf, level+i, bpe, i, xalign, yalign, zalign, offset); 643e88f27b3Smrg /* level0 and first mipmap need to have alignment */ 644e88f27b3Smrg offset = surf->bo_size; 645a884aba1Smrg if (i == 0) { 646e88f27b3Smrg offset = ALIGN(offset, surf->bo_alignment); 647e88f27b3Smrg } 648e88f27b3Smrg } 649e88f27b3Smrg return 0; 650e88f27b3Smrg} 651e88f27b3Smrg 652e88f27b3Smrgstatic int eg_surface_init_2d(struct radeon_surface_manager *surf_man, 653e88f27b3Smrg struct radeon_surface *surf, 654e88f27b3Smrg struct radeon_surface_level *level, 655e88f27b3Smrg unsigned bpe, unsigned tile_split, 656e88f27b3Smrg uint64_t offset, unsigned start_level) 657e88f27b3Smrg{ 658e88f27b3Smrg unsigned tilew, tileh, tileb; 659e88f27b3Smrg unsigned mtilew, mtileh, mtileb; 660e88f27b3Smrg unsigned slice_pt; 661e88f27b3Smrg unsigned i; 662e88f27b3Smrg 663e88f27b3Smrg /* compute tile values */ 664e88f27b3Smrg tilew = 8; 665e88f27b3Smrg tileh = 8; 666e88f27b3Smrg tileb = tilew * tileh * bpe * surf->nsamples; 667e88f27b3Smrg /* slices per tile */ 668e88f27b3Smrg slice_pt = 1; 669e88f27b3Smrg if (tileb > tile_split && tile_split) { 670e88f27b3Smrg slice_pt = tileb / tile_split; 671e88f27b3Smrg } 672e88f27b3Smrg tileb = tileb / slice_pt; 673e88f27b3Smrg 674e88f27b3Smrg /* macro tile width & height */ 675e88f27b3Smrg mtilew = (tilew * surf->bankw * surf_man->hw_info.num_pipes) * surf->mtilea; 676e88f27b3Smrg mtileh = (tileh * surf->bankh * surf_man->hw_info.num_banks) / surf->mtilea; 677e88f27b3Smrg /* macro tile bytes */ 678e88f27b3Smrg mtileb = (mtilew / tilew) * (mtileh / tileh) * tileb; 679e88f27b3Smrg 680e88f27b3Smrg if (!start_level) { 681e88f27b3Smrg unsigned alignment = MAX2(256, mtileb); 682e88f27b3Smrg surf->bo_alignment = MAX2(surf->bo_alignment, alignment); 683e88f27b3Smrg 684e88f27b3Smrg if (offset) { 685e88f27b3Smrg offset = ALIGN(offset, alignment); 686e88f27b3Smrg } 687e88f27b3Smrg } 688e88f27b3Smrg 689e88f27b3Smrg /* build mipmap tree */ 690e88f27b3Smrg for (i = start_level; i <= surf->last_level; i++) { 691e88f27b3Smrg level[i].mode = RADEON_SURF_MODE_2D; 692e88f27b3Smrg eg_surf_minify(surf, level+i, bpe, i, slice_pt, mtilew, mtileh, mtileb, offset); 693e88f27b3Smrg if (level[i].mode == RADEON_SURF_MODE_1D) { 694e88f27b3Smrg return eg_surface_init_1d(surf_man, surf, level, bpe, offset, i); 695e88f27b3Smrg } 696e88f27b3Smrg /* level0 and first mipmap need to have alignment */ 697e88f27b3Smrg offset = surf->bo_size; 698a884aba1Smrg if (i == 0) { 699e88f27b3Smrg offset = ALIGN(offset, surf->bo_alignment); 700e88f27b3Smrg } 701e88f27b3Smrg } 702e88f27b3Smrg return 0; 703e88f27b3Smrg} 704e88f27b3Smrg 705e88f27b3Smrgstatic int eg_surface_sanity(struct radeon_surface_manager *surf_man, 706e88f27b3Smrg struct radeon_surface *surf, 707e88f27b3Smrg unsigned mode) 708e88f27b3Smrg{ 709e88f27b3Smrg unsigned tileb; 710e88f27b3Smrg 711e88f27b3Smrg /* check surface dimension */ 712e88f27b3Smrg if (surf->npix_x > 16384 || surf->npix_y > 16384 || surf->npix_z > 16384) { 713e88f27b3Smrg return -EINVAL; 714e88f27b3Smrg } 715e88f27b3Smrg 716e88f27b3Smrg /* check mipmap last_level */ 717e88f27b3Smrg if (surf->last_level > 15) { 718e88f27b3Smrg return -EINVAL; 719e88f27b3Smrg } 720e88f27b3Smrg 721e88f27b3Smrg /* force 1d on kernel that can't do 2d */ 722e88f27b3Smrg if (!surf_man->hw_info.allow_2d && mode > RADEON_SURF_MODE_1D) { 723e88f27b3Smrg if (surf->nsamples > 1) { 724e88f27b3Smrg fprintf(stderr, "radeon: Cannot use 2D tiling for an MSAA surface (%i).\n", __LINE__); 725e88f27b3Smrg return -EFAULT; 726e88f27b3Smrg } 727e88f27b3Smrg mode = RADEON_SURF_MODE_1D; 728e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 729e88f27b3Smrg surf->flags |= RADEON_SURF_SET(mode, MODE); 730e88f27b3Smrg } 731e88f27b3Smrg 732e88f27b3Smrg /* check tile split */ 733e88f27b3Smrg if (mode == RADEON_SURF_MODE_2D) { 734e88f27b3Smrg switch (surf->tile_split) { 735e88f27b3Smrg case 64: 736e88f27b3Smrg case 128: 737e88f27b3Smrg case 256: 738e88f27b3Smrg case 512: 739e88f27b3Smrg case 1024: 740e88f27b3Smrg case 2048: 741e88f27b3Smrg case 4096: 742e88f27b3Smrg break; 743e88f27b3Smrg default: 744e88f27b3Smrg return -EINVAL; 745e88f27b3Smrg } 746e88f27b3Smrg switch (surf->mtilea) { 747e88f27b3Smrg case 1: 748e88f27b3Smrg case 2: 749e88f27b3Smrg case 4: 750e88f27b3Smrg case 8: 751e88f27b3Smrg break; 752e88f27b3Smrg default: 753e88f27b3Smrg return -EINVAL; 754e88f27b3Smrg } 755e88f27b3Smrg /* check aspect ratio */ 756e88f27b3Smrg if (surf_man->hw_info.num_banks < surf->mtilea) { 757e88f27b3Smrg return -EINVAL; 758e88f27b3Smrg } 759e88f27b3Smrg /* check bank width */ 760e88f27b3Smrg switch (surf->bankw) { 761e88f27b3Smrg case 1: 762e88f27b3Smrg case 2: 763e88f27b3Smrg case 4: 764e88f27b3Smrg case 8: 765e88f27b3Smrg break; 766e88f27b3Smrg default: 767e88f27b3Smrg return -EINVAL; 768e88f27b3Smrg } 769e88f27b3Smrg /* check bank height */ 770e88f27b3Smrg switch (surf->bankh) { 771e88f27b3Smrg case 1: 772e88f27b3Smrg case 2: 773e88f27b3Smrg case 4: 774e88f27b3Smrg case 8: 775e88f27b3Smrg break; 776e88f27b3Smrg default: 777e88f27b3Smrg return -EINVAL; 778e88f27b3Smrg } 779e88f27b3Smrg tileb = MIN2(surf->tile_split, 64 * surf->bpe * surf->nsamples); 780e88f27b3Smrg if ((tileb * surf->bankh * surf->bankw) < surf_man->hw_info.group_bytes) { 781e88f27b3Smrg return -EINVAL; 782e88f27b3Smrg } 783e88f27b3Smrg } 784e88f27b3Smrg 785e88f27b3Smrg return 0; 786e88f27b3Smrg} 787e88f27b3Smrg 788e88f27b3Smrgstatic int eg_surface_init_1d_miptrees(struct radeon_surface_manager *surf_man, 789e88f27b3Smrg struct radeon_surface *surf) 790e88f27b3Smrg{ 791e88f27b3Smrg unsigned zs_flags = RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER; 792e88f27b3Smrg int r, is_depth_stencil = (surf->flags & zs_flags) == zs_flags; 793424e9256Smrg /* Old libdrm_macros.headers didn't have stencil_level in it. This prevents crashes. */ 794e88f27b3Smrg struct radeon_surface_level tmp[RADEON_SURF_MAX_LEVEL]; 795e88f27b3Smrg struct radeon_surface_level *stencil_level = 796e88f27b3Smrg (surf->flags & RADEON_SURF_HAS_SBUFFER_MIPTREE) ? surf->stencil_level : tmp; 797e88f27b3Smrg 798e88f27b3Smrg r = eg_surface_init_1d(surf_man, surf, surf->level, surf->bpe, 0, 0); 799e88f27b3Smrg if (r) 800e88f27b3Smrg return r; 801e88f27b3Smrg 802e88f27b3Smrg if (is_depth_stencil) { 803e88f27b3Smrg r = eg_surface_init_1d(surf_man, surf, stencil_level, 1, 804e88f27b3Smrg surf->bo_size, 0); 805e88f27b3Smrg surf->stencil_offset = stencil_level[0].offset; 806e88f27b3Smrg } 807e88f27b3Smrg return r; 808e88f27b3Smrg} 809e88f27b3Smrg 810e88f27b3Smrgstatic int eg_surface_init_2d_miptrees(struct radeon_surface_manager *surf_man, 811e88f27b3Smrg struct radeon_surface *surf) 812e88f27b3Smrg{ 813e88f27b3Smrg unsigned zs_flags = RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER; 814e88f27b3Smrg int r, is_depth_stencil = (surf->flags & zs_flags) == zs_flags; 815424e9256Smrg /* Old libdrm_macros.headers didn't have stencil_level in it. This prevents crashes. */ 816e88f27b3Smrg struct radeon_surface_level tmp[RADEON_SURF_MAX_LEVEL]; 817e88f27b3Smrg struct radeon_surface_level *stencil_level = 818e88f27b3Smrg (surf->flags & RADEON_SURF_HAS_SBUFFER_MIPTREE) ? surf->stencil_level : tmp; 819e88f27b3Smrg 820e88f27b3Smrg r = eg_surface_init_2d(surf_man, surf, surf->level, surf->bpe, 821e88f27b3Smrg surf->tile_split, 0, 0); 822e88f27b3Smrg if (r) 823e88f27b3Smrg return r; 824e88f27b3Smrg 825e88f27b3Smrg if (is_depth_stencil) { 826e88f27b3Smrg r = eg_surface_init_2d(surf_man, surf, stencil_level, 1, 827e88f27b3Smrg surf->stencil_tile_split, surf->bo_size, 0); 828e88f27b3Smrg surf->stencil_offset = stencil_level[0].offset; 829e88f27b3Smrg } 830e88f27b3Smrg return r; 831e88f27b3Smrg} 832e88f27b3Smrg 833e88f27b3Smrgstatic int eg_surface_init(struct radeon_surface_manager *surf_man, 834e88f27b3Smrg struct radeon_surface *surf) 835e88f27b3Smrg{ 836e88f27b3Smrg unsigned mode; 837e88f27b3Smrg int r; 838e88f27b3Smrg 839e88f27b3Smrg /* MSAA surfaces support the 2D mode only. */ 840e88f27b3Smrg if (surf->nsamples > 1) { 841e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 842e88f27b3Smrg surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE); 843e88f27b3Smrg } 844e88f27b3Smrg 845e88f27b3Smrg /* tiling mode */ 846e88f27b3Smrg mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK; 847e88f27b3Smrg 848e88f27b3Smrg if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)) { 849e88f27b3Smrg /* zbuffer only support 1D or 2D tiled surface */ 850e88f27b3Smrg switch (mode) { 851e88f27b3Smrg case RADEON_SURF_MODE_1D: 852e88f27b3Smrg case RADEON_SURF_MODE_2D: 853e88f27b3Smrg break; 854e88f27b3Smrg default: 855e88f27b3Smrg mode = RADEON_SURF_MODE_1D; 856e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 857e88f27b3Smrg surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE); 858e88f27b3Smrg break; 859e88f27b3Smrg } 860e88f27b3Smrg } 861e88f27b3Smrg 862e88f27b3Smrg r = eg_surface_sanity(surf_man, surf, mode); 863e88f27b3Smrg if (r) { 864e88f27b3Smrg return r; 865e88f27b3Smrg } 866e88f27b3Smrg 867e88f27b3Smrg surf->stencil_offset = 0; 868e88f27b3Smrg surf->bo_alignment = 0; 869e88f27b3Smrg 870e88f27b3Smrg /* check tiling mode */ 871e88f27b3Smrg switch (mode) { 872e88f27b3Smrg case RADEON_SURF_MODE_LINEAR: 873e88f27b3Smrg r = r6_surface_init_linear(surf_man, surf, 0, 0); 874e88f27b3Smrg break; 875e88f27b3Smrg case RADEON_SURF_MODE_LINEAR_ALIGNED: 876e88f27b3Smrg r = r6_surface_init_linear_aligned(surf_man, surf, 0, 0); 877e88f27b3Smrg break; 878e88f27b3Smrg case RADEON_SURF_MODE_1D: 879e88f27b3Smrg r = eg_surface_init_1d_miptrees(surf_man, surf); 880e88f27b3Smrg break; 881e88f27b3Smrg case RADEON_SURF_MODE_2D: 882e88f27b3Smrg r = eg_surface_init_2d_miptrees(surf_man, surf); 883e88f27b3Smrg break; 884e88f27b3Smrg default: 885e88f27b3Smrg return -EINVAL; 886e88f27b3Smrg } 887e88f27b3Smrg return r; 888e88f27b3Smrg} 889e88f27b3Smrg 890e88f27b3Smrgstatic unsigned log2_int(unsigned x) 891e88f27b3Smrg{ 892e88f27b3Smrg unsigned l; 893e88f27b3Smrg 894e88f27b3Smrg if (x < 2) { 895e88f27b3Smrg return 0; 896e88f27b3Smrg } 897e88f27b3Smrg for (l = 2; ; l++) { 898e88f27b3Smrg if ((unsigned)(1 << l) > x) { 899e88f27b3Smrg return l - 1; 900e88f27b3Smrg } 901e88f27b3Smrg } 902e88f27b3Smrg return 0; 903e88f27b3Smrg} 904e88f27b3Smrg 905e88f27b3Smrg/* compute best tile_split, bankw, bankh, mtilea 906e88f27b3Smrg * depending on surface 907e88f27b3Smrg */ 908e88f27b3Smrgstatic int eg_surface_best(struct radeon_surface_manager *surf_man, 909e88f27b3Smrg struct radeon_surface *surf) 910e88f27b3Smrg{ 911e88f27b3Smrg unsigned mode, tileb, h_over_w; 912e88f27b3Smrg int r; 913e88f27b3Smrg 914e88f27b3Smrg /* tiling mode */ 915e88f27b3Smrg mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK; 916e88f27b3Smrg 917e88f27b3Smrg /* set some default value to avoid sanity check choking on them */ 918e88f27b3Smrg surf->tile_split = 1024; 919e88f27b3Smrg surf->bankw = 1; 920e88f27b3Smrg surf->bankh = 1; 921e88f27b3Smrg surf->mtilea = surf_man->hw_info.num_banks; 922e88f27b3Smrg tileb = MIN2(surf->tile_split, 64 * surf->bpe * surf->nsamples); 923e88f27b3Smrg for (; surf->bankh <= 8; surf->bankh *= 2) { 924e88f27b3Smrg if ((tileb * surf->bankh * surf->bankw) >= surf_man->hw_info.group_bytes) { 925e88f27b3Smrg break; 926e88f27b3Smrg } 927e88f27b3Smrg } 928e88f27b3Smrg if (surf->mtilea > 8) { 929e88f27b3Smrg surf->mtilea = 8; 930e88f27b3Smrg } 931e88f27b3Smrg 932e88f27b3Smrg r = eg_surface_sanity(surf_man, surf, mode); 933e88f27b3Smrg if (r) { 934e88f27b3Smrg return r; 935e88f27b3Smrg } 936e88f27b3Smrg 937e88f27b3Smrg if (mode != RADEON_SURF_MODE_2D) { 938e88f27b3Smrg /* nothing to do for non 2D tiled surface */ 939e88f27b3Smrg return 0; 940e88f27b3Smrg } 941e88f27b3Smrg 942e88f27b3Smrg /* Tweak TILE_SPLIT for performance here. */ 943e88f27b3Smrg if (surf->nsamples > 1) { 944e88f27b3Smrg if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)) { 945e88f27b3Smrg switch (surf->nsamples) { 946e88f27b3Smrg case 2: 947e88f27b3Smrg surf->tile_split = 128; 948e88f27b3Smrg break; 949e88f27b3Smrg case 4: 950e88f27b3Smrg surf->tile_split = 128; 951e88f27b3Smrg break; 952e88f27b3Smrg case 8: 953e88f27b3Smrg surf->tile_split = 256; 954e88f27b3Smrg break; 955e88f27b3Smrg case 16: /* cayman only */ 956e88f27b3Smrg surf->tile_split = 512; 957e88f27b3Smrg break; 958e88f27b3Smrg default: 959e88f27b3Smrg fprintf(stderr, "radeon: Wrong number of samples %i (%i)\n", 960e88f27b3Smrg surf->nsamples, __LINE__); 961e88f27b3Smrg return -EINVAL; 962e88f27b3Smrg } 963e88f27b3Smrg surf->stencil_tile_split = 64; 964e88f27b3Smrg } else { 965fe517fc9Smrg /* tile split must be >= 256 for colorbuffer surfaces, 966fe517fc9Smrg * SAMPLE_SPLIT = tile_split / (bpe * 64), the optimal value is 2 967fe517fc9Smrg */ 968fe517fc9Smrg surf->tile_split = MAX2(2 * surf->bpe * 64, 256); 969e88f27b3Smrg if (surf->tile_split > 4096) 970e88f27b3Smrg surf->tile_split = 4096; 971e88f27b3Smrg } 972e88f27b3Smrg } else { 973e88f27b3Smrg /* set tile split to row size */ 974e88f27b3Smrg surf->tile_split = surf_man->hw_info.row_size; 975e88f27b3Smrg surf->stencil_tile_split = surf_man->hw_info.row_size / 2; 976e88f27b3Smrg } 977e88f27b3Smrg 978e88f27b3Smrg /* bankw or bankh greater than 1 increase alignment requirement, not 979e88f27b3Smrg * sure if it's worth using smaller bankw & bankh to stick with 2D 980e88f27b3Smrg * tiling on small surface rather than falling back to 1D tiling. 9812ee35494Smrg * Use recommended value based on tile size for now. 982e88f27b3Smrg * 983e88f27b3Smrg * fmask buffer has different optimal value figure them out once we 984e88f27b3Smrg * use it. 985e88f27b3Smrg */ 986e88f27b3Smrg if (surf->flags & RADEON_SURF_SBUFFER) { 987e88f27b3Smrg /* assume 1 bytes for stencil, we optimize for stencil as stencil 988e88f27b3Smrg * and depth shares surface values 989e88f27b3Smrg */ 990e88f27b3Smrg tileb = MIN2(surf->tile_split, 64 * surf->nsamples); 991e88f27b3Smrg } else { 992e88f27b3Smrg tileb = MIN2(surf->tile_split, 64 * surf->bpe * surf->nsamples); 993e88f27b3Smrg } 994e88f27b3Smrg 995e88f27b3Smrg /* use bankw of 1 to minimize width alignment, might be interesting to 996e88f27b3Smrg * increase it for large surface 997e88f27b3Smrg */ 998e88f27b3Smrg surf->bankw = 1; 999e88f27b3Smrg switch (tileb) { 1000e88f27b3Smrg case 64: 1001e88f27b3Smrg surf->bankh = 4; 1002e88f27b3Smrg break; 1003e88f27b3Smrg case 128: 1004e88f27b3Smrg case 256: 1005e88f27b3Smrg surf->bankh = 2; 1006e88f27b3Smrg break; 1007e88f27b3Smrg default: 1008e88f27b3Smrg surf->bankh = 1; 1009e88f27b3Smrg break; 1010e88f27b3Smrg } 1011e88f27b3Smrg /* double check the constraint */ 1012e88f27b3Smrg for (; surf->bankh <= 8; surf->bankh *= 2) { 1013e88f27b3Smrg if ((tileb * surf->bankh * surf->bankw) >= surf_man->hw_info.group_bytes) { 1014e88f27b3Smrg break; 1015e88f27b3Smrg } 1016e88f27b3Smrg } 1017e88f27b3Smrg 1018e88f27b3Smrg h_over_w = (((surf->bankh * surf_man->hw_info.num_banks) << 16) / 1019e88f27b3Smrg (surf->bankw * surf_man->hw_info.num_pipes)) >> 16; 1020e88f27b3Smrg surf->mtilea = 1 << (log2_int(h_over_w) >> 1); 1021e88f27b3Smrg 1022e88f27b3Smrg return 0; 1023e88f27b3Smrg} 1024e88f27b3Smrg 1025e88f27b3Smrg 1026e88f27b3Smrg/* =========================================================================== 1027e88f27b3Smrg * Southern Islands family 1028e88f27b3Smrg */ 1029e88f27b3Smrg#define SI__GB_TILE_MODE__PIPE_CONFIG(x) (((x) >> 6) & 0x1f) 1030e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P2 0 1031e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P4_8x16 4 1032e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P4_16x16 5 1033e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P4_16x32 6 1034e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P4_32x32 7 1035e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P8_16x16_8x16 8 1036e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P8_16x32_8x16 9 1037e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_8x16 10 1038e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P8_16x32_16x16 11 1039e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x16 12 1040e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x32 13 1041e88f27b3Smrg#define SI__PIPE_CONFIG__ADDR_SURF_P8_32x64_32x32 14 1042e88f27b3Smrg#define SI__GB_TILE_MODE__TILE_SPLIT(x) (((x) >> 11) & 0x7) 1043e88f27b3Smrg#define SI__TILE_SPLIT__64B 0 1044e88f27b3Smrg#define SI__TILE_SPLIT__128B 1 1045e88f27b3Smrg#define SI__TILE_SPLIT__256B 2 1046e88f27b3Smrg#define SI__TILE_SPLIT__512B 3 1047e88f27b3Smrg#define SI__TILE_SPLIT__1024B 4 1048e88f27b3Smrg#define SI__TILE_SPLIT__2048B 5 1049e88f27b3Smrg#define SI__TILE_SPLIT__4096B 6 1050e88f27b3Smrg#define SI__GB_TILE_MODE__BANK_WIDTH(x) (((x) >> 14) & 0x3) 1051e88f27b3Smrg#define SI__BANK_WIDTH__1 0 1052e88f27b3Smrg#define SI__BANK_WIDTH__2 1 1053e88f27b3Smrg#define SI__BANK_WIDTH__4 2 1054e88f27b3Smrg#define SI__BANK_WIDTH__8 3 1055e88f27b3Smrg#define SI__GB_TILE_MODE__BANK_HEIGHT(x) (((x) >> 16) & 0x3) 1056e88f27b3Smrg#define SI__BANK_HEIGHT__1 0 1057e88f27b3Smrg#define SI__BANK_HEIGHT__2 1 1058e88f27b3Smrg#define SI__BANK_HEIGHT__4 2 1059e88f27b3Smrg#define SI__BANK_HEIGHT__8 3 1060e88f27b3Smrg#define SI__GB_TILE_MODE__MACRO_TILE_ASPECT(x) (((x) >> 18) & 0x3) 1061e88f27b3Smrg#define SI__MACRO_TILE_ASPECT__1 0 1062e88f27b3Smrg#define SI__MACRO_TILE_ASPECT__2 1 1063e88f27b3Smrg#define SI__MACRO_TILE_ASPECT__4 2 1064e88f27b3Smrg#define SI__MACRO_TILE_ASPECT__8 3 1065e88f27b3Smrg#define SI__GB_TILE_MODE__NUM_BANKS(x) (((x) >> 20) & 0x3) 1066e88f27b3Smrg#define SI__NUM_BANKS__2_BANK 0 1067e88f27b3Smrg#define SI__NUM_BANKS__4_BANK 1 1068e88f27b3Smrg#define SI__NUM_BANKS__8_BANK 2 1069e88f27b3Smrg#define SI__NUM_BANKS__16_BANK 3 1070e88f27b3Smrg 1071e88f27b3Smrg 1072e88f27b3Smrgstatic void si_gb_tile_mode(uint32_t gb_tile_mode, 1073e88f27b3Smrg unsigned *num_pipes, 1074e88f27b3Smrg unsigned *num_banks, 1075e88f27b3Smrg uint32_t *macro_tile_aspect, 1076e88f27b3Smrg uint32_t *bank_w, 1077e88f27b3Smrg uint32_t *bank_h, 1078e88f27b3Smrg uint32_t *tile_split) 1079e88f27b3Smrg{ 1080e88f27b3Smrg if (num_pipes) { 1081e88f27b3Smrg switch (SI__GB_TILE_MODE__PIPE_CONFIG(gb_tile_mode)) { 1082e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P2: 1083e88f27b3Smrg default: 1084e88f27b3Smrg *num_pipes = 2; 1085e88f27b3Smrg break; 1086e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P4_8x16: 1087e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P4_16x16: 1088e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P4_16x32: 1089e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P4_32x32: 1090e88f27b3Smrg *num_pipes = 4; 1091e88f27b3Smrg break; 1092e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P8_16x16_8x16: 1093e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P8_16x32_8x16: 1094e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_8x16: 1095e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P8_16x32_16x16: 1096e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x16: 1097e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x32: 1098e88f27b3Smrg case SI__PIPE_CONFIG__ADDR_SURF_P8_32x64_32x32: 1099e88f27b3Smrg *num_pipes = 8; 1100e88f27b3Smrg break; 1101e88f27b3Smrg } 1102e88f27b3Smrg } 1103e88f27b3Smrg if (num_banks) { 1104e88f27b3Smrg switch (SI__GB_TILE_MODE__NUM_BANKS(gb_tile_mode)) { 1105e88f27b3Smrg default: 1106e88f27b3Smrg case SI__NUM_BANKS__2_BANK: 1107e88f27b3Smrg *num_banks = 2; 1108e88f27b3Smrg break; 1109e88f27b3Smrg case SI__NUM_BANKS__4_BANK: 1110e88f27b3Smrg *num_banks = 4; 1111e88f27b3Smrg break; 1112e88f27b3Smrg case SI__NUM_BANKS__8_BANK: 1113e88f27b3Smrg *num_banks = 8; 1114e88f27b3Smrg break; 1115e88f27b3Smrg case SI__NUM_BANKS__16_BANK: 1116e88f27b3Smrg *num_banks = 16; 1117e88f27b3Smrg break; 1118e88f27b3Smrg } 1119e88f27b3Smrg } 1120e88f27b3Smrg if (macro_tile_aspect) { 1121e88f27b3Smrg switch (SI__GB_TILE_MODE__MACRO_TILE_ASPECT(gb_tile_mode)) { 1122e88f27b3Smrg default: 1123e88f27b3Smrg case SI__MACRO_TILE_ASPECT__1: 1124e88f27b3Smrg *macro_tile_aspect = 1; 1125e88f27b3Smrg break; 1126e88f27b3Smrg case SI__MACRO_TILE_ASPECT__2: 1127e88f27b3Smrg *macro_tile_aspect = 2; 1128e88f27b3Smrg break; 1129e88f27b3Smrg case SI__MACRO_TILE_ASPECT__4: 1130e88f27b3Smrg *macro_tile_aspect = 4; 1131e88f27b3Smrg break; 1132e88f27b3Smrg case SI__MACRO_TILE_ASPECT__8: 1133e88f27b3Smrg *macro_tile_aspect = 8; 1134e88f27b3Smrg break; 1135e88f27b3Smrg } 1136e88f27b3Smrg } 1137e88f27b3Smrg if (bank_w) { 1138e88f27b3Smrg switch (SI__GB_TILE_MODE__BANK_WIDTH(gb_tile_mode)) { 1139e88f27b3Smrg default: 1140e88f27b3Smrg case SI__BANK_WIDTH__1: 1141e88f27b3Smrg *bank_w = 1; 1142e88f27b3Smrg break; 1143e88f27b3Smrg case SI__BANK_WIDTH__2: 1144e88f27b3Smrg *bank_w = 2; 1145e88f27b3Smrg break; 1146e88f27b3Smrg case SI__BANK_WIDTH__4: 1147e88f27b3Smrg *bank_w = 4; 1148e88f27b3Smrg break; 1149e88f27b3Smrg case SI__BANK_WIDTH__8: 1150e88f27b3Smrg *bank_w = 8; 1151e88f27b3Smrg break; 1152e88f27b3Smrg } 1153e88f27b3Smrg } 1154e88f27b3Smrg if (bank_h) { 1155e88f27b3Smrg switch (SI__GB_TILE_MODE__BANK_HEIGHT(gb_tile_mode)) { 1156e88f27b3Smrg default: 1157e88f27b3Smrg case SI__BANK_HEIGHT__1: 1158e88f27b3Smrg *bank_h = 1; 1159e88f27b3Smrg break; 1160e88f27b3Smrg case SI__BANK_HEIGHT__2: 1161e88f27b3Smrg *bank_h = 2; 1162e88f27b3Smrg break; 1163e88f27b3Smrg case SI__BANK_HEIGHT__4: 1164e88f27b3Smrg *bank_h = 4; 1165e88f27b3Smrg break; 1166e88f27b3Smrg case SI__BANK_HEIGHT__8: 1167e88f27b3Smrg *bank_h = 8; 1168e88f27b3Smrg break; 1169e88f27b3Smrg } 1170e88f27b3Smrg } 1171e88f27b3Smrg if (tile_split) { 1172e88f27b3Smrg switch (SI__GB_TILE_MODE__TILE_SPLIT(gb_tile_mode)) { 1173e88f27b3Smrg default: 1174e88f27b3Smrg case SI__TILE_SPLIT__64B: 1175e88f27b3Smrg *tile_split = 64; 1176e88f27b3Smrg break; 1177e88f27b3Smrg case SI__TILE_SPLIT__128B: 1178e88f27b3Smrg *tile_split = 128; 1179e88f27b3Smrg break; 1180e88f27b3Smrg case SI__TILE_SPLIT__256B: 1181e88f27b3Smrg *tile_split = 256; 1182e88f27b3Smrg break; 1183e88f27b3Smrg case SI__TILE_SPLIT__512B: 1184e88f27b3Smrg *tile_split = 512; 1185e88f27b3Smrg break; 1186e88f27b3Smrg case SI__TILE_SPLIT__1024B: 1187e88f27b3Smrg *tile_split = 1024; 1188e88f27b3Smrg break; 1189e88f27b3Smrg case SI__TILE_SPLIT__2048B: 1190e88f27b3Smrg *tile_split = 2048; 1191e88f27b3Smrg break; 1192e88f27b3Smrg case SI__TILE_SPLIT__4096B: 1193e88f27b3Smrg *tile_split = 4096; 1194e88f27b3Smrg break; 1195e88f27b3Smrg } 1196e88f27b3Smrg } 1197e88f27b3Smrg} 1198e88f27b3Smrg 1199e88f27b3Smrgstatic int si_init_hw_info(struct radeon_surface_manager *surf_man) 1200e88f27b3Smrg{ 1201e88f27b3Smrg uint32_t tiling_config; 1202e88f27b3Smrg drmVersionPtr version; 1203e88f27b3Smrg int r; 1204e88f27b3Smrg 1205e88f27b3Smrg r = radeon_get_value(surf_man->fd, RADEON_INFO_TILING_CONFIG, 1206e88f27b3Smrg &tiling_config); 1207e88f27b3Smrg if (r) { 1208e88f27b3Smrg return r; 1209e88f27b3Smrg } 1210e88f27b3Smrg 1211e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 1212e88f27b3Smrg version = drmGetVersion(surf_man->fd); 1213e88f27b3Smrg if (version && version->version_minor >= 33) { 1214e88f27b3Smrg if (!radeon_get_value(surf_man->fd, RADEON_INFO_SI_TILE_MODE_ARRAY, surf_man->hw_info.tile_mode_array)) { 1215e88f27b3Smrg surf_man->hw_info.allow_2d = 1; 1216e88f27b3Smrg } 1217e88f27b3Smrg } 1218e88f27b3Smrg drmFreeVersion(version); 1219e88f27b3Smrg 1220e88f27b3Smrg switch (tiling_config & 0xf) { 1221e88f27b3Smrg case 0: 1222e88f27b3Smrg surf_man->hw_info.num_pipes = 1; 1223e88f27b3Smrg break; 1224e88f27b3Smrg case 1: 1225e88f27b3Smrg surf_man->hw_info.num_pipes = 2; 1226e88f27b3Smrg break; 1227e88f27b3Smrg case 2: 1228e88f27b3Smrg surf_man->hw_info.num_pipes = 4; 1229e88f27b3Smrg break; 1230e88f27b3Smrg case 3: 1231e88f27b3Smrg surf_man->hw_info.num_pipes = 8; 1232e88f27b3Smrg break; 1233e88f27b3Smrg default: 1234e88f27b3Smrg surf_man->hw_info.num_pipes = 8; 1235e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 1236e88f27b3Smrg break; 1237e88f27b3Smrg } 1238e88f27b3Smrg 1239e88f27b3Smrg switch ((tiling_config & 0xf0) >> 4) { 1240e88f27b3Smrg case 0: 1241e88f27b3Smrg surf_man->hw_info.num_banks = 4; 1242e88f27b3Smrg break; 1243e88f27b3Smrg case 1: 1244e88f27b3Smrg surf_man->hw_info.num_banks = 8; 1245e88f27b3Smrg break; 1246e88f27b3Smrg case 2: 1247e88f27b3Smrg surf_man->hw_info.num_banks = 16; 1248e88f27b3Smrg break; 1249e88f27b3Smrg default: 1250e88f27b3Smrg surf_man->hw_info.num_banks = 8; 1251e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 1252e88f27b3Smrg break; 1253e88f27b3Smrg } 1254e88f27b3Smrg 1255e88f27b3Smrg switch ((tiling_config & 0xf00) >> 8) { 1256e88f27b3Smrg case 0: 1257e88f27b3Smrg surf_man->hw_info.group_bytes = 256; 1258e88f27b3Smrg break; 1259e88f27b3Smrg case 1: 1260e88f27b3Smrg surf_man->hw_info.group_bytes = 512; 1261e88f27b3Smrg break; 1262e88f27b3Smrg default: 1263e88f27b3Smrg surf_man->hw_info.group_bytes = 256; 1264e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 1265e88f27b3Smrg break; 1266e88f27b3Smrg } 1267e88f27b3Smrg 1268e88f27b3Smrg switch ((tiling_config & 0xf000) >> 12) { 1269e88f27b3Smrg case 0: 1270e88f27b3Smrg surf_man->hw_info.row_size = 1024; 1271e88f27b3Smrg break; 1272e88f27b3Smrg case 1: 1273e88f27b3Smrg surf_man->hw_info.row_size = 2048; 1274e88f27b3Smrg break; 1275e88f27b3Smrg case 2: 1276e88f27b3Smrg surf_man->hw_info.row_size = 4096; 1277e88f27b3Smrg break; 1278e88f27b3Smrg default: 1279e88f27b3Smrg surf_man->hw_info.row_size = 4096; 1280e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 1281e88f27b3Smrg break; 1282e88f27b3Smrg } 1283e88f27b3Smrg return 0; 1284e88f27b3Smrg} 1285e88f27b3Smrg 1286e88f27b3Smrgstatic int si_surface_sanity(struct radeon_surface_manager *surf_man, 1287e88f27b3Smrg struct radeon_surface *surf, 1288e88f27b3Smrg unsigned mode, unsigned *tile_mode, unsigned *stencil_tile_mode) 1289e88f27b3Smrg{ 1290e88f27b3Smrg uint32_t gb_tile_mode; 1291e88f27b3Smrg 1292e88f27b3Smrg /* check surface dimension */ 1293e88f27b3Smrg if (surf->npix_x > 16384 || surf->npix_y > 16384 || surf->npix_z > 16384) { 1294e88f27b3Smrg return -EINVAL; 1295e88f27b3Smrg } 1296e88f27b3Smrg 1297e88f27b3Smrg /* check mipmap last_level */ 1298e88f27b3Smrg if (surf->last_level > 15) { 1299e88f27b3Smrg return -EINVAL; 1300e88f27b3Smrg } 1301e88f27b3Smrg 1302e88f27b3Smrg /* force 1d on kernel that can't do 2d */ 1303e88f27b3Smrg if (mode > RADEON_SURF_MODE_1D && 1304e88f27b3Smrg (!surf_man->hw_info.allow_2d || !(surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX))) { 1305e88f27b3Smrg if (surf->nsamples > 1) { 1306e88f27b3Smrg fprintf(stderr, "radeon: Cannot use 1D tiling for an MSAA surface (%i).\n", __LINE__); 1307e88f27b3Smrg return -EFAULT; 1308e88f27b3Smrg } 1309e88f27b3Smrg mode = RADEON_SURF_MODE_1D; 1310e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 1311e88f27b3Smrg surf->flags |= RADEON_SURF_SET(mode, MODE); 1312e88f27b3Smrg } 1313e88f27b3Smrg 1314e88f27b3Smrg if (surf->nsamples > 1 && mode != RADEON_SURF_MODE_2D) { 1315e88f27b3Smrg return -EINVAL; 1316e88f27b3Smrg } 1317e88f27b3Smrg 1318e88f27b3Smrg if (!surf->tile_split) { 1319e88f27b3Smrg /* default value */ 1320e88f27b3Smrg surf->mtilea = 1; 1321e88f27b3Smrg surf->bankw = 1; 1322a884aba1Smrg surf->bankh = 1; 1323e88f27b3Smrg surf->tile_split = 64; 1324e88f27b3Smrg surf->stencil_tile_split = 64; 1325e88f27b3Smrg } 1326e88f27b3Smrg 1327e88f27b3Smrg switch (mode) { 1328e88f27b3Smrg case RADEON_SURF_MODE_2D: 1329e88f27b3Smrg if (surf->flags & RADEON_SURF_SBUFFER) { 1330e88f27b3Smrg switch (surf->nsamples) { 1331e88f27b3Smrg case 1: 1332e88f27b3Smrg *stencil_tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D; 1333e88f27b3Smrg break; 1334e88f27b3Smrg case 2: 1335e88f27b3Smrg *stencil_tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_2AA; 1336e88f27b3Smrg break; 1337e88f27b3Smrg case 4: 1338e88f27b3Smrg *stencil_tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_4AA; 1339e88f27b3Smrg break; 1340e88f27b3Smrg case 8: 1341e88f27b3Smrg *stencil_tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_8AA; 1342e88f27b3Smrg break; 1343e88f27b3Smrg default: 1344e88f27b3Smrg return -EINVAL; 1345e88f27b3Smrg } 1346e88f27b3Smrg /* retrieve tiling mode value */ 1347e88f27b3Smrg gb_tile_mode = surf_man->hw_info.tile_mode_array[*stencil_tile_mode]; 1348e88f27b3Smrg si_gb_tile_mode(gb_tile_mode, NULL, NULL, NULL, NULL, NULL, &surf->stencil_tile_split); 1349e88f27b3Smrg } 1350e88f27b3Smrg if (surf->flags & RADEON_SURF_ZBUFFER) { 1351e88f27b3Smrg switch (surf->nsamples) { 1352e88f27b3Smrg case 1: 1353e88f27b3Smrg *tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D; 1354e88f27b3Smrg break; 1355e88f27b3Smrg case 2: 1356e88f27b3Smrg *tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_2AA; 1357e88f27b3Smrg break; 1358e88f27b3Smrg case 4: 1359e88f27b3Smrg *tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_4AA; 1360e88f27b3Smrg break; 1361e88f27b3Smrg case 8: 1362e88f27b3Smrg *tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_8AA; 1363e88f27b3Smrg break; 1364e88f27b3Smrg default: 1365e88f27b3Smrg return -EINVAL; 1366e88f27b3Smrg } 1367e88f27b3Smrg } else if (surf->flags & RADEON_SURF_SCANOUT) { 1368e88f27b3Smrg switch (surf->bpe) { 1369e88f27b3Smrg case 2: 1370e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_2D_SCANOUT_16BPP; 1371e88f27b3Smrg break; 1372e88f27b3Smrg case 4: 1373e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_2D_SCANOUT_32BPP; 1374e88f27b3Smrg break; 1375e88f27b3Smrg default: 1376e88f27b3Smrg return -EINVAL; 1377e88f27b3Smrg } 1378e88f27b3Smrg } else { 1379e88f27b3Smrg switch (surf->bpe) { 1380e88f27b3Smrg case 1: 1381e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_2D_8BPP; 1382e88f27b3Smrg break; 1383e88f27b3Smrg case 2: 1384e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_2D_16BPP; 1385e88f27b3Smrg break; 1386e88f27b3Smrg case 4: 1387e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_2D_32BPP; 1388e88f27b3Smrg break; 1389e88f27b3Smrg case 8: 1390e88f27b3Smrg case 16: 1391e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_2D_64BPP; 1392e88f27b3Smrg break; 1393e88f27b3Smrg default: 1394e88f27b3Smrg return -EINVAL; 1395e88f27b3Smrg } 1396e88f27b3Smrg } 1397e88f27b3Smrg /* retrieve tiling mode value */ 1398e88f27b3Smrg gb_tile_mode = surf_man->hw_info.tile_mode_array[*tile_mode]; 1399e88f27b3Smrg si_gb_tile_mode(gb_tile_mode, NULL, NULL, &surf->mtilea, &surf->bankw, &surf->bankh, &surf->tile_split); 1400e88f27b3Smrg break; 1401e88f27b3Smrg case RADEON_SURF_MODE_1D: 1402e88f27b3Smrg if (surf->flags & RADEON_SURF_SBUFFER) { 1403e88f27b3Smrg *stencil_tile_mode = SI_TILE_MODE_DEPTH_STENCIL_1D; 1404e88f27b3Smrg } 1405e88f27b3Smrg if (surf->flags & RADEON_SURF_ZBUFFER) { 1406e88f27b3Smrg *tile_mode = SI_TILE_MODE_DEPTH_STENCIL_1D; 1407e88f27b3Smrg } else if (surf->flags & RADEON_SURF_SCANOUT) { 1408e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_1D_SCANOUT; 1409e88f27b3Smrg } else { 1410e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_1D; 1411e88f27b3Smrg } 1412e88f27b3Smrg break; 1413e88f27b3Smrg case RADEON_SURF_MODE_LINEAR_ALIGNED: 1414e88f27b3Smrg default: 1415e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_LINEAR_ALIGNED; 1416e88f27b3Smrg } 1417e88f27b3Smrg 1418e88f27b3Smrg return 0; 1419e88f27b3Smrg} 1420e88f27b3Smrg 1421e88f27b3Smrgstatic void si_surf_minify(struct radeon_surface *surf, 1422e88f27b3Smrg struct radeon_surface_level *surflevel, 1423e88f27b3Smrg unsigned bpe, unsigned level, 1424e88f27b3Smrg uint32_t xalign, uint32_t yalign, uint32_t zalign, 1425fe517fc9Smrg uint32_t slice_align, uint64_t offset) 1426e88f27b3Smrg{ 1427e88f27b3Smrg if (level == 0) { 1428e88f27b3Smrg surflevel->npix_x = surf->npix_x; 1429e88f27b3Smrg } else { 1430e88f27b3Smrg surflevel->npix_x = mip_minify(next_power_of_two(surf->npix_x), level); 1431e88f27b3Smrg } 1432e88f27b3Smrg surflevel->npix_y = mip_minify(surf->npix_y, level); 1433e88f27b3Smrg surflevel->npix_z = mip_minify(surf->npix_z, level); 1434e88f27b3Smrg 1435e88f27b3Smrg if (level == 0 && surf->last_level > 0) { 1436e88f27b3Smrg surflevel->nblk_x = (next_power_of_two(surflevel->npix_x) + surf->blk_w - 1) / surf->blk_w; 1437e88f27b3Smrg surflevel->nblk_y = (next_power_of_two(surflevel->npix_y) + surf->blk_h - 1) / surf->blk_h; 1438e88f27b3Smrg surflevel->nblk_z = (next_power_of_two(surflevel->npix_z) + surf->blk_d - 1) / surf->blk_d; 1439e88f27b3Smrg } else { 1440e88f27b3Smrg surflevel->nblk_x = (surflevel->npix_x + surf->blk_w - 1) / surf->blk_w; 1441e88f27b3Smrg surflevel->nblk_y = (surflevel->npix_y + surf->blk_h - 1) / surf->blk_h; 1442e88f27b3Smrg surflevel->nblk_z = (surflevel->npix_z + surf->blk_d - 1) / surf->blk_d; 1443e88f27b3Smrg } 1444e88f27b3Smrg 1445e88f27b3Smrg surflevel->nblk_y = ALIGN(surflevel->nblk_y, yalign); 1446e88f27b3Smrg 1447e88f27b3Smrg /* XXX: Texture sampling uses unexpectedly large pitches in some cases, 1448e88f27b3Smrg * these are just guesses for the rules behind those 1449e88f27b3Smrg */ 1450e88f27b3Smrg if (level == 0 && surf->last_level == 0) 1451e88f27b3Smrg /* Non-mipmap pitch padded to slice alignment */ 1452e88f27b3Smrg /* Using just bpe here breaks stencil blitting; surf->bpe works. */ 1453e88f27b3Smrg xalign = MAX2(xalign, slice_align / surf->bpe); 1454e88f27b3Smrg else if (surflevel->mode == RADEON_SURF_MODE_LINEAR_ALIGNED) 1455e88f27b3Smrg /* Small rows evenly distributed across slice */ 1456e88f27b3Smrg xalign = MAX2(xalign, slice_align / bpe / surflevel->nblk_y); 1457e88f27b3Smrg 1458e88f27b3Smrg surflevel->nblk_x = ALIGN(surflevel->nblk_x, xalign); 1459e88f27b3Smrg surflevel->nblk_z = ALIGN(surflevel->nblk_z, zalign); 1460e88f27b3Smrg 1461e88f27b3Smrg surflevel->offset = offset; 1462e88f27b3Smrg surflevel->pitch_bytes = surflevel->nblk_x * bpe * surf->nsamples; 1463fe517fc9Smrg surflevel->slice_size = ALIGN((uint64_t)surflevel->pitch_bytes * surflevel->nblk_y, 1464fe517fc9Smrg (uint64_t)slice_align); 1465e88f27b3Smrg 1466e88f27b3Smrg surf->bo_size = offset + surflevel->slice_size * surflevel->nblk_z * surf->array_size; 1467e88f27b3Smrg} 1468e88f27b3Smrg 1469e88f27b3Smrgstatic void si_surf_minify_2d(struct radeon_surface *surf, 1470e88f27b3Smrg struct radeon_surface_level *surflevel, 1471e88f27b3Smrg unsigned bpe, unsigned level, unsigned slice_pt, 1472e88f27b3Smrg uint32_t xalign, uint32_t yalign, uint32_t zalign, 1473fe517fc9Smrg unsigned mtileb, uint64_t offset) 1474e88f27b3Smrg{ 1475e88f27b3Smrg unsigned mtile_pr, mtile_ps; 1476e88f27b3Smrg 1477e88f27b3Smrg if (level == 0) { 1478e88f27b3Smrg surflevel->npix_x = surf->npix_x; 1479e88f27b3Smrg } else { 1480e88f27b3Smrg surflevel->npix_x = mip_minify(next_power_of_two(surf->npix_x), level); 1481e88f27b3Smrg } 1482e88f27b3Smrg surflevel->npix_y = mip_minify(surf->npix_y, level); 1483e88f27b3Smrg surflevel->npix_z = mip_minify(surf->npix_z, level); 1484e88f27b3Smrg 1485e88f27b3Smrg if (level == 0 && surf->last_level > 0) { 1486e88f27b3Smrg surflevel->nblk_x = (next_power_of_two(surflevel->npix_x) + surf->blk_w - 1) / surf->blk_w; 1487e88f27b3Smrg surflevel->nblk_y = (next_power_of_two(surflevel->npix_y) + surf->blk_h - 1) / surf->blk_h; 1488e88f27b3Smrg surflevel->nblk_z = (next_power_of_two(surflevel->npix_z) + surf->blk_d - 1) / surf->blk_d; 1489e88f27b3Smrg } else { 1490e88f27b3Smrg surflevel->nblk_x = (surflevel->npix_x + surf->blk_w - 1) / surf->blk_w; 1491e88f27b3Smrg surflevel->nblk_y = (surflevel->npix_y + surf->blk_h - 1) / surf->blk_h; 1492e88f27b3Smrg surflevel->nblk_z = (surflevel->npix_z + surf->blk_d - 1) / surf->blk_d; 1493e88f27b3Smrg } 1494e88f27b3Smrg 1495e88f27b3Smrg if (surf->nsamples == 1 && surflevel->mode == RADEON_SURF_MODE_2D && 1496e88f27b3Smrg !(surf->flags & RADEON_SURF_FMASK)) { 1497e88f27b3Smrg if (surflevel->nblk_x < xalign || surflevel->nblk_y < yalign) { 1498e88f27b3Smrg surflevel->mode = RADEON_SURF_MODE_1D; 1499e88f27b3Smrg return; 1500e88f27b3Smrg } 1501e88f27b3Smrg } 1502e88f27b3Smrg surflevel->nblk_x = ALIGN(surflevel->nblk_x, xalign); 1503e88f27b3Smrg surflevel->nblk_y = ALIGN(surflevel->nblk_y, yalign); 1504e88f27b3Smrg surflevel->nblk_z = ALIGN(surflevel->nblk_z, zalign); 1505e88f27b3Smrg 1506e88f27b3Smrg /* macro tile per row */ 1507e88f27b3Smrg mtile_pr = surflevel->nblk_x / xalign; 1508e88f27b3Smrg /* macro tile per slice */ 1509e88f27b3Smrg mtile_ps = (mtile_pr * surflevel->nblk_y) / yalign; 1510e88f27b3Smrg surflevel->offset = offset; 15113c748557Ssnj surflevel->pitch_bytes = surflevel->nblk_x * bpe * surf->nsamples; 1512fe517fc9Smrg surflevel->slice_size = (uint64_t)mtile_ps * mtileb * slice_pt; 1513e88f27b3Smrg 1514e88f27b3Smrg surf->bo_size = offset + surflevel->slice_size * surflevel->nblk_z * surf->array_size; 1515e88f27b3Smrg} 1516e88f27b3Smrg 1517e88f27b3Smrgstatic int si_surface_init_linear_aligned(struct radeon_surface_manager *surf_man, 1518e88f27b3Smrg struct radeon_surface *surf, 1519e88f27b3Smrg unsigned tile_mode, 1520e88f27b3Smrg uint64_t offset, unsigned start_level) 1521e88f27b3Smrg{ 1522e88f27b3Smrg uint32_t xalign, yalign, zalign, slice_align; 1523e88f27b3Smrg unsigned i; 1524e88f27b3Smrg 1525e88f27b3Smrg /* compute alignment */ 1526e88f27b3Smrg if (!start_level) { 1527e88f27b3Smrg surf->bo_alignment = MAX2(256, surf_man->hw_info.group_bytes); 1528e88f27b3Smrg } 1529e88f27b3Smrg xalign = MAX2(8, 64 / surf->bpe); 1530e88f27b3Smrg yalign = 1; 1531e88f27b3Smrg zalign = 1; 1532e88f27b3Smrg slice_align = MAX2(64 * surf->bpe, surf_man->hw_info.group_bytes); 1533e88f27b3Smrg 1534e88f27b3Smrg /* build mipmap tree */ 1535e88f27b3Smrg for (i = start_level; i <= surf->last_level; i++) { 1536e88f27b3Smrg surf->level[i].mode = RADEON_SURF_MODE_LINEAR_ALIGNED; 1537e88f27b3Smrg si_surf_minify(surf, surf->level+i, surf->bpe, i, xalign, yalign, zalign, slice_align, offset); 1538e88f27b3Smrg /* level0 and first mipmap need to have alignment */ 1539e88f27b3Smrg offset = surf->bo_size; 1540a884aba1Smrg if (i == 0) { 1541e88f27b3Smrg offset = ALIGN(offset, surf->bo_alignment); 1542e88f27b3Smrg } 1543e88f27b3Smrg if (surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX) { 1544e88f27b3Smrg surf->tiling_index[i] = tile_mode; 1545e88f27b3Smrg } 1546e88f27b3Smrg } 1547e88f27b3Smrg return 0; 1548e88f27b3Smrg} 1549e88f27b3Smrg 1550e88f27b3Smrgstatic int si_surface_init_1d(struct radeon_surface_manager *surf_man, 1551e88f27b3Smrg struct radeon_surface *surf, 1552e88f27b3Smrg struct radeon_surface_level *level, 1553e88f27b3Smrg unsigned bpe, unsigned tile_mode, 1554e88f27b3Smrg uint64_t offset, unsigned start_level) 1555e88f27b3Smrg{ 1556e88f27b3Smrg uint32_t xalign, yalign, zalign, slice_align; 1557e88f27b3Smrg unsigned alignment = MAX2(256, surf_man->hw_info.group_bytes); 1558e88f27b3Smrg unsigned i; 1559e88f27b3Smrg 1560e88f27b3Smrg /* compute alignment */ 1561e88f27b3Smrg xalign = 8; 1562e88f27b3Smrg yalign = 8; 1563e88f27b3Smrg zalign = 1; 1564e88f27b3Smrg slice_align = surf_man->hw_info.group_bytes; 1565e88f27b3Smrg if (surf->flags & RADEON_SURF_SCANOUT) { 1566e88f27b3Smrg xalign = MAX2((bpe == 1) ? 64 : 32, xalign); 1567e88f27b3Smrg } 1568e88f27b3Smrg 1569e88f27b3Smrg if (start_level <= 1) { 1570e88f27b3Smrg surf->bo_alignment = MAX2(surf->bo_alignment, alignment); 1571e88f27b3Smrg 1572e88f27b3Smrg if (offset) { 1573e88f27b3Smrg offset = ALIGN(offset, alignment); 1574e88f27b3Smrg } 1575e88f27b3Smrg } 1576e88f27b3Smrg 1577e88f27b3Smrg /* build mipmap tree */ 1578e88f27b3Smrg for (i = start_level; i <= surf->last_level; i++) { 1579e88f27b3Smrg level[i].mode = RADEON_SURF_MODE_1D; 1580e88f27b3Smrg si_surf_minify(surf, level+i, bpe, i, xalign, yalign, zalign, slice_align, offset); 1581e88f27b3Smrg /* level0 and first mipmap need to have alignment */ 1582e88f27b3Smrg offset = surf->bo_size; 1583a884aba1Smrg if (i == 0) { 1584e88f27b3Smrg offset = ALIGN(offset, alignment); 1585e88f27b3Smrg } 1586e88f27b3Smrg if (surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX) { 1587e88f27b3Smrg if (surf->level == level) { 1588e88f27b3Smrg surf->tiling_index[i] = tile_mode; 1589e88f27b3Smrg /* it's ok because stencil is done after */ 1590e88f27b3Smrg surf->stencil_tiling_index[i] = tile_mode; 1591e88f27b3Smrg } else { 1592e88f27b3Smrg surf->stencil_tiling_index[i] = tile_mode; 1593e88f27b3Smrg } 1594e88f27b3Smrg } 1595e88f27b3Smrg } 1596e88f27b3Smrg return 0; 1597e88f27b3Smrg} 1598e88f27b3Smrg 1599e88f27b3Smrgstatic int si_surface_init_1d_miptrees(struct radeon_surface_manager *surf_man, 1600e88f27b3Smrg struct radeon_surface *surf, 1601e88f27b3Smrg unsigned tile_mode, unsigned stencil_tile_mode) 1602e88f27b3Smrg{ 1603e88f27b3Smrg int r; 1604e88f27b3Smrg 1605e88f27b3Smrg r = si_surface_init_1d(surf_man, surf, surf->level, surf->bpe, tile_mode, 0, 0); 1606e88f27b3Smrg if (r) { 1607e88f27b3Smrg return r; 1608e88f27b3Smrg } 1609e88f27b3Smrg 1610e88f27b3Smrg if (surf->flags & RADEON_SURF_SBUFFER) { 1611e88f27b3Smrg r = si_surface_init_1d(surf_man, surf, surf->stencil_level, 1, stencil_tile_mode, surf->bo_size, 0); 1612e88f27b3Smrg surf->stencil_offset = surf->stencil_level[0].offset; 1613e88f27b3Smrg } 1614e88f27b3Smrg return r; 1615e88f27b3Smrg} 1616e88f27b3Smrg 1617e88f27b3Smrgstatic int si_surface_init_2d(struct radeon_surface_manager *surf_man, 1618e88f27b3Smrg struct radeon_surface *surf, 1619e88f27b3Smrg struct radeon_surface_level *level, 1620e88f27b3Smrg unsigned bpe, unsigned tile_mode, 1621e88f27b3Smrg unsigned num_pipes, unsigned num_banks, 1622e88f27b3Smrg unsigned tile_split, 1623e88f27b3Smrg uint64_t offset, 1624e88f27b3Smrg unsigned start_level) 1625e88f27b3Smrg{ 1626e88f27b3Smrg uint64_t aligned_offset = offset; 1627e88f27b3Smrg unsigned tilew, tileh, tileb; 1628e88f27b3Smrg unsigned mtilew, mtileh, mtileb; 1629e88f27b3Smrg unsigned slice_pt; 1630e88f27b3Smrg unsigned i; 1631e88f27b3Smrg 1632e88f27b3Smrg /* compute tile values */ 1633e88f27b3Smrg tilew = 8; 1634e88f27b3Smrg tileh = 8; 1635e88f27b3Smrg tileb = tilew * tileh * bpe * surf->nsamples; 1636e88f27b3Smrg /* slices per tile */ 1637e88f27b3Smrg slice_pt = 1; 1638e88f27b3Smrg if (tileb > tile_split && tile_split) { 1639e88f27b3Smrg slice_pt = tileb / tile_split; 1640e88f27b3Smrg } 1641e88f27b3Smrg tileb = tileb / slice_pt; 1642e88f27b3Smrg 1643e88f27b3Smrg /* macro tile width & height */ 1644e88f27b3Smrg mtilew = (tilew * surf->bankw * num_pipes) * surf->mtilea; 1645e88f27b3Smrg mtileh = (tileh * surf->bankh * num_banks) / surf->mtilea; 1646e88f27b3Smrg 1647e88f27b3Smrg /* macro tile bytes */ 1648e88f27b3Smrg mtileb = (mtilew / tilew) * (mtileh / tileh) * tileb; 1649e88f27b3Smrg 1650e88f27b3Smrg if (start_level <= 1) { 1651e88f27b3Smrg unsigned alignment = MAX2(256, mtileb); 1652e88f27b3Smrg surf->bo_alignment = MAX2(surf->bo_alignment, alignment); 1653e88f27b3Smrg 1654e88f27b3Smrg if (aligned_offset) { 1655e88f27b3Smrg aligned_offset = ALIGN(aligned_offset, alignment); 1656e88f27b3Smrg } 1657e88f27b3Smrg } 1658e88f27b3Smrg 1659e88f27b3Smrg /* build mipmap tree */ 1660e88f27b3Smrg for (i = start_level; i <= surf->last_level; i++) { 1661e88f27b3Smrg level[i].mode = RADEON_SURF_MODE_2D; 1662e88f27b3Smrg si_surf_minify_2d(surf, level+i, bpe, i, slice_pt, mtilew, mtileh, 1, mtileb, aligned_offset); 1663e88f27b3Smrg if (level[i].mode == RADEON_SURF_MODE_1D) { 1664e88f27b3Smrg switch (tile_mode) { 1665e88f27b3Smrg case SI_TILE_MODE_COLOR_2D_8BPP: 1666e88f27b3Smrg case SI_TILE_MODE_COLOR_2D_16BPP: 1667e88f27b3Smrg case SI_TILE_MODE_COLOR_2D_32BPP: 1668e88f27b3Smrg case SI_TILE_MODE_COLOR_2D_64BPP: 1669e88f27b3Smrg tile_mode = SI_TILE_MODE_COLOR_1D; 1670e88f27b3Smrg break; 1671e88f27b3Smrg case SI_TILE_MODE_COLOR_2D_SCANOUT_16BPP: 1672e88f27b3Smrg case SI_TILE_MODE_COLOR_2D_SCANOUT_32BPP: 1673e88f27b3Smrg tile_mode = SI_TILE_MODE_COLOR_1D_SCANOUT; 1674e88f27b3Smrg break; 1675e88f27b3Smrg case SI_TILE_MODE_DEPTH_STENCIL_2D: 1676e88f27b3Smrg tile_mode = SI_TILE_MODE_DEPTH_STENCIL_1D; 1677e88f27b3Smrg break; 1678e88f27b3Smrg default: 1679e88f27b3Smrg return -EINVAL; 1680e88f27b3Smrg } 1681e88f27b3Smrg return si_surface_init_1d(surf_man, surf, level, bpe, tile_mode, offset, i); 1682e88f27b3Smrg } 1683e88f27b3Smrg /* level0 and first mipmap need to have alignment */ 1684e88f27b3Smrg aligned_offset = offset = surf->bo_size; 1685a884aba1Smrg if (i == 0) { 1686e88f27b3Smrg aligned_offset = ALIGN(aligned_offset, surf->bo_alignment); 1687e88f27b3Smrg } 1688e88f27b3Smrg if (surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX) { 1689e88f27b3Smrg if (surf->level == level) { 1690e88f27b3Smrg surf->tiling_index[i] = tile_mode; 1691e88f27b3Smrg /* it's ok because stencil is done after */ 1692e88f27b3Smrg surf->stencil_tiling_index[i] = tile_mode; 1693e88f27b3Smrg } else { 1694e88f27b3Smrg surf->stencil_tiling_index[i] = tile_mode; 1695e88f27b3Smrg } 1696e88f27b3Smrg } 1697e88f27b3Smrg } 1698e88f27b3Smrg return 0; 1699e88f27b3Smrg} 1700e88f27b3Smrg 1701e88f27b3Smrgstatic int si_surface_init_2d_miptrees(struct radeon_surface_manager *surf_man, 1702e88f27b3Smrg struct radeon_surface *surf, 1703e88f27b3Smrg unsigned tile_mode, unsigned stencil_tile_mode) 1704e88f27b3Smrg{ 1705e88f27b3Smrg unsigned num_pipes, num_banks; 1706e88f27b3Smrg uint32_t gb_tile_mode; 1707e88f27b3Smrg int r; 1708e88f27b3Smrg 1709e88f27b3Smrg /* retrieve tiling mode value */ 1710e88f27b3Smrg gb_tile_mode = surf_man->hw_info.tile_mode_array[tile_mode]; 1711e88f27b3Smrg si_gb_tile_mode(gb_tile_mode, &num_pipes, &num_banks, NULL, NULL, NULL, NULL); 1712e88f27b3Smrg 1713e88f27b3Smrg r = si_surface_init_2d(surf_man, surf, surf->level, surf->bpe, tile_mode, num_pipes, num_banks, surf->tile_split, 0, 0); 1714e88f27b3Smrg if (r) { 1715e88f27b3Smrg return r; 1716e88f27b3Smrg } 1717e88f27b3Smrg 1718e88f27b3Smrg if (surf->flags & RADEON_SURF_SBUFFER) { 1719e88f27b3Smrg r = si_surface_init_2d(surf_man, surf, surf->stencil_level, 1, stencil_tile_mode, num_pipes, num_banks, surf->stencil_tile_split, surf->bo_size, 0); 1720e88f27b3Smrg surf->stencil_offset = surf->stencil_level[0].offset; 1721e88f27b3Smrg } 1722e88f27b3Smrg return r; 1723e88f27b3Smrg} 1724e88f27b3Smrg 1725e88f27b3Smrgstatic int si_surface_init(struct radeon_surface_manager *surf_man, 1726e88f27b3Smrg struct radeon_surface *surf) 1727e88f27b3Smrg{ 1728e88f27b3Smrg unsigned mode, tile_mode, stencil_tile_mode; 1729e88f27b3Smrg int r; 1730e88f27b3Smrg 1731e88f27b3Smrg /* MSAA surfaces support the 2D mode only. */ 1732e88f27b3Smrg if (surf->nsamples > 1) { 1733e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 1734e88f27b3Smrg surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE); 1735e88f27b3Smrg } 1736e88f27b3Smrg 1737e88f27b3Smrg /* tiling mode */ 1738e88f27b3Smrg mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK; 1739e88f27b3Smrg 1740e88f27b3Smrg if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)) { 1741e88f27b3Smrg /* zbuffer only support 1D or 2D tiled surface */ 1742e88f27b3Smrg switch (mode) { 1743e88f27b3Smrg case RADEON_SURF_MODE_1D: 1744e88f27b3Smrg case RADEON_SURF_MODE_2D: 1745e88f27b3Smrg break; 1746e88f27b3Smrg default: 1747e88f27b3Smrg mode = RADEON_SURF_MODE_1D; 1748e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 1749e88f27b3Smrg surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE); 1750e88f27b3Smrg break; 1751e88f27b3Smrg } 1752e88f27b3Smrg } 1753e88f27b3Smrg 1754e88f27b3Smrg r = si_surface_sanity(surf_man, surf, mode, &tile_mode, &stencil_tile_mode); 1755e88f27b3Smrg if (r) { 1756e88f27b3Smrg return r; 1757e88f27b3Smrg } 1758e88f27b3Smrg 1759e88f27b3Smrg surf->stencil_offset = 0; 1760e88f27b3Smrg surf->bo_alignment = 0; 1761e88f27b3Smrg 1762e88f27b3Smrg /* check tiling mode */ 1763e88f27b3Smrg switch (mode) { 1764e88f27b3Smrg case RADEON_SURF_MODE_LINEAR: 1765e88f27b3Smrg r = r6_surface_init_linear(surf_man, surf, 0, 0); 1766e88f27b3Smrg break; 1767e88f27b3Smrg case RADEON_SURF_MODE_LINEAR_ALIGNED: 1768e88f27b3Smrg r = si_surface_init_linear_aligned(surf_man, surf, tile_mode, 0, 0); 1769e88f27b3Smrg break; 1770e88f27b3Smrg case RADEON_SURF_MODE_1D: 1771e88f27b3Smrg r = si_surface_init_1d_miptrees(surf_man, surf, tile_mode, stencil_tile_mode); 1772e88f27b3Smrg break; 1773e88f27b3Smrg case RADEON_SURF_MODE_2D: 1774e88f27b3Smrg r = si_surface_init_2d_miptrees(surf_man, surf, tile_mode, stencil_tile_mode); 1775e88f27b3Smrg break; 1776e88f27b3Smrg default: 1777e88f27b3Smrg return -EINVAL; 1778e88f27b3Smrg } 1779e88f27b3Smrg return r; 1780e88f27b3Smrg} 1781e88f27b3Smrg 1782e88f27b3Smrg/* 1783e88f27b3Smrg * depending on surface 1784e88f27b3Smrg */ 1785e88f27b3Smrgstatic int si_surface_best(struct radeon_surface_manager *surf_man, 1786e88f27b3Smrg struct radeon_surface *surf) 1787e88f27b3Smrg{ 1788e88f27b3Smrg unsigned mode, tile_mode, stencil_tile_mode; 1789e88f27b3Smrg 1790e88f27b3Smrg /* tiling mode */ 1791e88f27b3Smrg mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK; 1792e88f27b3Smrg 1793e88f27b3Smrg if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER) && 1794e88f27b3Smrg !(surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX)) { 1795e88f27b3Smrg /* depth/stencil force 1d tiling for old mesa */ 1796e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 1797e88f27b3Smrg surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE); 1798e88f27b3Smrg } 1799e88f27b3Smrg 1800e88f27b3Smrg return si_surface_sanity(surf_man, surf, mode, &tile_mode, &stencil_tile_mode); 1801e88f27b3Smrg} 1802e88f27b3Smrg 1803e88f27b3Smrg 1804e88f27b3Smrg/* =========================================================================== 1805e88f27b3Smrg * Sea Islands family 1806e88f27b3Smrg */ 1807e88f27b3Smrg#define CIK__GB_TILE_MODE__PIPE_CONFIG(x) (((x) >> 6) & 0x1f) 1808e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P2 0 1809e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P4_8x16 4 1810e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P4_16x16 5 1811e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P4_16x32 6 1812e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P4_32x32 7 1813e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P8_16x16_8x16 8 1814e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P8_16x32_8x16 9 1815e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_8x16 10 1816e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P8_16x32_16x16 11 1817e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x16 12 1818e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x32 13 1819e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P8_32x64_32x32 14 1820e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P16_32X32_8X16 16 1821e88f27b3Smrg#define CIK__PIPE_CONFIG__ADDR_SURF_P16_32X32_16X16 17 1822e88f27b3Smrg#define CIK__GB_TILE_MODE__TILE_SPLIT(x) (((x) >> 11) & 0x7) 1823e88f27b3Smrg#define CIK__TILE_SPLIT__64B 0 1824e88f27b3Smrg#define CIK__TILE_SPLIT__128B 1 1825e88f27b3Smrg#define CIK__TILE_SPLIT__256B 2 1826e88f27b3Smrg#define CIK__TILE_SPLIT__512B 3 1827e88f27b3Smrg#define CIK__TILE_SPLIT__1024B 4 1828e88f27b3Smrg#define CIK__TILE_SPLIT__2048B 5 1829e88f27b3Smrg#define CIK__TILE_SPLIT__4096B 6 1830e88f27b3Smrg#define CIK__GB_TILE_MODE__SAMPLE_SPLIT(x) (((x) >> 25) & 0x3) 1831e88f27b3Smrg#define CIK__SAMPLE_SPLIT__1 0 1832e88f27b3Smrg#define CIK__SAMPLE_SPLIT__2 1 1833e88f27b3Smrg#define CIK__SAMPLE_SPLIT__4 2 1834e88f27b3Smrg#define CIK__SAMPLE_SPLIT__8 3 1835e88f27b3Smrg#define CIK__GB_MACROTILE_MODE__BANK_WIDTH(x) ((x) & 0x3) 1836e88f27b3Smrg#define CIK__BANK_WIDTH__1 0 1837e88f27b3Smrg#define CIK__BANK_WIDTH__2 1 1838e88f27b3Smrg#define CIK__BANK_WIDTH__4 2 1839e88f27b3Smrg#define CIK__BANK_WIDTH__8 3 1840e88f27b3Smrg#define CIK__GB_MACROTILE_MODE__BANK_HEIGHT(x) (((x) >> 2) & 0x3) 1841e88f27b3Smrg#define CIK__BANK_HEIGHT__1 0 1842e88f27b3Smrg#define CIK__BANK_HEIGHT__2 1 1843e88f27b3Smrg#define CIK__BANK_HEIGHT__4 2 1844e88f27b3Smrg#define CIK__BANK_HEIGHT__8 3 1845e88f27b3Smrg#define CIK__GB_MACROTILE_MODE__MACRO_TILE_ASPECT(x) (((x) >> 4) & 0x3) 1846e88f27b3Smrg#define CIK__MACRO_TILE_ASPECT__1 0 1847e88f27b3Smrg#define CIK__MACRO_TILE_ASPECT__2 1 1848e88f27b3Smrg#define CIK__MACRO_TILE_ASPECT__4 2 1849e88f27b3Smrg#define CIK__MACRO_TILE_ASPECT__8 3 1850e88f27b3Smrg#define CIK__GB_MACROTILE_MODE__NUM_BANKS(x) (((x) >> 6) & 0x3) 1851e88f27b3Smrg#define CIK__NUM_BANKS__2_BANK 0 1852e88f27b3Smrg#define CIK__NUM_BANKS__4_BANK 1 1853e88f27b3Smrg#define CIK__NUM_BANKS__8_BANK 2 1854e88f27b3Smrg#define CIK__NUM_BANKS__16_BANK 3 1855e88f27b3Smrg 1856e88f27b3Smrg 1857e88f27b3Smrgstatic void cik_get_2d_params(struct radeon_surface_manager *surf_man, 1858e88f27b3Smrg unsigned bpe, unsigned nsamples, bool is_color, 1859e88f27b3Smrg unsigned tile_mode, 1860e88f27b3Smrg uint32_t *num_pipes, 1861e88f27b3Smrg uint32_t *tile_split_ptr, 1862e88f27b3Smrg uint32_t *num_banks, 1863e88f27b3Smrg uint32_t *macro_tile_aspect, 1864e88f27b3Smrg uint32_t *bank_w, 1865e88f27b3Smrg uint32_t *bank_h) 1866e88f27b3Smrg{ 1867e88f27b3Smrg uint32_t gb_tile_mode = surf_man->hw_info.tile_mode_array[tile_mode]; 1868e88f27b3Smrg unsigned tileb_1x, tileb; 1869e88f27b3Smrg unsigned gb_macrotile_mode; 1870e88f27b3Smrg unsigned macrotile_index; 1871e88f27b3Smrg unsigned tile_split, sample_split; 1872e88f27b3Smrg 1873e88f27b3Smrg if (num_pipes) { 1874e88f27b3Smrg switch (CIK__GB_TILE_MODE__PIPE_CONFIG(gb_tile_mode)) { 1875e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P2: 1876e88f27b3Smrg default: 1877e88f27b3Smrg *num_pipes = 2; 1878e88f27b3Smrg break; 1879e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P4_8x16: 1880e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P4_16x16: 1881e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P4_16x32: 1882e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P4_32x32: 1883e88f27b3Smrg *num_pipes = 4; 1884e88f27b3Smrg break; 1885e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P8_16x16_8x16: 1886e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P8_16x32_8x16: 1887e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_8x16: 1888e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P8_16x32_16x16: 1889e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x16: 1890e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x32: 1891e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P8_32x64_32x32: 1892e88f27b3Smrg *num_pipes = 8; 1893e88f27b3Smrg break; 1894e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P16_32X32_8X16: 1895e88f27b3Smrg case CIK__PIPE_CONFIG__ADDR_SURF_P16_32X32_16X16: 1896e88f27b3Smrg *num_pipes = 16; 1897e88f27b3Smrg break; 1898e88f27b3Smrg } 1899e88f27b3Smrg } 1900e88f27b3Smrg switch (CIK__GB_TILE_MODE__TILE_SPLIT(gb_tile_mode)) { 1901e88f27b3Smrg default: 1902e88f27b3Smrg case CIK__TILE_SPLIT__64B: 1903e88f27b3Smrg tile_split = 64; 1904e88f27b3Smrg break; 1905e88f27b3Smrg case CIK__TILE_SPLIT__128B: 1906e88f27b3Smrg tile_split = 128; 1907e88f27b3Smrg break; 1908e88f27b3Smrg case CIK__TILE_SPLIT__256B: 1909e88f27b3Smrg tile_split = 256; 1910e88f27b3Smrg break; 1911e88f27b3Smrg case CIK__TILE_SPLIT__512B: 1912e88f27b3Smrg tile_split = 512; 1913e88f27b3Smrg break; 1914e88f27b3Smrg case CIK__TILE_SPLIT__1024B: 1915e88f27b3Smrg tile_split = 1024; 1916e88f27b3Smrg break; 1917e88f27b3Smrg case CIK__TILE_SPLIT__2048B: 1918e88f27b3Smrg tile_split = 2048; 1919e88f27b3Smrg break; 1920e88f27b3Smrg case CIK__TILE_SPLIT__4096B: 1921e88f27b3Smrg tile_split = 4096; 1922e88f27b3Smrg break; 1923e88f27b3Smrg } 1924e88f27b3Smrg switch (CIK__GB_TILE_MODE__SAMPLE_SPLIT(gb_tile_mode)) { 1925e88f27b3Smrg default: 1926e88f27b3Smrg case CIK__SAMPLE_SPLIT__1: 1927e88f27b3Smrg sample_split = 1; 1928e88f27b3Smrg break; 1929e88f27b3Smrg case CIK__SAMPLE_SPLIT__2: 1930a884aba1Smrg sample_split = 2; 1931e88f27b3Smrg break; 1932e88f27b3Smrg case CIK__SAMPLE_SPLIT__4: 1933e88f27b3Smrg sample_split = 4; 1934e88f27b3Smrg break; 1935e88f27b3Smrg case CIK__SAMPLE_SPLIT__8: 1936e88f27b3Smrg sample_split = 8; 1937e88f27b3Smrg break; 1938e88f27b3Smrg } 1939e88f27b3Smrg 1940e88f27b3Smrg /* Adjust the tile split. */ 1941e88f27b3Smrg tileb_1x = 8 * 8 * bpe; 1942e88f27b3Smrg if (is_color) { 1943e88f27b3Smrg tile_split = MAX2(256, sample_split * tileb_1x); 1944e88f27b3Smrg } 1945e88f27b3Smrg tile_split = MIN2(surf_man->hw_info.row_size, tile_split); 1946e88f27b3Smrg 1947e88f27b3Smrg /* Determine the macrotile index. */ 1948e88f27b3Smrg tileb = MIN2(tile_split, nsamples * tileb_1x); 1949e88f27b3Smrg 1950e88f27b3Smrg for (macrotile_index = 0; tileb > 64; macrotile_index++) { 1951e88f27b3Smrg tileb >>= 1; 1952e88f27b3Smrg } 1953e88f27b3Smrg gb_macrotile_mode = surf_man->hw_info.macrotile_mode_array[macrotile_index]; 1954e88f27b3Smrg 1955e88f27b3Smrg if (tile_split_ptr) { 1956e88f27b3Smrg *tile_split_ptr = tile_split; 1957e88f27b3Smrg } 1958e88f27b3Smrg if (num_banks) { 1959e88f27b3Smrg switch (CIK__GB_MACROTILE_MODE__NUM_BANKS(gb_macrotile_mode)) { 1960e88f27b3Smrg default: 1961e88f27b3Smrg case CIK__NUM_BANKS__2_BANK: 1962e88f27b3Smrg *num_banks = 2; 1963e88f27b3Smrg break; 1964e88f27b3Smrg case CIK__NUM_BANKS__4_BANK: 1965e88f27b3Smrg *num_banks = 4; 1966e88f27b3Smrg break; 1967e88f27b3Smrg case CIK__NUM_BANKS__8_BANK: 1968e88f27b3Smrg *num_banks = 8; 1969e88f27b3Smrg break; 1970e88f27b3Smrg case CIK__NUM_BANKS__16_BANK: 1971e88f27b3Smrg *num_banks = 16; 1972e88f27b3Smrg break; 1973e88f27b3Smrg } 1974e88f27b3Smrg } 1975e88f27b3Smrg if (macro_tile_aspect) { 1976e88f27b3Smrg switch (CIK__GB_MACROTILE_MODE__MACRO_TILE_ASPECT(gb_macrotile_mode)) { 1977e88f27b3Smrg default: 1978e88f27b3Smrg case CIK__MACRO_TILE_ASPECT__1: 1979e88f27b3Smrg *macro_tile_aspect = 1; 1980e88f27b3Smrg break; 1981e88f27b3Smrg case CIK__MACRO_TILE_ASPECT__2: 1982e88f27b3Smrg *macro_tile_aspect = 2; 1983e88f27b3Smrg break; 1984e88f27b3Smrg case CIK__MACRO_TILE_ASPECT__4: 1985e88f27b3Smrg *macro_tile_aspect = 4; 1986e88f27b3Smrg break; 1987e88f27b3Smrg case CIK__MACRO_TILE_ASPECT__8: 1988e88f27b3Smrg *macro_tile_aspect = 8; 1989e88f27b3Smrg break; 1990e88f27b3Smrg } 1991e88f27b3Smrg } 1992e88f27b3Smrg if (bank_w) { 1993e88f27b3Smrg switch (CIK__GB_MACROTILE_MODE__BANK_WIDTH(gb_macrotile_mode)) { 1994e88f27b3Smrg default: 1995e88f27b3Smrg case CIK__BANK_WIDTH__1: 1996e88f27b3Smrg *bank_w = 1; 1997e88f27b3Smrg break; 1998e88f27b3Smrg case CIK__BANK_WIDTH__2: 1999e88f27b3Smrg *bank_w = 2; 2000e88f27b3Smrg break; 2001e88f27b3Smrg case CIK__BANK_WIDTH__4: 2002e88f27b3Smrg *bank_w = 4; 2003e88f27b3Smrg break; 2004e88f27b3Smrg case CIK__BANK_WIDTH__8: 2005e88f27b3Smrg *bank_w = 8; 2006e88f27b3Smrg break; 2007e88f27b3Smrg } 2008e88f27b3Smrg } 2009e88f27b3Smrg if (bank_h) { 2010e88f27b3Smrg switch (CIK__GB_MACROTILE_MODE__BANK_HEIGHT(gb_macrotile_mode)) { 2011e88f27b3Smrg default: 2012e88f27b3Smrg case CIK__BANK_HEIGHT__1: 2013e88f27b3Smrg *bank_h = 1; 2014e88f27b3Smrg break; 2015e88f27b3Smrg case CIK__BANK_HEIGHT__2: 2016e88f27b3Smrg *bank_h = 2; 2017e88f27b3Smrg break; 2018e88f27b3Smrg case CIK__BANK_HEIGHT__4: 2019e88f27b3Smrg *bank_h = 4; 2020e88f27b3Smrg break; 2021e88f27b3Smrg case CIK__BANK_HEIGHT__8: 2022e88f27b3Smrg *bank_h = 8; 2023e88f27b3Smrg break; 2024e88f27b3Smrg } 2025e88f27b3Smrg } 2026e88f27b3Smrg} 2027e88f27b3Smrg 2028e88f27b3Smrgstatic int cik_init_hw_info(struct radeon_surface_manager *surf_man) 2029e88f27b3Smrg{ 2030e88f27b3Smrg uint32_t tiling_config; 2031e88f27b3Smrg drmVersionPtr version; 2032e88f27b3Smrg int r; 2033e88f27b3Smrg 2034e88f27b3Smrg r = radeon_get_value(surf_man->fd, RADEON_INFO_TILING_CONFIG, 2035e88f27b3Smrg &tiling_config); 2036e88f27b3Smrg if (r) { 2037e88f27b3Smrg return r; 2038e88f27b3Smrg } 2039e88f27b3Smrg 2040e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 2041e88f27b3Smrg version = drmGetVersion(surf_man->fd); 2042e88f27b3Smrg if (version && version->version_minor >= 35) { 2043e88f27b3Smrg if (!radeon_get_value(surf_man->fd, RADEON_INFO_SI_TILE_MODE_ARRAY, surf_man->hw_info.tile_mode_array) && 2044e88f27b3Smrg !radeon_get_value(surf_man->fd, RADEON_INFO_CIK_MACROTILE_MODE_ARRAY, surf_man->hw_info.macrotile_mode_array)) { 2045e88f27b3Smrg surf_man->hw_info.allow_2d = 1; 2046e88f27b3Smrg } 2047e88f27b3Smrg } 2048e88f27b3Smrg drmFreeVersion(version); 2049e88f27b3Smrg 2050e88f27b3Smrg switch (tiling_config & 0xf) { 2051e88f27b3Smrg case 0: 2052e88f27b3Smrg surf_man->hw_info.num_pipes = 1; 2053e88f27b3Smrg break; 2054e88f27b3Smrg case 1: 2055e88f27b3Smrg surf_man->hw_info.num_pipes = 2; 2056e88f27b3Smrg break; 2057e88f27b3Smrg case 2: 2058e88f27b3Smrg surf_man->hw_info.num_pipes = 4; 2059e88f27b3Smrg break; 2060e88f27b3Smrg case 3: 2061e88f27b3Smrg surf_man->hw_info.num_pipes = 8; 2062e88f27b3Smrg break; 2063e88f27b3Smrg default: 2064e88f27b3Smrg surf_man->hw_info.num_pipes = 8; 2065e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 2066e88f27b3Smrg break; 2067e88f27b3Smrg } 2068e88f27b3Smrg 2069e88f27b3Smrg switch ((tiling_config & 0xf0) >> 4) { 2070e88f27b3Smrg case 0: 2071e88f27b3Smrg surf_man->hw_info.num_banks = 4; 2072e88f27b3Smrg break; 2073e88f27b3Smrg case 1: 2074e88f27b3Smrg surf_man->hw_info.num_banks = 8; 2075e88f27b3Smrg break; 2076e88f27b3Smrg case 2: 2077e88f27b3Smrg surf_man->hw_info.num_banks = 16; 2078e88f27b3Smrg break; 2079e88f27b3Smrg default: 2080e88f27b3Smrg surf_man->hw_info.num_banks = 8; 2081e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 2082e88f27b3Smrg break; 2083e88f27b3Smrg } 2084e88f27b3Smrg 2085e88f27b3Smrg switch ((tiling_config & 0xf00) >> 8) { 2086e88f27b3Smrg case 0: 2087e88f27b3Smrg surf_man->hw_info.group_bytes = 256; 2088e88f27b3Smrg break; 2089e88f27b3Smrg case 1: 2090e88f27b3Smrg surf_man->hw_info.group_bytes = 512; 2091e88f27b3Smrg break; 2092e88f27b3Smrg default: 2093e88f27b3Smrg surf_man->hw_info.group_bytes = 256; 2094e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 2095e88f27b3Smrg break; 2096e88f27b3Smrg } 2097e88f27b3Smrg 2098e88f27b3Smrg switch ((tiling_config & 0xf000) >> 12) { 2099e88f27b3Smrg case 0: 2100e88f27b3Smrg surf_man->hw_info.row_size = 1024; 2101e88f27b3Smrg break; 2102e88f27b3Smrg case 1: 2103e88f27b3Smrg surf_man->hw_info.row_size = 2048; 2104e88f27b3Smrg break; 2105e88f27b3Smrg case 2: 2106e88f27b3Smrg surf_man->hw_info.row_size = 4096; 2107e88f27b3Smrg break; 2108e88f27b3Smrg default: 2109e88f27b3Smrg surf_man->hw_info.row_size = 4096; 2110e88f27b3Smrg surf_man->hw_info.allow_2d = 0; 2111e88f27b3Smrg break; 2112e88f27b3Smrg } 2113e88f27b3Smrg return 0; 2114e88f27b3Smrg} 2115e88f27b3Smrg 2116e88f27b3Smrgstatic int cik_surface_sanity(struct radeon_surface_manager *surf_man, 2117e88f27b3Smrg struct radeon_surface *surf, 2118e88f27b3Smrg unsigned mode, unsigned *tile_mode, unsigned *stencil_tile_mode) 2119e88f27b3Smrg{ 2120e88f27b3Smrg /* check surface dimension */ 2121e88f27b3Smrg if (surf->npix_x > 16384 || surf->npix_y > 16384 || surf->npix_z > 16384) { 2122e88f27b3Smrg return -EINVAL; 2123e88f27b3Smrg } 2124e88f27b3Smrg 2125e88f27b3Smrg /* check mipmap last_level */ 2126e88f27b3Smrg if (surf->last_level > 15) { 2127e88f27b3Smrg return -EINVAL; 2128e88f27b3Smrg } 2129e88f27b3Smrg 2130e88f27b3Smrg /* force 1d on kernel that can't do 2d */ 2131e88f27b3Smrg if (mode > RADEON_SURF_MODE_1D && 2132e88f27b3Smrg (!surf_man->hw_info.allow_2d || !(surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX))) { 2133e88f27b3Smrg if (surf->nsamples > 1) { 2134e88f27b3Smrg fprintf(stderr, "radeon: Cannot use 1D tiling for an MSAA surface (%i).\n", __LINE__); 2135e88f27b3Smrg return -EFAULT; 2136e88f27b3Smrg } 2137e88f27b3Smrg mode = RADEON_SURF_MODE_1D; 2138e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 2139e88f27b3Smrg surf->flags |= RADEON_SURF_SET(mode, MODE); 2140e88f27b3Smrg } 2141e88f27b3Smrg 2142e88f27b3Smrg if (surf->nsamples > 1 && mode != RADEON_SURF_MODE_2D) { 2143e88f27b3Smrg return -EINVAL; 2144e88f27b3Smrg } 2145e88f27b3Smrg 2146e88f27b3Smrg if (!surf->tile_split) { 2147e88f27b3Smrg /* default value */ 2148e88f27b3Smrg surf->mtilea = 1; 2149e88f27b3Smrg surf->bankw = 1; 2150a884aba1Smrg surf->bankh = 1; 2151e88f27b3Smrg surf->tile_split = 64; 2152e88f27b3Smrg surf->stencil_tile_split = 64; 2153e88f27b3Smrg } 2154e88f27b3Smrg 2155e88f27b3Smrg switch (mode) { 2156e88f27b3Smrg case RADEON_SURF_MODE_2D: { 2157e88f27b3Smrg if (surf->flags & RADEON_SURF_Z_OR_SBUFFER) { 2158e88f27b3Smrg switch (surf->nsamples) { 2159e88f27b3Smrg case 1: 2160e88f27b3Smrg *tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_64; 2161e88f27b3Smrg break; 2162e88f27b3Smrg case 2: 2163e88f27b3Smrg case 4: 2164e88f27b3Smrg *tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_128; 2165e88f27b3Smrg break; 2166e88f27b3Smrg case 8: 2167e88f27b3Smrg *tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_256; 2168e88f27b3Smrg break; 2169e88f27b3Smrg default: 2170e88f27b3Smrg return -EINVAL; 2171e88f27b3Smrg } 2172e88f27b3Smrg 2173e88f27b3Smrg if (surf->flags & RADEON_SURF_SBUFFER) { 2174e88f27b3Smrg *stencil_tile_mode = *tile_mode; 2175e88f27b3Smrg 2176e88f27b3Smrg cik_get_2d_params(surf_man, 1, surf->nsamples, false, 2177e88f27b3Smrg *stencil_tile_mode, NULL, 2178e88f27b3Smrg &surf->stencil_tile_split, 2179e88f27b3Smrg NULL, NULL, NULL, NULL); 2180e88f27b3Smrg } 2181e88f27b3Smrg } else if (surf->flags & RADEON_SURF_SCANOUT) { 2182e88f27b3Smrg *tile_mode = CIK_TILE_MODE_COLOR_2D_SCANOUT; 2183e88f27b3Smrg } else { 2184e88f27b3Smrg *tile_mode = CIK_TILE_MODE_COLOR_2D; 2185e88f27b3Smrg } 2186e88f27b3Smrg 2187e88f27b3Smrg /* retrieve tiling mode values */ 2188e88f27b3Smrg cik_get_2d_params(surf_man, surf->bpe, surf->nsamples, 2189e88f27b3Smrg !(surf->flags & RADEON_SURF_Z_OR_SBUFFER), *tile_mode, 2190e88f27b3Smrg NULL, &surf->tile_split, NULL, &surf->mtilea, 2191e88f27b3Smrg &surf->bankw, &surf->bankh); 2192e88f27b3Smrg break; 2193e88f27b3Smrg } 2194e88f27b3Smrg case RADEON_SURF_MODE_1D: 2195e88f27b3Smrg if (surf->flags & RADEON_SURF_SBUFFER) { 2196e88f27b3Smrg *stencil_tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_1D; 2197e88f27b3Smrg } 2198e88f27b3Smrg if (surf->flags & RADEON_SURF_ZBUFFER) { 2199e88f27b3Smrg *tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_1D; 2200e88f27b3Smrg } else if (surf->flags & RADEON_SURF_SCANOUT) { 2201e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_1D_SCANOUT; 2202e88f27b3Smrg } else { 2203e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_1D; 2204e88f27b3Smrg } 2205e88f27b3Smrg break; 2206e88f27b3Smrg case RADEON_SURF_MODE_LINEAR_ALIGNED: 2207e88f27b3Smrg default: 220848246ce7Smrg *stencil_tile_mode = SI_TILE_MODE_COLOR_LINEAR_ALIGNED; 2209e88f27b3Smrg *tile_mode = SI_TILE_MODE_COLOR_LINEAR_ALIGNED; 2210e88f27b3Smrg } 2211e88f27b3Smrg 2212e88f27b3Smrg return 0; 2213e88f27b3Smrg} 2214e88f27b3Smrg 2215e88f27b3Smrgstatic int cik_surface_init_2d(struct radeon_surface_manager *surf_man, 2216e88f27b3Smrg struct radeon_surface *surf, 2217e88f27b3Smrg struct radeon_surface_level *level, 2218e88f27b3Smrg unsigned bpe, unsigned tile_mode, 2219e88f27b3Smrg unsigned tile_split, 2220e88f27b3Smrg unsigned num_pipes, unsigned num_banks, 2221e88f27b3Smrg uint64_t offset, 2222e88f27b3Smrg unsigned start_level) 2223e88f27b3Smrg{ 2224e88f27b3Smrg uint64_t aligned_offset = offset; 2225e88f27b3Smrg unsigned tilew, tileh, tileb_1x, tileb; 2226e88f27b3Smrg unsigned mtilew, mtileh, mtileb; 2227e88f27b3Smrg unsigned slice_pt; 2228e88f27b3Smrg unsigned i; 2229e88f27b3Smrg 2230e88f27b3Smrg /* compute tile values */ 2231e88f27b3Smrg tilew = 8; 2232e88f27b3Smrg tileh = 8; 2233e88f27b3Smrg tileb_1x = tilew * tileh * bpe; 2234e88f27b3Smrg 2235e88f27b3Smrg tile_split = MIN2(surf_man->hw_info.row_size, tile_split); 2236e88f27b3Smrg 2237e88f27b3Smrg tileb = surf->nsamples * tileb_1x; 2238e88f27b3Smrg 2239e88f27b3Smrg /* slices per tile */ 2240e88f27b3Smrg slice_pt = 1; 2241e88f27b3Smrg if (tileb > tile_split && tile_split) { 2242e88f27b3Smrg slice_pt = tileb / tile_split; 2243e88f27b3Smrg tileb = tileb / slice_pt; 2244e88f27b3Smrg } 2245e88f27b3Smrg 2246e88f27b3Smrg /* macro tile width & height */ 2247e88f27b3Smrg mtilew = (tilew * surf->bankw * num_pipes) * surf->mtilea; 2248e88f27b3Smrg mtileh = (tileh * surf->bankh * num_banks) / surf->mtilea; 2249e88f27b3Smrg 2250e88f27b3Smrg /* macro tile bytes */ 2251e88f27b3Smrg mtileb = (mtilew / tilew) * (mtileh / tileh) * tileb; 2252e88f27b3Smrg 2253e88f27b3Smrg if (start_level <= 1) { 2254e88f27b3Smrg unsigned alignment = MAX2(256, mtileb); 2255e88f27b3Smrg surf->bo_alignment = MAX2(surf->bo_alignment, alignment); 2256e88f27b3Smrg 2257e88f27b3Smrg if (aligned_offset) { 2258e88f27b3Smrg aligned_offset = ALIGN(aligned_offset, alignment); 2259e88f27b3Smrg } 2260e88f27b3Smrg } 2261e88f27b3Smrg 2262e88f27b3Smrg /* build mipmap tree */ 2263e88f27b3Smrg for (i = start_level; i <= surf->last_level; i++) { 2264e88f27b3Smrg level[i].mode = RADEON_SURF_MODE_2D; 2265e88f27b3Smrg si_surf_minify_2d(surf, level+i, bpe, i, slice_pt, mtilew, mtileh, 1, mtileb, aligned_offset); 2266e88f27b3Smrg if (level[i].mode == RADEON_SURF_MODE_1D) { 2267e88f27b3Smrg switch (tile_mode) { 2268e88f27b3Smrg case CIK_TILE_MODE_COLOR_2D: 2269e88f27b3Smrg tile_mode = SI_TILE_MODE_COLOR_1D; 2270e88f27b3Smrg break; 2271e88f27b3Smrg case CIK_TILE_MODE_COLOR_2D_SCANOUT: 2272e88f27b3Smrg tile_mode = SI_TILE_MODE_COLOR_1D_SCANOUT; 2273e88f27b3Smrg break; 2274e88f27b3Smrg case CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_64: 2275e88f27b3Smrg case CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_128: 2276e88f27b3Smrg case CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_256: 2277e88f27b3Smrg case CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_512: 2278e88f27b3Smrg case CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_ROW_SIZE: 2279e88f27b3Smrg tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_1D; 2280e88f27b3Smrg break; 2281e88f27b3Smrg default: 2282e88f27b3Smrg return -EINVAL; 2283e88f27b3Smrg } 2284e88f27b3Smrg return si_surface_init_1d(surf_man, surf, level, bpe, tile_mode, offset, i); 2285e88f27b3Smrg } 2286e88f27b3Smrg /* level0 and first mipmap need to have alignment */ 2287e88f27b3Smrg aligned_offset = offset = surf->bo_size; 2288e88f27b3Smrg if (i == 0) { 2289e88f27b3Smrg aligned_offset = ALIGN(aligned_offset, surf->bo_alignment); 2290e88f27b3Smrg } 2291e88f27b3Smrg if (surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX) { 2292e88f27b3Smrg if (surf->level == level) { 2293e88f27b3Smrg surf->tiling_index[i] = tile_mode; 2294e88f27b3Smrg /* it's ok because stencil is done after */ 2295e88f27b3Smrg surf->stencil_tiling_index[i] = tile_mode; 2296e88f27b3Smrg } else { 2297e88f27b3Smrg surf->stencil_tiling_index[i] = tile_mode; 2298e88f27b3Smrg } 2299e88f27b3Smrg } 2300e88f27b3Smrg } 2301e88f27b3Smrg return 0; 2302e88f27b3Smrg} 2303e88f27b3Smrg 2304e88f27b3Smrgstatic int cik_surface_init_2d_miptrees(struct radeon_surface_manager *surf_man, 2305e88f27b3Smrg struct radeon_surface *surf, 2306e88f27b3Smrg unsigned tile_mode, unsigned stencil_tile_mode) 2307e88f27b3Smrg{ 2308e88f27b3Smrg int r; 2309e88f27b3Smrg uint32_t num_pipes, num_banks; 2310e88f27b3Smrg 2311e88f27b3Smrg cik_get_2d_params(surf_man, surf->bpe, surf->nsamples, 2312e88f27b3Smrg !(surf->flags & RADEON_SURF_Z_OR_SBUFFER), tile_mode, 2313e88f27b3Smrg &num_pipes, NULL, &num_banks, NULL, NULL, NULL); 2314e88f27b3Smrg 2315e88f27b3Smrg r = cik_surface_init_2d(surf_man, surf, surf->level, surf->bpe, tile_mode, 2316e88f27b3Smrg surf->tile_split, num_pipes, num_banks, 0, 0); 2317e88f27b3Smrg if (r) { 2318e88f27b3Smrg return r; 2319e88f27b3Smrg } 2320e88f27b3Smrg 2321e88f27b3Smrg if (surf->flags & RADEON_SURF_SBUFFER) { 2322e88f27b3Smrg r = cik_surface_init_2d(surf_man, surf, surf->stencil_level, 1, stencil_tile_mode, 2323e88f27b3Smrg surf->stencil_tile_split, num_pipes, num_banks, 2324e88f27b3Smrg surf->bo_size, 0); 2325e88f27b3Smrg surf->stencil_offset = surf->stencil_level[0].offset; 2326e88f27b3Smrg } 2327e88f27b3Smrg return r; 2328e88f27b3Smrg} 2329e88f27b3Smrg 2330e88f27b3Smrgstatic int cik_surface_init(struct radeon_surface_manager *surf_man, 2331e88f27b3Smrg struct radeon_surface *surf) 2332e88f27b3Smrg{ 2333e88f27b3Smrg unsigned mode, tile_mode, stencil_tile_mode; 2334e88f27b3Smrg int r; 2335e88f27b3Smrg 2336e88f27b3Smrg /* MSAA surfaces support the 2D mode only. */ 2337e88f27b3Smrg if (surf->nsamples > 1) { 2338e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 2339e88f27b3Smrg surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE); 2340e88f27b3Smrg } 2341e88f27b3Smrg 2342e88f27b3Smrg /* tiling mode */ 2343e88f27b3Smrg mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK; 2344e88f27b3Smrg 2345e88f27b3Smrg if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)) { 2346e88f27b3Smrg /* zbuffer only support 1D or 2D tiled surface */ 2347e88f27b3Smrg switch (mode) { 2348e88f27b3Smrg case RADEON_SURF_MODE_1D: 2349e88f27b3Smrg case RADEON_SURF_MODE_2D: 2350e88f27b3Smrg break; 2351e88f27b3Smrg default: 2352e88f27b3Smrg mode = RADEON_SURF_MODE_1D; 2353e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 2354e88f27b3Smrg surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE); 2355e88f27b3Smrg break; 2356e88f27b3Smrg } 2357e88f27b3Smrg } 2358e88f27b3Smrg 2359e88f27b3Smrg r = cik_surface_sanity(surf_man, surf, mode, &tile_mode, &stencil_tile_mode); 2360e88f27b3Smrg if (r) { 2361e88f27b3Smrg return r; 2362e88f27b3Smrg } 2363e88f27b3Smrg 2364e88f27b3Smrg surf->stencil_offset = 0; 2365e88f27b3Smrg surf->bo_alignment = 0; 2366e88f27b3Smrg 2367e88f27b3Smrg /* check tiling mode */ 2368e88f27b3Smrg switch (mode) { 2369e88f27b3Smrg case RADEON_SURF_MODE_LINEAR: 2370e88f27b3Smrg r = r6_surface_init_linear(surf_man, surf, 0, 0); 2371e88f27b3Smrg break; 2372e88f27b3Smrg case RADEON_SURF_MODE_LINEAR_ALIGNED: 2373e88f27b3Smrg r = si_surface_init_linear_aligned(surf_man, surf, tile_mode, 0, 0); 2374e88f27b3Smrg break; 2375e88f27b3Smrg case RADEON_SURF_MODE_1D: 2376e88f27b3Smrg r = si_surface_init_1d_miptrees(surf_man, surf, tile_mode, stencil_tile_mode); 2377e88f27b3Smrg break; 2378e88f27b3Smrg case RADEON_SURF_MODE_2D: 2379e88f27b3Smrg r = cik_surface_init_2d_miptrees(surf_man, surf, tile_mode, stencil_tile_mode); 2380e88f27b3Smrg break; 2381e88f27b3Smrg default: 2382e88f27b3Smrg return -EINVAL; 2383e88f27b3Smrg } 2384e88f27b3Smrg return r; 2385e88f27b3Smrg} 2386e88f27b3Smrg 2387e88f27b3Smrg/* 2388e88f27b3Smrg * depending on surface 2389e88f27b3Smrg */ 2390e88f27b3Smrgstatic int cik_surface_best(struct radeon_surface_manager *surf_man, 2391e88f27b3Smrg struct radeon_surface *surf) 2392e88f27b3Smrg{ 2393e88f27b3Smrg unsigned mode, tile_mode, stencil_tile_mode; 2394e88f27b3Smrg 2395e88f27b3Smrg /* tiling mode */ 2396e88f27b3Smrg mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK; 2397e88f27b3Smrg 2398e88f27b3Smrg if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER) && 2399e88f27b3Smrg !(surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX)) { 2400e88f27b3Smrg /* depth/stencil force 1d tiling for old mesa */ 2401e88f27b3Smrg surf->flags = RADEON_SURF_CLR(surf->flags, MODE); 2402e88f27b3Smrg surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE); 2403e88f27b3Smrg } 2404e88f27b3Smrg 2405e88f27b3Smrg return cik_surface_sanity(surf_man, surf, mode, &tile_mode, &stencil_tile_mode); 2406e88f27b3Smrg} 2407e88f27b3Smrg 2408e88f27b3Smrg 2409e88f27b3Smrg/* =========================================================================== 2410e88f27b3Smrg * public API 2411e88f27b3Smrg */ 24126260e5d5Smrgdrm_public struct radeon_surface_manager * 2413a884aba1Smrgradeon_surface_manager_new(int fd) 2414e88f27b3Smrg{ 2415e88f27b3Smrg struct radeon_surface_manager *surf_man; 2416e88f27b3Smrg 2417e88f27b3Smrg surf_man = calloc(1, sizeof(struct radeon_surface_manager)); 2418e88f27b3Smrg if (surf_man == NULL) { 2419e88f27b3Smrg return NULL; 2420e88f27b3Smrg } 2421e88f27b3Smrg surf_man->fd = fd; 2422e88f27b3Smrg if (radeon_get_value(fd, RADEON_INFO_DEVICE_ID, &surf_man->device_id)) { 2423e88f27b3Smrg goto out_err; 2424e88f27b3Smrg } 2425e88f27b3Smrg if (radeon_get_family(surf_man)) { 2426e88f27b3Smrg goto out_err; 2427e88f27b3Smrg } 2428e88f27b3Smrg 2429e88f27b3Smrg if (surf_man->family <= CHIP_RV740) { 2430e88f27b3Smrg if (r6_init_hw_info(surf_man)) { 2431e88f27b3Smrg goto out_err; 2432e88f27b3Smrg } 2433e88f27b3Smrg surf_man->surface_init = &r6_surface_init; 2434e88f27b3Smrg surf_man->surface_best = &r6_surface_best; 2435e88f27b3Smrg } else if (surf_man->family <= CHIP_ARUBA) { 2436e88f27b3Smrg if (eg_init_hw_info(surf_man)) { 2437e88f27b3Smrg goto out_err; 2438e88f27b3Smrg } 2439e88f27b3Smrg surf_man->surface_init = &eg_surface_init; 2440e88f27b3Smrg surf_man->surface_best = &eg_surface_best; 2441e88f27b3Smrg } else if (surf_man->family < CHIP_BONAIRE) { 2442e88f27b3Smrg if (si_init_hw_info(surf_man)) { 2443e88f27b3Smrg goto out_err; 2444e88f27b3Smrg } 2445e88f27b3Smrg surf_man->surface_init = &si_surface_init; 2446e88f27b3Smrg surf_man->surface_best = &si_surface_best; 2447e88f27b3Smrg } else { 2448e88f27b3Smrg if (cik_init_hw_info(surf_man)) { 2449e88f27b3Smrg goto out_err; 2450e88f27b3Smrg } 2451e88f27b3Smrg surf_man->surface_init = &cik_surface_init; 2452e88f27b3Smrg surf_man->surface_best = &cik_surface_best; 2453e88f27b3Smrg } 2454e88f27b3Smrg 2455e88f27b3Smrg return surf_man; 2456e88f27b3Smrgout_err: 2457e88f27b3Smrg free(surf_man); 2458e88f27b3Smrg return NULL; 2459e88f27b3Smrg} 2460e88f27b3Smrg 24616260e5d5Smrgdrm_public void 2462a884aba1Smrgradeon_surface_manager_free(struct radeon_surface_manager *surf_man) 2463e88f27b3Smrg{ 2464e88f27b3Smrg free(surf_man); 2465e88f27b3Smrg} 2466e88f27b3Smrg 2467e88f27b3Smrgstatic int radeon_surface_sanity(struct radeon_surface_manager *surf_man, 2468e88f27b3Smrg struct radeon_surface *surf, 2469e88f27b3Smrg unsigned type, 2470e88f27b3Smrg unsigned mode) 2471e88f27b3Smrg{ 2472e88f27b3Smrg if (surf_man == NULL || surf_man->surface_init == NULL || surf == NULL) { 2473e88f27b3Smrg return -EINVAL; 2474e88f27b3Smrg } 2475e88f27b3Smrg 2476e88f27b3Smrg /* all dimension must be at least 1 ! */ 2477e88f27b3Smrg if (!surf->npix_x || !surf->npix_y || !surf->npix_z) { 2478e88f27b3Smrg return -EINVAL; 2479e88f27b3Smrg } 2480e88f27b3Smrg if (!surf->blk_w || !surf->blk_h || !surf->blk_d) { 2481e88f27b3Smrg return -EINVAL; 2482e88f27b3Smrg } 2483e88f27b3Smrg if (!surf->array_size) { 2484e88f27b3Smrg return -EINVAL; 2485e88f27b3Smrg } 2486e88f27b3Smrg /* array size must be a power of 2 */ 2487e88f27b3Smrg surf->array_size = next_power_of_two(surf->array_size); 2488e88f27b3Smrg 2489e88f27b3Smrg switch (surf->nsamples) { 2490e88f27b3Smrg case 1: 2491e88f27b3Smrg case 2: 2492e88f27b3Smrg case 4: 2493e88f27b3Smrg case 8: 2494e88f27b3Smrg break; 2495e88f27b3Smrg default: 2496e88f27b3Smrg return -EINVAL; 2497e88f27b3Smrg } 2498e88f27b3Smrg /* check type */ 2499e88f27b3Smrg switch (type) { 2500e88f27b3Smrg case RADEON_SURF_TYPE_1D: 2501e88f27b3Smrg if (surf->npix_y > 1) { 2502e88f27b3Smrg return -EINVAL; 2503e88f27b3Smrg } 25040655efefSmrg /* fallthrough */ 2505e88f27b3Smrg case RADEON_SURF_TYPE_2D: 2506e88f27b3Smrg if (surf->npix_z > 1) { 2507e88f27b3Smrg return -EINVAL; 2508e88f27b3Smrg } 2509e88f27b3Smrg break; 2510e88f27b3Smrg case RADEON_SURF_TYPE_CUBEMAP: 2511e88f27b3Smrg if (surf->npix_z > 1) { 2512e88f27b3Smrg return -EINVAL; 2513e88f27b3Smrg } 2514e88f27b3Smrg /* deal with cubemap as they were texture array */ 2515e88f27b3Smrg if (surf_man->family >= CHIP_RV770) { 2516e88f27b3Smrg surf->array_size = 8; 2517e88f27b3Smrg } else { 2518e88f27b3Smrg surf->array_size = 6; 2519e88f27b3Smrg } 2520e88f27b3Smrg break; 2521e88f27b3Smrg case RADEON_SURF_TYPE_3D: 2522e88f27b3Smrg break; 2523e88f27b3Smrg case RADEON_SURF_TYPE_1D_ARRAY: 2524e88f27b3Smrg if (surf->npix_y > 1) { 2525e88f27b3Smrg return -EINVAL; 2526e88f27b3Smrg } 2527e88f27b3Smrg case RADEON_SURF_TYPE_2D_ARRAY: 2528e88f27b3Smrg break; 2529e88f27b3Smrg default: 2530e88f27b3Smrg return -EINVAL; 2531e88f27b3Smrg } 2532e88f27b3Smrg return 0; 2533e88f27b3Smrg} 2534e88f27b3Smrg 25356260e5d5Smrgdrm_public int 2536a884aba1Smrgradeon_surface_init(struct radeon_surface_manager *surf_man, 2537a884aba1Smrg struct radeon_surface *surf) 2538e88f27b3Smrg{ 2539e88f27b3Smrg unsigned mode, type; 2540e88f27b3Smrg int r; 2541e88f27b3Smrg 2542e88f27b3Smrg type = RADEON_SURF_GET(surf->flags, TYPE); 2543e88f27b3Smrg mode = RADEON_SURF_GET(surf->flags, MODE); 2544e88f27b3Smrg 2545e88f27b3Smrg r = radeon_surface_sanity(surf_man, surf, type, mode); 2546e88f27b3Smrg if (r) { 2547e88f27b3Smrg return r; 2548e88f27b3Smrg } 2549e88f27b3Smrg return surf_man->surface_init(surf_man, surf); 2550e88f27b3Smrg} 2551e88f27b3Smrg 25526260e5d5Smrgdrm_public int 2553a884aba1Smrgradeon_surface_best(struct radeon_surface_manager *surf_man, 2554a884aba1Smrg struct radeon_surface *surf) 2555e88f27b3Smrg{ 2556e88f27b3Smrg unsigned mode, type; 2557e88f27b3Smrg int r; 2558e88f27b3Smrg 2559e88f27b3Smrg type = RADEON_SURF_GET(surf->flags, TYPE); 2560e88f27b3Smrg mode = RADEON_SURF_GET(surf->flags, MODE); 2561e88f27b3Smrg 2562e88f27b3Smrg r = radeon_surface_sanity(surf_man, surf, type, mode); 2563e88f27b3Smrg if (r) { 2564e88f27b3Smrg return r; 2565e88f27b3Smrg } 2566e88f27b3Smrg return surf_man->surface_best(surf_man, surf); 2567e88f27b3Smrg} 2568