zip_device.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /***********************license start************************************
  2. * Copyright (c) 2003-2017 Cavium, Inc.
  3. * All rights reserved.
  4. *
  5. * License: one of 'Cavium License' or 'GNU General Public License Version 2'
  6. *
  7. * This file is provided under the terms of the Cavium License (see below)
  8. * or under the terms of GNU General Public License, Version 2, as
  9. * published by the Free Software Foundation. When using or redistributing
  10. * this file, you may do so under either license.
  11. *
  12. * Cavium License: Redistribution and use in source and binary forms, with
  13. * or without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * * Neither the name of Cavium Inc. nor the names of its contributors may be
  25. * used to endorse or promote products derived from this software without
  26. * specific prior written permission.
  27. *
  28. * This Software, including technical data, may be subject to U.S. export
  29. * control laws, including the U.S. Export Administration Act and its
  30. * associated regulations, and may be subject to export or import
  31. * regulations in other countries.
  32. *
  33. * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
  34. * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
  35. * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
  36. * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
  37. * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
  38. * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
  39. * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
  40. * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
  41. * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
  42. * ENTIRE RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
  43. * WITH YOU.
  44. ***********************license end**************************************/
  45. #include "common.h"
  46. #include "zip_deflate.h"
  47. /**
  48. * zip_cmd_queue_consumed - Calculates the space consumed in the command queue.
  49. *
  50. * @zip_dev: Pointer to zip device structure
  51. * @queue: Queue number
  52. *
  53. * Return: Bytes consumed in the command queue buffer.
  54. */
  55. static inline u32 zip_cmd_queue_consumed(struct zip_device *zip_dev, int queue)
  56. {
  57. return ((zip_dev->iq[queue].sw_head - zip_dev->iq[queue].sw_tail) *
  58. sizeof(u64 *));
  59. }
  60. /**
  61. * zip_load_instr - Submits the instruction into the ZIP command queue
  62. * @instr: Pointer to the instruction to be submitted
  63. * @zip_dev: Pointer to ZIP device structure to which the instruction is to
  64. * be submitted
  65. *
  66. * This function copies the ZIP instruction to the command queue and rings the
  67. * doorbell to notify the engine of the instruction submission. The command
  68. * queue is maintained in a circular fashion. When there is space for exactly
  69. * one instruction in the queue, next chunk pointer of the queue is made to
  70. * point to the head of the queue, thus maintaining a circular queue.
  71. *
  72. * Return: Queue number to which the instruction was submitted
  73. */
  74. u32 zip_load_instr(union zip_inst_s *instr,
  75. struct zip_device *zip_dev)
  76. {
  77. union zip_quex_doorbell dbell;
  78. u32 queue = 0;
  79. u32 consumed = 0;
  80. u64 *ncb_ptr = NULL;
  81. union zip_nptr_s ncp;
  82. /*
  83. * Distribute the instructions between the enabled queues based on
  84. * the CPU id.
  85. */
  86. if (raw_smp_processor_id() % 2 == 0)
  87. queue = 0;
  88. else
  89. queue = 1;
  90. zip_dbg("CPU Core: %d Queue number:%d", raw_smp_processor_id(), queue);
  91. /* Take cmd buffer lock */
  92. spin_lock(&zip_dev->iq[queue].lock);
  93. /*
  94. * Command Queue implementation
  95. * 1. If there is place for new instructions, push the cmd at sw_head.
  96. * 2. If there is place for exactly one instruction, push the new cmd
  97. * at the sw_head. Make sw_head point to the sw_tail to make it
  98. * circular. Write sw_head's physical address to the "Next-Chunk
  99. * Buffer Ptr" to make it cmd_hw_tail.
  100. * 3. Ring the door bell.
  101. */
  102. zip_dbg("sw_head : %lx", zip_dev->iq[queue].sw_head);
  103. zip_dbg("sw_tail : %lx", zip_dev->iq[queue].sw_tail);
  104. consumed = zip_cmd_queue_consumed(zip_dev, queue);
  105. /* Check if there is space to push just one cmd */
  106. if ((consumed + 128) == (ZIP_CMD_QBUF_SIZE - 8)) {
  107. zip_dbg("Cmd queue space available for single command");
  108. /* Space for one cmd, pust it and make it circular queue */
  109. memcpy((u8 *)zip_dev->iq[queue].sw_head, (u8 *)instr,
  110. sizeof(union zip_inst_s));
  111. zip_dev->iq[queue].sw_head += 16; /* 16 64_bit words = 128B */
  112. /* Now, point the "Next-Chunk Buffer Ptr" to sw_head */
  113. ncb_ptr = zip_dev->iq[queue].sw_head;
  114. zip_dbg("ncb addr :0x%lx sw_head addr :0x%lx",
  115. ncb_ptr, zip_dev->iq[queue].sw_head - 16);
  116. /* Using Circular command queue */
  117. zip_dev->iq[queue].sw_head = zip_dev->iq[queue].sw_tail;
  118. /* Mark this buffer for free */
  119. zip_dev->iq[queue].free_flag = 1;
  120. /* Write new chunk buffer address at "Next-Chunk Buffer Ptr" */
  121. ncp.u_reg64 = 0ull;
  122. ncp.s.addr = __pa(zip_dev->iq[queue].sw_head);
  123. *ncb_ptr = ncp.u_reg64;
  124. zip_dbg("*ncb_ptr :0x%lx sw_head[phys] :0x%lx",
  125. *ncb_ptr, __pa(zip_dev->iq[queue].sw_head));
  126. zip_dev->iq[queue].pend_cnt++;
  127. } else {
  128. zip_dbg("Enough space is available for commands");
  129. /* Push this cmd to cmd queue buffer */
  130. memcpy((u8 *)zip_dev->iq[queue].sw_head, (u8 *)instr,
  131. sizeof(union zip_inst_s));
  132. zip_dev->iq[queue].sw_head += 16; /* 16 64_bit words = 128B */
  133. zip_dev->iq[queue].pend_cnt++;
  134. }
  135. zip_dbg("sw_head :0x%lx sw_tail :0x%lx hw_tail :0x%lx",
  136. zip_dev->iq[queue].sw_head, zip_dev->iq[queue].sw_tail,
  137. zip_dev->iq[queue].hw_tail);
  138. zip_dbg(" Pushed the new cmd : pend_cnt : %d",
  139. zip_dev->iq[queue].pend_cnt);
  140. /* Ring the doorbell */
  141. dbell.u_reg64 = 0ull;
  142. dbell.s.dbell_cnt = 1;
  143. zip_reg_write(dbell.u_reg64,
  144. (zip_dev->reg_base + ZIP_QUEX_DOORBELL(queue)));
  145. /* Unlock cmd buffer lock */
  146. spin_unlock(&zip_dev->iq[queue].lock);
  147. return queue;
  148. }
  149. /**
  150. * zip_update_cmd_bufs - Updates the queue statistics after posting the
  151. * instruction
  152. * @zip_dev: Pointer to zip device structure
  153. * @queue: Queue number
  154. */
  155. void zip_update_cmd_bufs(struct zip_device *zip_dev, u32 queue)
  156. {
  157. /* Take cmd buffer lock */
  158. spin_lock(&zip_dev->iq[queue].lock);
  159. /* Check if the previous buffer can be freed */
  160. if (zip_dev->iq[queue].free_flag == 1) {
  161. zip_dbg("Free flag. Free cmd buffer, adjust sw head and tail");
  162. /* Reset the free flag */
  163. zip_dev->iq[queue].free_flag = 0;
  164. /* Point the hw_tail to start of the new chunk buffer */
  165. zip_dev->iq[queue].hw_tail = zip_dev->iq[queue].sw_head;
  166. } else {
  167. zip_dbg("Free flag not set. increment hw tail");
  168. zip_dev->iq[queue].hw_tail += 16; /* 16 64_bit words = 128B */
  169. }
  170. zip_dev->iq[queue].done_cnt++;
  171. zip_dev->iq[queue].pend_cnt--;
  172. zip_dbg("sw_head :0x%lx sw_tail :0x%lx hw_tail :0x%lx",
  173. zip_dev->iq[queue].sw_head, zip_dev->iq[queue].sw_tail,
  174. zip_dev->iq[queue].hw_tail);
  175. zip_dbg(" Got CC : pend_cnt : %d\n", zip_dev->iq[queue].pend_cnt);
  176. spin_unlock(&zip_dev->iq[queue].lock);
  177. }