summaryrefslogtreecommitdiff
path: root/usr/src/uts/i86pc/io/gfx_private/gfxp_pci.c
blob: 50ddba3f8ec1790cf35c1ed2a8437c9add507802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/debug.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/buf.h>
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/signal.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <sys/map.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/mman.h>
#include <sys/cred.h>
#include <sys/open.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/vnode.h>
#include <vm/page.h>
#include <vm/as.h>
#include <vm/hat.h>
#include <vm/seg.h>
#include <sys/ddi.h>
#include <sys/devops.h>
#include <sys/sunddi.h>
#include <sys/ddi_impldefs.h>
#include <sys/fs/snode.h>
#include <sys/pci.h>
#include <sys/modctl.h>
#include <sys/uio.h>
#include <sys/visual_io.h>
#include <sys/fbio.h>
#include <sys/ddidmareq.h>
#include <sys/kstat.h>
#include <sys/callb.h>
#include <sys/pci_cfgspace.h>
#include <sys/gfx_private.h>

typedef struct gfxp_pci_bsf {
	uint16_t	vendor;
	uint16_t	device;
	uint8_t		bus;
	uint8_t		slot;
	uint8_t		function;
	uint8_t		found;
	dev_info_t	*dip;
} gfxp_pci_bsf_t;

/* The use of pci_get?/put?_func() depends on misc/pci_autoconfig */

static int
gfxp_pci_get_bsf(dev_info_t *dip, uint8_t *bus, uint8_t *dev, uint8_t *func)
{
	pci_regspec_t   *pci_rp;
	uint32_t	length;
	int	rc;

	/* get "reg" property */
	rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
		DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
		(uint_t *)&length);
	if ((rc != DDI_SUCCESS) || (length <
			(sizeof (pci_regspec_t) / sizeof (int)))) {
		return (DDI_FAILURE);
	}

	*bus = PCI_REG_BUS_G(pci_rp->pci_phys_hi);
	*dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
	*func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);

	/*
	 * free the memory allocated by ddi_prop_lookup_int_array().
	 */
	ddi_prop_free(pci_rp);

	return (DDI_SUCCESS);
}

static int
gfxp_pci_find_bsf(dev_info_t *dip, void *arg)
{
	int	rc;
	uint8_t bus, dev, func;
	gfxp_pci_bsf_t    *pci_bsf;
	int vendor_id, device_id, class_code;

	/*
	 * Look for vendor-id, device-id, class-code to verify
	 * this is some type of PCI child node.
	 */
	vendor_id = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
				"vendor-id", -1);
	device_id = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
				"device-id", -1);
	class_code = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
				"class-code", -1);
	if ((vendor_id == -1) || (device_id == -1) || (class_code == -1)) {
		return (DDI_WALK_CONTINUE);
	}

	if (gfxp_pci_get_bsf(dip, &bus, &dev, &func) != DDI_SUCCESS)
		return (DDI_WALK_TERMINATE);

	pci_bsf = (gfxp_pci_bsf_t *)arg;

	if ((bus == pci_bsf->bus) && (dev == pci_bsf->slot) &&
		(func == pci_bsf->function)) {
		pci_bsf->dip = dip;
		pci_bsf->vendor = vendor_id;
		pci_bsf->device = device_id;
		pci_bsf->found = 1;
		rc = DDI_WALK_TERMINATE;
	} else {
		rc = DDI_WALK_CONTINUE;
	}

	return (rc);
}

gfxp_acc_handle_t
gfxp_pci_init_handle(uint8_t bus, uint8_t slot, uint8_t function,
	uint16_t *vendor, uint16_t *device)
{
	dev_info_t	*dip;
	gfxp_pci_bsf_t	*pci_bsf;

	/*
	 * Find a PCI device based on its address, and return a unique handle
	 * to be used in subsequent calls to read from or write to the config
	 * space of this device.
	 */

	if ((pci_bsf = kmem_zalloc(sizeof (gfxp_pci_bsf_t), KM_SLEEP))
			== NULL) {
		return (NULL);
	}

	pci_bsf->bus = bus;
	pci_bsf->slot = slot;
	pci_bsf->function = function;

	ddi_walk_devs(ddi_root_node(), gfxp_pci_find_bsf, pci_bsf);

	if (pci_bsf->found) {
		dip = pci_bsf->dip;

		if (vendor) *vendor = pci_bsf->vendor;
		if (device) *device = pci_bsf->device;
	} else {
		dip = NULL;
		if (vendor) *vendor = 0x0000;
		if (device) *device = 0x0000;
	}

	kmem_free(pci_bsf, sizeof (gfxp_pci_bsf_t));

	return ((gfxp_acc_handle_t)dip);
}

uint8_t
gfxp_pci_read_byte(gfxp_acc_handle_t handle, uint16_t offset)
{
	dev_info_t	*dip = (dev_info_t *)handle;
	uint8_t	val;
	uint8_t	bus, dev, func;

	if (dip == NULL)
		return ((uint8_t)~0);

	if (gfxp_pci_get_bsf(dip, &bus, &dev, &func) != DDI_SUCCESS)
		return ((uint8_t)~0);

	val = (*pci_getb_func)(bus, dev, func, offset);
	return (val);
}

uint16_t
gfxp_pci_read_word(gfxp_acc_handle_t handle, uint16_t offset)
{
	dev_info_t	*dip = (dev_info_t *)handle;
	uint16_t	val;
	uint8_t 	bus, dev, func;

	if (dip == NULL)
		return ((uint16_t)~0);

	if (gfxp_pci_get_bsf(dip, &bus, &dev, &func) != DDI_SUCCESS)
		return ((uint16_t)~0);

	val = (*pci_getw_func)(bus, dev, func, offset);
	return (val);
}

uint32_t
gfxp_pci_read_dword(gfxp_acc_handle_t handle, uint16_t offset)
{
	dev_info_t	*dip = (dev_info_t *)handle;
	uint32_t	val;
	uint8_t		bus, dev, func;

	if (dip == NULL)
		return ((uint32_t)~0);

	if (gfxp_pci_get_bsf(dip, &bus, &dev, &func) != DDI_SUCCESS)
		return ((uint32_t)~0);

	val = (*pci_getl_func)(bus, dev, func, offset);
	return (val);
}

void
gfxp_pci_write_byte(gfxp_acc_handle_t handle, uint16_t offset, uint8_t value)
{
	dev_info_t	*dip = (dev_info_t *)handle;
	uint8_t		bus, dev, func;

	if (dip == NULL)
		return;

	if (gfxp_pci_get_bsf(dip, &bus, &dev, &func) != DDI_SUCCESS)
		return;

	(*pci_putb_func)(bus, dev, func, offset, value);
}

void
gfxp_pci_write_word(gfxp_acc_handle_t handle, uint16_t offset, uint16_t value)
{
	dev_info_t	*dip = (dev_info_t *)handle;
	uint8_t		bus, dev, func;

	if (dip == NULL)
		return;

	if (gfxp_pci_get_bsf(dip, &bus, &dev, &func) != DDI_SUCCESS)
		return;

	(*pci_putw_func)(bus, dev, func, offset, value);
}

void
gfxp_pci_write_dword(gfxp_acc_handle_t handle, uint16_t offset, uint32_t value)
{
	dev_info_t	*dip = (dev_info_t *)handle;
	uint8_t		bus, dev, func;

	if (dip == NULL)
		return;

	if (gfxp_pci_get_bsf(dip, &bus, &dev, &func) != DDI_SUCCESS)
		return;

	(*pci_putl_func)(bus, dev, func, offset, value);
}

static int
gfxp_pci_find_vd(dev_info_t *dip, void *arg)
{
	int		rc;
	gfxp_pci_bsf_t	*pci_bsf;
	int		vendor_id, device_id, class_code;

	/*
	 * Look for vendor-id, device-id, class-code to verify
	 * this is some type of PCI child node.
	 */
	vendor_id = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
			"vendor-id", -1);
	device_id = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
			"device-id", -1);
	class_code = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
			"class-code", -1);
	if ((vendor_id == -1) || (device_id == -1) || (class_code == -1)) {
		return (DDI_WALK_CONTINUE);
	}

	pci_bsf = (gfxp_pci_bsf_t *)arg;

	if ((vendor_id == pci_bsf->vendor) && (device_id == pci_bsf->device)) {
		pci_bsf->found = 1;
		rc = DDI_WALK_TERMINATE;
	} else {
		rc = DDI_WALK_CONTINUE;
	}

	return (rc);
}

int
gfxp_pci_device_present(uint16_t vendor, uint16_t device)
{
	gfxp_pci_bsf_t	*pci_bsf;
	int		rv;

	/*
	 * Find a PCI device based on its device and vendor id.
	 */

	if ((pci_bsf = kmem_zalloc(sizeof (gfxp_pci_bsf_t), KM_SLEEP)) == NULL)
	    return (0);

	pci_bsf->vendor = vendor;
	pci_bsf->device = device;
	ddi_walk_devs(ddi_root_node(), gfxp_pci_find_vd, pci_bsf);

	if (pci_bsf->found) {
		rv = 1;
	} else {
		rv = 0;
	}

	kmem_free(pci_bsf, sizeof (gfxp_pci_bsf_t));

	return (rv);
}