blob: 6b96a7374f94756cf8af40421b40ccae7b0734e8 [file] [log] [blame]
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +01001/*
2 * vSMPowered(tm) systems specific initialization
3 * Copyright (C) 2005 ScaleMP Inc.
4 *
5 * Use of this code is subject to the terms and conditions of the
6 * GNU general public license version 2. See "COPYING" or
7 * http://www.gnu.org/licenses/gpl.html
8 *
9 * Ravikiran Thirumalai <kiran@scalemp.com>,
10 * Shai Fultheim <shai@scalemp.com>
Glauber Costa96597fd2008-02-11 17:16:04 -020011 * Paravirt ops integration: Glauber de Oliveira Costa <gcosta@redhat.com>,
12 * Ravikiran Thirumalai <kiran@scalemp.com>
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +010013 */
14
15#include <linux/init.h>
16#include <linux/pci_ids.h>
17#include <linux/pci_regs.h>
Shai Fultheimead91d42012-04-16 10:39:35 +030018#include <linux/smp.h>
Ravikiran Thirumalai110c1e12012-06-03 01:11:35 +030019#include <linux/irq.h>
Thomas Gleixnereef8f872008-05-12 15:43:34 +020020
21#include <asm/apic.h>
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +010022#include <asm/pci-direct.h>
Ravikiran Thirumalai734c4c62006-10-12 12:17:52 -070023#include <asm/io.h>
Glauber Costa96597fd2008-02-11 17:16:04 -020024#include <asm/paravirt.h>
Thomas Gleixnereef8f872008-05-12 15:43:34 +020025#include <asm/setup.h>
Glauber Costa96597fd2008-02-11 17:16:04 -020026
Shai Fultheimead91d42012-04-16 10:39:35 +030027#define TOPOLOGY_REGISTER_OFFSET 0x10
28
Ravikiran G Thirumalai70511132009-03-23 23:14:29 -070029#if defined CONFIG_PCI && defined CONFIG_PARAVIRT
Glauber Costa96597fd2008-02-11 17:16:04 -020030/*
31 * Interrupt control on vSMPowered systems:
32 * ~AC is a shadow of IF. If IF is 'on' AC should be 'off'
33 * and vice versa.
34 */
35
36static unsigned long vsmp_save_fl(void)
37{
38 unsigned long flags = native_save_fl();
39
40 if (!(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC))
41 flags &= ~X86_EFLAGS_IF;
42 return flags;
43}
Jeremy Fitzhardingeecb93d12009-01-28 14:35:05 -080044PV_CALLEE_SAVE_REGS_THUNK(vsmp_save_fl);
Glauber Costa96597fd2008-02-11 17:16:04 -020045
46static void vsmp_restore_fl(unsigned long flags)
47{
48 if (flags & X86_EFLAGS_IF)
49 flags &= ~X86_EFLAGS_AC;
50 else
51 flags |= X86_EFLAGS_AC;
52 native_restore_fl(flags);
53}
Jeremy Fitzhardingeecb93d12009-01-28 14:35:05 -080054PV_CALLEE_SAVE_REGS_THUNK(vsmp_restore_fl);
Glauber Costa96597fd2008-02-11 17:16:04 -020055
56static void vsmp_irq_disable(void)
57{
58 unsigned long flags = native_save_fl();
59
60 native_restore_fl((flags & ~X86_EFLAGS_IF) | X86_EFLAGS_AC);
61}
Jeremy Fitzhardingeecb93d12009-01-28 14:35:05 -080062PV_CALLEE_SAVE_REGS_THUNK(vsmp_irq_disable);
Glauber Costa96597fd2008-02-11 17:16:04 -020063
64static void vsmp_irq_enable(void)
65{
66 unsigned long flags = native_save_fl();
67
68 native_restore_fl((flags | X86_EFLAGS_IF) & (~X86_EFLAGS_AC));
69}
Jeremy Fitzhardingeecb93d12009-01-28 14:35:05 -080070PV_CALLEE_SAVE_REGS_THUNK(vsmp_irq_enable);
Glauber Costa96597fd2008-02-11 17:16:04 -020071
Ravikiran G Thirumalai05e12e12008-09-22 22:58:47 -070072static unsigned __init_or_module vsmp_patch(u8 type, u16 clobbers, void *ibuf,
Glauber Costa96597fd2008-02-11 17:16:04 -020073 unsigned long addr, unsigned len)
74{
75 switch (type) {
76 case PARAVIRT_PATCH(pv_irq_ops.irq_enable):
77 case PARAVIRT_PATCH(pv_irq_ops.irq_disable):
78 case PARAVIRT_PATCH(pv_irq_ops.save_fl):
79 case PARAVIRT_PATCH(pv_irq_ops.restore_fl):
80 return paravirt_patch_default(type, clobbers, ibuf, addr, len);
81 default:
82 return native_patch(type, clobbers, ibuf, addr, len);
83 }
84
85}
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +010086
Ravikiran G Thirumalaiaa7d8e25e2008-03-20 00:41:16 -070087static void __init set_vsmp_pv_ops(void)
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +010088{
Harvey Harrison9352f562008-10-28 23:05:22 -070089 void __iomem *address;
Glauber Costa2785c8d2008-02-11 17:16:03 -020090 unsigned int cap, ctl, cfg;
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +010091
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +010092 /* set vSMP magic bits to indicate vSMP capable kernel */
Glauber Costa2785c8d2008-02-11 17:16:03 -020093 cfg = read_pci_config(0, 0x1f, 0, PCI_BASE_ADDRESS_0);
94 address = early_ioremap(cfg, 8);
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +010095 cap = readl(address);
96 ctl = readl(address + 4);
Thomas Gleixnered4aed92008-01-30 13:30:24 +010097 printk(KERN_INFO "vSMP CTL: capabilities:0x%08x control:0x%08x\n",
98 cap, ctl);
Ravikiran Thirumalai110c1e12012-06-03 01:11:35 +030099
100 /* If possible, let the vSMP foundation route the interrupt optimally */
101#ifdef CONFIG_SMP
102 if (cap & ctl & BIT(8)) {
103 ctl &= ~BIT(8);
104 no_irq_affinity = 1;
105 }
106#endif
107
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +0100108 if (cap & ctl & (1 << 4)) {
Ravikiran G Thirumalai9f6d8552008-03-20 00:43:16 -0700109 /* Setup irq ops and turn on vSMP IRQ fastpath handling */
Jeremy Fitzhardingeecb93d12009-01-28 14:35:05 -0800110 pv_irq_ops.irq_disable = PV_CALLEE_SAVE(vsmp_irq_disable);
111 pv_irq_ops.irq_enable = PV_CALLEE_SAVE(vsmp_irq_enable);
112 pv_irq_ops.save_fl = PV_CALLEE_SAVE(vsmp_save_fl);
113 pv_irq_ops.restore_fl = PV_CALLEE_SAVE(vsmp_restore_fl);
Ravikiran G Thirumalai9f6d8552008-03-20 00:43:16 -0700114 pv_init_ops.patch = vsmp_patch;
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +0100115 ctl &= ~(1 << 4);
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +0100116 }
Ravikiran Thirumalai110c1e12012-06-03 01:11:35 +0300117 writel(ctl, address + 4);
118 ctl = readl(address + 4);
119 pr_info("vSMP CTL: control set to:0x%08x\n", ctl);
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +0100120
Glauber Costa2785c8d2008-02-11 17:16:03 -0200121 early_iounmap(address, 8);
Ravikiran G Thirumalaiaa7d8e25e2008-03-20 00:41:16 -0700122}
123#else
124static void __init set_vsmp_pv_ops(void)
125{
126}
127#endif
128
Ravikiran G Thirumalai70511132009-03-23 23:14:29 -0700129#ifdef CONFIG_PCI
Ravikiran G Thirumalaie5699a82008-03-24 14:48:36 -0700130static int is_vsmp = -1;
131
132static void __init detect_vsmp_box(void)
133{
134 is_vsmp = 0;
135
136 if (!early_pci_allowed())
137 return;
138
139 /* Check if we are running on a ScaleMP vSMPowered box */
140 if (read_pci_config(0, 0x1f, 0, PCI_VENDOR_ID) ==
141 (PCI_VENDOR_ID_SCALEMP | (PCI_DEVICE_ID_SCALEMP_VSMP_CTL << 16)))
142 is_vsmp = 1;
143}
Ravikiran G Thirumalaiaa7d8e25e2008-03-20 00:41:16 -0700144
145int is_vsmp_box(void)
146{
Ravikiran G Thirumalaie5699a82008-03-24 14:48:36 -0700147 if (is_vsmp != -1)
148 return is_vsmp;
149 else {
150 WARN_ON_ONCE(1);
151 return 0;
152 }
Ravikiran G Thirumalaiaa7d8e25e2008-03-20 00:41:16 -0700153}
Ravikiran G Thirumalaiaa7d8e25e2008-03-20 00:41:16 -0700154
Ravikiran G Thirumalai70511132009-03-23 23:14:29 -0700155#else
156static void __init detect_vsmp_box(void)
157{
158}
159int is_vsmp_box(void)
160{
161 return 0;
162}
163#endif
Shai Fultheimead91d42012-04-16 10:39:35 +0300164
165static void __init vsmp_cap_cpus(void)
166{
167#if !defined(CONFIG_X86_VSMP) && defined(CONFIG_SMP)
168 void __iomem *address;
169 unsigned int cfg, topology, node_shift, maxcpus;
170
171 /*
172 * CONFIG_X86_VSMP is not configured, so limit the number CPUs to the
173 * ones present in the first board, unless explicitly overridden by
174 * setup_max_cpus
175 */
176 if (setup_max_cpus != NR_CPUS)
177 return;
178
179 /* Read the vSMP Foundation topology register */
180 cfg = read_pci_config(0, 0x1f, 0, PCI_BASE_ADDRESS_0);
181 address = early_ioremap(cfg + TOPOLOGY_REGISTER_OFFSET, 4);
182 if (WARN_ON(!address))
183 return;
184
185 topology = readl(address);
186 node_shift = (topology >> 16) & 0x7;
187 if (!node_shift)
188 /* The value 0 should be decoded as 8 */
189 node_shift = 8;
190 maxcpus = (topology & ((1 << node_shift) - 1)) + 1;
191
192 pr_info("vSMP CTL: Capping CPUs to %d (CONFIG_X86_VSMP is unset)\n",
193 maxcpus);
194 setup_max_cpus = maxcpus;
195 early_iounmap(address, 4);
196#endif
197}
198
Ido Yariv7db971b2012-06-03 01:11:34 +0300199static int apicid_phys_pkg_id(int initial_apic_id, int index_msb)
200{
201 return hard_smp_processor_id() >> index_msb;
202}
203
Ravikiran Thirumalai110c1e12012-06-03 01:11:35 +0300204/*
205 * In vSMP, all cpus should be capable of handling interrupts, regardless of
206 * the APIC used.
207 */
208static void fill_vector_allocation_domain(int cpu, struct cpumask *retmask)
209{
210 cpumask_setall(retmask);
211}
212
Ido Yariv7db971b2012-06-03 01:11:34 +0300213static void vsmp_apic_post_init(void)
214{
215 /* need to update phys_pkg_id */
216 apic->phys_pkg_id = apicid_phys_pkg_id;
Ravikiran Thirumalai110c1e12012-06-03 01:11:35 +0300217 apic->vector_allocation_domain = fill_vector_allocation_domain;
Ido Yariv7db971b2012-06-03 01:11:34 +0300218}
219
Ravikiran G Thirumalaiaa7d8e25e2008-03-20 00:41:16 -0700220void __init vsmp_init(void)
221{
Ravikiran G Thirumalaie5699a82008-03-24 14:48:36 -0700222 detect_vsmp_box();
Ravikiran G Thirumalaiaa7d8e25e2008-03-20 00:41:16 -0700223 if (!is_vsmp_box())
224 return;
225
Ido Yariv7db971b2012-06-03 01:11:34 +0300226 x86_platform.apic_post_init = vsmp_apic_post_init;
227
Shai Fultheimead91d42012-04-16 10:39:35 +0300228 vsmp_cap_cpus();
229
Ravikiran G Thirumalaiaa7d8e25e2008-03-20 00:41:16 -0700230 set_vsmp_pv_ops();
Glauber Costaa2beab32008-02-11 17:16:02 -0200231 return;
Ravikiran G Thirumalai79f12612006-01-11 22:46:18 +0100232}