visorchannel.c 12 KB

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