Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008-9 Intel Corporation |
| 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 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * Chris Wilson <chris@chris-wilson.co.uk> |
| 26 | * |
| 27 | */ |
| 28 | |
Damien Lespiau | 7ee278f | 2013-02-20 14:40:07 +0000 | [diff] [blame^] | 29 | #ifdef HAVE_CONFIG_H |
Alan Coopersmith | 504c4fa | 2012-01-06 15:45:29 -0800 | [diff] [blame] | 30 | #include "config.h" |
Damien Lespiau | 7ee278f | 2013-02-20 14:40:07 +0000 | [diff] [blame^] | 31 | #endif |
Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 32 | |
| 33 | #include <unistd.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | #include <assert.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <inttypes.h> |
| 40 | #include <errno.h> |
| 41 | #include <sys/stat.h> |
| 42 | #include <sys/ioctl.h> |
| 43 | #include <sys/mman.h> |
| 44 | #include <pthread.h> |
| 45 | #include "drm.h" |
| 46 | #include "i915_drm.h" |
| 47 | #include "drmtest.h" |
| 48 | |
Daniel Vetter | 2e9e27c | 2011-10-15 13:46:19 +0200 | [diff] [blame] | 49 | #define OBJECT_SIZE (128*1024) /* restricted to 1MiB alignment on i915 fences */ |
Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 50 | |
| 51 | /* Before introduction of the LRU list for fences, allocation of a fence for a page |
| 52 | * fault would use the first inactive fence (i.e. in preference one with no outstanding |
| 53 | * GPU activity, or it would wait on the first to finish). Given the choice, it would simply |
| 54 | * reuse the fence that had just been allocated for the previous page-fault - the worst choice |
| 55 | * when copying between two buffers and thus constantly swapping fences. |
| 56 | */ |
| 57 | |
| 58 | static void * |
| 59 | bo_create (int fd) |
| 60 | { |
Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 61 | void *ptr; |
| 62 | int handle; |
Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 63 | |
Daniel Vetter | ff93f35 | 2012-03-24 19:22:21 +0100 | [diff] [blame] | 64 | handle = gem_create(fd, OBJECT_SIZE); |
Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 65 | |
Daniel Vetter | ff93f35 | 2012-03-24 19:22:21 +0100 | [diff] [blame] | 66 | gem_set_tiling(fd, handle, I915_TILING_X, 1024); |
Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 67 | |
Daniel Vetter | ff93f35 | 2012-03-24 19:22:21 +0100 | [diff] [blame] | 68 | ptr = gem_mmap(fd, handle, OBJECT_SIZE, PROT_READ | PROT_WRITE); |
Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 69 | |
| 70 | /* XXX: mmap_gtt pulls the bo into the GTT read domain. */ |
Daniel Vetter | 673e6b2 | 2012-01-10 16:05:34 +0100 | [diff] [blame] | 71 | gem_sync(fd, handle); |
Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 72 | |
| 73 | return ptr; |
| 74 | } |
| 75 | |
| 76 | static void * |
| 77 | bo_copy (void *_arg) |
| 78 | { |
| 79 | int fd = *(int *)_arg; |
Daniel Vetter | 2e9e27c | 2011-10-15 13:46:19 +0200 | [diff] [blame] | 80 | int n; |
Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 81 | char *a, *b; |
| 82 | |
| 83 | a = bo_create (fd); |
| 84 | b = bo_create (fd); |
| 85 | |
Daniel Vetter | 2e9e27c | 2011-10-15 13:46:19 +0200 | [diff] [blame] | 86 | for (n = 0; n < 1000; n++) { |
| 87 | memcpy (a, b, OBJECT_SIZE); |
Alan Coopersmith | 0075159 | 2012-01-06 15:45:28 -0800 | [diff] [blame] | 88 | sched_yield (); |
Chris Wilson | 8f3f862 | 2009-09-01 10:09:55 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | int |
| 95 | main(int argc, char **argv) |
| 96 | { |
| 97 | drm_i915_getparam_t gp; |
| 98 | pthread_t threads[32]; |
| 99 | int n, num_fences; |
| 100 | int fd, ret; |
| 101 | |
| 102 | fd = drm_open_any(); |
| 103 | |
| 104 | gp.param = I915_PARAM_NUM_FENCES_AVAIL; |
| 105 | gp.value = &num_fences; |
| 106 | ret = ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp); |
| 107 | assert (ret == 0); |
| 108 | |
| 109 | printf ("creating %d threads\n", num_fences); |
| 110 | assert (num_fences < sizeof (threads) / sizeof (threads[0])); |
| 111 | |
| 112 | for (n = 0; n < num_fences; n++) |
| 113 | pthread_create (&threads[n], NULL, bo_copy, &fd); |
| 114 | |
| 115 | for (n = 0; n < num_fences; n++) |
| 116 | pthread_join (threads[n], NULL); |
| 117 | |
| 118 | close(fd); |
| 119 | |
| 120 | return 0; |
| 121 | } |