pagination for attachments page in wordpress

I have a piece of code for showing wordpress attachments. this code shows latest 40 attachments in the page but I want to add pagination for this code.
code:

    $args = array( 'post_type'  => 'attachment',
        'post_mime_type' => 'image',
             'post_status'    => 'inherit',
     'orderby' => 'date',
         'order' => 'DESC',
    'post__not_in'=>array($post->ID),
            'posts_per_page' => 40,
            'tax_query' => $tax_query );

    // finally run the query
    $loop = new WP_Query($args);

    if( $loop->have_posts() ) {

    ?>
          
<div class="masonry-items">
  <ul class="grid effect-2" id="grid">
    <?php while( $loop->have_posts() ) : $loop->the_post(); ?> 
           <li class="item">
      <div class="thumb">
      <a class="relative" href="<?php echo get_attachment_link( $image->ID ); ?>">
           <?php echo wp_get_attachment_image($image->ID, $size = 'thumbnail'); ?>
            <div class="caption absolute">
               <p>
                   <?php  echo wp_get_attachment_caption(get_post_thumbnail_id()); ?>
               </p>
             </div>
           </a>            
      </div>
    </li>
       <?php 
        endwhile;
    }
    wp_reset_query();
?>