Serenity Operating System
1/*
2 * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
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 are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <Kernel/Interrupts/IRQController.h>
30
31namespace Kernel {
32struct [[gnu::packed]] ioapic_mmio_regs
33{
34 volatile u32 select;
35 u32 reserved[3];
36 volatile u32 window;
37};
38
39class ISAInterruptOverrideMetadata;
40class PCIInterruptOverrideMetadata;
41
42class IOAPIC final : public IRQController {
43public:
44 IOAPIC(ioapic_mmio_regs& regs, u32 gsi_base);
45 virtual void enable(const GenericInterruptHandler&) override;
46 virtual void disable(const GenericInterruptHandler&) override;
47 virtual void hard_disable() override;
48 virtual void eoi(const GenericInterruptHandler&) const override;
49 virtual void spurious_eoi(const GenericInterruptHandler&) const override;
50 virtual bool is_vector_enabled(u8 number) const override;
51 virtual bool is_enabled() const override;
52 virtual u16 get_isr() const override;
53 virtual u16 get_irr() const override;
54 virtual u32 gsi_base() const override { return m_gsi_base; }
55 virtual size_t interrupt_vectors_count() const { return m_redirection_entries_count; }
56 virtual const char* model() const override { return "IOAPIC"; };
57 virtual IRQControllerType type() const override { return IRQControllerType::i82093AA; }
58
59private:
60 void configure_redirection_entry(int index, u8 interrupt_vector, u8 delivery_mode, bool logical_destination, bool active_low, bool trigger_level_mode, bool masked, u8 destination) const;
61 void reset_redirection_entry(int index) const;
62 void map_interrupt_redirection(u8 interrupt_vector);
63 void reset_all_redirection_entries() const;
64
65 void mask_all_redirection_entries() const;
66 void mask_redirection_entry(u8 index) const;
67 void unmask_redirection_entry(u8 index) const;
68 bool is_redirection_entry_masked(u8 index) const;
69
70 u8 read_redirection_entry_vector(u8 index) const;
71 Optional<int> find_redirection_entry_by_vector(u8 vector) const;
72 void configure_redirections() const;
73
74 void write_register(u32 index, u32 value) const;
75 u32 read_register(u32 index) const;
76
77 virtual void initialize() override;
78 void map_isa_interrupts();
79 void map_pci_interrupts();
80 void isa_identity_map(int index);
81
82 ioapic_mmio_regs& m_physical_access_registers;
83 u32 m_gsi_base;
84 u8 m_id;
85 u8 m_version;
86 size_t m_redirection_entries_count;
87};
88}