Merge tag 'acpi-6.3-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6-block.git] / drivers / dma / idxd / sysfs.c
CommitLineData
c52ca478
DJ
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/device.h>
8#include <linux/io-64-nonatomic-lo-hi.h>
9#include <uapi/linux/idxd.h>
10#include "registers.h"
11#include "idxd.h"
12
13static char *idxd_wq_type_names[] = {
14 [IDXD_WQT_NONE] = "none",
15 [IDXD_WQT_KERNEL] = "kernel",
42d279f9 16 [IDXD_WQT_USER] = "user",
c52ca478
DJ
17};
18
c52ca478
DJ
19/* IDXD engine attributes */
20static ssize_t engine_group_id_show(struct device *dev,
21 struct device_attribute *attr, char *buf)
22{
700af3a0 23 struct idxd_engine *engine = confdev_to_engine(dev);
c52ca478
DJ
24
25 if (engine->group)
8241571f 26 return sysfs_emit(buf, "%d\n", engine->group->id);
c52ca478 27 else
8241571f 28 return sysfs_emit(buf, "%d\n", -1);
c52ca478
DJ
29}
30
31static ssize_t engine_group_id_store(struct device *dev,
32 struct device_attribute *attr,
33 const char *buf, size_t count)
34{
700af3a0 35 struct idxd_engine *engine = confdev_to_engine(dev);
c52ca478
DJ
36 struct idxd_device *idxd = engine->idxd;
37 long id;
38 int rc;
f7b280c6 39 struct idxd_group *prevg;
c52ca478
DJ
40
41 rc = kstrtol(buf, 10, &id);
42 if (rc < 0)
43 return -EINVAL;
44
45 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
46 return -EPERM;
47
48 if (id > idxd->max_groups - 1 || id < -1)
49 return -EINVAL;
50
51 if (id == -1) {
52 if (engine->group) {
53 engine->group->num_engines--;
54 engine->group = NULL;
55 }
56 return count;
57 }
58
c52ca478
DJ
59 prevg = engine->group;
60
61 if (prevg)
62 prevg->num_engines--;
defe49f9 63 engine->group = idxd->groups[id];
c52ca478
DJ
64 engine->group->num_engines++;
65
66 return count;
67}
68
69static struct device_attribute dev_attr_engine_group =
70 __ATTR(group_id, 0644, engine_group_id_show,
71 engine_group_id_store);
72
73static struct attribute *idxd_engine_attributes[] = {
74 &dev_attr_engine_group.attr,
75 NULL,
76};
77
78static const struct attribute_group idxd_engine_attribute_group = {
79 .attrs = idxd_engine_attributes,
80};
81
82static const struct attribute_group *idxd_engine_attribute_groups[] = {
83 &idxd_engine_attribute_group,
84 NULL,
85};
86
75b91130
DJ
87static void idxd_conf_engine_release(struct device *dev)
88{
700af3a0 89 struct idxd_engine *engine = confdev_to_engine(dev);
75b91130
DJ
90
91 kfree(engine);
92}
93
94struct device_type idxd_engine_device_type = {
95 .name = "engine",
96 .release = idxd_conf_engine_release,
97 .groups = idxd_engine_attribute_groups,
98};
99
c52ca478
DJ
100/* Group attributes */
101
7ed6f1b8 102static void idxd_set_free_rdbufs(struct idxd_device *idxd)
c52ca478 103{
7ed6f1b8 104 int i, rdbufs;
c52ca478 105
7ed6f1b8 106 for (i = 0, rdbufs = 0; i < idxd->max_groups; i++) {
defe49f9 107 struct idxd_group *g = idxd->groups[i];
c52ca478 108
7ed6f1b8 109 rdbufs += g->rdbufs_reserved;
c52ca478
DJ
110 }
111
7ed6f1b8 112 idxd->nr_rdbufs = idxd->max_rdbufs - rdbufs;
c52ca478
DJ
113}
114
fde212e4
DJ
115static ssize_t group_read_buffers_reserved_show(struct device *dev,
116 struct device_attribute *attr,
117 char *buf)
c52ca478 118{
700af3a0 119 struct idxd_group *group = confdev_to_group(dev);
c52ca478 120
7ed6f1b8 121 return sysfs_emit(buf, "%u\n", group->rdbufs_reserved);
c52ca478
DJ
122}
123
fde212e4
DJ
124static ssize_t group_tokens_reserved_show(struct device *dev,
125 struct device_attribute *attr,
126 char *buf)
127{
128 dev_warn_once(dev, "attribute deprecated, see read_buffers_reserved.\n");
129 return group_read_buffers_reserved_show(dev, attr, buf);
130}
131
132static ssize_t group_read_buffers_reserved_store(struct device *dev,
133 struct device_attribute *attr,
134 const char *buf, size_t count)
c52ca478 135{
700af3a0 136 struct idxd_group *group = confdev_to_group(dev);
c52ca478
DJ
137 struct idxd_device *idxd = group->idxd;
138 unsigned long val;
139 int rc;
140
141 rc = kstrtoul(buf, 10, &val);
142 if (rc < 0)
143 return -EINVAL;
144
435b512d 145 if (idxd->data->type == IDXD_TYPE_IAX)
f25b4638
DJ
146 return -EOPNOTSUPP;
147
c52ca478
DJ
148 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
149 return -EPERM;
150
151 if (idxd->state == IDXD_DEV_ENABLED)
152 return -EPERM;
153
7ed6f1b8 154 if (val > idxd->max_rdbufs)
c52ca478
DJ
155 return -EINVAL;
156
7ed6f1b8 157 if (val > idxd->nr_rdbufs + group->rdbufs_reserved)
c52ca478
DJ
158 return -EINVAL;
159
7ed6f1b8
DJ
160 group->rdbufs_reserved = val;
161 idxd_set_free_rdbufs(idxd);
c52ca478
DJ
162 return count;
163}
164
fde212e4
DJ
165static ssize_t group_tokens_reserved_store(struct device *dev,
166 struct device_attribute *attr,
167 const char *buf, size_t count)
168{
169 dev_warn_once(dev, "attribute deprecated, see read_buffers_reserved.\n");
170 return group_read_buffers_reserved_store(dev, attr, buf, count);
171}
172
c52ca478
DJ
173static struct device_attribute dev_attr_group_tokens_reserved =
174 __ATTR(tokens_reserved, 0644, group_tokens_reserved_show,
175 group_tokens_reserved_store);
176
fde212e4
DJ
177static struct device_attribute dev_attr_group_read_buffers_reserved =
178 __ATTR(read_buffers_reserved, 0644, group_read_buffers_reserved_show,
179 group_read_buffers_reserved_store);
180
181static ssize_t group_read_buffers_allowed_show(struct device *dev,
182 struct device_attribute *attr,
183 char *buf)
c52ca478 184{
700af3a0 185 struct idxd_group *group = confdev_to_group(dev);
c52ca478 186
7ed6f1b8 187 return sysfs_emit(buf, "%u\n", group->rdbufs_allowed);
c52ca478
DJ
188}
189
fde212e4
DJ
190static ssize_t group_tokens_allowed_show(struct device *dev,
191 struct device_attribute *attr,
192 char *buf)
193{
194 dev_warn_once(dev, "attribute deprecated, see read_buffers_allowed.\n");
195 return group_read_buffers_allowed_show(dev, attr, buf);
196}
197
198static ssize_t group_read_buffers_allowed_store(struct device *dev,
199 struct device_attribute *attr,
200 const char *buf, size_t count)
c52ca478 201{
700af3a0 202 struct idxd_group *group = confdev_to_group(dev);
c52ca478
DJ
203 struct idxd_device *idxd = group->idxd;
204 unsigned long val;
205 int rc;
206
207 rc = kstrtoul(buf, 10, &val);
208 if (rc < 0)
209 return -EINVAL;
210
435b512d 211 if (idxd->data->type == IDXD_TYPE_IAX)
f25b4638
DJ
212 return -EOPNOTSUPP;
213
c52ca478
DJ
214 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
215 return -EPERM;
216
217 if (idxd->state == IDXD_DEV_ENABLED)
218 return -EPERM;
219
c52ca478 220 if (val < 4 * group->num_engines ||
7ed6f1b8 221 val > group->rdbufs_reserved + idxd->nr_rdbufs)
c52ca478
DJ
222 return -EINVAL;
223
7ed6f1b8 224 group->rdbufs_allowed = val;
c52ca478
DJ
225 return count;
226}
227
fde212e4
DJ
228static ssize_t group_tokens_allowed_store(struct device *dev,
229 struct device_attribute *attr,
230 const char *buf, size_t count)
231{
232 dev_warn_once(dev, "attribute deprecated, see read_buffers_allowed.\n");
233 return group_read_buffers_allowed_store(dev, attr, buf, count);
234}
235
c52ca478
DJ
236static struct device_attribute dev_attr_group_tokens_allowed =
237 __ATTR(tokens_allowed, 0644, group_tokens_allowed_show,
238 group_tokens_allowed_store);
239
fde212e4
DJ
240static struct device_attribute dev_attr_group_read_buffers_allowed =
241 __ATTR(read_buffers_allowed, 0644, group_read_buffers_allowed_show,
242 group_read_buffers_allowed_store);
243
244static ssize_t group_use_read_buffer_limit_show(struct device *dev,
245 struct device_attribute *attr,
246 char *buf)
c52ca478 247{
700af3a0 248 struct idxd_group *group = confdev_to_group(dev);
c52ca478 249
7ed6f1b8 250 return sysfs_emit(buf, "%u\n", group->use_rdbuf_limit);
c52ca478
DJ
251}
252
fde212e4
DJ
253static ssize_t group_use_token_limit_show(struct device *dev,
254 struct device_attribute *attr,
255 char *buf)
256{
257 dev_warn_once(dev, "attribute deprecated, see use_read_buffer_limit.\n");
258 return group_use_read_buffer_limit_show(dev, attr, buf);
259}
260
261static ssize_t group_use_read_buffer_limit_store(struct device *dev,
262 struct device_attribute *attr,
263 const char *buf, size_t count)
c52ca478 264{
700af3a0 265 struct idxd_group *group = confdev_to_group(dev);
c52ca478
DJ
266 struct idxd_device *idxd = group->idxd;
267 unsigned long val;
268 int rc;
269
270 rc = kstrtoul(buf, 10, &val);
271 if (rc < 0)
272 return -EINVAL;
273
435b512d 274 if (idxd->data->type == IDXD_TYPE_IAX)
f25b4638
DJ
275 return -EOPNOTSUPP;
276
c52ca478
DJ
277 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
278 return -EPERM;
279
280 if (idxd->state == IDXD_DEV_ENABLED)
281 return -EPERM;
282
7ed6f1b8 283 if (idxd->rdbuf_limit == 0)
c52ca478
DJ
284 return -EPERM;
285
7ed6f1b8 286 group->use_rdbuf_limit = !!val;
c52ca478
DJ
287 return count;
288}
289
fde212e4
DJ
290static ssize_t group_use_token_limit_store(struct device *dev,
291 struct device_attribute *attr,
292 const char *buf, size_t count)
293{
294 dev_warn_once(dev, "attribute deprecated, see use_read_buffer_limit.\n");
295 return group_use_read_buffer_limit_store(dev, attr, buf, count);
296}
297
c52ca478
DJ
298static struct device_attribute dev_attr_group_use_token_limit =
299 __ATTR(use_token_limit, 0644, group_use_token_limit_show,
300 group_use_token_limit_store);
301
fde212e4
DJ
302static struct device_attribute dev_attr_group_use_read_buffer_limit =
303 __ATTR(use_read_buffer_limit, 0644, group_use_read_buffer_limit_show,
304 group_use_read_buffer_limit_store);
305
c52ca478
DJ
306static ssize_t group_engines_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
308{
700af3a0 309 struct idxd_group *group = confdev_to_group(dev);
c52ca478 310 int i, rc = 0;
c52ca478
DJ
311 struct idxd_device *idxd = group->idxd;
312
313 for (i = 0; i < idxd->max_engines; i++) {
75b91130 314 struct idxd_engine *engine = idxd->engines[i];
c52ca478
DJ
315
316 if (!engine->group)
317 continue;
318
319 if (engine->group->id == group->id)
8241571f 320 rc += sysfs_emit_at(buf, rc, "engine%d.%d ", idxd->id, engine->id);
c52ca478
DJ
321 }
322
8241571f
DJ
323 if (!rc)
324 return 0;
c52ca478 325 rc--;
8241571f 326 rc += sysfs_emit_at(buf, rc, "\n");
c52ca478
DJ
327
328 return rc;
329}
330
331static struct device_attribute dev_attr_group_engines =
332 __ATTR(engines, 0444, group_engines_show, NULL);
333
334static ssize_t group_work_queues_show(struct device *dev,
335 struct device_attribute *attr, char *buf)
336{
700af3a0 337 struct idxd_group *group = confdev_to_group(dev);
c52ca478 338 int i, rc = 0;
c52ca478
DJ
339 struct idxd_device *idxd = group->idxd;
340
341 for (i = 0; i < idxd->max_wqs; i++) {
7c5dd23e 342 struct idxd_wq *wq = idxd->wqs[i];
c52ca478
DJ
343
344 if (!wq->group)
345 continue;
346
347 if (wq->group->id == group->id)
8241571f 348 rc += sysfs_emit_at(buf, rc, "wq%d.%d ", idxd->id, wq->id);
c52ca478
DJ
349 }
350
8241571f
DJ
351 if (!rc)
352 return 0;
c52ca478 353 rc--;
8241571f 354 rc += sysfs_emit_at(buf, rc, "\n");
c52ca478
DJ
355
356 return rc;
357}
358
359static struct device_attribute dev_attr_group_work_queues =
360 __ATTR(work_queues, 0444, group_work_queues_show, NULL);
361
362static ssize_t group_traffic_class_a_show(struct device *dev,
363 struct device_attribute *attr,
364 char *buf)
365{
700af3a0 366 struct idxd_group *group = confdev_to_group(dev);
c52ca478 367
8241571f 368 return sysfs_emit(buf, "%d\n", group->tc_a);
c52ca478
DJ
369}
370
371static ssize_t group_traffic_class_a_store(struct device *dev,
372 struct device_attribute *attr,
373 const char *buf, size_t count)
374{
700af3a0 375 struct idxd_group *group = confdev_to_group(dev);
c52ca478
DJ
376 struct idxd_device *idxd = group->idxd;
377 long val;
378 int rc;
379
380 rc = kstrtol(buf, 10, &val);
381 if (rc < 0)
382 return -EINVAL;
383
384 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
385 return -EPERM;
386
387 if (idxd->state == IDXD_DEV_ENABLED)
388 return -EPERM;
389
9735bde3 390 if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override)
ade8a86b
DJ
391 return -EPERM;
392
c52ca478
DJ
393 if (val < 0 || val > 7)
394 return -EINVAL;
395
396 group->tc_a = val;
397 return count;
398}
399
400static struct device_attribute dev_attr_group_traffic_class_a =
401 __ATTR(traffic_class_a, 0644, group_traffic_class_a_show,
402 group_traffic_class_a_store);
403
404static ssize_t group_traffic_class_b_show(struct device *dev,
405 struct device_attribute *attr,
406 char *buf)
407{
700af3a0 408 struct idxd_group *group = confdev_to_group(dev);
c52ca478 409
8241571f 410 return sysfs_emit(buf, "%d\n", group->tc_b);
c52ca478
DJ
411}
412
413static ssize_t group_traffic_class_b_store(struct device *dev,
414 struct device_attribute *attr,
415 const char *buf, size_t count)
416{
700af3a0 417 struct idxd_group *group = confdev_to_group(dev);
c52ca478
DJ
418 struct idxd_device *idxd = group->idxd;
419 long val;
420 int rc;
421
422 rc = kstrtol(buf, 10, &val);
423 if (rc < 0)
424 return -EINVAL;
425
426 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
427 return -EPERM;
428
429 if (idxd->state == IDXD_DEV_ENABLED)
430 return -EPERM;
431
9735bde3 432 if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override)
ade8a86b
DJ
433 return -EPERM;
434
c52ca478
DJ
435 if (val < 0 || val > 7)
436 return -EINVAL;
437
438 group->tc_b = val;
439 return count;
440}
441
442static struct device_attribute dev_attr_group_traffic_class_b =
443 __ATTR(traffic_class_b, 0644, group_traffic_class_b_show,
444 group_traffic_class_b_store);
445
1f273752
DJ
446static ssize_t group_desc_progress_limit_show(struct device *dev,
447 struct device_attribute *attr,
448 char *buf)
449{
450 struct idxd_group *group = confdev_to_group(dev);
451
452 return sysfs_emit(buf, "%d\n", group->desc_progress_limit);
453}
454
455static ssize_t group_desc_progress_limit_store(struct device *dev,
456 struct device_attribute *attr,
457 const char *buf, size_t count)
458{
459 struct idxd_group *group = confdev_to_group(dev);
460 int val, rc;
461
462 rc = kstrtoint(buf, 10, &val);
463 if (rc < 0)
464 return -EINVAL;
465
466 if (val & ~GENMASK(1, 0))
467 return -EINVAL;
468
469 group->desc_progress_limit = val;
470 return count;
471}
472
473static struct device_attribute dev_attr_group_desc_progress_limit =
474 __ATTR(desc_progress_limit, 0644, group_desc_progress_limit_show,
475 group_desc_progress_limit_store);
476
7ca68fa3
DJ
477static ssize_t group_batch_progress_limit_show(struct device *dev,
478 struct device_attribute *attr,
479 char *buf)
480{
481 struct idxd_group *group = confdev_to_group(dev);
482
483 return sysfs_emit(buf, "%d\n", group->batch_progress_limit);
484}
485
486static ssize_t group_batch_progress_limit_store(struct device *dev,
487 struct device_attribute *attr,
488 const char *buf, size_t count)
489{
490 struct idxd_group *group = confdev_to_group(dev);
491 int val, rc;
492
493 rc = kstrtoint(buf, 10, &val);
494 if (rc < 0)
495 return -EINVAL;
496
497 if (val & ~GENMASK(1, 0))
498 return -EINVAL;
499
500 group->batch_progress_limit = val;
501 return count;
502}
503
504static struct device_attribute dev_attr_group_batch_progress_limit =
505 __ATTR(batch_progress_limit, 0644, group_batch_progress_limit_show,
506 group_batch_progress_limit_store);
c52ca478
DJ
507static struct attribute *idxd_group_attributes[] = {
508 &dev_attr_group_work_queues.attr,
509 &dev_attr_group_engines.attr,
510 &dev_attr_group_use_token_limit.attr,
fde212e4 511 &dev_attr_group_use_read_buffer_limit.attr,
c52ca478 512 &dev_attr_group_tokens_allowed.attr,
fde212e4 513 &dev_attr_group_read_buffers_allowed.attr,
c52ca478 514 &dev_attr_group_tokens_reserved.attr,
fde212e4 515 &dev_attr_group_read_buffers_reserved.attr,
c52ca478
DJ
516 &dev_attr_group_traffic_class_a.attr,
517 &dev_attr_group_traffic_class_b.attr,
1f273752 518 &dev_attr_group_desc_progress_limit.attr,
7ca68fa3 519 &dev_attr_group_batch_progress_limit.attr,
c52ca478
DJ
520 NULL,
521};
522
1f273752
DJ
523static bool idxd_group_attr_progress_limit_invisible(struct attribute *attr,
524 struct idxd_device *idxd)
525{
7ca68fa3
DJ
526 return (attr == &dev_attr_group_desc_progress_limit.attr ||
527 attr == &dev_attr_group_batch_progress_limit.attr) &&
528 !idxd->hw.group_cap.progress_limit;
1f273752
DJ
529}
530
9a8ddb35
XS
531static bool idxd_group_attr_read_buffers_invisible(struct attribute *attr,
532 struct idxd_device *idxd)
533{
534 /*
535 * Intel IAA does not support Read Buffer allocation control,
536 * make these attributes invisible.
537 */
538 return (attr == &dev_attr_group_use_token_limit.attr ||
539 attr == &dev_attr_group_use_read_buffer_limit.attr ||
540 attr == &dev_attr_group_tokens_allowed.attr ||
541 attr == &dev_attr_group_read_buffers_allowed.attr ||
542 attr == &dev_attr_group_tokens_reserved.attr ||
543 attr == &dev_attr_group_read_buffers_reserved.attr) &&
544 idxd->data->type == IDXD_TYPE_IAX;
545}
546
1f273752
DJ
547static umode_t idxd_group_attr_visible(struct kobject *kobj,
548 struct attribute *attr, int n)
549{
550 struct device *dev = container_of(kobj, struct device, kobj);
551 struct idxd_group *group = confdev_to_group(dev);
552 struct idxd_device *idxd = group->idxd;
553
554 if (idxd_group_attr_progress_limit_invisible(attr, idxd))
555 return 0;
556
9a8ddb35
XS
557 if (idxd_group_attr_read_buffers_invisible(attr, idxd))
558 return 0;
559
1f273752
DJ
560 return attr->mode;
561}
562
c52ca478
DJ
563static const struct attribute_group idxd_group_attribute_group = {
564 .attrs = idxd_group_attributes,
1f273752 565 .is_visible = idxd_group_attr_visible,
c52ca478
DJ
566};
567
568static const struct attribute_group *idxd_group_attribute_groups[] = {
569 &idxd_group_attribute_group,
570 NULL,
571};
572
defe49f9
DJ
573static void idxd_conf_group_release(struct device *dev)
574{
700af3a0 575 struct idxd_group *group = confdev_to_group(dev);
defe49f9
DJ
576
577 kfree(group);
578}
579
580struct device_type idxd_group_device_type = {
581 .name = "group",
582 .release = idxd_conf_group_release,
583 .groups = idxd_group_attribute_groups,
584};
585
c52ca478
DJ
586/* IDXD work queue attribs */
587static ssize_t wq_clients_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
589{
700af3a0 590 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478 591
8241571f 592 return sysfs_emit(buf, "%d\n", wq->client_count);
c52ca478
DJ
593}
594
595static struct device_attribute dev_attr_wq_clients =
596 __ATTR(clients, 0444, wq_clients_show, NULL);
597
598static ssize_t wq_state_show(struct device *dev,
599 struct device_attribute *attr, char *buf)
600{
700af3a0 601 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478
DJ
602
603 switch (wq->state) {
604 case IDXD_WQ_DISABLED:
8241571f 605 return sysfs_emit(buf, "disabled\n");
c52ca478 606 case IDXD_WQ_ENABLED:
8241571f 607 return sysfs_emit(buf, "enabled\n");
c52ca478
DJ
608 }
609
8241571f 610 return sysfs_emit(buf, "unknown\n");
c52ca478
DJ
611}
612
613static struct device_attribute dev_attr_wq_state =
614 __ATTR(state, 0444, wq_state_show, NULL);
615
616static ssize_t wq_group_id_show(struct device *dev,
617 struct device_attribute *attr, char *buf)
618{
700af3a0 619 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478
DJ
620
621 if (wq->group)
8241571f 622 return sysfs_emit(buf, "%u\n", wq->group->id);
c52ca478 623 else
8241571f 624 return sysfs_emit(buf, "-1\n");
c52ca478
DJ
625}
626
627static ssize_t wq_group_id_store(struct device *dev,
628 struct device_attribute *attr,
629 const char *buf, size_t count)
630{
700af3a0 631 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478
DJ
632 struct idxd_device *idxd = wq->idxd;
633 long id;
634 int rc;
635 struct idxd_group *prevg, *group;
636
637 rc = kstrtol(buf, 10, &id);
638 if (rc < 0)
639 return -EINVAL;
640
641 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
642 return -EPERM;
643
644 if (wq->state != IDXD_WQ_DISABLED)
645 return -EPERM;
646
647 if (id > idxd->max_groups - 1 || id < -1)
648 return -EINVAL;
649
650 if (id == -1) {
651 if (wq->group) {
652 wq->group->num_wqs--;
653 wq->group = NULL;
654 }
655 return count;
656 }
657
defe49f9 658 group = idxd->groups[id];
c52ca478
DJ
659 prevg = wq->group;
660
661 if (prevg)
662 prevg->num_wqs--;
663 wq->group = group;
664 group->num_wqs++;
665 return count;
666}
667
668static struct device_attribute dev_attr_wq_group_id =
669 __ATTR(group_id, 0644, wq_group_id_show, wq_group_id_store);
670
671static ssize_t wq_mode_show(struct device *dev, struct device_attribute *attr,
672 char *buf)
673{
700af3a0 674 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478 675
8241571f 676 return sysfs_emit(buf, "%s\n", wq_dedicated(wq) ? "dedicated" : "shared");
c52ca478
DJ
677}
678
679static ssize_t wq_mode_store(struct device *dev,
680 struct device_attribute *attr, const char *buf,
681 size_t count)
682{
700af3a0 683 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478
DJ
684 struct idxd_device *idxd = wq->idxd;
685
686 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
687 return -EPERM;
688
689 if (wq->state != IDXD_WQ_DISABLED)
690 return -EPERM;
691
692 if (sysfs_streq(buf, "dedicated")) {
693 set_bit(WQ_FLAG_DEDICATED, &wq->flags);
694 wq->threshold = 0;
42a1b738 695 } else if (sysfs_streq(buf, "shared")) {
8e50d392 696 clear_bit(WQ_FLAG_DEDICATED, &wq->flags);
c52ca478
DJ
697 } else {
698 return -EINVAL;
699 }
700
701 return count;
702}
703
704static struct device_attribute dev_attr_wq_mode =
705 __ATTR(mode, 0644, wq_mode_show, wq_mode_store);
706
707static ssize_t wq_size_show(struct device *dev, struct device_attribute *attr,
708 char *buf)
709{
700af3a0 710 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478 711
8241571f 712 return sysfs_emit(buf, "%u\n", wq->size);
c52ca478
DJ
713}
714
50e7e7f6
DJ
715static int total_claimed_wq_size(struct idxd_device *idxd)
716{
717 int i;
718 int wq_size = 0;
719
720 for (i = 0; i < idxd->max_wqs; i++) {
7c5dd23e 721 struct idxd_wq *wq = idxd->wqs[i];
50e7e7f6
DJ
722
723 wq_size += wq->size;
724 }
725
726 return wq_size;
727}
728
c52ca478
DJ
729static ssize_t wq_size_store(struct device *dev,
730 struct device_attribute *attr, const char *buf,
731 size_t count)
732{
700af3a0 733 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478
DJ
734 unsigned long size;
735 struct idxd_device *idxd = wq->idxd;
736 int rc;
737
738 rc = kstrtoul(buf, 10, &size);
739 if (rc < 0)
740 return -EINVAL;
741
742 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
743 return -EPERM;
744
0fff71c5 745 if (idxd->state == IDXD_DEV_ENABLED)
c52ca478
DJ
746 return -EPERM;
747
50e7e7f6 748 if (size + total_claimed_wq_size(idxd) - wq->size > idxd->max_wq_size)
c52ca478
DJ
749 return -EINVAL;
750
751 wq->size = size;
752 return count;
753}
754
755static struct device_attribute dev_attr_wq_size =
756 __ATTR(size, 0644, wq_size_show, wq_size_store);
757
758static ssize_t wq_priority_show(struct device *dev,
759 struct device_attribute *attr, char *buf)
760{
700af3a0 761 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478 762
8241571f 763 return sysfs_emit(buf, "%u\n", wq->priority);
c52ca478
DJ
764}
765
766static ssize_t wq_priority_store(struct device *dev,
767 struct device_attribute *attr,
768 const char *buf, size_t count)
769{
700af3a0 770 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478
DJ
771 unsigned long prio;
772 struct idxd_device *idxd = wq->idxd;
773 int rc;
774
775 rc = kstrtoul(buf, 10, &prio);
776 if (rc < 0)
777 return -EINVAL;
778
779 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
780 return -EPERM;
781
782 if (wq->state != IDXD_WQ_DISABLED)
783 return -EPERM;
784
785 if (prio > IDXD_MAX_PRIORITY)
786 return -EINVAL;
787
788 wq->priority = prio;
789 return count;
790}
791
792static struct device_attribute dev_attr_wq_priority =
793 __ATTR(priority, 0644, wq_priority_show, wq_priority_store);
794
8e50d392
DJ
795static ssize_t wq_block_on_fault_show(struct device *dev,
796 struct device_attribute *attr, char *buf)
797{
700af3a0 798 struct idxd_wq *wq = confdev_to_wq(dev);
8e50d392 799
8241571f 800 return sysfs_emit(buf, "%u\n", test_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags));
8e50d392
DJ
801}
802
803static ssize_t wq_block_on_fault_store(struct device *dev,
804 struct device_attribute *attr,
805 const char *buf, size_t count)
806{
700af3a0 807 struct idxd_wq *wq = confdev_to_wq(dev);
8e50d392
DJ
808 struct idxd_device *idxd = wq->idxd;
809 bool bof;
810 int rc;
811
81c2f79c
DJ
812 if (!idxd->hw.gen_cap.block_on_fault)
813 return -EOPNOTSUPP;
814
8e50d392
DJ
815 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
816 return -EPERM;
817
818 if (wq->state != IDXD_WQ_DISABLED)
819 return -ENXIO;
820
821 rc = kstrtobool(buf, &bof);
822 if (rc < 0)
823 return rc;
824
825 if (bof)
826 set_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
827 else
828 clear_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
829
830 return count;
831}
832
833static struct device_attribute dev_attr_wq_block_on_fault =
834 __ATTR(block_on_fault, 0644, wq_block_on_fault_show,
835 wq_block_on_fault_store);
836
837static ssize_t wq_threshold_show(struct device *dev,
838 struct device_attribute *attr, char *buf)
839{
700af3a0 840 struct idxd_wq *wq = confdev_to_wq(dev);
8e50d392 841
8241571f 842 return sysfs_emit(buf, "%u\n", wq->threshold);
8e50d392
DJ
843}
844
845static ssize_t wq_threshold_store(struct device *dev,
846 struct device_attribute *attr,
847 const char *buf, size_t count)
848{
700af3a0 849 struct idxd_wq *wq = confdev_to_wq(dev);
8e50d392
DJ
850 struct idxd_device *idxd = wq->idxd;
851 unsigned int val;
852 int rc;
853
854 rc = kstrtouint(buf, 0, &val);
855 if (rc < 0)
856 return -EINVAL;
857
858 if (val > wq->size || val <= 0)
859 return -EINVAL;
860
861 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
862 return -EPERM;
863
864 if (wq->state != IDXD_WQ_DISABLED)
865 return -ENXIO;
866
867 if (test_bit(WQ_FLAG_DEDICATED, &wq->flags))
868 return -EINVAL;
869
870 wq->threshold = val;
871
872 return count;
873}
874
875static struct device_attribute dev_attr_wq_threshold =
876 __ATTR(threshold, 0644, wq_threshold_show, wq_threshold_store);
877
c52ca478
DJ
878static ssize_t wq_type_show(struct device *dev,
879 struct device_attribute *attr, char *buf)
880{
700af3a0 881 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478
DJ
882
883 switch (wq->type) {
884 case IDXD_WQT_KERNEL:
8241571f 885 return sysfs_emit(buf, "%s\n", idxd_wq_type_names[IDXD_WQT_KERNEL]);
42d279f9 886 case IDXD_WQT_USER:
8241571f 887 return sysfs_emit(buf, "%s\n", idxd_wq_type_names[IDXD_WQT_USER]);
c52ca478
DJ
888 case IDXD_WQT_NONE:
889 default:
8241571f 890 return sysfs_emit(buf, "%s\n", idxd_wq_type_names[IDXD_WQT_NONE]);
c52ca478
DJ
891 }
892
893 return -EINVAL;
894}
895
896static ssize_t wq_type_store(struct device *dev,
897 struct device_attribute *attr, const char *buf,
898 size_t count)
899{
700af3a0 900 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478
DJ
901 enum idxd_wq_type old_type;
902
903 if (wq->state != IDXD_WQ_DISABLED)
904 return -EPERM;
905
906 old_type = wq->type;
88402c5b
DJ
907 if (sysfs_streq(buf, idxd_wq_type_names[IDXD_WQT_NONE]))
908 wq->type = IDXD_WQT_NONE;
909 else if (sysfs_streq(buf, idxd_wq_type_names[IDXD_WQT_KERNEL]))
c52ca478 910 wq->type = IDXD_WQT_KERNEL;
42d279f9
DJ
911 else if (sysfs_streq(buf, idxd_wq_type_names[IDXD_WQT_USER]))
912 wq->type = IDXD_WQT_USER;
c52ca478 913 else
88402c5b 914 return -EINVAL;
c52ca478
DJ
915
916 /* If we are changing queue type, clear the name */
917 if (wq->type != old_type)
918 memset(wq->name, 0, WQ_NAME_SIZE + 1);
919
920 return count;
921}
922
923static struct device_attribute dev_attr_wq_type =
924 __ATTR(type, 0644, wq_type_show, wq_type_store);
925
926static ssize_t wq_name_show(struct device *dev,
927 struct device_attribute *attr, char *buf)
928{
700af3a0 929 struct idxd_wq *wq = confdev_to_wq(dev);
c52ca478 930
8241571f 931 return sysfs_emit(buf, "%s\n", wq->name);
c52ca478
DJ
932}
933
934static ssize_t wq_name_store(struct device *dev,
935 struct device_attribute *attr, const char *buf,
936 size_t count)
937{
700af3a0 938 struct idxd_wq *wq = confdev_to_wq(dev);
81f5eb2b 939 char *input, *pos;
c52ca478
DJ
940
941 if (wq->state != IDXD_WQ_DISABLED)
942 return -EPERM;
943
944 if (strlen(buf) > WQ_NAME_SIZE || strlen(buf) == 0)
945 return -EINVAL;
946
8e50d392
DJ
947 /*
948 * This is temporarily placed here until we have SVM support for
949 * dmaengine.
950 */
951 if (wq->type == IDXD_WQT_KERNEL && device_pasid_enabled(wq->idxd))
952 return -EOPNOTSUPP;
953
81f5eb2b
DJ
954 input = kstrndup(buf, count, GFP_KERNEL);
955 if (!input)
956 return -ENOMEM;
957
958 pos = strim(input);
c52ca478 959 memset(wq->name, 0, WQ_NAME_SIZE + 1);
81f5eb2b
DJ
960 sprintf(wq->name, "%s", pos);
961 kfree(input);
c52ca478
DJ
962 return count;
963}
964
965static struct device_attribute dev_attr_wq_name =
966 __ATTR(name, 0644, wq_name_show, wq_name_store);
967
42d279f9
DJ
968static ssize_t wq_cdev_minor_show(struct device *dev,
969 struct device_attribute *attr, char *buf)
970{
700af3a0 971 struct idxd_wq *wq = confdev_to_wq(dev);
04922b74 972 int minor = -1;
42d279f9 973
04922b74
DJ
974 mutex_lock(&wq->wq_lock);
975 if (wq->idxd_cdev)
976 minor = wq->idxd_cdev->minor;
977 mutex_unlock(&wq->wq_lock);
978
979 if (minor == -1)
980 return -ENXIO;
981 return sysfs_emit(buf, "%d\n", minor);
42d279f9
DJ
982}
983
984static struct device_attribute dev_attr_wq_cdev_minor =
985 __ATTR(cdev_minor, 0444, wq_cdev_minor_show, NULL);
986
e7184b15
DJ
987static int __get_sysfs_u64(const char *buf, u64 *val)
988{
989 int rc;
990
991 rc = kstrtou64(buf, 0, val);
992 if (rc < 0)
993 return -EINVAL;
994
995 if (*val == 0)
996 return -EINVAL;
997
998 *val = roundup_pow_of_two(*val);
999 return 0;
1000}
1001
d7aad555
DJ
1002static ssize_t wq_max_transfer_size_show(struct device *dev, struct device_attribute *attr,
1003 char *buf)
1004{
700af3a0 1005 struct idxd_wq *wq = confdev_to_wq(dev);
d7aad555 1006
8241571f 1007 return sysfs_emit(buf, "%llu\n", wq->max_xfer_bytes);
d7aad555
DJ
1008}
1009
1010static ssize_t wq_max_transfer_size_store(struct device *dev, struct device_attribute *attr,
1011 const char *buf, size_t count)
1012{
700af3a0 1013 struct idxd_wq *wq = confdev_to_wq(dev);
d7aad555
DJ
1014 struct idxd_device *idxd = wq->idxd;
1015 u64 xfer_size;
1016 int rc;
1017
505a2d10
DJ
1018 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1019 return -EPERM;
1020
d7aad555
DJ
1021 if (wq->state != IDXD_WQ_DISABLED)
1022 return -EPERM;
1023
e7184b15 1024 rc = __get_sysfs_u64(buf, &xfer_size);
d7aad555 1025 if (rc < 0)
e7184b15 1026 return rc;
d7aad555 1027
d7aad555
DJ
1028 if (xfer_size > idxd->max_xfer_bytes)
1029 return -EINVAL;
1030
1031 wq->max_xfer_bytes = xfer_size;
1032
1033 return count;
1034}
1035
1036static struct device_attribute dev_attr_wq_max_transfer_size =
1037 __ATTR(max_transfer_size, 0644,
1038 wq_max_transfer_size_show, wq_max_transfer_size_store);
1039
e7184b15
DJ
1040static ssize_t wq_max_batch_size_show(struct device *dev, struct device_attribute *attr, char *buf)
1041{
700af3a0 1042 struct idxd_wq *wq = confdev_to_wq(dev);
e7184b15 1043
8241571f 1044 return sysfs_emit(buf, "%u\n", wq->max_batch_size);
e7184b15
DJ
1045}
1046
1047static ssize_t wq_max_batch_size_store(struct device *dev, struct device_attribute *attr,
1048 const char *buf, size_t count)
1049{
700af3a0 1050 struct idxd_wq *wq = confdev_to_wq(dev);
e7184b15
DJ
1051 struct idxd_device *idxd = wq->idxd;
1052 u64 batch_size;
1053 int rc;
1054
66903461
DJ
1055 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1056 return -EPERM;
1057
e7184b15
DJ
1058 if (wq->state != IDXD_WQ_DISABLED)
1059 return -EPERM;
1060
1061 rc = __get_sysfs_u64(buf, &batch_size);
1062 if (rc < 0)
1063 return rc;
1064
1065 if (batch_size > idxd->max_batch_size)
1066 return -EINVAL;
1067
e8dbd644 1068 idxd_wq_set_max_batch_size(idxd->data->type, wq, (u32)batch_size);
e7184b15
DJ
1069
1070 return count;
1071}
1072
1073static struct device_attribute dev_attr_wq_max_batch_size =
1074 __ATTR(max_batch_size, 0644, wq_max_batch_size_show, wq_max_batch_size_store);
1075
92de5fa2
DJ
1076static ssize_t wq_ats_disable_show(struct device *dev, struct device_attribute *attr, char *buf)
1077{
700af3a0 1078 struct idxd_wq *wq = confdev_to_wq(dev);
92de5fa2 1079
22bd0df8 1080 return sysfs_emit(buf, "%u\n", test_bit(WQ_FLAG_ATS_DISABLE, &wq->flags));
92de5fa2
DJ
1081}
1082
1083static ssize_t wq_ats_disable_store(struct device *dev, struct device_attribute *attr,
1084 const char *buf, size_t count)
1085{
700af3a0 1086 struct idxd_wq *wq = confdev_to_wq(dev);
92de5fa2
DJ
1087 struct idxd_device *idxd = wq->idxd;
1088 bool ats_dis;
1089 int rc;
1090
1091 if (wq->state != IDXD_WQ_DISABLED)
1092 return -EPERM;
1093
1094 if (!idxd->hw.wq_cap.wq_ats_support)
1095 return -EOPNOTSUPP;
1096
1097 rc = kstrtobool(buf, &ats_dis);
1098 if (rc < 0)
1099 return rc;
1100
22bd0df8
DJ
1101 if (ats_dis)
1102 set_bit(WQ_FLAG_ATS_DISABLE, &wq->flags);
1103 else
1104 clear_bit(WQ_FLAG_ATS_DISABLE, &wq->flags);
92de5fa2
DJ
1105
1106 return count;
1107}
1108
1109static struct device_attribute dev_attr_wq_ats_disable =
1110 __ATTR(ats_disable, 0644, wq_ats_disable_show, wq_ats_disable_store);
1111
e753a64b
DJ
1112static ssize_t wq_occupancy_show(struct device *dev, struct device_attribute *attr, char *buf)
1113{
1114 struct idxd_wq *wq = confdev_to_wq(dev);
1115 struct idxd_device *idxd = wq->idxd;
1116 u32 occup, offset;
1117
1118 if (!idxd->hw.wq_cap.occupancy)
1119 return -EOPNOTSUPP;
1120
1121 offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_OCCUP_IDX);
1122 occup = ioread32(idxd->reg_base + offset) & WQCFG_OCCUP_MASK;
1123
1124 return sysfs_emit(buf, "%u\n", occup);
1125}
1126
1127static struct device_attribute dev_attr_wq_occupancy =
1128 __ATTR(occupancy, 0444, wq_occupancy_show, NULL);
1129
7930d855
DJ
1130static ssize_t wq_enqcmds_retries_show(struct device *dev,
1131 struct device_attribute *attr, char *buf)
1132{
1133 struct idxd_wq *wq = confdev_to_wq(dev);
1134
1135 if (wq_dedicated(wq))
1136 return -EOPNOTSUPP;
1137
1138 return sysfs_emit(buf, "%u\n", wq->enqcmds_retries);
1139}
1140
1141static ssize_t wq_enqcmds_retries_store(struct device *dev, struct device_attribute *attr,
1142 const char *buf, size_t count)
1143{
1144 struct idxd_wq *wq = confdev_to_wq(dev);
1145 int rc;
1146 unsigned int retries;
1147
1148 if (wq_dedicated(wq))
1149 return -EOPNOTSUPP;
1150
1151 rc = kstrtouint(buf, 10, &retries);
1152 if (rc < 0)
1153 return rc;
1154
1155 if (retries > IDXD_ENQCMDS_MAX_RETRIES)
1156 retries = IDXD_ENQCMDS_MAX_RETRIES;
1157
1158 wq->enqcmds_retries = retries;
1159 return count;
1160}
1161
1162static struct device_attribute dev_attr_wq_enqcmds_retries =
1163 __ATTR(enqcmds_retries, 0644, wq_enqcmds_retries_show, wq_enqcmds_retries_store);
1164
b0325aef
DJ
1165static ssize_t wq_op_config_show(struct device *dev,
1166 struct device_attribute *attr, char *buf)
1167{
1168 struct idxd_wq *wq = confdev_to_wq(dev);
1169
1170 return sysfs_emit(buf, "%*pb\n", IDXD_MAX_OPCAP_BITS, wq->opcap_bmap);
1171}
1172
1173static int idxd_verify_supported_opcap(struct idxd_device *idxd, unsigned long *opmask)
1174{
1175 int bit;
1176
1177 /*
1178 * The OPCAP is defined as 256 bits that represents each operation the device
1179 * supports per bit. Iterate through all the bits and check if the input mask
1180 * is set for bits that are not set in the OPCAP for the device. If no OPCAP
1181 * bit is set and input mask has the bit set, then return error.
1182 */
1183 for_each_set_bit(bit, opmask, IDXD_MAX_OPCAP_BITS) {
1184 if (!test_bit(bit, idxd->opcap_bmap))
1185 return -EINVAL;
1186 }
1187
1188 return 0;
1189}
1190
1191static ssize_t wq_op_config_store(struct device *dev, struct device_attribute *attr,
1192 const char *buf, size_t count)
1193{
1194 struct idxd_wq *wq = confdev_to_wq(dev);
1195 struct idxd_device *idxd = wq->idxd;
1196 unsigned long *opmask;
1197 int rc;
1198
1199 if (wq->state != IDXD_WQ_DISABLED)
1200 return -EPERM;
1201
1202 opmask = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
1203 if (!opmask)
1204 return -ENOMEM;
1205
1206 rc = bitmap_parse(buf, count, opmask, IDXD_MAX_OPCAP_BITS);
1207 if (rc < 0)
1208 goto err;
1209
1210 rc = idxd_verify_supported_opcap(idxd, opmask);
1211 if (rc < 0)
1212 goto err;
1213
1214 bitmap_copy(wq->opcap_bmap, opmask, IDXD_MAX_OPCAP_BITS);
1215
1216 bitmap_free(opmask);
1217 return count;
1218
1219err:
1220 bitmap_free(opmask);
1221 return rc;
1222}
1223
1224static struct device_attribute dev_attr_wq_op_config =
1225 __ATTR(op_config, 0644, wq_op_config_show, wq_op_config_store);
1226
c52ca478
DJ
1227static struct attribute *idxd_wq_attributes[] = {
1228 &dev_attr_wq_clients.attr,
1229 &dev_attr_wq_state.attr,
1230 &dev_attr_wq_group_id.attr,
1231 &dev_attr_wq_mode.attr,
1232 &dev_attr_wq_size.attr,
1233 &dev_attr_wq_priority.attr,
8e50d392
DJ
1234 &dev_attr_wq_block_on_fault.attr,
1235 &dev_attr_wq_threshold.attr,
c52ca478
DJ
1236 &dev_attr_wq_type.attr,
1237 &dev_attr_wq_name.attr,
42d279f9 1238 &dev_attr_wq_cdev_minor.attr,
d7aad555 1239 &dev_attr_wq_max_transfer_size.attr,
e7184b15 1240 &dev_attr_wq_max_batch_size.attr,
92de5fa2 1241 &dev_attr_wq_ats_disable.attr,
e753a64b 1242 &dev_attr_wq_occupancy.attr,
7930d855 1243 &dev_attr_wq_enqcmds_retries.attr,
b0325aef 1244 &dev_attr_wq_op_config.attr,
c52ca478
DJ
1245 NULL,
1246};
1247
b0325aef
DJ
1248static bool idxd_wq_attr_op_config_invisible(struct attribute *attr,
1249 struct idxd_device *idxd)
1250{
1251 return attr == &dev_attr_wq_op_config.attr &&
1252 !idxd->hw.wq_cap.op_config;
1253}
1254
91123b37
XS
1255static bool idxd_wq_attr_max_batch_size_invisible(struct attribute *attr,
1256 struct idxd_device *idxd)
1257{
1258 /* Intel IAA does not support batch processing, make it invisible */
1259 return attr == &dev_attr_wq_max_batch_size.attr &&
1260 idxd->data->type == IDXD_TYPE_IAX;
1261}
1262
b0325aef
DJ
1263static umode_t idxd_wq_attr_visible(struct kobject *kobj,
1264 struct attribute *attr, int n)
1265{
1266 struct device *dev = container_of(kobj, struct device, kobj);
1267 struct idxd_wq *wq = confdev_to_wq(dev);
1268 struct idxd_device *idxd = wq->idxd;
1269
1270 if (idxd_wq_attr_op_config_invisible(attr, idxd))
1271 return 0;
1272
91123b37
XS
1273 if (idxd_wq_attr_max_batch_size_invisible(attr, idxd))
1274 return 0;
1275
b0325aef
DJ
1276 return attr->mode;
1277}
1278
c52ca478
DJ
1279static const struct attribute_group idxd_wq_attribute_group = {
1280 .attrs = idxd_wq_attributes,
b0325aef 1281 .is_visible = idxd_wq_attr_visible,
c52ca478
DJ
1282};
1283
1284static const struct attribute_group *idxd_wq_attribute_groups[] = {
1285 &idxd_wq_attribute_group,
1286 NULL,
1287};
1288
7c5dd23e
DJ
1289static void idxd_conf_wq_release(struct device *dev)
1290{
700af3a0 1291 struct idxd_wq *wq = confdev_to_wq(dev);
7c5dd23e 1292
b0325aef 1293 bitmap_free(wq->opcap_bmap);
7c5dd23e
DJ
1294 kfree(wq->wqcfg);
1295 kfree(wq);
1296}
1297
1298struct device_type idxd_wq_device_type = {
1299 .name = "wq",
1300 .release = idxd_conf_wq_release,
1301 .groups = idxd_wq_attribute_groups,
1302};
1303
c52ca478 1304/* IDXD device attribs */
c2ce6bbc
DJ
1305static ssize_t version_show(struct device *dev, struct device_attribute *attr,
1306 char *buf)
1307{
700af3a0 1308 struct idxd_device *idxd = confdev_to_idxd(dev);
c2ce6bbc 1309
8241571f 1310 return sysfs_emit(buf, "%#x\n", idxd->hw.version);
c2ce6bbc
DJ
1311}
1312static DEVICE_ATTR_RO(version);
1313
c52ca478
DJ
1314static ssize_t max_work_queues_size_show(struct device *dev,
1315 struct device_attribute *attr,
1316 char *buf)
1317{
700af3a0 1318 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1319
8241571f 1320 return sysfs_emit(buf, "%u\n", idxd->max_wq_size);
c52ca478
DJ
1321}
1322static DEVICE_ATTR_RO(max_work_queues_size);
1323
1324static ssize_t max_groups_show(struct device *dev,
1325 struct device_attribute *attr, char *buf)
1326{
700af3a0 1327 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1328
8241571f 1329 return sysfs_emit(buf, "%u\n", idxd->max_groups);
c52ca478
DJ
1330}
1331static DEVICE_ATTR_RO(max_groups);
1332
1333static ssize_t max_work_queues_show(struct device *dev,
1334 struct device_attribute *attr, char *buf)
1335{
700af3a0 1336 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1337
8241571f 1338 return sysfs_emit(buf, "%u\n", idxd->max_wqs);
c52ca478
DJ
1339}
1340static DEVICE_ATTR_RO(max_work_queues);
1341
1342static ssize_t max_engines_show(struct device *dev,
1343 struct device_attribute *attr, char *buf)
1344{
700af3a0 1345 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1346
8241571f 1347 return sysfs_emit(buf, "%u\n", idxd->max_engines);
c52ca478
DJ
1348}
1349static DEVICE_ATTR_RO(max_engines);
1350
1351static ssize_t numa_node_show(struct device *dev,
1352 struct device_attribute *attr, char *buf)
1353{
700af3a0 1354 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1355
8241571f 1356 return sysfs_emit(buf, "%d\n", dev_to_node(&idxd->pdev->dev));
c52ca478
DJ
1357}
1358static DEVICE_ATTR_RO(numa_node);
1359
1360static ssize_t max_batch_size_show(struct device *dev,
1361 struct device_attribute *attr, char *buf)
1362{
700af3a0 1363 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1364
8241571f 1365 return sysfs_emit(buf, "%u\n", idxd->max_batch_size);
c52ca478
DJ
1366}
1367static DEVICE_ATTR_RO(max_batch_size);
1368
1369static ssize_t max_transfer_size_show(struct device *dev,
1370 struct device_attribute *attr,
1371 char *buf)
1372{
700af3a0 1373 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1374
8241571f 1375 return sysfs_emit(buf, "%llu\n", idxd->max_xfer_bytes);
c52ca478
DJ
1376}
1377static DEVICE_ATTR_RO(max_transfer_size);
1378
1379static ssize_t op_cap_show(struct device *dev,
1380 struct device_attribute *attr, char *buf)
1381{
700af3a0 1382 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1383
a8563a33 1384 return sysfs_emit(buf, "%*pb\n", IDXD_MAX_OPCAP_BITS, idxd->opcap_bmap);
c52ca478
DJ
1385}
1386static DEVICE_ATTR_RO(op_cap);
1387
9065958e
DJ
1388static ssize_t gen_cap_show(struct device *dev,
1389 struct device_attribute *attr, char *buf)
1390{
700af3a0 1391 struct idxd_device *idxd = confdev_to_idxd(dev);
9065958e 1392
8241571f 1393 return sysfs_emit(buf, "%#llx\n", idxd->hw.gen_cap.bits);
9065958e
DJ
1394}
1395static DEVICE_ATTR_RO(gen_cap);
1396
c52ca478
DJ
1397static ssize_t configurable_show(struct device *dev,
1398 struct device_attribute *attr, char *buf)
1399{
700af3a0 1400 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1401
8241571f 1402 return sysfs_emit(buf, "%u\n", test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags));
c52ca478
DJ
1403}
1404static DEVICE_ATTR_RO(configurable);
1405
1406static ssize_t clients_show(struct device *dev,
1407 struct device_attribute *attr, char *buf)
1408{
700af3a0 1409 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478
DJ
1410 int count = 0, i;
1411
cf84a4b9 1412 spin_lock(&idxd->dev_lock);
c52ca478 1413 for (i = 0; i < idxd->max_wqs; i++) {
7c5dd23e 1414 struct idxd_wq *wq = idxd->wqs[i];
c52ca478
DJ
1415
1416 count += wq->client_count;
1417 }
cf84a4b9 1418 spin_unlock(&idxd->dev_lock);
c52ca478 1419
8241571f 1420 return sysfs_emit(buf, "%d\n", count);
c52ca478
DJ
1421}
1422static DEVICE_ATTR_RO(clients);
1423
8e50d392
DJ
1424static ssize_t pasid_enabled_show(struct device *dev,
1425 struct device_attribute *attr, char *buf)
1426{
700af3a0 1427 struct idxd_device *idxd = confdev_to_idxd(dev);
8e50d392 1428
8241571f 1429 return sysfs_emit(buf, "%u\n", device_pasid_enabled(idxd));
8e50d392
DJ
1430}
1431static DEVICE_ATTR_RO(pasid_enabled);
1432
c52ca478
DJ
1433static ssize_t state_show(struct device *dev,
1434 struct device_attribute *attr, char *buf)
1435{
700af3a0 1436 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478
DJ
1437
1438 switch (idxd->state) {
1439 case IDXD_DEV_DISABLED:
8241571f 1440 return sysfs_emit(buf, "disabled\n");
c52ca478 1441 case IDXD_DEV_ENABLED:
8241571f 1442 return sysfs_emit(buf, "enabled\n");
c52ca478 1443 case IDXD_DEV_HALTED:
8241571f 1444 return sysfs_emit(buf, "halted\n");
c52ca478
DJ
1445 }
1446
8241571f 1447 return sysfs_emit(buf, "unknown\n");
c52ca478
DJ
1448}
1449static DEVICE_ATTR_RO(state);
1450
1451static ssize_t errors_show(struct device *dev,
1452 struct device_attribute *attr, char *buf)
1453{
700af3a0 1454 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1455 int i, out = 0;
c52ca478 1456
cf84a4b9 1457 spin_lock(&idxd->dev_lock);
c52ca478 1458 for (i = 0; i < 4; i++)
8241571f 1459 out += sysfs_emit_at(buf, out, "%#018llx ", idxd->sw_err.bits[i]);
cf84a4b9 1460 spin_unlock(&idxd->dev_lock);
c52ca478 1461 out--;
8241571f 1462 out += sysfs_emit_at(buf, out, "\n");
c52ca478
DJ
1463 return out;
1464}
1465static DEVICE_ATTR_RO(errors);
1466
fde212e4
DJ
1467static ssize_t max_read_buffers_show(struct device *dev,
1468 struct device_attribute *attr, char *buf)
c52ca478 1469{
700af3a0 1470 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1471
7ed6f1b8 1472 return sysfs_emit(buf, "%u\n", idxd->max_rdbufs);
c52ca478 1473}
c52ca478 1474
fde212e4
DJ
1475static ssize_t max_tokens_show(struct device *dev,
1476 struct device_attribute *attr, char *buf)
1477{
1478 dev_warn_once(dev, "attribute deprecated, see max_read_buffers.\n");
1479 return max_read_buffers_show(dev, attr, buf);
1480}
1481
1482static DEVICE_ATTR_RO(max_tokens); /* deprecated */
1483static DEVICE_ATTR_RO(max_read_buffers);
1484
1485static ssize_t read_buffer_limit_show(struct device *dev,
1486 struct device_attribute *attr, char *buf)
c52ca478 1487{
700af3a0 1488 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478 1489
7ed6f1b8 1490 return sysfs_emit(buf, "%u\n", idxd->rdbuf_limit);
c52ca478
DJ
1491}
1492
fde212e4
DJ
1493static ssize_t token_limit_show(struct device *dev,
1494 struct device_attribute *attr, char *buf)
1495{
1496 dev_warn_once(dev, "attribute deprecated, see read_buffer_limit.\n");
1497 return read_buffer_limit_show(dev, attr, buf);
1498}
1499
1500static ssize_t read_buffer_limit_store(struct device *dev,
1501 struct device_attribute *attr,
1502 const char *buf, size_t count)
c52ca478 1503{
700af3a0 1504 struct idxd_device *idxd = confdev_to_idxd(dev);
c52ca478
DJ
1505 unsigned long val;
1506 int rc;
1507
1508 rc = kstrtoul(buf, 10, &val);
1509 if (rc < 0)
1510 return -EINVAL;
1511
1512 if (idxd->state == IDXD_DEV_ENABLED)
1513 return -EPERM;
1514
1515 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1516 return -EPERM;
1517
7ed6f1b8 1518 if (!idxd->hw.group_cap.rdbuf_limit)
c52ca478
DJ
1519 return -EPERM;
1520
7ed6f1b8 1521 if (val > idxd->hw.group_cap.total_rdbufs)
c52ca478
DJ
1522 return -EINVAL;
1523
7ed6f1b8 1524 idxd->rdbuf_limit = val;
c52ca478
DJ
1525 return count;
1526}
fde212e4
DJ
1527
1528static ssize_t token_limit_store(struct device *dev,
1529 struct device_attribute *attr,
1530 const char *buf, size_t count)
1531{
1532 dev_warn_once(dev, "attribute deprecated, see read_buffer_limit\n");
1533 return read_buffer_limit_store(dev, attr, buf, count);
1534}
1535
1536static DEVICE_ATTR_RW(token_limit); /* deprecated */
1537static DEVICE_ATTR_RW(read_buffer_limit);
c52ca478 1538
42d279f9
DJ
1539static ssize_t cdev_major_show(struct device *dev,
1540 struct device_attribute *attr, char *buf)
1541{
700af3a0 1542 struct idxd_device *idxd = confdev_to_idxd(dev);
42d279f9 1543
8241571f 1544 return sysfs_emit(buf, "%u\n", idxd->major);
42d279f9
DJ
1545}
1546static DEVICE_ATTR_RO(cdev_major);
1547
ff18de55
DJ
1548static ssize_t cmd_status_show(struct device *dev,
1549 struct device_attribute *attr, char *buf)
1550{
700af3a0 1551 struct idxd_device *idxd = confdev_to_idxd(dev);
ff18de55 1552
8241571f 1553 return sysfs_emit(buf, "%#x\n", idxd->cmd_status);
ff18de55 1554}
125d1037
DJ
1555
1556static ssize_t cmd_status_store(struct device *dev, struct device_attribute *attr,
1557 const char *buf, size_t count)
1558{
1559 struct idxd_device *idxd = confdev_to_idxd(dev);
1560
1561 idxd->cmd_status = 0;
1562 return count;
1563}
1564static DEVICE_ATTR_RW(cmd_status);
ff18de55 1565
91123b37
XS
1566static bool idxd_device_attr_max_batch_size_invisible(struct attribute *attr,
1567 struct idxd_device *idxd)
1568{
1569 /* Intel IAA does not support batch processing, make it invisible */
1570 return attr == &dev_attr_max_batch_size.attr &&
1571 idxd->data->type == IDXD_TYPE_IAX;
1572}
1573
9a8ddb35
XS
1574static bool idxd_device_attr_read_buffers_invisible(struct attribute *attr,
1575 struct idxd_device *idxd)
1576{
1577 /*
1578 * Intel IAA does not support Read Buffer allocation control,
1579 * make these attributes invisible.
1580 */
1581 return (attr == &dev_attr_max_tokens.attr ||
1582 attr == &dev_attr_max_read_buffers.attr ||
1583 attr == &dev_attr_token_limit.attr ||
1584 attr == &dev_attr_read_buffer_limit.attr) &&
1585 idxd->data->type == IDXD_TYPE_IAX;
1586}
1587
91123b37
XS
1588static umode_t idxd_device_attr_visible(struct kobject *kobj,
1589 struct attribute *attr, int n)
1590{
1591 struct device *dev = container_of(kobj, struct device, kobj);
1592 struct idxd_device *idxd = confdev_to_idxd(dev);
1593
1594 if (idxd_device_attr_max_batch_size_invisible(attr, idxd))
1595 return 0;
1596
9a8ddb35
XS
1597 if (idxd_device_attr_read_buffers_invisible(attr, idxd))
1598 return 0;
1599
91123b37
XS
1600 return attr->mode;
1601}
1602
c52ca478 1603static struct attribute *idxd_device_attributes[] = {
c2ce6bbc 1604 &dev_attr_version.attr,
c52ca478
DJ
1605 &dev_attr_max_groups.attr,
1606 &dev_attr_max_work_queues.attr,
1607 &dev_attr_max_work_queues_size.attr,
1608 &dev_attr_max_engines.attr,
1609 &dev_attr_numa_node.attr,
1610 &dev_attr_max_batch_size.attr,
1611 &dev_attr_max_transfer_size.attr,
1612 &dev_attr_op_cap.attr,
9065958e 1613 &dev_attr_gen_cap.attr,
c52ca478
DJ
1614 &dev_attr_configurable.attr,
1615 &dev_attr_clients.attr,
8e50d392 1616 &dev_attr_pasid_enabled.attr,
c52ca478
DJ
1617 &dev_attr_state.attr,
1618 &dev_attr_errors.attr,
1619 &dev_attr_max_tokens.attr,
fde212e4 1620 &dev_attr_max_read_buffers.attr,
c52ca478 1621 &dev_attr_token_limit.attr,
fde212e4 1622 &dev_attr_read_buffer_limit.attr,
42d279f9 1623 &dev_attr_cdev_major.attr,
ff18de55 1624 &dev_attr_cmd_status.attr,
c52ca478
DJ
1625 NULL,
1626};
1627
1628static const struct attribute_group idxd_device_attribute_group = {
1629 .attrs = idxd_device_attributes,
91123b37 1630 .is_visible = idxd_device_attr_visible,
c52ca478
DJ
1631};
1632
1633static const struct attribute_group *idxd_attribute_groups[] = {
1634 &idxd_device_attribute_group,
1635 NULL,
1636};
1637
47c16ac2
DJ
1638static void idxd_conf_device_release(struct device *dev)
1639{
700af3a0 1640 struct idxd_device *idxd = confdev_to_idxd(dev);
47c16ac2
DJ
1641
1642 kfree(idxd->groups);
de5819b9 1643 bitmap_free(idxd->wq_enable_map);
47c16ac2
DJ
1644 kfree(idxd->wqs);
1645 kfree(idxd->engines);
4b73e4eb 1646 ida_free(&idxd_ida, idxd->id);
a8563a33 1647 bitmap_free(idxd->opcap_bmap);
47c16ac2
DJ
1648 kfree(idxd);
1649}
1650
1651struct device_type dsa_device_type = {
1652 .name = "dsa",
1653 .release = idxd_conf_device_release,
1654 .groups = idxd_attribute_groups,
1655};
1656
1657struct device_type iax_device_type = {
1658 .name = "iax",
1659 .release = idxd_conf_device_release,
1660 .groups = idxd_attribute_groups,
1661};
1662
75b91130 1663static int idxd_register_engine_devices(struct idxd_device *idxd)
c52ca478 1664{
700af3a0 1665 struct idxd_engine *engine;
75b91130 1666 int i, j, rc;
c52ca478
DJ
1667
1668 for (i = 0; i < idxd->max_engines; i++) {
700af3a0
DJ
1669 engine = idxd->engines[i];
1670 rc = device_add(engine_confdev(engine));
75b91130 1671 if (rc < 0)
c52ca478 1672 goto cleanup;
c52ca478
DJ
1673 }
1674
1675 return 0;
1676
1677cleanup:
75b91130 1678 j = i - 1;
700af3a0
DJ
1679 for (; i < idxd->max_engines; i++) {
1680 engine = idxd->engines[i];
1681 put_device(engine_confdev(engine));
1682 }
c52ca478 1683
700af3a0
DJ
1684 while (j--) {
1685 engine = idxd->engines[j];
1686 device_unregister(engine_confdev(engine));
1687 }
c52ca478
DJ
1688 return rc;
1689}
1690
defe49f9 1691static int idxd_register_group_devices(struct idxd_device *idxd)
c52ca478 1692{
700af3a0 1693 struct idxd_group *group;
defe49f9 1694 int i, j, rc;
c52ca478
DJ
1695
1696 for (i = 0; i < idxd->max_groups; i++) {
700af3a0
DJ
1697 group = idxd->groups[i];
1698 rc = device_add(group_confdev(group));
defe49f9 1699 if (rc < 0)
c52ca478 1700 goto cleanup;
c52ca478
DJ
1701 }
1702
1703 return 0;
1704
1705cleanup:
defe49f9 1706 j = i - 1;
700af3a0
DJ
1707 for (; i < idxd->max_groups; i++) {
1708 group = idxd->groups[i];
1709 put_device(group_confdev(group));
1710 }
c52ca478 1711
700af3a0
DJ
1712 while (j--) {
1713 group = idxd->groups[j];
1714 device_unregister(group_confdev(group));
1715 }
c52ca478
DJ
1716 return rc;
1717}
1718
7c5dd23e 1719static int idxd_register_wq_devices(struct idxd_device *idxd)
c52ca478 1720{
700af3a0 1721 struct idxd_wq *wq;
7c5dd23e 1722 int i, rc, j;
c52ca478
DJ
1723
1724 for (i = 0; i < idxd->max_wqs; i++) {
700af3a0
DJ
1725 wq = idxd->wqs[i];
1726 rc = device_add(wq_confdev(wq));
7c5dd23e 1727 if (rc < 0)
c52ca478 1728 goto cleanup;
c52ca478
DJ
1729 }
1730
1731 return 0;
1732
1733cleanup:
7c5dd23e 1734 j = i - 1;
700af3a0
DJ
1735 for (; i < idxd->max_wqs; i++) {
1736 wq = idxd->wqs[i];
1737 put_device(wq_confdev(wq));
1738 }
c52ca478 1739
700af3a0
DJ
1740 while (j--) {
1741 wq = idxd->wqs[j];
1742 device_unregister(wq_confdev(wq));
1743 }
c52ca478
DJ
1744 return rc;
1745}
1746
47c16ac2 1747int idxd_register_devices(struct idxd_device *idxd)
c52ca478
DJ
1748{
1749 struct device *dev = &idxd->pdev->dev;
7c5dd23e 1750 int rc, i;
c52ca478 1751
700af3a0 1752 rc = device_add(idxd_confdev(idxd));
47c16ac2 1753 if (rc < 0)
c52ca478 1754 return rc;
c52ca478 1755
7c5dd23e 1756 rc = idxd_register_wq_devices(idxd);
c52ca478 1757 if (rc < 0) {
7c5dd23e
DJ
1758 dev_dbg(dev, "WQ devices registering failed: %d\n", rc);
1759 goto err_wq;
c52ca478
DJ
1760 }
1761
75b91130 1762 rc = idxd_register_engine_devices(idxd);
c52ca478 1763 if (rc < 0) {
75b91130
DJ
1764 dev_dbg(dev, "Engine devices registering failed: %d\n", rc);
1765 goto err_engine;
c52ca478
DJ
1766 }
1767
defe49f9 1768 rc = idxd_register_group_devices(idxd);
c52ca478 1769 if (rc < 0) {
defe49f9 1770 dev_dbg(dev, "Group device registering failed: %d\n", rc);
75b91130 1771 goto err_group;
c52ca478
DJ
1772 }
1773
1774 return 0;
7c5dd23e 1775
75b91130
DJ
1776 err_group:
1777 for (i = 0; i < idxd->max_engines; i++)
700af3a0 1778 device_unregister(engine_confdev(idxd->engines[i]));
75b91130 1779 err_engine:
7c5dd23e 1780 for (i = 0; i < idxd->max_wqs; i++)
700af3a0 1781 device_unregister(wq_confdev(idxd->wqs[i]));
7c5dd23e 1782 err_wq:
700af3a0 1783 device_del(idxd_confdev(idxd));
7c5dd23e 1784 return rc;
c52ca478
DJ
1785}
1786
47c16ac2 1787void idxd_unregister_devices(struct idxd_device *idxd)
c52ca478
DJ
1788{
1789 int i;
1790
1791 for (i = 0; i < idxd->max_wqs; i++) {
7c5dd23e 1792 struct idxd_wq *wq = idxd->wqs[i];
c52ca478 1793
700af3a0 1794 device_unregister(wq_confdev(wq));
c52ca478
DJ
1795 }
1796
1797 for (i = 0; i < idxd->max_engines; i++) {
75b91130 1798 struct idxd_engine *engine = idxd->engines[i];
c52ca478 1799
700af3a0 1800 device_unregister(engine_confdev(engine));
c52ca478
DJ
1801 }
1802
1803 for (i = 0; i < idxd->max_groups; i++) {
defe49f9 1804 struct idxd_group *group = idxd->groups[i];
c52ca478 1805
700af3a0 1806 device_unregister(group_confdev(group));
c52ca478 1807 }
c52ca478
DJ
1808}
1809
1810int idxd_register_bus_type(void)
1811{
4b73e4eb 1812 return bus_register(&dsa_bus_type);
c52ca478
DJ
1813}
1814
1815void idxd_unregister_bus_type(void)
1816{
4b73e4eb 1817 bus_unregister(&dsa_bus_type);
c52ca478 1818}