forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-13 9d77db3c730780c8ef5ccd4b66403ff5675cfe4e
kernel/drivers/hwtracing/coresight/coresight-etb10.c
....@@ -63,15 +63,18 @@
6363 #define ETB_FFSR_BIT 1
6464 #define ETB_FRAME_SIZE_WORDS 4
6565
66
+DEFINE_CORESIGHT_DEVLIST(etb_devs, "etb");
67
+
6668 /**
6769 * struct etb_drvdata - specifics associated to an ETB component
6870 * @base: memory mapped base address for this component.
69
- * @dev: the device entity associated to this component.
7071 * @atclk: optional clock for the core parts of the ETB.
7172 * @csdev: component vitals needed by the framework.
7273 * @miscdev: specifics to handle "/dev/xyz.etb" entry.
7374 * @spinlock: only one at a time pls.
7475 * @reading: synchronise user space access to etb buffer.
76
+ * @pid: Process ID of the process being monitored by the session
77
+ * that is using this component.
7578 * @buf: area of memory where ETB buffer content gets sent.
7679 * @mode: this ETB is being used.
7780 * @buffer_depth: size of @buf.
....@@ -79,12 +82,12 @@
7982 */
8083 struct etb_drvdata {
8184 void __iomem *base;
82
- struct device *dev;
8385 struct clk *atclk;
8486 struct coresight_device *csdev;
8587 struct miscdevice miscdev;
8688 spinlock_t spinlock;
8789 local_t reading;
90
+ pid_t pid;
8891 u8 *buf;
8992 u32 mode;
9093 u32 buffer_depth;
....@@ -94,17 +97,9 @@
9497 static int etb_set_buffer(struct coresight_device *csdev,
9598 struct perf_output_handle *handle);
9699
97
-static unsigned int etb_get_buffer_depth(struct etb_drvdata *drvdata)
100
+static inline unsigned int etb_get_buffer_depth(struct etb_drvdata *drvdata)
98101 {
99
- u32 depth = 0;
100
-
101
- pm_runtime_get_sync(drvdata->dev);
102
-
103
- /* RO registers don't need locking */
104
- depth = readl_relaxed(drvdata->base + ETB_RAM_DEPTH_REG);
105
-
106
- pm_runtime_put(drvdata->dev);
107
- return depth;
102
+ return readl_relaxed(drvdata->base + ETB_RAM_DEPTH_REG);
108103 }
109104
110105 static void __etb_enable_hw(struct etb_drvdata *drvdata)
....@@ -137,7 +132,7 @@
137132
138133 static int etb_enable_hw(struct etb_drvdata *drvdata)
139134 {
140
- int rc = coresight_claim_device(drvdata->base);
135
+ int rc = coresight_claim_device(drvdata->csdev);
141136
142137 if (rc)
143138 return rc;
....@@ -177,14 +172,34 @@
177172 static int etb_enable_perf(struct coresight_device *csdev, void *data)
178173 {
179174 int ret = 0;
175
+ pid_t pid;
180176 unsigned long flags;
181177 struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
178
+ struct perf_output_handle *handle = data;
179
+ struct cs_buffers *buf = etm_perf_sink_config(handle);
182180
183181 spin_lock_irqsave(&drvdata->spinlock, flags);
184182
185
- /* No need to continue if the component is already in use. */
186
- if (drvdata->mode != CS_MODE_DISABLED) {
183
+ /* No need to continue if the component is already in used by sysFS. */
184
+ if (drvdata->mode == CS_MODE_SYSFS) {
187185 ret = -EBUSY;
186
+ goto out;
187
+ }
188
+
189
+ /* Get a handle on the pid of the process to monitor */
190
+ pid = buf->pid;
191
+
192
+ if (drvdata->pid != -1 && drvdata->pid != pid) {
193
+ ret = -EBUSY;
194
+ goto out;
195
+ }
196
+
197
+ /*
198
+ * No HW configuration is needed if the sink is already in
199
+ * use for this session.
200
+ */
201
+ if (drvdata->pid == pid) {
202
+ atomic_inc(csdev->refcnt);
188203 goto out;
189204 }
190205
....@@ -193,12 +208,14 @@
193208 * the perf buffer. So we can perform the step before we turn the
194209 * ETB on and leave without cleaning up.
195210 */
196
- ret = etb_set_buffer(csdev, (struct perf_output_handle *)data);
211
+ ret = etb_set_buffer(csdev, handle);
197212 if (ret)
198213 goto out;
199214
200215 ret = etb_enable_hw(drvdata);
201216 if (!ret) {
217
+ /* Associate with monitored process. */
218
+ drvdata->pid = pid;
202219 drvdata->mode = CS_MODE_PERF;
203220 atomic_inc(csdev->refcnt);
204221 }
....@@ -211,7 +228,6 @@
211228 static int etb_enable(struct coresight_device *csdev, u32 mode, void *data)
212229 {
213230 int ret;
214
- struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
215231
216232 switch (mode) {
217233 case CS_MODE_SYSFS:
....@@ -228,13 +244,15 @@
228244 if (ret)
229245 return ret;
230246
231
- dev_dbg(drvdata->dev, "ETB enabled\n");
247
+ dev_dbg(&csdev->dev, "ETB enabled\n");
232248 return 0;
233249 }
234250
235251 static void __etb_disable_hw(struct etb_drvdata *drvdata)
236252 {
237253 u32 ffcr;
254
+ struct device *dev = &drvdata->csdev->dev;
255
+ struct csdev_access *csa = &drvdata->csdev->access;
238256
239257 CS_UNLOCK(drvdata->base);
240258
....@@ -246,16 +264,16 @@
246264 ffcr |= ETB_FFCR_FON_MAN;
247265 writel_relaxed(ffcr, drvdata->base + ETB_FFCR);
248266
249
- if (coresight_timeout(drvdata->base, ETB_FFCR, ETB_FFCR_BIT, 0)) {
250
- dev_err(drvdata->dev,
267
+ if (coresight_timeout(csa, ETB_FFCR, ETB_FFCR_BIT, 0)) {
268
+ dev_err(dev,
251269 "timeout while waiting for completion of Manual Flush\n");
252270 }
253271
254272 /* disable trace capture */
255273 writel_relaxed(0x0, drvdata->base + ETB_CTL_REG);
256274
257
- if (coresight_timeout(drvdata->base, ETB_FFSR, ETB_FFSR_BIT, 1)) {
258
- dev_err(drvdata->dev,
275
+ if (coresight_timeout(csa, ETB_FFSR, ETB_FFSR_BIT, 1)) {
276
+ dev_err(dev,
259277 "timeout while waiting for Formatter to Stop\n");
260278 }
261279
....@@ -270,6 +288,7 @@
270288 u32 read_data, depth;
271289 u32 read_ptr, write_ptr;
272290 u32 frame_off, frame_endoff;
291
+ struct device *dev = &drvdata->csdev->dev;
273292
274293 CS_UNLOCK(drvdata->base);
275294
....@@ -279,10 +298,10 @@
279298 frame_off = write_ptr % ETB_FRAME_SIZE_WORDS;
280299 frame_endoff = ETB_FRAME_SIZE_WORDS - frame_off;
281300 if (frame_off) {
282
- dev_err(drvdata->dev,
301
+ dev_err(dev,
283302 "write_ptr: %lu not aligned to formatter frame size\n",
284303 (unsigned long)write_ptr);
285
- dev_err(drvdata->dev, "frameoff: %lu, frame_endoff: %lu\n",
304
+ dev_err(dev, "frameoff: %lu, frame_endoff: %lu\n",
286305 (unsigned long)frame_off, (unsigned long)frame_endoff);
287306 write_ptr += frame_endoff;
288307 }
....@@ -326,7 +345,7 @@
326345 {
327346 __etb_disable_hw(drvdata);
328347 etb_dump_hw(drvdata);
329
- coresight_disclaim_device(drvdata->base);
348
+ coresight_disclaim_device(drvdata->csdev);
330349 }
331350
332351 static int etb_disable(struct coresight_device *csdev)
....@@ -344,10 +363,12 @@
344363 /* Complain if we (somehow) got out of sync */
345364 WARN_ON_ONCE(drvdata->mode == CS_MODE_DISABLED);
346365 etb_disable_hw(drvdata);
366
+ /* Dissociate from monitored process. */
367
+ drvdata->pid = -1;
347368 drvdata->mode = CS_MODE_DISABLED;
348369 spin_unlock_irqrestore(&drvdata->spinlock, flags);
349370
350
- dev_dbg(drvdata->dev, "ETB disabled\n");
371
+ dev_dbg(&csdev->dev, "ETB disabled\n");
351372 return 0;
352373 }
353374
....@@ -355,15 +376,16 @@
355376 struct perf_event *event, void **pages,
356377 int nr_pages, bool overwrite)
357378 {
358
- int node, cpu = event->cpu;
379
+ int node;
359380 struct cs_buffers *buf;
360381
361
- node = (cpu == -1) ? NUMA_NO_NODE : cpu_to_node(cpu);
382
+ node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
362383
363384 buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
364385 if (!buf)
365386 return NULL;
366387
388
+ buf->pid = task_pid_nr(event->owner);
367389 buf->snapshot = overwrite;
368390 buf->nr_pages = nr_pages;
369391 buf->data_pages = pages;
....@@ -412,7 +434,7 @@
412434 const u32 *barrier;
413435 u32 read_ptr, write_ptr, capacity;
414436 u32 status, read_data;
415
- unsigned long offset, to_read, flags;
437
+ unsigned long offset, to_read = 0, flags;
416438 struct cs_buffers *buf = sink_config;
417439 struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
418440
....@@ -422,6 +444,11 @@
422444 capacity = drvdata->buffer_depth * ETB_FRAME_SIZE_WORDS;
423445
424446 spin_lock_irqsave(&drvdata->spinlock, flags);
447
+
448
+ /* Don't do anything if another tracer is using this sink */
449
+ if (atomic_read(csdev->refcnt) != 1)
450
+ goto out;
451
+
425452 __etb_disable_hw(drvdata);
426453 CS_UNLOCK(drvdata->base);
427454
....@@ -435,7 +462,7 @@
435462 * chance to fix things.
436463 */
437464 if (write_ptr % ETB_FRAME_SIZE_WORDS) {
438
- dev_err(drvdata->dev,
465
+ dev_err(&csdev->dev,
439466 "write_ptr: %lu not aligned to formatter frame size\n",
440467 (unsigned long)write_ptr);
441468
....@@ -501,7 +528,7 @@
501528
502529 cur = buf->cur;
503530 offset = buf->offset;
504
- barrier = barrier_pkt;
531
+ barrier = coresight_barrier_pkt;
505532
506533 for (i = 0; i < to_read; i += 4) {
507534 buf_ptr = buf->data_pages[cur] + offset;
....@@ -529,15 +556,17 @@
529556 writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
530557
531558 /*
532
- * In snapshot mode we have to update the handle->head to point
533
- * to the new location.
559
+ * In snapshot mode we simply increment the head by the number of byte
560
+ * that were written. User space function cs_etm_find_snapshot() will
561
+ * figure out how many bytes to get from the AUX buffer based on the
562
+ * position of the head.
534563 */
535
- if (buf->snapshot) {
536
- handle->head = (cur * PAGE_SIZE) + offset;
537
- to_read = buf->nr_pages << PAGE_SHIFT;
538
- }
564
+ if (buf->snapshot)
565
+ handle->head += to_read;
566
+
539567 __etb_enable_hw(drvdata);
540568 CS_LOCK(drvdata->base);
569
+out:
541570 spin_unlock_irqrestore(&drvdata->spinlock, flags);
542571
543572 return to_read;
....@@ -567,7 +596,7 @@
567596 }
568597 spin_unlock_irqrestore(&drvdata->spinlock, flags);
569598
570
- dev_dbg(drvdata->dev, "ETB dumped\n");
599
+ dev_dbg(&drvdata->csdev->dev, "ETB dumped\n");
571600 }
572601
573602 static int etb_open(struct inode *inode, struct file *file)
....@@ -578,7 +607,7 @@
578607 if (local_cmpxchg(&drvdata->reading, 0, 1))
579608 return -EBUSY;
580609
581
- dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__);
610
+ dev_dbg(&drvdata->csdev->dev, "%s: successfully opened\n", __func__);
582611 return 0;
583612 }
584613
....@@ -588,6 +617,7 @@
588617 u32 depth;
589618 struct etb_drvdata *drvdata = container_of(file->private_data,
590619 struct etb_drvdata, miscdev);
620
+ struct device *dev = &drvdata->csdev->dev;
591621
592622 etb_dump(drvdata);
593623
....@@ -596,13 +626,14 @@
596626 len = depth * 4 - *ppos;
597627
598628 if (copy_to_user(data, drvdata->buf + *ppos, len)) {
599
- dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__);
629
+ dev_dbg(dev,
630
+ "%s: copy_to_user failed\n", __func__);
600631 return -EFAULT;
601632 }
602633
603634 *ppos += len;
604635
605
- dev_dbg(drvdata->dev, "%s: %zu bytes copied, %d bytes left\n",
636
+ dev_dbg(dev, "%s: %zu bytes copied, %d bytes left\n",
606637 __func__, len, (int)(depth * 4 - *ppos));
607638 return len;
608639 }
....@@ -613,7 +644,7 @@
613644 struct etb_drvdata, miscdev);
614645 local_set(&drvdata->reading, 0);
615646
616
- dev_dbg(drvdata->dev, "%s: released\n", __func__);
647
+ dev_dbg(&drvdata->csdev->dev, "%s: released\n", __func__);
617648 return 0;
618649 }
619650
....@@ -689,7 +720,7 @@
689720 .name = "mgmt",
690721 };
691722
692
-const struct attribute_group *coresight_etb_groups[] = {
723
+static const struct attribute_group *coresight_etb_groups[] = {
693724 &coresight_etb_group,
694725 &coresight_etb_mgmt_group,
695726 NULL,
....@@ -704,20 +735,15 @@
704735 struct etb_drvdata *drvdata;
705736 struct resource *res = &adev->res;
706737 struct coresight_desc desc = { 0 };
707
- struct device_node *np = adev->dev.of_node;
708738
709
- if (np) {
710
- pdata = of_get_coresight_platform_data(dev, np);
711
- if (IS_ERR(pdata))
712
- return PTR_ERR(pdata);
713
- adev->dev.platform_data = pdata;
714
- }
739
+ desc.name = coresight_alloc_device_name(&etb_devs, dev);
740
+ if (!desc.name)
741
+ return -ENOMEM;
715742
716743 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
717744 if (!drvdata)
718745 return -ENOMEM;
719746
720
- drvdata->dev = &adev->dev;
721747 drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
722748 if (!IS_ERR(drvdata->atclk)) {
723749 ret = clk_prepare_enable(drvdata->atclk);
....@@ -732,11 +758,11 @@
732758 return PTR_ERR(base);
733759
734760 drvdata->base = base;
761
+ desc.access = CSDEV_ACCESS_IOMEM(base);
735762
736763 spin_lock_init(&drvdata->spinlock);
737764
738765 drvdata->buffer_depth = etb_get_buffer_depth(drvdata);
739
- pm_runtime_put(&adev->dev);
740766
741767 if (drvdata->buffer_depth & 0x80000000)
742768 return -EINVAL;
....@@ -745,6 +771,14 @@
745771 drvdata->buffer_depth, 4, GFP_KERNEL);
746772 if (!drvdata->buf)
747773 return -ENOMEM;
774
+
775
+ /* This device is not associated with a session */
776
+ drvdata->pid = -1;
777
+
778
+ pdata = coresight_get_platform_data(dev);
779
+ if (IS_ERR(pdata))
780
+ return PTR_ERR(pdata);
781
+ adev->dev.platform_data = pdata;
748782
749783 desc.type = CORESIGHT_DEV_TYPE_SINK;
750784 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
....@@ -756,18 +790,32 @@
756790 if (IS_ERR(drvdata->csdev))
757791 return PTR_ERR(drvdata->csdev);
758792
759
- drvdata->miscdev.name = pdata->name;
793
+ drvdata->miscdev.name = desc.name;
760794 drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
761795 drvdata->miscdev.fops = &etb_fops;
762796 ret = misc_register(&drvdata->miscdev);
763797 if (ret)
764798 goto err_misc_register;
765799
800
+ pm_runtime_put(&adev->dev);
766801 return 0;
767802
768803 err_misc_register:
769804 coresight_unregister(drvdata->csdev);
770805 return ret;
806
+}
807
+
808
+static void etb_remove(struct amba_device *adev)
809
+{
810
+ struct etb_drvdata *drvdata = dev_get_drvdata(&adev->dev);
811
+
812
+ /*
813
+ * Since misc_open() holds a refcount on the f_ops, which is
814
+ * etb fops in this case, device is there until last file
815
+ * handler to this device is closed.
816
+ */
817
+ misc_deregister(&drvdata->miscdev);
818
+ coresight_unregister(drvdata->csdev);
771819 }
772820
773821 #ifdef CONFIG_PM
....@@ -804,6 +852,8 @@
804852 { 0, 0},
805853 };
806854
855
+MODULE_DEVICE_TABLE(amba, etb_ids);
856
+
807857 static struct amba_driver etb_driver = {
808858 .drv = {
809859 .name = "coresight-etb10",
....@@ -813,6 +863,13 @@
813863
814864 },
815865 .probe = etb_probe,
866
+ .remove = etb_remove,
816867 .id_table = etb_ids,
817868 };
818
-builtin_amba_driver(etb_driver);
869
+
870
+module_amba_driver(etb_driver);
871
+
872
+MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
873
+MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
874
+MODULE_DESCRIPTION("Arm CoreSight Embedded Trace Buffer driver");
875
+MODULE_LICENSE("GPL v2");