Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/bus/fsl-mc/fsl-mc-bus.c
51948 views
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
* Freescale Management Complex (MC) bus driver
4
*
5
* Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
6
* Copyright 2019-2020 NXP
7
* Author: German Rivera <[email protected]>
8
*
9
*/
10
11
#define pr_fmt(fmt) "fsl-mc: " fmt
12
13
#include <linux/module.h>
14
#include <linux/of_device.h>
15
#include <linux/of_address.h>
16
#include <linux/ioport.h>
17
#include <linux/platform_device.h>
18
#include <linux/slab.h>
19
#include <linux/limits.h>
20
#include <linux/bitops.h>
21
#include <linux/dma-mapping.h>
22
#include <linux/acpi.h>
23
#include <linux/iommu.h>
24
#include <linux/dma-map-ops.h>
25
26
#include "fsl-mc-private.h"
27
28
/*
29
* Default DMA mask for devices on a fsl-mc bus
30
*/
31
#define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
32
33
static struct fsl_mc_version mc_version;
34
35
/**
36
* struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
37
* @root_mc_bus_dev: fsl-mc 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
* @fsl_mc_regs: base address of register bank
41
*/
42
struct fsl_mc {
43
struct fsl_mc_device *root_mc_bus_dev;
44
u8 num_translation_ranges;
45
struct fsl_mc_addr_translation_range *translation_ranges;
46
void __iomem *fsl_mc_regs;
47
};
48
49
/**
50
* struct fsl_mc_addr_translation_range - bus to system address translation
51
* range
52
* @mc_region_type: Type of MC region for the range being translated
53
* @start_mc_offset: Start MC offset of the range being translated
54
* @end_mc_offset: MC offset of the first byte after the range (last MC
55
* offset of the range is end_mc_offset - 1)
56
* @start_phys_addr: system physical address corresponding to start_mc_addr
57
*/
58
struct fsl_mc_addr_translation_range {
59
enum dprc_region_type mc_region_type;
60
u64 start_mc_offset;
61
u64 end_mc_offset;
62
phys_addr_t start_phys_addr;
63
};
64
65
#define FSL_MC_GCR1 0x0
66
#define GCR1_P1_STOP BIT(31)
67
#define GCR1_P2_STOP BIT(30)
68
69
#define FSL_MC_FAPR 0x28
70
#define MC_FAPR_PL BIT(18)
71
#define MC_FAPR_BMT BIT(17)
72
73
static phys_addr_t mc_portal_base_phys_addr;
74
75
/**
76
* fsl_mc_bus_match - device to driver matching callback
77
* @dev: the fsl-mc device to match against
78
* @drv: the device driver to search for matching fsl-mc object type
79
* structures
80
*
81
* Returns 1 on success, 0 otherwise.
82
*/
83
static int fsl_mc_bus_match(struct device *dev, const struct device_driver *drv)
84
{
85
const struct fsl_mc_device_id *id;
86
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
87
const struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
88
bool found = false;
89
90
/* When driver_override is set, only bind to the matching driver */
91
if (mc_dev->driver_override) {
92
found = !strcmp(mc_dev->driver_override, mc_drv->driver.name);
93
goto out;
94
}
95
96
if (!mc_drv->match_id_table)
97
goto out;
98
99
/*
100
* If the object is not 'plugged' don't match.
101
* Only exception is the root DPRC, which is a special case.
102
*/
103
if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
104
!fsl_mc_is_root_dprc(&mc_dev->dev))
105
goto out;
106
107
/*
108
* Traverse the match_id table of the given driver, trying to find
109
* a matching for the given device.
110
*/
111
for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
112
if (id->vendor == mc_dev->obj_desc.vendor &&
113
strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
114
found = true;
115
116
break;
117
}
118
}
119
120
out:
121
dev_dbg(dev, "%smatched\n", found ? "" : "not ");
122
return found;
123
}
124
125
/*
126
* fsl_mc_bus_uevent - callback invoked when a device is added
127
*/
128
static int fsl_mc_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
129
{
130
const struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
131
132
if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
133
mc_dev->obj_desc.vendor,
134
mc_dev->obj_desc.type))
135
return -ENOMEM;
136
137
return 0;
138
}
139
140
static int fsl_mc_probe(struct device *dev)
141
{
142
struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
143
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
144
145
if (mc_drv->probe)
146
return mc_drv->probe(mc_dev);
147
148
return 0;
149
}
150
151
static void fsl_mc_remove(struct device *dev)
152
{
153
struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
154
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
155
156
if (mc_drv->remove)
157
mc_drv->remove(mc_dev);
158
}
159
160
static void fsl_mc_shutdown(struct device *dev)
161
{
162
struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
163
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
164
165
if (dev->driver && mc_drv->shutdown)
166
mc_drv->shutdown(mc_dev);
167
}
168
169
static int fsl_mc_dma_configure(struct device *dev)
170
{
171
const struct device_driver *drv = READ_ONCE(dev->driver);
172
struct device *dma_dev = dev;
173
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
174
u32 input_id = mc_dev->icid;
175
int ret;
176
177
while (dev_is_fsl_mc(dma_dev))
178
dma_dev = dma_dev->parent;
179
180
if (dev_of_node(dma_dev))
181
ret = of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
182
else
183
ret = acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
184
185
/* @drv may not be valid when we're called from the IOMMU layer */
186
if (!ret && drv && !to_fsl_mc_driver(drv)->driver_managed_dma) {
187
ret = iommu_device_use_default_domain(dev);
188
if (ret)
189
arch_teardown_dma_ops(dev);
190
}
191
192
return ret;
193
}
194
195
static void fsl_mc_dma_cleanup(struct device *dev)
196
{
197
struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
198
199
if (!mc_drv->driver_managed_dma)
200
iommu_device_unuse_default_domain(dev);
201
}
202
203
static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
204
char *buf)
205
{
206
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
207
208
return sysfs_emit(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
209
mc_dev->obj_desc.type);
210
}
211
static DEVICE_ATTR_RO(modalias);
212
213
static ssize_t driver_override_store(struct device *dev,
214
struct device_attribute *attr,
215
const char *buf, size_t count)
216
{
217
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
218
int ret;
219
220
if (WARN_ON(dev->bus != &fsl_mc_bus_type))
221
return -EINVAL;
222
223
ret = driver_set_override(dev, &mc_dev->driver_override, buf, count);
224
if (ret)
225
return ret;
226
227
return count;
228
}
229
230
static ssize_t driver_override_show(struct device *dev,
231
struct device_attribute *attr, char *buf)
232
{
233
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
234
ssize_t len;
235
236
device_lock(dev);
237
len = sysfs_emit(buf, "%s\n", mc_dev->driver_override);
238
device_unlock(dev);
239
return len;
240
}
241
static DEVICE_ATTR_RW(driver_override);
242
243
static struct attribute *fsl_mc_dev_attrs[] = {
244
&dev_attr_modalias.attr,
245
&dev_attr_driver_override.attr,
246
NULL,
247
};
248
249
ATTRIBUTE_GROUPS(fsl_mc_dev);
250
251
static int scan_fsl_mc_bus(struct device *dev, void *data)
252
{
253
struct fsl_mc_device *root_mc_dev;
254
struct fsl_mc_bus *root_mc_bus;
255
256
if (!fsl_mc_is_root_dprc(dev))
257
goto exit;
258
259
root_mc_dev = to_fsl_mc_device(dev);
260
root_mc_bus = to_fsl_mc_bus(root_mc_dev);
261
mutex_lock(&root_mc_bus->scan_mutex);
262
dprc_scan_objects(root_mc_dev, false);
263
mutex_unlock(&root_mc_bus->scan_mutex);
264
265
exit:
266
return 0;
267
}
268
269
static ssize_t rescan_store(const struct bus_type *bus,
270
const char *buf, size_t count)
271
{
272
unsigned long val;
273
274
if (kstrtoul(buf, 0, &val) < 0)
275
return -EINVAL;
276
277
if (val)
278
bus_for_each_dev(bus, NULL, NULL, scan_fsl_mc_bus);
279
280
return count;
281
}
282
static BUS_ATTR_WO(rescan);
283
284
static int fsl_mc_bus_set_autorescan(struct device *dev, void *data)
285
{
286
struct fsl_mc_device *root_mc_dev;
287
unsigned long val;
288
char *buf = data;
289
290
if (!fsl_mc_is_root_dprc(dev))
291
goto exit;
292
293
root_mc_dev = to_fsl_mc_device(dev);
294
295
if (kstrtoul(buf, 0, &val) < 0)
296
return -EINVAL;
297
298
if (val)
299
enable_dprc_irq(root_mc_dev);
300
else
301
disable_dprc_irq(root_mc_dev);
302
303
exit:
304
return 0;
305
}
306
307
static int fsl_mc_bus_get_autorescan(struct device *dev, void *data)
308
{
309
struct fsl_mc_device *root_mc_dev;
310
char *buf = data;
311
312
if (!fsl_mc_is_root_dprc(dev))
313
goto exit;
314
315
root_mc_dev = to_fsl_mc_device(dev);
316
317
sprintf(buf, "%d\n", get_dprc_irq_state(root_mc_dev));
318
exit:
319
return 0;
320
}
321
322
static ssize_t autorescan_store(const struct bus_type *bus,
323
const char *buf, size_t count)
324
{
325
bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_set_autorescan);
326
327
return count;
328
}
329
330
static ssize_t autorescan_show(const struct bus_type *bus, char *buf)
331
{
332
bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_get_autorescan);
333
return strlen(buf);
334
}
335
336
static BUS_ATTR_RW(autorescan);
337
338
static struct attribute *fsl_mc_bus_attrs[] = {
339
&bus_attr_rescan.attr,
340
&bus_attr_autorescan.attr,
341
NULL,
342
};
343
344
ATTRIBUTE_GROUPS(fsl_mc_bus);
345
346
const struct bus_type fsl_mc_bus_type = {
347
.name = "fsl-mc",
348
.match = fsl_mc_bus_match,
349
.uevent = fsl_mc_bus_uevent,
350
.probe = fsl_mc_probe,
351
.remove = fsl_mc_remove,
352
.shutdown = fsl_mc_shutdown,
353
.dma_configure = fsl_mc_dma_configure,
354
.dma_cleanup = fsl_mc_dma_cleanup,
355
.dev_groups = fsl_mc_dev_groups,
356
.bus_groups = fsl_mc_bus_groups,
357
};
358
EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
359
360
const struct device_type fsl_mc_bus_dprc_type = {
361
.name = "fsl_mc_bus_dprc"
362
};
363
EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
364
365
const struct device_type fsl_mc_bus_dpni_type = {
366
.name = "fsl_mc_bus_dpni"
367
};
368
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
369
370
const struct device_type fsl_mc_bus_dpio_type = {
371
.name = "fsl_mc_bus_dpio"
372
};
373
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
374
375
const struct device_type fsl_mc_bus_dpsw_type = {
376
.name = "fsl_mc_bus_dpsw"
377
};
378
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
379
380
const struct device_type fsl_mc_bus_dpbp_type = {
381
.name = "fsl_mc_bus_dpbp"
382
};
383
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
384
385
const struct device_type fsl_mc_bus_dpcon_type = {
386
.name = "fsl_mc_bus_dpcon"
387
};
388
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
389
390
const struct device_type fsl_mc_bus_dpmcp_type = {
391
.name = "fsl_mc_bus_dpmcp"
392
};
393
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
394
395
const struct device_type fsl_mc_bus_dpmac_type = {
396
.name = "fsl_mc_bus_dpmac"
397
};
398
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
399
400
const struct device_type fsl_mc_bus_dprtc_type = {
401
.name = "fsl_mc_bus_dprtc"
402
};
403
EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
404
405
const struct device_type fsl_mc_bus_dpseci_type = {
406
.name = "fsl_mc_bus_dpseci"
407
};
408
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
409
410
const struct device_type fsl_mc_bus_dpdmux_type = {
411
.name = "fsl_mc_bus_dpdmux"
412
};
413
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
414
415
const struct device_type fsl_mc_bus_dpdcei_type = {
416
.name = "fsl_mc_bus_dpdcei"
417
};
418
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
419
420
const struct device_type fsl_mc_bus_dpaiop_type = {
421
.name = "fsl_mc_bus_dpaiop"
422
};
423
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
424
425
const struct device_type fsl_mc_bus_dpci_type = {
426
.name = "fsl_mc_bus_dpci"
427
};
428
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
429
430
const struct device_type fsl_mc_bus_dpdmai_type = {
431
.name = "fsl_mc_bus_dpdmai"
432
};
433
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
434
435
const struct device_type fsl_mc_bus_dpdbg_type = {
436
.name = "fsl_mc_bus_dpdbg"
437
};
438
EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdbg_type);
439
440
static const struct device_type *fsl_mc_get_device_type(const char *type)
441
{
442
static const struct {
443
const struct device_type *dev_type;
444
const char *type;
445
} dev_types[] = {
446
{ &fsl_mc_bus_dprc_type, "dprc" },
447
{ &fsl_mc_bus_dpni_type, "dpni" },
448
{ &fsl_mc_bus_dpio_type, "dpio" },
449
{ &fsl_mc_bus_dpsw_type, "dpsw" },
450
{ &fsl_mc_bus_dpbp_type, "dpbp" },
451
{ &fsl_mc_bus_dpcon_type, "dpcon" },
452
{ &fsl_mc_bus_dpmcp_type, "dpmcp" },
453
{ &fsl_mc_bus_dpmac_type, "dpmac" },
454
{ &fsl_mc_bus_dprtc_type, "dprtc" },
455
{ &fsl_mc_bus_dpseci_type, "dpseci" },
456
{ &fsl_mc_bus_dpdmux_type, "dpdmux" },
457
{ &fsl_mc_bus_dpdcei_type, "dpdcei" },
458
{ &fsl_mc_bus_dpaiop_type, "dpaiop" },
459
{ &fsl_mc_bus_dpci_type, "dpci" },
460
{ &fsl_mc_bus_dpdmai_type, "dpdmai" },
461
{ &fsl_mc_bus_dpdbg_type, "dpdbg" },
462
{ NULL, NULL }
463
};
464
int i;
465
466
for (i = 0; dev_types[i].dev_type; i++)
467
if (!strcmp(dev_types[i].type, type))
468
return dev_types[i].dev_type;
469
470
return NULL;
471
}
472
473
/*
474
* __fsl_mc_driver_register - registers a child device driver with the
475
* MC bus
476
*
477
* This function is implicitly invoked from the registration function of
478
* fsl_mc device drivers, which is generated by the
479
* module_fsl_mc_driver() macro.
480
*/
481
int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
482
struct module *owner)
483
{
484
int error;
485
486
mc_driver->driver.owner = owner;
487
mc_driver->driver.bus = &fsl_mc_bus_type;
488
489
error = driver_register(&mc_driver->driver);
490
if (error < 0) {
491
pr_err("driver_register() failed for %s: %d\n",
492
mc_driver->driver.name, error);
493
return error;
494
}
495
496
return 0;
497
}
498
EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
499
500
/*
501
* fsl_mc_driver_unregister - unregisters a device driver from the
502
* MC bus
503
*/
504
void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
505
{
506
driver_unregister(&mc_driver->driver);
507
}
508
EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
509
510
/**
511
* mc_get_version() - Retrieves the Management Complex firmware
512
* version information
513
* @mc_io: Pointer to opaque I/O object
514
* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
515
* @mc_ver_info: Returned version information structure
516
*
517
* Return: '0' on Success; Error code otherwise.
518
*/
519
static int mc_get_version(struct fsl_mc_io *mc_io,
520
u32 cmd_flags,
521
struct fsl_mc_version *mc_ver_info)
522
{
523
struct fsl_mc_command cmd = { 0 };
524
struct dpmng_rsp_get_version *rsp_params;
525
int err;
526
527
/* prepare command */
528
cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
529
cmd_flags,
530
0);
531
532
/* send command to mc*/
533
err = mc_send_command(mc_io, &cmd);
534
if (err)
535
return err;
536
537
/* retrieve response parameters */
538
rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
539
mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
540
mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
541
mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
542
543
return 0;
544
}
545
546
/**
547
* fsl_mc_get_version - function to retrieve the MC f/w version information
548
*
549
* Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
550
*/
551
struct fsl_mc_version *fsl_mc_get_version(void)
552
{
553
if (mc_version.major)
554
return &mc_version;
555
556
return NULL;
557
}
558
EXPORT_SYMBOL_GPL(fsl_mc_get_version);
559
560
/*
561
* fsl_mc_get_root_dprc - function to traverse to the root dprc
562
*/
563
void fsl_mc_get_root_dprc(struct device *dev,
564
struct device **root_dprc_dev)
565
{
566
if (!dev) {
567
*root_dprc_dev = NULL;
568
} else if (!dev_is_fsl_mc(dev)) {
569
*root_dprc_dev = NULL;
570
} else {
571
*root_dprc_dev = dev;
572
while (dev_is_fsl_mc((*root_dprc_dev)->parent))
573
*root_dprc_dev = (*root_dprc_dev)->parent;
574
}
575
}
576
577
static int get_dprc_attr(struct fsl_mc_io *mc_io,
578
int container_id, struct dprc_attributes *attr)
579
{
580
u16 dprc_handle;
581
int error;
582
583
error = dprc_open(mc_io, 0, container_id, &dprc_handle);
584
if (error < 0) {
585
dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
586
return error;
587
}
588
589
memset(attr, 0, sizeof(struct dprc_attributes));
590
error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
591
if (error < 0) {
592
dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
593
error);
594
goto common_cleanup;
595
}
596
597
error = 0;
598
599
common_cleanup:
600
(void)dprc_close(mc_io, 0, dprc_handle);
601
return error;
602
}
603
604
static int get_dprc_icid(struct fsl_mc_io *mc_io,
605
int container_id, u32 *icid)
606
{
607
struct dprc_attributes attr;
608
int error;
609
610
error = get_dprc_attr(mc_io, container_id, &attr);
611
if (error == 0)
612
*icid = attr.icid;
613
614
return error;
615
}
616
617
static int translate_mc_addr(struct fsl_mc_device *mc_dev,
618
enum dprc_region_type mc_region_type,
619
u64 mc_offset, phys_addr_t *phys_addr)
620
{
621
int i;
622
struct device *root_dprc_dev;
623
struct fsl_mc *mc;
624
625
fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
626
mc = dev_get_drvdata(root_dprc_dev->parent);
627
628
if (mc->num_translation_ranges == 0) {
629
/*
630
* Do identity mapping:
631
*/
632
*phys_addr = mc_offset;
633
return 0;
634
}
635
636
for (i = 0; i < mc->num_translation_ranges; i++) {
637
struct fsl_mc_addr_translation_range *range =
638
&mc->translation_ranges[i];
639
640
if (mc_region_type == range->mc_region_type &&
641
mc_offset >= range->start_mc_offset &&
642
mc_offset < range->end_mc_offset) {
643
*phys_addr = range->start_phys_addr +
644
(mc_offset - range->start_mc_offset);
645
return 0;
646
}
647
}
648
649
return -EFAULT;
650
}
651
652
static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
653
struct fsl_mc_device *mc_bus_dev)
654
{
655
int i;
656
int error;
657
struct resource *regions;
658
struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
659
struct device *parent_dev = mc_dev->dev.parent;
660
enum dprc_region_type mc_region_type;
661
662
if (is_fsl_mc_bus_dprc(mc_dev) ||
663
is_fsl_mc_bus_dpmcp(mc_dev)) {
664
mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
665
} else if (is_fsl_mc_bus_dpio(mc_dev)) {
666
mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
667
} else {
668
/*
669
* This function should not have been called for this MC object
670
* type, as this object type is not supposed to have MMIO
671
* regions
672
*/
673
return -EINVAL;
674
}
675
676
regions = kmalloc_array(obj_desc->region_count,
677
sizeof(regions[0]), GFP_KERNEL);
678
if (!regions)
679
return -ENOMEM;
680
681
for (i = 0; i < obj_desc->region_count; i++) {
682
struct dprc_region_desc region_desc;
683
684
error = dprc_get_obj_region(mc_bus_dev->mc_io,
685
0,
686
mc_bus_dev->mc_handle,
687
obj_desc->type,
688
obj_desc->id, i, &region_desc);
689
if (error < 0) {
690
dev_err(parent_dev,
691
"dprc_get_obj_region() failed: %d\n", error);
692
goto error_cleanup_regions;
693
}
694
/*
695
* Older MC only returned region offset and no base address
696
* If base address is in the region_desc use it otherwise
697
* revert to old mechanism
698
*/
699
if (region_desc.base_address) {
700
regions[i].start = region_desc.base_address +
701
region_desc.base_offset;
702
} else {
703
error = translate_mc_addr(mc_dev, mc_region_type,
704
region_desc.base_offset,
705
&regions[i].start);
706
707
/*
708
* Some versions of the MC firmware wrongly report
709
* 0 for register base address of the DPMCP associated
710
* with child DPRC objects thus rendering them unusable.
711
* This is particularly troublesome in ACPI boot
712
* scenarios where the legacy way of extracting this
713
* base address from the device tree does not apply.
714
* Given that DPMCPs share the same base address,
715
* workaround this by using the base address extracted
716
* from the root DPRC container.
717
*/
718
if (is_fsl_mc_bus_dprc(mc_dev) &&
719
regions[i].start == region_desc.base_offset)
720
regions[i].start += mc_portal_base_phys_addr;
721
}
722
723
if (error < 0) {
724
dev_err(parent_dev,
725
"Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
726
region_desc.base_offset,
727
obj_desc->type, obj_desc->id, i);
728
goto error_cleanup_regions;
729
}
730
731
regions[i].end = regions[i].start + region_desc.size - 1;
732
regions[i].name = "fsl-mc object MMIO region";
733
regions[i].flags = region_desc.flags & IORESOURCE_BITS;
734
regions[i].flags |= IORESOURCE_MEM;
735
}
736
737
mc_dev->regions = regions;
738
return 0;
739
740
error_cleanup_regions:
741
kfree(regions);
742
return error;
743
}
744
745
/*
746
* fsl_mc_is_root_dprc - function to check if a given device is a root dprc
747
*/
748
bool fsl_mc_is_root_dprc(struct device *dev)
749
{
750
struct device *root_dprc_dev;
751
752
fsl_mc_get_root_dprc(dev, &root_dprc_dev);
753
if (!root_dprc_dev)
754
return false;
755
return dev == root_dprc_dev;
756
}
757
758
static void fsl_mc_device_release(struct device *dev)
759
{
760
struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
761
762
kfree(mc_dev->regions);
763
764
if (is_fsl_mc_bus_dprc(mc_dev))
765
kfree(to_fsl_mc_bus(mc_dev));
766
else
767
kfree(mc_dev);
768
}
769
770
/*
771
* Add a newly discovered fsl-mc device to be visible in Linux
772
*/
773
int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
774
struct fsl_mc_io *mc_io,
775
struct device *parent_dev,
776
struct fsl_mc_device **new_mc_dev)
777
{
778
int error;
779
struct fsl_mc_device *mc_dev = NULL;
780
struct fsl_mc_bus *mc_bus = NULL;
781
struct fsl_mc_device *parent_mc_dev;
782
783
if (dev_is_fsl_mc(parent_dev))
784
parent_mc_dev = to_fsl_mc_device(parent_dev);
785
else
786
parent_mc_dev = NULL;
787
788
if (strcmp(obj_desc->type, "dprc") == 0) {
789
/*
790
* Allocate an MC bus device object:
791
*/
792
mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
793
if (!mc_bus)
794
return -ENOMEM;
795
796
mutex_init(&mc_bus->scan_mutex);
797
mc_dev = &mc_bus->mc_dev;
798
} else {
799
/*
800
* Allocate a regular fsl_mc_device object:
801
*/
802
mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
803
if (!mc_dev)
804
return -ENOMEM;
805
}
806
807
mc_dev->obj_desc = *obj_desc;
808
mc_dev->mc_io = mc_io;
809
device_initialize(&mc_dev->dev);
810
mc_dev->dev.parent = parent_dev;
811
mc_dev->dev.bus = &fsl_mc_bus_type;
812
mc_dev->dev.release = fsl_mc_device_release;
813
mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
814
if (!mc_dev->dev.type) {
815
error = -ENODEV;
816
dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
817
goto error_cleanup_dev;
818
}
819
dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
820
821
if (strcmp(obj_desc->type, "dprc") == 0) {
822
struct fsl_mc_io *mc_io2;
823
824
mc_dev->flags |= FSL_MC_IS_DPRC;
825
826
/*
827
* To get the DPRC's ICID, we need to open the DPRC
828
* in get_dprc_icid(). For child DPRCs, we do so using the
829
* parent DPRC's MC portal instead of the child DPRC's MC
830
* portal, in case the child DPRC is already opened with
831
* its own portal (e.g., the DPRC used by AIOP).
832
*
833
* NOTE: There cannot be more than one active open for a
834
* given MC object, using the same MC portal.
835
*/
836
if (parent_mc_dev) {
837
/*
838
* device being added is a child DPRC device
839
*/
840
mc_io2 = parent_mc_dev->mc_io;
841
} else {
842
/*
843
* device being added is the root DPRC device
844
*/
845
if (!mc_io) {
846
error = -EINVAL;
847
goto error_cleanup_dev;
848
}
849
850
mc_io2 = mc_io;
851
}
852
853
error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
854
if (error < 0)
855
goto error_cleanup_dev;
856
} else {
857
/*
858
* A non-DPRC object has to be a child of a DPRC, use the
859
* parent's ICID and interrupt domain.
860
*/
861
mc_dev->icid = parent_mc_dev->icid;
862
mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
863
mc_dev->dev.dma_mask = &mc_dev->dma_mask;
864
mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
865
dev_set_msi_domain(&mc_dev->dev,
866
dev_get_msi_domain(&parent_mc_dev->dev));
867
}
868
869
/*
870
* Get MMIO regions for the device from the MC:
871
*
872
* NOTE: the root DPRC is a special case as its MMIO region is
873
* obtained from the device tree
874
*/
875
if (parent_mc_dev && obj_desc->region_count != 0) {
876
error = fsl_mc_device_get_mmio_regions(mc_dev,
877
parent_mc_dev);
878
if (error < 0)
879
goto error_cleanup_dev;
880
}
881
882
/*
883
* The device-specific probe callback will get invoked by device_add()
884
*/
885
error = device_add(&mc_dev->dev);
886
if (error < 0) {
887
dev_err(parent_dev,
888
"device_add() failed for device %s: %d\n",
889
dev_name(&mc_dev->dev), error);
890
goto error_cleanup_dev;
891
}
892
893
dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
894
895
*new_mc_dev = mc_dev;
896
return 0;
897
898
error_cleanup_dev:
899
put_device(&mc_dev->dev);
900
901
return error;
902
}
903
EXPORT_SYMBOL_GPL(fsl_mc_device_add);
904
905
static struct notifier_block fsl_mc_nb;
906
907
/**
908
* fsl_mc_device_remove - Remove an fsl-mc device from being visible to
909
* Linux
910
*
911
* @mc_dev: Pointer to an fsl-mc device
912
*/
913
void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
914
{
915
kfree(mc_dev->driver_override);
916
mc_dev->driver_override = NULL;
917
918
/*
919
* The device-specific remove callback will get invoked by device_del()
920
*/
921
device_del(&mc_dev->dev);
922
put_device(&mc_dev->dev);
923
}
924
EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
925
926
struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,
927
u16 if_id)
928
{
929
struct fsl_mc_device *mc_bus_dev, *endpoint;
930
struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
931
struct dprc_endpoint endpoint1 = {{ 0 }};
932
struct dprc_endpoint endpoint2 = {{ 0 }};
933
struct fsl_mc_bus *mc_bus;
934
int state, err;
935
936
mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
937
strcpy(endpoint1.type, mc_dev->obj_desc.type);
938
endpoint1.id = mc_dev->obj_desc.id;
939
endpoint1.if_id = if_id;
940
941
err = dprc_get_connection(mc_bus_dev->mc_io, 0,
942
mc_bus_dev->mc_handle,
943
&endpoint1, &endpoint2,
944
&state);
945
946
if (err == -ENOTCONN || state == -1)
947
return ERR_PTR(-ENOTCONN);
948
949
if (err < 0) {
950
dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
951
return ERR_PTR(err);
952
}
953
954
strcpy(endpoint_desc.type, endpoint2.type);
955
endpoint_desc.id = endpoint2.id;
956
endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
957
if (endpoint)
958
return endpoint;
959
960
/*
961
* We know that the device has an endpoint because we verified by
962
* interrogating the firmware. This is the case when the device was not
963
* yet discovered by the fsl-mc bus, thus the lookup returned NULL.
964
* Force a rescan of the devices in this container and retry the lookup.
965
*/
966
mc_bus = to_fsl_mc_bus(mc_bus_dev);
967
if (mutex_trylock(&mc_bus->scan_mutex)) {
968
err = dprc_scan_objects(mc_bus_dev, true);
969
mutex_unlock(&mc_bus->scan_mutex);
970
}
971
if (err < 0)
972
return ERR_PTR(err);
973
974
endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
975
/*
976
* This means that the endpoint might reside in a different isolation
977
* context (DPRC/container). Not much to do, so return a permssion
978
* error.
979
*/
980
if (!endpoint)
981
return ERR_PTR(-EPERM);
982
983
return endpoint;
984
}
985
EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
986
987
static int get_mc_addr_translation_ranges(struct device *dev,
988
struct fsl_mc_addr_translation_range
989
**ranges,
990
u8 *num_ranges)
991
{
992
struct fsl_mc_addr_translation_range *r;
993
struct of_range_parser parser;
994
struct of_range range;
995
996
of_range_parser_init(&parser, dev->of_node);
997
*num_ranges = of_range_count(&parser);
998
if (!*num_ranges) {
999
/*
1000
* Missing or empty ranges property ("ranges;") for the
1001
* 'fsl,qoriq-mc' node. In this case, identity mapping
1002
* will be used.
1003
*/
1004
*ranges = NULL;
1005
return 0;
1006
}
1007
1008
*ranges = devm_kcalloc(dev, *num_ranges,
1009
sizeof(struct fsl_mc_addr_translation_range),
1010
GFP_KERNEL);
1011
if (!(*ranges))
1012
return -ENOMEM;
1013
1014
r = *ranges;
1015
for_each_of_range(&parser, &range) {
1016
r->mc_region_type = range.flags;
1017
r->start_mc_offset = range.bus_addr;
1018
r->end_mc_offset = range.bus_addr + range.size;
1019
r->start_phys_addr = range.cpu_addr;
1020
r++;
1021
}
1022
1023
return 0;
1024
}
1025
1026
/*
1027
* fsl_mc_bus_probe - callback invoked when the root MC bus is being
1028
* added
1029
*/
1030
static int fsl_mc_bus_probe(struct platform_device *pdev)
1031
{
1032
struct fsl_mc_obj_desc obj_desc;
1033
int error;
1034
struct fsl_mc *mc;
1035
struct fsl_mc_device *mc_bus_dev = NULL;
1036
struct fsl_mc_io *mc_io = NULL;
1037
int container_id;
1038
phys_addr_t mc_portal_phys_addr;
1039
u32 mc_portal_size, mc_stream_id;
1040
struct resource *plat_res;
1041
1042
mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
1043
if (!mc)
1044
return -ENOMEM;
1045
1046
platform_set_drvdata(pdev, mc);
1047
1048
plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1049
if (plat_res) {
1050
mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
1051
if (IS_ERR(mc->fsl_mc_regs))
1052
return PTR_ERR(mc->fsl_mc_regs);
1053
}
1054
1055
if (mc->fsl_mc_regs) {
1056
if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
1057
mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
1058
/*
1059
* HW ORs the PL and BMT bit, places the result in bit
1060
* 14 of the StreamID and ORs in the ICID. Calculate it
1061
* accordingly.
1062
*/
1063
mc_stream_id = (mc_stream_id & 0xffff) |
1064
((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
1065
BIT(14) : 0);
1066
error = acpi_dma_configure_id(&pdev->dev,
1067
DEV_DMA_COHERENT,
1068
&mc_stream_id);
1069
if (error == -EPROBE_DEFER)
1070
return error;
1071
if (error)
1072
dev_warn(&pdev->dev,
1073
"failed to configure dma: %d.\n",
1074
error);
1075
}
1076
1077
/*
1078
* Some bootloaders pause the MC firmware before booting the
1079
* kernel so that MC will not cause faults as soon as the
1080
* SMMU probes due to the fact that there's no configuration
1081
* in place for MC.
1082
* At this point MC should have all its SMMU setup done so make
1083
* sure it is resumed.
1084
*/
1085
writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) &
1086
(~(GCR1_P1_STOP | GCR1_P2_STOP)),
1087
mc->fsl_mc_regs + FSL_MC_GCR1);
1088
}
1089
1090
/*
1091
* Get physical address of MC portal for the root DPRC:
1092
*/
1093
plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1094
if (!plat_res)
1095
return -EINVAL;
1096
1097
mc_portal_phys_addr = plat_res->start;
1098
mc_portal_size = resource_size(plat_res);
1099
mc_portal_base_phys_addr = mc_portal_phys_addr & ~0x3ffffff;
1100
1101
error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
1102
mc_portal_size, NULL,
1103
FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
1104
if (error < 0)
1105
return error;
1106
1107
error = mc_get_version(mc_io, 0, &mc_version);
1108
if (error != 0) {
1109
dev_err(&pdev->dev,
1110
"mc_get_version() failed with error %d\n", error);
1111
goto error_cleanup_mc_io;
1112
}
1113
1114
dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
1115
mc_version.major, mc_version.minor, mc_version.revision);
1116
1117
if (dev_of_node(&pdev->dev)) {
1118
error = get_mc_addr_translation_ranges(&pdev->dev,
1119
&mc->translation_ranges,
1120
&mc->num_translation_ranges);
1121
if (error < 0)
1122
goto error_cleanup_mc_io;
1123
}
1124
1125
error = dprc_get_container_id(mc_io, 0, &container_id);
1126
if (error < 0) {
1127
dev_err(&pdev->dev,
1128
"dprc_get_container_id() failed: %d\n", error);
1129
goto error_cleanup_mc_io;
1130
}
1131
1132
memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
1133
error = dprc_get_api_version(mc_io, 0,
1134
&obj_desc.ver_major,
1135
&obj_desc.ver_minor);
1136
if (error < 0)
1137
goto error_cleanup_mc_io;
1138
1139
obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
1140
strcpy(obj_desc.type, "dprc");
1141
obj_desc.id = container_id;
1142
obj_desc.irq_count = 1;
1143
obj_desc.region_count = 0;
1144
1145
error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
1146
if (error < 0)
1147
goto error_cleanup_mc_io;
1148
1149
mc->root_mc_bus_dev = mc_bus_dev;
1150
mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
1151
return 0;
1152
1153
error_cleanup_mc_io:
1154
fsl_destroy_mc_io(mc_io);
1155
return error;
1156
}
1157
1158
/*
1159
* fsl_mc_bus_remove - callback invoked when the root MC bus is being
1160
* removed
1161
*/
1162
static void fsl_mc_bus_remove(struct platform_device *pdev)
1163
{
1164
struct fsl_mc *mc = platform_get_drvdata(pdev);
1165
struct fsl_mc_io *mc_io;
1166
1167
mc_io = mc->root_mc_bus_dev->mc_io;
1168
fsl_mc_device_remove(mc->root_mc_bus_dev);
1169
fsl_destroy_mc_io(mc_io);
1170
1171
bus_unregister_notifier(&fsl_mc_bus_type, &fsl_mc_nb);
1172
1173
if (mc->fsl_mc_regs) {
1174
/*
1175
* Pause the MC firmware so that it doesn't crash in certain
1176
* scenarios, such as kexec.
1177
*/
1178
writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) |
1179
(GCR1_P1_STOP | GCR1_P2_STOP),
1180
mc->fsl_mc_regs + FSL_MC_GCR1);
1181
}
1182
}
1183
1184
static const struct of_device_id fsl_mc_bus_match_table[] = {
1185
{.compatible = "fsl,qoriq-mc",},
1186
{},
1187
};
1188
1189
MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1190
1191
static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1192
{"NXP0008", 0 },
1193
{ }
1194
};
1195
MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1196
1197
static struct platform_driver fsl_mc_bus_driver = {
1198
.driver = {
1199
.name = "fsl_mc_bus",
1200
.pm = NULL,
1201
.of_match_table = fsl_mc_bus_match_table,
1202
.acpi_match_table = fsl_mc_bus_acpi_match_table,
1203
},
1204
.probe = fsl_mc_bus_probe,
1205
.remove = fsl_mc_bus_remove,
1206
.shutdown = fsl_mc_bus_remove,
1207
};
1208
1209
static int fsl_mc_bus_notifier(struct notifier_block *nb,
1210
unsigned long action, void *data)
1211
{
1212
struct device *dev = data;
1213
struct resource *res;
1214
void __iomem *fsl_mc_regs;
1215
1216
if (action != BUS_NOTIFY_ADD_DEVICE)
1217
return 0;
1218
1219
if (!of_match_device(fsl_mc_bus_match_table, dev) &&
1220
!acpi_match_device(fsl_mc_bus_acpi_match_table, dev))
1221
return 0;
1222
1223
res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1);
1224
if (!res)
1225
return 0;
1226
1227
fsl_mc_regs = ioremap(res->start, resource_size(res));
1228
if (!fsl_mc_regs)
1229
return 0;
1230
1231
/*
1232
* Make sure that the MC firmware is paused before the IOMMU setup for
1233
* it is done or otherwise the firmware will crash right after the SMMU
1234
* gets probed and enabled.
1235
*/
1236
writel(readl(fsl_mc_regs + FSL_MC_GCR1) | (GCR1_P1_STOP | GCR1_P2_STOP),
1237
fsl_mc_regs + FSL_MC_GCR1);
1238
iounmap(fsl_mc_regs);
1239
1240
return 0;
1241
}
1242
1243
static struct notifier_block fsl_mc_nb = {
1244
.notifier_call = fsl_mc_bus_notifier,
1245
};
1246
1247
static int __init fsl_mc_bus_driver_init(void)
1248
{
1249
int error;
1250
1251
error = bus_register(&fsl_mc_bus_type);
1252
if (error < 0) {
1253
pr_err("bus type registration failed: %d\n", error);
1254
goto error_cleanup_cache;
1255
}
1256
1257
error = platform_driver_register(&fsl_mc_bus_driver);
1258
if (error < 0) {
1259
pr_err("platform_driver_register() failed: %d\n", error);
1260
goto error_cleanup_bus;
1261
}
1262
1263
error = dprc_driver_init();
1264
if (error < 0)
1265
goto error_cleanup_driver;
1266
1267
error = fsl_mc_allocator_driver_init();
1268
if (error < 0)
1269
goto error_cleanup_dprc_driver;
1270
1271
return bus_register_notifier(&platform_bus_type, &fsl_mc_nb);
1272
1273
error_cleanup_dprc_driver:
1274
dprc_driver_exit();
1275
1276
error_cleanup_driver:
1277
platform_driver_unregister(&fsl_mc_bus_driver);
1278
1279
error_cleanup_bus:
1280
bus_unregister(&fsl_mc_bus_type);
1281
1282
error_cleanup_cache:
1283
return error;
1284
}
1285
postcore_initcall(fsl_mc_bus_driver_init);
1286
1287