dma-if.c 4.6 KB

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