summaryrefstree
path: root/kernel/net/neuron/channel/ch_mq_shmem_common.h
blob: 7b9b90f2f5903c14a8d784d7acfc61d471bfed2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* SPDX-License-Identifier: GPL-2.0-only */
/* Copyright (c) 2020 The Linux Foundation. All rights reserved. */

/* Neuron Message Queue transport layer header file */
#ifndef __NEURON_MQ_H

#include <linux/sched.h>
#include <linux/wait.h>

/* Current version number */
#define NEURON_SHMEM_CHANNEL_V1 0xcafe0001

/* Shared Memory header definition.
 * It locates on the beginning of the shared memory.
 */
struct neuron_shmem_channel_header {
	/* channel header version */
	u32 version;
	/* field for keeping head value, which is an offset value
	 * against the ring buffer
	 */
	u32 head;
	/* tail offset */
	u32 tail_offset;
	/* field for the expecting length of the next message.*/
	u32 space_for_next;
	/* Maximum message size */
	u32 max_msg_size;
	/* ring buffer offset */
	u32 ring_buffer_offset;
	/* ring buffer lenghth */
	u32 ring_buffer_len;
	/* message alignment bytes */
	u16 message_alignment;
	/* notification flags. Currently unused. */
	u16 notification_flags;
} __packed;

/* Message header. The message payload is somewhere aligned with
 * message_alignment field after this header.
 */
struct neuron_msg_hdr {
	u32 size; // size
} __packed;

/* Packet header size*/
#define PACKET_HEADER_SIZE sizeof(struct neuron_msg_hdr)

/* Message Queue definition */
struct neuron_msg_queue {
	/* Point to field head in shared memory header */
	u32 *headp;
	/* Point to tail offset field in shared memory header */
	u32 *tailp;
	/* Point to space_for_next field in shared memory header */
	u32 *space_for_next_p;
	/* ring buffer address */
	void *ring_buffer_p;
	/* ring buffer length */
	u32 ring_buffer_len;
	/* message alignment bytes */
	u16 message_alignment;
	/* current local offset. It is the head offset for the sender
	 * and the tail offset for the receiver.
	 */
	u32 offset;
};

/* A struct for driver private data */
struct neuron_mq_data_priv {
	/* outgoing vIRQ */
	u32 virq_line;
	/* incoming vIRQ */
	u32 virq;
	/* unused at the moment */
	atomic64_t virq_payload;
	/* A counter to calculate the interrupt received. */
	u32 interrupt_counter;
	/* name of peer vm */
	u32 peer_name;
	/* label to get haven resources like doorbell and shm */
	u32 haven_label;
	/* haven tx doorbell descriptor */
	void *tx_dbl;
	/* haven rx doorbell descriptor */
	void *rx_dbl;
	/* memparcel handle after assigning label to shared memory */
	u32 shm_memparcel;
	/* haven rm status notifier block */
	struct notifier_block rm_nb;
	/* pointer to the device structure */
	struct neuron_channel *dev;
	/* shared memory mapped address */
	void *base;
	/* shared memory resource */
	struct resource buffer;
	/* Flag to show whether the channel is synced. */
	int synced;
	/* The pointer of thread doing the sync work. */
	struct task_struct *sync_thread;
	/* Waiting queue for sync thread */
	wait_queue_head_t wait_q;
	/* Message Queue */
	struct neuron_msg_queue msgq;
};

/* Setting limits for each side. */
static inline int channel_set_limits(struct neuron_channel *channel,
				     struct neuron_msg_queue *msgq)
{
	/* Default limit is one message filling the whole buffer */
	if (!channel->max_size && !channel->queue_length)
		channel->queue_length = 1;

	/* Calculate max size based on given queue length */
	if (!channel->max_size)
		channel->max_size =
			round_down((msgq->ring_buffer_len - 1) /
				   channel->queue_length,
				   msgq->message_alignment) -
				   round_up(PACKET_HEADER_SIZE,
					    msgq->message_alignment);

	/* Calculate queue length based on given max size */
	if (!channel->queue_length)
		channel->queue_length = (msgq->ring_buffer_len - 1) /
			(round_up(PACKET_HEADER_SIZE,
				  msgq->message_alignment) +
			 round_up(channel->max_size,
				  msgq->message_alignment));

	/* Assert that max_size and queue_length are sane. */
	if ((size_t)msgq->ring_buffer_len - 1 <
		channel->queue_length *
			(size_t)(round_up(channel->max_size,
				 msgq->message_alignment) +
				 round_up(PACKET_HEADER_SIZE,
					  msgq->message_alignment))) {
		dev_err(&channel->dev,
			"ring buf size %zu too small for %u * %zu messages\n",
			(size_t)msgq->ring_buffer_len,
			(unsigned int)channel->queue_length,
			channel->max_size);
		return -EINVAL;
	}

	return 0;
}

#endif