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> 51f48ad614SDennis Dalessandro 52f48ad614SDennis Dalessandro #include "hfi.h" 53f48ad614SDennis Dalessandro #include "affinity.h" 54f48ad614SDennis Dalessandro #include "sdma.h" 55f48ad614SDennis Dalessandro #include "trace.h" 56f48ad614SDennis Dalessandro 574197344bSDennis Dalessandro struct hfi1_affinity_node_list node_affinity = { 584197344bSDennis Dalessandro .list = LIST_HEAD_INIT(node_affinity.list), 59584d9577STadeusz Struk .lock = __MUTEX_INITIALIZER(node_affinity.lock) 604197344bSDennis Dalessandro }; 614197344bSDennis Dalessandro 62f48ad614SDennis Dalessandro /* Name of IRQ types, indexed by enum irq_type */ 63f48ad614SDennis Dalessandro static const char * const irq_type_names[] = { 64f48ad614SDennis Dalessandro "SDMA", 65f48ad614SDennis Dalessandro "RCVCTXT", 66f48ad614SDennis Dalessandro "GENERAL", 67f48ad614SDennis Dalessandro "OTHER", 68f48ad614SDennis Dalessandro }; 69f48ad614SDennis Dalessandro 70d6373019SSebastian Sanchez /* Per NUMA node count of HFI devices */ 71d6373019SSebastian Sanchez static unsigned int *hfi1_per_node_cntr; 72d6373019SSebastian Sanchez 73f48ad614SDennis Dalessandro static inline void init_cpu_mask_set(struct cpu_mask_set *set) 74f48ad614SDennis Dalessandro { 75f48ad614SDennis Dalessandro cpumask_clear(&set->mask); 76f48ad614SDennis Dalessandro cpumask_clear(&set->used); 77f48ad614SDennis Dalessandro set->gen = 0; 78f48ad614SDennis Dalessandro } 79f48ad614SDennis Dalessandro 80cf38ea10SSebastian Sanchez /* Increment generation of CPU set if needed */ 81cf38ea10SSebastian Sanchez static void _cpu_mask_set_gen_inc(struct cpu_mask_set *set) 82cf38ea10SSebastian Sanchez { 83cf38ea10SSebastian Sanchez if (cpumask_equal(&set->mask, &set->used)) { 84cf38ea10SSebastian Sanchez /* 85cf38ea10SSebastian Sanchez * We've used up all the CPUs, bump up the generation 86cf38ea10SSebastian Sanchez * and reset the 'used' map 87cf38ea10SSebastian Sanchez */ 88cf38ea10SSebastian Sanchez set->gen++; 89cf38ea10SSebastian Sanchez cpumask_clear(&set->used); 90cf38ea10SSebastian Sanchez } 91cf38ea10SSebastian Sanchez } 92cf38ea10SSebastian Sanchez 93cf38ea10SSebastian Sanchez static void _cpu_mask_set_gen_dec(struct cpu_mask_set *set) 94cf38ea10SSebastian Sanchez { 95cf38ea10SSebastian Sanchez if (cpumask_empty(&set->used) && set->gen) { 96cf38ea10SSebastian Sanchez set->gen--; 97cf38ea10SSebastian Sanchez cpumask_copy(&set->used, &set->mask); 98cf38ea10SSebastian Sanchez } 99cf38ea10SSebastian Sanchez } 100cf38ea10SSebastian Sanchez 101cf38ea10SSebastian Sanchez /* Get the first CPU from the list of unused CPUs in a CPU set data structure */ 102cf38ea10SSebastian Sanchez static int cpu_mask_set_get_first(struct cpu_mask_set *set, cpumask_var_t diff) 103cf38ea10SSebastian Sanchez { 104cf38ea10SSebastian Sanchez int cpu; 105cf38ea10SSebastian Sanchez 106cf38ea10SSebastian Sanchez if (!diff || !set) 107cf38ea10SSebastian Sanchez return -EINVAL; 108cf38ea10SSebastian Sanchez 109cf38ea10SSebastian Sanchez _cpu_mask_set_gen_inc(set); 110cf38ea10SSebastian Sanchez 111cf38ea10SSebastian Sanchez /* Find out CPUs left in CPU mask */ 112cf38ea10SSebastian Sanchez cpumask_andnot(diff, &set->mask, &set->used); 113cf38ea10SSebastian Sanchez 114cf38ea10SSebastian Sanchez cpu = cpumask_first(diff); 115cf38ea10SSebastian Sanchez if (cpu >= nr_cpu_ids) /* empty */ 116cf38ea10SSebastian Sanchez cpu = -EINVAL; 117cf38ea10SSebastian Sanchez else 118cf38ea10SSebastian Sanchez cpumask_set_cpu(cpu, &set->used); 119cf38ea10SSebastian Sanchez 120cf38ea10SSebastian Sanchez return cpu; 121cf38ea10SSebastian Sanchez } 122cf38ea10SSebastian Sanchez 123cf38ea10SSebastian Sanchez static void cpu_mask_set_put(struct cpu_mask_set *set, int cpu) 124cf38ea10SSebastian Sanchez { 125cf38ea10SSebastian Sanchez if (!set) 126cf38ea10SSebastian Sanchez return; 127cf38ea10SSebastian Sanchez 128cf38ea10SSebastian Sanchez cpumask_clear_cpu(cpu, &set->used); 129cf38ea10SSebastian Sanchez _cpu_mask_set_gen_dec(set); 130cf38ea10SSebastian Sanchez } 131cf38ea10SSebastian Sanchez 132f48ad614SDennis Dalessandro /* Initialize non-HT cpu cores mask */ 1334197344bSDennis Dalessandro void init_real_cpu_mask(void) 134f48ad614SDennis Dalessandro { 135f48ad614SDennis Dalessandro int possible, curr_cpu, i, ht; 136f48ad614SDennis Dalessandro 1374197344bSDennis Dalessandro cpumask_clear(&node_affinity.real_cpu_mask); 138f48ad614SDennis Dalessandro 139f48ad614SDennis Dalessandro /* Start with cpu online mask as the real cpu mask */ 1404197344bSDennis Dalessandro cpumask_copy(&node_affinity.real_cpu_mask, cpu_online_mask); 141f48ad614SDennis Dalessandro 142f48ad614SDennis Dalessandro /* 143f48ad614SDennis Dalessandro * Remove HT cores from the real cpu mask. Do this in two steps below. 144f48ad614SDennis Dalessandro */ 1454197344bSDennis Dalessandro possible = cpumask_weight(&node_affinity.real_cpu_mask); 146f48ad614SDennis Dalessandro ht = cpumask_weight(topology_sibling_cpumask( 1474197344bSDennis Dalessandro cpumask_first(&node_affinity.real_cpu_mask))); 148f48ad614SDennis Dalessandro /* 149f48ad614SDennis Dalessandro * Step 1. Skip over the first N HT siblings and use them as the 150f48ad614SDennis Dalessandro * "real" cores. Assumes that HT cores are not enumerated in 151f48ad614SDennis Dalessandro * succession (except in the single core case). 152f48ad614SDennis Dalessandro */ 1534197344bSDennis Dalessandro curr_cpu = cpumask_first(&node_affinity.real_cpu_mask); 154f48ad614SDennis Dalessandro for (i = 0; i < possible / ht; i++) 1554197344bSDennis Dalessandro curr_cpu = cpumask_next(curr_cpu, &node_affinity.real_cpu_mask); 156f48ad614SDennis Dalessandro /* 157f48ad614SDennis Dalessandro * Step 2. Remove the remaining HT siblings. Use cpumask_next() to 158f48ad614SDennis Dalessandro * skip any gaps. 159f48ad614SDennis Dalessandro */ 160f48ad614SDennis Dalessandro for (; i < possible; i++) { 1614197344bSDennis Dalessandro cpumask_clear_cpu(curr_cpu, &node_affinity.real_cpu_mask); 1624197344bSDennis Dalessandro curr_cpu = cpumask_next(curr_cpu, &node_affinity.real_cpu_mask); 1634197344bSDennis Dalessandro } 164f48ad614SDennis Dalessandro } 165f48ad614SDennis Dalessandro 166d6373019SSebastian Sanchez int node_affinity_init(void) 1674197344bSDennis Dalessandro { 168d6373019SSebastian Sanchez int node; 169d6373019SSebastian Sanchez struct pci_dev *dev = NULL; 170d6373019SSebastian Sanchez const struct pci_device_id *ids = hfi1_pci_tbl; 171d6373019SSebastian Sanchez 172b094a36fSSebastian Sanchez cpumask_clear(&node_affinity.proc.used); 1734197344bSDennis Dalessandro cpumask_copy(&node_affinity.proc.mask, cpu_online_mask); 174b094a36fSSebastian Sanchez 175b094a36fSSebastian Sanchez node_affinity.proc.gen = 0; 176b094a36fSSebastian Sanchez node_affinity.num_core_siblings = 177b094a36fSSebastian Sanchez cpumask_weight(topology_sibling_cpumask( 178b094a36fSSebastian Sanchez cpumask_first(&node_affinity.proc.mask) 179b094a36fSSebastian Sanchez )); 1809d8145a6SHarish Chegondi node_affinity.num_possible_nodes = num_possible_nodes(); 181b094a36fSSebastian Sanchez node_affinity.num_online_nodes = num_online_nodes(); 182b094a36fSSebastian Sanchez node_affinity.num_online_cpus = num_online_cpus(); 183b094a36fSSebastian Sanchez 1844197344bSDennis Dalessandro /* 1854197344bSDennis Dalessandro * The real cpu mask is part of the affinity struct but it has to be 1864197344bSDennis Dalessandro * initialized early. It is needed to calculate the number of user 1874197344bSDennis Dalessandro * contexts in set_up_context_variables(). 1884197344bSDennis Dalessandro */ 1894197344bSDennis Dalessandro init_real_cpu_mask(); 190d6373019SSebastian Sanchez 1919d8145a6SHarish Chegondi hfi1_per_node_cntr = kcalloc(node_affinity.num_possible_nodes, 192d6373019SSebastian Sanchez sizeof(*hfi1_per_node_cntr), GFP_KERNEL); 193d6373019SSebastian Sanchez if (!hfi1_per_node_cntr) 194d6373019SSebastian Sanchez return -ENOMEM; 195d6373019SSebastian Sanchez 196d6373019SSebastian Sanchez while (ids->vendor) { 197d6373019SSebastian Sanchez dev = NULL; 198d6373019SSebastian Sanchez while ((dev = pci_get_device(ids->vendor, ids->device, dev))) { 199d6373019SSebastian Sanchez node = pcibus_to_node(dev->bus); 200d6373019SSebastian Sanchez if (node < 0) 201c513de49SMichael J. Ruhl goto out; 202d6373019SSebastian Sanchez 203d6373019SSebastian Sanchez hfi1_per_node_cntr[node]++; 204d6373019SSebastian Sanchez } 205d6373019SSebastian Sanchez ids++; 206d6373019SSebastian Sanchez } 207d6373019SSebastian Sanchez 208d6373019SSebastian Sanchez return 0; 209c513de49SMichael J. Ruhl 210c513de49SMichael J. Ruhl out: 211c513de49SMichael J. Ruhl /* 212c513de49SMichael J. Ruhl * Invalid PCI NUMA node information found, note it, and populate 213c513de49SMichael J. Ruhl * our database 1:1. 214c513de49SMichael J. Ruhl */ 215c513de49SMichael J. Ruhl pr_err("HFI: Invalid PCI NUMA node. Performance may be affected\n"); 216c513de49SMichael J. Ruhl pr_err("HFI: System BIOS may need to be upgraded\n"); 217c513de49SMichael J. Ruhl for (node = 0; node < node_affinity.num_possible_nodes; node++) 218c513de49SMichael J. Ruhl hfi1_per_node_cntr[node] = 1; 219c513de49SMichael J. Ruhl 220c513de49SMichael J. Ruhl return 0; 2214197344bSDennis Dalessandro } 2224197344bSDennis Dalessandro 2235d18ee67SSebastian Sanchez static void node_affinity_destroy(struct hfi1_affinity_node *entry) 2245d18ee67SSebastian Sanchez { 2255d18ee67SSebastian Sanchez free_percpu(entry->comp_vect_affinity); 2265d18ee67SSebastian Sanchez kfree(entry); 2275d18ee67SSebastian Sanchez } 2285d18ee67SSebastian Sanchez 2295d18ee67SSebastian Sanchez void node_affinity_destroy_all(void) 2304197344bSDennis Dalessandro { 2314197344bSDennis Dalessandro struct list_head *pos, *q; 2324197344bSDennis Dalessandro struct hfi1_affinity_node *entry; 2334197344bSDennis Dalessandro 234584d9577STadeusz Struk mutex_lock(&node_affinity.lock); 2354197344bSDennis Dalessandro list_for_each_safe(pos, q, &node_affinity.list) { 2364197344bSDennis Dalessandro entry = list_entry(pos, struct hfi1_affinity_node, 2374197344bSDennis Dalessandro list); 2384197344bSDennis Dalessandro list_del(pos); 2395d18ee67SSebastian Sanchez node_affinity_destroy(entry); 2404197344bSDennis Dalessandro } 241584d9577STadeusz Struk mutex_unlock(&node_affinity.lock); 242d6373019SSebastian Sanchez kfree(hfi1_per_node_cntr); 2434197344bSDennis Dalessandro } 2444197344bSDennis Dalessandro 2454197344bSDennis Dalessandro static struct hfi1_affinity_node *node_affinity_allocate(int node) 2464197344bSDennis Dalessandro { 2474197344bSDennis Dalessandro struct hfi1_affinity_node *entry; 2484197344bSDennis Dalessandro 2494197344bSDennis Dalessandro entry = kzalloc(sizeof(*entry), GFP_KERNEL); 2504197344bSDennis Dalessandro if (!entry) 2514197344bSDennis Dalessandro return NULL; 2524197344bSDennis Dalessandro entry->node = node; 2535d18ee67SSebastian Sanchez entry->comp_vect_affinity = alloc_percpu(u16); 2544197344bSDennis Dalessandro INIT_LIST_HEAD(&entry->list); 2554197344bSDennis Dalessandro 2564197344bSDennis Dalessandro return entry; 2574197344bSDennis Dalessandro } 2584197344bSDennis Dalessandro 2594197344bSDennis Dalessandro /* 2604197344bSDennis Dalessandro * It appends an entry to the list. 2614197344bSDennis Dalessandro * It *must* be called with node_affinity.lock held. 2624197344bSDennis Dalessandro */ 2634197344bSDennis Dalessandro static void node_affinity_add_tail(struct hfi1_affinity_node *entry) 2644197344bSDennis Dalessandro { 2654197344bSDennis Dalessandro list_add_tail(&entry->list, &node_affinity.list); 2664197344bSDennis Dalessandro } 2674197344bSDennis Dalessandro 2684197344bSDennis Dalessandro /* It must be called with node_affinity.lock held */ 2694197344bSDennis Dalessandro static struct hfi1_affinity_node *node_affinity_lookup(int node) 2704197344bSDennis Dalessandro { 2714197344bSDennis Dalessandro struct list_head *pos; 2724197344bSDennis Dalessandro struct hfi1_affinity_node *entry; 2734197344bSDennis Dalessandro 2744197344bSDennis Dalessandro list_for_each(pos, &node_affinity.list) { 2754197344bSDennis Dalessandro entry = list_entry(pos, struct hfi1_affinity_node, list); 2764197344bSDennis Dalessandro if (entry->node == node) 2774197344bSDennis Dalessandro return entry; 2784197344bSDennis Dalessandro } 2794197344bSDennis Dalessandro 2804197344bSDennis Dalessandro return NULL; 281f48ad614SDennis Dalessandro } 282f48ad614SDennis Dalessandro 2835d18ee67SSebastian Sanchez static int per_cpu_affinity_get(cpumask_var_t possible_cpumask, 2845d18ee67SSebastian Sanchez u16 __percpu *comp_vect_affinity) 2855d18ee67SSebastian Sanchez { 2865d18ee67SSebastian Sanchez int curr_cpu; 2875d18ee67SSebastian Sanchez u16 cntr; 2885d18ee67SSebastian Sanchez u16 prev_cntr; 2895d18ee67SSebastian Sanchez int ret_cpu; 2905d18ee67SSebastian Sanchez 2915d18ee67SSebastian Sanchez if (!possible_cpumask) { 2925d18ee67SSebastian Sanchez ret_cpu = -EINVAL; 2935d18ee67SSebastian Sanchez goto fail; 2945d18ee67SSebastian Sanchez } 2955d18ee67SSebastian Sanchez 2965d18ee67SSebastian Sanchez if (!comp_vect_affinity) { 2975d18ee67SSebastian Sanchez ret_cpu = -EINVAL; 2985d18ee67SSebastian Sanchez goto fail; 2995d18ee67SSebastian Sanchez } 3005d18ee67SSebastian Sanchez 3015d18ee67SSebastian Sanchez ret_cpu = cpumask_first(possible_cpumask); 3025d18ee67SSebastian Sanchez if (ret_cpu >= nr_cpu_ids) { 3035d18ee67SSebastian Sanchez ret_cpu = -EINVAL; 3045d18ee67SSebastian Sanchez goto fail; 3055d18ee67SSebastian Sanchez } 3065d18ee67SSebastian Sanchez 3075d18ee67SSebastian Sanchez prev_cntr = *per_cpu_ptr(comp_vect_affinity, ret_cpu); 3085d18ee67SSebastian Sanchez for_each_cpu(curr_cpu, possible_cpumask) { 3095d18ee67SSebastian Sanchez cntr = *per_cpu_ptr(comp_vect_affinity, curr_cpu); 3105d18ee67SSebastian Sanchez 3115d18ee67SSebastian Sanchez if (cntr < prev_cntr) { 3125d18ee67SSebastian Sanchez ret_cpu = curr_cpu; 3135d18ee67SSebastian Sanchez prev_cntr = cntr; 3145d18ee67SSebastian Sanchez } 3155d18ee67SSebastian Sanchez } 3165d18ee67SSebastian Sanchez 3175d18ee67SSebastian Sanchez *per_cpu_ptr(comp_vect_affinity, ret_cpu) += 1; 3185d18ee67SSebastian Sanchez 3195d18ee67SSebastian Sanchez fail: 3205d18ee67SSebastian Sanchez return ret_cpu; 3215d18ee67SSebastian Sanchez } 3225d18ee67SSebastian Sanchez 3235d18ee67SSebastian Sanchez static int per_cpu_affinity_put_max(cpumask_var_t possible_cpumask, 3245d18ee67SSebastian Sanchez u16 __percpu *comp_vect_affinity) 3255d18ee67SSebastian Sanchez { 3265d18ee67SSebastian Sanchez int curr_cpu; 3275d18ee67SSebastian Sanchez int max_cpu; 3285d18ee67SSebastian Sanchez u16 cntr; 3295d18ee67SSebastian Sanchez u16 prev_cntr; 3305d18ee67SSebastian Sanchez 3315d18ee67SSebastian Sanchez if (!possible_cpumask) 3325d18ee67SSebastian Sanchez return -EINVAL; 3335d18ee67SSebastian Sanchez 3345d18ee67SSebastian Sanchez if (!comp_vect_affinity) 3355d18ee67SSebastian Sanchez return -EINVAL; 3365d18ee67SSebastian Sanchez 3375d18ee67SSebastian Sanchez max_cpu = cpumask_first(possible_cpumask); 3385d18ee67SSebastian Sanchez if (max_cpu >= nr_cpu_ids) 3395d18ee67SSebastian Sanchez return -EINVAL; 3405d18ee67SSebastian Sanchez 3415d18ee67SSebastian Sanchez prev_cntr = *per_cpu_ptr(comp_vect_affinity, max_cpu); 3425d18ee67SSebastian Sanchez for_each_cpu(curr_cpu, possible_cpumask) { 3435d18ee67SSebastian Sanchez cntr = *per_cpu_ptr(comp_vect_affinity, curr_cpu); 3445d18ee67SSebastian Sanchez 3455d18ee67SSebastian Sanchez if (cntr > prev_cntr) { 3465d18ee67SSebastian Sanchez max_cpu = curr_cpu; 3475d18ee67SSebastian Sanchez prev_cntr = cntr; 3485d18ee67SSebastian Sanchez } 3495d18ee67SSebastian Sanchez } 3505d18ee67SSebastian Sanchez 3515d18ee67SSebastian Sanchez *per_cpu_ptr(comp_vect_affinity, max_cpu) -= 1; 3525d18ee67SSebastian Sanchez 3535d18ee67SSebastian Sanchez return max_cpu; 3545d18ee67SSebastian Sanchez } 3555d18ee67SSebastian Sanchez 3565d18ee67SSebastian Sanchez /* 3575d18ee67SSebastian Sanchez * Non-interrupt CPUs are used first, then interrupt CPUs. 3585d18ee67SSebastian Sanchez * Two already allocated cpu masks must be passed. 3595d18ee67SSebastian Sanchez */ 3605d18ee67SSebastian Sanchez static int _dev_comp_vect_cpu_get(struct hfi1_devdata *dd, 3615d18ee67SSebastian Sanchez struct hfi1_affinity_node *entry, 3625d18ee67SSebastian Sanchez cpumask_var_t non_intr_cpus, 3635d18ee67SSebastian Sanchez cpumask_var_t available_cpus) 3645d18ee67SSebastian Sanchez __must_hold(&node_affinity.lock) 3655d18ee67SSebastian Sanchez { 3665d18ee67SSebastian Sanchez int cpu; 3675d18ee67SSebastian Sanchez struct cpu_mask_set *set = dd->comp_vect; 3685d18ee67SSebastian Sanchez 3695d18ee67SSebastian Sanchez lockdep_assert_held(&node_affinity.lock); 3705d18ee67SSebastian Sanchez if (!non_intr_cpus) { 3715d18ee67SSebastian Sanchez cpu = -1; 3725d18ee67SSebastian Sanchez goto fail; 3735d18ee67SSebastian Sanchez } 3745d18ee67SSebastian Sanchez 3755d18ee67SSebastian Sanchez if (!available_cpus) { 3765d18ee67SSebastian Sanchez cpu = -1; 3775d18ee67SSebastian Sanchez goto fail; 3785d18ee67SSebastian Sanchez } 3795d18ee67SSebastian Sanchez 3805d18ee67SSebastian Sanchez /* Available CPUs for pinning completion vectors */ 3815d18ee67SSebastian Sanchez _cpu_mask_set_gen_inc(set); 3825d18ee67SSebastian Sanchez cpumask_andnot(available_cpus, &set->mask, &set->used); 3835d18ee67SSebastian Sanchez 3845d18ee67SSebastian Sanchez /* Available CPUs without SDMA engine interrupts */ 3855d18ee67SSebastian Sanchez cpumask_andnot(non_intr_cpus, available_cpus, 3865d18ee67SSebastian Sanchez &entry->def_intr.used); 3875d18ee67SSebastian Sanchez 3885d18ee67SSebastian Sanchez /* If there are non-interrupt CPUs available, use them first */ 3895d18ee67SSebastian Sanchez if (!cpumask_empty(non_intr_cpus)) 3905d18ee67SSebastian Sanchez cpu = cpumask_first(non_intr_cpus); 3915d18ee67SSebastian Sanchez else /* Otherwise, use interrupt CPUs */ 3925d18ee67SSebastian Sanchez cpu = cpumask_first(available_cpus); 3935d18ee67SSebastian Sanchez 3945d18ee67SSebastian Sanchez if (cpu >= nr_cpu_ids) { /* empty */ 3955d18ee67SSebastian Sanchez cpu = -1; 3965d18ee67SSebastian Sanchez goto fail; 3975d18ee67SSebastian Sanchez } 3985d18ee67SSebastian Sanchez cpumask_set_cpu(cpu, &set->used); 3995d18ee67SSebastian Sanchez 4005d18ee67SSebastian Sanchez fail: 4015d18ee67SSebastian Sanchez return cpu; 4025d18ee67SSebastian Sanchez } 4035d18ee67SSebastian Sanchez 4045d18ee67SSebastian Sanchez static void _dev_comp_vect_cpu_put(struct hfi1_devdata *dd, int cpu) 4055d18ee67SSebastian Sanchez { 4065d18ee67SSebastian Sanchez struct cpu_mask_set *set = dd->comp_vect; 4075d18ee67SSebastian Sanchez 4085d18ee67SSebastian Sanchez if (cpu < 0) 4095d18ee67SSebastian Sanchez return; 4105d18ee67SSebastian Sanchez 4115d18ee67SSebastian Sanchez cpu_mask_set_put(set, cpu); 4125d18ee67SSebastian Sanchez } 4135d18ee67SSebastian Sanchez 4145d18ee67SSebastian Sanchez /* _dev_comp_vect_mappings_destroy() is reentrant */ 4155d18ee67SSebastian Sanchez static void _dev_comp_vect_mappings_destroy(struct hfi1_devdata *dd) 4165d18ee67SSebastian Sanchez { 4175d18ee67SSebastian Sanchez int i, cpu; 4185d18ee67SSebastian Sanchez 4195d18ee67SSebastian Sanchez if (!dd->comp_vect_mappings) 4205d18ee67SSebastian Sanchez return; 4215d18ee67SSebastian Sanchez 4225d18ee67SSebastian Sanchez for (i = 0; i < dd->comp_vect_possible_cpus; i++) { 4235d18ee67SSebastian Sanchez cpu = dd->comp_vect_mappings[i]; 4245d18ee67SSebastian Sanchez _dev_comp_vect_cpu_put(dd, cpu); 4255d18ee67SSebastian Sanchez dd->comp_vect_mappings[i] = -1; 4265d18ee67SSebastian Sanchez hfi1_cdbg(AFFINITY, 4275d18ee67SSebastian Sanchez "[%s] Release CPU %d from completion vector %d", 4285d18ee67SSebastian Sanchez rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), cpu, i); 4295d18ee67SSebastian Sanchez } 4305d18ee67SSebastian Sanchez 4315d18ee67SSebastian Sanchez kfree(dd->comp_vect_mappings); 4325d18ee67SSebastian Sanchez dd->comp_vect_mappings = NULL; 4335d18ee67SSebastian Sanchez } 4345d18ee67SSebastian Sanchez 4355d18ee67SSebastian Sanchez /* 4365d18ee67SSebastian Sanchez * This function creates the table for looking up CPUs for completion vectors. 4375d18ee67SSebastian Sanchez * num_comp_vectors needs to have been initilized before calling this function. 4385d18ee67SSebastian Sanchez */ 4395d18ee67SSebastian Sanchez static int _dev_comp_vect_mappings_create(struct hfi1_devdata *dd, 4405d18ee67SSebastian Sanchez struct hfi1_affinity_node *entry) 4415d18ee67SSebastian Sanchez __must_hold(&node_affinity.lock) 4425d18ee67SSebastian Sanchez { 4435d18ee67SSebastian Sanchez int i, cpu, ret; 4445d18ee67SSebastian Sanchez cpumask_var_t non_intr_cpus; 4455d18ee67SSebastian Sanchez cpumask_var_t available_cpus; 4465d18ee67SSebastian Sanchez 4475d18ee67SSebastian Sanchez lockdep_assert_held(&node_affinity.lock); 4485d18ee67SSebastian Sanchez 4495d18ee67SSebastian Sanchez if (!zalloc_cpumask_var(&non_intr_cpus, GFP_KERNEL)) 4505d18ee67SSebastian Sanchez return -ENOMEM; 4515d18ee67SSebastian Sanchez 4525d18ee67SSebastian Sanchez if (!zalloc_cpumask_var(&available_cpus, GFP_KERNEL)) { 4535d18ee67SSebastian Sanchez free_cpumask_var(non_intr_cpus); 4545d18ee67SSebastian Sanchez return -ENOMEM; 4555d18ee67SSebastian Sanchez } 4565d18ee67SSebastian Sanchez 4575d18ee67SSebastian Sanchez dd->comp_vect_mappings = kcalloc(dd->comp_vect_possible_cpus, 4585d18ee67SSebastian Sanchez sizeof(*dd->comp_vect_mappings), 4595d18ee67SSebastian Sanchez GFP_KERNEL); 4605d18ee67SSebastian Sanchez if (!dd->comp_vect_mappings) { 4615d18ee67SSebastian Sanchez ret = -ENOMEM; 4625d18ee67SSebastian Sanchez goto fail; 4635d18ee67SSebastian Sanchez } 4645d18ee67SSebastian Sanchez for (i = 0; i < dd->comp_vect_possible_cpus; i++) 4655d18ee67SSebastian Sanchez dd->comp_vect_mappings[i] = -1; 4665d18ee67SSebastian Sanchez 4675d18ee67SSebastian Sanchez for (i = 0; i < dd->comp_vect_possible_cpus; i++) { 4685d18ee67SSebastian Sanchez cpu = _dev_comp_vect_cpu_get(dd, entry, non_intr_cpus, 4695d18ee67SSebastian Sanchez available_cpus); 4705d18ee67SSebastian Sanchez if (cpu < 0) { 4715d18ee67SSebastian Sanchez ret = -EINVAL; 4725d18ee67SSebastian Sanchez goto fail; 4735d18ee67SSebastian Sanchez } 4745d18ee67SSebastian Sanchez 4755d18ee67SSebastian Sanchez dd->comp_vect_mappings[i] = cpu; 4765d18ee67SSebastian Sanchez hfi1_cdbg(AFFINITY, 4775d18ee67SSebastian Sanchez "[%s] Completion Vector %d -> CPU %d", 4785d18ee67SSebastian Sanchez rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), i, cpu); 4795d18ee67SSebastian Sanchez } 4805d18ee67SSebastian Sanchez 4815d18ee67SSebastian Sanchez return 0; 4825d18ee67SSebastian Sanchez 4835d18ee67SSebastian Sanchez fail: 4845d18ee67SSebastian Sanchez free_cpumask_var(available_cpus); 4855d18ee67SSebastian Sanchez free_cpumask_var(non_intr_cpus); 4865d18ee67SSebastian Sanchez _dev_comp_vect_mappings_destroy(dd); 4875d18ee67SSebastian Sanchez 4885d18ee67SSebastian Sanchez return ret; 4895d18ee67SSebastian Sanchez } 4905d18ee67SSebastian Sanchez 4915d18ee67SSebastian Sanchez int hfi1_comp_vectors_set_up(struct hfi1_devdata *dd) 4925d18ee67SSebastian Sanchez { 4935d18ee67SSebastian Sanchez int ret; 4945d18ee67SSebastian Sanchez struct hfi1_affinity_node *entry; 4955d18ee67SSebastian Sanchez 4965d18ee67SSebastian Sanchez mutex_lock(&node_affinity.lock); 4975d18ee67SSebastian Sanchez entry = node_affinity_lookup(dd->node); 4985d18ee67SSebastian Sanchez if (!entry) { 4995d18ee67SSebastian Sanchez ret = -EINVAL; 5005d18ee67SSebastian Sanchez goto unlock; 5015d18ee67SSebastian Sanchez } 5025d18ee67SSebastian Sanchez ret = _dev_comp_vect_mappings_create(dd, entry); 5035d18ee67SSebastian Sanchez unlock: 5045d18ee67SSebastian Sanchez mutex_unlock(&node_affinity.lock); 5055d18ee67SSebastian Sanchez 5065d18ee67SSebastian Sanchez return ret; 5075d18ee67SSebastian Sanchez } 5085d18ee67SSebastian Sanchez 5095d18ee67SSebastian Sanchez void hfi1_comp_vectors_clean_up(struct hfi1_devdata *dd) 5105d18ee67SSebastian Sanchez { 5115d18ee67SSebastian Sanchez _dev_comp_vect_mappings_destroy(dd); 5125d18ee67SSebastian Sanchez } 5135d18ee67SSebastian Sanchez 5145d18ee67SSebastian Sanchez int hfi1_comp_vect_mappings_lookup(struct rvt_dev_info *rdi, int comp_vect) 5155d18ee67SSebastian Sanchez { 5165d18ee67SSebastian Sanchez struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi); 5175d18ee67SSebastian Sanchez struct hfi1_devdata *dd = dd_from_dev(verbs_dev); 5185d18ee67SSebastian Sanchez 5195d18ee67SSebastian Sanchez if (!dd->comp_vect_mappings) 5205d18ee67SSebastian Sanchez return -EINVAL; 5215d18ee67SSebastian Sanchez if (comp_vect >= dd->comp_vect_possible_cpus) 5225d18ee67SSebastian Sanchez return -EINVAL; 5235d18ee67SSebastian Sanchez 5245d18ee67SSebastian Sanchez return dd->comp_vect_mappings[comp_vect]; 5255d18ee67SSebastian Sanchez } 5265d18ee67SSebastian Sanchez 5275d18ee67SSebastian Sanchez /* 5285d18ee67SSebastian Sanchez * It assumes dd->comp_vect_possible_cpus is available. 5295d18ee67SSebastian Sanchez */ 5305d18ee67SSebastian Sanchez static int _dev_comp_vect_cpu_mask_init(struct hfi1_devdata *dd, 5315d18ee67SSebastian Sanchez struct hfi1_affinity_node *entry, 5325d18ee67SSebastian Sanchez bool first_dev_init) 5335d18ee67SSebastian Sanchez __must_hold(&node_affinity.lock) 5345d18ee67SSebastian Sanchez { 5355d18ee67SSebastian Sanchez int i, j, curr_cpu; 5365d18ee67SSebastian Sanchez int possible_cpus_comp_vect = 0; 5375d18ee67SSebastian Sanchez struct cpumask *dev_comp_vect_mask = &dd->comp_vect->mask; 5385d18ee67SSebastian Sanchez 5395d18ee67SSebastian Sanchez lockdep_assert_held(&node_affinity.lock); 5405d18ee67SSebastian Sanchez /* 5415d18ee67SSebastian Sanchez * If there's only one CPU available for completion vectors, then 5425d18ee67SSebastian Sanchez * there will only be one completion vector available. Othewise, 5435d18ee67SSebastian Sanchez * the number of completion vector available will be the number of 5445d18ee67SSebastian Sanchez * available CPUs divide it by the number of devices in the 5455d18ee67SSebastian Sanchez * local NUMA node. 5465d18ee67SSebastian Sanchez */ 5475d18ee67SSebastian Sanchez if (cpumask_weight(&entry->comp_vect_mask) == 1) { 5485d18ee67SSebastian Sanchez possible_cpus_comp_vect = 1; 5495d18ee67SSebastian Sanchez dd_dev_warn(dd, 5505d18ee67SSebastian Sanchez "Number of kernel receive queues is too large for completion vector affinity to be effective\n"); 5515d18ee67SSebastian Sanchez } else { 5525d18ee67SSebastian Sanchez possible_cpus_comp_vect += 5535d18ee67SSebastian Sanchez cpumask_weight(&entry->comp_vect_mask) / 5545d18ee67SSebastian Sanchez hfi1_per_node_cntr[dd->node]; 5555d18ee67SSebastian Sanchez 5565d18ee67SSebastian Sanchez /* 5575d18ee67SSebastian Sanchez * If the completion vector CPUs available doesn't divide 5585d18ee67SSebastian Sanchez * evenly among devices, then the first device device to be 5595d18ee67SSebastian Sanchez * initialized gets an extra CPU. 5605d18ee67SSebastian Sanchez */ 5615d18ee67SSebastian Sanchez if (first_dev_init && 5625d18ee67SSebastian Sanchez cpumask_weight(&entry->comp_vect_mask) % 5635d18ee67SSebastian Sanchez hfi1_per_node_cntr[dd->node] != 0) 5645d18ee67SSebastian Sanchez possible_cpus_comp_vect++; 5655d18ee67SSebastian Sanchez } 5665d18ee67SSebastian Sanchez 5675d18ee67SSebastian Sanchez dd->comp_vect_possible_cpus = possible_cpus_comp_vect; 5685d18ee67SSebastian Sanchez 5695d18ee67SSebastian Sanchez /* Reserving CPUs for device completion vector */ 5705d18ee67SSebastian Sanchez for (i = 0; i < dd->comp_vect_possible_cpus; i++) { 5715d18ee67SSebastian Sanchez curr_cpu = per_cpu_affinity_get(&entry->comp_vect_mask, 5725d18ee67SSebastian Sanchez entry->comp_vect_affinity); 5735d18ee67SSebastian Sanchez if (curr_cpu < 0) 5745d18ee67SSebastian Sanchez goto fail; 5755d18ee67SSebastian Sanchez 5765d18ee67SSebastian Sanchez cpumask_set_cpu(curr_cpu, dev_comp_vect_mask); 5775d18ee67SSebastian Sanchez } 5785d18ee67SSebastian Sanchez 5795d18ee67SSebastian Sanchez hfi1_cdbg(AFFINITY, 5805d18ee67SSebastian Sanchez "[%s] Completion vector affinity CPU set(s) %*pbl", 5815d18ee67SSebastian Sanchez rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), 5825d18ee67SSebastian Sanchez cpumask_pr_args(dev_comp_vect_mask)); 5835d18ee67SSebastian Sanchez 5845d18ee67SSebastian Sanchez return 0; 5855d18ee67SSebastian Sanchez 5865d18ee67SSebastian Sanchez fail: 5875d18ee67SSebastian Sanchez for (j = 0; j < i; j++) 5885d18ee67SSebastian Sanchez per_cpu_affinity_put_max(&entry->comp_vect_mask, 5895d18ee67SSebastian Sanchez entry->comp_vect_affinity); 5905d18ee67SSebastian Sanchez 5915d18ee67SSebastian Sanchez return curr_cpu; 5925d18ee67SSebastian Sanchez } 5935d18ee67SSebastian Sanchez 5945d18ee67SSebastian Sanchez /* 5955d18ee67SSebastian Sanchez * It assumes dd->comp_vect_possible_cpus is available. 5965d18ee67SSebastian Sanchez */ 5975d18ee67SSebastian Sanchez static void _dev_comp_vect_cpu_mask_clean_up(struct hfi1_devdata *dd, 5985d18ee67SSebastian Sanchez struct hfi1_affinity_node *entry) 5995d18ee67SSebastian Sanchez __must_hold(&node_affinity.lock) 6005d18ee67SSebastian Sanchez { 6015d18ee67SSebastian Sanchez int i, cpu; 6025d18ee67SSebastian Sanchez 6035d18ee67SSebastian Sanchez lockdep_assert_held(&node_affinity.lock); 6045d18ee67SSebastian Sanchez if (!dd->comp_vect_possible_cpus) 6055d18ee67SSebastian Sanchez return; 6065d18ee67SSebastian Sanchez 6075d18ee67SSebastian Sanchez for (i = 0; i < dd->comp_vect_possible_cpus; i++) { 6085d18ee67SSebastian Sanchez cpu = per_cpu_affinity_put_max(&dd->comp_vect->mask, 6095d18ee67SSebastian Sanchez entry->comp_vect_affinity); 6105d18ee67SSebastian Sanchez /* Clearing CPU in device completion vector cpu mask */ 6115d18ee67SSebastian Sanchez if (cpu >= 0) 6125d18ee67SSebastian Sanchez cpumask_clear_cpu(cpu, &dd->comp_vect->mask); 6135d18ee67SSebastian Sanchez } 6145d18ee67SSebastian Sanchez 6155d18ee67SSebastian Sanchez dd->comp_vect_possible_cpus = 0; 6165d18ee67SSebastian Sanchez } 6175d18ee67SSebastian Sanchez 618f48ad614SDennis Dalessandro /* 619f48ad614SDennis Dalessandro * Interrupt affinity. 620f48ad614SDennis Dalessandro * 621f48ad614SDennis Dalessandro * non-rcv avail gets a default mask that 622f48ad614SDennis Dalessandro * starts as possible cpus with threads reset 623f48ad614SDennis Dalessandro * and each rcv avail reset. 624f48ad614SDennis Dalessandro * 625f48ad614SDennis Dalessandro * rcv avail gets node relative 1 wrapping back 626f48ad614SDennis Dalessandro * to the node relative 1 as necessary. 627f48ad614SDennis Dalessandro * 628f48ad614SDennis Dalessandro */ 6294197344bSDennis Dalessandro int hfi1_dev_affinity_init(struct hfi1_devdata *dd) 630f48ad614SDennis Dalessandro { 631f48ad614SDennis Dalessandro int node = pcibus_to_node(dd->pcidev->bus); 6324197344bSDennis Dalessandro struct hfi1_affinity_node *entry; 633f48ad614SDennis Dalessandro const struct cpumask *local_mask; 6345d18ee67SSebastian Sanchez int curr_cpu, possible, i, ret; 6355d18ee67SSebastian Sanchez bool new_entry = false; 636f48ad614SDennis Dalessandro 637c513de49SMichael J. Ruhl /* 638c513de49SMichael J. Ruhl * If the BIOS does not have the NUMA node information set, select 639c513de49SMichael J. Ruhl * NUMA 0 so we get consistent performance. 640c513de49SMichael J. Ruhl */ 641c513de49SMichael J. Ruhl if (node < 0) { 642c513de49SMichael J. Ruhl dd_dev_err(dd, "Invalid PCI NUMA node. Performance may be affected\n"); 643c513de49SMichael J. Ruhl node = 0; 644c513de49SMichael J. Ruhl } 645f48ad614SDennis Dalessandro dd->node = node; 646f48ad614SDennis Dalessandro 647f48ad614SDennis Dalessandro local_mask = cpumask_of_node(dd->node); 648f48ad614SDennis Dalessandro if (cpumask_first(local_mask) >= nr_cpu_ids) 649f48ad614SDennis Dalessandro local_mask = topology_core_cpumask(0); 6504197344bSDennis Dalessandro 651584d9577STadeusz Struk mutex_lock(&node_affinity.lock); 6524197344bSDennis Dalessandro entry = node_affinity_lookup(dd->node); 6534197344bSDennis Dalessandro 6544197344bSDennis Dalessandro /* 6554197344bSDennis Dalessandro * If this is the first time this NUMA node's affinity is used, 6564197344bSDennis Dalessandro * create an entry in the global affinity structure and initialize it. 6574197344bSDennis Dalessandro */ 6584197344bSDennis Dalessandro if (!entry) { 6594197344bSDennis Dalessandro entry = node_affinity_allocate(node); 6604197344bSDennis Dalessandro if (!entry) { 6614197344bSDennis Dalessandro dd_dev_err(dd, 6624197344bSDennis Dalessandro "Unable to allocate global affinity node\n"); 6635d18ee67SSebastian Sanchez ret = -ENOMEM; 6645d18ee67SSebastian Sanchez goto fail; 6654197344bSDennis Dalessandro } 6665d18ee67SSebastian Sanchez new_entry = true; 6675d18ee67SSebastian Sanchez 6684197344bSDennis Dalessandro init_cpu_mask_set(&entry->def_intr); 6694197344bSDennis Dalessandro init_cpu_mask_set(&entry->rcv_intr); 6705d18ee67SSebastian Sanchez cpumask_clear(&entry->comp_vect_mask); 671d6373019SSebastian Sanchez cpumask_clear(&entry->general_intr_mask); 672f48ad614SDennis Dalessandro /* Use the "real" cpu mask of this node as the default */ 6734197344bSDennis Dalessandro cpumask_and(&entry->def_intr.mask, &node_affinity.real_cpu_mask, 6744197344bSDennis Dalessandro local_mask); 675f48ad614SDennis Dalessandro 676f48ad614SDennis Dalessandro /* fill in the receive list */ 6774197344bSDennis Dalessandro possible = cpumask_weight(&entry->def_intr.mask); 6784197344bSDennis Dalessandro curr_cpu = cpumask_first(&entry->def_intr.mask); 6794197344bSDennis Dalessandro 680f48ad614SDennis Dalessandro if (possible == 1) { 681f48ad614SDennis Dalessandro /* only one CPU, everyone will use it */ 6824197344bSDennis Dalessandro cpumask_set_cpu(curr_cpu, &entry->rcv_intr.mask); 683d6373019SSebastian Sanchez cpumask_set_cpu(curr_cpu, &entry->general_intr_mask); 684f48ad614SDennis Dalessandro } else { 685f48ad614SDennis Dalessandro /* 686d6373019SSebastian Sanchez * The general/control context will be the first CPU in 687d6373019SSebastian Sanchez * the default list, so it is removed from the default 688d6373019SSebastian Sanchez * list and added to the general interrupt list. 689f48ad614SDennis Dalessandro */ 690d6373019SSebastian Sanchez cpumask_clear_cpu(curr_cpu, &entry->def_intr.mask); 691d6373019SSebastian Sanchez cpumask_set_cpu(curr_cpu, &entry->general_intr_mask); 6924197344bSDennis Dalessandro curr_cpu = cpumask_next(curr_cpu, 6934197344bSDennis Dalessandro &entry->def_intr.mask); 6944197344bSDennis Dalessandro 695f48ad614SDennis Dalessandro /* 696f48ad614SDennis Dalessandro * Remove the remaining kernel receive queues from 697f48ad614SDennis Dalessandro * the default list and add them to the receive list. 698f48ad614SDennis Dalessandro */ 699d6373019SSebastian Sanchez for (i = 0; 700d6373019SSebastian Sanchez i < (dd->n_krcv_queues - 1) * 701d6373019SSebastian Sanchez hfi1_per_node_cntr[dd->node]; 702d6373019SSebastian Sanchez i++) { 7034197344bSDennis Dalessandro cpumask_clear_cpu(curr_cpu, 7044197344bSDennis Dalessandro &entry->def_intr.mask); 7054197344bSDennis Dalessandro cpumask_set_cpu(curr_cpu, 7064197344bSDennis Dalessandro &entry->rcv_intr.mask); 7074197344bSDennis Dalessandro curr_cpu = cpumask_next(curr_cpu, 7084197344bSDennis Dalessandro &entry->def_intr.mask); 709f48ad614SDennis Dalessandro if (curr_cpu >= nr_cpu_ids) 710f48ad614SDennis Dalessandro break; 711f48ad614SDennis Dalessandro } 712d6373019SSebastian Sanchez 713d6373019SSebastian Sanchez /* 714d6373019SSebastian Sanchez * If there ends up being 0 CPU cores leftover for SDMA 715d6373019SSebastian Sanchez * engines, use the same CPU cores as general/control 716d6373019SSebastian Sanchez * context. 717d6373019SSebastian Sanchez */ 718d6373019SSebastian Sanchez if (cpumask_weight(&entry->def_intr.mask) == 0) 719d6373019SSebastian Sanchez cpumask_copy(&entry->def_intr.mask, 720d6373019SSebastian Sanchez &entry->general_intr_mask); 721f48ad614SDennis Dalessandro } 722f48ad614SDennis Dalessandro 7235d18ee67SSebastian Sanchez /* Determine completion vector CPUs for the entire node */ 7245d18ee67SSebastian Sanchez cpumask_and(&entry->comp_vect_mask, 7255d18ee67SSebastian Sanchez &node_affinity.real_cpu_mask, local_mask); 7265d18ee67SSebastian Sanchez cpumask_andnot(&entry->comp_vect_mask, 7275d18ee67SSebastian Sanchez &entry->comp_vect_mask, 7285d18ee67SSebastian Sanchez &entry->rcv_intr.mask); 7295d18ee67SSebastian Sanchez cpumask_andnot(&entry->comp_vect_mask, 7305d18ee67SSebastian Sanchez &entry->comp_vect_mask, 7315d18ee67SSebastian Sanchez &entry->general_intr_mask); 7325d18ee67SSebastian Sanchez 7335d18ee67SSebastian Sanchez /* 7345d18ee67SSebastian Sanchez * If there ends up being 0 CPU cores leftover for completion 7355d18ee67SSebastian Sanchez * vectors, use the same CPU core as the general/control 7365d18ee67SSebastian Sanchez * context. 7375d18ee67SSebastian Sanchez */ 7385d18ee67SSebastian Sanchez if (cpumask_weight(&entry->comp_vect_mask) == 0) 7395d18ee67SSebastian Sanchez cpumask_copy(&entry->comp_vect_mask, 7405d18ee67SSebastian Sanchez &entry->general_intr_mask); 741f48ad614SDennis Dalessandro } 7425d18ee67SSebastian Sanchez 7435d18ee67SSebastian Sanchez ret = _dev_comp_vect_cpu_mask_init(dd, entry, new_entry); 7445d18ee67SSebastian Sanchez if (ret < 0) 7455d18ee67SSebastian Sanchez goto fail; 7465d18ee67SSebastian Sanchez 7475d18ee67SSebastian Sanchez if (new_entry) 7485d18ee67SSebastian Sanchez node_affinity_add_tail(entry); 7495d18ee67SSebastian Sanchez 750584d9577STadeusz Struk mutex_unlock(&node_affinity.lock); 7515d18ee67SSebastian Sanchez 7524197344bSDennis Dalessandro return 0; 7535d18ee67SSebastian Sanchez 7545d18ee67SSebastian Sanchez fail: 7555d18ee67SSebastian Sanchez if (new_entry) 7565d18ee67SSebastian Sanchez node_affinity_destroy(entry); 7575d18ee67SSebastian Sanchez mutex_unlock(&node_affinity.lock); 7585d18ee67SSebastian Sanchez return ret; 7595d18ee67SSebastian Sanchez } 7605d18ee67SSebastian Sanchez 7615d18ee67SSebastian Sanchez void hfi1_dev_affinity_clean_up(struct hfi1_devdata *dd) 7625d18ee67SSebastian Sanchez { 7635d18ee67SSebastian Sanchez struct hfi1_affinity_node *entry; 7645d18ee67SSebastian Sanchez 7655d18ee67SSebastian Sanchez if (dd->node < 0) 7665d18ee67SSebastian Sanchez return; 7675d18ee67SSebastian Sanchez 7685d18ee67SSebastian Sanchez mutex_lock(&node_affinity.lock); 7695d18ee67SSebastian Sanchez entry = node_affinity_lookup(dd->node); 7705d18ee67SSebastian Sanchez if (!entry) 7715d18ee67SSebastian Sanchez goto unlock; 7725d18ee67SSebastian Sanchez 7735d18ee67SSebastian Sanchez /* 7745d18ee67SSebastian Sanchez * Free device completion vector CPUs to be used by future 7755d18ee67SSebastian Sanchez * completion vectors 7765d18ee67SSebastian Sanchez */ 7775d18ee67SSebastian Sanchez _dev_comp_vect_cpu_mask_clean_up(dd, entry); 7785d18ee67SSebastian Sanchez unlock: 7795d18ee67SSebastian Sanchez mutex_unlock(&node_affinity.lock); 7805d18ee67SSebastian Sanchez dd->node = -1; 781f48ad614SDennis Dalessandro } 782f48ad614SDennis Dalessandro 783584d9577STadeusz Struk /* 7842d01c37dSTadeusz Struk * Function updates the irq affinity hint for msix after it has been changed 7852d01c37dSTadeusz Struk * by the user using the /proc/irq interface. This function only accepts 7862d01c37dSTadeusz Struk * one cpu in the mask. 7872d01c37dSTadeusz Struk */ 7882d01c37dSTadeusz Struk static void hfi1_update_sdma_affinity(struct hfi1_msix_entry *msix, int cpu) 7892d01c37dSTadeusz Struk { 7902d01c37dSTadeusz Struk struct sdma_engine *sde = msix->arg; 7912d01c37dSTadeusz Struk struct hfi1_devdata *dd = sde->dd; 7922d01c37dSTadeusz Struk struct hfi1_affinity_node *entry; 7932d01c37dSTadeusz Struk struct cpu_mask_set *set; 7942d01c37dSTadeusz Struk int i, old_cpu; 7952d01c37dSTadeusz Struk 7962d01c37dSTadeusz Struk if (cpu > num_online_cpus() || cpu == sde->cpu) 7972d01c37dSTadeusz Struk return; 7982d01c37dSTadeusz Struk 7992d01c37dSTadeusz Struk mutex_lock(&node_affinity.lock); 8002d01c37dSTadeusz Struk entry = node_affinity_lookup(dd->node); 8012d01c37dSTadeusz Struk if (!entry) 8022d01c37dSTadeusz Struk goto unlock; 8032d01c37dSTadeusz Struk 8042d01c37dSTadeusz Struk old_cpu = sde->cpu; 8052d01c37dSTadeusz Struk sde->cpu = cpu; 8062d01c37dSTadeusz Struk cpumask_clear(&msix->mask); 8072d01c37dSTadeusz Struk cpumask_set_cpu(cpu, &msix->mask); 808bb7dde87SMichael J. Ruhl dd_dev_dbg(dd, "IRQ: %u, type %s engine %u -> cpu: %d\n", 809bb7dde87SMichael J. Ruhl msix->irq, irq_type_names[msix->type], 8102d01c37dSTadeusz Struk sde->this_idx, cpu); 811bb7dde87SMichael J. Ruhl irq_set_affinity_hint(msix->irq, &msix->mask); 8122d01c37dSTadeusz Struk 8132d01c37dSTadeusz Struk /* 8142d01c37dSTadeusz Struk * Set the new cpu in the hfi1_affinity_node and clean 8152d01c37dSTadeusz Struk * the old cpu if it is not used by any other IRQ 8162d01c37dSTadeusz Struk */ 8172d01c37dSTadeusz Struk set = &entry->def_intr; 8182d01c37dSTadeusz Struk cpumask_set_cpu(cpu, &set->mask); 8192d01c37dSTadeusz Struk cpumask_set_cpu(cpu, &set->used); 820*6eb4eb10SMichael J. Ruhl for (i = 0; i < dd->msix_info.max_requested; i++) { 8212d01c37dSTadeusz Struk struct hfi1_msix_entry *other_msix; 8222d01c37dSTadeusz Struk 823*6eb4eb10SMichael J. Ruhl other_msix = &dd->msix_info.msix_entries[i]; 8242d01c37dSTadeusz Struk if (other_msix->type != IRQ_SDMA || other_msix == msix) 8252d01c37dSTadeusz Struk continue; 8262d01c37dSTadeusz Struk 8272d01c37dSTadeusz Struk if (cpumask_test_cpu(old_cpu, &other_msix->mask)) 8282d01c37dSTadeusz Struk goto unlock; 8292d01c37dSTadeusz Struk } 8302d01c37dSTadeusz Struk cpumask_clear_cpu(old_cpu, &set->mask); 8312d01c37dSTadeusz Struk cpumask_clear_cpu(old_cpu, &set->used); 8322d01c37dSTadeusz Struk unlock: 8332d01c37dSTadeusz Struk mutex_unlock(&node_affinity.lock); 8342d01c37dSTadeusz Struk } 8352d01c37dSTadeusz Struk 8362d01c37dSTadeusz Struk static void hfi1_irq_notifier_notify(struct irq_affinity_notify *notify, 8372d01c37dSTadeusz Struk const cpumask_t *mask) 8382d01c37dSTadeusz Struk { 8392d01c37dSTadeusz Struk int cpu = cpumask_first(mask); 8402d01c37dSTadeusz Struk struct hfi1_msix_entry *msix = container_of(notify, 8412d01c37dSTadeusz Struk struct hfi1_msix_entry, 8422d01c37dSTadeusz Struk notify); 8432d01c37dSTadeusz Struk 8442d01c37dSTadeusz Struk /* Only one CPU configuration supported currently */ 8452d01c37dSTadeusz Struk hfi1_update_sdma_affinity(msix, cpu); 8462d01c37dSTadeusz Struk } 8472d01c37dSTadeusz Struk 8482d01c37dSTadeusz Struk static void hfi1_irq_notifier_release(struct kref *ref) 8492d01c37dSTadeusz Struk { 8502d01c37dSTadeusz Struk /* 8512d01c37dSTadeusz Struk * This is required by affinity notifier. We don't have anything to 8522d01c37dSTadeusz Struk * free here. 8532d01c37dSTadeusz Struk */ 8542d01c37dSTadeusz Struk } 8552d01c37dSTadeusz Struk 8562d01c37dSTadeusz Struk static void hfi1_setup_sdma_notifier(struct hfi1_msix_entry *msix) 8572d01c37dSTadeusz Struk { 8582d01c37dSTadeusz Struk struct irq_affinity_notify *notify = &msix->notify; 8592d01c37dSTadeusz Struk 860bb7dde87SMichael J. Ruhl notify->irq = msix->irq; 8612d01c37dSTadeusz Struk notify->notify = hfi1_irq_notifier_notify; 8622d01c37dSTadeusz Struk notify->release = hfi1_irq_notifier_release; 8632d01c37dSTadeusz Struk 8642d01c37dSTadeusz Struk if (irq_set_affinity_notifier(notify->irq, notify)) 8652d01c37dSTadeusz Struk pr_err("Failed to register sdma irq affinity notifier for irq %d\n", 8662d01c37dSTadeusz Struk notify->irq); 8672d01c37dSTadeusz Struk } 8682d01c37dSTadeusz Struk 8692d01c37dSTadeusz Struk static void hfi1_cleanup_sdma_notifier(struct hfi1_msix_entry *msix) 8702d01c37dSTadeusz Struk { 8712d01c37dSTadeusz Struk struct irq_affinity_notify *notify = &msix->notify; 8722d01c37dSTadeusz Struk 8732d01c37dSTadeusz Struk if (irq_set_affinity_notifier(notify->irq, NULL)) 8742d01c37dSTadeusz Struk pr_err("Failed to cleanup sdma irq affinity notifier for irq %d\n", 8752d01c37dSTadeusz Struk notify->irq); 8762d01c37dSTadeusz Struk } 8772d01c37dSTadeusz Struk 8782d01c37dSTadeusz Struk /* 879584d9577STadeusz Struk * Function sets the irq affinity for msix. 880584d9577STadeusz Struk * It *must* be called with node_affinity.lock held. 881584d9577STadeusz Struk */ 882584d9577STadeusz Struk static int get_irq_affinity(struct hfi1_devdata *dd, 883584d9577STadeusz Struk struct hfi1_msix_entry *msix) 884f48ad614SDennis Dalessandro { 885f48ad614SDennis Dalessandro cpumask_var_t diff; 8864197344bSDennis Dalessandro struct hfi1_affinity_node *entry; 887d6373019SSebastian Sanchez struct cpu_mask_set *set = NULL; 888f48ad614SDennis Dalessandro struct sdma_engine *sde = NULL; 889f48ad614SDennis Dalessandro struct hfi1_ctxtdata *rcd = NULL; 890f48ad614SDennis Dalessandro char extra[64]; 891f48ad614SDennis Dalessandro int cpu = -1; 892f48ad614SDennis Dalessandro 893f48ad614SDennis Dalessandro extra[0] = '\0'; 894f48ad614SDennis Dalessandro cpumask_clear(&msix->mask); 895f48ad614SDennis Dalessandro 8964197344bSDennis Dalessandro entry = node_affinity_lookup(dd->node); 8974197344bSDennis Dalessandro 898f48ad614SDennis Dalessandro switch (msix->type) { 899f48ad614SDennis Dalessandro case IRQ_SDMA: 900f48ad614SDennis Dalessandro sde = (struct sdma_engine *)msix->arg; 901f48ad614SDennis Dalessandro scnprintf(extra, 64, "engine %u", sde->this_idx); 9024197344bSDennis Dalessandro set = &entry->def_intr; 903f48ad614SDennis Dalessandro break; 904d6373019SSebastian Sanchez case IRQ_GENERAL: 905d6373019SSebastian Sanchez cpu = cpumask_first(&entry->general_intr_mask); 906d6373019SSebastian Sanchez break; 907f48ad614SDennis Dalessandro case IRQ_RCVCTXT: 908f48ad614SDennis Dalessandro rcd = (struct hfi1_ctxtdata *)msix->arg; 909d6373019SSebastian Sanchez if (rcd->ctxt == HFI1_CTRL_CTXT) 910d6373019SSebastian Sanchez cpu = cpumask_first(&entry->general_intr_mask); 911d6373019SSebastian Sanchez else 9124197344bSDennis Dalessandro set = &entry->rcv_intr; 913f48ad614SDennis Dalessandro scnprintf(extra, 64, "ctxt %u", rcd->ctxt); 914f48ad614SDennis Dalessandro break; 915f48ad614SDennis Dalessandro default: 916f48ad614SDennis Dalessandro dd_dev_err(dd, "Invalid IRQ type %d\n", msix->type); 917f48ad614SDennis Dalessandro return -EINVAL; 918f48ad614SDennis Dalessandro } 919f48ad614SDennis Dalessandro 920f48ad614SDennis Dalessandro /* 921d6373019SSebastian Sanchez * The general and control contexts are placed on a particular 922d6373019SSebastian Sanchez * CPU, which is set above. Skip accounting for it. Everything else 923d6373019SSebastian Sanchez * finds its CPU here. 924f48ad614SDennis Dalessandro */ 9254197344bSDennis Dalessandro if (cpu == -1 && set) { 92659482a14SSebastian Sanchez if (!zalloc_cpumask_var(&diff, GFP_KERNEL)) 92759482a14SSebastian Sanchez return -ENOMEM; 92859482a14SSebastian Sanchez 929cf38ea10SSebastian Sanchez cpu = cpu_mask_set_get_first(set, diff); 930cf38ea10SSebastian Sanchez if (cpu < 0) { 931cf38ea10SSebastian Sanchez free_cpumask_var(diff); 932cf38ea10SSebastian Sanchez dd_dev_err(dd, "Failure to obtain CPU for IRQ\n"); 933cf38ea10SSebastian Sanchez return cpu; 934f48ad614SDennis Dalessandro } 93559482a14SSebastian Sanchez 93659482a14SSebastian Sanchez free_cpumask_var(diff); 937f48ad614SDennis Dalessandro } 938f48ad614SDennis Dalessandro 939f48ad614SDennis Dalessandro cpumask_set_cpu(cpu, &msix->mask); 940bb7dde87SMichael J. Ruhl dd_dev_info(dd, "IRQ: %u, type %s %s -> cpu: %d\n", 941bb7dde87SMichael J. Ruhl msix->irq, irq_type_names[msix->type], 942f48ad614SDennis Dalessandro extra, cpu); 943bb7dde87SMichael J. Ruhl irq_set_affinity_hint(msix->irq, &msix->mask); 944f48ad614SDennis Dalessandro 9452d01c37dSTadeusz Struk if (msix->type == IRQ_SDMA) { 9462d01c37dSTadeusz Struk sde->cpu = cpu; 9472d01c37dSTadeusz Struk hfi1_setup_sdma_notifier(msix); 9482d01c37dSTadeusz Struk } 9492d01c37dSTadeusz Struk 950f48ad614SDennis Dalessandro return 0; 951f48ad614SDennis Dalessandro } 952f48ad614SDennis Dalessandro 953584d9577STadeusz Struk int hfi1_get_irq_affinity(struct hfi1_devdata *dd, struct hfi1_msix_entry *msix) 954584d9577STadeusz Struk { 955584d9577STadeusz Struk int ret; 956584d9577STadeusz Struk 957584d9577STadeusz Struk mutex_lock(&node_affinity.lock); 958584d9577STadeusz Struk ret = get_irq_affinity(dd, msix); 959584d9577STadeusz Struk mutex_unlock(&node_affinity.lock); 960584d9577STadeusz Struk return ret; 961584d9577STadeusz Struk } 962584d9577STadeusz Struk 963f48ad614SDennis Dalessandro void hfi1_put_irq_affinity(struct hfi1_devdata *dd, 964f48ad614SDennis Dalessandro struct hfi1_msix_entry *msix) 965f48ad614SDennis Dalessandro { 966f48ad614SDennis Dalessandro struct cpu_mask_set *set = NULL; 967f48ad614SDennis Dalessandro struct hfi1_ctxtdata *rcd; 9684197344bSDennis Dalessandro struct hfi1_affinity_node *entry; 9694197344bSDennis Dalessandro 970584d9577STadeusz Struk mutex_lock(&node_affinity.lock); 9714197344bSDennis Dalessandro entry = node_affinity_lookup(dd->node); 972f48ad614SDennis Dalessandro 973f48ad614SDennis Dalessandro switch (msix->type) { 974f48ad614SDennis Dalessandro case IRQ_SDMA: 9754197344bSDennis Dalessandro set = &entry->def_intr; 9762d01c37dSTadeusz Struk hfi1_cleanup_sdma_notifier(msix); 977f48ad614SDennis Dalessandro break; 978d6373019SSebastian Sanchez case IRQ_GENERAL: 979b094a36fSSebastian Sanchez /* Don't do accounting for general contexts */ 980d6373019SSebastian Sanchez break; 981f48ad614SDennis Dalessandro case IRQ_RCVCTXT: 982f48ad614SDennis Dalessandro rcd = (struct hfi1_ctxtdata *)msix->arg; 983d6373019SSebastian Sanchez /* Don't do accounting for control contexts */ 984f48ad614SDennis Dalessandro if (rcd->ctxt != HFI1_CTRL_CTXT) 9854197344bSDennis Dalessandro set = &entry->rcv_intr; 986f48ad614SDennis Dalessandro break; 987f48ad614SDennis Dalessandro default: 988584d9577STadeusz Struk mutex_unlock(&node_affinity.lock); 989f48ad614SDennis Dalessandro return; 990f48ad614SDennis Dalessandro } 991f48ad614SDennis Dalessandro 992f48ad614SDennis Dalessandro if (set) { 993f48ad614SDennis Dalessandro cpumask_andnot(&set->used, &set->used, &msix->mask); 994cf38ea10SSebastian Sanchez _cpu_mask_set_gen_dec(set); 995f48ad614SDennis Dalessandro } 996f48ad614SDennis Dalessandro 997bb7dde87SMichael J. Ruhl irq_set_affinity_hint(msix->irq, NULL); 998f48ad614SDennis Dalessandro cpumask_clear(&msix->mask); 999584d9577STadeusz Struk mutex_unlock(&node_affinity.lock); 1000f48ad614SDennis Dalessandro } 1001f48ad614SDennis Dalessandro 1002b094a36fSSebastian Sanchez /* This should be called with node_affinity.lock held */ 1003b094a36fSSebastian Sanchez static void find_hw_thread_mask(uint hw_thread_no, cpumask_var_t hw_thread_mask, 1004b094a36fSSebastian Sanchez struct hfi1_affinity_node_list *affinity) 1005f48ad614SDennis Dalessandro { 1006b094a36fSSebastian Sanchez int possible, curr_cpu, i; 1007b094a36fSSebastian Sanchez uint num_cores_per_socket = node_affinity.num_online_cpus / 1008b094a36fSSebastian Sanchez affinity->num_core_siblings / 1009b094a36fSSebastian Sanchez node_affinity.num_online_nodes; 1010b094a36fSSebastian Sanchez 1011b094a36fSSebastian Sanchez cpumask_copy(hw_thread_mask, &affinity->proc.mask); 1012b094a36fSSebastian Sanchez if (affinity->num_core_siblings > 0) { 1013b094a36fSSebastian Sanchez /* Removing other siblings not needed for now */ 1014b094a36fSSebastian Sanchez possible = cpumask_weight(hw_thread_mask); 1015b094a36fSSebastian Sanchez curr_cpu = cpumask_first(hw_thread_mask); 1016b094a36fSSebastian Sanchez for (i = 0; 1017b094a36fSSebastian Sanchez i < num_cores_per_socket * node_affinity.num_online_nodes; 1018b094a36fSSebastian Sanchez i++) 1019b094a36fSSebastian Sanchez curr_cpu = cpumask_next(curr_cpu, hw_thread_mask); 1020b094a36fSSebastian Sanchez 1021b094a36fSSebastian Sanchez for (; i < possible; i++) { 1022b094a36fSSebastian Sanchez cpumask_clear_cpu(curr_cpu, hw_thread_mask); 1023b094a36fSSebastian Sanchez curr_cpu = cpumask_next(curr_cpu, hw_thread_mask); 1024b094a36fSSebastian Sanchez } 1025b094a36fSSebastian Sanchez 1026b094a36fSSebastian Sanchez /* Identifying correct HW threads within physical cores */ 1027b094a36fSSebastian Sanchez cpumask_shift_left(hw_thread_mask, hw_thread_mask, 1028b094a36fSSebastian Sanchez num_cores_per_socket * 1029b094a36fSSebastian Sanchez node_affinity.num_online_nodes * 1030b094a36fSSebastian Sanchez hw_thread_no); 1031b094a36fSSebastian Sanchez } 1032b094a36fSSebastian Sanchez } 1033b094a36fSSebastian Sanchez 1034b094a36fSSebastian Sanchez int hfi1_get_proc_affinity(int node) 1035b094a36fSSebastian Sanchez { 1036b094a36fSSebastian Sanchez int cpu = -1, ret, i; 10374197344bSDennis Dalessandro struct hfi1_affinity_node *entry; 1038b094a36fSSebastian Sanchez cpumask_var_t diff, hw_thread_mask, available_mask, intrs_mask; 1039f48ad614SDennis Dalessandro const struct cpumask *node_mask, 10400c98d344SIngo Molnar *proc_mask = ¤t->cpus_allowed; 1041b094a36fSSebastian Sanchez struct hfi1_affinity_node_list *affinity = &node_affinity; 1042b094a36fSSebastian Sanchez struct cpu_mask_set *set = &affinity->proc; 1043f48ad614SDennis Dalessandro 1044f48ad614SDennis Dalessandro /* 1045f48ad614SDennis Dalessandro * check whether process/context affinity has already 1046f48ad614SDennis Dalessandro * been set 1047f48ad614SDennis Dalessandro */ 1048f48ad614SDennis Dalessandro if (cpumask_weight(proc_mask) == 1) { 1049f242d93aSLeon Romanovsky hfi1_cdbg(PROC, "PID %u %s affinity set to CPU %*pbl", 1050f242d93aSLeon Romanovsky current->pid, current->comm, 1051f242d93aSLeon Romanovsky cpumask_pr_args(proc_mask)); 1052f48ad614SDennis Dalessandro /* 1053f48ad614SDennis Dalessandro * Mark the pre-set CPU as used. This is atomic so we don't 1054f48ad614SDennis Dalessandro * need the lock 1055f48ad614SDennis Dalessandro */ 1056f48ad614SDennis Dalessandro cpu = cpumask_first(proc_mask); 1057f48ad614SDennis Dalessandro cpumask_set_cpu(cpu, &set->used); 1058f48ad614SDennis Dalessandro goto done; 1059f48ad614SDennis Dalessandro } else if (cpumask_weight(proc_mask) < cpumask_weight(&set->mask)) { 1060f242d93aSLeon Romanovsky hfi1_cdbg(PROC, "PID %u %s affinity set to CPU set(s) %*pbl", 1061f242d93aSLeon Romanovsky current->pid, current->comm, 1062f242d93aSLeon Romanovsky cpumask_pr_args(proc_mask)); 1063f48ad614SDennis Dalessandro goto done; 1064f48ad614SDennis Dalessandro } 1065f48ad614SDennis Dalessandro 1066f48ad614SDennis Dalessandro /* 1067f48ad614SDennis Dalessandro * The process does not have a preset CPU affinity so find one to 1068b094a36fSSebastian Sanchez * recommend using the following algorithm: 1069b094a36fSSebastian Sanchez * 1070b094a36fSSebastian Sanchez * For each user process that is opening a context on HFI Y: 1071b094a36fSSebastian Sanchez * a) If all cores are filled, reinitialize the bitmask 1072b094a36fSSebastian Sanchez * b) Fill real cores first, then HT cores (First set of HT 1073b094a36fSSebastian Sanchez * cores on all physical cores, then second set of HT core, 1074b094a36fSSebastian Sanchez * and, so on) in the following order: 1075b094a36fSSebastian Sanchez * 1076b094a36fSSebastian Sanchez * 1. Same NUMA node as HFI Y and not running an IRQ 1077b094a36fSSebastian Sanchez * handler 1078b094a36fSSebastian Sanchez * 2. Same NUMA node as HFI Y and running an IRQ handler 1079b094a36fSSebastian Sanchez * 3. Different NUMA node to HFI Y and not running an IRQ 1080b094a36fSSebastian Sanchez * handler 1081b094a36fSSebastian Sanchez * 4. Different NUMA node to HFI Y and running an IRQ 1082b094a36fSSebastian Sanchez * handler 1083b094a36fSSebastian Sanchez * c) Mark core as filled in the bitmask. As user processes are 1084b094a36fSSebastian Sanchez * done, clear cores from the bitmask. 1085f48ad614SDennis Dalessandro */ 1086f48ad614SDennis Dalessandro 1087f48ad614SDennis Dalessandro ret = zalloc_cpumask_var(&diff, GFP_KERNEL); 1088f48ad614SDennis Dalessandro if (!ret) 1089f48ad614SDennis Dalessandro goto done; 1090b094a36fSSebastian Sanchez ret = zalloc_cpumask_var(&hw_thread_mask, GFP_KERNEL); 1091f48ad614SDennis Dalessandro if (!ret) 1092f48ad614SDennis Dalessandro goto free_diff; 1093b094a36fSSebastian Sanchez ret = zalloc_cpumask_var(&available_mask, GFP_KERNEL); 1094f48ad614SDennis Dalessandro if (!ret) 1095b094a36fSSebastian Sanchez goto free_hw_thread_mask; 1096b094a36fSSebastian Sanchez ret = zalloc_cpumask_var(&intrs_mask, GFP_KERNEL); 1097b094a36fSSebastian Sanchez if (!ret) 1098b094a36fSSebastian Sanchez goto free_available_mask; 1099f48ad614SDennis Dalessandro 1100584d9577STadeusz Struk mutex_lock(&affinity->lock); 1101f48ad614SDennis Dalessandro /* 1102b094a36fSSebastian Sanchez * If we've used all available HW threads, clear the mask and start 1103f48ad614SDennis Dalessandro * overloading. 1104f48ad614SDennis Dalessandro */ 1105cf38ea10SSebastian Sanchez _cpu_mask_set_gen_inc(set); 1106f48ad614SDennis Dalessandro 1107d6373019SSebastian Sanchez /* 1108d6373019SSebastian Sanchez * If NUMA node has CPUs used by interrupt handlers, include them in the 1109d6373019SSebastian Sanchez * interrupt handler mask. 1110d6373019SSebastian Sanchez */ 1111d6373019SSebastian Sanchez entry = node_affinity_lookup(node); 1112d6373019SSebastian Sanchez if (entry) { 1113b094a36fSSebastian Sanchez cpumask_copy(intrs_mask, (entry->def_intr.gen ? 11144197344bSDennis Dalessandro &entry->def_intr.mask : 11154197344bSDennis Dalessandro &entry->def_intr.used)); 1116b094a36fSSebastian Sanchez cpumask_or(intrs_mask, intrs_mask, (entry->rcv_intr.gen ? 11174197344bSDennis Dalessandro &entry->rcv_intr.mask : 11184197344bSDennis Dalessandro &entry->rcv_intr.used)); 1119b094a36fSSebastian Sanchez cpumask_or(intrs_mask, intrs_mask, &entry->general_intr_mask); 1120d6373019SSebastian Sanchez } 1121f242d93aSLeon Romanovsky hfi1_cdbg(PROC, "CPUs used by interrupts: %*pbl", 1122b094a36fSSebastian Sanchez cpumask_pr_args(intrs_mask)); 1123b094a36fSSebastian Sanchez 1124b094a36fSSebastian Sanchez cpumask_copy(hw_thread_mask, &set->mask); 1125f48ad614SDennis Dalessandro 1126f48ad614SDennis Dalessandro /* 1127b094a36fSSebastian Sanchez * If HT cores are enabled, identify which HW threads within the 1128b094a36fSSebastian Sanchez * physical cores should be used. 1129f48ad614SDennis Dalessandro */ 1130b094a36fSSebastian Sanchez if (affinity->num_core_siblings > 0) { 1131b094a36fSSebastian Sanchez for (i = 0; i < affinity->num_core_siblings; i++) { 1132b094a36fSSebastian Sanchez find_hw_thread_mask(i, hw_thread_mask, affinity); 1133b094a36fSSebastian Sanchez 1134b094a36fSSebastian Sanchez /* 1135b094a36fSSebastian Sanchez * If there's at least one available core for this HW 1136b094a36fSSebastian Sanchez * thread number, stop looking for a core. 1137b094a36fSSebastian Sanchez * 1138b094a36fSSebastian Sanchez * diff will always be not empty at least once in this 1139b094a36fSSebastian Sanchez * loop as the used mask gets reset when 1140b094a36fSSebastian Sanchez * (set->mask == set->used) before this loop. 1141b094a36fSSebastian Sanchez */ 1142b094a36fSSebastian Sanchez cpumask_andnot(diff, hw_thread_mask, &set->used); 1143b094a36fSSebastian Sanchez if (!cpumask_empty(diff)) 1144b094a36fSSebastian Sanchez break; 1145b094a36fSSebastian Sanchez } 1146b094a36fSSebastian Sanchez } 1147b094a36fSSebastian Sanchez hfi1_cdbg(PROC, "Same available HW thread on all physical CPUs: %*pbl", 1148b094a36fSSebastian Sanchez cpumask_pr_args(hw_thread_mask)); 1149b094a36fSSebastian Sanchez 1150f48ad614SDennis Dalessandro node_mask = cpumask_of_node(node); 1151b094a36fSSebastian Sanchez hfi1_cdbg(PROC, "Device on NUMA %u, CPUs %*pbl", node, 1152f242d93aSLeon Romanovsky cpumask_pr_args(node_mask)); 1153f48ad614SDennis Dalessandro 1154b094a36fSSebastian Sanchez /* Get cpumask of available CPUs on preferred NUMA */ 1155b094a36fSSebastian Sanchez cpumask_and(available_mask, hw_thread_mask, node_mask); 1156b094a36fSSebastian Sanchez cpumask_andnot(available_mask, available_mask, &set->used); 1157b094a36fSSebastian Sanchez hfi1_cdbg(PROC, "Available CPUs on NUMA %u: %*pbl", node, 1158b094a36fSSebastian Sanchez cpumask_pr_args(available_mask)); 1159f48ad614SDennis Dalessandro 1160b094a36fSSebastian Sanchez /* 1161b094a36fSSebastian Sanchez * At first, we don't want to place processes on the same 1162b094a36fSSebastian Sanchez * CPUs as interrupt handlers. Then, CPUs running interrupt 1163b094a36fSSebastian Sanchez * handlers are used. 1164b094a36fSSebastian Sanchez * 1165b094a36fSSebastian Sanchez * 1) If diff is not empty, then there are CPUs not running 1166b094a36fSSebastian Sanchez * non-interrupt handlers available, so diff gets copied 1167b094a36fSSebastian Sanchez * over to available_mask. 1168b094a36fSSebastian Sanchez * 2) If diff is empty, then all CPUs not running interrupt 1169b094a36fSSebastian Sanchez * handlers are taken, so available_mask contains all 1170b094a36fSSebastian Sanchez * available CPUs running interrupt handlers. 1171b094a36fSSebastian Sanchez * 3) If available_mask is empty, then all CPUs on the 1172b094a36fSSebastian Sanchez * preferred NUMA node are taken, so other NUMA nodes are 1173b094a36fSSebastian Sanchez * used for process assignments using the same method as 1174b094a36fSSebastian Sanchez * the preferred NUMA node. 1175b094a36fSSebastian Sanchez */ 1176b094a36fSSebastian Sanchez cpumask_andnot(diff, available_mask, intrs_mask); 1177b094a36fSSebastian Sanchez if (!cpumask_empty(diff)) 1178b094a36fSSebastian Sanchez cpumask_copy(available_mask, diff); 1179b094a36fSSebastian Sanchez 1180b094a36fSSebastian Sanchez /* If we don't have CPUs on the preferred node, use other NUMA nodes */ 1181b094a36fSSebastian Sanchez if (cpumask_empty(available_mask)) { 1182b094a36fSSebastian Sanchez cpumask_andnot(available_mask, hw_thread_mask, &set->used); 1183b094a36fSSebastian Sanchez /* Excluding preferred NUMA cores */ 1184b094a36fSSebastian Sanchez cpumask_andnot(available_mask, available_mask, node_mask); 1185b094a36fSSebastian Sanchez hfi1_cdbg(PROC, 1186b094a36fSSebastian Sanchez "Preferred NUMA node cores are taken, cores available in other NUMA nodes: %*pbl", 1187b094a36fSSebastian Sanchez cpumask_pr_args(available_mask)); 1188f48ad614SDennis Dalessandro 1189f48ad614SDennis Dalessandro /* 1190f48ad614SDennis Dalessandro * At first, we don't want to place processes on the same 1191f48ad614SDennis Dalessandro * CPUs as interrupt handlers. 1192f48ad614SDennis Dalessandro */ 1193b094a36fSSebastian Sanchez cpumask_andnot(diff, available_mask, intrs_mask); 1194f48ad614SDennis Dalessandro if (!cpumask_empty(diff)) 1195b094a36fSSebastian Sanchez cpumask_copy(available_mask, diff); 1196f48ad614SDennis Dalessandro } 1197b094a36fSSebastian Sanchez hfi1_cdbg(PROC, "Possible CPUs for process: %*pbl", 1198b094a36fSSebastian Sanchez cpumask_pr_args(available_mask)); 1199f48ad614SDennis Dalessandro 1200b094a36fSSebastian Sanchez cpu = cpumask_first(available_mask); 1201f48ad614SDennis Dalessandro if (cpu >= nr_cpu_ids) /* empty */ 1202f48ad614SDennis Dalessandro cpu = -1; 1203f48ad614SDennis Dalessandro else 1204f48ad614SDennis Dalessandro cpumask_set_cpu(cpu, &set->used); 1205584d9577STadeusz Struk 1206584d9577STadeusz Struk mutex_unlock(&affinity->lock); 1207b094a36fSSebastian Sanchez hfi1_cdbg(PROC, "Process assigned to CPU %d", cpu); 1208f48ad614SDennis Dalessandro 1209b094a36fSSebastian Sanchez free_cpumask_var(intrs_mask); 1210b094a36fSSebastian Sanchez free_available_mask: 1211b094a36fSSebastian Sanchez free_cpumask_var(available_mask); 1212b094a36fSSebastian Sanchez free_hw_thread_mask: 1213b094a36fSSebastian Sanchez free_cpumask_var(hw_thread_mask); 1214f48ad614SDennis Dalessandro free_diff: 1215f48ad614SDennis Dalessandro free_cpumask_var(diff); 1216f48ad614SDennis Dalessandro done: 1217f48ad614SDennis Dalessandro return cpu; 1218f48ad614SDennis Dalessandro } 1219f48ad614SDennis Dalessandro 1220b094a36fSSebastian Sanchez void hfi1_put_proc_affinity(int cpu) 1221f48ad614SDennis Dalessandro { 1222b094a36fSSebastian Sanchez struct hfi1_affinity_node_list *affinity = &node_affinity; 1223b094a36fSSebastian Sanchez struct cpu_mask_set *set = &affinity->proc; 1224f48ad614SDennis Dalessandro 1225f48ad614SDennis Dalessandro if (cpu < 0) 1226f48ad614SDennis Dalessandro return; 1227584d9577STadeusz Struk 1228584d9577STadeusz Struk mutex_lock(&affinity->lock); 1229cf38ea10SSebastian Sanchez cpu_mask_set_put(set, cpu); 1230b094a36fSSebastian Sanchez hfi1_cdbg(PROC, "Returning CPU %d for future process assignment", cpu); 1231584d9577STadeusz Struk mutex_unlock(&affinity->lock); 1232f48ad614SDennis Dalessandro } 1233