| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: linux_sg.c,v 1.3 2021/12/19 11:38:04 riastradh Exp $ */ | | 1 | /* $NetBSD: linux_sg.c,v 1.4 2021/12/19 12:03:13 riastradh Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2021 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2021 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -17,27 +17,27 @@ | | | @@ -17,27 +17,27 @@ |
17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. | | 26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ | | 27 | */ |
28 | | | 28 | |
29 | #include <sys/cdefs.h> | | 29 | #include <sys/cdefs.h> |
30 | __KERNEL_RCSID(0, "$NetBSD: linux_sg.c,v 1.3 2021/12/19 11:38:04 riastradh Exp $"); | | 30 | __KERNEL_RCSID(0, "$NetBSD: linux_sg.c,v 1.4 2021/12/19 12:03:13 riastradh Exp $"); |
31 | | | 31 | |
32 | #include <sys/bus.h> | | 32 | #include <sys/bus.h> |
33 | #include <sys/errno.h> | | 33 | #include <sys/errno.h> |
34 | | | 34 | |
35 | #include <drm/bus_dma_hacks.h> | | 35 | #include <drm/bus_dma_hacks.h> |
36 | | | 36 | |
37 | #include <linux/dma-mapping.h> | | 37 | #include <linux/dma-mapping.h> |
38 | #include <linux/gfp.h> | | 38 | #include <linux/gfp.h> |
39 | #include <linux/mm_types.h> | | 39 | #include <linux/mm_types.h> |
40 | #include <linux/scatterlist.h> | | 40 | #include <linux/scatterlist.h> |
41 | #include <linux/slab.h> | | 41 | #include <linux/slab.h> |
42 | | | 42 | |
43 | int | | 43 | int |
| @@ -77,31 +77,46 @@ __sg_alloc_table_from_pages(struct sg_ta | | | @@ -77,31 +77,46 @@ __sg_alloc_table_from_pages(struct sg_ta |
77 | int | | 77 | int |
78 | sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pgs, | | 78 | sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pgs, |
79 | unsigned npgs, bus_size_t offset, bus_size_t size, gfp_t gfp) | | 79 | unsigned npgs, bus_size_t offset, bus_size_t size, gfp_t gfp) |
80 | { | | 80 | { |
81 | | | 81 | |
82 | return __sg_alloc_table_from_pages(sgt, pgs, npgs, offset, size, | | 82 | return __sg_alloc_table_from_pages(sgt, pgs, npgs, offset, size, |
83 | -1, gfp); | | 83 | -1, gfp); |
84 | } | | 84 | } |
85 | | | 85 | |
86 | int | | 86 | int |
87 | sg_alloc_table_from_bus_dmamem(struct sg_table *sgt, bus_dma_tag_t dmat, | | 87 | sg_alloc_table_from_bus_dmamem(struct sg_table *sgt, bus_dma_tag_t dmat, |
88 | const bus_dma_segment_t *seg, int nseg, gfp_t gfp) | | 88 | const bus_dma_segment_t *seg, int nseg, gfp_t gfp) |
89 | { | | 89 | { |
| | | 90 | int i, npgs = 0; |
90 | int ret; | | 91 | int ret; |
91 | | | 92 | |
92 | KASSERT(nseg >= 1); | | 93 | KASSERT(nseg >= 1); |
93 | | | 94 | |
94 | ret = sg_alloc_table(sgt, nseg, gfp); | | 95 | /* |
| | | 96 | * Count the number of pages. Some segments may span multiple |
| | | 97 | * contiguous pages. |
| | | 98 | */ |
| | | 99 | for (i = 0; i < nseg; i++) { |
| | | 100 | bus_size_t len = seg[i].ds_len; |
| | | 101 | for (; len >= PAGE_SIZE; len -= PAGE_SIZE, npgs++) { |
| | | 102 | if (npgs == INT_MAX) |
| | | 103 | return -ENOMEM; |
| | | 104 | } |
| | | 105 | KASSERTMSG(len == 0, "misaligned segment length: %ju\n", |
| | | 106 | (uintmax_t)seg[i].ds_len); |
| | | 107 | } |
| | | 108 | |
| | | 109 | ret = sg_alloc_table(sgt, npgs, gfp); |
95 | if (ret) | | 110 | if (ret) |
96 | return ret; | | 111 | return ret; |
97 | | | 112 | |
98 | /* XXX errno NetBSD->Linux */ | | 113 | /* XXX errno NetBSD->Linux */ |
99 | ret = -bus_dmamem_export_pages(dmat, seg, nseg, sgt->sgl->sg_pgs, | | 114 | ret = -bus_dmamem_export_pages(dmat, seg, nseg, sgt->sgl->sg_pgs, |
100 | sgt->sgl->sg_npgs); | | 115 | sgt->sgl->sg_npgs); |
101 | if (ret) | | 116 | if (ret) |
102 | goto out; | | 117 | goto out; |
103 | | | 118 | |
104 | /* Success! */ | | 119 | /* Success! */ |
105 | ret = 0; | | 120 | ret = 0; |
106 | | | 121 | |
107 | out: if (ret) | | 122 | out: if (ret) |