1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
4 * Description: CoreSight Trace Memory Controller driver
7 #include <linux/acpi.h>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/types.h>
11 #include <linux/device.h>
12 #include <linux/idr.h>
14 #include <linux/iommu.h>
15 #include <linux/err.h>
17 #include <linux/miscdevice.h>
18 #include <linux/mutex.h>
19 #include <linux/property.h>
20 #include <linux/uaccess.h>
21 #include <linux/slab.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/spinlock.h>
24 #include <linux/pm_runtime.h>
26 #include <linux/coresight.h>
27 #include <linux/amba/bus.h>
28 #include <linux/platform_device.h>
30 #include "coresight-priv.h"
31 #include "coresight-tmc.h"
33 DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
34 DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
35 DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
37 int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
39 struct coresight_device *csdev = drvdata->csdev;
40 struct csdev_access *csa = &csdev->access;
42 /* Ensure formatter, unformatter and hardware fifo are empty */
43 if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
45 "timeout while waiting for TMC to be Ready\n");
51 void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
53 struct coresight_device *csdev = drvdata->csdev;
54 struct csdev_access *csa = &csdev->access;
57 ffcr = readl_relaxed(drvdata->base + TMC_FFCR);
58 ffcr |= TMC_FFCR_STOP_ON_FLUSH;
59 writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
60 ffcr |= BIT(TMC_FFCR_FLUSHMAN_BIT);
61 writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
62 /* Ensure flush completes */
63 if (coresight_timeout(csa, TMC_FFCR, TMC_FFCR_FLUSHMAN_BIT, 0)) {
65 "timeout while waiting for completion of Manual Flush\n");
68 tmc_wait_for_tmcready(drvdata);
71 void tmc_enable_hw(struct tmc_drvdata *drvdata)
73 writel_relaxed(TMC_CTL_CAPT_EN, drvdata->base + TMC_CTL);
76 void tmc_disable_hw(struct tmc_drvdata *drvdata)
78 writel_relaxed(0x0, drvdata->base + TMC_CTL);
81 u32 tmc_get_memwidth_mask(struct tmc_drvdata *drvdata)
86 * When moving RRP or an offset address forward, the new values must
87 * be byte-address aligned to the width of the trace memory databus
88 * _and_ to a frame boundary (16 byte), whichever is the biggest. For
89 * example, for 32-bit, 64-bit and 128-bit wide trace memory, the four
90 * LSBs must be 0s. For 256-bit wide trace memory, the five LSBs must
93 switch (drvdata->memwidth) {
94 case TMC_MEM_INTF_WIDTH_32BITS:
95 case TMC_MEM_INTF_WIDTH_64BITS:
96 case TMC_MEM_INTF_WIDTH_128BITS:
97 mask = GENMASK(31, 4);
99 case TMC_MEM_INTF_WIDTH_256BITS:
100 mask = GENMASK(31, 5);
107 static int tmc_read_prepare(struct tmc_drvdata *drvdata)
111 switch (drvdata->config_type) {
112 case TMC_CONFIG_TYPE_ETB:
113 case TMC_CONFIG_TYPE_ETF:
114 ret = tmc_read_prepare_etb(drvdata);
116 case TMC_CONFIG_TYPE_ETR:
117 ret = tmc_read_prepare_etr(drvdata);
124 dev_dbg(&drvdata->csdev->dev, "TMC read start\n");
129 static int tmc_read_unprepare(struct tmc_drvdata *drvdata)
133 switch (drvdata->config_type) {
134 case TMC_CONFIG_TYPE_ETB:
135 case TMC_CONFIG_TYPE_ETF:
136 ret = tmc_read_unprepare_etb(drvdata);
138 case TMC_CONFIG_TYPE_ETR:
139 ret = tmc_read_unprepare_etr(drvdata);
146 dev_dbg(&drvdata->csdev->dev, "TMC read end\n");
151 static int tmc_open(struct inode *inode, struct file *file)
154 struct tmc_drvdata *drvdata = container_of(file->private_data,
155 struct tmc_drvdata, miscdev);
157 ret = tmc_read_prepare(drvdata);
161 nonseekable_open(inode, file);
163 dev_dbg(&drvdata->csdev->dev, "%s: successfully opened\n", __func__);
167 static inline ssize_t tmc_get_sysfs_trace(struct tmc_drvdata *drvdata,
168 loff_t pos, size_t len, char **bufpp)
170 switch (drvdata->config_type) {
171 case TMC_CONFIG_TYPE_ETB:
172 case TMC_CONFIG_TYPE_ETF:
173 return tmc_etb_get_sysfs_trace(drvdata, pos, len, bufpp);
174 case TMC_CONFIG_TYPE_ETR:
175 return tmc_etr_get_sysfs_trace(drvdata, pos, len, bufpp);
181 static ssize_t tmc_read(struct file *file, char __user *data, size_t len,
186 struct tmc_drvdata *drvdata = container_of(file->private_data,
187 struct tmc_drvdata, miscdev);
188 actual = tmc_get_sysfs_trace(drvdata, *ppos, len, &bufp);
192 if (copy_to_user(data, bufp, actual)) {
193 dev_dbg(&drvdata->csdev->dev,
194 "%s: copy_to_user failed\n", __func__);
199 dev_dbg(&drvdata->csdev->dev, "%zu bytes copied\n", actual);
204 static int tmc_release(struct inode *inode, struct file *file)
207 struct tmc_drvdata *drvdata = container_of(file->private_data,
208 struct tmc_drvdata, miscdev);
210 ret = tmc_read_unprepare(drvdata);
214 dev_dbg(&drvdata->csdev->dev, "%s: released\n", __func__);
218 static const struct file_operations tmc_fops = {
219 .owner = THIS_MODULE,
222 .release = tmc_release,
226 static enum tmc_mem_intf_width tmc_get_memwidth(u32 devid)
228 enum tmc_mem_intf_width memwidth;
231 * Excerpt from the TRM:
233 * DEVID::MEMWIDTH[10:8]
234 * 0x2 Memory interface databus is 32 bits wide.
235 * 0x3 Memory interface databus is 64 bits wide.
236 * 0x4 Memory interface databus is 128 bits wide.
237 * 0x5 Memory interface databus is 256 bits wide.
239 switch (BMVAL(devid, 8, 10)) {
241 memwidth = TMC_MEM_INTF_WIDTH_32BITS;
244 memwidth = TMC_MEM_INTF_WIDTH_64BITS;
247 memwidth = TMC_MEM_INTF_WIDTH_128BITS;
250 memwidth = TMC_MEM_INTF_WIDTH_256BITS;
259 static struct attribute *coresight_tmc_mgmt_attrs[] = {
260 coresight_simple_reg32(rsz, TMC_RSZ),
261 coresight_simple_reg32(sts, TMC_STS),
262 coresight_simple_reg64(rrp, TMC_RRP, TMC_RRPHI),
263 coresight_simple_reg64(rwp, TMC_RWP, TMC_RWPHI),
264 coresight_simple_reg32(trg, TMC_TRG),
265 coresight_simple_reg32(ctl, TMC_CTL),
266 coresight_simple_reg32(ffsr, TMC_FFSR),
267 coresight_simple_reg32(ffcr, TMC_FFCR),
268 coresight_simple_reg32(mode, TMC_MODE),
269 coresight_simple_reg32(pscr, TMC_PSCR),
270 coresight_simple_reg32(devid, CORESIGHT_DEVID),
271 coresight_simple_reg64(dba, TMC_DBALO, TMC_DBAHI),
272 coresight_simple_reg32(axictl, TMC_AXICTL),
273 coresight_simple_reg32(authstatus, TMC_AUTHSTATUS),
277 static ssize_t trigger_cntr_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
280 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
281 unsigned long val = drvdata->trigger_cntr;
283 return sprintf(buf, "%#lx\n", val);
286 static ssize_t trigger_cntr_store(struct device *dev,
287 struct device_attribute *attr,
288 const char *buf, size_t size)
292 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
294 ret = kstrtoul(buf, 16, &val);
298 drvdata->trigger_cntr = val;
301 static DEVICE_ATTR_RW(trigger_cntr);
303 static ssize_t buffer_size_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
306 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
308 return sprintf(buf, "%#x\n", drvdata->size);
311 static ssize_t buffer_size_store(struct device *dev,
312 struct device_attribute *attr,
313 const char *buf, size_t size)
317 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
319 /* Only permitted for TMC-ETRs */
320 if (drvdata->config_type != TMC_CONFIG_TYPE_ETR)
323 ret = kstrtoul(buf, 0, &val);
326 /* The buffer size should be page aligned */
327 if (val & (PAGE_SIZE - 1))
333 static DEVICE_ATTR_RW(buffer_size);
335 static struct attribute *coresight_tmc_attrs[] = {
336 &dev_attr_trigger_cntr.attr,
337 &dev_attr_buffer_size.attr,
341 static const struct attribute_group coresight_tmc_group = {
342 .attrs = coresight_tmc_attrs,
345 static const struct attribute_group coresight_tmc_mgmt_group = {
346 .attrs = coresight_tmc_mgmt_attrs,
350 static const struct attribute_group *coresight_etf_groups[] = {
351 &coresight_tmc_group,
352 &coresight_tmc_mgmt_group,
356 static const struct attribute_group *coresight_etr_groups[] = {
357 &coresight_etr_group,
358 &coresight_tmc_group,
359 &coresight_tmc_mgmt_group,
363 static inline bool tmc_etr_can_use_sg(struct device *dev)
369 * Presence of the property 'arm,scatter-gather' is checked
370 * on the platform for the feature support, rather than its
373 if (is_of_node(dev->fwnode)) {
374 return fwnode_property_present(dev->fwnode, "arm,scatter-gather");
375 } else if (is_acpi_device_node(dev->fwnode)) {
377 * TMC_DEVID_NOSCAT test in tmc_etr_setup_caps(), has already ensured
378 * this property is only checked for Coresight SoC 400 TMC configured
381 ret = fwnode_property_read_u8(dev->fwnode, "arm-armhc97c-sg-enable", &val_u8);
385 if (fwnode_property_present(dev->fwnode, "arm,scatter-gather")) {
386 pr_warn_once("Deprecated ACPI property - arm,scatter-gather\n");
393 static inline bool tmc_etr_has_non_secure_access(struct tmc_drvdata *drvdata)
395 u32 auth = readl_relaxed(drvdata->base + TMC_AUTHSTATUS);
397 return (auth & TMC_AUTH_NSID_MASK) == 0x3;
400 static const struct amba_id tmc_ids[];
402 /* Detect and initialise the capabilities of a TMC ETR */
403 static int tmc_etr_setup_caps(struct device *parent, u32 devid,
404 struct csdev_access *access)
407 u32 tmc_pid, dma_mask = 0;
408 struct tmc_drvdata *drvdata = dev_get_drvdata(parent);
411 if (!tmc_etr_has_non_secure_access(drvdata))
414 tmc_pid = coresight_get_pid(access);
415 dev_caps = coresight_get_uci_data_from_amba(tmc_ids, tmc_pid);
417 /* Set the unadvertised capabilities */
418 tmc_etr_init_caps(drvdata, (u32)(unsigned long)dev_caps);
420 if (!(devid & TMC_DEVID_NOSCAT) && tmc_etr_can_use_sg(parent))
421 tmc_etr_set_cap(drvdata, TMC_ETR_SG);
423 /* Check if the AXI address width is available */
424 if (devid & TMC_DEVID_AXIAW_VALID)
425 dma_mask = ((devid >> TMC_DEVID_AXIAW_SHIFT) &
426 TMC_DEVID_AXIAW_MASK);
429 * Unless specified in the device configuration, ETR uses a 40-bit
430 * AXI master in place of the embedded SRAM of ETB/ETF.
438 dev_info(parent, "Detected dma mask %dbits\n", dma_mask);
444 rc = dma_set_mask_and_coherent(parent, DMA_BIT_MASK(dma_mask));
446 dev_err(parent, "Failed to setup DMA mask: %d\n", rc);
450 static u32 tmc_etr_get_default_buffer_size(struct device *dev)
454 if (fwnode_property_read_u32(dev->fwnode, "arm,buffer-size", &size))
459 static u32 tmc_etr_get_max_burst_size(struct device *dev)
463 if (fwnode_property_read_u32(dev->fwnode, "arm,max-burst-size",
465 return TMC_AXICTL_WR_BURST_16;
467 /* Only permissible values are 0 to 15 */
468 if (burst_size > 0xF)
469 burst_size = TMC_AXICTL_WR_BURST_16;
474 static int __tmc_probe(struct device *dev, struct resource *res)
479 struct coresight_platform_data *pdata = NULL;
480 struct tmc_drvdata *drvdata = dev_get_drvdata(dev);
481 struct coresight_desc desc = { 0 };
482 struct coresight_dev_list *dev_list = NULL;
486 /* Validity for the resource is already checked by the AMBA core */
487 base = devm_ioremap_resource(dev, res);
493 drvdata->base = base;
494 desc.access = CSDEV_ACCESS_IOMEM(base);
496 spin_lock_init(&drvdata->spinlock);
498 devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
499 drvdata->config_type = BMVAL(devid, 6, 7);
500 drvdata->memwidth = tmc_get_memwidth(devid);
501 /* This device is not associated with a session */
503 drvdata->etr_mode = ETR_MODE_AUTO;
505 if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
506 drvdata->size = tmc_etr_get_default_buffer_size(dev);
507 drvdata->max_burst_size = tmc_etr_get_max_burst_size(dev);
509 drvdata->size = readl_relaxed(drvdata->base + TMC_RSZ) * 4;
514 switch (drvdata->config_type) {
515 case TMC_CONFIG_TYPE_ETB:
516 desc.groups = coresight_etf_groups;
517 desc.type = CORESIGHT_DEV_TYPE_SINK;
518 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
519 desc.ops = &tmc_etb_cs_ops;
520 dev_list = &etb_devs;
522 case TMC_CONFIG_TYPE_ETR:
523 desc.groups = coresight_etr_groups;
524 desc.type = CORESIGHT_DEV_TYPE_SINK;
525 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_SYSMEM;
526 desc.ops = &tmc_etr_cs_ops;
527 ret = tmc_etr_setup_caps(dev, devid, &desc.access);
530 idr_init(&drvdata->idr);
531 mutex_init(&drvdata->idr_mutex);
532 dev_list = &etr_devs;
534 case TMC_CONFIG_TYPE_ETF:
535 desc.groups = coresight_etf_groups;
536 desc.type = CORESIGHT_DEV_TYPE_LINKSINK;
537 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
538 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_FIFO;
539 desc.ops = &tmc_etf_cs_ops;
540 dev_list = &etf_devs;
543 pr_err("%s: Unsupported TMC config\n", desc.name);
548 desc.name = coresight_alloc_device_name(dev_list, dev);
554 pdata = coresight_get_platform_data(dev);
556 ret = PTR_ERR(pdata);
559 dev->platform_data = pdata;
562 drvdata->csdev = coresight_register(&desc);
563 if (IS_ERR(drvdata->csdev)) {
564 ret = PTR_ERR(drvdata->csdev);
568 drvdata->miscdev.name = desc.name;
569 drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
570 drvdata->miscdev.fops = &tmc_fops;
571 ret = misc_register(&drvdata->miscdev);
573 coresight_unregister(drvdata->csdev);
578 static int tmc_probe(struct amba_device *adev, const struct amba_id *id)
580 struct tmc_drvdata *drvdata;
583 drvdata = devm_kzalloc(&adev->dev, sizeof(*drvdata), GFP_KERNEL);
587 amba_set_drvdata(adev, drvdata);
588 ret = __tmc_probe(&adev->dev, &adev->res);
590 pm_runtime_put(&adev->dev);
595 static void tmc_shutdown(struct amba_device *adev)
598 struct tmc_drvdata *drvdata = amba_get_drvdata(adev);
600 spin_lock_irqsave(&drvdata->spinlock, flags);
602 if (coresight_get_mode(drvdata->csdev) == CS_MODE_DISABLED)
605 if (drvdata->config_type == TMC_CONFIG_TYPE_ETR)
606 tmc_etr_disable_hw(drvdata);
609 * We do not care about coresight unregister here unlike remove
610 * callback which is required for making coresight modular since
611 * the system is going down after this.
614 spin_unlock_irqrestore(&drvdata->spinlock, flags);
617 static void __tmc_remove(struct device *dev)
619 struct tmc_drvdata *drvdata = dev_get_drvdata(dev);
622 * Since misc_open() holds a refcount on the f_ops, which is
623 * etb fops in this case, device is there until last file
624 * handler to this device is closed.
626 misc_deregister(&drvdata->miscdev);
627 coresight_unregister(drvdata->csdev);
630 static void tmc_remove(struct amba_device *adev)
632 __tmc_remove(&adev->dev);
635 static const struct amba_id tmc_ids[] = {
636 CS_AMBA_ID(0x000bb961),
637 /* Coresight SoC 600 TMC-ETR/ETS */
638 CS_AMBA_ID_DATA(0x000bb9e8, (unsigned long)CORESIGHT_SOC_600_ETR_CAPS),
639 /* Coresight SoC 600 TMC-ETB */
640 CS_AMBA_ID(0x000bb9e9),
641 /* Coresight SoC 600 TMC-ETF */
642 CS_AMBA_ID(0x000bb9ea),
646 MODULE_DEVICE_TABLE(amba, tmc_ids);
648 static struct amba_driver tmc_driver = {
650 .name = "coresight-tmc",
651 .suppress_bind_attrs = true,
654 .shutdown = tmc_shutdown,
655 .remove = tmc_remove,
659 static int tmc_platform_probe(struct platform_device *pdev)
661 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
662 struct tmc_drvdata *drvdata;
665 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
669 drvdata->pclk = coresight_get_enable_apb_pclk(&pdev->dev);
670 if (IS_ERR(drvdata->pclk))
673 dev_set_drvdata(&pdev->dev, drvdata);
674 pm_runtime_get_noresume(&pdev->dev);
675 pm_runtime_set_active(&pdev->dev);
676 pm_runtime_enable(&pdev->dev);
678 ret = __tmc_probe(&pdev->dev, res);
679 pm_runtime_put(&pdev->dev);
681 pm_runtime_disable(&pdev->dev);
686 static void tmc_platform_remove(struct platform_device *pdev)
688 struct tmc_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
690 if (WARN_ON(!drvdata))
693 __tmc_remove(&pdev->dev);
694 pm_runtime_disable(&pdev->dev);
695 if (!IS_ERR_OR_NULL(drvdata->pclk))
696 clk_put(drvdata->pclk);
700 static int tmc_runtime_suspend(struct device *dev)
702 struct tmc_drvdata *drvdata = dev_get_drvdata(dev);
704 if (drvdata && !IS_ERR_OR_NULL(drvdata->pclk))
705 clk_disable_unprepare(drvdata->pclk);
709 static int tmc_runtime_resume(struct device *dev)
711 struct tmc_drvdata *drvdata = dev_get_drvdata(dev);
713 if (drvdata && !IS_ERR_OR_NULL(drvdata->pclk))
714 clk_prepare_enable(drvdata->pclk);
719 static const struct dev_pm_ops tmc_dev_pm_ops = {
720 SET_RUNTIME_PM_OPS(tmc_runtime_suspend, tmc_runtime_resume, NULL)
724 static const struct acpi_device_id tmc_acpi_ids[] = {
725 {"ARMHC501", 0, 0, 0}, /* ARM CoreSight ETR */
726 {"ARMHC97C", 0, 0, 0}, /* ARM CoreSight SoC-400 TMC, SoC-600 ETF/ETB */
729 MODULE_DEVICE_TABLE(acpi, tmc_acpi_ids);
732 static struct platform_driver tmc_platform_driver = {
733 .probe = tmc_platform_probe,
734 .remove_new = tmc_platform_remove,
736 .name = "coresight-tmc-platform",
737 .acpi_match_table = ACPI_PTR(tmc_acpi_ids),
738 .suppress_bind_attrs = true,
739 .pm = &tmc_dev_pm_ops,
743 static int __init tmc_init(void)
745 return coresight_init_driver("tmc", &tmc_driver, &tmc_platform_driver);
748 static void __exit tmc_exit(void)
750 coresight_remove_driver(&tmc_driver, &tmc_platform_driver);
752 module_init(tmc_init);
753 module_exit(tmc_exit);
756 MODULE_DESCRIPTION("Arm CoreSight Trace Memory Controller driver");
757 MODULE_LICENSE("GPL v2");