1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
6 #include <linux/sbitmap.h>
7 #include <linux/dmaengine.h>
8 #include <linux/percpu-rwsem.h>
9 #include <linux/wait.h>
10 #include <linux/cdev.h>
11 #include <linux/idr.h>
12 #include <linux/pci.h>
13 #include <linux/ioasid.h>
14 #include <linux/perf_event.h>
15 #include <uapi/linux/idxd.h>
16 #include "registers.h"
18 #define IDXD_DRIVER_VERSION "1.00"
20 extern struct kmem_cache *idxd_desc_pool;
21 extern bool tc_override;
38 struct device conf_dev;
39 enum idxd_dev_type type;
42 #define IDXD_REG_TIMEOUT 50
43 #define IDXD_DRAIN_TIMEOUT 5000
46 IDXD_TYPE_UNKNOWN = -1,
52 #define IDXD_NAME_SIZE 128
53 #define IDXD_PMU_EVENT_MAX 64
55 #define IDXD_ENQCMDS_RETRIES 32
56 #define IDXD_ENQCMDS_MAX_RETRIES 64
58 struct idxd_device_driver {
60 enum idxd_dev_type *type;
61 int (*probe)(struct idxd_dev *idxd_dev);
62 void (*remove)(struct idxd_dev *idxd_dev);
63 struct device_driver drv;
66 extern struct idxd_device_driver dsa_drv;
67 extern struct idxd_device_driver idxd_drv;
68 extern struct idxd_device_driver idxd_dmaengine_drv;
69 extern struct idxd_device_driver idxd_user_drv;
71 #define INVALID_INT_HANDLE -1
72 struct idxd_irq_entry {
75 struct llist_head pending_llist;
76 struct list_head work_list;
78 * Lock to protect access between irq thread process descriptor
79 * and irq thread processing error descriptor.
87 struct idxd_dev idxd_dev;
88 struct idxd_device *idxd;
101 struct idxd_device *idxd;
103 struct perf_event *event_list[IDXD_PMU_EVENT_MAX];
106 DECLARE_BITMAP(used_mask, IDXD_PMU_EVENT_MAX);
109 char name[IDXD_NAME_SIZE];
114 int n_event_categories;
116 bool per_counter_caps_supported;
117 unsigned long supported_event_categories;
119 unsigned long supported_filters;
122 struct hlist_node cpuhp_node;
125 #define IDXD_MAX_PRIORITY 0xf
128 IDXD_WQ_DISABLED = 0,
133 WQ_FLAG_DEDICATED = 0,
134 WQ_FLAG_BLOCK_ON_FAULT,
146 struct idxd_dev idxd_dev;
150 #define IDXD_ALLOCATED_BATCH_SIZE 128U
151 #define WQ_NAME_SIZE 1024
152 #define WQ_TYPE_SIZE 10
154 #define WQ_DEFAULT_QUEUE_DEPTH 16
155 #define WQ_DEFAULT_MAX_XFER SZ_2M
156 #define WQ_DEFAULT_MAX_BATCH 32
160 IDXD_OP_NONBLOCK = 1,
163 enum idxd_complete_type {
164 IDXD_COMPLETE_NORMAL = 0,
166 IDXD_COMPLETE_DEV_FAIL,
169 struct idxd_dma_chan {
170 struct dma_chan chan;
175 void __iomem *portal;
177 unsigned int enqcmds_retries;
178 struct percpu_ref wq_active;
179 struct completion wq_dead;
180 struct completion wq_resurrect;
181 struct idxd_dev idxd_dev;
182 struct idxd_cdev *idxd_cdev;
183 struct wait_queue_head err_queue;
184 struct idxd_device *idxd;
186 struct idxd_irq_entry ie;
187 enum idxd_wq_type type;
188 struct idxd_group *group;
190 struct mutex wq_lock; /* mutex for workqueue */
194 enum idxd_wq_state state;
197 struct dsa_hw_desc **hw_descs;
200 struct dsa_completion_record *compls;
201 struct iax_completion_record *iax_compls;
203 dma_addr_t compls_addr;
205 struct idxd_desc **descs;
206 struct sbitmap_queue sbq;
207 struct idxd_dma_chan *idxd_chan;
208 char name[WQ_NAME_SIZE + 1];
215 struct idxd_dev idxd_dev;
217 struct idxd_group *group;
218 struct idxd_device *idxd;
221 /* shadow registers */
224 union gen_cap_reg gen_cap;
225 union wq_cap_reg wq_cap;
226 union group_cap_reg group_cap;
227 union engine_cap_reg engine_cap;
232 enum idxd_device_state {
233 IDXD_DEV_HALTED = -1,
234 IDXD_DEV_DISABLED = 0,
238 enum idxd_device_flag {
239 IDXD_FLAG_CONFIGURABLE = 0,
240 IDXD_FLAG_CMD_RUNNING,
241 IDXD_FLAG_PASID_ENABLED,
242 IDXD_FLAG_USER_PASID_ENABLED,
245 struct idxd_dma_dev {
246 struct idxd_device *idxd;
247 struct dma_device dma;
250 struct idxd_driver_data {
251 const char *name_prefix;
253 struct device_type *dev_type;
259 struct idxd_dev idxd_dev;
260 struct idxd_driver_data *data;
261 struct list_head list;
263 enum idxd_device_state state;
268 struct idxd_irq_entry ie; /* misc irq, msix 0 */
270 struct pci_dev *pdev;
271 void __iomem *reg_base;
273 spinlock_t dev_lock; /* spinlock for device */
274 spinlock_t cmd_lock; /* spinlock for device commands */
275 struct completion *cmd_done;
276 struct idxd_group **groups;
277 struct idxd_wq **wqs;
278 struct idxd_engine **engines;
280 struct iommu_sva *sva;
285 bool request_int_handles;
287 u32 msix_perm_offset;
300 int nr_rdbufs; /* non-reserved read buffers */
301 unsigned int wqcfg_size;
303 union sw_err_reg sw_err;
304 wait_queue_head_t cmd_waitq;
306 struct idxd_dma_dev *idxd_dma;
307 struct workqueue_struct *wq;
308 struct work_struct work;
310 struct idxd_pmu *idxd_pmu;
313 /* IDXD software descriptor */
316 struct dsa_hw_desc *hw;
317 struct iax_hw_desc *iax_hw;
321 struct dsa_completion_record *completion;
322 struct iax_completion_record *iax_completion;
324 dma_addr_t compl_dma;
325 struct dma_async_tx_descriptor txd;
326 struct llist_node llnode;
327 struct list_head list;
334 * This is software defined error for the completion status. We overload the error code
335 * that will never appear in completion status and only SWERR register.
337 enum idxd_completion_status {
338 IDXD_COMP_DESC_ABORT = 0xff,
341 #define idxd_confdev(idxd) &idxd->idxd_dev.conf_dev
342 #define wq_confdev(wq) &wq->idxd_dev.conf_dev
343 #define engine_confdev(engine) &engine->idxd_dev.conf_dev
344 #define group_confdev(group) &group->idxd_dev.conf_dev
345 #define cdev_dev(cdev) &cdev->idxd_dev.conf_dev
347 #define confdev_to_idxd_dev(dev) container_of(dev, struct idxd_dev, conf_dev)
348 #define idxd_dev_to_idxd(idxd_dev) container_of(idxd_dev, struct idxd_device, idxd_dev)
349 #define idxd_dev_to_wq(idxd_dev) container_of(idxd_dev, struct idxd_wq, idxd_dev)
351 static inline struct idxd_device *confdev_to_idxd(struct device *dev)
353 struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
355 return idxd_dev_to_idxd(idxd_dev);
358 static inline struct idxd_wq *confdev_to_wq(struct device *dev)
360 struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
362 return idxd_dev_to_wq(idxd_dev);
365 static inline struct idxd_engine *confdev_to_engine(struct device *dev)
367 struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
369 return container_of(idxd_dev, struct idxd_engine, idxd_dev);
372 static inline struct idxd_group *confdev_to_group(struct device *dev)
374 struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
376 return container_of(idxd_dev, struct idxd_group, idxd_dev);
379 static inline struct idxd_cdev *dev_to_cdev(struct device *dev)
381 struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
383 return container_of(idxd_dev, struct idxd_cdev, idxd_dev);
386 static inline void idxd_dev_set_type(struct idxd_dev *idev, int type)
388 if (type >= IDXD_DEV_MAX_TYPE) {
389 idev->type = IDXD_DEV_NONE;
396 static inline struct idxd_irq_entry *idxd_get_ie(struct idxd_device *idxd, int idx)
398 return (idx == 0) ? &idxd->ie : &idxd->wqs[idx - 1]->ie;
401 static inline struct idxd_wq *ie_to_wq(struct idxd_irq_entry *ie)
403 return container_of(ie, struct idxd_wq, ie);
406 static inline struct idxd_device *ie_to_idxd(struct idxd_irq_entry *ie)
408 return container_of(ie, struct idxd_device, ie);
411 extern struct bus_type dsa_bus_type;
413 extern bool support_enqcmd;
414 extern struct ida idxd_ida;
415 extern struct device_type dsa_device_type;
416 extern struct device_type iax_device_type;
417 extern struct device_type idxd_wq_device_type;
418 extern struct device_type idxd_engine_device_type;
419 extern struct device_type idxd_group_device_type;
421 static inline bool is_dsa_dev(struct idxd_dev *idxd_dev)
423 return idxd_dev->type == IDXD_DEV_DSA;
426 static inline bool is_iax_dev(struct idxd_dev *idxd_dev)
428 return idxd_dev->type == IDXD_DEV_IAX;
431 static inline bool is_idxd_dev(struct idxd_dev *idxd_dev)
433 return is_dsa_dev(idxd_dev) || is_iax_dev(idxd_dev);
436 static inline bool is_idxd_wq_dev(struct idxd_dev *idxd_dev)
438 return idxd_dev->type == IDXD_DEV_WQ;
441 static inline bool is_idxd_wq_dmaengine(struct idxd_wq *wq)
443 if (wq->type == IDXD_WQT_KERNEL && strcmp(wq->name, "dmaengine") == 0)
448 static inline bool is_idxd_wq_user(struct idxd_wq *wq)
450 return wq->type == IDXD_WQT_USER;
453 static inline bool is_idxd_wq_kernel(struct idxd_wq *wq)
455 return wq->type == IDXD_WQT_KERNEL;
458 static inline bool wq_dedicated(struct idxd_wq *wq)
460 return test_bit(WQ_FLAG_DEDICATED, &wq->flags);
463 static inline bool wq_shared(struct idxd_wq *wq)
465 return !test_bit(WQ_FLAG_DEDICATED, &wq->flags);
468 static inline bool device_pasid_enabled(struct idxd_device *idxd)
470 return test_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
473 static inline bool device_user_pasid_enabled(struct idxd_device *idxd)
475 return test_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);
478 static inline bool wq_pasid_enabled(struct idxd_wq *wq)
480 return (is_idxd_wq_kernel(wq) && device_pasid_enabled(wq->idxd)) ||
481 (is_idxd_wq_user(wq) && device_user_pasid_enabled(wq->idxd));
484 static inline bool wq_shared_supported(struct idxd_wq *wq)
486 return (support_enqcmd && wq_pasid_enabled(wq));
489 enum idxd_portal_prot {
490 IDXD_PORTAL_UNLIMITED = 0,
494 enum idxd_interrupt_type {
499 static inline int idxd_get_wq_portal_offset(enum idxd_portal_prot prot)
501 return prot * 0x1000;
504 static inline int idxd_get_wq_portal_full_offset(int wq_id,
505 enum idxd_portal_prot prot)
507 return ((wq_id * 4) << PAGE_SHIFT) + idxd_get_wq_portal_offset(prot);
510 #define IDXD_PORTAL_MASK (PAGE_SIZE - 1)
513 * Even though this function can be accessed by multiple threads, it is safe to use.
514 * At worst the address gets used more than once before it gets incremented. We don't
515 * hit a threshold until iops becomes many million times a second. So the occasional
516 * reuse of the same address is tolerable compare to using an atomic variable. This is
517 * safe on a system that has atomic load/store for 32bit integers. Given that this is an
518 * Intel iEP device, that should not be a problem.
520 static inline void __iomem *idxd_wq_portal_addr(struct idxd_wq *wq)
522 int ofs = wq->portal_offset;
524 wq->portal_offset = (ofs + sizeof(struct dsa_raw_desc)) & IDXD_PORTAL_MASK;
525 return wq->portal + ofs;
528 static inline void idxd_wq_get(struct idxd_wq *wq)
533 static inline void idxd_wq_put(struct idxd_wq *wq)
538 static inline int idxd_wq_refcount(struct idxd_wq *wq)
540 return wq->client_count;
543 int __must_check __idxd_driver_register(struct idxd_device_driver *idxd_drv,
544 struct module *module, const char *mod_name);
545 #define idxd_driver_register(driver) \
546 __idxd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
548 void idxd_driver_unregister(struct idxd_device_driver *idxd_drv);
550 #define module_idxd_driver(__idxd_driver) \
551 module_driver(__idxd_driver, idxd_driver_register, idxd_driver_unregister)
553 int idxd_register_bus_type(void);
554 void idxd_unregister_bus_type(void);
555 int idxd_register_devices(struct idxd_device *idxd);
556 void idxd_unregister_devices(struct idxd_device *idxd);
557 int idxd_register_driver(void);
558 void idxd_unregister_driver(void);
559 void idxd_wqs_quiesce(struct idxd_device *idxd);
560 bool idxd_queue_int_handle_resubmit(struct idxd_desc *desc);
562 /* device interrupt control */
563 irqreturn_t idxd_misc_thread(int vec, void *data);
564 irqreturn_t idxd_wq_thread(int irq, void *data);
565 void idxd_mask_error_interrupts(struct idxd_device *idxd);
566 void idxd_unmask_error_interrupts(struct idxd_device *idxd);
569 int idxd_register_idxd_drv(void);
570 void idxd_unregister_idxd_drv(void);
571 int idxd_device_drv_probe(struct idxd_dev *idxd_dev);
572 void idxd_device_drv_remove(struct idxd_dev *idxd_dev);
573 int drv_enable_wq(struct idxd_wq *wq);
574 void drv_disable_wq(struct idxd_wq *wq);
575 int idxd_device_init_reset(struct idxd_device *idxd);
576 int idxd_device_enable(struct idxd_device *idxd);
577 int idxd_device_disable(struct idxd_device *idxd);
578 void idxd_device_reset(struct idxd_device *idxd);
579 void idxd_device_clear_state(struct idxd_device *idxd);
580 int idxd_device_config(struct idxd_device *idxd);
581 void idxd_device_drain_pasid(struct idxd_device *idxd, int pasid);
582 int idxd_device_load_config(struct idxd_device *idxd);
583 int idxd_device_request_int_handle(struct idxd_device *idxd, int idx, int *handle,
584 enum idxd_interrupt_type irq_type);
585 int idxd_device_release_int_handle(struct idxd_device *idxd, int handle,
586 enum idxd_interrupt_type irq_type);
588 /* work queue control */
589 void idxd_wqs_unmap_portal(struct idxd_device *idxd);
590 int idxd_wq_alloc_resources(struct idxd_wq *wq);
591 void idxd_wq_free_resources(struct idxd_wq *wq);
592 int idxd_wq_enable(struct idxd_wq *wq);
593 int idxd_wq_disable(struct idxd_wq *wq, bool reset_config);
594 void idxd_wq_drain(struct idxd_wq *wq);
595 void idxd_wq_reset(struct idxd_wq *wq);
596 int idxd_wq_map_portal(struct idxd_wq *wq);
597 void idxd_wq_unmap_portal(struct idxd_wq *wq);
598 int idxd_wq_set_pasid(struct idxd_wq *wq, int pasid);
599 int idxd_wq_disable_pasid(struct idxd_wq *wq);
600 void __idxd_wq_quiesce(struct idxd_wq *wq);
601 void idxd_wq_quiesce(struct idxd_wq *wq);
602 int idxd_wq_init_percpu_ref(struct idxd_wq *wq);
603 void idxd_wq_free_irq(struct idxd_wq *wq);
604 int idxd_wq_request_irq(struct idxd_wq *wq);
607 int idxd_submit_desc(struct idxd_wq *wq, struct idxd_desc *desc);
608 struct idxd_desc *idxd_alloc_desc(struct idxd_wq *wq, enum idxd_op_type optype);
609 void idxd_free_desc(struct idxd_wq *wq, struct idxd_desc *desc);
610 int idxd_enqcmds(struct idxd_wq *wq, void __iomem *portal, const void *desc);
613 int idxd_register_dma_device(struct idxd_device *idxd);
614 void idxd_unregister_dma_device(struct idxd_device *idxd);
615 void idxd_parse_completion_status(u8 status, enum dmaengine_tx_result *res);
616 void idxd_dma_complete_txd(struct idxd_desc *desc,
617 enum idxd_complete_type comp_type, bool free_desc);
620 int idxd_cdev_register(void);
621 void idxd_cdev_remove(void);
622 int idxd_cdev_get_major(struct idxd_device *idxd);
623 int idxd_wq_add_cdev(struct idxd_wq *wq);
624 void idxd_wq_del_cdev(struct idxd_wq *wq);
627 #if IS_ENABLED(CONFIG_INTEL_IDXD_PERFMON)
628 int perfmon_pmu_init(struct idxd_device *idxd);
629 void perfmon_pmu_remove(struct idxd_device *idxd);
630 void perfmon_counter_overflow(struct idxd_device *idxd);
631 void perfmon_init(void);
632 void perfmon_exit(void);
634 static inline int perfmon_pmu_init(struct idxd_device *idxd) { return 0; }
635 static inline void perfmon_pmu_remove(struct idxd_device *idxd) {}
636 static inline void perfmon_counter_overflow(struct idxd_device *idxd) {}
637 static inline void perfmon_init(void) {}
638 static inline void perfmon_exit(void) {}