cdx.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * CDX bus driver.
  4. *
  5. * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
  6. */
  7. /*
  8. * Architecture Overview
  9. * =====================
  10. * CDX is a Hardware Architecture designed for AMD FPGA devices. It
  11. * consists of sophisticated mechanism for interaction between FPGA,
  12. * Firmware and the APUs (Application CPUs).
  13. *
  14. * Firmware resides on RPU (Realtime CPUs) which interacts with
  15. * the FPGA program manager and the APUs. The RPU provides memory-mapped
  16. * interface (RPU if) which is used to communicate with APUs.
  17. *
  18. * The diagram below shows an overview of the CDX architecture:
  19. *
  20. * +--------------------------------------+
  21. * | Application CPUs (APU) |
  22. * | |
  23. * | CDX device drivers|
  24. * | Linux OS | |
  25. * | CDX bus |
  26. * | | |
  27. * | CDX controller |
  28. * | | |
  29. * +-----------------------------|--------+
  30. * | (discover, config,
  31. * | reset, rescan)
  32. * |
  33. * +------------------------| RPU if |----+
  34. * | | |
  35. * | V |
  36. * | Realtime CPUs (RPU) |
  37. * | |
  38. * +--------------------------------------+
  39. * |
  40. * +---------------------|----------------+
  41. * | FPGA | |
  42. * | +-----------------------+ |
  43. * | | | | |
  44. * | +-------+ +-------+ +-------+ |
  45. * | | dev 1 | | dev 2 | | dev 3 | |
  46. * | +-------+ +-------+ +-------+ |
  47. * +--------------------------------------+
  48. *
  49. * The RPU firmware extracts the device information from the loaded FPGA
  50. * image and implements a mechanism that allows the APU drivers to
  51. * enumerate such devices (device personality and resource details) via
  52. * a dedicated communication channel. RPU mediates operations such as
  53. * discover, reset and rescan of the FPGA devices for the APU. This is
  54. * done using memory mapped interface provided by the RPU to APU.
  55. */
  56. #include <linux/init.h>
  57. #include <linux/irqdomain.h>
  58. #include <linux/kernel.h>
  59. #include <linux/of.h>
  60. #include <linux/of_device.h>
  61. #include <linux/of_platform.h>
  62. #include <linux/platform_device.h>
  63. #include <linux/slab.h>
  64. #include <linux/mm.h>
  65. #include <linux/idr.h>
  66. #include <linux/cdx/cdx_bus.h>
  67. #include <linux/iommu.h>
  68. #include <linux/dma-map-ops.h>
  69. #include <linux/debugfs.h>
  70. #include "cdx.h"
  71. /* Default DMA mask for devices on a CDX bus */
  72. #define CDX_DEFAULT_DMA_MASK (~0ULL)
  73. #define MAX_CDX_CONTROLLERS 16
  74. /* IDA for CDX controllers registered with the CDX bus */
  75. static DEFINE_IDA(cdx_controller_ida);
  76. /* Lock to protect controller ops */
  77. static DEFINE_MUTEX(cdx_controller_lock);
  78. /* Debugfs dir for cdx bus */
  79. static struct dentry *cdx_debugfs_dir;
  80. static char *compat_node_name = "xlnx,versal-net-cdx";
  81. static void cdx_destroy_res_attr(struct cdx_device *cdx_dev, int num);
  82. /**
  83. * cdx_dev_reset - Reset a CDX device
  84. * @dev: CDX device
  85. *
  86. * Return: -errno on failure, 0 on success.
  87. */
  88. int cdx_dev_reset(struct device *dev)
  89. {
  90. struct cdx_device *cdx_dev = to_cdx_device(dev);
  91. struct cdx_controller *cdx = cdx_dev->cdx;
  92. struct cdx_device_config dev_config = {0};
  93. struct cdx_driver *cdx_drv;
  94. int ret;
  95. cdx_drv = to_cdx_driver(dev->driver);
  96. /* Notify driver that device is being reset */
  97. if (cdx_drv && cdx_drv->reset_prepare)
  98. cdx_drv->reset_prepare(cdx_dev);
  99. dev_config.type = CDX_DEV_RESET_CONF;
  100. ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
  101. cdx_dev->dev_num, &dev_config);
  102. if (ret)
  103. dev_err(dev, "cdx device reset failed\n");
  104. /* Notify driver that device reset is complete */
  105. if (cdx_drv && cdx_drv->reset_done)
  106. cdx_drv->reset_done(cdx_dev);
  107. return ret;
  108. }
  109. EXPORT_SYMBOL_GPL(cdx_dev_reset);
  110. /**
  111. * reset_cdx_device - Reset a CDX device
  112. * @dev: CDX device
  113. * @data: This is always passed as NULL, and is not used in this API,
  114. * but is required here as the device_for_each_child() API expects
  115. * the passed function to have this as an argument.
  116. *
  117. * Return: -errno on failure, 0 on success.
  118. */
  119. static int reset_cdx_device(struct device *dev, void *data)
  120. {
  121. return cdx_dev_reset(dev);
  122. }
  123. /**
  124. * cdx_unregister_device - Unregister a CDX device
  125. * @dev: CDX device
  126. * @data: This is always passed as NULL, and is not used in this API,
  127. * but is required here as the bus_for_each_dev() API expects
  128. * the passed function (cdx_unregister_device) to have this
  129. * as an argument.
  130. *
  131. * Return: 0 on success.
  132. */
  133. static int cdx_unregister_device(struct device *dev,
  134. void *data)
  135. {
  136. struct cdx_device *cdx_dev = to_cdx_device(dev);
  137. struct cdx_controller *cdx = cdx_dev->cdx;
  138. if (cdx_dev->is_bus) {
  139. device_for_each_child(dev, NULL, cdx_unregister_device);
  140. if (cdx_dev->enabled && cdx->ops->bus_disable)
  141. cdx->ops->bus_disable(cdx, cdx_dev->bus_num);
  142. } else {
  143. cdx_destroy_res_attr(cdx_dev, MAX_CDX_DEV_RESOURCES);
  144. debugfs_remove_recursive(cdx_dev->debugfs_dir);
  145. kfree(cdx_dev->driver_override);
  146. cdx_dev->driver_override = NULL;
  147. }
  148. /*
  149. * Do not free cdx_dev here as it would be freed in
  150. * cdx_device_release() called from within put_device().
  151. */
  152. device_del(&cdx_dev->dev);
  153. put_device(&cdx_dev->dev);
  154. return 0;
  155. }
  156. static void cdx_unregister_devices(const struct bus_type *bus)
  157. {
  158. /* Reset all the devices attached to cdx bus */
  159. bus_for_each_dev(bus, NULL, NULL, cdx_unregister_device);
  160. }
  161. /**
  162. * cdx_match_one_device - Tell if a CDX device structure has a matching
  163. * CDX device id structure
  164. * @id: single CDX device id structure to match
  165. * @dev: the CDX device structure to match against
  166. *
  167. * Return: matching cdx_device_id structure or NULL if there is no match.
  168. */
  169. static inline const struct cdx_device_id *
  170. cdx_match_one_device(const struct cdx_device_id *id,
  171. const struct cdx_device *dev)
  172. {
  173. /* Use vendor ID and device ID for matching */
  174. if ((id->vendor == CDX_ANY_ID || id->vendor == dev->vendor) &&
  175. (id->device == CDX_ANY_ID || id->device == dev->device) &&
  176. (id->subvendor == CDX_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
  177. (id->subdevice == CDX_ANY_ID || id->subdevice == dev->subsystem_device) &&
  178. !((id->class ^ dev->class) & id->class_mask))
  179. return id;
  180. return NULL;
  181. }
  182. /**
  183. * cdx_match_id - See if a CDX device matches a given cdx_id table
  184. * @ids: array of CDX device ID structures to search in
  185. * @dev: the CDX device structure to match against.
  186. *
  187. * Used by a driver to check whether a CDX device is in its list of
  188. * supported devices. Returns the matching cdx_device_id structure or
  189. * NULL if there is no match.
  190. *
  191. * Return: matching cdx_device_id structure or NULL if there is no match.
  192. */
  193. static inline const struct cdx_device_id *
  194. cdx_match_id(const struct cdx_device_id *ids, struct cdx_device *dev)
  195. {
  196. if (ids) {
  197. while (ids->vendor || ids->device) {
  198. if (cdx_match_one_device(ids, dev))
  199. return ids;
  200. ids++;
  201. }
  202. }
  203. return NULL;
  204. }
  205. int cdx_set_master(struct cdx_device *cdx_dev)
  206. {
  207. struct cdx_controller *cdx = cdx_dev->cdx;
  208. struct cdx_device_config dev_config;
  209. int ret = -EOPNOTSUPP;
  210. dev_config.type = CDX_DEV_BUS_MASTER_CONF;
  211. dev_config.bus_master_enable = true;
  212. if (cdx->ops->dev_configure)
  213. ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
  214. cdx_dev->dev_num, &dev_config);
  215. return ret;
  216. }
  217. EXPORT_SYMBOL_GPL(cdx_set_master);
  218. int cdx_clear_master(struct cdx_device *cdx_dev)
  219. {
  220. struct cdx_controller *cdx = cdx_dev->cdx;
  221. struct cdx_device_config dev_config;
  222. int ret = -EOPNOTSUPP;
  223. dev_config.type = CDX_DEV_BUS_MASTER_CONF;
  224. dev_config.bus_master_enable = false;
  225. if (cdx->ops->dev_configure)
  226. ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
  227. cdx_dev->dev_num, &dev_config);
  228. return ret;
  229. }
  230. EXPORT_SYMBOL_GPL(cdx_clear_master);
  231. /**
  232. * cdx_bus_match - device to driver matching callback
  233. * @dev: the cdx device to match against
  234. * @drv: the device driver to search for matching cdx device
  235. * structures
  236. *
  237. * Return: true on success, false otherwise.
  238. */
  239. static int cdx_bus_match(struct device *dev, const struct device_driver *drv)
  240. {
  241. struct cdx_device *cdx_dev = to_cdx_device(dev);
  242. const struct cdx_driver *cdx_drv = to_cdx_driver(drv);
  243. const struct cdx_device_id *found_id = NULL;
  244. const struct cdx_device_id *ids;
  245. if (cdx_dev->is_bus)
  246. return false;
  247. ids = cdx_drv->match_id_table;
  248. /* When driver_override is set, only bind to the matching driver */
  249. if (cdx_dev->driver_override && strcmp(cdx_dev->driver_override, drv->name))
  250. return false;
  251. found_id = cdx_match_id(ids, cdx_dev);
  252. if (!found_id)
  253. return false;
  254. do {
  255. /*
  256. * In case override_only was set, enforce driver_override
  257. * matching.
  258. */
  259. if (!found_id->override_only)
  260. return true;
  261. if (cdx_dev->driver_override)
  262. return true;
  263. ids = found_id + 1;
  264. found_id = cdx_match_id(ids, cdx_dev);
  265. } while (found_id);
  266. return false;
  267. }
  268. static int cdx_probe(struct device *dev)
  269. {
  270. struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
  271. struct cdx_device *cdx_dev = to_cdx_device(dev);
  272. struct cdx_controller *cdx = cdx_dev->cdx;
  273. int error;
  274. /*
  275. * Setup MSI device data so that generic MSI alloc/free can
  276. * be used by the device driver.
  277. */
  278. if (IS_ENABLED(CONFIG_GENERIC_MSI_IRQ) && cdx->msi_domain) {
  279. error = msi_setup_device_data(&cdx_dev->dev);
  280. if (error)
  281. return error;
  282. }
  283. error = cdx_drv->probe(cdx_dev);
  284. if (error) {
  285. dev_err_probe(dev, error, "%s failed\n", __func__);
  286. return error;
  287. }
  288. return 0;
  289. }
  290. static void cdx_remove(struct device *dev)
  291. {
  292. struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
  293. struct cdx_device *cdx_dev = to_cdx_device(dev);
  294. if (cdx_drv && cdx_drv->remove)
  295. cdx_drv->remove(cdx_dev);
  296. }
  297. static void cdx_shutdown(struct device *dev)
  298. {
  299. struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
  300. struct cdx_device *cdx_dev = to_cdx_device(dev);
  301. struct cdx_controller *cdx = cdx_dev->cdx;
  302. if (cdx_dev->is_bus && cdx_dev->enabled && cdx->ops->bus_disable)
  303. cdx->ops->bus_disable(cdx, cdx_dev->bus_num);
  304. if (cdx_drv && cdx_drv->shutdown)
  305. cdx_drv->shutdown(cdx_dev);
  306. }
  307. static int cdx_dma_configure(struct device *dev)
  308. {
  309. struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
  310. struct cdx_device *cdx_dev = to_cdx_device(dev);
  311. struct cdx_controller *cdx = cdx_dev->cdx;
  312. u32 input_id = cdx_dev->req_id;
  313. int ret;
  314. ret = of_dma_configure_id(dev, cdx->dev->of_node, 0, &input_id);
  315. if (ret && ret != -EPROBE_DEFER) {
  316. dev_err(dev, "of_dma_configure_id() failed\n");
  317. return ret;
  318. }
  319. /* @cdx_drv may not be valid when we're called from the IOMMU layer */
  320. if (!ret && dev->driver && !cdx_drv->driver_managed_dma) {
  321. ret = iommu_device_use_default_domain(dev);
  322. if (ret)
  323. arch_teardown_dma_ops(dev);
  324. }
  325. return 0;
  326. }
  327. static void cdx_dma_cleanup(struct device *dev)
  328. {
  329. struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
  330. if (!cdx_drv->driver_managed_dma)
  331. iommu_device_unuse_default_domain(dev);
  332. }
  333. /* show configuration fields */
  334. #define cdx_config_attr(field, format_string) \
  335. static ssize_t \
  336. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  337. { \
  338. struct cdx_device *cdx_dev = to_cdx_device(dev); \
  339. return sysfs_emit(buf, format_string, cdx_dev->field); \
  340. } \
  341. static DEVICE_ATTR_RO(field)
  342. cdx_config_attr(vendor, "0x%04x\n");
  343. cdx_config_attr(device, "0x%04x\n");
  344. cdx_config_attr(subsystem_vendor, "0x%04x\n");
  345. cdx_config_attr(subsystem_device, "0x%04x\n");
  346. cdx_config_attr(revision, "0x%02x\n");
  347. cdx_config_attr(class, "0x%06x\n");
  348. static ssize_t remove_store(struct device *dev,
  349. struct device_attribute *attr,
  350. const char *buf, size_t count)
  351. {
  352. bool val;
  353. if (kstrtobool(buf, &val) < 0)
  354. return -EINVAL;
  355. if (!val)
  356. return -EINVAL;
  357. if (device_remove_file_self(dev, attr)) {
  358. int ret;
  359. ret = cdx_unregister_device(dev, NULL);
  360. if (ret)
  361. return ret;
  362. }
  363. return count;
  364. }
  365. static DEVICE_ATTR_WO(remove);
  366. static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
  367. const char *buf, size_t count)
  368. {
  369. struct cdx_device *cdx_dev = to_cdx_device(dev);
  370. bool val;
  371. int ret;
  372. if (kstrtobool(buf, &val) < 0)
  373. return -EINVAL;
  374. if (!val)
  375. return -EINVAL;
  376. if (cdx_dev->is_bus)
  377. /* Reset all the devices attached to cdx bus */
  378. ret = device_for_each_child(dev, NULL, reset_cdx_device);
  379. else
  380. ret = cdx_dev_reset(dev);
  381. return ret < 0 ? ret : count;
  382. }
  383. static DEVICE_ATTR_WO(reset);
  384. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  385. char *buf)
  386. {
  387. struct cdx_device *cdx_dev = to_cdx_device(dev);
  388. return sprintf(buf, "cdx:v%04Xd%04Xsv%04Xsd%04Xc%06X\n", cdx_dev->vendor,
  389. cdx_dev->device, cdx_dev->subsystem_vendor, cdx_dev->subsystem_device,
  390. cdx_dev->class);
  391. }
  392. static DEVICE_ATTR_RO(modalias);
  393. static ssize_t driver_override_store(struct device *dev,
  394. struct device_attribute *attr,
  395. const char *buf, size_t count)
  396. {
  397. struct cdx_device *cdx_dev = to_cdx_device(dev);
  398. int ret;
  399. if (WARN_ON(dev->bus != &cdx_bus_type))
  400. return -EINVAL;
  401. ret = driver_set_override(dev, &cdx_dev->driver_override, buf, count);
  402. if (ret)
  403. return ret;
  404. return count;
  405. }
  406. static ssize_t driver_override_show(struct device *dev,
  407. struct device_attribute *attr, char *buf)
  408. {
  409. struct cdx_device *cdx_dev = to_cdx_device(dev);
  410. ssize_t len;
  411. device_lock(dev);
  412. len = sysfs_emit(buf, "%s\n", cdx_dev->driver_override);
  413. device_unlock(dev);
  414. return len;
  415. }
  416. static DEVICE_ATTR_RW(driver_override);
  417. static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
  418. const char *buf, size_t count)
  419. {
  420. struct cdx_device *cdx_dev = to_cdx_device(dev);
  421. struct cdx_controller *cdx = cdx_dev->cdx;
  422. bool enable;
  423. int ret;
  424. if (kstrtobool(buf, &enable) < 0)
  425. return -EINVAL;
  426. if (enable == cdx_dev->enabled)
  427. return count;
  428. if (enable && cdx->ops->bus_enable)
  429. ret = cdx->ops->bus_enable(cdx, cdx_dev->bus_num);
  430. else if (!enable && cdx->ops->bus_disable)
  431. ret = cdx->ops->bus_disable(cdx, cdx_dev->bus_num);
  432. else
  433. ret = -EOPNOTSUPP;
  434. if (!ret)
  435. cdx_dev->enabled = enable;
  436. return ret < 0 ? ret : count;
  437. }
  438. static ssize_t enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  439. {
  440. struct cdx_device *cdx_dev = to_cdx_device(dev);
  441. return sysfs_emit(buf, "%u\n", cdx_dev->enabled);
  442. }
  443. static DEVICE_ATTR_RW(enable);
  444. static umode_t cdx_dev_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n)
  445. {
  446. struct device *dev = kobj_to_dev(kobj);
  447. struct cdx_device *cdx_dev;
  448. cdx_dev = to_cdx_device(dev);
  449. if (!cdx_dev->is_bus)
  450. return a->mode;
  451. return 0;
  452. }
  453. static umode_t cdx_bus_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n)
  454. {
  455. struct device *dev = kobj_to_dev(kobj);
  456. struct cdx_device *cdx_dev;
  457. cdx_dev = to_cdx_device(dev);
  458. if (cdx_dev->is_bus)
  459. return a->mode;
  460. return 0;
  461. }
  462. static struct attribute *cdx_dev_attrs[] = {
  463. &dev_attr_remove.attr,
  464. &dev_attr_reset.attr,
  465. &dev_attr_vendor.attr,
  466. &dev_attr_device.attr,
  467. &dev_attr_subsystem_vendor.attr,
  468. &dev_attr_subsystem_device.attr,
  469. &dev_attr_class.attr,
  470. &dev_attr_revision.attr,
  471. &dev_attr_modalias.attr,
  472. &dev_attr_driver_override.attr,
  473. NULL,
  474. };
  475. static const struct attribute_group cdx_dev_group = {
  476. .attrs = cdx_dev_attrs,
  477. .is_visible = cdx_dev_attrs_are_visible,
  478. };
  479. static struct attribute *cdx_bus_dev_attrs[] = {
  480. &dev_attr_enable.attr,
  481. &dev_attr_reset.attr,
  482. NULL,
  483. };
  484. static const struct attribute_group cdx_bus_dev_group = {
  485. .attrs = cdx_bus_dev_attrs,
  486. .is_visible = cdx_bus_attrs_are_visible,
  487. };
  488. static const struct attribute_group *cdx_dev_groups[] = {
  489. &cdx_dev_group,
  490. &cdx_bus_dev_group,
  491. NULL,
  492. };
  493. static int cdx_debug_resource_show(struct seq_file *s, void *data)
  494. {
  495. struct cdx_device *cdx_dev = s->private;
  496. int i;
  497. for (i = 0; i < MAX_CDX_DEV_RESOURCES; i++) {
  498. struct resource *res = &cdx_dev->res[i];
  499. seq_printf(s, "%pr\n", res);
  500. }
  501. return 0;
  502. }
  503. DEFINE_SHOW_ATTRIBUTE(cdx_debug_resource);
  504. static void cdx_device_debugfs_init(struct cdx_device *cdx_dev)
  505. {
  506. cdx_dev->debugfs_dir = debugfs_create_dir(dev_name(&cdx_dev->dev), cdx_debugfs_dir);
  507. if (IS_ERR(cdx_dev->debugfs_dir))
  508. return;
  509. debugfs_create_file("resource", 0444, cdx_dev->debugfs_dir, cdx_dev,
  510. &cdx_debug_resource_fops);
  511. }
  512. static ssize_t rescan_store(const struct bus_type *bus,
  513. const char *buf, size_t count)
  514. {
  515. struct cdx_controller *cdx;
  516. struct platform_device *pd;
  517. bool val;
  518. if (kstrtobool(buf, &val) < 0)
  519. return -EINVAL;
  520. if (!val)
  521. return -EINVAL;
  522. guard(mutex)(&cdx_controller_lock);
  523. /* Unregister all the devices on the bus */
  524. cdx_unregister_devices(&cdx_bus_type);
  525. /* Rescan all the devices */
  526. for_each_compatible_node_scoped(np, NULL, compat_node_name) {
  527. pd = of_find_device_by_node(np);
  528. if (!pd)
  529. return -EINVAL;
  530. cdx = platform_get_drvdata(pd);
  531. if (cdx && cdx->controller_registered && cdx->ops->scan)
  532. cdx->ops->scan(cdx);
  533. put_device(&pd->dev);
  534. }
  535. return count;
  536. }
  537. static BUS_ATTR_WO(rescan);
  538. static struct attribute *cdx_bus_attrs[] = {
  539. &bus_attr_rescan.attr,
  540. NULL,
  541. };
  542. ATTRIBUTE_GROUPS(cdx_bus);
  543. const struct bus_type cdx_bus_type = {
  544. .name = "cdx",
  545. .match = cdx_bus_match,
  546. .probe = cdx_probe,
  547. .remove = cdx_remove,
  548. .shutdown = cdx_shutdown,
  549. .dma_configure = cdx_dma_configure,
  550. .dma_cleanup = cdx_dma_cleanup,
  551. .bus_groups = cdx_bus_groups,
  552. .dev_groups = cdx_dev_groups,
  553. };
  554. EXPORT_SYMBOL_GPL(cdx_bus_type);
  555. int __cdx_driver_register(struct cdx_driver *cdx_driver,
  556. struct module *owner)
  557. {
  558. int error;
  559. cdx_driver->driver.owner = owner;
  560. cdx_driver->driver.bus = &cdx_bus_type;
  561. error = driver_register(&cdx_driver->driver);
  562. if (error) {
  563. pr_err("driver_register() failed for %s: %d\n",
  564. cdx_driver->driver.name, error);
  565. return error;
  566. }
  567. return 0;
  568. }
  569. EXPORT_SYMBOL_GPL(__cdx_driver_register);
  570. void cdx_driver_unregister(struct cdx_driver *cdx_driver)
  571. {
  572. driver_unregister(&cdx_driver->driver);
  573. }
  574. EXPORT_SYMBOL_GPL(cdx_driver_unregister);
  575. static void cdx_device_release(struct device *dev)
  576. {
  577. struct cdx_device *cdx_dev = to_cdx_device(dev);
  578. kfree(cdx_dev);
  579. }
  580. static const struct vm_operations_struct cdx_phys_vm_ops = {
  581. #ifdef CONFIG_HAVE_IOREMAP_PROT
  582. .access = generic_access_phys,
  583. #endif
  584. };
  585. /**
  586. * cdx_mmap_resource - map a CDX resource into user memory space
  587. * @fp: File pointer. Not used in this function, but required where
  588. * this API is registered as a callback.
  589. * @kobj: kobject for mapping
  590. * @attr: struct bin_attribute for the file being mapped
  591. * @vma: struct vm_area_struct passed into the mmap
  592. *
  593. * Use the regular CDX mapping routines to map a CDX resource into userspace.
  594. *
  595. * Return: true on success, false otherwise.
  596. */
  597. static int cdx_mmap_resource(struct file *fp, struct kobject *kobj,
  598. const struct bin_attribute *attr,
  599. struct vm_area_struct *vma)
  600. {
  601. struct cdx_device *cdx_dev = to_cdx_device(kobj_to_dev(kobj));
  602. int num = (unsigned long)attr->private;
  603. struct resource *res;
  604. unsigned long size;
  605. res = &cdx_dev->res[num];
  606. if (iomem_is_exclusive(res->start))
  607. return -EINVAL;
  608. /* Make sure the caller is mapping a valid resource for this device */
  609. size = ((cdx_resource_len(cdx_dev, num) - 1) >> PAGE_SHIFT) + 1;
  610. if (vma->vm_pgoff + vma_pages(vma) > size)
  611. return -EINVAL;
  612. /*
  613. * Map memory region and vm->vm_pgoff is expected to be an
  614. * offset within that region.
  615. */
  616. vma->vm_page_prot = pgprot_device(vma->vm_page_prot);
  617. vma->vm_pgoff += (cdx_resource_start(cdx_dev, num) >> PAGE_SHIFT);
  618. vma->vm_ops = &cdx_phys_vm_ops;
  619. return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  620. vma->vm_end - vma->vm_start,
  621. vma->vm_page_prot);
  622. }
  623. static void cdx_destroy_res_attr(struct cdx_device *cdx_dev, int num)
  624. {
  625. int i;
  626. /* removing the bin attributes */
  627. for (i = 0; i < num; i++) {
  628. struct bin_attribute *res_attr;
  629. res_attr = cdx_dev->res_attr[i];
  630. if (res_attr) {
  631. sysfs_remove_bin_file(&cdx_dev->dev.kobj, res_attr);
  632. kfree(res_attr);
  633. }
  634. }
  635. }
  636. #define CDX_RES_ATTR_NAME_LEN 10
  637. static int cdx_create_res_attr(struct cdx_device *cdx_dev, int num)
  638. {
  639. struct bin_attribute *res_attr;
  640. char *res_attr_name;
  641. int ret;
  642. res_attr = kzalloc(sizeof(*res_attr) + CDX_RES_ATTR_NAME_LEN, GFP_ATOMIC);
  643. if (!res_attr)
  644. return -ENOMEM;
  645. res_attr_name = (char *)(res_attr + 1);
  646. sysfs_bin_attr_init(res_attr);
  647. cdx_dev->res_attr[num] = res_attr;
  648. sprintf(res_attr_name, "resource%d", num);
  649. res_attr->mmap = cdx_mmap_resource;
  650. res_attr->attr.name = res_attr_name;
  651. res_attr->attr.mode = 0600;
  652. res_attr->size = cdx_resource_len(cdx_dev, num);
  653. res_attr->private = (void *)(unsigned long)num;
  654. ret = sysfs_create_bin_file(&cdx_dev->dev.kobj, res_attr);
  655. if (ret)
  656. kfree(res_attr);
  657. return ret;
  658. }
  659. int cdx_device_add(struct cdx_dev_params *dev_params)
  660. {
  661. struct cdx_controller *cdx = dev_params->cdx;
  662. struct cdx_device *cdx_dev;
  663. int ret, i;
  664. cdx_dev = kzalloc_obj(*cdx_dev);
  665. if (!cdx_dev)
  666. return -ENOMEM;
  667. /* Populate resource */
  668. memcpy(cdx_dev->res, dev_params->res, sizeof(struct resource) *
  669. dev_params->res_count);
  670. cdx_dev->res_count = dev_params->res_count;
  671. /* Populate CDX dev params */
  672. cdx_dev->req_id = dev_params->req_id;
  673. cdx_dev->msi_dev_id = dev_params->msi_dev_id;
  674. cdx_dev->vendor = dev_params->vendor;
  675. cdx_dev->device = dev_params->device;
  676. cdx_dev->subsystem_vendor = dev_params->subsys_vendor;
  677. cdx_dev->subsystem_device = dev_params->subsys_device;
  678. cdx_dev->class = dev_params->class;
  679. cdx_dev->revision = dev_params->revision;
  680. cdx_dev->bus_num = dev_params->bus_num;
  681. cdx_dev->dev_num = dev_params->dev_num;
  682. cdx_dev->cdx = dev_params->cdx;
  683. cdx_dev->dma_mask = CDX_DEFAULT_DMA_MASK;
  684. /* Initialize generic device */
  685. device_initialize(&cdx_dev->dev);
  686. cdx_dev->dev.parent = dev_params->parent;
  687. cdx_dev->dev.bus = &cdx_bus_type;
  688. cdx_dev->dev.dma_mask = &cdx_dev->dma_mask;
  689. cdx_dev->dev.release = cdx_device_release;
  690. cdx_dev->msi_write_pending = false;
  691. mutex_init(&cdx_dev->irqchip_lock);
  692. /* Set Name */
  693. dev_set_name(&cdx_dev->dev, "cdx-%02x:%02x",
  694. ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (cdx_dev->bus_num & CDX_BUS_NUM_MASK)),
  695. cdx_dev->dev_num);
  696. if (IS_ENABLED(CONFIG_GENERIC_MSI_IRQ) && cdx->msi_domain) {
  697. cdx_dev->num_msi = dev_params->num_msi;
  698. dev_set_msi_domain(&cdx_dev->dev, cdx->msi_domain);
  699. }
  700. ret = device_add(&cdx_dev->dev);
  701. if (ret) {
  702. dev_err(&cdx_dev->dev,
  703. "cdx device add failed: %d", ret);
  704. goto fail;
  705. }
  706. /* Create resource<N> attributes */
  707. for (i = 0; i < MAX_CDX_DEV_RESOURCES; i++) {
  708. if (cdx_resource_flags(cdx_dev, i) & IORESOURCE_MEM) {
  709. /* skip empty resources */
  710. if (!cdx_resource_len(cdx_dev, i))
  711. continue;
  712. ret = cdx_create_res_attr(cdx_dev, i);
  713. if (ret != 0) {
  714. dev_err(&cdx_dev->dev,
  715. "cdx device resource<%d> file creation failed: %d", i, ret);
  716. goto resource_create_fail;
  717. }
  718. }
  719. }
  720. cdx_device_debugfs_init(cdx_dev);
  721. return 0;
  722. resource_create_fail:
  723. cdx_destroy_res_attr(cdx_dev, i);
  724. device_del(&cdx_dev->dev);
  725. fail:
  726. /*
  727. * Do not free cdx_dev here as it would be freed in
  728. * cdx_device_release() called from put_device().
  729. */
  730. put_device(&cdx_dev->dev);
  731. return ret;
  732. }
  733. EXPORT_SYMBOL_NS_GPL(cdx_device_add, "CDX_BUS_CONTROLLER");
  734. struct device *cdx_bus_add(struct cdx_controller *cdx, u8 bus_num)
  735. {
  736. struct cdx_device *cdx_dev;
  737. int ret;
  738. cdx_dev = kzalloc_obj(*cdx_dev);
  739. if (!cdx_dev)
  740. return NULL;
  741. device_initialize(&cdx_dev->dev);
  742. cdx_dev->cdx = cdx;
  743. cdx_dev->dev.parent = cdx->dev;
  744. cdx_dev->dev.bus = &cdx_bus_type;
  745. cdx_dev->dev.release = cdx_device_release;
  746. cdx_dev->is_bus = true;
  747. cdx_dev->bus_num = bus_num;
  748. dev_set_name(&cdx_dev->dev, "cdx-%02x",
  749. ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (bus_num & CDX_BUS_NUM_MASK)));
  750. ret = device_add(&cdx_dev->dev);
  751. if (ret) {
  752. dev_err(&cdx_dev->dev, "cdx bus device add failed: %d\n", ret);
  753. goto device_add_fail;
  754. }
  755. if (cdx->ops->bus_enable) {
  756. ret = cdx->ops->bus_enable(cdx, bus_num);
  757. if (ret && ret != -EALREADY) {
  758. dev_err(cdx->dev, "cdx bus enable failed: %d\n", ret);
  759. goto bus_enable_fail;
  760. }
  761. }
  762. cdx_dev->enabled = true;
  763. return &cdx_dev->dev;
  764. bus_enable_fail:
  765. device_del(&cdx_dev->dev);
  766. device_add_fail:
  767. put_device(&cdx_dev->dev);
  768. return NULL;
  769. }
  770. EXPORT_SYMBOL_NS_GPL(cdx_bus_add, "CDX_BUS_CONTROLLER");
  771. int cdx_register_controller(struct cdx_controller *cdx)
  772. {
  773. int ret;
  774. ret = ida_alloc_range(&cdx_controller_ida, 0, MAX_CDX_CONTROLLERS - 1, GFP_KERNEL);
  775. if (ret < 0) {
  776. dev_err(cdx->dev,
  777. "No free index available. Maximum controllers already registered\n");
  778. cdx->id = (u8)MAX_CDX_CONTROLLERS;
  779. return ret;
  780. }
  781. mutex_lock(&cdx_controller_lock);
  782. cdx->id = ret;
  783. /* Scan all the devices */
  784. if (cdx->ops->scan)
  785. cdx->ops->scan(cdx);
  786. cdx->controller_registered = true;
  787. mutex_unlock(&cdx_controller_lock);
  788. return 0;
  789. }
  790. EXPORT_SYMBOL_NS_GPL(cdx_register_controller, "CDX_BUS_CONTROLLER");
  791. void cdx_unregister_controller(struct cdx_controller *cdx)
  792. {
  793. if (cdx->id >= MAX_CDX_CONTROLLERS)
  794. return;
  795. mutex_lock(&cdx_controller_lock);
  796. cdx->controller_registered = false;
  797. device_for_each_child(cdx->dev, NULL, cdx_unregister_device);
  798. ida_free(&cdx_controller_ida, cdx->id);
  799. mutex_unlock(&cdx_controller_lock);
  800. }
  801. EXPORT_SYMBOL_NS_GPL(cdx_unregister_controller, "CDX_BUS_CONTROLLER");
  802. static int __init cdx_bus_init(void)
  803. {
  804. int ret;
  805. ret = bus_register(&cdx_bus_type);
  806. if (!ret)
  807. cdx_debugfs_dir = debugfs_create_dir(cdx_bus_type.name, NULL);
  808. return ret;
  809. }
  810. postcore_initcall(cdx_bus_init);