core-iso.c 9.6 KB

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