PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/infiniband/hw/mlx5/mem.c

https://bitbucket.org/alfredchen/linux-gc
C | 248 lines | 165 code | 27 blank | 56 comment | 26 complexity | a3855aefa5f36457c3babc79c362380e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/module.h>
  33. #include <rdma/ib_umem.h>
  34. #include <rdma/ib_umem_odp.h>
  35. #include "mlx5_ib.h"
  36. /* @umem: umem object to scan
  37. * @addr: ib virtual address requested by the user
  38. * @max_page_shift: high limit for page_shift - 0 means no limit
  39. * @count: number of PAGE_SIZE pages covered by umem
  40. * @shift: page shift for the compound pages found in the region
  41. * @ncont: number of compund pages
  42. * @order: log2 of the number of compound pages
  43. */
  44. void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr,
  45. unsigned long max_page_shift,
  46. int *count, int *shift,
  47. int *ncont, int *order)
  48. {
  49. unsigned long tmp;
  50. unsigned long m;
  51. int i, k;
  52. u64 base = 0;
  53. int p = 0;
  54. int skip;
  55. int mask;
  56. u64 len;
  57. u64 pfn;
  58. struct scatterlist *sg;
  59. int entry;
  60. unsigned long page_shift = ilog2(umem->page_size);
  61. /* With ODP we must always match OS page size. */
  62. if (umem->odp_data) {
  63. *count = ib_umem_page_count(umem);
  64. *shift = PAGE_SHIFT;
  65. *ncont = *count;
  66. if (order)
  67. *order = ilog2(roundup_pow_of_two(*count));
  68. return;
  69. }
  70. addr = addr >> page_shift;
  71. tmp = (unsigned long)addr;
  72. m = find_first_bit(&tmp, BITS_PER_LONG);
  73. if (max_page_shift)
  74. m = min_t(unsigned long, max_page_shift - page_shift, m);
  75. skip = 1 << m;
  76. mask = skip - 1;
  77. i = 0;
  78. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  79. len = sg_dma_len(sg) >> page_shift;
  80. pfn = sg_dma_address(sg) >> page_shift;
  81. for (k = 0; k < len; k++) {
  82. if (!(i & mask)) {
  83. tmp = (unsigned long)pfn;
  84. m = min_t(unsigned long, m, find_first_bit(&tmp, BITS_PER_LONG));
  85. skip = 1 << m;
  86. mask = skip - 1;
  87. base = pfn;
  88. p = 0;
  89. } else {
  90. if (base + p != pfn) {
  91. tmp = (unsigned long)p;
  92. m = find_first_bit(&tmp, BITS_PER_LONG);
  93. skip = 1 << m;
  94. mask = skip - 1;
  95. base = pfn;
  96. p = 0;
  97. }
  98. }
  99. p++;
  100. i++;
  101. }
  102. }
  103. if (i) {
  104. m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
  105. if (order)
  106. *order = ilog2(roundup_pow_of_two(i) >> m);
  107. *ncont = DIV_ROUND_UP(i, (1 << m));
  108. } else {
  109. m = 0;
  110. if (order)
  111. *order = 0;
  112. *ncont = 0;
  113. }
  114. *shift = page_shift + m;
  115. *count = i;
  116. }
  117. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  118. static u64 umem_dma_to_mtt(dma_addr_t umem_dma)
  119. {
  120. u64 mtt_entry = umem_dma & ODP_DMA_ADDR_MASK;
  121. if (umem_dma & ODP_READ_ALLOWED_BIT)
  122. mtt_entry |= MLX5_IB_MTT_READ;
  123. if (umem_dma & ODP_WRITE_ALLOWED_BIT)
  124. mtt_entry |= MLX5_IB_MTT_WRITE;
  125. return mtt_entry;
  126. }
  127. #endif
  128. /*
  129. * Populate the given array with bus addresses from the umem.
  130. *
  131. * dev - mlx5_ib device
  132. * umem - umem to use to fill the pages
  133. * page_shift - determines the page size used in the resulting array
  134. * offset - offset into the umem to start from,
  135. * only implemented for ODP umems
  136. * num_pages - total number of pages to fill
  137. * pas - bus addresses array to fill
  138. * access_flags - access flags to set on all present pages.
  139. use enum mlx5_ib_mtt_access_flags for this.
  140. */
  141. void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  142. int page_shift, size_t offset, size_t num_pages,
  143. __be64 *pas, int access_flags)
  144. {
  145. unsigned long umem_page_shift = ilog2(umem->page_size);
  146. int shift = page_shift - umem_page_shift;
  147. int mask = (1 << shift) - 1;
  148. int i, k, idx;
  149. u64 cur = 0;
  150. u64 base;
  151. int len;
  152. struct scatterlist *sg;
  153. int entry;
  154. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  155. const bool odp = umem->odp_data != NULL;
  156. if (odp) {
  157. WARN_ON(shift != 0);
  158. WARN_ON(access_flags != (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE));
  159. for (i = 0; i < num_pages; ++i) {
  160. dma_addr_t pa = umem->odp_data->dma_list[offset + i];
  161. pas[i] = cpu_to_be64(umem_dma_to_mtt(pa));
  162. }
  163. return;
  164. }
  165. #endif
  166. i = 0;
  167. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  168. len = sg_dma_len(sg) >> umem_page_shift;
  169. base = sg_dma_address(sg);
  170. /* Skip elements below offset */
  171. if (i + len < offset << shift) {
  172. i += len;
  173. continue;
  174. }
  175. /* Skip pages below offset */
  176. if (i < offset << shift) {
  177. k = (offset << shift) - i;
  178. i = offset << shift;
  179. } else {
  180. k = 0;
  181. }
  182. for (; k < len; k++) {
  183. if (!(i & mask)) {
  184. cur = base + (k << umem_page_shift);
  185. cur |= access_flags;
  186. idx = (i >> shift) - offset;
  187. pas[idx] = cpu_to_be64(cur);
  188. mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
  189. i >> shift, be64_to_cpu(pas[idx]));
  190. }
  191. i++;
  192. /* Stop after num_pages reached */
  193. if (i >> shift >= offset + num_pages)
  194. return;
  195. }
  196. }
  197. }
  198. void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  199. int page_shift, __be64 *pas, int access_flags)
  200. {
  201. return __mlx5_ib_populate_pas(dev, umem, page_shift, 0,
  202. ib_umem_num_pages(umem), pas,
  203. access_flags);
  204. }
  205. int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
  206. {
  207. u64 page_size;
  208. u64 page_mask;
  209. u64 off_size;
  210. u64 off_mask;
  211. u64 buf_off;
  212. page_size = (u64)1 << page_shift;
  213. page_mask = page_size - 1;
  214. buf_off = addr & page_mask;
  215. off_size = page_size >> 6;
  216. off_mask = off_size - 1;
  217. if (buf_off & off_mask)
  218. return -EINVAL;
  219. *offset = buf_off >> ilog2(off_size);
  220. return 0;
  221. }