dts: early mount efs
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / block / blk-mq-cpumap.c
CommitLineData
75bb4625
JA
1/*
2 * CPU <-> hardware queue mapping helpers
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 */
320ae51f
JA
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
320ae51f
JA
17static int cpu_to_queue_index(unsigned int nr_cpus, unsigned int nr_queues,
18 const int cpu)
19{
959f5f5b 20 return cpu * nr_queues / nr_cpus;
320ae51f
JA
21}
22
23static int get_first_sibling(unsigned int cpu)
24{
25 unsigned int ret;
26
06931e62 27 ret = cpumask_first(topology_sibling_cpumask(cpu));
320ae51f
JA
28 if (ret < nr_cpu_ids)
29 return ret;
30
31 return cpu;
32}
33
1cac41cb 34static int blk_mq_update_queue_map(unsigned int *map, unsigned int nr_queues,
5778322e 35 const struct cpumask *online_mask)
320ae51f
JA
36{
37 unsigned int i, nr_cpus, nr_uniq_cpus, queue, first_sibling;
38 cpumask_var_t cpus;
39
40 if (!alloc_cpumask_var(&cpus, GFP_ATOMIC))
41 return 1;
42
43 cpumask_clear(cpus);
44 nr_cpus = nr_uniq_cpus = 0;
5778322e 45 for_each_cpu(i, online_mask) {
320ae51f
JA
46 nr_cpus++;
47 first_sibling = get_first_sibling(i);
48 if (!cpumask_test_cpu(first_sibling, cpus))
49 nr_uniq_cpus++;
50 cpumask_set_cpu(i, cpus);
51 }
52
53 queue = 0;
54 for_each_possible_cpu(i) {
320ae51f
JA
55 /*
56 * Easy case - we have equal or more hardware queues. Or
57 * there are no thread siblings to take into account. Do
58 * 1:1 if enough, or sequential mapping if less.
59 */
1cac41cb
MB
60 if (nr_queues >= nr_cpus) {
61 map[i] = cpu_to_queue_index(nr_cpu_ids, nr_queues, queue);
320ae51f
JA
62 queue++;
63 continue;
64 }
65
66 /*
67 * Less then nr_cpus queues, and we have some number of
68 * threads per cores. Map sibling threads to the same
69 * queue.
70 */
71 first_sibling = get_first_sibling(i);
72 if (first_sibling == i) {
1cac41cb 73 map[i] = cpu_to_queue_index(nr_cpu_ids, nr_queues,
320ae51f
JA
74 queue);
75 queue++;
76 } else
77 map[i] = map[first_sibling];
78 }
79
320ae51f
JA
80 free_cpumask_var(cpus);
81 return 0;
82}
83
24d2f903 84unsigned int *blk_mq_make_queue_map(struct blk_mq_tag_set *set)
320ae51f
JA
85{
86 unsigned int *map;
87
88 /* If cpus are offline, map them to first hctx */
a33c1ba2 89 map = kzalloc_node(sizeof(*map) * nr_cpu_ids, GFP_KERNEL,
24d2f903 90 set->numa_node);
320ae51f
JA
91 if (!map)
92 return NULL;
93
5778322e 94 if (!blk_mq_update_queue_map(map, set->nr_hw_queues, cpu_online_mask))
320ae51f
JA
95 return map;
96
97 kfree(map);
98 return NULL;
99}
f14bbe77
JA
100
101/*
102 * We have no quick way of doing reverse lookups. This is only used at
103 * queue init time, so runtime isn't important.
104 */
105int blk_mq_hw_queue_to_node(unsigned int *mq_map, unsigned int index)
106{
107 int i;
108
109 for_each_possible_cpu(i) {
110 if (index == mq_map[i])
111 return cpu_to_node(i);
112 }
113
114 return NUMA_NO_NODE;
115}