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_pin *netdev_dpll_pin(const struct net_device *dev)
47 return rcu_dereference_rtnl(dev->dpll_pin);
50 struct dpll_device *dpll_device_get_by_id(int id)
52 if (xa_get_mark(&dpll_device_xa, id, DPLL_REGISTERED))
53 return xa_load(&dpll_device_xa, id);
58 static struct dpll_pin_registration *
59 dpll_pin_registration_find(struct dpll_pin_ref *ref,
60 const struct dpll_pin_ops *ops, void *priv)
62 struct dpll_pin_registration *reg;
64 list_for_each_entry(reg, &ref->registration_list, list) {
65 if (reg->ops == ops && reg->priv == priv)
72 dpll_xa_ref_pin_add(struct xarray *xa_pins, struct dpll_pin *pin,
73 const struct dpll_pin_ops *ops, void *priv)
75 struct dpll_pin_registration *reg;
76 struct dpll_pin_ref *ref;
77 bool ref_exists = false;
81 xa_for_each(xa_pins, i, ref) {
84 reg = dpll_pin_registration_find(ref, ops, priv);
86 refcount_inc(&ref->refcount);
94 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
98 INIT_LIST_HEAD(&ref->registration_list);
99 ret = xa_insert(xa_pins, pin->pin_idx, ref, GFP_KERNEL);
104 refcount_set(&ref->refcount, 1);
107 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
110 xa_erase(xa_pins, pin->pin_idx);
118 refcount_inc(&ref->refcount);
119 list_add_tail(®->list, &ref->registration_list);
124 static int dpll_xa_ref_pin_del(struct xarray *xa_pins, struct dpll_pin *pin,
125 const struct dpll_pin_ops *ops, void *priv)
127 struct dpll_pin_registration *reg;
128 struct dpll_pin_ref *ref;
131 xa_for_each(xa_pins, i, ref) {
134 reg = dpll_pin_registration_find(ref, ops, priv);
137 if (refcount_dec_and_test(&ref->refcount)) {
138 list_del(®->list);
140 xa_erase(xa_pins, i);
141 WARN_ON(!list_empty(&ref->registration_list));
151 dpll_xa_ref_dpll_add(struct xarray *xa_dplls, struct dpll_device *dpll,
152 const struct dpll_pin_ops *ops, void *priv)
154 struct dpll_pin_registration *reg;
155 struct dpll_pin_ref *ref;
156 bool ref_exists = false;
160 xa_for_each(xa_dplls, i, ref) {
161 if (ref->dpll != dpll)
163 reg = dpll_pin_registration_find(ref, ops, priv);
165 refcount_inc(&ref->refcount);
173 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
177 INIT_LIST_HEAD(&ref->registration_list);
178 ret = xa_insert(xa_dplls, dpll->id, ref, GFP_KERNEL);
183 refcount_set(&ref->refcount, 1);
186 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
189 xa_erase(xa_dplls, dpll->id);
197 refcount_inc(&ref->refcount);
198 list_add_tail(®->list, &ref->registration_list);
204 dpll_xa_ref_dpll_del(struct xarray *xa_dplls, struct dpll_device *dpll,
205 const struct dpll_pin_ops *ops, void *priv)
207 struct dpll_pin_registration *reg;
208 struct dpll_pin_ref *ref;
211 xa_for_each(xa_dplls, i, ref) {
212 if (ref->dpll != dpll)
214 reg = dpll_pin_registration_find(ref, ops, priv);
217 if (refcount_dec_and_test(&ref->refcount)) {
218 list_del(®->list);
220 xa_erase(xa_dplls, i);
221 WARN_ON(!list_empty(&ref->registration_list));
228 struct dpll_pin_ref *dpll_xa_ref_dpll_first(struct xarray *xa_refs)
230 struct dpll_pin_ref *ref;
233 ref = xa_find(xa_refs, &i, ULONG_MAX, XA_PRESENT);
238 static struct dpll_device *
239 dpll_device_alloc(const u64 clock_id, u32 device_idx, struct module *module)
241 struct dpll_device *dpll;
244 dpll = kzalloc(sizeof(*dpll), GFP_KERNEL);
246 return ERR_PTR(-ENOMEM);
247 refcount_set(&dpll->refcount, 1);
248 INIT_LIST_HEAD(&dpll->registration_list);
249 dpll->device_idx = device_idx;
250 dpll->clock_id = clock_id;
251 dpll->module = module;
252 ret = xa_alloc_cyclic(&dpll_device_xa, &dpll->id, dpll, xa_limit_32b,
253 &dpll_device_xa_id, GFP_KERNEL);
258 xa_init_flags(&dpll->pin_refs, XA_FLAGS_ALLOC);
264 * dpll_device_get - find existing or create new dpll device
265 * @clock_id: clock_id of creator
266 * @device_idx: idx given by device driver
267 * @module: reference to registering module
269 * Get existing object of a dpll device, unique for given arguments.
270 * Create new if doesn't exist yet.
272 * Context: Acquires a lock (dpll_lock)
274 * * valid dpll_device struct pointer if succeeded
275 * * ERR_PTR(X) - error
278 dpll_device_get(u64 clock_id, u32 device_idx, struct module *module)
280 struct dpll_device *dpll, *ret = NULL;
283 mutex_lock(&dpll_lock);
284 xa_for_each(&dpll_device_xa, index, dpll) {
285 if (dpll->clock_id == clock_id &&
286 dpll->device_idx == device_idx &&
287 dpll->module == module) {
289 refcount_inc(&ret->refcount);
294 ret = dpll_device_alloc(clock_id, device_idx, module);
295 mutex_unlock(&dpll_lock);
299 EXPORT_SYMBOL_GPL(dpll_device_get);
302 * dpll_device_put - decrease the refcount and free memory if possible
303 * @dpll: dpll_device struct pointer
305 * Context: Acquires a lock (dpll_lock)
306 * Drop reference for a dpll device, if all references are gone, delete
307 * dpll device object.
309 void dpll_device_put(struct dpll_device *dpll)
311 mutex_lock(&dpll_lock);
312 if (refcount_dec_and_test(&dpll->refcount)) {
313 ASSERT_DPLL_NOT_REGISTERED(dpll);
314 WARN_ON_ONCE(!xa_empty(&dpll->pin_refs));
315 xa_destroy(&dpll->pin_refs);
316 xa_erase(&dpll_device_xa, dpll->id);
317 WARN_ON(!list_empty(&dpll->registration_list));
320 mutex_unlock(&dpll_lock);
322 EXPORT_SYMBOL_GPL(dpll_device_put);
324 static struct dpll_device_registration *
325 dpll_device_registration_find(struct dpll_device *dpll,
326 const struct dpll_device_ops *ops, void *priv)
328 struct dpll_device_registration *reg;
330 list_for_each_entry(reg, &dpll->registration_list, list) {
331 if (reg->ops == ops && reg->priv == priv)
338 * dpll_device_register - register the dpll device in the subsystem
339 * @dpll: pointer to a dpll
340 * @type: type of a dpll
341 * @ops: ops for a dpll device
342 * @priv: pointer to private information of owner
344 * Make dpll device available for user space.
346 * Context: Acquires a lock (dpll_lock)
349 * * negative - error value
351 int dpll_device_register(struct dpll_device *dpll, enum dpll_type type,
352 const struct dpll_device_ops *ops, void *priv)
354 struct dpll_device_registration *reg;
355 bool first_registration = false;
359 if (WARN_ON(!ops->mode_get))
361 if (WARN_ON(!ops->lock_status_get))
363 if (WARN_ON(type < DPLL_TYPE_PPS || type > DPLL_TYPE_MAX))
366 mutex_lock(&dpll_lock);
367 reg = dpll_device_registration_find(dpll, ops, priv);
369 mutex_unlock(&dpll_lock);
373 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
375 mutex_unlock(&dpll_lock);
381 first_registration = list_empty(&dpll->registration_list);
382 list_add_tail(®->list, &dpll->registration_list);
383 if (!first_registration) {
384 mutex_unlock(&dpll_lock);
388 xa_set_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
389 dpll_device_create_ntf(dpll);
390 mutex_unlock(&dpll_lock);
394 EXPORT_SYMBOL_GPL(dpll_device_register);
397 * dpll_device_unregister - unregister dpll device
398 * @dpll: registered dpll pointer
399 * @ops: ops for a dpll device
400 * @priv: pointer to private information of owner
402 * Unregister device, make it unavailable for userspace.
403 * Note: It does not free the memory
404 * Context: Acquires a lock (dpll_lock)
406 void dpll_device_unregister(struct dpll_device *dpll,
407 const struct dpll_device_ops *ops, void *priv)
409 struct dpll_device_registration *reg;
411 mutex_lock(&dpll_lock);
412 ASSERT_DPLL_REGISTERED(dpll);
413 dpll_device_delete_ntf(dpll);
414 reg = dpll_device_registration_find(dpll, ops, priv);
416 mutex_unlock(&dpll_lock);
419 list_del(®->list);
422 if (!list_empty(&dpll->registration_list)) {
423 mutex_unlock(&dpll_lock);
426 xa_clear_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
427 mutex_unlock(&dpll_lock);
429 EXPORT_SYMBOL_GPL(dpll_device_unregister);
431 static void dpll_pin_prop_free(struct dpll_pin_properties *prop)
433 kfree(prop->package_label);
434 kfree(prop->panel_label);
435 kfree(prop->board_label);
436 kfree(prop->freq_supported);
439 static int dpll_pin_prop_dup(const struct dpll_pin_properties *src,
440 struct dpll_pin_properties *dst)
442 memcpy(dst, src, sizeof(*dst));
443 if (src->freq_supported && src->freq_supported_num) {
444 size_t freq_size = src->freq_supported_num *
445 sizeof(*src->freq_supported);
446 dst->freq_supported = kmemdup(src->freq_supported,
447 freq_size, GFP_KERNEL);
448 if (!src->freq_supported)
451 if (src->board_label) {
452 dst->board_label = kstrdup(src->board_label, GFP_KERNEL);
453 if (!dst->board_label)
454 goto err_board_label;
456 if (src->panel_label) {
457 dst->panel_label = kstrdup(src->panel_label, GFP_KERNEL);
458 if (!dst->panel_label)
459 goto err_panel_label;
461 if (src->package_label) {
462 dst->package_label = kstrdup(src->package_label, GFP_KERNEL);
463 if (!dst->package_label)
464 goto err_package_label;
470 kfree(dst->panel_label);
472 kfree(dst->board_label);
474 kfree(dst->freq_supported);
478 static struct dpll_pin *
479 dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,
480 const struct dpll_pin_properties *prop)
482 struct dpll_pin *pin;
485 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
487 return ERR_PTR(-ENOMEM);
488 pin->pin_idx = pin_idx;
489 pin->clock_id = clock_id;
490 pin->module = module;
491 if (WARN_ON(prop->type < DPLL_PIN_TYPE_MUX ||
492 prop->type > DPLL_PIN_TYPE_MAX)) {
496 ret = dpll_pin_prop_dup(prop, &pin->prop);
499 refcount_set(&pin->refcount, 1);
500 xa_init_flags(&pin->dpll_refs, XA_FLAGS_ALLOC);
501 xa_init_flags(&pin->parent_refs, XA_FLAGS_ALLOC);
502 ret = xa_alloc_cyclic(&dpll_pin_xa, &pin->id, pin, xa_limit_32b,
503 &dpll_pin_xa_id, GFP_KERNEL);
508 xa_destroy(&pin->dpll_refs);
509 xa_destroy(&pin->parent_refs);
510 dpll_pin_prop_free(&pin->prop);
517 * dpll_pin_get - find existing or create new dpll pin
518 * @clock_id: clock_id of creator
519 * @pin_idx: idx given by dev driver
520 * @module: reference to registering module
521 * @prop: dpll pin properties
523 * Get existing object of a pin (unique for given arguments) or create new
524 * if doesn't exist yet.
526 * Context: Acquires a lock (dpll_lock)
528 * * valid allocated dpll_pin struct pointer if succeeded
529 * * ERR_PTR(X) - error
532 dpll_pin_get(u64 clock_id, u32 pin_idx, struct module *module,
533 const struct dpll_pin_properties *prop)
535 struct dpll_pin *pos, *ret = NULL;
538 mutex_lock(&dpll_lock);
539 xa_for_each(&dpll_pin_xa, i, pos) {
540 if (pos->clock_id == clock_id &&
541 pos->pin_idx == pin_idx &&
542 pos->module == module) {
544 refcount_inc(&ret->refcount);
549 ret = dpll_pin_alloc(clock_id, pin_idx, module, prop);
550 mutex_unlock(&dpll_lock);
554 EXPORT_SYMBOL_GPL(dpll_pin_get);
557 * dpll_pin_put - decrease the refcount and free memory if possible
558 * @pin: pointer to a pin to be put
560 * Drop reference for a pin, if all references are gone, delete pin object.
562 * Context: Acquires a lock (dpll_lock)
564 void dpll_pin_put(struct dpll_pin *pin)
566 mutex_lock(&dpll_lock);
567 if (refcount_dec_and_test(&pin->refcount)) {
568 xa_destroy(&pin->dpll_refs);
569 xa_destroy(&pin->parent_refs);
570 xa_erase(&dpll_pin_xa, pin->id);
571 dpll_pin_prop_free(&pin->prop);
574 mutex_unlock(&dpll_lock);
576 EXPORT_SYMBOL_GPL(dpll_pin_put);
579 __dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
580 const struct dpll_pin_ops *ops, void *priv)
584 ret = dpll_xa_ref_pin_add(&dpll->pin_refs, pin, ops, priv);
587 ret = dpll_xa_ref_dpll_add(&pin->dpll_refs, dpll, ops, priv);
590 xa_set_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
591 dpll_pin_create_ntf(pin);
596 dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
601 * dpll_pin_register - register the dpll pin in the subsystem
602 * @dpll: pointer to a dpll
603 * @pin: pointer to a dpll pin
604 * @ops: ops for a dpll pin ops
605 * @priv: pointer to private information of owner
607 * Context: Acquires a lock (dpll_lock)
610 * * negative - error value
613 dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
614 const struct dpll_pin_ops *ops, void *priv)
619 WARN_ON(!ops->state_on_dpll_get) ||
620 WARN_ON(!ops->direction_get))
623 mutex_lock(&dpll_lock);
624 if (WARN_ON(!(dpll->module == pin->module &&
625 dpll->clock_id == pin->clock_id)))
628 ret = __dpll_pin_register(dpll, pin, ops, priv);
629 mutex_unlock(&dpll_lock);
633 EXPORT_SYMBOL_GPL(dpll_pin_register);
636 __dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
637 const struct dpll_pin_ops *ops, void *priv)
639 dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
640 dpll_xa_ref_dpll_del(&pin->dpll_refs, dpll, ops, priv);
641 if (xa_empty(&pin->dpll_refs))
642 xa_clear_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
646 * dpll_pin_unregister - unregister dpll pin from dpll device
647 * @dpll: registered dpll pointer
648 * @pin: pointer to a pin
649 * @ops: ops for a dpll pin
650 * @priv: pointer to private information of owner
652 * Note: It does not free the memory
653 * Context: Acquires a lock (dpll_lock)
655 void dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
656 const struct dpll_pin_ops *ops, void *priv)
658 if (WARN_ON(xa_empty(&dpll->pin_refs)))
660 if (WARN_ON(!xa_empty(&pin->parent_refs)))
663 mutex_lock(&dpll_lock);
664 dpll_pin_delete_ntf(pin);
665 __dpll_pin_unregister(dpll, pin, ops, priv);
666 mutex_unlock(&dpll_lock);
668 EXPORT_SYMBOL_GPL(dpll_pin_unregister);
671 * dpll_pin_on_pin_register - register a pin with a parent pin
672 * @parent: pointer to a parent pin
673 * @pin: pointer to a pin
674 * @ops: ops for a dpll pin
675 * @priv: pointer to private information of owner
677 * Register a pin with a parent pin, create references between them and
678 * between newly registered pin and dplls connected with a parent pin.
680 * Context: Acquires a lock (dpll_lock)
683 * * negative - error value
685 int dpll_pin_on_pin_register(struct dpll_pin *parent, struct dpll_pin *pin,
686 const struct dpll_pin_ops *ops, void *priv)
688 struct dpll_pin_ref *ref;
689 unsigned long i, stop;
692 if (WARN_ON(parent->prop.type != DPLL_PIN_TYPE_MUX))
696 WARN_ON(!ops->state_on_pin_get) ||
697 WARN_ON(!ops->direction_get))
700 mutex_lock(&dpll_lock);
701 ret = dpll_xa_ref_pin_add(&pin->parent_refs, parent, ops, priv);
704 refcount_inc(&pin->refcount);
705 xa_for_each(&parent->dpll_refs, i, ref) {
706 ret = __dpll_pin_register(ref->dpll, pin, ops, priv);
709 goto dpll_unregister;
711 dpll_pin_create_ntf(pin);
713 mutex_unlock(&dpll_lock);
718 xa_for_each(&parent->dpll_refs, i, ref)
720 __dpll_pin_unregister(ref->dpll, pin, ops, priv);
721 dpll_pin_delete_ntf(pin);
723 refcount_dec(&pin->refcount);
724 dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
726 mutex_unlock(&dpll_lock);
729 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_register);
732 * dpll_pin_on_pin_unregister - unregister dpll pin from a parent pin
733 * @parent: pointer to a parent pin
734 * @pin: pointer to a pin
735 * @ops: ops for a dpll pin
736 * @priv: pointer to private information of owner
738 * Context: Acquires a lock (dpll_lock)
739 * Note: It does not free the memory
741 void dpll_pin_on_pin_unregister(struct dpll_pin *parent, struct dpll_pin *pin,
742 const struct dpll_pin_ops *ops, void *priv)
744 struct dpll_pin_ref *ref;
747 mutex_lock(&dpll_lock);
748 dpll_pin_delete_ntf(pin);
749 dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
750 refcount_dec(&pin->refcount);
751 xa_for_each(&pin->dpll_refs, i, ref)
752 __dpll_pin_unregister(ref->dpll, pin, ops, priv);
753 mutex_unlock(&dpll_lock);
755 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_unregister);
757 static struct dpll_device_registration *
758 dpll_device_registration_first(struct dpll_device *dpll)
760 struct dpll_device_registration *reg;
762 reg = list_first_entry_or_null((struct list_head *)&dpll->registration_list,
763 struct dpll_device_registration, list);
768 void *dpll_priv(struct dpll_device *dpll)
770 struct dpll_device_registration *reg;
772 reg = dpll_device_registration_first(dpll);
776 const struct dpll_device_ops *dpll_device_ops(struct dpll_device *dpll)
778 struct dpll_device_registration *reg;
780 reg = dpll_device_registration_first(dpll);
784 static struct dpll_pin_registration *
785 dpll_pin_registration_first(struct dpll_pin_ref *ref)
787 struct dpll_pin_registration *reg;
789 reg = list_first_entry_or_null(&ref->registration_list,
790 struct dpll_pin_registration, list);
795 void *dpll_pin_on_dpll_priv(struct dpll_device *dpll,
796 struct dpll_pin *pin)
798 struct dpll_pin_registration *reg;
799 struct dpll_pin_ref *ref;
801 ref = xa_load(&dpll->pin_refs, pin->pin_idx);
804 reg = dpll_pin_registration_first(ref);
808 void *dpll_pin_on_pin_priv(struct dpll_pin *parent,
809 struct dpll_pin *pin)
811 struct dpll_pin_registration *reg;
812 struct dpll_pin_ref *ref;
814 ref = xa_load(&pin->parent_refs, parent->pin_idx);
817 reg = dpll_pin_registration_first(ref);
821 const struct dpll_pin_ops *dpll_pin_ops(struct dpll_pin_ref *ref)
823 struct dpll_pin_registration *reg;
825 reg = dpll_pin_registration_first(ref);
829 static int __init dpll_init(void)
833 ret = genl_register_family(&dpll_nl_family);
840 mutex_destroy(&dpll_lock);
844 static void __exit dpll_exit(void)
846 genl_unregister_family(&dpll_nl_family);
847 mutex_destroy(&dpll_lock);
850 subsys_initcall(dpll_init);
851 module_exit(dpll_exit);