memalloc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2014 - 2021 VERISILICON
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (C) 2014 - 2021 VERISILICON
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program; if not, write to the Free Software Foundation,
  43. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  44. *
  45. *****************************************************************************
  46. *
  47. * Note: This software is released under dual MIT and GPL licenses. A
  48. * recipient may use this file under the terms of either the MIT license or
  49. * GPL License. If you wish to use only one license not the other, you can
  50. * indicate your decision by deleting one of the above license notices in your
  51. * version of this file.
  52. *
  53. *****************************************************************************/
  54. #include <asm/io.h>
  55. #include <linux/uaccess.h>
  56. #include <linux/errno.h>
  57. #include <linux/fs.h>
  58. #include <linux/init.h>
  59. #include <linux/ioport.h>
  60. #include <linux/kernel.h>
  61. #include <linux/list.h>
  62. #include <linux/mm.h>
  63. #include <linux/module.h>
  64. #include <linux/sched.h>
  65. #include <linux/slab.h>
  66. #include <linux/version.h>
  67. #include <linux/vmalloc.h>
  68. #include "memalloc.h"
  69. #ifndef HLINA_START_ADDRESS
  70. #define HLINA_START_ADDRESS 0x1e0000000
  71. #endif
  72. #ifndef HLINA_SIZE
  73. #define HLINA_SIZE 512
  74. #endif
  75. #ifndef HLINA_TRANSL_OFFSET
  76. #define HLINA_TRANSL_OFFSET 0x0
  77. #endif
  78. /* the size of chunk in MEMALLOC_DYNAMIC */
  79. #define CHUNK_SIZE (PAGE_SIZE * 4)
  80. /* memory size in MBs for MEMALLOC_DYNAMIC */
  81. unsigned long alloc_size = HLINA_SIZE;
  82. unsigned long alloc_base = HLINA_START_ADDRESS;
  83. /* user space SW will substract HLINA_TRANSL_OFFSET from the bus address
  84. * and decoder HW will use the result as the address translated base
  85. * address. The SW needs the original host memory bus address for memory
  86. * mapping to virtual address. */
  87. unsigned long addr_transl = HLINA_TRANSL_OFFSET;
  88. static int memalloc_major = 0; /* dynamic */
  89. /* module_param(name, type, perm) */
  90. module_param(alloc_size, ulong, 0);
  91. module_param(alloc_base, ulong, 0);
  92. module_param(addr_transl, ulong, 0);
  93. static DEFINE_SPINLOCK(mem_lock);
  94. typedef struct hlinc {
  95. u64 bus_address;
  96. u32 chunks_reserved;
  97. const struct file *filp; /* Client that allocated this chunk */
  98. } hlina_chunk;
  99. static hlina_chunk *hlina_chunks = NULL;
  100. static size_t chunks = 0;
  101. static int AllocMemory(unsigned long *busaddr, unsigned int size,
  102. const struct file *filp);
  103. static int FreeMemory(unsigned long busaddr, const struct file *filp);
  104. static void ResetMems(void);
  105. static long memalloc_ioctl(struct file *filp, unsigned int cmd,
  106. unsigned long arg) {
  107. int ret = 0;
  108. MemallocParams memparams;
  109. unsigned long busaddr;
  110. PDEBUG("ioctl cmd 0x%08x\n", cmd);
  111. /*
  112. * extract the type and number bitfields, and don't decode
  113. * wrong cmds: return ENOTTY (inappropriate ioctl) before access_ok()
  114. */
  115. if (_IOC_TYPE(cmd) != MEMALLOC_IOC_MAGIC) return -ENOTTY;
  116. if (_IOC_NR(cmd) > MEMALLOC_IOC_MAXNR) return -ENOTTY;
  117. if (_IOC_DIR(cmd) & _IOC_READ)
  118. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,0,0)
  119. ret = !access_ok(arg, _IOC_SIZE(cmd));
  120. #else
  121. ret = !access_ok(VERIFY_WRITE, arg, _IOC_SIZE(cmd));
  122. #endif
  123. else if (_IOC_DIR(cmd) & _IOC_WRITE)
  124. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,0,0)
  125. ret = !access_ok(arg, _IOC_SIZE(cmd));
  126. #else
  127. ret = !access_ok(VERIFY_READ, arg, _IOC_SIZE(cmd));
  128. #endif
  129. if (ret) return -EFAULT;
  130. switch (cmd) {
  131. case MEMALLOC_IOCHARDRESET:
  132. PDEBUG("HARDRESET\n");
  133. ResetMems();
  134. break;
  135. case MEMALLOC_IOCXGETBUFFER:
  136. PDEBUG("GETBUFFER");
  137. ret = copy_from_user(&memparams, (MemallocParams *)arg,
  138. sizeof(MemallocParams));
  139. if (ret) break;
  140. ret = AllocMemory(&memparams.bus_address, memparams.size, filp);
  141. memparams.translation_offset = addr_transl;
  142. ret |= copy_to_user((MemallocParams *)arg, &memparams,
  143. sizeof(MemallocParams));
  144. break;
  145. case MEMALLOC_IOCSFREEBUFFER:
  146. PDEBUG("FREEBUFFER\n");
  147. __get_user(busaddr, (unsigned long *)arg);
  148. ret = FreeMemory(busaddr, filp);
  149. break;
  150. }
  151. return ret ? -EFAULT : 0;
  152. }
  153. static int memalloc_open(struct inode *inode, struct file *filp) {
  154. PDEBUG("dev opened\n");
  155. return 0;
  156. }
  157. static int memalloc_release(struct inode *inode, struct file *filp) {
  158. int i = 0;
  159. spin_lock(&mem_lock);
  160. for (i = 0; i < chunks; i++) {
  161. if (hlina_chunks[i].filp == filp) {
  162. printk(KERN_WARNING "memalloc: Found unfreed memory at release time!\n");
  163. hlina_chunks[i].filp = NULL;
  164. hlina_chunks[i].chunks_reserved = 0;
  165. }
  166. }
  167. spin_unlock(&mem_lock);
  168. PDEBUG("dev closed\n");
  169. return 0;
  170. }
  171. void __exit memalloc_cleanup(void) {
  172. if (hlina_chunks != NULL) vfree(hlina_chunks);
  173. unregister_chrdev(memalloc_major, "memalloc");
  174. PDEBUG("module removed\n");
  175. return;
  176. }
  177. /* VFS methods */
  178. static struct file_operations memalloc_fops = {
  179. .owner = THIS_MODULE,
  180. .open = memalloc_open,
  181. .release = memalloc_release,
  182. .unlocked_ioctl = memalloc_ioctl
  183. };
  184. int __init memalloc_init(void) {
  185. int result;
  186. PDEBUG("module init\n");
  187. printk("memalloc: Linear Memory Allocator\n");
  188. printk("memalloc: Linear memory base = 0x%llx\n", alloc_base);
  189. chunks = (alloc_size * 1024 * 1024) / CHUNK_SIZE;
  190. printk(KERN_INFO
  191. "memalloc: Total size %ld MB; %d chunks"
  192. " of size %lu\n",
  193. alloc_size, (int)chunks, CHUNK_SIZE);
  194. hlina_chunks = (hlina_chunk *)vmalloc(chunks * sizeof(hlina_chunk));
  195. if (hlina_chunks == NULL) {
  196. printk(KERN_ERR "memalloc: cannot allocate hlina_chunks\n");
  197. result = -ENOMEM;
  198. goto err;
  199. }
  200. result = register_chrdev(memalloc_major, "memalloc", &memalloc_fops);
  201. if (result < 0) {
  202. PDEBUG("memalloc: unable to get major %d\n", memalloc_major);
  203. goto err;
  204. } else if (result != 0) {/* this is for dynamic major */
  205. memalloc_major = result;
  206. }
  207. ResetMems();
  208. return 0;
  209. err:
  210. if (hlina_chunks != NULL) vfree(hlina_chunks);
  211. return result;
  212. }
  213. /* Cycle through the buffers we have, give the first free one */
  214. static int AllocMemory(unsigned long *busaddr, unsigned int size,
  215. const struct file *filp) {
  216. int i = 0;
  217. int j = 0;
  218. unsigned int skip_chunks = 0;
  219. /* calculate how many chunks we need; round up to chunk boundary */
  220. unsigned int alloc_chunks = (size + CHUNK_SIZE - 1) / CHUNK_SIZE;
  221. *busaddr = 0;
  222. spin_lock(&mem_lock);
  223. /* run through the chunk table */
  224. for (i = 0; i < chunks;) {
  225. skip_chunks = 0;
  226. /* if this chunk is available */
  227. if (!hlina_chunks[i].chunks_reserved) {
  228. /* check that there is enough memory left */
  229. if (i + alloc_chunks > chunks) break;
  230. /* check that there is enough consecutive chunks available */
  231. for (j = i; j < i + alloc_chunks; j++) {
  232. if (hlina_chunks[j].chunks_reserved) {
  233. skip_chunks = 1;
  234. /* skip the used chunks */
  235. i = j + hlina_chunks[j].chunks_reserved;
  236. break;
  237. }
  238. }
  239. /* if enough free memory found */
  240. if (!skip_chunks) {
  241. *busaddr = hlina_chunks[i].bus_address;
  242. hlina_chunks[i].filp = filp;
  243. hlina_chunks[i].chunks_reserved = alloc_chunks;
  244. break;
  245. }
  246. } else {
  247. /* skip the used chunks */
  248. i += hlina_chunks[i].chunks_reserved;
  249. }
  250. }
  251. spin_unlock(&mem_lock);
  252. if (*busaddr == 0) {
  253. printk("memalloc: Allocation FAILED: size = %ld\n", size);
  254. return -EFAULT;
  255. } else {
  256. PDEBUG("MEMALLOC OK: size: %d, reserved: %ld\n", size,
  257. alloc_chunks * CHUNK_SIZE);
  258. }
  259. return 0;
  260. }
  261. /* Free a buffer based on bus address */
  262. static int FreeMemory(unsigned long busaddr, const struct file *filp) {
  263. int i = 0;
  264. spin_lock(&mem_lock);
  265. for (i = 0; i < chunks; i++) {
  266. /* user space SW has stored the translated bus address, add addr_transl to
  267. * translate back to our address space */
  268. if (hlina_chunks[i].bus_address == busaddr + addr_transl) {
  269. if (hlina_chunks[i].filp == filp) {
  270. hlina_chunks[i].filp = NULL;
  271. hlina_chunks[i].chunks_reserved = 0;
  272. } else {
  273. printk(KERN_WARNING "memalloc: Owner mismatch while freeing memory!\n");
  274. }
  275. break;
  276. }
  277. }
  278. spin_unlock(&mem_lock);
  279. return 0;
  280. }
  281. /* Reset "used" status */
  282. static void ResetMems(void) {
  283. int i = 0;
  284. unsigned long ba = alloc_base;
  285. spin_lock(&mem_lock);
  286. for (i = 0; i < chunks; i++) {
  287. hlina_chunks[i].bus_address = ba;
  288. hlina_chunks[i].filp = NULL;
  289. hlina_chunks[i].chunks_reserved = 0;
  290. ba += CHUNK_SIZE;
  291. }
  292. spin_unlock(&mem_lock);
  293. }
  294. module_init(memalloc_init);
  295. module_exit(memalloc_cleanup);
  296. /* module description */
  297. MODULE_LICENSE("GPL");
  298. MODULE_AUTHOR("Verisilicon");
  299. MODULE_DESCRIPTION("Linear RAM allocation");