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