// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for the NXP SAA7164 PCIe bridge * * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
*/
#include"saa7164.h"
/* The message bus to/from the firmware is a ring buffer in PCI address * space. Establish the defaults.
*/ int saa7164_bus_setup(struct saa7164_dev *dev)
{ struct tmComResBusInfo *b = &dev->bus;
/* Intensionally throw a BUG() if the state of the message bus looks corrupt */ staticvoid saa7164_bus_verify(struct saa7164_dev *dev)
{ struct tmComResBusInfo *b = &dev->bus; int bug = 0;
if (saa7164_readl(b->m_dwSetReadPos) > b->m_dwSizeSetRing)
bug++;
if (saa7164_readl(b->m_dwSetWritePos) > b->m_dwSizeSetRing)
bug++;
if (saa7164_readl(b->m_dwGetReadPos) > b->m_dwSizeGetRing)
bug++;
if (saa7164_readl(b->m_dwGetWritePos) > b->m_dwSizeGetRing)
bug++;
if (bug) {
saa_debug = 0xffff; /* Ensure we get the bus dump */
saa7164_bus_dump(dev);
saa_debug = 1024; /* Ensure we get the bus dump */
BUG();
}
}
/* * Places a command or a response on the bus. The implementation does not * know if it is a command or a response it just places the data on the * bus depending on the bus information given in the struct tmComResBusInfo * structure. If the command or response does not fit into the bus ring * buffer it will be refused. * * Return Value: * SAA_OK The function executed successfully. * < 0 One or more members are not initialized.
*/ int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg, void *buf)
{ struct tmComResBusInfo *bus = &dev->bus;
u32 bytes_to_write, free_write_space, timeout, curr_srp, curr_swp;
u32 new_swp, space_rem; int ret = SAA_ERR_BAD_PARAMETER;
u16 size;
if (!msg) {
printk(KERN_ERR "%s() !msg\n", __func__); return SAA_ERR_BAD_PARAMETER;
}
/* Deal with ring wrapping issues */ if (curr_srp > curr_swp) /* Deal with the wrapped ring */
free_write_space = curr_srp - curr_swp; else /* The ring has not wrapped yet */
free_write_space = (curr_srp + bus->m_dwSizeSetRing) - curr_swp;
/* Process the msg and write the content onto the bus */ while (bytes_to_write >= free_write_space) {
if (timeout-- == 0) {
printk(KERN_ERR "%s() bus timeout\n", __func__);
ret = SAA_ERR_NO_RESOURCES; goto out;
}
/* TODO: Review this delay, efficient? */ /* Wait, allowing the hardware fetch time */
mdelay(1);
/* Check the space usage again */
curr_srp = saa7164_readl(bus->m_dwSetReadPos);
/* Deal with ring wrapping issues */ if (curr_srp > curr_swp) /* Deal with the wrapped ring */
free_write_space = curr_srp - curr_swp; else /* Read didn't wrap around the buffer */
free_write_space = (curr_srp + bus->m_dwSizeSetRing) -
curr_swp;
}
/* Calculate the new write position */
new_swp = curr_swp + bytes_to_write;
/* * Make a copy of msg->size before it is converted to le16 since it is * used in the code below.
*/
size = msg->size; /* Convert to le16/le32 */
msg->size = (__force u16)cpu_to_le16(msg->size);
msg->command = (__force u32)cpu_to_le32(msg->command);
msg->controlselector = (__force u16)cpu_to_le16(msg->controlselector);
/* Mental Note: line 462 tmmhComResBusPCIe.cpp */
/* Check if we're going to wrap again */ if (new_swp > bus->m_dwSizeSetRing) {
if (space_rem < sizeof(*msg)) {
dprintk(DBGLVL_BUS, "%s() tr4\n", __func__);
/* Split the msg into pieces as the ring wraps */
memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, space_rem);
memcpy_toio(bus->m_pdwSetRing, (u8 *)msg + space_rem, sizeof(*msg) - space_rem);
/* Additional data at the beginning of the ring */
memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
memcpy_toio(bus->m_pdwSetRing, buf, size);
} else { /* Additional data wraps around the ring */
memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg)); if (size > 0) {
memcpy_toio(bus->m_pdwSetRing + curr_swp + sizeof(*msg), buf, space_rem - sizeof(*msg));
memcpy_toio(bus->m_pdwSetRing, (u8 *)buf +
space_rem - sizeof(*msg),
bytes_to_write - space_rem);
}
/* Update the bus write position */
saa7164_writel(bus->m_dwSetWritePos, new_swp);
/* Convert back to cpu after writing the msg to the ringbuffer. */
msg->size = le16_to_cpu((__force __le16)msg->size);
msg->command = le32_to_cpu((__force __le32)msg->command);
msg->controlselector = le16_to_cpu((__force __le16)msg->controlselector);
ret = SAA_OK;
/* * Receive a command or a response from the bus. The implementation does not * know if it is a command or a response it simply dequeues the data, * depending on the bus information given in the struct tmComResBusInfo * structure. * * Return Value: * 0 The function executed successfully. * < 0 One or more members are not initialized.
*/ int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg, void *buf, int peekonly)
{ struct tmComResBusInfo *bus = &dev->bus;
u32 bytes_to_read, write_distance, curr_grp, curr_gwp,
new_grp, buf_size, space_rem; struct tmComResInfo msg_tmp; int ret = SAA_ERR_BAD_PARAMETER;
if ((peekonly == 0) && (msg->size > 0) && (buf == NULL)) {
printk(KERN_ERR "%s() Missing msg buf, size should be %d bytes\n",
__func__, msg->size); return ret;
}
mutex_lock(&bus->lock);
/* Peek the bus to see if a msg exists, if it's not what we're expecting * then return cleanly else read the message from the bus.
*/
curr_gwp = saa7164_readl(bus->m_dwGetWritePos);
curr_grp = saa7164_readl(bus->m_dwGetReadPos);
if (curr_gwp == curr_grp) {
ret = SAA_ERR_EMPTY; goto out;
}
bytes_to_read = sizeof(*msg);
/* Calculate write distance to current read position */
write_distance = 0; if (curr_gwp >= curr_grp) /* Write doesn't wrap around the ring */
write_distance = curr_gwp - curr_grp; else /* Write wraps around the ring */
write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
if (bytes_to_read > write_distance) {
printk(KERN_ERR "%s() No message/response found\n", __func__);
ret = SAA_ERR_INVALID_COMMAND; goto out;
}
/* Calculate the new read position */
new_grp = curr_grp + bytes_to_read; if (new_grp > bus->m_dwSizeGetRing) {
} else { /* No wrapping */
memcpy_fromio(&msg_tmp, bus->m_pdwGetRing + curr_grp, bytes_to_read);
} /* Convert from little endian to CPU */
msg_tmp.size = le16_to_cpu((__force __le16)msg_tmp.size);
msg_tmp.command = le32_to_cpu((__force __le32)msg_tmp.command);
msg_tmp.controlselector = le16_to_cpu((__force __le16)msg_tmp.controlselector);
memcpy(msg, &msg_tmp, sizeof(*msg));
/* No need to update the read positions, because this was a peek */ /* If the caller specifically want to peek, return */ if (peekonly) { goto peekout;
}
/* Check if the command/response matches what is expected */ if ((msg_tmp.id != msg->id) || (msg_tmp.command != msg->command) ||
(msg_tmp.controlselector != msg->controlselector) ||
(msg_tmp.seqno != msg->seqno) || (msg_tmp.size != msg->size)) {
/* Get the actual command and response from the bus */
buf_size = msg->size;
bytes_to_read = sizeof(*msg) + msg->size; /* Calculate write distance to current read position */
write_distance = 0; if (curr_gwp >= curr_grp) /* Write doesn't wrap around the ring */
write_distance = curr_gwp - curr_grp; else /* Write wraps around the ring */
write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
if (bytes_to_read > write_distance) {
printk(KERN_ERR "%s() Invalid bus state, missing msg or mangled ring, faulty H/W / bad code?\n",
__func__);
ret = SAA_ERR_INVALID_COMMAND; goto out;
}
/* Calculate the new read position */
new_grp = curr_grp + bytes_to_read; if (new_grp > bus->m_dwSizeGetRing) {
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.