1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * dpll_core.c - DPLL subsystem kernel-space interface implementation.
4 *
5 * Copyright (c) 2023 Meta Platforms, Inc. and affiliates
6 * Copyright (c) 2023 Intel Corporation.
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15
16 #include "dpll_core.h"
17 #include "dpll_netlink.h"
18
19 /* Mutex lock to protect DPLL subsystem devices and pins */
20 DEFINE_MUTEX(dpll_lock);
21
22 DEFINE_XARRAY_FLAGS(dpll_device_xa, XA_FLAGS_ALLOC);
23 DEFINE_XARRAY_FLAGS(dpll_pin_xa, XA_FLAGS_ALLOC);
24
25 static u32 dpll_device_xa_id;
26 static u32 dpll_pin_xa_id;
27
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))
32
33 struct dpll_device_registration {
34 struct list_head list;
35 const struct dpll_device_ops *ops;
36 void *priv;
37 };
38
39 struct dpll_pin_registration {
40 struct list_head list;
41 const struct dpll_pin_ops *ops;
42 void *priv;
43 };
44
dpll_device_get_by_id(int id)45 struct dpll_device *dpll_device_get_by_id(int id)
46 {
47 if (xa_get_mark(&dpll_device_xa, id, DPLL_REGISTERED))
48 return xa_load(&dpll_device_xa, id);
49
50 return NULL;
51 }
52
53 static struct dpll_pin_registration *
dpll_pin_registration_find(struct dpll_pin_ref * ref,const struct dpll_pin_ops * ops,void * priv)54 dpll_pin_registration_find(struct dpll_pin_ref *ref,
55 const struct dpll_pin_ops *ops, void *priv)
56 {
57 struct dpll_pin_registration *reg;
58
59 list_for_each_entry(reg, &ref->registration_list, list) {
60 if (reg->ops == ops && reg->priv == priv)
61 return reg;
62 }
63 return NULL;
64 }
65
66 static int
dpll_xa_ref_pin_add(struct xarray * xa_pins,struct dpll_pin * pin,const struct dpll_pin_ops * ops,void * priv)67 dpll_xa_ref_pin_add(struct xarray *xa_pins, struct dpll_pin *pin,
68 const struct dpll_pin_ops *ops, void *priv)
69 {
70 struct dpll_pin_registration *reg;
71 struct dpll_pin_ref *ref;
72 bool ref_exists = false;
73 unsigned long i;
74 int ret;
75
76 xa_for_each(xa_pins, i, ref) {
77 if (ref->pin != pin)
78 continue;
79 reg = dpll_pin_registration_find(ref, ops, priv);
80 if (reg) {
81 refcount_inc(&ref->refcount);
82 return 0;
83 }
84 ref_exists = true;
85 break;
86 }
87
88 if (!ref_exists) {
89 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
90 if (!ref)
91 return -ENOMEM;
92 ref->pin = pin;
93 INIT_LIST_HEAD(&ref->registration_list);
94 ret = xa_insert(xa_pins, pin->pin_idx, ref, GFP_KERNEL);
95 if (ret) {
96 kfree(ref);
97 return ret;
98 }
99 refcount_set(&ref->refcount, 1);
100 }
101
102 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
103 if (!reg) {
104 if (!ref_exists) {
105 xa_erase(xa_pins, pin->pin_idx);
106 kfree(ref);
107 }
108 return -ENOMEM;
109 }
110 reg->ops = ops;
111 reg->priv = priv;
112 if (ref_exists)
113 refcount_inc(&ref->refcount);
114 list_add_tail(®->list, &ref->registration_list);
115
116 return 0;
117 }
118
dpll_xa_ref_pin_del(struct xarray * xa_pins,struct dpll_pin * pin,const struct dpll_pin_ops * ops,void * priv)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)
121 {
122 struct dpll_pin_registration *reg;
123 struct dpll_pin_ref *ref;
124 unsigned long i;
125
126 xa_for_each(xa_pins, i, ref) {
127 if (ref->pin != pin)
128 continue;
129 reg = dpll_pin_registration_find(ref, ops, priv);
130 if (WARN_ON(!reg))
131 return -EINVAL;
132 if (refcount_dec_and_test(&ref->refcount)) {
133 list_del(®->list);
134 kfree(reg);
135 xa_erase(xa_pins, i);
136 WARN_ON(!list_empty(&ref->registration_list));
137 kfree(ref);
138 }
139 return 0;
140 }
141
142 return -EINVAL;
143 }
144
145 static int
dpll_xa_ref_dpll_add(struct xarray * xa_dplls,struct dpll_device * dpll,const struct dpll_pin_ops * ops,void * priv)146 dpll_xa_ref_dpll_add(struct xarray *xa_dplls, struct dpll_device *dpll,
147 const struct dpll_pin_ops *ops, void *priv)
148 {
149 struct dpll_pin_registration *reg;
150 struct dpll_pin_ref *ref;
151 bool ref_exists = false;
152 unsigned long i;
153 int ret;
154
155 xa_for_each(xa_dplls, i, ref) {
156 if (ref->dpll != dpll)
157 continue;
158 reg = dpll_pin_registration_find(ref, ops, priv);
159 if (reg) {
160 refcount_inc(&ref->refcount);
161 return 0;
162 }
163 ref_exists = true;
164 break;
165 }
166
167 if (!ref_exists) {
168 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
169 if (!ref)
170 return -ENOMEM;
171 ref->dpll = dpll;
172 INIT_LIST_HEAD(&ref->registration_list);
173 ret = xa_insert(xa_dplls, dpll->id, ref, GFP_KERNEL);
174 if (ret) {
175 kfree(ref);
176 return ret;
177 }
178 refcount_set(&ref->refcount, 1);
179 }
180
181 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
182 if (!reg) {
183 if (!ref_exists) {
184 xa_erase(xa_dplls, dpll->id);
185 kfree(ref);
186 }
187 return -ENOMEM;
188 }
189 reg->ops = ops;
190 reg->priv = priv;
191 if (ref_exists)
192 refcount_inc(&ref->refcount);
193 list_add_tail(®->list, &ref->registration_list);
194
195 return 0;
196 }
197
198 static void
dpll_xa_ref_dpll_del(struct xarray * xa_dplls,struct dpll_device * dpll,const struct dpll_pin_ops * ops,void * priv)199 dpll_xa_ref_dpll_del(struct xarray *xa_dplls, struct dpll_device *dpll,
200 const struct dpll_pin_ops *ops, void *priv)
201 {
202 struct dpll_pin_registration *reg;
203 struct dpll_pin_ref *ref;
204 unsigned long i;
205
206 xa_for_each(xa_dplls, i, ref) {
207 if (ref->dpll != dpll)
208 continue;
209 reg = dpll_pin_registration_find(ref, ops, priv);
210 if (WARN_ON(!reg))
211 return;
212 if (refcount_dec_and_test(&ref->refcount)) {
213 list_del(®->list);
214 kfree(reg);
215 xa_erase(xa_dplls, i);
216 WARN_ON(!list_empty(&ref->registration_list));
217 kfree(ref);
218 }
219 return;
220 }
221 }
222
dpll_xa_ref_dpll_first(struct xarray * xa_refs)223 struct dpll_pin_ref *dpll_xa_ref_dpll_first(struct xarray *xa_refs)
224 {
225 struct dpll_pin_ref *ref;
226 unsigned long i = 0;
227
228 ref = xa_find(xa_refs, &i, ULONG_MAX, XA_PRESENT);
229 WARN_ON(!ref);
230 return ref;
231 }
232
233 static struct dpll_device *
dpll_device_alloc(const u64 clock_id,u32 device_idx,struct module * module)234 dpll_device_alloc(const u64 clock_id, u32 device_idx, struct module *module)
235 {
236 struct dpll_device *dpll;
237 int ret;
238
239 dpll = kzalloc(sizeof(*dpll), GFP_KERNEL);
240 if (!dpll)
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);
249 if (ret < 0) {
250 kfree(dpll);
251 return ERR_PTR(ret);
252 }
253 xa_init_flags(&dpll->pin_refs, XA_FLAGS_ALLOC);
254
255 return dpll;
256 }
257
258 /**
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
263 *
264 * Get existing object of a dpll device, unique for given arguments.
265 * Create new if doesn't exist yet.
266 *
267 * Context: Acquires a lock (dpll_lock)
268 * Return:
269 * * valid dpll_device struct pointer if succeeded
270 * * ERR_PTR(X) - error
271 */
272 struct dpll_device *
dpll_device_get(u64 clock_id,u32 device_idx,struct module * module)273 dpll_device_get(u64 clock_id, u32 device_idx, struct module *module)
274 {
275 struct dpll_device *dpll, *ret = NULL;
276 unsigned long index;
277
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) {
283 ret = dpll;
284 refcount_inc(&ret->refcount);
285 break;
286 }
287 }
288 if (!ret)
289 ret = dpll_device_alloc(clock_id, device_idx, module);
290 mutex_unlock(&dpll_lock);
291
292 return ret;
293 }
294 EXPORT_SYMBOL_GPL(dpll_device_get);
295
296 /**
297 * dpll_device_put - decrease the refcount and free memory if possible
298 * @dpll: dpll_device struct pointer
299 *
300 * Context: Acquires a lock (dpll_lock)
301 * Drop reference for a dpll device, if all references are gone, delete
302 * dpll device object.
303 */
dpll_device_put(struct dpll_device * dpll)304 void dpll_device_put(struct dpll_device *dpll)
305 {
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));
313 kfree(dpll);
314 }
315 mutex_unlock(&dpll_lock);
316 }
317 EXPORT_SYMBOL_GPL(dpll_device_put);
318
319 static struct dpll_device_registration *
dpll_device_registration_find(struct dpll_device * dpll,const struct dpll_device_ops * ops,void * priv)320 dpll_device_registration_find(struct dpll_device *dpll,
321 const struct dpll_device_ops *ops, void *priv)
322 {
323 struct dpll_device_registration *reg;
324
325 list_for_each_entry(reg, &dpll->registration_list, list) {
326 if (reg->ops == ops && reg->priv == priv)
327 return reg;
328 }
329 return NULL;
330 }
331
332 /**
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
338 *
339 * Make dpll device available for user space.
340 *
341 * Context: Acquires a lock (dpll_lock)
342 * Return:
343 * * 0 on success
344 * * negative - error value
345 */
dpll_device_register(struct dpll_device * dpll,enum dpll_type type,const struct dpll_device_ops * ops,void * priv)346 int dpll_device_register(struct dpll_device *dpll, enum dpll_type type,
347 const struct dpll_device_ops *ops, void *priv)
348 {
349 struct dpll_device_registration *reg;
350 bool first_registration = false;
351
352 if (WARN_ON(!ops))
353 return -EINVAL;
354 if (WARN_ON(!ops->mode_get))
355 return -EINVAL;
356 if (WARN_ON(!ops->lock_status_get))
357 return -EINVAL;
358 if (WARN_ON(type < DPLL_TYPE_PPS || type > DPLL_TYPE_MAX))
359 return -EINVAL;
360
361 mutex_lock(&dpll_lock);
362 reg = dpll_device_registration_find(dpll, ops, priv);
363 if (reg) {
364 mutex_unlock(&dpll_lock);
365 return -EEXIST;
366 }
367
368 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
369 if (!reg) {
370 mutex_unlock(&dpll_lock);
371 return -ENOMEM;
372 }
373 reg->ops = ops;
374 reg->priv = priv;
375 dpll->type = type;
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);
380 return 0;
381 }
382
383 xa_set_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
384 dpll_device_create_ntf(dpll);
385 mutex_unlock(&dpll_lock);
386
387 return 0;
388 }
389 EXPORT_SYMBOL_GPL(dpll_device_register);
390
391 /**
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
396 *
397 * Unregister device, make it unavailable for userspace.
398 * Note: It does not free the memory
399 * Context: Acquires a lock (dpll_lock)
400 */
dpll_device_unregister(struct dpll_device * dpll,const struct dpll_device_ops * ops,void * priv)401 void dpll_device_unregister(struct dpll_device *dpll,
402 const struct dpll_device_ops *ops, void *priv)
403 {
404 struct dpll_device_registration *reg;
405
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);
410 if (WARN_ON(!reg)) {
411 mutex_unlock(&dpll_lock);
412 return;
413 }
414 list_del(®->list);
415 kfree(reg);
416
417 if (!list_empty(&dpll->registration_list)) {
418 mutex_unlock(&dpll_lock);
419 return;
420 }
421 xa_clear_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
422 mutex_unlock(&dpll_lock);
423 }
424 EXPORT_SYMBOL_GPL(dpll_device_unregister);
425
dpll_pin_prop_free(struct dpll_pin_properties * prop)426 static void dpll_pin_prop_free(struct dpll_pin_properties *prop)
427 {
428 kfree(prop->package_label);
429 kfree(prop->panel_label);
430 kfree(prop->board_label);
431 kfree(prop->freq_supported);
432 }
433
dpll_pin_prop_dup(const struct dpll_pin_properties * src,struct dpll_pin_properties * dst)434 static int dpll_pin_prop_dup(const struct dpll_pin_properties *src,
435 struct dpll_pin_properties *dst)
436 {
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)
444 return -ENOMEM;
445 }
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;
450 }
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;
455 }
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;
460 }
461
462 return 0;
463
464 err_package_label:
465 kfree(dst->panel_label);
466 err_panel_label:
467 kfree(dst->board_label);
468 err_board_label:
469 kfree(dst->freq_supported);
470 return -ENOMEM;
471 }
472
473 static struct dpll_pin *
dpll_pin_alloc(u64 clock_id,u32 pin_idx,struct module * module,const struct dpll_pin_properties * prop)474 dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,
475 const struct dpll_pin_properties *prop)
476 {
477 struct dpll_pin *pin;
478 int ret;
479
480 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
481 if (!pin)
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)) {
488 ret = -EINVAL;
489 goto err_pin_prop;
490 }
491 ret = dpll_pin_prop_dup(prop, &pin->prop);
492 if (ret)
493 goto err_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);
499 if (ret)
500 goto err_xa_alloc;
501 return pin;
502 err_xa_alloc:
503 xa_destroy(&pin->dpll_refs);
504 xa_destroy(&pin->parent_refs);
505 dpll_pin_prop_free(&pin->prop);
506 err_pin_prop:
507 kfree(pin);
508 return ERR_PTR(ret);
509 }
510
dpll_netdev_pin_assign(struct net_device * dev,struct dpll_pin * dpll_pin)511 static void dpll_netdev_pin_assign(struct net_device *dev, struct dpll_pin *dpll_pin)
512 {
513 rtnl_lock();
514 rcu_assign_pointer(dev->dpll_pin, dpll_pin);
515 rtnl_unlock();
516 }
517
dpll_netdev_pin_set(struct net_device * dev,struct dpll_pin * dpll_pin)518 void dpll_netdev_pin_set(struct net_device *dev, struct dpll_pin *dpll_pin)
519 {
520 WARN_ON(!dpll_pin);
521 dpll_netdev_pin_assign(dev, dpll_pin);
522 }
523 EXPORT_SYMBOL(dpll_netdev_pin_set);
524
dpll_netdev_pin_clear(struct net_device * dev)525 void dpll_netdev_pin_clear(struct net_device *dev)
526 {
527 dpll_netdev_pin_assign(dev, NULL);
528 }
529 EXPORT_SYMBOL(dpll_netdev_pin_clear);
530
531 /**
532 * dpll_pin_get - find existing or create new dpll pin
533 * @clock_id: clock_id of creator
534 * @pin_idx: idx given by dev driver
535 * @module: reference to registering module
536 * @prop: dpll pin properties
537 *
538 * Get existing object of a pin (unique for given arguments) or create new
539 * if doesn't exist yet.
540 *
541 * Context: Acquires a lock (dpll_lock)
542 * Return:
543 * * valid allocated dpll_pin struct pointer if succeeded
544 * * ERR_PTR(X) - error
545 */
546 struct dpll_pin *
dpll_pin_get(u64 clock_id,u32 pin_idx,struct module * module,const struct dpll_pin_properties * prop)547 dpll_pin_get(u64 clock_id, u32 pin_idx, struct module *module,
548 const struct dpll_pin_properties *prop)
549 {
550 struct dpll_pin *pos, *ret = NULL;
551 unsigned long i;
552
553 mutex_lock(&dpll_lock);
554 xa_for_each(&dpll_pin_xa, i, pos) {
555 if (pos->clock_id == clock_id &&
556 pos->pin_idx == pin_idx &&
557 pos->module == module) {
558 ret = pos;
559 refcount_inc(&ret->refcount);
560 break;
561 }
562 }
563 if (!ret)
564 ret = dpll_pin_alloc(clock_id, pin_idx, module, prop);
565 mutex_unlock(&dpll_lock);
566
567 return ret;
568 }
569 EXPORT_SYMBOL_GPL(dpll_pin_get);
570
571 /**
572 * dpll_pin_put - decrease the refcount and free memory if possible
573 * @pin: pointer to a pin to be put
574 *
575 * Drop reference for a pin, if all references are gone, delete pin object.
576 *
577 * Context: Acquires a lock (dpll_lock)
578 */
dpll_pin_put(struct dpll_pin * pin)579 void dpll_pin_put(struct dpll_pin *pin)
580 {
581 mutex_lock(&dpll_lock);
582 if (refcount_dec_and_test(&pin->refcount)) {
583 xa_destroy(&pin->dpll_refs);
584 xa_destroy(&pin->parent_refs);
585 xa_erase(&dpll_pin_xa, pin->id);
586 dpll_pin_prop_free(&pin->prop);
587 kfree_rcu(pin, rcu);
588 }
589 mutex_unlock(&dpll_lock);
590 }
591 EXPORT_SYMBOL_GPL(dpll_pin_put);
592
593 static int
__dpll_pin_register(struct dpll_device * dpll,struct dpll_pin * pin,const struct dpll_pin_ops * ops,void * priv)594 __dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
595 const struct dpll_pin_ops *ops, void *priv)
596 {
597 int ret;
598
599 ret = dpll_xa_ref_pin_add(&dpll->pin_refs, pin, ops, priv);
600 if (ret)
601 return ret;
602 ret = dpll_xa_ref_dpll_add(&pin->dpll_refs, dpll, ops, priv);
603 if (ret)
604 goto ref_pin_del;
605 xa_set_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
606 dpll_pin_create_ntf(pin);
607
608 return ret;
609
610 ref_pin_del:
611 dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
612 return ret;
613 }
614
615 /**
616 * dpll_pin_register - register the dpll pin in the subsystem
617 * @dpll: pointer to a dpll
618 * @pin: pointer to a dpll pin
619 * @ops: ops for a dpll pin ops
620 * @priv: pointer to private information of owner
621 *
622 * Context: Acquires a lock (dpll_lock)
623 * Return:
624 * * 0 on success
625 * * negative - error value
626 */
627 int
dpll_pin_register(struct dpll_device * dpll,struct dpll_pin * pin,const struct dpll_pin_ops * ops,void * priv)628 dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
629 const struct dpll_pin_ops *ops, void *priv)
630 {
631 int ret;
632
633 if (WARN_ON(!ops) ||
634 WARN_ON(!ops->state_on_dpll_get) ||
635 WARN_ON(!ops->direction_get))
636 return -EINVAL;
637
638 mutex_lock(&dpll_lock);
639 if (WARN_ON(!(dpll->module == pin->module &&
640 dpll->clock_id == pin->clock_id)))
641 ret = -EINVAL;
642 else
643 ret = __dpll_pin_register(dpll, pin, ops, priv);
644 mutex_unlock(&dpll_lock);
645
646 return ret;
647 }
648 EXPORT_SYMBOL_GPL(dpll_pin_register);
649
650 static void
__dpll_pin_unregister(struct dpll_device * dpll,struct dpll_pin * pin,const struct dpll_pin_ops * ops,void * priv)651 __dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
652 const struct dpll_pin_ops *ops, void *priv)
653 {
654 dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv);
655 dpll_xa_ref_dpll_del(&pin->dpll_refs, dpll, ops, priv);
656 if (xa_empty(&pin->dpll_refs))
657 xa_clear_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
658 }
659
660 /**
661 * dpll_pin_unregister - unregister dpll pin from dpll device
662 * @dpll: registered dpll pointer
663 * @pin: pointer to a pin
664 * @ops: ops for a dpll pin
665 * @priv: pointer to private information of owner
666 *
667 * Note: It does not free the memory
668 * Context: Acquires a lock (dpll_lock)
669 */
dpll_pin_unregister(struct dpll_device * dpll,struct dpll_pin * pin,const struct dpll_pin_ops * ops,void * priv)670 void dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
671 const struct dpll_pin_ops *ops, void *priv)
672 {
673 if (WARN_ON(xa_empty(&dpll->pin_refs)))
674 return;
675 if (WARN_ON(!xa_empty(&pin->parent_refs)))
676 return;
677
678 mutex_lock(&dpll_lock);
679 dpll_pin_delete_ntf(pin);
680 __dpll_pin_unregister(dpll, pin, ops, priv);
681 mutex_unlock(&dpll_lock);
682 }
683 EXPORT_SYMBOL_GPL(dpll_pin_unregister);
684
685 /**
686 * dpll_pin_on_pin_register - register a pin with a parent pin
687 * @parent: pointer to a parent pin
688 * @pin: pointer to a pin
689 * @ops: ops for a dpll pin
690 * @priv: pointer to private information of owner
691 *
692 * Register a pin with a parent pin, create references between them and
693 * between newly registered pin and dplls connected with a parent pin.
694 *
695 * Context: Acquires a lock (dpll_lock)
696 * Return:
697 * * 0 on success
698 * * negative - error value
699 */
dpll_pin_on_pin_register(struct dpll_pin * parent,struct dpll_pin * pin,const struct dpll_pin_ops * ops,void * priv)700 int dpll_pin_on_pin_register(struct dpll_pin *parent, struct dpll_pin *pin,
701 const struct dpll_pin_ops *ops, void *priv)
702 {
703 struct dpll_pin_ref *ref;
704 unsigned long i, stop;
705 int ret;
706
707 if (WARN_ON(parent->prop.type != DPLL_PIN_TYPE_MUX))
708 return -EINVAL;
709
710 if (WARN_ON(!ops) ||
711 WARN_ON(!ops->state_on_pin_get) ||
712 WARN_ON(!ops->direction_get))
713 return -EINVAL;
714
715 mutex_lock(&dpll_lock);
716 ret = dpll_xa_ref_pin_add(&pin->parent_refs, parent, ops, priv);
717 if (ret)
718 goto unlock;
719 refcount_inc(&pin->refcount);
720 xa_for_each(&parent->dpll_refs, i, ref) {
721 ret = __dpll_pin_register(ref->dpll, pin, ops, priv);
722 if (ret) {
723 stop = i;
724 goto dpll_unregister;
725 }
726 dpll_pin_create_ntf(pin);
727 }
728 mutex_unlock(&dpll_lock);
729
730 return ret;
731
732 dpll_unregister:
733 xa_for_each(&parent->dpll_refs, i, ref)
734 if (i < stop) {
735 __dpll_pin_unregister(ref->dpll, pin, ops, priv);
736 dpll_pin_delete_ntf(pin);
737 }
738 refcount_dec(&pin->refcount);
739 dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
740 unlock:
741 mutex_unlock(&dpll_lock);
742 return ret;
743 }
744 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_register);
745
746 /**
747 * dpll_pin_on_pin_unregister - unregister dpll pin from a parent pin
748 * @parent: pointer to a parent pin
749 * @pin: pointer to a pin
750 * @ops: ops for a dpll pin
751 * @priv: pointer to private information of owner
752 *
753 * Context: Acquires a lock (dpll_lock)
754 * Note: It does not free the memory
755 */
dpll_pin_on_pin_unregister(struct dpll_pin * parent,struct dpll_pin * pin,const struct dpll_pin_ops * ops,void * priv)756 void dpll_pin_on_pin_unregister(struct dpll_pin *parent, struct dpll_pin *pin,
757 const struct dpll_pin_ops *ops, void *priv)
758 {
759 struct dpll_pin_ref *ref;
760 unsigned long i;
761
762 mutex_lock(&dpll_lock);
763 dpll_pin_delete_ntf(pin);
764 dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv);
765 refcount_dec(&pin->refcount);
766 xa_for_each(&pin->dpll_refs, i, ref)
767 __dpll_pin_unregister(ref->dpll, pin, ops, priv);
768 mutex_unlock(&dpll_lock);
769 }
770 EXPORT_SYMBOL_GPL(dpll_pin_on_pin_unregister);
771
772 static struct dpll_device_registration *
dpll_device_registration_first(struct dpll_device * dpll)773 dpll_device_registration_first(struct dpll_device *dpll)
774 {
775 struct dpll_device_registration *reg;
776
777 reg = list_first_entry_or_null((struct list_head *)&dpll->registration_list,
778 struct dpll_device_registration, list);
779 WARN_ON(!reg);
780 return reg;
781 }
782
dpll_priv(struct dpll_device * dpll)783 void *dpll_priv(struct dpll_device *dpll)
784 {
785 struct dpll_device_registration *reg;
786
787 reg = dpll_device_registration_first(dpll);
788 return reg->priv;
789 }
790
dpll_device_ops(struct dpll_device * dpll)791 const struct dpll_device_ops *dpll_device_ops(struct dpll_device *dpll)
792 {
793 struct dpll_device_registration *reg;
794
795 reg = dpll_device_registration_first(dpll);
796 return reg->ops;
797 }
798
799 static struct dpll_pin_registration *
dpll_pin_registration_first(struct dpll_pin_ref * ref)800 dpll_pin_registration_first(struct dpll_pin_ref *ref)
801 {
802 struct dpll_pin_registration *reg;
803
804 reg = list_first_entry_or_null(&ref->registration_list,
805 struct dpll_pin_registration, list);
806 WARN_ON(!reg);
807 return reg;
808 }
809
dpll_pin_on_dpll_priv(struct dpll_device * dpll,struct dpll_pin * pin)810 void *dpll_pin_on_dpll_priv(struct dpll_device *dpll,
811 struct dpll_pin *pin)
812 {
813 struct dpll_pin_registration *reg;
814 struct dpll_pin_ref *ref;
815
816 ref = xa_load(&dpll->pin_refs, pin->pin_idx);
817 if (!ref)
818 return NULL;
819 reg = dpll_pin_registration_first(ref);
820 return reg->priv;
821 }
822
dpll_pin_on_pin_priv(struct dpll_pin * parent,struct dpll_pin * pin)823 void *dpll_pin_on_pin_priv(struct dpll_pin *parent,
824 struct dpll_pin *pin)
825 {
826 struct dpll_pin_registration *reg;
827 struct dpll_pin_ref *ref;
828
829 ref = xa_load(&pin->parent_refs, parent->pin_idx);
830 if (!ref)
831 return NULL;
832 reg = dpll_pin_registration_first(ref);
833 return reg->priv;
834 }
835
dpll_pin_ops(struct dpll_pin_ref * ref)836 const struct dpll_pin_ops *dpll_pin_ops(struct dpll_pin_ref *ref)
837 {
838 struct dpll_pin_registration *reg;
839
840 reg = dpll_pin_registration_first(ref);
841 return reg->ops;
842 }
843
dpll_init(void)844 static int __init dpll_init(void)
845 {
846 int ret;
847
848 ret = genl_register_family(&dpll_nl_family);
849 if (ret)
850 goto error;
851
852 return 0;
853
854 error:
855 mutex_destroy(&dpll_lock);
856 return ret;
857 }
858
dpll_exit(void)859 static void __exit dpll_exit(void)
860 {
861 genl_unregister_family(&dpll_nl_family);
862 mutex_destroy(&dpll_lock);
863 }
864
865 subsys_initcall(dpll_init);
866 module_exit(dpll_exit);
867