source: rtems/bsps/i386/pc386/start/smp-imps.c @ f99b1f0

5
Last change on this file since f99b1f0 was f99b1f0, checked in by Jan Sommer <jan.sommer@…>, on 05/31/20 at 14:22:52

bsp/pc386: Turn start16.S into a startAP.S

start16.S is now only used for SMP configurations to start the
application processors.

This commit removes all unnecessary parts for this job,
i.e. video conssole initalisation, A20 gate activation
and all non-AP related code.

Update #3335

  • Property mode set to 100644
File size: 22.1 KB
Line 
1/*
2 * Author: Erich Boleyn  <erich@uruk.org>
3 *         http://www.uruk.org/~erich/
4 *
5 * Copyright (c) 1997-2011 Erich Boleyn.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 *  Source file implementing Intel MultiProcessor Specification (MPS)
32 *  version 1.1 and 1.4 SMP hardware control for Intel Architecture CPUs,
33 *  with hooks for running correctly on a standard PC without the hardware.
34 *
35 *  This file was created from information in the Intel MPS version 1.4
36 *  document, order number 242016-004, which can be ordered from the
37 *  Intel literature center.
38 *
39 *  General limitations of this code:
40 *
41 *   (1) : This code has never been tested on an MPS-compatible system with
42 *           486 CPUs, but is expected to work.
43 *   (2) : Presumes "int", "long", and "unsigned" are 32 bits in size, and
44 *       that 32-bit pointers and memory addressing is used uniformly.
45 */
46
47#define _SMP_IMPS_C
48
49/*
50 *  Includes here
51 */
52#if 0
53#define IMPS_DEBUG
54#endif
55
56#include <bsp/apic.h>
57#include <bsp/smp-imps.h>
58#include <bsp/irq.h>
59#include <rtems/score/smpimpl.h>
60
61/*
62 *  XXXXX  The following absolutely must be defined!!!
63 *
64 *  The "KERNEL_PRINT" could be made a null macro with no danger, of
65 *  course, but pretty much nothing would work without the other
66 *  ones defined.
67 */
68
69#if 0
70#define KERNEL_PRINT(x)       /* some kind of print function */
71#define CMOS_WRITE_BYTE(x,y)  /* write unsigned char "y" at CMOS loc "x" */
72#define CMOS_READ_BYTE(x)     /* read unsigned char at CMOS loc "x" */
73#define PHYS_TO_VIRTUAL(x)    /* convert physical address "x" to virtual */
74#define VIRTUAL_TO_PHYS(x)    /* convert virtual address "x" to physical */
75#define UDELAY(x)             /* delay roughly at least "x" microsecs */
76#define READ_MSR_LO(x)        /* Read MSR low function */
77#else
78#include <string.h>
79#include <unistd.h>
80#include <rtems.h>
81#include <rtems/bspIo.h>
82#include <rtems/score/cpu.h>
83#include <assert.h>
84
85extern void _pc386_delay(void);
86
87/* #define KERNEL_PRINT(_format)       printk(_format) */
88
89static void CMOS_WRITE_BYTE(
90  unsigned int  offset,
91  unsigned char value
92)
93{
94  if ( offset < 128 ) {
95    outport_byte( 0x70, offset );
96    outport_byte( 0x71, value );
97  } else {
98    outport_byte( 0x72, offset );
99    outport_byte( 0x73, value );
100  }
101}
102
103static unsigned char CMOS_READ_BYTE(
104  unsigned int  offset
105)
106{
107  unsigned char value;
108  if ( offset < 128 ) {
109    outport_byte( 0x70, offset );
110    inport_byte( 0x71, value );
111  } else {
112    outport_byte( 0x72, offset );
113    inport_byte( 0x73, value );
114  }
115  return value;
116}
117
118#define PHYS_TO_VIRTUAL(_x)    _x
119#define VIRTUAL_TO_PHYS(_x)    _x
120static void UDELAY(int x)
121{ int _i = x;
122  while ( _i-- )
123    _pc386_delay();
124}
125
126#define READ_MSR_LO(_x) \
127  (unsigned int)(read_msr(_x) & 0xffffffff)
128
129static inline unsigned long long read_msr(unsigned int msr)
130{
131  unsigned long long value;
132
133  asm volatile("rdmsr" : "=A" (value) : "c" (msr));
134  return value;
135}
136#endif
137
138/*
139 *  Defines that are here so as not to be in the global header file.
140 */
141#define EBDA_SEG_ADDR       0x40E
142#define BIOS_RESET_VECTOR   0x467
143#define LAPIC_ADDR_DEFAULT  0xFEE00000uL
144#define IOAPIC_ADDR_DEFAULT 0xFEC00000uL
145#define CMOS_RESET_CODE     0xF
146#define CMOS_RESET_JUMP     0xa
147#define CMOS_BASE_MEMORY    0x15
148
149/*
150 *  Static defines here for SMP use.
151 */
152
153#define DEF_ENTRIES  23
154
155static struct {
156  imps_processor proc[2];
157  imps_bus bus[2];
158  imps_ioapic ioapic;
159  imps_interrupt intin[16];
160  imps_interrupt lintin[2];
161} defconfig = {
162  { { IMPS_BCT_PROCESSOR, 0, 0, 0, 0, 0},
163    { IMPS_BCT_PROCESSOR, 1, 0, 0, 0, 0} },
164  { { IMPS_BCT_BUS, 0, {'E', 'I', 'S', 'A', ' ', ' '}},
165    { 255, 1, {'P', 'C', 'I', ' ', ' ', ' '}} },
166  { IMPS_BCT_IOAPIC, 0, 0, IMPS_FLAG_ENABLED, IOAPIC_ADDR_DEFAULT },
167  { { IMPS_BCT_IO_INTERRUPT, IMPS_INT_EXTINT, 0, 0, 0, 0xFF, 0},
168    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 1, 0xFF, 1},
169    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 0, 0xFF, 2},
170    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 3, 0xFF, 3},
171    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 4, 0xFF, 4},
172    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 5, 0xFF, 5},
173    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 6, 0xFF, 6},
174    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 7, 0xFF, 7},
175    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 8, 0xFF, 8},
176    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 9, 0xFF, 9},
177    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 10, 0xFF, 10},
178    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 11, 0xFF, 11},
179    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 12, 0xFF, 12},
180    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 13, 0xFF, 13},
181    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 14, 0xFF, 14},
182    { IMPS_BCT_IO_INTERRUPT, IMPS_INT_INT, 0, 0, 15, 0xFF, 15} },
183  { { IMPS_BCT_LOCAL_INTERRUPT, IMPS_INT_EXTINT, 0, 0, 15, 0xFF, 0},
184    { IMPS_BCT_LOCAL_INTERRUPT, IMPS_INT_NMI, 0, 0, 15, 0xFF, 1} }
185};
186
187/*
188 *  Exported globals here.
189 */
190
191volatile int imps_release_cpus = 0;
192int imps_enabled = 0;
193int imps_num_cpus = 1;
194unsigned char imps_cpu_apic_map[IMPS_MAX_CPUS];
195unsigned char imps_apic_cpu_map[IMPS_MAX_CPUS];
196
197/* now defined in getcpuid.c */
198extern unsigned imps_lapic_addr;
199
200static void secondary_cpu_initialize(void);
201
202/*
203 *  MPS checksum function
204 *
205 *  Function finished.
206 */
207static int
208get_checksum(unsigned start, int length)
209{
210  unsigned sum = 0;
211
212  while (length-- > 0) {
213    sum += *((unsigned char *) (start++));
214  }
215
216  return (sum&0xFF);
217}
218
219/*
220 *  APIC ICR write and status check function.
221 */
222static int
223send_ipi(unsigned int dst, unsigned int v)
224{
225  int to, send_status;
226
227  IMPS_LAPIC_WRITE(LAPIC_ICR+0x10, (dst << 24));
228  IMPS_LAPIC_WRITE(LAPIC_ICR, v);
229
230  /* Wait for send to finish */
231  to = 0;
232  do {
233    UDELAY(100);
234    send_status = IMPS_LAPIC_READ(LAPIC_ICR) & LAPIC_ICR_STATUS_PEND;
235  } while (send_status && (to++ < 1000));
236
237  return (to < 1000);
238}
239
240/*
241 *  Primary function for booting individual CPUs.
242 *
243 *  This must be modified to perform whatever OS-specific initialization
244 *  that is required.
245 */
246static int
247boot_cpu(imps_processor *proc)
248{
249  int apicid = proc->apic_id, success = 1;
250  unsigned bootaddr;
251  unsigned bios_reset_vector = PHYS_TO_VIRTUAL(BIOS_RESET_VECTOR);
252
253  /*
254   * Copy boot code for secondary CPUs here.  Find it in between
255   * "patch_code_start" and "patch_code_end" symbols.  The other CPUs
256   * will start there in 16-bit real mode under the 1MB boundary.
257   * "patch_code_start" should be placed at a 4K-aligned address
258   * under the 1MB boundary.
259   */
260
261  uint32_t *reset;
262
263  bootaddr = (512-64)*1024;
264  reset= (uint32_t *)bootaddr;
265
266  memcpy(
267    (char *) bootaddr,
268    _binary_appstart_bin_start,
269    (size_t)_binary_appstart_bin_size
270  );
271
272  reset[1] = (uint32_t)secondary_cpu_initialize;
273  reset[2] = (uint32_t)_Per_CPU_Get_by_index(apicid)->interrupt_stack_high;
274
275  /*
276   *  Generic CPU startup sequence starts here.
277   */
278
279  /* set BIOS reset vector */
280  CMOS_WRITE_BYTE(CMOS_RESET_CODE, CMOS_RESET_JUMP);
281  *((volatile unsigned *) bios_reset_vector) = ((bootaddr & 0xFF000) << 12);
282
283  /* clear the APIC error register */
284  IMPS_LAPIC_WRITE(LAPIC_ESR, 0);
285  IMPS_LAPIC_READ(LAPIC_ESR);
286
287  /* assert INIT IPI */
288  send_ipi(
289    apicid,
290    LAPIC_ICR_TM_LEVEL | LAPIC_ICR_LEVELASSERT | LAPIC_ICR_DM_INIT
291  );
292  UDELAY(10000);
293
294  /* de-assert INIT IPI */
295  send_ipi(apicid, LAPIC_ICR_TM_LEVEL | LAPIC_ICR_DM_INIT);
296
297  UDELAY(10000);
298
299  /*
300   *  Send Startup IPIs if not an old pre-integrated APIC.
301   */
302
303  if (proc->apic_ver >= APIC_VER_NEW) {
304    int i;
305    for (i = 1; i <= 2; i++) {
306      send_ipi(apicid, LAPIC_ICR_DM_SIPI | ((bootaddr >> 12) & 0xFF));
307      UDELAY(1000);
308    }
309  }
310
311  /*
312   *  Wait until AP is in protected mode before starting the next AP
313   */
314  while (reset[2] != 0);
315
316  /*
317   *  Generic CPU startup sequence ends here, the rest is cleanup.
318   */
319
320  /* clear the APIC error register */
321  IMPS_LAPIC_WRITE(LAPIC_ESR, 0);
322  IMPS_LAPIC_READ(LAPIC_ESR);
323
324  /* clean up BIOS reset vector */
325  CMOS_WRITE_BYTE(CMOS_RESET_CODE, 0);
326  *((volatile unsigned *) bios_reset_vector) = 0;
327
328  printk("\n");
329
330  return success;
331}
332
333/*
334 *  read bios stuff and fill tables
335 */
336static void
337add_processor(imps_processor *proc)
338{
339  int apicid = proc->apic_id;
340
341  printk("  Processor [APIC id %d ver %d]: ", apicid, proc->apic_ver);
342  if (!(proc->flags & IMPS_FLAG_ENABLED)) {
343    printk("DISABLED\n");
344    return;
345  }
346  if (proc->flags & (IMPS_CPUFLAG_BOOT)) {
347    printk("#0  BootStrap Processor (BSP)\n");
348    return;
349  }
350  /* Setup the apic/cpu maps before booting the APs
351   * otherwise calls to _Get_current_processor can deliver
352   * wrong values if the BSP gets interrupted
353   */
354  imps_cpu_apic_map[imps_num_cpus] = apicid;
355  imps_apic_cpu_map[apicid] = imps_num_cpus;
356  if (boot_cpu(proc)) {
357
358    /*  XXXXX  add OS-specific setup for secondary CPUs here */
359
360    /* AP booted successfully, increase number of available cores */
361    imps_num_cpus++;
362  }
363}
364
365
366static void
367add_bus(imps_bus *bus)
368{
369  char str[8];
370
371  memcpy(str, bus->bus_type, 6);
372  str[6] = 0;
373  printk("  Bus id %d is %s\n", bus->id, str);
374
375  /*  XXXXX  add OS-specific code here */
376}
377
378static void
379add_ioapic(imps_ioapic *ioapic)
380{
381  printk("  I/O APIC id %d ver %d, address: 0x%x  ",
382          ioapic->id, ioapic->ver, ioapic->addr);
383  if (!(ioapic->flags & IMPS_FLAG_ENABLED)) {
384    printk("DISABLED\n");
385    return;
386  }
387  printk("\n");
388
389  /*  XXXXX  add OS-specific code here */
390}
391
392static void
393imps_read_config_table(unsigned start, int count)
394{
395  while (count-- > 0) {
396    switch (*((unsigned char *)start)) {
397    case IMPS_BCT_PROCESSOR:
398      if ( imps_num_cpus < rtems_configuration_get_maximum_processors() ) {
399        if (_SMP_Should_start_processor((uint32_t) imps_num_cpus)) {
400          add_processor((imps_processor *)start);
401        }
402      } else
403        imps_num_cpus++;
404      start += 12;  /* 20 total */
405      break;
406    case IMPS_BCT_BUS:
407      add_bus((imps_bus *)start);
408      break;
409    case IMPS_BCT_IOAPIC:
410      add_ioapic((imps_ioapic *)start);
411      break;
412#if 0  /*  XXXXX  uncomment this if "add_io_interrupt" is implemented */
413    case IMPS_BCT_IO_INTERRUPT:
414      add_io_interrupt((imps_interrupt *)start);
415      break;
416#endif
417#if 0  /*  XXXXX  uncomment this if "add_local_interrupt" is implemented */
418    case IMPS_BCT_LOCAL_INTERRUPT:
419      add_local_interupt((imps_interrupt *)start);
420      break;
421#endif
422    default:
423      break;
424    }
425    start += 8;
426  }
427  if ( imps_num_cpus > rtems_configuration_get_maximum_processors() ) {
428    printk(
429      "WARNING!! Found more CPUs (%d) than configured for (%d)!!\n",
430      imps_num_cpus - 1,
431      rtems_configuration_get_maximum_processors()
432    );
433    imps_num_cpus = rtems_configuration_get_maximum_processors();
434    return;
435  }
436}
437
438static int
439imps_bad_bios(imps_fps *fps_ptr)
440{
441  int sum;
442  imps_cth *local_cth_ptr
443    = (imps_cth *) PHYS_TO_VIRTUAL(fps_ptr->cth_ptr);
444
445  if (fps_ptr->feature_info[0] > IMPS_FPS_DEFAULT_MAX) {
446    printk("    Invalid MP System Configuration type %d\n",
447            fps_ptr->feature_info[0]);
448    return 1;
449  }
450
451  if (fps_ptr->cth_ptr) {
452    sum = get_checksum((unsigned)local_cth_ptr,
453                                   local_cth_ptr->base_length);
454    if (local_cth_ptr->sig != IMPS_CTH_SIGNATURE || sum) {
455      printk(
456        "    Bad MP Config Table sig 0x%x and/or checksum 0x%x\n",
457        (unsigned)(fps_ptr->cth_ptr),
458        sum
459      );
460      return 1;
461    }
462    if (local_cth_ptr->spec_rev != fps_ptr->spec_rev) {
463      printk(
464        "    Bad MP Config Table sub-revision # %d\n",
465        local_cth_ptr->spec_rev
466      );
467      return 1;
468    }
469    if (local_cth_ptr->extended_length) {
470      sum = (get_checksum(((unsigned)local_cth_ptr)
471              + local_cth_ptr->base_length,
472              local_cth_ptr->extended_length)
473             + local_cth_ptr->extended_checksum) & 0xFF;
474      if (sum) {
475        printk("    Bad Extended MP Config Table checksum 0x%x\n", sum);
476        return 1;
477      }
478    }
479  } else if (!fps_ptr->feature_info[0]) {
480    printk("    Missing configuration information\n");
481    return 1;
482  }
483
484  return 0;
485}
486
487static void
488imps_read_bios(imps_fps *fps_ptr)
489{
490  int apicid;
491  unsigned cth_start, cth_count;
492  imps_cth *local_cth_ptr
493    = (imps_cth *)PHYS_TO_VIRTUAL(fps_ptr->cth_ptr);
494  char *str_ptr;
495
496  printk("Intel MultiProcessor Spec 1.%d BIOS support detected\n",
497          fps_ptr->spec_rev);
498
499  /*
500   *  Do all checking of errors which would definitely
501   *  lead to failure of the SMP boot here.
502   */
503  if (imps_bad_bios(fps_ptr)) {
504    printk("    Disabling MPS support\n");
505    return;
506  }
507
508  if (fps_ptr->feature_info[1] & IMPS_FPS_IMCRP_BIT) {
509    str_ptr = "IMCR and PIC";
510  } else {
511    str_ptr = "Virtual Wire";
512  }
513  if (fps_ptr->cth_ptr) {
514    imps_lapic_addr = local_cth_ptr->lapic_addr;
515  } else {
516    imps_lapic_addr = LAPIC_ADDR_DEFAULT;
517  }
518  printk("    APIC config: \"%s mode\"    Local APIC address: 0x%x\n",
519          str_ptr, imps_lapic_addr);
520  if (imps_lapic_addr != (READ_MSR_LO(0x1b) & 0xFFFFF000)) {
521    printk("Inconsistent Local APIC address, Disabling SMP support\n");
522    return;
523  }
524  imps_lapic_addr = PHYS_TO_VIRTUAL(imps_lapic_addr);
525
526  /*
527   *  Setup primary CPU.
528   */
529  apicid = IMPS_LAPIC_READ(LAPIC_SPIV);
530  IMPS_LAPIC_WRITE(LAPIC_SPIV, apicid|LAPIC_SPIV_ENABLE_APIC);
531  apicid = APIC_ID(IMPS_LAPIC_READ(LAPIC_ID));
532  imps_cpu_apic_map[0] = apicid;
533  imps_apic_cpu_map[apicid] = 0;
534
535  if (fps_ptr->cth_ptr) {
536    char str1[16], str2[16];
537    memcpy(str1, local_cth_ptr->oem_id, 8);
538    str1[8] = 0;
539    memcpy(str2, local_cth_ptr->prod_id, 12);
540    str2[12] = 0;
541    printk("  OEM id: %s  Product id: %s\n", str1, str2);
542    cth_start = ((unsigned) local_cth_ptr) + sizeof(imps_cth);
543    cth_count = local_cth_ptr->entry_count;
544  } else {
545    *((volatile unsigned *) IOAPIC_ADDR_DEFAULT) =  IOAPIC_ID;
546    defconfig.ioapic.id
547      = APIC_ID(*((volatile unsigned *)
548            (IOAPIC_ADDR_DEFAULT+IOAPIC_RW)));
549    *((volatile unsigned *) IOAPIC_ADDR_DEFAULT) =  IOAPIC_VER;
550    defconfig.ioapic.ver
551      = APIC_VERSION(*((volatile unsigned *)
552           (IOAPIC_ADDR_DEFAULT+IOAPIC_RW)));
553    defconfig.proc[apicid].flags
554      = IMPS_FLAG_ENABLED|IMPS_CPUFLAG_BOOT;
555    defconfig.proc[!apicid].flags = IMPS_FLAG_ENABLED;
556    imps_num_cpus = 2;
557    if (fps_ptr->feature_info[0] == 1
558     || fps_ptr->feature_info[0] == 5) {
559      memcpy(defconfig.bus[0].bus_type, "ISA   ", 6);
560    }
561    if (fps_ptr->feature_info[0] == 4
562     || fps_ptr->feature_info[0] == 7) {
563      memcpy(defconfig.bus[0].bus_type, "MCA   ", 6);
564    }
565    if (fps_ptr->feature_info[0] > 4) {
566      defconfig.proc[0].apic_ver = 0x10;
567      defconfig.proc[1].apic_ver = 0x10;
568      defconfig.bus[1].type = IMPS_BCT_BUS;
569    }
570    if (fps_ptr->feature_info[0] == 2) {
571      defconfig.intin[2].type = 255;
572      defconfig.intin[13].type = 255;
573    }
574    if (fps_ptr->feature_info[0] == 7) {
575      defconfig.intin[0].type = 255;
576    }
577    cth_start = (unsigned) &defconfig;
578    cth_count = DEF_ENTRIES;
579  }
580  imps_read_config_table(cth_start, cth_count);
581
582  /* %%%%% ESB read extended entries here */
583
584  imps_enabled = 1;
585}
586
587/*
588 *  Given a region to check, this actually looks for the "MP Floating
589 *  Pointer Structure".  The return value indicates if the correct
590 *  signature and checksum for a floating pointer structure of the
591 *  appropriate spec revision was found.  If so, then do not search
592 *  further.
593 *
594 *  NOTE:  The memory scan will always be in the bottom 1 MB.
595 *
596 *  This function presumes that "start" will always be aligned to a 16-bit
597 *  boundary.
598 *
599 *  Function finished.
600 */
601static int
602imps_scan(unsigned start, unsigned length)
603{
604  printk("Scanning from 0x%x for %d bytes\n", start, length);
605
606  while (length > 0) {
607    imps_fps *fps_ptr = (imps_fps *) PHYS_TO_VIRTUAL(start);
608
609    if (fps_ptr->sig == IMPS_FPS_SIGNATURE
610     && fps_ptr->length == 1
611     && (fps_ptr->spec_rev == 1 || fps_ptr->spec_rev == 4)
612     && !get_checksum(start, 16)) {
613      printk("Found MP Floating Structure Pointer at %x\n", start);
614      imps_read_bios(fps_ptr);
615      return 1;
616    }
617
618    length -= 16;
619    start += 16;
620  }
621
622  return 0;
623}
624
625#if !defined(__rtems__)
626/*
627 *  This is the primary function to "force" SMP support, with
628 *  the assumption that you have consecutively numbered APIC ids.
629 */
630int
631imps_force(int ncpus)
632{
633  int apicid, i;
634  imps_processor p;
635
636  printk("Intel MultiProcessor \"Force\" Support\n");
637
638  imps_lapic_addr = (READ_MSR_LO(0x1b) & 0xFFFFF000);
639  imps_lapic_addr = PHYS_TO_VIRTUAL(imps_lapic_addr);
640
641  /*
642   *  Setup primary CPU.
643   */
644  apicid = IMPS_LAPIC_READ(LAPIC_SPIV);
645  IMPS_LAPIC_WRITE(LAPIC_SPIV, apicid|LAPIC_SPIV_ENABLE_APIC);
646  apicid = APIC_ID(IMPS_LAPIC_READ(LAPIC_ID));
647  imps_cpu_apic_map[0] = apicid;
648  imps_apic_cpu_map[apicid] = 0;
649
650  p.type = 0;
651  p.apic_ver = 0x10;
652  p.signature = p.features = 0;
653
654  for (i = 0; i < ncpus; i++) {
655    if (apicid == i) {
656      p.flags = IMPS_FLAG_ENABLED | IMPS_CPUFLAG_BOOT;
657    } else {
658      p.flags = IMPS_FLAG_ENABLED;
659    }
660    p.apic_id = i;
661    add_processor(&p);
662  }
663
664  return imps_num_cpus;
665}
666#endif
667
668/*
669 *  This is the primary function for probing for MPS compatible hardware
670 *  and BIOS information.  Call this during the early stages of OS startup,
671 *  before memory can be messed up.
672 *
673 *  The probe looks for the "MP Floating Pointer Structure" at locations
674 *  listed at the top of page 4-2 of the spec.
675 *
676 *  Environment requirements from the OS to run:
677 *
678 *   (1) : A non-linear virtual to physical memory mapping is probably OK,
679 *       as (I think) the structures all fall within page boundaries,
680 *       but a linear mapping is recommended.  Currently assumes that
681 *       the mapping will remain identical over time (which should be
682 *       OK since it only accesses memory which shouldn't be munged
683 *       by the OS anyway).
684 *   (2) : The OS only consumes memory which the BIOS says is OK to use,
685 *       and not any of the BIOS standard areas (the areas 0x400 to
686 *       0x600, the EBDA, 0xE0000 to 0xFFFFF, and unreported physical
687 *       RAM).  Sometimes a small amount of physical RAM is not
688 *       reported by the BIOS, to be used to store MPS and other
689 *       information.
690 *   (3) : It must be possible to read the CMOS.
691 *   (4) : There must be between 512K and 640K of lower memory (this is a
692 *       sanity check).
693 *
694 *  Function finished.
695 */
696static int
697imps_probe(void)
698{
699  /*
700   *  Determine possible address of the EBDA
701   */
702  unsigned ebda_addr = *((unsigned short *)
703             PHYS_TO_VIRTUAL(EBDA_SEG_ADDR)) << 4;
704
705  /*
706   *  Determine amount of installed lower memory (not *available*
707   *  lower memory).
708   *
709   *  NOTE:  This should work reliably as long as we verify the
710   *         machine is at least a system that could possibly have
711   *         MPS compatibility to begin with.
712   */
713  unsigned mem_lower = ((CMOS_READ_BYTE(CMOS_BASE_MEMORY+1) << 8)
714            | CMOS_READ_BYTE(CMOS_BASE_MEMORY))       << 10;
715
716#ifdef IMPS_DEBUG
717  imps_enabled = 0;
718  imps_num_cpus = 1;
719#endif
720
721  /*
722   *  Sanity check : if this isn't reasonable, it is almost impossibly
723   *    unlikely to be an MPS compatible machine, so return failure.
724   */
725  if (mem_lower < 512*1024 || mem_lower > 640*1024) {
726    return 0;
727  }
728
729  if (ebda_addr > mem_lower - 1024
730   || ebda_addr + *((unsigned char *) PHYS_TO_VIRTUAL(ebda_addr))
731         * 1024 > mem_lower) {
732    ebda_addr = 0;
733  }
734
735  if (((ebda_addr && imps_scan(ebda_addr, 1024))
736   || (!ebda_addr && imps_scan(mem_lower - 1024, 1024))
737   || imps_scan(0xF0000, 0x10000)) && imps_enabled) {
738    return imps_num_cpus;
739  }
740
741  /*
742   *  If no BIOS info on MPS hardware is found, then return failure.
743   */
744
745  return 0;
746}
747
748/*
749 *  RTEMS SMP BSP Support
750 */
751static void smp_apic_ack(void)
752{
753  (void) IMPS_LAPIC_READ(LAPIC_SPIV);  /* dummy read */
754  IMPS_LAPIC_WRITE(LAPIC_EOI, 0 );     /* ACK the interrupt */
755}
756
757static void bsp_inter_processor_interrupt(void *arg)
758{
759  (void) arg;
760
761  smp_apic_ack();
762
763  _SMP_Inter_processor_interrupt_handler(_Per_CPU_Get());
764}
765
766static void ipi_install_irq(void)
767{
768  rtems_status_code status;
769
770  status = rtems_interrupt_handler_install(
771    16,
772    "smp-imps",
773    RTEMS_INTERRUPT_UNIQUE,
774    bsp_inter_processor_interrupt,
775    NULL
776  );
777  assert(status == RTEMS_SUCCESSFUL);
778}
779
780#ifdef __SSE__
781extern void enable_sse(void);
782#endif
783
784/* pc386 specific initialization */
785static void secondary_cpu_initialize(void)
786{
787  int apicid;
788
789  asm volatile( "lidt IDT_Descriptor" );
790
791  apicid = IMPS_LAPIC_READ(LAPIC_SPIV);
792  IMPS_LAPIC_WRITE(LAPIC_SPIV, apicid|LAPIC_SPIV_ENABLE_APIC);
793
794#ifdef __SSE__
795  enable_sse();
796#endif
797
798  _SMP_Start_multitasking_on_secondary_processor( _Per_CPU_Get() );
799}
800
801uint32_t _CPU_SMP_Initialize( void )
802{
803  /* XXX need to deal with finding too many cores */
804
805  return (uint32_t) imps_probe();
806}
807
808void _CPU_SMP_Prepare_start_multitasking( void )
809{
810  /* Do nothing */
811}
812
813bool _CPU_SMP_Start_processor( uint32_t cpu_index )
814{
815  (void) cpu_index;
816
817  return true;
818}
819
820void _CPU_SMP_Finalize_initialization( uint32_t cpu_count )
821{
822  if ( cpu_count > 1 )
823    ipi_install_irq();
824}
825
826void _CPU_SMP_Send_interrupt( uint32_t target_processor_index )
827{
828  send_ipi( target_processor_index, 0x30 );
829}
Note: See TracBrowser for help on using the repository browser.