3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2015 Intel Corporation.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
21 * Copyright(c) 2015 Intel Corporation.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include <linux/module.h>
52 #include <linux/slab.h>
53 #include <linux/vmalloc.h>
55 #include <linux/errno.h>
56 #include <asm/pgtable.h>
61 * hfi1_release_mmap_info - free mmap info structure
62 * @ref: a pointer to the kref within struct hfi1_mmap_info
64 void hfi1_release_mmap_info(struct kref *ref)
66 struct hfi1_mmap_info *ip =
67 container_of(ref, struct hfi1_mmap_info, ref);
68 struct hfi1_ibdev *dev = to_idev(ip->context->device);
70 spin_lock_irq(&dev->pending_lock);
71 list_del(&ip->pending_mmaps);
72 spin_unlock_irq(&dev->pending_lock);
79 * open and close keep track of how many times the CQ is mapped,
80 * to avoid releasing it.
82 static void hfi1_vma_open(struct vm_area_struct *vma)
84 struct hfi1_mmap_info *ip = vma->vm_private_data;
89 static void hfi1_vma_close(struct vm_area_struct *vma)
91 struct hfi1_mmap_info *ip = vma->vm_private_data;
93 kref_put(&ip->ref, hfi1_release_mmap_info);
96 static struct vm_operations_struct hfi1_vm_ops = {
97 .open = hfi1_vma_open,
98 .close = hfi1_vma_close,
102 * hfi1_mmap - create a new mmap region
103 * @context: the IB user context of the process making the mmap() call
104 * @vma: the VMA to be initialized
105 * Return zero if the mmap is OK. Otherwise, return an errno.
107 int hfi1_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
109 struct hfi1_ibdev *dev = to_idev(context->device);
110 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
111 unsigned long size = vma->vm_end - vma->vm_start;
112 struct hfi1_mmap_info *ip, *pp;
116 * Search the device's list of objects waiting for a mmap call.
117 * Normally, this list is very short since a call to create a
118 * CQ, QP, or SRQ is soon followed by a call to mmap().
120 spin_lock_irq(&dev->pending_lock);
121 list_for_each_entry_safe(ip, pp, &dev->pending_mmaps,
123 /* Only the creator is allowed to mmap the object */
124 if (context != ip->context || (__u64) offset != ip->offset)
126 /* Don't allow a mmap larger than the object. */
130 list_del_init(&ip->pending_mmaps);
131 spin_unlock_irq(&dev->pending_lock);
133 ret = remap_vmalloc_range(vma, ip->obj, 0);
136 vma->vm_ops = &hfi1_vm_ops;
137 vma->vm_private_data = ip;
141 spin_unlock_irq(&dev->pending_lock);
147 * Allocate information for hfi1_mmap
149 struct hfi1_mmap_info *hfi1_create_mmap_info(struct hfi1_ibdev *dev,
151 struct ib_ucontext *context,
153 struct hfi1_mmap_info *ip;
155 ip = kmalloc(sizeof(*ip), GFP_KERNEL);
159 size = PAGE_ALIGN(size);
161 spin_lock_irq(&dev->mmap_offset_lock);
162 if (dev->mmap_offset == 0)
163 dev->mmap_offset = PAGE_SIZE;
164 ip->offset = dev->mmap_offset;
165 dev->mmap_offset += size;
166 spin_unlock_irq(&dev->mmap_offset_lock);
168 INIT_LIST_HEAD(&ip->pending_mmaps);
170 ip->context = context;
178 void hfi1_update_mmap_info(struct hfi1_ibdev *dev, struct hfi1_mmap_info *ip,
181 size = PAGE_ALIGN(size);
183 spin_lock_irq(&dev->mmap_offset_lock);
184 if (dev->mmap_offset == 0)
185 dev->mmap_offset = PAGE_SIZE;
186 ip->offset = dev->mmap_offset;
187 dev->mmap_offset += size;
188 spin_unlock_irq(&dev->mmap_offset_lock);