Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | #ifndef IB_UMEM_ODP_H |
| 34 | #define IB_UMEM_ODP_H |
| 35 | |
| 36 | #include <rdma/ib_umem.h> |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 37 | #include <rdma/ib_verbs.h> |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 38 | |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 39 | struct ib_umem_odp { |
Jason Gunthorpe | 41b4deea | 2018-09-16 20:48:05 +0300 | [diff] [blame] | 40 | struct ib_umem umem; |
Jason Gunthorpe | f25a546 | 2019-11-12 16:22:22 -0400 | [diff] [blame] | 41 | struct mmu_interval_notifier notifier; |
| 42 | struct pid *tgid; |
Jason Gunthorpe | c9990ab | 2018-09-16 20:48:07 +0300 | [diff] [blame] | 43 | |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 44 | /* |
| 45 | * An array of the pages included in the on-demand paging umem. |
| 46 | * Indices of pages that are currently not mapped into the device will |
| 47 | * contain NULL. |
| 48 | */ |
| 49 | struct page **page_list; |
| 50 | /* |
| 51 | * An array of the same size as page_list, with DMA addresses mapped |
| 52 | * for pages the pages in page_list. The lower two bits designate |
| 53 | * access permissions. See ODP_READ_ALLOWED_BIT and |
| 54 | * ODP_WRITE_ALLOWED_BIT. |
| 55 | */ |
| 56 | dma_addr_t *dma_list; |
| 57 | /* |
| 58 | * The umem_mutex protects the page_list and dma_list fields of an ODP |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 59 | * umem, allowing only a single thread to map/unmap pages. The mutex |
| 60 | * also protects access to the mmu notifier counters. |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 61 | */ |
| 62 | struct mutex umem_mutex; |
| 63 | void *private; /* for the HW driver to use. */ |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 64 | |
Shiraz Saleem | d10bcf9 | 2019-04-02 14:52:52 -0500 | [diff] [blame] | 65 | int npages; |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 66 | |
Jason Gunthorpe | fd7dbf0 | 2019-08-19 14:17:01 +0300 | [diff] [blame] | 67 | /* |
| 68 | * An implicit odp umem cannot be DMA mapped, has 0 length, and serves |
| 69 | * only as an anchor for the driver to hold onto the per_mm. FIXME: |
| 70 | * This should be removed and drivers should work with the per_mm |
| 71 | * directly. |
| 72 | */ |
| 73 | bool is_implicit_odp; |
| 74 | |
Jason Gunthorpe | d2183c6 | 2019-05-20 09:05:25 +0300 | [diff] [blame] | 75 | unsigned int page_shift; |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 76 | }; |
| 77 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 78 | static inline struct ib_umem_odp *to_ib_umem_odp(struct ib_umem *umem) |
| 79 | { |
Jason Gunthorpe | 41b4deea | 2018-09-16 20:48:05 +0300 | [diff] [blame] | 80 | return container_of(umem, struct ib_umem_odp, umem); |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 81 | } |
| 82 | |
Jason Gunthorpe | d2183c6 | 2019-05-20 09:05:25 +0300 | [diff] [blame] | 83 | /* Returns the first page of an ODP umem. */ |
| 84 | static inline unsigned long ib_umem_start(struct ib_umem_odp *umem_odp) |
| 85 | { |
Jason Gunthorpe | f25a546 | 2019-11-12 16:22:22 -0400 | [diff] [blame] | 86 | return umem_odp->notifier.interval_tree.start; |
Jason Gunthorpe | d2183c6 | 2019-05-20 09:05:25 +0300 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* Returns the address of the page after the last one of an ODP umem. */ |
| 90 | static inline unsigned long ib_umem_end(struct ib_umem_odp *umem_odp) |
| 91 | { |
Jason Gunthorpe | f25a546 | 2019-11-12 16:22:22 -0400 | [diff] [blame] | 92 | return umem_odp->notifier.interval_tree.last + 1; |
Jason Gunthorpe | d2183c6 | 2019-05-20 09:05:25 +0300 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static inline size_t ib_umem_odp_num_pages(struct ib_umem_odp *umem_odp) |
| 96 | { |
| 97 | return (ib_umem_end(umem_odp) - ib_umem_start(umem_odp)) >> |
| 98 | umem_odp->page_shift; |
| 99 | } |
| 100 | |
Leon Romanovsky | 13859d5d | 2019-01-08 16:07:26 +0200 | [diff] [blame] | 101 | /* |
| 102 | * The lower 2 bits of the DMA address signal the R/W permissions for |
| 103 | * the entry. To upgrade the permissions, provide the appropriate |
| 104 | * bitmask to the map_dma_pages function. |
| 105 | * |
| 106 | * Be aware that upgrading a mapped address might result in change of |
| 107 | * the DMA address for the page. |
| 108 | */ |
| 109 | #define ODP_READ_ALLOWED_BIT (1<<0ULL) |
| 110 | #define ODP_WRITE_ALLOWED_BIT (1<<1ULL) |
| 111 | |
| 112 | #define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT)) |
| 113 | |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 114 | #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING |
| 115 | |
Jason Gunthorpe | f25a546 | 2019-11-12 16:22:22 -0400 | [diff] [blame] | 116 | struct ib_umem_odp * |
Moni Shoua | c320e52 | 2020-01-15 14:43:31 +0200 | [diff] [blame] | 117 | ib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size, |
Jason Gunthorpe | f25a546 | 2019-11-12 16:22:22 -0400 | [diff] [blame] | 118 | int access, const struct mmu_interval_notifier_ops *ops); |
Moni Shoua | c320e52 | 2020-01-15 14:43:31 +0200 | [diff] [blame] | 119 | struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_device *device, |
Jason Gunthorpe | f20bef6 | 2019-08-19 14:17:03 +0300 | [diff] [blame] | 120 | int access); |
Jason Gunthorpe | f25a546 | 2019-11-12 16:22:22 -0400 | [diff] [blame] | 121 | struct ib_umem_odp * |
| 122 | ib_umem_odp_alloc_child(struct ib_umem_odp *root_umem, unsigned long addr, |
| 123 | size_t size, |
| 124 | const struct mmu_interval_notifier_ops *ops); |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 125 | void ib_umem_odp_release(struct ib_umem_odp *umem_odp); |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 126 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 127 | int ib_umem_odp_map_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset, |
| 128 | u64 bcnt, u64 access_mask, |
| 129 | unsigned long current_seq); |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 130 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 131 | void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset, |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 132 | u64 bound); |
| 133 | |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 134 | #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */ |
| 135 | |
Jason Gunthorpe | f25a546 | 2019-11-12 16:22:22 -0400 | [diff] [blame] | 136 | static inline struct ib_umem_odp * |
Moni Shoua | c320e52 | 2020-01-15 14:43:31 +0200 | [diff] [blame] | 137 | ib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size, |
Jason Gunthorpe | f25a546 | 2019-11-12 16:22:22 -0400 | [diff] [blame] | 138 | int access, const struct mmu_interval_notifier_ops *ops) |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 139 | { |
Jason Gunthorpe | 261dc53 | 2019-08-19 14:17:04 +0300 | [diff] [blame] | 140 | return ERR_PTR(-EINVAL); |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 143 | static inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {} |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 144 | |
| 145 | #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */ |
| 146 | |
| 147 | #endif /* IB_UMEM_ODP_H */ |