pci_debug.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2012,2015
  4. *
  5. * Author(s):
  6. * Jan Glauber <jang@linux.vnet.ibm.com>
  7. */
  8. #define pr_fmt(fmt) "zpci: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/export.h>
  13. #include <linux/pci.h>
  14. #include <asm/debug.h>
  15. #include <asm/pci_dma.h>
  16. static struct dentry *debugfs_root;
  17. debug_info_t *pci_debug_msg_id;
  18. EXPORT_SYMBOL_GPL(pci_debug_msg_id);
  19. debug_info_t *pci_debug_err_id;
  20. EXPORT_SYMBOL_GPL(pci_debug_err_id);
  21. static char *pci_common_names[] = {
  22. "Load operations",
  23. "Store operations",
  24. "Store block operations",
  25. "Refresh operations",
  26. };
  27. static char *pci_fmt0_names[] = {
  28. "DMA read bytes",
  29. "DMA write bytes",
  30. };
  31. static char *pci_fmt1_names[] = {
  32. "Received bytes",
  33. "Received packets",
  34. "Transmitted bytes",
  35. "Transmitted packets",
  36. };
  37. static char *pci_fmt2_names[] = {
  38. "Consumed work units",
  39. "Maximum work units",
  40. };
  41. static char *pci_fmt3_names[] = {
  42. "Transmitted bytes",
  43. };
  44. static char *pci_sw_names[] = {
  45. "Mapped pages",
  46. "Unmapped pages",
  47. "Global RPCITs",
  48. "Sync Map RPCITs",
  49. "Sync RPCITs",
  50. };
  51. static void pci_fmb_show(struct seq_file *m, char *name[], int length,
  52. u64 *data)
  53. {
  54. int i;
  55. for (i = 0; i < length; i++, data++)
  56. seq_printf(m, "%26s:\t%llu\n", name[i], *data);
  57. }
  58. static void pci_sw_counter_show(struct seq_file *m)
  59. {
  60. struct zpci_dev *zdev = m->private;
  61. struct zpci_iommu_ctrs *ctrs;
  62. atomic64_t *counter;
  63. unsigned long flags;
  64. int i;
  65. spin_lock_irqsave(&zdev->dom_lock, flags);
  66. ctrs = zpci_get_iommu_ctrs(m->private);
  67. if (!ctrs)
  68. goto unlock;
  69. counter = &ctrs->mapped_pages;
  70. for (i = 0; i < ARRAY_SIZE(pci_sw_names); i++, counter++)
  71. seq_printf(m, "%26s:\t%llu\n", pci_sw_names[i],
  72. atomic64_read(counter));
  73. unlock:
  74. spin_unlock_irqrestore(&zdev->dom_lock, flags);
  75. }
  76. static int pci_perf_show(struct seq_file *m, void *v)
  77. {
  78. struct zpci_dev *zdev = m->private;
  79. if (!zdev)
  80. return 0;
  81. mutex_lock(&zdev->fmb_lock);
  82. if (!zdev->fmb) {
  83. mutex_unlock(&zdev->fmb_lock);
  84. seq_puts(m, "FMB statistics disabled\n");
  85. return 0;
  86. }
  87. /* header */
  88. seq_printf(m, "Update interval: %u ms\n", zdev->fmb_update);
  89. seq_printf(m, "Samples: %u\n", zdev->fmb->samples);
  90. seq_printf(m, "Last update TOD: %Lx\n", zdev->fmb->last_update);
  91. pci_fmb_show(m, pci_common_names, ARRAY_SIZE(pci_common_names),
  92. &zdev->fmb->ld_ops);
  93. switch (zdev->fmb->format) {
  94. case 0:
  95. if (!(zdev->fmb->fmt_ind & ZPCI_FMB_DMA_COUNTER_VALID))
  96. break;
  97. pci_fmb_show(m, pci_fmt0_names, ARRAY_SIZE(pci_fmt0_names),
  98. &zdev->fmb->fmt0.dma_rbytes);
  99. break;
  100. case 1:
  101. pci_fmb_show(m, pci_fmt1_names, ARRAY_SIZE(pci_fmt1_names),
  102. &zdev->fmb->fmt1.rx_bytes);
  103. break;
  104. case 2:
  105. pci_fmb_show(m, pci_fmt2_names, ARRAY_SIZE(pci_fmt2_names),
  106. &zdev->fmb->fmt2.consumed_work_units);
  107. break;
  108. case 3:
  109. pci_fmb_show(m, pci_fmt3_names, ARRAY_SIZE(pci_fmt3_names),
  110. &zdev->fmb->fmt3.tx_bytes);
  111. break;
  112. default:
  113. seq_puts(m, "Unknown format\n");
  114. }
  115. pci_sw_counter_show(m);
  116. mutex_unlock(&zdev->fmb_lock);
  117. return 0;
  118. }
  119. static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
  120. size_t count, loff_t *off)
  121. {
  122. struct zpci_dev *zdev = ((struct seq_file *) file->private_data)->private;
  123. unsigned long val;
  124. int rc;
  125. if (!zdev)
  126. return 0;
  127. rc = kstrtoul_from_user(ubuf, count, 10, &val);
  128. if (rc)
  129. return rc;
  130. mutex_lock(&zdev->fmb_lock);
  131. switch (val) {
  132. case 0:
  133. rc = zpci_fmb_disable_device(zdev);
  134. break;
  135. case 1:
  136. rc = zpci_fmb_enable_device(zdev);
  137. break;
  138. }
  139. mutex_unlock(&zdev->fmb_lock);
  140. return rc ? rc : count;
  141. }
  142. static int pci_perf_seq_open(struct inode *inode, struct file *filp)
  143. {
  144. return single_open(filp, pci_perf_show,
  145. file_inode(filp)->i_private);
  146. }
  147. static const struct file_operations debugfs_pci_perf_fops = {
  148. .open = pci_perf_seq_open,
  149. .read = seq_read,
  150. .write = pci_perf_seq_write,
  151. .llseek = seq_lseek,
  152. .release = single_release,
  153. };
  154. void zpci_debug_init_device(struct zpci_dev *zdev, const char *name)
  155. {
  156. zdev->debugfs_dev = debugfs_create_dir(name, debugfs_root);
  157. debugfs_create_file("statistics", S_IFREG | S_IRUGO | S_IWUSR,
  158. zdev->debugfs_dev, zdev, &debugfs_pci_perf_fops);
  159. }
  160. void zpci_debug_exit_device(struct zpci_dev *zdev)
  161. {
  162. debugfs_remove_recursive(zdev->debugfs_dev);
  163. }
  164. int __init zpci_debug_init(void)
  165. {
  166. /* event trace buffer */
  167. pci_debug_msg_id = debug_register("pci_msg", 8, 1, 8 * sizeof(long));
  168. if (!pci_debug_msg_id)
  169. return -EINVAL;
  170. debug_register_view(pci_debug_msg_id, &debug_sprintf_view);
  171. debug_set_level(pci_debug_msg_id, 3);
  172. /* error log */
  173. pci_debug_err_id = debug_register("pci_error", 2, 1, 16);
  174. if (!pci_debug_err_id)
  175. return -EINVAL;
  176. debug_register_view(pci_debug_err_id, &debug_hex_ascii_view);
  177. debug_set_level(pci_debug_err_id, 3);
  178. debugfs_root = debugfs_create_dir("pci", NULL);
  179. return 0;
  180. }
  181. void zpci_debug_exit(void)
  182. {
  183. debug_unregister(pci_debug_msg_id);
  184. debug_unregister(pci_debug_err_id);
  185. debugfs_remove(debugfs_root);
  186. }