test_decode.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright © 2011 Intel Corporation
  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 (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <err.h>
  31. #include "libdrm_macros.h"
  32. #include "intel_bufmgr.h"
  33. #include "intel_chipset.h"
  34. #define HW_OFFSET 0x12300000
  35. static void
  36. usage(void)
  37. {
  38. fprintf(stderr, "usage:\n");
  39. fprintf(stderr, " test_decode <batch>\n");
  40. fprintf(stderr, " test_decode <batch> -dump\n");
  41. exit(1);
  42. }
  43. static void
  44. read_file(const char *filename, void **ptr, size_t *size)
  45. {
  46. int fd, ret;
  47. struct stat st;
  48. fd = open(filename, O_RDONLY);
  49. if (fd < 0)
  50. errx(1, "couldn't open `%s'", filename);
  51. ret = fstat(fd, &st);
  52. if (ret)
  53. errx(1, "couldn't stat `%s'", filename);
  54. *size = st.st_size;
  55. *ptr = drm_mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  56. if (*ptr == MAP_FAILED)
  57. errx(1, "couldn't map `%s'", filename);
  58. close(fd);
  59. }
  60. static void
  61. dump_batch(struct drm_intel_decode *ctx, const char *batch_filename)
  62. {
  63. void *batch_ptr;
  64. size_t batch_size;
  65. read_file(batch_filename, &batch_ptr, &batch_size);
  66. drm_intel_decode_set_batch_pointer(ctx, batch_ptr, HW_OFFSET,
  67. batch_size / 4);
  68. drm_intel_decode_set_output_file(ctx, stdout);
  69. drm_intel_decode(ctx);
  70. }
  71. static void
  72. compare_batch(struct drm_intel_decode *ctx, const char *batch_filename)
  73. {
  74. FILE *out = NULL;
  75. char *ptr;
  76. void *ref_ptr, *batch_ptr;
  77. #if HAVE_OPEN_MEMSTREAM
  78. size_t size;
  79. #endif
  80. size_t ref_size, batch_size;
  81. const char *ref_suffix = "-ref.txt";
  82. char *ref_filename;
  83. ref_filename = malloc(strlen(batch_filename) + strlen(ref_suffix) + 1);
  84. sprintf(ref_filename, "%s%s", batch_filename, ref_suffix);
  85. /* Read the batch and reference. */
  86. read_file(batch_filename, &batch_ptr, &batch_size);
  87. read_file(ref_filename, &ref_ptr, &ref_size);
  88. /* Set up our decode output in memory, because I don't want to
  89. * figure out how to output to a file in a safe and sane way
  90. * inside of an automake project's test infrastructure.
  91. */
  92. #if HAVE_OPEN_MEMSTREAM
  93. out = open_memstream(&ptr, &size);
  94. #else
  95. fprintf(stderr, "platform lacks open_memstream, skipping.\n");
  96. exit(77);
  97. #endif
  98. drm_intel_decode_set_batch_pointer(ctx, batch_ptr, HW_OFFSET,
  99. batch_size / 4);
  100. drm_intel_decode_set_output_file(ctx, out);
  101. drm_intel_decode(ctx);
  102. if (strcmp(ref_ptr, ptr) != 0) {
  103. fprintf(stderr, "Decode mismatch with reference `%s'.\n",
  104. ref_filename);
  105. fprintf(stderr, "You can dump the new output using:\n");
  106. fprintf(stderr, " test_decode \"%s\" -dump\n", batch_filename);
  107. exit(1);
  108. }
  109. fclose(out);
  110. free(ref_filename);
  111. free(ptr);
  112. }
  113. static uint16_t
  114. infer_devid(const char *batch_filename)
  115. {
  116. struct {
  117. const char *name;
  118. uint16_t devid;
  119. } chipsets[] = {
  120. { "830", 0x3577},
  121. { "855", 0x3582},
  122. { "945", 0x2772},
  123. { "gen4", 0x2a02 },
  124. { "gm45", 0x2a42 },
  125. { "gen5", PCI_CHIP_ILD_G },
  126. { "gen6", PCI_CHIP_SANDYBRIDGE_GT2 },
  127. { "gen7", PCI_CHIP_IVYBRIDGE_GT2 },
  128. { "gen8", 0x1616 },
  129. { NULL, 0 },
  130. };
  131. int i;
  132. for (i = 0; chipsets[i].name != NULL; i++) {
  133. if (strstr(batch_filename, chipsets[i].name))
  134. return chipsets[i].devid;
  135. }
  136. fprintf(stderr, "Couldn't guess chipset id from batch filename `%s'.\n",
  137. batch_filename);
  138. fprintf(stderr, "Must be contain one of:\n");
  139. for (i = 0; chipsets[i].name != NULL; i++) {
  140. fprintf(stderr, " %s\n", chipsets[i].name);
  141. }
  142. exit(1);
  143. }
  144. int
  145. main(int argc, char **argv)
  146. {
  147. uint16_t devid;
  148. struct drm_intel_decode *ctx;
  149. if (argc < 2)
  150. usage();
  151. devid = infer_devid(argv[1]);
  152. ctx = drm_intel_decode_context_alloc(devid);
  153. if (argc == 3) {
  154. if (strcmp(argv[2], "-dump") == 0)
  155. dump_batch(ctx, argv[1]);
  156. else
  157. usage();
  158. } else {
  159. compare_batch(ctx, argv[1]);
  160. }
  161. drm_intel_decode_context_free(ctx);
  162. return 0;
  163. }