dmaengine.c
来自「linux 内核源代码」· C语言 代码 · 共 607 行 · 第 1/2 页
C
607 行
if (!client) return; mutex_lock(&dma_list_mutex); /* free all channels the client is holding */ list_for_each_entry(device, &dma_device_list, global_node) list_for_each_entry(chan, &device->channels, device_node) { ack = client->event_callback(client, chan, DMA_RESOURCE_REMOVED); if (ack == DMA_ACK) dma_chan_put(chan); } list_del(&client->global_node); mutex_unlock(&dma_list_mutex);}EXPORT_SYMBOL(dma_async_client_unregister);/** * dma_async_client_chan_request - send all available channels to the * client that satisfy the capability mask * @client - requester */void dma_async_client_chan_request(struct dma_client *client){ mutex_lock(&dma_list_mutex); dma_client_chan_alloc(client); mutex_unlock(&dma_list_mutex);}EXPORT_SYMBOL(dma_async_client_chan_request);/** * dma_async_device_register - registers DMA devices found * @device: &dma_device */int dma_async_device_register(struct dma_device *device){ static int id; int chancnt = 0, rc; struct dma_chan* chan; if (!device) return -ENODEV; /* validate device routines */ BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) && !device->device_prep_dma_memcpy); BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) && !device->device_prep_dma_xor); BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) && !device->device_prep_dma_zero_sum); BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) && !device->device_prep_dma_memset); BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) && !device->device_prep_dma_interrupt); BUG_ON(!device->device_alloc_chan_resources); BUG_ON(!device->device_free_chan_resources); BUG_ON(!device->device_dependency_added); BUG_ON(!device->device_is_tx_complete); BUG_ON(!device->device_issue_pending); BUG_ON(!device->dev); init_completion(&device->done); kref_init(&device->refcount); device->dev_id = id++; /* represent channels in sysfs. Probably want devs too */ list_for_each_entry(chan, &device->channels, device_node) { chan->local = alloc_percpu(typeof(*chan->local)); if (chan->local == NULL) continue; chan->chan_id = chancnt++; chan->class_dev.class = &dma_devclass; chan->class_dev.dev = NULL; snprintf(chan->class_dev.class_id, BUS_ID_SIZE, "dma%dchan%d", device->dev_id, chan->chan_id); rc = class_device_register(&chan->class_dev); if (rc) { chancnt--; free_percpu(chan->local); chan->local = NULL; goto err_out; } /* One for the channel, one of the class device */ kref_get(&device->refcount); kref_get(&device->refcount); kref_init(&chan->refcount); chan->slow_ref = 0; INIT_RCU_HEAD(&chan->rcu); } mutex_lock(&dma_list_mutex); list_add_tail(&device->global_node, &dma_device_list); mutex_unlock(&dma_list_mutex); dma_clients_notify_available(); return 0;err_out: list_for_each_entry(chan, &device->channels, device_node) { if (chan->local == NULL) continue; kref_put(&device->refcount, dma_async_device_cleanup); class_device_unregister(&chan->class_dev); chancnt--; free_percpu(chan->local); } return rc;}EXPORT_SYMBOL(dma_async_device_register);/** * dma_async_device_cleanup - function called when all references are released * @kref: kernel reference object */static void dma_async_device_cleanup(struct kref *kref){ struct dma_device *device; device = container_of(kref, struct dma_device, refcount); complete(&device->done);}/** * dma_async_device_unregister - unregisters DMA devices * @device: &dma_device */void dma_async_device_unregister(struct dma_device *device){ struct dma_chan *chan; mutex_lock(&dma_list_mutex); list_del(&device->global_node); mutex_unlock(&dma_list_mutex); list_for_each_entry(chan, &device->channels, device_node) { dma_clients_notify_removed(chan); class_device_unregister(&chan->class_dev); dma_chan_release(chan); } kref_put(&device->refcount, dma_async_device_cleanup); wait_for_completion(&device->done);}EXPORT_SYMBOL(dma_async_device_unregister);/** * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses * @chan: DMA channel to offload copy to * @dest: destination address (virtual) * @src: source address (virtual) * @len: length * * Both @dest and @src must be mappable to a bus address according to the * DMA mapping API rules for streaming mappings. * Both @dest and @src must stay memory resident (kernel memory or locked * user space pages). */dma_cookie_tdma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest, void *src, size_t len){ struct dma_device *dev = chan->device; struct dma_async_tx_descriptor *tx; dma_addr_t addr; dma_cookie_t cookie; int cpu; tx = dev->device_prep_dma_memcpy(chan, len, 0); if (!tx) return -ENOMEM; tx->ack = 1; tx->callback = NULL; addr = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE); tx->tx_set_src(addr, tx, 0); addr = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE); tx->tx_set_dest(addr, tx, 0); cookie = tx->tx_submit(tx); cpu = get_cpu(); per_cpu_ptr(chan->local, cpu)->bytes_transferred += len; per_cpu_ptr(chan->local, cpu)->memcpy_count++; put_cpu(); return cookie;}EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);/** * dma_async_memcpy_buf_to_pg - offloaded copy from address to page * @chan: DMA channel to offload copy to * @page: destination page * @offset: offset in page to copy to * @kdata: source address (virtual) * @len: length * * Both @page/@offset and @kdata must be mappable to a bus address according * to the DMA mapping API rules for streaming mappings. * Both @page/@offset and @kdata must stay memory resident (kernel memory or * locked user space pages) */dma_cookie_tdma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page, unsigned int offset, void *kdata, size_t len){ struct dma_device *dev = chan->device; struct dma_async_tx_descriptor *tx; dma_addr_t addr; dma_cookie_t cookie; int cpu; tx = dev->device_prep_dma_memcpy(chan, len, 0); if (!tx) return -ENOMEM; tx->ack = 1; tx->callback = NULL; addr = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE); tx->tx_set_src(addr, tx, 0); addr = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE); tx->tx_set_dest(addr, tx, 0); cookie = tx->tx_submit(tx); cpu = get_cpu(); per_cpu_ptr(chan->local, cpu)->bytes_transferred += len; per_cpu_ptr(chan->local, cpu)->memcpy_count++; put_cpu(); return cookie;}EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);/** * dma_async_memcpy_pg_to_pg - offloaded copy from page to page * @chan: DMA channel to offload copy to * @dest_pg: destination page * @dest_off: offset in page to copy to * @src_pg: source page * @src_off: offset in page to copy from * @len: length * * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus * address according to the DMA mapping API rules for streaming mappings. * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident * (kernel memory or locked user space pages). */dma_cookie_tdma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg, unsigned int dest_off, struct page *src_pg, unsigned int src_off, size_t len){ struct dma_device *dev = chan->device; struct dma_async_tx_descriptor *tx; dma_addr_t addr; dma_cookie_t cookie; int cpu; tx = dev->device_prep_dma_memcpy(chan, len, 0); if (!tx) return -ENOMEM; tx->ack = 1; tx->callback = NULL; addr = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE); tx->tx_set_src(addr, tx, 0); addr = dma_map_page(dev->dev, dest_pg, dest_off, len, DMA_FROM_DEVICE); tx->tx_set_dest(addr, tx, 0); cookie = tx->tx_submit(tx); cpu = get_cpu(); per_cpu_ptr(chan->local, cpu)->bytes_transferred += len; per_cpu_ptr(chan->local, cpu)->memcpy_count++; put_cpu(); return cookie;}EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx, struct dma_chan *chan){ tx->chan = chan; spin_lock_init(&tx->lock); INIT_LIST_HEAD(&tx->depend_node); INIT_LIST_HEAD(&tx->depend_list);}EXPORT_SYMBOL(dma_async_tx_descriptor_init);static int __init dma_bus_init(void){ mutex_init(&dma_list_mutex); return class_register(&dma_devclass);}subsys_initcall(dma_bus_init);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?