xref: /netbsd-src/sys/external/bsd/drm2/include/drm/bus_dma_hacks.h (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: bus_dma_hacks.h,v 1.3 2014/04/03 19:18:29 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef	_DRM_BUS_DMA_HACKS_H_
33 #define	_DRM_BUS_DMA_HACKS_H_
34 
35 #include <sys/cdefs.h>
36 #include <sys/bus.h>
37 
38 #include <uvm/uvm.h>
39 #include <uvm/uvm_extern.h>
40 
41 /* XXX This is x86-specific bollocks.  */
42 
43 static inline int
44 bus_dmamem_wire_uvm_object(bus_dma_tag_t tag, struct uvm_object *uobj,
45     off_t start, bus_size_t size, struct pglist *pages, bus_size_t alignment,
46     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
47     int flags)
48 {
49 	struct pglist pageq;
50 	struct vm_page *page;
51 	bus_addr_t prev_addr, addr;
52 	unsigned int i;
53 	int error;
54 
55 	/*
56 	 * XXX `#ifdef __x86_64__' is a horrible way to work around a
57 	 * completely stupid GCC warning that encourages unsafe,
58 	 * nonportable code and has no obvious way to be selectively
59 	 * suppressed.
60 	 */
61 #if __x86_64__
62 	KASSERT(size <= __type_max(off_t));
63 #endif
64 
65 	KASSERT(start <= (__type_max(off_t) - size));
66 	KASSERT(alignment == PAGE_SIZE); /* XXX */
67 	KASSERT(0 < nsegs);
68 
69 	if (pages == NULL) {
70 		TAILQ_INIT(&pageq);
71 		pages = &pageq;
72 	}
73 
74 	error = uvm_obj_wirepages(uobj, start, (start + size), pages);
75 	if (error)
76 		goto fail0;
77 
78 	page = TAILQ_FIRST(pages);
79 	KASSERT(page != NULL);
80 
81 	addr = VM_PAGE_TO_PHYS(page);
82 	segs[0].ds_addr = addr;
83 	segs[0].ds_len = PAGE_SIZE;
84 
85 	i = 0;
86 	while ((page = TAILQ_NEXT(page, pageq.queue)) != NULL) {
87 		prev_addr = addr;
88 		addr = VM_PAGE_TO_PHYS(page);
89 		if ((addr == (prev_addr + PAGE_SIZE)) &&
90 		    ((addr & boundary) == (prev_addr & boundary))) {
91 			segs[i].ds_len += PAGE_SIZE;
92 		} else {
93 			i += 1;
94 			if (i >= nsegs) {
95 				error = EFBIG;
96 				goto fail1;
97 			}
98 			segs[i].ds_addr = addr;
99 			segs[i].ds_len = PAGE_SIZE;
100 		}
101 	}
102 
103 	/* Success!  */
104 	*rsegs = (i + 1);
105 	return 0;
106 
107 fail1:	uvm_obj_unwirepages(uobj, start, (start + size));
108 fail0:	return error;
109 }
110 
111 static inline void
112 bus_dmamem_unwire_uvm_object(bus_dma_tag_t tag __unused,
113     struct uvm_object *uobj, off_t start, bus_size_t size,
114     bus_dma_segment_t *segs __unused, int nsegs __unused)
115 {
116 	uvm_obj_unwirepages(uobj, start, (start + size));
117 }
118 
119 #endif	/* _DRM_BUS_DMA_HACKS_H_ */
120