1/*
2 * CPU <-> hardware queue mapping helpers
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 */
6#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
17static int cpu_to_queue_index(struct blk_mq_queue_map *qmap,
18 unsigned int nr_queues, const int cpu)
19{
20 return qmap->queue_offset + (cpu % nr_queues);
21}
22
23static int get_first_sibling(unsigned int cpu)
24{
25 unsigned int ret;
26
27 ret = cpumask_first(topology_sibling_cpumask(cpu));
28 if (ret < nr_cpu_ids)
29 return ret;
30
31 return cpu;
32}
33
34int blk_mq_map_queues(struct blk_mq_queue_map *qmap)
35{
36 unsigned int *map = qmap->mq_map;
37 unsigned int nr_queues = qmap->nr_queues;
38 unsigned int cpu, first_sibling;
39
40 for_each_possible_cpu(cpu) {
41 /*
42 * First do sequential mapping between CPUs and queues.
43 * In case we still have CPUs to map, and we have some number of
44 * threads per cores then map sibling threads to the same queue for
45 * performace optimizations.
46 */
47 if (cpu < nr_queues) {
48 map[cpu] = cpu_to_queue_index(qmap, nr_queues, cpu);
49 } else {
50 first_sibling = get_first_sibling(cpu);
51 if (first_sibling == cpu)
52 map[cpu] = cpu_to_queue_index(qmap, nr_queues, cpu);
53 else
54 map[cpu] = map[first_sibling];
55 }
56 }
57
58 return 0;
59}
60EXPORT_SYMBOL_GPL(blk_mq_map_queues);
61
62/*
63 * We have no quick way of doing reverse lookups. This is only used at
64 * queue init time, so runtime isn't important.
65 */
66int blk_mq_hw_queue_to_node(struct blk_mq_queue_map *qmap, unsigned int index)
67{
68 int i;
69
70 for_each_possible_cpu(i) {
71 if (index == qmap->mq_map[i])
72 return local_memory_node(cpu_to_node(i));
73 }
74
75 return NUMA_NO_NODE;
76}
77