wcnss_prealloc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (c) 2012, 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. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/err.h>
  15. #include <linux/wcnss_wlan.h>
  16. #include <linux/spinlock.h>
  17. static DEFINE_SPINLOCK(alloc_lock);
  18. struct wcnss_prealloc {
  19. int occupied;
  20. unsigned int size;
  21. void *ptr;
  22. };
  23. /* pre-alloced mem for WLAN driver */
  24. static struct wcnss_prealloc wcnss_allocs[] = {
  25. {0, 8 * 1024, NULL},
  26. {0, 8 * 1024, NULL},
  27. {0, 8 * 1024, NULL},
  28. {0, 8 * 1024, NULL},
  29. {0, 32 * 1024, NULL},
  30. {0, 32 * 1024, NULL},
  31. {0, 32 * 1024, NULL},
  32. {0, 32 * 1024, NULL},
  33. {0, 32 * 1024, NULL},
  34. {0, 32 * 1024, NULL},
  35. {0, 32 * 1024, NULL},
  36. {0, 64 * 1024, NULL},
  37. {0, 64 * 1024, NULL},
  38. };
  39. int wcnss_prealloc_init(void)
  40. {
  41. int i;
  42. for (i = 0; i < ARRAY_SIZE(wcnss_allocs); i++) {
  43. wcnss_allocs[i].occupied = 0;
  44. wcnss_allocs[i].ptr = kmalloc(wcnss_allocs[i].size, GFP_KERNEL);
  45. if (wcnss_allocs[i].ptr == NULL)
  46. return -ENOMEM;
  47. }
  48. return 0;
  49. }
  50. void wcnss_prealloc_deinit(void)
  51. {
  52. int i = 0;
  53. for (i = 0; i < ARRAY_SIZE(wcnss_allocs); i++) {
  54. kfree(wcnss_allocs[i].ptr);
  55. wcnss_allocs[i].ptr = NULL;
  56. }
  57. }
  58. void *wcnss_prealloc_get(unsigned int size)
  59. {
  60. int i = 0;
  61. unsigned long flags;
  62. spin_lock_irqsave(&alloc_lock, flags);
  63. for (i = 0; i < ARRAY_SIZE(wcnss_allocs); i++) {
  64. if (wcnss_allocs[i].occupied)
  65. continue;
  66. if (wcnss_allocs[i].size > size) {
  67. /* we found the slot */
  68. wcnss_allocs[i].occupied = 1;
  69. spin_unlock_irqrestore(&alloc_lock, flags);
  70. return wcnss_allocs[i].ptr;
  71. }
  72. }
  73. spin_unlock_irqrestore(&alloc_lock, flags);
  74. pr_err("wcnss: %s: prealloc not available for size: %d\n",
  75. __func__, size);
  76. return NULL;
  77. }
  78. EXPORT_SYMBOL(wcnss_prealloc_get);
  79. int wcnss_prealloc_put(void *ptr)
  80. {
  81. int i = 0;
  82. unsigned long flags;
  83. spin_lock_irqsave(&alloc_lock, flags);
  84. for (i = 0; i < ARRAY_SIZE(wcnss_allocs); i++) {
  85. if (wcnss_allocs[i].ptr == ptr) {
  86. wcnss_allocs[i].occupied = 0;
  87. spin_unlock_irqrestore(&alloc_lock, flags);
  88. return 1;
  89. }
  90. }
  91. spin_unlock_irqrestore(&alloc_lock, flags);
  92. return 0;
  93. }
  94. EXPORT_SYMBOL(wcnss_prealloc_put);