Deleted Added
full compact
drm_pci.c (182080) drm_pci.c (182883)
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>
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 $");
25__FBSDID("$FreeBSD: head/sys/dev/drm/drm_pci.c 182883 2008-09-09 02:05:03Z 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__
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();
77 /* Make sure we aren't holding locks here */
78 if (mtx_owned(&dev->dev_lock))
79 DRM_ERROR("called while holding dev_lock\n");
80 if (mtx_owned(&dev->dma_lock))
81 DRM_ERROR("called while holding dma_lock\n");
82
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);
83 ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */
84 maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
85 NULL, NULL, /* filtfunc, filtfuncargs */
86 size, 1, size, /* maxsize, nsegs, maxsegsize */
87 BUS_DMA_ALLOCNOW, NULL, NULL, /* flags, lockfunc, lockfuncargs */
88 &dmah->tag);
89 if (ret != 0) {
90 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);
91 return NULL;
92 }
93
94 ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr, BUS_DMA_NOWAIT,
95 &dmah->map);
96 if (ret != 0) {
97 bus_dma_tag_destroy(dmah->tag);
98 free(dmah, M_DRM);
95 DRM_LOCK();
96 return NULL;
97 }
99 return NULL;
100 }
98 DRM_LOCK();
101
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/*@}*/
102 ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
103 drm_pci_busdma_callback, dmah, 0);
104 if (ret != 0) {
105 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
106 bus_dma_tag_destroy(dmah->tag);
107 free(dmah, M_DRM);
108 return NULL;
109 }
110#elif defined(__NetBSD__)
111 ret = bus_dmamem_alloc(dev->dma_tag, size, align, PAGE_SIZE,
112 &dmah->seg, 1, &nsegs, BUS_DMA_NOWAIT);
113 if ((ret != 0) || (nsegs != 1)) {
114 free(dmah, M_DRM);
115 return NULL;
116 }
117
118 ret = bus_dmamem_map(dev->dma_tag, &dmah->seg, 1, size, &dmah->addr,
119 BUS_DMA_NOWAIT);
120 if (ret != 0) {
121 bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);
122 free(dmah, M_DRM);
123 return NULL;
124 }
125
126 dmah->dmaaddr = h->seg.ds_addr;
127#endif
128
129 return dmah;
130}
131
132/**
133 * \brief Free a DMA-accessible consistent memory block.
134 */
135void
136drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah)
137{
138 if (dmah == NULL)
139 return;
140
141#if defined(__FreeBSD__)
142 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
143 bus_dma_tag_destroy(dmah->tag);
144#elif defined(__NetBSD__)
145 bus_dmamem_free(dev->dma_tag, &dmah->seg, 1);
146#endif
147
148 free(dmah, M_DRM);
149}
150
151/*@}*/