ps3-vuart.c 31 KB

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