1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * Copyright 2013-2016 Freescale Semiconductor Inc.
7 #include <linux/kernel.h>
8 #include <linux/fsl/mc.h>
10 #include "fsl-mc-private.h"
13 * cache the DPRC version to reduce the number of commands
14 * towards the mc firmware
16 static u16 dprc_major_ver
;
17 static u16 dprc_minor_ver
;
20 * dprc_open() - Open DPRC object for use
21 * @mc_io: Pointer to MC portal's I/O object
22 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
23 * @container_id: Container ID to open
24 * @token: Returned token of DPRC object
26 * Return: '0' on Success; Error code otherwise.
28 * @warning Required before any operation on the object.
30 int dprc_open(struct fsl_mc_io
*mc_io
,
35 struct fsl_mc_command cmd
= { 0 };
36 struct dprc_cmd_open
*cmd_params
;
40 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_OPEN
, cmd_flags
,
42 cmd_params
= (struct dprc_cmd_open
*)cmd
.params
;
43 cmd_params
->container_id
= cpu_to_le32(container_id
);
45 /* send command to mc*/
46 err
= mc_send_command(mc_io
, &cmd
);
50 /* retrieve response parameters */
51 *token
= mc_cmd_hdr_read_token(&cmd
);
55 EXPORT_SYMBOL_GPL(dprc_open
);
58 * dprc_close() - Close the control session of the object
59 * @mc_io: Pointer to MC portal's I/O object
60 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
61 * @token: Token of DPRC object
63 * After this function is called, no further operations are
64 * allowed on the object without opening a new control session.
66 * Return: '0' on Success; Error code otherwise.
68 int dprc_close(struct fsl_mc_io
*mc_io
,
72 struct fsl_mc_command cmd
= { 0 };
75 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_CLOSE
, cmd_flags
,
78 /* send command to mc*/
79 return mc_send_command(mc_io
, &cmd
);
81 EXPORT_SYMBOL_GPL(dprc_close
);
84 * dprc_reset_container - Reset child container.
85 * @mc_io: Pointer to MC portal's I/O object
86 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
87 * @token: Token of DPRC object
88 * @child_container_id: ID of the container to reset
89 * @options: 32 bit options:
90 * - 0 (no bits set) - all the objects inside the container are
91 * reset. The child containers are entered recursively and the
92 * objects reset. All the objects (including the child containers)
94 * - bit 0 set - all the objects inside the container are reset.
95 * However the child containers are not entered recursively.
96 * This option is supported for API versions >= 6.5
97 * In case a software context crashes or becomes non-responsive, the parent
98 * may wish to reset its resources container before the software context is
101 * This routine informs all objects assigned to the child container that the
102 * container is being reset, so they may perform any cleanup operations that are
103 * needed. All objects handles that were owned by the child container shall be
106 * Note that such request may be submitted even if the child software context
107 * has not crashed, but the resulting object cleanup operations will not be
110 * Return: '0' on Success; Error code otherwise.
112 int dprc_reset_container(struct fsl_mc_io
*mc_io
,
115 int child_container_id
,
118 struct fsl_mc_command cmd
= { 0 };
119 struct dprc_cmd_reset_container
*cmd_params
;
120 u32 cmdid
= DPRC_CMDID_RESET_CONT
;
124 * If the DPRC object version was not yet cached, cache it now.
125 * Otherwise use the already cached value.
127 if (!dprc_major_ver
&& !dprc_minor_ver
) {
128 err
= dprc_get_api_version(mc_io
, 0,
136 * MC API 6.5 introduced a new field in the command used to pass
138 * Bit 0 indicates that the child containers are not recursively reset.
140 if (dprc_major_ver
> 6 || (dprc_major_ver
== 6 && dprc_minor_ver
>= 5))
141 cmdid
= DPRC_CMDID_RESET_CONT_V2
;
143 /* prepare command */
144 cmd
.header
= mc_encode_cmd_header(cmdid
, cmd_flags
, token
);
145 cmd_params
= (struct dprc_cmd_reset_container
*)cmd
.params
;
146 cmd_params
->child_container_id
= cpu_to_le32(child_container_id
);
147 cmd_params
->options
= cpu_to_le32(options
);
149 /* send command to mc*/
150 return mc_send_command(mc_io
, &cmd
);
152 EXPORT_SYMBOL_GPL(dprc_reset_container
);
155 * dprc_set_irq() - Set IRQ information for the DPRC to trigger an interrupt.
156 * @mc_io: Pointer to MC portal's I/O object
157 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
158 * @token: Token of DPRC object
159 * @irq_index: Identifies the interrupt index to configure
160 * @irq_cfg: IRQ configuration
162 * Return: '0' on Success; Error code otherwise.
164 int dprc_set_irq(struct fsl_mc_io
*mc_io
,
168 struct dprc_irq_cfg
*irq_cfg
)
170 struct fsl_mc_command cmd
= { 0 };
171 struct dprc_cmd_set_irq
*cmd_params
;
173 /* prepare command */
174 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_SET_IRQ
,
177 cmd_params
= (struct dprc_cmd_set_irq
*)cmd
.params
;
178 cmd_params
->irq_val
= cpu_to_le32(irq_cfg
->val
);
179 cmd_params
->irq_index
= irq_index
;
180 cmd_params
->irq_addr
= cpu_to_le64(irq_cfg
->paddr
);
181 cmd_params
->irq_num
= cpu_to_le32(irq_cfg
->irq_num
);
183 /* send command to mc*/
184 return mc_send_command(mc_io
, &cmd
);
188 * dprc_set_irq_enable() - Set overall interrupt state.
189 * @mc_io: Pointer to MC portal's I/O object
190 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
191 * @token: Token of DPRC object
192 * @irq_index: The interrupt index to configure
193 * @en: Interrupt state - enable = 1, disable = 0
195 * Allows GPP software to control when interrupts are generated.
196 * Each interrupt can have up to 32 causes. The enable/disable control's the
197 * overall interrupt state. if the interrupt is disabled no causes will cause
200 * Return: '0' on Success; Error code otherwise.
202 int dprc_set_irq_enable(struct fsl_mc_io
*mc_io
,
208 struct fsl_mc_command cmd
= { 0 };
209 struct dprc_cmd_set_irq_enable
*cmd_params
;
211 /* prepare command */
212 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_SET_IRQ_ENABLE
,
214 cmd_params
= (struct dprc_cmd_set_irq_enable
*)cmd
.params
;
215 cmd_params
->enable
= en
& DPRC_ENABLE
;
216 cmd_params
->irq_index
= irq_index
;
218 /* send command to mc*/
219 return mc_send_command(mc_io
, &cmd
);
223 * dprc_set_irq_mask() - Set interrupt mask.
224 * @mc_io: Pointer to MC portal's I/O object
225 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
226 * @token: Token of DPRC object
227 * @irq_index: The interrupt index to configure
228 * @mask: event mask to trigger interrupt;
231 * 1 = consider event for asserting irq
233 * Every interrupt can have up to 32 causes and the interrupt model supports
234 * masking/unmasking each cause independently
236 * Return: '0' on Success; Error code otherwise.
238 int dprc_set_irq_mask(struct fsl_mc_io
*mc_io
,
244 struct fsl_mc_command cmd
= { 0 };
245 struct dprc_cmd_set_irq_mask
*cmd_params
;
247 /* prepare command */
248 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_SET_IRQ_MASK
,
250 cmd_params
= (struct dprc_cmd_set_irq_mask
*)cmd
.params
;
251 cmd_params
->mask
= cpu_to_le32(mask
);
252 cmd_params
->irq_index
= irq_index
;
254 /* send command to mc*/
255 return mc_send_command(mc_io
, &cmd
);
259 * dprc_get_irq_status() - Get the current status of any pending interrupts.
260 * @mc_io: Pointer to MC portal's I/O object
261 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
262 * @token: Token of DPRC object
263 * @irq_index: The interrupt index to configure
264 * @status: Returned interrupts status - one bit per cause:
265 * 0 = no interrupt pending
266 * 1 = interrupt pending
268 * Return: '0' on Success; Error code otherwise.
270 int dprc_get_irq_status(struct fsl_mc_io
*mc_io
,
276 struct fsl_mc_command cmd
= { 0 };
277 struct dprc_cmd_get_irq_status
*cmd_params
;
278 struct dprc_rsp_get_irq_status
*rsp_params
;
281 /* prepare command */
282 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_GET_IRQ_STATUS
,
284 cmd_params
= (struct dprc_cmd_get_irq_status
*)cmd
.params
;
285 cmd_params
->status
= cpu_to_le32(*status
);
286 cmd_params
->irq_index
= irq_index
;
288 /* send command to mc*/
289 err
= mc_send_command(mc_io
, &cmd
);
293 /* retrieve response parameters */
294 rsp_params
= (struct dprc_rsp_get_irq_status
*)cmd
.params
;
295 *status
= le32_to_cpu(rsp_params
->status
);
301 * dprc_clear_irq_status() - Clear a pending interrupt's status
302 * @mc_io: Pointer to MC portal's I/O object
303 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
304 * @token: Token of DPRC object
305 * @irq_index: The interrupt index to configure
306 * @status: bits to clear (W1C) - one bit per cause:
308 * 1 = clear status bit
310 * Return: '0' on Success; Error code otherwise.
312 int dprc_clear_irq_status(struct fsl_mc_io
*mc_io
,
318 struct fsl_mc_command cmd
= { 0 };
319 struct dprc_cmd_clear_irq_status
*cmd_params
;
321 /* prepare command */
322 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_CLEAR_IRQ_STATUS
,
324 cmd_params
= (struct dprc_cmd_clear_irq_status
*)cmd
.params
;
325 cmd_params
->status
= cpu_to_le32(status
);
326 cmd_params
->irq_index
= irq_index
;
328 /* send command to mc*/
329 return mc_send_command(mc_io
, &cmd
);
333 * dprc_get_attributes() - Obtains container attributes
334 * @mc_io: Pointer to MC portal's I/O object
335 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
336 * @token: Token of DPRC object
337 * @attr: Returned container attributes
339 * Return: '0' on Success; Error code otherwise.
341 int dprc_get_attributes(struct fsl_mc_io
*mc_io
,
344 struct dprc_attributes
*attr
)
346 struct fsl_mc_command cmd
= { 0 };
347 struct dprc_rsp_get_attributes
*rsp_params
;
350 /* prepare command */
351 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_GET_ATTR
,
355 /* send command to mc*/
356 err
= mc_send_command(mc_io
, &cmd
);
360 /* retrieve response parameters */
361 rsp_params
= (struct dprc_rsp_get_attributes
*)cmd
.params
;
362 attr
->container_id
= le32_to_cpu(rsp_params
->container_id
);
363 attr
->icid
= le32_to_cpu(rsp_params
->icid
);
364 attr
->options
= le32_to_cpu(rsp_params
->options
);
365 attr
->portal_id
= le32_to_cpu(rsp_params
->portal_id
);
371 * dprc_get_obj_count() - Obtains the number of objects in the DPRC
372 * @mc_io: Pointer to MC portal's I/O object
373 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
374 * @token: Token of DPRC object
375 * @obj_count: Number of objects assigned to the DPRC
377 * Return: '0' on Success; Error code otherwise.
379 int dprc_get_obj_count(struct fsl_mc_io
*mc_io
,
384 struct fsl_mc_command cmd
= { 0 };
385 struct dprc_rsp_get_obj_count
*rsp_params
;
388 /* prepare command */
389 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_GET_OBJ_COUNT
,
392 /* send command to mc*/
393 err
= mc_send_command(mc_io
, &cmd
);
397 /* retrieve response parameters */
398 rsp_params
= (struct dprc_rsp_get_obj_count
*)cmd
.params
;
399 *obj_count
= le32_to_cpu(rsp_params
->obj_count
);
403 EXPORT_SYMBOL_GPL(dprc_get_obj_count
);
406 * dprc_get_obj() - Get general information on an object
407 * @mc_io: Pointer to MC portal's I/O object
408 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
409 * @token: Token of DPRC object
410 * @obj_index: Index of the object to be queried (< obj_count)
411 * @obj_desc: Returns the requested object descriptor
413 * The object descriptors are retrieved one by one by incrementing
414 * obj_index up to (not including) the value of obj_count returned
415 * from dprc_get_obj_count(). dprc_get_obj_count() must
416 * be called prior to dprc_get_obj().
418 * Return: '0' on Success; Error code otherwise.
420 int dprc_get_obj(struct fsl_mc_io
*mc_io
,
424 struct fsl_mc_obj_desc
*obj_desc
)
426 struct fsl_mc_command cmd
= { 0 };
427 struct dprc_cmd_get_obj
*cmd_params
;
428 struct dprc_rsp_get_obj
*rsp_params
;
431 /* prepare command */
432 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_GET_OBJ
,
435 cmd_params
= (struct dprc_cmd_get_obj
*)cmd
.params
;
436 cmd_params
->obj_index
= cpu_to_le32(obj_index
);
438 /* send command to mc*/
439 err
= mc_send_command(mc_io
, &cmd
);
443 /* retrieve response parameters */
444 rsp_params
= (struct dprc_rsp_get_obj
*)cmd
.params
;
445 obj_desc
->id
= le32_to_cpu(rsp_params
->id
);
446 obj_desc
->vendor
= le16_to_cpu(rsp_params
->vendor
);
447 obj_desc
->irq_count
= rsp_params
->irq_count
;
448 obj_desc
->region_count
= rsp_params
->region_count
;
449 obj_desc
->state
= le32_to_cpu(rsp_params
->state
);
450 obj_desc
->ver_major
= le16_to_cpu(rsp_params
->version_major
);
451 obj_desc
->ver_minor
= le16_to_cpu(rsp_params
->version_minor
);
452 obj_desc
->flags
= le16_to_cpu(rsp_params
->flags
);
453 strscpy_pad(obj_desc
->type
, rsp_params
->type
, 16);
454 strscpy_pad(obj_desc
->label
, rsp_params
->label
, 16);
457 EXPORT_SYMBOL_GPL(dprc_get_obj
);
460 * dprc_set_obj_irq() - Set IRQ information for object to trigger an interrupt.
461 * @mc_io: Pointer to MC portal's I/O object
462 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
463 * @token: Token of DPRC object
464 * @obj_type: Type of the object to set its IRQ
465 * @obj_id: ID of the object to set its IRQ
466 * @irq_index: The interrupt index to configure
467 * @irq_cfg: IRQ configuration
469 * Return: '0' on Success; Error code otherwise.
471 int dprc_set_obj_irq(struct fsl_mc_io
*mc_io
,
477 struct dprc_irq_cfg
*irq_cfg
)
479 struct fsl_mc_command cmd
= { 0 };
480 struct dprc_cmd_set_obj_irq
*cmd_params
;
482 /* prepare command */
483 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_SET_OBJ_IRQ
,
486 cmd_params
= (struct dprc_cmd_set_obj_irq
*)cmd
.params
;
487 cmd_params
->irq_val
= cpu_to_le32(irq_cfg
->val
);
488 cmd_params
->irq_index
= irq_index
;
489 cmd_params
->irq_addr
= cpu_to_le64(irq_cfg
->paddr
);
490 cmd_params
->irq_num
= cpu_to_le32(irq_cfg
->irq_num
);
491 cmd_params
->obj_id
= cpu_to_le32(obj_id
);
492 strscpy_pad(cmd_params
->obj_type
, obj_type
, 16);
494 /* send command to mc*/
495 return mc_send_command(mc_io
, &cmd
);
497 EXPORT_SYMBOL_GPL(dprc_set_obj_irq
);
500 * dprc_get_obj_region() - Get region information for a specified object.
501 * @mc_io: Pointer to MC portal's I/O object
502 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
503 * @token: Token of DPRC object
504 * @obj_type: Object type as returned in dprc_get_obj()
505 * @obj_id: Unique object instance as returned in dprc_get_obj()
506 * @region_index: The specific region to query
507 * @region_desc: Returns the requested region descriptor
509 * Return: '0' on Success; Error code otherwise.
511 int dprc_get_obj_region(struct fsl_mc_io
*mc_io
,
517 struct dprc_region_desc
*region_desc
)
519 struct fsl_mc_command cmd
= { 0 };
520 struct dprc_cmd_get_obj_region
*cmd_params
;
521 struct dprc_rsp_get_obj_region
*rsp_params
;
525 * If the DPRC object version was not yet cached, cache it now.
526 * Otherwise use the already cached value.
528 if (!dprc_major_ver
&& !dprc_minor_ver
) {
529 err
= dprc_get_api_version(mc_io
, 0,
536 if (dprc_major_ver
> 6 || (dprc_major_ver
== 6 && dprc_minor_ver
>= 6)) {
538 * MC API version 6.6 changed the size of the MC portals and software
539 * portals to 64K (as implemented by hardware). If older API is in use the
540 * size reported is less (64 bytes for mc portals and 4K for software
544 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_GET_OBJ_REG_V3
,
547 } else if (dprc_major_ver
== 6 && dprc_minor_ver
>= 3) {
549 * MC API version 6.3 introduced a new field to the region
550 * descriptor: base_address. If the older API is in use then the base
551 * address is set to zero to indicate it needs to be obtained elsewhere
552 * (typically the device tree).
554 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_GET_OBJ_REG_V2
,
557 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_GET_OBJ_REG
,
561 cmd_params
= (struct dprc_cmd_get_obj_region
*)cmd
.params
;
562 cmd_params
->obj_id
= cpu_to_le32(obj_id
);
563 cmd_params
->region_index
= region_index
;
564 strscpy_pad(cmd_params
->obj_type
, obj_type
, 16);
566 /* send command to mc*/
567 err
= mc_send_command(mc_io
, &cmd
);
571 /* retrieve response parameters */
572 rsp_params
= (struct dprc_rsp_get_obj_region
*)cmd
.params
;
573 region_desc
->base_offset
= le64_to_cpu(rsp_params
->base_offset
);
574 region_desc
->size
= le32_to_cpu(rsp_params
->size
);
575 region_desc
->type
= rsp_params
->type
;
576 region_desc
->flags
= le32_to_cpu(rsp_params
->flags
);
577 if (dprc_major_ver
> 6 || (dprc_major_ver
== 6 && dprc_minor_ver
>= 3))
578 region_desc
->base_address
= le64_to_cpu(rsp_params
->base_addr
);
580 region_desc
->base_address
= 0;
584 EXPORT_SYMBOL_GPL(dprc_get_obj_region
);
587 * dprc_get_api_version - Get Data Path Resource Container API version
588 * @mc_io: Pointer to Mc portal's I/O object
589 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
590 * @major_ver: Major version of Data Path Resource Container API
591 * @minor_ver: Minor version of Data Path Resource Container API
593 * Return: '0' on Success; Error code otherwise.
595 int dprc_get_api_version(struct fsl_mc_io
*mc_io
,
600 struct fsl_mc_command cmd
= { 0 };
603 /* prepare command */
604 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_GET_API_VERSION
,
607 /* send command to mc */
608 err
= mc_send_command(mc_io
, &cmd
);
612 /* retrieve response parameters */
613 mc_cmd_read_api_version(&cmd
, major_ver
, minor_ver
);
619 * dprc_get_container_id - Get container ID associated with a given portal.
620 * @mc_io: Pointer to Mc portal's I/O object
621 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
622 * @container_id: Requested container id
624 * Return: '0' on Success; Error code otherwise.
626 int dprc_get_container_id(struct fsl_mc_io
*mc_io
,
630 struct fsl_mc_command cmd
= { 0 };
633 /* prepare command */
634 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_GET_CONT_ID
,
638 /* send command to mc*/
639 err
= mc_send_command(mc_io
, &cmd
);
643 /* retrieve response parameters */
644 *container_id
= (int)mc_cmd_read_object_id(&cmd
);
650 * dprc_get_connection() - Get connected endpoint and link status if connection
652 * @mc_io: Pointer to MC portal's I/O object
653 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
654 * @token: Token of DPRC object
655 * @endpoint1: Endpoint 1 configuration parameters
656 * @endpoint2: Returned endpoint 2 configuration parameters
657 * @state: Returned link state:
660 * -1 - no connection (endpoint2 information is irrelevant)
662 * Return: '0' on Success; -ENOTCONN if connection does not exist.
664 int dprc_get_connection(struct fsl_mc_io
*mc_io
,
667 const struct dprc_endpoint
*endpoint1
,
668 struct dprc_endpoint
*endpoint2
,
671 struct dprc_cmd_get_connection
*cmd_params
;
672 struct dprc_rsp_get_connection
*rsp_params
;
673 struct fsl_mc_command cmd
= { 0 };
676 /* prepare command */
677 cmd
.header
= mc_encode_cmd_header(DPRC_CMDID_GET_CONNECTION
,
680 cmd_params
= (struct dprc_cmd_get_connection
*)cmd
.params
;
681 cmd_params
->ep1_id
= cpu_to_le32(endpoint1
->id
);
682 cmd_params
->ep1_interface_id
= cpu_to_le16(endpoint1
->if_id
);
683 for (i
= 0; i
< 16; i
++)
684 cmd_params
->ep1_type
[i
] = endpoint1
->type
[i
];
686 /* send command to mc */
687 err
= mc_send_command(mc_io
, &cmd
);
691 /* retrieve response parameters */
692 rsp_params
= (struct dprc_rsp_get_connection
*)cmd
.params
;
693 endpoint2
->id
= le32_to_cpu(rsp_params
->ep2_id
);
694 endpoint2
->if_id
= le16_to_cpu(rsp_params
->ep2_interface_id
);
695 *state
= le32_to_cpu(rsp_params
->state
);
696 for (i
= 0; i
< 16; i
++)
697 endpoint2
->type
[i
] = rsp_params
->ep2_type
[i
];