ps3-vuart.c 30 KB

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