drm_pci.c revision 182080
1/*-
2 * Copyright 2003 Eric Anholt.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19 * AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include <sys/cdefs.h>
25__FBSDID("$FreeBSD: head/sys/dev/drm/drm_pci.c 182080 2008-08-23 20:59:12Z rnoland $");
26
27/**
28 * \file drm_pci.h
29 * \brief PCI consistent, DMA-accessible memory allocation.
30 *
31 * \author Eric Anholt <anholt@FreeBSD.org>
32 */
33
34#include "dev/drm/drmP.h"
35
36/**********************************************************************/
37/** \name PCI memory */
38/*@{*/
39
40#if defined(__FreeBSD__)
41static void
42drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
43{
44	drm_dma_handle_t *dmah = arg;
45
46	if (error != 0)
47		return;
48
49	KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
50	dmah->busaddr = segs[0].ds_addr;
51}
52#endif
53
54/**
55 * \brief Allocate a physically contiguous DMA-accessible consistent
56 * memory block.
57 */
58drm_dma_handle_t *
59drm_pci_alloc(struct drm_device *dev, size_t size,
60	      size_t align, dma_addr_t maxaddr)
61{
62	drm_dma_handle_t *dmah;
63	int ret;
64
65	/* Need power-of-two alignment, so fail the allocation if it isn't. */
66	if ((align & (align - 1)) != 0) {
67		DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
68		    (int)align);
69		return NULL;
70	}
71
72	dmah = malloc(sizeof(drm_dma_handle_t), M_DRM, M_ZERO | M_NOWAIT);
73	if (dmah == NULL)
74		return NULL;
75
76#ifdef __FreeBSD__
77	DRM_UNLOCK();
78	ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */
79	    maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
80	    NULL, NULL, /* filtfunc, filtfuncargs */
81	    size, 1, size, /* maxsize, nsegs, maxsegsize */
82	    BUS_DMA_ALLOCNOW, NULL, NULL, /* flags, lockfunc, lockfuncargs */
83	    &dmah->tag);
84	if (ret != 0) {
85		free(dmah, M_DRM);
86		DRM_LOCK();
87		return NULL;
88	}
89
90	ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr, BUS_DMA_NOWAIT,
91	    &dmah->map);
92	if (ret != 0) {
93		bus_dma_tag_destroy(dmah->tag);
94		free(dmah, M_DRM);
95		DRM_LOCK();
96		return NULL;
97	}
98	DRM_LOCK();
99	ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
100	    drm_pci_busdma_callback, dmah, 0);
101	if (ret != 0) {
102		bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
103		bus_dma_tag_destroy(dmah->tag);
104		free(dmah, M_DRM);
105		return NULL;
106	}
107#elif defined(__NetBSD__)
108	ret = bus_dmamem_alloc(dev->dma_tag, size, align, PAGE_SIZE,
109	    &dmah->seg, 1, &nsegs, BUS_DMA_NOWAIT);
110	if ((ret != 0) || (nsegs != 1)) {
111		free(dmah, M_DRM);
112		return NULL;
113	}
114
115	ret = bus_dmamem_map(dev->dma_tag, &dmah->seg, 1, size, &dmah->addr,
116	    BUS_DMA_NOWAIT);
117	if (ret != 0) {
118		bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);
119		free(dmah, M_DRM);
120		return NULL;
121	}
122
123	dmah->dmaaddr = h->seg.ds_addr;
124#endif
125
126	return dmah;
127}
128
129/**
130 * \brief Free a DMA-accessible consistent memory block.
131 */
132void
133drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah)
134{
135	if (dmah == NULL)
136		return;
137
138#if defined(__FreeBSD__)
139	bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
140	bus_dma_tag_destroy(dmah->tag);
141#elif defined(__NetBSD__)
142	bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);
143#endif
144
145	free(dmah, M_DRM);
146}
147
148/*@}*/
149