coresight-tmc-etr.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright(C) 2016 Linaro Limited. All rights reserved.
  4. * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
  5. */
  6. #include <linux/coresight.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/iommu.h>
  9. #include <linux/slab.h>
  10. #include <linux/vmalloc.h>
  11. #include "coresight-catu.h"
  12. #include "coresight-etm-perf.h"
  13. #include "coresight-priv.h"
  14. #include "coresight-tmc.h"
  15. struct etr_flat_buf {
  16. struct device *dev;
  17. dma_addr_t daddr;
  18. void *vaddr;
  19. size_t size;
  20. };
  21. /*
  22. * etr_perf_buffer - Perf buffer used for ETR
  23. * @etr_buf - Actual buffer used by the ETR
  24. * @snaphost - Perf session mode
  25. * @head - handle->head at the beginning of the session.
  26. * @nr_pages - Number of pages in the ring buffer.
  27. * @pages - Array of Pages in the ring buffer.
  28. */
  29. struct etr_perf_buffer {
  30. struct etr_buf *etr_buf;
  31. bool snapshot;
  32. unsigned long head;
  33. int nr_pages;
  34. void **pages;
  35. };
  36. /* Convert the perf index to an offset within the ETR buffer */
  37. #define PERF_IDX2OFF(idx, buf) ((idx) % ((buf)->nr_pages << PAGE_SHIFT))
  38. /* Lower limit for ETR hardware buffer */
  39. #define TMC_ETR_PERF_MIN_BUF_SIZE SZ_1M
  40. /*
  41. * The TMC ETR SG has a page size of 4K. The SG table contains pointers
  42. * to 4KB buffers. However, the OS may use a PAGE_SIZE different from
  43. * 4K (i.e, 16KB or 64KB). This implies that a single OS page could
  44. * contain more than one SG buffer and tables.
  45. *
  46. * A table entry has the following format:
  47. *
  48. * ---Bit31------------Bit4-------Bit1-----Bit0--
  49. * | Address[39:12] | SBZ | Entry Type |
  50. * ----------------------------------------------
  51. *
  52. * Address: Bits [39:12] of a physical page address. Bits [11:0] are
  53. * always zero.
  54. *
  55. * Entry type:
  56. * b00 - Reserved.
  57. * b01 - Last entry in the tables, points to 4K page buffer.
  58. * b10 - Normal entry, points to 4K page buffer.
  59. * b11 - Link. The address points to the base of next table.
  60. */
  61. typedef u32 sgte_t;
  62. #define ETR_SG_PAGE_SHIFT 12
  63. #define ETR_SG_PAGE_SIZE (1UL << ETR_SG_PAGE_SHIFT)
  64. #define ETR_SG_PAGES_PER_SYSPAGE (PAGE_SIZE / ETR_SG_PAGE_SIZE)
  65. #define ETR_SG_PTRS_PER_PAGE (ETR_SG_PAGE_SIZE / sizeof(sgte_t))
  66. #define ETR_SG_PTRS_PER_SYSPAGE (PAGE_SIZE / sizeof(sgte_t))
  67. #define ETR_SG_ET_MASK 0x3
  68. #define ETR_SG_ET_LAST 0x1
  69. #define ETR_SG_ET_NORMAL 0x2
  70. #define ETR_SG_ET_LINK 0x3
  71. #define ETR_SG_ADDR_SHIFT 4
  72. #define ETR_SG_ENTRY(addr, type) \
  73. (sgte_t)((((addr) >> ETR_SG_PAGE_SHIFT) << ETR_SG_ADDR_SHIFT) | \
  74. (type & ETR_SG_ET_MASK))
  75. #define ETR_SG_ADDR(entry) \
  76. (((dma_addr_t)(entry) >> ETR_SG_ADDR_SHIFT) << ETR_SG_PAGE_SHIFT)
  77. #define ETR_SG_ET(entry) ((entry) & ETR_SG_ET_MASK)
  78. /*
  79. * struct etr_sg_table : ETR SG Table
  80. * @sg_table: Generic SG Table holding the data/table pages.
  81. * @hwaddr: hwaddress used by the TMC, which is the base
  82. * address of the table.
  83. */
  84. struct etr_sg_table {
  85. struct tmc_sg_table *sg_table;
  86. dma_addr_t hwaddr;
  87. };
  88. /*
  89. * tmc_etr_sg_table_entries: Total number of table entries required to map
  90. * @nr_pages system pages.
  91. *
  92. * We need to map @nr_pages * ETR_SG_PAGES_PER_SYSPAGE data pages.
  93. * Each TMC page can map (ETR_SG_PTRS_PER_PAGE - 1) buffer pointers,
  94. * with the last entry pointing to another page of table entries.
  95. * If we spill over to a new page for mapping 1 entry, we could as
  96. * well replace the link entry of the previous page with the last entry.
  97. */
  98. static inline unsigned long __attribute_const__
  99. tmc_etr_sg_table_entries(int nr_pages)
  100. {
  101. unsigned long nr_sgpages = nr_pages * ETR_SG_PAGES_PER_SYSPAGE;
  102. unsigned long nr_sglinks = nr_sgpages / (ETR_SG_PTRS_PER_PAGE - 1);
  103. /*
  104. * If we spill over to a new page for 1 entry, we could as well
  105. * make it the LAST entry in the previous page, skipping the Link
  106. * address.
  107. */
  108. if (nr_sglinks && (nr_sgpages % (ETR_SG_PTRS_PER_PAGE - 1) < 2))
  109. nr_sglinks--;
  110. return nr_sgpages + nr_sglinks;
  111. }
  112. /*
  113. * tmc_pages_get_offset: Go through all the pages in the tmc_pages
  114. * and map the device address @addr to an offset within the virtual
  115. * contiguous buffer.
  116. */
  117. static long
  118. tmc_pages_get_offset(struct tmc_pages *tmc_pages, dma_addr_t addr)
  119. {
  120. int i;
  121. dma_addr_t page_start;
  122. for (i = 0; i < tmc_pages->nr_pages; i++) {
  123. page_start = tmc_pages->daddrs[i];
  124. if (addr >= page_start && addr < (page_start + PAGE_SIZE))
  125. return i * PAGE_SIZE + (addr - page_start);
  126. }
  127. return -EINVAL;
  128. }
  129. /*
  130. * tmc_pages_free : Unmap and free the pages used by tmc_pages.
  131. * If the pages were not allocated in tmc_pages_alloc(), we would
  132. * simply drop the refcount.
  133. */
  134. static void tmc_pages_free(struct tmc_pages *tmc_pages,
  135. struct device *dev, enum dma_data_direction dir)
  136. {
  137. int i;
  138. for (i = 0; i < tmc_pages->nr_pages; i++) {
  139. if (tmc_pages->daddrs && tmc_pages->daddrs[i])
  140. dma_unmap_page(dev, tmc_pages->daddrs[i],
  141. PAGE_SIZE, dir);
  142. if (tmc_pages->pages && tmc_pages->pages[i])
  143. __free_page(tmc_pages->pages[i]);
  144. }
  145. kfree(tmc_pages->pages);
  146. kfree(tmc_pages->daddrs);
  147. tmc_pages->pages = NULL;
  148. tmc_pages->daddrs = NULL;
  149. tmc_pages->nr_pages = 0;
  150. }
  151. /*
  152. * tmc_pages_alloc : Allocate and map pages for a given @tmc_pages.
  153. * If @pages is not NULL, the list of page virtual addresses are
  154. * used as the data pages. The pages are then dma_map'ed for @dev
  155. * with dma_direction @dir.
  156. *
  157. * Returns 0 upon success, else the error number.
  158. */
  159. static int tmc_pages_alloc(struct tmc_pages *tmc_pages,
  160. struct device *dev, int node,
  161. enum dma_data_direction dir, void **pages)
  162. {
  163. int i, nr_pages;
  164. dma_addr_t paddr;
  165. struct page *page;
  166. nr_pages = tmc_pages->nr_pages;
  167. tmc_pages->daddrs = kcalloc(nr_pages, sizeof(*tmc_pages->daddrs),
  168. GFP_KERNEL);
  169. if (!tmc_pages->daddrs)
  170. return -ENOMEM;
  171. tmc_pages->pages = kcalloc(nr_pages, sizeof(*tmc_pages->pages),
  172. GFP_KERNEL);
  173. if (!tmc_pages->pages) {
  174. kfree(tmc_pages->daddrs);
  175. tmc_pages->daddrs = NULL;
  176. return -ENOMEM;
  177. }
  178. for (i = 0; i < nr_pages; i++) {
  179. if (pages && pages[i]) {
  180. page = virt_to_page(pages[i]);
  181. /* Hold a refcount on the page */
  182. get_page(page);
  183. } else {
  184. page = alloc_pages_node(node,
  185. GFP_KERNEL | __GFP_ZERO, 0);
  186. }
  187. paddr = dma_map_page(dev, page, 0, PAGE_SIZE, dir);
  188. if (dma_mapping_error(dev, paddr))
  189. goto err;
  190. tmc_pages->daddrs[i] = paddr;
  191. tmc_pages->pages[i] = page;
  192. }
  193. return 0;
  194. err:
  195. tmc_pages_free(tmc_pages, dev, dir);
  196. return -ENOMEM;
  197. }
  198. static inline long
  199. tmc_sg_get_data_page_offset(struct tmc_sg_table *sg_table, dma_addr_t addr)
  200. {
  201. return tmc_pages_get_offset(&sg_table->data_pages, addr);
  202. }
  203. static inline void tmc_free_table_pages(struct tmc_sg_table *sg_table)
  204. {
  205. if (sg_table->table_vaddr)
  206. vunmap(sg_table->table_vaddr);
  207. tmc_pages_free(&sg_table->table_pages, sg_table->dev, DMA_TO_DEVICE);
  208. }
  209. static void tmc_free_data_pages(struct tmc_sg_table *sg_table)
  210. {
  211. if (sg_table->data_vaddr)
  212. vunmap(sg_table->data_vaddr);
  213. tmc_pages_free(&sg_table->data_pages, sg_table->dev, DMA_FROM_DEVICE);
  214. }
  215. void tmc_free_sg_table(struct tmc_sg_table *sg_table)
  216. {
  217. tmc_free_table_pages(sg_table);
  218. tmc_free_data_pages(sg_table);
  219. }
  220. /*
  221. * Alloc pages for the table. Since this will be used by the device,
  222. * allocate the pages closer to the device (i.e, dev_to_node(dev)
  223. * rather than the CPU node).
  224. */
  225. static int tmc_alloc_table_pages(struct tmc_sg_table *sg_table)
  226. {
  227. int rc;
  228. struct tmc_pages *table_pages = &sg_table->table_pages;
  229. rc = tmc_pages_alloc(table_pages, sg_table->dev,
  230. dev_to_node(sg_table->dev),
  231. DMA_TO_DEVICE, NULL);
  232. if (rc)
  233. return rc;
  234. sg_table->table_vaddr = vmap(table_pages->pages,
  235. table_pages->nr_pages,
  236. VM_MAP,
  237. PAGE_KERNEL);
  238. if (!sg_table->table_vaddr)
  239. rc = -ENOMEM;
  240. else
  241. sg_table->table_daddr = table_pages->daddrs[0];
  242. return rc;
  243. }
  244. static int tmc_alloc_data_pages(struct tmc_sg_table *sg_table, void **pages)
  245. {
  246. int rc;
  247. /* Allocate data pages on the node requested by the caller */
  248. rc = tmc_pages_alloc(&sg_table->data_pages,
  249. sg_table->dev, sg_table->node,
  250. DMA_FROM_DEVICE, pages);
  251. if (!rc) {
  252. sg_table->data_vaddr = vmap(sg_table->data_pages.pages,
  253. sg_table->data_pages.nr_pages,
  254. VM_MAP,
  255. PAGE_KERNEL);
  256. if (!sg_table->data_vaddr)
  257. rc = -ENOMEM;
  258. }
  259. return rc;
  260. }
  261. /*
  262. * tmc_alloc_sg_table: Allocate and setup dma pages for the TMC SG table
  263. * and data buffers. TMC writes to the data buffers and reads from the SG
  264. * Table pages.
  265. *
  266. * @dev - Device to which page should be DMA mapped.
  267. * @node - Numa node for mem allocations
  268. * @nr_tpages - Number of pages for the table entries.
  269. * @nr_dpages - Number of pages for Data buffer.
  270. * @pages - Optional list of virtual address of pages.
  271. */
  272. struct tmc_sg_table *tmc_alloc_sg_table(struct device *dev,
  273. int node,
  274. int nr_tpages,
  275. int nr_dpages,
  276. void **pages)
  277. {
  278. long rc;
  279. struct tmc_sg_table *sg_table;
  280. sg_table = kzalloc(sizeof(*sg_table), GFP_KERNEL);
  281. if (!sg_table)
  282. return ERR_PTR(-ENOMEM);
  283. sg_table->data_pages.nr_pages = nr_dpages;
  284. sg_table->table_pages.nr_pages = nr_tpages;
  285. sg_table->node = node;
  286. sg_table->dev = dev;
  287. rc = tmc_alloc_data_pages(sg_table, pages);
  288. if (!rc)
  289. rc = tmc_alloc_table_pages(sg_table);
  290. if (rc) {
  291. tmc_free_sg_table(sg_table);
  292. kfree(sg_table);
  293. return ERR_PTR(rc);
  294. }
  295. return sg_table;
  296. }
  297. /*
  298. * tmc_sg_table_sync_data_range: Sync the data buffer written
  299. * by the device from @offset upto a @size bytes.
  300. */
  301. void tmc_sg_table_sync_data_range(struct tmc_sg_table *table,
  302. u64 offset, u64 size)
  303. {
  304. int i, index, start;
  305. int npages = DIV_ROUND_UP(size, PAGE_SIZE);
  306. struct device *dev = table->dev;
  307. struct tmc_pages *data = &table->data_pages;
  308. start = offset >> PAGE_SHIFT;
  309. for (i = start; i < (start + npages); i++) {
  310. index = i % data->nr_pages;
  311. dma_sync_single_for_cpu(dev, data->daddrs[index],
  312. PAGE_SIZE, DMA_FROM_DEVICE);
  313. }
  314. }
  315. /* tmc_sg_sync_table: Sync the page table */
  316. void tmc_sg_table_sync_table(struct tmc_sg_table *sg_table)
  317. {
  318. int i;
  319. struct device *dev = sg_table->dev;
  320. struct tmc_pages *table_pages = &sg_table->table_pages;
  321. for (i = 0; i < table_pages->nr_pages; i++)
  322. dma_sync_single_for_device(dev, table_pages->daddrs[i],
  323. PAGE_SIZE, DMA_TO_DEVICE);
  324. }
  325. /*
  326. * tmc_sg_table_get_data: Get the buffer pointer for data @offset
  327. * in the SG buffer. The @bufpp is updated to point to the buffer.
  328. * Returns :
  329. * the length of linear data available at @offset.
  330. * or
  331. * <= 0 if no data is available.
  332. */
  333. ssize_t tmc_sg_table_get_data(struct tmc_sg_table *sg_table,
  334. u64 offset, size_t len, char **bufpp)
  335. {
  336. size_t size;
  337. int pg_idx = offset >> PAGE_SHIFT;
  338. int pg_offset = offset & (PAGE_SIZE - 1);
  339. struct tmc_pages *data_pages = &sg_table->data_pages;
  340. size = tmc_sg_table_buf_size(sg_table);
  341. if (offset >= size)
  342. return -EINVAL;
  343. /* Make sure we don't go beyond the end */
  344. len = (len < (size - offset)) ? len : size - offset;
  345. /* Respect the page boundaries */
  346. len = (len < (PAGE_SIZE - pg_offset)) ? len : (PAGE_SIZE - pg_offset);
  347. if (len > 0)
  348. *bufpp = page_address(data_pages->pages[pg_idx]) + pg_offset;
  349. return len;
  350. }
  351. #ifdef ETR_SG_DEBUG
  352. /* Map a dma address to virtual address */
  353. static unsigned long
  354. tmc_sg_daddr_to_vaddr(struct tmc_sg_table *sg_table,
  355. dma_addr_t addr, bool table)
  356. {
  357. long offset;
  358. unsigned long base;
  359. struct tmc_pages *tmc_pages;
  360. if (table) {
  361. tmc_pages = &sg_table->table_pages;
  362. base = (unsigned long)sg_table->table_vaddr;
  363. } else {
  364. tmc_pages = &sg_table->data_pages;
  365. base = (unsigned long)sg_table->data_vaddr;
  366. }
  367. offset = tmc_pages_get_offset(tmc_pages, addr);
  368. if (offset < 0)
  369. return 0;
  370. return base + offset;
  371. }
  372. /* Dump the given sg_table */
  373. static void tmc_etr_sg_table_dump(struct etr_sg_table *etr_table)
  374. {
  375. sgte_t *ptr;
  376. int i = 0;
  377. dma_addr_t addr;
  378. struct tmc_sg_table *sg_table = etr_table->sg_table;
  379. ptr = (sgte_t *)tmc_sg_daddr_to_vaddr(sg_table,
  380. etr_table->hwaddr, true);
  381. while (ptr) {
  382. addr = ETR_SG_ADDR(*ptr);
  383. switch (ETR_SG_ET(*ptr)) {
  384. case ETR_SG_ET_NORMAL:
  385. dev_dbg(sg_table->dev,
  386. "%05d: %p\t:[N] 0x%llx\n", i, ptr, addr);
  387. ptr++;
  388. break;
  389. case ETR_SG_ET_LINK:
  390. dev_dbg(sg_table->dev,
  391. "%05d: *** %p\t:{L} 0x%llx ***\n",
  392. i, ptr, addr);
  393. ptr = (sgte_t *)tmc_sg_daddr_to_vaddr(sg_table,
  394. addr, true);
  395. break;
  396. case ETR_SG_ET_LAST:
  397. dev_dbg(sg_table->dev,
  398. "%05d: ### %p\t:[L] 0x%llx ###\n",
  399. i, ptr, addr);
  400. return;
  401. default:
  402. dev_dbg(sg_table->dev,
  403. "%05d: xxx %p\t:[INVALID] 0x%llx xxx\n",
  404. i, ptr, addr);
  405. return;
  406. }
  407. i++;
  408. }
  409. dev_dbg(sg_table->dev, "******* End of Table *****\n");
  410. }
  411. #else
  412. static inline void tmc_etr_sg_table_dump(struct etr_sg_table *etr_table) {}
  413. #endif
  414. /*
  415. * Populate the SG Table page table entries from table/data
  416. * pages allocated. Each Data page has ETR_SG_PAGES_PER_SYSPAGE SG pages.
  417. * So does a Table page. So we keep track of indices of the tables
  418. * in each system page and move the pointers accordingly.
  419. */
  420. #define INC_IDX_ROUND(idx, size) ((idx) = ((idx) + 1) % (size))
  421. static void tmc_etr_sg_table_populate(struct etr_sg_table *etr_table)
  422. {
  423. dma_addr_t paddr;
  424. int i, type, nr_entries;
  425. int tpidx = 0; /* index to the current system table_page */
  426. int sgtidx = 0; /* index to the sg_table within the current syspage */
  427. int sgtentry = 0; /* the entry within the sg_table */
  428. int dpidx = 0; /* index to the current system data_page */
  429. int spidx = 0; /* index to the SG page within the current data page */
  430. sgte_t *ptr; /* pointer to the table entry to fill */
  431. struct tmc_sg_table *sg_table = etr_table->sg_table;
  432. dma_addr_t *table_daddrs = sg_table->table_pages.daddrs;
  433. dma_addr_t *data_daddrs = sg_table->data_pages.daddrs;
  434. nr_entries = tmc_etr_sg_table_entries(sg_table->data_pages.nr_pages);
  435. /*
  436. * Use the contiguous virtual address of the table to update entries.
  437. */
  438. ptr = sg_table->table_vaddr;
  439. /*
  440. * Fill all the entries, except the last entry to avoid special
  441. * checks within the loop.
  442. */
  443. for (i = 0; i < nr_entries - 1; i++) {
  444. if (sgtentry == ETR_SG_PTRS_PER_PAGE - 1) {
  445. /*
  446. * Last entry in a sg_table page is a link address to
  447. * the next table page. If this sg_table is the last
  448. * one in the system page, it links to the first
  449. * sg_table in the next system page. Otherwise, it
  450. * links to the next sg_table page within the system
  451. * page.
  452. */
  453. if (sgtidx == ETR_SG_PAGES_PER_SYSPAGE - 1) {
  454. paddr = table_daddrs[tpidx + 1];
  455. } else {
  456. paddr = table_daddrs[tpidx] +
  457. (ETR_SG_PAGE_SIZE * (sgtidx + 1));
  458. }
  459. type = ETR_SG_ET_LINK;
  460. } else {
  461. /*
  462. * Update the indices to the data_pages to point to the
  463. * next sg_page in the data buffer.
  464. */
  465. type = ETR_SG_ET_NORMAL;
  466. paddr = data_daddrs[dpidx] + spidx * ETR_SG_PAGE_SIZE;
  467. if (!INC_IDX_ROUND(spidx, ETR_SG_PAGES_PER_SYSPAGE))
  468. dpidx++;
  469. }
  470. *ptr++ = ETR_SG_ENTRY(paddr, type);
  471. /*
  472. * Move to the next table pointer, moving the table page index
  473. * if necessary
  474. */
  475. if (!INC_IDX_ROUND(sgtentry, ETR_SG_PTRS_PER_PAGE)) {
  476. if (!INC_IDX_ROUND(sgtidx, ETR_SG_PAGES_PER_SYSPAGE))
  477. tpidx++;
  478. }
  479. }
  480. /* Set up the last entry, which is always a data pointer */
  481. paddr = data_daddrs[dpidx] + spidx * ETR_SG_PAGE_SIZE;
  482. *ptr++ = ETR_SG_ENTRY(paddr, ETR_SG_ET_LAST);
  483. }
  484. /*
  485. * tmc_init_etr_sg_table: Allocate a TMC ETR SG table, data buffer of @size and
  486. * populate the table.
  487. *
  488. * @dev - Device pointer for the TMC
  489. * @node - NUMA node where the memory should be allocated
  490. * @size - Total size of the data buffer
  491. * @pages - Optional list of page virtual address
  492. */
  493. static struct etr_sg_table *
  494. tmc_init_etr_sg_table(struct device *dev, int node,
  495. unsigned long size, void **pages)
  496. {
  497. int nr_entries, nr_tpages;
  498. int nr_dpages = size >> PAGE_SHIFT;
  499. struct tmc_sg_table *sg_table;
  500. struct etr_sg_table *etr_table;
  501. etr_table = kzalloc(sizeof(*etr_table), GFP_KERNEL);
  502. if (!etr_table)
  503. return ERR_PTR(-ENOMEM);
  504. nr_entries = tmc_etr_sg_table_entries(nr_dpages);
  505. nr_tpages = DIV_ROUND_UP(nr_entries, ETR_SG_PTRS_PER_SYSPAGE);
  506. sg_table = tmc_alloc_sg_table(dev, node, nr_tpages, nr_dpages, pages);
  507. if (IS_ERR(sg_table)) {
  508. kfree(etr_table);
  509. return ERR_CAST(sg_table);
  510. }
  511. etr_table->sg_table = sg_table;
  512. /* TMC should use table base address for DBA */
  513. etr_table->hwaddr = sg_table->table_daddr;
  514. tmc_etr_sg_table_populate(etr_table);
  515. /* Sync the table pages for the HW */
  516. tmc_sg_table_sync_table(sg_table);
  517. tmc_etr_sg_table_dump(etr_table);
  518. return etr_table;
  519. }
  520. /*
  521. * tmc_etr_alloc_flat_buf: Allocate a contiguous DMA buffer.
  522. */
  523. static int tmc_etr_alloc_flat_buf(struct tmc_drvdata *drvdata,
  524. struct etr_buf *etr_buf, int node,
  525. void **pages)
  526. {
  527. struct etr_flat_buf *flat_buf;
  528. /* We cannot reuse existing pages for flat buf */
  529. if (pages)
  530. return -EINVAL;
  531. flat_buf = kzalloc(sizeof(*flat_buf), GFP_KERNEL);
  532. if (!flat_buf)
  533. return -ENOMEM;
  534. flat_buf->vaddr = dma_alloc_coherent(drvdata->dev, etr_buf->size,
  535. &flat_buf->daddr, GFP_KERNEL);
  536. if (!flat_buf->vaddr) {
  537. kfree(flat_buf);
  538. return -ENOMEM;
  539. }
  540. flat_buf->size = etr_buf->size;
  541. flat_buf->dev = drvdata->dev;
  542. etr_buf->hwaddr = flat_buf->daddr;
  543. etr_buf->mode = ETR_MODE_FLAT;
  544. etr_buf->private = flat_buf;
  545. return 0;
  546. }
  547. static void tmc_etr_free_flat_buf(struct etr_buf *etr_buf)
  548. {
  549. struct etr_flat_buf *flat_buf = etr_buf->private;
  550. if (flat_buf && flat_buf->daddr)
  551. dma_free_coherent(flat_buf->dev, flat_buf->size,
  552. flat_buf->vaddr, flat_buf->daddr);
  553. kfree(flat_buf);
  554. }
  555. static void tmc_etr_sync_flat_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
  556. {
  557. /*
  558. * Adjust the buffer to point to the beginning of the trace data
  559. * and update the available trace data.
  560. */
  561. etr_buf->offset = rrp - etr_buf->hwaddr;
  562. if (etr_buf->full)
  563. etr_buf->len = etr_buf->size;
  564. else
  565. etr_buf->len = rwp - rrp;
  566. }
  567. static ssize_t tmc_etr_get_data_flat_buf(struct etr_buf *etr_buf,
  568. u64 offset, size_t len, char **bufpp)
  569. {
  570. struct etr_flat_buf *flat_buf = etr_buf->private;
  571. *bufpp = (char *)flat_buf->vaddr + offset;
  572. /*
  573. * tmc_etr_buf_get_data already adjusts the length to handle
  574. * buffer wrapping around.
  575. */
  576. return len;
  577. }
  578. static const struct etr_buf_operations etr_flat_buf_ops = {
  579. .alloc = tmc_etr_alloc_flat_buf,
  580. .free = tmc_etr_free_flat_buf,
  581. .sync = tmc_etr_sync_flat_buf,
  582. .get_data = tmc_etr_get_data_flat_buf,
  583. };
  584. /*
  585. * tmc_etr_alloc_sg_buf: Allocate an SG buf @etr_buf. Setup the parameters
  586. * appropriately.
  587. */
  588. static int tmc_etr_alloc_sg_buf(struct tmc_drvdata *drvdata,
  589. struct etr_buf *etr_buf, int node,
  590. void **pages)
  591. {
  592. struct etr_sg_table *etr_table;
  593. etr_table = tmc_init_etr_sg_table(drvdata->dev, node,
  594. etr_buf->size, pages);
  595. if (IS_ERR(etr_table))
  596. return -ENOMEM;
  597. etr_buf->hwaddr = etr_table->hwaddr;
  598. etr_buf->mode = ETR_MODE_ETR_SG;
  599. etr_buf->private = etr_table;
  600. return 0;
  601. }
  602. static void tmc_etr_free_sg_buf(struct etr_buf *etr_buf)
  603. {
  604. struct etr_sg_table *etr_table = etr_buf->private;
  605. if (etr_table) {
  606. tmc_free_sg_table(etr_table->sg_table);
  607. kfree(etr_table);
  608. }
  609. }
  610. static ssize_t tmc_etr_get_data_sg_buf(struct etr_buf *etr_buf, u64 offset,
  611. size_t len, char **bufpp)
  612. {
  613. struct etr_sg_table *etr_table = etr_buf->private;
  614. return tmc_sg_table_get_data(etr_table->sg_table, offset, len, bufpp);
  615. }
  616. static void tmc_etr_sync_sg_buf(struct etr_buf *etr_buf, u64 rrp, u64 rwp)
  617. {
  618. long r_offset, w_offset;
  619. struct etr_sg_table *etr_table = etr_buf->private;
  620. struct tmc_sg_table *table = etr_table->sg_table;
  621. /* Convert hw address to offset in the buffer */
  622. r_offset = tmc_sg_get_data_page_offset(table, rrp);
  623. if (r_offset < 0) {
  624. dev_warn(table->dev,
  625. "Unable to map RRP %llx to offset\n", rrp);
  626. etr_buf->len = 0;
  627. return;
  628. }
  629. w_offset = tmc_sg_get_data_page_offset(table, rwp);
  630. if (w_offset < 0) {
  631. dev_warn(table->dev,
  632. "Unable to map RWP %llx to offset\n", rwp);
  633. etr_buf->len = 0;
  634. return;
  635. }
  636. etr_buf->offset = r_offset;
  637. if (etr_buf->full)
  638. etr_buf->len = etr_buf->size;
  639. else
  640. etr_buf->len = ((w_offset < r_offset) ? etr_buf->size : 0) +
  641. w_offset - r_offset;
  642. tmc_sg_table_sync_data_range(table, r_offset, etr_buf->len);
  643. }
  644. static const struct etr_buf_operations etr_sg_buf_ops = {
  645. .alloc = tmc_etr_alloc_sg_buf,
  646. .free = tmc_etr_free_sg_buf,
  647. .sync = tmc_etr_sync_sg_buf,
  648. .get_data = tmc_etr_get_data_sg_buf,
  649. };
  650. /*
  651. * TMC ETR could be connected to a CATU device, which can provide address
  652. * translation service. This is represented by the Output port of the TMC
  653. * (ETR) connected to the input port of the CATU.
  654. *
  655. * Returns : coresight_device ptr for the CATU device if a CATU is found.
  656. * : NULL otherwise.
  657. */
  658. struct coresight_device *
  659. tmc_etr_get_catu_device(struct tmc_drvdata *drvdata)
  660. {
  661. int i;
  662. struct coresight_device *tmp, *etr = drvdata->csdev;
  663. if (!IS_ENABLED(CONFIG_CORESIGHT_CATU))
  664. return NULL;
  665. for (i = 0; i < etr->nr_outport; i++) {
  666. tmp = etr->conns[i].child_dev;
  667. if (tmp && coresight_is_catu_device(tmp))
  668. return tmp;
  669. }
  670. return NULL;
  671. }
  672. static inline int tmc_etr_enable_catu(struct tmc_drvdata *drvdata,
  673. struct etr_buf *etr_buf)
  674. {
  675. struct coresight_device *catu = tmc_etr_get_catu_device(drvdata);
  676. if (catu && helper_ops(catu)->enable)
  677. return helper_ops(catu)->enable(catu, etr_buf);
  678. return 0;
  679. }
  680. static inline void tmc_etr_disable_catu(struct tmc_drvdata *drvdata)
  681. {
  682. struct coresight_device *catu = tmc_etr_get_catu_device(drvdata);
  683. if (catu && helper_ops(catu)->disable)
  684. helper_ops(catu)->disable(catu, drvdata->etr_buf);
  685. }
  686. static const struct etr_buf_operations *etr_buf_ops[] = {
  687. [ETR_MODE_FLAT] = &etr_flat_buf_ops,
  688. [ETR_MODE_ETR_SG] = &etr_sg_buf_ops,
  689. [ETR_MODE_CATU] = &etr_catu_buf_ops,
  690. };
  691. static inline int tmc_etr_mode_alloc_buf(int mode,
  692. struct tmc_drvdata *drvdata,
  693. struct etr_buf *etr_buf, int node,
  694. void **pages)
  695. {
  696. int rc = -EINVAL;
  697. switch (mode) {
  698. case ETR_MODE_FLAT:
  699. case ETR_MODE_ETR_SG:
  700. case ETR_MODE_CATU:
  701. if (etr_buf_ops[mode]->alloc)
  702. rc = etr_buf_ops[mode]->alloc(drvdata, etr_buf,
  703. node, pages);
  704. if (!rc)
  705. etr_buf->ops = etr_buf_ops[mode];
  706. return rc;
  707. default:
  708. return -EINVAL;
  709. }
  710. }
  711. /*
  712. * tmc_alloc_etr_buf: Allocate a buffer use by ETR.
  713. * @drvdata : ETR device details.
  714. * @size : size of the requested buffer.
  715. * @flags : Required properties for the buffer.
  716. * @node : Node for memory allocations.
  717. * @pages : An optional list of pages.
  718. */
  719. static struct etr_buf *tmc_alloc_etr_buf(struct tmc_drvdata *drvdata,
  720. ssize_t size, int flags,
  721. int node, void **pages)
  722. {
  723. int rc = -ENOMEM;
  724. bool has_etr_sg, has_iommu;
  725. bool has_sg, has_catu;
  726. struct etr_buf *etr_buf;
  727. has_etr_sg = tmc_etr_has_cap(drvdata, TMC_ETR_SG);
  728. has_iommu = iommu_get_domain_for_dev(drvdata->dev);
  729. has_catu = !!tmc_etr_get_catu_device(drvdata);
  730. has_sg = has_catu || has_etr_sg;
  731. etr_buf = kzalloc(sizeof(*etr_buf), GFP_KERNEL);
  732. if (!etr_buf)
  733. return ERR_PTR(-ENOMEM);
  734. etr_buf->size = size;
  735. /*
  736. * If we have to use an existing list of pages, we cannot reliably
  737. * use a contiguous DMA memory (even if we have an IOMMU). Otherwise,
  738. * we use the contiguous DMA memory if at least one of the following
  739. * conditions is true:
  740. * a) The ETR cannot use Scatter-Gather.
  741. * b) we have a backing IOMMU
  742. * c) The requested memory size is smaller (< 1M).
  743. *
  744. * Fallback to available mechanisms.
  745. *
  746. */
  747. if (!pages &&
  748. (!has_sg || has_iommu || size < SZ_1M))
  749. rc = tmc_etr_mode_alloc_buf(ETR_MODE_FLAT, drvdata,
  750. etr_buf, node, pages);
  751. if (rc && has_etr_sg)
  752. rc = tmc_etr_mode_alloc_buf(ETR_MODE_ETR_SG, drvdata,
  753. etr_buf, node, pages);
  754. if (rc && has_catu)
  755. rc = tmc_etr_mode_alloc_buf(ETR_MODE_CATU, drvdata,
  756. etr_buf, node, pages);
  757. if (rc) {
  758. kfree(etr_buf);
  759. return ERR_PTR(rc);
  760. }
  761. dev_dbg(drvdata->dev, "allocated buffer of size %ldKB in mode %d\n",
  762. (unsigned long)size >> 10, etr_buf->mode);
  763. return etr_buf;
  764. }
  765. static void tmc_free_etr_buf(struct etr_buf *etr_buf)
  766. {
  767. WARN_ON(!etr_buf->ops || !etr_buf->ops->free);
  768. etr_buf->ops->free(etr_buf);
  769. kfree(etr_buf);
  770. }
  771. /*
  772. * tmc_etr_buf_get_data: Get the pointer the trace data at @offset
  773. * with a maximum of @len bytes.
  774. * Returns: The size of the linear data available @pos, with *bufpp
  775. * updated to point to the buffer.
  776. */
  777. static ssize_t tmc_etr_buf_get_data(struct etr_buf *etr_buf,
  778. u64 offset, size_t len, char **bufpp)
  779. {
  780. /* Adjust the length to limit this transaction to end of buffer */
  781. len = (len < (etr_buf->size - offset)) ? len : etr_buf->size - offset;
  782. return etr_buf->ops->get_data(etr_buf, (u64)offset, len, bufpp);
  783. }
  784. static inline s64
  785. tmc_etr_buf_insert_barrier_packet(struct etr_buf *etr_buf, u64 offset)
  786. {
  787. ssize_t len;
  788. char *bufp;
  789. len = tmc_etr_buf_get_data(etr_buf, offset,
  790. CORESIGHT_BARRIER_PKT_SIZE, &bufp);
  791. if (WARN_ON(len < CORESIGHT_BARRIER_PKT_SIZE))
  792. return -EINVAL;
  793. coresight_insert_barrier_packet(bufp);
  794. return offset + CORESIGHT_BARRIER_PKT_SIZE;
  795. }
  796. /*
  797. * tmc_sync_etr_buf: Sync the trace buffer availability with drvdata.
  798. * Makes sure the trace data is synced to the memory for consumption.
  799. * @etr_buf->offset will hold the offset to the beginning of the trace data
  800. * within the buffer, with @etr_buf->len bytes to consume.
  801. */
  802. static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
  803. {
  804. struct etr_buf *etr_buf = drvdata->etr_buf;
  805. u64 rrp, rwp;
  806. u32 status;
  807. rrp = tmc_read_rrp(drvdata);
  808. rwp = tmc_read_rwp(drvdata);
  809. status = readl_relaxed(drvdata->base + TMC_STS);
  810. etr_buf->full = status & TMC_STS_FULL;
  811. WARN_ON(!etr_buf->ops || !etr_buf->ops->sync);
  812. etr_buf->ops->sync(etr_buf, rrp, rwp);
  813. /* Insert barrier packets at the beginning, if there was an overflow */
  814. if (etr_buf->full)
  815. tmc_etr_buf_insert_barrier_packet(etr_buf, etr_buf->offset);
  816. }
  817. static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
  818. {
  819. u32 axictl, sts;
  820. struct etr_buf *etr_buf = drvdata->etr_buf;
  821. CS_UNLOCK(drvdata->base);
  822. /* Wait for TMCSReady bit to be set */
  823. tmc_wait_for_tmcready(drvdata);
  824. writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
  825. writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
  826. axictl = readl_relaxed(drvdata->base + TMC_AXICTL);
  827. axictl &= ~TMC_AXICTL_CLEAR_MASK;
  828. axictl |= (TMC_AXICTL_PROT_CTL_B1 | TMC_AXICTL_WR_BURST_16);
  829. axictl |= TMC_AXICTL_AXCACHE_OS;
  830. if (tmc_etr_has_cap(drvdata, TMC_ETR_AXI_ARCACHE)) {
  831. axictl &= ~TMC_AXICTL_ARCACHE_MASK;
  832. axictl |= TMC_AXICTL_ARCACHE_OS;
  833. }
  834. if (etr_buf->mode == ETR_MODE_ETR_SG)
  835. axictl |= TMC_AXICTL_SCT_GAT_MODE;
  836. writel_relaxed(axictl, drvdata->base + TMC_AXICTL);
  837. tmc_write_dba(drvdata, etr_buf->hwaddr);
  838. /*
  839. * If the TMC pointers must be programmed before the session,
  840. * we have to set it properly (i.e, RRP/RWP to base address and
  841. * STS to "not full").
  842. */
  843. if (tmc_etr_has_cap(drvdata, TMC_ETR_SAVE_RESTORE)) {
  844. tmc_write_rrp(drvdata, etr_buf->hwaddr);
  845. tmc_write_rwp(drvdata, etr_buf->hwaddr);
  846. sts = readl_relaxed(drvdata->base + TMC_STS) & ~TMC_STS_FULL;
  847. writel_relaxed(sts, drvdata->base + TMC_STS);
  848. }
  849. writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
  850. TMC_FFCR_FON_FLIN | TMC_FFCR_FON_TRIG_EVT |
  851. TMC_FFCR_TRIGON_TRIGIN,
  852. drvdata->base + TMC_FFCR);
  853. writel_relaxed(drvdata->trigger_cntr, drvdata->base + TMC_TRG);
  854. tmc_enable_hw(drvdata);
  855. CS_LOCK(drvdata->base);
  856. }
  857. static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
  858. struct etr_buf *etr_buf)
  859. {
  860. int rc;
  861. /* Callers should provide an appropriate buffer for use */
  862. if (WARN_ON(!etr_buf))
  863. return -EINVAL;
  864. if ((etr_buf->mode == ETR_MODE_ETR_SG) &&
  865. WARN_ON(!tmc_etr_has_cap(drvdata, TMC_ETR_SG)))
  866. return -EINVAL;
  867. if (WARN_ON(drvdata->etr_buf))
  868. return -EBUSY;
  869. /*
  870. * If this ETR is connected to a CATU, enable it before we turn
  871. * this on.
  872. */
  873. rc = tmc_etr_enable_catu(drvdata, etr_buf);
  874. if (rc)
  875. return rc;
  876. rc = coresight_claim_device(drvdata->base);
  877. if (!rc) {
  878. drvdata->etr_buf = etr_buf;
  879. __tmc_etr_enable_hw(drvdata);
  880. }
  881. return rc;
  882. }
  883. /*
  884. * Return the available trace data in the buffer (starts at etr_buf->offset,
  885. * limited by etr_buf->len) from @pos, with a maximum limit of @len,
  886. * also updating the @bufpp on where to find it. Since the trace data
  887. * starts at anywhere in the buffer, depending on the RRP, we adjust the
  888. * @len returned to handle buffer wrapping around.
  889. *
  890. * We are protected here by drvdata->reading != 0, which ensures the
  891. * sysfs_buf stays alive.
  892. */
  893. ssize_t tmc_etr_get_sysfs_trace(struct tmc_drvdata *drvdata,
  894. loff_t pos, size_t len, char **bufpp)
  895. {
  896. s64 offset;
  897. ssize_t actual = len;
  898. struct etr_buf *etr_buf = drvdata->sysfs_buf;
  899. if (pos + actual > etr_buf->len)
  900. actual = etr_buf->len - pos;
  901. if (actual <= 0)
  902. return actual;
  903. /* Compute the offset from which we read the data */
  904. offset = etr_buf->offset + pos;
  905. if (offset >= etr_buf->size)
  906. offset -= etr_buf->size;
  907. return tmc_etr_buf_get_data(etr_buf, offset, actual, bufpp);
  908. }
  909. static struct etr_buf *
  910. tmc_etr_setup_sysfs_buf(struct tmc_drvdata *drvdata)
  911. {
  912. return tmc_alloc_etr_buf(drvdata, drvdata->size,
  913. 0, cpu_to_node(0), NULL);
  914. }
  915. static void
  916. tmc_etr_free_sysfs_buf(struct etr_buf *buf)
  917. {
  918. if (buf)
  919. tmc_free_etr_buf(buf);
  920. }
  921. static void tmc_etr_sync_sysfs_buf(struct tmc_drvdata *drvdata)
  922. {
  923. struct etr_buf *etr_buf = drvdata->etr_buf;
  924. if (WARN_ON(drvdata->sysfs_buf != etr_buf)) {
  925. tmc_etr_free_sysfs_buf(drvdata->sysfs_buf);
  926. drvdata->sysfs_buf = NULL;
  927. } else {
  928. tmc_sync_etr_buf(drvdata);
  929. }
  930. }
  931. static void __tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
  932. {
  933. CS_UNLOCK(drvdata->base);
  934. tmc_flush_and_stop(drvdata);
  935. /*
  936. * When operating in sysFS mode the content of the buffer needs to be
  937. * read before the TMC is disabled.
  938. */
  939. if (drvdata->mode == CS_MODE_SYSFS)
  940. tmc_etr_sync_sysfs_buf(drvdata);
  941. tmc_disable_hw(drvdata);
  942. CS_LOCK(drvdata->base);
  943. }
  944. static void tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
  945. {
  946. __tmc_etr_disable_hw(drvdata);
  947. /* Disable CATU device if this ETR is connected to one */
  948. tmc_etr_disable_catu(drvdata);
  949. coresight_disclaim_device(drvdata->base);
  950. /* Reset the ETR buf used by hardware */
  951. drvdata->etr_buf = NULL;
  952. }
  953. static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)
  954. {
  955. int ret = 0;
  956. unsigned long flags;
  957. struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  958. struct etr_buf *sysfs_buf = NULL, *new_buf = NULL, *free_buf = NULL;
  959. /*
  960. * If we are enabling the ETR from disabled state, we need to make
  961. * sure we have a buffer with the right size. The etr_buf is not reset
  962. * immediately after we stop the tracing in SYSFS mode as we wait for
  963. * the user to collect the data. We may be able to reuse the existing
  964. * buffer, provided the size matches. Any allocation has to be done
  965. * with the lock released.
  966. */
  967. spin_lock_irqsave(&drvdata->spinlock, flags);
  968. sysfs_buf = READ_ONCE(drvdata->sysfs_buf);
  969. if (!sysfs_buf || (sysfs_buf->size != drvdata->size)) {
  970. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  971. /* Allocate memory with the locks released */
  972. free_buf = new_buf = tmc_etr_setup_sysfs_buf(drvdata);
  973. if (IS_ERR(new_buf))
  974. return PTR_ERR(new_buf);
  975. /* Let's try again */
  976. spin_lock_irqsave(&drvdata->spinlock, flags);
  977. }
  978. if (drvdata->reading || drvdata->mode == CS_MODE_PERF) {
  979. ret = -EBUSY;
  980. goto out;
  981. }
  982. /*
  983. * In sysFS mode we can have multiple writers per sink. Since this
  984. * sink is already enabled no memory is needed and the HW need not be
  985. * touched, even if the buffer size has changed.
  986. */
  987. if (drvdata->mode == CS_MODE_SYSFS)
  988. goto out;
  989. /*
  990. * If we don't have a buffer or it doesn't match the requested size,
  991. * use the buffer allocated above. Otherwise reuse the existing buffer.
  992. */
  993. sysfs_buf = READ_ONCE(drvdata->sysfs_buf);
  994. if (!sysfs_buf || (new_buf && sysfs_buf->size != new_buf->size)) {
  995. free_buf = sysfs_buf;
  996. drvdata->sysfs_buf = new_buf;
  997. }
  998. ret = tmc_etr_enable_hw(drvdata, drvdata->sysfs_buf);
  999. if (!ret)
  1000. drvdata->mode = CS_MODE_SYSFS;
  1001. out:
  1002. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  1003. /* Free memory outside the spinlock if need be */
  1004. if (free_buf)
  1005. tmc_etr_free_sysfs_buf(free_buf);
  1006. if (!ret)
  1007. dev_dbg(drvdata->dev, "TMC-ETR enabled\n");
  1008. return ret;
  1009. }
  1010. /*
  1011. * tmc_etr_setup_perf_buf: Allocate ETR buffer for use by perf.
  1012. * The size of the hardware buffer is dependent on the size configured
  1013. * via sysfs and the perf ring buffer size. We prefer to allocate the
  1014. * largest possible size, scaling down the size by half until it
  1015. * reaches a minimum limit (1M), beyond which we give up.
  1016. */
  1017. static struct etr_perf_buffer *
  1018. tmc_etr_setup_perf_buf(struct tmc_drvdata *drvdata, int node, int nr_pages,
  1019. void **pages, bool snapshot)
  1020. {
  1021. struct etr_buf *etr_buf;
  1022. struct etr_perf_buffer *etr_perf;
  1023. unsigned long size;
  1024. etr_perf = kzalloc_node(sizeof(*etr_perf), GFP_KERNEL, node);
  1025. if (!etr_perf)
  1026. return ERR_PTR(-ENOMEM);
  1027. /*
  1028. * Try to match the perf ring buffer size if it is larger
  1029. * than the size requested via sysfs.
  1030. */
  1031. if ((nr_pages << PAGE_SHIFT) > drvdata->size) {
  1032. etr_buf = tmc_alloc_etr_buf(drvdata, (nr_pages << PAGE_SHIFT),
  1033. 0, node, NULL);
  1034. if (!IS_ERR(etr_buf))
  1035. goto done;
  1036. }
  1037. /*
  1038. * Else switch to configured size for this ETR
  1039. * and scale down until we hit the minimum limit.
  1040. */
  1041. size = drvdata->size;
  1042. do {
  1043. etr_buf = tmc_alloc_etr_buf(drvdata, size, 0, node, NULL);
  1044. if (!IS_ERR(etr_buf))
  1045. goto done;
  1046. size /= 2;
  1047. } while (size >= TMC_ETR_PERF_MIN_BUF_SIZE);
  1048. kfree(etr_perf);
  1049. return ERR_PTR(-ENOMEM);
  1050. done:
  1051. etr_perf->etr_buf = etr_buf;
  1052. return etr_perf;
  1053. }
  1054. static void *tmc_alloc_etr_buffer(struct coresight_device *csdev,
  1055. int cpu, void **pages, int nr_pages,
  1056. bool snapshot)
  1057. {
  1058. struct etr_perf_buffer *etr_perf;
  1059. struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  1060. if (cpu == -1)
  1061. cpu = smp_processor_id();
  1062. etr_perf = tmc_etr_setup_perf_buf(drvdata, cpu_to_node(cpu),
  1063. nr_pages, pages, snapshot);
  1064. if (IS_ERR(etr_perf)) {
  1065. dev_dbg(drvdata->dev, "Unable to allocate ETR buffer\n");
  1066. return NULL;
  1067. }
  1068. etr_perf->snapshot = snapshot;
  1069. etr_perf->nr_pages = nr_pages;
  1070. etr_perf->pages = pages;
  1071. return etr_perf;
  1072. }
  1073. static void tmc_free_etr_buffer(void *config)
  1074. {
  1075. struct etr_perf_buffer *etr_perf = config;
  1076. if (etr_perf->etr_buf)
  1077. tmc_free_etr_buf(etr_perf->etr_buf);
  1078. kfree(etr_perf);
  1079. }
  1080. /*
  1081. * tmc_etr_sync_perf_buffer: Copy the actual trace data from the hardware
  1082. * buffer to the perf ring buffer.
  1083. */
  1084. static void tmc_etr_sync_perf_buffer(struct etr_perf_buffer *etr_perf)
  1085. {
  1086. long bytes, to_copy;
  1087. long pg_idx, pg_offset, src_offset;
  1088. unsigned long head = etr_perf->head;
  1089. char **dst_pages, *src_buf;
  1090. struct etr_buf *etr_buf = etr_perf->etr_buf;
  1091. head = etr_perf->head;
  1092. pg_idx = head >> PAGE_SHIFT;
  1093. pg_offset = head & (PAGE_SIZE - 1);
  1094. dst_pages = (char **)etr_perf->pages;
  1095. src_offset = etr_buf->offset;
  1096. to_copy = etr_buf->len;
  1097. while (to_copy > 0) {
  1098. /*
  1099. * In one iteration, we can copy minimum of :
  1100. * 1) what is available in the source buffer,
  1101. * 2) what is available in the source buffer, before it
  1102. * wraps around.
  1103. * 3) what is available in the destination page.
  1104. * in one iteration.
  1105. */
  1106. bytes = tmc_etr_buf_get_data(etr_buf, src_offset, to_copy,
  1107. &src_buf);
  1108. if (WARN_ON_ONCE(bytes <= 0))
  1109. break;
  1110. bytes = min(bytes, (long)(PAGE_SIZE - pg_offset));
  1111. memcpy(dst_pages[pg_idx] + pg_offset, src_buf, bytes);
  1112. to_copy -= bytes;
  1113. /* Move destination pointers */
  1114. pg_offset += bytes;
  1115. if (pg_offset == PAGE_SIZE) {
  1116. pg_offset = 0;
  1117. if (++pg_idx == etr_perf->nr_pages)
  1118. pg_idx = 0;
  1119. }
  1120. /* Move source pointers */
  1121. src_offset += bytes;
  1122. if (src_offset >= etr_buf->size)
  1123. src_offset -= etr_buf->size;
  1124. }
  1125. }
  1126. /*
  1127. * tmc_update_etr_buffer : Update the perf ring buffer with the
  1128. * available trace data. We use software double buffering at the moment.
  1129. *
  1130. * TODO: Add support for reusing the perf ring buffer.
  1131. */
  1132. static unsigned long
  1133. tmc_update_etr_buffer(struct coresight_device *csdev,
  1134. struct perf_output_handle *handle,
  1135. void *config)
  1136. {
  1137. bool lost = false;
  1138. unsigned long flags, size = 0;
  1139. struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  1140. struct etr_perf_buffer *etr_perf = config;
  1141. struct etr_buf *etr_buf = etr_perf->etr_buf;
  1142. spin_lock_irqsave(&drvdata->spinlock, flags);
  1143. if (WARN_ON(drvdata->perf_data != etr_perf)) {
  1144. lost = true;
  1145. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  1146. goto out;
  1147. }
  1148. CS_UNLOCK(drvdata->base);
  1149. tmc_flush_and_stop(drvdata);
  1150. tmc_sync_etr_buf(drvdata);
  1151. CS_LOCK(drvdata->base);
  1152. /* Reset perf specific data */
  1153. drvdata->perf_data = NULL;
  1154. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  1155. size = etr_buf->len;
  1156. tmc_etr_sync_perf_buffer(etr_perf);
  1157. /*
  1158. * Update handle->head in snapshot mode. Also update the size to the
  1159. * hardware buffer size if there was an overflow.
  1160. */
  1161. if (etr_perf->snapshot) {
  1162. handle->head += size;
  1163. if (etr_buf->full)
  1164. size = etr_buf->size;
  1165. }
  1166. lost |= etr_buf->full;
  1167. out:
  1168. if (lost)
  1169. perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
  1170. return size;
  1171. }
  1172. static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
  1173. {
  1174. int rc = 0;
  1175. unsigned long flags;
  1176. struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  1177. struct perf_output_handle *handle = data;
  1178. struct etr_perf_buffer *etr_perf = etm_perf_sink_config(handle);
  1179. spin_lock_irqsave(&drvdata->spinlock, flags);
  1180. /*
  1181. * There can be only one writer per sink in perf mode. If the sink
  1182. * is already open in SYSFS mode, we can't use it.
  1183. */
  1184. if (drvdata->mode != CS_MODE_DISABLED || WARN_ON(drvdata->perf_data)) {
  1185. rc = -EBUSY;
  1186. goto unlock_out;
  1187. }
  1188. if (WARN_ON(!etr_perf || !etr_perf->etr_buf)) {
  1189. rc = -EINVAL;
  1190. goto unlock_out;
  1191. }
  1192. etr_perf->head = PERF_IDX2OFF(handle->head, etr_perf);
  1193. drvdata->perf_data = etr_perf;
  1194. rc = tmc_etr_enable_hw(drvdata, etr_perf->etr_buf);
  1195. if (!rc)
  1196. drvdata->mode = CS_MODE_PERF;
  1197. unlock_out:
  1198. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  1199. return rc;
  1200. }
  1201. static int tmc_enable_etr_sink(struct coresight_device *csdev,
  1202. u32 mode, void *data)
  1203. {
  1204. switch (mode) {
  1205. case CS_MODE_SYSFS:
  1206. return tmc_enable_etr_sink_sysfs(csdev);
  1207. case CS_MODE_PERF:
  1208. return tmc_enable_etr_sink_perf(csdev, data);
  1209. }
  1210. /* We shouldn't be here */
  1211. return -EINVAL;
  1212. }
  1213. static void tmc_disable_etr_sink(struct coresight_device *csdev)
  1214. {
  1215. unsigned long flags;
  1216. struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  1217. spin_lock_irqsave(&drvdata->spinlock, flags);
  1218. if (drvdata->reading) {
  1219. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  1220. return;
  1221. }
  1222. /* Disable the TMC only if it needs to */
  1223. if (drvdata->mode != CS_MODE_DISABLED) {
  1224. tmc_etr_disable_hw(drvdata);
  1225. drvdata->mode = CS_MODE_DISABLED;
  1226. }
  1227. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  1228. dev_dbg(drvdata->dev, "TMC-ETR disabled\n");
  1229. }
  1230. static const struct coresight_ops_sink tmc_etr_sink_ops = {
  1231. .enable = tmc_enable_etr_sink,
  1232. .disable = tmc_disable_etr_sink,
  1233. .alloc_buffer = tmc_alloc_etr_buffer,
  1234. .update_buffer = tmc_update_etr_buffer,
  1235. .free_buffer = tmc_free_etr_buffer,
  1236. };
  1237. const struct coresight_ops tmc_etr_cs_ops = {
  1238. .sink_ops = &tmc_etr_sink_ops,
  1239. };
  1240. int tmc_read_prepare_etr(struct tmc_drvdata *drvdata)
  1241. {
  1242. int ret = 0;
  1243. unsigned long flags;
  1244. /* config types are set a boot time and never change */
  1245. if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
  1246. return -EINVAL;
  1247. spin_lock_irqsave(&drvdata->spinlock, flags);
  1248. if (drvdata->reading) {
  1249. ret = -EBUSY;
  1250. goto out;
  1251. }
  1252. /*
  1253. * We can safely allow reads even if the ETR is operating in PERF mode,
  1254. * since the sysfs session is captured in mode specific data.
  1255. * If drvdata::sysfs_data is NULL the trace data has been read already.
  1256. */
  1257. if (!drvdata->sysfs_buf) {
  1258. ret = -EINVAL;
  1259. goto out;
  1260. }
  1261. /* Disable the TMC if we are trying to read from a running session. */
  1262. if (drvdata->mode == CS_MODE_SYSFS)
  1263. __tmc_etr_disable_hw(drvdata);
  1264. drvdata->reading = true;
  1265. out:
  1266. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  1267. return ret;
  1268. }
  1269. int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
  1270. {
  1271. unsigned long flags;
  1272. struct etr_buf *sysfs_buf = NULL;
  1273. /* config types are set a boot time and never change */
  1274. if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
  1275. return -EINVAL;
  1276. spin_lock_irqsave(&drvdata->spinlock, flags);
  1277. /* RE-enable the TMC if need be */
  1278. if (drvdata->mode == CS_MODE_SYSFS) {
  1279. /*
  1280. * The trace run will continue with the same allocated trace
  1281. * buffer. Since the tracer is still enabled drvdata::buf can't
  1282. * be NULL.
  1283. */
  1284. __tmc_etr_enable_hw(drvdata);
  1285. } else {
  1286. /*
  1287. * The ETR is not tracing and the buffer was just read.
  1288. * As such prepare to free the trace buffer.
  1289. */
  1290. sysfs_buf = drvdata->sysfs_buf;
  1291. drvdata->sysfs_buf = NULL;
  1292. }
  1293. drvdata->reading = false;
  1294. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  1295. /* Free allocated memory out side of the spinlock */
  1296. if (sysfs_buf)
  1297. tmc_etr_free_sysfs_buf(sysfs_buf);
  1298. return 0;
  1299. }