dtl.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/slab.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/spinlock.h>
  25. #include <asm/smp.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/firmware.h>
  28. #include <asm/lppaca.h>
  29. #include <asm/debug.h>
  30. #include <asm/plpar_wrappers.h>
  31. #include <asm/machdep.h>
  32. struct dtl {
  33. struct dtl_entry *buf;
  34. struct dentry *file;
  35. int cpu;
  36. int buf_entries;
  37. u64 last_idx;
  38. spinlock_t lock;
  39. };
  40. static DEFINE_PER_CPU(struct dtl, cpu_dtl);
  41. /*
  42. * Dispatch trace log event mask:
  43. * 0x7: 0x1: voluntary virtual processor waits
  44. * 0x2: time-slice preempts
  45. * 0x4: virtual partition memory page faults
  46. */
  47. static u8 dtl_event_mask = 0x7;
  48. /*
  49. * Size of per-cpu log buffers. Firmware requires that the buffer does
  50. * not cross a 4k boundary.
  51. */
  52. static int dtl_buf_entries = N_DISPATCH_LOG;
  53. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  54. struct dtl_ring {
  55. u64 write_index;
  56. struct dtl_entry *write_ptr;
  57. struct dtl_entry *buf;
  58. struct dtl_entry *buf_end;
  59. u8 saved_dtl_mask;
  60. };
  61. static DEFINE_PER_CPU(struct dtl_ring, dtl_rings);
  62. static atomic_t dtl_count;
  63. /*
  64. * The cpu accounting code controls the DTL ring buffer, and we get
  65. * given entries as they are processed.
  66. */
  67. static void consume_dtle(struct dtl_entry *dtle, u64 index)
  68. {
  69. struct dtl_ring *dtlr = this_cpu_ptr(&dtl_rings);
  70. struct dtl_entry *wp = dtlr->write_ptr;
  71. struct lppaca *vpa = local_paca->lppaca_ptr;
  72. if (!wp)
  73. return;
  74. *wp = *dtle;
  75. barrier();
  76. /* check for hypervisor ring buffer overflow, ignore this entry if so */
  77. if (index + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx))
  78. return;
  79. ++wp;
  80. if (wp == dtlr->buf_end)
  81. wp = dtlr->buf;
  82. dtlr->write_ptr = wp;
  83. /* incrementing write_index makes the new entry visible */
  84. smp_wmb();
  85. ++dtlr->write_index;
  86. }
  87. static int dtl_start(struct dtl *dtl)
  88. {
  89. struct dtl_ring *dtlr = &per_cpu(dtl_rings, dtl->cpu);
  90. dtlr->buf = dtl->buf;
  91. dtlr->buf_end = dtl->buf + dtl->buf_entries;
  92. dtlr->write_index = 0;
  93. /* setting write_ptr enables logging into our buffer */
  94. smp_wmb();
  95. dtlr->write_ptr = dtl->buf;
  96. /* enable event logging */
  97. dtlr->saved_dtl_mask = lppaca_of(dtl->cpu).dtl_enable_mask;
  98. lppaca_of(dtl->cpu).dtl_enable_mask |= dtl_event_mask;
  99. dtl_consumer = consume_dtle;
  100. atomic_inc(&dtl_count);
  101. return 0;
  102. }
  103. static void dtl_stop(struct dtl *dtl)
  104. {
  105. struct dtl_ring *dtlr = &per_cpu(dtl_rings, dtl->cpu);
  106. dtlr->write_ptr = NULL;
  107. smp_wmb();
  108. dtlr->buf = NULL;
  109. /* restore dtl_enable_mask */
  110. lppaca_of(dtl->cpu).dtl_enable_mask = dtlr->saved_dtl_mask;
  111. if (atomic_dec_and_test(&dtl_count))
  112. dtl_consumer = NULL;
  113. }
  114. static u64 dtl_current_index(struct dtl *dtl)
  115. {
  116. return per_cpu(dtl_rings, dtl->cpu).write_index;
  117. }
  118. #else /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  119. static int dtl_start(struct dtl *dtl)
  120. {
  121. unsigned long addr;
  122. int ret, hwcpu;
  123. /* Register our dtl buffer with the hypervisor. The HV expects the
  124. * buffer size to be passed in the second word of the buffer */
  125. ((u32 *)dtl->buf)[1] = DISPATCH_LOG_BYTES;
  126. hwcpu = get_hard_smp_processor_id(dtl->cpu);
  127. addr = __pa(dtl->buf);
  128. ret = register_dtl(hwcpu, addr);
  129. if (ret) {
  130. printk(KERN_WARNING "%s: DTL registration for cpu %d (hw %d) "
  131. "failed with %d\n", __func__, dtl->cpu, hwcpu, ret);
  132. return -EIO;
  133. }
  134. /* set our initial buffer indices */
  135. lppaca_of(dtl->cpu).dtl_idx = 0;
  136. /* ensure that our updates to the lppaca fields have occurred before
  137. * we actually enable the logging */
  138. smp_wmb();
  139. /* enable event logging */
  140. lppaca_of(dtl->cpu).dtl_enable_mask = dtl_event_mask;
  141. return 0;
  142. }
  143. static void dtl_stop(struct dtl *dtl)
  144. {
  145. int hwcpu = get_hard_smp_processor_id(dtl->cpu);
  146. lppaca_of(dtl->cpu).dtl_enable_mask = 0x0;
  147. unregister_dtl(hwcpu);
  148. }
  149. static u64 dtl_current_index(struct dtl *dtl)
  150. {
  151. return lppaca_of(dtl->cpu).dtl_idx;
  152. }
  153. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  154. static int dtl_enable(struct dtl *dtl)
  155. {
  156. long int n_entries;
  157. long int rc;
  158. struct dtl_entry *buf = NULL;
  159. if (!dtl_cache)
  160. return -ENOMEM;
  161. /* only allow one reader */
  162. if (dtl->buf)
  163. return -EBUSY;
  164. n_entries = dtl_buf_entries;
  165. buf = kmem_cache_alloc_node(dtl_cache, GFP_KERNEL, cpu_to_node(dtl->cpu));
  166. if (!buf) {
  167. printk(KERN_WARNING "%s: buffer alloc failed for cpu %d\n",
  168. __func__, dtl->cpu);
  169. return -ENOMEM;
  170. }
  171. spin_lock(&dtl->lock);
  172. rc = -EBUSY;
  173. if (!dtl->buf) {
  174. /* store the original allocation size for use during read */
  175. dtl->buf_entries = n_entries;
  176. dtl->buf = buf;
  177. dtl->last_idx = 0;
  178. rc = dtl_start(dtl);
  179. if (rc)
  180. dtl->buf = NULL;
  181. }
  182. spin_unlock(&dtl->lock);
  183. if (rc)
  184. kmem_cache_free(dtl_cache, buf);
  185. return rc;
  186. }
  187. static void dtl_disable(struct dtl *dtl)
  188. {
  189. spin_lock(&dtl->lock);
  190. dtl_stop(dtl);
  191. kmem_cache_free(dtl_cache, dtl->buf);
  192. dtl->buf = NULL;
  193. dtl->buf_entries = 0;
  194. spin_unlock(&dtl->lock);
  195. }
  196. /* file interface */
  197. static int dtl_file_open(struct inode *inode, struct file *filp)
  198. {
  199. struct dtl *dtl = inode->i_private;
  200. int rc;
  201. rc = dtl_enable(dtl);
  202. if (rc)
  203. return rc;
  204. filp->private_data = dtl;
  205. return 0;
  206. }
  207. static int dtl_file_release(struct inode *inode, struct file *filp)
  208. {
  209. struct dtl *dtl = inode->i_private;
  210. dtl_disable(dtl);
  211. return 0;
  212. }
  213. static ssize_t dtl_file_read(struct file *filp, char __user *buf, size_t len,
  214. loff_t *pos)
  215. {
  216. long int rc, n_read, n_req, read_size;
  217. struct dtl *dtl;
  218. u64 cur_idx, last_idx, i;
  219. if ((len % sizeof(struct dtl_entry)) != 0)
  220. return -EINVAL;
  221. dtl = filp->private_data;
  222. /* requested number of entries to read */
  223. n_req = len / sizeof(struct dtl_entry);
  224. /* actual number of entries read */
  225. n_read = 0;
  226. spin_lock(&dtl->lock);
  227. cur_idx = dtl_current_index(dtl);
  228. last_idx = dtl->last_idx;
  229. if (last_idx + dtl->buf_entries <= cur_idx)
  230. last_idx = cur_idx - dtl->buf_entries + 1;
  231. if (last_idx + n_req > cur_idx)
  232. n_req = cur_idx - last_idx;
  233. if (n_req > 0)
  234. dtl->last_idx = last_idx + n_req;
  235. spin_unlock(&dtl->lock);
  236. if (n_req <= 0)
  237. return 0;
  238. i = last_idx % dtl->buf_entries;
  239. /* read the tail of the buffer if we've wrapped */
  240. if (i + n_req > dtl->buf_entries) {
  241. read_size = dtl->buf_entries - i;
  242. rc = copy_to_user(buf, &dtl->buf[i],
  243. read_size * sizeof(struct dtl_entry));
  244. if (rc)
  245. return -EFAULT;
  246. i = 0;
  247. n_req -= read_size;
  248. n_read += read_size;
  249. buf += read_size * sizeof(struct dtl_entry);
  250. }
  251. /* .. and now the head */
  252. rc = copy_to_user(buf, &dtl->buf[i], n_req * sizeof(struct dtl_entry));
  253. if (rc)
  254. return -EFAULT;
  255. n_read += n_req;
  256. return n_read * sizeof(struct dtl_entry);
  257. }
  258. static const struct file_operations dtl_fops = {
  259. .open = dtl_file_open,
  260. .release = dtl_file_release,
  261. .read = dtl_file_read,
  262. .llseek = no_llseek,
  263. };
  264. static struct dentry *dtl_dir;
  265. static int dtl_setup_file(struct dtl *dtl)
  266. {
  267. char name[10];
  268. sprintf(name, "cpu-%d", dtl->cpu);
  269. dtl->file = debugfs_create_file(name, 0400, dtl_dir, dtl, &dtl_fops);
  270. if (!dtl->file)
  271. return -ENOMEM;
  272. return 0;
  273. }
  274. static int dtl_init(void)
  275. {
  276. struct dentry *event_mask_file, *buf_entries_file;
  277. int rc, i;
  278. if (!firmware_has_feature(FW_FEATURE_SPLPAR))
  279. return -ENODEV;
  280. /* set up common debugfs structure */
  281. rc = -ENOMEM;
  282. dtl_dir = debugfs_create_dir("dtl", powerpc_debugfs_root);
  283. if (!dtl_dir) {
  284. printk(KERN_WARNING "%s: can't create dtl root dir\n",
  285. __func__);
  286. goto err;
  287. }
  288. event_mask_file = debugfs_create_x8("dtl_event_mask", 0600,
  289. dtl_dir, &dtl_event_mask);
  290. buf_entries_file = debugfs_create_u32("dtl_buf_entries", 0400,
  291. dtl_dir, &dtl_buf_entries);
  292. if (!event_mask_file || !buf_entries_file) {
  293. printk(KERN_WARNING "%s: can't create dtl files\n", __func__);
  294. goto err_remove_dir;
  295. }
  296. /* set up the per-cpu log structures */
  297. for_each_possible_cpu(i) {
  298. struct dtl *dtl = &per_cpu(cpu_dtl, i);
  299. spin_lock_init(&dtl->lock);
  300. dtl->cpu = i;
  301. rc = dtl_setup_file(dtl);
  302. if (rc)
  303. goto err_remove_dir;
  304. }
  305. return 0;
  306. err_remove_dir:
  307. debugfs_remove_recursive(dtl_dir);
  308. err:
  309. return rc;
  310. }
  311. machine_arch_initcall(pseries, dtl_init);