coresight-etm-perf.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright(C) 2015 Linaro Limited. All rights reserved.
  3. * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/coresight.h>
  18. #include <linux/coresight-pmu.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/device.h>
  21. #include <linux/list.h>
  22. #include <linux/mm.h>
  23. #include <linux/init.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/slab.h>
  26. #include <linux/types.h>
  27. #include <linux/workqueue.h>
  28. #include "coresight-priv.h"
  29. static struct pmu etm_pmu;
  30. static bool etm_perf_up;
  31. /**
  32. * struct etm_event_data - Coresight specifics associated to an event
  33. * @work: Handle to free allocated memory outside IRQ context.
  34. * @mask: Hold the CPU(s) this event was set for.
  35. * @snk_config: The sink configuration.
  36. * @path: An array of path, each slot for one CPU.
  37. */
  38. struct etm_event_data {
  39. struct work_struct work;
  40. cpumask_t mask;
  41. void *snk_config;
  42. struct list_head **path;
  43. };
  44. static DEFINE_PER_CPU(struct perf_output_handle, ctx_handle);
  45. static DEFINE_PER_CPU(struct coresight_device *, csdev_src);
  46. /* ETMv3.5/PTM's ETMCR is 'config' */
  47. PMU_FORMAT_ATTR(cycacc, "config:" __stringify(ETM_OPT_CYCACC));
  48. PMU_FORMAT_ATTR(timestamp, "config:" __stringify(ETM_OPT_TS));
  49. static struct attribute *etm_config_formats_attr[] = {
  50. &format_attr_cycacc.attr,
  51. &format_attr_timestamp.attr,
  52. NULL,
  53. };
  54. static struct attribute_group etm_pmu_format_group = {
  55. .name = "format",
  56. .attrs = etm_config_formats_attr,
  57. };
  58. static const struct attribute_group *etm_pmu_attr_groups[] = {
  59. &etm_pmu_format_group,
  60. NULL,
  61. };
  62. static void etm_event_read(struct perf_event *event) {}
  63. static int etm_event_init(struct perf_event *event)
  64. {
  65. if (event->attr.type != etm_pmu.type)
  66. return -ENOENT;
  67. return 0;
  68. }
  69. static void free_event_data(struct work_struct *work)
  70. {
  71. int cpu;
  72. cpumask_t *mask;
  73. struct etm_event_data *event_data;
  74. struct coresight_device *sink;
  75. event_data = container_of(work, struct etm_event_data, work);
  76. mask = &event_data->mask;
  77. /*
  78. * First deal with the sink configuration. See comment in
  79. * etm_setup_aux() about why we take the first available path.
  80. */
  81. if (event_data->snk_config) {
  82. cpu = cpumask_first(mask);
  83. sink = coresight_get_sink(event_data->path[cpu]);
  84. if (sink_ops(sink)->free_buffer)
  85. sink_ops(sink)->free_buffer(event_data->snk_config);
  86. }
  87. for_each_cpu(cpu, mask) {
  88. if (event_data->path[cpu])
  89. coresight_release_path(event_data->path[cpu]);
  90. }
  91. kfree(event_data->path);
  92. kfree(event_data);
  93. }
  94. static void *alloc_event_data(int cpu)
  95. {
  96. int size;
  97. cpumask_t *mask;
  98. struct etm_event_data *event_data;
  99. /* First get memory for the session's data */
  100. event_data = kzalloc(sizeof(struct etm_event_data), GFP_KERNEL);
  101. if (!event_data)
  102. return NULL;
  103. /* Make sure nothing disappears under us */
  104. get_online_cpus();
  105. size = num_online_cpus();
  106. mask = &event_data->mask;
  107. if (cpu != -1)
  108. cpumask_set_cpu(cpu, mask);
  109. else
  110. cpumask_copy(mask, cpu_online_mask);
  111. put_online_cpus();
  112. /*
  113. * Each CPU has a single path between source and destination. As such
  114. * allocate an array using CPU numbers as indexes. That way a path
  115. * for any CPU can easily be accessed at any given time. We proceed
  116. * the same way for sessions involving a single CPU. The cost of
  117. * unused memory when dealing with single CPU trace scenarios is small
  118. * compared to the cost of searching through an optimized array.
  119. */
  120. event_data->path = kcalloc(size,
  121. sizeof(struct list_head *), GFP_KERNEL);
  122. if (!event_data->path) {
  123. kfree(event_data);
  124. return NULL;
  125. }
  126. return event_data;
  127. }
  128. static void etm_free_aux(void *data)
  129. {
  130. struct etm_event_data *event_data = data;
  131. schedule_work(&event_data->work);
  132. }
  133. static void *etm_setup_aux(int event_cpu, void **pages,
  134. int nr_pages, bool overwrite)
  135. {
  136. int cpu;
  137. cpumask_t *mask;
  138. struct coresight_device *sink;
  139. struct etm_event_data *event_data = NULL;
  140. event_data = alloc_event_data(event_cpu);
  141. if (!event_data)
  142. return NULL;
  143. INIT_WORK(&event_data->work, free_event_data);
  144. mask = &event_data->mask;
  145. /* Setup the path for each CPU in a trace session */
  146. for_each_cpu(cpu, mask) {
  147. struct coresight_device *csdev;
  148. csdev = per_cpu(csdev_src, cpu);
  149. if (!csdev)
  150. goto err;
  151. /*
  152. * Building a path doesn't enable it, it simply builds a
  153. * list of devices from source to sink that can be
  154. * referenced later when the path is actually needed.
  155. */
  156. event_data->path[cpu] = coresight_build_path(csdev);
  157. if (!event_data->path[cpu])
  158. goto err;
  159. }
  160. /*
  161. * In theory nothing prevent tracers in a trace session from being
  162. * associated with different sinks, nor having a sink per tracer. But
  163. * until we have HW with this kind of topology and a way to convey
  164. * sink assignement from the perf cmd line we need to assume tracers
  165. * in a trace session are using the same sink. Therefore pick the sink
  166. * found at the end of the first available path.
  167. */
  168. cpu = cpumask_first(mask);
  169. /* Grab the sink at the end of the path */
  170. sink = coresight_get_sink(event_data->path[cpu]);
  171. if (!sink)
  172. goto err;
  173. if (!sink_ops(sink)->alloc_buffer)
  174. goto err;
  175. /* Get the AUX specific data from the sink buffer */
  176. event_data->snk_config =
  177. sink_ops(sink)->alloc_buffer(sink, cpu, pages,
  178. nr_pages, overwrite);
  179. if (!event_data->snk_config)
  180. goto err;
  181. out:
  182. return event_data;
  183. err:
  184. etm_free_aux(event_data);
  185. event_data = NULL;
  186. goto out;
  187. }
  188. static void etm_event_start(struct perf_event *event, int flags)
  189. {
  190. int cpu = smp_processor_id();
  191. struct etm_event_data *event_data;
  192. struct perf_output_handle *handle = this_cpu_ptr(&ctx_handle);
  193. struct coresight_device *sink, *csdev = per_cpu(csdev_src, cpu);
  194. if (!csdev)
  195. goto fail;
  196. /*
  197. * Deal with the ring buffer API and get a handle on the
  198. * session's information.
  199. */
  200. event_data = perf_aux_output_begin(handle, event);
  201. if (!event_data)
  202. goto fail;
  203. /* We need a sink, no need to continue without one */
  204. sink = coresight_get_sink(event_data->path[cpu]);
  205. if (WARN_ON_ONCE(!sink || !sink_ops(sink)->set_buffer))
  206. goto fail_end_stop;
  207. /* Configure the sink */
  208. if (sink_ops(sink)->set_buffer(sink, handle,
  209. event_data->snk_config))
  210. goto fail_end_stop;
  211. /* Nothing will happen without a path */
  212. if (coresight_enable_path(event_data->path[cpu], CS_MODE_PERF))
  213. goto fail_end_stop;
  214. /* Tell the perf core the event is alive */
  215. event->hw.state = 0;
  216. /* Finally enable the tracer */
  217. if (source_ops(csdev)->enable(csdev, &event->attr, CS_MODE_PERF))
  218. goto fail_end_stop;
  219. out:
  220. return;
  221. fail_end_stop:
  222. perf_aux_output_end(handle, 0, true);
  223. fail:
  224. event->hw.state = PERF_HES_STOPPED;
  225. goto out;
  226. }
  227. static void etm_event_stop(struct perf_event *event, int mode)
  228. {
  229. bool lost;
  230. int cpu = smp_processor_id();
  231. unsigned long size;
  232. struct coresight_device *sink, *csdev = per_cpu(csdev_src, cpu);
  233. struct perf_output_handle *handle = this_cpu_ptr(&ctx_handle);
  234. struct etm_event_data *event_data = perf_get_aux(handle);
  235. if (event->hw.state == PERF_HES_STOPPED)
  236. return;
  237. if (!csdev)
  238. return;
  239. sink = coresight_get_sink(event_data->path[cpu]);
  240. if (!sink)
  241. return;
  242. /* stop tracer */
  243. source_ops(csdev)->disable(csdev);
  244. /* tell the core */
  245. event->hw.state = PERF_HES_STOPPED;
  246. if (mode & PERF_EF_UPDATE) {
  247. if (WARN_ON_ONCE(handle->event != event))
  248. return;
  249. /* update trace information */
  250. if (!sink_ops(sink)->update_buffer)
  251. return;
  252. sink_ops(sink)->update_buffer(sink, handle,
  253. event_data->snk_config);
  254. if (!sink_ops(sink)->reset_buffer)
  255. return;
  256. size = sink_ops(sink)->reset_buffer(sink, handle,
  257. event_data->snk_config,
  258. &lost);
  259. perf_aux_output_end(handle, size, lost);
  260. }
  261. /* Disabling the path make its elements available to other sessions */
  262. coresight_disable_path(event_data->path[cpu]);
  263. }
  264. static int etm_event_add(struct perf_event *event, int mode)
  265. {
  266. int ret = 0;
  267. struct hw_perf_event *hwc = &event->hw;
  268. if (mode & PERF_EF_START) {
  269. etm_event_start(event, 0);
  270. if (hwc->state & PERF_HES_STOPPED)
  271. ret = -EINVAL;
  272. } else {
  273. hwc->state = PERF_HES_STOPPED;
  274. }
  275. return ret;
  276. }
  277. static void etm_event_del(struct perf_event *event, int mode)
  278. {
  279. etm_event_stop(event, PERF_EF_UPDATE);
  280. }
  281. int etm_perf_symlink(struct coresight_device *csdev, bool link)
  282. {
  283. char entry[sizeof("cpu9999999")];
  284. int ret = 0, cpu = source_ops(csdev)->cpu_id(csdev);
  285. struct device *pmu_dev = etm_pmu.dev;
  286. struct device *cs_dev = &csdev->dev;
  287. sprintf(entry, "cpu%d", cpu);
  288. if (!etm_perf_up)
  289. return -EPROBE_DEFER;
  290. if (link) {
  291. ret = sysfs_create_link(&pmu_dev->kobj, &cs_dev->kobj, entry);
  292. if (ret)
  293. return ret;
  294. per_cpu(csdev_src, cpu) = csdev;
  295. } else {
  296. sysfs_remove_link(&pmu_dev->kobj, entry);
  297. per_cpu(csdev_src, cpu) = NULL;
  298. }
  299. return 0;
  300. }
  301. static int __init etm_perf_init(void)
  302. {
  303. int ret;
  304. etm_pmu.capabilities = PERF_PMU_CAP_EXCLUSIVE;
  305. etm_pmu.attr_groups = etm_pmu_attr_groups;
  306. etm_pmu.task_ctx_nr = perf_sw_context;
  307. etm_pmu.read = etm_event_read;
  308. etm_pmu.event_init = etm_event_init;
  309. etm_pmu.setup_aux = etm_setup_aux;
  310. etm_pmu.free_aux = etm_free_aux;
  311. etm_pmu.start = etm_event_start;
  312. etm_pmu.stop = etm_event_stop;
  313. etm_pmu.add = etm_event_add;
  314. etm_pmu.del = etm_event_del;
  315. ret = perf_pmu_register(&etm_pmu, CORESIGHT_ETM_PMU_NAME, -1);
  316. if (ret == 0)
  317. etm_perf_up = true;
  318. return ret;
  319. }
  320. device_initcall(etm_perf_init);