xref: /netbsd-src/sys/arch/x86/pci/imcsmb/imc.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /* $NetBSD: imc.c,v 1.3 2021/04/24 23:36:51 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Goyette
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 /*-
33  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
34  *
35  * Authors: Joe Kloss; Ravi Pokala (rpokala@freebsd.org)
36  *
37  * Copyright (c) 2017-2018 Panasas
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  */
61 
62 /*
63  * Driver to expose the SMBus controllers in Intel's Integrated
64  * Memory Controllers in certain CPUs.
65  */
66 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: imc.c,v 1.3 2021/04/24 23:36:51 thorpej Exp $");
69 
70 #include <sys/param.h>
71 #include <sys/kernel.h>
72 #include <sys/module.h>
73 #include <sys/errno.h>
74 #include <sys/mutex.h>
75 #include <sys/bus.h>
76 
77 #include <dev/pci/pcidevs.h>
78 #include <dev/pci/pcivar.h>
79 #include <dev/pci/pcireg.h>
80 
81 #include "imcsmb_reg.h"
82 #include "imcsmb_var.h"
83 
84 #include "ioconf.h"
85 
86 /* (Sandy,Ivy)bridge-Xeon and (Has,Broad)well-Xeon CPUs contain one or two
87  * "Integrated Memory Controllers" (iMCs), and each iMC contains two separate
88  * SMBus controllers. These are used for reading SPD data from the DIMMs, and
89  * for reading the "Thermal Sensor on DIMM" (TSODs). The iMC SMBus controllers
90  * are very simple devices, and have limited functionality compared to
91  * full-fledged SMBus controllers, like the one in Intel ICHs and PCHs.
92  *
93  * The publicly available documentation for the iMC SMBus controllers can be
94  * found in the CPU datasheets for (Sandy,Ivy)bridge-Xeon and
95  * (Has,broad)well-Xeon, respectively:
96  *
97  * https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/
98  *      Sandybridge     xeon-e5-1600-2600-vol-2-datasheet.pdf
99  *      Ivybridge       xeon-e5-v2-datasheet-vol-2.pdf
100  *      Haswell         xeon-e5-v3-datasheet-vol-2.pdf
101  *      Broadwell       xeon-e5-v4-datasheet-vol-2.pdf
102  *
103  * Another useful resource is the Linux driver. It is not in the main tree.
104  *
105  * https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg840043.html
106  *
107  * The iMC SMBus controllers do not support interrupts (thus, they must be
108  * polled for IO completion). All of the iMC registers are in PCI configuration
109  * space; there is no support for PIO or MMIO. As a result, this driver does
110  * not need to perform and newbus resource manipulation.
111  *
112  * Because there are multiple SMBus controllers sharing the same PCI device,
113  * this driver is actually *two* drivers:
114  *
115  * - "imcsmb" is an smbus(4)-compliant SMBus controller driver
116  *
117  * - "imcsmb_pci" recognizes the PCI device and assigns the appropriate set of
118  *    PCI config registers to a specific "imcsmb" instance.
119  */
120 
121 /* Depending on the motherboard and firmware, the TSODs might be polled by
122  * firmware. Therefore, when this driver accesses these SMBus controllers, the
123  * firmware polling must be disabled as part of requesting the bus, and
124  * re-enabled when releasing the bus. Unfortunately, the details of how to do
125  * this are vendor-specific. Contact your motherboard vendor to get the
126  * information you need to do proper implementations.
127  *
128  * For NVDIMMs which conform to the ACPI "NFIT" standard, the ACPI firmware
129  * manages the NVDIMM; for those which pre-date the standard, the operating
130  * system interacts with the NVDIMM controller using a vendor-proprietary API
131  * over the SMBus. In that case, the NVDIMM driver would be an SMBus slave
132  * device driver, and would interface with the hardware via an SMBus controller
133  * driver such as this one.
134  */
135 
136 /* PCIe device IDs for (Sandy,Ivy)bridge)-Xeon and (Has,Broad)well-Xeon */
137 
138 #define IMCSMB_PCI_DEV_ID_IMC0_SBX	0x3ca8
139 #define IMCSMB_PCI_DEV_ID_IMC0_IBX	0x0ea8
140 #define IMCSMB_PCI_DEV_ID_IMC0_HSX	PCI_PRODUCT_INTEL_XE5_V3_IMC0_MAIN
141 #define IMCSMB_PCI_DEV_ID_IMC0_BDX	PCI_PRODUCT_INTEL_XEOND_MEM_0_TTR_1
142 
143 /* (Sandy,Ivy)bridge-Xeon only have a single memory controller per socket */
144 
145 #define IMCSMB_PCI_DEV_ID_IMC1_HSX	PCI_PRODUCT_INTEL_XE5_V3_IMC1_MAIN
146 #define IMCSMB_PCI_DEV_ID_IMC1_BDX	PCI_PRODUCT_INTEL_COREI76K_IMC_0
147 
148 /* There are two SMBus controllers in each device. These define the registers
149  * for each of these devices.
150  */
151 static struct imcsmb_reg_set imcsmb_regs[] = {
152 	{
153 		.smb_stat = IMCSMB_REG_STATUS0,
154 		.smb_cmd  = IMCSMB_REG_COMMAND0,
155 		.smb_cntl = IMCSMB_REG_CONTROL0
156 	},
157 	{
158 		.smb_stat = IMCSMB_REG_STATUS1,
159 		.smb_cmd  = IMCSMB_REG_COMMAND1,
160 		.smb_cntl = IMCSMB_REG_CONTROL1
161 	},
162 };
163 
164 static struct imcsmb_pci_device {
165 	uint16_t	id;
166 	const char	*name;
167 } imcsmb_pci_devices[] = {
168 	{IMCSMB_PCI_DEV_ID_IMC0_SBX,
169 	    "Intel Sandybridge Xeon iMC 0 SMBus controllers"	},
170 	{IMCSMB_PCI_DEV_ID_IMC0_IBX,
171 	    "Intel Ivybridge Xeon iMC 0 SMBus controllers"	},
172 	{IMCSMB_PCI_DEV_ID_IMC0_HSX,
173 	    "Intel Haswell Xeon iMC 0 SMBus controllers"	},
174 	{IMCSMB_PCI_DEV_ID_IMC1_HSX,
175 	    "Intel Haswell Xeon iMC 1 SMBus controllers"	},
176 	{IMCSMB_PCI_DEV_ID_IMC0_BDX,
177 	    "Intel Broadwell Xeon iMC 0 SMBus controllers"	},
178 	{IMCSMB_PCI_DEV_ID_IMC1_BDX,
179 	    "Intel Broadwell Xeon iMC 1 SMBus controllers"	},
180 	{0, NULL},
181 };
182 
183 /* Device methods. */
184 static void imc_attach(device_t, device_t, void *);
185 static int  imc_rescan(device_t, const char *, const int *);
186 static int  imc_detach(device_t, int);
187 static int  imc_probe(device_t, cfdata_t, void *);
188 static void imc_chdet(device_t, device_t);
189 
190 CFATTACH_DECL3_NEW(imc, sizeof(struct imc_softc),
191     imc_probe, imc_attach, imc_detach, NULL, imc_rescan, imc_chdet, 0);
192 
193 /**
194  * device_attach() method. Set up the PCI device's softc, then explicitly create
195  * children for the actual imcsmbX controllers. Set up the child's ivars to
196  * point to the proper set of the PCI device's config registers. Finally, probe
197  * and attach anything which might be downstream.
198  *
199  * @author Joe Kloss, rpokala
200  *
201  * @param[in,out] dev
202  *      Device being attached.
203  */
204 static void
205 imc_attach(device_t parent, device_t self, void *aux)
206 {
207 	struct imc_softc *sc = device_private(self);
208 	struct pci_attach_args *pa = aux;
209 	int i;
210 
211 	sc->sc_dev = self;
212 	sc->sc_pci_tag = pa->pa_tag;
213 	sc->sc_pci_chipset_tag = pa->pa_pc;
214 
215 	pci_aprint_devinfo(pa, NULL);
216 
217 	for (i = 0; imcsmb_pci_devices[i].id != 0; i++) {
218 		if (PCI_PRODUCT(pa->pa_id) == imcsmb_pci_devices[i].id) {
219 			aprint_normal_dev(self, "%s\n",
220 			    imcsmb_pci_devices[i].name);
221 			break;
222 		}
223 	}
224 
225 	if (!pmf_device_register(self, NULL, NULL))
226 		aprint_error_dev(self, "couldn't establish power handler\n");
227 
228 	imc_rescan(self, NULL, NULL);
229 }
230 
231 /* Create the imcsmbX children */
232 
233 static int
234 imc_rescan(device_t self, const char *ifattr, const int *locs)
235 {
236 	struct imc_softc *sc = device_private(self);
237 	struct imc_attach_args imca;
238 	int unit;
239 
240 	for (unit = 0; unit < 2; unit++) {
241 		if (sc->sc_smbchild[unit] != NULL)
242 			continue;
243 
244 		imca.ia_unit = unit;
245 		imca.ia_regs = &imcsmb_regs[unit];
246 		imca.ia_pci_tag = sc->sc_pci_tag;
247 		imca.ia_pci_chipset_tag = sc->sc_pci_chipset_tag;
248 		sc->sc_smbchild[unit] =
249 		    config_found(self, &imca, NULL, CFARG_EOL);
250 	}
251 
252 	return 0;
253 }
254 
255 /*
256  * device_detach() method. attach() didn't do any allocations, so there's
257  * nothing special needed
258  */
259 static int
260 imc_detach(device_t self, int flags)
261 {
262 	struct imc_softc *sc = device_private(self);
263 	int i, error;
264 
265 	for (i = 0; i < 2; i++) {
266 		if (sc->sc_smbchild[i] != NULL) {
267 			error = config_detach(sc->sc_smbchild[i], flags);
268 			if (error)
269 				return error;
270 		}
271 	}
272 
273 	pmf_device_deregister(self);
274 	return 0;
275 }
276 
277 /**
278  * device_probe() method. Look for the right PCI vendor/device IDs.
279  *
280  * @author Joe Kloss, rpokala
281  *
282  * @param[in,out] dev
283  *      Device being probed.
284  */
285 static int
286 imc_probe(device_t dev, cfdata_t cf, void *aux)
287 {
288 	struct pci_attach_args *pa = aux;
289 
290 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
291 		switch(PCI_PRODUCT(pa->pa_id)) {
292 		case  PCI_PRODUCT_INTEL_COREI76K_IMC_0:
293 		case  PCI_PRODUCT_INTEL_XEOND_MEM_0_TTR_1:
294 		case  PCI_PRODUCT_INTEL_XE5_V3_IMC0_MAIN:
295 		case  PCI_PRODUCT_INTEL_XE5_V3_IMC1_MAIN:
296 		case  PCI_PRODUCT_INTEL_E5_IMC_TA:
297 		case  PCI_PRODUCT_INTEL_E5V2_IMC_TA:
298 			return 1;
299 		}
300 	}
301 	return 0;
302 }
303 
304 /*
305  * child_detach() method
306  */
307 static void
308 imc_chdet(device_t self, device_t child)
309 {
310 	struct imc_softc *sc = device_private(self);
311 	int unit;
312 
313 	for (unit = 0; unit < 2; unit++)
314 		if (sc->sc_smbchild[unit] == child)
315 			sc->sc_smbchild[unit] = NULL;
316 	return;
317 }
318 
319 /*
320  * bios/motherboard access control
321  *
322  * XXX
323  * If necessary, add the code here to prevent concurrent access to the
324  * IMC controllers.  The softc argument is for the imcsmb child device
325  * (for the specific i2cbus instance); if you need to disable all
326  * i2cbus instances on a given IMC (or all instances on all IMCs), you
327  * may need to examine the softc's parent.
328  * XXX
329  */
330 
331 kmutex_t imc_access_mutex;
332 static int imc_access_count = 0;
333 
334 void
335 imc_callback(struct imcsmb_softc *sc, imc_bios_control action)
336 {
337 
338 	mutex_enter(&imc_access_mutex);
339 	switch (action) {
340 	case IMC_BIOS_ENABLE:
341 		imc_access_count--;
342 		if (imc_access_count == 0) {
343 			/*
344 			 * Insert motherboard-specific enable code here!
345 			 */
346 		}
347 		break;
348 	case IMC_BIOS_DISABLE:
349 		if (imc_access_count == 0) {
350 			/*
351 			 * Insert motherboard-specific disable code here!
352 			 */
353 		}
354 		imc_access_count++;
355 		break;
356 	}
357 	mutex_exit(&imc_access_mutex);
358 }
359 
360 MODULE(MODULE_CLASS_DRIVER, imc, "pci");
361 
362 #ifdef _MODULE
363 #include "ioconf.c"
364 #endif
365 
366 static int
367 imc_modcmd(modcmd_t cmd, void *opaque)
368 {
369 	int error = 0;
370 
371 	switch (cmd) {
372 	case MODULE_CMD_INIT:
373 		mutex_init(&imc_access_mutex, MUTEX_DEFAULT, IPL_NONE);
374 #ifdef _MODULE
375 		error = config_init_component(cfdriver_ioconf_imc,
376 		    cfattach_ioconf_imc, cfdata_ioconf_imc);
377 #endif
378 		break;
379 
380 	case MODULE_CMD_FINI:
381 #ifdef _MODULE
382 		error = config_fini_component(cfdriver_ioconf_imc,
383 		    cfattach_ioconf_imc, cfdata_ioconf_imc);
384 #endif
385 		if (error == 0)
386 			mutex_destroy(&imc_access_mutex);
387 		break;
388 	default:
389 		error = ENOTTY;
390 		break;
391 	}
392 
393 	return error;
394 }
395