1 /* $NetBSD: amdccp_pci.c,v 1.4 2022/12/18 15:50:32 reinoud 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.4 2022/12/18 15:50:32 reinoud 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_1X_PSP, 69 }, 70 { .vendor = PCI_VENDOR_AMD, 71 .product = PCI_PRODUCT_AMD_F17_7X_CCP, 72 }, 73 { .vendor = PCI_VENDOR_AMD, 74 .product = PCI_PRODUCT_AMD_F17_9X_CCP, 75 }, 76 }; 77 78 static const struct amdccp_pci_product * 79 amdccp_pci_lookup(const struct pci_attach_args * const pa) 80 { 81 unsigned int i; 82 83 for (i = 0; i < __arraycount(amdccp_pci_products); i++) { 84 if (PCI_VENDOR(pa->pa_id) == amdccp_pci_products[i].vendor && 85 PCI_PRODUCT(pa->pa_id) == amdccp_pci_products[i].product) { 86 return &amdccp_pci_products[i]; 87 } 88 } 89 return NULL; 90 } 91 92 static int 93 amdccp_pci_match(device_t parent, cfdata_t cf, void *aux) 94 { 95 const struct pci_attach_args * const pa = aux; 96 97 return amdccp_pci_lookup(pa) != NULL; 98 } 99 100 static void 101 amdccp_pci_attach(device_t parent, device_t self, void *aux) 102 { 103 struct amdccp_softc * const sc = device_private(self); 104 const struct pci_attach_args * const pa = aux; 105 pcireg_t type; 106 107 sc->sc_dev = self; 108 109 aprint_naive("\n"); 110 aprint_normal(": AMD Cryptographic Coprocessor\n"); 111 112 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, AMDCCP_MEM_BAR); 113 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM) { 114 aprint_error_dev(self, "expected MEM register, got IO\n"); 115 return; 116 } 117 118 if (pci_mapreg_map(pa, AMDCCP_MEM_BAR, type, 0, 119 &sc->sc_bst, &sc->sc_bsh, NULL, NULL) != 0) { 120 aprint_error_dev(self, "unable to map device registers\n"); 121 return; 122 } 123 124 amdccp_common_attach(sc); 125 pmf_device_register(self, NULL, NULL); 126 } 127