1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3bf215546Sopenharmony_ci * Copyright © 2009 Joakim Sindholt <opensource@zhasha.com>
4bf215546Sopenharmony_ci * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
5bf215546Sopenharmony_ci * Copyright © 2015 Advanced Micro Devices, Inc.
6bf215546Sopenharmony_ci * All Rights Reserved.
7bf215546Sopenharmony_ci *
8bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
9bf215546Sopenharmony_ci * a copy of this software and associated documentation files (the
10bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
11bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
12bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
13bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
14bf215546Sopenharmony_ci * the following conditions:
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18bf215546Sopenharmony_ci * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19bf215546Sopenharmony_ci * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
20bf215546Sopenharmony_ci * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
24bf215546Sopenharmony_ci *
25bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
26bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
27bf215546Sopenharmony_ci * of the Software.
28bf215546Sopenharmony_ci */
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "amdgpu_cs.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "util/os_file.h"
33bf215546Sopenharmony_ci#include "util/os_misc.h"
34bf215546Sopenharmony_ci#include "util/u_cpu_detect.h"
35bf215546Sopenharmony_ci#include "util/u_hash_table.h"
36bf215546Sopenharmony_ci#include "util/hash_table.h"
37bf215546Sopenharmony_ci#include "util/xmlconfig.h"
38bf215546Sopenharmony_ci#include "drm-uapi/amdgpu_drm.h"
39bf215546Sopenharmony_ci#include <xf86drm.h>
40bf215546Sopenharmony_ci#include <stdio.h>
41bf215546Sopenharmony_ci#include <sys/stat.h>
42bf215546Sopenharmony_ci#include <fcntl.h>
43bf215546Sopenharmony_ci#include "ac_llvm_util.h"
44bf215546Sopenharmony_ci#include "sid.h"
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_cistatic struct hash_table *dev_tab = NULL;
47bf215546Sopenharmony_cistatic simple_mtx_t dev_tab_mutex = _SIMPLE_MTX_INITIALIZER_NP;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci#if DEBUG
50bf215546Sopenharmony_ciDEBUG_GET_ONCE_BOOL_OPTION(all_bos, "RADEON_ALL_BOS", false)
51bf215546Sopenharmony_ci#endif
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic void handle_env_var_force_family(struct amdgpu_winsys *ws)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci      const char *family = debug_get_option("SI_FORCE_FAMILY", NULL);
56bf215546Sopenharmony_ci      unsigned i;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci      if (!family)
59bf215546Sopenharmony_ci               return;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci      for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
62bf215546Sopenharmony_ci         if (!strcmp(family, ac_get_llvm_processor_name(i))) {
63bf215546Sopenharmony_ci            /* Override family and gfx_level. */
64bf215546Sopenharmony_ci            ws->info.family = i;
65bf215546Sopenharmony_ci            ws->info.name = "NOOP";
66bf215546Sopenharmony_ci            strcpy(ws->info.lowercase_name , "noop");
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci            if (i >= CHIP_GFX1100)
69bf215546Sopenharmony_ci               ws->info.gfx_level = GFX11;
70bf215546Sopenharmony_ci            else if (i >= CHIP_NAVI21)
71bf215546Sopenharmony_ci               ws->info.gfx_level = GFX10_3;
72bf215546Sopenharmony_ci            else if (i >= CHIP_NAVI10)
73bf215546Sopenharmony_ci               ws->info.gfx_level = GFX10;
74bf215546Sopenharmony_ci            else if (i >= CHIP_VEGA10)
75bf215546Sopenharmony_ci               ws->info.gfx_level = GFX9;
76bf215546Sopenharmony_ci            else if (i >= CHIP_TONGA)
77bf215546Sopenharmony_ci               ws->info.gfx_level = GFX8;
78bf215546Sopenharmony_ci            else if (i >= CHIP_BONAIRE)
79bf215546Sopenharmony_ci               ws->info.gfx_level = GFX7;
80bf215546Sopenharmony_ci            else
81bf215546Sopenharmony_ci               ws->info.gfx_level = GFX6;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci            /* Don't submit any IBs. */
84bf215546Sopenharmony_ci            setenv("RADEON_NOOP", "1", 1);
85bf215546Sopenharmony_ci            return;
86bf215546Sopenharmony_ci         }
87bf215546Sopenharmony_ci      }
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci      fprintf(stderr, "radeonsi: Unknown family: %s\n", family);
90bf215546Sopenharmony_ci      exit(1);
91bf215546Sopenharmony_ci}
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci/* Helper function to do the ioctls needed for setup and init. */
94bf215546Sopenharmony_cistatic bool do_winsys_init(struct amdgpu_winsys *ws,
95bf215546Sopenharmony_ci                           const struct pipe_screen_config *config,
96bf215546Sopenharmony_ci                           int fd)
97bf215546Sopenharmony_ci{
98bf215546Sopenharmony_ci   if (!ac_query_gpu_info(fd, ws->dev, &ws->info))
99bf215546Sopenharmony_ci      goto fail;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   /* TODO: Enable this once the kernel handles it efficiently. */
102bf215546Sopenharmony_ci   if (ws->info.has_dedicated_vram)
103bf215546Sopenharmony_ci      ws->info.has_local_buffers = false;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   handle_env_var_force_family(ws);
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   ws->addrlib = ac_addrlib_create(&ws->info, &ws->info.max_alignment);
108bf215546Sopenharmony_ci   if (!ws->addrlib) {
109bf215546Sopenharmony_ci      fprintf(stderr, "amdgpu: Cannot create addrlib.\n");
110bf215546Sopenharmony_ci      goto fail;
111bf215546Sopenharmony_ci   }
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   ws->check_vm = strstr(debug_get_option("R600_DEBUG", ""), "check_vm") != NULL ||
114bf215546Sopenharmony_ci                  strstr(debug_get_option("AMD_DEBUG", ""), "check_vm") != NULL;
115bf215546Sopenharmony_ci   ws->noop_cs = debug_get_bool_option("RADEON_NOOP", false);
116bf215546Sopenharmony_ci#if DEBUG
117bf215546Sopenharmony_ci   ws->debug_all_bos = debug_get_option_all_bos();
118bf215546Sopenharmony_ci#endif
119bf215546Sopenharmony_ci   ws->reserve_vmid = strstr(debug_get_option("R600_DEBUG", ""), "reserve_vmid") != NULL ||
120bf215546Sopenharmony_ci                      strstr(debug_get_option("AMD_DEBUG", ""), "reserve_vmid") != NULL ||
121bf215546Sopenharmony_ci                      strstr(debug_get_option("AMD_DEBUG", ""), "sqtt") != NULL;
122bf215546Sopenharmony_ci   ws->zero_all_vram_allocs = strstr(debug_get_option("R600_DEBUG", ""), "zerovram") != NULL ||
123bf215546Sopenharmony_ci                              driQueryOptionb(config->options, "radeonsi_zerovram");
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   return true;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_cifail:
128bf215546Sopenharmony_ci   amdgpu_device_deinitialize(ws->dev);
129bf215546Sopenharmony_ci   ws->dev = NULL;
130bf215546Sopenharmony_ci   return false;
131bf215546Sopenharmony_ci}
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_cistatic void do_winsys_deinit(struct amdgpu_winsys *ws)
134bf215546Sopenharmony_ci{
135bf215546Sopenharmony_ci   if (ws->reserve_vmid)
136bf215546Sopenharmony_ci      amdgpu_vm_unreserve_vmid(ws->dev, 0);
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   if (util_queue_is_initialized(&ws->cs_queue))
139bf215546Sopenharmony_ci      util_queue_destroy(&ws->cs_queue);
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   simple_mtx_destroy(&ws->bo_fence_lock);
142bf215546Sopenharmony_ci   for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++) {
143bf215546Sopenharmony_ci      if (ws->bo_slabs[i].groups)
144bf215546Sopenharmony_ci         pb_slabs_deinit(&ws->bo_slabs[i]);
145bf215546Sopenharmony_ci   }
146bf215546Sopenharmony_ci   pb_cache_deinit(&ws->bo_cache);
147bf215546Sopenharmony_ci   _mesa_hash_table_destroy(ws->bo_export_table, NULL);
148bf215546Sopenharmony_ci   simple_mtx_destroy(&ws->sws_list_lock);
149bf215546Sopenharmony_ci#if DEBUG
150bf215546Sopenharmony_ci   simple_mtx_destroy(&ws->global_bo_list_lock);
151bf215546Sopenharmony_ci#endif
152bf215546Sopenharmony_ci   simple_mtx_destroy(&ws->bo_export_table_lock);
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   ac_addrlib_destroy(ws->addrlib);
155bf215546Sopenharmony_ci   amdgpu_device_deinitialize(ws->dev);
156bf215546Sopenharmony_ci   FREE(ws);
157bf215546Sopenharmony_ci}
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_cistatic void amdgpu_winsys_destroy_locked(struct radeon_winsys *rws, bool locked)
160bf215546Sopenharmony_ci{
161bf215546Sopenharmony_ci   struct amdgpu_screen_winsys *sws = amdgpu_screen_winsys(rws);
162bf215546Sopenharmony_ci   struct amdgpu_winsys *ws = sws->aws;
163bf215546Sopenharmony_ci   bool destroy;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   /* When the reference counter drops to zero, remove the device pointer
166bf215546Sopenharmony_ci    * from the table.
167bf215546Sopenharmony_ci    * This must happen while the mutex is locked, so that
168bf215546Sopenharmony_ci    * amdgpu_winsys_create in another thread doesn't get the winsys
169bf215546Sopenharmony_ci    * from the table when the counter drops to 0.
170bf215546Sopenharmony_ci    */
171bf215546Sopenharmony_ci   if (!locked)
172bf215546Sopenharmony_ci      simple_mtx_lock(&dev_tab_mutex);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   destroy = pipe_reference(&ws->reference, NULL);
175bf215546Sopenharmony_ci   if (destroy && dev_tab) {
176bf215546Sopenharmony_ci      _mesa_hash_table_remove_key(dev_tab, ws->dev);
177bf215546Sopenharmony_ci      if (_mesa_hash_table_num_entries(dev_tab) == 0) {
178bf215546Sopenharmony_ci         _mesa_hash_table_destroy(dev_tab, NULL);
179bf215546Sopenharmony_ci         dev_tab = NULL;
180bf215546Sopenharmony_ci      }
181bf215546Sopenharmony_ci   }
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci   if (!locked)
184bf215546Sopenharmony_ci      simple_mtx_unlock(&dev_tab_mutex);
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci   if (destroy)
187bf215546Sopenharmony_ci      do_winsys_deinit(ws);
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci   close(sws->fd);
190bf215546Sopenharmony_ci   FREE(rws);
191bf215546Sopenharmony_ci}
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_cistatic void amdgpu_winsys_destroy(struct radeon_winsys *rws)
194bf215546Sopenharmony_ci{
195bf215546Sopenharmony_ci   amdgpu_winsys_destroy_locked(rws, false);
196bf215546Sopenharmony_ci}
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_cistatic void amdgpu_winsys_query_info(struct radeon_winsys *rws,
199bf215546Sopenharmony_ci                                     struct radeon_info *info,
200bf215546Sopenharmony_ci                                     bool enable_smart_access_memory,
201bf215546Sopenharmony_ci                                     bool disable_smart_access_memory)
202bf215546Sopenharmony_ci{
203bf215546Sopenharmony_ci   struct amdgpu_winsys *ws = amdgpu_winsys(rws);
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci   if (disable_smart_access_memory)
206bf215546Sopenharmony_ci      ws->info.smart_access_memory = false;
207bf215546Sopenharmony_ci   else if (enable_smart_access_memory && ws->info.all_vram_visible)
208bf215546Sopenharmony_ci      ws->info.smart_access_memory = true;
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci   *info = ws->info;
211bf215546Sopenharmony_ci}
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_cistatic bool amdgpu_cs_request_feature(struct radeon_cmdbuf *rcs,
214bf215546Sopenharmony_ci                                      enum radeon_feature_id fid,
215bf215546Sopenharmony_ci                                      bool enable)
216bf215546Sopenharmony_ci{
217bf215546Sopenharmony_ci   return false;
218bf215546Sopenharmony_ci}
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_cistatic uint64_t amdgpu_query_value(struct radeon_winsys *rws,
221bf215546Sopenharmony_ci                                   enum radeon_value_id value)
222bf215546Sopenharmony_ci{
223bf215546Sopenharmony_ci   struct amdgpu_winsys *ws = amdgpu_winsys(rws);
224bf215546Sopenharmony_ci   struct amdgpu_heap_info heap;
225bf215546Sopenharmony_ci   uint64_t retval = 0;
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci   switch (value) {
228bf215546Sopenharmony_ci   case RADEON_REQUESTED_VRAM_MEMORY:
229bf215546Sopenharmony_ci      return ws->allocated_vram;
230bf215546Sopenharmony_ci   case RADEON_REQUESTED_GTT_MEMORY:
231bf215546Sopenharmony_ci      return ws->allocated_gtt;
232bf215546Sopenharmony_ci   case RADEON_MAPPED_VRAM:
233bf215546Sopenharmony_ci      return ws->mapped_vram;
234bf215546Sopenharmony_ci   case RADEON_MAPPED_GTT:
235bf215546Sopenharmony_ci      return ws->mapped_gtt;
236bf215546Sopenharmony_ci   case RADEON_SLAB_WASTED_VRAM:
237bf215546Sopenharmony_ci      return ws->slab_wasted_vram;
238bf215546Sopenharmony_ci   case RADEON_SLAB_WASTED_GTT:
239bf215546Sopenharmony_ci      return ws->slab_wasted_gtt;
240bf215546Sopenharmony_ci   case RADEON_BUFFER_WAIT_TIME_NS:
241bf215546Sopenharmony_ci      return ws->buffer_wait_time;
242bf215546Sopenharmony_ci   case RADEON_NUM_MAPPED_BUFFERS:
243bf215546Sopenharmony_ci      return ws->num_mapped_buffers;
244bf215546Sopenharmony_ci   case RADEON_TIMESTAMP:
245bf215546Sopenharmony_ci      amdgpu_query_info(ws->dev, AMDGPU_INFO_TIMESTAMP, 8, &retval);
246bf215546Sopenharmony_ci      return retval;
247bf215546Sopenharmony_ci   case RADEON_NUM_GFX_IBS:
248bf215546Sopenharmony_ci      return ws->num_gfx_IBs;
249bf215546Sopenharmony_ci   case RADEON_NUM_SDMA_IBS:
250bf215546Sopenharmony_ci      return ws->num_sdma_IBs;
251bf215546Sopenharmony_ci   case RADEON_GFX_BO_LIST_COUNTER:
252bf215546Sopenharmony_ci      return ws->gfx_bo_list_counter;
253bf215546Sopenharmony_ci   case RADEON_GFX_IB_SIZE_COUNTER:
254bf215546Sopenharmony_ci      return ws->gfx_ib_size_counter;
255bf215546Sopenharmony_ci   case RADEON_NUM_BYTES_MOVED:
256bf215546Sopenharmony_ci      amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_BYTES_MOVED, 8, &retval);
257bf215546Sopenharmony_ci      return retval;
258bf215546Sopenharmony_ci   case RADEON_NUM_EVICTIONS:
259bf215546Sopenharmony_ci      amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_EVICTIONS, 8, &retval);
260bf215546Sopenharmony_ci      return retval;
261bf215546Sopenharmony_ci   case RADEON_NUM_VRAM_CPU_PAGE_FAULTS:
262bf215546Sopenharmony_ci      amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS, 8, &retval);
263bf215546Sopenharmony_ci      return retval;
264bf215546Sopenharmony_ci   case RADEON_VRAM_USAGE:
265bf215546Sopenharmony_ci      amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM, 0, &heap);
266bf215546Sopenharmony_ci      return heap.heap_usage;
267bf215546Sopenharmony_ci   case RADEON_VRAM_VIS_USAGE:
268bf215546Sopenharmony_ci      amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM,
269bf215546Sopenharmony_ci                             AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, &heap);
270bf215546Sopenharmony_ci      return heap.heap_usage;
271bf215546Sopenharmony_ci   case RADEON_GTT_USAGE:
272bf215546Sopenharmony_ci      amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_GTT, 0, &heap);
273bf215546Sopenharmony_ci      return heap.heap_usage;
274bf215546Sopenharmony_ci   case RADEON_GPU_TEMPERATURE:
275bf215546Sopenharmony_ci      amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GPU_TEMP, 4, &retval);
276bf215546Sopenharmony_ci      return retval;
277bf215546Sopenharmony_ci   case RADEON_CURRENT_SCLK:
278bf215546Sopenharmony_ci      amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_SCLK, 4, &retval);
279bf215546Sopenharmony_ci      return retval;
280bf215546Sopenharmony_ci   case RADEON_CURRENT_MCLK:
281bf215546Sopenharmony_ci      amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_MCLK, 4, &retval);
282bf215546Sopenharmony_ci      return retval;
283bf215546Sopenharmony_ci   case RADEON_CS_THREAD_TIME:
284bf215546Sopenharmony_ci      return util_queue_get_thread_time_nano(&ws->cs_queue, 0);
285bf215546Sopenharmony_ci   }
286bf215546Sopenharmony_ci   return 0;
287bf215546Sopenharmony_ci}
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_cistatic bool amdgpu_read_registers(struct radeon_winsys *rws,
290bf215546Sopenharmony_ci                                  unsigned reg_offset,
291bf215546Sopenharmony_ci                                  unsigned num_registers, uint32_t *out)
292bf215546Sopenharmony_ci{
293bf215546Sopenharmony_ci   struct amdgpu_winsys *ws = amdgpu_winsys(rws);
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci   return amdgpu_read_mm_registers(ws->dev, reg_offset / 4, num_registers,
296bf215546Sopenharmony_ci                                   0xffffffff, 0, out) == 0;
297bf215546Sopenharmony_ci}
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_cistatic bool amdgpu_winsys_unref(struct radeon_winsys *rws)
300bf215546Sopenharmony_ci{
301bf215546Sopenharmony_ci   struct amdgpu_screen_winsys *sws = amdgpu_screen_winsys(rws);
302bf215546Sopenharmony_ci   struct amdgpu_winsys *aws = sws->aws;
303bf215546Sopenharmony_ci   bool ret;
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci   simple_mtx_lock(&aws->sws_list_lock);
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_ci   ret = pipe_reference(&sws->reference, NULL);
308bf215546Sopenharmony_ci   if (ret) {
309bf215546Sopenharmony_ci      struct amdgpu_screen_winsys **sws_iter;
310bf215546Sopenharmony_ci      struct amdgpu_winsys *aws = sws->aws;
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci      /* Remove this amdgpu_screen_winsys from amdgpu_winsys' list, so that
313bf215546Sopenharmony_ci       * amdgpu_winsys_create can't re-use it anymore
314bf215546Sopenharmony_ci       */
315bf215546Sopenharmony_ci      for (sws_iter = &aws->sws_list; *sws_iter; sws_iter = &(*sws_iter)->next) {
316bf215546Sopenharmony_ci         if (*sws_iter == sws) {
317bf215546Sopenharmony_ci            *sws_iter = sws->next;
318bf215546Sopenharmony_ci            break;
319bf215546Sopenharmony_ci         }
320bf215546Sopenharmony_ci      }
321bf215546Sopenharmony_ci   }
322bf215546Sopenharmony_ci
323bf215546Sopenharmony_ci   simple_mtx_unlock(&aws->sws_list_lock);
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci   if (ret && sws->kms_handles) {
326bf215546Sopenharmony_ci      struct drm_gem_close args;
327bf215546Sopenharmony_ci
328bf215546Sopenharmony_ci      hash_table_foreach(sws->kms_handles, entry) {
329bf215546Sopenharmony_ci         args.handle = (uintptr_t)entry->data;
330bf215546Sopenharmony_ci         drmIoctl(sws->fd, DRM_IOCTL_GEM_CLOSE, &args);
331bf215546Sopenharmony_ci      }
332bf215546Sopenharmony_ci      _mesa_hash_table_destroy(sws->kms_handles, NULL);
333bf215546Sopenharmony_ci   }
334bf215546Sopenharmony_ci
335bf215546Sopenharmony_ci   return ret;
336bf215546Sopenharmony_ci}
337bf215546Sopenharmony_ci
338bf215546Sopenharmony_cistatic void amdgpu_pin_threads_to_L3_cache(struct radeon_winsys *rws,
339bf215546Sopenharmony_ci                                           unsigned cache)
340bf215546Sopenharmony_ci{
341bf215546Sopenharmony_ci   struct amdgpu_winsys *ws = amdgpu_winsys(rws);
342bf215546Sopenharmony_ci
343bf215546Sopenharmony_ci   util_set_thread_affinity(ws->cs_queue.threads[0],
344bf215546Sopenharmony_ci                            util_get_cpu_caps()->L3_affinity_mask[cache],
345bf215546Sopenharmony_ci                            NULL, util_get_cpu_caps()->num_cpu_mask_bits);
346bf215546Sopenharmony_ci}
347bf215546Sopenharmony_ci
348bf215546Sopenharmony_cistatic uint32_t kms_handle_hash(const void *key)
349bf215546Sopenharmony_ci{
350bf215546Sopenharmony_ci   const struct amdgpu_winsys_bo *bo = key;
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ci   return bo->u.real.kms_handle;
353bf215546Sopenharmony_ci}
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_cistatic bool kms_handle_equals(const void *a, const void *b)
356bf215546Sopenharmony_ci{
357bf215546Sopenharmony_ci   return a == b;
358bf215546Sopenharmony_ci}
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_cistatic bool amdgpu_cs_is_secure(struct radeon_cmdbuf *rcs)
361bf215546Sopenharmony_ci{
362bf215546Sopenharmony_ci   struct amdgpu_cs *cs = amdgpu_cs(rcs);
363bf215546Sopenharmony_ci   return cs->csc->secure;
364bf215546Sopenharmony_ci}
365bf215546Sopenharmony_ci
366bf215546Sopenharmony_ciPUBLIC struct radeon_winsys *
367bf215546Sopenharmony_ciamdgpu_winsys_create(int fd, const struct pipe_screen_config *config,
368bf215546Sopenharmony_ci		     radeon_screen_create_t screen_create)
369bf215546Sopenharmony_ci{
370bf215546Sopenharmony_ci   struct amdgpu_screen_winsys *ws;
371bf215546Sopenharmony_ci   struct amdgpu_winsys *aws;
372bf215546Sopenharmony_ci   amdgpu_device_handle dev;
373bf215546Sopenharmony_ci   uint32_t drm_major, drm_minor;
374bf215546Sopenharmony_ci   int r;
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci   ws = CALLOC_STRUCT(amdgpu_screen_winsys);
377bf215546Sopenharmony_ci   if (!ws)
378bf215546Sopenharmony_ci      return NULL;
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci   pipe_reference_init(&ws->reference, 1);
381bf215546Sopenharmony_ci   ws->fd = os_dupfd_cloexec(fd);
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ci   /* Look up the winsys from the dev table. */
384bf215546Sopenharmony_ci   simple_mtx_lock(&dev_tab_mutex);
385bf215546Sopenharmony_ci   if (!dev_tab)
386bf215546Sopenharmony_ci      dev_tab = util_hash_table_create_ptr_keys();
387bf215546Sopenharmony_ci
388bf215546Sopenharmony_ci   /* Initialize the amdgpu device. This should always return the same pointer
389bf215546Sopenharmony_ci    * for the same fd. */
390bf215546Sopenharmony_ci   r = amdgpu_device_initialize(ws->fd, &drm_major, &drm_minor, &dev);
391bf215546Sopenharmony_ci   if (r) {
392bf215546Sopenharmony_ci      fprintf(stderr, "amdgpu: amdgpu_device_initialize failed.\n");
393bf215546Sopenharmony_ci      goto fail;
394bf215546Sopenharmony_ci   }
395bf215546Sopenharmony_ci
396bf215546Sopenharmony_ci   /* Lookup a winsys if we have already created one for this device. */
397bf215546Sopenharmony_ci   aws = util_hash_table_get(dev_tab, dev);
398bf215546Sopenharmony_ci   if (aws) {
399bf215546Sopenharmony_ci      struct amdgpu_screen_winsys *sws_iter;
400bf215546Sopenharmony_ci
401bf215546Sopenharmony_ci      /* Release the device handle, because we don't need it anymore.
402bf215546Sopenharmony_ci       * This function is returning an existing winsys instance, which
403bf215546Sopenharmony_ci       * has its own device handle.
404bf215546Sopenharmony_ci       */
405bf215546Sopenharmony_ci      amdgpu_device_deinitialize(dev);
406bf215546Sopenharmony_ci
407bf215546Sopenharmony_ci      simple_mtx_lock(&aws->sws_list_lock);
408bf215546Sopenharmony_ci      for (sws_iter = aws->sws_list; sws_iter; sws_iter = sws_iter->next) {
409bf215546Sopenharmony_ci         r = os_same_file_description(sws_iter->fd, ws->fd);
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci         if (r == 0) {
412bf215546Sopenharmony_ci            close(ws->fd);
413bf215546Sopenharmony_ci            FREE(ws);
414bf215546Sopenharmony_ci            ws = sws_iter;
415bf215546Sopenharmony_ci            pipe_reference(NULL, &ws->reference);
416bf215546Sopenharmony_ci            simple_mtx_unlock(&aws->sws_list_lock);
417bf215546Sopenharmony_ci            goto unlock;
418bf215546Sopenharmony_ci         } else if (r < 0) {
419bf215546Sopenharmony_ci            static bool logged;
420bf215546Sopenharmony_ci
421bf215546Sopenharmony_ci            if (!logged) {
422bf215546Sopenharmony_ci               os_log_message("amdgpu: os_same_file_description couldn't "
423bf215546Sopenharmony_ci                              "determine if two DRM fds reference the same "
424bf215546Sopenharmony_ci                              "file description.\n"
425bf215546Sopenharmony_ci                              "If they do, bad things may happen!\n");
426bf215546Sopenharmony_ci               logged = true;
427bf215546Sopenharmony_ci            }
428bf215546Sopenharmony_ci         }
429bf215546Sopenharmony_ci      }
430bf215546Sopenharmony_ci      simple_mtx_unlock(&aws->sws_list_lock);
431bf215546Sopenharmony_ci
432bf215546Sopenharmony_ci      ws->kms_handles = _mesa_hash_table_create(NULL, kms_handle_hash,
433bf215546Sopenharmony_ci                                                kms_handle_equals);
434bf215546Sopenharmony_ci      if (!ws->kms_handles)
435bf215546Sopenharmony_ci         goto fail;
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_ci      pipe_reference(NULL, &aws->reference);
438bf215546Sopenharmony_ci   } else {
439bf215546Sopenharmony_ci      /* Create a new winsys. */
440bf215546Sopenharmony_ci      aws = CALLOC_STRUCT(amdgpu_winsys);
441bf215546Sopenharmony_ci      if (!aws)
442bf215546Sopenharmony_ci         goto fail;
443bf215546Sopenharmony_ci
444bf215546Sopenharmony_ci      aws->dev = dev;
445bf215546Sopenharmony_ci      aws->fd = ws->fd;
446bf215546Sopenharmony_ci      aws->info.drm_major = drm_major;
447bf215546Sopenharmony_ci      aws->info.drm_minor = drm_minor;
448bf215546Sopenharmony_ci      aws->dummy_ws.aws = aws; /* only the pointer is used */
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_ci      if (!do_winsys_init(aws, config, fd))
451bf215546Sopenharmony_ci         goto fail_alloc;
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_ci      /* Create managers. */
454bf215546Sopenharmony_ci      pb_cache_init(&aws->bo_cache, RADEON_NUM_HEAPS,
455bf215546Sopenharmony_ci                    500000, aws->check_vm ? 1.0f : 2.0f, 0,
456bf215546Sopenharmony_ci                    ((uint64_t)aws->info.vram_size_kb + aws->info.gart_size_kb) * 1024 / 8, aws,
457bf215546Sopenharmony_ci                    /* Cast to void* because one of the function parameters
458bf215546Sopenharmony_ci                     * is a struct pointer instead of void*. */
459bf215546Sopenharmony_ci                    (void*)amdgpu_bo_destroy, (void*)amdgpu_bo_can_reclaim);
460bf215546Sopenharmony_ci
461bf215546Sopenharmony_ci      unsigned min_slab_order = 8;  /* 256 bytes */
462bf215546Sopenharmony_ci      unsigned max_slab_order = 20; /* 1 MB (slab size = 2 MB) */
463bf215546Sopenharmony_ci      unsigned num_slab_orders_per_allocator = (max_slab_order - min_slab_order) /
464bf215546Sopenharmony_ci                                               NUM_SLAB_ALLOCATORS;
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_ci      /* Divide the size order range among slab managers. */
467bf215546Sopenharmony_ci      for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++) {
468bf215546Sopenharmony_ci         unsigned min_order = min_slab_order;
469bf215546Sopenharmony_ci         unsigned max_order = MIN2(min_order + num_slab_orders_per_allocator,
470bf215546Sopenharmony_ci                                   max_slab_order);
471bf215546Sopenharmony_ci
472bf215546Sopenharmony_ci         if (!pb_slabs_init(&aws->bo_slabs[i],
473bf215546Sopenharmony_ci                            min_order, max_order,
474bf215546Sopenharmony_ci                            RADEON_NUM_HEAPS, true,
475bf215546Sopenharmony_ci                            aws,
476bf215546Sopenharmony_ci                            amdgpu_bo_can_reclaim_slab,
477bf215546Sopenharmony_ci                            amdgpu_bo_slab_alloc,
478bf215546Sopenharmony_ci                            /* Cast to void* because one of the function parameters
479bf215546Sopenharmony_ci                             * is a struct pointer instead of void*. */
480bf215546Sopenharmony_ci                            (void*)amdgpu_bo_slab_free)) {
481bf215546Sopenharmony_ci            amdgpu_winsys_destroy(&ws->base);
482bf215546Sopenharmony_ci            simple_mtx_unlock(&dev_tab_mutex);
483bf215546Sopenharmony_ci            return NULL;
484bf215546Sopenharmony_ci         }
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_ci         min_slab_order = max_order + 1;
487bf215546Sopenharmony_ci      }
488bf215546Sopenharmony_ci
489bf215546Sopenharmony_ci      aws->info.min_alloc_size = 1 << aws->bo_slabs[0].min_order;
490bf215546Sopenharmony_ci
491bf215546Sopenharmony_ci      /* init reference */
492bf215546Sopenharmony_ci      pipe_reference_init(&aws->reference, 1);
493bf215546Sopenharmony_ci#if DEBUG
494bf215546Sopenharmony_ci      list_inithead(&aws->global_bo_list);
495bf215546Sopenharmony_ci#endif
496bf215546Sopenharmony_ci      aws->bo_export_table = util_hash_table_create_ptr_keys();
497bf215546Sopenharmony_ci
498bf215546Sopenharmony_ci      (void) simple_mtx_init(&aws->sws_list_lock, mtx_plain);
499bf215546Sopenharmony_ci#if DEBUG
500bf215546Sopenharmony_ci      (void) simple_mtx_init(&aws->global_bo_list_lock, mtx_plain);
501bf215546Sopenharmony_ci#endif
502bf215546Sopenharmony_ci      (void) simple_mtx_init(&aws->bo_fence_lock, mtx_plain);
503bf215546Sopenharmony_ci      (void) simple_mtx_init(&aws->bo_export_table_lock, mtx_plain);
504bf215546Sopenharmony_ci
505bf215546Sopenharmony_ci      if (!util_queue_init(&aws->cs_queue, "cs", 8, 1,
506bf215546Sopenharmony_ci                           UTIL_QUEUE_INIT_RESIZE_IF_FULL, NULL)) {
507bf215546Sopenharmony_ci         amdgpu_winsys_destroy(&ws->base);
508bf215546Sopenharmony_ci         simple_mtx_unlock(&dev_tab_mutex);
509bf215546Sopenharmony_ci         return NULL;
510bf215546Sopenharmony_ci      }
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci      _mesa_hash_table_insert(dev_tab, dev, aws);
513bf215546Sopenharmony_ci
514bf215546Sopenharmony_ci      if (aws->reserve_vmid) {
515bf215546Sopenharmony_ci         r = amdgpu_vm_reserve_vmid(dev, 0);
516bf215546Sopenharmony_ci         if (r) {
517bf215546Sopenharmony_ci            amdgpu_winsys_destroy(&ws->base);
518bf215546Sopenharmony_ci            simple_mtx_unlock(&dev_tab_mutex);
519bf215546Sopenharmony_ci            return NULL;
520bf215546Sopenharmony_ci         }
521bf215546Sopenharmony_ci      }
522bf215546Sopenharmony_ci   }
523bf215546Sopenharmony_ci
524bf215546Sopenharmony_ci   ws->aws = aws;
525bf215546Sopenharmony_ci
526bf215546Sopenharmony_ci   /* Set functions. */
527bf215546Sopenharmony_ci   ws->base.unref = amdgpu_winsys_unref;
528bf215546Sopenharmony_ci   ws->base.destroy = amdgpu_winsys_destroy;
529bf215546Sopenharmony_ci   ws->base.query_info = amdgpu_winsys_query_info;
530bf215546Sopenharmony_ci   ws->base.cs_request_feature = amdgpu_cs_request_feature;
531bf215546Sopenharmony_ci   ws->base.query_value = amdgpu_query_value;
532bf215546Sopenharmony_ci   ws->base.read_registers = amdgpu_read_registers;
533bf215546Sopenharmony_ci   ws->base.pin_threads_to_L3_cache = amdgpu_pin_threads_to_L3_cache;
534bf215546Sopenharmony_ci   ws->base.cs_is_secure = amdgpu_cs_is_secure;
535bf215546Sopenharmony_ci
536bf215546Sopenharmony_ci   amdgpu_bo_init_functions(ws);
537bf215546Sopenharmony_ci   amdgpu_cs_init_functions(ws);
538bf215546Sopenharmony_ci   amdgpu_surface_init_functions(ws);
539bf215546Sopenharmony_ci
540bf215546Sopenharmony_ci   simple_mtx_lock(&aws->sws_list_lock);
541bf215546Sopenharmony_ci   ws->next = aws->sws_list;
542bf215546Sopenharmony_ci   aws->sws_list = ws;
543bf215546Sopenharmony_ci   simple_mtx_unlock(&aws->sws_list_lock);
544bf215546Sopenharmony_ci
545bf215546Sopenharmony_ci   /* Create the screen at the end. The winsys must be initialized
546bf215546Sopenharmony_ci    * completely.
547bf215546Sopenharmony_ci    *
548bf215546Sopenharmony_ci    * Alternatively, we could create the screen based on "ws->gen"
549bf215546Sopenharmony_ci    * and link all drivers into one binary blob. */
550bf215546Sopenharmony_ci   ws->base.screen = screen_create(&ws->base, config);
551bf215546Sopenharmony_ci   if (!ws->base.screen) {
552bf215546Sopenharmony_ci      amdgpu_winsys_destroy_locked(&ws->base, true);
553bf215546Sopenharmony_ci      simple_mtx_unlock(&dev_tab_mutex);
554bf215546Sopenharmony_ci      return NULL;
555bf215546Sopenharmony_ci   }
556bf215546Sopenharmony_ci
557bf215546Sopenharmony_ciunlock:
558bf215546Sopenharmony_ci   /* We must unlock the mutex once the winsys is fully initialized, so that
559bf215546Sopenharmony_ci    * other threads attempting to create the winsys from the same fd will
560bf215546Sopenharmony_ci    * get a fully initialized winsys and not just half-way initialized. */
561bf215546Sopenharmony_ci   simple_mtx_unlock(&dev_tab_mutex);
562bf215546Sopenharmony_ci
563bf215546Sopenharmony_ci   return &ws->base;
564bf215546Sopenharmony_ci
565bf215546Sopenharmony_cifail_alloc:
566bf215546Sopenharmony_ci   FREE(aws);
567bf215546Sopenharmony_cifail:
568bf215546Sopenharmony_ci   if (ws->kms_handles)
569bf215546Sopenharmony_ci      _mesa_hash_table_destroy(ws->kms_handles, NULL);
570bf215546Sopenharmony_ci   close(ws->fd);
571bf215546Sopenharmony_ci   FREE(ws);
572bf215546Sopenharmony_ci   simple_mtx_unlock(&dev_tab_mutex);
573bf215546Sopenharmony_ci   return NULL;
574bf215546Sopenharmony_ci}
575