core-iso.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Isochronous I/O functionality:
  4. * - Isochronous DMA context management
  5. * - Isochronous bus resource management (channels, bandwidth), client side
  6. *
  7. * Copyright (C) 2006 Kristian Hoegsberg <krh@bitplanet.net>
  8. */
  9. #include <linux/dma-mapping.h>
  10. #include <linux/errno.h>
  11. #include <linux/firewire.h>
  12. #include <linux/firewire-constants.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/export.h>
  19. #include <asm/byteorder.h>
  20. #include "core.h"
  21. #include <trace/events/firewire.h>
  22. /*
  23. * Isochronous DMA context management
  24. */
  25. int fw_iso_buffer_alloc(struct fw_iso_buffer *buffer, int page_count)
  26. {
  27. struct page **page_array __free(kfree) = kzalloc_objs(page_array[0],
  28. page_count);
  29. if (!page_array)
  30. return -ENOMEM;
  31. // Retrieve noncontiguous pages. The descriptors for 1394 OHCI isochronous DMA contexts
  32. // have a set of address and length per each, while the reason to use pages is the
  33. // convenience to map them into virtual address space of user process.
  34. unsigned long nr_populated = alloc_pages_bulk(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO,
  35. page_count, page_array);
  36. if (nr_populated != page_count) {
  37. // Assuming the above call fills page_array sequentially from the beginning.
  38. release_pages(page_array, nr_populated);
  39. return -ENOMEM;
  40. }
  41. buffer->page_count = page_count;
  42. buffer->pages = no_free_ptr(page_array);
  43. return 0;
  44. }
  45. int fw_iso_buffer_map_dma(struct fw_iso_buffer *buffer, struct fw_card *card,
  46. enum dma_data_direction direction)
  47. {
  48. dma_addr_t *dma_addrs __free(kfree) = kzalloc_objs(dma_addrs[0],
  49. buffer->page_count);
  50. int i;
  51. if (!dma_addrs)
  52. return -ENOMEM;
  53. // Retrieve DMA mapping addresses for the pages. They are not contiguous. Maintain the cache
  54. // coherency for the pages by hand.
  55. for (i = 0; i < buffer->page_count; i++) {
  56. // The dma_map_phys() with a physical address per page is available here, instead.
  57. dma_addr_t dma_addr = dma_map_page(card->device, buffer->pages[i], 0, PAGE_SIZE,
  58. direction);
  59. if (dma_mapping_error(card->device, dma_addr))
  60. break;
  61. dma_addrs[i] = dma_addr;
  62. }
  63. if (i < buffer->page_count) {
  64. while (i-- > 0)
  65. dma_unmap_page(card->device, dma_addrs[i], PAGE_SIZE, buffer->direction);
  66. return -ENOMEM;
  67. }
  68. buffer->direction = direction;
  69. buffer->dma_addrs = no_free_ptr(dma_addrs);
  70. return 0;
  71. }
  72. int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
  73. int page_count, enum dma_data_direction direction)
  74. {
  75. int ret;
  76. ret = fw_iso_buffer_alloc(buffer, page_count);
  77. if (ret < 0)
  78. return ret;
  79. ret = fw_iso_buffer_map_dma(buffer, card, direction);
  80. if (ret < 0)
  81. fw_iso_buffer_destroy(buffer, card);
  82. return ret;
  83. }
  84. EXPORT_SYMBOL(fw_iso_buffer_init);
  85. void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer,
  86. struct fw_card *card)
  87. {
  88. if (buffer->dma_addrs) {
  89. for (int i = 0; i < buffer->page_count; ++i) {
  90. dma_addr_t dma_addr = buffer->dma_addrs[i];
  91. dma_unmap_page(card->device, dma_addr, PAGE_SIZE, buffer->direction);
  92. }
  93. kfree(buffer->dma_addrs);
  94. buffer->dma_addrs = NULL;
  95. }
  96. if (buffer->pages) {
  97. release_pages(buffer->pages, buffer->page_count);
  98. kfree(buffer->pages);
  99. buffer->pages = NULL;
  100. }
  101. buffer->page_count = 0;
  102. }
  103. EXPORT_SYMBOL(fw_iso_buffer_destroy);
  104. /* Convert DMA address to offset into virtually contiguous buffer. */
  105. size_t fw_iso_buffer_lookup(struct fw_iso_buffer *buffer, dma_addr_t completed)
  106. {
  107. for (int i = 0; i < buffer->page_count; i++) {
  108. dma_addr_t dma_addr = buffer->dma_addrs[i];
  109. ssize_t offset = (ssize_t)completed - (ssize_t)dma_addr;
  110. if (offset > 0 && offset <= PAGE_SIZE)
  111. return (i << PAGE_SHIFT) + offset;
  112. }
  113. return 0;
  114. }
  115. struct fw_iso_context *__fw_iso_context_create(struct fw_card *card, int type, int channel,
  116. int speed, size_t header_size, size_t header_storage_size,
  117. union fw_iso_callback callback, void *callback_data)
  118. {
  119. struct fw_iso_context *ctx;
  120. ctx = card->driver->allocate_iso_context(card, type, channel, header_size,
  121. header_storage_size);
  122. if (IS_ERR(ctx))
  123. return ctx;
  124. ctx->card = card;
  125. ctx->type = type;
  126. ctx->channel = channel;
  127. ctx->speed = speed;
  128. ctx->flags = 0;
  129. ctx->header_size = header_size;
  130. ctx->header_storage_size = header_storage_size;
  131. ctx->callback = callback;
  132. ctx->callback_data = callback_data;
  133. trace_isoc_outbound_allocate(ctx, channel, speed);
  134. trace_isoc_inbound_single_allocate(ctx, channel, header_size);
  135. trace_isoc_inbound_multiple_allocate(ctx);
  136. return ctx;
  137. }
  138. EXPORT_SYMBOL(__fw_iso_context_create);
  139. void fw_iso_context_destroy(struct fw_iso_context *ctx)
  140. {
  141. trace_isoc_outbound_destroy(ctx);
  142. trace_isoc_inbound_single_destroy(ctx);
  143. trace_isoc_inbound_multiple_destroy(ctx);
  144. ctx->card->driver->free_iso_context(ctx);
  145. }
  146. EXPORT_SYMBOL(fw_iso_context_destroy);
  147. int fw_iso_context_start(struct fw_iso_context *ctx,
  148. int cycle, int sync, int tags)
  149. {
  150. trace_isoc_outbound_start(ctx, cycle);
  151. trace_isoc_inbound_single_start(ctx, cycle, sync, tags);
  152. trace_isoc_inbound_multiple_start(ctx, cycle, sync, tags);
  153. return ctx->card->driver->start_iso(ctx, cycle, sync, tags);
  154. }
  155. EXPORT_SYMBOL(fw_iso_context_start);
  156. int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels)
  157. {
  158. trace_isoc_inbound_multiple_channels(ctx, *channels);
  159. return ctx->card->driver->set_iso_channels(ctx, channels);
  160. }
  161. int fw_iso_context_queue(struct fw_iso_context *ctx,
  162. struct fw_iso_packet *packet,
  163. struct fw_iso_buffer *buffer,
  164. unsigned long payload)
  165. {
  166. trace_isoc_outbound_queue(ctx, payload, packet);
  167. trace_isoc_inbound_single_queue(ctx, payload, packet);
  168. trace_isoc_inbound_multiple_queue(ctx, payload, packet);
  169. return ctx->card->driver->queue_iso(ctx, packet, buffer, payload);
  170. }
  171. EXPORT_SYMBOL(fw_iso_context_queue);
  172. void fw_iso_context_queue_flush(struct fw_iso_context *ctx)
  173. {
  174. trace_isoc_outbound_flush(ctx);
  175. trace_isoc_inbound_single_flush(ctx);
  176. trace_isoc_inbound_multiple_flush(ctx);
  177. ctx->card->driver->flush_queue_iso(ctx);
  178. }
  179. EXPORT_SYMBOL(fw_iso_context_queue_flush);
  180. /**
  181. * fw_iso_context_flush_completions() - process isochronous context in current process context.
  182. * @ctx: the isochronous context
  183. *
  184. * Process the isochronous context in the current process context. The registered callback function
  185. * is called when a queued packet buffer with the interrupt flag is completed, either after
  186. * transmission in the IT context or after being filled in the IR context. Additionally, the
  187. * callback function is also called for the packet buffer completed at last. Furthermore, the
  188. * callback function is called as well when the header buffer in the context becomes full. If it is
  189. * required to process the context asynchronously, fw_iso_context_schedule_flush_completions() is
  190. * available instead.
  191. *
  192. * Context: Process context. May sleep due to disable_work_sync().
  193. */
  194. int fw_iso_context_flush_completions(struct fw_iso_context *ctx)
  195. {
  196. int err;
  197. trace_isoc_outbound_flush_completions(ctx);
  198. trace_isoc_inbound_single_flush_completions(ctx);
  199. trace_isoc_inbound_multiple_flush_completions(ctx);
  200. might_sleep();
  201. // Avoid dead lock due to programming mistake.
  202. if (WARN_ON_ONCE(current_work() == &ctx->work))
  203. return 0;
  204. disable_work_sync(&ctx->work);
  205. err = ctx->card->driver->flush_iso_completions(ctx);
  206. enable_work(&ctx->work);
  207. return err;
  208. }
  209. EXPORT_SYMBOL(fw_iso_context_flush_completions);
  210. int fw_iso_context_stop(struct fw_iso_context *ctx)
  211. {
  212. int err;
  213. trace_isoc_outbound_stop(ctx);
  214. trace_isoc_inbound_single_stop(ctx);
  215. trace_isoc_inbound_multiple_stop(ctx);
  216. might_sleep();
  217. // Avoid dead lock due to programming mistake.
  218. if (WARN_ON_ONCE(current_work() == &ctx->work))
  219. return 0;
  220. err = ctx->card->driver->stop_iso(ctx);
  221. cancel_work_sync(&ctx->work);
  222. return err;
  223. }
  224. EXPORT_SYMBOL(fw_iso_context_stop);
  225. /*
  226. * Isochronous bus resource management (channels, bandwidth), client side
  227. */
  228. static int manage_bandwidth(struct fw_card *card, int irm_id, int generation,
  229. int bandwidth, bool allocate)
  230. {
  231. int try, new, old = allocate ? BANDWIDTH_AVAILABLE_INITIAL : 0;
  232. __be32 data[2];
  233. /*
  234. * On a 1394a IRM with low contention, try < 1 is enough.
  235. * On a 1394-1995 IRM, we need at least try < 2.
  236. * Let's just do try < 5.
  237. */
  238. for (try = 0; try < 5; try++) {
  239. new = allocate ? old - bandwidth : old + bandwidth;
  240. if (new < 0 || new > BANDWIDTH_AVAILABLE_INITIAL)
  241. return -EBUSY;
  242. data[0] = cpu_to_be32(old);
  243. data[1] = cpu_to_be32(new);
  244. switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
  245. irm_id, generation, SCODE_100,
  246. CSR_REGISTER_BASE + CSR_BANDWIDTH_AVAILABLE,
  247. data, 8)) {
  248. case RCODE_GENERATION:
  249. /* A generation change frees all bandwidth. */
  250. return allocate ? -EAGAIN : bandwidth;
  251. case RCODE_COMPLETE:
  252. if (be32_to_cpup(data) == old)
  253. return bandwidth;
  254. old = be32_to_cpup(data);
  255. /* Fall through. */
  256. }
  257. }
  258. return -EIO;
  259. }
  260. static int manage_channel(struct fw_card *card, int irm_id, int generation,
  261. u32 channels_mask, u64 offset, bool allocate)
  262. {
  263. __be32 bit, all, old;
  264. __be32 data[2];
  265. int channel, ret = -EIO, retry = 5;
  266. old = all = allocate ? cpu_to_be32(~0) : 0;
  267. for (channel = 0; channel < 32; channel++) {
  268. if (!(channels_mask & 1 << channel))
  269. continue;
  270. ret = -EBUSY;
  271. bit = cpu_to_be32(1 << (31 - channel));
  272. if ((old & bit) != (all & bit))
  273. continue;
  274. data[0] = old;
  275. data[1] = old ^ bit;
  276. switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
  277. irm_id, generation, SCODE_100,
  278. offset, data, 8)) {
  279. case RCODE_GENERATION:
  280. /* A generation change frees all channels. */
  281. return allocate ? -EAGAIN : channel;
  282. case RCODE_COMPLETE:
  283. if (data[0] == old)
  284. return channel;
  285. old = data[0];
  286. /* Is the IRM 1394a-2000 compliant? */
  287. if ((data[0] & bit) == (data[1] & bit))
  288. continue;
  289. fallthrough; /* It's a 1394-1995 IRM, retry */
  290. default:
  291. if (retry) {
  292. retry--;
  293. channel--;
  294. } else {
  295. ret = -EIO;
  296. }
  297. }
  298. }
  299. return ret;
  300. }
  301. static void deallocate_channel(struct fw_card *card, int irm_id,
  302. int generation, int channel)
  303. {
  304. u32 mask;
  305. u64 offset;
  306. mask = channel < 32 ? 1 << channel : 1 << (channel - 32);
  307. offset = channel < 32 ? CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI :
  308. CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO;
  309. manage_channel(card, irm_id, generation, mask, offset, false);
  310. }
  311. /**
  312. * fw_iso_resource_manage() - Allocate or deallocate a channel and/or bandwidth
  313. * @card: card interface for this action
  314. * @generation: bus generation
  315. * @channels_mask: bitmask for channel allocation
  316. * @channel: pointer for returning channel allocation result
  317. * @bandwidth: pointer for returning bandwidth allocation result
  318. * @allocate: whether to allocate (true) or deallocate (false)
  319. *
  320. * In parameters: card, generation, channels_mask, bandwidth, allocate
  321. * Out parameters: channel, bandwidth
  322. *
  323. * This function blocks (sleeps) during communication with the IRM.
  324. *
  325. * Allocates or deallocates at most one channel out of channels_mask.
  326. * channels_mask is a bitfield with MSB for channel 63 and LSB for channel 0.
  327. * (Note, the IRM's CHANNELS_AVAILABLE is a big-endian bitfield with MSB for
  328. * channel 0 and LSB for channel 63.)
  329. * Allocates or deallocates as many bandwidth allocation units as specified.
  330. *
  331. * Returns channel < 0 if no channel was allocated or deallocated.
  332. * Returns bandwidth = 0 if no bandwidth was allocated or deallocated.
  333. *
  334. * If generation is stale, deallocations succeed but allocations fail with
  335. * channel = -EAGAIN.
  336. *
  337. * If channel allocation fails, no bandwidth will be allocated either.
  338. * If bandwidth allocation fails, no channel will be allocated either.
  339. * But deallocations of channel and bandwidth are tried independently
  340. * of each other's success.
  341. */
  342. void fw_iso_resource_manage(struct fw_card *card, int generation,
  343. u64 channels_mask, int *channel, int *bandwidth,
  344. bool allocate)
  345. {
  346. u32 channels_hi = channels_mask; /* channels 31...0 */
  347. u32 channels_lo = channels_mask >> 32; /* channels 63...32 */
  348. int irm_id, ret, c = -EINVAL;
  349. scoped_guard(spinlock_irq, &card->lock)
  350. irm_id = card->irm_node->node_id;
  351. if (channels_hi)
  352. c = manage_channel(card, irm_id, generation, channels_hi,
  353. CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI,
  354. allocate);
  355. if (channels_lo && c < 0) {
  356. c = manage_channel(card, irm_id, generation, channels_lo,
  357. CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO,
  358. allocate);
  359. if (c >= 0)
  360. c += 32;
  361. }
  362. *channel = c;
  363. if (allocate && channels_mask != 0 && c < 0)
  364. *bandwidth = 0;
  365. if (*bandwidth == 0)
  366. return;
  367. ret = manage_bandwidth(card, irm_id, generation, *bandwidth, allocate);
  368. if (ret < 0)
  369. *bandwidth = 0;
  370. if (allocate && ret < 0) {
  371. if (c >= 0)
  372. deallocate_channel(card, irm_id, generation, c);
  373. *channel = ret;
  374. }
  375. }
  376. EXPORT_SYMBOL(fw_iso_resource_manage);