device.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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/pci.h>
  7. #include <linux/io-64-nonatomic-lo-hi.h>
  8. #include <linux/dmaengine.h>
  9. #include <linux/irq.h>
  10. #include <linux/msi.h>
  11. #include <uapi/linux/idxd.h>
  12. #include "../dmaengine.h"
  13. #include "idxd.h"
  14. #include "registers.h"
  15. static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
  16. u32 *status);
  17. static void idxd_device_wqs_clear_state(struct idxd_device *idxd);
  18. static void idxd_wq_disable_cleanup(struct idxd_wq *wq);
  19. /* Interrupt control bits */
  20. void idxd_unmask_error_interrupts(struct idxd_device *idxd)
  21. {
  22. union genctrl_reg genctrl;
  23. genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
  24. genctrl.softerr_int_en = 1;
  25. genctrl.halt_int_en = 1;
  26. iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
  27. }
  28. void idxd_mask_error_interrupts(struct idxd_device *idxd)
  29. {
  30. union genctrl_reg genctrl;
  31. genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
  32. genctrl.softerr_int_en = 0;
  33. genctrl.halt_int_en = 0;
  34. iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
  35. }
  36. static void free_hw_descs(struct idxd_wq *wq)
  37. {
  38. int i;
  39. for (i = 0; i < wq->num_descs; i++)
  40. kfree(wq->hw_descs[i]);
  41. kfree(wq->hw_descs);
  42. }
  43. static int alloc_hw_descs(struct idxd_wq *wq, int num)
  44. {
  45. struct device *dev = &wq->idxd->pdev->dev;
  46. int i;
  47. int node = dev_to_node(dev);
  48. wq->hw_descs = kcalloc_node(num, sizeof(struct dsa_hw_desc *),
  49. GFP_KERNEL, node);
  50. if (!wq->hw_descs)
  51. return -ENOMEM;
  52. for (i = 0; i < num; i++) {
  53. wq->hw_descs[i] = kzalloc_node(sizeof(*wq->hw_descs[i]),
  54. GFP_KERNEL, node);
  55. if (!wq->hw_descs[i]) {
  56. free_hw_descs(wq);
  57. return -ENOMEM;
  58. }
  59. }
  60. return 0;
  61. }
  62. static void free_descs(struct idxd_wq *wq)
  63. {
  64. int i;
  65. for (i = 0; i < wq->num_descs; i++)
  66. kfree(wq->descs[i]);
  67. kfree(wq->descs);
  68. }
  69. static int alloc_descs(struct idxd_wq *wq, int num)
  70. {
  71. struct device *dev = &wq->idxd->pdev->dev;
  72. int i;
  73. int node = dev_to_node(dev);
  74. wq->descs = kcalloc_node(num, sizeof(struct idxd_desc *),
  75. GFP_KERNEL, node);
  76. if (!wq->descs)
  77. return -ENOMEM;
  78. for (i = 0; i < num; i++) {
  79. wq->descs[i] = kzalloc_node(sizeof(*wq->descs[i]),
  80. GFP_KERNEL, node);
  81. if (!wq->descs[i]) {
  82. free_descs(wq);
  83. return -ENOMEM;
  84. }
  85. }
  86. return 0;
  87. }
  88. /* WQ control bits */
  89. int idxd_wq_alloc_resources(struct idxd_wq *wq)
  90. {
  91. struct idxd_device *idxd = wq->idxd;
  92. struct device *dev = &idxd->pdev->dev;
  93. int rc, num_descs, i;
  94. if (wq->type != IDXD_WQT_KERNEL)
  95. return 0;
  96. num_descs = wq_dedicated(wq) ? wq->size : wq->threshold;
  97. wq->num_descs = num_descs;
  98. rc = alloc_hw_descs(wq, num_descs);
  99. if (rc < 0)
  100. return rc;
  101. wq->compls_size = num_descs * idxd->data->compl_size;
  102. wq->compls = dma_alloc_coherent(dev, wq->compls_size, &wq->compls_addr, GFP_KERNEL);
  103. if (!wq->compls) {
  104. rc = -ENOMEM;
  105. goto fail_alloc_compls;
  106. }
  107. rc = alloc_descs(wq, num_descs);
  108. if (rc < 0)
  109. goto fail_alloc_descs;
  110. rc = sbitmap_queue_init_node(&wq->sbq, num_descs, -1, false, GFP_KERNEL,
  111. dev_to_node(dev));
  112. if (rc < 0)
  113. goto fail_sbitmap_init;
  114. for (i = 0; i < num_descs; i++) {
  115. struct idxd_desc *desc = wq->descs[i];
  116. desc->hw = wq->hw_descs[i];
  117. if (idxd->data->type == IDXD_TYPE_DSA)
  118. desc->completion = &wq->compls[i];
  119. else if (idxd->data->type == IDXD_TYPE_IAX)
  120. desc->iax_completion = &wq->iax_compls[i];
  121. desc->compl_dma = wq->compls_addr + idxd->data->compl_size * i;
  122. desc->id = i;
  123. desc->wq = wq;
  124. desc->cpu = -1;
  125. }
  126. return 0;
  127. fail_sbitmap_init:
  128. free_descs(wq);
  129. fail_alloc_descs:
  130. dma_free_coherent(dev, wq->compls_size, wq->compls, wq->compls_addr);
  131. fail_alloc_compls:
  132. free_hw_descs(wq);
  133. return rc;
  134. }
  135. void idxd_wq_free_resources(struct idxd_wq *wq)
  136. {
  137. struct device *dev = &wq->idxd->pdev->dev;
  138. if (wq->type != IDXD_WQT_KERNEL)
  139. return;
  140. free_hw_descs(wq);
  141. free_descs(wq);
  142. dma_free_coherent(dev, wq->compls_size, wq->compls, wq->compls_addr);
  143. sbitmap_queue_free(&wq->sbq);
  144. }
  145. int idxd_wq_enable(struct idxd_wq *wq)
  146. {
  147. struct idxd_device *idxd = wq->idxd;
  148. struct device *dev = &idxd->pdev->dev;
  149. u32 status;
  150. if (wq->state == IDXD_WQ_ENABLED) {
  151. dev_dbg(dev, "WQ %d already enabled\n", wq->id);
  152. return 0;
  153. }
  154. idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_WQ, wq->id, &status);
  155. if (status != IDXD_CMDSTS_SUCCESS &&
  156. status != IDXD_CMDSTS_ERR_WQ_ENABLED) {
  157. dev_dbg(dev, "WQ enable failed: %#x\n", status);
  158. return -ENXIO;
  159. }
  160. wq->state = IDXD_WQ_ENABLED;
  161. set_bit(wq->id, idxd->wq_enable_map);
  162. dev_dbg(dev, "WQ %d enabled\n", wq->id);
  163. return 0;
  164. }
  165. int idxd_wq_disable(struct idxd_wq *wq, bool reset_config)
  166. {
  167. struct idxd_device *idxd = wq->idxd;
  168. struct device *dev = &idxd->pdev->dev;
  169. u32 status, operand;
  170. dev_dbg(dev, "Disabling WQ %d\n", wq->id);
  171. if (wq->state != IDXD_WQ_ENABLED) {
  172. dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
  173. return 0;
  174. }
  175. operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
  176. idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_WQ, operand, &status);
  177. if (status != IDXD_CMDSTS_SUCCESS) {
  178. dev_dbg(dev, "WQ disable failed: %#x\n", status);
  179. return -ENXIO;
  180. }
  181. if (reset_config)
  182. idxd_wq_disable_cleanup(wq);
  183. clear_bit(wq->id, idxd->wq_enable_map);
  184. wq->state = IDXD_WQ_DISABLED;
  185. dev_dbg(dev, "WQ %d disabled\n", wq->id);
  186. return 0;
  187. }
  188. void idxd_wq_drain(struct idxd_wq *wq)
  189. {
  190. struct idxd_device *idxd = wq->idxd;
  191. struct device *dev = &idxd->pdev->dev;
  192. u32 operand;
  193. if (wq->state != IDXD_WQ_ENABLED) {
  194. dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
  195. return;
  196. }
  197. dev_dbg(dev, "Draining WQ %d\n", wq->id);
  198. operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
  199. idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_WQ, operand, NULL);
  200. }
  201. void idxd_wq_reset(struct idxd_wq *wq)
  202. {
  203. struct idxd_device *idxd = wq->idxd;
  204. struct device *dev = &idxd->pdev->dev;
  205. u32 operand;
  206. if (wq->state != IDXD_WQ_ENABLED) {
  207. dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
  208. return;
  209. }
  210. operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
  211. idxd_cmd_exec(idxd, IDXD_CMD_RESET_WQ, operand, NULL);
  212. idxd_wq_disable_cleanup(wq);
  213. }
  214. int idxd_wq_map_portal(struct idxd_wq *wq)
  215. {
  216. struct idxd_device *idxd = wq->idxd;
  217. struct pci_dev *pdev = idxd->pdev;
  218. struct device *dev = &pdev->dev;
  219. resource_size_t start;
  220. start = pci_resource_start(pdev, IDXD_WQ_BAR);
  221. start += idxd_get_wq_portal_full_offset(wq->id, IDXD_PORTAL_LIMITED);
  222. wq->portal = devm_ioremap(dev, start, IDXD_PORTAL_SIZE);
  223. if (!wq->portal)
  224. return -ENOMEM;
  225. return 0;
  226. }
  227. void idxd_wq_unmap_portal(struct idxd_wq *wq)
  228. {
  229. struct device *dev = &wq->idxd->pdev->dev;
  230. devm_iounmap(dev, wq->portal);
  231. wq->portal = NULL;
  232. wq->portal_offset = 0;
  233. }
  234. void idxd_wqs_unmap_portal(struct idxd_device *idxd)
  235. {
  236. int i;
  237. for (i = 0; i < idxd->max_wqs; i++) {
  238. struct idxd_wq *wq = idxd->wqs[i];
  239. if (wq->portal)
  240. idxd_wq_unmap_portal(wq);
  241. }
  242. }
  243. static void __idxd_wq_set_priv_locked(struct idxd_wq *wq, int priv)
  244. {
  245. struct idxd_device *idxd = wq->idxd;
  246. union wqcfg wqcfg;
  247. unsigned int offset;
  248. offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PRIVL_IDX);
  249. spin_lock(&idxd->dev_lock);
  250. wqcfg.bits[WQCFG_PRIVL_IDX] = ioread32(idxd->reg_base + offset);
  251. wqcfg.priv = priv;
  252. wq->wqcfg->bits[WQCFG_PRIVL_IDX] = wqcfg.bits[WQCFG_PRIVL_IDX];
  253. iowrite32(wqcfg.bits[WQCFG_PRIVL_IDX], idxd->reg_base + offset);
  254. spin_unlock(&idxd->dev_lock);
  255. }
  256. static void __idxd_wq_set_pasid_locked(struct idxd_wq *wq, int pasid)
  257. {
  258. struct idxd_device *idxd = wq->idxd;
  259. union wqcfg wqcfg;
  260. unsigned int offset;
  261. offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
  262. spin_lock(&idxd->dev_lock);
  263. wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
  264. wqcfg.pasid_en = 1;
  265. wqcfg.pasid = pasid;
  266. wq->wqcfg->bits[WQCFG_PASID_IDX] = wqcfg.bits[WQCFG_PASID_IDX];
  267. iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
  268. spin_unlock(&idxd->dev_lock);
  269. }
  270. int idxd_wq_set_pasid(struct idxd_wq *wq, int pasid)
  271. {
  272. int rc;
  273. rc = idxd_wq_disable(wq, false);
  274. if (rc < 0)
  275. return rc;
  276. __idxd_wq_set_pasid_locked(wq, pasid);
  277. rc = idxd_wq_enable(wq);
  278. if (rc < 0)
  279. return rc;
  280. return 0;
  281. }
  282. int idxd_wq_disable_pasid(struct idxd_wq *wq)
  283. {
  284. struct idxd_device *idxd = wq->idxd;
  285. int rc;
  286. union wqcfg wqcfg;
  287. unsigned int offset;
  288. rc = idxd_wq_disable(wq, false);
  289. if (rc < 0)
  290. return rc;
  291. offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
  292. spin_lock(&idxd->dev_lock);
  293. wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
  294. wqcfg.pasid_en = 0;
  295. wqcfg.pasid = 0;
  296. iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
  297. spin_unlock(&idxd->dev_lock);
  298. rc = idxd_wq_enable(wq);
  299. if (rc < 0)
  300. return rc;
  301. return 0;
  302. }
  303. static void idxd_wq_disable_cleanup(struct idxd_wq *wq)
  304. {
  305. struct idxd_device *idxd = wq->idxd;
  306. lockdep_assert_held(&wq->wq_lock);
  307. wq->state = IDXD_WQ_DISABLED;
  308. memset(wq->wqcfg, 0, idxd->wqcfg_size);
  309. wq->type = IDXD_WQT_NONE;
  310. wq->threshold = 0;
  311. wq->priority = 0;
  312. wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
  313. clear_bit(WQ_FLAG_DEDICATED, &wq->flags);
  314. clear_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
  315. clear_bit(WQ_FLAG_ATS_DISABLE, &wq->flags);
  316. memset(wq->name, 0, WQ_NAME_SIZE);
  317. wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER;
  318. idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH);
  319. if (wq->opcap_bmap)
  320. bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
  321. }
  322. static void idxd_wq_device_reset_cleanup(struct idxd_wq *wq)
  323. {
  324. lockdep_assert_held(&wq->wq_lock);
  325. wq->size = 0;
  326. wq->group = NULL;
  327. }
  328. static void idxd_wq_ref_release(struct percpu_ref *ref)
  329. {
  330. struct idxd_wq *wq = container_of(ref, struct idxd_wq, wq_active);
  331. complete(&wq->wq_dead);
  332. }
  333. int idxd_wq_init_percpu_ref(struct idxd_wq *wq)
  334. {
  335. int rc;
  336. memset(&wq->wq_active, 0, sizeof(wq->wq_active));
  337. rc = percpu_ref_init(&wq->wq_active, idxd_wq_ref_release,
  338. PERCPU_REF_ALLOW_REINIT, GFP_KERNEL);
  339. if (rc < 0)
  340. return rc;
  341. reinit_completion(&wq->wq_dead);
  342. reinit_completion(&wq->wq_resurrect);
  343. return 0;
  344. }
  345. void __idxd_wq_quiesce(struct idxd_wq *wq)
  346. {
  347. lockdep_assert_held(&wq->wq_lock);
  348. reinit_completion(&wq->wq_resurrect);
  349. percpu_ref_kill(&wq->wq_active);
  350. complete_all(&wq->wq_resurrect);
  351. wait_for_completion(&wq->wq_dead);
  352. }
  353. void idxd_wq_quiesce(struct idxd_wq *wq)
  354. {
  355. mutex_lock(&wq->wq_lock);
  356. __idxd_wq_quiesce(wq);
  357. mutex_unlock(&wq->wq_lock);
  358. }
  359. /* Device control bits */
  360. static inline bool idxd_is_enabled(struct idxd_device *idxd)
  361. {
  362. union gensts_reg gensts;
  363. gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
  364. if (gensts.state == IDXD_DEVICE_STATE_ENABLED)
  365. return true;
  366. return false;
  367. }
  368. static inline bool idxd_device_is_halted(struct idxd_device *idxd)
  369. {
  370. union gensts_reg gensts;
  371. gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
  372. return (gensts.state == IDXD_DEVICE_STATE_HALT);
  373. }
  374. /*
  375. * This is function is only used for reset during probe and will
  376. * poll for completion. Once the device is setup with interrupts,
  377. * all commands will be done via interrupt completion.
  378. */
  379. int idxd_device_init_reset(struct idxd_device *idxd)
  380. {
  381. struct device *dev = &idxd->pdev->dev;
  382. union idxd_command_reg cmd;
  383. if (idxd_device_is_halted(idxd)) {
  384. dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
  385. return -ENXIO;
  386. }
  387. memset(&cmd, 0, sizeof(cmd));
  388. cmd.cmd = IDXD_CMD_RESET_DEVICE;
  389. dev_dbg(dev, "%s: sending reset for init.\n", __func__);
  390. spin_lock(&idxd->cmd_lock);
  391. iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
  392. while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) &
  393. IDXD_CMDSTS_ACTIVE)
  394. cpu_relax();
  395. spin_unlock(&idxd->cmd_lock);
  396. return 0;
  397. }
  398. static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
  399. u32 *status)
  400. {
  401. union idxd_command_reg cmd;
  402. DECLARE_COMPLETION_ONSTACK(done);
  403. u32 stat;
  404. unsigned long flags;
  405. if (idxd_device_is_halted(idxd)) {
  406. dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
  407. if (status)
  408. *status = IDXD_CMDSTS_HW_ERR;
  409. return;
  410. }
  411. memset(&cmd, 0, sizeof(cmd));
  412. cmd.cmd = cmd_code;
  413. cmd.operand = operand;
  414. cmd.int_req = 1;
  415. spin_lock_irqsave(&idxd->cmd_lock, flags);
  416. wait_event_lock_irq(idxd->cmd_waitq,
  417. !test_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags),
  418. idxd->cmd_lock);
  419. dev_dbg(&idxd->pdev->dev, "%s: sending cmd: %#x op: %#x\n",
  420. __func__, cmd_code, operand);
  421. idxd->cmd_status = 0;
  422. __set_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
  423. idxd->cmd_done = &done;
  424. iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
  425. /*
  426. * After command submitted, release lock and go to sleep until
  427. * the command completes via interrupt.
  428. */
  429. spin_unlock_irqrestore(&idxd->cmd_lock, flags);
  430. wait_for_completion(&done);
  431. stat = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
  432. spin_lock(&idxd->cmd_lock);
  433. if (status)
  434. *status = stat;
  435. idxd->cmd_status = stat & GENMASK(7, 0);
  436. __clear_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
  437. /* Wake up other pending commands */
  438. wake_up(&idxd->cmd_waitq);
  439. spin_unlock(&idxd->cmd_lock);
  440. }
  441. int idxd_device_enable(struct idxd_device *idxd)
  442. {
  443. struct device *dev = &idxd->pdev->dev;
  444. u32 status;
  445. if (idxd_is_enabled(idxd)) {
  446. dev_dbg(dev, "Device already enabled\n");
  447. return -ENXIO;
  448. }
  449. idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_DEVICE, 0, &status);
  450. /* If the command is successful or if the device was enabled */
  451. if (status != IDXD_CMDSTS_SUCCESS &&
  452. status != IDXD_CMDSTS_ERR_DEV_ENABLED) {
  453. dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
  454. return -ENXIO;
  455. }
  456. idxd->state = IDXD_DEV_ENABLED;
  457. return 0;
  458. }
  459. int idxd_device_disable(struct idxd_device *idxd)
  460. {
  461. struct device *dev = &idxd->pdev->dev;
  462. u32 status;
  463. if (!idxd_is_enabled(idxd)) {
  464. dev_dbg(dev, "Device is not enabled\n");
  465. return 0;
  466. }
  467. idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_DEVICE, 0, &status);
  468. /* If the command is successful or if the device was disabled */
  469. if (status != IDXD_CMDSTS_SUCCESS &&
  470. !(status & IDXD_CMDSTS_ERR_DIS_DEV_EN)) {
  471. dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
  472. return -ENXIO;
  473. }
  474. idxd_device_clear_state(idxd);
  475. return 0;
  476. }
  477. void idxd_device_reset(struct idxd_device *idxd)
  478. {
  479. idxd_cmd_exec(idxd, IDXD_CMD_RESET_DEVICE, 0, NULL);
  480. idxd_device_clear_state(idxd);
  481. spin_lock(&idxd->dev_lock);
  482. idxd_unmask_error_interrupts(idxd);
  483. spin_unlock(&idxd->dev_lock);
  484. }
  485. void idxd_device_drain_pasid(struct idxd_device *idxd, int pasid)
  486. {
  487. struct device *dev = &idxd->pdev->dev;
  488. u32 operand;
  489. operand = pasid;
  490. dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_DRAIN_PASID, operand);
  491. idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_PASID, operand, NULL);
  492. dev_dbg(dev, "pasid %d drained\n", pasid);
  493. }
  494. int idxd_device_request_int_handle(struct idxd_device *idxd, int idx, int *handle,
  495. enum idxd_interrupt_type irq_type)
  496. {
  497. struct device *dev = &idxd->pdev->dev;
  498. u32 operand, status;
  499. if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)))
  500. return -EOPNOTSUPP;
  501. dev_dbg(dev, "get int handle, idx %d\n", idx);
  502. operand = idx & GENMASK(15, 0);
  503. if (irq_type == IDXD_IRQ_IMS)
  504. operand |= CMD_INT_HANDLE_IMS;
  505. dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_REQUEST_INT_HANDLE, operand);
  506. idxd_cmd_exec(idxd, IDXD_CMD_REQUEST_INT_HANDLE, operand, &status);
  507. if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
  508. dev_dbg(dev, "request int handle failed: %#x\n", status);
  509. return -ENXIO;
  510. }
  511. *handle = (status >> IDXD_CMDSTS_RES_SHIFT) & GENMASK(15, 0);
  512. dev_dbg(dev, "int handle acquired: %u\n", *handle);
  513. return 0;
  514. }
  515. int idxd_device_release_int_handle(struct idxd_device *idxd, int handle,
  516. enum idxd_interrupt_type irq_type)
  517. {
  518. struct device *dev = &idxd->pdev->dev;
  519. u32 operand, status;
  520. union idxd_command_reg cmd;
  521. if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)))
  522. return -EOPNOTSUPP;
  523. dev_dbg(dev, "release int handle, handle %d\n", handle);
  524. memset(&cmd, 0, sizeof(cmd));
  525. operand = handle & GENMASK(15, 0);
  526. if (irq_type == IDXD_IRQ_IMS)
  527. operand |= CMD_INT_HANDLE_IMS;
  528. cmd.cmd = IDXD_CMD_RELEASE_INT_HANDLE;
  529. cmd.operand = operand;
  530. dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_RELEASE_INT_HANDLE, operand);
  531. spin_lock(&idxd->cmd_lock);
  532. iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
  533. while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) & IDXD_CMDSTS_ACTIVE)
  534. cpu_relax();
  535. status = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
  536. spin_unlock(&idxd->cmd_lock);
  537. if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
  538. dev_dbg(dev, "release int handle failed: %#x\n", status);
  539. return -ENXIO;
  540. }
  541. dev_dbg(dev, "int handle released.\n");
  542. return 0;
  543. }
  544. /* Device configuration bits */
  545. static void idxd_engines_clear_state(struct idxd_device *idxd)
  546. {
  547. struct idxd_engine *engine;
  548. int i;
  549. lockdep_assert_held(&idxd->dev_lock);
  550. for (i = 0; i < idxd->max_engines; i++) {
  551. engine = idxd->engines[i];
  552. engine->group = NULL;
  553. }
  554. }
  555. static void idxd_groups_clear_state(struct idxd_device *idxd)
  556. {
  557. struct idxd_group *group;
  558. int i;
  559. lockdep_assert_held(&idxd->dev_lock);
  560. for (i = 0; i < idxd->max_groups; i++) {
  561. group = idxd->groups[i];
  562. memset(&group->grpcfg, 0, sizeof(group->grpcfg));
  563. group->num_engines = 0;
  564. group->num_wqs = 0;
  565. group->use_rdbuf_limit = false;
  566. group->rdbufs_allowed = 0;
  567. group->rdbufs_reserved = 0;
  568. if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) {
  569. group->tc_a = 1;
  570. group->tc_b = 1;
  571. } else {
  572. group->tc_a = -1;
  573. group->tc_b = -1;
  574. }
  575. group->desc_progress_limit = 0;
  576. group->batch_progress_limit = 0;
  577. }
  578. }
  579. static void idxd_device_wqs_clear_state(struct idxd_device *idxd)
  580. {
  581. int i;
  582. for (i = 0; i < idxd->max_wqs; i++) {
  583. struct idxd_wq *wq = idxd->wqs[i];
  584. mutex_lock(&wq->wq_lock);
  585. idxd_wq_disable_cleanup(wq);
  586. idxd_wq_device_reset_cleanup(wq);
  587. mutex_unlock(&wq->wq_lock);
  588. }
  589. }
  590. void idxd_device_clear_state(struct idxd_device *idxd)
  591. {
  592. /* IDXD is always disabled. Other states are cleared only when IDXD is configurable. */
  593. if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
  594. /*
  595. * Clearing wq state is protected by wq lock.
  596. * So no need to be protected by device lock.
  597. */
  598. idxd_device_wqs_clear_state(idxd);
  599. spin_lock(&idxd->dev_lock);
  600. idxd_groups_clear_state(idxd);
  601. idxd_engines_clear_state(idxd);
  602. } else {
  603. spin_lock(&idxd->dev_lock);
  604. }
  605. idxd->state = IDXD_DEV_DISABLED;
  606. spin_unlock(&idxd->dev_lock);
  607. }
  608. static void idxd_group_config_write(struct idxd_group *group)
  609. {
  610. struct idxd_device *idxd = group->idxd;
  611. struct device *dev = &idxd->pdev->dev;
  612. int i;
  613. u32 grpcfg_offset;
  614. dev_dbg(dev, "Writing group %d cfg registers\n", group->id);
  615. /* setup GRPWQCFG */
  616. for (i = 0; i < GRPWQCFG_STRIDES; i++) {
  617. grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
  618. iowrite64(group->grpcfg.wqs[i], idxd->reg_base + grpcfg_offset);
  619. dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
  620. group->id, i, grpcfg_offset,
  621. ioread64(idxd->reg_base + grpcfg_offset));
  622. }
  623. /* setup GRPENGCFG */
  624. grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
  625. iowrite64(group->grpcfg.engines, idxd->reg_base + grpcfg_offset);
  626. dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
  627. grpcfg_offset, ioread64(idxd->reg_base + grpcfg_offset));
  628. /* setup GRPFLAGS */
  629. grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
  630. iowrite64(group->grpcfg.flags.bits, idxd->reg_base + grpcfg_offset);
  631. dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#llx\n",
  632. group->id, grpcfg_offset,
  633. ioread64(idxd->reg_base + grpcfg_offset));
  634. }
  635. static int idxd_groups_config_write(struct idxd_device *idxd)
  636. {
  637. union gencfg_reg reg;
  638. int i;
  639. struct device *dev = &idxd->pdev->dev;
  640. /* Setup bandwidth rdbuf limit */
  641. if (idxd->hw.gen_cap.config_en && idxd->rdbuf_limit) {
  642. reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
  643. reg.rdbuf_limit = idxd->rdbuf_limit;
  644. iowrite32(reg.bits, idxd->reg_base + IDXD_GENCFG_OFFSET);
  645. }
  646. dev_dbg(dev, "GENCFG(%#x): %#x\n", IDXD_GENCFG_OFFSET,
  647. ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET));
  648. for (i = 0; i < idxd->max_groups; i++) {
  649. struct idxd_group *group = idxd->groups[i];
  650. idxd_group_config_write(group);
  651. }
  652. return 0;
  653. }
  654. static bool idxd_device_pasid_priv_enabled(struct idxd_device *idxd)
  655. {
  656. struct pci_dev *pdev = idxd->pdev;
  657. if (pdev->pasid_enabled && (pdev->pasid_features & PCI_PASID_CAP_PRIV))
  658. return true;
  659. return false;
  660. }
  661. static int idxd_wq_config_write(struct idxd_wq *wq)
  662. {
  663. struct idxd_device *idxd = wq->idxd;
  664. struct device *dev = &idxd->pdev->dev;
  665. u32 wq_offset;
  666. int i, n;
  667. if (!wq->group)
  668. return 0;
  669. /*
  670. * Instead of memset the entire shadow copy of WQCFG, copy from the hardware after
  671. * wq reset. This will copy back the sticky values that are present on some devices.
  672. */
  673. for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
  674. wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
  675. wq->wqcfg->bits[i] |= ioread32(idxd->reg_base + wq_offset);
  676. }
  677. if (wq->size == 0 && wq->type != IDXD_WQT_NONE)
  678. wq->size = WQ_DEFAULT_QUEUE_DEPTH;
  679. /* byte 0-3 */
  680. wq->wqcfg->wq_size = wq->size;
  681. /* bytes 4-7 */
  682. wq->wqcfg->wq_thresh = wq->threshold;
  683. /* byte 8-11 */
  684. if (wq_dedicated(wq))
  685. wq->wqcfg->mode = 1;
  686. /*
  687. * The WQ priv bit is set depending on the WQ type. priv = 1 if the
  688. * WQ type is kernel to indicate privileged access. This setting only
  689. * matters for dedicated WQ. According to the DSA spec:
  690. * If the WQ is in dedicated mode, WQ PASID Enable is 1, and the
  691. * Privileged Mode Enable field of the PCI Express PASID capability
  692. * is 0, this field must be 0.
  693. *
  694. * In the case of a dedicated kernel WQ that is not able to support
  695. * the PASID cap, then the configuration will be rejected.
  696. */
  697. if (wq_dedicated(wq) && wq->wqcfg->pasid_en &&
  698. !idxd_device_pasid_priv_enabled(idxd) &&
  699. wq->type == IDXD_WQT_KERNEL) {
  700. idxd->cmd_status = IDXD_SCMD_WQ_NO_PRIV;
  701. return -EOPNOTSUPP;
  702. }
  703. wq->wqcfg->priority = wq->priority;
  704. if (idxd->hw.gen_cap.block_on_fault &&
  705. test_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags))
  706. wq->wqcfg->bof = 1;
  707. if (idxd->hw.wq_cap.wq_ats_support)
  708. wq->wqcfg->wq_ats_disable = test_bit(WQ_FLAG_ATS_DISABLE, &wq->flags);
  709. /* bytes 12-15 */
  710. wq->wqcfg->max_xfer_shift = ilog2(wq->max_xfer_bytes);
  711. idxd_wqcfg_set_max_batch_shift(idxd->data->type, wq->wqcfg, ilog2(wq->max_batch_size));
  712. /* bytes 32-63 */
  713. if (idxd->hw.wq_cap.op_config && wq->opcap_bmap) {
  714. memset(wq->wqcfg->op_config, 0, IDXD_MAX_OPCAP_BITS / 8);
  715. for_each_set_bit(n, wq->opcap_bmap, IDXD_MAX_OPCAP_BITS) {
  716. int pos = n % BITS_PER_LONG_LONG;
  717. int idx = n / BITS_PER_LONG_LONG;
  718. wq->wqcfg->op_config[idx] |= BIT(pos);
  719. }
  720. }
  721. dev_dbg(dev, "WQ %d CFGs\n", wq->id);
  722. for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
  723. wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
  724. iowrite32(wq->wqcfg->bits[i], idxd->reg_base + wq_offset);
  725. dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n",
  726. wq->id, i, wq_offset,
  727. ioread32(idxd->reg_base + wq_offset));
  728. }
  729. return 0;
  730. }
  731. static int idxd_wqs_config_write(struct idxd_device *idxd)
  732. {
  733. int i, rc;
  734. for (i = 0; i < idxd->max_wqs; i++) {
  735. struct idxd_wq *wq = idxd->wqs[i];
  736. rc = idxd_wq_config_write(wq);
  737. if (rc < 0)
  738. return rc;
  739. }
  740. return 0;
  741. }
  742. static void idxd_group_flags_setup(struct idxd_device *idxd)
  743. {
  744. int i;
  745. /* TC-A 0 and TC-B 1 should be defaults */
  746. for (i = 0; i < idxd->max_groups; i++) {
  747. struct idxd_group *group = idxd->groups[i];
  748. if (group->tc_a == -1)
  749. group->tc_a = group->grpcfg.flags.tc_a = 0;
  750. else
  751. group->grpcfg.flags.tc_a = group->tc_a;
  752. if (group->tc_b == -1)
  753. group->tc_b = group->grpcfg.flags.tc_b = 1;
  754. else
  755. group->grpcfg.flags.tc_b = group->tc_b;
  756. group->grpcfg.flags.use_rdbuf_limit = group->use_rdbuf_limit;
  757. group->grpcfg.flags.rdbufs_reserved = group->rdbufs_reserved;
  758. if (group->rdbufs_allowed)
  759. group->grpcfg.flags.rdbufs_allowed = group->rdbufs_allowed;
  760. else
  761. group->grpcfg.flags.rdbufs_allowed = idxd->max_rdbufs;
  762. group->grpcfg.flags.desc_progress_limit = group->desc_progress_limit;
  763. group->grpcfg.flags.batch_progress_limit = group->batch_progress_limit;
  764. }
  765. }
  766. static int idxd_engines_setup(struct idxd_device *idxd)
  767. {
  768. int i, engines = 0;
  769. struct idxd_engine *eng;
  770. struct idxd_group *group;
  771. for (i = 0; i < idxd->max_groups; i++) {
  772. group = idxd->groups[i];
  773. group->grpcfg.engines = 0;
  774. }
  775. for (i = 0; i < idxd->max_engines; i++) {
  776. eng = idxd->engines[i];
  777. group = eng->group;
  778. if (!group)
  779. continue;
  780. group->grpcfg.engines |= BIT(eng->id);
  781. engines++;
  782. }
  783. if (!engines)
  784. return -EINVAL;
  785. return 0;
  786. }
  787. static int idxd_wqs_setup(struct idxd_device *idxd)
  788. {
  789. struct idxd_wq *wq;
  790. struct idxd_group *group;
  791. int i, j, configured = 0;
  792. struct device *dev = &idxd->pdev->dev;
  793. for (i = 0; i < idxd->max_groups; i++) {
  794. group = idxd->groups[i];
  795. for (j = 0; j < 4; j++)
  796. group->grpcfg.wqs[j] = 0;
  797. }
  798. for (i = 0; i < idxd->max_wqs; i++) {
  799. wq = idxd->wqs[i];
  800. group = wq->group;
  801. if (!wq->group)
  802. continue;
  803. if (wq_shared(wq) && !wq_shared_supported(wq)) {
  804. idxd->cmd_status = IDXD_SCMD_WQ_NO_SWQ_SUPPORT;
  805. dev_warn(dev, "No shared wq support but configured.\n");
  806. return -EINVAL;
  807. }
  808. group->grpcfg.wqs[wq->id / 64] |= BIT(wq->id % 64);
  809. configured++;
  810. }
  811. if (configured == 0) {
  812. idxd->cmd_status = IDXD_SCMD_WQ_NONE_CONFIGURED;
  813. return -EINVAL;
  814. }
  815. return 0;
  816. }
  817. int idxd_device_config(struct idxd_device *idxd)
  818. {
  819. int rc;
  820. lockdep_assert_held(&idxd->dev_lock);
  821. rc = idxd_wqs_setup(idxd);
  822. if (rc < 0)
  823. return rc;
  824. rc = idxd_engines_setup(idxd);
  825. if (rc < 0)
  826. return rc;
  827. idxd_group_flags_setup(idxd);
  828. rc = idxd_wqs_config_write(idxd);
  829. if (rc < 0)
  830. return rc;
  831. rc = idxd_groups_config_write(idxd);
  832. if (rc < 0)
  833. return rc;
  834. return 0;
  835. }
  836. static int idxd_wq_load_config(struct idxd_wq *wq)
  837. {
  838. struct idxd_device *idxd = wq->idxd;
  839. struct device *dev = &idxd->pdev->dev;
  840. int wqcfg_offset;
  841. int i;
  842. wqcfg_offset = WQCFG_OFFSET(idxd, wq->id, 0);
  843. memcpy_fromio(wq->wqcfg, idxd->reg_base + wqcfg_offset, idxd->wqcfg_size);
  844. wq->size = wq->wqcfg->wq_size;
  845. wq->threshold = wq->wqcfg->wq_thresh;
  846. /* The driver does not support shared WQ mode in read-only config yet */
  847. if (wq->wqcfg->mode == 0 || wq->wqcfg->pasid_en)
  848. return -EOPNOTSUPP;
  849. set_bit(WQ_FLAG_DEDICATED, &wq->flags);
  850. wq->priority = wq->wqcfg->priority;
  851. wq->max_xfer_bytes = 1ULL << wq->wqcfg->max_xfer_shift;
  852. idxd_wq_set_max_batch_size(idxd->data->type, wq, 1U << wq->wqcfg->max_batch_shift);
  853. for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
  854. wqcfg_offset = WQCFG_OFFSET(idxd, wq->id, i);
  855. dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n", wq->id, i, wqcfg_offset, wq->wqcfg->bits[i]);
  856. }
  857. return 0;
  858. }
  859. static void idxd_group_load_config(struct idxd_group *group)
  860. {
  861. struct idxd_device *idxd = group->idxd;
  862. struct device *dev = &idxd->pdev->dev;
  863. int i, j, grpcfg_offset;
  864. /*
  865. * Load WQS bit fields
  866. * Iterate through all 256 bits 64 bits at a time
  867. */
  868. for (i = 0; i < GRPWQCFG_STRIDES; i++) {
  869. struct idxd_wq *wq;
  870. grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
  871. group->grpcfg.wqs[i] = ioread64(idxd->reg_base + grpcfg_offset);
  872. dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
  873. group->id, i, grpcfg_offset, group->grpcfg.wqs[i]);
  874. if (i * 64 >= idxd->max_wqs)
  875. break;
  876. /* Iterate through all 64 bits and check for wq set */
  877. for (j = 0; j < 64; j++) {
  878. int id = i * 64 + j;
  879. /* No need to check beyond max wqs */
  880. if (id >= idxd->max_wqs)
  881. break;
  882. /* Set group assignment for wq if wq bit is set */
  883. if (group->grpcfg.wqs[i] & BIT(j)) {
  884. wq = idxd->wqs[id];
  885. wq->group = group;
  886. }
  887. }
  888. }
  889. grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
  890. group->grpcfg.engines = ioread64(idxd->reg_base + grpcfg_offset);
  891. dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
  892. grpcfg_offset, group->grpcfg.engines);
  893. /* Iterate through all 64 bits to check engines set */
  894. for (i = 0; i < 64; i++) {
  895. if (i >= idxd->max_engines)
  896. break;
  897. if (group->grpcfg.engines & BIT(i)) {
  898. struct idxd_engine *engine = idxd->engines[i];
  899. engine->group = group;
  900. }
  901. }
  902. grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
  903. group->grpcfg.flags.bits = ioread64(idxd->reg_base + grpcfg_offset);
  904. dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#llx\n",
  905. group->id, grpcfg_offset, group->grpcfg.flags.bits);
  906. }
  907. int idxd_device_load_config(struct idxd_device *idxd)
  908. {
  909. union gencfg_reg reg;
  910. int i, rc;
  911. reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
  912. idxd->rdbuf_limit = reg.rdbuf_limit;
  913. for (i = 0; i < idxd->max_groups; i++) {
  914. struct idxd_group *group = idxd->groups[i];
  915. idxd_group_load_config(group);
  916. }
  917. for (i = 0; i < idxd->max_wqs; i++) {
  918. struct idxd_wq *wq = idxd->wqs[i];
  919. rc = idxd_wq_load_config(wq);
  920. if (rc < 0)
  921. return rc;
  922. }
  923. return 0;
  924. }
  925. static void idxd_flush_pending_descs(struct idxd_irq_entry *ie)
  926. {
  927. struct idxd_desc *desc, *itr;
  928. struct llist_node *head;
  929. LIST_HEAD(flist);
  930. enum idxd_complete_type ctype;
  931. spin_lock(&ie->list_lock);
  932. head = llist_del_all(&ie->pending_llist);
  933. if (head) {
  934. llist_for_each_entry_safe(desc, itr, head, llnode)
  935. list_add_tail(&desc->list, &ie->work_list);
  936. }
  937. list_for_each_entry_safe(desc, itr, &ie->work_list, list)
  938. list_move_tail(&desc->list, &flist);
  939. spin_unlock(&ie->list_lock);
  940. list_for_each_entry_safe(desc, itr, &flist, list) {
  941. struct dma_async_tx_descriptor *tx;
  942. list_del(&desc->list);
  943. ctype = desc->completion->status ? IDXD_COMPLETE_NORMAL : IDXD_COMPLETE_ABORT;
  944. /*
  945. * wq is being disabled. Any remaining descriptors are
  946. * likely to be stuck and can be dropped. callback could
  947. * point to code that is no longer accessible, for example
  948. * if dmatest module has been unloaded.
  949. */
  950. tx = &desc->txd;
  951. tx->callback = NULL;
  952. tx->callback_result = NULL;
  953. idxd_dma_complete_txd(desc, ctype, true);
  954. }
  955. }
  956. static void idxd_device_set_perm_entry(struct idxd_device *idxd,
  957. struct idxd_irq_entry *ie)
  958. {
  959. union msix_perm mperm;
  960. if (ie->pasid == INVALID_IOASID)
  961. return;
  962. mperm.bits = 0;
  963. mperm.pasid = ie->pasid;
  964. mperm.pasid_en = 1;
  965. iowrite32(mperm.bits, idxd->reg_base + idxd->msix_perm_offset + ie->id * 8);
  966. }
  967. static void idxd_device_clear_perm_entry(struct idxd_device *idxd,
  968. struct idxd_irq_entry *ie)
  969. {
  970. iowrite32(0, idxd->reg_base + idxd->msix_perm_offset + ie->id * 8);
  971. }
  972. void idxd_wq_free_irq(struct idxd_wq *wq)
  973. {
  974. struct idxd_device *idxd = wq->idxd;
  975. struct idxd_irq_entry *ie = &wq->ie;
  976. if (wq->type != IDXD_WQT_KERNEL)
  977. return;
  978. free_irq(ie->vector, ie);
  979. idxd_flush_pending_descs(ie);
  980. if (idxd->request_int_handles)
  981. idxd_device_release_int_handle(idxd, ie->int_handle, IDXD_IRQ_MSIX);
  982. idxd_device_clear_perm_entry(idxd, ie);
  983. ie->vector = -1;
  984. ie->int_handle = INVALID_INT_HANDLE;
  985. ie->pasid = INVALID_IOASID;
  986. }
  987. int idxd_wq_request_irq(struct idxd_wq *wq)
  988. {
  989. struct idxd_device *idxd = wq->idxd;
  990. struct pci_dev *pdev = idxd->pdev;
  991. struct device *dev = &pdev->dev;
  992. struct idxd_irq_entry *ie;
  993. int rc;
  994. if (wq->type != IDXD_WQT_KERNEL)
  995. return 0;
  996. ie = &wq->ie;
  997. ie->vector = pci_irq_vector(pdev, ie->id);
  998. ie->pasid = device_pasid_enabled(idxd) ? idxd->pasid : INVALID_IOASID;
  999. idxd_device_set_perm_entry(idxd, ie);
  1000. rc = request_threaded_irq(ie->vector, NULL, idxd_wq_thread, 0, "idxd-portal", ie);
  1001. if (rc < 0) {
  1002. dev_err(dev, "Failed to request irq %d.\n", ie->vector);
  1003. goto err_irq;
  1004. }
  1005. if (idxd->request_int_handles) {
  1006. rc = idxd_device_request_int_handle(idxd, ie->id, &ie->int_handle,
  1007. IDXD_IRQ_MSIX);
  1008. if (rc < 0)
  1009. goto err_int_handle;
  1010. } else {
  1011. ie->int_handle = ie->id;
  1012. }
  1013. return 0;
  1014. err_int_handle:
  1015. ie->int_handle = INVALID_INT_HANDLE;
  1016. free_irq(ie->vector, ie);
  1017. err_irq:
  1018. idxd_device_clear_perm_entry(idxd, ie);
  1019. ie->pasid = INVALID_IOASID;
  1020. return rc;
  1021. }
  1022. int drv_enable_wq(struct idxd_wq *wq)
  1023. {
  1024. struct idxd_device *idxd = wq->idxd;
  1025. struct device *dev = &idxd->pdev->dev;
  1026. int rc = -ENXIO;
  1027. lockdep_assert_held(&wq->wq_lock);
  1028. if (idxd->state != IDXD_DEV_ENABLED) {
  1029. idxd->cmd_status = IDXD_SCMD_DEV_NOT_ENABLED;
  1030. goto err;
  1031. }
  1032. if (wq->state != IDXD_WQ_DISABLED) {
  1033. dev_dbg(dev, "wq %d already enabled.\n", wq->id);
  1034. idxd->cmd_status = IDXD_SCMD_WQ_ENABLED;
  1035. rc = -EBUSY;
  1036. goto err;
  1037. }
  1038. if (!wq->group) {
  1039. dev_dbg(dev, "wq %d not attached to group.\n", wq->id);
  1040. idxd->cmd_status = IDXD_SCMD_WQ_NO_GRP;
  1041. goto err;
  1042. }
  1043. if (strlen(wq->name) == 0) {
  1044. idxd->cmd_status = IDXD_SCMD_WQ_NO_NAME;
  1045. dev_dbg(dev, "wq %d name not set.\n", wq->id);
  1046. goto err;
  1047. }
  1048. /* Shared WQ checks */
  1049. if (wq_shared(wq)) {
  1050. if (!wq_shared_supported(wq)) {
  1051. idxd->cmd_status = IDXD_SCMD_WQ_NO_SVM;
  1052. dev_dbg(dev, "PASID not enabled and shared wq.\n");
  1053. goto err;
  1054. }
  1055. /*
  1056. * Shared wq with the threshold set to 0 means the user
  1057. * did not set the threshold or transitioned from a
  1058. * dedicated wq but did not set threshold. A value
  1059. * of 0 would effectively disable the shared wq. The
  1060. * driver does not allow a value of 0 to be set for
  1061. * threshold via sysfs.
  1062. */
  1063. if (wq->threshold == 0) {
  1064. idxd->cmd_status = IDXD_SCMD_WQ_NO_THRESH;
  1065. dev_dbg(dev, "Shared wq and threshold 0.\n");
  1066. goto err;
  1067. }
  1068. }
  1069. /*
  1070. * In the event that the WQ is configurable for pasid and priv bits.
  1071. * For kernel wq, the driver should setup the pasid, pasid_en, and priv bit.
  1072. * However, for non-kernel wq, the driver should only set the pasid_en bit for
  1073. * shared wq. A dedicated wq that is not 'kernel' type will configure pasid and
  1074. * pasid_en later on so there is no need to setup.
  1075. */
  1076. if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
  1077. int priv = 0;
  1078. if (wq_pasid_enabled(wq)) {
  1079. if (is_idxd_wq_kernel(wq) || wq_shared(wq)) {
  1080. u32 pasid = wq_dedicated(wq) ? idxd->pasid : 0;
  1081. __idxd_wq_set_pasid_locked(wq, pasid);
  1082. }
  1083. }
  1084. if (is_idxd_wq_kernel(wq))
  1085. priv = 1;
  1086. __idxd_wq_set_priv_locked(wq, priv);
  1087. }
  1088. rc = 0;
  1089. spin_lock(&idxd->dev_lock);
  1090. if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
  1091. rc = idxd_device_config(idxd);
  1092. spin_unlock(&idxd->dev_lock);
  1093. if (rc < 0) {
  1094. dev_dbg(dev, "Writing wq %d config failed: %d\n", wq->id, rc);
  1095. goto err;
  1096. }
  1097. rc = idxd_wq_enable(wq);
  1098. if (rc < 0) {
  1099. dev_dbg(dev, "wq %d enabling failed: %d\n", wq->id, rc);
  1100. goto err;
  1101. }
  1102. rc = idxd_wq_map_portal(wq);
  1103. if (rc < 0) {
  1104. idxd->cmd_status = IDXD_SCMD_WQ_PORTAL_ERR;
  1105. dev_dbg(dev, "wq %d portal mapping failed: %d\n", wq->id, rc);
  1106. goto err_map_portal;
  1107. }
  1108. wq->client_count = 0;
  1109. rc = idxd_wq_request_irq(wq);
  1110. if (rc < 0) {
  1111. idxd->cmd_status = IDXD_SCMD_WQ_IRQ_ERR;
  1112. dev_dbg(dev, "WQ %d irq setup failed: %d\n", wq->id, rc);
  1113. goto err_irq;
  1114. }
  1115. rc = idxd_wq_alloc_resources(wq);
  1116. if (rc < 0) {
  1117. idxd->cmd_status = IDXD_SCMD_WQ_RES_ALLOC_ERR;
  1118. dev_dbg(dev, "WQ resource alloc failed\n");
  1119. goto err_res_alloc;
  1120. }
  1121. rc = idxd_wq_init_percpu_ref(wq);
  1122. if (rc < 0) {
  1123. idxd->cmd_status = IDXD_SCMD_PERCPU_ERR;
  1124. dev_dbg(dev, "percpu_ref setup failed\n");
  1125. goto err_ref;
  1126. }
  1127. return 0;
  1128. err_ref:
  1129. idxd_wq_free_resources(wq);
  1130. err_res_alloc:
  1131. idxd_wq_free_irq(wq);
  1132. err_irq:
  1133. idxd_wq_unmap_portal(wq);
  1134. err_map_portal:
  1135. if (idxd_wq_disable(wq, false))
  1136. dev_dbg(dev, "wq %s disable failed\n", dev_name(wq_confdev(wq)));
  1137. err:
  1138. return rc;
  1139. }
  1140. void drv_disable_wq(struct idxd_wq *wq)
  1141. {
  1142. struct idxd_device *idxd = wq->idxd;
  1143. struct device *dev = &idxd->pdev->dev;
  1144. lockdep_assert_held(&wq->wq_lock);
  1145. if (idxd_wq_refcount(wq))
  1146. dev_warn(dev, "Clients has claim on wq %d: %d\n",
  1147. wq->id, idxd_wq_refcount(wq));
  1148. idxd_wq_unmap_portal(wq);
  1149. idxd_wq_drain(wq);
  1150. idxd_wq_free_irq(wq);
  1151. idxd_wq_reset(wq);
  1152. idxd_wq_free_resources(wq);
  1153. percpu_ref_exit(&wq->wq_active);
  1154. wq->type = IDXD_WQT_NONE;
  1155. wq->client_count = 0;
  1156. }
  1157. int idxd_device_drv_probe(struct idxd_dev *idxd_dev)
  1158. {
  1159. struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
  1160. int rc = 0;
  1161. /*
  1162. * Device should be in disabled state for the idxd_drv to load. If it's in
  1163. * enabled state, then the device was altered outside of driver's control.
  1164. * If the state is in halted state, then we don't want to proceed.
  1165. */
  1166. if (idxd->state != IDXD_DEV_DISABLED) {
  1167. idxd->cmd_status = IDXD_SCMD_DEV_ENABLED;
  1168. return -ENXIO;
  1169. }
  1170. /* Device configuration */
  1171. spin_lock(&idxd->dev_lock);
  1172. if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
  1173. rc = idxd_device_config(idxd);
  1174. spin_unlock(&idxd->dev_lock);
  1175. if (rc < 0)
  1176. return -ENXIO;
  1177. /* Start device */
  1178. rc = idxd_device_enable(idxd);
  1179. if (rc < 0)
  1180. return rc;
  1181. /* Setup DMA device without channels */
  1182. rc = idxd_register_dma_device(idxd);
  1183. if (rc < 0) {
  1184. idxd_device_disable(idxd);
  1185. idxd->cmd_status = IDXD_SCMD_DEV_DMA_ERR;
  1186. return rc;
  1187. }
  1188. idxd->cmd_status = 0;
  1189. return 0;
  1190. }
  1191. void idxd_device_drv_remove(struct idxd_dev *idxd_dev)
  1192. {
  1193. struct device *dev = &idxd_dev->conf_dev;
  1194. struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
  1195. int i;
  1196. for (i = 0; i < idxd->max_wqs; i++) {
  1197. struct idxd_wq *wq = idxd->wqs[i];
  1198. struct device *wq_dev = wq_confdev(wq);
  1199. if (wq->state == IDXD_WQ_DISABLED)
  1200. continue;
  1201. dev_warn(dev, "Active wq %d on disable %s.\n", i, dev_name(wq_dev));
  1202. device_release_driver(wq_dev);
  1203. }
  1204. idxd_unregister_dma_device(idxd);
  1205. idxd_device_disable(idxd);
  1206. if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
  1207. idxd_device_reset(idxd);
  1208. }
  1209. static enum idxd_dev_type dev_types[] = {
  1210. IDXD_DEV_DSA,
  1211. IDXD_DEV_IAX,
  1212. IDXD_DEV_NONE,
  1213. };
  1214. struct idxd_device_driver idxd_drv = {
  1215. .type = dev_types,
  1216. .probe = idxd_device_drv_probe,
  1217. .remove = idxd_device_drv_remove,
  1218. .name = "idxd",
  1219. };
  1220. EXPORT_SYMBOL_GPL(idxd_drv);