Linux 2.6.31-rc6
[linux-2.6] / drivers / gpu / drm / radeon / radeon_test.c
1 /*
2  * Copyright 2009 VMware, Inc.
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Michel Dänzer
23  */
24 #include <drm/drmP.h>
25 #include <drm/radeon_drm.h>
26 #include "radeon_reg.h"
27 #include "radeon.h"
28
29
30 /* Test BO GTT->VRAM and VRAM->GTT GPU copies across the whole GTT aperture */
31 void radeon_test_moves(struct radeon_device *rdev)
32 {
33         struct radeon_object *vram_obj = NULL;
34         struct radeon_object **gtt_obj = NULL;
35         struct radeon_fence *fence = NULL;
36         uint64_t gtt_addr, vram_addr;
37         unsigned i, n, size;
38         int r;
39
40         size = 1024 * 1024;
41
42         /* Number of tests =
43          * (Total GTT - IB pool - writeback page - ring buffer) / test size
44          */
45         n = (rdev->mc.gtt_size - RADEON_IB_POOL_SIZE*64*1024 - 4096 -
46              rdev->cp.ring_size) / size;
47
48         gtt_obj = kzalloc(n * sizeof(*gtt_obj), GFP_KERNEL);
49         if (!gtt_obj) {
50                 DRM_ERROR("Failed to allocate %d pointers\n", n);
51                 r = 1;
52                 goto out_cleanup;
53         }
54
55         r = radeon_object_create(rdev, NULL, size, true, RADEON_GEM_DOMAIN_VRAM,
56                                  false, &vram_obj);
57         if (r) {
58                 DRM_ERROR("Failed to create VRAM object\n");
59                 goto out_cleanup;
60         }
61
62         r = radeon_object_pin(vram_obj, RADEON_GEM_DOMAIN_VRAM, &vram_addr);
63         if (r) {
64                 DRM_ERROR("Failed to pin VRAM object\n");
65                 goto out_cleanup;
66         }
67
68         for (i = 0; i < n; i++) {
69                 void *gtt_map, *vram_map;
70                 void **gtt_start, **gtt_end;
71                 void **vram_start, **vram_end;
72
73                 r = radeon_object_create(rdev, NULL, size, true,
74                                          RADEON_GEM_DOMAIN_GTT, false, gtt_obj + i);
75                 if (r) {
76                         DRM_ERROR("Failed to create GTT object %d\n", i);
77                         goto out_cleanup;
78                 }
79
80                 r = radeon_object_pin(gtt_obj[i], RADEON_GEM_DOMAIN_GTT, &gtt_addr);
81                 if (r) {
82                         DRM_ERROR("Failed to pin GTT object %d\n", i);
83                         goto out_cleanup;
84                 }
85
86                 r = radeon_object_kmap(gtt_obj[i], &gtt_map);
87                 if (r) {
88                         DRM_ERROR("Failed to map GTT object %d\n", i);
89                         goto out_cleanup;
90                 }
91
92                 for (gtt_start = gtt_map, gtt_end = gtt_map + size;
93                      gtt_start < gtt_end;
94                      gtt_start++)
95                         *gtt_start = gtt_start;
96
97                 radeon_object_kunmap(gtt_obj[i]);
98
99                 r = radeon_fence_create(rdev, &fence);
100                 if (r) {
101                         DRM_ERROR("Failed to create GTT->VRAM fence %d\n", i);
102                         goto out_cleanup;
103                 }
104
105                 r = radeon_copy(rdev, gtt_addr, vram_addr, size / 4096, fence);
106                 if (r) {
107                         DRM_ERROR("Failed GTT->VRAM copy %d\n", i);
108                         goto out_cleanup;
109                 }
110
111                 r = radeon_fence_wait(fence, false);
112                 if (r) {
113                         DRM_ERROR("Failed to wait for GTT->VRAM fence %d\n", i);
114                         goto out_cleanup;
115                 }
116
117                 radeon_fence_unref(&fence);
118
119                 r = radeon_object_kmap(vram_obj, &vram_map);
120                 if (r) {
121                         DRM_ERROR("Failed to map VRAM object after copy %d\n", i);
122                         goto out_cleanup;
123                 }
124
125                 for (gtt_start = gtt_map, gtt_end = gtt_map + size,
126                      vram_start = vram_map, vram_end = vram_map + size;
127                      vram_start < vram_end;
128                      gtt_start++, vram_start++) {
129                         if (*vram_start != gtt_start) {
130                                 DRM_ERROR("Incorrect GTT->VRAM copy %d: Got 0x%p, "
131                                           "expected 0x%p (GTT map 0x%p-0x%p)\n",
132                                           i, *vram_start, gtt_start, gtt_map,
133                                           gtt_end);
134                                 radeon_object_kunmap(vram_obj);
135                                 goto out_cleanup;
136                         }
137                         *vram_start = vram_start;
138                 }
139
140                 radeon_object_kunmap(vram_obj);
141
142                 r = radeon_fence_create(rdev, &fence);
143                 if (r) {
144                         DRM_ERROR("Failed to create VRAM->GTT fence %d\n", i);
145                         goto out_cleanup;
146                 }
147
148                 r = radeon_copy(rdev, vram_addr, gtt_addr, size / 4096, fence);
149                 if (r) {
150                         DRM_ERROR("Failed VRAM->GTT copy %d\n", i);
151                         goto out_cleanup;
152                 }
153
154                 r = radeon_fence_wait(fence, false);
155                 if (r) {
156                         DRM_ERROR("Failed to wait for VRAM->GTT fence %d\n", i);
157                         goto out_cleanup;
158                 }
159
160                 radeon_fence_unref(&fence);
161
162                 r = radeon_object_kmap(gtt_obj[i], &gtt_map);
163                 if (r) {
164                         DRM_ERROR("Failed to map GTT object after copy %d\n", i);
165                         goto out_cleanup;
166                 }
167
168                 for (gtt_start = gtt_map, gtt_end = gtt_map + size,
169                      vram_start = vram_map, vram_end = vram_map + size;
170                      gtt_start < gtt_end;
171                      gtt_start++, vram_start++) {
172                         if (*gtt_start != vram_start) {
173                                 DRM_ERROR("Incorrect VRAM->GTT copy %d: Got 0x%p, "
174                                           "expected 0x%p (VRAM map 0x%p-0x%p)\n",
175                                           i, *gtt_start, vram_start, vram_map,
176                                           vram_end);
177                                 radeon_object_kunmap(gtt_obj[i]);
178                                 goto out_cleanup;
179                         }
180                 }
181
182                 radeon_object_kunmap(gtt_obj[i]);
183
184                 DRM_INFO("Tested GTT->VRAM and VRAM->GTT copy for GTT offset 0x%llx\n",
185                          gtt_addr - rdev->mc.gtt_location);
186         }
187
188 out_cleanup:
189         if (vram_obj) {
190                 radeon_object_unpin(vram_obj);
191                 radeon_object_unref(&vram_obj);
192         }
193         if (gtt_obj) {
194                 for (i = 0; i < n; i++) {
195                         if (gtt_obj[i]) {
196                                 radeon_object_unpin(gtt_obj[i]);
197                                 radeon_object_unref(&gtt_obj[i]);
198                         }
199                 }
200                 kfree(gtt_obj);
201         }
202         if (fence) {
203                 radeon_fence_unref(&fence);
204         }
205         if (r) {
206                 printk(KERN_WARNING "Error while testing BO move.\n");
207         }
208 }
209