aboutsummaryrefslogtreecommitdiff
path: root/core/opal-msg.c
blob: af1ec7d000d52cb5ed09f0bfd770ed3e1422f2fb (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Copyright 2013-2019 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define pr_fmt(fmt) "opalmsg: " fmt
#include <skiboot.h>
#include <opal-msg.h>
#include <opal-api.h>
#include <lock.h>

#define OPAL_MAX_MSGS		(OPAL_MSG_TYPE_MAX + OPAL_MAX_ASYNC_COMP - 1)

struct opal_msg_entry {
	struct list_node link;
	void (*consumed)(void *data, int status);
	bool extended;
	void *data;
	struct opal_msg msg;
};

static LIST_HEAD(msg_free_list);
static LIST_HEAD(msg_pending_list);

static struct lock opal_msg_lock = LOCK_UNLOCKED;

int _opal_queue_msg(enum opal_msg_type msg_type, void *data,
		    void (*consumed)(void *data, int status),
		    size_t params_size, const void *params)
{
	struct opal_msg_entry *entry;
	uint64_t entry_size;

	if ((params_size + OPAL_MSG_HDR_SIZE) > OPAL_MSG_SIZE) {
		prlog(PR_DEBUG, "param_size (0x%x) > opal_msg param size (0x%x)\n",
		      (u32)params_size, (u32)(OPAL_MSG_SIZE - OPAL_MSG_HDR_SIZE));
		return OPAL_PARAMETER;
	}

	lock(&opal_msg_lock);

	if (params_size > OPAL_MSG_FIXED_PARAMS_SIZE) {
		entry_size = sizeof(struct opal_msg_entry) + params_size;
		entry_size -= OPAL_MSG_FIXED_PARAMS_SIZE;
		entry = zalloc(entry_size);
		if (entry)
			entry->extended = true;
	} else {
		entry = list_pop(&msg_free_list, struct opal_msg_entry, link);
		if (!entry) {
			prerror("No available node in the free list, allocating\n");
			entry = zalloc(sizeof(struct opal_msg_entry));
		}
	}
	if (!entry) {
		prerror("Allocation failed\n");
		unlock(&opal_msg_lock);
		return OPAL_RESOURCE;
	}

	entry->consumed = consumed;
	entry->data = data;
	entry->msg.msg_type = cpu_to_be32(msg_type);
	entry->msg.size = cpu_to_be32(params_size);
	memcpy(entry->msg.params, params, params_size);

	list_add_tail(&msg_pending_list, &entry->link);
	opal_update_pending_evt(OPAL_EVENT_MSG_PENDING,
				OPAL_EVENT_MSG_PENDING);
	unlock(&opal_msg_lock);

	return OPAL_SUCCESS;
}

static int64_t opal_get_msg(uint64_t *buffer, uint64_t size)
{
	struct opal_msg_entry *entry;
	void (*callback)(void *data, int status);
	void *data;
	uint64_t msg_size;
	int rc = OPAL_SUCCESS;

	if (size < sizeof(struct opal_msg) || !buffer)
		return OPAL_PARAMETER;

	if (!opal_addr_valid(buffer))
		return OPAL_PARAMETER;

	lock(&opal_msg_lock);

	entry = list_pop(&msg_pending_list, struct opal_msg_entry, link);
	if (!entry) {
		unlock(&opal_msg_lock);
		return OPAL_RESOURCE;
	}

	msg_size = OPAL_MSG_HDR_SIZE + be32_to_cpu(entry->msg.size);
	if (size < msg_size) {
		/* Send partial data to Linux */
		prlog(PR_NOTICE, "Sending partial data [msg_type : 0x%x, "
		      "msg_size : 0x%x, buf_size : 0x%x]\n",
		      be32_to_cpu(entry->msg.msg_type),
		      (u32)msg_size, (u32)size);

		entry->msg.size = cpu_to_be32(size - OPAL_MSG_HDR_SIZE);
		msg_size = size;
		rc = OPAL_PARTIAL;
	}

	memcpy((void *)buffer, (void *)&entry->msg, msg_size);
	callback = entry->consumed;
	data = entry->data;

	if (entry->extended)
		free(entry);
	else
		list_add(&msg_free_list, &entry->link);

	if (list_empty(&msg_pending_list))
		opal_update_pending_evt(OPAL_EVENT_MSG_PENDING, 0);

	unlock(&opal_msg_lock);

	if (callback)
		callback(data, rc);

	return rc;
}
opal_call(OPAL_GET_MSG, opal_get_msg, 2);

static int64_t opal_check_completion(uint64_t *buffer, uint64_t size,
				     uint64_t token)
{
	struct opal_msg_entry *entry, *next_entry;
	void (*callback)(void *data, int status) = NULL;
	int rc = OPAL_BUSY;
	void *data = NULL;

	if (!opal_addr_valid(buffer))
		return OPAL_PARAMETER;

	lock(&opal_msg_lock);
	list_for_each_safe(&msg_pending_list, entry, next_entry, link) {
		if (entry->msg.msg_type == OPAL_MSG_ASYNC_COMP &&
		    be64_to_cpu(entry->msg.params[0]) == token) {
			list_del(&entry->link);
			callback = entry->consumed;
			data = entry->data;
			list_add(&msg_free_list, &entry->link);
			if (list_empty(&msg_pending_list))
				opal_update_pending_evt(OPAL_EVENT_MSG_PENDING,
							0);
			rc = OPAL_SUCCESS;
			break;
		}
	}

	if (rc == OPAL_SUCCESS && size >= sizeof(struct opal_msg))
		memcpy(buffer, &entry->msg, sizeof(entry->msg));

	unlock(&opal_msg_lock);

	if (callback)
		callback(data, OPAL_SUCCESS);

	return rc;

}
opal_call(OPAL_CHECK_ASYNC_COMPLETION, opal_check_completion, 3);

void opal_init_msg(void)
{
	struct opal_msg_entry *entry;
	int i;

	for (i = 0; i < OPAL_MAX_MSGS; i++, entry++) {
                entry = zalloc(sizeof(*entry));
                if (!entry)
                        goto err;
		list_add_tail(&msg_free_list, &entry->link);
        }
        return;

err:
        for (; i > 0; i--) {
                entry = list_pop(&msg_free_list, struct opal_msg_entry, link);
                if (entry)
                        free(entry);
        }
}