Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the Free |
| 6 | * Software Foundation; either version 2 of the License, or (at your option) |
| 7 | * any later version. |
| 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., 59 |
| 16 | * Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | * |
| 18 | * The full GNU General Public License is included in this distribution in the |
| 19 | * file called COPYING. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * This driver supports an Intel I/OAT DMA engine, which does asynchronous |
| 24 | * copy operations. |
| 25 | */ |
| 26 | |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/pci.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/dmaengine.h> |
| 32 | #include <linux/delay.h> |
David S. Miller | 6b00c92 | 2006-05-23 17:37:58 -0700 | [diff] [blame] | 33 | #include <linux/dma-mapping.h> |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 34 | #include "ioatdma.h" |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 35 | #include "ioatdma_registers.h" |
| 36 | #include "ioatdma_hw.h" |
| 37 | |
| 38 | #define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common) |
| 39 | #define to_ioat_device(dev) container_of(dev, struct ioat_device, common) |
| 40 | #define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node) |
| 41 | |
| 42 | /* internal functions */ |
| 43 | static int __devinit ioat_probe(struct pci_dev *pdev, const struct pci_device_id *ent); |
Dan Aloni | 428ed60 | 2007-03-08 09:57:36 -0800 | [diff] [blame^] | 44 | static void ioat_shutdown(struct pci_dev *pdev); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 45 | static void __devexit ioat_remove(struct pci_dev *pdev); |
| 46 | |
| 47 | static int enumerate_dma_channels(struct ioat_device *device) |
| 48 | { |
| 49 | u8 xfercap_scale; |
| 50 | u32 xfercap; |
| 51 | int i; |
| 52 | struct ioat_dma_chan *ioat_chan; |
| 53 | |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 54 | device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET); |
| 55 | xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 56 | xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale)); |
| 57 | |
| 58 | for (i = 0; i < device->common.chancnt; i++) { |
| 59 | ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL); |
| 60 | if (!ioat_chan) { |
| 61 | device->common.chancnt = i; |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | ioat_chan->device = device; |
| 66 | ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1)); |
| 67 | ioat_chan->xfercap = xfercap; |
| 68 | spin_lock_init(&ioat_chan->cleanup_lock); |
| 69 | spin_lock_init(&ioat_chan->desc_lock); |
| 70 | INIT_LIST_HEAD(&ioat_chan->free_desc); |
| 71 | INIT_LIST_HEAD(&ioat_chan->used_desc); |
| 72 | /* This should be made common somewhere in dmaengine.c */ |
| 73 | ioat_chan->common.device = &device->common; |
| 74 | ioat_chan->common.client = NULL; |
| 75 | list_add_tail(&ioat_chan->common.device_node, |
| 76 | &device->common.channels); |
| 77 | } |
| 78 | return device->common.chancnt; |
| 79 | } |
| 80 | |
| 81 | static struct ioat_desc_sw *ioat_dma_alloc_descriptor( |
| 82 | struct ioat_dma_chan *ioat_chan, |
Al Viro | 47b1653 | 2006-10-10 22:45:47 +0100 | [diff] [blame] | 83 | gfp_t flags) |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 84 | { |
| 85 | struct ioat_dma_descriptor *desc; |
| 86 | struct ioat_desc_sw *desc_sw; |
| 87 | struct ioat_device *ioat_device; |
| 88 | dma_addr_t phys; |
| 89 | |
| 90 | ioat_device = to_ioat_device(ioat_chan->common.device); |
| 91 | desc = pci_pool_alloc(ioat_device->dma_pool, flags, &phys); |
| 92 | if (unlikely(!desc)) |
| 93 | return NULL; |
| 94 | |
| 95 | desc_sw = kzalloc(sizeof(*desc_sw), flags); |
| 96 | if (unlikely(!desc_sw)) { |
| 97 | pci_pool_free(ioat_device->dma_pool, desc, phys); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | memset(desc, 0, sizeof(*desc)); |
| 102 | desc_sw->hw = desc; |
| 103 | desc_sw->phys = phys; |
| 104 | |
| 105 | return desc_sw; |
| 106 | } |
| 107 | |
| 108 | #define INITIAL_IOAT_DESC_COUNT 128 |
| 109 | |
| 110 | static void ioat_start_null_desc(struct ioat_dma_chan *ioat_chan); |
| 111 | |
| 112 | /* returns the actual number of allocated descriptors */ |
| 113 | static int ioat_dma_alloc_chan_resources(struct dma_chan *chan) |
| 114 | { |
| 115 | struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan); |
| 116 | struct ioat_desc_sw *desc = NULL; |
| 117 | u16 chanctrl; |
| 118 | u32 chanerr; |
| 119 | int i; |
| 120 | LIST_HEAD(tmp_list); |
| 121 | |
| 122 | /* |
| 123 | * In-use bit automatically set by reading chanctrl |
| 124 | * If 0, we got it, if 1, someone else did |
| 125 | */ |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 126 | chanctrl = readw(ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 127 | if (chanctrl & IOAT_CHANCTRL_CHANNEL_IN_USE) |
| 128 | return -EBUSY; |
| 129 | |
| 130 | /* Setup register to interrupt and write completion status on error */ |
| 131 | chanctrl = IOAT_CHANCTRL_CHANNEL_IN_USE | |
| 132 | IOAT_CHANCTRL_ERR_INT_EN | |
| 133 | IOAT_CHANCTRL_ANY_ERR_ABORT_EN | |
| 134 | IOAT_CHANCTRL_ERR_COMPLETION_EN; |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 135 | writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 136 | |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 137 | chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 138 | if (chanerr) { |
| 139 | printk("IOAT: CHANERR = %x, clearing\n", chanerr); |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 140 | writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* Allocate descriptors */ |
| 144 | for (i = 0; i < INITIAL_IOAT_DESC_COUNT; i++) { |
| 145 | desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL); |
| 146 | if (!desc) { |
| 147 | printk(KERN_ERR "IOAT: Only %d initial descriptors\n", i); |
| 148 | break; |
| 149 | } |
| 150 | list_add_tail(&desc->node, &tmp_list); |
| 151 | } |
| 152 | spin_lock_bh(&ioat_chan->desc_lock); |
| 153 | list_splice(&tmp_list, &ioat_chan->free_desc); |
| 154 | spin_unlock_bh(&ioat_chan->desc_lock); |
| 155 | |
| 156 | /* allocate a completion writeback area */ |
| 157 | /* doing 2 32bit writes to mmio since 1 64b write doesn't work */ |
| 158 | ioat_chan->completion_virt = |
| 159 | pci_pool_alloc(ioat_chan->device->completion_pool, |
| 160 | GFP_KERNEL, |
| 161 | &ioat_chan->completion_addr); |
| 162 | memset(ioat_chan->completion_virt, 0, |
| 163 | sizeof(*ioat_chan->completion_virt)); |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 164 | writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF, |
| 165 | ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW); |
| 166 | writel(((u64) ioat_chan->completion_addr) >> 32, |
| 167 | ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 168 | |
| 169 | ioat_start_null_desc(ioat_chan); |
| 170 | return i; |
| 171 | } |
| 172 | |
| 173 | static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan); |
| 174 | |
| 175 | static void ioat_dma_free_chan_resources(struct dma_chan *chan) |
| 176 | { |
| 177 | struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan); |
| 178 | struct ioat_device *ioat_device = to_ioat_device(chan->device); |
| 179 | struct ioat_desc_sw *desc, *_desc; |
| 180 | u16 chanctrl; |
| 181 | int in_use_descs = 0; |
| 182 | |
| 183 | ioat_dma_memcpy_cleanup(ioat_chan); |
| 184 | |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 185 | writeb(IOAT_CHANCMD_RESET, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 186 | |
| 187 | spin_lock_bh(&ioat_chan->desc_lock); |
| 188 | list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) { |
| 189 | in_use_descs++; |
| 190 | list_del(&desc->node); |
| 191 | pci_pool_free(ioat_device->dma_pool, desc->hw, desc->phys); |
| 192 | kfree(desc); |
| 193 | } |
| 194 | list_for_each_entry_safe(desc, _desc, &ioat_chan->free_desc, node) { |
| 195 | list_del(&desc->node); |
| 196 | pci_pool_free(ioat_device->dma_pool, desc->hw, desc->phys); |
| 197 | kfree(desc); |
| 198 | } |
| 199 | spin_unlock_bh(&ioat_chan->desc_lock); |
| 200 | |
| 201 | pci_pool_free(ioat_device->completion_pool, |
| 202 | ioat_chan->completion_virt, |
| 203 | ioat_chan->completion_addr); |
| 204 | |
| 205 | /* one is ok since we left it on there on purpose */ |
| 206 | if (in_use_descs > 1) |
| 207 | printk(KERN_ERR "IOAT: Freeing %d in use descriptors!\n", |
| 208 | in_use_descs - 1); |
| 209 | |
| 210 | ioat_chan->last_completion = ioat_chan->completion_addr = 0; |
| 211 | |
| 212 | /* Tell hw the chan is free */ |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 213 | chanctrl = readw(ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 214 | chanctrl &= ~IOAT_CHANCTRL_CHANNEL_IN_USE; |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 215 | writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
| 219 | * do_ioat_dma_memcpy - actual function that initiates a IOAT DMA transaction |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 220 | * @ioat_chan: IOAT DMA channel handle |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 221 | * @dest: DMA destination address |
| 222 | * @src: DMA source address |
| 223 | * @len: transaction length in bytes |
| 224 | */ |
| 225 | |
| 226 | static dma_cookie_t do_ioat_dma_memcpy(struct ioat_dma_chan *ioat_chan, |
| 227 | dma_addr_t dest, |
| 228 | dma_addr_t src, |
| 229 | size_t len) |
| 230 | { |
| 231 | struct ioat_desc_sw *first; |
| 232 | struct ioat_desc_sw *prev; |
| 233 | struct ioat_desc_sw *new; |
| 234 | dma_cookie_t cookie; |
| 235 | LIST_HEAD(new_chain); |
| 236 | u32 copy; |
| 237 | size_t orig_len; |
| 238 | dma_addr_t orig_src, orig_dst; |
| 239 | unsigned int desc_count = 0; |
| 240 | unsigned int append = 0; |
| 241 | |
| 242 | if (!ioat_chan || !dest || !src) |
| 243 | return -EFAULT; |
| 244 | |
| 245 | if (!len) |
| 246 | return ioat_chan->common.cookie; |
| 247 | |
| 248 | orig_len = len; |
| 249 | orig_src = src; |
| 250 | orig_dst = dest; |
| 251 | |
| 252 | first = NULL; |
| 253 | prev = NULL; |
| 254 | |
| 255 | spin_lock_bh(&ioat_chan->desc_lock); |
| 256 | |
| 257 | while (len) { |
| 258 | if (!list_empty(&ioat_chan->free_desc)) { |
| 259 | new = to_ioat_desc(ioat_chan->free_desc.next); |
| 260 | list_del(&new->node); |
| 261 | } else { |
| 262 | /* try to get another desc */ |
| 263 | new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC); |
| 264 | /* will this ever happen? */ |
| 265 | /* TODO add upper limit on these */ |
| 266 | BUG_ON(!new); |
| 267 | } |
| 268 | |
| 269 | copy = min((u32) len, ioat_chan->xfercap); |
| 270 | |
| 271 | new->hw->size = copy; |
| 272 | new->hw->ctl = 0; |
| 273 | new->hw->src_addr = src; |
| 274 | new->hw->dst_addr = dest; |
| 275 | new->cookie = 0; |
| 276 | |
| 277 | /* chain together the physical address list for the HW */ |
| 278 | if (!first) |
| 279 | first = new; |
| 280 | else |
| 281 | prev->hw->next = (u64) new->phys; |
| 282 | |
| 283 | prev = new; |
| 284 | |
| 285 | len -= copy; |
| 286 | dest += copy; |
| 287 | src += copy; |
| 288 | |
| 289 | list_add_tail(&new->node, &new_chain); |
| 290 | desc_count++; |
| 291 | } |
| 292 | new->hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS; |
| 293 | new->hw->next = 0; |
| 294 | |
| 295 | /* cookie incr and addition to used_list must be atomic */ |
| 296 | |
| 297 | cookie = ioat_chan->common.cookie; |
| 298 | cookie++; |
| 299 | if (cookie < 0) |
| 300 | cookie = 1; |
| 301 | ioat_chan->common.cookie = new->cookie = cookie; |
| 302 | |
| 303 | pci_unmap_addr_set(new, src, orig_src); |
| 304 | pci_unmap_addr_set(new, dst, orig_dst); |
| 305 | pci_unmap_len_set(new, src_len, orig_len); |
| 306 | pci_unmap_len_set(new, dst_len, orig_len); |
| 307 | |
| 308 | /* write address into NextDescriptor field of last desc in chain */ |
| 309 | to_ioat_desc(ioat_chan->used_desc.prev)->hw->next = first->phys; |
| 310 | list_splice_init(&new_chain, ioat_chan->used_desc.prev); |
| 311 | |
| 312 | ioat_chan->pending += desc_count; |
Chris Leech | 000725d | 2007-03-08 09:57:33 -0800 | [diff] [blame] | 313 | if (ioat_chan->pending >= 4) { |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 314 | append = 1; |
| 315 | ioat_chan->pending = 0; |
| 316 | } |
| 317 | |
| 318 | spin_unlock_bh(&ioat_chan->desc_lock); |
| 319 | |
| 320 | if (append) |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 321 | writeb(IOAT_CHANCMD_APPEND, |
| 322 | ioat_chan->reg_base + IOAT_CHANCMD_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 323 | return cookie; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * ioat_dma_memcpy_buf_to_buf - wrapper that takes src & dest bufs |
| 328 | * @chan: IOAT DMA channel handle |
| 329 | * @dest: DMA destination address |
| 330 | * @src: DMA source address |
| 331 | * @len: transaction length in bytes |
| 332 | */ |
| 333 | |
| 334 | static dma_cookie_t ioat_dma_memcpy_buf_to_buf(struct dma_chan *chan, |
| 335 | void *dest, |
| 336 | void *src, |
| 337 | size_t len) |
| 338 | { |
| 339 | dma_addr_t dest_addr; |
| 340 | dma_addr_t src_addr; |
| 341 | struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan); |
| 342 | |
| 343 | dest_addr = pci_map_single(ioat_chan->device->pdev, |
| 344 | dest, len, PCI_DMA_FROMDEVICE); |
| 345 | src_addr = pci_map_single(ioat_chan->device->pdev, |
| 346 | src, len, PCI_DMA_TODEVICE); |
| 347 | |
| 348 | return do_ioat_dma_memcpy(ioat_chan, dest_addr, src_addr, len); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * ioat_dma_memcpy_buf_to_pg - wrapper, copying from a buf to a page |
| 353 | * @chan: IOAT DMA channel handle |
| 354 | * @page: pointer to the page to copy to |
| 355 | * @offset: offset into that page |
| 356 | * @src: DMA source address |
| 357 | * @len: transaction length in bytes |
| 358 | */ |
| 359 | |
| 360 | static dma_cookie_t ioat_dma_memcpy_buf_to_pg(struct dma_chan *chan, |
| 361 | struct page *page, |
| 362 | unsigned int offset, |
| 363 | void *src, |
| 364 | size_t len) |
| 365 | { |
| 366 | dma_addr_t dest_addr; |
| 367 | dma_addr_t src_addr; |
| 368 | struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan); |
| 369 | |
| 370 | dest_addr = pci_map_page(ioat_chan->device->pdev, |
| 371 | page, offset, len, PCI_DMA_FROMDEVICE); |
| 372 | src_addr = pci_map_single(ioat_chan->device->pdev, |
| 373 | src, len, PCI_DMA_TODEVICE); |
| 374 | |
| 375 | return do_ioat_dma_memcpy(ioat_chan, dest_addr, src_addr, len); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * ioat_dma_memcpy_pg_to_pg - wrapper, copying between two pages |
| 380 | * @chan: IOAT DMA channel handle |
| 381 | * @dest_pg: pointer to the page to copy to |
| 382 | * @dest_off: offset into that page |
| 383 | * @src_pg: pointer to the page to copy from |
| 384 | * @src_off: offset into that page |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 385 | * @len: transaction length in bytes. This is guaranteed not to make a copy |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 386 | * across a page boundary. |
| 387 | */ |
| 388 | |
| 389 | static dma_cookie_t ioat_dma_memcpy_pg_to_pg(struct dma_chan *chan, |
| 390 | struct page *dest_pg, |
| 391 | unsigned int dest_off, |
| 392 | struct page *src_pg, |
| 393 | unsigned int src_off, |
| 394 | size_t len) |
| 395 | { |
| 396 | dma_addr_t dest_addr; |
| 397 | dma_addr_t src_addr; |
| 398 | struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan); |
| 399 | |
| 400 | dest_addr = pci_map_page(ioat_chan->device->pdev, |
| 401 | dest_pg, dest_off, len, PCI_DMA_FROMDEVICE); |
| 402 | src_addr = pci_map_page(ioat_chan->device->pdev, |
| 403 | src_pg, src_off, len, PCI_DMA_TODEVICE); |
| 404 | |
| 405 | return do_ioat_dma_memcpy(ioat_chan, dest_addr, src_addr, len); |
| 406 | } |
| 407 | |
| 408 | /** |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 409 | * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended descriptors to hw |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 410 | * @chan: DMA channel handle |
| 411 | */ |
| 412 | |
| 413 | static void ioat_dma_memcpy_issue_pending(struct dma_chan *chan) |
| 414 | { |
| 415 | struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan); |
| 416 | |
| 417 | if (ioat_chan->pending != 0) { |
| 418 | ioat_chan->pending = 0; |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 419 | writeb(IOAT_CHANCMD_APPEND, |
| 420 | ioat_chan->reg_base + IOAT_CHANCMD_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *chan) |
| 425 | { |
| 426 | unsigned long phys_complete; |
| 427 | struct ioat_desc_sw *desc, *_desc; |
| 428 | dma_cookie_t cookie = 0; |
| 429 | |
| 430 | prefetch(chan->completion_virt); |
| 431 | |
| 432 | if (!spin_trylock(&chan->cleanup_lock)) |
| 433 | return; |
| 434 | |
| 435 | /* The completion writeback can happen at any time, |
| 436 | so reads by the driver need to be atomic operations |
| 437 | The descriptor physical addresses are limited to 32-bits |
| 438 | when the CPU can only do a 32-bit mov */ |
| 439 | |
| 440 | #if (BITS_PER_LONG == 64) |
| 441 | phys_complete = |
| 442 | chan->completion_virt->full & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR; |
| 443 | #else |
| 444 | phys_complete = chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK; |
| 445 | #endif |
| 446 | |
| 447 | if ((chan->completion_virt->full & IOAT_CHANSTS_DMA_TRANSFER_STATUS) == |
| 448 | IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) { |
| 449 | printk("IOAT: Channel halted, chanerr = %x\n", |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 450 | readl(chan->reg_base + IOAT_CHANERR_OFFSET)); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 451 | |
| 452 | /* TODO do something to salvage the situation */ |
| 453 | } |
| 454 | |
| 455 | if (phys_complete == chan->last_completion) { |
| 456 | spin_unlock(&chan->cleanup_lock); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | spin_lock_bh(&chan->desc_lock); |
| 461 | list_for_each_entry_safe(desc, _desc, &chan->used_desc, node) { |
| 462 | |
| 463 | /* |
| 464 | * Incoming DMA requests may use multiple descriptors, due to |
| 465 | * exceeding xfercap, perhaps. If so, only the last one will |
| 466 | * have a cookie, and require unmapping. |
| 467 | */ |
| 468 | if (desc->cookie) { |
| 469 | cookie = desc->cookie; |
| 470 | |
| 471 | /* yes we are unmapping both _page and _single alloc'd |
| 472 | regions with unmap_page. Is this *really* that bad? |
| 473 | */ |
| 474 | pci_unmap_page(chan->device->pdev, |
| 475 | pci_unmap_addr(desc, dst), |
| 476 | pci_unmap_len(desc, dst_len), |
| 477 | PCI_DMA_FROMDEVICE); |
| 478 | pci_unmap_page(chan->device->pdev, |
| 479 | pci_unmap_addr(desc, src), |
| 480 | pci_unmap_len(desc, src_len), |
| 481 | PCI_DMA_TODEVICE); |
| 482 | } |
| 483 | |
| 484 | if (desc->phys != phys_complete) { |
| 485 | /* a completed entry, but not the last, so cleanup */ |
| 486 | list_del(&desc->node); |
| 487 | list_add_tail(&desc->node, &chan->free_desc); |
| 488 | } else { |
| 489 | /* last used desc. Do not remove, so we can append from |
| 490 | it, but don't look at it next time, either */ |
| 491 | desc->cookie = 0; |
| 492 | |
| 493 | /* TODO check status bits? */ |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | spin_unlock_bh(&chan->desc_lock); |
| 499 | |
| 500 | chan->last_completion = phys_complete; |
| 501 | if (cookie != 0) |
| 502 | chan->completed_cookie = cookie; |
| 503 | |
| 504 | spin_unlock(&chan->cleanup_lock); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * ioat_dma_is_complete - poll the status of a IOAT DMA transaction |
| 509 | * @chan: IOAT DMA channel handle |
| 510 | * @cookie: DMA transaction identifier |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 511 | * @done: if not %NULL, updated with last completed transaction |
| 512 | * @used: if not %NULL, updated with last used transaction |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 513 | */ |
| 514 | |
| 515 | static enum dma_status ioat_dma_is_complete(struct dma_chan *chan, |
| 516 | dma_cookie_t cookie, |
| 517 | dma_cookie_t *done, |
| 518 | dma_cookie_t *used) |
| 519 | { |
| 520 | struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan); |
| 521 | dma_cookie_t last_used; |
| 522 | dma_cookie_t last_complete; |
| 523 | enum dma_status ret; |
| 524 | |
| 525 | last_used = chan->cookie; |
| 526 | last_complete = ioat_chan->completed_cookie; |
| 527 | |
| 528 | if (done) |
| 529 | *done= last_complete; |
| 530 | if (used) |
| 531 | *used = last_used; |
| 532 | |
| 533 | ret = dma_async_is_complete(cookie, last_complete, last_used); |
| 534 | if (ret == DMA_SUCCESS) |
| 535 | return ret; |
| 536 | |
| 537 | ioat_dma_memcpy_cleanup(ioat_chan); |
| 538 | |
| 539 | last_used = chan->cookie; |
| 540 | last_complete = ioat_chan->completed_cookie; |
| 541 | |
| 542 | if (done) |
| 543 | *done= last_complete; |
| 544 | if (used) |
| 545 | *used = last_used; |
| 546 | |
| 547 | return dma_async_is_complete(cookie, last_complete, last_used); |
| 548 | } |
| 549 | |
| 550 | /* PCI API */ |
| 551 | |
| 552 | static struct pci_device_id ioat_pci_tbl[] = { |
| 553 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT) }, |
| 554 | { 0, } |
| 555 | }; |
| 556 | |
Randy Dunlap | 92504f7 | 2007-06-27 14:09:56 -0700 | [diff] [blame] | 557 | static struct pci_driver ioat_pci_driver = { |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 558 | .name = "ioatdma", |
| 559 | .id_table = ioat_pci_tbl, |
| 560 | .probe = ioat_probe, |
Dan Aloni | 428ed60 | 2007-03-08 09:57:36 -0800 | [diff] [blame^] | 561 | .shutdown = ioat_shutdown, |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 562 | .remove = __devexit_p(ioat_remove), |
| 563 | }; |
| 564 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 565 | static irqreturn_t ioat_do_interrupt(int irq, void *data) |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 566 | { |
| 567 | struct ioat_device *instance = data; |
| 568 | unsigned long attnstatus; |
| 569 | u8 intrctrl; |
| 570 | |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 571 | intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 572 | |
| 573 | if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN)) |
| 574 | return IRQ_NONE; |
| 575 | |
| 576 | if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) { |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 577 | writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 578 | return IRQ_NONE; |
| 579 | } |
| 580 | |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 581 | attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 582 | |
| 583 | printk(KERN_ERR "ioatdma error: interrupt! status %lx\n", attnstatus); |
| 584 | |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 585 | writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 586 | return IRQ_HANDLED; |
| 587 | } |
| 588 | |
| 589 | static void ioat_start_null_desc(struct ioat_dma_chan *ioat_chan) |
| 590 | { |
| 591 | struct ioat_desc_sw *desc; |
| 592 | |
| 593 | spin_lock_bh(&ioat_chan->desc_lock); |
| 594 | |
| 595 | if (!list_empty(&ioat_chan->free_desc)) { |
| 596 | desc = to_ioat_desc(ioat_chan->free_desc.next); |
| 597 | list_del(&desc->node); |
| 598 | } else { |
| 599 | /* try to get another desc */ |
| 600 | spin_unlock_bh(&ioat_chan->desc_lock); |
| 601 | desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL); |
| 602 | spin_lock_bh(&ioat_chan->desc_lock); |
| 603 | /* will this ever happen? */ |
| 604 | BUG_ON(!desc); |
| 605 | } |
| 606 | |
| 607 | desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL; |
| 608 | desc->hw->next = 0; |
| 609 | |
| 610 | list_add_tail(&desc->node, &ioat_chan->used_desc); |
| 611 | spin_unlock_bh(&ioat_chan->desc_lock); |
| 612 | |
Chris Leech | 70774b4 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 613 | writel(((u64) desc->phys) & 0x00000000FFFFFFFF, |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 614 | ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_LOW); |
Chris Leech | 70774b4 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 615 | writel(((u64) desc->phys) >> 32, |
| 616 | ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_HIGH); |
| 617 | |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 618 | writeb(IOAT_CHANCMD_START, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* |
| 622 | * Perform a IOAT transaction to verify the HW works. |
| 623 | */ |
| 624 | #define IOAT_TEST_SIZE 2000 |
| 625 | |
| 626 | static int ioat_self_test(struct ioat_device *device) |
| 627 | { |
| 628 | int i; |
| 629 | u8 *src; |
| 630 | u8 *dest; |
| 631 | struct dma_chan *dma_chan; |
| 632 | dma_cookie_t cookie; |
| 633 | int err = 0; |
| 634 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 635 | src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 636 | if (!src) |
| 637 | return -ENOMEM; |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 638 | dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 639 | if (!dest) { |
| 640 | kfree(src); |
| 641 | return -ENOMEM; |
| 642 | } |
| 643 | |
| 644 | /* Fill in src buffer */ |
| 645 | for (i = 0; i < IOAT_TEST_SIZE; i++) |
| 646 | src[i] = (u8)i; |
| 647 | |
| 648 | /* Start copy, using first DMA channel */ |
| 649 | dma_chan = container_of(device->common.channels.next, |
| 650 | struct dma_chan, |
| 651 | device_node); |
| 652 | if (ioat_dma_alloc_chan_resources(dma_chan) < 1) { |
| 653 | err = -ENODEV; |
| 654 | goto out; |
| 655 | } |
| 656 | |
| 657 | cookie = ioat_dma_memcpy_buf_to_buf(dma_chan, dest, src, IOAT_TEST_SIZE); |
| 658 | ioat_dma_memcpy_issue_pending(dma_chan); |
| 659 | msleep(1); |
| 660 | |
| 661 | if (ioat_dma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) { |
| 662 | printk(KERN_ERR "ioatdma: Self-test copy timed out, disabling\n"); |
| 663 | err = -ENODEV; |
| 664 | goto free_resources; |
| 665 | } |
| 666 | if (memcmp(src, dest, IOAT_TEST_SIZE)) { |
| 667 | printk(KERN_ERR "ioatdma: Self-test copy failed compare, disabling\n"); |
| 668 | err = -ENODEV; |
| 669 | goto free_resources; |
| 670 | } |
| 671 | |
| 672 | free_resources: |
| 673 | ioat_dma_free_chan_resources(dma_chan); |
| 674 | out: |
| 675 | kfree(src); |
| 676 | kfree(dest); |
| 677 | return err; |
| 678 | } |
| 679 | |
| 680 | static int __devinit ioat_probe(struct pci_dev *pdev, |
| 681 | const struct pci_device_id *ent) |
| 682 | { |
| 683 | int err; |
| 684 | unsigned long mmio_start, mmio_len; |
Al Viro | 47b1653 | 2006-10-10 22:45:47 +0100 | [diff] [blame] | 685 | void __iomem *reg_base; |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 686 | struct ioat_device *device; |
| 687 | |
| 688 | err = pci_enable_device(pdev); |
| 689 | if (err) |
| 690 | goto err_enable_device; |
| 691 | |
| 692 | err = pci_set_dma_mask(pdev, DMA_64BIT_MASK); |
| 693 | if (err) |
| 694 | err = pci_set_dma_mask(pdev, DMA_32BIT_MASK); |
| 695 | if (err) |
| 696 | goto err_set_dma_mask; |
| 697 | |
Randy Dunlap | 92504f7 | 2007-06-27 14:09:56 -0700 | [diff] [blame] | 698 | err = pci_request_regions(pdev, ioat_pci_driver.name); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 699 | if (err) |
| 700 | goto err_request_regions; |
| 701 | |
| 702 | mmio_start = pci_resource_start(pdev, 0); |
| 703 | mmio_len = pci_resource_len(pdev, 0); |
| 704 | |
| 705 | reg_base = ioremap(mmio_start, mmio_len); |
| 706 | if (!reg_base) { |
| 707 | err = -ENOMEM; |
| 708 | goto err_ioremap; |
| 709 | } |
| 710 | |
| 711 | device = kzalloc(sizeof(*device), GFP_KERNEL); |
| 712 | if (!device) { |
| 713 | err = -ENOMEM; |
| 714 | goto err_kzalloc; |
| 715 | } |
| 716 | |
| 717 | /* DMA coherent memory pool for DMA descriptor allocations */ |
| 718 | device->dma_pool = pci_pool_create("dma_desc_pool", pdev, |
| 719 | sizeof(struct ioat_dma_descriptor), 64, 0); |
| 720 | if (!device->dma_pool) { |
| 721 | err = -ENOMEM; |
| 722 | goto err_dma_pool; |
| 723 | } |
| 724 | |
| 725 | device->completion_pool = pci_pool_create("completion_pool", pdev, sizeof(u64), SMP_CACHE_BYTES, SMP_CACHE_BYTES); |
| 726 | if (!device->completion_pool) { |
| 727 | err = -ENOMEM; |
| 728 | goto err_completion_pool; |
| 729 | } |
| 730 | |
| 731 | device->pdev = pdev; |
| 732 | pci_set_drvdata(pdev, device); |
| 733 | #ifdef CONFIG_PCI_MSI |
| 734 | if (pci_enable_msi(pdev) == 0) { |
| 735 | device->msi = 1; |
| 736 | } else { |
| 737 | device->msi = 0; |
| 738 | } |
| 739 | #endif |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 740 | err = request_irq(pdev->irq, &ioat_do_interrupt, IRQF_SHARED, "ioat", |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 741 | device); |
| 742 | if (err) |
| 743 | goto err_irq; |
| 744 | |
| 745 | device->reg_base = reg_base; |
| 746 | |
Chris Leech | e382881 | 2007-03-08 09:57:35 -0800 | [diff] [blame] | 747 | writeb(IOAT_INTRCTRL_MASTER_INT_EN, device->reg_base + IOAT_INTRCTRL_OFFSET); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 748 | pci_set_master(pdev); |
| 749 | |
| 750 | INIT_LIST_HEAD(&device->common.channels); |
| 751 | enumerate_dma_channels(device); |
| 752 | |
| 753 | device->common.device_alloc_chan_resources = ioat_dma_alloc_chan_resources; |
| 754 | device->common.device_free_chan_resources = ioat_dma_free_chan_resources; |
| 755 | device->common.device_memcpy_buf_to_buf = ioat_dma_memcpy_buf_to_buf; |
| 756 | device->common.device_memcpy_buf_to_pg = ioat_dma_memcpy_buf_to_pg; |
| 757 | device->common.device_memcpy_pg_to_pg = ioat_dma_memcpy_pg_to_pg; |
| 758 | device->common.device_memcpy_complete = ioat_dma_is_complete; |
| 759 | device->common.device_memcpy_issue_pending = ioat_dma_memcpy_issue_pending; |
| 760 | printk(KERN_INFO "Intel(R) I/OAT DMA Engine found, %d channels\n", |
| 761 | device->common.chancnt); |
| 762 | |
| 763 | err = ioat_self_test(device); |
| 764 | if (err) |
| 765 | goto err_self_test; |
| 766 | |
| 767 | dma_async_device_register(&device->common); |
| 768 | |
| 769 | return 0; |
| 770 | |
| 771 | err_self_test: |
| 772 | err_irq: |
| 773 | pci_pool_destroy(device->completion_pool); |
| 774 | err_completion_pool: |
| 775 | pci_pool_destroy(device->dma_pool); |
| 776 | err_dma_pool: |
| 777 | kfree(device); |
| 778 | err_kzalloc: |
| 779 | iounmap(reg_base); |
| 780 | err_ioremap: |
| 781 | pci_release_regions(pdev); |
| 782 | err_request_regions: |
| 783 | err_set_dma_mask: |
| 784 | pci_disable_device(pdev); |
| 785 | err_enable_device: |
Dan Aloni | 428ed60 | 2007-03-08 09:57:36 -0800 | [diff] [blame^] | 786 | |
| 787 | printk(KERN_ERR "Intel(R) I/OAT DMA Engine initialization failed\n"); |
| 788 | |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 789 | return err; |
| 790 | } |
| 791 | |
Dan Aloni | 428ed60 | 2007-03-08 09:57:36 -0800 | [diff] [blame^] | 792 | static void ioat_shutdown(struct pci_dev *pdev) |
| 793 | { |
| 794 | struct ioat_device *device; |
| 795 | device = pci_get_drvdata(pdev); |
| 796 | |
| 797 | dma_async_device_unregister(&device->common); |
| 798 | } |
| 799 | |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 800 | static void __devexit ioat_remove(struct pci_dev *pdev) |
| 801 | { |
| 802 | struct ioat_device *device; |
| 803 | struct dma_chan *chan, *_chan; |
| 804 | struct ioat_dma_chan *ioat_chan; |
| 805 | |
| 806 | device = pci_get_drvdata(pdev); |
| 807 | dma_async_device_unregister(&device->common); |
| 808 | |
| 809 | free_irq(device->pdev->irq, device); |
| 810 | #ifdef CONFIG_PCI_MSI |
| 811 | if (device->msi) |
| 812 | pci_disable_msi(device->pdev); |
| 813 | #endif |
| 814 | pci_pool_destroy(device->dma_pool); |
| 815 | pci_pool_destroy(device->completion_pool); |
| 816 | iounmap(device->reg_base); |
| 817 | pci_release_regions(pdev); |
| 818 | pci_disable_device(pdev); |
| 819 | list_for_each_entry_safe(chan, _chan, &device->common.channels, device_node) { |
| 820 | ioat_chan = to_ioat_chan(chan); |
| 821 | list_del(&chan->device_node); |
| 822 | kfree(ioat_chan); |
| 823 | } |
| 824 | kfree(device); |
| 825 | } |
| 826 | |
| 827 | /* MODULE API */ |
Chris Leech | 000725d | 2007-03-08 09:57:33 -0800 | [diff] [blame] | 828 | MODULE_VERSION("1.9"); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 829 | MODULE_LICENSE("GPL"); |
| 830 | MODULE_AUTHOR("Intel Corporation"); |
| 831 | |
| 832 | static int __init ioat_init_module(void) |
| 833 | { |
| 834 | /* it's currently unsafe to unload this module */ |
| 835 | /* if forced, worst case is that rmmod hangs */ |
David S. Miller | 8070b2b | 2006-06-26 00:10:46 -0700 | [diff] [blame] | 836 | __unsafe(THIS_MODULE); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 837 | |
Randy Dunlap | 92504f7 | 2007-06-27 14:09:56 -0700 | [diff] [blame] | 838 | return pci_register_driver(&ioat_pci_driver); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | module_init(ioat_init_module); |
| 842 | |
| 843 | static void __exit ioat_exit_module(void) |
| 844 | { |
Randy Dunlap | 92504f7 | 2007-06-27 14:09:56 -0700 | [diff] [blame] | 845 | pci_unregister_driver(&ioat_pci_driver); |
Chris Leech | 0bbd5f4 | 2006-05-23 17:35:34 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | module_exit(ioat_exit_module); |