init.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
  3. #include <linux/init.h>
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/slab.h>
  7. #include <linux/pci.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/fs.h>
  13. #include <linux/io-64-nonatomic-lo-hi.h>
  14. #include <linux/device.h>
  15. #include <linux/idr.h>
  16. #include <linux/iommu.h>
  17. #include <uapi/linux/idxd.h>
  18. #include <linux/dmaengine.h>
  19. #include "../dmaengine.h"
  20. #include "registers.h"
  21. #include "idxd.h"
  22. #include "perfmon.h"
  23. MODULE_VERSION(IDXD_DRIVER_VERSION);
  24. MODULE_DESCRIPTION("Intel Data Streaming Accelerator and In-Memory Analytics Accelerator common driver");
  25. MODULE_LICENSE("GPL v2");
  26. MODULE_AUTHOR("Intel Corporation");
  27. MODULE_IMPORT_NS(IDXD);
  28. static bool sva = true;
  29. module_param(sva, bool, 0644);
  30. MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
  31. bool tc_override;
  32. module_param(tc_override, bool, 0644);
  33. MODULE_PARM_DESC(tc_override, "Override traffic class defaults");
  34. #define DRV_NAME "idxd"
  35. bool support_enqcmd;
  36. DEFINE_IDA(idxd_ida);
  37. static struct idxd_driver_data idxd_driver_data[] = {
  38. [IDXD_TYPE_DSA] = {
  39. .name_prefix = "dsa",
  40. .type = IDXD_TYPE_DSA,
  41. .compl_size = sizeof(struct dsa_completion_record),
  42. .align = 32,
  43. .dev_type = &dsa_device_type,
  44. .evl_cr_off = offsetof(struct dsa_evl_entry, cr),
  45. .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */
  46. .cr_status_off = offsetof(struct dsa_completion_record, status),
  47. .cr_result_off = offsetof(struct dsa_completion_record, result),
  48. },
  49. [IDXD_TYPE_IAX] = {
  50. .name_prefix = "iax",
  51. .type = IDXD_TYPE_IAX,
  52. .compl_size = sizeof(struct iax_completion_record),
  53. .align = 64,
  54. .dev_type = &iax_device_type,
  55. .evl_cr_off = offsetof(struct iax_evl_entry, cr),
  56. .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */
  57. .cr_status_off = offsetof(struct iax_completion_record, status),
  58. .cr_result_off = offsetof(struct iax_completion_record, error_code),
  59. .load_device_defaults = idxd_load_iaa_device_defaults,
  60. },
  61. };
  62. static struct pci_device_id idxd_pci_tbl[] = {
  63. /* DSA ver 1.0 platforms */
  64. { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
  65. /* DSA on GNR-D platforms */
  66. { PCI_DEVICE_DATA(INTEL, DSA_GNRD, &idxd_driver_data[IDXD_TYPE_DSA]) },
  67. /* DSA on DMR platforms */
  68. { PCI_DEVICE_DATA(INTEL, DSA_DMR, &idxd_driver_data[IDXD_TYPE_DSA]) },
  69. /* IAX ver 1.0 platforms */
  70. { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
  71. /* IAA on DMR platforms */
  72. { PCI_DEVICE_DATA(INTEL, IAA_DMR, &idxd_driver_data[IDXD_TYPE_IAX]) },
  73. { 0, }
  74. };
  75. MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
  76. static int idxd_setup_interrupts(struct idxd_device *idxd)
  77. {
  78. struct pci_dev *pdev = idxd->pdev;
  79. struct device *dev = &pdev->dev;
  80. struct idxd_irq_entry *ie;
  81. int i, msixcnt;
  82. int rc = 0;
  83. msixcnt = pci_msix_vec_count(pdev);
  84. if (msixcnt < 0) {
  85. dev_err(dev, "Not MSI-X interrupt capable.\n");
  86. return -ENOSPC;
  87. }
  88. idxd->irq_cnt = msixcnt;
  89. rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
  90. if (rc != msixcnt) {
  91. dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
  92. return -ENOSPC;
  93. }
  94. dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
  95. ie = idxd_get_ie(idxd, 0);
  96. ie->vector = pci_irq_vector(pdev, 0);
  97. rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie);
  98. if (rc < 0) {
  99. dev_err(dev, "Failed to allocate misc interrupt.\n");
  100. goto err_misc_irq;
  101. }
  102. dev_dbg(dev, "Requested idxd-misc handler on msix vector %d\n", ie->vector);
  103. for (i = 0; i < idxd->max_wqs; i++) {
  104. int msix_idx = i + 1;
  105. ie = idxd_get_ie(idxd, msix_idx);
  106. ie->id = msix_idx;
  107. ie->int_handle = INVALID_INT_HANDLE;
  108. ie->pasid = IOMMU_PASID_INVALID;
  109. spin_lock_init(&ie->list_lock);
  110. init_llist_head(&ie->pending_llist);
  111. INIT_LIST_HEAD(&ie->work_list);
  112. }
  113. idxd_unmask_error_interrupts(idxd);
  114. return 0;
  115. err_misc_irq:
  116. idxd_mask_error_interrupts(idxd);
  117. pci_free_irq_vectors(pdev);
  118. dev_err(dev, "No usable interrupts\n");
  119. return rc;
  120. }
  121. static void idxd_cleanup_interrupts(struct idxd_device *idxd)
  122. {
  123. struct pci_dev *pdev = idxd->pdev;
  124. struct idxd_irq_entry *ie;
  125. int msixcnt;
  126. msixcnt = pci_msix_vec_count(pdev);
  127. if (msixcnt <= 0)
  128. return;
  129. ie = idxd_get_ie(idxd, 0);
  130. idxd_mask_error_interrupts(idxd);
  131. free_irq(ie->vector, ie);
  132. pci_free_irq_vectors(pdev);
  133. }
  134. static void idxd_clean_wqs(struct idxd_device *idxd)
  135. {
  136. struct idxd_wq *wq;
  137. struct device *conf_dev;
  138. int i;
  139. for (i = 0; i < idxd->max_wqs; i++) {
  140. wq = idxd->wqs[i];
  141. if (idxd->hw.wq_cap.op_config)
  142. bitmap_free(wq->opcap_bmap);
  143. kfree(wq->wqcfg);
  144. conf_dev = wq_confdev(wq);
  145. put_device(conf_dev);
  146. kfree(wq);
  147. }
  148. bitmap_free(idxd->wq_enable_map);
  149. kfree(idxd->wqs);
  150. }
  151. static int idxd_setup_wqs(struct idxd_device *idxd)
  152. {
  153. struct device *dev = &idxd->pdev->dev;
  154. struct idxd_wq *wq;
  155. struct device *conf_dev;
  156. int i, rc;
  157. idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
  158. GFP_KERNEL, dev_to_node(dev));
  159. if (!idxd->wqs)
  160. return -ENOMEM;
  161. idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev));
  162. if (!idxd->wq_enable_map) {
  163. rc = -ENOMEM;
  164. goto err_free_wqs;
  165. }
  166. for (i = 0; i < idxd->max_wqs; i++) {
  167. wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
  168. if (!wq) {
  169. rc = -ENOMEM;
  170. goto err_unwind;
  171. }
  172. idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
  173. conf_dev = wq_confdev(wq);
  174. wq->id = i;
  175. wq->idxd = idxd;
  176. device_initialize(conf_dev);
  177. conf_dev->parent = idxd_confdev(idxd);
  178. conf_dev->bus = &dsa_bus_type;
  179. conf_dev->type = &idxd_wq_device_type;
  180. rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
  181. if (rc < 0) {
  182. put_device(conf_dev);
  183. kfree(wq);
  184. goto err_unwind;
  185. }
  186. mutex_init(&wq->wq_lock);
  187. init_waitqueue_head(&wq->err_queue);
  188. init_completion(&wq->wq_dead);
  189. init_completion(&wq->wq_resurrect);
  190. wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER;
  191. idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH);
  192. wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
  193. wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
  194. if (!wq->wqcfg) {
  195. put_device(conf_dev);
  196. kfree(wq);
  197. rc = -ENOMEM;
  198. goto err_unwind;
  199. }
  200. if (idxd->hw.wq_cap.op_config) {
  201. wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
  202. if (!wq->opcap_bmap) {
  203. kfree(wq->wqcfg);
  204. put_device(conf_dev);
  205. kfree(wq);
  206. rc = -ENOMEM;
  207. goto err_unwind;
  208. }
  209. bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
  210. }
  211. mutex_init(&wq->uc_lock);
  212. xa_init(&wq->upasid_xa);
  213. idxd->wqs[i] = wq;
  214. }
  215. return 0;
  216. err_unwind:
  217. while (--i >= 0) {
  218. wq = idxd->wqs[i];
  219. if (idxd->hw.wq_cap.op_config)
  220. bitmap_free(wq->opcap_bmap);
  221. kfree(wq->wqcfg);
  222. conf_dev = wq_confdev(wq);
  223. put_device(conf_dev);
  224. kfree(wq);
  225. }
  226. bitmap_free(idxd->wq_enable_map);
  227. err_free_wqs:
  228. kfree(idxd->wqs);
  229. return rc;
  230. }
  231. static void idxd_clean_engines(struct idxd_device *idxd)
  232. {
  233. struct idxd_engine *engine;
  234. struct device *conf_dev;
  235. int i;
  236. for (i = 0; i < idxd->max_engines; i++) {
  237. engine = idxd->engines[i];
  238. conf_dev = engine_confdev(engine);
  239. put_device(conf_dev);
  240. kfree(engine);
  241. }
  242. kfree(idxd->engines);
  243. }
  244. static int idxd_setup_engines(struct idxd_device *idxd)
  245. {
  246. struct idxd_engine *engine;
  247. struct device *dev = &idxd->pdev->dev;
  248. struct device *conf_dev;
  249. int i, rc;
  250. idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
  251. GFP_KERNEL, dev_to_node(dev));
  252. if (!idxd->engines)
  253. return -ENOMEM;
  254. for (i = 0; i < idxd->max_engines; i++) {
  255. engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
  256. if (!engine) {
  257. rc = -ENOMEM;
  258. goto err;
  259. }
  260. idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE);
  261. conf_dev = engine_confdev(engine);
  262. engine->id = i;
  263. engine->idxd = idxd;
  264. device_initialize(conf_dev);
  265. conf_dev->parent = idxd_confdev(idxd);
  266. conf_dev->bus = &dsa_bus_type;
  267. conf_dev->type = &idxd_engine_device_type;
  268. rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
  269. if (rc < 0) {
  270. put_device(conf_dev);
  271. kfree(engine);
  272. goto err;
  273. }
  274. idxd->engines[i] = engine;
  275. }
  276. return 0;
  277. err:
  278. while (--i >= 0) {
  279. engine = idxd->engines[i];
  280. conf_dev = engine_confdev(engine);
  281. put_device(conf_dev);
  282. kfree(engine);
  283. }
  284. kfree(idxd->engines);
  285. return rc;
  286. }
  287. static void idxd_clean_groups(struct idxd_device *idxd)
  288. {
  289. struct idxd_group *group;
  290. int i;
  291. for (i = 0; i < idxd->max_groups; i++) {
  292. group = idxd->groups[i];
  293. put_device(group_confdev(group));
  294. kfree(group);
  295. }
  296. kfree(idxd->groups);
  297. }
  298. static int idxd_setup_groups(struct idxd_device *idxd)
  299. {
  300. struct device *dev = &idxd->pdev->dev;
  301. struct device *conf_dev;
  302. struct idxd_group *group;
  303. int i, rc;
  304. idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
  305. GFP_KERNEL, dev_to_node(dev));
  306. if (!idxd->groups)
  307. return -ENOMEM;
  308. for (i = 0; i < idxd->max_groups; i++) {
  309. group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
  310. if (!group) {
  311. rc = -ENOMEM;
  312. goto err;
  313. }
  314. idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP);
  315. conf_dev = group_confdev(group);
  316. group->id = i;
  317. group->idxd = idxd;
  318. device_initialize(conf_dev);
  319. conf_dev->parent = idxd_confdev(idxd);
  320. conf_dev->bus = &dsa_bus_type;
  321. conf_dev->type = &idxd_group_device_type;
  322. rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
  323. if (rc < 0) {
  324. put_device(conf_dev);
  325. kfree(group);
  326. goto err;
  327. }
  328. idxd->groups[i] = group;
  329. if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) {
  330. group->tc_a = 1;
  331. group->tc_b = 1;
  332. } else {
  333. group->tc_a = -1;
  334. group->tc_b = -1;
  335. }
  336. /*
  337. * The default value is the same as the value of
  338. * total read buffers in GRPCAP.
  339. */
  340. group->rdbufs_allowed = idxd->max_rdbufs;
  341. }
  342. return 0;
  343. err:
  344. while (--i >= 0) {
  345. group = idxd->groups[i];
  346. put_device(group_confdev(group));
  347. kfree(group);
  348. }
  349. kfree(idxd->groups);
  350. return rc;
  351. }
  352. static void idxd_cleanup_internals(struct idxd_device *idxd)
  353. {
  354. idxd_clean_groups(idxd);
  355. idxd_clean_engines(idxd);
  356. idxd_clean_wqs(idxd);
  357. destroy_workqueue(idxd->wq);
  358. }
  359. static int idxd_init_evl(struct idxd_device *idxd)
  360. {
  361. struct device *dev = &idxd->pdev->dev;
  362. unsigned int evl_cache_size;
  363. struct idxd_evl *evl;
  364. const char *idxd_name;
  365. if (idxd->hw.gen_cap.evl_support == 0)
  366. return 0;
  367. evl = kzalloc_node(sizeof(*evl), GFP_KERNEL, dev_to_node(dev));
  368. if (!evl)
  369. return -ENOMEM;
  370. mutex_init(&evl->lock);
  371. evl->size = IDXD_EVL_SIZE_MIN;
  372. idxd_name = dev_name(idxd_confdev(idxd));
  373. evl_cache_size = sizeof(struct idxd_evl_fault) + evl_ent_size(idxd);
  374. /*
  375. * Since completion record in evl_cache will be copied to user
  376. * when handling completion record page fault, need to create
  377. * the cache suitable for user copy.
  378. */
  379. idxd->evl_cache = kmem_cache_create_usercopy(idxd_name, evl_cache_size,
  380. 0, 0, 0, evl_cache_size,
  381. NULL);
  382. if (!idxd->evl_cache) {
  383. kfree(evl);
  384. return -ENOMEM;
  385. }
  386. idxd->evl = evl;
  387. return 0;
  388. }
  389. static int idxd_setup_internals(struct idxd_device *idxd)
  390. {
  391. struct device *dev = &idxd->pdev->dev;
  392. int rc;
  393. init_waitqueue_head(&idxd->cmd_waitq);
  394. rc = idxd_setup_wqs(idxd);
  395. if (rc < 0)
  396. goto err_wqs;
  397. rc = idxd_setup_engines(idxd);
  398. if (rc < 0)
  399. goto err_engine;
  400. rc = idxd_setup_groups(idxd);
  401. if (rc < 0)
  402. goto err_group;
  403. idxd->wq = create_workqueue(dev_name(dev));
  404. if (!idxd->wq) {
  405. rc = -ENOMEM;
  406. goto err_wkq_create;
  407. }
  408. rc = idxd_init_evl(idxd);
  409. if (rc < 0)
  410. goto err_evl;
  411. return 0;
  412. err_evl:
  413. destroy_workqueue(idxd->wq);
  414. err_wkq_create:
  415. idxd_clean_groups(idxd);
  416. err_group:
  417. idxd_clean_engines(idxd);
  418. err_engine:
  419. idxd_clean_wqs(idxd);
  420. err_wqs:
  421. return rc;
  422. }
  423. static void idxd_read_table_offsets(struct idxd_device *idxd)
  424. {
  425. union offsets_reg offsets;
  426. struct device *dev = &idxd->pdev->dev;
  427. offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
  428. offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
  429. idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
  430. dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
  431. idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
  432. dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
  433. idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
  434. dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
  435. idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
  436. dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
  437. }
  438. void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count)
  439. {
  440. int i, j, nr;
  441. for (i = 0, nr = 0; i < count; i++) {
  442. for (j = 0; j < BITS_PER_LONG_LONG; j++) {
  443. if (val[i] & BIT(j))
  444. set_bit(nr, bmap);
  445. nr++;
  446. }
  447. }
  448. }
  449. static void idxd_read_caps(struct idxd_device *idxd)
  450. {
  451. struct device *dev = &idxd->pdev->dev;
  452. int i;
  453. /* reading generic capabilities */
  454. idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
  455. dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
  456. if (idxd->hw.gen_cap.cmd_cap) {
  457. idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
  458. dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
  459. }
  460. /* reading command capabilities */
  461. if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE))
  462. idxd->request_int_handles = true;
  463. idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
  464. dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
  465. idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift);
  466. dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
  467. if (idxd->hw.gen_cap.config_en)
  468. set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
  469. /* reading group capabilities */
  470. idxd->hw.group_cap.bits =
  471. ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
  472. dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
  473. idxd->max_groups = idxd->hw.group_cap.num_groups;
  474. dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
  475. idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs;
  476. dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs);
  477. idxd->nr_rdbufs = idxd->max_rdbufs;
  478. /* read engine capabilities */
  479. idxd->hw.engine_cap.bits =
  480. ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
  481. dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
  482. idxd->max_engines = idxd->hw.engine_cap.num_engines;
  483. dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
  484. /* read workqueue capabilities */
  485. idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
  486. dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
  487. idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
  488. dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
  489. idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
  490. dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
  491. idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
  492. dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
  493. /* reading operation capabilities */
  494. for (i = 0; i < 4; i++) {
  495. idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
  496. IDXD_OPCAP_OFFSET + i * sizeof(u64));
  497. dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
  498. }
  499. multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4);
  500. /* read iaa cap */
  501. if (idxd->data->type == IDXD_TYPE_IAX && idxd->hw.version >= DEVICE_VERSION_2)
  502. idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET);
  503. }
  504. static void idxd_free(struct idxd_device *idxd)
  505. {
  506. if (!idxd)
  507. return;
  508. put_device(idxd_confdev(idxd));
  509. bitmap_free(idxd->opcap_bmap);
  510. ida_free(&idxd_ida, idxd->id);
  511. kfree(idxd);
  512. }
  513. static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
  514. {
  515. struct device *dev = &pdev->dev;
  516. struct device *conf_dev;
  517. struct idxd_device *idxd;
  518. int rc;
  519. idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
  520. if (!idxd)
  521. return NULL;
  522. conf_dev = idxd_confdev(idxd);
  523. idxd->pdev = pdev;
  524. idxd->data = data;
  525. idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
  526. idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
  527. if (idxd->id < 0)
  528. goto err_ida;
  529. idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev));
  530. if (!idxd->opcap_bmap)
  531. goto err_opcap;
  532. device_initialize(conf_dev);
  533. conf_dev->parent = dev;
  534. conf_dev->bus = &dsa_bus_type;
  535. conf_dev->type = idxd->data->dev_type;
  536. rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
  537. if (rc < 0)
  538. goto err_name;
  539. spin_lock_init(&idxd->dev_lock);
  540. spin_lock_init(&idxd->cmd_lock);
  541. return idxd;
  542. err_name:
  543. put_device(conf_dev);
  544. bitmap_free(idxd->opcap_bmap);
  545. err_opcap:
  546. ida_free(&idxd_ida, idxd->id);
  547. err_ida:
  548. kfree(idxd);
  549. return NULL;
  550. }
  551. static int idxd_enable_system_pasid(struct idxd_device *idxd)
  552. {
  553. struct pci_dev *pdev = idxd->pdev;
  554. struct device *dev = &pdev->dev;
  555. struct iommu_domain *domain;
  556. ioasid_t pasid;
  557. int ret;
  558. /*
  559. * Attach a global PASID to the DMA domain so that we can use ENQCMDS
  560. * to submit work on buffers mapped by DMA API.
  561. */
  562. domain = iommu_get_domain_for_dev(dev);
  563. if (!domain)
  564. return -EPERM;
  565. pasid = iommu_alloc_global_pasid(dev);
  566. if (pasid == IOMMU_PASID_INVALID)
  567. return -ENOSPC;
  568. /*
  569. * DMA domain is owned by the driver, it should support all valid
  570. * types such as DMA-FQ, identity, etc.
  571. */
  572. ret = iommu_attach_device_pasid(domain, dev, pasid, NULL);
  573. if (ret) {
  574. dev_err(dev, "failed to attach device pasid %d, domain type %d",
  575. pasid, domain->type);
  576. iommu_free_global_pasid(pasid);
  577. return ret;
  578. }
  579. /* Since we set user privilege for kernel DMA, enable completion IRQ */
  580. idxd_set_user_intr(idxd, 1);
  581. idxd->pasid = pasid;
  582. return ret;
  583. }
  584. static void idxd_disable_system_pasid(struct idxd_device *idxd)
  585. {
  586. struct pci_dev *pdev = idxd->pdev;
  587. struct device *dev = &pdev->dev;
  588. struct iommu_domain *domain;
  589. domain = iommu_get_domain_for_dev(dev);
  590. if (!domain)
  591. return;
  592. iommu_detach_device_pasid(domain, dev, idxd->pasid);
  593. iommu_free_global_pasid(idxd->pasid);
  594. idxd_set_user_intr(idxd, 0);
  595. idxd->sva = NULL;
  596. idxd->pasid = IOMMU_PASID_INVALID;
  597. }
  598. static int idxd_enable_sva(struct pci_dev *pdev)
  599. {
  600. int ret;
  601. ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF);
  602. if (ret)
  603. return ret;
  604. ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
  605. if (ret)
  606. iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF);
  607. return ret;
  608. }
  609. static void idxd_disable_sva(struct pci_dev *pdev)
  610. {
  611. iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
  612. iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF);
  613. }
  614. static int idxd_probe(struct idxd_device *idxd)
  615. {
  616. struct pci_dev *pdev = idxd->pdev;
  617. struct device *dev = &pdev->dev;
  618. int rc;
  619. dev_dbg(dev, "%s entered and resetting device\n", __func__);
  620. rc = idxd_device_init_reset(idxd);
  621. if (rc < 0)
  622. return rc;
  623. dev_dbg(dev, "IDXD reset complete\n");
  624. if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
  625. if (idxd_enable_sva(pdev)) {
  626. dev_warn(dev, "Unable to turn on user SVA feature.\n");
  627. } else {
  628. set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);
  629. rc = idxd_enable_system_pasid(idxd);
  630. if (rc)
  631. dev_warn(dev, "No in-kernel DMA with PASID. %d\n", rc);
  632. else
  633. set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
  634. }
  635. } else if (!sva) {
  636. dev_warn(dev, "User forced SVA off via module param.\n");
  637. }
  638. idxd_read_caps(idxd);
  639. idxd_read_table_offsets(idxd);
  640. rc = idxd_setup_internals(idxd);
  641. if (rc)
  642. goto err;
  643. /* If the configs are readonly, then load them from device */
  644. if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
  645. dev_dbg(dev, "Loading RO device config\n");
  646. rc = idxd_device_load_config(idxd);
  647. if (rc < 0)
  648. goto err_config;
  649. }
  650. rc = idxd_setup_interrupts(idxd);
  651. if (rc)
  652. goto err_config;
  653. idxd->major = idxd_cdev_get_major(idxd);
  654. rc = perfmon_pmu_init(idxd);
  655. if (rc < 0)
  656. dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
  657. dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
  658. return 0;
  659. err_config:
  660. idxd_cleanup_internals(idxd);
  661. err:
  662. if (device_pasid_enabled(idxd))
  663. idxd_disable_system_pasid(idxd);
  664. if (device_user_pasid_enabled(idxd))
  665. idxd_disable_sva(pdev);
  666. return rc;
  667. }
  668. static void idxd_cleanup(struct idxd_device *idxd)
  669. {
  670. perfmon_pmu_remove(idxd);
  671. idxd_cleanup_interrupts(idxd);
  672. idxd_cleanup_internals(idxd);
  673. if (device_pasid_enabled(idxd))
  674. idxd_disable_system_pasid(idxd);
  675. if (device_user_pasid_enabled(idxd))
  676. idxd_disable_sva(idxd->pdev);
  677. }
  678. static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  679. {
  680. struct device *dev = &pdev->dev;
  681. struct idxd_device *idxd;
  682. struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
  683. int rc;
  684. rc = pci_enable_device(pdev);
  685. if (rc)
  686. return rc;
  687. dev_dbg(dev, "Alloc IDXD context\n");
  688. idxd = idxd_alloc(pdev, data);
  689. if (!idxd) {
  690. rc = -ENOMEM;
  691. goto err_idxd_alloc;
  692. }
  693. dev_dbg(dev, "Mapping BARs\n");
  694. idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
  695. if (!idxd->reg_base) {
  696. rc = -ENOMEM;
  697. goto err_iomap;
  698. }
  699. dev_dbg(dev, "Set DMA masks\n");
  700. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  701. if (rc)
  702. goto err;
  703. dev_dbg(dev, "Set PCI master\n");
  704. pci_set_master(pdev);
  705. pci_set_drvdata(pdev, idxd);
  706. idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
  707. rc = idxd_probe(idxd);
  708. if (rc) {
  709. dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
  710. goto err;
  711. }
  712. if (data->load_device_defaults) {
  713. rc = data->load_device_defaults(idxd);
  714. if (rc)
  715. dev_warn(dev, "IDXD loading device defaults failed\n");
  716. }
  717. rc = idxd_register_devices(idxd);
  718. if (rc) {
  719. dev_err(dev, "IDXD sysfs setup failed\n");
  720. goto err_dev_register;
  721. }
  722. rc = idxd_device_init_debugfs(idxd);
  723. if (rc)
  724. dev_warn(dev, "IDXD debugfs failed to setup\n");
  725. dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
  726. idxd->hw.version);
  727. idxd->user_submission_safe = data->user_submission_safe;
  728. return 0;
  729. err_dev_register:
  730. idxd_cleanup(idxd);
  731. err:
  732. pci_iounmap(pdev, idxd->reg_base);
  733. err_iomap:
  734. idxd_free(idxd);
  735. err_idxd_alloc:
  736. pci_disable_device(pdev);
  737. return rc;
  738. }
  739. void idxd_wqs_quiesce(struct idxd_device *idxd)
  740. {
  741. struct idxd_wq *wq;
  742. int i;
  743. for (i = 0; i < idxd->max_wqs; i++) {
  744. wq = idxd->wqs[i];
  745. if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
  746. idxd_wq_quiesce(wq);
  747. }
  748. }
  749. static void idxd_shutdown(struct pci_dev *pdev)
  750. {
  751. struct idxd_device *idxd = pci_get_drvdata(pdev);
  752. struct idxd_irq_entry *irq_entry;
  753. int rc;
  754. rc = idxd_device_disable(idxd);
  755. if (rc)
  756. dev_err(&pdev->dev, "Disabling device failed\n");
  757. irq_entry = &idxd->ie;
  758. synchronize_irq(irq_entry->vector);
  759. idxd_mask_error_interrupts(idxd);
  760. flush_workqueue(idxd->wq);
  761. }
  762. static void idxd_remove(struct pci_dev *pdev)
  763. {
  764. struct idxd_device *idxd = pci_get_drvdata(pdev);
  765. idxd_unregister_devices(idxd);
  766. /*
  767. * When ->release() is called for the idxd->conf_dev, it frees all the memory related
  768. * to the idxd context. The driver still needs those bits in order to do the rest of
  769. * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
  770. * on the device here to hold off the freeing while allowing the idxd sub-driver
  771. * to unbind.
  772. */
  773. get_device(idxd_confdev(idxd));
  774. device_unregister(idxd_confdev(idxd));
  775. idxd_shutdown(pdev);
  776. idxd_device_remove_debugfs(idxd);
  777. perfmon_pmu_remove(idxd);
  778. idxd_cleanup_interrupts(idxd);
  779. if (device_pasid_enabled(idxd))
  780. idxd_disable_system_pasid(idxd);
  781. if (device_user_pasid_enabled(idxd))
  782. idxd_disable_sva(idxd->pdev);
  783. pci_iounmap(pdev, idxd->reg_base);
  784. put_device(idxd_confdev(idxd));
  785. pci_disable_device(pdev);
  786. }
  787. static struct pci_driver idxd_pci_driver = {
  788. .name = DRV_NAME,
  789. .id_table = idxd_pci_tbl,
  790. .probe = idxd_pci_probe,
  791. .remove = idxd_remove,
  792. .shutdown = idxd_shutdown,
  793. };
  794. static int __init idxd_init_module(void)
  795. {
  796. int err;
  797. /*
  798. * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
  799. * enumerating the device. We can not utilize it.
  800. */
  801. if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
  802. pr_warn("idxd driver failed to load without MOVDIR64B.\n");
  803. return -ENODEV;
  804. }
  805. if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
  806. pr_warn("Platform does not have ENQCMD(S) support.\n");
  807. else
  808. support_enqcmd = true;
  809. err = idxd_driver_register(&idxd_drv);
  810. if (err < 0)
  811. goto err_idxd_driver_register;
  812. err = idxd_driver_register(&idxd_dmaengine_drv);
  813. if (err < 0)
  814. goto err_idxd_dmaengine_driver_register;
  815. err = idxd_driver_register(&idxd_user_drv);
  816. if (err < 0)
  817. goto err_idxd_user_driver_register;
  818. err = idxd_cdev_register();
  819. if (err)
  820. goto err_cdev_register;
  821. err = idxd_init_debugfs();
  822. if (err)
  823. goto err_debugfs;
  824. err = pci_register_driver(&idxd_pci_driver);
  825. if (err)
  826. goto err_pci_register;
  827. return 0;
  828. err_pci_register:
  829. idxd_remove_debugfs();
  830. err_debugfs:
  831. idxd_cdev_remove();
  832. err_cdev_register:
  833. idxd_driver_unregister(&idxd_user_drv);
  834. err_idxd_user_driver_register:
  835. idxd_driver_unregister(&idxd_dmaengine_drv);
  836. err_idxd_dmaengine_driver_register:
  837. idxd_driver_unregister(&idxd_drv);
  838. err_idxd_driver_register:
  839. return err;
  840. }
  841. module_init(idxd_init_module);
  842. static void __exit idxd_exit_module(void)
  843. {
  844. idxd_driver_unregister(&idxd_user_drv);
  845. idxd_driver_unregister(&idxd_dmaengine_drv);
  846. idxd_driver_unregister(&idxd_drv);
  847. pci_unregister_driver(&idxd_pci_driver);
  848. idxd_cdev_remove();
  849. idxd_remove_debugfs();
  850. }
  851. module_exit(idxd_exit_module);