Linux 4.18.10
[linux/fpc-iii.git] / drivers / misc / mic / scif / scif_map.h
blob3e86360ba5a6b07287abea9b05092442d7b1162a
1 /*
2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2014 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * Intel SCIF driver.
18 #ifndef SCIF_MAP_H
19 #define SCIF_MAP_H
21 #include "../bus/scif_bus.h"
23 static __always_inline void *
24 scif_alloc_coherent(dma_addr_t *dma_handle,
25 struct scif_dev *scifdev, size_t size,
26 gfp_t gfp)
28 void *va;
30 if (scifdev_self(scifdev)) {
31 va = kmalloc(size, gfp);
32 if (va)
33 *dma_handle = virt_to_phys(va);
34 } else {
35 va = dma_alloc_coherent(&scifdev->sdev->dev,
36 size, dma_handle, gfp);
37 if (va && scifdev_is_p2p(scifdev))
38 *dma_handle = *dma_handle + scifdev->base_addr;
40 return va;
43 static __always_inline void
44 scif_free_coherent(void *va, dma_addr_t local,
45 struct scif_dev *scifdev, size_t size)
47 if (scifdev_self(scifdev)) {
48 kfree(va);
49 } else {
50 if (scifdev_is_p2p(scifdev) && local > scifdev->base_addr)
51 local = local - scifdev->base_addr;
52 dma_free_coherent(&scifdev->sdev->dev,
53 size, va, local);
57 static __always_inline int
58 scif_map_single(dma_addr_t *dma_handle,
59 void *local, struct scif_dev *scifdev, size_t size)
61 int err = 0;
63 if (scifdev_self(scifdev)) {
64 *dma_handle = virt_to_phys((local));
65 } else {
66 *dma_handle = dma_map_single(&scifdev->sdev->dev,
67 local, size, DMA_BIDIRECTIONAL);
68 if (dma_mapping_error(&scifdev->sdev->dev, *dma_handle))
69 err = -ENOMEM;
70 else if (scifdev_is_p2p(scifdev))
71 *dma_handle = *dma_handle + scifdev->base_addr;
73 if (err)
74 *dma_handle = 0;
75 return err;
78 static __always_inline void
79 scif_unmap_single(dma_addr_t local, struct scif_dev *scifdev,
80 size_t size)
82 if (!scifdev_self(scifdev)) {
83 if (scifdev_is_p2p(scifdev))
84 local = local - scifdev->base_addr;
85 dma_unmap_single(&scifdev->sdev->dev, local,
86 size, DMA_BIDIRECTIONAL);
90 static __always_inline void *
91 scif_ioremap(dma_addr_t phys, size_t size, struct scif_dev *scifdev)
93 void *out_virt;
94 struct scif_hw_dev *sdev = scifdev->sdev;
96 if (scifdev_self(scifdev))
97 out_virt = phys_to_virt(phys);
98 else
99 out_virt = (void __force *)
100 sdev->hw_ops->ioremap(sdev, phys, size);
101 return out_virt;
104 static __always_inline void
105 scif_iounmap(void *virt, size_t len, struct scif_dev *scifdev)
107 if (!scifdev_self(scifdev)) {
108 struct scif_hw_dev *sdev = scifdev->sdev;
110 sdev->hw_ops->iounmap(sdev, (void __force __iomem *)virt);
114 static __always_inline int
115 scif_map_page(dma_addr_t *dma_handle, struct page *page,
116 struct scif_dev *scifdev)
118 int err = 0;
120 if (scifdev_self(scifdev)) {
121 *dma_handle = page_to_phys(page);
122 } else {
123 struct scif_hw_dev *sdev = scifdev->sdev;
124 *dma_handle = dma_map_page(&sdev->dev,
125 page, 0x0, PAGE_SIZE,
126 DMA_BIDIRECTIONAL);
127 if (dma_mapping_error(&sdev->dev, *dma_handle))
128 err = -ENOMEM;
129 else if (scifdev_is_p2p(scifdev))
130 *dma_handle = *dma_handle + scifdev->base_addr;
132 if (err)
133 *dma_handle = 0;
134 return err;
136 #endif /* SCIF_MAP_H */