2 * Copyright(c) 2016 Intel Corporation.
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include <linux/slab.h>
49 #include <linux/vmalloc.h>
51 #include <asm/pgtable.h>
52 #include <rdma/uverbs_ioctl.h>
56 * rvt_mmap_init - init link list and lock for mem map
57 * @rdi: rvt dev struct
59 void rvt_mmap_init(struct rvt_dev_info
*rdi
)
61 INIT_LIST_HEAD(&rdi
->pending_mmaps
);
62 spin_lock_init(&rdi
->pending_lock
);
63 rdi
->mmap_offset
= PAGE_SIZE
;
64 spin_lock_init(&rdi
->mmap_offset_lock
);
68 * rvt_release_mmap_info - free mmap info structure
69 * @ref: a pointer to the kref within struct rvt_mmap_info
71 void rvt_release_mmap_info(struct kref
*ref
)
73 struct rvt_mmap_info
*ip
=
74 container_of(ref
, struct rvt_mmap_info
, ref
);
75 struct rvt_dev_info
*rdi
= ib_to_rvt(ip
->context
->device
);
77 spin_lock_irq(&rdi
->pending_lock
);
78 list_del(&ip
->pending_mmaps
);
79 spin_unlock_irq(&rdi
->pending_lock
);
85 static void rvt_vma_open(struct vm_area_struct
*vma
)
87 struct rvt_mmap_info
*ip
= vma
->vm_private_data
;
92 static void rvt_vma_close(struct vm_area_struct
*vma
)
94 struct rvt_mmap_info
*ip
= vma
->vm_private_data
;
96 kref_put(&ip
->ref
, rvt_release_mmap_info
);
99 static const struct vm_operations_struct rvt_vm_ops
= {
100 .open
= rvt_vma_open
,
101 .close
= rvt_vma_close
,
105 * rvt_mmap - create a new mmap region
106 * @context: the IB user context of the process making the mmap() call
107 * @vma: the VMA to be initialized
109 * Return: zero if the mmap is OK. Otherwise, return an errno.
111 int rvt_mmap(struct ib_ucontext
*context
, struct vm_area_struct
*vma
)
113 struct rvt_dev_info
*rdi
= ib_to_rvt(context
->device
);
114 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
115 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
116 struct rvt_mmap_info
*ip
, *pp
;
120 * Search the device's list of objects waiting for a mmap call.
121 * Normally, this list is very short since a call to create a
122 * CQ, QP, or SRQ is soon followed by a call to mmap().
124 spin_lock_irq(&rdi
->pending_lock
);
125 list_for_each_entry_safe(ip
, pp
, &rdi
->pending_mmaps
,
127 /* Only the creator is allowed to mmap the object */
128 if (context
!= ip
->context
|| (__u64
)offset
!= ip
->offset
)
130 /* Don't allow a mmap larger than the object. */
134 list_del_init(&ip
->pending_mmaps
);
135 spin_unlock_irq(&rdi
->pending_lock
);
137 ret
= remap_vmalloc_range(vma
, ip
->obj
, 0);
140 vma
->vm_ops
= &rvt_vm_ops
;
141 vma
->vm_private_data
= ip
;
145 spin_unlock_irq(&rdi
->pending_lock
);
151 * rvt_create_mmap_info - allocate information for hfi1_mmap
152 * @rdi: rvt dev struct
153 * @size: size in bytes to map
154 * @udata: user data (must be valid!)
155 * @obj: opaque pointer to a cq, wq etc
157 * Return: rvt_mmap struct on success
159 struct rvt_mmap_info
*rvt_create_mmap_info(struct rvt_dev_info
*rdi
, u32 size
,
160 struct ib_udata
*udata
, void *obj
)
162 struct rvt_mmap_info
*ip
;
165 return ERR_PTR(-EINVAL
);
167 ip
= kmalloc_node(sizeof(*ip
), GFP_KERNEL
, rdi
->dparms
.node
);
171 size
= PAGE_ALIGN(size
);
173 spin_lock_irq(&rdi
->mmap_offset_lock
);
174 if (rdi
->mmap_offset
== 0)
175 rdi
->mmap_offset
= ALIGN(PAGE_SIZE
, SHMLBA
);
176 ip
->offset
= rdi
->mmap_offset
;
177 rdi
->mmap_offset
+= ALIGN(size
, SHMLBA
);
178 spin_unlock_irq(&rdi
->mmap_offset_lock
);
180 INIT_LIST_HEAD(&ip
->pending_mmaps
);
183 container_of(udata
, struct uverbs_attr_bundle
, driver_udata
)
192 * rvt_update_mmap_info - update a mem map
193 * @rdi: rvt dev struct
194 * @ip: mmap info pointer
195 * @size: size to grow by
196 * @obj: opaque pointer to cq, wq, etc.
198 void rvt_update_mmap_info(struct rvt_dev_info
*rdi
, struct rvt_mmap_info
*ip
,
201 size
= PAGE_ALIGN(size
);
203 spin_lock_irq(&rdi
->mmap_offset_lock
);
204 if (rdi
->mmap_offset
== 0)
205 rdi
->mmap_offset
= PAGE_SIZE
;
206 ip
->offset
= rdi
->mmap_offset
;
207 rdi
->mmap_offset
+= size
;
208 spin_unlock_irq(&rdi
->mmap_offset_lock
);