dprc-driver.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Freescale data path resource container (DPRC) driver
  4. *
  5. * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
  6. * Author: German Rivera <German.Rivera@freescale.com>
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/msi.h>
  13. #include <linux/fsl/mc.h>
  14. #include "fsl-mc-private.h"
  15. #define FSL_MC_DPRC_DRIVER_NAME "fsl_mc_dprc"
  16. struct fsl_mc_child_objs {
  17. int child_count;
  18. struct fsl_mc_obj_desc *child_array;
  19. };
  20. static bool fsl_mc_device_match(struct fsl_mc_device *mc_dev,
  21. struct fsl_mc_obj_desc *obj_desc)
  22. {
  23. return mc_dev->obj_desc.id == obj_desc->id &&
  24. strcmp(mc_dev->obj_desc.type, obj_desc->type) == 0;
  25. }
  26. static int __fsl_mc_device_remove_if_not_in_mc(struct device *dev, void *data)
  27. {
  28. int i;
  29. struct fsl_mc_child_objs *objs;
  30. struct fsl_mc_device *mc_dev;
  31. mc_dev = to_fsl_mc_device(dev);
  32. objs = data;
  33. for (i = 0; i < objs->child_count; i++) {
  34. struct fsl_mc_obj_desc *obj_desc = &objs->child_array[i];
  35. if (strlen(obj_desc->type) != 0 &&
  36. fsl_mc_device_match(mc_dev, obj_desc))
  37. break;
  38. }
  39. if (i == objs->child_count)
  40. fsl_mc_device_remove(mc_dev);
  41. return 0;
  42. }
  43. static int __fsl_mc_device_remove(struct device *dev, void *data)
  44. {
  45. fsl_mc_device_remove(to_fsl_mc_device(dev));
  46. return 0;
  47. }
  48. /**
  49. * dprc_remove_devices - Removes devices for objects removed from a DPRC
  50. *
  51. * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
  52. * @obj_desc_array: array of object descriptors for child objects currently
  53. * present in the DPRC in the MC.
  54. * @num_child_objects_in_mc: number of entries in obj_desc_array
  55. *
  56. * Synchronizes the state of the Linux bus driver with the actual state of
  57. * the MC by removing devices that represent MC objects that have
  58. * been dynamically removed in the physical DPRC.
  59. */
  60. static void dprc_remove_devices(struct fsl_mc_device *mc_bus_dev,
  61. struct fsl_mc_obj_desc *obj_desc_array,
  62. int num_child_objects_in_mc)
  63. {
  64. if (num_child_objects_in_mc != 0) {
  65. /*
  66. * Remove child objects that are in the DPRC in Linux,
  67. * but not in the MC:
  68. */
  69. struct fsl_mc_child_objs objs;
  70. objs.child_count = num_child_objects_in_mc;
  71. objs.child_array = obj_desc_array;
  72. device_for_each_child(&mc_bus_dev->dev, &objs,
  73. __fsl_mc_device_remove_if_not_in_mc);
  74. } else {
  75. /*
  76. * There are no child objects for this DPRC in the MC.
  77. * So, remove all the child devices from Linux:
  78. */
  79. device_for_each_child(&mc_bus_dev->dev, NULL,
  80. __fsl_mc_device_remove);
  81. }
  82. }
  83. static int __fsl_mc_device_match(struct device *dev, void *data)
  84. {
  85. struct fsl_mc_obj_desc *obj_desc = data;
  86. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  87. return fsl_mc_device_match(mc_dev, obj_desc);
  88. }
  89. static struct fsl_mc_device *fsl_mc_device_lookup(struct fsl_mc_obj_desc
  90. *obj_desc,
  91. struct fsl_mc_device
  92. *mc_bus_dev)
  93. {
  94. struct device *dev;
  95. dev = device_find_child(&mc_bus_dev->dev, obj_desc,
  96. __fsl_mc_device_match);
  97. return dev ? to_fsl_mc_device(dev) : NULL;
  98. }
  99. /**
  100. * check_plugged_state_change - Check change in an MC object's plugged state
  101. *
  102. * @mc_dev: pointer to the fsl-mc device for a given MC object
  103. * @obj_desc: pointer to the MC object's descriptor in the MC
  104. *
  105. * If the plugged state has changed from unplugged to plugged, the fsl-mc
  106. * device is bound to the corresponding device driver.
  107. * If the plugged state has changed from plugged to unplugged, the fsl-mc
  108. * device is unbound from the corresponding device driver.
  109. */
  110. static void check_plugged_state_change(struct fsl_mc_device *mc_dev,
  111. struct fsl_mc_obj_desc *obj_desc)
  112. {
  113. int error;
  114. u32 plugged_flag_at_mc =
  115. obj_desc->state & FSL_MC_OBJ_STATE_PLUGGED;
  116. if (plugged_flag_at_mc !=
  117. (mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED)) {
  118. if (plugged_flag_at_mc) {
  119. mc_dev->obj_desc.state |= FSL_MC_OBJ_STATE_PLUGGED;
  120. error = device_attach(&mc_dev->dev);
  121. if (error < 0) {
  122. dev_err(&mc_dev->dev,
  123. "device_attach() failed: %d\n",
  124. error);
  125. }
  126. } else {
  127. mc_dev->obj_desc.state &= ~FSL_MC_OBJ_STATE_PLUGGED;
  128. device_release_driver(&mc_dev->dev);
  129. }
  130. }
  131. }
  132. /**
  133. * dprc_add_new_devices - Adds devices to the logical bus for a DPRC
  134. *
  135. * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
  136. * @obj_desc_array: array of device descriptors for child devices currently
  137. * present in the physical DPRC.
  138. * @num_child_objects_in_mc: number of entries in obj_desc_array
  139. *
  140. * Synchronizes the state of the Linux bus driver with the actual
  141. * state of the MC by adding objects that have been newly discovered
  142. * in the physical DPRC.
  143. */
  144. static void dprc_add_new_devices(struct fsl_mc_device *mc_bus_dev,
  145. struct fsl_mc_obj_desc *obj_desc_array,
  146. int num_child_objects_in_mc)
  147. {
  148. int error;
  149. int i;
  150. for (i = 0; i < num_child_objects_in_mc; i++) {
  151. struct fsl_mc_device *child_dev;
  152. struct fsl_mc_obj_desc *obj_desc = &obj_desc_array[i];
  153. if (strlen(obj_desc->type) == 0)
  154. continue;
  155. /*
  156. * Check if device is already known to Linux:
  157. */
  158. child_dev = fsl_mc_device_lookup(obj_desc, mc_bus_dev);
  159. if (child_dev) {
  160. check_plugged_state_change(child_dev, obj_desc);
  161. put_device(&child_dev->dev);
  162. continue;
  163. }
  164. error = fsl_mc_device_add(obj_desc, NULL, &mc_bus_dev->dev,
  165. &child_dev);
  166. if (error < 0)
  167. continue;
  168. }
  169. }
  170. /**
  171. * dprc_scan_objects - Discover objects in a DPRC
  172. *
  173. * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
  174. * @total_irq_count: If argument is provided the function populates the
  175. * total number of IRQs created by objects in the DPRC.
  176. *
  177. * Detects objects added and removed from a DPRC and synchronizes the
  178. * state of the Linux bus driver, MC by adding and removing
  179. * devices accordingly.
  180. * Two types of devices can be found in a DPRC: allocatable objects (e.g.,
  181. * dpbp, dpmcp) and non-allocatable devices (e.g., dprc, dpni).
  182. * All allocatable devices needed to be probed before all non-allocatable
  183. * devices, to ensure that device drivers for non-allocatable
  184. * devices can allocate any type of allocatable devices.
  185. * That is, we need to ensure that the corresponding resource pools are
  186. * populated before they can get allocation requests from probe callbacks
  187. * of the device drivers for the non-allocatable devices.
  188. */
  189. static int dprc_scan_objects(struct fsl_mc_device *mc_bus_dev,
  190. unsigned int *total_irq_count)
  191. {
  192. int num_child_objects;
  193. int dprc_get_obj_failures;
  194. int error;
  195. unsigned int irq_count = mc_bus_dev->obj_desc.irq_count;
  196. struct fsl_mc_obj_desc *child_obj_desc_array = NULL;
  197. struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
  198. error = dprc_get_obj_count(mc_bus_dev->mc_io,
  199. 0,
  200. mc_bus_dev->mc_handle,
  201. &num_child_objects);
  202. if (error < 0) {
  203. dev_err(&mc_bus_dev->dev, "dprc_get_obj_count() failed: %d\n",
  204. error);
  205. return error;
  206. }
  207. if (num_child_objects != 0) {
  208. int i;
  209. child_obj_desc_array =
  210. devm_kmalloc_array(&mc_bus_dev->dev, num_child_objects,
  211. sizeof(*child_obj_desc_array),
  212. GFP_KERNEL);
  213. if (!child_obj_desc_array)
  214. return -ENOMEM;
  215. /*
  216. * Discover objects currently present in the physical DPRC:
  217. */
  218. dprc_get_obj_failures = 0;
  219. for (i = 0; i < num_child_objects; i++) {
  220. struct fsl_mc_obj_desc *obj_desc =
  221. &child_obj_desc_array[i];
  222. error = dprc_get_obj(mc_bus_dev->mc_io,
  223. 0,
  224. mc_bus_dev->mc_handle,
  225. i, obj_desc);
  226. if (error < 0) {
  227. dev_err(&mc_bus_dev->dev,
  228. "dprc_get_obj(i=%d) failed: %d\n",
  229. i, error);
  230. /*
  231. * Mark the obj entry as "invalid", by using the
  232. * empty string as obj type:
  233. */
  234. obj_desc->type[0] = '\0';
  235. obj_desc->id = error;
  236. dprc_get_obj_failures++;
  237. continue;
  238. }
  239. /*
  240. * add a quirk for all versions of dpsec < 4.0...none
  241. * are coherent regardless of what the MC reports.
  242. */
  243. if ((strcmp(obj_desc->type, "dpseci") == 0) &&
  244. (obj_desc->ver_major < 4))
  245. obj_desc->flags |=
  246. FSL_MC_OBJ_FLAG_NO_MEM_SHAREABILITY;
  247. irq_count += obj_desc->irq_count;
  248. dev_dbg(&mc_bus_dev->dev,
  249. "Discovered object: type %s, id %d\n",
  250. obj_desc->type, obj_desc->id);
  251. }
  252. if (dprc_get_obj_failures != 0) {
  253. dev_err(&mc_bus_dev->dev,
  254. "%d out of %d devices could not be retrieved\n",
  255. dprc_get_obj_failures, num_child_objects);
  256. }
  257. }
  258. /*
  259. * Allocate IRQ's before binding the scanned devices with their
  260. * respective drivers.
  261. */
  262. if (dev_get_msi_domain(&mc_bus_dev->dev) && !mc_bus->irq_resources) {
  263. if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
  264. dev_warn(&mc_bus_dev->dev,
  265. "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
  266. irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
  267. }
  268. error = fsl_mc_populate_irq_pool(mc_bus,
  269. FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
  270. if (error < 0)
  271. return error;
  272. }
  273. if (total_irq_count)
  274. *total_irq_count = irq_count;
  275. dprc_remove_devices(mc_bus_dev, child_obj_desc_array,
  276. num_child_objects);
  277. dprc_add_new_devices(mc_bus_dev, child_obj_desc_array,
  278. num_child_objects);
  279. if (child_obj_desc_array)
  280. devm_kfree(&mc_bus_dev->dev, child_obj_desc_array);
  281. return 0;
  282. }
  283. /**
  284. * dprc_scan_container - Scans a physical DPRC and synchronizes Linux bus state
  285. *
  286. * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
  287. *
  288. * Scans the physical DPRC and synchronizes the state of the Linux
  289. * bus driver with the actual state of the MC by adding and removing
  290. * devices as appropriate.
  291. */
  292. static int dprc_scan_container(struct fsl_mc_device *mc_bus_dev)
  293. {
  294. int error;
  295. struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
  296. fsl_mc_init_all_resource_pools(mc_bus_dev);
  297. /*
  298. * Discover objects in the DPRC:
  299. */
  300. mutex_lock(&mc_bus->scan_mutex);
  301. error = dprc_scan_objects(mc_bus_dev, NULL);
  302. mutex_unlock(&mc_bus->scan_mutex);
  303. if (error < 0) {
  304. fsl_mc_cleanup_all_resource_pools(mc_bus_dev);
  305. return error;
  306. }
  307. return 0;
  308. }
  309. /**
  310. * dprc_irq0_handler - Regular ISR for DPRC interrupt 0
  311. *
  312. * @irq: IRQ number of the interrupt being handled
  313. * @arg: Pointer to device structure
  314. */
  315. static irqreturn_t dprc_irq0_handler(int irq_num, void *arg)
  316. {
  317. return IRQ_WAKE_THREAD;
  318. }
  319. /**
  320. * dprc_irq0_handler_thread - Handler thread function for DPRC interrupt 0
  321. *
  322. * @irq: IRQ number of the interrupt being handled
  323. * @arg: Pointer to device structure
  324. */
  325. static irqreturn_t dprc_irq0_handler_thread(int irq_num, void *arg)
  326. {
  327. int error;
  328. u32 status;
  329. struct device *dev = arg;
  330. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  331. struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
  332. struct fsl_mc_io *mc_io = mc_dev->mc_io;
  333. struct msi_desc *msi_desc = mc_dev->irqs[0]->msi_desc;
  334. dev_dbg(dev, "DPRC IRQ %d triggered on CPU %u\n",
  335. irq_num, smp_processor_id());
  336. if (!(mc_dev->flags & FSL_MC_IS_DPRC))
  337. return IRQ_HANDLED;
  338. mutex_lock(&mc_bus->scan_mutex);
  339. if (!msi_desc || msi_desc->irq != (u32)irq_num)
  340. goto out;
  341. status = 0;
  342. error = dprc_get_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
  343. &status);
  344. if (error < 0) {
  345. dev_err(dev,
  346. "dprc_get_irq_status() failed: %d\n", error);
  347. goto out;
  348. }
  349. error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
  350. status);
  351. if (error < 0) {
  352. dev_err(dev,
  353. "dprc_clear_irq_status() failed: %d\n", error);
  354. goto out;
  355. }
  356. if (status & (DPRC_IRQ_EVENT_OBJ_ADDED |
  357. DPRC_IRQ_EVENT_OBJ_REMOVED |
  358. DPRC_IRQ_EVENT_CONTAINER_DESTROYED |
  359. DPRC_IRQ_EVENT_OBJ_DESTROYED |
  360. DPRC_IRQ_EVENT_OBJ_CREATED)) {
  361. unsigned int irq_count;
  362. error = dprc_scan_objects(mc_dev, &irq_count);
  363. if (error < 0) {
  364. /*
  365. * If the error is -ENXIO, we ignore it, as it indicates
  366. * that the object scan was aborted, as we detected that
  367. * an object was removed from the DPRC in the MC, while
  368. * we were scanning the DPRC.
  369. */
  370. if (error != -ENXIO) {
  371. dev_err(dev, "dprc_scan_objects() failed: %d\n",
  372. error);
  373. }
  374. goto out;
  375. }
  376. if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
  377. dev_warn(dev,
  378. "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
  379. irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
  380. }
  381. }
  382. out:
  383. mutex_unlock(&mc_bus->scan_mutex);
  384. return IRQ_HANDLED;
  385. }
  386. /*
  387. * Disable and clear interrupt for a given DPRC object
  388. */
  389. static int disable_dprc_irq(struct fsl_mc_device *mc_dev)
  390. {
  391. int error;
  392. struct fsl_mc_io *mc_io = mc_dev->mc_io;
  393. /*
  394. * Disable generation of interrupt, while we configure it:
  395. */
  396. error = dprc_set_irq_enable(mc_io, 0, mc_dev->mc_handle, 0, 0);
  397. if (error < 0) {
  398. dev_err(&mc_dev->dev,
  399. "Disabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
  400. error);
  401. return error;
  402. }
  403. /*
  404. * Disable all interrupt causes for the interrupt:
  405. */
  406. error = dprc_set_irq_mask(mc_io, 0, mc_dev->mc_handle, 0, 0x0);
  407. if (error < 0) {
  408. dev_err(&mc_dev->dev,
  409. "Disabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
  410. error);
  411. return error;
  412. }
  413. /*
  414. * Clear any leftover interrupts:
  415. */
  416. error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0, ~0x0U);
  417. if (error < 0) {
  418. dev_err(&mc_dev->dev,
  419. "Disabling DPRC IRQ failed: dprc_clear_irq_status() failed: %d\n",
  420. error);
  421. return error;
  422. }
  423. return 0;
  424. }
  425. static int register_dprc_irq_handler(struct fsl_mc_device *mc_dev)
  426. {
  427. int error;
  428. struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
  429. /*
  430. * NOTE: devm_request_threaded_irq() invokes the device-specific
  431. * function that programs the MSI physically in the device
  432. */
  433. error = devm_request_threaded_irq(&mc_dev->dev,
  434. irq->msi_desc->irq,
  435. dprc_irq0_handler,
  436. dprc_irq0_handler_thread,
  437. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  438. dev_name(&mc_dev->dev),
  439. &mc_dev->dev);
  440. if (error < 0) {
  441. dev_err(&mc_dev->dev,
  442. "devm_request_threaded_irq() failed: %d\n",
  443. error);
  444. return error;
  445. }
  446. return 0;
  447. }
  448. static int enable_dprc_irq(struct fsl_mc_device *mc_dev)
  449. {
  450. int error;
  451. /*
  452. * Enable all interrupt causes for the interrupt:
  453. */
  454. error = dprc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle, 0,
  455. ~0x0u);
  456. if (error < 0) {
  457. dev_err(&mc_dev->dev,
  458. "Enabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
  459. error);
  460. return error;
  461. }
  462. /*
  463. * Enable generation of the interrupt:
  464. */
  465. error = dprc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle, 0, 1);
  466. if (error < 0) {
  467. dev_err(&mc_dev->dev,
  468. "Enabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
  469. error);
  470. return error;
  471. }
  472. return 0;
  473. }
  474. /*
  475. * Setup interrupt for a given DPRC device
  476. */
  477. static int dprc_setup_irq(struct fsl_mc_device *mc_dev)
  478. {
  479. int error;
  480. error = fsl_mc_allocate_irqs(mc_dev);
  481. if (error < 0)
  482. return error;
  483. error = disable_dprc_irq(mc_dev);
  484. if (error < 0)
  485. goto error_free_irqs;
  486. error = register_dprc_irq_handler(mc_dev);
  487. if (error < 0)
  488. goto error_free_irqs;
  489. error = enable_dprc_irq(mc_dev);
  490. if (error < 0)
  491. goto error_free_irqs;
  492. return 0;
  493. error_free_irqs:
  494. fsl_mc_free_irqs(mc_dev);
  495. return error;
  496. }
  497. /**
  498. * dprc_probe - callback invoked when a DPRC is being bound to this driver
  499. *
  500. * @mc_dev: Pointer to fsl-mc device representing a DPRC
  501. *
  502. * It opens the physical DPRC in the MC.
  503. * It scans the DPRC to discover the MC objects contained in it.
  504. * It creates the interrupt pool for the MC bus associated with the DPRC.
  505. * It configures the interrupts for the DPRC device itself.
  506. */
  507. static int dprc_probe(struct fsl_mc_device *mc_dev)
  508. {
  509. int error;
  510. size_t region_size;
  511. struct device *parent_dev = mc_dev->dev.parent;
  512. struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
  513. bool mc_io_created = false;
  514. bool msi_domain_set = false;
  515. u16 major_ver, minor_ver;
  516. if (!is_fsl_mc_bus_dprc(mc_dev))
  517. return -EINVAL;
  518. if (dev_get_msi_domain(&mc_dev->dev))
  519. return -EINVAL;
  520. if (!mc_dev->mc_io) {
  521. /*
  522. * This is a child DPRC:
  523. */
  524. if (!dev_is_fsl_mc(parent_dev))
  525. return -EINVAL;
  526. if (mc_dev->obj_desc.region_count == 0)
  527. return -EINVAL;
  528. region_size = resource_size(mc_dev->regions);
  529. error = fsl_create_mc_io(&mc_dev->dev,
  530. mc_dev->regions[0].start,
  531. region_size,
  532. NULL,
  533. FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
  534. &mc_dev->mc_io);
  535. if (error < 0)
  536. return error;
  537. mc_io_created = true;
  538. /*
  539. * Inherit parent MSI domain:
  540. */
  541. dev_set_msi_domain(&mc_dev->dev,
  542. dev_get_msi_domain(parent_dev));
  543. msi_domain_set = true;
  544. } else {
  545. /*
  546. * This is a root DPRC
  547. */
  548. struct irq_domain *mc_msi_domain;
  549. if (dev_is_fsl_mc(parent_dev))
  550. return -EINVAL;
  551. error = fsl_mc_find_msi_domain(parent_dev,
  552. &mc_msi_domain);
  553. if (error < 0) {
  554. dev_warn(&mc_dev->dev,
  555. "WARNING: MC bus without interrupt support\n");
  556. } else {
  557. dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
  558. msi_domain_set = true;
  559. }
  560. }
  561. error = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
  562. &mc_dev->mc_handle);
  563. if (error < 0) {
  564. dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", error);
  565. goto error_cleanup_msi_domain;
  566. }
  567. error = dprc_get_attributes(mc_dev->mc_io, 0, mc_dev->mc_handle,
  568. &mc_bus->dprc_attr);
  569. if (error < 0) {
  570. dev_err(&mc_dev->dev, "dprc_get_attributes() failed: %d\n",
  571. error);
  572. goto error_cleanup_open;
  573. }
  574. error = dprc_get_api_version(mc_dev->mc_io, 0,
  575. &major_ver,
  576. &minor_ver);
  577. if (error < 0) {
  578. dev_err(&mc_dev->dev, "dprc_get_api_version() failed: %d\n",
  579. error);
  580. goto error_cleanup_open;
  581. }
  582. if (major_ver < DPRC_MIN_VER_MAJOR ||
  583. (major_ver == DPRC_MIN_VER_MAJOR &&
  584. minor_ver < DPRC_MIN_VER_MINOR)) {
  585. dev_err(&mc_dev->dev,
  586. "ERROR: DPRC version %d.%d not supported\n",
  587. major_ver, minor_ver);
  588. error = -ENOTSUPP;
  589. goto error_cleanup_open;
  590. }
  591. mutex_init(&mc_bus->scan_mutex);
  592. /*
  593. * Discover MC objects in DPRC object:
  594. */
  595. error = dprc_scan_container(mc_dev);
  596. if (error < 0)
  597. goto error_cleanup_open;
  598. /*
  599. * Configure interrupt for the DPRC object associated with this MC bus:
  600. */
  601. error = dprc_setup_irq(mc_dev);
  602. if (error < 0)
  603. goto error_cleanup_open;
  604. dev_info(&mc_dev->dev, "DPRC device bound to driver");
  605. return 0;
  606. error_cleanup_open:
  607. (void)dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
  608. error_cleanup_msi_domain:
  609. if (msi_domain_set)
  610. dev_set_msi_domain(&mc_dev->dev, NULL);
  611. if (mc_io_created) {
  612. fsl_destroy_mc_io(mc_dev->mc_io);
  613. mc_dev->mc_io = NULL;
  614. }
  615. return error;
  616. }
  617. /*
  618. * Tear down interrupt for a given DPRC object
  619. */
  620. static void dprc_teardown_irq(struct fsl_mc_device *mc_dev)
  621. {
  622. struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
  623. (void)disable_dprc_irq(mc_dev);
  624. devm_free_irq(&mc_dev->dev, irq->msi_desc->irq, &mc_dev->dev);
  625. fsl_mc_free_irqs(mc_dev);
  626. }
  627. /**
  628. * dprc_remove - callback invoked when a DPRC is being unbound from this driver
  629. *
  630. * @mc_dev: Pointer to fsl-mc device representing the DPRC
  631. *
  632. * It removes the DPRC's child objects from Linux (not from the MC) and
  633. * closes the DPRC device in the MC.
  634. * It tears down the interrupts that were configured for the DPRC device.
  635. * It destroys the interrupt pool associated with this MC bus.
  636. */
  637. static int dprc_remove(struct fsl_mc_device *mc_dev)
  638. {
  639. int error;
  640. struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
  641. if (!is_fsl_mc_bus_dprc(mc_dev))
  642. return -EINVAL;
  643. if (!mc_dev->mc_io)
  644. return -EINVAL;
  645. if (!mc_bus->irq_resources)
  646. return -EINVAL;
  647. if (dev_get_msi_domain(&mc_dev->dev))
  648. dprc_teardown_irq(mc_dev);
  649. device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
  650. if (dev_get_msi_domain(&mc_dev->dev)) {
  651. fsl_mc_cleanup_irq_pool(mc_bus);
  652. dev_set_msi_domain(&mc_dev->dev, NULL);
  653. }
  654. fsl_mc_cleanup_all_resource_pools(mc_dev);
  655. error = dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
  656. if (error < 0)
  657. dev_err(&mc_dev->dev, "dprc_close() failed: %d\n", error);
  658. if (!fsl_mc_is_root_dprc(&mc_dev->dev)) {
  659. fsl_destroy_mc_io(mc_dev->mc_io);
  660. mc_dev->mc_io = NULL;
  661. }
  662. dev_info(&mc_dev->dev, "DPRC device unbound from driver");
  663. return 0;
  664. }
  665. static const struct fsl_mc_device_id match_id_table[] = {
  666. {
  667. .vendor = FSL_MC_VENDOR_FREESCALE,
  668. .obj_type = "dprc"},
  669. {.vendor = 0x0},
  670. };
  671. static struct fsl_mc_driver dprc_driver = {
  672. .driver = {
  673. .name = FSL_MC_DPRC_DRIVER_NAME,
  674. .owner = THIS_MODULE,
  675. .pm = NULL,
  676. },
  677. .match_id_table = match_id_table,
  678. .probe = dprc_probe,
  679. .remove = dprc_remove,
  680. };
  681. int __init dprc_driver_init(void)
  682. {
  683. return fsl_mc_driver_register(&dprc_driver);
  684. }
  685. void dprc_driver_exit(void)
  686. {
  687. fsl_mc_driver_unregister(&dprc_driver);
  688. }