tcm-sita.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * SImple Tiler Allocator (SiTA): 2D and 1D allocation(reservation) algorithm
  3. *
  4. * Authors: Ravi Ramachandra <r.ramachandra@ti.com>,
  5. * Lajos Molnar <molnar@ti.com>
  6. * Andy Gross <andy.gross@ti.com>
  7. *
  8. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  9. *
  10. * This package is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/errno.h>
  22. #include <linux/sched.h>
  23. #include <linux/wait.h>
  24. #include <linux/bitmap.h>
  25. #include <linux/slab.h>
  26. #include "tcm.h"
  27. static unsigned long mask[8];
  28. /*
  29. * pos position in bitmap
  30. * w width in slots
  31. * h height in slots
  32. * map ptr to bitmap
  33. * stride slots in a row
  34. */
  35. static void free_slots(unsigned long pos, u16 w, u16 h,
  36. unsigned long *map, u16 stride)
  37. {
  38. int i;
  39. for (i = 0; i < h; i++, pos += stride)
  40. bitmap_clear(map, pos, w);
  41. }
  42. /*
  43. * w width in slots
  44. * pos ptr to position
  45. * map ptr to bitmap
  46. * num_bits number of bits in bitmap
  47. */
  48. static int r2l_b2t_1d(u16 w, unsigned long *pos, unsigned long *map,
  49. size_t num_bits)
  50. {
  51. unsigned long search_count = 0;
  52. unsigned long bit;
  53. bool area_found = false;
  54. *pos = num_bits - w;
  55. while (search_count < num_bits) {
  56. bit = find_next_bit(map, num_bits, *pos);
  57. if (bit - *pos >= w) {
  58. /* found a long enough free area */
  59. bitmap_set(map, *pos, w);
  60. area_found = true;
  61. break;
  62. }
  63. search_count = num_bits - bit + w;
  64. *pos = bit - w;
  65. }
  66. return (area_found) ? 0 : -ENOMEM;
  67. }
  68. /*
  69. * w = width in slots
  70. * h = height in slots
  71. * a = align in slots (mask, 2^n-1, 0 is unaligned)
  72. * offset = offset in bytes from 4KiB
  73. * pos = position in bitmap for buffer
  74. * map = bitmap ptr
  75. * num_bits = size of bitmap
  76. * stride = bits in one row of container
  77. */
  78. static int l2r_t2b(u16 w, u16 h, u16 a, s16 offset,
  79. unsigned long *pos, unsigned long slot_bytes,
  80. unsigned long *map, size_t num_bits, size_t slot_stride)
  81. {
  82. int i;
  83. unsigned long index;
  84. bool area_free = false;
  85. unsigned long slots_per_band = PAGE_SIZE / slot_bytes;
  86. unsigned long bit_offset = (offset > 0) ? offset / slot_bytes : 0;
  87. unsigned long curr_bit = bit_offset;
  88. /* reset alignment to 1 if we are matching a specific offset */
  89. /* adjust alignment - 1 to get to the format expected in bitmaps */
  90. a = (offset > 0) ? 0 : a - 1;
  91. /* FIXME Return error if slots_per_band > stride */
  92. while (curr_bit < num_bits) {
  93. *pos = bitmap_find_next_zero_area(map, num_bits, curr_bit, w,
  94. a);
  95. /* skip forward if we are not at right offset */
  96. if (bit_offset > 0 && (*pos % slots_per_band != bit_offset)) {
  97. curr_bit = ALIGN(*pos, slots_per_band) + bit_offset;
  98. continue;
  99. }
  100. /* skip forward to next row if we overlap end of row */
  101. if ((*pos % slot_stride) + w > slot_stride) {
  102. curr_bit = ALIGN(*pos, slot_stride) + bit_offset;
  103. continue;
  104. }
  105. /* TODO: Handle overlapping 4K boundaries */
  106. /* break out of look if we will go past end of container */
  107. if ((*pos + slot_stride * h) > num_bits)
  108. break;
  109. /* generate mask that represents out matching pattern */
  110. bitmap_clear(mask, 0, slot_stride);
  111. bitmap_set(mask, (*pos % BITS_PER_LONG), w);
  112. /* assume the area is free until we find an overlap */
  113. area_free = true;
  114. /* check subsequent rows to see if complete area is free */
  115. for (i = 1; i < h; i++) {
  116. index = *pos / BITS_PER_LONG + i * 8;
  117. if (bitmap_intersects(&map[index], mask,
  118. (*pos % BITS_PER_LONG) + w)) {
  119. area_free = false;
  120. break;
  121. }
  122. }
  123. if (area_free)
  124. break;
  125. /* go forward past this match */
  126. if (bit_offset > 0)
  127. curr_bit = ALIGN(*pos, slots_per_band) + bit_offset;
  128. else
  129. curr_bit = *pos + a + 1;
  130. }
  131. if (area_free) {
  132. /* set area as in-use. iterate over rows */
  133. for (i = 0, index = *pos; i < h; i++, index += slot_stride)
  134. bitmap_set(map, index, w);
  135. }
  136. return (area_free) ? 0 : -ENOMEM;
  137. }
  138. static s32 sita_reserve_1d(struct tcm *tcm, u32 num_slots,
  139. struct tcm_area *area)
  140. {
  141. unsigned long pos;
  142. int ret;
  143. spin_lock(&(tcm->lock));
  144. ret = r2l_b2t_1d(num_slots, &pos, tcm->bitmap, tcm->map_size);
  145. if (!ret) {
  146. area->p0.x = pos % tcm->width;
  147. area->p0.y = pos / tcm->width;
  148. area->p1.x = (pos + num_slots - 1) % tcm->width;
  149. area->p1.y = (pos + num_slots - 1) / tcm->width;
  150. }
  151. spin_unlock(&(tcm->lock));
  152. return ret;
  153. }
  154. static s32 sita_reserve_2d(struct tcm *tcm, u16 h, u16 w, u16 align,
  155. s16 offset, u16 slot_bytes,
  156. struct tcm_area *area)
  157. {
  158. unsigned long pos;
  159. int ret;
  160. spin_lock(&(tcm->lock));
  161. ret = l2r_t2b(w, h, align, offset, &pos, slot_bytes, tcm->bitmap,
  162. tcm->map_size, tcm->width);
  163. if (!ret) {
  164. area->p0.x = pos % tcm->width;
  165. area->p0.y = pos / tcm->width;
  166. area->p1.x = area->p0.x + w - 1;
  167. area->p1.y = area->p0.y + h - 1;
  168. }
  169. spin_unlock(&(tcm->lock));
  170. return ret;
  171. }
  172. static void sita_deinit(struct tcm *tcm)
  173. {
  174. kfree(tcm);
  175. }
  176. static s32 sita_free(struct tcm *tcm, struct tcm_area *area)
  177. {
  178. unsigned long pos;
  179. u16 w, h;
  180. pos = area->p0.x + area->p0.y * tcm->width;
  181. if (area->is2d) {
  182. w = area->p1.x - area->p0.x + 1;
  183. h = area->p1.y - area->p0.y + 1;
  184. } else {
  185. w = area->p1.x + area->p1.y * tcm->width - pos + 1;
  186. h = 1;
  187. }
  188. spin_lock(&(tcm->lock));
  189. free_slots(pos, w, h, tcm->bitmap, tcm->width);
  190. spin_unlock(&(tcm->lock));
  191. return 0;
  192. }
  193. struct tcm *sita_init(u16 width, u16 height)
  194. {
  195. struct tcm *tcm;
  196. size_t map_size = BITS_TO_LONGS(width*height) * sizeof(unsigned long);
  197. if (width == 0 || height == 0)
  198. return NULL;
  199. tcm = kzalloc(sizeof(*tcm) + map_size, GFP_KERNEL);
  200. if (!tcm)
  201. goto error;
  202. /* Updating the pointers to SiTA implementation APIs */
  203. tcm->height = height;
  204. tcm->width = width;
  205. tcm->reserve_2d = sita_reserve_2d;
  206. tcm->reserve_1d = sita_reserve_1d;
  207. tcm->free = sita_free;
  208. tcm->deinit = sita_deinit;
  209. spin_lock_init(&tcm->lock);
  210. tcm->bitmap = (unsigned long *)(tcm + 1);
  211. bitmap_clear(tcm->bitmap, 0, width*height);
  212. tcm->map_size = width*height;
  213. return tcm;
  214. error:
  215. kfree(tcm);
  216. return NULL;
  217. }