186be9f0dSKonstantin Belousov /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3ebf5747bSPedro F. Giffuni *
40a110d5bSKonstantin Belousov * Copyright (c) 2013, 2015 The FreeBSD Foundation
586be9f0dSKonstantin Belousov *
686be9f0dSKonstantin Belousov * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
786be9f0dSKonstantin Belousov * under sponsorship from the FreeBSD Foundation.
886be9f0dSKonstantin Belousov *
986be9f0dSKonstantin Belousov * Redistribution and use in source and binary forms, with or without
1086be9f0dSKonstantin Belousov * modification, are permitted provided that the following conditions
1186be9f0dSKonstantin Belousov * are met:
1286be9f0dSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright
1386be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer.
1486be9f0dSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright
1586be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the
1686be9f0dSKonstantin Belousov * documentation and/or other materials provided with the distribution.
1786be9f0dSKonstantin Belousov *
1886be9f0dSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1986be9f0dSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2086be9f0dSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2186be9f0dSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2286be9f0dSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2386be9f0dSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2486be9f0dSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2586be9f0dSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2686be9f0dSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2786be9f0dSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2886be9f0dSKonstantin Belousov * SUCH DAMAGE.
2986be9f0dSKonstantin Belousov */
3086be9f0dSKonstantin Belousov
3186be9f0dSKonstantin Belousov #include <sys/param.h>
3286be9f0dSKonstantin Belousov #include <sys/bus.h>
3386be9f0dSKonstantin Belousov #include <sys/kernel.h>
3486be9f0dSKonstantin Belousov #include <sys/lock.h>
3586be9f0dSKonstantin Belousov #include <sys/malloc.h>
3686be9f0dSKonstantin Belousov #include <sys/memdesc.h>
3786be9f0dSKonstantin Belousov #include <sys/module.h>
38e2e050c8SConrad Meyer #include <sys/mutex.h>
3986be9f0dSKonstantin Belousov #include <sys/rman.h>
4086be9f0dSKonstantin Belousov #include <sys/rwlock.h>
4186be9f0dSKonstantin Belousov #include <sys/smp.h>
4286be9f0dSKonstantin Belousov #include <sys/taskqueue.h>
4386be9f0dSKonstantin Belousov #include <sys/tree.h>
440a110d5bSKonstantin Belousov #include <sys/vmem.h>
4586be9f0dSKonstantin Belousov #include <vm/vm.h>
4686be9f0dSKonstantin Belousov #include <vm/vm_extern.h>
4786be9f0dSKonstantin Belousov #include <vm/vm_kern.h>
4886be9f0dSKonstantin Belousov #include <vm/vm_object.h>
4986be9f0dSKonstantin Belousov #include <vm/vm_page.h>
5086be9f0dSKonstantin Belousov #include <vm/vm_pager.h>
5186be9f0dSKonstantin Belousov #include <vm/vm_map.h>
52c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/acpi.h>
53c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/accommon.h>
54c8597a1fSRuslan Bukin #include <dev/acpica/acpivar.h>
55685666aaSKonstantin Belousov #include <dev/pci/pcireg.h>
5686be9f0dSKonstantin Belousov #include <dev/pci/pcivar.h>
57c8597a1fSRuslan Bukin #include <machine/bus.h>
58c8597a1fSRuslan Bukin #include <x86/include/busdma_impl.h>
59c8597a1fSRuslan Bukin #include <dev/iommu/busdma_iommu.h>
60c8597a1fSRuslan Bukin #include <x86/iommu/intel_reg.h>
6140d951bcSKonstantin Belousov #include <x86/iommu/x86_iommu.h>
62c8597a1fSRuslan Bukin #include <x86/iommu/intel_dmar.h>
6386be9f0dSKonstantin Belousov
640a110d5bSKonstantin Belousov typedef void (*dmar_quirk_cpu_fun)(struct dmar_unit *);
6586be9f0dSKonstantin Belousov
6686be9f0dSKonstantin Belousov struct intel_dmar_quirk_cpu {
6786be9f0dSKonstantin Belousov u_int ext_family;
6886be9f0dSKonstantin Belousov u_int ext_model;
6986be9f0dSKonstantin Belousov u_int family_code;
7086be9f0dSKonstantin Belousov u_int model;
7186be9f0dSKonstantin Belousov u_int stepping;
720a110d5bSKonstantin Belousov dmar_quirk_cpu_fun quirk;
7386be9f0dSKonstantin Belousov const char *descr;
7486be9f0dSKonstantin Belousov };
7586be9f0dSKonstantin Belousov
760a110d5bSKonstantin Belousov typedef void (*dmar_quirk_nb_fun)(struct dmar_unit *, device_t nb);
770a110d5bSKonstantin Belousov
7886be9f0dSKonstantin Belousov struct intel_dmar_quirk_nb {
7986be9f0dSKonstantin Belousov u_int dev_id;
8086be9f0dSKonstantin Belousov u_int rev_no;
810a110d5bSKonstantin Belousov dmar_quirk_nb_fun quirk;
8286be9f0dSKonstantin Belousov const char *descr;
8386be9f0dSKonstantin Belousov };
8486be9f0dSKonstantin Belousov
850a110d5bSKonstantin Belousov #define QUIRK_NB_ALL_REV 0xffffffff
860a110d5bSKonstantin Belousov
8786be9f0dSKonstantin Belousov static void
dmar_match_quirks(struct dmar_unit * dmar,const struct intel_dmar_quirk_nb * nb_quirks,int nb_quirks_len,const struct intel_dmar_quirk_cpu * cpu_quirks,int cpu_quirks_len)8886be9f0dSKonstantin Belousov dmar_match_quirks(struct dmar_unit *dmar,
8986be9f0dSKonstantin Belousov const struct intel_dmar_quirk_nb *nb_quirks, int nb_quirks_len,
9086be9f0dSKonstantin Belousov const struct intel_dmar_quirk_cpu *cpu_quirks, int cpu_quirks_len)
9186be9f0dSKonstantin Belousov {
9286be9f0dSKonstantin Belousov device_t nb;
9386be9f0dSKonstantin Belousov const struct intel_dmar_quirk_nb *nb_quirk;
9486be9f0dSKonstantin Belousov const struct intel_dmar_quirk_cpu *cpu_quirk;
9586be9f0dSKonstantin Belousov u_int p[4];
9686be9f0dSKonstantin Belousov u_int dev_id, rev_no;
9786be9f0dSKonstantin Belousov u_int ext_family, ext_model, family_code, model, stepping;
9886be9f0dSKonstantin Belousov int i;
9986be9f0dSKonstantin Belousov
10086be9f0dSKonstantin Belousov if (nb_quirks != NULL) {
10186be9f0dSKonstantin Belousov nb = pci_find_bsf(0, 0, 0);
10286be9f0dSKonstantin Belousov if (nb != NULL) {
10386be9f0dSKonstantin Belousov dev_id = pci_get_device(nb);
10486be9f0dSKonstantin Belousov rev_no = pci_get_revid(nb);
10586be9f0dSKonstantin Belousov for (i = 0; i < nb_quirks_len; i++) {
10686be9f0dSKonstantin Belousov nb_quirk = &nb_quirks[i];
10786be9f0dSKonstantin Belousov if (nb_quirk->dev_id == dev_id &&
1080a110d5bSKonstantin Belousov (nb_quirk->rev_no == rev_no ||
1090a110d5bSKonstantin Belousov nb_quirk->rev_no == QUIRK_NB_ALL_REV)) {
11086be9f0dSKonstantin Belousov if (bootverbose) {
111*164fdee1SKonstantin Belousov device_printf(dmar->iommu.dev,
11286be9f0dSKonstantin Belousov "NB IOMMU quirk %s\n",
11386be9f0dSKonstantin Belousov nb_quirk->descr);
11486be9f0dSKonstantin Belousov }
1150a110d5bSKonstantin Belousov nb_quirk->quirk(dmar, nb);
11686be9f0dSKonstantin Belousov }
11786be9f0dSKonstantin Belousov }
11886be9f0dSKonstantin Belousov } else {
119*164fdee1SKonstantin Belousov device_printf(dmar->iommu.dev,
120*164fdee1SKonstantin Belousov "cannot find northbridge\n");
12186be9f0dSKonstantin Belousov }
12286be9f0dSKonstantin Belousov }
12386be9f0dSKonstantin Belousov if (cpu_quirks != NULL) {
12486be9f0dSKonstantin Belousov do_cpuid(1, p);
12586be9f0dSKonstantin Belousov ext_family = (p[0] & CPUID_EXT_FAMILY) >> 20;
12686be9f0dSKonstantin Belousov ext_model = (p[0] & CPUID_EXT_MODEL) >> 16;
12786be9f0dSKonstantin Belousov family_code = (p[0] & CPUID_FAMILY) >> 8;
12886be9f0dSKonstantin Belousov model = (p[0] & CPUID_MODEL) >> 4;
12986be9f0dSKonstantin Belousov stepping = p[0] & CPUID_STEPPING;
13086be9f0dSKonstantin Belousov for (i = 0; i < cpu_quirks_len; i++) {
13186be9f0dSKonstantin Belousov cpu_quirk = &cpu_quirks[i];
13286be9f0dSKonstantin Belousov if (cpu_quirk->ext_family == ext_family &&
13386be9f0dSKonstantin Belousov cpu_quirk->ext_model == ext_model &&
13486be9f0dSKonstantin Belousov cpu_quirk->family_code == family_code &&
13586be9f0dSKonstantin Belousov cpu_quirk->model == model &&
13686be9f0dSKonstantin Belousov (cpu_quirk->stepping == -1 ||
13786be9f0dSKonstantin Belousov cpu_quirk->stepping == stepping)) {
13886be9f0dSKonstantin Belousov if (bootverbose) {
139*164fdee1SKonstantin Belousov device_printf(dmar->iommu.dev,
14086be9f0dSKonstantin Belousov "CPU IOMMU quirk %s\n",
14186be9f0dSKonstantin Belousov cpu_quirk->descr);
14286be9f0dSKonstantin Belousov }
14386be9f0dSKonstantin Belousov cpu_quirk->quirk(dmar);
14486be9f0dSKonstantin Belousov }
14586be9f0dSKonstantin Belousov }
14686be9f0dSKonstantin Belousov }
14786be9f0dSKonstantin Belousov }
14886be9f0dSKonstantin Belousov
14986be9f0dSKonstantin Belousov static void
nb_5400_no_low_high_prot_mem(struct dmar_unit * unit,device_t nb __unused)1500a110d5bSKonstantin Belousov nb_5400_no_low_high_prot_mem(struct dmar_unit *unit, device_t nb __unused)
15186be9f0dSKonstantin Belousov {
15286be9f0dSKonstantin Belousov
15386be9f0dSKonstantin Belousov unit->hw_cap &= ~(DMAR_CAP_PHMR | DMAR_CAP_PLMR);
15486be9f0dSKonstantin Belousov }
15586be9f0dSKonstantin Belousov
1560a110d5bSKonstantin Belousov static void
nb_no_ir(struct dmar_unit * unit,device_t nb __unused)1570a110d5bSKonstantin Belousov nb_no_ir(struct dmar_unit *unit, device_t nb __unused)
1580a110d5bSKonstantin Belousov {
1590a110d5bSKonstantin Belousov
1600a110d5bSKonstantin Belousov unit->hw_ecap &= ~(DMAR_ECAP_IR | DMAR_ECAP_EIM);
1610a110d5bSKonstantin Belousov }
1620a110d5bSKonstantin Belousov
1630a110d5bSKonstantin Belousov static void
nb_5500_no_ir_rev13(struct dmar_unit * unit,device_t nb)1640a110d5bSKonstantin Belousov nb_5500_no_ir_rev13(struct dmar_unit *unit, device_t nb)
1650a110d5bSKonstantin Belousov {
1660a110d5bSKonstantin Belousov u_int rev_no;
1670a110d5bSKonstantin Belousov
1680a110d5bSKonstantin Belousov rev_no = pci_get_revid(nb);
1690a110d5bSKonstantin Belousov if (rev_no <= 0x13)
1700a110d5bSKonstantin Belousov nb_no_ir(unit, nb);
1710a110d5bSKonstantin Belousov }
1720a110d5bSKonstantin Belousov
17386be9f0dSKonstantin Belousov static const struct intel_dmar_quirk_nb pre_use_nb[] = {
17486be9f0dSKonstantin Belousov {
17586be9f0dSKonstantin Belousov .dev_id = 0x4001, .rev_no = 0x20,
17686be9f0dSKonstantin Belousov .quirk = nb_5400_no_low_high_prot_mem,
17786be9f0dSKonstantin Belousov .descr = "5400 E23" /* no low/high protected memory */
17886be9f0dSKonstantin Belousov },
17986be9f0dSKonstantin Belousov {
18086be9f0dSKonstantin Belousov .dev_id = 0x4003, .rev_no = 0x20,
18186be9f0dSKonstantin Belousov .quirk = nb_5400_no_low_high_prot_mem,
18286be9f0dSKonstantin Belousov .descr = "5400 E23" /* no low/high protected memory */
18386be9f0dSKonstantin Belousov },
1840a110d5bSKonstantin Belousov {
1850a110d5bSKonstantin Belousov .dev_id = 0x3403, .rev_no = QUIRK_NB_ALL_REV,
1860a110d5bSKonstantin Belousov .quirk = nb_5500_no_ir_rev13,
1870a110d5bSKonstantin Belousov .descr = "5500 E47, E53" /* interrupt remapping does not work */
1880a110d5bSKonstantin Belousov },
1890a110d5bSKonstantin Belousov {
1900a110d5bSKonstantin Belousov .dev_id = 0x3405, .rev_no = QUIRK_NB_ALL_REV,
1910a110d5bSKonstantin Belousov .quirk = nb_5500_no_ir_rev13,
1920a110d5bSKonstantin Belousov .descr = "5500 E47, E53" /* interrupt remapping does not work */
1930a110d5bSKonstantin Belousov },
1940a110d5bSKonstantin Belousov {
1950a110d5bSKonstantin Belousov .dev_id = 0x3405, .rev_no = 0x22,
1960a110d5bSKonstantin Belousov .quirk = nb_no_ir,
1970a110d5bSKonstantin Belousov .descr = "5500 E47, E53" /* interrupt remapping does not work */
1980a110d5bSKonstantin Belousov },
1990a110d5bSKonstantin Belousov {
2000a110d5bSKonstantin Belousov .dev_id = 0x3406, .rev_no = QUIRK_NB_ALL_REV,
2010a110d5bSKonstantin Belousov .quirk = nb_5500_no_ir_rev13,
2020a110d5bSKonstantin Belousov .descr = "5500 E47, E53" /* interrupt remapping does not work */
2030a110d5bSKonstantin Belousov },
20486be9f0dSKonstantin Belousov };
20586be9f0dSKonstantin Belousov
20686be9f0dSKonstantin Belousov static void
cpu_e5_am9(struct dmar_unit * unit)20786be9f0dSKonstantin Belousov cpu_e5_am9(struct dmar_unit *unit)
20886be9f0dSKonstantin Belousov {
20986be9f0dSKonstantin Belousov
21086be9f0dSKonstantin Belousov unit->hw_cap &= ~(0x3fULL << 48);
21186be9f0dSKonstantin Belousov unit->hw_cap |= (9ULL << 48);
21286be9f0dSKonstantin Belousov }
21386be9f0dSKonstantin Belousov
21486be9f0dSKonstantin Belousov static const struct intel_dmar_quirk_cpu post_ident_cpu[] = {
21586be9f0dSKonstantin Belousov {
21686be9f0dSKonstantin Belousov .ext_family = 0, .ext_model = 2, .family_code = 6, .model = 13,
21786be9f0dSKonstantin Belousov .stepping = 6, .quirk = cpu_e5_am9,
21886be9f0dSKonstantin Belousov .descr = "E5 BT176" /* AM should be at most 9 */
21986be9f0dSKonstantin Belousov },
22086be9f0dSKonstantin Belousov };
22186be9f0dSKonstantin Belousov
22286be9f0dSKonstantin Belousov void
dmar_quirks_pre_use(struct iommu_unit * unit)22359e37c8aSRuslan Bukin dmar_quirks_pre_use(struct iommu_unit *unit)
22486be9f0dSKonstantin Belousov {
22559e37c8aSRuslan Bukin struct dmar_unit *dmar;
22659e37c8aSRuslan Bukin
22778b51754SRuslan Bukin dmar = IOMMU2DMAR(unit);
22886be9f0dSKonstantin Belousov
22986be9f0dSKonstantin Belousov if (!dmar_barrier_enter(dmar, DMAR_BARRIER_USEQ))
23086be9f0dSKonstantin Belousov return;
23186be9f0dSKonstantin Belousov DMAR_LOCK(dmar);
23286be9f0dSKonstantin Belousov dmar_match_quirks(dmar, pre_use_nb, nitems(pre_use_nb),
23386be9f0dSKonstantin Belousov NULL, 0);
23486be9f0dSKonstantin Belousov dmar_barrier_exit(dmar, DMAR_BARRIER_USEQ);
23586be9f0dSKonstantin Belousov }
23686be9f0dSKonstantin Belousov
23786be9f0dSKonstantin Belousov void
dmar_quirks_post_ident(struct dmar_unit * dmar)23886be9f0dSKonstantin Belousov dmar_quirks_post_ident(struct dmar_unit *dmar)
23986be9f0dSKonstantin Belousov {
24086be9f0dSKonstantin Belousov
24186be9f0dSKonstantin Belousov dmar_match_quirks(dmar, NULL, 0, post_ident_cpu,
24286be9f0dSKonstantin Belousov nitems(post_ident_cpu));
24386be9f0dSKonstantin Belousov }
244