xref: /titanic_51/usr/src/uts/sun4v/io/px/px_libhv.c (revision 9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bb)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/sysmacros.h>
28 #include <sys/ddi.h>
29 #include <sys/async.h>
30 #include <sys/sunddi.h>
31 #include <sys/ddifm.h>
32 #include <sys/fm/protocol.h>
33 #include <sys/vmem.h>
34 #include <sys/intr.h>
35 #include <sys/ivintr.h>
36 #include <sys/errno.h>
37 #include <sys/hypervisor_api.h>
38 #include <sys/hsvc.h>
39 #include <px_obj.h>
40 #include <sys/machsystm.h>
41 #include "px_lib4v.h"
42 
43 #define	MPS_SET		0
44 #define	MPS_GET		1
45 
46 static uint64_t hvio_rp_mps(devhandle_t dev_hdl, pci_device_t bdf, int32_t *mps,
47 	int op);
48 
49 uint64_t
50 hvio_get_rp_mps_cap(devhandle_t dev_hdl, pci_device_t bdf, int32_t *mps_cap)
51 {
52 	return (hvio_rp_mps(dev_hdl, bdf, mps_cap, MPS_GET));
53 }
54 
55 uint64_t
56 hvio_set_rp_mps(devhandle_t dev_hdl, pci_device_t bdf, int32_t mps)
57 {
58 	return (hvio_rp_mps(dev_hdl, bdf, &mps, MPS_SET));
59 }
60 
61 uint64_t
62 hvio_rp_mps(devhandle_t dev_hdl, pci_device_t bdf, int32_t *mps, int op)
63 {
64 	uint64_t	data, hdr, hdr_next_ptr, hdr_cap_id;
65 	uint16_t	offset = PCI_CONF_STAT;
66 	int		deadcount = 0;
67 	pci_cfg_data_t	dataw;
68 
69 	if ((hvio_config_get(dev_hdl, bdf, PCI_CONF_VENID, 4,
70 	    (pci_cfg_data_t *)&data)) != H_EOK)
71 		return (H_ENOACCESS);
72 
73 	if ((data & 0xffff) != 0x108e)
74 		return (H_ENOACCESS);
75 
76 	if ((hvio_config_get(dev_hdl, bdf, PCI_CONF_COMM, 4,
77 	    (pci_cfg_data_t *)&hdr)) != H_EOK)
78 		return (H_ENOACCESS);
79 
80 	if (!(hdr & (PCI_STAT_CAP << 16)))
81 		return (H_ENOACCESS);
82 
83 	(void) hvio_config_get(dev_hdl, bdf, PCI_CONF_CAP_PTR, 4,
84 	    (pci_cfg_data_t *)&hdr);
85 
86 	hdr_next_ptr = hdr & 0xFF;
87 	hdr_cap_id = 0;
88 
89 	while ((hdr_next_ptr != PCI_CAP_NEXT_PTR_NULL) &&
90 	    (hdr_cap_id != PCI_CAP_ID_PCI_E)) {
91 
92 		offset = hdr_next_ptr;
93 
94 		if (hdr_next_ptr < 0x40)
95 			break;
96 
97 		(void) hvio_config_get(dev_hdl, bdf, hdr_next_ptr, 4,
98 		    (pci_cfg_data_t *)&hdr);
99 
100 		hdr_next_ptr = (hdr >> 8) & 0xFF;
101 		hdr_cap_id = hdr & 0xFF;
102 
103 		if (deadcount++ > 100)
104 			break;
105 	}
106 
107 	if (hdr_cap_id != PCI_CAP_ID_PCI_E)
108 		return (H_ENOACCESS);
109 
110 	if (op == MPS_SET) {
111 
112 		/* Write the MPS */
113 
114 		(void) hvio_config_get(dev_hdl, bdf, offset + PCIE_DEVCTL,
115 		    4, (pci_cfg_data_t *)&data);
116 
117 		data = (data & 0xffffff1f) | (*mps << 5);
118 
119 		dataw.qw = data;
120 
121 		(void) hvio_config_put(dev_hdl, bdf, offset + PCIE_DEVCTL,
122 		    4, dataw);
123 	} else {
124 
125 		/* Read the MPS Capabilities */
126 
127 		(void) hvio_config_get(dev_hdl, bdf, offset + PCIE_DEVCAP,
128 		    4, (pci_cfg_data_t *)&data);
129 
130 		*mps = data & 0x7;
131 	}
132 
133 	return (H_EOK);
134 }
135