1bfe1d560SDave Jiang // SPDX-License-Identifier: GPL-2.0
2bfe1d560SDave Jiang /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
3bfe1d560SDave Jiang #include <linux/init.h>
4bfe1d560SDave Jiang #include <linux/kernel.h>
5bfe1d560SDave Jiang #include <linux/module.h>
6bfe1d560SDave Jiang #include <linux/slab.h>
7bfe1d560SDave Jiang #include <linux/pci.h>
8bfe1d560SDave Jiang #include <linux/interrupt.h>
9bfe1d560SDave Jiang #include <linux/delay.h>
10bfe1d560SDave Jiang #include <linux/dma-mapping.h>
11bfe1d560SDave Jiang #include <linux/workqueue.h>
12bfe1d560SDave Jiang #include <linux/fs.h>
13bfe1d560SDave Jiang #include <linux/io-64-nonatomic-lo-hi.h>
14bfe1d560SDave Jiang #include <linux/device.h>
15bfe1d560SDave Jiang #include <linux/idr.h>
168e50d392SDave Jiang #include <linux/iommu.h>
17bfe1d560SDave Jiang #include <uapi/linux/idxd.h>
188f47d1a5SDave Jiang #include <linux/dmaengine.h>
198f47d1a5SDave Jiang #include "../dmaengine.h"
20bfe1d560SDave Jiang #include "registers.h"
21bfe1d560SDave Jiang #include "idxd.h"
220bde4444STom Zanussi #include "perfmon.h"
23bfe1d560SDave Jiang
24bfe1d560SDave Jiang MODULE_VERSION(IDXD_DRIVER_VERSION);
256e2fb806SJeff Johnson MODULE_DESCRIPTION("Intel Data Streaming Accelerator and In-Memory Analytics Accelerator common driver");
26bfe1d560SDave Jiang MODULE_LICENSE("GPL v2");
27bfe1d560SDave Jiang MODULE_AUTHOR("Intel Corporation");
28d9e5481fSDave Jiang MODULE_IMPORT_NS(IDXD);
29bfe1d560SDave Jiang
3003d939c7SDave Jiang static bool sva = true;
3103d939c7SDave Jiang module_param(sva, bool, 0644);
3203d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
3303d939c7SDave Jiang
34ade8a86bSDave Jiang bool tc_override;
35ade8a86bSDave Jiang module_param(tc_override, bool, 0644);
36ade8a86bSDave Jiang MODULE_PARM_DESC(tc_override, "Override traffic class defaults");
37ade8a86bSDave Jiang
38bfe1d560SDave Jiang #define DRV_NAME "idxd"
39bfe1d560SDave Jiang
408e50d392SDave Jiang bool support_enqcmd;
414b73e4ebSDave Jiang DEFINE_IDA(idxd_ida);
42bfe1d560SDave Jiang
43435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = {
44435b512dSDave Jiang [IDXD_TYPE_DSA] = {
45435b512dSDave Jiang .name_prefix = "dsa",
46435b512dSDave Jiang .type = IDXD_TYPE_DSA,
47435b512dSDave Jiang .compl_size = sizeof(struct dsa_completion_record),
48435b512dSDave Jiang .align = 32,
49435b512dSDave Jiang .dev_type = &dsa_device_type,
50c40bd7d9SDave Jiang .evl_cr_off = offsetof(struct dsa_evl_entry, cr),
51e11452ebSArjan van de Ven .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */
522442b747SDave Jiang .cr_status_off = offsetof(struct dsa_completion_record, status),
532442b747SDave Jiang .cr_result_off = offsetof(struct dsa_completion_record, result),
54435b512dSDave Jiang },
55435b512dSDave Jiang [IDXD_TYPE_IAX] = {
56435b512dSDave Jiang .name_prefix = "iax",
57435b512dSDave Jiang .type = IDXD_TYPE_IAX,
58435b512dSDave Jiang .compl_size = sizeof(struct iax_completion_record),
59435b512dSDave Jiang .align = 64,
60435b512dSDave Jiang .dev_type = &iax_device_type,
61c40bd7d9SDave Jiang .evl_cr_off = offsetof(struct iax_evl_entry, cr),
62e11452ebSArjan van de Ven .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */
632442b747SDave Jiang .cr_status_off = offsetof(struct iax_completion_record, status),
642442b747SDave Jiang .cr_result_off = offsetof(struct iax_completion_record, error_code),
65979f6dedSTom Zanussi .load_device_defaults = idxd_load_iaa_device_defaults,
66435b512dSDave Jiang },
67435b512dSDave Jiang };
68435b512dSDave Jiang
69bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = {
70bfe1d560SDave Jiang /* DSA ver 1.0 platforms */
71435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
72f91f2a98SFenghua Yu /* DSA on GNR-D platforms */
73f91f2a98SFenghua Yu { PCI_DEVICE_DATA(INTEL, DSA_GNRD, &idxd_driver_data[IDXD_TYPE_DSA]) },
74*4fecf944SFenghua Yu /* DSA on DMR platforms */
75*4fecf944SFenghua Yu { PCI_DEVICE_DATA(INTEL, DSA_DMR, &idxd_driver_data[IDXD_TYPE_DSA]) },
76f25b4638SDave Jiang
77f25b4638SDave Jiang /* IAX ver 1.0 platforms */
78435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
79*4fecf944SFenghua Yu /* IAA on DMR platforms */
80*4fecf944SFenghua Yu { PCI_DEVICE_DATA(INTEL, IAA_DMR, &idxd_driver_data[IDXD_TYPE_IAX]) },
81bfe1d560SDave Jiang { 0, }
82bfe1d560SDave Jiang };
83bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
84bfe1d560SDave Jiang
idxd_setup_interrupts(struct idxd_device * idxd)85bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd)
86bfe1d560SDave Jiang {
87bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev;
88bfe1d560SDave Jiang struct device *dev = &pdev->dev;
89ec0d6423SDave Jiang struct idxd_irq_entry *ie;
90bfe1d560SDave Jiang int i, msixcnt;
91bfe1d560SDave Jiang int rc = 0;
92bfe1d560SDave Jiang
93bfe1d560SDave Jiang msixcnt = pci_msix_vec_count(pdev);
94bfe1d560SDave Jiang if (msixcnt < 0) {
95bfe1d560SDave Jiang dev_err(dev, "Not MSI-X interrupt capable.\n");
965fc8e85fSDave Jiang return -ENOSPC;
97bfe1d560SDave Jiang }
988b67426eSDave Jiang idxd->irq_cnt = msixcnt;
99bfe1d560SDave Jiang
1005fc8e85fSDave Jiang rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
1015fc8e85fSDave Jiang if (rc != msixcnt) {
1025fc8e85fSDave Jiang dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
1035fc8e85fSDave Jiang return -ENOSPC;
104bfe1d560SDave Jiang }
105bfe1d560SDave Jiang dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
106bfe1d560SDave Jiang
107d5c10e0fSDave Jiang
108ec0d6423SDave Jiang ie = idxd_get_ie(idxd, 0);
109ec0d6423SDave Jiang ie->vector = pci_irq_vector(pdev, 0);
110ec0d6423SDave Jiang rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie);
111bfe1d560SDave Jiang if (rc < 0) {
112bfe1d560SDave Jiang dev_err(dev, "Failed to allocate misc interrupt.\n");
1135fc8e85fSDave Jiang goto err_misc_irq;
114bfe1d560SDave Jiang }
115403a2e23SDave Jiang dev_dbg(dev, "Requested idxd-misc handler on msix vector %d\n", ie->vector);
116bfe1d560SDave Jiang
117ec0d6423SDave Jiang for (i = 0; i < idxd->max_wqs; i++) {
118ec0d6423SDave Jiang int msix_idx = i + 1;
119bfe1d560SDave Jiang
120ec0d6423SDave Jiang ie = idxd_get_ie(idxd, msix_idx);
121ec0d6423SDave Jiang ie->id = msix_idx;
122ec0d6423SDave Jiang ie->int_handle = INVALID_INT_HANDLE;
123fffaed1eSJacob Pan ie->pasid = IOMMU_PASID_INVALID;
124403a2e23SDave Jiang
125ec0d6423SDave Jiang spin_lock_init(&ie->list_lock);
126ec0d6423SDave Jiang init_llist_head(&ie->pending_llist);
127ec0d6423SDave Jiang INIT_LIST_HEAD(&ie->work_list);
128bfe1d560SDave Jiang }
129bfe1d560SDave Jiang
130bfe1d560SDave Jiang idxd_unmask_error_interrupts(idxd);
131bfe1d560SDave Jiang return 0;
132bfe1d560SDave Jiang
1335fc8e85fSDave Jiang err_misc_irq:
134bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd);
1355fc8e85fSDave Jiang pci_free_irq_vectors(pdev);
136bfe1d560SDave Jiang dev_err(dev, "No usable interrupts\n");
137bfe1d560SDave Jiang return rc;
138bfe1d560SDave Jiang }
139bfe1d560SDave Jiang
idxd_cleanup_interrupts(struct idxd_device * idxd)140ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd)
141ddf742d4SDave Jiang {
142ddf742d4SDave Jiang struct pci_dev *pdev = idxd->pdev;
143ec0d6423SDave Jiang struct idxd_irq_entry *ie;
144403a2e23SDave Jiang int msixcnt;
145ddf742d4SDave Jiang
146403a2e23SDave Jiang msixcnt = pci_msix_vec_count(pdev);
147403a2e23SDave Jiang if (msixcnt <= 0)
148403a2e23SDave Jiang return;
149ddf742d4SDave Jiang
150403a2e23SDave Jiang ie = idxd_get_ie(idxd, 0);
151ddf742d4SDave Jiang idxd_mask_error_interrupts(idxd);
152403a2e23SDave Jiang free_irq(ie->vector, ie);
153ddf742d4SDave Jiang pci_free_irq_vectors(pdev);
154ddf742d4SDave Jiang }
155ddf742d4SDave Jiang
idxd_setup_wqs(struct idxd_device * idxd)1567c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd)
1577c5dd23eSDave Jiang {
1587c5dd23eSDave Jiang struct device *dev = &idxd->pdev->dev;
1597c5dd23eSDave Jiang struct idxd_wq *wq;
160700af3a0SDave Jiang struct device *conf_dev;
1617c5dd23eSDave Jiang int i, rc;
1627c5dd23eSDave Jiang
1637c5dd23eSDave Jiang idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
1647c5dd23eSDave Jiang GFP_KERNEL, dev_to_node(dev));
1657c5dd23eSDave Jiang if (!idxd->wqs)
1667c5dd23eSDave Jiang return -ENOMEM;
1677c5dd23eSDave Jiang
168de5819b9SJerry Snitselaar idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev));
169de5819b9SJerry Snitselaar if (!idxd->wq_enable_map) {
170de5819b9SJerry Snitselaar kfree(idxd->wqs);
171de5819b9SJerry Snitselaar return -ENOMEM;
172de5819b9SJerry Snitselaar }
173de5819b9SJerry Snitselaar
1747c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) {
1757c5dd23eSDave Jiang wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
1767c5dd23eSDave Jiang if (!wq) {
1777c5dd23eSDave Jiang rc = -ENOMEM;
1787c5dd23eSDave Jiang goto err;
1797c5dd23eSDave Jiang }
1807c5dd23eSDave Jiang
181700af3a0SDave Jiang idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
182700af3a0SDave Jiang conf_dev = wq_confdev(wq);
1837c5dd23eSDave Jiang wq->id = i;
1847c5dd23eSDave Jiang wq->idxd = idxd;
185700af3a0SDave Jiang device_initialize(wq_confdev(wq));
186700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd);
187700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type;
188700af3a0SDave Jiang conf_dev->type = &idxd_wq_device_type;
189700af3a0SDave Jiang rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
1907c5dd23eSDave Jiang if (rc < 0) {
191700af3a0SDave Jiang put_device(conf_dev);
1927c5dd23eSDave Jiang goto err;
1937c5dd23eSDave Jiang }
1947c5dd23eSDave Jiang
1957c5dd23eSDave Jiang mutex_init(&wq->wq_lock);
19604922b74SDave Jiang init_waitqueue_head(&wq->err_queue);
19793a40a6dSDave Jiang init_completion(&wq->wq_dead);
19856fc39f5SDave Jiang init_completion(&wq->wq_resurrect);
19992452a72SDave Jiang wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER;
200e8dbd644SXiaochen Shen idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH);
2017930d855SDave Jiang wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
2027c5dd23eSDave Jiang wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
2037c5dd23eSDave Jiang if (!wq->wqcfg) {
204700af3a0SDave Jiang put_device(conf_dev);
2057c5dd23eSDave Jiang rc = -ENOMEM;
2067c5dd23eSDave Jiang goto err;
2077c5dd23eSDave Jiang }
208b0325aefSDave Jiang
209b0325aefSDave Jiang if (idxd->hw.wq_cap.op_config) {
210b0325aefSDave Jiang wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
211b0325aefSDave Jiang if (!wq->opcap_bmap) {
212b0325aefSDave Jiang put_device(conf_dev);
213b0325aefSDave Jiang rc = -ENOMEM;
214b0325aefSDave Jiang goto err;
215b0325aefSDave Jiang }
216b0325aefSDave Jiang bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
217b0325aefSDave Jiang }
218b022f597SFenghua Yu mutex_init(&wq->uc_lock);
219b022f597SFenghua Yu xa_init(&wq->upasid_xa);
2207c5dd23eSDave Jiang idxd->wqs[i] = wq;
2217c5dd23eSDave Jiang }
2227c5dd23eSDave Jiang
2237c5dd23eSDave Jiang return 0;
2247c5dd23eSDave Jiang
2257c5dd23eSDave Jiang err:
226700af3a0SDave Jiang while (--i >= 0) {
227700af3a0SDave Jiang wq = idxd->wqs[i];
228700af3a0SDave Jiang conf_dev = wq_confdev(wq);
229700af3a0SDave Jiang put_device(conf_dev);
230700af3a0SDave Jiang }
2317c5dd23eSDave Jiang return rc;
2327c5dd23eSDave Jiang }
2337c5dd23eSDave Jiang
idxd_setup_engines(struct idxd_device * idxd)23475b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd)
23575b91130SDave Jiang {
23675b91130SDave Jiang struct idxd_engine *engine;
23775b91130SDave Jiang struct device *dev = &idxd->pdev->dev;
238700af3a0SDave Jiang struct device *conf_dev;
23975b91130SDave Jiang int i, rc;
24075b91130SDave Jiang
24175b91130SDave Jiang idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
24275b91130SDave Jiang GFP_KERNEL, dev_to_node(dev));
24375b91130SDave Jiang if (!idxd->engines)
24475b91130SDave Jiang return -ENOMEM;
24575b91130SDave Jiang
24675b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) {
24775b91130SDave Jiang engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
24875b91130SDave Jiang if (!engine) {
24975b91130SDave Jiang rc = -ENOMEM;
25075b91130SDave Jiang goto err;
25175b91130SDave Jiang }
25275b91130SDave Jiang
253700af3a0SDave Jiang idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE);
254700af3a0SDave Jiang conf_dev = engine_confdev(engine);
25575b91130SDave Jiang engine->id = i;
25675b91130SDave Jiang engine->idxd = idxd;
257700af3a0SDave Jiang device_initialize(conf_dev);
258700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd);
259700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type;
260700af3a0SDave Jiang conf_dev->type = &idxd_engine_device_type;
261700af3a0SDave Jiang rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
26275b91130SDave Jiang if (rc < 0) {
263700af3a0SDave Jiang put_device(conf_dev);
26475b91130SDave Jiang goto err;
26575b91130SDave Jiang }
26675b91130SDave Jiang
26775b91130SDave Jiang idxd->engines[i] = engine;
26875b91130SDave Jiang }
26975b91130SDave Jiang
27075b91130SDave Jiang return 0;
27175b91130SDave Jiang
27275b91130SDave Jiang err:
273700af3a0SDave Jiang while (--i >= 0) {
274700af3a0SDave Jiang engine = idxd->engines[i];
275700af3a0SDave Jiang conf_dev = engine_confdev(engine);
276700af3a0SDave Jiang put_device(conf_dev);
277700af3a0SDave Jiang }
27875b91130SDave Jiang return rc;
27975b91130SDave Jiang }
28075b91130SDave Jiang
idxd_setup_groups(struct idxd_device * idxd)281defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd)
282defe49f9SDave Jiang {
283defe49f9SDave Jiang struct device *dev = &idxd->pdev->dev;
284700af3a0SDave Jiang struct device *conf_dev;
285defe49f9SDave Jiang struct idxd_group *group;
286defe49f9SDave Jiang int i, rc;
287defe49f9SDave Jiang
288defe49f9SDave Jiang idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
289defe49f9SDave Jiang GFP_KERNEL, dev_to_node(dev));
290defe49f9SDave Jiang if (!idxd->groups)
291defe49f9SDave Jiang return -ENOMEM;
292defe49f9SDave Jiang
293defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) {
294defe49f9SDave Jiang group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
295defe49f9SDave Jiang if (!group) {
296defe49f9SDave Jiang rc = -ENOMEM;
297defe49f9SDave Jiang goto err;
298defe49f9SDave Jiang }
299defe49f9SDave Jiang
300700af3a0SDave Jiang idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP);
301700af3a0SDave Jiang conf_dev = group_confdev(group);
302defe49f9SDave Jiang group->id = i;
303defe49f9SDave Jiang group->idxd = idxd;
304700af3a0SDave Jiang device_initialize(conf_dev);
305700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd);
306700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type;
307700af3a0SDave Jiang conf_dev->type = &idxd_group_device_type;
308700af3a0SDave Jiang rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
309defe49f9SDave Jiang if (rc < 0) {
310700af3a0SDave Jiang put_device(conf_dev);
311defe49f9SDave Jiang goto err;
312defe49f9SDave Jiang }
313defe49f9SDave Jiang
314defe49f9SDave Jiang idxd->groups[i] = group;
3159735bde3SFenghua Yu if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) {
316ade8a86bSDave Jiang group->tc_a = 1;
317ade8a86bSDave Jiang group->tc_b = 1;
318ade8a86bSDave Jiang } else {
319defe49f9SDave Jiang group->tc_a = -1;
320defe49f9SDave Jiang group->tc_b = -1;
321defe49f9SDave Jiang }
322601bdadaSFenghua Yu /*
323601bdadaSFenghua Yu * The default value is the same as the value of
324601bdadaSFenghua Yu * total read buffers in GRPCAP.
325601bdadaSFenghua Yu */
326601bdadaSFenghua Yu group->rdbufs_allowed = idxd->max_rdbufs;
327ade8a86bSDave Jiang }
328defe49f9SDave Jiang
329defe49f9SDave Jiang return 0;
330defe49f9SDave Jiang
331defe49f9SDave Jiang err:
332700af3a0SDave Jiang while (--i >= 0) {
333700af3a0SDave Jiang group = idxd->groups[i];
334700af3a0SDave Jiang put_device(group_confdev(group));
335700af3a0SDave Jiang }
336defe49f9SDave Jiang return rc;
337defe49f9SDave Jiang }
338defe49f9SDave Jiang
idxd_cleanup_internals(struct idxd_device * idxd)339ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd)
340ddf742d4SDave Jiang {
341ddf742d4SDave Jiang int i;
342ddf742d4SDave Jiang
343ddf742d4SDave Jiang for (i = 0; i < idxd->max_groups; i++)
344700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i]));
345ddf742d4SDave Jiang for (i = 0; i < idxd->max_engines; i++)
346700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i]));
347ddf742d4SDave Jiang for (i = 0; i < idxd->max_wqs; i++)
348700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i]));
349ddf742d4SDave Jiang destroy_workqueue(idxd->wq);
350ddf742d4SDave Jiang }
351ddf742d4SDave Jiang
idxd_init_evl(struct idxd_device * idxd)3521649091fSDave Jiang static int idxd_init_evl(struct idxd_device *idxd)
3531649091fSDave Jiang {
3541649091fSDave Jiang struct device *dev = &idxd->pdev->dev;
355d3ea125dSFenghua Yu unsigned int evl_cache_size;
3561649091fSDave Jiang struct idxd_evl *evl;
357d3ea125dSFenghua Yu const char *idxd_name;
3581649091fSDave Jiang
3591649091fSDave Jiang if (idxd->hw.gen_cap.evl_support == 0)
3601649091fSDave Jiang return 0;
3611649091fSDave Jiang
3621649091fSDave Jiang evl = kzalloc_node(sizeof(*evl), GFP_KERNEL, dev_to_node(dev));
3631649091fSDave Jiang if (!evl)
3641649091fSDave Jiang return -ENOMEM;
3651649091fSDave Jiang
366d5638de8SRex Zhang mutex_init(&evl->lock);
3671649091fSDave Jiang evl->size = IDXD_EVL_SIZE_MIN;
368c2f156bfSDave Jiang
369d3ea125dSFenghua Yu idxd_name = dev_name(idxd_confdev(idxd));
370d3ea125dSFenghua Yu evl_cache_size = sizeof(struct idxd_evl_fault) + evl_ent_size(idxd);
371d3ea125dSFenghua Yu /*
372d3ea125dSFenghua Yu * Since completion record in evl_cache will be copied to user
373d3ea125dSFenghua Yu * when handling completion record page fault, need to create
374d3ea125dSFenghua Yu * the cache suitable for user copy.
375d3ea125dSFenghua Yu */
376d3ea125dSFenghua Yu idxd->evl_cache = kmem_cache_create_usercopy(idxd_name, evl_cache_size,
377d3ea125dSFenghua Yu 0, 0, 0, evl_cache_size,
378d3ea125dSFenghua Yu NULL);
379c2f156bfSDave Jiang if (!idxd->evl_cache) {
380c2f156bfSDave Jiang kfree(evl);
381c2f156bfSDave Jiang return -ENOMEM;
382c2f156bfSDave Jiang }
383c2f156bfSDave Jiang
3841649091fSDave Jiang idxd->evl = evl;
3851649091fSDave Jiang return 0;
3861649091fSDave Jiang }
3871649091fSDave Jiang
idxd_setup_internals(struct idxd_device * idxd)388bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd)
389bfe1d560SDave Jiang {
390bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev;
391defe49f9SDave Jiang int rc, i;
392bfe1d560SDave Jiang
3930d5c10b4SDave Jiang init_waitqueue_head(&idxd->cmd_waitq);
3947c5dd23eSDave Jiang
3957c5dd23eSDave Jiang rc = idxd_setup_wqs(idxd);
3967c5dd23eSDave Jiang if (rc < 0)
397eb15e715SDave Jiang goto err_wqs;
3987c5dd23eSDave Jiang
39975b91130SDave Jiang rc = idxd_setup_engines(idxd);
40075b91130SDave Jiang if (rc < 0)
40175b91130SDave Jiang goto err_engine;
40275b91130SDave Jiang
403defe49f9SDave Jiang rc = idxd_setup_groups(idxd);
404defe49f9SDave Jiang if (rc < 0)
405defe49f9SDave Jiang goto err_group;
406bfe1d560SDave Jiang
4070d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev));
4087c5dd23eSDave Jiang if (!idxd->wq) {
4097c5dd23eSDave Jiang rc = -ENOMEM;
410defe49f9SDave Jiang goto err_wkq_create;
4117c5dd23eSDave Jiang }
4120d5c10b4SDave Jiang
4131649091fSDave Jiang rc = idxd_init_evl(idxd);
4141649091fSDave Jiang if (rc < 0)
4151649091fSDave Jiang goto err_evl;
4161649091fSDave Jiang
417bfe1d560SDave Jiang return 0;
4187c5dd23eSDave Jiang
4191649091fSDave Jiang err_evl:
4201649091fSDave Jiang destroy_workqueue(idxd->wq);
421defe49f9SDave Jiang err_wkq_create:
422defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++)
423700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i]));
424defe49f9SDave Jiang err_group:
42575b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++)
426700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i]));
42775b91130SDave Jiang err_engine:
4287c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++)
429700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i]));
430eb15e715SDave Jiang err_wqs:
4317c5dd23eSDave Jiang return rc;
432bfe1d560SDave Jiang }
433bfe1d560SDave Jiang
idxd_read_table_offsets(struct idxd_device * idxd)434bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd)
435bfe1d560SDave Jiang {
436bfe1d560SDave Jiang union offsets_reg offsets;
437bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev;
438bfe1d560SDave Jiang
439bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
4402f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
4412f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
442bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
4432f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
4442f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
4452f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
4462f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
4472f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
448bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
449bfe1d560SDave Jiang }
450bfe1d560SDave Jiang
multi_u64_to_bmap(unsigned long * bmap,u64 * val,int count)45134ca0066SDave Jiang void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count)
452a8563a33SDave Jiang {
453a8563a33SDave Jiang int i, j, nr;
454a8563a33SDave Jiang
455a8563a33SDave Jiang for (i = 0, nr = 0; i < count; i++) {
456a8563a33SDave Jiang for (j = 0; j < BITS_PER_LONG_LONG; j++) {
457a8563a33SDave Jiang if (val[i] & BIT(j))
458a8563a33SDave Jiang set_bit(nr, bmap);
459a8563a33SDave Jiang nr++;
460a8563a33SDave Jiang }
461a8563a33SDave Jiang }
462a8563a33SDave Jiang }
463a8563a33SDave Jiang
idxd_read_caps(struct idxd_device * idxd)464bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd)
465bfe1d560SDave Jiang {
466bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev;
467bfe1d560SDave Jiang int i;
468bfe1d560SDave Jiang
469bfe1d560SDave Jiang /* reading generic capabilities */
470bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
471bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
472eb15e715SDave Jiang
473eb15e715SDave Jiang if (idxd->hw.gen_cap.cmd_cap) {
474eb15e715SDave Jiang idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
475eb15e715SDave Jiang dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
476eb15e715SDave Jiang }
477eb15e715SDave Jiang
4788b67426eSDave Jiang /* reading command capabilities */
4798b67426eSDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE))
4808b67426eSDave Jiang idxd->request_int_handles = true;
4818b67426eSDave Jiang
482bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
483bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
484e8dbd644SXiaochen Shen idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift);
485bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
486bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en)
487bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
488bfe1d560SDave Jiang
489bfe1d560SDave Jiang /* reading group capabilities */
490bfe1d560SDave Jiang idxd->hw.group_cap.bits =
491bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
492bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
493bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups;
494bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
4957ed6f1b8SDave Jiang idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs;
4967ed6f1b8SDave Jiang dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs);
4977ed6f1b8SDave Jiang idxd->nr_rdbufs = idxd->max_rdbufs;
498bfe1d560SDave Jiang
499bfe1d560SDave Jiang /* read engine capabilities */
500bfe1d560SDave Jiang idxd->hw.engine_cap.bits =
501bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
502bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
503bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines;
504bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
505bfe1d560SDave Jiang
506bfe1d560SDave Jiang /* read workqueue capabilities */
507bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
508bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
509bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
510bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
511bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
512bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
513d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
514d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
515bfe1d560SDave Jiang
516bfe1d560SDave Jiang /* reading operation capabilities */
517bfe1d560SDave Jiang for (i = 0; i < 4; i++) {
518bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
519bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64));
520bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
521bfe1d560SDave Jiang }
522a8563a33SDave Jiang multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4);
5239f0d99b3SDave Jiang
5249f0d99b3SDave Jiang /* read iaa cap */
5259f0d99b3SDave Jiang if (idxd->data->type == IDXD_TYPE_IAX && idxd->hw.version >= DEVICE_VERSION_2)
5269f0d99b3SDave Jiang idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET);
527bfe1d560SDave Jiang }
528bfe1d560SDave Jiang
idxd_alloc(struct pci_dev * pdev,struct idxd_driver_data * data)529435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
530bfe1d560SDave Jiang {
531bfe1d560SDave Jiang struct device *dev = &pdev->dev;
532700af3a0SDave Jiang struct device *conf_dev;
533bfe1d560SDave Jiang struct idxd_device *idxd;
53447c16ac2SDave Jiang int rc;
535bfe1d560SDave Jiang
53647c16ac2SDave Jiang idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
537bfe1d560SDave Jiang if (!idxd)
538bfe1d560SDave Jiang return NULL;
539bfe1d560SDave Jiang
540700af3a0SDave Jiang conf_dev = idxd_confdev(idxd);
541bfe1d560SDave Jiang idxd->pdev = pdev;
542435b512dSDave Jiang idxd->data = data;
543700af3a0SDave Jiang idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
5444b73e4ebSDave Jiang idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
54547c16ac2SDave Jiang if (idxd->id < 0)
54647c16ac2SDave Jiang return NULL;
54747c16ac2SDave Jiang
548a8563a33SDave Jiang idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev));
549a8563a33SDave Jiang if (!idxd->opcap_bmap) {
550a8563a33SDave Jiang ida_free(&idxd_ida, idxd->id);
551a8563a33SDave Jiang return NULL;
552a8563a33SDave Jiang }
553a8563a33SDave Jiang
554700af3a0SDave Jiang device_initialize(conf_dev);
555700af3a0SDave Jiang conf_dev->parent = dev;
556700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type;
557700af3a0SDave Jiang conf_dev->type = idxd->data->dev_type;
558700af3a0SDave Jiang rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
55947c16ac2SDave Jiang if (rc < 0) {
560700af3a0SDave Jiang put_device(conf_dev);
56147c16ac2SDave Jiang return NULL;
56247c16ac2SDave Jiang }
56347c16ac2SDave Jiang
564bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock);
56553b2ee7fSDave Jiang spin_lock_init(&idxd->cmd_lock);
566bfe1d560SDave Jiang
567bfe1d560SDave Jiang return idxd;
568bfe1d560SDave Jiang }
569bfe1d560SDave Jiang
idxd_enable_system_pasid(struct idxd_device * idxd)5708e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd)
5718e50d392SDave Jiang {
572f5ccf55eSJacob Pan struct pci_dev *pdev = idxd->pdev;
573f5ccf55eSJacob Pan struct device *dev = &pdev->dev;
574f5ccf55eSJacob Pan struct iommu_domain *domain;
575f5ccf55eSJacob Pan ioasid_t pasid;
576f5ccf55eSJacob Pan int ret;
577f5ccf55eSJacob Pan
578f5ccf55eSJacob Pan /*
579f5ccf55eSJacob Pan * Attach a global PASID to the DMA domain so that we can use ENQCMDS
580f5ccf55eSJacob Pan * to submit work on buffers mapped by DMA API.
581f5ccf55eSJacob Pan */
582f5ccf55eSJacob Pan domain = iommu_get_domain_for_dev(dev);
583f5ccf55eSJacob Pan if (!domain)
584f5ccf55eSJacob Pan return -EPERM;
585f5ccf55eSJacob Pan
586f5ccf55eSJacob Pan pasid = iommu_alloc_global_pasid(dev);
587f5ccf55eSJacob Pan if (pasid == IOMMU_PASID_INVALID)
588f5ccf55eSJacob Pan return -ENOSPC;
589f5ccf55eSJacob Pan
590f5ccf55eSJacob Pan /*
591f5ccf55eSJacob Pan * DMA domain is owned by the driver, it should support all valid
592f5ccf55eSJacob Pan * types such as DMA-FQ, identity, etc.
593f5ccf55eSJacob Pan */
59414678219SLu Baolu ret = iommu_attach_device_pasid(domain, dev, pasid, NULL);
595f5ccf55eSJacob Pan if (ret) {
596f5ccf55eSJacob Pan dev_err(dev, "failed to attach device pasid %d, domain type %d",
597f5ccf55eSJacob Pan pasid, domain->type);
598f5ccf55eSJacob Pan iommu_free_global_pasid(pasid);
599f5ccf55eSJacob Pan return ret;
600f5ccf55eSJacob Pan }
601f5ccf55eSJacob Pan
602f5ccf55eSJacob Pan /* Since we set user privilege for kernel DMA, enable completion IRQ */
603f5ccf55eSJacob Pan idxd_set_user_intr(idxd, 1);
604f5ccf55eSJacob Pan idxd->pasid = pasid;
605f5ccf55eSJacob Pan
606f5ccf55eSJacob Pan return ret;
6078e50d392SDave Jiang }
6088e50d392SDave Jiang
idxd_disable_system_pasid(struct idxd_device * idxd)6098e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd)
6108e50d392SDave Jiang {
611f5ccf55eSJacob Pan struct pci_dev *pdev = idxd->pdev;
612f5ccf55eSJacob Pan struct device *dev = &pdev->dev;
613f5ccf55eSJacob Pan struct iommu_domain *domain;
6148e50d392SDave Jiang
615f5ccf55eSJacob Pan domain = iommu_get_domain_for_dev(dev);
616f5ccf55eSJacob Pan if (!domain)
617f5ccf55eSJacob Pan return;
618f5ccf55eSJacob Pan
619f5ccf55eSJacob Pan iommu_detach_device_pasid(domain, dev, idxd->pasid);
620f5ccf55eSJacob Pan iommu_free_global_pasid(idxd->pasid);
621f5ccf55eSJacob Pan
622f5ccf55eSJacob Pan idxd_set_user_intr(idxd, 0);
6238e50d392SDave Jiang idxd->sva = NULL;
624f5ccf55eSJacob Pan idxd->pasid = IOMMU_PASID_INVALID;
6258e50d392SDave Jiang }
6268e50d392SDave Jiang
idxd_enable_sva(struct pci_dev * pdev)62784c9ef72SLu Baolu static int idxd_enable_sva(struct pci_dev *pdev)
62884c9ef72SLu Baolu {
62984c9ef72SLu Baolu int ret;
63084c9ef72SLu Baolu
63184c9ef72SLu Baolu ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF);
63284c9ef72SLu Baolu if (ret)
63384c9ef72SLu Baolu return ret;
63484c9ef72SLu Baolu
63584c9ef72SLu Baolu ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
63684c9ef72SLu Baolu if (ret)
63784c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF);
63884c9ef72SLu Baolu
63984c9ef72SLu Baolu return ret;
64084c9ef72SLu Baolu }
64184c9ef72SLu Baolu
idxd_disable_sva(struct pci_dev * pdev)64284c9ef72SLu Baolu static void idxd_disable_sva(struct pci_dev *pdev)
64384c9ef72SLu Baolu {
64484c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
64584c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF);
64684c9ef72SLu Baolu }
64784c9ef72SLu Baolu
idxd_probe(struct idxd_device * idxd)648bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd)
649bfe1d560SDave Jiang {
650bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev;
651bfe1d560SDave Jiang struct device *dev = &pdev->dev;
652bfe1d560SDave Jiang int rc;
653bfe1d560SDave Jiang
654bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__);
65589e3becdSDave Jiang rc = idxd_device_init_reset(idxd);
65689e3becdSDave Jiang if (rc < 0)
65789e3becdSDave Jiang return rc;
65889e3becdSDave Jiang
659bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n");
660bfe1d560SDave Jiang
66103d939c7SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
66284c9ef72SLu Baolu if (idxd_enable_sva(pdev)) {
66342a1b738SDave Jiang dev_warn(dev, "Unable to turn on user SVA feature.\n");
6648ffccd11SJerry Snitselaar } else {
66542a1b738SDave Jiang set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);
66642a1b738SDave Jiang
667f5ccf55eSJacob Pan rc = idxd_enable_system_pasid(idxd);
668f5ccf55eSJacob Pan if (rc)
669f5ccf55eSJacob Pan dev_warn(dev, "No in-kernel DMA with PASID. %d\n", rc);
67042a1b738SDave Jiang else
6718e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
6728ffccd11SJerry Snitselaar }
67303d939c7SDave Jiang } else if (!sva) {
67403d939c7SDave Jiang dev_warn(dev, "User forced SVA off via module param.\n");
6758e50d392SDave Jiang }
6768e50d392SDave Jiang
677bfe1d560SDave Jiang idxd_read_caps(idxd);
678bfe1d560SDave Jiang idxd_read_table_offsets(idxd);
679bfe1d560SDave Jiang
680bfe1d560SDave Jiang rc = idxd_setup_internals(idxd);
681bfe1d560SDave Jiang if (rc)
6827c5dd23eSDave Jiang goto err;
683bfe1d560SDave Jiang
6848c66bbdcSDave Jiang /* If the configs are readonly, then load them from device */
6858c66bbdcSDave Jiang if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
6868c66bbdcSDave Jiang dev_dbg(dev, "Loading RO device config\n");
6878c66bbdcSDave Jiang rc = idxd_device_load_config(idxd);
6888c66bbdcSDave Jiang if (rc < 0)
689ddf742d4SDave Jiang goto err_config;
6908c66bbdcSDave Jiang }
6918c66bbdcSDave Jiang
692bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd);
693bfe1d560SDave Jiang if (rc)
694ddf742d4SDave Jiang goto err_config;
695bfe1d560SDave Jiang
69642d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd);
69742d279f9SDave Jiang
6980bde4444STom Zanussi rc = perfmon_pmu_init(idxd);
6990bde4444STom Zanussi if (rc < 0)
7000bde4444STom Zanussi dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
7010bde4444STom Zanussi
702bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
703bfe1d560SDave Jiang return 0;
704bfe1d560SDave Jiang
705ddf742d4SDave Jiang err_config:
706ddf742d4SDave Jiang idxd_cleanup_internals(idxd);
7077c5dd23eSDave Jiang err:
7088e50d392SDave Jiang if (device_pasid_enabled(idxd))
7098e50d392SDave Jiang idxd_disable_system_pasid(idxd);
71042a1b738SDave Jiang if (device_user_pasid_enabled(idxd))
71184c9ef72SLu Baolu idxd_disable_sva(pdev);
712bfe1d560SDave Jiang return rc;
713bfe1d560SDave Jiang }
714bfe1d560SDave Jiang
idxd_cleanup(struct idxd_device * idxd)715ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd)
716ddf742d4SDave Jiang {
717ddf742d4SDave Jiang perfmon_pmu_remove(idxd);
718ddf742d4SDave Jiang idxd_cleanup_interrupts(idxd);
719ddf742d4SDave Jiang idxd_cleanup_internals(idxd);
720ddf742d4SDave Jiang if (device_pasid_enabled(idxd))
721ddf742d4SDave Jiang idxd_disable_system_pasid(idxd);
72242a1b738SDave Jiang if (device_user_pasid_enabled(idxd))
72384c9ef72SLu Baolu idxd_disable_sva(idxd->pdev);
724ddf742d4SDave Jiang }
725ddf742d4SDave Jiang
idxd_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)726bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
727bfe1d560SDave Jiang {
728bfe1d560SDave Jiang struct device *dev = &pdev->dev;
729bfe1d560SDave Jiang struct idxd_device *idxd;
730435b512dSDave Jiang struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
731bfe1d560SDave Jiang int rc;
732bfe1d560SDave Jiang
733a39c7cd0SDave Jiang rc = pci_enable_device(pdev);
734bfe1d560SDave Jiang if (rc)
735bfe1d560SDave Jiang return rc;
736bfe1d560SDave Jiang
7378e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n");
738435b512dSDave Jiang idxd = idxd_alloc(pdev, data);
739a39c7cd0SDave Jiang if (!idxd) {
740a39c7cd0SDave Jiang rc = -ENOMEM;
741a39c7cd0SDave Jiang goto err_idxd_alloc;
742a39c7cd0SDave Jiang }
743bfe1d560SDave Jiang
7448e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n");
745a39c7cd0SDave Jiang idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
746a39c7cd0SDave Jiang if (!idxd->reg_base) {
747a39c7cd0SDave Jiang rc = -ENOMEM;
748a39c7cd0SDave Jiang goto err_iomap;
749a39c7cd0SDave Jiang }
750bfe1d560SDave Jiang
751bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n");
75253b50458SChristophe JAILLET rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
753bfe1d560SDave Jiang if (rc)
754a39c7cd0SDave Jiang goto err;
755bfe1d560SDave Jiang
756bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n");
757bfe1d560SDave Jiang pci_set_master(pdev);
758bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd);
759bfe1d560SDave Jiang
760bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
761bfe1d560SDave Jiang rc = idxd_probe(idxd);
762bfe1d560SDave Jiang if (rc) {
763bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
764a39c7cd0SDave Jiang goto err;
765bfe1d560SDave Jiang }
766bfe1d560SDave Jiang
767979f6dedSTom Zanussi if (data->load_device_defaults) {
768979f6dedSTom Zanussi rc = data->load_device_defaults(idxd);
769979f6dedSTom Zanussi if (rc)
770979f6dedSTom Zanussi dev_warn(dev, "IDXD loading device defaults failed\n");
771979f6dedSTom Zanussi }
772979f6dedSTom Zanussi
77347c16ac2SDave Jiang rc = idxd_register_devices(idxd);
774c52ca478SDave Jiang if (rc) {
775c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n");
776ddf742d4SDave Jiang goto err_dev_register;
777c52ca478SDave Jiang }
778c52ca478SDave Jiang
7795fbe6503SDave Jiang rc = idxd_device_init_debugfs(idxd);
7805fbe6503SDave Jiang if (rc)
7815fbe6503SDave Jiang dev_warn(dev, "IDXD debugfs failed to setup\n");
7825fbe6503SDave Jiang
783bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
784bfe1d560SDave Jiang idxd->hw.version);
785bfe1d560SDave Jiang
786e11452ebSArjan van de Ven idxd->user_submission_safe = data->user_submission_safe;
787e11452ebSArjan van de Ven
788bfe1d560SDave Jiang return 0;
789a39c7cd0SDave Jiang
790ddf742d4SDave Jiang err_dev_register:
791ddf742d4SDave Jiang idxd_cleanup(idxd);
792a39c7cd0SDave Jiang err:
793a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base);
794a39c7cd0SDave Jiang err_iomap:
795700af3a0SDave Jiang put_device(idxd_confdev(idxd));
796a39c7cd0SDave Jiang err_idxd_alloc:
797a39c7cd0SDave Jiang pci_disable_device(pdev);
798a39c7cd0SDave Jiang return rc;
799bfe1d560SDave Jiang }
800bfe1d560SDave Jiang
idxd_wqs_quiesce(struct idxd_device * idxd)8015b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd)
8025b0c68c4SDave Jiang {
8035b0c68c4SDave Jiang struct idxd_wq *wq;
8045b0c68c4SDave Jiang int i;
8055b0c68c4SDave Jiang
8065b0c68c4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) {
8075b0c68c4SDave Jiang wq = idxd->wqs[i];
8085b0c68c4SDave Jiang if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
8095b0c68c4SDave Jiang idxd_wq_quiesce(wq);
8105b0c68c4SDave Jiang }
8115b0c68c4SDave Jiang }
8125b0c68c4SDave Jiang
idxd_shutdown(struct pci_dev * pdev)813bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev)
814bfe1d560SDave Jiang {
815bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev);
816bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry;
817403a2e23SDave Jiang int rc;
818bfe1d560SDave Jiang
819bfe1d560SDave Jiang rc = idxd_device_disable(idxd);
820bfe1d560SDave Jiang if (rc)
821bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n");
822bfe1d560SDave Jiang
823403a2e23SDave Jiang irq_entry = &idxd->ie;
8245fc8e85fSDave Jiang synchronize_irq(irq_entry->vector);
825403a2e23SDave Jiang idxd_mask_error_interrupts(idxd);
82649c4959fSDave Jiang flush_workqueue(idxd->wq);
827bfe1d560SDave Jiang }
828bfe1d560SDave Jiang
idxd_remove(struct pci_dev * pdev)829bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev)
830bfe1d560SDave Jiang {
831bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev);
83249c4959fSDave Jiang struct idxd_irq_entry *irq_entry;
833bfe1d560SDave Jiang
83498da0106SDave Jiang idxd_unregister_devices(idxd);
83598da0106SDave Jiang /*
83698da0106SDave Jiang * When ->release() is called for the idxd->conf_dev, it frees all the memory related
83798da0106SDave Jiang * to the idxd context. The driver still needs those bits in order to do the rest of
83898da0106SDave Jiang * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
83998da0106SDave Jiang * on the device here to hold off the freeing while allowing the idxd sub-driver
84098da0106SDave Jiang * to unbind.
84198da0106SDave Jiang */
84298da0106SDave Jiang get_device(idxd_confdev(idxd));
84398da0106SDave Jiang device_unregister(idxd_confdev(idxd));
844bfe1d560SDave Jiang idxd_shutdown(pdev);
8458e50d392SDave Jiang if (device_pasid_enabled(idxd))
8468e50d392SDave Jiang idxd_disable_system_pasid(idxd);
8475fbe6503SDave Jiang idxd_device_remove_debugfs(idxd);
84849c4959fSDave Jiang
849403a2e23SDave Jiang irq_entry = idxd_get_ie(idxd, 0);
85049c4959fSDave Jiang free_irq(irq_entry->vector, irq_entry);
85149c4959fSDave Jiang pci_free_irq_vectors(pdev);
85249c4959fSDave Jiang pci_iounmap(pdev, idxd->reg_base);
85342a1b738SDave Jiang if (device_user_pasid_enabled(idxd))
85484c9ef72SLu Baolu idxd_disable_sva(pdev);
85549c4959fSDave Jiang pci_disable_device(pdev);
85649c4959fSDave Jiang destroy_workqueue(idxd->wq);
85749c4959fSDave Jiang perfmon_pmu_remove(idxd);
85898da0106SDave Jiang put_device(idxd_confdev(idxd));
859bfe1d560SDave Jiang }
860bfe1d560SDave Jiang
861bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = {
862bfe1d560SDave Jiang .name = DRV_NAME,
863bfe1d560SDave Jiang .id_table = idxd_pci_tbl,
864bfe1d560SDave Jiang .probe = idxd_pci_probe,
865bfe1d560SDave Jiang .remove = idxd_remove,
866bfe1d560SDave Jiang .shutdown = idxd_shutdown,
867bfe1d560SDave Jiang };
868bfe1d560SDave Jiang
idxd_init_module(void)869bfe1d560SDave Jiang static int __init idxd_init_module(void)
870bfe1d560SDave Jiang {
8714b73e4ebSDave Jiang int err;
872bfe1d560SDave Jiang
873bfe1d560SDave Jiang /*
8748e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
875bfe1d560SDave Jiang * enumerating the device. We can not utilize it.
876bfe1d560SDave Jiang */
87774b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
878bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n");
879bfe1d560SDave Jiang return -ENODEV;
880bfe1d560SDave Jiang }
881bfe1d560SDave Jiang
88274b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
8838e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n");
8848e50d392SDave Jiang else
8858e50d392SDave Jiang support_enqcmd = true;
886bfe1d560SDave Jiang
887034b3290SDave Jiang err = idxd_driver_register(&idxd_drv);
888034b3290SDave Jiang if (err < 0)
889034b3290SDave Jiang goto err_idxd_driver_register;
890034b3290SDave Jiang
8910cda4f69SDave Jiang err = idxd_driver_register(&idxd_dmaengine_drv);
8920cda4f69SDave Jiang if (err < 0)
8930cda4f69SDave Jiang goto err_idxd_dmaengine_driver_register;
8940cda4f69SDave Jiang
895448c3de8SDave Jiang err = idxd_driver_register(&idxd_user_drv);
896448c3de8SDave Jiang if (err < 0)
897448c3de8SDave Jiang goto err_idxd_user_driver_register;
898448c3de8SDave Jiang
89942d279f9SDave Jiang err = idxd_cdev_register();
90042d279f9SDave Jiang if (err)
90142d279f9SDave Jiang goto err_cdev_register;
90242d279f9SDave Jiang
9035fbe6503SDave Jiang err = idxd_init_debugfs();
9045fbe6503SDave Jiang if (err)
9055fbe6503SDave Jiang goto err_debugfs;
9065fbe6503SDave Jiang
907c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver);
908c52ca478SDave Jiang if (err)
909c52ca478SDave Jiang goto err_pci_register;
910c52ca478SDave Jiang
911bfe1d560SDave Jiang return 0;
912c52ca478SDave Jiang
913c52ca478SDave Jiang err_pci_register:
9145fbe6503SDave Jiang idxd_remove_debugfs();
9155fbe6503SDave Jiang err_debugfs:
91642d279f9SDave Jiang idxd_cdev_remove();
91742d279f9SDave Jiang err_cdev_register:
918448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv);
919448c3de8SDave Jiang err_idxd_user_driver_register:
9200cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv);
9210cda4f69SDave Jiang err_idxd_dmaengine_driver_register:
922034b3290SDave Jiang idxd_driver_unregister(&idxd_drv);
923034b3290SDave Jiang err_idxd_driver_register:
924c52ca478SDave Jiang return err;
925bfe1d560SDave Jiang }
926bfe1d560SDave Jiang module_init(idxd_init_module);
927bfe1d560SDave Jiang
idxd_exit_module(void)928bfe1d560SDave Jiang static void __exit idxd_exit_module(void)
929bfe1d560SDave Jiang {
930448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv);
9310cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv);
932034b3290SDave Jiang idxd_driver_unregister(&idxd_drv);
933bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver);
93442d279f9SDave Jiang idxd_cdev_remove();
9355fbe6503SDave Jiang idxd_remove_debugfs();
936bfe1d560SDave Jiang }
937bfe1d560SDave Jiang module_exit(idxd_exit_module);
938