Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2010 - 2015 UNISYS CORPORATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *  This provides s-Par channel communication primitives, which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *  independent of the mechanism used to access the channel data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/uuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/visorbus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "visorbus_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "controlvmchannel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define VISOR_DRV_NAME "visorchannel"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define VISOR_CONSOLEVIDEO_CHANNEL_GUID \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	GUID_INIT(0x3cd6e705, 0xd6a2, 0x4aa5, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		  0xad, 0x5c, 0x7b, 0x8, 0x88, 0x9d, 0xff, 0xe2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static const guid_t visor_video_guid = VISOR_CONSOLEVIDEO_CHANNEL_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) struct visorchannel {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	u64 physaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	ulong nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	void *mapped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	bool requested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct channel_header chan_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	guid_t guid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	 * channel creator knows if more than one thread will be inserting or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	 * removing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	bool needs_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	/* protect head writes in chan_hdr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	spinlock_t insert_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	/* protect tail writes in chan_hdr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	spinlock_t remove_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	guid_t type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	guid_t inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) void visorchannel_destroy(struct visorchannel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (!channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (channel->mapped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		memunmap(channel->mapped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		if (channel->requested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			release_mem_region(channel->physaddr, channel->nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	kfree(channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) u64 visorchannel_get_physaddr(struct visorchannel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	return channel->physaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) ulong visorchannel_get_nbytes(struct visorchannel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return channel->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) char *visorchannel_guid_id(const guid_t *guid, char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	sprintf(s, "%pUL", guid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) char *visorchannel_id(struct visorchannel *channel, char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return visorchannel_guid_id(&channel->guid, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) char *visorchannel_zoneid(struct visorchannel *channel, char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return visorchannel_guid_id(&channel->chan_hdr.zone_guid, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) u64 visorchannel_get_clientpartition(struct visorchannel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return channel->chan_hdr.partition_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) int visorchannel_set_clientpartition(struct visorchannel *channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				     u64 partition_handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	channel->chan_hdr.partition_handle = partition_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * visorchannel_get_guid() - queries the GUID of the designated channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * @channel: the channel to query
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * Return: the GUID of the provided channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) const guid_t *visorchannel_get_guid(struct visorchannel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return &channel->guid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) EXPORT_SYMBOL_GPL(visorchannel_get_guid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int visorchannel_read(struct visorchannel *channel, ulong offset, void *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		      ulong nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (offset + nbytes > channel->nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	memcpy(dest, channel->mapped + offset, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int visorchannel_write(struct visorchannel *channel, ulong offset, void *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		       ulong nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	size_t chdr_size = sizeof(struct channel_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	size_t copy_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (offset + nbytes > channel->nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (offset < chdr_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		copy_size = min(chdr_size - offset, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		memcpy(((char *)(&channel->chan_hdr)) + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		       dest, copy_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	memcpy(channel->mapped + offset, dest, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) void *visorchannel_get_header(struct visorchannel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return &channel->chan_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * Return offset of a specific SIGNAL_QUEUE_HEADER from the beginning of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * channel header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int sig_queue_offset(struct channel_header *chan_hdr, int q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return ((chan_hdr)->ch_space_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	       ((q) * sizeof(struct signal_queue_header)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * Return offset of a specific queue entry (data) from the beginning of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * channel header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static int sig_data_offset(struct channel_header *chan_hdr, int q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			   struct signal_queue_header *sig_hdr, int slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return (sig_queue_offset(chan_hdr, q) + sig_hdr->sig_base_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	       (slot * sig_hdr->signal_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * Write the contents of a specific field within a SIGNAL_QUEUE_HEADER back into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * host memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #define SIG_WRITE_FIELD(channel, queue, sig_hdr, FIELD) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	visorchannel_write(channel, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			   sig_queue_offset(&channel->chan_hdr, queue) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			   offsetof(struct signal_queue_header, FIELD), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			   &((sig_hdr)->FIELD), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			   sizeof((sig_hdr)->FIELD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static int sig_read_header(struct visorchannel *channel, u32 queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			   struct signal_queue_header *sig_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (channel->chan_hdr.ch_space_offset < sizeof(struct channel_header))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	/* Read the appropriate SIGNAL_QUEUE_HEADER into local memory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	return visorchannel_read(channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				 sig_queue_offset(&channel->chan_hdr, queue),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 				 sig_hdr, sizeof(struct signal_queue_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static int sig_read_data(struct visorchannel *channel, u32 queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			 struct signal_queue_header *sig_hdr, u32 slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			 void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	int signal_data_offset = sig_data_offset(&channel->chan_hdr, queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 						 sig_hdr, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return visorchannel_read(channel, signal_data_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				 data, sig_hdr->signal_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int sig_write_data(struct visorchannel *channel, u32 queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			  struct signal_queue_header *sig_hdr, u32 slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			  void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	int signal_data_offset = sig_data_offset(&channel->chan_hdr, queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 						 sig_hdr, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return visorchannel_write(channel, signal_data_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 				  data, sig_hdr->signal_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int signalremove_inner(struct visorchannel *channel, u32 queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			      void *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct signal_queue_header sig_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	error = sig_read_header(channel, queue, &sig_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	/* No signals to remove; have caller try again. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (sig_hdr.head == sig_hdr.tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	sig_hdr.tail = (sig_hdr.tail + 1) % sig_hdr.max_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	error = sig_read_data(channel, queue, &sig_hdr, sig_hdr.tail, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	sig_hdr.num_received++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	 * For each data field in SIGNAL_QUEUE_HEADER that was modified, update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 * host memory. Required for channel sync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	error = SIG_WRITE_FIELD(channel, queue, &sig_hdr, tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	error = SIG_WRITE_FIELD(channel, queue, &sig_hdr, num_received);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  * visorchannel_signalremove() - removes a message from the designated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  *                               channel/queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  * @channel: the channel the message will be removed from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  * @queue:   the queue the message will be removed from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  * @msg:     the message to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  * Return: integer error code indicating the status of the removal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) int visorchannel_signalremove(struct visorchannel *channel, u32 queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			      void *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (channel->needs_lock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		spin_lock_irqsave(&channel->remove_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		rc = signalremove_inner(channel, queue, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		spin_unlock_irqrestore(&channel->remove_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		rc = signalremove_inner(channel, queue, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) EXPORT_SYMBOL_GPL(visorchannel_signalremove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static bool queue_empty(struct visorchannel *channel, u32 queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct signal_queue_header sig_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	if (sig_read_header(channel, queue, &sig_hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return (sig_hdr.head == sig_hdr.tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)  * visorchannel_signalempty() - checks if the designated channel/queue contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)  *				any messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  * @channel: the channel to query
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  * @queue:   the queue in the channel to query
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  * Return: boolean indicating whether any messages in the designated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)  *         channel/queue are present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) bool visorchannel_signalempty(struct visorchannel *channel, u32 queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	bool rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (!channel->needs_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		return queue_empty(channel, queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	spin_lock_irqsave(&channel->remove_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	rc = queue_empty(channel, queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	spin_unlock_irqrestore(&channel->remove_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) EXPORT_SYMBOL_GPL(visorchannel_signalempty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static int signalinsert_inner(struct visorchannel *channel, u32 queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			      void *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct signal_queue_header sig_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	err = sig_read_header(channel, queue, &sig_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	sig_hdr.head = (sig_hdr.head + 1) % sig_hdr.max_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (sig_hdr.head == sig_hdr.tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		sig_hdr.num_overflows++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		err = SIG_WRITE_FIELD(channel, queue, &sig_hdr, num_overflows);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	err = sig_write_data(channel, queue, &sig_hdr, sig_hdr.head, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	sig_hdr.num_sent++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	 * For each data field in SIGNAL_QUEUE_HEADER that was modified, update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	 * host memory. Required for channel sync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	err = SIG_WRITE_FIELD(channel, queue, &sig_hdr, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	err = SIG_WRITE_FIELD(channel, queue, &sig_hdr, num_sent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  * visorchannel_create() - creates the struct visorchannel abstraction for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  *                         data area in memory, but does NOT modify this data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  *                         area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  * @physaddr:      physical address of start of channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  * @gfp:           gfp_t to use when allocating memory for the data struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  * @guid:          GUID that identifies channel type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  * @needs_lock:    must specify true if you have multiple threads of execution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  *                 that will be calling visorchannel methods of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  *                 visorchannel at the same time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  * Return: pointer to visorchannel that was created if successful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  *         otherwise NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct visorchannel *visorchannel_create(u64 physaddr, gfp_t gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 					 const guid_t *guid, bool needs_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	struct visorchannel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	size_t size = sizeof(struct channel_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (physaddr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	channel = kzalloc(sizeof(*channel), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (!channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	channel->needs_lock = needs_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	spin_lock_init(&channel->insert_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	spin_lock_init(&channel->remove_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	 * Video driver constains the efi framebuffer so it will get a conflict
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	 * resource when requesting its full mem region. Since we are only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	 * using the efi framebuffer for video we can ignore this. Remember that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	 * we haven't requested it so we don't try to release later on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	channel->requested = request_mem_region(physaddr, size, VISOR_DRV_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (!channel->requested && !guid_equal(guid, &visor_video_guid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		/* we only care about errors if this is not the video channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		goto err_destroy_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	channel->mapped = memremap(physaddr, size, MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	if (!channel->mapped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		release_mem_region(physaddr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		goto err_destroy_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	channel->physaddr = physaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	channel->nbytes = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	err = visorchannel_read(channel, 0, &channel->chan_hdr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		goto err_destroy_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	size = (ulong)channel->chan_hdr.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	memunmap(channel->mapped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (channel->requested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		release_mem_region(channel->physaddr, channel->nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	channel->mapped = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	channel->requested = request_mem_region(channel->physaddr, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 						VISOR_DRV_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (!channel->requested && !guid_equal(guid, &visor_video_guid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		/* we only care about errors if this is not the video channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		goto err_destroy_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	channel->mapped = memremap(channel->physaddr, size, MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (!channel->mapped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		release_mem_region(channel->physaddr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		goto err_destroy_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	channel->nbytes = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	guid_copy(&channel->guid, guid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	return channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) err_destroy_channel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	visorchannel_destroy(channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  * visorchannel_signalinsert() - inserts a message into the designated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  *                               channel/queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)  * @channel: the channel the message will be added to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)  * @queue:   the queue the message will be added to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)  * @msg:     the message to insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  * Return: integer error code indicating the status of the insertion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) int visorchannel_signalinsert(struct visorchannel *channel, u32 queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			      void *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (channel->needs_lock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		spin_lock_irqsave(&channel->insert_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		rc = signalinsert_inner(channel, queue, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		spin_unlock_irqrestore(&channel->insert_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		rc = signalinsert_inner(channel, queue, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) EXPORT_SYMBOL_GPL(visorchannel_signalinsert);