core-iso.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Isochronous I/O functionality:
  3. * - Isochronous DMA context management
  4. * - Isochronous bus resource management (channels, bandwidth), client side
  5. *
  6. * Copyright (C) 2006 Kristian Hoegsberg <krh@bitplanet.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <linux/dma-mapping.h>
  23. #include <linux/errno.h>
  24. #include <linux/firewire.h>
  25. #include <linux/firewire-constants.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/export.h>
  32. #include <asm/byteorder.h>
  33. #include "core.h"
  34. /*
  35. * Isochronous DMA context management
  36. */
  37. int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
  38. int page_count, enum dma_data_direction direction)
  39. {
  40. int i, j;
  41. dma_addr_t address;
  42. buffer->page_count = page_count;
  43. buffer->direction = direction;
  44. buffer->pages = kmalloc(page_count * sizeof(buffer->pages[0]),
  45. GFP_KERNEL);
  46. if (buffer->pages == NULL)
  47. goto out;
  48. for (i = 0; i < buffer->page_count; i++) {
  49. buffer->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
  50. if (buffer->pages[i] == NULL)
  51. goto out_pages;
  52. address = dma_map_page(card->device, buffer->pages[i],
  53. 0, PAGE_SIZE, direction);
  54. if (dma_mapping_error(card->device, address)) {
  55. __free_page(buffer->pages[i]);
  56. goto out_pages;
  57. }
  58. set_page_private(buffer->pages[i], address);
  59. }
  60. return 0;
  61. out_pages:
  62. for (j = 0; j < i; j++) {
  63. address = page_private(buffer->pages[j]);
  64. dma_unmap_page(card->device, address,
  65. PAGE_SIZE, direction);
  66. __free_page(buffer->pages[j]);
  67. }
  68. kfree(buffer->pages);
  69. out:
  70. buffer->pages = NULL;
  71. return -ENOMEM;
  72. }
  73. EXPORT_SYMBOL(fw_iso_buffer_init);
  74. int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma)
  75. {
  76. unsigned long uaddr;
  77. int i, err;
  78. uaddr = vma->vm_start;
  79. for (i = 0; i < buffer->page_count; i++) {
  80. err = vm_insert_page(vma, uaddr, buffer->pages[i]);
  81. if (err)
  82. return err;
  83. uaddr += PAGE_SIZE;
  84. }
  85. return 0;
  86. }
  87. void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer,
  88. struct fw_card *card)
  89. {
  90. int i;
  91. dma_addr_t address;
  92. for (i = 0; i < buffer->page_count; i++) {
  93. address = page_private(buffer->pages[i]);
  94. dma_unmap_page(card->device, address,
  95. PAGE_SIZE, buffer->direction);
  96. __free_page(buffer->pages[i]);
  97. }
  98. kfree(buffer->pages);
  99. buffer->pages = NULL;
  100. }
  101. EXPORT_SYMBOL(fw_iso_buffer_destroy);
  102. /* Convert DMA address to offset into virtually contiguous buffer. */
  103. size_t fw_iso_buffer_lookup(struct fw_iso_buffer *buffer, dma_addr_t completed)
  104. {
  105. int i;
  106. dma_addr_t address;
  107. ssize_t offset;
  108. for (i = 0; i < buffer->page_count; i++) {
  109. address = page_private(buffer->pages[i]);
  110. offset = (ssize_t)completed - (ssize_t)address;
  111. if (offset > 0 && offset <= PAGE_SIZE)
  112. return (i << PAGE_SHIFT) + offset;
  113. }
  114. return 0;
  115. }
  116. struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
  117. int type, int channel, int speed, size_t header_size,
  118. fw_iso_callback_t callback, void *callback_data)
  119. {
  120. struct fw_iso_context *ctx;
  121. ctx = card->driver->allocate_iso_context(card,
  122. type, channel, header_size);
  123. if (IS_ERR(ctx))
  124. return ctx;
  125. ctx->card = card;
  126. ctx->type = type;
  127. ctx->channel = channel;
  128. ctx->speed = speed;
  129. ctx->header_size = header_size;
  130. ctx->callback.sc = callback;
  131. ctx->callback_data = callback_data;
  132. return ctx;
  133. }
  134. EXPORT_SYMBOL(fw_iso_context_create);
  135. void fw_iso_context_destroy(struct fw_iso_context *ctx)
  136. {
  137. ctx->card->driver->free_iso_context(ctx);
  138. }
  139. EXPORT_SYMBOL(fw_iso_context_destroy);
  140. int fw_iso_context_start(struct fw_iso_context *ctx,
  141. int cycle, int sync, int tags)
  142. {
  143. return ctx->card->driver->start_iso(ctx, cycle, sync, tags);
  144. }
  145. EXPORT_SYMBOL(fw_iso_context_start);
  146. int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels)
  147. {
  148. return ctx->card->driver->set_iso_channels(ctx, channels);
  149. }
  150. int fw_iso_context_queue(struct fw_iso_context *ctx,
  151. struct fw_iso_packet *packet,
  152. struct fw_iso_buffer *buffer,
  153. unsigned long payload)
  154. {
  155. return ctx->card->driver->queue_iso(ctx, packet, buffer, payload);
  156. }
  157. EXPORT_SYMBOL(fw_iso_context_queue);
  158. void fw_iso_context_queue_flush(struct fw_iso_context *ctx)
  159. {
  160. ctx->card->driver->flush_queue_iso(ctx);
  161. }
  162. EXPORT_SYMBOL(fw_iso_context_queue_flush);
  163. int fw_iso_context_flush_completions(struct fw_iso_context *ctx)
  164. {
  165. return ctx->card->driver->flush_iso_completions(ctx);
  166. }
  167. EXPORT_SYMBOL(fw_iso_context_flush_completions);
  168. int fw_iso_context_stop(struct fw_iso_context *ctx)
  169. {
  170. return ctx->card->driver->stop_iso(ctx);
  171. }
  172. EXPORT_SYMBOL(fw_iso_context_stop);
  173. /*
  174. * Isochronous bus resource management (channels, bandwidth), client side
  175. */
  176. static int manage_bandwidth(struct fw_card *card, int irm_id, int generation,
  177. int bandwidth, bool allocate)
  178. {
  179. int try, new, old = allocate ? BANDWIDTH_AVAILABLE_INITIAL : 0;
  180. __be32 data[2];
  181. /*
  182. * On a 1394a IRM with low contention, try < 1 is enough.
  183. * On a 1394-1995 IRM, we need at least try < 2.
  184. * Let's just do try < 5.
  185. */
  186. for (try = 0; try < 5; try++) {
  187. new = allocate ? old - bandwidth : old + bandwidth;
  188. if (new < 0 || new > BANDWIDTH_AVAILABLE_INITIAL)
  189. return -EBUSY;
  190. data[0] = cpu_to_be32(old);
  191. data[1] = cpu_to_be32(new);
  192. switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
  193. irm_id, generation, SCODE_100,
  194. CSR_REGISTER_BASE + CSR_BANDWIDTH_AVAILABLE,
  195. data, 8)) {
  196. case RCODE_GENERATION:
  197. /* A generation change frees all bandwidth. */
  198. return allocate ? -EAGAIN : bandwidth;
  199. case RCODE_COMPLETE:
  200. if (be32_to_cpup(data) == old)
  201. return bandwidth;
  202. old = be32_to_cpup(data);
  203. /* Fall through. */
  204. }
  205. }
  206. return -EIO;
  207. }
  208. static int manage_channel(struct fw_card *card, int irm_id, int generation,
  209. u32 channels_mask, u64 offset, bool allocate)
  210. {
  211. __be32 bit, all, old;
  212. __be32 data[2];
  213. int channel, ret = -EIO, retry = 5;
  214. old = all = allocate ? cpu_to_be32(~0) : 0;
  215. for (channel = 0; channel < 32; channel++) {
  216. if (!(channels_mask & 1 << channel))
  217. continue;
  218. ret = -EBUSY;
  219. bit = cpu_to_be32(1 << (31 - channel));
  220. if ((old & bit) != (all & bit))
  221. continue;
  222. data[0] = old;
  223. data[1] = old ^ bit;
  224. switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
  225. irm_id, generation, SCODE_100,
  226. offset, data, 8)) {
  227. case RCODE_GENERATION:
  228. /* A generation change frees all channels. */
  229. return allocate ? -EAGAIN : channel;
  230. case RCODE_COMPLETE:
  231. if (data[0] == old)
  232. return channel;
  233. old = data[0];
  234. /* Is the IRM 1394a-2000 compliant? */
  235. if ((data[0] & bit) == (data[1] & bit))
  236. continue;
  237. /* 1394-1995 IRM, fall through to retry. */
  238. default:
  239. if (retry) {
  240. retry--;
  241. channel--;
  242. } else {
  243. ret = -EIO;
  244. }
  245. }
  246. }
  247. return ret;
  248. }
  249. static void deallocate_channel(struct fw_card *card, int irm_id,
  250. int generation, int channel)
  251. {
  252. u32 mask;
  253. u64 offset;
  254. mask = channel < 32 ? 1 << channel : 1 << (channel - 32);
  255. offset = channel < 32 ? CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI :
  256. CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO;
  257. manage_channel(card, irm_id, generation, mask, offset, false);
  258. }
  259. /**
  260. * fw_iso_resource_manage() - Allocate or deallocate a channel and/or bandwidth
  261. *
  262. * In parameters: card, generation, channels_mask, bandwidth, allocate
  263. * Out parameters: channel, bandwidth
  264. * This function blocks (sleeps) during communication with the IRM.
  265. *
  266. * Allocates or deallocates at most one channel out of channels_mask.
  267. * channels_mask is a bitfield with MSB for channel 63 and LSB for channel 0.
  268. * (Note, the IRM's CHANNELS_AVAILABLE is a big-endian bitfield with MSB for
  269. * channel 0 and LSB for channel 63.)
  270. * Allocates or deallocates as many bandwidth allocation units as specified.
  271. *
  272. * Returns channel < 0 if no channel was allocated or deallocated.
  273. * Returns bandwidth = 0 if no bandwidth was allocated or deallocated.
  274. *
  275. * If generation is stale, deallocations succeed but allocations fail with
  276. * channel = -EAGAIN.
  277. *
  278. * If channel allocation fails, no bandwidth will be allocated either.
  279. * If bandwidth allocation fails, no channel will be allocated either.
  280. * But deallocations of channel and bandwidth are tried independently
  281. * of each other's success.
  282. */
  283. void fw_iso_resource_manage(struct fw_card *card, int generation,
  284. u64 channels_mask, int *channel, int *bandwidth,
  285. bool allocate)
  286. {
  287. u32 channels_hi = channels_mask; /* channels 31...0 */
  288. u32 channels_lo = channels_mask >> 32; /* channels 63...32 */
  289. int irm_id, ret, c = -EINVAL;
  290. spin_lock_irq(&card->lock);
  291. irm_id = card->irm_node->node_id;
  292. spin_unlock_irq(&card->lock);
  293. if (channels_hi)
  294. c = manage_channel(card, irm_id, generation, channels_hi,
  295. CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI,
  296. allocate);
  297. if (channels_lo && c < 0) {
  298. c = manage_channel(card, irm_id, generation, channels_lo,
  299. CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO,
  300. allocate);
  301. if (c >= 0)
  302. c += 32;
  303. }
  304. *channel = c;
  305. if (allocate && channels_mask != 0 && c < 0)
  306. *bandwidth = 0;
  307. if (*bandwidth == 0)
  308. return;
  309. ret = manage_bandwidth(card, irm_id, generation, *bandwidth, allocate);
  310. if (ret < 0)
  311. *bandwidth = 0;
  312. if (allocate && ret < 0) {
  313. if (c >= 0)
  314. deallocate_channel(card, irm_id, generation, c);
  315. *channel = ret;
  316. }
  317. }
  318. EXPORT_SYMBOL(fw_iso_resource_manage);