1 /*
2  * ISHTP DMA I/F functions
3  *
4  * Copyright (c) 2003-2016, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  */
16 
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/delay.h>
21 #include <linux/dma-mapping.h>
22 #include "ishtp-dev.h"
23 #include "client.h"
24 
25 /**
26  * ishtp_cl_alloc_dma_buf() - Allocate DMA RX and TX buffer
27  * @dev: ishtp device
28  *
29  * Allocate RX and TX DMA buffer once during bus setup.
30  * It allocates 1MB, RX and TX DMA buffer, which are divided
31  * into slots.
32  */
ishtp_cl_alloc_dma_buf(struct ishtp_device * dev)33 void	ishtp_cl_alloc_dma_buf(struct ishtp_device *dev)
34 {
35 	dma_addr_t	h;
36 
37 	if (dev->ishtp_host_dma_tx_buf)
38 		return;
39 
40 	dev->ishtp_host_dma_tx_buf_size = 1024*1024;
41 	dev->ishtp_host_dma_rx_buf_size = 1024*1024;
42 
43 	/* Allocate Tx buffer and init usage bitmap */
44 	dev->ishtp_host_dma_tx_buf = dma_alloc_coherent(dev->devc,
45 					dev->ishtp_host_dma_tx_buf_size,
46 					&h, GFP_KERNEL);
47 	if (dev->ishtp_host_dma_tx_buf)
48 		dev->ishtp_host_dma_tx_buf_phys = h;
49 
50 	dev->ishtp_dma_num_slots = dev->ishtp_host_dma_tx_buf_size /
51 						DMA_SLOT_SIZE;
52 
53 	dev->ishtp_dma_tx_map = kcalloc(dev->ishtp_dma_num_slots,
54 					sizeof(uint8_t),
55 					GFP_KERNEL);
56 	spin_lock_init(&dev->ishtp_dma_tx_lock);
57 
58 	/* Allocate Rx buffer */
59 	dev->ishtp_host_dma_rx_buf = dma_alloc_coherent(dev->devc,
60 					dev->ishtp_host_dma_rx_buf_size,
61 					 &h, GFP_KERNEL);
62 
63 	if (dev->ishtp_host_dma_rx_buf)
64 		dev->ishtp_host_dma_rx_buf_phys = h;
65 }
66 
67 /**
68  * ishtp_cl_free_dma_buf() - Free DMA RX and TX buffer
69  * @dev: ishtp device
70  *
71  * Free DMA buffer when all clients are released. This is
72  * only happens during error path in ISH built in driver
73  * model
74  */
ishtp_cl_free_dma_buf(struct ishtp_device * dev)75 void	ishtp_cl_free_dma_buf(struct ishtp_device *dev)
76 {
77 	dma_addr_t	h;
78 
79 	if (dev->ishtp_host_dma_tx_buf) {
80 		h = dev->ishtp_host_dma_tx_buf_phys;
81 		dma_free_coherent(dev->devc, dev->ishtp_host_dma_tx_buf_size,
82 				  dev->ishtp_host_dma_tx_buf, h);
83 	}
84 
85 	if (dev->ishtp_host_dma_rx_buf) {
86 		h = dev->ishtp_host_dma_rx_buf_phys;
87 		dma_free_coherent(dev->devc, dev->ishtp_host_dma_rx_buf_size,
88 				  dev->ishtp_host_dma_rx_buf, h);
89 	}
90 
91 	kfree(dev->ishtp_dma_tx_map);
92 	dev->ishtp_host_dma_tx_buf = NULL;
93 	dev->ishtp_host_dma_rx_buf = NULL;
94 	dev->ishtp_dma_tx_map = NULL;
95 }
96 
97 /*
98  * ishtp_cl_get_dma_send_buf() - Get a DMA memory slot
99  * @dev:	ishtp device
100  * @size:	Size of memory to get
101  *
102  * Find and return free address of "size" bytes in dma tx buffer.
103  * the function will mark this address as "in-used" memory.
104  *
105  * Return: NULL when no free buffer else a buffer to copy
106  */
ishtp_cl_get_dma_send_buf(struct ishtp_device * dev,uint32_t size)107 void *ishtp_cl_get_dma_send_buf(struct ishtp_device *dev,
108 				uint32_t size)
109 {
110 	unsigned long	flags;
111 	int i, j, free;
112 	/* additional slot is needed if there is rem */
113 	int required_slots = (size / DMA_SLOT_SIZE)
114 		+ 1 * (size % DMA_SLOT_SIZE != 0);
115 
116 	if (!dev->ishtp_dma_tx_map) {
117 		dev_err(dev->devc, "Fail to allocate Tx map\n");
118 		return NULL;
119 	}
120 
121 	spin_lock_irqsave(&dev->ishtp_dma_tx_lock, flags);
122 	for (i = 0; i <= (dev->ishtp_dma_num_slots - required_slots); i++) {
123 		free = 1;
124 		for (j = 0; j < required_slots; j++)
125 			if (dev->ishtp_dma_tx_map[i+j]) {
126 				free = 0;
127 				i += j;
128 				break;
129 			}
130 		if (free) {
131 			/* mark memory as "caught" */
132 			for (j = 0; j < required_slots; j++)
133 				dev->ishtp_dma_tx_map[i+j] = 1;
134 			spin_unlock_irqrestore(&dev->ishtp_dma_tx_lock, flags);
135 			return (i * DMA_SLOT_SIZE) +
136 				(unsigned char *)dev->ishtp_host_dma_tx_buf;
137 		}
138 	}
139 	spin_unlock_irqrestore(&dev->ishtp_dma_tx_lock, flags);
140 	dev_err(dev->devc, "No free DMA buffer to send msg\n");
141 	return NULL;
142 }
143 
144 /*
145  * ishtp_cl_release_dma_acked_mem() - Release DMA memory slot
146  * @dev:	ishtp device
147  * @msg_addr:	message address of slot
148  * @size:	Size of memory to get
149  *
150  * Release_dma_acked_mem - returnes the acked memory to free list.
151  * (from msg_addr, size bytes long)
152  */
ishtp_cl_release_dma_acked_mem(struct ishtp_device * dev,void * msg_addr,uint8_t size)153 void ishtp_cl_release_dma_acked_mem(struct ishtp_device *dev,
154 				    void *msg_addr,
155 				    uint8_t size)
156 {
157 	unsigned long	flags;
158 	int acked_slots = (size / DMA_SLOT_SIZE)
159 		+ 1 * (size % DMA_SLOT_SIZE != 0);
160 	int i, j;
161 
162 	if ((msg_addr - dev->ishtp_host_dma_tx_buf) % DMA_SLOT_SIZE) {
163 		dev_err(dev->devc, "Bad DMA Tx ack address\n");
164 		return;
165 	}
166 
167 	if (!dev->ishtp_dma_tx_map) {
168 		dev_err(dev->devc, "Fail to allocate Tx map\n");
169 		return;
170 	}
171 
172 	i = (msg_addr - dev->ishtp_host_dma_tx_buf) / DMA_SLOT_SIZE;
173 	spin_lock_irqsave(&dev->ishtp_dma_tx_lock, flags);
174 	for (j = 0; j < acked_slots; j++) {
175 		if ((i + j) >= dev->ishtp_dma_num_slots ||
176 					!dev->ishtp_dma_tx_map[i+j]) {
177 			/* no such slot, or memory is already free */
178 			spin_unlock_irqrestore(&dev->ishtp_dma_tx_lock, flags);
179 			dev_err(dev->devc, "Bad DMA Tx ack address\n");
180 			return;
181 		}
182 		dev->ishtp_dma_tx_map[i+j] = 0;
183 	}
184 	spin_unlock_irqrestore(&dev->ishtp_dma_tx_lock, flags);
185 }
186