dtl.c 6.4 KB

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