blob: 9f8cffc8a701ec24c87729b9ef4a1048cc7d205f [file] [log] [blame]
Jens Axboe75bb4622014-05-28 10:15:41 -06001/*
2 * CPU <-> hardware queue mapping helpers
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 */
Jens Axboe320ae512013-10-24 09:20:05 +01006#include <linux/kernel.h>
7#include <linux/threads.h>
8#include <linux/module.h>
9#include <linux/mm.h>
10#include <linux/smp.h>
11#include <linux/cpu.h>
12
13#include <linux/blk-mq.h>
14#include "blk.h"
15#include "blk-mq.h"
16
Linus Torvalds03ffbcd2017-07-03 16:50:31 -070017static int cpu_to_queue_index(unsigned int nr_queues, const int cpu)
Jens Axboe320ae512013-10-24 09:20:05 +010018{
Max Gurtovoyfe631452017-06-29 08:40:11 -060019 /*
Christoph Hellwig76451d72017-07-18 17:04:40 +020020 * Non present CPU will be mapped to queue index 0.
Max Gurtovoyfe631452017-06-29 08:40:11 -060021 */
Christoph Hellwig76451d72017-07-18 17:04:40 +020022 if (!cpu_present(cpu))
Max Gurtovoyfe631452017-06-29 08:40:11 -060023 return 0;
24 return cpu % nr_queues;
Jens Axboe320ae512013-10-24 09:20:05 +010025}
26
27static int get_first_sibling(unsigned int cpu)
28{
29 unsigned int ret;
30
Bartosz Golaszewski06931e62015-05-26 15:11:28 +020031 ret = cpumask_first(topology_sibling_cpumask(cpu));
Jens Axboe320ae512013-10-24 09:20:05 +010032 if (ret < nr_cpu_ids)
33 return ret;
34
35 return cpu;
36}
37
Christoph Hellwigda695ba2016-09-14 16:18:55 +020038int blk_mq_map_queues(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +010039{
Christoph Hellwigda695ba2016-09-14 16:18:55 +020040 unsigned int *map = set->mq_map;
41 unsigned int nr_queues = set->nr_hw_queues;
Max Gurtovoyfe631452017-06-29 08:40:11 -060042 unsigned int cpu, first_sibling;
Jens Axboe320ae512013-10-24 09:20:05 +010043
Max Gurtovoyfe631452017-06-29 08:40:11 -060044 for_each_possible_cpu(cpu) {
45 /*
46 * First do sequential mapping between CPUs and queues.
47 * In case we still have CPUs to map, and we have some number of
48 * threads per cores then map sibling threads to the same queue for
49 * performace optimizations.
50 */
51 if (cpu < nr_queues) {
Linus Torvalds03ffbcd2017-07-03 16:50:31 -070052 map[cpu] = cpu_to_queue_index(nr_queues, cpu);
Max Gurtovoyfe631452017-06-29 08:40:11 -060053 } else {
54 first_sibling = get_first_sibling(cpu);
55 if (first_sibling == cpu)
Linus Torvalds03ffbcd2017-07-03 16:50:31 -070056 map[cpu] = cpu_to_queue_index(nr_queues, cpu);
Max Gurtovoyfe631452017-06-29 08:40:11 -060057 else
58 map[cpu] = map[first_sibling];
59 }
Jens Axboe320ae512013-10-24 09:20:05 +010060 }
61
Jens Axboe320ae512013-10-24 09:20:05 +010062 return 0;
63}
Christoph Hellwig9e5a7e22016-11-01 08:12:47 -060064EXPORT_SYMBOL_GPL(blk_mq_map_queues);
Jens Axboe320ae512013-10-24 09:20:05 +010065
Jens Axboef14bbe72014-05-27 12:06:53 -060066/*
67 * We have no quick way of doing reverse lookups. This is only used at
68 * queue init time, so runtime isn't important.
69 */
70int blk_mq_hw_queue_to_node(unsigned int *mq_map, unsigned int index)
71{
72 int i;
73
74 for_each_possible_cpu(i) {
75 if (index == mq_map[i])
Raghavendra K Tbffed452015-12-02 16:59:05 +053076 return local_memory_node(cpu_to_node(i));
Jens Axboef14bbe72014-05-27 12:06:53 -060077 }
78
79 return NUMA_NO_NODE;
80}