saa7164-bus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. */
  17. #include "saa7164.h"
  18. /* The message bus to/from the firmware is a ring buffer in PCI address
  19. * space. Establish the defaults.
  20. */
  21. int saa7164_bus_setup(struct saa7164_dev *dev)
  22. {
  23. struct tmComResBusInfo *b = &dev->bus;
  24. mutex_init(&b->lock);
  25. b->Type = TYPE_BUS_PCIe;
  26. b->m_wMaxReqSize = SAA_DEVICE_MAXREQUESTSIZE;
  27. b->m_pdwSetRing = (u8 __iomem *)(dev->bmmio +
  28. ((u32)dev->busdesc.CommandRing));
  29. b->m_dwSizeSetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
  30. b->m_pdwGetRing = (u8 __iomem *)(dev->bmmio +
  31. ((u32)dev->busdesc.ResponseRing));
  32. b->m_dwSizeGetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
  33. b->m_dwSetWritePos = ((u32)dev->intfdesc.BARLocation) +
  34. (2 * sizeof(u64));
  35. b->m_dwSetReadPos = b->m_dwSetWritePos + (1 * sizeof(u32));
  36. b->m_dwGetWritePos = b->m_dwSetWritePos + (2 * sizeof(u32));
  37. b->m_dwGetReadPos = b->m_dwSetWritePos + (3 * sizeof(u32));
  38. return 0;
  39. }
  40. void saa7164_bus_dump(struct saa7164_dev *dev)
  41. {
  42. struct tmComResBusInfo *b = &dev->bus;
  43. dprintk(DBGLVL_BUS, "Dumping the bus structure:\n");
  44. dprintk(DBGLVL_BUS, " .type = %d\n", b->Type);
  45. dprintk(DBGLVL_BUS, " .dev->bmmio = 0x%p\n", dev->bmmio);
  46. dprintk(DBGLVL_BUS, " .m_wMaxReqSize = 0x%x\n", b->m_wMaxReqSize);
  47. dprintk(DBGLVL_BUS, " .m_pdwSetRing = 0x%p\n", b->m_pdwSetRing);
  48. dprintk(DBGLVL_BUS, " .m_dwSizeSetRing = 0x%x\n", b->m_dwSizeSetRing);
  49. dprintk(DBGLVL_BUS, " .m_pdwGetRing = 0x%p\n", b->m_pdwGetRing);
  50. dprintk(DBGLVL_BUS, " .m_dwSizeGetRing = 0x%x\n", b->m_dwSizeGetRing);
  51. dprintk(DBGLVL_BUS, " .m_dwSetReadPos = 0x%x (0x%08x)\n",
  52. b->m_dwSetReadPos, saa7164_readl(b->m_dwSetReadPos));
  53. dprintk(DBGLVL_BUS, " .m_dwSetWritePos = 0x%x (0x%08x)\n",
  54. b->m_dwSetWritePos, saa7164_readl(b->m_dwSetWritePos));
  55. dprintk(DBGLVL_BUS, " .m_dwGetReadPos = 0x%x (0x%08x)\n",
  56. b->m_dwGetReadPos, saa7164_readl(b->m_dwGetReadPos));
  57. dprintk(DBGLVL_BUS, " .m_dwGetWritePos = 0x%x (0x%08x)\n",
  58. b->m_dwGetWritePos, saa7164_readl(b->m_dwGetWritePos));
  59. }
  60. /* Intensionally throw a BUG() if the state of the message bus looks corrupt */
  61. static void saa7164_bus_verify(struct saa7164_dev *dev)
  62. {
  63. struct tmComResBusInfo *b = &dev->bus;
  64. int bug = 0;
  65. if (saa7164_readl(b->m_dwSetReadPos) > b->m_dwSizeSetRing)
  66. bug++;
  67. if (saa7164_readl(b->m_dwSetWritePos) > b->m_dwSizeSetRing)
  68. bug++;
  69. if (saa7164_readl(b->m_dwGetReadPos) > b->m_dwSizeGetRing)
  70. bug++;
  71. if (saa7164_readl(b->m_dwGetWritePos) > b->m_dwSizeGetRing)
  72. bug++;
  73. if (bug) {
  74. saa_debug = 0xffff; /* Ensure we get the bus dump */
  75. saa7164_bus_dump(dev);
  76. saa_debug = 1024; /* Ensure we get the bus dump */
  77. BUG();
  78. }
  79. }
  80. static void saa7164_bus_dumpmsg(struct saa7164_dev *dev, struct tmComResInfo *m,
  81. void *buf)
  82. {
  83. dprintk(DBGLVL_BUS, "Dumping msg structure:\n");
  84. dprintk(DBGLVL_BUS, " .id = %d\n", m->id);
  85. dprintk(DBGLVL_BUS, " .flags = 0x%x\n", m->flags);
  86. dprintk(DBGLVL_BUS, " .size = 0x%x\n", m->size);
  87. dprintk(DBGLVL_BUS, " .command = 0x%x\n", m->command);
  88. dprintk(DBGLVL_BUS, " .controlselector = 0x%x\n", m->controlselector);
  89. dprintk(DBGLVL_BUS, " .seqno = %d\n", m->seqno);
  90. if (buf)
  91. dprintk(DBGLVL_BUS, " .buffer (ignored)\n");
  92. }
  93. /*
  94. * Places a command or a response on the bus. The implementation does not
  95. * know if it is a command or a response it just places the data on the
  96. * bus depending on the bus information given in the struct tmComResBusInfo
  97. * structure. If the command or response does not fit into the bus ring
  98. * buffer it will be refused.
  99. *
  100. * Return Value:
  101. * SAA_OK The function executed successfully.
  102. * < 0 One or more members are not initialized.
  103. */
  104. int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
  105. void *buf)
  106. {
  107. struct tmComResBusInfo *bus = &dev->bus;
  108. u32 bytes_to_write, free_write_space, timeout, curr_srp, curr_swp;
  109. u32 new_swp, space_rem;
  110. int ret = SAA_ERR_BAD_PARAMETER;
  111. u16 size;
  112. if (!msg) {
  113. printk(KERN_ERR "%s() !msg\n", __func__);
  114. return SAA_ERR_BAD_PARAMETER;
  115. }
  116. dprintk(DBGLVL_BUS, "%s()\n", __func__);
  117. saa7164_bus_verify(dev);
  118. if (msg->size > dev->bus.m_wMaxReqSize) {
  119. printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
  120. __func__);
  121. return SAA_ERR_BAD_PARAMETER;
  122. }
  123. if ((msg->size > 0) && (buf == NULL)) {
  124. printk(KERN_ERR "%s() Missing message buffer\n", __func__);
  125. return SAA_ERR_BAD_PARAMETER;
  126. }
  127. /* Lock the bus from any other access */
  128. mutex_lock(&bus->lock);
  129. bytes_to_write = sizeof(*msg) + msg->size;
  130. free_write_space = 0;
  131. timeout = SAA_BUS_TIMEOUT;
  132. curr_srp = saa7164_readl(bus->m_dwSetReadPos);
  133. curr_swp = saa7164_readl(bus->m_dwSetWritePos);
  134. /* Deal with ring wrapping issues */
  135. if (curr_srp > curr_swp)
  136. /* Deal with the wrapped ring */
  137. free_write_space = curr_srp - curr_swp;
  138. else
  139. /* The ring has not wrapped yet */
  140. free_write_space = (curr_srp + bus->m_dwSizeSetRing) - curr_swp;
  141. dprintk(DBGLVL_BUS, "%s() bytes_to_write = %d\n", __func__,
  142. bytes_to_write);
  143. dprintk(DBGLVL_BUS, "%s() free_write_space = %d\n", __func__,
  144. free_write_space);
  145. dprintk(DBGLVL_BUS, "%s() curr_srp = %x\n", __func__, curr_srp);
  146. dprintk(DBGLVL_BUS, "%s() curr_swp = %x\n", __func__, curr_swp);
  147. /* Process the msg and write the content onto the bus */
  148. while (bytes_to_write >= free_write_space) {
  149. if (timeout-- == 0) {
  150. printk(KERN_ERR "%s() bus timeout\n", __func__);
  151. ret = SAA_ERR_NO_RESOURCES;
  152. goto out;
  153. }
  154. /* TODO: Review this delay, efficient? */
  155. /* Wait, allowing the hardware fetch time */
  156. mdelay(1);
  157. /* Check the space usage again */
  158. curr_srp = saa7164_readl(bus->m_dwSetReadPos);
  159. /* Deal with ring wrapping issues */
  160. if (curr_srp > curr_swp)
  161. /* Deal with the wrapped ring */
  162. free_write_space = curr_srp - curr_swp;
  163. else
  164. /* Read didn't wrap around the buffer */
  165. free_write_space = (curr_srp + bus->m_dwSizeSetRing) -
  166. curr_swp;
  167. }
  168. /* Calculate the new write position */
  169. new_swp = curr_swp + bytes_to_write;
  170. dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
  171. dprintk(DBGLVL_BUS, "%s() bus->m_dwSizeSetRing = %x\n", __func__,
  172. bus->m_dwSizeSetRing);
  173. /*
  174. * Make a copy of msg->size before it is converted to le16 since it is
  175. * used in the code below.
  176. */
  177. size = msg->size;
  178. /* Convert to le16/le32 */
  179. msg->size = (__force u16)cpu_to_le16(msg->size);
  180. msg->command = (__force u32)cpu_to_le32(msg->command);
  181. msg->controlselector = (__force u16)cpu_to_le16(msg->controlselector);
  182. /* Mental Note: line 462 tmmhComResBusPCIe.cpp */
  183. /* Check if we're going to wrap again */
  184. if (new_swp > bus->m_dwSizeSetRing) {
  185. /* Ring wraps */
  186. new_swp -= bus->m_dwSizeSetRing;
  187. space_rem = bus->m_dwSizeSetRing - curr_swp;
  188. dprintk(DBGLVL_BUS, "%s() space_rem = %x\n", __func__,
  189. space_rem);
  190. dprintk(DBGLVL_BUS, "%s() sizeof(*msg) = %d\n", __func__,
  191. (u32)sizeof(*msg));
  192. if (space_rem < sizeof(*msg)) {
  193. dprintk(DBGLVL_BUS, "%s() tr4\n", __func__);
  194. /* Split the msg into pieces as the ring wraps */
  195. memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, space_rem);
  196. memcpy_toio(bus->m_pdwSetRing, (u8 *)msg + space_rem,
  197. sizeof(*msg) - space_rem);
  198. memcpy_toio(bus->m_pdwSetRing + sizeof(*msg) - space_rem,
  199. buf, size);
  200. } else if (space_rem == sizeof(*msg)) {
  201. dprintk(DBGLVL_BUS, "%s() tr5\n", __func__);
  202. /* Additional data at the beginning of the ring */
  203. memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  204. memcpy_toio(bus->m_pdwSetRing, buf, size);
  205. } else {
  206. /* Additional data wraps around the ring */
  207. memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  208. if (size > 0) {
  209. memcpy_toio(bus->m_pdwSetRing + curr_swp +
  210. sizeof(*msg), buf, space_rem -
  211. sizeof(*msg));
  212. memcpy_toio(bus->m_pdwSetRing, (u8 *)buf +
  213. space_rem - sizeof(*msg),
  214. bytes_to_write - space_rem);
  215. }
  216. }
  217. } /* (new_swp > bus->m_dwSizeSetRing) */
  218. else {
  219. dprintk(DBGLVL_BUS, "%s() tr6\n", __func__);
  220. /* The ring buffer doesn't wrap, two simple copies */
  221. memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  222. memcpy_toio(bus->m_pdwSetRing + curr_swp + sizeof(*msg), buf,
  223. size);
  224. }
  225. dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
  226. /* Update the bus write position */
  227. saa7164_writel(bus->m_dwSetWritePos, new_swp);
  228. /* Convert back to cpu after writing the msg to the ringbuffer. */
  229. msg->size = le16_to_cpu((__force __le16)msg->size);
  230. msg->command = le32_to_cpu((__force __le32)msg->command);
  231. msg->controlselector = le16_to_cpu((__force __le16)msg->controlselector);
  232. ret = SAA_OK;
  233. out:
  234. saa7164_bus_dump(dev);
  235. mutex_unlock(&bus->lock);
  236. saa7164_bus_verify(dev);
  237. return ret;
  238. }
  239. /*
  240. * Receive a command or a response from the bus. The implementation does not
  241. * know if it is a command or a response it simply dequeues the data,
  242. * depending on the bus information given in the struct tmComResBusInfo
  243. * structure.
  244. *
  245. * Return Value:
  246. * 0 The function executed successfully.
  247. * < 0 One or more members are not initialized.
  248. */
  249. int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
  250. void *buf, int peekonly)
  251. {
  252. struct tmComResBusInfo *bus = &dev->bus;
  253. u32 bytes_to_read, write_distance, curr_grp, curr_gwp,
  254. new_grp, buf_size, space_rem;
  255. struct tmComResInfo msg_tmp;
  256. int ret = SAA_ERR_BAD_PARAMETER;
  257. saa7164_bus_verify(dev);
  258. if (msg == NULL)
  259. return ret;
  260. if (msg->size > dev->bus.m_wMaxReqSize) {
  261. printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
  262. __func__);
  263. return ret;
  264. }
  265. if ((peekonly == 0) && (msg->size > 0) && (buf == NULL)) {
  266. printk(KERN_ERR
  267. "%s() Missing msg buf, size should be %d bytes\n",
  268. __func__, msg->size);
  269. return ret;
  270. }
  271. mutex_lock(&bus->lock);
  272. /* Peek the bus to see if a msg exists, if it's not what we're expecting
  273. * then return cleanly else read the message from the bus.
  274. */
  275. curr_gwp = saa7164_readl(bus->m_dwGetWritePos);
  276. curr_grp = saa7164_readl(bus->m_dwGetReadPos);
  277. if (curr_gwp == curr_grp) {
  278. ret = SAA_ERR_EMPTY;
  279. goto out;
  280. }
  281. bytes_to_read = sizeof(*msg);
  282. /* Calculate write distance to current read position */
  283. write_distance = 0;
  284. if (curr_gwp >= curr_grp)
  285. /* Write doesn't wrap around the ring */
  286. write_distance = curr_gwp - curr_grp;
  287. else
  288. /* Write wraps around the ring */
  289. write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
  290. if (bytes_to_read > write_distance) {
  291. printk(KERN_ERR "%s() No message/response found\n", __func__);
  292. ret = SAA_ERR_INVALID_COMMAND;
  293. goto out;
  294. }
  295. /* Calculate the new read position */
  296. new_grp = curr_grp + bytes_to_read;
  297. if (new_grp > bus->m_dwSizeGetRing) {
  298. /* Ring wraps */
  299. new_grp -= bus->m_dwSizeGetRing;
  300. space_rem = bus->m_dwSizeGetRing - curr_grp;
  301. memcpy_fromio(&msg_tmp, bus->m_pdwGetRing + curr_grp, space_rem);
  302. memcpy_fromio((u8 *)&msg_tmp + space_rem, bus->m_pdwGetRing,
  303. bytes_to_read - space_rem);
  304. } else {
  305. /* No wrapping */
  306. memcpy_fromio(&msg_tmp, bus->m_pdwGetRing + curr_grp, bytes_to_read);
  307. }
  308. /* Convert from little endian to CPU */
  309. msg_tmp.size = le16_to_cpu((__force __le16)msg_tmp.size);
  310. msg_tmp.command = le32_to_cpu((__force __le32)msg_tmp.command);
  311. msg_tmp.controlselector = le16_to_cpu((__force __le16)msg_tmp.controlselector);
  312. memcpy(msg, &msg_tmp, sizeof(*msg));
  313. /* No need to update the read positions, because this was a peek */
  314. /* If the caller specifically want to peek, return */
  315. if (peekonly) {
  316. goto peekout;
  317. }
  318. /* Check if the command/response matches what is expected */
  319. if ((msg_tmp.id != msg->id) || (msg_tmp.command != msg->command) ||
  320. (msg_tmp.controlselector != msg->controlselector) ||
  321. (msg_tmp.seqno != msg->seqno) || (msg_tmp.size != msg->size)) {
  322. printk(KERN_ERR "%s() Unexpected msg miss-match\n", __func__);
  323. saa7164_bus_dumpmsg(dev, msg, buf);
  324. saa7164_bus_dumpmsg(dev, &msg_tmp, NULL);
  325. ret = SAA_ERR_INVALID_COMMAND;
  326. goto out;
  327. }
  328. /* Get the actual command and response from the bus */
  329. buf_size = msg->size;
  330. bytes_to_read = sizeof(*msg) + msg->size;
  331. /* Calculate write distance to current read position */
  332. write_distance = 0;
  333. if (curr_gwp >= curr_grp)
  334. /* Write doesn't wrap around the ring */
  335. write_distance = curr_gwp - curr_grp;
  336. else
  337. /* Write wraps around the ring */
  338. write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
  339. if (bytes_to_read > write_distance) {
  340. printk(KERN_ERR "%s() Invalid bus state, missing msg or mangled ring, faulty H/W / bad code?\n",
  341. __func__);
  342. ret = SAA_ERR_INVALID_COMMAND;
  343. goto out;
  344. }
  345. /* Calculate the new read position */
  346. new_grp = curr_grp + bytes_to_read;
  347. if (new_grp > bus->m_dwSizeGetRing) {
  348. /* Ring wraps */
  349. new_grp -= bus->m_dwSizeGetRing;
  350. space_rem = bus->m_dwSizeGetRing - curr_grp;
  351. if (space_rem < sizeof(*msg)) {
  352. if (buf)
  353. memcpy_fromio(buf, bus->m_pdwGetRing + sizeof(*msg) -
  354. space_rem, buf_size);
  355. } else if (space_rem == sizeof(*msg)) {
  356. if (buf)
  357. memcpy_fromio(buf, bus->m_pdwGetRing, buf_size);
  358. } else {
  359. /* Additional data wraps around the ring */
  360. if (buf) {
  361. memcpy_fromio(buf, bus->m_pdwGetRing + curr_grp +
  362. sizeof(*msg), space_rem - sizeof(*msg));
  363. memcpy_fromio(buf + space_rem - sizeof(*msg),
  364. bus->m_pdwGetRing, bytes_to_read -
  365. space_rem);
  366. }
  367. }
  368. } else {
  369. /* No wrapping */
  370. if (buf)
  371. memcpy_fromio(buf, bus->m_pdwGetRing + curr_grp + sizeof(*msg),
  372. buf_size);
  373. }
  374. /* Update the read positions, adjusting the ring */
  375. saa7164_writel(bus->m_dwGetReadPos, new_grp);
  376. peekout:
  377. ret = SAA_OK;
  378. out:
  379. mutex_unlock(&bus->lock);
  380. saa7164_bus_verify(dev);
  381. return ret;
  382. }