]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/summit_32.c
x86, summit: consolidate code, fix
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / summit_32.c
1 /*
2  * IBM Summit-Specific Code
3  *
4  * Written By: Matthew Dobson, IBM Corporation
5  *
6  * Copyright (c) 2003 IBM Corp.
7  *
8  * All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or (at
13  * your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18  * NON INFRINGEMENT.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * Send feedback to <colpatch@us.ibm.com>
26  *
27  */
28
29 #include <linux/mm.h>
30 #include <linux/init.h>
31 #include <asm/io.h>
32 #include <asm/bios_ebda.h>
33
34 /*
35  * APIC driver for the IBM "Summit" chipset.
36  */
37 #define APIC_DEFINITION 1
38 #include <linux/threads.h>
39 #include <linux/cpumask.h>
40 #include <asm/mpspec.h>
41 #include <asm/apic.h>
42 #include <asm/smp.h>
43 #include <asm/genapic.h>
44 #include <asm/fixmap.h>
45 #include <asm/apicdef.h>
46 #include <linux/kernel.h>
47 #include <linux/string.h>
48 #include <linux/init.h>
49 #include <linux/gfp.h>
50 #include <linux/smp.h>
51
52 static inline unsigned summit_get_apic_id(unsigned long x)
53 {
54         return (x >> 24) & 0xFF;
55 }
56
57 void default_send_IPI_mask_sequence(const cpumask_t *mask, int vector);
58 void default_send_IPI_mask_allbutself(const cpumask_t *mask, int vector);
59
60 static inline void summit_send_IPI_mask(const cpumask_t *mask, int vector)
61 {
62         default_send_IPI_mask_sequence(mask, vector);
63 }
64
65 static inline void summit_send_IPI_allbutself(int vector)
66 {
67         cpumask_t mask = cpu_online_map;
68         cpu_clear(smp_processor_id(), mask);
69
70         if (!cpus_empty(mask))
71                 summit_send_IPI_mask(&mask, vector);
72 }
73
74 static inline void summit_send_IPI_all(int vector)
75 {
76         summit_send_IPI_mask(&cpu_online_map, vector);
77 }
78
79 #include <asm/tsc.h>
80
81 extern int use_cyclone;
82
83 #ifdef CONFIG_X86_SUMMIT_NUMA
84 extern void setup_summit(void);
85 #else
86 #define setup_summit()  {}
87 #endif
88
89 static inline int
90 summit_mps_oem_check(struct mpc_table *mpc, char *oem, char *productid)
91 {
92         if (!strncmp(oem, "IBM ENSW", 8) &&
93                         (!strncmp(productid, "VIGIL SMP", 9)
94                          || !strncmp(productid, "EXA", 3)
95                          || !strncmp(productid, "RUTHLESS SMP", 12))){
96                 mark_tsc_unstable("Summit based system");
97                 use_cyclone = 1; /*enable cyclone-timer*/
98                 setup_summit();
99                 return 1;
100         }
101         return 0;
102 }
103
104 /* Hook from generic ACPI tables.c */
105 static inline int summit_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
106 {
107         if (!strncmp(oem_id, "IBM", 3) &&
108             (!strncmp(oem_table_id, "SERVIGIL", 8)
109              || !strncmp(oem_table_id, "EXA", 3))){
110                 mark_tsc_unstable("Summit based system");
111                 use_cyclone = 1; /*enable cyclone-timer*/
112                 setup_summit();
113                 return 1;
114         }
115         return 0;
116 }
117
118 struct rio_table_hdr {
119         unsigned char version;      /* Version number of this data structure           */
120                                     /* Version 3 adds chassis_num & WP_index           */
121         unsigned char num_scal_dev; /* # of Scalability devices (Twisters for Vigil)   */
122         unsigned char num_rio_dev;  /* # of RIO I/O devices (Cyclones and Winnipegs)   */
123 } __attribute__((packed));
124
125 struct scal_detail {
126         unsigned char node_id;      /* Scalability Node ID                             */
127         unsigned long CBAR;         /* Address of 1MB register space                   */
128         unsigned char port0node;    /* Node ID port connected to: 0xFF=None            */
129         unsigned char port0port;    /* Port num port connected to: 0,1,2, or 0xFF=None */
130         unsigned char port1node;    /* Node ID port connected to: 0xFF = None          */
131         unsigned char port1port;    /* Port num port connected to: 0,1,2, or 0xFF=None */
132         unsigned char port2node;    /* Node ID port connected to: 0xFF = None          */
133         unsigned char port2port;    /* Port num port connected to: 0,1,2, or 0xFF=None */
134         unsigned char chassis_num;  /* 1 based Chassis number (1 = boot node)          */
135 } __attribute__((packed));
136
137 struct rio_detail {
138         unsigned char node_id;      /* RIO Node ID                                     */
139         unsigned long BBAR;         /* Address of 1MB register space                   */
140         unsigned char type;         /* Type of device                                  */
141         unsigned char owner_id;     /* For WPEG: Node ID of Cyclone that owns this WPEG*/
142                                     /* For CYC:  Node ID of Twister that owns this CYC */
143         unsigned char port0node;    /* Node ID port connected to: 0xFF=None            */
144         unsigned char port0port;    /* Port num port connected to: 0,1,2, or 0xFF=None */
145         unsigned char port1node;    /* Node ID port connected to: 0xFF=None            */
146         unsigned char port1port;    /* Port num port connected to: 0,1,2, or 0xFF=None */
147         unsigned char first_slot;   /* For WPEG: Lowest slot number below this WPEG    */
148                                     /* For CYC:  0                                     */
149         unsigned char status;       /* For WPEG: Bit 0 = 1 : the XAPIC is used         */
150                                     /*                 = 0 : the XAPIC is not used, ie:*/
151                                     /*                     ints fwded to another XAPIC */
152                                     /*           Bits1:7 Reserved                      */
153                                     /* For CYC:  Bits0:7 Reserved                      */
154         unsigned char WP_index;     /* For WPEG: WPEG instance index - lower ones have */
155                                     /*           lower slot numbers/PCI bus numbers    */
156                                     /* For CYC:  No meaning                            */
157         unsigned char chassis_num;  /* 1 based Chassis number                          */
158                                     /* For LookOut WPEGs this field indicates the      */
159                                     /* Expansion Chassis #, enumerated from Boot       */
160                                     /* Node WPEG external port, then Boot Node CYC     */
161                                     /* external port, then Next Vigil chassis WPEG     */
162                                     /* external port, etc.                             */
163                                     /* Shared Lookouts have only 1 chassis number (the */
164                                     /* first one assigned)                             */
165 } __attribute__((packed));
166
167
168 typedef enum {
169         CompatTwister = 0,  /* Compatibility Twister               */
170         AltTwister    = 1,  /* Alternate Twister of internal 8-way */
171         CompatCyclone = 2,  /* Compatibility Cyclone               */
172         AltCyclone    = 3,  /* Alternate Cyclone of internal 8-way */
173         CompatWPEG    = 4,  /* Compatibility WPEG                  */
174         AltWPEG       = 5,  /* Second Planar WPEG                  */
175         LookOutAWPEG  = 6,  /* LookOut WPEG                        */
176         LookOutBWPEG  = 7,  /* LookOut WPEG                        */
177 } node_type;
178
179 static inline int is_WPEG(struct rio_detail *rio){
180         return (rio->type == CompatWPEG || rio->type == AltWPEG ||
181                 rio->type == LookOutAWPEG || rio->type == LookOutBWPEG);
182 }
183
184
185 /* In clustered mode, the high nibble of APIC ID is a cluster number.
186  * The low nibble is a 4-bit bitmap. */
187 #define XAPIC_DEST_CPUS_SHIFT   4
188 #define XAPIC_DEST_CPUS_MASK    ((1u << XAPIC_DEST_CPUS_SHIFT) - 1)
189 #define XAPIC_DEST_CLUSTER_MASK (XAPIC_DEST_CPUS_MASK << XAPIC_DEST_CPUS_SHIFT)
190
191 #define SUMMIT_APIC_DFR_VALUE   (APIC_DFR_CLUSTER)
192
193 static inline const cpumask_t *summit_target_cpus(void)
194 {
195         /* CPU_MASK_ALL (0xff) has undefined behaviour with
196          * dest_LowestPrio mode logical clustered apic interrupt routing
197          * Just start on cpu 0.  IRQ balancing will spread load
198          */
199         return &cpumask_of_cpu(0);
200 }
201
202 static inline unsigned long
203 summit_check_apicid_used(physid_mask_t bitmap, int apicid)
204 {
205         return 0;
206 }
207
208 /* we don't use the phys_cpu_present_map to indicate apicid presence */
209 static inline unsigned long summit_check_apicid_present(int bit)
210 {
211         return 1;
212 }
213
214 #define apicid_cluster(apicid) ((apicid) & XAPIC_DEST_CLUSTER_MASK)
215
216 extern u8 cpu_2_logical_apicid[];
217
218 static inline void summit_init_apic_ldr(void)
219 {
220         unsigned long val, id;
221         int count = 0;
222         u8 my_id = (u8)hard_smp_processor_id();
223         u8 my_cluster = (u8)apicid_cluster(my_id);
224 #ifdef CONFIG_SMP
225         u8 lid;
226         int i;
227
228         /* Create logical APIC IDs by counting CPUs already in cluster. */
229         for (count = 0, i = nr_cpu_ids; --i >= 0; ) {
230                 lid = cpu_2_logical_apicid[i];
231                 if (lid != BAD_APICID && apicid_cluster(lid) == my_cluster)
232                         ++count;
233         }
234 #endif
235         /* We only have a 4 wide bitmap in cluster mode.  If a deranged
236          * BIOS puts 5 CPUs in one APIC cluster, we're hosed. */
237         BUG_ON(count >= XAPIC_DEST_CPUS_SHIFT);
238         id = my_cluster | (1UL << count);
239         apic_write(APIC_DFR, SUMMIT_APIC_DFR_VALUE);
240         val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
241         val |= SET_APIC_LOGICAL_ID(id);
242         apic_write(APIC_LDR, val);
243 }
244
245 static inline int summit_apic_id_registered(void)
246 {
247         return 1;
248 }
249
250 static inline void summit_setup_apic_routing(void)
251 {
252         printk("Enabling APIC mode:  Summit.  Using %d I/O APICs\n",
253                                                 nr_ioapics);
254 }
255
256 static inline int summit_apicid_to_node(int logical_apicid)
257 {
258 #ifdef CONFIG_SMP
259         return apicid_2_node[hard_smp_processor_id()];
260 #else
261         return 0;
262 #endif
263 }
264
265 /* Mapping from cpu number to logical apicid */
266 static inline int summit_cpu_to_logical_apicid(int cpu)
267 {
268 #ifdef CONFIG_SMP
269         if (cpu >= nr_cpu_ids)
270                 return BAD_APICID;
271         return (int)cpu_2_logical_apicid[cpu];
272 #else
273         return logical_smp_processor_id();
274 #endif
275 }
276
277 static inline int summit_cpu_present_to_apicid(int mps_cpu)
278 {
279         if (mps_cpu < nr_cpu_ids)
280                 return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu);
281         else
282                 return BAD_APICID;
283 }
284
285 static inline physid_mask_t
286 summit_ioapic_phys_id_map(physid_mask_t phys_id_map)
287 {
288         /* For clustered we don't have a good way to do this yet - hack */
289         return physids_promote(0x0F);
290 }
291
292 static inline physid_mask_t summit_apicid_to_cpu_present(int apicid)
293 {
294         return physid_mask_of_physid(0);
295 }
296
297 static inline void summit_setup_portio_remap(void)
298 {
299 }
300
301 static inline int summit_check_phys_apicid_present(int boot_cpu_physical_apicid)
302 {
303         return 1;
304 }
305
306 static inline unsigned int summit_cpu_mask_to_apicid(const cpumask_t *cpumask)
307 {
308         int cpus_found = 0;
309         int num_bits_set;
310         int apicid;
311         int cpu;
312
313         num_bits_set = cpus_weight(*cpumask);
314         /* Return id to all */
315         if (num_bits_set >= nr_cpu_ids)
316                 return 0xFF;
317         /*
318          * The cpus in the mask must all be on the apic cluster.  If are not
319          * on the same apicid cluster return default value of target_cpus():
320          */
321         cpu = first_cpu(*cpumask);
322         apicid = summit_cpu_to_logical_apicid(cpu);
323
324         while (cpus_found < num_bits_set) {
325                 if (cpu_isset(cpu, *cpumask)) {
326                         int new_apicid = summit_cpu_to_logical_apicid(cpu);
327
328                         if (apicid_cluster(apicid) !=
329                                         apicid_cluster(new_apicid)) {
330                                 printk ("%s: Not a valid mask!\n", __func__);
331
332                                 return 0xFF;
333                         }
334                         apicid = apicid | new_apicid;
335                         cpus_found++;
336                 }
337                 cpu++;
338         }
339         return apicid;
340 }
341
342 static inline unsigned int
343 summit_cpu_mask_to_apicid_and(const struct cpumask *inmask,
344                               const struct cpumask *andmask)
345 {
346         int apicid = summit_cpu_to_logical_apicid(0);
347         cpumask_var_t cpumask;
348
349         if (!alloc_cpumask_var(&cpumask, GFP_ATOMIC))
350                 return apicid;
351
352         cpumask_and(cpumask, inmask, andmask);
353         cpumask_and(cpumask, cpumask, cpu_online_mask);
354         apicid = summit_cpu_mask_to_apicid(cpumask);
355
356         free_cpumask_var(cpumask);
357
358         return apicid;
359 }
360
361 /*
362  * cpuid returns the value latched in the HW at reset, not the APIC ID
363  * register's value.  For any box whose BIOS changes APIC IDs, like
364  * clustered APIC systems, we must use hard_smp_processor_id.
365  *
366  * See Intel's IA-32 SW Dev's Manual Vol2 under CPUID.
367  */
368 static inline int summit_phys_pkg_id(int cpuid_apic, int index_msb)
369 {
370         return hard_smp_processor_id() >> index_msb;
371 }
372
373 static int probe_summit(void)
374 {
375         /* probed later in mptable/ACPI hooks */
376         return 0;
377 }
378
379 static void summit_vector_allocation_domain(int cpu, cpumask_t *retmask)
380 {
381         /* Careful. Some cpus do not strictly honor the set of cpus
382          * specified in the interrupt destination when using lowest
383          * priority interrupt delivery mode.
384          *
385          * In particular there was a hyperthreading cpu observed to
386          * deliver interrupts to the wrong hyperthread when only one
387          * hyperthread was specified in the interrupt desitination.
388          */
389         *retmask = (cpumask_t){ { [0] = APIC_ALL_CPUS, } };
390 }
391
392 #ifdef CONFIG_X86_SUMMIT_NUMA
393 static struct rio_table_hdr *rio_table_hdr __initdata;
394 static struct scal_detail   *scal_devs[MAX_NUMNODES] __initdata;
395 static struct rio_detail    *rio_devs[MAX_NUMNODES*4] __initdata;
396
397 #ifndef CONFIG_X86_NUMAQ
398 static int mp_bus_id_to_node[MAX_MP_BUSSES] __initdata;
399 #endif
400
401 static int __init setup_pci_node_map_for_wpeg(int wpeg_num, int last_bus)
402 {
403         int twister = 0, node = 0;
404         int i, bus, num_buses;
405
406         for (i = 0; i < rio_table_hdr->num_rio_dev; i++) {
407                 if (rio_devs[i]->node_id == rio_devs[wpeg_num]->owner_id) {
408                         twister = rio_devs[i]->owner_id;
409                         break;
410                 }
411         }
412         if (i == rio_table_hdr->num_rio_dev) {
413                 printk(KERN_ERR "%s: Couldn't find owner Cyclone for Winnipeg!\n", __func__);
414                 return last_bus;
415         }
416
417         for (i = 0; i < rio_table_hdr->num_scal_dev; i++) {
418                 if (scal_devs[i]->node_id == twister) {
419                         node = scal_devs[i]->node_id;
420                         break;
421                 }
422         }
423         if (i == rio_table_hdr->num_scal_dev) {
424                 printk(KERN_ERR "%s: Couldn't find owner Twister for Cyclone!\n", __func__);
425                 return last_bus;
426         }
427
428         switch (rio_devs[wpeg_num]->type) {
429         case CompatWPEG:
430                 /*
431                  * The Compatibility Winnipeg controls the 2 legacy buses,
432                  * the 66MHz PCI bus [2 slots] and the 2 "extra" buses in case
433                  * a PCI-PCI bridge card is used in either slot: total 5 buses.
434                  */
435                 num_buses = 5;
436                 break;
437         case AltWPEG:
438                 /*
439                  * The Alternate Winnipeg controls the 2 133MHz buses [1 slot
440                  * each], their 2 "extra" buses, the 100MHz bus [2 slots] and
441                  * the "extra" buses for each of those slots: total 7 buses.
442                  */
443                 num_buses = 7;
444                 break;
445         case LookOutAWPEG:
446         case LookOutBWPEG:
447                 /*
448                  * A Lookout Winnipeg controls 3 100MHz buses [2 slots each]
449                  * & the "extra" buses for each of those slots: total 9 buses.
450                  */
451                 num_buses = 9;
452                 break;
453         default:
454                 printk(KERN_INFO "%s: Unsupported Winnipeg type!\n", __func__);
455                 return last_bus;
456         }
457
458         for (bus = last_bus; bus < last_bus + num_buses; bus++)
459                 mp_bus_id_to_node[bus] = node;
460         return bus;
461 }
462
463 static int __init build_detail_arrays(void)
464 {
465         unsigned long ptr;
466         int i, scal_detail_size, rio_detail_size;
467
468         if (rio_table_hdr->num_scal_dev > MAX_NUMNODES) {
469                 printk(KERN_WARNING "%s: MAX_NUMNODES too low!  Defined as %d, but system has %d nodes.\n", __func__, MAX_NUMNODES, rio_table_hdr->num_scal_dev);
470                 return 0;
471         }
472
473         switch (rio_table_hdr->version) {
474         default:
475                 printk(KERN_WARNING "%s: Invalid Rio Grande Table Version: %d\n", __func__, rio_table_hdr->version);
476                 return 0;
477         case 2:
478                 scal_detail_size = 11;
479                 rio_detail_size = 13;
480                 break;
481         case 3:
482                 scal_detail_size = 12;
483                 rio_detail_size = 15;
484                 break;
485         }
486
487         ptr = (unsigned long)rio_table_hdr + 3;
488         for (i = 0; i < rio_table_hdr->num_scal_dev; i++, ptr += scal_detail_size)
489                 scal_devs[i] = (struct scal_detail *)ptr;
490
491         for (i = 0; i < rio_table_hdr->num_rio_dev; i++, ptr += rio_detail_size)
492                 rio_devs[i] = (struct rio_detail *)ptr;
493
494         return 1;
495 }
496
497 void __init setup_summit(void)
498 {
499         unsigned long           ptr;
500         unsigned short          offset;
501         int                     i, next_wpeg, next_bus = 0;
502
503         /* The pointer to the EBDA is stored in the word @ phys 0x40E(40:0E) */
504         ptr = get_bios_ebda();
505         ptr = (unsigned long)phys_to_virt(ptr);
506
507         rio_table_hdr = NULL;
508         offset = 0x180;
509         while (offset) {
510                 /* The block id is stored in the 2nd word */
511                 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752) {
512                         /* set the pointer past the offset & block id */
513                         rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
514                         break;
515                 }
516                 /* The next offset is stored in the 1st word.  0 means no more */
517                 offset = *((unsigned short *)(ptr + offset));
518         }
519         if (!rio_table_hdr) {
520                 printk(KERN_ERR "%s: Unable to locate Rio Grande Table in EBDA - bailing!\n", __func__);
521                 return;
522         }
523
524         if (!build_detail_arrays())
525                 return;
526
527         /* The first Winnipeg we're looking for has an index of 0 */
528         next_wpeg = 0;
529         do {
530                 for (i = 0; i < rio_table_hdr->num_rio_dev; i++) {
531                         if (is_WPEG(rio_devs[i]) && rio_devs[i]->WP_index == next_wpeg) {
532                                 /* It's the Winnipeg we're looking for! */
533                                 next_bus = setup_pci_node_map_for_wpeg(i, next_bus);
534                                 next_wpeg++;
535                                 break;
536                         }
537                 }
538                 /*
539                  * If we go through all Rio devices and don't find one with
540                  * the next index, it means we've found all the Winnipegs,
541                  * and thus all the PCI buses.
542                  */
543                 if (i == rio_table_hdr->num_rio_dev)
544                         next_wpeg = 0;
545         } while (next_wpeg != 0);
546 }
547 #endif
548
549 struct genapic apic_summit = {
550
551         .name                           = "summit",
552         .probe                          = probe_summit,
553         .acpi_madt_oem_check            = summit_acpi_madt_oem_check,
554         .apic_id_registered             = summit_apic_id_registered,
555
556         .irq_delivery_mode              = dest_LowestPrio,
557         /* logical delivery broadcast to all CPUs: */
558         .irq_dest_mode                  = 1,
559
560         .target_cpus                    = summit_target_cpus,
561         .disable_esr                    = 1,
562         .dest_logical                   = APIC_DEST_LOGICAL,
563         .check_apicid_used              = summit_check_apicid_used,
564         .check_apicid_present           = summit_check_apicid_present,
565
566         .vector_allocation_domain       = summit_vector_allocation_domain,
567         .init_apic_ldr                  = summit_init_apic_ldr,
568
569         .ioapic_phys_id_map             = summit_ioapic_phys_id_map,
570         .setup_apic_routing             = summit_setup_apic_routing,
571         .multi_timer_check              = NULL,
572         .apicid_to_node                 = summit_apicid_to_node,
573         .cpu_to_logical_apicid          = summit_cpu_to_logical_apicid,
574         .cpu_present_to_apicid          = summit_cpu_present_to_apicid,
575         .apicid_to_cpu_present          = summit_apicid_to_cpu_present,
576         .setup_portio_remap             = NULL,
577         .check_phys_apicid_present      = summit_check_phys_apicid_present,
578         .enable_apic_mode               = NULL,
579         .phys_pkg_id                    = summit_phys_pkg_id,
580         .mps_oem_check                  = summit_mps_oem_check,
581
582         .get_apic_id                    = summit_get_apic_id,
583         .set_apic_id                    = NULL,
584         .apic_id_mask                   = 0xFF << 24,
585
586         .cpu_mask_to_apicid             = summit_cpu_mask_to_apicid,
587         .cpu_mask_to_apicid_and         = summit_cpu_mask_to_apicid_and,
588
589         .send_IPI_mask                  = summit_send_IPI_mask,
590         .send_IPI_mask_allbutself       = NULL,
591         .send_IPI_allbutself            = summit_send_IPI_allbutself,
592         .send_IPI_all                   = summit_send_IPI_all,
593         .send_IPI_self                  = NULL,
594
595         .wakeup_cpu                     = NULL,
596         .trampoline_phys_low            = DEFAULT_TRAMPOLINE_PHYS_LOW,
597         .trampoline_phys_high           = DEFAULT_TRAMPOLINE_PHYS_HIGH,
598
599         .wait_for_init_deassert         = default_wait_for_init_deassert,
600
601         .smp_callin_clear_local_apic    = NULL,
602         .store_NMI_vector               = NULL,
603         .inquire_remote_apic            = default_inquire_remote_apic,
604 };