]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/sun4v/sun4v/bus_machdep.c
MFC r362623:
[FreeBSD/stable/8.git] / sys / sun4v / sun4v / bus_machdep.c
1 /*-
2  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7  * NASA Ames Research Center.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*-
31  * Copyright (c) 1992, 1993
32  *      The Regents of the University of California.  All rights reserved.
33  *
34  * This software was developed by the Computer Systems Engineering group
35  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
36  * contributed to Berkeley.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 4. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 /*-
63  * Copyright (c) 1997, 1998 Justin T. Gibbs.
64  * All rights reserved.
65  * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.  All rights reserved.
66  *
67  * Redistribution and use in source and binary forms, with or without
68  * modification, are permitted provided that the following conditions
69  * are met:
70  * 1. Redistributions of source code must retain the above copyright
71  *    notice, this list of conditions, and the following disclaimer,
72  *    without modification, immediately at the beginning of the file.
73  * 2. The name of the author may not be used to endorse or promote products
74  *    derived from this software without specific prior written permission.
75  *
76  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
77  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
79  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
80  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
82  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
83  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
84  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
85  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
86  * SUCH DAMAGE.
87  *
88  *      from: @(#)machdep.c     8.6 (Berkeley) 1/14/94
89  *      from: NetBSD: machdep.c,v 1.221 2008/04/28 20:23:37 martin Exp
90  *      and
91  *      from: FreeBSD: src/sys/i386/i386/busdma_machdep.c,v 1.24 2001/08/15
92  */
93
94 #include <sys/cdefs.h>
95 __FBSDID("$FreeBSD$");
96
97 #include <sys/param.h>
98 #include <sys/bus.h>
99 #include <sys/lock.h>
100 #include <sys/malloc.h>
101 #include <sys/mbuf.h>
102 #include <sys/mutex.h>
103 #include <sys/proc.h>
104 #include <sys/smp.h>
105 #include <sys/systm.h>
106 #include <sys/uio.h>
107
108 #include <vm/vm.h>
109 #include <vm/vm_extern.h>
110 #include <vm/vm_kern.h>
111 #include <vm/vm_page.h>
112 #include <vm/vm_param.h>
113 #include <vm/vm_map.h>
114
115 #include <machine/asi.h>
116 #include <machine/atomic.h>
117 #include <machine/bus.h>
118 #include <machine/bus_private.h>
119 #include <machine/smp.h>
120 #include <machine/tlb.h>
121
122 static void nexus_bus_barrier(bus_space_tag_t, bus_space_handle_t,
123     bus_size_t, bus_size_t, int);
124
125 /* ASIs for bus access */
126 const int bus_type_asi[] = {
127         ASI_REAL_IO,            /* nexus */
128         ASI_REAL_IO,            /* SBus */
129         ASI_REAL_IO_L,          /* PCI configuration space */
130         ASI_REAL_IO_L,          /* PCI memory space */
131         ASI_REAL_IO_L,          /* PCI I/O space */
132         0
133 };
134
135 const int bus_stream_asi[] = {
136         ASI_REAL_IO,            /* nexus */
137         ASI_REAL_IO,            /* SBus */
138         ASI_REAL_IO,            /* PCI configuration space */
139         ASI_REAL_IO,            /* PCI memory space */
140         ASI_REAL_IO,            /* PCI I/O space */
141         0
142 };
143
144 /*
145  * Convenience function for manipulating driver locks from busdma (during
146  * busdma_swi, for example).  Drivers that don't provide their own locks
147  * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
148  * non-mutex locking scheme don't have to use this at all.
149  */
150 void
151 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
152 {
153         struct mtx *dmtx;
154
155         dmtx = (struct mtx *)arg;
156         switch (op) {
157         case BUS_DMA_LOCK:
158                 mtx_lock(dmtx);
159                 break;
160         case BUS_DMA_UNLOCK:
161                 mtx_unlock(dmtx);
162                 break;
163         default:
164                 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
165         }
166 }
167
168 /*
169  * dflt_lock should never get called.  It gets put into the dma tag when
170  * lockfunc == NULL, which is only valid if the maps that are associated
171  * with the tag are meant to never be defered.
172  * XXX Should have a way to identify which driver is responsible here.
173  */
174 static void
175 dflt_lock(void *arg, bus_dma_lock_op_t op)
176 {
177
178         panic("driver error: busdma dflt_lock called");
179 }
180
181 /*
182  * Since there is no way for a device to obtain a dma tag from its parent
183  * we use this kluge to handle different the different supported bus systems.
184  * The sparc64_root_dma_tag is used as parent for tags that have none, so that
185  * the correct methods will be used.
186  */
187 bus_dma_tag_t sparc64_root_dma_tag;
188
189 /*
190  * Allocate a device specific dma_tag.
191  */
192 int
193 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
194     bus_size_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
195     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
196     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
197     void *lockfuncarg, bus_dma_tag_t *dmat)
198 {
199         bus_dma_tag_t impptag;
200         bus_dma_tag_t newtag;
201
202         /* Return a NULL tag on failure */
203         *dmat = NULL;
204
205         newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, M_NOWAIT);
206         if (newtag == NULL)
207                 return (ENOMEM);
208
209         impptag = parent != NULL ? parent : sparc64_root_dma_tag;
210         /*
211          * The method table pointer and the cookie need to be taken over from
212          * the parent or the root tag.
213          */
214         newtag->dt_cookie = impptag->dt_cookie;
215         newtag->dt_mt = impptag->dt_mt;
216
217         newtag->dt_parent = parent;
218         newtag->dt_alignment = alignment;
219         newtag->dt_boundary = boundary;
220         newtag->dt_lowaddr = trunc_page((vm_offset_t)lowaddr) + (PAGE_SIZE - 1);
221         newtag->dt_highaddr = trunc_page((vm_offset_t)highaddr) +
222             (PAGE_SIZE - 1);
223         newtag->dt_filter = filter;
224         newtag->dt_filterarg = filterarg;
225         newtag->dt_maxsize = maxsize;
226         newtag->dt_nsegments = nsegments;
227         newtag->dt_maxsegsz = maxsegsz;
228         newtag->dt_flags = flags;
229         newtag->dt_ref_count = 1; /* Count ourselves */
230         newtag->dt_map_count = 0;
231
232         if (lockfunc != NULL) {
233                 newtag->dt_lockfunc = lockfunc;
234                 newtag->dt_lockfuncarg = lockfuncarg;
235         } else {
236                 newtag->dt_lockfunc = dflt_lock;
237                 newtag->dt_lockfuncarg = NULL;
238         }
239
240         newtag->dt_segments = NULL;
241
242         /* Take into account any restrictions imposed by our parent tag. */
243         if (parent != NULL) {
244                 newtag->dt_lowaddr = ulmin(parent->dt_lowaddr,
245                     newtag->dt_lowaddr);
246                 newtag->dt_highaddr = ulmax(parent->dt_highaddr,
247                     newtag->dt_highaddr);
248                 if (newtag->dt_boundary == 0)
249                         newtag->dt_boundary = parent->dt_boundary;
250                 else if (parent->dt_boundary != 0)
251                         newtag->dt_boundary = ulmin(parent->dt_boundary,
252                             newtag->dt_boundary);
253                 atomic_add_int(&parent->dt_ref_count, 1);
254         }
255
256         if (newtag->dt_boundary > 0)
257                 newtag->dt_maxsegsz = ulmin(newtag->dt_maxsegsz,
258                     newtag->dt_boundary);
259
260         *dmat = newtag;
261         return (0);
262 }
263
264 int
265 bus_dma_tag_destroy(bus_dma_tag_t dmat)
266 {
267         bus_dma_tag_t parent;
268
269         if (dmat != NULL) {
270                 if (dmat->dt_map_count != 0)
271                         return (EBUSY);
272                 while (dmat != NULL) {
273                         parent = dmat->dt_parent;
274                         atomic_subtract_int(&dmat->dt_ref_count, 1);
275                         if (dmat->dt_ref_count == 0) {
276                                 if (dmat->dt_segments != NULL)
277                                         free(dmat->dt_segments, M_DEVBUF);
278                                 free(dmat, M_DEVBUF);
279                                 /*
280                                  * Last reference count, so
281                                  * release our reference
282                                  * count on our parent.
283                                  */
284                                 dmat = parent;
285                         } else
286                                 dmat = NULL;
287                 }
288         }
289         return (0);
290 }
291
292 /* Allocate/free a tag, and do the necessary management work. */
293 int
294 sparc64_dma_alloc_map(bus_dma_tag_t dmat, bus_dmamap_t *mapp)
295 {
296
297         if (dmat->dt_segments == NULL) {
298                 dmat->dt_segments = (bus_dma_segment_t *)malloc(
299                     sizeof(bus_dma_segment_t) * dmat->dt_nsegments, M_DEVBUF,
300                     M_NOWAIT);
301                 if (dmat->dt_segments == NULL)
302                         return (ENOMEM);
303         }
304         *mapp = malloc(sizeof(**mapp), M_DEVBUF, M_NOWAIT | M_ZERO);
305         if (*mapp == NULL)
306                 return (ENOMEM);
307
308         SLIST_INIT(&(*mapp)->dm_reslist);
309         dmat->dt_map_count++;
310         return (0);
311 }
312
313 void
314 sparc64_dma_free_map(bus_dma_tag_t dmat, bus_dmamap_t map)
315 {
316
317         free(map, M_DEVBUF);
318         dmat->dt_map_count--;
319 }
320
321 static int
322 nexus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
323 {
324
325         return (sparc64_dma_alloc_map(dmat, mapp));
326 }
327
328 static int
329 nexus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
330 {
331
332         sparc64_dma_free_map(dmat, map);
333         return (0);
334 }
335
336 /*
337  * Utility function to load a linear buffer.  lastaddrp holds state
338  * between invocations (for multiple-buffer loads).  segp contains
339  * the starting segment on entrace, and the ending segment on exit.
340  * first indicates if this is the first invocation of this function.
341  */
342 static int
343 _nexus_dmamap_load_buffer(bus_dma_tag_t dmat, void *buf, bus_size_t buflen,
344     struct thread *td, int flags, bus_addr_t *lastaddrp,
345     bus_dma_segment_t *segs, int *segp, int first)
346 {
347         bus_size_t sgsize;
348         bus_addr_t curaddr, lastaddr, baddr, bmask;
349         vm_offset_t vaddr = (vm_offset_t)buf;
350         int seg;
351         pmap_t pmap;
352
353         if (td != NULL)
354                 pmap = vmspace_pmap(td->td_proc->p_vmspace);
355         else
356                 pmap = NULL;
357
358         lastaddr = *lastaddrp;
359         bmask  = ~(dmat->dt_boundary - 1);
360
361         for (seg = *segp; buflen > 0 ; ) {
362                 /*
363                  * Get the physical address for this segment.
364                  */
365                 if (pmap)
366                         curaddr = pmap_extract(pmap, vaddr);
367                 else
368                         curaddr = pmap_kextract(vaddr);
369
370                 /*
371                  * Compute the segment size, and adjust counts.
372                  */
373                 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
374                 if (sgsize > dmat->dt_maxsegsz)
375                         sgsize = dmat->dt_maxsegsz;
376                 if (buflen < sgsize)
377                         sgsize = buflen;
378
379                 /*
380                  * Make sure we don't cross any boundaries.
381                  */
382                 if (dmat->dt_boundary > 0) {
383                         baddr = (curaddr + dmat->dt_boundary) & bmask;
384                         if (sgsize > (baddr - curaddr))
385                                 sgsize = (baddr - curaddr);
386                 }
387
388                 /*
389                  * Insert chunk into a segment, coalescing with
390                  * previous segment if possible.
391                  */
392                 if (first) {
393                         segs[seg].ds_addr = curaddr;
394                         segs[seg].ds_len = sgsize;
395                         first = 0;
396                 } else {
397                         if (curaddr == lastaddr &&
398                             (segs[seg].ds_len + sgsize) <= dmat->dt_maxsegsz &&
399                             (dmat->dt_boundary == 0 ||
400                             (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
401                                 segs[seg].ds_len += sgsize;
402                         else {
403                                 if (++seg >= dmat->dt_nsegments)
404                                         break;
405                                 segs[seg].ds_addr = curaddr;
406                                 segs[seg].ds_len = sgsize;
407                         }
408                 }
409
410                 lastaddr = curaddr + sgsize;
411                 vaddr += sgsize;
412                 buflen -= sgsize;
413         }
414
415         *segp = seg;
416         *lastaddrp = lastaddr;
417
418         /*
419          * Did we fit?
420          */
421         return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
422 }
423
424 /*
425  * Common function for loading a DMA map with a linear buffer.  May
426  * be called by bus-specific DMA map load functions.
427  *
428  * Most SPARCs have IOMMUs in the bus controllers.  In those cases
429  * they only need one segment and will use virtual addresses for DVMA.
430  * Those bus controllers should intercept these vectors and should
431  * *NEVER* call nexus_dmamap_load() which is used only by devices that
432  * bypass DVMA.
433  */
434 static int
435 nexus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
436     bus_size_t buflen, bus_dmamap_callback_t *callback, void *callback_arg,
437     int flags)
438 {
439         bus_addr_t lastaddr;
440         int error, nsegs;
441
442         error = _nexus_dmamap_load_buffer(dmat, buf, buflen, NULL, flags,
443             &lastaddr, dmat->dt_segments, &nsegs, 1);
444
445         if (error == 0) {
446                 (*callback)(callback_arg, dmat->dt_segments, nsegs + 1, 0);
447                 map->dm_flags |= DMF_LOADED;
448         } else
449                 (*callback)(callback_arg, NULL, 0, error);
450
451         return (0);
452 }
453
454 /*
455  * Like nexus_dmamap_load(), but for mbufs.
456  */
457 static int
458 nexus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
459     bus_dmamap_callback2_t *callback, void *callback_arg, int flags)
460 {
461         int nsegs, error;
462
463         M_ASSERTPKTHDR(m0);
464
465         nsegs = 0;
466         error = 0;
467         if (m0->m_pkthdr.len <= dmat->dt_maxsize) {
468                 int first = 1;
469                 bus_addr_t lastaddr = 0;
470                 struct mbuf *m;
471
472                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
473                         if (m->m_len > 0) {
474                                 error = _nexus_dmamap_load_buffer(dmat,
475                                     m->m_data, m->m_len,NULL, flags, &lastaddr,
476                                     dmat->dt_segments, &nsegs, first);
477                                 first = 0;
478                         }
479                 }
480         } else {
481                 error = EINVAL;
482         }
483
484         if (error) {
485                 /* force "no valid mappings" in callback */
486                 (*callback)(callback_arg, dmat->dt_segments, 0, 0, error);
487         } else {
488                 map->dm_flags |= DMF_LOADED;
489                 (*callback)(callback_arg, dmat->dt_segments, nsegs + 1,
490                     m0->m_pkthdr.len, error);
491         }
492         return (error);
493 }
494
495 static int
496 nexus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
497     bus_dma_segment_t *segs, int *nsegs, int flags)
498 {
499         int error;
500
501         M_ASSERTPKTHDR(m0);
502
503         *nsegs = 0;
504         error = 0;
505         if (m0->m_pkthdr.len <= dmat->dt_maxsize) {
506                 int first = 1;
507                 bus_addr_t lastaddr = 0;
508                 struct mbuf *m;
509
510                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
511                         if (m->m_len > 0) {
512                                 error = _nexus_dmamap_load_buffer(dmat,
513                                     m->m_data, m->m_len,NULL, flags, &lastaddr,
514                                     segs, nsegs, first);
515                                 first = 0;
516                         }
517                 }
518         } else {
519                 error = EINVAL;
520         }
521
522         ++*nsegs;
523         return (error);
524 }
525
526 /*
527  * Like nexus_dmamap_load(), but for uios.
528  */
529 static int
530 nexus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio,
531     bus_dmamap_callback2_t *callback, void *callback_arg, int flags)
532 {
533         bus_addr_t lastaddr;
534         int nsegs, error, first, i;
535         bus_size_t resid;
536         struct iovec *iov;
537         struct thread *td = NULL;
538
539         resid = uio->uio_resid;
540         iov = uio->uio_iov;
541
542         if (uio->uio_segflg == UIO_USERSPACE) {
543                 td = uio->uio_td;
544                 KASSERT(td != NULL, ("%s: USERSPACE but no proc", __func__));
545         }
546
547         nsegs = 0;
548         error = 0;
549         first = 1;
550         for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
551                 /*
552                  * Now at the first iovec to load.  Load each iovec
553                  * until we have exhausted the residual count.
554                  */
555                 bus_size_t minlen =
556                         resid < iov[i].iov_len ? resid : iov[i].iov_len;
557                 caddr_t addr = (caddr_t) iov[i].iov_base;
558
559                 if (minlen > 0) {
560                         error = _nexus_dmamap_load_buffer(dmat, addr, minlen,
561                             td, flags, &lastaddr, dmat->dt_segments, &nsegs,
562                             first);
563                         first = 0;
564
565                         resid -= minlen;
566                 }
567         }
568
569         if (error) {
570                 /* force "no valid mappings" in callback */
571                 (*callback)(callback_arg, dmat->dt_segments, 0, 0, error);
572         } else {
573                 map->dm_flags |= DMF_LOADED;
574                 (*callback)(callback_arg, dmat->dt_segments, nsegs + 1,
575                     uio->uio_resid, error);
576         }
577         return (error);
578 }
579
580 /*
581  * Common function for unloading a DMA map.  May be called by
582  * bus-specific DMA map unload functions.
583  */
584 static void
585 nexus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
586 {
587
588         map->dm_flags &= ~DMF_LOADED;
589 }
590
591 /*
592  * Common function for DMA map synchronization.  May be called
593  * by bus-specific DMA map synchronization functions.
594  */
595 static void
596 nexus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
597 {
598
599         /*
600          * We sync out our caches, but the bus must do the same.
601          *
602          * Actually a #Sync is expensive.  We should optimize.
603          */
604         if ((op & BUS_DMASYNC_PREREAD) || (op & BUS_DMASYNC_PREWRITE)) {
605                 /*
606                  * Don't really need to do anything, but flush any pending
607                  * writes anyway.
608                  */
609                 membar(Sync);
610         }
611 #if 0
612         /* Should not be needed. */
613         if (op & BUS_DMASYNC_POSTREAD) {
614                 ecache_flush((vm_offset_t)map->buf,
615                     (vm_offset_t)map->buf + map->buflen - 1);
616         }
617 #endif
618         if (op & BUS_DMASYNC_POSTWRITE) {
619                 /* Nothing to do.  Handled by the bus controller. */
620         }
621 }
622
623 /*
624  * Common function for DMA-safe memory allocation.  May be called
625  * by bus-specific DMA memory allocation functions.
626  */
627 static int
628 nexus_dmamem_alloc(bus_dma_tag_t dmat, void **vaddr, int flags,
629     bus_dmamap_t *mapp)
630 {
631         int mflags;
632
633         if (flags & BUS_DMA_NOWAIT)
634                 mflags = M_NOWAIT;
635         else
636                 mflags = M_WAITOK;
637         if (flags & BUS_DMA_ZERO)
638                 mflags |= M_ZERO;
639
640         /*
641          * XXX:
642          * (dmat->dt_alignment < dmat->dt_maxsize) is just a quick hack; the
643          * exact alignment guarantees of malloc need to be nailed down, and
644          * the code below should be rewritten to take that into account.
645          *
646          * In the meantime, we'll warn the user if malloc gets it wrong.
647          */
648         if (dmat->dt_maxsize <= PAGE_SIZE &&
649             dmat->dt_alignment < dmat->dt_maxsize)
650                 *vaddr = malloc(dmat->dt_maxsize, M_DEVBUF, mflags);
651         else {
652                 /*
653                  * XXX use contigmalloc until it is merged into this
654                  * facility and handles multi-seg allocations.  Nobody
655                  * is doing multi-seg allocations yet though.
656                  */
657                 *vaddr = contigmalloc(dmat->dt_maxsize, M_DEVBUF, mflags,
658                     0ul, dmat->dt_lowaddr,
659                     dmat->dt_alignment ? dmat->dt_alignment : 1UL,
660                     dmat->dt_boundary);
661         }
662         if (*vaddr == NULL)
663                 return (ENOMEM);
664         if ((uintptr_t)*vaddr % dmat->dt_alignment)
665                 printf("%s: failed to align memory properly.\n", __func__);
666         return (0);
667 }
668
669 /*
670  * Common function for freeing DMA-safe memory.  May be called by
671  * bus-specific DMA memory free functions.
672  */
673 static void
674 nexus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
675 {
676
677         if (dmat->dt_maxsize <= PAGE_SIZE &&
678             dmat->dt_alignment < dmat->dt_maxsize)
679                 free(vaddr, M_DEVBUF);
680         else
681                 contigfree(vaddr, dmat->dt_maxsize, M_DEVBUF);
682 }
683
684 struct bus_dma_methods nexus_dma_methods = {
685         nexus_dmamap_create,
686         nexus_dmamap_destroy,
687         nexus_dmamap_load,
688         nexus_dmamap_load_mbuf,
689         nexus_dmamap_load_mbuf_sg,
690         nexus_dmamap_load_uio,
691         nexus_dmamap_unload,
692         nexus_dmamap_sync,
693         nexus_dmamem_alloc,
694         nexus_dmamem_free,
695 };
696
697 struct bus_dma_tag nexus_dmatag = {
698         NULL,
699         NULL,
700         8,
701         0,
702         0,
703         0x3ffffffff,
704         NULL,           /* XXX */
705         NULL,
706         0x3ffffffff,    /* XXX */
707         0xff,           /* XXX */
708         0xffffffff,     /* XXX */
709         0,
710         0,
711         0,
712         NULL,
713         NULL,
714         NULL,
715         &nexus_dma_methods,
716 };
717
718 /*
719  * Helpers to map/unmap bus memory
720  */
721 int
722 sparc64_bus_mem_map(bus_space_tag_t tag, bus_space_handle_t handle,
723     bus_size_t size, int flags, vm_offset_t vaddr, void **hp)
724 {
725 #if 0
726         vm_offset_t addr;
727         vm_offset_t sva;
728         vm_offset_t va;
729         vm_paddr_t pa;
730         vm_size_t vsz;
731         u_long pm_flags;
732
733         addr = (vm_offset_t)handle;
734         size = round_page(size);
735         if (size == 0) {
736                 printf("%s: zero size\n", __func__);
737                 return (EINVAL);
738         }
739         switch (tag->bst_type) {
740         case PCI_CONFIG_BUS_SPACE:
741         case PCI_IO_BUS_SPACE:
742         case PCI_MEMORY_BUS_SPACE:
743                 pm_flags = TD_IE;
744                 break;
745         default:
746                 pm_flags = 0;
747                 break;
748         }
749
750         if (!(flags & BUS_SPACE_MAP_CACHEABLE))
751                 pm_flags |= TD_E;
752
753         if (vaddr != 0L)
754                 sva = trunc_page(vaddr);
755         else {
756                 if ((sva = kmem_alloc_nofault(kernel_map, size)) == 0)
757                         panic("%s: cannot allocate virtual memory", __func__);
758         }
759
760         /* Preserve page offset. */
761         *hp = (void *)(sva | ((u_long)addr & PAGE_MASK));
762
763         pa = trunc_page(addr);
764         if ((flags & BUS_SPACE_MAP_READONLY) == 0)
765                 pm_flags |= TD_W;
766
767         va = sva;
768         vsz = size;
769         do {
770                 pmap_kenter_flags(va, pa, pm_flags);
771                 va += PAGE_SIZE;
772                 pa += PAGE_SIZE;
773         } while ((vsz -= PAGE_SIZE) > 0);
774         tlb_range_demap(kernel_pmap, sva, sva + size - 1);
775 #endif
776         return (0);
777 }
778
779 int
780 sparc64_bus_mem_unmap(void *bh, bus_size_t size)
781 {
782 #if 0
783         vm_offset_t sva;
784         vm_offset_t va;
785         vm_offset_t endva;
786
787         sva = trunc_page((vm_offset_t)bh);
788         endva = sva + round_page(size);
789         for (va = sva; va < endva; va += PAGE_SIZE)
790                 pmap_kremove_flags(va);
791         tlb_range_demap(kernel_pmap, sva, sva + size - 1);
792         kmem_free(kernel_map, sva, size);
793 #endif
794         return (0);
795 }
796
797 /*
798  * Fake up a bus tag, for use by console drivers in early boot when the
799  * regular means to allocate resources are not yet available.
800  * Addr is the physical address of the desired start of the handle.
801  */
802 bus_space_handle_t
803 sparc64_fake_bustag(int space, bus_addr_t addr, struct bus_space_tag *ptag)
804 {
805
806         ptag->bst_cookie = NULL;
807         ptag->bst_parent = NULL;
808         ptag->bst_type = space;
809         ptag->bst_bus_barrier = nexus_bus_barrier;
810         return (addr);
811 }
812
813 /*
814  * Base bus space handlers.
815  */
816
817 static void
818 nexus_bus_barrier(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset,
819     bus_size_t size, int flags)
820 {
821
822         /*
823          * We have lots of alternatives depending on whether we're
824          * synchronizing loads with loads, loads with stores, stores
825          * with loads, or stores with stores.  The only ones that seem
826          * generic are #Sync and #MemIssue.  I'll use #Sync for safety.
827          */
828         switch(flags) {
829         case BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE:
830         case BUS_SPACE_BARRIER_READ:
831         case BUS_SPACE_BARRIER_WRITE:
832                 membar(Sync);
833                 break;
834         default:
835                 panic("%s: unknown flags", __func__);
836         }
837         return;
838 }
839
840 struct bus_space_tag nexus_bustag = {
841         NULL,                           /* cookie */
842         NULL,                           /* parent bus tag */
843         NEXUS_BUS_SPACE,                /* type */
844         nexus_bus_barrier,              /* bus_space_barrier */
845 };