dpll_core.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/idr.h>
  12. #include <linux/property.h>
  13. #include <linux/slab.h>
  14. #include <linux/string.h>
  15. #include "dpll_core.h"
  16. #include "dpll_netlink.h"
  17. /* Mutex lock to protect DPLL subsystem devices and pins */
  18. DEFINE_MUTEX(dpll_lock);
  19. DEFINE_XARRAY_FLAGS(dpll_device_xa, XA_FLAGS_ALLOC);
  20. DEFINE_XARRAY_FLAGS(dpll_pin_xa, XA_FLAGS_ALLOC);
  21. static RAW_NOTIFIER_HEAD(dpll_notifier_chain);
  22. static DEFINE_IDA(dpll_pin_idx_ida);
  23. static u32 dpll_device_xa_id;
  24. static u32 dpll_pin_xa_id;
  25. #define ASSERT_DPLL_REGISTERED(d) \
  26. WARN_ON_ONCE(!xa_get_mark(&dpll_device_xa, (d)->id, DPLL_REGISTERED))
  27. #define ASSERT_DPLL_NOT_REGISTERED(d) \
  28. WARN_ON_ONCE(xa_get_mark(&dpll_device_xa, (d)->id, DPLL_REGISTERED))
  29. #define ASSERT_DPLL_PIN_REGISTERED(p) \
  30. WARN_ON_ONCE(!xa_get_mark(&dpll_pin_xa, (p)->id, DPLL_REGISTERED))
  31. struct dpll_device_registration {
  32. struct list_head list;
  33. const struct dpll_device_ops *ops;
  34. void *priv;
  35. dpll_tracker tracker;
  36. };
  37. struct dpll_pin_registration {
  38. struct list_head list;
  39. const struct dpll_pin_ops *ops;
  40. void *priv;
  41. void *cookie;
  42. dpll_tracker tracker;
  43. };
  44. static int call_dpll_notifiers(unsigned long action, void *info)
  45. {
  46. lockdep_assert_held(&dpll_lock);
  47. return raw_notifier_call_chain(&dpll_notifier_chain, action, info);
  48. }
  49. void dpll_device_notify(struct dpll_device *dpll, unsigned long action)
  50. {
  51. struct dpll_device_notifier_info info = {
  52. .dpll = dpll,
  53. .id = dpll->id,
  54. .idx = dpll->device_idx,
  55. .clock_id = dpll->clock_id,
  56. .type = dpll->type,
  57. };
  58. call_dpll_notifiers(action, &info);
  59. }
  60. void dpll_pin_notify(struct dpll_pin *pin, unsigned long action)
  61. {
  62. struct dpll_pin_notifier_info info = {
  63. .pin = pin,
  64. .id = pin->id,
  65. .idx = pin->pin_idx,
  66. .clock_id = pin->clock_id,
  67. .fwnode = pin->fwnode,
  68. .prop = &pin->prop,
  69. };
  70. call_dpll_notifiers(action, &info);
  71. }
  72. static void dpll_device_tracker_alloc(struct dpll_device *dpll,
  73. dpll_tracker *tracker)
  74. {
  75. #ifdef CONFIG_DPLL_REFCNT_TRACKER
  76. ref_tracker_alloc(&dpll->refcnt_tracker, tracker, GFP_KERNEL);
  77. #endif
  78. }
  79. static void dpll_device_tracker_free(struct dpll_device *dpll,
  80. dpll_tracker *tracker)
  81. {
  82. #ifdef CONFIG_DPLL_REFCNT_TRACKER
  83. ref_tracker_free(&dpll->refcnt_tracker, tracker);
  84. #endif
  85. }
  86. static void __dpll_device_hold(struct dpll_device *dpll, dpll_tracker *tracker)
  87. {
  88. dpll_device_tracker_alloc(dpll, tracker);
  89. refcount_inc(&dpll->refcount);
  90. }
  91. static void __dpll_device_put(struct dpll_device *dpll, dpll_tracker *tracker)
  92. {
  93. dpll_device_tracker_free(dpll, tracker);
  94. if (refcount_dec_and_test(&dpll->refcount)) {
  95. ASSERT_DPLL_NOT_REGISTERED(dpll);
  96. WARN_ON_ONCE(!xa_empty(&dpll->pin_refs));
  97. xa_destroy(&dpll->pin_refs);
  98. xa_erase(&dpll_device_xa, dpll->id);
  99. WARN_ON(!list_empty(&dpll->registration_list));
  100. ref_tracker_dir_exit(&dpll->refcnt_tracker);
  101. kfree(dpll);
  102. }
  103. }
  104. static void dpll_pin_tracker_alloc(struct dpll_pin *pin, dpll_tracker *tracker)
  105. {
  106. #ifdef CONFIG_DPLL_REFCNT_TRACKER
  107. ref_tracker_alloc(&pin->refcnt_tracker, tracker, GFP_KERNEL);
  108. #endif
  109. }
  110. static void dpll_pin_tracker_free(struct dpll_pin *pin, dpll_tracker *tracker)
  111. {
  112. #ifdef CONFIG_DPLL_REFCNT_TRACKER
  113. ref_tracker_free(&pin->refcnt_tracker, tracker);
  114. #endif
  115. }
  116. static void __dpll_pin_hold(struct dpll_pin *pin, dpll_tracker *tracker)
  117. {
  118. dpll_pin_tracker_alloc(pin, tracker);
  119. refcount_inc(&pin->refcount);
  120. }
  121. static void dpll_pin_idx_free(u32 pin_idx);
  122. static void dpll_pin_prop_free(struct dpll_pin_properties *prop);
  123. static void __dpll_pin_put(struct dpll_pin *pin, dpll_tracker *tracker)
  124. {
  125. dpll_pin_tracker_free(pin, tracker);
  126. if (refcount_dec_and_test(&pin->refcount)) {
  127. xa_erase(&dpll_pin_xa, pin->id);
  128. xa_destroy(&pin->dpll_refs);
  129. xa_destroy(&pin->parent_refs);
  130. xa_destroy(&pin->ref_sync_pins);
  131. dpll_pin_prop_free(&pin->prop);
  132. fwnode_handle_put(pin->fwnode);
  133. dpll_pin_idx_free(pin->pin_idx);
  134. ref_tracker_dir_exit(&pin->refcnt_tracker);
  135. kfree_rcu(pin, rcu);
  136. }
  137. }
  138. struct dpll_device *dpll_device_get_by_id(int id)
  139. {
  140. if (xa_get_mark(&dpll_device_xa, id, DPLL_REGISTERED))
  141. return xa_load(&dpll_device_xa, id);
  142. return NULL;
  143. }
  144. static struct dpll_pin_registration *
  145. dpll_pin_registration_find(struct dpll_pin_ref *ref,
  146. const struct dpll_pin_ops *ops, void *priv,
  147. void *cookie)
  148. {
  149. struct dpll_pin_registration *reg;
  150. list_for_each_entry(reg, &ref->registration_list, list) {
  151. if (reg->ops == ops && reg->priv == priv &&
  152. reg->cookie == cookie)
  153. return reg;
  154. }
  155. return NULL;
  156. }
  157. static int
  158. dpll_xa_ref_pin_add(struct xarray *xa_pins, struct dpll_pin *pin,
  159. const struct dpll_pin_ops *ops, void *priv,
  160. void *cookie)
  161. {
  162. struct dpll_pin_registration *reg;
  163. struct dpll_pin_ref *ref;
  164. bool ref_exists = false;
  165. unsigned long i;
  166. int ret;
  167. xa_for_each(xa_pins, i, ref) {
  168. if (ref->pin != pin)
  169. continue;
  170. reg = dpll_pin_registration_find(ref, ops, priv, cookie);
  171. if (reg)
  172. return -EEXIST;
  173. ref_exists = true;
  174. break;
  175. }
  176. if (!ref_exists) {
  177. ref = kzalloc_obj(*ref);
  178. if (!ref)
  179. return -ENOMEM;
  180. ref->pin = pin;
  181. INIT_LIST_HEAD(&ref->registration_list);
  182. ret = xa_insert(xa_pins, pin->pin_idx, ref, GFP_KERNEL);
  183. if (ret) {
  184. kfree(ref);
  185. return ret;
  186. }
  187. refcount_set(&ref->refcount, 1);
  188. }
  189. reg = kzalloc_obj(*reg);
  190. if (!reg) {
  191. if (!ref_exists) {
  192. xa_erase(xa_pins, pin->pin_idx);
  193. kfree(ref);
  194. }
  195. return -ENOMEM;
  196. }
  197. reg->ops = ops;
  198. reg->priv = priv;
  199. reg->cookie = cookie;
  200. __dpll_pin_hold(pin, &reg->tracker);
  201. if (ref_exists)
  202. refcount_inc(&ref->refcount);
  203. list_add_tail(&reg->list, &ref->registration_list);
  204. return 0;
  205. }
  206. static int dpll_xa_ref_pin_del(struct xarray *xa_pins, struct dpll_pin *pin,
  207. const struct dpll_pin_ops *ops, void *priv,
  208. void *cookie)
  209. {
  210. struct dpll_pin_registration *reg;
  211. struct dpll_pin_ref *ref;
  212. unsigned long i;
  213. xa_for_each(xa_pins, i, ref) {
  214. if (ref->pin != pin)
  215. continue;
  216. reg = dpll_pin_registration_find(ref, ops, priv, cookie);
  217. if (WARN_ON(!reg))
  218. return -EINVAL;
  219. list_del(&reg->list);
  220. __dpll_pin_put(pin, &reg->tracker);
  221. kfree(reg);
  222. if (refcount_dec_and_test(&ref->refcount)) {
  223. xa_erase(xa_pins, i);
  224. WARN_ON(!list_empty(&ref->registration_list));
  225. kfree(ref);
  226. }
  227. return 0;
  228. }
  229. return -EINVAL;
  230. }
  231. static int
  232. dpll_xa_ref_dpll_add(struct xarray *xa_dplls, struct dpll_device *dpll,
  233. const struct dpll_pin_ops *ops, void *priv, void *cookie)
  234. {
  235. struct dpll_pin_registration *reg;
  236. struct dpll_pin_ref *ref;
  237. bool ref_exists = false;
  238. unsigned long i;
  239. int ret;
  240. xa_for_each(xa_dplls, i, ref) {
  241. if (ref->dpll != dpll)
  242. continue;
  243. reg = dpll_pin_registration_find(ref, ops, priv, cookie);
  244. if (reg)
  245. return -EEXIST;
  246. ref_exists = true;
  247. break;
  248. }
  249. if (!ref_exists) {
  250. ref = kzalloc_obj(*ref);
  251. if (!ref)
  252. return -ENOMEM;
  253. ref->dpll = dpll;
  254. INIT_LIST_HEAD(&ref->registration_list);
  255. ret = xa_insert(xa_dplls, dpll->id, ref, GFP_KERNEL);
  256. if (ret) {
  257. kfree(ref);
  258. return ret;
  259. }
  260. refcount_set(&ref->refcount, 1);
  261. }
  262. reg = kzalloc_obj(*reg);
  263. if (!reg) {
  264. if (!ref_exists) {
  265. xa_erase(xa_dplls, dpll->id);
  266. kfree(ref);
  267. }
  268. return -ENOMEM;
  269. }
  270. reg->ops = ops;
  271. reg->priv = priv;
  272. reg->cookie = cookie;
  273. __dpll_device_hold(dpll, &reg->tracker);
  274. if (ref_exists)
  275. refcount_inc(&ref->refcount);
  276. list_add_tail(&reg->list, &ref->registration_list);
  277. return 0;
  278. }
  279. static void
  280. dpll_xa_ref_dpll_del(struct xarray *xa_dplls, struct dpll_device *dpll,
  281. const struct dpll_pin_ops *ops, void *priv, void *cookie)
  282. {
  283. struct dpll_pin_registration *reg;
  284. struct dpll_pin_ref *ref;
  285. unsigned long i;
  286. xa_for_each(xa_dplls, i, ref) {
  287. if (ref->dpll != dpll)
  288. continue;
  289. reg = dpll_pin_registration_find(ref, ops, priv, cookie);
  290. if (WARN_ON(!reg))
  291. return;
  292. list_del(&reg->list);
  293. __dpll_device_put(dpll, &reg->tracker);
  294. kfree(reg);
  295. if (refcount_dec_and_test(&ref->refcount)) {
  296. xa_erase(xa_dplls, i);
  297. WARN_ON(!list_empty(&ref->registration_list));
  298. kfree(ref);
  299. }
  300. return;
  301. }
  302. }
  303. struct dpll_pin_ref *dpll_xa_ref_dpll_first(struct xarray *xa_refs)
  304. {
  305. struct dpll_pin_ref *ref;
  306. unsigned long i = 0;
  307. ref = xa_find(xa_refs, &i, ULONG_MAX, XA_PRESENT);
  308. WARN_ON(!ref);
  309. return ref;
  310. }
  311. static struct dpll_device *
  312. dpll_device_alloc(const u64 clock_id, u32 device_idx, struct module *module)
  313. {
  314. struct dpll_device *dpll;
  315. int ret;
  316. dpll = kzalloc_obj(*dpll);
  317. if (!dpll)
  318. return ERR_PTR(-ENOMEM);
  319. refcount_set(&dpll->refcount, 1);
  320. INIT_LIST_HEAD(&dpll->registration_list);
  321. dpll->device_idx = device_idx;
  322. dpll->clock_id = clock_id;
  323. dpll->module = module;
  324. ret = xa_alloc_cyclic(&dpll_device_xa, &dpll->id, dpll, xa_limit_32b,
  325. &dpll_device_xa_id, GFP_KERNEL);
  326. if (ret < 0) {
  327. kfree(dpll);
  328. return ERR_PTR(ret);
  329. }
  330. xa_init_flags(&dpll->pin_refs, XA_FLAGS_ALLOC);
  331. ref_tracker_dir_init(&dpll->refcnt_tracker, 128, "dpll_device");
  332. return dpll;
  333. }
  334. /**
  335. * dpll_device_get - find existing or create new dpll device
  336. * @clock_id: clock_id of creator
  337. * @device_idx: idx given by device driver
  338. * @module: reference to registering module
  339. * @tracker: tracking object for the acquired reference
  340. *
  341. * Get existing object of a dpll device, unique for given arguments.
  342. * Create new if doesn't exist yet.
  343. *
  344. * Context: Acquires a lock (dpll_lock)
  345. * Return:
  346. * * valid dpll_device struct pointer if succeeded
  347. * * ERR_PTR(X) - error
  348. */
  349. struct dpll_device *
  350. dpll_device_get(u64 clock_id, u32 device_idx, struct module *module,
  351. dpll_tracker *tracker)
  352. {
  353. struct dpll_device *dpll, *ret = NULL;
  354. unsigned long index;
  355. mutex_lock(&dpll_lock);
  356. xa_for_each(&dpll_device_xa, index, dpll) {
  357. if (dpll->clock_id == clock_id &&
  358. dpll->device_idx == device_idx &&
  359. dpll->module == module) {
  360. __dpll_device_hold(dpll, tracker);
  361. ret = dpll;
  362. break;
  363. }
  364. }
  365. if (!ret) {
  366. ret = dpll_device_alloc(clock_id, device_idx, module);
  367. if (!IS_ERR(ret))
  368. dpll_device_tracker_alloc(ret, tracker);
  369. }
  370. mutex_unlock(&dpll_lock);
  371. return ret;
  372. }
  373. EXPORT_SYMBOL_GPL(dpll_device_get);
  374. /**
  375. * dpll_device_put - decrease the refcount and free memory if possible
  376. * @dpll: dpll_device struct pointer
  377. * @tracker: tracking object for the acquired reference
  378. *
  379. * Context: Acquires a lock (dpll_lock)
  380. * Drop reference for a dpll device, if all references are gone, delete
  381. * dpll device object.
  382. */
  383. void dpll_device_put(struct dpll_device *dpll, dpll_tracker *tracker)
  384. {
  385. mutex_lock(&dpll_lock);
  386. __dpll_device_put(dpll, tracker);
  387. mutex_unlock(&dpll_lock);
  388. }
  389. EXPORT_SYMBOL_GPL(dpll_device_put);
  390. static struct dpll_device_registration *
  391. dpll_device_registration_find(struct dpll_device *dpll,
  392. const struct dpll_device_ops *ops, void *priv)
  393. {
  394. struct dpll_device_registration *reg;
  395. list_for_each_entry(reg, &dpll->registration_list, list) {
  396. if (reg->ops == ops && reg->priv == priv)
  397. return reg;
  398. }
  399. return NULL;
  400. }
  401. /**
  402. * dpll_device_register - register the dpll device in the subsystem
  403. * @dpll: pointer to a dpll
  404. * @type: type of a dpll
  405. * @ops: ops for a dpll device
  406. * @priv: pointer to private information of owner
  407. *
  408. * Make dpll device available for user space.
  409. *
  410. * Context: Acquires a lock (dpll_lock)
  411. * Return:
  412. * * 0 on success
  413. * * negative - error value
  414. */
  415. int dpll_device_register(struct dpll_device *dpll, enum dpll_type type,
  416. const struct dpll_device_ops *ops, void *priv)
  417. {
  418. struct dpll_device_registration *reg;
  419. bool first_registration = false;
  420. if (WARN_ON(!ops))
  421. return -EINVAL;
  422. if (WARN_ON(!ops->mode_get))
  423. return -EINVAL;
  424. if (WARN_ON(!ops->lock_status_get))
  425. return -EINVAL;
  426. if (WARN_ON(type < DPLL_TYPE_PPS || type > DPLL_TYPE_MAX))
  427. return -EINVAL;
  428. mutex_lock(&dpll_lock);
  429. reg = dpll_device_registration_find(dpll, ops, priv);
  430. if (reg) {
  431. mutex_unlock(&dpll_lock);
  432. return -EEXIST;
  433. }
  434. reg = kzalloc_obj(*reg);
  435. if (!reg) {
  436. mutex_unlock(&dpll_lock);
  437. return -ENOMEM;
  438. }
  439. reg->ops = ops;
  440. reg->priv = priv;
  441. dpll->type = type;
  442. __dpll_device_hold(dpll, &reg->tracker);
  443. first_registration = list_empty(&dpll->registration_list);
  444. list_add_tail(&reg->list, &dpll->registration_list);
  445. if (!first_registration) {
  446. mutex_unlock(&dpll_lock);
  447. return 0;
  448. }
  449. xa_set_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
  450. dpll_device_create_ntf(dpll);
  451. mutex_unlock(&dpll_lock);
  452. return 0;
  453. }
  454. EXPORT_SYMBOL_GPL(dpll_device_register);
  455. /**
  456. * dpll_device_unregister - unregister dpll device
  457. * @dpll: registered dpll pointer
  458. * @ops: ops for a dpll device
  459. * @priv: pointer to private information of owner
  460. *
  461. * Unregister device, make it unavailable for userspace.
  462. * Note: It does not free the memory
  463. * Context: Acquires a lock (dpll_lock)
  464. */
  465. void dpll_device_unregister(struct dpll_device *dpll,
  466. const struct dpll_device_ops *ops, void *priv)
  467. {
  468. struct dpll_device_registration *reg;
  469. mutex_lock(&dpll_lock);
  470. ASSERT_DPLL_REGISTERED(dpll);
  471. dpll_device_delete_ntf(dpll);
  472. reg = dpll_device_registration_find(dpll, ops, priv);
  473. if (WARN_ON(!reg)) {
  474. mutex_unlock(&dpll_lock);
  475. return;
  476. }
  477. list_del(&reg->list);
  478. __dpll_device_put(dpll, &reg->tracker);
  479. kfree(reg);
  480. if (!list_empty(&dpll->registration_list)) {
  481. mutex_unlock(&dpll_lock);
  482. return;
  483. }
  484. xa_clear_mark(&dpll_device_xa, dpll->id, DPLL_REGISTERED);
  485. mutex_unlock(&dpll_lock);
  486. }
  487. EXPORT_SYMBOL_GPL(dpll_device_unregister);
  488. static int dpll_pin_idx_alloc(u32 *pin_idx)
  489. {
  490. int ret;
  491. if (!pin_idx)
  492. return -EINVAL;
  493. /* Alloc unique number from IDA. Number belongs to <0, INT_MAX> range */
  494. ret = ida_alloc(&dpll_pin_idx_ida, GFP_KERNEL);
  495. if (ret < 0)
  496. return ret;
  497. /* Map the value to dynamic pin index range <INT_MAX+1, U32_MAX> */
  498. *pin_idx = (u32)ret + INT_MAX + 1;
  499. return 0;
  500. }
  501. static void dpll_pin_idx_free(u32 pin_idx)
  502. {
  503. if (pin_idx <= INT_MAX)
  504. return; /* Not a dynamic pin index */
  505. /* Map the index value from dynamic pin index range to IDA range and
  506. * free it.
  507. */
  508. pin_idx -= (u32)INT_MAX + 1;
  509. ida_free(&dpll_pin_idx_ida, pin_idx);
  510. }
  511. static void dpll_pin_prop_free(struct dpll_pin_properties *prop)
  512. {
  513. kfree(prop->package_label);
  514. kfree(prop->panel_label);
  515. kfree(prop->board_label);
  516. kfree(prop->freq_supported);
  517. }
  518. static int dpll_pin_prop_dup(const struct dpll_pin_properties *src,
  519. struct dpll_pin_properties *dst)
  520. {
  521. if (WARN_ON(src->freq_supported && !src->freq_supported_num))
  522. return -EINVAL;
  523. memcpy(dst, src, sizeof(*dst));
  524. if (src->freq_supported) {
  525. size_t freq_size = src->freq_supported_num *
  526. sizeof(*src->freq_supported);
  527. dst->freq_supported = kmemdup(src->freq_supported,
  528. freq_size, GFP_KERNEL);
  529. if (!dst->freq_supported)
  530. return -ENOMEM;
  531. }
  532. if (src->board_label) {
  533. dst->board_label = kstrdup(src->board_label, GFP_KERNEL);
  534. if (!dst->board_label)
  535. goto err_board_label;
  536. }
  537. if (src->panel_label) {
  538. dst->panel_label = kstrdup(src->panel_label, GFP_KERNEL);
  539. if (!dst->panel_label)
  540. goto err_panel_label;
  541. }
  542. if (src->package_label) {
  543. dst->package_label = kstrdup(src->package_label, GFP_KERNEL);
  544. if (!dst->package_label)
  545. goto err_package_label;
  546. }
  547. return 0;
  548. err_package_label:
  549. kfree(dst->panel_label);
  550. err_panel_label:
  551. kfree(dst->board_label);
  552. err_board_label:
  553. kfree(dst->freq_supported);
  554. return -ENOMEM;
  555. }
  556. static struct dpll_pin *
  557. dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,
  558. const struct dpll_pin_properties *prop)
  559. {
  560. struct dpll_pin *pin;
  561. int ret;
  562. if (pin_idx == DPLL_PIN_IDX_UNSPEC) {
  563. ret = dpll_pin_idx_alloc(&pin_idx);
  564. if (ret)
  565. return ERR_PTR(ret);
  566. } else if (pin_idx > INT_MAX) {
  567. return ERR_PTR(-EINVAL);
  568. }
  569. pin = kzalloc_obj(*pin);
  570. if (!pin) {
  571. ret = -ENOMEM;
  572. goto err_pin_alloc;
  573. }
  574. pin->pin_idx = pin_idx;
  575. pin->clock_id = clock_id;
  576. pin->module = module;
  577. if (WARN_ON(prop->type < DPLL_PIN_TYPE_MUX ||
  578. prop->type > DPLL_PIN_TYPE_MAX)) {
  579. ret = -EINVAL;
  580. goto err_pin_prop;
  581. }
  582. ret = dpll_pin_prop_dup(prop, &pin->prop);
  583. if (ret)
  584. goto err_pin_prop;
  585. refcount_set(&pin->refcount, 1);
  586. xa_init_flags(&pin->dpll_refs, XA_FLAGS_ALLOC);
  587. xa_init_flags(&pin->parent_refs, XA_FLAGS_ALLOC);
  588. xa_init_flags(&pin->ref_sync_pins, XA_FLAGS_ALLOC);
  589. ret = xa_alloc_cyclic(&dpll_pin_xa, &pin->id, pin, xa_limit_32b,
  590. &dpll_pin_xa_id, GFP_KERNEL);
  591. if (ret < 0)
  592. goto err_xa_alloc;
  593. ref_tracker_dir_init(&pin->refcnt_tracker, 128, "dpll_pin");
  594. return pin;
  595. err_xa_alloc:
  596. xa_destroy(&pin->dpll_refs);
  597. xa_destroy(&pin->parent_refs);
  598. xa_destroy(&pin->ref_sync_pins);
  599. dpll_pin_prop_free(&pin->prop);
  600. err_pin_prop:
  601. kfree(pin);
  602. err_pin_alloc:
  603. dpll_pin_idx_free(pin_idx);
  604. return ERR_PTR(ret);
  605. }
  606. static void dpll_netdev_pin_assign(struct net_device *dev, struct dpll_pin *dpll_pin)
  607. {
  608. rtnl_lock();
  609. rcu_assign_pointer(dev->dpll_pin, dpll_pin);
  610. rtnl_unlock();
  611. }
  612. void dpll_netdev_pin_set(struct net_device *dev, struct dpll_pin *dpll_pin)
  613. {
  614. WARN_ON(!dpll_pin);
  615. dpll_netdev_pin_assign(dev, dpll_pin);
  616. }
  617. EXPORT_SYMBOL(dpll_netdev_pin_set);
  618. void dpll_netdev_pin_clear(struct net_device *dev)
  619. {
  620. dpll_netdev_pin_assign(dev, NULL);
  621. }
  622. EXPORT_SYMBOL(dpll_netdev_pin_clear);
  623. int register_dpll_notifier(struct notifier_block *nb)
  624. {
  625. int ret;
  626. mutex_lock(&dpll_lock);
  627. ret = raw_notifier_chain_register(&dpll_notifier_chain, nb);
  628. mutex_unlock(&dpll_lock);
  629. return ret;
  630. }
  631. EXPORT_SYMBOL_GPL(register_dpll_notifier);
  632. int unregister_dpll_notifier(struct notifier_block *nb)
  633. {
  634. int ret;
  635. mutex_lock(&dpll_lock);
  636. ret = raw_notifier_chain_unregister(&dpll_notifier_chain, nb);
  637. mutex_unlock(&dpll_lock);
  638. return ret;
  639. }
  640. EXPORT_SYMBOL_GPL(unregister_dpll_notifier);
  641. /**
  642. * dpll_pin_get - find existing or create new dpll pin
  643. * @clock_id: clock_id of creator
  644. * @pin_idx: idx given by dev driver
  645. * @module: reference to registering module
  646. * @prop: dpll pin properties
  647. * @tracker: tracking object for the acquired reference
  648. *
  649. * Get existing object of a pin (unique for given arguments) or create new
  650. * if doesn't exist yet.
  651. *
  652. * Context: Acquires a lock (dpll_lock)
  653. * Return:
  654. * * valid allocated dpll_pin struct pointer if succeeded
  655. * * ERR_PTR(X) - error
  656. */
  657. struct dpll_pin *
  658. dpll_pin_get(u64 clock_id, u32 pin_idx, struct module *module,
  659. const struct dpll_pin_properties *prop, dpll_tracker *tracker)
  660. {
  661. struct dpll_pin *pos, *ret = NULL;
  662. unsigned long i;
  663. mutex_lock(&dpll_lock);
  664. xa_for_each(&dpll_pin_xa, i, pos) {
  665. if (pos->clock_id == clock_id &&
  666. pos->pin_idx == pin_idx &&
  667. pos->module == module) {
  668. __dpll_pin_hold(pos, tracker);
  669. ret = pos;
  670. break;
  671. }
  672. }
  673. if (!ret) {
  674. ret = dpll_pin_alloc(clock_id, pin_idx, module, prop);
  675. if (!IS_ERR(ret))
  676. dpll_pin_tracker_alloc(ret, tracker);
  677. }
  678. mutex_unlock(&dpll_lock);
  679. return ret;
  680. }
  681. EXPORT_SYMBOL_GPL(dpll_pin_get);
  682. /**
  683. * dpll_pin_put - decrease the refcount and free memory if possible
  684. * @pin: pointer to a pin to be put
  685. * @tracker: tracking object for the acquired reference
  686. *
  687. * Drop reference for a pin, if all references are gone, delete pin object.
  688. *
  689. * Context: Acquires a lock (dpll_lock)
  690. */
  691. void dpll_pin_put(struct dpll_pin *pin, dpll_tracker *tracker)
  692. {
  693. mutex_lock(&dpll_lock);
  694. __dpll_pin_put(pin, tracker);
  695. mutex_unlock(&dpll_lock);
  696. }
  697. EXPORT_SYMBOL_GPL(dpll_pin_put);
  698. /**
  699. * dpll_pin_fwnode_set - set dpll pin firmware node reference
  700. * @pin: pointer to a dpll pin
  701. * @fwnode: firmware node handle
  702. *
  703. * Set firmware node handle for the given dpll pin.
  704. */
  705. void dpll_pin_fwnode_set(struct dpll_pin *pin, struct fwnode_handle *fwnode)
  706. {
  707. mutex_lock(&dpll_lock);
  708. fwnode_handle_put(pin->fwnode); /* Drop fwnode previously set */
  709. pin->fwnode = fwnode_handle_get(fwnode);
  710. mutex_unlock(&dpll_lock);
  711. }
  712. EXPORT_SYMBOL_GPL(dpll_pin_fwnode_set);
  713. /**
  714. * fwnode_dpll_pin_find - find dpll pin by firmware node reference
  715. * @fwnode: reference to firmware node
  716. * @tracker: tracking object for the acquired reference
  717. *
  718. * Get existing object of a pin that is associated with given firmware node
  719. * reference.
  720. *
  721. * Context: Acquires a lock (dpll_lock)
  722. * Return:
  723. * * valid dpll_pin pointer on success
  724. * * NULL when no such pin exists
  725. */
  726. struct dpll_pin *fwnode_dpll_pin_find(struct fwnode_handle *fwnode,
  727. dpll_tracker *tracker)
  728. {
  729. struct dpll_pin *pin, *ret = NULL;
  730. unsigned long index;
  731. mutex_lock(&dpll_lock);
  732. xa_for_each(&dpll_pin_xa, index, pin) {
  733. if (pin->fwnode == fwnode) {
  734. __dpll_pin_hold(pin, tracker);
  735. ret = pin;
  736. break;
  737. }
  738. }
  739. mutex_unlock(&dpll_lock);
  740. return ret;
  741. }
  742. EXPORT_SYMBOL_GPL(fwnode_dpll_pin_find);
  743. static int
  744. __dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
  745. const struct dpll_pin_ops *ops, void *priv, void *cookie)
  746. {
  747. int ret;
  748. ret = dpll_xa_ref_pin_add(&dpll->pin_refs, pin, ops, priv, cookie);
  749. if (ret)
  750. return ret;
  751. ret = dpll_xa_ref_dpll_add(&pin->dpll_refs, dpll, ops, priv, cookie);
  752. if (ret)
  753. goto ref_pin_del;
  754. xa_set_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
  755. dpll_pin_create_ntf(pin);
  756. return ret;
  757. ref_pin_del:
  758. dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv, cookie);
  759. return ret;
  760. }
  761. /**
  762. * dpll_pin_register - register the dpll pin in the subsystem
  763. * @dpll: pointer to a dpll
  764. * @pin: pointer to a dpll pin
  765. * @ops: ops for a dpll pin ops
  766. * @priv: pointer to private information of owner
  767. *
  768. * Context: Acquires a lock (dpll_lock)
  769. * Return:
  770. * * 0 on success
  771. * * negative - error value
  772. */
  773. int
  774. dpll_pin_register(struct dpll_device *dpll, struct dpll_pin *pin,
  775. const struct dpll_pin_ops *ops, void *priv)
  776. {
  777. int ret;
  778. if (WARN_ON(!ops) ||
  779. WARN_ON(!ops->state_on_dpll_get) ||
  780. WARN_ON(!ops->direction_get))
  781. return -EINVAL;
  782. mutex_lock(&dpll_lock);
  783. if (WARN_ON(!(dpll->module == pin->module &&
  784. dpll->clock_id == pin->clock_id)))
  785. ret = -EINVAL;
  786. else
  787. ret = __dpll_pin_register(dpll, pin, ops, priv, NULL);
  788. mutex_unlock(&dpll_lock);
  789. return ret;
  790. }
  791. EXPORT_SYMBOL_GPL(dpll_pin_register);
  792. static void dpll_pin_ref_sync_pair_del(u32 ref_sync_pin_id)
  793. {
  794. struct dpll_pin *pin, *ref_sync_pin;
  795. unsigned long i;
  796. xa_for_each(&dpll_pin_xa, i, pin) {
  797. ref_sync_pin = xa_load(&pin->ref_sync_pins, ref_sync_pin_id);
  798. if (ref_sync_pin) {
  799. xa_erase(&pin->ref_sync_pins, ref_sync_pin_id);
  800. __dpll_pin_change_ntf(pin);
  801. }
  802. }
  803. }
  804. static void
  805. __dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
  806. const struct dpll_pin_ops *ops, void *priv, void *cookie)
  807. {
  808. ASSERT_DPLL_PIN_REGISTERED(pin);
  809. dpll_pin_ref_sync_pair_del(pin->id);
  810. dpll_xa_ref_pin_del(&dpll->pin_refs, pin, ops, priv, cookie);
  811. dpll_xa_ref_dpll_del(&pin->dpll_refs, dpll, ops, priv, cookie);
  812. if (xa_empty(&pin->dpll_refs))
  813. xa_clear_mark(&dpll_pin_xa, pin->id, DPLL_REGISTERED);
  814. }
  815. /**
  816. * dpll_pin_unregister - unregister dpll pin from dpll device
  817. * @dpll: registered dpll pointer
  818. * @pin: pointer to a pin
  819. * @ops: ops for a dpll pin
  820. * @priv: pointer to private information of owner
  821. *
  822. * Note: It does not free the memory
  823. * Context: Acquires a lock (dpll_lock)
  824. */
  825. void dpll_pin_unregister(struct dpll_device *dpll, struct dpll_pin *pin,
  826. const struct dpll_pin_ops *ops, void *priv)
  827. {
  828. if (WARN_ON(xa_empty(&dpll->pin_refs)))
  829. return;
  830. if (WARN_ON(!xa_empty(&pin->parent_refs)))
  831. return;
  832. mutex_lock(&dpll_lock);
  833. dpll_pin_delete_ntf(pin);
  834. __dpll_pin_unregister(dpll, pin, ops, priv, NULL);
  835. mutex_unlock(&dpll_lock);
  836. }
  837. EXPORT_SYMBOL_GPL(dpll_pin_unregister);
  838. /**
  839. * dpll_pin_on_pin_register - register a pin with a parent pin
  840. * @parent: pointer to a parent pin
  841. * @pin: pointer to a pin
  842. * @ops: ops for a dpll pin
  843. * @priv: pointer to private information of owner
  844. *
  845. * Register a pin with a parent pin, create references between them and
  846. * between newly registered pin and dplls connected with a parent pin.
  847. *
  848. * Context: Acquires a lock (dpll_lock)
  849. * Return:
  850. * * 0 on success
  851. * * negative - error value
  852. */
  853. int dpll_pin_on_pin_register(struct dpll_pin *parent, struct dpll_pin *pin,
  854. const struct dpll_pin_ops *ops, void *priv)
  855. {
  856. struct dpll_pin_ref *ref;
  857. unsigned long i, stop;
  858. int ret;
  859. if (WARN_ON(parent->prop.type != DPLL_PIN_TYPE_MUX))
  860. return -EINVAL;
  861. if (WARN_ON(!ops) ||
  862. WARN_ON(!ops->state_on_pin_get) ||
  863. WARN_ON(!ops->direction_get))
  864. return -EINVAL;
  865. mutex_lock(&dpll_lock);
  866. ret = dpll_xa_ref_pin_add(&pin->parent_refs, parent, ops, priv, pin);
  867. if (ret)
  868. goto unlock;
  869. xa_for_each(&parent->dpll_refs, i, ref) {
  870. ret = __dpll_pin_register(ref->dpll, pin, ops, priv, parent);
  871. if (ret) {
  872. stop = i;
  873. goto dpll_unregister;
  874. }
  875. dpll_pin_create_ntf(pin);
  876. }
  877. mutex_unlock(&dpll_lock);
  878. return ret;
  879. dpll_unregister:
  880. xa_for_each(&parent->dpll_refs, i, ref)
  881. if (i < stop) {
  882. __dpll_pin_unregister(ref->dpll, pin, ops, priv,
  883. parent);
  884. dpll_pin_delete_ntf(pin);
  885. }
  886. dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv, pin);
  887. unlock:
  888. mutex_unlock(&dpll_lock);
  889. return ret;
  890. }
  891. EXPORT_SYMBOL_GPL(dpll_pin_on_pin_register);
  892. /**
  893. * dpll_pin_on_pin_unregister - unregister dpll pin from a parent pin
  894. * @parent: pointer to a parent pin
  895. * @pin: pointer to a pin
  896. * @ops: ops for a dpll pin
  897. * @priv: pointer to private information of owner
  898. *
  899. * Context: Acquires a lock (dpll_lock)
  900. * Note: It does not free the memory
  901. */
  902. void dpll_pin_on_pin_unregister(struct dpll_pin *parent, struct dpll_pin *pin,
  903. const struct dpll_pin_ops *ops, void *priv)
  904. {
  905. struct dpll_pin_ref *ref;
  906. unsigned long i;
  907. mutex_lock(&dpll_lock);
  908. dpll_pin_delete_ntf(pin);
  909. dpll_xa_ref_pin_del(&pin->parent_refs, parent, ops, priv, pin);
  910. xa_for_each(&pin->dpll_refs, i, ref)
  911. __dpll_pin_unregister(ref->dpll, pin, ops, priv, parent);
  912. mutex_unlock(&dpll_lock);
  913. }
  914. EXPORT_SYMBOL_GPL(dpll_pin_on_pin_unregister);
  915. /**
  916. * dpll_pin_ref_sync_pair_add - create a reference sync signal pin pair
  917. * @pin: pin which produces the base frequency
  918. * @ref_sync_pin: pin which produces the sync signal
  919. *
  920. * Once pins are paired, the user-space configuration of reference sync pair
  921. * is possible.
  922. * Context: Acquires a lock (dpll_lock)
  923. * Return:
  924. * * 0 on success
  925. * * negative - error value
  926. */
  927. int dpll_pin_ref_sync_pair_add(struct dpll_pin *pin,
  928. struct dpll_pin *ref_sync_pin)
  929. {
  930. int ret;
  931. mutex_lock(&dpll_lock);
  932. ret = xa_insert(&pin->ref_sync_pins, ref_sync_pin->id,
  933. ref_sync_pin, GFP_KERNEL);
  934. __dpll_pin_change_ntf(pin);
  935. mutex_unlock(&dpll_lock);
  936. return ret;
  937. }
  938. EXPORT_SYMBOL_GPL(dpll_pin_ref_sync_pair_add);
  939. static struct dpll_device_registration *
  940. dpll_device_registration_first(struct dpll_device *dpll)
  941. {
  942. struct dpll_device_registration *reg;
  943. reg = list_first_entry_or_null((struct list_head *)&dpll->registration_list,
  944. struct dpll_device_registration, list);
  945. WARN_ON(!reg);
  946. return reg;
  947. }
  948. void *dpll_priv(struct dpll_device *dpll)
  949. {
  950. struct dpll_device_registration *reg;
  951. reg = dpll_device_registration_first(dpll);
  952. return reg->priv;
  953. }
  954. const struct dpll_device_ops *dpll_device_ops(struct dpll_device *dpll)
  955. {
  956. struct dpll_device_registration *reg;
  957. reg = dpll_device_registration_first(dpll);
  958. return reg->ops;
  959. }
  960. static struct dpll_pin_registration *
  961. dpll_pin_registration_first(struct dpll_pin_ref *ref)
  962. {
  963. struct dpll_pin_registration *reg;
  964. reg = list_first_entry_or_null(&ref->registration_list,
  965. struct dpll_pin_registration, list);
  966. WARN_ON(!reg);
  967. return reg;
  968. }
  969. void *dpll_pin_on_dpll_priv(struct dpll_device *dpll,
  970. struct dpll_pin *pin)
  971. {
  972. struct dpll_pin_registration *reg;
  973. struct dpll_pin_ref *ref;
  974. ref = xa_load(&dpll->pin_refs, pin->pin_idx);
  975. if (!ref)
  976. return NULL;
  977. reg = dpll_pin_registration_first(ref);
  978. return reg->priv;
  979. }
  980. void *dpll_pin_on_pin_priv(struct dpll_pin *parent,
  981. struct dpll_pin *pin)
  982. {
  983. struct dpll_pin_registration *reg;
  984. struct dpll_pin_ref *ref;
  985. ref = xa_load(&pin->parent_refs, parent->pin_idx);
  986. if (!ref)
  987. return NULL;
  988. reg = dpll_pin_registration_first(ref);
  989. return reg->priv;
  990. }
  991. const struct dpll_pin_ops *dpll_pin_ops(struct dpll_pin_ref *ref)
  992. {
  993. struct dpll_pin_registration *reg;
  994. reg = dpll_pin_registration_first(ref);
  995. return reg->ops;
  996. }
  997. static int __init dpll_init(void)
  998. {
  999. int ret;
  1000. ret = genl_register_family(&dpll_nl_family);
  1001. if (ret)
  1002. goto error;
  1003. return 0;
  1004. error:
  1005. mutex_destroy(&dpll_lock);
  1006. return ret;
  1007. }
  1008. static void __exit dpll_exit(void)
  1009. {
  1010. genl_unregister_family(&dpll_nl_family);
  1011. mutex_destroy(&dpll_lock);
  1012. }
  1013. subsys_initcall(dpll_init);
  1014. module_exit(dpll_exit);