ps3-vuart.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. * PS3 virtual uart
  3. *
  4. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  5. * Copyright 2006 Sony Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  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. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/bitops.h>
  25. #include <asm/ps3.h>
  26. #include <asm/firmware.h>
  27. #include <asm/lv1call.h>
  28. #include "vuart.h"
  29. MODULE_AUTHOR("Sony Corporation");
  30. MODULE_LICENSE("GPL v2");
  31. MODULE_DESCRIPTION("PS3 vuart");
  32. /**
  33. * vuart - An inter-partition data link service.
  34. * port 0: PS3 AV Settings.
  35. * port 2: PS3 System Manager.
  36. *
  37. * The vuart provides a bi-directional byte stream data link between logical
  38. * partitions. Its primary role is as a communications link between the guest
  39. * OS and the system policy module. The current HV does not support any
  40. * connections other than those listed.
  41. */
  42. enum {PORT_COUNT = 3,};
  43. enum vuart_param {
  44. PARAM_TX_TRIGGER = 0,
  45. PARAM_RX_TRIGGER = 1,
  46. PARAM_INTERRUPT_MASK = 2,
  47. PARAM_RX_BUF_SIZE = 3, /* read only */
  48. PARAM_RX_BYTES = 4, /* read only */
  49. PARAM_TX_BUF_SIZE = 5, /* read only */
  50. PARAM_TX_BYTES = 6, /* read only */
  51. PARAM_INTERRUPT_STATUS = 7, /* read only */
  52. };
  53. enum vuart_interrupt_bit {
  54. INTERRUPT_BIT_TX = 0,
  55. INTERRUPT_BIT_RX = 1,
  56. INTERRUPT_BIT_DISCONNECT = 2,
  57. };
  58. enum vuart_interrupt_mask {
  59. INTERRUPT_MASK_TX = 1,
  60. INTERRUPT_MASK_RX = 2,
  61. INTERRUPT_MASK_DISCONNECT = 4,
  62. };
  63. /**
  64. * struct ps3_vuart_port_priv - private vuart device data.
  65. */
  66. struct ps3_vuart_port_priv {
  67. u64 interrupt_mask;
  68. struct {
  69. spinlock_t lock;
  70. struct list_head head;
  71. } tx_list;
  72. struct {
  73. struct ps3_vuart_work work;
  74. unsigned long bytes_held;
  75. spinlock_t lock;
  76. struct list_head head;
  77. } rx_list;
  78. struct ps3_vuart_stats stats;
  79. };
  80. static struct ps3_vuart_port_priv *to_port_priv(
  81. struct ps3_system_bus_device *dev)
  82. {
  83. BUG_ON(!dev);
  84. BUG_ON(!dev->driver_priv);
  85. return (struct ps3_vuart_port_priv *)dev->driver_priv;
  86. }
  87. /**
  88. * struct ports_bmp - bitmap indicating ports needing service.
  89. *
  90. * A 256 bit read only bitmap indicating ports needing service. Do not write
  91. * to these bits. Must not cross a page boundary.
  92. */
  93. struct ports_bmp {
  94. u64 status;
  95. u64 unused[3];
  96. } __attribute__((aligned(32)));
  97. #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
  98. static void __maybe_unused _dump_ports_bmp(
  99. const struct ports_bmp *bmp, const char *func, int line)
  100. {
  101. pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);
  102. }
  103. #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
  104. static void __maybe_unused _dump_port_params(unsigned int port_number,
  105. const char *func, int line)
  106. {
  107. #if defined(DEBUG)
  108. static const char *strings[] = {
  109. "tx_trigger ",
  110. "rx_trigger ",
  111. "interrupt_mask ",
  112. "rx_buf_size ",
  113. "rx_bytes ",
  114. "tx_buf_size ",
  115. "tx_bytes ",
  116. "interrupt_status",
  117. };
  118. int result;
  119. unsigned int i;
  120. u64 value;
  121. for (i = 0; i < ARRAY_SIZE(strings); i++) {
  122. result = lv1_get_virtual_uart_param(port_number, i, &value);
  123. if (result) {
  124. pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
  125. port_number, strings[i], ps3_result(result));
  126. continue;
  127. }
  128. pr_debug("%s:%d: port_%u: %s = %lxh\n",
  129. func, line, port_number, strings[i], value);
  130. }
  131. #endif
  132. }
  133. struct vuart_triggers {
  134. unsigned long rx;
  135. unsigned long tx;
  136. };
  137. int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
  138. struct vuart_triggers *trig)
  139. {
  140. int result;
  141. u64 size;
  142. u64 val;
  143. u64 tx;
  144. result = lv1_get_virtual_uart_param(dev->port_number,
  145. PARAM_TX_TRIGGER, &tx);
  146. trig->tx = tx;
  147. if (result) {
  148. dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
  149. __func__, __LINE__, ps3_result(result));
  150. return result;
  151. }
  152. result = lv1_get_virtual_uart_param(dev->port_number,
  153. PARAM_RX_BUF_SIZE, &size);
  154. if (result) {
  155. dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
  156. __func__, __LINE__, ps3_result(result));
  157. return result;
  158. }
  159. result = lv1_get_virtual_uart_param(dev->port_number,
  160. PARAM_RX_TRIGGER, &val);
  161. if (result) {
  162. dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
  163. __func__, __LINE__, ps3_result(result));
  164. return result;
  165. }
  166. trig->rx = size - val;
  167. dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
  168. trig->tx, trig->rx);
  169. return result;
  170. }
  171. int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
  172. unsigned int rx)
  173. {
  174. int result;
  175. u64 size;
  176. result = lv1_set_virtual_uart_param(dev->port_number,
  177. PARAM_TX_TRIGGER, tx);
  178. if (result) {
  179. dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
  180. __func__, __LINE__, ps3_result(result));
  181. return result;
  182. }
  183. result = lv1_get_virtual_uart_param(dev->port_number,
  184. PARAM_RX_BUF_SIZE, &size);
  185. if (result) {
  186. dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
  187. __func__, __LINE__, ps3_result(result));
  188. return result;
  189. }
  190. result = lv1_set_virtual_uart_param(dev->port_number,
  191. PARAM_RX_TRIGGER, size - rx);
  192. if (result) {
  193. dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
  194. __func__, __LINE__, ps3_result(result));
  195. return result;
  196. }
  197. dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
  198. tx, rx);
  199. return result;
  200. }
  201. static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
  202. u64 *bytes_waiting)
  203. {
  204. int result;
  205. result = lv1_get_virtual_uart_param(dev->port_number,
  206. PARAM_RX_BYTES, bytes_waiting);
  207. if (result)
  208. dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
  209. __func__, __LINE__, ps3_result(result));
  210. dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
  211. *bytes_waiting);
  212. return result;
  213. }
  214. /**
  215. * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
  216. * @dev: The struct ps3_system_bus_device instance.
  217. * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
  218. */
  219. static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
  220. unsigned long mask)
  221. {
  222. int result;
  223. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  224. dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
  225. priv->interrupt_mask = mask;
  226. result = lv1_set_virtual_uart_param(dev->port_number,
  227. PARAM_INTERRUPT_MASK, priv->interrupt_mask);
  228. if (result)
  229. dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
  230. __func__, __LINE__, ps3_result(result));
  231. return result;
  232. }
  233. static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
  234. unsigned long *status)
  235. {
  236. int result;
  237. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  238. u64 tmp;
  239. result = lv1_get_virtual_uart_param(dev->port_number,
  240. PARAM_INTERRUPT_STATUS, &tmp);
  241. if (result)
  242. dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
  243. __func__, __LINE__, ps3_result(result));
  244. *status = tmp & priv->interrupt_mask;
  245. dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
  246. __func__, __LINE__, priv->interrupt_mask, tmp, *status);
  247. return result;
  248. }
  249. int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
  250. {
  251. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  252. return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
  253. : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  254. | INTERRUPT_MASK_TX);
  255. }
  256. int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
  257. {
  258. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  259. return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
  260. : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  261. | INTERRUPT_MASK_RX);
  262. }
  263. int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
  264. {
  265. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  266. return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
  267. : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  268. | INTERRUPT_MASK_DISCONNECT);
  269. }
  270. int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
  271. {
  272. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  273. return (priv->interrupt_mask & INTERRUPT_MASK_TX)
  274. ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  275. & ~INTERRUPT_MASK_TX) : 0;
  276. }
  277. int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
  278. {
  279. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  280. return (priv->interrupt_mask & INTERRUPT_MASK_RX)
  281. ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  282. & ~INTERRUPT_MASK_RX) : 0;
  283. }
  284. int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
  285. {
  286. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  287. return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
  288. ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  289. & ~INTERRUPT_MASK_DISCONNECT) : 0;
  290. }
  291. /**
  292. * ps3_vuart_raw_write - Low level write helper.
  293. * @dev: The struct ps3_system_bus_device instance.
  294. *
  295. * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
  296. */
  297. static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
  298. const void *buf, unsigned int bytes, u64 *bytes_written)
  299. {
  300. int result;
  301. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  302. result = lv1_write_virtual_uart(dev->port_number,
  303. ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
  304. if (result) {
  305. dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
  306. "%s\n", __func__, __LINE__, ps3_result(result));
  307. return result;
  308. }
  309. priv->stats.bytes_written += *bytes_written;
  310. dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
  311. *bytes_written, bytes, priv->stats.bytes_written);
  312. return result;
  313. }
  314. /**
  315. * ps3_vuart_raw_read - Low level read helper.
  316. * @dev: The struct ps3_system_bus_device instance.
  317. *
  318. * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
  319. */
  320. static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
  321. unsigned int bytes, u64 *bytes_read)
  322. {
  323. int result;
  324. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  325. dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
  326. result = lv1_read_virtual_uart(dev->port_number,
  327. ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
  328. if (result) {
  329. dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
  330. __func__, __LINE__, ps3_result(result));
  331. return result;
  332. }
  333. priv->stats.bytes_read += *bytes_read;
  334. dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
  335. *bytes_read, bytes, priv->stats.bytes_read);
  336. return result;
  337. }
  338. /**
  339. * ps3_vuart_clear_rx_bytes - Discard bytes received.
  340. * @dev: The struct ps3_system_bus_device instance.
  341. * @bytes: Max byte count to discard, zero = all pending.
  342. *
  343. * Used to clear pending rx interrupt source. Will not block.
  344. */
  345. void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
  346. unsigned int bytes)
  347. {
  348. int result;
  349. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  350. u64 bytes_waiting;
  351. void *tmp;
  352. result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
  353. BUG_ON(result);
  354. bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
  355. dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
  356. if (!bytes)
  357. return;
  358. /* Add some extra space for recently arrived data. */
  359. bytes += 128;
  360. tmp = kmalloc(bytes, GFP_KERNEL);
  361. if (!tmp)
  362. return;
  363. ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
  364. kfree(tmp);
  365. /* Don't include these bytes in the stats. */
  366. priv->stats.bytes_read -= bytes_waiting;
  367. }
  368. EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
  369. /**
  370. * struct list_buffer - An element for a port device fifo buffer list.
  371. */
  372. struct list_buffer {
  373. struct list_head link;
  374. const unsigned char *head;
  375. const unsigned char *tail;
  376. unsigned long dbg_number;
  377. unsigned char data[];
  378. };
  379. /**
  380. * ps3_vuart_write - the entry point for writing data to a port
  381. * @dev: The struct ps3_system_bus_device instance.
  382. *
  383. * If the port is idle on entry as much of the incoming data is written to
  384. * the port as the port will accept. Otherwise a list buffer is created
  385. * and any remaning incoming data is copied to that buffer. The buffer is
  386. * then enqueued for transmision via the transmit interrupt.
  387. */
  388. int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
  389. unsigned int bytes)
  390. {
  391. static unsigned long dbg_number;
  392. int result;
  393. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  394. unsigned long flags;
  395. struct list_buffer *lb;
  396. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  397. bytes, bytes);
  398. spin_lock_irqsave(&priv->tx_list.lock, flags);
  399. if (list_empty(&priv->tx_list.head)) {
  400. u64 bytes_written;
  401. result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
  402. spin_unlock_irqrestore(&priv->tx_list.lock, flags);
  403. if (result) {
  404. dev_dbg(&dev->core,
  405. "%s:%d: ps3_vuart_raw_write failed\n",
  406. __func__, __LINE__);
  407. return result;
  408. }
  409. if (bytes_written == bytes) {
  410. dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
  411. __func__, __LINE__, bytes);
  412. return 0;
  413. }
  414. bytes -= bytes_written;
  415. buf += bytes_written;
  416. } else
  417. spin_unlock_irqrestore(&priv->tx_list.lock, flags);
  418. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
  419. if (!lb)
  420. return -ENOMEM;
  421. memcpy(lb->data, buf, bytes);
  422. lb->head = lb->data;
  423. lb->tail = lb->data + bytes;
  424. lb->dbg_number = ++dbg_number;
  425. spin_lock_irqsave(&priv->tx_list.lock, flags);
  426. list_add_tail(&lb->link, &priv->tx_list.head);
  427. ps3_vuart_enable_interrupt_tx(dev);
  428. spin_unlock_irqrestore(&priv->tx_list.lock, flags);
  429. dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
  430. __func__, __LINE__, lb->dbg_number, bytes);
  431. return 0;
  432. }
  433. EXPORT_SYMBOL_GPL(ps3_vuart_write);
  434. /**
  435. * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
  436. * @dev: The struct ps3_system_bus_device instance.
  437. * @bytes_queued: Number of bytes queued to the buffer list.
  438. *
  439. * Must be called with priv->rx_list.lock held.
  440. */
  441. static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
  442. u64 *bytes_queued)
  443. {
  444. static unsigned long dbg_number;
  445. int result;
  446. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  447. struct list_buffer *lb;
  448. u64 bytes;
  449. *bytes_queued = 0;
  450. result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
  451. BUG_ON(result);
  452. if (result)
  453. return -EIO;
  454. if (!bytes)
  455. return 0;
  456. /* Add some extra space for recently arrived data. */
  457. bytes += 128;
  458. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
  459. if (!lb)
  460. return -ENOMEM;
  461. ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
  462. lb->head = lb->data;
  463. lb->tail = lb->data + bytes;
  464. lb->dbg_number = ++dbg_number;
  465. list_add_tail(&lb->link, &priv->rx_list.head);
  466. priv->rx_list.bytes_held += bytes;
  467. dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
  468. __func__, __LINE__, lb->dbg_number, bytes);
  469. *bytes_queued = bytes;
  470. return 0;
  471. }
  472. /**
  473. * ps3_vuart_read - The entry point for reading data from a port.
  474. *
  475. * Queue data waiting at the port, and if enough bytes to satisfy the request
  476. * are held in the buffer list those bytes are dequeued and copied to the
  477. * caller's buffer. Emptied list buffers are retiered. If the request cannot
  478. * be statified by bytes held in the list buffers -EAGAIN is returned.
  479. */
  480. int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
  481. unsigned int bytes)
  482. {
  483. int result;
  484. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  485. unsigned long flags;
  486. struct list_buffer *lb, *n;
  487. unsigned long bytes_read;
  488. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  489. bytes, bytes);
  490. spin_lock_irqsave(&priv->rx_list.lock, flags);
  491. /* Queue rx bytes here for polled reads. */
  492. while (priv->rx_list.bytes_held < bytes) {
  493. u64 tmp;
  494. result = ps3_vuart_queue_rx_bytes(dev, &tmp);
  495. if (result || !tmp) {
  496. dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
  497. __func__, __LINE__,
  498. bytes - priv->rx_list.bytes_held);
  499. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  500. return -EAGAIN;
  501. }
  502. }
  503. list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
  504. bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
  505. memcpy(buf, lb->head, bytes_read);
  506. buf += bytes_read;
  507. bytes -= bytes_read;
  508. priv->rx_list.bytes_held -= bytes_read;
  509. if (bytes_read < lb->tail - lb->head) {
  510. lb->head += bytes_read;
  511. dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
  512. "bytes\n", __func__, __LINE__, lb->dbg_number,
  513. bytes_read);
  514. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  515. return 0;
  516. }
  517. dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
  518. "bytes\n", __func__, __LINE__, lb->dbg_number,
  519. bytes_read);
  520. list_del(&lb->link);
  521. kfree(lb);
  522. }
  523. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  524. return 0;
  525. }
  526. EXPORT_SYMBOL_GPL(ps3_vuart_read);
  527. /**
  528. * ps3_vuart_work - Asynchronous read handler.
  529. */
  530. static void ps3_vuart_work(struct work_struct *work)
  531. {
  532. struct ps3_system_bus_device *dev =
  533. ps3_vuart_work_to_system_bus_dev(work);
  534. struct ps3_vuart_port_driver *drv =
  535. ps3_system_bus_dev_to_vuart_drv(dev);
  536. BUG_ON(!drv);
  537. drv->work(dev);
  538. }
  539. int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
  540. {
  541. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  542. unsigned long flags;
  543. if (priv->rx_list.work.trigger) {
  544. dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
  545. __func__, __LINE__);
  546. return -EAGAIN;
  547. }
  548. BUG_ON(!bytes);
  549. PREPARE_WORK(&priv->rx_list.work.work, ps3_vuart_work);
  550. spin_lock_irqsave(&priv->rx_list.lock, flags);
  551. if (priv->rx_list.bytes_held >= bytes) {
  552. dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
  553. __func__, __LINE__, bytes);
  554. schedule_work(&priv->rx_list.work.work);
  555. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  556. return 0;
  557. }
  558. priv->rx_list.work.trigger = bytes;
  559. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  560. dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
  561. __LINE__, bytes, bytes);
  562. return 0;
  563. }
  564. EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
  565. void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
  566. {
  567. to_port_priv(dev)->rx_list.work.trigger = 0;
  568. }
  569. EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
  570. /**
  571. * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
  572. *
  573. * Services the transmit interrupt for the port. Writes as much data from the
  574. * buffer list as the port will accept. Retires any emptied list buffers and
  575. * adjusts the final list buffer state for a partial write.
  576. */
  577. static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
  578. {
  579. int result = 0;
  580. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  581. unsigned long flags;
  582. struct list_buffer *lb, *n;
  583. unsigned long bytes_total = 0;
  584. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  585. spin_lock_irqsave(&priv->tx_list.lock, flags);
  586. list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
  587. u64 bytes_written;
  588. result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
  589. &bytes_written);
  590. if (result) {
  591. dev_dbg(&dev->core,
  592. "%s:%d: ps3_vuart_raw_write failed\n",
  593. __func__, __LINE__);
  594. break;
  595. }
  596. bytes_total += bytes_written;
  597. if (bytes_written < lb->tail - lb->head) {
  598. lb->head += bytes_written;
  599. dev_dbg(&dev->core,
  600. "%s:%d cleared buf_%lu, %llxh bytes\n",
  601. __func__, __LINE__, lb->dbg_number,
  602. bytes_written);
  603. goto port_full;
  604. }
  605. dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
  606. lb->dbg_number);
  607. list_del(&lb->link);
  608. kfree(lb);
  609. }
  610. ps3_vuart_disable_interrupt_tx(dev);
  611. port_full:
  612. spin_unlock_irqrestore(&priv->tx_list.lock, flags);
  613. dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
  614. __func__, __LINE__, bytes_total);
  615. return result;
  616. }
  617. /**
  618. * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
  619. *
  620. * Services the receive interrupt for the port. Creates a list buffer and
  621. * copies all waiting port data to that buffer and enqueues the buffer in the
  622. * buffer list. Buffer list data is dequeued via ps3_vuart_read.
  623. */
  624. static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
  625. {
  626. int result;
  627. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  628. unsigned long flags;
  629. u64 bytes;
  630. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  631. spin_lock_irqsave(&priv->rx_list.lock, flags);
  632. result = ps3_vuart_queue_rx_bytes(dev, &bytes);
  633. if (result) {
  634. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  635. return result;
  636. }
  637. if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
  638. >= priv->rx_list.work.trigger) {
  639. dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
  640. __func__, __LINE__, priv->rx_list.work.trigger);
  641. priv->rx_list.work.trigger = 0;
  642. schedule_work(&priv->rx_list.work.work);
  643. }
  644. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  645. return result;
  646. }
  647. static int ps3_vuart_handle_interrupt_disconnect(
  648. struct ps3_system_bus_device *dev)
  649. {
  650. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  651. BUG_ON("no support");
  652. return -1;
  653. }
  654. /**
  655. * ps3_vuart_handle_port_interrupt - second stage interrupt handler
  656. *
  657. * Services any pending interrupt types for the port. Passes control to the
  658. * third stage type specific interrupt handler. Returns control to the first
  659. * stage handler after one iteration.
  660. */
  661. static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
  662. {
  663. int result;
  664. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  665. unsigned long status;
  666. result = ps3_vuart_get_interrupt_status(dev, &status);
  667. if (result)
  668. return result;
  669. dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
  670. status);
  671. if (status & INTERRUPT_MASK_DISCONNECT) {
  672. priv->stats.disconnect_interrupts++;
  673. result = ps3_vuart_handle_interrupt_disconnect(dev);
  674. if (result)
  675. ps3_vuart_disable_interrupt_disconnect(dev);
  676. }
  677. if (status & INTERRUPT_MASK_TX) {
  678. priv->stats.tx_interrupts++;
  679. result = ps3_vuart_handle_interrupt_tx(dev);
  680. if (result)
  681. ps3_vuart_disable_interrupt_tx(dev);
  682. }
  683. if (status & INTERRUPT_MASK_RX) {
  684. priv->stats.rx_interrupts++;
  685. result = ps3_vuart_handle_interrupt_rx(dev);
  686. if (result)
  687. ps3_vuart_disable_interrupt_rx(dev);
  688. }
  689. return 0;
  690. }
  691. struct vuart_bus_priv {
  692. struct ports_bmp *bmp;
  693. unsigned int virq;
  694. struct mutex probe_mutex;
  695. int use_count;
  696. struct ps3_system_bus_device *devices[PORT_COUNT];
  697. } static vuart_bus_priv;
  698. /**
  699. * ps3_vuart_irq_handler - first stage interrupt handler
  700. *
  701. * Loops finding any interrupting port and its associated instance data.
  702. * Passes control to the second stage port specific interrupt handler. Loops
  703. * until all outstanding interrupts are serviced.
  704. */
  705. static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
  706. {
  707. struct vuart_bus_priv *bus_priv = _private;
  708. BUG_ON(!bus_priv);
  709. while (1) {
  710. unsigned int port;
  711. dump_ports_bmp(bus_priv->bmp);
  712. port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
  713. if (port == BITS_PER_LONG)
  714. break;
  715. BUG_ON(port >= PORT_COUNT);
  716. BUG_ON(!bus_priv->devices[port]);
  717. ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
  718. }
  719. return IRQ_HANDLED;
  720. }
  721. static int ps3_vuart_bus_interrupt_get(void)
  722. {
  723. int result;
  724. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  725. vuart_bus_priv.use_count++;
  726. BUG_ON(vuart_bus_priv.use_count > 2);
  727. if (vuart_bus_priv.use_count != 1)
  728. return 0;
  729. BUG_ON(vuart_bus_priv.bmp);
  730. vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
  731. if (!vuart_bus_priv.bmp) {
  732. pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__);
  733. result = -ENOMEM;
  734. goto fail_bmp_malloc;
  735. }
  736. result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
  737. &vuart_bus_priv.virq);
  738. if (result) {
  739. pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
  740. __func__, __LINE__, result);
  741. result = -EPERM;
  742. goto fail_alloc_irq;
  743. }
  744. result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
  745. IRQF_DISABLED, "vuart", &vuart_bus_priv);
  746. if (result) {
  747. pr_debug("%s:%d: request_irq failed (%d)\n",
  748. __func__, __LINE__, result);
  749. goto fail_request_irq;
  750. }
  751. pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
  752. return result;
  753. fail_request_irq:
  754. ps3_vuart_irq_destroy(vuart_bus_priv.virq);
  755. vuart_bus_priv.virq = NO_IRQ;
  756. fail_alloc_irq:
  757. kfree(vuart_bus_priv.bmp);
  758. vuart_bus_priv.bmp = NULL;
  759. fail_bmp_malloc:
  760. vuart_bus_priv.use_count--;
  761. pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
  762. return result;
  763. }
  764. static int ps3_vuart_bus_interrupt_put(void)
  765. {
  766. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  767. vuart_bus_priv.use_count--;
  768. BUG_ON(vuart_bus_priv.use_count < 0);
  769. if (vuart_bus_priv.use_count != 0)
  770. return 0;
  771. free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
  772. ps3_vuart_irq_destroy(vuart_bus_priv.virq);
  773. vuart_bus_priv.virq = NO_IRQ;
  774. kfree(vuart_bus_priv.bmp);
  775. vuart_bus_priv.bmp = NULL;
  776. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  777. return 0;
  778. }
  779. static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
  780. {
  781. int result;
  782. struct ps3_vuart_port_driver *drv;
  783. struct ps3_vuart_port_priv *priv = NULL;
  784. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  785. drv = ps3_system_bus_dev_to_vuart_drv(dev);
  786. dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
  787. drv->core.core.name);
  788. BUG_ON(!drv);
  789. if (dev->port_number >= PORT_COUNT) {
  790. BUG();
  791. return -EINVAL;
  792. }
  793. mutex_lock(&vuart_bus_priv.probe_mutex);
  794. result = ps3_vuart_bus_interrupt_get();
  795. if (result)
  796. goto fail_setup_interrupt;
  797. if (vuart_bus_priv.devices[dev->port_number]) {
  798. dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
  799. __LINE__, dev->port_number);
  800. result = -EBUSY;
  801. goto fail_busy;
  802. }
  803. vuart_bus_priv.devices[dev->port_number] = dev;
  804. /* Setup dev->driver_priv. */
  805. dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
  806. GFP_KERNEL);
  807. if (!dev->driver_priv) {
  808. result = -ENOMEM;
  809. goto fail_dev_malloc;
  810. }
  811. priv = to_port_priv(dev);
  812. INIT_LIST_HEAD(&priv->tx_list.head);
  813. spin_lock_init(&priv->tx_list.lock);
  814. INIT_LIST_HEAD(&priv->rx_list.head);
  815. spin_lock_init(&priv->rx_list.lock);
  816. INIT_WORK(&priv->rx_list.work.work, NULL);
  817. priv->rx_list.work.trigger = 0;
  818. priv->rx_list.work.dev = dev;
  819. /* clear stale pending interrupts */
  820. ps3_vuart_clear_rx_bytes(dev, 0);
  821. ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
  822. ps3_vuart_set_triggers(dev, 1, 1);
  823. if (drv->probe)
  824. result = drv->probe(dev);
  825. else {
  826. result = 0;
  827. dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
  828. __LINE__);
  829. }
  830. if (result) {
  831. dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
  832. __func__, __LINE__);
  833. goto fail_probe;
  834. }
  835. mutex_unlock(&vuart_bus_priv.probe_mutex);
  836. return result;
  837. fail_probe:
  838. ps3_vuart_set_interrupt_mask(dev, 0);
  839. kfree(dev->driver_priv);
  840. dev->driver_priv = NULL;
  841. fail_dev_malloc:
  842. vuart_bus_priv.devices[dev->port_number] = NULL;
  843. fail_busy:
  844. ps3_vuart_bus_interrupt_put();
  845. fail_setup_interrupt:
  846. mutex_unlock(&vuart_bus_priv.probe_mutex);
  847. dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
  848. return result;
  849. }
  850. /**
  851. * ps3_vuart_cleanup - common cleanup helper.
  852. * @dev: The struct ps3_system_bus_device instance.
  853. *
  854. * Cleans interrupts and HV resources. Must be called with
  855. * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
  856. * ps3_vuart_shutdown. After this call, polled reading will still work.
  857. */
  858. static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
  859. {
  860. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  861. ps3_vuart_cancel_async(dev);
  862. ps3_vuart_set_interrupt_mask(dev, 0);
  863. ps3_vuart_bus_interrupt_put();
  864. return 0;
  865. }
  866. /**
  867. * ps3_vuart_remove - Completely clean the device instance.
  868. * @dev: The struct ps3_system_bus_device instance.
  869. *
  870. * Cleans all memory, interrupts and HV resources. After this call the
  871. * device can no longer be used.
  872. */
  873. static int ps3_vuart_remove(struct ps3_system_bus_device *dev)
  874. {
  875. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  876. struct ps3_vuart_port_driver *drv;
  877. BUG_ON(!dev);
  878. mutex_lock(&vuart_bus_priv.probe_mutex);
  879. dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
  880. dev->match_id);
  881. if (!dev->core.driver) {
  882. dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
  883. __LINE__);
  884. mutex_unlock(&vuart_bus_priv.probe_mutex);
  885. return 0;
  886. }
  887. drv = ps3_system_bus_dev_to_vuart_drv(dev);
  888. BUG_ON(!drv);
  889. if (drv->remove) {
  890. drv->remove(dev);
  891. } else {
  892. dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
  893. __LINE__);
  894. BUG();
  895. }
  896. ps3_vuart_cleanup(dev);
  897. vuart_bus_priv.devices[dev->port_number] = NULL;
  898. kfree(priv);
  899. priv = NULL;
  900. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  901. mutex_unlock(&vuart_bus_priv.probe_mutex);
  902. return 0;
  903. }
  904. /**
  905. * ps3_vuart_shutdown - Cleans interrupts and HV resources.
  906. * @dev: The struct ps3_system_bus_device instance.
  907. *
  908. * Cleans interrupts and HV resources. After this call the
  909. * device can still be used in polling mode. This behavior required
  910. * by sys-manager to be able to complete the device power operation
  911. * sequence.
  912. */
  913. static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
  914. {
  915. struct ps3_vuart_port_driver *drv;
  916. BUG_ON(!dev);
  917. mutex_lock(&vuart_bus_priv.probe_mutex);
  918. dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
  919. dev->match_id);
  920. if (!dev->core.driver) {
  921. dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
  922. __LINE__);
  923. mutex_unlock(&vuart_bus_priv.probe_mutex);
  924. return 0;
  925. }
  926. drv = ps3_system_bus_dev_to_vuart_drv(dev);
  927. BUG_ON(!drv);
  928. if (drv->shutdown)
  929. drv->shutdown(dev);
  930. else if (drv->remove) {
  931. dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
  932. __func__, __LINE__);
  933. drv->remove(dev);
  934. } else {
  935. dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
  936. __LINE__);
  937. BUG();
  938. }
  939. ps3_vuart_cleanup(dev);
  940. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  941. mutex_unlock(&vuart_bus_priv.probe_mutex);
  942. return 0;
  943. }
  944. static int __init ps3_vuart_bus_init(void)
  945. {
  946. pr_debug("%s:%d:\n", __func__, __LINE__);
  947. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  948. return -ENODEV;
  949. mutex_init(&vuart_bus_priv.probe_mutex);
  950. return 0;
  951. }
  952. static void __exit ps3_vuart_bus_exit(void)
  953. {
  954. pr_debug("%s:%d:\n", __func__, __LINE__);
  955. }
  956. core_initcall(ps3_vuart_bus_init);
  957. module_exit(ps3_vuart_bus_exit);
  958. /**
  959. * ps3_vuart_port_driver_register - Add a vuart port device driver.
  960. */
  961. int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
  962. {
  963. int result;
  964. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
  965. BUG_ON(!drv->core.match_id);
  966. BUG_ON(!drv->core.core.name);
  967. drv->core.probe = ps3_vuart_probe;
  968. drv->core.remove = ps3_vuart_remove;
  969. drv->core.shutdown = ps3_vuart_shutdown;
  970. result = ps3_system_bus_driver_register(&drv->core);
  971. return result;
  972. }
  973. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
  974. /**
  975. * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
  976. */
  977. void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
  978. {
  979. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
  980. ps3_system_bus_driver_unregister(&drv->core);
  981. }
  982. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);