cdx.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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(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 (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. if (cdx_drv && cdx_drv->shutdown)
  302. cdx_drv->shutdown(cdx_dev);
  303. }
  304. static int cdx_dma_configure(struct device *dev)
  305. {
  306. struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
  307. struct cdx_device *cdx_dev = to_cdx_device(dev);
  308. struct cdx_controller *cdx = cdx_dev->cdx;
  309. u32 input_id = cdx_dev->req_id;
  310. int ret;
  311. ret = of_dma_configure_id(dev, cdx->dev->of_node, 0, &input_id);
  312. if (ret && ret != -EPROBE_DEFER) {
  313. dev_err(dev, "of_dma_configure_id() failed\n");
  314. return ret;
  315. }
  316. if (!ret && !cdx_drv->driver_managed_dma) {
  317. ret = iommu_device_use_default_domain(dev);
  318. if (ret)
  319. arch_teardown_dma_ops(dev);
  320. }
  321. return 0;
  322. }
  323. static void cdx_dma_cleanup(struct device *dev)
  324. {
  325. struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
  326. if (!cdx_drv->driver_managed_dma)
  327. iommu_device_unuse_default_domain(dev);
  328. }
  329. /* show configuration fields */
  330. #define cdx_config_attr(field, format_string) \
  331. static ssize_t \
  332. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  333. { \
  334. struct cdx_device *cdx_dev = to_cdx_device(dev); \
  335. return sysfs_emit(buf, format_string, cdx_dev->field); \
  336. } \
  337. static DEVICE_ATTR_RO(field)
  338. cdx_config_attr(vendor, "0x%04x\n");
  339. cdx_config_attr(device, "0x%04x\n");
  340. cdx_config_attr(subsystem_vendor, "0x%04x\n");
  341. cdx_config_attr(subsystem_device, "0x%04x\n");
  342. cdx_config_attr(revision, "0x%02x\n");
  343. cdx_config_attr(class, "0x%06x\n");
  344. static ssize_t remove_store(struct device *dev,
  345. struct device_attribute *attr,
  346. const char *buf, size_t count)
  347. {
  348. bool val;
  349. if (kstrtobool(buf, &val) < 0)
  350. return -EINVAL;
  351. if (!val)
  352. return -EINVAL;
  353. if (device_remove_file_self(dev, attr)) {
  354. int ret;
  355. ret = cdx_unregister_device(dev, NULL);
  356. if (ret)
  357. return ret;
  358. }
  359. return count;
  360. }
  361. static DEVICE_ATTR_WO(remove);
  362. static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
  363. const char *buf, size_t count)
  364. {
  365. struct cdx_device *cdx_dev = to_cdx_device(dev);
  366. bool val;
  367. int ret;
  368. if (kstrtobool(buf, &val) < 0)
  369. return -EINVAL;
  370. if (!val)
  371. return -EINVAL;
  372. if (cdx_dev->is_bus)
  373. /* Reset all the devices attached to cdx bus */
  374. ret = device_for_each_child(dev, NULL, reset_cdx_device);
  375. else
  376. ret = cdx_dev_reset(dev);
  377. return ret < 0 ? ret : count;
  378. }
  379. static DEVICE_ATTR_WO(reset);
  380. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  381. char *buf)
  382. {
  383. struct cdx_device *cdx_dev = to_cdx_device(dev);
  384. return sprintf(buf, "cdx:v%04Xd%04Xsv%04Xsd%04Xc%06X\n", cdx_dev->vendor,
  385. cdx_dev->device, cdx_dev->subsystem_vendor, cdx_dev->subsystem_device,
  386. cdx_dev->class);
  387. }
  388. static DEVICE_ATTR_RO(modalias);
  389. static ssize_t driver_override_store(struct device *dev,
  390. struct device_attribute *attr,
  391. const char *buf, size_t count)
  392. {
  393. struct cdx_device *cdx_dev = to_cdx_device(dev);
  394. int ret;
  395. if (WARN_ON(dev->bus != &cdx_bus_type))
  396. return -EINVAL;
  397. ret = driver_set_override(dev, &cdx_dev->driver_override, buf, count);
  398. if (ret)
  399. return ret;
  400. return count;
  401. }
  402. static ssize_t driver_override_show(struct device *dev,
  403. struct device_attribute *attr, char *buf)
  404. {
  405. struct cdx_device *cdx_dev = to_cdx_device(dev);
  406. ssize_t len;
  407. device_lock(dev);
  408. len = sysfs_emit(buf, "%s\n", cdx_dev->driver_override);
  409. device_unlock(dev);
  410. return len;
  411. }
  412. static DEVICE_ATTR_RW(driver_override);
  413. static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
  414. const char *buf, size_t count)
  415. {
  416. struct cdx_device *cdx_dev = to_cdx_device(dev);
  417. struct cdx_controller *cdx = cdx_dev->cdx;
  418. bool enable;
  419. int ret;
  420. if (kstrtobool(buf, &enable) < 0)
  421. return -EINVAL;
  422. if (enable == cdx_dev->enabled)
  423. return count;
  424. if (enable && cdx->ops->bus_enable)
  425. ret = cdx->ops->bus_enable(cdx, cdx_dev->bus_num);
  426. else if (!enable && cdx->ops->bus_disable)
  427. ret = cdx->ops->bus_disable(cdx, cdx_dev->bus_num);
  428. else
  429. ret = -EOPNOTSUPP;
  430. if (!ret)
  431. cdx_dev->enabled = enable;
  432. return ret < 0 ? ret : count;
  433. }
  434. static ssize_t enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  435. {
  436. struct cdx_device *cdx_dev = to_cdx_device(dev);
  437. return sysfs_emit(buf, "%u\n", cdx_dev->enabled);
  438. }
  439. static DEVICE_ATTR_RW(enable);
  440. static umode_t cdx_dev_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n)
  441. {
  442. struct device *dev = kobj_to_dev(kobj);
  443. struct cdx_device *cdx_dev;
  444. cdx_dev = to_cdx_device(dev);
  445. if (!cdx_dev->is_bus)
  446. return a->mode;
  447. return 0;
  448. }
  449. static umode_t cdx_bus_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n)
  450. {
  451. struct device *dev = kobj_to_dev(kobj);
  452. struct cdx_device *cdx_dev;
  453. cdx_dev = to_cdx_device(dev);
  454. if (cdx_dev->is_bus)
  455. return a->mode;
  456. return 0;
  457. }
  458. static struct attribute *cdx_dev_attrs[] = {
  459. &dev_attr_remove.attr,
  460. &dev_attr_reset.attr,
  461. &dev_attr_vendor.attr,
  462. &dev_attr_device.attr,
  463. &dev_attr_subsystem_vendor.attr,
  464. &dev_attr_subsystem_device.attr,
  465. &dev_attr_class.attr,
  466. &dev_attr_revision.attr,
  467. &dev_attr_modalias.attr,
  468. &dev_attr_driver_override.attr,
  469. NULL,
  470. };
  471. static const struct attribute_group cdx_dev_group = {
  472. .attrs = cdx_dev_attrs,
  473. .is_visible = cdx_dev_attrs_are_visible,
  474. };
  475. static struct attribute *cdx_bus_dev_attrs[] = {
  476. &dev_attr_enable.attr,
  477. &dev_attr_reset.attr,
  478. NULL,
  479. };
  480. static const struct attribute_group cdx_bus_dev_group = {
  481. .attrs = cdx_bus_dev_attrs,
  482. .is_visible = cdx_bus_attrs_are_visible,
  483. };
  484. static const struct attribute_group *cdx_dev_groups[] = {
  485. &cdx_dev_group,
  486. &cdx_bus_dev_group,
  487. NULL,
  488. };
  489. static int cdx_debug_resource_show(struct seq_file *s, void *data)
  490. {
  491. struct cdx_device *cdx_dev = s->private;
  492. int i;
  493. for (i = 0; i < MAX_CDX_DEV_RESOURCES; i++) {
  494. struct resource *res = &cdx_dev->res[i];
  495. seq_printf(s, "%pr\n", res);
  496. }
  497. return 0;
  498. }
  499. DEFINE_SHOW_ATTRIBUTE(cdx_debug_resource);
  500. static void cdx_device_debugfs_init(struct cdx_device *cdx_dev)
  501. {
  502. cdx_dev->debugfs_dir = debugfs_create_dir(dev_name(&cdx_dev->dev), cdx_debugfs_dir);
  503. if (IS_ERR(cdx_dev->debugfs_dir))
  504. return;
  505. debugfs_create_file("resource", 0444, cdx_dev->debugfs_dir, cdx_dev,
  506. &cdx_debug_resource_fops);
  507. }
  508. static ssize_t rescan_store(const struct bus_type *bus,
  509. const char *buf, size_t count)
  510. {
  511. struct cdx_controller *cdx;
  512. struct platform_device *pd;
  513. struct device_node *np;
  514. bool val;
  515. if (kstrtobool(buf, &val) < 0)
  516. return -EINVAL;
  517. if (!val)
  518. return -EINVAL;
  519. mutex_lock(&cdx_controller_lock);
  520. /* Unregister all the devices on the bus */
  521. cdx_unregister_devices(&cdx_bus_type);
  522. /* Rescan all the devices */
  523. for_each_compatible_node(np, NULL, compat_node_name) {
  524. pd = of_find_device_by_node(np);
  525. if (!pd) {
  526. of_node_put(np);
  527. count = -EINVAL;
  528. goto unlock;
  529. }
  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. unlock:
  536. mutex_unlock(&cdx_controller_lock);
  537. return count;
  538. }
  539. static BUS_ATTR_WO(rescan);
  540. static struct attribute *cdx_bus_attrs[] = {
  541. &bus_attr_rescan.attr,
  542. NULL,
  543. };
  544. ATTRIBUTE_GROUPS(cdx_bus);
  545. struct bus_type cdx_bus_type = {
  546. .name = "cdx",
  547. .match = cdx_bus_match,
  548. .probe = cdx_probe,
  549. .remove = cdx_remove,
  550. .shutdown = cdx_shutdown,
  551. .dma_configure = cdx_dma_configure,
  552. .dma_cleanup = cdx_dma_cleanup,
  553. .bus_groups = cdx_bus_groups,
  554. .dev_groups = cdx_dev_groups,
  555. };
  556. EXPORT_SYMBOL_GPL(cdx_bus_type);
  557. int __cdx_driver_register(struct cdx_driver *cdx_driver,
  558. struct module *owner)
  559. {
  560. int error;
  561. cdx_driver->driver.owner = owner;
  562. cdx_driver->driver.bus = &cdx_bus_type;
  563. error = driver_register(&cdx_driver->driver);
  564. if (error) {
  565. pr_err("driver_register() failed for %s: %d\n",
  566. cdx_driver->driver.name, error);
  567. return error;
  568. }
  569. return 0;
  570. }
  571. EXPORT_SYMBOL_GPL(__cdx_driver_register);
  572. void cdx_driver_unregister(struct cdx_driver *cdx_driver)
  573. {
  574. driver_unregister(&cdx_driver->driver);
  575. }
  576. EXPORT_SYMBOL_GPL(cdx_driver_unregister);
  577. static void cdx_device_release(struct device *dev)
  578. {
  579. struct cdx_device *cdx_dev = to_cdx_device(dev);
  580. kfree(cdx_dev);
  581. }
  582. static const struct vm_operations_struct cdx_phys_vm_ops = {
  583. #ifdef CONFIG_HAVE_IOREMAP_PROT
  584. .access = generic_access_phys,
  585. #endif
  586. };
  587. /**
  588. * cdx_mmap_resource - map a CDX resource into user memory space
  589. * @fp: File pointer. Not used in this function, but required where
  590. * this API is registered as a callback.
  591. * @kobj: kobject for mapping
  592. * @attr: struct bin_attribute for the file being mapped
  593. * @vma: struct vm_area_struct passed into the mmap
  594. *
  595. * Use the regular CDX mapping routines to map a CDX resource into userspace.
  596. *
  597. * Return: true on success, false otherwise.
  598. */
  599. static int cdx_mmap_resource(struct file *fp, struct kobject *kobj,
  600. struct bin_attribute *attr,
  601. struct vm_area_struct *vma)
  602. {
  603. struct cdx_device *cdx_dev = to_cdx_device(kobj_to_dev(kobj));
  604. int num = (unsigned long)attr->private;
  605. struct resource *res;
  606. unsigned long size;
  607. res = &cdx_dev->res[num];
  608. if (iomem_is_exclusive(res->start))
  609. return -EINVAL;
  610. /* Make sure the caller is mapping a valid resource for this device */
  611. size = ((cdx_resource_len(cdx_dev, num) - 1) >> PAGE_SHIFT) + 1;
  612. if (vma->vm_pgoff + vma_pages(vma) > size)
  613. return -EINVAL;
  614. /*
  615. * Map memory region and vm->vm_pgoff is expected to be an
  616. * offset within that region.
  617. */
  618. vma->vm_page_prot = pgprot_device(vma->vm_page_prot);
  619. vma->vm_pgoff += (cdx_resource_start(cdx_dev, num) >> PAGE_SHIFT);
  620. vma->vm_ops = &cdx_phys_vm_ops;
  621. return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  622. vma->vm_end - vma->vm_start,
  623. vma->vm_page_prot);
  624. }
  625. static void cdx_destroy_res_attr(struct cdx_device *cdx_dev, int num)
  626. {
  627. int i;
  628. /* removing the bin attributes */
  629. for (i = 0; i < num; i++) {
  630. struct bin_attribute *res_attr;
  631. res_attr = cdx_dev->res_attr[i];
  632. if (res_attr) {
  633. sysfs_remove_bin_file(&cdx_dev->dev.kobj, res_attr);
  634. kfree(res_attr);
  635. }
  636. }
  637. }
  638. #define CDX_RES_ATTR_NAME_LEN 10
  639. static int cdx_create_res_attr(struct cdx_device *cdx_dev, int num)
  640. {
  641. struct bin_attribute *res_attr;
  642. char *res_attr_name;
  643. int ret;
  644. res_attr = kzalloc(sizeof(*res_attr) + CDX_RES_ATTR_NAME_LEN, GFP_ATOMIC);
  645. if (!res_attr)
  646. return -ENOMEM;
  647. res_attr_name = (char *)(res_attr + 1);
  648. sysfs_bin_attr_init(res_attr);
  649. cdx_dev->res_attr[num] = res_attr;
  650. sprintf(res_attr_name, "resource%d", num);
  651. res_attr->mmap = cdx_mmap_resource;
  652. res_attr->attr.name = res_attr_name;
  653. res_attr->attr.mode = 0600;
  654. res_attr->size = cdx_resource_len(cdx_dev, num);
  655. res_attr->private = (void *)(unsigned long)num;
  656. ret = sysfs_create_bin_file(&cdx_dev->dev.kobj, res_attr);
  657. if (ret)
  658. kfree(res_attr);
  659. return ret;
  660. }
  661. int cdx_device_add(struct cdx_dev_params *dev_params)
  662. {
  663. struct cdx_controller *cdx = dev_params->cdx;
  664. struct cdx_device *cdx_dev;
  665. int ret, i;
  666. cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL);
  667. if (!cdx_dev)
  668. return -ENOMEM;
  669. /* Populate resource */
  670. memcpy(cdx_dev->res, dev_params->res, sizeof(struct resource) *
  671. dev_params->res_count);
  672. cdx_dev->res_count = dev_params->res_count;
  673. /* Populate CDX dev params */
  674. cdx_dev->req_id = dev_params->req_id;
  675. cdx_dev->msi_dev_id = dev_params->msi_dev_id;
  676. cdx_dev->vendor = dev_params->vendor;
  677. cdx_dev->device = dev_params->device;
  678. cdx_dev->subsystem_vendor = dev_params->subsys_vendor;
  679. cdx_dev->subsystem_device = dev_params->subsys_device;
  680. cdx_dev->class = dev_params->class;
  681. cdx_dev->revision = dev_params->revision;
  682. cdx_dev->bus_num = dev_params->bus_num;
  683. cdx_dev->dev_num = dev_params->dev_num;
  684. cdx_dev->cdx = dev_params->cdx;
  685. cdx_dev->dma_mask = CDX_DEFAULT_DMA_MASK;
  686. /* Initialize generic device */
  687. device_initialize(&cdx_dev->dev);
  688. cdx_dev->dev.parent = dev_params->parent;
  689. cdx_dev->dev.bus = &cdx_bus_type;
  690. cdx_dev->dev.dma_mask = &cdx_dev->dma_mask;
  691. cdx_dev->dev.release = cdx_device_release;
  692. cdx_dev->msi_write_pending = false;
  693. mutex_init(&cdx_dev->irqchip_lock);
  694. /* Set Name */
  695. dev_set_name(&cdx_dev->dev, "cdx-%02x:%02x",
  696. ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (cdx_dev->bus_num & CDX_BUS_NUM_MASK)),
  697. cdx_dev->dev_num);
  698. if (cdx->msi_domain) {
  699. cdx_dev->num_msi = dev_params->num_msi;
  700. dev_set_msi_domain(&cdx_dev->dev, cdx->msi_domain);
  701. }
  702. ret = device_add(&cdx_dev->dev);
  703. if (ret) {
  704. dev_err(&cdx_dev->dev,
  705. "cdx device add failed: %d", ret);
  706. goto fail;
  707. }
  708. /* Create resource<N> attributes */
  709. for (i = 0; i < MAX_CDX_DEV_RESOURCES; i++) {
  710. if (cdx_resource_flags(cdx_dev, i) & IORESOURCE_MEM) {
  711. /* skip empty resources */
  712. if (!cdx_resource_len(cdx_dev, i))
  713. continue;
  714. ret = cdx_create_res_attr(cdx_dev, i);
  715. if (ret != 0) {
  716. dev_err(&cdx_dev->dev,
  717. "cdx device resource<%d> file creation failed: %d", i, ret);
  718. goto resource_create_fail;
  719. }
  720. }
  721. }
  722. cdx_device_debugfs_init(cdx_dev);
  723. return 0;
  724. resource_create_fail:
  725. cdx_destroy_res_attr(cdx_dev, i);
  726. device_del(&cdx_dev->dev);
  727. fail:
  728. /*
  729. * Do not free cdx_dev here as it would be freed in
  730. * cdx_device_release() called from put_device().
  731. */
  732. put_device(&cdx_dev->dev);
  733. return ret;
  734. }
  735. EXPORT_SYMBOL_NS_GPL(cdx_device_add, CDX_BUS_CONTROLLER);
  736. struct device *cdx_bus_add(struct cdx_controller *cdx, u8 bus_num)
  737. {
  738. struct cdx_device *cdx_dev;
  739. int ret;
  740. cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL);
  741. if (!cdx_dev)
  742. return NULL;
  743. device_initialize(&cdx_dev->dev);
  744. cdx_dev->cdx = cdx;
  745. cdx_dev->dev.parent = cdx->dev;
  746. cdx_dev->dev.bus = &cdx_bus_type;
  747. cdx_dev->dev.release = cdx_device_release;
  748. cdx_dev->is_bus = true;
  749. cdx_dev->bus_num = bus_num;
  750. dev_set_name(&cdx_dev->dev, "cdx-%02x",
  751. ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (bus_num & CDX_BUS_NUM_MASK)));
  752. ret = device_add(&cdx_dev->dev);
  753. if (ret) {
  754. dev_err(&cdx_dev->dev, "cdx bus device add failed: %d\n", ret);
  755. goto device_add_fail;
  756. }
  757. if (cdx->ops->bus_enable) {
  758. ret = cdx->ops->bus_enable(cdx, bus_num);
  759. if (ret && ret != -EALREADY) {
  760. dev_err(cdx->dev, "cdx bus enable failed: %d\n", ret);
  761. goto bus_enable_fail;
  762. }
  763. }
  764. cdx_dev->enabled = true;
  765. return &cdx_dev->dev;
  766. bus_enable_fail:
  767. device_del(&cdx_dev->dev);
  768. device_add_fail:
  769. put_device(&cdx_dev->dev);
  770. return NULL;
  771. }
  772. EXPORT_SYMBOL_NS_GPL(cdx_bus_add, CDX_BUS_CONTROLLER);
  773. int cdx_register_controller(struct cdx_controller *cdx)
  774. {
  775. int ret;
  776. ret = ida_alloc_range(&cdx_controller_ida, 0, MAX_CDX_CONTROLLERS - 1, GFP_KERNEL);
  777. if (ret < 0) {
  778. dev_err(cdx->dev,
  779. "No free index available. Maximum controllers already registered\n");
  780. cdx->id = (u8)MAX_CDX_CONTROLLERS;
  781. return ret;
  782. }
  783. mutex_lock(&cdx_controller_lock);
  784. cdx->id = ret;
  785. /* Scan all the devices */
  786. if (cdx->ops->scan)
  787. cdx->ops->scan(cdx);
  788. cdx->controller_registered = true;
  789. mutex_unlock(&cdx_controller_lock);
  790. return 0;
  791. }
  792. EXPORT_SYMBOL_NS_GPL(cdx_register_controller, CDX_BUS_CONTROLLER);
  793. void cdx_unregister_controller(struct cdx_controller *cdx)
  794. {
  795. if (cdx->id >= MAX_CDX_CONTROLLERS)
  796. return;
  797. mutex_lock(&cdx_controller_lock);
  798. cdx->controller_registered = false;
  799. device_for_each_child(cdx->dev, NULL, cdx_unregister_device);
  800. ida_free(&cdx_controller_ida, cdx->id);
  801. mutex_unlock(&cdx_controller_lock);
  802. }
  803. EXPORT_SYMBOL_NS_GPL(cdx_unregister_controller, CDX_BUS_CONTROLLER);
  804. static int __init cdx_bus_init(void)
  805. {
  806. int ret;
  807. ret = bus_register(&cdx_bus_type);
  808. if (!ret)
  809. cdx_debugfs_dir = debugfs_create_dir(cdx_bus_type.name, NULL);
  810. return ret;
  811. }
  812. postcore_initcall(cdx_bus_init);