fsl-mc-bus.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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. * Author: German Rivera <German.Rivera@freescale.com>
  7. *
  8. */
  9. #define pr_fmt(fmt) "fsl-mc: " fmt
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/of_address.h>
  13. #include <linux/ioport.h>
  14. #include <linux/slab.h>
  15. #include <linux/limits.h>
  16. #include <linux/bitops.h>
  17. #include <linux/msi.h>
  18. #include <linux/dma-mapping.h>
  19. #include "fsl-mc-private.h"
  20. /**
  21. * Default DMA mask for devices on a fsl-mc bus
  22. */
  23. #define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
  24. /**
  25. * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
  26. * @root_mc_bus_dev: fsl-mc device representing the root DPRC
  27. * @num_translation_ranges: number of entries in addr_translation_ranges
  28. * @translation_ranges: array of bus to system address translation ranges
  29. */
  30. struct fsl_mc {
  31. struct fsl_mc_device *root_mc_bus_dev;
  32. u8 num_translation_ranges;
  33. struct fsl_mc_addr_translation_range *translation_ranges;
  34. };
  35. /**
  36. * struct fsl_mc_addr_translation_range - bus to system address translation
  37. * range
  38. * @mc_region_type: Type of MC region for the range being translated
  39. * @start_mc_offset: Start MC offset of the range being translated
  40. * @end_mc_offset: MC offset of the first byte after the range (last MC
  41. * offset of the range is end_mc_offset - 1)
  42. * @start_phys_addr: system physical address corresponding to start_mc_addr
  43. */
  44. struct fsl_mc_addr_translation_range {
  45. enum dprc_region_type mc_region_type;
  46. u64 start_mc_offset;
  47. u64 end_mc_offset;
  48. phys_addr_t start_phys_addr;
  49. };
  50. /**
  51. * struct mc_version
  52. * @major: Major version number: incremented on API compatibility changes
  53. * @minor: Minor version number: incremented on API additions (that are
  54. * backward compatible); reset when major version is incremented
  55. * @revision: Internal revision number: incremented on implementation changes
  56. * and/or bug fixes that have no impact on API
  57. */
  58. struct mc_version {
  59. u32 major;
  60. u32 minor;
  61. u32 revision;
  62. };
  63. /**
  64. * fsl_mc_bus_match - device to driver matching callback
  65. * @dev: the fsl-mc device to match against
  66. * @drv: the device driver to search for matching fsl-mc object type
  67. * structures
  68. *
  69. * Returns 1 on success, 0 otherwise.
  70. */
  71. static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
  72. {
  73. const struct fsl_mc_device_id *id;
  74. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  75. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
  76. bool found = false;
  77. if (!mc_drv->match_id_table)
  78. goto out;
  79. /*
  80. * If the object is not 'plugged' don't match.
  81. * Only exception is the root DPRC, which is a special case.
  82. */
  83. if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
  84. !fsl_mc_is_root_dprc(&mc_dev->dev))
  85. goto out;
  86. /*
  87. * Traverse the match_id table of the given driver, trying to find
  88. * a matching for the given device.
  89. */
  90. for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
  91. if (id->vendor == mc_dev->obj_desc.vendor &&
  92. strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
  93. found = true;
  94. break;
  95. }
  96. }
  97. out:
  98. dev_dbg(dev, "%smatched\n", found ? "" : "not ");
  99. return found;
  100. }
  101. /**
  102. * fsl_mc_bus_uevent - callback invoked when a device is added
  103. */
  104. static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  105. {
  106. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  107. if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
  108. mc_dev->obj_desc.vendor,
  109. mc_dev->obj_desc.type))
  110. return -ENOMEM;
  111. return 0;
  112. }
  113. static int fsl_mc_dma_configure(struct device *dev)
  114. {
  115. struct device *dma_dev = dev;
  116. while (dev_is_fsl_mc(dma_dev))
  117. dma_dev = dma_dev->parent;
  118. return of_dma_configure(dev, dma_dev->of_node, 0);
  119. }
  120. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  121. char *buf)
  122. {
  123. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  124. return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
  125. mc_dev->obj_desc.type);
  126. }
  127. static DEVICE_ATTR_RO(modalias);
  128. static struct attribute *fsl_mc_dev_attrs[] = {
  129. &dev_attr_modalias.attr,
  130. NULL,
  131. };
  132. ATTRIBUTE_GROUPS(fsl_mc_dev);
  133. struct bus_type fsl_mc_bus_type = {
  134. .name = "fsl-mc",
  135. .match = fsl_mc_bus_match,
  136. .uevent = fsl_mc_bus_uevent,
  137. .dma_configure = fsl_mc_dma_configure,
  138. .dev_groups = fsl_mc_dev_groups,
  139. };
  140. EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
  141. struct device_type fsl_mc_bus_dprc_type = {
  142. .name = "fsl_mc_bus_dprc"
  143. };
  144. struct device_type fsl_mc_bus_dpni_type = {
  145. .name = "fsl_mc_bus_dpni"
  146. };
  147. struct device_type fsl_mc_bus_dpio_type = {
  148. .name = "fsl_mc_bus_dpio"
  149. };
  150. struct device_type fsl_mc_bus_dpsw_type = {
  151. .name = "fsl_mc_bus_dpsw"
  152. };
  153. struct device_type fsl_mc_bus_dpbp_type = {
  154. .name = "fsl_mc_bus_dpbp"
  155. };
  156. struct device_type fsl_mc_bus_dpcon_type = {
  157. .name = "fsl_mc_bus_dpcon"
  158. };
  159. struct device_type fsl_mc_bus_dpmcp_type = {
  160. .name = "fsl_mc_bus_dpmcp"
  161. };
  162. struct device_type fsl_mc_bus_dpmac_type = {
  163. .name = "fsl_mc_bus_dpmac"
  164. };
  165. struct device_type fsl_mc_bus_dprtc_type = {
  166. .name = "fsl_mc_bus_dprtc"
  167. };
  168. struct device_type fsl_mc_bus_dpseci_type = {
  169. .name = "fsl_mc_bus_dpseci"
  170. };
  171. static struct device_type *fsl_mc_get_device_type(const char *type)
  172. {
  173. static const struct {
  174. struct device_type *dev_type;
  175. const char *type;
  176. } dev_types[] = {
  177. { &fsl_mc_bus_dprc_type, "dprc" },
  178. { &fsl_mc_bus_dpni_type, "dpni" },
  179. { &fsl_mc_bus_dpio_type, "dpio" },
  180. { &fsl_mc_bus_dpsw_type, "dpsw" },
  181. { &fsl_mc_bus_dpbp_type, "dpbp" },
  182. { &fsl_mc_bus_dpcon_type, "dpcon" },
  183. { &fsl_mc_bus_dpmcp_type, "dpmcp" },
  184. { &fsl_mc_bus_dpmac_type, "dpmac" },
  185. { &fsl_mc_bus_dprtc_type, "dprtc" },
  186. { &fsl_mc_bus_dpseci_type, "dpseci" },
  187. { NULL, NULL }
  188. };
  189. int i;
  190. for (i = 0; dev_types[i].dev_type; i++)
  191. if (!strcmp(dev_types[i].type, type))
  192. return dev_types[i].dev_type;
  193. return NULL;
  194. }
  195. static int fsl_mc_driver_probe(struct device *dev)
  196. {
  197. struct fsl_mc_driver *mc_drv;
  198. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  199. int error;
  200. mc_drv = to_fsl_mc_driver(dev->driver);
  201. error = mc_drv->probe(mc_dev);
  202. if (error < 0) {
  203. if (error != -EPROBE_DEFER)
  204. dev_err(dev, "%s failed: %d\n", __func__, error);
  205. return error;
  206. }
  207. return 0;
  208. }
  209. static int fsl_mc_driver_remove(struct device *dev)
  210. {
  211. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
  212. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  213. int error;
  214. error = mc_drv->remove(mc_dev);
  215. if (error < 0) {
  216. dev_err(dev, "%s failed: %d\n", __func__, error);
  217. return error;
  218. }
  219. return 0;
  220. }
  221. static void fsl_mc_driver_shutdown(struct device *dev)
  222. {
  223. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
  224. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  225. mc_drv->shutdown(mc_dev);
  226. }
  227. /**
  228. * __fsl_mc_driver_register - registers a child device driver with the
  229. * MC bus
  230. *
  231. * This function is implicitly invoked from the registration function of
  232. * fsl_mc device drivers, which is generated by the
  233. * module_fsl_mc_driver() macro.
  234. */
  235. int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
  236. struct module *owner)
  237. {
  238. int error;
  239. mc_driver->driver.owner = owner;
  240. mc_driver->driver.bus = &fsl_mc_bus_type;
  241. if (mc_driver->probe)
  242. mc_driver->driver.probe = fsl_mc_driver_probe;
  243. if (mc_driver->remove)
  244. mc_driver->driver.remove = fsl_mc_driver_remove;
  245. if (mc_driver->shutdown)
  246. mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
  247. error = driver_register(&mc_driver->driver);
  248. if (error < 0) {
  249. pr_err("driver_register() failed for %s: %d\n",
  250. mc_driver->driver.name, error);
  251. return error;
  252. }
  253. return 0;
  254. }
  255. EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
  256. /**
  257. * fsl_mc_driver_unregister - unregisters a device driver from the
  258. * MC bus
  259. */
  260. void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
  261. {
  262. driver_unregister(&mc_driver->driver);
  263. }
  264. EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
  265. /**
  266. * mc_get_version() - Retrieves the Management Complex firmware
  267. * version information
  268. * @mc_io: Pointer to opaque I/O object
  269. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  270. * @mc_ver_info: Returned version information structure
  271. *
  272. * Return: '0' on Success; Error code otherwise.
  273. */
  274. static int mc_get_version(struct fsl_mc_io *mc_io,
  275. u32 cmd_flags,
  276. struct mc_version *mc_ver_info)
  277. {
  278. struct fsl_mc_command cmd = { 0 };
  279. struct dpmng_rsp_get_version *rsp_params;
  280. int err;
  281. /* prepare command */
  282. cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
  283. cmd_flags,
  284. 0);
  285. /* send command to mc*/
  286. err = mc_send_command(mc_io, &cmd);
  287. if (err)
  288. return err;
  289. /* retrieve response parameters */
  290. rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
  291. mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
  292. mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
  293. mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
  294. return 0;
  295. }
  296. /**
  297. * fsl_mc_get_root_dprc - function to traverse to the root dprc
  298. */
  299. static void fsl_mc_get_root_dprc(struct device *dev,
  300. struct device **root_dprc_dev)
  301. {
  302. if (!dev) {
  303. *root_dprc_dev = NULL;
  304. } else if (!dev_is_fsl_mc(dev)) {
  305. *root_dprc_dev = NULL;
  306. } else {
  307. *root_dprc_dev = dev;
  308. while (dev_is_fsl_mc((*root_dprc_dev)->parent))
  309. *root_dprc_dev = (*root_dprc_dev)->parent;
  310. }
  311. }
  312. static int get_dprc_attr(struct fsl_mc_io *mc_io,
  313. int container_id, struct dprc_attributes *attr)
  314. {
  315. u16 dprc_handle;
  316. int error;
  317. error = dprc_open(mc_io, 0, container_id, &dprc_handle);
  318. if (error < 0) {
  319. dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
  320. return error;
  321. }
  322. memset(attr, 0, sizeof(struct dprc_attributes));
  323. error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
  324. if (error < 0) {
  325. dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
  326. error);
  327. goto common_cleanup;
  328. }
  329. error = 0;
  330. common_cleanup:
  331. (void)dprc_close(mc_io, 0, dprc_handle);
  332. return error;
  333. }
  334. static int get_dprc_icid(struct fsl_mc_io *mc_io,
  335. int container_id, u16 *icid)
  336. {
  337. struct dprc_attributes attr;
  338. int error;
  339. error = get_dprc_attr(mc_io, container_id, &attr);
  340. if (error == 0)
  341. *icid = attr.icid;
  342. return error;
  343. }
  344. static int translate_mc_addr(struct fsl_mc_device *mc_dev,
  345. enum dprc_region_type mc_region_type,
  346. u64 mc_offset, phys_addr_t *phys_addr)
  347. {
  348. int i;
  349. struct device *root_dprc_dev;
  350. struct fsl_mc *mc;
  351. fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
  352. mc = dev_get_drvdata(root_dprc_dev->parent);
  353. if (mc->num_translation_ranges == 0) {
  354. /*
  355. * Do identity mapping:
  356. */
  357. *phys_addr = mc_offset;
  358. return 0;
  359. }
  360. for (i = 0; i < mc->num_translation_ranges; i++) {
  361. struct fsl_mc_addr_translation_range *range =
  362. &mc->translation_ranges[i];
  363. if (mc_region_type == range->mc_region_type &&
  364. mc_offset >= range->start_mc_offset &&
  365. mc_offset < range->end_mc_offset) {
  366. *phys_addr = range->start_phys_addr +
  367. (mc_offset - range->start_mc_offset);
  368. return 0;
  369. }
  370. }
  371. return -EFAULT;
  372. }
  373. static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
  374. struct fsl_mc_device *mc_bus_dev)
  375. {
  376. int i;
  377. int error;
  378. struct resource *regions;
  379. struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
  380. struct device *parent_dev = mc_dev->dev.parent;
  381. enum dprc_region_type mc_region_type;
  382. if (is_fsl_mc_bus_dprc(mc_dev) ||
  383. is_fsl_mc_bus_dpmcp(mc_dev)) {
  384. mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
  385. } else if (is_fsl_mc_bus_dpio(mc_dev)) {
  386. mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
  387. } else {
  388. /*
  389. * This function should not have been called for this MC object
  390. * type, as this object type is not supposed to have MMIO
  391. * regions
  392. */
  393. return -EINVAL;
  394. }
  395. regions = kmalloc_array(obj_desc->region_count,
  396. sizeof(regions[0]), GFP_KERNEL);
  397. if (!regions)
  398. return -ENOMEM;
  399. for (i = 0; i < obj_desc->region_count; i++) {
  400. struct dprc_region_desc region_desc;
  401. error = dprc_get_obj_region(mc_bus_dev->mc_io,
  402. 0,
  403. mc_bus_dev->mc_handle,
  404. obj_desc->type,
  405. obj_desc->id, i, &region_desc);
  406. if (error < 0) {
  407. dev_err(parent_dev,
  408. "dprc_get_obj_region() failed: %d\n", error);
  409. goto error_cleanup_regions;
  410. }
  411. /*
  412. * Older MC only returned region offset and no base address
  413. * If base address is in the region_desc use it otherwise
  414. * revert to old mechanism
  415. */
  416. if (region_desc.base_address)
  417. regions[i].start = region_desc.base_address +
  418. region_desc.base_offset;
  419. else
  420. error = translate_mc_addr(mc_dev, mc_region_type,
  421. region_desc.base_offset,
  422. &regions[i].start);
  423. if (error < 0) {
  424. dev_err(parent_dev,
  425. "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
  426. region_desc.base_offset,
  427. obj_desc->type, obj_desc->id, i);
  428. goto error_cleanup_regions;
  429. }
  430. regions[i].end = regions[i].start + region_desc.size - 1;
  431. regions[i].name = "fsl-mc object MMIO region";
  432. regions[i].flags = IORESOURCE_IO;
  433. if (region_desc.flags & DPRC_REGION_CACHEABLE)
  434. regions[i].flags |= IORESOURCE_CACHEABLE;
  435. if (region_desc.flags & DPRC_REGION_SHAREABLE)
  436. regions[i].flags |= IORESOURCE_MEM;
  437. }
  438. mc_dev->regions = regions;
  439. return 0;
  440. error_cleanup_regions:
  441. kfree(regions);
  442. return error;
  443. }
  444. /**
  445. * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
  446. */
  447. bool fsl_mc_is_root_dprc(struct device *dev)
  448. {
  449. struct device *root_dprc_dev;
  450. fsl_mc_get_root_dprc(dev, &root_dprc_dev);
  451. if (!root_dprc_dev)
  452. return false;
  453. return dev == root_dprc_dev;
  454. }
  455. static void fsl_mc_device_release(struct device *dev)
  456. {
  457. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  458. kfree(mc_dev->regions);
  459. if (is_fsl_mc_bus_dprc(mc_dev))
  460. kfree(to_fsl_mc_bus(mc_dev));
  461. else
  462. kfree(mc_dev);
  463. }
  464. /**
  465. * Add a newly discovered fsl-mc device to be visible in Linux
  466. */
  467. int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
  468. struct fsl_mc_io *mc_io,
  469. struct device *parent_dev,
  470. struct fsl_mc_device **new_mc_dev)
  471. {
  472. int error;
  473. struct fsl_mc_device *mc_dev = NULL;
  474. struct fsl_mc_bus *mc_bus = NULL;
  475. struct fsl_mc_device *parent_mc_dev;
  476. if (dev_is_fsl_mc(parent_dev))
  477. parent_mc_dev = to_fsl_mc_device(parent_dev);
  478. else
  479. parent_mc_dev = NULL;
  480. if (strcmp(obj_desc->type, "dprc") == 0) {
  481. /*
  482. * Allocate an MC bus device object:
  483. */
  484. mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
  485. if (!mc_bus)
  486. return -ENOMEM;
  487. mc_dev = &mc_bus->mc_dev;
  488. } else {
  489. /*
  490. * Allocate a regular fsl_mc_device object:
  491. */
  492. mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
  493. if (!mc_dev)
  494. return -ENOMEM;
  495. }
  496. mc_dev->obj_desc = *obj_desc;
  497. mc_dev->mc_io = mc_io;
  498. device_initialize(&mc_dev->dev);
  499. mc_dev->dev.parent = parent_dev;
  500. mc_dev->dev.bus = &fsl_mc_bus_type;
  501. mc_dev->dev.release = fsl_mc_device_release;
  502. mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
  503. if (!mc_dev->dev.type) {
  504. error = -ENODEV;
  505. dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
  506. goto error_cleanup_dev;
  507. }
  508. dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
  509. if (strcmp(obj_desc->type, "dprc") == 0) {
  510. struct fsl_mc_io *mc_io2;
  511. mc_dev->flags |= FSL_MC_IS_DPRC;
  512. /*
  513. * To get the DPRC's ICID, we need to open the DPRC
  514. * in get_dprc_icid(). For child DPRCs, we do so using the
  515. * parent DPRC's MC portal instead of the child DPRC's MC
  516. * portal, in case the child DPRC is already opened with
  517. * its own portal (e.g., the DPRC used by AIOP).
  518. *
  519. * NOTE: There cannot be more than one active open for a
  520. * given MC object, using the same MC portal.
  521. */
  522. if (parent_mc_dev) {
  523. /*
  524. * device being added is a child DPRC device
  525. */
  526. mc_io2 = parent_mc_dev->mc_io;
  527. } else {
  528. /*
  529. * device being added is the root DPRC device
  530. */
  531. if (!mc_io) {
  532. error = -EINVAL;
  533. goto error_cleanup_dev;
  534. }
  535. mc_io2 = mc_io;
  536. }
  537. error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
  538. if (error < 0)
  539. goto error_cleanup_dev;
  540. } else {
  541. /*
  542. * A non-DPRC object has to be a child of a DPRC, use the
  543. * parent's ICID and interrupt domain.
  544. */
  545. mc_dev->icid = parent_mc_dev->icid;
  546. mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
  547. mc_dev->dev.dma_mask = &mc_dev->dma_mask;
  548. mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
  549. dev_set_msi_domain(&mc_dev->dev,
  550. dev_get_msi_domain(&parent_mc_dev->dev));
  551. }
  552. /*
  553. * Get MMIO regions for the device from the MC:
  554. *
  555. * NOTE: the root DPRC is a special case as its MMIO region is
  556. * obtained from the device tree
  557. */
  558. if (parent_mc_dev && obj_desc->region_count != 0) {
  559. error = fsl_mc_device_get_mmio_regions(mc_dev,
  560. parent_mc_dev);
  561. if (error < 0)
  562. goto error_cleanup_dev;
  563. }
  564. /*
  565. * The device-specific probe callback will get invoked by device_add()
  566. */
  567. error = device_add(&mc_dev->dev);
  568. if (error < 0) {
  569. dev_err(parent_dev,
  570. "device_add() failed for device %s: %d\n",
  571. dev_name(&mc_dev->dev), error);
  572. goto error_cleanup_dev;
  573. }
  574. dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
  575. *new_mc_dev = mc_dev;
  576. return 0;
  577. error_cleanup_dev:
  578. kfree(mc_dev->regions);
  579. kfree(mc_bus);
  580. kfree(mc_dev);
  581. return error;
  582. }
  583. EXPORT_SYMBOL_GPL(fsl_mc_device_add);
  584. /**
  585. * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
  586. * Linux
  587. *
  588. * @mc_dev: Pointer to an fsl-mc device
  589. */
  590. void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
  591. {
  592. /*
  593. * The device-specific remove callback will get invoked by device_del()
  594. */
  595. device_del(&mc_dev->dev);
  596. put_device(&mc_dev->dev);
  597. }
  598. EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
  599. static int parse_mc_ranges(struct device *dev,
  600. int *paddr_cells,
  601. int *mc_addr_cells,
  602. int *mc_size_cells,
  603. const __be32 **ranges_start)
  604. {
  605. const __be32 *prop;
  606. int range_tuple_cell_count;
  607. int ranges_len;
  608. int tuple_len;
  609. struct device_node *mc_node = dev->of_node;
  610. *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
  611. if (!(*ranges_start) || !ranges_len) {
  612. dev_warn(dev,
  613. "missing or empty ranges property for device tree node '%pOFn'\n",
  614. mc_node);
  615. return 0;
  616. }
  617. *paddr_cells = of_n_addr_cells(mc_node);
  618. prop = of_get_property(mc_node, "#address-cells", NULL);
  619. if (prop)
  620. *mc_addr_cells = be32_to_cpup(prop);
  621. else
  622. *mc_addr_cells = *paddr_cells;
  623. prop = of_get_property(mc_node, "#size-cells", NULL);
  624. if (prop)
  625. *mc_size_cells = be32_to_cpup(prop);
  626. else
  627. *mc_size_cells = of_n_size_cells(mc_node);
  628. range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
  629. *mc_size_cells;
  630. tuple_len = range_tuple_cell_count * sizeof(__be32);
  631. if (ranges_len % tuple_len != 0) {
  632. dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
  633. return -EINVAL;
  634. }
  635. return ranges_len / tuple_len;
  636. }
  637. static int get_mc_addr_translation_ranges(struct device *dev,
  638. struct fsl_mc_addr_translation_range
  639. **ranges,
  640. u8 *num_ranges)
  641. {
  642. int ret;
  643. int paddr_cells;
  644. int mc_addr_cells;
  645. int mc_size_cells;
  646. int i;
  647. const __be32 *ranges_start;
  648. const __be32 *cell;
  649. ret = parse_mc_ranges(dev,
  650. &paddr_cells,
  651. &mc_addr_cells,
  652. &mc_size_cells,
  653. &ranges_start);
  654. if (ret < 0)
  655. return ret;
  656. *num_ranges = ret;
  657. if (!ret) {
  658. /*
  659. * Missing or empty ranges property ("ranges;") for the
  660. * 'fsl,qoriq-mc' node. In this case, identity mapping
  661. * will be used.
  662. */
  663. *ranges = NULL;
  664. return 0;
  665. }
  666. *ranges = devm_kcalloc(dev, *num_ranges,
  667. sizeof(struct fsl_mc_addr_translation_range),
  668. GFP_KERNEL);
  669. if (!(*ranges))
  670. return -ENOMEM;
  671. cell = ranges_start;
  672. for (i = 0; i < *num_ranges; ++i) {
  673. struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
  674. range->mc_region_type = of_read_number(cell, 1);
  675. range->start_mc_offset = of_read_number(cell + 1,
  676. mc_addr_cells - 1);
  677. cell += mc_addr_cells;
  678. range->start_phys_addr = of_read_number(cell, paddr_cells);
  679. cell += paddr_cells;
  680. range->end_mc_offset = range->start_mc_offset +
  681. of_read_number(cell, mc_size_cells);
  682. cell += mc_size_cells;
  683. }
  684. return 0;
  685. }
  686. /**
  687. * fsl_mc_bus_probe - callback invoked when the root MC bus is being
  688. * added
  689. */
  690. static int fsl_mc_bus_probe(struct platform_device *pdev)
  691. {
  692. struct fsl_mc_obj_desc obj_desc;
  693. int error;
  694. struct fsl_mc *mc;
  695. struct fsl_mc_device *mc_bus_dev = NULL;
  696. struct fsl_mc_io *mc_io = NULL;
  697. int container_id;
  698. phys_addr_t mc_portal_phys_addr;
  699. u32 mc_portal_size;
  700. struct mc_version mc_version;
  701. struct resource res;
  702. mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
  703. if (!mc)
  704. return -ENOMEM;
  705. platform_set_drvdata(pdev, mc);
  706. /*
  707. * Get physical address of MC portal for the root DPRC:
  708. */
  709. error = of_address_to_resource(pdev->dev.of_node, 0, &res);
  710. if (error < 0) {
  711. dev_err(&pdev->dev,
  712. "of_address_to_resource() failed for %pOF\n",
  713. pdev->dev.of_node);
  714. return error;
  715. }
  716. mc_portal_phys_addr = res.start;
  717. mc_portal_size = resource_size(&res);
  718. error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
  719. mc_portal_size, NULL,
  720. FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
  721. if (error < 0)
  722. return error;
  723. error = mc_get_version(mc_io, 0, &mc_version);
  724. if (error != 0) {
  725. dev_err(&pdev->dev,
  726. "mc_get_version() failed with error %d\n", error);
  727. goto error_cleanup_mc_io;
  728. }
  729. dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
  730. mc_version.major, mc_version.minor, mc_version.revision);
  731. error = get_mc_addr_translation_ranges(&pdev->dev,
  732. &mc->translation_ranges,
  733. &mc->num_translation_ranges);
  734. if (error < 0)
  735. goto error_cleanup_mc_io;
  736. error = dprc_get_container_id(mc_io, 0, &container_id);
  737. if (error < 0) {
  738. dev_err(&pdev->dev,
  739. "dprc_get_container_id() failed: %d\n", error);
  740. goto error_cleanup_mc_io;
  741. }
  742. memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
  743. error = dprc_get_api_version(mc_io, 0,
  744. &obj_desc.ver_major,
  745. &obj_desc.ver_minor);
  746. if (error < 0)
  747. goto error_cleanup_mc_io;
  748. obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
  749. strcpy(obj_desc.type, "dprc");
  750. obj_desc.id = container_id;
  751. obj_desc.irq_count = 1;
  752. obj_desc.region_count = 0;
  753. error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
  754. if (error < 0)
  755. goto error_cleanup_mc_io;
  756. mc->root_mc_bus_dev = mc_bus_dev;
  757. return 0;
  758. error_cleanup_mc_io:
  759. fsl_destroy_mc_io(mc_io);
  760. return error;
  761. }
  762. /**
  763. * fsl_mc_bus_remove - callback invoked when the root MC bus is being
  764. * removed
  765. */
  766. static int fsl_mc_bus_remove(struct platform_device *pdev)
  767. {
  768. struct fsl_mc *mc = platform_get_drvdata(pdev);
  769. if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
  770. return -EINVAL;
  771. fsl_mc_device_remove(mc->root_mc_bus_dev);
  772. fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
  773. mc->root_mc_bus_dev->mc_io = NULL;
  774. return 0;
  775. }
  776. static const struct of_device_id fsl_mc_bus_match_table[] = {
  777. {.compatible = "fsl,qoriq-mc",},
  778. {},
  779. };
  780. MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
  781. static struct platform_driver fsl_mc_bus_driver = {
  782. .driver = {
  783. .name = "fsl_mc_bus",
  784. .pm = NULL,
  785. .of_match_table = fsl_mc_bus_match_table,
  786. },
  787. .probe = fsl_mc_bus_probe,
  788. .remove = fsl_mc_bus_remove,
  789. };
  790. static int __init fsl_mc_bus_driver_init(void)
  791. {
  792. int error;
  793. error = bus_register(&fsl_mc_bus_type);
  794. if (error < 0) {
  795. pr_err("bus type registration failed: %d\n", error);
  796. goto error_cleanup_cache;
  797. }
  798. error = platform_driver_register(&fsl_mc_bus_driver);
  799. if (error < 0) {
  800. pr_err("platform_driver_register() failed: %d\n", error);
  801. goto error_cleanup_bus;
  802. }
  803. error = dprc_driver_init();
  804. if (error < 0)
  805. goto error_cleanup_driver;
  806. error = fsl_mc_allocator_driver_init();
  807. if (error < 0)
  808. goto error_cleanup_dprc_driver;
  809. return 0;
  810. error_cleanup_dprc_driver:
  811. dprc_driver_exit();
  812. error_cleanup_driver:
  813. platform_driver_unregister(&fsl_mc_bus_driver);
  814. error_cleanup_bus:
  815. bus_unregister(&fsl_mc_bus_type);
  816. error_cleanup_cache:
  817. return error;
  818. }
  819. postcore_initcall(fsl_mc_bus_driver_init);