履歴: 電気回路/zynq/DMA処理

各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 12 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます

日時タイトル文字数追加削除内容差分基準
2017/2/13 17:38:01電気回路/zynq/DMA処理 最新版40,681+1 -1表示差分—
2017/2/13 17:38:01電気回路/zynq/DMA処理 40,687+3 -0表示差分選択
2017/2/13 8:38:01電気回路/zynq/DMA処理 40,646+4 -3表示差分選択
2017/2/13 8:38:01電気回路/zynq/DMA処理 40,665+7 -5表示差分選択
2017/2/13 8:38:01電気回路/zynq/DMA処理 40,604+126 -53表示差分選択
2017/2/13 8:07:15電気回路/zynq/DMA処理 38,696+221 -6表示表示中選択
2017/2/12 17:58:52電気回路/zynq/DMA処理 32,687+229 -286表示差分選択
2017/2/12 13:35:09電気回路/zynq/DMA処理 32,574+307 -59表示差分選択
2017/2/11 18:07:45電気回路/zynq/DMA処理 24,593+10 -1表示差分選択
2017/2/11 18:07:45電気回路/zynq/DMA処理 24,305+235 -9表示差分選択
2017/2/11 9:49:18電気回路/zynq/DMA処理 17,700+244 -1表示差分選択
2017/2/11 3:44:02電気回路/zynq/DMA処理 8,398— —表示—基準

基準: 電気回路/zynq/DMA処理 (2017/2/11 3:44:02) → 比較: 電気回路/zynq/DMA処理 (2017/2/13 8:07:15)

@@ -2,10 +2,495 @@
#contents
+* 概要 [#f674dcb8]
+
+Zynq で DMA 処理するために勉強中の内容です。
+
+まだ動かせていないので間違ったことも多いと思います。
+
+- Xilinx のベアメタルドライバ
+-- Linux とはコンパチビリティのないドライバ
+- Xilinx の Linux ドライバ
+-- Linux 上で他のデバイスドライバから使われることを想定したドライバ
+-- ユーザーアプリケーションから使うものではない
+- サードパーティーの bperez77/xilinx_axidma
+-- Xilinx の Linux ドライバの上に構築された汎用ドライバ
+-- /dev/axidma 経由で簡単に DMA 転送が行える
+
+ということで、bperez77/xilinx_axidma を使えば良さそうに思えています。
+
+** 後で読むリスト [#dea41291]
+
+Linux DMA from User Space~
+https://forums.xilinx.com/xlnx/attachments/xlnx/ELINUX/10693/1/Linux%20DMA%20from%20User%20Space-public.pdf
+
+Linux DMA in Device Drivers~
+https://forums.xilinx.com/xlnx/attachments/xlnx/ELINUX/10658/1/drivers-session4-dma-4public.pdf
+
+Introduction to Linux Device Drivers~
+https://forums.xilinx.com/xlnx/attachments/xlnx/ELINUX/10669/1/drivers-sessions1-2-public.pdf
+
+* axi_dma のドキュメント [#e7f3007e]
+
+デザインダイアグラム上で axi_dma IP を右クリックして
+[IP Documentations] から各種ドキュメントが出る。
+
+https://www.xilinx.com/products/intellectual-property/axi_dma.html#overview
+
+など。
+
+* キャッシュについて [#g64cc687]
+
+** ACP について [#uf2e1786]
+
+accelerator coherency port を使うとキャッシュコヒーレンシーを
+アプリケーション側で制御しなくても良くなるみたい?
+
+http://marsee101.blog19.fc2.com/blog-entry-2297.html
+
+* bperez77/xilinx_axidma の使い方を調べる [#h7b14a1c]
+
+これが使えるとかなり楽ができそう。
+
+https://github.com/bperez77/xilinx_axidma
+
+** カーネルオプションを確認 [#c50efd43]
+
+z-turn 付属のカーネルにおいて
+
+- CONFIG_CMA=y
+- CONFIG_DMA_CMA=y
+- CONFIG_XILINX_DMAENGINES=y
+- CONFIG_XILINX_AXIDMA=y
+- CONFIG_XILINX_AXIVDMA=y
+- CONFIG_DMA_SHARED_BUFFER=y
+
+のうち、CONFIG_XILINX_DMAENGINES が無かったが、代わりに
+
+- CONFIG_XILINX_DMA_ENGINES=y
+
+があったので、Xilinx 製のドライバは初めから入っているみたい。
+
+
+** Device Tree [#ia885cc1]
+
+[[電気回路/HDL/VivadoでAXIバスを利用#sf57016a]] で作ったデバイスは、
+
+- address = 0x40400000
+-- tx は +0x0000
+-- rx は +0x0030
+- irq = GIC の F2P[4]
+- s2mm のみ
+- データ幅 32
+
+DTS における割り込み番号の設定方法は、
+http://qiita.com/ikwzm/items/b22592c31cdbb9ab6cf7 によれば、
+
+> interrupts=<0x0 0x1d 0x4> の2番目のパラメータの0x1d(十進数で29)が割り込み番号を指定しています。DMAコントローラーの割り込みはPS部のIRQ_F2P[0]に接続しています。ZYNQのテクニカルリファレンスマニュアルによれば、IRQ_F2P[0]は汎用割り込みコントローラー(GIC)内の共有ペリフェラル割り込み(SPI)の61番を通じて割り込みがかかります。このパラメータには61から32引いた値を設定するようです。
+>
+>interrupts=<0x0 0x1d 0x4>の1番目のパラメータはGIC内部の割り込みの種類を指定します。GIC内部には共有ペリフェラル割り込み(SPI)の他にCPUプライベートペリフェラル割り込み(PPI)とソフトウェア生成割り込み(SGI)があります。共有ペリフェラル割り込み(SPI)を使う場合は0を指定します。
+>
+>interrupts=<0x0 0x1d 0x4>の3番目のパラメータは割り込み信号がエッジトリガーかレベルトリガーかを指定します。1を指定するとエッジトリガー、4を指定するとレベルトリガーになります。
+
+とのことなので、今の場合は
+- 0 = 共有ペリフェラル割り込み(SPI)
+- 33 = 61 + 4 - 32 = 33
+- 4 = レベルトリガー
+
+インクルードファイルを使わない場合 &clkc = 1, &intc = 3 として、
+
+ axidma_chrdev: axidma_chrdev@0 {
+ compatible = "xlnx,axidma-chrdev";
+ dmas = <&axi_dma_0 0>;
+ dma-names = "rx_channel";
+ };
+
+ axidma0: axidma0@40400000 {
+ compatible = "xlnx,axi-dma", "xlnx,axi-dma-6.03.a", "xlnx,axi-dma-1.00.a";
+ reg = <0x40400000 0x10000>;
+ xlnx,include-sg;
+ #dma-cells = <1>;
+
+ dma-s2mm-channel@40400030 {
+ compatible = "xlnx,axi-dma-s2mm-channel";
+ dma-channels = <1>;
+ xlnx,datawidth = <32>;
+ xlnx,device-id = <0x0>;
+ clocks = <1 15>;
+ clock-names = "axis";
+ interrupt-parent = <3>;
+ interrupts = <0 33 4>;
+ };
+ };
+
+となりそうです?
+
+http://qiita.com/ikwzm/items/aae3dab578e28c13bc65 によると
+clocks = 15 は fclk0 のことのようなのですが、どこで調べられるのかよくわかっていません。。。
+
+** axidma デバイスが登録された [#nb7fbf42]
+
+起動メッセージに、
+
+ xilinx-dma 40400000.axidma0: Probing xilinx axi dma engine...Successful
+
+が現れ、
+
+ LANG:console
+ $ ls /sys/class/dma/dma1chan0/device/driver/
+ 40400000.axidma0 bind uevent unbind
+
+となっているので、まずは xilinx の axidma が登録されたことになります。
+これ自体はユーザーアプリケーションから直接使えるものではないので、
+この上に構築された汎用ドライバである bperez77/xilinx_axidma をかぶせて使うことになります。
+
+
+** bperez77/xilinx_axidma ドライバのビルド [#i70128a3]
+
+ LANG:console
+ $ git clone https://github.com/bperez77/xilinx_axidma.git
+ $ cd xilinx_axidma
+ $ make CROSS_COMPILE=arm-linux-gnueabihf- ARCH=arm KBUILD_DIR=/home/osamu/linux-zturn
+ make -C /home/osamu/linux-zturn M=/home/osamu/xilinx_axidma/driver modules
+ make[1]: Entering directory '/home/osamu/linux-zturn'
+ CC [M] /home/osamu/xilinx_axidma/driver/axi_dma.o
+ CC [M] /home/osamu/xilinx_axidma/driver/axidma_dma.o
+ In file included from /home/osamu/xilinx_axidma/driver/axidma_dma.c:25:0:
+ /home/osamu/xilinx_axidma/driver/version_portability.h: In function ‘axidma_setup_vdma_config’:
+ /home/osamu/xilinx_axidma/driver/version_portability.h:133:15: error: ‘struct xilinx_vdma_config’ has no member named ‘vsize’
+ dma_config->vsize = height; // Height of the image (in lines)
+ ^
+ /home/osamu/xilinx_axidma/driver/version_portability.h:134:15: error: ‘struct xilinx_vdma_config’ has no member named ‘hsize’
+ dma_config->hsize = width * depth; // Width of the image (in bytes)
+ ^
+ /home/osamu/xilinx_axidma/driver/version_portability.h:135:15: error: ‘struct xilinx_vdma_config’ has no member named ‘stride’
+ dma_config->stride = width * depth; // Number of bytes to process per line
+ ^
+ /home/osamu/xilinx_axidma/driver/version_portability.h: In function ‘axidma_to_xilinx_type’:
+ /home/osamu/xilinx_axidma/driver/version_portability.h:154:59: error: ‘XILINX_DMA_IP_VDMA’ undeclared (first use in this function)
+ return (dma_type == AXIDMA_DMA) ? XILINX_DMA_IP_DMA : XILINX_DMA_IP_VDMA;
+ ^
+ /home/osamu/xilinx_axidma/driver/version_portability.h:154:59: note: each undeclared identifier is reported only once for each function it appears in
+ /home/osamu/xilinx_axidma/driver/axidma_dma.c: In function ‘axidma_prep_transfer’:
+ /home/osamu/xilinx_axidma/driver/axidma_dma.c:198:32: error: ‘DMA_COMPL_SKIP_DEST_UNMAP’ undeclared (first use in this function)
+ dma_flags = DMA_CTRL_ACK | DMA_COMPL_SKIP_DEST_UNMAP | DMA_PREP_INTERRUPT;
+ ^
+ /home/osamu/xilinx_axidma/driver/axidma_dma.c: In function ‘axidma_start_transfer’:
+ /home/osamu/xilinx_axidma/driver/axidma_dma.c:271:30: error: ‘DMA_SUCCESS’ undeclared (first use in this function)
+ } else if (status != DMA_SUCCESS) {
+ ^
+ In file included from /home/osamu/xilinx_axidma/driver/axidma_dma.c:25:0:
+ /home/osamu/xilinx_axidma/driver/version_portability.h: In function ‘axidma_to_xilinx_type’:
+ /home/osamu/xilinx_axidma/driver/version_portability.h:155:1: error: control reaches end of non-void function [-Werror=return-type]
+ }
+ ^
+ cc1: all warnings being treated as errors
+ scripts/Makefile.build:318: recipe for target '/home/osamu/xilinx_axidma/driver/axidma_dma.o' failed
+ make[2]: *** [/home/osamu/xilinx_axidma/driver/axidma_dma.o] Error 1
+ Makefile:1310: recipe for target '_module_/home/osamu/xilinx_axidma/driver' failed
+ make[1]: *** [_module_/home/osamu/xilinx_axidma/driver] Error 2
+ make[1]: Leaving directory '/home/osamu/linux-zturn'
+ driver/Makefile:83: recipe for target 'driver/axidma.ko' failed
+ make: *** [driver/axidma.ko] Error 2
+ $ grep -A11 "struct xilinx_vdma_config {" ../linux-zturn/include/linux/amba/xilinx_dma.h
+ struct xilinx_vdma_config {
+ int frm_dly;
+ int gen_lock;
+ int master;
+ int frm_cnt_en;
+ int park;
+ int park_frm;
+ int coalesc;
+ int delay;
+ int reset;
+ int ext_fsync;
+ };
+ $ head -3 ../linux-zturn/Makefile
+ VERSION = 3
+ PATCHLEVEL = 15
+ SUBLEVEL = 0
+ $ # https://github.com/bperez77/xilinx_axidma/issues/14
+ $ jed driver/version_portability.h
+ + #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0)
+ + dma_config->vsize = height; // Height of the image (in lines)
+ + dma_config->hsize = width * depth; // Width of the image (in bytes)
+ + dma_config->stride = width * depth; // Number of bytes to process per line
+ + #endif
+ ...
+
+ + #ifdef XILINX_DMA_IP_VDMA
+ + BUG_ON(dma_type != AXIDMA_DMA && dma_type != AXIDMA_VDMA);
+ + return (dma_type == AXIDMA_DMA) ? XILINX_DMA_IP_DMA : XILINX_DMA_IP_VDMA;
+ + #else
+ + BUG_ON(dma_type != AXIDMA_DMA);
+ + return XILINX_DMA_IP_DMA;
+ + #endif
+ ...
+
+ + #ifndef DMA_SUCCESS
+ + // DMA_SUCCESS was renamed to DMA_COMPLETE (indicates a DMA transaction is done)
+ + #define DMA_SUCCESS DMA_COMPLETE
+ + #endif
+ +
+ + #ifndef DMA_COMPL_SKIP_DEST_UNMAP
+ + // The skip destination unmap DMA control option was removed in between 3.0 and 3.15 ?
+ + #define DMA_COMPL_SKIP_DEST_UNMAP 0
+ + #endif
+ $ make CROSS_COMPILE=arm-linux-gnueabihf- ARCH=arm KBUILD_DIR=/home/osamu/linux-zturn
+ $ ls outputs/
+ axidma.ko axidma_benchmark axidma_display_image axidma_transfer libaxidma.so
+
+4.x 向けに対策されている内容の一部が先取りして入っているようで、
+3.15 に対して同様の対策が必要になっているようです。
+そのあたりを見ながら無理矢理書き換えたところ、コンパイルが通りました。
+
+** インストール [#sa8cbf2c]
+
+axidma.ko と libaxidma.so を z-turn 上の Linux へコピーして、
+
+ LANG:console
+ $ sudo chown root:root axidma.ko libaxidma.so
+ $ sudo cp libaxidma.so /usr/local/lib/
+ $ sudo mkdir -p /lib/modules/3.15.0-xilinx/kernel/drivers/dma/xilinx/
+ $ sudo cp axidma.ko /lib/modules/3.15.0-xilinx/kernel/drivers/dma/xilinx/
+ $ sudo insmod axidma.ko
+ $ dmesg | tail
+ [ 469.438795] axidma: axidma_dma.c: axidma_dma_init: 674: DMA: Found 0 transmit channels and 1 receive channels.
+ [ 469.447790] axidma: axidma_dma.c: axidma_dma_init: 676: VDMA: Found 0 transmit channels and 0 receive channels.
+ $ ls /dev/axidma
+ /dev/axidma
+
+正しく入ったみたい。
+
+** bootarg の設定 [#q00cefa6]
+
+devicetree.dts
+
+ chosen {
+ bootargs = "console=ttyPS0,115200 earlyprintk root=/dev/mmcblk0p2 rootwait cma=25M";
+ };
+
+のように cma=25M を追加すればいいのでしょうけれど、
+設定しないときのブート時のメッセージを見ると、
+
+ cma: CMA: reserved 128 MiB at 27800000
+
+となっていて、始めから十分な大きさが確保されてそう?
+
+
+
+
+
+
+** 使ってみる [#wda13c3c]
+
+ドライバには、2つのがあるようで、それぞれの使い方は
+対応するヘッダファイルに詳しく書かれています。
+
+- /dev/axidma への ioctrl で制御する方法 ([[include/axidma_ioctl.h>https://github.com/bperez77/xilinx_axidma/blob/master/include/axidma_ioctl.h]])
+- ioctrl をラップして使いやすくするシェアドライブラリ ([[include/libaxidma.h>https://github.com/bperez77/xilinx_axidma/blob/master/include/libaxidma.h]])
+
+後者はとても簡単に使えるようになっているので、こちらを使うことにします。
+
+どちらを使った場合にも、Scatter Gather 機能のサポートは無いようです。
+
+ LANG:c
+ #include <stdio.h>
+ #include <stdbool.h>
+ #include "libaxidma.h"
+
+ #define MYCHAN 0
+ #define BUFFER_SIZE 1024
+ #define TRANSFER_LEN 16
+
+ void dma_int_handler(int channel_id, void *data)
+ {
+ if(channel_id != MYCHAN)
+ printf("ERROR: unexpected channel id %d\n", channel_id);
+
+ (*(int*)data) = 1; // done
+ }
+
+ int array_contains(int target, int *array, int length)
+ {
+ int i;
+ for (i=0; i<length; i++)
+ if ( array[i] == target ) return 1;
+ return 0;
+ }
+
+
+ void display_result(void *buffer)
+ {
+ int i, *p = (int*)buffer;
+ for(i=0; i<TRANSFER_LEN; i++) {
+ printf("%08x ", *(p++));
+ if (i % 8 == 7)
+ printf("\n");
+ }
+ }
+
+ void main()
+ {
+ // デバイスハンドルを得る
+ axidma_dev_t hdma = axidma_init();
+
+ // 使用するチャンネルが rx 機能を持っていることを確認
+ int nrxchan, *rxchans = axidma_get_dma_rx(hdma, &nrxchan);
+ if(!array_contains(MYCHAN, rxchans, nrxchan)) {
+ printf("チャンネル %d は読み込みをサポートしません", MYCHAN);
+ goto exit;
+ }
+
+ // バッファーを確保
+ void *buffer = axidma_malloc(hdma, BUFFER_SIZE);
+
+ // 割り込みハンドラを設定
+ volatile int done = 0;
+ axidma_set_callback(hdma, MYCHAN, dma_int_handler, (void*)&done);
+
+ // 非同期転送開始
+ printf("転送開始\n");
+ int ret = axidma_oneway_transfer(hdma, AXIDMA_READ, MYCHAN, buffer, TRANSFER_LEN, false);
+
+ // 終了待ち
+ while (!done) {
+ ;
+ // 途中で止める場合
+ // axidma_stop_transfer(hdma, MYCHAN, enum axidma_dir dir);
+ }
+
+ display_result(buffer);
+
+ // バッファーを解放
+ axidma_free(hdma, buffer, BUFFER_SIZE);
+
+ exit:
+
+ // デバイスハンドルを解放する
+ axidma_destroy(hdma);
+ }
+
+** リンクできない? → 解決 [#bdbfbc73]
+
+ LANG:console
+ $ gcc main.c -L /usr/local/lib -l libaxidma
+ /usr/bin/ld: -llibaxidma が見つかりません
+ $ file /usr/local/lib/libaxidma.so
+ /usr/local/lib/libaxidma.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=b533a8f1de3cf2fd0ca9faf9b7e1424b5dd8c776, not stripped
+
+リンクできない。。。
+
+理由が全然わからなかったのだけれど、
+xilinx_axidma の make は exmples のコードもリンクできているのだから、
+それをまねすればいいはず!と気づいて、
+
+ LANG:console
+ $ make -n CROSS_COMPILE=arm-linux-gnueabihf- ARCH=arm KBUILD_DIR=/home/osamu/linux-zturn
+ ...
+
+ arm-linux-gnueabihf-gcc -Wall -Wextra -Werror -std=gnu99 -O3 -I include \
+ examples/axidma_transfer.c examples/dma_util.c examples/util.c \
+ -o examples/axidma_transfer -L outputs -l axidma -Wl,-rpath,'$ORIGIN'
+ ...
+
+まねすると、
+
+ LANG:console
+ $ gcc main.c -L /usr/local/lib -l axidma -Wl,-rpath,'$ORIGIN'
+ $ ls a.out
+ a.out
+
+リンクできた。
+
+http://www.kotha.net/ghcguide_ja/7.0.4/using-shared-libs.html によれば、
+この指定はアプリケーションプログラムと同じディレクトリに .so ファイルを
+置くという宣言のようなので、むしろ
+
+ LANG:console
+ $ cat > Makefile
+ axidmatest: main.c
+ gcc -o axidmatest main.c -L /usr/local/lib -l axidma -Wl,-rpath,'/usr/local/lib'
+ $ make
+ gcc -o axidmatest main.c -L /usr/local/lib -l axidma -Wl,-rpath,'/usr/local/lib'
+
+の方が良いのかもしれない。
+
+
+** エラーが出る [#x4301f01]
+
+ LANG:console
+ $ cat /sys/class/dma/dma1chan0/memcpy_count
+ 0
+ $ cat /sys/class/dma/dma1chan0/bytes_transferred
+ 0
+ $ sudo ./axidmatest
+ 転送開始
+ xilinx_dma_start_transfer::simple DMA mode
+ xilinx-dma 40400000.axidma0: Channel e6f61950 has errors 11, cdr 0 tdr 0
+ 00000004 00000005 00000006 00000007 00000000 00000000 00000000 00000000
+ 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
+ $ cat /sys/class/dma/dma1chan0/memcpy_count
+ 0
+ $ cat /sys/class/dma/dma1chan0/bytes_transferred
+ 0
+ $ sudo ./axidmatest
+ 転送開始
+ xilinx_dma_start_transfer::simple DMA mode
+ xilinx-dma 40400000.axidma0: Channel e6f61950 has errors 11, cdr 0 tdr 0
+ 00000033 00000034 00000035 00000036 00000000 00000000 00000000 00000000
+ 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
+ $ sudo ./axidmatest
+ 転送開始
+ xilinx_dma_start_transfer::simple DMA mode
+ xilinx-dma 40400000.axidma0: Channel e6f61950 has errors 11, cdr 0 tdr 0
+ 00000062 00000063 00000064 00000065 00000000 00000000 00000000 00000000
+ 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
+ $ sudo ./axidmatest
+ 転送開始
+ xilinx_dma_start_transfer::simple DMA mode
+ xilinx-dma 40400000.axidma0: Channel e6f61950 has errors 11, cdr 0 tdr 0
+ 00000091 00000092 00000093 00000094 00000000 00000000 00000000 00000000
+ 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
+ $ sudo ./axidmatest
+ 転送開始
+ xilinx_dma_start_transfer::simple DMA mode
+ xilinx-dma 40400000.axidma0: Channel e6f61950 has errors 11, cdr 0 tdr 0
+ 000000c0 000000c1 000000c2 000000c3 00000000 00000000 00000000 00000000
+ 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
+ $ sudo ./axidmatest
+ 転送開始
+ xilinx_dma_start_transfer::simple DMA mode
+ xilinx-dma 40400000.axidma0: Channel e6f61950 has errors 11, cdr 0 tdr 0
+ 000000ef 000000f0 000000f1 000000f2 00000000 00000000 00000000 00000000
+ 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
+
+おかしいところ:
+- エラーが出ている xilinx-dma 40400000.axidma0: Channel e6f61950 has errors 11, cdr 0 tdr 0
+- 4ワードしか更新されていない
+- ゼロから始まっていない
+- 等間隔 0x2f に値が増えている
+
+
+
+
* Xilinx の XAxiDma ドライバ [#b6740db8]
-** ヘッダファイルの説明書き日本語訳 [#z33ce603]
+自分で Linux ドライバを作るときやベアメタルでは
+Xilinx の低レベルドライバを使うことになる?
+C:\Xilinx\SDK\2016.4\data\embeddedsw\XilinxProcessorIPLib\drivers\axidma_v9_3\src
+
+というようなディレクトリにソースコードが、
+
+C:\Xilinx\SDK\2016.4\data\embeddedsw\XilinxProcessorIPLib\drivers\axidma_v9_3\doc/html/api/index.html
+
+にドキュメントがある。[File List] から各ヘッダファイル *.h
+をクリックして関数一覧を見るのがわかりやすそう
+
+
+** ドキュメントトップの日本語訳 [#z33ce603]
+
以下は AXI DMA エンジンドライバの API です。
DMA 機能の全詳細はハードウェアのスペックを参照してください。
@@ -33,6 +518,9 @@
*** Scatter-Gather DMA (SGDMA) [#q9fc2618]
+(訳注:axi_dma では IP のカスタマイズで [Enable Scatter Gather Engine]
+をオンにしないとこの機能は使えない)
+
SGDMA ではユーザーアプリケーションがメモリ上に次善に準備した一連の転送処理設定を、
ハードウェアが順に処理します。この処理中にユーザーアプリケーションが介在する
必要はありません。また、処理中に新たな処理内容を追加することも可能であり、
@@ -330,3 +818,399 @@
さらに、xdebug.h の中の "#undef DEBUG" という行をコメントアウトして下さい。
** API メモ [#od119c43]
+
+*** xaxidma.h [#ef2e999d]
+
+- enum Direction { XAXIDMA_DMA_TO_DEVICE, XAXIDMA_DEVICE_TO_DMA };
+- XAxiDma_BdRing * XAxiDma_GetTxRing(XAxiDma * InstancePtr);
+- XAxiDma_BdRing * XAxiDma_GetRxRing(XAxiDma * InstancePtr);
+- XAxiDma_BdRing * XAxiDma_GetRxIndexRing(XAxiDma * InstancePtr, int RingIndex);
+- bool XAxiDma_HasSg(XAxiDma * InstancePtr);
+- void XAxiDma_IntrEnable(XAxiDma * InstancePtr, uint Mask, int Direction);
+- uint XAxiDma_IntrGetEnabled(XAxiDma * InstancePtr, int Direction);
+- void XAxiDma_IntrDisable(XAxiDma * InstancePtr, uint Mask, int Direction);
+- uint XAxiDma_IntrGetIrq(XAxiDma * InstancePtr, int Direction);
+- void XAxiDma_IntrAckIrq(XAxiDma * InstancePtr, uint Mask, int Direction);
+- XAxiDma_Config *XAxiDma_LookupConfig(u32 DeviceId);
+- int XAxiDma_CfgInitialize(XAxiDma * InstancePtr, XAxiDma_Config *Config);
+- void XAxiDma_Reset(XAxiDma * InstancePtr);
+- int XAxiDma_ResetIsDone(XAxiDma * InstancePtr);
+- int XAxiDma_Pause(XAxiDma * InstancePtr);
+- int XAxiDma_Resume(XAxiDma * InstancePtr);
+- u32 XAxiDma_Busy(XAxiDma *InstancePtr,int Direction);
+- u32 XAxiDma_SimpleTransfer(XAxiDma *InstancePtr, UINTPTR BuffAddr, u32 Length,int Direction);
+- int XAxiDma_SelectKeyHole(XAxiDma *InstancePtr, int Direction, int Select);
+- int XAxiDma_SelectCyclicMode(XAxiDma *InstancePtr, int Direction, int Select);
+- int XAxiDma_Selftest(XAxiDma * InstancePtr);
+
+*** xaxidma_bdring.h [#tb54f8b9]
+
+- int XAxiDma_BdRingCntCalc(u32 Alignment, u32 Bytes);
+- int XAxiDma_BdRingMemCalc(u32 Alignment, u32 NumBd)
+- int XAxiDma_BdRingGetCnt(XAxiDma_BdRing* RingPtr)
+- int XAxiDma_BdRingGetFreeCnt(XAxiDma_BdRing* RingPtr)
+- void XAxiDma_BdRingSnapShotCurrBd(XAxiDma_BdRing* RingPtr)
+-- RingPtr->IsRxChannel, RingPtr->ChanBase, RingPtr->RingIndex を設定してから呼び出す
+-- ハードウェアから現在値を読み出した値により RingPtr->BdaRestart, RingPtr->ChanBase
+がセットされる
+- XAxiDma_Bd * XAxiDma_BdRingGetCurrBd(XAxiDma_BdRing* RingPtr)
+- XAxiDma_Bd *XAxiDma_BdRingNext(XAxiDma_BdRing* RingPtr, XAxiDma_Bd *BdPtr)
+-- 配列上で次の XAxiDma_Bd を返す。連結リストの Next ではない
+-- Ring なので最後の要素の Next は最初の要素になる
+- XAxiDma_Bd *XAxiDma_BdRingPrev(XAxiDma_BdRing* RingPtr, XAxiDma_Bd *BdPtr)
+-- 配列上で前の XAxiDma_Bd を返す。連結リストの Prev ではない
+-- Ring なので最初の要素の Prev は最後の要素になる
+- u32 XAxiDma_BdRingGetSr(XAxiDma_BdRing* RingPtr)
+-- Sr はステータスレジスタ
+- u32 XAxiDma_BdRingGetError(XAxiDma_BdRing* RingPtr)
+-- ステータスレジスタにエラービットのマスクをかけて返す
+-- 内容を見るには XAXIDMA_ERR_*_MASK と & を取る (xaxidma_hw.h)
+- int XAxiDma_BdRingHwIsStarted(XAxiDma_BdRing* RingPtr)
+-- Halt か Not Halt かを見る
+- int XAxiDma_BdRingBusy(XAxiDma_BdRing* RingPtr)
+-- 実際に転送中かを見る
+- void XAxiDma_BdRingIntEnable(XAxiDma_BdRing* RingPtr, u32 Mask)
+-- XAXIDMA_CR_OFFSET レジスタを設定する
+- u32 XAxiDma_BdRingIntGetEnabled(XAxiDma_BdRing* RingPtr)
+-- XAXIDMA_CR_OFFSET レジスタを読む
+- void XAxiDma_BdRingIntDisable(XAxiDma_BdRing* RingPtr, u32 Mask)
+-- XAXIDMA_CR_OFFSET レジスタを設定する
+- u32 XAxiDma_BdRingGetIrq(XAxiDma_BdRing* RingPtr)
+-- IRQ_OFFSET レジスタを読む
+-- アプリケーションは XAXIDMA_IRQ_*** 定数と & を取って解釈する
+- void XAxiDma_BdRingAckIrq(XAxiDma_BdRing* RingPtr)
+- int XAxiDma_StartBdRingHw(XAxiDma_BdRing* RingPtr);
+- int XAxiDma_UpdateBdRingCDesc(XAxiDma_BdRing* RingPtr);
+- u32 XAxiDma_BdRingCreate(XAxiDma_BdRing * RingPtr, UINTPTR PhysAddr, UINTPTR VirtAddr, u32 Alignment, int BdCount);
+- int XAxiDma_BdRingClone(XAxiDma_BdRing * RingPtr, XAxiDma_Bd * SrcBdPtr);
+- int XAxiDma_BdRingAlloc(XAxiDma_BdRing * RingPtr, int NumBd, XAxiDma_Bd ** BdSetPtr);
+- int XAxiDma_BdRingUnAlloc(XAxiDma_BdRing * RingPtr, int NumBd, XAxiDma_Bd * BdSetPtr);
+- int XAxiDma_BdRingToHw(XAxiDma_BdRing * RingPtr, int NumBd, XAxiDma_Bd * BdSetPtr);
+- int XAxiDma_BdRingFromHw(XAxiDma_BdRing * RingPtr, int BdLimit, XAxiDma_Bd ** BdSetPtr);
+- int XAxiDma_BdRingFree(XAxiDma_BdRing * RingPtr, int NumBd, XAxiDma_Bd * BdSetPtr);
+- int XAxiDma_BdRingStart(XAxiDma_BdRing * RingPtr);
+- int XAxiDma_BdRingSetCoalesce(XAxiDma_BdRing * RingPtr, u32 Counter, u32 Timer);
+- void XAxiDma_BdRingGetCoalesce(XAxiDma_BdRing * RingPtr, u32 *CounterPtr, u32 *TimerPtr);
+
+*** xaxidma_bd.h [#ac50fffc]
+
+BD は
+
+typedef u32 XAxiDma_Bd[XAXIDMA_BD_NUM_WORDS];
+
+のように構造体じゃなく u32 の配列として定義されているのだけれど、
+
+ LANG:c
+ typedef struct {
+ XAxiDma_Bd *Next; // 64bit 次の BD
+ void *Buffer; // 64bit バッファーアドレス
+ struct {
+ u8 TDest; // 8bit
+ u8 TReserved1; // 8bit
+ u8 TId; // 8bit
+ u8 TReserved2; // 8bit
+ u8 TUser; // 8bit
+ u8 TReserved3; // 8bit
+ u8 ARCache; // 8bit
+ u8 ARUser; // 8bit
+ } MultiChannelControl; // 32bit MCCTL
+ u16 Stride; // 16bit
+ u3 Reserved0; // 3bit
+ u13 VSize; // 13bit
+ u16 Control; // 16bit
+ u16 Length; // 16bit 転送長
+ struct {
+ u26 ;
+ struct {
+ u1 RXEOF; // 1bit Last rx packet
+ u1 RXSOF; // 1bit First rx packet
+ struct {
+ u1 InternalError; // 1bit
+ u1 SlaveError; // 1bit
+ u1 DecodeError; // 1bit
+ } StatusErrorBits; // 4bit
+ u1 Completed; // 1bit
+ } StatusBits; // 6bit
+ } Status; // 32bit STS
+ u32 User0; // 32bit
+ u32 User1; // 32bit
+ u32 User2; // 32bit
+ u32 User3; // 32bit
+ u32 User4; // 32bit
+ u32 Id; // 32bit
+ u32 HasSTSCNTRL; // 32bit
+ u32 HasDRE; // 32bit
+ } XAxiDma_Bd;
+
+と考えればいいみたい。
+
+個々のフィールドにアクセスするために
+
+- XAxiDma_BdClear(BdPtr) // ゼロフィルする
+- XAxiDma_BdGetCtrl(BdPtr) // Control
+- XAxiDma_BdSetCtrl(BdPtr, Data); // Control
+- XAxiDma_BdGetSts(BdPtr) // Status
+- XAxiDma_BdGetLength(BdPtr, LengthMask) // 設定した転送長
+- XAxiDma_BdSetLength(BdPtr, LenBytes, LengthMask); // 転送長
+- XAxiDma_BdSetId(BdPtr, u32 Id) // アプリケーションが付ける任意の ID
+- XAxiDma_BdGetId(BdPtr) // アプリケーションが付けた任意の ID
+- XAxiDma_BdGetBufAddr(BdPtr)
+- XAxiDma_BdSetBufAddr(BdPtr, Addr);
+- XAxiDma_BdSetBufAddrMicroMode(BdPtr, Addr);
+- XAxiDma_BdHwCompleted(BdPtr) // Status の Completed ビットを読む
+- XAxiDma_BdGetActualLength(BdPtr, LengthMask) // Status の転送されたデータ長を取り出す
+- XAxiDma_BdSetTId(BdPtr, TId) // MCCTL の TId フィールド
+- XAxiDma_BdGetTId(BdPtr) // MCCTL の TId フィールド
+- XAxiDma_BdSetTDest(BdPtr, TDest) // MCCTL の TDest フィールド
+- XAxiDma_BdGetTDest(BdPtr) // MCCTL の TDest フィールド
+- XAxiDma_BdSetTUser(BdPtr, TUser) // MCCTL の TUser フィールド
+- XAxiDma_BdGetTUser(BdPtr) // MCCTL の TUser フィールド
+- XAxiDma_BdSetARCache(BdPtr, ARCache) // MCCTL の ARCache フィールド
+- XAxiDma_BdGetARCache(BdPtr) // MCCTL の ARCache フィールド
+- XAxiDma_BdSetARUser(BdPtr, ARUser) // MCCTL の ARUser フィールド
+- XAxiDma_BdGetARUser(BdPtr) // MCCTL の ARUser フィールド
+- XAxiDma_BdSetStride(BdPtr, Stride)
+- XAxiDma_BdGetStride(BdPtr)
+- XAxiDma_BdSetVSize(BdPtr, VSize)
+- XAxiDma_BdGetVSize(BdPtr)
+- XAxiDma_BdGetAppWord(BdPtr, Offset, int *Valid);
+- XAxiDma_BdSetAppWord(BdPtr, Offset, Word);
+
+** Xilinx の XAxiDma ドライバのサンプルを読む [#e3026669]
+
+エラー処理を飛ばして読んでみます。
+
+割り込みについて XPAR_INTC_0_DEVICE_ID による #ifdef があるけれど、
+Zynq は GIC を持っているので、XPAR_INTC_0_DEVICE_ID をセットしない、
+xscugic.h ベースのコードを追えばいいはず?
+
+Zynq での割り込み使用例:~
+https://japan.xilinx.com/support/answers/50572.html
+
+*** xaxidma_example_simple_intr.c [#l75492f1]
+
++ XAxiDma_LookupConfig(DeviceId) で DeviceId に対するハードウェア情報を得る (xaxidma_sinit.c)
++ XAxiDma_CfgInitialize(&AxiDma, CfgPtr); によりハードウェア情報を元に AxiDma を設定する
+-- 内部で呼ばれる XAxiDma_Reset がタイムアウトすると失敗する (AxiDma.Initialized == 0 となる)
++ XAxiDma_HasSg(&AxiDma) で SG モードかどうかを判別 (AxiDma.HasSg, CfgPtr->HasSg に対応)
+-- SG モード対応だと Simple モードでは動作しない???
++ SetupIntrSystem 内で割り込みを設定
+++ XScuGic_LookupConfig(INTC_DEVICE_ID); でハードウェア情報を得る
+++ XScuGic_CfgInitialize(IntcPtr,IntcConfig,IntcConfig->CpuBaseAddress); で Intc を設定
+++ XScuGic_SetPriorityTriggerType で Priority = 0xA0, TriggerType = 0x3 にする
+--- Priority は小さいほど高い。8 の倍数が有効。
+--- TriggerType = 3 は Rising edge sensitive
+++ XScuGic_Connect(IntcPtr, XxIntrId, XxIntrHandler, AxiDmaPtr); で割り込みハンドラを設定
+++ XScuGic_Enable(IntcPtr, XxIntrId); で割り込みを許可
+++ Xil_ExceptionInit(); これ、実際には何もしない。 (lib\bsp\standalone_vX.X\src\arm\common\xil_exception.c)
+++ Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, INTC_HANDLER, IntcPtr); 割り込みハンドラを登録
+--- INTC_HANDLER は xscugic_intr.c の XScuGic_InterruptHandler を指しており、
+そこから GIC へ登録したハンドラが呼ばれる
+++ Xil_ExceptionEnable(); で cpsr の XIL_EXCEPTION_IRQ で指定したビットをクリアする
++ XAxiDma_IntrDisable で一旦 DMA からの割り込み信号送出を禁止する
++ XAxiDma_IntrEnable で DMA からの割り込み信号送出を許可する
++ Xil_DCacheFlushRange で送信バッファのキャッシュをメモリに書き出す
++ XAxiDma_SimpleTransfer で送受信の要求
++ 割り込みハンドラで
+++ 引数から AxiDma へのポインタを得る
+++ XAxiDma_IntrGetIrq により対応する IRQ 番号を得る
+++ IrqStatus & XAXIDMA_IRQ_ALL_MASK で割り込みが生じたか確かめる
+--- いくつかの割り込み信号線をまとめている場合にも対応できる
+++ XAxiDma_IntrAckIrq により対応する IRQ に Acknowledge を送る
+++ IrqStatus & XAXIDMA_IRQ_ERROR_MASK ならエラーで割り込みが生じた
+--- XAxiDma_Reset して HALT 状態から戻す
+--- アプリケーションにエラーを通知する
+++ IrqStatus & XAXIDMA_IRQ_IOC_MASK なら正常終了
++ Xil_DCacheInvalidateRange で受信バッファのキャッシュ内容を無効化
++ DisableIntrSystem 内で割り込みハンドラを解除する
+++ XScuGic_Disconnect で GIC から割り込みハンドラを解除する
+
+ LANG:c
+ #include "xscugic.h"
+
+ void main(void)
+ {
+ int Status;
+ XAxiDma_Config *Config;
+ int Tries = NUMBER_OF_TRANSFERS;
+ int Index;
+ u8 *TxBufferPtr;
+ u8 *RxBufferPtr;
+ u8 Value;
+
+ TxBufferPtr = (u8 *)TX_BUFFER_BASE ;
+ RxBufferPtr = (u8 *)RX_BUFFER_BASE;
+
+ Config = XAxiDma_LookupConfig(DMA_DEV_ID);
+ XAxiDma_CfgInitialize(&AxiDma, Config);
+
+ assert(!XAxiDma_HasSg(&AxiDma));
+
+ // Set up Interrupt system
+ SetupIntrSystem(&Intc, &AxiDma, TX_INTR_ID, RX_INTR_ID);
+
+ // Disable all interrupts before setup
+ XAxiDma_IntrDisable(&AxiDma, XAXIDMA_IRQ_ALL_MASK, XAXIDMA_DMA_TO_DEVICE);
+ XAxiDma_IntrDisable(&AxiDma, XAXIDMA_IRQ_ALL_MASK, XAXIDMA_DEVICE_TO_DMA);
+
+ // Enable all interrupts
+ XAxiDma_IntrEnable(&AxiDma, XAXIDMA_IRQ_ALL_MASK, XAXIDMA_DMA_TO_DEVICE);
+ XAxiDma_IntrEnable(&AxiDma, XAXIDMA_IRQ_ALL_MASK, XAXIDMA_DEVICE_TO_DMA);
+
+ /* Initialize flags before start transfer test */
+ TxDone = 0;
+ RxDone = 0;
+ Error = 0;
+
+ InitializeTxBuffer(TxBuffer);
+
+ // Flush the SrcBuffer before the DMA transfer, in case the Data Cache is enabled
+ Xil_DCacheFlushRange((UINTPTR)TxBufferPtr, MAX_PKT_LEN);
+
+ // Send a packet
+ for(Index = 0; Index < Tries; Index ++) {
+
+ XAxiDma_SimpleTransfer(&AxiDma,(UINTPTR) RxBufferPtr,
+ MAX_PKT_LEN, XAXIDMA_DEVICE_TO_DMA);
+ XAxiDma_SimpleTransfer(&AxiDma,(UINTPTR) TxBufferPtr,
+ MAX_PKT_LEN, XAXIDMA_DMA_TO_DEVICE);
+
+ // Wait TX done and RX done
+ while (!TxDone && !RxDone && !Error) {
+ // NOP
+ }
+ assert(!Error);
+
+ // Transfer finished, check data
+ Xil_DCacheInvalidateRange((u32)RxPacket, Length);
+ Status = CheckData(MAX_PKT_LEN, 0xC);
+ assert(Status == XST_SUCCESS);
+ }
+
+ // Disable TX and RX Ring interrupts and return success
+ DisableIntrSystem(&Intc, TX_INTR_ID, RX_INTR_ID);
+ }
+
+ static void TxIntrHandler(void *Callback)
+ {
+ u32 IrqStatus;
+ int TimeOut;
+ XAxiDma *AxiDmaInst = (XAxiDma *)Callback;
+
+ // Read and acknowledge pending interrupts
+ IrqStatus = XAxiDma_IntrGetIrq(AxiDmaInst, XAXIDMA_DMA_TO_DEVICE);
+ XAxiDma_IntrAckIrq(AxiDmaInst, IrqStatus, XAXIDMA_DMA_TO_DEVICE);
+
+ // If no interrupt is asserted, we do not do anything
+ if (!(IrqStatus & XAXIDMA_IRQ_ALL_MASK))
+ return;
+
+ // If error interrupt is asserted, raise error flag, reset the
+ // hardware to recover from the error, and return with no further
+ // processing.
+ if ((IrqStatus & XAXIDMA_IRQ_ERROR_MASK)) {
+ Error = 1;
+
+ // Reset should never fail for transmit channel
+ XAxiDma_Reset(AxiDmaInst);
+
+ TimeOut = RESET_TIMEOUT_COUNTER;
+ while (TimeOut) {
+ if (XAxiDma_ResetIsDone(AxiDmaInst))
+ break;
+ TimeOut -= 1;
+ }
+ return;
+ }
+
+ // If Completion interrupt is asserted, then set the TxDone flag
+ if ((IrqStatus & XAXIDMA_IRQ_IOC_MASK))
+ TxDone = 1;
+ }
+
+ static void RxIntrHandler(void *Callback)
+ {
+ u32 IrqStatus;
+ int TimeOut;
+ XAxiDma *AxiDmaInst = (XAxiDma *)Callback;
+
+ // Read and acknowledge pending interrupts
+ IrqStatus = XAxiDma_IntrGetIrq(AxiDmaInst, XAXIDMA_DEVICE_TO_DMA);
+ XAxiDma_IntrAckIrq(AxiDmaInst, IrqStatus, XAXIDMA_DEVICE_TO_DMA);
+
+ // If no interrupt is asserted, we do not do anything
+ if (!(IrqStatus & XAXIDMA_IRQ_ALL_MASK))
+ return;
+
+ // If error interrupt is asserted, raise error flag, reset the
+ // hardware to recover from the error, and return with no further
+ // processing.
+ if ((IrqStatus & XAXIDMA_IRQ_ERROR_MASK)) {
+ Error = 1;
+
+ // Reset could fail and hang
+ // NEED a way to handle this or do not call it??
+ XAxiDma_Reset(AxiDmaInst);
+
+ TimeOut = RESET_TIMEOUT_COUNTER;
+ while (TimeOut) {
+ if(XAxiDma_ResetIsDone(AxiDmaInst))
+ break;
+ TimeOut -= 1;
+ }
+ return;
+ }
+
+ // If completion interrupt is asserted, then set RxDone flag
+ if ((IrqStatus & XAXIDMA_IRQ_IOC_MASK))
+ RxDone = 1;
+ }
+
+ static int SetupIntrSystem(INTC * IntcInstancePtr,
+ XAxiDma * AxiDmaPtr, u16 TxIntrId, u16 RxIntrId)
+ {
+ int Status;
+
+ XScuGic_Config *IntcConfig;
+
+ // Initialize the interrupt controller driver so that it is ready to use.
+ XScuGic_LookupConfig(INTC_DEVICE_ID);
+ XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,
+ IntcConfig->CpuBaseAddress);
+
+ XScuGic_SetPriorityTriggerType(IntcInstancePtr, TxIntrId, 0xA0, 0x3);
+ XScuGic_SetPriorityTriggerType(IntcInstancePtr, RxIntrId, 0xA0, 0x3);
+
+ // Connect the device driver handler that will be called when an
+ // interrupt for the device occurs, the handler defined above performs
+ // the specific interrupt processing for the device.
+ XScuGic_Connect(IntcInstancePtr, TxIntrId,
+ (Xil_InterruptHandler)TxIntrHandler, AxiDmaPtr);
+
+ XScuGic_Connect(IntcInstancePtr, RxIntrId,
+ (Xil_InterruptHandler)RxIntrHandler, AxiDmaPtr);
+
+ XScuGic_Enable(IntcInstancePtr, TxIntrId);
+ XScuGic_Enable(IntcInstancePtr, RxIntrId);
+
+ // Enable interrupts from the hardware
+ Xil_ExceptionInit();
+ Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
+ (Xil_ExceptionHandler)INTC_HANDLER,
+ (void *)IntcInstancePtr);
+
+ Xil_ExceptionEnable();
+
+ return XST_SUCCESS;
+ }
+
+ static void DisableIntrSystem(INTC * IntcInstancePtr,
+ u16 TxIntrId, u16 RxIntrId)
+ {
+ XScuGic_Disconnect(IntcInstancePtr, TxIntrId);
+ XScuGic_Disconnect(IntcInstancePtr, RxIntrId);
+ }
+
+*** Xilinx の XAxiDma ドライバ から直接叩いてみる [#a1f5bcfa]
+
+ベアメタルで試します