xref: /linux/drivers/infiniband/hw/hfi1/affinity.c (revision 3bd3706251ee8ab67e69d9340ac2abdca217e733)
1f48ad614SDennis Dalessandro /*
25d18ee67SSebastian Sanchez  * Copyright(c) 2015 - 2018 Intel Corporation.
3f48ad614SDennis Dalessandro  *
4f48ad614SDennis Dalessandro  * This file is provided under a dual BSD/GPLv2 license.  When using or
5f48ad614SDennis Dalessandro  * redistributing this file, you may do so under either license.
6f48ad614SDennis Dalessandro  *
7f48ad614SDennis Dalessandro  * GPL LICENSE SUMMARY
8f48ad614SDennis Dalessandro  *
9f48ad614SDennis Dalessandro  * This program is free software; you can redistribute it and/or modify
10f48ad614SDennis Dalessandro  * it under the terms of version 2 of the GNU General Public License as
11f48ad614SDennis Dalessandro  * published by the Free Software Foundation.
12f48ad614SDennis Dalessandro  *
13f48ad614SDennis Dalessandro  * This program is distributed in the hope that it will be useful, but
14f48ad614SDennis Dalessandro  * WITHOUT ANY WARRANTY; without even the implied warranty of
15f48ad614SDennis Dalessandro  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16f48ad614SDennis Dalessandro  * General Public License for more details.
17f48ad614SDennis Dalessandro  *
18f48ad614SDennis Dalessandro  * BSD LICENSE
19f48ad614SDennis Dalessandro  *
20f48ad614SDennis Dalessandro  * Redistribution and use in source and binary forms, with or without
21f48ad614SDennis Dalessandro  * modification, are permitted provided that the following conditions
22f48ad614SDennis Dalessandro  * are met:
23f48ad614SDennis Dalessandro  *
24f48ad614SDennis Dalessandro  *  - Redistributions of source code must retain the above copyright
25f48ad614SDennis Dalessandro  *    notice, this list of conditions and the following disclaimer.
26f48ad614SDennis Dalessandro  *  - Redistributions in binary form must reproduce the above copyright
27f48ad614SDennis Dalessandro  *    notice, this list of conditions and the following disclaimer in
28f48ad614SDennis Dalessandro  *    the documentation and/or other materials provided with the
29f48ad614SDennis Dalessandro  *    distribution.
30f48ad614SDennis Dalessandro  *  - Neither the name of Intel Corporation nor the names of its
31f48ad614SDennis Dalessandro  *    contributors may be used to endorse or promote products derived
32f48ad614SDennis Dalessandro  *    from this software without specific prior written permission.
33f48ad614SDennis Dalessandro  *
34f48ad614SDennis Dalessandro  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35f48ad614SDennis Dalessandro  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36f48ad614SDennis Dalessandro  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37f48ad614SDennis Dalessandro  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38f48ad614SDennis Dalessandro  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39f48ad614SDennis Dalessandro  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40f48ad614SDennis Dalessandro  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41f48ad614SDennis Dalessandro  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42f48ad614SDennis Dalessandro  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43f48ad614SDennis Dalessandro  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44f48ad614SDennis Dalessandro  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45f48ad614SDennis Dalessandro  *
46f48ad614SDennis Dalessandro  */
47f48ad614SDennis Dalessandro #include <linux/topology.h>
48f48ad614SDennis Dalessandro #include <linux/cpumask.h>
49f48ad614SDennis Dalessandro #include <linux/module.h>
502d01c37dSTadeusz Struk #include <linux/interrupt.h>
5198fa15f3SAnshuman Khandual #include <linux/numa.h>
52f48ad614SDennis Dalessandro 
53f48ad614SDennis Dalessandro #include "hfi.h"
54f48ad614SDennis Dalessandro #include "affinity.h"
55f48ad614SDennis Dalessandro #include "sdma.h"
56f48ad614SDennis Dalessandro #include "trace.h"
57f48ad614SDennis Dalessandro 
584197344bSDennis Dalessandro struct hfi1_affinity_node_list node_affinity = {
594197344bSDennis Dalessandro 	.list = LIST_HEAD_INIT(node_affinity.list),
60584d9577STadeusz Struk 	.lock = __MUTEX_INITIALIZER(node_affinity.lock)
614197344bSDennis Dalessandro };
624197344bSDennis Dalessandro 
63f48ad614SDennis Dalessandro /* Name of IRQ types, indexed by enum irq_type */
64f48ad614SDennis Dalessandro static const char * const irq_type_names[] = {
65f48ad614SDennis Dalessandro 	"SDMA",
66f48ad614SDennis Dalessandro 	"RCVCTXT",
67f48ad614SDennis Dalessandro 	"GENERAL",
68f48ad614SDennis Dalessandro 	"OTHER",
69f48ad614SDennis Dalessandro };
70f48ad614SDennis Dalessandro 
71d6373019SSebastian Sanchez /* Per NUMA node count of HFI devices */
72d6373019SSebastian Sanchez static unsigned int *hfi1_per_node_cntr;
73d6373019SSebastian Sanchez 
74f48ad614SDennis Dalessandro static inline void init_cpu_mask_set(struct cpu_mask_set *set)
75f48ad614SDennis Dalessandro {
76f48ad614SDennis Dalessandro 	cpumask_clear(&set->mask);
77f48ad614SDennis Dalessandro 	cpumask_clear(&set->used);
78f48ad614SDennis Dalessandro 	set->gen = 0;
79f48ad614SDennis Dalessandro }
80f48ad614SDennis Dalessandro 
81cf38ea10SSebastian Sanchez /* Increment generation of CPU set if needed */
82cf38ea10SSebastian Sanchez static void _cpu_mask_set_gen_inc(struct cpu_mask_set *set)
83cf38ea10SSebastian Sanchez {
84cf38ea10SSebastian Sanchez 	if (cpumask_equal(&set->mask, &set->used)) {
85cf38ea10SSebastian Sanchez 		/*
86cf38ea10SSebastian Sanchez 		 * We've used up all the CPUs, bump up the generation
87cf38ea10SSebastian Sanchez 		 * and reset the 'used' map
88cf38ea10SSebastian Sanchez 		 */
89cf38ea10SSebastian Sanchez 		set->gen++;
90cf38ea10SSebastian Sanchez 		cpumask_clear(&set->used);
91cf38ea10SSebastian Sanchez 	}
92cf38ea10SSebastian Sanchez }
93cf38ea10SSebastian Sanchez 
94cf38ea10SSebastian Sanchez static void _cpu_mask_set_gen_dec(struct cpu_mask_set *set)
95cf38ea10SSebastian Sanchez {
96cf38ea10SSebastian Sanchez 	if (cpumask_empty(&set->used) && set->gen) {
97cf38ea10SSebastian Sanchez 		set->gen--;
98cf38ea10SSebastian Sanchez 		cpumask_copy(&set->used, &set->mask);
99cf38ea10SSebastian Sanchez 	}
100cf38ea10SSebastian Sanchez }
101cf38ea10SSebastian Sanchez 
102cf38ea10SSebastian Sanchez /* Get the first CPU from the list of unused CPUs in a CPU set data structure */
103cf38ea10SSebastian Sanchez static int cpu_mask_set_get_first(struct cpu_mask_set *set, cpumask_var_t diff)
104cf38ea10SSebastian Sanchez {
105cf38ea10SSebastian Sanchez 	int cpu;
106cf38ea10SSebastian Sanchez 
107cf38ea10SSebastian Sanchez 	if (!diff || !set)
108cf38ea10SSebastian Sanchez 		return -EINVAL;
109cf38ea10SSebastian Sanchez 
110cf38ea10SSebastian Sanchez 	_cpu_mask_set_gen_inc(set);
111cf38ea10SSebastian Sanchez 
112cf38ea10SSebastian Sanchez 	/* Find out CPUs left in CPU mask */
113cf38ea10SSebastian Sanchez 	cpumask_andnot(diff, &set->mask, &set->used);
114cf38ea10SSebastian Sanchez 
115cf38ea10SSebastian Sanchez 	cpu = cpumask_first(diff);
116cf38ea10SSebastian Sanchez 	if (cpu >= nr_cpu_ids) /* empty */
117cf38ea10SSebastian Sanchez 		cpu = -EINVAL;
118cf38ea10SSebastian Sanchez 	else
119cf38ea10SSebastian Sanchez 		cpumask_set_cpu(cpu, &set->used);
120cf38ea10SSebastian Sanchez 
121cf38ea10SSebastian Sanchez 	return cpu;
122cf38ea10SSebastian Sanchez }
123cf38ea10SSebastian Sanchez 
124cf38ea10SSebastian Sanchez static void cpu_mask_set_put(struct cpu_mask_set *set, int cpu)
125cf38ea10SSebastian Sanchez {
126cf38ea10SSebastian Sanchez 	if (!set)
127cf38ea10SSebastian Sanchez 		return;
128cf38ea10SSebastian Sanchez 
129cf38ea10SSebastian Sanchez 	cpumask_clear_cpu(cpu, &set->used);
130cf38ea10SSebastian Sanchez 	_cpu_mask_set_gen_dec(set);
131cf38ea10SSebastian Sanchez }
132cf38ea10SSebastian Sanchez 
133f48ad614SDennis Dalessandro /* Initialize non-HT cpu cores mask */
1344197344bSDennis Dalessandro void init_real_cpu_mask(void)
135f48ad614SDennis Dalessandro {
136f48ad614SDennis Dalessandro 	int possible, curr_cpu, i, ht;
137f48ad614SDennis Dalessandro 
1384197344bSDennis Dalessandro 	cpumask_clear(&node_affinity.real_cpu_mask);
139f48ad614SDennis Dalessandro 
140f48ad614SDennis Dalessandro 	/* Start with cpu online mask as the real cpu mask */
1414197344bSDennis Dalessandro 	cpumask_copy(&node_affinity.real_cpu_mask, cpu_online_mask);
142f48ad614SDennis Dalessandro 
143f48ad614SDennis Dalessandro 	/*
144f48ad614SDennis Dalessandro 	 * Remove HT cores from the real cpu mask.  Do this in two steps below.
145f48ad614SDennis Dalessandro 	 */
1464197344bSDennis Dalessandro 	possible = cpumask_weight(&node_affinity.real_cpu_mask);
147f48ad614SDennis Dalessandro 	ht = cpumask_weight(topology_sibling_cpumask(
1484197344bSDennis Dalessandro 				cpumask_first(&node_affinity.real_cpu_mask)));
149f48ad614SDennis Dalessandro 	/*
150f48ad614SDennis Dalessandro 	 * Step 1.  Skip over the first N HT siblings and use them as the
151f48ad614SDennis Dalessandro 	 * "real" cores.  Assumes that HT cores are not enumerated in
152f48ad614SDennis Dalessandro 	 * succession (except in the single core case).
153f48ad614SDennis Dalessandro 	 */
1544197344bSDennis Dalessandro 	curr_cpu = cpumask_first(&node_affinity.real_cpu_mask);
155f48ad614SDennis Dalessandro 	for (i = 0; i < possible / ht; i++)
1564197344bSDennis Dalessandro 		curr_cpu = cpumask_next(curr_cpu, &node_affinity.real_cpu_mask);
157f48ad614SDennis Dalessandro 	/*
158f48ad614SDennis Dalessandro 	 * Step 2.  Remove the remaining HT siblings.  Use cpumask_next() to
159f48ad614SDennis Dalessandro 	 * skip any gaps.
160f48ad614SDennis Dalessandro 	 */
161f48ad614SDennis Dalessandro 	for (; i < possible; i++) {
1624197344bSDennis Dalessandro 		cpumask_clear_cpu(curr_cpu, &node_affinity.real_cpu_mask);
1634197344bSDennis Dalessandro 		curr_cpu = cpumask_next(curr_cpu, &node_affinity.real_cpu_mask);
1644197344bSDennis Dalessandro 	}
165f48ad614SDennis Dalessandro }
166f48ad614SDennis Dalessandro 
167d6373019SSebastian Sanchez int node_affinity_init(void)
1684197344bSDennis Dalessandro {
169d6373019SSebastian Sanchez 	int node;
170d6373019SSebastian Sanchez 	struct pci_dev *dev = NULL;
171d6373019SSebastian Sanchez 	const struct pci_device_id *ids = hfi1_pci_tbl;
172d6373019SSebastian Sanchez 
173b094a36fSSebastian Sanchez 	cpumask_clear(&node_affinity.proc.used);
1744197344bSDennis Dalessandro 	cpumask_copy(&node_affinity.proc.mask, cpu_online_mask);
175b094a36fSSebastian Sanchez 
176b094a36fSSebastian Sanchez 	node_affinity.proc.gen = 0;
177b094a36fSSebastian Sanchez 	node_affinity.num_core_siblings =
178b094a36fSSebastian Sanchez 				cpumask_weight(topology_sibling_cpumask(
179b094a36fSSebastian Sanchez 					cpumask_first(&node_affinity.proc.mask)
180b094a36fSSebastian Sanchez 					));
1819d8145a6SHarish Chegondi 	node_affinity.num_possible_nodes = num_possible_nodes();
182b094a36fSSebastian Sanchez 	node_affinity.num_online_nodes = num_online_nodes();
183b094a36fSSebastian Sanchez 	node_affinity.num_online_cpus = num_online_cpus();
184b094a36fSSebastian Sanchez 
1854197344bSDennis Dalessandro 	/*
1864197344bSDennis Dalessandro 	 * The real cpu mask is part of the affinity struct but it has to be
1874197344bSDennis Dalessandro 	 * initialized early. It is needed to calculate the number of user
1884197344bSDennis Dalessandro 	 * contexts in set_up_context_variables().
1894197344bSDennis Dalessandro 	 */
1904197344bSDennis Dalessandro 	init_real_cpu_mask();
191d6373019SSebastian Sanchez 
1929d8145a6SHarish Chegondi 	hfi1_per_node_cntr = kcalloc(node_affinity.num_possible_nodes,
193d6373019SSebastian Sanchez 				     sizeof(*hfi1_per_node_cntr), GFP_KERNEL);
194d6373019SSebastian Sanchez 	if (!hfi1_per_node_cntr)
195d6373019SSebastian Sanchez 		return -ENOMEM;
196d6373019SSebastian Sanchez 
197d6373019SSebastian Sanchez 	while (ids->vendor) {
198d6373019SSebastian Sanchez 		dev = NULL;
199d6373019SSebastian Sanchez 		while ((dev = pci_get_device(ids->vendor, ids->device, dev))) {
200d6373019SSebastian Sanchez 			node = pcibus_to_node(dev->bus);
201d6373019SSebastian Sanchez 			if (node < 0)
202c513de49SMichael J. Ruhl 				goto out;
203d6373019SSebastian Sanchez 
204d6373019SSebastian Sanchez 			hfi1_per_node_cntr[node]++;
205d6373019SSebastian Sanchez 		}
206d6373019SSebastian Sanchez 		ids++;
207d6373019SSebastian Sanchez 	}
208d6373019SSebastian Sanchez 
209d6373019SSebastian Sanchez 	return 0;
210c513de49SMichael J. Ruhl 
211c513de49SMichael J. Ruhl out:
212c513de49SMichael J. Ruhl 	/*
213c513de49SMichael J. Ruhl 	 * Invalid PCI NUMA node information found, note it, and populate
214c513de49SMichael J. Ruhl 	 * our database 1:1.
215c513de49SMichael J. Ruhl 	 */
216c513de49SMichael J. Ruhl 	pr_err("HFI: Invalid PCI NUMA node. Performance may be affected\n");
217c513de49SMichael J. Ruhl 	pr_err("HFI: System BIOS may need to be upgraded\n");
218c513de49SMichael J. Ruhl 	for (node = 0; node < node_affinity.num_possible_nodes; node++)
219c513de49SMichael J. Ruhl 		hfi1_per_node_cntr[node] = 1;
220c513de49SMichael J. Ruhl 
221c513de49SMichael J. Ruhl 	return 0;
2224197344bSDennis Dalessandro }
2234197344bSDennis Dalessandro 
2245d18ee67SSebastian Sanchez static void node_affinity_destroy(struct hfi1_affinity_node *entry)
2255d18ee67SSebastian Sanchez {
2265d18ee67SSebastian Sanchez 	free_percpu(entry->comp_vect_affinity);
2275d18ee67SSebastian Sanchez 	kfree(entry);
2285d18ee67SSebastian Sanchez }
2295d18ee67SSebastian Sanchez 
2305d18ee67SSebastian Sanchez void node_affinity_destroy_all(void)
2314197344bSDennis Dalessandro {
2324197344bSDennis Dalessandro 	struct list_head *pos, *q;
2334197344bSDennis Dalessandro 	struct hfi1_affinity_node *entry;
2344197344bSDennis Dalessandro 
235584d9577STadeusz Struk 	mutex_lock(&node_affinity.lock);
2364197344bSDennis Dalessandro 	list_for_each_safe(pos, q, &node_affinity.list) {
2374197344bSDennis Dalessandro 		entry = list_entry(pos, struct hfi1_affinity_node,
2384197344bSDennis Dalessandro 				   list);
2394197344bSDennis Dalessandro 		list_del(pos);
2405d18ee67SSebastian Sanchez 		node_affinity_destroy(entry);
2414197344bSDennis Dalessandro 	}
242584d9577STadeusz Struk 	mutex_unlock(&node_affinity.lock);
243d6373019SSebastian Sanchez 	kfree(hfi1_per_node_cntr);
2444197344bSDennis Dalessandro }
2454197344bSDennis Dalessandro 
2464197344bSDennis Dalessandro static struct hfi1_affinity_node *node_affinity_allocate(int node)
2474197344bSDennis Dalessandro {
2484197344bSDennis Dalessandro 	struct hfi1_affinity_node *entry;
2494197344bSDennis Dalessandro 
2504197344bSDennis Dalessandro 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
2514197344bSDennis Dalessandro 	if (!entry)
2524197344bSDennis Dalessandro 		return NULL;
2534197344bSDennis Dalessandro 	entry->node = node;
2545d18ee67SSebastian Sanchez 	entry->comp_vect_affinity = alloc_percpu(u16);
2554197344bSDennis Dalessandro 	INIT_LIST_HEAD(&entry->list);
2564197344bSDennis Dalessandro 
2574197344bSDennis Dalessandro 	return entry;
2584197344bSDennis Dalessandro }
2594197344bSDennis Dalessandro 
2604197344bSDennis Dalessandro /*
2614197344bSDennis Dalessandro  * It appends an entry to the list.
2624197344bSDennis Dalessandro  * It *must* be called with node_affinity.lock held.
2634197344bSDennis Dalessandro  */
2644197344bSDennis Dalessandro static void node_affinity_add_tail(struct hfi1_affinity_node *entry)
2654197344bSDennis Dalessandro {
2664197344bSDennis Dalessandro 	list_add_tail(&entry->list, &node_affinity.list);
2674197344bSDennis Dalessandro }
2684197344bSDennis Dalessandro 
2694197344bSDennis Dalessandro /* It must be called with node_affinity.lock held */
2704197344bSDennis Dalessandro static struct hfi1_affinity_node *node_affinity_lookup(int node)
2714197344bSDennis Dalessandro {
2724197344bSDennis Dalessandro 	struct list_head *pos;
2734197344bSDennis Dalessandro 	struct hfi1_affinity_node *entry;
2744197344bSDennis Dalessandro 
2754197344bSDennis Dalessandro 	list_for_each(pos, &node_affinity.list) {
2764197344bSDennis Dalessandro 		entry = list_entry(pos, struct hfi1_affinity_node, list);
2774197344bSDennis Dalessandro 		if (entry->node == node)
2784197344bSDennis Dalessandro 			return entry;
2794197344bSDennis Dalessandro 	}
2804197344bSDennis Dalessandro 
2814197344bSDennis Dalessandro 	return NULL;
282f48ad614SDennis Dalessandro }
283f48ad614SDennis Dalessandro 
2845d18ee67SSebastian Sanchez static int per_cpu_affinity_get(cpumask_var_t possible_cpumask,
2855d18ee67SSebastian Sanchez 				u16 __percpu *comp_vect_affinity)
2865d18ee67SSebastian Sanchez {
2875d18ee67SSebastian Sanchez 	int curr_cpu;
2885d18ee67SSebastian Sanchez 	u16 cntr;
2895d18ee67SSebastian Sanchez 	u16 prev_cntr;
2905d18ee67SSebastian Sanchez 	int ret_cpu;
2915d18ee67SSebastian Sanchez 
2925d18ee67SSebastian Sanchez 	if (!possible_cpumask) {
2935d18ee67SSebastian Sanchez 		ret_cpu = -EINVAL;
2945d18ee67SSebastian Sanchez 		goto fail;
2955d18ee67SSebastian Sanchez 	}
2965d18ee67SSebastian Sanchez 
2975d18ee67SSebastian Sanchez 	if (!comp_vect_affinity) {
2985d18ee67SSebastian Sanchez 		ret_cpu = -EINVAL;
2995d18ee67SSebastian Sanchez 		goto fail;
3005d18ee67SSebastian Sanchez 	}
3015d18ee67SSebastian Sanchez 
3025d18ee67SSebastian Sanchez 	ret_cpu = cpumask_first(possible_cpumask);
3035d18ee67SSebastian Sanchez 	if (ret_cpu >= nr_cpu_ids) {
3045d18ee67SSebastian Sanchez 		ret_cpu = -EINVAL;
3055d18ee67SSebastian Sanchez 		goto fail;
3065d18ee67SSebastian Sanchez 	}
3075d18ee67SSebastian Sanchez 
3085d18ee67SSebastian Sanchez 	prev_cntr = *per_cpu_ptr(comp_vect_affinity, ret_cpu);
3095d18ee67SSebastian Sanchez 	for_each_cpu(curr_cpu, possible_cpumask) {
3105d18ee67SSebastian Sanchez 		cntr = *per_cpu_ptr(comp_vect_affinity, curr_cpu);
3115d18ee67SSebastian Sanchez 
3125d18ee67SSebastian Sanchez 		if (cntr < prev_cntr) {
3135d18ee67SSebastian Sanchez 			ret_cpu = curr_cpu;
3145d18ee67SSebastian Sanchez 			prev_cntr = cntr;
3155d18ee67SSebastian Sanchez 		}
3165d18ee67SSebastian Sanchez 	}
3175d18ee67SSebastian Sanchez 
3185d18ee67SSebastian Sanchez 	*per_cpu_ptr(comp_vect_affinity, ret_cpu) += 1;
3195d18ee67SSebastian Sanchez 
3205d18ee67SSebastian Sanchez fail:
3215d18ee67SSebastian Sanchez 	return ret_cpu;
3225d18ee67SSebastian Sanchez }
3235d18ee67SSebastian Sanchez 
3245d18ee67SSebastian Sanchez static int per_cpu_affinity_put_max(cpumask_var_t possible_cpumask,
3255d18ee67SSebastian Sanchez 				    u16 __percpu *comp_vect_affinity)
3265d18ee67SSebastian Sanchez {
3275d18ee67SSebastian Sanchez 	int curr_cpu;
3285d18ee67SSebastian Sanchez 	int max_cpu;
3295d18ee67SSebastian Sanchez 	u16 cntr;
3305d18ee67SSebastian Sanchez 	u16 prev_cntr;
3315d18ee67SSebastian Sanchez 
3325d18ee67SSebastian Sanchez 	if (!possible_cpumask)
3335d18ee67SSebastian Sanchez 		return -EINVAL;
3345d18ee67SSebastian Sanchez 
3355d18ee67SSebastian Sanchez 	if (!comp_vect_affinity)
3365d18ee67SSebastian Sanchez 		return -EINVAL;
3375d18ee67SSebastian Sanchez 
3385d18ee67SSebastian Sanchez 	max_cpu = cpumask_first(possible_cpumask);
3395d18ee67SSebastian Sanchez 	if (max_cpu >= nr_cpu_ids)
3405d18ee67SSebastian Sanchez 		return -EINVAL;
3415d18ee67SSebastian Sanchez 
3425d18ee67SSebastian Sanchez 	prev_cntr = *per_cpu_ptr(comp_vect_affinity, max_cpu);
3435d18ee67SSebastian Sanchez 	for_each_cpu(curr_cpu, possible_cpumask) {
3445d18ee67SSebastian Sanchez 		cntr = *per_cpu_ptr(comp_vect_affinity, curr_cpu);
3455d18ee67SSebastian Sanchez 
3465d18ee67SSebastian Sanchez 		if (cntr > prev_cntr) {
3475d18ee67SSebastian Sanchez 			max_cpu = curr_cpu;
3485d18ee67SSebastian Sanchez 			prev_cntr = cntr;
3495d18ee67SSebastian Sanchez 		}
3505d18ee67SSebastian Sanchez 	}
3515d18ee67SSebastian Sanchez 
3525d18ee67SSebastian Sanchez 	*per_cpu_ptr(comp_vect_affinity, max_cpu) -= 1;
3535d18ee67SSebastian Sanchez 
3545d18ee67SSebastian Sanchez 	return max_cpu;
3555d18ee67SSebastian Sanchez }
3565d18ee67SSebastian Sanchez 
3575d18ee67SSebastian Sanchez /*
3585d18ee67SSebastian Sanchez  * Non-interrupt CPUs are used first, then interrupt CPUs.
3595d18ee67SSebastian Sanchez  * Two already allocated cpu masks must be passed.
3605d18ee67SSebastian Sanchez  */
3615d18ee67SSebastian Sanchez static int _dev_comp_vect_cpu_get(struct hfi1_devdata *dd,
3625d18ee67SSebastian Sanchez 				  struct hfi1_affinity_node *entry,
3635d18ee67SSebastian Sanchez 				  cpumask_var_t non_intr_cpus,
3645d18ee67SSebastian Sanchez 				  cpumask_var_t available_cpus)
3655d18ee67SSebastian Sanchez 	__must_hold(&node_affinity.lock)
3665d18ee67SSebastian Sanchez {
3675d18ee67SSebastian Sanchez 	int cpu;
3685d18ee67SSebastian Sanchez 	struct cpu_mask_set *set = dd->comp_vect;
3695d18ee67SSebastian Sanchez 
3705d18ee67SSebastian Sanchez 	lockdep_assert_held(&node_affinity.lock);
3715d18ee67SSebastian Sanchez 	if (!non_intr_cpus) {
3725d18ee67SSebastian Sanchez 		cpu = -1;
3735d18ee67SSebastian Sanchez 		goto fail;
3745d18ee67SSebastian Sanchez 	}
3755d18ee67SSebastian Sanchez 
3765d18ee67SSebastian Sanchez 	if (!available_cpus) {
3775d18ee67SSebastian Sanchez 		cpu = -1;
3785d18ee67SSebastian Sanchez 		goto fail;
3795d18ee67SSebastian Sanchez 	}
3805d18ee67SSebastian Sanchez 
3815d18ee67SSebastian Sanchez 	/* Available CPUs for pinning completion vectors */
3825d18ee67SSebastian Sanchez 	_cpu_mask_set_gen_inc(set);
3835d18ee67SSebastian Sanchez 	cpumask_andnot(available_cpus, &set->mask, &set->used);
3845d18ee67SSebastian Sanchez 
3855d18ee67SSebastian Sanchez 	/* Available CPUs without SDMA engine interrupts */
3865d18ee67SSebastian Sanchez 	cpumask_andnot(non_intr_cpus, available_cpus,
3875d18ee67SSebastian Sanchez 		       &entry->def_intr.used);
3885d18ee67SSebastian Sanchez 
3895d18ee67SSebastian Sanchez 	/* If there are non-interrupt CPUs available, use them first */
3905d18ee67SSebastian Sanchez 	if (!cpumask_empty(non_intr_cpus))
3915d18ee67SSebastian Sanchez 		cpu = cpumask_first(non_intr_cpus);
3925d18ee67SSebastian Sanchez 	else /* Otherwise, use interrupt CPUs */
3935d18ee67SSebastian Sanchez 		cpu = cpumask_first(available_cpus);
3945d18ee67SSebastian Sanchez 
3955d18ee67SSebastian Sanchez 	if (cpu >= nr_cpu_ids) { /* empty */
3965d18ee67SSebastian Sanchez 		cpu = -1;
3975d18ee67SSebastian Sanchez 		goto fail;
3985d18ee67SSebastian Sanchez 	}
3995d18ee67SSebastian Sanchez 	cpumask_set_cpu(cpu, &set->used);
4005d18ee67SSebastian Sanchez 
4015d18ee67SSebastian Sanchez fail:
4025d18ee67SSebastian Sanchez 	return cpu;
4035d18ee67SSebastian Sanchez }
4045d18ee67SSebastian Sanchez 
4055d18ee67SSebastian Sanchez static void _dev_comp_vect_cpu_put(struct hfi1_devdata *dd, int cpu)
4065d18ee67SSebastian Sanchez {
4075d18ee67SSebastian Sanchez 	struct cpu_mask_set *set = dd->comp_vect;
4085d18ee67SSebastian Sanchez 
4095d18ee67SSebastian Sanchez 	if (cpu < 0)
4105d18ee67SSebastian Sanchez 		return;
4115d18ee67SSebastian Sanchez 
4125d18ee67SSebastian Sanchez 	cpu_mask_set_put(set, cpu);
4135d18ee67SSebastian Sanchez }
4145d18ee67SSebastian Sanchez 
4155d18ee67SSebastian Sanchez /* _dev_comp_vect_mappings_destroy() is reentrant */
4165d18ee67SSebastian Sanchez static void _dev_comp_vect_mappings_destroy(struct hfi1_devdata *dd)
4175d18ee67SSebastian Sanchez {
4185d18ee67SSebastian Sanchez 	int i, cpu;
4195d18ee67SSebastian Sanchez 
4205d18ee67SSebastian Sanchez 	if (!dd->comp_vect_mappings)
4215d18ee67SSebastian Sanchez 		return;
4225d18ee67SSebastian Sanchez 
4235d18ee67SSebastian Sanchez 	for (i = 0; i < dd->comp_vect_possible_cpus; i++) {
4245d18ee67SSebastian Sanchez 		cpu = dd->comp_vect_mappings[i];
4255d18ee67SSebastian Sanchez 		_dev_comp_vect_cpu_put(dd, cpu);
4265d18ee67SSebastian Sanchez 		dd->comp_vect_mappings[i] = -1;
4275d18ee67SSebastian Sanchez 		hfi1_cdbg(AFFINITY,
4285d18ee67SSebastian Sanchez 			  "[%s] Release CPU %d from completion vector %d",
4295d18ee67SSebastian Sanchez 			  rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), cpu, i);
4305d18ee67SSebastian Sanchez 	}
4315d18ee67SSebastian Sanchez 
4325d18ee67SSebastian Sanchez 	kfree(dd->comp_vect_mappings);
4335d18ee67SSebastian Sanchez 	dd->comp_vect_mappings = NULL;
4345d18ee67SSebastian Sanchez }
4355d18ee67SSebastian Sanchez 
4365d18ee67SSebastian Sanchez /*
4375d18ee67SSebastian Sanchez  * This function creates the table for looking up CPUs for completion vectors.
4385d18ee67SSebastian Sanchez  * num_comp_vectors needs to have been initilized before calling this function.
4395d18ee67SSebastian Sanchez  */
4405d18ee67SSebastian Sanchez static int _dev_comp_vect_mappings_create(struct hfi1_devdata *dd,
4415d18ee67SSebastian Sanchez 					  struct hfi1_affinity_node *entry)
4425d18ee67SSebastian Sanchez 	__must_hold(&node_affinity.lock)
4435d18ee67SSebastian Sanchez {
4445d18ee67SSebastian Sanchez 	int i, cpu, ret;
4455d18ee67SSebastian Sanchez 	cpumask_var_t non_intr_cpus;
4465d18ee67SSebastian Sanchez 	cpumask_var_t available_cpus;
4475d18ee67SSebastian Sanchez 
4485d18ee67SSebastian Sanchez 	lockdep_assert_held(&node_affinity.lock);
4495d18ee67SSebastian Sanchez 
4505d18ee67SSebastian Sanchez 	if (!zalloc_cpumask_var(&non_intr_cpus, GFP_KERNEL))
4515d18ee67SSebastian Sanchez 		return -ENOMEM;
4525d18ee67SSebastian Sanchez 
4535d18ee67SSebastian Sanchez 	if (!zalloc_cpumask_var(&available_cpus, GFP_KERNEL)) {
4545d18ee67SSebastian Sanchez 		free_cpumask_var(non_intr_cpus);
4555d18ee67SSebastian Sanchez 		return -ENOMEM;
4565d18ee67SSebastian Sanchez 	}
4575d18ee67SSebastian Sanchez 
4585d18ee67SSebastian Sanchez 	dd->comp_vect_mappings = kcalloc(dd->comp_vect_possible_cpus,
4595d18ee67SSebastian Sanchez 					 sizeof(*dd->comp_vect_mappings),
4605d18ee67SSebastian Sanchez 					 GFP_KERNEL);
4615d18ee67SSebastian Sanchez 	if (!dd->comp_vect_mappings) {
4625d18ee67SSebastian Sanchez 		ret = -ENOMEM;
4635d18ee67SSebastian Sanchez 		goto fail;
4645d18ee67SSebastian Sanchez 	}
4655d18ee67SSebastian Sanchez 	for (i = 0; i < dd->comp_vect_possible_cpus; i++)
4665d18ee67SSebastian Sanchez 		dd->comp_vect_mappings[i] = -1;
4675d18ee67SSebastian Sanchez 
4685d18ee67SSebastian Sanchez 	for (i = 0; i < dd->comp_vect_possible_cpus; i++) {
4695d18ee67SSebastian Sanchez 		cpu = _dev_comp_vect_cpu_get(dd, entry, non_intr_cpus,
4705d18ee67SSebastian Sanchez 					     available_cpus);
4715d18ee67SSebastian Sanchez 		if (cpu < 0) {
4725d18ee67SSebastian Sanchez 			ret = -EINVAL;
4735d18ee67SSebastian Sanchez 			goto fail;
4745d18ee67SSebastian Sanchez 		}
4755d18ee67SSebastian Sanchez 
4765d18ee67SSebastian Sanchez 		dd->comp_vect_mappings[i] = cpu;
4775d18ee67SSebastian Sanchez 		hfi1_cdbg(AFFINITY,
4785d18ee67SSebastian Sanchez 			  "[%s] Completion Vector %d -> CPU %d",
4795d18ee67SSebastian Sanchez 			  rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), i, cpu);
4805d18ee67SSebastian Sanchez 	}
4815d18ee67SSebastian Sanchez 
4825d18ee67SSebastian Sanchez 	return 0;
4835d18ee67SSebastian Sanchez 
4845d18ee67SSebastian Sanchez fail:
4855d18ee67SSebastian Sanchez 	free_cpumask_var(available_cpus);
4865d18ee67SSebastian Sanchez 	free_cpumask_var(non_intr_cpus);
4875d18ee67SSebastian Sanchez 	_dev_comp_vect_mappings_destroy(dd);
4885d18ee67SSebastian Sanchez 
4895d18ee67SSebastian Sanchez 	return ret;
4905d18ee67SSebastian Sanchez }
4915d18ee67SSebastian Sanchez 
4925d18ee67SSebastian Sanchez int hfi1_comp_vectors_set_up(struct hfi1_devdata *dd)
4935d18ee67SSebastian Sanchez {
4945d18ee67SSebastian Sanchez 	int ret;
4955d18ee67SSebastian Sanchez 	struct hfi1_affinity_node *entry;
4965d18ee67SSebastian Sanchez 
4975d18ee67SSebastian Sanchez 	mutex_lock(&node_affinity.lock);
4985d18ee67SSebastian Sanchez 	entry = node_affinity_lookup(dd->node);
4995d18ee67SSebastian Sanchez 	if (!entry) {
5005d18ee67SSebastian Sanchez 		ret = -EINVAL;
5015d18ee67SSebastian Sanchez 		goto unlock;
5025d18ee67SSebastian Sanchez 	}
5035d18ee67SSebastian Sanchez 	ret = _dev_comp_vect_mappings_create(dd, entry);
5045d18ee67SSebastian Sanchez unlock:
5055d18ee67SSebastian Sanchez 	mutex_unlock(&node_affinity.lock);
5065d18ee67SSebastian Sanchez 
5075d18ee67SSebastian Sanchez 	return ret;
5085d18ee67SSebastian Sanchez }
5095d18ee67SSebastian Sanchez 
5105d18ee67SSebastian Sanchez void hfi1_comp_vectors_clean_up(struct hfi1_devdata *dd)
5115d18ee67SSebastian Sanchez {
5125d18ee67SSebastian Sanchez 	_dev_comp_vect_mappings_destroy(dd);
5135d18ee67SSebastian Sanchez }
5145d18ee67SSebastian Sanchez 
5155d18ee67SSebastian Sanchez int hfi1_comp_vect_mappings_lookup(struct rvt_dev_info *rdi, int comp_vect)
5165d18ee67SSebastian Sanchez {
5175d18ee67SSebastian Sanchez 	struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi);
5185d18ee67SSebastian Sanchez 	struct hfi1_devdata *dd = dd_from_dev(verbs_dev);
5195d18ee67SSebastian Sanchez 
5205d18ee67SSebastian Sanchez 	if (!dd->comp_vect_mappings)
5215d18ee67SSebastian Sanchez 		return -EINVAL;
5225d18ee67SSebastian Sanchez 	if (comp_vect >= dd->comp_vect_possible_cpus)
5235d18ee67SSebastian Sanchez 		return -EINVAL;
5245d18ee67SSebastian Sanchez 
5255d18ee67SSebastian Sanchez 	return dd->comp_vect_mappings[comp_vect];
5265d18ee67SSebastian Sanchez }
5275d18ee67SSebastian Sanchez 
5285d18ee67SSebastian Sanchez /*
5295d18ee67SSebastian Sanchez  * It assumes dd->comp_vect_possible_cpus is available.
5305d18ee67SSebastian Sanchez  */
5315d18ee67SSebastian Sanchez static int _dev_comp_vect_cpu_mask_init(struct hfi1_devdata *dd,
5325d18ee67SSebastian Sanchez 					struct hfi1_affinity_node *entry,
5335d18ee67SSebastian Sanchez 					bool first_dev_init)
5345d18ee67SSebastian Sanchez 	__must_hold(&node_affinity.lock)
5355d18ee67SSebastian Sanchez {
5365d18ee67SSebastian Sanchez 	int i, j, curr_cpu;
5375d18ee67SSebastian Sanchez 	int possible_cpus_comp_vect = 0;
5385d18ee67SSebastian Sanchez 	struct cpumask *dev_comp_vect_mask = &dd->comp_vect->mask;
5395d18ee67SSebastian Sanchez 
5405d18ee67SSebastian Sanchez 	lockdep_assert_held(&node_affinity.lock);
5415d18ee67SSebastian Sanchez 	/*
5425d18ee67SSebastian Sanchez 	 * If there's only one CPU available for completion vectors, then
5435d18ee67SSebastian Sanchez 	 * there will only be one completion vector available. Othewise,
5445d18ee67SSebastian Sanchez 	 * the number of completion vector available will be the number of
5455d18ee67SSebastian Sanchez 	 * available CPUs divide it by the number of devices in the
5465d18ee67SSebastian Sanchez 	 * local NUMA node.
5475d18ee67SSebastian Sanchez 	 */
5485d18ee67SSebastian Sanchez 	if (cpumask_weight(&entry->comp_vect_mask) == 1) {
5495d18ee67SSebastian Sanchez 		possible_cpus_comp_vect = 1;
5505d18ee67SSebastian Sanchez 		dd_dev_warn(dd,
5515d18ee67SSebastian Sanchez 			    "Number of kernel receive queues is too large for completion vector affinity to be effective\n");
5525d18ee67SSebastian Sanchez 	} else {
5535d18ee67SSebastian Sanchez 		possible_cpus_comp_vect +=
5545d18ee67SSebastian Sanchez 			cpumask_weight(&entry->comp_vect_mask) /
5555d18ee67SSebastian Sanchez 				       hfi1_per_node_cntr[dd->node];
5565d18ee67SSebastian Sanchez 
5575d18ee67SSebastian Sanchez 		/*
5585d18ee67SSebastian Sanchez 		 * If the completion vector CPUs available doesn't divide
5595d18ee67SSebastian Sanchez 		 * evenly among devices, then the first device device to be
5605d18ee67SSebastian Sanchez 		 * initialized gets an extra CPU.
5615d18ee67SSebastian Sanchez 		 */
5625d18ee67SSebastian Sanchez 		if (first_dev_init &&
5635d18ee67SSebastian Sanchez 		    cpumask_weight(&entry->comp_vect_mask) %
5645d18ee67SSebastian Sanchez 		    hfi1_per_node_cntr[dd->node] != 0)
5655d18ee67SSebastian Sanchez 			possible_cpus_comp_vect++;
5665d18ee67SSebastian Sanchez 	}
5675d18ee67SSebastian Sanchez 
5685d18ee67SSebastian Sanchez 	dd->comp_vect_possible_cpus = possible_cpus_comp_vect;
5695d18ee67SSebastian Sanchez 
5705d18ee67SSebastian Sanchez 	/* Reserving CPUs for device completion vector */
5715d18ee67SSebastian Sanchez 	for (i = 0; i < dd->comp_vect_possible_cpus; i++) {
5725d18ee67SSebastian Sanchez 		curr_cpu = per_cpu_affinity_get(&entry->comp_vect_mask,
5735d18ee67SSebastian Sanchez 						entry->comp_vect_affinity);
5745d18ee67SSebastian Sanchez 		if (curr_cpu < 0)
5755d18ee67SSebastian Sanchez 			goto fail;
5765d18ee67SSebastian Sanchez 
5775d18ee67SSebastian Sanchez 		cpumask_set_cpu(curr_cpu, dev_comp_vect_mask);
5785d18ee67SSebastian Sanchez 	}
5795d18ee67SSebastian Sanchez 
5805d18ee67SSebastian Sanchez 	hfi1_cdbg(AFFINITY,
5815d18ee67SSebastian Sanchez 		  "[%s] Completion vector affinity CPU set(s) %*pbl",
5825d18ee67SSebastian Sanchez 		  rvt_get_ibdev_name(&(dd)->verbs_dev.rdi),
5835d18ee67SSebastian Sanchez 		  cpumask_pr_args(dev_comp_vect_mask));
5845d18ee67SSebastian Sanchez 
5855d18ee67SSebastian Sanchez 	return 0;
5865d18ee67SSebastian Sanchez 
5875d18ee67SSebastian Sanchez fail:
5885d18ee67SSebastian Sanchez 	for (j = 0; j < i; j++)
5895d18ee67SSebastian Sanchez 		per_cpu_affinity_put_max(&entry->comp_vect_mask,
5905d18ee67SSebastian Sanchez 					 entry->comp_vect_affinity);
5915d18ee67SSebastian Sanchez 
5925d18ee67SSebastian Sanchez 	return curr_cpu;
5935d18ee67SSebastian Sanchez }
5945d18ee67SSebastian Sanchez 
5955d18ee67SSebastian Sanchez /*
5965d18ee67SSebastian Sanchez  * It assumes dd->comp_vect_possible_cpus is available.
5975d18ee67SSebastian Sanchez  */
5985d18ee67SSebastian Sanchez static void _dev_comp_vect_cpu_mask_clean_up(struct hfi1_devdata *dd,
5995d18ee67SSebastian Sanchez 					     struct hfi1_affinity_node *entry)
6005d18ee67SSebastian Sanchez 	__must_hold(&node_affinity.lock)
6015d18ee67SSebastian Sanchez {
6025d18ee67SSebastian Sanchez 	int i, cpu;
6035d18ee67SSebastian Sanchez 
6045d18ee67SSebastian Sanchez 	lockdep_assert_held(&node_affinity.lock);
6055d18ee67SSebastian Sanchez 	if (!dd->comp_vect_possible_cpus)
6065d18ee67SSebastian Sanchez 		return;
6075d18ee67SSebastian Sanchez 
6085d18ee67SSebastian Sanchez 	for (i = 0; i < dd->comp_vect_possible_cpus; i++) {
6095d18ee67SSebastian Sanchez 		cpu = per_cpu_affinity_put_max(&dd->comp_vect->mask,
6105d18ee67SSebastian Sanchez 					       entry->comp_vect_affinity);
6115d18ee67SSebastian Sanchez 		/* Clearing CPU in device completion vector cpu mask */
6125d18ee67SSebastian Sanchez 		if (cpu >= 0)
6135d18ee67SSebastian Sanchez 			cpumask_clear_cpu(cpu, &dd->comp_vect->mask);
6145d18ee67SSebastian Sanchez 	}
6155d18ee67SSebastian Sanchez 
6165d18ee67SSebastian Sanchez 	dd->comp_vect_possible_cpus = 0;
6175d18ee67SSebastian Sanchez }
6185d18ee67SSebastian Sanchez 
619f48ad614SDennis Dalessandro /*
620f48ad614SDennis Dalessandro  * Interrupt affinity.
621f48ad614SDennis Dalessandro  *
622f48ad614SDennis Dalessandro  * non-rcv avail gets a default mask that
623f48ad614SDennis Dalessandro  * starts as possible cpus with threads reset
624f48ad614SDennis Dalessandro  * and each rcv avail reset.
625f48ad614SDennis Dalessandro  *
626f48ad614SDennis Dalessandro  * rcv avail gets node relative 1 wrapping back
627f48ad614SDennis Dalessandro  * to the node relative 1 as necessary.
628f48ad614SDennis Dalessandro  *
629f48ad614SDennis Dalessandro  */
6304197344bSDennis Dalessandro int hfi1_dev_affinity_init(struct hfi1_devdata *dd)
631f48ad614SDennis Dalessandro {
632f48ad614SDennis Dalessandro 	int node = pcibus_to_node(dd->pcidev->bus);
6334197344bSDennis Dalessandro 	struct hfi1_affinity_node *entry;
634f48ad614SDennis Dalessandro 	const struct cpumask *local_mask;
6355d18ee67SSebastian Sanchez 	int curr_cpu, possible, i, ret;
6365d18ee67SSebastian Sanchez 	bool new_entry = false;
637f48ad614SDennis Dalessandro 
638c513de49SMichael J. Ruhl 	/*
639c513de49SMichael J. Ruhl 	 * If the BIOS does not have the NUMA node information set, select
640c513de49SMichael J. Ruhl 	 * NUMA 0 so we get consistent performance.
641c513de49SMichael J. Ruhl 	 */
642c513de49SMichael J. Ruhl 	if (node < 0) {
643c513de49SMichael J. Ruhl 		dd_dev_err(dd, "Invalid PCI NUMA node. Performance may be affected\n");
644c513de49SMichael J. Ruhl 		node = 0;
645c513de49SMichael J. Ruhl 	}
646f48ad614SDennis Dalessandro 	dd->node = node;
647f48ad614SDennis Dalessandro 
648f48ad614SDennis Dalessandro 	local_mask = cpumask_of_node(dd->node);
649f48ad614SDennis Dalessandro 	if (cpumask_first(local_mask) >= nr_cpu_ids)
650f48ad614SDennis Dalessandro 		local_mask = topology_core_cpumask(0);
6514197344bSDennis Dalessandro 
652584d9577STadeusz Struk 	mutex_lock(&node_affinity.lock);
6534197344bSDennis Dalessandro 	entry = node_affinity_lookup(dd->node);
6544197344bSDennis Dalessandro 
6554197344bSDennis Dalessandro 	/*
6564197344bSDennis Dalessandro 	 * If this is the first time this NUMA node's affinity is used,
6574197344bSDennis Dalessandro 	 * create an entry in the global affinity structure and initialize it.
6584197344bSDennis Dalessandro 	 */
6594197344bSDennis Dalessandro 	if (!entry) {
6604197344bSDennis Dalessandro 		entry = node_affinity_allocate(node);
6614197344bSDennis Dalessandro 		if (!entry) {
6624197344bSDennis Dalessandro 			dd_dev_err(dd,
6634197344bSDennis Dalessandro 				   "Unable to allocate global affinity node\n");
6645d18ee67SSebastian Sanchez 			ret = -ENOMEM;
6655d18ee67SSebastian Sanchez 			goto fail;
6664197344bSDennis Dalessandro 		}
6675d18ee67SSebastian Sanchez 		new_entry = true;
6685d18ee67SSebastian Sanchez 
6694197344bSDennis Dalessandro 		init_cpu_mask_set(&entry->def_intr);
6704197344bSDennis Dalessandro 		init_cpu_mask_set(&entry->rcv_intr);
6715d18ee67SSebastian Sanchez 		cpumask_clear(&entry->comp_vect_mask);
672d6373019SSebastian Sanchez 		cpumask_clear(&entry->general_intr_mask);
673f48ad614SDennis Dalessandro 		/* Use the "real" cpu mask of this node as the default */
6744197344bSDennis Dalessandro 		cpumask_and(&entry->def_intr.mask, &node_affinity.real_cpu_mask,
6754197344bSDennis Dalessandro 			    local_mask);
676f48ad614SDennis Dalessandro 
677f48ad614SDennis Dalessandro 		/* fill in the receive list */
6784197344bSDennis Dalessandro 		possible = cpumask_weight(&entry->def_intr.mask);
6794197344bSDennis Dalessandro 		curr_cpu = cpumask_first(&entry->def_intr.mask);
6804197344bSDennis Dalessandro 
681f48ad614SDennis Dalessandro 		if (possible == 1) {
682f48ad614SDennis Dalessandro 			/* only one CPU, everyone will use it */
6834197344bSDennis Dalessandro 			cpumask_set_cpu(curr_cpu, &entry->rcv_intr.mask);
684d6373019SSebastian Sanchez 			cpumask_set_cpu(curr_cpu, &entry->general_intr_mask);
685f48ad614SDennis Dalessandro 		} else {
686f48ad614SDennis Dalessandro 			/*
687d6373019SSebastian Sanchez 			 * The general/control context will be the first CPU in
688d6373019SSebastian Sanchez 			 * the default list, so it is removed from the default
689d6373019SSebastian Sanchez 			 * list and added to the general interrupt list.
690f48ad614SDennis Dalessandro 			 */
691d6373019SSebastian Sanchez 			cpumask_clear_cpu(curr_cpu, &entry->def_intr.mask);
692d6373019SSebastian Sanchez 			cpumask_set_cpu(curr_cpu, &entry->general_intr_mask);
6934197344bSDennis Dalessandro 			curr_cpu = cpumask_next(curr_cpu,
6944197344bSDennis Dalessandro 						&entry->def_intr.mask);
6954197344bSDennis Dalessandro 
696f48ad614SDennis Dalessandro 			/*
697f48ad614SDennis Dalessandro 			 * Remove the remaining kernel receive queues from
698f48ad614SDennis Dalessandro 			 * the default list and add them to the receive list.
699f48ad614SDennis Dalessandro 			 */
700d6373019SSebastian Sanchez 			for (i = 0;
701d6373019SSebastian Sanchez 			     i < (dd->n_krcv_queues - 1) *
702d6373019SSebastian Sanchez 				  hfi1_per_node_cntr[dd->node];
703d6373019SSebastian Sanchez 			     i++) {
7044197344bSDennis Dalessandro 				cpumask_clear_cpu(curr_cpu,
7054197344bSDennis Dalessandro 						  &entry->def_intr.mask);
7064197344bSDennis Dalessandro 				cpumask_set_cpu(curr_cpu,
7074197344bSDennis Dalessandro 						&entry->rcv_intr.mask);
7084197344bSDennis Dalessandro 				curr_cpu = cpumask_next(curr_cpu,
7094197344bSDennis Dalessandro 							&entry->def_intr.mask);
710f48ad614SDennis Dalessandro 				if (curr_cpu >= nr_cpu_ids)
711f48ad614SDennis Dalessandro 					break;
712f48ad614SDennis Dalessandro 			}
713d6373019SSebastian Sanchez 
714d6373019SSebastian Sanchez 			/*
715d6373019SSebastian Sanchez 			 * If there ends up being 0 CPU cores leftover for SDMA
716d6373019SSebastian Sanchez 			 * engines, use the same CPU cores as general/control
717d6373019SSebastian Sanchez 			 * context.
718d6373019SSebastian Sanchez 			 */
719d6373019SSebastian Sanchez 			if (cpumask_weight(&entry->def_intr.mask) == 0)
720d6373019SSebastian Sanchez 				cpumask_copy(&entry->def_intr.mask,
721d6373019SSebastian Sanchez 					     &entry->general_intr_mask);
722f48ad614SDennis Dalessandro 		}
723f48ad614SDennis Dalessandro 
7245d18ee67SSebastian Sanchez 		/* Determine completion vector CPUs for the entire node */
7255d18ee67SSebastian Sanchez 		cpumask_and(&entry->comp_vect_mask,
7265d18ee67SSebastian Sanchez 			    &node_affinity.real_cpu_mask, local_mask);
7275d18ee67SSebastian Sanchez 		cpumask_andnot(&entry->comp_vect_mask,
7285d18ee67SSebastian Sanchez 			       &entry->comp_vect_mask,
7295d18ee67SSebastian Sanchez 			       &entry->rcv_intr.mask);
7305d18ee67SSebastian Sanchez 		cpumask_andnot(&entry->comp_vect_mask,
7315d18ee67SSebastian Sanchez 			       &entry->comp_vect_mask,
7325d18ee67SSebastian Sanchez 			       &entry->general_intr_mask);
7335d18ee67SSebastian Sanchez 
7345d18ee67SSebastian Sanchez 		/*
7355d18ee67SSebastian Sanchez 		 * If there ends up being 0 CPU cores leftover for completion
7365d18ee67SSebastian Sanchez 		 * vectors, use the same CPU core as the general/control
7375d18ee67SSebastian Sanchez 		 * context.
7385d18ee67SSebastian Sanchez 		 */
7395d18ee67SSebastian Sanchez 		if (cpumask_weight(&entry->comp_vect_mask) == 0)
7405d18ee67SSebastian Sanchez 			cpumask_copy(&entry->comp_vect_mask,
7415d18ee67SSebastian Sanchez 				     &entry->general_intr_mask);
742f48ad614SDennis Dalessandro 	}
7435d18ee67SSebastian Sanchez 
7445d18ee67SSebastian Sanchez 	ret = _dev_comp_vect_cpu_mask_init(dd, entry, new_entry);
7455d18ee67SSebastian Sanchez 	if (ret < 0)
7465d18ee67SSebastian Sanchez 		goto fail;
7475d18ee67SSebastian Sanchez 
7485d18ee67SSebastian Sanchez 	if (new_entry)
7495d18ee67SSebastian Sanchez 		node_affinity_add_tail(entry);
7505d18ee67SSebastian Sanchez 
751584d9577STadeusz Struk 	mutex_unlock(&node_affinity.lock);
7525d18ee67SSebastian Sanchez 
7534197344bSDennis Dalessandro 	return 0;
7545d18ee67SSebastian Sanchez 
7555d18ee67SSebastian Sanchez fail:
7565d18ee67SSebastian Sanchez 	if (new_entry)
7575d18ee67SSebastian Sanchez 		node_affinity_destroy(entry);
7585d18ee67SSebastian Sanchez 	mutex_unlock(&node_affinity.lock);
7595d18ee67SSebastian Sanchez 	return ret;
7605d18ee67SSebastian Sanchez }
7615d18ee67SSebastian Sanchez 
7625d18ee67SSebastian Sanchez void hfi1_dev_affinity_clean_up(struct hfi1_devdata *dd)
7635d18ee67SSebastian Sanchez {
7645d18ee67SSebastian Sanchez 	struct hfi1_affinity_node *entry;
7655d18ee67SSebastian Sanchez 
7665d18ee67SSebastian Sanchez 	if (dd->node < 0)
7675d18ee67SSebastian Sanchez 		return;
7685d18ee67SSebastian Sanchez 
7695d18ee67SSebastian Sanchez 	mutex_lock(&node_affinity.lock);
7705d18ee67SSebastian Sanchez 	entry = node_affinity_lookup(dd->node);
7715d18ee67SSebastian Sanchez 	if (!entry)
7725d18ee67SSebastian Sanchez 		goto unlock;
7735d18ee67SSebastian Sanchez 
7745d18ee67SSebastian Sanchez 	/*
7755d18ee67SSebastian Sanchez 	 * Free device completion vector CPUs to be used by future
7765d18ee67SSebastian Sanchez 	 * completion vectors
7775d18ee67SSebastian Sanchez 	 */
7785d18ee67SSebastian Sanchez 	_dev_comp_vect_cpu_mask_clean_up(dd, entry);
7795d18ee67SSebastian Sanchez unlock:
7805d18ee67SSebastian Sanchez 	mutex_unlock(&node_affinity.lock);
78198fa15f3SAnshuman Khandual 	dd->node = NUMA_NO_NODE;
782f48ad614SDennis Dalessandro }
783f48ad614SDennis Dalessandro 
784584d9577STadeusz Struk /*
7852d01c37dSTadeusz Struk  * Function updates the irq affinity hint for msix after it has been changed
7862d01c37dSTadeusz Struk  * by the user using the /proc/irq interface. This function only accepts
7872d01c37dSTadeusz Struk  * one cpu in the mask.
7882d01c37dSTadeusz Struk  */
7892d01c37dSTadeusz Struk static void hfi1_update_sdma_affinity(struct hfi1_msix_entry *msix, int cpu)
7902d01c37dSTadeusz Struk {
7912d01c37dSTadeusz Struk 	struct sdma_engine *sde = msix->arg;
7922d01c37dSTadeusz Struk 	struct hfi1_devdata *dd = sde->dd;
7932d01c37dSTadeusz Struk 	struct hfi1_affinity_node *entry;
7942d01c37dSTadeusz Struk 	struct cpu_mask_set *set;
7952d01c37dSTadeusz Struk 	int i, old_cpu;
7962d01c37dSTadeusz Struk 
7972d01c37dSTadeusz Struk 	if (cpu > num_online_cpus() || cpu == sde->cpu)
7982d01c37dSTadeusz Struk 		return;
7992d01c37dSTadeusz Struk 
8002d01c37dSTadeusz Struk 	mutex_lock(&node_affinity.lock);
8012d01c37dSTadeusz Struk 	entry = node_affinity_lookup(dd->node);
8022d01c37dSTadeusz Struk 	if (!entry)
8032d01c37dSTadeusz Struk 		goto unlock;
8042d01c37dSTadeusz Struk 
8052d01c37dSTadeusz Struk 	old_cpu = sde->cpu;
8062d01c37dSTadeusz Struk 	sde->cpu = cpu;
8072d01c37dSTadeusz Struk 	cpumask_clear(&msix->mask);
8082d01c37dSTadeusz Struk 	cpumask_set_cpu(cpu, &msix->mask);
809bb7dde87SMichael J. Ruhl 	dd_dev_dbg(dd, "IRQ: %u, type %s engine %u -> cpu: %d\n",
810bb7dde87SMichael J. Ruhl 		   msix->irq, irq_type_names[msix->type],
8112d01c37dSTadeusz Struk 		   sde->this_idx, cpu);
812bb7dde87SMichael J. Ruhl 	irq_set_affinity_hint(msix->irq, &msix->mask);
8132d01c37dSTadeusz Struk 
8142d01c37dSTadeusz Struk 	/*
8152d01c37dSTadeusz Struk 	 * Set the new cpu in the hfi1_affinity_node and clean
8162d01c37dSTadeusz Struk 	 * the old cpu if it is not used by any other IRQ
8172d01c37dSTadeusz Struk 	 */
8182d01c37dSTadeusz Struk 	set = &entry->def_intr;
8192d01c37dSTadeusz Struk 	cpumask_set_cpu(cpu, &set->mask);
8202d01c37dSTadeusz Struk 	cpumask_set_cpu(cpu, &set->used);
8216eb4eb10SMichael J. Ruhl 	for (i = 0; i < dd->msix_info.max_requested; i++) {
8222d01c37dSTadeusz Struk 		struct hfi1_msix_entry *other_msix;
8232d01c37dSTadeusz Struk 
8246eb4eb10SMichael J. Ruhl 		other_msix = &dd->msix_info.msix_entries[i];
8252d01c37dSTadeusz Struk 		if (other_msix->type != IRQ_SDMA || other_msix == msix)
8262d01c37dSTadeusz Struk 			continue;
8272d01c37dSTadeusz Struk 
8282d01c37dSTadeusz Struk 		if (cpumask_test_cpu(old_cpu, &other_msix->mask))
8292d01c37dSTadeusz Struk 			goto unlock;
8302d01c37dSTadeusz Struk 	}
8312d01c37dSTadeusz Struk 	cpumask_clear_cpu(old_cpu, &set->mask);
8322d01c37dSTadeusz Struk 	cpumask_clear_cpu(old_cpu, &set->used);
8332d01c37dSTadeusz Struk unlock:
8342d01c37dSTadeusz Struk 	mutex_unlock(&node_affinity.lock);
8352d01c37dSTadeusz Struk }
8362d01c37dSTadeusz Struk 
8372d01c37dSTadeusz Struk static void hfi1_irq_notifier_notify(struct irq_affinity_notify *notify,
8382d01c37dSTadeusz Struk 				     const cpumask_t *mask)
8392d01c37dSTadeusz Struk {
8402d01c37dSTadeusz Struk 	int cpu = cpumask_first(mask);
8412d01c37dSTadeusz Struk 	struct hfi1_msix_entry *msix = container_of(notify,
8422d01c37dSTadeusz Struk 						    struct hfi1_msix_entry,
8432d01c37dSTadeusz Struk 						    notify);
8442d01c37dSTadeusz Struk 
8452d01c37dSTadeusz Struk 	/* Only one CPU configuration supported currently */
8462d01c37dSTadeusz Struk 	hfi1_update_sdma_affinity(msix, cpu);
8472d01c37dSTadeusz Struk }
8482d01c37dSTadeusz Struk 
8492d01c37dSTadeusz Struk static void hfi1_irq_notifier_release(struct kref *ref)
8502d01c37dSTadeusz Struk {
8512d01c37dSTadeusz Struk 	/*
8522d01c37dSTadeusz Struk 	 * This is required by affinity notifier. We don't have anything to
8532d01c37dSTadeusz Struk 	 * free here.
8542d01c37dSTadeusz Struk 	 */
8552d01c37dSTadeusz Struk }
8562d01c37dSTadeusz Struk 
8572d01c37dSTadeusz Struk static void hfi1_setup_sdma_notifier(struct hfi1_msix_entry *msix)
8582d01c37dSTadeusz Struk {
8592d01c37dSTadeusz Struk 	struct irq_affinity_notify *notify = &msix->notify;
8602d01c37dSTadeusz Struk 
861bb7dde87SMichael J. Ruhl 	notify->irq = msix->irq;
8622d01c37dSTadeusz Struk 	notify->notify = hfi1_irq_notifier_notify;
8632d01c37dSTadeusz Struk 	notify->release = hfi1_irq_notifier_release;
8642d01c37dSTadeusz Struk 
8652d01c37dSTadeusz Struk 	if (irq_set_affinity_notifier(notify->irq, notify))
8662d01c37dSTadeusz Struk 		pr_err("Failed to register sdma irq affinity notifier for irq %d\n",
8672d01c37dSTadeusz Struk 		       notify->irq);
8682d01c37dSTadeusz Struk }
8692d01c37dSTadeusz Struk 
8702d01c37dSTadeusz Struk static void hfi1_cleanup_sdma_notifier(struct hfi1_msix_entry *msix)
8712d01c37dSTadeusz Struk {
8722d01c37dSTadeusz Struk 	struct irq_affinity_notify *notify = &msix->notify;
8732d01c37dSTadeusz Struk 
8742d01c37dSTadeusz Struk 	if (irq_set_affinity_notifier(notify->irq, NULL))
8752d01c37dSTadeusz Struk 		pr_err("Failed to cleanup sdma irq affinity notifier for irq %d\n",
8762d01c37dSTadeusz Struk 		       notify->irq);
8772d01c37dSTadeusz Struk }
8782d01c37dSTadeusz Struk 
8792d01c37dSTadeusz Struk /*
880584d9577STadeusz Struk  * Function sets the irq affinity for msix.
881584d9577STadeusz Struk  * It *must* be called with node_affinity.lock held.
882584d9577STadeusz Struk  */
883584d9577STadeusz Struk static int get_irq_affinity(struct hfi1_devdata *dd,
884584d9577STadeusz Struk 			    struct hfi1_msix_entry *msix)
885f48ad614SDennis Dalessandro {
886f48ad614SDennis Dalessandro 	cpumask_var_t diff;
8874197344bSDennis Dalessandro 	struct hfi1_affinity_node *entry;
888d6373019SSebastian Sanchez 	struct cpu_mask_set *set = NULL;
889f48ad614SDennis Dalessandro 	struct sdma_engine *sde = NULL;
890f48ad614SDennis Dalessandro 	struct hfi1_ctxtdata *rcd = NULL;
891f48ad614SDennis Dalessandro 	char extra[64];
892f48ad614SDennis Dalessandro 	int cpu = -1;
893f48ad614SDennis Dalessandro 
894f48ad614SDennis Dalessandro 	extra[0] = '\0';
895f48ad614SDennis Dalessandro 	cpumask_clear(&msix->mask);
896f48ad614SDennis Dalessandro 
8974197344bSDennis Dalessandro 	entry = node_affinity_lookup(dd->node);
8984197344bSDennis Dalessandro 
899f48ad614SDennis Dalessandro 	switch (msix->type) {
900f48ad614SDennis Dalessandro 	case IRQ_SDMA:
901f48ad614SDennis Dalessandro 		sde = (struct sdma_engine *)msix->arg;
902f48ad614SDennis Dalessandro 		scnprintf(extra, 64, "engine %u", sde->this_idx);
9034197344bSDennis Dalessandro 		set = &entry->def_intr;
904f48ad614SDennis Dalessandro 		break;
905d6373019SSebastian Sanchez 	case IRQ_GENERAL:
906d6373019SSebastian Sanchez 		cpu = cpumask_first(&entry->general_intr_mask);
907d6373019SSebastian Sanchez 		break;
908f48ad614SDennis Dalessandro 	case IRQ_RCVCTXT:
909f48ad614SDennis Dalessandro 		rcd = (struct hfi1_ctxtdata *)msix->arg;
910d6373019SSebastian Sanchez 		if (rcd->ctxt == HFI1_CTRL_CTXT)
911d6373019SSebastian Sanchez 			cpu = cpumask_first(&entry->general_intr_mask);
912d6373019SSebastian Sanchez 		else
9134197344bSDennis Dalessandro 			set = &entry->rcv_intr;
914f48ad614SDennis Dalessandro 		scnprintf(extra, 64, "ctxt %u", rcd->ctxt);
915f48ad614SDennis Dalessandro 		break;
916f48ad614SDennis Dalessandro 	default:
917f48ad614SDennis Dalessandro 		dd_dev_err(dd, "Invalid IRQ type %d\n", msix->type);
918f48ad614SDennis Dalessandro 		return -EINVAL;
919f48ad614SDennis Dalessandro 	}
920f48ad614SDennis Dalessandro 
921f48ad614SDennis Dalessandro 	/*
922d6373019SSebastian Sanchez 	 * The general and control contexts are placed on a particular
923d6373019SSebastian Sanchez 	 * CPU, which is set above. Skip accounting for it. Everything else
924d6373019SSebastian Sanchez 	 * finds its CPU here.
925f48ad614SDennis Dalessandro 	 */
9264197344bSDennis Dalessandro 	if (cpu == -1 && set) {
92759482a14SSebastian Sanchez 		if (!zalloc_cpumask_var(&diff, GFP_KERNEL))
92859482a14SSebastian Sanchez 			return -ENOMEM;
92959482a14SSebastian Sanchez 
930cf38ea10SSebastian Sanchez 		cpu = cpu_mask_set_get_first(set, diff);
931cf38ea10SSebastian Sanchez 		if (cpu < 0) {
932cf38ea10SSebastian Sanchez 			free_cpumask_var(diff);
933cf38ea10SSebastian Sanchez 			dd_dev_err(dd, "Failure to obtain CPU for IRQ\n");
934cf38ea10SSebastian Sanchez 			return cpu;
935f48ad614SDennis Dalessandro 		}
93659482a14SSebastian Sanchez 
93759482a14SSebastian Sanchez 		free_cpumask_var(diff);
938f48ad614SDennis Dalessandro 	}
939f48ad614SDennis Dalessandro 
940f48ad614SDennis Dalessandro 	cpumask_set_cpu(cpu, &msix->mask);
941bb7dde87SMichael J. Ruhl 	dd_dev_info(dd, "IRQ: %u, type %s %s -> cpu: %d\n",
942bb7dde87SMichael J. Ruhl 		    msix->irq, irq_type_names[msix->type],
943f48ad614SDennis Dalessandro 		    extra, cpu);
944bb7dde87SMichael J. Ruhl 	irq_set_affinity_hint(msix->irq, &msix->mask);
945f48ad614SDennis Dalessandro 
9462d01c37dSTadeusz Struk 	if (msix->type == IRQ_SDMA) {
9472d01c37dSTadeusz Struk 		sde->cpu = cpu;
9482d01c37dSTadeusz Struk 		hfi1_setup_sdma_notifier(msix);
9492d01c37dSTadeusz Struk 	}
9502d01c37dSTadeusz Struk 
951f48ad614SDennis Dalessandro 	return 0;
952f48ad614SDennis Dalessandro }
953f48ad614SDennis Dalessandro 
954584d9577STadeusz Struk int hfi1_get_irq_affinity(struct hfi1_devdata *dd, struct hfi1_msix_entry *msix)
955584d9577STadeusz Struk {
956584d9577STadeusz Struk 	int ret;
957584d9577STadeusz Struk 
958584d9577STadeusz Struk 	mutex_lock(&node_affinity.lock);
959584d9577STadeusz Struk 	ret = get_irq_affinity(dd, msix);
960584d9577STadeusz Struk 	mutex_unlock(&node_affinity.lock);
961584d9577STadeusz Struk 	return ret;
962584d9577STadeusz Struk }
963584d9577STadeusz Struk 
964f48ad614SDennis Dalessandro void hfi1_put_irq_affinity(struct hfi1_devdata *dd,
965f48ad614SDennis Dalessandro 			   struct hfi1_msix_entry *msix)
966f48ad614SDennis Dalessandro {
967f48ad614SDennis Dalessandro 	struct cpu_mask_set *set = NULL;
968f48ad614SDennis Dalessandro 	struct hfi1_ctxtdata *rcd;
9694197344bSDennis Dalessandro 	struct hfi1_affinity_node *entry;
9704197344bSDennis Dalessandro 
971584d9577STadeusz Struk 	mutex_lock(&node_affinity.lock);
9724197344bSDennis Dalessandro 	entry = node_affinity_lookup(dd->node);
973f48ad614SDennis Dalessandro 
974f48ad614SDennis Dalessandro 	switch (msix->type) {
975f48ad614SDennis Dalessandro 	case IRQ_SDMA:
9764197344bSDennis Dalessandro 		set = &entry->def_intr;
9772d01c37dSTadeusz Struk 		hfi1_cleanup_sdma_notifier(msix);
978f48ad614SDennis Dalessandro 		break;
979d6373019SSebastian Sanchez 	case IRQ_GENERAL:
980b094a36fSSebastian Sanchez 		/* Don't do accounting for general contexts */
981d6373019SSebastian Sanchez 		break;
982f48ad614SDennis Dalessandro 	case IRQ_RCVCTXT:
983f48ad614SDennis Dalessandro 		rcd = (struct hfi1_ctxtdata *)msix->arg;
984d6373019SSebastian Sanchez 		/* Don't do accounting for control contexts */
985f48ad614SDennis Dalessandro 		if (rcd->ctxt != HFI1_CTRL_CTXT)
9864197344bSDennis Dalessandro 			set = &entry->rcv_intr;
987f48ad614SDennis Dalessandro 		break;
988f48ad614SDennis Dalessandro 	default:
989584d9577STadeusz Struk 		mutex_unlock(&node_affinity.lock);
990f48ad614SDennis Dalessandro 		return;
991f48ad614SDennis Dalessandro 	}
992f48ad614SDennis Dalessandro 
993f48ad614SDennis Dalessandro 	if (set) {
994f48ad614SDennis Dalessandro 		cpumask_andnot(&set->used, &set->used, &msix->mask);
995cf38ea10SSebastian Sanchez 		_cpu_mask_set_gen_dec(set);
996f48ad614SDennis Dalessandro 	}
997f48ad614SDennis Dalessandro 
998bb7dde87SMichael J. Ruhl 	irq_set_affinity_hint(msix->irq, NULL);
999f48ad614SDennis Dalessandro 	cpumask_clear(&msix->mask);
1000584d9577STadeusz Struk 	mutex_unlock(&node_affinity.lock);
1001f48ad614SDennis Dalessandro }
1002f48ad614SDennis Dalessandro 
1003b094a36fSSebastian Sanchez /* This should be called with node_affinity.lock held */
1004b094a36fSSebastian Sanchez static void find_hw_thread_mask(uint hw_thread_no, cpumask_var_t hw_thread_mask,
1005b094a36fSSebastian Sanchez 				struct hfi1_affinity_node_list *affinity)
1006f48ad614SDennis Dalessandro {
1007b094a36fSSebastian Sanchez 	int possible, curr_cpu, i;
1008b094a36fSSebastian Sanchez 	uint num_cores_per_socket = node_affinity.num_online_cpus /
1009b094a36fSSebastian Sanchez 					affinity->num_core_siblings /
1010b094a36fSSebastian Sanchez 						node_affinity.num_online_nodes;
1011b094a36fSSebastian Sanchez 
1012b094a36fSSebastian Sanchez 	cpumask_copy(hw_thread_mask, &affinity->proc.mask);
1013b094a36fSSebastian Sanchez 	if (affinity->num_core_siblings > 0) {
1014b094a36fSSebastian Sanchez 		/* Removing other siblings not needed for now */
1015b094a36fSSebastian Sanchez 		possible = cpumask_weight(hw_thread_mask);
1016b094a36fSSebastian Sanchez 		curr_cpu = cpumask_first(hw_thread_mask);
1017b094a36fSSebastian Sanchez 		for (i = 0;
1018b094a36fSSebastian Sanchez 		     i < num_cores_per_socket * node_affinity.num_online_nodes;
1019b094a36fSSebastian Sanchez 		     i++)
1020b094a36fSSebastian Sanchez 			curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
1021b094a36fSSebastian Sanchez 
1022b094a36fSSebastian Sanchez 		for (; i < possible; i++) {
1023b094a36fSSebastian Sanchez 			cpumask_clear_cpu(curr_cpu, hw_thread_mask);
1024b094a36fSSebastian Sanchez 			curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
1025b094a36fSSebastian Sanchez 		}
1026b094a36fSSebastian Sanchez 
1027b094a36fSSebastian Sanchez 		/* Identifying correct HW threads within physical cores */
1028b094a36fSSebastian Sanchez 		cpumask_shift_left(hw_thread_mask, hw_thread_mask,
1029b094a36fSSebastian Sanchez 				   num_cores_per_socket *
1030b094a36fSSebastian Sanchez 				   node_affinity.num_online_nodes *
1031b094a36fSSebastian Sanchez 				   hw_thread_no);
1032b094a36fSSebastian Sanchez 	}
1033b094a36fSSebastian Sanchez }
1034b094a36fSSebastian Sanchez 
1035b094a36fSSebastian Sanchez int hfi1_get_proc_affinity(int node)
1036b094a36fSSebastian Sanchez {
1037b094a36fSSebastian Sanchez 	int cpu = -1, ret, i;
10384197344bSDennis Dalessandro 	struct hfi1_affinity_node *entry;
1039b094a36fSSebastian Sanchez 	cpumask_var_t diff, hw_thread_mask, available_mask, intrs_mask;
1040f48ad614SDennis Dalessandro 	const struct cpumask *node_mask,
1041*3bd37062SSebastian Andrzej Siewior 		*proc_mask = current->cpus_ptr;
1042b094a36fSSebastian Sanchez 	struct hfi1_affinity_node_list *affinity = &node_affinity;
1043b094a36fSSebastian Sanchez 	struct cpu_mask_set *set = &affinity->proc;
1044f48ad614SDennis Dalessandro 
1045f48ad614SDennis Dalessandro 	/*
1046f48ad614SDennis Dalessandro 	 * check whether process/context affinity has already
1047f48ad614SDennis Dalessandro 	 * been set
1048f48ad614SDennis Dalessandro 	 */
1049*3bd37062SSebastian Andrzej Siewior 	if (current->nr_cpus_allowed == 1) {
1050f242d93aSLeon Romanovsky 		hfi1_cdbg(PROC, "PID %u %s affinity set to CPU %*pbl",
1051f242d93aSLeon Romanovsky 			  current->pid, current->comm,
1052f242d93aSLeon Romanovsky 			  cpumask_pr_args(proc_mask));
1053f48ad614SDennis Dalessandro 		/*
1054f48ad614SDennis Dalessandro 		 * Mark the pre-set CPU as used. This is atomic so we don't
1055f48ad614SDennis Dalessandro 		 * need the lock
1056f48ad614SDennis Dalessandro 		 */
1057f48ad614SDennis Dalessandro 		cpu = cpumask_first(proc_mask);
1058f48ad614SDennis Dalessandro 		cpumask_set_cpu(cpu, &set->used);
1059f48ad614SDennis Dalessandro 		goto done;
1060*3bd37062SSebastian Andrzej Siewior 	} else if (current->nr_cpus_allowed < cpumask_weight(&set->mask)) {
1061f242d93aSLeon Romanovsky 		hfi1_cdbg(PROC, "PID %u %s affinity set to CPU set(s) %*pbl",
1062f242d93aSLeon Romanovsky 			  current->pid, current->comm,
1063f242d93aSLeon Romanovsky 			  cpumask_pr_args(proc_mask));
1064f48ad614SDennis Dalessandro 		goto done;
1065f48ad614SDennis Dalessandro 	}
1066f48ad614SDennis Dalessandro 
1067f48ad614SDennis Dalessandro 	/*
1068f48ad614SDennis Dalessandro 	 * The process does not have a preset CPU affinity so find one to
1069b094a36fSSebastian Sanchez 	 * recommend using the following algorithm:
1070b094a36fSSebastian Sanchez 	 *
1071b094a36fSSebastian Sanchez 	 * For each user process that is opening a context on HFI Y:
1072b094a36fSSebastian Sanchez 	 *  a) If all cores are filled, reinitialize the bitmask
1073b094a36fSSebastian Sanchez 	 *  b) Fill real cores first, then HT cores (First set of HT
1074b094a36fSSebastian Sanchez 	 *     cores on all physical cores, then second set of HT core,
1075b094a36fSSebastian Sanchez 	 *     and, so on) in the following order:
1076b094a36fSSebastian Sanchez 	 *
1077b094a36fSSebastian Sanchez 	 *     1. Same NUMA node as HFI Y and not running an IRQ
1078b094a36fSSebastian Sanchez 	 *        handler
1079b094a36fSSebastian Sanchez 	 *     2. Same NUMA node as HFI Y and running an IRQ handler
1080b094a36fSSebastian Sanchez 	 *     3. Different NUMA node to HFI Y and not running an IRQ
1081b094a36fSSebastian Sanchez 	 *        handler
1082b094a36fSSebastian Sanchez 	 *     4. Different NUMA node to HFI Y and running an IRQ
1083b094a36fSSebastian Sanchez 	 *        handler
1084b094a36fSSebastian Sanchez 	 *  c) Mark core as filled in the bitmask. As user processes are
1085b094a36fSSebastian Sanchez 	 *     done, clear cores from the bitmask.
1086f48ad614SDennis Dalessandro 	 */
1087f48ad614SDennis Dalessandro 
1088f48ad614SDennis Dalessandro 	ret = zalloc_cpumask_var(&diff, GFP_KERNEL);
1089f48ad614SDennis Dalessandro 	if (!ret)
1090f48ad614SDennis Dalessandro 		goto done;
1091b094a36fSSebastian Sanchez 	ret = zalloc_cpumask_var(&hw_thread_mask, GFP_KERNEL);
1092f48ad614SDennis Dalessandro 	if (!ret)
1093f48ad614SDennis Dalessandro 		goto free_diff;
1094b094a36fSSebastian Sanchez 	ret = zalloc_cpumask_var(&available_mask, GFP_KERNEL);
1095f48ad614SDennis Dalessandro 	if (!ret)
1096b094a36fSSebastian Sanchez 		goto free_hw_thread_mask;
1097b094a36fSSebastian Sanchez 	ret = zalloc_cpumask_var(&intrs_mask, GFP_KERNEL);
1098b094a36fSSebastian Sanchez 	if (!ret)
1099b094a36fSSebastian Sanchez 		goto free_available_mask;
1100f48ad614SDennis Dalessandro 
1101584d9577STadeusz Struk 	mutex_lock(&affinity->lock);
1102f48ad614SDennis Dalessandro 	/*
1103b094a36fSSebastian Sanchez 	 * If we've used all available HW threads, clear the mask and start
1104f48ad614SDennis Dalessandro 	 * overloading.
1105f48ad614SDennis Dalessandro 	 */
1106cf38ea10SSebastian Sanchez 	_cpu_mask_set_gen_inc(set);
1107f48ad614SDennis Dalessandro 
1108d6373019SSebastian Sanchez 	/*
1109d6373019SSebastian Sanchez 	 * If NUMA node has CPUs used by interrupt handlers, include them in the
1110d6373019SSebastian Sanchez 	 * interrupt handler mask.
1111d6373019SSebastian Sanchez 	 */
1112d6373019SSebastian Sanchez 	entry = node_affinity_lookup(node);
1113d6373019SSebastian Sanchez 	if (entry) {
1114b094a36fSSebastian Sanchez 		cpumask_copy(intrs_mask, (entry->def_intr.gen ?
11154197344bSDennis Dalessandro 					  &entry->def_intr.mask :
11164197344bSDennis Dalessandro 					  &entry->def_intr.used));
1117b094a36fSSebastian Sanchez 		cpumask_or(intrs_mask, intrs_mask, (entry->rcv_intr.gen ?
11184197344bSDennis Dalessandro 						    &entry->rcv_intr.mask :
11194197344bSDennis Dalessandro 						    &entry->rcv_intr.used));
1120b094a36fSSebastian Sanchez 		cpumask_or(intrs_mask, intrs_mask, &entry->general_intr_mask);
1121d6373019SSebastian Sanchez 	}
1122f242d93aSLeon Romanovsky 	hfi1_cdbg(PROC, "CPUs used by interrupts: %*pbl",
1123b094a36fSSebastian Sanchez 		  cpumask_pr_args(intrs_mask));
1124b094a36fSSebastian Sanchez 
1125b094a36fSSebastian Sanchez 	cpumask_copy(hw_thread_mask, &set->mask);
1126f48ad614SDennis Dalessandro 
1127f48ad614SDennis Dalessandro 	/*
1128b094a36fSSebastian Sanchez 	 * If HT cores are enabled, identify which HW threads within the
1129b094a36fSSebastian Sanchez 	 * physical cores should be used.
1130f48ad614SDennis Dalessandro 	 */
1131b094a36fSSebastian Sanchez 	if (affinity->num_core_siblings > 0) {
1132b094a36fSSebastian Sanchez 		for (i = 0; i < affinity->num_core_siblings; i++) {
1133b094a36fSSebastian Sanchez 			find_hw_thread_mask(i, hw_thread_mask, affinity);
1134b094a36fSSebastian Sanchez 
1135b094a36fSSebastian Sanchez 			/*
1136b094a36fSSebastian Sanchez 			 * If there's at least one available core for this HW
1137b094a36fSSebastian Sanchez 			 * thread number, stop looking for a core.
1138b094a36fSSebastian Sanchez 			 *
1139b094a36fSSebastian Sanchez 			 * diff will always be not empty at least once in this
1140b094a36fSSebastian Sanchez 			 * loop as the used mask gets reset when
1141b094a36fSSebastian Sanchez 			 * (set->mask == set->used) before this loop.
1142b094a36fSSebastian Sanchez 			 */
1143b094a36fSSebastian Sanchez 			cpumask_andnot(diff, hw_thread_mask, &set->used);
1144b094a36fSSebastian Sanchez 			if (!cpumask_empty(diff))
1145b094a36fSSebastian Sanchez 				break;
1146b094a36fSSebastian Sanchez 		}
1147b094a36fSSebastian Sanchez 	}
1148b094a36fSSebastian Sanchez 	hfi1_cdbg(PROC, "Same available HW thread on all physical CPUs: %*pbl",
1149b094a36fSSebastian Sanchez 		  cpumask_pr_args(hw_thread_mask));
1150b094a36fSSebastian Sanchez 
1151f48ad614SDennis Dalessandro 	node_mask = cpumask_of_node(node);
1152b094a36fSSebastian Sanchez 	hfi1_cdbg(PROC, "Device on NUMA %u, CPUs %*pbl", node,
1153f242d93aSLeon Romanovsky 		  cpumask_pr_args(node_mask));
1154f48ad614SDennis Dalessandro 
1155b094a36fSSebastian Sanchez 	/* Get cpumask of available CPUs on preferred NUMA */
1156b094a36fSSebastian Sanchez 	cpumask_and(available_mask, hw_thread_mask, node_mask);
1157b094a36fSSebastian Sanchez 	cpumask_andnot(available_mask, available_mask, &set->used);
1158b094a36fSSebastian Sanchez 	hfi1_cdbg(PROC, "Available CPUs on NUMA %u: %*pbl", node,
1159b094a36fSSebastian Sanchez 		  cpumask_pr_args(available_mask));
1160f48ad614SDennis Dalessandro 
1161b094a36fSSebastian Sanchez 	/*
1162b094a36fSSebastian Sanchez 	 * At first, we don't want to place processes on the same
1163b094a36fSSebastian Sanchez 	 * CPUs as interrupt handlers. Then, CPUs running interrupt
1164b094a36fSSebastian Sanchez 	 * handlers are used.
1165b094a36fSSebastian Sanchez 	 *
1166b094a36fSSebastian Sanchez 	 * 1) If diff is not empty, then there are CPUs not running
1167b094a36fSSebastian Sanchez 	 *    non-interrupt handlers available, so diff gets copied
1168b094a36fSSebastian Sanchez 	 *    over to available_mask.
1169b094a36fSSebastian Sanchez 	 * 2) If diff is empty, then all CPUs not running interrupt
1170b094a36fSSebastian Sanchez 	 *    handlers are taken, so available_mask contains all
1171b094a36fSSebastian Sanchez 	 *    available CPUs running interrupt handlers.
1172b094a36fSSebastian Sanchez 	 * 3) If available_mask is empty, then all CPUs on the
1173b094a36fSSebastian Sanchez 	 *    preferred NUMA node are taken, so other NUMA nodes are
1174b094a36fSSebastian Sanchez 	 *    used for process assignments using the same method as
1175b094a36fSSebastian Sanchez 	 *    the preferred NUMA node.
1176b094a36fSSebastian Sanchez 	 */
1177b094a36fSSebastian Sanchez 	cpumask_andnot(diff, available_mask, intrs_mask);
1178b094a36fSSebastian Sanchez 	if (!cpumask_empty(diff))
1179b094a36fSSebastian Sanchez 		cpumask_copy(available_mask, diff);
1180b094a36fSSebastian Sanchez 
1181b094a36fSSebastian Sanchez 	/* If we don't have CPUs on the preferred node, use other NUMA nodes */
1182b094a36fSSebastian Sanchez 	if (cpumask_empty(available_mask)) {
1183b094a36fSSebastian Sanchez 		cpumask_andnot(available_mask, hw_thread_mask, &set->used);
1184b094a36fSSebastian Sanchez 		/* Excluding preferred NUMA cores */
1185b094a36fSSebastian Sanchez 		cpumask_andnot(available_mask, available_mask, node_mask);
1186b094a36fSSebastian Sanchez 		hfi1_cdbg(PROC,
1187b094a36fSSebastian Sanchez 			  "Preferred NUMA node cores are taken, cores available in other NUMA nodes: %*pbl",
1188b094a36fSSebastian Sanchez 			  cpumask_pr_args(available_mask));
1189f48ad614SDennis Dalessandro 
1190f48ad614SDennis Dalessandro 		/*
1191f48ad614SDennis Dalessandro 		 * At first, we don't want to place processes on the same
1192f48ad614SDennis Dalessandro 		 * CPUs as interrupt handlers.
1193f48ad614SDennis Dalessandro 		 */
1194b094a36fSSebastian Sanchez 		cpumask_andnot(diff, available_mask, intrs_mask);
1195f48ad614SDennis Dalessandro 		if (!cpumask_empty(diff))
1196b094a36fSSebastian Sanchez 			cpumask_copy(available_mask, diff);
1197f48ad614SDennis Dalessandro 	}
1198b094a36fSSebastian Sanchez 	hfi1_cdbg(PROC, "Possible CPUs for process: %*pbl",
1199b094a36fSSebastian Sanchez 		  cpumask_pr_args(available_mask));
1200f48ad614SDennis Dalessandro 
1201b094a36fSSebastian Sanchez 	cpu = cpumask_first(available_mask);
1202f48ad614SDennis Dalessandro 	if (cpu >= nr_cpu_ids) /* empty */
1203f48ad614SDennis Dalessandro 		cpu = -1;
1204f48ad614SDennis Dalessandro 	else
1205f48ad614SDennis Dalessandro 		cpumask_set_cpu(cpu, &set->used);
1206584d9577STadeusz Struk 
1207584d9577STadeusz Struk 	mutex_unlock(&affinity->lock);
1208b094a36fSSebastian Sanchez 	hfi1_cdbg(PROC, "Process assigned to CPU %d", cpu);
1209f48ad614SDennis Dalessandro 
1210b094a36fSSebastian Sanchez 	free_cpumask_var(intrs_mask);
1211b094a36fSSebastian Sanchez free_available_mask:
1212b094a36fSSebastian Sanchez 	free_cpumask_var(available_mask);
1213b094a36fSSebastian Sanchez free_hw_thread_mask:
1214b094a36fSSebastian Sanchez 	free_cpumask_var(hw_thread_mask);
1215f48ad614SDennis Dalessandro free_diff:
1216f48ad614SDennis Dalessandro 	free_cpumask_var(diff);
1217f48ad614SDennis Dalessandro done:
1218f48ad614SDennis Dalessandro 	return cpu;
1219f48ad614SDennis Dalessandro }
1220f48ad614SDennis Dalessandro 
1221b094a36fSSebastian Sanchez void hfi1_put_proc_affinity(int cpu)
1222f48ad614SDennis Dalessandro {
1223b094a36fSSebastian Sanchez 	struct hfi1_affinity_node_list *affinity = &node_affinity;
1224b094a36fSSebastian Sanchez 	struct cpu_mask_set *set = &affinity->proc;
1225f48ad614SDennis Dalessandro 
1226f48ad614SDennis Dalessandro 	if (cpu < 0)
1227f48ad614SDennis Dalessandro 		return;
1228584d9577STadeusz Struk 
1229584d9577STadeusz Struk 	mutex_lock(&affinity->lock);
1230cf38ea10SSebastian Sanchez 	cpu_mask_set_put(set, cpu);
1231b094a36fSSebastian Sanchez 	hfi1_cdbg(PROC, "Returning CPU %d for future process assignment", cpu);
1232584d9577STadeusz Struk 	mutex_unlock(&affinity->lock);
1233f48ad614SDennis Dalessandro }
1234