ps3-vuart.c 30 KB

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