~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/broadcom/vulkan/v3dv_bo.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2019 Raspberry Pi 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
20
 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
 
 * IN THE SOFTWARE.
22
 
 */
23
 
 
24
 
#include "v3dv_private.h"
25
 
 
26
 
#include <errno.h>
27
 
#include <sys/mman.h>
28
 
 
29
 
#include "drm-uapi/v3d_drm.h"
30
 
#include "util/u_memory.h"
31
 
 
32
 
/* Default max size of the bo cache, in MB.
33
 
 *
34
 
 * FIXME: we got this value when testing some apps using the rpi4 with 4GB,
35
 
 * but it should depend on the total amount of RAM. But for that we would need
36
 
 * to test on real hw with different amount of RAM. Using this value for now.
37
 
 */
38
 
#define DEFAULT_MAX_BO_CACHE_SIZE 512
39
 
 
40
 
/* Discarded to use a V3D_DEBUG for this, as it would mean adding a run-time
41
 
 * check for most of the calls
42
 
 */
43
 
static const bool dump_stats = false;
44
 
 
45
 
static void
46
 
bo_dump_stats(struct v3dv_device *device)
47
 
{
48
 
   struct v3dv_bo_cache *cache = &device->bo_cache;
49
 
 
50
 
   fprintf(stderr, "  BOs allocated:   %d\n", device->bo_count);
51
 
   fprintf(stderr, "  BOs size:        %dkb\n", device->bo_size / 1024);
52
 
   fprintf(stderr, "  BOs cached:      %d\n", cache->cache_count);
53
 
   fprintf(stderr, "  BOs cached size: %dkb\n", cache->cache_size / 1024);
54
 
 
55
 
   if (!list_is_empty(&cache->time_list)) {
56
 
      struct v3dv_bo *first = list_first_entry(&cache->time_list,
57
 
                                              struct v3dv_bo,
58
 
                                              time_list);
59
 
      struct v3dv_bo *last = list_last_entry(&cache->time_list,
60
 
                                            struct v3dv_bo,
61
 
                                            time_list);
62
 
 
63
 
      fprintf(stderr, "  oldest cache time: %ld\n",
64
 
              (long)first->free_time);
65
 
      fprintf(stderr, "  newest cache time: %ld\n",
66
 
              (long)last->free_time);
67
 
 
68
 
      struct timespec time;
69
 
      clock_gettime(CLOCK_MONOTONIC, &time);
70
 
      fprintf(stderr, "  now:               %lld\n",
71
 
              (long long)time.tv_sec);
72
 
   }
73
 
 
74
 
   if (cache->size_list_size) {
75
 
      uint32_t empty_size_list = 0;
76
 
      for (uint32_t i = 0; i < cache->size_list_size; i++) {
77
 
         if (list_is_empty(&cache->size_list[i]))
78
 
            empty_size_list++;
79
 
      }
80
 
      fprintf(stderr, "  Empty size_list lists: %d\n", empty_size_list);
81
 
   }
82
 
}
83
 
 
84
 
static void
85
 
bo_remove_from_cache(struct v3dv_bo_cache *cache, struct v3dv_bo *bo)
86
 
{
87
 
   list_del(&bo->time_list);
88
 
   list_del(&bo->size_list);
89
 
 
90
 
   cache->cache_count--;
91
 
   cache->cache_size -= bo->size;
92
 
}
93
 
 
94
 
static struct v3dv_bo *
95
 
bo_from_cache(struct v3dv_device *device, uint32_t size, const char *name)
96
 
{
97
 
   struct v3dv_bo_cache *cache = &device->bo_cache;
98
 
   uint32_t page_index = size / 4096 - 1;
99
 
 
100
 
   if (cache->size_list_size <= page_index)
101
 
      return NULL;
102
 
 
103
 
   struct v3dv_bo *bo = NULL;
104
 
 
105
 
   mtx_lock(&cache->lock);
106
 
   if (!list_is_empty(&cache->size_list[page_index])) {
107
 
      bo = list_first_entry(&cache->size_list[page_index],
108
 
                            struct v3dv_bo, size_list);
109
 
 
110
 
      /* Check that the BO has gone idle.  If not, then we want to
111
 
       * allocate something new instead, since we assume that the
112
 
       * user will proceed to CPU map it and fill it with stuff.
113
 
       */
114
 
      if (!v3dv_bo_wait(device, bo, 0)) {
115
 
         mtx_unlock(&cache->lock);
116
 
         return NULL;
117
 
      }
118
 
 
119
 
      bo_remove_from_cache(cache, bo);
120
 
      bo->name = name;
121
 
      p_atomic_set(&bo->refcnt, 1);
122
 
   }
123
 
   mtx_unlock(&cache->lock);
124
 
   return bo;
125
 
}
126
 
 
127
 
static bool
128
 
bo_free(struct v3dv_device *device,
129
 
        struct v3dv_bo *bo)
130
 
{
131
 
   if (!bo)
132
 
      return true;
133
 
 
134
 
   assert(p_atomic_read(&bo->refcnt) == 0);
135
 
 
136
 
   if (bo->map)
137
 
      v3dv_bo_unmap(device, bo);
138
 
 
139
 
   /* Our BO structs are stored in a sparse array in the physical device,
140
 
    * so we don't want to free the BO pointer, instead we want to reset it
141
 
    * to 0, to signal that array entry as being free.
142
 
    */
143
 
   uint32_t handle = bo->handle;
144
 
   memset(bo, 0, sizeof(*bo));
145
 
 
146
 
   struct drm_gem_close c;
147
 
   memset(&c, 0, sizeof(c));
148
 
   c.handle = handle;
149
 
   int ret = v3dv_ioctl(device->pdevice->render_fd, DRM_IOCTL_GEM_CLOSE, &c);
150
 
   if (ret != 0)
151
 
      fprintf(stderr, "close object %d: %s\n", bo->handle, strerror(errno));
152
 
 
153
 
   device->bo_count--;
154
 
   device->bo_size -= bo->size;
155
 
 
156
 
   if (dump_stats) {
157
 
      fprintf(stderr, "Freed %s%s%dkb:\n",
158
 
              bo->name ? bo->name : "",
159
 
              bo->name ? " " : "",
160
 
              bo->size / 1024);
161
 
      bo_dump_stats(device);
162
 
   }
163
 
 
164
 
   return ret == 0;
165
 
}
166
 
 
167
 
static void
168
 
bo_cache_free_all(struct v3dv_device *device,
169
 
                       bool with_lock)
170
 
{
171
 
   struct v3dv_bo_cache *cache = &device->bo_cache;
172
 
 
173
 
   if (with_lock)
174
 
      mtx_lock(&cache->lock);
175
 
   list_for_each_entry_safe(struct v3dv_bo, bo, &cache->time_list,
176
 
                            time_list) {
177
 
      bo_remove_from_cache(cache, bo);
178
 
      bo_free(device, bo);
179
 
   }
180
 
   if (with_lock)
181
 
      mtx_unlock(&cache->lock);
182
 
 
183
 
}
184
 
 
185
 
void
186
 
v3dv_bo_init(struct v3dv_bo *bo,
187
 
             uint32_t handle,
188
 
             uint32_t size,
189
 
             uint32_t offset,
190
 
             const char *name,
191
 
             bool private)
192
 
{
193
 
   p_atomic_set(&bo->refcnt, 1);
194
 
   bo->handle = handle;
195
 
   bo->handle_bit = 1ull << (handle % 64);
196
 
   bo->size = size;
197
 
   bo->offset = offset;
198
 
   bo->map = NULL;
199
 
   bo->map_size = 0;
200
 
   bo->name = name;
201
 
   bo->private = private;
202
 
   bo->dumb_handle = -1;
203
 
   list_inithead(&bo->list_link);
204
 
}
205
 
 
206
 
struct v3dv_bo *
207
 
v3dv_bo_alloc(struct v3dv_device *device,
208
 
              uint32_t size,
209
 
              const char *name,
210
 
              bool private)
211
 
{
212
 
   struct v3dv_bo *bo;
213
 
 
214
 
   const uint32_t page_align = 4096; /* Always allocate full pages */
215
 
   size = align(size, page_align);
216
 
 
217
 
   if (private) {
218
 
      bo = bo_from_cache(device, size, name);
219
 
      if (bo) {
220
 
         if (dump_stats) {
221
 
            fprintf(stderr, "Allocated %s %dkb from cache:\n",
222
 
                    name, size / 1024);
223
 
            bo_dump_stats(device);
224
 
         }
225
 
         return bo;
226
 
      }
227
 
   }
228
 
 
229
 
 retry:
230
 
   ;
231
 
 
232
 
   bool cleared_and_retried = false;
233
 
   struct drm_v3d_create_bo create = {
234
 
      .size = size
235
 
   };
236
 
 
237
 
   int ret = v3dv_ioctl(device->pdevice->render_fd,
238
 
                        DRM_IOCTL_V3D_CREATE_BO, &create);
239
 
   if (ret != 0) {
240
 
      if (!list_is_empty(&device->bo_cache.time_list) &&
241
 
          !cleared_and_retried) {
242
 
         cleared_and_retried = true;
243
 
         bo_cache_free_all(device, true);
244
 
         goto retry;
245
 
      }
246
 
 
247
 
      fprintf(stderr, "Failed to allocate device memory for BO\n");
248
 
      return NULL;
249
 
   }
250
 
 
251
 
   assert(create.offset % page_align == 0);
252
 
   assert((create.offset & 0xffffffff) == create.offset);
253
 
 
254
 
   bo = v3dv_device_lookup_bo(device->pdevice, create.handle);
255
 
   assert(bo && bo->handle == 0);
256
 
 
257
 
   v3dv_bo_init(bo, create.handle, size, create.offset, name, private);
258
 
 
259
 
   device->bo_count++;
260
 
   device->bo_size += bo->size;
261
 
   if (dump_stats) {
262
 
      fprintf(stderr, "Allocated %s %dkb:\n", name, size / 1024);
263
 
      bo_dump_stats(device);
264
 
   }
265
 
 
266
 
   return bo;
267
 
}
268
 
 
269
 
bool
270
 
v3dv_bo_map_unsynchronized(struct v3dv_device *device,
271
 
                           struct v3dv_bo *bo,
272
 
                           uint32_t size)
273
 
{
274
 
   assert(bo != NULL && size <= bo->size);
275
 
 
276
 
   if (bo->map)
277
 
      return bo->map;
278
 
 
279
 
   struct drm_v3d_mmap_bo map;
280
 
   memset(&map, 0, sizeof(map));
281
 
   map.handle = bo->handle;
282
 
   int ret = v3dv_ioctl(device->pdevice->render_fd,
283
 
                        DRM_IOCTL_V3D_MMAP_BO, &map);
284
 
   if (ret != 0) {
285
 
      fprintf(stderr, "map ioctl failure\n");
286
 
      return false;
287
 
   }
288
 
 
289
 
   bo->map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
290
 
                  device->pdevice->render_fd, map.offset);
291
 
   if (bo->map == MAP_FAILED) {
292
 
      fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
293
 
              bo->handle, (long long)map.offset, (uint32_t)bo->size);
294
 
      return false;
295
 
   }
296
 
   VG(VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, false));
297
 
 
298
 
   bo->map_size = size;
299
 
 
300
 
   return true;
301
 
}
302
 
 
303
 
bool
304
 
v3dv_bo_wait(struct v3dv_device *device,
305
 
             struct v3dv_bo *bo,
306
 
             uint64_t timeout_ns)
307
 
{
308
 
   struct drm_v3d_wait_bo wait = {
309
 
      .handle = bo->handle,
310
 
      .timeout_ns = timeout_ns,
311
 
   };
312
 
   return v3dv_ioctl(device->pdevice->render_fd,
313
 
                     DRM_IOCTL_V3D_WAIT_BO, &wait) == 0;
314
 
}
315
 
 
316
 
bool
317
 
v3dv_bo_map(struct v3dv_device *device, struct v3dv_bo *bo, uint32_t size)
318
 
{
319
 
   assert(bo && size <= bo->size);
320
 
 
321
 
   bool ok = v3dv_bo_map_unsynchronized(device, bo, size);
322
 
   if (!ok)
323
 
      return false;
324
 
 
325
 
   ok = v3dv_bo_wait(device, bo, PIPE_TIMEOUT_INFINITE);
326
 
   if (!ok) {
327
 
      fprintf(stderr, "memory wait for map failed\n");
328
 
      return false;
329
 
   }
330
 
 
331
 
   return true;
332
 
}
333
 
 
334
 
void
335
 
v3dv_bo_unmap(struct v3dv_device *device, struct v3dv_bo *bo)
336
 
{
337
 
   assert(bo && bo->map && bo->map_size > 0);
338
 
 
339
 
   munmap(bo->map, bo->map_size);
340
 
   VG(VALGRIND_FREELIKE_BLOCK(bo->map, 0));
341
 
   bo->map = NULL;
342
 
   bo->map_size = 0;
343
 
}
344
 
 
345
 
static boolean
346
 
reallocate_size_list(struct v3dv_bo_cache *cache,
347
 
                     struct v3dv_device *device,
348
 
                     uint32_t size)
349
 
{
350
 
   struct list_head *new_list =
351
 
      vk_alloc(&device->vk.alloc, sizeof(struct list_head) * size, 8,
352
 
               VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
353
 
 
354
 
   if (!new_list) {
355
 
      fprintf(stderr, "Failed to allocate host memory for cache bo list\n");
356
 
      return false;
357
 
   }
358
 
   struct list_head *old_list = cache->size_list;
359
 
 
360
 
   /* Move old list contents over (since the array has moved, and
361
 
    * therefore the pointers to the list heads have to change).
362
 
    */
363
 
   for (int i = 0; i < cache->size_list_size; i++) {
364
 
      struct list_head *old_head = &cache->size_list[i];
365
 
      if (list_is_empty(old_head)) {
366
 
         list_inithead(&new_list[i]);
367
 
      } else {
368
 
         new_list[i].next = old_head->next;
369
 
         new_list[i].prev = old_head->prev;
370
 
         new_list[i].next->prev = &new_list[i];
371
 
         new_list[i].prev->next = &new_list[i];
372
 
      }
373
 
   }
374
 
   for (int i = cache->size_list_size; i < size; i++)
375
 
      list_inithead(&new_list[i]);
376
 
 
377
 
   cache->size_list = new_list;
378
 
   cache->size_list_size = size;
379
 
   vk_free(&device->vk.alloc, old_list);
380
 
 
381
 
   return true;
382
 
}
383
 
 
384
 
void
385
 
v3dv_bo_cache_init(struct v3dv_device *device)
386
 
{
387
 
   device->bo_size = 0;
388
 
   device->bo_count = 0;
389
 
   list_inithead(&device->bo_cache.time_list);
390
 
   /* FIXME: perhaps set a initial size for the size-list, to avoid run-time
391
 
    * reallocations
392
 
    */
393
 
   device->bo_cache.size_list_size = 0;
394
 
 
395
 
   const char *max_cache_size_str = getenv("V3DV_MAX_BO_CACHE_SIZE");
396
 
   if (max_cache_size_str == NULL)
397
 
      device->bo_cache.max_cache_size = DEFAULT_MAX_BO_CACHE_SIZE;
398
 
   else
399
 
      device->bo_cache.max_cache_size = atoll(max_cache_size_str);
400
 
 
401
 
   if (dump_stats) {
402
 
      fprintf(stderr, "MAX BO CACHE SIZE: %iMB\n", device->bo_cache.max_cache_size);
403
 
   }
404
 
 
405
 
   device->bo_cache.max_cache_size *= 1024 * 1024;
406
 
   device->bo_cache.cache_count = 0;
407
 
   device->bo_cache.cache_size = 0;
408
 
}
409
 
 
410
 
void
411
 
v3dv_bo_cache_destroy(struct v3dv_device *device)
412
 
{
413
 
   bo_cache_free_all(device, true);
414
 
   vk_free(&device->vk.alloc, device->bo_cache.size_list);
415
 
 
416
 
   if (dump_stats) {
417
 
      fprintf(stderr, "BO stats after screen destroy:\n");
418
 
      bo_dump_stats(device);
419
 
   }
420
 
}
421
 
 
422
 
 
423
 
static void
424
 
free_stale_bos(struct v3dv_device *device,
425
 
               time_t time)
426
 
{
427
 
   struct v3dv_bo_cache *cache = &device->bo_cache;
428
 
   bool freed_any = false;
429
 
 
430
 
   list_for_each_entry_safe(struct v3dv_bo, bo, &cache->time_list,
431
 
                            time_list) {
432
 
      /* If it's more than a second old, free it. */
433
 
      if (time - bo->free_time > 2) {
434
 
         if (dump_stats && !freed_any) {
435
 
            fprintf(stderr, "Freeing stale BOs:\n");
436
 
            bo_dump_stats(device);
437
 
            freed_any = true;
438
 
         }
439
 
 
440
 
         bo_remove_from_cache(cache, bo);
441
 
         bo_free(device, bo);
442
 
      } else {
443
 
         break;
444
 
      }
445
 
   }
446
 
 
447
 
   if (dump_stats && freed_any) {
448
 
      fprintf(stderr, "Freed stale BOs:\n");
449
 
      bo_dump_stats(device);
450
 
   }
451
 
}
452
 
 
453
 
bool
454
 
v3dv_bo_free(struct v3dv_device *device,
455
 
             struct v3dv_bo *bo)
456
 
{
457
 
   if (!bo)
458
 
      return true;
459
 
 
460
 
   if (!p_atomic_dec_zero(&bo->refcnt))
461
 
      return true;
462
 
 
463
 
   struct timespec time;
464
 
   struct v3dv_bo_cache *cache = &device->bo_cache;
465
 
   uint32_t page_index = bo->size / 4096 - 1;
466
 
 
467
 
   if (bo->private &&
468
 
       bo->size > cache->max_cache_size - cache->cache_size) {
469
 
      clock_gettime(CLOCK_MONOTONIC, &time);
470
 
      mtx_lock(&cache->lock);
471
 
      free_stale_bos(device, time.tv_sec);
472
 
      mtx_unlock(&cache->lock);
473
 
   }
474
 
 
475
 
   if (!bo->private ||
476
 
       bo->size > cache->max_cache_size - cache->cache_size) {
477
 
      return bo_free(device, bo);
478
 
   }
479
 
 
480
 
   clock_gettime(CLOCK_MONOTONIC, &time);
481
 
   mtx_lock(&cache->lock);
482
 
 
483
 
   if (cache->size_list_size <= page_index) {
484
 
      if (!reallocate_size_list(cache, device, page_index + 1)) {
485
 
         bool outcome = bo_free(device, bo);
486
 
         /* If the reallocation failed, it usually means that we are out of
487
 
          * memory, so we also free all the bo cache. We need to call it to
488
 
          * not use the cache lock, as we are already under it.
489
 
          */
490
 
         bo_cache_free_all(device, false);
491
 
         mtx_unlock(&cache->lock);
492
 
         return outcome;
493
 
      }
494
 
   }
495
 
 
496
 
   bo->free_time = time.tv_sec;
497
 
   list_addtail(&bo->size_list, &cache->size_list[page_index]);
498
 
   list_addtail(&bo->time_list, &cache->time_list);
499
 
 
500
 
   cache->cache_count++;
501
 
   cache->cache_size += bo->size;
502
 
 
503
 
   if (dump_stats) {
504
 
      fprintf(stderr, "Freed %s %dkb to cache:\n",
505
 
              bo->name, bo->size / 1024);
506
 
      bo_dump_stats(device);
507
 
   }
508
 
   bo->name = NULL;
509
 
 
510
 
   free_stale_bos(device, time.tv_sec);
511
 
 
512
 
   mtx_unlock(&cache->lock);
513
 
 
514
 
   return true;
515
 
}