Linux-libre 4.9.174-gnu
[librecmc/linux-libre.git] / drivers / staging / fsl-mc / bus / mc-io.c
1 /* Copyright 2013-2016 Freescale Semiconductor Inc.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5  *     * Redistributions of source code must retain the above copyright
6  *       notice, this list of conditions and the following disclaimer.
7  *     * Redistributions in binary form must reproduce the above copyright
8  *       notice, this list of conditions and the following disclaimer in the
9  *       documentation and/or other materials provided with the distribution.
10  *     * Neither the name of the above-listed copyright holders nor the
11  *       names of any contributors may be used to endorse or promote products
12  *       derived from this software without specific prior written permission.
13  *
14  *
15  * ALTERNATIVELY, this software may be distributed under the terms of the
16  * GNU General Public License ("GPL") as published by the Free Software
17  * Foundation, either version 2 of that License or (at your option) any
18  * later version.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <linux/io.h>
34 #include "../include/mc-bus.h"
35 #include "../include/mc-sys.h"
36
37 #include "fsl-mc-private.h"
38 #include "dpmcp.h"
39 #include "dpmcp-cmd.h"
40
41 static int fsl_mc_io_set_dpmcp(struct fsl_mc_io *mc_io,
42                                struct fsl_mc_device *dpmcp_dev)
43 {
44         int error;
45
46         if (WARN_ON(!dpmcp_dev))
47                 return -EINVAL;
48
49         if (WARN_ON(mc_io->dpmcp_dev))
50                 return -EINVAL;
51
52         if (WARN_ON(dpmcp_dev->mc_io))
53                 return -EINVAL;
54
55         error = dpmcp_open(mc_io,
56                            0,
57                            dpmcp_dev->obj_desc.id,
58                            &dpmcp_dev->mc_handle);
59         if (error < 0)
60                 return error;
61
62         mc_io->dpmcp_dev = dpmcp_dev;
63         dpmcp_dev->mc_io = mc_io;
64         return 0;
65 }
66
67 static void fsl_mc_io_unset_dpmcp(struct fsl_mc_io *mc_io)
68 {
69         int error;
70         struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev;
71
72         if (WARN_ON(!dpmcp_dev))
73                 return;
74
75         if (WARN_ON(dpmcp_dev->mc_io != mc_io))
76                 return;
77
78         error = dpmcp_close(mc_io,
79                             0,
80                             dpmcp_dev->mc_handle);
81         if (error < 0) {
82                 dev_err(&dpmcp_dev->dev, "dpmcp_close() failed: %d\n",
83                         error);
84         }
85
86         mc_io->dpmcp_dev = NULL;
87         dpmcp_dev->mc_io = NULL;
88 }
89
90 /**
91  * Creates an MC I/O object
92  *
93  * @dev: device to be associated with the MC I/O object
94  * @mc_portal_phys_addr: physical address of the MC portal to use
95  * @mc_portal_size: size in bytes of the MC portal
96  * @dpmcp-dev: Pointer to the DPMCP object associated with this MC I/O
97  * object or NULL if none.
98  * @flags: flags for the new MC I/O object
99  * @new_mc_io: Area to return pointer to newly created MC I/O object
100  *
101  * Returns '0' on Success; Error code otherwise.
102  */
103 int __must_check fsl_create_mc_io(struct device *dev,
104                                   phys_addr_t mc_portal_phys_addr,
105                                   u32 mc_portal_size,
106                                   struct fsl_mc_device *dpmcp_dev,
107                                   u32 flags, struct fsl_mc_io **new_mc_io)
108 {
109         int error;
110         struct fsl_mc_io *mc_io;
111         void __iomem *mc_portal_virt_addr;
112         struct resource *res;
113
114         mc_io = devm_kzalloc(dev, sizeof(*mc_io), GFP_KERNEL);
115         if (!mc_io)
116                 return -ENOMEM;
117
118         mc_io->dev = dev;
119         mc_io->flags = flags;
120         mc_io->portal_phys_addr = mc_portal_phys_addr;
121         mc_io->portal_size = mc_portal_size;
122         if (flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
123                 spin_lock_init(&mc_io->spinlock);
124         else
125                 mutex_init(&mc_io->mutex);
126
127         res = devm_request_mem_region(dev,
128                                       mc_portal_phys_addr,
129                                       mc_portal_size,
130                                       "mc_portal");
131         if (!res) {
132                 dev_err(dev,
133                         "devm_request_mem_region failed for MC portal %#llx\n",
134                         mc_portal_phys_addr);
135                 return -EBUSY;
136         }
137
138         mc_portal_virt_addr = devm_ioremap_nocache(dev,
139                                                    mc_portal_phys_addr,
140                                                    mc_portal_size);
141         if (!mc_portal_virt_addr) {
142                 dev_err(dev,
143                         "devm_ioremap_nocache failed for MC portal %#llx\n",
144                         mc_portal_phys_addr);
145                 return -ENXIO;
146         }
147
148         mc_io->portal_virt_addr = mc_portal_virt_addr;
149         if (dpmcp_dev) {
150                 error = fsl_mc_io_set_dpmcp(mc_io, dpmcp_dev);
151                 if (error < 0)
152                         goto error_destroy_mc_io;
153         }
154
155         *new_mc_io = mc_io;
156         return 0;
157
158 error_destroy_mc_io:
159         fsl_destroy_mc_io(mc_io);
160         return error;
161 }
162
163 /**
164  * Destroys an MC I/O object
165  *
166  * @mc_io: MC I/O object to destroy
167  */
168 void fsl_destroy_mc_io(struct fsl_mc_io *mc_io)
169 {
170         struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev;
171
172         if (dpmcp_dev)
173                 fsl_mc_io_unset_dpmcp(mc_io);
174
175         devm_iounmap(mc_io->dev, mc_io->portal_virt_addr);
176         devm_release_mem_region(mc_io->dev,
177                                 mc_io->portal_phys_addr,
178                                 mc_io->portal_size);
179
180         mc_io->portal_virt_addr = NULL;
181         devm_kfree(mc_io->dev, mc_io);
182 }
183
184 /**
185  * fsl_mc_portal_allocate - Allocates an MC portal
186  *
187  * @mc_dev: MC device for which the MC portal is to be allocated
188  * @mc_io_flags: Flags for the fsl_mc_io object that wraps the allocated
189  * MC portal.
190  * @new_mc_io: Pointer to area where the pointer to the fsl_mc_io object
191  * that wraps the allocated MC portal is to be returned
192  *
193  * This function allocates an MC portal from the device's parent DPRC,
194  * from the corresponding MC bus' pool of MC portals and wraps
195  * it in a new fsl_mc_io object. If 'mc_dev' is a DPRC itself, the
196  * portal is allocated from its own MC bus.
197  */
198 int __must_check fsl_mc_portal_allocate(struct fsl_mc_device *mc_dev,
199                                         u16 mc_io_flags,
200                                         struct fsl_mc_io **new_mc_io)
201 {
202         struct fsl_mc_device *mc_bus_dev;
203         struct fsl_mc_bus *mc_bus;
204         phys_addr_t mc_portal_phys_addr;
205         size_t mc_portal_size;
206         struct fsl_mc_device *dpmcp_dev;
207         int error = -EINVAL;
208         struct fsl_mc_resource *resource = NULL;
209         struct fsl_mc_io *mc_io = NULL;
210
211         if (mc_dev->flags & FSL_MC_IS_DPRC) {
212                 mc_bus_dev = mc_dev;
213         } else {
214                 if (WARN_ON(!dev_is_fsl_mc(mc_dev->dev.parent)))
215                         return error;
216
217                 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
218         }
219
220         mc_bus = to_fsl_mc_bus(mc_bus_dev);
221         *new_mc_io = NULL;
222         error = fsl_mc_resource_allocate(mc_bus, FSL_MC_POOL_DPMCP, &resource);
223         if (error < 0)
224                 return error;
225
226         error = -EINVAL;
227         dpmcp_dev = resource->data;
228         if (WARN_ON(!dpmcp_dev))
229                 goto error_cleanup_resource;
230
231         if (dpmcp_dev->obj_desc.ver_major < DPMCP_MIN_VER_MAJOR ||
232             (dpmcp_dev->obj_desc.ver_major == DPMCP_MIN_VER_MAJOR &&
233              dpmcp_dev->obj_desc.ver_minor < DPMCP_MIN_VER_MINOR)) {
234                 dev_err(&dpmcp_dev->dev,
235                         "ERROR: Version %d.%d of DPMCP not supported.\n",
236                         dpmcp_dev->obj_desc.ver_major,
237                         dpmcp_dev->obj_desc.ver_minor);
238                 error = -ENOTSUPP;
239                 goto error_cleanup_resource;
240         }
241
242         if (WARN_ON(dpmcp_dev->obj_desc.region_count == 0))
243                 goto error_cleanup_resource;
244
245         mc_portal_phys_addr = dpmcp_dev->regions[0].start;
246         mc_portal_size = dpmcp_dev->regions[0].end -
247                          dpmcp_dev->regions[0].start + 1;
248
249         if (WARN_ON(mc_portal_size != mc_bus_dev->mc_io->portal_size))
250                 goto error_cleanup_resource;
251
252         error = fsl_create_mc_io(&mc_bus_dev->dev,
253                                  mc_portal_phys_addr,
254                                  mc_portal_size, dpmcp_dev,
255                                  mc_io_flags, &mc_io);
256         if (error < 0)
257                 goto error_cleanup_resource;
258
259         *new_mc_io = mc_io;
260         return 0;
261
262 error_cleanup_resource:
263         fsl_mc_resource_free(resource);
264         return error;
265 }
266 EXPORT_SYMBOL_GPL(fsl_mc_portal_allocate);
267
268 /**
269  * fsl_mc_portal_free - Returns an MC portal to the pool of free MC portals
270  * of a given MC bus
271  *
272  * @mc_io: Pointer to the fsl_mc_io object that wraps the MC portal to free
273  */
274 void fsl_mc_portal_free(struct fsl_mc_io *mc_io)
275 {
276         struct fsl_mc_device *dpmcp_dev;
277         struct fsl_mc_resource *resource;
278
279         /*
280          * Every mc_io obtained by calling fsl_mc_portal_allocate() is supposed
281          * to have a DPMCP object associated with.
282          */
283         dpmcp_dev = mc_io->dpmcp_dev;
284         if (WARN_ON(!dpmcp_dev))
285                 return;
286
287         resource = dpmcp_dev->resource;
288         if (WARN_ON(!resource || resource->type != FSL_MC_POOL_DPMCP))
289                 return;
290
291         if (WARN_ON(resource->data != dpmcp_dev))
292                 return;
293
294         fsl_destroy_mc_io(mc_io);
295         fsl_mc_resource_free(resource);
296 }
297 EXPORT_SYMBOL_GPL(fsl_mc_portal_free);
298
299 /**
300  * fsl_mc_portal_reset - Resets the dpmcp object for a given fsl_mc_io object
301  *
302  * @mc_io: Pointer to the fsl_mc_io object that wraps the MC portal to free
303  */
304 int fsl_mc_portal_reset(struct fsl_mc_io *mc_io)
305 {
306         int error;
307         struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev;
308
309         if (WARN_ON(!dpmcp_dev))
310                 return -EINVAL;
311
312         error = dpmcp_reset(mc_io, 0, dpmcp_dev->mc_handle);
313         if (error < 0) {
314                 dev_err(&dpmcp_dev->dev, "dpmcp_reset() failed: %d\n", error);
315                 return error;
316         }
317
318         return 0;
319 }
320 EXPORT_SYMBOL_GPL(fsl_mc_portal_reset);