Revert "drm/i915: Enable GMBUS for post-gen2 chipsets"
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / ring_buffer.c
blob66688fb69741b4165cadaa9fdd0652e9b142cee2
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>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include "logging.h"
27 #include "ring_buffer.h"
30 /* #defines */
33 /* Amount of space to write to */
34 #define BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
37 /*++
39 Name:
40 get_ringbuffer_availbytes()
42 Description:
43 Get number of bytes available to read and to write to
44 for the specified ring buffer
46 --*/
47 static inline void
48 get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
49 u32 *read, u32 *write)
51 u32 read_loc, write_loc;
53 /* Capture the read/write indices before they changed */
54 read_loc = rbi->ring_buffer->read_index;
55 write_loc = rbi->ring_buffer->write_index;
57 *write = BYTES_AVAIL_TO_WRITE(read_loc, write_loc, rbi->ring_datasize);
58 *read = rbi->ring_datasize - *write;
61 /*++
63 Name:
64 get_next_write_location()
66 Description:
67 Get the next write location for the specified ring buffer
69 --*/
70 static inline u32
71 get_next_write_location(struct hv_ring_buffer_info *ring_info)
73 u32 next = ring_info->ring_buffer->write_index;
75 /* ASSERT(next < ring_info->RingDataSize); */
77 return next;
80 /*++
82 Name:
83 set_next_write_location()
85 Description:
86 Set the next write location for the specified ring buffer
88 --*/
89 static inline void
90 set_next_write_location(struct hv_ring_buffer_info *ring_info,
91 u32 next_write_location)
93 ring_info->ring_buffer->write_index = next_write_location;
96 /*++
98 Name:
99 get_next_read_location()
101 Description:
102 Get the next read location for the specified ring buffer
104 --*/
105 static inline u32
106 get_next_read_location(struct hv_ring_buffer_info *ring_info)
108 u32 next = ring_info->ring_buffer->read_index;
110 /* ASSERT(next < ring_info->RingDataSize); */
112 return next;
115 /*++
117 Name:
118 get_next_readlocation_withoffset()
120 Description:
121 Get the next read location + offset for the specified ring buffer.
122 This allows the caller to skip
124 --*/
125 static inline u32
126 get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info,
127 u32 offset)
129 u32 next = ring_info->ring_buffer->read_index;
131 /* ASSERT(next < ring_info->RingDataSize); */
132 next += offset;
133 next %= ring_info->ring_datasize;
135 return next;
138 /*++
140 Name:
141 set_next_read_location()
143 Description:
144 Set the next read location for the specified ring buffer
146 --*/
147 static inline void
148 set_next_read_location(struct hv_ring_buffer_info *ring_info,
149 u32 next_read_location)
151 ring_info->ring_buffer->read_index = next_read_location;
155 /*++
157 Name:
158 get_ring_buffer()
160 Description:
161 Get the start of the ring buffer
163 --*/
164 static inline void *
165 get_ring_buffer(struct hv_ring_buffer_info *ring_info)
167 return (void *)ring_info->ring_buffer->buffer;
171 /*++
173 Name:
174 get_ring_buffersize()
176 Description:
177 Get the size of the ring buffer
179 --*/
180 static inline u32
181 get_ring_buffersize(struct hv_ring_buffer_info *ring_info)
183 return ring_info->ring_datasize;
186 /*++
188 Name:
189 get_ring_bufferindices()
191 Description:
192 Get the read and write indices as u64 of the specified ring buffer
194 --*/
195 static inline u64
196 get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
198 return (u64)ring_info->ring_buffer->write_index << 32;
202 /*++
204 Name:
205 dump_ring_info()
207 Description:
208 Dump out to console the ring buffer info
210 --*/
211 void dump_ring_info(struct hv_ring_buffer_info *ring_info, char *prefix)
213 u32 bytes_avail_towrite;
214 u32 bytes_avail_toread;
216 get_ringbuffer_availbytes(ring_info,
217 &bytes_avail_toread,
218 &bytes_avail_towrite);
220 DPRINT(VMBUS,
221 DEBUG_RING_LVL,
222 "%s <<ringinfo %p buffer %p avail write %u "
223 "avail read %u read idx %u write idx %u>>",
224 prefix,
225 ring_info,
226 ring_info->ring_buffer->buffer,
227 bytes_avail_towrite,
228 bytes_avail_toread,
229 ring_info->ring_buffer->read_index,
230 ring_info->ring_buffer->write_index);
234 /* Internal routines */
236 static u32
237 copyto_ringbuffer(
238 struct hv_ring_buffer_info *ring_info,
239 u32 start_write_offset,
240 void *src,
241 u32 srclen);
243 static u32
244 copyfrom_ringbuffer(
245 struct hv_ring_buffer_info *ring_info,
246 void *dest,
247 u32 destlen,
248 u32 start_read_offset);
252 /*++
254 Name:
255 ringbuffer_get_debuginfo()
257 Description:
258 Get various debug metrics for the specified ring buffer
260 --*/
261 void ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
262 struct hv_ring_buffer_debug_info *debug_info)
264 u32 bytes_avail_towrite;
265 u32 bytes_avail_toread;
267 if (ring_info->ring_buffer) {
268 get_ringbuffer_availbytes(ring_info,
269 &bytes_avail_toread,
270 &bytes_avail_towrite);
272 debug_info->bytes_avail_toread = bytes_avail_toread;
273 debug_info->bytes_avail_towrite = bytes_avail_towrite;
274 debug_info->current_read_index =
275 ring_info->ring_buffer->read_index;
276 debug_info->current_write_index =
277 ring_info->ring_buffer->write_index;
278 debug_info->current_interrupt_mask =
279 ring_info->ring_buffer->interrupt_mask;
284 /*++
286 Name:
287 get_ringbuffer_interrupt_mask()
289 Description:
290 Get the interrupt mask for the specified ring buffer
292 --*/
293 u32 get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *rbi)
295 return rbi->ring_buffer->interrupt_mask;
298 /*++
300 Name:
301 ringbuffer_init()
303 Description:
304 Initialize the ring buffer
306 --*/
307 int ringbuffer_init(struct hv_ring_buffer_info *ring_info,
308 void *buffer, u32 buflen)
310 if (sizeof(struct hv_ring_buffer) != PAGE_SIZE)
311 return -EINVAL;
313 memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
315 ring_info->ring_buffer = (struct hv_ring_buffer *)buffer;
316 ring_info->ring_buffer->read_index =
317 ring_info->ring_buffer->write_index = 0;
319 ring_info->ring_size = buflen;
320 ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer);
322 spin_lock_init(&ring_info->ring_lock);
324 return 0;
327 /*++
329 Name:
330 ringbuffer_cleanup()
332 Description:
333 Cleanup the ring buffer
335 --*/
336 void ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
340 /*++
342 Name:
343 ringbuffer_write()
345 Description:
346 Write to the ring buffer
348 --*/
349 int ringbuffer_write(struct hv_ring_buffer_info *outring_info,
350 struct scatterlist *sglist, u32 sgcount)
352 int i = 0;
353 u32 bytes_avail_towrite;
354 u32 bytes_avail_toread;
355 u32 totalbytes_towrite = 0;
357 struct scatterlist *sg;
358 volatile u32 next_write_location;
359 u64 prev_indices = 0;
360 unsigned long flags;
362 for_each_sg(sglist, sg, sgcount, i)
364 totalbytes_towrite += sg->length;
367 totalbytes_towrite += sizeof(u64);
369 spin_lock_irqsave(&outring_info->ring_lock, flags);
371 get_ringbuffer_availbytes(outring_info,
372 &bytes_avail_toread,
373 &bytes_avail_towrite);
375 DPRINT_DBG(VMBUS, "Writing %u bytes...", totalbytes_towrite);
377 /* Dumpring_info(Outring_info, "BEFORE "); */
379 /* If there is only room for the packet, assume it is full. */
380 /* Otherwise, the next time around, we think the ring buffer */
381 /* is empty since the read index == write index */
382 if (bytes_avail_towrite <= totalbytes_towrite) {
383 DPRINT_DBG(VMBUS,
384 "No more space left on outbound ring buffer "
385 "(needed %u, avail %u)",
386 totalbytes_towrite,
387 bytes_avail_towrite);
389 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
390 return -1;
393 /* Write to the ring buffer */
394 next_write_location = get_next_write_location(outring_info);
396 for_each_sg(sglist, sg, sgcount, i)
398 next_write_location = copyto_ringbuffer(outring_info,
399 next_write_location,
400 sg_virt(sg),
401 sg->length);
404 /* Set previous packet start */
405 prev_indices = get_ring_bufferindices(outring_info);
407 next_write_location = copyto_ringbuffer(outring_info,
408 next_write_location,
409 &prev_indices,
410 sizeof(u64));
412 /* Make sure we flush all writes before updating the writeIndex */
413 mb();
415 /* Now, update the write location */
416 set_next_write_location(outring_info, next_write_location);
418 /* Dumpring_info(Outring_info, "AFTER "); */
420 spin_unlock_irqrestore(&outring_info->ring_lock, flags);
421 return 0;
425 /*++
427 Name:
428 ringbuffer_peek()
430 Description:
431 Read without advancing the read index
433 --*/
434 int ringbuffer_peek(struct hv_ring_buffer_info *Inring_info,
435 void *Buffer, u32 buflen)
437 u32 bytes_avail_towrite;
438 u32 bytes_avail_toread;
439 u32 next_read_location = 0;
440 unsigned long flags;
442 spin_lock_irqsave(&Inring_info->ring_lock, flags);
444 get_ringbuffer_availbytes(Inring_info,
445 &bytes_avail_toread,
446 &bytes_avail_towrite);
448 /* Make sure there is something to read */
449 if (bytes_avail_toread < buflen) {
450 /* DPRINT_DBG(VMBUS,
451 "got callback but not enough to read "
452 "<avail to read %d read size %d>!!",
453 bytes_avail_toread,
454 BufferLen); */
456 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
458 return -1;
461 /* Convert to byte offset */
462 next_read_location = get_next_read_location(Inring_info);
464 next_read_location = copyfrom_ringbuffer(Inring_info,
465 Buffer,
466 buflen,
467 next_read_location);
469 spin_unlock_irqrestore(&Inring_info->ring_lock, flags);
471 return 0;
475 /*++
477 Name:
478 ringbuffer_read()
480 Description:
481 Read and advance the read index
483 --*/
484 int ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer,
485 u32 buflen, u32 offset)
487 u32 bytes_avail_towrite;
488 u32 bytes_avail_toread;
489 u32 next_read_location = 0;
490 u64 prev_indices = 0;
491 unsigned long flags;
493 if (buflen <= 0)
494 return -EINVAL;
496 spin_lock_irqsave(&inring_info->ring_lock, flags);
498 get_ringbuffer_availbytes(inring_info,
499 &bytes_avail_toread,
500 &bytes_avail_towrite);
502 DPRINT_DBG(VMBUS, "Reading %u bytes...", buflen);
504 /* Dumpring_info(Inring_info, "BEFORE "); */
506 /* Make sure there is something to read */
507 if (bytes_avail_toread < buflen) {
508 DPRINT_DBG(VMBUS,
509 "got callback but not enough to read "
510 "<avail to read %d read size %d>!!",
511 bytes_avail_toread,
512 buflen);
514 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
516 return -1;
519 next_read_location =
520 get_next_readlocation_withoffset(inring_info, offset);
522 next_read_location = copyfrom_ringbuffer(inring_info,
523 buffer,
524 buflen,
525 next_read_location);
527 next_read_location = copyfrom_ringbuffer(inring_info,
528 &prev_indices,
529 sizeof(u64),
530 next_read_location);
532 /* Make sure all reads are done before we update the read index since */
533 /* the writer may start writing to the read area once the read index */
534 /*is updated */
535 mb();
537 /* Update the read index */
538 set_next_read_location(inring_info, next_read_location);
540 /* Dumpring_info(Inring_info, "AFTER "); */
542 spin_unlock_irqrestore(&inring_info->ring_lock, flags);
544 return 0;
548 /*++
550 Name:
551 copyto_ringbuffer()
553 Description:
554 Helper routine to copy from source to ring buffer.
555 Assume there is enough room. Handles wrap-around in dest case only!!
557 --*/
558 static u32
559 copyto_ringbuffer(
560 struct hv_ring_buffer_info *ring_info,
561 u32 start_write_offset,
562 void *src,
563 u32 srclen)
565 void *ring_buffer = get_ring_buffer(ring_info);
566 u32 ring_buffer_size = get_ring_buffersize(ring_info);
567 u32 frag_len;
569 /* wrap-around detected! */
570 if (srclen > ring_buffer_size - start_write_offset) {
571 DPRINT_DBG(VMBUS, "wrap-around detected!");
573 frag_len = ring_buffer_size - start_write_offset;
574 memcpy(ring_buffer + start_write_offset, src, frag_len);
575 memcpy(ring_buffer, src + frag_len, srclen - frag_len);
576 } else
577 memcpy(ring_buffer + start_write_offset, src, srclen);
579 start_write_offset += srclen;
580 start_write_offset %= ring_buffer_size;
582 return start_write_offset;
586 /*++
588 Name:
589 copyfrom_ringbuffer()
591 Description:
592 Helper routine to copy to source from ring buffer.
593 Assume there is enough room. Handles wrap-around in src case only!!
595 --*/
596 static u32
597 copyfrom_ringbuffer(
598 struct hv_ring_buffer_info *ring_info,
599 void *dest,
600 u32 destlen,
601 u32 start_read_offset)
603 void *ring_buffer = get_ring_buffer(ring_info);
604 u32 ring_buffer_size = get_ring_buffersize(ring_info);
606 u32 frag_len;
608 /* wrap-around detected at the src */
609 if (destlen > ring_buffer_size - start_read_offset) {
610 DPRINT_DBG(VMBUS, "src wrap-around detected!");
612 frag_len = ring_buffer_size - start_read_offset;
614 memcpy(dest, ring_buffer + start_read_offset, frag_len);
615 memcpy(dest + frag_len, ring_buffer, destlen - frag_len);
616 } else
618 memcpy(dest, ring_buffer + start_read_offset, destlen);
621 start_read_offset += destlen;
622 start_read_offset %= ring_buffer_size;
624 return start_read_offset;
628 /* eof */