ps3-vuart.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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 = NO_IRQ;
  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 = NO_IRQ;
  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);