fw-iso.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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-constants.h>
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/vmalloc.h>
  29. #include "fw-topology.h"
  30. #include "fw-transaction.h"
  31. /*
  32. * Isochronous DMA context management
  33. */
  34. int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
  35. int page_count, enum dma_data_direction direction)
  36. {
  37. int i, j;
  38. dma_addr_t address;
  39. buffer->page_count = page_count;
  40. buffer->direction = direction;
  41. buffer->pages = kmalloc(page_count * sizeof(buffer->pages[0]),
  42. GFP_KERNEL);
  43. if (buffer->pages == NULL)
  44. goto out;
  45. for (i = 0; i < buffer->page_count; i++) {
  46. buffer->pages[i] = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
  47. if (buffer->pages[i] == NULL)
  48. goto out_pages;
  49. address = dma_map_page(card->device, buffer->pages[i],
  50. 0, PAGE_SIZE, direction);
  51. if (dma_mapping_error(card->device, address)) {
  52. __free_page(buffer->pages[i]);
  53. goto out_pages;
  54. }
  55. set_page_private(buffer->pages[i], address);
  56. }
  57. return 0;
  58. out_pages:
  59. for (j = 0; j < i; j++) {
  60. address = page_private(buffer->pages[j]);
  61. dma_unmap_page(card->device, address,
  62. PAGE_SIZE, DMA_TO_DEVICE);
  63. __free_page(buffer->pages[j]);
  64. }
  65. kfree(buffer->pages);
  66. out:
  67. buffer->pages = NULL;
  68. return -ENOMEM;
  69. }
  70. int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma)
  71. {
  72. unsigned long uaddr;
  73. int i, err;
  74. uaddr = vma->vm_start;
  75. for (i = 0; i < buffer->page_count; i++) {
  76. err = vm_insert_page(vma, uaddr, buffer->pages[i]);
  77. if (err)
  78. return err;
  79. uaddr += PAGE_SIZE;
  80. }
  81. return 0;
  82. }
  83. void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer,
  84. struct fw_card *card)
  85. {
  86. int i;
  87. dma_addr_t address;
  88. for (i = 0; i < buffer->page_count; i++) {
  89. address = page_private(buffer->pages[i]);
  90. dma_unmap_page(card->device, address,
  91. PAGE_SIZE, DMA_TO_DEVICE);
  92. __free_page(buffer->pages[i]);
  93. }
  94. kfree(buffer->pages);
  95. buffer->pages = NULL;
  96. }
  97. struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
  98. int type, int channel, int speed, size_t header_size,
  99. fw_iso_callback_t callback, void *callback_data)
  100. {
  101. struct fw_iso_context *ctx;
  102. ctx = card->driver->allocate_iso_context(card,
  103. type, channel, header_size);
  104. if (IS_ERR(ctx))
  105. return ctx;
  106. ctx->card = card;
  107. ctx->type = type;
  108. ctx->channel = channel;
  109. ctx->speed = speed;
  110. ctx->header_size = header_size;
  111. ctx->callback = callback;
  112. ctx->callback_data = callback_data;
  113. return ctx;
  114. }
  115. void fw_iso_context_destroy(struct fw_iso_context *ctx)
  116. {
  117. struct fw_card *card = ctx->card;
  118. card->driver->free_iso_context(ctx);
  119. }
  120. int fw_iso_context_start(struct fw_iso_context *ctx,
  121. int cycle, int sync, int tags)
  122. {
  123. return ctx->card->driver->start_iso(ctx, cycle, sync, tags);
  124. }
  125. int fw_iso_context_queue(struct fw_iso_context *ctx,
  126. struct fw_iso_packet *packet,
  127. struct fw_iso_buffer *buffer,
  128. unsigned long payload)
  129. {
  130. struct fw_card *card = ctx->card;
  131. return card->driver->queue_iso(ctx, packet, buffer, payload);
  132. }
  133. int fw_iso_context_stop(struct fw_iso_context *ctx)
  134. {
  135. return ctx->card->driver->stop_iso(ctx);
  136. }
  137. /*
  138. * Isochronous bus resource management (channels, bandwidth), client side
  139. */
  140. static int manage_bandwidth(struct fw_card *card, int irm_id, int generation,
  141. int bandwidth, bool allocate)
  142. {
  143. __be32 data[2];
  144. int try, new, old = allocate ? BANDWIDTH_AVAILABLE_INITIAL : 0;
  145. /*
  146. * On a 1394a IRM with low contention, try < 1 is enough.
  147. * On a 1394-1995 IRM, we need at least try < 2.
  148. * Let's just do try < 5.
  149. */
  150. for (try = 0; try < 5; try++) {
  151. new = allocate ? old - bandwidth : old + bandwidth;
  152. if (new < 0 || new > BANDWIDTH_AVAILABLE_INITIAL)
  153. break;
  154. data[0] = cpu_to_be32(old);
  155. data[1] = cpu_to_be32(new);
  156. switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
  157. irm_id, generation, SCODE_100,
  158. CSR_REGISTER_BASE + CSR_BANDWIDTH_AVAILABLE,
  159. data, sizeof(data))) {
  160. case RCODE_GENERATION:
  161. /* A generation change frees all bandwidth. */
  162. return allocate ? -EAGAIN : bandwidth;
  163. case RCODE_COMPLETE:
  164. if (be32_to_cpup(data) == old)
  165. return bandwidth;
  166. old = be32_to_cpup(data);
  167. /* Fall through. */
  168. }
  169. }
  170. return -EIO;
  171. }
  172. static int manage_channel(struct fw_card *card, int irm_id, int generation,
  173. u32 channels_mask, u64 offset, bool allocate)
  174. {
  175. __be32 data[2], c, all, old;
  176. int i, retry = 5;
  177. old = all = allocate ? cpu_to_be32(~0) : 0;
  178. for (i = 0; i < 32; i++) {
  179. if (!(channels_mask & 1 << i))
  180. continue;
  181. c = cpu_to_be32(1 << (31 - i));
  182. if ((old & c) != (all & c))
  183. continue;
  184. data[0] = old;
  185. data[1] = old ^ c;
  186. switch (fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
  187. irm_id, generation, SCODE_100,
  188. offset, data, sizeof(data))) {
  189. case RCODE_GENERATION:
  190. /* A generation change frees all channels. */
  191. return allocate ? -EAGAIN : i;
  192. case RCODE_COMPLETE:
  193. if (data[0] == old)
  194. return i;
  195. old = data[0];
  196. /* Is the IRM 1394a-2000 compliant? */
  197. if ((data[0] & c) == (data[1] & c))
  198. continue;
  199. /* 1394-1995 IRM, fall through to retry. */
  200. default:
  201. if (retry--)
  202. i--;
  203. }
  204. }
  205. return -EIO;
  206. }
  207. static void deallocate_channel(struct fw_card *card, int irm_id,
  208. int generation, int channel)
  209. {
  210. u32 mask;
  211. u64 offset;
  212. mask = channel < 32 ? 1 << channel : 1 << (channel - 32);
  213. offset = channel < 32 ? CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI :
  214. CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO;
  215. manage_channel(card, irm_id, generation, mask, offset, false);
  216. }
  217. /**
  218. * fw_iso_resource_manage - Allocate or deallocate a channel and/or bandwidth
  219. *
  220. * In parameters: card, generation, channels_mask, bandwidth, allocate
  221. * Out parameters: channel, bandwidth
  222. * This function blocks (sleeps) during communication with the IRM.
  223. *
  224. * Allocates or deallocates at most one channel out of channels_mask.
  225. * channels_mask is a bitfield with MSB for channel 63 and LSB for channel 0.
  226. * (Note, the IRM's CHANNELS_AVAILABLE is a big-endian bitfield with MSB for
  227. * channel 0 and LSB for channel 63.)
  228. * Allocates or deallocates as many bandwidth allocation units as specified.
  229. *
  230. * Returns channel < 0 if no channel was allocated or deallocated.
  231. * Returns bandwidth = 0 if no bandwidth was allocated or deallocated.
  232. *
  233. * If generation is stale, deallocations succeed but allocations fail with
  234. * channel = -EAGAIN.
  235. *
  236. * If channel allocation fails, no bandwidth will be allocated either.
  237. * If bandwidth allocation fails, no channel will be allocated either.
  238. * But deallocations of channel and bandwidth are tried independently
  239. * of each other's success.
  240. */
  241. void fw_iso_resource_manage(struct fw_card *card, int generation,
  242. u64 channels_mask, int *channel, int *bandwidth,
  243. bool allocate)
  244. {
  245. u32 channels_hi = channels_mask; /* channels 31...0 */
  246. u32 channels_lo = channels_mask >> 32; /* channels 63...32 */
  247. int irm_id, ret, c = -EINVAL;
  248. spin_lock_irq(&card->lock);
  249. irm_id = card->irm_node->node_id;
  250. spin_unlock_irq(&card->lock);
  251. if (channels_hi)
  252. c = manage_channel(card, irm_id, generation, channels_hi,
  253. CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_HI, allocate);
  254. if (channels_lo && c < 0) {
  255. c = manage_channel(card, irm_id, generation, channels_lo,
  256. CSR_REGISTER_BASE + CSR_CHANNELS_AVAILABLE_LO, allocate);
  257. if (c >= 0)
  258. c += 32;
  259. }
  260. *channel = c;
  261. if (allocate && channels_mask != 0 && c < 0)
  262. *bandwidth = 0;
  263. if (*bandwidth == 0)
  264. return;
  265. ret = manage_bandwidth(card, irm_id, generation, *bandwidth, allocate);
  266. if (ret < 0)
  267. *bandwidth = 0;
  268. if (allocate && ret < 0 && c >= 0) {
  269. deallocate_channel(card, irm_id, generation, c);
  270. *channel = ret;
  271. }
  272. }