dtl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Virtual Processor Dispatch Trace Log
  3. *
  4. * (C) Copyright IBM Corporation 2009
  5. *
  6. * Author: Jeremy Kerr <jk@ozlabs.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/debugfs.h>
  25. #include <asm/smp.h>
  26. #include <asm/system.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/firmware.h>
  29. #include <asm/lppaca.h>
  30. #include "plpar_wrappers.h"
  31. struct dtl {
  32. struct dtl_entry *buf;
  33. struct dentry *file;
  34. int cpu;
  35. int buf_entries;
  36. u64 last_idx;
  37. };
  38. static DEFINE_PER_CPU(struct dtl, cpu_dtl);
  39. /*
  40. * Dispatch trace log event mask:
  41. * 0x7: 0x1: voluntary virtual processor waits
  42. * 0x2: time-slice preempts
  43. * 0x4: virtual partition memory page faults
  44. */
  45. static u8 dtl_event_mask = 0x7;
  46. /*
  47. * Size of per-cpu log buffers. Default is just under 16 pages worth.
  48. */
  49. static int dtl_buf_entries = (16 * 85);
  50. static int dtl_enable(struct dtl *dtl)
  51. {
  52. unsigned long addr;
  53. int ret, hwcpu;
  54. /* only allow one reader */
  55. if (dtl->buf)
  56. return -EBUSY;
  57. /* we need to store the original allocation size for use during read */
  58. dtl->buf_entries = dtl_buf_entries;
  59. dtl->buf = kmalloc_node(dtl->buf_entries * sizeof(struct dtl_entry),
  60. GFP_KERNEL, cpu_to_node(dtl->cpu));
  61. if (!dtl->buf) {
  62. printk(KERN_WARNING "%s: buffer alloc failed for cpu %d\n",
  63. __func__, dtl->cpu);
  64. return -ENOMEM;
  65. }
  66. /* Register our dtl buffer with the hypervisor. The HV expects the
  67. * buffer size to be passed in the second word of the buffer */
  68. ((u32 *)dtl->buf)[1] = dtl->buf_entries * sizeof(struct dtl_entry);
  69. hwcpu = get_hard_smp_processor_id(dtl->cpu);
  70. addr = __pa(dtl->buf);
  71. ret = register_dtl(hwcpu, addr);
  72. if (ret) {
  73. printk(KERN_WARNING "%s: DTL registration for cpu %d (hw %d) "
  74. "failed with %d\n", __func__, dtl->cpu, hwcpu, ret);
  75. kfree(dtl->buf);
  76. return -EIO;
  77. }
  78. /* set our initial buffer indices */
  79. dtl->last_idx = lppaca_of(dtl->cpu).dtl_idx = 0;
  80. /* ensure that our updates to the lppaca fields have occurred before
  81. * we actually enable the logging */
  82. smp_wmb();
  83. /* enable event logging */
  84. lppaca_of(dtl->cpu).dtl_enable_mask = dtl_event_mask;
  85. return 0;
  86. }
  87. static void dtl_disable(struct dtl *dtl)
  88. {
  89. int hwcpu = get_hard_smp_processor_id(dtl->cpu);
  90. lppaca_of(dtl->cpu).dtl_enable_mask = 0x0;
  91. unregister_dtl(hwcpu, __pa(dtl->buf));
  92. kfree(dtl->buf);
  93. dtl->buf = NULL;
  94. dtl->buf_entries = 0;
  95. }
  96. /* file interface */
  97. static int dtl_file_open(struct inode *inode, struct file *filp)
  98. {
  99. struct dtl *dtl = inode->i_private;
  100. int rc;
  101. rc = dtl_enable(dtl);
  102. if (rc)
  103. return rc;
  104. filp->private_data = dtl;
  105. return 0;
  106. }
  107. static int dtl_file_release(struct inode *inode, struct file *filp)
  108. {
  109. struct dtl *dtl = inode->i_private;
  110. dtl_disable(dtl);
  111. return 0;
  112. }
  113. static ssize_t dtl_file_read(struct file *filp, char __user *buf, size_t len,
  114. loff_t *pos)
  115. {
  116. int rc, cur_idx, last_idx, n_read, n_req, read_size;
  117. struct dtl *dtl;
  118. if ((len % sizeof(struct dtl_entry)) != 0)
  119. return -EINVAL;
  120. dtl = filp->private_data;
  121. /* requested number of entries to read */
  122. n_req = len / sizeof(struct dtl_entry);
  123. /* actual number of entries read */
  124. n_read = 0;
  125. cur_idx = lppaca_of(dtl->cpu).dtl_idx;
  126. last_idx = dtl->last_idx;
  127. if (cur_idx - last_idx > dtl->buf_entries) {
  128. pr_debug("%s: hv buffer overflow for cpu %d, samples lost\n",
  129. __func__, dtl->cpu);
  130. }
  131. cur_idx %= dtl->buf_entries;
  132. last_idx %= dtl->buf_entries;
  133. /* read the tail of the buffer if we've wrapped */
  134. if (last_idx > cur_idx) {
  135. read_size = min(n_req, dtl->buf_entries - last_idx);
  136. rc = copy_to_user(buf, &dtl->buf[last_idx],
  137. read_size * sizeof(struct dtl_entry));
  138. if (rc)
  139. return -EFAULT;
  140. last_idx = 0;
  141. n_req -= read_size;
  142. n_read += read_size;
  143. buf += read_size * sizeof(struct dtl_entry);
  144. }
  145. /* .. and now the head */
  146. read_size = min(n_req, cur_idx - last_idx);
  147. rc = copy_to_user(buf, &dtl->buf[last_idx],
  148. read_size * sizeof(struct dtl_entry));
  149. if (rc)
  150. return -EFAULT;
  151. n_read += read_size;
  152. dtl->last_idx += n_read;
  153. return n_read * sizeof(struct dtl_entry);
  154. }
  155. static const struct file_operations dtl_fops = {
  156. .open = dtl_file_open,
  157. .release = dtl_file_release,
  158. .read = dtl_file_read,
  159. .llseek = no_llseek,
  160. };
  161. static struct dentry *dtl_dir;
  162. static int dtl_setup_file(struct dtl *dtl)
  163. {
  164. char name[10];
  165. sprintf(name, "cpu-%d", dtl->cpu);
  166. dtl->file = debugfs_create_file(name, 0400, dtl_dir, dtl, &dtl_fops);
  167. if (!dtl->file)
  168. return -ENOMEM;
  169. return 0;
  170. }
  171. static int dtl_init(void)
  172. {
  173. struct dentry *event_mask_file, *buf_entries_file;
  174. int rc, i;
  175. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  176. /* disable this for now */
  177. return -ENODEV;
  178. #endif
  179. if (!firmware_has_feature(FW_FEATURE_SPLPAR))
  180. return -ENODEV;
  181. /* set up common debugfs structure */
  182. rc = -ENOMEM;
  183. dtl_dir = debugfs_create_dir("dtl", powerpc_debugfs_root);
  184. if (!dtl_dir) {
  185. printk(KERN_WARNING "%s: can't create dtl root dir\n",
  186. __func__);
  187. goto err;
  188. }
  189. event_mask_file = debugfs_create_x8("dtl_event_mask", 0600,
  190. dtl_dir, &dtl_event_mask);
  191. buf_entries_file = debugfs_create_u32("dtl_buf_entries", 0600,
  192. dtl_dir, &dtl_buf_entries);
  193. if (!event_mask_file || !buf_entries_file) {
  194. printk(KERN_WARNING "%s: can't create dtl files\n", __func__);
  195. goto err_remove_dir;
  196. }
  197. /* set up the per-cpu log structures */
  198. for_each_possible_cpu(i) {
  199. struct dtl *dtl = &per_cpu(cpu_dtl, i);
  200. dtl->cpu = i;
  201. rc = dtl_setup_file(dtl);
  202. if (rc)
  203. goto err_remove_dir;
  204. }
  205. return 0;
  206. err_remove_dir:
  207. debugfs_remove_recursive(dtl_dir);
  208. err:
  209. return rc;
  210. }
  211. arch_initcall(dtl_init);