xref: /openbmc/linux/arch/x86/kernel/apic/apic_common.c (revision 83a10522)
164063505SThomas Gleixner /*
264063505SThomas Gleixner  * Common functions shared between the various APIC flavours
364063505SThomas Gleixner  *
464063505SThomas Gleixner  * SPDX-License-Identifier: GPL-2.0
564063505SThomas Gleixner  */
664063505SThomas Gleixner #include <linux/irq.h>
764063505SThomas Gleixner #include <asm/apic.h>
864063505SThomas Gleixner 
983a10522SThomas Gleixner int default_cpu_mask_to_apicid(const struct cpumask *msk, struct irq_data *irqd,
1083a10522SThomas Gleixner 			       unsigned int *apicid)
1183a10522SThomas Gleixner {
1283a10522SThomas Gleixner 	unsigned int cpu = cpumask_first(msk);
1383a10522SThomas Gleixner 
1483a10522SThomas Gleixner 	if (cpu >= nr_cpu_ids)
1583a10522SThomas Gleixner 		return -EINVAL;
1683a10522SThomas Gleixner 	*apicid = per_cpu(x86_cpu_to_apicid, cpu);
1783a10522SThomas Gleixner 	irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
1883a10522SThomas Gleixner 	return 0;
1983a10522SThomas Gleixner }
2083a10522SThomas Gleixner 
2183a10522SThomas Gleixner int flat_cpu_mask_to_apicid(const struct cpumask *mask, struct irq_data *irqd,
2283a10522SThomas Gleixner 			    unsigned int *apicid)
2383a10522SThomas Gleixner 
2483a10522SThomas Gleixner {
2583a10522SThomas Gleixner 	struct cpumask *effmsk = irq_data_get_effective_affinity_mask(irqd);
2683a10522SThomas Gleixner 	unsigned long cpu_mask = cpumask_bits(mask)[0] & APIC_ALL_CPUS;
2783a10522SThomas Gleixner 
2883a10522SThomas Gleixner 	if (!cpu_mask)
2983a10522SThomas Gleixner 		return -EINVAL;
3083a10522SThomas Gleixner 	*apicid = (unsigned int)cpu_mask;
3183a10522SThomas Gleixner 	cpumask_bits(effmsk)[0] = cpu_mask;
3283a10522SThomas Gleixner 	return 0;
3383a10522SThomas Gleixner }
3483a10522SThomas Gleixner 
3583a10522SThomas Gleixner bool default_check_apicid_used(physid_mask_t *map, int apicid)
3683a10522SThomas Gleixner {
3783a10522SThomas Gleixner 	return physid_isset(apicid, *map);
3883a10522SThomas Gleixner }
3983a10522SThomas Gleixner 
4083a10522SThomas Gleixner void flat_vector_allocation_domain(int cpu, struct cpumask *retmask,
4183a10522SThomas Gleixner 				   const struct cpumask *mask)
4283a10522SThomas Gleixner {
4383a10522SThomas Gleixner 	/*
4483a10522SThomas Gleixner 	 * Careful. Some cpus do not strictly honor the set of cpus
4583a10522SThomas Gleixner 	 * specified in the interrupt destination when using lowest
4683a10522SThomas Gleixner 	 * priority interrupt delivery mode.
4783a10522SThomas Gleixner 	 *
4883a10522SThomas Gleixner 	 * In particular there was a hyperthreading cpu observed to
4983a10522SThomas Gleixner 	 * deliver interrupts to the wrong hyperthread when only one
5083a10522SThomas Gleixner 	 * hyperthread was specified in the interrupt desitination.
5183a10522SThomas Gleixner 	 */
5283a10522SThomas Gleixner 	cpumask_clear(retmask);
5383a10522SThomas Gleixner 	cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
5483a10522SThomas Gleixner }
5583a10522SThomas Gleixner 
5683a10522SThomas Gleixner void default_vector_allocation_domain(int cpu, struct cpumask *retmask,
5783a10522SThomas Gleixner 				      const struct cpumask *mask)
5883a10522SThomas Gleixner {
5983a10522SThomas Gleixner 	cpumask_copy(retmask, cpumask_of(cpu));
6083a10522SThomas Gleixner }
6183a10522SThomas Gleixner 
6283a10522SThomas Gleixner void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap)
6383a10522SThomas Gleixner {
6483a10522SThomas Gleixner 	*retmap = *phys_map;
6583a10522SThomas Gleixner }
6683a10522SThomas Gleixner 
6764063505SThomas Gleixner int default_cpu_present_to_apicid(int mps_cpu)
6864063505SThomas Gleixner {
6964063505SThomas Gleixner 	if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu))
7064063505SThomas Gleixner 		return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu);
7164063505SThomas Gleixner 	else
7264063505SThomas Gleixner 		return BAD_APICID;
7364063505SThomas Gleixner }
7483a10522SThomas Gleixner EXPORT_SYMBOL_GPL(default_cpu_present_to_apicid);
7564063505SThomas Gleixner 
7664063505SThomas Gleixner int default_check_phys_apicid_present(int phys_apicid)
7764063505SThomas Gleixner {
7864063505SThomas Gleixner 	return physid_isset(phys_apicid, phys_cpu_present_map);
7964063505SThomas Gleixner }
8083a10522SThomas Gleixner 
8183a10522SThomas Gleixner const struct cpumask *default_target_cpus(void)
8283a10522SThomas Gleixner {
8383a10522SThomas Gleixner #ifdef CONFIG_SMP
8483a10522SThomas Gleixner 	return cpu_online_mask;
8583a10522SThomas Gleixner #else
8683a10522SThomas Gleixner 	return cpumask_of(0);
8783a10522SThomas Gleixner #endif
8883a10522SThomas Gleixner }
8983a10522SThomas Gleixner 
9083a10522SThomas Gleixner const struct cpumask *online_target_cpus(void)
9183a10522SThomas Gleixner {
9283a10522SThomas Gleixner 	return cpu_online_mask;
9383a10522SThomas Gleixner }
9483a10522SThomas Gleixner 
9583a10522SThomas Gleixner int default_apic_id_valid(int apicid)
9683a10522SThomas Gleixner {
9783a10522SThomas Gleixner 	return (apicid < 255);
9883a10522SThomas Gleixner }
99