sps_mem.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /**
  13. * Pipe-Memory allocation/free management.
  14. */
  15. #include <linux/types.h> /* u32 */
  16. #include <linux/kernel.h> /* pr_info() */
  17. #include <linux/io.h> /* ioremap() */
  18. #include <linux/mutex.h> /* mutex */
  19. #include <linux/list.h> /* list_head */
  20. #include <linux/genalloc.h> /* gen_pool_alloc() */
  21. #include <linux/errno.h> /* ENOMEM */
  22. #include "sps_bam.h"
  23. #include "spsi.h"
  24. static phys_addr_t iomem_phys;
  25. static void *iomem_virt;
  26. static u32 iomem_size;
  27. static u32 iomem_offset;
  28. static struct gen_pool *pool;
  29. static u32 nid = 0xaa;
  30. /* Debug */
  31. static u32 total_alloc;
  32. static u32 total_free;
  33. /**
  34. * Translate physical to virtual address
  35. *
  36. */
  37. void *spsi_get_mem_ptr(phys_addr_t phys_addr)
  38. {
  39. void *virt = NULL;
  40. if ((phys_addr >= iomem_phys) &&
  41. (phys_addr < (iomem_phys + iomem_size))) {
  42. virt = (u8 *) iomem_virt + (phys_addr - iomem_phys);
  43. } else {
  44. virt = phys_to_virt(phys_addr);
  45. SPS_ERR("sps:spsi_get_mem_ptr.invalid phys addr=0x%pa.",
  46. &phys_addr);
  47. }
  48. return virt;
  49. }
  50. /**
  51. * Allocate I/O (pipe) memory
  52. *
  53. */
  54. phys_addr_t sps_mem_alloc_io(u32 bytes)
  55. {
  56. phys_addr_t phys_addr = SPS_ADDR_INVALID;
  57. unsigned long virt_addr = 0;
  58. virt_addr = gen_pool_alloc(pool, bytes);
  59. if (virt_addr) {
  60. iomem_offset = virt_addr - (uintptr_t) iomem_virt;
  61. phys_addr = iomem_phys + iomem_offset;
  62. total_alloc += bytes;
  63. } else {
  64. SPS_ERR("sps:gen_pool_alloc %d bytes fail.", bytes);
  65. return SPS_ADDR_INVALID;
  66. }
  67. SPS_DBG2("sps:sps_mem_alloc_io.phys=%pa.virt=0x%lx.size=0x%x.",
  68. &phys_addr, virt_addr, bytes);
  69. return phys_addr;
  70. }
  71. /**
  72. * Free I/O memory
  73. *
  74. */
  75. void sps_mem_free_io(phys_addr_t phys_addr, u32 bytes)
  76. {
  77. unsigned long virt_addr = 0;
  78. iomem_offset = phys_addr - iomem_phys;
  79. virt_addr = (uintptr_t) iomem_virt + iomem_offset;
  80. SPS_DBG2("sps:sps_mem_free_io.phys=%pa.virt=0x%lx.size=0x%x.",
  81. &phys_addr, virt_addr, bytes);
  82. gen_pool_free(pool, virt_addr, bytes);
  83. total_free += bytes;
  84. }
  85. /**
  86. * Initialize driver memory module
  87. *
  88. */
  89. int sps_mem_init(phys_addr_t pipemem_phys_base, u32 pipemem_size)
  90. {
  91. int res;
  92. /* 2^8=128. The desc-fifo and data-fifo minimal allocation. */
  93. int min_alloc_order = 8;
  94. if ((d_type == 0) || (d_type == 2) || imem) {
  95. iomem_phys = pipemem_phys_base;
  96. iomem_size = pipemem_size;
  97. if (iomem_phys == 0) {
  98. SPS_ERR("sps:Invalid Pipe-Mem address");
  99. return SPS_ERROR;
  100. } else {
  101. iomem_virt = ioremap(iomem_phys, iomem_size);
  102. if (!iomem_virt) {
  103. SPS_ERR("sps:Failed to IO map pipe memory.\n");
  104. return -ENOMEM;
  105. }
  106. }
  107. iomem_offset = 0;
  108. SPS_DBG("sps:sps_mem_init.iomem_phys=%pa,iomem_virt=0x%p.",
  109. &iomem_phys, iomem_virt);
  110. }
  111. pool = gen_pool_create(min_alloc_order, nid);
  112. if (!pool) {
  113. SPS_ERR("sps:Failed to create a new memory pool.\n");
  114. return -ENOMEM;
  115. }
  116. if ((d_type == 0) || (d_type == 2) || imem) {
  117. res = gen_pool_add(pool, (uintptr_t)iomem_virt,
  118. iomem_size, nid);
  119. if (res)
  120. return res;
  121. }
  122. return 0;
  123. }
  124. /**
  125. * De-initialize driver memory module
  126. *
  127. */
  128. int sps_mem_de_init(void)
  129. {
  130. if (iomem_virt != NULL) {
  131. gen_pool_destroy(pool);
  132. pool = NULL;
  133. iounmap(iomem_virt);
  134. iomem_virt = NULL;
  135. }
  136. if (total_alloc == total_free)
  137. return 0;
  138. else {
  139. SPS_ERR("sps:sps_mem_de_init:some memory not free");
  140. return SPS_ERROR;
  141. }
  142. }