Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Intel I/OAT DMA Linux driver |
| 3 | * Copyright(c) 2004 - 2009 Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | * |
| 18 | * The full GNU General Public License is included in this distribution in |
| 19 | * the file called "COPYING". |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * This driver supports an Intel I/OAT DMA engine (versions >= 2), which |
| 25 | * does asynchronous data movement and checksumming operations. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 31 | #include <linux/pci.h> |
| 32 | #include <linux/interrupt.h> |
| 33 | #include <linux/dmaengine.h> |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/dma-mapping.h> |
| 36 | #include <linux/workqueue.h> |
Paul Gortmaker | 70c7160 | 2011-05-22 16:47:17 -0400 | [diff] [blame] | 37 | #include <linux/prefetch.h> |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 38 | #include <linux/i7300_idle.h> |
| 39 | #include "dma.h" |
| 40 | #include "dma_v2.h" |
| 41 | #include "registers.h" |
| 42 | #include "hw.h" |
| 43 | |
Russell King - ARM Linux | d2ebfb3 | 2012-03-06 22:34:26 +0000 | [diff] [blame^] | 44 | #include "../dmaengine.h" |
| 45 | |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 46 | int ioat_ring_alloc_order = 8; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 47 | module_param(ioat_ring_alloc_order, int, 0644); |
| 48 | MODULE_PARM_DESC(ioat_ring_alloc_order, |
Dan Williams | 376ec37 | 2009-09-16 15:16:50 -0700 | [diff] [blame] | 49 | "ioat2+: allocate 2^n descriptors per channel" |
| 50 | " (default: 8 max: 16)"); |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 51 | static int ioat_ring_max_alloc_order = IOAT_MAX_ORDER; |
| 52 | module_param(ioat_ring_max_alloc_order, int, 0644); |
| 53 | MODULE_PARM_DESC(ioat_ring_max_alloc_order, |
Dan Williams | 376ec37 | 2009-09-16 15:16:50 -0700 | [diff] [blame] | 54 | "ioat2+: upper limit for ring size (default: 16)"); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 55 | |
Dan Williams | b094ad3 | 2009-09-08 17:42:57 -0700 | [diff] [blame] | 56 | void __ioat2_issue_pending(struct ioat2_dma_chan *ioat) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 57 | { |
Dan Williams | 281befa | 2010-03-03 11:47:43 -0700 | [diff] [blame] | 58 | struct ioat_chan_common *chan = &ioat->base; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 59 | |
Dan Williams | 376ec37 | 2009-09-16 15:16:50 -0700 | [diff] [blame] | 60 | ioat->dmacount += ioat2_ring_pending(ioat); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 61 | ioat->issued = ioat->head; |
Dan Williams | 281befa | 2010-03-03 11:47:43 -0700 | [diff] [blame] | 62 | writew(ioat->dmacount, chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET); |
| 63 | dev_dbg(to_dev(chan), |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 64 | "%s: head: %#x tail: %#x issued: %#x count: %#x\n", |
| 65 | __func__, ioat->head, ioat->tail, ioat->issued, ioat->dmacount); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Dan Williams | 281befa | 2010-03-03 11:47:43 -0700 | [diff] [blame] | 68 | void ioat2_issue_pending(struct dma_chan *c) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 69 | { |
Dan Williams | 281befa | 2010-03-03 11:47:43 -0700 | [diff] [blame] | 70 | struct ioat2_dma_chan *ioat = to_ioat2_chan(c); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 71 | |
Dan Williams | 281befa | 2010-03-03 11:47:43 -0700 | [diff] [blame] | 72 | if (ioat2_ring_pending(ioat)) { |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 73 | spin_lock_bh(&ioat->prep_lock); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 74 | __ioat2_issue_pending(ioat); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 75 | spin_unlock_bh(&ioat->prep_lock); |
Dan Williams | 281befa | 2010-03-03 11:47:43 -0700 | [diff] [blame] | 76 | } |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * ioat2_update_pending - log pending descriptors |
| 81 | * @ioat: ioat2+ channel |
| 82 | * |
Dan Williams | 281befa | 2010-03-03 11:47:43 -0700 | [diff] [blame] | 83 | * Check if the number of unsubmitted descriptors has exceeded the |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 84 | * watermark. Called with prep_lock held |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 85 | */ |
| 86 | static void ioat2_update_pending(struct ioat2_dma_chan *ioat) |
| 87 | { |
Dan Williams | 281befa | 2010-03-03 11:47:43 -0700 | [diff] [blame] | 88 | if (ioat2_ring_pending(ioat) > ioat_pending_level) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 89 | __ioat2_issue_pending(ioat); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void __ioat2_start_null_desc(struct ioat2_dma_chan *ioat) |
| 93 | { |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 94 | struct ioat_ring_ent *desc; |
| 95 | struct ioat_dma_descriptor *hw; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 96 | |
| 97 | if (ioat2_ring_space(ioat) < 1) { |
| 98 | dev_err(to_dev(&ioat->base), |
| 99 | "Unable to start null desc - ring full\n"); |
| 100 | return; |
| 101 | } |
| 102 | |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 103 | dev_dbg(to_dev(&ioat->base), "%s: head: %#x tail: %#x issued: %#x\n", |
| 104 | __func__, ioat->head, ioat->tail, ioat->issued); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 105 | desc = ioat2_get_ring_ent(ioat, ioat->head); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 106 | |
| 107 | hw = desc->hw; |
| 108 | hw->ctl = 0; |
| 109 | hw->ctl_f.null = 1; |
| 110 | hw->ctl_f.int_en = 1; |
| 111 | hw->ctl_f.compl_write = 1; |
| 112 | /* set size to non-zero value (channel returns error when size is 0) */ |
| 113 | hw->size = NULL_DESC_BUFFER_SIZE; |
| 114 | hw->src_addr = 0; |
| 115 | hw->dst_addr = 0; |
| 116 | async_tx_ack(&desc->txd); |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 117 | ioat2_set_chainaddr(ioat, desc->txd.phys); |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 118 | dump_desc_dbg(ioat, desc); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 119 | wmb(); |
| 120 | ioat->head += 1; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 121 | __ioat2_issue_pending(ioat); |
| 122 | } |
| 123 | |
| 124 | static void ioat2_start_null_desc(struct ioat2_dma_chan *ioat) |
| 125 | { |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 126 | spin_lock_bh(&ioat->prep_lock); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 127 | __ioat2_start_null_desc(ioat); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 128 | spin_unlock_bh(&ioat->prep_lock); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 131 | static void __cleanup(struct ioat2_dma_chan *ioat, unsigned long phys_complete) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 132 | { |
| 133 | struct ioat_chan_common *chan = &ioat->base; |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 134 | struct dma_async_tx_descriptor *tx; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 135 | struct ioat_ring_ent *desc; |
| 136 | bool seen_current = false; |
| 137 | u16 active; |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 138 | int idx = ioat->tail, i; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 139 | |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 140 | dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n", |
| 141 | __func__, ioat->head, ioat->tail, ioat->issued); |
| 142 | |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 143 | active = ioat2_ring_active(ioat); |
| 144 | for (i = 0; i < active && !seen_current; i++) { |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 145 | smp_read_barrier_depends(); |
| 146 | prefetch(ioat2_get_ring_ent(ioat, idx + i + 1)); |
| 147 | desc = ioat2_get_ring_ent(ioat, idx + i); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 148 | tx = &desc->txd; |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 149 | dump_desc_dbg(ioat, desc); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 150 | if (tx->cookie) { |
| 151 | ioat_dma_unmap(chan, tx->flags, desc->len, desc->hw); |
Russell King - ARM Linux | 4d4e58d | 2012-03-06 22:34:06 +0000 | [diff] [blame] | 152 | chan->common.completed_cookie = tx->cookie; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 153 | tx->cookie = 0; |
| 154 | if (tx->callback) { |
| 155 | tx->callback(tx->callback_param); |
| 156 | tx->callback = NULL; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (tx->phys == phys_complete) |
| 161 | seen_current = true; |
| 162 | } |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 163 | smp_mb(); /* finish all descriptor reads before incrementing tail */ |
| 164 | ioat->tail = idx + i; |
Dan Williams | aa75db0 | 2010-03-03 21:21:10 -0700 | [diff] [blame] | 165 | BUG_ON(active && !seen_current); /* no active descs have written a completion? */ |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 166 | |
| 167 | chan->last_completion = phys_complete; |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 168 | if (active - i == 0) { |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 169 | dev_dbg(to_dev(chan), "%s: cancel completion timeout\n", |
| 170 | __func__); |
| 171 | clear_bit(IOAT_COMPLETION_PENDING, &chan->state); |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 172 | mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT); |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 175 | |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 176 | /** |
| 177 | * ioat2_cleanup - clean finished descriptors (advance tail pointer) |
| 178 | * @chan: ioat channel to be cleaned up |
| 179 | */ |
| 180 | static void ioat2_cleanup(struct ioat2_dma_chan *ioat) |
| 181 | { |
| 182 | struct ioat_chan_common *chan = &ioat->base; |
| 183 | unsigned long phys_complete; |
| 184 | |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 185 | spin_lock_bh(&chan->cleanup_lock); |
| 186 | if (ioat_cleanup_preamble(chan, &phys_complete)) |
| 187 | __cleanup(ioat, phys_complete); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 188 | spin_unlock_bh(&chan->cleanup_lock); |
| 189 | } |
| 190 | |
Dan Williams | aa4d72a | 2010-03-03 21:21:13 -0700 | [diff] [blame] | 191 | void ioat2_cleanup_event(unsigned long data) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 192 | { |
Dan Williams | aa4d72a | 2010-03-03 21:21:13 -0700 | [diff] [blame] | 193 | struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 194 | |
| 195 | ioat2_cleanup(ioat); |
Dan Williams | f6ab95b | 2009-09-08 12:01:21 -0700 | [diff] [blame] | 196 | writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 199 | void __ioat2_restart_chan(struct ioat2_dma_chan *ioat) |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 200 | { |
| 201 | struct ioat_chan_common *chan = &ioat->base; |
| 202 | |
| 203 | /* set the tail to be re-issued */ |
| 204 | ioat->issued = ioat->tail; |
| 205 | ioat->dmacount = 0; |
| 206 | set_bit(IOAT_COMPLETION_PENDING, &chan->state); |
| 207 | mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT); |
| 208 | |
| 209 | dev_dbg(to_dev(chan), |
| 210 | "%s: head: %#x tail: %#x issued: %#x count: %#x\n", |
| 211 | __func__, ioat->head, ioat->tail, ioat->issued, ioat->dmacount); |
| 212 | |
| 213 | if (ioat2_ring_pending(ioat)) { |
| 214 | struct ioat_ring_ent *desc; |
| 215 | |
| 216 | desc = ioat2_get_ring_ent(ioat, ioat->tail); |
| 217 | ioat2_set_chainaddr(ioat, desc->txd.phys); |
| 218 | __ioat2_issue_pending(ioat); |
| 219 | } else |
| 220 | __ioat2_start_null_desc(ioat); |
| 221 | } |
| 222 | |
Dan Williams | a6d52d7 | 2009-12-19 15:36:02 -0700 | [diff] [blame] | 223 | int ioat2_quiesce(struct ioat_chan_common *chan, unsigned long tmo) |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 224 | { |
Dan Williams | a6d52d7 | 2009-12-19 15:36:02 -0700 | [diff] [blame] | 225 | unsigned long end = jiffies + tmo; |
| 226 | int err = 0; |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 227 | u32 status; |
| 228 | |
| 229 | status = ioat_chansts(chan); |
| 230 | if (is_ioat_active(status) || is_ioat_idle(status)) |
| 231 | ioat_suspend(chan); |
| 232 | while (is_ioat_active(status) || is_ioat_idle(status)) { |
Dan Williams | 7e55a70 | 2010-01-13 13:33:12 -0700 | [diff] [blame] | 233 | if (tmo && time_after(jiffies, end)) { |
Dan Williams | a6d52d7 | 2009-12-19 15:36:02 -0700 | [diff] [blame] | 234 | err = -ETIMEDOUT; |
| 235 | break; |
| 236 | } |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 237 | status = ioat_chansts(chan); |
| 238 | cpu_relax(); |
| 239 | } |
| 240 | |
Dan Williams | a6d52d7 | 2009-12-19 15:36:02 -0700 | [diff] [blame] | 241 | return err; |
| 242 | } |
| 243 | |
| 244 | int ioat2_reset_sync(struct ioat_chan_common *chan, unsigned long tmo) |
| 245 | { |
| 246 | unsigned long end = jiffies + tmo; |
| 247 | int err = 0; |
| 248 | |
| 249 | ioat_reset(chan); |
| 250 | while (ioat_reset_pending(chan)) { |
| 251 | if (end && time_after(jiffies, end)) { |
| 252 | err = -ETIMEDOUT; |
| 253 | break; |
| 254 | } |
| 255 | cpu_relax(); |
| 256 | } |
| 257 | |
| 258 | return err; |
| 259 | } |
| 260 | |
| 261 | static void ioat2_restart_channel(struct ioat2_dma_chan *ioat) |
| 262 | { |
| 263 | struct ioat_chan_common *chan = &ioat->base; |
| 264 | unsigned long phys_complete; |
| 265 | |
| 266 | ioat2_quiesce(chan, 0); |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 267 | if (ioat_cleanup_preamble(chan, &phys_complete)) |
| 268 | __cleanup(ioat, phys_complete); |
| 269 | |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 270 | __ioat2_restart_chan(ioat); |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Dan Williams | e323271 | 2009-09-08 17:43:02 -0700 | [diff] [blame] | 273 | void ioat2_timer_event(unsigned long data) |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 274 | { |
Dan Williams | aa4d72a | 2010-03-03 21:21:13 -0700 | [diff] [blame] | 275 | struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data); |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 276 | struct ioat_chan_common *chan = &ioat->base; |
| 277 | |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 278 | if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) { |
| 279 | unsigned long phys_complete; |
| 280 | u64 status; |
| 281 | |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 282 | status = ioat_chansts(chan); |
| 283 | |
| 284 | /* when halted due to errors check for channel |
| 285 | * programming errors before advancing the completion state |
| 286 | */ |
| 287 | if (is_ioat_halted(status)) { |
| 288 | u32 chanerr; |
| 289 | |
| 290 | chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET); |
Dan Williams | b57014d | 2009-11-19 17:10:07 -0700 | [diff] [blame] | 291 | dev_err(to_dev(chan), "%s: Channel halted (%x)\n", |
| 292 | __func__, chanerr); |
Dan Williams | 556ab45 | 2010-07-23 15:47:56 -0700 | [diff] [blame] | 293 | if (test_bit(IOAT_RUN, &chan->state)) |
| 294 | BUG_ON(is_ioat_bug(chanerr)); |
| 295 | else /* we never got off the ground */ |
| 296 | return; |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* if we haven't made progress and we have already |
| 300 | * acknowledged a pending completion once, then be more |
| 301 | * forceful with a restart |
| 302 | */ |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 303 | spin_lock_bh(&chan->cleanup_lock); |
| 304 | if (ioat_cleanup_preamble(chan, &phys_complete)) { |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 305 | __cleanup(ioat, phys_complete); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 306 | } else if (test_bit(IOAT_COMPLETION_ACK, &chan->state)) { |
| 307 | spin_lock_bh(&ioat->prep_lock); |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 308 | ioat2_restart_channel(ioat); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 309 | spin_unlock_bh(&ioat->prep_lock); |
| 310 | } else { |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 311 | set_bit(IOAT_COMPLETION_ACK, &chan->state); |
| 312 | mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT); |
| 313 | } |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 314 | spin_unlock_bh(&chan->cleanup_lock); |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 315 | } else { |
| 316 | u16 active; |
| 317 | |
| 318 | /* if the ring is idle, empty, and oversized try to step |
| 319 | * down the size |
| 320 | */ |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 321 | spin_lock_bh(&chan->cleanup_lock); |
| 322 | spin_lock_bh(&ioat->prep_lock); |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 323 | active = ioat2_ring_active(ioat); |
| 324 | if (active == 0 && ioat->alloc_order > ioat_get_alloc_order()) |
| 325 | reshape_ring(ioat, ioat->alloc_order-1); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 326 | spin_unlock_bh(&ioat->prep_lock); |
| 327 | spin_unlock_bh(&chan->cleanup_lock); |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 328 | |
| 329 | /* keep shrinking until we get back to our minimum |
| 330 | * default size |
| 331 | */ |
| 332 | if (ioat->alloc_order > ioat_get_alloc_order()) |
| 333 | mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT); |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 334 | } |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Dan Williams | a6d52d7 | 2009-12-19 15:36:02 -0700 | [diff] [blame] | 337 | static int ioat2_reset_hw(struct ioat_chan_common *chan) |
| 338 | { |
| 339 | /* throw away whatever the channel was doing and get it initialized */ |
| 340 | u32 chanerr; |
| 341 | |
| 342 | ioat2_quiesce(chan, msecs_to_jiffies(100)); |
| 343 | |
| 344 | chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET); |
| 345 | writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET); |
| 346 | |
| 347 | return ioat2_reset_sync(chan, msecs_to_jiffies(200)); |
| 348 | } |
| 349 | |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 350 | /** |
| 351 | * ioat2_enumerate_channels - find and initialize the device's channels |
| 352 | * @device: the device to be enumerated |
| 353 | */ |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 354 | int ioat2_enumerate_channels(struct ioatdma_device *device) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 355 | { |
| 356 | struct ioat2_dma_chan *ioat; |
| 357 | struct device *dev = &device->pdev->dev; |
| 358 | struct dma_device *dma = &device->common; |
| 359 | u8 xfercap_log; |
| 360 | int i; |
| 361 | |
| 362 | INIT_LIST_HEAD(&dma->channels); |
| 363 | dma->chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET); |
Dan Williams | bb32078 | 2009-09-08 12:01:14 -0700 | [diff] [blame] | 364 | dma->chancnt &= 0x1f; /* bits [4:0] valid */ |
| 365 | if (dma->chancnt > ARRAY_SIZE(device->idx)) { |
| 366 | dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n", |
| 367 | dma->chancnt, ARRAY_SIZE(device->idx)); |
| 368 | dma->chancnt = ARRAY_SIZE(device->idx); |
| 369 | } |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 370 | xfercap_log = readb(device->reg_base + IOAT_XFERCAP_OFFSET); |
Dan Williams | bb32078 | 2009-09-08 12:01:14 -0700 | [diff] [blame] | 371 | xfercap_log &= 0x1f; /* bits [4:0] valid */ |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 372 | if (xfercap_log == 0) |
| 373 | return 0; |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 374 | dev_dbg(dev, "%s: xfercap = %d\n", __func__, 1 << xfercap_log); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 375 | |
| 376 | /* FIXME which i/oat version is i7300? */ |
| 377 | #ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL |
| 378 | if (i7300_idle_platform_probe(NULL, NULL, 1) == 0) |
| 379 | dma->chancnt--; |
| 380 | #endif |
| 381 | for (i = 0; i < dma->chancnt; i++) { |
| 382 | ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL); |
| 383 | if (!ioat) |
| 384 | break; |
| 385 | |
Dan Williams | aa4d72a | 2010-03-03 21:21:13 -0700 | [diff] [blame] | 386 | ioat_init_channel(device, &ioat->base, i); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 387 | ioat->xfercap_log = xfercap_log; |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 388 | spin_lock_init(&ioat->prep_lock); |
Dan Williams | a6d52d7 | 2009-12-19 15:36:02 -0700 | [diff] [blame] | 389 | if (device->reset_hw(&ioat->base)) { |
| 390 | i = 0; |
| 391 | break; |
| 392 | } |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 393 | } |
| 394 | dma->chancnt = i; |
| 395 | return i; |
| 396 | } |
| 397 | |
| 398 | static dma_cookie_t ioat2_tx_submit_unlock(struct dma_async_tx_descriptor *tx) |
| 399 | { |
| 400 | struct dma_chan *c = tx->chan; |
| 401 | struct ioat2_dma_chan *ioat = to_ioat2_chan(c); |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 402 | struct ioat_chan_common *chan = &ioat->base; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 403 | dma_cookie_t cookie = c->cookie; |
| 404 | |
| 405 | cookie++; |
| 406 | if (cookie < 0) |
| 407 | cookie = 1; |
| 408 | tx->cookie = cookie; |
| 409 | c->cookie = cookie; |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 410 | dev_dbg(to_dev(&ioat->base), "%s: cookie: %d\n", __func__, cookie); |
| 411 | |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 412 | if (!test_and_set_bit(IOAT_COMPLETION_PENDING, &chan->state)) |
| 413 | mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 414 | |
| 415 | /* make descriptor updates visible before advancing ioat->head, |
| 416 | * this is purposefully not smp_wmb() since we are also |
| 417 | * publishing the descriptor updates to a dma device |
| 418 | */ |
| 419 | wmb(); |
| 420 | |
| 421 | ioat->head += ioat->produce; |
| 422 | |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 423 | ioat2_update_pending(ioat); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 424 | spin_unlock_bh(&ioat->prep_lock); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 425 | |
| 426 | return cookie; |
| 427 | } |
| 428 | |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 429 | static struct ioat_ring_ent *ioat2_alloc_ring_ent(struct dma_chan *chan, gfp_t flags) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 430 | { |
| 431 | struct ioat_dma_descriptor *hw; |
| 432 | struct ioat_ring_ent *desc; |
| 433 | struct ioatdma_device *dma; |
| 434 | dma_addr_t phys; |
| 435 | |
| 436 | dma = to_ioatdma_device(chan->device); |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 437 | hw = pci_pool_alloc(dma->dma_pool, flags, &phys); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 438 | if (!hw) |
| 439 | return NULL; |
| 440 | memset(hw, 0, sizeof(*hw)); |
| 441 | |
Dan Williams | 162b96e | 2009-09-08 17:53:04 -0700 | [diff] [blame] | 442 | desc = kmem_cache_alloc(ioat2_cache, flags); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 443 | if (!desc) { |
| 444 | pci_pool_free(dma->dma_pool, hw, phys); |
| 445 | return NULL; |
| 446 | } |
Dan Williams | 162b96e | 2009-09-08 17:53:04 -0700 | [diff] [blame] | 447 | memset(desc, 0, sizeof(*desc)); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 448 | |
| 449 | dma_async_tx_descriptor_init(&desc->txd, chan); |
| 450 | desc->txd.tx_submit = ioat2_tx_submit_unlock; |
| 451 | desc->hw = hw; |
| 452 | desc->txd.phys = phys; |
| 453 | return desc; |
| 454 | } |
| 455 | |
| 456 | static void ioat2_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan) |
| 457 | { |
| 458 | struct ioatdma_device *dma; |
| 459 | |
| 460 | dma = to_ioatdma_device(chan->device); |
| 461 | pci_pool_free(dma->dma_pool, desc->hw, desc->txd.phys); |
Dan Williams | 162b96e | 2009-09-08 17:53:04 -0700 | [diff] [blame] | 462 | kmem_cache_free(ioat2_cache, desc); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 465 | static struct ioat_ring_ent **ioat2_alloc_ring(struct dma_chan *c, int order, gfp_t flags) |
| 466 | { |
| 467 | struct ioat_ring_ent **ring; |
| 468 | int descs = 1 << order; |
| 469 | int i; |
| 470 | |
| 471 | if (order > ioat_get_max_alloc_order()) |
| 472 | return NULL; |
| 473 | |
| 474 | /* allocate the array to hold the software ring */ |
| 475 | ring = kcalloc(descs, sizeof(*ring), flags); |
| 476 | if (!ring) |
| 477 | return NULL; |
| 478 | for (i = 0; i < descs; i++) { |
| 479 | ring[i] = ioat2_alloc_ring_ent(c, flags); |
| 480 | if (!ring[i]) { |
| 481 | while (i--) |
| 482 | ioat2_free_ring_ent(ring[i], c); |
| 483 | kfree(ring); |
| 484 | return NULL; |
| 485 | } |
| 486 | set_desc_id(ring[i], i); |
| 487 | } |
| 488 | |
| 489 | /* link descs */ |
| 490 | for (i = 0; i < descs-1; i++) { |
| 491 | struct ioat_ring_ent *next = ring[i+1]; |
| 492 | struct ioat_dma_descriptor *hw = ring[i]->hw; |
| 493 | |
| 494 | hw->next = next->txd.phys; |
| 495 | } |
| 496 | ring[i]->hw->next = ring[0]->txd.phys; |
| 497 | |
| 498 | return ring; |
| 499 | } |
| 500 | |
Dan Williams | 556ab45 | 2010-07-23 15:47:56 -0700 | [diff] [blame] | 501 | void ioat2_free_chan_resources(struct dma_chan *c); |
| 502 | |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 503 | /* ioat2_alloc_chan_resources - allocate/initialize ioat2 descriptor ring |
| 504 | * @chan: channel to be initialized |
| 505 | */ |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 506 | int ioat2_alloc_chan_resources(struct dma_chan *c) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 507 | { |
| 508 | struct ioat2_dma_chan *ioat = to_ioat2_chan(c); |
| 509 | struct ioat_chan_common *chan = &ioat->base; |
| 510 | struct ioat_ring_ent **ring; |
Dan Williams | 556ab45 | 2010-07-23 15:47:56 -0700 | [diff] [blame] | 511 | u64 status; |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 512 | int order; |
Dimitri Sivanich | 19d78a6 | 2011-05-06 10:33:44 -0500 | [diff] [blame] | 513 | int i = 0; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 514 | |
| 515 | /* have we already been set up? */ |
| 516 | if (ioat->ring) |
| 517 | return 1 << ioat->alloc_order; |
| 518 | |
| 519 | /* Setup register to interrupt and write completion status on error */ |
Dan Williams | f6ab95b | 2009-09-08 12:01:21 -0700 | [diff] [blame] | 520 | writew(IOAT_CHANCTRL_RUN, chan->reg_base + IOAT_CHANCTRL_OFFSET); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 521 | |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 522 | /* allocate a completion writeback area */ |
| 523 | /* doing 2 32bit writes to mmio since 1 64b write doesn't work */ |
Dan Williams | 4fb9b9e | 2009-09-08 12:01:04 -0700 | [diff] [blame] | 524 | chan->completion = pci_pool_alloc(chan->device->completion_pool, |
| 525 | GFP_KERNEL, &chan->completion_dma); |
| 526 | if (!chan->completion) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 527 | return -ENOMEM; |
| 528 | |
Dan Williams | 4fb9b9e | 2009-09-08 12:01:04 -0700 | [diff] [blame] | 529 | memset(chan->completion, 0, sizeof(*chan->completion)); |
| 530 | writel(((u64) chan->completion_dma) & 0x00000000FFFFFFFF, |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 531 | chan->reg_base + IOAT_CHANCMP_OFFSET_LOW); |
Dan Williams | 4fb9b9e | 2009-09-08 12:01:04 -0700 | [diff] [blame] | 532 | writel(((u64) chan->completion_dma) >> 32, |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 533 | chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH); |
| 534 | |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 535 | order = ioat_get_alloc_order(); |
| 536 | ring = ioat2_alloc_ring(c, order, GFP_KERNEL); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 537 | if (!ring) |
| 538 | return -ENOMEM; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 539 | |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 540 | spin_lock_bh(&chan->cleanup_lock); |
| 541 | spin_lock_bh(&ioat->prep_lock); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 542 | ioat->ring = ring; |
| 543 | ioat->head = 0; |
| 544 | ioat->issued = 0; |
| 545 | ioat->tail = 0; |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 546 | ioat->alloc_order = order; |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 547 | spin_unlock_bh(&ioat->prep_lock); |
| 548 | spin_unlock_bh(&chan->cleanup_lock); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 549 | |
| 550 | tasklet_enable(&chan->cleanup_task); |
| 551 | ioat2_start_null_desc(ioat); |
| 552 | |
Dan Williams | 556ab45 | 2010-07-23 15:47:56 -0700 | [diff] [blame] | 553 | /* check that we got off the ground */ |
Dimitri Sivanich | 19d78a6 | 2011-05-06 10:33:44 -0500 | [diff] [blame] | 554 | do { |
| 555 | udelay(1); |
| 556 | status = ioat_chansts(chan); |
| 557 | } while (i++ < 20 && !is_ioat_active(status) && !is_ioat_idle(status)); |
| 558 | |
Dan Williams | 556ab45 | 2010-07-23 15:47:56 -0700 | [diff] [blame] | 559 | if (is_ioat_active(status) || is_ioat_idle(status)) { |
| 560 | set_bit(IOAT_RUN, &chan->state); |
| 561 | return 1 << ioat->alloc_order; |
| 562 | } else { |
| 563 | u32 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET); |
| 564 | |
| 565 | dev_WARN(to_dev(chan), |
| 566 | "failed to start channel chanerr: %#x\n", chanerr); |
| 567 | ioat2_free_chan_resources(c); |
| 568 | return -EFAULT; |
| 569 | } |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 572 | bool reshape_ring(struct ioat2_dma_chan *ioat, int order) |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 573 | { |
| 574 | /* reshape differs from normal ring allocation in that we want |
| 575 | * to allocate a new software ring while only |
| 576 | * extending/truncating the hardware ring |
| 577 | */ |
| 578 | struct ioat_chan_common *chan = &ioat->base; |
| 579 | struct dma_chan *c = &chan->common; |
Dan Williams | abb12df | 2010-05-01 15:22:54 -0700 | [diff] [blame] | 580 | const u16 curr_size = ioat2_ring_size(ioat); |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 581 | const u16 active = ioat2_ring_active(ioat); |
| 582 | const u16 new_size = 1 << order; |
| 583 | struct ioat_ring_ent **ring; |
| 584 | u16 i; |
| 585 | |
| 586 | if (order > ioat_get_max_alloc_order()) |
| 587 | return false; |
| 588 | |
| 589 | /* double check that we have at least 1 free descriptor */ |
| 590 | if (active == curr_size) |
| 591 | return false; |
| 592 | |
| 593 | /* when shrinking, verify that we can hold the current active |
| 594 | * set in the new ring |
| 595 | */ |
| 596 | if (active >= new_size) |
| 597 | return false; |
| 598 | |
| 599 | /* allocate the array to hold the software ring */ |
| 600 | ring = kcalloc(new_size, sizeof(*ring), GFP_NOWAIT); |
| 601 | if (!ring) |
| 602 | return false; |
| 603 | |
| 604 | /* allocate/trim descriptors as needed */ |
| 605 | if (new_size > curr_size) { |
| 606 | /* copy current descriptors to the new ring */ |
| 607 | for (i = 0; i < curr_size; i++) { |
| 608 | u16 curr_idx = (ioat->tail+i) & (curr_size-1); |
| 609 | u16 new_idx = (ioat->tail+i) & (new_size-1); |
| 610 | |
| 611 | ring[new_idx] = ioat->ring[curr_idx]; |
| 612 | set_desc_id(ring[new_idx], new_idx); |
| 613 | } |
| 614 | |
| 615 | /* add new descriptors to the ring */ |
| 616 | for (i = curr_size; i < new_size; i++) { |
| 617 | u16 new_idx = (ioat->tail+i) & (new_size-1); |
| 618 | |
| 619 | ring[new_idx] = ioat2_alloc_ring_ent(c, GFP_NOWAIT); |
| 620 | if (!ring[new_idx]) { |
| 621 | while (i--) { |
| 622 | u16 new_idx = (ioat->tail+i) & (new_size-1); |
| 623 | |
| 624 | ioat2_free_ring_ent(ring[new_idx], c); |
| 625 | } |
| 626 | kfree(ring); |
| 627 | return false; |
| 628 | } |
| 629 | set_desc_id(ring[new_idx], new_idx); |
| 630 | } |
| 631 | |
| 632 | /* hw link new descriptors */ |
| 633 | for (i = curr_size-1; i < new_size; i++) { |
| 634 | u16 new_idx = (ioat->tail+i) & (new_size-1); |
| 635 | struct ioat_ring_ent *next = ring[(new_idx+1) & (new_size-1)]; |
| 636 | struct ioat_dma_descriptor *hw = ring[new_idx]->hw; |
| 637 | |
| 638 | hw->next = next->txd.phys; |
| 639 | } |
| 640 | } else { |
| 641 | struct ioat_dma_descriptor *hw; |
| 642 | struct ioat_ring_ent *next; |
| 643 | |
| 644 | /* copy current descriptors to the new ring, dropping the |
| 645 | * removed descriptors |
| 646 | */ |
| 647 | for (i = 0; i < new_size; i++) { |
| 648 | u16 curr_idx = (ioat->tail+i) & (curr_size-1); |
| 649 | u16 new_idx = (ioat->tail+i) & (new_size-1); |
| 650 | |
| 651 | ring[new_idx] = ioat->ring[curr_idx]; |
| 652 | set_desc_id(ring[new_idx], new_idx); |
| 653 | } |
| 654 | |
| 655 | /* free deleted descriptors */ |
| 656 | for (i = new_size; i < curr_size; i++) { |
| 657 | struct ioat_ring_ent *ent; |
| 658 | |
| 659 | ent = ioat2_get_ring_ent(ioat, ioat->tail+i); |
| 660 | ioat2_free_ring_ent(ent, c); |
| 661 | } |
| 662 | |
| 663 | /* fix up hardware ring */ |
| 664 | hw = ring[(ioat->tail+new_size-1) & (new_size-1)]->hw; |
| 665 | next = ring[(ioat->tail+new_size) & (new_size-1)]; |
| 666 | hw->next = next->txd.phys; |
| 667 | } |
| 668 | |
| 669 | dev_dbg(to_dev(chan), "%s: allocated %d descriptors\n", |
| 670 | __func__, new_size); |
| 671 | |
| 672 | kfree(ioat->ring); |
| 673 | ioat->ring = ring; |
| 674 | ioat->alloc_order = order; |
| 675 | |
| 676 | return true; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /** |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 680 | * ioat2_check_space_lock - verify space and grab ring producer lock |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 681 | * @ioat: ioat2,3 channel (ring) to operate on |
| 682 | * @num_descs: allocation length |
| 683 | */ |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 684 | int ioat2_check_space_lock(struct ioat2_dma_chan *ioat, int num_descs) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 685 | { |
| 686 | struct ioat_chan_common *chan = &ioat->base; |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 687 | bool retry; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 688 | |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 689 | retry: |
| 690 | spin_lock_bh(&ioat->prep_lock); |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 691 | /* never allow the last descriptor to be consumed, we need at |
| 692 | * least one free at all times to allow for on-the-fly ring |
| 693 | * resizing. |
| 694 | */ |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 695 | if (likely(ioat2_ring_space(ioat) > num_descs)) { |
| 696 | dev_dbg(to_dev(chan), "%s: num_descs: %d (%x:%x:%x)\n", |
| 697 | __func__, num_descs, ioat->head, ioat->tail, ioat->issued); |
| 698 | ioat->produce = num_descs; |
| 699 | return 0; /* with ioat->prep_lock held */ |
| 700 | } |
| 701 | retry = test_and_set_bit(IOAT_RESHAPE_PENDING, &chan->state); |
| 702 | spin_unlock_bh(&ioat->prep_lock); |
Dan Williams | a309218 | 2009-09-08 12:02:01 -0700 | [diff] [blame] | 703 | |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 704 | /* is another cpu already trying to expand the ring? */ |
| 705 | if (retry) |
| 706 | goto retry; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 707 | |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 708 | spin_lock_bh(&chan->cleanup_lock); |
| 709 | spin_lock_bh(&ioat->prep_lock); |
| 710 | retry = reshape_ring(ioat, ioat->alloc_order + 1); |
| 711 | clear_bit(IOAT_RESHAPE_PENDING, &chan->state); |
| 712 | spin_unlock_bh(&ioat->prep_lock); |
| 713 | spin_unlock_bh(&chan->cleanup_lock); |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 714 | |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 715 | /* if we were able to expand the ring retry the allocation */ |
| 716 | if (retry) |
| 717 | goto retry; |
| 718 | |
| 719 | if (printk_ratelimit()) |
| 720 | dev_dbg(to_dev(chan), "%s: ring full! num_descs: %d (%x:%x:%x)\n", |
| 721 | __func__, num_descs, ioat->head, ioat->tail, ioat->issued); |
| 722 | |
| 723 | /* progress reclaim in the allocation failure case we may be |
| 724 | * called under bh_disabled so we need to trigger the timer |
| 725 | * event directly |
| 726 | */ |
| 727 | if (jiffies > chan->timer.expires && timer_pending(&chan->timer)) { |
| 728 | struct ioatdma_device *device = chan->device; |
| 729 | |
| 730 | mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT); |
| 731 | device->timer_fn((unsigned long) &chan->common); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 734 | return -ENOMEM; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 737 | struct dma_async_tx_descriptor * |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 738 | ioat2_dma_prep_memcpy_lock(struct dma_chan *c, dma_addr_t dma_dest, |
| 739 | dma_addr_t dma_src, size_t len, unsigned long flags) |
| 740 | { |
| 741 | struct ioat2_dma_chan *ioat = to_ioat2_chan(c); |
| 742 | struct ioat_dma_descriptor *hw; |
| 743 | struct ioat_ring_ent *desc; |
| 744 | dma_addr_t dst = dma_dest; |
| 745 | dma_addr_t src = dma_src; |
| 746 | size_t total_len = len; |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 747 | int num_descs, idx, i; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 748 | |
| 749 | num_descs = ioat2_xferlen_to_descs(ioat, len); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 750 | if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs) == 0) |
| 751 | idx = ioat->head; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 752 | else |
| 753 | return NULL; |
Andrew Morton | f477f5b | 2009-09-21 09:17:58 -0700 | [diff] [blame] | 754 | i = 0; |
| 755 | do { |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 756 | size_t copy = min_t(size_t, len, 1 << ioat->xfercap_log); |
| 757 | |
| 758 | desc = ioat2_get_ring_ent(ioat, idx + i); |
| 759 | hw = desc->hw; |
| 760 | |
| 761 | hw->size = copy; |
| 762 | hw->ctl = 0; |
| 763 | hw->src_addr = src; |
| 764 | hw->dst_addr = dst; |
| 765 | |
| 766 | len -= copy; |
| 767 | dst += copy; |
| 768 | src += copy; |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 769 | dump_desc_dbg(ioat, desc); |
Andrew Morton | f477f5b | 2009-09-21 09:17:58 -0700 | [diff] [blame] | 770 | } while (++i < num_descs); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 771 | |
| 772 | desc->txd.flags = flags; |
| 773 | desc->len = total_len; |
| 774 | hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT); |
Dan Williams | 128f2d5 | 2009-09-08 17:42:53 -0700 | [diff] [blame] | 775 | hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 776 | hw->ctl_f.compl_write = 1; |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 777 | dump_desc_dbg(ioat, desc); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 778 | /* we leave the channel locked to ensure in order submission */ |
| 779 | |
| 780 | return &desc->txd; |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * ioat2_free_chan_resources - release all the descriptors |
| 785 | * @chan: the channel to be cleaned |
| 786 | */ |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 787 | void ioat2_free_chan_resources(struct dma_chan *c) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 788 | { |
| 789 | struct ioat2_dma_chan *ioat = to_ioat2_chan(c); |
| 790 | struct ioat_chan_common *chan = &ioat->base; |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 791 | struct ioatdma_device *device = chan->device; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 792 | struct ioat_ring_ent *desc; |
| 793 | const u16 total_descs = 1 << ioat->alloc_order; |
| 794 | int descs; |
| 795 | int i; |
| 796 | |
| 797 | /* Before freeing channel resources first check |
| 798 | * if they have been previously allocated for this channel. |
| 799 | */ |
| 800 | if (!ioat->ring) |
| 801 | return; |
| 802 | |
| 803 | tasklet_disable(&chan->cleanup_task); |
Dan Williams | 09c8a5b | 2009-09-08 12:01:49 -0700 | [diff] [blame] | 804 | del_timer_sync(&chan->timer); |
Dan Williams | aa4d72a | 2010-03-03 21:21:13 -0700 | [diff] [blame] | 805 | device->cleanup_fn((unsigned long) c); |
Dan Williams | a6d52d7 | 2009-12-19 15:36:02 -0700 | [diff] [blame] | 806 | device->reset_hw(chan); |
Dan Williams | 556ab45 | 2010-07-23 15:47:56 -0700 | [diff] [blame] | 807 | clear_bit(IOAT_RUN, &chan->state); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 808 | |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 809 | spin_lock_bh(&chan->cleanup_lock); |
| 810 | spin_lock_bh(&ioat->prep_lock); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 811 | descs = ioat2_ring_space(ioat); |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 812 | dev_dbg(to_dev(chan), "freeing %d idle descriptors\n", descs); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 813 | for (i = 0; i < descs; i++) { |
| 814 | desc = ioat2_get_ring_ent(ioat, ioat->head + i); |
| 815 | ioat2_free_ring_ent(desc, c); |
| 816 | } |
| 817 | |
| 818 | if (descs < total_descs) |
| 819 | dev_err(to_dev(chan), "Freeing %d in use descriptors!\n", |
| 820 | total_descs - descs); |
| 821 | |
| 822 | for (i = 0; i < total_descs - descs; i++) { |
| 823 | desc = ioat2_get_ring_ent(ioat, ioat->tail + i); |
Dan Williams | 6df9183 | 2009-09-08 12:00:55 -0700 | [diff] [blame] | 824 | dump_desc_dbg(ioat, desc); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 825 | ioat2_free_ring_ent(desc, c); |
| 826 | } |
| 827 | |
| 828 | kfree(ioat->ring); |
| 829 | ioat->ring = NULL; |
| 830 | ioat->alloc_order = 0; |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 831 | pci_pool_free(device->completion_pool, chan->completion, |
Dan Williams | 4fb9b9e | 2009-09-08 12:01:04 -0700 | [diff] [blame] | 832 | chan->completion_dma); |
Dan Williams | 074cc47 | 2010-05-01 15:22:55 -0700 | [diff] [blame] | 833 | spin_unlock_bh(&ioat->prep_lock); |
| 834 | spin_unlock_bh(&chan->cleanup_lock); |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 835 | |
| 836 | chan->last_completion = 0; |
Dan Williams | 4fb9b9e | 2009-09-08 12:01:04 -0700 | [diff] [blame] | 837 | chan->completion_dma = 0; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 838 | ioat->dmacount = 0; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Dan Williams | 5669e31 | 2009-09-08 17:42:56 -0700 | [diff] [blame] | 841 | static ssize_t ring_size_show(struct dma_chan *c, char *page) |
| 842 | { |
| 843 | struct ioat2_dma_chan *ioat = to_ioat2_chan(c); |
| 844 | |
| 845 | return sprintf(page, "%d\n", (1 << ioat->alloc_order) & ~1); |
| 846 | } |
| 847 | static struct ioat_sysfs_entry ring_size_attr = __ATTR_RO(ring_size); |
| 848 | |
| 849 | static ssize_t ring_active_show(struct dma_chan *c, char *page) |
| 850 | { |
| 851 | struct ioat2_dma_chan *ioat = to_ioat2_chan(c); |
| 852 | |
| 853 | /* ...taken outside the lock, no need to be precise */ |
| 854 | return sprintf(page, "%d\n", ioat2_ring_active(ioat)); |
| 855 | } |
| 856 | static struct ioat_sysfs_entry ring_active_attr = __ATTR_RO(ring_active); |
| 857 | |
| 858 | static struct attribute *ioat2_attrs[] = { |
| 859 | &ring_size_attr.attr, |
| 860 | &ring_active_attr.attr, |
| 861 | &ioat_cap_attr.attr, |
| 862 | &ioat_version_attr.attr, |
| 863 | NULL, |
| 864 | }; |
| 865 | |
| 866 | struct kobj_type ioat2_ktype = { |
| 867 | .sysfs_ops = &ioat_sysfs_ops, |
| 868 | .default_attrs = ioat2_attrs, |
| 869 | }; |
| 870 | |
Dan Williams | 345d852 | 2009-09-08 12:01:30 -0700 | [diff] [blame] | 871 | int __devinit ioat2_dma_probe(struct ioatdma_device *device, int dca) |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 872 | { |
| 873 | struct pci_dev *pdev = device->pdev; |
| 874 | struct dma_device *dma; |
| 875 | struct dma_chan *c; |
| 876 | struct ioat_chan_common *chan; |
| 877 | int err; |
| 878 | |
| 879 | device->enumerate_channels = ioat2_enumerate_channels; |
Dan Williams | a6d52d7 | 2009-12-19 15:36:02 -0700 | [diff] [blame] | 880 | device->reset_hw = ioat2_reset_hw; |
Dan Williams | aa4d72a | 2010-03-03 21:21:13 -0700 | [diff] [blame] | 881 | device->cleanup_fn = ioat2_cleanup_event; |
Dan Williams | bf40a68 | 2009-09-08 17:42:55 -0700 | [diff] [blame] | 882 | device->timer_fn = ioat2_timer_event; |
Dan Williams | 9de6fc7 | 2009-09-08 17:42:58 -0700 | [diff] [blame] | 883 | device->self_test = ioat_dma_self_test; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 884 | dma = &device->common; |
| 885 | dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock; |
| 886 | dma->device_issue_pending = ioat2_issue_pending; |
| 887 | dma->device_alloc_chan_resources = ioat2_alloc_chan_resources; |
| 888 | dma->device_free_chan_resources = ioat2_free_chan_resources; |
Dan Williams | c50a898 | 2010-10-13 15:43:10 -0700 | [diff] [blame] | 889 | dma->device_tx_status = ioat_dma_tx_status; |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 890 | |
| 891 | err = ioat_probe(device); |
| 892 | if (err) |
| 893 | return err; |
| 894 | ioat_set_tcp_copy_break(2048); |
| 895 | |
| 896 | list_for_each_entry(c, &dma->channels, device_node) { |
| 897 | chan = to_chan_common(c); |
| 898 | writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE | IOAT_DMA_DCA_ANY_CPU, |
| 899 | chan->reg_base + IOAT_DCACTRL_OFFSET); |
| 900 | } |
| 901 | |
| 902 | err = ioat_register(device); |
| 903 | if (err) |
| 904 | return err; |
Dan Williams | 5669e31 | 2009-09-08 17:42:56 -0700 | [diff] [blame] | 905 | |
| 906 | ioat_kobject_add(device, &ioat2_ktype); |
| 907 | |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 908 | if (dca) |
| 909 | device->dca = ioat2_dca_init(pdev, device->reg_base); |
| 910 | |
Dan Williams | 5cbafa6 | 2009-08-26 13:01:44 -0700 | [diff] [blame] | 911 | return err; |
| 912 | } |