Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/dma: Use generic swiotlb_ops

The generic swiotlb DMA ops were based on the x86 ones and provide
equivalent functionality, so use them.

Also fix the sta2x11 case. For that SOC the DMA map ops need an
additional physical to DMA address translations. For swiotlb buffers
that is done throught the phys_to_dma helper, but the sta2x11_dma_ops
also added an additional translation on the return value from
x86_swiotlb_alloc_coherent, which is only correct if that functions
returns a direct allocation and not a swiotlb buffer. With the
generic swiotlb and DMA-direct code phys_to_dma is not always used
and the separate sta2x11_dma_ops can be replaced with a simple
bit that marks if the additional physical to DMA address translation
is needed.

Tested-by: Tom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Joerg Roedel <joro@8bytes.org>
Cc: Jon Mason <jdmason@kudzu.us>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Muli Ben-Yehuda <mulix@mulix.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: iommu@lists.linux-foundation.org
Link: http://lkml.kernel.org/r/20180319103826.12853-5-hch@lst.de
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Christoph Hellwig and committed by
Ingo Molnar
6e4bf586 fec777c3

+9 -95
+3
arch/x86/include/asm/device.h
··· 6 6 #if defined(CONFIG_INTEL_IOMMU) || defined(CONFIG_AMD_IOMMU) 7 7 void *iommu; /* hook for IOMMU specific extension */ 8 8 #endif 9 + #ifdef CONFIG_STA2X11 10 + bool is_sta2x11; 11 + #endif 9 12 }; 10 13 11 14 #if defined(CONFIG_X86_DEV_DMA_OPS) && defined(CONFIG_PCI_DOMAINS)
-8
arch/x86/include/asm/swiotlb.h
··· 27 27 { 28 28 } 29 29 #endif 30 - 31 - extern void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size, 32 - dma_addr_t *dma_handle, gfp_t flags, 33 - unsigned long attrs); 34 - extern void x86_swiotlb_free_coherent(struct device *dev, size_t size, 35 - void *vaddr, dma_addr_t dma_addr, 36 - unsigned long attrs); 37 - 38 30 #endif /* _ASM_X86_SWIOTLB_H */
+1 -46
arch/x86/kernel/pci-swiotlb.c
··· 17 17 18 18 int swiotlb __read_mostly; 19 19 20 - void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size, 21 - dma_addr_t *dma_handle, gfp_t flags, 22 - unsigned long attrs) 23 - { 24 - void *vaddr; 25 - 26 - /* 27 - * Don't print a warning when the first allocation attempt fails. 28 - * swiotlb_alloc_coherent() will print a warning when the DMA 29 - * memory allocation ultimately failed. 30 - */ 31 - flags |= __GFP_NOWARN; 32 - 33 - vaddr = dma_direct_alloc(hwdev, size, dma_handle, flags, attrs); 34 - if (vaddr) 35 - return vaddr; 36 - 37 - return swiotlb_alloc_coherent(hwdev, size, dma_handle, flags); 38 - } 39 - 40 - void x86_swiotlb_free_coherent(struct device *dev, size_t size, 41 - void *vaddr, dma_addr_t dma_addr, 42 - unsigned long attrs) 43 - { 44 - if (is_swiotlb_buffer(dma_to_phys(dev, dma_addr))) 45 - swiotlb_free_coherent(dev, size, vaddr, dma_addr); 46 - else 47 - dma_direct_free(dev, size, vaddr, dma_addr, attrs); 48 - } 49 - 50 - static const struct dma_map_ops x86_swiotlb_dma_ops = { 51 - .mapping_error = swiotlb_dma_mapping_error, 52 - .alloc = x86_swiotlb_alloc_coherent, 53 - .free = x86_swiotlb_free_coherent, 54 - .sync_single_for_cpu = swiotlb_sync_single_for_cpu, 55 - .sync_single_for_device = swiotlb_sync_single_for_device, 56 - .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu, 57 - .sync_sg_for_device = swiotlb_sync_sg_for_device, 58 - .map_sg = swiotlb_map_sg_attrs, 59 - .unmap_sg = swiotlb_unmap_sg_attrs, 60 - .map_page = swiotlb_map_page, 61 - .unmap_page = swiotlb_unmap_page, 62 - .dma_supported = NULL, 63 - }; 64 - 65 20 /* 66 21 * pci_swiotlb_detect_override - set swiotlb to 1 if necessary 67 22 * ··· 66 111 { 67 112 if (swiotlb) { 68 113 swiotlb_init(0); 69 - dma_ops = &x86_swiotlb_dma_ops; 114 + dma_ops = &swiotlb_dma_ops; 70 115 } 71 116 } 72 117
+5 -41
arch/x86/pci/sta2x11-fixup.c
··· 159 159 return p; 160 160 } 161 161 162 - /** 163 - * sta2x11_swiotlb_alloc_coherent - Allocate swiotlb bounce buffers 164 - * returns virtual address. This is the only "special" function here. 165 - * @dev: PCI device 166 - * @size: Size of the buffer 167 - * @dma_handle: DMA address 168 - * @flags: memory flags 169 - */ 170 - static void *sta2x11_swiotlb_alloc_coherent(struct device *dev, 171 - size_t size, 172 - dma_addr_t *dma_handle, 173 - gfp_t flags, 174 - unsigned long attrs) 175 - { 176 - void *vaddr; 177 - 178 - vaddr = x86_swiotlb_alloc_coherent(dev, size, dma_handle, flags, attrs); 179 - *dma_handle = p2a(*dma_handle, to_pci_dev(dev)); 180 - return vaddr; 181 - } 182 - 183 - /* We have our own dma_ops: the same as swiotlb but from alloc (above) */ 184 - static const struct dma_map_ops sta2x11_dma_ops = { 185 - .alloc = sta2x11_swiotlb_alloc_coherent, 186 - .free = x86_swiotlb_free_coherent, 187 - .map_page = swiotlb_map_page, 188 - .unmap_page = swiotlb_unmap_page, 189 - .map_sg = swiotlb_map_sg_attrs, 190 - .unmap_sg = swiotlb_unmap_sg_attrs, 191 - .sync_single_for_cpu = swiotlb_sync_single_for_cpu, 192 - .sync_single_for_device = swiotlb_sync_single_for_device, 193 - .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu, 194 - .sync_sg_for_device = swiotlb_sync_sg_for_device, 195 - .mapping_error = swiotlb_dma_mapping_error, 196 - .dma_supported = dma_direct_supported, 197 - }; 198 - 199 162 /* At setup time, we use our own ops if the device is a ConneXt one */ 200 163 static void sta2x11_setup_pdev(struct pci_dev *pdev) 201 164 { ··· 168 205 return; 169 206 pci_set_consistent_dma_mask(pdev, STA2X11_AMBA_SIZE - 1); 170 207 pci_set_dma_mask(pdev, STA2X11_AMBA_SIZE - 1); 171 - pdev->dev.dma_ops = &sta2x11_dma_ops; 208 + pdev->dev.dma_ops = &swiotlb_dma_ops; 209 + pdev->dev.archdata.is_sta2x11 = true; 172 210 173 211 /* We must enable all devices as master, for audio DMA to work */ 174 212 pci_set_master(pdev); ··· 189 225 { 190 226 struct sta2x11_mapping *map; 191 227 192 - if (dev->dma_ops != &sta2x11_dma_ops) { 228 + if (!dev->archdata.is_sta2x11) { 193 229 if (!dev->dma_mask) 194 230 return false; 195 231 return addr + size - 1 <= *dev->dma_mask; ··· 213 249 */ 214 250 dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 215 251 { 216 - if (dev->dma_ops != &sta2x11_dma_ops) 252 + if (!dev->archdata.is_sta2x11) 217 253 return paddr; 218 254 return p2a(paddr, to_pci_dev(dev)); 219 255 } ··· 225 261 */ 226 262 phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) 227 263 { 228 - if (dev->dma_ops != &sta2x11_dma_ops) 264 + if (!dev->archdata.is_sta2x11) 229 265 return daddr; 230 266 return a2p(daddr, to_pci_dev(dev)); 231 267 }