summaryrefslogtreecommitdiff
path: root/src/kernel/madt.c
blob: adeb547190ec2a99cd47c4367c69a926b016f95e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdint.h>
#include <libc.h>
#include <acpi.h>
#include <int.h>
#include <addr.h>
#include <stddef.h>
#include <heap.h>
#include <printf.h>
#include <smp.h>
#include <madt.h>
#include <cpuid.h>

//madt entry types. there's more, we don't need em yet
#define LOCAL_APIC                        0
#define IO_APIC                           1
#define IO_INT_OVERRIDE                   2
#define IO_APIC_NMI_SOURCE                3
#define IO_APIC_NMI                       4
#define MADT_LOCAL_APIC_ADDR_OVERRIDE     5

struct madt_sdt {
  sdt_head header;
  uint32_t apic_base;
  uint32_t flags;
} __attribute__((packed));

struct madt_entry {
  uint8_t type;
  uint8_t length;
} __attribute__((packed));

struct madt_local_apic {
  uint8_t processor_id;
  uint8_t apic_id;
  uint32_t flags;
} __attribute__((packed));

struct madt_local_apic_override {
  uint16_t reserved;
  void *apic_base;
} __attribute__((packed));

struct madt_ioapic {
  uint8_t apic_id;
  uint8_t reserved;
  uint32_t addr;
  uint32_t gsi_base;
} __attribute__((packed));

struct madt_io_int_override {
  uint8_t bus_src; //not sure what this is used for.
  uint8_t irq;
  uint32_t global_vector;
  uint16_t flags;
} __attribute__((packed));

static struct madt_sdt *madt;


void init_madt() {
  madt = find_sdt(SDT_MADT);
}

void debug_madt() {
  struct madt_entry *entry;
  for(
  entry = (void *)madt + sizeof(*madt); 
  (void *)entry < (void *)madt + madt->header.length; 
  entry = (void *)entry + entry->length) {
    printf("MADT debug: ");
    switch(entry->type) {
      case LOCAL_APIC:
        printf("local APIC\n");
        break;
      case IO_APIC:
        printf("IOAPIC\n");
        break;
      case IO_INT_OVERRIDE:
        printf("IOAPIC interupt override\n");
        break;
      case IO_APIC_NMI_SOURCE:
        printf("IOAPIC NMI\n");
        break;
      case IO_APIC_NMI:
        printf("Local APIC NMI\n");
        break;
      case MADT_LOCAL_APIC_ADDR_OVERRIDE:
        printf("APIC addr override\n");
        break;
      default:
        printf("other\n");
        break;
    }
  }
}

unsigned int irq_to_gsi(unsigned int irq) {
  struct madt_io_int_override *irq_override;
  struct madt_entry *entry;

  for(
  entry = (void *)madt + sizeof(*madt); 
  (void *)entry < (void *)madt + madt->header.length; 
  entry = (void *)entry + entry->length) {

    if(entry->type == IO_INT_OVERRIDE) {
      irq_override = (void *)entry + 2;
      if(irq_override->irq == irq) return irq_override->global_vector;
    }
  }
  return (uint32_t)irq; //no entry found
}

void get_ioapic(struct ioapic_fixedlen *ret) {
  struct madt_entry *entry;
  struct madt_ioapic *mpio_entry;

  ret->count = 0;
  
  for(
  entry = (void *)madt + sizeof(*madt); 
  (void *)entry < (void *)madt + madt->header.length; 
  entry = (void *)entry + entry->length) {
    if(entry->type == IO_APIC) {
      mpio_entry = (void *)entry + 2;
      if(ret->count) {
        ret->ioapic = realloc(ret->ioapic, ++ret->count * sizeof(struct ioapic_t));
      }
      else {
        ret->ioapic = malloc(++ret->count * sizeof(struct ioapic_t));
      }
      ret->ioapic[ret->count - 1].address = PHYS_TO_VIRT((uintptr_t)mpio_entry->addr);
      ret->ioapic[ret->count - 1].gsi_base = mpio_entry->gsi_base;
    }
  }
}

lapic_t get_lapic() {
  struct madt_local_apic_override *ap_override;
  struct madt_entry *entry;
  
  for(
  entry = (void *)madt + sizeof(*madt); 
  (void *)entry < (void *)madt + madt->header.length; 
  entry = (void *)entry + entry->length) {

    if(entry->type == MADT_LOCAL_APIC_ADDR_OVERRIDE) {
      ap_override = (void *)entry + 2;
      
      return (lapic_t)PHYS_TO_VIRT(ap_override->apic_base);
    }
  }
  return (lapic_t)PHYS_TO_VIRT((uint64_t)madt->apic_base);
}

void get_coreinfo(struct cores_info *cores) {
  struct madt_local_apic *ap_info; 
  struct madt_entry *entry;
  uint32_t unused_cpuid, ebx;

  __get_cpuid(1, &unused_cpuid, &ebx, &unused_cpuid, &unused_cpuid);
  cores->bsp = ebx << 24;

  bzero(cores, sizeof(*cores));
  
  for(
  entry = (void *)madt + sizeof(*madt); 
  (void *)entry < (void *)madt + madt->header.length; 
  entry = (void *)entry + entry->length) {

    if(entry->type == LOCAL_APIC) { 
      ap_info = (void *)entry + 2;
      if(ap_info->flags & 1) {
        cores->apic_id[cores->corecount] = ap_info->processor_id;
        cores->corecount++;
      }
    }
  }
}