@@ -130,16 +130,13 @@ static int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags,
130
130
dmabuf_fd );
131
131
}
132
132
133
- static void dmabuf_sync (int fd , int start_stop )
133
+ static int dmabuf_sync (int fd , int start_stop )
134
134
{
135
135
struct dma_buf_sync sync = {
136
136
.flags = start_stop | DMA_BUF_SYNC_RW ,
137
137
};
138
- int ret ;
139
138
140
- ret = ioctl (fd , DMA_BUF_IOCTL_SYNC , & sync );
141
- if (ret )
142
- printf ("sync failed %d\n" , errno );
139
+ return ioctl (fd , DMA_BUF_IOCTL_SYNC , & sync );
143
140
}
144
141
145
142
#define ONE_MEG (1024 * 1024)
@@ -151,16 +148,14 @@ static int test_alloc_and_import(char *heap_name)
151
148
void * p = NULL ;
152
149
int ret ;
153
150
154
- printf ("Testing heap: %s\n" , heap_name );
155
-
156
151
heap_fd = dmabuf_heap_open (heap_name );
157
152
if (heap_fd < 0 )
158
153
return -1 ;
159
154
160
- printf ("Allocating 1 MEG\n " );
155
+ printf (" Testing allocation and importing: " );
161
156
ret = dmabuf_heap_alloc (heap_fd , ONE_MEG , 0 , & dmabuf_fd );
162
157
if (ret ) {
163
- printf ("Allocation Failed!\n" );
158
+ printf ("FAIL ( Allocation Failed!) \n" );
164
159
ret = -1 ;
165
160
goto out ;
166
161
}
@@ -172,11 +167,10 @@ static int test_alloc_and_import(char *heap_name)
172
167
dmabuf_fd ,
173
168
0 );
174
169
if (p == MAP_FAILED ) {
175
- printf ("mmap() failed: %m \n" );
170
+ printf ("FAIL ( mmap() failed) \n" );
176
171
ret = -1 ;
177
172
goto out ;
178
173
}
179
- printf ("mmap passed\n" );
180
174
181
175
dmabuf_sync (dmabuf_fd , DMA_BUF_SYNC_START );
182
176
memset (p , 1 , ONE_MEG / 2 );
@@ -186,25 +180,31 @@ static int test_alloc_and_import(char *heap_name)
186
180
importer_fd = open_vgem ();
187
181
if (importer_fd < 0 ) {
188
182
ret = importer_fd ;
189
- printf ("Failed to open vgem\n" );
190
- goto out ;
183
+ printf ("(Could not open vgem - skipping): " );
184
+ } else {
185
+ ret = import_vgem_fd (importer_fd , dmabuf_fd , & handle );
186
+ if (ret < 0 ) {
187
+ printf ("FAIL (Failed to import buffer)\n" );
188
+ goto out ;
189
+ }
191
190
}
192
191
193
- ret = import_vgem_fd ( importer_fd , dmabuf_fd , & handle );
192
+ ret = dmabuf_sync ( dmabuf_fd , DMA_BUF_SYNC_START );
194
193
if (ret < 0 ) {
195
- printf ("Failed to import buffer \n" );
194
+ printf ("FAIL (DMA_BUF_SYNC_START failed!) \n" );
196
195
goto out ;
197
196
}
198
- printf ("import passed\n" );
199
197
200
- dmabuf_sync (dmabuf_fd , DMA_BUF_SYNC_START );
201
198
memset (p , 0xff , ONE_MEG );
202
- dmabuf_sync (dmabuf_fd , DMA_BUF_SYNC_END );
203
- printf ("syncs passed\n" );
199
+ ret = dmabuf_sync (dmabuf_fd , DMA_BUF_SYNC_END );
200
+ if (ret < 0 ) {
201
+ printf ("FAIL (DMA_BUF_SYNC_END failed!)\n" );
202
+ goto out ;
203
+ }
204
204
205
205
close_handle (importer_fd , handle );
206
206
ret = 0 ;
207
-
207
+ printf ( " OK\n" );
208
208
out :
209
209
if (p )
210
210
munmap (p , ONE_MEG );
@@ -218,6 +218,84 @@ static int test_alloc_and_import(char *heap_name)
218
218
return ret ;
219
219
}
220
220
221
+ static int test_alloc_zeroed (char * heap_name , size_t size )
222
+ {
223
+ int heap_fd = -1 , dmabuf_fd [32 ];
224
+ int i , j , ret ;
225
+ void * p = NULL ;
226
+ char * c ;
227
+
228
+ printf (" Testing alloced %ldk buffers are zeroed: " , size / 1024 );
229
+ heap_fd = dmabuf_heap_open (heap_name );
230
+ if (heap_fd < 0 )
231
+ return -1 ;
232
+
233
+ /* Allocate and fill a bunch of buffers */
234
+ for (i = 0 ; i < 32 ; i ++ ) {
235
+ ret = dmabuf_heap_alloc (heap_fd , size , 0 , & dmabuf_fd [i ]);
236
+ if (ret < 0 ) {
237
+ printf ("FAIL (Allocation (%i) failed)\n" , i );
238
+ goto out ;
239
+ }
240
+ /* mmap and fill with simple pattern */
241
+ p = mmap (NULL , size , PROT_READ | PROT_WRITE , MAP_SHARED , dmabuf_fd [i ], 0 );
242
+ if (p == MAP_FAILED ) {
243
+ printf ("FAIL (mmap() failed!)\n" );
244
+ ret = -1 ;
245
+ goto out ;
246
+ }
247
+ dmabuf_sync (dmabuf_fd [i ], DMA_BUF_SYNC_START );
248
+ memset (p , 0xff , size );
249
+ dmabuf_sync (dmabuf_fd [i ], DMA_BUF_SYNC_END );
250
+ munmap (p , size );
251
+ }
252
+ /* close them all */
253
+ for (i = 0 ; i < 32 ; i ++ )
254
+ close (dmabuf_fd [i ]);
255
+
256
+ /* Allocate and validate all buffers are zeroed */
257
+ for (i = 0 ; i < 32 ; i ++ ) {
258
+ ret = dmabuf_heap_alloc (heap_fd , size , 0 , & dmabuf_fd [i ]);
259
+ if (ret < 0 ) {
260
+ printf ("FAIL (Allocation (%i) failed)\n" , i );
261
+ goto out ;
262
+ }
263
+
264
+ /* mmap and validate everything is zero */
265
+ p = mmap (NULL , size , PROT_READ | PROT_WRITE , MAP_SHARED , dmabuf_fd [i ], 0 );
266
+ if (p == MAP_FAILED ) {
267
+ printf ("FAIL (mmap() failed!)\n" );
268
+ ret = -1 ;
269
+ goto out ;
270
+ }
271
+ dmabuf_sync (dmabuf_fd [i ], DMA_BUF_SYNC_START );
272
+ c = (char * )p ;
273
+ for (j = 0 ; j < size ; j ++ ) {
274
+ if (c [j ] != 0 ) {
275
+ printf ("FAIL (Allocated buffer not zeroed @ %i)\n" , j );
276
+ break ;
277
+ }
278
+ }
279
+ dmabuf_sync (dmabuf_fd [i ], DMA_BUF_SYNC_END );
280
+ munmap (p , size );
281
+ }
282
+ /* close them all */
283
+ for (i = 0 ; i < 32 ; i ++ )
284
+ close (dmabuf_fd [i ]);
285
+
286
+ close (heap_fd );
287
+ printf ("OK\n" );
288
+ return 0 ;
289
+
290
+ out :
291
+ while (i > 0 ) {
292
+ close (dmabuf_fd [i ]);
293
+ i -- ;
294
+ }
295
+ close (heap_fd );
296
+ return ret ;
297
+ }
298
+
221
299
/* Test the ioctl version compatibility w/ a smaller structure then expected */
222
300
static int dmabuf_heap_alloc_older (int fd , size_t len , unsigned int flags ,
223
301
int * dmabuf_fd )
@@ -292,23 +370,24 @@ static int test_alloc_compat(char *heap_name)
292
370
if (heap_fd < 0 )
293
371
return -1 ;
294
372
295
- printf ("Testing (theoretical)older alloc compat\n " );
373
+ printf (" Testing (theoretical)older alloc compat: " );
296
374
ret = dmabuf_heap_alloc_older (heap_fd , ONE_MEG , 0 , & dmabuf_fd );
297
375
if (ret ) {
298
- printf ("Older compat allocation failed!\n" );
376
+ printf ("FAIL ( Older compat allocation failed!) \n" );
299
377
ret = -1 ;
300
378
goto out ;
301
379
}
302
380
close (dmabuf_fd );
381
+ printf ("OK\n" );
303
382
304
- printf ("Testing (theoretical)newer alloc compat\n " );
383
+ printf (" Testing (theoretical)newer alloc compat: " );
305
384
ret = dmabuf_heap_alloc_newer (heap_fd , ONE_MEG , 0 , & dmabuf_fd );
306
385
if (ret ) {
307
- printf ("Newer compat allocation failed!\n" );
386
+ printf ("FAIL ( Newer compat allocation failed!) \n" );
308
387
ret = -1 ;
309
388
goto out ;
310
389
}
311
- printf ("Ioctl compatibility tests passed \n" );
390
+ printf ("OK \n" );
312
391
out :
313
392
if (dmabuf_fd >= 0 )
314
393
close (dmabuf_fd );
@@ -327,30 +406,30 @@ static int test_alloc_errors(char *heap_name)
327
406
if (heap_fd < 0 )
328
407
return -1 ;
329
408
330
- printf ("Testing expected error cases\n " );
409
+ printf (" Testing expected error cases: " );
331
410
ret = dmabuf_heap_alloc (0 , ONE_MEG , 0x111111 , & dmabuf_fd );
332
411
if (!ret ) {
333
- printf ("Did not see expected error (invalid fd)!\n" );
412
+ printf ("FAIL ( Did not see expected error (invalid fd)!) \n" );
334
413
ret = -1 ;
335
414
goto out ;
336
415
}
337
416
338
417
ret = dmabuf_heap_alloc (heap_fd , ONE_MEG , 0x111111 , & dmabuf_fd );
339
418
if (!ret ) {
340
- printf ("Did not see expected error (invalid heap flags)!\n" );
419
+ printf ("FAIL ( Did not see expected error (invalid heap flags)!) \n" );
341
420
ret = -1 ;
342
421
goto out ;
343
422
}
344
423
345
424
ret = dmabuf_heap_alloc_fdflags (heap_fd , ONE_MEG ,
346
425
~(O_RDWR | O_CLOEXEC ), 0 , & dmabuf_fd );
347
426
if (!ret ) {
348
- printf ("Did not see expected error (invalid fd flags)!\n" );
427
+ printf ("FAIL ( Did not see expected error (invalid fd flags)!) \n" );
349
428
ret = -1 ;
350
429
goto out ;
351
430
}
352
431
353
- printf ("Expected error checking passed \n" );
432
+ printf ("OK \n" );
354
433
ret = 0 ;
355
434
out :
356
435
if (dmabuf_fd >= 0 )
@@ -379,10 +458,20 @@ int main(void)
379
458
if (!strncmp (dir -> d_name , ".." , 3 ))
380
459
continue ;
381
460
461
+ printf ("Testing heap: %s\n" , dir -> d_name );
462
+ printf ("=======================================\n" );
382
463
ret = test_alloc_and_import (dir -> d_name );
383
464
if (ret )
384
465
break ;
385
466
467
+ ret = test_alloc_zeroed (dir -> d_name , 4 * 1024 );
468
+ if (ret )
469
+ break ;
470
+
471
+ ret = test_alloc_zeroed (dir -> d_name , ONE_MEG );
472
+ if (ret )
473
+ break ;
474
+
386
475
ret = test_alloc_compat (dir -> d_name );
387
476
if (ret )
388
477
break ;
0 commit comments