blob: 44f64b6f0fc90654e78d9a15ab4230ab066c9c84 [file] [log] [blame]
J. German Riverabbf9d172015-03-05 19:29:10 -06001/*
2 * Freescale Management Complex (MC) bus driver
3 *
4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
5 * Author: German Rivera <German.Rivera@freescale.com>
6 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
J. German Riverabbf9d172015-03-05 19:29:10 -060012#include <linux/module.h>
13#include <linux/of_device.h>
14#include <linux/of_address.h>
15#include <linux/ioport.h>
16#include <linux/slab.h>
17#include <linux/limits.h>
J. German Rivera660a24b2016-01-06 16:03:29 -060018#include <linux/bitops.h>
19#include <linux/msi.h>
Stuart Yoder5143ecf2016-08-23 17:14:18 -050020#include <linux/dma-mapping.h>
Stuart Yoderd4e75132016-08-23 17:14:23 -050021#include "../include/mc-bus.h"
J. German Riverabbf9d172015-03-05 19:29:10 -060022#include "../include/dpmng.h"
23#include "../include/mc-sys.h"
Stuart Yoderd4e75132016-08-23 17:14:23 -050024
Stuart Yoder243444f2016-08-23 17:13:30 -050025#include "fsl-mc-private.h"
J. German Riverabbf9d172015-03-05 19:29:10 -060026#include "dprc-cmd.h"
27
28static struct kmem_cache *mc_dev_cache;
29
30/**
Stuart Yodere730d862016-08-23 17:13:40 -050031 * Default DMA mask for devices on a fsl-mc bus
32 */
33#define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
34
35/**
36 * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
37 * @root_mc_bus_dev: MC object device representing the root DPRC
38 * @num_translation_ranges: number of entries in addr_translation_ranges
39 * @translation_ranges: array of bus to system address translation ranges
40 */
41struct fsl_mc {
42 struct fsl_mc_device *root_mc_bus_dev;
43 u8 num_translation_ranges;
44 struct fsl_mc_addr_translation_range *translation_ranges;
45};
46
47/**
48 * struct fsl_mc_addr_translation_range - bus to system address translation
49 * range
50 * @mc_region_type: Type of MC region for the range being translated
51 * @start_mc_offset: Start MC offset of the range being translated
52 * @end_mc_offset: MC offset of the first byte after the range (last MC
53 * offset of the range is end_mc_offset - 1)
54 * @start_phys_addr: system physical address corresponding to start_mc_addr
55 */
56struct fsl_mc_addr_translation_range {
57 enum dprc_region_type mc_region_type;
58 u64 start_mc_offset;
59 u64 end_mc_offset;
60 phys_addr_t start_phys_addr;
61};
62
63/**
J. German Riverabbf9d172015-03-05 19:29:10 -060064 * fsl_mc_bus_match - device to driver matching callback
65 * @dev: the MC object device structure to match against
66 * @drv: the device driver to search for matching MC object device id
67 * structures
68 *
69 * Returns 1 on success, 0 otherwise.
70 */
71static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
72{
Stuart Yoder57538af2016-06-22 16:40:44 -050073 const struct fsl_mc_device_id *id;
J. German Riverabbf9d172015-03-05 19:29:10 -060074 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
75 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
76 bool found = false;
77
Itai Katz14f92802015-10-04 10:09:50 +030078 if (WARN_ON(!fsl_mc_bus_exists()))
J. German Riverabbf9d172015-03-05 19:29:10 -060079 goto out;
80
81 if (!mc_drv->match_id_table)
82 goto out;
83
84 /*
85 * If the object is not 'plugged' don't match.
86 * Only exception is the root DPRC, which is a special case.
87 */
88 if ((mc_dev->obj_desc.state & DPRC_OBJ_STATE_PLUGGED) == 0 &&
Itai Katzb55f00c2015-10-04 10:09:51 +030089 !fsl_mc_is_root_dprc(&mc_dev->dev))
J. German Riverabbf9d172015-03-05 19:29:10 -060090 goto out;
91
92 /*
93 * Traverse the match_id table of the given driver, trying to find
94 * a matching for the given MC object device.
95 */
96 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
97 if (id->vendor == mc_dev->obj_desc.vendor &&
J. German Rivera3b0a9b12015-03-27 16:01:09 -050098 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
Itai Katz9787d4e2016-04-11 11:55:40 -050099 found = true;
J. German Rivera3b0a9b12015-03-27 16:01:09 -0500100
J. German Riverabbf9d172015-03-05 19:29:10 -0600101 break;
102 }
103 }
104
105out:
106 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
107 return found;
108}
109
110/**
111 * fsl_mc_bus_uevent - callback invoked when a device is added
112 */
113static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
114{
Stuart Yoderd568b762016-06-22 16:40:43 -0500115 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
116
117 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
118 mc_dev->obj_desc.vendor,
119 mc_dev->obj_desc.type))
120 return -ENOMEM;
121
J. German Riverabbf9d172015-03-05 19:29:10 -0600122 return 0;
123}
124
Stuart Yoder3d579c32016-06-22 16:40:42 -0500125static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
126 char *buf)
127{
128 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
129
130 return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
131 mc_dev->obj_desc.type);
132}
133static DEVICE_ATTR_RO(modalias);
134
135static struct attribute *fsl_mc_dev_attrs[] = {
136 &dev_attr_modalias.attr,
137 NULL,
138};
139
Wei Yongjuna71a6d92016-08-28 16:19:29 +0000140ATTRIBUTE_GROUPS(fsl_mc_dev);
Stuart Yoder3d579c32016-06-22 16:40:42 -0500141
J. German Riverabbf9d172015-03-05 19:29:10 -0600142struct bus_type fsl_mc_bus_type = {
143 .name = "fsl-mc",
144 .match = fsl_mc_bus_match,
145 .uevent = fsl_mc_bus_uevent,
Stuart Yoder3d579c32016-06-22 16:40:42 -0500146 .dev_groups = fsl_mc_dev_groups,
J. German Riverabbf9d172015-03-05 19:29:10 -0600147};
148EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
149
Itai Katz565b9452015-10-04 10:09:53 +0300150static atomic_t root_dprc_count = ATOMIC_INIT(0);
151
J. German Riverabbf9d172015-03-05 19:29:10 -0600152static int fsl_mc_driver_probe(struct device *dev)
153{
154 struct fsl_mc_driver *mc_drv;
155 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
156 int error;
157
158 if (WARN_ON(!dev->driver))
159 return -EINVAL;
160
161 mc_drv = to_fsl_mc_driver(dev->driver);
162 if (WARN_ON(!mc_drv->probe))
163 return -EINVAL;
164
165 error = mc_drv->probe(mc_dev);
166 if (error < 0) {
167 dev_err(dev, "MC object device probe callback failed: %d\n",
168 error);
169 return error;
170 }
171
172 return 0;
173}
174
175static int fsl_mc_driver_remove(struct device *dev)
176{
177 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
178 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
179 int error;
180
181 if (WARN_ON(!dev->driver))
182 return -EINVAL;
183
184 error = mc_drv->remove(mc_dev);
185 if (error < 0) {
186 dev_err(dev,
187 "MC object device remove callback failed: %d\n",
188 error);
189 return error;
190 }
191
192 return 0;
193}
194
195static void fsl_mc_driver_shutdown(struct device *dev)
196{
197 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
198 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
199
200 mc_drv->shutdown(mc_dev);
201}
202
203/**
204 * __fsl_mc_driver_register - registers a child device driver with the
205 * MC bus
206 *
207 * This function is implicitly invoked from the registration function of
208 * fsl_mc device drivers, which is generated by the
209 * module_fsl_mc_driver() macro.
210 */
211int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
212 struct module *owner)
213{
214 int error;
215
216 mc_driver->driver.owner = owner;
217 mc_driver->driver.bus = &fsl_mc_bus_type;
218
219 if (mc_driver->probe)
220 mc_driver->driver.probe = fsl_mc_driver_probe;
221
222 if (mc_driver->remove)
223 mc_driver->driver.remove = fsl_mc_driver_remove;
224
225 if (mc_driver->shutdown)
226 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
227
228 error = driver_register(&mc_driver->driver);
229 if (error < 0) {
230 pr_err("driver_register() failed for %s: %d\n",
231 mc_driver->driver.name, error);
232 return error;
233 }
234
235 pr_info("MC object device driver %s registered\n",
236 mc_driver->driver.name);
237 return 0;
238}
239EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
240
241/**
242 * fsl_mc_driver_unregister - unregisters a device driver from the
243 * MC bus
244 */
245void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
246{
247 driver_unregister(&mc_driver->driver);
248}
249EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
250
Itai Katz14f92802015-10-04 10:09:50 +0300251/**
252 * fsl_mc_bus_exists - check if a root dprc exists
253 */
254bool fsl_mc_bus_exists(void)
255{
Itai Katz565b9452015-10-04 10:09:53 +0300256 return atomic_read(&root_dprc_count) > 0;
Itai Katz14f92802015-10-04 10:09:50 +0300257}
258EXPORT_SYMBOL_GPL(fsl_mc_bus_exists);
259
Itai Katzb55f00c2015-10-04 10:09:51 +0300260/**
Ramiro Oliveiraf86a1802016-09-30 15:01:21 +0100261 * fsl_mc_get_root_dprc - function to traverse to the root dprc
262 */
Stuart Yoder27365d82016-08-23 17:13:46 -0500263void fsl_mc_get_root_dprc(struct device *dev,
264 struct device **root_dprc_dev)
Itai Katze4ea40f2015-10-04 10:09:52 +0300265{
266 if (WARN_ON(!dev)) {
267 *root_dprc_dev = NULL;
Nipun Guptadf5e9b52016-06-29 22:44:39 +0530268 } else if (WARN_ON(!dev_is_fsl_mc(dev))) {
Itai Katze4ea40f2015-10-04 10:09:52 +0300269 *root_dprc_dev = NULL;
270 } else {
271 *root_dprc_dev = dev;
Nipun Guptadf5e9b52016-06-29 22:44:39 +0530272 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
Itai Katze4ea40f2015-10-04 10:09:52 +0300273 *root_dprc_dev = (*root_dprc_dev)->parent;
274 }
275}
Stuart Yoder27365d82016-08-23 17:13:46 -0500276EXPORT_SYMBOL_GPL(fsl_mc_get_root_dprc);
Itai Katze4ea40f2015-10-04 10:09:52 +0300277
Itai Katz9529d162016-04-11 11:55:55 -0500278static int get_dprc_attr(struct fsl_mc_io *mc_io,
279 int container_id, struct dprc_attributes *attr)
J. German Riverabbf9d172015-03-05 19:29:10 -0600280{
Nayeemahmed Badebadea2f9ff62015-09-04 22:30:33 +0530281 u16 dprc_handle;
J. German Riverabbf9d172015-03-05 19:29:10 -0600282 int error;
283
J. German Rivera1ee695f2015-09-23 16:11:03 -0500284 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
J. German Riverabbf9d172015-03-05 19:29:10 -0600285 if (error < 0) {
Cihangir Akturk2e115902016-03-14 18:14:07 +0200286 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
J. German Riverabbf9d172015-03-05 19:29:10 -0600287 return error;
288 }
289
Itai Katz9529d162016-04-11 11:55:55 -0500290 memset(attr, 0, sizeof(struct dprc_attributes));
291 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
J. German Riverabbf9d172015-03-05 19:29:10 -0600292 if (error < 0) {
Cihangir Akturk2e115902016-03-14 18:14:07 +0200293 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
Bhumika Goyal454b0ec2016-03-04 19:15:55 +0530294 error);
J. German Riverabbf9d172015-03-05 19:29:10 -0600295 goto common_cleanup;
296 }
297
J. German Riverabbf9d172015-03-05 19:29:10 -0600298 error = 0;
299
300common_cleanup:
J. German Rivera1ee695f2015-09-23 16:11:03 -0500301 (void)dprc_close(mc_io, 0, dprc_handle);
J. German Riverabbf9d172015-03-05 19:29:10 -0600302 return error;
303}
304
Itai Katz9529d162016-04-11 11:55:55 -0500305static int get_dprc_icid(struct fsl_mc_io *mc_io,
306 int container_id, u16 *icid)
307{
308 struct dprc_attributes attr;
309 int error;
310
311 error = get_dprc_attr(mc_io, container_id, &attr);
312 if (error == 0)
313 *icid = attr.icid;
314
315 return error;
316}
317
318static int get_dprc_version(struct fsl_mc_io *mc_io,
319 int container_id, u16 *major, u16 *minor)
320{
321 struct dprc_attributes attr;
322 int error;
323
324 error = get_dprc_attr(mc_io, container_id, &attr);
325 if (error == 0) {
326 *major = attr.version.major;
327 *minor = attr.version.minor;
328 }
329
330 return error;
331}
332
Itai Katze4ea40f2015-10-04 10:09:52 +0300333static int translate_mc_addr(struct fsl_mc_device *mc_dev,
334 enum dprc_region_type mc_region_type,
J. German Rivera1ee695f2015-09-23 16:11:03 -0500335 u64 mc_offset, phys_addr_t *phys_addr)
J. German Riverabbf9d172015-03-05 19:29:10 -0600336{
337 int i;
Itai Katze4ea40f2015-10-04 10:09:52 +0300338 struct device *root_dprc_dev;
339 struct fsl_mc *mc;
340
341 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
342 if (WARN_ON(!root_dprc_dev))
343 return -EINVAL;
344 mc = dev_get_drvdata(root_dprc_dev->parent);
J. German Riverabbf9d172015-03-05 19:29:10 -0600345
346 if (mc->num_translation_ranges == 0) {
347 /*
348 * Do identity mapping:
349 */
J. German Rivera1ee695f2015-09-23 16:11:03 -0500350 *phys_addr = mc_offset;
J. German Riverabbf9d172015-03-05 19:29:10 -0600351 return 0;
352 }
353
354 for (i = 0; i < mc->num_translation_ranges; i++) {
355 struct fsl_mc_addr_translation_range *range =
356 &mc->translation_ranges[i];
357
J. German Rivera1ee695f2015-09-23 16:11:03 -0500358 if (mc_region_type == range->mc_region_type &&
359 mc_offset >= range->start_mc_offset &&
360 mc_offset < range->end_mc_offset) {
J. German Riverabbf9d172015-03-05 19:29:10 -0600361 *phys_addr = range->start_phys_addr +
J. German Rivera1ee695f2015-09-23 16:11:03 -0500362 (mc_offset - range->start_mc_offset);
J. German Riverabbf9d172015-03-05 19:29:10 -0600363 return 0;
364 }
365 }
366
367 return -EFAULT;
368}
369
370static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
371 struct fsl_mc_device *mc_bus_dev)
372{
373 int i;
374 int error;
375 struct resource *regions;
376 struct dprc_obj_desc *obj_desc = &mc_dev->obj_desc;
377 struct device *parent_dev = mc_dev->dev.parent;
J. German Rivera1ee695f2015-09-23 16:11:03 -0500378 enum dprc_region_type mc_region_type;
379
380 if (strcmp(obj_desc->type, "dprc") == 0 ||
381 strcmp(obj_desc->type, "dpmcp") == 0) {
382 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
383 } else if (strcmp(obj_desc->type, "dpio") == 0) {
384 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
385 } else {
386 /*
387 * This function should not have been called for this MC object
388 * type, as this object type is not supposed to have MMIO
389 * regions
390 */
391 WARN_ON(true);
392 return -EINVAL;
393 }
J. German Riverabbf9d172015-03-05 19:29:10 -0600394
395 regions = kmalloc_array(obj_desc->region_count,
396 sizeof(regions[0]), GFP_KERNEL);
397 if (!regions)
398 return -ENOMEM;
399
400 for (i = 0; i < obj_desc->region_count; i++) {
401 struct dprc_region_desc region_desc;
402
403 error = dprc_get_obj_region(mc_bus_dev->mc_io,
J. German Rivera1ee695f2015-09-23 16:11:03 -0500404 0,
J. German Riverabbf9d172015-03-05 19:29:10 -0600405 mc_bus_dev->mc_handle,
406 obj_desc->type,
407 obj_desc->id, i, &region_desc);
408 if (error < 0) {
409 dev_err(parent_dev,
410 "dprc_get_obj_region() failed: %d\n", error);
411 goto error_cleanup_regions;
412 }
413
J. German Riverabbf9d172015-03-05 19:29:10 -0600414 WARN_ON(region_desc.size == 0);
Itai Katze4ea40f2015-10-04 10:09:52 +0300415 error = translate_mc_addr(mc_dev, mc_region_type,
J. German Rivera1ee695f2015-09-23 16:11:03 -0500416 region_desc.base_offset,
J. German Riverabbf9d172015-03-05 19:29:10 -0600417 &regions[i].start);
418 if (error < 0) {
419 dev_err(parent_dev,
J. German Rivera1ee695f2015-09-23 16:11:03 -0500420 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
421 region_desc.base_offset,
422 obj_desc->type, obj_desc->id, i);
J. German Riverabbf9d172015-03-05 19:29:10 -0600423 goto error_cleanup_regions;
424 }
425
426 regions[i].end = regions[i].start + region_desc.size - 1;
427 regions[i].name = "fsl-mc object MMIO region";
428 regions[i].flags = IORESOURCE_IO;
Itai Katzb3721fc2016-04-11 11:55:48 -0500429 if (region_desc.flags & DPRC_REGION_CACHEABLE)
430 regions[i].flags |= IORESOURCE_CACHEABLE;
J. German Riverabbf9d172015-03-05 19:29:10 -0600431 }
432
433 mc_dev->regions = regions;
434 return 0;
435
436error_cleanup_regions:
437 kfree(regions);
438 return error;
439}
440
441/**
Stuart Yoderfde867d2016-06-22 16:40:47 -0500442 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
443 */
444bool fsl_mc_is_root_dprc(struct device *dev)
445{
446 struct device *root_dprc_dev;
447
448 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
449 if (!root_dprc_dev)
450 return false;
451 return dev == root_dprc_dev;
452}
453
454/**
J. German Riverabbf9d172015-03-05 19:29:10 -0600455 * Add a newly discovered MC object device to be visible in Linux
456 */
457int fsl_mc_device_add(struct dprc_obj_desc *obj_desc,
458 struct fsl_mc_io *mc_io,
459 struct device *parent_dev,
460 struct fsl_mc_device **new_mc_dev)
461{
462 int error;
463 struct fsl_mc_device *mc_dev = NULL;
464 struct fsl_mc_bus *mc_bus = NULL;
465 struct fsl_mc_device *parent_mc_dev;
466
Nipun Guptadf5e9b52016-06-29 22:44:39 +0530467 if (dev_is_fsl_mc(parent_dev))
J. German Riverabbf9d172015-03-05 19:29:10 -0600468 parent_mc_dev = to_fsl_mc_device(parent_dev);
469 else
470 parent_mc_dev = NULL;
471
472 if (strcmp(obj_desc->type, "dprc") == 0) {
473 /*
474 * Allocate an MC bus device object:
475 */
476 mc_bus = devm_kzalloc(parent_dev, sizeof(*mc_bus), GFP_KERNEL);
477 if (!mc_bus)
478 return -ENOMEM;
479
480 mc_dev = &mc_bus->mc_dev;
481 } else {
482 /*
483 * Allocate a regular fsl_mc_device object:
484 */
485 mc_dev = kmem_cache_zalloc(mc_dev_cache, GFP_KERNEL);
486 if (!mc_dev)
487 return -ENOMEM;
488 }
489
490 mc_dev->obj_desc = *obj_desc;
491 mc_dev->mc_io = mc_io;
492 device_initialize(&mc_dev->dev);
493 mc_dev->dev.parent = parent_dev;
494 mc_dev->dev.bus = &fsl_mc_bus_type;
J. German Rivera77371fb2015-03-27 16:01:04 -0500495 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
J. German Riverabbf9d172015-03-05 19:29:10 -0600496
497 if (strcmp(obj_desc->type, "dprc") == 0) {
498 struct fsl_mc_io *mc_io2;
499
500 mc_dev->flags |= FSL_MC_IS_DPRC;
501
502 /*
503 * To get the DPRC's ICID, we need to open the DPRC
504 * in get_dprc_icid(). For child DPRCs, we do so using the
505 * parent DPRC's MC portal instead of the child DPRC's MC
506 * portal, in case the child DPRC is already opened with
507 * its own portal (e.g., the DPRC used by AIOP).
508 *
509 * NOTE: There cannot be more than one active open for a
510 * given MC object, using the same MC portal.
511 */
512 if (parent_mc_dev) {
513 /*
514 * device being added is a child DPRC device
515 */
516 mc_io2 = parent_mc_dev->mc_io;
517 } else {
518 /*
519 * device being added is the root DPRC device
520 */
521 if (WARN_ON(!mc_io)) {
522 error = -EINVAL;
523 goto error_cleanup_dev;
524 }
525
526 mc_io2 = mc_io;
527
Itai Katz565b9452015-10-04 10:09:53 +0300528 atomic_inc(&root_dprc_count);
J. German Riverabbf9d172015-03-05 19:29:10 -0600529 }
530
531 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
532 if (error < 0)
533 goto error_cleanup_dev;
534 } else {
535 /*
536 * A non-DPRC MC object device has to be a child of another
537 * MC object (specifically a DPRC object)
538 */
539 mc_dev->icid = parent_mc_dev->icid;
540 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
541 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
J. German Rivera660a24b2016-01-06 16:03:29 -0600542 dev_set_msi_domain(&mc_dev->dev,
543 dev_get_msi_domain(&parent_mc_dev->dev));
J. German Riverabbf9d172015-03-05 19:29:10 -0600544 }
545
546 /*
547 * Get MMIO regions for the device from the MC:
548 *
549 * NOTE: the root DPRC is a special case as its MMIO region is
550 * obtained from the device tree
551 */
552 if (parent_mc_dev && obj_desc->region_count != 0) {
553 error = fsl_mc_device_get_mmio_regions(mc_dev,
554 parent_mc_dev);
555 if (error < 0)
556 goto error_cleanup_dev;
557 }
558
Stuart Yoder0f90f252016-04-11 11:49:13 -0500559 /* Objects are coherent, unless 'no shareability' flag set. */
560 if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY))
561 arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true);
562
J. German Riverabbf9d172015-03-05 19:29:10 -0600563 /*
564 * The device-specific probe callback will get invoked by device_add()
565 */
566 error = device_add(&mc_dev->dev);
567 if (error < 0) {
568 dev_err(parent_dev,
569 "device_add() failed for device %s: %d\n",
570 dev_name(&mc_dev->dev), error);
571 goto error_cleanup_dev;
572 }
573
574 (void)get_device(&mc_dev->dev);
575 dev_dbg(parent_dev, "Added MC object device %s\n",
576 dev_name(&mc_dev->dev));
577
578 *new_mc_dev = mc_dev;
579 return 0;
580
581error_cleanup_dev:
582 kfree(mc_dev->regions);
583 if (mc_bus)
584 devm_kfree(parent_dev, mc_bus);
585 else
586 kmem_cache_free(mc_dev_cache, mc_dev);
587
588 return error;
589}
590EXPORT_SYMBOL_GPL(fsl_mc_device_add);
591
592/**
593 * fsl_mc_device_remove - Remove a MC object device from being visible to
594 * Linux
595 *
596 * @mc_dev: Pointer to a MC object device object
597 */
598void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
599{
600 struct fsl_mc_bus *mc_bus = NULL;
601
602 kfree(mc_dev->regions);
603
604 /*
605 * The device-specific remove callback will get invoked by device_del()
606 */
607 device_del(&mc_dev->dev);
608 put_device(&mc_dev->dev);
609
610 if (strcmp(mc_dev->obj_desc.type, "dprc") == 0) {
J. German Riverabbf9d172015-03-05 19:29:10 -0600611 mc_bus = to_fsl_mc_bus(mc_dev);
J. German Rivera2bdc55d2015-03-27 16:01:07 -0500612
Itai Katz565b9452015-10-04 10:09:53 +0300613 if (fsl_mc_is_root_dprc(&mc_dev->dev)) {
Itai Katz565b9452015-10-04 10:09:53 +0300614 if (atomic_read(&root_dprc_count) > 0)
615 atomic_dec(&root_dprc_count);
616 else
617 WARN_ON(1);
618 }
J. German Riverabbf9d172015-03-05 19:29:10 -0600619 }
620
J. German Riverabbf9d172015-03-05 19:29:10 -0600621 if (mc_bus)
622 devm_kfree(mc_dev->dev.parent, mc_bus);
623 else
624 kmem_cache_free(mc_dev_cache, mc_dev);
625}
626EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
627
628static int parse_mc_ranges(struct device *dev,
629 int *paddr_cells,
630 int *mc_addr_cells,
631 int *mc_size_cells,
632 const __be32 **ranges_start,
J. German Riveraba72f252015-09-25 11:21:01 -0500633 u8 *num_ranges)
J. German Riverabbf9d172015-03-05 19:29:10 -0600634{
635 const __be32 *prop;
636 int range_tuple_cell_count;
637 int ranges_len;
638 int tuple_len;
639 struct device_node *mc_node = dev->of_node;
640
641 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
642 if (!(*ranges_start) || !ranges_len) {
643 dev_warn(dev,
644 "missing or empty ranges property for device tree node '%s'\n",
645 mc_node->name);
646
647 *num_ranges = 0;
648 return 0;
649 }
650
651 *paddr_cells = of_n_addr_cells(mc_node);
652
653 prop = of_get_property(mc_node, "#address-cells", NULL);
654 if (prop)
655 *mc_addr_cells = be32_to_cpup(prop);
656 else
657 *mc_addr_cells = *paddr_cells;
658
659 prop = of_get_property(mc_node, "#size-cells", NULL);
660 if (prop)
661 *mc_size_cells = be32_to_cpup(prop);
662 else
663 *mc_size_cells = of_n_size_cells(mc_node);
664
665 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
666 *mc_size_cells;
667
668 tuple_len = range_tuple_cell_count * sizeof(__be32);
669 if (ranges_len % tuple_len != 0) {
670 dev_err(dev, "malformed ranges property '%s'\n", mc_node->name);
671 return -EINVAL;
672 }
673
674 *num_ranges = ranges_len / tuple_len;
675 return 0;
676}
677
678static int get_mc_addr_translation_ranges(struct device *dev,
679 struct fsl_mc_addr_translation_range
680 **ranges,
J. German Riveraba72f252015-09-25 11:21:01 -0500681 u8 *num_ranges)
J. German Riverabbf9d172015-03-05 19:29:10 -0600682{
683 int error;
684 int paddr_cells;
685 int mc_addr_cells;
686 int mc_size_cells;
687 int i;
688 const __be32 *ranges_start;
689 const __be32 *cell;
690
691 error = parse_mc_ranges(dev,
692 &paddr_cells,
693 &mc_addr_cells,
694 &mc_size_cells,
695 &ranges_start,
696 num_ranges);
697 if (error < 0)
698 return error;
699
700 if (!(*num_ranges)) {
701 /*
702 * Missing or empty ranges property ("ranges;") for the
703 * 'fsl,qoriq-mc' node. In this case, identity mapping
704 * will be used.
705 */
706 *ranges = NULL;
707 return 0;
708 }
709
710 *ranges = devm_kcalloc(dev, *num_ranges,
711 sizeof(struct fsl_mc_addr_translation_range),
712 GFP_KERNEL);
713 if (!(*ranges))
714 return -ENOMEM;
715
716 cell = ranges_start;
717 for (i = 0; i < *num_ranges; ++i) {
718 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
719
J. German Rivera1ee695f2015-09-23 16:11:03 -0500720 range->mc_region_type = of_read_number(cell, 1);
721 range->start_mc_offset = of_read_number(cell + 1,
722 mc_addr_cells - 1);
J. German Riverabbf9d172015-03-05 19:29:10 -0600723 cell += mc_addr_cells;
724 range->start_phys_addr = of_read_number(cell, paddr_cells);
725 cell += paddr_cells;
J. German Rivera1ee695f2015-09-23 16:11:03 -0500726 range->end_mc_offset = range->start_mc_offset +
J. German Riverabbf9d172015-03-05 19:29:10 -0600727 of_read_number(cell, mc_size_cells);
728
729 cell += mc_size_cells;
730 }
731
732 return 0;
733}
734
735/**
736 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
737 * added
738 */
739static int fsl_mc_bus_probe(struct platform_device *pdev)
740{
741 struct dprc_obj_desc obj_desc;
742 int error;
743 struct fsl_mc *mc;
744 struct fsl_mc_device *mc_bus_dev = NULL;
745 struct fsl_mc_io *mc_io = NULL;
746 int container_id;
747 phys_addr_t mc_portal_phys_addr;
Nayeemahmed Badebadea2f9ff62015-09-04 22:30:33 +0530748 u32 mc_portal_size;
J. German Riverabbf9d172015-03-05 19:29:10 -0600749 struct mc_version mc_version;
750 struct resource res;
751
752 dev_info(&pdev->dev, "Root MC bus device probed");
753
754 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
755 if (!mc)
756 return -ENOMEM;
757
758 platform_set_drvdata(pdev, mc);
759
760 /*
761 * Get physical address of MC portal for the root DPRC:
762 */
763 error = of_address_to_resource(pdev->dev.of_node, 0, &res);
764 if (error < 0) {
765 dev_err(&pdev->dev,
766 "of_address_to_resource() failed for %s\n",
767 pdev->dev.of_node->full_name);
768 return error;
769 }
770
771 mc_portal_phys_addr = res.start;
772 mc_portal_size = resource_size(&res);
773 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
J. German Rivera1129cde2016-01-06 16:03:24 -0600774 mc_portal_size, NULL,
775 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
J. German Riverabbf9d172015-03-05 19:29:10 -0600776 if (error < 0)
777 return error;
778
J. German Rivera40577432015-09-23 16:10:59 -0500779 error = mc_get_version(mc_io, 0, &mc_version);
J. German Riverabbf9d172015-03-05 19:29:10 -0600780 if (error != 0) {
781 dev_err(&pdev->dev,
782 "mc_get_version() failed with error %d\n", error);
783 goto error_cleanup_mc_io;
784 }
785
786 dev_info(&pdev->dev,
787 "Freescale Management Complex Firmware version: %u.%u.%u\n",
788 mc_version.major, mc_version.minor, mc_version.revision);
789
J. German Riverabbf9d172015-03-05 19:29:10 -0600790 error = get_mc_addr_translation_ranges(&pdev->dev,
791 &mc->translation_ranges,
792 &mc->num_translation_ranges);
793 if (error < 0)
794 goto error_cleanup_mc_io;
795
J. German Rivera40577432015-09-23 16:10:59 -0500796 error = dpmng_get_container_id(mc_io, 0, &container_id);
J. German Riverabbf9d172015-03-05 19:29:10 -0600797 if (error < 0) {
798 dev_err(&pdev->dev,
799 "dpmng_get_container_id() failed: %d\n", error);
800 goto error_cleanup_mc_io;
801 }
802
Itai Katz1716cb42016-04-11 11:56:05 -0500803 memset(&obj_desc, 0, sizeof(struct dprc_obj_desc));
Itai Katz9529d162016-04-11 11:55:55 -0500804 error = get_dprc_version(mc_io, container_id,
805 &obj_desc.ver_major, &obj_desc.ver_minor);
806 if (error < 0)
807 goto error_cleanup_mc_io;
808
J. German Riverabbf9d172015-03-05 19:29:10 -0600809 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
810 strcpy(obj_desc.type, "dprc");
811 obj_desc.id = container_id;
J. German Riveraa17f4aa2015-10-17 11:18:23 -0500812 obj_desc.irq_count = 1;
J. German Riverabbf9d172015-03-05 19:29:10 -0600813 obj_desc.region_count = 0;
814
815 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
816 if (error < 0)
817 goto error_cleanup_mc_io;
818
819 mc->root_mc_bus_dev = mc_bus_dev;
820 return 0;
821
822error_cleanup_mc_io:
823 fsl_destroy_mc_io(mc_io);
824 return error;
825}
826
827/**
828 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
829 * removed
830 */
831static int fsl_mc_bus_remove(struct platform_device *pdev)
832{
833 struct fsl_mc *mc = platform_get_drvdata(pdev);
834
Itai Katzb55f00c2015-10-04 10:09:51 +0300835 if (WARN_ON(!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev)))
J. German Riverabbf9d172015-03-05 19:29:10 -0600836 return -EINVAL;
837
838 fsl_mc_device_remove(mc->root_mc_bus_dev);
Bharat Bhushanf9362712016-06-22 16:40:48 -0500839
840 fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
841 mc->root_mc_bus_dev->mc_io = NULL;
842
J. German Riverabbf9d172015-03-05 19:29:10 -0600843 dev_info(&pdev->dev, "Root MC bus device removed");
844 return 0;
845}
846
847static const struct of_device_id fsl_mc_bus_match_table[] = {
848 {.compatible = "fsl,qoriq-mc",},
849 {},
850};
851
852MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
853
854static struct platform_driver fsl_mc_bus_driver = {
855 .driver = {
856 .name = "fsl_mc_bus",
J. German Riverabbf9d172015-03-05 19:29:10 -0600857 .pm = NULL,
858 .of_match_table = fsl_mc_bus_match_table,
859 },
860 .probe = fsl_mc_bus_probe,
861 .remove = fsl_mc_bus_remove,
862};
863
864static int __init fsl_mc_bus_driver_init(void)
865{
866 int error;
867
868 mc_dev_cache = kmem_cache_create("fsl_mc_device",
869 sizeof(struct fsl_mc_device), 0, 0,
870 NULL);
871 if (!mc_dev_cache) {
872 pr_err("Could not create fsl_mc_device cache\n");
873 return -ENOMEM;
874 }
875
876 error = bus_register(&fsl_mc_bus_type);
877 if (error < 0) {
878 pr_err("fsl-mc bus type registration failed: %d\n", error);
879 goto error_cleanup_cache;
880 }
881
882 pr_info("fsl-mc bus type registered\n");
883
884 error = platform_driver_register(&fsl_mc_bus_driver);
885 if (error < 0) {
886 pr_err("platform_driver_register() failed: %d\n", error);
887 goto error_cleanup_bus;
888 }
889
J. German Riveraf2f27262015-03-05 19:29:11 -0600890 error = dprc_driver_init();
891 if (error < 0)
892 goto error_cleanup_driver;
893
J. German Riverae91ffa92015-03-27 16:01:08 -0500894 error = fsl_mc_allocator_driver_init();
895 if (error < 0)
896 goto error_cleanup_dprc_driver;
897
J. German Rivera660a24b2016-01-06 16:03:29 -0600898 error = its_fsl_mc_msi_init();
899 if (error < 0)
900 goto error_cleanup_mc_allocator;
901
J. German Riverabbf9d172015-03-05 19:29:10 -0600902 return 0;
903
J. German Rivera660a24b2016-01-06 16:03:29 -0600904error_cleanup_mc_allocator:
905 fsl_mc_allocator_driver_exit();
906
J. German Riverae91ffa92015-03-27 16:01:08 -0500907error_cleanup_dprc_driver:
908 dprc_driver_exit();
909
J. German Riveraf2f27262015-03-05 19:29:11 -0600910error_cleanup_driver:
911 platform_driver_unregister(&fsl_mc_bus_driver);
912
J. German Riverabbf9d172015-03-05 19:29:10 -0600913error_cleanup_bus:
914 bus_unregister(&fsl_mc_bus_type);
915
916error_cleanup_cache:
917 kmem_cache_destroy(mc_dev_cache);
918 return error;
919}
J. German Riverabbf9d172015-03-05 19:29:10 -0600920postcore_initcall(fsl_mc_bus_driver_init);