履歴: 電気回路/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 18:07:45) → 比較: 電気回路/zynq/DMA処理 (2017/2/13 8:38:01)

@@ -4,40 +4,540 @@
* 概要 [#f674dcb8]
-Zynq で DMA 処理するために勉強中の内容です。
+Zynq で簡単な DMA 処理をするための手順をまとめました。
-まだ動かせていないので間違ったことも多いと思います。
+Web上で探すといろんなドライバに関する情報が交錯していて、
+正解にたどり着くまでに非常に苦労しました。
-* axi_dma のドキュメント [#e7f3007e]
+- Xilinx のベアメタルドライバ
+-- Linux とはコンパチビリティのないドライバ
+- Xilinx の Linux ドライバ
+-- Linux 上で他のデバイスドライバから使われることを想定したドライバ
+-- ユーザーアプリケーションから使うものではない
+- サードパーティーの bperez77/xilinx_axidma
+-- Xilinx の Linux ドライバの上に構築された汎用ドライバ
+-- /dev/axidma 経由で簡単に DMA 転送が行える
-デザインダイアグラム上で axi_dma IP を右クリックして
-[IP Documentations] から各種ドキュメントが出る。
+のようにいろいろあるのですが、お手軽に使うには
+bperez77/xilinx_axidma が良さそうです。
-https://www.xilinx.com/products/intellectual-property/axi_dma.html#overview
+サードパーティー製のドライバは他にもいくるかあるようですが、
+少なくとも上記のものは単純転送で割り込み通知を使うだけなら
+非常に簡単に使えるのでおすすめです。
-など。
+Scatter Gather 機能を使いたければもう少し苦労が必要なのかもしれませんが、
+割り込みハンドラのスレッドを分けてしまえば、
+次のパケットを CPU で受け取るというのも、
+そんなに悪くない設計に思えます。
-* 後で読む [#te8e7275]
+* bperez77/xilinx_axidma の使い方を調べる [#h7b14a1c]
-Zynq DMA 用 Linux ドライバ:~
-https://github.com/bmartini/zynq-xdma
+これが使えるとかなり楽ができそう。
-その使用例?:~
-https://tobygomersallwordpresscom.wordpress.com/2016/02/11/xilinx-zynq-using-dma-to-transfer-data-to-the-linux-userspace/
+https://github.com/bperez77/xilinx_axidma
-上手に動かさないと速度が出ない:~
-https://github.com/bmartini/zynq-xdma/issues/6
+** カーネルオプションを確認 [#c50efd43]
-Zynq DMA 用 Linux ドライバ2:~
-https://github.com/bmartini/zynq-axis.git
+ドライバに書かれている条件を満たしているか、
+z-turn 付属のカーネル設定を調べます。
-[stackoverflow] Linux DMAドライバのインスタンス化および利用方法~
-http://qiita.com/wstone/items/ab4d47a3690bb1a9bf4c ~
+- CONFIG_CMA=y
+- CONFIG_DMA_CMA=y
+- CONFIG_XILINX_DMAENGINES=y
+- CONFIG_XILINX_AXIDMA=y
+- CONFIG_XILINX_AXIVDMA=y
+- CONFIG_DMA_SHARED_BUFFER=y
-AXI DMA with Zynq Running Linux Accepted Solution [Solved] ~
-https://forums.xilinx.com/t5/Embedded-Linux/AXI-DMA-with-Zynq-Running-Linux/td-p/522755 ~
+のうち、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 のことのようなのですが、どこで調べられるのかよくわかっていません。。。
+
+始め dma-s2mm-channel のアドレスを 40400000 としていて
+うまく行かずはまりました。40400000 は mm2s 用のレジスタのために確保されているので、
+mm2s を実装しているかどうかにかかわらず、s2mm のレジスタは 40400030 から始まります。
+** axidma デバイスが登録された [#nb7fbf42]
+
+上記 devicetree をインストールしたところ、起動メッセージに、
+
+ 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 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
+
+正しく入ったみたい。
+
+** bootargs の設定 [#q00cefa6]
+
+ドライバの説明書きには
+
+uEnv.txt の bootargs 行に 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
+
+so ファイルはあるにもかかわらずリンクできないという症状に悩まされました。
+
+理由がわからずかなり悩みましたが、
+xilinx_axidma の make は exmples のコードもリンクできているのだから、
+それをまねすればいいはず!と気づいて(実はデバイスドライバの説明にも「まねしろ」と書かれてました orz)、まずはどうしているか確認。
+
+ 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 の xilinx_dma.c のここで、~
+https://github.com/Xilinx/linux-xlnx/blob/f6b354241c0b0eebd8aa9bff1411c24301e1e50f/drivers/dma/xilinx/xilinx_dma.c#L1474
+
+axi_dma のステータスレジスタのエラービットをそのまま表示したのが 0x11 ということになる。
+
+axi_dma のリファレンスによれば、~
+https://www.xilinx.com/support/documentation/ip_documentation/axi_dma/v7_1/pg021_axi_dma.pdf
+
+&ref(dmasr_register.png,,66%);
+
+0x11 = DMAIntErr + Halted なので、問題は DMAIntErr だ。
+
+このエラーが出る理由は、Stream から送られたデータ量が読み込み側が期待するより大きいとき、
+とのことで、、、マジカッカカカ。
+
+読み取りパケットサイズと送信パケットサイズを合わせなければならないのだそうです。
+
+** 受信できました [#md9f68cd]
+
+パケットサイズをハードウェアに合わせて 256ワード = 1024バイト にする。
+
+あと、TRANSFER_LEN をワード単位と勘違いしていたので、
+display_result を修正して、
+
+ LANG:c
+ #define BUFFER_SIZE 2048
+ #define TRANSFER_LEN 1024
+ ...
+ void display_result(void *buffer)
+ {
+ int i, *p = (int*)buffer;
+ for(i=0; i<TRANSFER_LEN/4; i++) {
+ ...
+
+としたところ、正しく動作しました。
+
+ LANG:console
+ $ sudo ./axidmatest
+ 転送開始
+ xilinx_dma_start_transfer::simple DMA mode
+ 00000800 00000801 00000802 00000803 00000804 00000805 00000806 00000807
+ 00000808 00000809 0000080a 0000080b 0000080c 0000080d 0000080e 0000080f
+ ...
+ 000008f8 000008f9 000008fa 000008fb 000008fc 000008fd 000008fe 000008ff
+ $ cat /sys/class/dma/dma1chan0/memcpy_count
+ 0
+ $ cat /sys/class/dma/dma1chan0/bytes_transferred
+ 0
+
+/sys/class/dma/dma1chan0/ 以下の値が変化しないのはそういうものなのかどうか。
+
+** 次善にパケットサイズがわかっていないと受け取れない? [#eead2d62]
+
+このインタフェースを用いる限り、
+いつどのくらいの頻度で送られてくるかわからないデータを
+DMA を使って受け取るのは難しいみたいですね。
+
+axi_dma 自体の説明によれば時間、あるいはパケットサイズで割り込みを送出する
+機能があるようなので、どうにかうまく使えないか、後で調べたいと思います。
+
+* 以下、もっと突っ込んだ話を勉強しなければならなくなった時用の資料 [#n06c1de4]
+
+というか、上記の簡単な解にたどり着くまでに苦労した経緯だったりもします。
+
+** 後で読むリスト [#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]
@@ -47,17 +547,10 @@
http://marsee101.blog19.fc2.com/blog-entry-2297.html
-* udmabuf [#o0e80ea9]
-
-DMA バッファとして使うメモリ領域に Linux のユーザーアプリケーションから
-アクセスするには udmabuf を使うのが良いらしい?
-
-http://qiita.com/ikwzm/items/cc1bb33ff43a491440ea
-
* Xilinx の XAxiDma ドライバ [#b6740db8]
自分で Linux ドライバを作るときやベアメタルでは
-Xilinx の低レベルドライバを使うことになる。
+Xilinx の低レベルドライバを使うことになる?
C:\Xilinx\SDK\2016.4\data\embeddedsw\XilinxProcessorIPLib\drivers\axidma_v9_3\src
@@ -68,6 +561,7 @@
にドキュメントがある。[File List] から各ヘッダファイル *.h
をクリックして関数一覧を見るのがわかりやすそう
+
** ドキュメントトップの日本語訳 [#z33ce603]
以下は AXI DMA エンジンドライバの API です。
@@ -555,74 +1049,55 @@
- XAxiDma_BdGetAppWord(BdPtr, Offset, int *Valid);
- XAxiDma_BdSetAppWord(BdPtr, Offset, Word);
-* Xilinx の XAxiDma ドライバのサンプルを読む [#e3026669]
+** Xilinx の XAxiDma ドライバのサンプルを読む [#e3026669]
エラー処理を飛ばして読んでみます。
割り込みについて XPAR_INTC_0_DEVICE_ID による #ifdef があるけれど、
Zynq は GIC を持っているので、XPAR_INTC_0_DEVICE_ID をセットしない、
-xscugic.h ベースのコードを追えばいいはず。
+xscugic.h ベースのコードを追えばいいはず?
Zynq での割り込み使用例:~
https://japan.xilinx.com/support/answers/50572.html
-** xaxidma_example_simple_poll.c [#xf15a662]
+*** xaxidma_example_simple_intr.c [#l75492f1]
- LANG:c
- void main()
- {
- /* Run the poll example for simple transfer */
- XAxiDma_SimplePollExample(DMA_DEV_ID);
- }
-
- int XAxiDma_SimplePollExample(u16 DeviceId)
- {
- XAxiDma_Config *CfgPtr;
- int Status;
- int Tries = NUMBER_OF_TRANSFERS;
- int Index;
- u8 *TxBufferPtr;
- u8 *RxBufferPtr;
- u8 Value;
-
- TxBufferPtr = (u8 *)TX_BUFFER_BASE;
- RxBufferPtr = (u8 *)RX_BUFFER_BASE;
-
- // Initialize the XAxiDma device.
- CfgPtr = XAxiDma_LookupConfig(DeviceId);
- XAxiDma_CfgInitialize(&AxiDma, CfgPtr);
- assert(! XAxiDma_HasSg(&AxiDma)); // SG mode でないことを確認
-
- // Disable interrupts, we use polling mode
- XAxiDma_IntrDisable(&AxiDma, XAXIDMA_IRQ_ALL_MASK, XAXIDMA_DEVICE_TO_DMA);
- XAxiDma_IntrDisable(&AxiDma, XAXIDMA_IRQ_ALL_MASK, XAXIDMA_DMA_TO_DEVICE);
-
- InitializeTxBuffer(TxBufferPtr);
-
- // キャッシュのフラッシュ
- Xil_DCacheFlushRange((UINTPTR)TxBufferPtr, MAX_PKT_LEN);
-
- // 受信要求
- XAxiDma_SimpleTransfer(&AxiDma,(UINTPTR) RxBufferPtr,
- MAX_PKT_LEN, XAXIDMA_DEVICE_TO_DMA);
-
- // 送信要求
- XAxiDma_SimpleTransfer(&AxiDma,(UINTPTR) TxBufferPtr,
- MAX_PKT_LEN, XAXIDMA_DMA_TO_DEVICE);
-
- // 終わるのを待つ
- while ( (XAxiDma_Busy(&AxiDma,XAXIDMA_DEVICE_TO_DMA)) ||
- (XAxiDma_Busy(&AxiDma,XAXIDMA_DMA_TO_DEVICE)) ) {
- /* Wait */
- }
-
- // キャッシュの Invalidate
- Xil_DCacheInvalidateRange((UINTPTR)RxPacket, MAX_PKT_LEN);
- return XST_SUCCESS;
- }
++ 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 から割り込みハンドラを解除する
-** xaxidma_example_simple_intr.c [#l75492f1]
-
LANG:c
#include "xscugic.h"
@@ -808,3 +1283,7 @@
XScuGic_Disconnect(IntcInstancePtr, TxIntrId);
XScuGic_Disconnect(IntcInstancePtr, RxIntrId);
}
+
+*** Xilinx の XAxiDma ドライバ から直接叩いてみる [#a1f5bcfa]
+
+ベアメタルで試します