dma-if.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * ISHTP DMA I/F functions
  3. *
  4. * Copyright (c) 2003-2016, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/sched.h>
  18. #include <linux/wait.h>
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include "ishtp-dev.h"
  22. #include "client.h"
  23. /**
  24. * ishtp_cl_alloc_dma_buf() - Allocate DMA RX and TX buffer
  25. * @dev: ishtp device
  26. *
  27. * Allocate RX and TX DMA buffer once during bus setup.
  28. * It allocates 1MB, RX and TX DMA buffer, which are divided
  29. * into slots.
  30. */
  31. void ishtp_cl_alloc_dma_buf(struct ishtp_device *dev)
  32. {
  33. dma_addr_t h;
  34. if (dev->ishtp_host_dma_tx_buf)
  35. return;
  36. dev->ishtp_host_dma_tx_buf_size = 1024*1024;
  37. dev->ishtp_host_dma_rx_buf_size = 1024*1024;
  38. /* Allocate Tx buffer and init usage bitmap */
  39. dev->ishtp_host_dma_tx_buf = dma_alloc_coherent(dev->devc,
  40. dev->ishtp_host_dma_tx_buf_size,
  41. &h, GFP_KERNEL);
  42. if (dev->ishtp_host_dma_tx_buf)
  43. dev->ishtp_host_dma_tx_buf_phys = h;
  44. dev->ishtp_dma_num_slots = dev->ishtp_host_dma_tx_buf_size /
  45. DMA_SLOT_SIZE;
  46. dev->ishtp_dma_tx_map = kcalloc(dev->ishtp_dma_num_slots,
  47. sizeof(uint8_t),
  48. GFP_KERNEL);
  49. spin_lock_init(&dev->ishtp_dma_tx_lock);
  50. /* Allocate Rx buffer */
  51. dev->ishtp_host_dma_rx_buf = dma_alloc_coherent(dev->devc,
  52. dev->ishtp_host_dma_rx_buf_size,
  53. &h, GFP_KERNEL);
  54. if (dev->ishtp_host_dma_rx_buf)
  55. dev->ishtp_host_dma_rx_buf_phys = h;
  56. }
  57. /**
  58. * ishtp_cl_free_dma_buf() - Free DMA RX and TX buffer
  59. * @dev: ishtp device
  60. *
  61. * Free DMA buffer when all clients are released. This is
  62. * only happens during error path in ISH built in driver
  63. * model
  64. */
  65. void ishtp_cl_free_dma_buf(struct ishtp_device *dev)
  66. {
  67. dma_addr_t h;
  68. if (dev->ishtp_host_dma_tx_buf) {
  69. h = dev->ishtp_host_dma_tx_buf_phys;
  70. dma_free_coherent(dev->devc, dev->ishtp_host_dma_tx_buf_size,
  71. dev->ishtp_host_dma_tx_buf, h);
  72. }
  73. if (dev->ishtp_host_dma_rx_buf) {
  74. h = dev->ishtp_host_dma_rx_buf_phys;
  75. dma_free_coherent(dev->devc, dev->ishtp_host_dma_rx_buf_size,
  76. dev->ishtp_host_dma_rx_buf, h);
  77. }
  78. kfree(dev->ishtp_dma_tx_map);
  79. dev->ishtp_host_dma_tx_buf = NULL;
  80. dev->ishtp_host_dma_rx_buf = NULL;
  81. dev->ishtp_dma_tx_map = NULL;
  82. }
  83. /*
  84. * ishtp_cl_get_dma_send_buf() - Get a DMA memory slot
  85. * @dev: ishtp device
  86. * @size: Size of memory to get
  87. *
  88. * Find and return free address of "size" bytes in dma tx buffer.
  89. * the function will mark this address as "in-used" memory.
  90. *
  91. * Return: NULL when no free buffer else a buffer to copy
  92. */
  93. void *ishtp_cl_get_dma_send_buf(struct ishtp_device *dev,
  94. uint32_t size)
  95. {
  96. unsigned long flags;
  97. int i, j, free;
  98. /* additional slot is needed if there is rem */
  99. int required_slots = (size / DMA_SLOT_SIZE)
  100. + 1 * (size % DMA_SLOT_SIZE != 0);
  101. spin_lock_irqsave(&dev->ishtp_dma_tx_lock, flags);
  102. for (i = 0; i <= (dev->ishtp_dma_num_slots - required_slots); i++) {
  103. free = 1;
  104. for (j = 0; j < required_slots; j++)
  105. if (dev->ishtp_dma_tx_map[i+j]) {
  106. free = 0;
  107. i += j;
  108. break;
  109. }
  110. if (free) {
  111. /* mark memory as "caught" */
  112. for (j = 0; j < required_slots; j++)
  113. dev->ishtp_dma_tx_map[i+j] = 1;
  114. spin_unlock_irqrestore(&dev->ishtp_dma_tx_lock, flags);
  115. return (i * DMA_SLOT_SIZE) +
  116. (unsigned char *)dev->ishtp_host_dma_tx_buf;
  117. }
  118. }
  119. spin_unlock_irqrestore(&dev->ishtp_dma_tx_lock, flags);
  120. dev_err(dev->devc, "No free DMA buffer to send msg\n");
  121. return NULL;
  122. }
  123. /*
  124. * ishtp_cl_release_dma_acked_mem() - Release DMA memory slot
  125. * @dev: ishtp device
  126. * @msg_addr: message address of slot
  127. * @size: Size of memory to get
  128. *
  129. * Release_dma_acked_mem - returnes the acked memory to free list.
  130. * (from msg_addr, size bytes long)
  131. */
  132. void ishtp_cl_release_dma_acked_mem(struct ishtp_device *dev,
  133. void *msg_addr,
  134. uint8_t size)
  135. {
  136. unsigned long flags;
  137. int acked_slots = (size / DMA_SLOT_SIZE)
  138. + 1 * (size % DMA_SLOT_SIZE != 0);
  139. int i, j;
  140. if ((msg_addr - dev->ishtp_host_dma_tx_buf) % DMA_SLOT_SIZE) {
  141. dev_err(dev->devc, "Bad DMA Tx ack address\n");
  142. return;
  143. }
  144. i = (msg_addr - dev->ishtp_host_dma_tx_buf) / DMA_SLOT_SIZE;
  145. spin_lock_irqsave(&dev->ishtp_dma_tx_lock, flags);
  146. for (j = 0; j < acked_slots; j++) {
  147. if ((i + j) >= dev->ishtp_dma_num_slots ||
  148. !dev->ishtp_dma_tx_map[i+j]) {
  149. /* no such slot, or memory is already free */
  150. spin_unlock_irqrestore(&dev->ishtp_dma_tx_lock, flags);
  151. dev_err(dev->devc, "Bad DMA Tx ack address\n");
  152. return;
  153. }
  154. dev->ishtp_dma_tx_map[i+j] = 0;
  155. }
  156. spin_unlock_irqrestore(&dev->ishtp_dma_tx_lock, flags);
  157. }