// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)1/*2* Copyright 2013-2016 Freescale Semiconductor Inc.3* Copyright 2020 NXP4*5*/6#include <linux/kernel.h>7#include <linux/fsl/mc.h>89#include "fsl-mc-private.h"1011/*12* cache the DPRC version to reduce the number of commands13* towards the mc firmware14*/15static u16 dprc_major_ver;16static u16 dprc_minor_ver;1718/**19* dprc_open() - Open DPRC object for use20* @mc_io: Pointer to MC portal's I/O object21* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'22* @container_id: Container ID to open23* @token: Returned token of DPRC object24*25* Return: '0' on Success; Error code otherwise.26*27* @warning Required before any operation on the object.28*/29int dprc_open(struct fsl_mc_io *mc_io,30u32 cmd_flags,31int container_id,32u16 *token)33{34struct fsl_mc_command cmd = { 0 };35struct dprc_cmd_open *cmd_params;36int err;3738/* prepare command */39cmd.header = mc_encode_cmd_header(DPRC_CMDID_OPEN, cmd_flags,400);41cmd_params = (struct dprc_cmd_open *)cmd.params;42cmd_params->container_id = cpu_to_le32(container_id);4344/* send command to mc*/45err = mc_send_command(mc_io, &cmd);46if (err)47return err;4849/* retrieve response parameters */50*token = mc_cmd_hdr_read_token(&cmd);5152return 0;53}54EXPORT_SYMBOL_GPL(dprc_open);5556/**57* dprc_close() - Close the control session of the object58* @mc_io: Pointer to MC portal's I/O object59* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'60* @token: Token of DPRC object61*62* After this function is called, no further operations are63* allowed on the object without opening a new control session.64*65* Return: '0' on Success; Error code otherwise.66*/67int dprc_close(struct fsl_mc_io *mc_io,68u32 cmd_flags,69u16 token)70{71struct fsl_mc_command cmd = { 0 };7273/* prepare command */74cmd.header = mc_encode_cmd_header(DPRC_CMDID_CLOSE, cmd_flags,75token);7677/* send command to mc*/78return mc_send_command(mc_io, &cmd);79}80EXPORT_SYMBOL_GPL(dprc_close);8182/**83* dprc_reset_container - Reset child container.84* @mc_io: Pointer to MC portal's I/O object85* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'86* @token: Token of DPRC object87* @child_container_id: ID of the container to reset88* @options: 32 bit options:89* - 0 (no bits set) - all the objects inside the container are90* reset. The child containers are entered recursively and the91* objects reset. All the objects (including the child containers)92* are closed.93* - bit 0 set - all the objects inside the container are reset.94* However the child containers are not entered recursively.95* This option is supported for API versions >= 6.596* In case a software context crashes or becomes non-responsive, the parent97* may wish to reset its resources container before the software context is98* restarted.99*100* This routine informs all objects assigned to the child container that the101* container is being reset, so they may perform any cleanup operations that are102* needed. All objects handles that were owned by the child container shall be103* closed.104*105* Note that such request may be submitted even if the child software context106* has not crashed, but the resulting object cleanup operations will not be107* aware of that.108*109* Return: '0' on Success; Error code otherwise.110*/111int dprc_reset_container(struct fsl_mc_io *mc_io,112u32 cmd_flags,113u16 token,114int child_container_id,115u32 options)116{117struct fsl_mc_command cmd = { 0 };118struct dprc_cmd_reset_container *cmd_params;119u32 cmdid = DPRC_CMDID_RESET_CONT;120int err;121122/*123* If the DPRC object version was not yet cached, cache it now.124* Otherwise use the already cached value.125*/126if (!dprc_major_ver && !dprc_minor_ver) {127err = dprc_get_api_version(mc_io, 0,128&dprc_major_ver,129&dprc_minor_ver);130if (err)131return err;132}133134/*135* MC API 6.5 introduced a new field in the command used to pass136* some flags.137* Bit 0 indicates that the child containers are not recursively reset.138*/139if (dprc_major_ver > 6 || (dprc_major_ver == 6 && dprc_minor_ver >= 5))140cmdid = DPRC_CMDID_RESET_CONT_V2;141142/* prepare command */143cmd.header = mc_encode_cmd_header(cmdid, cmd_flags, token);144cmd_params = (struct dprc_cmd_reset_container *)cmd.params;145cmd_params->child_container_id = cpu_to_le32(child_container_id);146cmd_params->options = cpu_to_le32(options);147148/* send command to mc*/149return mc_send_command(mc_io, &cmd);150}151EXPORT_SYMBOL_GPL(dprc_reset_container);152153/**154* dprc_set_irq() - Set IRQ information for the DPRC to trigger an interrupt.155* @mc_io: Pointer to MC portal's I/O object156* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'157* @token: Token of DPRC object158* @irq_index: Identifies the interrupt index to configure159* @irq_cfg: IRQ configuration160*161* Return: '0' on Success; Error code otherwise.162*/163int dprc_set_irq(struct fsl_mc_io *mc_io,164u32 cmd_flags,165u16 token,166u8 irq_index,167struct dprc_irq_cfg *irq_cfg)168{169struct fsl_mc_command cmd = { 0 };170struct dprc_cmd_set_irq *cmd_params;171172/* prepare command */173cmd.header = mc_encode_cmd_header(DPRC_CMDID_SET_IRQ,174cmd_flags,175token);176cmd_params = (struct dprc_cmd_set_irq *)cmd.params;177cmd_params->irq_val = cpu_to_le32(irq_cfg->val);178cmd_params->irq_index = irq_index;179cmd_params->irq_addr = cpu_to_le64(irq_cfg->paddr);180cmd_params->irq_num = cpu_to_le32(irq_cfg->irq_num);181182/* send command to mc*/183return mc_send_command(mc_io, &cmd);184}185186/**187* dprc_set_irq_enable() - Set overall interrupt state.188* @mc_io: Pointer to MC portal's I/O object189* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'190* @token: Token of DPRC object191* @irq_index: The interrupt index to configure192* @en: Interrupt state - enable = 1, disable = 0193*194* Allows GPP software to control when interrupts are generated.195* Each interrupt can have up to 32 causes. The enable/disable control's the196* overall interrupt state. if the interrupt is disabled no causes will cause197* an interrupt.198*199* Return: '0' on Success; Error code otherwise.200*/201int dprc_set_irq_enable(struct fsl_mc_io *mc_io,202u32 cmd_flags,203u16 token,204u8 irq_index,205u8 en)206{207struct fsl_mc_command cmd = { 0 };208struct dprc_cmd_set_irq_enable *cmd_params;209210/* prepare command */211cmd.header = mc_encode_cmd_header(DPRC_CMDID_SET_IRQ_ENABLE,212cmd_flags, token);213cmd_params = (struct dprc_cmd_set_irq_enable *)cmd.params;214cmd_params->enable = en & DPRC_ENABLE;215cmd_params->irq_index = irq_index;216217/* send command to mc*/218return mc_send_command(mc_io, &cmd);219}220221/**222* dprc_set_irq_mask() - Set interrupt mask.223* @mc_io: Pointer to MC portal's I/O object224* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'225* @token: Token of DPRC object226* @irq_index: The interrupt index to configure227* @mask: event mask to trigger interrupt;228* each bit:229* 0 = ignore event230* 1 = consider event for asserting irq231*232* Every interrupt can have up to 32 causes and the interrupt model supports233* masking/unmasking each cause independently234*235* Return: '0' on Success; Error code otherwise.236*/237int dprc_set_irq_mask(struct fsl_mc_io *mc_io,238u32 cmd_flags,239u16 token,240u8 irq_index,241u32 mask)242{243struct fsl_mc_command cmd = { 0 };244struct dprc_cmd_set_irq_mask *cmd_params;245246/* prepare command */247cmd.header = mc_encode_cmd_header(DPRC_CMDID_SET_IRQ_MASK,248cmd_flags, token);249cmd_params = (struct dprc_cmd_set_irq_mask *)cmd.params;250cmd_params->mask = cpu_to_le32(mask);251cmd_params->irq_index = irq_index;252253/* send command to mc*/254return mc_send_command(mc_io, &cmd);255}256257/**258* dprc_get_irq_status() - Get the current status of any pending interrupts.259* @mc_io: Pointer to MC portal's I/O object260* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'261* @token: Token of DPRC object262* @irq_index: The interrupt index to configure263* @status: Returned interrupts status - one bit per cause:264* 0 = no interrupt pending265* 1 = interrupt pending266*267* Return: '0' on Success; Error code otherwise.268*/269int dprc_get_irq_status(struct fsl_mc_io *mc_io,270u32 cmd_flags,271u16 token,272u8 irq_index,273u32 *status)274{275struct fsl_mc_command cmd = { 0 };276struct dprc_cmd_get_irq_status *cmd_params;277struct dprc_rsp_get_irq_status *rsp_params;278int err;279280/* prepare command */281cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_IRQ_STATUS,282cmd_flags, token);283cmd_params = (struct dprc_cmd_get_irq_status *)cmd.params;284cmd_params->status = cpu_to_le32(*status);285cmd_params->irq_index = irq_index;286287/* send command to mc*/288err = mc_send_command(mc_io, &cmd);289if (err)290return err;291292/* retrieve response parameters */293rsp_params = (struct dprc_rsp_get_irq_status *)cmd.params;294*status = le32_to_cpu(rsp_params->status);295296return 0;297}298299/**300* dprc_clear_irq_status() - Clear a pending interrupt's status301* @mc_io: Pointer to MC portal's I/O object302* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'303* @token: Token of DPRC object304* @irq_index: The interrupt index to configure305* @status: bits to clear (W1C) - one bit per cause:306* 0 = don't change307* 1 = clear status bit308*309* Return: '0' on Success; Error code otherwise.310*/311int dprc_clear_irq_status(struct fsl_mc_io *mc_io,312u32 cmd_flags,313u16 token,314u8 irq_index,315u32 status)316{317struct fsl_mc_command cmd = { 0 };318struct dprc_cmd_clear_irq_status *cmd_params;319320/* prepare command */321cmd.header = mc_encode_cmd_header(DPRC_CMDID_CLEAR_IRQ_STATUS,322cmd_flags, token);323cmd_params = (struct dprc_cmd_clear_irq_status *)cmd.params;324cmd_params->status = cpu_to_le32(status);325cmd_params->irq_index = irq_index;326327/* send command to mc*/328return mc_send_command(mc_io, &cmd);329}330331/**332* dprc_get_attributes() - Obtains container attributes333* @mc_io: Pointer to MC portal's I/O object334* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'335* @token: Token of DPRC object336* @attr: Returned container attributes337*338* Return: '0' on Success; Error code otherwise.339*/340int dprc_get_attributes(struct fsl_mc_io *mc_io,341u32 cmd_flags,342u16 token,343struct dprc_attributes *attr)344{345struct fsl_mc_command cmd = { 0 };346struct dprc_rsp_get_attributes *rsp_params;347int err;348349/* prepare command */350cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_ATTR,351cmd_flags,352token);353354/* send command to mc*/355err = mc_send_command(mc_io, &cmd);356if (err)357return err;358359/* retrieve response parameters */360rsp_params = (struct dprc_rsp_get_attributes *)cmd.params;361attr->container_id = le32_to_cpu(rsp_params->container_id);362attr->icid = le32_to_cpu(rsp_params->icid);363attr->options = le32_to_cpu(rsp_params->options);364attr->portal_id = le32_to_cpu(rsp_params->portal_id);365366return 0;367}368369/**370* dprc_get_obj_count() - Obtains the number of objects in the DPRC371* @mc_io: Pointer to MC portal's I/O object372* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'373* @token: Token of DPRC object374* @obj_count: Number of objects assigned to the DPRC375*376* Return: '0' on Success; Error code otherwise.377*/378int dprc_get_obj_count(struct fsl_mc_io *mc_io,379u32 cmd_flags,380u16 token,381int *obj_count)382{383struct fsl_mc_command cmd = { 0 };384struct dprc_rsp_get_obj_count *rsp_params;385int err;386387/* prepare command */388cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_OBJ_COUNT,389cmd_flags, token);390391/* send command to mc*/392err = mc_send_command(mc_io, &cmd);393if (err)394return err;395396/* retrieve response parameters */397rsp_params = (struct dprc_rsp_get_obj_count *)cmd.params;398*obj_count = le32_to_cpu(rsp_params->obj_count);399400return 0;401}402EXPORT_SYMBOL_GPL(dprc_get_obj_count);403404/**405* dprc_get_obj() - Get general information on an object406* @mc_io: Pointer to MC portal's I/O object407* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'408* @token: Token of DPRC object409* @obj_index: Index of the object to be queried (< obj_count)410* @obj_desc: Returns the requested object descriptor411*412* The object descriptors are retrieved one by one by incrementing413* obj_index up to (not including) the value of obj_count returned414* from dprc_get_obj_count(). dprc_get_obj_count() must415* be called prior to dprc_get_obj().416*417* Return: '0' on Success; Error code otherwise.418*/419int dprc_get_obj(struct fsl_mc_io *mc_io,420u32 cmd_flags,421u16 token,422int obj_index,423struct fsl_mc_obj_desc *obj_desc)424{425struct fsl_mc_command cmd = { 0 };426struct dprc_cmd_get_obj *cmd_params;427struct dprc_rsp_get_obj *rsp_params;428int err;429430/* prepare command */431cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_OBJ,432cmd_flags,433token);434cmd_params = (struct dprc_cmd_get_obj *)cmd.params;435cmd_params->obj_index = cpu_to_le32(obj_index);436437/* send command to mc*/438err = mc_send_command(mc_io, &cmd);439if (err)440return err;441442/* retrieve response parameters */443rsp_params = (struct dprc_rsp_get_obj *)cmd.params;444obj_desc->id = le32_to_cpu(rsp_params->id);445obj_desc->vendor = le16_to_cpu(rsp_params->vendor);446obj_desc->irq_count = rsp_params->irq_count;447obj_desc->region_count = rsp_params->region_count;448obj_desc->state = le32_to_cpu(rsp_params->state);449obj_desc->ver_major = le16_to_cpu(rsp_params->version_major);450obj_desc->ver_minor = le16_to_cpu(rsp_params->version_minor);451obj_desc->flags = le16_to_cpu(rsp_params->flags);452strscpy_pad(obj_desc->type, rsp_params->type, 16);453strscpy_pad(obj_desc->label, rsp_params->label, 16);454return 0;455}456EXPORT_SYMBOL_GPL(dprc_get_obj);457458/**459* dprc_set_obj_irq() - Set IRQ information for object to trigger an interrupt.460* @mc_io: Pointer to MC portal's I/O object461* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'462* @token: Token of DPRC object463* @obj_type: Type of the object to set its IRQ464* @obj_id: ID of the object to set its IRQ465* @irq_index: The interrupt index to configure466* @irq_cfg: IRQ configuration467*468* Return: '0' on Success; Error code otherwise.469*/470int dprc_set_obj_irq(struct fsl_mc_io *mc_io,471u32 cmd_flags,472u16 token,473char *obj_type,474int obj_id,475u8 irq_index,476struct dprc_irq_cfg *irq_cfg)477{478struct fsl_mc_command cmd = { 0 };479struct dprc_cmd_set_obj_irq *cmd_params;480481/* prepare command */482cmd.header = mc_encode_cmd_header(DPRC_CMDID_SET_OBJ_IRQ,483cmd_flags,484token);485cmd_params = (struct dprc_cmd_set_obj_irq *)cmd.params;486cmd_params->irq_val = cpu_to_le32(irq_cfg->val);487cmd_params->irq_index = irq_index;488cmd_params->irq_addr = cpu_to_le64(irq_cfg->paddr);489cmd_params->irq_num = cpu_to_le32(irq_cfg->irq_num);490cmd_params->obj_id = cpu_to_le32(obj_id);491strscpy(cmd_params->obj_type, obj_type);492493/* send command to mc*/494return mc_send_command(mc_io, &cmd);495}496EXPORT_SYMBOL_GPL(dprc_set_obj_irq);497498/**499* dprc_get_obj_region() - Get region information for a specified object.500* @mc_io: Pointer to MC portal's I/O object501* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'502* @token: Token of DPRC object503* @obj_type: Object type as returned in dprc_get_obj()504* @obj_id: Unique object instance as returned in dprc_get_obj()505* @region_index: The specific region to query506* @region_desc: Returns the requested region descriptor507*508* Return: '0' on Success; Error code otherwise.509*/510int dprc_get_obj_region(struct fsl_mc_io *mc_io,511u32 cmd_flags,512u16 token,513char *obj_type,514int obj_id,515u8 region_index,516struct dprc_region_desc *region_desc)517{518struct fsl_mc_command cmd = { 0 };519struct dprc_cmd_get_obj_region *cmd_params;520struct dprc_rsp_get_obj_region *rsp_params;521int err;522523/*524* If the DPRC object version was not yet cached, cache it now.525* Otherwise use the already cached value.526*/527if (!dprc_major_ver && !dprc_minor_ver) {528err = dprc_get_api_version(mc_io, 0,529&dprc_major_ver,530&dprc_minor_ver);531if (err)532return err;533}534535if (dprc_major_ver > 6 || (dprc_major_ver == 6 && dprc_minor_ver >= 6)) {536/*537* MC API version 6.6 changed the size of the MC portals and software538* portals to 64K (as implemented by hardware). If older API is in use the539* size reported is less (64 bytes for mc portals and 4K for software540* portals).541*/542543cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_OBJ_REG_V3,544cmd_flags, token);545546} else if (dprc_major_ver == 6 && dprc_minor_ver >= 3) {547/*548* MC API version 6.3 introduced a new field to the region549* descriptor: base_address. If the older API is in use then the base550* address is set to zero to indicate it needs to be obtained elsewhere551* (typically the device tree).552*/553cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_OBJ_REG_V2,554cmd_flags, token);555} else {556cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_OBJ_REG,557cmd_flags, token);558}559560cmd_params = (struct dprc_cmd_get_obj_region *)cmd.params;561cmd_params->obj_id = cpu_to_le32(obj_id);562cmd_params->region_index = region_index;563strscpy(cmd_params->obj_type, obj_type);564565/* send command to mc*/566err = mc_send_command(mc_io, &cmd);567if (err)568return err;569570/* retrieve response parameters */571rsp_params = (struct dprc_rsp_get_obj_region *)cmd.params;572region_desc->base_offset = le64_to_cpu(rsp_params->base_offset);573region_desc->size = le32_to_cpu(rsp_params->size);574region_desc->type = rsp_params->type;575region_desc->flags = le32_to_cpu(rsp_params->flags);576if (dprc_major_ver > 6 || (dprc_major_ver == 6 && dprc_minor_ver >= 3))577region_desc->base_address = le64_to_cpu(rsp_params->base_addr);578else579region_desc->base_address = 0;580581return 0;582}583EXPORT_SYMBOL_GPL(dprc_get_obj_region);584585/**586* dprc_get_api_version - Get Data Path Resource Container API version587* @mc_io: Pointer to Mc portal's I/O object588* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'589* @major_ver: Major version of Data Path Resource Container API590* @minor_ver: Minor version of Data Path Resource Container API591*592* Return: '0' on Success; Error code otherwise.593*/594int dprc_get_api_version(struct fsl_mc_io *mc_io,595u32 cmd_flags,596u16 *major_ver,597u16 *minor_ver)598{599struct fsl_mc_command cmd = { 0 };600int err;601602/* prepare command */603cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_API_VERSION,604cmd_flags, 0);605606/* send command to mc */607err = mc_send_command(mc_io, &cmd);608if (err)609return err;610611/* retrieve response parameters */612mc_cmd_read_api_version(&cmd, major_ver, minor_ver);613614return 0;615}616617/**618* dprc_get_container_id - Get container ID associated with a given portal.619* @mc_io: Pointer to Mc portal's I/O object620* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'621* @container_id: Requested container id622*623* Return: '0' on Success; Error code otherwise.624*/625int dprc_get_container_id(struct fsl_mc_io *mc_io,626u32 cmd_flags,627int *container_id)628{629struct fsl_mc_command cmd = { 0 };630int err;631632/* prepare command */633cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_CONT_ID,634cmd_flags,6350);636637/* send command to mc*/638err = mc_send_command(mc_io, &cmd);639if (err)640return err;641642/* retrieve response parameters */643*container_id = (int)mc_cmd_read_object_id(&cmd);644645return 0;646}647648/**649* dprc_get_connection() - Get connected endpoint and link status if connection650* exists.651* @mc_io: Pointer to MC portal's I/O object652* @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'653* @token: Token of DPRC object654* @endpoint1: Endpoint 1 configuration parameters655* @endpoint2: Returned endpoint 2 configuration parameters656* @state: Returned link state:657* 1 - link is up;658* 0 - link is down;659* -1 - no connection (endpoint2 information is irrelevant)660*661* Return: '0' on Success; -ENOTCONN if connection does not exist.662*/663int dprc_get_connection(struct fsl_mc_io *mc_io,664u32 cmd_flags,665u16 token,666const struct dprc_endpoint *endpoint1,667struct dprc_endpoint *endpoint2,668int *state)669{670struct dprc_cmd_get_connection *cmd_params;671struct dprc_rsp_get_connection *rsp_params;672struct fsl_mc_command cmd = { 0 };673int err, i;674675/* prepare command */676cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_CONNECTION,677cmd_flags,678token);679cmd_params = (struct dprc_cmd_get_connection *)cmd.params;680cmd_params->ep1_id = cpu_to_le32(endpoint1->id);681cmd_params->ep1_interface_id = cpu_to_le16(endpoint1->if_id);682for (i = 0; i < 16; i++)683cmd_params->ep1_type[i] = endpoint1->type[i];684685/* send command to mc */686err = mc_send_command(mc_io, &cmd);687if (err)688return -ENOTCONN;689690/* retrieve response parameters */691rsp_params = (struct dprc_rsp_get_connection *)cmd.params;692endpoint2->id = le32_to_cpu(rsp_params->ep2_id);693endpoint2->if_id = le16_to_cpu(rsp_params->ep2_interface_id);694*state = le32_to_cpu(rsp_params->state);695for (i = 0; i < 16; i++)696endpoint2->type[i] = rsp_params->ep2_type[i];697698return 0;699}700701702