]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/busdma_machdep-v6.c
followup to r332730/r332752: set kdb_why to "trap" for fatal traps
[FreeBSD/FreeBSD.git] / sys / arm / arm / busdma_machdep-v6.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012-2015 Ian Lepore
5  * Copyright (c) 2010 Mark Tinguely
6  * Copyright (c) 2004 Olivier Houchard
7  * Copyright (c) 2002 Peter Grehan
8  * Copyright (c) 1997, 1998 Justin T. Gibbs.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification, immediately at the beginning of the file.
17  * 2. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *  From i386/busdma_machdep.c 191438 2009-04-23 20:24:19Z jhb
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/bus.h>
42 #include <sys/busdma_bufalloc.h>
43 #include <sys/counter.h>
44 #include <sys/interrupt.h>
45 #include <sys/kernel.h>
46 #include <sys/ktr.h>
47 #include <sys/lock.h>
48 #include <sys/memdesc.h>
49 #include <sys/proc.h>
50 #include <sys/mutex.h>
51 #include <sys/sysctl.h>
52 #include <sys/uio.h>
53
54 #include <vm/vm.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_kern.h>
59
60 #include <machine/atomic.h>
61 #include <machine/bus.h>
62 #include <machine/cpu.h>
63 #include <machine/md_var.h>
64
65 #define BUSDMA_DCACHE_ALIGN     cpuinfo.dcache_line_size
66 #define BUSDMA_DCACHE_MASK      cpuinfo.dcache_line_mask
67
68 #define MAX_BPAGES              64
69 #define MAX_DMA_SEGMENTS        4096
70 #define BUS_DMA_EXCL_BOUNCE     BUS_DMA_BUS2
71 #define BUS_DMA_ALIGN_BOUNCE    BUS_DMA_BUS3
72 #define BUS_DMA_COULD_BOUNCE    (BUS_DMA_EXCL_BOUNCE | BUS_DMA_ALIGN_BOUNCE)
73 #define BUS_DMA_MIN_ALLOC_COMP  BUS_DMA_BUS4
74
75 struct bounce_zone;
76
77 struct bus_dma_tag {
78         bus_dma_tag_t           parent;
79         bus_size_t              alignment;
80         bus_addr_t              boundary;
81         bus_addr_t              lowaddr;
82         bus_addr_t              highaddr;
83         bus_dma_filter_t        *filter;
84         void                    *filterarg;
85         bus_size_t              maxsize;
86         u_int                   nsegments;
87         bus_size_t              maxsegsz;
88         int                     flags;
89         int                     ref_count;
90         int                     map_count;
91         bus_dma_lock_t          *lockfunc;
92         void                    *lockfuncarg;
93         struct bounce_zone      *bounce_zone;
94 };
95
96 struct bounce_page {
97         vm_offset_t     vaddr;          /* kva of bounce buffer */
98         bus_addr_t      busaddr;        /* Physical address */
99         vm_offset_t     datavaddr;      /* kva of client data */
100         vm_page_t       datapage;       /* physical page of client data */
101         vm_offset_t     dataoffs;       /* page offset of client data */
102         bus_size_t      datacount;      /* client data count */
103         STAILQ_ENTRY(bounce_page) links;
104 };
105
106 struct sync_list {
107         vm_offset_t     vaddr;          /* kva of client data */
108         bus_addr_t      paddr;          /* physical address */
109         vm_page_t       pages;          /* starting page of client data */
110         bus_size_t      datacount;      /* client data count */
111 };
112
113 int busdma_swi_pending;
114
115 struct bounce_zone {
116         STAILQ_ENTRY(bounce_zone) links;
117         STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
118         int             total_bpages;
119         int             free_bpages;
120         int             reserved_bpages;
121         int             active_bpages;
122         int             total_bounced;
123         int             total_deferred;
124         int             map_count;
125         bus_size_t      alignment;
126         bus_addr_t      lowaddr;
127         char            zoneid[8];
128         char            lowaddrid[20];
129         struct sysctl_ctx_list sysctl_tree;
130         struct sysctl_oid *sysctl_tree_top;
131 };
132
133 static struct mtx bounce_lock;
134 static int total_bpages;
135 static int busdma_zonecount;
136 static uint32_t tags_total;
137 static uint32_t maps_total;
138 static uint32_t maps_dmamem;
139 static uint32_t maps_coherent;
140 static counter_u64_t maploads_total;
141 static counter_u64_t maploads_bounced;
142 static counter_u64_t maploads_coherent;
143 static counter_u64_t maploads_dmamem;
144 static counter_u64_t maploads_mbuf;
145 static counter_u64_t maploads_physmem;
146
147 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
148
149 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
150 SYSCTL_UINT(_hw_busdma, OID_AUTO, tags_total, CTLFLAG_RD, &tags_total, 0,
151    "Number of active tags");
152 SYSCTL_UINT(_hw_busdma, OID_AUTO, maps_total, CTLFLAG_RD, &maps_total, 0,
153    "Number of active maps");
154 SYSCTL_UINT(_hw_busdma, OID_AUTO, maps_dmamem, CTLFLAG_RD, &maps_dmamem, 0,
155    "Number of active maps for bus_dmamem_alloc buffers");
156 SYSCTL_UINT(_hw_busdma, OID_AUTO, maps_coherent, CTLFLAG_RD, &maps_coherent, 0,
157    "Number of active maps with BUS_DMA_COHERENT flag set");
158 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_total, CTLFLAG_RD,
159     &maploads_total, "Number of load operations performed");
160 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_bounced, CTLFLAG_RD,
161     &maploads_bounced, "Number of load operations that used bounce buffers");
162 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_coherent, CTLFLAG_RD,
163     &maploads_dmamem, "Number of load operations on BUS_DMA_COHERENT memory");
164 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_dmamem, CTLFLAG_RD,
165     &maploads_dmamem, "Number of load operations on bus_dmamem_alloc buffers");
166 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_mbuf, CTLFLAG_RD,
167     &maploads_mbuf, "Number of load operations for mbufs");
168 SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_physmem, CTLFLAG_RD,
169     &maploads_physmem, "Number of load operations on physical buffers");
170 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
171    "Total bounce pages");
172
173 struct bus_dmamap {
174         struct bp_list          bpages;
175         int                     pagesneeded;
176         int                     pagesreserved;
177         bus_dma_tag_t           dmat;
178         struct memdesc          mem;
179         bus_dmamap_callback_t   *callback;
180         void                    *callback_arg;
181         int                     flags;
182 #define DMAMAP_COHERENT         (1 << 0)
183 #define DMAMAP_DMAMEM_ALLOC     (1 << 1)
184 #define DMAMAP_MBUF             (1 << 2)
185         STAILQ_ENTRY(bus_dmamap) links;
186         bus_dma_segment_t       *segments;
187         int                     sync_count;
188         struct sync_list        slist[];
189 };
190
191 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
192 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
193
194 static void init_bounce_pages(void *dummy);
195 static int alloc_bounce_zone(bus_dma_tag_t dmat);
196 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
197 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
198     int commit);
199 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
200     vm_offset_t vaddr, bus_addr_t addr, bus_size_t size);
201 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
202 static void _bus_dmamap_count_pages(bus_dma_tag_t dmat, pmap_t pmap,
203     bus_dmamap_t map, void *buf, bus_size_t buflen, int flags);
204 static void _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
205     vm_paddr_t buf, bus_size_t buflen, int flags);
206 static int _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
207     int flags);
208 static void dma_preread_safe(vm_offset_t va, vm_paddr_t pa, vm_size_t size);
209 static void dma_dcache_sync(struct sync_list *sl, bus_dmasync_op_t op);
210
211 static busdma_bufalloc_t coherent_allocator;    /* Cache of coherent buffers */
212 static busdma_bufalloc_t standard_allocator;    /* Cache of standard buffers */
213
214 MALLOC_DEFINE(M_BUSDMA, "busdma", "busdma metadata");
215 MALLOC_DEFINE(M_BOUNCE, "bounce", "busdma bounce pages");
216
217 static void
218 busdma_init(void *dummy)
219 {
220         int uma_flags;
221
222         maploads_total    = counter_u64_alloc(M_WAITOK);
223         maploads_bounced  = counter_u64_alloc(M_WAITOK);
224         maploads_coherent = counter_u64_alloc(M_WAITOK);
225         maploads_dmamem   = counter_u64_alloc(M_WAITOK);
226         maploads_mbuf     = counter_u64_alloc(M_WAITOK);
227         maploads_physmem  = counter_u64_alloc(M_WAITOK);
228
229         uma_flags = 0;
230
231         /* Create a cache of buffers in standard (cacheable) memory. */
232         standard_allocator = busdma_bufalloc_create("buffer",
233             BUSDMA_DCACHE_ALIGN,/* minimum_alignment */
234             NULL,               /* uma_alloc func */
235             NULL,               /* uma_free func */
236             uma_flags);         /* uma_zcreate_flags */
237
238 #ifdef INVARIANTS
239         /*
240          * Force UMA zone to allocate service structures like
241          * slabs using own allocator. uma_debug code performs
242          * atomic ops on uma_slab_t fields and safety of this
243          * operation is not guaranteed for write-back caches
244          */
245         uma_flags = UMA_ZONE_OFFPAGE;
246 #endif
247         /*
248          * Create a cache of buffers in uncacheable memory, to implement the
249          * BUS_DMA_COHERENT (and potentially BUS_DMA_NOCACHE) flag.
250          */
251         coherent_allocator = busdma_bufalloc_create("coherent",
252             BUSDMA_DCACHE_ALIGN,/* minimum_alignment */
253             busdma_bufalloc_alloc_uncacheable,
254             busdma_bufalloc_free_uncacheable,
255             uma_flags); /* uma_zcreate_flags */
256 }
257
258 /*
259  * This init historically used SI_SUB_VM, but now the init code requires
260  * malloc(9) using M_BUSDMA memory and the pcpu zones for counter(9), which get
261  * set up by SI_SUB_KMEM and SI_ORDER_LAST, so we'll go right after that by
262  * using SI_SUB_KMEM+1.
263  */
264 SYSINIT(busdma, SI_SUB_KMEM+1, SI_ORDER_FIRST, busdma_init, NULL);
265
266 /*
267  * This routine checks the exclusion zone constraints from a tag against the
268  * physical RAM available on the machine.  If a tag specifies an exclusion zone
269  * but there's no RAM in that zone, then we avoid allocating resources to bounce
270  * a request, and we can use any memory allocator (as opposed to needing
271  * kmem_alloc_contig() just because it can allocate pages in an address range).
272  *
273  * Most tags have BUS_SPACE_MAXADDR or BUS_SPACE_MAXADDR_32BIT (they are the
274  * same value on 32-bit architectures) as their lowaddr constraint, and we can't
275  * possibly have RAM at an address higher than the highest address we can
276  * express, so we take a fast out.
277  */
278 static int
279 exclusion_bounce_check(vm_offset_t lowaddr, vm_offset_t highaddr)
280 {
281         int i;
282
283         if (lowaddr >= BUS_SPACE_MAXADDR)
284                 return (0);
285
286         for (i = 0; phys_avail[i] && phys_avail[i + 1]; i += 2) {
287                 if ((lowaddr >= phys_avail[i] && lowaddr < phys_avail[i + 1]) ||
288                     (lowaddr < phys_avail[i] && highaddr >= phys_avail[i]))
289                         return (1);
290         }
291         return (0);
292 }
293
294 /*
295  * Return true if the tag has an exclusion zone that could lead to bouncing.
296  */
297 static __inline int
298 exclusion_bounce(bus_dma_tag_t dmat)
299 {
300
301         return (dmat->flags & BUS_DMA_EXCL_BOUNCE);
302 }
303
304 /*
305  * Return true if the given address does not fall on the alignment boundary.
306  */
307 static __inline int
308 alignment_bounce(bus_dma_tag_t dmat, bus_addr_t addr)
309 {
310
311         return (addr & (dmat->alignment - 1));
312 }
313
314 /*
315  * Return true if the DMA should bounce because the start or end does not fall
316  * on a cacheline boundary (which would require a partial cacheline flush).
317  * COHERENT memory doesn't trigger cacheline flushes.  Memory allocated by
318  * bus_dmamem_alloc() is always aligned to cacheline boundaries, and there's a
319  * strict rule that such memory cannot be accessed by the CPU while DMA is in
320  * progress (or by multiple DMA engines at once), so that it's always safe to do
321  * full cacheline flushes even if that affects memory outside the range of a
322  * given DMA operation that doesn't involve the full allocated buffer.  If we're
323  * mapping an mbuf, that follows the same rules as a buffer we allocated.
324  */
325 static __inline int
326 cacheline_bounce(bus_dmamap_t map, bus_addr_t addr, bus_size_t size)
327 {
328
329         if (map->flags & (DMAMAP_DMAMEM_ALLOC | DMAMAP_COHERENT | DMAMAP_MBUF))
330                 return (0);
331         return ((addr | size) & BUSDMA_DCACHE_MASK);
332 }
333
334 /*
335  * Return true if we might need to bounce the DMA described by addr and size.
336  *
337  * This is used to quick-check whether we need to do the more expensive work of
338  * checking the DMA page-by-page looking for alignment and exclusion bounces.
339  *
340  * Note that the addr argument might be either virtual or physical.  It doesn't
341  * matter because we only look at the low-order bits, which are the same in both
342  * address spaces.
343  */
344 static __inline int
345 might_bounce(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t addr,
346     bus_size_t size)
347 {
348
349         return ((dmat->flags & BUS_DMA_EXCL_BOUNCE) ||
350             alignment_bounce(dmat, addr) ||
351             cacheline_bounce(map, addr, size));
352 }
353
354 /*
355  * Return true if we must bounce the DMA described by paddr and size.
356  *
357  * Bouncing can be triggered by DMA that doesn't begin and end on cacheline
358  * boundaries, or doesn't begin on an alignment boundary, or falls within the
359  * exclusion zone of any tag in the ancestry chain.
360  *
361  * For exclusions, walk the chain of tags comparing paddr to the exclusion zone
362  * within each tag.  If the tag has a filter function, use it to decide whether
363  * the DMA needs to bounce, otherwise any DMA within the zone bounces.
364  */
365 static int
366 must_bounce(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t paddr,
367     bus_size_t size)
368 {
369
370         if (cacheline_bounce(map, paddr, size))
371                 return (1);
372
373         /*
374          *  The tag already contains ancestors' alignment restrictions so this
375          *  check doesn't need to be inside the loop.
376          */
377         if (alignment_bounce(dmat, paddr))
378                 return (1);
379
380         /*
381          * Even though each tag has an exclusion zone that is a superset of its
382          * own and all its ancestors' exclusions, the exclusion zone of each tag
383          * up the chain must be checked within the loop, because the busdma
384          * rules say the filter function is called only when the address lies
385          * within the low-highaddr range of the tag that filterfunc belongs to.
386          */
387         while (dmat != NULL && exclusion_bounce(dmat)) {
388                 if ((paddr >= dmat->lowaddr && paddr <= dmat->highaddr) &&
389                     (dmat->filter == NULL ||
390                     dmat->filter(dmat->filterarg, paddr) != 0))
391                         return (1);
392                 dmat = dmat->parent;
393         }
394
395         return (0);
396 }
397
398 /*
399  * Convenience function for manipulating driver locks from busdma (during
400  * busdma_swi, for example).  Drivers that don't provide their own locks
401  * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
402  * non-mutex locking scheme don't have to use this at all.
403  */
404 void
405 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
406 {
407         struct mtx *dmtx;
408
409         dmtx = (struct mtx *)arg;
410         switch (op) {
411         case BUS_DMA_LOCK:
412                 mtx_lock(dmtx);
413                 break;
414         case BUS_DMA_UNLOCK:
415                 mtx_unlock(dmtx);
416                 break;
417         default:
418                 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
419         }
420 }
421
422 /*
423  * dflt_lock should never get called.  It gets put into the dma tag when
424  * lockfunc == NULL, which is only valid if the maps that are associated
425  * with the tag are meant to never be defered.
426  * XXX Should have a way to identify which driver is responsible here.
427  */
428 static void
429 dflt_lock(void *arg, bus_dma_lock_op_t op)
430 {
431
432         panic("driver error: busdma dflt_lock called");
433 }
434
435 /*
436  * Allocate a device specific dma_tag.
437  */
438 int
439 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
440     bus_addr_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
441     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
442     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
443     void *lockfuncarg, bus_dma_tag_t *dmat)
444 {
445         bus_dma_tag_t newtag;
446         int error = 0;
447
448         /* Basic sanity checking. */
449         KASSERT(boundary == 0 || powerof2(boundary),
450             ("dma tag boundary %lu, must be a power of 2", boundary));
451         KASSERT(boundary == 0 || boundary >= maxsegsz,
452             ("dma tag boundary %lu is < maxsegsz %lu\n", boundary, maxsegsz));
453         KASSERT(alignment != 0 && powerof2(alignment),
454             ("dma tag alignment %lu, must be non-zero power of 2", alignment));
455         KASSERT(maxsegsz != 0, ("dma tag maxsegsz must not be zero"));
456
457         /* Return a NULL tag on failure */
458         *dmat = NULL;
459
460         newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_BUSDMA,
461             M_ZERO | M_NOWAIT);
462         if (newtag == NULL) {
463                 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
464                     __func__, newtag, 0, error);
465                 return (ENOMEM);
466         }
467
468         newtag->parent = parent;
469         newtag->alignment = alignment;
470         newtag->boundary = boundary;
471         newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
472         newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
473             (PAGE_SIZE - 1);
474         newtag->filter = filter;
475         newtag->filterarg = filterarg;
476         newtag->maxsize = maxsize;
477         newtag->nsegments = nsegments;
478         newtag->maxsegsz = maxsegsz;
479         newtag->flags = flags;
480         newtag->ref_count = 1; /* Count ourself */
481         newtag->map_count = 0;
482         if (lockfunc != NULL) {
483                 newtag->lockfunc = lockfunc;
484                 newtag->lockfuncarg = lockfuncarg;
485         } else {
486                 newtag->lockfunc = dflt_lock;
487                 newtag->lockfuncarg = NULL;
488         }
489
490         /* Take into account any restrictions imposed by our parent tag */
491         if (parent != NULL) {
492                 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
493                 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
494                 newtag->alignment = MAX(parent->alignment, newtag->alignment);
495                 newtag->flags |= parent->flags & BUS_DMA_COULD_BOUNCE;
496                 newtag->flags |= parent->flags & BUS_DMA_COHERENT;
497                 if (newtag->boundary == 0)
498                         newtag->boundary = parent->boundary;
499                 else if (parent->boundary != 0)
500                         newtag->boundary = MIN(parent->boundary,
501                                                newtag->boundary);
502                 if (newtag->filter == NULL) {
503                         /*
504                          * Short circuit to looking at our parent directly
505                          * since we have encapsulated all of its information
506                          */
507                         newtag->filter = parent->filter;
508                         newtag->filterarg = parent->filterarg;
509                         newtag->parent = parent->parent;
510                 }
511                 if (newtag->parent != NULL)
512                         atomic_add_int(&parent->ref_count, 1);
513         }
514
515         if (exclusion_bounce_check(newtag->lowaddr, newtag->highaddr))
516                 newtag->flags |= BUS_DMA_EXCL_BOUNCE;
517         if (alignment_bounce(newtag, 1))
518                 newtag->flags |= BUS_DMA_ALIGN_BOUNCE;
519
520         /*
521          * Any request can auto-bounce due to cacheline alignment, in addition
522          * to any alignment or boundary specifications in the tag, so if the
523          * ALLOCNOW flag is set, there's always work to do.
524          */
525         if ((flags & BUS_DMA_ALLOCNOW) != 0) {
526                 struct bounce_zone *bz;
527                 /*
528                  * Round size up to a full page, and add one more page because
529                  * there can always be one more boundary crossing than the
530                  * number of pages in a transfer.
531                  */
532                 maxsize = roundup2(maxsize, PAGE_SIZE) + PAGE_SIZE;
533
534                 if ((error = alloc_bounce_zone(newtag)) != 0) {
535                         free(newtag, M_BUSDMA);
536                         return (error);
537                 }
538                 bz = newtag->bounce_zone;
539
540                 if (ptoa(bz->total_bpages) < maxsize) {
541                         int pages;
542
543                         pages = atop(maxsize) - bz->total_bpages;
544
545                         /* Add pages to our bounce pool */
546                         if (alloc_bounce_pages(newtag, pages) < pages)
547                                 error = ENOMEM;
548                 }
549                 /* Performed initial allocation */
550                 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
551         } else
552                 newtag->bounce_zone = NULL;
553
554         if (error != 0) {
555                 free(newtag, M_BUSDMA);
556         } else {
557                 atomic_add_32(&tags_total, 1);
558                 *dmat = newtag;
559         }
560         CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
561             __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
562         return (error);
563 }
564
565 int
566 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
567 {
568
569         return (0);
570 }
571
572 int
573 bus_dma_tag_destroy(bus_dma_tag_t dmat)
574 {
575         bus_dma_tag_t dmat_copy;
576         int error;
577
578         error = 0;
579         dmat_copy = dmat;
580
581         if (dmat != NULL) {
582
583                 if (dmat->map_count != 0) {
584                         error = EBUSY;
585                         goto out;
586                 }
587
588                 while (dmat != NULL) {
589                         bus_dma_tag_t parent;
590
591                         parent = dmat->parent;
592                         atomic_subtract_int(&dmat->ref_count, 1);
593                         if (dmat->ref_count == 0) {
594                                 atomic_subtract_32(&tags_total, 1);
595                                 free(dmat, M_BUSDMA);
596                                 /*
597                                  * Last reference count, so
598                                  * release our reference
599                                  * count on our parent.
600                                  */
601                                 dmat = parent;
602                         } else
603                                 dmat = NULL;
604                 }
605         }
606 out:
607         CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
608         return (error);
609 }
610
611 static int
612 allocate_bz_and_pages(bus_dma_tag_t dmat, bus_dmamap_t mapp)
613 {
614         struct bounce_zone *bz;
615         int maxpages;
616         int error;
617
618         if (dmat->bounce_zone == NULL)
619                 if ((error = alloc_bounce_zone(dmat)) != 0)
620                         return (error);
621         bz = dmat->bounce_zone;
622         /* Initialize the new map */
623         STAILQ_INIT(&(mapp->bpages));
624
625         /*
626          * Attempt to add pages to our pool on a per-instance basis up to a sane
627          * limit.  Even if the tag isn't flagged as COULD_BOUNCE due to
628          * alignment and boundary constraints, it could still auto-bounce due to
629          * cacheline alignment, which requires at most two bounce pages.
630          */
631         if (dmat->flags & BUS_DMA_COULD_BOUNCE)
632                 maxpages = MAX_BPAGES;
633         else
634                 maxpages = 2 * bz->map_count;
635         if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0 ||
636             (bz->map_count > 0 && bz->total_bpages < maxpages)) {
637                 int pages;
638
639                 pages = atop(roundup2(dmat->maxsize, PAGE_SIZE)) + 1;
640                 pages = MIN(maxpages - bz->total_bpages, pages);
641                 pages = MAX(pages, 2);
642                 if (alloc_bounce_pages(dmat, pages) < pages)
643                         return (ENOMEM);
644
645                 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0)
646                         dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
647         }
648         bz->map_count++;
649         return (0);
650 }
651
652 static bus_dmamap_t
653 allocate_map(bus_dma_tag_t dmat, int mflags)
654 {
655         int mapsize, segsize;
656         bus_dmamap_t map;
657
658         /*
659          * Allocate the map.  The map structure ends with an embedded
660          * variable-sized array of sync_list structures.  Following that
661          * we allocate enough extra space to hold the array of bus_dma_segments.
662          */
663         KASSERT(dmat->nsegments <= MAX_DMA_SEGMENTS,
664            ("cannot allocate %u dma segments (max is %u)",
665             dmat->nsegments, MAX_DMA_SEGMENTS));
666         segsize = sizeof(struct bus_dma_segment) * dmat->nsegments;
667         mapsize = sizeof(*map) + sizeof(struct sync_list) * dmat->nsegments;
668         map = malloc(mapsize + segsize, M_BUSDMA, mflags | M_ZERO);
669         if (map == NULL) {
670                 CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM);
671                 return (NULL);
672         }
673         map->segments = (bus_dma_segment_t *)((uintptr_t)map + mapsize);
674         return (map);
675 }
676
677 /*
678  * Allocate a handle for mapping from kva/uva/physical
679  * address space into bus device space.
680  */
681 int
682 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
683 {
684         bus_dmamap_t map;
685         int error = 0;
686
687         *mapp = map = allocate_map(dmat, M_NOWAIT);
688         if (map == NULL) {
689                 CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM);
690                 return (ENOMEM);
691         }
692
693         /*
694          * Bouncing might be required if the driver asks for an exclusion
695          * region, a data alignment that is stricter than 1, or DMA that begins
696          * or ends with a partial cacheline.  Whether bouncing will actually
697          * happen can't be known until mapping time, but we need to pre-allocate
698          * resources now because we might not be allowed to at mapping time.
699          */
700         error = allocate_bz_and_pages(dmat, map);
701         if (error != 0) {
702                 free(map, M_BUSDMA);
703                 *mapp = NULL;
704                 return (error);
705         }
706         if (map->flags & DMAMAP_COHERENT)
707                 atomic_add_32(&maps_coherent, 1);
708         atomic_add_32(&maps_total, 1);
709         dmat->map_count++;
710
711         return (0);
712 }
713
714 /*
715  * Destroy a handle for mapping from kva/uva/physical
716  * address space into bus device space.
717  */
718 int
719 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
720 {
721
722         if (STAILQ_FIRST(&map->bpages) != NULL || map->sync_count != 0) {
723                 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
724                     __func__, dmat, EBUSY);
725                 return (EBUSY);
726         }
727         if (dmat->bounce_zone)
728                 dmat->bounce_zone->map_count--;
729         if (map->flags & DMAMAP_COHERENT)
730                 atomic_subtract_32(&maps_coherent, 1);
731         atomic_subtract_32(&maps_total, 1);
732         free(map, M_BUSDMA);
733         dmat->map_count--;
734         CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
735         return (0);
736 }
737
738 /*
739  * Allocate a piece of memory that can be efficiently mapped into bus device
740  * space based on the constraints listed in the dma tag.  Returns a pointer to
741  * the allocated memory, and a pointer to an associated bus_dmamap.
742  */
743 int
744 bus_dmamem_alloc(bus_dma_tag_t dmat, void **vaddr, int flags,
745     bus_dmamap_t *mapp)
746 {
747         busdma_bufalloc_t ba;
748         struct busdma_bufzone *bufzone;
749         bus_dmamap_t map;
750         vm_memattr_t memattr;
751         int mflags;
752
753         if (flags & BUS_DMA_NOWAIT)
754                 mflags = M_NOWAIT;
755         else
756                 mflags = M_WAITOK;
757         if (flags & BUS_DMA_ZERO)
758                 mflags |= M_ZERO;
759
760         *mapp = map = allocate_map(dmat, mflags);
761         if (map == NULL) {
762                 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
763                     __func__, dmat, dmat->flags, ENOMEM);
764                 return (ENOMEM);
765         }
766         map->flags = DMAMAP_DMAMEM_ALLOC;
767
768         /* For coherent memory, set the map flag that disables sync ops. */
769         if (flags & BUS_DMA_COHERENT)
770                 map->flags |= DMAMAP_COHERENT;
771
772         /*
773          * Choose a busdma buffer allocator based on memory type flags.
774          * If the tag's COHERENT flag is set, that means normal memory
775          * is already coherent, use the normal allocator.
776          */
777         if ((flags & BUS_DMA_COHERENT) &&
778             ((dmat->flags & BUS_DMA_COHERENT) == 0)) {
779                 memattr = VM_MEMATTR_UNCACHEABLE;
780                 ba = coherent_allocator;
781         } else {
782                 memattr = VM_MEMATTR_DEFAULT;
783                 ba = standard_allocator;
784         }
785
786         /*
787          * Try to find a bufzone in the allocator that holds a cache of buffers
788          * of the right size for this request.  If the buffer is too big to be
789          * held in the allocator cache, this returns NULL.
790          */
791         bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize);
792
793         /*
794          * Allocate the buffer from the uma(9) allocator if...
795          *  - It's small enough to be in the allocator (bufzone not NULL).
796          *  - The alignment constraint isn't larger than the allocation size
797          *    (the allocator aligns buffers to their size boundaries).
798          *  - There's no need to handle lowaddr/highaddr exclusion zones.
799          * else allocate non-contiguous pages if...
800          *  - The page count that could get allocated doesn't exceed
801          *    nsegments also when the maximum segment size is less
802          *    than PAGE_SIZE.
803          *  - The alignment constraint isn't larger than a page boundary.
804          *  - There are no boundary-crossing constraints.
805          * else allocate a block of contiguous pages because one or more of the
806          * constraints is something that only the contig allocator can fulfill.
807          */
808         if (bufzone != NULL && dmat->alignment <= bufzone->size &&
809             !exclusion_bounce(dmat)) {
810                 *vaddr = uma_zalloc(bufzone->umazone, mflags);
811         } else if (dmat->nsegments >=
812             howmany(dmat->maxsize, MIN(dmat->maxsegsz, PAGE_SIZE)) &&
813             dmat->alignment <= PAGE_SIZE &&
814             (dmat->boundary % PAGE_SIZE) == 0) {
815                 *vaddr = (void *)kmem_alloc_attr(kernel_arena, dmat->maxsize,
816                     mflags, 0, dmat->lowaddr, memattr);
817         } else {
818                 *vaddr = (void *)kmem_alloc_contig(kernel_arena, dmat->maxsize,
819                     mflags, 0, dmat->lowaddr, dmat->alignment, dmat->boundary,
820                     memattr);
821         }
822         if (*vaddr == NULL) {
823                 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
824                     __func__, dmat, dmat->flags, ENOMEM);
825                 free(map, M_BUSDMA);
826                 *mapp = NULL;
827                 return (ENOMEM);
828         }
829         if (map->flags & DMAMAP_COHERENT)
830                 atomic_add_32(&maps_coherent, 1);
831         atomic_add_32(&maps_dmamem, 1);
832         atomic_add_32(&maps_total, 1);
833         dmat->map_count++;
834
835         CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
836             __func__, dmat, dmat->flags, 0);
837         return (0);
838 }
839
840 /*
841  * Free a piece of memory that was allocated via bus_dmamem_alloc, along with
842  * its associated map.
843  */
844 void
845 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
846 {
847         struct busdma_bufzone *bufzone;
848         busdma_bufalloc_t ba;
849
850         if ((map->flags & DMAMAP_COHERENT) &&
851             ((dmat->flags & BUS_DMA_COHERENT) == 0))
852                 ba = coherent_allocator;
853         else
854                 ba = standard_allocator;
855
856         bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize);
857
858         if (bufzone != NULL && dmat->alignment <= bufzone->size &&
859             !exclusion_bounce(dmat))
860                 uma_zfree(bufzone->umazone, vaddr);
861         else
862                 kmem_free(kernel_arena, (vm_offset_t)vaddr, dmat->maxsize);
863
864         dmat->map_count--;
865         if (map->flags & DMAMAP_COHERENT)
866                 atomic_subtract_32(&maps_coherent, 1);
867         atomic_subtract_32(&maps_total, 1);
868         atomic_subtract_32(&maps_dmamem, 1);
869         free(map, M_BUSDMA);
870         CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
871 }
872
873 static void
874 _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
875     bus_size_t buflen, int flags)
876 {
877         bus_addr_t curaddr;
878         bus_size_t sgsize;
879
880         if (map->pagesneeded == 0) {
881                 CTR5(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d"
882                     " map= %p, pagesneeded= %d",
883                     dmat->lowaddr, dmat->boundary, dmat->alignment,
884                     map, map->pagesneeded);
885                 /*
886                  * Count the number of bounce pages
887                  * needed in order to complete this transfer
888                  */
889                 curaddr = buf;
890                 while (buflen != 0) {
891                         sgsize = MIN(buflen, dmat->maxsegsz);
892                         if (must_bounce(dmat, map, curaddr, sgsize) != 0) {
893                                 sgsize = MIN(sgsize,
894                                     PAGE_SIZE - (curaddr & PAGE_MASK));
895                                 map->pagesneeded++;
896                         }
897                         curaddr += sgsize;
898                         buflen -= sgsize;
899                 }
900                 CTR1(KTR_BUSDMA, "pagesneeded= %d", map->pagesneeded);
901         }
902 }
903
904 static void
905 _bus_dmamap_count_pages(bus_dma_tag_t dmat, pmap_t pmap, bus_dmamap_t map,
906     void *buf, bus_size_t buflen, int flags)
907 {
908         vm_offset_t vaddr;
909         vm_offset_t vendaddr;
910         bus_addr_t paddr;
911
912         if (map->pagesneeded == 0) {
913                 CTR5(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d"
914                     " map= %p, pagesneeded= %d",
915                     dmat->lowaddr, dmat->boundary, dmat->alignment,
916                     map, map->pagesneeded);
917                 /*
918                  * Count the number of bounce pages
919                  * needed in order to complete this transfer
920                  */
921                 vaddr = (vm_offset_t)buf;
922                 vendaddr = (vm_offset_t)buf + buflen;
923
924                 while (vaddr < vendaddr) {
925                         if (__predict_true(pmap == kernel_pmap))
926                                 paddr = pmap_kextract(vaddr);
927                         else
928                                 paddr = pmap_extract(pmap, vaddr);
929                         if (must_bounce(dmat, map, paddr,
930                             min(vendaddr - vaddr, (PAGE_SIZE - ((vm_offset_t)vaddr &
931                             PAGE_MASK)))) != 0) {
932                                 map->pagesneeded++;
933                         }
934                         vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
935
936                 }
937                 CTR1(KTR_BUSDMA, "pagesneeded= %d", map->pagesneeded);
938         }
939 }
940
941 static int
942 _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int flags)
943 {
944
945         /* Reserve Necessary Bounce Pages */
946         mtx_lock(&bounce_lock);
947         if (flags & BUS_DMA_NOWAIT) {
948                 if (reserve_bounce_pages(dmat, map, 0) != 0) {
949                         map->pagesneeded = 0;
950                         mtx_unlock(&bounce_lock);
951                         return (ENOMEM);
952                 }
953         } else {
954                 if (reserve_bounce_pages(dmat, map, 1) != 0) {
955                         /* Queue us for resources */
956                         STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links);
957                         mtx_unlock(&bounce_lock);
958                         return (EINPROGRESS);
959                 }
960         }
961         mtx_unlock(&bounce_lock);
962
963         return (0);
964 }
965
966 /*
967  * Add a single contiguous physical range to the segment list.
968  */
969 static int
970 _bus_dmamap_addseg(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t curaddr,
971     bus_size_t sgsize, bus_dma_segment_t *segs, int *segp)
972 {
973         bus_addr_t baddr, bmask;
974         int seg;
975
976         /*
977          * Make sure we don't cross any boundaries.
978          */
979         bmask = ~(dmat->boundary - 1);
980         if (dmat->boundary > 0) {
981                 baddr = (curaddr + dmat->boundary) & bmask;
982                 if (sgsize > (baddr - curaddr))
983                         sgsize = (baddr - curaddr);
984         }
985
986         /*
987          * Insert chunk into a segment, coalescing with
988          * previous segment if possible.
989          */
990         seg = *segp;
991         if (seg == -1) {
992                 seg = 0;
993                 segs[seg].ds_addr = curaddr;
994                 segs[seg].ds_len = sgsize;
995         } else {
996                 if (curaddr == segs[seg].ds_addr + segs[seg].ds_len &&
997                     (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
998                     (dmat->boundary == 0 ||
999                     (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
1000                         segs[seg].ds_len += sgsize;
1001                 else {
1002                         if (++seg >= dmat->nsegments)
1003                                 return (0);
1004                         segs[seg].ds_addr = curaddr;
1005                         segs[seg].ds_len = sgsize;
1006                 }
1007         }
1008         *segp = seg;
1009         return (sgsize);
1010 }
1011
1012 /*
1013  * Utility function to load a physical buffer.  segp contains
1014  * the starting segment on entrace, and the ending segment on exit.
1015  */
1016 int
1017 _bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
1018     bus_size_t buflen, int flags, bus_dma_segment_t *segs, int *segp)
1019 {
1020         bus_addr_t curaddr;
1021         bus_addr_t sl_end = 0;
1022         bus_size_t sgsize;
1023         struct sync_list *sl;
1024         int error;
1025
1026         if (segs == NULL)
1027                 segs = map->segments;
1028
1029         counter_u64_add(maploads_total, 1);
1030         counter_u64_add(maploads_physmem, 1);
1031
1032         if (might_bounce(dmat, map, (bus_addr_t)buf, buflen)) {
1033                 _bus_dmamap_count_phys(dmat, map, buf, buflen, flags);
1034                 if (map->pagesneeded != 0) {
1035                         counter_u64_add(maploads_bounced, 1);
1036                         error = _bus_dmamap_reserve_pages(dmat, map, flags);
1037                         if (error)
1038                                 return (error);
1039                 }
1040         }
1041
1042         sl = map->slist + map->sync_count - 1;
1043
1044         while (buflen > 0) {
1045                 curaddr = buf;
1046                 sgsize = MIN(buflen, dmat->maxsegsz);
1047                 if (map->pagesneeded != 0 && must_bounce(dmat, map, curaddr,
1048                     sgsize)) {
1049                         sgsize = MIN(sgsize, PAGE_SIZE - (curaddr & PAGE_MASK));
1050                         curaddr = add_bounce_page(dmat, map, 0, curaddr,
1051                             sgsize);
1052                 } else if ((dmat->flags & BUS_DMA_COHERENT) == 0) {
1053                         if (map->sync_count > 0)
1054                                 sl_end = sl->paddr + sl->datacount;
1055
1056                         if (map->sync_count == 0 || curaddr != sl_end) {
1057                                 if (++map->sync_count > dmat->nsegments)
1058                                         break;
1059                                 sl++;
1060                                 sl->vaddr = 0;
1061                                 sl->paddr = curaddr;
1062                                 sl->datacount = sgsize;
1063                                 sl->pages = PHYS_TO_VM_PAGE(curaddr);
1064                                 KASSERT(sl->pages != NULL,
1065                                     ("%s: page at PA:0x%08lx is not in "
1066                                     "vm_page_array", __func__, curaddr));
1067                         } else
1068                                 sl->datacount += sgsize;
1069                 }
1070                 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
1071                     segp);
1072                 if (sgsize == 0)
1073                         break;
1074                 buf += sgsize;
1075                 buflen -= sgsize;
1076         }
1077
1078         /*
1079          * Did we fit?
1080          */
1081         if (buflen != 0) {
1082                 bus_dmamap_unload(dmat, map);
1083                 return (EFBIG); /* XXX better return value here? */
1084         }
1085         return (0);
1086 }
1087
1088 int
1089 _bus_dmamap_load_ma(bus_dma_tag_t dmat, bus_dmamap_t map,
1090     struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
1091     bus_dma_segment_t *segs, int *segp)
1092 {
1093
1094         return (bus_dmamap_load_ma_triv(dmat, map, ma, tlen, ma_offs, flags,
1095             segs, segp));
1096 }
1097
1098 /*
1099  * Utility function to load a linear buffer.  segp contains
1100  * the starting segment on entrance, and the ending segment on exit.
1101  */
1102 int
1103 _bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
1104     bus_size_t buflen, pmap_t pmap, int flags, bus_dma_segment_t *segs,
1105     int *segp)
1106 {
1107         bus_size_t sgsize;
1108         bus_addr_t curaddr;
1109         bus_addr_t sl_pend = 0;
1110         vm_offset_t kvaddr, vaddr, sl_vend = 0;
1111         struct sync_list *sl;
1112         int error;
1113
1114         counter_u64_add(maploads_total, 1);
1115         if (map->flags & DMAMAP_COHERENT)
1116                 counter_u64_add(maploads_coherent, 1);
1117         if (map->flags & DMAMAP_DMAMEM_ALLOC)
1118                 counter_u64_add(maploads_dmamem, 1);
1119
1120         if (segs == NULL)
1121                 segs = map->segments;
1122
1123         if (flags & BUS_DMA_LOAD_MBUF) {
1124                 counter_u64_add(maploads_mbuf, 1);
1125                 map->flags |= DMAMAP_MBUF;
1126         }
1127
1128         if (might_bounce(dmat, map, (bus_addr_t)buf, buflen)) {
1129                 _bus_dmamap_count_pages(dmat, pmap, map, buf, buflen, flags);
1130                 if (map->pagesneeded != 0) {
1131                         counter_u64_add(maploads_bounced, 1);
1132                         error = _bus_dmamap_reserve_pages(dmat, map, flags);
1133                         if (error)
1134                                 return (error);
1135                 }
1136         }
1137
1138         sl = map->slist + map->sync_count - 1;
1139         vaddr = (vm_offset_t)buf;
1140
1141         while (buflen > 0) {
1142                 /*
1143                  * Get the physical address for this segment.
1144                  */
1145                 if (__predict_true(pmap == kernel_pmap)) {
1146                         curaddr = pmap_kextract(vaddr);
1147                         kvaddr = vaddr;
1148                 } else {
1149                         curaddr = pmap_extract(pmap, vaddr);
1150                         kvaddr = 0;
1151                 }
1152
1153                 /*
1154                  * Compute the segment size, and adjust counts.
1155                  */
1156                 sgsize = PAGE_SIZE - (curaddr & PAGE_MASK);
1157                 if (sgsize > dmat->maxsegsz)
1158                         sgsize = dmat->maxsegsz;
1159                 if (buflen < sgsize)
1160                         sgsize = buflen;
1161
1162                 if (map->pagesneeded != 0 && must_bounce(dmat, map, curaddr,
1163                     sgsize)) {
1164                         curaddr = add_bounce_page(dmat, map, kvaddr, curaddr,
1165                             sgsize);
1166                 } else if ((dmat->flags & BUS_DMA_COHERENT) == 0) {
1167                         if (map->sync_count > 0) {
1168                                 sl_pend = sl->paddr + sl->datacount;
1169                                 sl_vend = sl->vaddr + sl->datacount;
1170                         }
1171
1172                         if (map->sync_count == 0 ||
1173                             (kvaddr != 0 && kvaddr != sl_vend) ||
1174                             (curaddr != sl_pend)) {
1175
1176                                 if (++map->sync_count > dmat->nsegments)
1177                                         goto cleanup;
1178                                 sl++;
1179                                 sl->vaddr = kvaddr;
1180                                 sl->paddr = curaddr;
1181                                 if (kvaddr != 0) {
1182                                         sl->pages = NULL;
1183                                 } else {
1184                                         sl->pages = PHYS_TO_VM_PAGE(curaddr);
1185                                         KASSERT(sl->pages != NULL,
1186                                             ("%s: page at PA:0x%08lx is not "
1187                                             "in vm_page_array", __func__,
1188                                             curaddr));
1189                                 }
1190                                 sl->datacount = sgsize;
1191                         } else
1192                                 sl->datacount += sgsize;
1193                 }
1194                 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
1195                     segp);
1196                 if (sgsize == 0)
1197                         break;
1198                 vaddr += sgsize;
1199                 buflen -= sgsize;
1200         }
1201
1202 cleanup:
1203         /*
1204          * Did we fit?
1205          */
1206         if (buflen != 0) {
1207                 bus_dmamap_unload(dmat, map);
1208                 return (EFBIG); /* XXX better return value here? */
1209         }
1210         return (0);
1211 }
1212
1213 void
1214 _bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map, struct memdesc *mem,
1215     bus_dmamap_callback_t *callback, void *callback_arg)
1216 {
1217
1218         map->mem = *mem;
1219         map->dmat = dmat;
1220         map->callback = callback;
1221         map->callback_arg = callback_arg;
1222 }
1223
1224 bus_dma_segment_t *
1225 _bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map,
1226     bus_dma_segment_t *segs, int nsegs, int error)
1227 {
1228
1229         if (segs == NULL)
1230                 segs = map->segments;
1231         return (segs);
1232 }
1233
1234 /*
1235  * Release the mapping held by map.
1236  */
1237 void
1238 bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
1239 {
1240         struct bounce_page *bpage;
1241         struct bounce_zone *bz;
1242
1243         if ((bz = dmat->bounce_zone) != NULL) {
1244                 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
1245                         STAILQ_REMOVE_HEAD(&map->bpages, links);
1246                         free_bounce_page(dmat, bpage);
1247                 }
1248
1249                 bz = dmat->bounce_zone;
1250                 bz->free_bpages += map->pagesreserved;
1251                 bz->reserved_bpages -= map->pagesreserved;
1252                 map->pagesreserved = 0;
1253                 map->pagesneeded = 0;
1254         }
1255         map->sync_count = 0;
1256         map->flags &= ~DMAMAP_MBUF;
1257 }
1258
1259 static void
1260 dma_preread_safe(vm_offset_t va, vm_paddr_t pa, vm_size_t size)
1261 {
1262         /*
1263          * Write back any partial cachelines immediately before and
1264          * after the DMA region.  We don't need to round the address
1265          * down to the nearest cacheline or specify the exact size,
1266          * as dcache_wb_poc() will do the rounding for us and works
1267          * at cacheline granularity.
1268          */
1269         if (va & BUSDMA_DCACHE_MASK)
1270                 dcache_wb_poc(va, pa, 1);
1271         if ((va + size) & BUSDMA_DCACHE_MASK)
1272                 dcache_wb_poc(va + size, pa + size, 1);
1273
1274         dcache_inv_poc_dma(va, pa, size);
1275 }
1276
1277 static void
1278 dma_dcache_sync(struct sync_list *sl, bus_dmasync_op_t op)
1279 {
1280         uint32_t len, offset;
1281         vm_page_t m;
1282         vm_paddr_t pa;
1283         vm_offset_t va, tempva;
1284         bus_size_t size;
1285
1286         offset = sl->paddr & PAGE_MASK;
1287         m = sl->pages;
1288         size = sl->datacount;
1289         pa = sl->paddr;
1290
1291         for ( ; size != 0; size -= len, pa += len, offset = 0, ++m) {
1292                 tempva = 0;
1293                 if (sl->vaddr == 0) {
1294                         len = min(PAGE_SIZE - offset, size);
1295                         tempva = pmap_quick_enter_page(m);
1296                         va = tempva | offset;
1297                         KASSERT(pa == (VM_PAGE_TO_PHYS(m) | offset),
1298                             ("unexpected vm_page_t phys: 0x%08x != 0x%08x",
1299                             VM_PAGE_TO_PHYS(m) | offset, pa));
1300                 } else {
1301                         len = sl->datacount;
1302                         va = sl->vaddr;
1303                 }
1304
1305                 switch (op) {
1306                 case BUS_DMASYNC_PREWRITE:
1307                 case BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD:
1308                         dcache_wb_poc(va, pa, len);
1309                         break;
1310                 case BUS_DMASYNC_PREREAD:
1311                         /*
1312                          * An mbuf may start in the middle of a cacheline. There
1313                          * will be no cpu writes to the beginning of that line
1314                          * (which contains the mbuf header) while dma is in
1315                          * progress.  Handle that case by doing a writeback of
1316                          * just the first cacheline before invalidating the
1317                          * overall buffer.  Any mbuf in a chain may have this
1318                          * misalignment.  Buffers which are not mbufs bounce if
1319                          * they are not aligned to a cacheline.
1320                          */
1321                         dma_preread_safe(va, pa, len);
1322                         break;
1323                 case BUS_DMASYNC_POSTREAD:
1324                 case BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE:
1325                         dcache_inv_poc(va, pa, len);
1326                         break;
1327                 default:
1328                         panic("unsupported combination of sync operations: "
1329                               "0x%08x\n", op);
1330                 }
1331
1332                 if (tempva != 0)
1333                         pmap_quick_remove_page(tempva);
1334         }
1335 }
1336
1337 void
1338 bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
1339 {
1340         struct bounce_page *bpage;
1341         struct sync_list *sl, *end;
1342         vm_offset_t datavaddr, tempvaddr;
1343
1344         if (op == BUS_DMASYNC_POSTWRITE)
1345                 return;
1346
1347         /*
1348          * If the buffer was from user space, it is possible that this is not
1349          * the same vm map, especially on a POST operation.  It's not clear that
1350          * dma on userland buffers can work at all right now.  To be safe, until
1351          * we're able to test direct userland dma, panic on a map mismatch.
1352          */
1353         if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
1354
1355                 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
1356                     "performing bounce", __func__, dmat, dmat->flags, op);
1357
1358                 /*
1359                  * For PREWRITE do a writeback.  Clean the caches from the
1360                  * innermost to the outermost levels.
1361                  */
1362                 if (op & BUS_DMASYNC_PREWRITE) {
1363                         while (bpage != NULL) {
1364                                 tempvaddr = 0;
1365                                 datavaddr = bpage->datavaddr;
1366                                 if (datavaddr == 0) {
1367                                         tempvaddr = pmap_quick_enter_page(
1368                                             bpage->datapage);
1369                                         datavaddr = tempvaddr | bpage->dataoffs;
1370                                 }
1371                                 bcopy((void *)datavaddr, (void *)bpage->vaddr,
1372                                     bpage->datacount);
1373                                 if (tempvaddr != 0)
1374                                         pmap_quick_remove_page(tempvaddr);
1375                                 if ((dmat->flags & BUS_DMA_COHERENT) == 0)
1376                                         dcache_wb_poc(bpage->vaddr,
1377                                             bpage->busaddr, bpage->datacount);
1378                                 bpage = STAILQ_NEXT(bpage, links);
1379                         }
1380                         dmat->bounce_zone->total_bounced++;
1381                 }
1382
1383                 /*
1384                  * Do an invalidate for PREREAD unless a writeback was already
1385                  * done above due to PREWRITE also being set.  The reason for a
1386                  * PREREAD invalidate is to prevent dirty lines currently in the
1387                  * cache from being evicted during the DMA.  If a writeback was
1388                  * done due to PREWRITE also being set there will be no dirty
1389                  * lines and the POSTREAD invalidate handles the rest. The
1390                  * invalidate is done from the innermost to outermost level. If
1391                  * L2 were done first, a dirty cacheline could be automatically
1392                  * evicted from L1 before we invalidated it, re-dirtying the L2.
1393                  */
1394                 if ((op & BUS_DMASYNC_PREREAD) && !(op & BUS_DMASYNC_PREWRITE)) {
1395                         bpage = STAILQ_FIRST(&map->bpages);
1396                         while (bpage != NULL) {
1397                                 if ((dmat->flags & BUS_DMA_COHERENT) == 0)
1398                                         dcache_inv_poc_dma(bpage->vaddr,
1399                                             bpage->busaddr, bpage->datacount);
1400                                 bpage = STAILQ_NEXT(bpage, links);
1401                         }
1402                 }
1403
1404                 /*
1405                  * Re-invalidate the caches on a POSTREAD, even though they were
1406                  * already invalidated at PREREAD time.  Aggressive prefetching
1407                  * due to accesses to other data near the dma buffer could have
1408                  * brought buffer data into the caches which is now stale.  The
1409                  * caches are invalidated from the outermost to innermost; the
1410                  * prefetches could be happening right now, and if L1 were
1411                  * invalidated first, stale L2 data could be prefetched into L1.
1412                  */
1413                 if (op & BUS_DMASYNC_POSTREAD) {
1414                         while (bpage != NULL) {
1415                                 if ((dmat->flags & BUS_DMA_COHERENT) == 0)
1416                                         dcache_inv_poc(bpage->vaddr,
1417                                             bpage->busaddr, bpage->datacount);
1418                                 tempvaddr = 0;
1419                                 datavaddr = bpage->datavaddr;
1420                                 if (datavaddr == 0) {
1421                                         tempvaddr = pmap_quick_enter_page(
1422                                             bpage->datapage);
1423                                         datavaddr = tempvaddr | bpage->dataoffs;
1424                                 }
1425                                 bcopy((void *)bpage->vaddr, (void *)datavaddr,
1426                                     bpage->datacount);
1427                                 if (tempvaddr != 0)
1428                                         pmap_quick_remove_page(tempvaddr);
1429                                 bpage = STAILQ_NEXT(bpage, links);
1430                         }
1431                         dmat->bounce_zone->total_bounced++;
1432                 }
1433         }
1434
1435         /*
1436          * For COHERENT memory no cache maintenance is necessary, but ensure all
1437          * writes have reached memory for the PREWRITE case.  No action is
1438          * needed for a PREREAD without PREWRITE also set, because that would
1439          * imply that the cpu had written to the COHERENT buffer and expected
1440          * the dma device to see that change, and by definition a PREWRITE sync
1441          * is required to make that happen.
1442          */
1443         if (map->flags & DMAMAP_COHERENT) {
1444                 if (op & BUS_DMASYNC_PREWRITE) {
1445                         dsb();
1446                         if ((dmat->flags & BUS_DMA_COHERENT) == 0)
1447                                 cpu_l2cache_drain_writebuf();
1448                 }
1449                 return;
1450         }
1451
1452         /*
1453          * Cache maintenance for normal (non-COHERENT non-bounce) buffers.  All
1454          * the comments about the sequences for flushing cache levels in the
1455          * bounce buffer code above apply here as well.  In particular, the fact
1456          * that the sequence is inner-to-outer for PREREAD invalidation and
1457          * outer-to-inner for POSTREAD invalidation is not a mistake.
1458          */
1459         if (map->sync_count != 0) {
1460                 sl = &map->slist[0];
1461                 end = &map->slist[map->sync_count];
1462                 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
1463                     "performing sync", __func__, dmat, dmat->flags, op);
1464
1465                 for ( ; sl != end; ++sl)
1466                         dma_dcache_sync(sl, op);
1467         }
1468 }
1469
1470 static void
1471 init_bounce_pages(void *dummy __unused)
1472 {
1473
1474         total_bpages = 0;
1475         STAILQ_INIT(&bounce_zone_list);
1476         STAILQ_INIT(&bounce_map_waitinglist);
1477         STAILQ_INIT(&bounce_map_callbacklist);
1478         mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
1479 }
1480 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
1481
1482 static struct sysctl_ctx_list *
1483 busdma_sysctl_tree(struct bounce_zone *bz)
1484 {
1485
1486         return (&bz->sysctl_tree);
1487 }
1488
1489 static struct sysctl_oid *
1490 busdma_sysctl_tree_top(struct bounce_zone *bz)
1491 {
1492
1493         return (bz->sysctl_tree_top);
1494 }
1495
1496 static int
1497 alloc_bounce_zone(bus_dma_tag_t dmat)
1498 {
1499         struct bounce_zone *bz;
1500
1501         /* Check to see if we already have a suitable zone */
1502         STAILQ_FOREACH(bz, &bounce_zone_list, links) {
1503                 if ((dmat->alignment <= bz->alignment) &&
1504                     (dmat->lowaddr >= bz->lowaddr)) {
1505                         dmat->bounce_zone = bz;
1506                         return (0);
1507                 }
1508         }
1509
1510         if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_BUSDMA,
1511             M_NOWAIT | M_ZERO)) == NULL)
1512                 return (ENOMEM);
1513
1514         STAILQ_INIT(&bz->bounce_page_list);
1515         bz->free_bpages = 0;
1516         bz->reserved_bpages = 0;
1517         bz->active_bpages = 0;
1518         bz->lowaddr = dmat->lowaddr;
1519         bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
1520         bz->map_count = 0;
1521         snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1522         busdma_zonecount++;
1523         snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1524         STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1525         dmat->bounce_zone = bz;
1526
1527         sysctl_ctx_init(&bz->sysctl_tree);
1528         bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1529             SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1530             CTLFLAG_RD, 0, "");
1531         if (bz->sysctl_tree_top == NULL) {
1532                 sysctl_ctx_free(&bz->sysctl_tree);
1533                 return (0);     /* XXX error code? */
1534         }
1535
1536         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1537             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1538             "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1539             "Total bounce pages");
1540         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1541             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1542             "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1543             "Free bounce pages");
1544         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1545             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1546             "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1547             "Reserved bounce pages");
1548         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1549             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1550             "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1551             "Active bounce pages");
1552         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1553             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1554             "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1555             "Total bounce requests (pages bounced)");
1556         SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1557             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1558             "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1559             "Total bounce requests that were deferred");
1560         SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1561             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1562             "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1563         SYSCTL_ADD_ULONG(busdma_sysctl_tree(bz),
1564             SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1565             "alignment", CTLFLAG_RD, &bz->alignment, "");
1566
1567         return (0);
1568 }
1569
1570 static int
1571 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1572 {
1573         struct bounce_zone *bz;
1574         int count;
1575
1576         bz = dmat->bounce_zone;
1577         count = 0;
1578         while (numpages > 0) {
1579                 struct bounce_page *bpage;
1580
1581                 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_BUSDMA,
1582                     M_NOWAIT | M_ZERO);
1583
1584                 if (bpage == NULL)
1585                         break;
1586                 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_BOUNCE,
1587                     M_NOWAIT, 0ul, bz->lowaddr, PAGE_SIZE, 0);
1588                 if (bpage->vaddr == 0) {
1589                         free(bpage, M_BUSDMA);
1590                         break;
1591                 }
1592                 bpage->busaddr = pmap_kextract(bpage->vaddr);
1593                 mtx_lock(&bounce_lock);
1594                 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1595                 total_bpages++;
1596                 bz->total_bpages++;
1597                 bz->free_bpages++;
1598                 mtx_unlock(&bounce_lock);
1599                 count++;
1600                 numpages--;
1601         }
1602         return (count);
1603 }
1604
1605 static int
1606 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1607 {
1608         struct bounce_zone *bz;
1609         int pages;
1610
1611         mtx_assert(&bounce_lock, MA_OWNED);
1612         bz = dmat->bounce_zone;
1613         pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1614         if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1615                 return (map->pagesneeded - (map->pagesreserved + pages));
1616         bz->free_bpages -= pages;
1617         bz->reserved_bpages += pages;
1618         map->pagesreserved += pages;
1619         pages = map->pagesneeded - map->pagesreserved;
1620
1621         return (pages);
1622 }
1623
1624 static bus_addr_t
1625 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1626     bus_addr_t addr, bus_size_t size)
1627 {
1628         struct bounce_zone *bz;
1629         struct bounce_page *bpage;
1630
1631         KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1632         KASSERT(map != NULL, ("add_bounce_page: bad map %p", map));
1633
1634         bz = dmat->bounce_zone;
1635         if (map->pagesneeded == 0)
1636                 panic("add_bounce_page: map doesn't need any pages");
1637         map->pagesneeded--;
1638
1639         if (map->pagesreserved == 0)
1640                 panic("add_bounce_page: map doesn't need any pages");
1641         map->pagesreserved--;
1642
1643         mtx_lock(&bounce_lock);
1644         bpage = STAILQ_FIRST(&bz->bounce_page_list);
1645         if (bpage == NULL)
1646                 panic("add_bounce_page: free page list is empty");
1647
1648         STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1649         bz->reserved_bpages--;
1650         bz->active_bpages++;
1651         mtx_unlock(&bounce_lock);
1652
1653         if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1654                 /* Page offset needs to be preserved. */
1655                 bpage->vaddr |= addr & PAGE_MASK;
1656                 bpage->busaddr |= addr & PAGE_MASK;
1657         }
1658         bpage->datavaddr = vaddr;
1659         bpage->datapage = PHYS_TO_VM_PAGE(addr);
1660         bpage->dataoffs = addr & PAGE_MASK;
1661         bpage->datacount = size;
1662         STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1663         return (bpage->busaddr);
1664 }
1665
1666 static void
1667 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1668 {
1669         struct bus_dmamap *map;
1670         struct bounce_zone *bz;
1671
1672         bz = dmat->bounce_zone;
1673         bpage->datavaddr = 0;
1674         bpage->datacount = 0;
1675         if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1676                 /*
1677                  * Reset the bounce page to start at offset 0.  Other uses
1678                  * of this bounce page may need to store a full page of
1679                  * data and/or assume it starts on a page boundary.
1680                  */
1681                 bpage->vaddr &= ~PAGE_MASK;
1682                 bpage->busaddr &= ~PAGE_MASK;
1683         }
1684
1685         mtx_lock(&bounce_lock);
1686         STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1687         bz->free_bpages++;
1688         bz->active_bpages--;
1689         if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1690                 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1691                         STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1692                         STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1693                             map, links);
1694                         busdma_swi_pending = 1;
1695                         bz->total_deferred++;
1696                         swi_sched(vm_ih, 0);
1697                 }
1698         }
1699         mtx_unlock(&bounce_lock);
1700 }
1701
1702 void
1703 busdma_swi(void)
1704 {
1705         bus_dma_tag_t dmat;
1706         struct bus_dmamap *map;
1707
1708         mtx_lock(&bounce_lock);
1709         while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1710                 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1711                 mtx_unlock(&bounce_lock);
1712                 dmat = map->dmat;
1713                 dmat->lockfunc(dmat->lockfuncarg, BUS_DMA_LOCK);
1714                 bus_dmamap_load_mem(map->dmat, map, &map->mem, map->callback,
1715                     map->callback_arg, BUS_DMA_WAITOK);
1716                 dmat->lockfunc(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1717                 mtx_lock(&bounce_lock);
1718         }
1719         mtx_unlock(&bounce_lock);
1720 }