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#include <sys/ddi.h>
28#include <sys/sunddi.h>
29
30#include <mcamd_pcicfg.h>
31#include <sys/pci_cfgspace.h>
32
33struct _mc_pcicfg_hdl {
34	mc_t *cfh_mc;
35	enum mc_funcnum cfh_func;
36	ddi_acc_handle_t cfh_hdl;
37};
38
39static int
40mccfgsetup(struct _mc_pcicfg_hdl *hdlp, mc_t *mc, enum mc_funcnum func)
41{
42	hdlp->cfh_mc = mc;
43	hdlp->cfh_func = func;
44
45	if (mc->mc_funcs[func].mcf_devi == NULL)
46		return (DDI_FAILURE);
47
48	if (pci_config_setup(mc->mc_funcs[func].mcf_devi, &hdlp->cfh_hdl) !=
49	    DDI_SUCCESS)
50		return (DDI_FAILURE);
51
52	return (DDI_SUCCESS);
53}
54
55int
56mc_pcicfg_setup(mc_t *mc, enum mc_funcnum func, mc_pcicfg_hdl_t *cookiep)
57{
58	struct _mc_pcicfg_hdl *hdlp;
59
60	*cookiep = hdlp = kmem_alloc(sizeof (struct _mc_pcicfg_hdl), KM_SLEEP);
61
62	if (mccfgsetup(hdlp, mc, func) == DDI_FAILURE) {
63		kmem_free(hdlp, sizeof (*hdlp));
64		return (DDI_FAILURE);
65	}
66
67	return (DDI_SUCCESS);
68}
69
70void
71mc_pcicfg_teardown(mc_pcicfg_hdl_t cookie)
72{
73	struct _mc_pcicfg_hdl *hdlp = cookie;
74
75	pci_config_teardown(&hdlp->cfh_hdl);
76	kmem_free(hdlp, sizeof (*hdlp));
77}
78
79uint32_t
80mc_pcicfg_get32(mc_pcicfg_hdl_t cookie, off_t offset)
81{
82	struct _mc_pcicfg_hdl *hdlp = cookie;
83
84	return (pci_config_get32(hdlp->cfh_hdl, offset));
85}
86
87void
88mc_pcicfg_put32(mc_pcicfg_hdl_t cookie, off_t offset, uint32_t val)
89{
90	struct _mc_pcicfg_hdl *hdlp = cookie;
91
92	pci_config_put32(hdlp->cfh_hdl, offset, val);
93}
94
95uint32_t
96mc_pcicfg_get32_nohdl(mc_t *mc, enum mc_funcnum func, off_t offset)
97{
98	return ((*pci_getl_func)(0, MC_AMD_DEV_OFFSET + mc->mc_props.mcp_num,
99	    func, offset));
100}
101
102void
103mc_pcicfg_put32_nohdl(mc_t *mc, enum mc_funcnum func, off_t offset,
104    uint32_t val)
105{
106	(*pci_putl_func)(0, MC_AMD_DEV_OFFSET + mc->mc_props.mcp_num,
107	    func, offset, val);
108}
109