1 /* visorchannel_funcs.c
3 * Copyright (C) 2010 - 2015 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
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, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
18 * This provides s-Par channel communication primitives, which are
19 * independent of the mechanism used to access the channel data.
22 #include <linux/uuid.h>
26 #include "controlvmchannel.h"
28 #define MYDRVNAME "visorchannel"
30 #define SPAR_CONSOLEVIDEO_CHANNEL_PROTOCOL_GUID \
31 UUID_LE(0x3cd6e705, 0xd6a2, 0x4aa5, \
32 0xad, 0x5c, 0x7b, 0x8, 0x88, 0x9d, 0xff, 0xe2)
33 static const uuid_le spar_video_guid = SPAR_CONSOLEVIDEO_CHANNEL_PROTOCOL_GUID;
40 struct channel_header chan_hdr;
42 bool needs_lock; /* channel creator knows if more than one */
43 /* thread will be inserting or removing */
44 spinlock_t insert_lock; /* protect head writes in chan_hdr */
45 spinlock_t remove_lock; /* protect tail writes in chan_hdr */
48 struct signal_queue_header req_queue;
49 struct signal_queue_header rsp_queue;
50 struct signal_queue_header event_queue;
51 struct signal_queue_header ack_queue;
58 visorchannel_destroy(struct visorchannel *channel)
62 if (channel->mapped) {
63 memunmap(channel->mapped);
64 if (channel->requested)
65 release_mem_region(channel->physaddr, channel->nbytes);
71 visorchannel_get_physaddr(struct visorchannel *channel)
73 return channel->physaddr;
77 visorchannel_get_nbytes(struct visorchannel *channel)
79 return channel->nbytes;
83 visorchannel_uuid_id(uuid_le *guid, char *s)
85 sprintf(s, "%pUL", guid);
90 visorchannel_id(struct visorchannel *channel, char *s)
92 return visorchannel_uuid_id(&channel->guid, s);
96 visorchannel_zoneid(struct visorchannel *channel, char *s)
98 return visorchannel_uuid_id(&channel->chan_hdr.zone_uuid, s);
102 visorchannel_get_clientpartition(struct visorchannel *channel)
104 return channel->chan_hdr.partition_handle;
108 visorchannel_set_clientpartition(struct visorchannel *channel,
109 u64 partition_handle)
111 channel->chan_hdr.partition_handle = partition_handle;
116 * visorchannel_get_uuid() - queries the UUID of the designated channel
117 * @channel: the channel to query
119 * Return: the UUID of the provided channel
122 visorchannel_get_uuid(struct visorchannel *channel)
124 return channel->guid;
126 EXPORT_SYMBOL_GPL(visorchannel_get_uuid);
129 visorchannel_read(struct visorchannel *channel, ulong offset,
130 void *local, ulong nbytes)
132 if (offset + nbytes > channel->nbytes)
135 memcpy(local, channel->mapped + offset, nbytes);
141 visorchannel_write(struct visorchannel *channel, ulong offset,
142 void *local, ulong nbytes)
144 size_t chdr_size = sizeof(struct channel_header);
147 if (offset + nbytes > channel->nbytes)
150 if (offset < chdr_size) {
151 copy_size = min(chdr_size - offset, nbytes);
152 memcpy(((char *)(&channel->chan_hdr)) + offset,
156 memcpy(channel->mapped + offset, local, nbytes);
162 visorchannel_get_header(struct visorchannel *channel)
164 return (void __iomem *)&channel->chan_hdr;
168 * Return offset of a specific SIGNAL_QUEUE_HEADER from the beginning of a
171 #define SIG_QUEUE_OFFSET(chan_hdr, q) \
172 ((chan_hdr)->ch_space_offset + \
173 ((q) * sizeof(struct signal_queue_header)))
176 * Return offset of a specific queue entry (data) from the beginning of a
179 #define SIG_DATA_OFFSET(chan_hdr, q, sig_hdr, slot) \
180 (SIG_QUEUE_OFFSET(chan_hdr, q) + (sig_hdr)->sig_base_offset + \
181 ((slot) * (sig_hdr)->signal_size))
184 * Write the contents of a specific field within a SIGNAL_QUEUE_HEADER back
187 #define SIG_WRITE_FIELD(channel, queue, sig_hdr, FIELD) \
188 visorchannel_write(channel, \
189 SIG_QUEUE_OFFSET(&channel->chan_hdr, queue) +\
190 offsetof(struct signal_queue_header, FIELD), \
191 &((sig_hdr)->FIELD), \
192 sizeof((sig_hdr)->FIELD))
195 sig_read_header(struct visorchannel *channel, u32 queue,
196 struct signal_queue_header *sig_hdr)
198 if (channel->chan_hdr.ch_space_offset < sizeof(struct channel_header))
201 /* Read the appropriate SIGNAL_QUEUE_HEADER into local memory. */
202 return visorchannel_read(channel,
203 SIG_QUEUE_OFFSET(&channel->chan_hdr, queue),
204 sig_hdr, sizeof(struct signal_queue_header));
208 sig_read_data(struct visorchannel *channel, u32 queue,
209 struct signal_queue_header *sig_hdr, u32 slot, void *data)
211 int signal_data_offset = SIG_DATA_OFFSET(&channel->chan_hdr, queue,
214 return visorchannel_read(channel, signal_data_offset,
215 data, sig_hdr->signal_size);
219 sig_write_data(struct visorchannel *channel, u32 queue,
220 struct signal_queue_header *sig_hdr, u32 slot, void *data)
222 int signal_data_offset = SIG_DATA_OFFSET(&channel->chan_hdr, queue,
225 return visorchannel_write(channel, signal_data_offset,
226 data, sig_hdr->signal_size);
230 signalremove_inner(struct visorchannel *channel, u32 queue, void *msg)
232 struct signal_queue_header sig_hdr;
235 error = sig_read_header(channel, queue, &sig_hdr);
239 if (sig_hdr.head == sig_hdr.tail)
240 return -EIO; /* no signals to remove */
242 sig_hdr.tail = (sig_hdr.tail + 1) % sig_hdr.max_slots;
244 error = sig_read_data(channel, queue, &sig_hdr, sig_hdr.tail, msg);
248 sig_hdr.num_received++;
251 * For each data field in SIGNAL_QUEUE_HEADER that was modified,
252 * update host memory.
254 mb(); /* required for channel synch */
256 error = SIG_WRITE_FIELD(channel, queue, &sig_hdr, tail);
259 error = SIG_WRITE_FIELD(channel, queue, &sig_hdr, num_received);
267 * visorchannel_signalremove() - removes a message from the designated
269 * @channel: the channel the message will be removed from
270 * @queue: the queue the message will be removed from
271 * @msg: the message to remove
273 * Return: integer error code indicating the status of the removal
276 visorchannel_signalremove(struct visorchannel *channel, u32 queue, void *msg)
281 if (channel->needs_lock) {
282 spin_lock_irqsave(&channel->remove_lock, flags);
283 rc = signalremove_inner(channel, queue, msg);
284 spin_unlock_irqrestore(&channel->remove_lock, flags);
286 rc = signalremove_inner(channel, queue, msg);
291 EXPORT_SYMBOL_GPL(visorchannel_signalremove);
294 * visorchannel_signalempty() - checks if the designated channel/queue
295 * contains any messages
296 * @channel: the channel to query
297 * @queue: the queue in the channel to query
299 * Return: boolean indicating whether any messages in the designated
300 * channel/queue are present
303 visorchannel_signalempty(struct visorchannel *channel, u32 queue)
305 unsigned long flags = 0;
306 struct signal_queue_header sig_hdr;
309 if (channel->needs_lock)
310 spin_lock_irqsave(&channel->remove_lock, flags);
312 if (sig_read_header(channel, queue, &sig_hdr))
314 if (sig_hdr.head == sig_hdr.tail)
316 if (channel->needs_lock)
317 spin_unlock_irqrestore(&channel->remove_lock, flags);
321 EXPORT_SYMBOL_GPL(visorchannel_signalempty);
324 signalinsert_inner(struct visorchannel *channel, u32 queue, void *msg)
326 struct signal_queue_header sig_hdr;
329 error = sig_read_header(channel, queue, &sig_hdr);
333 sig_hdr.head = (sig_hdr.head + 1) % sig_hdr.max_slots;
334 if (sig_hdr.head == sig_hdr.tail) {
335 sig_hdr.num_overflows++;
336 visorchannel_write(channel,
337 SIG_QUEUE_OFFSET(&channel->chan_hdr, queue) +
338 offsetof(struct signal_queue_header,
340 &sig_hdr.num_overflows,
341 sizeof(sig_hdr.num_overflows));
345 error = sig_write_data(channel, queue, &sig_hdr, sig_hdr.head, msg);
352 * For each data field in SIGNAL_QUEUE_HEADER that was modified,
353 * update host memory.
355 mb(); /* required for channel synch */
357 error = SIG_WRITE_FIELD(channel, queue, &sig_hdr, head);
360 error = SIG_WRITE_FIELD(channel, queue, &sig_hdr, num_sent);
368 * visorchannel_create_guts() - creates the struct visorchannel abstraction
369 * for a data area in memory, but does NOT modify
371 * @physaddr: physical address of start of channel
372 * @channel_bytes: size of the channel in bytes; this may 0 if the channel has
373 * already been initialized in memory (which is true for all
374 * channels provided to guest environments by the s-Par
375 * back-end), in which case the actual channel size will be
376 * read from the channel header in memory
377 * @gfp: gfp_t to use when allocating memory for the data struct
378 * @guid: uuid that identifies channel type; this may 0 if the channel
379 * has already been initialized in memory (which is true for all
380 * channels provided to guest environments by the s-Par
381 * back-end), in which case the actual channel guid will be
382 * read from the channel header in memory
383 * @needs_lock: must specify true if you have multiple threads of execution
384 * that will be calling visorchannel methods of this
385 * visorchannel at the same time
387 * Return: pointer to visorchannel that was created if successful,
390 static struct visorchannel *
391 visorchannel_create_guts(u64 physaddr, unsigned long channel_bytes,
392 gfp_t gfp, uuid_le guid, bool needs_lock)
394 struct visorchannel *channel;
396 size_t size = sizeof(struct channel_header);
401 channel = kzalloc(sizeof(*channel), gfp);
405 channel->needs_lock = needs_lock;
406 spin_lock_init(&channel->insert_lock);
407 spin_lock_init(&channel->remove_lock);
410 * Video driver constains the efi framebuffer so it will get a
411 * conflict resource when requesting its full mem region. Since
412 * we are only using the efi framebuffer for video we can ignore
413 * this. Remember that we haven't requested it so we don't try to
416 channel->requested = request_mem_region(physaddr, size, MYDRVNAME);
417 if (!channel->requested) {
418 if (uuid_le_cmp(guid, spar_video_guid)) {
419 /* Not the video channel we care about this */
420 goto err_destroy_channel;
424 channel->mapped = memremap(physaddr, size, MEMREMAP_WB);
425 if (!channel->mapped) {
426 release_mem_region(physaddr, size);
427 goto err_destroy_channel;
430 channel->physaddr = physaddr;
431 channel->nbytes = size;
433 err = visorchannel_read(channel, 0, &channel->chan_hdr,
434 sizeof(struct channel_header));
436 goto err_destroy_channel;
438 /* we had better be a CLIENT of this channel */
439 if (channel_bytes == 0)
440 channel_bytes = (ulong)channel->chan_hdr.size;
441 if (uuid_le_cmp(guid, NULL_UUID_LE) == 0)
442 guid = channel->chan_hdr.chtype;
444 memunmap(channel->mapped);
445 if (channel->requested)
446 release_mem_region(channel->physaddr, channel->nbytes);
447 channel->mapped = NULL;
448 channel->requested = request_mem_region(channel->physaddr,
449 channel_bytes, MYDRVNAME);
450 if (!channel->requested) {
451 if (uuid_le_cmp(guid, spar_video_guid)) {
452 /* Different we care about this */
453 goto err_destroy_channel;
457 channel->mapped = memremap(channel->physaddr, channel_bytes,
459 if (!channel->mapped) {
460 release_mem_region(channel->physaddr, channel_bytes);
461 goto err_destroy_channel;
464 channel->nbytes = channel_bytes;
465 channel->guid = guid;
469 visorchannel_destroy(channel);
473 struct visorchannel *
474 visorchannel_create(u64 physaddr, unsigned long channel_bytes,
475 gfp_t gfp, uuid_le guid)
477 return visorchannel_create_guts(physaddr, channel_bytes, gfp, guid,
481 struct visorchannel *
482 visorchannel_create_with_lock(u64 physaddr, unsigned long channel_bytes,
483 gfp_t gfp, uuid_le guid)
485 return visorchannel_create_guts(physaddr, channel_bytes, gfp, guid,
490 * visorchannel_signalinsert() - inserts a message into the designated
492 * @channel: the channel the message will be added to
493 * @queue: the queue the message will be added to
494 * @msg: the message to insert
496 * Return: integer error code indicating the status of the insertion
499 visorchannel_signalinsert(struct visorchannel *channel, u32 queue, void *msg)
504 if (channel->needs_lock) {
505 spin_lock_irqsave(&channel->insert_lock, flags);
506 rc = signalinsert_inner(channel, queue, msg);
507 spin_unlock_irqrestore(&channel->insert_lock, flags);
509 rc = signalinsert_inner(channel, queue, msg);
514 EXPORT_SYMBOL_GPL(visorchannel_signalinsert);