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_ID_OFFSET			4
83 
84 #define DPRC_CMD(id)	(((id) << DPRC_CMD_ID_OFFSET) | DPRC_CMD_BASE_VERSION)
85 
86 /* DPRC command IDs */
87 #define DPRC_CMDID_CLOSE                        DPRC_CMD(0x800)
88 #define DPRC_CMDID_OPEN                         DPRC_CMD(0x805)
89 #define DPRC_CMDID_GET_API_VERSION              DPRC_CMD(0xa05)
90 
91 #define DPRC_CMDID_GET_ATTR                     DPRC_CMD(0x004)
92 
93 #define DPRC_CMDID_SET_IRQ                      DPRC_CMD(0x010)
94 #define DPRC_CMDID_SET_IRQ_ENABLE               DPRC_CMD(0x012)
95 #define DPRC_CMDID_SET_IRQ_MASK                 DPRC_CMD(0x014)
96 #define DPRC_CMDID_GET_IRQ_STATUS               DPRC_CMD(0x016)
97 #define DPRC_CMDID_CLEAR_IRQ_STATUS             DPRC_CMD(0x017)
98 
99 #define DPRC_CMDID_GET_CONT_ID                  DPRC_CMD(0x830)
100 #define DPRC_CMDID_GET_OBJ_COUNT                DPRC_CMD(0x159)
101 #define DPRC_CMDID_GET_OBJ                      DPRC_CMD(0x15A)
102 #define DPRC_CMDID_GET_OBJ_REG                  DPRC_CMD(0x15E)
103 #define DPRC_CMDID_SET_OBJ_IRQ                  DPRC_CMD(0x15F)
104 
105 struct dprc_cmd_open {
106 	__le32 container_id;
107 };
108 
109 struct dprc_cmd_set_irq {
110 	/* cmd word 0 */
111 	__le32 irq_val;
112 	u8 irq_index;
113 	u8 pad[3];
114 	/* cmd word 1 */
115 	__le64 irq_addr;
116 	/* cmd word 2 */
117 	__le32 irq_num;
118 };
119 
120 #define DPRC_ENABLE		0x1
121 
122 struct dprc_cmd_set_irq_enable {
123 	u8 enable;
124 	u8 pad[3];
125 	u8 irq_index;
126 };
127 
128 struct dprc_cmd_set_irq_mask {
129 	__le32 mask;
130 	u8 irq_index;
131 };
132 
133 struct dprc_cmd_get_irq_status {
134 	__le32 status;
135 	u8 irq_index;
136 };
137 
138 struct dprc_rsp_get_irq_status {
139 	__le32 status;
140 };
141 
142 struct dprc_cmd_clear_irq_status {
143 	__le32 status;
144 	u8 irq_index;
145 };
146 
147 struct dprc_rsp_get_attributes {
148 	/* response word 0 */
149 	__le32 container_id;
150 	__le16 icid;
151 	__le16 pad;
152 	/* response word 1 */
153 	__le32 options;
154 	__le32 portal_id;
155 };
156 
157 struct dprc_rsp_get_obj_count {
158 	__le32 pad;
159 	__le32 obj_count;
160 };
161 
162 struct dprc_cmd_get_obj {
163 	__le32 obj_index;
164 };
165 
166 struct dprc_rsp_get_obj {
167 	/* response word 0 */
168 	__le32 pad0;
169 	__le32 id;
170 	/* response word 1 */
171 	__le16 vendor;
172 	u8 irq_count;
173 	u8 region_count;
174 	__le32 state;
175 	/* response word 2 */
176 	__le16 version_major;
177 	__le16 version_minor;
178 	__le16 flags;
179 	__le16 pad1;
180 	/* response word 3-4 */
181 	u8 type[16];
182 	/* response word 5-6 */
183 	u8 label[16];
184 };
185 
186 struct dprc_cmd_get_obj_region {
187 	/* cmd word 0 */
188 	__le32 obj_id;
189 	__le16 pad0;
190 	u8 region_index;
191 	u8 pad1;
192 	/* cmd word 1-2 */
193 	__le64 pad2[2];
194 	/* cmd word 3-4 */
195 	u8 obj_type[16];
196 };
197 
198 struct dprc_rsp_get_obj_region {
199 	/* response word 0 */
200 	__le64 pad;
201 	/* response word 1 */
202 	__le64 base_addr;
203 	/* response word 2 */
204 	__le32 size;
205 };
206 
207 struct dprc_cmd_set_obj_irq {
208 	/* cmd word 0 */
209 	__le32 irq_val;
210 	u8 irq_index;
211 	u8 pad[3];
212 	/* cmd word 1 */
213 	__le64 irq_addr;
214 	/* cmd word 2 */
215 	__le32 irq_num;
216 	__le32 obj_id;
217 	/* cmd word 3-4 */
218 	u8 obj_type[16];
219 };
220 
221 /*
222  * DPRC API for managing and querying DPAA resources
223  */
224 int dprc_open(struct fsl_mc_io *mc_io,
225 	      u32 cmd_flags,
226 	      int container_id,
227 	      u16 *token);
228 
229 int dprc_close(struct fsl_mc_io *mc_io,
230 	       u32 cmd_flags,
231 	       u16 token);
232 
233 /* DPRC IRQ events */
234 
235 /* IRQ event - Indicates that a new object added to the container */
236 #define DPRC_IRQ_EVENT_OBJ_ADDED		0x00000001
237 /* IRQ event - Indicates that an object was removed from the container */
238 #define DPRC_IRQ_EVENT_OBJ_REMOVED		0x00000002
239 /*
240  * IRQ event - Indicates that one of the descendant containers that opened by
241  * this container is destroyed
242  */
243 #define DPRC_IRQ_EVENT_CONTAINER_DESTROYED	0x00000010
244 
245 /*
246  * IRQ event - Indicates that on one of the container's opened object is
247  * destroyed
248  */
249 #define DPRC_IRQ_EVENT_OBJ_DESTROYED		0x00000020
250 
251 /* Irq event - Indicates that object is created at the container */
252 #define DPRC_IRQ_EVENT_OBJ_CREATED		0x00000040
253 
254 /**
255  * struct dprc_irq_cfg - IRQ configuration
256  * @paddr:	Address that must be written to signal a message-based interrupt
257  * @val:	Value to write into irq_addr address
258  * @irq_num:	A user defined number associated with this IRQ
259  */
260 struct dprc_irq_cfg {
261 	     phys_addr_t paddr;
262 	     u32 val;
263 	     int irq_num;
264 };
265 
266 int dprc_set_irq(struct fsl_mc_io *mc_io,
267 		 u32 cmd_flags,
268 		 u16 token,
269 		 u8 irq_index,
270 		 struct dprc_irq_cfg *irq_cfg);
271 
272 int dprc_set_irq_enable(struct fsl_mc_io *mc_io,
273 			u32 cmd_flags,
274 			u16 token,
275 			u8 irq_index,
276 			u8 en);
277 
278 int dprc_set_irq_mask(struct fsl_mc_io *mc_io,
279 		      u32 cmd_flags,
280 		      u16 token,
281 		      u8 irq_index,
282 		      u32 mask);
283 
284 int dprc_get_irq_status(struct fsl_mc_io *mc_io,
285 			u32 cmd_flags,
286 			u16 token,
287 			u8 irq_index,
288 			u32 *status);
289 
290 int dprc_clear_irq_status(struct fsl_mc_io *mc_io,
291 			  u32 cmd_flags,
292 			  u16 token,
293 			  u8 irq_index,
294 			  u32 status);
295 
296 /**
297  * struct dprc_attributes - Container attributes
298  * @container_id: Container's ID
299  * @icid: Container's ICID
300  * @portal_id: Container's portal ID
301  * @options: Container's options as set at container's creation
302  */
303 struct dprc_attributes {
304 	int container_id;
305 	u16 icid;
306 	int portal_id;
307 	u64 options;
308 };
309 
310 int dprc_get_attributes(struct fsl_mc_io *mc_io,
311 			u32 cmd_flags,
312 			u16 token,
313 			struct dprc_attributes *attributes);
314 
315 int dprc_get_obj_count(struct fsl_mc_io *mc_io,
316 		       u32 cmd_flags,
317 		       u16 token,
318 		       int *obj_count);
319 
320 int dprc_get_obj(struct fsl_mc_io *mc_io,
321 		 u32 cmd_flags,
322 		 u16 token,
323 		 int obj_index,
324 		 struct fsl_mc_obj_desc *obj_desc);
325 
326 int dprc_set_obj_irq(struct fsl_mc_io *mc_io,
327 		     u32 cmd_flags,
328 		     u16 token,
329 		     char *obj_type,
330 		     int obj_id,
331 		     u8 irq_index,
332 		     struct dprc_irq_cfg *irq_cfg);
333 
334 /* Region flags */
335 /* Cacheable - Indicates that region should be mapped as cacheable */
336 #define DPRC_REGION_CACHEABLE	0x00000001
337 
338 /**
339  * enum dprc_region_type - Region type
340  * @DPRC_REGION_TYPE_MC_PORTAL: MC portal region
341  * @DPRC_REGION_TYPE_QBMAN_PORTAL: Qbman portal region
342  */
343 enum dprc_region_type {
344 	DPRC_REGION_TYPE_MC_PORTAL,
345 	DPRC_REGION_TYPE_QBMAN_PORTAL
346 };
347 
348 /**
349  * struct dprc_region_desc - Mappable region descriptor
350  * @base_offset: Region offset from region's base address.
351  *	For DPMCP and DPRC objects, region base is offset from SoC MC portals
352  *	base address; For DPIO, region base is offset from SoC QMan portals
353  *	base address
354  * @size: Region size (in bytes)
355  * @flags: Region attributes
356  * @type: Portal region type
357  */
358 struct dprc_region_desc {
359 	u32 base_offset;
360 	u32 size;
361 	u32 flags;
362 	enum dprc_region_type type;
363 };
364 
365 int dprc_get_obj_region(struct fsl_mc_io *mc_io,
366 			u32 cmd_flags,
367 			u16 token,
368 			char *obj_type,
369 			int obj_id,
370 			u8 region_index,
371 			struct dprc_region_desc *region_desc);
372 
373 int dprc_get_api_version(struct fsl_mc_io *mc_io,
374 			 u32 cmd_flags,
375 			 u16 *major_ver,
376 			 u16 *minor_ver);
377 
378 int dprc_get_container_id(struct fsl_mc_io *mc_io,
379 			  u32 cmd_flags,
380 			  int *container_id);
381 
382 /*
383  * Data Path Buffer Pool (DPBP) API
384  */
385 
386 /* DPBP Version */
387 #define DPBP_VER_MAJOR				3
388 #define DPBP_VER_MINOR				2
389 
390 /* Command versioning */
391 #define DPBP_CMD_BASE_VERSION			1
392 #define DPBP_CMD_ID_OFFSET			4
393 
394 #define DPBP_CMD(id)	(((id) << DPBP_CMD_ID_OFFSET) | DPBP_CMD_BASE_VERSION)
395 
396 /* Command IDs */
397 #define DPBP_CMDID_CLOSE		DPBP_CMD(0x800)
398 #define DPBP_CMDID_OPEN			DPBP_CMD(0x804)
399 
400 #define DPBP_CMDID_ENABLE		DPBP_CMD(0x002)
401 #define DPBP_CMDID_DISABLE		DPBP_CMD(0x003)
402 #define DPBP_CMDID_GET_ATTR		DPBP_CMD(0x004)
403 #define DPBP_CMDID_RESET		DPBP_CMD(0x005)
404 
405 struct dpbp_cmd_open {
406 	__le32 dpbp_id;
407 };
408 
409 #define DPBP_ENABLE			0x1
410 
411 struct dpbp_rsp_get_attributes {
412 	/* response word 0 */
413 	__le16 pad;
414 	__le16 bpid;
415 	__le32 id;
416 	/* response word 1 */
417 	__le16 version_major;
418 	__le16 version_minor;
419 };
420 
421 /*
422  * Data Path Concentrator (DPCON) API
423  */
424 
425 /* DPCON Version */
426 #define DPCON_VER_MAJOR				3
427 #define DPCON_VER_MINOR				2
428 
429 /* Command versioning */
430 #define DPCON_CMD_BASE_VERSION			1
431 #define DPCON_CMD_ID_OFFSET			4
432 
433 #define DPCON_CMD(id)	(((id) << DPCON_CMD_ID_OFFSET) | DPCON_CMD_BASE_VERSION)
434 
435 /* Command IDs */
436 #define DPCON_CMDID_CLOSE			DPCON_CMD(0x800)
437 #define DPCON_CMDID_OPEN			DPCON_CMD(0x808)
438 
439 #define DPCON_CMDID_ENABLE			DPCON_CMD(0x002)
440 #define DPCON_CMDID_DISABLE			DPCON_CMD(0x003)
441 #define DPCON_CMDID_GET_ATTR			DPCON_CMD(0x004)
442 #define DPCON_CMDID_RESET			DPCON_CMD(0x005)
443 
444 #define DPCON_CMDID_SET_NOTIFICATION		DPCON_CMD(0x100)
445 
446 struct dpcon_cmd_open {
447 	__le32 dpcon_id;
448 };
449 
450 #define DPCON_ENABLE			1
451 
452 struct dpcon_rsp_get_attr {
453 	/* response word 0 */
454 	__le32 id;
455 	__le16 qbman_ch_id;
456 	u8 num_priorities;
457 	u8 pad;
458 };
459 
460 struct dpcon_cmd_set_notification {
461 	/* cmd word 0 */
462 	__le32 dpio_id;
463 	u8 priority;
464 	u8 pad[3];
465 	/* cmd word 1 */
466 	__le64 user_ctx;
467 };
468 
469 /**
470  * Maximum number of total IRQs that can be pre-allocated for an MC bus'
471  * IRQ pool
472  */
473 #define FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS	256
474 
475 /**
476  * struct fsl_mc_resource_pool - Pool of MC resources of a given
477  * type
478  * @type: type of resources in the pool
479  * @max_count: maximum number of resources in the pool
480  * @free_count: number of free resources in the pool
481  * @mutex: mutex to serialize access to the pool's free list
482  * @free_list: anchor node of list of free resources in the pool
483  * @mc_bus: pointer to the MC bus that owns this resource pool
484  */
485 struct fsl_mc_resource_pool {
486 	enum fsl_mc_pool_type type;
487 	int max_count;
488 	int free_count;
489 	struct mutex mutex;	/* serializes access to free_list */
490 	struct list_head free_list;
491 	struct fsl_mc_bus *mc_bus;
492 };
493 
494 /**
495  * struct fsl_mc_bus - logical bus that corresponds to a physical DPRC
496  * @mc_dev: fsl-mc device for the bus device itself.
497  * @resource_pools: array of resource pools (one pool per resource type)
498  * for this MC bus. These resources represent allocatable entities
499  * from the physical DPRC.
500  * @irq_resources: Pointer to array of IRQ objects for the IRQ pool
501  * @scan_mutex: Serializes bus scanning
502  * @dprc_attr: DPRC attributes
503  */
504 struct fsl_mc_bus {
505 	struct fsl_mc_device mc_dev;
506 	struct fsl_mc_resource_pool resource_pools[FSL_MC_NUM_POOL_TYPES];
507 	struct fsl_mc_device_irq *irq_resources;
508 	struct mutex scan_mutex;    /* serializes bus scanning */
509 	struct dprc_attributes dprc_attr;
510 };
511 
512 #define to_fsl_mc_bus(_mc_dev) \
513 	container_of(_mc_dev, struct fsl_mc_bus, mc_dev)
514 
515 int __must_check fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
516 				   struct fsl_mc_io *mc_io,
517 				   struct device *parent_dev,
518 				   struct fsl_mc_device **new_mc_dev);
519 
520 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev);
521 
522 int __init dprc_driver_init(void);
523 
524 void dprc_driver_exit(void);
525 
526 int __init fsl_mc_allocator_driver_init(void);
527 
528 void fsl_mc_allocator_driver_exit(void);
529 
530 void fsl_mc_init_all_resource_pools(struct fsl_mc_device *mc_bus_dev);
531 
532 void fsl_mc_cleanup_all_resource_pools(struct fsl_mc_device *mc_bus_dev);
533 
534 int __must_check fsl_mc_resource_allocate(struct fsl_mc_bus *mc_bus,
535 					  enum fsl_mc_pool_type pool_type,
536 					  struct fsl_mc_resource
537 							  **new_resource);
538 
539 void fsl_mc_resource_free(struct fsl_mc_resource *resource);
540 
541 int fsl_mc_msi_domain_alloc_irqs(struct device *dev,
542 				 unsigned int irq_count);
543 
544 void fsl_mc_msi_domain_free_irqs(struct device *dev);
545 
546 int fsl_mc_find_msi_domain(struct device *mc_platform_dev,
547 			   struct irq_domain **mc_msi_domain);
548 
549 int fsl_mc_populate_irq_pool(struct fsl_mc_bus *mc_bus,
550 			     unsigned int irq_count);
551 
552 void fsl_mc_cleanup_irq_pool(struct fsl_mc_bus *mc_bus);
553 
554 int __must_check fsl_create_mc_io(struct device *dev,
555 				  phys_addr_t mc_portal_phys_addr,
556 				  u32 mc_portal_size,
557 				  struct fsl_mc_device *dpmcp_dev,
558 				  u32 flags, struct fsl_mc_io **new_mc_io);
559 
560 void fsl_destroy_mc_io(struct fsl_mc_io *mc_io);
561 
562 bool fsl_mc_is_root_dprc(struct device *dev);
563 
564 #endif /* _FSL_MC_PRIVATE_H_ */
565