Scatter/gether's knowledge point struct scatterlist

Source: Internet
Author: User
struct Scatterlist *sglist {
unsigned long page_link;//page position, approximately the location of the virtual Address page
unsigned int offset;//offset
unsigned int length;//length
dma_addr_t DMA_ADDRESS;//DMA address, should be the bus address
unsigned int dma_length;
}

We have a few scattered memory content to be routed and need to call
Int Dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, enum_dma_direction direction);
Nents is the number of incoming scatter tables. The return value is the number of DMA buffers to send, which may be less than nents. (that is, some elements in the scatter table may be adjacent to the physical address and will be mapped into a block of memory) if returning 0 indicates failure

int I, count;
struct Scatterlist *sg;

Count = Dma_map_sg (Dev, sglist, nents, direction);

For_each_sg(Sglist, SG, COUNT, i) {
Hw_address[i] = sg_dma_address (SG);
Hw_len[i] = Sg_dma_len (SG);
}

Sg_init_table (I2c->sg_io, 2);
Sg_set_buf (&i2c->sg_io, &i2c->addr_data, 1);
Sg_set_buf (&i2c->sg_io, Msg->buf, Msg->len);

Dma_map_sg (I2c->dev, I2c->sg_io, 2, Dma_to_device);


Sg_init_one (data.sg, Idata->buf, idata->buf_bytes);

void *dma_alloc_coherent (struct device *dev, size_t size, dma_addr_t *handle, gfp_t GFP);

The return value of this function is the virtual address of the DMA buffer to which the request is applied. In addition, the function returns the bus address of the DMA buffer via parameter handle. The corresponding release function is:

void dma_free_coherent (struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle);

dma_addr_t dma_map_single (struct device *dev, void *buffer, size_t size, enum dma_data_direction direction);  If the mapping succeeds, the bus address is returned, or null is returned. The last parameter DMA direction may fetch Dma_to_device, Dma_form_device, dma_bidirectional and Dma_none;



The Linux kernel also provides a function pci_alloc_consistent () for the PCI device to request a DMA buffer, and the prototype is:

void *pci_alloc_consistent (struct Pci_dev *dev, size_t size, dma_addr_t *DMA_ADDRP);  The corresponding release function is:
void Pci_free_consistent (struct Pci_dev *pdev, size_t size, void *cpu_addr, dma_addr_t dma_addr);.

By the way, the physical address of the bus address and physical address
1 is related to the CPU. The physical address is generated on the CPU's address signal line. After the virtual address in the program instruction passes the segment mapping and the page map, the physical address is generated, and the physical address is placed on the CPU's address line. (see from CPU side)
2 bus address, as the name suggests, is related to the bus, is the bus address line or in the address cycle of the signal generated. The peripheral uses a bus address. (see from Device side)
3 The relationship between the physical address and the bus address is determined by the system design. On the x86 platform, the physical address is the same as the PCI bus address. On other platforms, there may be some kind of conversion, usually a linear one.

For example: The CPU needs to access the physical address is 0xfa000 unit, then on the x86 platform, will produce a PCI bus on the 0xfa000 address access. This unit is either in memory, or it is a storage unit on a card, and may not even have a corresponding memory on this address. On another platform, the access that may be generated on the PCI bus is for the address-0x1fa000 unit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.