coresight-etm-perf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright(C) 2015 Linaro Limited. All rights reserved.
  4. * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
  5. */
  6. #include <linux/coresight.h>
  7. #include <linux/coresight-pmu.h>
  8. #include <linux/cpumask.h>
  9. #include <linux/device.h>
  10. #include <linux/list.h>
  11. #include <linux/mm.h>
  12. #include <linux/init.h>
  13. #include <linux/perf_event.h>
  14. #include <linux/percpu-defs.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <linux/workqueue.h>
  18. #include "coresight-etm-perf.h"
  19. #include "coresight-priv.h"
  20. static struct pmu etm_pmu;
  21. static bool etm_perf_up;
  22. static DEFINE_PER_CPU(struct perf_output_handle, ctx_handle);
  23. static DEFINE_PER_CPU(struct coresight_device *, csdev_src);
  24. /* ETMv3.5/PTM's ETMCR is 'config' */
  25. PMU_FORMAT_ATTR(cycacc, "config:" __stringify(ETM_OPT_CYCACC));
  26. PMU_FORMAT_ATTR(timestamp, "config:" __stringify(ETM_OPT_TS));
  27. PMU_FORMAT_ATTR(retstack, "config:" __stringify(ETM_OPT_RETSTK));
  28. static struct attribute *etm_config_formats_attr[] = {
  29. &format_attr_cycacc.attr,
  30. &format_attr_timestamp.attr,
  31. &format_attr_retstack.attr,
  32. NULL,
  33. };
  34. static const struct attribute_group etm_pmu_format_group = {
  35. .name = "format",
  36. .attrs = etm_config_formats_attr,
  37. };
  38. static const struct attribute_group *etm_pmu_attr_groups[] = {
  39. &etm_pmu_format_group,
  40. NULL,
  41. };
  42. static inline struct list_head **
  43. etm_event_cpu_path_ptr(struct etm_event_data *data, int cpu)
  44. {
  45. return per_cpu_ptr(data->path, cpu);
  46. }
  47. static inline struct list_head *
  48. etm_event_cpu_path(struct etm_event_data *data, int cpu)
  49. {
  50. return *etm_event_cpu_path_ptr(data, cpu);
  51. }
  52. static void etm_event_read(struct perf_event *event) {}
  53. static int etm_addr_filters_alloc(struct perf_event *event)
  54. {
  55. struct etm_filters *filters;
  56. int node = event->cpu == -1 ? -1 : cpu_to_node(event->cpu);
  57. filters = kzalloc_node(sizeof(struct etm_filters), GFP_KERNEL, node);
  58. if (!filters)
  59. return -ENOMEM;
  60. if (event->parent)
  61. memcpy(filters, event->parent->hw.addr_filters,
  62. sizeof(*filters));
  63. event->hw.addr_filters = filters;
  64. return 0;
  65. }
  66. static void etm_event_destroy(struct perf_event *event)
  67. {
  68. kfree(event->hw.addr_filters);
  69. event->hw.addr_filters = NULL;
  70. }
  71. static int etm_event_init(struct perf_event *event)
  72. {
  73. int ret = 0;
  74. if (event->attr.type != etm_pmu.type) {
  75. ret = -ENOENT;
  76. goto out;
  77. }
  78. ret = etm_addr_filters_alloc(event);
  79. if (ret)
  80. goto out;
  81. event->destroy = etm_event_destroy;
  82. out:
  83. return ret;
  84. }
  85. static void free_event_data(struct work_struct *work)
  86. {
  87. int cpu;
  88. cpumask_t *mask;
  89. struct etm_event_data *event_data;
  90. struct coresight_device *sink;
  91. event_data = container_of(work, struct etm_event_data, work);
  92. mask = &event_data->mask;
  93. /* Free the sink buffers, if there are any */
  94. if (event_data->snk_config && !WARN_ON(cpumask_empty(mask))) {
  95. cpu = cpumask_first(mask);
  96. sink = coresight_get_sink(etm_event_cpu_path(event_data, cpu));
  97. if (sink_ops(sink)->free_buffer)
  98. sink_ops(sink)->free_buffer(event_data->snk_config);
  99. }
  100. for_each_cpu(cpu, mask) {
  101. struct list_head **ppath;
  102. ppath = etm_event_cpu_path_ptr(event_data, cpu);
  103. if (!(IS_ERR_OR_NULL(*ppath)))
  104. coresight_release_path(*ppath);
  105. *ppath = NULL;
  106. }
  107. free_percpu(event_data->path);
  108. kfree(event_data);
  109. }
  110. static void *alloc_event_data(int cpu)
  111. {
  112. cpumask_t *mask;
  113. struct etm_event_data *event_data;
  114. /* First get memory for the session's data */
  115. event_data = kzalloc(sizeof(struct etm_event_data), GFP_KERNEL);
  116. if (!event_data)
  117. return NULL;
  118. mask = &event_data->mask;
  119. if (cpu != -1)
  120. cpumask_set_cpu(cpu, mask);
  121. else
  122. cpumask_copy(mask, cpu_present_mask);
  123. /*
  124. * Each CPU has a single path between source and destination. As such
  125. * allocate an array using CPU numbers as indexes. That way a path
  126. * for any CPU can easily be accessed at any given time. We proceed
  127. * the same way for sessions involving a single CPU. The cost of
  128. * unused memory when dealing with single CPU trace scenarios is small
  129. * compared to the cost of searching through an optimized array.
  130. */
  131. event_data->path = alloc_percpu(struct list_head *);
  132. if (!event_data->path) {
  133. kfree(event_data);
  134. return NULL;
  135. }
  136. return event_data;
  137. }
  138. static void etm_free_aux(void *data)
  139. {
  140. struct etm_event_data *event_data = data;
  141. schedule_work(&event_data->work);
  142. }
  143. static void *etm_setup_aux(int event_cpu, void **pages,
  144. int nr_pages, bool overwrite)
  145. {
  146. int cpu;
  147. cpumask_t *mask;
  148. struct coresight_device *sink;
  149. struct etm_event_data *event_data = NULL;
  150. event_data = alloc_event_data(event_cpu);
  151. if (!event_data)
  152. return NULL;
  153. INIT_WORK(&event_data->work, free_event_data);
  154. /*
  155. * In theory nothing prevent tracers in a trace session from being
  156. * associated with different sinks, nor having a sink per tracer. But
  157. * until we have HW with this kind of topology we need to assume tracers
  158. * in a trace session are using the same sink. Therefore go through
  159. * the coresight bus and pick the first enabled sink.
  160. *
  161. * When operated from sysFS users are responsible to enable the sink
  162. * while from perf, the perf tools will do it based on the choice made
  163. * on the cmd line. As such the "enable_sink" flag in sysFS is reset.
  164. */
  165. sink = coresight_get_enabled_sink(true);
  166. if (!sink || !sink_ops(sink)->alloc_buffer)
  167. goto err;
  168. mask = &event_data->mask;
  169. /*
  170. * Setup the path for each CPU in a trace session. We try to build
  171. * trace path for each CPU in the mask. If we don't find an ETM
  172. * for the CPU or fail to build a path, we clear the CPU from the
  173. * mask and continue with the rest. If ever we try to trace on those
  174. * CPUs, we can handle it and fail the session.
  175. */
  176. for_each_cpu(cpu, mask) {
  177. struct list_head *path;
  178. struct coresight_device *csdev;
  179. csdev = per_cpu(csdev_src, cpu);
  180. /*
  181. * If there is no ETM associated with this CPU clear it from
  182. * the mask and continue with the rest. If ever we try to trace
  183. * on this CPU, we handle it accordingly.
  184. */
  185. if (!csdev) {
  186. cpumask_clear_cpu(cpu, mask);
  187. continue;
  188. }
  189. /*
  190. * Building a path doesn't enable it, it simply builds a
  191. * list of devices from source to sink that can be
  192. * referenced later when the path is actually needed.
  193. */
  194. path = coresight_build_path(csdev, sink);
  195. if (IS_ERR(path)) {
  196. cpumask_clear_cpu(cpu, mask);
  197. continue;
  198. }
  199. *etm_event_cpu_path_ptr(event_data, cpu) = path;
  200. }
  201. /* If we don't have any CPUs ready for tracing, abort */
  202. cpu = cpumask_first(mask);
  203. if (cpu >= nr_cpu_ids)
  204. goto err;
  205. /* Allocate the sink buffer for this session */
  206. event_data->snk_config =
  207. sink_ops(sink)->alloc_buffer(sink, cpu, pages,
  208. nr_pages, overwrite);
  209. if (!event_data->snk_config)
  210. goto err;
  211. out:
  212. return event_data;
  213. err:
  214. etm_free_aux(event_data);
  215. event_data = NULL;
  216. goto out;
  217. }
  218. static void etm_event_start(struct perf_event *event, int flags)
  219. {
  220. int cpu = smp_processor_id();
  221. struct etm_event_data *event_data;
  222. struct perf_output_handle *handle = this_cpu_ptr(&ctx_handle);
  223. struct coresight_device *sink, *csdev = per_cpu(csdev_src, cpu);
  224. struct list_head *path;
  225. if (!csdev)
  226. goto fail;
  227. /*
  228. * Deal with the ring buffer API and get a handle on the
  229. * session's information.
  230. */
  231. event_data = perf_aux_output_begin(handle, event);
  232. if (!event_data)
  233. goto fail;
  234. path = etm_event_cpu_path(event_data, cpu);
  235. /* We need a sink, no need to continue without one */
  236. sink = coresight_get_sink(path);
  237. if (WARN_ON_ONCE(!sink))
  238. goto fail_end_stop;
  239. /* Nothing will happen without a path */
  240. if (coresight_enable_path(path, CS_MODE_PERF, handle))
  241. goto fail_end_stop;
  242. /* Tell the perf core the event is alive */
  243. event->hw.state = 0;
  244. /* Finally enable the tracer */
  245. if (source_ops(csdev)->enable(csdev, event, CS_MODE_PERF))
  246. goto fail_disable_path;
  247. out:
  248. return;
  249. fail_disable_path:
  250. coresight_disable_path(path);
  251. fail_end_stop:
  252. perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
  253. perf_aux_output_end(handle, 0);
  254. fail:
  255. event->hw.state = PERF_HES_STOPPED;
  256. goto out;
  257. }
  258. static void etm_event_stop(struct perf_event *event, int mode)
  259. {
  260. int cpu = smp_processor_id();
  261. unsigned long size;
  262. struct coresight_device *sink, *csdev = per_cpu(csdev_src, cpu);
  263. struct perf_output_handle *handle = this_cpu_ptr(&ctx_handle);
  264. struct etm_event_data *event_data = perf_get_aux(handle);
  265. struct list_head *path;
  266. if (event->hw.state == PERF_HES_STOPPED)
  267. return;
  268. if (!csdev)
  269. return;
  270. path = etm_event_cpu_path(event_data, cpu);
  271. if (!path)
  272. return;
  273. sink = coresight_get_sink(path);
  274. if (!sink)
  275. return;
  276. /* stop tracer */
  277. source_ops(csdev)->disable(csdev, event);
  278. /* tell the core */
  279. event->hw.state = PERF_HES_STOPPED;
  280. if (mode & PERF_EF_UPDATE) {
  281. if (WARN_ON_ONCE(handle->event != event))
  282. return;
  283. /* update trace information */
  284. if (!sink_ops(sink)->update_buffer)
  285. return;
  286. size = sink_ops(sink)->update_buffer(sink, handle,
  287. event_data->snk_config);
  288. perf_aux_output_end(handle, size);
  289. }
  290. /* Disabling the path make its elements available to other sessions */
  291. coresight_disable_path(path);
  292. }
  293. static int etm_event_add(struct perf_event *event, int mode)
  294. {
  295. int ret = 0;
  296. struct hw_perf_event *hwc = &event->hw;
  297. if (mode & PERF_EF_START) {
  298. etm_event_start(event, 0);
  299. if (hwc->state & PERF_HES_STOPPED)
  300. ret = -EINVAL;
  301. } else {
  302. hwc->state = PERF_HES_STOPPED;
  303. }
  304. return ret;
  305. }
  306. static void etm_event_del(struct perf_event *event, int mode)
  307. {
  308. etm_event_stop(event, PERF_EF_UPDATE);
  309. }
  310. static int etm_addr_filters_validate(struct list_head *filters)
  311. {
  312. bool range = false, address = false;
  313. int index = 0;
  314. struct perf_addr_filter *filter;
  315. list_for_each_entry(filter, filters, entry) {
  316. /*
  317. * No need to go further if there's no more
  318. * room for filters.
  319. */
  320. if (++index > ETM_ADDR_CMP_MAX)
  321. return -EOPNOTSUPP;
  322. /* filter::size==0 means single address trigger */
  323. if (filter->size) {
  324. /*
  325. * The existing code relies on START/STOP filters
  326. * being address filters.
  327. */
  328. if (filter->action == PERF_ADDR_FILTER_ACTION_START ||
  329. filter->action == PERF_ADDR_FILTER_ACTION_STOP)
  330. return -EOPNOTSUPP;
  331. range = true;
  332. } else
  333. address = true;
  334. /*
  335. * At this time we don't allow range and start/stop filtering
  336. * to cohabitate, they have to be mutually exclusive.
  337. */
  338. if (range && address)
  339. return -EOPNOTSUPP;
  340. }
  341. return 0;
  342. }
  343. static void etm_addr_filters_sync(struct perf_event *event)
  344. {
  345. struct perf_addr_filters_head *head = perf_event_addr_filters(event);
  346. unsigned long start, stop, *offs = event->addr_filters_offs;
  347. struct etm_filters *filters = event->hw.addr_filters;
  348. struct etm_filter *etm_filter;
  349. struct perf_addr_filter *filter;
  350. int i = 0;
  351. list_for_each_entry(filter, &head->list, entry) {
  352. start = filter->offset + offs[i];
  353. stop = start + filter->size;
  354. etm_filter = &filters->etm_filter[i];
  355. switch (filter->action) {
  356. case PERF_ADDR_FILTER_ACTION_FILTER:
  357. etm_filter->start_addr = start;
  358. etm_filter->stop_addr = stop;
  359. etm_filter->type = ETM_ADDR_TYPE_RANGE;
  360. break;
  361. case PERF_ADDR_FILTER_ACTION_START:
  362. etm_filter->start_addr = start;
  363. etm_filter->type = ETM_ADDR_TYPE_START;
  364. break;
  365. case PERF_ADDR_FILTER_ACTION_STOP:
  366. etm_filter->stop_addr = stop;
  367. etm_filter->type = ETM_ADDR_TYPE_STOP;
  368. break;
  369. }
  370. i++;
  371. }
  372. filters->nr_filters = i;
  373. }
  374. int etm_perf_symlink(struct coresight_device *csdev, bool link)
  375. {
  376. char entry[sizeof("cpu9999999")];
  377. int ret = 0, cpu = source_ops(csdev)->cpu_id(csdev);
  378. struct device *pmu_dev = etm_pmu.dev;
  379. struct device *cs_dev = &csdev->dev;
  380. sprintf(entry, "cpu%d", cpu);
  381. if (!etm_perf_up)
  382. return -EPROBE_DEFER;
  383. if (link) {
  384. ret = sysfs_create_link(&pmu_dev->kobj, &cs_dev->kobj, entry);
  385. if (ret)
  386. return ret;
  387. per_cpu(csdev_src, cpu) = csdev;
  388. } else {
  389. sysfs_remove_link(&pmu_dev->kobj, entry);
  390. per_cpu(csdev_src, cpu) = NULL;
  391. }
  392. return 0;
  393. }
  394. static int __init etm_perf_init(void)
  395. {
  396. int ret;
  397. etm_pmu.capabilities = PERF_PMU_CAP_EXCLUSIVE;
  398. etm_pmu.attr_groups = etm_pmu_attr_groups;
  399. etm_pmu.task_ctx_nr = perf_sw_context;
  400. etm_pmu.read = etm_event_read;
  401. etm_pmu.event_init = etm_event_init;
  402. etm_pmu.setup_aux = etm_setup_aux;
  403. etm_pmu.free_aux = etm_free_aux;
  404. etm_pmu.start = etm_event_start;
  405. etm_pmu.stop = etm_event_stop;
  406. etm_pmu.add = etm_event_add;
  407. etm_pmu.del = etm_event_del;
  408. etm_pmu.addr_filters_sync = etm_addr_filters_sync;
  409. etm_pmu.addr_filters_validate = etm_addr_filters_validate;
  410. etm_pmu.nr_addr_filters = ETM_ADDR_CMP_MAX;
  411. ret = perf_pmu_register(&etm_pmu, CORESIGHT_ETM_PMU_NAME, -1);
  412. if (ret == 0)
  413. etm_perf_up = true;
  414. return ret;
  415. }
  416. device_initcall(etm_perf_init);