Skip to content

Commit 9ad8b64

Browse files
sudeepduttgregkh
authored andcommitted
misc: mic: SCIF ring buffer infrastructure
SCIF ring buffer is a single producer, single consumer byte stream ring buffer optimized for avoiding reads across the PCIe bus while adding the required barriers and hardware workarounds for the MIC Coprocessor. The ring buffer is used to implement a receive queue for SCIF driver messaging between two nodes and for byte stream messaging between SCIF endpoints. The existing in-kernel ring buffer was not reused since it has not been designed for our use across the PCIe bus where each node runs an independent OS. Each SCIF node has a receive queue for every other SCIF node, and each connected endpoint has a receive queue for messages from its peer. This pair of receive queues is referred to as a SCIF queue pair. Reviewed-by: Nikhil Rao <[email protected]> Reviewed-by: Ashutosh Dixit <[email protected]> Signed-off-by: Sudeep Dutt <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 7df20f2 commit 9ad8b64

File tree

2 files changed

+348
-0
lines changed

2 files changed

+348
-0
lines changed

drivers/misc/mic/scif/scif_rb.c

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
/*
2+
* Intel MIC Platform Software Stack (MPSS)
3+
*
4+
* Copyright(c) 2014 Intel Corporation.
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License, version 2, as
8+
* published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful, but
11+
* WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* General Public License for more details.
14+
*
15+
* Intel SCIF driver.
16+
*
17+
*/
18+
#include <linux/circ_buf.h>
19+
#include <linux/types.h>
20+
#include <linux/io.h>
21+
22+
#include "scif_rb.h"
23+
24+
#define scif_rb_ring_cnt(head, tail, size) CIRC_CNT(head, tail, size)
25+
#define scif_rb_ring_space(head, tail, size) CIRC_SPACE(head, tail, size)
26+
27+
/**
28+
* scif_rb_init - Initializes the ring buffer
29+
* @rb: ring buffer
30+
* @read_ptr: A pointer to the read offset
31+
* @write_ptr: A pointer to the write offset
32+
* @rb_base: A pointer to the base of the ring buffer
33+
* @size: The size of the ring buffer in powers of two
34+
*/
35+
void scif_rb_init(struct scif_rb *rb, u32 *read_ptr, u32 *write_ptr,
36+
void *rb_base, u8 size)
37+
{
38+
rb->rb_base = rb_base;
39+
rb->size = (1 << size);
40+
rb->read_ptr = read_ptr;
41+
rb->write_ptr = write_ptr;
42+
rb->current_read_offset = *read_ptr;
43+
rb->current_write_offset = *write_ptr;
44+
}
45+
46+
/* Copies a message to the ring buffer -- handles the wrap around case */
47+
static void memcpy_torb(struct scif_rb *rb, void *header,
48+
void *msg, u32 size)
49+
{
50+
u32 size1, size2;
51+
52+
if (header + size >= rb->rb_base + rb->size) {
53+
/* Need to call two copies if it wraps around */
54+
size1 = (u32)(rb->rb_base + rb->size - header);
55+
size2 = size - size1;
56+
memcpy_toio((void __iomem __force *)header, msg, size1);
57+
memcpy_toio((void __iomem __force *)rb->rb_base,
58+
msg + size1, size2);
59+
} else {
60+
memcpy_toio((void __iomem __force *)header, msg, size);
61+
}
62+
}
63+
64+
/* Copies a message from the ring buffer -- handles the wrap around case */
65+
static void memcpy_fromrb(struct scif_rb *rb, void *header,
66+
void *msg, u32 size)
67+
{
68+
u32 size1, size2;
69+
70+
if (header + size >= rb->rb_base + rb->size) {
71+
/* Need to call two copies if it wraps around */
72+
size1 = (u32)(rb->rb_base + rb->size - header);
73+
size2 = size - size1;
74+
memcpy_fromio(msg, (void __iomem __force *)header, size1);
75+
memcpy_fromio(msg + size1,
76+
(void __iomem __force *)rb->rb_base, size2);
77+
} else {
78+
memcpy_fromio(msg, (void __iomem __force *)header, size);
79+
}
80+
}
81+
82+
/**
83+
* scif_rb_space - Query space available for writing to the RB
84+
* @rb: ring buffer
85+
*
86+
* Return: size available for writing to RB in bytes.
87+
*/
88+
u32 scif_rb_space(struct scif_rb *rb)
89+
{
90+
rb->current_read_offset = *rb->read_ptr;
91+
/*
92+
* Update from the HW read pointer only once the peer has exposed the
93+
* new empty slot. This barrier is paired with the memory barrier
94+
* scif_rb_update_read_ptr()
95+
*/
96+
mb();
97+
return scif_rb_ring_space(rb->current_write_offset,
98+
rb->current_read_offset, rb->size);
99+
}
100+
101+
/**
102+
* scif_rb_write - Write a message to the RB
103+
* @rb: ring buffer
104+
* @msg: buffer to send the message. Must be at least size bytes long
105+
* @size: the size (in bytes) to be copied to the RB
106+
*
107+
* This API does not block if there isn't enough space in the RB.
108+
* Returns: 0 on success or -ENOMEM on failure
109+
*/
110+
int scif_rb_write(struct scif_rb *rb, void *msg, u32 size)
111+
{
112+
void *header;
113+
114+
if (scif_rb_space(rb) < size)
115+
return -ENOMEM;
116+
header = rb->rb_base + rb->current_write_offset;
117+
memcpy_torb(rb, header, msg, size);
118+
/*
119+
* Wait until scif_rb_commit(). Update the local ring
120+
* buffer data, not the shared data until commit.
121+
*/
122+
rb->current_write_offset =
123+
(rb->current_write_offset + size) & (rb->size - 1);
124+
return 0;
125+
}
126+
127+
/**
128+
* scif_rb_commit - To submit the message to let the peer fetch it
129+
* @rb: ring buffer
130+
*/
131+
void scif_rb_commit(struct scif_rb *rb)
132+
{
133+
/*
134+
* We must ensure ordering between the all the data committed
135+
* previously before we expose the new message to the peer by
136+
* updating the write_ptr. This write barrier is paired with
137+
* the read barrier in scif_rb_count(..)
138+
*/
139+
wmb();
140+
ACCESS_ONCE(*rb->write_ptr) = rb->current_write_offset;
141+
#ifdef CONFIG_INTEL_MIC_CARD
142+
/*
143+
* X100 Si bug: For the case where a Core is performing an EXT_WR
144+
* followed by a Doorbell Write, the Core must perform two EXT_WR to the
145+
* same address with the same data before it does the Doorbell Write.
146+
* This way, if ordering is violated for the Interrupt Message, it will
147+
* fall just behind the first Posted associated with the first EXT_WR.
148+
*/
149+
ACCESS_ONCE(*rb->write_ptr) = rb->current_write_offset;
150+
#endif
151+
}
152+
153+
/**
154+
* scif_rb_get - To get next message from the ring buffer
155+
* @rb: ring buffer
156+
* @size: Number of bytes to be read
157+
*
158+
* Return: NULL if no bytes to be read from the ring buffer, otherwise the
159+
* pointer to the next byte
160+
*/
161+
static void *scif_rb_get(struct scif_rb *rb, u32 size)
162+
{
163+
void *header = NULL;
164+
165+
if (scif_rb_count(rb, size) >= size)
166+
header = rb->rb_base + rb->current_read_offset;
167+
return header;
168+
}
169+
170+
/*
171+
* scif_rb_get_next - Read from ring buffer.
172+
* @rb: ring buffer
173+
* @msg: buffer to hold the message. Must be at least size bytes long
174+
* @size: Number of bytes to be read
175+
*
176+
* Return: number of bytes read if available bytes are >= size, otherwise
177+
* returns zero.
178+
*/
179+
u32 scif_rb_get_next(struct scif_rb *rb, void *msg, u32 size)
180+
{
181+
void *header = NULL;
182+
int read_size = 0;
183+
184+
header = scif_rb_get(rb, size);
185+
if (header) {
186+
u32 next_cmd_offset =
187+
(rb->current_read_offset + size) & (rb->size - 1);
188+
189+
read_size = size;
190+
rb->current_read_offset = next_cmd_offset;
191+
memcpy_fromrb(rb, header, msg, size);
192+
}
193+
return read_size;
194+
}
195+
196+
/**
197+
* scif_rb_update_read_ptr
198+
* @rb: ring buffer
199+
*/
200+
void scif_rb_update_read_ptr(struct scif_rb *rb)
201+
{
202+
u32 new_offset;
203+
204+
new_offset = rb->current_read_offset;
205+
/*
206+
* We must ensure ordering between the all the data committed or read
207+
* previously before we expose the empty slot to the peer by updating
208+
* the read_ptr. This barrier is paired with the memory barrier in
209+
* scif_rb_space(..)
210+
*/
211+
mb();
212+
ACCESS_ONCE(*rb->read_ptr) = new_offset;
213+
#ifdef CONFIG_INTEL_MIC_CARD
214+
/*
215+
* X100 Si Bug: For the case where a Core is performing an EXT_WR
216+
* followed by a Doorbell Write, the Core must perform two EXT_WR to the
217+
* same address with the same data before it does the Doorbell Write.
218+
* This way, if ordering is violated for the Interrupt Message, it will
219+
* fall just behind the first Posted associated with the first EXT_WR.
220+
*/
221+
ACCESS_ONCE(*rb->read_ptr) = new_offset;
222+
#endif
223+
}
224+
225+
/**
226+
* scif_rb_count
227+
* @rb: ring buffer
228+
* @size: Number of bytes expected to be read
229+
*
230+
* Return: number of bytes that can be read from the RB
231+
*/
232+
u32 scif_rb_count(struct scif_rb *rb, u32 size)
233+
{
234+
if (scif_rb_ring_cnt(rb->current_write_offset,
235+
rb->current_read_offset,
236+
rb->size) < size) {
237+
rb->current_write_offset = *rb->write_ptr;
238+
/*
239+
* Update from the HW write pointer if empty only once the peer
240+
* has exposed the new message. This read barrier is paired
241+
* with the write barrier in scif_rb_commit(..)
242+
*/
243+
smp_rmb();
244+
}
245+
return scif_rb_ring_cnt(rb->current_write_offset,
246+
rb->current_read_offset,
247+
rb->size);
248+
}

drivers/misc/mic/scif/scif_rb.h

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Intel MIC Platform Software Stack (MPSS)
3+
*
4+
* This file is provided under a dual BSD/GPLv2 license. When using or
5+
* redistributing this file, you may do so under either license.
6+
*
7+
* GPL LICENSE SUMMARY
8+
*
9+
* Copyright(c) 2014 Intel Corporation.
10+
*
11+
* This program is free software; you can redistribute it and/or modify
12+
* it under the terms of version 2 of the GNU General Public License as
13+
* published by the Free Software Foundation.
14+
*
15+
* This program is distributed in the hope that it will be useful, but
16+
* WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
* General Public License for more details.
19+
*
20+
* BSD LICENSE
21+
*
22+
* Copyright(c) 2014 Intel Corporation.
23+
*
24+
* Redistribution and use in source and binary forms, with or without
25+
* modification, are permitted provided that the following conditions
26+
* are met:
27+
*
28+
* * Redistributions of source code must retain the above copyright
29+
* notice, this list of conditions and the following disclaimer.
30+
* * Redistributions in binary form must reproduce the above copyright
31+
* notice, this list of conditions and the following disclaimer in
32+
* the documentation and/or other materials provided with the
33+
* distribution.
34+
* * Neither the name of Intel Corporation nor the names of its
35+
* contributors may be used to endorse or promote products derived
36+
* from this software without specific prior written permission.
37+
*
38+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49+
*
50+
* Intel SCIF driver.
51+
*/
52+
#ifndef SCIF_RB_H
53+
#define SCIF_RB_H
54+
/*
55+
* This file describes a general purpose, byte based ring buffer. Writers to the
56+
* ring buffer need to synchronize using a lock. The same is true for readers,
57+
* although in practice, the ring buffer has a single reader. It is lockless
58+
* between producer and consumer so it can handle being used across the PCIe
59+
* bus. The ring buffer ensures that there are no reads across the PCIe bus for
60+
* performance reasons. Two of these are used to form a single bidirectional
61+
* queue-pair across PCIe.
62+
*/
63+
/*
64+
* struct scif_rb - SCIF Ring Buffer
65+
*
66+
* @rb_base: The base of the memory used for storing RB messages
67+
* @read_ptr: Pointer to the read offset
68+
* @write_ptr: Pointer to the write offset
69+
* @size: Size of the memory in rb_base
70+
* @current_read_offset: Cached read offset for performance
71+
* @current_write_offset: Cached write offset for performance
72+
*/
73+
struct scif_rb {
74+
void *rb_base;
75+
u32 *read_ptr;
76+
u32 *write_ptr;
77+
u32 size;
78+
u32 current_read_offset;
79+
u32 current_write_offset;
80+
};
81+
82+
/* methods used by both */
83+
void scif_rb_init(struct scif_rb *rb, u32 *read_ptr, u32 *write_ptr,
84+
void *rb_base, u8 size);
85+
/* writer only methods */
86+
/* write a new command, then scif_rb_commit() */
87+
int scif_rb_write(struct scif_rb *rb, void *msg, u32 size);
88+
/* after write(), then scif_rb_commit() */
89+
void scif_rb_commit(struct scif_rb *rb);
90+
/* query space available for writing to a RB. */
91+
u32 scif_rb_space(struct scif_rb *rb);
92+
93+
/* reader only methods */
94+
/* read a new message from the ring buffer of size bytes */
95+
u32 scif_rb_get_next(struct scif_rb *rb, void *msg, u32 size);
96+
/* update the read pointer so that the space can be reused */
97+
void scif_rb_update_read_ptr(struct scif_rb *rb);
98+
/* count the number of bytes that can be read */
99+
u32 scif_rb_count(struct scif_rb *rb, u32 size);
100+
#endif

0 commit comments

Comments
 (0)