xref: /titanic_50/usr/src/uts/common/os/sunpci.c (revision 65b180883d21463716cc4a2c6f5708a76eb0731e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/sunndi.h>
31 #include <sys/sysmacros.h>
32 #include <sys/pci.h>
33 #include <sys/pcie.h>
34 #include <sys/pci_impl.h>
35 #include <sys/epm.h>
36 
37 int
38 pci_config_setup(dev_info_t *dip, ddi_acc_handle_t *handle)
39 {
40 	caddr_t	cfgaddr;
41 	ddi_device_acc_attr_t attr;
42 
43 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
44 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
45 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
46 
47 	/* Check for fault management capabilities */
48 	if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(dip))) {
49 		attr.devacc_attr_version = DDI_DEVICE_ATTR_V1;
50 		attr.devacc_attr_access = DDI_FLAGERR_ACC;
51 	}
52 
53 	return (ddi_regs_map_setup(dip, 0, &cfgaddr, 0, 0, &attr, handle));
54 }
55 
56 void
57 pci_config_teardown(ddi_acc_handle_t *handle)
58 {
59 	ddi_regs_map_free(handle);
60 }
61 
62 uint8_t
63 pci_config_get8(ddi_acc_handle_t handle, off_t offset)
64 {
65 	caddr_t	cfgaddr;
66 	ddi_acc_hdl_t *hp;
67 
68 	hp = impl_acc_hdl_get(handle);
69 	cfgaddr = hp->ah_addr + offset;
70 	return (ddi_get8(handle, (uint8_t *)cfgaddr));
71 }
72 
73 uint16_t
74 pci_config_get16(ddi_acc_handle_t handle, off_t offset)
75 {
76 	caddr_t	cfgaddr;
77 	ddi_acc_hdl_t *hp;
78 
79 	hp = impl_acc_hdl_get(handle);
80 	cfgaddr = hp->ah_addr + offset;
81 	return (ddi_get16(handle, (uint16_t *)cfgaddr));
82 }
83 
84 uint32_t
85 pci_config_get32(ddi_acc_handle_t handle, off_t offset)
86 {
87 	caddr_t	cfgaddr;
88 	ddi_acc_hdl_t *hp;
89 
90 	hp = impl_acc_hdl_get(handle);
91 	cfgaddr = hp->ah_addr + offset;
92 	return (ddi_get32(handle, (uint32_t *)cfgaddr));
93 }
94 
95 uint64_t
96 pci_config_get64(ddi_acc_handle_t handle, off_t offset)
97 {
98 	caddr_t	cfgaddr;
99 	ddi_acc_hdl_t *hp;
100 
101 	hp = impl_acc_hdl_get(handle);
102 	cfgaddr = hp->ah_addr + offset;
103 	return (ddi_get64(handle, (uint64_t *)cfgaddr));
104 }
105 
106 void
107 pci_config_put8(ddi_acc_handle_t handle, off_t offset, uint8_t value)
108 {
109 	caddr_t	cfgaddr;
110 	ddi_acc_hdl_t *hp;
111 
112 	hp = impl_acc_hdl_get(handle);
113 	cfgaddr = hp->ah_addr + offset;
114 	ddi_put8(handle, (uint8_t *)cfgaddr, value);
115 }
116 
117 void
118 pci_config_put16(ddi_acc_handle_t handle, off_t offset, uint16_t value)
119 {
120 	caddr_t	cfgaddr;
121 	ddi_acc_hdl_t *hp;
122 
123 	hp = impl_acc_hdl_get(handle);
124 	cfgaddr = hp->ah_addr + offset;
125 	ddi_put16(handle, (uint16_t *)cfgaddr, value);
126 }
127 
128 void
129 pci_config_put32(ddi_acc_handle_t handle, off_t offset, uint32_t value)
130 {
131 	caddr_t	cfgaddr;
132 	ddi_acc_hdl_t *hp;
133 
134 	hp = impl_acc_hdl_get(handle);
135 	cfgaddr = hp->ah_addr + offset;
136 	ddi_put32(handle, (uint32_t *)cfgaddr, value);
137 }
138 
139 void
140 pci_config_put64(ddi_acc_handle_t handle, off_t offset, uint64_t value)
141 {
142 	caddr_t	cfgaddr;
143 	ddi_acc_hdl_t *hp;
144 
145 	hp = impl_acc_hdl_get(handle);
146 	cfgaddr = hp->ah_addr + offset;
147 	ddi_put64(handle, (uint64_t *)cfgaddr, value);
148 }
149 
150 /*
151  * We need to separate the old interfaces from the new ones and leave them
152  * in here for a while. Previous versions of the OS defined the new interfaces
153  * to the old interfaces. This way we can fix things up so that we can
154  * eventually remove these interfaces.
155  * e.g. A 3rd party module/driver using pci_config_get8 and built against S10
156  * or earlier will actually have a reference to pci_config_getb in the binary.
157  */
158 #ifdef _ILP32
159 uint8_t
160 pci_config_getb(ddi_acc_handle_t handle, off_t offset)
161 {
162 	caddr_t	cfgaddr;
163 	ddi_acc_hdl_t *hp;
164 
165 	hp = impl_acc_hdl_get(handle);
166 	cfgaddr = hp->ah_addr + offset;
167 	return (ddi_get8(handle, (uint8_t *)cfgaddr));
168 }
169 
170 uint16_t
171 pci_config_getw(ddi_acc_handle_t handle, off_t offset)
172 {
173 	caddr_t	cfgaddr;
174 	ddi_acc_hdl_t *hp;
175 
176 	hp = impl_acc_hdl_get(handle);
177 	cfgaddr = hp->ah_addr + offset;
178 	return (ddi_get16(handle, (uint16_t *)cfgaddr));
179 }
180 
181 uint32_t
182 pci_config_getl(ddi_acc_handle_t handle, off_t offset)
183 {
184 	caddr_t	cfgaddr;
185 	ddi_acc_hdl_t *hp;
186 
187 	hp = impl_acc_hdl_get(handle);
188 	cfgaddr = hp->ah_addr + offset;
189 	return (ddi_get32(handle, (uint32_t *)cfgaddr));
190 }
191 
192 uint64_t
193 pci_config_getll(ddi_acc_handle_t handle, off_t offset)
194 {
195 	caddr_t	cfgaddr;
196 	ddi_acc_hdl_t *hp;
197 
198 	hp = impl_acc_hdl_get(handle);
199 	cfgaddr = hp->ah_addr + offset;
200 	return (ddi_get64(handle, (uint64_t *)cfgaddr));
201 }
202 
203 void
204 pci_config_putb(ddi_acc_handle_t handle, off_t offset, uint8_t value)
205 {
206 	caddr_t	cfgaddr;
207 	ddi_acc_hdl_t *hp;
208 
209 	hp = impl_acc_hdl_get(handle);
210 	cfgaddr = hp->ah_addr + offset;
211 	ddi_put8(handle, (uint8_t *)cfgaddr, value);
212 }
213 
214 void
215 pci_config_putw(ddi_acc_handle_t handle, off_t offset, uint16_t value)
216 {
217 	caddr_t	cfgaddr;
218 	ddi_acc_hdl_t *hp;
219 
220 	hp = impl_acc_hdl_get(handle);
221 	cfgaddr = hp->ah_addr + offset;
222 	ddi_put16(handle, (uint16_t *)cfgaddr, value);
223 }
224 
225 void
226 pci_config_putl(ddi_acc_handle_t handle, off_t offset, uint32_t value)
227 {
228 	caddr_t	cfgaddr;
229 	ddi_acc_hdl_t *hp;
230 
231 	hp = impl_acc_hdl_get(handle);
232 	cfgaddr = hp->ah_addr + offset;
233 	ddi_put32(handle, (uint32_t *)cfgaddr, value);
234 }
235 
236 void
237 pci_config_putll(ddi_acc_handle_t handle, off_t offset, uint64_t value)
238 {
239 	caddr_t	cfgaddr;
240 	ddi_acc_hdl_t *hp;
241 
242 	hp = impl_acc_hdl_get(handle);
243 	cfgaddr = hp->ah_addr + offset;
244 	ddi_put64(handle, (uint64_t *)cfgaddr, value);
245 }
246 #endif /* _ILP32 */
247 
248 /*ARGSUSED*/
249 int
250 pci_report_pmcap(dev_info_t *dip, int cap, void *arg)
251 {
252 	return (DDI_SUCCESS);
253 }
254 
255 /*
256  * Note about saving and restoring config space.
257  * PCI devices have only upto 256 bytes of config space while PCI Express
258  * devices can have upto 4k config space. In case of PCI Express device,
259  * we save all 4k config space and restore it even if it doesn't make use
260  * of all 4k. But some devices don't respond to reads to non-existent
261  * registers within the config space. To avoid any panics, we use ddi_peek
262  * to do the reads. A bit mask is used to indicate which words of the
263  * config space are accessible. While restoring the config space, only those
264  * readable words are restored. We do all this in 32 bit size words.
265  */
266 #define	INDEX_SHIFT		3
267 #define	BITMASK			0x7
268 
269 static uint32_t pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf,
270     pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp);
271 static void pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf,
272     pci_cap_save_desc_t *cap_descp, uint32_t elements);
273 static uint32_t pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
274     uint32_t *regbuf, uint32_t nwords);
275 static uint32_t pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
276     uint32_t *regbuf, uint32_t notused);
277 static uint32_t pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
278     uint32_t *regbuf, uint32_t notused);
279 static uint32_t pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
280     uint32_t *regbuf, uint32_t notused);
281 static void pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
282     uint32_t *regbuf, uint32_t nwords);
283 static uint32_t cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf,
284     pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace);
285 static void pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf,
286     uint16_t pmcap_offset);
287 
288 /*
289  * Table below specifies the number of registers to be saved for each PCI
290  * capability. pci_generic_save saves the number of words specified in the
291  * table. Any special considerations will be taken care by the capability
292  * specific save function e.g. use pci_msi_save to save registers associated
293  * with MSI capability. PCI_UNKNOWN_SIZE indicates that number of registers
294  * to be saved is variable and will be determined by the specific save function.
295  * Currently we save/restore all the registers associated with the capability
296  * including read only registers. Regsiters are saved and restored in 32 bit
297  * size words.
298  */
299 static pci_cap_entry_t pci_cap_table[] = {
300 	{PCI_CAP_ID_PM, PCI_PMCAP_NDWORDS, pci_generic_save},
301 	{PCI_CAP_ID_AGP, PCI_AGP_NDWORDS, pci_generic_save},
302 	{PCI_CAP_ID_SLOT_ID, PCI_SLOTID_NDWORDS, pci_generic_save},
303 	{PCI_CAP_ID_MSI_X, PCI_MSIX_NDWORDS, pci_generic_save},
304 	{PCI_CAP_ID_MSI, PCI_CAP_SZUNKNOWN, pci_msi_save},
305 	{PCI_CAP_ID_PCIX, PCI_CAP_SZUNKNOWN, pci_pcix_save},
306 	{PCI_CAP_ID_PCI_E, PCI_CAP_SZUNKNOWN, pci_pcie_save},
307 	/*
308 	 * {PCI_CAP_ID_cPCI_CRC, 0, NULL},
309 	 * {PCI_CAP_ID_VPD, 0, NULL},
310 	 * {PCI_CAP_ID_cPCI_HS, 0, NULL},
311 	 * {PCI_CAP_ID_PCI_HOTPLUG, 0, NULL},
312 	 * {PCI_CAP_ID_AGP_8X, 0, NULL},
313 	 * {PCI_CAP_ID_SECURE_DEV, 0, NULL},
314 	 */
315 	{PCI_CAP_NEXT_PTR_NULL, 0, NULL}
316 };
317 
318 /*
319  * Save the configuration registers for cdip as a property
320  * so that it persists after detach/uninitchild.
321  */
322 int
323 pci_save_config_regs(dev_info_t *dip)
324 {
325 	peekpoke_ctlops_t cautacc_ctlops_arg;
326 	ddi_acc_handle_t confhdl;
327 	pci_config_header_state_t *chsp;
328 	pci_cap_save_desc_t *pci_cap_descp;
329 	int ret;
330 	uint32_t i, ncaps, nwords;
331 	uint32_t *regbuf, *p;
332 	uint8_t *maskbuf;
333 	size_t maskbufsz, regbufsz, capbufsz;
334 	ddi_acc_hdl_t *hp;
335 	off_t offset = 0;
336 	uint8_t cap_ptr, cap_id;
337 	int pcie = 0;
338 	PMD(PMD_SX, ("pci_save_config_regs %s:%d\n", ddi_driver_name(dip),
339 	    ddi_get_instance(dip)))
340 
341 	if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) {
342 		cmn_err(CE_WARN, "%s%d can't get config handle",
343 		    ddi_driver_name(dip), ddi_get_instance(dip));
344 
345 		return (DDI_FAILURE);
346 	}
347 	/*
348 	 * Determine if it is a pci express device. If it is, save entire
349 	 * 4k config space treating it as a array of 32 bit integers.
350 	 * If it is not, do it in a usual PCI way.
351 	 */
352 	cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR);
353 	/*
354 	 * Walk the capabilities searching for pci express capability
355 	 */
356 	while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) {
357 		cap_id = pci_config_get8(confhdl,
358 		    cap_ptr + PCI_CAP_ID);
359 		if (cap_id == PCI_CAP_ID_PCI_E) {
360 			pcie = 1;
361 			break;
362 		}
363 		cap_ptr = pci_config_get8(confhdl,
364 		    cap_ptr + PCI_CAP_NEXT_PTR);
365 	}
366 
367 	if (pcie) {
368 		/* PCI express device. Can have data in all 4k space */
369 		regbuf = (uint32_t *)kmem_zalloc((size_t)PCIE_CONF_HDR_SIZE,
370 		    KM_SLEEP);
371 		p = regbuf;
372 		/*
373 		 * Allocate space for mask.
374 		 * mask size is 128 bytes (4096 / 4 / 8 )
375 		 */
376 		maskbufsz = (size_t)((PCIE_CONF_HDR_SIZE/ sizeof (uint32_t)) >>
377 		    INDEX_SHIFT);
378 		maskbuf = (uint8_t *)kmem_zalloc(maskbufsz, KM_SLEEP);
379 		hp = impl_acc_hdl_get(confhdl);
380 		cautacc_ctlops_arg.size = sizeof (uint32_t);
381 		cautacc_ctlops_arg.handle = confhdl;
382 		cautacc_ctlops_arg.repcount = 1;
383 		cautacc_ctlops_arg.flags = 0;
384 		for (i = 0; i < (PCIE_CONF_HDR_SIZE / sizeof (uint32_t)); i++) {
385 			cautacc_ctlops_arg.dev_addr = (uintptr_t)(hp->ah_addr +
386 			    offset);
387 			cautacc_ctlops_arg.host_addr = (uintptr_t)p;
388 			ret = ddi_ctlops(dip, dip, DDI_CTLOPS_PEEK,
389 			    &cautacc_ctlops_arg, NULL);
390 			if (ret == DDI_SUCCESS) {
391 				/* it is readable register. set the bit */
392 				maskbuf[i >> INDEX_SHIFT] |=
393 				    (uint8_t)(1 << (i & BITMASK));
394 			}
395 			p++;
396 			offset += sizeof (uint32_t);
397 		}
398 
399 		if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip,
400 		    SAVED_CONFIG_REGS_MASK, (uchar_t *)maskbuf,
401 		    maskbufsz)) != DDI_PROP_SUCCESS) {
402 			cmn_err(CE_WARN, "couldn't create %s property while"
403 			    "saving config space for %s@%d\n",
404 			    SAVED_CONFIG_REGS_MASK, ddi_driver_name(dip),
405 			    ddi_get_instance(dip));
406 		} else if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE,
407 		    dip, SAVED_CONFIG_REGS, (uchar_t *)regbuf,
408 		    (size_t)PCIE_CONF_HDR_SIZE)) != DDI_PROP_SUCCESS) {
409 			(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
410 			    SAVED_CONFIG_REGS_MASK);
411 			cmn_err(CE_WARN, "%s%d can't update prop %s",
412 			    ddi_driver_name(dip), ddi_get_instance(dip),
413 			    SAVED_CONFIG_REGS);
414 		}
415 
416 		kmem_free(maskbuf, (size_t)maskbufsz);
417 		kmem_free(regbuf, (size_t)PCIE_CONF_HDR_SIZE);
418 	} else {
419 		regbuf = (uint32_t *)kmem_zalloc((size_t)PCI_CONF_HDR_SIZE,
420 		    KM_SLEEP);
421 		chsp = (pci_config_header_state_t *)regbuf;
422 
423 		chsp->chs_command = pci_config_get16(confhdl, PCI_CONF_COMM);
424 		chsp->chs_header_type =	pci_config_get8(confhdl,
425 		    PCI_CONF_HEADER);
426 		if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) ==
427 		    PCI_HEADER_ONE)
428 			chsp->chs_bridge_control =
429 			    pci_config_get16(confhdl, PCI_BCNF_BCNTRL);
430 		chsp->chs_cache_line_size = pci_config_get8(confhdl,
431 		    PCI_CONF_CACHE_LINESZ);
432 		chsp->chs_latency_timer = pci_config_get8(confhdl,
433 		    PCI_CONF_LATENCY_TIMER);
434 		if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) ==
435 		    PCI_HEADER_ONE) {
436 			chsp->chs_sec_latency_timer =
437 			    pci_config_get8(confhdl, PCI_BCNF_LATENCY_TIMER);
438 		}
439 
440 		chsp->chs_base0 = pci_config_get32(confhdl, PCI_CONF_BASE0);
441 		chsp->chs_base1 = pci_config_get32(confhdl, PCI_CONF_BASE1);
442 		chsp->chs_base2 = pci_config_get32(confhdl, PCI_CONF_BASE2);
443 		chsp->chs_base3 = pci_config_get32(confhdl, PCI_CONF_BASE3);
444 		chsp->chs_base4 = pci_config_get32(confhdl, PCI_CONF_BASE4);
445 		chsp->chs_base5 = pci_config_get32(confhdl, PCI_CONF_BASE5);
446 
447 		/*
448 		 * Allocate maximum space required for capability descriptions.
449 		 * The maximum number of capabilties saved is the number of
450 		 * capabilities listed in the pci_cap_table.
451 		 */
452 		ncaps = (sizeof (pci_cap_table) / sizeof (pci_cap_entry_t));
453 		capbufsz = ncaps * sizeof (pci_cap_save_desc_t);
454 		pci_cap_descp = (pci_cap_save_desc_t *)kmem_zalloc(
455 		    capbufsz, KM_SLEEP);
456 		p = (uint32_t *)((caddr_t)regbuf +
457 		    sizeof (pci_config_header_state_t));
458 		nwords = pci_save_caps(confhdl, p, pci_cap_descp, &ncaps);
459 		regbufsz = sizeof (pci_config_header_state_t) +
460 		    nwords * sizeof (uint32_t);
461 
462 		if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip,
463 		    SAVED_CONFIG_REGS, (uchar_t *)regbuf, regbufsz)) !=
464 		    DDI_PROP_SUCCESS) {
465 			cmn_err(CE_WARN, "%s%d can't update prop %s",
466 			    ddi_driver_name(dip), ddi_get_instance(dip),
467 			    SAVED_CONFIG_REGS);
468 		} else if (ncaps) {
469 			ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip,
470 			    SAVED_CONFIG_REGS_CAPINFO, (uchar_t *)pci_cap_descp,
471 			    ncaps * sizeof (pci_cap_save_desc_t));
472 			if (ret != DDI_PROP_SUCCESS)
473 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
474 				    SAVED_CONFIG_REGS);
475 		}
476 		kmem_free(regbuf, (size_t)PCI_CONF_HDR_SIZE);
477 		kmem_free(pci_cap_descp, capbufsz);
478 	}
479 	pci_config_teardown(&confhdl);
480 
481 	if (ret != DDI_PROP_SUCCESS)
482 		return (DDI_FAILURE);
483 
484 	return (DDI_SUCCESS);
485 }
486 
487 /*
488  * Saves registers associated with PCI capabilities.
489  * Returns number of 32 bit words saved.
490  * Number of capabilities saved is returned in ncapsp.
491  */
492 static uint32_t
493 pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf,
494     pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp)
495 {
496 	return (cap_walk_and_save(confhdl, regbuf, cap_descp, ncapsp, 0));
497 }
498 
499 static uint32_t
500 cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf,
501     pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace)
502 {
503 	pci_cap_entry_t *pci_cap_entp;
504 	uint16_t cap_id, offset;
505 	uint32_t words_saved = 0, nwords = 0;
506 	uint16_t cap_ptr = PCI_CAP_NEXT_PTR_NULL;
507 
508 	*ncapsp = 0;
509 	if (!xspace)
510 		cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR);
511 	/*
512 	 * Walk the capabilities
513 	 */
514 	while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) {
515 		cap_id = CAP_ID(confhdl, cap_ptr, xspace);
516 		/* Search for this cap id in our table */
517 		if (!xspace)
518 			pci_cap_entp = pci_cap_table;
519 		while (pci_cap_entp->cap_id != PCI_CAP_NEXT_PTR_NULL &&
520 		    pci_cap_entp->cap_id != cap_id)
521 			pci_cap_entp++;
522 
523 		offset = cap_ptr;
524 		cap_ptr = NEXT_CAP(confhdl, cap_ptr, xspace);
525 		/*
526 		 * If this cap id is not found in the table, there is nothing
527 		 * to save.
528 		 */
529 		if (pci_cap_entp->cap_id == PCI_CAP_NEXT_PTR_NULL)
530 			continue;
531 		if (pci_cap_entp->cap_save_func) {
532 			if ((nwords = pci_cap_entp->cap_save_func(confhdl,
533 			    offset, regbuf, pci_cap_entp->cap_ndwords))) {
534 				cap_descp->cap_nregs = nwords;
535 				cap_descp->cap_offset = offset;
536 				cap_descp->cap_id = cap_id;
537 				regbuf += nwords;
538 				cap_descp++;
539 				words_saved += nwords;
540 				(*ncapsp)++;
541 			}
542 		}
543 
544 	}
545 	return (words_saved);
546 }
547 
548 static void
549 pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
550     uint32_t *regbuf, uint32_t nwords)
551 {
552 	int i;
553 
554 	for (i = 0; i < nwords; i++) {
555 		*regbuf = pci_config_get32(confhdl, cap_ptr);
556 		regbuf++;
557 		cap_ptr += 4;
558 	}
559 }
560 
561 static uint32_t
562 pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf,
563     uint32_t nwords)
564 {
565 	pci_fill_buf(confhdl, cap_ptr, regbuf, nwords);
566 	return (nwords);
567 }
568 
569 /*ARGSUSED*/
570 static uint32_t
571 pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf,
572     uint32_t notused)
573 {
574 	uint32_t nwords = PCI_MSI_MIN_WORDS;
575 	uint16_t msi_ctrl;
576 
577 	/* Figure out how many registers to be saved */
578 	msi_ctrl = pci_config_get16(confhdl, cap_ptr + PCI_MSI_CTRL);
579 	/* If 64 bit address capable add one word */
580 	if (msi_ctrl & PCI_MSI_64BIT_MASK)
581 		nwords++;
582 	/* If per vector masking capable, add two more words */
583 	if (msi_ctrl & PCI_MSI_PVM_MASK)
584 		nwords += 2;
585 	pci_fill_buf(confhdl, cap_ptr, regbuf, nwords);
586 
587 	return (nwords);
588 }
589 
590 /*ARGSUSED*/
591 static uint32_t
592 pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf,
593     uint32_t notused)
594 {
595 	uint32_t nwords = PCI_PCIX_MIN_WORDS;
596 	uint16_t pcix_command;
597 
598 	/* Figure out how many registers to be saved */
599 	pcix_command = pci_config_get16(confhdl, cap_ptr + PCI_PCIX_COMMAND);
600 	/* If it is version 1 or version 2, add 4 words */
601 	if (((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_1) ||
602 	    ((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_2))
603 		nwords += 4;
604 	pci_fill_buf(confhdl, cap_ptr, regbuf, nwords);
605 
606 	return (nwords);
607 }
608 
609 /*ARGSUSED*/
610 static uint32_t
611 pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf,
612     uint32_t notused)
613 {
614 	return (0);
615 }
616 
617 static void
618 pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf,
619     uint16_t pmcap_offset)
620 {
621 	uint16_t pmcsr;
622 	uint16_t pmcsr_offset = pmcap_offset + PCI_PMCSR;
623 	uint32_t *saved_pmcsrp = (uint32_t *)((caddr_t)regbuf + PCI_PMCSR);
624 
625 	/*
626 	 * Copy the power state bits from the PMCSR to our saved copy.
627 	 * This is to make sure that we don't change the D state when
628 	 * we restore config space of the device.
629 	 */
630 	pmcsr = pci_config_get16(confhdl, pmcsr_offset);
631 	(*saved_pmcsrp) &= ~PCI_PMCSR_STATE_MASK;
632 	(*saved_pmcsrp) |= (pmcsr & PCI_PMCSR_STATE_MASK);
633 }
634 
635 static void
636 pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf,
637     pci_cap_save_desc_t *cap_descp, uint32_t elements)
638 {
639 	int i, j;
640 	uint16_t offset;
641 
642 	for (i = 0; i < (elements / sizeof (pci_cap_save_desc_t)); i++) {
643 		offset = cap_descp->cap_offset;
644 		if (cap_descp->cap_id == PCI_CAP_ID_PM)
645 			pci_pmcap_check(confhdl, regbuf, offset);
646 		for (j = 0; j < cap_descp->cap_nregs; j++) {
647 			pci_config_put32(confhdl, offset, *regbuf);
648 			regbuf++;
649 			offset += 4;
650 		}
651 		cap_descp++;
652 	}
653 }
654 
655 /*
656  * Restore config_regs from a single devinfo node.
657  */
658 int
659 pci_restore_config_regs(dev_info_t *dip)
660 {
661 	ddi_acc_handle_t confhdl;
662 	pci_config_header_state_t *chs_p;
663 	pci_cap_save_desc_t *cap_descp;
664 	uint32_t elements, i;
665 	uint8_t *maskbuf;
666 	uint32_t *regbuf, *p;
667 	off_t offset = 0;
668 
669 	if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) {
670 		cmn_err(CE_WARN, "%s%d can't get config handle",
671 		    ddi_driver_name(dip), ddi_get_instance(dip));
672 		return (DDI_FAILURE);
673 	}
674 
675 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
676 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS_MASK,
677 	    (uchar_t **)&maskbuf, &elements) == DDI_PROP_SUCCESS) {
678 
679 		if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
680 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS,
681 		    (uchar_t **)&regbuf, &elements) != DDI_PROP_SUCCESS) {
682 			goto restoreconfig_err;
683 		}
684 		ASSERT(elements == PCIE_CONF_HDR_SIZE);
685 		/* pcie device and has 4k config space saved */
686 		p = regbuf;
687 		for (i = 0; i < PCIE_CONF_HDR_SIZE / sizeof (uint32_t); i++) {
688 			/* If the word is readable then restore it */
689 			if (maskbuf[i >> INDEX_SHIFT] &
690 			    (uint8_t)(1 << (i & BITMASK)))
691 				pci_config_put32(confhdl, offset, *p);
692 			p++;
693 			offset += sizeof (uint32_t);
694 		}
695 		ddi_prop_free(regbuf);
696 		ddi_prop_free(maskbuf);
697 		if (ndi_prop_remove(DDI_DEV_T_NONE, dip,
698 		    SAVED_CONFIG_REGS_MASK) != DDI_PROP_SUCCESS) {
699 			cmn_err(CE_WARN, "%s%d can't remove prop %s",
700 			    ddi_driver_name(dip), ddi_get_instance(dip),
701 			    SAVED_CONFIG_REGS_MASK);
702 		}
703 	} else {
704 		if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
705 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS,
706 		    (uchar_t **)&regbuf, &elements) != DDI_PROP_SUCCESS) {
707 
708 			pci_config_teardown(&confhdl);
709 			return (DDI_SUCCESS);
710 		}
711 
712 		chs_p = (pci_config_header_state_t *)regbuf;
713 		pci_config_put16(confhdl, PCI_CONF_COMM,
714 		    chs_p->chs_command);
715 		if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) ==
716 		    PCI_HEADER_ONE) {
717 			pci_config_put16(confhdl, PCI_BCNF_BCNTRL,
718 			    chs_p->chs_bridge_control);
719 		}
720 		pci_config_put8(confhdl, PCI_CONF_CACHE_LINESZ,
721 		    chs_p->chs_cache_line_size);
722 		pci_config_put8(confhdl, PCI_CONF_LATENCY_TIMER,
723 		    chs_p->chs_latency_timer);
724 		if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) ==
725 		    PCI_HEADER_ONE)
726 			pci_config_put8(confhdl, PCI_BCNF_LATENCY_TIMER,
727 			    chs_p->chs_sec_latency_timer);
728 
729 		pci_config_put32(confhdl, PCI_CONF_BASE0, chs_p->chs_base0);
730 		pci_config_put32(confhdl, PCI_CONF_BASE1, chs_p->chs_base1);
731 		pci_config_put32(confhdl, PCI_CONF_BASE2, chs_p->chs_base2);
732 		pci_config_put32(confhdl, PCI_CONF_BASE3, chs_p->chs_base3);
733 		pci_config_put32(confhdl, PCI_CONF_BASE4, chs_p->chs_base4);
734 		pci_config_put32(confhdl, PCI_CONF_BASE5, chs_p->chs_base5);
735 
736 		if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
737 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
738 		    SAVED_CONFIG_REGS_CAPINFO,
739 		    (uchar_t **)&cap_descp, &elements) == DDI_PROP_SUCCESS) {
740 			/*
741 			 * PCI capability related regsiters are saved.
742 			 * Restore them based on the description.
743 			 */
744 			p = (uint32_t *)((caddr_t)regbuf +
745 			    sizeof (pci_config_header_state_t));
746 			pci_restore_caps(confhdl, p, cap_descp, elements);
747 			ddi_prop_free(cap_descp);
748 		}
749 
750 		ddi_prop_free(regbuf);
751 	}
752 
753 	/*
754 	 * Make sure registers are flushed
755 	 */
756 	(void) pci_config_get32(confhdl, PCI_CONF_BASE5);
757 
758 
759 	if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS) !=
760 	    DDI_PROP_SUCCESS) {
761 		cmn_err(CE_WARN, "%s%d can't remove prop %s",
762 		    ddi_driver_name(dip), ddi_get_instance(dip),
763 		    SAVED_CONFIG_REGS);
764 	}
765 
766 	pci_config_teardown(&confhdl);
767 
768 	return (DDI_SUCCESS);
769 
770 restoreconfig_err:
771 	ddi_prop_free(maskbuf);
772 	if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS_MASK) !=
773 	    DDI_PROP_SUCCESS) {
774 		cmn_err(CE_WARN, "%s%d can't remove prop %s",
775 		    ddi_driver_name(dip), ddi_get_instance(dip),
776 		    SAVED_CONFIG_REGS_MASK);
777 	}
778 	pci_config_teardown(&confhdl);
779 	return (DDI_FAILURE);
780 }
781 
782 /*ARGSUSED*/
783 static int
784 pci_lookup_pmcap(dev_info_t *dip, ddi_acc_handle_t conf_hdl,
785 	uint16_t *pmcap_offsetp)
786 {
787 	uint8_t cap_ptr;
788 	uint8_t cap_id;
789 	uint8_t header_type;
790 	uint16_t status;
791 
792 	header_type = pci_config_get8(conf_hdl, PCI_CONF_HEADER);
793 	header_type &= PCI_HEADER_TYPE_M;
794 
795 	/* we don't deal with bridges, etc here */
796 	if (header_type != PCI_HEADER_ZERO) {
797 		return (DDI_FAILURE);
798 	}
799 
800 	status = pci_config_get16(conf_hdl, PCI_CONF_STAT);
801 	if ((status & PCI_STAT_CAP) == 0) {
802 		return (DDI_FAILURE);
803 	}
804 
805 	cap_ptr = pci_config_get8(conf_hdl, PCI_CONF_CAP_PTR);
806 
807 	/*
808 	 * Walk the capabilities searching for a PM entry.
809 	 */
810 	while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) {
811 		cap_id = pci_config_get8(conf_hdl, cap_ptr + PCI_CAP_ID);
812 		if (cap_id == PCI_CAP_ID_PM) {
813 			break;
814 		}
815 		cap_ptr = pci_config_get8(conf_hdl,
816 		    cap_ptr + PCI_CAP_NEXT_PTR);
817 	}
818 
819 	if (cap_ptr == PCI_CAP_NEXT_PTR_NULL) {
820 		return (DDI_FAILURE);
821 	}
822 	*pmcap_offsetp = cap_ptr;
823 	return (DDI_SUCCESS);
824 }
825 
826 /*
827  * Do common pci-specific suspend actions:
828  *  - enable wakeup if appropriate for the device
829  *  - put device in lowest D-state that supports wakeup, or D3 if none
830  *  - turn off bus mastering in control register
831  * For lack of per-dip storage (parent private date is pretty busy)
832  * we use properties to store the necessary context
833  * To avoid grotting through pci config space on every suspend,
834  * we leave the prop in existence after resume, cause we know that
835  * the detach framework code will dispose of it for us.
836  */
837 
838 typedef struct pci_pm_context {
839 	int		ppc_flags;
840 	uint16_t	ppc_cap_offset;	/* offset in config space to pm cap */
841 	uint16_t	ppc_pmcsr;	/* need this too */
842 	uint16_t	ppc_suspend_level;
843 } pci_pm_context_t;
844 
845 #define	SAVED_PM_CONTEXT	"pci-pm-context"
846 
847 /* values for ppc_flags	*/
848 #define	PPCF_NOPMCAP	1
849 
850 /*
851  * Handle pci-specific suspend processing
852  *   PM CSR and PCI CMD are saved by pci_save_config_regs().
853  *   If device can wake up system via PME, enable it to do so
854  *   Set device power level to lowest that can generate PME, or D3 if none can
855  *   Turn off bus master enable in pci command register
856  */
857 #if defined(__x86)
858 extern int acpi_ddi_setwake(dev_info_t *dip, int level);
859 #endif
860 
861 int
862 pci_post_suspend(dev_info_t *dip)
863 {
864 	pci_pm_context_t *p;
865 	uint16_t	pmcap, pmcsr, pcicmd;
866 	uint_t length;
867 	int ret;
868 	int fromprop = 1;	/* source of memory *p */
869 	ddi_acc_handle_t hdl;
870 
871 	PMD(PMD_SX, ("pci_post_suspend %s:%d\n",
872 	    ddi_driver_name(dip), ddi_get_instance(dip)))
873 
874 	if (pci_save_config_regs(dip) != DDI_SUCCESS) {
875 		return (DDI_FAILURE);
876 	}
877 
878 	if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) {
879 		return (DDI_FAILURE);
880 	}
881 
882 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
883 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
884 	    SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) {
885 		p = (pci_pm_context_t *)kmem_zalloc(sizeof (*p), KM_SLEEP);
886 		fromprop = 0;
887 		if (pci_lookup_pmcap(dip, hdl,
888 		    &p->ppc_cap_offset) != DDI_SUCCESS) {
889 			p->ppc_flags |= PPCF_NOPMCAP;
890 			ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip,
891 			    SAVED_PM_CONTEXT, (uchar_t *)p,
892 			    sizeof (pci_pm_context_t));
893 			if (ret != DDI_PROP_SUCCESS) {
894 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
895 				    SAVED_PM_CONTEXT);
896 				ret = DDI_FAILURE;
897 			} else {
898 				ret = DDI_SUCCESS;
899 			}
900 			kmem_free(p, sizeof (*p));
901 			pci_config_teardown(&hdl);
902 			return (DDI_SUCCESS);
903 		}
904 		/*
905 		 * Upon suspend, set the power level to the lowest that can
906 		 * wake the system.  If none can, then set to lowest.
907 		 * XXX later we will need to check policy to see if this
908 		 * XXX device has had wakeup disabled
909 		 */
910 		pmcap = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCAP);
911 		if ((pmcap & PCI_PMCAP_D3COLD_PME) != 0)
912 			p->ppc_suspend_level =
913 			    (PCI_PMCSR_PME_EN | PCI_PMCSR_D3HOT);
914 		else if ((pmcap & (PCI_PMCAP_D3HOT_PME | PCI_PMCAP_D2_PME)) !=
915 		    0)
916 			p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D2;
917 		else if ((pmcap & PCI_PMCAP_D1_PME) != 0)
918 			p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D1;
919 		else if ((pmcap & PCI_PMCAP_D0_PME) != 0)
920 			p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D0;
921 		else
922 			p->ppc_suspend_level = PCI_PMCSR_D3HOT;
923 
924 		/*
925 		 * we defer updating the property to catch the saved
926 		 * register values as well
927 		 */
928 	}
929 	/* If we set this in kmem_zalloc'd memory, we already returned above */
930 	if ((p->ppc_flags & PPCF_NOPMCAP) != 0) {
931 		ddi_prop_free(p);
932 		pci_config_teardown(&hdl);
933 		return (DDI_SUCCESS);
934 	}
935 
936 
937 	/*
938 	 * Turn off (Bus) Master Enable, since acpica will be turning off
939 	 * bus master aribitration
940 	 */
941 	pcicmd = pci_config_get16(hdl, PCI_CONF_COMM);
942 	pcicmd &= ~PCI_COMM_ME;
943 	pci_config_put16(hdl, PCI_CONF_COMM, pcicmd);
944 
945 	/*
946 	 * set pm csr
947 	 */
948 	pmcsr = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCSR);
949 	p->ppc_pmcsr = pmcsr;
950 	pmcsr &= (PCI_PMCSR_STATE_MASK);
951 	pmcsr |= (PCI_PMCSR_PME_STAT | p->ppc_suspend_level);
952 	pci_config_put16(hdl, p->ppc_cap_offset + PCI_PMCSR, pmcsr);
953 
954 #if defined(__x86)
955 	/*
956 	 * Arrange for platform wakeup enabling
957 	 */
958 	if ((p->ppc_suspend_level & PCI_PMCSR_PME_EN) != 0) {
959 		int retval;
960 
961 		retval = acpi_ddi_setwake(dip, 3);	/* XXX 3 for now */
962 		if (retval) {
963 			PMD(PMD_SX, ("pci_post_suspend, setwake %s@%s rets "
964 			    "%x\n", PM_NAME(dip), PM_ADDR(dip), retval));
965 		}
966 	}
967 #endif
968 
969 	/*
970 	 * Push out saved register values
971 	 */
972 	ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT,
973 	    (uchar_t *)p, sizeof (pci_pm_context_t));
974 	if (ret == DDI_PROP_SUCCESS) {
975 		if (fromprop)
976 			ddi_prop_free(p);
977 		else
978 			kmem_free(p, sizeof (*p));
979 		pci_config_teardown(&hdl);
980 		return (DDI_SUCCESS);
981 	}
982 	/* Failed; put things back the way we found them */
983 	(void) pci_restore_config_regs(dip);
984 	if (fromprop)
985 		ddi_prop_free(p);
986 	else
987 		kmem_free(p, sizeof (*p));
988 	(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT);
989 	pci_config_teardown(&hdl);
990 	return (DDI_FAILURE);
991 }
992 
993 /*
994  * The inverse of pci_post_suspend; handle pci-specific resume processing
995  *   First, turn device back on, then restore config space.
996  */
997 
998 int
999 pci_pre_resume(dev_info_t *dip)
1000 {
1001 	ddi_acc_handle_t hdl;
1002 	pci_pm_context_t *p;
1003 	/* E_FUNC_SET_NOT_USED */
1004 	uint16_t	pmcap, pmcsr;
1005 	int flags;
1006 	uint_t length;
1007 	clock_t drv_usectohz(clock_t microsecs);
1008 #if defined(__x86)
1009 	uint16_t	suspend_level;
1010 #endif
1011 
1012 	PMD(PMD_SX, ("pci_pre_resume %s:%d\n", ddi_driver_name(dip),
1013 	    ddi_get_instance(dip)))
1014 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
1015 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1016 	    SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) {
1017 		return (DDI_FAILURE);
1018 	}
1019 	flags = p->ppc_flags;
1020 	pmcap = p->ppc_cap_offset;
1021 	pmcsr = p->ppc_pmcsr;
1022 #if defined(__x86)
1023 	suspend_level = p->ppc_suspend_level;
1024 #endif
1025 	ddi_prop_free(p);
1026 	if ((flags & PPCF_NOPMCAP) != 0)
1027 		goto done;
1028 #if defined(__x86)
1029 	/*
1030 	 * Turn platform wake enable back off
1031 	 */
1032 	if ((suspend_level & PCI_PMCSR_PME_EN) != 0) {
1033 		int retval;
1034 
1035 		retval = acpi_ddi_setwake(dip, 0);	/* 0 for now */
1036 		if (retval) {
1037 			PMD(PMD_SX, ("pci_pre_resume, setwake %s@%s rets "
1038 			    "%x\n", PM_NAME(dip), PM_ADDR(dip), retval));
1039 		}
1040 	}
1041 #endif
1042 	if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) {
1043 		return (DDI_FAILURE);
1044 	}
1045 	pci_config_put16(hdl, pmcap + PCI_PMCSR, pmcsr);
1046 	delay(drv_usectohz(10000));	/* PCI PM spec D3->D0 (10ms) */
1047 	pci_config_teardown(&hdl);
1048 done:
1049 	(void) pci_restore_config_regs(dip);	/* fudges D-state! */
1050 	return (DDI_SUCCESS);
1051 }
1052