Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

via_fb.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
  3. * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial portions
  14. * of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <assert.h>
  25. #include "via_context.h"
  26. #include "via_ioctl.h"
  27. #include "via_fb.h"
  28. #include "xf86drm.h"
  29. #include "imports.h"
  30. #include "simple_list.h"
  31. #include <sys/ioctl.h>
  32. GLboolean
  33. via_alloc_draw_buffer(struct via_context *vmesa, struct via_buffer *buf)
  34. {
  35. drm_via_mem_t mem;
  36. mem.context = vmesa->hHWContext;
  37. mem.size = buf->size;
  38. mem.type = VIA_MEM_VIDEO;
  39. mem.offset = 0;
  40. mem.index = 0;
  41. if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &mem))
  42. return GL_FALSE;
  43. buf->offset = mem.offset;
  44. buf->map = (char *)vmesa->driScreen->pFB + mem.offset;
  45. buf->index = mem.index;
  46. return GL_TRUE;
  47. }
  48. void
  49. via_free_draw_buffer(struct via_context *vmesa, struct via_buffer *buf)
  50. {
  51. drm_via_mem_t mem;
  52. if (!vmesa) return;
  53. mem.context = vmesa->hHWContext;
  54. mem.index = buf->index;
  55. mem.type = VIA_MEM_VIDEO;
  56. mem.offset = buf->offset;
  57. mem.size = buf->size;
  58. ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &mem);
  59. buf->map = NULL;
  60. }
  61. GLboolean
  62. via_alloc_dma_buffer(struct via_context *vmesa)
  63. {
  64. drm_via_dma_init_t init;
  65. vmesa->dma = (GLubyte *) malloc(VIA_DMA_BUFSIZ);
  66. /*
  67. * Check whether AGP DMA has been initialized.
  68. */
  69. memset(&init, 0, sizeof(init));
  70. init.func = VIA_DMA_INITIALIZED;
  71. vmesa->useAgp =
  72. ( 0 == drmCommandWrite(vmesa->driFd, DRM_VIA_DMA_INIT,
  73. &init, sizeof(init)));
  74. if (VIA_DEBUG & DEBUG_DMA) {
  75. if (vmesa->useAgp)
  76. fprintf(stderr, "unichrome_dri.so: Using AGP.\n");
  77. else
  78. fprintf(stderr, "unichrome_dri.so: Using PCI.\n");
  79. }
  80. return ((vmesa->dma) ? GL_TRUE : GL_FALSE);
  81. }
  82. void
  83. via_free_dma_buffer(struct via_context *vmesa)
  84. {
  85. if (!vmesa) return;
  86. free(vmesa->dma);
  87. vmesa->dma = 0;
  88. }
  89. /* These functions now allocate and free the via_tex_buffer struct as well:
  90. */
  91. struct via_tex_buffer *
  92. via_alloc_texture(struct via_context *vmesa,
  93. GLuint size,
  94. GLuint memType)
  95. {
  96. struct via_tex_buffer *t = CALLOC_STRUCT(via_tex_buffer);
  97. if (!t)
  98. goto cleanup;
  99. t->size = size;
  100. t->memType = memType;
  101. insert_at_tail(&vmesa->tex_image_list[memType], t);
  102. if (t->memType == VIA_MEM_AGP ||
  103. t->memType == VIA_MEM_VIDEO) {
  104. drm_via_mem_t fb;
  105. fb.context = vmesa->hHWContext;
  106. fb.size = t->size;
  107. fb.type = t->memType;
  108. fb.offset = 0;
  109. fb.index = 0;
  110. if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb) != 0 ||
  111. fb.index == 0)
  112. goto cleanup;
  113. if (0)
  114. fprintf(stderr, "offset %lx index %lx\n", fb.offset, fb.index);
  115. t->offset = fb.offset;
  116. t->index = fb.index;
  117. if (t->memType == VIA_MEM_AGP) {
  118. t->bufAddr = (GLubyte *)((GLuint)vmesa->viaScreen->agpLinearStart +
  119. fb.offset);
  120. t->texBase = (GLuint)vmesa->agpBase + fb.offset;
  121. }
  122. else {
  123. t->bufAddr = (GLubyte *)(fb.offset + (GLuint)vmesa->driScreen->pFB);
  124. t->texBase = fb.offset;
  125. }
  126. vmesa->total_alloc[t->memType] += t->size;
  127. return t;
  128. }
  129. else if (t->memType == VIA_MEM_SYSTEM) {
  130. t->bufAddr = _mesa_malloc(t->size);
  131. if (!t->bufAddr)
  132. goto cleanup;
  133. vmesa->total_alloc[t->memType] += t->size;
  134. return t;
  135. }
  136. cleanup:
  137. if (t) {
  138. remove_from_list(t);
  139. FREE(t);
  140. }
  141. return NULL;
  142. }
  143. static void
  144. via_do_free_texture(struct via_context *vmesa, struct via_tex_buffer *t)
  145. {
  146. drm_via_mem_t fb;
  147. remove_from_list( t );
  148. vmesa->total_alloc[t->memType] -= t->size;
  149. fb.context = vmesa->hHWContext;
  150. fb.index = t->index;
  151. fb.offset = t->offset;
  152. fb.type = t->memType;
  153. fb.size = t->size;
  154. if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
  155. fprintf(stderr, "via_free_texture fail\n");
  156. }
  157. FREE(t);
  158. }
  159. /* Release textures which were potentially still being referenced by
  160. * hardware at the time when they were originally freed.
  161. */
  162. void
  163. via_release_pending_textures( struct via_context *vmesa )
  164. {
  165. struct via_tex_buffer *s, *tmp;
  166. foreach_s( s, tmp, &vmesa->freed_tex_buffers ) {
  167. if (s->lastUsed < vmesa->lastBreadcrumbRead) {
  168. if (VIA_DEBUG & DEBUG_TEXTURE)
  169. fprintf(stderr, "%s: release tex sz %d lastUsed %x\n",
  170. __FUNCTION__, s->size, s->lastUsed);
  171. via_do_free_texture(vmesa, s);
  172. }
  173. }
  174. }
  175. void
  176. via_free_texture(struct via_context *vmesa, struct via_tex_buffer *t)
  177. {
  178. if (!t) {
  179. return;
  180. }
  181. else if (t->memType == VIA_MEM_SYSTEM) {
  182. remove_from_list(t);
  183. vmesa->total_alloc[t->memType] -= t->size;
  184. _mesa_free(t->bufAddr);
  185. _mesa_free(t);
  186. }
  187. else if (t->index && viaCheckBreadcrumb(vmesa, t->lastUsed)) {
  188. via_do_free_texture( vmesa, t );
  189. }
  190. else {
  191. /* Close current breadcrumb so that we can free this eventually:
  192. */
  193. if (t->lastUsed == vmesa->lastBreadcrumbWrite)
  194. viaEmitBreadcrumb(vmesa);
  195. move_to_tail( &vmesa->freed_tex_buffers, t );
  196. }
  197. }