OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / drivers / hv / ring_buffer.c
blob8af25a097d75ae2a189d5e5aa25c1b226c849dc7
1 /*
3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 * K. Y. Srinivasan <kys@microsoft.com>
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/hyperv.h>
30 #include "hyperv_vmbus.h"
33 /* #defines */
36 /* Amount of space to write to */
37 #define BYTES_AVAIL_TO_WRITE(r, w, z) \
38 ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
43 * hv_get_ringbuffer_availbytes()
45 * Get number of bytes available to read and to write to
46 * for the specified ring buffer
48 static inline void
49 hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
50 u32 *read, u32 *write)
52 u32 read_loc, write_loc;
54 smp_read_barrier_depends();
56 /* Capture the read/write indices before they changed */
57 read_loc = rbi->ring_buffer->read_index;
58 write_loc = rbi->ring_buffer->write_index;
60 *write = BYTES_AVAIL_TO_WRITE(read_loc, write_loc, rbi->ring_datasize);
61 *read = rbi->ring_datasize - *write;
65 * hv_get_next_write_location()
67 * Get the next write location for the specified ring buffer
70 static inline u32
71 hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
73 u32 next = ring_info->ring_buffer->write_index;
75 return next;
79 * hv_set_next_write_location()
81 * Set the next write location for the specified ring buffer
84 static inline void
85 hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
86 u32 next_write_location)
88 ring_info->ring_buffer->write_index = next_write_location;
92 * hv_get_next_read_location()
94 * Get the next read location for the specified ring buffer
96 static inline u32
97 hv_get_next_read_location(struct hv_ring_buffer_info *ring_info)
99 u32 next = ring_info->ring_buffer->read_index;
101 return next;
105 * hv_get_next_readlocation_withoffset()
107 * Get the next read location + offset for the specified ring buffer.
108 * This allows the caller to skip
110 static inline u32
111 hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
112 u32 offset)
114 u32 next = ring_info->ring_buffer->read_index;
116 next += offset;
117 next %= ring_info->ring_datasize;
119 return next;
124 * hv_set_next_read_location()
126 * Set the next read location for the specified ring buffer
129 static inline void
130 hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
131 u32 next_read_location)
133 ring_info->ring_buffer->read_index = next_read_location;
139 * hv_get_ring_buffer()
141 * Get the start of the ring buffer
143 static inline void *
144 hv_get_ring_buffer(struct hv_ring_buffer_info *ring_info)
146 return (void *)ring_info->ring_buffer->buffer;
152 * hv_get_ring_buffersize()
154 * Get the size of the ring buffer
156 static inline u32
157 hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
159 return ring_info->ring_datasize;
164 * hv_get_ring_bufferindices()
166 * Get the read and write indices as u64 of the specified ring buffer
169 static inline u64
170 hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
172 return (u64)ring_info->ring_buffer->write_index << 32;
177 * hv_copyfrom_ringbuffer()
179 * Helper routine to copy to source from ring buffer.
180 * Assume there is enough room. Handles wrap-around in src case only!!
183 static u32 hv_copyfrom_ringbuffer(
184 struct hv_ring_buffer_info *ring_info,
185 void *dest,
186 u32 destlen,
187 u32 start_read_offset)
189 void *ring_buffer = hv_get_ring_buffer(ring_info);
190 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
192 u32 frag_len;
194 /* wrap-around detected at the src */
195 if (destlen > ring_buffer_size - start_read_offset) {
196 frag_len = ring_buffer_size - start_read_offset;
198 memcpy(dest, ring_buffer + start_read_offset, frag_len);
199 memcpy(dest + frag_len, ring_buffer, destlen - frag_len);
200 } else
202 memcpy(dest, ring_buffer + start_read_offset, destlen);
205 start_read_offset += destlen;
206 start_read_offset %= ring_buffer_size;
208 return start_read_offset;
214 * hv_copyto_ringbuffer()
216 * Helper routine to copy from source to ring buffer.
217 * Assume there is enough room. Handles wrap-around in dest case only!!
220 static u32 hv_copyto_ringbuffer(
221 struct hv_ring_buffer_info *ring_info,
222 u32 start_write_offset,
223 void *src,
224 u32 srclen)
226 void *ring_buffer = hv_get_ring_buffer(ring_info);
227 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
228 u32 frag_len;
230 /* wrap-around detected! */
231 if (srclen > ring_buffer_size - start_write_offset) {
232 frag_len = ring_buffer_size - start_write_offset;
233 memcpy(ring_buffer + start_write_offset, src, frag_len);
234 memcpy(ring_buffer, src + frag_len, srclen - frag_len);
235 } else
236 memcpy(ring_buffer + start_write_offset, src, srclen);
238 start_write_offset += srclen;
239 start_write_offset %= ring_buffer_size;
241 return start_write_offset;
246 * hv_ringbuffer_get_debuginfo()
248 * Get various debug metrics for the specified ring buffer
251 void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
252 struct hv_ring_buffer_debug_info *debug_info)
254 u32 bytes_avail_towrite;
255 u32 bytes_avail_toread;
257 if (ring_info->ring_buffer) {
258 hv_get_ringbuffer_availbytes(ring_info,
259 &bytes_avail_toread,
260 &bytes_avail_towrite);
262 debug_info->bytes_avail_toread = bytes_avail_toread;
263 debug_info->bytes_avail_towrite = bytes_avail_towrite;
264 debug_info->current_read_index =
265 ring_info->ring_buffer->read_index;
266 debug_info->current_write_index =
267 ring_info->ring_buffer->write_index;
268 debug_info->current_interrupt_mask =
269 ring_info->ring_buffer->interrupt_mask;
276 * hv_get_ringbuffer_interrupt_mask()
278 * Get the interrupt mask for the specified ring buffer
281 u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *rbi)
283 return rbi->ring_buffer->interrupt_mask;
288 * hv_ringbuffer_init()
290 *Initialize the ring buffer
293 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
294 void *buffer, u32 buflen)
296 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
297 return -EINVAL;
299 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
301 ring_info->ring_buffer = (struct hv_ring_buffer *)buffer;
302 ring_info->ring_buffer->read_index =
303 ring_info->ring_buffer->write_index = 0;
305 ring_info->ring_size = buflen;
306 ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
308 spin_lock_init(&ring_info->ring_lock);
310 return 0;
315 * hv_ringbuffer_cleanup()
317 * Cleanup the ring buffer
320 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
326 * hv_ringbuffer_write()
328 * Write to the ring buffer
331 int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info,
332 struct scatterlist *sglist, u32 sgcount)
334 int i = 0;
335 u32 bytes_avail_towrite;
336 u32 bytes_avail_toread;
337 u32 totalbytes_towrite = 0;
339 struct scatterlist *sg;
340 u32 next_write_location;
341 u64 prev_indices = 0;
342 unsigned long flags;
344 for_each_sg(sglist, sg, sgcount, i)
346 totalbytes_towrite += sg->length;
349 totalbytes_towrite += sizeof(u64);
351 spin_lock_irqsave(&outring_info->ring_lock, flags);
353 hv_get_ringbuffer_availbytes(outring_info,
354 &bytes_avail_toread,
355 &bytes_avail_towrite);
358 /* If there is only room for the packet, assume it is full. */
359 /* Otherwise, the next time around, we think the ring buffer */
360 /* is empty since the read index == write index */
361 if (bytes_avail_towrite <= totalbytes_towrite) {
362 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
363 return -EAGAIN;
366 /* Write to the ring buffer */
367 next_write_location = hv_get_next_write_location(outring_info);
369 for_each_sg(sglist, sg, sgcount, i)
371 next_write_location = hv_copyto_ringbuffer(outring_info,
372 next_write_location,
373 sg_virt(sg),
374 sg->length);
377 /* Set previous packet start */
378 prev_indices = hv_get_ring_bufferindices(outring_info);
380 next_write_location = hv_copyto_ringbuffer(outring_info,
381 next_write_location,
382 &prev_indices,
383 sizeof(u64));
385 /* Make sure we flush all writes before updating the writeIndex */
386 smp_wmb();
388 /* Now, update the write location */
389 hv_set_next_write_location(outring_info, next_write_location);
392 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
393 return 0;
399 * hv_ringbuffer_peek()
401 * Read without advancing the read index
404 int hv_ringbuffer_peek(struct hv_ring_buffer_info *Inring_info,
405 void *Buffer, u32 buflen)
407 u32 bytes_avail_towrite;
408 u32 bytes_avail_toread;
409 u32 next_read_location = 0;
410 unsigned long flags;
412 spin_lock_irqsave(&Inring_info->ring_lock, flags);
414 hv_get_ringbuffer_availbytes(Inring_info,
415 &bytes_avail_toread,
416 &bytes_avail_towrite);
418 /* Make sure there is something to read */
419 if (bytes_avail_toread < buflen) {
421 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
423 return -EAGAIN;
426 /* Convert to byte offset */
427 next_read_location = hv_get_next_read_location(Inring_info);
429 next_read_location = hv_copyfrom_ringbuffer(Inring_info,
430 Buffer,
431 buflen,
432 next_read_location);
434 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
436 return 0;
442 * hv_ringbuffer_read()
444 * Read and advance the read index
447 int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer,
448 u32 buflen, u32 offset)
450 u32 bytes_avail_towrite;
451 u32 bytes_avail_toread;
452 u32 next_read_location = 0;
453 u64 prev_indices = 0;
454 unsigned long flags;
456 if (buflen <= 0)
457 return -EINVAL;
459 spin_lock_irqsave(&inring_info->ring_lock, flags);
461 hv_get_ringbuffer_availbytes(inring_info,
462 &bytes_avail_toread,
463 &bytes_avail_towrite);
465 /* Make sure there is something to read */
466 if (bytes_avail_toread < buflen) {
467 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
469 return -EAGAIN;
472 next_read_location =
473 hv_get_next_readlocation_withoffset(inring_info, offset);
475 next_read_location = hv_copyfrom_ringbuffer(inring_info,
476 buffer,
477 buflen,
478 next_read_location);
480 next_read_location = hv_copyfrom_ringbuffer(inring_info,
481 &prev_indices,
482 sizeof(u64),
483 next_read_location);
485 /* Make sure all reads are done before we update the read index since */
486 /* the writer may start writing to the read area once the read index */
487 /*is updated */
488 smp_mb();
490 /* Update the read index */
491 hv_set_next_read_location(inring_info, next_read_location);
493 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
495 return 0;