1 /* visorchannel_funcs.c
3 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
19 * This provides Supervisor channel communication primitives, which are
20 * independent of the mechanism used to access the channel data.
23 #include <linux/uuid.h>
28 #include "controlvmchannel.h"
30 #define MYDRVNAME "visorchannel"
32 #define SPAR_CONSOLEVIDEO_CHANNEL_PROTOCOL_GUID \
33 UUID_LE(0x3cd6e705, 0xd6a2, 0x4aa5, \
34 0xad, 0x5c, 0x7b, 0x8, 0x88, 0x9d, 0xff, 0xe2)
35 static const uuid_le spar_video_guid = SPAR_CONSOLEVIDEO_CHANNEL_PROTOCOL_GUID;
42 struct channel_header chan_hdr;
45 bool needs_lock; /* channel creator knows if more than one
46 * thread will be inserting or removing */
47 spinlock_t insert_lock; /* protect head writes in chan_hdr */
48 spinlock_t remove_lock; /* protect tail writes in chan_hdr */
51 struct signal_queue_header req_queue;
52 struct signal_queue_header rsp_queue;
53 struct signal_queue_header event_queue;
54 struct signal_queue_header ack_queue;
60 /* Creates the struct visorchannel abstraction for a data area in memory,
61 * but does NOT modify this data area.
63 static struct visorchannel *
64 visorchannel_create_guts(u64 physaddr, unsigned long channel_bytes,
65 gfp_t gfp, unsigned long off,
66 uuid_le guid, bool needs_lock)
68 struct visorchannel *channel;
70 size_t size = sizeof(struct channel_header);
75 channel = kzalloc(sizeof(*channel), gfp);
79 channel->needs_lock = needs_lock;
80 spin_lock_init(&channel->insert_lock);
81 spin_lock_init(&channel->remove_lock);
83 /* Video driver constains the efi framebuffer so it will get a
84 * conflict resource when requesting its full mem region. Since
85 * we are only using the efi framebuffer for video we can ignore
86 * this. Remember that we haven't requested it so we don't try to
89 channel->requested = request_mem_region(physaddr, size, MYDRVNAME);
90 if (!channel->requested) {
91 if (uuid_le_cmp(guid, spar_video_guid)) {
92 /* Not the video channel we care about this */
97 channel->mapped = memremap(physaddr, size, MEMREMAP_WB);
98 if (!channel->mapped) {
99 release_mem_region(physaddr, size);
103 channel->physaddr = physaddr;
104 channel->nbytes = size;
106 err = visorchannel_read(channel, 0, &channel->chan_hdr,
107 sizeof(struct channel_header));
111 /* we had better be a CLIENT of this channel */
112 if (channel_bytes == 0)
113 channel_bytes = (ulong)channel->chan_hdr.size;
114 if (uuid_le_cmp(guid, NULL_UUID_LE) == 0)
115 guid = channel->chan_hdr.chtype;
117 memunmap(channel->mapped);
118 if (channel->requested)
119 release_mem_region(channel->physaddr, channel->nbytes);
120 channel->mapped = NULL;
121 channel->requested = request_mem_region(channel->physaddr,
122 channel_bytes, MYDRVNAME);
123 if (!channel->requested) {
124 if (uuid_le_cmp(guid, spar_video_guid)) {
125 /* Different we care about this */
130 channel->mapped = memremap(channel->physaddr, channel_bytes,
132 if (!channel->mapped) {
133 release_mem_region(channel->physaddr, channel_bytes);
137 channel->nbytes = channel_bytes;
139 channel->size = channel_bytes;
140 channel->guid = guid;
144 visorchannel_destroy(channel);
148 struct visorchannel *
149 visorchannel_create(u64 physaddr, unsigned long channel_bytes,
150 gfp_t gfp, uuid_le guid)
152 return visorchannel_create_guts(physaddr, channel_bytes, gfp, 0, guid,
155 EXPORT_SYMBOL_GPL(visorchannel_create);
157 struct visorchannel *
158 visorchannel_create_with_lock(u64 physaddr, unsigned long channel_bytes,
159 gfp_t gfp, uuid_le guid)
161 return visorchannel_create_guts(physaddr, channel_bytes, gfp, 0, guid,
164 EXPORT_SYMBOL_GPL(visorchannel_create_with_lock);
167 visorchannel_destroy(struct visorchannel *channel)
171 if (channel->mapped) {
172 memunmap(channel->mapped);
173 if (channel->requested)
174 release_mem_region(channel->physaddr, channel->nbytes);
178 EXPORT_SYMBOL_GPL(visorchannel_destroy);
181 visorchannel_get_physaddr(struct visorchannel *channel)
183 return channel->physaddr;
185 EXPORT_SYMBOL_GPL(visorchannel_get_physaddr);
188 visorchannel_get_nbytes(struct visorchannel *channel)
190 return channel->size;
192 EXPORT_SYMBOL_GPL(visorchannel_get_nbytes);
195 visorchannel_uuid_id(uuid_le *guid, char *s)
197 sprintf(s, "%pUL", guid);
200 EXPORT_SYMBOL_GPL(visorchannel_uuid_id);
203 visorchannel_id(struct visorchannel *channel, char *s)
205 return visorchannel_uuid_id(&channel->guid, s);
207 EXPORT_SYMBOL_GPL(visorchannel_id);
210 visorchannel_zoneid(struct visorchannel *channel, char *s)
212 return visorchannel_uuid_id(&channel->chan_hdr.zone_uuid, s);
214 EXPORT_SYMBOL_GPL(visorchannel_zoneid);
217 visorchannel_get_clientpartition(struct visorchannel *channel)
219 return channel->chan_hdr.partition_handle;
221 EXPORT_SYMBOL_GPL(visorchannel_get_clientpartition);
224 visorchannel_set_clientpartition(struct visorchannel *channel,
225 u64 partition_handle)
227 channel->chan_hdr.partition_handle = partition_handle;
230 EXPORT_SYMBOL_GPL(visorchannel_set_clientpartition);
233 visorchannel_get_uuid(struct visorchannel *channel)
235 return channel->guid;
237 EXPORT_SYMBOL_GPL(visorchannel_get_uuid);
240 visorchannel_read(struct visorchannel *channel, ulong offset,
241 void *local, ulong nbytes)
243 if (offset + nbytes > channel->nbytes)
246 memcpy(local, channel->mapped + offset, nbytes);
250 EXPORT_SYMBOL_GPL(visorchannel_read);
253 visorchannel_write(struct visorchannel *channel, ulong offset,
254 void *local, ulong nbytes)
256 size_t chdr_size = sizeof(struct channel_header);
259 if (offset + nbytes > channel->nbytes)
262 if (offset < chdr_size) {
263 copy_size = min(chdr_size - offset, nbytes);
264 memcpy(&channel->chan_hdr + offset, local, copy_size);
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)
422 if (channel->needs_lock) {
423 spin_lock(&channel->remove_lock);
424 rc = signalremove_inner(channel, queue, msg);
425 spin_unlock(&channel->remove_lock);
427 rc = signalremove_inner(channel, queue, msg);
432 EXPORT_SYMBOL_GPL(visorchannel_signalremove);
435 signalinsert_inner(struct visorchannel *channel, u32 queue, void *msg)
437 struct signal_queue_header sig_hdr;
439 if (!sig_read_header(channel, queue, &sig_hdr))
442 sig_hdr.head = ((sig_hdr.head + 1) % sig_hdr.max_slots);
443 if (sig_hdr.head == sig_hdr.tail) {
444 sig_hdr.num_overflows++;
445 visorchannel_write(channel,
446 SIG_QUEUE_OFFSET(&channel->chan_hdr, queue) +
447 offsetof(struct signal_queue_header,
449 &(sig_hdr.num_overflows),
450 sizeof(sig_hdr.num_overflows));
454 if (!sig_write_data(channel, queue, &sig_hdr, sig_hdr.head, msg))
459 /* For each data field in SIGNAL_QUEUE_HEADER that was modified,
460 * update host memory.
462 mb(); /* required for channel synch */
463 if (!SIG_WRITE_FIELD(channel, queue, &sig_hdr, head))
465 if (!SIG_WRITE_FIELD(channel, queue, &sig_hdr, num_sent))
472 visorchannel_signalinsert(struct visorchannel *channel, u32 queue, void *msg)
476 if (channel->needs_lock) {
477 spin_lock(&channel->insert_lock);
478 rc = signalinsert_inner(channel, queue, msg);
479 spin_unlock(&channel->insert_lock);
481 rc = signalinsert_inner(channel, queue, msg);
486 EXPORT_SYMBOL_GPL(visorchannel_signalinsert);
489 visorchannel_signalqueue_slots_avail(struct visorchannel *channel, u32 queue)
491 struct signal_queue_header sig_hdr;
492 u32 slots_avail, slots_used;
495 if (!sig_read_header(channel, queue, &sig_hdr))
500 head = head + sig_hdr.max_slots;
501 slots_used = (head - tail);
502 slots_avail = sig_hdr.max_signals - slots_used;
503 return (int)slots_avail;
505 EXPORT_SYMBOL_GPL(visorchannel_signalqueue_slots_avail);
508 visorchannel_signalqueue_max_slots(struct visorchannel *channel, u32 queue)
510 struct signal_queue_header sig_hdr;
512 if (!sig_read_header(channel, queue, &sig_hdr))
514 return (int)sig_hdr.max_signals;
516 EXPORT_SYMBOL_GPL(visorchannel_signalqueue_max_slots);
519 sigqueue_debug(struct signal_queue_header *q, int which, struct seq_file *seq)
521 seq_printf(seq, "Signal Queue #%d\n", which);
522 seq_printf(seq, " VersionId = %lu\n", (ulong)q->version);
523 seq_printf(seq, " Type = %lu\n", (ulong)q->chtype);
524 seq_printf(seq, " oSignalBase = %llu\n",
525 (long long)q->sig_base_offset);
526 seq_printf(seq, " SignalSize = %lu\n", (ulong)q->signal_size);
527 seq_printf(seq, " MaxSignalSlots = %lu\n",
528 (ulong)q->max_slots);
529 seq_printf(seq, " MaxSignals = %lu\n", (ulong)q->max_signals);
530 seq_printf(seq, " FeatureFlags = %-16.16Lx\n",
531 (long long)q->features);
532 seq_printf(seq, " NumSignalsSent = %llu\n",
533 (long long)q->num_sent);
534 seq_printf(seq, " NumSignalsReceived = %llu\n",
535 (long long)q->num_received);
536 seq_printf(seq, " NumOverflows = %llu\n",
537 (long long)q->num_overflows);
538 seq_printf(seq, " Head = %lu\n", (ulong)q->head);
539 seq_printf(seq, " Tail = %lu\n", (ulong)q->tail);
543 visorchannel_debug(struct visorchannel *channel, int num_queues,
544 struct seq_file *seq, u32 off)
547 ulong nbytes = 0, nbytes_region = 0;
548 struct channel_header hdr;
549 struct channel_header *phdr = &hdr;
556 addr = visorchannel_get_physaddr(channel);
557 nbytes_region = visorchannel_get_nbytes(channel);
558 errcode = visorchannel_read(channel, off,
559 phdr, sizeof(struct channel_header));
562 "Read of channel header failed with errcode=%d)\n",
565 phdr = &channel->chan_hdr;
566 seq_puts(seq, "(following data may be stale)\n");
571 nbytes = (ulong)(phdr->size);
572 seq_printf(seq, "--- Begin channel @0x%-16.16Lx for 0x%lx bytes (region=0x%lx bytes) ---\n",
573 addr + off, nbytes, nbytes_region);
574 seq_printf(seq, "Type = %pUL\n", &phdr->chtype);
575 seq_printf(seq, "ZoneGuid = %pUL\n", &phdr->zone_uuid);
576 seq_printf(seq, "Signature = 0x%-16.16Lx\n",
577 (long long)phdr->signature);
578 seq_printf(seq, "LegacyState = %lu\n", (ulong)phdr->legacy_state);
579 seq_printf(seq, "SrvState = %lu\n", (ulong)phdr->srv_state);
580 seq_printf(seq, "CliStateBoot = %lu\n", (ulong)phdr->cli_state_boot);
581 seq_printf(seq, "CliStateOS = %lu\n", (ulong)phdr->cli_state_os);
582 seq_printf(seq, "HeaderSize = %lu\n", (ulong)phdr->header_size);
583 seq_printf(seq, "Size = %llu\n", (long long)phdr->size);
584 seq_printf(seq, "Features = 0x%-16.16llx\n",
585 (long long)phdr->features);
586 seq_printf(seq, "PartitionHandle = 0x%-16.16llx\n",
587 (long long)phdr->partition_handle);
588 seq_printf(seq, "Handle = 0x%-16.16llx\n",
589 (long long)phdr->handle);
590 seq_printf(seq, "VersionId = %lu\n", (ulong)phdr->version_id);
591 seq_printf(seq, "oChannelSpace = %llu\n",
592 (long long)phdr->ch_space_offset);
593 if ((phdr->ch_space_offset == 0) || (errcode < 0))
596 for (i = 0; i < num_queues; i++) {
597 struct signal_queue_header q;
599 errcode = visorchannel_read(channel,
601 phdr->ch_space_offset +
606 "failed to read signal queue #%d from channel @0x%-16.16Lx errcode=%d\n",
610 sigqueue_debug(&q, i, seq);
612 seq_printf(seq, "--- End channel @0x%-16.16Lx for 0x%lx bytes ---\n",
615 EXPORT_SYMBOL_GPL(visorchannel_debug);