• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012-2016 VMware, Inc.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of EITHER the GNU General Public License
6  * version 2 as published by the Free Software Foundation or the BSD
7  * 2-Clause License. This program is distributed in the hope that it
8  * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
9  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License version 2 for more details at
11  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program available in the file COPYING in the main
15  * directory of this source tree.
16  *
17  * The BSD 2-Clause License
18  *
19  *     Redistribution and use in source and binary forms, with or
20  *     without modification, are permitted provided that the following
21  *     conditions are met:
22  *
23  *      - Redistributions of source code must retain the above
24  *        copyright notice, this list of conditions and the following
25  *        disclaimer.
26  *
27  *      - Redistributions in binary form must reproduce the above
28  *        copyright notice, this list of conditions and the following
29  *        disclaimer in the documentation and/or other materials
30  *        provided with the distribution.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43  * OF THE POSSIBILITY OF SUCH DAMAGE.
44  */
45 
46 #ifndef __PVRDMA_RING_H__
47 #define __PVRDMA_RING_H__
48 
49 #include <linux/types.h>
50 
51 #define PVRDMA_INVALID_IDX	-1	/* Invalid index. */
52 
53 struct pvrdma_ring {
54 	atomic_t prod_tail;	/* Producer tail. */
55 	atomic_t cons_head;	/* Consumer head. */
56 };
57 
58 struct pvrdma_ring_state {
59 	struct pvrdma_ring tx;	/* Tx ring. */
60 	struct pvrdma_ring rx;	/* Rx ring. */
61 };
62 
pvrdma_idx_valid(__u32 idx,__u32 max_elems)63 static inline int pvrdma_idx_valid(__u32 idx, __u32 max_elems)
64 {
65 	/* Generates fewer instructions than a less-than. */
66 	return (idx & ~((max_elems << 1) - 1)) == 0;
67 }
68 
pvrdma_idx(atomic_t * var,__u32 max_elems)69 static inline __s32 pvrdma_idx(atomic_t *var, __u32 max_elems)
70 {
71 	const unsigned int idx = atomic_read(var);
72 
73 	if (pvrdma_idx_valid(idx, max_elems))
74 		return idx & (max_elems - 1);
75 	return PVRDMA_INVALID_IDX;
76 }
77 
pvrdma_idx_ring_inc(atomic_t * var,__u32 max_elems)78 static inline void pvrdma_idx_ring_inc(atomic_t *var, __u32 max_elems)
79 {
80 	__u32 idx = atomic_read(var) + 1;	/* Increment. */
81 
82 	idx &= (max_elems << 1) - 1;		/* Modulo size, flip gen. */
83 	atomic_set(var, idx);
84 }
85 
pvrdma_idx_ring_has_space(const struct pvrdma_ring * r,__u32 max_elems,__u32 * out_tail)86 static inline __s32 pvrdma_idx_ring_has_space(const struct pvrdma_ring *r,
87 					      __u32 max_elems, __u32 *out_tail)
88 {
89 	const __u32 tail = atomic_read(&r->prod_tail);
90 	const __u32 head = atomic_read(&r->cons_head);
91 
92 	if (pvrdma_idx_valid(tail, max_elems) &&
93 	    pvrdma_idx_valid(head, max_elems)) {
94 		*out_tail = tail & (max_elems - 1);
95 		return tail != (head ^ max_elems);
96 	}
97 	return PVRDMA_INVALID_IDX;
98 }
99 
pvrdma_idx_ring_has_data(const struct pvrdma_ring * r,__u32 max_elems,__u32 * out_head)100 static inline __s32 pvrdma_idx_ring_has_data(const struct pvrdma_ring *r,
101 					     __u32 max_elems, __u32 *out_head)
102 {
103 	const __u32 tail = atomic_read(&r->prod_tail);
104 	const __u32 head = atomic_read(&r->cons_head);
105 
106 	if (pvrdma_idx_valid(tail, max_elems) &&
107 	    pvrdma_idx_valid(head, max_elems)) {
108 		*out_head = head & (max_elems - 1);
109 		return tail != head;
110 	}
111 	return PVRDMA_INVALID_IDX;
112 }
113 
114 #endif /* __PVRDMA_RING_H__ */
115