xref: /openbmc/linux/arch/x86/kernel/apic/apic_common.c (revision 9f9e3bb1)
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 
99f9e3bb1SThomas Gleixner u32 apic_default_calc_apicid(unsigned int cpu)
109f9e3bb1SThomas Gleixner {
119f9e3bb1SThomas Gleixner 	return per_cpu(x86_cpu_to_apicid, cpu);
129f9e3bb1SThomas Gleixner }
139f9e3bb1SThomas Gleixner 
1483a10522SThomas Gleixner int default_cpu_mask_to_apicid(const struct cpumask *msk, struct irq_data *irqd,
1583a10522SThomas Gleixner 			       unsigned int *apicid)
1683a10522SThomas Gleixner {
1783a10522SThomas Gleixner 	unsigned int cpu = cpumask_first(msk);
1883a10522SThomas Gleixner 
1983a10522SThomas Gleixner 	if (cpu >= nr_cpu_ids)
2083a10522SThomas Gleixner 		return -EINVAL;
2183a10522SThomas Gleixner 	*apicid = per_cpu(x86_cpu_to_apicid, cpu);
2283a10522SThomas Gleixner 	irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
2383a10522SThomas Gleixner 	return 0;
2483a10522SThomas Gleixner }
2583a10522SThomas Gleixner 
269f9e3bb1SThomas Gleixner u32 apic_flat_calc_apicid(unsigned int cpu)
279f9e3bb1SThomas Gleixner {
289f9e3bb1SThomas Gleixner 	return 1U << cpu;
299f9e3bb1SThomas Gleixner }
309f9e3bb1SThomas Gleixner 
3183a10522SThomas Gleixner int flat_cpu_mask_to_apicid(const struct cpumask *mask, struct irq_data *irqd,
3283a10522SThomas Gleixner 			    unsigned int *apicid)
3383a10522SThomas Gleixner 
3483a10522SThomas Gleixner {
3583a10522SThomas Gleixner 	struct cpumask *effmsk = irq_data_get_effective_affinity_mask(irqd);
3683a10522SThomas Gleixner 	unsigned long cpu_mask = cpumask_bits(mask)[0] & APIC_ALL_CPUS;
3783a10522SThomas Gleixner 
3883a10522SThomas Gleixner 	if (!cpu_mask)
3983a10522SThomas Gleixner 		return -EINVAL;
4083a10522SThomas Gleixner 	*apicid = (unsigned int)cpu_mask;
4183a10522SThomas Gleixner 	cpumask_bits(effmsk)[0] = cpu_mask;
4283a10522SThomas Gleixner 	return 0;
4383a10522SThomas Gleixner }
4483a10522SThomas Gleixner 
4583a10522SThomas Gleixner bool default_check_apicid_used(physid_mask_t *map, int apicid)
4683a10522SThomas Gleixner {
4783a10522SThomas Gleixner 	return physid_isset(apicid, *map);
4883a10522SThomas Gleixner }
4983a10522SThomas Gleixner 
5083a10522SThomas Gleixner void flat_vector_allocation_domain(int cpu, struct cpumask *retmask,
5183a10522SThomas Gleixner 				   const struct cpumask *mask)
5283a10522SThomas Gleixner {
5383a10522SThomas Gleixner 	/*
5483a10522SThomas Gleixner 	 * Careful. Some cpus do not strictly honor the set of cpus
5583a10522SThomas Gleixner 	 * specified in the interrupt destination when using lowest
5683a10522SThomas Gleixner 	 * priority interrupt delivery mode.
5783a10522SThomas Gleixner 	 *
5883a10522SThomas Gleixner 	 * In particular there was a hyperthreading cpu observed to
5983a10522SThomas Gleixner 	 * deliver interrupts to the wrong hyperthread when only one
6083a10522SThomas Gleixner 	 * hyperthread was specified in the interrupt desitination.
6183a10522SThomas Gleixner 	 */
6283a10522SThomas Gleixner 	cpumask_clear(retmask);
6383a10522SThomas Gleixner 	cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
6483a10522SThomas Gleixner }
6583a10522SThomas Gleixner 
6683a10522SThomas Gleixner void default_vector_allocation_domain(int cpu, struct cpumask *retmask,
6783a10522SThomas Gleixner 				      const struct cpumask *mask)
6883a10522SThomas Gleixner {
6983a10522SThomas Gleixner 	cpumask_copy(retmask, cpumask_of(cpu));
7083a10522SThomas Gleixner }
7183a10522SThomas Gleixner 
7283a10522SThomas Gleixner void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap)
7383a10522SThomas Gleixner {
7483a10522SThomas Gleixner 	*retmap = *phys_map;
7583a10522SThomas Gleixner }
7683a10522SThomas Gleixner 
7764063505SThomas Gleixner int default_cpu_present_to_apicid(int mps_cpu)
7864063505SThomas Gleixner {
7964063505SThomas Gleixner 	if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu))
8064063505SThomas Gleixner 		return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu);
8164063505SThomas Gleixner 	else
8264063505SThomas Gleixner 		return BAD_APICID;
8364063505SThomas Gleixner }
8483a10522SThomas Gleixner EXPORT_SYMBOL_GPL(default_cpu_present_to_apicid);
8564063505SThomas Gleixner 
8664063505SThomas Gleixner int default_check_phys_apicid_present(int phys_apicid)
8764063505SThomas Gleixner {
8864063505SThomas Gleixner 	return physid_isset(phys_apicid, phys_cpu_present_map);
8964063505SThomas Gleixner }
9083a10522SThomas Gleixner 
9183a10522SThomas Gleixner int default_apic_id_valid(int apicid)
9283a10522SThomas Gleixner {
9383a10522SThomas Gleixner 	return (apicid < 255);
9483a10522SThomas Gleixner }
95