mem.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. u64 base = ~0, p = 0;
  52. u64 len, pfn;
  53. int i = 0;
  54. struct scatterlist *sg;
  55. int entry;
  56. unsigned long page_shift = umem->page_shift;
  57. if (umem->odp_data) {
  58. *ncont = ib_umem_page_count(umem);
  59. *count = *ncont << (page_shift - PAGE_SHIFT);
  60. *shift = page_shift;
  61. if (order)
  62. *order = ilog2(roundup_pow_of_two(*ncont));
  63. return;
  64. }
  65. addr = addr >> page_shift;
  66. tmp = (unsigned long)addr;
  67. m = find_first_bit(&tmp, BITS_PER_LONG);
  68. if (max_page_shift)
  69. m = min_t(unsigned long, max_page_shift - page_shift, m);
  70. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  71. len = sg_dma_len(sg) >> page_shift;
  72. pfn = sg_dma_address(sg) >> page_shift;
  73. if (base + p != pfn) {
  74. /* If either the offset or the new
  75. * base are unaligned update m
  76. */
  77. tmp = (unsigned long)(pfn | p);
  78. if (!IS_ALIGNED(tmp, 1 << m))
  79. m = find_first_bit(&tmp, BITS_PER_LONG);
  80. base = pfn;
  81. p = 0;
  82. }
  83. p += len;
  84. i += len;
  85. }
  86. if (i) {
  87. m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
  88. if (order)
  89. *order = ilog2(roundup_pow_of_two(i) >> m);
  90. *ncont = DIV_ROUND_UP(i, (1 << m));
  91. } else {
  92. m = 0;
  93. if (order)
  94. *order = 0;
  95. *ncont = 0;
  96. }
  97. *shift = page_shift + m;
  98. *count = i;
  99. }
  100. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  101. static u64 umem_dma_to_mtt(dma_addr_t umem_dma)
  102. {
  103. u64 mtt_entry = umem_dma & ODP_DMA_ADDR_MASK;
  104. if (umem_dma & ODP_READ_ALLOWED_BIT)
  105. mtt_entry |= MLX5_IB_MTT_READ;
  106. if (umem_dma & ODP_WRITE_ALLOWED_BIT)
  107. mtt_entry |= MLX5_IB_MTT_WRITE;
  108. return mtt_entry;
  109. }
  110. #endif
  111. /*
  112. * Populate the given array with bus addresses from the umem.
  113. *
  114. * dev - mlx5_ib device
  115. * umem - umem to use to fill the pages
  116. * page_shift - determines the page size used in the resulting array
  117. * offset - offset into the umem to start from,
  118. * only implemented for ODP umems
  119. * num_pages - total number of pages to fill
  120. * pas - bus addresses array to fill
  121. * access_flags - access flags to set on all present pages.
  122. use enum mlx5_ib_mtt_access_flags for this.
  123. */
  124. void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  125. int page_shift, size_t offset, size_t num_pages,
  126. __be64 *pas, int access_flags)
  127. {
  128. unsigned long umem_page_shift = umem->page_shift;
  129. int shift = page_shift - umem_page_shift;
  130. int mask = (1 << shift) - 1;
  131. int i, k, idx;
  132. u64 cur = 0;
  133. u64 base;
  134. int len;
  135. struct scatterlist *sg;
  136. int entry;
  137. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  138. const bool odp = umem->odp_data != NULL;
  139. if (odp) {
  140. WARN_ON(shift != 0);
  141. WARN_ON(access_flags != (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE));
  142. for (i = 0; i < num_pages; ++i) {
  143. dma_addr_t pa = umem->odp_data->dma_list[offset + i];
  144. pas[i] = cpu_to_be64(umem_dma_to_mtt(pa));
  145. }
  146. return;
  147. }
  148. #endif
  149. i = 0;
  150. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  151. len = sg_dma_len(sg) >> umem_page_shift;
  152. base = sg_dma_address(sg);
  153. /* Skip elements below offset */
  154. if (i + len < offset << shift) {
  155. i += len;
  156. continue;
  157. }
  158. /* Skip pages below offset */
  159. if (i < offset << shift) {
  160. k = (offset << shift) - i;
  161. i = offset << shift;
  162. } else {
  163. k = 0;
  164. }
  165. for (; k < len; k++) {
  166. if (!(i & mask)) {
  167. cur = base + (k << umem_page_shift);
  168. cur |= access_flags;
  169. idx = (i >> shift) - offset;
  170. pas[idx] = cpu_to_be64(cur);
  171. mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
  172. i >> shift, be64_to_cpu(pas[idx]));
  173. }
  174. i++;
  175. /* Stop after num_pages reached */
  176. if (i >> shift >= offset + num_pages)
  177. return;
  178. }
  179. }
  180. }
  181. void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  182. int page_shift, __be64 *pas, int access_flags)
  183. {
  184. return __mlx5_ib_populate_pas(dev, umem, page_shift, 0,
  185. ib_umem_num_pages(umem), pas,
  186. access_flags);
  187. }
  188. int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
  189. {
  190. u64 page_size;
  191. u64 page_mask;
  192. u64 off_size;
  193. u64 off_mask;
  194. u64 buf_off;
  195. page_size = (u64)1 << page_shift;
  196. page_mask = page_size - 1;
  197. buf_off = addr & page_mask;
  198. off_size = page_size >> 6;
  199. off_mask = off_size - 1;
  200. if (buf_off & off_mask)
  201. return -EINVAL;
  202. *offset = buf_off >> ilog2(off_size);
  203. return 0;
  204. }