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 33 #include <rdma/ib_umem_odp.h> 34 #include <rdma/iter.h> 35 #include "mlx5_ib.h" 36 37 /* 38 * Fill in a physical address list. ib_umem_num_dma_blocks() entries will be 39 * filled in the pas array. 40 */ 41 void mlx5_ib_populate_pas(struct ib_umem *umem, size_t page_size, __be64 *pas, 42 u64 access_flags) 43 { 44 struct ib_block_iter biter; 45 46 rdma_umem_for_each_dma_block (umem, &biter, page_size) { 47 *pas = cpu_to_be64(rdma_block_iter_dma_address(&biter) | 48 access_flags); 49 pas++; 50 } 51 } 52 53 /* 54 * Compute the page shift and page_offset for mailboxes that use a quantized 55 * page_offset. The granulatity of the page offset scales according to page 56 * size. 57 */ 58 unsigned long __mlx5_umem_find_best_quantized_pgoff( 59 struct ib_umem *umem, unsigned long pgsz_bitmap, 60 unsigned int page_offset_bits, u64 pgoff_bitmask, unsigned int scale, 61 unsigned int *page_offset_quantized) 62 { 63 const u64 page_offset_mask = (1UL << page_offset_bits) - 1; 64 unsigned long page_size; 65 u64 page_offset; 66 67 page_size = ib_umem_find_best_pgoff(umem, pgsz_bitmap, pgoff_bitmask); 68 if (!page_size) 69 return 0; 70 71 /* 72 * page size is the largest possible page size. 73 * 74 * Reduce the page_size, and thus the page_offset and quanta, until the 75 * page_offset fits into the mailbox field. Once page_size < scale this 76 * loop is guaranteed to terminate. 77 */ 78 page_offset = ib_umem_dma_offset(umem, page_size); 79 while (page_offset & ~(u64)(page_offset_mask * (page_size / scale))) { 80 page_size /= 2; 81 page_offset = ib_umem_dma_offset(umem, page_size); 82 } 83 84 /* 85 * The address is not aligned, or otherwise cannot be represented by the 86 * page_offset. 87 */ 88 if (!(pgsz_bitmap & page_size)) 89 return 0; 90 91 *page_offset_quantized = 92 (unsigned long)page_offset / (page_size / scale); 93 if (WARN_ON(*page_offset_quantized > page_offset_mask)) 94 return 0; 95 return page_size; 96 } 97