pvrdma_doorbell.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
  3. *
  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.
  12. *
  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.
  16. *
  17. * The BSD 2-Clause License
  18. *
  19. * Redistribution and use in source and binary forms, with or
  20. * without modification, are permitted provided that the following
  21. * conditions are met:
  22. *
  23. * - Redistributions of source code must retain the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer.
  26. *
  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.
  31. *
  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.
  44. */
  45. #include <linux/bitmap.h>
  46. #include <linux/errno.h>
  47. #include <linux/slab.h>
  48. #include "pvrdma.h"
  49. int pvrdma_uar_table_init(struct pvrdma_dev *dev)
  50. {
  51. u32 num = dev->dsr->caps.max_uar;
  52. u32 mask = num - 1;
  53. struct pvrdma_id_table *tbl = &dev->uar_table.tbl;
  54. if (!is_power_of_2(num))
  55. return -EINVAL;
  56. tbl->last = 0;
  57. tbl->top = 0;
  58. tbl->max = num;
  59. tbl->mask = mask;
  60. spin_lock_init(&tbl->lock);
  61. tbl->table = kcalloc(BITS_TO_LONGS(num), sizeof(long), GFP_KERNEL);
  62. if (!tbl->table)
  63. return -ENOMEM;
  64. /* 0th UAR is taken by the device. */
  65. set_bit(0, tbl->table);
  66. return 0;
  67. }
  68. void pvrdma_uar_table_cleanup(struct pvrdma_dev *dev)
  69. {
  70. struct pvrdma_id_table *tbl = &dev->uar_table.tbl;
  71. kfree(tbl->table);
  72. }
  73. int pvrdma_uar_alloc(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar)
  74. {
  75. struct pvrdma_id_table *tbl;
  76. unsigned long flags;
  77. u32 obj;
  78. tbl = &dev->uar_table.tbl;
  79. spin_lock_irqsave(&tbl->lock, flags);
  80. obj = find_next_zero_bit(tbl->table, tbl->max, tbl->last);
  81. if (obj >= tbl->max) {
  82. tbl->top = (tbl->top + tbl->max) & tbl->mask;
  83. obj = find_first_zero_bit(tbl->table, tbl->max);
  84. }
  85. if (obj >= tbl->max) {
  86. spin_unlock_irqrestore(&tbl->lock, flags);
  87. return -ENOMEM;
  88. }
  89. set_bit(obj, tbl->table);
  90. obj |= tbl->top;
  91. spin_unlock_irqrestore(&tbl->lock, flags);
  92. uar->index = obj;
  93. uar->pfn = (pci_resource_start(dev->pdev, PVRDMA_PCI_RESOURCE_UAR) >>
  94. PAGE_SHIFT) + uar->index;
  95. return 0;
  96. }
  97. void pvrdma_uar_free(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar)
  98. {
  99. struct pvrdma_id_table *tbl = &dev->uar_table.tbl;
  100. unsigned long flags;
  101. u32 obj;
  102. obj = uar->index & (tbl->max - 1);
  103. spin_lock_irqsave(&tbl->lock, flags);
  104. clear_bit(obj, tbl->table);
  105. tbl->last = min(tbl->last, obj);
  106. tbl->top = (tbl->top + tbl->max) & tbl->mask;
  107. spin_unlock_irqrestore(&tbl->lock, flags);
  108. }