dtl.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/debugfs.h>
  24. #include <asm/smp.h>
  25. #include <asm/system.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/firmware.h>
  28. #include "plpar_wrappers.h"
  29. /*
  30. * Layout of entries in the hypervisor's DTL buffer. Although we don't
  31. * actually access the internals of an entry (we only need to know the size),
  32. * we might as well define it here for reference.
  33. */
  34. struct dtl_entry {
  35. u8 dispatch_reason;
  36. u8 preempt_reason;
  37. u16 processor_id;
  38. u32 enqueue_to_dispatch_time;
  39. u32 ready_to_enqueue_time;
  40. u32 waiting_to_ready_time;
  41. u64 timebase;
  42. u64 fault_addr;
  43. u64 srr0;
  44. u64 srr1;
  45. };
  46. struct dtl {
  47. struct dtl_entry *buf;
  48. struct dentry *file;
  49. int cpu;
  50. int buf_entries;
  51. u64 last_idx;
  52. };
  53. static DEFINE_PER_CPU(struct dtl, dtl);
  54. /*
  55. * Dispatch trace log event mask:
  56. * 0x7: 0x1: voluntary virtual processor waits
  57. * 0x2: time-slice preempts
  58. * 0x4: virtual partition memory page faults
  59. */
  60. static u8 dtl_event_mask = 0x7;
  61. /*
  62. * Size of per-cpu log buffers. Default is just under 16 pages worth.
  63. */
  64. static int dtl_buf_entries = (16 * 85);
  65. static int dtl_enable(struct dtl *dtl)
  66. {
  67. unsigned long addr;
  68. int ret, hwcpu;
  69. /* only allow one reader */
  70. if (dtl->buf)
  71. return -EBUSY;
  72. /* we need to store the original allocation size for use during read */
  73. dtl->buf_entries = dtl_buf_entries;
  74. dtl->buf = kmalloc_node(dtl->buf_entries * sizeof(struct dtl_entry),
  75. GFP_KERNEL, cpu_to_node(dtl->cpu));
  76. if (!dtl->buf) {
  77. printk(KERN_WARNING "%s: buffer alloc failed for cpu %d\n",
  78. __func__, dtl->cpu);
  79. return -ENOMEM;
  80. }
  81. /* Register our dtl buffer with the hypervisor. The HV expects the
  82. * buffer size to be passed in the second word of the buffer */
  83. ((u32 *)dtl->buf)[1] = dtl->buf_entries * sizeof(struct dtl_entry);
  84. hwcpu = get_hard_smp_processor_id(dtl->cpu);
  85. addr = __pa(dtl->buf);
  86. ret = register_dtl(hwcpu, addr);
  87. if (ret) {
  88. printk(KERN_WARNING "%s: DTL registration for cpu %d (hw %d) "
  89. "failed with %d\n", __func__, dtl->cpu, hwcpu, ret);
  90. kfree(dtl->buf);
  91. return -EIO;
  92. }
  93. /* set our initial buffer indices */
  94. dtl->last_idx = lppaca[dtl->cpu].dtl_idx = 0;
  95. /* ensure that our updates to the lppaca fields have occurred before
  96. * we actually enable the logging */
  97. smp_wmb();
  98. /* enable event logging */
  99. lppaca[dtl->cpu].dtl_enable_mask = dtl_event_mask;
  100. return 0;
  101. }
  102. static void dtl_disable(struct dtl *dtl)
  103. {
  104. int hwcpu = get_hard_smp_processor_id(dtl->cpu);
  105. lppaca[dtl->cpu].dtl_enable_mask = 0x0;
  106. unregister_dtl(hwcpu, __pa(dtl->buf));
  107. kfree(dtl->buf);
  108. dtl->buf = NULL;
  109. dtl->buf_entries = 0;
  110. }
  111. /* file interface */
  112. static int dtl_file_open(struct inode *inode, struct file *filp)
  113. {
  114. struct dtl *dtl = inode->i_private;
  115. int rc;
  116. rc = dtl_enable(dtl);
  117. if (rc)
  118. return rc;
  119. filp->private_data = dtl;
  120. return 0;
  121. }
  122. static int dtl_file_release(struct inode *inode, struct file *filp)
  123. {
  124. struct dtl *dtl = inode->i_private;
  125. dtl_disable(dtl);
  126. return 0;
  127. }
  128. static ssize_t dtl_file_read(struct file *filp, char __user *buf, size_t len,
  129. loff_t *pos)
  130. {
  131. int rc, cur_idx, last_idx, n_read, n_req, read_size;
  132. struct dtl *dtl;
  133. if ((len % sizeof(struct dtl_entry)) != 0)
  134. return -EINVAL;
  135. dtl = filp->private_data;
  136. /* requested number of entries to read */
  137. n_req = len / sizeof(struct dtl_entry);
  138. /* actual number of entries read */
  139. n_read = 0;
  140. cur_idx = lppaca[dtl->cpu].dtl_idx;
  141. last_idx = dtl->last_idx;
  142. if (cur_idx - last_idx > dtl->buf_entries) {
  143. pr_debug("%s: hv buffer overflow for cpu %d, samples lost\n",
  144. __func__, dtl->cpu);
  145. }
  146. cur_idx %= dtl->buf_entries;
  147. last_idx %= dtl->buf_entries;
  148. /* read the tail of the buffer if we've wrapped */
  149. if (last_idx > cur_idx) {
  150. read_size = min(n_req, dtl->buf_entries - last_idx);
  151. rc = copy_to_user(buf, &dtl->buf[last_idx],
  152. read_size * sizeof(struct dtl_entry));
  153. if (rc)
  154. return -EFAULT;
  155. last_idx = 0;
  156. n_req -= read_size;
  157. n_read += read_size;
  158. buf += read_size * sizeof(struct dtl_entry);
  159. }
  160. /* .. and now the head */
  161. read_size = min(n_req, cur_idx - last_idx);
  162. rc = copy_to_user(buf, &dtl->buf[last_idx],
  163. read_size * sizeof(struct dtl_entry));
  164. if (rc)
  165. return -EFAULT;
  166. n_read += read_size;
  167. dtl->last_idx += n_read;
  168. return n_read * sizeof(struct dtl_entry);
  169. }
  170. static struct file_operations dtl_fops = {
  171. .open = dtl_file_open,
  172. .release = dtl_file_release,
  173. .read = dtl_file_read,
  174. .llseek = no_llseek,
  175. };
  176. static struct dentry *dtl_dir;
  177. static int dtl_setup_file(struct dtl *dtl)
  178. {
  179. char name[10];
  180. sprintf(name, "cpu-%d", dtl->cpu);
  181. dtl->file = debugfs_create_file(name, 0400, dtl_dir, dtl, &dtl_fops);
  182. if (!dtl->file)
  183. return -ENOMEM;
  184. return 0;
  185. }
  186. static int dtl_init(void)
  187. {
  188. struct dentry *event_mask_file, *buf_entries_file;
  189. int rc, i;
  190. if (!firmware_has_feature(FW_FEATURE_SPLPAR))
  191. return -ENODEV;
  192. /* set up common debugfs structure */
  193. rc = -ENOMEM;
  194. dtl_dir = debugfs_create_dir("dtl", powerpc_debugfs_root);
  195. if (!dtl_dir) {
  196. printk(KERN_WARNING "%s: can't create dtl root dir\n",
  197. __func__);
  198. goto err;
  199. }
  200. event_mask_file = debugfs_create_x8("dtl_event_mask", 0600,
  201. dtl_dir, &dtl_event_mask);
  202. buf_entries_file = debugfs_create_u32("dtl_buf_entries", 0600,
  203. dtl_dir, &dtl_buf_entries);
  204. if (!event_mask_file || !buf_entries_file) {
  205. printk(KERN_WARNING "%s: can't create dtl files\n", __func__);
  206. goto err_remove_dir;
  207. }
  208. /* set up the per-cpu log structures */
  209. for_each_possible_cpu(i) {
  210. struct dtl *dtl = &per_cpu(dtl, i);
  211. dtl->cpu = i;
  212. rc = dtl_setup_file(dtl);
  213. if (rc)
  214. goto err_remove_dir;
  215. }
  216. return 0;
  217. err_remove_dir:
  218. debugfs_remove_recursive(dtl_dir);
  219. err:
  220. return rc;
  221. }
  222. arch_initcall(dtl_init);