2 * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of EITHER the GNU General Public License
6 * version 2 as published by the Free Software Foundation or the BSD
7 * 2-Clause License. This program is distributed in the hope that it
8 * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
9 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
10 * See the GNU General Public License version 2 for more details at
11 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
13 * You should have received a copy of the GNU General Public License
14 * along with this program available in the file COPYING in the main
15 * directory of this source tree.
17 * The BSD 2-Clause License
19 * Redistribution and use in source and binary forms, with or
20 * without modification, are permitted provided that the following
23 * - Redistributions of source code must retain the above
24 * copyright notice, this list of conditions and the following
27 * - Redistributions in binary form must reproduce the above
28 * copyright notice, this list of conditions and the following
29 * disclaimer in the documentation and/or other materials
30 * provided with the distribution.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43 * OF THE POSSIBILITY OF SUCH DAMAGE.
46 #include <linux/bitmap.h>
47 #include <linux/errno.h>
48 #include <linux/slab.h>
52 int pvrdma_uar_table_init(struct pvrdma_dev
*dev
)
54 u32 num
= dev
->dsr
->caps
.max_uar
;
56 struct pvrdma_id_table
*tbl
= &dev
->uar_table
.tbl
;
58 if (!is_power_of_2(num
))
65 spin_lock_init(&tbl
->lock
);
66 tbl
->table
= kcalloc(BITS_TO_LONGS(num
), sizeof(long), GFP_KERNEL
);
70 /* 0th UAR is taken by the device. */
71 set_bit(0, tbl
->table
);
76 void pvrdma_uar_table_cleanup(struct pvrdma_dev
*dev
)
78 struct pvrdma_id_table
*tbl
= &dev
->uar_table
.tbl
;
83 int pvrdma_uar_alloc(struct pvrdma_dev
*dev
, struct pvrdma_uar_map
*uar
)
85 struct pvrdma_id_table
*tbl
;
89 tbl
= &dev
->uar_table
.tbl
;
91 spin_lock_irqsave(&tbl
->lock
, flags
);
92 obj
= find_next_zero_bit(tbl
->table
, tbl
->max
, tbl
->last
);
93 if (obj
>= tbl
->max
) {
94 tbl
->top
= (tbl
->top
+ tbl
->max
) & tbl
->mask
;
95 obj
= find_first_zero_bit(tbl
->table
, tbl
->max
);
98 if (obj
>= tbl
->max
) {
99 spin_unlock_irqrestore(&tbl
->lock
, flags
);
103 set_bit(obj
, tbl
->table
);
106 spin_unlock_irqrestore(&tbl
->lock
, flags
);
109 uar
->pfn
= (pci_resource_start(dev
->pdev
, PVRDMA_PCI_RESOURCE_UAR
) >>
110 PAGE_SHIFT
) + uar
->index
;
115 void pvrdma_uar_free(struct pvrdma_dev
*dev
, struct pvrdma_uar_map
*uar
)
117 struct pvrdma_id_table
*tbl
= &dev
->uar_table
.tbl
;
121 obj
= uar
->index
& (tbl
->max
- 1);
122 spin_lock_irqsave(&tbl
->lock
, flags
);
123 clear_bit(obj
, tbl
->table
);
124 tbl
->last
= min(tbl
->last
, obj
);
125 tbl
->top
= (tbl
->top
+ tbl
->max
) & tbl
->mask
;
126 spin_unlock_irqrestore(&tbl
->lock
, flags
);