1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2021-2022 NVIDIA Corporation
5 * Author: Dipen Patel <dipenp@nvidia.com>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/err.h>
11 #include <linux/slab.h>
13 #include <linux/of_device.h>
14 #include <linux/mutex.h>
15 #include <linux/uaccess.h>
16 #include <linux/hte.h>
17 #include <linux/delay.h>
18 #include <linux/debugfs.h>
20 #define HTE_TS_NAME_LEN 10
22 /* Global list of the HTE devices */
23 static DEFINE_SPINLOCK(hte_lock);
24 static LIST_HEAD(hte_devices);
34 * struct hte_ts_info - Information related to requested timestamp.
36 * @xlated_id: Timestamp ID as understood between HTE subsys and HTE provider,
37 * See xlate callback API.
38 * @flags: Flags holding state information.
39 * @hte_cb_flags: Callback related flags.
40 * @seq: Timestamp sequence counter.
41 * @line_name: HTE allocated line name.
42 * @free_attr_name: If set, free the attr name.
43 * @cb: A nonsleeping callback function provided by clients.
44 * @tcb: A secondary sleeping callback function provided by clients.
45 * @dropped_ts: Dropped timestamps.
46 * @slock: Spin lock to synchronize between disable/enable,
47 * request/release APIs.
48 * @cb_work: callback workqueue, used when tcb is specified.
49 * @req_mlock: Lock during timestamp request/release APIs.
50 * @ts_dbg_root: Root for the debug fs.
51 * @gdev: HTE abstract device that this timestamp information belongs to.
52 * @cl_data: Client specific data.
57 unsigned long hte_cb_flags;
65 struct work_struct cb_work;
66 struct mutex req_mlock;
67 struct dentry *ts_dbg_root;
68 struct hte_device *gdev;
73 * struct hte_device - HTE abstract device
74 * @nlines: Number of entities this device supports.
75 * @ts_req: Total number of entities requested.
76 * @sdev: Device used at various debug prints.
77 * @dbg_root: Root directory for debug fs.
78 * @list: List node to store hte_device for each provider.
79 * @chip: HTE chip providing this HTE device.
80 * @owner: helps prevent removal of modules when in use.
81 * @ei: Timestamp information.
87 struct dentry *dbg_root;
88 struct list_head list;
89 struct hte_chip *chip;
91 struct hte_ts_info ei[];
94 #ifdef CONFIG_DEBUG_FS
96 static struct dentry *hte_root;
98 static int __init hte_subsys_dbgfs_init(void)
100 /* creates /sys/kernel/debug/hte/ */
101 hte_root = debugfs_create_dir("hte", NULL);
105 subsys_initcall(hte_subsys_dbgfs_init);
107 static void hte_chip_dbgfs_init(struct hte_device *gdev)
109 const struct hte_chip *chip = gdev->chip;
110 const char *name = chip->name ? chip->name : dev_name(chip->dev);
112 gdev->dbg_root = debugfs_create_dir(name, hte_root);
114 debugfs_create_atomic_t("ts_requested", 0444, gdev->dbg_root,
116 debugfs_create_u32("total_ts", 0444, gdev->dbg_root,
120 static void hte_ts_dbgfs_init(const char *name, struct hte_ts_info *ei)
122 if (!ei->gdev->dbg_root || !name)
125 ei->ts_dbg_root = debugfs_create_dir(name, ei->gdev->dbg_root);
127 debugfs_create_atomic_t("dropped_timestamps", 0444, ei->ts_dbg_root,
133 static void hte_chip_dbgfs_init(struct hte_device *gdev)
137 static void hte_ts_dbgfs_init(const char *name, struct hte_ts_info *ei)
144 * hte_ts_put() - Release and disable timestamp for the given desc.
146 * @desc: timestamp descriptor.
148 * Context: debugfs_remove_recursive() function call may use sleeping locks,
149 * not suitable from atomic context.
150 * Returns: 0 on success or a negative error code on failure.
152 int hte_ts_put(struct hte_ts_desc *desc)
156 struct hte_device *gdev;
157 struct hte_ts_info *ei;
164 if (!ei || !ei->gdev)
169 mutex_lock(&ei->req_mlock);
171 if (unlikely(!test_bit(HTE_TS_REQ, &ei->flags) &&
172 !test_bit(HTE_TS_REGISTERED, &ei->flags))) {
173 dev_info(gdev->sdev, "id:%d is not requested\n",
179 if (unlikely(!test_bit(HTE_TS_REQ, &ei->flags) &&
180 test_bit(HTE_TS_REGISTERED, &ei->flags))) {
181 dev_info(gdev->sdev, "id:%d is registered but not requested\n",
187 if (test_bit(HTE_TS_REQ, &ei->flags) &&
188 !test_bit(HTE_TS_REGISTERED, &ei->flags)) {
189 clear_bit(HTE_TS_REQ, &ei->flags);
190 desc->hte_data = NULL;
195 ret = gdev->chip->ops->release(gdev->chip, desc, ei->xlated_id);
197 dev_err(gdev->sdev, "id: %d free failed\n",
202 kfree(ei->line_name);
203 if (ei->free_attr_name)
204 kfree_const(desc->attr.name);
206 debugfs_remove_recursive(ei->ts_dbg_root);
208 spin_lock_irqsave(&ei->slock, flag);
210 if (test_bit(HTE_TS_QUEUE_WK, &ei->flags)) {
211 spin_unlock_irqrestore(&ei->slock, flag);
212 flush_work(&ei->cb_work);
213 spin_lock_irqsave(&ei->slock, flag);
216 atomic_dec(&gdev->ts_req);
217 atomic_set(&ei->dropped_ts, 0);
221 desc->hte_data = NULL;
223 spin_unlock_irqrestore(&ei->slock, flag);
230 module_put(gdev->owner);
232 mutex_unlock(&ei->req_mlock);
233 dev_dbg(gdev->sdev, "release id: %d\n", desc->attr.line_id);
237 EXPORT_SYMBOL_GPL(hte_ts_put);
239 static int hte_ts_dis_en_common(struct hte_ts_desc *desc, bool en)
242 struct hte_device *gdev;
243 struct hte_ts_info *ei;
252 if (!ei || !ei->gdev)
256 ts_id = desc->attr.line_id;
258 mutex_lock(&ei->req_mlock);
260 if (!test_bit(HTE_TS_REGISTERED, &ei->flags)) {
261 dev_dbg(gdev->sdev, "id:%d is not registered", ts_id);
266 spin_lock_irqsave(&ei->slock, flag);
269 if (!test_bit(HTE_TS_DISABLE, &ei->flags)) {
274 spin_unlock_irqrestore(&ei->slock, flag);
275 ret = gdev->chip->ops->enable(gdev->chip, ei->xlated_id);
277 dev_warn(gdev->sdev, "id: %d enable failed\n",
282 spin_lock_irqsave(&ei->slock, flag);
283 clear_bit(HTE_TS_DISABLE, &ei->flags);
285 if (test_bit(HTE_TS_DISABLE, &ei->flags)) {
290 spin_unlock_irqrestore(&ei->slock, flag);
291 ret = gdev->chip->ops->disable(gdev->chip, ei->xlated_id);
293 dev_warn(gdev->sdev, "id: %d disable failed\n",
298 spin_lock_irqsave(&ei->slock, flag);
299 set_bit(HTE_TS_DISABLE, &ei->flags);
303 spin_unlock_irqrestore(&ei->slock, flag);
305 mutex_unlock(&ei->req_mlock);
310 * hte_disable_ts() - Disable timestamp on given descriptor.
312 * The API does not release any resources associated with desc.
314 * @desc: ts descriptor, this is the same as returned by the request API.
316 * Context: Holds mutex lock, not suitable from atomic context.
317 * Returns: 0 on success or a negative error code on failure.
319 int hte_disable_ts(struct hte_ts_desc *desc)
321 return hte_ts_dis_en_common(desc, false);
323 EXPORT_SYMBOL_GPL(hte_disable_ts);
326 * hte_enable_ts() - Enable timestamp on given descriptor.
328 * @desc: ts descriptor, this is the same as returned by the request API.
330 * Context: Holds mutex lock, not suitable from atomic context.
331 * Returns: 0 on success or a negative error code on failure.
333 int hte_enable_ts(struct hte_ts_desc *desc)
335 return hte_ts_dis_en_common(desc, true);
337 EXPORT_SYMBOL_GPL(hte_enable_ts);
339 static void hte_do_cb_work(struct work_struct *w)
342 struct hte_ts_info *ei = container_of(w, struct hte_ts_info, cb_work);
344 if (unlikely(!ei->tcb))
347 ei->tcb(ei->cl_data);
349 spin_lock_irqsave(&ei->slock, flag);
350 clear_bit(HTE_TS_QUEUE_WK, &ei->flags);
351 spin_unlock_irqrestore(&ei->slock, flag);
354 static int __hte_req_ts(struct hte_ts_desc *desc, hte_ts_cb_t cb,
355 hte_ts_sec_cb_t tcb, void *data)
358 struct hte_device *gdev;
359 struct hte_ts_info *ei = desc->hte_data;
363 * There is a chance that multiple consumers requesting same entity,
366 mutex_lock(&ei->req_mlock);
368 if (test_bit(HTE_TS_REGISTERED, &ei->flags) ||
369 !test_bit(HTE_TS_REQ, &ei->flags)) {
370 dev_dbg(gdev->chip->dev, "id:%u req failed\n",
379 INIT_WORK(&ei->cb_work, hte_do_cb_work);
381 ret = gdev->chip->ops->request(gdev->chip, desc, ei->xlated_id);
383 dev_err(gdev->chip->dev, "ts request failed\n");
390 atomic_inc(&gdev->ts_req);
392 ei->line_name = NULL;
393 if (!desc->attr.name) {
394 ei->line_name = kzalloc(HTE_TS_NAME_LEN, GFP_KERNEL);
396 scnprintf(ei->line_name, HTE_TS_NAME_LEN, "ts_%u",
400 hte_ts_dbgfs_init(desc->attr.name == NULL ?
401 ei->line_name : desc->attr.name, ei);
402 set_bit(HTE_TS_REGISTERED, &ei->flags);
404 dev_dbg(gdev->chip->dev, "id: %u, xlated id:%u",
405 desc->attr.line_id, ei->xlated_id);
410 mutex_unlock(&ei->req_mlock);
415 static int hte_bind_ts_info_locked(struct hte_ts_info *ei,
416 struct hte_ts_desc *desc, u32 x_id)
420 mutex_lock(&ei->req_mlock);
422 if (test_bit(HTE_TS_REQ, &ei->flags)) {
423 dev_dbg(ei->gdev->chip->dev, "id:%u is already requested\n",
429 set_bit(HTE_TS_REQ, &ei->flags);
431 ei->xlated_id = x_id;
434 mutex_unlock(&ei->req_mlock);
439 static struct hte_device *of_node_to_htedevice(struct device_node *np)
441 struct hte_device *gdev;
443 spin_lock(&hte_lock);
445 list_for_each_entry(gdev, &hte_devices, list)
446 if (gdev->chip && gdev->chip->dev &&
447 gdev->chip->dev->of_node == np) {
448 spin_unlock(&hte_lock);
452 spin_unlock(&hte_lock);
454 return ERR_PTR(-ENODEV);
457 static struct hte_device *hte_find_dev_from_linedata(struct hte_ts_desc *desc)
459 struct hte_device *gdev;
461 spin_lock(&hte_lock);
463 list_for_each_entry(gdev, &hte_devices, list)
464 if (gdev->chip && gdev->chip->match_from_linedata) {
465 if (!gdev->chip->match_from_linedata(gdev->chip, desc))
467 spin_unlock(&hte_lock);
471 spin_unlock(&hte_lock);
473 return ERR_PTR(-ENODEV);
477 * of_hte_req_count - Return the number of entities to timestamp.
479 * The function returns the total count of the requested entities to timestamp
480 * by parsing device tree.
482 * @dev: The HTE consumer.
484 * Returns: Positive number on success, -ENOENT if no entries,
485 * -EINVAL for other errors.
487 int of_hte_req_count(struct device *dev)
491 if (!dev || !dev->of_node)
494 count = of_count_phandle_with_args(dev->of_node, "timestamps",
497 return count ? count : -ENOENT;
499 EXPORT_SYMBOL_GPL(of_hte_req_count);
501 static inline struct hte_device *hte_get_dev(struct hte_ts_desc *desc)
503 return hte_find_dev_from_linedata(desc);
506 static struct hte_device *hte_of_get_dev(struct device *dev,
507 struct hte_ts_desc *desc,
509 struct of_phandle_args *args,
513 struct device_node *np;
517 return ERR_PTR(-EINVAL);
521 if (!of_find_property(np, "timestamp-names", NULL)) {
522 /* Let hte core construct it during request time */
523 desc->attr.name = NULL;
525 ret = of_property_read_string_index(np, "timestamp-names",
526 index, &desc->attr.name);
528 pr_err("can't parse \"timestamp-names\" property\n");
532 if (desc->attr.name) {
533 temp = skip_spaces(desc->attr.name);
535 desc->attr.name = NULL;
539 ret = of_parse_phandle_with_args(np, "timestamps", "#timestamp-cells",
542 pr_err("%s(): can't parse \"timestamps\" property\n",
547 of_node_put(args->np);
549 return of_node_to_htedevice(args->np);
553 * hte_ts_get() - The function to initialize and obtain HTE desc.
555 * The function initializes the consumer provided HTE descriptor. If consumer
556 * has device tree node, index is used to parse the line id and other details.
557 * The function needs to be called before using any request APIs.
559 * @dev: HTE consumer/client device, used in case of parsing device tree node.
560 * @desc: Pre-allocated timestamp descriptor.
561 * @index: The index will be used as an index to parse line_id from the
562 * device tree node if node is present.
564 * Context: Holds mutex lock.
565 * Returns: Returns 0 on success or negative error code on failure.
567 int hte_ts_get(struct device *dev, struct hte_ts_desc *desc, int index)
569 struct hte_device *gdev;
570 struct hte_ts_info *ei;
571 const struct fwnode_handle *fwnode;
572 struct of_phandle_args args;
575 bool free_name = false;
580 fwnode = dev ? dev_fwnode(dev) : NULL;
582 if (is_of_node(fwnode))
583 gdev = hte_of_get_dev(dev, desc, index, &args, &free_name);
585 gdev = hte_get_dev(desc);
588 pr_err("%s() no hte dev found\n", __func__);
589 return PTR_ERR(gdev);
592 if (!try_module_get(gdev->owner))
596 pr_err("%s(): requested id does not have provider\n",
602 if (is_of_node(fwnode)) {
603 if (!gdev->chip->xlate_of)
606 ret = gdev->chip->xlate_of(gdev->chip, &args,
609 if (!gdev->chip->xlate_plat)
612 ret = gdev->chip->xlate_plat(gdev->chip, desc,
619 ei = &gdev->ei[xlated_id];
621 ret = hte_bind_ts_info_locked(ei, desc, xlated_id);
625 ei->free_attr_name = free_name;
630 module_put(gdev->owner);
633 EXPORT_SYMBOL_GPL(hte_ts_get);
635 static void __devm_hte_release_ts(void *res)
641 * hte_request_ts_ns() - The API to request and enable hardware timestamp in
644 * The entity is provider specific for example, GPIO lines, signals, buses
645 * etc...The API allocates necessary resources and enables the timestamp.
647 * @desc: Pre-allocated and initialized timestamp descriptor.
648 * @cb: Callback to push the timestamp data to consumer.
649 * @tcb: Optional callback. If its provided, subsystem initializes
650 * workqueue. It is called when cb returns HTE_RUN_SECOND_CB.
651 * @data: Client data, used during cb and tcb callbacks.
653 * Context: Holds mutex lock.
654 * Returns: Returns 0 on success or negative error code on failure.
656 int hte_request_ts_ns(struct hte_ts_desc *desc, hte_ts_cb_t cb,
657 hte_ts_sec_cb_t tcb, void *data)
660 struct hte_ts_info *ei;
662 if (!desc || !desc->hte_data || !cb)
666 if (!ei || !ei->gdev)
669 ret = __hte_req_ts(desc, cb, tcb, data);
671 dev_err(ei->gdev->chip->dev,
672 "failed to request id: %d\n", desc->attr.line_id);
678 EXPORT_SYMBOL_GPL(hte_request_ts_ns);
681 * devm_hte_request_ts_ns() - Resource managed API to request and enable
682 * hardware timestamp in nanoseconds.
684 * The entity is provider specific for example, GPIO lines, signals, buses
685 * etc...The API allocates necessary resources and enables the timestamp. It
686 * deallocates and disables automatically when the consumer exits.
688 * @dev: HTE consumer/client device.
689 * @desc: Pre-allocated and initialized timestamp descriptor.
690 * @cb: Callback to push the timestamp data to consumer.
691 * @tcb: Optional callback. If its provided, subsystem initializes
692 * workqueue. It is called when cb returns HTE_RUN_SECOND_CB.
693 * @data: Client data, used during cb and tcb callbacks.
695 * Context: Holds mutex lock.
696 * Returns: Returns 0 on success or negative error code on failure.
698 int devm_hte_request_ts_ns(struct device *dev, struct hte_ts_desc *desc,
699 hte_ts_cb_t cb, hte_ts_sec_cb_t tcb,
707 err = hte_request_ts_ns(desc, cb, tcb, data);
711 err = devm_add_action_or_reset(dev, __devm_hte_release_ts, desc);
717 EXPORT_SYMBOL_GPL(devm_hte_request_ts_ns);
720 * hte_init_line_attr() - Initialize line attributes.
722 * Zeroes out line attributes and initializes with provided arguments.
723 * The function needs to be called before calling any consumer facing
726 * @desc: Pre-allocated timestamp descriptor.
728 * @edge_flags: edge flags related to line_id.
729 * @name: name of the line.
730 * @data: line data related to line_id.
733 * Returns: 0 on success or negative error code for the failure.
735 int hte_init_line_attr(struct hte_ts_desc *desc, u32 line_id,
736 unsigned long edge_flags, const char *name, void *data)
741 memset(&desc->attr, 0, sizeof(desc->attr));
743 desc->attr.edge_flags = edge_flags;
744 desc->attr.line_id = line_id;
745 desc->attr.line_data = data;
747 name = kstrdup_const(name, GFP_KERNEL);
752 desc->attr.name = name;
756 EXPORT_SYMBOL_GPL(hte_init_line_attr);
759 * hte_get_clk_src_info() - Get the clock source information for a ts
762 * @desc: ts descriptor, same as returned from request API.
763 * @ci: The API fills this structure with the clock information data.
765 * Context: Any context.
766 * Returns: 0 on success else negative error code on failure.
768 int hte_get_clk_src_info(const struct hte_ts_desc *desc,
769 struct hte_clk_info *ci)
771 struct hte_chip *chip;
772 struct hte_ts_info *ei;
774 if (!desc || !desc->hte_data || !ci) {
775 pr_debug("%s:%d\n", __func__, __LINE__);
780 if (!ei->gdev || !ei->gdev->chip)
783 chip = ei->gdev->chip;
784 if (!chip->ops->get_clk_src_info)
787 return chip->ops->get_clk_src_info(chip, ci);
789 EXPORT_SYMBOL_GPL(hte_get_clk_src_info);
792 * hte_push_ts_ns() - Push timestamp data in nanoseconds.
794 * It is used by the provider to push timestamp data.
796 * @chip: The HTE chip, used during the registration.
797 * @xlated_id: entity id understood by both subsystem and provider, this is
798 * obtained from xlate callback during request API.
799 * @data: timestamp data.
801 * Returns: 0 on success or a negative error code on failure.
803 int hte_push_ts_ns(const struct hte_chip *chip, u32 xlated_id,
804 struct hte_ts_data *data)
808 struct hte_ts_info *ei;
811 if (!chip || !data || !chip->gdev)
814 if (xlated_id >= chip->nlines)
817 ei = &chip->gdev->ei[xlated_id];
819 spin_lock_irqsave(&ei->slock, flag);
821 /* timestamp sequence counter */
822 data->seq = ei->seq++;
824 if (!test_bit(HTE_TS_REGISTERED, &ei->flags) ||
825 test_bit(HTE_TS_DISABLE, &ei->flags)) {
826 dev_dbg(chip->dev, "Unknown timestamp push\n");
827 atomic_inc(&ei->dropped_ts);
832 ret = ei->cb(data, ei->cl_data);
833 if (ret == HTE_RUN_SECOND_CB && ei->tcb) {
834 queue_work(system_unbound_wq, &ei->cb_work);
835 set_bit(HTE_TS_QUEUE_WK, &ei->flags);
839 spin_unlock_irqrestore(&ei->slock, flag);
843 EXPORT_SYMBOL_GPL(hte_push_ts_ns);
845 static int hte_register_chip(struct hte_chip *chip)
847 struct hte_device *gdev;
850 if (!chip || !chip->dev || !chip->dev->of_node)
853 if (!chip->ops || !chip->ops->request || !chip->ops->release) {
854 dev_err(chip->dev, "Driver needs to provide ops\n");
858 gdev = kzalloc(struct_size(gdev, ei, chip->nlines), GFP_KERNEL);
864 gdev->nlines = chip->nlines;
865 gdev->sdev = chip->dev;
867 for (i = 0; i < chip->nlines; i++) {
868 gdev->ei[i].gdev = gdev;
869 mutex_init(&gdev->ei[i].req_mlock);
870 spin_lock_init(&gdev->ei[i].slock);
873 if (chip->dev->driver)
874 gdev->owner = chip->dev->driver->owner;
876 gdev->owner = THIS_MODULE;
878 of_node_get(chip->dev->of_node);
880 INIT_LIST_HEAD(&gdev->list);
882 spin_lock(&hte_lock);
883 list_add_tail(&gdev->list, &hte_devices);
884 spin_unlock(&hte_lock);
886 hte_chip_dbgfs_init(gdev);
888 dev_dbg(chip->dev, "Added hte chip\n");
893 static int hte_unregister_chip(struct hte_chip *chip)
895 struct hte_device *gdev;
902 spin_lock(&hte_lock);
903 list_del(&gdev->list);
904 spin_unlock(&hte_lock);
908 of_node_put(chip->dev->of_node);
909 debugfs_remove_recursive(gdev->dbg_root);
912 dev_dbg(chip->dev, "Removed hte chip\n");
917 static void _hte_devm_unregister_chip(void *chip)
919 hte_unregister_chip(chip);
923 * devm_hte_register_chip() - Resource managed API to register HTE chip.
925 * It is used by the provider to register itself with the HTE subsystem.
926 * The unregistration is done automatically when the provider exits.
928 * @chip: the HTE chip to add to subsystem.
930 * Returns: 0 on success or a negative error code on failure.
932 int devm_hte_register_chip(struct hte_chip *chip)
936 err = hte_register_chip(chip);
940 err = devm_add_action_or_reset(chip->dev, _hte_devm_unregister_chip,
947 EXPORT_SYMBOL_GPL(devm_hte_register_chip);