bus/fsl-mc: Export IRQ pool handling functions to be used by VFIO
[platform/kernel/linux-starfive.git] / drivers / bus / fsl-mc / fsl-mc-private.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Freescale Management Complex (MC) bus private declarations
4  *
5  * Copyright (C) 2016 Freescale Semiconductor, Inc.
6  *
7  */
8 #ifndef _FSL_MC_PRIVATE_H_
9 #define _FSL_MC_PRIVATE_H_
10
11 #include <linux/fsl/mc.h>
12 #include <linux/mutex.h>
13
14 /*
15  * Data Path Management Complex (DPMNG) General API
16  */
17
18 /* DPMNG command versioning */
19 #define DPMNG_CMD_BASE_VERSION          1
20 #define DPMNG_CMD_ID_OFFSET             4
21
22 #define DPMNG_CMD(id)   (((id) << DPMNG_CMD_ID_OFFSET) | DPMNG_CMD_BASE_VERSION)
23
24 /* DPMNG command IDs */
25 #define DPMNG_CMDID_GET_VERSION         DPMNG_CMD(0x831)
26
27 struct dpmng_rsp_get_version {
28         __le32 revision;
29         __le32 version_major;
30         __le32 version_minor;
31 };
32
33 /*
34  * Data Path Management Command Portal (DPMCP) API
35  */
36
37 /* Minimal supported DPMCP Version */
38 #define DPMCP_MIN_VER_MAJOR             3
39 #define DPMCP_MIN_VER_MINOR             0
40
41 /* DPMCP command versioning */
42 #define DPMCP_CMD_BASE_VERSION          1
43 #define DPMCP_CMD_ID_OFFSET             4
44
45 #define DPMCP_CMD(id)   (((id) << DPMCP_CMD_ID_OFFSET) | DPMCP_CMD_BASE_VERSION)
46
47 /* DPMCP command IDs */
48 #define DPMCP_CMDID_CLOSE               DPMCP_CMD(0x800)
49 #define DPMCP_CMDID_OPEN                DPMCP_CMD(0x80b)
50 #define DPMCP_CMDID_RESET               DPMCP_CMD(0x005)
51
52 struct dpmcp_cmd_open {
53         __le32 dpmcp_id;
54 };
55
56 /*
57  * Initialization and runtime control APIs for DPMCP
58  */
59 int dpmcp_open(struct fsl_mc_io *mc_io,
60                u32 cmd_flags,
61                int dpmcp_id,
62                u16 *token);
63
64 int dpmcp_close(struct fsl_mc_io *mc_io,
65                 u32 cmd_flags,
66                 u16 token);
67
68 int dpmcp_reset(struct fsl_mc_io *mc_io,
69                 u32 cmd_flags,
70                 u16 token);
71
72 /*
73  * Data Path Resource Container (DPRC) API
74  */
75
76 /* Minimal supported DPRC Version */
77 #define DPRC_MIN_VER_MAJOR                      6
78 #define DPRC_MIN_VER_MINOR                      0
79
80 /* DPRC command versioning */
81 #define DPRC_CMD_BASE_VERSION                   1
82 #define DPRC_CMD_2ND_VERSION                    2
83 #define DPRC_CMD_ID_OFFSET                      4
84
85 #define DPRC_CMD(id)    (((id) << DPRC_CMD_ID_OFFSET) | DPRC_CMD_BASE_VERSION)
86 #define DPRC_CMD_V2(id) (((id) << DPRC_CMD_ID_OFFSET) | DPRC_CMD_2ND_VERSION)
87
88 /* DPRC command IDs */
89 #define DPRC_CMDID_CLOSE                        DPRC_CMD(0x800)
90 #define DPRC_CMDID_OPEN                         DPRC_CMD(0x805)
91 #define DPRC_CMDID_GET_API_VERSION              DPRC_CMD(0xa05)
92
93 #define DPRC_CMDID_GET_ATTR                     DPRC_CMD(0x004)
94 #define DPRC_CMDID_RESET_CONT                   DPRC_CMD(0x005)
95 #define DPRC_CMDID_RESET_CONT_V2                DPRC_CMD_V2(0x005)
96
97 #define DPRC_CMDID_SET_IRQ                      DPRC_CMD(0x010)
98 #define DPRC_CMDID_SET_IRQ_ENABLE               DPRC_CMD(0x012)
99 #define DPRC_CMDID_SET_IRQ_MASK                 DPRC_CMD(0x014)
100 #define DPRC_CMDID_GET_IRQ_STATUS               DPRC_CMD(0x016)
101 #define DPRC_CMDID_CLEAR_IRQ_STATUS             DPRC_CMD(0x017)
102
103 #define DPRC_CMDID_GET_CONT_ID                  DPRC_CMD(0x830)
104 #define DPRC_CMDID_GET_OBJ_COUNT                DPRC_CMD(0x159)
105 #define DPRC_CMDID_GET_OBJ                      DPRC_CMD(0x15A)
106 #define DPRC_CMDID_GET_OBJ_REG                  DPRC_CMD(0x15E)
107 #define DPRC_CMDID_GET_OBJ_REG_V2               DPRC_CMD_V2(0x15E)
108 #define DPRC_CMDID_SET_OBJ_IRQ                  DPRC_CMD(0x15F)
109
110 #define DPRC_CMDID_GET_CONNECTION               DPRC_CMD(0x16C)
111
112 struct dprc_cmd_open {
113         __le32 container_id;
114 };
115
116 struct dprc_cmd_reset_container {
117         __le32 child_container_id;
118         __le32 options;
119 };
120
121 struct dprc_cmd_set_irq {
122         /* cmd word 0 */
123         __le32 irq_val;
124         u8 irq_index;
125         u8 pad[3];
126         /* cmd word 1 */
127         __le64 irq_addr;
128         /* cmd word 2 */
129         __le32 irq_num;
130 };
131
132 #define DPRC_ENABLE             0x1
133
134 struct dprc_cmd_set_irq_enable {
135         u8 enable;
136         u8 pad[3];
137         u8 irq_index;
138 };
139
140 struct dprc_cmd_set_irq_mask {
141         __le32 mask;
142         u8 irq_index;
143 };
144
145 struct dprc_cmd_get_irq_status {
146         __le32 status;
147         u8 irq_index;
148 };
149
150 struct dprc_rsp_get_irq_status {
151         __le32 status;
152 };
153
154 struct dprc_cmd_clear_irq_status {
155         __le32 status;
156         u8 irq_index;
157 };
158
159 struct dprc_rsp_get_attributes {
160         /* response word 0 */
161         __le32 container_id;
162         __le16 icid;
163         __le16 pad;
164         /* response word 1 */
165         __le32 options;
166         __le32 portal_id;
167 };
168
169 struct dprc_rsp_get_obj_count {
170         __le32 pad;
171         __le32 obj_count;
172 };
173
174 struct dprc_cmd_get_obj {
175         __le32 obj_index;
176 };
177
178 struct dprc_rsp_get_obj {
179         /* response word 0 */
180         __le32 pad0;
181         __le32 id;
182         /* response word 1 */
183         __le16 vendor;
184         u8 irq_count;
185         u8 region_count;
186         __le32 state;
187         /* response word 2 */
188         __le16 version_major;
189         __le16 version_minor;
190         __le16 flags;
191         __le16 pad1;
192         /* response word 3-4 */
193         u8 type[16];
194         /* response word 5-6 */
195         u8 label[16];
196 };
197
198 struct dprc_cmd_get_obj_region {
199         /* cmd word 0 */
200         __le32 obj_id;
201         __le16 pad0;
202         u8 region_index;
203         u8 pad1;
204         /* cmd word 1-2 */
205         __le64 pad2[2];
206         /* cmd word 3-4 */
207         u8 obj_type[16];
208 };
209
210 struct dprc_rsp_get_obj_region {
211         /* response word 0 */
212         __le64 pad;
213         /* response word 1 */
214         __le64 base_offset;
215         /* response word 2 */
216         __le32 size;
217         __le32 pad2;
218         /* response word 3 */
219         __le32 flags;
220         __le32 pad3;
221         /* response word 4 */
222         /* base_addr may be zero if older MC firmware is used */
223         __le64 base_addr;
224 };
225
226 struct dprc_cmd_set_obj_irq {
227         /* cmd word 0 */
228         __le32 irq_val;
229         u8 irq_index;
230         u8 pad[3];
231         /* cmd word 1 */
232         __le64 irq_addr;
233         /* cmd word 2 */
234         __le32 irq_num;
235         __le32 obj_id;
236         /* cmd word 3-4 */
237         u8 obj_type[16];
238 };
239
240 struct dprc_cmd_get_connection {
241         __le32 ep1_id;
242         __le16 ep1_interface_id;
243         u8 pad[2];
244         u8 ep1_type[16];
245 };
246
247 struct dprc_rsp_get_connection {
248         __le64 pad[3];
249         __le32 ep2_id;
250         __le16 ep2_interface_id;
251         __le16 pad1;
252         u8 ep2_type[16];
253         __le32 state;
254 };
255
256 /*
257  * DPRC API for managing and querying DPAA resources
258  */
259 int dprc_open(struct fsl_mc_io *mc_io,
260               u32 cmd_flags,
261               int container_id,
262               u16 *token);
263
264 int dprc_close(struct fsl_mc_io *mc_io,
265                u32 cmd_flags,
266                u16 token);
267
268 /* DPRC IRQ events */
269
270 /* IRQ event - Indicates that a new object added to the container */
271 #define DPRC_IRQ_EVENT_OBJ_ADDED                0x00000001
272 /* IRQ event - Indicates that an object was removed from the container */
273 #define DPRC_IRQ_EVENT_OBJ_REMOVED              0x00000002
274 /*
275  * IRQ event - Indicates that one of the descendant containers that opened by
276  * this container is destroyed
277  */
278 #define DPRC_IRQ_EVENT_CONTAINER_DESTROYED      0x00000010
279
280 /*
281  * IRQ event - Indicates that on one of the container's opened object is
282  * destroyed
283  */
284 #define DPRC_IRQ_EVENT_OBJ_DESTROYED            0x00000020
285
286 /* Irq event - Indicates that object is created at the container */
287 #define DPRC_IRQ_EVENT_OBJ_CREATED              0x00000040
288
289 /**
290  * struct dprc_irq_cfg - IRQ configuration
291  * @paddr:      Address that must be written to signal a message-based interrupt
292  * @val:        Value to write into irq_addr address
293  * @irq_num:    A user defined number associated with this IRQ
294  */
295 struct dprc_irq_cfg {
296              phys_addr_t paddr;
297              u32 val;
298              int irq_num;
299 };
300
301 int dprc_set_irq(struct fsl_mc_io *mc_io,
302                  u32 cmd_flags,
303                  u16 token,
304                  u8 irq_index,
305                  struct dprc_irq_cfg *irq_cfg);
306
307 int dprc_set_irq_enable(struct fsl_mc_io *mc_io,
308                         u32 cmd_flags,
309                         u16 token,
310                         u8 irq_index,
311                         u8 en);
312
313 int dprc_set_irq_mask(struct fsl_mc_io *mc_io,
314                       u32 cmd_flags,
315                       u16 token,
316                       u8 irq_index,
317                       u32 mask);
318
319 int dprc_get_irq_status(struct fsl_mc_io *mc_io,
320                         u32 cmd_flags,
321                         u16 token,
322                         u8 irq_index,
323                         u32 *status);
324
325 int dprc_clear_irq_status(struct fsl_mc_io *mc_io,
326                           u32 cmd_flags,
327                           u16 token,
328                           u8 irq_index,
329                           u32 status);
330
331 /**
332  * struct dprc_attributes - Container attributes
333  * @container_id: Container's ID
334  * @icid: Container's ICID
335  * @portal_id: Container's portal ID
336  * @options: Container's options as set at container's creation
337  */
338 struct dprc_attributes {
339         int container_id;
340         u16 icid;
341         int portal_id;
342         u64 options;
343 };
344
345 int dprc_get_attributes(struct fsl_mc_io *mc_io,
346                         u32 cmd_flags,
347                         u16 token,
348                         struct dprc_attributes *attributes);
349
350 int dprc_get_obj_count(struct fsl_mc_io *mc_io,
351                        u32 cmd_flags,
352                        u16 token,
353                        int *obj_count);
354
355 int dprc_get_obj(struct fsl_mc_io *mc_io,
356                  u32 cmd_flags,
357                  u16 token,
358                  int obj_index,
359                  struct fsl_mc_obj_desc *obj_desc);
360
361 int dprc_set_obj_irq(struct fsl_mc_io *mc_io,
362                      u32 cmd_flags,
363                      u16 token,
364                      char *obj_type,
365                      int obj_id,
366                      u8 irq_index,
367                      struct dprc_irq_cfg *irq_cfg);
368 /**
369  * enum dprc_region_type - Region type
370  * @DPRC_REGION_TYPE_MC_PORTAL: MC portal region
371  * @DPRC_REGION_TYPE_QBMAN_PORTAL: Qbman portal region
372  */
373 enum dprc_region_type {
374         DPRC_REGION_TYPE_MC_PORTAL,
375         DPRC_REGION_TYPE_QBMAN_PORTAL,
376         DPRC_REGION_TYPE_QBMAN_MEM_BACKED_PORTAL
377 };
378
379 /**
380  * struct dprc_region_desc - Mappable region descriptor
381  * @base_offset: Region offset from region's base address.
382  *      For DPMCP and DPRC objects, region base is offset from SoC MC portals
383  *      base address; For DPIO, region base is offset from SoC QMan portals
384  *      base address
385  * @size: Region size (in bytes)
386  * @flags: Region attributes
387  * @type: Portal region type
388  */
389 struct dprc_region_desc {
390         u32 base_offset;
391         u32 size;
392         u32 flags;
393         enum dprc_region_type type;
394         u64 base_address;
395 };
396
397 int dprc_get_obj_region(struct fsl_mc_io *mc_io,
398                         u32 cmd_flags,
399                         u16 token,
400                         char *obj_type,
401                         int obj_id,
402                         u8 region_index,
403                         struct dprc_region_desc *region_desc);
404
405 int dprc_get_api_version(struct fsl_mc_io *mc_io,
406                          u32 cmd_flags,
407                          u16 *major_ver,
408                          u16 *minor_ver);
409
410 int dprc_get_container_id(struct fsl_mc_io *mc_io,
411                           u32 cmd_flags,
412                           int *container_id);
413
414 /**
415  * struct dprc_endpoint - Endpoint description for link connect/disconnect
416  *                      operations
417  * @type:       Endpoint object type: NULL terminated string
418  * @id:         Endpoint object ID
419  * @if_id:      Interface ID; should be set for endpoints with multiple
420  *              interfaces ("dpsw", "dpdmux"); for others, always set to 0
421  */
422 struct dprc_endpoint {
423         char type[16];
424         int id;
425         u16 if_id;
426 };
427
428 int dprc_get_connection(struct fsl_mc_io *mc_io,
429                         u32 cmd_flags,
430                         u16 token,
431                         const struct dprc_endpoint *endpoint1,
432                         struct dprc_endpoint *endpoint2,
433                         int *state);
434
435 /*
436  * Data Path Buffer Pool (DPBP) API
437  */
438
439 /* DPBP Version */
440 #define DPBP_VER_MAJOR                          3
441 #define DPBP_VER_MINOR                          2
442
443 /* Command versioning */
444 #define DPBP_CMD_BASE_VERSION                   1
445 #define DPBP_CMD_ID_OFFSET                      4
446
447 #define DPBP_CMD(id)    (((id) << DPBP_CMD_ID_OFFSET) | DPBP_CMD_BASE_VERSION)
448
449 /* Command IDs */
450 #define DPBP_CMDID_CLOSE                DPBP_CMD(0x800)
451 #define DPBP_CMDID_OPEN                 DPBP_CMD(0x804)
452
453 #define DPBP_CMDID_ENABLE               DPBP_CMD(0x002)
454 #define DPBP_CMDID_DISABLE              DPBP_CMD(0x003)
455 #define DPBP_CMDID_GET_ATTR             DPBP_CMD(0x004)
456 #define DPBP_CMDID_RESET                DPBP_CMD(0x005)
457
458 struct dpbp_cmd_open {
459         __le32 dpbp_id;
460 };
461
462 #define DPBP_ENABLE                     0x1
463
464 struct dpbp_rsp_get_attributes {
465         /* response word 0 */
466         __le16 pad;
467         __le16 bpid;
468         __le32 id;
469         /* response word 1 */
470         __le16 version_major;
471         __le16 version_minor;
472 };
473
474 /*
475  * Data Path Concentrator (DPCON) API
476  */
477
478 /* DPCON Version */
479 #define DPCON_VER_MAJOR                         3
480 #define DPCON_VER_MINOR                         2
481
482 /* Command versioning */
483 #define DPCON_CMD_BASE_VERSION                  1
484 #define DPCON_CMD_ID_OFFSET                     4
485
486 #define DPCON_CMD(id)   (((id) << DPCON_CMD_ID_OFFSET) | DPCON_CMD_BASE_VERSION)
487
488 /* Command IDs */
489 #define DPCON_CMDID_CLOSE                       DPCON_CMD(0x800)
490 #define DPCON_CMDID_OPEN                        DPCON_CMD(0x808)
491
492 #define DPCON_CMDID_ENABLE                      DPCON_CMD(0x002)
493 #define DPCON_CMDID_DISABLE                     DPCON_CMD(0x003)
494 #define DPCON_CMDID_GET_ATTR                    DPCON_CMD(0x004)
495 #define DPCON_CMDID_RESET                       DPCON_CMD(0x005)
496
497 #define DPCON_CMDID_SET_NOTIFICATION            DPCON_CMD(0x100)
498
499 struct dpcon_cmd_open {
500         __le32 dpcon_id;
501 };
502
503 #define DPCON_ENABLE                    1
504
505 struct dpcon_rsp_get_attr {
506         /* response word 0 */
507         __le32 id;
508         __le16 qbman_ch_id;
509         u8 num_priorities;
510         u8 pad;
511 };
512
513 struct dpcon_cmd_set_notification {
514         /* cmd word 0 */
515         __le32 dpio_id;
516         u8 priority;
517         u8 pad[3];
518         /* cmd word 1 */
519         __le64 user_ctx;
520 };
521
522
523 /**
524  * struct fsl_mc_resource_pool - Pool of MC resources of a given
525  * type
526  * @type: type of resources in the pool
527  * @max_count: maximum number of resources in the pool
528  * @free_count: number of free resources in the pool
529  * @mutex: mutex to serialize access to the pool's free list
530  * @free_list: anchor node of list of free resources in the pool
531  * @mc_bus: pointer to the MC bus that owns this resource pool
532  */
533 struct fsl_mc_resource_pool {
534         enum fsl_mc_pool_type type;
535         int max_count;
536         int free_count;
537         struct mutex mutex;     /* serializes access to free_list */
538         struct list_head free_list;
539         struct fsl_mc_bus *mc_bus;
540 };
541
542 /**
543  * struct fsl_mc_bus - logical bus that corresponds to a physical DPRC
544  * @mc_dev: fsl-mc device for the bus device itself.
545  * @resource_pools: array of resource pools (one pool per resource type)
546  * for this MC bus. These resources represent allocatable entities
547  * from the physical DPRC.
548  * @irq_resources: Pointer to array of IRQ objects for the IRQ pool
549  * @scan_mutex: Serializes bus scanning
550  * @dprc_attr: DPRC attributes
551  */
552 struct fsl_mc_bus {
553         struct fsl_mc_device mc_dev;
554         struct fsl_mc_resource_pool resource_pools[FSL_MC_NUM_POOL_TYPES];
555         struct fsl_mc_device_irq *irq_resources;
556         struct mutex scan_mutex;    /* serializes bus scanning */
557         struct dprc_attributes dprc_attr;
558 };
559
560 #define to_fsl_mc_bus(_mc_dev) \
561         container_of(_mc_dev, struct fsl_mc_bus, mc_dev)
562
563 int __must_check fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
564                                    struct fsl_mc_io *mc_io,
565                                    struct device *parent_dev,
566                                    struct fsl_mc_device **new_mc_dev);
567
568 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev);
569
570 int __init dprc_driver_init(void);
571
572 void dprc_driver_exit(void);
573
574 int __init fsl_mc_allocator_driver_init(void);
575
576 void fsl_mc_allocator_driver_exit(void);
577
578 void fsl_mc_init_all_resource_pools(struct fsl_mc_device *mc_bus_dev);
579
580 void fsl_mc_cleanup_all_resource_pools(struct fsl_mc_device *mc_bus_dev);
581
582 int __must_check fsl_mc_resource_allocate(struct fsl_mc_bus *mc_bus,
583                                           enum fsl_mc_pool_type pool_type,
584                                           struct fsl_mc_resource
585                                                           **new_resource);
586
587 void fsl_mc_resource_free(struct fsl_mc_resource *resource);
588
589 int fsl_mc_msi_domain_alloc_irqs(struct device *dev,
590                                  unsigned int irq_count);
591
592 void fsl_mc_msi_domain_free_irqs(struct device *dev);
593
594 struct irq_domain *fsl_mc_find_msi_domain(struct device *dev);
595
596 int __must_check fsl_create_mc_io(struct device *dev,
597                                   phys_addr_t mc_portal_phys_addr,
598                                   u32 mc_portal_size,
599                                   struct fsl_mc_device *dpmcp_dev,
600                                   u32 flags, struct fsl_mc_io **new_mc_io);
601
602 void fsl_destroy_mc_io(struct fsl_mc_io *mc_io);
603
604 bool fsl_mc_is_root_dprc(struct device *dev);
605
606 void fsl_mc_get_root_dprc(struct device *dev,
607                          struct device **root_dprc_dev);
608
609 struct fsl_mc_device *fsl_mc_device_lookup(struct fsl_mc_obj_desc *obj_desc,
610                                            struct fsl_mc_device *mc_bus_dev);
611
612 #endif /* _FSL_MC_PRIVATE_H_ */