1 /* SPDX-License-Identifier: GPL-2.0 */
6 * Copyright IBM Corp. 2000
10 * 05/04/02 code restructuring.
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/types.h>
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <asm/dma-types.h>
24 #define IDA_SIZE_SHIFT 12
25 #define IDA_BLOCK_SIZE (1UL << IDA_SIZE_SHIFT)
27 #define IDA_2K_SIZE_SHIFT 11
28 #define IDA_2K_BLOCK_SIZE (1UL << IDA_2K_SIZE_SHIFT)
31 * Test if an address/length pair needs an idal list.
33 static inline bool idal_is_needed(void *vaddr, unsigned int length)
35 dma64_t paddr = virt_to_dma64(vaddr);
37 return (((__force unsigned long)(paddr) + length - 1) >> 31) != 0;
41 * Return the number of idal words needed for an address/length pair.
43 static inline unsigned int idal_nr_words(void *vaddr, unsigned int length)
47 cidaw = (unsigned long)vaddr & (IDA_BLOCK_SIZE - 1);
48 cidaw += length + IDA_BLOCK_SIZE - 1;
49 cidaw >>= IDA_SIZE_SHIFT;
54 * Return the number of 2K IDA words needed for an address/length pair.
56 static inline unsigned int idal_2k_nr_words(void *vaddr, unsigned int length)
60 cidaw = (unsigned long)vaddr & (IDA_2K_BLOCK_SIZE - 1);
61 cidaw += length + IDA_2K_BLOCK_SIZE - 1;
62 cidaw >>= IDA_2K_SIZE_SHIFT;
67 * Create the list of idal words for an address/length pair.
69 static inline dma64_t *idal_create_words(dma64_t *idaws, void *vaddr, unsigned int length)
71 dma64_t paddr = virt_to_dma64(vaddr);
75 cidaw = idal_nr_words(vaddr, length);
76 paddr = dma64_and(paddr, -IDA_BLOCK_SIZE);
78 paddr = dma64_add(paddr, IDA_BLOCK_SIZE);
85 * Sets the address of the data in CCW.
86 * If necessary it allocates an IDAL and sets the appropriate flags.
88 static inline int set_normalized_cda(struct ccw1 *ccw, void *vaddr)
93 if (ccw->flags & CCW_FLAG_IDA)
95 nridaws = idal_nr_words(vaddr, ccw->count);
97 idal = kcalloc(nridaws, sizeof(*idal), GFP_ATOMIC | GFP_DMA);
100 idal_create_words(idal, vaddr, ccw->count);
101 ccw->flags |= CCW_FLAG_IDA;
104 ccw->cda = virt_to_dma32(vaddr);
109 * Releases any allocated IDAL related to the CCW.
111 static inline void clear_normalized_cda(struct ccw1 *ccw)
113 if (ccw->flags & CCW_FLAG_IDA) {
114 kfree(dma32_to_virt(ccw->cda));
115 ccw->flags &= ~CCW_FLAG_IDA;
121 * Idal buffer extension
130 * Allocate an idal buffer
132 static inline struct idal_buffer *idal_buffer_alloc(size_t size, int page_order)
134 int nr_chunks, nr_ptrs, i;
135 struct idal_buffer *ib;
138 nr_ptrs = (size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_SHIFT;
139 nr_chunks = (PAGE_SIZE << page_order) >> IDA_SIZE_SHIFT;
140 ib = kmalloc(struct_size(ib, data, nr_ptrs), GFP_DMA | GFP_KERNEL);
142 return ERR_PTR(-ENOMEM);
144 ib->page_order = page_order;
145 for (i = 0; i < nr_ptrs; i++) {
146 if (i & (nr_chunks - 1)) {
147 ib->data[i] = dma64_add(ib->data[i - 1], IDA_BLOCK_SIZE);
150 vaddr = (void *)__get_free_pages(GFP_KERNEL, page_order);
153 ib->data[i] = virt_to_dma64(vaddr);
157 while (i >= nr_chunks) {
159 vaddr = dma64_to_virt(ib->data[i]);
160 free_pages((unsigned long)vaddr, ib->page_order);
163 return ERR_PTR(-ENOMEM);
167 * Free an idal buffer.
169 static inline void idal_buffer_free(struct idal_buffer *ib)
171 int nr_chunks, nr_ptrs, i;
174 nr_ptrs = (ib->size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_SHIFT;
175 nr_chunks = (PAGE_SIZE << ib->page_order) >> IDA_SIZE_SHIFT;
176 for (i = 0; i < nr_ptrs; i += nr_chunks) {
177 vaddr = dma64_to_virt(ib->data[i]);
178 free_pages((unsigned long)vaddr, ib->page_order);
184 * Test if a idal list is really needed.
186 static inline bool __idal_buffer_is_needed(struct idal_buffer *ib)
188 if (ib->size > (PAGE_SIZE << ib->page_order))
190 return idal_is_needed(dma64_to_virt(ib->data[0]), ib->size);
194 * Set channel data address to idal buffer.
196 static inline void idal_buffer_set_cda(struct idal_buffer *ib, struct ccw1 *ccw)
200 if (__idal_buffer_is_needed(ib)) {
202 ccw->cda = virt_to_dma32(ib->data);
203 ccw->flags |= CCW_FLAG_IDA;
206 * No idals needed - use direct addressing. Convert from
207 * dma64_t to virt and then to dma32_t only because of type
208 * checking. The physical address is known to be below 2GB.
210 vaddr = dma64_to_virt(ib->data[0]);
211 ccw->cda = virt_to_dma32(vaddr);
213 ccw->count = ib->size;
217 * Copy count bytes from an idal buffer to user memory
219 static inline size_t idal_buffer_to_user(struct idal_buffer *ib, void __user *to, size_t count)
225 BUG_ON(count > ib->size);
226 for (i = 0; count > IDA_BLOCK_SIZE; i++) {
227 vaddr = dma64_to_virt(ib->data[i]);
228 left = copy_to_user(to, vaddr, IDA_BLOCK_SIZE);
230 return left + count - IDA_BLOCK_SIZE;
231 to = (void __user *)to + IDA_BLOCK_SIZE;
232 count -= IDA_BLOCK_SIZE;
234 vaddr = dma64_to_virt(ib->data[i]);
235 return copy_to_user(to, vaddr, count);
239 * Copy count bytes from user memory to an idal buffer
241 static inline size_t idal_buffer_from_user(struct idal_buffer *ib, const void __user *from, size_t count)
247 BUG_ON(count > ib->size);
248 for (i = 0; count > IDA_BLOCK_SIZE; i++) {
249 vaddr = dma64_to_virt(ib->data[i]);
250 left = copy_from_user(vaddr, from, IDA_BLOCK_SIZE);
252 return left + count - IDA_BLOCK_SIZE;
253 from = (void __user *)from + IDA_BLOCK_SIZE;
254 count -= IDA_BLOCK_SIZE;
256 vaddr = dma64_to_virt(ib->data[i]);
257 return copy_from_user(vaddr, from, count);