mem.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. struct ib_umem_chunk *chunk;
  46. unsigned long tmp;
  47. unsigned long m;
  48. int i, j, k;
  49. u64 base = 0;
  50. int p = 0;
  51. int skip;
  52. int mask;
  53. u64 len;
  54. u64 pfn;
  55. addr = addr >> PAGE_SHIFT;
  56. tmp = (unsigned long)addr;
  57. m = find_first_bit(&tmp, sizeof(tmp));
  58. skip = 1 << m;
  59. mask = skip - 1;
  60. i = 0;
  61. list_for_each_entry(chunk, &umem->chunk_list, list)
  62. for (j = 0; j < chunk->nmap; j++) {
  63. len = sg_dma_len(&chunk->page_list[j]) >> PAGE_SHIFT;
  64. pfn = sg_dma_address(&chunk->page_list[j]) >> PAGE_SHIFT;
  65. for (k = 0; k < len; k++) {
  66. if (!(i & mask)) {
  67. tmp = (unsigned long)pfn;
  68. m = min(m, find_first_bit(&tmp, sizeof(tmp)));
  69. skip = 1 << m;
  70. mask = skip - 1;
  71. base = pfn;
  72. p = 0;
  73. } else {
  74. if (base + p != pfn) {
  75. tmp = (unsigned long)p;
  76. m = find_first_bit(&tmp, sizeof(tmp));
  77. skip = 1 << m;
  78. mask = skip - 1;
  79. base = pfn;
  80. p = 0;
  81. }
  82. }
  83. p++;
  84. i++;
  85. }
  86. }
  87. if (i) {
  88. m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
  89. if (order)
  90. *order = ilog2(roundup_pow_of_two(i) >> m);
  91. *ncont = DIV_ROUND_UP(i, (1 << m));
  92. } else {
  93. m = 0;
  94. if (order)
  95. *order = 0;
  96. *ncont = 0;
  97. }
  98. *shift = PAGE_SHIFT + m;
  99. *count = i;
  100. }
  101. void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  102. int page_shift, __be64 *pas, int umr)
  103. {
  104. int shift = page_shift - PAGE_SHIFT;
  105. int mask = (1 << shift) - 1;
  106. struct ib_umem_chunk *chunk;
  107. int i, j, k;
  108. u64 cur = 0;
  109. u64 base;
  110. int len;
  111. i = 0;
  112. list_for_each_entry(chunk, &umem->chunk_list, list)
  113. for (j = 0; j < chunk->nmap; j++) {
  114. len = sg_dma_len(&chunk->page_list[j]) >> PAGE_SHIFT;
  115. base = sg_dma_address(&chunk->page_list[j]);
  116. for (k = 0; k < len; k++) {
  117. if (!(i & mask)) {
  118. cur = base + (k << PAGE_SHIFT);
  119. if (umr)
  120. cur |= 3;
  121. pas[i >> shift] = cpu_to_be64(cur);
  122. mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
  123. i >> shift, be64_to_cpu(pas[i >> shift]));
  124. } else
  125. mlx5_ib_dbg(dev, "=====> 0x%llx\n",
  126. base + (k << PAGE_SHIFT));
  127. i++;
  128. }
  129. }
  130. }
  131. int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
  132. {
  133. u64 page_size;
  134. u64 page_mask;
  135. u64 off_size;
  136. u64 off_mask;
  137. u64 buf_off;
  138. page_size = 1 << page_shift;
  139. page_mask = page_size - 1;
  140. buf_off = addr & page_mask;
  141. off_size = page_size >> 6;
  142. off_mask = off_size - 1;
  143. if (buf_off & off_mask)
  144. return -EINVAL;
  145. *offset = buf_off >> ilog2(off_size);
  146. return 0;
  147. }