xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/panfrost/pan_device.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2019 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *   Alyssa Rosenzweig <[email protected]>
25  */
26 
27 #include <xf86drm.h>
28 
29 #include "drm-uapi/panfrost_drm.h"
30 #include "util/hash_table.h"
31 #include "util/macros.h"
32 #include "util/u_math.h"
33 #include "util/u_thread.h"
34 #include "pan_bo.h"
35 #include "pan_device.h"
36 #include "pan_encoder.h"
37 #include "pan_samples.h"
38 #include "pan_texture.h"
39 #include "pan_util.h"
40 #include "wrap.h"
41 
42 /* DRM_PANFROST_PARAM_TEXTURE_FEATURES0 will return a bitmask of supported
43  * compressed formats, so we offer a helper to test if a format is supported */
44 
45 bool
panfrost_supports_compressed_format(struct panfrost_device * dev,unsigned fmt)46 panfrost_supports_compressed_format(struct panfrost_device *dev, unsigned fmt)
47 {
48    if (MALI_EXTRACT_TYPE(fmt) != MALI_FORMAT_COMPRESSED)
49       return true;
50 
51    unsigned idx = fmt & ~MALI_FORMAT_COMPRESSED;
52    assert(idx < 32);
53 
54    return panfrost_query_compressed_formats(&dev->kmod.props) & (1 << idx);
55 }
56 
57 void
panfrost_open_device(void * memctx,int fd,struct panfrost_device * dev)58 panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev)
59 {
60    dev->memctx = memctx;
61 
62    dev->kmod.dev = pan_kmod_dev_create(fd, PAN_KMOD_DEV_FLAG_OWNS_FD, NULL);
63    if (!dev->kmod.dev) {
64       close(fd);
65       return;
66    }
67 
68    pan_kmod_dev_query_props(dev->kmod.dev, &dev->kmod.props);
69 
70    dev->arch = pan_arch(dev->kmod.props.gpu_prod_id);
71    dev->model = panfrost_get_model(dev->kmod.props.gpu_prod_id,
72                                    dev->kmod.props.gpu_variant);
73 
74    /* If we don't recognize the model, bail early */
75    if (!dev->model)
76       goto err_free_kmod_dev;
77 
78    /* 48bit address space max, with the lower 32MB reserved. We clamp
79     * things so it matches kmod VA range limitations.
80     */
81    uint64_t user_va_start =
82       panfrost_clamp_to_usable_va_range(dev->kmod.dev, PAN_VA_USER_START);
83    uint64_t user_va_end =
84       panfrost_clamp_to_usable_va_range(dev->kmod.dev, PAN_VA_USER_END);
85 
86    dev->kmod.vm = pan_kmod_vm_create(
87       dev->kmod.dev, PAN_KMOD_VM_FLAG_AUTO_VA | PAN_KMOD_VM_FLAG_TRACK_ACTIVITY,
88       user_va_start, user_va_end - user_va_start);
89    if (!dev->kmod.vm)
90       goto err_free_kmod_dev;
91 
92    dev->core_count =
93       panfrost_query_core_count(&dev->kmod.props, &dev->core_id_range);
94    dev->thread_tls_alloc = panfrost_query_thread_tls_alloc(&dev->kmod.props);
95    dev->optimal_tib_size = panfrost_query_optimal_tib_size(dev->model);
96    dev->compressed_formats =
97       panfrost_query_compressed_formats(&dev->kmod.props);
98    dev->tiler_features = panfrost_query_tiler_features(&dev->kmod.props);
99    dev->has_afbc = panfrost_query_afbc(&dev->kmod.props);
100    dev->has_afrc = panfrost_query_afrc(&dev->kmod.props);
101    dev->formats = panfrost_format_table(dev->arch);
102    dev->blendable_formats = panfrost_blendable_format_table(dev->arch);
103 
104    util_sparse_array_init(&dev->bo_map, sizeof(struct panfrost_bo), 512);
105 
106    pthread_mutex_init(&dev->bo_cache.lock, NULL);
107    list_inithead(&dev->bo_cache.lru);
108 
109    for (unsigned i = 0; i < ARRAY_SIZE(dev->bo_cache.buckets); ++i)
110       list_inithead(&dev->bo_cache.buckets[i]);
111 
112    /* Initialize pandecode before we start allocating */
113    if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))
114       dev->decode_ctx = pandecode_create_context(!(dev->debug & PAN_DBG_TRACE));
115 
116    /* Tiler heap is internally required by the tiler, which can only be
117     * active for a single job chain at once, so a single heap can be
118     * shared across batches/contextes.
119     *
120     * Heap management is completely different on CSF HW, don't allocate the
121     * heap BO in that case.
122     */
123 
124    if (dev->arch < 10) {
125       dev->tiler_heap = panfrost_bo_create(
126          dev, 128 * 1024 * 1024, PAN_BO_INVISIBLE | PAN_BO_GROWABLE, "Tiler heap");
127       assert(dev->tiler_heap);
128    }
129 
130    pthread_mutex_init(&dev->submit_lock, NULL);
131 
132    /* Done once on init */
133    dev->sample_positions = panfrost_bo_create(
134       dev, panfrost_sample_positions_buffer_size(), 0, "Sample positions");
135    assert(dev->sample_positions);
136 
137    panfrost_upload_sample_positions(dev->sample_positions->ptr.cpu);
138    return;
139 
140 err_free_kmod_dev:
141    pan_kmod_dev_destroy(dev->kmod.dev);
142    dev->kmod.dev = NULL;
143 }
144 
145 void
panfrost_close_device(struct panfrost_device * dev)146 panfrost_close_device(struct panfrost_device *dev)
147 {
148    /* If we don't recognize the model, the rest of the device won't exist,
149     * we will have early-exited the device open.
150     */
151    if (dev->model) {
152       pthread_mutex_destroy(&dev->submit_lock);
153       panfrost_bo_unreference(dev->tiler_heap);
154       panfrost_bo_unreference(dev->sample_positions);
155       panfrost_bo_cache_evict_all(dev);
156       pthread_mutex_destroy(&dev->bo_cache.lock);
157       util_sparse_array_finish(&dev->bo_map);
158    }
159 
160    if (dev->kmod.vm)
161       pan_kmod_vm_destroy(dev->kmod.vm);
162 
163    if (dev->kmod.dev)
164       pan_kmod_dev_destroy(dev->kmod.dev);
165 }
166