📄 dmabounce.c
字号:
if (device_info) buf = find_safe_buffer(device_info, dma_addr); if (buf) { /* * Both of these checks from original code need to be * commented out b/c some drivers rely on the following: * * 1) Drivers may map a large chunk of memory into DMA space * but only sync a small portion of it. Good example is * allocating a large buffer, mapping it, and then * breaking it up into small descriptors. No point * in syncing the whole buffer if you only have to * touch one descriptor. * * 2) Buffers that are mapped as DMA_BIDIRECTIONAL are * usually only synced in one dir at a time. * * See drivers/net/eepro100.c for examples of both cases. * * -ds * * BUG_ON(buf->size != size); * BUG_ON(buf->direction != dir); */ dev_dbg(dev, "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n", __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr), buf->safe, (void *) buf->safe_dma_addr); DO_STATS ( device_info->bounce_count++ ); switch (dir) { case DMA_FROM_DEVICE: dev_dbg(dev, "%s: copy back safe %p to unsafe %p size %d\n", __func__, buf->safe, buf->ptr, size); memcpy(buf->ptr, buf->safe, size); break; case DMA_TO_DEVICE: dev_dbg(dev, "%s: copy out unsafe %p to safe %p, size %d\n", __func__,buf->ptr, buf->safe, size); memcpy(buf->safe, buf->ptr, size); break; case DMA_BIDIRECTIONAL: BUG(); /* is this allowed? what does it mean? */ default: BUG(); } /* * No need to sync the safe buffer - it was allocated * via the coherent allocators. */ } else { dma_cache_maint(dma_to_virt(dev, dma_addr), size, dir); }}/* ************************************************** *//* * see if a buffer address is in an 'unsafe' range. if it is * allocate a 'safe' buffer and copy the unsafe buffer into it. * substitute the safe buffer for the unsafe one. * (basically move the buffer from an unsafe area to a safe one) */dma_addr_tdma_map_single(struct device *dev, void *ptr, size_t size, enum dma_data_direction dir){ dma_addr_t dma_addr; dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n", __func__, ptr, size, dir); BUG_ON(dir == DMA_NONE); dma_addr = map_single(dev, ptr, size, dir); return dma_addr;}/* * see if a mapped address was really a "safe" buffer and if so, copy * the data from the safe buffer back to the unsafe buffer and free up * the safe buffer. (basically return things back to the way they * should be) */voiddma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir){ dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n", __func__, (void *) dma_addr, size, dir); BUG_ON(dir == DMA_NONE); unmap_single(dev, dma_addr, size, dir);}intdma_map_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir){ int i; dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n", __func__, sg, nents, dir); BUG_ON(dir == DMA_NONE); for (i = 0; i < nents; i++, sg++) { struct page *page = sg_page(sg); unsigned int offset = sg->offset; unsigned int length = sg->length; void *ptr = page_address(page) + offset; sg->dma_address = map_single(dev, ptr, length, dir); } return nents;}voiddma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir){ int i; dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n", __func__, sg, nents, dir); BUG_ON(dir == DMA_NONE); for (i = 0; i < nents; i++, sg++) { dma_addr_t dma_addr = sg->dma_address; unsigned int length = sg->length; unmap_single(dev, dma_addr, length, dir); }}voiddma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir){ dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n", __func__, (void *) dma_addr, size, dir); sync_single(dev, dma_addr, size, dir);}voiddma_sync_single_for_device(struct device *dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir){ dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n", __func__, (void *) dma_addr, size, dir); sync_single(dev, dma_addr, size, dir);}voiddma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir){ int i; dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n", __func__, sg, nents, dir); BUG_ON(dir == DMA_NONE); for (i = 0; i < nents; i++, sg++) { dma_addr_t dma_addr = sg->dma_address; unsigned int length = sg->length; sync_single(dev, dma_addr, length, dir); }}voiddma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir){ int i; dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n", __func__, sg, nents, dir); BUG_ON(dir == DMA_NONE); for (i = 0; i < nents; i++, sg++) { dma_addr_t dma_addr = sg->dma_address; unsigned int length = sg->length; sync_single(dev, dma_addr, length, dir); }}static intdmabounce_init_pool(struct dmabounce_pool *pool, struct device *dev, const char *name, unsigned long size){ pool->size = size; DO_STATS(pool->allocs = 0); pool->pool = dma_pool_create(name, dev, size, 0 /* byte alignment */, 0 /* no page-crossing issues */); return pool->pool ? 0 : -ENOMEM;}intdmabounce_register_dev(struct device *dev, unsigned long small_buffer_size, unsigned long large_buffer_size){ struct dmabounce_device_info *device_info; int ret; device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC); if (!device_info) { printk(KERN_ERR "Could not allocated dmabounce_device_info for %s", dev->bus_id); return -ENOMEM; } ret = dmabounce_init_pool(&device_info->small, dev, "small_dmabounce_pool", small_buffer_size); if (ret) { dev_err(dev, "dmabounce: could not allocate DMA pool for %ld byte objects\n", small_buffer_size); goto err_free; } if (large_buffer_size) { ret = dmabounce_init_pool(&device_info->large, dev, "large_dmabounce_pool", large_buffer_size); if (ret) { dev_err(dev, "dmabounce: could not allocate DMA pool for %ld byte objects\n", large_buffer_size); goto err_destroy; } } device_info->dev = dev; INIT_LIST_HEAD(&device_info->safe_buffers); rwlock_init(&device_info->lock);#ifdef STATS device_info->total_allocs = 0; device_info->map_op_count = 0; device_info->bounce_count = 0; device_info->attr_res = device_create_file(dev, &dev_attr_dmabounce_stats);#endif dev->archdata.dmabounce = device_info; printk(KERN_INFO "dmabounce: registered device %s on %s bus\n", dev->bus_id, dev->bus->name); return 0; err_destroy: dma_pool_destroy(device_info->small.pool); err_free: kfree(device_info); return ret;}voiddmabounce_unregister_dev(struct device *dev){ struct dmabounce_device_info *device_info = dev->archdata.dmabounce; dev->archdata.dmabounce = NULL; if (!device_info) { printk(KERN_WARNING "%s: Never registered with dmabounce but attempting" \ "to unregister!\n", dev->bus_id); return; } if (!list_empty(&device_info->safe_buffers)) { printk(KERN_ERR "%s: Removing from dmabounce with pending buffers!\n", dev->bus_id); BUG(); } if (device_info->small.pool) dma_pool_destroy(device_info->small.pool); if (device_info->large.pool) dma_pool_destroy(device_info->large.pool);#ifdef STATS if (device_info->attr_res == 0) device_remove_file(dev, &dev_attr_dmabounce_stats);#endif kfree(device_info); printk(KERN_INFO "dmabounce: device %s on %s bus unregistered\n", dev->bus_id, dev->bus->name);}EXPORT_SYMBOL(dma_map_single);EXPORT_SYMBOL(dma_unmap_single);EXPORT_SYMBOL(dma_map_sg);EXPORT_SYMBOL(dma_unmap_sg);EXPORT_SYMBOL(dma_sync_single_for_cpu);EXPORT_SYMBOL(dma_sync_single_for_device);EXPORT_SYMBOL(dma_sync_sg);EXPORT_SYMBOL(dmabounce_register_dev);EXPORT_SYMBOL(dmabounce_unregister_dev);MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>");MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows");MODULE_LICENSE("GPL");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -