device.c 39 KB

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