1 /* $OpenBSD: psp_pci.c,v 1.2 2024/11/08 17:34:22 bluhm Exp $ */
2
3 /*
4 * Copyright (c) 2023-2024 Hans-Joerg Hoexer <hshoexer@genua.de>
5 * Copyright (c) 2024 Alexander Bluhm <bluhm@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/device.h>
23
24 #include <machine/bus.h>
25
26 #include <dev/pci/pcidevs.h>
27 #include <dev/pci/pcivar.h>
28
29 #include <dev/ic/ccpvar.h>
30 #include <dev/ic/pspvar.h>
31
32 static const struct pci_matchid psp_pci_devices[] = {
33 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_17_CCP_1 },
34 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_17_3X_CCP },
35 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_19_1X_PSP },
36 };
37
38 int
psp_pci_match(struct ccp_softc * sc,struct pci_attach_args * pa)39 psp_pci_match(struct ccp_softc *sc, struct pci_attach_args *pa)
40 {
41 bus_size_t reg_capabilities;
42 uint32_t capabilities;
43
44 if (!pci_matchbyid(pa, psp_pci_devices, nitems(psp_pci_devices)))
45 return (0);
46
47 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_17_CCP_1)
48 reg_capabilities = PSPV1_REG_CAPABILITIES;
49 else
50 reg_capabilities = PSP_REG_CAPABILITIES;
51 capabilities = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
52 reg_capabilities);
53 if (!ISSET(capabilities, PSP_CAP_SEV))
54 return (0);
55
56 return (1);
57 }
58
59 void
psp_pci_intr_map(struct ccp_softc * sc,struct pci_attach_args * pa)60 psp_pci_intr_map(struct ccp_softc *sc, struct pci_attach_args *pa)
61 {
62 pci_intr_handle_t ih;
63 const char *intrstr = NULL;
64 bus_size_t reg_inten, reg_intsts;
65
66 /* clear and disable interrupts */
67 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_17_CCP_1) {
68 reg_inten = PSPV1_REG_INTEN;
69 reg_intsts = PSPV1_REG_INTSTS;
70 } else {
71 reg_inten = PSP_REG_INTEN;
72 reg_intsts = PSP_REG_INTSTS;
73 }
74 bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg_inten, 0);
75 bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg_intsts, -1);
76
77 if (pci_intr_map_msix(pa, 0, &ih) != 0 &&
78 pci_intr_map_msi(pa, &ih) != 0 && pci_intr_map(pa, &ih) != 0) {
79 printf(": couldn't map interrupt\n");
80 return;
81 }
82
83 intrstr = pci_intr_string(pa->pa_pc, ih);
84 sc->sc_irqh = pci_intr_establish(pa->pa_pc, ih, IPL_BIO | IPL_MPSAFE,
85 psp_sev_intr, sc, sc->sc_dev.dv_xname);
86 if (sc->sc_irqh != NULL)
87 printf(": %s", intrstr);
88 }
89
90 void
psp_pci_attach(struct ccp_softc * sc,struct pci_attach_args * pa)91 psp_pci_attach(struct ccp_softc *sc, struct pci_attach_args *pa)
92 {
93 struct psp_attach_args arg;
94 struct device *self = (struct device *)sc;
95 bus_size_t reg_capabilities;
96
97 memset(&arg, 0, sizeof(arg));
98 arg.iot = sc->sc_iot;
99 arg.ioh = sc->sc_ioh;
100 arg.dmat = pa->pa_dmat;
101 switch (PCI_PRODUCT(pa->pa_id)) {
102 case PCI_PRODUCT_AMD_17_CCP_1:
103 arg.version = 1;
104 reg_capabilities = PSPV1_REG_CAPABILITIES;
105 break;
106 case PCI_PRODUCT_AMD_17_3X_CCP:
107 arg.version = 2;
108 reg_capabilities = PSP_REG_CAPABILITIES;
109 break;
110 case PCI_PRODUCT_AMD_19_1X_PSP:
111 arg.version = 4;
112 reg_capabilities = PSP_REG_CAPABILITIES;
113 break;
114 default:
115 reg_capabilities = PSP_REG_CAPABILITIES;
116 break;
117 }
118 arg.capabilities = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
119 reg_capabilities);
120
121 sc->sc_psp = config_found_sm(self, &arg, pspprint, pspsubmatch);
122 if (sc->sc_psp == NULL) {
123 pci_intr_disestablish(pa->pa_pc, sc->sc_irqh);
124 return;
125 }
126 }
127