| 1 | /* |
| 2 | * Copyright 2010-2017 Intel Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * Disclaimer: The codes contained in these modules may be specific to |
| 14 | * the Intel Software Development Platform codenamed Knights Ferry, |
| 15 | * and the Intel product codenamed Knights Corner, and are not backward |
| 16 | * compatible with other Intel products. Additionally, Intel will NOT |
| 17 | * support the codes or instruction set in future products. |
| 18 | * |
| 19 | * Intel offers no warranty of any kind regarding the code. This code is |
| 20 | * licensed on an "AS IS" basis and Intel is not obligated to provide |
| 21 | * any support, assistance, installation, training, or other services |
| 22 | * of any kind. Intel is also not obligated to provide any updates, |
| 23 | * enhancements or extensions. Intel specifically disclaims any warranty |
| 24 | * of merchantability, non-infringement, fitness for any particular |
| 25 | * purpose, and any other warranty. |
| 26 | * |
| 27 | * Further, Intel disclaims all liability of any kind, including but |
| 28 | * not limited to liability for infringement of any proprietary rights, |
| 29 | * relating to the use of the code, even if Intel is notified of the |
| 30 | * possibility of such liability. Except as expressly stated in an Intel |
| 31 | * license agreement provided with this code and agreed upon with Intel, |
| 32 | * no license, express or implied, by estoppel or otherwise, to any |
| 33 | * intellectual property rights is granted herein. |
| 34 | */ |
| 35 | |
| 36 | #ifndef MIC_DMA_LIB_H |
| 37 | #define MIC_DMA_LIB_H |
| 38 | |
| 39 | #include <linux/errno.h> |
| 40 | #include <linux/hardirq.h> |
| 41 | #include <linux/types.h> |
| 42 | #include <linux/capability.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/string.h> |
| 45 | #include <linux/gfp.h> |
| 46 | #include <linux/vmalloc.h> |
| 47 | #include <asm/io.h> |
| 48 | #include <linux/kernel.h> |
| 49 | #include <linux/mm_types.h> |
| 50 | #include <linux/jiffies.h> |
| 51 | #include <linux/timer.h> |
| 52 | #include <linux/irqflags.h> |
| 53 | #include <linux/time.h> |
| 54 | #include <linux/spinlock.h> |
| 55 | #include <linux/mutex.h> |
| 56 | #include <linux/semaphore.h> |
| 57 | #include <linux/kthread.h> |
| 58 | #include <linux/sched.h> |
| 59 | #include <linux/delay.h> |
| 60 | #include <linux/wait.h> |
| 61 | #include <asm/bug.h> |
| 62 | #include <linux/pci.h> |
| 63 | #include <linux/device.h> |
| 64 | #include <linux/fs.h> |
| 65 | #include <linux/list.h> |
| 66 | #include <linux/workqueue.h> |
| 67 | #include <linux/interrupt.h> |
| 68 | #include <asm/atomic.h> |
| 69 | #include <linux/netdevice.h> |
| 70 | #include <linux/debugfs.h> |
| 71 | |
| 72 | /* Program SUD for poll ring */ |
| 73 | #define DO_DMA_POLLING (1<<0) |
| 74 | /* Program SUD for interrupt ring */ |
| 75 | #define DO_DMA_INTR (1<<1) |
| 76 | |
| 77 | struct dma_channel; |
| 78 | |
| 79 | struct dma_completion_cb { |
| 80 | void (*dma_completion_func) (uint64_t cookie); |
| 81 | uint64_t cb_cookie; |
| 82 | uint8_t *temp_buf; |
| 83 | uint8_t *temp_buf_to_free; |
| 84 | bool is_cache; |
| 85 | uint64_t dst_offset; |
| 86 | uint64_t tmp_offset; |
| 87 | struct reg_range_t *dst_window; |
| 88 | size_t len; |
| 89 | dma_addr_t temp_phys; |
| 90 | int remote_node; |
| 91 | int header_padding; |
| 92 | }; |
| 93 | |
| 94 | int get_chan_num(struct dma_channel *chan); |
| 95 | /* |
| 96 | * do_dma - main dma function: perform a dma memcpy, len bytes from src to dst |
| 97 | * |
| 98 | * @chan - DMA channel to use for the transfer. The channel can be allocated |
| 99 | * dynamically by calling allocate_dma_channel, or statically by |
| 100 | * reserve_dma_channel. Using a channel not allocated in this way will |
| 101 | * result in undefined behavior. |
| 102 | * @flags - ATOMIC, called from an interrupt context (no blocking) |
| 103 | * @src - src physical address |
| 104 | * @dst - dst physical address |
| 105 | * @len - Length of the dma |
| 106 | * @comp_cb - When the DMA is complete, the struct's function will be called. NOTE! |
| 107 | * comp_cb(cb_cookie) is called from an interrupt context, so the |
| 108 | * function must not sleep or block. |
| 109 | * |
| 110 | * Return < 1 on error |
| 111 | * Return 0 on success and DMA is completed |
| 112 | * Return > 1: DMA has been queued. Return value can be polled on for completion |
| 113 | * (poll cookie). An example (simplified w/ no error handling). |
| 114 | * int cookie = do_dma(...); |
| 115 | * while (poll_dma_completion(cookie) == 0); |
| 116 | * printf("DMA now complete\n"); |
| 117 | */ |
| 118 | int do_dma(struct dma_channel *chan, int flags, |
| 119 | uint64_t src, uint64_t dst, size_t len, |
| 120 | struct dma_completion_cb *comp_cb); |
| 121 | /* |
| 122 | * poll_dma_completion - check if a DMA is complete |
| 123 | * |
| 124 | * @poll_cookie - value returned from do_dma |
| 125 | * |
| 126 | * Returns |
| 127 | * < 0 -> error (e.g., invalid cookie) |
| 128 | * 0 -> DMA pending |
| 129 | * 1 -> DMA completed |
| 130 | * |
| 131 | * Note: This is mostly useful after calling do_dma with a NULL comp_cb parameter, as |
| 132 | * it will allow the caller to wait for DMA completion. |
| 133 | */ |
| 134 | int poll_dma_completion(int poll_cookie, struct dma_channel *chan); |
| 135 | |
| 136 | /* |
| 137 | * do_status_update: Update physical address location with the value provided. |
| 138 | * Ensures all previous DMA descriptors submitted on this DMA |
| 139 | * channel are executed. |
| 140 | * @chan - DMA channel to use for the transfer. The channel can be allocated |
| 141 | * dynamically by calling allocate_dma_channel, or statically by |
| 142 | * reserve_dma_channel. Using a channel not allocated in this way will |
| 143 | * result in undefined behavior. |
| 144 | * @phys - physical address |
| 145 | * @value - Value to be programmed |
| 146 | * |
| 147 | * Return 0 on success and appropriate error value on error. |
| 148 | */ |
| 149 | int do_status_update(struct dma_channel *chan, uint64_t phys, uint64_t value); |
| 150 | |
| 151 | /* |
| 152 | * get_dma_mark: Obtain current value of DMA mark |
| 153 | * @chan - DMA channel to use for the transfer. The channel can be allocated |
| 154 | * dynamically by calling allocate_dma_channel, or statically by |
| 155 | * reserve_dma_channel. Using a channel not allocated in this way will |
| 156 | * result in undefined behavior. |
| 157 | * |
| 158 | * Return mark. |
| 159 | */ |
| 160 | int get_dma_mark(struct dma_channel *chan); |
| 161 | |
| 162 | /* |
| 163 | * is_current_dma_mark: Check if the dma mark provided is the current DMA mark. |
| 164 | * @chan - DMA channel |
| 165 | * @mark - DMA mark |
| 166 | * |
| 167 | * Return true on success and false on failure. |
| 168 | */ |
| 169 | bool is_current_dma_mark(struct dma_channel *chan, int mark); |
| 170 | |
| 171 | /* |
| 172 | * program_dma_mark: Increment the current value of the DMA mark for a DMA channel |
| 173 | * and program an interrupt status update descriptor which ensures that all DMA |
| 174 | * descriptors programmed until this point in time are completed. |
| 175 | * @chan - DMA channel to use for the transfer. The channel can be allocated |
| 176 | * dynamically by calling allocate_dma_channel, or statically by |
| 177 | * reserve_dma_channel. Using a channel not allocated in this way will |
| 178 | * result in undefined behavior. |
| 179 | * |
| 180 | * Return mark upon success and appropriate negative error value on error. |
| 181 | */ |
| 182 | int program_dma_mark(struct dma_channel *chan); |
| 183 | |
| 184 | /* |
| 185 | * is_dma_mark_wait: Check if the dma mark provided has been processed. |
| 186 | * @chan - DMA channel |
| 187 | * @mark - DMA mark |
| 188 | * |
| 189 | * Return true on success and false on failure. |
| 190 | */ |
| 191 | bool is_dma_mark_processed(struct dma_channel *chan, int mark); |
| 192 | |
| 193 | /* |
| 194 | * dma_mark_wait: Wait for the dma mark to complete. |
| 195 | * @chan - DMA channel |
| 196 | * @mark - DMA mark |
| 197 | * @is_interruptible - Use wait_event_interruptible() or not. |
| 198 | * |
| 199 | * Return 0 on success and appropriate error value on error. |
| 200 | */ |
| 201 | int dma_mark_wait(struct dma_channel *chan, int mark, bool is_interruptible); |
| 202 | |
| 203 | #ifndef _MIC_SCIF_ |
| 204 | void host_dma_lib_interrupt_handler(struct dma_channel *chan); |
| 205 | #endif |
| 206 | |
| 207 | #endif /* MIC_DMA_LIB_H */ |