img_pdump.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*!
  2. *****************************************************************************
  3. * Copyright (c) Imagination Technologies Ltd.
  4. *
  5. * The contents of this file are subject to the MIT license as set out below.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions 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 NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of the
  26. * GNU General Public License Version 2 ("GPL")in which case the provisions of
  27. * GPL are applicable instead of those above.
  28. *
  29. * If you wish to allow use of your version of this file only under the terms
  30. * of GPL, and not to allow others to use your version of this file under the
  31. * terms of the MIT license, indicate your decision by deleting the provisions
  32. * above and replace them with the notice and other provisions required by GPL
  33. * as set out in the file called "GPLHEADER" included in this distribution. If
  34. * you do not delete the provisions above, a recipient may use your version of
  35. * this file under the terms of either the MIT license or GPL.
  36. *
  37. * This License is also included in this distribution in the file called
  38. * "MIT_COPYING".
  39. *
  40. *****************************************************************************/
  41. #include <linux/module.h>
  42. #include <linux/mm.h>
  43. #include <linux/mutex.h>
  44. #include <linux/list.h>
  45. #include <linux/slab.h>
  46. #include <linux/vmalloc.h>
  47. #include <stdarg.h>
  48. #include <img_mem_man.h>
  49. #include <vha_drv_common.h>
  50. /*
  51. * create a pdump buffer
  52. * Pdump buffers are currently identified by hard coded number:
  53. * from PDUMP_TXT up to PDUMP_MAX.
  54. * Buffer is allocated using vmalloc, because it might be several MBytes.
  55. *
  56. * If size==0, the buffer can be used for SAB, but not for LDB or TXT.
  57. * (in other words, no memory will be allocated,
  58. * but it will still have a 'length')
  59. */
  60. struct pdump_buf *img_pdump_create(struct pdump_descr* pdump, uint32_t pdump_num, size_t size)
  61. {
  62. struct pdump_buf *pbuf = &pdump->pbufs[pdump_num];
  63. if (pdump_num >= PDUMP_MAX) {
  64. pr_err("%s: invalid pdump number:%d\n", __func__, pdump_num);
  65. return NULL;
  66. }
  67. if (pbuf->ptr != NULL) {
  68. pr_err("%s: pdump %d already created\n", __func__, pdump_num);
  69. return NULL;
  70. }
  71. pbuf->size = size;
  72. pbuf->len = 0;
  73. if (size == 0)
  74. return pbuf;
  75. pbuf->ptr = vmalloc(size);
  76. pr_debug("%s %d buffer %p size:%zu!\n", __func__,
  77. pdump_num, pbuf->ptr, size);
  78. if (pbuf->ptr == NULL) {
  79. pr_err("%s: failed to create pdump %d\n", __func__, pdump_num);
  80. return NULL;
  81. }
  82. return pbuf;
  83. }
  84. EXPORT_SYMBOL(img_pdump_create);
  85. /*
  86. * append binary data to one of the pdump buffers
  87. */
  88. int img_pdump_write(struct pdump_descr* pdump, uint32_t pdump_num, const void *ptr, size_t size)
  89. {
  90. struct pdump_buf *pbuf = &pdump->pbufs[pdump_num];
  91. int ret = 0;
  92. if (pdump_num >= PDUMP_MAX || ptr == NULL || pbuf->ptr == NULL)
  93. return -EINVAL;
  94. mutex_lock(&pdump->lock);
  95. if (pbuf->len + size > pbuf->size)
  96. size = pbuf->size - pbuf->len;
  97. if (!size) {
  98. pr_err("%s: no space left in the pdump %d buffer!\n",
  99. __func__, pdump_num);
  100. ret = -ENOSPC;
  101. goto unlock;
  102. }
  103. pr_debug("%s %d buffer len:%zu size:%zu!\n", __func__,
  104. pdump_num, size, pbuf->len);
  105. memcpy(pbuf->ptr + pbuf->len, ptr, size);
  106. pbuf->len += size;
  107. pr_debug("%s end!\n", __func__);
  108. unlock:
  109. mutex_unlock(&pdump->lock);
  110. return ret;
  111. }
  112. EXPORT_SYMBOL(img_pdump_write);
  113. /*
  114. * append a string to the TXT pdump buffer.
  115. * returns the number of bytes printed or error.
  116. */
  117. __printf(2, 3)
  118. int __img_pdump_printf(struct device* dev, const char *fmt, ...)
  119. {
  120. struct pdump_descr* pdump = vha_pdump_dev_get_drvdata(dev);
  121. struct pdump_buf *pbuf;
  122. va_list ap;
  123. BUG_ON(pdump==NULL);
  124. pbuf = &pdump->pbufs[PDUMP_TXT];
  125. if (pbuf->ptr == NULL)
  126. return -EINVAL;
  127. mutex_lock(&pdump->lock);
  128. va_start(ap, fmt);
  129. if (pbuf->len < pbuf->size) {
  130. #if defined(OSID)
  131. /* Prepend OSID to pdump comments */
  132. if (fmt[0] == '-' && fmt[1] == '-')
  133. pbuf->len += sprintf(pbuf->ptr + pbuf->len,
  134. "-- (OS%d) ", OSID);
  135. #endif
  136. pbuf->len += vsnprintf(pbuf->ptr + pbuf->len,
  137. pbuf->size - pbuf->len,
  138. fmt, ap);
  139. }
  140. /*
  141. * vsnprintf returns the number of bytes that WOULD have been printed
  142. */
  143. pbuf->len = min(pbuf->size, pbuf->len);
  144. va_end(ap);
  145. mutex_unlock(&pdump->lock);
  146. return pbuf->len;
  147. }
  148. EXPORT_SYMBOL(__img_pdump_printf);
  149. void img_pdump_destroy(struct pdump_descr* pdump)
  150. {
  151. int i;
  152. for (i = 0; i < PDUMP_MAX; i++) {
  153. void *ptr = pdump->pbufs[i].ptr;
  154. pdump->pbufs[i].ptr = NULL;
  155. pr_debug("%s %d buffer %p!\n", __func__, i, ptr);
  156. vfree(ptr);
  157. }
  158. }
  159. EXPORT_SYMBOL(img_pdump_destroy);
  160. /*
  161. * PDUMP generation is disabled until a PDUMP TXT buffer has been created
  162. */
  163. bool img_pdump_enabled(struct pdump_descr* pdump)
  164. {
  165. return pdump && pdump->pbufs[PDUMP_TXT].ptr != NULL;
  166. }
  167. EXPORT_SYMBOL(img_pdump_enabled);