Subversion Repositories HelenOS

Rev

Rev 4490 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2005 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup genarch
  30.  * @{
  31.  */
  32. /**
  33.  * @file
  34.  * @brief   Multiple APIC Description Table (MADT) parsing.
  35.  */
  36.  
  37. #include <arch/types.h>
  38. #include <genarch/acpi/acpi.h>
  39. #include <genarch/acpi/madt.h>
  40. #include <arch/smp/apic.h>
  41. #include <arch/smp/smp.h>
  42. #include <panic.h>
  43. #include <debug.h>
  44. #include <config.h>
  45. #include <print.h>
  46. #include <mm/slab.h>
  47. #include <memstr.h>
  48. #include <sort.h>
  49.  
  50. struct acpi_madt *acpi_madt = NULL;
  51.  
  52. #ifdef CONFIG_SMP
  53.  
  54. /** Standard ISA IRQ map; can be overriden by Interrupt Source Override entries of MADT. */
  55. int isa_irq_map[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  56.  
  57. static void madt_l_apic_entry(struct madt_l_apic *la, uint32_t index);
  58. static void madt_io_apic_entry(struct madt_io_apic *ioa, uint32_t index);
  59. static void madt_intr_src_ovrd_entry(struct madt_intr_src_ovrd *override, uint32_t index);
  60. static int madt_cmp(void * a, void * b);
  61.  
  62. struct madt_l_apic *madt_l_apic_entries = NULL;
  63. struct madt_io_apic *madt_io_apic_entries = NULL;
  64.  
  65. size_t madt_l_apic_entry_index = 0;
  66. size_t madt_io_apic_entry_index = 0;
  67. size_t madt_l_apic_entry_cnt = 0;
  68. size_t madt_io_apic_entry_cnt = 0;
  69. size_t cpu_count = 0;
  70.  
  71. struct madt_apic_header * * madt_entries_index = NULL;
  72. unsigned int madt_entries_index_cnt = 0;
  73.  
  74. char *entry[] = {
  75.     "L_APIC",
  76.     "IO_APIC",
  77.     "INTR_SRC_OVRD",
  78.     "NMI_SRC",
  79.     "L_APIC_NMI",
  80.     "L_APIC_ADDR_OVRD",
  81.     "IO_SAPIC",
  82.     "L_SAPIC",
  83.     "PLATFORM_INTR_SRC"
  84. };
  85.  
  86. /*
  87.  * ACPI MADT Implementation of SMP configuration interface.
  88.  */
  89. static size_t madt_cpu_count(void);
  90. static bool madt_cpu_enabled(size_t i);
  91. static bool madt_cpu_bootstrap(size_t i);
  92. static uint8_t madt_cpu_apic_id(size_t i);
  93. static int madt_irq_to_pin(unsigned int irq);
  94.  
  95. struct smp_config_operations madt_config_operations = {
  96.     .cpu_count = madt_cpu_count,
  97.     .cpu_enabled = madt_cpu_enabled,
  98.     .cpu_bootstrap = madt_cpu_bootstrap,
  99.     .cpu_apic_id = madt_cpu_apic_id,
  100.     .irq_to_pin = madt_irq_to_pin
  101. };
  102.  
  103. size_t madt_cpu_count(void)
  104. {
  105.     return madt_l_apic_entry_cnt;
  106. }
  107.  
  108. bool madt_cpu_enabled(size_t i)
  109. {
  110.     ASSERT(i < madt_l_apic_entry_cnt);
  111.     return ((struct madt_l_apic *) madt_entries_index[madt_l_apic_entry_index + i])->flags & 0x1;
  112.  
  113. }
  114.  
  115. bool madt_cpu_bootstrap(size_t i)
  116. {
  117.     ASSERT(i < madt_l_apic_entry_cnt);
  118.     return ((struct madt_l_apic *) madt_entries_index[madt_l_apic_entry_index + i])->apic_id == l_apic_id();
  119. }
  120.  
  121. uint8_t madt_cpu_apic_id(size_t i)
  122. {
  123.     ASSERT(i < madt_l_apic_entry_cnt);
  124.     return ((struct madt_l_apic *) madt_entries_index[madt_l_apic_entry_index + i])->apic_id;
  125. }
  126.  
  127. int madt_irq_to_pin(unsigned int irq)
  128. {
  129.     ASSERT(irq < sizeof(isa_irq_map) / sizeof(int));
  130.         return isa_irq_map[irq];
  131. }
  132.  
  133. int madt_cmp(void * a, void * b)
  134. {
  135.     return
  136.         (((struct madt_apic_header *) a)->type > ((struct madt_apic_header *) b)->type) ?
  137.         1 :
  138.         ((((struct madt_apic_header *) a)->type < ((struct madt_apic_header *) b)->type) ? -1 : 0);
  139. }
  140.    
  141. void acpi_madt_parse(void)
  142. {
  143.     struct madt_apic_header *end = (struct madt_apic_header *) (((uint8_t *) acpi_madt) + acpi_madt->header.length);
  144.     struct madt_apic_header *h;
  145.    
  146.     l_apic = (uint32_t *) (unative_t) acpi_madt->l_apic_address;
  147.  
  148.     /* calculate madt entries */
  149.     for (h = &acpi_madt->apic_header[0]; h < end; h = (struct madt_apic_header *) (((uint8_t *) h) + h->length)) {
  150.         madt_entries_index_cnt++;
  151.     }
  152.  
  153.     /* create madt apic entries index array */
  154.     madt_entries_index = (struct madt_apic_header * *) malloc(madt_entries_index_cnt * sizeof(struct madt_apic_header * *), FRAME_ATOMIC);
  155.     if (!madt_entries_index)
  156.         panic("Memory allocation error.");
  157.  
  158.     uint32_t index = 0;
  159.  
  160.     for (h = &acpi_madt->apic_header[0]; h < end; h = (struct madt_apic_header *) (((uint8_t *) h) + h->length)) {
  161.         madt_entries_index[index++] = h;
  162.     }
  163.  
  164.     /* Quicksort MADT index structure */
  165.     qsort(madt_entries_index, madt_entries_index_cnt, sizeof(uintptr_t), &madt_cmp);
  166.  
  167.     /* Parse MADT entries */
  168.     if (madt_entries_index_cnt > 0) {  
  169.         for (index = 0; index < madt_entries_index_cnt - 1; index++) {
  170.             h = madt_entries_index[index];
  171.             switch (h->type) {
  172.                 case MADT_L_APIC:
  173.                     madt_l_apic_entry((struct madt_l_apic *) h, index);
  174.                     break;
  175.                 case MADT_IO_APIC:
  176.                     madt_io_apic_entry((struct madt_io_apic *) h, index);
  177.                     break;
  178.                 case MADT_INTR_SRC_OVRD:
  179.                     madt_intr_src_ovrd_entry((struct madt_intr_src_ovrd *) h, index);
  180.                     break;
  181.                 case MADT_NMI_SRC:
  182.                 case MADT_L_APIC_NMI:
  183.                 case MADT_L_APIC_ADDR_OVRD:
  184.                 case MADT_IO_SAPIC:
  185.                 case MADT_L_SAPIC:
  186.                 case MADT_PLATFORM_INTR_SRC:
  187.                     printf("MADT: skipping %s entry (type=%" PRIu8 ")\n", entry[h->type], h->type);
  188.                     break;
  189.    
  190.                 default:
  191.                     if (h->type >= MADT_RESERVED_SKIP_BEGIN && h->type <= MADT_RESERVED_SKIP_END) {
  192.                         printf("MADT: skipping reserved entry (type=%" PRIu8 ")\n", h->type);
  193.                     }
  194.                     if (h->type >= MADT_RESERVED_OEM_BEGIN) {
  195.                         printf("MADT: skipping OEM entry (type=%" PRIu8 ")\n", h->type);
  196.                     }
  197.                     break;
  198.             }
  199.         }
  200.     }
  201.  
  202.     if (cpu_count)
  203.         config.cpu_count = cpu_count;
  204. }
  205.  
  206.  
  207. void madt_l_apic_entry(struct madt_l_apic *la, uint32_t index)
  208. {
  209.     if (!madt_l_apic_entry_cnt++) {
  210.         madt_l_apic_entry_index = index;
  211.     }
  212.        
  213.     if (!(la->flags & 0x1)) {
  214.         /* Processor is unusable, skip it. */
  215.         return;
  216.     }
  217.    
  218.     cpu_count++;   
  219.     apic_id_mask |= 1<<la->apic_id;
  220. }
  221.  
  222. void madt_io_apic_entry(struct madt_io_apic *ioa, uint32_t index)
  223. {
  224.     if (!madt_io_apic_entry_cnt++) {
  225.         /* remember index of the first io apic entry */
  226.         madt_io_apic_entry_index = index;
  227.         io_apic = (uint32_t *) (unative_t) ioa->io_apic_address;
  228.     } else {
  229.         /* currently not supported */
  230.         return;
  231.     }
  232. }
  233.  
  234. void madt_intr_src_ovrd_entry(struct madt_intr_src_ovrd *override, uint32_t index)
  235. {
  236.     ASSERT(override->source < sizeof(isa_irq_map) / sizeof(int));
  237.     printf("MADT: ignoring %s entry: bus=%" PRIu8 ", source=%" PRIu8 ", global_int=%" PRIu32 ", flags=%#" PRIx16 "\n",
  238.         entry[override->header.type], override->bus, override->source,
  239.         override->global_int, override->flags);
  240. }
  241.  
  242. #endif /* CONFIG_SMP */
  243.  
  244. /** @}
  245.  */
  246.