1 /* Copyright 2013-2016 Freescale Semiconductor Inc.
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.
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
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.
34 #include "../include/mc-bus.h"
35 #include "../include/mc-sys.h"
37 #include "fsl-mc-private.h"
39 #include "dpmcp-cmd.h"
41 static int fsl_mc_io_set_dpmcp(struct fsl_mc_io *mc_io,
42 struct fsl_mc_device *dpmcp_dev)
46 if (WARN_ON(!dpmcp_dev))
49 if (WARN_ON(mc_io->dpmcp_dev))
52 if (WARN_ON(dpmcp_dev->mc_io))
55 error = dpmcp_open(mc_io,
57 dpmcp_dev->obj_desc.id,
58 &dpmcp_dev->mc_handle);
62 mc_io->dpmcp_dev = dpmcp_dev;
63 dpmcp_dev->mc_io = mc_io;
67 static void fsl_mc_io_unset_dpmcp(struct fsl_mc_io *mc_io)
70 struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev;
72 if (WARN_ON(!dpmcp_dev))
75 if (WARN_ON(dpmcp_dev->mc_io != mc_io))
78 error = dpmcp_close(mc_io,
80 dpmcp_dev->mc_handle);
82 dev_err(&dpmcp_dev->dev, "dpmcp_close() failed: %d\n",
86 mc_io->dpmcp_dev = NULL;
87 dpmcp_dev->mc_io = NULL;
91 * Creates an MC I/O object
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
101 * Returns '0' on Success; Error code otherwise.
103 int __must_check fsl_create_mc_io(struct device *dev,
104 phys_addr_t mc_portal_phys_addr,
106 struct fsl_mc_device *dpmcp_dev,
107 u32 flags, struct fsl_mc_io **new_mc_io)
110 struct fsl_mc_io *mc_io;
111 void __iomem *mc_portal_virt_addr;
112 struct resource *res;
114 mc_io = devm_kzalloc(dev, sizeof(*mc_io), GFP_KERNEL);
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);
125 mutex_init(&mc_io->mutex);
127 res = devm_request_mem_region(dev,
133 "devm_request_mem_region failed for MC portal %#llx\n",
134 mc_portal_phys_addr);
138 mc_portal_virt_addr = devm_ioremap_nocache(dev,
141 if (!mc_portal_virt_addr) {
143 "devm_ioremap_nocache failed for MC portal %#llx\n",
144 mc_portal_phys_addr);
148 mc_io->portal_virt_addr = mc_portal_virt_addr;
150 error = fsl_mc_io_set_dpmcp(mc_io, dpmcp_dev);
152 goto error_destroy_mc_io;
159 fsl_destroy_mc_io(mc_io);
164 * Destroys an MC I/O object
166 * @mc_io: MC I/O object to destroy
168 void fsl_destroy_mc_io(struct fsl_mc_io *mc_io)
170 struct fsl_mc_device *dpmcp_dev;
175 dpmcp_dev = mc_io->dpmcp_dev;
178 fsl_mc_io_unset_dpmcp(mc_io);
180 devm_iounmap(mc_io->dev, mc_io->portal_virt_addr);
181 devm_release_mem_region(mc_io->dev,
182 mc_io->portal_phys_addr,
185 mc_io->portal_virt_addr = NULL;
186 devm_kfree(mc_io->dev, mc_io);
190 * fsl_mc_portal_allocate - Allocates an MC portal
192 * @mc_dev: MC device for which the MC portal is to be allocated
193 * @mc_io_flags: Flags for the fsl_mc_io object that wraps the allocated
195 * @new_mc_io: Pointer to area where the pointer to the fsl_mc_io object
196 * that wraps the allocated MC portal is to be returned
198 * This function allocates an MC portal from the device's parent DPRC,
199 * from the corresponding MC bus' pool of MC portals and wraps
200 * it in a new fsl_mc_io object. If 'mc_dev' is a DPRC itself, the
201 * portal is allocated from its own MC bus.
203 int __must_check fsl_mc_portal_allocate(struct fsl_mc_device *mc_dev,
205 struct fsl_mc_io **new_mc_io)
207 struct fsl_mc_device *mc_bus_dev;
208 struct fsl_mc_bus *mc_bus;
209 phys_addr_t mc_portal_phys_addr;
210 size_t mc_portal_size;
211 struct fsl_mc_device *dpmcp_dev;
213 struct fsl_mc_resource *resource = NULL;
214 struct fsl_mc_io *mc_io = NULL;
216 if (mc_dev->flags & FSL_MC_IS_DPRC) {
219 if (WARN_ON(!dev_is_fsl_mc(mc_dev->dev.parent)))
222 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
225 mc_bus = to_fsl_mc_bus(mc_bus_dev);
227 error = fsl_mc_resource_allocate(mc_bus, FSL_MC_POOL_DPMCP, &resource);
232 dpmcp_dev = resource->data;
233 if (WARN_ON(!dpmcp_dev))
234 goto error_cleanup_resource;
236 if (dpmcp_dev->obj_desc.ver_major < DPMCP_MIN_VER_MAJOR ||
237 (dpmcp_dev->obj_desc.ver_major == DPMCP_MIN_VER_MAJOR &&
238 dpmcp_dev->obj_desc.ver_minor < DPMCP_MIN_VER_MINOR)) {
239 dev_err(&dpmcp_dev->dev,
240 "ERROR: Version %d.%d of DPMCP not supported.\n",
241 dpmcp_dev->obj_desc.ver_major,
242 dpmcp_dev->obj_desc.ver_minor);
244 goto error_cleanup_resource;
247 if (WARN_ON(dpmcp_dev->obj_desc.region_count == 0))
248 goto error_cleanup_resource;
250 mc_portal_phys_addr = dpmcp_dev->regions[0].start;
251 mc_portal_size = dpmcp_dev->regions[0].end -
252 dpmcp_dev->regions[0].start + 1;
254 if (WARN_ON(mc_portal_size != mc_bus_dev->mc_io->portal_size))
255 goto error_cleanup_resource;
257 error = fsl_create_mc_io(&mc_bus_dev->dev,
259 mc_portal_size, dpmcp_dev,
260 mc_io_flags, &mc_io);
262 goto error_cleanup_resource;
267 error_cleanup_resource:
268 fsl_mc_resource_free(resource);
271 EXPORT_SYMBOL_GPL(fsl_mc_portal_allocate);
274 * fsl_mc_portal_free - Returns an MC portal to the pool of free MC portals
277 * @mc_io: Pointer to the fsl_mc_io object that wraps the MC portal to free
279 void fsl_mc_portal_free(struct fsl_mc_io *mc_io)
281 struct fsl_mc_device *dpmcp_dev;
282 struct fsl_mc_resource *resource;
285 * Every mc_io obtained by calling fsl_mc_portal_allocate() is supposed
286 * to have a DPMCP object associated with.
288 dpmcp_dev = mc_io->dpmcp_dev;
289 if (WARN_ON(!dpmcp_dev))
292 resource = dpmcp_dev->resource;
293 if (WARN_ON(!resource || resource->type != FSL_MC_POOL_DPMCP))
296 if (WARN_ON(resource->data != dpmcp_dev))
299 fsl_destroy_mc_io(mc_io);
300 fsl_mc_resource_free(resource);
302 EXPORT_SYMBOL_GPL(fsl_mc_portal_free);
305 * fsl_mc_portal_reset - Resets the dpmcp object for a given fsl_mc_io object
307 * @mc_io: Pointer to the fsl_mc_io object that wraps the MC portal to free
309 int fsl_mc_portal_reset(struct fsl_mc_io *mc_io)
312 struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev;
314 if (WARN_ON(!dpmcp_dev))
317 error = dpmcp_reset(mc_io, 0, dpmcp_dev->mc_handle);
319 dev_err(&dpmcp_dev->dev, "dpmcp_reset() failed: %d\n", error);
325 EXPORT_SYMBOL_GPL(fsl_mc_portal_reset);