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