dfl-afu-region.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <[email protected]>
  9. * Xiao Guangrong <[email protected]>
  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. * @region_index: region index.
  36. * @region_size: region size.
  37. * @phys: region's physical address of this region.
  38. * @flags: region flags (access permission).
  39. *
  40. * Return: 0 on success, negative error code otherwise.
  41. */
  42. int afu_mmio_region_add(struct dfl_feature_platform_data *pdata,
  43. u32 region_index, u64 region_size, u64 phys, u32 flags)
  44. {
  45. struct dfl_afu_mmio_region *region;
  46. struct dfl_afu *afu;
  47. int ret = 0;
  48. region = devm_kzalloc(&pdata->dev->dev, sizeof(*region), GFP_KERNEL);
  49. if (!region)
  50. return -ENOMEM;
  51. region->index = region_index;
  52. region->size = region_size;
  53. region->phys = phys;
  54. region->flags = flags;
  55. mutex_lock(&pdata->lock);
  56. afu = dfl_fpga_pdata_get_private(pdata);
  57. /* check if @index already exists */
  58. if (get_region_by_index(afu, region_index)) {
  59. mutex_unlock(&pdata->lock);
  60. ret = -EEXIST;
  61. goto exit;
  62. }
  63. region_size = PAGE_ALIGN(region_size);
  64. region->offset = afu->region_cur_offset;
  65. list_add(&region->node, &afu->regions);
  66. afu->region_cur_offset += region_size;
  67. afu->num_regions++;
  68. mutex_unlock(&pdata->lock);
  69. return 0;
  70. exit:
  71. devm_kfree(&pdata->dev->dev, region);
  72. return ret;
  73. }
  74. /**
  75. * afu_mmio_region_destroy - destroy all mmio regions under given feature dev.
  76. * @pdata: afu platform device's pdata.
  77. */
  78. void afu_mmio_region_destroy(struct dfl_feature_platform_data *pdata)
  79. {
  80. struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
  81. struct dfl_afu_mmio_region *tmp, *region;
  82. list_for_each_entry_safe(region, tmp, &afu->regions, node)
  83. devm_kfree(&pdata->dev->dev, region);
  84. }
  85. /**
  86. * afu_mmio_region_get_by_index - find an afu region by index.
  87. * @pdata: afu platform device's pdata.
  88. * @region_index: region index.
  89. * @pregion: ptr to region for result.
  90. *
  91. * Return: 0 on success, negative error code otherwise.
  92. */
  93. int afu_mmio_region_get_by_index(struct dfl_feature_platform_data *pdata,
  94. u32 region_index,
  95. struct dfl_afu_mmio_region *pregion)
  96. {
  97. struct dfl_afu_mmio_region *region;
  98. struct dfl_afu *afu;
  99. int ret = 0;
  100. mutex_lock(&pdata->lock);
  101. afu = dfl_fpga_pdata_get_private(pdata);
  102. region = get_region_by_index(afu, region_index);
  103. if (!region) {
  104. ret = -EINVAL;
  105. goto exit;
  106. }
  107. *pregion = *region;
  108. exit:
  109. mutex_unlock(&pdata->lock);
  110. return ret;
  111. }
  112. /**
  113. * afu_mmio_region_get_by_offset - find an afu mmio region by offset and size
  114. *
  115. * @pdata: afu platform device's pdata.
  116. * @offset: region offset from start of the device fd.
  117. * @size: region size.
  118. * @pregion: ptr to region for result.
  119. *
  120. * Find the region which fully contains the region described by input
  121. * parameters (offset and size) from the feature dev's region linked list.
  122. *
  123. * Return: 0 on success, negative error code otherwise.
  124. */
  125. int afu_mmio_region_get_by_offset(struct dfl_feature_platform_data *pdata,
  126. u64 offset, u64 size,
  127. struct dfl_afu_mmio_region *pregion)
  128. {
  129. struct dfl_afu_mmio_region *region;
  130. struct dfl_afu *afu;
  131. int ret = 0;
  132. mutex_lock(&pdata->lock);
  133. afu = dfl_fpga_pdata_get_private(pdata);
  134. for_each_region(region, afu)
  135. if (region->offset <= offset &&
  136. region->offset + region->size >= offset + size) {
  137. *pregion = *region;
  138. goto exit;
  139. }
  140. ret = -EINVAL;
  141. exit:
  142. mutex_unlock(&pdata->lock);
  143. return ret;
  144. }