1 // SPDX-License-Identifier: GPL-2.0
3 * dpll_core.c - DPLL subsystem kernel-space interface implementation.
5 * Copyright (c) 2023 Meta Platforms, Inc. and affiliates
6 * Copyright (c) 2023 Intel Corporation.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
16 #include "dpll_core.h"
17 #include "dpll_netlink.h"
19 /* Mutex lock to protect DPLL subsystem devices and pins */
20 DEFINE_MUTEX(dpll_lock);
22 DEFINE_XARRAY_FLAGS(dpll_device_xa, XA_FLAGS_ALLOC);
23 DEFINE_XARRAY_FLAGS(dpll_pin_xa, XA_FLAGS_ALLOC);
25 static u32 dpll_xa_id;
27 #define ASSERT_DPLL_REGISTERED(d) \
28 WARN_ON_ONCE(!xa_get_mark(&dpll_device_xa, (d)->id, DPLL_REGISTERED))
29 #define ASSERT_DPLL_NOT_REGISTERED(d) \
30 WARN_ON_ONCE(xa_get_mark(&dpll_device_xa, (d)->id, DPLL_REGISTERED))
32 struct dpll_device_registration {
33 struct list_head list;
34 const struct dpll_device_ops *ops;
38 struct dpll_pin_registration {
39 struct list_head list;
40 const struct dpll_pin_ops *ops;
44 struct dpll_device *dpll_device_get_by_id(int id)
46 if (xa_get_mark(&dpll_device_xa, id, DPLL_REGISTERED))
47 return xa_load(&dpll_device_xa, id);
52 static struct dpll_pin_registration *
53 dpll_pin_registration_find(struct dpll_pin_ref *ref,
54 const struct dpll_pin_ops *ops, void *priv)
56 struct dpll_pin_registration *reg;
58 list_for_each_entry(reg, &ref->registration_list, list) {
59 if (reg->ops == ops && reg->priv == priv)
66 dpll_xa_ref_pin_add(struct xarray *xa_pins, struct dpll_pin *pin,
67 const struct dpll_pin_ops *ops, void *priv)
69 struct dpll_pin_registration *reg;
70 struct dpll_pin_ref *ref;
71 bool ref_exists = false;
75 xa_for_each(xa_pins, i, ref) {
78 reg = dpll_pin_registration_find(ref, ops, priv);
80 refcount_inc(&ref->refcount);
88 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
92 INIT_LIST_HEAD(&ref->registration_list);
93 ret = xa_insert(xa_pins, pin->pin_idx, ref, GFP_KERNEL);
98 refcount_set(&ref->refcount, 1);
101 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
104 xa_erase(xa_pins, pin->pin_idx);
112 refcount_inc(&ref->refcount);
113 list_add_tail(®->list, &ref->registration_list);
118 static int dpll_xa_ref_pin_del(struct xarray *xa_pins, struct dpll_pin *pin,
119 const struct dpll_pin_ops *ops, void *priv)
121 struct dpll_pin_registration *reg;
122 struct dpll_pin_ref *ref;
125 xa_for_each(xa_pins, i, ref) {
128 reg = dpll_pin_registration_find(ref, ops, priv);
131 if (refcount_dec_and_test(&ref->refcount)) {
132 list_del(®->list);
134 xa_erase(xa_pins, i);
135 WARN_ON(!list_empty(&ref->registration_list));
145 dpll_xa_ref_dpll_add(struct xarray *xa_dplls, struct dpll_device *dpll,
146 const struct dpll_pin_ops *ops, void *priv)
148 struct dpll_pin_registration *reg;
149 struct dpll_pin_ref *ref;
150 bool ref_exists = false;
154 xa_for_each(xa_dplls, i, ref) {
155 if (ref->dpll != dpll)
157 reg = dpll_pin_registration_find(ref, ops, priv);
159 refcount_inc(&ref->refcount);
167 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
171 INIT_LIST_HEAD(&ref->registration_list);
172 ret = xa_insert(xa_dplls, dpll->id, ref, GFP_KERNEL);
177 refcount_set(&ref->refcount, 1);
180 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
183 xa_erase(xa_dplls, dpll->id);
191 refcount_inc(&ref->refcount);
192 list_add_tail(®->list, &ref->registration_list);
198 dpll_xa_ref_dpll_del(struct xarray *xa_dplls, struct dpll_device *dpll,
199 const struct dpll_pin_ops *ops, void *priv)
201 struct dpll_pin_registration *reg;
202 struct dpll_pin_ref *ref;
205 xa_for_each(xa_dplls, i, ref) {
206 if (ref->dpll != dpll)
208 reg = dpll_pin_registration_find(ref, ops, priv);
211 if (refcount_dec_and_test(&ref->refcount)) {
212 list_del(®->list);
214 xa_erase(xa_dplls, i);
215 WARN_ON(!list_empty(&ref->registration_list));
222 struct dpll_pin_ref *dpll_xa_ref_dpll_first(struct xarray *xa_refs)
224 struct dpll_pin_ref *ref;
227 ref = xa_find(xa_refs, &i, ULONG_MAX, XA_PRESENT);
232 static struct dpll_device *
233 dpll_device_alloc(const u64 clock_id, u32 device_idx, struct module *module)
235 struct dpll_device *dpll;
238 dpll = kzalloc(sizeof(*dpll), GFP_KERNEL);
240 return ERR_PTR(-ENOMEM);
241 refcount_set(&dpll->refcount, 1);
242 INIT_LIST_HEAD(&dpll->registration_list);
243 dpll->device_idx = device_idx;
244 dpll->clock_id = clock_id;
245 dpll->module = module;
246 ret = xa_alloc_cyclic(&dpll_device_xa, &dpll->id, dpll, xa_limit_32b,
247 &dpll_xa_id, GFP_KERNEL);
252 xa_init_flags(&dpll->pin_refs, XA_FLAGS_ALLOC);
258 * dpll_device_get - find existing or create new dpll device
259 * @clock_id: clock_id of creator
260 * @device_idx: idx given by device driver
261 * @module: reference to registering module
263 * Get existing object of a dpll device, unique for given arguments.
264 * Create new if doesn't exist yet.
266 * Context: Acquires a lock (dpll_lock)
268 * * valid dpll_device struct pointer if succeeded
269 * * ERR_PTR(X) - error
272 dpll_device_get(u64 clock_id, u32 device_idx, struct module *module)
274 struct dpll_device *dpll, *ret = NULL;
277 mutex_lock(&dpll_lock);
278 xa_for_each(&dpll_device_xa, index, dpll) {
279 if (dpll->clock_id == clock_id &&
280 dpll->device_idx == device_idx &&
281 dpll->module == module) {
283 refcount_inc(&ret->refcount);
288 ret = dpll_device_alloc(clock_id, device_idx, module);
289 mutex_unlock(&dpll_lock);
293 EXPORT_SYMBOL_GPL(dpll_device_get);
296 * dpll_device_put - decrease the refcount and free memory if possible
297 * @dpll: dpll_device struct pointer
299 * Context: Acquires a lock (dpll_lock)
300 * Drop reference for a dpll device, if all references are gone, delete
301 * dpll device object.
303 void dpll_device_put(struct dpll_device *dpll)
305 mutex_lock(&dpll_lock);
306 if (refcount_dec_and_test(&dpll->refcount)) {
307 ASSERT_DPLL_NOT_REGISTERED(dpll);
308 WARN_ON_ONCE(!xa_empty(&dpll->pin_refs));
309 xa_destroy(&dpll->pin_refs);
310 xa_erase(&dpll_device_xa, dpll->id);
311 WARN_ON(!list_empty(&dpll->registration_list));
314 mutex_unlock(&dpll_lock);
316 EXPORT_SYMBOL_GPL(dpll_device_put);
318 static struct dpll_device_registration *
319 dpll_device_registration_find(struct dpll_device *dpll,
320 const struct dpll_device_ops *ops, void *priv)
322 struct dpll_device_registration *reg;
324 list_for_each_entry(reg, &dpll->registration_list, list) {
325 if (reg->ops == ops && reg->priv == priv)
332 * dpll_device_register - register the dpll device in the subsystem
333 * @dpll: pointer to a dpll
334 * @type: type of a dpll
335 * @ops: ops for a dpll device
336 * @priv: pointer to private information of owner
338 * Make dpll device available for user space.
340 * Context: Acquires a lock (dpll_lock)
343 * * negative - error value
345 int dpll_device_register(struct dpll_device *dpll, enum dpll_type type,
346 const struct dpll_device_ops *ops, void *priv)
348 struct dpll_device_registration *reg;
349 bool first_registration = false;
353 if (WARN_ON(!ops->mode_get))
355 if (WARN_ON(!ops->lock_status_get))
357 if (WARN_ON(type < DPLL_TYPE_PPS || type > DPLL_TYPE_MAX))
360 mutex_lock(&dpll_lock);
361 reg = dpll_device_registration_find(dpll, ops, priv);
363 mutex_unlock(&dpll_lock);
367 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
369 mutex_unlock(&dpll_lock);
375 first_registration = list_empty(&dpll->registration_list);
376 list_add_tail(®->list, &dpll->registration_list);
377 if (!first_registration) {
378 mutex_unlock(&dpll_lock);
382 xa_set_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
383 dpll_device_create_ntf(dpll);
384 mutex_unlock(&dpll_lock);
388 EXPORT_SYMBOL_GPL(dpll_device_register);
391 * dpll_device_unregister - unregister dpll device
392 * @dpll: registered dpll pointer
393 * @ops: ops for a dpll device
394 * @priv: pointer to private information of owner
396 * Unregister device, make it unavailable for userspace.
397 * Note: It does not free the memory
398 * Context: Acquires a lock (dpll_lock)
400 void dpll_device_unregister(struct dpll_device *dpll,
401 const struct dpll_device_ops *ops, void *priv)
403 struct dpll_device_registration *reg;
405 mutex_lock(&dpll_lock);
406 ASSERT_DPLL_REGISTERED(dpll);
407 dpll_device_delete_ntf(dpll);
408 reg = dpll_device_registration_find(dpll, ops, priv);
410 mutex_unlock(&dpll_lock);
413 list_del(®->list);
416 if (!list_empty(&dpll->registration_list)) {
417 mutex_unlock(&dpll_lock);
420 xa_clear_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
421 mutex_unlock(&dpll_lock);
423 EXPORT_SYMBOL_GPL(dpll_device_unregister);
425 static void dpll_pin_prop_free(struct dpll_pin_properties *prop)
427 kfree(prop->package_label);
428 kfree(prop->panel_label);
429 kfree(prop->board_label);
430 kfree(prop->freq_supported);
433 static int dpll_pin_prop_dup(const struct dpll_pin_properties *src,
434 struct dpll_pin_properties *dst)
436 memcpy(dst, src, sizeof(*dst));
437 if (src->freq_supported && src->freq_supported_num) {
438 size_t freq_size = src->freq_supported_num *
439 sizeof(*src->freq_supported);
440 dst->freq_supported = kmemdup(src->freq_supported,
441 freq_size, GFP_KERNEL);
442 if (!src->freq_supported)
445 if (src->board_label) {
446 dst->board_label = kstrdup(src->board_label, GFP_KERNEL);
447 if (!dst->board_label)
448 goto err_board_label;
450 if (src->panel_label) {
451 dst->panel_label = kstrdup(src->panel_label, GFP_KERNEL);
452 if (!dst->panel_label)
453 goto err_panel_label;
455 if (src->package_label) {
456 dst->package_label = kstrdup(src->package_label, GFP_KERNEL);
457 if (!dst->package_label)
458 goto err_package_label;
464 kfree(dst->panel_label);
466 kfree(dst->board_label);
468 kfree(dst->freq_supported);
472 static struct dpll_pin *
473 dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,
474 const struct dpll_pin_properties *prop)
476 struct dpll_pin *pin;
479 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
481 return ERR_PTR(-ENOMEM);
482 pin->pin_idx = pin_idx;
483 pin->clock_id = clock_id;
484 pin->module = module;
485 if (WARN_ON(prop->type < DPLL_PIN_TYPE_MUX ||
486 prop->type > DPLL_PIN_TYPE_MAX)) {
490 ret = dpll_pin_prop_dup(prop, &pin->prop);
493 refcount_set(&pin->refcount, 1);
494 xa_init_flags(&pin->dpll_refs, XA_FLAGS_ALLOC);
495 xa_init_flags(&pin->parent_refs, XA_FLAGS_ALLOC);
496 ret = xa_alloc(&dpll_pin_xa, &pin->id, pin, xa_limit_16b, GFP_KERNEL);
501 xa_destroy(&pin->dpll_refs);
502 xa_destroy(&pin->parent_refs);
503 dpll_pin_prop_free(&pin->prop);
510 * dpll_pin_get - find existing or create new dpll pin
511 * @clock_id: clock_id of creator
512 * @pin_idx: idx given by dev driver
513 * @module: reference to registering module
514 * @prop: dpll pin properties
516 * Get existing object of a pin (unique for given arguments) or create new
517 * if doesn't exist yet.
519 * Context: Acquires a lock (dpll_lock)
521 * * valid allocated dpll_pin struct pointer if succeeded
522 * * ERR_PTR(X) - error
525 dpll_pin_get(u64 clock_id, u32 pin_idx, struct module *module,
526 const struct dpll_pin_properties *prop)
528 struct dpll_pin *pos, *ret = NULL;
531 mutex_lock(&dpll_lock);
532 xa_for_each(&dpll_pin_xa, i, pos) {
533 if (pos->clock_id == clock_id &&
534 pos->pin_idx == pin_idx &&
535 pos->module == module) {
537 refcount_inc(&ret->refcount);
542 ret = dpll_pin_alloc(clock_id, pin_idx, module, prop);
543 mutex_unlock(&dpll_lock);
547 EXPORT_SYMBOL_GPL(dpll_pin_get);
550 * dpll_pin_put - decrease the refcount and free memory if possible
551 * @pin: pointer to a pin to be put
553 * Drop reference for a pin, if all references are gone, delete pin object.
555 * Context: Acquires a lock (dpll_lock)
557 void dpll_pin_put(struct dpll_pin *pin)
559 mutex_lock(&dpll_lock);
560 if (refcount_dec_and_test(&pin->refcount)) {
561 xa_destroy(&pin->dpll_refs);
562 xa_destroy(&pin->parent_refs);
563 xa_erase(&dpll_pin_xa, pin->id);
564 dpll_pin_prop_free(&pin->prop);
567 mutex_unlock(&dpll_lock);
569 EXPORT_SYMBOL_GPL(dpll_pin_put);
572 __dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
573 const struct dpll_pin_ops *ops, void *priv)
577 ret = dpll_xa_ref_pin_add(&dpll->pin_refs, pin, ops, priv);
580 ret = dpll_xa_ref_dpll_add(&pin->dpll_refs, dpll, ops, priv);
583 xa_set_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
584 dpll_pin_create_ntf(pin);
589 dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
594 * dpll_pin_register - register the dpll pin in the subsystem
595 * @dpll: pointer to a dpll
596 * @pin: pointer to a dpll pin
597 * @ops: ops for a dpll pin ops
598 * @priv: pointer to private information of owner
600 * Context: Acquires a lock (dpll_lock)
603 * * negative - error value
606 dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
607 const struct dpll_pin_ops *ops, void *priv)
612 WARN_ON(!ops->state_on_dpll_get) ||
613 WARN_ON(!ops->direction_get))
616 mutex_lock(&dpll_lock);
617 if (WARN_ON(!(dpll->module == pin->module &&
618 dpll->clock_id == pin->clock_id)))
621 ret = __dpll_pin_register(dpll, pin, ops, priv);
622 mutex_unlock(&dpll_lock);
626 EXPORT_SYMBOL_GPL(dpll_pin_register);
629 __dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
630 const struct dpll_pin_ops *ops, void *priv)
632 dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
633 dpll_xa_ref_dpll_del(&pin->dpll_refs, dpll, ops, priv);
634 if (xa_empty(&pin->dpll_refs))
635 xa_clear_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
639 * dpll_pin_unregister - unregister dpll pin from dpll device
640 * @dpll: registered dpll pointer
641 * @pin: pointer to a pin
642 * @ops: ops for a dpll pin
643 * @priv: pointer to private information of owner
645 * Note: It does not free the memory
646 * Context: Acquires a lock (dpll_lock)
648 void dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
649 const struct dpll_pin_ops *ops, void *priv)
651 if (WARN_ON(xa_empty(&dpll->pin_refs)))
653 if (WARN_ON(!xa_empty(&pin->parent_refs)))
656 mutex_lock(&dpll_lock);
657 dpll_pin_delete_ntf(pin);
658 __dpll_pin_unregister(dpll, pin, ops, priv);
659 mutex_unlock(&dpll_lock);
661 EXPORT_SYMBOL_GPL(dpll_pin_unregister);
664 * dpll_pin_on_pin_register - register a pin with a parent pin
665 * @parent: pointer to a parent pin
666 * @pin: pointer to a pin
667 * @ops: ops for a dpll pin
668 * @priv: pointer to private information of owner
670 * Register a pin with a parent pin, create references between them and
671 * between newly registered pin and dplls connected with a parent pin.
673 * Context: Acquires a lock (dpll_lock)
676 * * negative - error value
678 int dpll_pin_on_pin_register(struct dpll_pin *parent, struct dpll_pin *pin,
679 const struct dpll_pin_ops *ops, void *priv)
681 struct dpll_pin_ref *ref;
682 unsigned long i, stop;
685 if (WARN_ON(parent->prop.type != DPLL_PIN_TYPE_MUX))
689 WARN_ON(!ops->state_on_pin_get) ||
690 WARN_ON(!ops->direction_get))
693 mutex_lock(&dpll_lock);
694 ret = dpll_xa_ref_pin_add(&pin->parent_refs, parent, ops, priv);
697 refcount_inc(&pin->refcount);
698 xa_for_each(&parent->dpll_refs, i, ref) {
699 ret = __dpll_pin_register(ref->dpll, pin, ops, priv);
702 goto dpll_unregister;
704 dpll_pin_create_ntf(pin);
706 mutex_unlock(&dpll_lock);
711 xa_for_each(&parent->dpll_refs, i, ref)
713 __dpll_pin_unregister(ref->dpll, pin, ops, priv);
714 dpll_pin_delete_ntf(pin);
716 refcount_dec(&pin->refcount);
717 dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
719 mutex_unlock(&dpll_lock);
722 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_register);
725 * dpll_pin_on_pin_unregister - unregister dpll pin from a parent pin
726 * @parent: pointer to a parent pin
727 * @pin: pointer to a pin
728 * @ops: ops for a dpll pin
729 * @priv: pointer to private information of owner
731 * Context: Acquires a lock (dpll_lock)
732 * Note: It does not free the memory
734 void dpll_pin_on_pin_unregister(struct dpll_pin *parent, struct dpll_pin *pin,
735 const struct dpll_pin_ops *ops, void *priv)
737 struct dpll_pin_ref *ref;
740 mutex_lock(&dpll_lock);
741 dpll_pin_delete_ntf(pin);
742 dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
743 refcount_dec(&pin->refcount);
744 xa_for_each(&pin->dpll_refs, i, ref)
745 __dpll_pin_unregister(ref->dpll, pin, ops, priv);
746 mutex_unlock(&dpll_lock);
748 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_unregister);
750 static struct dpll_device_registration *
751 dpll_device_registration_first(struct dpll_device *dpll)
753 struct dpll_device_registration *reg;
755 reg = list_first_entry_or_null((struct list_head *)&dpll->registration_list,
756 struct dpll_device_registration, list);
761 void *dpll_priv(struct dpll_device *dpll)
763 struct dpll_device_registration *reg;
765 reg = dpll_device_registration_first(dpll);
769 const struct dpll_device_ops *dpll_device_ops(struct dpll_device *dpll)
771 struct dpll_device_registration *reg;
773 reg = dpll_device_registration_first(dpll);
777 static struct dpll_pin_registration *
778 dpll_pin_registration_first(struct dpll_pin_ref *ref)
780 struct dpll_pin_registration *reg;
782 reg = list_first_entry_or_null(&ref->registration_list,
783 struct dpll_pin_registration, list);
788 void *dpll_pin_on_dpll_priv(struct dpll_device *dpll,
789 struct dpll_pin *pin)
791 struct dpll_pin_registration *reg;
792 struct dpll_pin_ref *ref;
794 ref = xa_load(&dpll->pin_refs, pin->pin_idx);
797 reg = dpll_pin_registration_first(ref);
801 void *dpll_pin_on_pin_priv(struct dpll_pin *parent,
802 struct dpll_pin *pin)
804 struct dpll_pin_registration *reg;
805 struct dpll_pin_ref *ref;
807 ref = xa_load(&pin->parent_refs, parent->pin_idx);
810 reg = dpll_pin_registration_first(ref);
814 const struct dpll_pin_ops *dpll_pin_ops(struct dpll_pin_ref *ref)
816 struct dpll_pin_registration *reg;
818 reg = dpll_pin_registration_first(ref);
822 static int __init dpll_init(void)
826 ret = genl_register_family(&dpll_nl_family);
833 mutex_destroy(&dpll_lock);
837 static void __exit dpll_exit(void)
839 genl_unregister_family(&dpll_nl_family);
840 mutex_destroy(&dpll_lock);
843 subsys_initcall(dpll_init);
844 module_exit(dpll_exit);