vbva_base.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: MIT
  2. /* Copyright (C) 2006-2017 Oracle Corporation */
  3. #include <linux/vbox_err.h>
  4. #include "vbox_drv.h"
  5. #include "vboxvideo_guest.h"
  6. #include "hgsmi_channels.h"
  7. /*
  8. * There is a hardware ring buffer in the graphics device video RAM, formerly
  9. * in the VBox VMMDev PCI memory space.
  10. * All graphics commands go there serialized by vbva_buffer_begin_update.
  11. * and vbva_buffer_end_update.
  12. *
  13. * free_offset is writing position. data_offset is reading position.
  14. * free_offset == data_offset means buffer is empty.
  15. * There must be always gap between data_offset and free_offset when data
  16. * are in the buffer.
  17. * Guest only changes free_offset, host changes data_offset.
  18. */
  19. static u32 vbva_buffer_available(const struct vbva_buffer *vbva)
  20. {
  21. s32 diff = vbva->data_offset - vbva->free_offset;
  22. return diff > 0 ? diff : vbva->data_len + diff;
  23. }
  24. static void vbva_buffer_place_data_at(struct vbva_buf_ctx *vbva_ctx,
  25. const void *p, u32 len, u32 offset)
  26. {
  27. struct vbva_buffer *vbva = vbva_ctx->vbva;
  28. u32 bytes_till_boundary = vbva->data_len - offset;
  29. u8 *dst = &vbva->data[offset];
  30. s32 diff = len - bytes_till_boundary;
  31. if (diff <= 0) {
  32. /* Chunk will not cross buffer boundary. */
  33. memcpy(dst, p, len);
  34. } else {
  35. /* Chunk crosses buffer boundary. */
  36. memcpy(dst, p, bytes_till_boundary);
  37. memcpy(&vbva->data[0], (u8 *)p + bytes_till_boundary, diff);
  38. }
  39. }
  40. static void vbva_buffer_flush(struct gen_pool *ctx)
  41. {
  42. struct vbva_flush *p;
  43. p = hgsmi_buffer_alloc(ctx, sizeof(*p), HGSMI_CH_VBVA, VBVA_FLUSH);
  44. if (!p)
  45. return;
  46. p->reserved = 0;
  47. hgsmi_buffer_submit(ctx, p);
  48. hgsmi_buffer_free(ctx, p);
  49. }
  50. bool vbva_write(struct vbva_buf_ctx *vbva_ctx, struct gen_pool *ctx,
  51. const void *p, u32 len)
  52. {
  53. struct vbva_record *record;
  54. struct vbva_buffer *vbva;
  55. u32 available;
  56. vbva = vbva_ctx->vbva;
  57. record = vbva_ctx->record;
  58. if (!vbva || vbva_ctx->buffer_overflow ||
  59. !record || !(record->len_and_flags & VBVA_F_RECORD_PARTIAL))
  60. return false;
  61. available = vbva_buffer_available(vbva);
  62. while (len > 0) {
  63. u32 chunk = len;
  64. if (chunk >= available) {
  65. vbva_buffer_flush(ctx);
  66. available = vbva_buffer_available(vbva);
  67. }
  68. if (chunk >= available) {
  69. if (WARN_ON(available <= vbva->partial_write_tresh)) {
  70. vbva_ctx->buffer_overflow = true;
  71. return false;
  72. }
  73. chunk = available - vbva->partial_write_tresh;
  74. }
  75. vbva_buffer_place_data_at(vbva_ctx, p, chunk,
  76. vbva->free_offset);
  77. vbva->free_offset = (vbva->free_offset + chunk) %
  78. vbva->data_len;
  79. record->len_and_flags += chunk;
  80. available -= chunk;
  81. len -= chunk;
  82. p += chunk;
  83. }
  84. return true;
  85. }
  86. static bool vbva_inform_host(struct vbva_buf_ctx *vbva_ctx,
  87. struct gen_pool *ctx, s32 screen, bool enable)
  88. {
  89. struct vbva_enable_ex *p;
  90. bool ret;
  91. p = hgsmi_buffer_alloc(ctx, sizeof(*p), HGSMI_CH_VBVA, VBVA_ENABLE);
  92. if (!p)
  93. return false;
  94. p->base.flags = enable ? VBVA_F_ENABLE : VBVA_F_DISABLE;
  95. p->base.offset = vbva_ctx->buffer_offset;
  96. p->base.result = VERR_NOT_SUPPORTED;
  97. if (screen >= 0) {
  98. p->base.flags |= VBVA_F_EXTENDED | VBVA_F_ABSOFFSET;
  99. p->screen_id = screen;
  100. }
  101. hgsmi_buffer_submit(ctx, p);
  102. if (enable)
  103. ret = p->base.result >= 0;
  104. else
  105. ret = true;
  106. hgsmi_buffer_free(ctx, p);
  107. return ret;
  108. }
  109. bool vbva_enable(struct vbva_buf_ctx *vbva_ctx, struct gen_pool *ctx,
  110. struct vbva_buffer *vbva, s32 screen)
  111. {
  112. bool ret = false;
  113. memset(vbva, 0, sizeof(*vbva));
  114. vbva->partial_write_tresh = 256;
  115. vbva->data_len = vbva_ctx->buffer_length - sizeof(struct vbva_buffer);
  116. vbva_ctx->vbva = vbva;
  117. ret = vbva_inform_host(vbva_ctx, ctx, screen, true);
  118. if (!ret)
  119. vbva_disable(vbva_ctx, ctx, screen);
  120. return ret;
  121. }
  122. void vbva_disable(struct vbva_buf_ctx *vbva_ctx, struct gen_pool *ctx,
  123. s32 screen)
  124. {
  125. vbva_ctx->buffer_overflow = false;
  126. vbva_ctx->record = NULL;
  127. vbva_ctx->vbva = NULL;
  128. vbva_inform_host(vbva_ctx, ctx, screen, false);
  129. }
  130. bool vbva_buffer_begin_update(struct vbva_buf_ctx *vbva_ctx,
  131. struct gen_pool *ctx)
  132. {
  133. struct vbva_record *record;
  134. u32 next;
  135. if (!vbva_ctx->vbva ||
  136. !(vbva_ctx->vbva->host_flags.host_events & VBVA_F_MODE_ENABLED))
  137. return false;
  138. WARN_ON(vbva_ctx->buffer_overflow || vbva_ctx->record);
  139. next = (vbva_ctx->vbva->record_free_index + 1) % VBVA_MAX_RECORDS;
  140. /* Flush if all slots in the records queue are used */
  141. if (next == vbva_ctx->vbva->record_first_index)
  142. vbva_buffer_flush(ctx);
  143. /* If even after flush there is no place then fail the request */
  144. if (next == vbva_ctx->vbva->record_first_index)
  145. return false;
  146. record = &vbva_ctx->vbva->records[vbva_ctx->vbva->record_free_index];
  147. record->len_and_flags = VBVA_F_RECORD_PARTIAL;
  148. vbva_ctx->vbva->record_free_index = next;
  149. /* Remember which record we are using. */
  150. vbva_ctx->record = record;
  151. return true;
  152. }
  153. void vbva_buffer_end_update(struct vbva_buf_ctx *vbva_ctx)
  154. {
  155. struct vbva_record *record = vbva_ctx->record;
  156. WARN_ON(!vbva_ctx->vbva || !record ||
  157. !(record->len_and_flags & VBVA_F_RECORD_PARTIAL));
  158. /* Mark the record completed. */
  159. record->len_and_flags &= ~VBVA_F_RECORD_PARTIAL;
  160. vbva_ctx->buffer_overflow = false;
  161. vbva_ctx->record = NULL;
  162. }
  163. void vbva_setup_buffer_context(struct vbva_buf_ctx *vbva_ctx,
  164. u32 buffer_offset, u32 buffer_length)
  165. {
  166. vbva_ctx->buffer_offset = buffer_offset;
  167. vbva_ctx->buffer_length = buffer_length;
  168. }