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 Supervisor channel communication primitives, which are
19 * independent of the mechanism used to access the channel data.
22 #include <linux/uuid.h>
27 #include "controlvmchannel.h"
29 #define MYDRVNAME "visorchannel"
31 #define SPAR_CONSOLEVIDEO_CHANNEL_PROTOCOL_GUID \
32 UUID_LE(0x3cd6e705, 0xd6a2, 0x4aa5, \
33 0xad, 0x5c, 0x7b, 0x8, 0x88, 0x9d, 0xff, 0xe2)
34 static const uuid_le spar_video_guid = SPAR_CONSOLEVIDEO_CHANNEL_PROTOCOL_GUID;
41 struct channel_header chan_hdr;
44 bool needs_lock; /* channel creator knows if more than one
45 * thread will be inserting or removing */
46 spinlock_t insert_lock; /* protect head writes in chan_hdr */
47 spinlock_t remove_lock; /* protect tail writes in chan_hdr */
50 struct signal_queue_header req_queue;
51 struct signal_queue_header rsp_queue;
52 struct signal_queue_header event_queue;
53 struct signal_queue_header ack_queue;
59 /* Creates the struct visorchannel abstraction for a data area in memory,
60 * but does NOT modify this data area.
62 static struct visorchannel *
63 visorchannel_create_guts(u64 physaddr, unsigned long channel_bytes,
64 gfp_t gfp, unsigned long off,
65 uuid_le guid, bool needs_lock)
67 struct visorchannel *channel;
69 size_t size = sizeof(struct channel_header);
74 channel = kzalloc(sizeof(*channel), gfp);
78 channel->needs_lock = needs_lock;
79 spin_lock_init(&channel->insert_lock);
80 spin_lock_init(&channel->remove_lock);
82 /* Video driver constains the efi framebuffer so it will get a
83 * conflict resource when requesting its full mem region. Since
84 * we are only using the efi framebuffer for video we can ignore
85 * this. Remember that we haven't requested it so we don't try to
88 channel->requested = request_mem_region(physaddr, size, MYDRVNAME);
89 if (!channel->requested) {
90 if (uuid_le_cmp(guid, spar_video_guid)) {
91 /* Not the video channel we care about this */
96 channel->mapped = memremap(physaddr, size, MEMREMAP_WB);
97 if (!channel->mapped) {
98 release_mem_region(physaddr, size);
102 channel->physaddr = physaddr;
103 channel->nbytes = size;
105 err = visorchannel_read(channel, 0, &channel->chan_hdr,
106 sizeof(struct channel_header));
110 /* we had better be a CLIENT of this channel */
111 if (channel_bytes == 0)
112 channel_bytes = (ulong)channel->chan_hdr.size;
113 if (uuid_le_cmp(guid, NULL_UUID_LE) == 0)
114 guid = channel->chan_hdr.chtype;
116 memunmap(channel->mapped);
117 if (channel->requested)
118 release_mem_region(channel->physaddr, channel->nbytes);
119 channel->mapped = NULL;
120 channel->requested = request_mem_region(channel->physaddr,
121 channel_bytes, MYDRVNAME);
122 if (!channel->requested) {
123 if (uuid_le_cmp(guid, spar_video_guid)) {
124 /* Different we care about this */
129 channel->mapped = memremap(channel->physaddr, channel_bytes,
131 if (!channel->mapped) {
132 release_mem_region(channel->physaddr, channel_bytes);
136 channel->nbytes = channel_bytes;
138 channel->size = channel_bytes;
139 channel->guid = guid;
143 visorchannel_destroy(channel);
147 struct visorchannel *
148 visorchannel_create(u64 physaddr, unsigned long channel_bytes,
149 gfp_t gfp, uuid_le guid)
151 return visorchannel_create_guts(physaddr, channel_bytes, gfp, 0, guid,
154 EXPORT_SYMBOL_GPL(visorchannel_create);
156 struct visorchannel *
157 visorchannel_create_with_lock(u64 physaddr, unsigned long channel_bytes,
158 gfp_t gfp, uuid_le guid)
160 return visorchannel_create_guts(physaddr, channel_bytes, gfp, 0, guid,
163 EXPORT_SYMBOL_GPL(visorchannel_create_with_lock);
166 visorchannel_destroy(struct visorchannel *channel)
170 if (channel->mapped) {
171 memunmap(channel->mapped);
172 if (channel->requested)
173 release_mem_region(channel->physaddr, channel->nbytes);
177 EXPORT_SYMBOL_GPL(visorchannel_destroy);
180 visorchannel_get_physaddr(struct visorchannel *channel)
182 return channel->physaddr;
184 EXPORT_SYMBOL_GPL(visorchannel_get_physaddr);
187 visorchannel_get_nbytes(struct visorchannel *channel)
189 return channel->size;
191 EXPORT_SYMBOL_GPL(visorchannel_get_nbytes);
194 visorchannel_uuid_id(uuid_le *guid, char *s)
196 sprintf(s, "%pUL", guid);
199 EXPORT_SYMBOL_GPL(visorchannel_uuid_id);
202 visorchannel_id(struct visorchannel *channel, char *s)
204 return visorchannel_uuid_id(&channel->guid, s);
206 EXPORT_SYMBOL_GPL(visorchannel_id);
209 visorchannel_zoneid(struct visorchannel *channel, char *s)
211 return visorchannel_uuid_id(&channel->chan_hdr.zone_uuid, s);
213 EXPORT_SYMBOL_GPL(visorchannel_zoneid);
216 visorchannel_get_clientpartition(struct visorchannel *channel)
218 return channel->chan_hdr.partition_handle;
220 EXPORT_SYMBOL_GPL(visorchannel_get_clientpartition);
223 visorchannel_set_clientpartition(struct visorchannel *channel,
224 u64 partition_handle)
226 channel->chan_hdr.partition_handle = partition_handle;
229 EXPORT_SYMBOL_GPL(visorchannel_set_clientpartition);
232 visorchannel_get_uuid(struct visorchannel *channel)
234 return channel->guid;
236 EXPORT_SYMBOL_GPL(visorchannel_get_uuid);
239 visorchannel_read(struct visorchannel *channel, ulong offset,
240 void *local, ulong nbytes)
242 if (offset + nbytes > channel->nbytes)
245 memcpy(local, channel->mapped + offset, nbytes);
249 EXPORT_SYMBOL_GPL(visorchannel_read);
252 visorchannel_write(struct visorchannel *channel, ulong offset,
253 void *local, ulong nbytes)
255 size_t chdr_size = sizeof(struct channel_header);
258 if (offset + nbytes > channel->nbytes)
261 if (offset < chdr_size) {
262 copy_size = min(chdr_size - offset, nbytes);
263 memcpy(((char *)(&channel->chan_hdr)) + offset,
267 memcpy(channel->mapped + offset, local, nbytes);
271 EXPORT_SYMBOL_GPL(visorchannel_write);
274 visorchannel_clear(struct visorchannel *channel, ulong offset, u8 ch,
278 int bufsize = PAGE_SIZE;
282 buf = (u8 *)__get_free_page(GFP_KERNEL);
286 memset(buf, ch, bufsize);
289 int thisbytes = bufsize;
291 if (nbytes < thisbytes)
293 err = visorchannel_write(channel, offset + written,
298 written += thisbytes;
304 free_page((unsigned long)buf);
307 EXPORT_SYMBOL_GPL(visorchannel_clear);
310 visorchannel_get_header(struct visorchannel *channel)
312 return (void __iomem *)&channel->chan_hdr;
314 EXPORT_SYMBOL_GPL(visorchannel_get_header);
316 /** Return offset of a specific SIGNAL_QUEUE_HEADER from the beginning of a
319 #define SIG_QUEUE_OFFSET(chan_hdr, q) \
320 ((chan_hdr)->ch_space_offset + \
321 ((q) * sizeof(struct signal_queue_header)))
323 /** Return offset of a specific queue entry (data) from the beginning of a
326 #define SIG_DATA_OFFSET(chan_hdr, q, sig_hdr, slot) \
327 (SIG_QUEUE_OFFSET(chan_hdr, q) + (sig_hdr)->sig_base_offset + \
328 ((slot) * (sig_hdr)->signal_size))
330 /** Write the contents of a specific field within a SIGNAL_QUEUE_HEADER back
333 #define SIG_WRITE_FIELD(channel, queue, sig_hdr, FIELD) \
334 (visorchannel_write(channel, \
335 SIG_QUEUE_OFFSET(&channel->chan_hdr, queue) +\
336 offsetof(struct signal_queue_header, FIELD), \
337 &((sig_hdr)->FIELD), \
338 sizeof((sig_hdr)->FIELD)) >= 0)
341 sig_read_header(struct visorchannel *channel, u32 queue,
342 struct signal_queue_header *sig_hdr)
346 if (channel->chan_hdr.ch_space_offset < sizeof(struct channel_header))
349 /* Read the appropriate SIGNAL_QUEUE_HEADER into local memory. */
350 err = visorchannel_read(channel,
351 SIG_QUEUE_OFFSET(&channel->chan_hdr, queue),
352 sig_hdr, sizeof(struct signal_queue_header));
360 sig_read_data(struct visorchannel *channel, u32 queue,
361 struct signal_queue_header *sig_hdr, u32 slot, void *data)
364 int signal_data_offset = SIG_DATA_OFFSET(&channel->chan_hdr, queue,
367 err = visorchannel_read(channel, signal_data_offset,
368 data, sig_hdr->signal_size);
376 sig_write_data(struct visorchannel *channel, u32 queue,
377 struct signal_queue_header *sig_hdr, u32 slot, void *data)
380 int signal_data_offset = SIG_DATA_OFFSET(&channel->chan_hdr, queue,
383 err = visorchannel_write(channel, signal_data_offset,
384 data, sig_hdr->signal_size);
392 signalremove_inner(struct visorchannel *channel, u32 queue, void *msg)
394 struct signal_queue_header sig_hdr;
396 if (!sig_read_header(channel, queue, &sig_hdr))
398 if (sig_hdr.head == sig_hdr.tail)
399 return false; /* no signals to remove */
401 sig_hdr.tail = (sig_hdr.tail + 1) % sig_hdr.max_slots;
402 if (!sig_read_data(channel, queue, &sig_hdr, sig_hdr.tail, msg))
404 sig_hdr.num_received++;
406 /* For each data field in SIGNAL_QUEUE_HEADER that was modified,
407 * update host memory.
409 mb(); /* required for channel synch */
410 if (!SIG_WRITE_FIELD(channel, queue, &sig_hdr, tail))
412 if (!SIG_WRITE_FIELD(channel, queue, &sig_hdr, num_received))
418 visorchannel_signalremove(struct visorchannel *channel, u32 queue, void *msg)
423 if (channel->needs_lock) {
424 spin_lock_irqsave(&channel->remove_lock, flags);
425 rc = signalremove_inner(channel, queue, msg);
426 spin_unlock_irqrestore(&channel->remove_lock, flags);
428 rc = signalremove_inner(channel, queue, msg);
433 EXPORT_SYMBOL_GPL(visorchannel_signalremove);
436 visorchannel_signalempty(struct visorchannel *channel, u32 queue)
438 unsigned long flags = 0;
439 struct signal_queue_header sig_hdr;
442 if (channel->needs_lock)
443 spin_lock_irqsave(&channel->remove_lock, flags);
445 if (!sig_read_header(channel, queue, &sig_hdr))
447 if (sig_hdr.head == sig_hdr.tail)
449 if (channel->needs_lock)
450 spin_unlock_irqrestore(&channel->remove_lock, flags);
454 EXPORT_SYMBOL_GPL(visorchannel_signalempty);
457 signalinsert_inner(struct visorchannel *channel, u32 queue, void *msg)
459 struct signal_queue_header sig_hdr;
461 if (!sig_read_header(channel, queue, &sig_hdr))
464 sig_hdr.head = ((sig_hdr.head + 1) % sig_hdr.max_slots);
465 if (sig_hdr.head == sig_hdr.tail) {
466 sig_hdr.num_overflows++;
467 visorchannel_write(channel,
468 SIG_QUEUE_OFFSET(&channel->chan_hdr, queue) +
469 offsetof(struct signal_queue_header,
471 &sig_hdr.num_overflows,
472 sizeof(sig_hdr.num_overflows));
476 if (!sig_write_data(channel, queue, &sig_hdr, sig_hdr.head, msg))
481 /* For each data field in SIGNAL_QUEUE_HEADER that was modified,
482 * update host memory.
484 mb(); /* required for channel synch */
485 if (!SIG_WRITE_FIELD(channel, queue, &sig_hdr, head))
487 if (!SIG_WRITE_FIELD(channel, queue, &sig_hdr, num_sent))
494 visorchannel_signalinsert(struct visorchannel *channel, u32 queue, void *msg)
499 if (channel->needs_lock) {
500 spin_lock_irqsave(&channel->insert_lock, flags);
501 rc = signalinsert_inner(channel, queue, msg);
502 spin_unlock_irqrestore(&channel->insert_lock, flags);
504 rc = signalinsert_inner(channel, queue, msg);
509 EXPORT_SYMBOL_GPL(visorchannel_signalinsert);
512 visorchannel_signalqueue_slots_avail(struct visorchannel *channel, u32 queue)
514 struct signal_queue_header sig_hdr;
515 u32 slots_avail, slots_used;
518 if (!sig_read_header(channel, queue, &sig_hdr))
523 head = head + sig_hdr.max_slots;
524 slots_used = (head - tail);
525 slots_avail = sig_hdr.max_signals - slots_used;
526 return (int)slots_avail;
528 EXPORT_SYMBOL_GPL(visorchannel_signalqueue_slots_avail);
531 visorchannel_signalqueue_max_slots(struct visorchannel *channel, u32 queue)
533 struct signal_queue_header sig_hdr;
535 if (!sig_read_header(channel, queue, &sig_hdr))
537 return (int)sig_hdr.max_signals;
539 EXPORT_SYMBOL_GPL(visorchannel_signalqueue_max_slots);
542 sigqueue_debug(struct signal_queue_header *q, int which, struct seq_file *seq)
544 seq_printf(seq, "Signal Queue #%d\n", which);
545 seq_printf(seq, " VersionId = %lu\n", (ulong)q->version);
546 seq_printf(seq, " Type = %lu\n", (ulong)q->chtype);
547 seq_printf(seq, " oSignalBase = %llu\n",
548 (long long)q->sig_base_offset);
549 seq_printf(seq, " SignalSize = %lu\n", (ulong)q->signal_size);
550 seq_printf(seq, " MaxSignalSlots = %lu\n",
551 (ulong)q->max_slots);
552 seq_printf(seq, " MaxSignals = %lu\n", (ulong)q->max_signals);
553 seq_printf(seq, " FeatureFlags = %-16.16Lx\n",
554 (long long)q->features);
555 seq_printf(seq, " NumSignalsSent = %llu\n",
556 (long long)q->num_sent);
557 seq_printf(seq, " NumSignalsReceived = %llu\n",
558 (long long)q->num_received);
559 seq_printf(seq, " NumOverflows = %llu\n",
560 (long long)q->num_overflows);
561 seq_printf(seq, " Head = %lu\n", (ulong)q->head);
562 seq_printf(seq, " Tail = %lu\n", (ulong)q->tail);
566 visorchannel_debug(struct visorchannel *channel, int num_queues,
567 struct seq_file *seq, u32 off)
570 ulong nbytes = 0, nbytes_region = 0;
571 struct channel_header hdr;
572 struct channel_header *phdr = &hdr;
579 addr = visorchannel_get_physaddr(channel);
580 nbytes_region = visorchannel_get_nbytes(channel);
581 errcode = visorchannel_read(channel, off,
582 phdr, sizeof(struct channel_header));
585 "Read of channel header failed with errcode=%d)\n",
588 phdr = &channel->chan_hdr;
589 seq_puts(seq, "(following data may be stale)\n");
594 nbytes = (ulong)(phdr->size);
595 seq_printf(seq, "--- Begin channel @0x%-16.16Lx for 0x%lx bytes (region=0x%lx bytes) ---\n",
596 addr + off, nbytes, nbytes_region);
597 seq_printf(seq, "Type = %pUL\n", &phdr->chtype);
598 seq_printf(seq, "ZoneGuid = %pUL\n", &phdr->zone_uuid);
599 seq_printf(seq, "Signature = 0x%-16.16Lx\n",
600 (long long)phdr->signature);
601 seq_printf(seq, "LegacyState = %lu\n", (ulong)phdr->legacy_state);
602 seq_printf(seq, "SrvState = %lu\n", (ulong)phdr->srv_state);
603 seq_printf(seq, "CliStateBoot = %lu\n", (ulong)phdr->cli_state_boot);
604 seq_printf(seq, "CliStateOS = %lu\n", (ulong)phdr->cli_state_os);
605 seq_printf(seq, "HeaderSize = %lu\n", (ulong)phdr->header_size);
606 seq_printf(seq, "Size = %llu\n", (long long)phdr->size);
607 seq_printf(seq, "Features = 0x%-16.16llx\n",
608 (long long)phdr->features);
609 seq_printf(seq, "PartitionHandle = 0x%-16.16llx\n",
610 (long long)phdr->partition_handle);
611 seq_printf(seq, "Handle = 0x%-16.16llx\n",
612 (long long)phdr->handle);
613 seq_printf(seq, "VersionId = %lu\n", (ulong)phdr->version_id);
614 seq_printf(seq, "oChannelSpace = %llu\n",
615 (long long)phdr->ch_space_offset);
616 if ((phdr->ch_space_offset == 0) || (errcode < 0))
619 for (i = 0; i < num_queues; i++) {
620 struct signal_queue_header q;
622 errcode = visorchannel_read(channel,
624 phdr->ch_space_offset +
629 "failed to read signal queue #%d from channel @0x%-16.16Lx errcode=%d\n",
633 sigqueue_debug(&q, i, seq);
635 seq_printf(seq, "--- End channel @0x%-16.16Lx for 0x%lx bytes ---\n",
638 EXPORT_SYMBOL_GPL(visorchannel_debug);