/* $Id: dma.c,v 1.5 1992/11/18 02:49:05 root Exp root $
 * linux/kernel/dma.c: A DMA channel allocator. Inspired by linux/kernel/irq.c.
 * Written by Hennus Bergman, 1992.
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <asm/dma.h>

/* A note on resource allocation:
 *
 * All drivers needing DMA channels, should allocate and release them
 * through the public routines `request_dma()' and `free_dma()'.
 *
 * In order to avoid problems, all processes should allocate resources in
 * the same sequence and release them in the reverse order.
 *
 * So, when allocating DMAs and IRQs, first allocate the IRQ, then the DMA.
 * When releasing them, first release the DMA, then release the IRQ.
 * If you don't, you may cause allocation requests to fail unnecessarily.
 * This doesn't really matter now, but it will once we get real semaphores
 * in the kernel.
 */

/* Channel n is busy iff dma_chan_busy[n] != 0.
 * DMA0 is reserved for DRAM refresh, I think.
 * DMA4 is reserved for cascading (?).
 */
 /* dma_chan_busy[n] != 0 表示该通道不可用
 * DMA0 用作DRAM的刷新.
 * DMA4 用作级连.
 */
static volatile unsigned int dma_chan_busy[MAX_DMA_CHANNELS] = {
    1, 0, 0, 0, 1, 0, 0, 0
};

/* Atomically swap memory location [32 bits] with `newval'.
 * This avoid the cli()/sti() junk and related problems.
 * [And it's faster too :-)]
 * Maybe this should be in include/asm/mutex.h and be used for
 * implementing kernel-semaphores as well.
 */
 // 将*p指向的数据替换为newval.由关键字xchgl看,这个值是32位的值。
 // xchgl是原子操作,可以省去cli和sti.
static __inline__ unsigned int mutex_atomic_swap(volatile unsigned int * p, unsigned int newval)
{
    unsigned int semval = newval;

/* If one of the operands for the XCHG instructions is a memory ref,
     * it makes the swap an uninterruptible RMW cycle.
     *
     * One operand must be in memory, the other in a register, otherwise
     * the swap may not be atomic.
     */

asm __volatile__ ("xchgl %2, %0\n"
            : /* outputs: semval   */ "=r" (semval)
            : /* inputs: newval, p */ "0" (semval), "m" (*p)
            );    /* p is a var, containing an address */
    return semval;
} /* mutex_atomic_swap */

//请求DMA,核心由上个函数mutex_atomic_swap完成
int request_dma(unsigned int dmanr)
{
    if (dmanr >= MAX_DMA_CHANNELS)
        return -EINVAL;

if (mutex_atomic_swap(&dma_chan_busy[dmanr], 1) != 0)
        return -EBUSY;
    else
        /* old flag was 0, now contains 1 to indicate busy */
        return 0;
} /* request_dma */

//释放DMA,核心由函数mutex_atomic_swap完成
void free_dma(unsigned int dmanr)
{
    if (dmanr >= MAX_DMA_CHANNELS) {
        printk("Trying to free DMA%d\n", dmanr);
        return;
    }

if (mutex_atomic_swap(&dma_chan_busy[dmanr], 0) == 0)
        printk("Trying to free free DMA%d\n", dmanr);
} /* free_dma */

kernel/dma.c的更多相关文章

  1. include/asm/dma.h

    /* $Id: dma.h,v 1.7 1992/12/14 00:29:34 root Exp root $ * linux/include/asm/dma.h: Defines for using ...

  2. iommu分析之---DMA remap框架实现

    本文主要介绍iommu的框架.基于4.19.204内核 IOMMU核心框架是管理IOMMU设备的一个通过框架,IOMMU设备通过实现特定的回调函数并将自身注册到IOMMU核心框架中,以此通过IOMMU ...

  3. Linux服务器宕机案例一则

    案例环境 操作系统 :Oracle Linux Server release 5.7 64bit 虚拟机 硬件配置 : 物理机型号为DELL R720 资源配置 :RAM 8G Intel(R) Xe ...

  4. Linux系统OOM killer机制详解

    介绍: Linux下面有个特性叫OOM killer(Out Of Memory killer),会在系统内存耗尽的情况下出现,选择性的干掉一些进程以求释放一些内存.广大从事Linux方面的IT农民工 ...

  5. VNF网络性能提升解决方案及实践

    VNF网络性能提升解决方案及实践 2016年7月 作者:    王智民 贡献者:     创建时间:    2016-7-20 稳定程度:    初稿 修改历史 版本 日期 修订人 说明 1.0 20 ...

  6. linux驱动(续)

    网络通信 --> IO多路复用之select.poll.epoll详解 IO多路复用之select.poll.epoll详解      目前支持I/O多路复用的系统调用有 select,psel ...

  7. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  8. 【内核】linux内核启动流程详细分析

    Linux内核启动流程 arch/arm/kernel/head-armv.S 该文件是内核最先执行的一个文件,包括内核入口ENTRY(stext)到start_kernel间的初始化代码, 主要作用 ...

  9. 【内核】linux内核启动流程详细分析【转】

    转自:http://www.cnblogs.com/lcw/p/3337937.html Linux内核启动流程 arch/arm/kernel/head-armv.S 该文件是内核最先执行的一个文件 ...

随机推荐

  1. Win环境下的文件读写

    在win环境下,有许多方法可以对文件进行读写操作,如MFC 中的CFile类,及一些开源的项目如QT中的QFile.开源的好得是可以多平台,而MFC只是微软自家的东西,对于想写跨平台的人,最好不用MF ...

  2. 50个C/C++源代码网站

    C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些网站提供c/c++源代码 .这份清单提供了源代码的链接以及它们的小说明.我已尽力包括最佳的C/C++源代码的网站.这不是一个完整的 ...

  3. hypermesh2flac3d

    hypermesh2ansys2flac3d 目的: 将hypermesh中划分的网格输出到flac3d中.过程是hypermesh12.0-ansys13.0-flac3d3.0. 视频教程详见:h ...

  4. android工程中menu的使用总结

    android的Menu使用 转自:thinkYeah的博客 1.普通的Menu 先来看看最简单的Menu怎样实现. 在主Activity中覆盖onCreateOptionsMenu(Menu men ...

  5. Java泛型学习笔记 - (四)有界类型参数

    1. 当我们希望对泛型的类型参数的类型进行限制的时候(好拗口), 我们就应该使用有界类型参数(Bounded Type Parameters). 有界类型参数使用extends关键字后面接上边界类型来 ...

  6. build and set proxy in Ubuntu

    build http://www.2cto.com/os/201310/249690.html set http://www.360doc.com/content/11/1112/00/2617151 ...

  7. linux下查看进程状态

    1.查看进程的启动时间和执行时间 使用 ps 命令 :#ps -A -opid,stime,etime,args $ ps -A -opid,stime,etime,args PID STIME EL ...

  8. 快速了解IOC的几种姿势

    一.首先我们了解IOC如何注入的几种姿势 构造函数注入(Constructor Injection) Ioc容器会智能的选择和调用合适的构造函数以创建依赖的对象.如果被选择的构造函数具有相应的参数,I ...

  9. .Net读取Excel文件时丢失数据的问题 (转载)

    相信很多人都试过通过OleDB读取Excel文件,这种方法效率十分高,只是有一点会让人十分头痛,就是当一列中既有混合型数据,又有纯数据时,往往容易丢失数据. 百度过后,改连接字符串 “HDR=YES; ...

  10. 排序系列 之 简单选择排序及其改进算法 —— Java实现

    简单选择排序算法: 基本思想: 在待排序数据中,选出最小的一个数与第一个位置的数交换:然后在剩下的数中选出最小的数与第二个数交换:依次类推,直至循环到只剩下两个数进行比较为止. 实例: 0.初始状态 ...