Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2012-2016 VMware, Inc.  All rights reserved.
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of EITHER the GNU General Public License
0006  * version 2 as published by the Free Software Foundation or the BSD
0007  * 2-Clause License. This program is distributed in the hope that it
0008  * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
0009  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
0010  * See the GNU General Public License version 2 for more details at
0011  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
0012  *
0013  * You should have received a copy of the GNU General Public License
0014  * along with this program available in the file COPYING in the main
0015  * directory of this source tree.
0016  *
0017  * The BSD 2-Clause License
0018  *
0019  *     Redistribution and use in source and binary forms, with or
0020  *     without modification, are permitted provided that the following
0021  *     conditions are met:
0022  *
0023  *      - Redistributions of source code must retain the above
0024  *        copyright notice, this list of conditions and the following
0025  *        disclaimer.
0026  *
0027  *      - Redistributions in binary form must reproduce the above
0028  *        copyright notice, this list of conditions and the following
0029  *        disclaimer in the documentation and/or other materials
0030  *        provided with the distribution.
0031  *
0032  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0033  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0034  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0035  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0036  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
0037  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0038  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
0039  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0040  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
0041  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0042  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
0043  * OF THE POSSIBILITY OF SUCH DAMAGE.
0044  */
0045 
0046 #ifndef __PVRDMA_RING_H__
0047 #define __PVRDMA_RING_H__
0048 
0049 #include <linux/types.h>
0050 
0051 #define PVRDMA_INVALID_IDX  -1  /* Invalid index. */
0052 
0053 struct pvrdma_ring {
0054     atomic_t prod_tail; /* Producer tail. */
0055     atomic_t cons_head; /* Consumer head. */
0056 };
0057 
0058 struct pvrdma_ring_state {
0059     struct pvrdma_ring tx;  /* Tx ring. */
0060     struct pvrdma_ring rx;  /* Rx ring. */
0061 };
0062 
0063 static inline int pvrdma_idx_valid(__u32 idx, __u32 max_elems)
0064 {
0065     /* Generates fewer instructions than a less-than. */
0066     return (idx & ~((max_elems << 1) - 1)) == 0;
0067 }
0068 
0069 static inline __s32 pvrdma_idx(atomic_t *var, __u32 max_elems)
0070 {
0071     const unsigned int idx = atomic_read(var);
0072 
0073     if (pvrdma_idx_valid(idx, max_elems))
0074         return idx & (max_elems - 1);
0075     return PVRDMA_INVALID_IDX;
0076 }
0077 
0078 static inline void pvrdma_idx_ring_inc(atomic_t *var, __u32 max_elems)
0079 {
0080     __u32 idx = atomic_read(var) + 1;   /* Increment. */
0081 
0082     idx &= (max_elems << 1) - 1;        /* Modulo size, flip gen. */
0083     atomic_set(var, idx);
0084 }
0085 
0086 static inline __s32 pvrdma_idx_ring_has_space(const struct pvrdma_ring *r,
0087                           __u32 max_elems, __u32 *out_tail)
0088 {
0089     const __u32 tail = atomic_read(&r->prod_tail);
0090     const __u32 head = atomic_read(&r->cons_head);
0091 
0092     if (pvrdma_idx_valid(tail, max_elems) &&
0093         pvrdma_idx_valid(head, max_elems)) {
0094         *out_tail = tail & (max_elems - 1);
0095         return tail != (head ^ max_elems);
0096     }
0097     return PVRDMA_INVALID_IDX;
0098 }
0099 
0100 static inline __s32 pvrdma_idx_ring_has_data(const struct pvrdma_ring *r,
0101                          __u32 max_elems, __u32 *out_head)
0102 {
0103     const __u32 tail = atomic_read(&r->prod_tail);
0104     const __u32 head = atomic_read(&r->cons_head);
0105 
0106     if (pvrdma_idx_valid(tail, max_elems) &&
0107         pvrdma_idx_valid(head, max_elems)) {
0108         *out_head = head & (max_elems - 1);
0109         return tail != head;
0110     }
0111     return PVRDMA_INVALID_IDX;
0112 }
0113 
0114 #endif /* __PVRDMA_RING_H__ */