amdccp_pci.c revision 1.2 1 1.2 thorpej /* $NetBSD: amdccp_pci.c,v 1.2 2020/06/25 16:40:40 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <sys/cdefs.h>
33 1.2 thorpej __KERNEL_RCSID(0, "$NetBSD: amdccp_pci.c,v 1.2 2020/06/25 16:40:40 thorpej Exp $");
34 1.1 thorpej
35 1.1 thorpej #include <sys/param.h>
36 1.1 thorpej #include <sys/bus.h>
37 1.1 thorpej #include <sys/cpu.h>
38 1.1 thorpej #include <sys/device.h>
39 1.1 thorpej
40 1.1 thorpej #include <dev/pci/pcireg.h>
41 1.1 thorpej #include <dev/pci/pcivar.h>
42 1.1 thorpej #include <dev/pci/pcidevs.h>
43 1.1 thorpej
44 1.1 thorpej #include <dev/ic/amdccpvar.h>
45 1.1 thorpej
46 1.1 thorpej static int amdccp_pci_match(device_t, cfdata_t, void *);
47 1.1 thorpej static void amdccp_pci_attach(device_t, device_t, void *);
48 1.1 thorpej
49 1.1 thorpej CFATTACH_DECL_NEW(amdccp_pci, sizeof(struct amdccp_softc),
50 1.1 thorpej amdccp_pci_match, amdccp_pci_attach, NULL, NULL);
51 1.1 thorpej
52 1.1 thorpej #define AMDCCP_MEM_BAR PCI_BAR2
53 1.1 thorpej
54 1.1 thorpej static const struct amdccp_pci_product {
55 1.1 thorpej pci_vendor_id_t vendor;
56 1.1 thorpej pci_product_id_t product;
57 1.1 thorpej } amdccp_pci_products[] = {
58 1.1 thorpej { .vendor = PCI_VENDOR_AMD,
59 1.1 thorpej .product = PCI_PRODUCT_AMD_F16_CCP,
60 1.1 thorpej },
61 1.1 thorpej { .vendor = PCI_VENDOR_AMD,
62 1.1 thorpej .product = PCI_PRODUCT_AMD_F17_CCP_1,
63 1.1 thorpej },
64 1.1 thorpej { .vendor = PCI_VENDOR_AMD,
65 1.1 thorpej .product = PCI_PRODUCT_AMD_F17_CCP_2,
66 1.1 thorpej },
67 1.1 thorpej { .vendor = PCI_VENDOR_AMD,
68 1.1 thorpej .product = PCI_PRODUCT_AMD_F17_7X_CCP,
69 1.1 thorpej },
70 1.1 thorpej };
71 1.1 thorpej
72 1.1 thorpej static const struct amdccp_pci_product *
73 1.1 thorpej amdccp_pci_lookup(const struct pci_attach_args * const pa)
74 1.1 thorpej {
75 1.1 thorpej unsigned int i;
76 1.1 thorpej
77 1.1 thorpej for (i = 0; i < __arraycount(amdccp_pci_products); i++) {
78 1.1 thorpej if (PCI_VENDOR(pa->pa_id) == amdccp_pci_products[i].vendor &&
79 1.1 thorpej PCI_PRODUCT(pa->pa_id) == amdccp_pci_products[i].product) {
80 1.1 thorpej return &amdccp_pci_products[i];
81 1.1 thorpej }
82 1.1 thorpej }
83 1.1 thorpej return NULL;
84 1.1 thorpej }
85 1.1 thorpej
86 1.1 thorpej static int
87 1.1 thorpej amdccp_pci_match(device_t parent, cfdata_t cf, void *aux)
88 1.1 thorpej {
89 1.1 thorpej const struct pci_attach_args * const pa = aux;
90 1.1 thorpej
91 1.1 thorpej return amdccp_pci_lookup(pa) != NULL;
92 1.1 thorpej }
93 1.1 thorpej
94 1.1 thorpej static void
95 1.1 thorpej amdccp_pci_attach(device_t parent, device_t self, void *aux)
96 1.1 thorpej {
97 1.1 thorpej struct amdccp_softc * const sc = device_private(self);
98 1.1 thorpej const struct pci_attach_args * const pa = aux;
99 1.1 thorpej pcireg_t type;
100 1.1 thorpej
101 1.1 thorpej sc->sc_dev = self;
102 1.1 thorpej
103 1.1 thorpej aprint_naive("\n");
104 1.2 thorpej aprint_normal(": AMD Cryptographic Coprocessor\n");
105 1.1 thorpej
106 1.1 thorpej type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, AMDCCP_MEM_BAR);
107 1.1 thorpej if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM) {
108 1.1 thorpej aprint_error_dev(self, "expected MEM register, got IO\n");
109 1.1 thorpej return;
110 1.1 thorpej }
111 1.1 thorpej
112 1.1 thorpej if (pci_mapreg_map(pa, AMDCCP_MEM_BAR, type, 0,
113 1.1 thorpej &sc->sc_bst, &sc->sc_bsh, NULL, NULL) != 0) {
114 1.1 thorpej aprint_error_dev(self, "unable to map device registers\n");
115 1.1 thorpej return;
116 1.1 thorpej }
117 1.1 thorpej
118 1.1 thorpej amdccp_common_attach(sc);
119 1.1 thorpej }
120