brintos

brintos / linux-shallow public Read only

0
0
Text · 3.9 KiB · 9a4de96 Raw
128 lines · c
1/*2 * Copyright (c) 2012-2016 VMware, Inc.  All rights reserved.3 *4 * This program is free software; you can redistribute it and/or5 * modify it under the terms of EITHER the GNU General Public License6 * version 2 as published by the Free Software Foundation or the BSD7 * 2-Clause License. This program is distributed in the hope that it8 * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED9 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.10 * See the GNU General Public License version 2 for more details at11 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.12 *13 * You should have received a copy of the GNU General Public License14 * along with this program available in the file COPYING in the main15 * directory of this source tree.16 *17 * The BSD 2-Clause License18 *19 *     Redistribution and use in source and binary forms, with or20 *     without modification, are permitted provided that the following21 *     conditions are met:22 *23 *      - Redistributions of source code must retain the above24 *        copyright notice, this list of conditions and the following25 *        disclaimer.26 *27 *      - Redistributions in binary form must reproduce the above28 *        copyright notice, this list of conditions and the following29 *        disclaimer in the documentation and/or other materials30 *        provided with the distribution.31 *32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE36 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR39 * 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 ADVISED43 * OF THE POSSIBILITY OF SUCH DAMAGE.44 */45 46#include <linux/bitmap.h>47#include <linux/errno.h>48#include <linux/slab.h>49 50#include "pvrdma.h"51 52int pvrdma_uar_table_init(struct pvrdma_dev *dev)53{54	u32 num = dev->dsr->caps.max_uar;55	u32 mask = num - 1;56	struct pvrdma_id_table *tbl = &dev->uar_table.tbl;57 58	if (!is_power_of_2(num))59		return -EINVAL;60 61	tbl->last = 0;62	tbl->top = 0;63	tbl->max = num;64	tbl->mask = mask;65	spin_lock_init(&tbl->lock);66	tbl->table = bitmap_zalloc(num, GFP_KERNEL);67	if (!tbl->table)68		return -ENOMEM;69 70	/* 0th UAR is taken by the device. */71	__set_bit(0, tbl->table);72 73	return 0;74}75 76void pvrdma_uar_table_cleanup(struct pvrdma_dev *dev)77{78	struct pvrdma_id_table *tbl = &dev->uar_table.tbl;79 80	bitmap_free(tbl->table);81}82 83int pvrdma_uar_alloc(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar)84{85	struct pvrdma_id_table *tbl;86	unsigned long flags;87	u32 obj;88 89	tbl = &dev->uar_table.tbl;90 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);96	}97 98	if (obj >= tbl->max) {99		spin_unlock_irqrestore(&tbl->lock, flags);100		return -ENOMEM;101	}102 103	__set_bit(obj, tbl->table);104	obj |= tbl->top;105 106	spin_unlock_irqrestore(&tbl->lock, flags);107 108	uar->index = obj;109	uar->pfn = (pci_resource_start(dev->pdev, PVRDMA_PCI_RESOURCE_UAR) >>110		    PAGE_SHIFT) + uar->index;111 112	return 0;113}114 115void pvrdma_uar_free(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar)116{117	struct pvrdma_id_table *tbl = &dev->uar_table.tbl;118	unsigned long flags;119	u32 obj;120 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);127}128