dfl-afu-region.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for FPGA Accelerated Function Unit (AFU) MMIO Region Management
  4. *
  5. * Copyright (C) 2017-2018 Intel Corporation, Inc.
  6. *
  7. * Authors:
  8. * Wu Hao <hao.wu@intel.com>
  9. * Xiao Guangrong <guangrong.xiao@linux.intel.com>
  10. */
  11. #include "dfl-afu.h"
  12. /**
  13. * afu_mmio_region_init - init function for afu mmio region support
  14. * @pdata: afu platform device's pdata.
  15. */
  16. void afu_mmio_region_init(struct dfl_feature_platform_data *pdata)
  17. {
  18. struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
  19. INIT_LIST_HEAD(&afu->regions);
  20. }
  21. #define for_each_region(region, afu) \
  22. list_for_each_entry((region), &(afu)->regions, node)
  23. static struct dfl_afu_mmio_region *get_region_by_index(struct dfl_afu *afu,
  24. u32 region_index)
  25. {
  26. struct dfl_afu_mmio_region *region;
  27. for_each_region(region, afu)
  28. if (region->index == region_index)
  29. return region;
  30. return NULL;
  31. }
  32. /**
  33. * afu_mmio_region_add - add a mmio region to given feature dev.
  34. *
  35. * @pdata: afu platform device's pdata.
  36. * @region_index: region index.
  37. * @region_size: region size.
  38. * @phys: region's physical address of this region.
  39. * @flags: region flags (access permission).
  40. *
  41. * Return: 0 on success, negative error code otherwise.
  42. */
  43. int afu_mmio_region_add(struct dfl_feature_platform_data *pdata,
  44. u32 region_index, u64 region_size, u64 phys, u32 flags)
  45. {
  46. struct dfl_afu_mmio_region *region;
  47. struct dfl_afu *afu;
  48. int ret = 0;
  49. region = devm_kzalloc(&pdata->dev->dev, sizeof(*region), GFP_KERNEL);
  50. if (!region)
  51. return -ENOMEM;
  52. region->index = region_index;
  53. region->size = region_size;
  54. region->phys = phys;
  55. region->flags = flags;
  56. mutex_lock(&pdata->lock);
  57. afu = dfl_fpga_pdata_get_private(pdata);
  58. /* check if @index already exists */
  59. if (get_region_by_index(afu, region_index)) {
  60. mutex_unlock(&pdata->lock);
  61. ret = -EEXIST;
  62. goto exit;
  63. }
  64. region_size = PAGE_ALIGN(region_size);
  65. region->offset = afu->region_cur_offset;
  66. list_add(&region->node, &afu->regions);
  67. afu->region_cur_offset += region_size;
  68. afu->num_regions++;
  69. mutex_unlock(&pdata->lock);
  70. return 0;
  71. exit:
  72. devm_kfree(&pdata->dev->dev, region);
  73. return ret;
  74. }
  75. /**
  76. * afu_mmio_region_destroy - destroy all mmio regions under given feature dev.
  77. * @pdata: afu platform device's pdata.
  78. */
  79. void afu_mmio_region_destroy(struct dfl_feature_platform_data *pdata)
  80. {
  81. struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
  82. struct dfl_afu_mmio_region *tmp, *region;
  83. list_for_each_entry_safe(region, tmp, &afu->regions, node)
  84. devm_kfree(&pdata->dev->dev, region);
  85. }
  86. /**
  87. * afu_mmio_region_get_by_index - find an afu region by index.
  88. * @pdata: afu platform device's pdata.
  89. * @region_index: region index.
  90. * @pregion: ptr to region for result.
  91. *
  92. * Return: 0 on success, negative error code otherwise.
  93. */
  94. int afu_mmio_region_get_by_index(struct dfl_feature_platform_data *pdata,
  95. u32 region_index,
  96. struct dfl_afu_mmio_region *pregion)
  97. {
  98. struct dfl_afu_mmio_region *region;
  99. struct dfl_afu *afu;
  100. int ret = 0;
  101. mutex_lock(&pdata->lock);
  102. afu = dfl_fpga_pdata_get_private(pdata);
  103. region = get_region_by_index(afu, region_index);
  104. if (!region) {
  105. ret = -EINVAL;
  106. goto exit;
  107. }
  108. *pregion = *region;
  109. exit:
  110. mutex_unlock(&pdata->lock);
  111. return ret;
  112. }
  113. /**
  114. * afu_mmio_region_get_by_offset - find an afu mmio region by offset and size
  115. *
  116. * @pdata: afu platform device's pdata.
  117. * @offset: region offset from start of the device fd.
  118. * @size: region size.
  119. * @pregion: ptr to region for result.
  120. *
  121. * Find the region which fully contains the region described by input
  122. * parameters (offset and size) from the feature dev's region linked list.
  123. *
  124. * Return: 0 on success, negative error code otherwise.
  125. */
  126. int afu_mmio_region_get_by_offset(struct dfl_feature_platform_data *pdata,
  127. u64 offset, u64 size,
  128. struct dfl_afu_mmio_region *pregion)
  129. {
  130. struct dfl_afu_mmio_region *region;
  131. struct dfl_afu *afu;
  132. int ret = 0;
  133. mutex_lock(&pdata->lock);
  134. afu = dfl_fpga_pdata_get_private(pdata);
  135. for_each_region(region, afu)
  136. if (region->offset <= offset &&
  137. region->offset + region->size >= offset + size) {
  138. *pregion = *region;
  139. goto exit;
  140. }
  141. ret = -EINVAL;
  142. exit:
  143. mutex_unlock(&pdata->lock);
  144. return ret;
  145. }