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))
31 #define ASSERT_PIN_REGISTERED(p) \
32 WARN_ON_ONCE(!xa_get_mark(&dpll_pin_xa, (p)->id, DPLL_REGISTERED))
34 struct dpll_device_registration {
35 struct list_head list;
36 const struct dpll_device_ops *ops;
40 struct dpll_pin_registration {
41 struct list_head list;
42 const struct dpll_pin_ops *ops;
46 struct dpll_device *dpll_device_get_by_id(int id)
48 if (xa_get_mark(&dpll_device_xa, id, DPLL_REGISTERED))
49 return xa_load(&dpll_device_xa, id);
54 static struct dpll_pin_registration *
55 dpll_pin_registration_find(struct dpll_pin_ref *ref,
56 const struct dpll_pin_ops *ops, void *priv)
58 struct dpll_pin_registration *reg;
60 list_for_each_entry(reg, &ref->registration_list, list) {
61 if (reg->ops == ops && reg->priv == priv)
68 dpll_xa_ref_pin_add(struct xarray *xa_pins, struct dpll_pin *pin,
69 const struct dpll_pin_ops *ops, void *priv)
71 struct dpll_pin_registration *reg;
72 struct dpll_pin_ref *ref;
73 bool ref_exists = false;
77 xa_for_each(xa_pins, i, ref) {
80 reg = dpll_pin_registration_find(ref, ops, priv);
82 refcount_inc(&ref->refcount);
90 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
94 INIT_LIST_HEAD(&ref->registration_list);
95 ret = xa_insert(xa_pins, pin->pin_idx, ref, GFP_KERNEL);
100 refcount_set(&ref->refcount, 1);
103 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
106 xa_erase(xa_pins, pin->pin_idx);
114 refcount_inc(&ref->refcount);
115 list_add_tail(®->list, &ref->registration_list);
120 static int dpll_xa_ref_pin_del(struct xarray *xa_pins, struct dpll_pin *pin,
121 const struct dpll_pin_ops *ops, void *priv)
123 struct dpll_pin_registration *reg;
124 struct dpll_pin_ref *ref;
127 xa_for_each(xa_pins, i, ref) {
130 reg = dpll_pin_registration_find(ref, ops, priv);
133 if (refcount_dec_and_test(&ref->refcount)) {
134 list_del(®->list);
136 xa_erase(xa_pins, i);
137 WARN_ON(!list_empty(&ref->registration_list));
147 dpll_xa_ref_dpll_add(struct xarray *xa_dplls, struct dpll_device *dpll,
148 const struct dpll_pin_ops *ops, void *priv)
150 struct dpll_pin_registration *reg;
151 struct dpll_pin_ref *ref;
152 bool ref_exists = false;
156 xa_for_each(xa_dplls, i, ref) {
157 if (ref->dpll != dpll)
159 reg = dpll_pin_registration_find(ref, ops, priv);
161 refcount_inc(&ref->refcount);
169 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
173 INIT_LIST_HEAD(&ref->registration_list);
174 ret = xa_insert(xa_dplls, dpll->id, ref, GFP_KERNEL);
179 refcount_set(&ref->refcount, 1);
182 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
185 xa_erase(xa_dplls, dpll->id);
193 refcount_inc(&ref->refcount);
194 list_add_tail(®->list, &ref->registration_list);
200 dpll_xa_ref_dpll_del(struct xarray *xa_dplls, struct dpll_device *dpll,
201 const struct dpll_pin_ops *ops, void *priv)
203 struct dpll_pin_registration *reg;
204 struct dpll_pin_ref *ref;
207 xa_for_each(xa_dplls, i, ref) {
208 if (ref->dpll != dpll)
210 reg = dpll_pin_registration_find(ref, ops, priv);
213 if (refcount_dec_and_test(&ref->refcount)) {
214 list_del(®->list);
216 xa_erase(xa_dplls, i);
217 WARN_ON(!list_empty(&ref->registration_list));
224 struct dpll_pin_ref *dpll_xa_ref_dpll_first(struct xarray *xa_refs)
226 struct dpll_pin_ref *ref;
229 ref = xa_find(xa_refs, &i, ULONG_MAX, XA_PRESENT);
234 static struct dpll_device *
235 dpll_device_alloc(const u64 clock_id, u32 device_idx, struct module *module)
237 struct dpll_device *dpll;
240 dpll = kzalloc(sizeof(*dpll), GFP_KERNEL);
242 return ERR_PTR(-ENOMEM);
243 refcount_set(&dpll->refcount, 1);
244 INIT_LIST_HEAD(&dpll->registration_list);
245 dpll->device_idx = device_idx;
246 dpll->clock_id = clock_id;
247 dpll->module = module;
248 ret = xa_alloc_cyclic(&dpll_device_xa, &dpll->id, dpll, xa_limit_32b,
249 &dpll_xa_id, GFP_KERNEL);
254 xa_init_flags(&dpll->pin_refs, XA_FLAGS_ALLOC);
260 * dpll_device_get - find existing or create new dpll device
261 * @clock_id: clock_id of creator
262 * @device_idx: idx given by device driver
263 * @module: reference to registering module
265 * Get existing object of a dpll device, unique for given arguments.
266 * Create new if doesn't exist yet.
268 * Context: Acquires a lock (dpll_lock)
270 * * valid dpll_device struct pointer if succeeded
271 * * ERR_PTR(X) - error
274 dpll_device_get(u64 clock_id, u32 device_idx, struct module *module)
276 struct dpll_device *dpll, *ret = NULL;
279 mutex_lock(&dpll_lock);
280 xa_for_each(&dpll_device_xa, index, dpll) {
281 if (dpll->clock_id == clock_id &&
282 dpll->device_idx == device_idx &&
283 dpll->module == module) {
285 refcount_inc(&ret->refcount);
290 ret = dpll_device_alloc(clock_id, device_idx, module);
291 mutex_unlock(&dpll_lock);
295 EXPORT_SYMBOL_GPL(dpll_device_get);
298 * dpll_device_put - decrease the refcount and free memory if possible
299 * @dpll: dpll_device struct pointer
301 * Context: Acquires a lock (dpll_lock)
302 * Drop reference for a dpll device, if all references are gone, delete
303 * dpll device object.
305 void dpll_device_put(struct dpll_device *dpll)
307 mutex_lock(&dpll_lock);
308 if (refcount_dec_and_test(&dpll->refcount)) {
309 ASSERT_DPLL_NOT_REGISTERED(dpll);
310 WARN_ON_ONCE(!xa_empty(&dpll->pin_refs));
311 xa_destroy(&dpll->pin_refs);
312 xa_erase(&dpll_device_xa, dpll->id);
313 WARN_ON(!list_empty(&dpll->registration_list));
316 mutex_unlock(&dpll_lock);
318 EXPORT_SYMBOL_GPL(dpll_device_put);
320 static struct dpll_device_registration *
321 dpll_device_registration_find(struct dpll_device *dpll,
322 const struct dpll_device_ops *ops, void *priv)
324 struct dpll_device_registration *reg;
326 list_for_each_entry(reg, &dpll->registration_list, list) {
327 if (reg->ops == ops && reg->priv == priv)
334 * dpll_device_register - register the dpll device in the subsystem
335 * @dpll: pointer to a dpll
336 * @type: type of a dpll
337 * @ops: ops for a dpll device
338 * @priv: pointer to private information of owner
340 * Make dpll device available for user space.
342 * Context: Acquires a lock (dpll_lock)
345 * * negative - error value
347 int dpll_device_register(struct dpll_device *dpll, enum dpll_type type,
348 const struct dpll_device_ops *ops, void *priv)
350 struct dpll_device_registration *reg;
351 bool first_registration = false;
355 if (WARN_ON(!ops->mode_get))
357 if (WARN_ON(!ops->lock_status_get))
359 if (WARN_ON(type < DPLL_TYPE_PPS || type > DPLL_TYPE_MAX))
362 mutex_lock(&dpll_lock);
363 reg = dpll_device_registration_find(dpll, ops, priv);
365 mutex_unlock(&dpll_lock);
369 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
371 mutex_unlock(&dpll_lock);
377 first_registration = list_empty(&dpll->registration_list);
378 list_add_tail(®->list, &dpll->registration_list);
379 if (!first_registration) {
380 mutex_unlock(&dpll_lock);
384 xa_set_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
385 dpll_device_create_ntf(dpll);
386 mutex_unlock(&dpll_lock);
390 EXPORT_SYMBOL_GPL(dpll_device_register);
393 * dpll_device_unregister - unregister dpll device
394 * @dpll: registered dpll pointer
395 * @ops: ops for a dpll device
396 * @priv: pointer to private information of owner
398 * Unregister device, make it unavailable for userspace.
399 * Note: It does not free the memory
400 * Context: Acquires a lock (dpll_lock)
402 void dpll_device_unregister(struct dpll_device *dpll,
403 const struct dpll_device_ops *ops, void *priv)
405 struct dpll_device_registration *reg;
407 mutex_lock(&dpll_lock);
408 ASSERT_DPLL_REGISTERED(dpll);
409 dpll_device_delete_ntf(dpll);
410 reg = dpll_device_registration_find(dpll, ops, priv);
412 mutex_unlock(&dpll_lock);
415 list_del(®->list);
418 if (!list_empty(&dpll->registration_list)) {
419 mutex_unlock(&dpll_lock);
422 xa_clear_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
423 mutex_unlock(&dpll_lock);
425 EXPORT_SYMBOL_GPL(dpll_device_unregister);
427 static struct dpll_pin *
428 dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,
429 const struct dpll_pin_properties *prop)
431 struct dpll_pin *pin;
434 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
436 return ERR_PTR(-ENOMEM);
437 pin->pin_idx = pin_idx;
438 pin->clock_id = clock_id;
439 pin->module = module;
440 if (WARN_ON(prop->type < DPLL_PIN_TYPE_MUX ||
441 prop->type > DPLL_PIN_TYPE_MAX)) {
446 refcount_set(&pin->refcount, 1);
447 xa_init_flags(&pin->dpll_refs, XA_FLAGS_ALLOC);
448 xa_init_flags(&pin->parent_refs, XA_FLAGS_ALLOC);
449 ret = xa_alloc(&dpll_pin_xa, &pin->id, pin, xa_limit_16b, GFP_KERNEL);
454 xa_destroy(&pin->dpll_refs);
455 xa_destroy(&pin->parent_refs);
461 * dpll_pin_get - find existing or create new dpll pin
462 * @clock_id: clock_id of creator
463 * @pin_idx: idx given by dev driver
464 * @module: reference to registering module
465 * @prop: dpll pin properties
467 * Get existing object of a pin (unique for given arguments) or create new
468 * if doesn't exist yet.
470 * Context: Acquires a lock (dpll_lock)
472 * * valid allocated dpll_pin struct pointer if succeeded
473 * * ERR_PTR(X) - error
476 dpll_pin_get(u64 clock_id, u32 pin_idx, struct module *module,
477 const struct dpll_pin_properties *prop)
479 struct dpll_pin *pos, *ret = NULL;
482 mutex_lock(&dpll_lock);
483 xa_for_each(&dpll_pin_xa, i, pos) {
484 if (pos->clock_id == clock_id &&
485 pos->pin_idx == pin_idx &&
486 pos->module == module) {
488 refcount_inc(&ret->refcount);
493 ret = dpll_pin_alloc(clock_id, pin_idx, module, prop);
494 mutex_unlock(&dpll_lock);
498 EXPORT_SYMBOL_GPL(dpll_pin_get);
501 * dpll_pin_put - decrease the refcount and free memory if possible
502 * @pin: pointer to a pin to be put
504 * Drop reference for a pin, if all references are gone, delete pin object.
506 * Context: Acquires a lock (dpll_lock)
508 void dpll_pin_put(struct dpll_pin *pin)
510 mutex_lock(&dpll_lock);
511 if (refcount_dec_and_test(&pin->refcount)) {
512 xa_destroy(&pin->dpll_refs);
513 xa_destroy(&pin->parent_refs);
514 xa_erase(&dpll_pin_xa, pin->id);
517 mutex_unlock(&dpll_lock);
519 EXPORT_SYMBOL_GPL(dpll_pin_put);
522 __dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
523 const struct dpll_pin_ops *ops, void *priv)
527 ret = dpll_xa_ref_pin_add(&dpll->pin_refs, pin, ops, priv);
530 ret = dpll_xa_ref_dpll_add(&pin->dpll_refs, dpll, ops, priv);
533 xa_set_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
534 dpll_pin_create_ntf(pin);
539 dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
544 * dpll_pin_register - register the dpll pin in the subsystem
545 * @dpll: pointer to a dpll
546 * @pin: pointer to a dpll pin
547 * @ops: ops for a dpll pin ops
548 * @priv: pointer to private information of owner
550 * Context: Acquires a lock (dpll_lock)
553 * * negative - error value
556 dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
557 const struct dpll_pin_ops *ops, void *priv)
562 WARN_ON(!ops->state_on_dpll_get) ||
563 WARN_ON(!ops->direction_get))
565 if (ASSERT_DPLL_REGISTERED(dpll))
568 mutex_lock(&dpll_lock);
569 if (WARN_ON(!(dpll->module == pin->module &&
570 dpll->clock_id == pin->clock_id)))
573 ret = __dpll_pin_register(dpll, pin, ops, priv);
574 mutex_unlock(&dpll_lock);
578 EXPORT_SYMBOL_GPL(dpll_pin_register);
581 __dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
582 const struct dpll_pin_ops *ops, void *priv)
584 dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
585 dpll_xa_ref_dpll_del(&pin->dpll_refs, dpll, ops, priv);
586 if (xa_empty(&pin->dpll_refs))
587 xa_clear_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
591 * dpll_pin_unregister - unregister dpll pin from dpll device
592 * @dpll: registered dpll pointer
593 * @pin: pointer to a pin
594 * @ops: ops for a dpll pin
595 * @priv: pointer to private information of owner
597 * Note: It does not free the memory
598 * Context: Acquires a lock (dpll_lock)
600 void dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
601 const struct dpll_pin_ops *ops, void *priv)
603 if (WARN_ON(xa_empty(&dpll->pin_refs)))
605 if (WARN_ON(!xa_empty(&pin->parent_refs)))
608 mutex_lock(&dpll_lock);
609 dpll_pin_delete_ntf(pin);
610 __dpll_pin_unregister(dpll, pin, ops, priv);
611 mutex_unlock(&dpll_lock);
613 EXPORT_SYMBOL_GPL(dpll_pin_unregister);
616 * dpll_pin_on_pin_register - register a pin with a parent pin
617 * @parent: pointer to a parent pin
618 * @pin: pointer to a pin
619 * @ops: ops for a dpll pin
620 * @priv: pointer to private information of owner
622 * Register a pin with a parent pin, create references between them and
623 * between newly registered pin and dplls connected with a parent pin.
625 * Context: Acquires a lock (dpll_lock)
628 * * negative - error value
630 int dpll_pin_on_pin_register(struct dpll_pin *parent, struct dpll_pin *pin,
631 const struct dpll_pin_ops *ops, void *priv)
633 struct dpll_pin_ref *ref;
634 unsigned long i, stop;
637 if (WARN_ON(parent->prop->type != DPLL_PIN_TYPE_MUX))
641 WARN_ON(!ops->state_on_pin_get) ||
642 WARN_ON(!ops->direction_get))
644 if (ASSERT_PIN_REGISTERED(parent))
647 mutex_lock(&dpll_lock);
648 ret = dpll_xa_ref_pin_add(&pin->parent_refs, parent, ops, priv);
651 refcount_inc(&pin->refcount);
652 xa_for_each(&parent->dpll_refs, i, ref) {
653 ret = __dpll_pin_register(ref->dpll, pin, ops, priv);
656 goto dpll_unregister;
658 dpll_pin_create_ntf(pin);
660 mutex_unlock(&dpll_lock);
665 xa_for_each(&parent->dpll_refs, i, ref)
667 __dpll_pin_unregister(ref->dpll, pin, ops, priv);
668 dpll_pin_delete_ntf(pin);
670 refcount_dec(&pin->refcount);
671 dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
673 mutex_unlock(&dpll_lock);
676 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_register);
679 * dpll_pin_on_pin_unregister - unregister dpll pin from a parent pin
680 * @parent: pointer to a parent pin
681 * @pin: pointer to a pin
682 * @ops: ops for a dpll pin
683 * @priv: pointer to private information of owner
685 * Context: Acquires a lock (dpll_lock)
686 * Note: It does not free the memory
688 void dpll_pin_on_pin_unregister(struct dpll_pin *parent, struct dpll_pin *pin,
689 const struct dpll_pin_ops *ops, void *priv)
691 struct dpll_pin_ref *ref;
694 mutex_lock(&dpll_lock);
695 dpll_pin_delete_ntf(pin);
696 dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
697 refcount_dec(&pin->refcount);
698 xa_for_each(&pin->dpll_refs, i, ref)
699 __dpll_pin_unregister(ref->dpll, pin, ops, priv);
700 mutex_unlock(&dpll_lock);
702 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_unregister);
704 static struct dpll_device_registration *
705 dpll_device_registration_first(struct dpll_device *dpll)
707 struct dpll_device_registration *reg;
709 reg = list_first_entry_or_null((struct list_head *)&dpll->registration_list,
710 struct dpll_device_registration, list);
715 void *dpll_priv(struct dpll_device *dpll)
717 struct dpll_device_registration *reg;
719 reg = dpll_device_registration_first(dpll);
723 const struct dpll_device_ops *dpll_device_ops(struct dpll_device *dpll)
725 struct dpll_device_registration *reg;
727 reg = dpll_device_registration_first(dpll);
731 static struct dpll_pin_registration *
732 dpll_pin_registration_first(struct dpll_pin_ref *ref)
734 struct dpll_pin_registration *reg;
736 reg = list_first_entry_or_null(&ref->registration_list,
737 struct dpll_pin_registration, list);
742 void *dpll_pin_on_dpll_priv(struct dpll_device *dpll,
743 struct dpll_pin *pin)
745 struct dpll_pin_registration *reg;
746 struct dpll_pin_ref *ref;
748 ref = xa_load(&dpll->pin_refs, pin->pin_idx);
751 reg = dpll_pin_registration_first(ref);
755 void *dpll_pin_on_pin_priv(struct dpll_pin *parent,
756 struct dpll_pin *pin)
758 struct dpll_pin_registration *reg;
759 struct dpll_pin_ref *ref;
761 ref = xa_load(&pin->parent_refs, parent->pin_idx);
764 reg = dpll_pin_registration_first(ref);
768 const struct dpll_pin_ops *dpll_pin_ops(struct dpll_pin_ref *ref)
770 struct dpll_pin_registration *reg;
772 reg = dpll_pin_registration_first(ref);
776 static int __init dpll_init(void)
780 ret = genl_register_family(&dpll_nl_family);
787 mutex_destroy(&dpll_lock);
791 static void __exit dpll_exit(void)
793 genl_unregister_family(&dpll_nl_family);
794 mutex_destroy(&dpll_lock);
797 subsys_initcall(dpll_init);
798 module_exit(dpll_exit);