blob: c66ca7b1d31ac3717f14d319e6e64d2c7eac7df2 [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 */
11#include <linux/config.h>
12#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>
18#include <asm/smp.h>
19#include <asm/ipi.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static cpumask_t flat_target_cpus(void)
22{
23 return cpu_online_map;
24}
25
26/*
27 * Set up the logical destination ID.
28 *
29 * Intel recommends to set DFR, LDR and TPR before enabling
30 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
31 * document number 292116). So here it goes...
32 */
33static void flat_init_apic_ldr(void)
34{
35 unsigned long val;
36 unsigned long num, id;
37
38 num = smp_processor_id();
39 id = 1UL << num;
40 x86_cpu_to_log_apicid[num] = id;
Andi Kleeneddfb4e2005-09-12 18:49:23 +020041 apic_write(APIC_DFR, APIC_DFR_FLAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
43 val |= SET_APIC_LOGICAL_ID(id);
Andi Kleeneddfb4e2005-09-12 18:49:23 +020044 apic_write(APIC_LDR, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static void flat_send_IPI_mask(cpumask_t cpumask, int vector)
48{
49 unsigned long mask = cpus_addr(cpumask)[0];
50 unsigned long cfg;
51 unsigned long flags;
52
53 local_save_flags(flags);
54 local_irq_disable();
55
56 /*
57 * Wait for idle.
58 */
59 apic_wait_icr_idle();
60
61 /*
62 * prepare target chip field
63 */
64 cfg = __prepare_ICR2(mask);
Andi Kleeneddfb4e2005-09-12 18:49:23 +020065 apic_write(APIC_ICR2, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 /*
68 * program the ICR
69 */
70 cfg = __prepare_ICR(0, vector, APIC_DEST_LOGICAL);
71
72 /*
73 * Send the IPI. The write to APIC_ICR fires this off.
74 */
Andi Kleeneddfb4e2005-09-12 18:49:23 +020075 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 local_irq_restore(flags);
77}
78
Ashok Raj884d9e42005-06-25 14:55:02 -070079static void flat_send_IPI_allbutself(int vector)
80{
Ashok Rajfdf26d92005-09-09 13:01:52 -070081#ifndef CONFIG_HOTPLUG_CPU
Ashok Raja02c4cb2005-06-25 14:55:03 -070082 if (((num_online_cpus()) - 1) >= 1)
Andi Kleen37a47e62005-07-28 21:15:41 -070083 __send_IPI_shortcut(APIC_DEST_ALLBUT, vector,APIC_DEST_LOGICAL);
Ashok Rajfdf26d92005-09-09 13:01:52 -070084#else
85 cpumask_t allbutme = cpu_online_map;
Zwane Mwaikambo329d4002006-01-11 22:43:09 +010086
87 cpu_clear(smp_processor_id(), allbutme);
Ashok Rajfdf26d92005-09-09 13:01:52 -070088
89 if (!cpus_empty(allbutme))
90 flat_send_IPI_mask(allbutme, vector);
Ashok Rajfdf26d92005-09-09 13:01:52 -070091#endif
Ashok Raj884d9e42005-06-25 14:55:02 -070092}
93
94static void flat_send_IPI_all(int vector)
95{
Andi Kleen37a47e62005-07-28 21:15:41 -070096 __send_IPI_shortcut(APIC_DEST_ALLINC, vector, APIC_DEST_LOGICAL);
Ashok Raj884d9e42005-06-25 14:55:02 -070097}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int flat_apic_id_registered(void)
100{
101 return physid_isset(GET_APIC_ID(apic_read(APIC_ID)), phys_cpu_present_map);
102}
103
104static unsigned int flat_cpu_mask_to_apicid(cpumask_t cpumask)
105{
106 return cpus_addr(cpumask)[0] & APIC_ALL_CPUS;
107}
108
109static unsigned int phys_pkg_id(int index_msb)
110{
ravikiran thirumalai0f4fdb72006-06-26 13:56:04 +0200111 return hard_smp_processor_id() >> index_msb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114struct genapic apic_flat = {
115 .name = "flat",
116 .int_delivery_mode = dest_LowestPrio,
117 .int_dest_mode = (APIC_DEST_LOGICAL != 0),
118 .int_delivery_dest = APIC_DEST_LOGICAL | APIC_DM_LOWEST,
119 .target_cpus = flat_target_cpus,
120 .apic_id_registered = flat_apic_id_registered,
121 .init_apic_ldr = flat_init_apic_ldr,
122 .send_IPI_all = flat_send_IPI_all,
123 .send_IPI_allbutself = flat_send_IPI_allbutself,
124 .send_IPI_mask = flat_send_IPI_mask,
125 .cpu_mask_to_apicid = flat_cpu_mask_to_apicid,
126 .phys_pkg_id = phys_pkg_id,
127};
Andi Kleenf8d31192005-07-28 21:15:42 -0700128
129/*
130 * Physflat mode is used when there are more than 8 CPUs on a AMD system.
131 * We cannot use logical delivery in this case because the mask
132 * overflows, so use physical mode.
133 */
134
135static cpumask_t physflat_target_cpus(void)
136{
137 return cpumask_of_cpu(0);
138}
139
140static void physflat_send_IPI_mask(cpumask_t cpumask, int vector)
141{
142 send_IPI_mask_sequence(cpumask, vector);
143}
144
145static void physflat_send_IPI_allbutself(int vector)
146{
147 cpumask_t allbutme = cpu_online_map;
Zwane Mwaikambo329d4002006-01-11 22:43:09 +0100148
149 cpu_clear(smp_processor_id(), allbutme);
Andi Kleenf8d31192005-07-28 21:15:42 -0700150 physflat_send_IPI_mask(allbutme, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700151}
152
153static void physflat_send_IPI_all(int vector)
154{
155 physflat_send_IPI_mask(cpu_online_map, vector);
156}
157
158static unsigned int physflat_cpu_mask_to_apicid(cpumask_t cpumask)
159{
160 int cpu;
161
162 /*
163 * We're using fixed IRQ delivery, can only return one phys APIC ID.
164 * May as well be the first.
165 */
166 cpu = first_cpu(cpumask);
167 if ((unsigned)cpu < NR_CPUS)
168 return x86_cpu_to_apicid[cpu];
169 else
170 return BAD_APICID;
171}
172
173struct genapic apic_physflat = {
174 .name = "physical flat",
Ashok Rajc1a71a12005-09-12 18:49:24 +0200175 .int_delivery_mode = dest_Fixed,
Andi Kleenf8d31192005-07-28 21:15:42 -0700176 .int_dest_mode = (APIC_DEST_PHYSICAL != 0),
Ashok Rajc1a71a12005-09-12 18:49:24 +0200177 .int_delivery_dest = APIC_DEST_PHYSICAL | APIC_DM_FIXED,
Andi Kleenf8d31192005-07-28 21:15:42 -0700178 .target_cpus = physflat_target_cpus,
179 .apic_id_registered = flat_apic_id_registered,
180 .init_apic_ldr = flat_init_apic_ldr,/*not needed, but shouldn't hurt*/
181 .send_IPI_all = physflat_send_IPI_all,
182 .send_IPI_allbutself = physflat_send_IPI_allbutself,
183 .send_IPI_mask = physflat_send_IPI_mask,
184 .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
185 .phys_pkg_id = phys_pkg_id,
186};