summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/drm/drm_pci.c
blob: 37a02a1d9590c9a7729ef9adef745f51fbbc256b (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
347
348
349
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/* BEGIN CSTYLED */
/**
 * \file drm_pci.h
 * \brief PCI consistent, DMA-accessible memory functions.
 *
 * \author Eric Anholt <anholt@FreeBSD.org>
 */

/*-
 * Copyright 2003 Eric Anholt.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**********************************************************************/
/** \name PCI memory */
/*@{*/
/* END CSTYLED */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "drmP.h"
#include <vm/seg_kmem.h>

#define	PCI_DEVICE(x)	(((x)>>11) & 0x1f)
#define	PCI_FUNCTION(x)	(((x) & 0x700) >> 8)
#define	PCI_BUS(x)	(((x) & 0xff0000) >> 16)

typedef struct drm_pci_resource {
	uint_t	regnum;
	unsigned long offset;
	unsigned long size;
} drm_pci_resource_t;

int
pci_get_info(drm_device_t *softstate, int *bus, int *slot, int *func)
{
	int *regs_list;
	uint_t nregs = 0;

	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, softstate->dip,
	    DDI_PROP_DONTPASS, "reg", (int **)&regs_list, &nregs)
	    != DDI_PROP_SUCCESS) {
		DRM_ERROR("pci_get_info: get pci function bus device failed");
		goto error;
	}
	*bus  = (int)PCI_BUS(regs_list[0]);
	*slot = (int)PCI_DEVICE(regs_list[0]);
	*func = (int)PCI_FUNCTION(regs_list[0]);

	if (nregs > 0) {
		ddi_prop_free(regs_list);
	}
	return (DDI_SUCCESS);
error:
	if (nregs > 0) {
		ddi_prop_free(regs_list);
	}
	return (DDI_FAILURE);
}

int
pci_get_irq(drm_device_t *statep)
{
	int irq;

	extern int drm_supp_get_irq(void *);

	irq = ddi_prop_get_int(DDI_DEV_T_ANY,
	    statep->dip, DDI_PROP_DONTPASS, "interrupts", -1);

	if (irq > 0) {
		irq = drm_supp_get_irq(statep->drm_handle);
	}

	return (irq);
}

int
pci_get_vendor(drm_device_t *statep)
{
	int vendorid;

	vendorid = ddi_prop_get_int(DDI_DEV_T_ANY,
	    statep->dip, DDI_PROP_DONTPASS, "vendor-id", 0);

	return (vendorid);
}

int
pci_get_device(drm_device_t *statep)
{
	int deviceid;

	deviceid = ddi_prop_get_int(DDI_DEV_T_ANY,
	    statep->dip, DDI_PROP_DONTPASS, "device-id", 0);

	return (deviceid);
}

void
drm_core_ioremap(struct drm_local_map *map, drm_device_t *dev)
{
	if ((map->type == _DRM_AGP) && dev->agp) {
		/*
		 * During AGP mapping initialization, we map AGP aperture
		 * into kernel space. So, when we access the memory which
		 * managed by agp gart in kernel space, we have to go
		 * through two-level address translation: kernel virtual
		 * address --> aperture address --> physical address. For
		 * improving this, here in opensourced code, agp_remap()
		 * gets invoking to dispose the mapping between agp aperture
		 * and kernel space, and directly map the actual physical
		 * memory which is allocated to agp gart to kernel space.
		 * After that, access to physical memory managed by agp gart
		 * hardware in kernel space doesn't go through agp hardware,
		 * it will be: kernel virtual ---> physical address.
		 * Obviously, it is more efficient. But in solaris operating
		 * system, the ioctl AGPIOC_ALLOCATE of apggart driver does
		 * not return physical address. We are unable to create the
		 * direct mapping between kernel space and agp memory. So,
		 * we remove the calling to agp_remap().
		 */
		DRM_DEBUG("drm_core_ioremap: skipping agp_remap\n");
	} else {
		(void) drm_ioremap(dev, map);

	}
}

/*ARGSUSED*/
void
drm_core_ioremapfree(struct drm_local_map *map, drm_device_t *dev)
{
	if (map->type != _DRM_AGP) {
		if (map->handle && map->size)
			drm_ioremapfree(map);
	} else {
		/*
		 * Refer to the comments in drm_core_ioremap() where we removed
		 * the calling to agp_remap(), correspondingly, we remove the
		 * calling to agp_remap_free(dev, map);
		 */
		DRM_DEBUG("drm_core_ioremap: skipping agp_remap_free\n");
	}
}

struct drm_local_map *
drm_core_findmap(drm_device_t *dev, unsigned long handle)
{
	drm_local_map_t *map;

	DRM_SPINLOCK_ASSERT(&dev->dev_lock);

/*
 * For the time being, we compare the low 32 bit only,
 * We will hash handle to 32-bit to solve this issue later.
 */
	TAILQ_FOREACH(map, &dev->maplist, link) {
		if ((((unsigned long)map->handle) & 0x00000000ffffffff)
		    == (handle & 0x00000000ffffffff))
			return (map);
	}

	return (NULL);
}

/*
 * pci_alloc_consistent()
 */
static ddi_dma_attr_t	hw_dma_attr = {
		DMA_ATTR_V0,		/* version */
		0,			/* addr_lo */
		0xffffffff,	/* addr_hi */
		0xffffffff,	/* count_max */
		4096, 			/* alignment */
		0xfff,			/* burstsize */
		1,			/* minxfer */
		0xffffffff,		/* maxxfer */
		0xffffffff,		/* seg */
		1,			/* sgllen */
		4,			/* granular */
		0			/* flags */
};

static ddi_device_acc_attr_t hw_acc_attr = {
	DDI_DEVICE_ATTR_V0,
	DDI_NEVERSWAP_ACC,
	DDI_STRICTORDER_ACC
};


void *
drm_pci_alloc(drm_device_t *dev, size_t size,
    size_t align,  dma_addr_t maxaddr, int segments)
{
	drm_dma_handle_t	*dmah;
	uint_t	count;
	int ret = DDI_FAILURE;

	/* allocat continous physical memory for hw status page */
	if (align == 0)
		hw_dma_attr.dma_attr_align =  1;
	else
		hw_dma_attr.dma_attr_align = align;

	hw_dma_attr.dma_attr_addr_hi = maxaddr;
	hw_dma_attr.dma_attr_sgllen = segments;

	dmah = kmem_zalloc(sizeof (drm_dma_handle_t), KM_SLEEP);
	if (ret = ddi_dma_alloc_handle(dev->dip, &hw_dma_attr,
	    DDI_DMA_SLEEP, NULL, &dmah->dma_hdl)) {
		DRM_ERROR("drm_pci_alloc:ddi_dma_alloc_handle failed\n");
		goto err3;
	}

	if (ret = ddi_dma_mem_alloc(dmah->dma_hdl, size, &hw_acc_attr,
	    DDI_DMA_CONSISTENT | IOMEM_DATA_UNCACHED,
	    DDI_DMA_SLEEP, NULL, (caddr_t *)&dmah->vaddr,
	    &dmah->real_sz, &dmah->acc_hdl)) {
		DRM_ERROR("drm_pci_alloc: ddi_dma_mem_alloc failed\n");
		goto err2;
	}

	ret = ddi_dma_addr_bind_handle(dmah->dma_hdl, NULL,
	    (caddr_t)dmah->vaddr, dmah->real_sz,
	    DDI_DMA_RDWR|DDI_DMA_CONSISTENT,
	    DDI_DMA_SLEEP, NULL, &dmah->cookie,  &count);
	if (ret != DDI_DMA_MAPPED) {
		DRM_ERROR("drm_pci_alloc: alloc phys memory failed");
		goto err1;
	}

	if (count > segments) {
		(void) ddi_dma_unbind_handle(dmah->dma_hdl);
		goto err1;
	}

	dmah->cookie_num = count;
	if (count == 1)
		dmah->paddr = dmah->cookie.dmac_address;

	return (dmah);

err1:
	ddi_dma_mem_free(&dmah->acc_hdl);
err2:
	ddi_dma_free_handle(&dmah->dma_hdl);
err3:
	kmem_free(dmah, sizeof (*dmah));
	return (NULL);
}

/*
 * pci_free_consistent()
 */
/*ARGSUSED*/
void
drm_pci_free(drm_device_t *dev, drm_dma_handle_t *dmah)
{
	ASSERT(dmah != NULL);
	(void) ddi_dma_unbind_handle(dmah->dma_hdl);
	ddi_dma_mem_free(&dmah->acc_hdl);
	ddi_dma_free_handle(&dmah->dma_hdl);
	kmem_free(dmah, sizeof (drm_dma_handle_t));
}

int
do_get_pci_res(drm_device_t *dev, drm_pci_resource_t *resp)
{
	int length;
	pci_regspec_t	*regs;

	if (ddi_getlongprop(
	    DDI_DEV_T_ANY, dev->dip, DDI_PROP_DONTPASS,
	    "assigned-addresses", (caddr_t)&regs, &length) !=
	    DDI_PROP_SUCCESS) {
		DRM_ERROR("do_get_pci_res: ddi_getlongprop failed!\n");
		return (EFAULT);
	}
	resp->offset =
	    (unsigned long)regs[resp->regnum].pci_phys_low;
	resp->size =
	    (unsigned long)regs[resp->regnum].pci_size_low;
	kmem_free(regs, (size_t)length);

	return (0);
}

/*ARGSUSED*/
unsigned long
drm_get_resource_start(drm_device_t *softstate, unsigned int regnum)
{
	drm_pci_resource_t res;
	int ret;

	res.regnum = regnum;

	ret = do_get_pci_res(softstate, &res);

	if (ret != 0) {
		DRM_ERROR("drm_get_resource_start: ioctl failed");
		return (0);
	}

	return (res.offset);

}

/*ARGSUSED*/
unsigned long
drm_get_resource_len(drm_device_t *softstate, unsigned int regnum)
{
	drm_pci_resource_t res;
	int ret;

	res.regnum = regnum;

	ret = do_get_pci_res(softstate, &res);

	if (ret != 0) {
		DRM_ERROR("drm_get_resource_len: ioctl failed");
		return (0);
	}

	return (res.size);
}