1 /*
2  * Copyright © 2016 Red Hat.
3  * Copyright © 2016 Bas Nieuwenhuizen
4  *
5  * based on amdgpu winsys.
6  * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
7  * Copyright © 2015 Advanced Micro Devices, Inc.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26  * IN THE SOFTWARE.
27  */
28 
29 #include "util/bitset.h"
30 #include "radv_amdgpu_surface.h"
31 #include "radv_amdgpu_winsys.h"
32 #include "radv_private.h"
33 #include "sid.h"
34 
35 #include "ac_surface.h"
36 
37 static int
radv_amdgpu_surface_sanity(const struct ac_surf_info * surf_info,const struct radeon_surf * surf)38 radv_amdgpu_surface_sanity(const struct ac_surf_info *surf_info, const struct radeon_surf *surf)
39 {
40    unsigned type = RADEON_SURF_GET(surf->flags, TYPE);
41 
42    if (!surf->blk_w || !surf->blk_h)
43       return -EINVAL;
44 
45    switch (type) {
46    case RADEON_SURF_TYPE_1D:
47       if (surf_info->height > 1)
48          return -EINVAL;
49       FALLTHROUGH;
50    case RADEON_SURF_TYPE_2D:
51    case RADEON_SURF_TYPE_CUBEMAP:
52       if (surf_info->depth > 1 || surf_info->array_size > 1)
53          return -EINVAL;
54       break;
55    case RADEON_SURF_TYPE_3D:
56       if (surf_info->array_size > 1)
57          return -EINVAL;
58       break;
59    case RADEON_SURF_TYPE_1D_ARRAY:
60       if (surf_info->height > 1)
61          return -EINVAL;
62       FALLTHROUGH;
63    case RADEON_SURF_TYPE_2D_ARRAY:
64       if (surf_info->depth > 1)
65          return -EINVAL;
66       break;
67    default:
68       return -EINVAL;
69    }
70    return 0;
71 }
72 
73 static int
radv_amdgpu_winsys_surface_init(struct radeon_winsys * _ws,const struct ac_surf_info * surf_info,struct radeon_surf * surf)74 radv_amdgpu_winsys_surface_init(struct radeon_winsys *_ws, const struct ac_surf_info *surf_info,
75                                 struct radeon_surf *surf)
76 {
77    struct radv_amdgpu_winsys *ws = radv_amdgpu_winsys(_ws);
78    unsigned mode, type;
79    int r;
80 
81    r = radv_amdgpu_surface_sanity(surf_info, surf);
82    if (r)
83       return r;
84 
85    type = RADEON_SURF_GET(surf->flags, TYPE);
86    mode = RADEON_SURF_GET(surf->flags, MODE);
87 
88    struct ac_surf_config config;
89 
90    memcpy(&config.info, surf_info, sizeof(config.info));
91    config.is_1d = type == RADEON_SURF_TYPE_1D || type == RADEON_SURF_TYPE_1D_ARRAY;
92    config.is_3d = type == RADEON_SURF_TYPE_3D;
93    config.is_cube = type == RADEON_SURF_TYPE_CUBEMAP;
94 
95    return ac_compute_surface(ws->addrlib, &ws->info, &config, mode, surf);
96 }
97 
98 void
radv_amdgpu_surface_init_functions(struct radv_amdgpu_winsys * ws)99 radv_amdgpu_surface_init_functions(struct radv_amdgpu_winsys *ws)
100 {
101    ws->base.surface_init = radv_amdgpu_winsys_surface_init;
102 }
103