1/**********************************************************
2 * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26#include "util/u_math.h"
27#include "util/u_memory.h"
28#include "util/crc32.h"
29
30#include "svga_debug.h"
31#include "svga_format.h"
32#include "svga_winsys.h"
33#include "svga_screen.h"
34#include "svga_screen_cache.h"
35#include "svga_context.h"
36#include "svga_cmd.h"
37
38#define SVGA_SURFACE_CACHE_ENABLED 1
39
40
41/**
42 * Return the size of the surface described by the key (in bytes).
43 */
44unsigned
45svga_surface_size(const struct svga_host_surface_cache_key *key)
46{
47   unsigned bw, bh, bpb, total_size, i;
48
49   assert(key->numMipLevels > 0);
50   assert(key->numFaces > 0);
51   assert(key->arraySize > 0);
52
53   if (key->format == SVGA3D_BUFFER) {
54      /* Special case: we don't want to count vertex/index buffers
55       * against the cache size limit, so view them as zero-sized.
56       */
57      return 0;
58   }
59
60   svga_format_size(key->format, &bw, &bh, &bpb);
61
62   total_size = 0;
63
64   for (i = 0; i < key->numMipLevels; i++) {
65      unsigned w = u_minify(key->size.width, i);
66      unsigned h = u_minify(key->size.height, i);
67      unsigned d = u_minify(key->size.depth, i);
68      unsigned img_size = ((w + bw - 1) / bw) * ((h + bh - 1) / bh) * d * bpb;
69      total_size += img_size;
70   }
71
72   total_size *= key->numFaces * key->arraySize * MAX2(1, key->sampleCount);
73
74   return total_size;
75}
76
77
78/**
79 * Compute the bucket for this key.
80 */
81static inline unsigned
82svga_screen_cache_bucket(const struct svga_host_surface_cache_key *key)
83{
84   return util_hash_crc32(key, sizeof *key) % SVGA_HOST_SURFACE_CACHE_BUCKETS;
85}
86
87
88/**
89 * Search the cache for a surface that matches the key.  If a match is
90 * found, remove it from the cache and return the surface pointer.
91 * Return NULL otherwise.
92 */
93static struct svga_winsys_surface *
94svga_screen_cache_lookup(struct svga_screen *svgascreen,
95                         const struct svga_host_surface_cache_key *key)
96{
97   struct svga_host_surface_cache *cache = &svgascreen->cache;
98   struct svga_winsys_screen *sws = svgascreen->sws;
99   struct svga_host_surface_cache_entry *entry;
100   struct svga_winsys_surface *handle = NULL;
101   struct list_head *curr, *next;
102   unsigned bucket;
103   unsigned tries = 0;
104
105   assert(key->cachable);
106
107   bucket = svga_screen_cache_bucket(key);
108
109   mtx_lock(&cache->mutex);
110
111   curr = cache->bucket[bucket].next;
112   next = curr->next;
113   while (curr != &cache->bucket[bucket]) {
114      ++tries;
115
116      entry = list_entry(curr, struct svga_host_surface_cache_entry, bucket_head);
117
118      assert(entry->handle);
119
120      /* If the key matches and the fence is signalled (the surface is no
121       * longer needed) the lookup was successful.  We found a surface that
122       * can be reused.
123       * We unlink the surface from the cache entry and we add the entry to
124       * the 'empty' list.
125       */
126      if (memcmp(&entry->key, key, sizeof *key) == 0 &&
127          sws->fence_signalled(sws, entry->fence, 0) == 0) {
128         unsigned surf_size;
129
130         assert(sws->surface_is_flushed(sws, entry->handle));
131
132         handle = entry->handle; /* Reference is transfered here. */
133         entry->handle = NULL;
134
135         /* Remove from hash table */
136         list_del(&entry->bucket_head);
137
138         /* remove from LRU list */
139         list_del(&entry->head);
140
141         /* Add the cache entry (but not the surface!) to the empty list */
142         list_add(&entry->head, &cache->empty);
143
144         /* update the cache size */
145         surf_size = svga_surface_size(&entry->key);
146         assert(surf_size <= cache->total_size);
147         if (surf_size > cache->total_size)
148            cache->total_size = 0; /* should never happen, but be safe */
149         else
150            cache->total_size -= surf_size;
151
152         break;
153      }
154
155      curr = next;
156      next = curr->next;
157   }
158
159   mtx_unlock(&cache->mutex);
160
161   if (SVGA_DEBUG & DEBUG_DMA)
162      debug_printf("%s: cache %s after %u tries (bucket %d)\n", __FUNCTION__,
163                   handle ? "hit" : "miss", tries, bucket);
164
165   return handle;
166}
167
168
169/**
170 * Free the least recently used entries in the surface cache until the
171 * cache size is <= the target size OR there are no unused entries left
172 * to discard.  We don't do any flushing to try to free up additional
173 * surfaces.
174 */
175static void
176svga_screen_cache_shrink(struct svga_screen *svgascreen,
177                         unsigned target_size)
178{
179   struct svga_host_surface_cache *cache = &svgascreen->cache;
180   struct svga_winsys_screen *sws = svgascreen->sws;
181   struct svga_host_surface_cache_entry *entry = NULL, *next_entry;
182
183   /* Walk over the list of unused buffers in reverse order: from oldest
184    * to newest.
185    */
186   LIST_FOR_EACH_ENTRY_SAFE_REV(entry, next_entry, &cache->unused, head) {
187      if (entry->key.format != SVGA3D_BUFFER) {
188         /* we don't want to discard vertex/index buffers */
189
190         cache->total_size -= svga_surface_size(&entry->key);
191
192         assert(entry->handle);
193         sws->surface_reference(sws, &entry->handle, NULL);
194
195         list_del(&entry->bucket_head);
196         list_del(&entry->head);
197         list_add(&entry->head, &cache->empty);
198
199         if (cache->total_size <= target_size) {
200            /* all done */
201            break;
202         }
203      }
204   }
205}
206
207
208/**
209 * Add a surface to the cache.  This is done when the driver deletes
210 * the surface.  Note: transfers a handle reference.
211 */
212static void
213svga_screen_cache_add(struct svga_screen *svgascreen,
214                      const struct svga_host_surface_cache_key *key,
215                      boolean to_invalidate,
216                      struct svga_winsys_surface **p_handle)
217{
218   struct svga_host_surface_cache *cache = &svgascreen->cache;
219   struct svga_winsys_screen *sws = svgascreen->sws;
220   struct svga_host_surface_cache_entry *entry = NULL;
221   struct svga_winsys_surface *handle = *p_handle;
222   unsigned surf_size;
223
224   assert(key->cachable);
225
226   if (!handle)
227      return;
228
229   surf_size = svga_surface_size(key);
230
231   *p_handle = NULL;
232   mtx_lock(&cache->mutex);
233
234   if (surf_size >= SVGA_HOST_SURFACE_CACHE_BYTES) {
235      /* this surface is too large to cache, just free it */
236      sws->surface_reference(sws, &handle, NULL);
237      mtx_unlock(&cache->mutex);
238      return;
239   }
240
241   if (cache->total_size + surf_size > SVGA_HOST_SURFACE_CACHE_BYTES) {
242      /* Adding this surface would exceed the cache size.
243       * Try to discard least recently used entries until we hit the
244       * new target cache size.
245       */
246      unsigned target_size = SVGA_HOST_SURFACE_CACHE_BYTES - surf_size;
247
248      svga_screen_cache_shrink(svgascreen, target_size);
249
250      if (cache->total_size > target_size) {
251         /* we weren't able to shrink the cache as much as we wanted so
252          * just discard this surface.
253          */
254         sws->surface_reference(sws, &handle, NULL);
255         mtx_unlock(&cache->mutex);
256         return;
257      }
258   }
259
260   if (!list_is_empty(&cache->empty)) {
261      /* An empty entry has no surface associated with it.
262       * Use the first empty entry.
263       */
264      entry = list_entry(cache->empty.next,
265                         struct svga_host_surface_cache_entry,
266                         head);
267
268      /* Remove from LRU list */
269      list_del(&entry->head);
270   }
271   else if (!list_is_empty(&cache->unused)) {
272      /* free the last used buffer and reuse its entry */
273      entry = list_entry(cache->unused.prev,
274                         struct svga_host_surface_cache_entry,
275                         head);
276      SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
277               "unref sid %p (make space)\n", entry->handle);
278
279      cache->total_size -= svga_surface_size(&entry->key);
280
281      sws->surface_reference(sws, &entry->handle, NULL);
282
283      /* Remove from hash table */
284      list_del(&entry->bucket_head);
285
286      /* Remove from LRU list */
287      list_del(&entry->head);
288   }
289
290   if (entry) {
291      assert(entry->handle == NULL);
292      entry->handle = handle;
293      memcpy(&entry->key, key, sizeof entry->key);
294
295      SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
296               "cache sid %p\n", entry->handle);
297
298      /* If we don't have gb objects, we don't need to invalidate. */
299      if (sws->have_gb_objects) {
300         if (to_invalidate)
301            list_add(&entry->head, &cache->validated);
302         else
303            list_add(&entry->head, &cache->invalidated);
304      }
305      else
306         list_add(&entry->head, &cache->invalidated);
307
308      cache->total_size += surf_size;
309   }
310   else {
311      /* Couldn't cache the buffer -- this really shouldn't happen */
312      SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
313               "unref sid %p (couldn't find space)\n", handle);
314      sws->surface_reference(sws, &handle, NULL);
315   }
316
317   mtx_unlock(&cache->mutex);
318}
319
320
321/* Maximum number of invalidate surface commands in a command buffer */
322# define SVGA_MAX_SURFACE_TO_INVALIDATE 1000
323
324/**
325 * Called during the screen flush to move all buffers not in a validate list
326 * into the unused list.
327 */
328void
329svga_screen_cache_flush(struct svga_screen *svgascreen,
330                        struct svga_context *svga,
331                        struct pipe_fence_handle *fence)
332{
333   struct svga_host_surface_cache *cache = &svgascreen->cache;
334   struct svga_winsys_screen *sws = svgascreen->sws;
335   struct svga_host_surface_cache_entry *entry;
336   struct list_head *curr, *next;
337   unsigned bucket;
338
339   mtx_lock(&cache->mutex);
340
341   /* Loop over entries in the invalidated list */
342   curr = cache->invalidated.next;
343   next = curr->next;
344   while (curr != &cache->invalidated) {
345      entry = list_entry(curr, struct svga_host_surface_cache_entry, head);
346
347      assert(entry->handle);
348
349      if (sws->surface_is_flushed(sws, entry->handle)) {
350         /* remove entry from the invalidated list */
351         list_del(&entry->head);
352
353         sws->fence_reference(sws, &entry->fence, fence);
354
355         /* Add entry to the unused list */
356         list_add(&entry->head, &cache->unused);
357
358         /* Add entry to the hash table bucket */
359         bucket = svga_screen_cache_bucket(&entry->key);
360         list_add(&entry->bucket_head, &cache->bucket[bucket]);
361      }
362
363      curr = next;
364      next = curr->next;
365   }
366
367   unsigned nsurf = 0;
368   curr = cache->validated.next;
369   next = curr->next;
370   while (curr != &cache->validated) {
371      entry = list_entry(curr, struct svga_host_surface_cache_entry, head);
372
373      assert(entry->handle);
374      assert(svga_have_gb_objects(svga));
375
376      if (sws->surface_is_flushed(sws, entry->handle)) {
377         /* remove entry from the validated list */
378         list_del(&entry->head);
379
380         /* It is now safe to invalidate the surface content.
381          * It will be done using the current context.
382          */
383         if (SVGA_TRY(SVGA3D_InvalidateGBSurface(svga->swc, entry->handle))
384             != PIPE_OK) {
385            ASSERTED enum pipe_error ret;
386
387            /* Even though surface invalidation here is done after the command
388             * buffer is flushed, it is still possible that it will
389             * fail because there might be just enough of this command that is
390             * filling up the command buffer, so in this case we will call
391             * the winsys flush directly to flush the buffer.
392             * Note, we don't want to call svga_context_flush() here because
393             * this function itself is called inside svga_context_flush().
394             */
395            svga_retry_enter(svga);
396            svga->swc->flush(svga->swc, NULL);
397            nsurf = 0;
398            ret = SVGA3D_InvalidateGBSurface(svga->swc, entry->handle);
399            svga_retry_exit(svga);
400            assert(ret == PIPE_OK);
401         }
402
403         /* add the entry to the invalidated list */
404
405         list_add(&entry->head, &cache->invalidated);
406         nsurf++;
407      }
408
409      curr = next;
410      next = curr->next;
411   }
412
413   mtx_unlock(&cache->mutex);
414
415   /**
416    * In some rare cases (when running ARK survival), we hit the max number
417    * of surface relocations with invalidated surfaces during context flush.
418    * So if the number of invalidated surface exceeds a certain limit (1000),
419    * we'll do another winsys flush.
420    */
421   if (nsurf > SVGA_MAX_SURFACE_TO_INVALIDATE) {
422      svga->swc->flush(svga->swc, NULL);
423   }
424}
425
426
427/**
428 * Free all the surfaces in the cache.
429 * Called when destroying the svga screen object.
430 */
431void
432svga_screen_cache_cleanup(struct svga_screen *svgascreen)
433{
434   struct svga_host_surface_cache *cache = &svgascreen->cache;
435   struct svga_winsys_screen *sws = svgascreen->sws;
436   unsigned i;
437
438   for (i = 0; i < SVGA_HOST_SURFACE_CACHE_SIZE; ++i) {
439      if (cache->entries[i].handle) {
440	 SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
441                  "unref sid %p (shutdown)\n", cache->entries[i].handle);
442	 sws->surface_reference(sws, &cache->entries[i].handle, NULL);
443
444         cache->total_size -= svga_surface_size(&cache->entries[i].key);
445      }
446
447      if (cache->entries[i].fence)
448         sws->fence_reference(sws, &cache->entries[i].fence, NULL);
449   }
450
451   mtx_destroy(&cache->mutex);
452}
453
454
455enum pipe_error
456svga_screen_cache_init(struct svga_screen *svgascreen)
457{
458   struct svga_host_surface_cache *cache = &svgascreen->cache;
459   unsigned i;
460
461   assert(cache->total_size == 0);
462
463   (void) mtx_init(&cache->mutex, mtx_plain);
464
465   for (i = 0; i < SVGA_HOST_SURFACE_CACHE_BUCKETS; ++i)
466      list_inithead(&cache->bucket[i]);
467
468   list_inithead(&cache->unused);
469
470   list_inithead(&cache->validated);
471
472   list_inithead(&cache->invalidated);
473
474   list_inithead(&cache->empty);
475   for (i = 0; i < SVGA_HOST_SURFACE_CACHE_SIZE; ++i)
476      list_addtail(&cache->entries[i].head, &cache->empty);
477
478   return PIPE_OK;
479}
480
481
482/**
483 * Allocate a new host-side surface.  If the surface is marked as cachable,
484 * first try re-using a surface in the cache of freed surfaces.  Otherwise,
485 * allocate a new surface.
486 * \param bind_flags  bitmask of PIPE_BIND_x flags
487 * \param usage  one of PIPE_USAGE_x values
488 * \param validated return True if the surface is a reused surface
489 */
490struct svga_winsys_surface *
491svga_screen_surface_create(struct svga_screen *svgascreen,
492                           unsigned bind_flags, enum pipe_resource_usage usage,
493                           boolean *validated,
494                           struct svga_host_surface_cache_key *key)
495{
496   struct svga_winsys_screen *sws = svgascreen->sws;
497   struct svga_winsys_surface *handle = NULL;
498   boolean cachable = SVGA_SURFACE_CACHE_ENABLED && key->cachable;
499
500   SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
501            "%s sz %dx%dx%d mips %d faces %d arraySize %d cachable %d\n",
502            __FUNCTION__,
503            key->size.width,
504            key->size.height,
505            key->size.depth,
506            key->numMipLevels,
507            key->numFaces,
508            key->arraySize,
509            key->cachable);
510
511   if (cachable) {
512      /* Try to re-cycle a previously freed, cached surface */
513      if (key->format == SVGA3D_BUFFER) {
514         SVGA3dSurfaceAllFlags hint_flag;
515
516         /* For buffers, round the buffer size up to the nearest power
517          * of two to increase the probability of cache hits.  Keep
518          * texture surface dimensions unchanged.
519          */
520         uint32_t size = 1;
521         while (size < key->size.width)
522            size <<= 1;
523         key->size.width = size;
524
525         /* Determine whether the buffer is static or dynamic.
526          * This is a bit of a heuristic which can be tuned as needed.
527          */
528         if (usage == PIPE_USAGE_DEFAULT ||
529             usage == PIPE_USAGE_IMMUTABLE) {
530            hint_flag = SVGA3D_SURFACE_HINT_STATIC;
531         }
532         else if (bind_flags & PIPE_BIND_INDEX_BUFFER) {
533            /* Index buffers don't change too often.  Mark them as static.
534             */
535            hint_flag = SVGA3D_SURFACE_HINT_STATIC;
536         }
537         else {
538            /* Since we're reusing buffers we're effectively transforming all
539             * of them into dynamic buffers.
540             *
541             * It would be nice to not cache long lived static buffers. But there
542             * is no way to detect the long lived from short lived ones yet. A
543             * good heuristic would be buffer size.
544             */
545            hint_flag = SVGA3D_SURFACE_HINT_DYNAMIC;
546         }
547
548         key->flags &= ~(SVGA3D_SURFACE_HINT_STATIC |
549                         SVGA3D_SURFACE_HINT_DYNAMIC);
550         key->flags |= hint_flag;
551      }
552
553      handle = svga_screen_cache_lookup(svgascreen, key);
554      if (handle) {
555         if (key->format == SVGA3D_BUFFER)
556            SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
557                     "reuse sid %p sz %d (buffer)\n", handle,
558                     key->size.width);
559         else
560            SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
561                     "reuse sid %p sz %dx%dx%d mips %d faces %d arraySize %d\n", handle,
562                     key->size.width,
563                     key->size.height,
564                     key->size.depth,
565                     key->numMipLevels,
566                     key->numFaces,
567                     key->arraySize);
568         *validated = TRUE;
569      }
570   }
571
572   if (!handle) {
573      /* Unable to recycle surface, allocate a new one */
574      unsigned usage = 0;
575
576      if (!key->cachable)
577         usage |= SVGA_SURFACE_USAGE_SHARED;
578      if (key->scanout)
579         usage |= SVGA_SURFACE_USAGE_SCANOUT;
580      if (key->coherent)
581         usage |= SVGA_SURFACE_USAGE_COHERENT;
582
583      handle = sws->surface_create(sws,
584                                   key->flags,
585                                   key->format,
586                                   usage,
587                                   key->size,
588                                   key->numFaces * key->arraySize,
589                                   key->numMipLevels,
590                                   key->sampleCount);
591      if (handle)
592         SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
593                  "  CREATE sid %p sz %dx%dx%d\n",
594                  handle,
595                  key->size.width,
596                  key->size.height,
597                  key->size.depth);
598
599      *validated = FALSE;
600   }
601
602   return handle;
603}
604
605
606/**
607 * Release a surface.  We don't actually free the surface- we put
608 * it into the cache of freed surfaces (if it's cachable).
609 */
610void
611svga_screen_surface_destroy(struct svga_screen *svgascreen,
612                            const struct svga_host_surface_cache_key *key,
613                            boolean to_invalidate,
614                            struct svga_winsys_surface **p_handle)
615{
616   struct svga_winsys_screen *sws = svgascreen->sws;
617
618   /* We only set the cachable flag for surfaces of which we are the
619    * exclusive owner.  So just hold onto our existing reference in
620    * that case.
621    */
622   if (SVGA_SURFACE_CACHE_ENABLED && key->cachable) {
623      svga_screen_cache_add(svgascreen, key, to_invalidate, p_handle);
624   }
625   else {
626      SVGA_DBG(DEBUG_DMA,
627               "unref sid %p (uncachable)\n", *p_handle);
628      sws->surface_reference(sws, p_handle, NULL);
629   }
630}
631
632
633/**
634 * Print/dump the contents of the screen cache.  For debugging.
635 */
636void
637svga_screen_cache_dump(const struct svga_screen *svgascreen)
638{
639   const struct svga_host_surface_cache *cache = &svgascreen->cache;
640   unsigned bucket;
641   unsigned count = 0;
642
643   debug_printf("svga3d surface cache:\n");
644   for (bucket = 0; bucket < SVGA_HOST_SURFACE_CACHE_BUCKETS; bucket++) {
645      struct list_head *curr;
646      curr = cache->bucket[bucket].next;
647      while (curr && curr != &cache->bucket[bucket]) {
648         struct svga_host_surface_cache_entry *entry =
649            list_entry(curr, struct svga_host_surface_cache_entry,bucket_head);
650         if (entry->key.format == SVGA3D_BUFFER) {
651            debug_printf("  %p: buffer %u bytes\n",
652                         entry->handle,
653                         entry->key.size.width);
654         }
655         else {
656            debug_printf("  %p: %u x %u x %u format %u\n",
657                         entry->handle,
658                         entry->key.size.width,
659                         entry->key.size.height,
660                         entry->key.size.depth,
661                         entry->key.format);
662         }
663         curr = curr->next;
664         count++;
665      }
666   }
667
668   debug_printf("%u surfaces, %u bytes\n", count, cache->total_size);
669}
670