fsl-mc-bus.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Freescale Management Complex (MC) bus 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. #define pr_fmt(fmt) "fsl-mc: " fmt
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_address.h>
  14. #include <linux/ioport.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/limits.h>
  18. #include <linux/bitops.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/acpi.h>
  21. #include <linux/iommu.h>
  22. #include <linux/dma-map-ops.h>
  23. #include "fsl-mc-private.h"
  24. /*
  25. * Default DMA mask for devices on a fsl-mc bus
  26. */
  27. #define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
  28. static struct fsl_mc_version mc_version;
  29. /**
  30. * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
  31. * @root_mc_bus_dev: fsl-mc device representing the root DPRC
  32. * @num_translation_ranges: number of entries in addr_translation_ranges
  33. * @translation_ranges: array of bus to system address translation ranges
  34. * @fsl_mc_regs: base address of register bank
  35. */
  36. struct fsl_mc {
  37. struct fsl_mc_device *root_mc_bus_dev;
  38. u8 num_translation_ranges;
  39. struct fsl_mc_addr_translation_range *translation_ranges;
  40. void __iomem *fsl_mc_regs;
  41. };
  42. /**
  43. * struct fsl_mc_addr_translation_range - bus to system address translation
  44. * range
  45. * @mc_region_type: Type of MC region for the range being translated
  46. * @start_mc_offset: Start MC offset of the range being translated
  47. * @end_mc_offset: MC offset of the first byte after the range (last MC
  48. * offset of the range is end_mc_offset - 1)
  49. * @start_phys_addr: system physical address corresponding to start_mc_addr
  50. */
  51. struct fsl_mc_addr_translation_range {
  52. enum dprc_region_type mc_region_type;
  53. u64 start_mc_offset;
  54. u64 end_mc_offset;
  55. phys_addr_t start_phys_addr;
  56. };
  57. #define FSL_MC_GCR1 0x0
  58. #define GCR1_P1_STOP BIT(31)
  59. #define GCR1_P2_STOP BIT(30)
  60. #define FSL_MC_FAPR 0x28
  61. #define MC_FAPR_PL BIT(18)
  62. #define MC_FAPR_BMT BIT(17)
  63. static phys_addr_t mc_portal_base_phys_addr;
  64. /**
  65. * fsl_mc_bus_match - device to driver matching callback
  66. * @dev: the fsl-mc device to match against
  67. * @drv: the device driver to search for matching fsl-mc object type
  68. * structures
  69. *
  70. * Returns 1 on success, 0 otherwise.
  71. */
  72. static int fsl_mc_bus_match(struct device *dev, const struct device_driver *drv)
  73. {
  74. const struct fsl_mc_device_id *id;
  75. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  76. const struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
  77. bool found = false;
  78. /* When driver_override is set, only bind to the matching driver */
  79. if (mc_dev->driver_override) {
  80. found = !strcmp(mc_dev->driver_override, mc_drv->driver.name);
  81. goto out;
  82. }
  83. if (!mc_drv->match_id_table)
  84. goto out;
  85. /*
  86. * If the object is not 'plugged' don't match.
  87. * Only exception is the root DPRC, which is a special case.
  88. */
  89. if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
  90. !fsl_mc_is_root_dprc(&mc_dev->dev))
  91. goto out;
  92. /*
  93. * Traverse the match_id table of the given driver, trying to find
  94. * a matching for the given device.
  95. */
  96. for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
  97. if (id->vendor == mc_dev->obj_desc.vendor &&
  98. strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
  99. found = true;
  100. break;
  101. }
  102. }
  103. out:
  104. dev_dbg(dev, "%smatched\n", found ? "" : "not ");
  105. return found;
  106. }
  107. /*
  108. * fsl_mc_bus_uevent - callback invoked when a device is added
  109. */
  110. static int fsl_mc_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
  111. {
  112. const struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  113. if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
  114. mc_dev->obj_desc.vendor,
  115. mc_dev->obj_desc.type))
  116. return -ENOMEM;
  117. return 0;
  118. }
  119. static int fsl_mc_dma_configure(struct device *dev)
  120. {
  121. struct device *dma_dev = dev;
  122. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  123. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
  124. u32 input_id = mc_dev->icid;
  125. int ret;
  126. while (dev_is_fsl_mc(dma_dev))
  127. dma_dev = dma_dev->parent;
  128. if (dev_of_node(dma_dev))
  129. ret = of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
  130. else
  131. ret = acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
  132. if (!ret && !mc_drv->driver_managed_dma) {
  133. ret = iommu_device_use_default_domain(dev);
  134. if (ret)
  135. arch_teardown_dma_ops(dev);
  136. }
  137. return ret;
  138. }
  139. static void fsl_mc_dma_cleanup(struct device *dev)
  140. {
  141. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
  142. if (!mc_drv->driver_managed_dma)
  143. iommu_device_unuse_default_domain(dev);
  144. }
  145. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  146. char *buf)
  147. {
  148. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  149. return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
  150. mc_dev->obj_desc.type);
  151. }
  152. static DEVICE_ATTR_RO(modalias);
  153. static ssize_t driver_override_store(struct device *dev,
  154. struct device_attribute *attr,
  155. const char *buf, size_t count)
  156. {
  157. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  158. int ret;
  159. if (WARN_ON(dev->bus != &fsl_mc_bus_type))
  160. return -EINVAL;
  161. ret = driver_set_override(dev, &mc_dev->driver_override, buf, count);
  162. if (ret)
  163. return ret;
  164. return count;
  165. }
  166. static ssize_t driver_override_show(struct device *dev,
  167. struct device_attribute *attr, char *buf)
  168. {
  169. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  170. return snprintf(buf, PAGE_SIZE, "%s\n", mc_dev->driver_override);
  171. }
  172. static DEVICE_ATTR_RW(driver_override);
  173. static struct attribute *fsl_mc_dev_attrs[] = {
  174. &dev_attr_modalias.attr,
  175. &dev_attr_driver_override.attr,
  176. NULL,
  177. };
  178. ATTRIBUTE_GROUPS(fsl_mc_dev);
  179. static int scan_fsl_mc_bus(struct device *dev, void *data)
  180. {
  181. struct fsl_mc_device *root_mc_dev;
  182. struct fsl_mc_bus *root_mc_bus;
  183. if (!fsl_mc_is_root_dprc(dev))
  184. goto exit;
  185. root_mc_dev = to_fsl_mc_device(dev);
  186. root_mc_bus = to_fsl_mc_bus(root_mc_dev);
  187. mutex_lock(&root_mc_bus->scan_mutex);
  188. dprc_scan_objects(root_mc_dev, false);
  189. mutex_unlock(&root_mc_bus->scan_mutex);
  190. exit:
  191. return 0;
  192. }
  193. static ssize_t rescan_store(const struct bus_type *bus,
  194. const char *buf, size_t count)
  195. {
  196. unsigned long val;
  197. if (kstrtoul(buf, 0, &val) < 0)
  198. return -EINVAL;
  199. if (val)
  200. bus_for_each_dev(bus, NULL, NULL, scan_fsl_mc_bus);
  201. return count;
  202. }
  203. static BUS_ATTR_WO(rescan);
  204. static int fsl_mc_bus_set_autorescan(struct device *dev, void *data)
  205. {
  206. struct fsl_mc_device *root_mc_dev;
  207. unsigned long val;
  208. char *buf = data;
  209. if (!fsl_mc_is_root_dprc(dev))
  210. goto exit;
  211. root_mc_dev = to_fsl_mc_device(dev);
  212. if (kstrtoul(buf, 0, &val) < 0)
  213. return -EINVAL;
  214. if (val)
  215. enable_dprc_irq(root_mc_dev);
  216. else
  217. disable_dprc_irq(root_mc_dev);
  218. exit:
  219. return 0;
  220. }
  221. static int fsl_mc_bus_get_autorescan(struct device *dev, void *data)
  222. {
  223. struct fsl_mc_device *root_mc_dev;
  224. char *buf = data;
  225. if (!fsl_mc_is_root_dprc(dev))
  226. goto exit;
  227. root_mc_dev = to_fsl_mc_device(dev);
  228. sprintf(buf, "%d\n", get_dprc_irq_state(root_mc_dev));
  229. exit:
  230. return 0;
  231. }
  232. static ssize_t autorescan_store(const struct bus_type *bus,
  233. const char *buf, size_t count)
  234. {
  235. bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_set_autorescan);
  236. return count;
  237. }
  238. static ssize_t autorescan_show(const struct bus_type *bus, char *buf)
  239. {
  240. bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_get_autorescan);
  241. return strlen(buf);
  242. }
  243. static BUS_ATTR_RW(autorescan);
  244. static struct attribute *fsl_mc_bus_attrs[] = {
  245. &bus_attr_rescan.attr,
  246. &bus_attr_autorescan.attr,
  247. NULL,
  248. };
  249. ATTRIBUTE_GROUPS(fsl_mc_bus);
  250. const struct bus_type fsl_mc_bus_type = {
  251. .name = "fsl-mc",
  252. .match = fsl_mc_bus_match,
  253. .uevent = fsl_mc_bus_uevent,
  254. .dma_configure = fsl_mc_dma_configure,
  255. .dma_cleanup = fsl_mc_dma_cleanup,
  256. .dev_groups = fsl_mc_dev_groups,
  257. .bus_groups = fsl_mc_bus_groups,
  258. };
  259. EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
  260. struct device_type fsl_mc_bus_dprc_type = {
  261. .name = "fsl_mc_bus_dprc"
  262. };
  263. EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
  264. struct device_type fsl_mc_bus_dpni_type = {
  265. .name = "fsl_mc_bus_dpni"
  266. };
  267. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
  268. struct device_type fsl_mc_bus_dpio_type = {
  269. .name = "fsl_mc_bus_dpio"
  270. };
  271. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
  272. struct device_type fsl_mc_bus_dpsw_type = {
  273. .name = "fsl_mc_bus_dpsw"
  274. };
  275. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
  276. struct device_type fsl_mc_bus_dpbp_type = {
  277. .name = "fsl_mc_bus_dpbp"
  278. };
  279. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
  280. struct device_type fsl_mc_bus_dpcon_type = {
  281. .name = "fsl_mc_bus_dpcon"
  282. };
  283. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
  284. struct device_type fsl_mc_bus_dpmcp_type = {
  285. .name = "fsl_mc_bus_dpmcp"
  286. };
  287. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
  288. struct device_type fsl_mc_bus_dpmac_type = {
  289. .name = "fsl_mc_bus_dpmac"
  290. };
  291. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
  292. struct device_type fsl_mc_bus_dprtc_type = {
  293. .name = "fsl_mc_bus_dprtc"
  294. };
  295. EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
  296. struct device_type fsl_mc_bus_dpseci_type = {
  297. .name = "fsl_mc_bus_dpseci"
  298. };
  299. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
  300. struct device_type fsl_mc_bus_dpdmux_type = {
  301. .name = "fsl_mc_bus_dpdmux"
  302. };
  303. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
  304. struct device_type fsl_mc_bus_dpdcei_type = {
  305. .name = "fsl_mc_bus_dpdcei"
  306. };
  307. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
  308. struct device_type fsl_mc_bus_dpaiop_type = {
  309. .name = "fsl_mc_bus_dpaiop"
  310. };
  311. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
  312. struct device_type fsl_mc_bus_dpci_type = {
  313. .name = "fsl_mc_bus_dpci"
  314. };
  315. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
  316. struct device_type fsl_mc_bus_dpdmai_type = {
  317. .name = "fsl_mc_bus_dpdmai"
  318. };
  319. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
  320. struct device_type fsl_mc_bus_dpdbg_type = {
  321. .name = "fsl_mc_bus_dpdbg"
  322. };
  323. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdbg_type);
  324. static struct device_type *fsl_mc_get_device_type(const char *type)
  325. {
  326. static const struct {
  327. struct device_type *dev_type;
  328. const char *type;
  329. } dev_types[] = {
  330. { &fsl_mc_bus_dprc_type, "dprc" },
  331. { &fsl_mc_bus_dpni_type, "dpni" },
  332. { &fsl_mc_bus_dpio_type, "dpio" },
  333. { &fsl_mc_bus_dpsw_type, "dpsw" },
  334. { &fsl_mc_bus_dpbp_type, "dpbp" },
  335. { &fsl_mc_bus_dpcon_type, "dpcon" },
  336. { &fsl_mc_bus_dpmcp_type, "dpmcp" },
  337. { &fsl_mc_bus_dpmac_type, "dpmac" },
  338. { &fsl_mc_bus_dprtc_type, "dprtc" },
  339. { &fsl_mc_bus_dpseci_type, "dpseci" },
  340. { &fsl_mc_bus_dpdmux_type, "dpdmux" },
  341. { &fsl_mc_bus_dpdcei_type, "dpdcei" },
  342. { &fsl_mc_bus_dpaiop_type, "dpaiop" },
  343. { &fsl_mc_bus_dpci_type, "dpci" },
  344. { &fsl_mc_bus_dpdmai_type, "dpdmai" },
  345. { &fsl_mc_bus_dpdbg_type, "dpdbg" },
  346. { NULL, NULL }
  347. };
  348. int i;
  349. for (i = 0; dev_types[i].dev_type; i++)
  350. if (!strcmp(dev_types[i].type, type))
  351. return dev_types[i].dev_type;
  352. return NULL;
  353. }
  354. static int fsl_mc_driver_probe(struct device *dev)
  355. {
  356. struct fsl_mc_driver *mc_drv;
  357. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  358. int error;
  359. mc_drv = to_fsl_mc_driver(dev->driver);
  360. error = mc_drv->probe(mc_dev);
  361. if (error < 0) {
  362. if (error != -EPROBE_DEFER)
  363. dev_err(dev, "%s failed: %d\n", __func__, error);
  364. return error;
  365. }
  366. return 0;
  367. }
  368. static int fsl_mc_driver_remove(struct device *dev)
  369. {
  370. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
  371. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  372. mc_drv->remove(mc_dev);
  373. return 0;
  374. }
  375. static void fsl_mc_driver_shutdown(struct device *dev)
  376. {
  377. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
  378. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  379. mc_drv->shutdown(mc_dev);
  380. }
  381. /*
  382. * __fsl_mc_driver_register - registers a child device driver with the
  383. * MC bus
  384. *
  385. * This function is implicitly invoked from the registration function of
  386. * fsl_mc device drivers, which is generated by the
  387. * module_fsl_mc_driver() macro.
  388. */
  389. int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
  390. struct module *owner)
  391. {
  392. int error;
  393. mc_driver->driver.owner = owner;
  394. mc_driver->driver.bus = &fsl_mc_bus_type;
  395. if (mc_driver->probe)
  396. mc_driver->driver.probe = fsl_mc_driver_probe;
  397. if (mc_driver->remove)
  398. mc_driver->driver.remove = fsl_mc_driver_remove;
  399. if (mc_driver->shutdown)
  400. mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
  401. error = driver_register(&mc_driver->driver);
  402. if (error < 0) {
  403. pr_err("driver_register() failed for %s: %d\n",
  404. mc_driver->driver.name, error);
  405. return error;
  406. }
  407. return 0;
  408. }
  409. EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
  410. /*
  411. * fsl_mc_driver_unregister - unregisters a device driver from the
  412. * MC bus
  413. */
  414. void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
  415. {
  416. driver_unregister(&mc_driver->driver);
  417. }
  418. EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
  419. /**
  420. * mc_get_version() - Retrieves the Management Complex firmware
  421. * version information
  422. * @mc_io: Pointer to opaque I/O object
  423. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  424. * @mc_ver_info: Returned version information structure
  425. *
  426. * Return: '0' on Success; Error code otherwise.
  427. */
  428. static int mc_get_version(struct fsl_mc_io *mc_io,
  429. u32 cmd_flags,
  430. struct fsl_mc_version *mc_ver_info)
  431. {
  432. struct fsl_mc_command cmd = { 0 };
  433. struct dpmng_rsp_get_version *rsp_params;
  434. int err;
  435. /* prepare command */
  436. cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
  437. cmd_flags,
  438. 0);
  439. /* send command to mc*/
  440. err = mc_send_command(mc_io, &cmd);
  441. if (err)
  442. return err;
  443. /* retrieve response parameters */
  444. rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
  445. mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
  446. mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
  447. mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
  448. return 0;
  449. }
  450. /**
  451. * fsl_mc_get_version - function to retrieve the MC f/w version information
  452. *
  453. * Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
  454. */
  455. struct fsl_mc_version *fsl_mc_get_version(void)
  456. {
  457. if (mc_version.major)
  458. return &mc_version;
  459. return NULL;
  460. }
  461. EXPORT_SYMBOL_GPL(fsl_mc_get_version);
  462. /*
  463. * fsl_mc_get_root_dprc - function to traverse to the root dprc
  464. */
  465. void fsl_mc_get_root_dprc(struct device *dev,
  466. struct device **root_dprc_dev)
  467. {
  468. if (!dev) {
  469. *root_dprc_dev = NULL;
  470. } else if (!dev_is_fsl_mc(dev)) {
  471. *root_dprc_dev = NULL;
  472. } else {
  473. *root_dprc_dev = dev;
  474. while (dev_is_fsl_mc((*root_dprc_dev)->parent))
  475. *root_dprc_dev = (*root_dprc_dev)->parent;
  476. }
  477. }
  478. static int get_dprc_attr(struct fsl_mc_io *mc_io,
  479. int container_id, struct dprc_attributes *attr)
  480. {
  481. u16 dprc_handle;
  482. int error;
  483. error = dprc_open(mc_io, 0, container_id, &dprc_handle);
  484. if (error < 0) {
  485. dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
  486. return error;
  487. }
  488. memset(attr, 0, sizeof(struct dprc_attributes));
  489. error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
  490. if (error < 0) {
  491. dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
  492. error);
  493. goto common_cleanup;
  494. }
  495. error = 0;
  496. common_cleanup:
  497. (void)dprc_close(mc_io, 0, dprc_handle);
  498. return error;
  499. }
  500. static int get_dprc_icid(struct fsl_mc_io *mc_io,
  501. int container_id, u32 *icid)
  502. {
  503. struct dprc_attributes attr;
  504. int error;
  505. error = get_dprc_attr(mc_io, container_id, &attr);
  506. if (error == 0)
  507. *icid = attr.icid;
  508. return error;
  509. }
  510. static int translate_mc_addr(struct fsl_mc_device *mc_dev,
  511. enum dprc_region_type mc_region_type,
  512. u64 mc_offset, phys_addr_t *phys_addr)
  513. {
  514. int i;
  515. struct device *root_dprc_dev;
  516. struct fsl_mc *mc;
  517. fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
  518. mc = dev_get_drvdata(root_dprc_dev->parent);
  519. if (mc->num_translation_ranges == 0) {
  520. /*
  521. * Do identity mapping:
  522. */
  523. *phys_addr = mc_offset;
  524. return 0;
  525. }
  526. for (i = 0; i < mc->num_translation_ranges; i++) {
  527. struct fsl_mc_addr_translation_range *range =
  528. &mc->translation_ranges[i];
  529. if (mc_region_type == range->mc_region_type &&
  530. mc_offset >= range->start_mc_offset &&
  531. mc_offset < range->end_mc_offset) {
  532. *phys_addr = range->start_phys_addr +
  533. (mc_offset - range->start_mc_offset);
  534. return 0;
  535. }
  536. }
  537. return -EFAULT;
  538. }
  539. static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
  540. struct fsl_mc_device *mc_bus_dev)
  541. {
  542. int i;
  543. int error;
  544. struct resource *regions;
  545. struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
  546. struct device *parent_dev = mc_dev->dev.parent;
  547. enum dprc_region_type mc_region_type;
  548. if (is_fsl_mc_bus_dprc(mc_dev) ||
  549. is_fsl_mc_bus_dpmcp(mc_dev)) {
  550. mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
  551. } else if (is_fsl_mc_bus_dpio(mc_dev)) {
  552. mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
  553. } else {
  554. /*
  555. * This function should not have been called for this MC object
  556. * type, as this object type is not supposed to have MMIO
  557. * regions
  558. */
  559. return -EINVAL;
  560. }
  561. regions = kmalloc_array(obj_desc->region_count,
  562. sizeof(regions[0]), GFP_KERNEL);
  563. if (!regions)
  564. return -ENOMEM;
  565. for (i = 0; i < obj_desc->region_count; i++) {
  566. struct dprc_region_desc region_desc;
  567. error = dprc_get_obj_region(mc_bus_dev->mc_io,
  568. 0,
  569. mc_bus_dev->mc_handle,
  570. obj_desc->type,
  571. obj_desc->id, i, &region_desc);
  572. if (error < 0) {
  573. dev_err(parent_dev,
  574. "dprc_get_obj_region() failed: %d\n", error);
  575. goto error_cleanup_regions;
  576. }
  577. /*
  578. * Older MC only returned region offset and no base address
  579. * If base address is in the region_desc use it otherwise
  580. * revert to old mechanism
  581. */
  582. if (region_desc.base_address) {
  583. regions[i].start = region_desc.base_address +
  584. region_desc.base_offset;
  585. } else {
  586. error = translate_mc_addr(mc_dev, mc_region_type,
  587. region_desc.base_offset,
  588. &regions[i].start);
  589. /*
  590. * Some versions of the MC firmware wrongly report
  591. * 0 for register base address of the DPMCP associated
  592. * with child DPRC objects thus rendering them unusable.
  593. * This is particularly troublesome in ACPI boot
  594. * scenarios where the legacy way of extracting this
  595. * base address from the device tree does not apply.
  596. * Given that DPMCPs share the same base address,
  597. * workaround this by using the base address extracted
  598. * from the root DPRC container.
  599. */
  600. if (is_fsl_mc_bus_dprc(mc_dev) &&
  601. regions[i].start == region_desc.base_offset)
  602. regions[i].start += mc_portal_base_phys_addr;
  603. }
  604. if (error < 0) {
  605. dev_err(parent_dev,
  606. "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
  607. region_desc.base_offset,
  608. obj_desc->type, obj_desc->id, i);
  609. goto error_cleanup_regions;
  610. }
  611. regions[i].end = regions[i].start + region_desc.size - 1;
  612. regions[i].name = "fsl-mc object MMIO region";
  613. regions[i].flags = region_desc.flags & IORESOURCE_BITS;
  614. regions[i].flags |= IORESOURCE_MEM;
  615. }
  616. mc_dev->regions = regions;
  617. return 0;
  618. error_cleanup_regions:
  619. kfree(regions);
  620. return error;
  621. }
  622. /*
  623. * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
  624. */
  625. bool fsl_mc_is_root_dprc(struct device *dev)
  626. {
  627. struct device *root_dprc_dev;
  628. fsl_mc_get_root_dprc(dev, &root_dprc_dev);
  629. if (!root_dprc_dev)
  630. return false;
  631. return dev == root_dprc_dev;
  632. }
  633. static void fsl_mc_device_release(struct device *dev)
  634. {
  635. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  636. kfree(mc_dev->regions);
  637. if (is_fsl_mc_bus_dprc(mc_dev))
  638. kfree(to_fsl_mc_bus(mc_dev));
  639. else
  640. kfree(mc_dev);
  641. }
  642. /*
  643. * Add a newly discovered fsl-mc device to be visible in Linux
  644. */
  645. int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
  646. struct fsl_mc_io *mc_io,
  647. struct device *parent_dev,
  648. struct fsl_mc_device **new_mc_dev)
  649. {
  650. int error;
  651. struct fsl_mc_device *mc_dev = NULL;
  652. struct fsl_mc_bus *mc_bus = NULL;
  653. struct fsl_mc_device *parent_mc_dev;
  654. if (dev_is_fsl_mc(parent_dev))
  655. parent_mc_dev = to_fsl_mc_device(parent_dev);
  656. else
  657. parent_mc_dev = NULL;
  658. if (strcmp(obj_desc->type, "dprc") == 0) {
  659. /*
  660. * Allocate an MC bus device object:
  661. */
  662. mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
  663. if (!mc_bus)
  664. return -ENOMEM;
  665. mutex_init(&mc_bus->scan_mutex);
  666. mc_dev = &mc_bus->mc_dev;
  667. } else {
  668. /*
  669. * Allocate a regular fsl_mc_device object:
  670. */
  671. mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
  672. if (!mc_dev)
  673. return -ENOMEM;
  674. }
  675. mc_dev->obj_desc = *obj_desc;
  676. mc_dev->mc_io = mc_io;
  677. device_initialize(&mc_dev->dev);
  678. mc_dev->dev.parent = parent_dev;
  679. mc_dev->dev.bus = &fsl_mc_bus_type;
  680. mc_dev->dev.release = fsl_mc_device_release;
  681. mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
  682. if (!mc_dev->dev.type) {
  683. error = -ENODEV;
  684. dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
  685. goto error_cleanup_dev;
  686. }
  687. dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
  688. if (strcmp(obj_desc->type, "dprc") == 0) {
  689. struct fsl_mc_io *mc_io2;
  690. mc_dev->flags |= FSL_MC_IS_DPRC;
  691. /*
  692. * To get the DPRC's ICID, we need to open the DPRC
  693. * in get_dprc_icid(). For child DPRCs, we do so using the
  694. * parent DPRC's MC portal instead of the child DPRC's MC
  695. * portal, in case the child DPRC is already opened with
  696. * its own portal (e.g., the DPRC used by AIOP).
  697. *
  698. * NOTE: There cannot be more than one active open for a
  699. * given MC object, using the same MC portal.
  700. */
  701. if (parent_mc_dev) {
  702. /*
  703. * device being added is a child DPRC device
  704. */
  705. mc_io2 = parent_mc_dev->mc_io;
  706. } else {
  707. /*
  708. * device being added is the root DPRC device
  709. */
  710. if (!mc_io) {
  711. error = -EINVAL;
  712. goto error_cleanup_dev;
  713. }
  714. mc_io2 = mc_io;
  715. }
  716. error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
  717. if (error < 0)
  718. goto error_cleanup_dev;
  719. } else {
  720. /*
  721. * A non-DPRC object has to be a child of a DPRC, use the
  722. * parent's ICID and interrupt domain.
  723. */
  724. mc_dev->icid = parent_mc_dev->icid;
  725. mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
  726. mc_dev->dev.dma_mask = &mc_dev->dma_mask;
  727. mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
  728. dev_set_msi_domain(&mc_dev->dev,
  729. dev_get_msi_domain(&parent_mc_dev->dev));
  730. }
  731. /*
  732. * Get MMIO regions for the device from the MC:
  733. *
  734. * NOTE: the root DPRC is a special case as its MMIO region is
  735. * obtained from the device tree
  736. */
  737. if (parent_mc_dev && obj_desc->region_count != 0) {
  738. error = fsl_mc_device_get_mmio_regions(mc_dev,
  739. parent_mc_dev);
  740. if (error < 0)
  741. goto error_cleanup_dev;
  742. }
  743. /*
  744. * The device-specific probe callback will get invoked by device_add()
  745. */
  746. error = device_add(&mc_dev->dev);
  747. if (error < 0) {
  748. dev_err(parent_dev,
  749. "device_add() failed for device %s: %d\n",
  750. dev_name(&mc_dev->dev), error);
  751. goto error_cleanup_dev;
  752. }
  753. dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
  754. *new_mc_dev = mc_dev;
  755. return 0;
  756. error_cleanup_dev:
  757. kfree(mc_dev->regions);
  758. if (mc_bus)
  759. kfree(mc_bus);
  760. else
  761. kfree(mc_dev);
  762. return error;
  763. }
  764. EXPORT_SYMBOL_GPL(fsl_mc_device_add);
  765. static struct notifier_block fsl_mc_nb;
  766. /**
  767. * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
  768. * Linux
  769. *
  770. * @mc_dev: Pointer to an fsl-mc device
  771. */
  772. void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
  773. {
  774. kfree(mc_dev->driver_override);
  775. mc_dev->driver_override = NULL;
  776. /*
  777. * The device-specific remove callback will get invoked by device_del()
  778. */
  779. device_del(&mc_dev->dev);
  780. put_device(&mc_dev->dev);
  781. }
  782. EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
  783. struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,
  784. u16 if_id)
  785. {
  786. struct fsl_mc_device *mc_bus_dev, *endpoint;
  787. struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
  788. struct dprc_endpoint endpoint1 = {{ 0 }};
  789. struct dprc_endpoint endpoint2 = {{ 0 }};
  790. struct fsl_mc_bus *mc_bus;
  791. int state, err;
  792. mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
  793. strcpy(endpoint1.type, mc_dev->obj_desc.type);
  794. endpoint1.id = mc_dev->obj_desc.id;
  795. endpoint1.if_id = if_id;
  796. err = dprc_get_connection(mc_bus_dev->mc_io, 0,
  797. mc_bus_dev->mc_handle,
  798. &endpoint1, &endpoint2,
  799. &state);
  800. if (err == -ENOTCONN || state == -1)
  801. return ERR_PTR(-ENOTCONN);
  802. if (err < 0) {
  803. dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
  804. return ERR_PTR(err);
  805. }
  806. strcpy(endpoint_desc.type, endpoint2.type);
  807. endpoint_desc.id = endpoint2.id;
  808. endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
  809. if (endpoint)
  810. return endpoint;
  811. /*
  812. * We know that the device has an endpoint because we verified by
  813. * interrogating the firmware. This is the case when the device was not
  814. * yet discovered by the fsl-mc bus, thus the lookup returned NULL.
  815. * Force a rescan of the devices in this container and retry the lookup.
  816. */
  817. mc_bus = to_fsl_mc_bus(mc_bus_dev);
  818. if (mutex_trylock(&mc_bus->scan_mutex)) {
  819. err = dprc_scan_objects(mc_bus_dev, true);
  820. mutex_unlock(&mc_bus->scan_mutex);
  821. }
  822. if (err < 0)
  823. return ERR_PTR(err);
  824. endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
  825. /*
  826. * This means that the endpoint might reside in a different isolation
  827. * context (DPRC/container). Not much to do, so return a permssion
  828. * error.
  829. */
  830. if (!endpoint)
  831. return ERR_PTR(-EPERM);
  832. return endpoint;
  833. }
  834. EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
  835. static int get_mc_addr_translation_ranges(struct device *dev,
  836. struct fsl_mc_addr_translation_range
  837. **ranges,
  838. u8 *num_ranges)
  839. {
  840. struct fsl_mc_addr_translation_range *r;
  841. struct of_range_parser parser;
  842. struct of_range range;
  843. of_range_parser_init(&parser, dev->of_node);
  844. *num_ranges = of_range_count(&parser);
  845. if (!*num_ranges) {
  846. /*
  847. * Missing or empty ranges property ("ranges;") for the
  848. * 'fsl,qoriq-mc' node. In this case, identity mapping
  849. * will be used.
  850. */
  851. *ranges = NULL;
  852. return 0;
  853. }
  854. *ranges = devm_kcalloc(dev, *num_ranges,
  855. sizeof(struct fsl_mc_addr_translation_range),
  856. GFP_KERNEL);
  857. if (!(*ranges))
  858. return -ENOMEM;
  859. r = *ranges;
  860. for_each_of_range(&parser, &range) {
  861. r->mc_region_type = range.flags;
  862. r->start_mc_offset = range.bus_addr;
  863. r->end_mc_offset = range.bus_addr + range.size;
  864. r->start_phys_addr = range.cpu_addr;
  865. r++;
  866. }
  867. return 0;
  868. }
  869. /*
  870. * fsl_mc_bus_probe - callback invoked when the root MC bus is being
  871. * added
  872. */
  873. static int fsl_mc_bus_probe(struct platform_device *pdev)
  874. {
  875. struct fsl_mc_obj_desc obj_desc;
  876. int error;
  877. struct fsl_mc *mc;
  878. struct fsl_mc_device *mc_bus_dev = NULL;
  879. struct fsl_mc_io *mc_io = NULL;
  880. int container_id;
  881. phys_addr_t mc_portal_phys_addr;
  882. u32 mc_portal_size, mc_stream_id;
  883. struct resource *plat_res;
  884. mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
  885. if (!mc)
  886. return -ENOMEM;
  887. platform_set_drvdata(pdev, mc);
  888. plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  889. if (plat_res) {
  890. mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
  891. if (IS_ERR(mc->fsl_mc_regs))
  892. return PTR_ERR(mc->fsl_mc_regs);
  893. }
  894. if (mc->fsl_mc_regs) {
  895. if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
  896. mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
  897. /*
  898. * HW ORs the PL and BMT bit, places the result in bit
  899. * 14 of the StreamID and ORs in the ICID. Calculate it
  900. * accordingly.
  901. */
  902. mc_stream_id = (mc_stream_id & 0xffff) |
  903. ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
  904. BIT(14) : 0);
  905. error = acpi_dma_configure_id(&pdev->dev,
  906. DEV_DMA_COHERENT,
  907. &mc_stream_id);
  908. if (error == -EPROBE_DEFER)
  909. return error;
  910. if (error)
  911. dev_warn(&pdev->dev,
  912. "failed to configure dma: %d.\n",
  913. error);
  914. }
  915. /*
  916. * Some bootloaders pause the MC firmware before booting the
  917. * kernel so that MC will not cause faults as soon as the
  918. * SMMU probes due to the fact that there's no configuration
  919. * in place for MC.
  920. * At this point MC should have all its SMMU setup done so make
  921. * sure it is resumed.
  922. */
  923. writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) &
  924. (~(GCR1_P1_STOP | GCR1_P2_STOP)),
  925. mc->fsl_mc_regs + FSL_MC_GCR1);
  926. }
  927. /*
  928. * Get physical address of MC portal for the root DPRC:
  929. */
  930. plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  931. if (!plat_res)
  932. return -EINVAL;
  933. mc_portal_phys_addr = plat_res->start;
  934. mc_portal_size = resource_size(plat_res);
  935. mc_portal_base_phys_addr = mc_portal_phys_addr & ~0x3ffffff;
  936. error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
  937. mc_portal_size, NULL,
  938. FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
  939. if (error < 0)
  940. return error;
  941. error = mc_get_version(mc_io, 0, &mc_version);
  942. if (error != 0) {
  943. dev_err(&pdev->dev,
  944. "mc_get_version() failed with error %d\n", error);
  945. goto error_cleanup_mc_io;
  946. }
  947. dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
  948. mc_version.major, mc_version.minor, mc_version.revision);
  949. if (dev_of_node(&pdev->dev)) {
  950. error = get_mc_addr_translation_ranges(&pdev->dev,
  951. &mc->translation_ranges,
  952. &mc->num_translation_ranges);
  953. if (error < 0)
  954. goto error_cleanup_mc_io;
  955. }
  956. error = dprc_get_container_id(mc_io, 0, &container_id);
  957. if (error < 0) {
  958. dev_err(&pdev->dev,
  959. "dprc_get_container_id() failed: %d\n", error);
  960. goto error_cleanup_mc_io;
  961. }
  962. memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
  963. error = dprc_get_api_version(mc_io, 0,
  964. &obj_desc.ver_major,
  965. &obj_desc.ver_minor);
  966. if (error < 0)
  967. goto error_cleanup_mc_io;
  968. obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
  969. strcpy(obj_desc.type, "dprc");
  970. obj_desc.id = container_id;
  971. obj_desc.irq_count = 1;
  972. obj_desc.region_count = 0;
  973. error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
  974. if (error < 0)
  975. goto error_cleanup_mc_io;
  976. mc->root_mc_bus_dev = mc_bus_dev;
  977. mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
  978. return 0;
  979. error_cleanup_mc_io:
  980. fsl_destroy_mc_io(mc_io);
  981. return error;
  982. }
  983. /*
  984. * fsl_mc_bus_remove - callback invoked when the root MC bus is being
  985. * removed
  986. */
  987. static void fsl_mc_bus_remove(struct platform_device *pdev)
  988. {
  989. struct fsl_mc *mc = platform_get_drvdata(pdev);
  990. struct fsl_mc_io *mc_io;
  991. mc_io = mc->root_mc_bus_dev->mc_io;
  992. fsl_mc_device_remove(mc->root_mc_bus_dev);
  993. fsl_destroy_mc_io(mc_io);
  994. bus_unregister_notifier(&fsl_mc_bus_type, &fsl_mc_nb);
  995. if (mc->fsl_mc_regs) {
  996. /*
  997. * Pause the MC firmware so that it doesn't crash in certain
  998. * scenarios, such as kexec.
  999. */
  1000. writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) |
  1001. (GCR1_P1_STOP | GCR1_P2_STOP),
  1002. mc->fsl_mc_regs + FSL_MC_GCR1);
  1003. }
  1004. }
  1005. static const struct of_device_id fsl_mc_bus_match_table[] = {
  1006. {.compatible = "fsl,qoriq-mc",},
  1007. {},
  1008. };
  1009. MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
  1010. static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
  1011. {"NXP0008", 0 },
  1012. { }
  1013. };
  1014. MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
  1015. static struct platform_driver fsl_mc_bus_driver = {
  1016. .driver = {
  1017. .name = "fsl_mc_bus",
  1018. .pm = NULL,
  1019. .of_match_table = fsl_mc_bus_match_table,
  1020. .acpi_match_table = fsl_mc_bus_acpi_match_table,
  1021. },
  1022. .probe = fsl_mc_bus_probe,
  1023. .remove_new = fsl_mc_bus_remove,
  1024. .shutdown = fsl_mc_bus_remove,
  1025. };
  1026. static int fsl_mc_bus_notifier(struct notifier_block *nb,
  1027. unsigned long action, void *data)
  1028. {
  1029. struct device *dev = data;
  1030. struct resource *res;
  1031. void __iomem *fsl_mc_regs;
  1032. if (action != BUS_NOTIFY_ADD_DEVICE)
  1033. return 0;
  1034. if (!of_match_device(fsl_mc_bus_match_table, dev) &&
  1035. !acpi_match_device(fsl_mc_bus_acpi_match_table, dev))
  1036. return 0;
  1037. res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1);
  1038. if (!res)
  1039. return 0;
  1040. fsl_mc_regs = ioremap(res->start, resource_size(res));
  1041. if (!fsl_mc_regs)
  1042. return 0;
  1043. /*
  1044. * Make sure that the MC firmware is paused before the IOMMU setup for
  1045. * it is done or otherwise the firmware will crash right after the SMMU
  1046. * gets probed and enabled.
  1047. */
  1048. writel(readl(fsl_mc_regs + FSL_MC_GCR1) | (GCR1_P1_STOP | GCR1_P2_STOP),
  1049. fsl_mc_regs + FSL_MC_GCR1);
  1050. iounmap(fsl_mc_regs);
  1051. return 0;
  1052. }
  1053. static struct notifier_block fsl_mc_nb = {
  1054. .notifier_call = fsl_mc_bus_notifier,
  1055. };
  1056. static int __init fsl_mc_bus_driver_init(void)
  1057. {
  1058. int error;
  1059. error = bus_register(&fsl_mc_bus_type);
  1060. if (error < 0) {
  1061. pr_err("bus type registration failed: %d\n", error);
  1062. goto error_cleanup_cache;
  1063. }
  1064. error = platform_driver_register(&fsl_mc_bus_driver);
  1065. if (error < 0) {
  1066. pr_err("platform_driver_register() failed: %d\n", error);
  1067. goto error_cleanup_bus;
  1068. }
  1069. error = dprc_driver_init();
  1070. if (error < 0)
  1071. goto error_cleanup_driver;
  1072. error = fsl_mc_allocator_driver_init();
  1073. if (error < 0)
  1074. goto error_cleanup_dprc_driver;
  1075. return bus_register_notifier(&platform_bus_type, &fsl_mc_nb);
  1076. error_cleanup_dprc_driver:
  1077. dprc_driver_exit();
  1078. error_cleanup_driver:
  1079. platform_driver_unregister(&fsl_mc_bus_driver);
  1080. error_cleanup_bus:
  1081. bus_unregister(&fsl_mc_bus_type);
  1082. error_cleanup_cache:
  1083. return error;
  1084. }
  1085. postcore_initcall(fsl_mc_bus_driver_init);