bufctx.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include <stdbool.h>
  28. #include <assert.h>
  29. #include <errno.h>
  30. #include "libdrm_lists.h"
  31. #include "nouveau.h"
  32. #include "private.h"
  33. struct nouveau_bufref_priv {
  34. struct nouveau_bufref base;
  35. struct nouveau_bufref_priv *next;
  36. struct nouveau_bufctx *bufctx;
  37. };
  38. struct nouveau_bufbin_priv {
  39. struct nouveau_bufref_priv *list;
  40. int relocs;
  41. };
  42. struct nouveau_bufctx_priv {
  43. struct nouveau_bufctx base;
  44. struct nouveau_bufref_priv *free;
  45. int nr_bins;
  46. struct nouveau_bufbin_priv bins[];
  47. };
  48. static inline struct nouveau_bufctx_priv *
  49. nouveau_bufctx(struct nouveau_bufctx *bctx)
  50. {
  51. return (struct nouveau_bufctx_priv *)bctx;
  52. }
  53. drm_public int
  54. nouveau_bufctx_new(struct nouveau_client *client, int bins,
  55. struct nouveau_bufctx **pbctx)
  56. {
  57. struct nouveau_bufctx_priv *priv;
  58. priv = calloc(1, sizeof(*priv) + sizeof(priv->bins[0]) * bins);
  59. if (priv) {
  60. DRMINITLISTHEAD(&priv->base.head);
  61. DRMINITLISTHEAD(&priv->base.pending);
  62. DRMINITLISTHEAD(&priv->base.current);
  63. priv->base.client = client;
  64. priv->nr_bins = bins;
  65. *pbctx = &priv->base;
  66. return 0;
  67. }
  68. return -ENOMEM;
  69. }
  70. drm_public void
  71. nouveau_bufctx_del(struct nouveau_bufctx **pbctx)
  72. {
  73. struct nouveau_bufctx_priv *pctx = nouveau_bufctx(*pbctx);
  74. struct nouveau_bufref_priv *pref;
  75. if (pctx) {
  76. while (pctx->nr_bins--)
  77. nouveau_bufctx_reset(&pctx->base, pctx->nr_bins);
  78. while ((pref = pctx->free)) {
  79. pctx->free = pref->next;
  80. free(pref);
  81. }
  82. free(pctx);
  83. *pbctx = NULL;
  84. }
  85. }
  86. drm_public void
  87. nouveau_bufctx_reset(struct nouveau_bufctx *bctx, int bin)
  88. {
  89. struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
  90. struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
  91. struct nouveau_bufref_priv *pref;
  92. while ((pref = pbin->list)) {
  93. DRMLISTDELINIT(&pref->base.thead);
  94. pbin->list = pref->next;
  95. pref->next = pctx->free;
  96. pctx->free = pref;
  97. }
  98. bctx->relocs -= pbin->relocs;
  99. pbin->relocs = 0;
  100. }
  101. drm_public struct nouveau_bufref *
  102. nouveau_bufctx_refn(struct nouveau_bufctx *bctx, int bin,
  103. struct nouveau_bo *bo, uint32_t flags)
  104. {
  105. struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
  106. struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
  107. struct nouveau_bufref_priv *pref = pctx->free;
  108. if (!pref)
  109. pref = malloc(sizeof(*pref));
  110. else
  111. pctx->free = pref->next;
  112. if (pref) {
  113. pref->base.bo = bo;
  114. pref->base.flags = flags;
  115. pref->base.packet = 0;
  116. DRMLISTADDTAIL(&pref->base.thead, &bctx->pending);
  117. pref->bufctx = bctx;
  118. pref->next = pbin->list;
  119. pbin->list = pref;
  120. }
  121. return &pref->base;
  122. }
  123. drm_public struct nouveau_bufref *
  124. nouveau_bufctx_mthd(struct nouveau_bufctx *bctx, int bin, uint32_t packet,
  125. struct nouveau_bo *bo, uint64_t data, uint32_t flags,
  126. uint32_t vor, uint32_t tor)
  127. {
  128. struct nouveau_bufctx_priv *pctx = nouveau_bufctx(bctx);
  129. struct nouveau_bufbin_priv *pbin = &pctx->bins[bin];
  130. struct nouveau_bufref *bref = nouveau_bufctx_refn(bctx, bin, bo, flags);
  131. if (bref) {
  132. bref->packet = packet;
  133. bref->data = data;
  134. bref->vor = vor;
  135. bref->tor = tor;
  136. pbin->relocs++;
  137. bctx->relocs++;
  138. }
  139. return bref;
  140. }