rsvmem_pool.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2022 Alibaba Group. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/mman.h>
  21. #include <linux/cdev.h>
  22. #include <linux/errno.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/dma-buf.h>
  25. #include <linux/version.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/genalloc.h>
  30. #include "rsvmem_pool.h"
  31. /* 12 bits (4096 bytes) */
  32. #define GEN_POOL_ALLOC_ORDER 12
  33. static rsvmem_pool_info_t rsvmem_pool_regions[MAX_RSVMEM_REGION_COUNT];
  34. int rsvmem_pool_create(struct device *dev)
  35. {
  36. struct device_node *np = dev->of_node;
  37. struct device_node *rsvmem_node;
  38. struct resource res;
  39. int pool_id = 0;
  40. int ret;
  41. if (!dev || !dev->of_node)
  42. return -EINVAL;
  43. rsvmem_node = of_parse_phandle(np, "memory-region", 0);
  44. if (!rsvmem_node) {
  45. dev_notice(dev, "No memory region node\n");
  46. return -ENODEV;
  47. }
  48. while (pool_id < MAX_RSVMEM_REGION_COUNT &&
  49. of_address_to_resource(rsvmem_node, pool_id, &res) == 0) {
  50. struct gen_pool *pool = gen_pool_create(GEN_POOL_ALLOC_ORDER, -1);
  51. if (pool == NULL) {
  52. dev_err(dev, "Failed to create reserved memory pool region[%d]\n", pool_id);
  53. return -ENOMEM;
  54. }
  55. ret = gen_pool_add(pool, res.start, resource_size(&res), -1);
  56. if (ret) {
  57. dev_err(dev, "%s: gen_pool_add failed\n", __func__);
  58. gen_pool_destroy(pool);
  59. return ret;
  60. }
  61. rsvmem_pool_regions[pool_id].pool = pool;
  62. rsvmem_pool_regions[pool_id].base = res.start;
  63. rsvmem_pool_regions[pool_id].size = resource_size(&res);
  64. dev_err(dev, "%s: rsvmem_pool_region[%d] = {pool=%px, base=0x%llx, size=0x%llx}\n",
  65. __func__, pool_id, rsvmem_pool_regions[pool_id].pool,
  66. rsvmem_pool_regions[pool_id].base, rsvmem_pool_regions[pool_id].size);
  67. pool_id ++;
  68. }
  69. return 0;
  70. }
  71. void rsvmem_pool_destroy(void)
  72. {
  73. int i;
  74. for (i = 0; i < MAX_RSVMEM_REGION_COUNT; i++) {
  75. if (rsvmem_pool_regions[i].pool != NULL) {
  76. gen_pool_destroy(rsvmem_pool_regions[i].pool);
  77. memset(&rsvmem_pool_regions[i], 0, sizeof(rsvmem_pool_info_t));
  78. }
  79. }
  80. }
  81. unsigned long rsvmem_pool_alloc(int region_id, size_t size)
  82. {
  83. struct gen_pool *pool;
  84. unsigned long addr;
  85. if (region_id < 0 || region_id >= MAX_RSVMEM_REGION_COUNT) {
  86. pr_err("%s: region_id(%d) is invalid\n", __func__, region_id);
  87. return 0;
  88. }
  89. pool = rsvmem_pool_regions[region_id].pool;
  90. if (pool == NULL) {
  91. pr_err("%s: pool region[%d] is invalid\n", __func__, region_id);
  92. return 0;
  93. }
  94. addr = gen_pool_alloc(pool, size);
  95. pr_debug("%s: Allocated %zu bytes from pool region[%d]: 0x%08lx\n", __func__, size, region_id, addr);
  96. return addr;
  97. }
  98. void rsvmem_pool_free(int region_id, size_t size, unsigned long addr)
  99. {
  100. struct gen_pool *pool;
  101. if (region_id < 0 || region_id >= MAX_RSVMEM_REGION_COUNT) {
  102. pr_err("%s: region_id(%d) is invalid\n", __func__, region_id);
  103. return;
  104. }
  105. pool = rsvmem_pool_regions[region_id].pool;
  106. if (pool == NULL) {
  107. pr_err("%s: rsvmem pool region[%d] is invalid\n", __func__, region_id);
  108. return;
  109. }
  110. gen_pool_free(pool, addr, size);
  111. }