@@ -130,16 +130,13 @@ static int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags,
130130 dmabuf_fd );
131131}
132132
133- static void dmabuf_sync (int fd , int start_stop )
133+ static int dmabuf_sync (int fd , int start_stop )
134134{
135135 struct dma_buf_sync sync = {
136136 .flags = start_stop | DMA_BUF_SYNC_RW ,
137137 };
138- int ret ;
139138
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 );
143140}
144141
145142#define ONE_MEG (1024 * 1024)
@@ -151,16 +148,14 @@ static int test_alloc_and_import(char *heap_name)
151148 void * p = NULL ;
152149 int ret ;
153150
154- printf ("Testing heap: %s\n" , heap_name );
155-
156151 heap_fd = dmabuf_heap_open (heap_name );
157152 if (heap_fd < 0 )
158153 return -1 ;
159154
160- printf ("Allocating 1 MEG\n " );
155+ printf (" Testing allocation and importing: " );
161156 ret = dmabuf_heap_alloc (heap_fd , ONE_MEG , 0 , & dmabuf_fd );
162157 if (ret ) {
163- printf ("Allocation Failed!\n" );
158+ printf ("FAIL ( Allocation Failed!) \n" );
164159 ret = -1 ;
165160 goto out ;
166161 }
@@ -172,11 +167,10 @@ static int test_alloc_and_import(char *heap_name)
172167 dmabuf_fd ,
173168 0 );
174169 if (p == MAP_FAILED ) {
175- printf ("mmap() failed: %m \n" );
170+ printf ("FAIL ( mmap() failed) \n" );
176171 ret = -1 ;
177172 goto out ;
178173 }
179- printf ("mmap passed\n" );
180174
181175 dmabuf_sync (dmabuf_fd , DMA_BUF_SYNC_START );
182176 memset (p , 1 , ONE_MEG / 2 );
@@ -186,25 +180,31 @@ static int test_alloc_and_import(char *heap_name)
186180 importer_fd = open_vgem ();
187181 if (importer_fd < 0 ) {
188182 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+ }
191190 }
192191
193- ret = import_vgem_fd ( importer_fd , dmabuf_fd , & handle );
192+ ret = dmabuf_sync ( dmabuf_fd , DMA_BUF_SYNC_START );
194193 if (ret < 0 ) {
195- printf ("Failed to import buffer \n" );
194+ printf ("FAIL (DMA_BUF_SYNC_START failed!) \n" );
196195 goto out ;
197196 }
198- printf ("import passed\n" );
199197
200- dmabuf_sync (dmabuf_fd , DMA_BUF_SYNC_START );
201198 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+ }
204204
205205 close_handle (importer_fd , handle );
206206 ret = 0 ;
207-
207+ printf ( " OK\n" );
208208out :
209209 if (p )
210210 munmap (p , ONE_MEG );
@@ -218,6 +218,84 @@ static int test_alloc_and_import(char *heap_name)
218218 return ret ;
219219}
220220
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+
221299/* Test the ioctl version compatibility w/ a smaller structure then expected */
222300static int dmabuf_heap_alloc_older (int fd , size_t len , unsigned int flags ,
223301 int * dmabuf_fd )
@@ -292,23 +370,24 @@ static int test_alloc_compat(char *heap_name)
292370 if (heap_fd < 0 )
293371 return -1 ;
294372
295- printf ("Testing (theoretical)older alloc compat\n " );
373+ printf (" Testing (theoretical)older alloc compat: " );
296374 ret = dmabuf_heap_alloc_older (heap_fd , ONE_MEG , 0 , & dmabuf_fd );
297375 if (ret ) {
298- printf ("Older compat allocation failed!\n" );
376+ printf ("FAIL ( Older compat allocation failed!) \n" );
299377 ret = -1 ;
300378 goto out ;
301379 }
302380 close (dmabuf_fd );
381+ printf ("OK\n" );
303382
304- printf ("Testing (theoretical)newer alloc compat\n " );
383+ printf (" Testing (theoretical)newer alloc compat: " );
305384 ret = dmabuf_heap_alloc_newer (heap_fd , ONE_MEG , 0 , & dmabuf_fd );
306385 if (ret ) {
307- printf ("Newer compat allocation failed!\n" );
386+ printf ("FAIL ( Newer compat allocation failed!) \n" );
308387 ret = -1 ;
309388 goto out ;
310389 }
311- printf ("Ioctl compatibility tests passed \n" );
390+ printf ("OK \n" );
312391out :
313392 if (dmabuf_fd >= 0 )
314393 close (dmabuf_fd );
@@ -327,30 +406,30 @@ static int test_alloc_errors(char *heap_name)
327406 if (heap_fd < 0 )
328407 return -1 ;
329408
330- printf ("Testing expected error cases\n " );
409+ printf (" Testing expected error cases: " );
331410 ret = dmabuf_heap_alloc (0 , ONE_MEG , 0x111111 , & dmabuf_fd );
332411 if (!ret ) {
333- printf ("Did not see expected error (invalid fd)!\n" );
412+ printf ("FAIL ( Did not see expected error (invalid fd)!) \n" );
334413 ret = -1 ;
335414 goto out ;
336415 }
337416
338417 ret = dmabuf_heap_alloc (heap_fd , ONE_MEG , 0x111111 , & dmabuf_fd );
339418 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" );
341420 ret = -1 ;
342421 goto out ;
343422 }
344423
345424 ret = dmabuf_heap_alloc_fdflags (heap_fd , ONE_MEG ,
346425 ~(O_RDWR | O_CLOEXEC ), 0 , & dmabuf_fd );
347426 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" );
349428 ret = -1 ;
350429 goto out ;
351430 }
352431
353- printf ("Expected error checking passed \n" );
432+ printf ("OK \n" );
354433 ret = 0 ;
355434out :
356435 if (dmabuf_fd >= 0 )
@@ -379,10 +458,20 @@ int main(void)
379458 if (!strncmp (dir -> d_name , ".." , 3 ))
380459 continue ;
381460
461+ printf ("Testing heap: %s\n" , dir -> d_name );
462+ printf ("=======================================\n" );
382463 ret = test_alloc_and_import (dir -> d_name );
383464 if (ret )
384465 break ;
385466
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+
386475 ret = test_alloc_compat (dir -> d_name );
387476 if (ret )
388477 break ;
0 commit comments