mem.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2013, Mellanox Technologies inc. 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 "mlx5_ib.h"
  35. /* @umem: umem object to scan
  36. * @addr: ib virtual address requested by the user
  37. * @count: number of PAGE_SIZE pages covered by umem
  38. * @shift: page shift for the compound pages found in the region
  39. * @ncont: number of compund pages
  40. * @order: log2 of the number of compound pages
  41. */
  42. void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr, int *count, int *shift,
  43. int *ncont, int *order)
  44. {
  45. unsigned long tmp;
  46. unsigned long m;
  47. int i, k;
  48. u64 base = 0;
  49. int p = 0;
  50. int skip;
  51. int mask;
  52. u64 len;
  53. u64 pfn;
  54. struct scatterlist *sg;
  55. int entry;
  56. unsigned long page_shift = ilog2(umem->page_size);
  57. addr = addr >> page_shift;
  58. tmp = (unsigned long)addr;
  59. m = find_first_bit(&tmp, sizeof(tmp));
  60. skip = 1 << m;
  61. mask = skip - 1;
  62. i = 0;
  63. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  64. len = sg_dma_len(sg) >> page_shift;
  65. pfn = sg_dma_address(sg) >> page_shift;
  66. for (k = 0; k < len; k++) {
  67. if (!(i & mask)) {
  68. tmp = (unsigned long)pfn;
  69. m = min(m, find_first_bit(&tmp, sizeof(tmp)));
  70. skip = 1 << m;
  71. mask = skip - 1;
  72. base = pfn;
  73. p = 0;
  74. } else {
  75. if (base + p != pfn) {
  76. tmp = (unsigned long)p;
  77. m = find_first_bit(&tmp, sizeof(tmp));
  78. skip = 1 << m;
  79. mask = skip - 1;
  80. base = pfn;
  81. p = 0;
  82. }
  83. }
  84. p++;
  85. i++;
  86. }
  87. }
  88. if (i) {
  89. m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
  90. if (order)
  91. *order = ilog2(roundup_pow_of_two(i) >> m);
  92. *ncont = DIV_ROUND_UP(i, (1 << m));
  93. } else {
  94. m = 0;
  95. if (order)
  96. *order = 0;
  97. *ncont = 0;
  98. }
  99. *shift = page_shift + m;
  100. *count = i;
  101. }
  102. void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  103. int page_shift, __be64 *pas, int umr)
  104. {
  105. unsigned long umem_page_shift = ilog2(umem->page_size);
  106. int shift = page_shift - umem_page_shift;
  107. int mask = (1 << shift) - 1;
  108. int i, k;
  109. u64 cur = 0;
  110. u64 base;
  111. int len;
  112. struct scatterlist *sg;
  113. int entry;
  114. i = 0;
  115. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  116. len = sg_dma_len(sg) >> umem_page_shift;
  117. base = sg_dma_address(sg);
  118. for (k = 0; k < len; k++) {
  119. if (!(i & mask)) {
  120. cur = base + (k << umem_page_shift);
  121. if (umr)
  122. cur |= 3;
  123. pas[i >> shift] = cpu_to_be64(cur);
  124. mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
  125. i >> shift, be64_to_cpu(pas[i >> shift]));
  126. } else
  127. mlx5_ib_dbg(dev, "=====> 0x%llx\n",
  128. base + (k << umem_page_shift));
  129. i++;
  130. }
  131. }
  132. }
  133. int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
  134. {
  135. u64 page_size;
  136. u64 page_mask;
  137. u64 off_size;
  138. u64 off_mask;
  139. u64 buf_off;
  140. page_size = (u64)1 << page_shift;
  141. page_mask = page_size - 1;
  142. buf_off = addr & page_mask;
  143. off_size = page_size >> 6;
  144. off_mask = off_size - 1;
  145. if (buf_off & off_mask)
  146. return -EINVAL;
  147. *offset = buf_off >> ilog2(off_size);
  148. return 0;
  149. }