via_ds.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
  3. * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
  4. * Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * 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 NON-INFRINGEMENT. IN NO EVENT SHALL
  20. * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/delay.h>
  27. #include <linux/errno.h>
  28. #include <linux/kernel.h>
  29. #include <linux/slab.h>
  30. #include <linux/poll.h>
  31. #include <linux/pci.h>
  32. #include <asm/io.h>
  33. #include "via_ds.h"
  34. extern unsigned int VIA_DEBUG;
  35. set_t *via_setInit(void)
  36. {
  37. int i;
  38. set_t *set;
  39. set = (set_t *) drm_alloc(sizeof(set_t), DRM_MEM_DRIVER);
  40. for (i = 0; i < SET_SIZE; i++) {
  41. set->list[i].free_next = i + 1;
  42. set->list[i].alloc_next = -1;
  43. }
  44. set->list[SET_SIZE - 1].free_next = -1;
  45. set->free = 0;
  46. set->alloc = -1;
  47. set->trace = -1;
  48. return set;
  49. }
  50. int via_setAdd(set_t * set, ITEM_TYPE item)
  51. {
  52. int free = set->free;
  53. if (free != -1) {
  54. set->list[free].val = item;
  55. set->free = set->list[free].free_next;
  56. } else {
  57. return 0;
  58. }
  59. set->list[free].alloc_next = set->alloc;
  60. set->alloc = free;
  61. set->list[free].free_next = -1;
  62. return 1;
  63. }
  64. int via_setDel(set_t * set, ITEM_TYPE item)
  65. {
  66. int alloc = set->alloc;
  67. int prev = -1;
  68. while (alloc != -1) {
  69. if (set->list[alloc].val == item) {
  70. if (prev != -1)
  71. set->list[prev].alloc_next =
  72. set->list[alloc].alloc_next;
  73. else
  74. set->alloc = set->list[alloc].alloc_next;
  75. break;
  76. }
  77. prev = alloc;
  78. alloc = set->list[alloc].alloc_next;
  79. }
  80. if (alloc == -1)
  81. return 0;
  82. set->list[alloc].free_next = set->free;
  83. set->free = alloc;
  84. set->list[alloc].alloc_next = -1;
  85. return 1;
  86. }
  87. /* setFirst -> setAdd -> setNext is wrong */
  88. int via_setFirst(set_t * set, ITEM_TYPE * item)
  89. {
  90. if (set->alloc == -1)
  91. return 0;
  92. *item = set->list[set->alloc].val;
  93. set->trace = set->list[set->alloc].alloc_next;
  94. return 1;
  95. }
  96. int via_setNext(set_t * set, ITEM_TYPE * item)
  97. {
  98. if (set->trace == -1)
  99. return 0;
  100. *item = set->list[set->trace].val;
  101. set->trace = set->list[set->trace].alloc_next;
  102. return 1;
  103. }
  104. int via_setDestroy(set_t * set)
  105. {
  106. drm_free(set, sizeof(set_t), DRM_MEM_DRIVER);
  107. return 1;
  108. }
  109. #define ISFREE(bptr) ((bptr)->free)
  110. #define fprintf(fmt, arg...) do{}while(0)
  111. memHeap_t *via_mmInit(int ofs, int size)
  112. {
  113. PMemBlock blocks;
  114. if (size <= 0)
  115. return NULL;
  116. blocks = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock), DRM_MEM_DRIVER);
  117. if (blocks) {
  118. blocks->ofs = ofs;
  119. blocks->size = size;
  120. blocks->free = 1;
  121. return (memHeap_t *) blocks;
  122. } else
  123. return NULL;
  124. }
  125. static TMemBlock *SliceBlock(TMemBlock * p,
  126. int startofs, int size,
  127. int reserved, int alignment)
  128. {
  129. TMemBlock *newblock;
  130. /* break left */
  131. if (startofs > p->ofs) {
  132. newblock =
  133. (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
  134. DRM_MEM_DRIVER);
  135. newblock->ofs = startofs;
  136. newblock->size = p->size - (startofs - p->ofs);
  137. newblock->free = 1;
  138. newblock->next = p->next;
  139. p->size -= newblock->size;
  140. p->next = newblock;
  141. p = newblock;
  142. }
  143. /* break right */
  144. if (size < p->size) {
  145. newblock =
  146. (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
  147. DRM_MEM_DRIVER);
  148. newblock->ofs = startofs + size;
  149. newblock->size = p->size - size;
  150. newblock->free = 1;
  151. newblock->next = p->next;
  152. p->size = size;
  153. p->next = newblock;
  154. }
  155. /* p = middle block */
  156. p->align = alignment;
  157. p->free = 0;
  158. p->reserved = reserved;
  159. return p;
  160. }
  161. PMemBlock via_mmAllocMem(memHeap_t * heap, int size, int align2,
  162. int startSearch)
  163. {
  164. int mask, startofs, endofs;
  165. TMemBlock *p;
  166. if (!heap || align2 < 0 || size <= 0)
  167. return NULL;
  168. mask = (1 << align2) - 1;
  169. startofs = 0;
  170. p = (TMemBlock *) heap;
  171. while (p) {
  172. if (ISFREE(p)) {
  173. startofs = (p->ofs + mask) & ~mask;
  174. if (startofs < startSearch)
  175. startofs = startSearch;
  176. endofs = startofs + size;
  177. if (endofs <= (p->ofs + p->size))
  178. break;
  179. }
  180. p = p->next;
  181. }
  182. if (!p)
  183. return NULL;
  184. p = SliceBlock(p, startofs, size, 0, mask + 1);
  185. p->heap = heap;
  186. return p;
  187. }
  188. static __inline__ int Join2Blocks(TMemBlock * p)
  189. {
  190. if (p->free && p->next && p->next->free) {
  191. TMemBlock *q = p->next;
  192. p->size += q->size;
  193. p->next = q->next;
  194. drm_free(q, sizeof(TMemBlock), DRM_MEM_DRIVER);
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. int via_mmFreeMem(PMemBlock b)
  200. {
  201. TMemBlock *p, *prev;
  202. if (!b)
  203. return 0;
  204. if (!b->heap) {
  205. fprintf(stderr, "no heap\n");
  206. return -1;
  207. }
  208. p = b->heap;
  209. prev = NULL;
  210. while (p && p != b) {
  211. prev = p;
  212. p = p->next;
  213. }
  214. if (!p || p->free || p->reserved) {
  215. if (!p)
  216. fprintf(stderr, "block not found in heap\n");
  217. else if (p->free)
  218. fprintf(stderr, "block already free\n");
  219. else
  220. fprintf(stderr, "block is reserved\n");
  221. return -1;
  222. }
  223. p->free = 1;
  224. Join2Blocks(p);
  225. if (prev)
  226. Join2Blocks(prev);
  227. return 0;
  228. }