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_device_xa_id;
26 static u32 dpll_pin_xa_id;
28 #define ASSERT_DPLL_REGISTERED(d) \
29 WARN_ON_ONCE(!xa_get_mark(&dpll_device_xa, (d)->id, DPLL_REGISTERED))
30 #define ASSERT_DPLL_NOT_REGISTERED(d) \
31 WARN_ON_ONCE(xa_get_mark(&dpll_device_xa, (d)->id, DPLL_REGISTERED))
33 struct dpll_device_registration {
34 struct list_head list;
35 const struct dpll_device_ops *ops;
39 struct dpll_pin_registration {
40 struct list_head list;
41 const struct dpll_pin_ops *ops;
45 struct dpll_device *dpll_device_get_by_id(int id)
47 if (xa_get_mark(&dpll_device_xa, id, DPLL_REGISTERED))
48 return xa_load(&dpll_device_xa, id);
53 static struct dpll_pin_registration *
54 dpll_pin_registration_find(struct dpll_pin_ref *ref,
55 const struct dpll_pin_ops *ops, void *priv)
57 struct dpll_pin_registration *reg;
59 list_for_each_entry(reg, &ref->registration_list, list) {
60 if (reg->ops == ops && reg->priv == priv)
67 dpll_xa_ref_pin_add(struct xarray *xa_pins, struct dpll_pin *pin,
68 const struct dpll_pin_ops *ops, void *priv)
70 struct dpll_pin_registration *reg;
71 struct dpll_pin_ref *ref;
72 bool ref_exists = false;
76 xa_for_each(xa_pins, i, ref) {
79 reg = dpll_pin_registration_find(ref, ops, priv);
81 refcount_inc(&ref->refcount);
89 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
93 INIT_LIST_HEAD(&ref->registration_list);
94 ret = xa_insert(xa_pins, pin->pin_idx, ref, GFP_KERNEL);
99 refcount_set(&ref->refcount, 1);
102 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
105 xa_erase(xa_pins, pin->pin_idx);
113 refcount_inc(&ref->refcount);
114 list_add_tail(®->list, &ref->registration_list);
119 static int dpll_xa_ref_pin_del(struct xarray *xa_pins, struct dpll_pin *pin,
120 const struct dpll_pin_ops *ops, void *priv)
122 struct dpll_pin_registration *reg;
123 struct dpll_pin_ref *ref;
126 xa_for_each(xa_pins, i, ref) {
129 reg = dpll_pin_registration_find(ref, ops, priv);
132 if (refcount_dec_and_test(&ref->refcount)) {
133 list_del(®->list);
135 xa_erase(xa_pins, i);
136 WARN_ON(!list_empty(&ref->registration_list));
146 dpll_xa_ref_dpll_add(struct xarray *xa_dplls, struct dpll_device *dpll,
147 const struct dpll_pin_ops *ops, void *priv)
149 struct dpll_pin_registration *reg;
150 struct dpll_pin_ref *ref;
151 bool ref_exists = false;
155 xa_for_each(xa_dplls, i, ref) {
156 if (ref->dpll != dpll)
158 reg = dpll_pin_registration_find(ref, ops, priv);
160 refcount_inc(&ref->refcount);
168 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
172 INIT_LIST_HEAD(&ref->registration_list);
173 ret = xa_insert(xa_dplls, dpll->id, ref, GFP_KERNEL);
178 refcount_set(&ref->refcount, 1);
181 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
184 xa_erase(xa_dplls, dpll->id);
192 refcount_inc(&ref->refcount);
193 list_add_tail(®->list, &ref->registration_list);
199 dpll_xa_ref_dpll_del(struct xarray *xa_dplls, struct dpll_device *dpll,
200 const struct dpll_pin_ops *ops, void *priv)
202 struct dpll_pin_registration *reg;
203 struct dpll_pin_ref *ref;
206 xa_for_each(xa_dplls, i, ref) {
207 if (ref->dpll != dpll)
209 reg = dpll_pin_registration_find(ref, ops, priv);
212 if (refcount_dec_and_test(&ref->refcount)) {
213 list_del(®->list);
215 xa_erase(xa_dplls, i);
216 WARN_ON(!list_empty(&ref->registration_list));
223 struct dpll_pin_ref *dpll_xa_ref_dpll_first(struct xarray *xa_refs)
225 struct dpll_pin_ref *ref;
228 ref = xa_find(xa_refs, &i, ULONG_MAX, XA_PRESENT);
233 static struct dpll_device *
234 dpll_device_alloc(const u64 clock_id, u32 device_idx, struct module *module)
236 struct dpll_device *dpll;
239 dpll = kzalloc(sizeof(*dpll), GFP_KERNEL);
241 return ERR_PTR(-ENOMEM);
242 refcount_set(&dpll->refcount, 1);
243 INIT_LIST_HEAD(&dpll->registration_list);
244 dpll->device_idx = device_idx;
245 dpll->clock_id = clock_id;
246 dpll->module = module;
247 ret = xa_alloc_cyclic(&dpll_device_xa, &dpll->id, dpll, xa_limit_32b,
248 &dpll_device_xa_id, GFP_KERNEL);
253 xa_init_flags(&dpll->pin_refs, XA_FLAGS_ALLOC);
259 * dpll_device_get - find existing or create new dpll device
260 * @clock_id: clock_id of creator
261 * @device_idx: idx given by device driver
262 * @module: reference to registering module
264 * Get existing object of a dpll device, unique for given arguments.
265 * Create new if doesn't exist yet.
267 * Context: Acquires a lock (dpll_lock)
269 * * valid dpll_device struct pointer if succeeded
270 * * ERR_PTR(X) - error
273 dpll_device_get(u64 clock_id, u32 device_idx, struct module *module)
275 struct dpll_device *dpll, *ret = NULL;
278 mutex_lock(&dpll_lock);
279 xa_for_each(&dpll_device_xa, index, dpll) {
280 if (dpll->clock_id == clock_id &&
281 dpll->device_idx == device_idx &&
282 dpll->module == module) {
284 refcount_inc(&ret->refcount);
289 ret = dpll_device_alloc(clock_id, device_idx, module);
290 mutex_unlock(&dpll_lock);
294 EXPORT_SYMBOL_GPL(dpll_device_get);
297 * dpll_device_put - decrease the refcount and free memory if possible
298 * @dpll: dpll_device struct pointer
300 * Context: Acquires a lock (dpll_lock)
301 * Drop reference for a dpll device, if all references are gone, delete
302 * dpll device object.
304 void dpll_device_put(struct dpll_device *dpll)
306 mutex_lock(&dpll_lock);
307 if (refcount_dec_and_test(&dpll->refcount)) {
308 ASSERT_DPLL_NOT_REGISTERED(dpll);
309 WARN_ON_ONCE(!xa_empty(&dpll->pin_refs));
310 xa_destroy(&dpll->pin_refs);
311 xa_erase(&dpll_device_xa, dpll->id);
312 WARN_ON(!list_empty(&dpll->registration_list));
315 mutex_unlock(&dpll_lock);
317 EXPORT_SYMBOL_GPL(dpll_device_put);
319 static struct dpll_device_registration *
320 dpll_device_registration_find(struct dpll_device *dpll,
321 const struct dpll_device_ops *ops, void *priv)
323 struct dpll_device_registration *reg;
325 list_for_each_entry(reg, &dpll->registration_list, list) {
326 if (reg->ops == ops && reg->priv == priv)
333 * dpll_device_register - register the dpll device in the subsystem
334 * @dpll: pointer to a dpll
335 * @type: type of a dpll
336 * @ops: ops for a dpll device
337 * @priv: pointer to private information of owner
339 * Make dpll device available for user space.
341 * Context: Acquires a lock (dpll_lock)
344 * * negative - error value
346 int dpll_device_register(struct dpll_device *dpll, enum dpll_type type,
347 const struct dpll_device_ops *ops, void *priv)
349 struct dpll_device_registration *reg;
350 bool first_registration = false;
354 if (WARN_ON(!ops->mode_get))
356 if (WARN_ON(!ops->lock_status_get))
358 if (WARN_ON(type < DPLL_TYPE_PPS || type > DPLL_TYPE_MAX))
361 mutex_lock(&dpll_lock);
362 reg = dpll_device_registration_find(dpll, ops, priv);
364 mutex_unlock(&dpll_lock);
368 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
370 mutex_unlock(&dpll_lock);
376 first_registration = list_empty(&dpll->registration_list);
377 list_add_tail(®->list, &dpll->registration_list);
378 if (!first_registration) {
379 mutex_unlock(&dpll_lock);
383 xa_set_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
384 dpll_device_create_ntf(dpll);
385 mutex_unlock(&dpll_lock);
389 EXPORT_SYMBOL_GPL(dpll_device_register);
392 * dpll_device_unregister - unregister dpll device
393 * @dpll: registered dpll pointer
394 * @ops: ops for a dpll device
395 * @priv: pointer to private information of owner
397 * Unregister device, make it unavailable for userspace.
398 * Note: It does not free the memory
399 * Context: Acquires a lock (dpll_lock)
401 void dpll_device_unregister(struct dpll_device *dpll,
402 const struct dpll_device_ops *ops, void *priv)
404 struct dpll_device_registration *reg;
406 mutex_lock(&dpll_lock);
407 ASSERT_DPLL_REGISTERED(dpll);
408 dpll_device_delete_ntf(dpll);
409 reg = dpll_device_registration_find(dpll, ops, priv);
411 mutex_unlock(&dpll_lock);
414 list_del(®->list);
417 if (!list_empty(&dpll->registration_list)) {
418 mutex_unlock(&dpll_lock);
421 xa_clear_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
422 mutex_unlock(&dpll_lock);
424 EXPORT_SYMBOL_GPL(dpll_device_unregister);
426 static void dpll_pin_prop_free(struct dpll_pin_properties *prop)
428 kfree(prop->package_label);
429 kfree(prop->panel_label);
430 kfree(prop->board_label);
431 kfree(prop->freq_supported);
434 static int dpll_pin_prop_dup(const struct dpll_pin_properties *src,
435 struct dpll_pin_properties *dst)
437 memcpy(dst, src, sizeof(*dst));
438 if (src->freq_supported && src->freq_supported_num) {
439 size_t freq_size = src->freq_supported_num *
440 sizeof(*src->freq_supported);
441 dst->freq_supported = kmemdup(src->freq_supported,
442 freq_size, GFP_KERNEL);
443 if (!src->freq_supported)
446 if (src->board_label) {
447 dst->board_label = kstrdup(src->board_label, GFP_KERNEL);
448 if (!dst->board_label)
449 goto err_board_label;
451 if (src->panel_label) {
452 dst->panel_label = kstrdup(src->panel_label, GFP_KERNEL);
453 if (!dst->panel_label)
454 goto err_panel_label;
456 if (src->package_label) {
457 dst->package_label = kstrdup(src->package_label, GFP_KERNEL);
458 if (!dst->package_label)
459 goto err_package_label;
465 kfree(dst->panel_label);
467 kfree(dst->board_label);
469 kfree(dst->freq_supported);
473 static struct dpll_pin *
474 dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,
475 const struct dpll_pin_properties *prop)
477 struct dpll_pin *pin;
480 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
482 return ERR_PTR(-ENOMEM);
483 pin->pin_idx = pin_idx;
484 pin->clock_id = clock_id;
485 pin->module = module;
486 if (WARN_ON(prop->type < DPLL_PIN_TYPE_MUX ||
487 prop->type > DPLL_PIN_TYPE_MAX)) {
491 ret = dpll_pin_prop_dup(prop, &pin->prop);
494 refcount_set(&pin->refcount, 1);
495 xa_init_flags(&pin->dpll_refs, XA_FLAGS_ALLOC);
496 xa_init_flags(&pin->parent_refs, XA_FLAGS_ALLOC);
497 ret = xa_alloc_cyclic(&dpll_pin_xa, &pin->id, pin, xa_limit_32b,
498 &dpll_pin_xa_id, GFP_KERNEL);
503 xa_destroy(&pin->dpll_refs);
504 xa_destroy(&pin->parent_refs);
505 dpll_pin_prop_free(&pin->prop);
512 * dpll_pin_get - find existing or create new dpll pin
513 * @clock_id: clock_id of creator
514 * @pin_idx: idx given by dev driver
515 * @module: reference to registering module
516 * @prop: dpll pin properties
518 * Get existing object of a pin (unique for given arguments) or create new
519 * if doesn't exist yet.
521 * Context: Acquires a lock (dpll_lock)
523 * * valid allocated dpll_pin struct pointer if succeeded
524 * * ERR_PTR(X) - error
527 dpll_pin_get(u64 clock_id, u32 pin_idx, struct module *module,
528 const struct dpll_pin_properties *prop)
530 struct dpll_pin *pos, *ret = NULL;
533 mutex_lock(&dpll_lock);
534 xa_for_each(&dpll_pin_xa, i, pos) {
535 if (pos->clock_id == clock_id &&
536 pos->pin_idx == pin_idx &&
537 pos->module == module) {
539 refcount_inc(&ret->refcount);
544 ret = dpll_pin_alloc(clock_id, pin_idx, module, prop);
545 mutex_unlock(&dpll_lock);
549 EXPORT_SYMBOL_GPL(dpll_pin_get);
552 * dpll_pin_put - decrease the refcount and free memory if possible
553 * @pin: pointer to a pin to be put
555 * Drop reference for a pin, if all references are gone, delete pin object.
557 * Context: Acquires a lock (dpll_lock)
559 void dpll_pin_put(struct dpll_pin *pin)
561 mutex_lock(&dpll_lock);
562 if (refcount_dec_and_test(&pin->refcount)) {
563 xa_destroy(&pin->dpll_refs);
564 xa_destroy(&pin->parent_refs);
565 xa_erase(&dpll_pin_xa, pin->id);
566 dpll_pin_prop_free(&pin->prop);
569 mutex_unlock(&dpll_lock);
571 EXPORT_SYMBOL_GPL(dpll_pin_put);
574 __dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
575 const struct dpll_pin_ops *ops, void *priv)
579 ret = dpll_xa_ref_pin_add(&dpll->pin_refs, pin, ops, priv);
582 ret = dpll_xa_ref_dpll_add(&pin->dpll_refs, dpll, ops, priv);
585 xa_set_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
586 dpll_pin_create_ntf(pin);
591 dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
596 * dpll_pin_register - register the dpll pin in the subsystem
597 * @dpll: pointer to a dpll
598 * @pin: pointer to a dpll pin
599 * @ops: ops for a dpll pin ops
600 * @priv: pointer to private information of owner
602 * Context: Acquires a lock (dpll_lock)
605 * * negative - error value
608 dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
609 const struct dpll_pin_ops *ops, void *priv)
614 WARN_ON(!ops->state_on_dpll_get) ||
615 WARN_ON(!ops->direction_get))
618 mutex_lock(&dpll_lock);
619 if (WARN_ON(!(dpll->module == pin->module &&
620 dpll->clock_id == pin->clock_id)))
623 ret = __dpll_pin_register(dpll, pin, ops, priv);
624 mutex_unlock(&dpll_lock);
628 EXPORT_SYMBOL_GPL(dpll_pin_register);
631 __dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
632 const struct dpll_pin_ops *ops, void *priv)
634 dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
635 dpll_xa_ref_dpll_del(&pin->dpll_refs, dpll, ops, priv);
636 if (xa_empty(&pin->dpll_refs))
637 xa_clear_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
641 * dpll_pin_unregister - unregister dpll pin from dpll device
642 * @dpll: registered dpll pointer
643 * @pin: pointer to a pin
644 * @ops: ops for a dpll pin
645 * @priv: pointer to private information of owner
647 * Note: It does not free the memory
648 * Context: Acquires a lock (dpll_lock)
650 void dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
651 const struct dpll_pin_ops *ops, void *priv)
653 if (WARN_ON(xa_empty(&dpll->pin_refs)))
655 if (WARN_ON(!xa_empty(&pin->parent_refs)))
658 mutex_lock(&dpll_lock);
659 dpll_pin_delete_ntf(pin);
660 __dpll_pin_unregister(dpll, pin, ops, priv);
661 mutex_unlock(&dpll_lock);
663 EXPORT_SYMBOL_GPL(dpll_pin_unregister);
666 * dpll_pin_on_pin_register - register a pin with a parent pin
667 * @parent: pointer to a parent pin
668 * @pin: pointer to a pin
669 * @ops: ops for a dpll pin
670 * @priv: pointer to private information of owner
672 * Register a pin with a parent pin, create references between them and
673 * between newly registered pin and dplls connected with a parent pin.
675 * Context: Acquires a lock (dpll_lock)
678 * * negative - error value
680 int dpll_pin_on_pin_register(struct dpll_pin *parent, struct dpll_pin *pin,
681 const struct dpll_pin_ops *ops, void *priv)
683 struct dpll_pin_ref *ref;
684 unsigned long i, stop;
687 if (WARN_ON(parent->prop.type != DPLL_PIN_TYPE_MUX))
691 WARN_ON(!ops->state_on_pin_get) ||
692 WARN_ON(!ops->direction_get))
695 mutex_lock(&dpll_lock);
696 ret = dpll_xa_ref_pin_add(&pin->parent_refs, parent, ops, priv);
699 refcount_inc(&pin->refcount);
700 xa_for_each(&parent->dpll_refs, i, ref) {
701 ret = __dpll_pin_register(ref->dpll, pin, ops, priv);
704 goto dpll_unregister;
706 dpll_pin_create_ntf(pin);
708 mutex_unlock(&dpll_lock);
713 xa_for_each(&parent->dpll_refs, i, ref)
715 __dpll_pin_unregister(ref->dpll, pin, ops, priv);
716 dpll_pin_delete_ntf(pin);
718 refcount_dec(&pin->refcount);
719 dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
721 mutex_unlock(&dpll_lock);
724 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_register);
727 * dpll_pin_on_pin_unregister - unregister dpll pin from a parent pin
728 * @parent: pointer to a parent pin
729 * @pin: pointer to a pin
730 * @ops: ops for a dpll pin
731 * @priv: pointer to private information of owner
733 * Context: Acquires a lock (dpll_lock)
734 * Note: It does not free the memory
736 void dpll_pin_on_pin_unregister(struct dpll_pin *parent, struct dpll_pin *pin,
737 const struct dpll_pin_ops *ops, void *priv)
739 struct dpll_pin_ref *ref;
742 mutex_lock(&dpll_lock);
743 dpll_pin_delete_ntf(pin);
744 dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
745 refcount_dec(&pin->refcount);
746 xa_for_each(&pin->dpll_refs, i, ref)
747 __dpll_pin_unregister(ref->dpll, pin, ops, priv);
748 mutex_unlock(&dpll_lock);
750 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_unregister);
752 static struct dpll_device_registration *
753 dpll_device_registration_first(struct dpll_device *dpll)
755 struct dpll_device_registration *reg;
757 reg = list_first_entry_or_null((struct list_head *)&dpll->registration_list,
758 struct dpll_device_registration, list);
763 void *dpll_priv(struct dpll_device *dpll)
765 struct dpll_device_registration *reg;
767 reg = dpll_device_registration_first(dpll);
771 const struct dpll_device_ops *dpll_device_ops(struct dpll_device *dpll)
773 struct dpll_device_registration *reg;
775 reg = dpll_device_registration_first(dpll);
779 static struct dpll_pin_registration *
780 dpll_pin_registration_first(struct dpll_pin_ref *ref)
782 struct dpll_pin_registration *reg;
784 reg = list_first_entry_or_null(&ref->registration_list,
785 struct dpll_pin_registration, list);
790 void *dpll_pin_on_dpll_priv(struct dpll_device *dpll,
791 struct dpll_pin *pin)
793 struct dpll_pin_registration *reg;
794 struct dpll_pin_ref *ref;
796 ref = xa_load(&dpll->pin_refs, pin->pin_idx);
799 reg = dpll_pin_registration_first(ref);
803 void *dpll_pin_on_pin_priv(struct dpll_pin *parent,
804 struct dpll_pin *pin)
806 struct dpll_pin_registration *reg;
807 struct dpll_pin_ref *ref;
809 ref = xa_load(&pin->parent_refs, parent->pin_idx);
812 reg = dpll_pin_registration_first(ref);
816 const struct dpll_pin_ops *dpll_pin_ops(struct dpll_pin_ref *ref)
818 struct dpll_pin_registration *reg;
820 reg = dpll_pin_registration_first(ref);
824 static int __init dpll_init(void)
828 ret = genl_register_family(&dpll_nl_family);
835 mutex_destroy(&dpll_lock);
839 static void __exit dpll_exit(void)
841 genl_unregister_family(&dpll_nl_family);
842 mutex_destroy(&dpll_lock);
845 subsys_initcall(dpll_init);
846 module_exit(dpll_exit);