dprc-driver.c 22 KB

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