Annotation of kernel/machdep/i386/dma_buf.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
                      3:  *
                      4:  * @APPLE_LICENSE_HEADER_START@
                      5:  * 
                      6:  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
                      7:  * Reserved.  This file contains Original Code and/or Modifications of
                      8:  * Original Code as defined in and that are subject to the Apple Public
                      9:  * Source License Version 1.1 (the "License").  You may not use this file
                     10:  * except in compliance with the License.  Please obtain a copy of the
                     11:  * License at http://www.apple.com/publicsource and read it before using
                     12:  * this file.
                     13:  * 
                     14:  * The Original Code and all software distributed under the License are
                     15:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
                     16:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
                     17:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
                     18:  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
                     19:  * License for the specific language governing rights and limitations
                     20:  * under the License.
                     21:  * 
                     22:  * @APPLE_LICENSE_HEADER_END@
                     23:  */
                     24: 
                     25: /*
                     26:  * Copyright (c) 1992 NeXT Computer, Inc.
                     27:  *
                     28:  * dma_buf.c  -- DMA buffer handling.
                     29:  *
                     30:  * HISTORY
                     31:  *
                     32:  * 26 August 1992 ? at NeXT
                     33:  *     Created.
                     34:  */
                     35: 
                     36: #import <mach/mach_types.h>
                     37: 
                     38: #import <machdep/i386/dma_buf_internal.h>
                     39: #import <machdep/i386/dma_exported.h>
                     40: 
                     41: dma_buf_type_t         dma_buf_sm, dma_buf_lg;
                     42: 
                     43: static inline int is_aligned( void *addr, unsigned int alignment )
                     44: {
                     45:     return (((unsigned int)addr & (alignment - 1)) == 0);
                     46: }
                     47: void
                     48: dma_buf_initialize(
                     49:     void
                     50: )
                     51: {
                     52:     int maxbufs = DMA_BUF_LG_LEN / DMA_BUF_SM_LEN + 1;
                     53:     dma_buf_t buf, bufs[maxbufs];
                     54:     int nbufs = 0;
                     55:     
                     56:     dma_buf_sm.create_fcn = dma_buf_sm_create;
                     57:     dma_buf_lg.create_fcn = dma_buf_lg_create;
                     58:     
                     59:     /*
                     60:      * Allocate small buffers until the next buffer
                     61:      * will be on a large-buffer boundary.
                     62:      * This makes assumptions about the way alloc_cnvmem works -- FIXME!
                     63:      */
                     64:     do {
                     65:        if (dma_buf_alloc(&buf, DMA_BUF_SM_LEN) != TRUE)
                     66:            break;
                     67:        bufs[nbufs++] = buf;
                     68:     } while (maxbufs-- && 
                     69:            !is_aligned((char *)buf._ptr+DMA_BUF_SM_LEN, DMA_BUF_LG_LEN));
                     70:     while (nbufs)
                     71:        dma_buf_free(&bufs[(nbufs--) - 1]);
                     72:     
                     73:     /*
                     74:      * Now create one large buffer.
                     75:      */
                     76:     if (dma_buf_alloc(&buf, DMA_BUF_LG_LEN) == TRUE)
                     77:        dma_buf_free(&buf);
                     78: }
                     79: 
                     80: boolean_t
                     81: dma_buf_alloc(
                     82:     dma_buf_t          *buf,
                     83:     vm_size_t          len
                     84: )
                     85: {
                     86:     dma_buf_region_t   *ptr;
                     87:     dma_buf_type_t     *buft;
                     88:     int                        s;
                     89:     
                     90:     if (len > DMA_BUF_LG_LEN)
                     91:        return (FALSE);
                     92:     else if (len > DMA_BUF_SM_LEN)
                     93:        buft = &dma_buf_lg;
                     94:     else
                     95:        buft = &dma_buf_sm;
                     96: 
                     97:     s = spldma();
                     98: 
                     99:     if (ptr = buft->free_head) {
                    100:        buft->free_head = ptr->next;
                    101:        buft->free_count--;
                    102:     }
                    103:     else {
                    104:        ptr = (*buft->create_fcn)();
                    105:        if (ptr)
                    106:            buft->total_count++;
                    107:     }
                    108: 
                    109:     splx(s);
                    110:     
                    111:     if (ptr == 0)
                    112:        return (FALSE);
                    113: 
                    114:     buf->_ptr = (void *)ptr;
                    115:     buf->_type = (void *)buft;
                    116:     
                    117:     return (TRUE);
                    118: }
                    119: 
                    120: void
                    121: dma_buf_free(
                    122:     dma_buf_t          *buf
                    123: )
                    124: {
                    125:     dma_buf_region_t   *ptr;
                    126:     dma_buf_type_t     *buft;
                    127:     int                        s;
                    128:     
                    129:     if ((ptr = (dma_buf_region_t *)buf->_ptr) == 0)
                    130:        return;
                    131:        
                    132:     buft = (dma_buf_type_t *)buf->_type;
                    133: 
                    134:     s = spldma();
                    135:     
                    136:     ptr->next = buft->free_head;
                    137:     buft->free_head = ptr;
                    138:     buft->free_count++;        
                    139:     
                    140:     splx(s);
                    141: 
                    142:     buf->_ptr = 0;
                    143:     buf->_type = 0;
                    144: }
                    145: 
                    146: static
                    147: dma_buf_region_t *
                    148: dma_buf_sm_create(
                    149:     void
                    150: )
                    151: {
                    152:     caddr_t buf = (caddr_t)alloc_cnvmem(DMA_BUF_SM_LEN, DMA_BUF_SM_LEN);
                    153:     if (buf)
                    154:        bzero(buf,DMA_BUF_SM_LEN);
                    155:     return ((dma_buf_region_t *)buf );
                    156: }
                    157: 
                    158: /*
                    159:  * Eventually, this should allocate small buffers until we get
                    160:  * to a large buffer alignment boundary.
                    161:  */
                    162:  
                    163: static
                    164: dma_buf_region_t *
                    165: dma_buf_lg_create(
                    166:     void
                    167: )
                    168: {
                    169:     caddr_t buf = (caddr_t)alloc_cnvmem(DMA_BUF_LG_LEN, DMA_BUF_LG_LEN);
                    170:     if (buf)
                    171:        bzero(buf,DMA_BUF_LG_LEN);
                    172:     return ((dma_buf_region_t *)buf );
                    173: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.