blob: 735586822135b4c891cddfe0eb0b82e6477955b9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2004 James Cleverdon, IBM.
3 * Subject to the GNU Public License, v.2
4 *
Andi Kleenf8d31192005-07-28 21:15:42 -07005 * Flat APIC subarch code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Hacked for x86-64 by James Cleverdon from i386 architecture code by
8 * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
9 * James Cleverdon.
10 */
Andi Kleenf19cccf2007-05-02 19:27:21 +020011#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/threads.h>
13#include <linux/cpumask.h>
14#include <linux/string.h>
15#include <linux/kernel.h>
16#include <linux/ctype.h>
17#include <linux/init.h>
Suresh Siddha0c81c742008-07-10 11:16:48 -070018#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/smp.h>
20#include <asm/ipi.h>
Jan Beulich00f1ea62007-05-02 19:27:04 +020021#include <asm/genapic.h>
Suresh Siddha0c81c742008-07-10 11:16:48 -070022#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static cpumask_t flat_target_cpus(void)
25{
26 return cpu_online_map;
27}
28
Eric W. Biedermanc7111c132006-10-08 07:47:55 -060029static cpumask_t flat_vector_allocation_domain(int cpu)
30{
31 /* Careful. Some cpus do not strictly honor the set of cpus
32 * specified in the interrupt destination when using lowest
33 * priority interrupt delivery mode.
34 *
35 * In particular there was a hyperthreading cpu observed to
36 * deliver interrupts to the wrong hyperthread when only one
37 * hyperthread was specified in the interrupt desitination.
38 */
39 cpumask_t domain = { { [0] = APIC_ALL_CPUS, } };
40 return domain;
41}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * Set up the logical destination ID.
45 *
46 * Intel recommends to set DFR, LDR and TPR before enabling
47 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
48 * document number 292116). So here it goes...
49 */
50static void flat_init_apic_ldr(void)
51{
52 unsigned long val;
53 unsigned long num, id;
54
55 num = smp_processor_id();
56 id = 1UL << num;
Andi Kleeneddfb4e2005-09-12 18:49:23 +020057 apic_write(APIC_DFR, APIC_DFR_FLAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
59 val |= SET_APIC_LOGICAL_ID(id);
Andi Kleeneddfb4e2005-09-12 18:49:23 +020060 apic_write(APIC_LDR, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void flat_send_IPI_mask(cpumask_t cpumask, int vector)
64{
65 unsigned long mask = cpus_addr(cpumask)[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 unsigned long flags;
67
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +020068 local_irq_save(flags);
Fernando Luis [** ISO-8859-1 charset **] VázquezCao9062d882007-05-02 19:27:18 +020069 __send_IPI_dest_field(mask, vector, APIC_DEST_LOGICAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 local_irq_restore(flags);
71}
72
Ashok Raj884d9e42005-06-25 14:55:02 -070073static void flat_send_IPI_allbutself(int vector)
74{
Keith Owense77deac2006-06-26 13:59:56 +020075#ifdef CONFIG_HOTPLUG_CPU
76 int hotplug = 1;
Ashok Rajfdf26d92005-09-09 13:01:52 -070077#else
Keith Owense77deac2006-06-26 13:59:56 +020078 int hotplug = 0;
Ashok Rajfdf26d92005-09-09 13:01:52 -070079#endif
Keith Owense77deac2006-06-26 13:59:56 +020080 if (hotplug || vector == NMI_VECTOR) {
81 cpumask_t allbutme = cpu_online_map;
82
83 cpu_clear(smp_processor_id(), allbutme);
84
85 if (!cpus_empty(allbutme))
86 flat_send_IPI_mask(allbutme, vector);
87 } else if (num_online_cpus() > 1) {
88 __send_IPI_shortcut(APIC_DEST_ALLBUT, vector,APIC_DEST_LOGICAL);
89 }
Ashok Raj884d9e42005-06-25 14:55:02 -070090}
91
92static void flat_send_IPI_all(int vector)
93{
Keith Owense77deac2006-06-26 13:59:56 +020094 if (vector == NMI_VECTOR)
95 flat_send_IPI_mask(cpu_online_map, vector);
96 else
97 __send_IPI_shortcut(APIC_DEST_ALLINC, vector, APIC_DEST_LOGICAL);
Ashok Raj884d9e42005-06-25 14:55:02 -070098}
99
Suresh Siddha0c81c742008-07-10 11:16:48 -0700100static unsigned int read_xapic_id(void)
101{
102 unsigned int id;
103
104 id = GET_XAPIC_ID(apic_read(APIC_ID));
105 return id;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int flat_apic_id_registered(void)
109{
Suresh Siddha0c81c742008-07-10 11:16:48 -0700110 return physid_isset(read_xapic_id(), phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static unsigned int flat_cpu_mask_to_apicid(cpumask_t cpumask)
114{
115 return cpus_addr(cpumask)[0] & APIC_ALL_CPUS;
116}
117
118static unsigned int phys_pkg_id(int index_msb)
119{
ravikiran thirumalai0f4fdb72006-06-26 13:56:04 +0200120 return hard_smp_processor_id() >> index_msb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123struct genapic apic_flat = {
124 .name = "flat",
125 .int_delivery_mode = dest_LowestPrio,
126 .int_dest_mode = (APIC_DEST_LOGICAL != 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .target_cpus = flat_target_cpus,
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600128 .vector_allocation_domain = flat_vector_allocation_domain,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .apic_id_registered = flat_apic_id_registered,
130 .init_apic_ldr = flat_init_apic_ldr,
131 .send_IPI_all = flat_send_IPI_all,
132 .send_IPI_allbutself = flat_send_IPI_allbutself,
133 .send_IPI_mask = flat_send_IPI_mask,
Suresh Siddhacff73a62008-07-10 11:16:53 -0700134 .send_IPI_self = apic_send_IPI_self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .cpu_mask_to_apicid = flat_cpu_mask_to_apicid,
136 .phys_pkg_id = phys_pkg_id,
Suresh Siddha0c81c742008-07-10 11:16:48 -0700137 .read_apic_id = read_xapic_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
Andi Kleenf8d31192005-07-28 21:15:42 -0700139
140/*
141 * Physflat mode is used when there are more than 8 CPUs on a AMD system.
142 * We cannot use logical delivery in this case because the mask
143 * overflows, so use physical mode.
144 */
145
146static cpumask_t physflat_target_cpus(void)
147{
Eric W. Biederman84f404f2006-10-21 18:37:02 +0200148 return cpu_online_map;
Andi Kleenf8d31192005-07-28 21:15:42 -0700149}
150
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600151static cpumask_t physflat_vector_allocation_domain(int cpu)
152{
Akinobu Mita7281c962008-04-05 22:39:08 +0900153 return cpumask_of_cpu(cpu);
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600154}
155
Andi Kleenf8d31192005-07-28 21:15:42 -0700156static void physflat_send_IPI_mask(cpumask_t cpumask, int vector)
157{
158 send_IPI_mask_sequence(cpumask, vector);
159}
160
161static void physflat_send_IPI_allbutself(int vector)
162{
163 cpumask_t allbutme = cpu_online_map;
Zwane Mwaikambo329d4002006-01-11 22:43:09 +0100164
165 cpu_clear(smp_processor_id(), allbutme);
Andi Kleenf8d31192005-07-28 21:15:42 -0700166 physflat_send_IPI_mask(allbutme, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700167}
168
169static void physflat_send_IPI_all(int vector)
170{
171 physflat_send_IPI_mask(cpu_online_map, vector);
172}
173
174static unsigned int physflat_cpu_mask_to_apicid(cpumask_t cpumask)
175{
176 int cpu;
177
178 /*
179 * We're using fixed IRQ delivery, can only return one phys APIC ID.
180 * May as well be the first.
181 */
182 cpu = first_cpu(cpumask);
183 if ((unsigned)cpu < NR_CPUS)
Mike Travis71fff5e2007-10-19 20:35:03 +0200184 return per_cpu(x86_cpu_to_apicid, cpu);
Andi Kleenf8d31192005-07-28 21:15:42 -0700185 else
186 return BAD_APICID;
187}
188
189struct genapic apic_physflat = {
190 .name = "physical flat",
Ashok Rajc1a71a12005-09-12 18:49:24 +0200191 .int_delivery_mode = dest_Fixed,
Andi Kleenf8d31192005-07-28 21:15:42 -0700192 .int_dest_mode = (APIC_DEST_PHYSICAL != 0),
Andi Kleenf8d31192005-07-28 21:15:42 -0700193 .target_cpus = physflat_target_cpus,
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600194 .vector_allocation_domain = physflat_vector_allocation_domain,
Andi Kleenf8d31192005-07-28 21:15:42 -0700195 .apic_id_registered = flat_apic_id_registered,
196 .init_apic_ldr = flat_init_apic_ldr,/*not needed, but shouldn't hurt*/
197 .send_IPI_all = physflat_send_IPI_all,
198 .send_IPI_allbutself = physflat_send_IPI_allbutself,
199 .send_IPI_mask = physflat_send_IPI_mask,
Suresh Siddhacff73a62008-07-10 11:16:53 -0700200 .send_IPI_self = apic_send_IPI_self,
Andi Kleenf8d31192005-07-28 21:15:42 -0700201 .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
202 .phys_pkg_id = phys_pkg_id,
Suresh Siddha0c81c742008-07-10 11:16:48 -0700203 .read_apic_id = read_xapic_id,
Andi Kleenf8d31192005-07-28 21:15:42 -0700204};