dprc-driver.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Freescale data path resource container (DPRC) driver
  3. *
  4. * Copyright (C) 2014 Freescale Semiconductor, Inc.
  5. * Author: German Rivera <German.Rivera@freescale.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include "../include/mc-private.h"
  12. #include "../include/mc-sys.h"
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include "dprc-cmd.h"
  16. struct dprc_child_objs {
  17. int child_count;
  18. struct dprc_obj_desc *child_array;
  19. };
  20. static int __fsl_mc_device_remove_if_not_in_mc(struct device *dev, void *data)
  21. {
  22. int i;
  23. struct dprc_child_objs *objs;
  24. struct fsl_mc_device *mc_dev;
  25. WARN_ON(!dev);
  26. WARN_ON(!data);
  27. mc_dev = to_fsl_mc_device(dev);
  28. objs = data;
  29. for (i = 0; i < objs->child_count; i++) {
  30. struct dprc_obj_desc *obj_desc = &objs->child_array[i];
  31. if (strlen(obj_desc->type) != 0 &&
  32. FSL_MC_DEVICE_MATCH(mc_dev, obj_desc))
  33. break;
  34. }
  35. if (i == objs->child_count)
  36. fsl_mc_device_remove(mc_dev);
  37. return 0;
  38. }
  39. static int __fsl_mc_device_remove(struct device *dev, void *data)
  40. {
  41. WARN_ON(!dev);
  42. WARN_ON(data);
  43. fsl_mc_device_remove(to_fsl_mc_device(dev));
  44. return 0;
  45. }
  46. /**
  47. * dprc_remove_devices - Removes devices for objects removed from a DPRC
  48. *
  49. * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
  50. * @obj_desc_array: array of object descriptors for child objects currently
  51. * present in the DPRC in the MC.
  52. * @num_child_objects_in_mc: number of entries in obj_desc_array
  53. *
  54. * Synchronizes the state of the Linux bus driver with the actual state of
  55. * the MC by removing devices that represent MC objects that have
  56. * been dynamically removed in the physical DPRC.
  57. */
  58. static void dprc_remove_devices(struct fsl_mc_device *mc_bus_dev,
  59. struct dprc_obj_desc *obj_desc_array,
  60. int num_child_objects_in_mc)
  61. {
  62. if (num_child_objects_in_mc != 0) {
  63. /*
  64. * Remove child objects that are in the DPRC in Linux,
  65. * but not in the MC:
  66. */
  67. struct dprc_child_objs objs;
  68. objs.child_count = num_child_objects_in_mc;
  69. objs.child_array = obj_desc_array;
  70. device_for_each_child(&mc_bus_dev->dev, &objs,
  71. __fsl_mc_device_remove_if_not_in_mc);
  72. } else {
  73. /*
  74. * There are no child objects for this DPRC in the MC.
  75. * So, remove all the child devices from Linux:
  76. */
  77. device_for_each_child(&mc_bus_dev->dev, NULL,
  78. __fsl_mc_device_remove);
  79. }
  80. }
  81. static int __fsl_mc_device_match(struct device *dev, void *data)
  82. {
  83. struct dprc_obj_desc *obj_desc = data;
  84. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  85. return FSL_MC_DEVICE_MATCH(mc_dev, obj_desc);
  86. }
  87. static struct fsl_mc_device *fsl_mc_device_lookup(struct dprc_obj_desc
  88. *obj_desc,
  89. struct fsl_mc_device
  90. *mc_bus_dev)
  91. {
  92. struct device *dev;
  93. dev = device_find_child(&mc_bus_dev->dev, obj_desc,
  94. __fsl_mc_device_match);
  95. return dev ? to_fsl_mc_device(dev) : NULL;
  96. }
  97. /**
  98. * check_plugged_state_change - Check change in an MC object's plugged state
  99. *
  100. * @mc_dev: pointer to the fsl-mc device for a given MC object
  101. * @obj_desc: pointer to the MC object's descriptor in the MC
  102. *
  103. * If the plugged state has changed from unplugged to plugged, the fsl-mc
  104. * device is bound to the corresponding device driver.
  105. * If the plugged state has changed from plugged to unplugged, the fsl-mc
  106. * device is unbound from the corresponding device driver.
  107. */
  108. static void check_plugged_state_change(struct fsl_mc_device *mc_dev,
  109. struct dprc_obj_desc *obj_desc)
  110. {
  111. int error;
  112. u32 plugged_flag_at_mc =
  113. (obj_desc->state & DPRC_OBJ_STATE_PLUGGED);
  114. if (plugged_flag_at_mc !=
  115. (mc_dev->obj_desc.state & DPRC_OBJ_STATE_PLUGGED)) {
  116. if (plugged_flag_at_mc) {
  117. mc_dev->obj_desc.state |= DPRC_OBJ_STATE_PLUGGED;
  118. error = device_attach(&mc_dev->dev);
  119. if (error < 0) {
  120. dev_err(&mc_dev->dev,
  121. "device_attach() failed: %d\n",
  122. error);
  123. }
  124. } else {
  125. mc_dev->obj_desc.state &= ~DPRC_OBJ_STATE_PLUGGED;
  126. device_release_driver(&mc_dev->dev);
  127. }
  128. }
  129. }
  130. /**
  131. * dprc_add_new_devices - Adds devices to the logical bus for a DPRC
  132. *
  133. * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
  134. * @obj_desc_array: array of device descriptors for child devices currently
  135. * present in the physical DPRC.
  136. * @num_child_objects_in_mc: number of entries in obj_desc_array
  137. *
  138. * Synchronizes the state of the Linux bus driver with the actual
  139. * state of the MC by adding objects that have been newly discovered
  140. * in the physical DPRC.
  141. */
  142. static void dprc_add_new_devices(struct fsl_mc_device *mc_bus_dev,
  143. struct dprc_obj_desc *obj_desc_array,
  144. int num_child_objects_in_mc)
  145. {
  146. int error;
  147. int i;
  148. for (i = 0; i < num_child_objects_in_mc; i++) {
  149. struct fsl_mc_device *child_dev;
  150. struct dprc_obj_desc *obj_desc = &obj_desc_array[i];
  151. if (strlen(obj_desc->type) == 0)
  152. continue;
  153. /*
  154. * Check if device is already known to Linux:
  155. */
  156. child_dev = fsl_mc_device_lookup(obj_desc, mc_bus_dev);
  157. if (child_dev) {
  158. check_plugged_state_change(child_dev, obj_desc);
  159. continue;
  160. }
  161. error = fsl_mc_device_add(obj_desc, NULL, &mc_bus_dev->dev,
  162. &child_dev);
  163. if (error < 0)
  164. continue;
  165. }
  166. }
  167. static void dprc_init_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
  168. {
  169. int pool_type;
  170. struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
  171. for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++) {
  172. struct fsl_mc_resource_pool *res_pool =
  173. &mc_bus->resource_pools[pool_type];
  174. res_pool->type = pool_type;
  175. res_pool->max_count = 0;
  176. res_pool->free_count = 0;
  177. res_pool->mc_bus = mc_bus;
  178. INIT_LIST_HEAD(&res_pool->free_list);
  179. mutex_init(&res_pool->mutex);
  180. }
  181. }
  182. static void dprc_cleanup_resource_pool(struct fsl_mc_device *mc_bus_dev,
  183. enum fsl_mc_pool_type pool_type)
  184. {
  185. struct fsl_mc_resource *resource;
  186. struct fsl_mc_resource *next;
  187. struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
  188. struct fsl_mc_resource_pool *res_pool =
  189. &mc_bus->resource_pools[pool_type];
  190. int free_count = 0;
  191. WARN_ON(res_pool->type != pool_type);
  192. WARN_ON(res_pool->free_count != res_pool->max_count);
  193. list_for_each_entry_safe(resource, next, &res_pool->free_list, node) {
  194. free_count++;
  195. WARN_ON(resource->type != res_pool->type);
  196. WARN_ON(resource->parent_pool != res_pool);
  197. devm_kfree(&mc_bus_dev->dev, resource);
  198. }
  199. WARN_ON(free_count != res_pool->free_count);
  200. }
  201. static void dprc_cleanup_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
  202. {
  203. int pool_type;
  204. for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++)
  205. dprc_cleanup_resource_pool(mc_bus_dev, pool_type);
  206. }
  207. /**
  208. * dprc_scan_objects - Discover objects in a DPRC
  209. *
  210. * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
  211. *
  212. * Detects objects added and removed from a DPRC and synchronizes the
  213. * state of the Linux bus driver, MC by adding and removing
  214. * devices accordingly.
  215. * Two types of devices can be found in a DPRC: allocatable objects (e.g.,
  216. * dpbp, dpmcp) and non-allocatable devices (e.g., dprc, dpni).
  217. * All allocatable devices needed to be probed before all non-allocatable
  218. * devices, to ensure that device drivers for non-allocatable
  219. * devices can allocate any type of allocatable devices.
  220. * That is, we need to ensure that the corresponding resource pools are
  221. * populated before they can get allocation requests from probe callbacks
  222. * of the device drivers for the non-allocatable devices.
  223. */
  224. int dprc_scan_objects(struct fsl_mc_device *mc_bus_dev)
  225. {
  226. int num_child_objects;
  227. int dprc_get_obj_failures;
  228. int error;
  229. struct dprc_obj_desc *child_obj_desc_array = NULL;
  230. error = dprc_get_obj_count(mc_bus_dev->mc_io,
  231. 0,
  232. mc_bus_dev->mc_handle,
  233. &num_child_objects);
  234. if (error < 0) {
  235. dev_err(&mc_bus_dev->dev, "dprc_get_obj_count() failed: %d\n",
  236. error);
  237. return error;
  238. }
  239. if (num_child_objects != 0) {
  240. int i;
  241. child_obj_desc_array =
  242. devm_kmalloc_array(&mc_bus_dev->dev, num_child_objects,
  243. sizeof(*child_obj_desc_array),
  244. GFP_KERNEL);
  245. if (!child_obj_desc_array)
  246. return -ENOMEM;
  247. /*
  248. * Discover objects currently present in the physical DPRC:
  249. */
  250. dprc_get_obj_failures = 0;
  251. for (i = 0; i < num_child_objects; i++) {
  252. struct dprc_obj_desc *obj_desc =
  253. &child_obj_desc_array[i];
  254. error = dprc_get_obj(mc_bus_dev->mc_io,
  255. 0,
  256. mc_bus_dev->mc_handle,
  257. i, obj_desc);
  258. if (error < 0) {
  259. dev_err(&mc_bus_dev->dev,
  260. "dprc_get_obj(i=%d) failed: %d\n",
  261. i, error);
  262. /*
  263. * Mark the obj entry as "invalid", by using the
  264. * empty string as obj type:
  265. */
  266. obj_desc->type[0] = '\0';
  267. obj_desc->id = error;
  268. dprc_get_obj_failures++;
  269. continue;
  270. }
  271. dev_dbg(&mc_bus_dev->dev,
  272. "Discovered object: type %s, id %d\n",
  273. obj_desc->type, obj_desc->id);
  274. }
  275. if (dprc_get_obj_failures != 0) {
  276. dev_err(&mc_bus_dev->dev,
  277. "%d out of %d devices could not be retrieved\n",
  278. dprc_get_obj_failures, num_child_objects);
  279. }
  280. }
  281. dprc_remove_devices(mc_bus_dev, child_obj_desc_array,
  282. num_child_objects);
  283. dprc_add_new_devices(mc_bus_dev, child_obj_desc_array,
  284. num_child_objects);
  285. if (child_obj_desc_array)
  286. devm_kfree(&mc_bus_dev->dev, child_obj_desc_array);
  287. return 0;
  288. }
  289. EXPORT_SYMBOL_GPL(dprc_scan_objects);
  290. /**
  291. * dprc_scan_container - Scans a physical DPRC and synchronizes Linux bus state
  292. *
  293. * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
  294. *
  295. * Scans the physical DPRC and synchronizes the state of the Linux
  296. * bus driver with the actual state of the MC by adding and removing
  297. * devices as appropriate.
  298. */
  299. int dprc_scan_container(struct fsl_mc_device *mc_bus_dev)
  300. {
  301. int error;
  302. struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
  303. dprc_init_all_resource_pools(mc_bus_dev);
  304. /*
  305. * Discover objects in the DPRC:
  306. */
  307. mutex_lock(&mc_bus->scan_mutex);
  308. error = dprc_scan_objects(mc_bus_dev);
  309. mutex_unlock(&mc_bus->scan_mutex);
  310. if (error < 0)
  311. goto error;
  312. return 0;
  313. error:
  314. dprc_cleanup_all_resource_pools(mc_bus_dev);
  315. return error;
  316. }
  317. EXPORT_SYMBOL_GPL(dprc_scan_container);
  318. /**
  319. * dprc_probe - callback invoked when a DPRC is being bound to this driver
  320. *
  321. * @mc_dev: Pointer to fsl-mc device representing a DPRC
  322. *
  323. * It opens the physical DPRC in the MC.
  324. * It scans the DPRC to discover the MC objects contained in it.
  325. * It creates the interrupt pool for the MC bus associated with the DPRC.
  326. * It configures the interrupts for the DPRC device itself.
  327. */
  328. static int dprc_probe(struct fsl_mc_device *mc_dev)
  329. {
  330. int error;
  331. size_t region_size;
  332. struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
  333. if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
  334. return -EINVAL;
  335. if (!mc_dev->mc_io) {
  336. /*
  337. * This is a child DPRC:
  338. */
  339. if (WARN_ON(mc_dev->obj_desc.region_count == 0))
  340. return -EINVAL;
  341. region_size = mc_dev->regions[0].end -
  342. mc_dev->regions[0].start + 1;
  343. error = fsl_create_mc_io(&mc_dev->dev,
  344. mc_dev->regions[0].start,
  345. region_size,
  346. NULL, 0, &mc_dev->mc_io);
  347. if (error < 0)
  348. return error;
  349. }
  350. error = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
  351. &mc_dev->mc_handle);
  352. if (error < 0) {
  353. dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", error);
  354. goto error_cleanup_mc_io;
  355. }
  356. mutex_init(&mc_bus->scan_mutex);
  357. /*
  358. * Discover MC objects in DPRC object:
  359. */
  360. error = dprc_scan_container(mc_dev);
  361. if (error < 0)
  362. goto error_cleanup_open;
  363. dev_info(&mc_dev->dev, "DPRC device bound to driver");
  364. return 0;
  365. error_cleanup_open:
  366. (void)dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
  367. error_cleanup_mc_io:
  368. fsl_destroy_mc_io(mc_dev->mc_io);
  369. return error;
  370. }
  371. /**
  372. * dprc_remove - callback invoked when a DPRC is being unbound from this driver
  373. *
  374. * @mc_dev: Pointer to fsl-mc device representing the DPRC
  375. *
  376. * It removes the DPRC's child objects from Linux (not from the MC) and
  377. * closes the DPRC device in the MC.
  378. * It tears down the interrupts that were configured for the DPRC device.
  379. * It destroys the interrupt pool associated with this MC bus.
  380. */
  381. static int dprc_remove(struct fsl_mc_device *mc_dev)
  382. {
  383. int error;
  384. if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
  385. return -EINVAL;
  386. if (WARN_ON(!mc_dev->mc_io))
  387. return -EINVAL;
  388. device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
  389. dprc_cleanup_all_resource_pools(mc_dev);
  390. error = dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
  391. if (error < 0)
  392. dev_err(&mc_dev->dev, "dprc_close() failed: %d\n", error);
  393. dev_info(&mc_dev->dev, "DPRC device unbound from driver");
  394. return 0;
  395. }
  396. static const struct fsl_mc_device_match_id match_id_table[] = {
  397. {
  398. .vendor = FSL_MC_VENDOR_FREESCALE,
  399. .obj_type = "dprc",
  400. .ver_major = DPRC_VER_MAJOR,
  401. .ver_minor = DPRC_VER_MINOR},
  402. {.vendor = 0x0},
  403. };
  404. static struct fsl_mc_driver dprc_driver = {
  405. .driver = {
  406. .name = FSL_MC_DPRC_DRIVER_NAME,
  407. .owner = THIS_MODULE,
  408. .pm = NULL,
  409. },
  410. .match_id_table = match_id_table,
  411. .probe = dprc_probe,
  412. .remove = dprc_remove,
  413. };
  414. int __init dprc_driver_init(void)
  415. {
  416. return fsl_mc_driver_register(&dprc_driver);
  417. }
  418. void dprc_driver_exit(void)
  419. {
  420. fsl_mc_driver_unregister(&dprc_driver);
  421. }